Changes in procfs.c to fix bug with inferior's siginfo struct getting
[deliverable/binutils-gdb.git] / gdb / procfs.c
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 #include "defs.h"
36
37 #ifdef USE_PROC_FS /* Entire file goes away if not using /proc */
38
39 #include <time.h>
40 #include <sys/procfs.h>
41 #include <fcntl.h>
42 #include <errno.h>
43
44 #include "inferior.h"
45 #include "target.h"
46
47 #ifndef PROC_NAME_FMT
48 #define PROC_NAME_FMT "/proc/%d"
49 #endif
50
51 #if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */
52 CORE_ADDR kernel_u_addr;
53 #endif
54
55 /* All access to the inferior, either one started by gdb or one that has
56 been attached to, is controlled by an instance of a procinfo structure,
57 defined below. Since gdb currently only handles one inferior at a time,
58 the procinfo structure for the inferior is statically allocated and
59 only one exists at any given time. There is a separate procinfo
60 structure for use by the "info proc" command, so that we can print
61 useful information about any random process without interfering with
62 the inferior's procinfo information. */
63
64 struct procinfo {
65 int valid; /* Nonzero if pid, fd, & pathname are valid */
66 int pid; /* Process ID of inferior */
67 int fd; /* File descriptor for /proc entry */
68 char *pathname; /* Pathname to /proc entry */
69 int was_stopped; /* Nonzero if was stopped prior to attach */
70 prrun_t prrun; /* Control state when it is run */
71 prstatus_t prstatus; /* Current process status info */
72 gregset_t gregset; /* General register set */
73 fpregset_t fpregset; /* Floating point register set */
74 fltset_t fltset; /* Current traced hardware fault set */
75 sigset_t trace; /* Current traced signal set */
76 sysset_t exitset; /* Current traced system call exit set */
77 sysset_t entryset; /* Current traced system call entry set */
78 };
79
80 static struct procinfo pi; /* Inferior's process information */
81
82 /* Prototypes for local functions */
83
84 static void
85 set_proc_siginfo PARAMS ((struct procinfo *, int));
86
87 static int
88 proc_address_to_fd PARAMS ((CORE_ADDR, int));
89
90 static int
91 open_proc_file PARAMS ((int, struct procinfo *));
92
93 static void
94 close_proc_file PARAMS ((struct procinfo *));
95
96 static void
97 unconditionally_kill_inferior PARAMS ((void));
98
99 static void
100 proc_init_failed PARAMS ((char *));
101
102 static void
103 proc_info PARAMS ((char *, int));
104
105 static void
106 proc_info_address_map PARAMS ((struct procinfo *, int));
107
108 static char *
109 mappingflags PARAMS ((long));
110
111 /* External function prototypes that can't be easily included in any
112 header file because the args are typedefs in system include files. */
113
114 extern void
115 supply_gregset PARAMS ((gregset_t *));
116
117 extern void
118 fill_gregset PARAMS ((gregset_t *, int));
119
120 extern void
121 supply_fpregset PARAMS ((fpregset_t *));
122
123 extern void
124 fill_fpregset PARAMS ((fpregset_t *, int));
125
126
127 /*
128
129 GLOBAL FUNCTION
130
131 ptrace -- override library version to force errors for /proc version
132
133 SYNOPSIS
134
135 int ptrace (int request, int pid, int arg3, int arg4)
136
137 DESCRIPTION
138
139 When gdb is configured to use /proc, it should not be calling
140 or otherwise attempting to use ptrace. In order to catch errors
141 where use of /proc is configured, but some routine is still calling
142 ptrace, we provide a local version of a function with that name
143 that does nothing but issue an error message.
144 */
145
146 int
147 ptrace (request, pid, arg3, arg4)
148 int request;
149 int pid;
150 int arg3;
151 int arg4;
152 {
153 error ("internal error - there is a call to ptrace() somewhere");
154 /*NOTREACHED*/
155 }
156
157 /*
158
159 GLOBAL FUNCTION
160
161 kill_inferior_fast -- kill inferior while gdb is exiting
162
163 SYNOPSIS
164
165 void kill_inferior_fast (void)
166
167 DESCRIPTION
168
169 This is used when GDB is exiting. It gives less chance of error.
170
171 NOTES
172
173 Don't attempt to kill attached inferiors since we may be called
174 when gdb is in the process of aborting, and killing the attached
175 inferior may be very anti-social. This is particularly true if we
176 were attached just so we could use the /proc facilities to get
177 detailed information about it's status.
178
179 */
180
181 void
182 kill_inferior_fast ()
183 {
184 if (inferior_pid != 0 && !attach_flag)
185 {
186 unconditionally_kill_inferior ();
187 }
188 }
189
190 /*
191
192 GLOBAL FUNCTION
193
194 kill_inferior - kill any currently inferior
195
196 SYNOPSIS
197
198 void kill_inferior (void)
199
200 DESCRIPTION
201
202 Kill any current inferior.
203
204 NOTES
205
206 Kills even attached inferiors. Presumably the user has already
207 been prompted that the inferior is an attached one rather than
208 one started by gdb. (FIXME?)
209
210 */
211
212 void
213 kill_inferior ()
214 {
215 if (inferior_pid != 0)
216 {
217 unconditionally_kill_inferior ();
218 target_mourn_inferior ();
219 }
220 }
221
222 /*
223
224 LOCAL FUNCTION
225
226 unconditionally_kill_inferior - terminate the inferior
227
228 SYNOPSIS
229
230 static void unconditionally_kill_inferior (void)
231
232 DESCRIPTION
233
234 Kill the current inferior. Should not be called until it
235 is at least tested that there is an inferior.
236
237 NOTE
238
239 A possibly useful enhancement would be to first try sending
240 the inferior a terminate signal, politely asking it to commit
241 suicide, before we murder it.
242
243 */
244
245 static void
246 unconditionally_kill_inferior ()
247 {
248 int signo;
249
250 signo = SIGKILL;
251 (void) ioctl (pi.fd, PIOCKILL, &signo);
252 close_proc_file (&pi);
253 wait ((int *) 0);
254 }
255
256 /*
257
258 GLOBAL FUNCTION
259
260 child_xfer_memory -- copy data to or from inferior memory space
261
262 SYNOPSIS
263
264 int child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
265 int dowrite, struct target_ops target)
266
267 DESCRIPTION
268
269 Copy LEN bytes to/from inferior's memory starting at MEMADDR
270 from/to debugger memory starting at MYADDR. Copy from inferior
271 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
272
273 Returns the length copied, which is either the LEN argument or
274 zero. This xfer function does not do partial moves, since child_ops
275 doesn't allow memory operations to cross below us in the target stack
276 anyway.
277
278 NOTES
279
280 The /proc interface makes this an almost trivial task.
281 */
282
283
284 int
285 child_xfer_memory (memaddr, myaddr, len, dowrite, target)
286 CORE_ADDR memaddr;
287 char *myaddr;
288 int len;
289 int dowrite;
290 struct target_ops *target; /* ignored */
291 {
292 int nbytes = 0;
293
294 if (lseek (pi.fd, (off_t) memaddr, 0) == (off_t) memaddr)
295 {
296 if (dowrite)
297 {
298 nbytes = write (pi.fd, myaddr, len);
299 }
300 else
301 {
302 nbytes = read (pi.fd, myaddr, len);
303 }
304 if (nbytes < 0)
305 {
306 nbytes = 0;
307 }
308 }
309 return (nbytes);
310 }
311
312 /*
313
314 GLOBAL FUNCTION
315
316 store_inferior_registers -- copy register values back to inferior
317
318 SYNOPSIS
319
320 void store_inferior_registers (int regno)
321
322 DESCRIPTION
323
324 Store our current register values back into the inferior. If
325 REGNO is -1 then store all the register, otherwise store just
326 the value specified by REGNO.
327
328 NOTES
329
330 If we are storing only a single register, we first have to get all
331 the current values from the process, overwrite the desired register
332 in the gregset with the one we want from gdb's registers, and then
333 send the whole set back to the process. For writing all the
334 registers, all we have to do is generate the gregset and send it to
335 the process.
336
337 Also note that the process has to be stopped on an event of interest
338 for this to work, which basically means that it has to have been
339 run under the control of one of the other /proc ioctl calls and not
340 ptrace. Since we don't use ptrace anyway, we don't worry about this
341 fine point, but it is worth noting for future reference.
342
343 Gdb is confused about what this function is supposed to return.
344 Some versions return a value, others return nothing. Some are
345 declared to return a value and actually return nothing. Gdb ignores
346 anything returned. (FIXME)
347
348 */
349
350 void
351 store_inferior_registers (regno)
352 int regno;
353 {
354 if (regno != -1)
355 {
356 (void) ioctl (pi.fd, PIOCGREG, &pi.gregset);
357 }
358 fill_gregset (&pi.gregset, regno);
359 (void) ioctl (pi.fd, PIOCSREG, &pi.gregset);
360
361 #if defined (FP0_REGNUM)
362
363 /* Now repeat everything using the floating point register set, if the
364 target has floating point hardware. Since we ignore the returned value,
365 we'll never know whether it worked or not anyway. */
366
367 if (regno != -1)
368 {
369 (void) ioctl (pi.fd, PIOCGFPREG, &pi.fpregset);
370 }
371 fill_fpregset (&pi.fpregset, regno);
372 (void) ioctl (pi.fd, PIOCSFPREG, &pi.fpregset);
373
374 #endif /* FP0_REGNUM */
375
376 }
377
378 /*
379
380 GLOBAL FUNCTION
381
382 inferior_proc_init - initialize access to a /proc entry
383
384 SYNOPSIS
385
386 void inferior_proc_init (int pid)
387
388 DESCRIPTION
389
390 When gdb starts an inferior, this function is called in the parent
391 process immediately after the fork. It waits for the child to stop
392 on the return from the exec system call (the child itself takes care
393 of ensuring that this is set up), then sets up the set of signals
394 and faults that are to be traced.
395
396 NOTES
397
398 If proc_init_failed ever gets called, control returns to the command
399 processing loop via the standard error handling code.
400 */
401
402 void
403 inferior_proc_init (pid)
404 int pid;
405 {
406 if (!open_proc_file (pid, &pi))
407 {
408 proc_init_failed ("can't open process file");
409 }
410 else
411 {
412 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
413 prfillset (&pi.prrun.pr_trace);
414 prfillset (&pi.prrun.pr_fault);
415 prdelset (&pi.prrun.pr_fault, FLTPAGE);
416 if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0)
417 {
418 proc_init_failed ("PIOCWSTOP failed");
419 }
420 else if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace) < 0)
421 {
422 proc_init_failed ("PIOCSTRACE failed");
423 }
424 else if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault) < 0)
425 {
426 proc_init_failed ("PIOCSFAULT failed");
427 }
428 }
429 }
430
431 /*
432
433 GLOBAL FUNCTION
434
435 proc_set_exec_trap -- arrange for exec'd child to halt at startup
436
437 SYNOPSIS
438
439 void proc_set_exec_trap (void)
440
441 DESCRIPTION
442
443 This function is called in the child process when starting up
444 an inferior, prior to doing the exec of the actual inferior.
445 It sets the child process's exitset to make exit from the exec
446 system call an event of interest to stop on, and then simply
447 returns. The child does the exec, the system call returns, and
448 the child stops at the first instruction, ready for the gdb
449 parent process to take control of it.
450
451 NOTE
452
453 We need to use all local variables since the child may be sharing
454 it's data space with the parent, if vfork was used rather than
455 fork.
456 */
457
458 void
459 proc_set_exec_trap ()
460 {
461 sysset_t exitset;
462 auto char procname[32];
463 int fd;
464
465 (void) sprintf (procname, PROC_NAME_FMT, getpid ());
466 if ((fd = open (procname, O_RDWR)) < 0)
467 {
468 perror (procname);
469 fflush (stderr);
470 _exit (127);
471 }
472 premptyset (&exitset);
473
474 /*
475 * GW: Rationale...
476 * Not all systems with /proc have all the exec* syscalls with the same
477 * names. On the SGI, for example, there is no SYS_exec, but there
478 * *is* a SYS_execv. So, we try to account for that.
479 */
480 #ifdef SYS_exec
481 praddset (&exitset, SYS_exec);
482 #endif
483 #ifdef SYS_execve
484 praddset (&exitset, SYS_execve);
485 #endif
486 #ifdef SYS_execv
487 praddset(&exitset, SYS_execv);
488 #endif
489
490 if (ioctl (fd, PIOCSEXIT, &exitset) < 0)
491 {
492 perror (procname);
493 fflush (stderr);
494 _exit (127);
495 }
496 }
497
498 /*
499
500 GLOBAL FUNCTION
501
502 proc_iterate_over_mappings -- call function for every mapped space
503
504 SYNOPSIS
505
506 int proc_iterate_over_mappings (int (*func)())
507
508 DESCRIPTION
509
510 Given a pointer to a function, call that function for every
511 mapped address space, passing it an open file descriptor for
512 the file corresponding to that mapped address space (if any)
513 and the base address of the mapped space. Quit when we hit
514 the end of the mappings or the function returns nonzero.
515 */
516
517 int
518 proc_iterate_over_mappings (func)
519 int (*func) PARAMS ((int, CORE_ADDR));
520 {
521 int nmap;
522 int fd;
523 int funcstat = 0;
524 struct prmap *prmaps;
525 struct prmap *prmap;
526 CORE_ADDR baseaddr = 0;
527
528 if (pi.valid && (ioctl (pi.fd, PIOCNMAP, &nmap) == 0))
529 {
530 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
531 if (ioctl (pi.fd, PIOCMAP, prmaps) == 0)
532 {
533 for (prmap = prmaps; prmap -> pr_size && funcstat == 0; ++prmap)
534 {
535 fd = proc_address_to_fd ((CORE_ADDR) prmap -> pr_vaddr, 0);
536 funcstat = (*func) (fd, (CORE_ADDR) prmap -> pr_vaddr);
537 close (fd);
538 }
539 }
540 }
541 return (funcstat);
542 }
543
544 /*
545
546 GLOBAL FUNCTION
547
548 proc_base_address -- find base address for segment containing address
549
550 SYNOPSIS
551
552 CORE_ADDR proc_base_address (CORE_ADDR addr)
553
554 DESCRIPTION
555
556 Given an address of a location in the inferior, find and return
557 the base address of the mapped segment containing that address.
558
559 This is used for example, by the shared library support code,
560 where we have the pc value for some location in the shared library
561 where we are stopped, and need to know the base address of the
562 segment containing that address.
563 */
564
565
566 #if 0 /* Currently unused */
567
568 CORE_ADDR
569 proc_base_address (addr)
570 CORE_ADDR addr;
571 {
572 int nmap;
573 struct prmap *prmaps;
574 struct prmap *prmap;
575 CORE_ADDR baseaddr = 0;
576
577 if (pi.valid && (ioctl (pi.fd, PIOCNMAP, &nmap) == 0))
578 {
579 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
580 if (ioctl (pi.fd, PIOCMAP, prmaps) == 0)
581 {
582 for (prmap = prmaps; prmap -> pr_size; ++prmap)
583 {
584 if ((prmap -> pr_vaddr <= (caddr_t) addr) &&
585 (prmap -> pr_vaddr + prmap -> pr_size > (caddr_t) addr))
586 {
587 baseaddr = (CORE_ADDR) prmap -> pr_vaddr;
588 break;
589 }
590 }
591 }
592 }
593 return (baseaddr);
594 }
595
596 #endif /* 0 */
597
598 /*
599
600 GLOBAL_FUNCTION
601
602 proc_address_to_fd -- return open fd for file mapped to address
603
604 SYNOPSIS
605
606 int proc_address_to_fd (CORE_ADDR addr, complain)
607
608 DESCRIPTION
609
610 Given an address in the current inferior's address space, use the
611 /proc interface to find an open file descriptor for the file that
612 this address was mapped in from. Return -1 if there is no current
613 inferior. Print a warning message if there is an inferior but
614 the address corresponds to no file (IE a bogus address).
615
616 */
617
618 static int
619 proc_address_to_fd (addr, complain)
620 CORE_ADDR addr;
621 int complain;
622 {
623 int fd = -1;
624
625 if (pi.valid)
626 {
627 if ((fd = ioctl (pi.fd, PIOCOPENM, (caddr_t *) &addr)) < 0)
628 {
629 if (complain)
630 {
631 print_sys_errmsg (pi.pathname, errno);
632 warning ("can't find mapped file for address 0x%x", addr);
633 }
634 }
635 }
636 return (fd);
637 }
638
639
640 #ifdef ATTACH_DETACH
641
642 /*
643
644 GLOBAL FUNCTION
645
646 attach -- attach to an already existing process
647
648 SYNOPSIS
649
650 int attach (int pid)
651
652 DESCRIPTION
653
654 Attach to an already existing process with the specified process
655 id. If the process is not already stopped, query whether to
656 stop it or not.
657
658 NOTES
659
660 The option of stopping at attach time is specific to the /proc
661 versions of gdb. Versions using ptrace force the attachee
662 to stop.
663
664 */
665
666 int
667 attach (pid)
668 int pid;
669 {
670 if (!open_proc_file (pid, &pi))
671 {
672 perror_with_name (pi.pathname);
673 /* NOTREACHED */
674 }
675
676 /* Get current status of process and if it is not already stopped,
677 then stop it. Remember whether or not it was stopped when we first
678 examined it. */
679
680 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
681 {
682 print_sys_errmsg (pi.pathname, errno);
683 close_proc_file (&pi);
684 error ("PIOCSTATUS failed");
685 }
686 if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
687 {
688 pi.was_stopped = 1;
689 }
690 else
691 {
692 pi.was_stopped = 0;
693 if (query ("Process is currently running, stop it? "))
694 {
695 if (ioctl (pi.fd, PIOCSTOP, &pi.prstatus) < 0)
696 {
697 print_sys_errmsg (pi.pathname, errno);
698 close_proc_file (&pi);
699 error ("PIOCSTOP failed");
700 }
701 }
702 }
703
704 /* Remember some things about the inferior that we will, or might, change
705 so that we can restore them when we detach. */
706
707 (void) ioctl (pi.fd, PIOCGTRACE, &pi.trace);
708 (void) ioctl (pi.fd, PIOCGFAULT, &pi.fltset);
709 (void) ioctl (pi.fd, PIOCGENTRY, &pi.entryset);
710 (void) ioctl (pi.fd, PIOCGEXIT, &pi.exitset);
711
712 /* Set up trace and fault sets, as gdb expects them. */
713
714 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
715 prfillset (&pi.prrun.pr_trace);
716 prfillset (&pi.prrun.pr_fault);
717 prdelset (&pi.prrun.pr_fault, FLTPAGE);
718 if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault))
719 {
720 print_sys_errmsg ("PIOCSFAULT failed", errno);
721 }
722 if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace))
723 {
724 print_sys_errmsg ("PIOCSTRACE failed", errno);
725 }
726 attach_flag = 1;
727 return (pid);
728 }
729
730 /*
731
732 GLOBAL FUNCTION
733
734 detach -- detach from an attached-to process
735
736 SYNOPSIS
737
738 void detach (int signal)
739
740 DESCRIPTION
741
742 Detach from the current attachee.
743
744 If signal is non-zero, the attachee is started running again and sent
745 the specified signal.
746
747 If signal is zero and the attachee was not already stopped when we
748 attached to it, then we make it runnable again when we detach.
749
750 Otherwise, we query whether or not to make the attachee runnable
751 again, since we may simply want to leave it in the state it was in
752 when we attached.
753
754 We report any problems, but do not consider them errors, since we
755 MUST detach even if some things don't seem to go right. This may not
756 be the ideal situation. (FIXME).
757 */
758
759 void
760 detach (signal)
761 int signal;
762 {
763 if (signal)
764 {
765 set_proc_siginfo (&pi, signal);
766 }
767 if (ioctl (pi.fd, PIOCSEXIT, &pi.exitset) < 0)
768 {
769 print_sys_errmsg (pi.pathname, errno);
770 printf ("PIOCSEXIT failed.\n");
771 }
772 if (ioctl (pi.fd, PIOCSENTRY, &pi.entryset) < 0)
773 {
774 print_sys_errmsg (pi.pathname, errno);
775 printf ("PIOCSENTRY failed.\n");
776 }
777 if (ioctl (pi.fd, PIOCSTRACE, &pi.trace) < 0)
778 {
779 print_sys_errmsg (pi.pathname, errno);
780 printf ("PIOCSTRACE failed.\n");
781 }
782 if (ioctl (pi.fd, PIOCSFAULT, &pi.fltset) < 0)
783 {
784 print_sys_errmsg (pi.pathname, errno);
785 printf ("PIOCSFAULT failed.\n");
786 }
787 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
788 {
789 print_sys_errmsg (pi.pathname, errno);
790 printf ("PIOCSTATUS failed.\n");
791 }
792 else
793 {
794 if (signal || (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
795 {
796 if (signal || !pi.was_stopped ||
797 query ("Was stopped when attached, make it runnable again? "))
798 {
799 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
800 pi.prrun.pr_flags = PRCFAULT;
801 if (ioctl (pi.fd, PIOCRUN, &pi.prrun))
802 {
803 print_sys_errmsg (pi.pathname, errno);
804 printf ("PIOCRUN failed.\n");
805 }
806 }
807 }
808 }
809 close_proc_file (&pi);
810 attach_flag = 0;
811 }
812
813 #endif /* ATTACH_DETACH */
814
815 /*
816
817 GLOBAL FUNCTION
818
819 proc_wait -- emulate wait() as much as possible
820
821 SYNOPSIS
822
823 int proc_wait (int *statloc)
824
825 DESCRIPTION
826
827 Try to emulate wait() as much as possible. Not sure why we can't
828 just use wait(), but it seems to have problems when applied to a
829 process being controlled with the /proc interface.
830
831 NOTES
832
833 We have a race problem here with no obvious solution. We need to let
834 the inferior run until it stops on an event of interest, which means
835 that we need to use the PIOCWSTOP ioctl. However, we cannot use this
836 ioctl if the process is already stopped on something that is not an
837 event of interest, or the call will hang indefinitely. Thus we first
838 use PIOCSTATUS to see if the process is not stopped. If not, then we
839 use PIOCWSTOP. But during the window between the two, if the process
840 stops for any reason that is not an event of interest (such as a job
841 control signal) then gdb will hang. One possible workaround is to set
842 an alarm to wake up every minute of so and check to see if the process
843 is still running, and if so, then reissue the PIOCWSTOP. But this is
844 a real kludge, so has not been implemented. FIXME: investigate
845 alternatives.
846
847 FIXME: Investigate why wait() seems to have problems with programs
848 being control by /proc routines.
849
850 */
851
852 int
853 proc_wait (statloc)
854 int *statloc;
855 {
856 short what;
857 short why;
858 int statval = 0;
859 int checkerr = 0;
860 int rtnval = -1;
861
862 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
863 {
864 checkerr++;
865 }
866 else if (!(pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
867 {
868 if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0)
869 {
870 checkerr++;
871 }
872 }
873 if (checkerr)
874 {
875 if (errno == ENOENT)
876 {
877 rtnval = wait (&statval);
878 if (rtnval != inferior_pid)
879 {
880 error ("PIOCWSTOP, wait failed, returned %d", rtnval);
881 /* NOTREACHED */
882 }
883 }
884 else
885 {
886 print_sys_errmsg (pi.pathname, errno);
887 error ("PIOCSTATUS or PIOCWSTOP failed.");
888 /* NOTREACHED */
889 }
890 }
891 else if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
892 {
893 rtnval = pi.prstatus.pr_pid;
894 why = pi.prstatus.pr_why;
895 what = pi.prstatus.pr_what;
896 if (why == PR_SIGNALLED)
897 {
898 statval = (what << 8) | 0177;
899 }
900 else if ((why == PR_SYSEXIT)
901 &&
902 (
903 #ifdef SYS_exec
904 what == SYS_exec
905 #else
906 0 == 0
907 #endif
908 #ifdef SYS_execve
909 || what == SYS_execve
910 #endif
911 #ifdef SYS_execv
912 || what == SYS_execv
913 #endif
914 ))
915 {
916 statval = (SIGTRAP << 8) | 0177;
917 }
918 else if (why == PR_REQUESTED)
919 {
920 statval = (SIGSTOP << 8) | 0177;
921 }
922 else if (why == PR_JOBCONTROL)
923 {
924 statval = (what << 8) | 0177;
925 }
926 else if (why == PR_FAULTED)
927 {
928 switch (what)
929 {
930 case FLTPRIV:
931 case FLTILL:
932 statval = (SIGILL << 8) | 0177;
933 break;
934 case FLTBPT:
935 case FLTTRACE:
936 statval = (SIGTRAP << 8) | 0177;
937 break;
938 case FLTSTACK:
939 case FLTACCESS:
940 case FLTBOUNDS:
941 statval = (SIGSEGV << 8) | 0177;
942 break;
943 case FLTIOVF:
944 case FLTIZDIV:
945 case FLTFPE:
946 statval = (SIGFPE << 8) | 0177;
947 break;
948 case FLTPAGE: /* Recoverable page fault */
949 default:
950 rtnval = -1;
951 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
952 /* NOTREACHED */
953 }
954 }
955 else
956 {
957 rtnval = -1;
958 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
959 /* NOTREACHED */
960 }
961 }
962 else
963 {
964 error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x",
965 pi.prstatus.pr_flags);
966 /* NOTREACHED */
967 }
968 if (statloc)
969 {
970 *statloc = statval;
971 }
972 return (rtnval);
973 }
974
975 /*
976
977 LOCAL FUNCTION
978
979 set_proc_siginfo - set a process's current signal info
980
981 SYNOPSIS
982
983 void set_proc_siginfo (struct procinfo *pip, int signo);
984
985 DESCRIPTION
986
987 Given a pointer to a process info struct in PIP and a signal number
988 in SIGNO, set the process's current signal and its associated signal
989 information. The signal will be delivered to the process immediately
990 after execution is resumed, even if it is being held. In addition,
991 this particular delivery will not cause another PR_SIGNALLED stop
992 even if the signal is being traced.
993
994 If we are not delivering the same signal that the prstatus siginfo
995 struct contains information about, then synthesize a siginfo struct
996 to match the signal we are doing to deliver, make it of the type
997 "generated by a user process", and send this synthesized copy. When
998 used to set the inferior's signal state, this will be required if we
999 are not currently stopped because of a traced signal, or if we decide
1000 to continue with a different signal.
1001
1002 Note that when continuing the inferior from a stop due to receipt
1003 of a traced signal, we either have set PRCSIG to clear the existing
1004 signal, or we have to call this function to do a PIOCSSIG with either
1005 the existing siginfo struct from pr_info, or one we have synthesized
1006 appropriately for the signal we want to deliver. Otherwise if the
1007 signal is still being traced, the inferior will immediately stop
1008 again.
1009
1010 See siginfo(5) for more details.
1011 */
1012
1013 static void
1014 set_proc_siginfo (pip, signo)
1015 struct procinfo *pip;
1016 int signo;
1017 {
1018 struct siginfo newsiginfo;
1019 struct siginfo *sip;
1020
1021 if (pip -> valid)
1022 {
1023 if (signo == pip -> prstatus.pr_info.si_signo)
1024 {
1025 sip = &pip -> prstatus.pr_info;
1026 }
1027 else
1028 {
1029 (void) memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
1030 sip = &newsiginfo;
1031 sip -> si_signo = signo;
1032 sip -> si_code = 0;
1033 sip -> si_errno = 0;
1034 sip -> si_pid = getpid ();
1035 sip -> si_uid = getuid ();
1036 }
1037 if (ioctl (pip -> fd, PIOCSSIG, sip) < 0)
1038 {
1039 print_sys_errmsg (pip -> pathname, errno);
1040 warning ("PIOCSSIG failed");
1041 }
1042 }
1043 }
1044
1045 /*
1046
1047 GLOBAL FUNCTION
1048
1049 child_resume -- resume execution of the inferior process
1050
1051 SYNOPSIS
1052
1053 void child_resume (int step, int signal)
1054
1055 DESCRIPTION
1056
1057 Resume execution of the inferior process. If STEP is nozero, then
1058 just single step it. If SIGNAL is nonzero, restart it with that
1059 signal activated.
1060
1061 NOTE
1062
1063 It may not be absolutely necessary to specify the PC value for
1064 restarting, but to be safe we use the value that gdb considers
1065 to be current. One case where this might be necessary is if the
1066 user explicitly changes the PC value that gdb considers to be
1067 current. FIXME: Investigate if this is necessary or not.
1068 */
1069
1070 void
1071 child_resume (step, signal)
1072 int step;
1073 int signal;
1074 {
1075 errno = 0;
1076 pi.prrun.pr_flags = PRSVADDR | PRSTRACE | PRSFAULT | PRCFAULT;
1077 pi.prrun.pr_vaddr = (caddr_t) *(int *) &registers[REGISTER_BYTE (PC_REGNUM)];
1078 if (signal)
1079 {
1080 set_proc_siginfo (&pi, signal);
1081 }
1082 else
1083 {
1084 pi.prrun.pr_flags |= PRCSIG;
1085 }
1086 if (step)
1087 {
1088 pi.prrun.pr_flags |= PRSTEP;
1089 }
1090 if (ioctl (pi.fd, PIOCRUN, &pi.prrun) != 0)
1091 {
1092 perror_with_name (pi.pathname);
1093 /* NOTREACHED */
1094 }
1095 }
1096
1097 /*
1098
1099 GLOBAL FUNCTION
1100
1101 fetch_inferior_registers -- fetch current registers from inferior
1102
1103 SYNOPSIS
1104
1105 void fetch_inferior_registers (int regno)
1106
1107 DESCRIPTION
1108
1109 Read the current values of the inferior's registers, both the
1110 general register set and floating point registers (if supported)
1111 and update gdb's idea of their current values.
1112
1113 */
1114
1115 void
1116 fetch_inferior_registers (regno)
1117 int regno;
1118 {
1119 if (ioctl (pi.fd, PIOCGREG, &pi.gregset) != -1)
1120 {
1121 supply_gregset (&pi.gregset);
1122 }
1123 #if defined (FP0_REGNUM)
1124 if (ioctl (pi.fd, PIOCGFPREG, &pi.fpregset) != -1)
1125 {
1126 supply_fpregset (&pi.fpregset);
1127 }
1128 #endif
1129 }
1130
1131 /*
1132
1133 GLOBAL FUNCTION
1134
1135 fetch_core_registers -- fetch current registers from core file data
1136
1137 SYNOPSIS
1138
1139 void fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
1140 int which, unsigned in reg_addr)
1141
1142 DESCRIPTION
1143
1144 Read the values of either the general register set (WHICH equals 0)
1145 or the floating point register set (WHICH equals 2) from the core
1146 file data (pointed to by CORE_REG_SECT), and update gdb's idea of
1147 their current values. The CORE_REG_SIZE parameter is ignored.
1148
1149 NOTES
1150
1151 Use the indicated sizes to validate the gregset and fpregset
1152 structures.
1153 */
1154
1155 void
1156 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
1157 char *core_reg_sect;
1158 unsigned core_reg_size;
1159 int which;
1160 unsigned int reg_addr; /* Unused in this version */
1161 {
1162
1163 if (which == 0)
1164 {
1165 if (core_reg_size != sizeof (pi.gregset))
1166 {
1167 warning ("wrong size gregset struct in core file");
1168 }
1169 else
1170 {
1171 (void) memcpy ((char *) &pi.gregset, core_reg_sect,
1172 sizeof (pi.gregset));
1173 supply_gregset (&pi.gregset);
1174 }
1175 }
1176 else if (which == 2)
1177 {
1178 if (core_reg_size != sizeof (pi.fpregset))
1179 {
1180 warning ("wrong size fpregset struct in core file");
1181 }
1182 else
1183 {
1184 (void) memcpy ((char *) &pi.fpregset, core_reg_sect,
1185 sizeof (pi.fpregset));
1186 #if defined (FP0_REGNUM)
1187 supply_fpregset (&pi.fpregset);
1188 #endif
1189 }
1190 }
1191 }
1192
1193 /*
1194
1195 LOCAL FUNCTION
1196
1197 proc_init_failed - called whenever /proc access initialization fails
1198
1199 SYNOPSIS
1200
1201 static void proc_init_failed (char *why)
1202
1203 DESCRIPTION
1204
1205 This function is called whenever initialization of access to a /proc
1206 entry fails. It prints a suitable error message, does some cleanup,
1207 and then invokes the standard error processing routine which dumps
1208 us back into the command loop.
1209 */
1210
1211 static void
1212 proc_init_failed (why)
1213 char *why;
1214 {
1215 print_sys_errmsg (pi.pathname, errno);
1216 (void) kill (pi.pid, SIGKILL);
1217 close_proc_file (&pi);
1218 error (why);
1219 /* NOTREACHED */
1220 }
1221
1222 /*
1223
1224 LOCAL FUNCTION
1225
1226 close_proc_file - close any currently open /proc entry
1227
1228 SYNOPSIS
1229
1230 static void close_proc_file (struct procinfo *pip)
1231
1232 DESCRIPTION
1233
1234 Close any currently open /proc entry and mark the process information
1235 entry as invalid. In order to ensure that we don't try to reuse any
1236 stale information, the pid, fd, and pathnames are explicitly
1237 invalidated, which may be overkill.
1238
1239 */
1240
1241 static void
1242 close_proc_file (pip)
1243 struct procinfo *pip;
1244 {
1245 pip -> pid = 0;
1246 if (pip -> valid)
1247 {
1248 (void) close (pip -> fd);
1249 }
1250 pip -> fd = -1;
1251 if (pip -> pathname)
1252 {
1253 free (pip -> pathname);
1254 pip -> pathname = NULL;
1255 }
1256 pip -> valid = 0;
1257 }
1258
1259 /*
1260
1261 LOCAL FUNCTION
1262
1263 open_proc_file - open a /proc entry for a given process id
1264
1265 SYNOPSIS
1266
1267 static int open_proc_file (pid, struct procinfo *pip)
1268
1269 DESCRIPTION
1270
1271 Given a process id, close the existing open /proc entry (if any)
1272 and open one for the new process id. Once it is open, then
1273 mark the local process information structure as valid, which
1274 guarantees that the pid, fd, and pathname fields match an open
1275 /proc entry. Returns zero if the open fails, nonzero otherwise.
1276
1277 Note that the pathname is left intact, even when the open fails,
1278 so that callers can use it to construct meaningful error messages
1279 rather than just "file open failed".
1280 */
1281
1282 static int
1283 open_proc_file (pid, pip)
1284 int pid;
1285 struct procinfo *pip;
1286 {
1287 pip -> valid = 0;
1288 if (pip -> valid)
1289 {
1290 (void) close (pip -> fd);
1291 }
1292 if (pip -> pathname == NULL)
1293 {
1294 pip -> pathname = xmalloc (32);
1295 }
1296 sprintf (pip -> pathname, PROC_NAME_FMT, pid);
1297 if ((pip -> fd = open (pip -> pathname, O_RDWR)) >= 0)
1298 {
1299 long pr_flags;
1300
1301 pip -> valid = 1;
1302 pip -> pid = pid;
1303 pr_flags = PR_FORK;
1304 (void) ioctl (pip -> fd, PIOCRESET, &pr_flags);
1305 }
1306 return (pip -> valid);
1307 }
1308
1309 static char *
1310 mappingflags (flags)
1311 long flags;
1312 {
1313 static char asciiflags[7];
1314
1315 strcpy (asciiflags, "------");
1316 if (flags & MA_STACK) asciiflags[0] = 's';
1317 if (flags & MA_BREAK) asciiflags[1] = 'b';
1318 if (flags & MA_SHARED) asciiflags[2] = 's';
1319 if (flags & MA_READ) asciiflags[3] = 'r';
1320 if (flags & MA_WRITE) asciiflags[4] = 'w';
1321 if (flags & MA_EXEC) asciiflags[5] = 'x';
1322 return (asciiflags);
1323 }
1324
1325 static void
1326 proc_info_address_map (pip, verbose)
1327 struct procinfo *pip;
1328 int verbose;
1329 {
1330 int nmap;
1331 struct prmap *prmaps;
1332 struct prmap *prmap;
1333
1334 printf_filtered ("Mapped address spaces:\n\n");
1335 printf_filtered ("\t%10s %10s %10s %10s %6s\n",
1336 "Start Addr",
1337 " End Addr",
1338 " Size",
1339 " Offset",
1340 "Flags");
1341 if (ioctl (pip -> fd, PIOCNMAP, &nmap) == 0)
1342 {
1343 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
1344 if (ioctl (pip -> fd, PIOCMAP, prmaps) == 0)
1345 {
1346 for (prmap = prmaps; prmap -> pr_size; ++prmap)
1347 {
1348 printf_filtered ("\t%#10x %#10x %#10x %#10x %6s\n",
1349 prmap -> pr_vaddr,
1350 prmap -> pr_vaddr + prmap -> pr_size - 1,
1351 prmap -> pr_size,
1352 prmap -> pr_off,
1353 mappingflags (prmap -> pr_mflags));
1354 }
1355 }
1356 }
1357 printf_filtered ("\n\n");
1358 }
1359
1360 /*
1361
1362 LOCAL FUNCTION
1363
1364 proc_info -- implement the "info proc" command
1365
1366 SYNOPSIS
1367
1368 void proc_info (char *args, int from_tty)
1369
1370 DESCRIPTION
1371
1372 Implement gdb's "info proc" command by using the /proc interface
1373 to print status information about any currently running process.
1374
1375 Examples of the use of "info proc" are:
1376
1377 info proc Print short info about current inferior.
1378 info proc verbose Print verbose info about current inferior.
1379 info proc 123 Print short info about process pid 123.
1380 info proc 123 verbose Print verbose info about process pid 123.
1381
1382 */
1383
1384 static void
1385 proc_info (args, from_tty)
1386 char *args;
1387 int from_tty;
1388 {
1389 int verbose = 0;
1390 int pid;
1391 struct procinfo pii;
1392 struct procinfo *pip;
1393 struct cleanup *old_chain;
1394 char *nexttok;
1395
1396 old_chain = make_cleanup (null_cleanup, 0);
1397
1398 /* Default to using the current inferior if no pid specified */
1399
1400 pip = &pi;
1401
1402 /* Parse the args string, looking for "verbose" (or any abbrev) and
1403 for a specific pid. If a specific pid is found, the process
1404 file is opened. */
1405
1406 if (args != NULL)
1407 {
1408 while ((nexttok = strtok (args, " \t")) != NULL)
1409 {
1410 args = NULL;
1411 if (strncmp (nexttok, "verbose", strlen (nexttok)) == 0)
1412 {
1413 verbose++;
1414 }
1415 else if ((pii.pid = atoi (nexttok)) > 0)
1416 {
1417 pid = pii.pid;
1418 pip = &pii;
1419 (void) memset (&pii, 0, sizeof (pii));
1420 if (!open_proc_file (pid, pip))
1421 {
1422 perror_with_name (pip -> pathname);
1423 /* NOTREACHED */
1424 }
1425 make_cleanup (close_proc_file, pip);
1426 }
1427 }
1428 }
1429
1430 /* If we don't have a valid open process at this point, then we have no
1431 inferior or didn't specify a specific pid. */
1432
1433 if (!pip -> valid)
1434 {
1435 error ("No process. Run an inferior or specify an explicit pid.");
1436 }
1437 if (ioctl (pip -> fd, PIOCSTATUS, &(pip -> prstatus)) < 0)
1438 {
1439 print_sys_errmsg (pip -> pathname, errno);
1440 error ("PIOCSTATUS failed");
1441 }
1442
1443 printf_filtered ("\nStatus information for %s:\n\n", pip -> pathname);
1444 proc_info_address_map (pip, verbose);
1445 #if 0
1446 proc_info_flags (pip, verbose);
1447 proc_info_why (pip, verbose);
1448 proc_info_what (pip, verbose);
1449 proc_info_info (pip, verbose);
1450 proc_info_cursig (pip, verbose);
1451 proc_info_sigpend (pip, verbose);
1452 proc_info_sighold (pip, verbose);
1453 proc_info_altstack (pip, verbose);
1454 proc_info_action (pip, verbose);
1455 proc_info_id (pip, verbose);
1456 proc_info_times (pip, verbose);
1457 proc_info_clname (pip,verbose);
1458 proc_info_instr (pip, verbose);
1459 proc_info_reg (pip, verbose);
1460 #endif
1461
1462 /* All done, deal with closing any temporary process info structure,
1463 freeing temporary memory , etc. */
1464
1465 do_cleanups (old_chain);
1466 }
1467
1468 /*
1469
1470 GLOBAL FUNCTION
1471
1472 _initialize_proc_fs -- initialize the process file system stuff
1473
1474 SYNOPSIS
1475
1476 void _initialize_proc_fs (void)
1477
1478 DESCRIPTION
1479
1480 Do required initializations during gdb startup for using the
1481 /proc file system interface.
1482
1483 */
1484
1485 static char *proc_desc =
1486 "Show current process status information using /proc entry.\n\
1487 With no arguments, prints short form. With 'verbose' prints long form.";
1488
1489 void
1490 _initialize_proc_fs ()
1491 {
1492 add_info ("proc", proc_info, proc_desc);
1493 }
1494
1495 #endif /* USE_PROC_FS */
This page took 0.062007 seconds and 4 git commands to generate.