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