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