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.6 2006/02/24 01:17:05 bernie
18 *#* Update for new emulator.
20 *#* Revision 1.5 2005/11/04 16:20:02 bernie
21 *#* Fix reference to README.devlib in header.
23 *#* Revision 1.4 2005/04/11 19:10:28 bernie
24 *#* Include top-level headers from cfg/ subdir.
26 *#* Revision 1.3 2004/11/28 23:20:25 bernie
27 *#* Remove obsolete INITLIST macro.
29 *#* Revision 1.2 2004/10/26 09:01:15 bernie
30 *#* monitor_rename(): New function.
32 *#* Revision 1.1 2004/10/03 20:39:03 bernie
35 *#* Revision 1.2 2004/10/03 20:36:43 bernie
36 *#* Use debug.h instead of drv/kdebug.h; Misc spacing/header fixes.
38 *#* Revision 1.1 2004/09/30 23:19:30 rasky
39 *#* Estratto il monitor degli stack da proc.c in due file a parte: monitor.c/h
40 *#* Rinominata monitor_debug_stacks in monitor_report
45 #if CONFIG_KERN_MONITOR
48 #include <mware/list.h>
49 #include <drv/timer.h>
50 #include <kern/proc.h>
51 #include <cfg/macros.h>
52 #include <cfg/debug.h>
55 static List MonitorProcs;
58 void monitor_init(void)
60 LIST_INIT(&MonitorProcs);
64 void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
66 proc->monitor.name = name;
67 proc->monitor.stack_base = stack_base;
68 proc->monitor.stack_size = stack_size;
70 ADDTAIL(&MonitorProcs, &proc->monitor.link);
74 void monitor_remove(Process* proc)
76 REMOVE(&proc->monitor.link);
79 void monitor_rename(Process *proc, const char* name)
81 proc->monitor.name = name;
84 #define MONITOR_NODE_TO_PROCESS(node) \
85 (struct Process *)((char *)(node) - offsetof(struct Process, monitor.link))
87 size_t monitor_checkStack(cpustack_t* stack_base, size_t stack_size)
95 end = stack_base + stack_size / sizeof(cpustack_t) - 1;
97 if (CPU_STACK_GROWS_UPWARD)
107 if (*cur != CONFIG_KERN_STACKFILLCODE)
110 if (CPU_STACK_GROWS_UPWARD)
116 sp_free = ABS(cur - beg) * sizeof(cpustack_t);
121 void monitor_report(void)
126 if (LIST_EMPTY(&MonitorProcs))
128 kputs("No stacks registered in the monitor\n");
132 kprintf("%-24s%-8s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
137 for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
138 p->monitor.link.succ;
139 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
141 size_t free = monitor_checkStack(p->monitor.stack_base, p->monitor.stack_size);
142 kprintf("%-24s%8p%8p%8lx%8lx\n",
143 p->monitor.name, p, p->monitor.stack_base, p->monitor.stack_size, free);
148 static void NORETURN monitor(void)
154 for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
155 p->monitor.link.succ;
156 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
158 size_t free = monitor_checkStack(p->monitor.stack_base, p->monitor.stack_size);
162 kprintf("MONITOR: ***************************************\n");
163 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %04x chars\n", p->monitor.name, free);
164 kprintf("MONITOR: ***************************************\n\n");
173 void monitor_start(size_t stacksize, cpustack_t *stack)
175 proc_new(monitor, NULL, stacksize, stack);
178 #endif /* CONFIG_KERN_MONITOR */