4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
33 * \brief Monitor to check for stack overflows
37 * \author Giovanni Bajo <rasky@develer.com>
42 *#* Revision 1.10 2006/09/20 13:58:42 marco
43 *#* Used z modifier instead l.
45 *#* Revision 1.9 2006/07/19 12:56:27 bernie
46 *#* Convert to new Doxygen style.
48 *#* Revision 1.8 2006/03/22 09:49:22 bernie
49 *#* Reduce memory usage.
51 *#* Revision 1.7 2006/03/13 02:07:14 bernie
54 *#* Revision 1.6 2006/02/24 01:17:05 bernie
55 *#* Update for new emulator.
57 *#* Revision 1.5 2005/11/04 16:20:02 bernie
58 *#* Fix reference to README.devlib in header.
60 *#* Revision 1.4 2005/04/11 19:10:28 bernie
61 *#* Include top-level headers from cfg/ subdir.
63 *#* Revision 1.3 2004/11/28 23:20:25 bernie
64 *#* Remove obsolete INITLIST macro.
66 *#* Revision 1.2 2004/10/26 09:01:15 bernie
67 *#* monitor_rename(): New function.
69 *#* Revision 1.1 2004/10/03 20:39:03 bernie
72 *#* Revision 1.2 2004/10/03 20:36:43 bernie
73 *#* Use debug.h instead of drv/kdebug.h; Misc spacing/header fixes.
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
82 #if CONFIG_KERN_MONITOR
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>
92 static List MonitorProcs;
95 void monitor_init(void)
97 LIST_INIT(&MonitorProcs);
101 void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
103 proc->monitor.name = name;
104 proc->monitor.stack_base = stack_base;
105 proc->monitor.stack_size = stack_size;
107 ADDTAIL(&MonitorProcs, &proc->monitor.link);
111 void monitor_remove(Process* proc)
113 REMOVE(&proc->monitor.link);
116 void monitor_rename(Process *proc, const char* name)
118 proc->monitor.name = name;
121 /* TODO: use containerof() */
122 #define MONITOR_NODE_TO_PROCESS(node) \
123 (struct Process *)((char *)(node) - offsetof(struct Process, monitor.link))
125 size_t monitor_checkStack(cpustack_t* stack_base, size_t stack_size)
133 end = stack_base + stack_size / sizeof(cpustack_t) - 1;
135 if (CPU_STACK_GROWS_UPWARD)
145 if (*cur != CONFIG_KERN_STACKFILLCODE)
148 if (CPU_STACK_GROWS_UPWARD)
154 sp_free = ABS(cur - beg) * sizeof(cpustack_t);
159 void monitor_report(void)
164 if (LIST_EMPTY(&MonitorProcs))
166 kputs("No stacks registered in the monitor\n");
170 kprintf("%-24s%-8s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
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))
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);
186 static void NORETURN monitor(void)
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))
196 size_t free = monitor_checkStack(p->monitor.stack_base, p->monitor.stack_size);
199 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %x chars\n",
200 p->monitor.name, free);
208 void monitor_start(size_t stacksize, cpustack_t *stack)
210 proc_new(monitor, NULL, stacksize, stack);
213 #endif /* CONFIG_KERN_MONITOR */