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.2 2004/10/26 09:01:15 bernie
18 *#* monitor_rename(): New function.
20 *#* Revision 1.1 2004/10/03 20:39:03 bernie
23 *#* Revision 1.2 2004/10/03 20:36:43 bernie
24 *#* Use debug.h instead of drv/kdebug.h; Misc spacing/header fixes.
26 *#* Revision 1.1 2004/09/30 23:19:30 rasky
27 *#* Estratto il monitor degli stack da proc.c in due file a parte: monitor.c/h
28 *#* Rinominata monitor_debug_stacks in monitor_report
33 #if CONFIG_KERN_MONITOR
36 #include <mware/list.h>
37 #include <drv/timer.h>
38 #include <kern/proc.h>
43 static List MonitorProcs;
46 void monitor_init(void)
48 INITLIST(&MonitorProcs);
52 void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
54 proc->monitor.name = name;
55 proc->monitor.stack_base = stack_base;
56 proc->monitor.stack_size = stack_size;
58 ADDTAIL(&MonitorProcs, &proc->monitor.link);
62 void monitor_remove(Process* proc)
64 REMOVE(&proc->monitor.link);
67 void monitor_rename(Process *proc, const char* name)
69 proc->monitor.name = name;
72 #define MONITOR_NODE_TO_PROCESS(node) \
73 (struct Process*)((char*)(node) - offsetof(struct Process, monitor.link))
75 size_t monitor_check_stack(cpustack_t* stack_base, size_t stack_size)
83 end = stack_base + stack_size / sizeof(cpustack_t) - 1;
85 if (CPU_STACK_GROWS_UPWARD)
95 if (*cur != CONFIG_KERN_STACKFILLCODE)
98 if (CPU_STACK_GROWS_UPWARD)
104 sp_free = ABS(cur - beg) * sizeof(cpustack_t);
109 void monitor_report(void)
114 if (ISLISTEMPTY(&MonitorProcs))
116 kputs("No stacks registered in the monitor\n");
120 kprintf("%-24s %-6s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
125 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
126 p->monitor.link.succ;
127 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
129 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
130 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);
135 static void monitor(void)
141 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
142 p->monitor.link.succ;
143 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
145 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
149 kprintf("MONITOR: ***************************************\n");
150 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %04x chars\n", p->monitor.name, free);
151 kprintf("MONITOR: ***************************************\n\n");
160 void monitor_start(size_t stacksize, cpustack_t *stack)
162 proc_new(monitor, NULL, stacksize, stack);
165 #endif /* CONFIG_KERN_MONITOR */