Commit | Line | Data |
---|---|---|
9a3bab7a ML |
1 | /* Copyright (c) 1982 Regents of the University of California */ |
2 | ||
3 | static char sccsid[] = "@(#)@(#)process.c 1.1 %G%"; | |
4 | ||
5 | /* | |
6 | * Process management. | |
7 | * | |
8 | * This module contains the routines to manage the execution and | |
9 | * tracing of the debuggee process. | |
10 | */ | |
11 | ||
12 | #include "defs.h" | |
13 | #include "process.h" | |
14 | #include "machine.h" | |
15 | #include "events.h" | |
16 | #include "tree.h" | |
17 | #include "operators.h" | |
18 | #include "source.h" | |
19 | #include "object.h" | |
20 | #include "mappings.h" | |
21 | #include "main.h" | |
22 | #include "coredump.h" | |
23 | #include <signal.h> | |
24 | #include <errno.h> | |
25 | #include <sys/param.h> | |
26 | #include <sys/reg.h> | |
27 | #include <sys/stat.h> | |
28 | ||
29 | #ifndef public | |
30 | ||
31 | typedef struct Process *Process; | |
32 | ||
33 | Process process; | |
34 | ||
35 | #include "machine.h" | |
36 | ||
37 | #endif | |
38 | ||
39 | #define NOTSTARTED 1 | |
40 | #define STOPPED 0177 | |
41 | #define FINISHED 0 | |
42 | ||
43 | /* | |
44 | * Cache-ing of instruction segment is done to reduce the number | |
45 | * of system calls. | |
46 | */ | |
47 | ||
48 | #define CSIZE 1003 /* size of instruction cache */ | |
49 | ||
50 | typedef struct { | |
51 | Word addr; | |
52 | Word val; | |
53 | } CacheWord; | |
54 | ||
55 | /* | |
56 | * This structure holds the information we need from the user structure. | |
57 | */ | |
58 | ||
59 | struct Process { | |
60 | int pid; /* process being traced */ | |
61 | int mask; /* ps */ | |
62 | Word reg[NREG]; /* process's registers */ | |
63 | Word oreg[NREG]; /* registers when process last stopped */ | |
64 | short status; /* either STOPPED or FINISHED */ | |
65 | short signo; /* signal that stopped process */ | |
66 | int exitval; /* return value from exit() */ | |
67 | long sigset; /* bit array of traced signals */ | |
68 | CacheWord word[CSIZE]; /* text segment cache */ | |
69 | }; | |
70 | ||
71 | /* | |
72 | * These definitions are for the arguments to "pio". | |
73 | */ | |
74 | ||
75 | typedef enum { PREAD, PWRITE } PioOp; | |
76 | typedef enum { TEXTSEG, DATASEG } PioSeg; | |
77 | ||
78 | private struct Process pbuf; | |
79 | ||
80 | #define MAXNCMDARGS 10 /* maximum number of arguments to RUN */ | |
81 | ||
82 | private Boolean just_started; | |
83 | private int argc; | |
84 | private String argv[MAXNCMDARGS]; | |
85 | private String infile, outfile; | |
86 | ||
87 | /* | |
88 | * Initialize process information. | |
89 | */ | |
90 | ||
91 | public process_init() | |
92 | { | |
93 | register Integer i; | |
94 | Char buf[10]; | |
95 | ||
96 | process = &pbuf; | |
97 | process->status = (coredump) ? STOPPED : NOTSTARTED; | |
98 | setsigtrace(); | |
99 | for (i = 0; i < NREG; i++) { | |
100 | sprintf(buf, "$r%d", i); | |
101 | defregname(identname(buf, false), i); | |
102 | } | |
103 | defregname(identname("$ap", true), ARGP); | |
104 | defregname(identname("$fp", true), FRP); | |
105 | defregname(identname("$sp", true), STKP); | |
106 | defregname(identname("$pc", true), PROGCTR); | |
107 | if (coredump) { | |
108 | coredump_readin(process->mask, process->reg, process->signo); | |
109 | } | |
110 | } | |
111 | ||
112 | /* | |
113 | * Routines to get at process information from outside this module. | |
114 | */ | |
115 | ||
116 | public Word reg(n) | |
117 | Integer n; | |
118 | { | |
119 | register Word w; | |
120 | ||
121 | if (n == NREG) { | |
122 | w = process->mask; | |
123 | } else { | |
124 | w = process->reg[n]; | |
125 | } | |
126 | return w; | |
127 | } | |
128 | ||
129 | public setreg(n, w) | |
130 | Integer n; | |
131 | Word w; | |
132 | { | |
133 | process->reg[n] = w; | |
134 | } | |
135 | ||
136 | /* | |
137 | * Begin execution. | |
138 | * | |
139 | * We set a breakpoint at the end of the code so that the | |
140 | * process data doesn't disappear after the program terminates. | |
141 | */ | |
142 | ||
143 | private Boolean remade(); | |
144 | ||
145 | public start(argv, infile, outfile) | |
146 | String argv[]; | |
147 | String infile, outfile; | |
148 | { | |
149 | String pargv[4]; | |
150 | Node cond; | |
151 | ||
152 | if (coredump) { | |
153 | coredump = false; | |
154 | fclose(corefile); | |
155 | coredump_close(); | |
156 | } | |
157 | if (argv == nil) { | |
158 | argv = pargv; | |
159 | pargv[0] = objname; | |
160 | pargv[1] = nil; | |
161 | } else { | |
162 | argv[argc] = nil; | |
163 | } | |
164 | if (remade(objname)) { | |
165 | reinit(argv, infile, outfile); | |
166 | } | |
167 | pstart(process, argv, infile, outfile); | |
168 | if (process->status == STOPPED) { | |
169 | pc = 0; | |
170 | curfunc = program; | |
171 | if (objsize != 0) { | |
172 | cond = build(O_EQ, build(O_SYM, pcsym), build(O_LCON, lastaddr())); | |
173 | event_once(cond, buildcmdlist(build(O_ENDX))); | |
174 | } | |
175 | } | |
176 | } | |
177 | ||
178 | /* | |
179 | * Check to see if the object file has changed since the symbolic | |
180 | * information last was read. | |
181 | */ | |
182 | ||
183 | private time_t modtime; | |
184 | ||
185 | private Boolean remade(filename) | |
186 | String filename; | |
187 | { | |
188 | struct stat s; | |
189 | Boolean b; | |
190 | ||
191 | stat(filename, &s); | |
192 | b = (Boolean) (modtime != 0 and modtime < s.st_mtime); | |
193 | modtime = s.st_mtime; | |
194 | return b; | |
195 | } | |
196 | ||
197 | /* | |
198 | * Set up what signals we want to trace. | |
199 | */ | |
200 | ||
201 | private setsigtrace() | |
202 | { | |
203 | register Integer i; | |
204 | register Process p; | |
205 | ||
206 | p = process; | |
207 | for (i = 1; i <= NSIG; i++) { | |
208 | psigtrace(p, i, true); | |
209 | } | |
210 | psigtrace(p, SIGHUP, false); | |
211 | psigtrace(p, SIGKILL, false); | |
212 | psigtrace(p, SIGALRM, false); | |
213 | psigtrace(p, SIGTSTP, false); | |
214 | psigtrace(p, SIGCONT, false); | |
215 | psigtrace(p, SIGCHLD, false); | |
216 | } | |
217 | ||
218 | /* | |
219 | * Initialize the argument list. | |
220 | */ | |
221 | ||
222 | public arginit() | |
223 | { | |
224 | infile = nil; | |
225 | outfile = nil; | |
226 | argv[0] = objname; | |
227 | argc = 1; | |
228 | } | |
229 | ||
230 | /* | |
231 | * Add an argument to the list for the debuggee. | |
232 | */ | |
233 | ||
234 | public newarg(arg) | |
235 | String arg; | |
236 | { | |
237 | if (argc >= MAXNCMDARGS) { | |
238 | error("too many arguments"); | |
239 | } | |
240 | argv[argc++] = arg; | |
241 | } | |
242 | ||
243 | /* | |
244 | * Set the standard input for the debuggee. | |
245 | */ | |
246 | ||
247 | public inarg(filename) | |
248 | String filename; | |
249 | { | |
250 | if (infile != nil) { | |
251 | error("multiple input redirects"); | |
252 | } | |
253 | infile = filename; | |
254 | } | |
255 | ||
256 | /* | |
257 | * Set the standard output for the debuggee. | |
258 | * Probably should check to avoid overwriting an existing file. | |
259 | */ | |
260 | ||
261 | public outarg(filename) | |
262 | String filename; | |
263 | { | |
264 | if (outfile != nil) { | |
265 | error("multiple output redirect"); | |
266 | } | |
267 | outfile = filename; | |
268 | } | |
269 | ||
270 | /* | |
271 | * Start debuggee executing. | |
272 | */ | |
273 | ||
274 | public run() | |
275 | { | |
276 | process->status = STOPPED; | |
277 | fixbps(); | |
278 | curline = 0; | |
279 | start(argv, infile, outfile); | |
280 | just_started = true; | |
281 | isstopped = false; | |
282 | cont(); | |
283 | } | |
284 | ||
285 | /* | |
286 | * Continue execution wherever we left off. | |
287 | * | |
288 | * Note that this routine never returns. Eventually bpact() will fail | |
289 | * and we'll call printstatus or step will call it. | |
290 | */ | |
291 | ||
292 | typedef int Intfunc(); | |
293 | ||
294 | private Intfunc *dbintr; | |
295 | private intr(); | |
296 | ||
297 | #define succeeds == true | |
298 | #define fails == false | |
299 | ||
300 | public cont() | |
301 | { | |
302 | dbintr = signal(SIGINT, intr); | |
303 | if (just_started) { | |
304 | just_started = false; | |
305 | } else { | |
306 | if (not isstopped) { | |
307 | error("can't continue execution"); | |
308 | } | |
309 | isstopped = false; | |
310 | step(); | |
311 | } | |
312 | for (;;) { | |
313 | if (single_stepping) { | |
314 | printnews(); | |
315 | } else { | |
316 | setallbps(); | |
317 | resume(); | |
318 | unsetallbps(); | |
319 | if (bpact() fails) { | |
320 | printstatus(); | |
321 | } | |
322 | } | |
323 | step(); | |
324 | } | |
325 | /* NOTREACHED */ | |
326 | } | |
327 | ||
328 | /* | |
329 | * This routine is called if we get an interrupt while "running" px | |
330 | * but actually in the debugger. Could happen, for example, while | |
331 | * processing breakpoints. | |
332 | * | |
333 | * We basically just want to keep going; the assumption is | |
334 | * that when the process resumes it will get the interrupt | |
335 | * which will then be handled. | |
336 | */ | |
337 | ||
338 | private intr() | |
339 | { | |
340 | signal(SIGINT, intr); | |
341 | } | |
342 | ||
343 | public fixintr() | |
344 | { | |
345 | signal(SIGINT, dbintr); | |
346 | } | |
347 | ||
348 | /* | |
349 | * Resume execution. | |
350 | */ | |
351 | ||
352 | public resume() | |
353 | { | |
354 | register Process p; | |
355 | ||
356 | p = process; | |
357 | if (traceexec) { | |
358 | printf("execution resumes at pc 0x%x\n", process->reg[PROGCTR]); | |
359 | fflush(stdout); | |
360 | } | |
361 | pcont(p); | |
362 | pc = process->reg[PROGCTR]; | |
363 | if (traceexec) { | |
364 | printf("execution stops at pc 0x%x on sig %d\n", | |
365 | process->reg[PROGCTR], p->signo); | |
366 | fflush(stdout); | |
367 | } | |
368 | } | |
369 | ||
370 | /* | |
371 | * Continue execution up to the next source line. | |
372 | * | |
373 | * There are two ways to define the next source line depending on what | |
374 | * is desired when a procedure or function call is encountered. Step | |
375 | * stops at the beginning of the procedure or call; next skips over it. | |
376 | */ | |
377 | ||
378 | /* | |
379 | * Stepc is what is called when the step command is given. | |
380 | * It has to play with the "isstopped" information. | |
381 | */ | |
382 | ||
383 | public stepc() | |
384 | { | |
385 | if (not isstopped) { | |
386 | error("can't continue execution"); | |
387 | } | |
388 | isstopped = false; | |
389 | dostep(false); | |
390 | isstopped = true; | |
391 | } | |
392 | ||
393 | public next() | |
394 | { | |
395 | if (not isstopped) { | |
396 | error("can't continue execution"); | |
397 | } | |
398 | isstopped = false; | |
399 | dostep(true); | |
400 | isstopped = true; | |
401 | } | |
402 | ||
403 | public step() | |
404 | { | |
405 | dostep(false); | |
406 | } | |
407 | ||
408 | /* | |
409 | * Resume execution up to the given address. It is assumed that | |
410 | * no breakpoints exist between the current address and the one | |
411 | * we're stepping to. This saves us from setting all the breakpoints. | |
412 | */ | |
413 | ||
414 | public stepto(addr) | |
415 | Address addr; | |
416 | { | |
417 | setbp(addr); | |
418 | resume(); | |
419 | unsetbp(addr); | |
420 | if (not isbperr()) { | |
421 | printstatus(); | |
422 | } | |
423 | } | |
424 | ||
425 | /* | |
426 | * Print the status of the process. | |
427 | * This routine does not return. | |
428 | */ | |
429 | ||
430 | public printstatus() | |
431 | { | |
432 | curfunc = whatblock(pc); | |
433 | if (process->signo == SIGINT) { | |
434 | isstopped = true; | |
435 | printerror(); | |
436 | } | |
437 | if (isbperr() and isstopped) { | |
438 | printf("stopped "); | |
439 | getsrcpos(); | |
440 | if (curline > 0) { | |
441 | printsrcpos(); | |
442 | putchar('\n'); | |
443 | printlines(curline, curline); | |
444 | } else { | |
445 | printf("in "); | |
446 | printwhich(stdout, curfunc); | |
447 | printf(" at 0x%x\n", pc); | |
448 | printinst(pc, pc); | |
449 | } | |
450 | erecover(); | |
451 | } else { | |
452 | fixbps(); | |
453 | fixintr(); | |
454 | if (process->status == FINISHED) { | |
455 | exit(0); | |
456 | } else { | |
457 | isstopped = true; | |
458 | printerror(); | |
459 | } | |
460 | } | |
461 | } | |
462 | ||
463 | /* | |
464 | * Some functions for testing the state of the process. | |
465 | */ | |
466 | ||
467 | public Boolean notstarted(p) | |
468 | Process p; | |
469 | { | |
470 | return (Boolean) (p->status == NOTSTARTED); | |
471 | } | |
472 | ||
473 | public Boolean isfinished(p) | |
474 | Process p; | |
475 | { | |
476 | return (Boolean) (p->status == FINISHED); | |
477 | } | |
478 | ||
479 | /* | |
480 | * Return the signal number which stopped the process. | |
481 | */ | |
482 | ||
483 | public Integer errnum(p) | |
484 | Process p; | |
485 | { | |
486 | return p->signo; | |
487 | } | |
488 | ||
489 | /* | |
490 | * Return the termination code of the process. | |
491 | */ | |
492 | ||
493 | public Integer exitcode(p) | |
494 | Process p; | |
495 | { | |
496 | return p->exitval; | |
497 | } | |
498 | ||
499 | /* | |
500 | * These routines are used to access the debuggee process from | |
501 | * outside this module. | |
502 | * | |
503 | * They invoke "pio" which eventually leads to a call to "ptrace". | |
504 | * The system generates an I/O error when a ptrace fails, we catch | |
505 | * that here and assume its due to a misguided address. | |
506 | */ | |
507 | ||
508 | extern Intfunc *onsyserr(); | |
509 | ||
510 | private badaddr; | |
511 | private rwerr(); | |
512 | ||
513 | /* | |
514 | * Read from the process' instruction area. | |
515 | */ | |
516 | ||
517 | public iread(buff, addr, nbytes) | |
518 | char *buff; | |
519 | Address addr; | |
520 | int nbytes; | |
521 | { | |
522 | Intfunc *f; | |
523 | ||
524 | f = onsyserr(EIO, rwerr); | |
525 | badaddr = addr; | |
526 | if (coredump) { | |
527 | coredump_readtext(buff, addr, nbytes); | |
528 | } else { | |
529 | pio(process, PREAD, TEXTSEG, buff, addr, nbytes); | |
530 | } | |
531 | onsyserr(EIO, f); | |
532 | } | |
533 | ||
534 | /* | |
535 | * Write to the process' instruction area, usually in order to set | |
536 | * or unset a breakpoint. | |
537 | */ | |
538 | ||
539 | public iwrite(buff, addr, nbytes) | |
540 | char *buff; | |
541 | Address addr; | |
542 | int nbytes; | |
543 | { | |
544 | Intfunc *f; | |
545 | ||
546 | if (coredump) { | |
547 | error("no process to write to"); | |
548 | } | |
549 | f = onsyserr(EIO, rwerr); | |
550 | badaddr = addr; | |
551 | pio(process, PWRITE, TEXTSEG, buff, addr, nbytes); | |
552 | onsyserr(EIO, f); | |
553 | } | |
554 | ||
555 | /* | |
556 | * Read for the process' data area. | |
557 | */ | |
558 | ||
559 | public dread(buff, addr, nbytes) | |
560 | char *buff; | |
561 | Address addr; | |
562 | int nbytes; | |
563 | { | |
564 | Intfunc *f; | |
565 | ||
566 | f = onsyserr(EIO, rwerr); | |
567 | badaddr = addr; | |
568 | if (coredump) { | |
569 | coredump_readdata(buff, addr, nbytes); | |
570 | } else { | |
571 | pio(process, PREAD, DATASEG, buff, addr, nbytes); | |
572 | } | |
573 | onsyserr(EIO, f); | |
574 | } | |
575 | ||
576 | /* | |
577 | * Write to the process' data area. | |
578 | */ | |
579 | ||
580 | public dwrite(buff, addr, nbytes) | |
581 | char *buff; | |
582 | Address addr; | |
583 | int nbytes; | |
584 | { | |
585 | Intfunc *f; | |
586 | ||
587 | if (coredump) { | |
588 | error("no process to write to"); | |
589 | } | |
590 | f = onsyserr(EIO, rwerr); | |
591 | badaddr = addr; | |
592 | pio(process, PWRITE, DATASEG, buff, addr, nbytes); | |
593 | onsyserr(EIO, f); | |
594 | } | |
595 | ||
596 | /* | |
597 | * Error handler. | |
598 | */ | |
599 | ||
600 | private rwerr() | |
601 | { | |
602 | error("bad read/write process address 0x%x", badaddr); | |
603 | } | |
604 | ||
605 | /* | |
606 | * Ptrace interface. | |
607 | */ | |
608 | ||
609 | /* | |
610 | * This magic macro enables us to look at the process' registers | |
611 | * in its user structure. Very gross. | |
612 | */ | |
613 | ||
614 | #define regloc(reg) (ctob(UPAGES) + ( sizeof(int) * (reg) )) | |
615 | ||
616 | #define WMASK (~(sizeof(Word) - 1)) | |
617 | #define cachehash(addr) ((unsigned) ((addr >> 2) % CSIZE)) | |
618 | ||
619 | #define FIRSTSIG SIGINT | |
620 | #define LASTSIG SIGQUIT | |
621 | #define ischild(pid) ((pid) == 0) | |
622 | #define traceme() ptrace(0, 0, 0, 0) | |
623 | #define setrep(n) (1 << ((n)-1)) | |
624 | #define istraced(p) (p->sigset&setrep(p->signo)) | |
625 | ||
626 | /* | |
627 | * Ptrace options (specified in first argument). | |
628 | */ | |
629 | ||
630 | #define UREAD 3 /* read from process's user structure */ | |
631 | #define UWRITE 6 /* write to process's user structure */ | |
632 | #define IREAD 1 /* read from process's instruction space */ | |
633 | #define IWRITE 4 /* write to process's instruction space */ | |
634 | #define DREAD 2 /* read from process's data space */ | |
635 | #define DWRITE 5 /* write to process's data space */ | |
636 | #define CONT 7 /* continue stopped process */ | |
637 | #define SSTEP 9 /* continue for approximately one instruction */ | |
638 | #define PKILL 8 /* terminate the process */ | |
639 | ||
640 | /* | |
641 | * Start up a new process by forking and exec-ing the | |
642 | * given argument list, returning when the process is loaded | |
643 | * and ready to execute. The PROCESS information (pointed to | |
644 | * by the first argument) is appropriately filled. | |
645 | * | |
646 | * If the given PROCESS structure is associated with an already running | |
647 | * process, we terminate it. | |
648 | */ | |
649 | ||
650 | /* VARARGS2 */ | |
651 | private pstart(p, argv, infile, outfile) | |
652 | Process p; | |
653 | String argv[]; | |
654 | String infile; | |
655 | String outfile; | |
656 | { | |
657 | int status; | |
658 | File in, out; | |
659 | ||
660 | if (p->pid != 0) { /* child already running? */ | |
661 | ptrace(PKILL, p->pid, 0, 0); /* ... kill it! */ | |
662 | } | |
663 | psigtrace(p, SIGTRAP, true); | |
664 | if ((p->pid = fork()) == -1) { | |
665 | panic("can't fork"); | |
666 | } | |
667 | if (ischild(p->pid)) { | |
668 | traceme(); | |
669 | if (infile != nil) { | |
670 | in = fopen(infile, "r"); | |
671 | if (in == nil) { | |
672 | printf("can't read %s\n", infile); | |
673 | exit(1); | |
674 | } | |
675 | fswap(0, fileno(in)); | |
676 | } | |
677 | if (outfile != nil) { | |
678 | out = fopen(outfile, "w"); | |
679 | if (out == nil) { | |
680 | printf("can't write %s\n", outfile); | |
681 | exit(1); | |
682 | } | |
683 | fswap(1, fileno(out)); | |
684 | } | |
685 | execvp(argv[0], argv); | |
686 | panic("can't exec %s", argv[0]); | |
687 | } | |
688 | pwait(p->pid, &status); | |
689 | getinfo(p, status); | |
690 | if (p->status != STOPPED) { | |
691 | error("program could not begin execution"); | |
692 | } | |
693 | } | |
694 | ||
695 | /* | |
696 | * Continue a stopped process. The argument points to a PROCESS structure. | |
697 | * Before the process is restarted it's user area is modified according to | |
698 | * the values in the structure. When this routine finishes, | |
699 | * the structure has the new values from the process's user area. | |
700 | * | |
701 | * Pcont terminates when the process stops with a signal pending that | |
702 | * is being traced (via psigtrace), or when the process terminates. | |
703 | */ | |
704 | ||
705 | private pcont(p) | |
706 | Process p; | |
707 | { | |
708 | int status; | |
709 | ||
710 | if (p->pid == 0) { | |
711 | error("program not active"); | |
712 | } | |
713 | do { | |
714 | setinfo(p); | |
715 | sigs_off(); | |
716 | if (ptrace(CONT, p->pid, p->reg[PROGCTR], p->signo) < 0) { | |
717 | panic("can't continue process"); | |
718 | } | |
719 | pwait(p->pid, &status); | |
720 | sigs_on(); | |
721 | getinfo(p, status); | |
722 | } while (p->status == STOPPED and not istraced(p)); | |
723 | } | |
724 | ||
725 | /* | |
726 | * Single step as best ptrace can. | |
727 | */ | |
728 | ||
729 | public pstep(p) | |
730 | Process p; | |
731 | { | |
732 | int status; | |
733 | ||
734 | setinfo(p); | |
735 | sigs_off(); | |
736 | ptrace(SSTEP, p->pid, p->reg[PROGCTR], p->signo); | |
737 | pwait(p->pid, &status); | |
738 | sigs_on(); | |
739 | getinfo(p, status); | |
740 | } | |
741 | ||
742 | /* | |
743 | * Return from execution when the given signal is pending. | |
744 | */ | |
745 | ||
746 | public psigtrace(p, sig, sw) | |
747 | Process p; | |
748 | int sig; | |
749 | Boolean sw; | |
750 | { | |
751 | if (sw) { | |
752 | p->sigset |= setrep(sig); | |
753 | } else { | |
754 | p->sigset &= ~setrep(sig); | |
755 | } | |
756 | } | |
757 | ||
758 | /* | |
759 | * Don't catch any signals. | |
760 | * Particularly useful when letting a process finish uninhibited. | |
761 | */ | |
762 | ||
763 | public unsetsigtraces(p) | |
764 | Process p; | |
765 | { | |
766 | p->sigset = 0; | |
767 | } | |
768 | ||
769 | /* | |
770 | * Turn off attention to signals not being caught. | |
771 | */ | |
772 | ||
773 | private Intfunc *sigfunc[NSIG]; | |
774 | ||
775 | private sigs_off() | |
776 | { | |
777 | register int i; | |
778 | ||
779 | for (i = FIRSTSIG; i < LASTSIG; i++) { | |
780 | if (i != SIGKILL) { | |
781 | sigfunc[i] = signal(i, SIG_IGN); | |
782 | } | |
783 | } | |
784 | } | |
785 | ||
786 | /* | |
787 | * Turn back on attention to signals. | |
788 | */ | |
789 | ||
790 | private sigs_on() | |
791 | { | |
792 | register int i; | |
793 | ||
794 | for (i = FIRSTSIG; i < LASTSIG; i++) { | |
795 | if (i != SIGKILL) { | |
796 | signal(i, sigfunc[i]); | |
797 | } | |
798 | } | |
799 | } | |
800 | ||
801 | /* | |
802 | * Get process information from user area. | |
803 | */ | |
804 | ||
805 | private int rloc[] ={ | |
806 | R0, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, AP, FP, SP, PC | |
807 | }; | |
808 | ||
809 | private getinfo(p, status) | |
810 | register Process p; | |
811 | register int status; | |
812 | { | |
813 | register int i; | |
814 | ||
815 | p->signo = (status&0177); | |
816 | p->exitval = ((status >> 8)&0377); | |
817 | if (p->signo != STOPPED) { | |
818 | p->status = FINISHED; | |
819 | } else { | |
820 | p->status = p->signo; | |
821 | p->signo = p->exitval; | |
822 | p->exitval = 0; | |
823 | p->mask = ptrace(UREAD, p->pid, regloc(PS), 0); | |
824 | for (i = 0; i < NREG; i++) { | |
825 | p->reg[i] = ptrace(UREAD, p->pid, regloc(rloc[i]), 0); | |
826 | p->oreg[i] = p->reg[i]; | |
827 | } | |
828 | } | |
829 | } | |
830 | ||
831 | /* | |
832 | * Set process's user area information from given process structure. | |
833 | */ | |
834 | ||
835 | private setinfo(p) | |
836 | register Process p; | |
837 | { | |
838 | register int i; | |
839 | register int r; | |
840 | ||
841 | if (istraced(p)) { | |
842 | p->signo = 0; | |
843 | } | |
844 | for (i = 0; i < NREG; i++) { | |
845 | if ((r = p->reg[i]) != p->oreg[i]) { | |
846 | ptrace(UWRITE, p->pid, regloc(rloc[i]), r); | |
847 | } | |
848 | } | |
849 | } | |
850 | ||
851 | /* | |
852 | * Structure for reading and writing by words, but dealing with bytes. | |
853 | */ | |
854 | ||
855 | typedef union { | |
856 | Word pword; | |
857 | Byte pbyte[sizeof(Word)]; | |
858 | } Pword; | |
859 | ||
860 | /* | |
861 | * Read (write) from (to) the process' address space. | |
862 | * We must deal with ptrace's inability to look anywhere other | |
863 | * than at a word boundary. | |
864 | */ | |
865 | ||
866 | private Word fetch(); | |
867 | private store(); | |
868 | ||
869 | private pio(p, op, seg, buff, addr, nbytes) | |
870 | Process p; | |
871 | PioOp op; | |
872 | PioSeg seg; | |
873 | char *buff; | |
874 | Address addr; | |
875 | int nbytes; | |
876 | { | |
877 | register int i; | |
878 | register Address newaddr; | |
879 | register char *cp; | |
880 | char *bufend; | |
881 | Pword w; | |
882 | Address wordaddr; | |
883 | int byteoff; | |
884 | ||
885 | if (p->status != STOPPED) { | |
886 | error("program is not active"); | |
887 | } | |
888 | cp = buff; | |
889 | newaddr = addr; | |
890 | wordaddr = (newaddr&WMASK); | |
891 | if (wordaddr != newaddr) { | |
892 | w.pword = fetch(p, seg, wordaddr); | |
893 | for (i = newaddr - wordaddr; i < sizeof(Word) and nbytes > 0; i++) { | |
894 | if (op == PREAD) { | |
895 | *cp++ = w.pbyte[i]; | |
896 | } else { | |
897 | w.pbyte[i] = *cp++; | |
898 | } | |
899 | nbytes--; | |
900 | } | |
901 | if (op == PWRITE) { | |
902 | store(p, seg, wordaddr, w.pword); | |
903 | } | |
904 | newaddr = wordaddr + sizeof(Word); | |
905 | } | |
906 | byteoff = (nbytes&(~WMASK)); | |
907 | nbytes -= byteoff; | |
908 | bufend = cp + nbytes; | |
909 | while (cp < bufend) { | |
910 | if (op == PREAD) { | |
911 | *((Word *) cp) = fetch(p, seg, newaddr); | |
912 | } else { | |
913 | store(p, seg, newaddr, *((Word *) cp)); | |
914 | } | |
915 | cp += sizeof(Word); | |
916 | newaddr += sizeof(Word); | |
917 | } | |
918 | if (byteoff > 0) { | |
919 | w.pword = fetch(p, seg, newaddr); | |
920 | for (i = 0; i < byteoff; i++) { | |
921 | if (op == PREAD) { | |
922 | *cp++ = w.pbyte[i]; | |
923 | } else { | |
924 | w.pbyte[i] = *cp++; | |
925 | } | |
926 | } | |
927 | if (op == PWRITE) { | |
928 | store(p, seg, newaddr, w.pword); | |
929 | } | |
930 | } | |
931 | } | |
932 | ||
933 | /* | |
934 | * Get a word from a process at the given address. | |
935 | * The address is assumed to be on a word boundary. | |
936 | * | |
937 | * A simple cache scheme is used to avoid redundant ptrace calls | |
938 | * to the instruction space since it is assumed to be pure. | |
939 | * | |
940 | * It is necessary to use a write-through scheme so that | |
941 | * breakpoints right next to each other don't interfere. | |
942 | */ | |
943 | ||
944 | private Integer nfetchs, nreads, nwrites; | |
945 | ||
946 | private Word fetch(p, seg, addr) | |
947 | Process p; | |
948 | PioSeg seg; | |
949 | register int addr; | |
950 | { | |
951 | register CacheWord *wp; | |
952 | register Word w; | |
953 | ||
954 | switch (seg) { | |
955 | case TEXTSEG: | |
956 | ++nfetchs; | |
957 | wp = &p->word[cachehash(addr)]; | |
958 | if (addr == 0 or wp->addr != addr) { | |
959 | ++nreads; | |
960 | w = ptrace(IREAD, p->pid, addr, 0); | |
961 | wp->addr = addr; | |
962 | wp->val = w; | |
963 | } else { | |
964 | w = wp->val; | |
965 | } | |
966 | break; | |
967 | ||
968 | case DATASEG: | |
969 | w = ptrace(DREAD, p->pid, addr, 0); | |
970 | break; | |
971 | ||
972 | default: | |
973 | panic("fetch: bad seg %d", seg); | |
974 | /* NOTREACHED */ | |
975 | } | |
976 | return w; | |
977 | } | |
978 | ||
979 | /* | |
980 | * Put a word into the process' address space at the given address. | |
981 | * The address is assumed to be on a word boundary. | |
982 | */ | |
983 | ||
984 | private store(p, seg, addr, data) | |
985 | Process p; | |
986 | PioSeg seg; | |
987 | int addr; | |
988 | Word data; | |
989 | { | |
990 | register CacheWord *wp; | |
991 | ||
992 | switch (seg) { | |
993 | case TEXTSEG: | |
994 | ++nwrites; | |
995 | wp = &p->word[cachehash(addr)]; | |
996 | wp->addr = addr; | |
997 | wp->val = data; | |
998 | ptrace(IWRITE, p->pid, addr, data); | |
999 | break; | |
1000 | ||
1001 | case DATASEG: | |
1002 | ptrace(DWRITE, p->pid, addr, data); | |
1003 | break; | |
1004 | ||
1005 | default: | |
1006 | panic("store: bad seg %d", seg); | |
1007 | /* NOTREACHED */ | |
1008 | } | |
1009 | } | |
1010 | ||
1011 | public printptraceinfo() | |
1012 | { | |
1013 | printf("%d fetchs, %d reads, %d writes\n", nfetchs, nreads, nwrites); | |
1014 | } | |
1015 | ||
1016 | /* | |
1017 | * Swap file numbers so as to redirect standard input and output. | |
1018 | */ | |
1019 | ||
1020 | private fswap(oldfd, newfd) | |
1021 | int oldfd; | |
1022 | int newfd; | |
1023 | { | |
1024 | if (oldfd != newfd) { | |
1025 | close(oldfd); | |
1026 | dup(newfd); | |
1027 | close(newfd); | |
1028 | } | |
1029 | } |