Commit | Line | Data |
---|---|---|
35f5886e FF |
1 | /* Machine independent support for SVR4 /proc (process file system) for GDB. |
2 | Copyright (C) 1991 Free Software Foundation, Inc. | |
3 | Written by Fred Fish at Cygnus Support. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | ||
22 | /* N O T E S | |
23 | ||
24 | For information on the details of using /proc consult section proc(4) | |
25 | in the UNIX System V Release 4 System Administrator's Reference Manual. | |
26 | ||
27 | The general register and floating point register sets are manipulated by | |
28 | separate ioctl's. This file makes the assumption that if FP0_REGNUM is | |
29 | defined, then support for the floating point register set is desired, | |
30 | regardless of whether or not the actual target has floating point hardware. | |
31 | ||
32 | */ | |
33 | ||
34 | ||
35 | ||
5129100c | 36 | #include "defs.h" |
35f5886e FF |
37 | |
38 | #ifdef USE_PROC_FS /* Entire file goes away if not using /proc */ | |
39 | ||
40 | #include <stdio.h> | |
41 | #include <sys/procfs.h> | |
42 | #include <fcntl.h> | |
43 | #include <errno.h> | |
44 | ||
35f5886e FF |
45 | #include "ansidecl.h" |
46 | #include "inferior.h" | |
47 | #include "target.h" | |
48 | ||
49 | #ifndef PROC_NAME_FMT | |
50 | #define PROC_NAME_FMT "/proc/%d" | |
51 | #endif | |
52 | ||
53 | extern void EXFUN(supply_gregset, (gregset_t *gregsetp)); | |
54 | extern void EXFUN(fill_gregset, (gregset_t *gresetp, int regno)); | |
55 | ||
56 | #if defined (FP0_REGNUM) | |
57 | extern void EXFUN(supply_fpregset, (fpregset_t *fpregsetp)); | |
58 | extern void EXFUN(fill_fpregset, (fpregset_t *fpresetp, int regno)); | |
59 | #endif | |
60 | ||
61 | #if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */ | |
62 | CORE_ADDR kernel_u_addr; | |
63 | #endif | |
64 | ||
65 | /* All access to the inferior, either one started by gdb or one that has | |
66 | been attached to, is controlled by an instance of a procinfo structure, | |
67 | defined below. Since gdb currently only handles one inferior at a time, | |
68 | the procinfo structure is statically allocated and only one exists at | |
69 | any given time. */ | |
70 | ||
71 | struct procinfo { | |
72 | int valid; /* Nonzero if pid, fd, & pathname are valid */ | |
73 | int pid; /* Process ID of inferior */ | |
74 | int fd; /* File descriptor for /proc entry */ | |
75 | char *pathname; /* Pathname to /proc entry */ | |
76 | int was_stopped; /* Nonzero if was stopped prior to attach */ | |
77 | prrun_t prrun; /* Control state when it is run */ | |
78 | prstatus_t prstatus; /* Current process status info */ | |
79 | gregset_t gregset; /* General register set */ | |
80 | fpregset_t fpregset; /* Floating point register set */ | |
81 | fltset_t fltset; /* Current traced hardware fault set */ | |
82 | sigset_t trace; /* Current traced signal set */ | |
83 | sysset_t exitset; /* Current traced system call exit set */ | |
84 | sysset_t entryset; /* Current traced system call entry set */ | |
85 | } pi; | |
86 | ||
87 | /* Forward declarations of static functions so we don't have to worry | |
88 | about ordering within this file. The EXFUN macro may be slightly | |
89 | misleading. Should probably be called DCLFUN instead, or something | |
90 | more intuitive, since it can be used for both static and external | |
91 | definitions. */ | |
92 | ||
93 | static void EXFUN(proc_init_failed, (char *why)); | |
94 | static int EXFUN(open_proc_file, (int pid)); | |
95 | static void EXFUN(close_proc_file, (void)); | |
96 | static void EXFUN(unconditionally_kill_inferior, (void)); | |
97 | ||
98 | /* | |
99 | ||
100 | GLOBAL FUNCTION | |
101 | ||
102 | ptrace -- override library version to force errors for /proc version | |
103 | ||
104 | SYNOPSIS | |
105 | ||
106 | int ptrace (int request, int pid, int arg3, int arg4) | |
107 | ||
108 | DESCRIPTION | |
109 | ||
110 | When gdb is configured to use /proc, it should not be calling | |
111 | or otherwise attempting to use ptrace. In order to catch errors | |
112 | where use of /proc is configured, but some routine is still calling | |
113 | ptrace, we provide a local version of a function with that name | |
114 | that does nothing but issue an error message. | |
115 | */ | |
116 | ||
117 | int | |
118 | DEFUN(ptrace, (request, pid, arg3, arg4), | |
119 | int request AND | |
120 | int pid AND | |
121 | int arg3 AND | |
122 | int arg4) | |
123 | { | |
124 | error ("internal error - there is a call to ptrace() somewhere"); | |
125 | /*NOTREACHED*/ | |
126 | } | |
127 | ||
128 | /* | |
129 | ||
130 | GLOBAL FUNCTION | |
131 | ||
132 | kill_inferior_fast -- kill inferior while gdb is exiting | |
133 | ||
134 | SYNOPSIS | |
135 | ||
136 | void kill_inferior_fast (void) | |
137 | ||
138 | DESCRIPTION | |
139 | ||
140 | This is used when GDB is exiting. It gives less chance of error. | |
141 | ||
142 | NOTES | |
143 | ||
144 | Don't attempt to kill attached inferiors since we may be called | |
145 | when gdb is in the process of aborting, and killing the attached | |
146 | inferior may be very anti-social. This is particularly true if we | |
147 | were attached just so we could use the /proc facilities to get | |
148 | detailed information about it's status. | |
149 | ||
150 | */ | |
151 | ||
152 | void | |
153 | DEFUN_VOID(kill_inferior_fast) | |
154 | { | |
155 | if (inferior_pid != 0 && !attach_flag) | |
156 | { | |
157 | unconditionally_kill_inferior (); | |
158 | } | |
159 | } | |
160 | ||
161 | /* | |
162 | ||
163 | GLOBAL FUNCTION | |
164 | ||
165 | kill_inferior - kill any currently inferior | |
166 | ||
167 | SYNOPSIS | |
168 | ||
169 | void kill_inferior (void) | |
170 | ||
171 | DESCRIPTION | |
172 | ||
173 | Kill any current inferior. | |
174 | ||
175 | NOTES | |
176 | ||
177 | Kills even attached inferiors. Presumably the user has already | |
178 | been prompted that the inferior is an attached one rather than | |
179 | one started by gdb. (FIXME?) | |
180 | ||
181 | */ | |
182 | ||
183 | void | |
184 | DEFUN_VOID(kill_inferior) | |
185 | { | |
186 | if (inferior_pid != 0) | |
187 | { | |
188 | unconditionally_kill_inferior (); | |
189 | target_mourn_inferior (); | |
190 | } | |
191 | } | |
192 | ||
193 | /* | |
194 | ||
195 | LOCAL FUNCTION | |
196 | ||
197 | unconditionally_kill_inferior - terminate the inferior | |
198 | ||
199 | SYNOPSIS | |
200 | ||
201 | static void unconditionally_kill_inferior (void) | |
202 | ||
203 | DESCRIPTION | |
204 | ||
205 | Kill the current inferior. Should not be called until it | |
206 | is at least tested that there is an inferior. | |
207 | ||
208 | NOTE | |
209 | ||
210 | A possibly useful enhancement would be to first try sending | |
211 | the inferior a terminate signal, politely asking it to commit | |
212 | suicide, before we murder it. | |
213 | ||
214 | */ | |
215 | ||
216 | static void | |
217 | DEFUN_VOID(unconditionally_kill_inferior) | |
218 | { | |
219 | int signo; | |
220 | ||
221 | signo = SIGKILL; | |
222 | (void) ioctl (pi.fd, PIOCKILL, &signo); | |
223 | close_proc_file (); | |
224 | wait ((int *) 0); | |
225 | } | |
226 | ||
227 | /* | |
228 | ||
229 | GLOBAL FUNCTION | |
230 | ||
231 | child_xfer_memory -- copy data to or from inferior memory space | |
232 | ||
233 | SYNOPSIS | |
234 | ||
235 | int child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, | |
236 | int dowrite, struct target_ops target) | |
237 | ||
238 | DESCRIPTION | |
239 | ||
240 | Copy LEN bytes to/from inferior's memory starting at MEMADDR | |
241 | from/to debugger memory starting at MYADDR. Copy from inferior | |
242 | if DOWRITE is zero or to inferior if DOWRITE is nonzero. | |
243 | ||
244 | Returns the length copied, which is either the LEN argument or | |
245 | zero. This xfer function does not do partial moves, since child_ops | |
246 | doesn't allow memory operations to cross below us in the target stack | |
247 | anyway. | |
248 | ||
249 | NOTES | |
250 | ||
251 | The /proc interface makes this an almost trivial task. | |
252 | */ | |
253 | ||
254 | ||
255 | int | |
256 | DEFUN(child_xfer_memory, (memaddr, myaddr, len, dowrite, target), | |
257 | CORE_ADDR memaddr AND | |
258 | char *myaddr AND | |
259 | int len AND | |
260 | int dowrite AND | |
261 | struct target_ops target /* ignored */) | |
262 | { | |
263 | int nbytes = 0; | |
264 | ||
265 | if (lseek (pi.fd, (off_t) memaddr, 0) == (off_t) memaddr) | |
266 | { | |
267 | if (dowrite) | |
268 | { | |
269 | nbytes = write (pi.fd, myaddr, len); | |
270 | } | |
271 | else | |
272 | { | |
273 | nbytes = read (pi.fd, myaddr, len); | |
274 | } | |
275 | if (nbytes < 0) | |
276 | { | |
277 | nbytes = 0; | |
278 | } | |
279 | } | |
280 | return (nbytes); | |
281 | } | |
282 | ||
283 | /* | |
284 | ||
285 | GLOBAL FUNCTION | |
286 | ||
287 | store_inferior_registers -- copy register values back to inferior | |
288 | ||
289 | SYNOPSIS | |
290 | ||
291 | void store_inferior_registers (int regno) | |
292 | ||
293 | DESCRIPTION | |
294 | ||
295 | Store our current register values back into the inferior. If | |
296 | REGNO is -1 then store all the register, otherwise store just | |
297 | the value specified by REGNO. | |
298 | ||
299 | NOTES | |
300 | ||
301 | If we are storing only a single register, we first have to get all | |
302 | the current values from the process, overwrite the desired register | |
303 | in the gregset with the one we want from gdb's registers, and then | |
304 | send the whole set back to the process. For writing all the | |
305 | registers, all we have to do is generate the gregset and send it to | |
306 | the process. | |
307 | ||
308 | Also note that the process has to be stopped on an event of interest | |
309 | for this to work, which basically means that it has to have been | |
310 | run under the control of one of the other /proc ioctl calls and not | |
311 | ptrace. Since we don't use ptrace anyway, we don't worry about this | |
312 | fine point, but it is worth noting for future reference. | |
313 | ||
314 | Gdb is confused about what this function is supposed to return. | |
315 | Some versions return a value, others return nothing. Some are | |
316 | declared to return a value and actually return nothing. Gdb ignores | |
317 | anything returned. (FIXME) | |
318 | ||
319 | */ | |
320 | ||
321 | void | |
322 | DEFUN(store_inferior_registers, (regno), | |
323 | int regno) | |
324 | { | |
325 | if (regno != -1) | |
326 | { | |
327 | (void) ioctl (pi.fd, PIOCGREG, &pi.gregset); | |
328 | } | |
329 | fill_gregset (&pi.gregset, regno); | |
330 | (void) ioctl (pi.fd, PIOCSREG, &pi.gregset); | |
331 | ||
332 | #if defined (FP0_REGNUM) | |
333 | ||
334 | /* Now repeat everything using the floating point register set, if the | |
335 | target has floating point hardware. Since we ignore the returned value, | |
336 | we'll never know whether it worked or not anyway. */ | |
337 | ||
338 | if (regno != -1) | |
339 | { | |
340 | (void) ioctl (pi.fd, PIOCGFPREG, &pi.fpregset); | |
341 | } | |
342 | fill_fpregset (&pi.fpregset, regno); | |
343 | (void) ioctl (pi.fd, PIOCSFPREG, &pi.fpregset); | |
344 | ||
345 | #endif /* FP0_REGNUM */ | |
346 | ||
347 | } | |
348 | ||
349 | /* | |
350 | ||
351 | GLOBAL FUNCTION | |
352 | ||
353 | inferior_proc_init - initialize access to a /proc entry | |
354 | ||
355 | SYNOPSIS | |
356 | ||
357 | void inferior_proc_init (int pid) | |
358 | ||
359 | DESCRIPTION | |
360 | ||
361 | When gdb starts an inferior, this function is called in the parent | |
362 | process immediately after the fork. It waits for the child to stop | |
363 | on the return from the exec system call (the child itself takes care | |
364 | of ensuring that this is set up), then sets up the set of signals | |
365 | and faults that are to be traced. | |
366 | ||
367 | NOTES | |
368 | ||
369 | If proc_init_failed ever gets called, control returns to the command | |
370 | processing loop via the standard error handling code. | |
371 | */ | |
372 | ||
373 | void | |
374 | DEFUN(inferior_proc_init, (int pid), | |
375 | int pid) | |
376 | { | |
377 | if (!open_proc_file (pid)) | |
378 | { | |
379 | proc_init_failed ("can't open process file"); | |
380 | } | |
381 | else | |
382 | { | |
383 | (void) memset (&pi.prrun, 0, sizeof (pi.prrun)); | |
384 | prfillset (&pi.prrun.pr_trace); | |
385 | prfillset (&pi.prrun.pr_fault); | |
386 | prdelset (&pi.prrun.pr_fault, FLTPAGE); | |
387 | if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0) | |
388 | { | |
389 | proc_init_failed ("PIOCWSTOP failed"); | |
390 | } | |
391 | else if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace) < 0) | |
392 | { | |
393 | proc_init_failed ("PIOCSTRACE failed"); | |
394 | } | |
395 | else if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault) < 0) | |
396 | { | |
397 | proc_init_failed ("PIOCSFAULT failed"); | |
398 | } | |
399 | } | |
400 | } | |
401 | ||
402 | /* | |
403 | ||
404 | GLOBAL FUNCTION | |
405 | ||
406 | proc_set_exec_trap -- arrange for exec'd child to halt at startup | |
407 | ||
408 | SYNOPSIS | |
409 | ||
410 | void proc_set_exec_trap (void) | |
411 | ||
412 | DESCRIPTION | |
413 | ||
414 | This function is called in the child process when starting up | |
415 | an inferior, prior to doing the exec of the actual inferior. | |
416 | It sets the child process's exitset to make exit from the exec | |
417 | system call an event of interest to stop on, and then simply | |
418 | returns. The child does the exec, the system call returns, and | |
419 | the child stops at the first instruction, ready for the gdb | |
420 | parent process to take control of it. | |
421 | ||
422 | NOTE | |
423 | ||
424 | We need to use all local variables since the child may be sharing | |
425 | it's data space with the parent, if vfork was used rather than | |
426 | fork. | |
427 | */ | |
428 | ||
429 | void | |
430 | DEFUN_VOID(proc_set_exec_trap) | |
431 | { | |
432 | sysset_t exitset; | |
433 | auto char procname[32]; | |
434 | int fd; | |
435 | ||
436 | (void) sprintf (procname, PROC_NAME_FMT, getpid ()); | |
437 | if ((fd = open (procname, O_RDWR)) < 0) | |
438 | { | |
439 | perror (procname); | |
440 | fflush (stderr); | |
441 | _exit (127); | |
442 | } | |
443 | premptyset (&exitset); | |
444 | praddset (&exitset, SYS_exec); | |
445 | praddset (&exitset, SYS_execve); | |
446 | if (ioctl (fd, PIOCSEXIT, &exitset) < 0) | |
447 | { | |
448 | perror (procname); | |
449 | fflush (stderr); | |
450 | _exit (127); | |
451 | } | |
452 | } | |
453 | ||
454 | ||
455 | #ifdef ATTACH_DETACH | |
456 | ||
457 | /* | |
458 | ||
459 | GLOBAL FUNCTION | |
460 | ||
461 | attach -- attach to an already existing process | |
462 | ||
463 | SYNOPSIS | |
464 | ||
465 | int attach (int pid) | |
466 | ||
467 | DESCRIPTION | |
468 | ||
469 | Attach to an already existing process with the specified process | |
470 | id. If the process is not already stopped, query whether to | |
471 | stop it or not. | |
472 | ||
473 | NOTES | |
474 | ||
475 | The option of stopping at attach time is specific to the /proc | |
476 | versions of gdb. Versions using ptrace force the attachee | |
477 | to stop. | |
478 | ||
479 | */ | |
480 | ||
481 | int | |
482 | DEFUN(attach, (pid), | |
483 | int pid) | |
484 | { | |
485 | if (!open_proc_file (pid)) | |
486 | { | |
487 | perror_with_name (pi.pathname); | |
488 | /* NOTREACHED */ | |
489 | } | |
490 | ||
491 | /* Get current status of process and if it is not already stopped, | |
492 | then stop it. Remember whether or not it was stopped when we first | |
493 | examined it. */ | |
494 | ||
495 | if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0) | |
496 | { | |
497 | print_sys_errmsg (pi.pathname, errno); | |
498 | close_proc_file (); | |
499 | error ("PIOCSTATUS failed"); | |
500 | } | |
501 | if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)) | |
502 | { | |
503 | pi.was_stopped = 1; | |
504 | } | |
505 | else | |
506 | { | |
507 | pi.was_stopped = 0; | |
508 | if (query ("Process is currently running, stop it? ")) | |
509 | { | |
510 | if (ioctl (pi.fd, PIOCSTOP, &pi.prstatus) < 0) | |
511 | { | |
512 | print_sys_errmsg (pi.pathname, errno); | |
513 | close_proc_file (); | |
514 | error ("PIOCSTOP failed"); | |
515 | } | |
516 | } | |
517 | } | |
518 | ||
519 | /* Remember some things about the inferior that we will, or might, change | |
520 | so that we can restore them when we detach. */ | |
521 | ||
522 | (void) ioctl (pi.fd, PIOCGTRACE, &pi.trace); | |
523 | (void) ioctl (pi.fd, PIOCGFAULT, &pi.fltset); | |
524 | (void) ioctl (pi.fd, PIOCGENTRY, &pi.entryset); | |
525 | (void) ioctl (pi.fd, PIOCGEXIT, &pi.exitset); | |
526 | ||
527 | /* Set up trace and fault sets, as gdb expects them. */ | |
528 | ||
529 | (void) memset (&pi.prrun, 0, sizeof (pi.prrun)); | |
530 | prfillset (&pi.prrun.pr_trace); | |
531 | prfillset (&pi.prrun.pr_fault); | |
532 | prdelset (&pi.prrun.pr_fault, FLTPAGE); | |
533 | if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault)) | |
534 | { | |
535 | print_sys_errmsg ("PIOCSFAULT failed"); | |
536 | } | |
537 | if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace)) | |
538 | { | |
539 | print_sys_errmsg ("PIOCSTRACE failed"); | |
540 | } | |
541 | attach_flag = 1; | |
542 | return (pid); | |
543 | } | |
544 | ||
545 | /* | |
546 | ||
547 | GLOBAL FUNCTION | |
548 | ||
549 | detach -- detach from an attached-to process | |
550 | ||
551 | SYNOPSIS | |
552 | ||
553 | void detach (int signal) | |
554 | ||
555 | DESCRIPTION | |
556 | ||
557 | Detach from the current attachee. | |
558 | ||
559 | If signal is non-zero, the attachee is started running again and sent | |
560 | the specified signal. | |
561 | ||
562 | If signal is zero and the attachee was not already stopped when we | |
563 | attached to it, then we make it runnable again when we detach. | |
564 | ||
565 | Otherwise, we query whether or not to make the attachee runnable | |
566 | again, since we may simply want to leave it in the state it was in | |
567 | when we attached. | |
568 | ||
569 | We report any problems, but do not consider them errors, since we | |
570 | MUST detach even if some things don't seem to go right. This may not | |
571 | be the ideal situation. (FIXME). | |
572 | */ | |
573 | ||
574 | void | |
575 | DEFUN(detach, (signal), | |
576 | int signal) | |
577 | { | |
578 | if (signal) | |
579 | { | |
580 | struct siginfo siginfo; | |
581 | siginfo.si_signo = signal; | |
582 | siginfo.si_code = 0; | |
583 | siginfo.si_errno = 0; | |
584 | if (ioctl (pi.fd, PIOCSSIG, &siginfo) < 0) | |
585 | { | |
586 | print_sys_errmsg (pi.pathname, errno); | |
587 | printf ("PIOCSSIG failed.\n"); | |
588 | } | |
589 | } | |
590 | if (ioctl (pi.fd, PIOCSEXIT, &pi.exitset) < 0) | |
591 | { | |
592 | print_sys_errmsg (pi.pathname, errno); | |
593 | printf ("PIOCSEXIT failed.\n"); | |
594 | } | |
595 | if (ioctl (pi.fd, PIOCSENTRY, &pi.entryset) < 0) | |
596 | { | |
597 | print_sys_errmsg (pi.pathname, errno); | |
598 | printf ("PIOCSENTRY failed.\n"); | |
599 | } | |
600 | if (ioctl (pi.fd, PIOCSTRACE, &pi.trace) < 0) | |
601 | { | |
602 | print_sys_errmsg (pi.pathname, errno); | |
603 | printf ("PIOCSTRACE failed.\n"); | |
604 | } | |
605 | if (ioctl (pi.fd, PIOCSFAULT, &pi.fltset) < 0) | |
606 | { | |
607 | print_sys_errmsg (pi.pathname, errno); | |
608 | printf ("PIOCSFAULT failed.\n"); | |
609 | } | |
610 | if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0) | |
611 | { | |
612 | print_sys_errmsg (pi.pathname, errno); | |
613 | printf ("PIOCSTATUS failed.\n"); | |
614 | } | |
615 | else | |
616 | { | |
617 | if (signal || (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))) | |
618 | { | |
619 | if (signal || !pi.was_stopped || | |
620 | query ("Was stopped when attached, make it runnable again? ")) | |
621 | { | |
622 | (void) memset (&pi.prrun, 0, sizeof (pi.prrun)); | |
623 | pi.prrun.pr_flags = PRCFAULT; | |
624 | if (ioctl (pi.fd, PIOCRUN, &pi.prrun)) | |
625 | { | |
626 | print_sys_errmsg (pi.pathname, errno); | |
627 | printf ("PIOCRUN failed.\n"); | |
628 | } | |
629 | } | |
630 | } | |
631 | } | |
632 | close_proc_file (); | |
633 | attach_flag = 0; | |
634 | } | |
635 | ||
fb182850 FF |
636 | #endif /* ATTACH_DETACH */ |
637 | ||
35f5886e FF |
638 | /* |
639 | ||
640 | GLOBAL FUNCTION | |
641 | ||
642 | proc_wait -- emulate wait() as much as possible | |
643 | ||
644 | SYNOPSIS | |
645 | ||
646 | int proc_wait (int *statloc) | |
647 | ||
648 | DESCRIPTION | |
649 | ||
650 | Try to emulate wait() as much as possible. Not sure why we can't | |
651 | just use wait(), but it seems to have problems when applied to a | |
652 | process being controlled with the /proc interface. | |
653 | ||
654 | NOTES | |
655 | ||
656 | We have a race problem here with no obvious solution. We need to let | |
657 | the inferior run until it stops on an event of interest, which means | |
658 | that we need to use the PIOCWSTOP ioctl. However, we cannot use this | |
659 | ioctl if the process is already stopped on something that is not an | |
660 | event of interest, or the call will hang indefinitely. Thus we first | |
661 | use PIOCSTATUS to see if the process is not stopped. If not, then we | |
662 | use PIOCWSTOP. But during the window between the two, if the process | |
663 | stops for any reason that is not an event of interest (such as a job | |
664 | control signal) then gdb will hang. One possible workaround is to set | |
665 | an alarm to wake up every minute of so and check to see if the process | |
666 | is still running, and if so, then reissue the PIOCWSTOP. But this is | |
667 | a real kludge, so has not been implemented. FIXME: investigate | |
668 | alternatives. | |
669 | ||
670 | FIXME: Investigate why wait() seems to have problems with programs | |
671 | being control by /proc routines. | |
672 | ||
673 | */ | |
674 | ||
675 | int | |
676 | DEFUN(proc_wait, (statloc), | |
677 | int *statloc) | |
678 | { | |
679 | short what; | |
680 | short why; | |
681 | int statval = 0; | |
682 | int checkerr = 0; | |
683 | int rtnval = -1; | |
684 | ||
685 | if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0) | |
686 | { | |
687 | checkerr++; | |
688 | } | |
689 | else if (!(pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))) | |
690 | { | |
691 | if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0) | |
692 | { | |
693 | checkerr++; | |
694 | } | |
695 | } | |
696 | if (checkerr) | |
697 | { | |
698 | if (errno == ENOENT) | |
699 | { | |
700 | rtnval = wait (&statval); | |
701 | if (rtnval != inferior_pid) | |
702 | { | |
703 | error ("PIOCWSTOP, wait failed, returned %d", rtnval); | |
704 | /* NOTREACHED */ | |
705 | } | |
706 | } | |
707 | else | |
708 | { | |
709 | print_sys_errmsg (pi.pathname, errno); | |
710 | error ("PIOCSTATUS or PIOCWSTOP failed."); | |
711 | /* NOTREACHED */ | |
712 | } | |
713 | } | |
714 | else if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)) | |
715 | { | |
716 | rtnval = pi.prstatus.pr_pid; | |
717 | why = pi.prstatus.pr_why; | |
718 | what = pi.prstatus.pr_what; | |
719 | if (why == PR_SIGNALLED) | |
720 | { | |
721 | statval = (what << 8) | 0177; | |
722 | } | |
723 | else if ((why == PR_SYSEXIT) && | |
724 | (what == SYS_exec || what == SYS_execve)) | |
725 | { | |
726 | statval = (SIGTRAP << 8) | 0177; | |
727 | } | |
728 | else if (why == PR_REQUESTED) | |
729 | { | |
730 | statval = (SIGSTOP << 8) | 0177; | |
731 | } | |
732 | else if (why == PR_JOBCONTROL) | |
733 | { | |
734 | statval = (what << 8) | 0177; | |
735 | } | |
736 | else if (why == PR_FAULTED) | |
737 | { | |
738 | switch (what) | |
739 | { | |
740 | case FLTPRIV: | |
741 | case FLTILL: | |
742 | statval = (SIGILL << 8) | 0177; | |
743 | break; | |
744 | case FLTBPT: | |
745 | case FLTTRACE: | |
746 | statval = (SIGTRAP << 8) | 0177; | |
747 | break; | |
748 | case FLTSTACK: | |
749 | case FLTACCESS: | |
750 | case FLTBOUNDS: | |
751 | statval = (SIGSEGV << 8) | 0177; | |
752 | break; | |
753 | case FLTIOVF: | |
754 | case FLTIZDIV: | |
755 | case FLTFPE: | |
756 | statval = (SIGFPE << 8) | 0177; | |
757 | break; | |
758 | case FLTPAGE: /* Recoverable page fault */ | |
759 | default: | |
760 | rtnval = -1; | |
761 | error ("PIOCWSTOP, unknown why %d, what %d", why, what); | |
762 | /* NOTREACHED */ | |
763 | } | |
764 | } | |
765 | else | |
766 | { | |
767 | rtnval = -1; | |
768 | error ("PIOCWSTOP, unknown why %d, what %d", why, what); | |
769 | /* NOTREACHED */ | |
770 | } | |
771 | } | |
772 | else | |
773 | { | |
774 | error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x", | |
775 | pi.prstatus.pr_flags); | |
776 | /* NOTREACHED */ | |
777 | } | |
778 | if (statloc) | |
779 | { | |
780 | *statloc = statval; | |
781 | } | |
782 | return (rtnval); | |
783 | } | |
784 | ||
785 | /* | |
786 | ||
787 | GLOBAL FUNCTION | |
788 | ||
789 | child_resume -- resume execution of the inferior process | |
790 | ||
791 | SYNOPSIS | |
792 | ||
793 | void child_resume (int step, int signal) | |
794 | ||
795 | DESCRIPTION | |
796 | ||
797 | Resume execution of the inferior process. If STEP is nozero, then | |
798 | just single step it. If SIGNAL is nonzero, restart it with that | |
799 | signal activated. | |
800 | ||
801 | NOTE | |
802 | ||
803 | It may not be absolutely necessary to specify the PC value for | |
804 | restarting, but to be safe we use the value that gdb considers | |
805 | to be current. One case where this might be necessary is if the | |
806 | user explicitly changes the PC value that gdb considers to be | |
807 | current. FIXME: Investigate if this is necessary or not. | |
808 | */ | |
809 | ||
810 | void | |
811 | DEFUN(child_resume, (step, signal), | |
812 | int step AND | |
813 | int signal) | |
814 | { | |
815 | errno = 0; | |
816 | pi.prrun.pr_flags = PRSVADDR | PRSTRACE | PRSFAULT | PRCFAULT; | |
817 | pi.prrun.pr_vaddr = (caddr_t) *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)]; | |
818 | if (signal) | |
819 | { | |
820 | if (signal != pi.prstatus.pr_cursig) | |
821 | { | |
822 | struct siginfo siginfo; | |
823 | siginfo.si_signo = signal; | |
824 | siginfo.si_code = 0; | |
825 | siginfo.si_errno = 0; | |
826 | (void) ioctl (pi.fd, PIOCSSIG, &siginfo); | |
827 | } | |
828 | } | |
829 | else | |
830 | { | |
831 | pi.prrun.pr_flags |= PRCSIG; | |
832 | } | |
833 | if (step) | |
834 | { | |
835 | pi.prrun.pr_flags |= PRSTEP; | |
836 | } | |
837 | if (ioctl (pi.fd, PIOCRUN, &pi.prrun) != 0) | |
838 | { | |
839 | perror_with_name (pi.pathname); | |
840 | /* NOTREACHED */ | |
841 | } | |
842 | } | |
843 | ||
844 | /* | |
845 | ||
846 | GLOBAL FUNCTION | |
847 | ||
848 | fetch_inferior_registers -- fetch current registers from inferior | |
849 | ||
850 | SYNOPSIS | |
851 | ||
852 | void fetch_inferior_registers (void) | |
853 | ||
854 | DESCRIPTION | |
855 | ||
856 | Read the current values of the inferior's registers, both the | |
857 | general register set and floating point registers (if supported) | |
858 | and update gdb's idea of their current values. | |
859 | ||
860 | */ | |
861 | ||
862 | void | |
863 | DEFUN_VOID(fetch_inferior_registers) | |
864 | { | |
865 | if (ioctl (pi.fd, PIOCGREG, &pi.gregset) != -1) | |
866 | { | |
867 | supply_gregset (&pi.gregset); | |
868 | } | |
869 | #if defined (FP0_REGNUM) | |
870 | if (ioctl (pi.fd, PIOCGFPREG, &pi.fpregset) != -1) | |
871 | { | |
872 | supply_fpregset (&pi.fpregset); | |
873 | } | |
874 | #endif | |
875 | } | |
876 | ||
fb182850 FF |
877 | /* |
878 | ||
879 | GLOBAL FUNCTION | |
880 | ||
881 | fetch_core_registers -- fetch current registers from core file data | |
882 | ||
883 | SYNOPSIS | |
884 | ||
885 | void fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, | |
886 | int which) | |
887 | ||
888 | DESCRIPTION | |
889 | ||
890 | Read the values of either the general register set (WHICH equals 0) | |
891 | or the floating point register set (WHICH equals 2) from the core | |
892 | file data (pointed to by CORE_REG_SECT), and update gdb's idea of | |
893 | their current values. The CORE_REG_SIZE parameter is ignored. | |
894 | ||
895 | NOTES | |
896 | ||
897 | Use the indicated sizes to validate the gregset and fpregset | |
898 | structures. | |
899 | */ | |
900 | ||
901 | void | |
902 | fetch_core_registers (core_reg_sect, core_reg_size, which) | |
903 | char *core_reg_sect; | |
904 | unsigned core_reg_size; | |
905 | int which; | |
906 | { | |
907 | ||
908 | if (which == 0) | |
909 | { | |
910 | if (core_reg_size != sizeof (pi.gregset)) | |
911 | { | |
912 | warning ("wrong size gregset struct in core file"); | |
913 | } | |
914 | else | |
915 | { | |
916 | (void) memcpy ((char *) &pi.gregset, core_reg_sect, | |
917 | sizeof (pi.gregset)); | |
918 | supply_gregset (&pi.gregset); | |
919 | } | |
920 | } | |
921 | else if (which == 2) | |
922 | { | |
923 | if (core_reg_size != sizeof (pi.fpregset)) | |
924 | { | |
925 | warning ("wrong size fpregset struct in core file"); | |
926 | } | |
927 | else | |
928 | { | |
929 | (void) memcpy ((char *) &pi.fpregset, core_reg_sect, | |
930 | sizeof (pi.fpregset)); | |
931 | #if defined (FP0_REGNUM) | |
932 | supply_fpregset (&pi.fpregset); | |
933 | #endif | |
934 | } | |
935 | } | |
936 | } | |
35f5886e FF |
937 | |
938 | /* | |
939 | ||
940 | LOCAL FUNCTION | |
941 | ||
942 | proc_init_failed - called whenever /proc access initialization fails | |
943 | ||
944 | SYNOPSIS | |
945 | ||
946 | static void proc_init_failed (char *why) | |
947 | ||
948 | DESCRIPTION | |
949 | ||
950 | This function is called whenever initialization of access to a /proc | |
951 | entry fails. It prints a suitable error message, does some cleanup, | |
952 | and then invokes the standard error processing routine which dumps | |
953 | us back into the command loop. | |
954 | */ | |
955 | ||
956 | static void | |
957 | DEFUN(proc_init_failed, (why), | |
958 | char *why) | |
959 | { | |
960 | print_sys_errmsg (pi.pathname, errno); | |
961 | (void) kill (pi.pid, SIGKILL); | |
962 | close_proc_file (); | |
963 | error (why); | |
964 | /* NOTREACHED */ | |
965 | } | |
966 | ||
967 | /* | |
968 | ||
969 | LOCAL FUNCTION | |
970 | ||
971 | close_proc_file - close any currently open /proc entry | |
972 | ||
973 | SYNOPSIS | |
974 | ||
975 | static void close_proc_file (void) | |
976 | ||
977 | DESCRIPTION | |
978 | ||
979 | Close any currently open /proc entry and mark the process information | |
980 | entry as invalid. In order to ensure that we don't try to reuse any | |
981 | stale information, the pid, fd, and pathnames are explicitly | |
982 | invalidated, which may be overkill. | |
983 | ||
984 | */ | |
985 | ||
986 | static void | |
987 | DEFUN_VOID(close_proc_file) | |
988 | { | |
989 | pi.pid = 0; | |
990 | if (pi.valid) | |
991 | { | |
992 | (void) close (pi.fd); | |
993 | } | |
994 | pi.fd = -1; | |
995 | if (pi.pathname) | |
996 | { | |
997 | free (pi.pathname); | |
998 | pi.pathname = NULL; | |
999 | } | |
1000 | pi.valid = 0; | |
1001 | } | |
1002 | ||
1003 | /* | |
1004 | ||
1005 | LOCAL FUNCTION | |
1006 | ||
1007 | open_proc_file - open a /proc entry for a given process id | |
1008 | ||
1009 | SYNOPSIS | |
1010 | ||
1011 | static int open_proc_file (pid) | |
1012 | ||
1013 | DESCRIPTION | |
1014 | ||
1015 | Given a process id, close the existing open /proc entry (if any) | |
1016 | and open one for the new process id. Once it is open, then | |
1017 | mark the local process information structure as valid, which | |
1018 | guarantees that the pid, fd, and pathname fields match an open | |
1019 | /proc entry. Returns zero if the open fails, nonzero otherwise. | |
1020 | ||
1021 | Note that the pathname is left intact, even when the open fails, | |
1022 | so that callers can use it to construct meaningful error messages | |
1023 | rather than just "file open failed". | |
1024 | */ | |
1025 | ||
1026 | static int | |
1027 | DEFUN(open_proc_file, (pid), | |
1028 | int pid) | |
1029 | { | |
1030 | pi.valid = 0; | |
1031 | if (pi.valid) | |
1032 | { | |
1033 | (void) close (pi.fd); | |
1034 | } | |
1035 | if (pi.pathname == NULL) | |
1036 | { | |
1037 | pi.pathname = xmalloc (32); | |
1038 | } | |
1039 | sprintf (pi.pathname, PROC_NAME_FMT, pid); | |
1040 | if ((pi.fd = open (pi.pathname, O_RDWR)) >= 0) | |
1041 | { | |
1042 | pi.valid = 1; | |
1043 | pi.pid = pid; | |
1044 | } | |
1045 | return (pi.valid); | |
1046 | } | |
1047 | ||
1048 | #endif /* USE_PROC_FS */ |