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