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