kern: Unify stack_base/stack_size copies in Process
[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 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Monitor to check for stack overflows
34  *
35  * \version $Id$
36  *
37  * \author Giovanni Bajo <rasky@develer.com>
38  */
39
40
41 #include "monitor.h"
42
43 #if CONFIG_KERN_MONITOR
44
45 #include "proc_p.h"
46 #include <mware/list.h>
47 #include <drv/timer.h>
48 #include <kern/proc.h>
49 #include <cfg/macros.h>
50 #include <cfg/debug.h>
51
52
53 static List MonitorProcs;
54
55
56 void monitor_init(void)
57 {
58         LIST_INIT(&MonitorProcs);
59 }
60
61
62 void monitor_add(Process *proc, const char *name)
63 {
64         proc->monitor.name = name;
65
66         ADDTAIL(&MonitorProcs, &proc->monitor.link);
67 }
68
69
70 void monitor_remove(Process* proc)
71 {
72         REMOVE(&proc->monitor.link);
73 }
74
75 void monitor_rename(Process *proc, const char *name)
76 {
77         proc->monitor.name = name;
78 }
79
80 /* TODO: use containerof() */
81 #define MONITOR_NODE_TO_PROCESS(node) \
82         (struct Process *)((intptr_t)(node) - offsetof(struct Process, monitor.link))
83
84 size_t monitor_checkStack(cpustack_t *stack_base, size_t stack_size)
85 {
86         cpustack_t* beg;
87         cpustack_t* cur;
88         cpustack_t* end;
89         size_t sp_free;
90
91         beg = stack_base;
92         end = stack_base + stack_size / sizeof(cpustack_t) - 1;
93
94         if (CPU_STACK_GROWS_UPWARD)
95         {
96                 cur = beg;
97                 beg = end;
98                 end = cur;
99         }
100
101         cur = beg;
102         while (cur != end)
103         {
104                 if (*cur != CONFIG_KERN_STACKFILLCODE)
105                         break;
106
107                 if (CPU_STACK_GROWS_UPWARD)
108                         cur--;
109                 else
110                         cur++;
111         }
112
113         sp_free = ABS(cur - beg) * sizeof(cpustack_t);
114         return sp_free;
115 }
116
117
118 void monitor_report(void)
119 {
120         struct Process* p;
121         int i;
122
123         if (LIST_EMPTY(&MonitorProcs))
124         {
125                 kputs("No stacks registered in the monitor\n");
126                 return;
127         }
128
129         kprintf("%-24s%-8s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
130         for (i=0;i<56;i++)
131                 kputchar('-');
132         kputchar('\n');
133
134         for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
135                  p->monitor.link.succ;
136                  p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
137         {
138                 size_t free = monitor_checkStack(p->stack_base, p->stack_size);
139                 kprintf("%-24s%-8p%-8p%-8lu%-8lu\n",
140                         p->monitor.name, p, p->stack_base, p->stack_size, free);
141         }
142 }
143
144
145 static void NORETURN monitor(void)
146 {
147         struct Process *p;
148
149         while (1)
150         {
151                 for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
152                         p->monitor.link.succ;
153                         p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
154                 {
155                         size_t free = monitor_checkStack(p->stack_base, p->stack_size);
156
157                         if (free < 0x20)
158                                 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %u chars\n",
159                                                 p->monitor.name, (unsigned int)free);
160
161                         timer_delay(500);
162                 }
163         }
164 }
165
166
167 void monitor_start(size_t stacksize, cpustack_t *stack)
168 {
169         proc_new(monitor, NULL, stacksize, stack);
170 }
171
172 #endif /* CONFIG_KERN_MONITOR */