4 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
8 * \brief Monitor to check for stack overflows
12 * \author Giovanni Bajo <rasky@develer.com>
17 *#* Revision 1.8 2006/03/22 09:49:22 bernie
18 *#* Reduce memory usage.
20 *#* Revision 1.7 2006/03/13 02:07:14 bernie
23 *#* Revision 1.6 2006/02/24 01:17:05 bernie
24 *#* Update for new emulator.
26 *#* Revision 1.5 2005/11/04 16:20:02 bernie
27 *#* Fix reference to README.devlib in header.
29 *#* Revision 1.4 2005/04/11 19:10:28 bernie
30 *#* Include top-level headers from cfg/ subdir.
32 *#* Revision 1.3 2004/11/28 23:20:25 bernie
33 *#* Remove obsolete INITLIST macro.
35 *#* Revision 1.2 2004/10/26 09:01:15 bernie
36 *#* monitor_rename(): New function.
38 *#* Revision 1.1 2004/10/03 20:39:03 bernie
41 *#* Revision 1.2 2004/10/03 20:36:43 bernie
42 *#* Use debug.h instead of drv/kdebug.h; Misc spacing/header fixes.
44 *#* Revision 1.1 2004/09/30 23:19:30 rasky
45 *#* Estratto il monitor degli stack da proc.c in due file a parte: monitor.c/h
46 *#* Rinominata monitor_debug_stacks in monitor_report
51 #if CONFIG_KERN_MONITOR
54 #include <mware/list.h>
55 #include <drv/timer.h>
56 #include <kern/proc.h>
57 #include <cfg/macros.h>
58 #include <cfg/debug.h>
61 static List MonitorProcs;
64 void monitor_init(void)
66 LIST_INIT(&MonitorProcs);
70 void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
72 proc->monitor.name = name;
73 proc->monitor.stack_base = stack_base;
74 proc->monitor.stack_size = stack_size;
76 ADDTAIL(&MonitorProcs, &proc->monitor.link);
80 void monitor_remove(Process* proc)
82 REMOVE(&proc->monitor.link);
85 void monitor_rename(Process *proc, const char* name)
87 proc->monitor.name = name;
90 /* TODO: use containerof() */
91 #define MONITOR_NODE_TO_PROCESS(node) \
92 (struct Process *)((char *)(node) - offsetof(struct Process, monitor.link))
94 size_t monitor_checkStack(cpustack_t* stack_base, size_t stack_size)
102 end = stack_base + stack_size / sizeof(cpustack_t) - 1;
104 if (CPU_STACK_GROWS_UPWARD)
114 if (*cur != CONFIG_KERN_STACKFILLCODE)
117 if (CPU_STACK_GROWS_UPWARD)
123 sp_free = ABS(cur - beg) * sizeof(cpustack_t);
128 void monitor_report(void)
133 if (LIST_EMPTY(&MonitorProcs))
135 kputs("No stacks registered in the monitor\n");
139 kprintf("%-24s%-8s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
144 for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
145 p->monitor.link.succ;
146 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
148 size_t free = monitor_checkStack(p->monitor.stack_base, p->monitor.stack_size);
149 kprintf("%-24s%8p%8p%8lx%8lx\n",
150 p->monitor.name, p, p->monitor.stack_base, p->monitor.stack_size, free);
155 static void NORETURN monitor(void)
161 for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
162 p->monitor.link.succ;
163 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
165 size_t free = monitor_checkStack(p->monitor.stack_base, p->monitor.stack_size);
168 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %x chars\n",
169 p->monitor.name, free);
177 void monitor_start(size_t stacksize, cpustack_t *stack)
179 proc_new(monitor, NULL, stacksize, stack);
182 #endif /* CONFIG_KERN_MONITOR */