restore keywords
[unix-history] / usr / src / old / dbx / process.c
index ba5b5b6..0815d95 100644 (file)
@@ -1,6 +1,14 @@
-/* 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
+static char sccsid[] = "@(#)process.c  5.2 (Berkeley) %G%";
+#endif not lint
 
 
-static char sccsid[] = "@(#)process.c  1.15 (Berkeley) %G%";
+static char rcsid[] = "$Header: process.c,v 1.5 84/12/26 10:41:37 linton Exp $";
 
 /*
  * Process management.
 
 /*
  * Process management.
@@ -23,10 +31,6 @@ static       char sccsid[] = "@(#)process.c  1.15 (Berkeley) %G%";
 #include "coredump.h"
 #include <signal.h>
 #include <errno.h>
 #include "coredump.h"
 #include <signal.h>
 #include <errno.h>
-#include <sys/param.h>
-#include <sys/dir.h>
-#include <sys/user.h>
-#include <machine/reg.h>
 #include <sys/stat.h>
 
 #ifndef public
 #include <sys/stat.h>
 
 #ifndef public
@@ -69,7 +73,7 @@ struct Process {
     Word oreg[NREG];           /* registers when process last stopped */
     short status;              /* either STOPPED or FINISHED */
     short signo;               /* signal that stopped process */
     Word oreg[NREG];           /* registers when process last stopped */
     short status;              /* either STOPPED or FINISHED */
     short signo;               /* signal that stopped process */
-    short sigcode;             /* auxiliary signal info code */
+    short sigcode;             /* extra signal information */
     int exitval;               /* return value from exit() */
     long sigset;               /* bit array of traced signals */
     CacheWord word[CSIZE];     /* text segment cache */
     int exitval;               /* return value from exit() */
     long sigset;               /* bit array of traced signals */
     CacheWord word[CSIZE];     /* text segment cache */
@@ -86,7 +90,7 @@ typedef enum { TEXTSEG, DATASEG } PioSeg;
 
 private struct Process pbuf;
 
 
 private struct Process pbuf;
 
-#define MAXNCMDARGS 100         /* maximum number of arguments to RUN */
+#define MAXNCMDARGS 1000         /* maximum number of arguments to RUN */
 
 extern int errno;
 
 
 extern int errno;
 
@@ -111,7 +115,9 @@ public process_init()
        sprintf(buf, "$r%d", i);
        defregname(identname(buf, false), i);
     }
        sprintf(buf, "$r%d", i);
        defregname(identname(buf, false), i);
     }
+#ifdef vax
     defregname(identname("$ap", true), ARGP);
     defregname(identname("$ap", true), ARGP);
+#endif
     defregname(identname("$fp", true), FRP);
     defregname(identname("$sp", true), STKP);
     defregname(identname("$pc", true), PROGCTR);
     defregname(identname("$fp", true), FRP);
     defregname(identname("$sp", true), STKP);
     defregname(identname("$pc", true), PROGCTR);
@@ -175,10 +181,10 @@ String infile, outfile;
     } else {
        argv[argc] = nil;
     }
     } else {
        argv[argc] = nil;
     }
+    pstart(process, argv, infile, outfile);
     if (remade(objname)) {
        reinit(argv, infile, outfile);
     }
     if (remade(objname)) {
        reinit(argv, infile, outfile);
     }
