3.1.7 branch.
[minix.git] / kernel / debug.c
blob4199fc2d744d85974cdfc4da4d07c06325a023e7
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"
7 #include "proc.h"
8 #include "debug.h"
10 #include <minix/sysutil.h>
11 #include <limits.h>
12 #include <string.h>
14 #define MAX_LOOP (NR_PROCS + NR_TASKS)
16 PUBLIC int
17 runqueues_ok(void)
19 int q, l = 0;
20 register struct proc *xp;
22 for (xp = BEG_PROC_ADDR; xp < END_PROC_ADDR; ++xp) {
23 xp->p_found = 0;
24 if (l++ > MAX_LOOP) panic("check error");
27 for (q=l=0; q < NR_SCHED_QUEUES; q++) {
28 if (rdy_head[q] && !rdy_tail[q]) {
29 printf("head but no tail in %d\n", q);
30 return 0;
32 if (!rdy_head[q] && rdy_tail[q]) {
33 printf("tail but no head in %d\n", q);
34 return 0;
36 if (rdy_tail[q] && rdy_tail[q]->p_nextready) {
37 printf("tail and tail->next not null in %d\n", q);
38 return 0;
40 for(xp = rdy_head[q]; xp; xp = xp->p_nextready) {
41 const vir_bytes vxp = (vir_bytes) xp;
42 vir_bytes dxp;
43 if(vxp < (vir_bytes) BEG_PROC_ADDR || vxp >= (vir_bytes) END_PROC_ADDR) {
44 printf("xp out of range\n");
45 return 0;
47 dxp = vxp - (vir_bytes) BEG_PROC_ADDR;
48 if(dxp % sizeof(struct proc)) {
49 printf("xp not a real pointer");
50 return 0;
52 if(!proc_ptr_ok(xp)) {
53 printf("xp bogus pointer");
54 return 0;
56 if (RTS_ISSET(xp, RTS_SLOT_FREE)) {
57 printf("scheduling error: dead proc q %d %d\n",
58 q, xp->p_endpoint);
59 return 0;
61 if (!proc_is_runnable(xp)) {
62 printf("scheduling error: unready on runq %d proc %d\n",
63 q, xp->p_nr);
64 return 0;
66 if (xp->p_priority != q) {
67 printf("scheduling error: wrong priority q %d proc %d ep %d name %s\n",
68 q, xp->p_nr, xp->p_endpoint, xp->p_name);
69 return 0;
71 if (xp->p_found) {
72 printf("scheduling error: double sched q %d proc %d\n",
73 q, xp->p_nr);
74 return 0;
76 xp->p_found = 1;
77 if (!xp->p_nextready && rdy_tail[q] != xp) {
78 printf("sched err: last element not tail q %d proc %d\n",
79 q, xp->p_nr);
80 return 0;
82 if (l++ > MAX_LOOP) {
83 printf("loop in schedule queue?");
84 return 0;
89 l = 0;
90 for (xp = BEG_PROC_ADDR; xp < END_PROC_ADDR; ++xp) {
91 if(!proc_ptr_ok(xp)) {
92 printf("xp bogus pointer in proc table\n");
93 return 0;
95 if (isemptyp(xp))
96 continue;
97 if(proc_is_runnable(xp) && !xp->p_found) {
98 printf("sched error: ready proc %d not on queue\n", xp->p_nr);
99 return 0;
100 if (l++ > MAX_LOOP) {
101 printf("loop in debug.c?\n");
102 return 0;
107 /* All is ok. */
108 return 1;
111 PUBLIC char *
112 rtsflagstr(const int flags)
114 static char str[100];
115 str[0] = '\0';
117 #define FLAG(n) if(flags & n) { strcat(str, #n " "); }
119 FLAG(RTS_SLOT_FREE);
120 FLAG(RTS_PROC_STOP);
121 FLAG(RTS_SENDING);
122 FLAG(RTS_RECEIVING);
123 FLAG(RTS_SIGNALED);
124 FLAG(RTS_SIG_PENDING);
125 FLAG(RTS_P_STOP);
126 FLAG(RTS_NO_PRIV);
127 FLAG(RTS_NO_ENDPOINT);
128 FLAG(RTS_VMINHIBIT);
129 FLAG(RTS_PAGEFAULT);
130 FLAG(RTS_VMREQUEST);
131 FLAG(RTS_VMREQTARGET);
132 FLAG(RTS_PREEMPTED);
133 FLAG(RTS_NO_QUANTUM);
135 return str;
138 PUBLIC char *
139 miscflagstr(const int flags)
141 static char str[100];
142 str[0] = '\0';
144 FLAG(MF_REPLY_PEND);
145 FLAG(MF_ASYNMSG);
146 FLAG(MF_FULLVM);
147 FLAG(MF_DELIVERMSG);
148 FLAG(MF_KCALL_RESUME);
150 return str;
153 PUBLIC char *
154 schedulerstr(struct proc *scheduler)
156 if (scheduler != NULL)
158 return scheduler->p_name;
161 return "KERNEL";
164 PUBLIC void print_proc(struct proc *pp)
166 struct proc *depproc = NULL;
167 endpoint_t dep;
169 printf("%d: %s %d prio %d time %d/%d cycles 0x%x%08x cr3 0x%lx rts %s misc %s sched %s",
170 proc_nr(pp), pp->p_name, pp->p_endpoint,
171 pp->p_priority, pp->p_user_time,
172 pp->p_sys_time, pp->p_cycles.hi, pp->p_cycles.lo, pp->p_seg.p_cr3,
173 rtsflagstr(pp->p_rts_flags), miscflagstr(pp->p_misc_flags),
174 schedulerstr(pp->p_scheduler));
176 dep = P_BLOCKEDON(pp);
177 if(dep != NONE) {
178 printf(" blocked on: ");
179 if(dep == ANY) {
180 printf(" ANY\n");
181 } else {
182 int procno;
183 if(!isokendpt(dep, &procno)) {
184 printf(" ??? %d\n", dep);
185 } else {
186 depproc = proc_addr(procno);
187 if(isemptyp(depproc)) {
188 printf(" empty slot %d???\n", procno);
189 depproc = NULL;
190 } else {
191 printf(" %s\n", depproc->p_name);
195 } else {
196 printf("\n");
200 PRIVATE void print_proc_depends(struct proc *pp, const int level)
202 struct proc *depproc = NULL;
203 endpoint_t dep;
204 #define COL { int i; for(i = 0; i < level; i++) printf("> "); }
206 if(level >= NR_PROCS) {
207 printf("loop??\n");
208 return;
213 print_proc(pp);
216 proc_stacktrace(pp);
219 dep = P_BLOCKEDON(pp);
220 if(dep != NONE && dep != ANY) {
221 int procno;
222 if(isokendpt(dep, &procno)) {
223 depproc = proc_addr(procno);
224 if(isemptyp(depproc))
225 depproc = NULL;
227 if (depproc)
228 print_proc_depends(depproc, level+1);
232 PUBLIC void print_proc_recursive(struct proc *pp)
234 print_proc_depends(pp, 0);