date and time created 88/01/12 00:40:29 by donn
[unix-history] / usr / src / old / dbx / main.c
index c8e21e4..9aa9abb 100644 (file)
@@ -1,6 +1,20 @@
-/* Copyright (c) 1982 Regents of the University of California */
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)main.c     5.3 (Berkeley) %G%";
+#endif not lint
 
 
-static char sccsid[] = "@(#)main.c 1.4 %G%";
+static char rcsid[] = "$Header: main.c,v 1.5 84/12/26 10:40:16 linton Exp $";
 
 /*
  * Debugger main routine.
 
 /*
  * Debugger main routine.
@@ -11,28 +25,45 @@ static char sccsid[] = "@(#)main.c 1.4 %G%";
 #include <signal.h>
 #include <errno.h>
 #include "main.h"
 #include <signal.h>
 #include <errno.h>
 #include "main.h"
+#include "eval.h"
+#include "debug.h"
+#include "symbols.h"
 #include "scanner.h"
 #include "scanner.h"
+#include "keywords.h"
 #include "process.h"
 #include "process.h"
+#include "runtime.h"
 #include "source.h"
 #include "object.h"
 #include "source.h"
 #include "object.h"
+#include "mappings.h"
+#include "coredump.h"
 
 #ifndef public
 
 #define isterm(file)   (interactive or isatty(fileno(file)))
 
 #include <sgtty.h>
 
 #ifndef public
 
 #define isterm(file)   (interactive or isatty(fileno(file)))
 
 #include <sgtty.h>
+#include <fcntl.h>
 
 
-typedef struct sgttyb Ttyinfo;
+typedef struct {
+    struct sgttyb sg;          /* standard sgttyb structure */
+    struct tchars tc;          /* terminal characters */
+    struct ltchars ltc;                /* local special characters */
+    integer ldisc;             /* line discipline */
+    integer local;             /* TIOCLGET */
+    integer fcflags;           /* fcntl(2) F_GETFL, F_SETFL */
+} Ttyinfo;
 
 #endif
 
 
 #endif
 
-public Boolean coredump;               /* true if using a core dump */
-public Boolean runfirst;               /* run program immediately */
-public Boolean interactive;            /* standard input IS a terminal */
-public Boolean lexdebug;               /* trace yylex return values */
-public Boolean tracebpts;              /* trace create/delete breakpoints */
-public Boolean traceexec;              /* trace process execution */
-public Boolean tracesyms;              /* print symbols as their read */
+public boolean coredump;               /* true if using a core dump */
+public boolean runfirst;               /* run program immediately */
+public boolean interactive;            /* standard input IS a terminal */
+public boolean lexdebug;               /* trace scanner return values */
+public boolean tracebpts;              /* trace create/delete breakpoints */
+public boolean traceexec;              /* trace execution */
+public boolean tracesyms;              /* print symbols are they are read */
+public boolean traceblocks;            /* trace blocks while reading symbols */
+public boolean vaddrs;                 /* map addresses through page tables */
 
 public File corefile;                  /* File id of core dump */
 
 
 public File corefile;                  /* File id of core dump */
 
@@ -42,9 +73,9 @@ private Boolean initdone = false;     /* true if initialization done */
 private jmp_buf env;                   /* setjmp/longjmp data */
 private char outbuf[BUFSIZ];           /* standard output buffer */
 private char namebuf[512];             /* possible name of object file */
 private jmp_buf env;                   /* setjmp/longjmp data */
 private char outbuf[BUFSIZ];           /* standard output buffer */
 private char namebuf[512];             /* possible name of object file */
-private int firstarg;                  /* first program argument (for -r) */
 
 private Ttyinfo ttyinfo;
 
 private Ttyinfo ttyinfo;
+private String corename;               /* name of core file */
 
 private catchintr();
 
 
 private catchintr();
 
