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 | ||
f8b76e70 FF |
454 | /* |
455 | ||
456 | GLOBAL FUNCTION | |
457 | ||
458 | proc_base_address -- find base address for segment containing address | |
459 | ||
460 | SYNOPSIS | |
461 | ||
462 | CORE_ADDR proc_base_address (CORE_ADDR addr) | |
463 | ||
464 | DESCRIPTION | |
465 | ||
466 | Given an address of a location in the inferior, find and return | |
467 | the base address of the mapped segment containing that address. | |
468 | ||
469 | This is used for example, by the shared library support code, | |
470 | where we have the pc value for some location in the shared library | |
471 | where we are stopped, and need to know the base address of the | |
472 | segment containing that address. | |
473 | */ | |
474 | ||
475 | ||
476 | CORE_ADDR | |
477 | DEFUN(proc_base_address, (addr), | |
478 | CORE_ADDR addr) | |
479 | { | |
480 | int nmap; | |
481 | struct prmap *prmaps; | |
482 | struct prmap *prmap; | |
483 | CORE_ADDR baseaddr = 0; | |
484 | ||
485 | if (ioctl (pi.fd, PIOCNMAP, &nmap) == 0) | |
486 | { | |
487 | prmaps = alloca ((nmap + 1) * sizeof (*prmaps)); | |
488 | if (ioctl (pi.fd, PIOCMAP, prmaps) == 0) | |
489 | { | |
490 | for (prmap = prmaps; prmap -> pr_size; ++prmap) | |
491 | { | |
492 | if ((prmap -> pr_vaddr <= (caddr_t) addr) && | |
493 | (prmap -> pr_vaddr + prmap -> pr_size > (caddr_t) addr)) | |
494 | { | |
495 | baseaddr = (CORE_ADDR) prmap -> pr_vaddr; | |
496 | break; | |
497 | } | |
498 | } | |
499 | } | |
500 | } | |
501 | return (baseaddr); | |
502 | } | |
503 | ||
504 | /* | |
505 | ||
506 | GLOBAL_FUNCTION | |
507 | ||
508 | proc_address_to_fd -- return open fd for file mapped to address | |
509 | ||
510 | SYNOPSIS | |
511 | ||
512 | int proc_address_to_fd (CORE_ADDR addr) | |
513 | ||
514 | DESCRIPTION | |
515 | ||
516 | Given an address in the current inferior's address space, use the | |
517 | /proc interface to find an open file descriptor for the file that | |
518 | this address was mapped in from. Return -1 if there is no current | |
519 | inferior. Print a warning message if there is an inferior but | |
520 | the address corresponds to no file (IE a bogus address). | |
521 | ||
522 | */ | |
523 | ||
524 | int | |
525 | DEFUN(proc_address_to_fd, (addr), | |
526 | CORE_ADDR addr) | |
527 | { | |
528 | int fd = -1; | |
529 | ||
530 | if (pi.valid) | |
531 | { | |
532 | if ((fd = ioctl (pi.fd, PIOCOPENM, (caddr_t *) &addr)) < 0) | |
533 | { | |
534 | print_sys_errmsg (pi.pathname, errno); | |
535 | warning ("can't find mapped file for address 0x%x", addr); | |
536 | } | |
537 | } | |
538 | return (fd); | |
539 | } | |
540 | ||
35f5886e FF |
541 | |
542 | #ifdef ATTACH_DETACH | |
543 | ||
544 | /* | |
545 | ||
546 | GLOBAL FUNCTION | |
547 | ||
548 | attach -- attach to an already existing process | |
549 | ||
550 | SYNOPSIS | |
551 | ||
552 | int attach (int pid) | |
553 | ||
554 | DESCRIPTION | |
555 | ||
556 | Attach to an already existing process with the specified process | |
557 | id. If the process is not already stopped, query whether to | |
558 | stop it or not. | |
559 | ||
560 | NOTES | |
561 | ||
562 | The option of stopping at attach time is specific to the /proc | |
563 | versions of gdb. Versions using ptrace force the attachee | |
564 | to stop. | |
565 | ||
566 | */ | |
567 | ||
568 | int | |
569 | DEFUN(attach, (pid), | |
570 | int pid) | |
571 | { | |
572 | if (!open_proc_file (pid)) | |
573 | { | |
574 | perror_with_name (pi.pathname); | |
575 | /* NOTREACHED */ | |
576 | } | |
577 | ||
578 | /* Get current status of process and if it is not already stopped, | |
579 | then stop it. Remember whether or not it was stopped when we first | |
580 | examined it. */ | |
581 | ||
582 | if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0) | |
583 | { | |
584 | print_sys_errmsg (pi.pathname, errno); | |
585 | close_proc_file (); | |
586 | error ("PIOCSTATUS failed"); | |
587 | } | |
588 | if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)) | |
589 | { | |
590 | pi.was_stopped = 1; | |
591 | } | |
592 | else | |
593 | { | |
594 | pi.was_stopped = 0; | |
595 | if (query ("Process is currently running, stop it? ")) | |
596 | { | |
597 | if (ioctl (pi.fd, PIOCSTOP, &pi.prstatus) < 0) | |
598 | { | |
599 | print_sys_errmsg (pi.pathname, errno); | |
600 | close_proc_file (); | |
601 | error ("PIOCSTOP failed"); | |
602 | } | |
603 | } | |
604 | } | |
605 | ||
606 | /* Remember some things about the inferior that we will, or might, change | |
607 | so that we can restore them when we detach. */ | |
608 | ||
609 | (void) ioctl (pi.fd, PIOCGTRACE, &pi.trace); | |
610 | (void) ioctl (pi.fd, PIOCGFAULT, &pi.fltset); | |
611 | (void) ioctl (pi.fd, PIOCGENTRY, &pi.entryset); | |
612 | (void) ioctl (pi.fd, PIOCGEXIT, &pi.exitset); | |
613 | ||
614 | /* Set up trace and fault sets, as gdb expects them. */ | |
615 | ||
616 | (void) memset (&pi.prrun, 0, sizeof (pi.prrun)); | |
617 | prfillset (&pi.prrun.pr_trace); | |
618 | prfillset (&pi.prrun.pr_fault); | |
619 | prdelset (&pi.prrun.pr_fault, FLTPAGE); | |
620 | if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault)) | |
621 | { | |
622 | print_sys_errmsg ("PIOCSFAULT failed"); | |
623 | } | |
624 | if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace)) | |
625 | { | |
626 | print_sys_errmsg ("PIOCSTRACE failed"); | |
627 | } | |
628 | attach_flag = 1; | |
629 | return (pid); | |
630 | } | |
631 | ||
632 | /* | |
633 | ||
634 | GLOBAL FUNCTION | |
635 | ||
636 | detach -- detach from an attached-to process | |
637 | ||
638 | SYNOPSIS | |
639 | ||
640 | void detach (int signal) | |
641 | ||
642 | DESCRIPTION | |
643 | ||
644 | Detach from the current attachee. | |
645 | ||
646 | If signal is non-zero, the attachee is started running again and sent | |
647 | the specified signal. | |
648 | ||
649 | If signal is zero and the attachee was not already stopped when we | |
650 | attached to it, then we make it runnable again when we detach. | |
651 | ||
652 | Otherwise, we query whether or not to make the attachee runnable | |
653 | again, since we may simply want to leave it in the state it was in | |
654 | when we attached. | |
655 | ||
656 | We report any problems, but do not consider them errors, since we | |
657 | MUST detach even if some things don't seem to go right. This may not | |
658 | be the ideal situation. (FIXME). | |
659 | */ | |
660 | ||
661 | void | |
662 | DEFUN(detach, (signal), | |
663 | int signal) | |
664 | { | |
665 | if (signal) | |
666 | { | |
667 | struct siginfo siginfo; | |
668 | siginfo.si_signo = signal; | |
669 | siginfo.si_code = 0; | |
670 | siginfo.si_errno = 0; | |
671 | if (ioctl (pi.fd, PIOCSSIG, &siginfo) < 0) | |
672 | { | |
673 | print_sys_errmsg (pi.pathname, errno); | |
674 | printf ("PIOCSSIG failed.\n"); | |
675 | } | |
676 | } | |
677 | if (ioctl (pi.fd, PIOCSEXIT, &pi.exitset) < 0) | |
678 | { | |
679 | print_sys_errmsg (pi.pathname, errno); | |
680 | printf ("PIOCSEXIT failed.\n"); | |
681 | } | |
682 | if (ioctl (pi.fd, PIOCSENTRY, &pi.entryset) < 0) | |
683 | { | |
684 | print_sys_errmsg (pi.pathname, errno); | |
685 | printf ("PIOCSENTRY failed.\n"); | |
686 | } | |
687 | if (ioctl (pi.fd, PIOCSTRACE, &pi.trace) < 0) | |
688 | { | |
689 | print_sys_errmsg (pi.pathname, errno); | |
690 | printf ("PIOCSTRACE failed.\n"); | |
691 | } | |
692 | if (ioctl (pi.fd, PIOCSFAULT, &pi.fltset) < 0) | |
693 | { | |
694 | print_sys_errmsg (pi.pathname, errno); | |
695 | printf ("PIOCSFAULT failed.\n"); | |
696 | } | |
697 | if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0) | |
698 | { | |
699 | print_sys_errmsg (pi.pathname, errno); | |
700 | printf ("PIOCSTATUS failed.\n"); | |
701 | } | |
702 | else | |
703 | { | |
704 | if (signal || (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))) | |
705 | { | |
706 | if (signal || !pi.was_stopped || | |
707 | query ("Was stopped when attached, make it runnable again? ")) | |
708 | { | |
709 | (void) memset (&pi.prrun, 0, sizeof (pi.prrun)); | |
710 | pi.prrun.pr_flags = PRCFAULT; | |
711 | if (ioctl (pi.fd, PIOCRUN, &pi.prrun)) | |
712 | { | |
713 | print_sys_errmsg (pi.pathname, errno); | |
714 | printf ("PIOCRUN failed.\n"); | |
715 | } | |
716 | } | |
717 | } | |
718 | } | |
719 | close_proc_file (); | |
720 | attach_flag = 0; | |
721 | } | |
722 | ||
fb182850 FF |
723 | #endif /* ATTACH_DETACH */ |
724 | ||
35f5886e FF |
725 | /* |
726 | ||
727 | GLOBAL FUNCTION | |
728 | ||
729 | proc_wait -- emulate wait() as much as possible | |
730 | ||
731 | SYNOPSIS | |
732 | ||
733 | int proc_wait (int *statloc) | |
734 | ||
735 | DESCRIPTION | |
736 | ||
737 | Try to emulate wait() as much as possible. Not sure why we can't | |
738 | just use wait(), but it seems to have problems when applied to a | |
739 | process being controlled with the /proc interface. | |
740 | ||
741 | NOTES | |
742 | ||
743 | We have a race problem here with no obvious solution. We need to let | |
744 | the inferior run until it stops on an event of interest, which means | |
745 | that we need to use the PIOCWSTOP ioctl. However, we cannot use this | |
746 | ioctl if the process is already stopped on something that is not an | |
747 | event of interest, or the call will hang indefinitely. Thus we first | |
748 | use PIOCSTATUS to see if the process is not stopped. If not, then we | |
749 | use PIOCWSTOP. But during the window between the two, if the process | |
750 | stops for any reason that is not an event of interest (such as a job | |
751 | control signal) then gdb will hang. One possible workaround is to set | |
752 | an alarm to wake up every minute of so and check to see if the process | |
753 | is still running, and if so, then reissue the PIOCWSTOP. But this is | |
754 | a real kludge, so has not been implemented. FIXME: investigate | |
755 | alternatives. | |
756 | ||
757 | FIXME: Investigate why wait() seems to have problems with programs | |
758 | being control by /proc routines. | |
759 | ||
760 | */ | |
761 | ||
762 | int | |
763 | DEFUN(proc_wait, (statloc), | |
764 | int *statloc) | |
765 | { | |
766 | short what; | |
767 | short why; | |
768 | int statval = 0; | |
769 | int checkerr = 0; | |
770 | int rtnval = -1; | |
771 | ||
772 | if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0) | |
773 | { | |
774 | checkerr++; | |
775 | } | |
776 | else if (!(pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))) | |
777 | { | |
778 | if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0) | |
779 | { | |
780 | checkerr++; | |
781 | } | |
782 | } | |
783 | if (checkerr) | |
784 | { | |
785 | if (errno == ENOENT) | |
786 | { | |
787 | rtnval = wait (&statval); | |
788 | if (rtnval != inferior_pid) | |
789 | { | |
790 | error ("PIOCWSTOP, wait failed, returned %d", rtnval); | |
791 | /* NOTREACHED */ | |
792 | } | |
793 | } | |
794 | else | |
795 | { | |
796 | print_sys_errmsg (pi.pathname, errno); | |
797 | error ("PIOCSTATUS or PIOCWSTOP failed."); | |
798 | /* NOTREACHED */ | |
799 | } | |
800 | } | |
801 | else if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)) | |
802 | { | |
803 | rtnval = pi.prstatus.pr_pid; | |
804 | why = pi.prstatus.pr_why; | |
805 | what = pi.prstatus.pr_what; | |
806 | if (why == PR_SIGNALLED) | |
807 | { | |
808 | statval = (what << 8) | 0177; | |
809 | } | |
810 | else if ((why == PR_SYSEXIT) && | |
811 | (what == SYS_exec || what == SYS_execve)) | |
812 | { | |
813 | statval = (SIGTRAP << 8) | 0177; | |
814 | } | |
815 | else if (why == PR_REQUESTED) | |
816 | { | |
817 | statval = (SIGSTOP << 8) | 0177; | |
818 | } | |
819 | else if (why == PR_JOBCONTROL) | |
820 | { | |
821 | statval = (what << 8) | 0177; | |
822 | } | |
823 | else if (why == PR_FAULTED) | |
824 | { | |
825 | switch (what) | |
826 | { | |
827 | case FLTPRIV: | |
828 | case FLTILL: | |
829 | statval = (SIGILL << 8) | 0177; | |
830 | break; | |
831 | case FLTBPT: | |
832 | case FLTTRACE: | |
833 | statval = (SIGTRAP << 8) | 0177; | |
834 | break; | |
835 | case FLTSTACK: | |
836 | case FLTACCESS: | |
837 | case FLTBOUNDS: | |
838 | statval = (SIGSEGV << 8) | 0177; | |
839 | break; | |
840 | case FLTIOVF: | |
841 | case FLTIZDIV: | |
842 | case FLTFPE: | |
843 | statval = (SIGFPE << 8) | 0177; | |
844 | break; | |
845 | case FLTPAGE: /* Recoverable page fault */ | |
846 | default: | |
847 | rtnval = -1; | |
848 | error ("PIOCWSTOP, unknown why %d, what %d", why, what); | |
849 | /* NOTREACHED */ | |
850 | } | |
851 | } | |
852 | else | |
853 | { | |
854 | rtnval = -1; | |
855 | error ("PIOCWSTOP, unknown why %d, what %d", why, what); | |
856 | /* NOTREACHED */ | |
857 | } | |
858 | } | |
859 | else | |
860 | { | |
861 | error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x", | |
862 | pi.prstatus.pr_flags); | |
863 | /* NOTREACHED */ | |
864 | } | |
865 | if (statloc) | |
866 | { | |
867 | *statloc = statval; | |
868 | } | |
869 | return (rtnval); | |
870 | } | |
871 | ||
872 | /* | |
873 | ||
874 | GLOBAL FUNCTION | |
875 | ||
876 | child_resume -- resume execution of the inferior process | |
877 | ||
878 | SYNOPSIS | |
879 | ||
880 | void child_resume (int step, int signal) | |
881 | ||
882 | DESCRIPTION | |
883 | ||
884 | Resume execution of the inferior process. If STEP is nozero, then | |
885 | just single step it. If SIGNAL is nonzero, restart it with that | |
886 | signal activated. | |
887 | ||
888 | NOTE | |
889 | ||
890 | It may not be absolutely necessary to specify the PC value for | |
891 | restarting, but to be safe we use the value that gdb considers | |
892 | to be current. One case where this might be necessary is if the | |
893 | user explicitly changes the PC value that gdb considers to be | |
894 | current. FIXME: Investigate if this is necessary or not. | |
895 | */ | |
896 | ||
897 | void | |
898 | DEFUN(child_resume, (step, signal), | |
899 | int step AND | |
900 | int signal) | |
901 | { | |
902 | errno = 0; | |
903 | pi.prrun.pr_flags = PRSVADDR | PRSTRACE | PRSFAULT | PRCFAULT; | |
904 | pi.prrun.pr_vaddr = (caddr_t) *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)]; | |
905 | if (signal) | |
906 | { | |
907 | if (signal != pi.prstatus.pr_cursig) | |
908 | { | |
909 | struct siginfo siginfo; | |
910 | siginfo.si_signo = signal; | |
911 | siginfo.si_code = 0; | |
912 | siginfo.si_errno = 0; | |
913 | (void) ioctl (pi.fd, PIOCSSIG, &siginfo); | |
914 | } | |
915 | } | |
916 | else | |
917 | { | |
918 | pi.prrun.pr_flags |= PRCSIG; | |
919 | } | |
920 | if (step) | |
921 | { | |
922 | pi.prrun.pr_flags |= PRSTEP; | |
923 | } | |
924 | if (ioctl (pi.fd, PIOCRUN, &pi.prrun) != 0) | |
925 | { | |
926 | perror_with_name (pi.pathname); | |
927 | /* NOTREACHED */ | |
928 | } | |
929 | } | |
930 | ||
931 | /* | |
932 | ||
933 | GLOBAL FUNCTION | |
934 | ||
935 | fetch_inferior_registers -- fetch current registers from inferior | |
936 | ||
937 | SYNOPSIS | |
938 | ||
939 | void fetch_inferior_registers (void) | |
940 | ||
941 | DESCRIPTION | |
942 | ||
943 | Read the current values of the inferior's registers, both the | |
944 | general register set and floating point registers (if supported) | |
945 | and update gdb's idea of their current values. | |
946 | ||
947 | */ | |
948 | ||
949 | void | |
950 | DEFUN_VOID(fetch_inferior_registers) | |
951 | { | |
952 | if (ioctl (pi.fd, PIOCGREG, &pi.gregset) != -1) | |
953 | { | |
954 | supply_gregset (&pi.gregset); | |
955 | } | |
956 | #if defined (FP0_REGNUM) | |
957 | if (ioctl (pi.fd, PIOCGFPREG, &pi.fpregset) != -1) | |
958 | { | |
959 | supply_fpregset (&pi.fpregset); | |
960 | } | |
961 | #endif | |
962 | } | |
963 | ||
fb182850 FF |
964 | /* |
965 | ||
966 | GLOBAL FUNCTION | |
967 | ||
968 | fetch_core_registers -- fetch current registers from core file data | |
969 | ||
970 | SYNOPSIS | |
971 | ||
972 | void fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, | |
973 | int which) | |
974 | ||
975 | DESCRIPTION | |
976 | ||
977 | Read the values of either the general register set (WHICH equals 0) | |
978 | or the floating point register set (WHICH equals 2) from the core | |
979 | file data (pointed to by CORE_REG_SECT), and update gdb's idea of | |
980 | their current values. The CORE_REG_SIZE parameter is ignored. | |
981 | ||
982 | NOTES | |
983 | ||
984 | Use the indicated sizes to validate the gregset and fpregset | |
985 | structures. | |
986 | */ | |
987 | ||
988 | void | |
989 | fetch_core_registers (core_reg_sect, core_reg_size, which) | |
990 | char *core_reg_sect; | |
991 | unsigned core_reg_size; | |
992 | int which; | |
993 | { | |
994 | ||
995 | if (which == 0) | |
996 | { | |
997 | if (core_reg_size != sizeof (pi.gregset)) | |
998 | { | |
999 | warning ("wrong size gregset struct in core file"); | |
1000 | } | |
1001 | else | |
1002 | { | |
1003 | (void) memcpy ((char *) &pi.gregset, core_reg_sect, | |
1004 | sizeof (pi.gregset)); | |
1005 | supply_gregset (&pi.gregset); | |
1006 | } | |
1007 | } | |
1008 | else if (which == 2) | |
1009 | { | |
1010 | if (core_reg_size != sizeof (pi.fpregset)) | |
1011 | { | |
1012 | warning ("wrong size fpregset struct in core file"); | |
1013 | } | |
1014 | else | |
1015 | { | |
1016 | (void) memcpy ((char *) &pi.fpregset, core_reg_sect, | |
1017 | sizeof (pi.fpregset)); | |
1018 | #if defined (FP0_REGNUM) | |
1019 | supply_fpregset (&pi.fpregset); | |
1020 | #endif | |
1021 | } | |
1022 | } | |
1023 | } | |
35f5886e FF |
1024 | |
1025 | /* | |
1026 | ||
1027 | LOCAL FUNCTION | |
1028 | ||
1029 | proc_init_failed - called whenever /proc access initialization fails | |
1030 | ||
1031 | SYNOPSIS | |
1032 | ||
1033 | static void proc_init_failed (char *why) | |
1034 | ||
1035 | DESCRIPTION | |
1036 | ||
1037 | This function is called whenever initialization of access to a /proc | |
1038 | entry fails. It prints a suitable error message, does some cleanup, | |
1039 | and then invokes the standard error processing routine which dumps | |
1040 | us back into the command loop. | |
1041 | */ | |
1042 | ||
1043 | static void | |
1044 | DEFUN(proc_init_failed, (why), | |
1045 | char *why) | |
1046 | { | |
1047 | print_sys_errmsg (pi.pathname, errno); | |
1048 | (void) kill (pi.pid, SIGKILL); | |
1049 | close_proc_file (); | |
1050 | error (why); | |
1051 | /* NOTREACHED */ | |
1052 | } | |
1053 | ||
1054 | /* | |
1055 | ||
1056 | LOCAL FUNCTION | |
1057 | ||
1058 | close_proc_file - close any currently open /proc entry | |
1059 | ||
1060 | SYNOPSIS | |
1061 | ||
1062 | static void close_proc_file (void) | |
1063 | ||
1064 | DESCRIPTION | |
1065 | ||
1066 | Close any currently open /proc entry and mark the process information | |
1067 | entry as invalid. In order to ensure that we don't try to reuse any | |
1068 | stale information, the pid, fd, and pathnames are explicitly | |
1069 | invalidated, which may be overkill. | |
1070 | ||
1071 | */ | |
1072 | ||
1073 | static void | |
1074 | DEFUN_VOID(close_proc_file) | |
1075 | { | |
1076 | pi.pid = 0; | |
1077 | if (pi.valid) | |
1078 | { | |
1079 | (void) close (pi.fd); | |
1080 | } | |
1081 | pi.fd = -1; | |
1082 | if (pi.pathname) | |
1083 | { | |
1084 | free (pi.pathname); | |
1085 | pi.pathname = NULL; | |
1086 | } | |
1087 | pi.valid = 0; | |
1088 | } | |
1089 | ||
1090 | /* | |
1091 | ||
1092 | LOCAL FUNCTION | |
1093 | ||
1094 | open_proc_file - open a /proc entry for a given process id | |
1095 | ||
1096 | SYNOPSIS | |
1097 | ||
1098 | static int open_proc_file (pid) | |
1099 | ||
1100 | DESCRIPTION | |
1101 | ||
1102 | Given a process id, close the existing open /proc entry (if any) | |
1103 | and open one for the new process id. Once it is open, then | |
1104 | mark the local process information structure as valid, which | |
1105 | guarantees that the pid, fd, and pathname fields match an open | |
1106 | /proc entry. Returns zero if the open fails, nonzero otherwise. | |
1107 | ||
1108 | Note that the pathname is left intact, even when the open fails, | |
1109 | so that callers can use it to construct meaningful error messages | |
1110 | rather than just "file open failed". | |
1111 | */ | |
1112 | ||
1113 | static int | |
1114 | DEFUN(open_proc_file, (pid), | |
1115 | int pid) | |
1116 | { | |
1117 | pi.valid = 0; | |
1118 | if (pi.valid) | |
1119 | { | |
1120 | (void) close (pi.fd); | |
1121 | } | |
1122 | if (pi.pathname == NULL) | |
1123 | { | |
1124 | pi.pathname = xmalloc (32); | |
1125 | } | |
1126 | sprintf (pi.pathname, PROC_NAME_FMT, pid); | |
1127 | if ((pi.fd = open (pi.pathname, O_RDWR)) >= 0) | |
1128 | { | |
1129 | pi.valid = 1; | |
1130 | pi.pid = pid; | |
1131 | } | |
1132 | return (pi.valid); | |
1133 | } | |
1134 | ||
1135 | #endif /* USE_PROC_FS */ |