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.
5 This file is part of GDB.
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.
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.
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. */
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.
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.
37 #ifdef USE_PROC_FS /* Entire file goes away if not using /proc */
40 #include <sys/procfs.h>
48 #define PROC_NAME_FMT "/proc/%d"
51 #if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */
52 CORE_ADDR kernel_u_addr;
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. */
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 */
80 static struct procinfo pi; /* Inferior's process information */
82 /* Prototypes for local functions */
85 proc_address_to_fd PARAMS ((CORE_ADDR, int));
88 open_proc_file PARAMS ((int, struct procinfo *));
91 close_proc_file PARAMS ((struct procinfo *));
94 unconditionally_kill_inferior PARAMS ((void));
97 proc_init_failed PARAMS ((char *));
100 proc_info PARAMS ((char *, int));
103 proc_info_address_map PARAMS ((struct procinfo *, int));
106 mappingflags PARAMS ((long));
108 /* External function prototypes that can't be easily included in any
109 header file because the args are typedefs in system include files. */
112 supply_gregset PARAMS ((gregset_t *));
115 fill_gregset PARAMS ((gregset_t *, int));
118 supply_fpregset PARAMS ((fpregset_t *));
121 fill_fpregset PARAMS ((fpregset_t *, int));
128 ptrace -- override library version to force errors for /proc version
132 int ptrace (int request, int pid, int arg3, int arg4)
136 When gdb is configured to use /proc, it should not be calling
137 or otherwise attempting to use ptrace. In order to catch errors
138 where use of /proc is configured, but some routine is still calling
139 ptrace, we provide a local version of a function with that name
140 that does nothing but issue an error message.
144 ptrace (request, pid, arg3, arg4)
150 error ("internal error - there is a call to ptrace() somewhere");
158 kill_inferior_fast -- kill inferior while gdb is exiting
162 void kill_inferior_fast (void)
166 This is used when GDB is exiting. It gives less chance of error.
170 Don't attempt to kill attached inferiors since we may be called
171 when gdb is in the process of aborting, and killing the attached
172 inferior may be very anti-social. This is particularly true if we
173 were attached just so we could use the /proc facilities to get
174 detailed information about it's status.
179 kill_inferior_fast ()
181 if (inferior_pid != 0 && !attach_flag)
183 unconditionally_kill_inferior ();
191 kill_inferior - kill any currently inferior
195 void kill_inferior (void)
199 Kill any current inferior.
203 Kills even attached inferiors. Presumably the user has already
204 been prompted that the inferior is an attached one rather than
205 one started by gdb. (FIXME?)
212 if (inferior_pid != 0)
214 unconditionally_kill_inferior ();
215 target_mourn_inferior ();
223 unconditionally_kill_inferior - terminate the inferior
227 static void unconditionally_kill_inferior (void)
231 Kill the current inferior. Should not be called until it
232 is at least tested that there is an inferior.
236 A possibly useful enhancement would be to first try sending
237 the inferior a terminate signal, politely asking it to commit
238 suicide, before we murder it.
243 unconditionally_kill_inferior ()
248 (void) ioctl (pi.fd, PIOCKILL, &signo);
249 close_proc_file (&pi);
257 child_xfer_memory -- copy data to or from inferior memory space
261 int child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
262 int dowrite, struct target_ops target)
266 Copy LEN bytes to/from inferior's memory starting at MEMADDR
267 from/to debugger memory starting at MYADDR. Copy from inferior
268 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
270 Returns the length copied, which is either the LEN argument or
271 zero. This xfer function does not do partial moves, since child_ops
272 doesn't allow memory operations to cross below us in the target stack
277 The /proc interface makes this an almost trivial task.
282 child_xfer_memory (memaddr, myaddr, len, dowrite, target)
287 struct target_ops *target; /* ignored */
291 if (lseek (pi.fd, (off_t) memaddr, 0) == (off_t) memaddr)
295 nbytes = write (pi.fd, myaddr, len);
299 nbytes = read (pi.fd, myaddr, len);
313 store_inferior_registers -- copy register values back to inferior
317 void store_inferior_registers (int regno)
321 Store our current register values back into the inferior. If
322 REGNO is -1 then store all the register, otherwise store just
323 the value specified by REGNO.
327 If we are storing only a single register, we first have to get all
328 the current values from the process, overwrite the desired register
329 in the gregset with the one we want from gdb's registers, and then
330 send the whole set back to the process. For writing all the
331 registers, all we have to do is generate the gregset and send it to
334 Also note that the process has to be stopped on an event of interest
335 for this to work, which basically means that it has to have been
336 run under the control of one of the other /proc ioctl calls and not
337 ptrace. Since we don't use ptrace anyway, we don't worry about this
338 fine point, but it is worth noting for future reference.
340 Gdb is confused about what this function is supposed to return.
341 Some versions return a value, others return nothing. Some are
342 declared to return a value and actually return nothing. Gdb ignores
343 anything returned. (FIXME)
348 store_inferior_registers (regno)
353 (void) ioctl (pi.fd, PIOCGREG, &pi.gregset);
355 fill_gregset (&pi.gregset, regno);
356 (void) ioctl (pi.fd, PIOCSREG, &pi.gregset);
358 #if defined (FP0_REGNUM)
360 /* Now repeat everything using the floating point register set, if the
361 target has floating point hardware. Since we ignore the returned value,
362 we'll never know whether it worked or not anyway. */
366 (void) ioctl (pi.fd, PIOCGFPREG, &pi.fpregset);
368 fill_fpregset (&pi.fpregset, regno);
369 (void) ioctl (pi.fd, PIOCSFPREG, &pi.fpregset);
371 #endif /* FP0_REGNUM */
379 inferior_proc_init - initialize access to a /proc entry
383 void inferior_proc_init (int pid)
387 When gdb starts an inferior, this function is called in the parent
388 process immediately after the fork. It waits for the child to stop
389 on the return from the exec system call (the child itself takes care
390 of ensuring that this is set up), then sets up the set of signals
391 and faults that are to be traced.
395 If proc_init_failed ever gets called, control returns to the command
396 processing loop via the standard error handling code.
400 inferior_proc_init (pid)
403 if (!open_proc_file (pid, &pi))
405 proc_init_failed ("can't open process file");
409 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
410 prfillset (&pi.prrun.pr_trace);
411 prfillset (&pi.prrun.pr_fault);
412 prdelset (&pi.prrun.pr_fault, FLTPAGE);
413 if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0)
415 proc_init_failed ("PIOCWSTOP failed");
417 else if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace) < 0)
419 proc_init_failed ("PIOCSTRACE failed");
421 else if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault) < 0)
423 proc_init_failed ("PIOCSFAULT failed");
432 proc_set_exec_trap -- arrange for exec'd child to halt at startup
436 void proc_set_exec_trap (void)
440 This function is called in the child process when starting up
441 an inferior, prior to doing the exec of the actual inferior.
442 It sets the child process's exitset to make exit from the exec
443 system call an event of interest to stop on, and then simply
444 returns. The child does the exec, the system call returns, and
445 the child stops at the first instruction, ready for the gdb
446 parent process to take control of it.
450 We need to use all local variables since the child may be sharing
451 it's data space with the parent, if vfork was used rather than
456 proc_set_exec_trap ()
459 auto char procname[32];
462 (void) sprintf (procname, PROC_NAME_FMT, getpid ());
463 if ((fd = open (procname, O_RDWR)) < 0)
469 premptyset (&exitset);
473 * Not all systems with /proc have all the exec* syscalls with the same
474 * names. On the SGI, for example, there is no SYS_exec, but there
475 * *is* a SYS_execv. So, we try to account for that.
478 praddset (&exitset, SYS_exec);
481 praddset (&exitset, SYS_execve);
484 praddset(&exitset, SYS_execv);
487 if (ioctl (fd, PIOCSEXIT, &exitset) < 0)
499 proc_iterate_over_mappings -- call function for every mapped space
503 int proc_iterate_over_mappings (int (*func)())
507 Given a pointer to a function, call that function for every
508 mapped address space, passing it an open file descriptor for
509 the file corresponding to that mapped address space (if any)
510 and the base address of the mapped space. Quit when we hit
511 the end of the mappings or the function returns nonzero.
515 proc_iterate_over_mappings (func)
516 int (*func) PARAMS ((int, CORE_ADDR));
521 struct prmap *prmaps;
523 CORE_ADDR baseaddr = 0;
525 if (pi.valid && (ioctl (pi.fd, PIOCNMAP, &nmap) == 0))
527 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
528 if (ioctl (pi.fd, PIOCMAP, prmaps) == 0)
530 for (prmap = prmaps; prmap -> pr_size && funcstat == 0; ++prmap)
532 fd = proc_address_to_fd ((CORE_ADDR) prmap -> pr_vaddr, 0);
533 funcstat = (*func) (fd, (CORE_ADDR) prmap -> pr_vaddr);
545 proc_base_address -- find base address for segment containing address
549 CORE_ADDR proc_base_address (CORE_ADDR addr)
553 Given an address of a location in the inferior, find and return
554 the base address of the mapped segment containing that address.
556 This is used for example, by the shared library support code,
557 where we have the pc value for some location in the shared library
558 where we are stopped, and need to know the base address of the
559 segment containing that address.
563 #if 0 /* Currently unused */
566 proc_base_address (addr)
570 struct prmap *prmaps;
572 CORE_ADDR baseaddr = 0;
574 if (pi.valid && (ioctl (pi.fd, PIOCNMAP, &nmap) == 0))
576 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
577 if (ioctl (pi.fd, PIOCMAP, prmaps) == 0)
579 for (prmap = prmaps; prmap -> pr_size; ++prmap)
581 if ((prmap -> pr_vaddr <= (caddr_t) addr) &&
582 (prmap -> pr_vaddr + prmap -> pr_size > (caddr_t) addr))
584 baseaddr = (CORE_ADDR) prmap -> pr_vaddr;
599 proc_address_to_fd -- return open fd for file mapped to address
603 int proc_address_to_fd (CORE_ADDR addr, complain)
607 Given an address in the current inferior's address space, use the
608 /proc interface to find an open file descriptor for the file that
609 this address was mapped in from. Return -1 if there is no current
610 inferior. Print a warning message if there is an inferior but
611 the address corresponds to no file (IE a bogus address).
616 proc_address_to_fd (addr, complain)
624 if ((fd = ioctl (pi.fd, PIOCOPENM, (caddr_t *) &addr)) < 0)
628 print_sys_errmsg (pi.pathname, errno);
629 warning ("can't find mapped file for address 0x%x", addr);
643 attach -- attach to an already existing process
651 Attach to an already existing process with the specified process
652 id. If the process is not already stopped, query whether to
657 The option of stopping at attach time is specific to the /proc
658 versions of gdb. Versions using ptrace force the attachee
667 if (!open_proc_file (pid, &pi))
669 perror_with_name (pi.pathname);
673 /* Get current status of process and if it is not already stopped,
674 then stop it. Remember whether or not it was stopped when we first
677 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
679 print_sys_errmsg (pi.pathname, errno);
680 close_proc_file (&pi);
681 error ("PIOCSTATUS failed");
683 if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
690 if (query ("Process is currently running, stop it? "))
692 if (ioctl (pi.fd, PIOCSTOP, &pi.prstatus) < 0)
694 print_sys_errmsg (pi.pathname, errno);
695 close_proc_file (&pi);
696 error ("PIOCSTOP failed");
701 /* Remember some things about the inferior that we will, or might, change
702 so that we can restore them when we detach. */
704 (void) ioctl (pi.fd, PIOCGTRACE, &pi.trace);
705 (void) ioctl (pi.fd, PIOCGFAULT, &pi.fltset);
706 (void) ioctl (pi.fd, PIOCGENTRY, &pi.entryset);
707 (void) ioctl (pi.fd, PIOCGEXIT, &pi.exitset);
709 /* Set up trace and fault sets, as gdb expects them. */
711 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
712 prfillset (&pi.prrun.pr_trace);
713 prfillset (&pi.prrun.pr_fault);
714 prdelset (&pi.prrun.pr_fault, FLTPAGE);
715 if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault))
717 print_sys_errmsg ("PIOCSFAULT failed", errno);
719 if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace))
721 print_sys_errmsg ("PIOCSTRACE failed", errno);
731 detach -- detach from an attached-to process
735 void detach (int signal)
739 Detach from the current attachee.
741 If signal is non-zero, the attachee is started running again and sent
742 the specified signal.
744 If signal is zero and the attachee was not already stopped when we
745 attached to it, then we make it runnable again when we detach.
747 Otherwise, we query whether or not to make the attachee runnable
748 again, since we may simply want to leave it in the state it was in
751 We report any problems, but do not consider them errors, since we
752 MUST detach even if some things don't seem to go right. This may not
753 be the ideal situation. (FIXME).
762 struct siginfo siginfo;
763 siginfo.si_signo = signal;
765 siginfo.si_errno = 0;
766 if (ioctl (pi.fd, PIOCSSIG, &siginfo) < 0)
768 print_sys_errmsg (pi.pathname, errno);
769 printf ("PIOCSSIG failed.\n");
772 if (ioctl (pi.fd, PIOCSEXIT, &pi.exitset) < 0)
774 print_sys_errmsg (pi.pathname, errno);
775 printf ("PIOCSEXIT failed.\n");
777 if (ioctl (pi.fd, PIOCSENTRY, &pi.entryset) < 0)
779 print_sys_errmsg (pi.pathname, errno);
780 printf ("PIOCSENTRY failed.\n");
782 if (ioctl (pi.fd, PIOCSTRACE, &pi.trace) < 0)
784 print_sys_errmsg (pi.pathname, errno);
785 printf ("PIOCSTRACE failed.\n");
787 if (ioctl (pi.fd, PIOCSFAULT, &pi.fltset) < 0)
789 print_sys_errmsg (pi.pathname, errno);
790 printf ("PIOCSFAULT failed.\n");
792 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
794 print_sys_errmsg (pi.pathname, errno);
795 printf ("PIOCSTATUS failed.\n");
799 if (signal || (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
801 if (signal || !pi.was_stopped ||
802 query ("Was stopped when attached, make it runnable again? "))
804 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
805 pi.prrun.pr_flags = PRCFAULT;
806 if (ioctl (pi.fd, PIOCRUN, &pi.prrun))
808 print_sys_errmsg (pi.pathname, errno);
809 printf ("PIOCRUN failed.\n");
814 close_proc_file (&pi);
818 #endif /* ATTACH_DETACH */
824 proc_wait -- emulate wait() as much as possible
828 int proc_wait (int *statloc)
832 Try to emulate wait() as much as possible. Not sure why we can't
833 just use wait(), but it seems to have problems when applied to a
834 process being controlled with the /proc interface.
838 We have a race problem here with no obvious solution. We need to let
839 the inferior run until it stops on an event of interest, which means
840 that we need to use the PIOCWSTOP ioctl. However, we cannot use this
841 ioctl if the process is already stopped on something that is not an
842 event of interest, or the call will hang indefinitely. Thus we first
843 use PIOCSTATUS to see if the process is not stopped. If not, then we
844 use PIOCWSTOP. But during the window between the two, if the process
845 stops for any reason that is not an event of interest (such as a job
846 control signal) then gdb will hang. One possible workaround is to set
847 an alarm to wake up every minute of so and check to see if the process
848 is still running, and if so, then reissue the PIOCWSTOP. But this is
849 a real kludge, so has not been implemented. FIXME: investigate
852 FIXME: Investigate why wait() seems to have problems with programs
853 being control by /proc routines.
867 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
871 else if (!(pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
873 if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0)
882 rtnval = wait (&statval);
883 if (rtnval != inferior_pid)
885 error ("PIOCWSTOP, wait failed, returned %d", rtnval);
891 print_sys_errmsg (pi.pathname, errno);
892 error ("PIOCSTATUS or PIOCWSTOP failed.");
896 else if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
898 rtnval = pi.prstatus.pr_pid;
899 why = pi.prstatus.pr_why;
900 what = pi.prstatus.pr_what;
901 if (why == PR_SIGNALLED)
903 statval = (what << 8) | 0177;
905 else if ((why == PR_SYSEXIT)
914 || what == SYS_execve
921 statval = (SIGTRAP << 8) | 0177;
923 else if (why == PR_REQUESTED)
925 statval = (SIGSTOP << 8) | 0177;
927 else if (why == PR_JOBCONTROL)
929 statval = (what << 8) | 0177;
931 else if (why == PR_FAULTED)
937 statval = (SIGILL << 8) | 0177;
941 statval = (SIGTRAP << 8) | 0177;
946 statval = (SIGSEGV << 8) | 0177;
951 statval = (SIGFPE << 8) | 0177;
953 case FLTPAGE: /* Recoverable page fault */
956 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
963 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
969 error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x",
970 pi.prstatus.pr_flags);
984 child_resume -- resume execution of the inferior process
988 void child_resume (int step, int signal)
992 Resume execution of the inferior process. If STEP is nozero, then
993 just single step it. If SIGNAL is nonzero, restart it with that
998 It may not be absolutely necessary to specify the PC value for
999 restarting, but to be safe we use the value that gdb considers
1000 to be current. One case where this might be necessary is if the
1001 user explicitly changes the PC value that gdb considers to be
1002 current. FIXME: Investigate if this is necessary or not.
1006 child_resume (step, signal)
1011 pi.prrun.pr_flags = PRSVADDR | PRSTRACE | PRSFAULT | PRCFAULT;
1012 pi.prrun.pr_vaddr = (caddr_t) *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)];
1015 if (signal != pi.prstatus.pr_cursig)
1017 struct siginfo siginfo;
1018 siginfo.si_signo = signal;
1019 siginfo.si_code = 0;
1020 siginfo.si_errno = 0;
1021 (void) ioctl (pi.fd, PIOCSSIG, &siginfo);
1026 pi.prrun.pr_flags |= PRCSIG;
1030 pi.prrun.pr_flags |= PRSTEP;
1032 if (ioctl (pi.fd, PIOCRUN, &pi.prrun) != 0)
1034 perror_with_name (pi.pathname);
1043 fetch_inferior_registers -- fetch current registers from inferior
1047 void fetch_inferior_registers (int regno)
1051 Read the current values of the inferior's registers, both the
1052 general register set and floating point registers (if supported)
1053 and update gdb's idea of their current values.
1058 fetch_inferior_registers (regno)
1061 if (ioctl (pi.fd, PIOCGREG, &pi.gregset) != -1)
1063 supply_gregset (&pi.gregset);
1065 #if defined (FP0_REGNUM)
1066 if (ioctl (pi.fd, PIOCGFPREG, &pi.fpregset) != -1)
1068 supply_fpregset (&pi.fpregset);
1077 fetch_core_registers -- fetch current registers from core file data
1081 void fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
1082 int which, unsigned in reg_addr)
1086 Read the values of either the general register set (WHICH equals 0)
1087 or the floating point register set (WHICH equals 2) from the core
1088 file data (pointed to by CORE_REG_SECT), and update gdb's idea of
1089 their current values. The CORE_REG_SIZE parameter is ignored.
1093 Use the indicated sizes to validate the gregset and fpregset
1098 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
1099 char *core_reg_sect;
1100 unsigned core_reg_size;
1102 unsigned int reg_addr; /* Unused in this version */
1107 if (core_reg_size != sizeof (pi.gregset))
1109 warning ("wrong size gregset struct in core file");
1113 (void) memcpy ((char *) &pi.gregset, core_reg_sect,
1114 sizeof (pi.gregset));
1115 supply_gregset (&pi.gregset);
1118 else if (which == 2)
1120 if (core_reg_size != sizeof (pi.fpregset))
1122 warning ("wrong size fpregset struct in core file");
1126 (void) memcpy ((char *) &pi.fpregset, core_reg_sect,
1127 sizeof (pi.fpregset));
1128 #if defined (FP0_REGNUM)
1129 supply_fpregset (&pi.fpregset);
1139 proc_init_failed - called whenever /proc access initialization fails
1143 static void proc_init_failed (char *why)
1147 This function is called whenever initialization of access to a /proc
1148 entry fails. It prints a suitable error message, does some cleanup,
1149 and then invokes the standard error processing routine which dumps
1150 us back into the command loop.
1154 proc_init_failed (why)
1157 print_sys_errmsg (pi.pathname, errno);
1158 (void) kill (pi.pid, SIGKILL);
1159 close_proc_file (&pi);
1168 close_proc_file - close any currently open /proc entry
1172 static void close_proc_file (struct procinfo *pip)
1176 Close any currently open /proc entry and mark the process information
1177 entry as invalid. In order to ensure that we don't try to reuse any
1178 stale information, the pid, fd, and pathnames are explicitly
1179 invalidated, which may be overkill.
1184 close_proc_file (pip)
1185 struct procinfo *pip;
1190 (void) close (pip -> fd);
1193 if (pip -> pathname)
1195 free (pip -> pathname);
1196 pip -> pathname = NULL;
1205 open_proc_file - open a /proc entry for a given process id
1209 static int open_proc_file (pid, struct procinfo *pip)
1213 Given a process id, close the existing open /proc entry (if any)
1214 and open one for the new process id. Once it is open, then
1215 mark the local process information structure as valid, which
1216 guarantees that the pid, fd, and pathname fields match an open
1217 /proc entry. Returns zero if the open fails, nonzero otherwise.
1219 Note that the pathname is left intact, even when the open fails,
1220 so that callers can use it to construct meaningful error messages
1221 rather than just "file open failed".
1225 open_proc_file (pid, pip)
1227 struct procinfo *pip;
1232 (void) close (pip -> fd);
1234 if (pip -> pathname == NULL)
1236 pip -> pathname = xmalloc (32);
1238 sprintf (pip -> pathname, PROC_NAME_FMT, pid);
1239 if ((pip -> fd = open (pip -> pathname, O_RDWR)) >= 0)
1246 (void) ioctl (pip -> fd, PIOCRESET, &pr_flags);
1248 return (pip -> valid);
1252 mappingflags (flags)
1255 static char asciiflags[7];
1257 strcpy (asciiflags, "------");
1258 if (flags & MA_STACK) asciiflags[0] = 's';
1259 if (flags & MA_BREAK) asciiflags[1] = 'b';
1260 if (flags & MA_SHARED) asciiflags[2] = 's';
1261 if (flags & MA_READ) asciiflags[3] = 'r';
1262 if (flags & MA_WRITE) asciiflags[4] = 'w';
1263 if (flags & MA_EXEC) asciiflags[5] = 'x';
1264 return (asciiflags);
1268 proc_info_address_map (pip, verbose)
1269 struct procinfo *pip;
1273 struct prmap *prmaps;
1274 struct prmap *prmap;
1276 printf_filtered ("Mapped address spaces:\n\n");
1277 printf_filtered ("\t%10s %10s %10s %10s %6s\n",
1283 if (ioctl (pip -> fd, PIOCNMAP, &nmap) == 0)
1285 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
1286 if (ioctl (pip -> fd, PIOCMAP, prmaps) == 0)
1288 for (prmap = prmaps; prmap -> pr_size; ++prmap)
1290 printf_filtered ("\t%#10x %#10x %#10x %#10x %6s\n",
1292 prmap -> pr_vaddr + prmap -> pr_size - 1,
1295 mappingflags (prmap -> pr_mflags));
1299 printf_filtered ("\n\n");
1306 proc_info -- implement the "info proc" command
1310 void proc_info (char *args, int from_tty)
1314 Implement gdb's "info proc" command by using the /proc interface
1315 to print status information about any currently running process.
1317 Examples of the use of "info proc" are:
1319 info proc Print short info about current inferior.
1320 info proc verbose Print verbose info about current inferior.
1321 info proc 123 Print short info about process pid 123.
1322 info proc 123 verbose Print verbose info about process pid 123.
1327 proc_info (args, from_tty)
1333 struct procinfo pii;
1334 struct procinfo *pip;
1335 struct cleanup *old_chain;
1338 old_chain = make_cleanup (null_cleanup, 0);
1340 /* Default to using the current inferior if no pid specified */
1344 /* Parse the args string, looking for "verbose" (or any abbrev) and
1345 for a specific pid. If a specific pid is found, the process
1350 while ((nexttok = strtok (args, " \t")) != NULL)
1353 if (strncmp (nexttok, "verbose", strlen (nexttok)) == 0)
1357 else if ((pii.pid = atoi (nexttok)) > 0)
1361 (void) memset (&pii, 0, sizeof (pii));
1362 if (!open_proc_file (pid, pip))
1364 perror_with_name (pip -> pathname);
1367 make_cleanup (close_proc_file, pip);
1372 /* If we don't have a valid open process at this point, then we have no
1373 inferior or didn't specify a specific pid. */
1377 error ("No process. Run an inferior or specify an explicit pid.");
1379 if (ioctl (pip -> fd, PIOCSTATUS, &(pip -> prstatus)) < 0)
1381 print_sys_errmsg (pip -> pathname, errno);
1382 error ("PIOCSTATUS failed");
1385 printf_filtered ("\nStatus information for %s:\n\n", pip -> pathname);
1386 proc_info_address_map (pip, verbose);
1388 proc_info_flags (pip, verbose);
1389 proc_info_why (pip, verbose);
1390 proc_info_what (pip, verbose);
1391 proc_info_info (pip, verbose);
1392 proc_info_cursig (pip, verbose);
1393 proc_info_sigpend (pip, verbose);
1394 proc_info_sighold (pip, verbose);
1395 proc_info_altstack (pip, verbose);
1396 proc_info_action (pip, verbose);
1397 proc_info_id (pip, verbose);
1398 proc_info_times (pip, verbose);
1399 proc_info_clname (pip,verbose);
1400 proc_info_instr (pip, verbose);
1401 proc_info_reg (pip, verbose);
1404 /* All done, deal with closing any temporary process info structure,
1405 freeing temporary memory , etc. */
1407 do_cleanups (old_chain);
1414 _initialize_proc_fs -- initialize the process file system stuff
1418 void _initialize_proc_fs (void)
1422 Do required initializations during gdb startup for using the
1423 /proc file system interface.
1427 static char *proc_desc =
1428 "Show current process status information using /proc entry.\n\
1429 With no arguments, prints short form. With 'verbose' prints long form.";
1432 _initialize_proc_fs ()
1434 add_info ("proc", proc_info, proc_desc);
1437 #endif /* USE_PROC_FS */