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.4 2005/04/11 19:10:28 bernie
18 *#* Include top-level headers from cfg/ subdir.
20 *#* Revision 1.3 2004/11/28 23:20:25 bernie
21 *#* Remove obsolete INITLIST macro.
23 *#* Revision 1.2 2004/10/26 09:01:15 bernie
24 *#* monitor_rename(): New function.
26 *#* Revision 1.1 2004/10/03 20:39:03 bernie
29 *#* Revision 1.2 2004/10/03 20:36:43 bernie
30 *#* Use debug.h instead of drv/kdebug.h; Misc spacing/header fixes.
32 *#* Revision 1.1 2004/09/30 23:19:30 rasky
33 *#* Estratto il monitor degli stack da proc.c in due file a parte: monitor.c/h
34 *#* Rinominata monitor_debug_stacks in monitor_report
39 #if CONFIG_KERN_MONITOR
42 #include <mware/list.h>
43 #include <drv/timer.h>
44 #include <kern/proc.h>
45 #include <cfg/macros.h>
46 #include <cfg/debug.h>
49 static List MonitorProcs;
52 void monitor_init(void)
54 LIST_INIT(&MonitorProcs);
58 void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
60 proc->monitor.name = name;
61 proc->monitor.stack_base = stack_base;
62 proc->monitor.stack_size = stack_size;
64 ADDTAIL(&MonitorProcs, &proc->monitor.link);
68 void monitor_remove(Process* proc)
70 REMOVE(&proc->monitor.link);
73 void monitor_rename(Process *proc, const char* name)
75 proc->monitor.name = name;
78 #define MONITOR_NODE_TO_PROCESS(node) \
79 (struct Process*)((char*)(node) - offsetof(struct Process, monitor.link))
81 size_t monitor_check_stack(cpustack_t* stack_base, size_t stack_size)
89 end = stack_base + stack_size / sizeof(cpustack_t) - 1;
91 if (CPU_STACK_GROWS_UPWARD)
101 if (*cur != CONFIG_KERN_STACKFILLCODE)
104 if (CPU_STACK_GROWS_UPWARD)
110 sp_free = ABS(cur - beg) * sizeof(cpustack_t);
115 void monitor_report(void)
120 if (ISLISTEMPTY(&MonitorProcs))
122 kputs("No stacks registered in the monitor\n");
126 kprintf("%-24s %-6s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
131 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
132 p->monitor.link.succ;
133 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
135 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
136 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);
141 static void monitor(void)
147 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
148 p->monitor.link.succ;
149 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
151 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
155 kprintf("MONITOR: ***************************************\n");
156 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %04x chars\n", p->monitor.name, free);
157 kprintf("MONITOR: ***************************************\n\n");
166 void monitor_start(size_t stacksize, cpustack_t *stack)
168 proc_new(monitor, NULL, stacksize, stack);
171 #endif /* CONFIG_KERN_MONITOR */