1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
2 Copyright 1991, 1992 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 #include <sys/types.h>
39 #include <sys/procfs.h>
53 #define MAX_SYSCALLS 256 /* Maximum number of syscalls for table */
56 #define PROC_NAME_FMT "/proc/%05d"
59 extern struct target_ops procfs_ops; /* Forward declaration */
61 #if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */
62 CORE_ADDR kernel_u_addr;
65 #ifdef BROKEN_SIGINFO_H /* Workaround broken SGS <sys/siginfo.h> */
67 #define si_pid _data._proc.pid
69 #define si_uid _data._proc._pdata._kill.uid
70 #endif /* BROKEN_SIGINFO_H */
72 /* All access to the inferior, either one started by gdb or one that has
73 been attached to, is controlled by an instance of a procinfo structure,
74 defined below. Since gdb currently only handles one inferior at a time,
75 the procinfo structure for the inferior is statically allocated and
76 only one exists at any given time. There is a separate procinfo
77 structure for use by the "info proc" command, so that we can print
78 useful information about any random process without interfering with
79 the inferior's procinfo information. */
82 struct procinfo *next;
83 int pid; /* Process ID of inferior */
84 int fd; /* File descriptor for /proc entry */
85 char *pathname; /* Pathname to /proc entry */
86 int had_event; /* poll/select says something happened */
87 int was_stopped; /* Nonzero if was stopped prior to attach */
88 int nopass_next_sigstop; /* Don't pass a sigstop on next resume */
89 prrun_t prrun; /* Control state when it is run */
90 prstatus_t prstatus; /* Current process status info */
91 gregset_t gregset; /* General register set */
92 fpregset_t fpregset; /* Floating point register set */
93 fltset_t fltset; /* Current traced hardware fault set */
94 sigset_t trace; /* Current traced signal set */
95 sysset_t exitset; /* Current traced system call exit set */
96 sysset_t entryset; /* Current traced system call entry set */
97 fltset_t saved_fltset; /* Saved traced hardware fault set */
98 sigset_t saved_trace; /* Saved traced signal set */
99 sigset_t saved_sighold; /* Saved held signal set */
100 sysset_t saved_exitset; /* Saved traced system call exit set */
101 sysset_t saved_entryset; /* Saved traced system call entry set */
104 /* List of inferior process information */
105 static struct procinfo *procinfo_list = NULL;
107 static struct pollfd *poll_list; /* pollfds used for waiting on /proc */
109 static int num_poll_list = 0; /* Number of entries in poll_list */
111 static int last_resume_pid = -1; /* Last pid used with procfs_resume */
113 /* Much of the information used in the /proc interface, particularly for
114 printing status information, is kept as tables of structures of the
115 following form. These tables can be used to map numeric values to
116 their symbolic names and to a string that describes their specific use. */
119 int value; /* The numeric value */
120 char *name; /* The equivalent symbolic value */
121 char *desc; /* Short description of value */
124 /* Translate bits in the pr_flags member of the prstatus structure, into the
125 names and desc information. */
127 static struct trans pr_flag_table[] =
129 #if defined (PR_STOPPED)
130 PR_STOPPED, "PR_STOPPED", "Process is stopped",
132 #if defined (PR_ISTOP)
133 PR_ISTOP, "PR_ISTOP", "Stopped on an event of interest",
135 #if defined (PR_DSTOP)
136 PR_DSTOP, "PR_DSTOP", "A stop directive is in effect",
138 #if defined (PR_ASLEEP)
139 PR_ASLEEP, "PR_ASLEEP", "Sleeping in an interruptible system call",
141 #if defined (PR_FORK)
142 PR_FORK, "PR_FORK", "Inherit-on-fork is in effect",
145 PR_RLC, "PR_RLC", "Run-on-last-close is in effect",
147 #if defined (PR_PTRACE)
148 PR_PTRACE, "PR_PTRACE", "Process is being controlled by ptrace",
150 #if defined (PR_PCINVAL)
151 PR_PCINVAL, "PR_PCINVAL", "PC refers to an invalid virtual address",
153 #if defined (PR_ISSYS)
154 PR_ISSYS, "PR_ISSYS", "Is a system process",
156 #if defined (PR_STEP)
157 PR_STEP, "PR_STEP", "Process has single step pending",
160 PR_KLC, "PR_KLC", "Kill-on-last-close is in effect",
162 #if defined (PR_ASYNC)
163 PR_ASYNC, "PR_ASYNC", "Asynchronous stop is in effect",
165 #if defined (PR_PCOMPAT)
166 PR_PCOMPAT, "PR_PCOMPAT", "Ptrace compatibility mode in effect",
171 /* Translate values in the pr_why field of the prstatus struct. */
173 static struct trans pr_why_table[] =
175 #if defined (PR_REQUESTED)
176 PR_REQUESTED, "PR_REQUESTED", "Directed to stop via PIOCSTOP/PIOCWSTOP",
178 #if defined (PR_SIGNALLED)
179 PR_SIGNALLED, "PR_SIGNALLED", "Receipt of a traced signal",
181 #if defined (PR_FAULTED)
182 PR_FAULTED, "PR_FAULTED", "Incurred a traced hardware fault",
184 #if defined (PR_SYSENTRY)
185 PR_SYSENTRY, "PR_SYSENTRY", "Entry to a traced system call",
187 #if defined (PR_SYSEXIT)
188 PR_SYSEXIT, "PR_SYSEXIT", "Exit from a traced system call",
190 #if defined (PR_JOBCONTROL)
191 PR_JOBCONTROL, "PR_JOBCONTROL", "Default job control stop signal action",
193 #if defined (PR_SUSPENDED)
194 PR_SUSPENDED, "PR_SUSPENDED", "Process suspended",
199 /* Hardware fault translation table. */
201 static struct trans faults_table[] =
204 FLTILL, "FLTILL", "Illegal instruction",
206 #if defined (FLTPRIV)
207 FLTPRIV, "FLTPRIV", "Privileged instruction",
210 FLTBPT, "FLTBPT", "Breakpoint trap",
212 #if defined (FLTTRACE)
213 FLTTRACE, "FLTTRACE", "Trace trap",
215 #if defined (FLTACCESS)
216 FLTACCESS, "FLTACCESS", "Memory access fault",
218 #if defined (FLTBOUNDS)
219 FLTBOUNDS, "FLTBOUNDS", "Memory bounds violation",
221 #if defined (FLTIOVF)
222 FLTIOVF, "FLTIOVF", "Integer overflow",
224 #if defined (FLTIZDIV)
225 FLTIZDIV, "FLTIZDIV", "Integer zero divide",
228 FLTFPE, "FLTFPE", "Floating-point exception",
230 #if defined (FLTSTACK)
231 FLTSTACK, "FLTSTACK", "Unrecoverable stack fault",
233 #if defined (FLTPAGE)
234 FLTPAGE, "FLTPAGE", "Recoverable page fault",
239 /* Translation table for signal generation information. See UNIX System
240 V Release 4 Programmer's Reference Manual, siginfo(5). */
242 static struct sigcode {
247 } siginfo_table[] = {
248 #if defined (SIGILL) && defined (ILL_ILLOPC)
249 SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode",
251 #if defined (SIGILL) && defined (ILL_ILLOPN)
252 SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand",
254 #if defined (SIGILL) && defined (ILL_ILLADR)
255 SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode",
257 #if defined (SIGILL) && defined (ILL_ILLTRP)
258 SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap",
260 #if defined (SIGILL) && defined (ILL_PRVOPC)
261 SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode",
263 #if defined (SIGILL) && defined (ILL_PRVREG)
264 SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register",
266 #if defined (SIGILL) && defined (ILL_COPROC)
267 SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error",
269 #if defined (SIGILL) && defined (ILL_BADSTK)
270 SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error",
272 #if defined (SIGFPE) && defined (FPE_INTDIV)
273 SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero",
275 #if defined (SIGFPE) && defined (FPE_INTOVF)
276 SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow",
278 #if defined (SIGFPE) && defined (FPE_FLTDIV)
279 SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating point divide by zero",
281 #if defined (SIGFPE) && defined (FPE_FLTOVF)
282 SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating point overflow",
284 #if defined (SIGFPE) && defined (FPE_FLTUND)
285 SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating point underflow",
287 #if defined (SIGFPE) && defined (FPE_FLTRES)
288 SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating point inexact result",
290 #if defined (SIGFPE) && defined (FPE_FLTINV)
291 SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating point operation",
293 #if defined (SIGFPE) && defined (FPE_FLTSUB)
294 SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range",
296 #if defined (SIGSEGV) && defined (SEGV_MAPERR)
297 SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object",
299 #if defined (SIGSEGV) && defined (SEGV_ACCERR)
300 SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for object",
302 #if defined (SIGBUS) && defined (BUS_ADRALN)
303 SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment",
305 #if defined (SIGBUS) && defined (BUS_ADRERR)
306 SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Non-existent physical address",
308 #if defined (SIGBUS) && defined (BUS_OBJERR)
309 SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object specific hardware error",
311 #if defined (SIGTRAP) && defined (TRAP_BRKPT)
312 SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint",
314 #if defined (SIGTRAP) && defined (TRAP_TRACE)
315 SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap",
317 #if defined (SIGCLD) && defined (CLD_EXITED)
318 SIGCLD, CLD_EXITED, "CLD_EXITED", "Child has exited",
320 #if defined (SIGCLD) && defined (CLD_KILLED)
321 SIGCLD, CLD_KILLED, "CLD_KILLED", "Child was killed",
323 #if defined (SIGCLD) && defined (CLD_DUMPED)
324 SIGCLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally",
326 #if defined (SIGCLD) && defined (CLD_TRAPPED)
327 SIGCLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped",
329 #if defined (SIGCLD) && defined (CLD_STOPPED)
330 SIGCLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped",
332 #if defined (SIGCLD) && defined (CLD_CONTINUED)
333 SIGCLD, CLD_CONTINUED, "CLD_CONTINUED", "Stopped child had continued",
335 #if defined (SIGPOLL) && defined (POLL_IN)
336 SIGPOLL, POLL_IN, "POLL_IN", "Input input available",
338 #if defined (SIGPOLL) && defined (POLL_OUT)
339 SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available",
341 #if defined (SIGPOLL) && defined (POLL_MSG)
342 SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available",
344 #if defined (SIGPOLL) && defined (POLL_ERR)
345 SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error",
347 #if defined (SIGPOLL) && defined (POLL_PRI)
348 SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available",
350 #if defined (SIGPOLL) && defined (POLL_HUP)
351 SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected",
356 static char *syscall_table[MAX_SYSCALLS];
358 /* Prototypes for local functions */
361 set_proc_siginfo PARAMS ((struct procinfo *, int));
364 init_syscall_table PARAMS ((void));
367 syscallname PARAMS ((int));
370 signalname PARAMS ((int));
373 errnoname PARAMS ((int));
376 proc_address_to_fd PARAMS ((struct procinfo *, CORE_ADDR, int));
379 open_proc_file PARAMS ((int, struct procinfo *, int));
382 close_proc_file PARAMS ((struct procinfo *));
385 unconditionally_kill_inferior PARAMS ((struct procinfo *));
388 proc_init_failed PARAMS ((struct procinfo *, char *));
391 info_proc PARAMS ((char *, int));
394 info_proc_flags PARAMS ((struct procinfo *, int));
397 info_proc_stop PARAMS ((struct procinfo *, int));
400 info_proc_siginfo PARAMS ((struct procinfo *, int));
403 info_proc_syscalls PARAMS ((struct procinfo *, int));
406 info_proc_mappings PARAMS ((struct procinfo *, int));
409 info_proc_signals PARAMS ((struct procinfo *, int));
412 info_proc_faults PARAMS ((struct procinfo *, int));
415 mappingflags PARAMS ((long));
418 lookupname PARAMS ((struct trans *, unsigned int, char *));
421 lookupdesc PARAMS ((struct trans *, unsigned int));
424 do_attach PARAMS ((int pid));
427 do_detach PARAMS ((int siggnal));
430 procfs_create_inferior PARAMS ((char *, char *, char **));
433 procfs_notice_signals PARAMS ((int pid));
435 static struct procinfo *
436 find_procinfo PARAMS ((pid_t pid, int okfail));
438 /* External function prototypes that can't be easily included in any
439 header file because the args are typedefs in system include files. */
442 supply_gregset PARAMS ((gregset_t *));
445 fill_gregset PARAMS ((gregset_t *, int));
448 supply_fpregset PARAMS ((fpregset_t *));
451 fill_fpregset PARAMS ((fpregset_t *, int));
457 find_procinfo -- convert a process id to a struct procinfo
461 static struct procinfo * find_procinfo (pid_t pid, int okfail);
465 Given a process id, look it up in the procinfo chain. Returns
466 a struct procinfo *. If can't find pid, then call error(),
467 unless okfail is set, in which case, return NULL;
470 static struct procinfo *
471 find_procinfo (pid, okfail)
475 struct procinfo *procinfo;
477 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
478 if (procinfo->pid == pid)
484 error ("procfs (find_procinfo): Couldn't locate pid %d", pid);
491 current_procinfo -- convert inferior_pid to a struct procinfo
495 static struct procinfo * current_procinfo;
499 Looks up inferior_pid in the procinfo chain. Always returns a
500 struct procinfo *. If process can't be found, we error() out.
503 #define current_procinfo find_procinfo (inferior_pid, 0)
509 add_fd -- Add the fd to the poll/select list
513 static void add_fd (struct procinfo *);
517 Add the fd of the supplied procinfo to the list of fds used for
518 poll/select operations.
525 if (num_poll_list <= 0)
526 poll_list = (struct pollfd *) xmalloc (sizeof (struct pollfd));
528 poll_list = (struct pollfd *) xrealloc (poll_list,
530 * sizeof (struct pollfd));
531 poll_list[num_poll_list].fd = pi->fd;
532 poll_list[num_poll_list].events = POLLPRI;
543 for (i = 0; i < num_poll_list; i++)
545 if (poll_list[i].fd == pi->fd)
547 if (i != num_poll_list - 1)
548 memcpy (poll_list, poll_list + i + 1,
549 (num_poll_list - i - 1) * sizeof (struct pollfd));
553 if (num_poll_list == 0)
556 poll_list = (struct pollfd *) xrealloc (poll_list,
558 * sizeof (struct pollfd));
564 #define LOSING_POLL unixware_sux
566 static struct procinfo *
574 set_sigint_trap (); /* Causes SIGINT to be passed on to the
579 num_fds = poll (poll_list, num_poll_list, -1);
581 pi = current_procinfo;
583 while (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0)
587 print_sys_errmsg (pi->pathname, errno);
588 error ("PIOCWSTOP failed");
602 print_sys_errmsg ("poll failed\n", errno);
603 error ("Poll failed, returned %d", num_fds);
606 for (i = 0; i < num_poll_list && num_fds > 0; i++)
608 if ((poll_list[i].revents & (POLLPRI|POLLERR|POLLHUP|POLLNVAL)) == 0)
610 for (pi = procinfo_list; pi; pi = pi->next)
612 if (poll_list[i].fd == pi->fd)
614 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
616 print_sys_errmsg (pi->pathname, errno);
617 error ("PIOCSTATUS failed");
625 error ("procfs_wait: Couldn't find procinfo for fd %d\n",
628 #endif /* LOSING_POLL */
637 lookupdesc -- translate a value to a summary desc string
641 static char *lookupdesc (struct trans *transp, unsigned int val);
645 Given a pointer to a translation table and a value to be translated,
646 lookup the desc string and return it.
650 lookupdesc (transp, val)
651 struct trans *transp;
656 for (desc = NULL; transp -> name != NULL; transp++)
658 if (transp -> value == val)
660 desc = transp -> desc;
665 /* Didn't find a translation for the specified value, set a default one. */
678 lookupname -- translate a value to symbolic name
682 static char *lookupname (struct trans *transp, unsigned int val,
687 Given a pointer to a translation table, a value to be translated,
688 and a default prefix to return if the value can't be translated,
689 match the value with one of the translation table entries and
690 return a pointer to the symbolic name.
692 If no match is found it just returns the value as a printable string,
693 with the given prefix. The previous such value, if any, is freed
698 lookupname (transp, val, prefix)
699 struct trans *transp;
706 for (name = NULL; transp -> name != NULL; transp++)
708 if (transp -> value == val)
710 name = transp -> name;
715 /* Didn't find a translation for the specified value, build a default
716 one using the specified prefix and return it. The lifetime of
717 the value is only until the next one is needed. */
725 locbuf = xmalloc (strlen (prefix) + 16);
726 sprintf (locbuf, "%s %u", prefix, val);
738 static char locbuf[32];
740 for (scp = siginfo_table; scp -> codename != NULL; scp++)
742 if ((scp -> signo == sip -> si_signo) &&
743 (scp -> code == sip -> si_code))
745 name = scp -> codename;
751 sprintf (locbuf, "sigcode %u", sip -> si_signo);
764 for (scp = siginfo_table; scp -> codename != NULL; scp++)
766 if ((scp -> signo == sip -> si_signo) &&
767 (scp -> code == sip -> si_code))
775 desc = "Unrecognized signal or trap use";
784 syscallname - translate a system call number into a system call name
788 char *syscallname (int syscallnum)
792 Given a system call number, translate it into the printable name
793 of a system call, or into "syscall <num>" if it is an unknown
798 syscallname (syscallnum)
801 static char locbuf[32];
804 if (syscallnum >= 0 && syscallnum < MAX_SYSCALLS)
806 rtnval = syscall_table[syscallnum];
810 sprintf (locbuf, "syscall %u", syscallnum);
820 init_syscall_table - initialize syscall translation table
824 void init_syscall_table (void)
828 Dynamically initialize the translation table to convert system
829 call numbers into printable system call names. Done once per
830 gdb run, on initialization.
834 This is awfully ugly, but preprocessor tricks to make it prettier
835 tend to be nonportable.
839 init_syscall_table ()
841 #if defined (SYS_exit)
842 syscall_table[SYS_exit] = "exit";
844 #if defined (SYS_fork)
845 syscall_table[SYS_fork] = "fork";
847 #if defined (SYS_read)
848 syscall_table[SYS_read] = "read";
850 #if defined (SYS_write)
851 syscall_table[SYS_write] = "write";
853 #if defined (SYS_open)
854 syscall_table[SYS_open] = "open";
856 #if defined (SYS_close)
857 syscall_table[SYS_close] = "close";
859 #if defined (SYS_wait)
860 syscall_table[SYS_wait] = "wait";
862 #if defined (SYS_creat)
863 syscall_table[SYS_creat] = "creat";
865 #if defined (SYS_link)
866 syscall_table[SYS_link] = "link";
868 #if defined (SYS_unlink)
869 syscall_table[SYS_unlink] = "unlink";
871 #if defined (SYS_exec)
872 syscall_table[SYS_exec] = "exec";
874 #if defined (SYS_execv)
875 syscall_table[SYS_execv] = "execv";
877 #if defined (SYS_execve)
878 syscall_table[SYS_execve] = "execve";
880 #if defined (SYS_chdir)
881 syscall_table[SYS_chdir] = "chdir";
883 #if defined (SYS_time)
884 syscall_table[SYS_time] = "time";
886 #if defined (SYS_mknod)
887 syscall_table[SYS_mknod] = "mknod";
889 #if defined (SYS_chmod)
890 syscall_table[SYS_chmod] = "chmod";
892 #if defined (SYS_chown)
893 syscall_table[SYS_chown] = "chown";
895 #if defined (SYS_brk)
896 syscall_table[SYS_brk] = "brk";
898 #if defined (SYS_stat)
899 syscall_table[SYS_stat] = "stat";
901 #if defined (SYS_lseek)
902 syscall_table[SYS_lseek] = "lseek";
904 #if defined (SYS_getpid)
905 syscall_table[SYS_getpid] = "getpid";
907 #if defined (SYS_mount)
908 syscall_table[SYS_mount] = "mount";
910 #if defined (SYS_umount)
911 syscall_table[SYS_umount] = "umount";
913 #if defined (SYS_setuid)
914 syscall_table[SYS_setuid] = "setuid";
916 #if defined (SYS_getuid)
917 syscall_table[SYS_getuid] = "getuid";
919 #if defined (SYS_stime)
920 syscall_table[SYS_stime] = "stime";
922 #if defined (SYS_ptrace)
923 syscall_table[SYS_ptrace] = "ptrace";
925 #if defined (SYS_alarm)
926 syscall_table[SYS_alarm] = "alarm";
928 #if defined (SYS_fstat)
929 syscall_table[SYS_fstat] = "fstat";
931 #if defined (SYS_pause)
932 syscall_table[SYS_pause] = "pause";
934 #if defined (SYS_utime)
935 syscall_table[SYS_utime] = "utime";
937 #if defined (SYS_stty)
938 syscall_table[SYS_stty] = "stty";
940 #if defined (SYS_gtty)
941 syscall_table[SYS_gtty] = "gtty";
943 #if defined (SYS_access)
944 syscall_table[SYS_access] = "access";
946 #if defined (SYS_nice)
947 syscall_table[SYS_nice] = "nice";
949 #if defined (SYS_statfs)
950 syscall_table[SYS_statfs] = "statfs";
952 #if defined (SYS_sync)
953 syscall_table[SYS_sync] = "sync";
955 #if defined (SYS_kill)
956 syscall_table[SYS_kill] = "kill";
958 #if defined (SYS_fstatfs)
959 syscall_table[SYS_fstatfs] = "fstatfs";
961 #if defined (SYS_pgrpsys)
962 syscall_table[SYS_pgrpsys] = "pgrpsys";
964 #if defined (SYS_xenix)
965 syscall_table[SYS_xenix] = "xenix";
967 #if defined (SYS_dup)
968 syscall_table[SYS_dup] = "dup";
970 #if defined (SYS_pipe)
971 syscall_table[SYS_pipe] = "pipe";
973 #if defined (SYS_times)
974 syscall_table[SYS_times] = "times";
976 #if defined (SYS_profil)
977 syscall_table[SYS_profil] = "profil";
979 #if defined (SYS_plock)
980 syscall_table[SYS_plock] = "plock";
982 #if defined (SYS_setgid)
983 syscall_table[SYS_setgid] = "setgid";
985 #if defined (SYS_getgid)
986 syscall_table[SYS_getgid] = "getgid";
988 #if defined (SYS_signal)
989 syscall_table[SYS_signal] = "signal";
991 #if defined (SYS_msgsys)
992 syscall_table[SYS_msgsys] = "msgsys";
994 #if defined (SYS_sys3b)
995 syscall_table[SYS_sys3b] = "sys3b";
997 #if defined (SYS_acct)
998 syscall_table[SYS_acct] = "acct";
1000 #if defined (SYS_shmsys)
1001 syscall_table[SYS_shmsys] = "shmsys";
1003 #if defined (SYS_semsys)
1004 syscall_table[SYS_semsys] = "semsys";
1006 #if defined (SYS_ioctl)
1007 syscall_table[SYS_ioctl] = "ioctl";
1009 #if defined (SYS_uadmin)
1010 syscall_table[SYS_uadmin] = "uadmin";
1012 #if defined (SYS_utssys)
1013 syscall_table[SYS_utssys] = "utssys";
1015 #if defined (SYS_fsync)
1016 syscall_table[SYS_fsync] = "fsync";
1018 #if defined (SYS_umask)
1019 syscall_table[SYS_umask] = "umask";
1021 #if defined (SYS_chroot)
1022 syscall_table[SYS_chroot] = "chroot";
1024 #if defined (SYS_fcntl)
1025 syscall_table[SYS_fcntl] = "fcntl";
1027 #if defined (SYS_ulimit)
1028 syscall_table[SYS_ulimit] = "ulimit";
1030 #if defined (SYS_rfsys)
1031 syscall_table[SYS_rfsys] = "rfsys";
1033 #if defined (SYS_rmdir)
1034 syscall_table[SYS_rmdir] = "rmdir";
1036 #if defined (SYS_mkdir)
1037 syscall_table[SYS_mkdir] = "mkdir";
1039 #if defined (SYS_getdents)
1040 syscall_table[SYS_getdents] = "getdents";
1042 #if defined (SYS_sysfs)
1043 syscall_table[SYS_sysfs] = "sysfs";
1045 #if defined (SYS_getmsg)
1046 syscall_table[SYS_getmsg] = "getmsg";
1048 #if defined (SYS_putmsg)
1049 syscall_table[SYS_putmsg] = "putmsg";
1051 #if defined (SYS_poll)
1052 syscall_table[SYS_poll] = "poll";
1054 #if defined (SYS_lstat)
1055 syscall_table[SYS_lstat] = "lstat";
1057 #if defined (SYS_symlink)
1058 syscall_table[SYS_symlink] = "symlink";
1060 #if defined (SYS_readlink)
1061 syscall_table[SYS_readlink] = "readlink";
1063 #if defined (SYS_setgroups)
1064 syscall_table[SYS_setgroups] = "setgroups";
1066 #if defined (SYS_getgroups)
1067 syscall_table[SYS_getgroups] = "getgroups";
1069 #if defined (SYS_fchmod)
1070 syscall_table[SYS_fchmod] = "fchmod";
1072 #if defined (SYS_fchown)
1073 syscall_table[SYS_fchown] = "fchown";
1075 #if defined (SYS_sigprocmask)
1076 syscall_table[SYS_sigprocmask] = "sigprocmask";
1078 #if defined (SYS_sigsuspend)
1079 syscall_table[SYS_sigsuspend] = "sigsuspend";
1081 #if defined (SYS_sigaltstack)
1082 syscall_table[SYS_sigaltstack] = "sigaltstack";
1084 #if defined (SYS_sigaction)
1085 syscall_table[SYS_sigaction] = "sigaction";
1087 #if defined (SYS_sigpending)
1088 syscall_table[SYS_sigpending] = "sigpending";
1090 #if defined (SYS_context)
1091 syscall_table[SYS_context] = "context";
1093 #if defined (SYS_evsys)
1094 syscall_table[SYS_evsys] = "evsys";
1096 #if defined (SYS_evtrapret)
1097 syscall_table[SYS_evtrapret] = "evtrapret";
1099 #if defined (SYS_statvfs)
1100 syscall_table[SYS_statvfs] = "statvfs";
1102 #if defined (SYS_fstatvfs)
1103 syscall_table[SYS_fstatvfs] = "fstatvfs";
1105 #if defined (SYS_nfssys)
1106 syscall_table[SYS_nfssys] = "nfssys";
1108 #if defined (SYS_waitsys)
1109 syscall_table[SYS_waitsys] = "waitsys";
1111 #if defined (SYS_sigsendsys)
1112 syscall_table[SYS_sigsendsys] = "sigsendsys";
1114 #if defined (SYS_hrtsys)
1115 syscall_table[SYS_hrtsys] = "hrtsys";
1117 #if defined (SYS_acancel)
1118 syscall_table[SYS_acancel] = "acancel";
1120 #if defined (SYS_async)
1121 syscall_table[SYS_async] = "async";
1123 #if defined (SYS_priocntlsys)
1124 syscall_table[SYS_priocntlsys] = "priocntlsys";
1126 #if defined (SYS_pathconf)
1127 syscall_table[SYS_pathconf] = "pathconf";
1129 #if defined (SYS_mincore)
1130 syscall_table[SYS_mincore] = "mincore";
1132 #if defined (SYS_mmap)
1133 syscall_table[SYS_mmap] = "mmap";
1135 #if defined (SYS_mprotect)
1136 syscall_table[SYS_mprotect] = "mprotect";
1138 #if defined (SYS_munmap)
1139 syscall_table[SYS_munmap] = "munmap";
1141 #if defined (SYS_fpathconf)
1142 syscall_table[SYS_fpathconf] = "fpathconf";
1144 #if defined (SYS_vfork)
1145 syscall_table[SYS_vfork] = "vfork";
1147 #if defined (SYS_fchdir)
1148 syscall_table[SYS_fchdir] = "fchdir";
1150 #if defined (SYS_readv)
1151 syscall_table[SYS_readv] = "readv";
1153 #if defined (SYS_writev)
1154 syscall_table[SYS_writev] = "writev";
1156 #if defined (SYS_xstat)
1157 syscall_table[SYS_xstat] = "xstat";
1159 #if defined (SYS_lxstat)
1160 syscall_table[SYS_lxstat] = "lxstat";
1162 #if defined (SYS_fxstat)
1163 syscall_table[SYS_fxstat] = "fxstat";
1165 #if defined (SYS_xmknod)
1166 syscall_table[SYS_xmknod] = "xmknod";
1168 #if defined (SYS_clocal)
1169 syscall_table[SYS_clocal] = "clocal";
1171 #if defined (SYS_setrlimit)
1172 syscall_table[SYS_setrlimit] = "setrlimit";
1174 #if defined (SYS_getrlimit)
1175 syscall_table[SYS_getrlimit] = "getrlimit";
1177 #if defined (SYS_lchown)
1178 syscall_table[SYS_lchown] = "lchown";
1180 #if defined (SYS_memcntl)
1181 syscall_table[SYS_memcntl] = "memcntl";
1183 #if defined (SYS_getpmsg)
1184 syscall_table[SYS_getpmsg] = "getpmsg";
1186 #if defined (SYS_putpmsg)
1187 syscall_table[SYS_putpmsg] = "putpmsg";
1189 #if defined (SYS_rename)
1190 syscall_table[SYS_rename] = "rename";
1192 #if defined (SYS_uname)
1193 syscall_table[SYS_uname] = "uname";
1195 #if defined (SYS_setegid)
1196 syscall_table[SYS_setegid] = "setegid";
1198 #if defined (SYS_sysconfig)
1199 syscall_table[SYS_sysconfig] = "sysconfig";
1201 #if defined (SYS_adjtime)
1202 syscall_table[SYS_adjtime] = "adjtime";
1204 #if defined (SYS_systeminfo)
1205 syscall_table[SYS_systeminfo] = "systeminfo";
1207 #if defined (SYS_seteuid)
1208 syscall_table[SYS_seteuid] = "seteuid";
1210 #if defined (SYS_sproc)
1211 syscall_table[SYS_sproc] = "sproc";
1219 procfs_kill_inferior - kill any currently inferior
1223 void procfs_kill_inferior (void)
1227 Kill any current inferior.
1231 Kills even attached inferiors. Presumably the user has already
1232 been prompted that the inferior is an attached one rather than
1233 one started by gdb. (FIXME?)
1238 procfs_kill_inferior ()
1240 target_mourn_inferior ();
1247 unconditionally_kill_inferior - terminate the inferior
1251 static void unconditionally_kill_inferior (struct procinfo *)
1255 Kill the specified inferior.
1259 A possibly useful enhancement would be to first try sending
1260 the inferior a terminate signal, politely asking it to commit
1261 suicide, before we murder it (we could call that
1262 politely_kill_inferior()).
1267 unconditionally_kill_inferior (pi)
1268 struct procinfo *pi;
1273 ppid = pi->prstatus.pr_ppid;
1276 ioctl (pi->fd, PIOCKILL, &signo);
1277 close_proc_file (pi);
1279 /* Only wait() for our direct children. Our grandchildren zombies are killed
1280 by the death of their parents. */
1282 if (ppid == getpid())
1290 procfs_xfer_memory -- copy data to or from inferior memory space
1294 int procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
1295 int dowrite, struct target_ops target)
1299 Copy LEN bytes to/from inferior's memory starting at MEMADDR
1300 from/to debugger memory starting at MYADDR. Copy from inferior
1301 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
1303 Returns the length copied, which is either the LEN argument or
1304 zero. This xfer function does not do partial moves, since procfs_ops
1305 doesn't allow memory operations to cross below us in the target stack
1310 The /proc interface makes this an almost trivial task.
1314 procfs_xfer_memory (memaddr, myaddr, len, dowrite, target)
1319 struct target_ops *target; /* ignored */
1322 struct procinfo *pi;
1324 pi = current_procinfo;
1326 if (lseek(pi->fd, (off_t) memaddr, 0) == (off_t) memaddr)
1330 nbytes = write (pi->fd, myaddr, len);
1334 nbytes = read (pi->fd, myaddr, len);
1348 procfs_store_registers -- copy register values back to inferior
1352 void procfs_store_registers (int regno)
1356 Store our current register values back into the inferior. If
1357 REGNO is -1 then store all the register, otherwise store just
1358 the value specified by REGNO.
1362 If we are storing only a single register, we first have to get all
1363 the current values from the process, overwrite the desired register
1364 in the gregset with the one we want from gdb's registers, and then
1365 send the whole set back to the process. For writing all the
1366 registers, all we have to do is generate the gregset and send it to
1369 Also note that the process has to be stopped on an event of interest
1370 for this to work, which basically means that it has to have been
1371 run under the control of one of the other /proc ioctl calls and not
1372 ptrace. Since we don't use ptrace anyway, we don't worry about this
1373 fine point, but it is worth noting for future reference.
1375 Gdb is confused about what this function is supposed to return.
1376 Some versions return a value, others return nothing. Some are
1377 declared to return a value and actually return nothing. Gdb ignores
1378 anything returned. (FIXME)
1383 procfs_store_registers (regno)
1386 struct procinfo *pi;
1388 pi = current_procinfo;
1392 ioctl (pi->fd, PIOCGREG, &pi->gregset);
1394 fill_gregset (&pi->gregset, regno);
1395 ioctl (pi->fd, PIOCSREG, &pi->gregset);
1397 #if defined (FP0_REGNUM)
1399 /* Now repeat everything using the floating point register set, if the
1400 target has floating point hardware. Since we ignore the returned value,
1401 we'll never know whether it worked or not anyway. */
1405 ioctl (pi->fd, PIOCGFPREG, &pi->fpregset);
1407 fill_fpregset (&pi->fpregset, regno);
1408 ioctl (pi->fd, PIOCSFPREG, &pi->fpregset);
1410 #endif /* FP0_REGNUM */
1418 create_procinfo - initialize access to a /proc entry
1422 void create_procinfo (int pid)
1426 Allocate a procinfo structure, open the /proc file and then sets up
1427 the set of signals and faults that are to be traced.
1431 If proc_init_failed ever gets called, control returns to the command
1432 processing loop via the standard error handling code.
1437 create_procinfo (pid)
1440 struct procinfo *pi;
1442 if (find_procinfo (pid, 1))
1443 return; /* All done! It already exists */
1445 pi = (struct procinfo *) xmalloc (sizeof (struct procinfo));
1447 if (!open_proc_file (pid, pi, O_RDWR))
1448 proc_init_failed (pi, "can't open process file");
1450 /* Add new process to process info list */
1452 pi->next = procinfo_list;
1455 add_fd (pi); /* Add to list for poll/select */
1457 memset ((char *) &pi->prrun, 0, sizeof (pi->prrun));
1458 prfillset (&pi->prrun.pr_trace);
1459 procfs_notice_signals (pid);
1460 prfillset (&pi->prrun.pr_fault);
1461 prdelset (&pi->prrun.pr_fault, FLTPAGE);
1463 if (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0)
1464 proc_init_failed (pi, "PIOCWSTOP failed");
1466 if (ioctl (pi->fd, PIOCSFAULT, &pi->prrun.pr_fault) < 0)
1467 proc_init_failed (pi, "PIOCSFAULT failed");
1474 procfs_init_inferior - initialize target vector and access to a
1479 void procfs_init_inferior (int pid)
1483 When gdb starts an inferior, this function is called in the parent
1484 process immediately after the fork. It waits for the child to stop
1485 on the return from the exec system call (the child itself takes care
1486 of ensuring that this is set up), then sets up the set of signals
1487 and faults that are to be traced.
1491 If proc_init_failed ever gets called, control returns to the command
1492 processing loop via the standard error handling code.
1497 procfs_init_inferior (pid)
1500 push_target (&procfs_ops);
1502 create_procinfo (pid);
1503 add_thread (pid); /* Setup initial thread */
1505 /* One trap to exec the shell, one to exec the program being debugged. */
1506 startup_inferior (2);
1513 procfs_notice_signals
1517 static void procfs_notice_signals (int pid);
1521 When the user changes the state of gdb's signal handling via the
1522 "handle" command, this function gets called to see if any change
1523 in the /proc interface is required. It is also called internally
1524 by other /proc interface functions to initialize the state of
1525 the traced signal set.
1527 One thing it does is that signals for which the state is "nostop",
1528 "noprint", and "pass", have their trace bits reset in the pr_trace
1529 field, so that they are no longer traced. This allows them to be
1530 delivered directly to the inferior without the debugger ever being
1535 procfs_notice_signals (pid)
1539 struct procinfo *pi;
1541 pi = find_procinfo (pid, 0);
1543 for (signo = 0; signo < NSIG; signo++)
1545 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
1546 signal_print_state (target_signal_from_host (signo)) == 0 &&
1547 signal_pass_state (target_signal_from_host (signo)) == 1)
1549 prdelset (&pi->prrun.pr_trace, signo);
1553 praddset (&pi->prrun.pr_trace, signo);
1556 if (ioctl (pi->fd, PIOCSTRACE, &pi->prrun.pr_trace))
1558 print_sys_errmsg ("PIOCSTRACE failed", errno);
1566 proc_set_exec_trap -- arrange for exec'd child to halt at startup
1570 void proc_set_exec_trap (void)
1574 This function is called in the child process when starting up
1575 an inferior, prior to doing the exec of the actual inferior.
1576 It sets the child process's exitset to make exit from the exec
1577 system call an event of interest to stop on, and then simply
1578 returns. The child does the exec, the system call returns, and
1579 the child stops at the first instruction, ready for the gdb
1580 parent process to take control of it.
1584 We need to use all local variables since the child may be sharing
1585 it's data space with the parent, if vfork was used rather than
1588 Also note that we want to turn off the inherit-on-fork flag in
1589 the child process so that any grand-children start with all
1590 tracing flags cleared.
1594 proc_set_exec_trap ()
1598 auto char procname[32];
1601 sprintf (procname, PROC_NAME_FMT, getpid ());
1602 if ((fd = open (procname, O_RDWR)) < 0)
1605 gdb_flush (gdb_stderr);
1608 premptyset (&exitset);
1609 premptyset (&entryset);
1612 Not all systems with /proc have all the exec* syscalls with the same
1613 names. On the SGI, for example, there is no SYS_exec, but there
1614 *is* a SYS_execv. So, we try to account for that. */
1617 praddset (&exitset, SYS_exec);
1620 praddset (&exitset, SYS_execve);
1623 praddset (&exitset, SYS_execv);
1626 if (ioctl (fd, PIOCSEXIT, &exitset) < 0)
1629 gdb_flush (gdb_stderr);
1633 praddset (&entryset, SYS_exit);
1635 if (ioctl (fd, PIOCSENTRY, &entryset) < 0)
1638 gdb_flush (gdb_stderr);
1642 /* Turn off inherit-on-fork flag so that all grand-children of gdb
1643 start with tracing flags cleared. */
1645 #if defined (PIOCRESET) /* New method */
1649 ioctl (fd, PIOCRESET, &pr_flags);
1652 #if defined (PIOCRFORK) /* Original method */
1653 ioctl (fd, PIOCRFORK, NULL);
1657 /* Turn on run-on-last-close flag so that this process will not hang
1658 if GDB goes away for some reason. */
1660 #if defined (PIOCSET) /* New method */
1664 (void) ioctl (fd, PIOCSET, &pr_flags);
1667 #if defined (PIOCSRLC) /* Original method */
1668 (void) ioctl (fd, PIOCSRLC, 0);
1677 proc_iterate_over_mappings -- call function for every mapped space
1681 int proc_iterate_over_mappings (int (*func)())
1685 Given a pointer to a function, call that function for every
1686 mapped address space, passing it an open file descriptor for
1687 the file corresponding to that mapped address space (if any)
1688 and the base address of the mapped space. Quit when we hit
1689 the end of the mappings or the function returns nonzero.
1693 proc_iterate_over_mappings (func)
1694 int (*func) PARAMS ((int, CORE_ADDR));
1699 struct prmap *prmaps;
1700 struct prmap *prmap;
1701 struct procinfo *pi;
1703 pi = current_procinfo;
1705 if (ioctl (pi->fd, PIOCNMAP, &nmap) == 0)
1707 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
1708 if (ioctl (pi->fd, PIOCMAP, prmaps) == 0)
1710 for (prmap = prmaps; prmap -> pr_size && funcstat == 0; ++prmap)
1712 fd = proc_address_to_fd (pi, (CORE_ADDR) prmap -> pr_vaddr, 0);
1713 funcstat = (*func) (fd, (CORE_ADDR) prmap -> pr_vaddr);
1721 #if 0 /* Currently unused */
1726 proc_base_address -- find base address for segment containing address
1730 CORE_ADDR proc_base_address (CORE_ADDR addr)
1734 Given an address of a location in the inferior, find and return
1735 the base address of the mapped segment containing that address.
1737 This is used for example, by the shared library support code,
1738 where we have the pc value for some location in the shared library
1739 where we are stopped, and need to know the base address of the
1740 segment containing that address.
1744 proc_base_address (addr)
1748 struct prmap *prmaps;
1749 struct prmap *prmap;
1750 CORE_ADDR baseaddr = 0;
1751 struct procinfo *pi;
1753 pi = current_procinfo;
1755 if (ioctl (pi->fd, PIOCNMAP, &nmap) == 0)
1757 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
1758 if (ioctl (pi->fd, PIOCMAP, prmaps) == 0)
1760 for (prmap = prmaps; prmap -> pr_size; ++prmap)
1762 if ((prmap -> pr_vaddr <= (caddr_t) addr) &&
1763 (prmap -> pr_vaddr + prmap -> pr_size > (caddr_t) addr))
1765 baseaddr = (CORE_ADDR) prmap -> pr_vaddr;
1780 proc_address_to_fd -- return open fd for file mapped to address
1784 int proc_address_to_fd (struct procinfo *pi, CORE_ADDR addr, complain)
1788 Given an address in the current inferior's address space, use the
1789 /proc interface to find an open file descriptor for the file that
1790 this address was mapped in from. Return -1 if there is no current
1791 inferior. Print a warning message if there is an inferior but
1792 the address corresponds to no file (IE a bogus address).
1797 proc_address_to_fd (pi, addr, complain)
1798 struct procinfo *pi;
1804 if ((fd = ioctl (pi->fd, PIOCOPENM, (caddr_t *) &addr)) < 0)
1808 print_sys_errmsg (pi->pathname, errno);
1809 warning ("can't find mapped file for address 0x%x", addr);
1816 /* Attach to process PID, then initialize for debugging it
1817 and wait for the trace-trap that results from attaching. */
1820 procfs_attach (args, from_tty)
1828 error_no_arg ("process-id to attach");
1832 if (pid == getpid()) /* Trying to masturbate? */
1833 error ("I refuse to debug myself!");
1837 exec_file = (char *) get_exec_file (0);
1840 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, target_pid_to_str (pid));
1842 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid));
1844 gdb_flush (gdb_stdout);
1849 push_target (&procfs_ops);
1853 /* Take a program previously attached to and detaches it.
1854 The program resumes execution and will no longer stop
1855 on signals, etc. We'd better not have left any breakpoints
1856 in the program or it'll die when it hits one. For this
1857 to work, it may be necessary for the process to have been
1858 previously attached. It *might* work if the program was
1859 started via the normal ptrace (PTRACE_TRACEME). */
1862 procfs_detach (args, from_tty)
1870 char *exec_file = get_exec_file (0);
1873 printf_unfiltered ("Detaching from program: %s %s\n",
1874 exec_file, target_pid_to_str (inferior_pid));
1875 gdb_flush (gdb_stdout);
1878 siggnal = atoi (args);
1880 do_detach (siggnal);
1882 unpush_target (&procfs_ops); /* Pop out of handling an inferior */
1885 /* Get ready to modify the registers array. On machines which store
1886 individual registers, this doesn't need to do anything. On machines
1887 which store all the registers in one fell swoop, this makes sure
1888 that registers contains all the registers from the program being
1892 procfs_prepare_to_store ()
1894 #ifdef CHILD_PREPARE_TO_STORE
1895 CHILD_PREPARE_TO_STORE ();
1899 /* Print status information about what we're accessing. */
1902 procfs_files_info (ignore)
1903 struct target_ops *ignore;
1905 printf_unfiltered ("\tUsing the running image of %s %s via /proc.\n",
1906 attach_flag? "attached": "child", target_pid_to_str (inferior_pid));
1911 procfs_open (arg, from_tty)
1915 error ("Use the \"run\" command to start a Unix child process.");
1922 do_attach -- attach to an already existing process
1926 int do_attach (int pid)
1930 Attach to an already existing process with the specified process
1931 id. If the process is not already stopped, query whether to
1936 The option of stopping at attach time is specific to the /proc
1937 versions of gdb. Versions using ptrace force the attachee
1938 to stop. (I have changed this version to do so, too. All you
1939 have to do is "continue" to make it go on. -- gnu@cygnus.com)
1948 struct procinfo *pi;
1950 pi = (struct procinfo *) xmalloc (sizeof (struct procinfo));
1952 if (!open_proc_file (pid, pi, O_RDWR))
1955 perror_with_name (pi->pathname);
1959 /* Add new process to process info list */
1961 pi->next = procinfo_list;
1964 add_fd (pi); /* Add to list for poll/select */
1966 /* Get current status of process and if it is not already stopped,
1967 then stop it. Remember whether or not it was stopped when we first
1970 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
1972 print_sys_errmsg (pi->pathname, errno);
1973 close_proc_file (pi);
1974 error ("PIOCSTATUS failed");
1976 if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
1978 pi->was_stopped = 1;
1982 pi->was_stopped = 0;
1983 if (1 || query ("Process is currently running, stop it? "))
1985 /* Make it run again when we close it. */
1986 #if defined (PIOCSET) /* New method */
1990 result = ioctl (pi->fd, PIOCSET, &pr_flags);
1993 #if defined (PIOCSRLC) /* Original method */
1994 result = ioctl (pi->fd, PIOCSRLC, 0);
1999 print_sys_errmsg (pi->pathname, errno);
2000 close_proc_file (pi);
2001 error ("PIOCSRLC or PIOCSET failed");
2003 if (ioctl (pi->fd, PIOCSTOP, &pi->prstatus) < 0)
2005 print_sys_errmsg (pi->pathname, errno);
2006 close_proc_file (pi);
2007 error ("PIOCSTOP failed");
2009 pi->nopass_next_sigstop = 1;
2013 printf_unfiltered ("Ok, gdb will wait for %s to stop.\n", target_pid_to_str (pid));
2017 /* Remember some things about the inferior that we will, or might, change
2018 so that we can restore them when we detach. */
2020 ioctl (pi->fd, PIOCGTRACE, &pi->saved_trace);
2021 ioctl (pi->fd, PIOCGHOLD, &pi->saved_sighold);
2022 ioctl (pi->fd, PIOCGFAULT, &pi->saved_fltset);
2023 ioctl (pi->fd, PIOCGENTRY, &pi->saved_entryset);
2024 ioctl (pi->fd, PIOCGEXIT, &pi->saved_exitset);
2026 /* Set up trace and fault sets, as gdb expects them. */
2028 memset (&pi->prrun, 0, sizeof (pi->prrun));
2029 prfillset (&pi->prrun.pr_trace);
2030 procfs_notice_signals (pid);
2031 prfillset (&pi->prrun.pr_fault);
2032 prdelset (&pi->prrun.pr_fault, FLTPAGE);
2033 if (ioctl (pi->fd, PIOCSFAULT, &pi->prrun.pr_fault))
2035 print_sys_errmsg ("PIOCSFAULT failed", errno);
2037 if (ioctl (pi->fd, PIOCSTRACE, &pi->prrun.pr_trace))
2039 print_sys_errmsg ("PIOCSTRACE failed", errno);
2049 do_detach -- detach from an attached-to process
2053 void do_detach (int signal)
2057 Detach from the current attachee.
2059 If signal is non-zero, the attachee is started running again and sent
2060 the specified signal.
2062 If signal is zero and the attachee was not already stopped when we
2063 attached to it, then we make it runnable again when we detach.
2065 Otherwise, we query whether or not to make the attachee runnable
2066 again, since we may simply want to leave it in the state it was in
2069 We report any problems, but do not consider them errors, since we
2070 MUST detach even if some things don't seem to go right. This may not
2071 be the ideal situation. (FIXME).
2079 struct procinfo *pi;
2081 pi = current_procinfo;
2085 set_proc_siginfo (pi, signal);
2087 if (ioctl (pi->fd, PIOCSEXIT, &pi->saved_exitset) < 0)
2089 print_sys_errmsg (pi->pathname, errno);
2090 printf_unfiltered ("PIOCSEXIT failed.\n");
2092 if (ioctl (pi->fd, PIOCSENTRY, &pi->saved_entryset) < 0)
2094 print_sys_errmsg (pi->pathname, errno);
2095 printf_unfiltered ("PIOCSENTRY failed.\n");
2097 if (ioctl (pi->fd, PIOCSTRACE, &pi->saved_trace) < 0)
2099 print_sys_errmsg (pi->pathname, errno);
2100 printf_unfiltered ("PIOCSTRACE failed.\n");
2102 if (ioctl (pi->fd, PIOCSHOLD, &pi->saved_sighold) < 0)
2104 print_sys_errmsg (pi->pathname, errno);
2105 printf_unfiltered ("PIOSCHOLD failed.\n");
2107 if (ioctl (pi->fd, PIOCSFAULT, &pi->saved_fltset) < 0)
2109 print_sys_errmsg (pi->pathname, errno);
2110 printf_unfiltered ("PIOCSFAULT failed.\n");
2112 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
2114 print_sys_errmsg (pi->pathname, errno);
2115 printf_unfiltered ("PIOCSTATUS failed.\n");
2119 if (signal || (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
2121 if (signal || !pi->was_stopped ||
2122 query ("Was stopped when attached, make it runnable again? "))
2124 /* Clear any fault that might have stopped it. */
2125 if (ioctl (pi->fd, PIOCCFAULT, 0))
2127 print_sys_errmsg (pi->pathname, errno);
2128 printf_unfiltered ("PIOCCFAULT failed.\n");
2131 /* Make it run again when we close it. */
2132 #if defined (PIOCSET) /* New method */
2136 result = ioctl (pi->fd, PIOCSET, &pr_flags);
2139 #if defined (PIOCSRLC) /* Original method */
2140 result = ioctl (pi->fd, PIOCSRLC, 0);
2145 print_sys_errmsg (pi->pathname, errno);
2146 printf_unfiltered ("PIOCSRLC or PIOCSET failed.\n");
2151 close_proc_file (pi);
2155 /* emulate wait() as much as possible.
2156 Wait for child to do something. Return pid of child, or -1 in case
2157 of error; store status in *OURSTATUS.
2159 Not sure why we can't
2160 just use wait(), but it seems to have problems when applied to a
2161 process being controlled with the /proc interface.
2163 We have a race problem here with no obvious solution. We need to let
2164 the inferior run until it stops on an event of interest, which means
2165 that we need to use the PIOCWSTOP ioctl. However, we cannot use this
2166 ioctl if the process is already stopped on something that is not an
2167 event of interest, or the call will hang indefinitely. Thus we first
2168 use PIOCSTATUS to see if the process is not stopped. If not, then we
2169 use PIOCWSTOP. But during the window between the two, if the process
2170 stops for any reason that is not an event of interest (such as a job
2171 control signal) then gdb will hang. One possible workaround is to set
2172 an alarm to wake up every minute of so and check to see if the process
2173 is still running, and if so, then reissue the PIOCWSTOP. But this is
2174 a real kludge, so has not been implemented. FIXME: investigate
2177 FIXME: Investigate why wait() seems to have problems with programs
2178 being control by /proc routines. */
2181 procfs_wait (pid, ourstatus)
2183 struct target_waitstatus *ourstatus;
2190 struct procinfo *pi;
2192 if (pid != -1) /* Non-specific process? */
2195 for (pi = procinfo_list; pi; pi = pi->next)
2205 for (pi = procinfo_list; pi; pi = pi->next)
2206 if (pi->pid == pid && pi->had_event)
2209 if (!pi && !checkerr)
2212 if (!checkerr && !(pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
2214 if (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0)
2221 if (errno == ENOENT)
2223 rtnval = wait (&statval);
2224 if (rtnval != inferior_pid)
2226 print_sys_errmsg (pi->pathname, errno);
2227 error ("PIOCWSTOP, wait failed, returned %d", rtnval);
2233 print_sys_errmsg (pi->pathname, errno);
2234 error ("PIOCSTATUS or PIOCWSTOP failed.");
2238 else if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
2240 rtnval = pi->prstatus.pr_pid;
2241 why = pi->prstatus.pr_why;
2242 what = pi->prstatus.pr_what;
2247 statval = (what << 8) | 0177;
2250 if (what != SYS_exit)
2251 error ("PR_SYSENTRY, unknown system call %d", what);
2253 pi->prrun.pr_flags = PRCFAULT;
2255 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
2256 perror_with_name (pi->pathname);
2258 rtnval = wait (&statval);
2273 statval = (SIGTRAP << 8) | 0177;
2277 /* We've just detected the completion of an sproc system call. Now we need to
2278 setup a procinfo struct for this thread, and notify the thread system of the
2281 /* If sproc failed, then nothing interesting happened. Continue the process and
2282 go back to sleep. */
2284 if (pi->prstatus.pr_errno != 0)
2286 pi->prrun.pr_flags &= PRSTEP;
2287 pi->prrun.pr_flags |= PRCFAULT;
2289 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
2290 perror_with_name (pi->pathname);
2295 /* At this point, the new thread is stopped at it's first instruction, and
2296 the parent is stopped at the exit from sproc. */
2298 /* Notify the caller of the arrival of a new thread. */
2299 create_procinfo (pi->prstatus.pr_rval1);
2301 rtnval = pi->prstatus.pr_rval1;
2302 statval = (SIGTRAP << 8) | 0177;
2305 #endif /* SYS_sproc */
2308 error ("PIOCSTATUS (PR_SYSEXIT): Unknown system call %d", what);
2312 statval = (SIGSTOP << 8) | 0177;
2315 statval = (what << 8) | 0177;
2322 statval = (SIGTRAP << 8) | 0177;
2327 statval = (SIGTRAP << 8) | 0177;
2330 #ifndef FAULTED_USE_SIGINFO
2331 /* Irix, contrary to the documentation, fills in 0 for si_signo.
2332 Solaris fills in si_signo. I'm not sure about others. */
2335 statval = (SIGILL << 8) | 0177;
2339 statval = (SIGTRAP << 8) | 0177;
2344 statval = (SIGSEGV << 8) | 0177;
2349 statval = (SIGFPE << 8) | 0177;
2351 case FLTPAGE: /* Recoverable page fault */
2352 #endif /* not FAULTED_USE_SIGINFO */
2354 /* Use the signal which the kernel assigns. This is better than
2355 trying to second-guess it from the fault. In fact, I suspect
2356 that FLTACCESS can be either SIGSEGV or SIGBUS. */
2357 statval = ((pi->prstatus.pr_info.si_signo) << 8) | 0177;
2362 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
2364 /* Stop all the other threads when any of them stops. */
2367 struct procinfo *procinfo;
2369 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
2371 if (!procinfo->had_event)
2372 if (ioctl (procinfo->fd, PIOCSTOP, &procinfo->prstatus) < 0)
2374 print_sys_errmsg (procinfo->pathname, errno);
2375 error ("PIOCSTOP failed");
2382 error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x",
2383 pi->prstatus.pr_flags);
2386 store_waitstatus (ourstatus, statval);
2388 if (rtnval == -1) /* No more children to wait for */
2390 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing.\n");
2391 /* Claim it exited with unknown signal. */
2392 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2393 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
2397 pi->had_event = 0; /* Indicate that we've seen this one */
2405 set_proc_siginfo - set a process's current signal info
2409 void set_proc_siginfo (struct procinfo *pip, int signo);
2413 Given a pointer to a process info struct in PIP and a signal number
2414 in SIGNO, set the process's current signal and its associated signal
2415 information. The signal will be delivered to the process immediately
2416 after execution is resumed, even if it is being held. In addition,
2417 this particular delivery will not cause another PR_SIGNALLED stop
2418 even if the signal is being traced.
2420 If we are not delivering the same signal that the prstatus siginfo
2421 struct contains information about, then synthesize a siginfo struct
2422 to match the signal we are doing to deliver, make it of the type
2423 "generated by a user process", and send this synthesized copy. When
2424 used to set the inferior's signal state, this will be required if we
2425 are not currently stopped because of a traced signal, or if we decide
2426 to continue with a different signal.
2428 Note that when continuing the inferior from a stop due to receipt
2429 of a traced signal, we either have set PRCSIG to clear the existing
2430 signal, or we have to call this function to do a PIOCSSIG with either
2431 the existing siginfo struct from pr_info, or one we have synthesized
2432 appropriately for the signal we want to deliver. Otherwise if the
2433 signal is still being traced, the inferior will immediately stop
2436 See siginfo(5) for more details.
2440 set_proc_siginfo (pip, signo)
2441 struct procinfo *pip;
2444 struct siginfo newsiginfo;
2445 struct siginfo *sip;
2447 if (signo == pip -> prstatus.pr_info.si_signo)
2449 sip = &pip -> prstatus.pr_info;
2453 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
2455 sip -> si_signo = signo;
2457 sip -> si_errno = 0;
2458 sip -> si_pid = getpid ();
2459 sip -> si_uid = getuid ();
2461 if (ioctl (pip -> fd, PIOCSSIG, sip) < 0)
2463 print_sys_errmsg (pip -> pathname, errno);
2464 warning ("PIOCSSIG failed");
2468 /* Resume execution of process PID. If STEP is nozero, then
2469 just single step it. If SIGNAL is nonzero, restart it with that
2470 signal activated. */
2473 procfs_resume (pid, step, signo)
2476 enum target_signal signo;
2479 struct procinfo *pi, *procinfo;
2481 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
2484 pi->prrun.pr_flags = PRSTRACE | PRSFAULT | PRCFAULT;
2487 /* It should not be necessary. If the user explicitly changes the value,
2488 value_assign calls write_register_bytes, which writes it. */
2489 /* It may not be absolutely necessary to specify the PC value for
2490 restarting, but to be safe we use the value that gdb considers
2491 to be current. One case where this might be necessary is if the
2492 user explicitly changes the PC value that gdb considers to be
2493 current. FIXME: Investigate if this is necessary or not. */
2495 #ifdef PRSVADDR_BROKEN
2496 /* Can't do this under Solaris running on a Sparc, as there seems to be no
2497 place to put nPC. In fact, if you use this, nPC seems to be set to some
2498 random garbage. We have to rely on the fact that PC and nPC have been
2499 written previously via PIOCSREG during a register flush. */
2501 pi->prrun.pr_vaddr = (caddr_t) *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)];
2502 pi->prrun.pr_flags != PRSVADDR;
2506 if (signo == TARGET_SIGNAL_STOP && pi->nopass_next_sigstop)
2507 /* When attaching to a child process, if we forced it to stop with
2508 a PIOCSTOP, then we will have set the nopass_next_sigstop flag.
2509 Upon resuming the first time after such a stop, we explicitly
2510 inhibit sending it another SIGSTOP, which would be the normal
2511 result of default signal handling. One potential drawback to
2512 this is that we will also ignore any attempt to by the user
2513 to explicitly continue after the attach with a SIGSTOP. Ultimately
2514 this problem should be dealt with by making the routines that
2515 deal with the inferior a little smarter, and possibly even allow
2516 an inferior to continue running at the same time as gdb. (FIXME?) */
2518 else if (signo == TARGET_SIGNAL_TSTP
2519 && pi->prstatus.pr_cursig == SIGTSTP
2520 && pi->prstatus.pr_action.sa_handler == SIG_DFL)
2522 /* We are about to pass the inferior a SIGTSTP whose action is
2523 SIG_DFL. The SIG_DFL action for a SIGTSTP is to stop
2524 (notifying the parent via wait()), and then keep going from the
2525 same place when the parent is ready for you to keep going. So
2526 under the debugger, it should do nothing (as if the program had
2527 been stopped and then later resumed. Under ptrace, this
2528 happens for us, but under /proc, the system obligingly stops
2529 the process, and wait_for_inferior would have no way of
2530 distinguishing that type of stop (which indicates that we
2531 should just start it again), with a stop due to the pr_trace
2532 field of the prrun_t struct.
2534 Note that if the SIGTSTP is being caught, we *do* need to pass it,
2535 because the handler needs to get executed. */
2538 signal_to_pass = target_signal_to_host (signo);
2542 set_proc_siginfo (pi, signal_to_pass);
2546 pi->prrun.pr_flags |= PRCSIG;
2548 pi->nopass_next_sigstop = 0;
2551 pi->prrun.pr_flags |= PRSTEP;
2553 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
2555 perror_with_name (pi->pathname);
2561 /* Continue all the other threads that haven't had an event of
2565 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
2567 if (pi != procinfo && !procinfo->had_event)
2569 procinfo->prrun.pr_flags &= PRSTEP;
2570 procinfo->prrun.pr_flags |= PRCFAULT | PRCSIG;
2571 ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus);
2572 if (ioctl (procinfo->fd, PIOCRUN, &procinfo->prrun) < 0)
2574 if (ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus) < 0)
2576 fprintf_unfiltered(gdb_stderr, "PIOCSTATUS failed, errno=%d\n", errno);
2578 print_sys_errmsg (procinfo->pathname, errno);
2579 error ("PIOCRUN failed");
2581 ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus);
2590 procfs_fetch_registers -- fetch current registers from inferior
2594 void procfs_fetch_registers (int regno)
2598 Read the current values of the inferior's registers, both the
2599 general register set and floating point registers (if supported)
2600 and update gdb's idea of their current values.
2605 procfs_fetch_registers (regno)
2608 struct procinfo *pi;
2610 pi = current_procinfo;
2612 if (ioctl (pi->fd, PIOCGREG, &pi->gregset) != -1)
2614 supply_gregset (&pi->gregset);
2616 #if defined (FP0_REGNUM)
2617 if (ioctl (pi->fd, PIOCGFPREG, &pi->fpregset) != -1)
2619 supply_fpregset (&pi->fpregset);
2628 proc_init_failed - called whenever /proc access initialization
2633 static void proc_init_failed (struct procinfo *pi, char *why)
2637 This function is called whenever initialization of access to a /proc
2638 entry fails. It prints a suitable error message, does some cleanup,
2639 and then invokes the standard error processing routine which dumps
2640 us back into the command loop.
2644 proc_init_failed (pi, why)
2645 struct procinfo *pi;
2648 print_sys_errmsg (pi->pathname, errno);
2649 kill (pi->pid, SIGKILL);
2650 close_proc_file (pi);
2659 close_proc_file - close any currently open /proc entry
2663 static void close_proc_file (struct procinfo *pip)
2667 Close any currently open /proc entry and mark the process information
2668 entry as invalid. In order to ensure that we don't try to reuse any
2669 stale information, the pid, fd, and pathnames are explicitly
2670 invalidated, which may be overkill.
2675 close_proc_file (pip)
2676 struct procinfo *pip;
2678 struct procinfo *procinfo;
2680 remove_fd (pip); /* Remove fd from poll/select list */
2684 free (pip -> pathname);
2686 /* Unlink pip from the procinfo chain. Note pip might not be on the list. */
2688 if (procinfo_list == pip)
2689 procinfo_list = pip->next;
2691 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
2692 if (procinfo->next == pip)
2693 procinfo->next = pip->next;
2702 open_proc_file - open a /proc entry for a given process id
2706 static int open_proc_file (int pid, struct procinfo *pip, int mode)
2710 Given a process id and a mode, close the existing open /proc
2711 entry (if any) and open one for the new process id, in the
2712 specified mode. Once it is open, then mark the local process
2713 information structure as valid, which guarantees that the pid,
2714 fd, and pathname fields match an open /proc entry. Returns
2715 zero if the open fails, nonzero otherwise.
2717 Note that the pathname is left intact, even when the open fails,
2718 so that callers can use it to construct meaningful error messages
2719 rather than just "file open failed".
2723 open_proc_file (pid, pip, mode)
2725 struct procinfo *pip;
2729 pip -> had_event = 0;
2730 pip -> pathname = xmalloc (32);
2733 sprintf (pip -> pathname, PROC_NAME_FMT, pid);
2734 if ((pip -> fd = open (pip -> pathname, mode)) < 0)
2741 mappingflags (flags)
2744 static char asciiflags[8];
2746 strcpy (asciiflags, "-------");
2747 #if defined (MA_PHYS)
2748 if (flags & MA_PHYS) asciiflags[0] = 'd';
2750 if (flags & MA_STACK) asciiflags[1] = 's';
2751 if (flags & MA_BREAK) asciiflags[2] = 'b';
2752 if (flags & MA_SHARED) asciiflags[3] = 's';
2753 if (flags & MA_READ) asciiflags[4] = 'r';
2754 if (flags & MA_WRITE) asciiflags[5] = 'w';
2755 if (flags & MA_EXEC) asciiflags[6] = 'x';
2756 return (asciiflags);
2760 info_proc_flags (pip, summary)
2761 struct procinfo *pip;
2764 struct trans *transp;
2766 printf_filtered ("%-32s", "Process status flags:");
2769 printf_filtered ("\n\n");
2771 for (transp = pr_flag_table; transp -> name != NULL; transp++)
2773 if (pip -> prstatus.pr_flags & transp -> value)
2777 printf_filtered ("%s ", transp -> name);
2781 printf_filtered ("\t%-16s %s.\n", transp -> name, transp -> desc);
2785 printf_filtered ("\n");
2789 info_proc_stop (pip, summary)
2790 struct procinfo *pip;
2793 struct trans *transp;
2797 why = pip -> prstatus.pr_why;
2798 what = pip -> prstatus.pr_what;
2800 if (pip -> prstatus.pr_flags & PR_STOPPED)
2802 printf_filtered ("%-32s", "Reason for stopping:");
2805 printf_filtered ("\n\n");
2807 for (transp = pr_why_table; transp -> name != NULL; transp++)
2809 if (why == transp -> value)
2813 printf_filtered ("%s ", transp -> name);
2817 printf_filtered ("\t%-16s %s.\n",
2818 transp -> name, transp -> desc);
2824 /* Use the pr_why field to determine what the pr_what field means, and
2825 print more information. */
2830 /* pr_what is unused for this case */
2836 printf_filtered ("%s ", signalname (what));
2840 printf_filtered ("\t%-16s %s.\n", signalname (what),
2841 safe_strsignal (what));
2847 printf_filtered ("%s ", syscallname (what));
2851 printf_filtered ("\t%-16s %s.\n", syscallname (what),
2852 "Entered this system call");
2858 printf_filtered ("%s ", syscallname (what));
2862 printf_filtered ("\t%-16s %s.\n", syscallname (what),
2863 "Returned from this system call");
2869 printf_filtered ("%s ",
2870 lookupname (faults_table, what, "fault"));
2874 printf_filtered ("\t%-16s %s.\n",
2875 lookupname (faults_table, what, "fault"),
2876 lookupdesc (faults_table, what));
2880 printf_filtered ("\n");
2885 info_proc_siginfo (pip, summary)
2886 struct procinfo *pip;
2889 struct siginfo *sip;
2891 if ((pip -> prstatus.pr_flags & PR_STOPPED) &&
2892 (pip -> prstatus.pr_why == PR_SIGNALLED ||
2893 pip -> prstatus.pr_why == PR_FAULTED))
2895 printf_filtered ("%-32s", "Additional signal/fault info:");
2896 sip = &pip -> prstatus.pr_info;
2899 printf_filtered ("%s ", signalname (sip -> si_signo));
2900 if (sip -> si_errno > 0)
2902 printf_filtered ("%s ", errnoname (sip -> si_errno));
2904 if (sip -> si_code <= 0)
2906 printf_filtered ("sent by %s, uid %d ",
2907 target_pid_to_str (sip -> si_pid),
2912 printf_filtered ("%s ", sigcodename (sip));
2913 if ((sip -> si_signo == SIGILL) ||
2914 (sip -> si_signo == SIGFPE) ||
2915 (sip -> si_signo == SIGSEGV) ||
2916 (sip -> si_signo == SIGBUS))
2918 printf_filtered ("addr=%#x ", sip -> si_addr);
2920 else if ((sip -> si_signo == SIGCHLD))
2922 printf_filtered ("child %s, status %u ",
2923 target_pid_to_str (sip -> si_pid),
2926 else if ((sip -> si_signo == SIGPOLL))
2928 printf_filtered ("band %u ", sip -> si_band);
2934 printf_filtered ("\n\n");
2935 printf_filtered ("\t%-16s %s.\n", signalname (sip -> si_signo),
2936 safe_strsignal (sip -> si_signo));
2937 if (sip -> si_errno > 0)
2939 printf_filtered ("\t%-16s %s.\n",
2940 errnoname (sip -> si_errno),
2941 safe_strerror (sip -> si_errno));
2943 if (sip -> si_code <= 0)
2945 printf_filtered ("\t%-16u %s\n", sip -> si_pid, /* XXX need target_pid_to_str() */
2946 "PID of process sending signal");
2947 printf_filtered ("\t%-16u %s\n", sip -> si_uid,
2948 "UID of process sending signal");
2952 printf_filtered ("\t%-16s %s.\n", sigcodename (sip),
2954 if ((sip -> si_signo == SIGILL) ||
2955 (sip -> si_signo == SIGFPE))
2957 printf_filtered ("\t%-16#x %s.\n", sip -> si_addr,
2958 "Address of faulting instruction");
2960 else if ((sip -> si_signo == SIGSEGV) ||
2961 (sip -> si_signo == SIGBUS))
2963 printf_filtered ("\t%-16#x %s.\n", sip -> si_addr,
2964 "Address of faulting memory reference");
2966 else if ((sip -> si_signo == SIGCHLD))
2968 printf_filtered ("\t%-16u %s.\n", sip -> si_pid, /* XXX need target_pid_to_str() */
2969 "Child process ID");
2970 printf_filtered ("\t%-16u %s.\n", sip -> si_status,
2971 "Child process exit value or signal");
2973 else if ((sip -> si_signo == SIGPOLL))
2975 printf_filtered ("\t%-16u %s.\n", sip -> si_band,
2976 "Band event for POLL_{IN,OUT,MSG}");
2980 printf_filtered ("\n");
2985 info_proc_syscalls (pip, summary)
2986 struct procinfo *pip;
2994 #if 0 /* FIXME: Needs to use gdb-wide configured info about system calls. */
2995 if (pip -> prstatus.pr_flags & PR_ASLEEP)
2997 int syscallnum = pip -> prstatus.pr_reg[R_D0];
3000 printf_filtered ("%-32s", "Sleeping in system call:");
3001 printf_filtered ("%s", syscallname (syscallnum));
3005 printf_filtered ("Sleeping in system call '%s'.\n",
3006 syscallname (syscallnum));
3011 if (ioctl (pip -> fd, PIOCGENTRY, &pip -> entryset) < 0)
3013 print_sys_errmsg (pip -> pathname, errno);
3014 error ("PIOCGENTRY failed");
3017 if (ioctl (pip -> fd, PIOCGEXIT, &pip -> exitset) < 0)
3019 print_sys_errmsg (pip -> pathname, errno);
3020 error ("PIOCGEXIT failed");
3023 printf_filtered ("System call tracing information:\n\n");
3025 printf_filtered ("\t%-12s %-8s %-8s\n",
3029 for (syscallnum = 0; syscallnum < MAX_SYSCALLS; syscallnum++)
3032 if (syscall_table[syscallnum] != NULL)
3034 printf_filtered ("\t%-12s ", syscall_table[syscallnum]);
3035 printf_filtered ("%-8s ",
3036 prismember (&pip -> entryset, syscallnum)
3038 printf_filtered ("%-8s ",
3039 prismember (&pip -> exitset, syscallnum)
3041 printf_filtered ("\n");
3044 printf_filtered ("\n");
3053 static char locbuf[32];
3055 name = strsigno (signo);
3058 sprintf (locbuf, "Signal %d", signo);
3062 sprintf (locbuf, "%s (%d)", name, signo);
3072 static char locbuf[32];
3074 name = strerrno (errnum);
3077 sprintf (locbuf, "Errno %d", errnum);
3081 sprintf (locbuf, "%s (%d)", name, errnum);
3087 info_proc_signals (pip, summary)
3088 struct procinfo *pip;
3095 if (ioctl (pip -> fd, PIOCGTRACE, &pip -> trace) < 0)
3097 print_sys_errmsg (pip -> pathname, errno);
3098 error ("PIOCGTRACE failed");
3101 printf_filtered ("Disposition of signals:\n\n");
3102 printf_filtered ("\t%-15s %-8s %-8s %-8s %s\n\n",
3103 "Signal", "Trace", "Hold", "Pending", "Description");
3104 for (signo = 0; signo < NSIG; signo++)
3107 printf_filtered ("\t%-15s ", signalname (signo));
3108 printf_filtered ("%-8s ",
3109 prismember (&pip -> trace, signo)
3111 printf_filtered ("%-8s ",
3112 prismember (&pip -> prstatus.pr_sighold, signo)
3114 printf_filtered ("%-8s ",
3115 prismember (&pip -> prstatus.pr_sigpend, signo)
3117 printf_filtered (" %s\n", safe_strsignal (signo));
3119 printf_filtered ("\n");
3124 info_proc_faults (pip, summary)
3125 struct procinfo *pip;
3128 struct trans *transp;
3132 if (ioctl (pip -> fd, PIOCGFAULT, &pip -> fltset) < 0)
3134 print_sys_errmsg (pip -> pathname, errno);
3135 error ("PIOCGFAULT failed");
3138 printf_filtered ("Current traced hardware fault set:\n\n");
3139 printf_filtered ("\t%-12s %-8s\n", "Fault", "Trace");
3141 for (transp = faults_table; transp -> name != NULL; transp++)
3144 printf_filtered ("\t%-12s ", transp -> name);
3145 printf_filtered ("%-8s", prismember (&pip -> fltset, transp -> value)
3147 printf_filtered ("\n");
3149 printf_filtered ("\n");
3154 info_proc_mappings (pip, summary)
3155 struct procinfo *pip;
3159 struct prmap *prmaps;
3160 struct prmap *prmap;
3164 printf_filtered ("Mapped address spaces:\n\n");
3165 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3171 if (ioctl (pip -> fd, PIOCNMAP, &nmap) == 0)
3173 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
3174 if (ioctl (pip -> fd, PIOCMAP, prmaps) == 0)
3176 for (prmap = prmaps; prmap -> pr_size; ++prmap)
3178 printf_filtered ("\t%#10x %#10x %#10x %#10x %7s\n",
3180 prmap -> pr_vaddr + prmap -> pr_size - 1,
3183 mappingflags (prmap -> pr_mflags));
3187 printf_filtered ("\n");
3195 info_proc -- implement the "info proc" command
3199 void info_proc (char *args, int from_tty)
3203 Implement gdb's "info proc" command by using the /proc interface
3204 to print status information about any currently running process.
3206 Examples of the use of "info proc" are:
3208 info proc (prints summary info for current inferior)
3209 info proc 123 (prints summary info for process with pid 123)
3210 info proc mappings (prints address mappings)
3211 info proc times (prints process/children times)
3212 info proc id (prints pid, ppid, gid, sid, etc)
3213 FIXME: i proc id not implemented.
3214 info proc status (prints general process state info)
3215 FIXME: i proc status not implemented.
3216 info proc signals (prints info about signal handling)
3217 info proc all (prints all info)
3222 info_proc (args, from_tty)
3227 struct procinfo *pip;
3228 struct cleanup *old_chain;
3242 old_chain = make_cleanup (null_cleanup, 0);
3244 /* Default to using the current inferior if no pid specified. Note
3245 that inferior_pid may be 0, hence we set okerr. */
3247 pip = find_procinfo (inferior_pid, 1);
3251 if ((argv = buildargv (args)) == NULL)
3255 make_cleanup (freeargv, (char *) argv);
3257 while (*argv != NULL)
3259 argsize = strlen (*argv);
3260 if (argsize >= 1 && strncmp (*argv, "all", argsize) == 0)
3265 else if (argsize >= 2 && strncmp (*argv, "faults", argsize) == 0)
3270 else if (argsize >= 2 && strncmp (*argv, "flags", argsize) == 0)
3275 else if (argsize >= 1 && strncmp (*argv, "id", argsize) == 0)
3280 else if (argsize >= 1 && strncmp (*argv, "mappings", argsize) == 0)
3285 else if (argsize >= 2 && strncmp (*argv, "signals", argsize) == 0)
3290 else if (argsize >= 2 && strncmp (*argv, "status", argsize) == 0)
3295 else if (argsize >= 2 && strncmp (*argv, "syscalls", argsize) == 0)
3300 else if (argsize >= 1 && strncmp (*argv, "times", argsize) == 0)
3305 else if ((pid = atoi (*argv)) > 0)
3307 pip = (struct procinfo *) xmalloc (sizeof (struct procinfo));
3308 memset (pip, 0, sizeof (*pip));
3311 if (!open_proc_file (pid, pip, O_RDONLY))
3313 perror_with_name (pip -> pathname);
3316 make_cleanup (close_proc_file, pip);
3318 else if (**argv != '\000')
3320 error ("Unrecognized or ambiguous keyword `%s'.", *argv);
3326 /* If we don't have a valid open process at this point, then we have no
3327 inferior or didn't specify a specific pid. */
3332 No process. Start debugging a program or specify an explicit process ID.");
3334 if (ioctl (pip -> fd, PIOCSTATUS, &(pip -> prstatus)) < 0)
3336 print_sys_errmsg (pip -> pathname, errno);
3337 error ("PIOCSTATUS failed");
3340 /* Print verbose information of the requested type(s), or just a summary
3341 of the information for all types. */
3343 printf_filtered ("\nInformation for %s:\n\n", pip -> pathname);
3344 if (summary || all || flags)
3346 info_proc_flags (pip, summary);
3350 info_proc_stop (pip, summary);
3352 if (summary || all || signals || faults)
3354 info_proc_siginfo (pip, summary);
3356 if (summary || all || syscalls)
3358 info_proc_syscalls (pip, summary);
3360 if (summary || all || mappings)
3362 info_proc_mappings (pip, summary);
3364 if (summary || all || signals)
3366 info_proc_signals (pip, summary);
3368 if (summary || all || faults)
3370 info_proc_faults (pip, summary);
3372 printf_filtered ("\n");
3374 /* All done, deal with closing any temporary process info structure,
3375 freeing temporary memory , etc. */
3377 do_cleanups (old_chain);
3384 procfs_set_sproc_trap -- arrange for exec'd child stop on sproc
3388 void procfs_set_sproc_trap (void)
3392 This function sets up a trap on sproc system call exits so that we can
3393 detect the arrival of a new thread. We are called with the child
3394 stopped prior to it's first instruction.
3396 Also note that we turn on the inherit-on-fork flag in the child process
3397 so that any grand-children start with all tracing flags set.
3403 procfs_set_sproc_trap (pi)
3404 struct procinfo *pi;
3408 if (ioctl (pi->fd, PIOCGEXIT, &exitset) < 0)
3410 print_sys_errmsg (pi->pathname, errno);
3411 error ("PIOCGEXIT failed");
3414 praddset (&exitset, SYS_sproc);
3416 if (ioctl (pi->fd, PIOCSEXIT, &exitset) < 0)
3418 print_sys_errmsg (pi->pathname, errno);
3419 error ("PIOCSEXIT failed");
3422 /* Turn on inherit-on-fork flag so that all grand-children of gdb start with
3423 tracing flags set. */
3425 #ifdef PIOCSET /* New method */
3429 ioctl (pi->fd, PIOCSET, &pr_flags);
3432 #ifdef PIOCSFORK /* Original method */
3433 ioctl (pi->fd, PIOCSFORK, NULL);
3437 #endif /* SYS_sproc */
3439 /* Fork an inferior process, and start debugging it with /proc. */
3442 procfs_create_inferior (exec_file, allargs, env)
3447 char *shell_file = getenv ("SHELL");
3449 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
3452 /* We will be looking down the PATH to find shell_file. If we
3453 just do this the normal way (via execlp, which operates by
3454 attempting an exec for each element of the PATH until it
3455 finds one which succeeds), then there will be an exec for
3456 each failed attempt, each of which will cause a PR_SYSEXIT
3457 stop, and we won't know how to distinguish the PR_SYSEXIT's
3458 for these failed execs with the ones for successful execs
3459 (whether the exec has succeeded is stored at that time in the
3460 carry bit or some such architecture-specific and
3461 non-ABI-specified place).
3463 So I can't think of anything better than to search the PATH
3464 now. This has several disadvantages: (1) There is a race
3465 condition; if we find a file now and it is deleted before we
3466 exec it, we lose, even if the deletion leaves a valid file
3467 further down in the PATH, (2) there is no way to know exactly
3468 what an executable (in the sense of "capable of being
3469 exec'd") file is. Using access() loses because it may lose
3470 if the caller is the superuser; failing to use it loses if
3471 there are ACLs or some such. */
3475 /* FIXME-maybe: might want "set path" command so user can change what
3476 path is used from within GDB. */
3477 char *path = getenv ("PATH");
3479 struct stat statbuf;
3482 path = "/bin:/usr/bin";
3484 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
3485 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
3487 p1 = strchr (p, ':');
3492 strncpy (tryname, p, len);
3493 tryname[len] = '\0';
3494 strcat (tryname, "/");
3495 strcat (tryname, shell_file);
3496 if (access (tryname, X_OK) < 0)
3498 if (stat (tryname, &statbuf) < 0)
3500 if (!S_ISREG (statbuf.st_mode))
3501 /* We certainly need to reject directories. I'm not quite
3502 as sure about FIFOs, sockets, etc., but I kind of doubt
3503 that people want to exec() these things. */
3508 /* Not found. This must be an error rather than merely passing
3509 the file to execlp(), because execlp() would try all the
3510 exec()s, causing GDB to get confused. */
3511 error ("Can't find shell %s in PATH", shell_file);
3513 shell_file = tryname;
3516 fork_inferior (exec_file, allargs, env,
3517 proc_set_exec_trap, procfs_init_inferior, shell_file);
3519 /* We are at the first instruction we care about. */
3520 /* Pedal to the metal... */
3522 /* Setup traps on exit from sproc() */
3525 procfs_set_sproc_trap (current_procinfo);
3528 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
3531 /* Clean up after the inferior dies. */
3534 procfs_mourn_inferior ()
3536 struct procinfo *pi;
3538 for (pi = procinfo_list; pi; pi = pi->next)
3539 unconditionally_kill_inferior (pi);
3541 unpush_target (&procfs_ops);
3542 generic_mourn_inferior ();
3545 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
3551 #ifdef TARGET_CAN_USE_HARDWARE_WATCHPOINT
3553 /* Insert a watchpoint */
3555 procfs_set_watchpoint(pid, addr, len, rw)
3561 struct procinfo *pi;
3564 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
3565 wpt.pr_vaddr = (caddr_t)addr;
3567 wpt.pr_wflags = ((rw & 1) ? MA_READ : 0) | ((rw & 2) ? MA_WRITE : 0);
3568 if (ioctl (pi->fd, PIOCSWATCH, &wpt) < 0)
3572 /* Currently it sometimes happens that the same watchpoint gets
3573 deleted twice - don't die in this case (FIXME please) */
3574 if (errno == ESRCH && len == 0)
3576 print_sys_errmsg (pi->pathname, errno);
3577 error ("PIOCSWATCH failed");
3583 procfs_stopped_by_watchpoint(pid)
3586 struct procinfo *pi;
3590 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
3591 if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
3593 why = pi->prstatus.pr_why;
3594 what = pi->prstatus.pr_what;
3595 if (why == PR_FAULTED
3596 #if defined (FLTWATCH) && defined (FLTKWATCH)
3597 && (what == FLTWATCH) || (what == FLTKWATCH)
3600 && (what == FLTWATCH)
3603 && (what == FLTKWATCH)
3614 struct target_ops procfs_ops = {
3615 "procfs", /* to_shortname */
3616 "Unix /proc child process", /* to_longname */
3617 "Unix /proc child process (started by the \"run\" command).", /* to_doc */
3618 procfs_open, /* to_open */
3620 procfs_attach, /* to_attach */
3621 procfs_detach, /* to_detach */
3622 procfs_resume, /* to_resume */
3623 procfs_wait, /* to_wait */
3624 procfs_fetch_registers, /* to_fetch_registers */
3625 procfs_store_registers, /* to_store_registers */
3626 procfs_prepare_to_store, /* to_prepare_to_store */
3627 procfs_xfer_memory, /* to_xfer_memory */
3628 procfs_files_info, /* to_files_info */
3629 memory_insert_breakpoint, /* to_insert_breakpoint */
3630 memory_remove_breakpoint, /* to_remove_breakpoint */
3631 terminal_init_inferior, /* to_terminal_init */
3632 terminal_inferior, /* to_terminal_inferior */
3633 terminal_ours_for_output, /* to_terminal_ours_for_output */
3634 terminal_ours, /* to_terminal_ours */
3635 child_terminal_info, /* to_terminal_info */
3636 procfs_kill_inferior, /* to_kill */
3638 0, /* to_lookup_symbol */
3639 procfs_create_inferior, /* to_create_inferior */
3640 procfs_mourn_inferior, /* to_mourn_inferior */
3641 procfs_can_run, /* to_can_run */
3642 procfs_notice_signals, /* to_notice_signals */
3643 process_stratum, /* to_stratum */
3645 1, /* to_has_all_memory */
3646 1, /* to_has_memory */
3647 1, /* to_has_stack */
3648 1, /* to_has_registers */
3649 1, /* to_has_execution */
3651 0, /* sections_end */
3652 OPS_MAGIC /* to_magic */
3656 _initialize_procfs ()
3658 add_target (&procfs_ops);
3660 add_info ("proc", info_proc,
3661 "Show process status information using /proc entry.\n\
3662 Specify process id or use current inferior by default.\n\
3663 Specify keywords for detailed information; default is summary.\n\
3664 Keywords are: `all', `faults', `flags', `id', `mappings', `signals',\n\
3665 `status', `syscalls', and `times'.\n\
3666 Unambiguous abbreviations may be used.");
3668 init_syscall_table ();