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"
56 #include "gdbthread.h"
58 #if !defined(SYS_lwp_create) && defined(SYS_lwpcreate)
59 #define SYS_lwp_create SYS_lwpcreate
62 #if !defined(SYS_lwp_exit) && defined(SYS_lwpexit)
63 #define SYS_lwp_exit SYS_lwpexit
66 #if !defined(SYS_lwp_wait) && defined(SYS_lwpwait)
67 #define SYS_lwp_wait SYS_lwpwait
70 #if !defined(SYS_lwp_self) && defined(SYS_lwpself)
71 #define SYS_lwp_self SYS_lwpself
74 #if !defined(SYS_lwp_info) && defined(SYS_lwpinfo)
75 #define SYS_lwp_info SYS_lwpinfo
78 #if !defined(SYS_lwp_private) && defined(SYS_lwpprivate)
79 #define SYS_lwp_private SYS_lwpprivate
82 #if !defined(SYS_lwp_kill) && defined(SYS_lwpkill)
83 #define SYS_lwp_kill SYS_lwpkill
86 #if !defined(SYS_lwp_suspend) && defined(SYS_lwpsuspend)
87 #define SYS_lwp_suspend SYS_lwpsuspend
90 #if !defined(SYS_lwp_continue) && defined(SYS_lwpcontinue)
91 #define SYS_lwp_continue SYS_lwpcontinue
94 /* the name of the proc status struct depends on the implementation */
95 /* Wrap Light Weight Process member in THE_PR_LWP macro for clearer code */
96 #ifndef HAVE_PSTATUS_T
97 typedef prstatus_t gdb_prstatus_t
;
98 #define THE_PR_LWP(a) a
99 #else /* HAVE_PSTATUS_T */
100 typedef pstatus_t gdb_prstatus_t
;
101 #define THE_PR_LWP(a) a.pr_lwp
102 #if !defined(HAVE_PRRUN_T) && defined(HAVE_MULTIPLE_PROC_FDS)
103 /* Fallback definitions - for using configure information directly */
107 #if !defined(PROCFS_USE_READ_WRITE) && !defined(HAVE_PROCFS_PIOCSET)
108 #define PROCFS_USE_READ_WRITE 1
110 #endif /* !HAVE_PRRUN_T && HAVE_MULTIPLE_PROC_FDS */
111 #endif /* HAVE_PSTATUS_T */
113 #define MAX_SYSCALLS 256 /* Maximum number of syscalls for table */
115 /* proc name formats may vary depending on the proc implementation */
116 #ifdef HAVE_MULTIPLE_PROC_FDS
117 #ifndef CTL_PROC_NAME_FMT
118 #define CTL_PROC_NAME_FMT "/proc/%d/ctl"
119 #define AS_PROC_NAME_FMT "/proc/%d/as"
120 #define MAP_PROC_NAME_FMT "/proc/%d/map"
121 #define STATUS_PROC_NAME_FMT "/proc/%d/status"
123 #else /* HAVE_MULTIPLE_PROC_FDS */
124 #ifndef CTL_PROC_NAME_FMT
125 #define CTL_PROC_NAME_FMT "/proc/%05d"
126 #define AS_PROC_NAME_FMT "/proc/%05d"
127 #define MAP_PROC_NAME_FMT "/proc/%05d"
128 #define STATUS_PROC_NAME_FMT "/proc/%05d"
130 #endif /* HAVE_MULTIPLE_PROC_FDS */
133 /* These #ifdefs are for sol2.x in particular. sol2.x has
134 both a "gregset_t" and a "prgregset_t", which have
135 similar uses but different layouts. sol2.x gdb tries to
136 use prgregset_t (and prfpregset_t) everywhere. */
138 #ifdef GDB_GREGSET_TYPE
139 typedef GDB_GREGSET_TYPE gdb_gregset_t
;
141 typedef gregset_t gdb_gregset_t
;
144 #ifdef GDB_FPREGSET_TYPE
145 typedef GDB_FPREGSET_TYPE gdb_fpregset_t
;
147 typedef fpregset_t gdb_fpregset_t
;
151 #define MAX_PROC_NAME_SIZE sizeof("/proc/1234567890/status")
153 struct target_ops procfs_ops
;
155 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
156 be a runnable target. Used by targets
157 that can sit atop procfs, such as solaris
160 #if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */
161 CORE_ADDR kernel_u_addr
;
164 #ifdef BROKEN_SIGINFO_H /* Workaround broken SGS <sys/siginfo.h> */
166 #define si_pid _data._proc.pid
168 #define si_uid _data._proc._pdata._kill.uid
169 #endif /* BROKEN_SIGINFO_H */
171 /* Define structures for passing commands to /proc/pid/ctl file. Note that
172 while we create these for the PROCFS_USE_READ_WRITE world, we use them
173 and ignore the extra cmd int in other proc schemes.
175 /* generic ctl msg */
182 /* set general registers */
186 gdb_gregset_t gregset
;
189 /* set fp registers */
193 gdb_fpregset_t fpregset
;
196 /* set signals to be traced */
203 /* set faults to be traced */
210 /* set system calls to be traced */
217 /* set current signal to be traced */
224 /* All access to the inferior, either one started by gdb or one that has
225 been attached to, is controlled by an instance of a procinfo structure,
226 defined below. Since gdb currently only handles one inferior at a time,
227 the procinfo structure for the inferior is statically allocated and
228 only one exists at any given time. There is a separate procinfo
229 structure for use by the "info proc" command, so that we can print
230 useful information about any random process without interfering with
231 the inferior's procinfo information. */
235 struct procinfo
*next
;
236 int pid
; /* Process ID of inferior */
237 int ctl_fd
; /* File descriptor for /proc ctl file */
238 int status_fd
; /* File descriptor for /proc status file */
239 int as_fd
; /* File descriptor for /proc as file */
240 int map_fd
; /* File descriptor for /proc map file */
241 char *pathname
; /* Pathname to /proc entry */
242 int had_event
; /* poll/select says something happened */
243 int was_stopped
; /* Nonzero if was stopped prior to attach */
244 int nopass_next_sigstop
; /* Don't pass a sigstop on next resume */
246 prrun_t prrun
; /* Control state when it is run */
248 gdb_prstatus_t prstatus
; /* Current process status info */
249 struct greg_ctl gregset
; /* General register set */
250 struct fpreg_ctl fpregset
; /* Floating point register set */
251 struct flt_ctl fltset
; /* Current traced hardware fault set */
252 struct sig_ctl trace
; /* Current traced signal set */
253 struct sys_ctl exitset
; /* Current traced system call exit set */
254 struct sys_ctl entryset
; /* Current traced system call entry set */
255 struct sig_ctl saved_sighold
; /* Saved held signal set */
256 struct flt_ctl saved_fltset
; /* Saved traced hardware fault set */
257 struct sig_ctl saved_trace
; /* Saved traced signal set */
258 struct sys_ctl saved_exitset
; /* Saved traced system call exit set */
259 struct sys_ctl saved_entryset
; /* Saved traced system call entry set */
260 int num_syscall_handlers
; /* Number of syscall trap handlers
261 currently installed */
262 /* Pointer to list of syscall trap handlers */
263 struct procfs_syscall_handler
*syscall_handlers
;
264 int saved_rtnval
; /* return value and status for wait(), */
265 int saved_statval
; /* as supplied by a syscall handler. */
266 int new_child
; /* Non-zero if it's a new thread */
269 /* List of inferior process information */
270 static struct procinfo
*procinfo_list
= NULL
;
271 static struct pollfd
*poll_list
; /* pollfds used for waiting on /proc */
273 static int num_poll_list
= 0; /* Number of entries in poll_list */
275 /* Much of the information used in the /proc interface, particularly for
276 printing status information, is kept as tables of structures of the
277 following form. These tables can be used to map numeric values to
278 their symbolic names and to a string that describes their specific use. */
282 int value
; /* The numeric value */
283 char *name
; /* The equivalent symbolic value */
284 char *desc
; /* Short description of value */
287 /* Translate bits in the pr_flags member of the prstatus structure, into the
288 names and desc information. */
290 static struct trans pr_flag_table
[] =
292 #if defined (PR_STOPPED)
293 {PR_STOPPED
, "PR_STOPPED", "Process is stopped"},
295 #if defined (PR_ISTOP)
296 {PR_ISTOP
, "PR_ISTOP", "Stopped on an event of interest"},
298 #if defined (PR_DSTOP)
299 {PR_DSTOP
, "PR_DSTOP", "A stop directive is in effect"},
301 #if defined (PR_ASLEEP)
302 {PR_ASLEEP
, "PR_ASLEEP", "Sleeping in an interruptible system call"},
304 #if defined (PR_FORK)
305 {PR_FORK
, "PR_FORK", "Inherit-on-fork is in effect"},
308 {PR_RLC
, "PR_RLC", "Run-on-last-close is in effect"},
310 #if defined (PR_PTRACE)
311 {PR_PTRACE
, "PR_PTRACE", "Process is being controlled by ptrace"},
313 #if defined (PR_PCINVAL)
314 {PR_PCINVAL
, "PR_PCINVAL", "PC refers to an invalid virtual address"},
316 #if defined (PR_ISSYS)
317 {PR_ISSYS
, "PR_ISSYS", "Is a system process"},
319 #if defined (PR_STEP)
320 {PR_STEP
, "PR_STEP", "Process has single step pending"},
323 {PR_KLC
, "PR_KLC", "Kill-on-last-close is in effect"},
325 #if defined (PR_ASYNC)
326 {PR_ASYNC
, "PR_ASYNC", "Asynchronous stop is in effect"},
328 #if defined (PR_PCOMPAT)
329 {PR_PCOMPAT
, "PR_PCOMPAT", "Ptrace compatibility mode in effect"},
331 #if defined (PR_MSACCT)
332 {PR_MSACCT
, "PR_MSACCT", "Microstate accounting enabled"},
334 #if defined (PR_BPTADJ)
335 {PR_BPTADJ
, "PR_BPTADJ", "Breakpoint PC adjustment in effect"},
337 #if defined (PR_ASLWP)
338 {PR_ASLWP
, "PR_ASLWP", "Asynchronus signal LWP"},
343 /* Translate values in the pr_why field of the prstatus struct. */
345 static struct trans pr_why_table
[] =
347 #if defined (PR_REQUESTED)
348 {PR_REQUESTED
, "PR_REQUESTED", "Directed to stop via PIOCSTOP/PIOCWSTOP"},
350 #if defined (PR_SIGNALLED)
351 {PR_SIGNALLED
, "PR_SIGNALLED", "Receipt of a traced signal"},
353 #if defined (PR_SYSENTRY)
354 {PR_SYSENTRY
, "PR_SYSENTRY", "Entry to a traced system call"},
356 #if defined (PR_SYSEXIT)
357 {PR_SYSEXIT
, "PR_SYSEXIT", "Exit from a traced system call"},
359 #if defined (PR_JOBCONTROL)
360 {PR_JOBCONTROL
, "PR_JOBCONTROL", "Default job control stop signal action"},
362 #if defined (PR_FAULTED)
363 {PR_FAULTED
, "PR_FAULTED", "Incurred a traced hardware fault"},
365 #if defined (PR_SUSPENDED)
366 {PR_SUSPENDED
, "PR_SUSPENDED", "Process suspended"},
368 #if defined (PR_CHECKPOINT)
369 {PR_CHECKPOINT
, "PR_CHECKPOINT", "(???)"},
374 /* Hardware fault translation table. */
376 static struct trans faults_table
[] =
379 {FLTILL
, "FLTILL", "Illegal instruction"},
381 #if defined (FLTPRIV)
382 {FLTPRIV
, "FLTPRIV", "Privileged instruction"},
385 {FLTBPT
, "FLTBPT", "Breakpoint trap"},
387 #if defined (FLTTRACE)
388 {FLTTRACE
, "FLTTRACE", "Trace trap"},
390 #if defined (FLTACCESS)
391 {FLTACCESS
, "FLTACCESS", "Memory access fault"},
393 #if defined (FLTBOUNDS)
394 {FLTBOUNDS
, "FLTBOUNDS", "Memory bounds violation"},
396 #if defined (FLTIOVF)
397 {FLTIOVF
, "FLTIOVF", "Integer overflow"},
399 #if defined (FLTIZDIV)
400 {FLTIZDIV
, "FLTIZDIV", "Integer zero divide"},
403 {FLTFPE
, "FLTFPE", "Floating-point exception"},
405 #if defined (FLTSTACK)
406 {FLTSTACK
, "FLTSTACK", "Unrecoverable stack fault"},
408 #if defined (FLTPAGE)
409 {FLTPAGE
, "FLTPAGE", "Recoverable page fault"},
414 /* Translation table for signal generation information. See UNIX System
415 V Release 4 Programmer's Reference Manual, siginfo(5). */
417 static struct sigcode
426 #if defined (SIGILL) && defined (ILL_ILLOPC)
428 SIGILL
, ILL_ILLOPC
, "ILL_ILLOPC", "Illegal opcode"
432 #if defined (SIGILL) && defined (ILL_ILLOPN)
434 SIGILL
, ILL_ILLOPN
, "ILL_ILLOPN", "Illegal operand",
438 #if defined (SIGILL) && defined (ILL_ILLADR)
440 SIGILL
, ILL_ILLADR
, "ILL_ILLADR", "Illegal addressing mode"
444 #if defined (SIGILL) && defined (ILL_ILLTRP)
446 SIGILL
, ILL_ILLTRP
, "ILL_ILLTRP", "Illegal trap"
450 #if defined (SIGILL) && defined (ILL_PRVOPC)
452 SIGILL
, ILL_PRVOPC
, "ILL_PRVOPC", "Privileged opcode"
456 #if defined (SIGILL) && defined (ILL_PRVREG)
458 SIGILL
, ILL_PRVREG
, "ILL_PRVREG", "Privileged register"
462 #if defined (SIGILL) && defined (ILL_COPROC)
464 SIGILL
, ILL_COPROC
, "ILL_COPROC", "Coprocessor error"
468 #if defined (SIGILL) && defined (ILL_BADSTK)
470 SIGILL
, ILL_BADSTK
, "ILL_BADSTK", "Internal stack error"
474 #if defined (SIGFPE) && defined (FPE_INTDIV)
476 SIGFPE
, FPE_INTDIV
, "FPE_INTDIV", "Integer divide by zero"
480 #if defined (SIGFPE) && defined (FPE_INTOVF)
482 SIGFPE
, FPE_INTOVF
, "FPE_INTOVF", "Integer overflow"
486 #if defined (SIGFPE) && defined (FPE_FLTDIV)
488 SIGFPE
, FPE_FLTDIV
, "FPE_FLTDIV", "Floating point divide by zero"
492 #if defined (SIGFPE) && defined (FPE_FLTOVF)
494 SIGFPE
, FPE_FLTOVF
, "FPE_FLTOVF", "Floating point overflow"
498 #if defined (SIGFPE) && defined (FPE_FLTUND)
500 SIGFPE
, FPE_FLTUND
, "FPE_FLTUND", "Floating point underflow"
504 #if defined (SIGFPE) && defined (FPE_FLTRES)
506 SIGFPE
, FPE_FLTRES
, "FPE_FLTRES", "Floating point inexact result"
510 #if defined (SIGFPE) && defined (FPE_FLTINV)
512 SIGFPE
, FPE_FLTINV
, "FPE_FLTINV", "Invalid floating point operation"
516 #if defined (SIGFPE) && defined (FPE_FLTSUB)
518 SIGFPE
, FPE_FLTSUB
, "FPE_FLTSUB", "Subscript out of range"
522 #if defined (SIGSEGV) && defined (SEGV_MAPERR)
524 SIGSEGV
, SEGV_MAPERR
, "SEGV_MAPERR", "Address not mapped to object"
528 #if defined (SIGSEGV) && defined (SEGV_ACCERR)
530 SIGSEGV
, SEGV_ACCERR
, "SEGV_ACCERR", "Invalid permissions for object"
534 #if defined (SIGBUS) && defined (BUS_ADRALN)
536 SIGBUS
, BUS_ADRALN
, "BUS_ADRALN", "Invalid address alignment"
540 #if defined (SIGBUS) && defined (BUS_ADRERR)
542 SIGBUS
, BUS_ADRERR
, "BUS_ADRERR", "Non-existent physical address"
546 #if defined (SIGBUS) && defined (BUS_OBJERR)
548 SIGBUS
, BUS_OBJERR
, "BUS_OBJERR", "Object specific hardware error"
552 #if defined (SIGTRAP) && defined (TRAP_BRKPT)
554 SIGTRAP
, TRAP_BRKPT
, "TRAP_BRKPT", "Process breakpoint"
558 #if defined (SIGTRAP) && defined (TRAP_TRACE)
560 SIGTRAP
, TRAP_TRACE
, "TRAP_TRACE", "Process trace trap"
564 #if defined (SIGCLD) && defined (CLD_EXITED)
566 SIGCLD
, CLD_EXITED
, "CLD_EXITED", "Child has exited"
570 #if defined (SIGCLD) && defined (CLD_KILLED)
572 SIGCLD
, CLD_KILLED
, "CLD_KILLED", "Child was killed"
576 #if defined (SIGCLD) && defined (CLD_DUMPED)
578 SIGCLD
, CLD_DUMPED
, "CLD_DUMPED", "Child has terminated abnormally"
582 #if defined (SIGCLD) && defined (CLD_TRAPPED)
584 SIGCLD
, CLD_TRAPPED
, "CLD_TRAPPED", "Traced child has trapped"
588 #if defined (SIGCLD) && defined (CLD_STOPPED)
590 SIGCLD
, CLD_STOPPED
, "CLD_STOPPED", "Child has stopped"
594 #if defined (SIGCLD) && defined (CLD_CONTINUED)
596 SIGCLD
, CLD_CONTINUED
, "CLD_CONTINUED", "Stopped child had continued"
600 #if defined (SIGPOLL) && defined (POLL_IN)
602 SIGPOLL
, POLL_IN
, "POLL_IN", "Input input available"
606 #if defined (SIGPOLL) && defined (POLL_OUT)
608 SIGPOLL
, POLL_OUT
, "POLL_OUT", "Output buffers available"
612 #if defined (SIGPOLL) && defined (POLL_MSG)
614 SIGPOLL
, POLL_MSG
, "POLL_MSG", "Input message available"
618 #if defined (SIGPOLL) && defined (POLL_ERR)
620 SIGPOLL
, POLL_ERR
, "POLL_ERR", "I/O error"
624 #if defined (SIGPOLL) && defined (POLL_PRI)
626 SIGPOLL
, POLL_PRI
, "POLL_PRI", "High priority input available"
630 #if defined (SIGPOLL) && defined (POLL_HUP)
632 SIGPOLL
, POLL_HUP
, "POLL_HUP", "Device disconnected"
641 static char *syscall_table
[MAX_SYSCALLS
];
643 /* Prototypes for local functions */
645 static void procfs_stop
PARAMS ((void));
647 static int procfs_thread_alive
PARAMS ((int));
649 static int procfs_can_run
PARAMS ((void));
651 static void procfs_mourn_inferior
PARAMS ((void));
653 static void procfs_fetch_registers
PARAMS ((int));
655 static int procfs_wait
PARAMS ((int, struct target_waitstatus
*));
657 static void procfs_open
PARAMS ((char *, int));
659 static void procfs_files_info
PARAMS ((struct target_ops
*));
661 static void procfs_prepare_to_store
PARAMS ((void));
663 static void procfs_detach
PARAMS ((char *, int));
665 static void procfs_attach
PARAMS ((char *, int));
667 static void proc_set_exec_trap
PARAMS ((void));
669 static void procfs_init_inferior
PARAMS ((int));
671 static struct procinfo
*create_procinfo
PARAMS ((int));
673 static void procfs_store_registers
PARAMS ((int));
675 static int procfs_xfer_memory
PARAMS ((CORE_ADDR
, char *, int, int, struct target_ops
*));
677 static void procfs_kill_inferior
PARAMS ((void));
679 static char *sigcodedesc
PARAMS ((siginfo_t
*));
681 static char *sigcodename
PARAMS ((siginfo_t
*));
683 static struct procinfo
*wait_fd
PARAMS ((void));
685 static void remove_fd
PARAMS ((struct procinfo
*));
687 static void add_fd
PARAMS ((struct procinfo
*));
689 static void set_proc_siginfo
PARAMS ((struct procinfo
*, int));
691 static void init_syscall_table
PARAMS ((void));
693 static char *syscallname
PARAMS ((int));
695 static char *signalname
PARAMS ((int));
697 static char *errnoname
PARAMS ((int));
699 static int proc_address_to_fd
PARAMS ((struct procinfo
*, CORE_ADDR
, int));
701 static int open_proc_file
PARAMS ((int, struct procinfo
*, int, int));
703 static void close_proc_file
PARAMS ((struct procinfo
*));
705 static void close_proc_file_cleanup
PARAMS ((void *));
707 static struct cleanup
*make_cleanup_close_proc_file
PARAMS ((struct procinfo
*));
709 static void unconditionally_kill_inferior
PARAMS ((struct procinfo
*));
711 static NORETURN
void proc_init_failed
712 PARAMS ((struct procinfo
*, char *, int)) ATTR_NORETURN
;
714 static void info_proc
PARAMS ((char *, int));
716 static void info_proc_flags
PARAMS ((struct procinfo
*, int));
718 static void info_proc_stop
PARAMS ((struct procinfo
*, int));
720 static void info_proc_siginfo
PARAMS ((struct procinfo
*, int));
722 static void info_proc_syscalls
PARAMS ((struct procinfo
*, int));
724 static void info_proc_mappings
PARAMS ((struct procinfo
*, int));
726 static void info_proc_signals
PARAMS ((struct procinfo
*, int));
728 static void info_proc_faults
PARAMS ((struct procinfo
*, int));
730 static char *mappingflags
PARAMS ((long));
732 static char *lookupname
PARAMS ((struct trans
*, unsigned int, char *));
734 static char *lookupdesc
PARAMS ((struct trans
*, unsigned int));
736 static int do_attach
PARAMS ((int pid
));
738 static void do_detach
PARAMS ((int siggnal
));
740 static void procfs_create_inferior
PARAMS ((char *, char *, char **));
742 static void procfs_notice_signals
PARAMS ((int pid
));
744 static void notice_signals
PARAMS ((struct procinfo
*, struct sig_ctl
*));
746 static struct procinfo
*find_procinfo
PARAMS ((pid_t pid
, int okfail
));
748 static int procfs_write_pcwstop
PARAMS ((struct procinfo
*));
749 static int procfs_read_status
PARAMS ((struct procinfo
*));
750 static void procfs_write_pckill
PARAMS ((struct procinfo
*));
752 typedef int syscall_func_t
PARAMS ((struct procinfo
* pi
, int syscall_num
,
753 int why
, int *rtnval
, int *statval
));
755 static void procfs_set_syscall_trap
PARAMS ((struct procinfo
* pi
,
756 int syscall_num
, int flags
,
757 syscall_func_t
* func
));
759 static void procfs_clear_syscall_trap
PARAMS ((struct procinfo
* pi
,
760 int syscall_num
, int errok
));
762 #define PROCFS_SYSCALL_ENTRY 0x1 /* Trap on entry to sys call */
763 #define PROCFS_SYSCALL_EXIT 0x2 /* Trap on exit from sys call */
765 static syscall_func_t procfs_exit_handler
;
767 static syscall_func_t procfs_exec_handler
;
770 static syscall_func_t procfs_sproc_handler
;
771 static syscall_func_t procfs_fork_handler
;
774 #ifdef SYS_lwp_create
775 static syscall_func_t procfs_lwp_creation_handler
;
778 static void modify_inherit_on_fork_flag
PARAMS ((int fd
, int flag
));
779 static void modify_run_on_last_close_flag
PARAMS ((int fd
, int flag
));
783 struct procfs_syscall_handler
785 int syscall_num
; /* The number of the system call being handled */
786 /* The function to be called */
787 syscall_func_t
*func
;
790 static void procfs_resume
PARAMS ((int pid
, int step
,
791 enum target_signal signo
));
793 static void init_procfs_ops
PARAMS ((void));
795 /* External function prototypes that can't be easily included in any
796 header file because the args are typedefs in system include files. */
798 extern void supply_gregset
PARAMS ((gdb_gregset_t
*));
800 extern void fill_gregset
PARAMS ((gdb_gregset_t
*, int));
803 extern void supply_fpregset
PARAMS ((gdb_fpregset_t
*));
805 extern void fill_fpregset
PARAMS ((gdb_fpregset_t
*, int));
812 find_procinfo -- convert a process id to a struct procinfo
816 static struct procinfo * find_procinfo (pid_t pid, int okfail);
820 Given a process id, look it up in the procinfo chain. Returns
821 a struct procinfo *. If can't find pid, then call error(),
822 unless okfail is set, in which case, return NULL;
825 static struct procinfo
*
826 find_procinfo (pid
, okfail
)
830 struct procinfo
*procinfo
;
832 for (procinfo
= procinfo_list
; procinfo
; procinfo
= procinfo
->next
)
833 if (procinfo
->pid
== pid
)
839 error ("procfs (find_procinfo): Couldn't locate pid %d", pid
);
846 current_procinfo -- convert inferior_pid to a struct procinfo
850 static struct procinfo * current_procinfo;
854 Looks up inferior_pid in the procinfo chain. Always returns a
855 struct procinfo *. If process can't be found, we error() out.
858 #define current_procinfo find_procinfo (inferior_pid, 0)
864 add_fd -- Add the fd to the poll/select list
868 static void add_fd (struct procinfo *);
872 Add the fd of the supplied procinfo to the list of fds used for
873 poll/select operations.
880 if (num_poll_list
<= 0)
881 poll_list
= (struct pollfd
*) xmalloc (sizeof (struct pollfd
));
883 poll_list
= (struct pollfd
*) xrealloc (poll_list
,
885 * sizeof (struct pollfd
));
886 poll_list
[num_poll_list
].fd
= pi
->ctl_fd
;
888 poll_list
[num_poll_list
].events
= POLLWRNORM
;
890 poll_list
[num_poll_list
].events
= POLLPRI
;
900 remove_fd -- Remove the fd from the poll/select list
904 static void remove_fd (struct procinfo *);
908 Remove the fd of the supplied procinfo from the list of fds used
909 for poll/select operations.
918 for (i
= 0; i
< num_poll_list
; i
++)
920 if (poll_list
[i
].fd
== pi
->ctl_fd
)
922 if (i
!= num_poll_list
- 1)
923 memcpy (poll_list
+ i
, poll_list
+ i
+ 1,
924 (num_poll_list
- i
- 1) * sizeof (struct pollfd
));
928 if (num_poll_list
== 0)
931 poll_list
= (struct pollfd
*) xrealloc (poll_list
,
933 * sizeof (struct pollfd
));
943 procfs_read_status - get procfs fd status
947 static int procfs_read_status (pi) struct procinfo *pi;
951 Given a pointer to a procinfo struct, get the status of
952 the status_fd in the appropriate way. Returns 0 on failure,
957 procfs_read_status (pi
)
960 #ifdef PROCFS_USE_READ_WRITE
961 if ((lseek (pi
->status_fd
, 0, SEEK_SET
) < 0) ||
962 (read (pi
->status_fd
, (char *) &pi
->prstatus
,
963 sizeof (gdb_prstatus_t
)) != sizeof (gdb_prstatus_t
)))
965 if (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) < 0)
976 procfs_write_pcwstop - send a PCWSTOP to procfs fd
980 static int procfs_write_pcwstop (pi) struct procinfo *pi;
984 Given a pointer to a procinfo struct, send a PCWSTOP to
985 the ctl_fd in the appropriate way. Returns 0 on failure,
990 procfs_write_pcwstop (pi
)
993 #ifdef PROCFS_USE_READ_WRITE
995 if (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (long)) < 0)
997 if (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) < 0)
1008 procfs_write_pckill - send a kill to procfs fd
1012 static void procfs_write_pckill (pi) struct procinfo *pi;
1016 Given a pointer to a procinfo struct, send a kill to
1017 the ctl_fd in the appropriate way. Returns 0 on failure,
1022 procfs_write_pckill (pi
)
1023 struct procinfo
*pi
;
1025 #ifdef PROCFS_USE_READ_WRITE
1026 struct proc_ctl pctl
;
1028 pctl
.data
= SIGKILL
;
1029 write (pi
->ctl_fd
, &pctl
, sizeof (struct proc_ctl
));
1031 int signo
= SIGKILL
;
1032 ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
);
1036 static struct procinfo
*
1039 struct procinfo
*pi
, *next_pi
;
1045 set_sigint_trap (); /* Causes SIGINT to be passed on to the
1046 attached process. */
1053 num_fds
= poll (poll_list
, num_poll_list
, -1);
1056 if (num_fds
< 0 && errno
== EINTR
)
1058 print_sys_errmsg ("poll failed", errno
);
1059 error ("Poll failed, returned %d", num_fds
);
1061 #else /* LOSING_POLL */
1062 pi
= current_procinfo
;
1064 while (!procfs_write_pcwstop (pi
))
1066 if (errno
== ENOENT
)
1068 /* Process exited. */
1069 pi
->prstatus
.pr_flags
= 0;
1072 else if (errno
!= EINTR
)
1074 print_sys_errmsg (pi
->pathname
, errno
);
1075 error ("procfs_write_pcwstop failed");
1079 #endif /* LOSING_POLL */
1081 clear_sigint_trap ();
1082 clear_sigio_trap ();
1086 for (i
= 0; i
< num_poll_list
&& num_fds
> 0; i
++)
1088 if (0 == (poll_list
[i
].revents
&
1089 (POLLWRNORM
| POLLPRI
| POLLERR
| POLLHUP
| POLLNVAL
)))
1091 for (pi
= procinfo_list
; pi
; pi
= next_pi
)
1094 if (poll_list
[i
].fd
== pi
->ctl_fd
)
1097 if ((poll_list
[i
].revents
& POLLHUP
) != 0 ||
1098 !procfs_read_status (pi
))
1099 { /* The LWP has apparently terminated. */
1100 if (num_poll_list
<= 1)
1102 pi
->prstatus
.pr_flags
= 0;
1107 printf_filtered ("LWP %d exited.\n",
1108 (pi
->pid
>> 16) & 0xffff);
1109 close_proc_file (pi
);
1110 i
--; /* don't skip deleted entry */
1112 break; /* already another event to process */
1114 goto wait_again
; /* wait for another event */
1121 error ("wait_fd: Couldn't find procinfo for fd %d\n",
1124 #endif /* LOSING_POLL */
1133 lookupdesc -- translate a value to a summary desc string
1137 static char *lookupdesc (struct trans *transp, unsigned int val);
1141 Given a pointer to a translation table and a value to be translated,
1142 lookup the desc string and return it.
1146 lookupdesc (transp
, val
)
1147 struct trans
*transp
;
1152 for (desc
= NULL
; transp
->name
!= NULL
; transp
++)
1154 if (transp
->value
== val
)
1156 desc
= transp
->desc
;
1161 /* Didn't find a translation for the specified value, set a default one. */
1174 lookupname -- translate a value to symbolic name
1178 static char *lookupname (struct trans *transp, unsigned int val,
1183 Given a pointer to a translation table, a value to be translated,
1184 and a default prefix to return if the value can't be translated,
1185 match the value with one of the translation table entries and
1186 return a pointer to the symbolic name.
1188 If no match is found it just returns the value as a printable string,
1189 with the given prefix. The previous such value, if any, is freed
1194 lookupname (transp
, val
, prefix
)
1195 struct trans
*transp
;
1199 static char *locbuf
;
1202 for (name
= NULL
; transp
->name
!= NULL
; transp
++)
1204 if (transp
->value
== val
)
1206 name
= transp
->name
;
1211 /* Didn't find a translation for the specified value, build a default
1212 one using the specified prefix and return it. The lifetime of
1213 the value is only until the next one is needed. */
1221 locbuf
= xmalloc (strlen (prefix
) + 16);
1222 sprintf (locbuf
, "%s %u", prefix
, val
);
1232 struct sigcode
*scp
;
1234 static char locbuf
[32];
1236 for (scp
= siginfo_table
; scp
->codename
!= NULL
; scp
++)
1238 if ((scp
->signo
== sip
->si_signo
) &&
1239 (scp
->code
== sip
->si_code
))
1241 name
= scp
->codename
;
1247 sprintf (locbuf
, "sigcode %u", sip
->si_signo
);
1257 struct sigcode
*scp
;
1260 for (scp
= siginfo_table
; scp
->codename
!= NULL
; scp
++)
1262 if ((scp
->signo
== sip
->si_signo
) &&
1263 (scp
->code
== sip
->si_code
))
1271 desc
= "Unrecognized signal or trap use";
1280 syscallname - translate a system call number into a system call name
1284 char *syscallname (int syscallnum)
1288 Given a system call number, translate it into the printable name
1289 of a system call, or into "syscall <num>" if it is an unknown
1294 syscallname (syscallnum
)
1297 static char locbuf
[32];
1299 if (syscallnum
>= 0 && syscallnum
< MAX_SYSCALLS
1300 && syscall_table
[syscallnum
] != NULL
)
1301 return syscall_table
[syscallnum
];
1304 sprintf (locbuf
, "syscall %u", syscallnum
);
1313 init_syscall_table - initialize syscall translation table
1317 void init_syscall_table (void)
1321 Dynamically initialize the translation table to convert system
1322 call numbers into printable system call names. Done once per
1323 gdb run, on initialization.
1327 This is awfully ugly, but preprocessor tricks to make it prettier
1328 tend to be nonportable.
1332 init_syscall_table ()
1334 #if defined (SYS_exit)
1335 syscall_table
[SYS_exit
] = "exit";
1337 #if defined (SYS_fork)
1338 syscall_table
[SYS_fork
] = "fork";
1340 #if defined (SYS_read)
1341 syscall_table
[SYS_read
] = "read";
1343 #if defined (SYS_write)
1344 syscall_table
[SYS_write
] = "write";
1346 #if defined (SYS_open)
1347 syscall_table
[SYS_open
] = "open";
1349 #if defined (SYS_close)
1350 syscall_table
[SYS_close
] = "close";
1352 #if defined (SYS_wait)
1353 syscall_table
[SYS_wait
] = "wait";
1355 #if defined (SYS_creat)
1356 syscall_table
[SYS_creat
] = "creat";
1358 #if defined (SYS_link)
1359 syscall_table
[SYS_link
] = "link";
1361 #if defined (SYS_unlink)
1362 syscall_table
[SYS_unlink
] = "unlink";
1364 #if defined (SYS_exec)
1365 syscall_table
[SYS_exec
] = "exec";
1367 #if defined (SYS_execv)
1368 syscall_table
[SYS_execv
] = "execv";
1370 #if defined (SYS_execve)
1371 syscall_table
[SYS_execve
] = "execve";
1373 #if defined (SYS_chdir)
1374 syscall_table
[SYS_chdir
] = "chdir";
1376 #if defined (SYS_time)
1377 syscall_table
[SYS_time
] = "time";
1379 #if defined (SYS_mknod)
1380 syscall_table
[SYS_mknod
] = "mknod";
1382 #if defined (SYS_chmod)
1383 syscall_table
[SYS_chmod
] = "chmod";
1385 #if defined (SYS_chown)
1386 syscall_table
[SYS_chown
] = "chown";
1388 #if defined (SYS_brk)
1389 syscall_table
[SYS_brk
] = "brk";
1391 #if defined (SYS_stat)
1392 syscall_table
[SYS_stat
] = "stat";
1394 #if defined (SYS_lseek)
1395 syscall_table
[SYS_lseek
] = "lseek";
1397 #if defined (SYS_getpid)
1398 syscall_table
[SYS_getpid
] = "getpid";
1400 #if defined (SYS_mount)
1401 syscall_table
[SYS_mount
] = "mount";
1403 #if defined (SYS_umount)
1404 syscall_table
[SYS_umount
] = "umount";
1406 #if defined (SYS_setuid)
1407 syscall_table
[SYS_setuid
] = "setuid";
1409 #if defined (SYS_getuid)
1410 syscall_table
[SYS_getuid
] = "getuid";
1412 #if defined (SYS_stime)
1413 syscall_table
[SYS_stime
] = "stime";
1415 #if defined (SYS_ptrace)
1416 syscall_table
[SYS_ptrace
] = "ptrace";
1418 #if defined (SYS_alarm)
1419 syscall_table
[SYS_alarm
] = "alarm";
1421 #if defined (SYS_fstat)
1422 syscall_table
[SYS_fstat
] = "fstat";
1424 #if defined (SYS_pause)
1425 syscall_table
[SYS_pause
] = "pause";
1427 #if defined (SYS_utime)
1428 syscall_table
[SYS_utime
] = "utime";
1430 #if defined (SYS_stty)
1431 syscall_table
[SYS_stty
] = "stty";
1433 #if defined (SYS_gtty)
1434 syscall_table
[SYS_gtty
] = "gtty";
1436 #if defined (SYS_access)
1437 syscall_table
[SYS_access
] = "access";
1439 #if defined (SYS_nice)
1440 syscall_table
[SYS_nice
] = "nice";
1442 #if defined (SYS_statfs)
1443 syscall_table
[SYS_statfs
] = "statfs";
1445 #if defined (SYS_sync)
1446 syscall_table
[SYS_sync
] = "sync";
1448 #if defined (SYS_kill)
1449 syscall_table
[SYS_kill
] = "kill";
1451 #if defined (SYS_fstatfs)
1452 syscall_table
[SYS_fstatfs
] = "fstatfs";
1454 #if defined (SYS_pgrpsys)
1455 syscall_table
[SYS_pgrpsys
] = "pgrpsys";
1457 #if defined (SYS_xenix)
1458 syscall_table
[SYS_xenix
] = "xenix";
1460 #if defined (SYS_dup)
1461 syscall_table
[SYS_dup
] = "dup";
1463 #if defined (SYS_pipe)
1464 syscall_table
[SYS_pipe
] = "pipe";
1466 #if defined (SYS_times)
1467 syscall_table
[SYS_times
] = "times";
1469 #if defined (SYS_profil)
1470 syscall_table
[SYS_profil
] = "profil";
1472 #if defined (SYS_plock)
1473 syscall_table
[SYS_plock
] = "plock";
1475 #if defined (SYS_setgid)
1476 syscall_table
[SYS_setgid
] = "setgid";
1478 #if defined (SYS_getgid)
1479 syscall_table
[SYS_getgid
] = "getgid";
1481 #if defined (SYS_signal)
1482 syscall_table
[SYS_signal
] = "signal";
1484 #if defined (SYS_msgsys)
1485 syscall_table
[SYS_msgsys
] = "msgsys";
1487 #if defined (SYS_sys3b)
1488 syscall_table
[SYS_sys3b
] = "sys3b";
1490 #if defined (SYS_sysi86)
1491 syscall_table
[SYS_sysi86
] = "sysi86";
1493 #if defined (SYS_acct)
1494 syscall_table
[SYS_acct
] = "acct";
1496 #if defined (SYS_shmsys)
1497 syscall_table
[SYS_shmsys
] = "shmsys";
1499 #if defined (SYS_semsys)
1500 syscall_table
[SYS_semsys
] = "semsys";
1502 #if defined (SYS_ioctl)
1503 syscall_table
[SYS_ioctl
] = "ioctl";
1505 #if defined (SYS_uadmin)
1506 syscall_table
[SYS_uadmin
] = "uadmin";
1508 #if defined (SYS_utssys)
1509 syscall_table
[SYS_utssys
] = "utssys";
1511 #if defined (SYS_fsync)
1512 syscall_table
[SYS_fsync
] = "fsync";
1514 #if defined (SYS_umask)
1515 syscall_table
[SYS_umask
] = "umask";
1517 #if defined (SYS_chroot)
1518 syscall_table
[SYS_chroot
] = "chroot";
1520 #if defined (SYS_fcntl)
1521 syscall_table
[SYS_fcntl
] = "fcntl";
1523 #if defined (SYS_ulimit)
1524 syscall_table
[SYS_ulimit
] = "ulimit";
1526 #if defined (SYS_rfsys)
1527 syscall_table
[SYS_rfsys
] = "rfsys";
1529 #if defined (SYS_rmdir)
1530 syscall_table
[SYS_rmdir
] = "rmdir";
1532 #if defined (SYS_mkdir)
1533 syscall_table
[SYS_mkdir
] = "mkdir";
1535 #if defined (SYS_getdents)
1536 syscall_table
[SYS_getdents
] = "getdents";
1538 #if defined (SYS_sysfs)
1539 syscall_table
[SYS_sysfs
] = "sysfs";
1541 #if defined (SYS_getmsg)
1542 syscall_table
[SYS_getmsg
] = "getmsg";
1544 #if defined (SYS_putmsg)
1545 syscall_table
[SYS_putmsg
] = "putmsg";
1547 #if defined (SYS_poll)
1548 syscall_table
[SYS_poll
] = "poll";
1550 #if defined (SYS_lstat)
1551 syscall_table
[SYS_lstat
] = "lstat";
1553 #if defined (SYS_symlink)
1554 syscall_table
[SYS_symlink
] = "symlink";
1556 #if defined (SYS_readlink)
1557 syscall_table
[SYS_readlink
] = "readlink";
1559 #if defined (SYS_setgroups)
1560 syscall_table
[SYS_setgroups
] = "setgroups";
1562 #if defined (SYS_getgroups)
1563 syscall_table
[SYS_getgroups
] = "getgroups";
1565 #if defined (SYS_fchmod)
1566 syscall_table
[SYS_fchmod
] = "fchmod";
1568 #if defined (SYS_fchown)
1569 syscall_table
[SYS_fchown
] = "fchown";
1571 #if defined (SYS_sigprocmask)
1572 syscall_table
[SYS_sigprocmask
] = "sigprocmask";
1574 #if defined (SYS_sigsuspend)
1575 syscall_table
[SYS_sigsuspend
] = "sigsuspend";
1577 #if defined (SYS_sigaltstack)
1578 syscall_table
[SYS_sigaltstack
] = "sigaltstack";
1580 #if defined (SYS_sigaction)
1581 syscall_table
[SYS_sigaction
] = "sigaction";
1583 #if defined (SYS_sigpending)
1584 syscall_table
[SYS_sigpending
] = "sigpending";
1586 #if defined (SYS_context)
1587 syscall_table
[SYS_context
] = "context";
1589 #if defined (SYS_evsys)
1590 syscall_table
[SYS_evsys
] = "evsys";
1592 #if defined (SYS_evtrapret)
1593 syscall_table
[SYS_evtrapret
] = "evtrapret";
1595 #if defined (SYS_statvfs)
1596 syscall_table
[SYS_statvfs
] = "statvfs";
1598 #if defined (SYS_fstatvfs)
1599 syscall_table
[SYS_fstatvfs
] = "fstatvfs";
1601 #if defined (SYS_nfssys)
1602 syscall_table
[SYS_nfssys
] = "nfssys";
1604 #if defined (SYS_waitsys)
1605 syscall_table
[SYS_waitsys
] = "waitsys";
1607 #if defined (SYS_sigsendsys)
1608 syscall_table
[SYS_sigsendsys
] = "sigsendsys";
1610 #if defined (SYS_hrtsys)
1611 syscall_table
[SYS_hrtsys
] = "hrtsys";
1613 #if defined (SYS_acancel)
1614 syscall_table
[SYS_acancel
] = "acancel";
1616 #if defined (SYS_async)
1617 syscall_table
[SYS_async
] = "async";
1619 #if defined (SYS_priocntlsys)
1620 syscall_table
[SYS_priocntlsys
] = "priocntlsys";
1622 #if defined (SYS_pathconf)
1623 syscall_table
[SYS_pathconf
] = "pathconf";
1625 #if defined (SYS_mincore)
1626 syscall_table
[SYS_mincore
] = "mincore";
1628 #if defined (SYS_mmap)
1629 syscall_table
[SYS_mmap
] = "mmap";
1631 #if defined (SYS_mprotect)
1632 syscall_table
[SYS_mprotect
] = "mprotect";
1634 #if defined (SYS_munmap)
1635 syscall_table
[SYS_munmap
] = "munmap";
1637 #if defined (SYS_fpathconf)
1638 syscall_table
[SYS_fpathconf
] = "fpathconf";
1640 #if defined (SYS_vfork)
1641 syscall_table
[SYS_vfork
] = "vfork";
1643 #if defined (SYS_fchdir)
1644 syscall_table
[SYS_fchdir
] = "fchdir";
1646 #if defined (SYS_readv)
1647 syscall_table
[SYS_readv
] = "readv";
1649 #if defined (SYS_writev)
1650 syscall_table
[SYS_writev
] = "writev";
1652 #if defined (SYS_xstat)
1653 syscall_table
[SYS_xstat
] = "xstat";
1655 #if defined (SYS_lxstat)
1656 syscall_table
[SYS_lxstat
] = "lxstat";
1658 #if defined (SYS_fxstat)
1659 syscall_table
[SYS_fxstat
] = "fxstat";
1661 #if defined (SYS_xmknod)
1662 syscall_table
[SYS_xmknod
] = "xmknod";
1664 #if defined (SYS_clocal)
1665 syscall_table
[SYS_clocal
] = "clocal";
1667 #if defined (SYS_setrlimit)
1668 syscall_table
[SYS_setrlimit
] = "setrlimit";
1670 #if defined (SYS_getrlimit)
1671 syscall_table
[SYS_getrlimit
] = "getrlimit";
1673 #if defined (SYS_lchown)
1674 syscall_table
[SYS_lchown
] = "lchown";
1676 #if defined (SYS_memcntl)
1677 syscall_table
[SYS_memcntl
] = "memcntl";
1679 #if defined (SYS_getpmsg)
1680 syscall_table
[SYS_getpmsg
] = "getpmsg";
1682 #if defined (SYS_putpmsg)
1683 syscall_table
[SYS_putpmsg
] = "putpmsg";
1685 #if defined (SYS_rename)
1686 syscall_table
[SYS_rename
] = "rename";
1688 #if defined (SYS_uname)
1689 syscall_table
[SYS_uname
] = "uname";
1691 #if defined (SYS_setegid)
1692 syscall_table
[SYS_setegid
] = "setegid";
1694 #if defined (SYS_sysconfig)
1695 syscall_table
[SYS_sysconfig
] = "sysconfig";
1697 #if defined (SYS_adjtime)
1698 syscall_table
[SYS_adjtime
] = "adjtime";
1700 #if defined (SYS_systeminfo)
1701 syscall_table
[SYS_systeminfo
] = "systeminfo";
1703 #if defined (SYS_seteuid)
1704 syscall_table
[SYS_seteuid
] = "seteuid";
1706 #if defined (SYS_sproc)
1707 syscall_table
[SYS_sproc
] = "sproc";
1709 #if defined (SYS_keyctl)
1710 syscall_table
[SYS_keyctl
] = "keyctl";
1712 #if defined (SYS_secsys)
1713 syscall_table
[SYS_secsys
] = "secsys";
1715 #if defined (SYS_filepriv)
1716 syscall_table
[SYS_filepriv
] = "filepriv";
1718 #if defined (SYS_procpriv)
1719 syscall_table
[SYS_procpriv
] = "procpriv";
1721 #if defined (SYS_devstat)
1722 syscall_table
[SYS_devstat
] = "devstat";
1724 #if defined (SYS_aclipc)
1725 syscall_table
[SYS_aclipc
] = "aclipc";
1727 #if defined (SYS_fdevstat)
1728 syscall_table
[SYS_fdevstat
] = "fdevstat";
1730 #if defined (SYS_flvlfile)
1731 syscall_table
[SYS_flvlfile
] = "flvlfile";
1733 #if defined (SYS_lvlfile)
1734 syscall_table
[SYS_lvlfile
] = "lvlfile";
1736 #if defined (SYS_lvlequal)
1737 syscall_table
[SYS_lvlequal
] = "lvlequal";
1739 #if defined (SYS_lvlproc)
1740 syscall_table
[SYS_lvlproc
] = "lvlproc";
1742 #if defined (SYS_lvlipc)
1743 syscall_table
[SYS_lvlipc
] = "lvlipc";
1745 #if defined (SYS_acl)
1746 syscall_table
[SYS_acl
] = "acl";
1748 #if defined (SYS_auditevt)
1749 syscall_table
[SYS_auditevt
] = "auditevt";
1751 #if defined (SYS_auditctl)
1752 syscall_table
[SYS_auditctl
] = "auditctl";
1754 #if defined (SYS_auditdmp)
1755 syscall_table
[SYS_auditdmp
] = "auditdmp";
1757 #if defined (SYS_auditlog)
1758 syscall_table
[SYS_auditlog
] = "auditlog";
1760 #if defined (SYS_auditbuf)
1761 syscall_table
[SYS_auditbuf
] = "auditbuf";
1763 #if defined (SYS_lvldom)
1764 syscall_table
[SYS_lvldom
] = "lvldom";
1766 #if defined (SYS_lvlvfs)
1767 syscall_table
[SYS_lvlvfs
] = "lvlvfs";
1769 #if defined (SYS_mkmld)
1770 syscall_table
[SYS_mkmld
] = "mkmld";
1772 #if defined (SYS_mldmode)
1773 syscall_table
[SYS_mldmode
] = "mldmode";
1775 #if defined (SYS_secadvise)
1776 syscall_table
[SYS_secadvise
] = "secadvise";
1778 #if defined (SYS_online)
1779 syscall_table
[SYS_online
] = "online";
1781 #if defined (SYS_setitimer)
1782 syscall_table
[SYS_setitimer
] = "setitimer";
1784 #if defined (SYS_getitimer)
1785 syscall_table
[SYS_getitimer
] = "getitimer";
1787 #if defined (SYS_gettimeofday)
1788 syscall_table
[SYS_gettimeofday
] = "gettimeofday";
1790 #if defined (SYS_settimeofday)
1791 syscall_table
[SYS_settimeofday
] = "settimeofday";
1793 #if defined (SYS_lwp_create)
1794 syscall_table
[SYS_lwp_create
] = "_lwp_create";
1796 #if defined (SYS_lwp_exit)
1797 syscall_table
[SYS_lwp_exit
] = "_lwp_exit";
1799 #if defined (SYS_lwp_wait)
1800 syscall_table
[SYS_lwp_wait
] = "_lwp_wait";
1802 #if defined (SYS_lwp_self)
1803 syscall_table
[SYS_lwp_self
] = "_lwp_self";
1805 #if defined (SYS_lwp_info)
1806 syscall_table
[SYS_lwp_info
] = "_lwp_info";
1808 #if defined (SYS_lwp_private)
1809 syscall_table
[SYS_lwp_private
] = "_lwp_private";
1811 #if defined (SYS_processor_bind)
1812 syscall_table
[SYS_processor_bind
] = "processor_bind";
1814 #if defined (SYS_processor_exbind)
1815 syscall_table
[SYS_processor_exbind
] = "processor_exbind";
1817 #if defined (SYS_prepblock)
1818 syscall_table
[SYS_prepblock
] = "prepblock";
1820 #if defined (SYS_block)
1821 syscall_table
[SYS_block
] = "block";
1823 #if defined (SYS_rdblock)
1824 syscall_table
[SYS_rdblock
] = "rdblock";
1826 #if defined (SYS_unblock)
1827 syscall_table
[SYS_unblock
] = "unblock";
1829 #if defined (SYS_cancelblock)
1830 syscall_table
[SYS_cancelblock
] = "cancelblock";
1832 #if defined (SYS_pread)
1833 syscall_table
[SYS_pread
] = "pread";
1835 #if defined (SYS_pwrite)
1836 syscall_table
[SYS_pwrite
] = "pwrite";
1838 #if defined (SYS_truncate)
1839 syscall_table
[SYS_truncate
] = "truncate";
1841 #if defined (SYS_ftruncate)
1842 syscall_table
[SYS_ftruncate
] = "ftruncate";
1844 #if defined (SYS_lwp_kill)
1845 syscall_table
[SYS_lwp_kill
] = "_lwp_kill";
1847 #if defined (SYS_sigwait)
1848 syscall_table
[SYS_sigwait
] = "sigwait";
1850 #if defined (SYS_fork1)
1851 syscall_table
[SYS_fork1
] = "fork1";
1853 #if defined (SYS_forkall)
1854 syscall_table
[SYS_forkall
] = "forkall";
1856 #if defined (SYS_modload)
1857 syscall_table
[SYS_modload
] = "modload";
1859 #if defined (SYS_moduload)
1860 syscall_table
[SYS_moduload
] = "moduload";
1862 #if defined (SYS_modpath)
1863 syscall_table
[SYS_modpath
] = "modpath";
1865 #if defined (SYS_modstat)
1866 syscall_table
[SYS_modstat
] = "modstat";
1868 #if defined (SYS_modadm)
1869 syscall_table
[SYS_modadm
] = "modadm";
1871 #if defined (SYS_getksym)
1872 syscall_table
[SYS_getksym
] = "getksym";
1874 #if defined (SYS_lwp_suspend)
1875 syscall_table
[SYS_lwp_suspend
] = "_lwp_suspend";
1877 #if defined (SYS_lwp_continue)
1878 syscall_table
[SYS_lwp_continue
] = "_lwp_continue";
1880 #if defined (SYS_priocntllst)
1881 syscall_table
[SYS_priocntllst
] = "priocntllst";
1883 #if defined (SYS_sleep)
1884 syscall_table
[SYS_sleep
] = "sleep";
1886 #if defined (SYS_lwp_sema_wait)
1887 syscall_table
[SYS_lwp_sema_wait
] = "_lwp_sema_wait";
1889 #if defined (SYS_lwp_sema_post)
1890 syscall_table
[SYS_lwp_sema_post
] = "_lwp_sema_post";
1892 #if defined (SYS_lwp_sema_trywait)
1893 syscall_table
[SYS_lwp_sema_trywait
] = "lwp_sema_trywait";
1895 #if defined(SYS_fstatvfs64)
1896 syscall_table
[SYS_fstatvfs64
] = "fstatvfs64";
1898 #if defined(SYS_statvfs64)
1899 syscall_table
[SYS_statvfs64
] = "statvfs64";
1901 #if defined(SYS_ftruncate64)
1902 syscall_table
[SYS_ftruncate64
] = "ftruncate64";
1904 #if defined(SYS_truncate64)
1905 syscall_table
[SYS_truncate64
] = "truncate64";
1907 #if defined(SYS_getrlimit64)
1908 syscall_table
[SYS_getrlimit64
] = "getrlimit64";
1910 #if defined(SYS_setrlimit64)
1911 syscall_table
[SYS_setrlimit64
] = "setrlimit64";
1913 #if defined(SYS_lseek64)
1914 syscall_table
[SYS_lseek64
] = "lseek64";
1916 #if defined(SYS_mmap64)
1917 syscall_table
[SYS_mmap64
] = "mmap64";
1919 #if defined(SYS_pread64)
1920 syscall_table
[SYS_pread64
] = "pread64";
1922 #if defined(SYS_creat64)
1923 syscall_table
[SYS_creat64
] = "creat64";
1925 #if defined(SYS_dshmsys)
1926 syscall_table
[SYS_dshmsys
] = "dshmsys";
1928 #if defined(SYS_invlpg)
1929 syscall_table
[SYS_invlpg
] = "invlpg";
1931 #if defined(SYS_cg_ids)
1932 syscall_table
[SYS_cg_ids
] = "cg_ids";
1934 #if defined(SYS_cg_processors)
1935 syscall_table
[SYS_cg_processors
] = "cg_processors";
1937 #if defined(SYS_cg_info)
1938 syscall_table
[SYS_cg_info
] = "cg_info";
1940 #if defined(SYS_cg_bind)
1941 syscall_table
[SYS_cg_bind
] = "cg_bind";
1943 #if defined(SYS_cg_current)
1944 syscall_table
[SYS_cg_current
] = "cg_current";
1946 #if defined(SYS_cg_memloc)
1947 syscall_table
[SYS_cg_memloc
] = "cg_memloc";
1955 procfs_kill_inferior - kill any current inferior
1959 void procfs_kill_inferior (void)
1963 Kill any current inferior.
1967 Kills even attached inferiors. Presumably the user has already
1968 been prompted that the inferior is an attached one rather than
1969 one started by gdb. (FIXME?)
1974 procfs_kill_inferior ()
1976 target_mourn_inferior ();
1983 unconditionally_kill_inferior - terminate the inferior
1987 static void unconditionally_kill_inferior (struct procinfo *)
1991 Kill the specified inferior.
1995 A possibly useful enhancement would be to first try sending
1996 the inferior a terminate signal, politely asking it to commit
1997 suicide, before we murder it (we could call that
1998 politely_kill_inferior()).
2003 unconditionally_kill_inferior (pi
)
2004 struct procinfo
*pi
;
2007 struct proc_ctl pctl
;
2009 ppid
= pi
->prstatus
.pr_ppid
;
2011 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
2012 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
2013 before the PIOCKILL, otherwise it might generate a corrupted core
2014 file for the inferior. */
2015 ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
);
2017 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
2018 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
2019 to kill the inferior, otherwise it might remain stopped with a
2021 We do not check the result of the PIOCSSIG, the inferior might have
2024 struct siginfo newsiginfo
;
2026 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
2027 newsiginfo
.si_signo
= SIGKILL
;
2028 newsiginfo
.si_code
= 0;
2029 newsiginfo
.si_errno
= 0;
2030 newsiginfo
.si_pid
= getpid ();
2031 newsiginfo
.si_uid
= getuid ();
2032 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
2034 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
2035 procfs_write_pckill (pi
);
2036 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
2038 close_proc_file (pi
);
2040 /* Only wait() for our direct children. Our grandchildren zombies are killed
2041 by the death of their parents. */
2043 if (ppid
== getpid ())
2051 procfs_xfer_memory -- copy data to or from inferior memory space
2055 int procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
2056 int dowrite, struct target_ops target)
2060 Copy LEN bytes to/from inferior's memory starting at MEMADDR
2061 from/to debugger memory starting at MYADDR. Copy from inferior
2062 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
2064 Returns the length copied, which is either the LEN argument or
2065 zero. This xfer function does not do partial moves, since procfs_ops
2066 doesn't allow memory operations to cross below us in the target stack
2071 The /proc interface makes this an almost trivial task.
2075 procfs_xfer_memory (memaddr
, myaddr
, len
, dowrite
, target
)
2080 struct target_ops
*target
; /* ignored */
2083 struct procinfo
*pi
;
2085 pi
= current_procinfo
;
2087 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
2091 nbytes
= write (pi
->as_fd
, myaddr
, len
);
2095 nbytes
= read (pi
->as_fd
, myaddr
, len
);
2109 procfs_store_registers -- copy register values back to inferior
2113 void procfs_store_registers (int regno)
2117 Store our current register values back into the inferior. If
2118 REGNO is -1 then store all the register, otherwise store just
2119 the value specified by REGNO.
2123 If we are storing only a single register, we first have to get all
2124 the current values from the process, overwrite the desired register
2125 in the gregset with the one we want from gdb's registers, and then
2126 send the whole set back to the process. For writing all the
2127 registers, all we have to do is generate the gregset and send it to
2130 Also note that the process has to be stopped on an event of interest
2131 for this to work, which basically means that it has to have been
2132 run under the control of one of the other /proc ioctl calls and not
2133 ptrace. Since we don't use ptrace anyway, we don't worry about this
2134 fine point, but it is worth noting for future reference.
2136 Gdb is confused about what this function is supposed to return.
2137 Some versions return a value, others return nothing. Some are
2138 declared to return a value and actually return nothing. Gdb ignores
2139 anything returned. (FIXME)
2144 procfs_store_registers (regno
)
2147 struct procinfo
*pi
;
2148 #ifdef PROCFS_USE_READ_WRITE
2149 struct greg_ctl greg
;
2150 struct fpreg_ctl fpreg
;
2153 pi
= current_procinfo
;
2155 #ifdef PROCFS_USE_READ_WRITE
2158 procfs_read_status (pi
);
2159 memcpy ((char *) &greg
.gregset
,
2160 (char *) &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
,
2161 sizeof (gdb_gregset_t
));
2163 fill_gregset (&greg
.gregset
, regno
);
2165 write (pi
->ctl_fd
, &greg
, sizeof (greg
));
2166 #else /* PROCFS_USE_READ_WRITE */
2169 ioctl (pi
->ctl_fd
, PIOCGREG
, &pi
->gregset
.gregset
);
2171 fill_gregset (&pi
->gregset
.gregset
, regno
);
2172 ioctl (pi
->ctl_fd
, PIOCSREG
, &pi
->gregset
.gregset
);
2173 #endif /* PROCFS_USE_READ_WRITE */
2175 #if defined (FP0_REGNUM)
2177 /* Now repeat everything using the floating point register set, if the
2178 target has floating point hardware. Since we ignore the returned value,
2179 we'll never know whether it worked or not anyway. */
2181 #ifdef PROCFS_USE_READ_WRITE
2184 procfs_read_status (pi
);
2185 memcpy ((char *) &fpreg
.fpregset
,
2186 (char *) &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
,
2187 sizeof (gdb_fpregset_t
));
2189 fill_fpregset (&fpreg
.fpregset
, regno
);
2190 fpreg
.cmd
= PCSFPREG
;
2191 write (pi
->ctl_fd
, &fpreg
, sizeof (fpreg
));
2192 #else /* PROCFS_USE_READ_WRITE */
2195 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
.fpregset
);
2197 fill_fpregset (&pi
->fpregset
.fpregset
, regno
);
2198 ioctl (pi
->ctl_fd
, PIOCSFPREG
, &pi
->fpregset
.fpregset
);
2199 #endif /* PROCFS_USE_READ_WRITE */
2201 #endif /* FP0_REGNUM */
2209 init_procinfo - setup a procinfo struct and connect it to a process
2213 struct procinfo * init_procinfo (int pid)
2217 Allocate a procinfo structure, open the /proc file and then set up the
2218 set of signals and faults that are to be traced. Returns a pointer to
2219 the new procinfo structure.
2223 If proc_init_failed ever gets called, control returns to the command
2224 processing loop via the standard error handling code.
2228 static struct procinfo
*
2229 init_procinfo (pid
, kill
)
2233 struct procinfo
*pi
= (struct procinfo
*)
2234 xmalloc (sizeof (struct procinfo
));
2235 struct sig_ctl sctl
;
2236 struct flt_ctl fctl
;
2238 memset ((char *) pi
, 0, sizeof (*pi
));
2239 if (!open_proc_file (pid
, pi
, O_RDWR
, 1))
2240 proc_init_failed (pi
, "can't open process file", kill
);
2242 /* open_proc_file may modify pid. */
2246 /* Add new process to process info list */
2248 pi
->next
= procinfo_list
;
2251 add_fd (pi
); /* Add to list for poll/select */
2253 /* Remember some things about the inferior that we will, or might, change
2254 so that we can restore them when we detach. */
2256 memcpy ((char *) &pi
->saved_trace
.sigset
,
2257 (char *) &pi
->prstatus
.pr_sigtrace
, sizeof (sigset_t
));
2258 memcpy ((char *) &pi
->saved_fltset
.fltset
,
2259 (char *) &pi
->prstatus
.pr_flttrace
, sizeof (fltset_t
));
2260 memcpy ((char *) &pi
->saved_entryset
.sysset
,
2261 (char *) &pi
->prstatus
.pr_sysentry
, sizeof (sysset_t
));
2262 memcpy ((char *) &pi
->saved_exitset
.sysset
,
2263 (char *) &pi
->prstatus
.pr_sysexit
, sizeof (sysset_t
));
2265 /* Set up trace and fault sets, as gdb expects them. */
2267 prfillset (&sctl
.sigset
);
2268 notice_signals (pi
, &sctl
);
2269 prfillset (&fctl
.fltset
);
2270 prdelset (&fctl
.fltset
, FLTPAGE
);
2272 #else /* ! UNIXWARE */
2273 ioctl (pi
->ctl_fd
, PIOCGTRACE
, &pi
->saved_trace
.sigset
);
2274 ioctl (pi
->ctl_fd
, PIOCGHOLD
, &pi
->saved_sighold
.sigset
);
2275 ioctl (pi
->ctl_fd
, PIOCGFAULT
, &pi
->saved_fltset
.fltset
);
2276 ioctl (pi
->ctl_fd
, PIOCGENTRY
, &pi
->saved_entryset
.sysset
);
2277 ioctl (pi
->ctl_fd
, PIOCGEXIT
, &pi
->saved_exitset
.sysset
);
2279 /* Set up trace and fault sets, as gdb expects them. */
2281 memset ((char *) &pi
->prrun
, 0, sizeof (pi
->prrun
));
2282 prfillset (&pi
->prrun
.pr_trace
);
2283 procfs_notice_signals (pid
);
2284 prfillset (&pi
->prrun
.pr_fault
);
2285 prdelset (&pi
->prrun
.pr_fault
, FLTPAGE
);
2286 #ifdef PROCFS_DONT_TRACE_FAULTS
2287 premptyset (&pi
->prrun
.pr_fault
);
2289 #endif /* UNIXWARE */
2291 if (!procfs_read_status (pi
))
2292 proc_init_failed (pi
, "procfs_read_status failed", kill
);
2301 create_procinfo - initialize access to a /proc entry
2305 struct procinfo * create_procinfo (int pid)
2309 Allocate a procinfo structure, open the /proc file and then set up the
2310 set of signals and faults that are to be traced. Returns a pointer to
2311 the new procinfo structure.
2315 If proc_init_failed ever gets called, control returns to the command
2316 processing loop via the standard error handling code.
2320 static struct procinfo
*
2321 create_procinfo (pid
)
2324 struct procinfo
*pi
;
2325 struct sig_ctl sctl
;
2326 struct flt_ctl fctl
;
2328 pi
= find_procinfo (pid
, 1);
2330 return pi
; /* All done! It already exists */
2332 pi
= init_procinfo (pid
, 1);
2335 /* A bug in Solaris (2.5 at least) causes PIOCWSTOP to hang on LWPs that are
2336 already stopped, even if they all have PR_ASYNC set. */
2337 if (!(pi
->prstatus
.pr_flags
& PR_STOPPED
))
2339 if (!procfs_write_pcwstop (pi
))
2340 proc_init_failed (pi
, "procfs_write_pcwstop failed", 1);
2342 #ifdef PROCFS_USE_READ_WRITE
2343 fctl
.cmd
= PCSFAULT
;
2344 if (write (pi
->ctl_fd
, (char *) &fctl
, sizeof (struct flt_ctl
)) < 0)
2345 proc_init_failed (pi
, "PCSFAULT failed", 1);
2347 if (ioctl (pi
->ctl_fd
, PIOCSFAULT
, &pi
->prrun
.pr_fault
) < 0)
2348 proc_init_failed (pi
, "PIOCSFAULT failed", 1);
2358 procfs_exit_handler - handle entry into the _exit syscall
2362 int procfs_exit_handler (pi, syscall_num, why, rtnvalp, statvalp)
2366 This routine is called when an inferior process enters the _exit()
2367 system call. It continues the process, and then collects the exit
2368 status and pid which are returned in *statvalp and *rtnvalp. After
2369 that it returns non-zero to indicate that procfs_wait should wake up.
2372 There is probably a better way to do this.
2377 procfs_exit_handler (pi
, syscall_num
, why
, rtnvalp
, statvalp
)
2378 struct procinfo
*pi
;
2384 struct procinfo
*temp_pi
, *next_pi
;
2385 struct proc_ctl pctl
;
2389 pctl
.data
= PRCFAULT
;
2391 pi
->prrun
.pr_flags
= PRCFAULT
;
2394 #ifdef PROCFS_USE_READ_WRITE
2395 if (write (pi
->ctl_fd
, (char *) &pctl
, sizeof (struct proc_ctl
)) < 0)
2397 if (ioctl (pi
->ctl_fd
, PIOCRUN
, &pi
->prrun
) != 0)
2399 perror_with_name (pi
->pathname
);
2403 /* Claim it exited (don't call wait). */
2405 printf_filtered ("(attached process has exited)\n");
2407 *rtnvalp
= inferior_pid
;
2411 *rtnvalp
= wait (statvalp
);
2416 /* Close ALL open proc file handles,
2417 except the one that called SYS_exit. */
2418 for (temp_pi
= procinfo_list
; temp_pi
; temp_pi
= next_pi
)
2420 next_pi
= temp_pi
->next
;
2422 continue; /* Handled below */
2423 close_proc_file (temp_pi
);
2432 procfs_exec_handler - handle exit from the exec family of syscalls
2436 int procfs_exec_handler (pi, syscall_num, why, rtnvalp, statvalp)
2440 This routine is called when an inferior process is about to finish any
2441 of the exec() family of system calls. It pretends that we got a
2442 SIGTRAP (for compatibility with ptrace behavior), and returns non-zero
2443 to tell procfs_wait to wake up.
2446 This need for compatibility with ptrace is questionable. In the
2447 future, it shouldn't be necessary.
2452 procfs_exec_handler (pi
, syscall_num
, why
, rtnvalp
, statvalp
)
2453 struct procinfo
*pi
;
2459 *statvalp
= (SIGTRAP
<< 8) | 0177;
2464 #if defined(SYS_sproc) && !defined(UNIXWARE)
2465 /* IRIX lwp creation system call */
2471 procfs_sproc_handler - handle exit from the sproc syscall
2475 int procfs_sproc_handler (pi, syscall_num, why, rtnvalp, statvalp)
2479 This routine is called when an inferior process is about to finish an
2480 sproc() system call. This is the system call that IRIX uses to create
2481 a lightweight process. When the target process gets this event, we can
2482 look at rval1 to find the new child processes ID, and create a new
2483 procinfo struct from that.
2485 After that, it pretends that we got a SIGTRAP, and returns non-zero
2486 to tell procfs_wait to wake up. Subsequently, wait_for_inferior gets
2487 woken up, sees the new process and continues it.
2490 We actually never see the child exiting from sproc because we will
2491 shortly stop the child with PIOCSTOP, which is then registered as the
2496 procfs_sproc_handler (pi
, syscall_num
, why
, rtnvalp
, statvalp
)
2497 struct procinfo
*pi
;
2503 /* We've just detected the completion of an sproc system call. Now we need to
2504 setup a procinfo struct for this thread, and notify the thread system of the
2507 /* If sproc failed, then nothing interesting happened. Continue the process
2508 and go back to sleep. */
2510 if (pi
->prstatus
.pr_errno
!= 0)
2512 pi
->prrun
.pr_flags
&= PRSTEP
;
2513 pi
->prrun
.pr_flags
|= PRCFAULT
;
2515 if (ioctl (pi
->ctl_fd
, PIOCRUN
, &pi
->prrun
) != 0)
2516 perror_with_name (pi
->pathname
);
2521 /* At this point, the new thread is stopped at it's first instruction, and
2522 the parent is stopped at the exit from sproc. */
2524 /* Notify the caller of the arrival of a new thread. */
2525 create_procinfo (pi
->prstatus
.pr_rval1
);
2527 *rtnvalp
= pi
->prstatus
.pr_rval1
;
2528 *statvalp
= (SIGTRAP
<< 8) | 0177;
2537 procfs_fork_handler - handle exit from the fork syscall
2541 int procfs_fork_handler (pi, syscall_num, why, rtnvalp, statvalp)
2545 This routine is called when an inferior process is about to finish a
2546 fork() system call. We will open up the new process, and then close
2547 it, which releases it from the clutches of the debugger.
2549 After that, we continue the target process as though nothing had
2553 This is necessary for IRIX because we have to set PR_FORK in order
2554 to catch the creation of lwps (via sproc()). When an actual fork
2555 occurs, it becomes necessary to reset the forks debugger flags and
2556 continue it because we can't hack multiple processes yet.
2560 procfs_fork_handler (pi
, syscall_num
, why
, rtnvalp
, statvalp
)
2561 struct procinfo
*pi
;
2567 struct procinfo
*pitemp
;
2569 /* At this point, we've detected the completion of a fork (or vfork) call in
2570 our child. The grandchild is also stopped because we set inherit-on-fork
2571 earlier. (Note that nobody has the grandchilds' /proc file open at this
2572 point.) We will release the grandchild from the debugger by opening it's
2573 /proc file and then closing it. Since run-on-last-close is set, the
2574 grandchild continues on its' merry way. */
2577 pitemp
= create_procinfo (pi
->prstatus
.pr_rval1
);
2579 close_proc_file (pitemp
);
2581 if (ioctl (pi
->ctl_fd
, PIOCRUN
, &pi
->prrun
) != 0)
2582 perror_with_name (pi
->pathname
);
2586 #endif /* SYS_sproc && !UNIXWARE */
2592 procfs_set_inferior_syscall_traps - setup the syscall traps
2596 void procfs_set_inferior_syscall_traps (struct procinfo *pip)
2600 Called for each "procinfo" (process, thread, or LWP) in the
2601 inferior, to register for notification of and handlers for
2602 syscall traps in the inferior.
2607 procfs_set_inferior_syscall_traps (pip
)
2608 struct procinfo
*pip
;
2610 procfs_set_syscall_trap (pip
, SYS_exit
, PROCFS_SYSCALL_ENTRY
,
2611 procfs_exit_handler
);
2613 #ifndef PRFS_STOPEXEC
2615 procfs_set_syscall_trap (pip
, SYS_exec
, PROCFS_SYSCALL_EXIT
,
2616 procfs_exec_handler
);
2619 procfs_set_syscall_trap (pip
, SYS_execv
, PROCFS_SYSCALL_EXIT
,
2620 procfs_exec_handler
);
2623 procfs_set_syscall_trap (pip
, SYS_execve
, PROCFS_SYSCALL_EXIT
,
2624 procfs_exec_handler
);
2626 #endif /* PRFS_STOPEXEC */
2628 /* Setup traps on exit from sproc() */
2631 procfs_set_syscall_trap (pip
, SYS_sproc
, PROCFS_SYSCALL_EXIT
,
2632 procfs_sproc_handler
);
2633 procfs_set_syscall_trap (pip
, SYS_fork
, PROCFS_SYSCALL_EXIT
,
2634 procfs_fork_handler
);
2636 procfs_set_syscall_trap (pip
, SYS_vfork
, PROCFS_SYSCALL_EXIT
,
2637 procfs_fork_handler
);
2639 /* Turn on inherit-on-fork flag so that all children of the target process
2640 start with tracing flags set. This allows us to trap lwp creation. Note
2641 that we also have to trap on fork and vfork in order to disable all tracing
2642 in the targets child processes. */
2644 modify_inherit_on_fork_flag (pip
->ctl_fd
, 1);
2647 #ifdef SYS_lwp_create
2648 procfs_set_syscall_trap (pip
, SYS_lwp_create
, PROCFS_SYSCALL_EXIT
,
2649 procfs_lwp_creation_handler
);
2657 procfs_init_inferior - initialize target vector and access to a
2662 void procfs_init_inferior (int pid)
2666 When gdb starts an inferior, this function is called in the parent
2667 process immediately after the fork. It waits for the child to stop
2668 on the return from the exec system call (the child itself takes care
2669 of ensuring that this is set up), then sets up the set of signals
2670 and faults that are to be traced. Returns the pid, which may have had
2671 the thread-id added to it.
2675 If proc_init_failed ever gets called, control returns to the command
2676 processing loop via the standard error handling code.
2681 procfs_init_inferior (pid
)
2684 struct procinfo
*pip
;
2686 push_target (&procfs_ops
);
2688 pip
= create_procinfo (pid
);
2690 procfs_set_inferior_syscall_traps (pip
);
2692 /* create_procinfo may change the pid, so we have to update inferior_pid
2693 here before calling other gdb routines that need the right pid. */
2698 add_thread (pip
->pid
); /* Setup initial thread */
2700 #ifdef START_INFERIOR_TRAPS_EXPECTED
2701 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
2703 /* One trap to exec the shell, one to exec the program being debugged. */
2704 startup_inferior (2);
2712 procfs_notice_signals
2716 static void procfs_notice_signals (int pid);
2720 When the user changes the state of gdb's signal handling via the
2721 "handle" command, this function gets called to see if any change
2722 in the /proc interface is required. It is also called internally
2723 by other /proc interface functions to initialize the state of
2724 the traced signal set.
2726 One thing it does is that signals for which the state is "nostop",
2727 "noprint", and "pass", have their trace bits reset in the pr_trace
2728 field, so that they are no longer traced. This allows them to be
2729 delivered directly to the inferior without the debugger ever being
2734 procfs_notice_signals (pid
)
2737 struct procinfo
*pi
;
2738 struct sig_ctl sctl
;
2740 pi
= find_procinfo (pid
, 0);
2742 #ifndef HAVE_PRRUN_T
2743 premptyset (&sctl
.sigset
);
2745 sctl
.sigset
= pi
->prrun
.pr_trace
;
2748 notice_signals (pi
, &sctl
);
2751 pi
->prrun
.pr_trace
= sctl
.sigset
;
2756 notice_signals (pi
, sctl
)
2757 struct procinfo
*pi
;
2758 struct sig_ctl
*sctl
;
2762 for (signo
= 0; signo
< NSIG
; signo
++)
2764 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
2765 signal_print_state (target_signal_from_host (signo
)) == 0 &&
2766 signal_pass_state (target_signal_from_host (signo
)) == 1)
2768 prdelset (&sctl
->sigset
, signo
);
2772 praddset (&sctl
->sigset
, signo
);
2775 #ifdef PROCFS_USE_READ_WRITE
2776 sctl
->cmd
= PCSTRACE
;
2777 if (write (pi
->ctl_fd
, (char *) sctl
, sizeof (struct sig_ctl
)) < 0)
2779 if (ioctl (pi
->ctl_fd
, PIOCSTRACE
, &sctl
->sigset
))
2782 print_sys_errmsg ("PIOCSTRACE failed", errno
);
2790 proc_set_exec_trap -- arrange for exec'd child to halt at startup
2794 void proc_set_exec_trap (void)
2798 This function is called in the child process when starting up
2799 an inferior, prior to doing the exec of the actual inferior.
2800 It sets the child process's exitset to make exit from the exec
2801 system call an event of interest to stop on, and then simply
2802 returns. The child does the exec, the system call returns, and
2803 the child stops at the first instruction, ready for the gdb
2804 parent process to take control of it.
2808 We need to use all local variables since the child may be sharing
2809 it's data space with the parent, if vfork was used rather than
2812 Also note that we want to turn off the inherit-on-fork flag in
2813 the child process so that any grand-children start with all
2814 tracing flags cleared.
2818 proc_set_exec_trap ()
2820 struct sys_ctl exitset
;
2821 struct sys_ctl entryset
;
2822 char procname
[MAX_PROC_NAME_SIZE
];
2825 sprintf (procname
, CTL_PROC_NAME_FMT
, getpid ());
2827 if ((fd
= open (procname
, O_WRONLY
)) < 0)
2829 if ((fd
= open (procname
, O_RDWR
)) < 0)
2833 gdb_flush (gdb_stderr
);
2836 premptyset (&exitset
.sysset
);
2837 premptyset (&entryset
.sysset
);
2839 #ifdef PRFS_STOPEXEC
2840 /* Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
2841 exits from exec system calls because of the user level loader. */
2845 if (ioctl (fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
2848 gdb_flush (gdb_stderr
);
2851 prfs_flags
|= PRFS_STOPEXEC
;
2852 if (ioctl (fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
2855 gdb_flush (gdb_stderr
);
2859 #else /* PRFS_STOPEXEC */
2861 Not all systems with /proc have all the exec* syscalls with the same
2862 names. On the SGI, for example, there is no SYS_exec, but there
2863 *is* a SYS_execv. So, we try to account for that. */
2866 praddset (&exitset
.sysset
, SYS_exec
);
2869 praddset (&exitset
.sysset
, SYS_execve
);
2872 praddset (&exitset
.sysset
, SYS_execv
);
2875 #ifdef PROCFS_USE_READ_WRITE
2876 exitset
.cmd
= PCSEXIT
;
2877 if (write (fd
, (char *) &exitset
, sizeof (struct sys_ctl
)) < 0)
2879 if (ioctl (fd
, PIOCSEXIT
, &exitset
.sysset
) < 0)
2883 gdb_flush (gdb_stderr
);
2886 #endif /* PRFS_STOPEXEC */
2888 praddset (&entryset
.sysset
, SYS_exit
);
2890 #ifdef PROCFS_USE_READ_WRITE
2891 entryset
.cmd
= PCSENTRY
;
2892 if (write (fd
, (char *) &entryset
, sizeof (struct sys_ctl
)) < 0)
2894 if (ioctl (fd
, PIOCSENTRY
, &entryset
.sysset
) < 0)
2898 gdb_flush (gdb_stderr
);
2902 /* Turn off inherit-on-fork flag so that all grand-children of gdb
2903 start with tracing flags cleared. */
2905 modify_inherit_on_fork_flag (fd
, 0);
2907 /* Turn on run-on-last-close flag so that this process will not hang
2908 if GDB goes away for some reason. */
2910 modify_run_on_last_close_flag (fd
, 1);
2912 #ifndef UNIXWARE /* since this is a solaris-ism, we don't want it */
2913 /* NOTE: revisit when doing thread support for UW */
2917 struct proc_ctl pctl
;
2919 /* Solaris needs this to make procfs treat all threads seperately. Without
2920 this, all threads halt whenever something happens to any thread. Since
2921 GDB wants to control all this itself, it needs to set PR_ASYNC. */
2923 pr_flags
= PR_ASYNC
;
2924 #ifdef PROCFS_USE_READ_WRITE
2926 pctl
.data
= PR_FORK
| PR_ASYNC
;
2927 write (fd
, (char *) &pctl
, sizeof (struct proc_ctl
));
2929 ioctl (fd
, PIOCSET
, &pr_flags
);
2932 #endif /* PR_ASYNC */
2933 #endif /* !UNIXWARE */
2940 proc_iterate_over_mappings -- call function for every mapped space
2944 int proc_iterate_over_mappings (int (*func)())
2948 Given a pointer to a function, call that function for every
2949 mapped address space, passing it an open file descriptor for
2950 the file corresponding to that mapped address space (if any)
2951 and the base address of the mapped space. Quit when we hit
2952 the end of the mappings or the function returns nonzero.
2957 proc_iterate_over_mappings (func
)
2958 int (*func
) PARAMS ((int, CORE_ADDR
));
2965 struct procinfo
*pi
;
2968 pi
= current_procinfo
;
2970 if (fstat (pi
->map_fd
, &sbuf
) < 0)
2973 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
2974 prmaps
= (prmap_t
*) alloca (nmap
* sizeof (prmap_t
));
2975 if ((lseek (pi
->map_fd
, 0, SEEK_SET
) == 0) &&
2976 (read (pi
->map_fd
, (char *) prmaps
, nmap
* sizeof (prmap_t
)) ==
2977 (nmap
* sizeof (prmap_t
))))
2980 for (prmap
= prmaps
; i
< nmap
&& funcstat
== 0; ++prmap
, ++i
)
2982 char name
[sizeof ("/proc/1234567890/object") +
2983 sizeof (prmap
->pr_mapname
)];
2984 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, prmap
->pr_mapname
);
2985 if ((fd
= open (name
, O_RDONLY
)) == -1)
2990 funcstat
= (*func
) (fd
, (CORE_ADDR
) prmap
->pr_vaddr
);
2996 #else /* UNIXWARE */
2998 proc_iterate_over_mappings (func
)
2999 int (*func
) PARAMS ((int, CORE_ADDR
));
3004 struct prmap
*prmaps
;
3005 struct prmap
*prmap
;
3006 struct procinfo
*pi
;
3008 pi
= current_procinfo
;
3010 if (ioctl (pi
->map_fd
, PIOCNMAP
, &nmap
) == 0)
3012 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
3013 if (ioctl (pi
->map_fd
, PIOCMAP
, prmaps
) == 0)
3015 for (prmap
= prmaps
; prmap
->pr_size
&& funcstat
== 0; ++prmap
)
3017 fd
= proc_address_to_fd (pi
, (CORE_ADDR
) prmap
->pr_vaddr
, 0);
3018 funcstat
= (*func
) (fd
, (CORE_ADDR
) prmap
->pr_vaddr
);
3025 #endif /* UNIXWARE */
3027 #if 0 /* Currently unused */
3032 proc_base_address -- find base address for segment containing address
3036 CORE_ADDR proc_base_address (CORE_ADDR addr)
3040 Given an address of a location in the inferior, find and return
3041 the base address of the mapped segment containing that address.
3043 This is used for example, by the shared library support code,
3044 where we have the pc value for some location in the shared library
3045 where we are stopped, and need to know the base address of the
3046 segment containing that address.
3050 proc_base_address (addr
)
3054 struct prmap
*prmaps
;
3055 struct prmap
*prmap
;
3056 CORE_ADDR baseaddr
= 0;
3057 struct procinfo
*pi
;
3059 pi
= current_procinfo
;
3061 if (ioctl (pi
->map_fd
, PIOCNMAP
, &nmap
) == 0)
3063 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
3064 if (ioctl (pi
->map_fd
, PIOCMAP
, prmaps
) == 0)
3066 for (prmap
= prmaps
; prmap
->pr_size
; ++prmap
)
3068 if ((prmap
->pr_vaddr
<= (caddr_t
) addr
) &&
3069 (prmap
->pr_vaddr
+ prmap
->pr_size
> (caddr_t
) addr
))
3071 baseaddr
= (CORE_ADDR
) prmap
->pr_vaddr
;
3087 proc_address_to_fd -- return open fd for file mapped to address
3091 int proc_address_to_fd (struct procinfo *pi, CORE_ADDR addr, complain)
3095 Given an address in the current inferior's address space, use the
3096 /proc interface to find an open file descriptor for the file that
3097 this address was mapped in from. Return -1 if there is no current
3098 inferior. Print a warning message if there is an inferior but
3099 the address corresponds to no file (IE a bogus address).
3104 proc_address_to_fd (pi
, addr
, complain
)
3105 struct procinfo
*pi
;
3111 if ((fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, (caddr_t
*) & addr
)) < 0)
3115 print_sys_errmsg (pi
->pathname
, errno
);
3116 warning ("can't find mapped file for address 0x%x", addr
);
3121 #endif /* !UNIXWARE */
3123 /* Attach to process PID, then initialize for debugging it
3124 and wait for the trace-trap that results from attaching. */
3127 procfs_attach (args
, from_tty
)
3135 error_no_arg ("process-id to attach");
3139 if (pid
== getpid ()) /* Trying to masturbate? */
3140 error ("I refuse to debug myself!");
3144 exec_file
= (char *) get_exec_file (0);
3147 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file
, target_pid_to_str (pid
));
3149 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid
));
3151 gdb_flush (gdb_stdout
);
3154 inferior_pid
= pid
= do_attach (pid
);
3155 push_target (&procfs_ops
);
3159 /* Take a program previously attached to and detaches it.
3160 The program resumes execution and will no longer stop
3161 on signals, etc. We'd better not have left any breakpoints
3162 in the program or it'll die when it hits one. For this
3163 to work, it may be necessary for the process to have been
3164 previously attached. It *might* work if the program was
3165 started via the normal ptrace (PTRACE_TRACEME). */
3168 procfs_detach (args
, from_tty
)
3176 char *exec_file
= get_exec_file (0);
3179 printf_unfiltered ("Detaching from program: %s %s\n",
3180 exec_file
, target_pid_to_str (inferior_pid
));
3181 gdb_flush (gdb_stdout
);
3184 siggnal
= atoi (args
);
3186 do_detach (siggnal
);
3188 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3191 /* Get ready to modify the registers array. On machines which store
3192 individual registers, this doesn't need to do anything. On machines
3193 which store all the registers in one fell swoop, this makes sure
3194 that registers contains all the registers from the program being
3198 procfs_prepare_to_store ()
3200 #ifdef CHILD_PREPARE_TO_STORE
3201 CHILD_PREPARE_TO_STORE ();
3205 /* Print status information about what we're accessing. */
3208 procfs_files_info (ignore
)
3209 struct target_ops
*ignore
;
3211 printf_unfiltered ("\tUsing the running image of %s %s via /proc.\n",
3212 attach_flag
? "attached" : "child", target_pid_to_str (inferior_pid
));
3217 procfs_open (arg
, from_tty
)
3221 error ("Use the \"run\" command to start a Unix child process.");
3228 do_attach -- attach to an already existing process
3232 int do_attach (int pid)
3236 Attach to an already existing process with the specified process
3237 id. If the process is not already stopped, query whether to
3242 The option of stopping at attach time is specific to the /proc
3243 versions of gdb. Versions using ptrace force the attachee
3244 to stop. (I have changed this version to do so, too. All you
3245 have to do is "continue" to make it go on. -- gnu@cygnus.com)
3253 struct procinfo
*pi
;
3254 struct sig_ctl sctl
;
3255 struct flt_ctl fctl
;
3258 pi
= init_procinfo (pid
, 0);
3261 nlwp
= pi
->prstatus
.pr_nlwp
;
3262 lwps
= alloca ((2 * nlwp
+ 2) * sizeof (id_t
));
3264 if (ioctl (pi
->ctl_fd
, PIOCLWPIDS
, lwps
))
3266 print_sys_errmsg (pi
->pathname
, errno
);
3267 error ("PIOCLWPIDS failed");
3269 #else /* PIOCLWPIDS */
3271 lwps
= alloca ((2 * nlwp
+ 2) * sizeof *lwps
);
3274 for (; nlwp
> 0; nlwp
--, lwps
++)
3276 /* First one has already been created above. */
3277 if ((pi
= find_procinfo ((*lwps
<< 16) | pid
, 1)) == 0)
3278 pi
= init_procinfo ((*lwps
<< 16) | pid
, 0);
3280 if (THE_PR_LWP (pi
->prstatus
).pr_flags
& (PR_STOPPED
| PR_ISTOP
))
3282 pi
->was_stopped
= 1;
3286 pi
->was_stopped
= 0;
3287 if (1 || query ("Process is currently running, stop it? "))
3290 /* Make it run again when we close it. */
3291 modify_run_on_last_close_flag (pi
->ctl_fd
, 1);
3292 #ifdef PROCFS_USE_READ_WRITE
3294 if (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (long)) < 0)
3296 if (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) < 0)
3299 print_sys_errmsg (pi
->pathname
, errno
);
3300 close_proc_file (pi
);
3301 error ("PIOCSTOP failed");
3304 if (!procfs_read_status (pi
))
3306 print_sys_errmsg (pi
->pathname
, errno
);
3307 close_proc_file (pi
);
3308 error ("procfs_read_status failed");
3311 pi
->nopass_next_sigstop
= 1;
3315 printf_unfiltered ("Ok, gdb will wait for %s to stop.\n",
3316 target_pid_to_str (pi
->pid
));
3320 #ifdef PROCFS_USE_READ_WRITE
3321 fctl
.cmd
= PCSFAULT
;
3322 if (write (pi
->ctl_fd
, (char *) &fctl
, sizeof (struct flt_ctl
)) < 0)
3323 print_sys_errmsg ("PCSFAULT failed", errno
);
3324 #else /* PROCFS_USE_READ_WRITE */
3325 if (ioctl (pi
->ctl_fd
, PIOCSFAULT
, &pi
->prrun
.pr_fault
))
3327 print_sys_errmsg ("PIOCSFAULT failed", errno
);
3329 if (ioctl (pi
->ctl_fd
, PIOCSTRACE
, &pi
->prrun
.pr_trace
))
3331 print_sys_errmsg ("PIOCSTRACE failed", errno
);
3333 add_thread (pi
->pid
);
3334 procfs_set_inferior_syscall_traps (pi
);
3335 #endif /* PROCFS_USE_READ_WRITE */
3345 do_detach -- detach from an attached-to process
3349 void do_detach (int signal)
3353 Detach from the current attachee.
3355 If signal is non-zero, the attachee is started running again and sent
3356 the specified signal.
3358 If signal is zero and the attachee was not already stopped when we
3359 attached to it, then we make it runnable again when we detach.
3361 Otherwise, we query whether or not to make the attachee runnable
3362 again, since we may simply want to leave it in the state it was in
3365 We report any problems, but do not consider them errors, since we
3366 MUST detach even if some things don't seem to go right. This may not
3367 be the ideal situation. (FIXME).
3374 struct procinfo
*pi
;
3376 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
3380 set_proc_siginfo (pi
, signal
);
3382 #ifdef PROCFS_USE_READ_WRITE
3383 pi
->saved_exitset
.cmd
= PCSEXIT
;
3384 if (write (pi
->ctl_fd
, (char *) &pi
->saved_exitset
,
3385 sizeof (struct sys_ctl
)) < 0)
3387 if (ioctl (pi
->ctl_fd
, PIOCSEXIT
, &pi
->saved_exitset
.sysset
) < 0)
3390 print_sys_errmsg (pi
->pathname
, errno
);
3391 printf_unfiltered ("PIOCSEXIT failed.\n");
3393 #ifdef PROCFS_USE_READ_WRITE
3394 pi
->saved_entryset
.cmd
= PCSENTRY
;
3395 if (write (pi
->ctl_fd
, (char *) &pi
->saved_entryset
,
3396 sizeof (struct sys_ctl
)) < 0)
3398 if (ioctl (pi
->ctl_fd
, PIOCSENTRY
, &pi
->saved_entryset
.sysset
) < 0)
3401 print_sys_errmsg (pi
->pathname
, errno
);
3402 printf_unfiltered ("PIOCSENTRY failed.\n");
3404 #ifdef PROCFS_USE_READ_WRITE
3405 pi
->saved_trace
.cmd
= PCSTRACE
;
3406 if (write (pi
->ctl_fd
, (char *) &pi
->saved_trace
,
3407 sizeof (struct sig_ctl
)) < 0)
3409 if (ioctl (pi
->ctl_fd
, PIOCSTRACE
, &pi
->saved_trace
.sigset
) < 0)
3412 print_sys_errmsg (pi
->pathname
, errno
);
3413 printf_unfiltered ("PIOCSTRACE failed.\n");
3416 if (ioctl (pi
->ctl_fd
, PIOCSHOLD
, &pi
->saved_sighold
.sigset
) < 0)
3418 print_sys_errmsg (pi
->pathname
, errno
);
3419 printf_unfiltered ("PIOSCHOLD failed.\n");
3422 #ifdef PROCFS_USE_READ_WRITE
3423 pi
->saved_fltset
.cmd
= PCSFAULT
;
3424 if (write (pi
->ctl_fd
, (char *) &pi
->saved_fltset
,
3425 sizeof (struct flt_ctl
)) < 0)
3427 if (ioctl (pi
->ctl_fd
, PIOCSFAULT
, &pi
->saved_fltset
.fltset
) < 0)
3430 print_sys_errmsg (pi
->pathname
, errno
);
3431 printf_unfiltered ("PIOCSFAULT failed.\n");
3433 if (!procfs_read_status (pi
))
3435 print_sys_errmsg (pi
->pathname
, errno
);
3436 printf_unfiltered ("procfs_read_status failed.\n");
3441 || (THE_PR_LWP (pi
->prstatus
).pr_flags
& (PR_STOPPED
| PR_ISTOP
)))
3444 struct proc_ctl pctl
;
3446 if (signal
|| !pi
->was_stopped
||
3447 query ("Was stopped when attached, make it runnable again? "))
3449 /* Clear any pending signal if we want to detach without
3452 set_proc_siginfo (pi
, signal
);
3454 /* Clear any fault that might have stopped it. */
3455 #ifdef PROCFS_USE_READ_WRITE
3457 if (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (long)) < 0)
3459 if (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0))
3462 print_sys_errmsg (pi
->pathname
, errno
);
3463 printf_unfiltered ("PIOCCFAULT failed.\n");
3466 /* Make it run again when we close it. */
3468 modify_run_on_last_close_flag (pi
->ctl_fd
, 1);
3472 close_proc_file (pi
);
3477 /* emulate wait() as much as possible.
3478 Wait for child to do something. Return pid of child, or -1 in case
3479 of error; store status in *OURSTATUS.
3481 Not sure why we can't
3482 just use wait(), but it seems to have problems when applied to a
3483 process being controlled with the /proc interface.
3485 We have a race problem here with no obvious solution. We need to let
3486 the inferior run until it stops on an event of interest, which means
3487 that we need to use the PIOCWSTOP ioctl. However, we cannot use this
3488 ioctl if the process is already stopped on something that is not an
3489 event of interest, or the call will hang indefinitely. Thus we first
3490 use PIOCSTATUS to see if the process is not stopped. If not, then we
3491 use PIOCWSTOP. But during the window between the two, if the process
3492 stops for any reason that is not an event of interest (such as a job
3493 control signal) then gdb will hang. One possible workaround is to set
3494 an alarm to wake up every minute of so and check to see if the process
3495 is still running, and if so, then reissue the PIOCWSTOP. But this is
3496 a real kludge, so has not been implemented. FIXME: investigate
3499 FIXME: Investigate why wait() seems to have problems with programs
3500 being control by /proc routines. */
3502 procfs_wait (pid
, ourstatus
)
3504 struct target_waitstatus
*ourstatus
;
3511 struct procinfo
*pi
;
3512 struct proc_ctl pctl
;
3516 /* handle all syscall events first, otherwise we might not
3517 notice a thread was created until too late. */
3519 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
3524 if (!(THE_PR_LWP (pi
->prstatus
).pr_flags
& (PR_STOPPED
| PR_ISTOP
)))
3527 why
= THE_PR_LWP (pi
->prstatus
).pr_why
;
3528 what
= THE_PR_LWP (pi
->prstatus
).pr_what
;
3529 if (why
== PR_SYSENTRY
|| why
== PR_SYSEXIT
)
3532 int found_handler
= 0;
3534 for (i
= 0; i
< pi
->num_syscall_handlers
; i
++)
3535 if (pi
->syscall_handlers
[i
].syscall_num
== what
)
3538 pi
->saved_rtnval
= pi
->pid
;
3539 pi
->saved_statval
= 0;
3540 if (!pi
->syscall_handlers
[i
].func
3541 (pi
, what
, why
, &pi
->saved_rtnval
, &pi
->saved_statval
))
3548 if (why
== PR_SYSENTRY
)
3549 error ("PR_SYSENTRY, unhandled system call %d", what
);
3551 error ("PR_SYSEXIT, unhandled system call %d", what
);
3556 /* find a relevant process with an event */
3558 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
3559 if (pi
->had_event
&& (pid
== -1 || pi
->pid
== pid
))
3569 && !(THE_PR_LWP (pi
->prstatus
).pr_flags
& (PR_STOPPED
| PR_ISTOP
)))
3571 if (!procfs_write_pcwstop (pi
))
3578 if (errno
== ENOENT
)
3580 /* XXX Fixme -- what to do if attached? Can't call wait... */
3581 rtnval
= wait (&statval
);
3582 if ((rtnval
) != (PIDGET (inferior_pid
)))
3584 print_sys_errmsg (pi
->pathname
, errno
);
3585 error ("procfs_wait: wait failed, returned %d", rtnval
);
3591 print_sys_errmsg (pi
->pathname
, errno
);
3592 error ("PIOCSTATUS or PIOCWSTOP failed.");
3596 else if (THE_PR_LWP (pi
->prstatus
).pr_flags
& (PR_STOPPED
| PR_ISTOP
))
3599 rtnval
= pi
->prstatus
.pr_pid
;
3603 why
= THE_PR_LWP (pi
->prstatus
).pr_why
;
3604 what
= THE_PR_LWP (pi
->prstatus
).pr_what
;
3609 statval
= (what
<< 8) | 0177;
3613 rtnval
= pi
->saved_rtnval
;
3614 statval
= pi
->saved_statval
;
3617 statval
= (SIGSTOP
<< 8) | 0177;
3620 statval
= (what
<< 8) | 0177;
3627 statval
= (SIGTRAP
<< 8) | 0177;
3632 statval
= (SIGTRAP
<< 8) | 0177;
3635 #ifndef FAULTED_USE_SIGINFO
3636 /* Irix, contrary to the documentation, fills in 0 for si_signo.
3637 Solaris fills in si_signo. I'm not sure about others. */
3640 statval
= (SIGILL
<< 8) | 0177;
3644 statval
= (SIGTRAP
<< 8) | 0177;
3649 statval
= (SIGSEGV
<< 8) | 0177;
3654 statval
= (SIGFPE
<< 8) | 0177;
3656 case FLTPAGE
: /* Recoverable page fault */
3657 #endif /* not FAULTED_USE_SIGINFO */
3659 /* Use the signal which the kernel assigns. This is better than
3660 trying to second-guess it from the fault. In fact, I suspect
3661 that FLTACCESS can be either SIGSEGV or SIGBUS. */
3663 ((THE_PR_LWP (pi
->prstatus
).pr_info
.si_signo
) << 8) | 0177;
3668 error ("PIOCWSTOP, unknown why %d, what %d", why
, what
);
3670 /* Stop all the other threads when any of them stops. */
3673 struct procinfo
*procinfo
, *next_pi
;
3675 for (procinfo
= procinfo_list
; procinfo
; procinfo
= next_pi
)
3677 next_pi
= procinfo
->next
;
3678 if (!procinfo
->had_event
)
3680 #ifdef PROCFS_USE_READ_WRITE
3682 if (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (long)) < 0)
3684 print_sys_errmsg (procinfo
->pathname
, errno
);
3685 error ("PCSTOP failed");
3688 /* A bug in Solaris (2.5) causes us to hang when trying to
3689 stop a stopped process. So, we have to check first in
3690 order to avoid the hang. */
3691 if (!procfs_read_status (procinfo
))
3693 /* The LWP has apparently terminated. */
3695 printf_filtered ("LWP %d doesn't respond.\n",
3696 (procinfo
->pid
>> 16) & 0xffff);
3697 close_proc_file (procinfo
);
3701 if (!(procinfo
->prstatus
.pr_flags
& PR_STOPPED
))
3702 if (ioctl (procinfo
->ctl_fd
, PIOCSTOP
, &procinfo
->prstatus
)
3705 print_sys_errmsg (procinfo
->pathname
, errno
);
3706 warning ("PIOCSTOP failed");
3715 error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x",
3716 THE_PR_LWP (pi
->prstatus
).pr_flags
);
3719 store_waitstatus (ourstatus
, statval
);
3721 if (rtnval
== -1) /* No more children to wait for */
3723 warning ("Child process unexpectedly missing");
3724 /* Claim it exited with unknown signal. */
3725 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
3726 ourstatus
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
3730 pi
->had_event
= 0; /* Indicate that we've seen this one */
3738 set_proc_siginfo - set a process's current signal info
3742 void set_proc_siginfo (struct procinfo *pip, int signo);
3746 Given a pointer to a process info struct in PIP and a signal number
3747 in SIGNO, set the process's current signal and its associated signal
3748 information. The signal will be delivered to the process immediately
3749 after execution is resumed, even if it is being held. In addition,
3750 this particular delivery will not cause another PR_SIGNALLED stop
3751 even if the signal is being traced.
3753 If we are not delivering the same signal that the prstatus siginfo
3754 struct contains information about, then synthesize a siginfo struct
3755 to match the signal we are going to deliver, make it of the type
3756 "generated by a user process", and send this synthesized copy. When
3757 used to set the inferior's signal state, this will be required if we
3758 are not currently stopped because of a traced signal, or if we decide
3759 to continue with a different signal.
3761 Note that when continuing the inferior from a stop due to receipt
3762 of a traced signal, we either have set PRCSIG to clear the existing
3763 signal, or we have to call this function to do a PIOCSSIG with either
3764 the existing siginfo struct from pr_info, or one we have synthesized
3765 appropriately for the signal we want to deliver. Otherwise if the
3766 signal is still being traced, the inferior will immediately stop
3769 See siginfo(5) for more details.
3773 set_proc_siginfo (pip
, signo
)
3774 struct procinfo
*pip
;
3777 struct siginfo newsiginfo
;
3778 struct siginfo
*sip
;
3779 struct sigi_ctl sictl
;
3781 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
3782 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
3783 receives a PIOCSSIG with a signal identical to the current signal,
3784 it messes up the current signal. Work around the kernel bug. */
3785 if (signo
== THE_PR_LWP (pip
->prstatus
).pr_cursig
)
3790 if (signo
== THE_PR_LWP (pip
->prstatus
).pr_info
.si_signo
)
3792 memcpy ((char *) &sictl
.siginfo
, (char *) &pip
->prstatus
.pr_lwp
.pr_info
,
3793 sizeof (siginfo_t
));
3796 if (signo
== THE_PR_LWP (pip
->prstatus
).pr_info
.si_signo
)
3798 sip
= &pip
->prstatus
.pr_info
;
3804 siginfo_t
*sip
= &sictl
.siginfo
;
3805 memset ((char *) sip
, 0, sizeof (siginfo_t
));
3807 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
3810 sip
->si_signo
= signo
;
3813 sip
->si_pid
= getpid ();
3814 sip
->si_uid
= getuid ();
3816 #ifdef PROCFS_USE_READ_WRITE
3818 if (write (pip
->ctl_fd
, (char *) &sictl
, sizeof (struct sigi_ctl
)) < 0)
3820 if (ioctl (pip
->ctl_fd
, PIOCSSIG
, sip
) < 0)
3823 print_sys_errmsg (pip
->pathname
, errno
);
3824 warning ("PIOCSSIG failed");
3828 /* Resume execution of process PID. If STEP is nozero, then
3829 just single step it. If SIGNAL is nonzero, restart it with that
3830 signal activated. */
3833 procfs_resume (pid
, step
, signo
)
3836 enum target_signal signo
;
3839 struct procinfo
*pi
, *procinfo
, *next_pi
;
3840 struct proc_ctl pctl
;
3842 pi
= find_procinfo (pid
== -1 ? inferior_pid
: pid
, 0);
3847 pctl
.data
= PRCFAULT
;
3849 pi
->prrun
.pr_flags
= PRSTRACE
| PRSFAULT
| PRCFAULT
;
3853 /* It should not be necessary. If the user explicitly changes the value,
3854 value_assign calls write_register_bytes, which writes it. */
3855 /* It may not be absolutely necessary to specify the PC value for
3856 restarting, but to be safe we use the value that gdb considers
3857 to be current. One case where this might be necessary is if the
3858 user explicitly changes the PC value that gdb considers to be
3859 current. FIXME: Investigate if this is necessary or not. */
3861 #ifdef PRSVADDR_BROKEN
3862 /* Can't do this under Solaris running on a Sparc, as there seems to be no
3863 place to put nPC. In fact, if you use this, nPC seems to be set to some
3864 random garbage. We have to rely on the fact that PC and nPC have been
3865 written previously via PIOCSREG during a register flush. */
3867 pi
->prrun
.pr_vaddr
= (caddr_t
) * (int *) ®isters
[REGISTER_BYTE (PC_REGNUM
)];
3868 pi
->prrun
.pr_flags
!= PRSVADDR
;
3872 if (signo
== TARGET_SIGNAL_STOP
&& pi
->nopass_next_sigstop
)
3873 /* When attaching to a child process, if we forced it to stop with
3874 a PIOCSTOP, then we will have set the nopass_next_sigstop flag.
3875 Upon resuming the first time after such a stop, we explicitly
3876 inhibit sending it another SIGSTOP, which would be the normal
3877 result of default signal handling. One potential drawback to
3878 this is that we will also ignore any attempt to by the user
3879 to explicitly continue after the attach with a SIGSTOP. Ultimately
3880 this problem should be dealt with by making the routines that
3881 deal with the inferior a little smarter, and possibly even allow
3882 an inferior to continue running at the same time as gdb. (FIXME?) */
3884 else if (signo
== TARGET_SIGNAL_TSTP
3885 && THE_PR_LWP (pi
->prstatus
).pr_cursig
== SIGTSTP
3886 && THE_PR_LWP (pi
->prstatus
).pr_action
.sa_handler
== SIG_DFL
3889 /* We are about to pass the inferior a SIGTSTP whose action is
3890 SIG_DFL. The SIG_DFL action for a SIGTSTP is to stop
3891 (notifying the parent via wait()), and then keep going from the
3892 same place when the parent is ready for you to keep going. So
3893 under the debugger, it should do nothing (as if the program had
3894 been stopped and then later resumed. Under ptrace, this
3895 happens for us, but under /proc, the system obligingly stops
3896 the process, and wait_for_inferior would have no way of
3897 distinguishing that type of stop (which indicates that we
3898 should just start it again), with a stop due to the pr_trace
3899 field of the prrun_t struct.
3901 Note that if the SIGTSTP is being caught, we *do* need to pass it,
3902 because the handler needs to get executed. */
3905 signal_to_pass
= target_signal_to_host (signo
);
3909 set_proc_siginfo (pi
, signal_to_pass
);
3914 pctl
.data
|= PRCSIG
;
3916 pi
->prrun
.pr_flags
|= PRCSIG
;
3919 pi
->nopass_next_sigstop
= 0;
3923 pctl
.data
|= PRSTEP
;
3925 pi
->prrun
.pr_flags
|= PRSTEP
;
3929 /* Don't try to start a process unless it's stopped on an
3930 `event of interest'. Doing so will cause errors. */
3932 if (!procfs_read_status (pi
))
3934 /* The LWP has apparently terminated. */
3936 printf_filtered ("LWP %d doesn't respond.\n",
3937 (pi
->pid
>> 16) & 0xffff);
3938 close_proc_file (pi
);
3942 #ifdef PROCFS_USE_READ_WRITE
3943 if (write (pi
->ctl_fd
, (char *) &pctl
, sizeof (struct proc_ctl
)) < 0)
3945 if ((pi
->prstatus
.pr_flags
& PR_ISTOP
)
3946 && ioctl (pi
->ctl_fd
, PIOCRUN
, &pi
->prrun
) != 0)
3949 /* The LWP has apparently terminated. */
3951 printf_filtered ("LWP %d doesn't respond.\n",
3952 (pi
->pid
>> 16) & 0xffff);
3953 close_proc_file (pi
);
3957 /* Continue all the other threads that haven't had an event of interest.
3958 Also continue them if they have NOPASS_NEXT_SIGSTOP set; this is only
3959 set by do_attach, and means this is the first resume after an attach.
3960 All threads were CSTOP'd by do_attach, and should be resumed now. */
3963 for (procinfo
= procinfo_list
; procinfo
; procinfo
= next_pi
)
3965 next_pi
= procinfo
->next
;
3967 if (!procinfo
->had_event
||
3968 (procinfo
->nopass_next_sigstop
&& signo
== TARGET_SIGNAL_STOP
))
3970 procinfo
->had_event
= procinfo
->nopass_next_sigstop
= 0;
3971 #ifdef PROCFS_USE_READ_WRITE
3972 pctl
.data
= PRCFAULT
| PRCSIG
;
3973 if (write (procinfo
->ctl_fd
, (char *) &pctl
,
3974 sizeof (struct proc_ctl
)) < 0)
3976 if (!procfs_read_status (procinfo
))
3977 fprintf_unfiltered (gdb_stderr
,
3978 "procfs_read_status failed, errno=%d\n",
3980 print_sys_errmsg (procinfo
->pathname
, errno
);
3981 error ("PCRUN failed");
3984 procinfo
->prrun
.pr_flags
&= PRSTEP
;
3985 procinfo
->prrun
.pr_flags
|= PRCFAULT
| PRCSIG
;
3986 if (!procfs_read_status (procinfo
))
3988 /* The LWP has apparently terminated. */
3990 printf_filtered ("LWP %d doesn't respond.\n",
3991 (procinfo
->pid
>> 16) & 0xffff);
3992 close_proc_file (procinfo
);
3996 /* Don't try to start a process unless it's stopped on an
3997 `event of interest'. Doing so will cause errors. */
3999 if ((procinfo
->prstatus
.pr_flags
& PR_ISTOP
)
4000 && ioctl (procinfo
->ctl_fd
, PIOCRUN
, &procinfo
->prrun
) < 0)
4002 if (!procfs_read_status (procinfo
))
4003 fprintf_unfiltered (gdb_stderr
,
4004 "procfs_read_status failed, errno=%d\n",
4006 print_sys_errmsg (procinfo
->pathname
, errno
);
4007 warning ("PIOCRUN failed");
4011 procfs_read_status (procinfo
);
4019 procfs_fetch_registers -- fetch current registers from inferior
4023 void procfs_fetch_registers (int regno)
4027 Read the current values of the inferior's registers, both the
4028 general register set and floating point registers (if supported)
4029 and update gdb's idea of their current values.
4034 procfs_fetch_registers (regno
)
4037 struct procinfo
*pi
;
4039 pi
= current_procinfo
;
4042 if (procfs_read_status (pi
))
4044 supply_gregset (&pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
);
4045 #if defined (FP0_REGNUM)
4046 supply_fpregset (&pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
);
4049 #else /* UNIXWARE */
4050 if (ioctl (pi
->ctl_fd
, PIOCGREG
, &pi
->gregset
.gregset
) != -1)
4052 supply_gregset (&pi
->gregset
.gregset
);
4054 #if defined (FP0_REGNUM)
4055 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
.fpregset
) != -1)
4057 supply_fpregset (&pi
->fpregset
.fpregset
);
4060 #endif /* UNIXWARE */
4067 proc_init_failed - called when /proc access initialization fails
4072 static void proc_init_failed (struct procinfo *pi,
4073 char *why, int kill_p)
4077 This function is called whenever initialization of access to a /proc
4078 entry fails. It prints a suitable error message, does some cleanup,
4079 and then invokes the standard error processing routine which dumps
4080 us back into the command loop. If KILL_P is true, sends SIGKILL.
4084 proc_init_failed (pi
, why
, kill_p
)
4085 struct procinfo
*pi
;
4089 print_sys_errmsg (pi
->pathname
, errno
);
4091 kill (pi
->pid
, SIGKILL
);
4092 close_proc_file (pi
);
4101 close_proc_file - close any currently open /proc entry
4105 static void close_proc_file (struct procinfo *pip)
4109 Close any currently open /proc entry and mark the process information
4110 entry as invalid. In order to ensure that we don't try to reuse any
4111 stale information, the pid, fd, and pathnames are explicitly
4112 invalidated, which may be overkill.
4117 close_proc_file (pip
)
4118 struct procinfo
*pip
;
4120 struct procinfo
*procinfo
;
4122 delete_thread (pip
->pid
); /* remove thread from GDB's thread list */
4123 remove_fd (pip
); /* Remove fd from poll/select list */
4125 close (pip
->ctl_fd
);
4126 #ifdef HAVE_MULTIPLE_PROC_FDS
4128 close (pip
->status_fd
);
4129 close (pip
->map_fd
);
4132 free (pip
->pathname
);
4134 /* Unlink pip from the procinfo chain. Note pip might not be on the list. */
4136 if (procinfo_list
== pip
)
4137 procinfo_list
= pip
->next
;
4140 for (procinfo
= procinfo_list
; procinfo
; procinfo
= procinfo
->next
)
4142 if (procinfo
->next
== pip
)
4144 procinfo
->next
= pip
->next
;
4153 close_proc_file_cleanup (pip
)
4156 close_proc_file ((struct procinfo
*) pip
);
4159 static struct cleanup
*
4160 make_cleanup_close_proc_file (pip
)
4161 struct procinfo
*pip
;
4163 return make_cleanup (close_proc_file_cleanup
, pip
);
4170 open_proc_file - open a /proc entry for a given process id
4174 static int open_proc_file (int pid, struct procinfo *pip, int mode)
4178 Given a process id and a mode, close the existing open /proc
4179 entry (if any) and open one for the new process id, in the
4180 specified mode. Once it is open, then mark the local process
4181 information structure as valid, which guarantees that the pid,
4182 fd, and pathname fields match an open /proc entry. Returns
4183 zero if the open fails, nonzero otherwise.
4185 Note that the pathname is left intact, even when the open fails,
4186 so that callers can use it to construct meaningful error messages
4187 rather than just "file open failed".
4189 Note that for Solaris, the process-id also includes an LWP-id, so we
4190 actually attempt to open that. If we are handed a pid with a 0 LWP-id,
4191 then we will ask the kernel what it is and add it to the pid. Hence,
4192 the pid can be changed by us.
4196 open_proc_file (pid
, pip
, mode
, control
)
4198 struct procinfo
*pip
;
4206 pip
->pathname
= xmalloc (MAX_PROC_NAME_SIZE
);
4215 #ifdef HAVE_MULTIPLE_PROC_FDS
4216 sprintf (pip
->pathname
, STATUS_PROC_NAME_FMT
, tmp
);
4217 if ((pip
->status_fd
= open (pip
->pathname
, O_RDONLY
)) < 0)
4222 sprintf (pip
->pathname
, AS_PROC_NAME_FMT
, tmp
);
4223 if ((pip
->as_fd
= open (pip
->pathname
, O_RDWR
)) < 0)
4225 close (pip
->status_fd
);
4229 sprintf (pip
->pathname
, MAP_PROC_NAME_FMT
, tmp
);
4230 if ((pip
->map_fd
= open (pip
->pathname
, O_RDONLY
)) < 0)
4232 close (pip
->status_fd
);
4239 sprintf (pip
->pathname
, CTL_PROC_NAME_FMT
, tmp
);
4240 if ((pip
->ctl_fd
= open (pip
->pathname
, O_WRONLY
)) < 0)
4242 close (pip
->status_fd
);
4244 close (pip
->map_fd
);
4249 #else /* HAVE_MULTIPLE_PROC_FDS */
4250 sprintf (pip
->pathname
, CTL_PROC_NAME_FMT
, tmp
);
4252 if ((tmpfd
= open (pip
->pathname
, mode
)) < 0)
4256 pip
->ctl_fd
= tmpfd
;
4258 pip
->map_fd
= tmpfd
;
4259 pip
->status_fd
= tmpfd
;
4261 tmp
= (pid
>> 16) & 0xffff; /* Extract thread id */
4264 { /* Don't know thread id yet */
4265 if (ioctl (tmpfd
, PIOCSTATUS
, &pip
->prstatus
) < 0)
4267 print_sys_errmsg (pip
->pathname
, errno
);
4269 error ("open_proc_file: PIOCSTATUS failed");
4272 tmp
= pip
->prstatus
.pr_who
; /* Get thread id from prstatus_t */
4273 pip
->pid
= (tmp
<< 16) | pid
; /* Update pip */
4276 if ((pip
->ctl_fd
= ioctl (tmpfd
, PIOCOPENLWP
, &tmp
)) < 0)
4282 #ifdef PIOCSET /* New method */
4285 pr_flags
= PR_ASYNC
;
4286 ioctl (pip
->ctl_fd
, PIOCSET
, &pr_flags
);
4290 /* keep extra fds in sync */
4291 pip
->as_fd
= pip
->ctl_fd
;
4292 pip
->map_fd
= pip
->ctl_fd
;
4293 pip
->status_fd
= pip
->ctl_fd
;
4295 close (tmpfd
); /* All done with main pid */
4296 #endif /* PIOCOPENLWP */
4298 #endif /* HAVE_MULTIPLE_PROC_FDS */
4304 mappingflags (flags
)
4307 static char asciiflags
[8];
4309 strcpy (asciiflags
, "-------");
4310 #if defined (MA_PHYS)
4311 if (flags
& MA_PHYS
)
4312 asciiflags
[0] = 'd';
4314 if (flags
& MA_STACK
)
4315 asciiflags
[1] = 's';
4316 if (flags
& MA_BREAK
)
4317 asciiflags
[2] = 'b';
4318 if (flags
& MA_SHARED
)
4319 asciiflags
[3] = 's';
4320 if (flags
& MA_READ
)
4321 asciiflags
[4] = 'r';
4322 if (flags
& MA_WRITE
)
4323 asciiflags
[5] = 'w';
4324 if (flags
& MA_EXEC
)
4325 asciiflags
[6] = 'x';
4326 return (asciiflags
);
4330 info_proc_flags (pip
, summary
)
4331 struct procinfo
*pip
;
4334 struct trans
*transp
;
4336 long flags
= pip
->prstatus
.pr_flags
| pip
->prstatus
.pr_lwp
.pr_flags
;
4338 long flags
= pip
->prstatus
.pr_flags
;
4341 printf_filtered ("%-32s", "Process status flags:");
4344 printf_filtered ("\n\n");
4346 for (transp
= pr_flag_table
; transp
->name
!= NULL
; transp
++)
4348 if (flags
& transp
->value
)
4352 printf_filtered ("%s ", transp
->name
);
4356 printf_filtered ("\t%-16s %s.\n", transp
->name
, transp
->desc
);
4360 printf_filtered ("\n");
4364 info_proc_stop (pip
, summary
)
4365 struct procinfo
*pip
;
4368 struct trans
*transp
;
4372 why
= THE_PR_LWP (pip
->prstatus
).pr_why
;
4373 what
= THE_PR_LWP (pip
->prstatus
).pr_what
;
4375 if (THE_PR_LWP (pip
->prstatus
).pr_flags
& PR_STOPPED
)
4377 printf_filtered ("%-32s", "Reason for stopping:");
4380 printf_filtered ("\n\n");
4382 for (transp
= pr_why_table
; transp
->name
!= NULL
; transp
++)
4384 if (why
== transp
->value
)
4388 printf_filtered ("%s ", transp
->name
);
4392 printf_filtered ("\t%-16s %s.\n",
4393 transp
->name
, transp
->desc
);
4399 /* Use the pr_why field to determine what the pr_what field means, and
4400 print more information. */
4405 /* pr_what is unused for this case */
4411 printf_filtered ("%s ", signalname (what
));
4415 printf_filtered ("\t%-16s %s.\n", signalname (what
),
4416 safe_strsignal (what
));
4422 printf_filtered ("%s ", syscallname (what
));
4426 printf_filtered ("\t%-16s %s.\n", syscallname (what
),
4427 "Entered this system call");
4433 printf_filtered ("%s ", syscallname (what
));
4437 printf_filtered ("\t%-16s %s.\n", syscallname (what
),
4438 "Returned from this system call");
4444 printf_filtered ("%s ",
4445 lookupname (faults_table
, what
, "fault"));
4449 printf_filtered ("\t%-16s %s.\n",
4450 lookupname (faults_table
, what
, "fault"),
4451 lookupdesc (faults_table
, what
));
4455 printf_filtered ("\n");
4460 info_proc_siginfo (pip
, summary
)
4461 struct procinfo
*pip
;
4464 struct siginfo
*sip
;
4466 if ((THE_PR_LWP (pip
->prstatus
).pr_flags
& PR_STOPPED
) &&
4467 (THE_PR_LWP (pip
->prstatus
).pr_why
== PR_SIGNALLED
||
4468 THE_PR_LWP (pip
->prstatus
).pr_why
== PR_FAULTED
))
4470 printf_filtered ("%-32s", "Additional signal/fault info:");
4471 sip
= &(THE_PR_LWP (pip
->prstatus
).pr_info
);
4474 printf_filtered ("%s ", signalname (sip
->si_signo
));
4475 if (sip
->si_errno
> 0)
4477 printf_filtered ("%s ", errnoname (sip
->si_errno
));
4479 if (sip
->si_code
<= 0)
4481 printf_filtered ("sent by %s, uid %d ",
4482 target_pid_to_str (sip
->si_pid
),
4487 printf_filtered ("%s ", sigcodename (sip
));
4488 if ((sip
->si_signo
== SIGILL
) ||
4489 (sip
->si_signo
== SIGFPE
) ||
4490 (sip
->si_signo
== SIGSEGV
) ||
4491 (sip
->si_signo
== SIGBUS
))
4493 printf_filtered ("addr=%#lx ",
4494 (unsigned long) sip
->si_addr
);
4496 else if ((sip
->si_signo
== SIGCHLD
))
4498 printf_filtered ("child %s, status %u ",
4499 target_pid_to_str (sip
->si_pid
),
4502 else if ((sip
->si_signo
== SIGPOLL
))
4504 printf_filtered ("band %u ", sip
->si_band
);
4510 printf_filtered ("\n\n");
4511 printf_filtered ("\t%-16s %s.\n", signalname (sip
->si_signo
),
4512 safe_strsignal (sip
->si_signo
));
4513 if (sip
->si_errno
> 0)
4515 printf_filtered ("\t%-16s %s.\n",
4516 errnoname (sip
->si_errno
),
4517 safe_strerror (sip
->si_errno
));
4519 if (sip
->si_code
<= 0)
4521 printf_filtered ("\t%-16u %s\n", sip
->si_pid
, /* XXX need target_pid_to_str() */
4522 "PID of process sending signal");
4523 printf_filtered ("\t%-16u %s\n", sip
->si_uid
,
4524 "UID of process sending signal");
4528 printf_filtered ("\t%-16s %s.\n", sigcodename (sip
),
4530 if ((sip
->si_signo
== SIGILL
) ||
4531 (sip
->si_signo
== SIGFPE
))
4533 printf_filtered ("\t%#-16lx %s.\n",
4534 (unsigned long) sip
->si_addr
,
4535 "Address of faulting instruction");
4537 else if ((sip
->si_signo
== SIGSEGV
) ||
4538 (sip
->si_signo
== SIGBUS
))
4540 printf_filtered ("\t%#-16lx %s.\n",
4541 (unsigned long) sip
->si_addr
,
4542 "Address of faulting memory reference");
4544 else if ((sip
->si_signo
== SIGCHLD
))
4546 printf_filtered ("\t%-16u %s.\n", sip
->si_pid
, /* XXX need target_pid_to_str() */
4547 "Child process ID");
4548 printf_filtered ("\t%-16u %s.\n", sip
->si_status
,
4549 "Child process exit value or signal");
4551 else if ((sip
->si_signo
== SIGPOLL
))
4553 printf_filtered ("\t%-16u %s.\n", sip
->si_band
,
4554 "Band event for POLL_{IN,OUT,MSG}");
4558 printf_filtered ("\n");
4563 info_proc_syscalls (pip
, summary
)
4564 struct procinfo
*pip
;
4572 #if 0 /* FIXME: Needs to use gdb-wide configured info about system calls. */
4573 if (pip
->prstatus
.pr_flags
& PR_ASLEEP
)
4575 int syscallnum
= pip
->prstatus
.pr_reg
[R_D0
];
4578 printf_filtered ("%-32s", "Sleeping in system call:");
4579 printf_filtered ("%s", syscallname (syscallnum
));
4583 printf_filtered ("Sleeping in system call '%s'.\n",
4584 syscallname (syscallnum
));
4590 if (ioctl (pip
->ctl_fd
, PIOCGENTRY
, &pip
->entryset
) < 0)
4592 print_sys_errmsg (pip
->pathname
, errno
);
4593 error ("PIOCGENTRY failed");
4596 if (ioctl (pip
->ctl_fd
, PIOCGEXIT
, &pip
->exitset
) < 0)
4598 print_sys_errmsg (pip
->pathname
, errno
);
4599 error ("PIOCGEXIT failed");
4603 printf_filtered ("System call tracing information:\n\n");
4605 printf_filtered ("\t%-12s %-8s %-8s\n",
4609 for (syscallnum
= 0; syscallnum
< MAX_SYSCALLS
; syscallnum
++)
4612 if (syscall_table
[syscallnum
] != NULL
)
4613 printf_filtered ("\t%-12s ", syscall_table
[syscallnum
]);
4615 printf_filtered ("\t%-12d ", syscallnum
);
4618 printf_filtered ("%-8s ",
4619 prismember (&pip
->prstatus
.pr_sysentry
, syscallnum
)
4621 printf_filtered ("%-8s ",
4622 prismember (&pip
->prstatus
.pr_sysexit
, syscallnum
)
4625 printf_filtered ("%-8s ",
4626 prismember (&pip
->entryset
, syscallnum
)
4628 printf_filtered ("%-8s ",
4629 prismember (&pip
->exitset
, syscallnum
)
4632 printf_filtered ("\n");
4634 printf_filtered ("\n");
4643 static char locbuf
[32];
4645 name
= strsigno (signo
);
4648 sprintf (locbuf
, "Signal %d", signo
);
4652 sprintf (locbuf
, "%s (%d)", name
, signo
);
4662 static char locbuf
[32];
4664 name
= strerrno (errnum
);
4667 sprintf (locbuf
, "Errno %d", errnum
);
4671 sprintf (locbuf
, "%s (%d)", name
, errnum
);
4677 info_proc_signals (pip
, summary
)
4678 struct procinfo
*pip
;
4685 #ifndef PROCFS_USE_READ_WRITE
4686 if (ioctl (pip
->ctl_fd
, PIOCGTRACE
, &pip
->trace
) < 0)
4688 print_sys_errmsg (pip
->pathname
, errno
);
4689 error ("PIOCGTRACE failed");
4693 printf_filtered ("Disposition of signals:\n\n");
4694 printf_filtered ("\t%-15s %-8s %-8s %-8s %s\n\n",
4695 "Signal", "Trace", "Hold", "Pending", "Description");
4696 for (signo
= 0; signo
< NSIG
; signo
++)
4699 printf_filtered ("\t%-15s ", signalname (signo
));
4701 printf_filtered ("%-8s ",
4702 prismember (&pip
->prstatus
.pr_sigtrace
, signo
)
4704 printf_filtered ("%-8s ",
4705 prismember (&pip
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
, signo
)
4708 printf_filtered ("%-8s ",
4709 prismember (&pip
->trace
, signo
)
4711 printf_filtered ("%-8s ",
4712 prismember (&pip
->prstatus
.pr_sighold
, signo
)
4717 if (prismember (&pip
->prstatus
.pr_sigpend
, signo
) ||
4718 prismember (&pip
->prstatus
.pr_lwp
.pr_lwppend
, signo
))
4719 printf_filtered ("%-8s ", "yes");
4721 printf_filtered ("%-8s ", "no");
4722 #else /* UNIXWARE */
4723 #ifdef PROCFS_SIGPEND_OFFSET
4724 /* Alpha OSF/1 numbers the pending signals from 1. */
4725 printf_filtered ("%-8s ",
4726 (signo
? prismember (&pip
->prstatus
.pr_sigpend
,
4731 printf_filtered ("%-8s ",
4732 prismember (&pip
->prstatus
.pr_sigpend
, signo
)
4735 #endif /* UNIXWARE */
4736 printf_filtered (" %s\n", safe_strsignal (signo
));
4738 printf_filtered ("\n");
4743 info_proc_faults (pip
, summary
)
4744 struct procinfo
*pip
;
4747 struct trans
*transp
;
4752 if (ioctl (pip
->ctl_fd
, PIOCGFAULT
, &pip
->fltset
.fltset
) < 0)
4754 print_sys_errmsg (pip
->pathname
, errno
);
4755 error ("PIOCGFAULT failed");
4759 printf_filtered ("Current traced hardware fault set:\n\n");
4760 printf_filtered ("\t%-12s %-8s\n", "Fault", "Trace");
4762 for (transp
= faults_table
; transp
->name
!= NULL
; transp
++)
4765 printf_filtered ("\t%-12s ", transp
->name
);
4767 printf_filtered ("%-8s", prismember (&pip
->prstatus
.pr_flttrace
, transp
->value
)
4770 printf_filtered ("%-8s", prismember (&pip
->fltset
.fltset
, transp
->value
)
4773 printf_filtered ("\n");
4775 printf_filtered ("\n");
4780 info_proc_mappings (pip
, summary
)
4781 struct procinfo
*pip
;
4785 struct prmap
*prmaps
;
4786 struct prmap
*prmap
;
4791 printf_filtered ("Mapped address spaces:\n\n");
4792 #ifdef BFD_HOST_64_BIT
4793 printf_filtered (" %18s %18s %10s %10s %7s\n",
4795 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
4802 #ifdef PROCFS_USE_READ_WRITE
4803 if (fstat (pip
->map_fd
, &sbuf
) == 0)
4805 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
4806 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
4807 if ((lseek (pip
->map_fd
, 0, SEEK_SET
) == 0) &&
4808 (read (pip
->map_fd
, (char *) prmaps
,
4809 nmap
* sizeof (*prmaps
)) == (nmap
* sizeof (*prmaps
))))
4812 for (prmap
= prmaps
; i
< nmap
; ++prmap
, ++i
)
4814 if (ioctl (pip
->ctl_fd
, PIOCNMAP
, &nmap
) == 0)
4816 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
4817 if (ioctl (pip
->ctl_fd
, PIOCMAP
, prmaps
) == 0)
4819 for (prmap
= prmaps
; prmap
->pr_size
; ++prmap
)
4820 #endif /* PROCFS_USE_READ_WRITE */
4822 #ifdef BFD_HOST_64_BIT
4823 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
4825 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
4827 (unsigned long) prmap
->pr_vaddr
,
4828 (unsigned long) prmap
->pr_vaddr
4829 + prmap
->pr_size
- 1,
4832 mappingflags (prmap
->pr_mflags
));
4836 printf_filtered ("\n");
4844 info_proc -- implement the "info proc" command
4848 void info_proc (char *args, int from_tty)
4852 Implement gdb's "info proc" command by using the /proc interface
4853 to print status information about any currently running process.
4855 Examples of the use of "info proc" are:
4857 info proc (prints summary info for current inferior)
4858 info proc 123 (prints summary info for process with pid 123)
4859 info proc mappings (prints address mappings)
4860 info proc times (prints process/children times)
4861 info proc id (prints pid, ppid, gid, sid, etc)
4862 FIXME: i proc id not implemented.
4863 info proc status (prints general process state info)
4864 FIXME: i proc status not implemented.
4865 info proc signals (prints info about signal handling)
4866 info proc all (prints all info)
4871 info_proc (args
, from_tty
)
4876 struct procinfo
*pip
;
4877 struct cleanup
*old_chain
;
4893 old_chain
= make_cleanup (null_cleanup
, 0);
4895 /* Default to using the current inferior if no pid specified. Note
4896 that inferior_pid may be 0, hence we set okerr. */
4898 pid
= inferior_pid
& 0x7fffffff; /* strip off sol-thread bit */
4899 if (!(pip
= find_procinfo (pid
, 1))) /* inferior_pid no good? */
4900 pip
= procinfo_list
; /* take first available */
4901 pid
= pid
& 0xffff; /* extract "real" pid */
4905 if ((argv
= buildargv (args
)) == NULL
)
4909 make_cleanup_freeargv (argv
);
4911 while (*argv
!= NULL
)
4913 argsize
= strlen (*argv
);
4914 if (argsize
>= 1 && strncmp (*argv
, "all", argsize
) == 0)
4919 else if (argsize
>= 2 && strncmp (*argv
, "faults", argsize
) == 0)
4924 else if (argsize
>= 2 && strncmp (*argv
, "flags", argsize
) == 0)
4929 else if (argsize
>= 1 && strncmp (*argv
, "id", argsize
) == 0)
4934 else if (argsize
>= 1 && strncmp (*argv
, "mappings", argsize
) == 0)
4939 else if (argsize
>= 2 && strncmp (*argv
, "signals", argsize
) == 0)
4944 else if (argsize
>= 2 && strncmp (*argv
, "status", argsize
) == 0)
4949 else if (argsize
>= 2 && strncmp (*argv
, "syscalls", argsize
) == 0)
4954 else if (argsize
>= 1 && strncmp (*argv
, "times", argsize
) == 0)
4959 else if ((pid
= atoi (*argv
)) > 0)
4961 pip
= (struct procinfo
*) xmalloc (sizeof (struct procinfo
));
4962 memset (pip
, 0, sizeof (*pip
));
4965 if (!open_proc_file (pid
, pip
, O_RDONLY
, 0))
4967 perror_with_name (pip
->pathname
);
4971 make_cleanup_close_proc_file (pip
);
4973 else if (**argv
!= '\000')
4975 error ("Unrecognized or ambiguous keyword `%s'.", *argv
);
4981 /* If we don't have a valid open process at this point, then we have no
4982 inferior or didn't specify a specific pid. */
4987 No process. Start debugging a program or specify an explicit process ID.");
4990 if (!procfs_read_status (pip
))
4992 print_sys_errmsg (pip
->pathname
, errno
);
4993 error ("procfs_read_status failed");
4996 #ifndef PROCFS_USE_READ_WRITE
4998 nlwp
= pip
->prstatus
.pr_nlwp
;
4999 lwps
= alloca ((2 * nlwp
+ 2) * sizeof (*lwps
));
5001 if (ioctl (pip
->ctl_fd
, PIOCLWPIDS
, lwps
))
5003 print_sys_errmsg (pip
->pathname
, errno
);
5004 error ("PIOCLWPIDS failed");
5006 #else /* PIOCLWPIDS */
5008 lwps
= alloca ((2 * nlwp
+ 2) * sizeof *lwps
);
5010 #endif /* PIOCLWPIDS */
5012 for (; nlwp
> 0; nlwp
--, lwps
++)
5014 pip
= find_procinfo ((*lwps
<< 16) | pid
, 1);
5018 pip
= (struct procinfo
*) xmalloc (sizeof (struct procinfo
));
5019 memset (pip
, 0, sizeof (*pip
));
5020 if (!open_proc_file ((*lwps
<< 16) | pid
, pip
, O_RDONLY
, 0))
5023 make_cleanup_close_proc_file (pip
);
5025 if (!procfs_read_status (pip
))
5027 print_sys_errmsg (pip
->pathname
, errno
);
5028 error ("procfs_read_status failed");
5032 #endif /* PROCFS_USE_READ_WRITE */
5034 /* Print verbose information of the requested type(s), or just a summary
5035 of the information for all types. */
5037 printf_filtered ("\nInformation for %s.%d:\n\n", pip
->pathname
, *lwps
);
5038 if (summary
|| all
|| flags
)
5040 info_proc_flags (pip
, summary
);
5044 info_proc_stop (pip
, summary
);
5046 supply_gregset (&pip
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
);
5048 supply_gregset (&pip
->prstatus
.pr_reg
);
5050 printf_filtered ("PC: ");
5051 print_address (read_pc (), gdb_stdout
);
5052 printf_filtered ("\n");
5054 if (summary
|| all
|| signals
|| faults
)
5056 info_proc_siginfo (pip
, summary
);
5058 if (summary
|| all
|| syscalls
)
5060 info_proc_syscalls (pip
, summary
);
5062 if (summary
|| all
|| mappings
)
5064 info_proc_mappings (pip
, summary
);
5066 if (summary
|| all
|| signals
)
5068 info_proc_signals (pip
, summary
);
5070 if (summary
|| all
|| faults
)
5072 info_proc_faults (pip
, summary
);
5074 printf_filtered ("\n");
5076 /* All done, deal with closing any temporary process info structure,
5077 freeing temporary memory , etc. */
5079 do_cleanups (old_chain
);
5080 #ifndef PROCFS_USE_READ_WRITE
5089 modify_inherit_on_fork_flag - Change the inherit-on-fork flag
5093 void modify_inherit_on_fork_flag (fd, flag)
5097 Call this routine to modify the inherit-on-fork flag. This routine is
5098 just a nice wrapper to hide the #ifdefs needed by various systems to
5104 modify_inherit_on_fork_flag (fd
, flag
)
5108 #if defined (PIOCSET) || defined (PCSET)
5112 struct proc_ctl pctl
;
5114 #if defined (PIOCSET) || defined (PCSET) /* New method */
5118 #ifdef PROCFS_USE_READ_WRITE
5120 pctl
.data
= PR_FORK
;
5121 if (write (fd
, (char *) &pctl
, sizeof (struct proc_ctl
)) < 0)
5124 retval
= ioctl (fd
, PIOCSET
, &pr_flags
);
5129 #ifdef PROCFS_USE_READ_WRITE
5131 pctl
.data
= PR_FORK
;
5132 if (write (fd
, (char *) &pctl
, sizeof (struct proc_ctl
)) < 0)
5135 retval
= ioctl (fd
, PIOCRESET
, &pr_flags
);
5140 #ifdef PIOCSFORK /* Original method */
5143 retval
= ioctl (fd
, PIOCSFORK
, NULL
);
5147 retval
= ioctl (fd
, PIOCRFORK
, NULL
);
5150 Neither PR_FORK nor PIOCSFORK exist
! !!
5157 print_sys_errmsg ("modify_inherit_on_fork_flag", errno
);
5158 error ("PIOCSFORK or PR_FORK modification failed");
5165 modify_run_on_last_close_flag - Change the run-on-last-close flag
5169 void modify_run_on_last_close_flag (fd, flag)
5173 Call this routine to modify the run-on-last-close flag. This routine
5174 is just a nice wrapper to hide the #ifdefs needed by various systems to
5180 modify_run_on_last_close_flag (fd
, flag
)
5184 #if defined (PIOCSET) || defined (PCSET)
5188 struct proc_ctl pctl
;
5190 #if defined (PIOCSET) || defined (PCSET) /* New method */
5194 #ifdef PROCFS_USE_READ_WRITE
5197 if (write (fd
, (char *) &pctl
, sizeof (struct proc_ctl
)) < 0)
5200 retval
= ioctl (fd
, PIOCSET
, &pr_flags
);
5205 #ifdef PROCFS_USE_READ_WRITE
5208 if (write (fd
, (char *) &pctl
, sizeof (struct proc_ctl
)) < 0)
5211 retval
= ioctl (fd
, PIOCRESET
, &pr_flags
);
5216 #ifdef PIOCSRLC /* Original method */
5218 retval
= ioctl (fd
, PIOCSRLC
, NULL
);
5220 retval
= ioctl (fd
, PIOCRRLC
, NULL
);
5222 Neither PR_RLC nor PIOCSRLC exist
! !!
5229 print_sys_errmsg ("modify_run_on_last_close_flag", errno
);
5230 error ("PIOCSRLC or PR_RLC modification failed");
5237 procfs_clear_syscall_trap -- Deletes the trap for the specified system call.
5241 void procfs_clear_syscall_trap (struct procinfo *, int syscall_num, int errok)
5245 This function function disables traps for the specified system call.
5246 errok is non-zero if errors should be ignored.
5250 procfs_clear_syscall_trap (pi
, syscall_num
, errok
)
5251 struct procinfo
*pi
;
5259 goterr
= ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysset
) < 0;
5261 if (goterr
&& !errok
)
5263 print_sys_errmsg (pi
->pathname
, errno
);
5264 error ("PIOCGENTRY failed");
5269 prdelset (&sysset
, syscall_num
);
5271 if ((ioctl (pi
->ctl_fd
, PIOCSENTRY
, &sysset
) < 0) && !errok
)
5273 print_sys_errmsg (pi
->pathname
, errno
);
5274 error ("PIOCSENTRY failed");
5278 goterr
= ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysset
) < 0;
5280 if (goterr
&& !errok
)
5282 procfs_clear_syscall_trap (pi
, syscall_num
, 1);
5283 print_sys_errmsg (pi
->pathname
, errno
);
5284 error ("PIOCGEXIT failed");
5289 praddset (&sysset
, syscall_num
);
5291 if ((ioctl (pi
->ctl_fd
, PIOCSEXIT
, &sysset
) < 0) && !errok
)
5293 procfs_clear_syscall_trap (pi
, syscall_num
, 1);
5294 print_sys_errmsg (pi
->pathname
, errno
);
5295 error ("PIOCSEXIT failed");
5300 if (!pi
->syscall_handlers
)
5303 error ("procfs_clear_syscall_trap: syscall_handlers is empty");
5307 /* Remove handler func from the handler list */
5309 for (i
= 0; i
< pi
->num_syscall_handlers
; i
++)
5310 if (pi
->syscall_handlers
[i
].syscall_num
== syscall_num
)
5312 if (i
+ 1 != pi
->num_syscall_handlers
)
5313 { /* Not the last entry.
5314 Move subsequent entries fwd. */
5315 memcpy (&pi
->syscall_handlers
[i
], &pi
->syscall_handlers
[i
+ 1],
5316 (pi
->num_syscall_handlers
- i
- 1)
5317 * sizeof (struct procfs_syscall_handler
));
5320 pi
->syscall_handlers
= xrealloc (pi
->syscall_handlers
,
5321 (pi
->num_syscall_handlers
- 1)
5322 * sizeof (struct procfs_syscall_handler
));
5323 pi
->num_syscall_handlers
--;
5328 error ("procfs_clear_syscall_trap: Couldn't find handler for sys call %d",
5336 procfs_set_syscall_trap -- arrange for a function to be called when the
5337 child executes the specified system call.
5341 void procfs_set_syscall_trap (struct procinfo *, int syscall_num, int flags,
5342 syscall_func_t *function)
5346 This function sets up an entry and/or exit trap for the specified system
5347 call. When the child executes the specified system call, your function
5348 will be called with the call #, a flag that indicates entry or exit, and
5349 pointers to rtnval and statval (which are used by procfs_wait). The
5350 function should return non-zero if something interesting happened, zero
5355 procfs_set_syscall_trap (pi
, syscall_num
, flags
, func
)
5356 struct procinfo
*pi
;
5359 syscall_func_t
*func
;
5364 if (flags
& PROCFS_SYSCALL_ENTRY
)
5366 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysset
) < 0)
5368 print_sys_errmsg (pi
->pathname
, errno
);
5369 error ("PIOCGENTRY failed");
5372 praddset (&sysset
, syscall_num
);
5374 if (ioctl (pi
->ctl_fd
, PIOCSENTRY
, &sysset
) < 0)
5376 print_sys_errmsg (pi
->pathname
, errno
);
5377 error ("PIOCSENTRY failed");
5381 if (flags
& PROCFS_SYSCALL_EXIT
)
5383 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysset
) < 0)
5385 procfs_clear_syscall_trap (pi
, syscall_num
, 1);
5386 print_sys_errmsg (pi
->pathname
, errno
);
5387 error ("PIOCGEXIT failed");
5390 praddset (&sysset
, syscall_num
);
5392 if (ioctl (pi
->ctl_fd
, PIOCSEXIT
, &sysset
) < 0)
5394 procfs_clear_syscall_trap (pi
, syscall_num
, 1);
5395 print_sys_errmsg (pi
->pathname
, errno
);
5396 error ("PIOCSEXIT failed");
5401 if (!pi
->syscall_handlers
)
5403 pi
->syscall_handlers
= xmalloc (sizeof (struct procfs_syscall_handler
));
5404 pi
->syscall_handlers
[0].syscall_num
= syscall_num
;
5405 pi
->syscall_handlers
[0].func
= func
;
5406 pi
->num_syscall_handlers
= 1;
5412 for (i
= 0; i
< pi
->num_syscall_handlers
; i
++)
5413 if (pi
->syscall_handlers
[i
].syscall_num
== syscall_num
)
5415 pi
->syscall_handlers
[i
].func
= func
;
5419 pi
->syscall_handlers
= xrealloc (pi
->syscall_handlers
, (i
+ 1)
5420 * sizeof (struct procfs_syscall_handler
));
5421 pi
->syscall_handlers
[i
].syscall_num
= syscall_num
;
5422 pi
->syscall_handlers
[i
].func
= func
;
5423 pi
->num_syscall_handlers
++;
5427 #ifdef SYS_lwp_create
5433 procfs_lwp_creation_handler - handle exit from the _lwp_create syscall
5437 int procfs_lwp_creation_handler (pi, syscall_num, why, rtnvalp, statvalp)
5441 This routine is called both when an inferior process and it's new lwp
5442 are about to finish a _lwp_create() system call. This is the system
5443 call that Solaris uses to create a lightweight process. When the
5444 target process gets this event, we can look at sysarg[2] to find the
5445 new childs lwp ID, and create a procinfo struct from that. After that,
5446 we pretend that we got a SIGTRAP, and return non-zero to tell
5447 procfs_wait to wake up. Subsequently, wait_for_inferior gets woken up,
5448 sees the new process and continues it.
5450 When we see the child exiting from lwp_create, we just contine it,
5451 since everything was handled when the parent trapped.
5454 In effect, we are only paying attention to the parent's completion of
5455 the lwp_create syscall. If we only paid attention to the child
5456 instead, then we wouldn't detect the creation of a suspended thread.
5460 procfs_lwp_creation_handler (pi
, syscall_num
, why
, rtnvalp
, statvalp
)
5461 struct procinfo
*pi
;
5468 struct procinfo
*childpi
;
5469 struct proc_ctl pctl
;
5471 /* We've just detected the completion of an lwp_create system call. Now we
5472 need to setup a procinfo struct for this thread, and notify the thread
5473 system of the new arrival. */
5475 /* If lwp_create failed, then nothing interesting happened. Continue the
5476 process and go back to sleep. */
5479 /* Joel ... can you check this logic out please? JKJ */
5480 if (pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
[R_EFL
] & 1)
5481 { /* _lwp_create failed */
5483 pctl
.data
= PRCFAULT
;
5485 if (write (pi
->ctl_fd
, (char *) &pctl
, sizeof (struct proc_ctl
)) < 0)
5486 perror_with_name (pi
->pathname
);
5490 #else /* UNIXWARE */
5491 if (PROCFS_GET_CARRY (pi
->prstatus
.pr_reg
))
5492 { /* _lwp_create failed */
5493 pi
->prrun
.pr_flags
&= PRSTEP
;
5494 pi
->prrun
.pr_flags
|= PRCFAULT
;
5496 if (ioctl (pi
->ctl_fd
, PIOCRUN
, &pi
->prrun
) != 0)
5497 perror_with_name (pi
->pathname
);
5503 /* At this point, the new thread is stopped at it's first instruction, and
5504 the parent is stopped at the exit from lwp_create. */
5506 if (pi
->new_child
) /* Child? */
5507 { /* Yes, just continue it */
5510 pctl
.data
= PRCFAULT
;
5512 if (write (pi
->ctl_fd
, (char *) &pctl
, sizeof (struct proc_ctl
)) < 0)
5513 #else /* !UNIXWARE */
5514 pi
->prrun
.pr_flags
&= PRSTEP
;
5515 pi
->prrun
.pr_flags
|= PRCFAULT
;
5517 if ((pi
->prstatus
.pr_flags
& PR_ISTOP
)
5518 && ioctl (pi
->ctl_fd
, PIOCRUN
, &pi
->prrun
) != 0)
5519 #endif /* !UNIXWARE */
5520 perror_with_name (pi
->pathname
);
5522 pi
->new_child
= 0; /* No longer new */
5527 /* We're the proud parent of a new thread. Setup an exit trap for lwp_create
5528 in the child and continue the parent. */
5530 /* Third arg is pointer to new thread id. */
5531 lwp_id
= read_memory_integer (
5532 THE_PR_LWP (pi
->prstatus
).pr_sysarg
[2], sizeof (int));
5534 lwp_id
= (lwp_id
<< 16) | PIDGET (pi
->pid
);
5536 childpi
= create_procinfo (lwp_id
);
5538 /* The new process has actually inherited the lwp_create syscall trap from
5539 it's parent, but we still have to call this to register handlers for
5542 procfs_set_inferior_syscall_traps (childpi
);
5543 add_thread (lwp_id
);
5544 printf_filtered ("[New %s]\n", target_pid_to_str (lwp_id
));
5546 /* Continue the parent */
5549 pctl
.data
= PRCFAULT
;
5551 if (write (pi
->ctl_fd
, (char *) &pctl
, sizeof (struct proc_ctl
)) < 0)
5553 pi
->prrun
.pr_flags
&= PRSTEP
;
5554 pi
->prrun
.pr_flags
|= PRCFAULT
;
5555 if (ioctl (pi
->ctl_fd
, PIOCRUN
, &pi
->prrun
) != 0)
5557 perror_with_name (pi
->pathname
);
5559 /* The new child may have been created in one of two states:
5560 SUSPENDED or RUNNABLE. If runnable, we will simply signal it to run.
5561 If suspended, we flag it to be continued later, when it has an event. */
5563 if (THE_PR_LWP (childpi
->prstatus
).pr_why
== PR_SUSPENDED
)
5564 childpi
->new_child
= 1; /* Flag this as an unseen child process */
5567 /* Continue the child */
5570 pctl
.data
= PRCFAULT
;
5572 if (write (pi
->ctl_fd
, (char *) &pctl
, sizeof (struct proc_ctl
)) < 0)
5574 childpi
->prrun
.pr_flags
&= PRSTEP
;
5575 childpi
->prrun
.pr_flags
|= PRCFAULT
;
5577 if (ioctl (childpi
->ctl_fd
, PIOCRUN
, &childpi
->prrun
) != 0)
5579 perror_with_name (childpi
->pathname
);
5583 #endif /* SYS_lwp_create */
5585 /* Fork an inferior process, and start debugging it with /proc. */
5588 procfs_create_inferior (exec_file
, allargs
, env
)
5593 char *shell_file
= getenv ("SHELL");
5595 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
5598 /* We will be looking down the PATH to find shell_file. If we
5599 just do this the normal way (via execlp, which operates by
5600 attempting an exec for each element of the PATH until it
5601 finds one which succeeds), then there will be an exec for
5602 each failed attempt, each of which will cause a PR_SYSEXIT
5603 stop, and we won't know how to distinguish the PR_SYSEXIT's
5604 for these failed execs with the ones for successful execs
5605 (whether the exec has succeeded is stored at that time in the
5606 carry bit or some such architecture-specific and
5607 non-ABI-specified place).
5609 So I can't think of anything better than to search the PATH
5610 now. This has several disadvantages: (1) There is a race
5611 condition; if we find a file now and it is deleted before we
5612 exec it, we lose, even if the deletion leaves a valid file
5613 further down in the PATH, (2) there is no way to know exactly
5614 what an executable (in the sense of "capable of being
5615 exec'd") file is. Using access() loses because it may lose
5616 if the caller is the superuser; failing to use it loses if
5617 there are ACLs or some such. */
5621 /* FIXME-maybe: might want "set path" command so user can change what
5622 path is used from within GDB. */
5623 char *path
= getenv ("PATH");
5625 struct stat statbuf
;
5628 path
= "/bin:/usr/bin";
5630 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
5631 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1 : NULL
)
5633 p1
= strchr (p
, ':');
5638 strncpy (tryname
, p
, len
);
5639 tryname
[len
] = '\0';
5640 strcat (tryname
, "/");
5641 strcat (tryname
, shell_file
);
5642 if (access (tryname
, X_OK
) < 0)
5644 if (stat (tryname
, &statbuf
) < 0)
5646 if (!S_ISREG (statbuf
.st_mode
))
5647 /* We certainly need to reject directories. I'm not quite
5648 as sure about FIFOs, sockets, etc., but I kind of doubt
5649 that people want to exec() these things. */
5654 /* Not found. This must be an error rather than merely passing
5655 the file to execlp(), because execlp() would try all the
5656 exec()s, causing GDB to get confused. */
5657 error ("Can't find shell %s in PATH", shell_file
);
5659 shell_file
= tryname
;
5662 fork_inferior (exec_file
, allargs
, env
,
5663 proc_set_exec_trap
, procfs_init_inferior
, NULL
, shell_file
);
5665 /* We are at the first instruction we care about. */
5666 /* Pedal to the metal... */
5668 proceed ((CORE_ADDR
) - 1, TARGET_SIGNAL_0
, 0);
5671 /* Clean up after the inferior dies. */
5674 procfs_mourn_inferior ()
5676 struct procinfo
*pi
;
5677 struct procinfo
*next_pi
;
5679 for (pi
= procinfo_list
; pi
; pi
= next_pi
)
5682 unconditionally_kill_inferior (pi
);
5685 unpush_target (&procfs_ops
);
5686 generic_mourn_inferior ();
5690 /* Mark our target-struct as eligible for stray "run" and "attach" commands. */
5694 /* This variable is controlled by modules that sit atop procfs that may layer
5695 their own process structure atop that provided here. sol-thread.c does
5696 this because of the Solaris two-level thread model. */
5698 return !procfs_suppress_run
;
5700 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
5703 /* Insert a watchpoint */
5705 procfs_set_watchpoint (pid
, addr
, len
, rw
)
5711 struct procinfo
*pi
;
5714 pi
= find_procinfo (pid
== -1 ? inferior_pid
: pid
, 0);
5715 wpt
.pr_vaddr
= (caddr_t
) addr
;
5717 wpt
.pr_wflags
= ((rw
& 1) ? MA_READ
: 0) | ((rw
& 2) ? MA_WRITE
: 0);
5718 if (ioctl (pi
->ctl_fd
, PIOCSWATCH
, &wpt
) < 0)
5722 /* Currently it sometimes happens that the same watchpoint gets
5723 deleted twice - don't die in this case (FIXME please) */
5724 if (errno
== ESRCH
&& len
== 0)
5726 print_sys_errmsg (pi
->pathname
, errno
);
5727 error ("PIOCSWATCH failed");
5733 procfs_stopped_by_watchpoint (pid
)
5736 struct procinfo
*pi
;
5740 pi
= find_procinfo (pid
== -1 ? inferior_pid
: pid
, 0);
5741 if (pi
->prstatus
.pr_flags
& (PR_STOPPED
| PR_ISTOP
))
5743 why
= pi
->prstatus
.pr_why
;
5744 what
= pi
->prstatus
.pr_what
;
5745 if (why
== PR_FAULTED
5746 #if defined (FLTWATCH) && defined (FLTKWATCH)
5747 && (what
== FLTWATCH
|| what
== FLTKWATCH
)
5750 && (what
== FLTWATCH
)
5753 && (what
== FLTKWATCH
)
5761 #endif /* !UNIXWARE */
5762 #endif /* TARGET_HAS_HARDWARE_WATCHPOINTS */
5764 /* Why is this necessary? Shouldn't dead threads just be removed from the
5768 procfs_thread_alive (pid
)
5771 struct procinfo
*pi
, *next_pi
;
5773 for (pi
= procinfo_list
; pi
; pi
= next_pi
)
5777 if (procfs_read_status (pi
)) /* alive */
5780 /* defunct (exited) */
5782 close_proc_file (pi
);
5790 procfs_first_available ()
5792 struct procinfo
*pi
;
5794 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
5796 if (procfs_read_status (pi
))
5803 procfs_get_pid_fd (pid
)
5806 struct procinfo
*pi
= find_procinfo (pid
, 1);
5814 /* Send a SIGINT to the process group. This acts just like the user typed a
5815 ^C on the controlling terminal.
5817 XXX - This may not be correct for all systems. Some may want to use
5818 killpg() instead of kill (-pgrp). */
5823 extern pid_t inferior_process_group
;
5825 kill (-inferior_process_group
, SIGINT
);
5828 /* Convert a pid to printable form. */
5832 procfs_pid_to_str (pid
)
5835 static char buf
[100];
5837 sprintf (buf
, "Kernel thread %d", TIDGET (pid
));
5847 procfs_ops
.to_shortname
= "procfs";
5848 procfs_ops
.to_longname
= "Unix /proc child process";
5849 procfs_ops
.to_doc
= "Unix /proc child process (started by the \"run\" command).";
5850 procfs_ops
.to_open
= procfs_open
;
5851 procfs_ops
.to_attach
= procfs_attach
;
5852 procfs_ops
.to_detach
= procfs_detach
;
5853 procfs_ops
.to_resume
= procfs_resume
;
5854 procfs_ops
.to_wait
= procfs_wait
;
5855 procfs_ops
.to_fetch_registers
= procfs_fetch_registers
;
5856 procfs_ops
.to_store_registers
= procfs_store_registers
;
5857 procfs_ops
.to_prepare_to_store
= procfs_prepare_to_store
;
5858 procfs_ops
.to_xfer_memory
= procfs_xfer_memory
;
5859 procfs_ops
.to_files_info
= procfs_files_info
;
5860 procfs_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
5861 procfs_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
5862 procfs_ops
.to_terminal_init
= terminal_init_inferior
;
5863 procfs_ops
.to_terminal_inferior
= terminal_inferior
;
5864 procfs_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
5865 procfs_ops
.to_terminal_ours
= terminal_ours
;
5866 procfs_ops
.to_terminal_info
= child_terminal_info
;
5867 procfs_ops
.to_kill
= procfs_kill_inferior
;
5868 procfs_ops
.to_create_inferior
= procfs_create_inferior
;
5869 procfs_ops
.to_mourn_inferior
= procfs_mourn_inferior
;
5870 procfs_ops
.to_can_run
= procfs_can_run
;
5871 procfs_ops
.to_notice_signals
= procfs_notice_signals
;
5872 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
5873 procfs_ops
.to_stop
= procfs_stop
;
5874 procfs_ops
.to_stratum
= process_stratum
;
5875 procfs_ops
.to_has_all_memory
= 1;
5876 procfs_ops
.to_has_memory
= 1;
5877 procfs_ops
.to_has_stack
= 1;
5878 procfs_ops
.to_has_registers
= 1;
5879 procfs_ops
.to_has_execution
= 1;
5880 procfs_ops
.to_magic
= OPS_MAGIC
;
5884 _initialize_procfs ()
5886 #ifdef HAVE_OPTIONAL_PROC_FS
5887 char procname
[MAX_PROC_NAME_SIZE
];
5890 /* If we have an optional /proc filesystem (e.g. under OSF/1),
5891 don't add procfs support if we cannot access the running
5893 sprintf (procname
, STATUS_PROC_NAME_FMT
, getpid ());
5894 if ((fd
= open (procname
, O_RDONLY
)) < 0)
5900 add_target (&procfs_ops
);
5902 add_info ("processes", info_proc
,
5903 "Show process status information using /proc entry.\n\
5904 Specify process id or use current inferior by default.\n\
5905 Specify keywords for detailed information; default is summary.\n\
5906 Keywords are: `all', `faults', `flags', `id', `mappings', `signals',\n\
5907 `status', `syscalls', and `times'.\n\
5908 Unambiguous abbreviations may be used.");
5910 init_syscall_table ();