monitor_checkStack(): fix pasto introduced in previous change. Spotted by R.Gianassi.
[bertos.git] / bertos / kern / monitor.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2008 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Monitor to check for stack overflows
34  *
35  * \version $Id$
36  * \author Giovanni Bajo <rasky@develer.com>
37  */
38
39
40 #include "monitor.h"
41
42 #if CONFIG_KERN_MONITOR
43
44 #include "proc_p.h"
45 #include <struct/list.h>
46 #include <drv/timer.h>
47 #include <kern/proc.h>
48 #include <cpu/frame.h> /* CPU_STACK_GROWS_UPWARD */
49 #include <cfg/macros.h>
50 #include <cfg/debug.h>
51
52
53 /* Access to this list must be protected against the scheduler */
54 static List MonitorProcs;
55
56
57 void monitor_init(void)
58 {
59         LIST_INIT(&MonitorProcs);
60 }
61
62
63 void monitor_add(Process *proc, const char *name)
64 {
65         proc->monitor.name = name;
66
67         PROC_ATOMIC(ADDTAIL(&MonitorProcs, &proc->monitor.link));
68 }
69
70
71 void monitor_remove(Process *proc)
72 {
73         PROC_ATOMIC(REMOVE(&proc->monitor.link));
74 }
75
76 void monitor_rename(Process *proc, const char *name)
77 {
78         proc->monitor.name = name;
79 }
80
81 size_t monitor_checkStack(cpustack_t *stack_base, size_t stack_size)
82 {
83         cpustack_t *beg;
84         cpustack_t *cur;
85         cpustack_t *end;
86         int inc;
87         size_t sp_free;
88
89
90         beg = stack_base;
91         end = stack_base + stack_size / sizeof(cpustack_t);
92         inc = +1;
93
94         if (CPU_STACK_GROWS_UPWARD)
95         {
96                 SWAP(beg, end);
97                 inc = -1;
98         }
99
100         cur = beg;
101         while (cur != end)
102         {
103                 if (*cur != CONFIG_KERN_STACKFILLCODE)
104                         break;
105
106                 cur += inc;
107         }
108
109         sp_free = ABS(cur - beg) * sizeof(cpustack_t);
110         return sp_free;
111 }
112
113
114 void monitor_report(void)
115 {
116         Node *node;
117         int i;
118
119         kprintf("%-8s%-8s%-8s%-8s %s\n", "TCB", "SPbase", "SPsize", "SPfree", "Name");
120         for (i = 0; i < 56; i++)
121                 kputchar('-');
122         kputchar('\n');
123
124         proc_forbid();
125         FOREACH_NODE(node, &MonitorProcs)
126         {
127                 Process *p = containerof(node, Process, monitor.link);
128                 size_t free = monitor_checkStack(p->stack_base, p->stack_size);
129                 kprintf("%-8p%-8p%-8zu%-8zu %s\n",
130                         p, p->stack_base, p->stack_size, free, p->monitor.name);
131         }
132         proc_permit();
133 }
134
135
136 static void NORETURN monitor(void)
137 {
138         Node *node;
139
140         for (;;)
141         {
142                 proc_forbid();
143                 FOREACH_NODE(node, &MonitorProcs)
144                 {
145                         Process *p = containerof(node, Process, monitor.link);
146                         size_t free = monitor_checkStack(p->stack_base, p->stack_size);
147
148                         if (free < 0x20)
149                                 kprintf("MONITOR: Free stack of process '%s' is only %u chars\n",
150                                                 p->monitor.name, (unsigned int)free);
151                 }
152                 proc_permit();
153
154                 /* Give some rest to the system */
155                 timer_delay(500);
156         }
157 }
158
159
160 void monitor_start(size_t stacksize, cpustack_t *stack)
161 {
162         proc_new(monitor, NULL, stacksize, stack);
163 }
164
165 #endif /* CONFIG_KERN_MONITOR */