xref: /csrg-svn/old/dbx/process.c (revision 11172)
19677Slinton /* Copyright (c) 1982 Regents of the University of California */
29677Slinton 
3*11172Slinton static char sccsid[] = "@(#)process.c 1.4 02/20/83";
49677Slinton 
59677Slinton /*
69677Slinton  * Process management.
79677Slinton  *
89677Slinton  * This module contains the routines to manage the execution and
99677Slinton  * tracing of the debuggee process.
109677Slinton  */
119677Slinton 
129677Slinton #include "defs.h"
139677Slinton #include "process.h"
149677Slinton #include "machine.h"
159677Slinton #include "events.h"
169677Slinton #include "tree.h"
179677Slinton #include "operators.h"
189677Slinton #include "source.h"
199677Slinton #include "object.h"
209677Slinton #include "mappings.h"
219677Slinton #include "main.h"
229677Slinton #include "coredump.h"
239677Slinton #include <signal.h>
249677Slinton #include <errno.h>
259677Slinton #include <sys/param.h>
269843Slinton #include <machine/reg.h>
279677Slinton #include <sys/stat.h>
289677Slinton 
299677Slinton #ifndef public
309677Slinton 
319677Slinton typedef struct Process *Process;
329677Slinton 
339677Slinton Process process;
349677Slinton 
359677Slinton #include "machine.h"
369677Slinton 
379677Slinton #endif
389677Slinton 
399677Slinton #define NOTSTARTED 1
409677Slinton #define STOPPED 0177
419677Slinton #define FINISHED 0
429677Slinton 
439677Slinton /*
449677Slinton  * Cache-ing of instruction segment is done to reduce the number
459677Slinton  * of system calls.
469677Slinton  */
479677Slinton 
489677Slinton #define CSIZE 1003       /* size of instruction cache */
499677Slinton 
509677Slinton typedef struct {
519677Slinton     Word addr;
529677Slinton     Word val;
539677Slinton } CacheWord;
549677Slinton 
559677Slinton /*
569677Slinton  * This structure holds the information we need from the user structure.
579677Slinton  */
589677Slinton 
599677Slinton struct Process {
609677Slinton     int pid;			/* process being traced */
619677Slinton     int mask;			/* ps */
629677Slinton     Word reg[NREG];		/* process's registers */
639677Slinton     Word oreg[NREG];		/* registers when process last stopped */
649677Slinton     short status;		/* either STOPPED or FINISHED */
659677Slinton     short signo;		/* signal that stopped process */
669677Slinton     int exitval;		/* return value from exit() */
679677Slinton     long sigset;		/* bit array of traced signals */
689677Slinton     CacheWord word[CSIZE];	/* text segment cache */
699677Slinton };
709677Slinton 
719677Slinton /*
729677Slinton  * These definitions are for the arguments to "pio".
739677Slinton  */
749677Slinton 
759677Slinton typedef enum { PREAD, PWRITE } PioOp;
769677Slinton typedef enum { TEXTSEG, DATASEG } PioSeg;
779677Slinton 
789677Slinton private struct Process pbuf;
799677Slinton 
809677Slinton #define MAXNCMDARGS 10         /* maximum number of arguments to RUN */
819677Slinton 
829677Slinton private Boolean just_started;
839677Slinton private int argc;
849677Slinton private String argv[MAXNCMDARGS];
859677Slinton private String infile, outfile;
869677Slinton 
879677Slinton /*
889677Slinton  * Initialize process information.
899677Slinton  */
909677Slinton 
919677Slinton public process_init()
929677Slinton {
939677Slinton     register Integer i;
949677Slinton     Char buf[10];
959677Slinton 
969677Slinton     process = &pbuf;
979677Slinton     process->status = (coredump) ? STOPPED : NOTSTARTED;
989677Slinton     setsigtrace();
999677Slinton     for (i = 0; i < NREG; i++) {
1009677Slinton 	sprintf(buf, "$r%d", i);
1019677Slinton 	defregname(identname(buf, false), i);
1029677Slinton     }
1039677Slinton     defregname(identname("$ap", true), ARGP);
1049677Slinton     defregname(identname("$fp", true), FRP);
1059677Slinton     defregname(identname("$sp", true), STKP);
1069677Slinton     defregname(identname("$pc", true), PROGCTR);
1079677Slinton     if (coredump) {
1089677Slinton 	coredump_readin(process->mask, process->reg, process->signo);
1099677Slinton     }
1109677Slinton }
1119677Slinton 
1129677Slinton /*
1139677Slinton  * Routines to get at process information from outside this module.
1149677Slinton  */
1159677Slinton 
1169677Slinton public Word reg(n)
1179677Slinton Integer n;
1189677Slinton {
1199677Slinton     register Word w;
1209677Slinton 
1219677Slinton     if (n == NREG) {
1229677Slinton 	w = process->mask;
1239677Slinton     } else {
1249677Slinton 	w = process->reg[n];
1259677Slinton     }
1269677Slinton     return w;
1279677Slinton }
1289677Slinton 
1299677Slinton public setreg(n, w)
1309677Slinton Integer n;
1319677Slinton Word w;
1329677Slinton {
1339677Slinton     process->reg[n] = w;
1349677Slinton }
1359677Slinton 
1369677Slinton /*
1379677Slinton  * Begin execution.
1389677Slinton  *
1399677Slinton  * We set a breakpoint at the end of the code so that the
1409677Slinton  * process data doesn't disappear after the program terminates.
1419677Slinton  */
1429677Slinton 
1439677Slinton private Boolean remade();
1449677Slinton 
1459677Slinton public start(argv, infile, outfile)
1469677Slinton String argv[];
1479677Slinton String infile, outfile;
1489677Slinton {
1499677Slinton     String pargv[4];
1509677Slinton     Node cond;
1519677Slinton 
1529677Slinton     if (coredump) {
1539677Slinton 	coredump = false;
1549677Slinton 	fclose(corefile);
1559677Slinton 	coredump_close();
1569677Slinton     }
1579677Slinton     if (argv == nil) {
1589677Slinton 	argv = pargv;
1599677Slinton 	pargv[0] = objname;
1609677Slinton 	pargv[1] = nil;
1619677Slinton     } else {
1629677Slinton 	argv[argc] = nil;
1639677Slinton     }
1649677Slinton     if (remade(objname)) {
1659677Slinton 	reinit(argv, infile, outfile);
1669677Slinton     }
1679677Slinton     pstart(process, argv, infile, outfile);
1689677Slinton     if (process->status == STOPPED) {
1699677Slinton 	pc = 0;
1709677Slinton 	curfunc = program;
1719677Slinton 	if (objsize != 0) {
1729677Slinton 	    cond = build(O_EQ, build(O_SYM, pcsym), build(O_LCON, lastaddr()));
1739677Slinton 	    event_once(cond, buildcmdlist(build(O_ENDX)));
1749677Slinton 	}
1759677Slinton     }
1769677Slinton }
1779677Slinton 
1789677Slinton /*
1799677Slinton  * Check to see if the object file has changed since the symbolic
1809677Slinton  * information last was read.
1819677Slinton  */
1829677Slinton 
1839677Slinton private time_t modtime;
1849677Slinton 
1859677Slinton private Boolean remade(filename)
1869677Slinton String filename;
1879677Slinton {
1889677Slinton     struct stat s;
1899677Slinton     Boolean b;
1909677Slinton 
1919677Slinton     stat(filename, &s);
1929677Slinton     b = (Boolean) (modtime != 0 and modtime < s.st_mtime);
1939677Slinton     modtime = s.st_mtime;
1949677Slinton     return b;
1959677Slinton }
1969677Slinton 
1979677Slinton /*
1989677Slinton  * Set up what signals we want to trace.
1999677Slinton  */
2009677Slinton 
2019677Slinton private setsigtrace()
2029677Slinton {
2039677Slinton     register Integer i;
2049677Slinton     register Process p;
2059677Slinton 
2069677Slinton     p = process;
2079677Slinton     for (i = 1; i <= NSIG; i++) {
2089677Slinton 	psigtrace(p, i, true);
2099677Slinton     }
2109677Slinton     psigtrace(p, SIGHUP, false);
2119677Slinton     psigtrace(p, SIGKILL, false);
2129677Slinton     psigtrace(p, SIGALRM, false);
2139677Slinton     psigtrace(p, SIGTSTP, false);
2149677Slinton     psigtrace(p, SIGCONT, false);
2159677Slinton     psigtrace(p, SIGCHLD, false);
2169677Slinton }
2179677Slinton 
2189677Slinton /*
2199677Slinton  * Initialize the argument list.
2209677Slinton  */
2219677Slinton 
2229677Slinton public arginit()
2239677Slinton {
2249677Slinton     infile = nil;
2259677Slinton     outfile = nil;
2269677Slinton     argv[0] = objname;
2279677Slinton     argc = 1;
2289677Slinton }
2299677Slinton 
2309677Slinton /*
2319677Slinton  * Add an argument to the list for the debuggee.
2329677Slinton  */
2339677Slinton 
2349677Slinton public newarg(arg)
2359677Slinton String arg;
2369677Slinton {
2379677Slinton     if (argc >= MAXNCMDARGS) {
2389677Slinton 	error("too many arguments");
2399677Slinton     }
2409677Slinton     argv[argc++] = arg;
2419677Slinton }
2429677Slinton 
2439677Slinton /*
2449677Slinton  * Set the standard input for the debuggee.
2459677Slinton  */
2469677Slinton 
2479677Slinton public inarg(filename)
2489677Slinton String filename;
2499677Slinton {
2509677Slinton     if (infile != nil) {
2519677Slinton 	error("multiple input redirects");
2529677Slinton     }
2539677Slinton     infile = filename;
2549677Slinton }
2559677Slinton 
2569677Slinton /*
2579677Slinton  * Set the standard output for the debuggee.
2589677Slinton  * Probably should check to avoid overwriting an existing file.
2599677Slinton  */
2609677Slinton 
2619677Slinton public outarg(filename)
2629677Slinton String filename;
2639677Slinton {
2649677Slinton     if (outfile != nil) {
2659677Slinton 	error("multiple output redirect");
2669677Slinton     }
2679677Slinton     outfile = filename;
2689677Slinton }
2699677Slinton 
2709677Slinton /*
2719677Slinton  * Start debuggee executing.
2729677Slinton  */
2739677Slinton 
2749677Slinton public run()
2759677Slinton {
2769677Slinton     process->status = STOPPED;
2779677Slinton     fixbps();
2789677Slinton     curline = 0;
2799677Slinton     start(argv, infile, outfile);
2809677Slinton     just_started = true;
2819677Slinton     isstopped = false;
2829677Slinton     cont();
2839677Slinton }
2849677Slinton 
2859677Slinton /*
2869677Slinton  * Continue execution wherever we left off.
2879677Slinton  *
2889677Slinton  * Note that this routine never returns.  Eventually bpact() will fail
2899677Slinton  * and we'll call printstatus or step will call it.
2909677Slinton  */
2919677Slinton 
2929677Slinton typedef int Intfunc();
2939677Slinton 
2949677Slinton private Intfunc *dbintr;
2959677Slinton private intr();
2969677Slinton 
2979677Slinton #define succeeds    == true
2989677Slinton #define fails       == false
2999677Slinton 
3009677Slinton public cont()
3019677Slinton {
3029677Slinton     dbintr = signal(SIGINT, intr);
3039677Slinton     if (just_started) {
3049677Slinton 	just_started = false;
3059677Slinton     } else {
3069677Slinton 	if (not isstopped) {
3079677Slinton 	    error("can't continue execution");
3089677Slinton 	}
3099677Slinton 	isstopped = false;
3109677Slinton 	step();
3119677Slinton     }
3129677Slinton     for (;;) {
3139677Slinton 	if (single_stepping) {
3149677Slinton 	    printnews();
3159677Slinton 	} else {
3169677Slinton 	    setallbps();
3179677Slinton 	    resume();
3189677Slinton 	    unsetallbps();
3199677Slinton 	    if (bpact() fails) {
3209677Slinton 		printstatus();
3219677Slinton 	    }
3229677Slinton 	}
3239677Slinton 	step();
3249677Slinton     }
3259677Slinton     /* NOTREACHED */
3269677Slinton }
3279677Slinton 
3289677Slinton /*
3299677Slinton  * This routine is called if we get an interrupt while "running" px
3309677Slinton  * but actually in the debugger.  Could happen, for example, while
3319677Slinton  * processing breakpoints.
3329677Slinton  *
3339677Slinton  * We basically just want to keep going; the assumption is
3349677Slinton  * that when the process resumes it will get the interrupt
3359677Slinton  * which will then be handled.
3369677Slinton  */
3379677Slinton 
3389677Slinton private intr()
3399677Slinton {
3409677Slinton     signal(SIGINT, intr);
3419677Slinton }
3429677Slinton 
3439677Slinton public fixintr()
3449677Slinton {
3459677Slinton     signal(SIGINT, dbintr);
3469677Slinton }
3479677Slinton 
3489677Slinton /*
3499677Slinton  * Resume execution.
3509677Slinton  */
3519677Slinton 
3529677Slinton public resume()
3539677Slinton {
3549677Slinton     register Process p;
3559677Slinton 
3569677Slinton     p = process;
3579677Slinton     if (traceexec) {
3589677Slinton 	printf("execution resumes at pc 0x%x\n", process->reg[PROGCTR]);
3599677Slinton 	fflush(stdout);
3609677Slinton     }
3619677Slinton     pcont(p);
3629677Slinton     pc = process->reg[PROGCTR];
3639677Slinton     if (traceexec) {
3649677Slinton 	printf("execution stops at pc 0x%x on sig %d\n",
3659677Slinton 	    process->reg[PROGCTR], p->signo);
3669677Slinton 	fflush(stdout);
3679677Slinton     }
3689677Slinton }
3699677Slinton 
3709677Slinton /*
3719677Slinton  * Continue execution up to the next source line.
3729677Slinton  *
3739677Slinton  * There are two ways to define the next source line depending on what
3749677Slinton  * is desired when a procedure or function call is encountered.  Step
3759677Slinton  * stops at the beginning of the procedure or call; next skips over it.
3769677Slinton  */
3779677Slinton 
3789677Slinton /*
3799677Slinton  * Stepc is what is called when the step command is given.
3809677Slinton  * It has to play with the "isstopped" information.
3819677Slinton  */
3829677Slinton 
3839677Slinton public stepc()
3849677Slinton {
3859677Slinton     if (not isstopped) {
3869677Slinton 	error("can't continue execution");
3879677Slinton     }
3889677Slinton     isstopped = false;
3899677Slinton     dostep(false);
3909677Slinton     isstopped = true;
3919677Slinton }
3929677Slinton 
3939677Slinton public next()
3949677Slinton {
3959677Slinton     if (not isstopped) {
3969677Slinton 	error("can't continue execution");
3979677Slinton     }
3989677Slinton     isstopped = false;
3999677Slinton     dostep(true);
4009677Slinton     isstopped = true;
4019677Slinton }
4029677Slinton 
4039677Slinton public step()
4049677Slinton {
4059677Slinton     dostep(false);
4069677Slinton }
4079677Slinton 
4089677Slinton /*
4099677Slinton  * Resume execution up to the given address.  It is assumed that
4109677Slinton  * no breakpoints exist between the current address and the one
4119677Slinton  * we're stepping to.  This saves us from setting all the breakpoints.
4129677Slinton  */
4139677Slinton 
4149677Slinton public stepto(addr)
4159677Slinton Address addr;
4169677Slinton {
4179677Slinton     setbp(addr);
4189677Slinton     resume();
4199677Slinton     unsetbp(addr);
4209677Slinton     if (not isbperr()) {
4219677Slinton 	printstatus();
4229677Slinton     }
4239677Slinton }
4249677Slinton 
4259677Slinton /*
4269677Slinton  * Print the status of the process.
4279677Slinton  * This routine does not return.
4289677Slinton  */
4299677Slinton 
4309677Slinton public printstatus()
4319677Slinton {
4329843Slinton     if (process->status == FINISHED) {
4339843Slinton 	exit(0);
4349843Slinton     } else {
4359843Slinton 	curfunc = whatblock(pc);
4369677Slinton 	getsrcpos();
4379843Slinton 	if (process->signo == SIGINT) {
4389843Slinton 	    isstopped = true;
4399843Slinton 	    printerror();
4409843Slinton 	} else if (isbperr() and isstopped) {
4419843Slinton 	    printf("stopped ");
442*11172Slinton 	    printloc();
443*11172Slinton 	    putchar('\n');
4449843Slinton 	    if (curline > 0) {
4459843Slinton 		printlines(curline, curline);
4469843Slinton 	    } else {
4479843Slinton 		printinst(pc, pc);
4489843Slinton 	    }
4499843Slinton 	    erecover();
4509677Slinton 	} else {
4519843Slinton 	    fixbps();
4529843Slinton 	    fixintr();
4539677Slinton 	    isstopped = true;
4549677Slinton 	    printerror();
4559677Slinton 	}
4569677Slinton     }
4579677Slinton }
4589677Slinton 
4599677Slinton /*
460*11172Slinton  * Print out the current location in the debuggee.
461*11172Slinton  */
462*11172Slinton 
463*11172Slinton public printloc()
464*11172Slinton {
465*11172Slinton     printf("in ");
466*11172Slinton     printname(stdout, curfunc);
467*11172Slinton     putchar(' ');
468*11172Slinton     if (curline > 0) {
469*11172Slinton 	printsrcpos();
470*11172Slinton     } else {
471*11172Slinton 	printf("at 0x%x", pc);
472*11172Slinton     }
473*11172Slinton }
474*11172Slinton 
475*11172Slinton /*
4769677Slinton  * Some functions for testing the state of the process.
4779677Slinton  */
4789677Slinton 
4799677Slinton public Boolean notstarted(p)
4809677Slinton Process p;
4819677Slinton {
4829677Slinton     return (Boolean) (p->status == NOTSTARTED);
4839677Slinton }
4849677Slinton 
4859677Slinton public Boolean isfinished(p)
4869677Slinton Process p;
4879677Slinton {
4889677Slinton     return (Boolean) (p->status == FINISHED);
4899677Slinton }
4909677Slinton 
4919677Slinton /*
4929677Slinton  * Return the signal number which stopped the process.
4939677Slinton  */
4949677Slinton 
4959677Slinton public Integer errnum(p)
4969677Slinton Process p;
4979677Slinton {
4989677Slinton     return p->signo;
4999677Slinton }
5009677Slinton 
5019677Slinton /*
5029677Slinton  * Return the termination code of the process.
5039677Slinton  */
5049677Slinton 
5059677Slinton public Integer exitcode(p)
5069677Slinton Process p;
5079677Slinton {
5089677Slinton     return p->exitval;
5099677Slinton }
5109677Slinton 
5119677Slinton /*
5129677Slinton  * These routines are used to access the debuggee process from
5139677Slinton  * outside this module.
5149677Slinton  *
5159677Slinton  * They invoke "pio" which eventually leads to a call to "ptrace".
5169677Slinton  * The system generates an I/O error when a ptrace fails, we catch
5179677Slinton  * that here and assume its due to a misguided address.
5189677Slinton  */
5199677Slinton 
5209677Slinton extern Intfunc *onsyserr();
5219677Slinton 
5229677Slinton private badaddr;
5239677Slinton private rwerr();
5249677Slinton 
5259677Slinton /*
5269677Slinton  * Read from the process' instruction area.
5279677Slinton  */
5289677Slinton 
5299677Slinton public iread(buff, addr, nbytes)
5309677Slinton char *buff;
5319677Slinton Address addr;
5329677Slinton int nbytes;
5339677Slinton {
5349677Slinton     Intfunc *f;
5359677Slinton 
5369677Slinton     f = onsyserr(EIO, rwerr);
5379677Slinton     badaddr = addr;
5389677Slinton     if (coredump) {
5399677Slinton 	coredump_readtext(buff, addr, nbytes);
5409677Slinton     } else {
5419677Slinton 	pio(process, PREAD, TEXTSEG, buff, addr, nbytes);
5429677Slinton     }
5439677Slinton     onsyserr(EIO, f);
5449677Slinton }
5459677Slinton 
5469677Slinton /*
5479677Slinton  * Write to the process' instruction area, usually in order to set
5489677Slinton  * or unset a breakpoint.
5499677Slinton  */
5509677Slinton 
5519677Slinton public iwrite(buff, addr, nbytes)
5529677Slinton char *buff;
5539677Slinton Address addr;
5549677Slinton int nbytes;
5559677Slinton {
5569677Slinton     Intfunc *f;
5579677Slinton 
5589677Slinton     if (coredump) {
5599677Slinton 	error("no process to write to");
5609677Slinton     }
5619677Slinton     f = onsyserr(EIO, rwerr);
5629677Slinton     badaddr = addr;
5639677Slinton     pio(process, PWRITE, TEXTSEG, buff, addr, nbytes);
5649677Slinton     onsyserr(EIO, f);
5659677Slinton }
5669677Slinton 
5679677Slinton /*
5689677Slinton  * Read for the process' data area.
5699677Slinton  */
5709677Slinton 
5719677Slinton public dread(buff, addr, nbytes)
5729677Slinton char *buff;
5739677Slinton Address addr;
5749677Slinton int nbytes;
5759677Slinton {
5769677Slinton     Intfunc *f;
5779677Slinton 
5789677Slinton     f = onsyserr(EIO, rwerr);
5799677Slinton     badaddr = addr;
5809677Slinton     if (coredump) {
5819677Slinton 	coredump_readdata(buff, addr, nbytes);
5829677Slinton     } else {
5839677Slinton 	pio(process, PREAD, DATASEG, buff, addr, nbytes);
5849677Slinton     }
5859677Slinton     onsyserr(EIO, f);
5869677Slinton }
5879677Slinton 
5889677Slinton /*
5899677Slinton  * Write to the process' data area.
5909677Slinton  */
5919677Slinton 
5929677Slinton public dwrite(buff, addr, nbytes)
5939677Slinton char *buff;
5949677Slinton Address addr;
5959677Slinton int nbytes;
5969677Slinton {
5979677Slinton     Intfunc *f;
5989677Slinton 
5999677Slinton     if (coredump) {
6009677Slinton 	error("no process to write to");
6019677Slinton     }
6029677Slinton     f = onsyserr(EIO, rwerr);
6039677Slinton     badaddr = addr;
6049677Slinton     pio(process, PWRITE, DATASEG, buff, addr, nbytes);
6059677Slinton     onsyserr(EIO, f);
6069677Slinton }
6079677Slinton 
6089677Slinton /*
6099677Slinton  * Error handler.
6109677Slinton  */
6119677Slinton 
6129677Slinton private rwerr()
6139677Slinton {
6149677Slinton     error("bad read/write process address 0x%x", badaddr);
6159677Slinton }
6169677Slinton 
6179677Slinton /*
6189677Slinton  * Ptrace interface.
6199677Slinton  */
6209677Slinton 
6219677Slinton /*
6229677Slinton  * This magic macro enables us to look at the process' registers
6239677Slinton  * in its user structure.  Very gross.
6249677Slinton  */
6259677Slinton 
6269677Slinton #define regloc(reg)     (ctob(UPAGES) + ( sizeof(int) * (reg) ))
6279677Slinton 
6289677Slinton #define WMASK           (~(sizeof(Word) - 1))
6299677Slinton #define cachehash(addr) ((unsigned) ((addr >> 2) % CSIZE))
6309677Slinton 
6319677Slinton #define FIRSTSIG        SIGINT
6329677Slinton #define LASTSIG         SIGQUIT
6339677Slinton #define ischild(pid)    ((pid) == 0)
6349677Slinton #define traceme()       ptrace(0, 0, 0, 0)
6359677Slinton #define setrep(n)       (1 << ((n)-1))
6369677Slinton #define istraced(p)     (p->sigset&setrep(p->signo))
6379677Slinton 
6389677Slinton /*
6399677Slinton  * Ptrace options (specified in first argument).
6409677Slinton  */
6419677Slinton 
6429677Slinton #define UREAD   3       /* read from process's user structure */
6439677Slinton #define UWRITE  6       /* write to process's user structure */
6449677Slinton #define IREAD   1       /* read from process's instruction space */
6459677Slinton #define IWRITE  4       /* write to process's instruction space */
6469677Slinton #define DREAD   2       /* read from process's data space */
6479677Slinton #define DWRITE  5       /* write to process's data space */
6489677Slinton #define CONT    7       /* continue stopped process */
6499677Slinton #define SSTEP   9       /* continue for approximately one instruction */
6509677Slinton #define PKILL   8       /* terminate the process */
6519677Slinton 
6529677Slinton /*
6539677Slinton  * Start up a new process by forking and exec-ing the
6549677Slinton  * given argument list, returning when the process is loaded
6559677Slinton  * and ready to execute.  The PROCESS information (pointed to
6569677Slinton  * by the first argument) is appropriately filled.
6579677Slinton  *
6589677Slinton  * If the given PROCESS structure is associated with an already running
6599677Slinton  * process, we terminate it.
6609677Slinton  */
6619677Slinton 
6629677Slinton /* VARARGS2 */
6639677Slinton private pstart(p, argv, infile, outfile)
6649677Slinton Process p;
6659677Slinton String argv[];
6669677Slinton String infile;
6679677Slinton String outfile;
6689677Slinton {
6699677Slinton     int status;
6709677Slinton 
6719677Slinton     if (p->pid != 0) {          	/* child already running? */
6729677Slinton 	ptrace(PKILL, p->pid, 0, 0);    /* ... kill it! */
6739677Slinton     }
6749677Slinton     psigtrace(p, SIGTRAP, true);
675*11172Slinton     if ((p->pid = vfork()) == -1) {
6769677Slinton 	panic("can't fork");
6779677Slinton     }
6789677Slinton     if (ischild(p->pid)) {
679*11172Slinton 	Fileid in, out;
680*11172Slinton 
6819677Slinton 	traceme();
6829677Slinton 	if (infile != nil) {
683*11172Slinton 	    in = open(infile, 0);
684*11172Slinton 	    if (in == -1) {
685*11172Slinton 		write(2, "can't read ", 11);
686*11172Slinton 		write(2, infile, strlen(infile));
687*11172Slinton 		write(2, "\n", 1);
688*11172Slinton 		_exit(1);
6899677Slinton 	    }
690*11172Slinton 	    fswap(0, in);
6919677Slinton 	}
6929677Slinton 	if (outfile != nil) {
693*11172Slinton 	    out = creat(outfile, 0666);
694*11172Slinton 	    if (out == -1) {
695*11172Slinton 		write(2, "can't write ", 12);
696*11172Slinton 		write(2, outfile, strlen(outfile));
697*11172Slinton 		write(2, "\n", 1);
698*11172Slinton 		_exit(1);
6999677Slinton 	    }
700*11172Slinton 	    fswap(1, out);
7019677Slinton 	}
7029677Slinton 	execvp(argv[0], argv);
703*11172Slinton 	write(2, "can't exec ", 11);
704*11172Slinton 	write(2, argv[0], strlen(argv[0]));
705*11172Slinton 	write(2, "\n", 1);
706*11172Slinton 	_exit(1);
7079677Slinton     }
7089677Slinton     pwait(p->pid, &status);
7099677Slinton     getinfo(p, status);
7109677Slinton     if (p->status != STOPPED) {
7119677Slinton 	error("program could not begin execution");
7129677Slinton     }
7139677Slinton }
7149677Slinton 
7159677Slinton /*
7169677Slinton  * Continue a stopped process.  The argument points to a PROCESS structure.
7179677Slinton  * Before the process is restarted it's user area is modified according to
7189677Slinton  * the values in the structure.  When this routine finishes,
7199677Slinton  * the structure has the new values from the process's user area.
7209677Slinton  *
7219677Slinton  * Pcont terminates when the process stops with a signal pending that
7229677Slinton  * is being traced (via psigtrace), or when the process terminates.
7239677Slinton  */
7249677Slinton 
7259677Slinton private pcont(p)
7269677Slinton Process p;
7279677Slinton {
7289677Slinton     int status;
7299677Slinton 
7309677Slinton     if (p->pid == 0) {
7319677Slinton 	error("program not active");
7329677Slinton     }
7339677Slinton     do {
7349677Slinton 	setinfo(p);
7359677Slinton 	sigs_off();
7369677Slinton 	if (ptrace(CONT, p->pid, p->reg[PROGCTR], p->signo) < 0) {
7379677Slinton 	    panic("can't continue process");
7389677Slinton 	}
7399677Slinton 	pwait(p->pid, &status);
7409677Slinton 	sigs_on();
7419677Slinton 	getinfo(p, status);
7429677Slinton     } while (p->status == STOPPED and not istraced(p));
7439677Slinton }
7449677Slinton 
7459677Slinton /*
7469677Slinton  * Single step as best ptrace can.
7479677Slinton  */
7489677Slinton 
7499677Slinton public pstep(p)
7509677Slinton Process p;
7519677Slinton {
7529677Slinton     int status;
7539677Slinton 
7549677Slinton     setinfo(p);
7559677Slinton     sigs_off();
7569677Slinton     ptrace(SSTEP, p->pid, p->reg[PROGCTR], p->signo);
7579677Slinton     pwait(p->pid, &status);
7589677Slinton     sigs_on();
7599677Slinton     getinfo(p, status);
7609677Slinton }
7619677Slinton 
7629677Slinton /*
7639677Slinton  * Return from execution when the given signal is pending.
7649677Slinton  */
7659677Slinton 
7669677Slinton public psigtrace(p, sig, sw)
7679677Slinton Process p;
7689677Slinton int sig;
7699677Slinton Boolean sw;
7709677Slinton {
7719677Slinton     if (sw) {
7729677Slinton 	p->sigset |= setrep(sig);
7739677Slinton     } else {
7749677Slinton 	p->sigset &= ~setrep(sig);
7759677Slinton     }
7769677Slinton }
7779677Slinton 
7789677Slinton /*
7799677Slinton  * Don't catch any signals.
7809677Slinton  * Particularly useful when letting a process finish uninhibited.
7819677Slinton  */
7829677Slinton 
7839677Slinton public unsetsigtraces(p)
7849677Slinton Process p;
7859677Slinton {
7869677Slinton     p->sigset = 0;
7879677Slinton }
7889677Slinton 
7899677Slinton /*
7909677Slinton  * Turn off attention to signals not being caught.
7919677Slinton  */
7929677Slinton 
7939677Slinton private Intfunc *sigfunc[NSIG];
7949677Slinton 
7959677Slinton private sigs_off()
7969677Slinton {
7979677Slinton     register int i;
7989677Slinton 
7999677Slinton     for (i = FIRSTSIG; i < LASTSIG; i++) {
8009677Slinton 	if (i != SIGKILL) {
8019677Slinton 	    sigfunc[i] = signal(i, SIG_IGN);
8029677Slinton 	}
8039677Slinton     }
8049677Slinton }
8059677Slinton 
8069677Slinton /*
8079677Slinton  * Turn back on attention to signals.
8089677Slinton  */
8099677Slinton 
8109677Slinton private sigs_on()
8119677Slinton {
8129677Slinton     register int i;
8139677Slinton 
8149677Slinton     for (i = FIRSTSIG; i < LASTSIG; i++) {
8159677Slinton 	if (i != SIGKILL) {
8169677Slinton 	    signal(i, sigfunc[i]);
8179677Slinton 	}
8189677Slinton     }
8199677Slinton }
8209677Slinton 
8219677Slinton /*
8229677Slinton  * Get process information from user area.
8239677Slinton  */
8249677Slinton 
8259677Slinton private int rloc[] ={
8269677Slinton     R0, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, AP, FP, SP, PC
8279677Slinton };
8289677Slinton 
8299677Slinton private getinfo(p, status)
8309677Slinton register Process p;
8319677Slinton register int status;
8329677Slinton {
8339677Slinton     register int i;
8349677Slinton 
8359677Slinton     p->signo = (status&0177);
8369677Slinton     p->exitval = ((status >> 8)&0377);
8379677Slinton     if (p->signo != STOPPED) {
8389677Slinton 	p->status = FINISHED;
8399677Slinton     } else {
8409677Slinton 	p->status = p->signo;
8419677Slinton 	p->signo = p->exitval;
8429677Slinton 	p->exitval = 0;
8439677Slinton 	p->mask = ptrace(UREAD, p->pid, regloc(PS), 0);
8449677Slinton 	for (i = 0; i < NREG; i++) {
8459677Slinton 	    p->reg[i] = ptrace(UREAD, p->pid, regloc(rloc[i]), 0);
8469677Slinton 	    p->oreg[i] = p->reg[i];
8479677Slinton 	}
8489677Slinton     }
8499677Slinton }
8509677Slinton 
8519677Slinton /*
8529677Slinton  * Set process's user area information from given process structure.
8539677Slinton  */
8549677Slinton 
8559677Slinton private setinfo(p)
8569677Slinton register Process p;
8579677Slinton {
8589677Slinton     register int i;
8599677Slinton     register int r;
8609677Slinton 
8619677Slinton     if (istraced(p)) {
8629677Slinton 	p->signo = 0;
8639677Slinton     }
8649677Slinton     for (i = 0; i < NREG; i++) {
8659677Slinton 	if ((r = p->reg[i]) != p->oreg[i]) {
8669677Slinton 	    ptrace(UWRITE, p->pid, regloc(rloc[i]), r);
8679677Slinton 	}
8689677Slinton     }
8699677Slinton }
8709677Slinton 
8719677Slinton /*
8729677Slinton  * Structure for reading and writing by words, but dealing with bytes.
8739677Slinton  */
8749677Slinton 
8759677Slinton typedef union {
8769677Slinton     Word pword;
8779677Slinton     Byte pbyte[sizeof(Word)];
8789677Slinton } Pword;
8799677Slinton 
8809677Slinton /*
8819677Slinton  * Read (write) from (to) the process' address space.
8829677Slinton  * We must deal with ptrace's inability to look anywhere other
8839677Slinton  * than at a word boundary.
8849677Slinton  */
8859677Slinton 
8869677Slinton private Word fetch();
8879677Slinton private store();
8889677Slinton 
8899677Slinton private pio(p, op, seg, buff, addr, nbytes)
8909677Slinton Process p;
8919677Slinton PioOp op;
8929677Slinton PioSeg seg;
8939677Slinton char *buff;
8949677Slinton Address addr;
8959677Slinton int nbytes;
8969677Slinton {
8979677Slinton     register int i;
8989677Slinton     register Address newaddr;
8999677Slinton     register char *cp;
9009677Slinton     char *bufend;
9019677Slinton     Pword w;
9029677Slinton     Address wordaddr;
9039677Slinton     int byteoff;
9049677Slinton 
9059677Slinton     if (p->status != STOPPED) {
9069677Slinton 	error("program is not active");
9079677Slinton     }
9089677Slinton     cp = buff;
9099677Slinton     newaddr = addr;
9109677Slinton     wordaddr = (newaddr&WMASK);
9119677Slinton     if (wordaddr != newaddr) {
9129677Slinton 	w.pword = fetch(p, seg, wordaddr);
9139677Slinton 	for (i = newaddr - wordaddr; i < sizeof(Word) and nbytes > 0; i++) {
9149677Slinton 	    if (op == PREAD) {
9159677Slinton 		*cp++ = w.pbyte[i];
9169677Slinton 	    } else {
9179677Slinton 		w.pbyte[i] = *cp++;
9189677Slinton 	    }
9199677Slinton 	    nbytes--;
9209677Slinton 	}
9219677Slinton 	if (op == PWRITE) {
9229677Slinton 	    store(p, seg, wordaddr, w.pword);
9239677Slinton 	}
9249677Slinton 	newaddr = wordaddr + sizeof(Word);
9259677Slinton     }
9269677Slinton     byteoff = (nbytes&(~WMASK));
9279677Slinton     nbytes -= byteoff;
9289677Slinton     bufend = cp + nbytes;
9299677Slinton     while (cp < bufend) {
9309677Slinton 	if (op == PREAD) {
9319677Slinton 	    *((Word *) cp) = fetch(p, seg, newaddr);
9329677Slinton 	} else {
9339677Slinton 	    store(p, seg, newaddr, *((Word *) cp));
9349677Slinton 	}
9359677Slinton 	cp += sizeof(Word);
9369677Slinton 	newaddr += sizeof(Word);
9379677Slinton     }
9389677Slinton     if (byteoff > 0) {
9399677Slinton 	w.pword = fetch(p, seg, newaddr);
9409677Slinton 	for (i = 0; i < byteoff; i++) {
9419677Slinton 	    if (op == PREAD) {
9429677Slinton 		*cp++ = w.pbyte[i];
9439677Slinton 	    } else {
9449677Slinton 		w.pbyte[i] = *cp++;
9459677Slinton 	    }
9469677Slinton 	}
9479677Slinton 	if (op == PWRITE) {
9489677Slinton 	    store(p, seg, newaddr, w.pword);
9499677Slinton 	}
9509677Slinton     }
9519677Slinton }
9529677Slinton 
9539677Slinton /*
9549677Slinton  * Get a word from a process at the given address.
9559677Slinton  * The address is assumed to be on a word boundary.
9569677Slinton  *
9579677Slinton  * A simple cache scheme is used to avoid redundant ptrace calls
9589677Slinton  * to the instruction space since it is assumed to be pure.
9599677Slinton  *
9609677Slinton  * It is necessary to use a write-through scheme so that
9619677Slinton  * breakpoints right next to each other don't interfere.
9629677Slinton  */
9639677Slinton 
9649677Slinton private Integer nfetchs, nreads, nwrites;
9659677Slinton 
9669677Slinton private Word fetch(p, seg, addr)
9679677Slinton Process p;
9689677Slinton PioSeg seg;
9699677Slinton register int addr;
9709677Slinton {
9719677Slinton     register CacheWord *wp;
9729677Slinton     register Word w;
9739677Slinton 
9749677Slinton     switch (seg) {
9759677Slinton 	case TEXTSEG:
9769677Slinton 	    ++nfetchs;
9779677Slinton 	    wp = &p->word[cachehash(addr)];
9789677Slinton 	    if (addr == 0 or wp->addr != addr) {
9799677Slinton 		++nreads;
9809677Slinton 		w = ptrace(IREAD, p->pid, addr, 0);
9819677Slinton 		wp->addr = addr;
9829677Slinton 		wp->val = w;
9839677Slinton 	    } else {
9849677Slinton 		w = wp->val;
9859677Slinton 	    }
9869677Slinton 	    break;
9879677Slinton 
9889677Slinton 	case DATASEG:
9899677Slinton 	    w = ptrace(DREAD, p->pid, addr, 0);
9909677Slinton 	    break;
9919677Slinton 
9929677Slinton 	default:
9939677Slinton 	    panic("fetch: bad seg %d", seg);
9949677Slinton 	    /* NOTREACHED */
9959677Slinton     }
9969677Slinton     return w;
9979677Slinton }
9989677Slinton 
9999677Slinton /*
10009677Slinton  * Put a word into the process' address space at the given address.
10019677Slinton  * The address is assumed to be on a word boundary.
10029677Slinton  */
10039677Slinton 
10049677Slinton private store(p, seg, addr, data)
10059677Slinton Process p;
10069677Slinton PioSeg seg;
10079677Slinton int addr;
10089677Slinton Word data;
10099677Slinton {
10109677Slinton     register CacheWord *wp;
10119677Slinton 
10129677Slinton     switch (seg) {
10139677Slinton 	case TEXTSEG:
10149677Slinton 	    ++nwrites;
10159677Slinton 	    wp = &p->word[cachehash(addr)];
10169677Slinton 	    wp->addr = addr;
10179677Slinton 	    wp->val = data;
10189677Slinton 	    ptrace(IWRITE, p->pid, addr, data);
10199677Slinton 	    break;
10209677Slinton 
10219677Slinton 	case DATASEG:
10229677Slinton 	    ptrace(DWRITE, p->pid, addr, data);
10239677Slinton 	    break;
10249677Slinton 
10259677Slinton 	default:
10269677Slinton 	    panic("store: bad seg %d", seg);
10279677Slinton 	    /* NOTREACHED */
10289677Slinton     }
10299677Slinton }
10309677Slinton 
10319677Slinton public printptraceinfo()
10329677Slinton {
10339677Slinton     printf("%d fetchs, %d reads, %d writes\n", nfetchs, nreads, nwrites);
10349677Slinton }
10359677Slinton 
10369677Slinton /*
10379677Slinton  * Swap file numbers so as to redirect standard input and output.
10389677Slinton  */
10399677Slinton 
10409677Slinton private fswap(oldfd, newfd)
10419677Slinton int oldfd;
10429677Slinton int newfd;
10439677Slinton {
10449677Slinton     if (oldfd != newfd) {
10459677Slinton 	close(oldfd);
10469677Slinton 	dup(newfd);
10479677Slinton 	close(newfd);
10489677Slinton     }
10499677Slinton }
1050