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.1 2004/10/03 20:39:03 bernie
20 *#* Revision 1.2 2004/10/03 20:36:43 bernie
21 *#* Use debug.h instead of drv/kdebug.h; Misc spacing/header fixes.
23 *#* Revision 1.1 2004/09/30 23:19:30 rasky
24 *#* Estratto il monitor degli stack da proc.c in due file a parte: monitor.c/h
25 *#* Rinominata monitor_debug_stacks in monitor_report
30 #if CONFIG_KERN_MONITOR
33 #include <mware/list.h>
34 #include <drv/timer.h>
35 #include <kern/proc.h>
40 static List MonitorProcs;
43 void monitor_init(void)
45 INITLIST(&MonitorProcs);
49 void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
51 proc->monitor.name = name;
52 proc->monitor.stack_base = stack_base;
53 proc->monitor.stack_size = stack_size;
55 ADDTAIL(&MonitorProcs, &proc->monitor.link);
59 void monitor_remove(Process* proc)
61 REMOVE(&proc->monitor.link);
64 #define MONITOR_NODE_TO_PROCESS(node) \
65 (struct Process*)((char*)(node) - offsetof(struct Process, monitor.link))
67 size_t monitor_check_stack(cpustack_t* stack_base, size_t stack_size)
75 end = stack_base + stack_size / sizeof(cpustack_t) - 1;
77 if (CPU_STACK_GROWS_UPWARD)
87 if (*cur != CONFIG_KERN_STACKFILLCODE)
90 if (CPU_STACK_GROWS_UPWARD)
96 sp_free = ABS(cur - beg) * sizeof(cpustack_t);
101 void monitor_report(void)
106 if (ISLISTEMPTY(&MonitorProcs))
108 kputs("No stacks registered in the monitor\n");
112 kprintf("%-24s %-6s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
117 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
118 p->monitor.link.succ;
119 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
121 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
122 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);
127 static void monitor(void)
133 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
134 p->monitor.link.succ;
135 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
137 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
141 kprintf("MONITOR: ***************************************\n");
142 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %04x chars\n", p->monitor.name, free);
143 kprintf("MONITOR: ***************************************\n\n");
152 void monitor_start(size_t stacksize, cpustack_t *stack)
154 proc_new(monitor, NULL, stacksize, stack);
157 #endif /* CONFIG_KERN_MONITOR */