4 * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See devlib/README for information.
8 * \brief Monitor to check for stack overflows
12 * \author Giovanni Bajo <rasky@develer.com>
17 *#* Revision 1.3 2004/11/28 23:20:25 bernie
18 *#* Remove obsolete INITLIST macro.
20 *#* Revision 1.2 2004/10/26 09:01:15 bernie
21 *#* monitor_rename(): New function.
23 *#* Revision 1.1 2004/10/03 20:39:03 bernie
26 *#* Revision 1.2 2004/10/03 20:36:43 bernie
27 *#* Use debug.h instead of drv/kdebug.h; Misc spacing/header fixes.
29 *#* Revision 1.1 2004/09/30 23:19:30 rasky
30 *#* Estratto il monitor degli stack da proc.c in due file a parte: monitor.c/h
31 *#* Rinominata monitor_debug_stacks in monitor_report
36 #if CONFIG_KERN_MONITOR
39 #include <mware/list.h>
40 #include <drv/timer.h>
41 #include <kern/proc.h>
46 static List MonitorProcs;
49 void monitor_init(void)
51 LIST_INIT(&MonitorProcs);
55 void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
57 proc->monitor.name = name;
58 proc->monitor.stack_base = stack_base;
59 proc->monitor.stack_size = stack_size;
61 ADDTAIL(&MonitorProcs, &proc->monitor.link);
65 void monitor_remove(Process* proc)
67 REMOVE(&proc->monitor.link);
70 void monitor_rename(Process *proc, const char* name)
72 proc->monitor.name = name;
75 #define MONITOR_NODE_TO_PROCESS(node) \
76 (struct Process*)((char*)(node) - offsetof(struct Process, monitor.link))
78 size_t monitor_check_stack(cpustack_t* stack_base, size_t stack_size)
86 end = stack_base + stack_size / sizeof(cpustack_t) - 1;
88 if (CPU_STACK_GROWS_UPWARD)
98 if (*cur != CONFIG_KERN_STACKFILLCODE)
101 if (CPU_STACK_GROWS_UPWARD)
107 sp_free = ABS(cur - beg) * sizeof(cpustack_t);
112 void monitor_report(void)
117 if (ISLISTEMPTY(&MonitorProcs))
119 kputs("No stacks registered in the monitor\n");
123 kprintf("%-24s %-6s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
128 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
129 p->monitor.link.succ;
130 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
132 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
133 kprintf("%-24s %04x %04x %4x %4x\n", p->monitor.name, (uint16_t)p, (uint16_t)p->monitor.stack_base, (uint16_t)p->monitor.stack_size, (uint16_t)free);
138 static void monitor(void)
144 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
145 p->monitor.link.succ;
146 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
148 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
152 kprintf("MONITOR: ***************************************\n");
153 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %04x chars\n", p->monitor.name, free);
154 kprintf("MONITOR: ***************************************\n\n");
163 void monitor_start(size_t stacksize, cpustack_t *stack)
165 proc_new(monitor, NULL, stacksize, stack);
168 #endif /* CONFIG_KERN_MONITOR */