-    pstart(process, argv, infile, outfile);
     if (process->status == STOPPED) {
        pc = 0;
        setcurfunc(program);
     if (process->status == STOPPED) {
        pc = 0;
        setcurfunc(program);
@@ -227,6 +233,7 @@ private setsigtrace()
     psigtrace(p, SIGTSTP, false);
     psigtrace(p, SIGCONT, false);
     psigtrace(p, SIGCHLD, false);
     psigtrace(p, SIGTSTP, false);
     psigtrace(p, SIGCONT, false);
     psigtrace(p, SIGCHLD, false);
+    psigtrace(p, SIGWINCH, false);
 }
 
 /*
 }
 
 /*
@@ -308,9 +315,6 @@ typedef int Intfunc();
 private Intfunc *dbintr;
 private intr();
 
 private Intfunc *dbintr;
 private intr();
 
-#define succeeds    == true
-#define fails       == false
-
 public cont(signo)
 integer signo;
 {
 public cont(signo)
 integer signo;
 {
@@ -335,7 +339,7 @@ integer signo;
            resume(s);
            unsetallbps();
            s = DEFSIG;
            resume(s);
            unsetallbps();
            s = DEFSIG;
-           if (bpact() fails) {
+           if (not isbperr() or not bpact()) {
                printstatus();
            }
        }
                printstatus();
            }
        }
@@ -345,12 +349,12 @@ integer signo;
 }
 
 /*
 }
 
 /*
- * This routine is called if we get an interrupt while "running" px
+ * This routine is called if we get an interrupt while "running"
  * but actually in the debugger.  Could happen, for example, while
  * processing breakpoints.
  *
  * We basically just want to keep going; the assumption is
  * but actually in the debugger.  Could happen, for example, while
  * processing breakpoints.
  *
  * We basically just want to keep going; the assumption is
- * that when the process resumes it will get the interrupt
+ * that when the process resumes it will get the interrupt,
  * which will then be handled.
  */
 
  * which will then be handled.
  */
 
@@ -380,7 +384,11 @@ int signo;
        if (p->signo != 0) {
            error("program terminated by signal %d", p->signo);
        } else if (not runfirst) {
        if (p->signo != 0) {
            error("program terminated by signal %d", p->signo);
        } else if (not runfirst) {
-           error("program unexpectedly exited with %d", p->exitval);
+           if (p->exitval == 0) {
+               error("program exited");
+           } else {
+               error("program exited with code %d", p->exitval);
+           }
        }
     }
 }
        }
     }
 }
@@ -456,7 +464,7 @@ Symbol f;
                    contto(addr);
                }
            }
                    contto(addr);
                }
            }
-           if (bpact() fails) {
+           if (not bpact()) {
                isstopped = true;
                printstatus();
            }
                isstopped = true;
                printstatus();
            }
@@ -494,9 +502,8 @@ public stepover()
 }
 
 /*
 }
 
 /*
- * Resume execution up to the given address.  It is assumed that
- * no breakpoints exist between the current address and the one
- * we're stepping to.  This saves us from setting all the breakpoints.
+ * Resume execution up to the given address.  We can either ignore
+ * breakpoints (stepto) or catch them (contto).
  */
 
 public stepto(addr)
  */
 
 public stepto(addr)
@@ -610,16 +617,43 @@ Process p;
 }
 
 /*
 }
 
 /*
- * Return the signal number which stopped the process.
+ * Predicate to test if the reason the process stopped was because
+ * of a breakpoint.  If so, as a side effect clear the local copy of
+ * signal handler associated with process.  We must do this so as to
+ * not confuse future stepping or continuing by possibly concluding
+ * the process should continue with a SIGTRAP handler.
+ */
+
+public boolean isbperr()
+{
+    Process p;
+    boolean b;
+
+    p = process;
+    if (p->status == STOPPED and p->signo == SIGTRAP) {
+       b = true;
+       p->sigstatus = 0;
+    } else {
+       b = false;
+    }
+    return b;
+}
+
+/*
+ * Return the signal number that stopped the process.
  */
 
  */
 
-public Integer errnum(p)
+public integer errnum (p)
 Process p;
 {
     return p->signo;
 }
 
 Process p;
 {
     return p->signo;
 }
 
