1 | /*
|
---|
2 | * mdb.c - MINIX program debugger
|
---|
3 | *
|
---|
4 | * Written by Bruce D. Szablak
|
---|
5 | *
|
---|
6 | * This free software is provided for non-commerical use. No warrantee
|
---|
7 | * of fitness for any use is implied. You get what you pay for. Anyone
|
---|
8 | * may make modifications and distribute them, but please keep this header
|
---|
9 | * in the distribution.
|
---|
10 | */
|
---|
11 |
|
---|
12 | /*
|
---|
13 | * Originally ported to MINIX-PC and MINIX-386 by Bruce Evans.
|
---|
14 | * NB: the original sym.c and mdbdis86.c come from his 'db'
|
---|
15 | *
|
---|
16 | * Added by Philip Murton:
|
---|
17 | *
|
---|
18 | * 2.0 'Core' file functions
|
---|
19 | * 2.1 Support for GNU exec
|
---|
20 | * 2.2 Changes for Minix 1.6.x Beta
|
---|
21 | * 2.3 Changes for Minix 1.7.0 and trace syscalls
|
---|
22 | * 2.4 Changes for Minix 1.7.2 and clean up
|
---|
23 | * 2.5.1 Add better help
|
---|
24 | * 2.5.2 Added io.c for logging options
|
---|
25 | * 2.5.3 Minor changes and tested with Minix 1.7.4
|
---|
26 | * 2.5.4 Command arguments processing improved (Thanks to Will Rose)
|
---|
27 | * 2.6.0 Final Version for MINIX CD (Sept/96)
|
---|
28 | */
|
---|
29 |
|
---|
30 | #define _MAIN_MDB
|
---|
31 | #include "mdb.h"
|
---|
32 |
|
---|
33 | #include <minix/type.h>
|
---|
34 |
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <signal.h>
|
---|
37 | #include <string.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <unistd.h>
|
---|
40 | #include <ctype.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <sys/wait.h>
|
---|
43 | #define ptrace mdbtrace
|
---|
44 | #include <sys/ptrace.h>
|
---|
45 | #include <setjmp.h>
|
---|
46 | #include "proto.h"
|
---|
47 |
|
---|
48 | #include <kernel/const.h>
|
---|
49 | #include <kernel/type.h>
|
---|
50 | #include <kernel/proc.h>
|
---|
51 |
|
---|
52 | /* buffer for proc and pointer to proc */
|
---|
53 | extern struct proc *prc;
|
---|
54 |
|
---|
55 | #define MAXLINE 128
|
---|
56 | #define MAXARG 20
|
---|
57 |
|
---|
58 | PRIVATE unsigned long lastexp = 0L; /* last expression and segment */
|
---|
59 | PRIVATE int lastseg = NOSEG;
|
---|
60 | PRIVATE char *prog; /* prog name */
|
---|
61 | PRIVATE char sbuf[MAXLINE];
|
---|
62 | PRIVATE char cbuf[MAXLINE];
|
---|
63 | PRIVATE char *cmd; /* current command */
|
---|
64 | PRIVATE char *cmdstart; /* start of command */
|
---|
65 | PRIVATE jmp_buf mainlp;
|
---|
66 |
|
---|
67 |
|
---|
68 | struct b_pnt {
|
---|
69 | struct b_pnt *nxt, *prv;
|
---|
70 | long addr;
|
---|
71 | long oldval;
|
---|
72 | char cmd[1];
|
---|
73 | } *b_head, *curpnt;
|
---|
74 |
|
---|
75 | _PROTOTYPE( void main , (int argc, char *argv[]));
|
---|
76 |
|
---|
77 | FORWARD _PROTOTYPE( void cleanup , (void));
|
---|
78 | FORWARD _PROTOTYPE( void freepnt , (struct b_pnt *pnt ));
|
---|
79 | FORWARD _PROTOTYPE( void findbpnt , (int verbose ));
|
---|
80 | FORWARD _PROTOTYPE( int exebpnt , (int restart ));
|
---|
81 | FORWARD _PROTOTYPE( void catch , (int sig ));
|
---|
82 | FORWARD _PROTOTYPE( int run , (char *name , char *argstr , int tflg ));
|
---|
83 | FORWARD _PROTOTYPE( int dowait , (void));
|
---|
84 | FORWARD _PROTOTYPE( void backtrace , (int all ));
|
---|
85 | FORWARD _PROTOTYPE( void modify , (long addr , int cnt , int verbose , int size ));
|
---|
86 | FORWARD _PROTOTYPE( void display , (long addr , int req ));
|
---|
87 | FORWARD _PROTOTYPE( void fill , (long addr , int req ));
|
---|
88 | FORWARD _PROTOTYPE( void dorun , (char *cmd ));
|
---|
89 | FORWARD _PROTOTYPE( void not_for_core , (void));
|
---|
90 | FORWARD _PROTOTYPE( void command , (void));
|
---|
91 |
|
---|
92 |
|
---|
93 | PRIVATE void cleanup()
|
---|
94 | {
|
---|
95 | curpid = 0;
|
---|
96 | curpnt = NULL;
|
---|
97 | while (b_head) freepnt(b_head);
|
---|
98 | }
|
---|
99 |
|
---|
100 | PRIVATE void findbpnt(verbose)
|
---|
101 | int verbose;
|
---|
102 | {
|
---|
103 | for (curpnt = b_head; curpnt; curpnt = curpnt->nxt) {
|
---|
104 | if (curpnt->addr == PC_MEMBER(prc) - BREAKPOINT_ADVANCE) {
|
---|
105 | ptrace(T_SETINS, curpid, curpnt->addr, curpnt->oldval);
|
---|
106 | ptrace(T_SETUSER, curpid, PC_OFF, curpnt->addr);
|
---|
107 | #if SYSCALLS_SUPPORT
|
---|
108 | if( syscalls )
|
---|
109 | do_syscall(curpnt->addr);
|
---|
110 | else if (curpnt->cmd[0] != '\n')
|
---|
111 | #else
|
---|
112 | if (curpnt->cmd[0] != '\n')
|
---|
113 | #endif
|
---|
114 | cmd = strcpy(cbuf, curpnt->cmd);
|
---|
115 | else if (verbose)
|
---|
116 | Printf("Breakpoint hit.\n");
|
---|
117 | return;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | if (verbose) Printf("Unknown breakpoint hit.\n");
|
---|
121 | }
|
---|
122 |
|
---|
123 | PRIVATE int exebpnt(restart)
|
---|
124 | int restart;
|
---|
125 | {
|
---|
126 | ptrace(T_STEP, curpid, 0L, (long) restart);
|
---|
127 | if (dowait() == 0) return TRUE;
|
---|
128 | ptrace(T_SETINS, curpid, curpnt->addr, BREAK(curpnt->oldval));
|
---|
129 | curpnt = NULL;
|
---|
130 | return FALSE;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | PRIVATE void freepnt(pnt)
|
---|
135 | struct b_pnt *pnt;
|
---|
136 | {
|
---|
137 | if (pnt->prv)
|
---|
138 | pnt->prv->nxt = pnt->nxt;
|
---|
139 | else
|
---|
140 | b_head = pnt->nxt;
|
---|
141 | if (pnt->nxt) pnt->nxt->prv = pnt->prv;
|
---|
142 | if (curpid > 0) ptrace(T_SETINS, curpid, pnt->addr, pnt->oldval);
|
---|
143 | free(pnt);
|
---|
144 | if (pnt == curpnt) curpnt = NULL;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | PUBLIC long breakpt(addr, cmd)
|
---|
149 | long addr;
|
---|
150 | char *cmd;
|
---|
151 | {
|
---|
152 | struct b_pnt *new;
|
---|
153 |
|
---|
154 | if (curpid <= 0) {
|
---|
155 | Printf("No active process.\n");
|
---|
156 | return 0L;
|
---|
157 | }
|
---|
158 | for (new = b_head; new; new = new->nxt)
|
---|
159 | if (new->addr == addr) {
|
---|
160 | Printf("Breakpoint already exists here.\n");
|
---|
161 | return 0L;
|
---|
162 | }
|
---|
163 | new = (struct b_pnt *) malloc(sizeof(struct b_pnt) + strlen(cmd));
|
---|
164 | if (new == NULL) {
|
---|
165 | Printf("No room for new breakpoint.\n");
|
---|
166 | return 0L;
|
---|
167 | }
|
---|
168 | new->nxt = b_head;
|
---|
169 | new->prv = 0;
|
---|
170 | if (b_head) b_head->prv = new;
|
---|
171 | b_head = new;
|
---|
172 | new->addr = addr;
|
---|
173 | strcpy(new->cmd, cmd);
|
---|
174 | new->oldval = ptrace(T_GETINS, curpid, addr, 0L);
|
---|
175 | ptrace(T_SETINS, curpid, addr, BREAK(new->oldval));
|
---|
176 | if (ptrace(T_GETINS, curpid, addr, 0L) != BREAK(new->oldval)) {
|
---|
177 | do_error("Can't set breakpoint");
|
---|
178 | freepnt(new);
|
---|
179 | return 0L;
|
---|
180 | }
|
---|
181 | return new->oldval;
|
---|
182 | }
|
---|
183 |
|
---|
184 | PRIVATE void catch(sig)
|
---|
185 | int sig;
|
---|
186 | {
|
---|
187 | signal(sig, catch);
|
---|
188 | if (sig == SIGINT || sig == SIGQUIT) return;
|
---|
189 | tstart(T_EXIT, 0, sig, 0);
|
---|
190 | exit(0);
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | PRIVATE int dowait()
|
---|
195 | {
|
---|
196 | int stat;
|
---|
197 |
|
---|
198 | if (corepid > 0) return cursig = 0;
|
---|
199 | while (wait(&stat) != curpid) {};
|
---|
200 | if ( WIFEXITED(stat) ) {
|
---|
201 | if (WEXITSTATUS(stat) != 127)
|
---|
202 | Printf("child exited with status %d\n", WEXITSTATUS(stat));
|
---|
203 | cleanup();
|
---|
204 | return 0;
|
---|
205 | }
|
---|
206 | if ( WIFSIGNALED(stat) ) {
|
---|
207 | Printf("child terminated by signal %d\n", WTERMSIG(stat) );
|
---|
208 | if (_LOW(stat) & 0x80) Printf("(core dumped)\n");
|
---|
209 | cleanup();
|
---|
210 | return 0;
|
---|
211 | }
|
---|
212 | return cursig = WSTOPSIG(stat);
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 |
|
---|
217 | PUBLIC void tstart(req, verbose, val, cnt)
|
---|
218 | int req, verbose, val, cnt;
|
---|
219 | {
|
---|
220 | if (curpid == 0) {
|
---|
221 | if (verbose) Printf("No active process.\n");
|
---|
222 | return;
|
---|
223 | }
|
---|
224 | if (req == T_EXIT) {
|
---|
225 | ptrace(T_EXIT, curpid, 0L, (long) val);
|
---|
226 | dowait();
|
---|
227 | return;
|
---|
228 | }
|
---|
229 | if (cnt == 0) cnt = 1;
|
---|
230 | do {
|
---|
231 | if (curpnt) {
|
---|
232 | if (exebpnt(val)) return;
|
---|
233 | if (req == T_RESUME) cnt++;
|
---|
234 | val = 0;
|
---|
235 | } else {
|
---|
236 | ptrace(req, curpid, 0L, (long) val);
|
---|
237 | if (dowait() == 0) return;
|
---|
238 | val = 0;
|
---|
239 | switch (cursig) {
|
---|
240 | case SIGEMT: /* breakpoint */
|
---|
241 | update();
|
---|
242 | findbpnt(cnt <= 1);
|
---|
243 | break;
|
---|
244 | case SIGTRAP: /* trace trap? */
|
---|
245 | if (req == T_STEP) break;
|
---|
246 | default: /* signal */
|
---|
247 | val = cursig;
|
---|
248 | break;
|
---|
249 | }
|
---|
250 | }
|
---|
251 | }
|
---|
252 | while (--cnt > 0);
|
---|
253 | update();
|
---|
254 | if ( verbose ) dasm((long) PC_MEMBER(prc), 1, 1);
|
---|
255 | }
|
---|
256 |
|
---|
257 | PRIVATE int run(name, argstr, tflg)
|
---|
258 | char *name, *argstr;
|
---|
259 | int tflg;
|
---|
260 | {
|
---|
261 | int procid;
|
---|
262 | char *argv[MAXARG], *inf = NULL, *outf = NULL;
|
---|
263 | int argc;
|
---|
264 |
|
---|
265 | if ((procid = fork()) == 0) {
|
---|
266 | /* trace me */
|
---|
267 | if (tflg) ptrace(T_OK, 0, 0L, 0L);
|
---|
268 | argv[0] = name;
|
---|
269 | for (argc = 1;;) {
|
---|
270 | argstr = skip(argstr);
|
---|
271 | if (*argstr == '\n' || *argstr == ';') {
|
---|
272 | argv[argc] = 0;
|
---|
273 | if (inf) freopen(inf, "r", stdin);
|
---|
274 | if (outf) freopen(outf, "w", stdout);
|
---|
275 | if (tflg) {
|
---|
276 | execv(name, argv);
|
---|
277 | do_error("execv");
|
---|
278 | } else {
|
---|
279 | execvp(name, argv);
|
---|
280 | do_error("execvp");
|
---|
281 | }
|
---|
282 | exit(127);
|
---|
283 | }
|
---|
284 | if (*argstr == '<')
|
---|
285 | inf = argstr + 1;
|
---|
286 | else if (*argstr == '>')
|
---|
287 | outf = argstr + 1;
|
---|
288 | else if (argc == MAXARG) {
|
---|
289 | Printf("Too many arguments.\n");
|
---|
290 | exit(127);
|
---|
291 | } else
|
---|
292 | argv[argc++] = argstr;
|
---|
293 | while (!isspace(*argstr)) argstr++;
|
---|
294 | if (*argstr == '\n') argstr[1] = '\n', argstr[2] = 0;
|
---|
295 | *argstr++ = 0;
|
---|
296 | }
|
---|
297 | }
|
---|
298 | if (procid < 0) do_error("Fork failed.\n");
|
---|
299 | return procid;
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | PRIVATE void dorun(cmd)
|
---|
304 | char *cmd;
|
---|
305 | {
|
---|
306 | if (curpid = run(prog, cmd, 1)) {
|
---|
307 | if (dowait()) {
|
---|
308 | ptrace(T_SETUSER, curpid, BP_OFF, 0L);
|
---|
309 | update();
|
---|
310 | Printf("Process stopped.\n");
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | /*
|
---|
316 | * backtrace - inspect the stack
|
---|
317 | */
|
---|
318 | PRIVATE void backtrace(all)
|
---|
319 | int all;
|
---|
320 | {
|
---|
321 | unsigned long pc, bp, off, val, obp;
|
---|
322 |
|
---|
323 | if (curpid <= 0) {
|
---|
324 | Printf("No process.\n");
|
---|
325 | return;
|
---|
326 | }
|
---|
327 | pc = get_reg(curpid,PC_OFF);
|
---|
328 | bp = get_reg(curpid,BP_OFF);
|
---|
329 | if (bp == 0) {
|
---|
330 | Printf("No active frame.\n");
|
---|
331 | return;
|
---|
332 | }
|
---|
333 | errno = 0;
|
---|
334 | do {
|
---|
335 | symbolic(pc, '(');
|
---|
336 | pc = (ptrace(T_GETDATA, curpid, bp + ADDRSIZE, 0L)
|
---|
337 | >> SHIFT(ADDRSIZE)) & MASK(ADDRSIZE);
|
---|
338 | off = ptrace(T_GETINS, curpid, pc, 0L);
|
---|
339 | #ifdef DEBUG
|
---|
340 | if(debug)
|
---|
341 | Printf("Return address %lx Value %lx\n",pc,off);
|
---|
342 | #endif
|
---|
343 | obp = bp;
|
---|
344 | bp += 2 * ADDRSIZE;
|
---|
345 |
|
---|
346 | /* Check for various instruction used to restore the stack.
|
---|
347 | * Should gives us the number of arguments.
|
---|
348 | * This is obvious dependent on interal features of the
|
---|
349 | * compiler used.
|
---|
350 | */
|
---|
351 | if (ADDQ(off)) off = ADDQ_CNT(off) + bp;
|
---|
352 | #ifdef __mc68000__
|
---|
353 | else if (LEA(off))
|
---|
354 | off = LEA_DISP(off) + bp;
|
---|
355 | #endif
|
---|
356 | else if (ADDA(off))
|
---|
357 | off = ADDA_CNT(ptrace(T_GETINS, curpid, pc + 2, 0L)) + bp;
|
---|
358 | #if (CHIP == INTEL)
|
---|
359 | else if (INCSP2(off))
|
---|
360 | off = bp + 2*INTSIZE;
|
---|
361 | else if (POPBX2(off))
|
---|
362 | off = bp + 2*INTSIZE;
|
---|
363 | else if (POPCX2(off))
|
---|
364 | off = bp + 2*INTSIZE;
|
---|
365 | else if (POPBX(off))
|
---|
366 | off = bp + INTSIZE;
|
---|
367 | else if (POPCX(off))
|
---|
368 | off = bp + INTSIZE;
|
---|
369 | #endif
|
---|
370 | else
|
---|
371 | goto skiplp;
|
---|
372 |
|
---|
373 | #ifdef DEBUG
|
---|
374 | if (debug)
|
---|
375 | Printf("Number of arguments: %d\n",(off-bp)/INTSIZE);
|
---|
376 | #endif
|
---|
377 |
|
---|
378 | for (;;) {
|
---|
379 | if (errno) return;
|
---|
380 | val = (ptrace(T_GETDATA, curpid, bp, 0L)
|
---|
381 | >> SHIFT(INTSIZE)) & MASK(INTSIZE);
|
---|
382 | Printf("0x%0*lx", 2 * INTSIZE, val);
|
---|
383 | bp += INTSIZE;
|
---|
384 | if (bp >= off) break;
|
---|
385 | Printf(",");
|
---|
386 | }
|
---|
387 |
|
---|
388 | skiplp:
|
---|
389 | Printf(")\n");
|
---|
390 | bp = (long) ( (reg_t) ptrace(T_GETDATA, curpid, obp, 0L) );
|
---|
391 | #ifdef DEBUG
|
---|
392 | if(debug)
|
---|
393 | Printf("Old BP %lx New %lx\n",obp,bp);
|
---|
394 | #endif
|
---|
395 | }
|
---|
396 | while (all && (reg_t) bp);
|
---|
397 | }
|
---|
398 |
|
---|
399 | PRIVATE void modify(addr, cnt, verbose, size)
|
---|
400 | long addr;
|
---|
401 | int cnt, verbose, size;
|
---|
402 | {
|
---|
403 | long curval, off;
|
---|
404 |
|
---|
405 | if (curpid == 0) {
|
---|
406 | Printf("No active process.\n");
|
---|
407 | return;
|
---|
408 | }
|
---|
409 | curval = ptrace(T_GETDATA, curpid, addr, 0L) & MASK(size);
|
---|
410 | do {
|
---|
411 | if (cursig == SIGTRAP) cursig = 0;
|
---|
412 | if (verbose) {
|
---|
413 | off = get_reg(curpid, PC_OFF);
|
---|
414 | dasm(off, 1, 0);
|
---|
415 | }
|
---|
416 | if (curpnt && exebpnt(cursig))
|
---|
417 | return;
|
---|
418 | else {
|
---|
419 | ptrace(T_STEP, curpid, addr, 0L);
|
---|
420 | switch (dowait()) {
|
---|
421 | case 0:
|
---|
422 | return;
|
---|
423 | case SIGEMT:
|
---|
424 | update();
|
---|
425 | findbpnt(0);
|
---|
426 | break;
|
---|
427 | }
|
---|
428 | }
|
---|
429 | if (curval != ptrace(T_GETDATA, curpid, addr, 0L) & MASK(size)) {
|
---|
430 | Printf("Modification detected\n");
|
---|
431 | break;
|
---|
432 | }
|
---|
433 | }
|
---|
434 | while (--cnt);
|
---|
435 | update();
|
---|
436 | dasm((long) PC_MEMBER(prc), 1, 1);
|
---|
437 | return;
|
---|
438 | }
|
---|
439 |
|
---|
440 | PRIVATE void display(addr, req)
|
---|
441 | long addr;
|
---|
442 | int req;
|
---|
443 | {
|
---|
444 | int count, size, out, shift;
|
---|
445 | long val, msk;
|
---|
446 | char fmt;
|
---|
447 |
|
---|
448 | if (curpid == 0) {
|
---|
449 | Printf("No active process\n");
|
---|
450 | return;
|
---|
451 | }
|
---|
452 | if (req == T_GETDATA && seg == T) req = T_GETINS;
|
---|
453 | count = strtol(cmd, &cmd, 0);
|
---|
454 | if (count == 0) count = 1;
|
---|
455 | cmd = skip(cmd);
|
---|
456 | if (*cmd == 'i' || *cmd == 'I') {
|
---|
457 | dasm(addr, count, *cmd == 'i');
|
---|
458 | return;
|
---|
459 | }
|
---|
460 | if (*cmd == 'y') {
|
---|
461 | symbolic(addr, '\n');
|
---|
462 | return;
|
---|
463 | }
|
---|
464 | switch (*cmd++) {
|
---|
465 | case 'b': size = sizeof(char); break;
|
---|
466 | case 'h': size = sizeof(short); break;
|
---|
467 | case 'l': size = sizeof(long); break;
|
---|
468 | default:
|
---|
469 | size = sizeof(int);
|
---|
470 | --cmd;
|
---|
471 | break;
|
---|
472 | }
|
---|
473 | switch (fmt = *cmd) {
|
---|
474 | case 'X':
|
---|
475 | case 'D':
|
---|
476 | size = sizeof(long);
|
---|
477 | break;
|
---|
478 | case 's':
|
---|
479 | addr = ptrace(req, curpid, addr, 0L);
|
---|
480 | req = T_GETDATA;
|
---|
481 | /* Fallthrough */
|
---|
482 | case 'a':
|
---|
483 | case 'c':
|
---|
484 | size = sizeof(char);
|
---|
485 | break;
|
---|
486 | }
|
---|
487 | out = 0;
|
---|
488 | msk = MASK(size);
|
---|
489 | shift = SHIFT(size);
|
---|
490 | do {
|
---|
491 | val = (ptrace(req, curpid, addr, 0L) >> shift) & msk;
|
---|
492 | if (out == 0) Printf("\n0x%0*lx: ", 2 * ADDRSIZE,
|
---|
493 | (addr >> SHIFT(ADDRSIZE)) & MASK(ADDRSIZE));
|
---|
494 | switch (fmt) {
|
---|
495 | case 'c':
|
---|
496 | Printf(isprint((int) (UCHAR(val))) ? " %c " : "\\%03o ",
|
---|
497 | (int) (UCHAR(val)));
|
---|
498 | if (++out == 8) out = 0;
|
---|
499 | break;
|
---|
500 | case 'u':
|
---|
501 | Printf("%12lu ", val);
|
---|
502 | if (++out == 4) out = 0;
|
---|
503 | break;
|
---|
504 | case 'x':
|
---|
505 | case 'X':
|
---|
506 | Printf("%*lx ", 2 * size, val);
|
---|
507 | if (++out == (size == 4 ? 4 : 8)) out = 0;
|
---|
508 | break;
|
---|
509 | case 'o':
|
---|
510 | Printf("%*lo ", 3 * size, val);
|
---|
511 | if (++out == (size == 4 ? 4 : 8)) out = 0;
|
---|
512 | break;
|
---|
513 | case 's':
|
---|
514 | case 'a':
|
---|
515 | if (val)
|
---|
516 | Printf("%c",val);
|
---|
517 | else
|
---|
518 | goto exitlp;
|
---|
519 | if (++out == 64) out = 0;
|
---|
520 | break;
|
---|
521 | default:
|
---|
522 | case 'd':
|
---|
523 | case 'D':
|
---|
524 | Printf("%12ld ", val);
|
---|
525 | if (++out == 4) out = 0;
|
---|
526 | break;
|
---|
527 | }
|
---|
528 | addr += size;
|
---|
529 | }
|
---|
530 | while (--count > 0 || fmt == 's' || fmt == 'a');
|
---|
531 | exitlp:
|
---|
532 | Printf("\n");
|
---|
533 | }
|
---|
534 |
|
---|
535 | PRIVATE void fill(addr, req)
|
---|
536 | long addr;
|
---|
537 | int req;
|
---|
538 | {
|
---|
539 | int count, size, shift;
|
---|
540 | long val, msk, nval;
|
---|
541 |
|
---|
542 | if (curpid == 0) {
|
---|
543 | Printf("No active process\n");
|
---|
544 | return;
|
---|
545 | }
|
---|
546 |
|
---|
547 | if (req == T_GETDATA && seg == T) {
|
---|
548 | req = T_GETINS;
|
---|
549 | Printf("mdb: warning - modifying text\n");
|
---|
550 | }
|
---|
551 | count = strtol(cmd, &cmd, 0);
|
---|
552 | if ( count == 0 ) count = 1;
|
---|
553 | switch (*cmd++) {
|
---|
554 | case 'b': size = sizeof(char); break;
|
---|
555 | case 'h': size = sizeof(short); break;
|
---|
556 | case 'l': size = sizeof(long); break;
|
---|
557 | default:
|
---|
558 | size = sizeof(int);
|
---|
559 | --cmd;
|
---|
560 | break;
|
---|
561 | }
|
---|
562 | shift = SHIFT(size);
|
---|
563 | msk = MASK(size);
|
---|
564 | cmd = getexp(cmd, &nval, &seg);
|
---|
565 |
|
---|
566 | #ifdef DEBUG
|
---|
567 | if (debug)
|
---|
568 | Printf("Filling for Count=%d Size=%d val=%lx\n",count,size,nval);
|
---|
569 | #endif
|
---|
570 |
|
---|
571 | nval <<= shift;
|
---|
572 | do {
|
---|
573 | val = ptrace(req, curpid, addr, 0L) | (nval & msk);
|
---|
574 | val &= (nval | ~msk);
|
---|
575 | ptrace(req + 3, curpid, addr, val);
|
---|
576 | addr += size;
|
---|
577 | }
|
---|
578 | while (--count > 0);
|
---|
579 | }
|
---|
580 |
|
---|
581 | PRIVATE void not_for_core()
|
---|
582 | {
|
---|
583 | if (corepid > 0)
|
---|
584 | mdb_error("Illegal command for 'core' file\n");
|
---|
585 | }
|
---|
586 |
|
---|
587 | PRIVATE void command()
|
---|
588 | {
|
---|
589 | char c, *p;
|
---|
590 | int i;
|
---|
591 | int size;
|
---|
592 | int stat;
|
---|
593 | long exp, lj, lk;
|
---|
594 | struct b_pnt *bp;
|
---|
595 |
|
---|
596 | seg = NOSEG; /* don't restrict segment expressions are in */
|
---|
597 | cmdstart = cmd = skip(cmd);
|
---|
598 | cmd = getexp(cmd, &exp, &seg);
|
---|
599 |
|
---|
600 | if (cmd == cmdstart) {
|
---|
601 | /* Not an expression */
|
---|
602 | if (corepid < 0) { /* default to pc for running processs */
|
---|
603 | seg = T;
|
---|
604 | exp = PC_MEMBER(prc);
|
---|
605 | } else {
|
---|
606 | seg = lastseg;
|
---|
607 | exp = lastexp;
|
---|
608 | }
|
---|
609 |
|
---|
610 | /* Is it a help command */
|
---|
611 | cmd = skip(cmd+1);
|
---|
612 | if (*cmd == '?') {
|
---|
613 | help_on(*cmdstart);
|
---|
614 | *cmd = '\n';
|
---|
615 | return;
|
---|
616 | }
|
---|
617 | else
|
---|
618 | cmd = cmdstart;
|
---|
619 | }
|
---|
620 |
|
---|
621 | if (seg == NOSEG) seg = T; /* Absolute becomes Text */
|
---|
622 | lastexp = exp; /* save last expression */
|
---|
623 | lastseg = seg;
|
---|
624 | #ifdef DEBUG
|
---|
625 | if(debug)
|
---|
626 | Printf("Current address 0x%0*lx and segment %d\n", 2 * ADDRSIZE, exp, seg);
|
---|
627 |
|
---|
628 | #endif
|
---|
629 |
|
---|
630 | /* Check commands */
|
---|
631 | switch (c = *cmd++) {
|
---|
632 | case 'r': /* illegal for 'core' files */
|
---|
633 | case 'R':
|
---|
634 | case 'k':
|
---|
635 | case 'B':
|
---|
636 | case 'd':
|
---|
637 | case 'D': not_for_core();
|
---|
638 | break;
|
---|
639 |
|
---|
640 | case 'b': /* illegal for 'core' files */
|
---|
641 | case 'c': /* Otherwise run process first */
|
---|
642 | case 'C':
|
---|
643 | case 'm':
|
---|
644 | case 'M':
|
---|
645 | #if SYSCALLS_SUPPORT
|
---|
646 | case 'z':
|
---|
647 | #endif
|
---|
648 | case 'i':
|
---|
649 | case 'I': not_for_core();
|
---|
650 | if (curpid <= 0) dorun("\n");
|
---|
651 | break;
|
---|
652 |
|
---|
653 | case 's': if (curpid <= 0) dorun("\n");
|
---|
654 | break;
|
---|
655 |
|
---|
656 | default: break;
|
---|
657 | }
|
---|
658 |
|
---|
659 | switch (c) {
|
---|
660 | case '!': /* escape to shell */
|
---|
661 | if (cmd == cmdstart + 1) {
|
---|
662 | cmd = skip(cmd);
|
---|
663 | if (*cmd == '\n' || *cmd == ';') {
|
---|
664 | i = run("/bin/sh", "\n", 0);
|
---|
665 | } else {
|
---|
666 | for (p = cmd + 1; *p && !isspace(*p); p++) {
|
---|
667 | };
|
---|
668 | *p++ = 0;
|
---|
669 | i = run(cmd, *p ? p : "\n", 0);
|
---|
670 | }
|
---|
671 | if (i > 0) while (wait(&stat) != i) {};
|
---|
672 | break;
|
---|
673 | }
|
---|
674 | if (corepid > 0) longjmp(mainlp, 0);
|
---|
675 | break;
|
---|
676 | case 'T': /* top line of backtrace */
|
---|
677 | backtrace(0);
|
---|
678 | break;
|
---|
679 | case 't': /* back trace */
|
---|
680 | backtrace(1);
|
---|
681 | break;
|
---|
682 | case '/': /* print variable value */
|
---|
683 | display(exp, T_GETDATA);
|
---|
684 | break;
|
---|
685 | case 'x': /* print registers and instruction */
|
---|
686 | if (disp_regs()) break;
|
---|
687 | /* FALLTHROUGH */
|
---|
688 | case 'X': /* print instruction - X n [, n] */
|
---|
689 | lj = strtol(cmd, &cmd, 0);
|
---|
690 | lk = 0;
|
---|
691 | if (*cmd != '\0')
|
---|
692 | lk = strtol(++cmd, &cmd, 0);
|
---|
693 | if (curpid > 0)
|
---|
694 | dasm(exp + lk, lj ? lj : 1, 1);
|
---|
695 | else
|
---|
696 | Printf("No active process.\n");
|
---|
697 | break;
|
---|
698 | case 'R': /* run program with no args */
|
---|
699 | case 'r': /* run program with args (possibly defaulted) */
|
---|
700 | tstart(T_EXIT, 0, 0, 0);
|
---|
701 | if (c == 'r') {
|
---|
702 | cmd = skip(cmd);
|
---|
703 | if (*cmd == '\n' || *cmd == ';')
|
---|
704 | cmd = sbuf;
|
---|
705 | else
|
---|
706 | strcpy(sbuf, cmd);
|
---|
707 | } else {
|
---|
708 | cmd = "\n";
|
---|
709 | }
|
---|
710 | dorun(cmd);
|
---|
711 | break;
|
---|
712 | case 'c': /* continue program - ignore signal */
|
---|
713 | cursig = 0;
|
---|
714 | case 'C': /* continue program - handle signal */
|
---|
715 | i = 0;
|
---|
716 | if (seg == T && curpnt == 0 && cmd != cmdstart + 1) {
|
---|
717 | breakpt(exp, "\n");
|
---|
718 | curpnt = b_head;
|
---|
719 | ptrace(T_SETINS, curpid, curpnt->addr, curpnt->oldval);
|
---|
720 | i = 1;
|
---|
721 | }
|
---|
722 | tstart(T_RESUME, 1, cursig, (int) strtol(cmd, &cmd, 0));
|
---|
723 | /* remove temporary bp */
|
---|
724 | if (i) freepnt(b_head);
|
---|
725 | if (cursig == SIGEMT) return;
|
---|
726 | if (curpid) Printf("Process stopped by signal %d\n", cursig);
|
---|
727 | break;
|
---|
728 | case 'i': /* single step - ignore signal */
|
---|
729 | tstart(T_STEP, 1, 0, (int) strtol(cmd, &cmd, 0));
|
---|
730 | break;
|
---|
731 | case 'I': /* single step - handle signal */
|
---|
732 | tstart(T_STEP, 1, cursig, (int) strtol(cmd, &cmd, 0));
|
---|
733 | break;
|
---|
734 | case 'm': /* single step until location modified */
|
---|
735 | case 'M': /* single step until location modified - verbose */
|
---|
736 | cmd = skip(cmd);
|
---|
737 | switch (*cmd++) {
|
---|
738 | case 'b': size = sizeof(char); break;
|
---|
739 | case 'h': size = sizeof(short); break;
|
---|
740 | case 'l': size = sizeof(long); break;
|
---|
741 | default:
|
---|
742 | size = sizeof(int);
|
---|
743 | --cmd;
|
---|
744 | break;
|
---|
745 | }
|
---|
746 | modify(exp, (int) strtol(cmd, &cmd, 0), c == 'M', size);
|
---|
747 | break;
|
---|
748 | case 'k': /* kill current program */
|
---|
749 | tstart(T_EXIT, 1, 0, 0);
|
---|
750 | break;
|
---|
751 | case 'b': /* set a breakpoint at the given line */
|
---|
752 | #ifdef MINIX_PC
|
---|
753 | if (seg != T || exp > end_addr ) {
|
---|
754 | #else
|
---|
755 | if (seg != T || exp < st_addr || exp > et_addr ) {
|
---|
756 | #endif
|
---|
757 | Printf("Address not in text space.\n");
|
---|
758 | return;
|
---|
759 | }
|
---|
760 | breakpt(exp, skip(cmd));
|
---|
761 | cmd = "\n";
|
---|
762 | return;
|
---|
763 | case 'B': /* print list of currently active breakpoints */
|
---|
764 | for (i = 1, bp = b_head; bp; bp = bp->nxt, i++) {
|
---|
765 | Printf("%2d: ", i);
|
---|
766 | symbolic((long) bp->addr, '\t');
|
---|
767 | Printf("(0x%lx)\t- %s", bp->addr, bp->cmd);
|
---|
768 | }
|
---|
769 | break;
|
---|
770 | case 'd': /* delete breakpoint */
|
---|
771 | if (seg == T) {
|
---|
772 | for (bp = b_head; bp && bp->addr != exp; bp = bp->nxt);
|
---|
773 | if (bp) {
|
---|
774 | freepnt(bp);
|
---|
775 | break;
|
---|
776 | }
|
---|
777 | }
|
---|
778 | Printf("No such breakpoint.\n");
|
---|
779 | break;
|
---|
780 | case 'D': /* delete all breakpoints */
|
---|
781 | while (b_head) freepnt(b_head);
|
---|
782 | break;
|
---|
783 | case 's':
|
---|
784 | dump_stack( strtol(cmd, &cmd, 0) );
|
---|
785 | break;
|
---|
786 | case 'P':
|
---|
787 | paging = !paging;
|
---|
788 | if (paging) Printf("Paging is ON\n");
|
---|
789 | break;
|
---|
790 | case 'l':
|
---|
791 | case 'L':
|
---|
792 | logging(c,skip(cmd));
|
---|
793 | break;
|
---|
794 | #if SYSCALLS_SUPPORT
|
---|
795 | case 'z':
|
---|
796 | start_syscall( strtol(cmd, &cmd, 0) );
|
---|
797 | if ( syscalls )
|
---|
798 | Printf("Break point set - use the 'c n' command\n");
|
---|
799 | break;
|
---|
800 | #endif
|
---|
801 | case 'q': /* quit */
|
---|
802 | tstart(T_EXIT, 0, 0, 0);
|
---|
803 | logging(c,cmd);
|
---|
804 | case 'Q':
|
---|
805 | exit(0);
|
---|
806 | break;
|
---|
807 | case '\n':
|
---|
808 | case ';':
|
---|
809 | if (isdigit(*cmdstart))
|
---|
810 | symbolic(exp, '\n');
|
---|
811 | else
|
---|
812 | Printf("0x%0*lx\n", 2 * ADDRSIZE, exp);
|
---|
813 | --cmd;
|
---|
814 | break;
|
---|
815 | #ifdef DEBUG
|
---|
816 | case 'v': /* toggle debug */
|
---|
817 | debug = !debug;
|
---|
818 | if (debug) Printf("Debug flag ON\n");
|
---|
819 | break;
|
---|
820 | #endif
|
---|
821 | case 'e': /* list symbols */
|
---|
822 | listsym(cmd);
|
---|
823 | break;
|
---|
824 | case 'y': /* print mapping */
|
---|
825 | prtmap();
|
---|
826 | break;
|
---|
827 | case '?': /* print help */
|
---|
828 | help_page();
|
---|
829 | break;
|
---|
830 | case 'V': /* print version info */
|
---|
831 | version_info();
|
---|
832 | break;
|
---|
833 | case '@': /* command file */
|
---|
834 | cmd = skip(cmd);
|
---|
835 | openin(cmd);
|
---|
836 | *cmd = '\n';
|
---|
837 | return;
|
---|
838 | case '#': /* set register or variable */
|
---|
839 | cmd = skip(cmd + 1);
|
---|
840 | if (*cmd == '$') {
|
---|
841 | cmd++;
|
---|
842 | i = reg_addr(cmd);
|
---|
843 | set_reg(curpid, i, strtol(cmd+2, &cmd, 0) );
|
---|
844 | update();
|
---|
845 | break;
|
---|
846 | }
|
---|
847 | cmd = getexp(cmd, &exp, &seg);
|
---|
848 | fill(exp, T_GETDATA);
|
---|
849 | break;
|
---|
850 | default:
|
---|
851 | help_page();
|
---|
852 | break;
|
---|
853 | }
|
---|
854 | while (*cmd != '\n' && *cmd != ';') ++cmd;
|
---|
855 | if (*cmd == ';') cmd = skip(cmd + 1);
|
---|
856 | }
|
---|
857 |
|
---|
858 | PUBLIC void mdb_error(s)
|
---|
859 | char *s;
|
---|
860 | {
|
---|
861 | Printf("%s",s);
|
---|
862 | longjmp(mainlp, 0);
|
---|
863 | }
|
---|
864 |
|
---|
865 | PUBLIC void main(argc, argv)
|
---|
866 | int argc;
|
---|
867 | char *argv[];
|
---|
868 | {
|
---|
869 | int i, c;
|
---|
870 | char *p, *q, *r;
|
---|
871 | int opt_c = FALSE; /* load core file */
|
---|
872 | int opt_f = FALSE; /* load object file */
|
---|
873 | int opt_l = FALSE; /* log to file */
|
---|
874 | int opt_L = FALSE; /* log to file and screen */
|
---|
875 |
|
---|
876 |
|
---|
877 | prc = (struct proc *) lbuf;
|
---|
878 | strcpy(sbuf, "\n");
|
---|
879 | corepid = -1; /* set to indicate none */
|
---|
880 | prog = p = q = r = NULL;
|
---|
881 |
|
---|
882 | if ( argc == 1 )
|
---|
883 | {
|
---|
884 | help_page();
|
---|
885 | exit(0);
|
---|
886 | }
|
---|
887 |
|
---|
888 | /* Possible combinations of arguments:
|
---|
889 | * A single file name:
|
---|
890 | * If the name is 'core', the coreonly flag is set.
|
---|
891 | * The -c flag: examine a core file.
|
---|
892 | * One filename is required with this flag.
|
---|
893 | * The -f flag: examine an object file.
|
---|
894 | * One file name is required with this flag.
|
---|
895 | * The -L or -l flag: write to a log file.
|
---|
896 | * One file name is required with these flags.
|
---|
897 | * The -x flag: turn on debugging.
|
---|
898 | * Used for debugging, and followed by an integer
|
---|
899 | * argument which is the debugging level.
|
---|
900 | *
|
---|
901 | * If any files remain on the argument list, the first
|
---|
902 | * file is an executable, and the second a core file.
|
---|
903 | * If any filename starts with '@' it is assumed to
|
---|
904 | * to be a command file. Only one command file is
|
---|
905 | * loaded.
|
---|
906 | */
|
---|
907 |
|
---|
908 | /* check for default file name and fake out getopt */
|
---|
909 | if (strcmp(argv[1], "core") == 0) {
|
---|
910 | for (i = argc ; i > 1 ; i--)
|
---|
911 | argv[i] = argv[i - 1];
|
---|
912 | argv[i] = "-c";
|
---|
913 | argc++;
|
---|
914 | }
|
---|
915 |
|
---|
916 | /* parse options */
|
---|
917 | opterr = 0;
|
---|
918 | while ((i = getopt(argc, argv, "c:f:L:l:x:")) != EOF) {
|
---|
919 | switch (i & 0377) {
|
---|
920 | case 'c': /* examine a core file */
|
---|
921 | if (opt_c == TRUE || opt_f == TRUE) {
|
---|
922 | help_page();
|
---|
923 | exit(1);
|
---|
924 | }
|
---|
925 | p = optarg;
|
---|
926 | opt_c = TRUE;
|
---|
927 | break;
|
---|
928 | case 'f': /* examine an object file */
|
---|
929 | if (opt_c == TRUE || opt_f == TRUE) {
|
---|
930 | help_page();
|
---|
931 | exit(1);
|
---|
932 | }
|
---|
933 | p = optarg;
|
---|
934 | opt_f = TRUE;
|
---|
935 | break;
|
---|
936 | case 'l': /* start logging */
|
---|
937 | if (opt_l == TRUE || opt_L == TRUE) {
|
---|
938 | help_page();
|
---|
939 | exit(1);
|
---|
940 | }
|
---|
941 | opt_l = TRUE;
|
---|
942 | logging(i, optarg);
|
---|
943 | break;
|
---|
944 | case 'L': /* start logging */
|
---|
945 | if (opt_l == TRUE || opt_L == TRUE) {
|
---|
946 | help_page();
|
---|
947 | exit(1);
|
---|
948 | }
|
---|
949 | opt_L = TRUE;
|
---|
950 | logging(i, optarg);
|
---|
951 | break;
|
---|
952 | #ifdef DEBUG
|
---|
953 | case 'x': /* set debug level */
|
---|
954 | debug = atoi(optarg);
|
---|
955 | break;
|
---|
956 | #endif
|
---|
957 | case '?': /* default arguments arrive here */
|
---|
958 | default:
|
---|
959 | help_page();
|
---|
960 | exit(1);
|
---|
961 | }
|
---|
962 | }
|
---|
963 |
|
---|
964 | /* can't cope without filenames */
|
---|
965 | if (!opt_c && !opt_f && optind >= argc) {
|
---|
966 | help_page();
|
---|
967 | exit(1);
|
---|
968 | }
|
---|
969 |
|
---|
970 | /* any remaining arguments are (optional) file names */
|
---|
971 | for (i = optind ; i < argc ; i++) {
|
---|
972 | if (*argv[i] == '@') { /* command file */
|
---|
973 | if (r == NULL) r = argv[i] + 1;
|
---|
974 | }
|
---|
975 | /* you can't combine a -c or -f object file and a core file */
|
---|
976 | else if (!opt_c && !opt_f && p == NULL) p = argv[i];
|
---|
977 | else if (q == NULL) q = argv[i]; /* core file */
|
---|
978 | }
|
---|
979 |
|
---|
980 | /* initialise stuff - fairly tricky logic */
|
---|
981 | coreonly = opt_c;
|
---|
982 | fileonly = opt_f;
|
---|
983 | /* when examining files, prog == NULL */
|
---|
984 | if (!opt_c && !opt_f) {
|
---|
985 | prog = p;
|
---|
986 | syminit(prog);
|
---|
987 | }
|
---|
988 |
|
---|
989 | /* file_init is called for non-core files.
|
---|
990 | * It is very similar to core_init. It opens the file and set
|
---|
991 | * various pointers so that we can read it using the same routines
|
---|
992 | * as a core file.
|
---|
993 | * NB: Currently there is no special provision to handle object files.
|
---|
994 | */
|
---|
995 |
|
---|
996 | /* A comment from Will Rose:
|
---|
997 | * It would be nice to have
|
---|
998 | * symbol tables available when reading a core
|
---|
999 | * or a.out, either as part of the executable or
|
---|
1000 | * as a separate file.
|
---|
1001 | * At least three separate types of file structure
|
---|
1002 | * may be used by mdb - core files, a.out files, and
|
---|
1003 | * object files (which may have several flavours).
|
---|
1004 | * A set of routines is needed for each type, with
|
---|
1005 | * a function switch table initialised when mdb is
|
---|
1006 | * started up.
|
---|
1007 | */
|
---|
1008 |
|
---|
1009 | if (opt_c) lastexp = core_init(p);
|
---|
1010 | if (opt_f) lastexp = file_init(p);
|
---|
1011 | if (q != NULL) lastexp = core_init(q);
|
---|
1012 | if (r != NULL) openin(r);
|
---|
1013 | for (i = 1; i < _NSIG; i++) signal(i, catch);
|
---|
1014 |
|
---|
1015 | setjmp(mainlp);
|
---|
1016 |
|
---|
1017 | while (get_cmd( cbuf, MAXLINE ) != NULL) {
|
---|
1018 | if (strlen(cbuf) == sizeof(cbuf) - 1) {
|
---|
1019 | Printf("Command line too long.\n");
|
---|
1020 | continue;
|
---|
1021 | }
|
---|
1022 | cmd = cbuf;
|
---|
1023 | command();
|
---|
1024 | while (*cmd != '\n') command();
|
---|
1025 | }
|
---|
1026 | tstart(T_EXIT, 0, 0, 0);
|
---|
1027 | exit(0);
|
---|
1028 | }
|
---|