Fix reference to README.devlib in header.
[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.5  2005/11/04 16:20:02  bernie
18  *#* Fix reference to README.devlib in header.
19  *#*
20  *#* Revision 1.4  2005/04/11 19:10:28  bernie
21  *#* Include top-level headers from cfg/ subdir.
22  *#*
23  *#* Revision 1.3  2004/11/28 23:20:25  bernie
24  *#* Remove obsolete INITLIST macro.
25  *#*
26  *#* Revision 1.2  2004/10/26 09:01:15  bernie
27  *#* monitor_rename(): New function.
28  *#*
29  *#* Revision 1.1  2004/10/03 20:39:03  bernie
30  *#* Import in DevLib.
31  *#*
32  *#* Revision 1.2  2004/10/03 20:36:43  bernie
33  *#* Use debug.h instead of drv/kdebug.h; Misc spacing/header fixes.
34  *#*
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
38  *#*/
39
40 #include "monitor.h"
41
42 #if CONFIG_KERN_MONITOR
43
44 #include "proc_p.h"
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>
50
51
52 static List MonitorProcs;
53
54
55 void monitor_init(void)
56 {
57         LIST_INIT(&MonitorProcs);
58 }
59
60
61 void monitor_add(Process* proc, const char* name, cpustack_t* stack_base, size_t stack_size)
62 {
63         proc->monitor.name = name;
64         proc->monitor.stack_base = stack_base;
65         proc->monitor.stack_size = stack_size;
66
67         ADDTAIL(&MonitorProcs, &proc->monitor.link);
68 }
69
70
71 void monitor_remove(Process* proc)
72 {
73         REMOVE(&proc->monitor.link);
74 }
75
76 void monitor_rename(Process *proc, const char* name)
77 {
78         proc->monitor.name = name;
79 }
80
81 #define MONITOR_NODE_TO_PROCESS(node) \
82         (struct Process*)((char*)(node) - offsetof(struct Process, monitor.link))
83
84 size_t monitor_check_stack(cpustack_t* stack_base, size_t stack_size)
85 {
86         cpustack_t* beg;
87         cpustack_t* cur;
88         cpustack_t* end;
89         size_t sp_free;
90
91         beg = stack_base;
92         end = stack_base + stack_size / sizeof(cpustack_t) - 1;
93
94         if (CPU_STACK_GROWS_UPWARD)
95         {
96                 cur = beg;
97                 beg = end;
98                 end = cur;
99         }
100
101         cur = beg;
102         while (cur != end)
103         {
104                 if (*cur != CONFIG_KERN_STACKFILLCODE)
105                         break;
106
107                 if (CPU_STACK_GROWS_UPWARD)
108                         cur--;
109                 else
110                         cur++;
111         }
112
113         sp_free = ABS(cur - beg) * sizeof(cpustack_t);
114         return sp_free;
115 }
116
117
118 void monitor_report(void)
119 {
120         struct Process* p;
121         int i;
122
123         if (ISLISTEMPTY(&MonitorProcs))
124         {
125                 kputs("No stacks registered in the monitor\n");
126                 return;
127         }
128
129         kprintf("%-24s    %-6s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
130         for (i=0;i<56;i++)
131                 kputchar('-');
132         kputchar('\n');
133
134         for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
135                  p->monitor.link.succ;
136                  p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
137         {
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);
140         }
141 }
142
143
144 static void monitor(void)
145 {
146         struct Process* p;
147
148         while (1)
149         {
150                 for (p = MONITOR_NODE_TO_PROCESS(MonitorProcs.head);
151                         p->monitor.link.succ;
152                         p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
153                 {
154                         size_t free = monitor_check_stack(p->monitor.stack_base, p->monitor.stack_size);
155
156                         if (free < 0x20)
157                         {
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");
161                         }
162
163                         timer_delay(500);
164                 }
165         }
166 }
167
168
169 void monitor_start(size_t stacksize, cpustack_t *stack)
170 {
171         proc_new(monitor, NULL, stacksize, stack);
172 }
173
174 #endif /* CONFIG_KERN_MONITOR */