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