1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
2 Copyright 1991, 1992-98, 1999 Free Software Foundation, Inc.
3 Written by Fred Fish at Cygnus Support. Changes for sysv4.2mp procfs
4 compatibility by Geoffrey Noer at Cygnus Solutions.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
26 For information on the details of using /proc consult section proc(4)
27 in the UNIX System V Release 4 System Administrator's Reference Manual.
29 The general register and floating point register sets are manipulated
30 separately. This file makes the assumption that if FP0_REGNUM is
31 defined, then support for the floating point register set is desired,
32 regardless of whether or not the actual target has floating point hardware.
39 #include <sys/types.h>
41 #include <sys/fault.h>
42 #include <sys/syscall.h>
43 #include <sys/procfs.h>
46 #include "gdb_string.h"
55 #include "gdbthread.h"
57 #if !defined(SYS_lwp_create) && defined(SYS_lwpcreate)
58 #define SYS_lwp_create SYS_lwpcreate
61 #if !defined(SYS_lwp_exit) && defined(SYS_lwpexit)
62 #define SYS_lwp_exit SYS_lwpexit
65 #if !defined(SYS_lwp_wait) && defined(SYS_lwpwait)
66 #define SYS_lwp_wait SYS_lwpwait
69 #if !defined(SYS_lwp_self) && defined(SYS_lwpself)
70 #define SYS_lwp_self SYS_lwpself
73 #if !defined(SYS_lwp_info) && defined(SYS_lwpinfo)
74 #define SYS_lwp_info SYS_lwpinfo
77 #if !defined(SYS_lwp_private) && defined(SYS_lwpprivate)
78 #define SYS_lwp_private SYS_lwpprivate
81 #if !defined(SYS_lwp_kill) && defined(SYS_lwpkill)
82 #define SYS_lwp_kill SYS_lwpkill
85 #if !defined(SYS_lwp_suspend) && defined(SYS_lwpsuspend)
86 #define SYS_lwp_suspend SYS_lwpsuspend
89 #if !defined(SYS_lwp_continue) && defined(SYS_lwpcontinue)
90 #define SYS_lwp_continue SYS_lwpcontinue
93 /* the name of the proc status struct depends on the implementation */
94 /* Wrap Light Weight Process member in THE_PR_LWP macro for clearer code */
95 #ifndef HAVE_PSTATUS_T
96 typedef prstatus_t gdb_prstatus_t;
97 #define THE_PR_LWP(a) a
98 #else /* HAVE_PSTATUS_T */
99 typedef pstatus_t gdb_prstatus_t;
100 #define THE_PR_LWP(a) a.pr_lwp
101 #if !defined(HAVE_PRRUN_T) && defined(HAVE_MULTIPLE_PROC_FDS)
102 /* Fallback definitions - for using configure information directly */
106 #if !defined(PROCFS_USE_READ_WRITE) && !defined(HAVE_PROCFS_PIOCSET)
107 #define PROCFS_USE_READ_WRITE 1
109 #endif /* !HAVE_PRRUN_T && HAVE_MULTIPLE_PROC_FDS */
110 #endif /* HAVE_PSTATUS_T */
112 #define MAX_SYSCALLS 256 /* Maximum number of syscalls for table */
114 /* proc name formats may vary depending on the proc implementation */
115 #ifdef HAVE_MULTIPLE_PROC_FDS
116 #ifndef CTL_PROC_NAME_FMT
117 #define CTL_PROC_NAME_FMT "/proc/%d/ctl"
118 #define AS_PROC_NAME_FMT "/proc/%d/as"
119 #define MAP_PROC_NAME_FMT "/proc/%d/map"
120 #define STATUS_PROC_NAME_FMT "/proc/%d/status"
122 #else /* HAVE_MULTIPLE_PROC_FDS */
123 #ifndef CTL_PROC_NAME_FMT
124 #define CTL_PROC_NAME_FMT "/proc/%05d"
125 #define AS_PROC_NAME_FMT "/proc/%05d"
126 #define MAP_PROC_NAME_FMT "/proc/%05d"
127 #define STATUS_PROC_NAME_FMT "/proc/%05d"
129 #endif /* HAVE_MULTIPLE_PROC_FDS */
132 /* These #ifdefs are for sol2.x in particular. sol2.x has
133 both a "gregset_t" and a "prgregset_t", which have
134 similar uses but different layouts. sol2.x gdb tries to
135 use prgregset_t (and prfpregset_t) everywhere. */
137 #ifdef GDB_GREGSET_TYPE
138 typedef GDB_GREGSET_TYPE gdb_gregset_t;
140 typedef gregset_t gdb_gregset_t;
143 #ifdef GDB_FPREGSET_TYPE
144 typedef GDB_FPREGSET_TYPE gdb_fpregset_t;
146 typedef fpregset_t gdb_fpregset_t;
150 #define MAX_PROC_NAME_SIZE sizeof("/proc/1234567890/status")
152 struct target_ops procfs_ops;
154 int procfs_suppress_run = 0; /* Non-zero if procfs should pretend not to
155 be a runnable target. Used by targets
156 that can sit atop procfs, such as solaris
159 #if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */
160 CORE_ADDR kernel_u_addr;
163 #ifdef BROKEN_SIGINFO_H /* Workaround broken SGS <sys/siginfo.h> */
165 #define si_pid _data._proc.pid
167 #define si_uid _data._proc._pdata._kill.uid
168 #endif /* BROKEN_SIGINFO_H */
170 /* Define structures for passing commands to /proc/pid/ctl file. Note that
171 while we create these for the PROCFS_USE_READ_WRITE world, we use them
172 and ignore the extra cmd int in other proc schemes.
174 /* generic ctl msg */
181 /* set general registers */
185 gdb_gregset_t gregset;
188 /* set fp registers */
192 gdb_fpregset_t fpregset;
195 /* set signals to be traced */
202 /* set faults to be traced */
209 /* set system calls to be traced */
216 /* set current signal to be traced */
223 /* All access to the inferior, either one started by gdb or one that has
224 been attached to, is controlled by an instance of a procinfo structure,
225 defined below. Since gdb currently only handles one inferior at a time,
226 the procinfo structure for the inferior is statically allocated and
227 only one exists at any given time. There is a separate procinfo
228 structure for use by the "info proc" command, so that we can print
229 useful information about any random process without interfering with
230 the inferior's procinfo information. */
234 struct procinfo *next;
235 int pid; /* Process ID of inferior */
236 int ctl_fd; /* File descriptor for /proc ctl file */
237 int status_fd; /* File descriptor for /proc status file */
238 int as_fd; /* File descriptor for /proc as file */
239 int map_fd; /* File descriptor for /proc map file */
240 char *pathname; /* Pathname to /proc entry */
241 int had_event; /* poll/select says something happened */
242 int was_stopped; /* Nonzero if was stopped prior to attach */
243 int nopass_next_sigstop; /* Don't pass a sigstop on next resume */
245 prrun_t prrun; /* Control state when it is run */
247 gdb_prstatus_t prstatus; /* Current process status info */
248 struct greg_ctl gregset; /* General register set */
249 struct fpreg_ctl fpregset; /* Floating point register set */
250 struct flt_ctl fltset; /* Current traced hardware fault set */
251 struct sig_ctl trace; /* Current traced signal set */
252 struct sys_ctl exitset; /* Current traced system call exit set */
253 struct sys_ctl entryset; /* Current traced system call entry set */
254 struct sig_ctl saved_sighold; /* Saved held signal set */
255 struct flt_ctl saved_fltset; /* Saved traced hardware fault set */
256 struct sig_ctl saved_trace; /* Saved traced signal set */
257 struct sys_ctl saved_exitset; /* Saved traced system call exit set */
258 struct sys_ctl saved_entryset; /* Saved traced system call entry set */
259 int num_syscall_handlers; /* Number of syscall trap handlers
260 currently installed */
261 /* Pointer to list of syscall trap handlers */
262 struct procfs_syscall_handler *syscall_handlers;
263 int saved_rtnval; /* return value and status for wait(), */
264 int saved_statval; /* as supplied by a syscall handler. */
265 int new_child; /* Non-zero if it's a new thread */
268 /* List of inferior process information */
269 static struct procinfo *procinfo_list = NULL;
270 static struct pollfd *poll_list; /* pollfds used for waiting on /proc */
272 static int num_poll_list = 0; /* Number of entries in poll_list */
274 /* Much of the information used in the /proc interface, particularly for
275 printing status information, is kept as tables of structures of the
276 following form. These tables can be used to map numeric values to
277 their symbolic names and to a string that describes their specific use. */
281 int value; /* The numeric value */
282 char *name; /* The equivalent symbolic value */
283 char *desc; /* Short description of value */
286 /* Translate bits in the pr_flags member of the prstatus structure, into the
287 names and desc information. */
289 static struct trans pr_flag_table[] =
291 #if defined (PR_STOPPED)
292 {PR_STOPPED, "PR_STOPPED", "Process is stopped"},
294 #if defined (PR_ISTOP)
295 {PR_ISTOP, "PR_ISTOP", "Stopped on an event of interest"},
297 #if defined (PR_DSTOP)
298 {PR_DSTOP, "PR_DSTOP", "A stop directive is in effect"},
300 #if defined (PR_ASLEEP)
301 {PR_ASLEEP, "PR_ASLEEP", "Sleeping in an interruptible system call"},
303 #if defined (PR_FORK)
304 {PR_FORK, "PR_FORK", "Inherit-on-fork is in effect"},
307 {PR_RLC, "PR_RLC", "Run-on-last-close is in effect"},
309 #if defined (PR_PTRACE)
310 {PR_PTRACE, "PR_PTRACE", "Process is being controlled by ptrace"},
312 #if defined (PR_PCINVAL)
313 {PR_PCINVAL, "PR_PCINVAL", "PC refers to an invalid virtual address"},
315 #if defined (PR_ISSYS)
316 {PR_ISSYS, "PR_ISSYS", "Is a system process"},
318 #if defined (PR_STEP)
319 {PR_STEP, "PR_STEP", "Process has single step pending"},
322 {PR_KLC, "PR_KLC", "Kill-on-last-close is in effect"},
324 #if defined (PR_ASYNC)
325 {PR_ASYNC, "PR_ASYNC", "Asynchronous stop is in effect"},
327 #if defined (PR_PCOMPAT)
328 {PR_PCOMPAT, "PR_PCOMPAT", "Ptrace compatibility mode in effect"},
330 #if defined (PR_MSACCT)
331 {PR_MSACCT, "PR_MSACCT", "Microstate accounting enabled"},
333 #if defined (PR_BPTADJ)
334 {PR_BPTADJ, "PR_BPTADJ", "Breakpoint PC adjustment in effect"},
336 #if defined (PR_ASLWP)
337 {PR_ASLWP, "PR_ASLWP", "Asynchronus signal LWP"},
342 /* Translate values in the pr_why field of the prstatus struct. */
344 static struct trans pr_why_table[] =
346 #if defined (PR_REQUESTED)
347 {PR_REQUESTED, "PR_REQUESTED", "Directed to stop via PIOCSTOP/PIOCWSTOP"},
349 #if defined (PR_SIGNALLED)
350 {PR_SIGNALLED, "PR_SIGNALLED", "Receipt of a traced signal"},
352 #if defined (PR_SYSENTRY)
353 {PR_SYSENTRY, "PR_SYSENTRY", "Entry to a traced system call"},
355 #if defined (PR_SYSEXIT)
356 {PR_SYSEXIT, "PR_SYSEXIT", "Exit from a traced system call"},
358 #if defined (PR_JOBCONTROL)
359 {PR_JOBCONTROL, "PR_JOBCONTROL", "Default job control stop signal action"},
361 #if defined (PR_FAULTED)
362 {PR_FAULTED, "PR_FAULTED", "Incurred a traced hardware fault"},
364 #if defined (PR_SUSPENDED)
365 {PR_SUSPENDED, "PR_SUSPENDED", "Process suspended"},
367 #if defined (PR_CHECKPOINT)
368 {PR_CHECKPOINT, "PR_CHECKPOINT", "(???)"},
373 /* Hardware fault translation table. */
375 static struct trans faults_table[] =
378 {FLTILL, "FLTILL", "Illegal instruction"},
380 #if defined (FLTPRIV)
381 {FLTPRIV, "FLTPRIV", "Privileged instruction"},
384 {FLTBPT, "FLTBPT", "Breakpoint trap"},
386 #if defined (FLTTRACE)
387 {FLTTRACE, "FLTTRACE", "Trace trap"},
389 #if defined (FLTACCESS)
390 {FLTACCESS, "FLTACCESS", "Memory access fault"},
392 #if defined (FLTBOUNDS)
393 {FLTBOUNDS, "FLTBOUNDS", "Memory bounds violation"},
395 #if defined (FLTIOVF)
396 {FLTIOVF, "FLTIOVF", "Integer overflow"},
398 #if defined (FLTIZDIV)
399 {FLTIZDIV, "FLTIZDIV", "Integer zero divide"},
402 {FLTFPE, "FLTFPE", "Floating-point exception"},
404 #if defined (FLTSTACK)
405 {FLTSTACK, "FLTSTACK", "Unrecoverable stack fault"},
407 #if defined (FLTPAGE)
408 {FLTPAGE, "FLTPAGE", "Recoverable page fault"},
413 /* Translation table for signal generation information. See UNIX System
414 V Release 4 Programmer's Reference Manual, siginfo(5). */
416 static struct sigcode
425 #if defined (SIGILL) && defined (ILL_ILLOPC)
427 SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode"
431 #if defined (SIGILL) && defined (ILL_ILLOPN)
433 SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand",
437 #if defined (SIGILL) && defined (ILL_ILLADR)
439 SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode"
443 #if defined (SIGILL) && defined (ILL_ILLTRP)
445 SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap"
449 #if defined (SIGILL) && defined (ILL_PRVOPC)
451 SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode"
455 #if defined (SIGILL) && defined (ILL_PRVREG)
457 SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register"
461 #if defined (SIGILL) && defined (ILL_COPROC)
463 SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error"
467 #if defined (SIGILL) && defined (ILL_BADSTK)
469 SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error"
473 #if defined (SIGFPE) && defined (FPE_INTDIV)
475 SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero"
479 #if defined (SIGFPE) && defined (FPE_INTOVF)
481 SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow"
485 #if defined (SIGFPE) && defined (FPE_FLTDIV)
487 SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating point divide by zero"
491 #if defined (SIGFPE) && defined (FPE_FLTOVF)
493 SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating point overflow"
497 #if defined (SIGFPE) && defined (FPE_FLTUND)
499 SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating point underflow"
503 #if defined (SIGFPE) && defined (FPE_FLTRES)
505 SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating point inexact result"
509 #if defined (SIGFPE) && defined (FPE_FLTINV)
511 SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating point operation"
515 #if defined (SIGFPE) && defined (FPE_FLTSUB)
517 SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range"
521 #if defined (SIGSEGV) && defined (SEGV_MAPERR)
523 SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object"
527 #if defined (SIGSEGV) && defined (SEGV_ACCERR)
529 SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for object"
533 #if defined (SIGBUS) && defined (BUS_ADRALN)
535 SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment"
539 #if defined (SIGBUS) && defined (BUS_ADRERR)
541 SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Non-existent physical address"
545 #if defined (SIGBUS) && defined (BUS_OBJERR)
547 SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object specific hardware error"
551 #if defined (SIGTRAP) && defined (TRAP_BRKPT)
553 SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint"
557 #if defined (SIGTRAP) && defined (TRAP_TRACE)
559 SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap"
563 #if defined (SIGCLD) && defined (CLD_EXITED)
565 SIGCLD, CLD_EXITED, "CLD_EXITED", "Child has exited"
569 #if defined (SIGCLD) && defined (CLD_KILLED)
571 SIGCLD, CLD_KILLED, "CLD_KILLED", "Child was killed"
575 #if defined (SIGCLD) && defined (CLD_DUMPED)
577 SIGCLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally"
581 #if defined (SIGCLD) && defined (CLD_TRAPPED)
583 SIGCLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped"
587 #if defined (SIGCLD) && defined (CLD_STOPPED)
589 SIGCLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped"
593 #if defined (SIGCLD) && defined (CLD_CONTINUED)
595 SIGCLD, CLD_CONTINUED, "CLD_CONTINUED", "Stopped child had continued"
599 #if defined (SIGPOLL) && defined (POLL_IN)
601 SIGPOLL, POLL_IN, "POLL_IN", "Input input available"
605 #if defined (SIGPOLL) && defined (POLL_OUT)
607 SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available"
611 #if defined (SIGPOLL) && defined (POLL_MSG)
613 SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available"
617 #if defined (SIGPOLL) && defined (POLL_ERR)
619 SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error"
623 #if defined (SIGPOLL) && defined (POLL_PRI)
625 SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available"
629 #if defined (SIGPOLL) && defined (POLL_HUP)
631 SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected"
640 static char *syscall_table[MAX_SYSCALLS];
642 /* Prototypes for local functions */
644 static void procfs_stop PARAMS ((void));
646 static int procfs_thread_alive PARAMS ((int));
648 static int procfs_can_run PARAMS ((void));
650 static void procfs_mourn_inferior PARAMS ((void));
652 static void procfs_fetch_registers PARAMS ((int));
654 static int procfs_wait PARAMS ((int, struct target_waitstatus *));
656 static void procfs_open PARAMS ((char *, int));
658 static void procfs_files_info PARAMS ((struct target_ops *));
660 static void procfs_prepare_to_store PARAMS ((void));
662 static void procfs_detach PARAMS ((char *, int));
664 static void procfs_attach PARAMS ((char *, int));
666 static void proc_set_exec_trap PARAMS ((void));
668 static void procfs_init_inferior PARAMS ((int));
670 static struct procinfo *create_procinfo PARAMS ((int));
672 static void procfs_store_registers PARAMS ((int));
674 static int procfs_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
676 static void procfs_kill_inferior PARAMS ((void));
678 static char *sigcodedesc PARAMS ((siginfo_t *));
680 static char *sigcodename PARAMS ((siginfo_t *));
682 static struct procinfo *wait_fd PARAMS ((void));
684 static void remove_fd PARAMS ((struct procinfo *));
686 static void add_fd PARAMS ((struct procinfo *));
688 static void set_proc_siginfo PARAMS ((struct procinfo *, int));
690 static void init_syscall_table PARAMS ((void));
692 static char *syscallname PARAMS ((int));
694 static char *signalname PARAMS ((int));
696 static char *errnoname PARAMS ((int));
698 static int proc_address_to_fd PARAMS ((struct procinfo *, CORE_ADDR, int));
700 static int open_proc_file PARAMS ((int, struct procinfo *, int, int));
702 static void close_proc_file PARAMS ((struct procinfo *));
704 static void close_proc_file_cleanup PARAMS ((void *));
706 static struct cleanup *make_cleanup_close_proc_file PARAMS ((struct procinfo *));
708 static void unconditionally_kill_inferior PARAMS ((struct procinfo *));
710 static NORETURN void proc_init_failed
711 PARAMS ((struct procinfo *, char *, int)) ATTR_NORETURN;
713 static void info_proc PARAMS ((char *, int));
715 static void info_proc_flags PARAMS ((struct procinfo *, int));
717 static void info_proc_stop PARAMS ((struct procinfo *, int));
719 static void info_proc_siginfo PARAMS ((struct procinfo *, int));
721 static void info_proc_syscalls PARAMS ((struct procinfo *, int));
723 static void info_proc_mappings PARAMS ((struct procinfo *, int));
725 static void info_proc_signals PARAMS ((struct procinfo *, int));
727 static void info_proc_faults PARAMS ((struct procinfo *, int));
729 static char *mappingflags PARAMS ((long));
731 static char *lookupname PARAMS ((struct trans *, unsigned int, char *));
733 static char *lookupdesc PARAMS ((struct trans *, unsigned int));
735 static int do_attach PARAMS ((int pid));
737 static void do_detach PARAMS ((int siggnal));
739 static void procfs_create_inferior PARAMS ((char *, char *, char **));
741 static void procfs_notice_signals PARAMS ((int pid));
743 static void notice_signals PARAMS ((struct procinfo *, struct sig_ctl *));
745 static struct procinfo *find_procinfo PARAMS ((pid_t pid, int okfail));
747 static int procfs_write_pcwstop PARAMS ((struct procinfo *));
748 static int procfs_read_status PARAMS ((struct procinfo *));
749 static void procfs_write_pckill PARAMS ((struct procinfo *));
751 typedef int syscall_func_t PARAMS ((struct procinfo * pi, int syscall_num,
752 int why, int *rtnval, int *statval));
754 static void procfs_set_syscall_trap PARAMS ((struct procinfo * pi,
755 int syscall_num, int flags,
756 syscall_func_t * func));
758 static void procfs_clear_syscall_trap PARAMS ((struct procinfo * pi,
759 int syscall_num, int errok));
761 #define PROCFS_SYSCALL_ENTRY 0x1 /* Trap on entry to sys call */
762 #define PROCFS_SYSCALL_EXIT 0x2 /* Trap on exit from sys call */
764 static syscall_func_t procfs_exit_handler;
766 static syscall_func_t procfs_exec_handler;
769 static syscall_func_t procfs_sproc_handler;
770 static syscall_func_t procfs_fork_handler;
773 #ifdef SYS_lwp_create
774 static syscall_func_t procfs_lwp_creation_handler;
777 static void modify_inherit_on_fork_flag PARAMS ((int fd, int flag));
778 static void modify_run_on_last_close_flag PARAMS ((int fd, int flag));
782 struct procfs_syscall_handler
784 int syscall_num; /* The number of the system call being handled */
785 /* The function to be called */
786 syscall_func_t *func;
789 static void procfs_resume PARAMS ((int pid, int step,
790 enum target_signal signo));
792 static void init_procfs_ops PARAMS ((void));
794 /* External function prototypes that can't be easily included in any
795 header file because the args are typedefs in system include files. */
797 extern void supply_gregset PARAMS ((gdb_gregset_t *));
799 extern void fill_gregset PARAMS ((gdb_gregset_t *, int));
802 extern void supply_fpregset PARAMS ((gdb_fpregset_t *));
804 extern void fill_fpregset PARAMS ((gdb_fpregset_t *, int));
811 find_procinfo -- convert a process id to a struct procinfo
815 static struct procinfo * find_procinfo (pid_t pid, int okfail);
819 Given a process id, look it up in the procinfo chain. Returns
820 a struct procinfo *. If can't find pid, then call error(),
821 unless okfail is set, in which case, return NULL;
824 static struct procinfo *
825 find_procinfo (pid, okfail)
829 struct procinfo *procinfo;
831 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
832 if (procinfo->pid == pid)
838 error ("procfs (find_procinfo): Couldn't locate pid %d", pid);
845 current_procinfo -- convert inferior_pid to a struct procinfo
849 static struct procinfo * current_procinfo;
853 Looks up inferior_pid in the procinfo chain. Always returns a
854 struct procinfo *. If process can't be found, we error() out.
857 #define current_procinfo find_procinfo (inferior_pid, 0)
863 add_fd -- Add the fd to the poll/select list
867 static void add_fd (struct procinfo *);
871 Add the fd of the supplied procinfo to the list of fds used for
872 poll/select operations.
879 if (num_poll_list <= 0)
880 poll_list = (struct pollfd *) xmalloc (sizeof (struct pollfd));
882 poll_list = (struct pollfd *) xrealloc (poll_list,
884 * sizeof (struct pollfd));
885 poll_list[num_poll_list].fd = pi->ctl_fd;
887 poll_list[num_poll_list].events = POLLWRNORM;
889 poll_list[num_poll_list].events = POLLPRI;
899 remove_fd -- Remove the fd from the poll/select list
903 static void remove_fd (struct procinfo *);
907 Remove the fd of the supplied procinfo from the list of fds used
908 for poll/select operations.
917 for (i = 0; i < num_poll_list; i++)
919 if (poll_list[i].fd == pi->ctl_fd)
921 if (i != num_poll_list - 1)
922 memcpy (poll_list + i, poll_list + i + 1,
923 (num_poll_list - i - 1) * sizeof (struct pollfd));
927 if (num_poll_list == 0)
930 poll_list = (struct pollfd *) xrealloc (poll_list,
932 * sizeof (struct pollfd));
942 procfs_read_status - get procfs fd status
946 static int procfs_read_status (pi) struct procinfo *pi;
950 Given a pointer to a procinfo struct, get the status of
951 the status_fd in the appropriate way. Returns 0 on failure,
956 procfs_read_status (pi)
959 #ifdef PROCFS_USE_READ_WRITE
960 if ((lseek (pi->status_fd, 0, SEEK_SET) < 0) ||
961 (read (pi->status_fd, (char *) &pi->prstatus,
962 sizeof (gdb_prstatus_t)) != sizeof (gdb_prstatus_t)))
964 if (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) < 0)
975 procfs_write_pcwstop - send a PCWSTOP to procfs fd
979 static int procfs_write_pcwstop (pi) struct procinfo *pi;
983 Given a pointer to a procinfo struct, send a PCWSTOP to
984 the ctl_fd in the appropriate way. Returns 0 on failure,
989 procfs_write_pcwstop (pi)
992 #ifdef PROCFS_USE_READ_WRITE
994 if (write (pi->ctl_fd, (char *) &cmd, sizeof (long)) < 0)
996 if (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) < 0)
1007 procfs_write_pckill - send a kill to procfs fd
1011 static void procfs_write_pckill (pi) struct procinfo *pi;
1015 Given a pointer to a procinfo struct, send a kill to
1016 the ctl_fd in the appropriate way. Returns 0 on failure,
1021 procfs_write_pckill (pi)
1022 struct procinfo *pi;
1024 #ifdef PROCFS_USE_READ_WRITE
1025 struct proc_ctl pctl;
1027 pctl.data = SIGKILL;
1028 write (pi->ctl_fd, &pctl, sizeof (struct proc_ctl));
1030 int signo = SIGKILL;
1031 ioctl (pi->ctl_fd, PIOCKILL, &signo);
1035 static struct procinfo *
1038 struct procinfo *pi, *next_pi;
1044 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1045 attached process. */
1052 num_fds = poll (poll_list, num_poll_list, -1);
1055 if (num_fds < 0 && errno == EINTR)
1057 print_sys_errmsg ("poll failed", errno);
1058 error ("Poll failed, returned %d", num_fds);
1060 #else /* LOSING_POLL */
1061 pi = current_procinfo;
1063 while (!procfs_write_pcwstop (pi))
1065 if (errno == ENOENT)
1067 /* Process exited. */
1068 pi->prstatus.pr_flags = 0;
1071 else if (errno != EINTR)
1073 print_sys_errmsg (pi->pathname, errno);
1074 error ("procfs_write_pcwstop failed");
1078 #endif /* LOSING_POLL */
1080 clear_sigint_trap ();
1081 clear_sigio_trap ();
1085 for (i = 0; i < num_poll_list && num_fds > 0; i++)
1087 if (0 == (poll_list[i].revents &
1088 (POLLWRNORM | POLLPRI | POLLERR | POLLHUP | POLLNVAL)))
1090 for (pi = procinfo_list; pi; pi = next_pi)
1093 if (poll_list[i].fd == pi->ctl_fd)
1096 if ((poll_list[i].revents & POLLHUP) != 0 ||
1097 !procfs_read_status (pi))
1098 { /* The LWP has apparently terminated. */
1099 if (num_poll_list <= 1)
1101 pi->prstatus.pr_flags = 0;
1106 printf_filtered ("LWP %d exited.\n",
1107 (pi->pid >> 16) & 0xffff);
1108 close_proc_file (pi);
1109 i--; /* don't skip deleted entry */
1111 break; /* already another event to process */
1113 goto wait_again; /* wait for another event */
1120 error ("wait_fd: Couldn't find procinfo for fd %d\n",
1123 #endif /* LOSING_POLL */
1132 lookupdesc -- translate a value to a summary desc string
1136 static char *lookupdesc (struct trans *transp, unsigned int val);
1140 Given a pointer to a translation table and a value to be translated,
1141 lookup the desc string and return it.
1145 lookupdesc (transp, val)
1146 struct trans *transp;
1151 for (desc = NULL; transp->name != NULL; transp++)
1153 if (transp->value == val)
1155 desc = transp->desc;
1160 /* Didn't find a translation for the specified value, set a default one. */
1173 lookupname -- translate a value to symbolic name
1177 static char *lookupname (struct trans *transp, unsigned int val,
1182 Given a pointer to a translation table, a value to be translated,
1183 and a default prefix to return if the value can't be translated,
1184 match the value with one of the translation table entries and
1185 return a pointer to the symbolic name.
1187 If no match is found it just returns the value as a printable string,
1188 with the given prefix. The previous such value, if any, is freed
1193 lookupname (transp, val, prefix)
1194 struct trans *transp;
1198 static char *locbuf;
1201 for (name = NULL; transp->name != NULL; transp++)
1203 if (transp->value == val)
1205 name = transp->name;
1210 /* Didn't find a translation for the specified value, build a default
1211 one using the specified prefix and return it. The lifetime of
1212 the value is only until the next one is needed. */
1220 locbuf = xmalloc (strlen (prefix) + 16);
1221 sprintf (locbuf, "%s %u", prefix, val);
1231 struct sigcode *scp;
1233 static char locbuf[32];
1235 for (scp = siginfo_table; scp->codename != NULL; scp++)
1237 if ((scp->signo == sip->si_signo) &&
1238 (scp->code == sip->si_code))
1240 name = scp->codename;
1246 sprintf (locbuf, "sigcode %u", sip->si_signo);
1256 struct sigcode *scp;
1259 for (scp = siginfo_table; scp->codename != NULL; scp++)
1261 if ((scp->signo == sip->si_signo) &&
1262 (scp->code == sip->si_code))
1270 desc = "Unrecognized signal or trap use";
1279 syscallname - translate a system call number into a system call name
1283 char *syscallname (int syscallnum)
1287 Given a system call number, translate it into the printable name
1288 of a system call, or into "syscall <num>" if it is an unknown
1293 syscallname (syscallnum)
1296 static char locbuf[32];
1298 if (syscallnum >= 0 && syscallnum < MAX_SYSCALLS
1299 && syscall_table[syscallnum] != NULL)
1300 return syscall_table[syscallnum];
1303 sprintf (locbuf, "syscall %u", syscallnum);
1312 init_syscall_table - initialize syscall translation table
1316 void init_syscall_table (void)
1320 Dynamically initialize the translation table to convert system
1321 call numbers into printable system call names. Done once per
1322 gdb run, on initialization.
1326 This is awfully ugly, but preprocessor tricks to make it prettier
1327 tend to be nonportable.
1331 init_syscall_table ()
1333 #if defined (SYS_exit)
1334 syscall_table[SYS_exit] = "exit";
1336 #if defined (SYS_fork)
1337 syscall_table[SYS_fork] = "fork";
1339 #if defined (SYS_read)
1340 syscall_table[SYS_read] = "read";
1342 #if defined (SYS_write)
1343 syscall_table[SYS_write] = "write";
1345 #if defined (SYS_open)
1346 syscall_table[SYS_open] = "open";
1348 #if defined (SYS_close)
1349 syscall_table[SYS_close] = "close";
1351 #if defined (SYS_wait)
1352 syscall_table[SYS_wait] = "wait";
1354 #if defined (SYS_creat)
1355 syscall_table[SYS_creat] = "creat";
1357 #if defined (SYS_link)
1358 syscall_table[SYS_link] = "link";
1360 #if defined (SYS_unlink)
1361 syscall_table[SYS_unlink] = "unlink";
1363 #if defined (SYS_exec)
1364 syscall_table[SYS_exec] = "exec";
1366 #if defined (SYS_execv)
1367 syscall_table[SYS_execv] = "execv";
1369 #if defined (SYS_execve)
1370 syscall_table[SYS_execve] = "execve";
1372 #if defined (SYS_chdir)
1373 syscall_table[SYS_chdir] = "chdir";
1375 #if defined (SYS_time)
1376 syscall_table[SYS_time] = "time";
1378 #if defined (SYS_mknod)
1379 syscall_table[SYS_mknod] = "mknod";
1381 #if defined (SYS_chmod)
1382 syscall_table[SYS_chmod] = "chmod";
1384 #if defined (SYS_chown)
1385 syscall_table[SYS_chown] = "chown";
1387 #if defined (SYS_brk)
1388 syscall_table[SYS_brk] = "brk";
1390 #if defined (SYS_stat)
1391 syscall_table[SYS_stat] = "stat";
1393 #if defined (SYS_lseek)
1394 syscall_table[SYS_lseek] = "lseek";
1396 #if defined (SYS_getpid)
1397 syscall_table[SYS_getpid] = "getpid";
1399 #if defined (SYS_mount)
1400 syscall_table[SYS_mount] = "mount";
1402 #if defined (SYS_umount)
1403 syscall_table[SYS_umount] = "umount";
1405 #if defined (SYS_setuid)
1406 syscall_table[SYS_setuid] = "setuid";
1408 #if defined (SYS_getuid)
1409 syscall_table[SYS_getuid] = "getuid";
1411 #if defined (SYS_stime)
1412 syscall_table[SYS_stime] = "stime";
1414 #if defined (SYS_ptrace)
1415 syscall_table[SYS_ptrace] = "ptrace";
1417 #if defined (SYS_alarm)
1418 syscall_table[SYS_alarm] = "alarm";
1420 #if defined (SYS_fstat)
1421 syscall_table[SYS_fstat] = "fstat";
1423 #if defined (SYS_pause)
1424 syscall_table[SYS_pause] = "pause";
1426 #if defined (SYS_utime)
1427 syscall_table[SYS_utime] = "utime";
1429 #if defined (SYS_stty)
1430 syscall_table[SYS_stty] = "stty";
1432 #if defined (SYS_gtty)
1433 syscall_table[SYS_gtty] = "gtty";
1435 #if defined (SYS_access)
1436 syscall_table[SYS_access] = "access";
1438 #if defined (SYS_nice)
1439 syscall_table[SYS_nice] = "nice";
1441 #if defined (SYS_statfs)
1442 syscall_table[SYS_statfs] = "statfs";
1444 #if defined (SYS_sync)
1445 syscall_table[SYS_sync] = "sync";
1447 #if defined (SYS_kill)
1448 syscall_table[SYS_kill] = "kill";
1450 #if defined (SYS_fstatfs)
1451 syscall_table[SYS_fstatfs] = "fstatfs";
1453 #if defined (SYS_pgrpsys)
1454 syscall_table[SYS_pgrpsys] = "pgrpsys";
1456 #if defined (SYS_xenix)
1457 syscall_table[SYS_xenix] = "xenix";
1459 #if defined (SYS_dup)
1460 syscall_table[SYS_dup] = "dup";
1462 #if defined (SYS_pipe)
1463 syscall_table[SYS_pipe] = "pipe";
1465 #if defined (SYS_times)
1466 syscall_table[SYS_times] = "times";
1468 #if defined (SYS_profil)
1469 syscall_table[SYS_profil] = "profil";
1471 #if defined (SYS_plock)
1472 syscall_table[SYS_plock] = "plock";
1474 #if defined (SYS_setgid)
1475 syscall_table[SYS_setgid] = "setgid";
1477 #if defined (SYS_getgid)
1478 syscall_table[SYS_getgid] = "getgid";
1480 #if defined (SYS_signal)
1481 syscall_table[SYS_signal] = "signal";
1483 #if defined (SYS_msgsys)
1484 syscall_table[SYS_msgsys] = "msgsys";
1486 #if defined (SYS_sys3b)
1487 syscall_table[SYS_sys3b] = "sys3b";
1489 #if defined (SYS_sysi86)
1490 syscall_table[SYS_sysi86] = "sysi86";
1492 #if defined (SYS_acct)
1493 syscall_table[SYS_acct] = "acct";
1495 #if defined (SYS_shmsys)
1496 syscall_table[SYS_shmsys] = "shmsys";
1498 #if defined (SYS_semsys)
1499 syscall_table[SYS_semsys] = "semsys";
1501 #if defined (SYS_ioctl)
1502 syscall_table[SYS_ioctl] = "ioctl";
1504 #if defined (SYS_uadmin)
1505 syscall_table[SYS_uadmin] = "uadmin";
1507 #if defined (SYS_utssys)
1508 syscall_table[SYS_utssys] = "utssys";
1510 #if defined (SYS_fsync)
1511 syscall_table[SYS_fsync] = "fsync";
1513 #if defined (SYS_umask)
1514 syscall_table[SYS_umask] = "umask";
1516 #if defined (SYS_chroot)
1517 syscall_table[SYS_chroot] = "chroot";
1519 #if defined (SYS_fcntl)
1520 syscall_table[SYS_fcntl] = "fcntl";
1522 #if defined (SYS_ulimit)
1523 syscall_table[SYS_ulimit] = "ulimit";
1525 #if defined (SYS_rfsys)
1526 syscall_table[SYS_rfsys] = "rfsys";
1528 #if defined (SYS_rmdir)
1529 syscall_table[SYS_rmdir] = "rmdir";
1531 #if defined (SYS_mkdir)
1532 syscall_table[SYS_mkdir] = "mkdir";
1534 #if defined (SYS_getdents)
1535 syscall_table[SYS_getdents] = "getdents";
1537 #if defined (SYS_sysfs)
1538 syscall_table[SYS_sysfs] = "sysfs";
1540 #if defined (SYS_getmsg)
1541 syscall_table[SYS_getmsg] = "getmsg";
1543 #if defined (SYS_putmsg)
1544 syscall_table[SYS_putmsg] = "putmsg";
1546 #if defined (SYS_poll)
1547 syscall_table[SYS_poll] = "poll";
1549 #if defined (SYS_lstat)
1550 syscall_table[SYS_lstat] = "lstat";
1552 #if defined (SYS_symlink)
1553 syscall_table[SYS_symlink] = "symlink";
1555 #if defined (SYS_readlink)
1556 syscall_table[SYS_readlink] = "readlink";
1558 #if defined (SYS_setgroups)
1559 syscall_table[SYS_setgroups] = "setgroups";
1561 #if defined (SYS_getgroups)
1562 syscall_table[SYS_getgroups] = "getgroups";
1564 #if defined (SYS_fchmod)
1565 syscall_table[SYS_fchmod] = "fchmod";
1567 #if defined (SYS_fchown)
1568 syscall_table[SYS_fchown] = "fchown";
1570 #if defined (SYS_sigprocmask)
1571 syscall_table[SYS_sigprocmask] = "sigprocmask";
1573 #if defined (SYS_sigsuspend)
1574 syscall_table[SYS_sigsuspend] = "sigsuspend";
1576 #if defined (SYS_sigaltstack)
1577 syscall_table[SYS_sigaltstack] = "sigaltstack";
1579 #if defined (SYS_sigaction)
1580 syscall_table[SYS_sigaction] = "sigaction";
1582 #if defined (SYS_sigpending)
1583 syscall_table[SYS_sigpending] = "sigpending";
1585 #if defined (SYS_context)
1586 syscall_table[SYS_context] = "context";
1588 #if defined (SYS_evsys)
1589 syscall_table[SYS_evsys] = "evsys";
1591 #if defined (SYS_evtrapret)
1592 syscall_table[SYS_evtrapret] = "evtrapret";
1594 #if defined (SYS_statvfs)
1595 syscall_table[SYS_statvfs] = "statvfs";
1597 #if defined (SYS_fstatvfs)
1598 syscall_table[SYS_fstatvfs] = "fstatvfs";
1600 #if defined (SYS_nfssys)
1601 syscall_table[SYS_nfssys] = "nfssys";
1603 #if defined (SYS_waitsys)
1604 syscall_table[SYS_waitsys] = "waitsys";
1606 #if defined (SYS_sigsendsys)
1607 syscall_table[SYS_sigsendsys] = "sigsendsys";
1609 #if defined (SYS_hrtsys)
1610 syscall_table[SYS_hrtsys] = "hrtsys";
1612 #if defined (SYS_acancel)
1613 syscall_table[SYS_acancel] = "acancel";
1615 #if defined (SYS_async)
1616 syscall_table[SYS_async] = "async";
1618 #if defined (SYS_priocntlsys)
1619 syscall_table[SYS_priocntlsys] = "priocntlsys";
1621 #if defined (SYS_pathconf)
1622 syscall_table[SYS_pathconf] = "pathconf";
1624 #if defined (SYS_mincore)
1625 syscall_table[SYS_mincore] = "mincore";
1627 #if defined (SYS_mmap)
1628 syscall_table[SYS_mmap] = "mmap";
1630 #if defined (SYS_mprotect)
1631 syscall_table[SYS_mprotect] = "mprotect";
1633 #if defined (SYS_munmap)
1634 syscall_table[SYS_munmap] = "munmap";
1636 #if defined (SYS_fpathconf)
1637 syscall_table[SYS_fpathconf] = "fpathconf";
1639 #if defined (SYS_vfork)
1640 syscall_table[SYS_vfork] = "vfork";
1642 #if defined (SYS_fchdir)
1643 syscall_table[SYS_fchdir] = "fchdir";
1645 #if defined (SYS_readv)
1646 syscall_table[SYS_readv] = "readv";
1648 #if defined (SYS_writev)
1649 syscall_table[SYS_writev] = "writev";
1651 #if defined (SYS_xstat)
1652 syscall_table[SYS_xstat] = "xstat";
1654 #if defined (SYS_lxstat)
1655 syscall_table[SYS_lxstat] = "lxstat";
1657 #if defined (SYS_fxstat)
1658 syscall_table[SYS_fxstat] = "fxstat";
1660 #if defined (SYS_xmknod)
1661 syscall_table[SYS_xmknod] = "xmknod";
1663 #if defined (SYS_clocal)
1664 syscall_table[SYS_clocal] = "clocal";
1666 #if defined (SYS_setrlimit)
1667 syscall_table[SYS_setrlimit] = "setrlimit";
1669 #if defined (SYS_getrlimit)
1670 syscall_table[SYS_getrlimit] = "getrlimit";
1672 #if defined (SYS_lchown)
1673 syscall_table[SYS_lchown] = "lchown";
1675 #if defined (SYS_memcntl)
1676 syscall_table[SYS_memcntl] = "memcntl";
1678 #if defined (SYS_getpmsg)
1679 syscall_table[SYS_getpmsg] = "getpmsg";
1681 #if defined (SYS_putpmsg)
1682 syscall_table[SYS_putpmsg] = "putpmsg";
1684 #if defined (SYS_rename)
1685 syscall_table[SYS_rename] = "rename";
1687 #if defined (SYS_uname)
1688 syscall_table[SYS_uname] = "uname";
1690 #if defined (SYS_setegid)
1691 syscall_table[SYS_setegid] = "setegid";
1693 #if defined (SYS_sysconfig)
1694 syscall_table[SYS_sysconfig] = "sysconfig";
1696 #if defined (SYS_adjtime)
1697 syscall_table[SYS_adjtime] = "adjtime";
1699 #if defined (SYS_systeminfo)
1700 syscall_table[SYS_systeminfo] = "systeminfo";
1702 #if defined (SYS_seteuid)
1703 syscall_table[SYS_seteuid] = "seteuid";
1705 #if defined (SYS_sproc)
1706 syscall_table[SYS_sproc] = "sproc";
1708 #if defined (SYS_keyctl)
1709 syscall_table[SYS_keyctl] = "keyctl";
1711 #if defined (SYS_secsys)
1712 syscall_table[SYS_secsys] = "secsys";
1714 #if defined (SYS_filepriv)
1715 syscall_table[SYS_filepriv] = "filepriv";
1717 #if defined (SYS_procpriv)
1718 syscall_table[SYS_procpriv] = "procpriv";
1720 #if defined (SYS_devstat)
1721 syscall_table[SYS_devstat] = "devstat";
1723 #if defined (SYS_aclipc)
1724 syscall_table[SYS_aclipc] = "aclipc";
1726 #if defined (SYS_fdevstat)
1727 syscall_table[SYS_fdevstat] = "fdevstat";
1729 #if defined (SYS_flvlfile)
1730 syscall_table[SYS_flvlfile] = "flvlfile";
1732 #if defined (SYS_lvlfile)
1733 syscall_table[SYS_lvlfile] = "lvlfile";
1735 #if defined (SYS_lvlequal)
1736 syscall_table[SYS_lvlequal] = "lvlequal";
1738 #if defined (SYS_lvlproc)
1739 syscall_table[SYS_lvlproc] = "lvlproc";
1741 #if defined (SYS_lvlipc)
1742 syscall_table[SYS_lvlipc] = "lvlipc";
1744 #if defined (SYS_acl)
1745 syscall_table[SYS_acl] = "acl";
1747 #if defined (SYS_auditevt)
1748 syscall_table[SYS_auditevt] = "auditevt";
1750 #if defined (SYS_auditctl)
1751 syscall_table[SYS_auditctl] = "auditctl";
1753 #if defined (SYS_auditdmp)
1754 syscall_table[SYS_auditdmp] = "auditdmp";
1756 #if defined (SYS_auditlog)
1757 syscall_table[SYS_auditlog] = "auditlog";
1759 #if defined (SYS_auditbuf)
1760 syscall_table[SYS_auditbuf] = "auditbuf";
1762 #if defined (SYS_lvldom)
1763 syscall_table[SYS_lvldom] = "lvldom";
1765 #if defined (SYS_lvlvfs)
1766 syscall_table[SYS_lvlvfs] = "lvlvfs";
1768 #if defined (SYS_mkmld)
1769 syscall_table[SYS_mkmld] = "mkmld";
1771 #if defined (SYS_mldmode)
1772 syscall_table[SYS_mldmode] = "mldmode";
1774 #if defined (SYS_secadvise)
1775 syscall_table[SYS_secadvise] = "secadvise";
1777 #if defined (SYS_online)
1778 syscall_table[SYS_online] = "online";
1780 #if defined (SYS_setitimer)
1781 syscall_table[SYS_setitimer] = "setitimer";
1783 #if defined (SYS_getitimer)
1784 syscall_table[SYS_getitimer] = "getitimer";
1786 #if defined (SYS_gettimeofday)
1787 syscall_table[SYS_gettimeofday] = "gettimeofday";
1789 #if defined (SYS_settimeofday)
1790 syscall_table[SYS_settimeofday] = "settimeofday";
1792 #if defined (SYS_lwp_create)
1793 syscall_table[SYS_lwp_create] = "_lwp_create";
1795 #if defined (SYS_lwp_exit)
1796 syscall_table[SYS_lwp_exit] = "_lwp_exit";
1798 #if defined (SYS_lwp_wait)
1799 syscall_table[SYS_lwp_wait] = "_lwp_wait";
1801 #if defined (SYS_lwp_self)
1802 syscall_table[SYS_lwp_self] = "_lwp_self";
1804 #if defined (SYS_lwp_info)
1805 syscall_table[SYS_lwp_info] = "_lwp_info";
1807 #if defined (SYS_lwp_private)
1808 syscall_table[SYS_lwp_private] = "_lwp_private";
1810 #if defined (SYS_processor_bind)
1811 syscall_table[SYS_processor_bind] = "processor_bind";
1813 #if defined (SYS_processor_exbind)
1814 syscall_table[SYS_processor_exbind] = "processor_exbind";
1816 #if defined (SYS_prepblock)
1817 syscall_table[SYS_prepblock] = "prepblock";
1819 #if defined (SYS_block)
1820 syscall_table[SYS_block] = "block";
1822 #if defined (SYS_rdblock)
1823 syscall_table[SYS_rdblock] = "rdblock";
1825 #if defined (SYS_unblock)
1826 syscall_table[SYS_unblock] = "unblock";
1828 #if defined (SYS_cancelblock)
1829 syscall_table[SYS_cancelblock] = "cancelblock";
1831 #if defined (SYS_pread)
1832 syscall_table[SYS_pread] = "pread";
1834 #if defined (SYS_pwrite)
1835 syscall_table[SYS_pwrite] = "pwrite";
1837 #if defined (SYS_truncate)
1838 syscall_table[SYS_truncate] = "truncate";
1840 #if defined (SYS_ftruncate)
1841 syscall_table[SYS_ftruncate] = "ftruncate";
1843 #if defined (SYS_lwp_kill)
1844 syscall_table[SYS_lwp_kill] = "_lwp_kill";
1846 #if defined (SYS_sigwait)
1847 syscall_table[SYS_sigwait] = "sigwait";
1849 #if defined (SYS_fork1)
1850 syscall_table[SYS_fork1] = "fork1";
1852 #if defined (SYS_forkall)
1853 syscall_table[SYS_forkall] = "forkall";
1855 #if defined (SYS_modload)
1856 syscall_table[SYS_modload] = "modload";
1858 #if defined (SYS_moduload)
1859 syscall_table[SYS_moduload] = "moduload";
1861 #if defined (SYS_modpath)
1862 syscall_table[SYS_modpath] = "modpath";
1864 #if defined (SYS_modstat)
1865 syscall_table[SYS_modstat] = "modstat";
1867 #if defined (SYS_modadm)
1868 syscall_table[SYS_modadm] = "modadm";
1870 #if defined (SYS_getksym)
1871 syscall_table[SYS_getksym] = "getksym";
1873 #if defined (SYS_lwp_suspend)
1874 syscall_table[SYS_lwp_suspend] = "_lwp_suspend";
1876 #if defined (SYS_lwp_continue)
1877 syscall_table[SYS_lwp_continue] = "_lwp_continue";
1879 #if defined (SYS_priocntllst)
1880 syscall_table[SYS_priocntllst] = "priocntllst";
1882 #if defined (SYS_sleep)
1883 syscall_table[SYS_sleep] = "sleep";
1885 #if defined (SYS_lwp_sema_wait)
1886 syscall_table[SYS_lwp_sema_wait] = "_lwp_sema_wait";
1888 #if defined (SYS_lwp_sema_post)
1889 syscall_table[SYS_lwp_sema_post] = "_lwp_sema_post";
1891 #if defined (SYS_lwp_sema_trywait)
1892 syscall_table[SYS_lwp_sema_trywait] = "lwp_sema_trywait";
1894 #if defined(SYS_fstatvfs64)
1895 syscall_table[SYS_fstatvfs64] = "fstatvfs64";
1897 #if defined(SYS_statvfs64)
1898 syscall_table[SYS_statvfs64] = "statvfs64";
1900 #if defined(SYS_ftruncate64)
1901 syscall_table[SYS_ftruncate64] = "ftruncate64";
1903 #if defined(SYS_truncate64)
1904 syscall_table[SYS_truncate64] = "truncate64";
1906 #if defined(SYS_getrlimit64)
1907 syscall_table[SYS_getrlimit64] = "getrlimit64";
1909 #if defined(SYS_setrlimit64)
1910 syscall_table[SYS_setrlimit64] = "setrlimit64";
1912 #if defined(SYS_lseek64)
1913 syscall_table[SYS_lseek64] = "lseek64";
1915 #if defined(SYS_mmap64)
1916 syscall_table[SYS_mmap64] = "mmap64";
1918 #if defined(SYS_pread64)
1919 syscall_table[SYS_pread64] = "pread64";
1921 #if defined(SYS_creat64)
1922 syscall_table[SYS_creat64] = "creat64";
1924 #if defined(SYS_dshmsys)
1925 syscall_table[SYS_dshmsys] = "dshmsys";
1927 #if defined(SYS_invlpg)
1928 syscall_table[SYS_invlpg] = "invlpg";
1930 #if defined(SYS_cg_ids)
1931 syscall_table[SYS_cg_ids] = "cg_ids";
1933 #if defined(SYS_cg_processors)
1934 syscall_table[SYS_cg_processors] = "cg_processors";
1936 #if defined(SYS_cg_info)
1937 syscall_table[SYS_cg_info] = "cg_info";
1939 #if defined(SYS_cg_bind)
1940 syscall_table[SYS_cg_bind] = "cg_bind";
1942 #if defined(SYS_cg_current)
1943 syscall_table[SYS_cg_current] = "cg_current";
1945 #if defined(SYS_cg_memloc)
1946 syscall_table[SYS_cg_memloc] = "cg_memloc";
1954 procfs_kill_inferior - kill any current inferior
1958 void procfs_kill_inferior (void)
1962 Kill any current inferior.
1966 Kills even attached inferiors. Presumably the user has already
1967 been prompted that the inferior is an attached one rather than
1968 one started by gdb. (FIXME?)
1973 procfs_kill_inferior ()
1975 target_mourn_inferior ();
1982 unconditionally_kill_inferior - terminate the inferior
1986 static void unconditionally_kill_inferior (struct procinfo *)
1990 Kill the specified inferior.
1994 A possibly useful enhancement would be to first try sending
1995 the inferior a terminate signal, politely asking it to commit
1996 suicide, before we murder it (we could call that
1997 politely_kill_inferior()).
2002 unconditionally_kill_inferior (pi)
2003 struct procinfo *pi;
2006 struct proc_ctl pctl;
2008 ppid = pi->prstatus.pr_ppid;
2010 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
2011 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
2012 before the PIOCKILL, otherwise it might generate a corrupted core
2013 file for the inferior. */
2014 ioctl (pi->ctl_fd, PIOCSSIG, NULL);
2016 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
2017 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
2018 to kill the inferior, otherwise it might remain stopped with a
2020 We do not check the result of the PIOCSSIG, the inferior might have
2023 struct siginfo newsiginfo;
2025 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
2026 newsiginfo.si_signo = SIGKILL;
2027 newsiginfo.si_code = 0;
2028 newsiginfo.si_errno = 0;
2029 newsiginfo.si_pid = getpid ();
2030 newsiginfo.si_uid = getuid ();
2031 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
2033 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
2034 procfs_write_pckill (pi);
2035 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
2037 close_proc_file (pi);
2039 /* Only wait() for our direct children. Our grandchildren zombies are killed
2040 by the death of their parents. */
2042 if (ppid == getpid ())
2050 procfs_xfer_memory -- copy data to or from inferior memory space
2054 int procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
2055 int dowrite, struct target_ops target)
2059 Copy LEN bytes to/from inferior's memory starting at MEMADDR
2060 from/to debugger memory starting at MYADDR. Copy from inferior
2061 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
2063 Returns the length copied, which is either the LEN argument or
2064 zero. This xfer function does not do partial moves, since procfs_ops
2065 doesn't allow memory operations to cross below us in the target stack
2070 The /proc interface makes this an almost trivial task.
2074 procfs_xfer_memory (memaddr, myaddr, len, dowrite, target)
2079 struct target_ops *target; /* ignored */
2082 struct procinfo *pi;
2084 pi = current_procinfo;
2086 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
2090 nbytes = write (pi->as_fd, myaddr, len);
2094 nbytes = read (pi->as_fd, myaddr, len);
2108 procfs_store_registers -- copy register values back to inferior
2112 void procfs_store_registers (int regno)
2116 Store our current register values back into the inferior. If
2117 REGNO is -1 then store all the register, otherwise store just
2118 the value specified by REGNO.
2122 If we are storing only a single register, we first have to get all
2123 the current values from the process, overwrite the desired register
2124 in the gregset with the one we want from gdb's registers, and then
2125 send the whole set back to the process. For writing all the
2126 registers, all we have to do is generate the gregset and send it to
2129 Also note that the process has to be stopped on an event of interest
2130 for this to work, which basically means that it has to have been
2131 run under the control of one of the other /proc ioctl calls and not
2132 ptrace. Since we don't use ptrace anyway, we don't worry about this
2133 fine point, but it is worth noting for future reference.
2135 Gdb is confused about what this function is supposed to return.
2136 Some versions return a value, others return nothing. Some are
2137 declared to return a value and actually return nothing. Gdb ignores
2138 anything returned. (FIXME)
2143 procfs_store_registers (regno)
2146 struct procinfo *pi;
2147 #ifdef PROCFS_USE_READ_WRITE
2148 struct greg_ctl greg;
2149 struct fpreg_ctl fpreg;
2152 pi = current_procinfo;
2154 #ifdef PROCFS_USE_READ_WRITE
2157 procfs_read_status (pi);
2158 memcpy ((char *) &greg.gregset,
2159 (char *) &pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs,
2160 sizeof (gdb_gregset_t));
2162 fill_gregset (&greg.gregset, regno);
2164 write (pi->ctl_fd, &greg, sizeof (greg));
2165 #else /* PROCFS_USE_READ_WRITE */
2168 ioctl (pi->ctl_fd, PIOCGREG, &pi->gregset.gregset);
2170 fill_gregset (&pi->gregset.gregset, regno);
2171 ioctl (pi->ctl_fd, PIOCSREG, &pi->gregset.gregset);
2172 #endif /* PROCFS_USE_READ_WRITE */
2174 #if defined (FP0_REGNUM)
2176 /* Now repeat everything using the floating point register set, if the
2177 target has floating point hardware. Since we ignore the returned value,
2178 we'll never know whether it worked or not anyway. */
2180 #ifdef PROCFS_USE_READ_WRITE
2183 procfs_read_status (pi);
2184 memcpy ((char *) &fpreg.fpregset,
2185 (char *) &pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs,
2186 sizeof (gdb_fpregset_t));
2188 fill_fpregset (&fpreg.fpregset, regno);
2189 fpreg.cmd = PCSFPREG;
2190 write (pi->ctl_fd, &fpreg, sizeof (fpreg));
2191 #else /* PROCFS_USE_READ_WRITE */
2194 ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset.fpregset);
2196 fill_fpregset (&pi->fpregset.fpregset, regno);
2197 ioctl (pi->ctl_fd, PIOCSFPREG, &pi->fpregset.fpregset);
2198 #endif /* PROCFS_USE_READ_WRITE */
2200 #endif /* FP0_REGNUM */
2208 init_procinfo - setup a procinfo struct and connect it to a process
2212 struct procinfo * init_procinfo (int pid)
2216 Allocate a procinfo structure, open the /proc file and then set up the
2217 set of signals and faults that are to be traced. Returns a pointer to
2218 the new procinfo structure.
2222 If proc_init_failed ever gets called, control returns to the command
2223 processing loop via the standard error handling code.
2227 static struct procinfo *
2228 init_procinfo (pid, kill)
2232 struct procinfo *pi = (struct procinfo *)
2233 xmalloc (sizeof (struct procinfo));
2234 struct sig_ctl sctl;
2235 struct flt_ctl fctl;
2237 memset ((char *) pi, 0, sizeof (*pi));
2238 if (!open_proc_file (pid, pi, O_RDWR, 1))
2239 proc_init_failed (pi, "can't open process file", kill);
2241 /* open_proc_file may modify pid. */
2245 /* Add new process to process info list */
2247 pi->next = procinfo_list;
2250 add_fd (pi); /* Add to list for poll/select */
2252 /* Remember some things about the inferior that we will, or might, change
2253 so that we can restore them when we detach. */
2255 memcpy ((char *) &pi->saved_trace.sigset,
2256 (char *) &pi->prstatus.pr_sigtrace, sizeof (sigset_t));
2257 memcpy ((char *) &pi->saved_fltset.fltset,
2258 (char *) &pi->prstatus.pr_flttrace, sizeof (fltset_t));
2259 memcpy ((char *) &pi->saved_entryset.sysset,
2260 (char *) &pi->prstatus.pr_sysentry, sizeof (sysset_t));
2261 memcpy ((char *) &pi->saved_exitset.sysset,
2262 (char *) &pi->prstatus.pr_sysexit, sizeof (sysset_t));
2264 /* Set up trace and fault sets, as gdb expects them. */
2266 prfillset (&sctl.sigset);
2267 notice_signals (pi, &sctl);
2268 prfillset (&fctl.fltset);
2269 prdelset (&fctl.fltset, FLTPAGE);
2271 #else /* ! UNIXWARE */
2272 ioctl (pi->ctl_fd, PIOCGTRACE, &pi->saved_trace.sigset);
2273 ioctl (pi->ctl_fd, PIOCGHOLD, &pi->saved_sighold.sigset);
2274 ioctl (pi->ctl_fd, PIOCGFAULT, &pi->saved_fltset.fltset);
2275 ioctl (pi->ctl_fd, PIOCGENTRY, &pi->saved_entryset.sysset);
2276 ioctl (pi->ctl_fd, PIOCGEXIT, &pi->saved_exitset.sysset);
2278 /* Set up trace and fault sets, as gdb expects them. */
2280 memset ((char *) &pi->prrun, 0, sizeof (pi->prrun));
2281 prfillset (&pi->prrun.pr_trace);
2282 procfs_notice_signals (pid);
2283 prfillset (&pi->prrun.pr_fault);
2284 prdelset (&pi->prrun.pr_fault, FLTPAGE);
2285 #ifdef PROCFS_DONT_TRACE_FAULTS
2286 premptyset (&pi->prrun.pr_fault);
2288 #endif /* UNIXWARE */
2290 if (!procfs_read_status (pi))
2291 proc_init_failed (pi, "procfs_read_status failed", kill);
2300 create_procinfo - initialize access to a /proc entry
2304 struct procinfo * create_procinfo (int pid)
2308 Allocate a procinfo structure, open the /proc file and then set up the
2309 set of signals and faults that are to be traced. Returns a pointer to
2310 the new procinfo structure.
2314 If proc_init_failed ever gets called, control returns to the command
2315 processing loop via the standard error handling code.
2319 static struct procinfo *
2320 create_procinfo (pid)
2323 struct procinfo *pi;
2324 struct sig_ctl sctl;
2325 struct flt_ctl fctl;
2327 pi = find_procinfo (pid, 1);
2329 return pi; /* All done! It already exists */
2331 pi = init_procinfo (pid, 1);
2334 /* A bug in Solaris (2.5 at least) causes PIOCWSTOP to hang on LWPs that are
2335 already stopped, even if they all have PR_ASYNC set. */
2336 if (!(pi->prstatus.pr_flags & PR_STOPPED))
2338 if (!procfs_write_pcwstop (pi))
2339 proc_init_failed (pi, "procfs_write_pcwstop failed", 1);
2341 #ifdef PROCFS_USE_READ_WRITE
2342 fctl.cmd = PCSFAULT;
2343 if (write (pi->ctl_fd, (char *) &fctl, sizeof (struct flt_ctl)) < 0)
2344 proc_init_failed (pi, "PCSFAULT failed", 1);
2346 if (ioctl (pi->ctl_fd, PIOCSFAULT, &pi->prrun.pr_fault) < 0)
2347 proc_init_failed (pi, "PIOCSFAULT failed", 1);
2357 procfs_exit_handler - handle entry into the _exit syscall
2361 int procfs_exit_handler (pi, syscall_num, why, rtnvalp, statvalp)
2365 This routine is called when an inferior process enters the _exit()
2366 system call. It continues the process, and then collects the exit
2367 status and pid which are returned in *statvalp and *rtnvalp. After
2368 that it returns non-zero to indicate that procfs_wait should wake up.
2371 There is probably a better way to do this.
2376 procfs_exit_handler (pi, syscall_num, why, rtnvalp, statvalp)
2377 struct procinfo *pi;
2383 struct procinfo *temp_pi, *next_pi;
2384 struct proc_ctl pctl;
2388 pctl.data = PRCFAULT;
2390 pi->prrun.pr_flags = PRCFAULT;
2393 #ifdef PROCFS_USE_READ_WRITE
2394 if (write (pi->ctl_fd, (char *) &pctl, sizeof (struct proc_ctl)) < 0)
2396 if (ioctl (pi->ctl_fd, PIOCRUN, &pi->prrun) != 0)
2398 perror_with_name (pi->pathname);
2402 /* Claim it exited (don't call wait). */
2404 printf_filtered ("(attached process has exited)\n");
2406 *rtnvalp = inferior_pid;
2410 *rtnvalp = wait (statvalp);
2415 /* Close ALL open proc file handles,
2416 except the one that called SYS_exit. */
2417 for (temp_pi = procinfo_list; temp_pi; temp_pi = next_pi)
2419 next_pi = temp_pi->next;
2421 continue; /* Handled below */
2422 close_proc_file (temp_pi);
2431 procfs_exec_handler - handle exit from the exec family of syscalls
2435 int procfs_exec_handler (pi, syscall_num, why, rtnvalp, statvalp)
2439 This routine is called when an inferior process is about to finish any
2440 of the exec() family of system calls. It pretends that we got a
2441 SIGTRAP (for compatibility with ptrace behavior), and returns non-zero
2442 to tell procfs_wait to wake up.
2445 This need for compatibility with ptrace is questionable. In the
2446 future, it shouldn't be necessary.
2451 procfs_exec_handler (pi, syscall_num, why, rtnvalp, statvalp)
2452 struct procinfo *pi;
2458 *statvalp = (SIGTRAP << 8) | 0177;
2463 #if defined(SYS_sproc) && !defined(UNIXWARE)
2464 /* IRIX lwp creation system call */
2470 procfs_sproc_handler - handle exit from the sproc syscall
2474 int procfs_sproc_handler (pi, syscall_num, why, rtnvalp, statvalp)
2478 This routine is called when an inferior process is about to finish an
2479 sproc() system call. This is the system call that IRIX uses to create
2480 a lightweight process. When the target process gets this event, we can
2481 look at rval1 to find the new child processes ID, and create a new
2482 procinfo struct from that.
2484 After that, it pretends that we got a SIGTRAP, and returns non-zero
2485 to tell procfs_wait to wake up. Subsequently, wait_for_inferior gets
2486 woken up, sees the new process and continues it.
2489 We actually never see the child exiting from sproc because we will
2490 shortly stop the child with PIOCSTOP, which is then registered as the
2495 procfs_sproc_handler (pi, syscall_num, why, rtnvalp, statvalp)
2496 struct procinfo *pi;
2502 /* We've just detected the completion of an sproc system call. Now we need to
2503 setup a procinfo struct for this thread, and notify the thread system of the
2506 /* If sproc failed, then nothing interesting happened. Continue the process
2507 and go back to sleep. */
2509 if (pi->prstatus.pr_errno != 0)
2511 pi->prrun.pr_flags &= PRSTEP;
2512 pi->prrun.pr_flags |= PRCFAULT;
2514 if (ioctl (pi->ctl_fd, PIOCRUN, &pi->prrun) != 0)
2515 perror_with_name (pi->pathname);
2520 /* At this point, the new thread is stopped at it's first instruction, and
2521 the parent is stopped at the exit from sproc. */
2523 /* Notify the caller of the arrival of a new thread. */
2524 create_procinfo (pi->prstatus.pr_rval1);
2526 *rtnvalp = pi->prstatus.pr_rval1;
2527 *statvalp = (SIGTRAP << 8) | 0177;
2536 procfs_fork_handler - handle exit from the fork syscall
2540 int procfs_fork_handler (pi, syscall_num, why, rtnvalp, statvalp)
2544 This routine is called when an inferior process is about to finish a
2545 fork() system call. We will open up the new process, and then close
2546 it, which releases it from the clutches of the debugger.
2548 After that, we continue the target process as though nothing had
2552 This is necessary for IRIX because we have to set PR_FORK in order
2553 to catch the creation of lwps (via sproc()). When an actual fork
2554 occurs, it becomes necessary to reset the forks debugger flags and
2555 continue it because we can't hack multiple processes yet.
2559 procfs_fork_handler (pi, syscall_num, why, rtnvalp, statvalp)
2560 struct procinfo *pi;
2566 struct procinfo *pitemp;
2568 /* At this point, we've detected the completion of a fork (or vfork) call in
2569 our child. The grandchild is also stopped because we set inherit-on-fork
2570 earlier. (Note that nobody has the grandchilds' /proc file open at this
2571 point.) We will release the grandchild from the debugger by opening it's
2572 /proc file and then closing it. Since run-on-last-close is set, the
2573 grandchild continues on its' merry way. */
2576 pitemp = create_procinfo (pi->prstatus.pr_rval1);
2578 close_proc_file (pitemp);
2580 if (ioctl (pi->ctl_fd, PIOCRUN, &pi->prrun) != 0)
2581 perror_with_name (pi->pathname);
2585 #endif /* SYS_sproc && !UNIXWARE */
2591 procfs_set_inferior_syscall_traps - setup the syscall traps
2595 void procfs_set_inferior_syscall_traps (struct procinfo *pip)
2599 Called for each "procinfo" (process, thread, or LWP) in the
2600 inferior, to register for notification of and handlers for
2601 syscall traps in the inferior.
2606 procfs_set_inferior_syscall_traps (pip)
2607 struct procinfo *pip;
2609 procfs_set_syscall_trap (pip, SYS_exit, PROCFS_SYSCALL_ENTRY,
2610 procfs_exit_handler);
2612 #ifndef PRFS_STOPEXEC
2614 procfs_set_syscall_trap (pip, SYS_exec, PROCFS_SYSCALL_EXIT,
2615 procfs_exec_handler);
2618 procfs_set_syscall_trap (pip, SYS_execv, PROCFS_SYSCALL_EXIT,
2619 procfs_exec_handler);
2622 procfs_set_syscall_trap (pip, SYS_execve, PROCFS_SYSCALL_EXIT,
2623 procfs_exec_handler);
2625 #endif /* PRFS_STOPEXEC */
2627 /* Setup traps on exit from sproc() */
2630 procfs_set_syscall_trap (pip, SYS_sproc, PROCFS_SYSCALL_EXIT,
2631 procfs_sproc_handler);
2632 procfs_set_syscall_trap (pip, SYS_fork, PROCFS_SYSCALL_EXIT,
2633 procfs_fork_handler);
2635 procfs_set_syscall_trap (pip, SYS_vfork, PROCFS_SYSCALL_EXIT,
2636 procfs_fork_handler);
2638 /* Turn on inherit-on-fork flag so that all children of the target process
2639 start with tracing flags set. This allows us to trap lwp creation. Note
2640 that we also have to trap on fork and vfork in order to disable all tracing
2641 in the targets child processes. */
2643 modify_inherit_on_fork_flag (pip->ctl_fd, 1);
2646 #ifdef SYS_lwp_create
2647 procfs_set_syscall_trap (pip, SYS_lwp_create, PROCFS_SYSCALL_EXIT,
2648 procfs_lwp_creation_handler);
2656 procfs_init_inferior - initialize target vector and access to a
2661 void procfs_init_inferior (int pid)
2665 When gdb starts an inferior, this function is called in the parent
2666 process immediately after the fork. It waits for the child to stop
2667 on the return from the exec system call (the child itself takes care
2668 of ensuring that this is set up), then sets up the set of signals
2669 and faults that are to be traced. Returns the pid, which may have had
2670 the thread-id added to it.
2674 If proc_init_failed ever gets called, control returns to the command
2675 processing loop via the standard error handling code.
2680 procfs_init_inferior (pid)
2683 struct procinfo *pip;
2685 push_target (&procfs_ops);
2687 pip = create_procinfo (pid);
2689 procfs_set_inferior_syscall_traps (pip);
2691 /* create_procinfo may change the pid, so we have to update inferior_pid
2692 here before calling other gdb routines that need the right pid. */
2697 add_thread (pip->pid); /* Setup initial thread */
2699 #ifdef START_INFERIOR_TRAPS_EXPECTED
2700 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
2702 /* One trap to exec the shell, one to exec the program being debugged. */
2703 startup_inferior (2);
2711 procfs_notice_signals
2715 static void procfs_notice_signals (int pid);
2719 When the user changes the state of gdb's signal handling via the
2720 "handle" command, this function gets called to see if any change
2721 in the /proc interface is required. It is also called internally
2722 by other /proc interface functions to initialize the state of
2723 the traced signal set.
2725 One thing it does is that signals for which the state is "nostop",
2726 "noprint", and "pass", have their trace bits reset in the pr_trace
2727 field, so that they are no longer traced. This allows them to be
2728 delivered directly to the inferior without the debugger ever being
2733 procfs_notice_signals (pid)
2736 struct procinfo *pi;
2737 struct sig_ctl sctl;
2739 pi = find_procinfo (pid, 0);
2741 #ifndef HAVE_PRRUN_T
2742 premptyset (&sctl.sigset);
2744 sctl.sigset = pi->prrun.pr_trace;
2747 notice_signals (pi, &sctl);
2750 pi->prrun.pr_trace = sctl.sigset;
2755 notice_signals (pi, sctl)
2756 struct procinfo *pi;
2757 struct sig_ctl *sctl;
2761 for (signo = 0; signo < NSIG; signo++)
2763 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
2764 signal_print_state (target_signal_from_host (signo)) == 0 &&
2765 signal_pass_state (target_signal_from_host (signo)) == 1)
2767 prdelset (&sctl->sigset, signo);
2771 praddset (&sctl->sigset, signo);
2774 #ifdef PROCFS_USE_READ_WRITE
2775 sctl->cmd = PCSTRACE;
2776 if (write (pi->ctl_fd, (char *) sctl, sizeof (struct sig_ctl)) < 0)
2778 if (ioctl (pi->ctl_fd, PIOCSTRACE, &sctl->sigset))
2781 print_sys_errmsg ("PIOCSTRACE failed", errno);
2789 proc_set_exec_trap -- arrange for exec'd child to halt at startup
2793 void proc_set_exec_trap (void)
2797 This function is called in the child process when starting up
2798 an inferior, prior to doing the exec of the actual inferior.
2799 It sets the child process's exitset to make exit from the exec
2800 system call an event of interest to stop on, and then simply
2801 returns. The child does the exec, the system call returns, and
2802 the child stops at the first instruction, ready for the gdb
2803 parent process to take control of it.
2807 We need to use all local variables since the child may be sharing
2808 it's data space with the parent, if vfork was used rather than
2811 Also note that we want to turn off the inherit-on-fork flag in
2812 the child process so that any grand-children start with all
2813 tracing flags cleared.
2817 proc_set_exec_trap ()
2819 struct sys_ctl exitset;
2820 struct sys_ctl entryset;
2821 char procname[MAX_PROC_NAME_SIZE];
2824 sprintf (procname, CTL_PROC_NAME_FMT, getpid ());
2826 if ((fd = open (procname, O_WRONLY)) < 0)
2828 if ((fd = open (procname, O_RDWR)) < 0)
2832 gdb_flush (gdb_stderr);
2835 premptyset (&exitset.sysset);
2836 premptyset (&entryset.sysset);
2838 #ifdef PRFS_STOPEXEC
2839 /* Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
2840 exits from exec system calls because of the user level loader. */
2844 if (ioctl (fd, PIOCGSPCACT, &prfs_flags) < 0)
2847 gdb_flush (gdb_stderr);
2850 prfs_flags |= PRFS_STOPEXEC;
2851 if (ioctl (fd, PIOCSSPCACT, &prfs_flags) < 0)
2854 gdb_flush (gdb_stderr);
2858 #else /* PRFS_STOPEXEC */
2860 Not all systems with /proc have all the exec* syscalls with the same
2861 names. On the SGI, for example, there is no SYS_exec, but there
2862 *is* a SYS_execv. So, we try to account for that. */
2865 praddset (&exitset.sysset, SYS_exec);
2868 praddset (&exitset.sysset, SYS_execve);
2871 praddset (&exitset.sysset, SYS_execv);
2874 #ifdef PROCFS_USE_READ_WRITE
2875 exitset.cmd = PCSEXIT;
2876 if (write (fd, (char *) &exitset, sizeof (struct sys_ctl)) < 0)
2878 if (ioctl (fd, PIOCSEXIT, &exitset.sysset) < 0)
2882 gdb_flush (gdb_stderr);
2885 #endif /* PRFS_STOPEXEC */
2887 praddset (&entryset.sysset, SYS_exit);
2889 #ifdef PROCFS_USE_READ_WRITE
2890 entryset.cmd = PCSENTRY;
2891 if (write (fd, (char *) &entryset, sizeof (struct sys_ctl)) < 0)
2893 if (ioctl (fd, PIOCSENTRY, &entryset.sysset) < 0)
2897 gdb_flush (gdb_stderr);
2901 /* Turn off inherit-on-fork flag so that all grand-children of gdb
2902 start with tracing flags cleared. */
2904 modify_inherit_on_fork_flag (fd, 0);
2906 /* Turn on run-on-last-close flag so that this process will not hang
2907 if GDB goes away for some reason. */
2909 modify_run_on_last_close_flag (fd, 1);
2911 #ifndef UNIXWARE /* since this is a solaris-ism, we don't want it */
2912 /* NOTE: revisit when doing thread support for UW */
2916 struct proc_ctl pctl;
2918 /* Solaris needs this to make procfs treat all threads seperately. Without
2919 this, all threads halt whenever something happens to any thread. Since
2920 GDB wants to control all this itself, it needs to set PR_ASYNC. */
2922 pr_flags = PR_ASYNC;
2923 #ifdef PROCFS_USE_READ_WRITE
2925 pctl.data = PR_FORK | PR_ASYNC;
2926 write (fd, (char *) &pctl, sizeof (struct proc_ctl));
2928 ioctl (fd, PIOCSET, &pr_flags);
2931 #endif /* PR_ASYNC */
2932 #endif /* !UNIXWARE */
2939 proc_iterate_over_mappings -- call function for every mapped space
2943 int proc_iterate_over_mappings (int (*func)())
2947 Given a pointer to a function, call that function for every
2948 mapped address space, passing it an open file descriptor for
2949 the file corresponding to that mapped address space (if any)
2950 and the base address of the mapped space. Quit when we hit
2951 the end of the mappings or the function returns nonzero.
2956 proc_iterate_over_mappings (func)
2957 int (*func) PARAMS ((int, CORE_ADDR));
2964 struct procinfo *pi;
2967 pi = current_procinfo;
2969 if (fstat (pi->map_fd, &sbuf) < 0)
2972 nmap = sbuf.st_size / sizeof (prmap_t);
2973 prmaps = (prmap_t *) alloca (nmap * sizeof (prmap_t));
2974 if ((lseek (pi->map_fd, 0, SEEK_SET) == 0) &&
2975 (read (pi->map_fd, (char *) prmaps, nmap * sizeof (prmap_t)) ==
2976 (nmap * sizeof (prmap_t))))
2979 for (prmap = prmaps; i < nmap && funcstat == 0; ++prmap, ++i)
2981 char name[sizeof ("/proc/1234567890/object") +
2982 sizeof (prmap->pr_mapname)];
2983 sprintf (name, "/proc/%d/object/%s", pi->pid, prmap->pr_mapname);
2984 if ((fd = open (name, O_RDONLY)) == -1)
2989 funcstat = (*func) (fd, (CORE_ADDR) prmap->pr_vaddr);
2995 #else /* UNIXWARE */
2997 proc_iterate_over_mappings (func)
2998 int (*func) PARAMS ((int, CORE_ADDR));
3003 struct prmap *prmaps;
3004 struct prmap *prmap;
3005 struct procinfo *pi;
3007 pi = current_procinfo;
3009 if (ioctl (pi->map_fd, PIOCNMAP, &nmap) == 0)
3011 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
3012 if (ioctl (pi->map_fd, PIOCMAP, prmaps) == 0)
3014 for (prmap = prmaps; prmap->pr_size && funcstat == 0; ++prmap)
3016 fd = proc_address_to_fd (pi, (CORE_ADDR) prmap->pr_vaddr, 0);
3017 funcstat = (*func) (fd, (CORE_ADDR) prmap->pr_vaddr);
3024 #endif /* UNIXWARE */
3026 #if 0 /* Currently unused */
3031 proc_base_address -- find base address for segment containing address
3035 CORE_ADDR proc_base_address (CORE_ADDR addr)
3039 Given an address of a location in the inferior, find and return
3040 the base address of the mapped segment containing that address.
3042 This is used for example, by the shared library support code,
3043 where we have the pc value for some location in the shared library
3044 where we are stopped, and need to know the base address of the
3045 segment containing that address.
3049 proc_base_address (addr)
3053 struct prmap *prmaps;
3054 struct prmap *prmap;
3055 CORE_ADDR baseaddr = 0;
3056 struct procinfo *pi;
3058 pi = current_procinfo;
3060 if (ioctl (pi->map_fd, PIOCNMAP, &nmap) == 0)
3062 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
3063 if (ioctl (pi->map_fd, PIOCMAP, prmaps) == 0)
3065 for (prmap = prmaps; prmap->pr_size; ++prmap)
3067 if ((prmap->pr_vaddr <= (caddr_t) addr) &&
3068 (prmap->pr_vaddr + prmap->pr_size > (caddr_t) addr))
3070 baseaddr = (CORE_ADDR) prmap->pr_vaddr;
3086 proc_address_to_fd -- return open fd for file mapped to address
3090 int proc_address_to_fd (struct procinfo *pi, CORE_ADDR addr, complain)
3094 Given an address in the current inferior's address space, use the
3095 /proc interface to find an open file descriptor for the file that
3096 this address was mapped in from. Return -1 if there is no current
3097 inferior. Print a warning message if there is an inferior but
3098 the address corresponds to no file (IE a bogus address).
3103 proc_address_to_fd (pi, addr, complain)
3104 struct procinfo *pi;
3110 if ((fd = ioctl (pi->ctl_fd, PIOCOPENM, (caddr_t *) & addr)) < 0)
3114 print_sys_errmsg (pi->pathname, errno);
3115 warning ("can't find mapped file for address 0x%x", addr);
3120 #endif /* !UNIXWARE */
3122 /* Attach to process PID, then initialize for debugging it
3123 and wait for the trace-trap that results from attaching. */
3126 procfs_attach (args, from_tty)
3134 error_no_arg ("process-id to attach");
3138 if (pid == getpid ()) /* Trying to masturbate? */
3139 error ("I refuse to debug myself!");
3143 exec_file = (char *) get_exec_file (0);
3146 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, target_pid_to_str (pid));
3148 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid));
3150 gdb_flush (gdb_stdout);
3153 inferior_pid = pid = do_attach (pid);
3154 push_target (&procfs_ops);
3158 /* Take a program previously attached to and detaches it.
3159 The program resumes execution and will no longer stop
3160 on signals, etc. We'd better not have left any breakpoints
3161 in the program or it'll die when it hits one. For this
3162 to work, it may be necessary for the process to have been
3163 previously attached. It *might* work if the program was
3164 started via the normal ptrace (PTRACE_TRACEME). */
3167 procfs_detach (args, from_tty)
3175 char *exec_file = get_exec_file (0);
3178 printf_unfiltered ("Detaching from program: %s %s\n",
3179 exec_file, target_pid_to_str (inferior_pid));
3180 gdb_flush (gdb_stdout);
3183 siggnal = atoi (args);
3185 do_detach (siggnal);
3187 unpush_target (&procfs_ops); /* Pop out of handling an inferior */
3190 /* Get ready to modify the registers array. On machines which store
3191 individual registers, this doesn't need to do anything. On machines
3192 which store all the registers in one fell swoop, this makes sure
3193 that registers contains all the registers from the program being
3197 procfs_prepare_to_store ()
3199 #ifdef CHILD_PREPARE_TO_STORE
3200 CHILD_PREPARE_TO_STORE ();
3204 /* Print status information about what we're accessing. */
3207 procfs_files_info (ignore)
3208 struct target_ops *ignore;
3210 printf_unfiltered ("\tUsing the running image of %s %s via /proc.\n",
3211 attach_flag ? "attached" : "child", target_pid_to_str (inferior_pid));
3216 procfs_open (arg, from_tty)
3220 error ("Use the \"run\" command to start a Unix child process.");
3227 do_attach -- attach to an already existing process
3231 int do_attach (int pid)
3235 Attach to an already existing process with the specified process
3236 id. If the process is not already stopped, query whether to
3241 The option of stopping at attach time is specific to the /proc
3242 versions of gdb. Versions using ptrace force the attachee
3243 to stop. (I have changed this version to do so, too. All you
3244 have to do is "continue" to make it go on. -- gnu@cygnus.com)
3252 struct procinfo *pi;
3253 struct sig_ctl sctl;
3254 struct flt_ctl fctl;
3257 pi = init_procinfo (pid, 0);
3260 nlwp = pi->prstatus.pr_nlwp;
3261 lwps = alloca ((2 * nlwp + 2) * sizeof (id_t));
3263 if (ioctl (pi->ctl_fd, PIOCLWPIDS, lwps))
3265 print_sys_errmsg (pi->pathname, errno);
3266 error ("PIOCLWPIDS failed");
3268 #else /* PIOCLWPIDS */
3270 lwps = alloca ((2 * nlwp + 2) * sizeof *lwps);
3273 for (; nlwp > 0; nlwp--, lwps++)
3275 /* First one has already been created above. */
3276 if ((pi = find_procinfo ((*lwps << 16) | pid, 1)) == 0)
3277 pi = init_procinfo ((*lwps << 16) | pid, 0);
3279 if (THE_PR_LWP (pi->prstatus).pr_flags & (PR_STOPPED | PR_ISTOP))
3281 pi->was_stopped = 1;
3285 pi->was_stopped = 0;
3286 if (1 || query ("Process is currently running, stop it? "))
3289 /* Make it run again when we close it. */
3290 modify_run_on_last_close_flag (pi->ctl_fd, 1);
3291 #ifdef PROCFS_USE_READ_WRITE
3293 if (write (pi->ctl_fd, (char *) &cmd, sizeof (long)) < 0)
3295 if (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) < 0)
3298 print_sys_errmsg (pi->pathname, errno);
3299 close_proc_file (pi);
3300 error ("PIOCSTOP failed");
3303 if (!procfs_read_status (pi))
3305 print_sys_errmsg (pi->pathname, errno);
3306 close_proc_file (pi);
3307 error ("procfs_read_status failed");
3310 pi->nopass_next_sigstop = 1;
3314 printf_unfiltered ("Ok, gdb will wait for %s to stop.\n",
3315 target_pid_to_str (pi->pid));
3319 #ifdef PROCFS_USE_READ_WRITE
3320 fctl.cmd = PCSFAULT;
3321 if (write (pi->ctl_fd, (char *) &fctl, sizeof (struct flt_ctl)) < 0)
3322 print_sys_errmsg ("PCSFAULT failed", errno);
3323 #else /* PROCFS_USE_READ_WRITE */
3324 if (ioctl (pi->ctl_fd, PIOCSFAULT, &pi->prrun.pr_fault))
3326 print_sys_errmsg ("PIOCSFAULT failed", errno);
3328 if (ioctl (pi->ctl_fd, PIOCSTRACE, &pi->prrun.pr_trace))
3330 print_sys_errmsg ("PIOCSTRACE failed", errno);
3332 add_thread (pi->pid);
3333 procfs_set_inferior_syscall_traps (pi);
3334 #endif /* PROCFS_USE_READ_WRITE */
3344 do_detach -- detach from an attached-to process
3348 void do_detach (int signal)
3352 Detach from the current attachee.
3354 If signal is non-zero, the attachee is started running again and sent
3355 the specified signal.
3357 If signal is zero and the attachee was not already stopped when we
3358 attached to it, then we make it runnable again when we detach.
3360 Otherwise, we query whether or not to make the attachee runnable
3361 again, since we may simply want to leave it in the state it was in
3364 We report any problems, but do not consider them errors, since we
3365 MUST detach even if some things don't seem to go right. This may not
3366 be the ideal situation. (FIXME).
3373 struct procinfo *pi;
3375 for (pi = procinfo_list; pi; pi = pi->next)
3379 set_proc_siginfo (pi, signal);
3381 #ifdef PROCFS_USE_READ_WRITE
3382 pi->saved_exitset.cmd = PCSEXIT;
3383 if (write (pi->ctl_fd, (char *) &pi->saved_exitset,
3384 sizeof (struct sys_ctl)) < 0)
3386 if (ioctl (pi->ctl_fd, PIOCSEXIT, &pi->saved_exitset.sysset) < 0)
3389 print_sys_errmsg (pi->pathname, errno);
3390 printf_unfiltered ("PIOCSEXIT failed.\n");
3392 #ifdef PROCFS_USE_READ_WRITE
3393 pi->saved_entryset.cmd = PCSENTRY;
3394 if (write (pi->ctl_fd, (char *) &pi->saved_entryset,
3395 sizeof (struct sys_ctl)) < 0)
3397 if (ioctl (pi->ctl_fd, PIOCSENTRY, &pi->saved_entryset.sysset) < 0)
3400 print_sys_errmsg (pi->pathname, errno);
3401 printf_unfiltered ("PIOCSENTRY failed.\n");
3403 #ifdef PROCFS_USE_READ_WRITE
3404 pi->saved_trace.cmd = PCSTRACE;
3405 if (write (pi->ctl_fd, (char *) &pi->saved_trace,
3406 sizeof (struct sig_ctl)) < 0)
3408 if (ioctl (pi->ctl_fd, PIOCSTRACE, &pi->saved_trace.sigset) < 0)
3411 print_sys_errmsg (pi->pathname, errno);
3412 printf_unfiltered ("PIOCSTRACE failed.\n");
3415 if (ioctl (pi->ctl_fd, PIOCSHOLD, &pi->saved_sighold.sigset) < 0)
3417 print_sys_errmsg (pi->pathname, errno);
3418 printf_unfiltered ("PIOSCHOLD failed.\n");
3421 #ifdef PROCFS_USE_READ_WRITE
3422 pi->saved_fltset.cmd = PCSFAULT;
3423 if (write (pi->ctl_fd, (char *) &pi->saved_fltset,
3424 sizeof (struct flt_ctl)) < 0)
3426 if (ioctl (pi->ctl_fd, PIOCSFAULT, &pi->saved_fltset.fltset) < 0)
3429 print_sys_errmsg (pi->pathname, errno);
3430 printf_unfiltered ("PIOCSFAULT failed.\n");
3432 if (!procfs_read_status (pi))
3434 print_sys_errmsg (pi->pathname, errno);
3435 printf_unfiltered ("procfs_read_status failed.\n");
3440 || (THE_PR_LWP (pi->prstatus).pr_flags & (PR_STOPPED | PR_ISTOP)))
3443 struct proc_ctl pctl;
3445 if (signal || !pi->was_stopped ||
3446 query ("Was stopped when attached, make it runnable again? "))
3448 /* Clear any pending signal if we want to detach without
3451 set_proc_siginfo (pi, signal);
3453 /* Clear any fault that might have stopped it. */
3454 #ifdef PROCFS_USE_READ_WRITE
3456 if (write (pi->ctl_fd, (char *) &cmd, sizeof (long)) < 0)
3458 if (ioctl (pi->ctl_fd, PIOCCFAULT, 0))
3461 print_sys_errmsg (pi->pathname, errno);
3462 printf_unfiltered ("PIOCCFAULT failed.\n");
3465 /* Make it run again when we close it. */
3467 modify_run_on_last_close_flag (pi->ctl_fd, 1);
3471 close_proc_file (pi);
3476 /* emulate wait() as much as possible.
3477 Wait for child to do something. Return pid of child, or -1 in case
3478 of error; store status in *OURSTATUS.
3480 Not sure why we can't
3481 just use wait(), but it seems to have problems when applied to a
3482 process being controlled with the /proc interface.
3484 We have a race problem here with no obvious solution. We need to let
3485 the inferior run until it stops on an event of interest, which means
3486 that we need to use the PIOCWSTOP ioctl. However, we cannot use this
3487 ioctl if the process is already stopped on something that is not an
3488 event of interest, or the call will hang indefinitely. Thus we first
3489 use PIOCSTATUS to see if the process is not stopped. If not, then we
3490 use PIOCWSTOP. But during the window between the two, if the process
3491 stops for any reason that is not an event of interest (such as a job
3492 control signal) then gdb will hang. One possible workaround is to set
3493 an alarm to wake up every minute of so and check to see if the process
3494 is still running, and if so, then reissue the PIOCWSTOP. But this is
3495 a real kludge, so has not been implemented. FIXME: investigate
3498 FIXME: Investigate why wait() seems to have problems with programs
3499 being control by /proc routines. */
3501 procfs_wait (pid, ourstatus)
3503 struct target_waitstatus *ourstatus;
3510 struct procinfo *pi;
3511 struct proc_ctl pctl;
3515 /* handle all syscall events first, otherwise we might not
3516 notice a thread was created until too late. */
3518 for (pi = procinfo_list; pi; pi = pi->next)
3523 if (!(THE_PR_LWP (pi->prstatus).pr_flags & (PR_STOPPED | PR_ISTOP)))
3526 why = THE_PR_LWP (pi->prstatus).pr_why;
3527 what = THE_PR_LWP (pi->prstatus).pr_what;
3528 if (why == PR_SYSENTRY || why == PR_SYSEXIT)
3531 int found_handler = 0;
3533 for (i = 0; i < pi->num_syscall_handlers; i++)
3534 if (pi->syscall_handlers[i].syscall_num == what)
3537 pi->saved_rtnval = pi->pid;
3538 pi->saved_statval = 0;
3539 if (!pi->syscall_handlers[i].func
3540 (pi, what, why, &pi->saved_rtnval, &pi->saved_statval))
3547 if (why == PR_SYSENTRY)
3548 error ("PR_SYSENTRY, unhandled system call %d", what);
3550 error ("PR_SYSEXIT, unhandled system call %d", what);
3555 /* find a relevant process with an event */
3557 for (pi = procinfo_list; pi; pi = pi->next)
3558 if (pi->had_event && (pid == -1 || pi->pid == pid))
3568 && !(THE_PR_LWP (pi->prstatus).pr_flags & (PR_STOPPED | PR_ISTOP)))
3570 if (!procfs_write_pcwstop (pi))
3577 if (errno == ENOENT)
3579 /* XXX Fixme -- what to do if attached? Can't call wait... */
3580 rtnval = wait (&statval);
3581 if ((rtnval) != (PIDGET (inferior_pid)))
3583 print_sys_errmsg (pi->pathname, errno);
3584 error ("procfs_wait: wait failed, returned %d", rtnval);
3590 print_sys_errmsg (pi->pathname, errno);
3591 error ("PIOCSTATUS or PIOCWSTOP failed.");
3595 else if (THE_PR_LWP (pi->prstatus).pr_flags & (PR_STOPPED | PR_ISTOP))
3598 rtnval = pi->prstatus.pr_pid;
3602 why = THE_PR_LWP (pi->prstatus).pr_why;
3603 what = THE_PR_LWP (pi->prstatus).pr_what;
3608 statval = (what << 8) | 0177;
3612 rtnval = pi->saved_rtnval;
3613 statval = pi->saved_statval;
3616 statval = (SIGSTOP << 8) | 0177;
3619 statval = (what << 8) | 0177;
3626 statval = (SIGTRAP << 8) | 0177;
3631 statval = (SIGTRAP << 8) | 0177;
3634 #ifndef FAULTED_USE_SIGINFO
3635 /* Irix, contrary to the documentation, fills in 0 for si_signo.
3636 Solaris fills in si_signo. I'm not sure about others. */
3639 statval = (SIGILL << 8) | 0177;
3643 statval = (SIGTRAP << 8) | 0177;
3648 statval = (SIGSEGV << 8) | 0177;
3653 statval = (SIGFPE << 8) | 0177;
3655 case FLTPAGE: /* Recoverable page fault */
3656 #endif /* not FAULTED_USE_SIGINFO */
3658 /* Use the signal which the kernel assigns. This is better than
3659 trying to second-guess it from the fault. In fact, I suspect
3660 that FLTACCESS can be either SIGSEGV or SIGBUS. */
3662 ((THE_PR_LWP (pi->prstatus).pr_info.si_signo) << 8) | 0177;
3667 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
3669 /* Stop all the other threads when any of them stops. */
3672 struct procinfo *procinfo, *next_pi;
3674 for (procinfo = procinfo_list; procinfo; procinfo = next_pi)
3676 next_pi = procinfo->next;
3677 if (!procinfo->had_event)
3679 #ifdef PROCFS_USE_READ_WRITE
3681 if (write (pi->ctl_fd, (char *) &cmd, sizeof (long)) < 0)
3683 print_sys_errmsg (procinfo->pathname, errno);
3684 error ("PCSTOP failed");
3687 /* A bug in Solaris (2.5) causes us to hang when trying to
3688 stop a stopped process. So, we have to check first in
3689 order to avoid the hang. */
3690 if (!procfs_read_status (procinfo))
3692 /* The LWP has apparently terminated. */
3694 printf_filtered ("LWP %d doesn't respond.\n",
3695 (procinfo->pid >> 16) & 0xffff);
3696 close_proc_file (procinfo);
3700 if (!(procinfo->prstatus.pr_flags & PR_STOPPED))
3701 if (ioctl (procinfo->ctl_fd, PIOCSTOP, &procinfo->prstatus)
3704 print_sys_errmsg (procinfo->pathname, errno);
3705 warning ("PIOCSTOP failed");
3714 error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x",
3715 THE_PR_LWP (pi->prstatus).pr_flags);
3718 store_waitstatus (ourstatus, statval);
3720 if (rtnval == -1) /* No more children to wait for */
3722 warning ("Child process unexpectedly missing");
3723 /* Claim it exited with unknown signal. */
3724 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
3725 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
3729 pi->had_event = 0; /* Indicate that we've seen this one */
3737 set_proc_siginfo - set a process's current signal info
3741 void set_proc_siginfo (struct procinfo *pip, int signo);
3745 Given a pointer to a process info struct in PIP and a signal number
3746 in SIGNO, set the process's current signal and its associated signal
3747 information. The signal will be delivered to the process immediately
3748 after execution is resumed, even if it is being held. In addition,
3749 this particular delivery will not cause another PR_SIGNALLED stop
3750 even if the signal is being traced.
3752 If we are not delivering the same signal that the prstatus siginfo
3753 struct contains information about, then synthesize a siginfo struct
3754 to match the signal we are going to deliver, make it of the type
3755 "generated by a user process", and send this synthesized copy. When
3756 used to set the inferior's signal state, this will be required if we
3757 are not currently stopped because of a traced signal, or if we decide
3758 to continue with a different signal.
3760 Note that when continuing the inferior from a stop due to receipt
3761 of a traced signal, we either have set PRCSIG to clear the existing
3762 signal, or we have to call this function to do a PIOCSSIG with either
3763 the existing siginfo struct from pr_info, or one we have synthesized
3764 appropriately for the signal we want to deliver. Otherwise if the
3765 signal is still being traced, the inferior will immediately stop
3768 See siginfo(5) for more details.
3772 set_proc_siginfo (pip, signo)
3773 struct procinfo *pip;
3776 struct siginfo newsiginfo;
3777 struct siginfo *sip;
3778 struct sigi_ctl sictl;
3780 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
3781 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
3782 receives a PIOCSSIG with a signal identical to the current signal,
3783 it messes up the current signal. Work around the kernel bug. */
3784 if (signo == THE_PR_LWP (pip->prstatus).pr_cursig)
3789 if (signo == THE_PR_LWP (pip->prstatus).pr_info.si_signo)
3791 memcpy ((char *) &sictl.siginfo, (char *) &pip->prstatus.pr_lwp.pr_info,
3792 sizeof (siginfo_t));
3795 if (signo == THE_PR_LWP (pip->prstatus).pr_info.si_signo)
3797 sip = &pip->prstatus.pr_info;
3803 siginfo_t *sip = &sictl.siginfo;
3804 memset ((char *) sip, 0, sizeof (siginfo_t));
3806 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
3809 sip->si_signo = signo;
3812 sip->si_pid = getpid ();
3813 sip->si_uid = getuid ();
3815 #ifdef PROCFS_USE_READ_WRITE
3817 if (write (pip->ctl_fd, (char *) &sictl, sizeof (struct sigi_ctl)) < 0)
3819 if (ioctl (pip->ctl_fd, PIOCSSIG, sip) < 0)
3822 print_sys_errmsg (pip->pathname, errno);
3823 warning ("PIOCSSIG failed");
3827 /* Resume execution of process PID. If STEP is nozero, then
3828 just single step it. If SIGNAL is nonzero, restart it with that
3829 signal activated. */
3832 procfs_resume (pid, step, signo)
3835 enum target_signal signo;
3838 struct procinfo *pi, *procinfo, *next_pi;
3839 struct proc_ctl pctl;
3841 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
3846 pctl.data = PRCFAULT;
3848 pi->prrun.pr_flags = PRSTRACE | PRSFAULT | PRCFAULT;
3852 /* It should not be necessary. If the user explicitly changes the value,
3853 value_assign calls write_register_bytes, which writes it. */
3854 /* It may not be absolutely necessary to specify the PC value for
3855 restarting, but to be safe we use the value that gdb considers
3856 to be current. One case where this might be necessary is if the
3857 user explicitly changes the PC value that gdb considers to be
3858 current. FIXME: Investigate if this is necessary or not. */
3860 #ifdef PRSVADDR_BROKEN
3861 /* Can't do this under Solaris running on a Sparc, as there seems to be no
3862 place to put nPC. In fact, if you use this, nPC seems to be set to some
3863 random garbage. We have to rely on the fact that PC and nPC have been
3864 written previously via PIOCSREG during a register flush. */
3866 pi->prrun.pr_vaddr = (caddr_t) * (int *) ®isters[REGISTER_BYTE (PC_REGNUM)];
3867 pi->prrun.pr_flags != PRSVADDR;
3871 if (signo == TARGET_SIGNAL_STOP && pi->nopass_next_sigstop)
3872 /* When attaching to a child process, if we forced it to stop with
3873 a PIOCSTOP, then we will have set the nopass_next_sigstop flag.
3874 Upon resuming the first time after such a stop, we explicitly
3875 inhibit sending it another SIGSTOP, which would be the normal
3876 result of default signal handling. One potential drawback to
3877 this is that we will also ignore any attempt to by the user
3878 to explicitly continue after the attach with a SIGSTOP. Ultimately
3879 this problem should be dealt with by making the routines that
3880 deal with the inferior a little smarter, and possibly even allow
3881 an inferior to continue running at the same time as gdb. (FIXME?) */
3883 else if (signo == TARGET_SIGNAL_TSTP
3884 && THE_PR_LWP (pi->prstatus).pr_cursig == SIGTSTP
3885 && THE_PR_LWP (pi->prstatus).pr_action.sa_handler == SIG_DFL
3888 /* We are about to pass the inferior a SIGTSTP whose action is
3889 SIG_DFL. The SIG_DFL action for a SIGTSTP is to stop
3890 (notifying the parent via wait()), and then keep going from the
3891 same place when the parent is ready for you to keep going. So
3892 under the debugger, it should do nothing (as if the program had
3893 been stopped and then later resumed. Under ptrace, this
3894 happens for us, but under /proc, the system obligingly stops
3895 the process, and wait_for_inferior would have no way of
3896 distinguishing that type of stop (which indicates that we
3897 should just start it again), with a stop due to the pr_trace
3898 field of the prrun_t struct.
3900 Note that if the SIGTSTP is being caught, we *do* need to pass it,
3901 because the handler needs to get executed. */
3904 signal_to_pass = target_signal_to_host (signo);
3908 set_proc_siginfo (pi, signal_to_pass);
3913 pctl.data |= PRCSIG;
3915 pi->prrun.pr_flags |= PRCSIG;
3918 pi->nopass_next_sigstop = 0;
3922 pctl.data |= PRSTEP;
3924 pi->prrun.pr_flags |= PRSTEP;
3928 /* Don't try to start a process unless it's stopped on an
3929 `event of interest'. Doing so will cause errors. */
3931 if (!procfs_read_status (pi))
3933 /* The LWP has apparently terminated. */
3935 printf_filtered ("LWP %d doesn't respond.\n",
3936 (pi->pid >> 16) & 0xffff);
3937 close_proc_file (pi);
3941 #ifdef PROCFS_USE_READ_WRITE
3942 if (write (pi->ctl_fd, (char *) &pctl, sizeof (struct proc_ctl)) < 0)
3944 if ((pi->prstatus.pr_flags & PR_ISTOP)
3945 && ioctl (pi->ctl_fd, PIOCRUN, &pi->prrun) != 0)
3948 /* The LWP has apparently terminated. */
3950 printf_filtered ("LWP %d doesn't respond.\n",
3951 (pi->pid >> 16) & 0xffff);
3952 close_proc_file (pi);
3956 /* Continue all the other threads that haven't had an event of interest.
3957 Also continue them if they have NOPASS_NEXT_SIGSTOP set; this is only
3958 set by do_attach, and means this is the first resume after an attach.
3959 All threads were CSTOP'd by do_attach, and should be resumed now. */
3962 for (procinfo = procinfo_list; procinfo; procinfo = next_pi)
3964 next_pi = procinfo->next;
3966 if (!procinfo->had_event ||
3967 (procinfo->nopass_next_sigstop && signo == TARGET_SIGNAL_STOP))
3969 procinfo->had_event = procinfo->nopass_next_sigstop = 0;
3970 #ifdef PROCFS_USE_READ_WRITE
3971 pctl.data = PRCFAULT | PRCSIG;
3972 if (write (procinfo->ctl_fd, (char *) &pctl,
3973 sizeof (struct proc_ctl)) < 0)
3975 if (!procfs_read_status (procinfo))
3976 fprintf_unfiltered (gdb_stderr,
3977 "procfs_read_status failed, errno=%d\n",
3979 print_sys_errmsg (procinfo->pathname, errno);
3980 error ("PCRUN failed");
3983 procinfo->prrun.pr_flags &= PRSTEP;
3984 procinfo->prrun.pr_flags |= PRCFAULT | PRCSIG;
3985 if (!procfs_read_status (procinfo))
3987 /* The LWP has apparently terminated. */
3989 printf_filtered ("LWP %d doesn't respond.\n",
3990 (procinfo->pid >> 16) & 0xffff);
3991 close_proc_file (procinfo);
3995 /* Don't try to start a process unless it's stopped on an
3996 `event of interest'. Doing so will cause errors. */
3998 if ((procinfo->prstatus.pr_flags & PR_ISTOP)
3999 && ioctl (procinfo->ctl_fd, PIOCRUN, &procinfo->prrun) < 0)
4001 if (!procfs_read_status (procinfo))
4002 fprintf_unfiltered (gdb_stderr,
4003 "procfs_read_status failed, errno=%d\n",
4005 print_sys_errmsg (procinfo->pathname, errno);
4006 warning ("PIOCRUN failed");
4010 procfs_read_status (procinfo);
4018 procfs_fetch_registers -- fetch current registers from inferior
4022 void procfs_fetch_registers (int regno)
4026 Read the current values of the inferior's registers, both the
4027 general register set and floating point registers (if supported)
4028 and update gdb's idea of their current values.
4033 procfs_fetch_registers (regno)
4036 struct procinfo *pi;
4038 pi = current_procinfo;
4041 if (procfs_read_status (pi))
4043 supply_gregset (&pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs);
4044 #if defined (FP0_REGNUM)
4045 supply_fpregset (&pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs);
4048 #else /* UNIXWARE */
4049 if (ioctl (pi->ctl_fd, PIOCGREG, &pi->gregset.gregset) != -1)
4051 supply_gregset (&pi->gregset.gregset);
4053 #if defined (FP0_REGNUM)
4054 if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset.fpregset) != -1)
4056 supply_fpregset (&pi->fpregset.fpregset);
4059 #endif /* UNIXWARE */
4066 proc_init_failed - called when /proc access initialization fails
4071 static void proc_init_failed (struct procinfo *pi,
4072 char *why, int kill_p)
4076 This function is called whenever initialization of access to a /proc
4077 entry fails. It prints a suitable error message, does some cleanup,
4078 and then invokes the standard error processing routine which dumps
4079 us back into the command loop. If KILL_P is true, sends SIGKILL.
4083 proc_init_failed (pi, why, kill_p)
4084 struct procinfo *pi;
4088 print_sys_errmsg (pi->pathname, errno);
4090 kill (pi->pid, SIGKILL);
4091 close_proc_file (pi);
4100 close_proc_file - close any currently open /proc entry
4104 static void close_proc_file (struct procinfo *pip)
4108 Close any currently open /proc entry and mark the process information
4109 entry as invalid. In order to ensure that we don't try to reuse any
4110 stale information, the pid, fd, and pathnames are explicitly
4111 invalidated, which may be overkill.
4116 close_proc_file (pip)
4117 struct procinfo *pip;
4119 struct procinfo *procinfo;
4121 delete_thread (pip->pid); /* remove thread from GDB's thread list */
4122 remove_fd (pip); /* Remove fd from poll/select list */
4124 close (pip->ctl_fd);
4125 #ifdef HAVE_MULTIPLE_PROC_FDS
4127 close (pip->status_fd);
4128 close (pip->map_fd);
4131 free (pip->pathname);
4133 /* Unlink pip from the procinfo chain. Note pip might not be on the list. */
4135 if (procinfo_list == pip)
4136 procinfo_list = pip->next;
4139 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
4141 if (procinfo->next == pip)
4143 procinfo->next = pip->next;
4152 close_proc_file_cleanup (pip)
4155 close_proc_file ((struct procinfo *) pip);
4158 static struct cleanup *
4159 make_cleanup_close_proc_file (pip)
4160 struct procinfo *pip;
4162 return make_cleanup (close_proc_file_cleanup, pip);
4169 open_proc_file - open a /proc entry for a given process id
4173 static int open_proc_file (int pid, struct procinfo *pip, int mode)
4177 Given a process id and a mode, close the existing open /proc
4178 entry (if any) and open one for the new process id, in the
4179 specified mode. Once it is open, then mark the local process
4180 information structure as valid, which guarantees that the pid,
4181 fd, and pathname fields match an open /proc entry. Returns
4182 zero if the open fails, nonzero otherwise.
4184 Note that the pathname is left intact, even when the open fails,
4185 so that callers can use it to construct meaningful error messages
4186 rather than just "file open failed".
4188 Note that for Solaris, the process-id also includes an LWP-id, so we
4189 actually attempt to open that. If we are handed a pid with a 0 LWP-id,
4190 then we will ask the kernel what it is and add it to the pid. Hence,
4191 the pid can be changed by us.
4195 open_proc_file (pid, pip, mode, control)
4197 struct procinfo *pip;
4205 pip->pathname = xmalloc (MAX_PROC_NAME_SIZE);
4214 #ifdef HAVE_MULTIPLE_PROC_FDS
4215 sprintf (pip->pathname, STATUS_PROC_NAME_FMT, tmp);
4216 if ((pip->status_fd = open (pip->pathname, O_RDONLY)) < 0)
4221 sprintf (pip->pathname, AS_PROC_NAME_FMT, tmp);
4222 if ((pip->as_fd = open (pip->pathname, O_RDWR)) < 0)
4224 close (pip->status_fd);
4228 sprintf (pip->pathname, MAP_PROC_NAME_FMT, tmp);
4229 if ((pip->map_fd = open (pip->pathname, O_RDONLY)) < 0)
4231 close (pip->status_fd);
4238 sprintf (pip->pathname, CTL_PROC_NAME_FMT, tmp);
4239 if ((pip->ctl_fd = open (pip->pathname, O_WRONLY)) < 0)
4241 close (pip->status_fd);
4243 close (pip->map_fd);
4248 #else /* HAVE_MULTIPLE_PROC_FDS */
4249 sprintf (pip->pathname, CTL_PROC_NAME_FMT, tmp);
4251 if ((tmpfd = open (pip->pathname, mode)) < 0)
4255 pip->ctl_fd = tmpfd;
4257 pip->map_fd = tmpfd;
4258 pip->status_fd = tmpfd;
4260 tmp = (pid >> 16) & 0xffff; /* Extract thread id */
4263 { /* Don't know thread id yet */
4264 if (ioctl (tmpfd, PIOCSTATUS, &pip->prstatus) < 0)
4266 print_sys_errmsg (pip->pathname, errno);
4268 error ("open_proc_file: PIOCSTATUS failed");
4271 tmp = pip->prstatus.pr_who; /* Get thread id from prstatus_t */
4272 pip->pid = (tmp << 16) | pid; /* Update pip */
4275 if ((pip->ctl_fd = ioctl (tmpfd, PIOCOPENLWP, &tmp)) < 0)
4281 #ifdef PIOCSET /* New method */
4284 pr_flags = PR_ASYNC;
4285 ioctl (pip->ctl_fd, PIOCSET, &pr_flags);
4289 /* keep extra fds in sync */
4290 pip->as_fd = pip->ctl_fd;
4291 pip->map_fd = pip->ctl_fd;
4292 pip->status_fd = pip->ctl_fd;
4294 close (tmpfd); /* All done with main pid */
4295 #endif /* PIOCOPENLWP */
4297 #endif /* HAVE_MULTIPLE_PROC_FDS */
4303 mappingflags (flags)
4306 static char asciiflags[8];
4308 strcpy (asciiflags, "-------");
4309 #if defined (MA_PHYS)
4310 if (flags & MA_PHYS)
4311 asciiflags[0] = 'd';
4313 if (flags & MA_STACK)
4314 asciiflags[1] = 's';
4315 if (flags & MA_BREAK)
4316 asciiflags[2] = 'b';
4317 if (flags & MA_SHARED)
4318 asciiflags[3] = 's';
4319 if (flags & MA_READ)
4320 asciiflags[4] = 'r';
4321 if (flags & MA_WRITE)
4322 asciiflags[5] = 'w';
4323 if (flags & MA_EXEC)
4324 asciiflags[6] = 'x';
4325 return (asciiflags);
4329 info_proc_flags (pip, summary)
4330 struct procinfo *pip;
4333 struct trans *transp;
4335 long flags = pip->prstatus.pr_flags | pip->prstatus.pr_lwp.pr_flags;
4337 long flags = pip->prstatus.pr_flags;
4340 printf_filtered ("%-32s", "Process status flags:");
4343 printf_filtered ("\n\n");
4345 for (transp = pr_flag_table; transp->name != NULL; transp++)
4347 if (flags & transp->value)
4351 printf_filtered ("%s ", transp->name);
4355 printf_filtered ("\t%-16s %s.\n", transp->name, transp->desc);
4359 printf_filtered ("\n");
4363 info_proc_stop (pip, summary)
4364 struct procinfo *pip;
4367 struct trans *transp;
4371 why = THE_PR_LWP (pip->prstatus).pr_why;
4372 what = THE_PR_LWP (pip->prstatus).pr_what;
4374 if (THE_PR_LWP (pip->prstatus).pr_flags & PR_STOPPED)
4376 printf_filtered ("%-32s", "Reason for stopping:");
4379 printf_filtered ("\n\n");
4381 for (transp = pr_why_table; transp->name != NULL; transp++)
4383 if (why == transp->value)
4387 printf_filtered ("%s ", transp->name);
4391 printf_filtered ("\t%-16s %s.\n",
4392 transp->name, transp->desc);
4398 /* Use the pr_why field to determine what the pr_what field means, and
4399 print more information. */
4404 /* pr_what is unused for this case */
4410 printf_filtered ("%s ", signalname (what));
4414 printf_filtered ("\t%-16s %s.\n", signalname (what),
4415 safe_strsignal (what));
4421 printf_filtered ("%s ", syscallname (what));
4425 printf_filtered ("\t%-16s %s.\n", syscallname (what),
4426 "Entered this system call");
4432 printf_filtered ("%s ", syscallname (what));
4436 printf_filtered ("\t%-16s %s.\n", syscallname (what),
4437 "Returned from this system call");
4443 printf_filtered ("%s ",
4444 lookupname (faults_table, what, "fault"));
4448 printf_filtered ("\t%-16s %s.\n",
4449 lookupname (faults_table, what, "fault"),
4450 lookupdesc (faults_table, what));
4454 printf_filtered ("\n");
4459 info_proc_siginfo (pip, summary)
4460 struct procinfo *pip;
4463 struct siginfo *sip;
4465 if ((THE_PR_LWP (pip->prstatus).pr_flags & PR_STOPPED) &&
4466 (THE_PR_LWP (pip->prstatus).pr_why == PR_SIGNALLED ||
4467 THE_PR_LWP (pip->prstatus).pr_why == PR_FAULTED))
4469 printf_filtered ("%-32s", "Additional signal/fault info:");
4470 sip = &(THE_PR_LWP (pip->prstatus).pr_info);
4473 printf_filtered ("%s ", signalname (sip->si_signo));
4474 if (sip->si_errno > 0)
4476 printf_filtered ("%s ", errnoname (sip->si_errno));
4478 if (sip->si_code <= 0)
4480 printf_filtered ("sent by %s, uid %d ",
4481 target_pid_to_str (sip->si_pid),
4486 printf_filtered ("%s ", sigcodename (sip));
4487 if ((sip->si_signo == SIGILL) ||
4488 (sip->si_signo == SIGFPE) ||
4489 (sip->si_signo == SIGSEGV) ||
4490 (sip->si_signo == SIGBUS))
4492 printf_filtered ("addr=%#lx ",
4493 (unsigned long) sip->si_addr);
4495 else if ((sip->si_signo == SIGCHLD))
4497 printf_filtered ("child %s, status %u ",
4498 target_pid_to_str (sip->si_pid),
4501 else if ((sip->si_signo == SIGPOLL))
4503 printf_filtered ("band %u ", sip->si_band);
4509 printf_filtered ("\n\n");
4510 printf_filtered ("\t%-16s %s.\n", signalname (sip->si_signo),
4511 safe_strsignal (sip->si_signo));
4512 if (sip->si_errno > 0)
4514 printf_filtered ("\t%-16s %s.\n",
4515 errnoname (sip->si_errno),
4516 safe_strerror (sip->si_errno));
4518 if (sip->si_code <= 0)
4520 printf_filtered ("\t%-16u %s\n", sip->si_pid, /* XXX need target_pid_to_str() */
4521 "PID of process sending signal");
4522 printf_filtered ("\t%-16u %s\n", sip->si_uid,
4523 "UID of process sending signal");
4527 printf_filtered ("\t%-16s %s.\n", sigcodename (sip),
4529 if ((sip->si_signo == SIGILL) ||
4530 (sip->si_signo == SIGFPE))
4532 printf_filtered ("\t%#-16lx %s.\n",
4533 (unsigned long) sip->si_addr,
4534 "Address of faulting instruction");
4536 else if ((sip->si_signo == SIGSEGV) ||
4537 (sip->si_signo == SIGBUS))
4539 printf_filtered ("\t%#-16lx %s.\n",
4540 (unsigned long) sip->si_addr,
4541 "Address of faulting memory reference");
4543 else if ((sip->si_signo == SIGCHLD))
4545 printf_filtered ("\t%-16u %s.\n", sip->si_pid, /* XXX need target_pid_to_str() */
4546 "Child process ID");
4547 printf_filtered ("\t%-16u %s.\n", sip->si_status,
4548 "Child process exit value or signal");
4550 else if ((sip->si_signo == SIGPOLL))
4552 printf_filtered ("\t%-16u %s.\n", sip->si_band,
4553 "Band event for POLL_{IN,OUT,MSG}");
4557 printf_filtered ("\n");
4562 info_proc_syscalls (pip, summary)
4563 struct procinfo *pip;
4571 #if 0 /* FIXME: Needs to use gdb-wide configured info about system calls. */
4572 if (pip->prstatus.pr_flags & PR_ASLEEP)
4574 int syscallnum = pip->prstatus.pr_reg[R_D0];
4577 printf_filtered ("%-32s", "Sleeping in system call:");
4578 printf_filtered ("%s", syscallname (syscallnum));
4582 printf_filtered ("Sleeping in system call '%s'.\n",
4583 syscallname (syscallnum));
4589 if (ioctl (pip->ctl_fd, PIOCGENTRY, &pip->entryset) < 0)
4591 print_sys_errmsg (pip->pathname, errno);
4592 error ("PIOCGENTRY failed");
4595 if (ioctl (pip->ctl_fd, PIOCGEXIT, &pip->exitset) < 0)
4597 print_sys_errmsg (pip->pathname, errno);
4598 error ("PIOCGEXIT failed");
4602 printf_filtered ("System call tracing information:\n\n");
4604 printf_filtered ("\t%-12s %-8s %-8s\n",
4608 for (syscallnum = 0; syscallnum < MAX_SYSCALLS; syscallnum++)
4611 if (syscall_table[syscallnum] != NULL)
4612 printf_filtered ("\t%-12s ", syscall_table[syscallnum]);
4614 printf_filtered ("\t%-12d ", syscallnum);
4617 printf_filtered ("%-8s ",
4618 prismember (&pip->prstatus.pr_sysentry, syscallnum)
4620 printf_filtered ("%-8s ",
4621 prismember (&pip->prstatus.pr_sysexit, syscallnum)
4624 printf_filtered ("%-8s ",
4625 prismember (&pip->entryset, syscallnum)
4627 printf_filtered ("%-8s ",
4628 prismember (&pip->exitset, syscallnum)
4631 printf_filtered ("\n");
4633 printf_filtered ("\n");
4642 static char locbuf[32];
4644 name = strsigno (signo);
4647 sprintf (locbuf, "Signal %d", signo);
4651 sprintf (locbuf, "%s (%d)", name, signo);
4661 static char locbuf[32];
4663 name = strerrno (errnum);
4666 sprintf (locbuf, "Errno %d", errnum);
4670 sprintf (locbuf, "%s (%d)", name, errnum);
4676 info_proc_signals (pip, summary)
4677 struct procinfo *pip;
4684 #ifndef PROCFS_USE_READ_WRITE
4685 if (ioctl (pip->ctl_fd, PIOCGTRACE, &pip->trace) < 0)
4687 print_sys_errmsg (pip->pathname, errno);
4688 error ("PIOCGTRACE failed");
4692 printf_filtered ("Disposition of signals:\n\n");
4693 printf_filtered ("\t%-15s %-8s %-8s %-8s %s\n\n",
4694 "Signal", "Trace", "Hold", "Pending", "Description");
4695 for (signo = 0; signo < NSIG; signo++)
4698 printf_filtered ("\t%-15s ", signalname (signo));
4700 printf_filtered ("%-8s ",
4701 prismember (&pip->prstatus.pr_sigtrace, signo)
4703 printf_filtered ("%-8s ",
4704 prismember (&pip->prstatus.pr_lwp.pr_context.uc_sigmask, signo)
4707 printf_filtered ("%-8s ",
4708 prismember (&pip->trace, signo)
4710 printf_filtered ("%-8s ",
4711 prismember (&pip->prstatus.pr_sighold, signo)
4716 if (prismember (&pip->prstatus.pr_sigpend, signo) ||
4717 prismember (&pip->prstatus.pr_lwp.pr_lwppend, signo))
4718 printf_filtered ("%-8s ", "yes");
4720 printf_filtered ("%-8s ", "no");
4721 #else /* UNIXWARE */
4722 #ifdef PROCFS_SIGPEND_OFFSET
4723 /* Alpha OSF/1 numbers the pending signals from 1. */
4724 printf_filtered ("%-8s ",
4725 (signo ? prismember (&pip->prstatus.pr_sigpend,
4730 printf_filtered ("%-8s ",
4731 prismember (&pip->prstatus.pr_sigpend, signo)
4734 #endif /* UNIXWARE */
4735 printf_filtered (" %s\n", safe_strsignal (signo));
4737 printf_filtered ("\n");
4742 info_proc_faults (pip, summary)
4743 struct procinfo *pip;
4746 struct trans *transp;
4751 if (ioctl (pip->ctl_fd, PIOCGFAULT, &pip->fltset.fltset) < 0)
4753 print_sys_errmsg (pip->pathname, errno);
4754 error ("PIOCGFAULT failed");
4758 printf_filtered ("Current traced hardware fault set:\n\n");
4759 printf_filtered ("\t%-12s %-8s\n", "Fault", "Trace");
4761 for (transp = faults_table; transp->name != NULL; transp++)
4764 printf_filtered ("\t%-12s ", transp->name);
4766 printf_filtered ("%-8s", prismember (&pip->prstatus.pr_flttrace, transp->value)
4769 printf_filtered ("%-8s", prismember (&pip->fltset.fltset, transp->value)
4772 printf_filtered ("\n");
4774 printf_filtered ("\n");
4779 info_proc_mappings (pip, summary)
4780 struct procinfo *pip;
4784 struct prmap *prmaps;
4785 struct prmap *prmap;
4790 printf_filtered ("Mapped address spaces:\n\n");
4791 #ifdef BFD_HOST_64_BIT
4792 printf_filtered (" %18s %18s %10s %10s %7s\n",
4794 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
4801 #ifdef PROCFS_USE_READ_WRITE
4802 if (fstat (pip->map_fd, &sbuf) == 0)
4804 nmap = sbuf.st_size / sizeof (prmap_t);
4805 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
4806 if ((lseek (pip->map_fd, 0, SEEK_SET) == 0) &&
4807 (read (pip->map_fd, (char *) prmaps,
4808 nmap * sizeof (*prmaps)) == (nmap * sizeof (*prmaps))))
4811 for (prmap = prmaps; i < nmap; ++prmap, ++i)
4813 if (ioctl (pip->ctl_fd, PIOCNMAP, &nmap) == 0)
4815 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
4816 if (ioctl (pip->ctl_fd, PIOCMAP, prmaps) == 0)
4818 for (prmap = prmaps; prmap->pr_size; ++prmap)
4819 #endif /* PROCFS_USE_READ_WRITE */
4821 #ifdef BFD_HOST_64_BIT
4822 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
4824 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
4826 (unsigned long) prmap->pr_vaddr,
4827 (unsigned long) prmap->pr_vaddr
4828 + prmap->pr_size - 1,
4831 mappingflags (prmap->pr_mflags));
4835 printf_filtered ("\n");
4843 info_proc -- implement the "info proc" command
4847 void info_proc (char *args, int from_tty)
4851 Implement gdb's "info proc" command by using the /proc interface
4852 to print status information about any currently running process.
4854 Examples of the use of "info proc" are:
4856 info proc (prints summary info for current inferior)
4857 info proc 123 (prints summary info for process with pid 123)
4858 info proc mappings (prints address mappings)
4859 info proc times (prints process/children times)
4860 info proc id (prints pid, ppid, gid, sid, etc)
4861 FIXME: i proc id not implemented.
4862 info proc status (prints general process state info)
4863 FIXME: i proc status not implemented.
4864 info proc signals (prints info about signal handling)
4865 info proc all (prints all info)
4870 info_proc (args, from_tty)
4875 struct procinfo *pip;
4876 struct cleanup *old_chain;
4892 old_chain = make_cleanup (null_cleanup, 0);
4894 /* Default to using the current inferior if no pid specified. Note
4895 that inferior_pid may be 0, hence we set okerr. */
4897 pid = inferior_pid & 0x7fffffff; /* strip off sol-thread bit */
4898 if (!(pip = find_procinfo (pid, 1))) /* inferior_pid no good? */
4899 pip = procinfo_list; /* take first available */
4900 pid = pid & 0xffff; /* extract "real" pid */
4904 if ((argv = buildargv (args)) == NULL)
4908 make_cleanup_freeargv (argv);
4910 while (*argv != NULL)
4912 argsize = strlen (*argv);
4913 if (argsize >= 1 && strncmp (*argv, "all", argsize) == 0)
4918 else if (argsize >= 2 && strncmp (*argv, "faults", argsize) == 0)
4923 else if (argsize >= 2 && strncmp (*argv, "flags", argsize) == 0)
4928 else if (argsize >= 1 && strncmp (*argv, "id", argsize) == 0)
4933 else if (argsize >= 1 && strncmp (*argv, "mappings", argsize) == 0)
4938 else if (argsize >= 2 && strncmp (*argv, "signals", argsize) == 0)
4943 else if (argsize >= 2 && strncmp (*argv, "status", argsize) == 0)
4948 else if (argsize >= 2 && strncmp (*argv, "syscalls", argsize) == 0)
4953 else if (argsize >= 1 && strncmp (*argv, "times", argsize) == 0)
4958 else if ((pid = atoi (*argv)) > 0)
4960 pip = (struct procinfo *) xmalloc (sizeof (struct procinfo));
4961 memset (pip, 0, sizeof (*pip));
4964 if (!open_proc_file (pid, pip, O_RDONLY, 0))
4966 perror_with_name (pip->pathname);
4970 make_cleanup_close_proc_file (pip);
4972 else if (**argv != '\000')
4974 error ("Unrecognized or ambiguous keyword `%s'.", *argv);
4980 /* If we don't have a valid open process at this point, then we have no
4981 inferior or didn't specify a specific pid. */
4986 No process. Start debugging a program or specify an explicit process ID.");
4989 if (!procfs_read_status (pip))
4991 print_sys_errmsg (pip->pathname, errno);
4992 error ("procfs_read_status failed");
4995 #ifndef PROCFS_USE_READ_WRITE
4997 nlwp = pip->prstatus.pr_nlwp;
4998 lwps = alloca ((2 * nlwp + 2) * sizeof (*lwps));
5000 if (ioctl (pip->ctl_fd, PIOCLWPIDS, lwps))
5002 print_sys_errmsg (pip->pathname, errno);
5003 error ("PIOCLWPIDS failed");
5005 #else /* PIOCLWPIDS */
5007 lwps = alloca ((2 * nlwp + 2) * sizeof *lwps);
5009 #endif /* PIOCLWPIDS */
5011 for (; nlwp > 0; nlwp--, lwps++)
5013 pip = find_procinfo ((*lwps << 16) | pid, 1);
5017 pip = (struct procinfo *) xmalloc (sizeof (struct procinfo));
5018 memset (pip, 0, sizeof (*pip));
5019 if (!open_proc_file ((*lwps << 16) | pid, pip, O_RDONLY, 0))
5022 make_cleanup_close_proc_file (pip);
5024 if (!procfs_read_status (pip))
5026 print_sys_errmsg (pip->pathname, errno);
5027 error ("procfs_read_status failed");
5031 #endif /* PROCFS_USE_READ_WRITE */
5033 /* Print verbose information of the requested type(s), or just a summary
5034 of the information for all types. */
5036 printf_filtered ("\nInformation for %s.%d:\n\n", pip->pathname, *lwps);
5037 if (summary || all || flags)
5039 info_proc_flags (pip, summary);
5043 info_proc_stop (pip, summary);
5045 supply_gregset (&pip->prstatus.pr_lwp.pr_context.uc_mcontext.gregs);
5047 supply_gregset (&pip->prstatus.pr_reg);
5049 printf_filtered ("PC: ");
5050 print_address (read_pc (), gdb_stdout);
5051 printf_filtered ("\n");
5053 if (summary || all || signals || faults)
5055 info_proc_siginfo (pip, summary);
5057 if (summary || all || syscalls)
5059 info_proc_syscalls (pip, summary);
5061 if (summary || all || mappings)
5063 info_proc_mappings (pip, summary);
5065 if (summary || all || signals)
5067 info_proc_signals (pip, summary);
5069 if (summary || all || faults)
5071 info_proc_faults (pip, summary);
5073 printf_filtered ("\n");
5075 /* All done, deal with closing any temporary process info structure,
5076 freeing temporary memory , etc. */
5078 do_cleanups (old_chain);
5079 #ifndef PROCFS_USE_READ_WRITE
5088 modify_inherit_on_fork_flag - Change the inherit-on-fork flag
5092 void modify_inherit_on_fork_flag (fd, flag)
5096 Call this routine to modify the inherit-on-fork flag. This routine is
5097 just a nice wrapper to hide the #ifdefs needed by various systems to
5103 modify_inherit_on_fork_flag (fd, flag)
5107 #if defined (PIOCSET) || defined (PCSET)
5111 struct proc_ctl pctl;
5113 #if defined (PIOCSET) || defined (PCSET) /* New method */
5117 #ifdef PROCFS_USE_READ_WRITE
5119 pctl.data = PR_FORK;
5120 if (write (fd, (char *) &pctl, sizeof (struct proc_ctl)) < 0)
5123 retval = ioctl (fd, PIOCSET, &pr_flags);
5128 #ifdef PROCFS_USE_READ_WRITE
5130 pctl.data = PR_FORK;
5131 if (write (fd, (char *) &pctl, sizeof (struct proc_ctl)) < 0)
5134 retval = ioctl (fd, PIOCRESET, &pr_flags);
5139 #ifdef PIOCSFORK /* Original method */
5142 retval = ioctl (fd, PIOCSFORK, NULL);
5146 retval = ioctl (fd, PIOCRFORK, NULL);
5149 Neither PR_FORK nor PIOCSFORK exist ! !!
5156 print_sys_errmsg ("modify_inherit_on_fork_flag", errno);
5157 error ("PIOCSFORK or PR_FORK modification failed");
5164 modify_run_on_last_close_flag - Change the run-on-last-close flag
5168 void modify_run_on_last_close_flag (fd, flag)
5172 Call this routine to modify the run-on-last-close flag. This routine
5173 is just a nice wrapper to hide the #ifdefs needed by various systems to
5179 modify_run_on_last_close_flag (fd, flag)
5183 #if defined (PIOCSET) || defined (PCSET)
5187 struct proc_ctl pctl;
5189 #if defined (PIOCSET) || defined (PCSET) /* New method */
5193 #ifdef PROCFS_USE_READ_WRITE
5196 if (write (fd, (char *) &pctl, sizeof (struct proc_ctl)) < 0)
5199 retval = ioctl (fd, PIOCSET, &pr_flags);
5204 #ifdef PROCFS_USE_READ_WRITE
5207 if (write (fd, (char *) &pctl, sizeof (struct proc_ctl)) < 0)
5210 retval = ioctl (fd, PIOCRESET, &pr_flags);
5215 #ifdef PIOCSRLC /* Original method */
5217 retval = ioctl (fd, PIOCSRLC, NULL);
5219 retval = ioctl (fd, PIOCRRLC, NULL);
5221 Neither PR_RLC nor PIOCSRLC exist ! !!
5228 print_sys_errmsg ("modify_run_on_last_close_flag", errno);
5229 error ("PIOCSRLC or PR_RLC modification failed");
5236 procfs_clear_syscall_trap -- Deletes the trap for the specified system call.
5240 void procfs_clear_syscall_trap (struct procinfo *, int syscall_num, int errok)
5244 This function function disables traps for the specified system call.
5245 errok is non-zero if errors should be ignored.
5249 procfs_clear_syscall_trap (pi, syscall_num, errok)
5250 struct procinfo *pi;
5258 goterr = ioctl (pi->ctl_fd, PIOCGENTRY, &sysset) < 0;
5260 if (goterr && !errok)
5262 print_sys_errmsg (pi->pathname, errno);
5263 error ("PIOCGENTRY failed");
5268 prdelset (&sysset, syscall_num);
5270 if ((ioctl (pi->ctl_fd, PIOCSENTRY, &sysset) < 0) && !errok)
5272 print_sys_errmsg (pi->pathname, errno);
5273 error ("PIOCSENTRY failed");
5277 goterr = ioctl (pi->ctl_fd, PIOCGEXIT, &sysset) < 0;
5279 if (goterr && !errok)
5281 procfs_clear_syscall_trap (pi, syscall_num, 1);
5282 print_sys_errmsg (pi->pathname, errno);
5283 error ("PIOCGEXIT failed");
5288 praddset (&sysset, syscall_num);
5290 if ((ioctl (pi->ctl_fd, PIOCSEXIT, &sysset) < 0) && !errok)
5292 procfs_clear_syscall_trap (pi, syscall_num, 1);
5293 print_sys_errmsg (pi->pathname, errno);
5294 error ("PIOCSEXIT failed");
5299 if (!pi->syscall_handlers)
5302 error ("procfs_clear_syscall_trap: syscall_handlers is empty");
5306 /* Remove handler func from the handler list */
5308 for (i = 0; i < pi->num_syscall_handlers; i++)
5309 if (pi->syscall_handlers[i].syscall_num == syscall_num)
5311 if (i + 1 != pi->num_syscall_handlers)
5312 { /* Not the last entry.
5313 Move subsequent entries fwd. */
5314 memcpy (&pi->syscall_handlers[i], &pi->syscall_handlers[i + 1],
5315 (pi->num_syscall_handlers - i - 1)
5316 * sizeof (struct procfs_syscall_handler));
5319 pi->syscall_handlers = xrealloc (pi->syscall_handlers,
5320 (pi->num_syscall_handlers - 1)
5321 * sizeof (struct procfs_syscall_handler));
5322 pi->num_syscall_handlers--;
5327 error ("procfs_clear_syscall_trap: Couldn't find handler for sys call %d",
5335 procfs_set_syscall_trap -- arrange for a function to be called when the
5336 child executes the specified system call.
5340 void procfs_set_syscall_trap (struct procinfo *, int syscall_num, int flags,
5341 syscall_func_t *function)
5345 This function sets up an entry and/or exit trap for the specified system
5346 call. When the child executes the specified system call, your function
5347 will be called with the call #, a flag that indicates entry or exit, and
5348 pointers to rtnval and statval (which are used by procfs_wait). The
5349 function should return non-zero if something interesting happened, zero
5354 procfs_set_syscall_trap (pi, syscall_num, flags, func)
5355 struct procinfo *pi;
5358 syscall_func_t *func;
5363 if (flags & PROCFS_SYSCALL_ENTRY)
5365 if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysset) < 0)
5367 print_sys_errmsg (pi->pathname, errno);
5368 error ("PIOCGENTRY failed");
5371 praddset (&sysset, syscall_num);
5373 if (ioctl (pi->ctl_fd, PIOCSENTRY, &sysset) < 0)
5375 print_sys_errmsg (pi->pathname, errno);
5376 error ("PIOCSENTRY failed");
5380 if (flags & PROCFS_SYSCALL_EXIT)
5382 if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysset) < 0)
5384 procfs_clear_syscall_trap (pi, syscall_num, 1);
5385 print_sys_errmsg (pi->pathname, errno);
5386 error ("PIOCGEXIT failed");
5389 praddset (&sysset, syscall_num);
5391 if (ioctl (pi->ctl_fd, PIOCSEXIT, &sysset) < 0)
5393 procfs_clear_syscall_trap (pi, syscall_num, 1);
5394 print_sys_errmsg (pi->pathname, errno);
5395 error ("PIOCSEXIT failed");
5400 if (!pi->syscall_handlers)
5402 pi->syscall_handlers = xmalloc (sizeof (struct procfs_syscall_handler));
5403 pi->syscall_handlers[0].syscall_num = syscall_num;
5404 pi->syscall_handlers[0].func = func;
5405 pi->num_syscall_handlers = 1;
5411 for (i = 0; i < pi->num_syscall_handlers; i++)
5412 if (pi->syscall_handlers[i].syscall_num == syscall_num)
5414 pi->syscall_handlers[i].func = func;
5418 pi->syscall_handlers = xrealloc (pi->syscall_handlers, (i + 1)
5419 * sizeof (struct procfs_syscall_handler));
5420 pi->syscall_handlers[i].syscall_num = syscall_num;
5421 pi->syscall_handlers[i].func = func;
5422 pi->num_syscall_handlers++;
5426 #ifdef SYS_lwp_create
5432 procfs_lwp_creation_handler - handle exit from the _lwp_create syscall
5436 int procfs_lwp_creation_handler (pi, syscall_num, why, rtnvalp, statvalp)
5440 This routine is called both when an inferior process and it's new lwp
5441 are about to finish a _lwp_create() system call. This is the system
5442 call that Solaris uses to create a lightweight process. When the
5443 target process gets this event, we can look at sysarg[2] to find the
5444 new childs lwp ID, and create a procinfo struct from that. After that,
5445 we pretend that we got a SIGTRAP, and return non-zero to tell
5446 procfs_wait to wake up. Subsequently, wait_for_inferior gets woken up,
5447 sees the new process and continues it.
5449 When we see the child exiting from lwp_create, we just contine it,
5450 since everything was handled when the parent trapped.
5453 In effect, we are only paying attention to the parent's completion of
5454 the lwp_create syscall. If we only paid attention to the child
5455 instead, then we wouldn't detect the creation of a suspended thread.
5459 procfs_lwp_creation_handler (pi, syscall_num, why, rtnvalp, statvalp)
5460 struct procinfo *pi;
5467 struct procinfo *childpi;
5468 struct proc_ctl pctl;
5470 /* We've just detected the completion of an lwp_create system call. Now we
5471 need to setup a procinfo struct for this thread, and notify the thread
5472 system of the new arrival. */
5474 /* If lwp_create failed, then nothing interesting happened. Continue the
5475 process and go back to sleep. */
5478 /* Joel ... can you check this logic out please? JKJ */
5479 if (pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs[R_EFL] & 1)
5480 { /* _lwp_create failed */
5482 pctl.data = PRCFAULT;
5484 if (write (pi->ctl_fd, (char *) &pctl, sizeof (struct proc_ctl)) < 0)
5485 perror_with_name (pi->pathname);
5489 #else /* UNIXWARE */
5490 if (PROCFS_GET_CARRY (pi->prstatus.pr_reg))
5491 { /* _lwp_create failed */
5492 pi->prrun.pr_flags &= PRSTEP;
5493 pi->prrun.pr_flags |= PRCFAULT;
5495 if (ioctl (pi->ctl_fd, PIOCRUN, &pi->prrun) != 0)
5496 perror_with_name (pi->pathname);
5502 /* At this point, the new thread is stopped at it's first instruction, and
5503 the parent is stopped at the exit from lwp_create. */
5505 if (pi->new_child) /* Child? */
5506 { /* Yes, just continue it */
5509 pctl.data = PRCFAULT;
5511 if (write (pi->ctl_fd, (char *) &pctl, sizeof (struct proc_ctl)) < 0)
5512 #else /* !UNIXWARE */
5513 pi->prrun.pr_flags &= PRSTEP;
5514 pi->prrun.pr_flags |= PRCFAULT;
5516 if ((pi->prstatus.pr_flags & PR_ISTOP)
5517 && ioctl (pi->ctl_fd, PIOCRUN, &pi->prrun) != 0)
5518 #endif /* !UNIXWARE */
5519 perror_with_name (pi->pathname);
5521 pi->new_child = 0; /* No longer new */
5526 /* We're the proud parent of a new thread. Setup an exit trap for lwp_create
5527 in the child and continue the parent. */
5529 /* Third arg is pointer to new thread id. */
5530 lwp_id = read_memory_integer (
5531 THE_PR_LWP (pi->prstatus).pr_sysarg[2], sizeof (int));
5533 lwp_id = (lwp_id << 16) | PIDGET (pi->pid);
5535 childpi = create_procinfo (lwp_id);
5537 /* The new process has actually inherited the lwp_create syscall trap from
5538 it's parent, but we still have to call this to register handlers for
5541 procfs_set_inferior_syscall_traps (childpi);
5542 add_thread (lwp_id);
5543 printf_filtered ("[New %s]\n", target_pid_to_str (lwp_id));
5545 /* Continue the parent */
5548 pctl.data = PRCFAULT;
5550 if (write (pi->ctl_fd, (char *) &pctl, sizeof (struct proc_ctl)) < 0)
5552 pi->prrun.pr_flags &= PRSTEP;
5553 pi->prrun.pr_flags |= PRCFAULT;
5554 if (ioctl (pi->ctl_fd, PIOCRUN, &pi->prrun) != 0)
5556 perror_with_name (pi->pathname);
5558 /* The new child may have been created in one of two states:
5559 SUSPENDED or RUNNABLE. If runnable, we will simply signal it to run.
5560 If suspended, we flag it to be continued later, when it has an event. */
5562 if (THE_PR_LWP (childpi->prstatus).pr_why == PR_SUSPENDED)
5563 childpi->new_child = 1; /* Flag this as an unseen child process */
5566 /* Continue the child */
5569 pctl.data = PRCFAULT;
5571 if (write (pi->ctl_fd, (char *) &pctl, sizeof (struct proc_ctl)) < 0)
5573 childpi->prrun.pr_flags &= PRSTEP;
5574 childpi->prrun.pr_flags |= PRCFAULT;
5576 if (ioctl (childpi->ctl_fd, PIOCRUN, &childpi->prrun) != 0)
5578 perror_with_name (childpi->pathname);
5582 #endif /* SYS_lwp_create */
5584 /* Fork an inferior process, and start debugging it with /proc. */
5587 procfs_create_inferior (exec_file, allargs, env)
5592 char *shell_file = getenv ("SHELL");
5594 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
5597 /* We will be looking down the PATH to find shell_file. If we
5598 just do this the normal way (via execlp, which operates by
5599 attempting an exec for each element of the PATH until it
5600 finds one which succeeds), then there will be an exec for
5601 each failed attempt, each of which will cause a PR_SYSEXIT
5602 stop, and we won't know how to distinguish the PR_SYSEXIT's
5603 for these failed execs with the ones for successful execs
5604 (whether the exec has succeeded is stored at that time in the
5605 carry bit or some such architecture-specific and
5606 non-ABI-specified place).
5608 So I can't think of anything better than to search the PATH
5609 now. This has several disadvantages: (1) There is a race
5610 condition; if we find a file now and it is deleted before we
5611 exec it, we lose, even if the deletion leaves a valid file
5612 further down in the PATH, (2) there is no way to know exactly
5613 what an executable (in the sense of "capable of being
5614 exec'd") file is. Using access() loses because it may lose
5615 if the caller is the superuser; failing to use it loses if
5616 there are ACLs or some such. */
5620 /* FIXME-maybe: might want "set path" command so user can change what
5621 path is used from within GDB. */
5622 char *path = getenv ("PATH");
5624 struct stat statbuf;
5627 path = "/bin:/usr/bin";
5629 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
5630 for (p = path; p != NULL; p = p1 ? p1 + 1 : NULL)
5632 p1 = strchr (p, ':');
5637 strncpy (tryname, p, len);
5638 tryname[len] = '\0';
5639 strcat (tryname, "/");
5640 strcat (tryname, shell_file);
5641 if (access (tryname, X_OK) < 0)
5643 if (stat (tryname, &statbuf) < 0)
5645 if (!S_ISREG (statbuf.st_mode))
5646 /* We certainly need to reject directories. I'm not quite
5647 as sure about FIFOs, sockets, etc., but I kind of doubt
5648 that people want to exec() these things. */
5653 /* Not found. This must be an error rather than merely passing
5654 the file to execlp(), because execlp() would try all the
5655 exec()s, causing GDB to get confused. */
5656 error ("Can't find shell %s in PATH", shell_file);
5658 shell_file = tryname;
5661 fork_inferior (exec_file, allargs, env,
5662 proc_set_exec_trap, procfs_init_inferior, NULL, shell_file);
5664 /* We are at the first instruction we care about. */
5665 /* Pedal to the metal... */
5667 proceed ((CORE_ADDR) - 1, TARGET_SIGNAL_0, 0);
5670 /* Clean up after the inferior dies. */
5673 procfs_mourn_inferior ()
5675 struct procinfo *pi;
5676 struct procinfo *next_pi;
5678 for (pi = procinfo_list; pi; pi = next_pi)
5681 unconditionally_kill_inferior (pi);
5684 unpush_target (&procfs_ops);
5685 generic_mourn_inferior ();
5689 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
5693 /* This variable is controlled by modules that sit atop procfs that may layer
5694 their own process structure atop that provided here. sol-thread.c does
5695 this because of the Solaris two-level thread model. */
5697 return !procfs_suppress_run;
5699 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
5702 /* Insert a watchpoint */
5704 procfs_set_watchpoint (pid, addr, len, rw)
5710 struct procinfo *pi;
5713 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
5714 wpt.pr_vaddr = (caddr_t) addr;
5716 wpt.pr_wflags = ((rw & 1) ? MA_READ : 0) | ((rw & 2) ? MA_WRITE : 0);
5717 if (ioctl (pi->ctl_fd, PIOCSWATCH, &wpt) < 0)
5721 /* Currently it sometimes happens that the same watchpoint gets
5722 deleted twice - don't die in this case (FIXME please) */
5723 if (errno == ESRCH && len == 0)
5725 print_sys_errmsg (pi->pathname, errno);
5726 error ("PIOCSWATCH failed");
5732 procfs_stopped_by_watchpoint (pid)
5735 struct procinfo *pi;
5739 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
5740 if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
5742 why = pi->prstatus.pr_why;
5743 what = pi->prstatus.pr_what;
5744 if (why == PR_FAULTED
5745 #if defined (FLTWATCH) && defined (FLTKWATCH)
5746 && (what == FLTWATCH || what == FLTKWATCH)
5749 && (what == FLTWATCH)
5752 && (what == FLTKWATCH)
5760 #endif /* !UNIXWARE */
5761 #endif /* TARGET_HAS_HARDWARE_WATCHPOINTS */
5763 /* Why is this necessary? Shouldn't dead threads just be removed from the
5767 procfs_thread_alive (pid)
5770 struct procinfo *pi, *next_pi;
5772 for (pi = procinfo_list; pi; pi = next_pi)
5776 if (procfs_read_status (pi)) /* alive */
5779 /* defunct (exited) */
5781 close_proc_file (pi);
5789 procfs_first_available ()
5791 struct procinfo *pi;
5793 for (pi = procinfo_list; pi; pi = pi->next)
5795 if (procfs_read_status (pi))
5802 procfs_get_pid_fd (pid)
5805 struct procinfo *pi = find_procinfo (pid, 1);
5813 /* Send a SIGINT to the process group. This acts just like the user typed a
5814 ^C on the controlling terminal.
5816 XXX - This may not be correct for all systems. Some may want to use
5817 killpg() instead of kill (-pgrp). */
5822 extern pid_t inferior_process_group;
5824 kill (-inferior_process_group, SIGINT);
5827 /* Convert a pid to printable form. */
5831 procfs_pid_to_str (pid)
5834 static char buf[100];
5836 sprintf (buf, "Kernel thread %d", TIDGET (pid));
5846 procfs_ops.to_shortname = "procfs";
5847 procfs_ops.to_longname = "Unix /proc child process";
5848 procfs_ops.to_doc = "Unix /proc child process (started by the \"run\" command).";
5849 procfs_ops.to_open = procfs_open;
5850 procfs_ops.to_attach = procfs_attach;
5851 procfs_ops.to_detach = procfs_detach;
5852 procfs_ops.to_resume = procfs_resume;
5853 procfs_ops.to_wait = procfs_wait;
5854 procfs_ops.to_fetch_registers = procfs_fetch_registers;
5855 procfs_ops.to_store_registers = procfs_store_registers;
5856 procfs_ops.to_prepare_to_store = procfs_prepare_to_store;
5857 procfs_ops.to_xfer_memory = procfs_xfer_memory;
5858 procfs_ops.to_files_info = procfs_files_info;
5859 procfs_ops.to_insert_breakpoint = memory_insert_breakpoint;
5860 procfs_ops.to_remove_breakpoint = memory_remove_breakpoint;
5861 procfs_ops.to_terminal_init = terminal_init_inferior;
5862 procfs_ops.to_terminal_inferior = terminal_inferior;
5863 procfs_ops.to_terminal_ours_for_output = terminal_ours_for_output;
5864 procfs_ops.to_terminal_ours = terminal_ours;
5865 procfs_ops.to_terminal_info = child_terminal_info;
5866 procfs_ops.to_kill = procfs_kill_inferior;
5867 procfs_ops.to_create_inferior = procfs_create_inferior;
5868 procfs_ops.to_mourn_inferior = procfs_mourn_inferior;
5869 procfs_ops.to_can_run = procfs_can_run;
5870 procfs_ops.to_notice_signals = procfs_notice_signals;
5871 procfs_ops.to_thread_alive = procfs_thread_alive;
5872 procfs_ops.to_stop = procfs_stop;
5873 procfs_ops.to_stratum = process_stratum;
5874 procfs_ops.to_has_all_memory = 1;
5875 procfs_ops.to_has_memory = 1;
5876 procfs_ops.to_has_stack = 1;
5877 procfs_ops.to_has_registers = 1;
5878 procfs_ops.to_has_execution = 1;
5879 procfs_ops.to_magic = OPS_MAGIC;
5883 _initialize_procfs ()
5885 #ifdef HAVE_OPTIONAL_PROC_FS
5886 char procname[MAX_PROC_NAME_SIZE];
5889 /* If we have an optional /proc filesystem (e.g. under OSF/1),
5890 don't add procfs support if we cannot access the running
5892 sprintf (procname, STATUS_PROC_NAME_FMT, getpid ());
5893 if ((fd = open (procname, O_RDONLY)) < 0)
5899 add_target (&procfs_ops);
5901 add_info ("processes", info_proc,
5902 "Show process status information using /proc entry.\n\
5903 Specify process id or use current inferior by default.\n\
5904 Specify keywords for detailed information; default is summary.\n\
5905 Keywords are: `all', `faults', `flags', `id', `mappings', `signals',\n\
5906 `status', `syscalls', and `times'.\n\
5907 Unambiguous abbreviations may be used.");
5909 init_syscall_table ();