RS: fix for fix
[minix.git] / kernel / debug.c
blob9f89363288da832a79ae5875b83284f7a59535d9
1 /* This file implements kernel debugging functionality that is not included
2 * in the standard kernel. Available functionality includes timing of lock
3 * functions and sanity checking of the scheduling queues.
4 */
6 #include "kernel.h"
8 #include <minix/callnr.h>
9 #include <minix/sysutil.h>
10 #include <minix/u64.h>
11 #include <limits.h>
12 #include <string.h>
13 #include <assert.h>
15 #define MAX_LOOP (NR_PROCS + NR_TASKS)
17 int runqueues_ok_cpu(unsigned cpu)
19 int q, l = 0;
20 register struct proc *xp;
21 struct proc **rdy_head, **rdy_tail;
23 rdy_head = get_cpu_var(cpu, run_q_head);
24 rdy_tail = get_cpu_var(cpu, run_q_tail);
26 for (xp = BEG_PROC_ADDR; xp < END_PROC_ADDR; ++xp) {
27 xp->p_found = 0;
28 if (l++ > MAX_LOOP) panic("check error");
31 for (q=l=0; q < NR_SCHED_QUEUES; q++) {
32 if (rdy_head[q] && !rdy_tail[q]) {
33 printf("head but no tail in %d\n", q);
34 return 0;
36 if (!rdy_head[q] && rdy_tail[q]) {
37 printf("tail but no head in %d\n", q);
38 return 0;
40 if (rdy_tail[q] && rdy_tail[q]->p_nextready) {
41 printf("tail and tail->next not null in %d\n", q);
42 return 0;
44 for(xp = rdy_head[q]; xp; xp = xp->p_nextready) {
45 const vir_bytes vxp = (vir_bytes) xp;
46 vir_bytes dxp;
47 if(vxp < (vir_bytes) BEG_PROC_ADDR || vxp >= (vir_bytes) END_PROC_ADDR) {
48 printf("xp out of range\n");
49 return 0;
51 dxp = vxp - (vir_bytes) BEG_PROC_ADDR;
52 if(dxp % sizeof(struct proc)) {
53 printf("xp not a real pointer");
54 return 0;
56 if(!proc_ptr_ok(xp)) {
57 printf("xp bogus pointer");
58 return 0;
60 if (RTS_ISSET(xp, RTS_SLOT_FREE)) {
61 printf("scheduling error: dead proc q %d %d\n",
62 q, xp->p_endpoint);
63 return 0;
65 if (!proc_is_runnable(xp)) {
66 printf("scheduling error: unready on runq %d proc %d\n",
67 q, xp->p_nr);
68 return 0;
70 if (xp->p_priority != q) {
71 printf("scheduling error: wrong priority q %d proc %d ep %d name %s\n",
72 q, xp->p_nr, xp->p_endpoint, xp->p_name);
73 return 0;
75 if (xp->p_found) {
76 printf("scheduling error: double sched q %d proc %d\n",
77 q, xp->p_nr);
78 return 0;
80 xp->p_found = 1;
81 if (!xp->p_nextready && rdy_tail[q] != xp) {
82 printf("sched err: last element not tail q %d proc %d\n",
83 q, xp->p_nr);
84 return 0;
86 if (l++ > MAX_LOOP) {
87 printf("loop in schedule queue?");
88 return 0;
93 for (xp = BEG_PROC_ADDR; xp < END_PROC_ADDR; ++xp) {
94 if(!proc_ptr_ok(xp)) {
95 printf("xp bogus pointer in proc table\n");
96 return 0;
98 if (isemptyp(xp))
99 continue;
100 if(proc_is_runnable(xp) && !xp->p_found) {
101 printf("sched error: ready proc %d not on queue\n", xp->p_nr);
102 return 0;
106 /* All is ok. */
107 return 1;
110 #ifdef CONFIG_SMP
111 static int runqueues_ok_all(void)
113 unsigned c;
115 for (c = 0 ; c < ncpus; c++) {
116 if (!runqueues_ok_cpu(c))
117 return 0;
119 return 1;
122 int runqueues_ok(void)
124 return runqueues_ok_all();
127 #else
129 int runqueues_ok(void)
131 return runqueues_ok_cpu(0);
135 #endif
137 char *
138 rtsflagstr(const u32_t flags)
140 static char str[100];
141 str[0] = '\0';
143 #define FLAG(n) if(flags & n) { strlcat(str, #n " ", sizeof(str)); }
145 FLAG(RTS_SLOT_FREE);
146 FLAG(RTS_PROC_STOP);
147 FLAG(RTS_SENDING);
148 FLAG(RTS_RECEIVING);
149 FLAG(RTS_SIGNALED);
150 FLAG(RTS_SIG_PENDING);
151 FLAG(RTS_P_STOP);
152 FLAG(RTS_NO_PRIV);
153 FLAG(RTS_NO_ENDPOINT);
154 FLAG(RTS_VMINHIBIT);
155 FLAG(RTS_PAGEFAULT);
156 FLAG(RTS_VMREQUEST);
157 FLAG(RTS_VMREQTARGET);
158 FLAG(RTS_PREEMPTED);
159 FLAG(RTS_NO_QUANTUM);
161 return str;
164 char *
165 miscflagstr(const u32_t flags)
167 static char str[100];
168 str[0] = '\0';
170 FLAG(MF_REPLY_PEND);
171 FLAG(MF_DELIVERMSG);
172 FLAG(MF_KCALL_RESUME);
174 return str;
177 char *
178 schedulerstr(struct proc *scheduler)
180 if (scheduler != NULL)
182 return scheduler->p_name;
185 return "KERNEL";
188 static void
189 print_proc_name(struct proc *pp)
191 char *name = pp->p_name;
192 endpoint_t ep = pp->p_endpoint;
194 if(name) {
195 printf("%s(%d)", name, ep);
197 else {
198 printf("%d", ep);
202 static void
203 print_endpoint(endpoint_t ep)
205 int proc_nr;
206 struct proc *pp = NULL;
208 switch(ep) {
209 case ANY:
210 printf("ANY");
211 break;
212 case SELF:
213 printf("SELF");
214 break;
215 case NONE:
216 printf("NONE");
217 break;
218 default:
219 if(!isokendpt(ep, &proc_nr)) {
220 printf("??? %d\n", ep);
222 else {
223 pp = proc_addr(proc_nr);
224 if(isemptyp(pp)) {
225 printf("??? empty slot %d\n", proc_nr);
227 else {
228 print_proc_name(pp);
231 break;
235 static void
236 print_sigmgr(struct proc *pp)
238 endpoint_t sig_mgr, bak_sig_mgr;
239 sig_mgr = priv(pp)->s_sig_mgr;
240 bak_sig_mgr = priv(pp)->s_bak_sig_mgr;
241 printf("sigmgr ");
242 print_endpoint(sig_mgr);
243 if(bak_sig_mgr != NONE) {
244 printf(" / ");
245 print_endpoint(bak_sig_mgr);
249 void print_proc(struct proc *pp)
251 endpoint_t dep;
253 printf("%d: %s %d prio %d time %d/%d cycles 0x%x%08x cpu %2d "
254 "pdbr 0x%lx rts %s misc %s sched %s ",
255 proc_nr(pp), pp->p_name, pp->p_endpoint,
256 pp->p_priority, pp->p_user_time,
257 pp->p_sys_time, ex64hi(pp->p_cycles),
258 ex64lo(pp->p_cycles), pp->p_cpu,
259 #if defined(__i386__)
260 pp->p_seg.p_cr3,
261 #elif defined(__arm__)
262 pp->p_seg.p_ttbr,
263 #endif
264 rtsflagstr(pp->p_rts_flags), miscflagstr(pp->p_misc_flags),
265 schedulerstr(pp->p_scheduler));
267 print_sigmgr(pp);
269 dep = P_BLOCKEDON(pp);
270 if(dep != NONE) {
271 printf(" blocked on: ");
272 print_endpoint(dep);
274 printf("\n");
277 static void print_proc_depends(struct proc *pp, const int level)
279 struct proc *depproc = NULL;
280 endpoint_t dep;
281 #define COL { int i; for(i = 0; i < level; i++) printf("> "); }
283 if(level >= NR_PROCS) {
284 printf("loop??\n");
285 return;
290 print_proc(pp);
293 proc_stacktrace(pp);
296 dep = P_BLOCKEDON(pp);
297 if(dep != NONE && dep != ANY) {
298 int procno;
299 if(isokendpt(dep, &procno)) {
300 depproc = proc_addr(procno);
301 if(isemptyp(depproc))
302 depproc = NULL;
304 if (depproc)
305 print_proc_depends(depproc, level+1);
309 void print_proc_recursive(struct proc *pp)
311 print_proc_depends(pp, 0);
314 #if DEBUG_DUMPIPC
315 static const char *mtypename(int mtype, int iscall)
317 /* use generated file to recognize message types */
318 if (iscall) {
319 switch(mtype) {
320 #define IDENT(x) case x: return #x;
321 #include "extracted-mtype.h"
322 #undef IDENT
324 } else {
325 switch(mtype) {
326 #define IDENT(x) case x: return #x;
327 #include "extracted-errno.h"
328 #undef IDENT
332 /* no match */
333 return NULL;
336 static void printproc(struct proc *rp)
338 if (rp)
339 printf(" %s(%d)", rp->p_name, rp - proc);
340 else
341 printf(" kernel");
344 static void printparam(const char *name, const void *data, size_t size)
346 printf(" %s=", name);
347 switch (size) {
348 case sizeof(char): printf("%d", *(char *) data); break;
349 case sizeof(short): printf("%d", *(short *) data); break;
350 case sizeof(int): printf("%d", *(int *) data); break;
351 default: printf("(%u bytes)", size); break;
355 static void printmsg(message *msg, struct proc *src, struct proc *dst,
356 char operation, int iscall, int printparams)
358 const char *name;
359 int mtype = msg->m_type;
361 /* source, destination and message type */
362 printf("%c", operation);
363 printproc(src);
364 printproc(dst);
365 name = mtypename(mtype, iscall);
366 if (name) {
367 printf(" %s(%d)", name, mtype);
368 } else {
369 printf(" %d", mtype);
372 if (iscall && printparams) {
373 #define IDENT(x, y) if (mtype == x) printparam(#y, &msg->y, sizeof(msg->y));
374 #include "extracted-mfield.h"
375 #undef IDENT
377 printf("\n");
379 #endif
381 #if DEBUG_IPCSTATS
382 #define IPCPROCS (NR_PROCS+1) /* number of slots we need */
383 #define KERNELIPC NR_PROCS /* slot number to use for kernel calls */
384 static int messages[IPCPROCS][IPCPROCS];
386 #define PRINTSLOTS 20
387 static struct {
388 int src, dst, messages;
389 } winners[PRINTSLOTS];
390 static int total, goodslots;
392 static void printstats(int ticks)
394 int i;
395 for(i = 0; i < goodslots; i++) {
396 #define name(s) (s == KERNELIPC ? "kernel" : proc_addr(s)->p_name)
397 #define persec(n) (system_hz*(n)/ticks)
398 char *n1 = name(winners[i].src),
399 *n2 = name(winners[i].dst);
400 printf("%2d. %8s -> %8s %9d/s\n",
401 i, n1, n2, persec(winners[i].messages));
403 printf("total %d/s\n", persec(total));
406 static void sortstats(void)
408 /* Print top message senders/receivers. */
409 int src_slot, dst_slot;
410 total = goodslots = 0;
411 for(src_slot = 0; src_slot < IPCPROCS; src_slot++) {
412 for(dst_slot = 0; dst_slot < IPCPROCS; dst_slot++) {
413 int w = PRINTSLOTS, rem,
414 n = messages[src_slot][dst_slot];
415 total += n;
416 while(w > 0 && n > winners[w-1].messages)
417 w--;
418 if(w >= PRINTSLOTS) continue;
420 /* This combination has beaten the current winners
421 * and should be inserted at position 'w.'
423 rem = PRINTSLOTS-w-1;
424 assert(rem >= 0);
425 assert(rem < PRINTSLOTS);
426 if(rem > 0) {
427 assert(w+1 <= PRINTSLOTS-1);
428 assert(w >= 0);
429 memmove(&winners[w+1], &winners[w],
430 rem*sizeof(winners[0]));
432 winners[w].src = src_slot;
433 winners[w].dst = dst_slot;
434 winners[w].messages = n;
435 if(goodslots < PRINTSLOTS) goodslots++;
440 #define proc2slot(p, s) { \
441 if(p) { s = p->p_nr; } \
442 else { s = KERNELIPC; } \
443 assert(s >= 0 && s < IPCPROCS); \
446 static void statmsg(message *msg, struct proc *srcp, struct proc *dstp)
448 int src, dst, now, secs, dt;
449 static int lastprint;
451 /* Stat message. */
452 assert(src);
453 proc2slot(srcp, src);
454 proc2slot(dstp, dst);
455 messages[src][dst]++;
457 /* Print something? */
458 now = get_uptime();
459 dt = now - lastprint;
460 secs = dt/system_hz;
461 if(secs >= 30) {
462 memset(winners, 0, sizeof(winners));
463 sortstats();
464 printstats(dt);
465 memset(messages, 0, sizeof(messages));
466 lastprint = now;
469 #endif
471 #if DEBUG_IPC_HOOK
472 void hook_ipc_msgkcall(message *msg, struct proc *proc)
474 #if DEBUG_DUMPIPC
475 printmsg(msg, proc, NULL, 'k', 1, 1);
476 #endif
479 void hook_ipc_msgkresult(message *msg, struct proc *proc)
481 #if DEBUG_DUMPIPC
482 printmsg(msg, NULL, proc, 'k', 0, 0);
483 #endif
484 #if DEBUG_IPCSTATS
485 statmsg(msg, proc, NULL);
486 #endif
489 void hook_ipc_msgrecv(message *msg, struct proc *src, struct proc *dst)
491 #if DEBUG_DUMPIPC
492 printmsg(msg, src, dst, 'r', src->p_misc_flags & MF_REPLY_PEND, 0);
493 #endif
494 #if DEBUG_IPCSTATS
495 statmsg(msg, src, dst);
496 #endif
499 void hook_ipc_msgsend(message *msg, struct proc *src, struct proc *dst)
501 #if DEBUG_DUMPIPC
502 printmsg(msg, src, dst, 's', src->p_misc_flags & MF_REPLY_PEND, 1);
503 #endif
506 void hook_ipc_clear(struct proc *p)
508 #if DEBUG_IPCSTATS
509 int slot, i;
510 assert(p);
511 proc2slot(p, slot);
512 for(i = 0; i < IPCPROCS; i++)
513 messages[slot][i] = messages[i][slot] = 0;
514 #endif
516 #endif