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.
38 #ifdef USE_PROC_FS /* Entire file goes away if not using /proc */
41 #include <sys/procfs.h>
50 #define PROC_NAME_FMT "/proc/%d"
53 extern void EXFUN(supply_gregset
, (gregset_t
*gregsetp
));
54 extern void EXFUN(fill_gregset
, (gregset_t
*gresetp
, int regno
));
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
));
61 #if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */
62 CORE_ADDR kernel_u_addr
;
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 for the inferior is statically allocated and
69 only one exists at any given time. There is a separate procinfo
70 structure for use by the "info proc" command, so that we can print
71 useful information about any random process without interfering with
72 the inferior's procinfo information. */
75 int valid
; /* Nonzero if pid, fd, & pathname are valid */
76 int pid
; /* Process ID of inferior */
77 int fd
; /* File descriptor for /proc entry */
78 char *pathname
; /* Pathname to /proc entry */
79 int was_stopped
; /* Nonzero if was stopped prior to attach */
80 prrun_t prrun
; /* Control state when it is run */
81 prstatus_t prstatus
; /* Current process status info */
82 gregset_t gregset
; /* General register set */
83 fpregset_t fpregset
; /* Floating point register set */
84 fltset_t fltset
; /* Current traced hardware fault set */
85 sigset_t trace
; /* Current traced signal set */
86 sysset_t exitset
; /* Current traced system call exit set */
87 sysset_t entryset
; /* Current traced system call entry set */
90 static struct procinfo pi
; /* Inferior's process information */
92 /* Forward declarations of static functions so we don't have to worry
93 about ordering within this file. The EXFUN macro may be slightly
94 misleading. Should probably be called DCLFUN instead, or something
95 more intuitive, since it can be used for both static and external
98 static void EXFUN(proc_init_failed
, (char *why
));
99 static int EXFUN(open_proc_file
, (int pid
, struct procinfo
*pip
));
100 static void EXFUN(close_proc_file
, (struct procinfo
*pip
));
101 static void EXFUN(unconditionally_kill_inferior
, (void));
107 ptrace -- override library version to force errors for /proc version
111 int ptrace (int request, int pid, int arg3, int arg4)
115 When gdb is configured to use /proc, it should not be calling
116 or otherwise attempting to use ptrace. In order to catch errors
117 where use of /proc is configured, but some routine is still calling
118 ptrace, we provide a local version of a function with that name
119 that does nothing but issue an error message.
123 DEFUN(ptrace
, (request
, pid
, arg3
, arg4
),
129 error ("internal error - there is a call to ptrace() somewhere");
137 kill_inferior_fast -- kill inferior while gdb is exiting
141 void kill_inferior_fast (void)
145 This is used when GDB is exiting. It gives less chance of error.
149 Don't attempt to kill attached inferiors since we may be called
150 when gdb is in the process of aborting, and killing the attached
151 inferior may be very anti-social. This is particularly true if we
152 were attached just so we could use the /proc facilities to get
153 detailed information about it's status.
158 DEFUN_VOID(kill_inferior_fast
)
160 if (inferior_pid
!= 0 && !attach_flag
)
162 unconditionally_kill_inferior ();
170 kill_inferior - kill any currently inferior
174 void kill_inferior (void)
178 Kill any current inferior.
182 Kills even attached inferiors. Presumably the user has already
183 been prompted that the inferior is an attached one rather than
184 one started by gdb. (FIXME?)
189 DEFUN_VOID(kill_inferior
)
191 if (inferior_pid
!= 0)
193 unconditionally_kill_inferior ();
194 target_mourn_inferior ();
202 unconditionally_kill_inferior - terminate the inferior
206 static void unconditionally_kill_inferior (void)
210 Kill the current inferior. Should not be called until it
211 is at least tested that there is an inferior.
215 A possibly useful enhancement would be to first try sending
216 the inferior a terminate signal, politely asking it to commit
217 suicide, before we murder it.
222 DEFUN_VOID(unconditionally_kill_inferior
)
227 (void) ioctl (pi
.fd
, PIOCKILL
, &signo
);
228 close_proc_file (&pi
);
236 child_xfer_memory -- copy data to or from inferior memory space
240 int child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
241 int dowrite, struct target_ops target)
245 Copy LEN bytes to/from inferior's memory starting at MEMADDR
246 from/to debugger memory starting at MYADDR. Copy from inferior
247 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
249 Returns the length copied, which is either the LEN argument or
250 zero. This xfer function does not do partial moves, since child_ops
251 doesn't allow memory operations to cross below us in the target stack
256 The /proc interface makes this an almost trivial task.
261 DEFUN(child_xfer_memory
, (memaddr
, myaddr
, len
, dowrite
, target
),
262 CORE_ADDR memaddr AND
266 struct target_ops
*target
/* ignored */)
270 if (lseek (pi
.fd
, (off_t
) memaddr
, 0) == (off_t
) memaddr
)
274 nbytes
= write (pi
.fd
, myaddr
, len
);
278 nbytes
= read (pi
.fd
, myaddr
, len
);
292 store_inferior_registers -- copy register values back to inferior
296 void store_inferior_registers (int regno)
300 Store our current register values back into the inferior. If
301 REGNO is -1 then store all the register, otherwise store just
302 the value specified by REGNO.
306 If we are storing only a single register, we first have to get all
307 the current values from the process, overwrite the desired register
308 in the gregset with the one we want from gdb's registers, and then
309 send the whole set back to the process. For writing all the
310 registers, all we have to do is generate the gregset and send it to
313 Also note that the process has to be stopped on an event of interest
314 for this to work, which basically means that it has to have been
315 run under the control of one of the other /proc ioctl calls and not
316 ptrace. Since we don't use ptrace anyway, we don't worry about this
317 fine point, but it is worth noting for future reference.
319 Gdb is confused about what this function is supposed to return.
320 Some versions return a value, others return nothing. Some are
321 declared to return a value and actually return nothing. Gdb ignores
322 anything returned. (FIXME)
327 DEFUN(store_inferior_registers
, (regno
),
332 (void) ioctl (pi
.fd
, PIOCGREG
, &pi
.gregset
);
334 fill_gregset (&pi
.gregset
, regno
);
335 (void) ioctl (pi
.fd
, PIOCSREG
, &pi
.gregset
);
337 #if defined (FP0_REGNUM)
339 /* Now repeat everything using the floating point register set, if the
340 target has floating point hardware. Since we ignore the returned value,
341 we'll never know whether it worked or not anyway. */
345 (void) ioctl (pi
.fd
, PIOCGFPREG
, &pi
.fpregset
);
347 fill_fpregset (&pi
.fpregset
, regno
);
348 (void) ioctl (pi
.fd
, PIOCSFPREG
, &pi
.fpregset
);
350 #endif /* FP0_REGNUM */
358 inferior_proc_init - initialize access to a /proc entry
362 void inferior_proc_init (int pid)
366 When gdb starts an inferior, this function is called in the parent
367 process immediately after the fork. It waits for the child to stop
368 on the return from the exec system call (the child itself takes care
369 of ensuring that this is set up), then sets up the set of signals
370 and faults that are to be traced.
374 If proc_init_failed ever gets called, control returns to the command
375 processing loop via the standard error handling code.
379 DEFUN(inferior_proc_init
, (pid
),
382 if (!open_proc_file (pid
, &pi
))
384 proc_init_failed ("can't open process file");
388 (void) memset (&pi
.prrun
, 0, sizeof (pi
.prrun
));
389 prfillset (&pi
.prrun
.pr_trace
);
390 prfillset (&pi
.prrun
.pr_fault
);
391 prdelset (&pi
.prrun
.pr_fault
, FLTPAGE
);
392 if (ioctl (pi
.fd
, PIOCWSTOP
, &pi
.prstatus
) < 0)
394 proc_init_failed ("PIOCWSTOP failed");
396 else if (ioctl (pi
.fd
, PIOCSTRACE
, &pi
.prrun
.pr_trace
) < 0)
398 proc_init_failed ("PIOCSTRACE failed");
400 else if (ioctl (pi
.fd
, PIOCSFAULT
, &pi
.prrun
.pr_fault
) < 0)
402 proc_init_failed ("PIOCSFAULT failed");
411 proc_set_exec_trap -- arrange for exec'd child to halt at startup
415 void proc_set_exec_trap (void)
419 This function is called in the child process when starting up
420 an inferior, prior to doing the exec of the actual inferior.
421 It sets the child process's exitset to make exit from the exec
422 system call an event of interest to stop on, and then simply
423 returns. The child does the exec, the system call returns, and
424 the child stops at the first instruction, ready for the gdb
425 parent process to take control of it.
429 We need to use all local variables since the child may be sharing
430 it's data space with the parent, if vfork was used rather than
435 DEFUN_VOID(proc_set_exec_trap
)
438 auto char procname
[32];
441 (void) sprintf (procname
, PROC_NAME_FMT
, getpid ());
442 if ((fd
= open (procname
, O_RDWR
)) < 0)
448 premptyset (&exitset
);
449 praddset (&exitset
, SYS_exec
);
450 praddset (&exitset
, SYS_execve
);
451 if (ioctl (fd
, PIOCSEXIT
, &exitset
) < 0)
463 proc_iterate_over_mappings -- call function for every mapped space
467 int proc_iterate_over_mappings (int (*func)())
471 Given a pointer to a function, call that function for every
472 mapped address space, passing it an open file descriptor for
473 the file corresponding to that mapped address space (if any)
474 and the base address of the mapped space. Quit when we hit
475 the end of the mappings or the function returns nonzero.
479 DEFUN(proc_iterate_over_mappings
, (func
),
485 struct prmap
*prmaps
;
487 CORE_ADDR baseaddr
= 0;
489 if (pi
.valid
&& (ioctl (pi
.fd
, PIOCNMAP
, &nmap
) == 0))
491 prmaps
= alloca ((nmap
+ 1) * sizeof (*prmaps
));
492 if (ioctl (pi
.fd
, PIOCMAP
, prmaps
) == 0)
494 for (prmap
= prmaps
; prmap
-> pr_size
&& funcstat
== 0; ++prmap
)
496 fd
= proc_address_to_fd (prmap
-> pr_vaddr
, 0);
497 funcstat
= (*func
) (fd
, prmap
-> pr_vaddr
);
509 proc_base_address -- find base address for segment containing address
513 CORE_ADDR proc_base_address (CORE_ADDR addr)
517 Given an address of a location in the inferior, find and return
518 the base address of the mapped segment containing that address.
520 This is used for example, by the shared library support code,
521 where we have the pc value for some location in the shared library
522 where we are stopped, and need to know the base address of the
523 segment containing that address.
528 DEFUN(proc_base_address
, (addr
),
532 struct prmap
*prmaps
;
534 CORE_ADDR baseaddr
= 0;
536 if (pi
.valid
&& (ioctl (pi
.fd
, PIOCNMAP
, &nmap
) == 0))
538 prmaps
= alloca ((nmap
+ 1) * sizeof (*prmaps
));
539 if (ioctl (pi
.fd
, PIOCMAP
, prmaps
) == 0)
541 for (prmap
= prmaps
; prmap
-> pr_size
; ++prmap
)
543 if ((prmap
-> pr_vaddr
<= (caddr_t
) addr
) &&
544 (prmap
-> pr_vaddr
+ prmap
-> pr_size
> (caddr_t
) addr
))
546 baseaddr
= (CORE_ADDR
) prmap
-> pr_vaddr
;
559 proc_address_to_fd -- return open fd for file mapped to address
563 int proc_address_to_fd (CORE_ADDR addr, complain)
567 Given an address in the current inferior's address space, use the
568 /proc interface to find an open file descriptor for the file that
569 this address was mapped in from. Return -1 if there is no current
570 inferior. Print a warning message if there is an inferior but
571 the address corresponds to no file (IE a bogus address).
576 DEFUN(proc_address_to_fd
, (addr
, complain
),
584 if ((fd
= ioctl (pi
.fd
, PIOCOPENM
, (caddr_t
*) &addr
)) < 0)
588 print_sys_errmsg (pi
.pathname
, errno
);
589 warning ("can't find mapped file for address 0x%x", addr
);
603 attach -- attach to an already existing process
611 Attach to an already existing process with the specified process
612 id. If the process is not already stopped, query whether to
617 The option of stopping at attach time is specific to the /proc
618 versions of gdb. Versions using ptrace force the attachee
627 if (!open_proc_file (pid
, &pi
))
629 perror_with_name (pi
.pathname
);
633 /* Get current status of process and if it is not already stopped,
634 then stop it. Remember whether or not it was stopped when we first
637 if (ioctl (pi
.fd
, PIOCSTATUS
, &pi
.prstatus
) < 0)
639 print_sys_errmsg (pi
.pathname
, errno
);
640 close_proc_file (&pi
);
641 error ("PIOCSTATUS failed");
643 if (pi
.prstatus
.pr_flags
& (PR_STOPPED
| PR_ISTOP
))
650 if (query ("Process is currently running, stop it? "))
652 if (ioctl (pi
.fd
, PIOCSTOP
, &pi
.prstatus
) < 0)
654 print_sys_errmsg (pi
.pathname
, errno
);
655 close_proc_file (&pi
);
656 error ("PIOCSTOP failed");
661 /* Remember some things about the inferior that we will, or might, change
662 so that we can restore them when we detach. */
664 (void) ioctl (pi
.fd
, PIOCGTRACE
, &pi
.trace
);
665 (void) ioctl (pi
.fd
, PIOCGFAULT
, &pi
.fltset
);
666 (void) ioctl (pi
.fd
, PIOCGENTRY
, &pi
.entryset
);
667 (void) ioctl (pi
.fd
, PIOCGEXIT
, &pi
.exitset
);
669 /* Set up trace and fault sets, as gdb expects them. */
671 (void) memset (&pi
.prrun
, 0, sizeof (pi
.prrun
));
672 prfillset (&pi
.prrun
.pr_trace
);
673 prfillset (&pi
.prrun
.pr_fault
);
674 prdelset (&pi
.prrun
.pr_fault
, FLTPAGE
);
675 if (ioctl (pi
.fd
, PIOCSFAULT
, &pi
.prrun
.pr_fault
))
677 print_sys_errmsg ("PIOCSFAULT failed", errno
);
679 if (ioctl (pi
.fd
, PIOCSTRACE
, &pi
.prrun
.pr_trace
))
681 print_sys_errmsg ("PIOCSTRACE failed", errno
);
691 detach -- detach from an attached-to process
695 void detach (int signal)
699 Detach from the current attachee.
701 If signal is non-zero, the attachee is started running again and sent
702 the specified signal.
704 If signal is zero and the attachee was not already stopped when we
705 attached to it, then we make it runnable again when we detach.
707 Otherwise, we query whether or not to make the attachee runnable
708 again, since we may simply want to leave it in the state it was in
711 We report any problems, but do not consider them errors, since we
712 MUST detach even if some things don't seem to go right. This may not
713 be the ideal situation. (FIXME).
717 DEFUN(detach
, (signal
),
722 struct siginfo siginfo
;
723 siginfo
.si_signo
= signal
;
725 siginfo
.si_errno
= 0;
726 if (ioctl (pi
.fd
, PIOCSSIG
, &siginfo
) < 0)
728 print_sys_errmsg (pi
.pathname
, errno
);
729 printf ("PIOCSSIG failed.\n");
732 if (ioctl (pi
.fd
, PIOCSEXIT
, &pi
.exitset
) < 0)
734 print_sys_errmsg (pi
.pathname
, errno
);
735 printf ("PIOCSEXIT failed.\n");
737 if (ioctl (pi
.fd
, PIOCSENTRY
, &pi
.entryset
) < 0)
739 print_sys_errmsg (pi
.pathname
, errno
);
740 printf ("PIOCSENTRY failed.\n");
742 if (ioctl (pi
.fd
, PIOCSTRACE
, &pi
.trace
) < 0)
744 print_sys_errmsg (pi
.pathname
, errno
);
745 printf ("PIOCSTRACE failed.\n");
747 if (ioctl (pi
.fd
, PIOCSFAULT
, &pi
.fltset
) < 0)
749 print_sys_errmsg (pi
.pathname
, errno
);
750 printf ("PIOCSFAULT failed.\n");
752 if (ioctl (pi
.fd
, PIOCSTATUS
, &pi
.prstatus
) < 0)
754 print_sys_errmsg (pi
.pathname
, errno
);
755 printf ("PIOCSTATUS failed.\n");
759 if (signal
|| (pi
.prstatus
.pr_flags
& (PR_STOPPED
| PR_ISTOP
)))
761 if (signal
|| !pi
.was_stopped
||
762 query ("Was stopped when attached, make it runnable again? "))
764 (void) memset (&pi
.prrun
, 0, sizeof (pi
.prrun
));
765 pi
.prrun
.pr_flags
= PRCFAULT
;
766 if (ioctl (pi
.fd
, PIOCRUN
, &pi
.prrun
))
768 print_sys_errmsg (pi
.pathname
, errno
);
769 printf ("PIOCRUN failed.\n");
774 close_proc_file (&pi
);
778 #endif /* ATTACH_DETACH */
784 proc_wait -- emulate wait() as much as possible
788 int proc_wait (int *statloc)
792 Try to emulate wait() as much as possible. Not sure why we can't
793 just use wait(), but it seems to have problems when applied to a
794 process being controlled with the /proc interface.
798 We have a race problem here with no obvious solution. We need to let
799 the inferior run until it stops on an event of interest, which means
800 that we need to use the PIOCWSTOP ioctl. However, we cannot use this
801 ioctl if the process is already stopped on something that is not an
802 event of interest, or the call will hang indefinitely. Thus we first
803 use PIOCSTATUS to see if the process is not stopped. If not, then we
804 use PIOCWSTOP. But during the window between the two, if the process
805 stops for any reason that is not an event of interest (such as a job
806 control signal) then gdb will hang. One possible workaround is to set
807 an alarm to wake up every minute of so and check to see if the process
808 is still running, and if so, then reissue the PIOCWSTOP. But this is
809 a real kludge, so has not been implemented. FIXME: investigate
812 FIXME: Investigate why wait() seems to have problems with programs
813 being control by /proc routines.
818 DEFUN(proc_wait
, (statloc
),
827 if (ioctl (pi
.fd
, PIOCSTATUS
, &pi
.prstatus
) < 0)
831 else if (!(pi
.prstatus
.pr_flags
& (PR_STOPPED
| PR_ISTOP
)))
833 if (ioctl (pi
.fd
, PIOCWSTOP
, &pi
.prstatus
) < 0)
842 rtnval
= wait (&statval
);
843 if (rtnval
!= inferior_pid
)
845 error ("PIOCWSTOP, wait failed, returned %d", rtnval
);
851 print_sys_errmsg (pi
.pathname
, errno
);
852 error ("PIOCSTATUS or PIOCWSTOP failed.");
856 else if (pi
.prstatus
.pr_flags
& (PR_STOPPED
| PR_ISTOP
))
858 rtnval
= pi
.prstatus
.pr_pid
;
859 why
= pi
.prstatus
.pr_why
;
860 what
= pi
.prstatus
.pr_what
;
861 if (why
== PR_SIGNALLED
)
863 statval
= (what
<< 8) | 0177;
865 else if ((why
== PR_SYSEXIT
) &&
866 (what
== SYS_exec
|| what
== SYS_execve
))
868 statval
= (SIGTRAP
<< 8) | 0177;
870 else if (why
== PR_REQUESTED
)
872 statval
= (SIGSTOP
<< 8) | 0177;
874 else if (why
== PR_JOBCONTROL
)
876 statval
= (what
<< 8) | 0177;
878 else if (why
== PR_FAULTED
)
884 statval
= (SIGILL
<< 8) | 0177;
888 statval
= (SIGTRAP
<< 8) | 0177;
893 statval
= (SIGSEGV
<< 8) | 0177;
898 statval
= (SIGFPE
<< 8) | 0177;
900 case FLTPAGE
: /* Recoverable page fault */
903 error ("PIOCWSTOP, unknown why %d, what %d", why
, what
);
910 error ("PIOCWSTOP, unknown why %d, what %d", why
, what
);
916 error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x",
917 pi
.prstatus
.pr_flags
);
931 child_resume -- resume execution of the inferior process
935 void child_resume (int step, int signal)
939 Resume execution of the inferior process. If STEP is nozero, then
940 just single step it. If SIGNAL is nonzero, restart it with that
945 It may not be absolutely necessary to specify the PC value for
946 restarting, but to be safe we use the value that gdb considers
947 to be current. One case where this might be necessary is if the
948 user explicitly changes the PC value that gdb considers to be
949 current. FIXME: Investigate if this is necessary or not.
953 DEFUN(child_resume
, (step
, signal
),
958 pi
.prrun
.pr_flags
= PRSVADDR
| PRSTRACE
| PRSFAULT
| PRCFAULT
;
959 pi
.prrun
.pr_vaddr
= (caddr_t
) *(int *) ®isters
[REGISTER_BYTE (PC_REGNUM
)];
962 if (signal
!= pi
.prstatus
.pr_cursig
)
964 struct siginfo siginfo
;
965 siginfo
.si_signo
= signal
;
967 siginfo
.si_errno
= 0;
968 (void) ioctl (pi
.fd
, PIOCSSIG
, &siginfo
);
973 pi
.prrun
.pr_flags
|= PRCSIG
;
977 pi
.prrun
.pr_flags
|= PRSTEP
;
979 if (ioctl (pi
.fd
, PIOCRUN
, &pi
.prrun
) != 0)
981 perror_with_name (pi
.pathname
);
990 fetch_inferior_registers -- fetch current registers from inferior
994 void fetch_inferior_registers (void)
998 Read the current values of the inferior's registers, both the
999 general register set and floating point registers (if supported)
1000 and update gdb's idea of their current values.
1005 DEFUN_VOID(fetch_inferior_registers
)
1007 if (ioctl (pi
.fd
, PIOCGREG
, &pi
.gregset
) != -1)
1009 supply_gregset (&pi
.gregset
);
1011 #if defined (FP0_REGNUM)
1012 if (ioctl (pi
.fd
, PIOCGFPREG
, &pi
.fpregset
) != -1)
1014 supply_fpregset (&pi
.fpregset
);
1023 fetch_core_registers -- fetch current registers from core file data
1027 void fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
1032 Read the values of either the general register set (WHICH equals 0)
1033 or the floating point register set (WHICH equals 2) from the core
1034 file data (pointed to by CORE_REG_SECT), and update gdb's idea of
1035 their current values. The CORE_REG_SIZE parameter is ignored.
1039 Use the indicated sizes to validate the gregset and fpregset
1044 fetch_core_registers (core_reg_sect
, core_reg_size
, which
)
1045 char *core_reg_sect
;
1046 unsigned core_reg_size
;
1052 if (core_reg_size
!= sizeof (pi
.gregset
))
1054 warning ("wrong size gregset struct in core file");
1058 (void) memcpy ((char *) &pi
.gregset
, core_reg_sect
,
1059 sizeof (pi
.gregset
));
1060 supply_gregset (&pi
.gregset
);
1063 else if (which
== 2)
1065 if (core_reg_size
!= sizeof (pi
.fpregset
))
1067 warning ("wrong size fpregset struct in core file");
1071 (void) memcpy ((char *) &pi
.fpregset
, core_reg_sect
,
1072 sizeof (pi
.fpregset
));
1073 #if defined (FP0_REGNUM)
1074 supply_fpregset (&pi
.fpregset
);
1084 proc_init_failed - called whenever /proc access initialization fails
1088 static void proc_init_failed (char *why)
1092 This function is called whenever initialization of access to a /proc
1093 entry fails. It prints a suitable error message, does some cleanup,
1094 and then invokes the standard error processing routine which dumps
1095 us back into the command loop.
1099 DEFUN(proc_init_failed
, (why
),
1102 print_sys_errmsg (pi
.pathname
, errno
);
1103 (void) kill (pi
.pid
, SIGKILL
);
1104 close_proc_file (&pi
);
1113 close_proc_file - close any currently open /proc entry
1117 static void close_proc_file (struct procinfo *pip)
1121 Close any currently open /proc entry and mark the process information
1122 entry as invalid. In order to ensure that we don't try to reuse any
1123 stale information, the pid, fd, and pathnames are explicitly
1124 invalidated, which may be overkill.
1129 DEFUN(close_proc_file
, (pip
),
1130 struct procinfo
*pip
)
1135 (void) close (pip
-> fd
);
1138 if (pip
-> pathname
)
1140 free (pip
-> pathname
);
1141 pip
-> pathname
= NULL
;
1150 open_proc_file - open a /proc entry for a given process id
1154 static int open_proc_file (pid, struct procinfo *pip)
1158 Given a process id, close the existing open /proc entry (if any)
1159 and open one for the new process id. Once it is open, then
1160 mark the local process information structure as valid, which
1161 guarantees that the pid, fd, and pathname fields match an open
1162 /proc entry. Returns zero if the open fails, nonzero otherwise.
1164 Note that the pathname is left intact, even when the open fails,
1165 so that callers can use it to construct meaningful error messages
1166 rather than just "file open failed".
1170 DEFUN(open_proc_file
, (pid
, pip
),
1172 struct procinfo
*pip
)
1177 (void) close (pip
-> fd
);
1179 if (pip
-> pathname
== NULL
)
1181 pip
-> pathname
= xmalloc (32);
1183 sprintf (pip
-> pathname
, PROC_NAME_FMT
, pid
);
1184 if ((pip
-> fd
= open (pip
-> pathname
, O_RDWR
)) >= 0)
1189 return (pip
-> valid
);
1193 DEFUN (mappingflags
, (flags
),
1196 static char asciiflags
[7];
1198 strcpy (asciiflags
, "------");
1199 if (flags
& MA_STACK
) asciiflags
[0] = 's';
1200 if (flags
& MA_BREAK
) asciiflags
[1] = 'b';
1201 if (flags
& MA_SHARED
) asciiflags
[2] = 's';
1202 if (flags
& MA_READ
) asciiflags
[3] = 'r';
1203 if (flags
& MA_WRITE
) asciiflags
[4] = 'w';
1204 if (flags
& MA_EXEC
) asciiflags
[5] = 'x';
1205 return (asciiflags
);
1209 DEFUN(proc_info_address_map
, (pip
, verbose
),
1210 struct procinfo
*pip AND
1214 struct prmap
*prmaps
;
1215 struct prmap
*prmap
;
1217 printf_filtered ("Mapped address spaces:\n\n");
1218 printf_filtered ("\t%10s %10s %10s %10s %6s\n",
1224 if (ioctl (pip
-> fd
, PIOCNMAP
, &nmap
) == 0)
1226 prmaps
= alloca ((nmap
+ 1) * sizeof (*prmaps
));
1227 if (ioctl (pip
-> fd
, PIOCMAP
, prmaps
) == 0)
1229 for (prmap
= prmaps
; prmap
-> pr_size
; ++prmap
)
1231 printf_filtered ("\t%#10x %#10x %#10x %#10x %6s\n",
1233 prmap
-> pr_vaddr
+ prmap
-> pr_size
- 1,
1236 mappingflags (prmap
-> pr_mflags
));
1240 printf_filtered ("\n\n");
1247 proc_info -- implement the "info proc" command
1251 void proc_info (char *args, int from_tty)
1255 Implement gdb's "info proc" command by using the /proc interface
1256 to print status information about any currently running process.
1258 Examples of the use of "info proc" are:
1260 info proc Print short info about current inferior.
1261 info proc verbose Print verbose info about current inferior.
1262 info proc 123 Print short info about process pid 123.
1263 info proc 123 verbose Print verbose info about process pid 123.
1268 DEFUN(proc_info
, (args
, from_tty
),
1274 struct procinfo pii
;
1275 struct procinfo
*pip
;
1276 struct cleanup
*old_chain
;
1278 extern char *strtok ();
1280 old_chain
= make_cleanup (null_cleanup
, 0);
1282 /* Default to using the current inferior if no pid specified */
1286 /* Parse the args string, looking for "verbose" (or any abbrev) and
1287 for a specific pid. If a specific pid is found, the process
1292 while ((nexttok
= strtok (args
, " \t")) != NULL
)
1295 if (strncmp (nexttok
, "verbose", strlen (nexttok
)) == 0)
1299 else if ((pii
.pid
= atoi (nexttok
)) > 0)
1303 (void) memset (&pii
, 0, sizeof (pii
));
1304 if (!open_proc_file (pid
, pip
))
1306 perror_with_name (pip
-> pathname
);
1309 make_cleanup (close_proc_file
, pip
);
1314 /* If we don't have a valid open process at this point, then we have no
1315 inferior or didn't specify a specific pid. */
1319 error ("No process. Run an inferior or specify an explicit pid.");
1321 if (ioctl (pip
-> fd
, PIOCSTATUS
, &(pip
-> prstatus
)) < 0)
1323 print_sys_errmsg (pip
-> pathname
, errno
);
1324 error ("PIOCSTATUS failed");
1327 printf_filtered ("\nStatus information for %s:\n\n", pip
-> pathname
);
1328 proc_info_address_map (pip
, verbose
);
1330 proc_info_flags (pip
, verbose
);
1331 proc_info_why (pip
, verbose
);
1332 proc_info_what (pip
, verbose
);
1333 proc_info_info (pip
, verbose
);
1334 proc_info_cursig (pip
, verbose
);
1335 proc_info_sigpend (pip
, verbose
);
1336 proc_info_sighold (pip
, verbose
);
1337 proc_info_altstack (pip
, verbose
);
1338 proc_info_action (pip
, verbose
);
1339 proc_info_id (pip
, verbose
);
1340 proc_info_times (pip
, verbose
);
1341 proc_info_clname (pip
,verbose
);
1342 proc_info_instr (pip
, verbose
);
1343 proc_info_reg (pip
, verbose
);
1346 /* All done, deal with closing any temporary process info structure,
1347 freeing temporary memory , etc. */
1349 do_cleanups (old_chain
);
1356 _initialize_proc_fs -- initialize the process file system stuff
1360 void _initialize_proc_fs (void)
1364 Do required initializations during gdb startup for using the
1365 /proc file system interface.
1369 static char *proc_desc
=
1370 "Show current process status information using /proc entry.\n\
1371 With no arguments, prints short form. With 'verbose' prints long form.";
1374 _initialize_proc_fs ()
1376 add_info ("proc", proc_info
, proc_desc
);
1379 #endif /* USE_PROC_FS */