monitor.c
Go to the documentation of this file.00001
00040 #include "monitor.h"
00041
00042 #if CONFIG_KERN_MONITOR
00043
00044 #include "proc_p.h"
00045 #include <cfg/macros.h>
00046 #include <cfg/debug.h>
00047
00048 #include <struct/list.h>
00049
00050 #include <drv/timer.h>
00051
00052 #include <kern/proc.h>
00053
00054 #include <cpu/frame.h>
00055
00056
00057 static List MonitorProcs;
00058
00059 void monitor_init(void)
00060 {
00061 LIST_INIT(&MonitorProcs);
00062 }
00063
00064
00065 void monitor_add(Process *proc, const char *name)
00066 {
00067 proc->monitor.name = name;
00068
00069 PROC_ATOMIC(ADDTAIL(&MonitorProcs, &proc->monitor.link));
00070 }
00071
00072
00073 void monitor_remove(Process *proc)
00074 {
00075 PROC_ATOMIC(REMOVE(&proc->monitor.link));
00076 }
00077
00078 void monitor_rename(Process *proc, const char *name)
00079 {
00080 proc->monitor.name = name;
00081 }
00082
00083 size_t monitor_checkStack(cpu_stack_t *stack_base, size_t stack_size)
00084 {
00085 cpu_stack_t *beg;
00086 cpu_stack_t *cur;
00087 cpu_stack_t *end;
00088 int inc;
00089 size_t sp_free;
00090
00091
00092 beg = stack_base;
00093 end = stack_base + stack_size / sizeof(cpu_stack_t);
00094 inc = +1;
00095
00096 if (CPU_STACK_GROWS_UPWARD)
00097 {
00098 SWAP(beg, end);
00099 inc = -1;
00100 }
00101
00102 cur = beg;
00103 while (cur != end)
00104 {
00105 if (*cur != CONFIG_KERN_STACKFILLCODE)
00106 break;
00107
00108 cur += inc;
00109 }
00110
00111 sp_free = ABS(cur - beg) * sizeof(cpu_stack_t);
00112 return sp_free;
00113 }
00114
00115
00116 void monitor_report(void)
00117 {
00118 Node *node;
00119 int i;
00120
00121 kprintf("%-9s%-9s%-9s%-9s%s\n", "TCB", "SPbase", "SPsize", "SPfree", "Name");
00122 for (i = 0; i < 56; i++)
00123 kputchar('-');
00124 kputchar('\n');
00125
00126 proc_forbid();
00127 FOREACH_NODE(node, &MonitorProcs)
00128 {
00129 Process *p = containerof(node, Process, monitor.link);
00130 size_t free = monitor_checkStack(p->stack_base, p->stack_size);
00131 kprintf("%-9p%-9p%-9zu%-9zu%s\n",
00132 p, p->stack_base, p->stack_size, free, p->monitor.name);
00133 }
00134 proc_permit();
00135 }
00136
00137
00138 static void NORETURN monitor(void)
00139 {
00140 Node *node;
00141
00142 for (;;)
00143 {
00144 proc_forbid();
00145 FOREACH_NODE(node, &MonitorProcs)
00146 {
00147 Process *p = containerof(node, Process, monitor.link);
00148 size_t free = monitor_checkStack(p->stack_base, p->stack_size);
00149
00150 if (free < 0x20)
00151 kprintf("MONITOR: Free stack of process '%s' is only %u chars\n",
00152 p->monitor.name, (unsigned int)free);
00153 }
00154 proc_permit();
00155
00156
00157 timer_delay(500);
00158 }
00159 }
00160
00161 void monitor_start(size_t stacksize, cpu_stack_t *stack)
00162 {
00163 struct Process *p = proc_new(monitor, NULL, stacksize, stack);
00164 proc_setPri(p, -10);
00165 }
00166
00167 #endif