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