@@ -56,24 +87,32 @@ main(argc, argv)
 int argc;
 String argv[];
 {
 int argc;
 String argv[];
 {
-    register Integer i;
+    register integer i;
     extern String date;
     extern String date;
+    extern integer versionNumber;
+    char **scanargs();
+
+    if (!(cmdname = rindex(*argv, '/')))
+       cmdname = *argv;
+    else
+       ++cmdname;
 
 
-    cmdname = argv[0];
     catcherrs();
     onsyserr(EINTR, nil);
     setbuf(stdout, outbuf);
     catcherrs();
     onsyserr(EINTR, nil);
     setbuf(stdout, outbuf);
-    printf("dbx version of %s.\nType 'help' for help.\n", date);
+    printf("dbx version 3.%d of %s.\nType 'help' for help.\n",
+       versionNumber, date);
     fflush(stdout);
     fflush(stdout);
-    scanargs(argc, argv);
+    argv = scanargs(argc, argv);
     language_init();
     language_init();
+    symbols_init();
     process_init();
     process_init();
+    optab_init();
     if (runfirst) {
        if (setjmp(env) == FIRST_TIME) {
            arginit();
     if (runfirst) {
        if (setjmp(env) == FIRST_TIME) {
            arginit();
-           for (i = firstarg; i < argc; i++) {
-               newarg(argv[i]);
-           }
+           while (*argv)
+               newarg(*argv++);
            run();
            /* NOTREACHED */
        } else {
            run();
            /* NOTREACHED */
        } else {
@@ -82,8 +121,9 @@ String argv[];
     } else {
        init();
     }
     } else {
        init();
     }
-    setjmp(env);
-    restoretty(stdout, &ttyinfo);
+    if (setjmp(env) != FIRST_TIME) {
+       restoretty(stdout, &ttyinfo);
+    }
     signal(SIGINT, catchintr);
     yyparse();
     putchar('\n');
     signal(SIGINT, catchintr);
     yyparse();
     putchar('\n');
@@ -113,7 +153,15 @@ public init()
     readobj(objname);
     printf("\n");
     fflush(stdout);
     readobj(objname);
     printf("\n");
     fflush(stdout);
-    curfunc = program;
+    if (coredump) {
+       printf("[using memory image in %s]\n", corename);
+       if (vaddrs) {
+           coredump_getkerinfo();
+       }
+       setcurfunc(whatblock(pc));
+    } else {
+       setcurfunc(program);
+    }
     bpinit();
     f = fopen(initfile, "r");
     if (f != nil) {
     bpinit();
     f = fopen(initfile, "r");
     if (f != nil) {
@@ -163,7 +211,7 @@ String outfile;
     tmpfile = mktemp("/tmp/dbxXXXX");
     setout(tmpfile);
     status();
     tmpfile = mktemp("/tmp/dbxXXXX");
     setout(tmpfile);
     status();
-    print_alias(nil);
+    alias(nil, nil, nil);
     if (argv != nil) {
        printf("run");
        for (i = 1; argv[i] != nil; i++) {
     if (argv != nil) {
        printf("run");
        for (i = 1; argv[i] != nil; i++) {
@@ -180,6 +228,7 @@ String outfile;
     unsetout();
     bpfree();
     objfree();
     unsetout();
     bpfree();
     objfree();
+    symbols_init();
     process_init();
     enterkeywords();
     scanner_init();
     process_init();
     enterkeywords();
     scanner_init();
@@ -195,7 +244,8 @@ String outfile;
 }
 
 /*
 }
 
 /*
- * After a non-fatal error we jump back to command parsing.
+ * After a non-fatal error we skip the rest of the current input line, and
+ * jump back to command parsing.
  */
 
 public erecover()
  */
 
 public erecover()
@@ -212,6 +262,10 @@ public erecover()
 
 private catchintr()
 {
 
 private catchintr()
 {
+    if (isredirected()) {
+       fflush(stdout);
+       unsetout();
+    }
     putchar('\n');
     longjmp(env, 1);
 }
     putchar('\n');
     longjmp(env, 1);
 }
@@ -220,13 +274,16 @@ private catchintr()
  * Scan the argument list.
  */
 
  * Scan the argument list.
  */
 
-private scanargs(argc, argv)
+private char **scanargs(argc, argv)
 int argc;
 String argv[];
 {
 int argc;
 String argv[];
 {
+    extern char *optarg;
+    extern int optind;
     register int i, j;
     register Boolean foundfile;
     register File f;
     register int i, j;
     register Boolean foundfile;
     register File f;
+    int ch;
     char *tmp;
 
     runfirst = false;
     char *tmp;
 
     runfirst = false;
@@ -235,40 +292,69 @@ String argv[];
     tracebpts = false;
     traceexec = false;
     tracesyms = false;
     tracebpts = false;
     traceexec = false;
     tracesyms = false;
+    traceblocks = false;
+    vaddrs = false;
     foundfile = false;
     corefile = nil;
     coredump = true;
     sourcepath = list_alloc();
     list_append(list_item("."), nil, sourcepath);
     foundfile = false;
     corefile = nil;
     coredump = true;
     sourcepath = list_alloc();
     list_append(list_item("."), nil, sourcepath);
-    i = 1;
-    while (i < argc and (not foundfile or corefile == nil)) {
-       if (argv[i][0] == '-') {
-           if (streq(argv[i], "-I")) {
-               ++i;
-               if (i >= argc) {
-                   fatal("missing directory for -I");
-               }
-               list_append(list_item(argv[i]), nil, sourcepath);
-           } else {
-               for (j = 1; argv[i][j] != '\0'; j++) {
-                   setoption(argv[i][j]);
-               }
-           }
-       } else if (not foundfile) {
-           objname = argv[i];
-           foundfile = true;
-       } else if (coredump and corefile == nil) {
-           corefile = fopen(argv[i], "r");
-           if (corefile == nil) {
+
+    while ((ch = getopt(argc, argv, "I:bc:eiklnrs")) != EOF)
+    switch((char)ch) {
+       case 'I':
+               list_append(list_item(optarg), nil, sourcepath);
+               break;
+       case 'b':
+               tracebpts = true;
+               break;
+       case 'c':
+               initfile = optarg;
+               break;
+       case 'e':
+               traceexec = true;
+               break;
+       case 'i':
+               interactive = true;
+               break;
+       case 'k':
+               vaddrs = true;
+               break;
+       case 'l':
+#ifdef LEXDEBUG
+               lexdebug = true;
+#else
+               fatal("\"-l\" only applicable when compiled with LEXDEBUG");
+#endif
+               break;
+       case 'n':
+               traceblocks = true;
+               break;
+       case 'r':       /* run program before accepting commands */
+               runfirst = true;
                coredump = false;
                coredump = false;
-           }
-       }
-       ++i;
+               break;
+       case 's':
+               tracesyms = true;
+               break;
+       case '?':
+       default:
+               fatal("unknown option");
     }
     }
-    if (i < argc and not runfirst) {
-       fatal("extraneous argument %s", argv[i]);
+    argv += optind;
+    if (*argv) {
+       objname = *argv;
+       foundfile = true;
+       if (*++argv && coredump) {
+               corename = *argv;
+               corefile = fopen(*argv, "r");
+               if (corefile == nil)
+                       coredump = false;
+               ++argv;
+       }
     }
     }
-    firstarg = i;
+    if (*argv and not runfirst)
+       fatal("extraneous argument %s", *argv);
     if (not foundfile and isatty(0)) {
        printf("enter object file name (default is `%s'): ", objname);
        fflush(stdout);
     if (not foundfile and isatty(0)) {
        printf("enter object file name (default is `%s'): ", objname);
        fflush(stdout);
@@ -289,53 +375,21 @@ String argv[];
        list_append(list_item(tmp), nil, sourcepath);
     }
     if (coredump and corefile == nil) {
        list_append(list_item(tmp), nil, sourcepath);
     }
     if (coredump and corefile == nil) {
-       corefile = fopen("core", "r");
-       if (corefile == nil) {
-           coredump = false;
+       if (vaddrs) {
+           corename = "/dev/mem";
+           corefile = fopen(corename, "r");
+           if (corefile == nil) {
+               panic("can't open /dev/mem");
+           }
+       } else {
+           corename = "core";
+           corefile = fopen(corename, "r");
+           if (corefile == nil) {
+               coredump = false;
+           }
        }
     }
        }
     }
-}
-
-/*
- * Take appropriate action for recognized command argument.
- */
-
-private setoption(c)
-char c;
-{
-    switch (c) {
-       case 'r':   /* run program before accepting commands */
-           runfirst = true;
-           coredump = false;
-           break;
-
-       case 'i':
-           interactive = true;
-           break;
-
-       case 'b':
-           tracebpts = true;
-           break;
-
-       case 'e':
-           traceexec = true;
-           break;
-
-       case 's':
-           tracesyms = true;
-           break;
-
-       case 'l':
-#          ifdef LEXDEBUG
-               lexdebug = true;
-#          else
-               fatal("\"-l\" only applicable when compiled with LEXDEBUG");
-#          endif
-           break;
-
-       default:
-           fatal("unknown option '%c'", c);
-    }
+    return(argv);
 }
 
 /*
 }
 
 /*
@@ -346,14 +400,24 @@ public savetty(f, t)
 File f;
 Ttyinfo *t;
 {
 File f;
 Ttyinfo *t;
 {
-    gtty(fileno(f), t);
+    ioctl(fileno(f), TIOCGETP, &(t->sg));
+    ioctl(fileno(f), TIOCGETC, &(t->tc));
+    ioctl(fileno(f), TIOCGLTC, &(t->ltc));
+    ioctl(fileno(f), TIOCGETD, &(t->ldisc));
+    ioctl(fileno(f), TIOCLGET, &(t->local));
+    t->fcflags = fcntl(fileno(f), F_GETFL, 0);
 }
 
 public restoretty(f, t)
 File f;
 Ttyinfo *t;
 {
 }
 
 public restoretty(f, t)
 File f;
 Ttyinfo *t;
 {
-    stty(fileno(f), t);
+    ioctl(fileno(f), TIOCSETN, &(t->sg));
+    ioctl(fileno(f), TIOCSETC, &(t->tc));
+    ioctl(fileno(f), TIOCSLTC, &(t->ltc));
+    ioctl(fileno(f), TIOCSETD, &(t->ldisc));
+    ioctl(fileno(f), TIOCLSET, &(t->local));
+    (void) fcntl(fileno(f), F_SETFL, t->fcflags);
 }
 
 /*
 }
 
 /*
@@ -363,5 +427,6 @@ Ttyinfo *t;
 public quit(r)
 Integer r;
 {
 public quit(r)
 Integer r;
 {
+    pterm(process);
     exit(r);
 }
     exit(r);
 }