Commit | Line | Data |
---|---|---|
9a3bab7a ML |
1 | /* Copyright (c) 1982 Regents of the University of California */ |
2 | ||
b0edae1c | 3 | static char sccsid[] = "@(#)process.c 1.3 %G%"; |
9a3bab7a ML |
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> | |
b0edae1c | 26 | #include <machine/reg.h> |
9a3bab7a ML |
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 | { | |
b0edae1c ML |
432 | if (process->status == FINISHED) { |
433 | exit(0); | |
9a3bab7a | 434 | } else { |
b0edae1c ML |
435 | curfunc = whatblock(pc); |
436 | getsrcpos(); | |
437 | if (process->signo == SIGINT) { | |
438 | isstopped = true; | |
439 | printerror(); | |
440 | } else if (isbperr() and isstopped) { | |
441 | printf("stopped "); | |
442 | if (curline > 0) { | |
443 | printsrcpos(); | |
444 | putchar('\n'); | |
445 | printlines(curline, curline); | |
446 | } else { | |
447 | printf("in "); | |
448 | printwhich(stdout, curfunc); | |
449 | printf(" at 0x%x\n", pc); | |
450 | printinst(pc, pc); | |
451 | } | |
452 | erecover(); | |
9a3bab7a | 453 | } else { |
b0edae1c ML |
454 | fixbps(); |
455 | fixintr(); | |
9a3bab7a ML |
456 | isstopped = true; |
457 | printerror(); | |
458 | } | |
459 | } | |
460 | } | |
461 | ||
462 | /* | |
463 | * Some functions for testing the state of the process. | |
464 | */ | |
465 | ||
466 | public Boolean notstarted(p) | |
467 | Process p; | |
468 | { | |
469 | return (Boolean) (p->status == NOTSTARTED); | |
470 | } | |
471 | ||
472 | public Boolean isfinished(p) | |
473 | Process p; | |
474 | { | |
475 | return (Boolean) (p->status == FINISHED); | |
476 | } | |
477 | ||
478 | /* | |
479 | * Return the signal number which stopped the process. | |
480 | */ | |
481 | ||
482 | public Integer errnum(p) | |
483 | Process p; | |
484 | { | |
485 | return p->signo; | |
486 | } | |
487 | ||
488 | /* | |
489 | * Return the termination code of the process. | |
490 | */ | |
491 | ||
492 | public Integer exitcode(p) | |
493 | Process p; | |
494 | { | |
495 | return p->exitval; | |
496 | } | |
497 | ||
498 | /* | |
499 | * These routines are used to access the debuggee process from | |
500 | * outside this module. | |
501 | * | |
502 | * They invoke "pio" which eventually leads to a call to "ptrace". | |
503 | * The system generates an I/O error when a ptrace fails, we catch | |
504 | * that here and assume its due to a misguided address. | |
505 | */ | |
506 | ||
507 | extern Intfunc *onsyserr(); | |
508 | ||
509 | private badaddr; | |
510 | private rwerr(); | |
511 | ||
512 | /* | |
513 | * Read from the process' instruction area. | |
514 | */ | |
515 | ||
516 | public iread(buff, addr, nbytes) | |
517 | char *buff; | |
518 | Address addr; | |
519 | int nbytes; | |
520 | { | |
521 | Intfunc *f; | |
522 | ||
523 | f = onsyserr(EIO, rwerr); | |
524 | badaddr = addr; | |
525 | if (coredump) { | |
526 | coredump_readtext(buff, addr, nbytes); | |
527 | } else { | |
528 | pio(process, PREAD, TEXTSEG, buff, addr, nbytes); | |
529 | } | |
530 | onsyserr(EIO, f); | |
531 | } | |
532 | ||
533 | /* | |
534 | * Write to the process' instruction area, usually in order to set | |
535 | * or unset a breakpoint. | |
536 | */ | |
537 | ||
538 | public iwrite(buff, addr, nbytes) | |
539 | char *buff; | |
540 | Address addr; | |
541 | int nbytes; | |
542 | { | |
543 | Intfunc *f; | |
544 | ||
545 | if (coredump) { | |
546 | error("no process to write to"); | |
547 | } | |
548 | f = onsyserr(EIO, rwerr); | |
549 | badaddr = addr; | |
550 | pio(process, PWRITE, TEXTSEG, buff, addr, nbytes); | |
551 | onsyserr(EIO, f); | |
552 | } | |
553 | ||
554 | /* | |
555 | * Read for the process' data area. | |
556 | */ | |
557 | ||
558 | public dread(buff, addr, nbytes) | |
559 | char *buff; | |
560 | Address addr; | |
561 | int nbytes; | |
562 | { | |
563 | Intfunc *f; | |
564 | ||
565 | f = onsyserr(EIO, rwerr); | |
566 | badaddr = addr; | |
567 | if (coredump) { | |
568 | coredump_readdata(buff, addr, nbytes); | |
569 | } else { | |
570 | pio(process, PREAD, DATASEG, buff, addr, nbytes); | |
571 | } | |
572 | onsyserr(EIO, f); | |
573 | } | |
574 | ||
575 | /* | |
576 | * Write to the process' data area. | |
577 | */ | |
578 | ||
579 | public dwrite(buff, addr, nbytes) | |
580 | char *buff; | |
581 | Address addr; | |
582 | int nbytes; | |
583 | { | |
584 | Intfunc *f; | |
585 | ||
586 | if (coredump) { | |
587 | error("no process to write to"); | |
588 | } | |
589 | f = onsyserr(EIO, rwerr); | |
590 | badaddr = addr; | |
591 | pio(process, PWRITE, DATASEG, buff, addr, nbytes); | |
592 | onsyserr(EIO, f); | |
593 | } | |
594 | ||
595 | /* | |
596 | * Error handler. | |
597 | */ | |
598 | ||
599 | private rwerr() | |
600 | { | |
601 | error("bad read/write process address 0x%x", badaddr); | |
602 | } | |
603 | ||
604 | /* | |
605 | * Ptrace interface. | |
606 | */ | |
607 | ||
608 | /* | |
609 | * This magic macro enables us to look at the process' registers | |
610 | * in its user structure. Very gross. | |
611 | */ | |
612 | ||
613 | #define regloc(reg) (ctob(UPAGES) + ( sizeof(int) * (reg) )) | |
614 | ||
615 | #define WMASK (~(sizeof(Word) - 1)) | |
616 | #define cachehash(addr) ((unsigned) ((addr >> 2) % CSIZE)) | |
617 | ||
618 | #define FIRSTSIG SIGINT | |
619 | #define LASTSIG SIGQUIT | |
620 | #define ischild(pid) ((pid) == 0) | |
621 | #define traceme() ptrace(0, 0, 0, 0) | |
622 | #define setrep(n) (1 << ((n)-1)) | |
623 | #define istraced(p) (p->sigset&setrep(p->signo)) | |
624 | ||
625 | /* | |
626 | * Ptrace options (specified in first argument). | |
627 | */ | |
628 | ||
629 | #define UREAD 3 /* read from process's user structure */ | |
630 | #define UWRITE 6 /* write to process's user structure */ | |
631 | #define IREAD 1 /* read from process's instruction space */ | |
632 | #define IWRITE 4 /* write to process's instruction space */ | |
633 | #define DREAD 2 /* read from process's data space */ | |
634 | #define DWRITE 5 /* write to process's data space */ | |
635 | #define CONT 7 /* continue stopped process */ | |
636 | #define SSTEP 9 /* continue for approximately one instruction */ | |
637 | #define PKILL 8 /* terminate the process */ | |
638 | ||
639 | /* | |
640 | * Start up a new process by forking and exec-ing the | |
641 | * given argument list, returning when the process is loaded | |
642 | * and ready to execute. The PROCESS information (pointed to | |
643 | * by the first argument) is appropriately filled. | |
644 | * | |
645 | * If the given PROCESS structure is associated with an already running | |
646 | * process, we terminate it. | |
647 | */ | |
648 | ||
649 | /* VARARGS2 */ | |
650 | private pstart(p, argv, infile, outfile) | |
651 | Process p; | |
652 | String argv[]; | |
653 | String infile; | |
654 | String outfile; | |
655 | { | |
656 | int status; | |
657 | File in, out; | |
658 | ||
659 | if (p->pid != 0) { /* child already running? */ | |
660 | ptrace(PKILL, p->pid, 0, 0); /* ... kill it! */ | |
661 | } | |
662 | psigtrace(p, SIGTRAP, true); | |
663 | if ((p->pid = fork()) == -1) { | |
664 | panic("can't fork"); | |
665 | } | |
666 | if (ischild(p->pid)) { | |
667 | traceme(); | |
668 | if (infile != nil) { | |
669 | in = fopen(infile, "r"); | |
670 | if (in == nil) { | |
671 | printf("can't read %s\n", infile); | |
672 | exit(1); | |
673 | } | |
674 | fswap(0, fileno(in)); | |
675 | } | |
676 | if (outfile != nil) { | |
677 | out = fopen(outfile, "w"); | |
678 | if (out == nil) { | |
679 | printf("can't write %s\n", outfile); | |
680 | exit(1); | |
681 | } | |
682 | fswap(1, fileno(out)); | |
683 | } | |
684 | execvp(argv[0], argv); | |
685 | panic("can't exec %s", argv[0]); | |
686 | } | |
687 | pwait(p->pid, &status); | |
688 | getinfo(p, status); | |
689 | if (p->status != STOPPED) { | |
690 | error("program could not begin execution"); | |
691 | } | |
692 | } | |
693 | ||
694 | /* | |
695 | * Continue a stopped process. The argument points to a PROCESS structure. | |
696 | * Before the process is restarted it's user area is modified according to | |
697 | * the values in the structure. When this routine finishes, | |
698 | * the structure has the new values from the process's user area. | |
699 | * | |
700 | * Pcont terminates when the process stops with a signal pending that | |
701 | * is being traced (via psigtrace), or when the process terminates. | |
702 | */ | |
703 | ||
704 | private pcont(p) | |
705 | Process p; | |
706 | { | |
707 | int status; | |
708 | ||
709 | if (p->pid == 0) { | |
710 | error("program not active"); | |
711 | } | |
712 | do { | |
713 | setinfo(p); | |
714 | sigs_off(); | |
715 | if (ptrace(CONT, p->pid, p->reg[PROGCTR], p->signo) < 0) { | |
716 | panic("can't continue process"); | |
717 | } | |
718 | pwait(p->pid, &status); | |
719 | sigs_on(); | |
720 | getinfo(p, status); | |
721 | } while (p->status == STOPPED and not istraced(p)); | |
722 | } | |
723 | ||
724 | /* | |
725 | * Single step as best ptrace can. | |
726 | */ | |
727 | ||
728 | public pstep(p) | |
729 | Process p; | |
730 | { | |
731 | int status; | |
732 | ||
733 | setinfo(p); | |
734 | sigs_off(); | |
735 | ptrace(SSTEP, p->pid, p->reg[PROGCTR], p->signo); | |
736 | pwait(p->pid, &status); | |
737 | sigs_on(); | |
738 | getinfo(p, status); | |
739 | } | |
740 | ||
741 | /* | |
742 | * Return from execution when the given signal is pending. | |
743 | */ | |
744 | ||
745 | public psigtrace(p, sig, sw) | |
746 | Process p; | |
747 | int sig; | |
748 | Boolean sw; | |
749 | { | |
750 | if (sw) { | |
751 | p->sigset |= setrep(sig); | |
752 | } else { | |
753 | p->sigset &= ~setrep(sig); | |
754 | } | |
755 | } | |
756 | ||
757 | /* | |
758 | * Don't catch any signals. | |
759 | * Particularly useful when letting a process finish uninhibited. | |
760 | */ | |
761 | ||
762 | public unsetsigtraces(p) | |
763 | Process p; | |
764 | { | |
765 | p->sigset = 0; | |
766 | } | |
767 | ||
768 | /* | |
769 | * Turn off attention to signals not being caught. | |
770 | */ | |
771 | ||
772 | private Intfunc *sigfunc[NSIG]; | |
773 | ||
774 | private sigs_off() | |
775 | { | |
776 | register int i; | |
777 | ||
778 | for (i = FIRSTSIG; i < LASTSIG; i++) { | |
779 | if (i != SIGKILL) { | |
780 | sigfunc[i] = signal(i, SIG_IGN); | |
781 | } | |
782 | } | |
783 | } | |
784 | ||
785 | /* | |
786 | * Turn back on attention to signals. | |
787 | */ | |
788 | ||
789 | private sigs_on() | |
790 | { | |
791 | register int i; | |
792 | ||
793 | for (i = FIRSTSIG; i < LASTSIG; i++) { | |
794 | if (i != SIGKILL) { | |
795 | signal(i, sigfunc[i]); | |
796 | } | |
797 | } | |
798 | } | |
799 | ||
800 | /* | |
801 | * Get process information from user area. | |
802 | */ | |
803 | ||
804 | private int rloc[] ={ | |
805 | R0, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, AP, FP, SP, PC | |
806 | }; | |
807 | ||
808 | private getinfo(p, status) | |
809 | register Process p; | |
810 | register int status; | |
811 | { | |
812 | register int i; | |
813 | ||
814 | p->signo = (status&0177); | |
815 | p->exitval = ((status >> 8)&0377); | |
816 | if (p->signo != STOPPED) { | |
817 | p->status = FINISHED; | |
818 | } else { | |
819 | p->status = p->signo; | |
820 | p->signo = p->exitval; | |
821 | p->exitval = 0; | |
822 | p->mask = ptrace(UREAD, p->pid, regloc(PS), 0); | |
823 | for (i = 0; i < NREG; i++) { | |
824 | p->reg[i] = ptrace(UREAD, p->pid, regloc(rloc[i]), 0); | |
825 | p->oreg[i] = p->reg[i]; | |
826 | } | |
827 | } | |
828 | } | |
829 | ||
830 | /* | |
831 | * Set process's user area information from given process structure. | |
832 | */ | |
833 | ||
834 | private setinfo(p) | |
835 | register Process p; | |
836 | { | |
837 | register int i; | |
838 | register int r; | |
839 | ||
840 | if (istraced(p)) { | |
841 | p->signo = 0; | |
842 | } | |
843 | for (i = 0; i < NREG; i++) { | |
844 | if ((r = p->reg[i]) != p->oreg[i]) { | |
845 | ptrace(UWRITE, p->pid, regloc(rloc[i]), r); | |
846 | } | |
847 | } | |
848 | } | |
849 | ||
850 | /* | |
851 | * Structure for reading and writing by words, but dealing with bytes. | |
852 | */ | |
853 | ||
854 | typedef union { | |
855 | Word pword; | |
856 | Byte pbyte[sizeof(Word)]; | |
857 | } Pword; | |
858 | ||
859 | /* | |
860 | * Read (write) from (to) the process' address space. | |
861 | * We must deal with ptrace's inability to look anywhere other | |
862 | * than at a word boundary. | |
863 | */ | |
864 | ||
865 | private Word fetch(); | |
866 | private store(); | |
867 | ||
868 | private pio(p, op, seg, buff, addr, nbytes) | |
869 | Process p; | |
870 | PioOp op; | |
871 | PioSeg seg; | |
872 | char *buff; | |
873 | Address addr; | |
874 | int nbytes; | |
875 | { | |
876 | register int i; | |
877 | register Address newaddr; | |
878 | register char *cp; | |
879 | char *bufend; | |
880 | Pword w; | |
881 | Address wordaddr; | |
882 | int byteoff; | |
883 | ||
884 | if (p->status != STOPPED) { | |
885 | error("program is not active"); | |
886 | } | |
887 | cp = buff; | |
888 | newaddr = addr; | |
889 | wordaddr = (newaddr&WMASK); | |
890 | if (wordaddr != newaddr) { | |
891 | w.pword = fetch(p, seg, wordaddr); | |
892 | for (i = newaddr - wordaddr; i < sizeof(Word) and nbytes > 0; i++) { | |
893 | if (op == PREAD) { | |
894 | *cp++ = w.pbyte[i]; | |
895 | } else { | |
896 | w.pbyte[i] = *cp++; | |
897 | } | |
898 | nbytes--; | |
899 | } | |
900 | if (op == PWRITE) { | |
901 | store(p, seg, wordaddr, w.pword); | |
902 | } | |
903 | newaddr = wordaddr + sizeof(Word); | |
904 | } | |
905 | byteoff = (nbytes&(~WMASK)); | |
906 | nbytes -= byteoff; | |
907 | bufend = cp + nbytes; | |
908 | while (cp < bufend) { | |
909 | if (op == PREAD) { | |
910 | *((Word *) cp) = fetch(p, seg, newaddr); | |
911 | } else { | |
912 | store(p, seg, newaddr, *((Word *) cp)); | |
913 | } | |
914 | cp += sizeof(Word); | |
915 | newaddr += sizeof(Word); | |
916 | } | |
917 | if (byteoff > 0) { | |
918 | w.pword = fetch(p, seg, newaddr); | |
919 | for (i = 0; i < byteoff; i++) { | |
920 | if (op == PREAD) { | |
921 | *cp++ = w.pbyte[i]; | |
922 | } else { | |
923 | w.pbyte[i] = *cp++; | |
924 | } | |
925 | } | |
926 | if (op == PWRITE) { | |
927 | store(p, seg, newaddr, w.pword); | |
928 | } | |
929 | } | |
930 | } | |
931 | ||
932 | /* | |
933 | * Get a word from a process at the given address. | |
934 | * The address is assumed to be on a word boundary. | |
935 | * | |
936 | * A simple cache scheme is used to avoid redundant ptrace calls | |
937 | * to the instruction space since it is assumed to be pure. | |
938 | * | |
939 | * It is necessary to use a write-through scheme so that | |
940 | * breakpoints right next to each other don't interfere. | |
941 | */ | |
942 | ||
943 | private Integer nfetchs, nreads, nwrites; | |
944 | ||
945 | private Word fetch(p, seg, addr) | |
946 | Process p; | |
947 | PioSeg seg; | |
948 | register int addr; | |
949 | { | |
950 | register CacheWord *wp; | |
951 | register Word w; | |
952 | ||
953 | switch (seg) { | |
954 | case TEXTSEG: | |
955 | ++nfetchs; | |
956 | wp = &p->word[cachehash(addr)]; | |
957 | if (addr == 0 or wp->addr != addr) { | |
958 | ++nreads; | |
959 | w = ptrace(IREAD, p->pid, addr, 0); | |
960 | wp->addr = addr; | |
961 | wp->val = w; | |
962 | } else { | |
963 | w = wp->val; | |
964 | } | |
965 | break; | |
966 | ||
967 | case DATASEG: | |
968 | w = ptrace(DREAD, p->pid, addr, 0); | |
969 | break; | |
970 | ||
971 | default: | |
972 | panic("fetch: bad seg %d", seg); | |
973 | /* NOTREACHED */ | |
974 | } | |
975 | return w; | |
976 | } | |
977 | ||
978 | /* | |
979 | * Put a word into the process' address space at the given address. | |
980 | * The address is assumed to be on a word boundary. | |
981 | */ | |
982 | ||
983 | private store(p, seg, addr, data) | |
984 | Process p; | |
985 | PioSeg seg; | |
986 | int addr; | |
987 | Word data; | |
988 | { | |
989 | register CacheWord *wp; | |
990 | ||
991 | switch (seg) { | |
992 | case TEXTSEG: | |
993 | ++nwrites; | |
994 | wp = &p->word[cachehash(addr)]; | |
995 | wp->addr = addr; | |
996 | wp->val = data; | |
997 | ptrace(IWRITE, p->pid, addr, data); | |
998 | break; | |
999 | ||
1000 | case DATASEG: | |
1001 | ptrace(DWRITE, p->pid, addr, data); | |
1002 | break; | |
1003 | ||
1004 | default: | |
1005 | panic("store: bad seg %d", seg); | |
1006 | /* NOTREACHED */ | |
1007 | } | |
1008 | } | |
1009 | ||
1010 | public printptraceinfo() | |
1011 | { | |
1012 | printf("%d fetchs, %d reads, %d writes\n", nfetchs, nreads, nwrites); | |
1013 | } | |
1014 | ||
1015 | /* | |
1016 | * Swap file numbers so as to redirect standard input and output. | |
1017 | */ | |
1018 | ||
1019 | private fswap(oldfd, newfd) | |
1020 | int oldfd; | |
1021 | int newfd; | |
1022 | { | |
1023 | if (oldfd != newfd) { | |
1024 | close(oldfd); | |
1025 | dup(newfd); | |
1026 | close(newfd); | |
1027 | } | |
1028 | } |