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