Add TODO item.
[bertos.git] / kern / monitor.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief Monitor to check for stack overflows
9  *
10  * \version $Id$
11  *
12  * \author Giovanni Bajo <rasky@develer.com>
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.7  2006/03/13 02:07:14  bernie
18  *#* Add TODO item.
19  *#*
20  *#* Revision 1.6  2006/02/24 01:17:05  bernie
21  *#* Update for new emulator.
22  *#*
23  *#* Revision 1.5  2005/11/04 16:20:02  bernie
24  *#* Fix reference to README.devlib in header.
25  *#*
26  *#* Revision 1.4  2005/04/11 19:10:28  bernie
27  *#* Include top-level headers from cfg/ subdir.
28  *#*
29  *#* Revision 1.3  2004/11/28 23:20:25  bernie
30  *#* Remove obsolete INITLIST macro.
31  *#*
32  *#* Revision 1.2  2004/10/26 09:01:15  bernie
33  *#* monitor_rename(): New function.
34  *#*
35  *#* Revision 1.1  2004/10/03 20:39:03  bernie
36  *#* Import in DevLib.
37  *#*
38  *#* Revision 1.2  2004/10/03 20:36:43  bernie
39  *#* Use debug.h instead of drv/kdebug.h; Misc spacing/header fixes.
40  *#*
41  *#* Revision 1.1  2004/09/30 23:19:30  rasky
42  *#* Estratto il monitor degli stack da proc.c in due file a parte: monitor.c/h
43  *#* Rinominata monitor_debug_stacks in monitor_report
44  *#*/
45
46 #include "monitor.h"
47
48 #if CONFIG_KERN_MONITOR
49
50 #include "proc_p.h"
51 #include <mware/list.h>
52 #include <drv/timer.h>
53 #include <kern/proc.h>
54 #include <cfg/macros.h>
55 #include <cfg/debug.h>
56
57
58 static List MonitorProcs;
59
60
61 void monitor_init(void)
62 {
63         LIST_INIT(&MonitorProcs);
64 }
65
66
67 void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
68 {
69         proc->monitor.name = name;
70         proc->monitor.stack_base = stack_base;
71         proc->monitor.stack_size = stack_size;
72
73         ADDTAIL(&MonitorProcs, &proc->monitor.link);
74 }
75
76
77 void monitor_remove(Process* proc)
78 {
79         REMOVE(&proc->monitor.link);
80 }
81
82 void monitor_rename(Process *proc, const char* name)
83 {
84         proc->monitor.name = name;
85 }
86
87 /* TODO: use containerof() */
88 #define MONITOR_NODE_TO_PROCESS(node) \
89         (struct Process *)((char *)(node) - offsetof(struct Process, monitor.link))
90
91 size_t monitor_checkStack(cpustack_t* stack_base, size_t stack_size)
92 {
93         cpustack_t* beg;
94         cpustack_t* cur;
95         cpustack_t* end;
96         size_t sp_free;
97
98         beg = stack_base;
99         end = stack_base + stack_size / sizeof(cpustack_t) - 1;
100
101         if (CPU_STACK_GROWS_UPWARD)
102         {
103                 cur = beg;
104                 beg = end;
105                 end = cur;
106         }
107
108         cur = beg;
109         while (cur != end)
110         {
111                 if (*cur != CONFIG_KERN_STACKFILLCODE)
112                         break;
113
114                 if (CPU_STACK_GROWS_UPWARD)
115                         cur--;
116                 else
117                         cur++;
118         }
119
120         sp_free = ABS(cur - beg) * sizeof(cpustack_t);
121         return sp_free;
122 }
123
124
125 void monitor_report(void)
126 {
127         struct Process* p;
128         int i;
129
130         if (LIST_EMPTY(&MonitorProcs))
131         {
132                 kputs("No stacks registered in the monitor\n");
133                 return;
134         }
135
136         kprintf("%-24s%-8s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
137         for (i=0;i<56;i++)
138                 kputchar('-');
139         kputchar('\n');
140
141         for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
142                  p->monitor.link.succ;
143                  p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
144         {
145                 size_t free = monitor_checkStack(p->monitor.stack_base, p->monitor.stack_size);
146                 kprintf("%-24s%8p%8p%8lx%8lx\n",
147                         p->monitor.name, p, p->monitor.stack_base, p->monitor.stack_size, free);
148         }
149 }
150
151
152 static void NORETURN monitor(void)
153 {
154         struct Process* p;
155
156         while (1)
157         {
158                 for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
159                         p->monitor.link.succ;
160                         p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
161                 {
162                         size_t free = monitor_checkStack(p->monitor.stack_base, p->monitor.stack_size);
163
164                         if (free < 0x20)
165                         {
166                                 kprintf("MONITOR: ***************************************\n");
167                                 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %04x chars\n", p->monitor.name, free);
168                                 kprintf("MONITOR: ***************************************\n\n");
169                         }
170
171                         timer_delay(500);
172                 }
173         }
174 }
175
176
177 void monitor_start(size_t stacksize, cpustack_t *stack)
178 {
179         proc_new(monitor, NULL, stacksize, stack);
180 }
181
182 #endif /* CONFIG_KERN_MONITOR */