Merge branch "preempt" in "trunk".
[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 <cfg/macros.h>
46 #include <cfg/debug.h>
47
48 #include <struct/list.h>
49
50 #include <drv/timer.h>
51
52 #include <kern/proc.h>
53
54 #include <cpu/frame.h> /* CPU_STACK_GROWS_UPWARD */
55
56 /* Access to this list must be protected against the scheduler */
57 static List MonitorProcs;
58
59 void monitor_init(void)
60 {
61         LIST_INIT(&MonitorProcs);
62 }
63
64
65 void monitor_add(Process *proc, const char *name)
66 {
67         proc->monitor.name = name;
68
69         PROC_ATOMIC(ADDTAIL(&MonitorProcs, &proc->monitor.link));
70 }
71
72
73 void monitor_remove(Process *proc)
74 {
75         PROC_ATOMIC(REMOVE(&proc->monitor.link));
76 }
77
78 void monitor_rename(Process *proc, const char *name)
79 {
80         proc->monitor.name = name;
81 }
82
83 size_t monitor_checkStack(cpu_stack_t *stack_base, size_t stack_size)
84 {
85         cpu_stack_t *beg;
86         cpu_stack_t *cur;
87         cpu_stack_t *end;
88         int inc;
89         size_t sp_free;
90
91
92         beg = stack_base;
93         end = stack_base + stack_size / sizeof(cpu_stack_t);
94         inc = +1;
95
96         if (CPU_STACK_GROWS_UPWARD)
97         {
98                 SWAP(beg, end);
99                 inc = -1;
100         }
101
102         cur = beg;
103         while (cur != end)
104         {
105                 if (*cur != CONFIG_KERN_STACKFILLCODE)
106                         break;
107
108                 cur += inc;
109         }
110
111         sp_free = ABS(cur - beg) * sizeof(cpu_stack_t);
112         return sp_free;
113 }
114
115
116 void monitor_report(void)
117 {
118         Node *node;
119         int i;
120
121         proc_forbid();
122         kprintf("%-9s%-9s%-9s%-9s%s\n", "TCB", "SPbase", "SPsize", "SPfree", "Name");
123         for (i = 0; i < 56; i++)
124                 kputchar('-');
125         kputchar('\n');
126
127         FOREACH_NODE(node, &MonitorProcs)
128         {
129                 Process *p = containerof(node, Process, monitor.link);
130                 size_t free = monitor_checkStack(p->stack_base, p->stack_size);
131                 kprintf("%-9p%-9p%-9zu%-9zu%s\n",
132                         p, p->stack_base, p->stack_size, free, p->monitor.name);
133         }
134         proc_permit();
135 }
136
137
138 static void NORETURN monitor(void)
139 {
140         Node *node;
141
142         for (;;)
143         {
144                 proc_forbid();
145                 FOREACH_NODE(node, &MonitorProcs)
146                 {
147                         Process *p = containerof(node, Process, monitor.link);
148                         size_t free = monitor_checkStack(p->stack_base, p->stack_size);
149
150                         if (free < 0x20)
151                                 kprintf("MONITOR: Free stack of process '%s' is only %u chars\n",
152                                                 p->monitor.name, (unsigned int)free);
153                 }
154                 proc_permit();
155
156                 /* Give some rest to the system */
157                 timer_delay(500);
158         }
159 }
160
161 void monitor_start(size_t stacksize, cpu_stack_t *stack)
162 {
163         struct Process *p = proc_new(monitor, NULL, stacksize, stack);
164         proc_setPri(p, -10);
165 }
166
167 #endif /* CONFIG_KERN_MONITOR */