Removed 'This file is part of DevLib ...'
[bertos.git] / 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  *#* $Log$
42  *#* Revision 1.10  2006/09/20 13:58:42  marco
43  *#* Used z modifier instead l.
44  *#*
45  *#* Revision 1.9  2006/07/19 12:56:27  bernie
46  *#* Convert to new Doxygen style.
47  *#*
48  *#* Revision 1.8  2006/03/22 09:49:22  bernie
49  *#* Reduce memory usage.
50  *#*
51  *#* Revision 1.7  2006/03/13 02:07:14  bernie
52  *#* Add TODO item.
53  *#*
54  *#* Revision 1.6  2006/02/24 01:17:05  bernie
55  *#* Update for new emulator.
56  *#*
57  *#* Revision 1.5  2005/11/04 16:20:02  bernie
58  *#* Fix reference to README.devlib in header.
59  *#*
60  *#* Revision 1.4  2005/04/11 19:10:28  bernie
61  *#* Include top-level headers from cfg/ subdir.
62  *#*
63  *#* Revision 1.3  2004/11/28 23:20:25  bernie
64  *#* Remove obsolete INITLIST macro.
65  *#*
66  *#* Revision 1.2  2004/10/26 09:01:15  bernie
67  *#* monitor_rename(): New function.
68  *#*
69  *#* Revision 1.1  2004/10/03 20:39:03  bernie
70  *#* Import in DevLib.
71  *#*
72  *#* Revision 1.2  2004/10/03 20:36:43  bernie
73  *#* Use debug.h instead of drv/kdebug.h; Misc spacing/header fixes.
74  *#*
75  *#* Revision 1.1  2004/09/30 23:19:30  rasky
76  *#* Estratto il monitor degli stack da proc.c in due file a parte: monitor.c/h
77  *#* Rinominata monitor_debug_stacks in monitor_report
78  *#*/
79
80 #include "monitor.h"
81
82 #if CONFIG_KERN_MONITOR
83
84 #include "proc_p.h"
85 #include <mware/list.h>
86 #include <drv/timer.h>
87 #include <kern/proc.h>
88 #include <cfg/macros.h>
89 #include <cfg/debug.h>
90
91
92 static List MonitorProcs;
93
94
95 void monitor_init(void)
96 {
97         LIST_INIT(&MonitorProcs);
98 }
99
100
101 void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
102 {
103         proc->monitor.name = name;
104         proc->monitor.stack_base = stack_base;
105         proc->monitor.stack_size = stack_size;
106
107         ADDTAIL(&MonitorProcs, &proc->monitor.link);
108 }
109
110
111 void monitor_remove(Process* proc)
112 {
113         REMOVE(&proc->monitor.link);
114 }
115
116 void monitor_rename(Process *proc, const char* name)
117 {
118         proc->monitor.name = name;
119 }
120
121 /* TODO: use containerof() */
122 #define MONITOR_NODE_TO_PROCESS(node) \
123         (struct Process *)((char *)(node) - offsetof(struct Process, monitor.link))
124
125 size_t monitor_checkStack(cpustack_t* stack_base, size_t stack_size)
126 {
127         cpustack_t* beg;
128         cpustack_t* cur;
129         cpustack_t* end;
130         size_t sp_free;
131
132         beg = stack_base;
133         end = stack_base + stack_size / sizeof(cpustack_t) - 1;
134
135         if (CPU_STACK_GROWS_UPWARD)
136         {
137                 cur = beg;
138                 beg = end;
139                 end = cur;
140         }
141
142         cur = beg;
143         while (cur != end)
144         {
145                 if (*cur != CONFIG_KERN_STACKFILLCODE)
146                         break;
147
148                 if (CPU_STACK_GROWS_UPWARD)
149                         cur--;
150                 else
151                         cur++;
152         }
153
154         sp_free = ABS(cur - beg) * sizeof(cpustack_t);
155         return sp_free;
156 }
157
158
159 void monitor_report(void)
160 {
161         struct Process* p;
162         int i;
163
164         if (LIST_EMPTY(&MonitorProcs))
165         {
166                 kputs("No stacks registered in the monitor\n");
167                 return;
168         }
169
170         kprintf("%-24s%-8s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
171         for (i=0;i<56;i++)
172                 kputchar('-');
173         kputchar('\n');
174
175         for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
176                  p->monitor.link.succ;
177                  p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
178         {
179                 size_t free = monitor_checkStack(p->monitor.stack_base, p->monitor.stack_size);
180                 kprintf("%-24s%8p%8p%8zx%8zx\n",
181                         p->monitor.name, p, p->monitor.stack_base, p->monitor.stack_size, free);
182         }
183 }
184
185
186 static void NORETURN monitor(void)
187 {
188         struct Process *p;
189
190         while (1)
191         {
192                 for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
193                         p->monitor.link.succ;
194                         p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
195                 {
196                         size_t free = monitor_checkStack(p->monitor.stack_base, p->monitor.stack_size);
197
198                         if (free < 0x20)
199                                 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %x chars\n",
200                                                 p->monitor.name, free);
201
202                         timer_delay(500);
203                 }
204         }
205 }
206
207
208 void monitor_start(size_t stacksize, cpustack_t *stack)
209 {
210         proc_new(monitor, NULL, stacksize, stack);
211 }
212
213 #endif /* CONFIG_KERN_MONITOR */