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.5 2005/11/04 16:20:02 bernie
18 *#* Fix reference to README.devlib in header.
20 *#* Revision 1.4 2005/04/11 19:10:28 bernie
21 *#* Include top-level headers from cfg/ subdir.
23 *#* Revision 1.3 2004/11/28 23:20:25 bernie
24 *#* Remove obsolete INITLIST macro.
26 *#* Revision 1.2 2004/10/26 09:01:15 bernie
27 *#* monitor_rename(): New function.
29 *#* Revision 1.1 2004/10/03 20:39:03 bernie
32 *#* Revision 1.2 2004/10/03 20:36:43 bernie
33 *#* Use debug.h instead of drv/kdebug.h; Misc spacing/header fixes.
35 *#* Revision 1.1 2004/09/30 23:19:30 rasky
36 *#* Estratto il monitor degli stack da proc.c in due file a parte: monitor.c/h
37 *#* Rinominata monitor_debug_stacks in monitor_report
42 #if CONFIG_KERN_MONITOR
45 #include <mware/list.h>
46 #include <drv/timer.h>
47 #include <kern/proc.h>
48 #include <cfg/macros.h>
49 #include <cfg/debug.h>
52 static List MonitorProcs;
55 void monitor_init(void)
57 LIST_INIT(&MonitorProcs);
61 void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
63 proc->monitor.name = name;
64 proc->monitor.stack_base = stack_base;
65 proc->monitor.stack_size = stack_size;
67 ADDTAIL(&MonitorProcs, &proc->monitor.link);
71 void monitor_remove(Process* proc)
73 REMOVE(&proc->monitor.link);
76 void monitor_rename(Process *proc, const char* name)
78 proc->monitor.name = name;
81 #define MONITOR_NODE_TO_PROCESS(node) \
82 (struct Process*)((char*)(node) - offsetof(struct Process, monitor.link))
84 size_t monitor_check_stack(cpustack_t* stack_base, size_t stack_size)
92 end = stack_base + stack_size / sizeof(cpustack_t) - 1;
94 if (CPU_STACK_GROWS_UPWARD)
104 if (*cur != CONFIG_KERN_STACKFILLCODE)
107 if (CPU_STACK_GROWS_UPWARD)
113 sp_free = ABS(cur - beg) * sizeof(cpustack_t);
118 void monitor_report(void)
123 if (ISLISTEMPTY(&MonitorProcs))
125 kputs("No stacks registered in the monitor\n");
129 kprintf("%-24s %-6s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
134 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
135 p->monitor.link.succ;
136 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
138 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
139 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);
144 static void monitor(void)
150 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
151 p->monitor.link.succ;
152 p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
154 size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
158 kprintf("MONITOR: ***************************************\n");
159 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %04x chars\n", p->monitor.name, free);
160 kprintf("MONITOR: ***************************************\n\n");
169 void monitor_start(size_t stacksize, cpustack_t *stack)
171 proc_new(monitor, NULL, stacksize, stack);
174 #endif /* CONFIG_KERN_MONITOR */