Bump copyright year; Indentation fixes
[bertos.git] / bertos / kern / monitor.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2008 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Monitor to check for stack overflows
34  *
35  * \version $Id$
36  * \author Giovanni Bajo <rasky@develer.com>
37  */
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)
62 {
63         proc->monitor.name = name;
64
65         ADDTAIL(&MonitorProcs, &proc->monitor.link);
66 }
67
68
69 void monitor_remove(Process *proc)
70 {
71         REMOVE(&proc->monitor.link);
72 }
73
74 void monitor_rename(Process *proc, const char *name)
75 {
76         proc->monitor.name = name;
77 }
78
79 /* TODO: use containerof() */
80 #define MONITOR_NODE_TO_PROCESS(node) \
81         (struct Process *)((intptr_t)(node) - offsetof(struct Process, monitor.link))
82
83 size_t monitor_checkStack(cpustack_t *stack_base, size_t stack_size)
84 {
85         cpustack_t *beg;
86         cpustack_t *cur;
87         cpustack_t *end;
88         size_t sp_free;
89
90         beg = stack_base;
91         end = stack_base + stack_size / sizeof(cpustack_t) - 1;
92
93         if (CPU_STACK_GROWS_UPWARD)
94         {
95                 cur = beg;
96                 beg = end;
97                 end = cur;
98         }
99
100         cur = beg;
101         while (cur != end)
102         {
103                 if (*cur != CONFIG_KERN_STACKFILLCODE)
104                         break;
105
106                 if (CPU_STACK_GROWS_UPWARD)
107                         cur--;
108                 else
109                         cur++;
110         }
111
112         sp_free = ABS(cur - beg) * sizeof(cpustack_t);
113         return sp_free;
114 }
115
116
117 void monitor_report(void)
118 {
119         struct Process* p;
120         int i;
121
122         if (LIST_EMPTY(&MonitorProcs))
123         {
124                 kputs("No stacks registered in the monitor\n");
125                 return;
126         }
127
128         kprintf("%-24s%-8s%-8s%-8s%-8s\n", "Process name", "TCB", "SPbase", "SPsize", "SPfree");
129         for (i=0;i<56;i++)
130                 kputchar('-');
131         kputchar('\n');
132
133         for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
134                  p->monitor.link.succ;
135                  p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
136         {
137                 size_t free = monitor_checkStack(p->stack_base, p->stack_size);
138                 kprintf("%-24s%-8p%-8p%-8lu%-8lu\n",
139                         p->monitor.name, p, p->stack_base, p->stack_size, free);
140         }
141 }
142
143
144 static void NORETURN monitor(void)
145 {
146         struct Process *p;
147
148         while (1)
149         {
150                 for (p = MONITOR_NODE_TO_PROCESS(LIST_HEAD(&MonitorProcs));
151                         p->monitor.link.succ;
152                         p = MONITOR_NODE_TO_PROCESS(p->monitor.link.succ))
153                 {
154                         size_t free = monitor_checkStack(p->stack_base, p->stack_size);
155
156                         if (free < 0x20)
157                                 kprintf("MONITOR: WARNING: Free stack for process '%s' is only %u chars\n",
158                                                 p->monitor.name, (unsigned int)free);
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 */