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