-public Integer errcode(p)
+/*
+ * Return the signal code associated with the signal.
+ */
+
+public integer errcode (p)
 Process p;
 {
     return p->sigcode;
 Process p;
 {
     return p->sigcode;
@@ -629,7 +663,7 @@ Process p;
  * Return the termination code of the process.
  */
 
  * Return the termination code of the process.
  */
 
-public Integer exitcode(p)
+public integer exitcode (p)
 Process p;
 {
     return p->exitval;
 Process p;
 {
     return p->exitval;
@@ -703,14 +737,16 @@ int nbytes;
 {
     Intfunc *f;
 
 {
     Intfunc *f;
 
-    f = onsyserr(EIO, read_err);
     badaddr = addr;
     if (coredump) {
     badaddr = addr;
     if (coredump) {
+       f = onsyserr(EFAULT, read_err);
        coredump_readdata(buff, addr, nbytes);
        coredump_readdata(buff, addr, nbytes);
+       onsyserr(EFAULT, f);
     } else {
     } else {
+       f = onsyserr(EIO, read_err);
        pio(process, PREAD, DATASEG, buff, addr, nbytes);
        pio(process, PREAD, DATASEG, buff, addr, nbytes);
+       onsyserr(EIO, f);
     }
     }
-    onsyserr(EIO, f);
 }
 
 /*
 }
 
 /*
@@ -807,13 +843,16 @@ String outfile;
 
     if (p->pid != 0) {
        pterm(p);
 
     if (p->pid != 0) {
        pterm(p);
+       cacheflush(p);
     }
     }
+    fflush(stdout);
     psigtrace(p, SIGTRAP, true);
     p->pid = vfork();
     if (p->pid == -1) {
        panic("can't fork");
     }
     if (ischild(p->pid)) {
     psigtrace(p, SIGTRAP, true);
     p->pid = vfork();
     if (p->pid == -1) {
        panic("can't fork");
     }
     if (ischild(p->pid)) {
+       nocatcherrs();
        traceme();
        if (infile != nil) {
            infrom(infile);
        traceme();
        if (infile != nil) {
            infrom(infile);
@@ -822,17 +861,16 @@ String outfile;
            outto(outfile);
        }
        execv(argv[0], argv);
            outto(outfile);
        }
        execv(argv[0], argv);
-       write(2, "can't exec ", 11);
-       write(2, argv[0], strlen(argv[0]));
-       write(2, "\n", 1);
        _exit(1);
     }
     pwait(p->pid, &status);
     getinfo(p, status);
     if (p->status != STOPPED) {
        _exit(1);
     }
     pwait(p->pid, &status);
     getinfo(p, status);
     if (p->status != STOPPED) {
-       error("program could not begin execution");
+       beginerrmsg();
+       fprintf(stderr, "warning: cannot execute %s\n", argv[0]);
+    } else {
+       ptraced(p->pid);
     }
     }
-    ptraced(p->pid);
 }
 
 /*
 }
 
 /*
@@ -868,7 +906,7 @@ int signo;
     int s, status;
 
     if (p->pid == 0) {
     int s, status;
 
     if (p->pid == 0) {
-       error("program not active");
+       error("program is not active");
     }
     s = signo;
     do {
     }
     s = signo;
     do {
@@ -885,8 +923,9 @@ int signo;
        pwait(p->pid, &status);
        sigs_on();
        getinfo(p, status);
        pwait(p->pid, &status);
        sigs_on();
        getinfo(p, status);
-       if (traceexec and not istraced(p)) {
-           printf("!! ignored signal %d at 0x%x\n", p->signo, p->reg[PROGCTR]);
+       if (p->status == STOPPED and traceexec and not istraced(p)) {
+           printf("!! ignored signal %d at 0x%x\n",
+               p->signo, p->reg[PROGCTR]);
            fflush(stdout);
        }
        s = p->signo;
            fflush(stdout);
        }
        s = p->signo;
@@ -905,27 +944,41 @@ public pstep(p, signo)
 Process p;
 integer signo;
 {
 Process p;
 integer signo;
 {
-    int status;
+    int s, status;
 
 
-    setinfo(p, signo);
-    if (traceexec) {
-       printf("!! pstep from pc 0x%x with signal %d (%d)\n",
-           p->reg[PROGCTR], signo, p->signo);
-       fflush(stdout);
-    }
-    sigs_off();
-    if (ptrace(SSTEP, p->pid, p->reg[PROGCTR], p->signo) < 0) {
-       panic("error %d trying to step process", errno);
-    }
-    pwait(p->pid, &status);
-    sigs_on();
-    getinfo(p, status);
+    s = signo;
+    do {
+       setinfo(p, s);
+       if (traceexec) {
+           printf("!! pstep from 0x%x with signal %d (%d)\n",
+               p->reg[PROGCTR], s, p->signo);
+           fflush(stdout);
+       }
+       sigs_off();
+       if (ptrace(SSTEP, p->pid, p->reg[PROGCTR], p->signo) < 0) {
+           panic("error %d trying to step process", errno);
+       }
+       pwait(p->pid, &status);
+       sigs_on();
+       getinfo(p, status);
+       if (p->status == STOPPED and traceexec and not istraced(p)) {
+           printf("!! pstep ignored signal %d at 0x%x\n",
+               p->signo, p->reg[PROGCTR]);
+           fflush(stdout);
+       }
+       s = p->signo;
+    } while (p->status == STOPPED and not istraced(p));
     if (traceexec) {
     if (traceexec) {
-       printf("!! pstep to pc 0x%x on signal %d\n", p->reg[PROGCTR], p->signo);
+       printf("!! pstep to 0x%x on signal %d\n",
+           p->reg[PROGCTR], p->signo);
        fflush(stdout);
     }
     if (p->status != STOPPED) {
        fflush(stdout);
     }
     if (p->status != STOPPED) {
-       error("program unexpectedly exited with %d\n", p->exitval);
+       if (p->exitval == 0) {
+           error("program exited\n");
+       } else {
+           error("program exited with code %d\n", p->exitval);
+       }
     }
 }
 
     }
 }
 
@@ -992,10 +1045,6 @@ private sigs_on()
  * Get process information from user area.
  */
 
  * Get process information from user area.
  */
 
-private int rloc[] ={
-    R0, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, AP, FP, SP, PC
-};
-
 private getinfo(p, status)
 register Process p;
 register int status;
 private getinfo(p, status)
 register Process p;
 register int status;
@@ -1012,7 +1061,7 @@ register int status;
     } else {
        p->status = p->signo;
        p->signo = p->exitval;
     } else {
        p->status = p->signo;
        p->signo = p->exitval;
-       p->sigcode = ptrace(UREAD, p->pid, &((struct user *)0)->u_code, 0);
+       p->sigcode = ptrace(UREAD, p->pid, &((struct user *) 0)->u_code, 0);
        p->exitval = 0;
        p->mask = ptrace(UREAD, p->pid, regloc(PS), 0);
        for (i = 0; i < NREG; i++) {
        p->exitval = 0;
        p->mask = ptrace(UREAD, p->pid, regloc(PS), 0);
        for (i = 0; i < NREG; i++) {
@@ -1126,6 +1175,28 @@ int nbytes;
     byteoff = (nbytes&(~WMASK));
     nbytes -= byteoff;
     bufend = cp + nbytes;
     byteoff = (nbytes&(~WMASK));
     nbytes -= byteoff;
     bufend = cp + nbytes;
+#ifdef tahoe
+    if (((int)cp)&WMASK) {
+       /*
+        * Must copy a byte at a time, buffer not word addressable.
+        */
+       while (cp < bufend) {
+           if (op == PREAD) {
+               w.pword = fetch(p, seg, newaddr);
+               for (i = 0; i < sizeof(Word); i++)
+                   *cp++ = w.pbyte[i];
+           } else {
+               for (i = 0; i < sizeof(Word); i++)
+                   w.pbyte[i] = *cp++;
+               store(p, seg, newaddr, w.pword);
+           }
+           newaddr += sizeof(Word);
+       }
+    } else {
+    /*
+     * Buffer, word aligned, act normally...
+     */
+#endif
     while (cp < bufend) {
        if (op == PREAD) {
            *((Word *) cp) = fetch(p, seg, newaddr);
     while (cp < bufend) {
        if (op == PREAD) {
            *((Word *) cp) = fetch(p, seg, newaddr);
@@ -1135,6 +1206,9 @@ int nbytes;
        cp += sizeof(Word);
        newaddr += sizeof(Word);
     }
        cp += sizeof(Word);
        newaddr += sizeof(Word);
     }
+#ifdef tahoe
+    }
+#endif
     if (byteoff > 0) {
        w.pword = fetch(p, seg, newaddr);
        for (i = 0; i < byteoff; i++) {
     if (byteoff > 0) {
        w.pword = fetch(p, seg, newaddr);
        for (i = 0; i < byteoff; i++) {
@@ -1228,6 +1302,16 @@ Word data;
     }
 }
 
     }
 }
 
+/*
+ * Flush the instruction cache associated with a process.
+ */
+
+private cacheflush (p)
+Process p;
+{
+    bzero(p->word, sizeof(p->word));
+}
+
 public printptraceinfo()
 {
     printf("%d fetchs, %d reads, %d writes\n", nfetchs, nreads, nwrites);
 public printptraceinfo()
 {
     printf("%d fetchs, %d reads, %d writes\n", nfetchs, nreads, nwrites);
@@ -1289,64 +1373,72 @@ Fileid newfd;
     }
 }
 
     }
 }
 
-#define        bit(i)          (1 << ((i)-1))
 /*
 /*
- * Signal manipulation routines.
+ * Signal name manipulation.
  */
  */
-static String signames[NSIG] = {
-       0,
-       "HUP",
-       "INT",
-       "QUIT",
-       "ILL",
-       "TRAP",
-       "IOT",
-       "EMT",
-       "FPE",
-       "KILL",
-       "BUS",
-       "SEGV",
-       "SYS",
-       "PIPE",
-       "ALRM",
-       "TERM",
-       0,
-       "STOP",
-       "TSTP",
-       "CONT",
-       "CHLD",
-       "TTIN",
-       "TTOU",
-       "TINT",
-       "XCPU",
-       "XFSZ",
+
+private String signames[NSIG] = {
+    0,
+    "HUP", "INT", "QUIT", "ILL", "TRAP",
+    "IOT", "EMT", "FPE", "KILL", "BUS",
+    "SEGV", "SYS", "PIPE", "ALRM", "TERM",
+    0, "STOP", "TSTP", "CONT", "CHLD",
+    "TTIN", "TTOU", "TINT", "XCPU", "XFSZ",
+    "VTALRM", "PROF", "WINCH", "USR1", "USR2"
 };
 
 /*
 };
 
 /*
- * Map a signal name to a number.
+ * Get the signal number associated with a given name.
+ * The name is first translated to upper case if necessary.
  */
  */
-public signalname(s)
+
+public integer siglookup (s)
 String s;
 {
 String s;
 {
-       register String *p;
-
-       if (strneq(s, "SIG", 3))
-           s += 3;
-       for (p = signames; p < &signames[NSIG]; p++)
-               if (*p && streq(*p, s))
-                       return (p - signames);
-       error("%s: Unknown signal.", s);
+    register char *p, *q;
+    char buf[100];
+    integer i;
+
+    p = s;
+    q = buf;
+    while (*p != '\0') {
+       if (*p >= 'a' and *p <= 'z') {
+           *q = (*p - 'a') + 'A';
+       } else {
+           *q = *p;
+       }
+       ++p;
+       ++q;
+    }
+    *q = '\0';
+    p = buf;
+    if (buf[0] == 'S' and buf[1] == 'I' and buf[2] == 'G') {
+       p += 3;
+    }
+    i = 1;
+    for (;;) {
+       if (i >= sizeof(signames) div sizeof(signames[0])) {
+           error("signal \"%s\" unknown", s);
+           i = 0;
+           break;
+       }
+       if (signames[i] != nil and streq(signames[i], p)) {
+           break;
+       }
+       ++i;
+    }
+    return i;
 }
 
 /*
 }
 
 /*
- * Print all signals being ignored by the
- * debugger.  These signals are auotmatically
+ * Print all signals being ignored by the debugger.
+ * These signals are auotmatically
  * passed on to the debugged process.
  */
  * passed on to the debugged process.
  */
-public printsigsignored(p)
+
+public printsigsignored (p)
 Process p;
 {
 Process p;
 {
-
     printsigs(~p->sigset);
 }
 
     printsigs(~p->sigset);
 }
 
@@ -1354,26 +1446,30 @@ Process p;
  * Print all signals being intercepted by
  * the debugger for the specified process.
  */
  * Print all signals being intercepted by
  * the debugger for the specified process.
  */
+
 public printsigscaught(p)
 Process p;
 {
 public printsigscaught(p)
 Process p;
 {
-
     printsigs(p->sigset);
 }
 
     printsigs(p->sigset);
 }
 
-private printsigs(vec)
-register Integer vec;
+private printsigs (set)
+integer set;
 {
 {
-    register Integer s;
-    String sep = "";
-
-    for (s = 1; s < NSIG; s++)
-       if (vec & bit(s) && signames[s]) {
-           printf("%s%s", sep, signames[s]);
-           sep = " ";
+    integer s;
+    char separator[2];
+
+    separator[0] = '\0';
+    for (s = 1; s < sizeof(signames) div sizeof(signames[0]); s++) {
+       if (set & setrep(s)) {
+           if (signames[s] != nil) {
+               printf("%s%s", separator, signames[s]);
+               separator[0] = ' ';
+               separator[1] = '\0';
+           }
        }
        }
-    if (*sep != '\0') {
+    }
+    if (separator[0] == ' ') {
        putchar('\n');
        putchar('\n');
-       fflush(stdout);
     }
 }
     }
 }