* libaout.h (enum machine_type): Change M_SPARCLET from 142 to 131.
[deliverable/binutils-gdb.git] / gdb / procfs.c
CommitLineData
35f5886e 1/* Machine independent support for SVR4 /proc (process file system) for GDB.
255181a9 2 Copyright 1991, 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
35f5886e
FF
3 Written by Fred Fish at Cygnus Support.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
6c9638b4 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
35f5886e
FF
20
21
22/* N O T E S
23
24For information on the details of using /proc consult section proc(4)
25in the UNIX System V Release 4 System Administrator's Reference Manual.
26
27The general register and floating point register sets are manipulated by
28separate ioctl's. This file makes the assumption that if FP0_REGNUM is
29defined, then support for the floating point register set is desired,
30regardless of whether or not the actual target has floating point hardware.
31
32 */
33
34
5129100c 35#include "defs.h"
35f5886e 36
08f74b92 37#include <sys/types.h>
407a8389 38#include <time.h>
2592eef8
PS
39#include <sys/fault.h>
40#include <sys/syscall.h>
35f5886e
FF
41#include <sys/procfs.h>
42#include <fcntl.h>
43#include <errno.h>
2b576293 44#include "gdb_string.h"
de43d7d0
SG
45#include <stropts.h>
46#include <poll.h>
08f74b92 47#include <unistd.h>
2b576293 48#include "gdb_stat.h"
35f5886e 49
35f5886e
FF
50#include "inferior.h"
51#include "target.h"
51b57ded 52#include "command.h"
3fbdd536 53#include "gdbcore.h"
fdfa3315 54#include "gdbthread.h"
cc221e76
FF
55
56#define MAX_SYSCALLS 256 /* Maximum number of syscalls for table */
35f5886e
FF
57
58#ifndef PROC_NAME_FMT
6c316cfd 59#define PROC_NAME_FMT "/proc/%05d"
35f5886e
FF
60#endif
61
3fbdd536
JG
62extern struct target_ops procfs_ops; /* Forward declaration */
63
8fc2b417
SG
64int procfs_suppress_run = 0; /* Non-zero if procfs should pretend not to
65 be a runnable target. Used by targets
66 that can sit atop procfs, such as solaris
67 thread support. */
68
35f5886e
FF
69#if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */
70CORE_ADDR kernel_u_addr;
71#endif
72
cc221e76
FF
73#ifdef BROKEN_SIGINFO_H /* Workaround broken SGS <sys/siginfo.h> */
74#undef si_pid
75#define si_pid _data._proc.pid
76#undef si_uid
77#define si_uid _data._proc._pdata._kill.uid
78#endif /* BROKEN_SIGINFO_H */
79
35f5886e
FF
80/* All access to the inferior, either one started by gdb or one that has
81 been attached to, is controlled by an instance of a procinfo structure,
82 defined below. Since gdb currently only handles one inferior at a time,
a39ad5ce
FF
83 the procinfo structure for the inferior is statically allocated and
84 only one exists at any given time. There is a separate procinfo
85 structure for use by the "info proc" command, so that we can print
86 useful information about any random process without interfering with
87 the inferior's procinfo information. */
35f5886e
FF
88
89struct procinfo {
de43d7d0 90 struct procinfo *next;
35f5886e
FF
91 int pid; /* Process ID of inferior */
92 int fd; /* File descriptor for /proc entry */
93 char *pathname; /* Pathname to /proc entry */
de43d7d0 94 int had_event; /* poll/select says something happened */
35f5886e 95 int was_stopped; /* Nonzero if was stopped prior to attach */
d65eee73 96 int nopass_next_sigstop; /* Don't pass a sigstop on next resume */
35f5886e
FF
97 prrun_t prrun; /* Control state when it is run */
98 prstatus_t prstatus; /* Current process status info */
99 gregset_t gregset; /* General register set */
100 fpregset_t fpregset; /* Floating point register set */
101 fltset_t fltset; /* Current traced hardware fault set */
102 sigset_t trace; /* Current traced signal set */
103 sysset_t exitset; /* Current traced system call exit set */
104 sysset_t entryset; /* Current traced system call entry set */
cc221e76
FF
105 fltset_t saved_fltset; /* Saved traced hardware fault set */
106 sigset_t saved_trace; /* Saved traced signal set */
107 sigset_t saved_sighold; /* Saved held signal set */
108 sysset_t saved_exitset; /* Saved traced system call exit set */
109 sysset_t saved_entryset; /* Saved traced system call entry set */
8fc2b417
SG
110 int num_syscall_handlers; /* Number of syscall handlers currently installed */
111 struct procfs_syscall_handler *syscall_handlers; /* Pointer to list of syscall trap handlers */
112 int new_child; /* Non-zero if it's a new thread */
a39ad5ce
FF
113};
114
de43d7d0
SG
115/* List of inferior process information */
116static struct procinfo *procinfo_list = NULL;
117
118static struct pollfd *poll_list; /* pollfds used for waiting on /proc */
119
120static int num_poll_list = 0; /* Number of entries in poll_list */
121
cc221e76
FF
122/* Much of the information used in the /proc interface, particularly for
123 printing status information, is kept as tables of structures of the
124 following form. These tables can be used to map numeric values to
125 their symbolic names and to a string that describes their specific use. */
126
127struct trans {
128 int value; /* The numeric value */
129 char *name; /* The equivalent symbolic value */
130 char *desc; /* Short description of value */
131};
132
133/* Translate bits in the pr_flags member of the prstatus structure, into the
134 names and desc information. */
135
136static struct trans pr_flag_table[] =
137{
138#if defined (PR_STOPPED)
e3be225e 139 { PR_STOPPED, "PR_STOPPED", "Process is stopped" },
cc221e76
FF
140#endif
141#if defined (PR_ISTOP)
e3be225e 142 { PR_ISTOP, "PR_ISTOP", "Stopped on an event of interest" },
cc221e76
FF
143#endif
144#if defined (PR_DSTOP)
e3be225e 145 { PR_DSTOP, "PR_DSTOP", "A stop directive is in effect" },
cc221e76
FF
146#endif
147#if defined (PR_ASLEEP)
e3be225e 148 { PR_ASLEEP, "PR_ASLEEP", "Sleeping in an interruptible system call" },
cc221e76
FF
149#endif
150#if defined (PR_FORK)
e3be225e 151 { PR_FORK, "PR_FORK", "Inherit-on-fork is in effect" },
cc221e76
FF
152#endif
153#if defined (PR_RLC)
e3be225e 154 { PR_RLC, "PR_RLC", "Run-on-last-close is in effect" },
cc221e76
FF
155#endif
156#if defined (PR_PTRACE)
e3be225e 157 { PR_PTRACE, "PR_PTRACE", "Process is being controlled by ptrace" },
cc221e76
FF
158#endif
159#if defined (PR_PCINVAL)
e3be225e 160 { PR_PCINVAL, "PR_PCINVAL", "PC refers to an invalid virtual address" },
cc221e76
FF
161#endif
162#if defined (PR_ISSYS)
e3be225e 163 { PR_ISSYS, "PR_ISSYS", "Is a system process" },
5c1c5e67
FF
164#endif
165#if defined (PR_STEP)
e3be225e 166 { PR_STEP, "PR_STEP", "Process has single step pending" },
5c1c5e67
FF
167#endif
168#if defined (PR_KLC)
e3be225e 169 { PR_KLC, "PR_KLC", "Kill-on-last-close is in effect" },
5c1c5e67
FF
170#endif
171#if defined (PR_ASYNC)
e3be225e 172 { PR_ASYNC, "PR_ASYNC", "Asynchronous stop is in effect" },
5c1c5e67
FF
173#endif
174#if defined (PR_PCOMPAT)
e3be225e 175 { PR_PCOMPAT, "PR_PCOMPAT", "Ptrace compatibility mode in effect" },
cc221e76 176#endif
e3be225e 177 { 0, NULL, NULL }
cc221e76
FF
178};
179
180/* Translate values in the pr_why field of the prstatus struct. */
181
182static struct trans pr_why_table[] =
183{
184#if defined (PR_REQUESTED)
e3be225e 185 { PR_REQUESTED, "PR_REQUESTED", "Directed to stop via PIOCSTOP/PIOCWSTOP" },
cc221e76
FF
186#endif
187#if defined (PR_SIGNALLED)
e3be225e 188 { PR_SIGNALLED, "PR_SIGNALLED", "Receipt of a traced signal" },
cc221e76
FF
189#endif
190#if defined (PR_FAULTED)
e3be225e 191 { PR_FAULTED, "PR_FAULTED", "Incurred a traced hardware fault" },
cc221e76
FF
192#endif
193#if defined (PR_SYSENTRY)
e3be225e 194 { PR_SYSENTRY, "PR_SYSENTRY", "Entry to a traced system call" },
cc221e76
FF
195#endif
196#if defined (PR_SYSEXIT)
e3be225e 197 { PR_SYSEXIT, "PR_SYSEXIT", "Exit from a traced system call" },
cc221e76
FF
198#endif
199#if defined (PR_JOBCONTROL)
e3be225e 200 { PR_JOBCONTROL, "PR_JOBCONTROL", "Default job control stop signal action" },
5c1c5e67
FF
201#endif
202#if defined (PR_SUSPENDED)
e3be225e 203 { PR_SUSPENDED, "PR_SUSPENDED", "Process suspended" },
cc221e76 204#endif
e3be225e 205 { 0, NULL, NULL }
cc221e76
FF
206};
207
208/* Hardware fault translation table. */
209
210static struct trans faults_table[] =
211{
212#if defined (FLTILL)
e3be225e 213 { FLTILL, "FLTILL", "Illegal instruction" },
cc221e76
FF
214#endif
215#if defined (FLTPRIV)
e3be225e 216 { FLTPRIV, "FLTPRIV", "Privileged instruction" },
cc221e76
FF
217#endif
218#if defined (FLTBPT)
e3be225e 219 { FLTBPT, "FLTBPT", "Breakpoint trap" },
cc221e76
FF
220#endif
221#if defined (FLTTRACE)
e3be225e 222 { FLTTRACE, "FLTTRACE", "Trace trap" },
cc221e76
FF
223#endif
224#if defined (FLTACCESS)
e3be225e 225 { FLTACCESS, "FLTACCESS", "Memory access fault" },
cc221e76
FF
226#endif
227#if defined (FLTBOUNDS)
e3be225e 228 { FLTBOUNDS, "FLTBOUNDS", "Memory bounds violation" },
cc221e76
FF
229#endif
230#if defined (FLTIOVF)
e3be225e 231 { FLTIOVF, "FLTIOVF", "Integer overflow" },
cc221e76
FF
232#endif
233#if defined (FLTIZDIV)
e3be225e 234 { FLTIZDIV, "FLTIZDIV", "Integer zero divide" },
cc221e76
FF
235#endif
236#if defined (FLTFPE)
e3be225e 237 { FLTFPE, "FLTFPE", "Floating-point exception" },
cc221e76
FF
238#endif
239#if defined (FLTSTACK)
e3be225e 240 { FLTSTACK, "FLTSTACK", "Unrecoverable stack fault" },
cc221e76
FF
241#endif
242#if defined (FLTPAGE)
e3be225e 243 { FLTPAGE, "FLTPAGE", "Recoverable page fault" },
cc221e76 244#endif
e3be225e 245 { 0, NULL, NULL }
cc221e76
FF
246};
247
248/* Translation table for signal generation information. See UNIX System
249 V Release 4 Programmer's Reference Manual, siginfo(5). */
250
251static struct sigcode {
252 int signo;
253 int code;
254 char *codename;
255 char *desc;
256} siginfo_table[] = {
257#if defined (SIGILL) && defined (ILL_ILLOPC)
e3be225e 258 { SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode" },
cc221e76
FF
259#endif
260#if defined (SIGILL) && defined (ILL_ILLOPN)
e3be225e 261 { SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand", },
cc221e76
FF
262#endif
263#if defined (SIGILL) && defined (ILL_ILLADR)
e3be225e 264 { SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode" },
cc221e76
FF
265#endif
266#if defined (SIGILL) && defined (ILL_ILLTRP)
e3be225e 267 { SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap" },
cc221e76
FF
268#endif
269#if defined (SIGILL) && defined (ILL_PRVOPC)
e3be225e 270 { SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode" },
cc221e76
FF
271#endif
272#if defined (SIGILL) && defined (ILL_PRVREG)
e3be225e 273 { SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register" },
cc221e76
FF
274#endif
275#if defined (SIGILL) && defined (ILL_COPROC)
e3be225e 276 { SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error" },
cc221e76
FF
277#endif
278#if defined (SIGILL) && defined (ILL_BADSTK)
e3be225e 279 { SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error" },
cc221e76
FF
280#endif
281#if defined (SIGFPE) && defined (FPE_INTDIV)
e3be225e 282 { SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero" },
cc221e76
FF
283#endif
284#if defined (SIGFPE) && defined (FPE_INTOVF)
e3be225e 285 { SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow" },
cc221e76
FF
286#endif
287#if defined (SIGFPE) && defined (FPE_FLTDIV)
e3be225e 288 { SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating point divide by zero" },
cc221e76
FF
289#endif
290#if defined (SIGFPE) && defined (FPE_FLTOVF)
e3be225e 291 { SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating point overflow" },
cc221e76
FF
292#endif
293#if defined (SIGFPE) && defined (FPE_FLTUND)
e3be225e 294 { SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating point underflow" },
cc221e76
FF
295#endif
296#if defined (SIGFPE) && defined (FPE_FLTRES)
e3be225e 297 { SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating point inexact result" },
cc221e76
FF
298#endif
299#if defined (SIGFPE) && defined (FPE_FLTINV)
e3be225e 300 { SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating point operation" },
cc221e76
FF
301#endif
302#if defined (SIGFPE) && defined (FPE_FLTSUB)
e3be225e 303 { SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range" },
cc221e76
FF
304#endif
305#if defined (SIGSEGV) && defined (SEGV_MAPERR)
e3be225e 306 { SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object" },
cc221e76
FF
307#endif
308#if defined (SIGSEGV) && defined (SEGV_ACCERR)
e3be225e 309 { SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for object" },
cc221e76
FF
310#endif
311#if defined (SIGBUS) && defined (BUS_ADRALN)
e3be225e 312 { SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment" },
cc221e76
FF
313#endif
314#if defined (SIGBUS) && defined (BUS_ADRERR)
e3be225e 315 { SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Non-existent physical address" },
cc221e76
FF
316#endif
317#if defined (SIGBUS) && defined (BUS_OBJERR)
e3be225e 318 { SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object specific hardware error" },
cc221e76
FF
319#endif
320#if defined (SIGTRAP) && defined (TRAP_BRKPT)
e3be225e 321 { SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint" },
cc221e76
FF
322#endif
323#if defined (SIGTRAP) && defined (TRAP_TRACE)
e3be225e 324 { SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap" },
cc221e76
FF
325#endif
326#if defined (SIGCLD) && defined (CLD_EXITED)
e3be225e 327 { SIGCLD, CLD_EXITED, "CLD_EXITED", "Child has exited" },
cc221e76
FF
328#endif
329#if defined (SIGCLD) && defined (CLD_KILLED)
e3be225e 330 { SIGCLD, CLD_KILLED, "CLD_KILLED", "Child was killed" },
cc221e76
FF
331#endif
332#if defined (SIGCLD) && defined (CLD_DUMPED)
e3be225e 333 { SIGCLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally" },
cc221e76
FF
334#endif
335#if defined (SIGCLD) && defined (CLD_TRAPPED)
e3be225e 336 { SIGCLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped" },
cc221e76
FF
337#endif
338#if defined (SIGCLD) && defined (CLD_STOPPED)
e3be225e 339 { SIGCLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped" },
cc221e76
FF
340#endif
341#if defined (SIGCLD) && defined (CLD_CONTINUED)
e3be225e 342 { SIGCLD, CLD_CONTINUED, "CLD_CONTINUED", "Stopped child had continued" },
cc221e76
FF
343#endif
344#if defined (SIGPOLL) && defined (POLL_IN)
e3be225e 345 { SIGPOLL, POLL_IN, "POLL_IN", "Input input available" },
cc221e76
FF
346#endif
347#if defined (SIGPOLL) && defined (POLL_OUT)
e3be225e 348 { SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available" },
cc221e76
FF
349#endif
350#if defined (SIGPOLL) && defined (POLL_MSG)
e3be225e 351 { SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available" },
cc221e76
FF
352#endif
353#if defined (SIGPOLL) && defined (POLL_ERR)
e3be225e 354 { SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error" },
cc221e76
FF
355#endif
356#if defined (SIGPOLL) && defined (POLL_PRI)
e3be225e 357 { SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available" },
cc221e76
FF
358#endif
359#if defined (SIGPOLL) && defined (POLL_HUP)
e3be225e 360 { SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected" },
cc221e76 361#endif
e3be225e 362 { 0, 0, NULL, NULL }
cc221e76
FF
363};
364
cc221e76
FF
365static char *syscall_table[MAX_SYSCALLS];
366
1ab3bf1b
JG
367/* Prototypes for local functions */
368
b607efe7
FF
369static void procfs_stop PARAMS ((void));
370
371static int procfs_thread_alive PARAMS ((int));
372
373static int procfs_can_run PARAMS ((void));
374
375static void procfs_mourn_inferior PARAMS ((void));
376
377static void procfs_fetch_registers PARAMS ((int));
378
379static int procfs_wait PARAMS ((int, struct target_waitstatus *));
380
381static void procfs_open PARAMS ((char *, int));
382
383static void procfs_files_info PARAMS ((struct target_ops *));
384
385static void procfs_prepare_to_store PARAMS ((void));
386
387static void procfs_detach PARAMS ((char *, int));
388
389static void procfs_attach PARAMS ((char *, int));
390
391static void proc_set_exec_trap PARAMS ((void));
392
393static int procfs_init_inferior PARAMS ((int));
394
395static struct procinfo *create_procinfo PARAMS ((int));
396
397static void procfs_store_registers PARAMS ((int));
398
399static int procfs_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
400
401static void procfs_kill_inferior PARAMS ((void));
402
403static char *sigcodedesc PARAMS ((siginfo_t *));
404
405static char *sigcodename PARAMS ((siginfo_t *));
406
407static struct procinfo *wait_fd PARAMS ((void));
408
409static void remove_fd PARAMS ((struct procinfo *));
410
411static void add_fd PARAMS ((struct procinfo *));
412
e3be225e 413static void set_proc_siginfo PARAMS ((struct procinfo *, int));
6b801388 414
e3be225e 415static void init_syscall_table PARAMS ((void));
cc221e76 416
e3be225e 417static char *syscallname PARAMS ((int));
cc221e76 418
e3be225e 419static char *signalname PARAMS ((int));
cc221e76 420
e3be225e 421static char *errnoname PARAMS ((int));
4ace50a5 422
e3be225e 423static int proc_address_to_fd PARAMS ((struct procinfo *, CORE_ADDR, int));
1ab3bf1b 424
e3be225e 425static int open_proc_file PARAMS ((int, struct procinfo *, int));
1ab3bf1b 426
e3be225e 427static void close_proc_file PARAMS ((struct procinfo *));
1ab3bf1b 428
e3be225e 429static void unconditionally_kill_inferior PARAMS ((struct procinfo *));
1ab3bf1b 430
e3be225e 431static NORETURN void proc_init_failed PARAMS ((struct procinfo *, char *)) ATTR_NORETURN;
1ab3bf1b 432
e3be225e 433static void info_proc PARAMS ((char *, int));
cc221e76 434
e3be225e 435static void info_proc_flags PARAMS ((struct procinfo *, int));
cc221e76 436
e3be225e 437static void info_proc_stop PARAMS ((struct procinfo *, int));
1ab3bf1b 438
e3be225e 439static void info_proc_siginfo PARAMS ((struct procinfo *, int));
cc221e76 440
e3be225e 441static void info_proc_syscalls PARAMS ((struct procinfo *, int));
cc221e76 442
e3be225e 443static void info_proc_mappings PARAMS ((struct procinfo *, int));
cc221e76 444
e3be225e 445static void info_proc_signals PARAMS ((struct procinfo *, int));
cc221e76 446
e3be225e 447static void info_proc_faults PARAMS ((struct procinfo *, int));
1ab3bf1b 448
e3be225e 449static char *mappingflags PARAMS ((long));
1ab3bf1b 450
e3be225e 451static char *lookupname PARAMS ((struct trans *, unsigned int, char *));
cc221e76 452
e3be225e 453static char *lookupdesc PARAMS ((struct trans *, unsigned int));
cc221e76 454
e3be225e 455static int do_attach PARAMS ((int pid));
3fbdd536 456
e3be225e 457static void do_detach PARAMS ((int siggnal));
3fbdd536 458
e3be225e 459static void procfs_create_inferior PARAMS ((char *, char *, char **));
3fbdd536 460
e3be225e 461static void procfs_notice_signals PARAMS ((int pid));
de43d7d0 462
e3be225e 463static struct procinfo *find_procinfo PARAMS ((pid_t pid, int okfail));
3950a34e 464
8fc2b417
SG
465typedef int syscall_func_t PARAMS ((struct procinfo *pi, int syscall_num,
466 int why, int *rtnval, int *statval));
467
468static void procfs_set_syscall_trap PARAMS ((struct procinfo *pi,
469 int syscall_num, int flags,
470 syscall_func_t *func));
471
472static void procfs_clear_syscall_trap PARAMS ((struct procinfo *pi,
473 int syscall_num, int errok));
474
475#define PROCFS_SYSCALL_ENTRY 0x1 /* Trap on entry to sys call */
476#define PROCFS_SYSCALL_EXIT 0x2 /* Trap on exit from sys call */
477
478static syscall_func_t procfs_exit_handler;
479
480static syscall_func_t procfs_exec_handler;
481
482#ifdef SYS_sproc
483static syscall_func_t procfs_sproc_handler;
484static syscall_func_t procfs_fork_handler;
485#endif
486
487#ifdef SYS_lwp_create
488static syscall_func_t procfs_lwp_creation_handler;
489#endif
490
491static void modify_inherit_on_fork_flag PARAMS ((int fd, int flag));
492static void modify_run_on_last_close_flag PARAMS ((int fd, int flag));
493
494/* */
495
496struct procfs_syscall_handler
497{
498 int syscall_num; /* The number of the system call being handled */
499 /* The function to be called */
500 syscall_func_t *func;
501};
502
503static void procfs_resume PARAMS ((int pid, int step,
504 enum target_signal signo));
505
1ab3bf1b
JG
506/* External function prototypes that can't be easily included in any
507 header file because the args are typedefs in system include files. */
508
e3be225e 509extern void supply_gregset PARAMS ((gregset_t *));
1ab3bf1b 510
e3be225e 511extern void fill_gregset PARAMS ((gregset_t *, int));
1ab3bf1b 512
e3be225e 513extern void supply_fpregset PARAMS ((fpregset_t *));
1ab3bf1b 514
e3be225e 515extern void fill_fpregset PARAMS ((fpregset_t *, int));
35f5886e 516
cc221e76
FF
517/*
518
de43d7d0
SG
519LOCAL FUNCTION
520
521 find_procinfo -- convert a process id to a struct procinfo
522
523SYNOPSIS
524
525 static struct procinfo * find_procinfo (pid_t pid, int okfail);
526
527DESCRIPTION
528
529 Given a process id, look it up in the procinfo chain. Returns
530 a struct procinfo *. If can't find pid, then call error(),
531 unless okfail is set, in which case, return NULL;
532 */
533
534static struct procinfo *
535find_procinfo (pid, okfail)
536 pid_t pid;
537 int okfail;
538{
539 struct procinfo *procinfo;
540
541 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
542 if (procinfo->pid == pid)
543 return procinfo;
544
545 if (okfail)
546 return NULL;
547
548 error ("procfs (find_procinfo): Couldn't locate pid %d", pid);
549}
550
551/*
552
7c5d526e
SG
553LOCAL MACRO
554
555 current_procinfo -- convert inferior_pid to a struct procinfo
556
557SYNOPSIS
558
559 static struct procinfo * current_procinfo;
560
561DESCRIPTION
562
563 Looks up inferior_pid in the procinfo chain. Always returns a
564 struct procinfo *. If process can't be found, we error() out.
565 */
566
567#define current_procinfo find_procinfo (inferior_pid, 0)
568
569/*
570
de43d7d0
SG
571LOCAL FUNCTION
572
573 add_fd -- Add the fd to the poll/select list
574
575SYNOPSIS
576
577 static void add_fd (struct procinfo *);
578
579DESCRIPTION
580
581 Add the fd of the supplied procinfo to the list of fds used for
582 poll/select operations.
583 */
584
585static void
586add_fd (pi)
587 struct procinfo *pi;
588{
589 if (num_poll_list <= 0)
590 poll_list = (struct pollfd *) xmalloc (sizeof (struct pollfd));
591 else
592 poll_list = (struct pollfd *) xrealloc (poll_list,
593 (num_poll_list + 1)
594 * sizeof (struct pollfd));
595 poll_list[num_poll_list].fd = pi->fd;
596 poll_list[num_poll_list].events = POLLPRI;
597
598 num_poll_list++;
599}
600
601static void
602remove_fd (pi)
603 struct procinfo *pi;
604{
605 int i;
606
607 for (i = 0; i < num_poll_list; i++)
608 {
609 if (poll_list[i].fd == pi->fd)
610 {
611 if (i != num_poll_list - 1)
8fc2b417 612 memcpy (poll_list + i, poll_list + i + 1,
de43d7d0
SG
613 (num_poll_list - i - 1) * sizeof (struct pollfd));
614
615 num_poll_list--;
616
617 if (num_poll_list == 0)
618 free (poll_list);
619 else
620 poll_list = (struct pollfd *) xrealloc (poll_list,
621 num_poll_list
622 * sizeof (struct pollfd));
623 return;
624 }
625 }
626}
627
7c5d526e
SG
628static struct procinfo *
629wait_fd ()
630{
631 struct procinfo *pi;
b607efe7 632#ifndef LOSING_POLL
7c5d526e
SG
633 int num_fds;
634 int i;
b607efe7 635#endif
de43d7d0 636
1e75b5f5 637 set_sigint_trap (); /* Causes SIGINT to be passed on to the
7c5d526e 638 attached process. */
429f1c9f 639 set_sigio_trap ();
de43d7d0 640
7c5d526e 641#ifndef LOSING_POLL
e172af81
FF
642 while (1)
643 {
644 num_fds = poll (poll_list, num_poll_list, -1);
645 if (num_fds > 0)
646 break;
647 if (num_fds < 0 && errno == EINTR)
648 continue;
649 print_sys_errmsg ("poll failed", errno);
650 error ("Poll failed, returned %d", num_fds);
651 }
7c5d526e
SG
652#else
653 pi = current_procinfo;
de43d7d0 654
8afd05c0 655 while (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0)
7c5d526e 656 {
cef0333e
PS
657 if (errno == ENOENT)
658 {
659 /* Process exited. */
660 pi->prstatus.pr_flags = 0;
661 break;
662 }
663 else if (errno != EINTR)
8afd05c0
JK
664 {
665 print_sys_errmsg (pi->pathname, errno);
666 error ("PIOCWSTOP failed");
667 }
7c5d526e 668 }
fb63d460 669 pi->had_event = 1;
7c5d526e
SG
670#endif
671
1e75b5f5 672 clear_sigint_trap ();
429f1c9f 673 clear_sigio_trap ();
de43d7d0 674
7c5d526e 675#ifndef LOSING_POLL
de43d7d0 676
7c5d526e
SG
677 for (i = 0; i < num_poll_list && num_fds > 0; i++)
678 {
679 if ((poll_list[i].revents & (POLLPRI|POLLERR|POLLHUP|POLLNVAL)) == 0)
680 continue;
681 for (pi = procinfo_list; pi; pi = pi->next)
682 {
683 if (poll_list[i].fd == pi->fd)
684 {
685 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
686 {
687 print_sys_errmsg (pi->pathname, errno);
688 error ("PIOCSTATUS failed");
689 }
690 num_fds--;
691 pi->had_event = 1;
692 break;
693 }
694 }
695 if (!pi)
8fc2b417 696 error ("wait_fd: Couldn't find procinfo for fd %d\n",
7c5d526e
SG
697 poll_list[i].fd);
698 }
699#endif /* LOSING_POLL */
700
701 return pi;
702}
de43d7d0
SG
703
704/*
705
cc221e76
FF
706LOCAL FUNCTION
707
708 lookupdesc -- translate a value to a summary desc string
709
710SYNOPSIS
711
712 static char *lookupdesc (struct trans *transp, unsigned int val);
713
714DESCRIPTION
715
716 Given a pointer to a translation table and a value to be translated,
717 lookup the desc string and return it.
718 */
719
720static char *
721lookupdesc (transp, val)
722 struct trans *transp;
723 unsigned int val;
724{
725 char *desc;
726
727 for (desc = NULL; transp -> name != NULL; transp++)
728 {
729 if (transp -> value == val)
730 {
731 desc = transp -> desc;
732 break;
733 }
734 }
735
736 /* Didn't find a translation for the specified value, set a default one. */
737
738 if (desc == NULL)
739 {
740 desc = "Unknown";
741 }
742 return (desc);
743}
744
745/*
746
747LOCAL FUNCTION
748
749 lookupname -- translate a value to symbolic name
750
751SYNOPSIS
752
753 static char *lookupname (struct trans *transp, unsigned int val,
754 char *prefix);
755
756DESCRIPTION
757
758 Given a pointer to a translation table, a value to be translated,
759 and a default prefix to return if the value can't be translated,
760 match the value with one of the translation table entries and
761 return a pointer to the symbolic name.
762
763 If no match is found it just returns the value as a printable string,
764 with the given prefix. The previous such value, if any, is freed
765 at this time.
766 */
767
768static char *
769lookupname (transp, val, prefix)
770 struct trans *transp;
771 unsigned int val;
772 char *prefix;
773{
774 static char *locbuf;
775 char *name;
776
777 for (name = NULL; transp -> name != NULL; transp++)
778 {
779 if (transp -> value == val)
780 {
781 name = transp -> name;
782 break;
783 }
784 }
785
786 /* Didn't find a translation for the specified value, build a default
787 one using the specified prefix and return it. The lifetime of
788 the value is only until the next one is needed. */
789
790 if (name == NULL)
791 {
792 if (locbuf != NULL)
793 {
794 free (locbuf);
795 }
796 locbuf = xmalloc (strlen (prefix) + 16);
4ed3a9ea 797 sprintf (locbuf, "%s %u", prefix, val);
cc221e76
FF
798 name = locbuf;
799 }
800 return (name);
801}
802
803static char *
804sigcodename (sip)
805 siginfo_t *sip;
806{
807 struct sigcode *scp;
808 char *name = NULL;
809 static char locbuf[32];
810
811 for (scp = siginfo_table; scp -> codename != NULL; scp++)
812 {
813 if ((scp -> signo == sip -> si_signo) &&
814 (scp -> code == sip -> si_code))
815 {
816 name = scp -> codename;
817 break;
818 }
819 }
820 if (name == NULL)
821 {
4ed3a9ea 822 sprintf (locbuf, "sigcode %u", sip -> si_signo);
cc221e76
FF
823 name = locbuf;
824 }
825 return (name);
826}
827
3fbdd536
JG
828static char *
829sigcodedesc (sip)
cc221e76
FF
830 siginfo_t *sip;
831{
832 struct sigcode *scp;
833 char *desc = NULL;
834
835 for (scp = siginfo_table; scp -> codename != NULL; scp++)
836 {
837 if ((scp -> signo == sip -> si_signo) &&
838 (scp -> code == sip -> si_code))
839 {
840 desc = scp -> desc;
841 break;
842 }
843 }
844 if (desc == NULL)
845 {
846 desc = "Unrecognized signal or trap use";
847 }
848 return (desc);
849}
850
851/*
852
853LOCAL FUNCTION
854
855 syscallname - translate a system call number into a system call name
856
857SYNOPSIS
858
859 char *syscallname (int syscallnum)
860
861DESCRIPTION
862
863 Given a system call number, translate it into the printable name
864 of a system call, or into "syscall <num>" if it is an unknown
865 number.
866 */
867
868static char *
869syscallname (syscallnum)
870 int syscallnum;
871{
872 static char locbuf[32];
cc221e76 873
8fc2b417
SG
874 if (syscallnum >= 0 && syscallnum < MAX_SYSCALLS
875 && syscall_table[syscallnum] != NULL)
876 return syscall_table[syscallnum];
cc221e76
FF
877 else
878 {
4ed3a9ea 879 sprintf (locbuf, "syscall %u", syscallnum);
8fc2b417 880 return locbuf;
cc221e76 881 }
cc221e76
FF
882}
883
884/*
885
886LOCAL FUNCTION
887
888 init_syscall_table - initialize syscall translation table
889
890SYNOPSIS
891
892 void init_syscall_table (void)
893
894DESCRIPTION
895
896 Dynamically initialize the translation table to convert system
897 call numbers into printable system call names. Done once per
898 gdb run, on initialization.
899
900NOTES
901
902 This is awfully ugly, but preprocessor tricks to make it prettier
903 tend to be nonportable.
904 */
905
906static void
907init_syscall_table ()
908{
cc221e76
FF
909#if defined (SYS_exit)
910 syscall_table[SYS_exit] = "exit";
911#endif
912#if defined (SYS_fork)
913 syscall_table[SYS_fork] = "fork";
914#endif
915#if defined (SYS_read)
916 syscall_table[SYS_read] = "read";
917#endif
918#if defined (SYS_write)
919 syscall_table[SYS_write] = "write";
920#endif
921#if defined (SYS_open)
922 syscall_table[SYS_open] = "open";
923#endif
924#if defined (SYS_close)
925 syscall_table[SYS_close] = "close";
926#endif
927#if defined (SYS_wait)
928 syscall_table[SYS_wait] = "wait";
929#endif
930#if defined (SYS_creat)
931 syscall_table[SYS_creat] = "creat";
932#endif
933#if defined (SYS_link)
934 syscall_table[SYS_link] = "link";
935#endif
936#if defined (SYS_unlink)
937 syscall_table[SYS_unlink] = "unlink";
938#endif
939#if defined (SYS_exec)
940 syscall_table[SYS_exec] = "exec";
941#endif
942#if defined (SYS_execv)
943 syscall_table[SYS_execv] = "execv";
944#endif
945#if defined (SYS_execve)
946 syscall_table[SYS_execve] = "execve";
947#endif
948#if defined (SYS_chdir)
949 syscall_table[SYS_chdir] = "chdir";
950#endif
951#if defined (SYS_time)
952 syscall_table[SYS_time] = "time";
953#endif
954#if defined (SYS_mknod)
955 syscall_table[SYS_mknod] = "mknod";
956#endif
957#if defined (SYS_chmod)
958 syscall_table[SYS_chmod] = "chmod";
959#endif
960#if defined (SYS_chown)
961 syscall_table[SYS_chown] = "chown";
962#endif
963#if defined (SYS_brk)
964 syscall_table[SYS_brk] = "brk";
965#endif
966#if defined (SYS_stat)
967 syscall_table[SYS_stat] = "stat";
968#endif
969#if defined (SYS_lseek)
970 syscall_table[SYS_lseek] = "lseek";
971#endif
972#if defined (SYS_getpid)
973 syscall_table[SYS_getpid] = "getpid";
974#endif
975#if defined (SYS_mount)
976 syscall_table[SYS_mount] = "mount";
977#endif
978#if defined (SYS_umount)
979 syscall_table[SYS_umount] = "umount";
980#endif
981#if defined (SYS_setuid)
982 syscall_table[SYS_setuid] = "setuid";
983#endif
984#if defined (SYS_getuid)
985 syscall_table[SYS_getuid] = "getuid";
986#endif
987#if defined (SYS_stime)
988 syscall_table[SYS_stime] = "stime";
989#endif
990#if defined (SYS_ptrace)
991 syscall_table[SYS_ptrace] = "ptrace";
992#endif
993#if defined (SYS_alarm)
994 syscall_table[SYS_alarm] = "alarm";
995#endif
996#if defined (SYS_fstat)
997 syscall_table[SYS_fstat] = "fstat";
998#endif
999#if defined (SYS_pause)
1000 syscall_table[SYS_pause] = "pause";
1001#endif
1002#if defined (SYS_utime)
1003 syscall_table[SYS_utime] = "utime";
1004#endif
1005#if defined (SYS_stty)
1006 syscall_table[SYS_stty] = "stty";
1007#endif
1008#if defined (SYS_gtty)
1009 syscall_table[SYS_gtty] = "gtty";
1010#endif
1011#if defined (SYS_access)
1012 syscall_table[SYS_access] = "access";
1013#endif
1014#if defined (SYS_nice)
1015 syscall_table[SYS_nice] = "nice";
1016#endif
1017#if defined (SYS_statfs)
1018 syscall_table[SYS_statfs] = "statfs";
1019#endif
1020#if defined (SYS_sync)
1021 syscall_table[SYS_sync] = "sync";
1022#endif
1023#if defined (SYS_kill)
1024 syscall_table[SYS_kill] = "kill";
1025#endif
1026#if defined (SYS_fstatfs)
1027 syscall_table[SYS_fstatfs] = "fstatfs";
1028#endif
1029#if defined (SYS_pgrpsys)
1030 syscall_table[SYS_pgrpsys] = "pgrpsys";
1031#endif
1032#if defined (SYS_xenix)
1033 syscall_table[SYS_xenix] = "xenix";
1034#endif
1035#if defined (SYS_dup)
1036 syscall_table[SYS_dup] = "dup";
1037#endif
1038#if defined (SYS_pipe)
1039 syscall_table[SYS_pipe] = "pipe";
1040#endif
1041#if defined (SYS_times)
1042 syscall_table[SYS_times] = "times";
1043#endif
1044#if defined (SYS_profil)
1045 syscall_table[SYS_profil] = "profil";
1046#endif
1047#if defined (SYS_plock)
1048 syscall_table[SYS_plock] = "plock";
1049#endif
1050#if defined (SYS_setgid)
1051 syscall_table[SYS_setgid] = "setgid";
1052#endif
1053#if defined (SYS_getgid)
1054 syscall_table[SYS_getgid] = "getgid";
1055#endif
1056#if defined (SYS_signal)
1057 syscall_table[SYS_signal] = "signal";
1058#endif
1059#if defined (SYS_msgsys)
1060 syscall_table[SYS_msgsys] = "msgsys";
1061#endif
1062#if defined (SYS_sys3b)
1063 syscall_table[SYS_sys3b] = "sys3b";
1064#endif
1065#if defined (SYS_acct)
1066 syscall_table[SYS_acct] = "acct";
1067#endif
1068#if defined (SYS_shmsys)
1069 syscall_table[SYS_shmsys] = "shmsys";
1070#endif
1071#if defined (SYS_semsys)
1072 syscall_table[SYS_semsys] = "semsys";
1073#endif
1074#if defined (SYS_ioctl)
1075 syscall_table[SYS_ioctl] = "ioctl";
1076#endif
1077#if defined (SYS_uadmin)
1078 syscall_table[SYS_uadmin] = "uadmin";
1079#endif
1080#if defined (SYS_utssys)
1081 syscall_table[SYS_utssys] = "utssys";
1082#endif
1083#if defined (SYS_fsync)
1084 syscall_table[SYS_fsync] = "fsync";
1085#endif
1086#if defined (SYS_umask)
1087 syscall_table[SYS_umask] = "umask";
1088#endif
1089#if defined (SYS_chroot)
1090 syscall_table[SYS_chroot] = "chroot";
1091#endif
1092#if defined (SYS_fcntl)
1093 syscall_table[SYS_fcntl] = "fcntl";
1094#endif
1095#if defined (SYS_ulimit)
1096 syscall_table[SYS_ulimit] = "ulimit";
1097#endif
1098#if defined (SYS_rfsys)
1099 syscall_table[SYS_rfsys] = "rfsys";
1100#endif
1101#if defined (SYS_rmdir)
1102 syscall_table[SYS_rmdir] = "rmdir";
1103#endif
1104#if defined (SYS_mkdir)
1105 syscall_table[SYS_mkdir] = "mkdir";
1106#endif
1107#if defined (SYS_getdents)
1108 syscall_table[SYS_getdents] = "getdents";
1109#endif
1110#if defined (SYS_sysfs)
1111 syscall_table[SYS_sysfs] = "sysfs";
1112#endif
1113#if defined (SYS_getmsg)
1114 syscall_table[SYS_getmsg] = "getmsg";
1115#endif
1116#if defined (SYS_putmsg)
1117 syscall_table[SYS_putmsg] = "putmsg";
1118#endif
1119#if defined (SYS_poll)
1120 syscall_table[SYS_poll] = "poll";
1121#endif
1122#if defined (SYS_lstat)
1123 syscall_table[SYS_lstat] = "lstat";
1124#endif
1125#if defined (SYS_symlink)
1126 syscall_table[SYS_symlink] = "symlink";
1127#endif
1128#if defined (SYS_readlink)
1129 syscall_table[SYS_readlink] = "readlink";
1130#endif
1131#if defined (SYS_setgroups)
1132 syscall_table[SYS_setgroups] = "setgroups";
1133#endif
1134#if defined (SYS_getgroups)
1135 syscall_table[SYS_getgroups] = "getgroups";
1136#endif
1137#if defined (SYS_fchmod)
1138 syscall_table[SYS_fchmod] = "fchmod";
1139#endif
1140#if defined (SYS_fchown)
1141 syscall_table[SYS_fchown] = "fchown";
1142#endif
1143#if defined (SYS_sigprocmask)
1144 syscall_table[SYS_sigprocmask] = "sigprocmask";
1145#endif
1146#if defined (SYS_sigsuspend)
1147 syscall_table[SYS_sigsuspend] = "sigsuspend";
1148#endif
1149#if defined (SYS_sigaltstack)
1150 syscall_table[SYS_sigaltstack] = "sigaltstack";
1151#endif
1152#if defined (SYS_sigaction)
1153 syscall_table[SYS_sigaction] = "sigaction";
1154#endif
1155#if defined (SYS_sigpending)
1156 syscall_table[SYS_sigpending] = "sigpending";
1157#endif
1158#if defined (SYS_context)
1159 syscall_table[SYS_context] = "context";
1160#endif
1161#if defined (SYS_evsys)
1162 syscall_table[SYS_evsys] = "evsys";
1163#endif
1164#if defined (SYS_evtrapret)
1165 syscall_table[SYS_evtrapret] = "evtrapret";
1166#endif
1167#if defined (SYS_statvfs)
1168 syscall_table[SYS_statvfs] = "statvfs";
1169#endif
1170#if defined (SYS_fstatvfs)
1171 syscall_table[SYS_fstatvfs] = "fstatvfs";
1172#endif
1173#if defined (SYS_nfssys)
1174 syscall_table[SYS_nfssys] = "nfssys";
1175#endif
1176#if defined (SYS_waitsys)
1177 syscall_table[SYS_waitsys] = "waitsys";
1178#endif
1179#if defined (SYS_sigsendsys)
1180 syscall_table[SYS_sigsendsys] = "sigsendsys";
1181#endif
1182#if defined (SYS_hrtsys)
1183 syscall_table[SYS_hrtsys] = "hrtsys";
1184#endif
1185#if defined (SYS_acancel)
1186 syscall_table[SYS_acancel] = "acancel";
1187#endif
1188#if defined (SYS_async)
1189 syscall_table[SYS_async] = "async";
1190#endif
1191#if defined (SYS_priocntlsys)
1192 syscall_table[SYS_priocntlsys] = "priocntlsys";
1193#endif
1194#if defined (SYS_pathconf)
1195 syscall_table[SYS_pathconf] = "pathconf";
1196#endif
1197#if defined (SYS_mincore)
1198 syscall_table[SYS_mincore] = "mincore";
1199#endif
1200#if defined (SYS_mmap)
1201 syscall_table[SYS_mmap] = "mmap";
1202#endif
1203#if defined (SYS_mprotect)
1204 syscall_table[SYS_mprotect] = "mprotect";
1205#endif
1206#if defined (SYS_munmap)
1207 syscall_table[SYS_munmap] = "munmap";
1208#endif
1209#if defined (SYS_fpathconf)
1210 syscall_table[SYS_fpathconf] = "fpathconf";
1211#endif
1212#if defined (SYS_vfork)
1213 syscall_table[SYS_vfork] = "vfork";
1214#endif
1215#if defined (SYS_fchdir)
1216 syscall_table[SYS_fchdir] = "fchdir";
1217#endif
1218#if defined (SYS_readv)
1219 syscall_table[SYS_readv] = "readv";
1220#endif
1221#if defined (SYS_writev)
1222 syscall_table[SYS_writev] = "writev";
1223#endif
1224#if defined (SYS_xstat)
1225 syscall_table[SYS_xstat] = "xstat";
1226#endif
1227#if defined (SYS_lxstat)
1228 syscall_table[SYS_lxstat] = "lxstat";
1229#endif
1230#if defined (SYS_fxstat)
1231 syscall_table[SYS_fxstat] = "fxstat";
1232#endif
1233#if defined (SYS_xmknod)
1234 syscall_table[SYS_xmknod] = "xmknod";
1235#endif
1236#if defined (SYS_clocal)
1237 syscall_table[SYS_clocal] = "clocal";
1238#endif
1239#if defined (SYS_setrlimit)
1240 syscall_table[SYS_setrlimit] = "setrlimit";
1241#endif
1242#if defined (SYS_getrlimit)
1243 syscall_table[SYS_getrlimit] = "getrlimit";
1244#endif
1245#if defined (SYS_lchown)
1246 syscall_table[SYS_lchown] = "lchown";
1247#endif
1248#if defined (SYS_memcntl)
1249 syscall_table[SYS_memcntl] = "memcntl";
1250#endif
1251#if defined (SYS_getpmsg)
1252 syscall_table[SYS_getpmsg] = "getpmsg";
1253#endif
1254#if defined (SYS_putpmsg)
1255 syscall_table[SYS_putpmsg] = "putpmsg";
1256#endif
1257#if defined (SYS_rename)
1258 syscall_table[SYS_rename] = "rename";
1259#endif
1260#if defined (SYS_uname)
1261 syscall_table[SYS_uname] = "uname";
1262#endif
1263#if defined (SYS_setegid)
1264 syscall_table[SYS_setegid] = "setegid";
1265#endif
1266#if defined (SYS_sysconfig)
1267 syscall_table[SYS_sysconfig] = "sysconfig";
1268#endif
1269#if defined (SYS_adjtime)
1270 syscall_table[SYS_adjtime] = "adjtime";
1271#endif
1272#if defined (SYS_systeminfo)
1273 syscall_table[SYS_systeminfo] = "systeminfo";
1274#endif
1275#if defined (SYS_seteuid)
1276 syscall_table[SYS_seteuid] = "seteuid";
1277#endif
de43d7d0
SG
1278#if defined (SYS_sproc)
1279 syscall_table[SYS_sproc] = "sproc";
1280#endif
cc221e76 1281}
35f5886e
FF
1282
1283/*
1284
3fbdd536 1285LOCAL FUNCTION
35f5886e 1286
3fbdd536 1287 procfs_kill_inferior - kill any currently inferior
35f5886e
FF
1288
1289SYNOPSIS
1290
3fbdd536 1291 void procfs_kill_inferior (void)
35f5886e
FF
1292
1293DESCRIPTION
1294
1295 Kill any current inferior.
1296
1297NOTES
1298
1299 Kills even attached inferiors. Presumably the user has already
1300 been prompted that the inferior is an attached one rather than
1301 one started by gdb. (FIXME?)
1302
1303*/
1304
3fbdd536
JG
1305static void
1306procfs_kill_inferior ()
35f5886e 1307{
de43d7d0 1308 target_mourn_inferior ();
35f5886e
FF
1309}
1310
1311/*
1312
1313LOCAL FUNCTION
1314
1315 unconditionally_kill_inferior - terminate the inferior
1316
1317SYNOPSIS
1318
de43d7d0 1319 static void unconditionally_kill_inferior (struct procinfo *)
35f5886e
FF
1320
1321DESCRIPTION
1322
de43d7d0 1323 Kill the specified inferior.
35f5886e
FF
1324
1325NOTE
1326
1327 A possibly useful enhancement would be to first try sending
1328 the inferior a terminate signal, politely asking it to commit
de43d7d0
SG
1329 suicide, before we murder it (we could call that
1330 politely_kill_inferior()).
35f5886e
FF
1331
1332*/
1333
1334static void
de43d7d0
SG
1335unconditionally_kill_inferior (pi)
1336 struct procinfo *pi;
35f5886e
FF
1337{
1338 int signo;
de43d7d0 1339 int ppid;
35f5886e 1340
de43d7d0
SG
1341 ppid = pi->prstatus.pr_ppid;
1342
35f5886e 1343 signo = SIGKILL;
2592eef8 1344
b6753b3f
PS
1345#ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
1346 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
1347 before the PIOCKILL, otherwise it might generate a corrupted core
1348 file for the inferior. */
1349 ioctl (pi->fd, PIOCSSIG, NULL);
1350#endif
2592eef8 1351#ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
b6753b3f 1352 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
f5de4904
PS
1353 to kill the inferior, otherwise it might remain stopped with a
1354 pending SIGKILL.
2592eef8
PS
1355 We do not check the result of the PIOCSSIG, the inferior might have
1356 died already. */
1357 {
1358 struct siginfo newsiginfo;
1359
1360 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
1361 newsiginfo.si_signo = signo;
1362 newsiginfo.si_code = 0;
1363 newsiginfo.si_errno = 0;
1364 newsiginfo.si_pid = getpid ();
1365 newsiginfo.si_uid = getuid ();
1366 ioctl (pi->fd, PIOCSSIG, &newsiginfo);
1367 }
f5de4904
PS
1368#else
1369 ioctl (pi->fd, PIOCKILL, &signo);
2592eef8
PS
1370#endif
1371
de43d7d0
SG
1372 close_proc_file (pi);
1373
1374/* Only wait() for our direct children. Our grandchildren zombies are killed
1375 by the death of their parents. */
1376
1377 if (ppid == getpid())
1378 wait ((int *) 0);
35f5886e
FF
1379}
1380
1381/*
1382
3fbdd536 1383LOCAL FUNCTION
35f5886e 1384
3fbdd536 1385 procfs_xfer_memory -- copy data to or from inferior memory space
35f5886e
FF
1386
1387SYNOPSIS
1388
3fbdd536 1389 int procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
35f5886e
FF
1390 int dowrite, struct target_ops target)
1391
1392DESCRIPTION
1393
1394 Copy LEN bytes to/from inferior's memory starting at MEMADDR
1395 from/to debugger memory starting at MYADDR. Copy from inferior
1396 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
1397
1398 Returns the length copied, which is either the LEN argument or
3fbdd536 1399 zero. This xfer function does not do partial moves, since procfs_ops
35f5886e
FF
1400 doesn't allow memory operations to cross below us in the target stack
1401 anyway.
1402
1403NOTES
1404
1405 The /proc interface makes this an almost trivial task.
1406 */
1407
3fbdd536
JG
1408static int
1409procfs_xfer_memory (memaddr, myaddr, len, dowrite, target)
1ab3bf1b
JG
1410 CORE_ADDR memaddr;
1411 char *myaddr;
1412 int len;
1413 int dowrite;
1414 struct target_ops *target; /* ignored */
35f5886e
FF
1415{
1416 int nbytes = 0;
de43d7d0 1417 struct procinfo *pi;
35f5886e 1418
de43d7d0
SG
1419 pi = current_procinfo;
1420
1421 if (lseek(pi->fd, (off_t) memaddr, 0) == (off_t) memaddr)
35f5886e
FF
1422 {
1423 if (dowrite)
1424 {
de43d7d0 1425 nbytes = write (pi->fd, myaddr, len);
35f5886e
FF
1426 }
1427 else
1428 {
de43d7d0 1429 nbytes = read (pi->fd, myaddr, len);
35f5886e
FF
1430 }
1431 if (nbytes < 0)
1432 {
1433 nbytes = 0;
1434 }
1435 }
1436 return (nbytes);
1437}
1438
1439/*
1440
3fbdd536 1441LOCAL FUNCTION
35f5886e 1442
3fbdd536 1443 procfs_store_registers -- copy register values back to inferior
35f5886e
FF
1444
1445SYNOPSIS
1446
3fbdd536 1447 void procfs_store_registers (int regno)
35f5886e
FF
1448
1449DESCRIPTION
1450
1451 Store our current register values back into the inferior. If
1452 REGNO is -1 then store all the register, otherwise store just
1453 the value specified by REGNO.
1454
1455NOTES
1456
1457 If we are storing only a single register, we first have to get all
1458 the current values from the process, overwrite the desired register
1459 in the gregset with the one we want from gdb's registers, and then
1460 send the whole set back to the process. For writing all the
1461 registers, all we have to do is generate the gregset and send it to
1462 the process.
1463
1464 Also note that the process has to be stopped on an event of interest
1465 for this to work, which basically means that it has to have been
1466 run under the control of one of the other /proc ioctl calls and not
1467 ptrace. Since we don't use ptrace anyway, we don't worry about this
1468 fine point, but it is worth noting for future reference.
1469
1470 Gdb is confused about what this function is supposed to return.
1471 Some versions return a value, others return nothing. Some are
1472 declared to return a value and actually return nothing. Gdb ignores
1473 anything returned. (FIXME)
1474
1475 */
1476
3fbdd536
JG
1477static void
1478procfs_store_registers (regno)
1ab3bf1b 1479 int regno;
35f5886e 1480{
de43d7d0
SG
1481 struct procinfo *pi;
1482
1483 pi = current_procinfo;
1484
35f5886e
FF
1485 if (regno != -1)
1486 {
de43d7d0 1487 ioctl (pi->fd, PIOCGREG, &pi->gregset);
35f5886e 1488 }
de43d7d0
SG
1489 fill_gregset (&pi->gregset, regno);
1490 ioctl (pi->fd, PIOCSREG, &pi->gregset);
35f5886e
FF
1491
1492#if defined (FP0_REGNUM)
1493
1494 /* Now repeat everything using the floating point register set, if the
1495 target has floating point hardware. Since we ignore the returned value,
1496 we'll never know whether it worked or not anyway. */
1497
1498 if (regno != -1)
1499 {
de43d7d0 1500 ioctl (pi->fd, PIOCGFPREG, &pi->fpregset);
35f5886e 1501 }
de43d7d0
SG
1502 fill_fpregset (&pi->fpregset, regno);
1503 ioctl (pi->fd, PIOCSFPREG, &pi->fpregset);
35f5886e
FF
1504
1505#endif /* FP0_REGNUM */
1506
1507}
1508
1509/*
1510
3fbdd536 1511LOCAL FUNCTION
35f5886e 1512
de43d7d0
SG
1513 create_procinfo - initialize access to a /proc entry
1514
1515SYNOPSIS
1516
eca4a350 1517 struct procinfo * create_procinfo (int pid)
de43d7d0
SG
1518
1519DESCRIPTION
1520
eca4a350
SG
1521 Allocate a procinfo structure, open the /proc file and then set up the
1522 set of signals and faults that are to be traced. Returns a pointer to
1523 the new procinfo structure.
de43d7d0
SG
1524
1525NOTES
1526
1527 If proc_init_failed ever gets called, control returns to the command
1528 processing loop via the standard error handling code.
1529
1530 */
1531
eca4a350 1532static struct procinfo *
de43d7d0
SG
1533create_procinfo (pid)
1534 int pid;
1535{
1536 struct procinfo *pi;
1537
2592eef8
PS
1538 pi = find_procinfo (pid, 1);
1539 if (pi != NULL)
1540 return pi; /* All done! It already exists */
de43d7d0
SG
1541
1542 pi = (struct procinfo *) xmalloc (sizeof (struct procinfo));
1543
1544 if (!open_proc_file (pid, pi, O_RDWR))
1545 proc_init_failed (pi, "can't open process file");
1546
8fc2b417
SG
1547 /* open_proc_file may modify pid. */
1548
1549 pid = pi -> pid;
1550
de43d7d0
SG
1551 /* Add new process to process info list */
1552
1553 pi->next = procinfo_list;
1554 procinfo_list = pi;
1555
1556 add_fd (pi); /* Add to list for poll/select */
1557
8fc2b417
SG
1558 pi->num_syscall_handlers = 0;
1559 pi->syscall_handlers = NULL;
de43d7d0
SG
1560 memset ((char *) &pi->prrun, 0, sizeof (pi->prrun));
1561 prfillset (&pi->prrun.pr_trace);
1562 procfs_notice_signals (pid);
1563 prfillset (&pi->prrun.pr_fault);
1564 prdelset (&pi->prrun.pr_fault, FLTPAGE);
1565
b9e58503
PS
1566#ifdef PROCFS_DONT_TRACE_FAULTS
1567 premptyset (&pi->prrun.pr_fault);
2592eef8
PS
1568#endif
1569
8fc2b417
SG
1570 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
1571 proc_init_failed (pi, "PIOCSTATUS failed");
1572
1573/* A bug in Solaris (2.5 at least) causes PIOCWSTOP to hang on LWPs that are
1574 already stopped, even if they all have PR_ASYNC set. */
1575
1576 if (!(pi->prstatus.pr_flags & PR_STOPPED))
1577 if (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0)
1578 proc_init_failed (pi, "PIOCWSTOP failed");
de43d7d0
SG
1579
1580 if (ioctl (pi->fd, PIOCSFAULT, &pi->prrun.pr_fault) < 0)
1581 proc_init_failed (pi, "PIOCSFAULT failed");
eca4a350
SG
1582
1583 return pi;
de43d7d0
SG
1584}
1585
1586/*
1587
8fc2b417
SG
1588LOCAL FUNCTION
1589
1590 procfs_exit_handler - handle entry into the _exit syscall
1591
1592SYNOPSIS
1593
1594 int procfs_exit_handler (pi, syscall_num, why, rtnvalp, statvalp)
1595
1596DESCRIPTION
1597
1598 This routine is called when an inferior process enters the _exit()
1599 system call. It continues the process, and then collects the exit
1600 status and pid which are returned in *statvalp and *rtnvalp. After
1601 that it returns non-zero to indicate that procfs_wait should wake up.
1602
1603NOTES
1604 There is probably a better way to do this.
1605
1606 */
1607
1608static int
1609procfs_exit_handler (pi, syscall_num, why, rtnvalp, statvalp)
1610 struct procinfo *pi;
1611 int syscall_num;
1612 int why;
1613 int *rtnvalp;
1614 int *statvalp;
1615{
1616 pi->prrun.pr_flags = PRCFAULT;
1617
1618 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
1619 perror_with_name (pi->pathname);
1620
1621 *rtnvalp = wait (statvalp);
1622 if (*rtnvalp >= 0)
1623 *rtnvalp = pi->pid;
1624
1625 return 1;
1626}
1627
1628/*
1629
1630LOCAL FUNCTION
1631
1632 procfs_exec_handler - handle exit from the exec family of syscalls
1633
1634SYNOPSIS
1635
1636 int procfs_exec_handler (pi, syscall_num, why, rtnvalp, statvalp)
1637
1638DESCRIPTION
1639
1640 This routine is called when an inferior process is about to finish any
1641 of the exec() family of system calls. It pretends that we got a
1642 SIGTRAP (for compatibility with ptrace behavior), and returns non-zero
1643 to tell procfs_wait to wake up.
1644
1645NOTES
1646 This need for compatibility with ptrace is questionable. In the
1647 future, it shouldn't be necessary.
1648
1649 */
1650
1651static int
1652procfs_exec_handler (pi, syscall_num, why, rtnvalp, statvalp)
1653 struct procinfo *pi;
1654 int syscall_num;
1655 int why;
1656 int *rtnvalp;
1657 int *statvalp;
1658{
1659 *statvalp = (SIGTRAP << 8) | 0177;
1660
1661 return 1;
1662}
1663
1664#ifdef SYS_sproc /* IRIX lwp creation system call */
1665
1666/*
1667
1668LOCAL FUNCTION
1669
1670 procfs_sproc_handler - handle exit from the sproc syscall
1671
1672SYNOPSIS
1673
1674 int procfs_sproc_handler (pi, syscall_num, why, rtnvalp, statvalp)
1675
1676DESCRIPTION
1677
1678 This routine is called when an inferior process is about to finish an
1679 sproc() system call. This is the system call that IRIX uses to create
1680 a lightweight process. When the target process gets this event, we can
1681 look at rval1 to find the new child processes ID, and create a new
1682 procinfo struct from that.
1683
1684 After that, it pretends that we got a SIGTRAP, and returns non-zero
1685 to tell procfs_wait to wake up. Subsequently, wait_for_inferior gets
1686 woken up, sees the new process and continues it.
1687
1688NOTES
1689 We actually never see the child exiting from sproc because we will
1690 shortly stop the child with PIOCSTOP, which is then registered as the
1691 event of interest.
1692 */
1693
1694static int
1695procfs_sproc_handler (pi, syscall_num, why, rtnvalp, statvalp)
1696 struct procinfo *pi;
1697 int syscall_num;
1698 int why;
1699 int *rtnvalp;
1700 int *statvalp;
1701{
1702/* We've just detected the completion of an sproc system call. Now we need to
1703 setup a procinfo struct for this thread, and notify the thread system of the
1704 new arrival. */
1705
1706/* If sproc failed, then nothing interesting happened. Continue the process
1707 and go back to sleep. */
1708
1709 if (pi->prstatus.pr_errno != 0)
1710 {
1711 pi->prrun.pr_flags &= PRSTEP;
1712 pi->prrun.pr_flags |= PRCFAULT;
1713
1714 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
1715 perror_with_name (pi->pathname);
1716
1717 return 0;
1718 }
1719
1720 /* At this point, the new thread is stopped at it's first instruction, and
1721 the parent is stopped at the exit from sproc. */
1722
1723 /* Notify the caller of the arrival of a new thread. */
1724 create_procinfo (pi->prstatus.pr_rval1);
1725
1726 *rtnvalp = pi->prstatus.pr_rval1;
1727 *statvalp = (SIGTRAP << 8) | 0177;
1728
1729 return 1;
1730}
1731
1732/*
1733
1734LOCAL FUNCTION
1735
1736 procfs_fork_handler - handle exit from the fork syscall
1737
1738SYNOPSIS
1739
1740 int procfs_fork_handler (pi, syscall_num, why, rtnvalp, statvalp)
1741
1742DESCRIPTION
1743
1744 This routine is called when an inferior process is about to finish a
1745 fork() system call. We will open up the new process, and then close
1746 it, which releases it from the clutches of the debugger.
1747
1748 After that, we continue the target process as though nothing had
1749 happened.
1750
1751NOTES
1752 This is necessary for IRIX because we have to set PR_FORK in order
1753 to catch the creation of lwps (via sproc()). When an actual fork
1754 occurs, it becomes necessary to reset the forks debugger flags and
1755 continue it because we can't hack multiple processes yet.
1756 */
1757
1758static int
1759procfs_fork_handler (pi, syscall_num, why, rtnvalp, statvalp)
1760 struct procinfo *pi;
1761 int syscall_num;
1762 int why;
1763 int *rtnvalp;
1764 int *statvalp;
1765{
1766 struct procinfo *pitemp;
1767
1768/* At this point, we've detected the completion of a fork (or vfork) call in
1769 our child. The grandchild is also stopped because we set inherit-on-fork
1770 earlier. (Note that nobody has the grandchilds' /proc file open at this
1771 point.) We will release the grandchild from the debugger by opening it's
1772 /proc file and then closing it. Since run-on-last-close is set, the
1773 grandchild continues on its' merry way. */
1774
1775
1776 pitemp = create_procinfo (pi->prstatus.pr_rval1);
1777 if (pitemp)
1778 close_proc_file (pitemp);
1779
1780 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
1781 perror_with_name (pi->pathname);
1782
1783 return 0;
1784}
1785#endif /* SYS_sproc */
1786
1787/*
1788
de43d7d0
SG
1789LOCAL FUNCTION
1790
1791 procfs_init_inferior - initialize target vector and access to a
1792 /proc entry
35f5886e
FF
1793
1794SYNOPSIS
1795
8fc2b417 1796 int procfs_init_inferior (int pid)
35f5886e
FF
1797
1798DESCRIPTION
1799
1800 When gdb starts an inferior, this function is called in the parent
1801 process immediately after the fork. It waits for the child to stop
1802 on the return from the exec system call (the child itself takes care
1803 of ensuring that this is set up), then sets up the set of signals
8fc2b417
SG
1804 and faults that are to be traced. Returns the pid, which may have had
1805 the thread-id added to it.
35f5886e
FF
1806
1807NOTES
1808
1809 If proc_init_failed ever gets called, control returns to the command
1810 processing loop via the standard error handling code.
cc221e76 1811
35f5886e
FF
1812 */
1813
8fc2b417 1814static int
3fbdd536 1815procfs_init_inferior (pid)
1ab3bf1b 1816 int pid;
35f5886e 1817{
8fc2b417
SG
1818 struct procinfo *pip;
1819
3fbdd536
JG
1820 push_target (&procfs_ops);
1821
8fc2b417
SG
1822 pip = create_procinfo (pid);
1823
1cb1b16c 1824#ifndef PIOCSSPCACT
8fc2b417
SG
1825 procfs_set_syscall_trap (pip, SYS_exit, PROCFS_SYSCALL_ENTRY,
1826 procfs_exit_handler);
1827
8fc2b417
SG
1828#ifdef SYS_exec
1829 procfs_set_syscall_trap (pip, SYS_exec, PROCFS_SYSCALL_EXIT,
1830 procfs_exec_handler);
1831#endif
1832#ifdef SYS_execv
1833 procfs_set_syscall_trap (pip, SYS_execv, PROCFS_SYSCALL_EXIT,
1834 procfs_exec_handler);
1835#endif
1836#ifdef SYS_execve
1837 procfs_set_syscall_trap (pip, SYS_execve, PROCFS_SYSCALL_EXIT,
1838 procfs_exec_handler);
1839#endif
1cb1b16c 1840#endif /* PIOCSSPCACT */
8fc2b417
SG
1841
1842 /* Setup traps on exit from sproc() */
1843
1844#ifdef SYS_sproc
1845 procfs_set_syscall_trap (pip, SYS_sproc, PROCFS_SYSCALL_EXIT,
1846 procfs_sproc_handler);
1847 procfs_set_syscall_trap (pip, SYS_fork, PROCFS_SYSCALL_EXIT,
1848 procfs_fork_handler);
1849#ifdef SYS_vfork
1850 procfs_set_syscall_trap (pip, SYS_vfork, PROCFS_SYSCALL_EXIT,
1851 procfs_fork_handler);
1852#endif
1853/* Turn on inherit-on-fork flag so that all children of the target process
1854 start with tracing flags set. This allows us to trap lwp creation. Note
1855 that we also have to trap on fork and vfork in order to disable all tracing
1856 in the targets child processes. */
1857
1858 modify_inherit_on_fork_flag (pip->fd, 1);
1859#endif
1860
1861#ifdef SYS_lwp_create
1862 procfs_set_syscall_trap (pip, SYS_lwp_create, PROCFS_SYSCALL_EXIT,
1863 procfs_lwp_creation_handler);
1864#endif
1865
1866 /* create_procinfo may change the pid, so we have to update inferior_pid
1867 here before calling other gdb routines that need the right pid. */
1868
1869 pid = pip -> pid;
1870 inferior_pid = pid;
1871
1872 add_thread (pip -> pid); /* Setup initial thread */
bc28a06c 1873
2592eef8
PS
1874#ifdef START_INFERIOR_TRAPS_EXPECTED
1875 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
1876#else
bc28a06c
JK
1877 /* One trap to exec the shell, one to exec the program being debugged. */
1878 startup_inferior (2);
2592eef8 1879#endif
8fc2b417
SG
1880
1881 return pid;
35f5886e
FF
1882}
1883
1884/*
1885
cc221e76
FF
1886GLOBAL FUNCTION
1887
3950a34e 1888 procfs_notice_signals
cc221e76
FF
1889
1890SYNOPSIS
1891
952a820e 1892 static void procfs_notice_signals (int pid);
cc221e76
FF
1893
1894DESCRIPTION
1895
1896 When the user changes the state of gdb's signal handling via the
1897 "handle" command, this function gets called to see if any change
1898 in the /proc interface is required. It is also called internally
1899 by other /proc interface functions to initialize the state of
1900 the traced signal set.
1901
1902 One thing it does is that signals for which the state is "nostop",
1903 "noprint", and "pass", have their trace bits reset in the pr_trace
1904 field, so that they are no longer traced. This allows them to be
1905 delivered directly to the inferior without the debugger ever being
1906 involved.
1907 */
1908
3950a34e 1909static void
de43d7d0 1910procfs_notice_signals (pid)
952a820e 1911 int pid;
cc221e76
FF
1912{
1913 int signo;
de43d7d0 1914 struct procinfo *pi;
cc221e76 1915
de43d7d0
SG
1916 pi = find_procinfo (pid, 0);
1917
1918 for (signo = 0; signo < NSIG; signo++)
cc221e76 1919 {
67ac9759
JK
1920 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
1921 signal_print_state (target_signal_from_host (signo)) == 0 &&
1922 signal_pass_state (target_signal_from_host (signo)) == 1)
cc221e76 1923 {
de43d7d0 1924 prdelset (&pi->prrun.pr_trace, signo);
cc221e76 1925 }
de43d7d0 1926 else
cc221e76 1927 {
de43d7d0 1928 praddset (&pi->prrun.pr_trace, signo);
cc221e76
FF
1929 }
1930 }
de43d7d0
SG
1931 if (ioctl (pi->fd, PIOCSTRACE, &pi->prrun.pr_trace))
1932 {
1933 print_sys_errmsg ("PIOCSTRACE failed", errno);
1934 }
cc221e76
FF
1935}
1936
1937/*
1938
3fbdd536 1939LOCAL FUNCTION
35f5886e
FF
1940
1941 proc_set_exec_trap -- arrange for exec'd child to halt at startup
1942
1943SYNOPSIS
1944
1945 void proc_set_exec_trap (void)
1946
1947DESCRIPTION
1948
1949 This function is called in the child process when starting up
1950 an inferior, prior to doing the exec of the actual inferior.
1951 It sets the child process's exitset to make exit from the exec
1952 system call an event of interest to stop on, and then simply
1953 returns. The child does the exec, the system call returns, and
1954 the child stops at the first instruction, ready for the gdb
1955 parent process to take control of it.
1956
1957NOTE
1958
1959 We need to use all local variables since the child may be sharing
1960 it's data space with the parent, if vfork was used rather than
1961 fork.
cc221e76
FF
1962
1963 Also note that we want to turn off the inherit-on-fork flag in
1964 the child process so that any grand-children start with all
1965 tracing flags cleared.
35f5886e
FF
1966 */
1967
3fbdd536 1968static void
1ab3bf1b 1969proc_set_exec_trap ()
35f5886e
FF
1970{
1971 sysset_t exitset;
fb63d460 1972 sysset_t entryset;
35f5886e
FF
1973 auto char procname[32];
1974 int fd;
1975
4ed3a9ea 1976 sprintf (procname, PROC_NAME_FMT, getpid ());
35f5886e
FF
1977 if ((fd = open (procname, O_RDWR)) < 0)
1978 {
1979 perror (procname);
199b2450 1980 gdb_flush (gdb_stderr);
35f5886e
FF
1981 _exit (127);
1982 }
1983 premptyset (&exitset);
fb63d460 1984 premptyset (&entryset);
407a8389 1985
2592eef8
PS
1986#ifdef PIOCSSPCACT
1987 /* Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
1cb1b16c
PS
1988 exits from exec system calls because of the user level loader.
1989 Starting with OSF/1-4.0, tracing the entry to the exit system
1990 call no longer works. So we have to use PRFS_STOPTERM to trace
1991 termination of the inferior. */
2592eef8
PS
1992 {
1993 int prfs_flags;
1994
1995 if (ioctl (fd, PIOCGSPCACT, &prfs_flags) < 0)
1996 {
1997 perror (procname);
1998 gdb_flush (gdb_stderr);
1999 _exit (127);
2000 }
1cb1b16c 2001 prfs_flags |= PRFS_STOPEXEC | PRFS_STOPTERM;
2592eef8
PS
2002 if (ioctl (fd, PIOCSSPCACT, &prfs_flags) < 0)
2003 {
2004 perror (procname);
2005 gdb_flush (gdb_stderr);
2006 _exit (127);
2007 }
2008 }
1cb1b16c 2009#else /* PIOCSSPCACT */
cc221e76
FF
2010 /* GW: Rationale...
2011 Not all systems with /proc have all the exec* syscalls with the same
2012 names. On the SGI, for example, there is no SYS_exec, but there
2013 *is* a SYS_execv. So, we try to account for that. */
2014
407a8389 2015#ifdef SYS_exec
35f5886e 2016 praddset (&exitset, SYS_exec);
407a8389
SG
2017#endif
2018#ifdef SYS_execve
35f5886e 2019 praddset (&exitset, SYS_execve);
407a8389
SG
2020#endif
2021#ifdef SYS_execv
fb63d460 2022 praddset (&exitset, SYS_execv);
407a8389
SG
2023#endif
2024
35f5886e
FF
2025 if (ioctl (fd, PIOCSEXIT, &exitset) < 0)
2026 {
2027 perror (procname);
199b2450 2028 gdb_flush (gdb_stderr);
35f5886e
FF
2029 _exit (127);
2030 }
cc221e76 2031
fb63d460
SG
2032 praddset (&entryset, SYS_exit);
2033
2034 if (ioctl (fd, PIOCSENTRY, &entryset) < 0)
2035 {
2036 perror (procname);
199b2450 2037 gdb_flush (gdb_stderr);
fb63d460
SG
2038 _exit (126);
2039 }
1cb1b16c 2040#endif /* PIOCSSPCACT */
fb63d460 2041
cc221e76
FF
2042 /* Turn off inherit-on-fork flag so that all grand-children of gdb
2043 start with tracing flags cleared. */
2044
8fc2b417 2045 modify_inherit_on_fork_flag (fd, 0);
ec8ceca3
JG
2046
2047 /* Turn on run-on-last-close flag so that this process will not hang
2048 if GDB goes away for some reason. */
2049
8fc2b417
SG
2050 modify_run_on_last_close_flag (fd, 1);
2051
2052#ifdef PR_ASYNC
ec8ceca3 2053 {
8fc2b417
SG
2054 long pr_flags;
2055
2056/* Solaris needs this to make procfs treat all threads seperately. Without
2057 this, all threads halt whenever something happens to any thread. Since
2058 GDB wants to control all this itself, it needs to set PR_ASYNC. */
2059
2060 pr_flags = PR_ASYNC;
2061
2062 ioctl (fd, PIOCSET, &pr_flags);
ec8ceca3 2063 }
8fc2b417 2064#endif /* PR_ASYNC */
35f5886e
FF
2065}
2066
f8b76e70
FF
2067/*
2068
a39ad5ce
FF
2069GLOBAL FUNCTION
2070
2071 proc_iterate_over_mappings -- call function for every mapped space
2072
2073SYNOPSIS
2074
2075 int proc_iterate_over_mappings (int (*func)())
2076
2077DESCRIPTION
2078
2079 Given a pointer to a function, call that function for every
2080 mapped address space, passing it an open file descriptor for
2081 the file corresponding to that mapped address space (if any)
2082 and the base address of the mapped space. Quit when we hit
2083 the end of the mappings or the function returns nonzero.
2084 */
2085
2086int
1ab3bf1b
JG
2087proc_iterate_over_mappings (func)
2088 int (*func) PARAMS ((int, CORE_ADDR));
a39ad5ce
FF
2089{
2090 int nmap;
2091 int fd;
2092 int funcstat = 0;
2093 struct prmap *prmaps;
2094 struct prmap *prmap;
de43d7d0
SG
2095 struct procinfo *pi;
2096
2097 pi = current_procinfo;
a39ad5ce 2098
de43d7d0 2099 if (ioctl (pi->fd, PIOCNMAP, &nmap) == 0)
a39ad5ce 2100 {
1ab3bf1b 2101 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
de43d7d0 2102 if (ioctl (pi->fd, PIOCMAP, prmaps) == 0)
a39ad5ce
FF
2103 {
2104 for (prmap = prmaps; prmap -> pr_size && funcstat == 0; ++prmap)
2105 {
de43d7d0 2106 fd = proc_address_to_fd (pi, (CORE_ADDR) prmap -> pr_vaddr, 0);
1ab3bf1b 2107 funcstat = (*func) (fd, (CORE_ADDR) prmap -> pr_vaddr);
a39ad5ce
FF
2108 close (fd);
2109 }
2110 }
2111 }
2112 return (funcstat);
2113}
2114
3fbdd536 2115#if 0 /* Currently unused */
a39ad5ce
FF
2116/*
2117
f8b76e70
FF
2118GLOBAL FUNCTION
2119
2120 proc_base_address -- find base address for segment containing address
2121
2122SYNOPSIS
2123
2124 CORE_ADDR proc_base_address (CORE_ADDR addr)
2125
2126DESCRIPTION
2127
2128 Given an address of a location in the inferior, find and return
2129 the base address of the mapped segment containing that address.
2130
2131 This is used for example, by the shared library support code,
2132 where we have the pc value for some location in the shared library
2133 where we are stopped, and need to know the base address of the
2134 segment containing that address.
2135*/
2136
f8b76e70 2137CORE_ADDR
1ab3bf1b 2138proc_base_address (addr)
cc221e76 2139 CORE_ADDR addr;
f8b76e70
FF
2140{
2141 int nmap;
2142 struct prmap *prmaps;
2143 struct prmap *prmap;
2144 CORE_ADDR baseaddr = 0;
de43d7d0 2145 struct procinfo *pi;
f8b76e70 2146
de43d7d0
SG
2147 pi = current_procinfo;
2148
2149 if (ioctl (pi->fd, PIOCNMAP, &nmap) == 0)
f8b76e70 2150 {
1ab3bf1b 2151 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
de43d7d0 2152 if (ioctl (pi->fd, PIOCMAP, prmaps) == 0)
f8b76e70
FF
2153 {
2154 for (prmap = prmaps; prmap -> pr_size; ++prmap)
2155 {
2156 if ((prmap -> pr_vaddr <= (caddr_t) addr) &&
2157 (prmap -> pr_vaddr + prmap -> pr_size > (caddr_t) addr))
2158 {
2159 baseaddr = (CORE_ADDR) prmap -> pr_vaddr;
2160 break;
2161 }
2162 }
2163 }
2164 }
2165 return (baseaddr);
2166}
2167
1ab3bf1b
JG
2168#endif /* 0 */
2169
f8b76e70
FF
2170/*
2171
cc221e76 2172LOCAL FUNCTION
f8b76e70
FF
2173
2174 proc_address_to_fd -- return open fd for file mapped to address
2175
2176SYNOPSIS
2177
de43d7d0 2178 int proc_address_to_fd (struct procinfo *pi, CORE_ADDR addr, complain)
f8b76e70
FF
2179
2180DESCRIPTION
2181
2182 Given an address in the current inferior's address space, use the
2183 /proc interface to find an open file descriptor for the file that
2184 this address was mapped in from. Return -1 if there is no current
2185 inferior. Print a warning message if there is an inferior but
2186 the address corresponds to no file (IE a bogus address).
2187
2188*/
2189
1ab3bf1b 2190static int
de43d7d0
SG
2191proc_address_to_fd (pi, addr, complain)
2192 struct procinfo *pi;
1ab3bf1b
JG
2193 CORE_ADDR addr;
2194 int complain;
f8b76e70
FF
2195{
2196 int fd = -1;
2197
de43d7d0 2198 if ((fd = ioctl (pi->fd, PIOCOPENM, (caddr_t *) &addr)) < 0)
f8b76e70 2199 {
de43d7d0 2200 if (complain)
f8b76e70 2201 {
de43d7d0
SG
2202 print_sys_errmsg (pi->pathname, errno);
2203 warning ("can't find mapped file for address 0x%x", addr);
f8b76e70
FF
2204 }
2205 }
2206 return (fd);
2207}
2208
35f5886e 2209
3fbdd536
JG
2210/* Attach to process PID, then initialize for debugging it
2211 and wait for the trace-trap that results from attaching. */
2212
2213static void
2214procfs_attach (args, from_tty)
2215 char *args;
2216 int from_tty;
2217{
2218 char *exec_file;
2219 int pid;
2220
2221 if (!args)
2222 error_no_arg ("process-id to attach");
2223
2224 pid = atoi (args);
2225
2226 if (pid == getpid()) /* Trying to masturbate? */
2227 error ("I refuse to debug myself!");
2228
2229 if (from_tty)
2230 {
2231 exec_file = (char *) get_exec_file (0);
2232
2233 if (exec_file)
199b2450 2234 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, target_pid_to_str (pid));
3fbdd536 2235 else
199b2450 2236 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid));
3fbdd536 2237
199b2450 2238 gdb_flush (gdb_stdout);
3fbdd536
JG
2239 }
2240
8fc2b417 2241 inferior_pid = pid = do_attach (pid);
3fbdd536
JG
2242 push_target (&procfs_ops);
2243}
2244
2245
2246/* Take a program previously attached to and detaches it.
2247 The program resumes execution and will no longer stop
2248 on signals, etc. We'd better not have left any breakpoints
2249 in the program or it'll die when it hits one. For this
2250 to work, it may be necessary for the process to have been
2251 previously attached. It *might* work if the program was
2252 started via the normal ptrace (PTRACE_TRACEME). */
2253
2254static void
2255procfs_detach (args, from_tty)
2256 char *args;
2257 int from_tty;
2258{
2259 int siggnal = 0;
2260
2261 if (from_tty)
2262 {
2263 char *exec_file = get_exec_file (0);
2264 if (exec_file == 0)
2265 exec_file = "";
199b2450 2266 printf_unfiltered ("Detaching from program: %s %s\n",
25286543 2267 exec_file, target_pid_to_str (inferior_pid));
199b2450 2268 gdb_flush (gdb_stdout);
3fbdd536
JG
2269 }
2270 if (args)
2271 siggnal = atoi (args);
2272
2273 do_detach (siggnal);
2274 inferior_pid = 0;
2275 unpush_target (&procfs_ops); /* Pop out of handling an inferior */
2276}
2277
2278/* Get ready to modify the registers array. On machines which store
2279 individual registers, this doesn't need to do anything. On machines
2280 which store all the registers in one fell swoop, this makes sure
2281 that registers contains all the registers from the program being
2282 debugged. */
2283
2284static void
2285procfs_prepare_to_store ()
2286{
2287#ifdef CHILD_PREPARE_TO_STORE
2288 CHILD_PREPARE_TO_STORE ();
2289#endif
2290}
2291
2292/* Print status information about what we're accessing. */
2293
2294static void
2295procfs_files_info (ignore)
2296 struct target_ops *ignore;
2297{
199b2450 2298 printf_unfiltered ("\tUsing the running image of %s %s via /proc.\n",
25286543 2299 attach_flag? "attached": "child", target_pid_to_str (inferior_pid));
3fbdd536
JG
2300}
2301
2302/* ARGSUSED */
2303static void
2304procfs_open (arg, from_tty)
2305 char *arg;
2306 int from_tty;
2307{
2308 error ("Use the \"run\" command to start a Unix child process.");
2309}
35f5886e
FF
2310
2311/*
2312
3fbdd536 2313LOCAL FUNCTION
35f5886e 2314
3fbdd536 2315 do_attach -- attach to an already existing process
35f5886e
FF
2316
2317SYNOPSIS
2318
3fbdd536 2319 int do_attach (int pid)
35f5886e
FF
2320
2321DESCRIPTION
2322
2323 Attach to an already existing process with the specified process
2324 id. If the process is not already stopped, query whether to
2325 stop it or not.
2326
2327NOTES
2328
2329 The option of stopping at attach time is specific to the /proc
2330 versions of gdb. Versions using ptrace force the attachee
ec8ceca3
JG
2331 to stop. (I have changed this version to do so, too. All you
2332 have to do is "continue" to make it go on. -- gnu@cygnus.com)
35f5886e
FF
2333
2334*/
2335
3fbdd536
JG
2336static int
2337do_attach (pid)
1ab3bf1b 2338 int pid;
35f5886e 2339{
de43d7d0
SG
2340 struct procinfo *pi;
2341
2342 pi = (struct procinfo *) xmalloc (sizeof (struct procinfo));
ec8ceca3 2343
de43d7d0 2344 if (!open_proc_file (pid, pi, O_RDWR))
35f5886e 2345 {
de43d7d0
SG
2346 free (pi);
2347 perror_with_name (pi->pathname);
35f5886e
FF
2348 /* NOTREACHED */
2349 }
2350
8fc2b417
SG
2351 pid = pi -> pid;
2352
de43d7d0
SG
2353 /* Add new process to process info list */
2354
2355 pi->next = procinfo_list;
2356 procinfo_list = pi;
2357
2358 add_fd (pi); /* Add to list for poll/select */
2359
35f5886e
FF
2360 /* Get current status of process and if it is not already stopped,
2361 then stop it. Remember whether or not it was stopped when we first
2362 examined it. */
2363
de43d7d0 2364 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
35f5886e 2365 {
de43d7d0
SG
2366 print_sys_errmsg (pi->pathname, errno);
2367 close_proc_file (pi);
35f5886e
FF
2368 error ("PIOCSTATUS failed");
2369 }
de43d7d0 2370 if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
35f5886e 2371 {
de43d7d0 2372 pi->was_stopped = 1;
35f5886e
FF
2373 }
2374 else
2375 {
de43d7d0 2376 pi->was_stopped = 0;
ec8ceca3 2377 if (1 || query ("Process is currently running, stop it? "))
35f5886e 2378 {
ec8ceca3 2379 /* Make it run again when we close it. */
8fc2b417
SG
2380
2381 modify_run_on_last_close_flag (pi->fd, 1);
2382
de43d7d0 2383 if (ioctl (pi->fd, PIOCSTOP, &pi->prstatus) < 0)
35f5886e 2384 {
de43d7d0
SG
2385 print_sys_errmsg (pi->pathname, errno);
2386 close_proc_file (pi);
35f5886e
FF
2387 error ("PIOCSTOP failed");
2388 }
de43d7d0 2389 pi->nopass_next_sigstop = 1;
d65eee73
FF
2390 }
2391 else
2392 {
199b2450 2393 printf_unfiltered ("Ok, gdb will wait for %s to stop.\n", target_pid_to_str (pid));
35f5886e
FF
2394 }
2395 }
ec8ceca3 2396
35f5886e
FF
2397 /* Remember some things about the inferior that we will, or might, change
2398 so that we can restore them when we detach. */
2399
de43d7d0
SG
2400 ioctl (pi->fd, PIOCGTRACE, &pi->saved_trace);
2401 ioctl (pi->fd, PIOCGHOLD, &pi->saved_sighold);
2402 ioctl (pi->fd, PIOCGFAULT, &pi->saved_fltset);
2403 ioctl (pi->fd, PIOCGENTRY, &pi->saved_entryset);
2404 ioctl (pi->fd, PIOCGEXIT, &pi->saved_exitset);
35f5886e
FF
2405
2406 /* Set up trace and fault sets, as gdb expects them. */
2407
de43d7d0
SG
2408 memset (&pi->prrun, 0, sizeof (pi->prrun));
2409 prfillset (&pi->prrun.pr_trace);
2410 procfs_notice_signals (pid);
2411 prfillset (&pi->prrun.pr_fault);
2412 prdelset (&pi->prrun.pr_fault, FLTPAGE);
2592eef8 2413
b9e58503
PS
2414#ifdef PROCFS_DONT_TRACE_FAULTS
2415 premptyset (&pi->prrun.pr_fault);
2592eef8
PS
2416#endif
2417
de43d7d0 2418 if (ioctl (pi->fd, PIOCSFAULT, &pi->prrun.pr_fault))
35f5886e 2419 {
f66f459f 2420 print_sys_errmsg ("PIOCSFAULT failed", errno);
35f5886e 2421 }
de43d7d0 2422 if (ioctl (pi->fd, PIOCSTRACE, &pi->prrun.pr_trace))
35f5886e 2423 {
f66f459f 2424 print_sys_errmsg ("PIOCSTRACE failed", errno);
35f5886e
FF
2425 }
2426 attach_flag = 1;
2427 return (pid);
2428}
2429
2430/*
2431
3fbdd536 2432LOCAL FUNCTION
35f5886e 2433
3fbdd536 2434 do_detach -- detach from an attached-to process
35f5886e
FF
2435
2436SYNOPSIS
2437
3fbdd536 2438 void do_detach (int signal)
35f5886e
FF
2439
2440DESCRIPTION
2441
2442 Detach from the current attachee.
2443
2444 If signal is non-zero, the attachee is started running again and sent
2445 the specified signal.
2446
2447 If signal is zero and the attachee was not already stopped when we
2448 attached to it, then we make it runnable again when we detach.
2449
2450 Otherwise, we query whether or not to make the attachee runnable
2451 again, since we may simply want to leave it in the state it was in
2452 when we attached.
2453
2454 We report any problems, but do not consider them errors, since we
2455 MUST detach even if some things don't seem to go right. This may not
2456 be the ideal situation. (FIXME).
2457 */
2458
3fbdd536
JG
2459static void
2460do_detach (signal)
1ab3bf1b 2461 int signal;
35f5886e 2462{
de43d7d0
SG
2463 struct procinfo *pi;
2464
2465 pi = current_procinfo;
ec8ceca3 2466
35f5886e
FF
2467 if (signal)
2468 {
de43d7d0 2469 set_proc_siginfo (pi, signal);
35f5886e 2470 }
de43d7d0 2471 if (ioctl (pi->fd, PIOCSEXIT, &pi->saved_exitset) < 0)
35f5886e 2472 {
de43d7d0 2473 print_sys_errmsg (pi->pathname, errno);
199b2450 2474 printf_unfiltered ("PIOCSEXIT failed.\n");
35f5886e 2475 }
de43d7d0 2476 if (ioctl (pi->fd, PIOCSENTRY, &pi->saved_entryset) < 0)
35f5886e 2477 {
de43d7d0 2478 print_sys_errmsg (pi->pathname, errno);
199b2450 2479 printf_unfiltered ("PIOCSENTRY failed.\n");
35f5886e 2480 }
de43d7d0 2481 if (ioctl (pi->fd, PIOCSTRACE, &pi->saved_trace) < 0)
35f5886e 2482 {
de43d7d0 2483 print_sys_errmsg (pi->pathname, errno);
199b2450 2484 printf_unfiltered ("PIOCSTRACE failed.\n");
35f5886e 2485 }
de43d7d0 2486 if (ioctl (pi->fd, PIOCSHOLD, &pi->saved_sighold) < 0)
cc221e76 2487 {
de43d7d0 2488 print_sys_errmsg (pi->pathname, errno);
199b2450 2489 printf_unfiltered ("PIOSCHOLD failed.\n");
cc221e76 2490 }
de43d7d0 2491 if (ioctl (pi->fd, PIOCSFAULT, &pi->saved_fltset) < 0)
35f5886e 2492 {
de43d7d0 2493 print_sys_errmsg (pi->pathname, errno);
199b2450 2494 printf_unfiltered ("PIOCSFAULT failed.\n");
35f5886e 2495 }
de43d7d0 2496 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
35f5886e 2497 {
de43d7d0 2498 print_sys_errmsg (pi->pathname, errno);
199b2450 2499 printf_unfiltered ("PIOCSTATUS failed.\n");
35f5886e
FF
2500 }
2501 else
2502 {
de43d7d0 2503 if (signal || (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
35f5886e 2504 {
de43d7d0 2505 if (signal || !pi->was_stopped ||
35f5886e
FF
2506 query ("Was stopped when attached, make it runnable again? "))
2507 {
2592eef8
PS
2508 /* Clear any pending signal if we want to detach without
2509 a signal. */
2510 if (signal == 0)
2511 set_proc_siginfo (pi, signal);
2512
ec8ceca3 2513 /* Clear any fault that might have stopped it. */
de43d7d0 2514 if (ioctl (pi->fd, PIOCCFAULT, 0))
eca4a350
SG
2515 {
2516 print_sys_errmsg (pi->pathname, errno);
199b2450 2517 printf_unfiltered ("PIOCCFAULT failed.\n");
eca4a350 2518 }
ec8ceca3
JG
2519
2520 /* Make it run again when we close it. */
8fc2b417
SG
2521
2522 modify_run_on_last_close_flag (pi->fd, 1);
35f5886e
FF
2523 }
2524 }
2525 }
de43d7d0 2526 close_proc_file (pi);
35f5886e
FF
2527 attach_flag = 0;
2528}
2529
45dc9be3
JK
2530/* emulate wait() as much as possible.
2531 Wait for child to do something. Return pid of child, or -1 in case
2532 of error; store status in *OURSTATUS.
2533
2534 Not sure why we can't
2535 just use wait(), but it seems to have problems when applied to a
2536 process being controlled with the /proc interface.
2537
2538 We have a race problem here with no obvious solution. We need to let
2539 the inferior run until it stops on an event of interest, which means
2540 that we need to use the PIOCWSTOP ioctl. However, we cannot use this
2541 ioctl if the process is already stopped on something that is not an
2542 event of interest, or the call will hang indefinitely. Thus we first
2543 use PIOCSTATUS to see if the process is not stopped. If not, then we
2544 use PIOCWSTOP. But during the window between the two, if the process
2545 stops for any reason that is not an event of interest (such as a job
2546 control signal) then gdb will hang. One possible workaround is to set
2547 an alarm to wake up every minute of so and check to see if the process
2548 is still running, and if so, then reissue the PIOCWSTOP. But this is
2549 a real kludge, so has not been implemented. FIXME: investigate
2550 alternatives.
2551
2552 FIXME: Investigate why wait() seems to have problems with programs
2553 being control by /proc routines. */
35f5886e 2554
3fbdd536 2555static int
45dc9be3 2556procfs_wait (pid, ourstatus)
de43d7d0 2557 int pid;
67ac9759 2558 struct target_waitstatus *ourstatus;
35f5886e
FF
2559{
2560 short what;
2561 short why;
2562 int statval = 0;
2563 int checkerr = 0;
2564 int rtnval = -1;
de43d7d0
SG
2565 struct procinfo *pi;
2566
2567 if (pid != -1) /* Non-specific process? */
2568 pi = NULL;
2569 else
2570 for (pi = procinfo_list; pi; pi = pi->next)
2571 if (pi->had_event)
2572 break;
2573
de43d7d0 2574 if (!pi)
eca4a350
SG
2575 {
2576 wait_again:
2577
8fc2b417
SG
2578 if (pi)
2579 pi->had_event = 0;
2580
eca4a350
SG
2581 pi = wait_fd ();
2582 }
de43d7d0
SG
2583
2584 if (pid != -1)
2585 for (pi = procinfo_list; pi; pi = pi->next)
2586 if (pi->pid == pid && pi->had_event)
2587 break;
2588
2589 if (!pi && !checkerr)
2590 goto wait_again;
2591
2592 if (!checkerr && !(pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
2593 {
2594 if (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0)
2595 {
2596 checkerr++;
2597 }
35f5886e
FF
2598 }
2599 if (checkerr)
2600 {
2601 if (errno == ENOENT)
2602 {
2603 rtnval = wait (&statval);
2604 if (rtnval != inferior_pid)
2605 {
de43d7d0 2606 print_sys_errmsg (pi->pathname, errno);
35f5886e
FF
2607 error ("PIOCWSTOP, wait failed, returned %d", rtnval);
2608 /* NOTREACHED */
2609 }
2610 }
2611 else
2612 {
de43d7d0 2613 print_sys_errmsg (pi->pathname, errno);
35f5886e
FF
2614 error ("PIOCSTATUS or PIOCWSTOP failed.");
2615 /* NOTREACHED */
2616 }
2617 }
de43d7d0 2618 else if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
35f5886e 2619 {
8fc2b417 2620 rtnval = pi->pid;
de43d7d0
SG
2621 why = pi->prstatus.pr_why;
2622 what = pi->prstatus.pr_what;
2623
2624 switch (why)
35f5886e 2625 {
de43d7d0 2626 case PR_SIGNALLED:
35f5886e 2627 statval = (what << 8) | 0177;
fb63d460
SG
2628 break;
2629 case PR_SYSENTRY:
de43d7d0 2630 case PR_SYSEXIT:
8fc2b417
SG
2631 {
2632 int i;
2633 int found_handler = 0;
de43d7d0 2634
8fc2b417
SG
2635 for (i = 0; i < pi->num_syscall_handlers; i++)
2636 if (pi->syscall_handlers[i].syscall_num == what)
de43d7d0 2637 {
8fc2b417
SG
2638 found_handler = 1;
2639 if (!pi->syscall_handlers[i].func (pi, what, why,
2640 &rtnval, &statval))
2641 goto wait_again;
de43d7d0 2642
8fc2b417 2643 break;
de43d7d0
SG
2644 }
2645
8fc2b417
SG
2646 if (!found_handler)
2647 if (why == PR_SYSENTRY)
2648 error ("PR_SYSENTRY, unhandled system call %d", what);
2649 else
2650 error ("PR_SYSEXIT, unhandled system call %d", what);
2651 }
de43d7d0 2652 break;
1cb1b16c
PS
2653#ifdef PR_DEAD
2654 case (short)PR_DEAD:
2655 {
2656 int dummy;
2657
2658 /* The inferior process is about to terminate.
2659 pr_what has the process's exit or return value.
2660 A PIOCRUN ioctl must be used to restart the process so it
2661 can finish exiting. */
2662
2663 pi->prrun.pr_flags = PRCFAULT;
2664
2665 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
2666 perror_with_name (pi->pathname);
2667
2668 if (wait (&dummy) < 0)
2669 rtnval = -1;
2670 statval = pi->prstatus.pr_what;
2671 }
2672 break;
2673#endif
de43d7d0 2674 case PR_REQUESTED:
35f5886e 2675 statval = (SIGSTOP << 8) | 0177;
de43d7d0
SG
2676 break;
2677 case PR_JOBCONTROL:
35f5886e 2678 statval = (what << 8) | 0177;
de43d7d0
SG
2679 break;
2680 case PR_FAULTED:
35f5886e
FF
2681 switch (what)
2682 {
e6b8a171 2683#ifdef FLTWATCH
999dd04b 2684 case FLTWATCH:
e6b8a171
JL
2685 statval = (SIGTRAP << 8) | 0177;
2686 break;
2687#endif
2688#ifdef FLTKWATCH
999dd04b 2689 case FLTKWATCH:
35f5886e
FF
2690 statval = (SIGTRAP << 8) | 0177;
2691 break;
e6b8a171 2692#endif
3f5e2fb5
JK
2693#ifndef FAULTED_USE_SIGINFO
2694 /* Irix, contrary to the documentation, fills in 0 for si_signo.
2695 Solaris fills in si_signo. I'm not sure about others. */
2696 case FLTPRIV:
2697 case FLTILL:
2698 statval = (SIGILL << 8) | 0177;
2699 break;
2700 case FLTBPT:
2701 case FLTTRACE:
2702 statval = (SIGTRAP << 8) | 0177;
2703 break;
2704 case FLTSTACK:
2705 case FLTACCESS:
2706 case FLTBOUNDS:
2707 statval = (SIGSEGV << 8) | 0177;
2708 break;
2709 case FLTIOVF:
2710 case FLTIZDIV:
2711 case FLTFPE:
2712 statval = (SIGFPE << 8) | 0177;
2713 break;
2714 case FLTPAGE: /* Recoverable page fault */
2715#endif /* not FAULTED_USE_SIGINFO */
35f5886e 2716 default:
890634ed
JK
2717 /* Use the signal which the kernel assigns. This is better than
2718 trying to second-guess it from the fault. In fact, I suspect
2719 that FLTACCESS can be either SIGSEGV or SIGBUS. */
2720 statval = ((pi->prstatus.pr_info.si_signo) << 8) | 0177;
2721 break;
35f5886e 2722 }
de43d7d0
SG
2723 break;
2724 default:
35f5886e 2725 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
35f5886e 2726 }
de43d7d0
SG
2727/* Stop all the other threads when any of them stops. */
2728
2729 {
2730 struct procinfo *procinfo;
2731
2732 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
2733 {
2734 if (!procinfo->had_event)
8fc2b417
SG
2735 {
2736 /* A bug in Solaris (2.5) causes us to hang when trying to
2737 stop a stopped process. So, we have to check first in
2738 order to avoid the hang. */
2739 if (ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus) < 0)
2740 {
2741 print_sys_errmsg (procinfo->pathname, errno);
2742 error ("PIOCSTATUS failed");
2743 }
2744 if (!(procinfo->prstatus.pr_flags & PR_STOPPED))
2745 if (ioctl (procinfo->fd, PIOCSTOP, &procinfo->prstatus) < 0)
2746 {
2747 print_sys_errmsg (procinfo->pathname, errno);
2748 error ("PIOCSTOP failed");
2749 }
2750 }
de43d7d0
SG
2751 }
2752 }
35f5886e
FF
2753 }
2754 else
2755 {
2756 error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x",
de43d7d0 2757 pi->prstatus.pr_flags);
35f5886e 2758 }
3fbdd536 2759
67ac9759 2760 store_waitstatus (ourstatus, statval);
3fbdd536
JG
2761
2762 if (rtnval == -1) /* No more children to wait for */
2763 {
199b2450 2764 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing.\n");
67ac9759
JK
2765 /* Claim it exited with unknown signal. */
2766 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2767 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
3fbdd536
JG
2768 return rtnval;
2769 }
2770
de43d7d0 2771 pi->had_event = 0; /* Indicate that we've seen this one */
35f5886e
FF
2772 return (rtnval);
2773}
2774
2775/*
2776
6b801388
FF
2777LOCAL FUNCTION
2778
2779 set_proc_siginfo - set a process's current signal info
2780
2781SYNOPSIS
2782
2783 void set_proc_siginfo (struct procinfo *pip, int signo);
2784
2785DESCRIPTION
2786
2787 Given a pointer to a process info struct in PIP and a signal number
2788 in SIGNO, set the process's current signal and its associated signal
2789 information. The signal will be delivered to the process immediately
2790 after execution is resumed, even if it is being held. In addition,
2791 this particular delivery will not cause another PR_SIGNALLED stop
2792 even if the signal is being traced.
2793
2794 If we are not delivering the same signal that the prstatus siginfo
2795 struct contains information about, then synthesize a siginfo struct
2796 to match the signal we are doing to deliver, make it of the type
2797 "generated by a user process", and send this synthesized copy. When
2798 used to set the inferior's signal state, this will be required if we
2799 are not currently stopped because of a traced signal, or if we decide
2800 to continue with a different signal.
2801
2802 Note that when continuing the inferior from a stop due to receipt
2803 of a traced signal, we either have set PRCSIG to clear the existing
2804 signal, or we have to call this function to do a PIOCSSIG with either
2805 the existing siginfo struct from pr_info, or one we have synthesized
2806 appropriately for the signal we want to deliver. Otherwise if the
2807 signal is still being traced, the inferior will immediately stop
2808 again.
2809
2810 See siginfo(5) for more details.
2811*/
2812
2813static void
2814set_proc_siginfo (pip, signo)
cc221e76
FF
2815 struct procinfo *pip;
2816 int signo;
6b801388
FF
2817{
2818 struct siginfo newsiginfo;
2819 struct siginfo *sip;
2820
2592eef8
PS
2821#ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2822 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
a1a0d974 2823 receives a PIOCSSIG with a signal identical to the current signal,
2592eef8
PS
2824 it messes up the current signal. Work around the kernel bug. */
2825 if (signo == pip -> prstatus.pr_cursig)
2826 return;
2827#endif
2828
de43d7d0 2829 if (signo == pip -> prstatus.pr_info.si_signo)
6b801388 2830 {
de43d7d0
SG
2831 sip = &pip -> prstatus.pr_info;
2832 }
2833 else
2834 {
2835 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
2836 sip = &newsiginfo;
2837 sip -> si_signo = signo;
2838 sip -> si_code = 0;
2839 sip -> si_errno = 0;
2840 sip -> si_pid = getpid ();
2841 sip -> si_uid = getuid ();
2842 }
2843 if (ioctl (pip -> fd, PIOCSSIG, sip) < 0)
2844 {
2845 print_sys_errmsg (pip -> pathname, errno);
2846 warning ("PIOCSSIG failed");
6b801388
FF
2847 }
2848}
2849
25286543 2850/* Resume execution of process PID. If STEP is nozero, then
59ba57da
JK
2851 just single step it. If SIGNAL is nonzero, restart it with that
2852 signal activated. */
35f5886e 2853
3fbdd536 2854static void
25286543
SG
2855procfs_resume (pid, step, signo)
2856 int pid;
1ab3bf1b 2857 int step;
67ac9759 2858 enum target_signal signo;
35f5886e 2859{
59ba57da 2860 int signal_to_pass;
de43d7d0
SG
2861 struct procinfo *pi, *procinfo;
2862
2863 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
59ba57da 2864
35f5886e 2865 errno = 0;
de43d7d0 2866 pi->prrun.pr_flags = PRSTRACE | PRSFAULT | PRCFAULT;
99fd9e3e 2867
59ba57da
JK
2868#if 0
2869 /* It should not be necessary. If the user explicitly changes the value,
2870 value_assign calls write_register_bytes, which writes it. */
2871/* It may not be absolutely necessary to specify the PC value for
2872 restarting, but to be safe we use the value that gdb considers
2873 to be current. One case where this might be necessary is if the
2874 user explicitly changes the PC value that gdb considers to be
2875 current. FIXME: Investigate if this is necessary or not. */
2876
c3192172 2877#ifdef PRSVADDR_BROKEN
99fd9e3e
SG
2878/* Can't do this under Solaris running on a Sparc, as there seems to be no
2879 place to put nPC. In fact, if you use this, nPC seems to be set to some
2880 random garbage. We have to rely on the fact that PC and nPC have been
2881 written previously via PIOCSREG during a register flush. */
2882
de43d7d0
SG
2883 pi->prrun.pr_vaddr = (caddr_t) *(int *) &registers[REGISTER_BYTE (PC_REGNUM)];
2884 pi->prrun.pr_flags != PRSVADDR;
99fd9e3e 2885#endif
59ba57da
JK
2886#endif
2887
67ac9759 2888 if (signo == TARGET_SIGNAL_STOP && pi->nopass_next_sigstop)
59ba57da
JK
2889 /* When attaching to a child process, if we forced it to stop with
2890 a PIOCSTOP, then we will have set the nopass_next_sigstop flag.
2891 Upon resuming the first time after such a stop, we explicitly
2892 inhibit sending it another SIGSTOP, which would be the normal
2893 result of default signal handling. One potential drawback to
2894 this is that we will also ignore any attempt to by the user
2895 to explicitly continue after the attach with a SIGSTOP. Ultimately
2896 this problem should be dealt with by making the routines that
2897 deal with the inferior a little smarter, and possibly even allow
2898 an inferior to continue running at the same time as gdb. (FIXME?) */
2899 signal_to_pass = 0;
67ac9759 2900 else if (signo == TARGET_SIGNAL_TSTP
de43d7d0
SG
2901 && pi->prstatus.pr_cursig == SIGTSTP
2902 && pi->prstatus.pr_action.sa_handler == SIG_DFL)
59ba57da
JK
2903
2904 /* We are about to pass the inferior a SIGTSTP whose action is
2905 SIG_DFL. The SIG_DFL action for a SIGTSTP is to stop
2906 (notifying the parent via wait()), and then keep going from the
2907 same place when the parent is ready for you to keep going. So
2908 under the debugger, it should do nothing (as if the program had
2909 been stopped and then later resumed. Under ptrace, this
2910 happens for us, but under /proc, the system obligingly stops
2911 the process, and wait_for_inferior would have no way of
2912 distinguishing that type of stop (which indicates that we
2913 should just start it again), with a stop due to the pr_trace
2914 field of the prrun_t struct.
2915
2916 Note that if the SIGTSTP is being caught, we *do* need to pass it,
2917 because the handler needs to get executed. */
2918 signal_to_pass = 0;
2919 else
67ac9759 2920 signal_to_pass = target_signal_to_host (signo);
99fd9e3e 2921
59ba57da 2922 if (signal_to_pass)
35f5886e 2923 {
de43d7d0 2924 set_proc_siginfo (pi, signal_to_pass);
35f5886e
FF
2925 }
2926 else
2927 {
de43d7d0 2928 pi->prrun.pr_flags |= PRCSIG;
35f5886e 2929 }
de43d7d0 2930 pi->nopass_next_sigstop = 0;
35f5886e
FF
2931 if (step)
2932 {
de43d7d0 2933 pi->prrun.pr_flags |= PRSTEP;
35f5886e 2934 }
8fc2b417
SG
2935
2936 /* Don't try to start a process unless it's stopped on an
2937 `event of interest'. Doing so will cause errors. */
2938
2939 if ((pi->prstatus.pr_flags & PR_ISTOP)
2940 && ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
35f5886e 2941 {
de43d7d0 2942 perror_with_name (pi->pathname);
35f5886e
FF
2943 /* NOTREACHED */
2944 }
de43d7d0
SG
2945
2946 pi->had_event = 0;
2947
2948 /* Continue all the other threads that haven't had an event of
2949 interest. */
2950
2951 if (pid == -1)
2952 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
2953 {
2954 if (pi != procinfo && !procinfo->had_event)
2955 {
2956 procinfo->prrun.pr_flags &= PRSTEP;
2957 procinfo->prrun.pr_flags |= PRCFAULT | PRCSIG;
2958 ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus);
8fc2b417
SG
2959
2960 /* Don't try to start a process unless it's stopped on an
2961 `event of interest'. Doing so will cause errors. */
2962
2963 if ((procinfo->prstatus.pr_flags & PR_ISTOP)
2964 && ioctl (procinfo->fd, PIOCRUN, &procinfo->prrun) < 0)
de43d7d0
SG
2965 {
2966 if (ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus) < 0)
2967 {
199b2450 2968 fprintf_unfiltered(gdb_stderr, "PIOCSTATUS failed, errno=%d\n", errno);
de43d7d0
SG
2969 }
2970 print_sys_errmsg (procinfo->pathname, errno);
2971 error ("PIOCRUN failed");
2972 }
2973 ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus);
2974 }
2975 }
35f5886e
FF
2976}
2977
2978/*
2979
3fbdd536 2980LOCAL FUNCTION
35f5886e 2981
3fbdd536 2982 procfs_fetch_registers -- fetch current registers from inferior
35f5886e
FF
2983
2984SYNOPSIS
2985
3fbdd536 2986 void procfs_fetch_registers (int regno)
35f5886e
FF
2987
2988DESCRIPTION
2989
2990 Read the current values of the inferior's registers, both the
2991 general register set and floating point registers (if supported)
2992 and update gdb's idea of their current values.
2993
2994*/
2995
3fbdd536
JG
2996static void
2997procfs_fetch_registers (regno)
1ab3bf1b 2998 int regno;
35f5886e 2999{
de43d7d0
SG
3000 struct procinfo *pi;
3001
3002 pi = current_procinfo;
3003
3004 if (ioctl (pi->fd, PIOCGREG, &pi->gregset) != -1)
35f5886e 3005 {
de43d7d0 3006 supply_gregset (&pi->gregset);
35f5886e
FF
3007 }
3008#if defined (FP0_REGNUM)
de43d7d0 3009 if (ioctl (pi->fd, PIOCGFPREG, &pi->fpregset) != -1)
35f5886e 3010 {
de43d7d0 3011 supply_fpregset (&pi->fpregset);
35f5886e
FF
3012 }
3013#endif
3014}
3015
fb182850
FF
3016/*
3017
35f5886e
FF
3018LOCAL FUNCTION
3019
de43d7d0
SG
3020 proc_init_failed - called whenever /proc access initialization
3021fails
35f5886e
FF
3022
3023SYNOPSIS
3024
de43d7d0 3025 static void proc_init_failed (struct procinfo *pi, char *why)
35f5886e
FF
3026
3027DESCRIPTION
3028
3029 This function is called whenever initialization of access to a /proc
3030 entry fails. It prints a suitable error message, does some cleanup,
3031 and then invokes the standard error processing routine which dumps
3032 us back into the command loop.
3033 */
3034
3035static void
de43d7d0
SG
3036proc_init_failed (pi, why)
3037 struct procinfo *pi;
1ab3bf1b 3038 char *why;
35f5886e 3039{
de43d7d0
SG
3040 print_sys_errmsg (pi->pathname, errno);
3041 kill (pi->pid, SIGKILL);
3042 close_proc_file (pi);
35f5886e
FF
3043 error (why);
3044 /* NOTREACHED */
3045}
3046
3047/*
3048
3049LOCAL FUNCTION
3050
3051 close_proc_file - close any currently open /proc entry
3052
3053SYNOPSIS
3054
a39ad5ce 3055 static void close_proc_file (struct procinfo *pip)
35f5886e
FF
3056
3057DESCRIPTION
3058
3059 Close any currently open /proc entry and mark the process information
3060 entry as invalid. In order to ensure that we don't try to reuse any
3061 stale information, the pid, fd, and pathnames are explicitly
3062 invalidated, which may be overkill.
3063
3064 */
3065
3066static void
1ab3bf1b
JG
3067close_proc_file (pip)
3068 struct procinfo *pip;
35f5886e 3069{
de43d7d0
SG
3070 struct procinfo *procinfo;
3071
3072 remove_fd (pip); /* Remove fd from poll/select list */
3073
3074 close (pip -> fd);
3075
3076 free (pip -> pathname);
3077
3078 /* Unlink pip from the procinfo chain. Note pip might not be on the list. */
3079
3080 if (procinfo_list == pip)
3081 procinfo_list = pip->next;
3082 else
3083 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
3084 if (procinfo->next == pip)
3085 procinfo->next = pip->next;
3086
3087 free (pip);
35f5886e
FF
3088}
3089
3090/*
3091
3092LOCAL FUNCTION
3093
3094 open_proc_file - open a /proc entry for a given process id
3095
3096SYNOPSIS
3097
ec8ceca3 3098 static int open_proc_file (int pid, struct procinfo *pip, int mode)
35f5886e
FF
3099
3100DESCRIPTION
3101
ec8ceca3
JG
3102 Given a process id and a mode, close the existing open /proc
3103 entry (if any) and open one for the new process id, in the
3104 specified mode. Once it is open, then mark the local process
3105 information structure as valid, which guarantees that the pid,
3106 fd, and pathname fields match an open /proc entry. Returns
3107 zero if the open fails, nonzero otherwise.
35f5886e
FF
3108
3109 Note that the pathname is left intact, even when the open fails,
3110 so that callers can use it to construct meaningful error messages
3111 rather than just "file open failed".
8fc2b417
SG
3112
3113 Note that for Solaris, the process-id also includes an LWP-id, so we
3114 actually attempt to open that. If we are handed a pid with a 0 LWP-id,
3115 then we will ask the kernel what it is and add it to the pid. Hence,
3116 the pid can be changed by us.
35f5886e
FF
3117 */
3118
3119static int
ec8ceca3 3120open_proc_file (pid, pip, mode)
1ab3bf1b
JG
3121 int pid;
3122 struct procinfo *pip;
ec8ceca3 3123 int mode;
35f5886e 3124{
8fc2b417
SG
3125 int tmp, tmpfd;
3126
de43d7d0
SG
3127 pip -> next = NULL;
3128 pip -> had_event = 0;
3129 pip -> pathname = xmalloc (32);
3130 pip -> pid = pid;
3131
8fc2b417
SG
3132#ifndef PIOCOPENLWP
3133 tmp = pid;
3134#else
3135 tmp = pid & 0xffff;
3136#endif
3137
3138 sprintf (pip -> pathname, PROC_NAME_FMT, tmp);
3139 if ((tmpfd = open (pip -> pathname, mode)) < 0)
de43d7d0
SG
3140 return 0;
3141
8fc2b417
SG
3142#ifndef PIOCOPENLWP
3143 pip -> fd = tmpfd;
3144#else
3145 tmp = (pid >> 16) & 0xffff; /* Extract thread id */
3146
3147 if (tmp == 0)
3148 { /* Don't know thread id yet */
3149 if (ioctl (tmpfd, PIOCSTATUS, &pip -> prstatus) < 0)
3150 {
3151 print_sys_errmsg (pip -> pathname, errno);
3152 close (tmpfd);
3153 error ("open_proc_file: PIOCSTATUS failed");
3154 }
3155
3156 tmp = pip -> prstatus.pr_who; /* Get thread id from prstatus_t */
3157 pip -> pid = (tmp << 16) | pid; /* Update pip */
3158 }
3159
3160 if ((pip -> fd = ioctl (tmpfd, PIOCOPENLWP, &tmp)) < 0)
3161 {
3162 close (tmpfd);
3163 return 0;
3164 }
3165
3166#ifdef PIOCSET /* New method */
3167 {
3168 long pr_flags;
3169 pr_flags = PR_ASYNC;
3170 ioctl (pip -> fd, PIOCSET, &pr_flags);
3171 }
3172#endif
3173
3174 close (tmpfd); /* All done with main pid */
3175#endif /* PIOCOPENLWP */
3176
de43d7d0 3177 return 1;
a39ad5ce
FF
3178}
3179
f66f459f 3180static char *
1ab3bf1b
JG
3181mappingflags (flags)
3182 long flags;
a39ad5ce 3183{
5c1c5e67 3184 static char asciiflags[8];
a39ad5ce 3185
5c1c5e67
FF
3186 strcpy (asciiflags, "-------");
3187#if defined (MA_PHYS)
3188 if (flags & MA_PHYS) asciiflags[0] = 'd';
3189#endif
3190 if (flags & MA_STACK) asciiflags[1] = 's';
3191 if (flags & MA_BREAK) asciiflags[2] = 'b';
3192 if (flags & MA_SHARED) asciiflags[3] = 's';
3193 if (flags & MA_READ) asciiflags[4] = 'r';
3194 if (flags & MA_WRITE) asciiflags[5] = 'w';
3195 if (flags & MA_EXEC) asciiflags[6] = 'x';
a39ad5ce
FF
3196 return (asciiflags);
3197}
3198
3199static void
cc221e76
FF
3200info_proc_flags (pip, summary)
3201 struct procinfo *pip;
3202 int summary;
3203{
3204 struct trans *transp;
3205
3206 printf_filtered ("%-32s", "Process status flags:");
3207 if (!summary)
3208 {
3209 printf_filtered ("\n\n");
3210 }
3211 for (transp = pr_flag_table; transp -> name != NULL; transp++)
3212 {
3213 if (pip -> prstatus.pr_flags & transp -> value)
3214 {
3215 if (summary)
3216 {
3217 printf_filtered ("%s ", transp -> name);
3218 }
3219 else
3220 {
3221 printf_filtered ("\t%-16s %s.\n", transp -> name, transp -> desc);
3222 }
3223 }
3224 }
3225 printf_filtered ("\n");
3226}
3227
3228static void
3229info_proc_stop (pip, summary)
3230 struct procinfo *pip;
3231 int summary;
3232{
3233 struct trans *transp;
3234 int why;
3235 int what;
3236
3237 why = pip -> prstatus.pr_why;
3238 what = pip -> prstatus.pr_what;
3239
3240 if (pip -> prstatus.pr_flags & PR_STOPPED)
3241 {
3242 printf_filtered ("%-32s", "Reason for stopping:");
3243 if (!summary)
3244 {
3245 printf_filtered ("\n\n");
3246 }
3247 for (transp = pr_why_table; transp -> name != NULL; transp++)
3248 {
3249 if (why == transp -> value)
3250 {
3251 if (summary)
3252 {
3253 printf_filtered ("%s ", transp -> name);
3254 }
3255 else
3256 {
3257 printf_filtered ("\t%-16s %s.\n",
3258 transp -> name, transp -> desc);
3259 }
3260 break;
3261 }
3262 }
3263
3264 /* Use the pr_why field to determine what the pr_what field means, and
3265 print more information. */
3266
3267 switch (why)
3268 {
3269 case PR_REQUESTED:
3270 /* pr_what is unused for this case */
3271 break;
3272 case PR_JOBCONTROL:
3273 case PR_SIGNALLED:
3274 if (summary)
3275 {
3276 printf_filtered ("%s ", signalname (what));
3277 }
3278 else
3279 {
3280 printf_filtered ("\t%-16s %s.\n", signalname (what),
4ace50a5 3281 safe_strsignal (what));
cc221e76
FF
3282 }
3283 break;
3284 case PR_SYSENTRY:
3285 if (summary)
3286 {
3287 printf_filtered ("%s ", syscallname (what));
3288 }
3289 else
3290 {
3291 printf_filtered ("\t%-16s %s.\n", syscallname (what),
3292 "Entered this system call");
3293 }
3294 break;
3295 case PR_SYSEXIT:
3296 if (summary)
3297 {
3298 printf_filtered ("%s ", syscallname (what));
3299 }
3300 else
3301 {
3302 printf_filtered ("\t%-16s %s.\n", syscallname (what),
3303 "Returned from this system call");
3304 }
3305 break;
3306 case PR_FAULTED:
3307 if (summary)
3308 {
3309 printf_filtered ("%s ",
3310 lookupname (faults_table, what, "fault"));
3311 }
3312 else
3313 {
3314 printf_filtered ("\t%-16s %s.\n",
3315 lookupname (faults_table, what, "fault"),
3316 lookupdesc (faults_table, what));
3317 }
3318 break;
3319 }
3320 printf_filtered ("\n");
3321 }
3322}
3323
3324static void
3325info_proc_siginfo (pip, summary)
3326 struct procinfo *pip;
3327 int summary;
3328{
3329 struct siginfo *sip;
3330
3331 if ((pip -> prstatus.pr_flags & PR_STOPPED) &&
3332 (pip -> prstatus.pr_why == PR_SIGNALLED ||
3333 pip -> prstatus.pr_why == PR_FAULTED))
3334 {
3335 printf_filtered ("%-32s", "Additional signal/fault info:");
3336 sip = &pip -> prstatus.pr_info;
3337 if (summary)
3338 {
3339 printf_filtered ("%s ", signalname (sip -> si_signo));
3340 if (sip -> si_errno > 0)
3341 {
4ace50a5 3342 printf_filtered ("%s ", errnoname (sip -> si_errno));
cc221e76
FF
3343 }
3344 if (sip -> si_code <= 0)
3345 {
25286543
SG
3346 printf_filtered ("sent by %s, uid %d ",
3347 target_pid_to_str (sip -> si_pid),
cc221e76
FF
3348 sip -> si_uid);
3349 }
3350 else
3351 {
3352 printf_filtered ("%s ", sigcodename (sip));
3353 if ((sip -> si_signo == SIGILL) ||
3354 (sip -> si_signo == SIGFPE) ||
3355 (sip -> si_signo == SIGSEGV) ||
3356 (sip -> si_signo == SIGBUS))
3357 {
b9e58503
PS
3358 printf_filtered ("addr=%#lx ",
3359 (unsigned long) sip -> si_addr);
cc221e76
FF
3360 }
3361 else if ((sip -> si_signo == SIGCHLD))
3362 {
25286543
SG
3363 printf_filtered ("child %s, status %u ",
3364 target_pid_to_str (sip -> si_pid),
cc221e76
FF
3365 sip -> si_status);
3366 }
3367 else if ((sip -> si_signo == SIGPOLL))
3368 {
3369 printf_filtered ("band %u ", sip -> si_band);
3370 }
3371 }
3372 }
3373 else
3374 {
3375 printf_filtered ("\n\n");
3376 printf_filtered ("\t%-16s %s.\n", signalname (sip -> si_signo),
4ace50a5 3377 safe_strsignal (sip -> si_signo));
cc221e76
FF
3378 if (sip -> si_errno > 0)
3379 {
3380 printf_filtered ("\t%-16s %s.\n",
4ace50a5
FF
3381 errnoname (sip -> si_errno),
3382 safe_strerror (sip -> si_errno));
cc221e76
FF
3383 }
3384 if (sip -> si_code <= 0)
3385 {
25286543 3386 printf_filtered ("\t%-16u %s\n", sip -> si_pid, /* XXX need target_pid_to_str() */
cc221e76
FF
3387 "PID of process sending signal");
3388 printf_filtered ("\t%-16u %s\n", sip -> si_uid,
3389 "UID of process sending signal");
3390 }
3391 else
3392 {
3393 printf_filtered ("\t%-16s %s.\n", sigcodename (sip),
3394 sigcodedesc (sip));
3395 if ((sip -> si_signo == SIGILL) ||
3396 (sip -> si_signo == SIGFPE))
3397 {
b9e58503
PS
3398 printf_filtered ("\t%#-16lx %s.\n",
3399 (unsigned long) sip -> si_addr,
cc221e76
FF
3400 "Address of faulting instruction");
3401 }
3402 else if ((sip -> si_signo == SIGSEGV) ||
3403 (sip -> si_signo == SIGBUS))
3404 {
b9e58503
PS
3405 printf_filtered ("\t%#-16lx %s.\n",
3406 (unsigned long) sip -> si_addr,
cc221e76
FF
3407 "Address of faulting memory reference");
3408 }
3409 else if ((sip -> si_signo == SIGCHLD))
3410 {
25286543 3411 printf_filtered ("\t%-16u %s.\n", sip -> si_pid, /* XXX need target_pid_to_str() */
cc221e76
FF
3412 "Child process ID");
3413 printf_filtered ("\t%-16u %s.\n", sip -> si_status,
3414 "Child process exit value or signal");
3415 }
3416 else if ((sip -> si_signo == SIGPOLL))
3417 {
3418 printf_filtered ("\t%-16u %s.\n", sip -> si_band,
3419 "Band event for POLL_{IN,OUT,MSG}");
3420 }
3421 }
3422 }
3423 printf_filtered ("\n");
3424 }
3425}
3426
3427static void
3428info_proc_syscalls (pip, summary)
3429 struct procinfo *pip;
3430 int summary;
3431{
3432 int syscallnum;
3433
3434 if (!summary)
3435 {
3436
3437#if 0 /* FIXME: Needs to use gdb-wide configured info about system calls. */
3438 if (pip -> prstatus.pr_flags & PR_ASLEEP)
3439 {
3440 int syscallnum = pip -> prstatus.pr_reg[R_D0];
3441 if (summary)
3442 {
3443 printf_filtered ("%-32s", "Sleeping in system call:");
3444 printf_filtered ("%s", syscallname (syscallnum));
3445 }
3446 else
3447 {
3448 printf_filtered ("Sleeping in system call '%s'.\n",
3449 syscallname (syscallnum));
3450 }
3451 }
3452#endif
3453
3454 if (ioctl (pip -> fd, PIOCGENTRY, &pip -> entryset) < 0)
3455 {
3456 print_sys_errmsg (pip -> pathname, errno);
3457 error ("PIOCGENTRY failed");
3458 }
3459
3460 if (ioctl (pip -> fd, PIOCGEXIT, &pip -> exitset) < 0)
3461 {
3462 print_sys_errmsg (pip -> pathname, errno);
3463 error ("PIOCGEXIT failed");
3464 }
3465
3466 printf_filtered ("System call tracing information:\n\n");
3467
3468 printf_filtered ("\t%-12s %-8s %-8s\n",
3469 "System call",
3470 "Entry",
3471 "Exit");
3472 for (syscallnum = 0; syscallnum < MAX_SYSCALLS; syscallnum++)
3473 {
3474 QUIT;
3475 if (syscall_table[syscallnum] != NULL)
8fc2b417
SG
3476 printf_filtered ("\t%-12s ", syscall_table[syscallnum]);
3477 else
3478 printf_filtered ("\t%-12d ", syscallnum);
3479
3480 printf_filtered ("%-8s ",
3481 prismember (&pip -> entryset, syscallnum)
3482 ? "on" : "off");
3483 printf_filtered ("%-8s ",
3484 prismember (&pip -> exitset, syscallnum)
3485 ? "on" : "off");
3486 printf_filtered ("\n");
3487 }
cc221e76
FF
3488 printf_filtered ("\n");
3489 }
3490}
3491
3492static char *
3493signalname (signo)
3494 int signo;
3495{
26a859ec 3496 const char *name;
4ace50a5
FF
3497 static char locbuf[32];
3498
3499 name = strsigno (signo);
3500 if (name == NULL)
3501 {
3502 sprintf (locbuf, "Signal %d", signo);
3503 }
3504 else
3505 {
3506 sprintf (locbuf, "%s (%d)", name, signo);
3507 }
3508 return (locbuf);
3509}
3510
3511static char *
3512errnoname (errnum)
3513 int errnum;
3514{
26a859ec 3515 const char *name;
cc221e76
FF
3516 static char locbuf[32];
3517
4ace50a5
FF
3518 name = strerrno (errnum);
3519 if (name == NULL)
cc221e76 3520 {
4ace50a5 3521 sprintf (locbuf, "Errno %d", errnum);
cc221e76
FF
3522 }
3523 else
3524 {
4ace50a5 3525 sprintf (locbuf, "%s (%d)", name, errnum);
cc221e76
FF
3526 }
3527 return (locbuf);
3528}
3529
3530static void
3531info_proc_signals (pip, summary)
3532 struct procinfo *pip;
3533 int summary;
3534{
3535 int signo;
3536
3537 if (!summary)
3538 {
3539 if (ioctl (pip -> fd, PIOCGTRACE, &pip -> trace) < 0)
3540 {
3541 print_sys_errmsg (pip -> pathname, errno);
3542 error ("PIOCGTRACE failed");
3543 }
3544
3545 printf_filtered ("Disposition of signals:\n\n");
3546 printf_filtered ("\t%-15s %-8s %-8s %-8s %s\n\n",
3547 "Signal", "Trace", "Hold", "Pending", "Description");
3548 for (signo = 0; signo < NSIG; signo++)
3549 {
3550 QUIT;
3551 printf_filtered ("\t%-15s ", signalname (signo));
3552 printf_filtered ("%-8s ",
3553 prismember (&pip -> trace, signo)
3554 ? "on" : "off");
3555 printf_filtered ("%-8s ",
3556 prismember (&pip -> prstatus.pr_sighold, signo)
3557 ? "on" : "off");
2592eef8
PS
3558
3559#ifdef PROCFS_SIGPEND_OFFSET
3560 /* Alpha OSF/1 numbers the pending signals from 1. */
3561 printf_filtered ("%-8s ",
3562 (signo ? prismember (&pip -> prstatus.pr_sigpend,
3563 signo - 1)
3564 : 0)
3565 ? "yes" : "no");
3566#else
cc221e76
FF
3567 printf_filtered ("%-8s ",
3568 prismember (&pip -> prstatus.pr_sigpend, signo)
3569 ? "yes" : "no");
2592eef8 3570#endif
4ace50a5 3571 printf_filtered (" %s\n", safe_strsignal (signo));
cc221e76
FF
3572 }
3573 printf_filtered ("\n");
3574 }
3575}
3576
3577static void
3578info_proc_faults (pip, summary)
3579 struct procinfo *pip;
3580 int summary;
3581{
3582 struct trans *transp;
3583
3584 if (!summary)
3585 {
3586 if (ioctl (pip -> fd, PIOCGFAULT, &pip -> fltset) < 0)
3587 {
3588 print_sys_errmsg (pip -> pathname, errno);
3589 error ("PIOCGFAULT failed");
3590 }
3591
3592 printf_filtered ("Current traced hardware fault set:\n\n");
3593 printf_filtered ("\t%-12s %-8s\n", "Fault", "Trace");
3594
3595 for (transp = faults_table; transp -> name != NULL; transp++)
3596 {
3597 QUIT;
3598 printf_filtered ("\t%-12s ", transp -> name);
3599 printf_filtered ("%-8s", prismember (&pip -> fltset, transp -> value)
3600 ? "on" : "off");
3601 printf_filtered ("\n");
3602 }
3603 printf_filtered ("\n");
3604 }
3605}
3606
3607static void
3608info_proc_mappings (pip, summary)
1ab3bf1b 3609 struct procinfo *pip;
cc221e76 3610 int summary;
a39ad5ce
FF
3611{
3612 int nmap;
3613 struct prmap *prmaps;
3614 struct prmap *prmap;
3615
cc221e76 3616 if (!summary)
a39ad5ce 3617 {
cc221e76 3618 printf_filtered ("Mapped address spaces:\n\n");
2592eef8
PS
3619#ifdef BFD_HOST_64_BIT
3620 printf_filtered (" %18s %18s %10s %10s %7s\n",
3621#else
5c1c5e67 3622 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2592eef8 3623#endif
cc221e76
FF
3624 "Start Addr",
3625 " End Addr",
3626 " Size",
3627 " Offset",
3628 "Flags");
3629 if (ioctl (pip -> fd, PIOCNMAP, &nmap) == 0)
a39ad5ce 3630 {
cc221e76
FF
3631 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
3632 if (ioctl (pip -> fd, PIOCMAP, prmaps) == 0)
a39ad5ce 3633 {
cc221e76
FF
3634 for (prmap = prmaps; prmap -> pr_size; ++prmap)
3635 {
2592eef8
PS
3636#ifdef BFD_HOST_64_BIT
3637 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3638#else
3639 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3640#endif
3641 (unsigned long)prmap -> pr_vaddr,
3642 (unsigned long)prmap -> pr_vaddr
3643 + prmap -> pr_size - 1,
cc221e76
FF
3644 prmap -> pr_size,
3645 prmap -> pr_off,
3646 mappingflags (prmap -> pr_mflags));
3647 }
a39ad5ce
FF
3648 }
3649 }
cc221e76 3650 printf_filtered ("\n");
a39ad5ce 3651 }
a39ad5ce
FF
3652}
3653
3654/*
3655
3656LOCAL FUNCTION
3657
cc221e76 3658 info_proc -- implement the "info proc" command
a39ad5ce
FF
3659
3660SYNOPSIS
3661
cc221e76 3662 void info_proc (char *args, int from_tty)
a39ad5ce
FF
3663
3664DESCRIPTION
3665
3666 Implement gdb's "info proc" command by using the /proc interface
3667 to print status information about any currently running process.
3668
3669 Examples of the use of "info proc" are:
3670
cc221e76
FF
3671 info proc (prints summary info for current inferior)
3672 info proc 123 (prints summary info for process with pid 123)
3673 info proc mappings (prints address mappings)
3674 info proc times (prints process/children times)
3675 info proc id (prints pid, ppid, gid, sid, etc)
3fbdd536 3676 FIXME: i proc id not implemented.
cc221e76 3677 info proc status (prints general process state info)
3fbdd536 3678 FIXME: i proc status not implemented.
cc221e76
FF
3679 info proc signals (prints info about signal handling)
3680 info proc all (prints all info)
a39ad5ce
FF
3681
3682 */
3683
3684static void
cc221e76 3685info_proc (args, from_tty)
1ab3bf1b
JG
3686 char *args;
3687 int from_tty;
a39ad5ce 3688{
8fc2b417 3689 int pid = inferior_pid;
a39ad5ce
FF
3690 struct procinfo *pip;
3691 struct cleanup *old_chain;
cc221e76
FF
3692 char **argv;
3693 int argsize;
3694 int summary = 1;
3695 int flags = 0;
3696 int syscalls = 0;
3697 int signals = 0;
3698 int faults = 0;
3699 int mappings = 0;
3700 int times = 0;
3701 int id = 0;
3702 int status = 0;
3703 int all = 0;
8fc2b417 3704 int nlwp;
47ef0da5 3705 int *lwps;
a39ad5ce
FF
3706
3707 old_chain = make_cleanup (null_cleanup, 0);
3708
de43d7d0
SG
3709 /* Default to using the current inferior if no pid specified. Note
3710 that inferior_pid may be 0, hence we set okerr. */
a39ad5ce 3711
de43d7d0 3712 pip = find_procinfo (inferior_pid, 1);
a39ad5ce 3713
a39ad5ce 3714 if (args != NULL)
35f5886e 3715 {
cc221e76 3716 if ((argv = buildargv (args)) == NULL)
a39ad5ce 3717 {
cc221e76
FF
3718 nomem (0);
3719 }
3720 make_cleanup (freeargv, (char *) argv);
3721
3722 while (*argv != NULL)
3723 {
3724 argsize = strlen (*argv);
3725 if (argsize >= 1 && strncmp (*argv, "all", argsize) == 0)
3726 {
3727 summary = 0;
3728 all = 1;
3729 }
3730 else if (argsize >= 2 && strncmp (*argv, "faults", argsize) == 0)
3731 {
3732 summary = 0;
3733 faults = 1;
3734 }
3735 else if (argsize >= 2 && strncmp (*argv, "flags", argsize) == 0)
3736 {
3737 summary = 0;
3738 flags = 1;
3739 }
3740 else if (argsize >= 1 && strncmp (*argv, "id", argsize) == 0)
3741 {
3742 summary = 0;
3743 id = 1;
3744 }
3745 else if (argsize >= 1 && strncmp (*argv, "mappings", argsize) == 0)
3746 {
3747 summary = 0;
3748 mappings = 1;
3749 }
3750 else if (argsize >= 2 && strncmp (*argv, "signals", argsize) == 0)
3751 {
3752 summary = 0;
3753 signals = 1;
3754 }
3755 else if (argsize >= 2 && strncmp (*argv, "status", argsize) == 0)
3756 {
3757 summary = 0;
3758 status = 1;
3759 }
3760 else if (argsize >= 2 && strncmp (*argv, "syscalls", argsize) == 0)
3761 {
3762 summary = 0;
3763 syscalls = 1;
3764 }
3765 else if (argsize >= 1 && strncmp (*argv, "times", argsize) == 0)
a39ad5ce 3766 {
cc221e76
FF
3767 summary = 0;
3768 times = 1;
a39ad5ce 3769 }
de43d7d0 3770 else if ((pid = atoi (*argv)) > 0)
a39ad5ce 3771 {
de43d7d0
SG
3772 pip = (struct procinfo *) xmalloc (sizeof (struct procinfo));
3773 memset (pip, 0, sizeof (*pip));
3774
3775 pip->pid = pid;
ec8ceca3 3776 if (!open_proc_file (pid, pip, O_RDONLY))
a39ad5ce
FF
3777 {
3778 perror_with_name (pip -> pathname);
3779 /* NOTREACHED */
3780 }
8fc2b417 3781 pid = pip->pid;
a39ad5ce
FF
3782 make_cleanup (close_proc_file, pip);
3783 }
cc221e76
FF
3784 else if (**argv != '\000')
3785 {
3786 error ("Unrecognized or ambiguous keyword `%s'.", *argv);
3787 }
3788 argv++;
a39ad5ce 3789 }
35f5886e 3790 }
a39ad5ce
FF
3791
3792 /* If we don't have a valid open process at this point, then we have no
3793 inferior or didn't specify a specific pid. */
3794
de43d7d0 3795 if (!pip)
35f5886e 3796 {
6fe90fc8
JK
3797 error ("\
3798No process. Start debugging a program or specify an explicit process ID.");
35f5886e 3799 }
a39ad5ce 3800 if (ioctl (pip -> fd, PIOCSTATUS, &(pip -> prstatus)) < 0)
35f5886e 3801 {
a39ad5ce
FF
3802 print_sys_errmsg (pip -> pathname, errno);
3803 error ("PIOCSTATUS failed");
35f5886e 3804 }
a39ad5ce 3805
8fc2b417
SG
3806#ifdef PIOCLWPIDS
3807 nlwp = pip->prstatus.pr_nlwp;
3808 lwps = alloca ((2 * nlwp + 2) * sizeof (id_t));
cc221e76 3809
8fc2b417 3810 if (ioctl (pip->fd, PIOCLWPIDS, lwps))
cc221e76 3811 {
8fc2b417
SG
3812 print_sys_errmsg (pip -> pathname, errno);
3813 error ("PIOCSTATUS failed");
cc221e76 3814 }
8fc2b417
SG
3815#else /* PIOCLWPIDS */
3816 nlwp = 1;
47ef0da5 3817 lwps = alloca ((2 * nlwp + 2) * sizeof *lwps);
8fc2b417
SG
3818 lwps[0] = 0;
3819#endif /* PIOCLWPIDS */
3820
3821 for (; nlwp > 0; nlwp--, lwps++)
cc221e76 3822 {
8fc2b417
SG
3823 pip = find_procinfo ((*lwps << 16) | pid, 1);
3824
3825 if (!pip)
3826 {
3827 pip = (struct procinfo *) xmalloc (sizeof (struct procinfo));
3828 memset (pip, 0, sizeof (*pip));
3829 if (!open_proc_file ((*lwps << 16) | pid, pip, O_RDONLY))
3830 continue;
3831
3832 make_cleanup (close_proc_file, pip);
3833
3834 if (ioctl (pip -> fd, PIOCSTATUS, &(pip -> prstatus)) < 0)
3835 {
3836 print_sys_errmsg (pip -> pathname, errno);
3837 error ("PIOCSTATUS failed");
3838 }
3839 }
3840
3841 /* Print verbose information of the requested type(s), or just a summary
3842 of the information for all types. */
3843
3844 printf_filtered ("\nInformation for %s.%d:\n\n", pip -> pathname, *lwps);
3845 if (summary || all || flags)
3846 {
3847 info_proc_flags (pip, summary);
3848 }
3849 if (summary || all)
3850 {
3851 info_proc_stop (pip, summary);
3852 }
3853 if (summary || all || signals || faults)
3854 {
3855 info_proc_siginfo (pip, summary);
3856 }
3857 if (summary || all || syscalls)
3858 {
3859 info_proc_syscalls (pip, summary);
3860 }
3861 if (summary || all || mappings)
3862 {
3863 info_proc_mappings (pip, summary);
3864 }
3865 if (summary || all || signals)
3866 {
3867 info_proc_signals (pip, summary);
3868 }
3869 if (summary || all || faults)
3870 {
3871 info_proc_faults (pip, summary);
3872 }
3873 printf_filtered ("\n");
3874
3875 /* All done, deal with closing any temporary process info structure,
3876 freeing temporary memory , etc. */
3877
3878 do_cleanups (old_chain);
cc221e76 3879 }
8fc2b417
SG
3880}
3881
3882/*
3883
3884LOCAL FUNCTION
3885
3886 modify_inherit_on_fork_flag - Change the inherit-on-fork flag
3887
3888SYNOPSIS
3889
3890 void modify_inherit_on_fork_flag (fd, flag)
3891
3892DESCRIPTION
3893
3894 Call this routine to modify the inherit-on-fork flag. This routine is
3895 just a nice wrapper to hide the #ifdefs needed by various systems to
3896 control this flag.
3897
3898 */
3899
3900static void
3901modify_inherit_on_fork_flag (fd, flag)
3902 int fd;
3903 int flag;
3904{
b607efe7 3905#ifdef PIOCSET
8fc2b417 3906 long pr_flags;
b607efe7 3907#endif
8fc2b417
SG
3908 int retval;
3909
3910#ifdef PIOCSET /* New method */
3911 pr_flags = PR_FORK;
3912 if (flag)
3913 retval = ioctl (fd, PIOCSET, &pr_flags);
3914 else
3915 retval = ioctl (fd, PIOCRESET, &pr_flags);
3916
3917#else
3918#ifdef PIOCSFORK /* Original method */
3919 if (flag)
3920 retval = ioctl (fd, PIOCSFORK, NULL);
3921 else
3922 retval = ioctl (fd, PIOCRFORK, NULL);
3923#else
3924 Neither PR_FORK nor PIOCSFORK exist!!!
3925#endif
3926#endif
3927
3928 if (!retval)
3929 return;
3930
3931 print_sys_errmsg ("modify_inherit_on_fork_flag", errno);
3932 error ("PIOCSFORK or PR_FORK modification failed");
3933}
3934
3935/*
3936
3937LOCAL FUNCTION
3938
3939 modify_run_on_last_close_flag - Change the run-on-last-close flag
3940
3941SYNOPSIS
3942
3943 void modify_run_on_last_close_flag (fd, flag)
3944
3945DESCRIPTION
3946
3947 Call this routine to modify the run-on-last-close flag. This routine
3948 is just a nice wrapper to hide the #ifdefs needed by various systems to
3949 control this flag.
3950
3951 */
3952
3953static void
3954modify_run_on_last_close_flag (fd, flag)
3955 int fd;
3956 int flag;
3957{
b607efe7 3958#ifdef PIOCSET
8fc2b417 3959 long pr_flags;
b607efe7 3960#endif
8fc2b417
SG
3961 int retval;
3962
3963#ifdef PIOCSET /* New method */
3964 pr_flags = PR_RLC;
3965 if (flag)
3966 retval = ioctl (fd, PIOCSET, &pr_flags);
3967 else
3968 retval = ioctl (fd, PIOCRESET, &pr_flags);
3969
3970#else
3971#ifdef PIOCSRLC /* Original method */
3972 if (flag)
3973 retval = ioctl (fd, PIOCSRLC, NULL);
3974 else
3975 retval = ioctl (fd, PIOCRRLC, NULL);
3976#else
3977 Neither PR_RLC nor PIOCSRLC exist!!!
3978#endif
3979#endif
3980
3981 if (!retval)
3982 return;
3983
3984 print_sys_errmsg ("modify_run_on_last_close_flag", errno);
3985 error ("PIOCSRLC or PR_RLC modification failed");
3986}
3987
3988/*
3989
3990LOCAL FUNCTION
3991
3992 procfs_clear_syscall_trap -- Deletes the trap for the specified system call.
3993
3994SYNOPSIS
3995
3996 void procfs_clear_syscall_trap (struct procinfo *, int syscall_num, int errok)
3997
3998DESCRIPTION
3999
4000 This function function disables traps for the specified system call.
4001 errok is non-zero if errors should be ignored.
4002 */
4003
4004static void
4005procfs_clear_syscall_trap (pi, syscall_num, errok)
4006 struct procinfo *pi;
4007 int syscall_num;
4008 int errok;
4009{
4010 sysset_t sysset;
4011 int goterr, i;
4012
4013 goterr = ioctl (pi->fd, PIOCGENTRY, &sysset) < 0;
4014
4015 if (goterr && !errok)
cc221e76 4016 {
8fc2b417
SG
4017 print_sys_errmsg (pi->pathname, errno);
4018 error ("PIOCGENTRY failed");
cc221e76 4019 }
8fc2b417
SG
4020
4021 if (!goterr)
cc221e76 4022 {
8fc2b417
SG
4023 prdelset (&sysset, syscall_num);
4024
4025 if ((ioctl (pi->fd, PIOCSENTRY, &sysset) < 0) && !errok)
4026 {
4027 print_sys_errmsg (pi->pathname, errno);
4028 error ("PIOCSENTRY failed");
4029 }
cc221e76 4030 }
8fc2b417
SG
4031
4032 goterr = ioctl (pi->fd, PIOCGEXIT, &sysset) < 0;
4033
4034 if (goterr && !errok)
cc221e76 4035 {
8fc2b417
SG
4036 procfs_clear_syscall_trap (pi, syscall_num, 1);
4037 print_sys_errmsg (pi->pathname, errno);
4038 error ("PIOCGEXIT failed");
cc221e76 4039 }
8fc2b417
SG
4040
4041 if (!goterr)
cc221e76 4042 {
8fc2b417
SG
4043 praddset (&sysset, syscall_num);
4044
4045 if ((ioctl (pi->fd, PIOCSEXIT, &sysset) < 0) && !errok)
4046 {
4047 procfs_clear_syscall_trap (pi, syscall_num, 1);
4048 print_sys_errmsg (pi->pathname, errno);
4049 error ("PIOCSEXIT failed");
4050 }
cc221e76 4051 }
8fc2b417
SG
4052
4053 if (!pi->syscall_handlers)
cc221e76 4054 {
8fc2b417
SG
4055 if (!errok)
4056 error ("procfs_clear_syscall_trap: syscall_handlers is empty");
4057 return;
cc221e76 4058 }
a39ad5ce 4059
8fc2b417 4060 /* Remove handler func from the handler list */
a39ad5ce 4061
8fc2b417
SG
4062 for (i = 0; i < pi->num_syscall_handlers; i++)
4063 if (pi->syscall_handlers[i].syscall_num == syscall_num)
4064 {
4065 if (i + 1 != pi->num_syscall_handlers)
4066 { /* Not the last entry.
4067 Move subsequent entries fwd. */
4068 memcpy (&pi->syscall_handlers[i], &pi->syscall_handlers[i + 1],
4069 (pi->num_syscall_handlers - i - 1)
4070 * sizeof (struct procfs_syscall_handler));
4071 }
4072
4073 pi->syscall_handlers = xrealloc (pi->syscall_handlers,
4074 (pi->num_syscall_handlers - 1)
4075 * sizeof (struct procfs_syscall_handler));
4076 pi->num_syscall_handlers--;
4077 return;
4078 }
4079
4080 if (!errok)
4081 error ("procfs_clear_syscall_trap: Couldn't find handler for sys call %d",
4082 syscall_num);
a39ad5ce
FF
4083}
4084
de43d7d0
SG
4085/*
4086
4087LOCAL FUNCTION
4088
8fc2b417
SG
4089 procfs_set_syscall_trap -- arrange for a function to be called when the
4090 child executes the specified system call.
de43d7d0
SG
4091
4092SYNOPSIS
4093
8fc2b417
SG
4094 void procfs_set_syscall_trap (struct procinfo *, int syscall_num, int flags,
4095 syscall_func_t *function)
de43d7d0
SG
4096
4097DESCRIPTION
4098
8fc2b417
SG
4099 This function sets up an entry and/or exit trap for the specified system
4100 call. When the child executes the specified system call, your function
4101 will be called with the call #, a flag that indicates entry or exit, and
4102 pointers to rtnval and statval (which are used by procfs_wait). The
4103 function should return non-zero if something interesting happened, zero
4104 otherwise.
de43d7d0
SG
4105 */
4106
4107static void
8fc2b417 4108procfs_set_syscall_trap (pi, syscall_num, flags, func)
de43d7d0 4109 struct procinfo *pi;
8fc2b417
SG
4110 int syscall_num;
4111 int flags;
4112 syscall_func_t *func;
de43d7d0 4113{
8fc2b417 4114 sysset_t sysset;
de43d7d0 4115
8fc2b417 4116 if (flags & PROCFS_SYSCALL_ENTRY)
de43d7d0 4117 {
8fc2b417
SG
4118 if (ioctl (pi->fd, PIOCGENTRY, &sysset) < 0)
4119 {
4120 print_sys_errmsg (pi->pathname, errno);
4121 error ("PIOCGENTRY failed");
4122 }
4123
4124 praddset (&sysset, syscall_num);
4125
4126 if (ioctl (pi->fd, PIOCSENTRY, &sysset) < 0)
4127 {
4128 print_sys_errmsg (pi->pathname, errno);
4129 error ("PIOCSENTRY failed");
4130 }
de43d7d0
SG
4131 }
4132
8fc2b417
SG
4133 if (flags & PROCFS_SYSCALL_EXIT)
4134 {
4135 if (ioctl (pi->fd, PIOCGEXIT, &sysset) < 0)
4136 {
4137 procfs_clear_syscall_trap (pi, syscall_num, 1);
4138 print_sys_errmsg (pi->pathname, errno);
4139 error ("PIOCGEXIT failed");
4140 }
de43d7d0 4141
8fc2b417 4142 praddset (&sysset, syscall_num);
eca4a350 4143
8fc2b417
SG
4144 if (ioctl (pi->fd, PIOCSEXIT, &sysset) < 0)
4145 {
4146 procfs_clear_syscall_trap (pi, syscall_num, 1);
4147 print_sys_errmsg (pi->pathname, errno);
4148 error ("PIOCSEXIT failed");
4149 }
4150 }
eca4a350 4151
8fc2b417 4152 if (!pi->syscall_handlers)
de43d7d0 4153 {
8fc2b417
SG
4154 pi->syscall_handlers = xmalloc (sizeof (struct procfs_syscall_handler));
4155 pi->syscall_handlers[0].syscall_num = syscall_num;
4156 pi->syscall_handlers[0].func = func;
4157 pi->num_syscall_handlers = 1;
de43d7d0 4158 }
8fc2b417
SG
4159 else
4160 {
4161 int i;
de43d7d0 4162
8fc2b417
SG
4163 for (i = 0; i < pi->num_syscall_handlers; i++)
4164 if (pi->syscall_handlers[i].syscall_num == syscall_num)
4165 {
4166 pi->syscall_handlers[i].func = func;
4167 return;
4168 }
de43d7d0 4169
8fc2b417
SG
4170 pi->syscall_handlers = xrealloc (pi->syscall_handlers, (i + 1)
4171 * sizeof (struct procfs_syscall_handler));
4172 pi->syscall_handlers[i].syscall_num = syscall_num;
4173 pi->syscall_handlers[i].func = func;
4174 pi->num_syscall_handlers++;
4175 }
de43d7d0 4176}
8fc2b417
SG
4177
4178#ifdef SYS_lwp_create
4179
4180/*
4181
4182LOCAL FUNCTION
4183
4184 procfs_lwp_creation_handler - handle exit from the _lwp_create syscall
4185
4186SYNOPSIS
4187
4188 int procfs_lwp_creation_handler (pi, syscall_num, why, rtnvalp, statvalp)
4189
4190DESCRIPTION
4191
4192 This routine is called both when an inferior process and it's new lwp
4193 are about to finish a _lwp_create() system call. This is the system
4194 call that Solaris uses to create a lightweight process. When the
4195 target process gets this event, we can look at sysarg[2] to find the
4196 new childs lwp ID, and create a procinfo struct from that. After that,
4197 we pretend that we got a SIGTRAP, and return non-zero to tell
4198 procfs_wait to wake up. Subsequently, wait_for_inferior gets woken up,
4199 sees the new process and continues it.
4200
4201 When we see the child exiting from lwp_create, we just contine it,
4202 since everything was handled when the parent trapped.
4203
4204NOTES
4205 In effect, we are only paying attention to the parent's completion of
4206 the lwp_create syscall. If we only paid attention to the child
4207 instead, then we wouldn't detect the creation of a suspended thread.
4208 */
4209
4210static int
4211procfs_lwp_creation_handler (pi, syscall_num, why, rtnvalp, statvalp)
4212 struct procinfo *pi;
4213 int syscall_num;
4214 int why;
4215 int *rtnvalp;
4216 int *statvalp;
4217{
4218 int lwp_id;
4219 struct procinfo *childpi;
4220
4221 /* We've just detected the completion of an lwp_create system call. Now we
4222 need to setup a procinfo struct for this thread, and notify the thread
4223 system of the new arrival. */
4224
4225 /* If lwp_create failed, then nothing interesting happened. Continue the
4226 process and go back to sleep. */
4227
4228 if (pi->prstatus.pr_reg[R_PSR] & PS_FLAG_CARRY)
4229 { /* _lwp_create failed */
4230 pi->prrun.pr_flags &= PRSTEP;
4231 pi->prrun.pr_flags |= PRCFAULT;
4232
4233 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
4234 perror_with_name (pi->pathname);
4235
4236 return 0;
4237 }
4238
4239 /* At this point, the new thread is stopped at it's first instruction, and
4240 the parent is stopped at the exit from lwp_create. */
4241
4242 if (pi->new_child) /* Child? */
4243 { /* Yes, just continue it */
4244 pi->prrun.pr_flags &= PRSTEP;
4245 pi->prrun.pr_flags |= PRCFAULT;
4246
4247 if ((pi->prstatus.pr_flags & PR_ISTOP)
4248 && ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
4249 perror_with_name (pi->pathname);
4250
4251 pi->new_child = 0; /* No longer new */
4252
4253 return 0;
4254 }
4255
4256 /* We're the proud parent of a new thread. Setup an exit trap for lwp_create
4257 in the child and continue the parent. */
4258
4259 /* Third arg is pointer to new thread id. */
4260 lwp_id = read_memory_integer (pi->prstatus.pr_sysarg[2], sizeof (int));
4261
4262 lwp_id = (lwp_id << 16) | PIDGET (pi->pid);
4263
4264 childpi = create_procinfo (lwp_id);
4265
4266 /* The new process has actually inherited the lwp_create syscall trap from
4267 it's parent, but we still have to call this to register a handler for
4268 that child. */
4269
4270 procfs_set_syscall_trap (childpi, SYS_lwp_create, PROCFS_SYSCALL_EXIT,
4271 procfs_lwp_creation_handler);
4272
4273 childpi->new_child = 1; /* Flag this as an unseen child process */
4274
4275 *rtnvalp = lwp_id; /* the new arrival. */
4276 *statvalp = (SIGTRAP << 8) | 0177;
4277
4278 return 1;
4279}
4280#endif /* SYS_lwp_create */
de43d7d0 4281
3fbdd536
JG
4282/* Fork an inferior process, and start debugging it with /proc. */
4283
4284static void
4285procfs_create_inferior (exec_file, allargs, env)
4286 char *exec_file;
4287 char *allargs;
4288 char **env;
4289{
08f74b92
JK
4290 char *shell_file = getenv ("SHELL");
4291 char *tryname;
4292 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
4293 {
4294
4295 /* We will be looking down the PATH to find shell_file. If we
4296 just do this the normal way (via execlp, which operates by
4297 attempting an exec for each element of the PATH until it
4298 finds one which succeeds), then there will be an exec for
4299 each failed attempt, each of which will cause a PR_SYSEXIT
4300 stop, and we won't know how to distinguish the PR_SYSEXIT's
4301 for these failed execs with the ones for successful execs
4302 (whether the exec has succeeded is stored at that time in the
4303 carry bit or some such architecture-specific and
4304 non-ABI-specified place).
4305
4306 So I can't think of anything better than to search the PATH
4307 now. This has several disadvantages: (1) There is a race
4308 condition; if we find a file now and it is deleted before we
4309 exec it, we lose, even if the deletion leaves a valid file
4310 further down in the PATH, (2) there is no way to know exactly
4311 what an executable (in the sense of "capable of being
4312 exec'd") file is. Using access() loses because it may lose
4313 if the caller is the superuser; failing to use it loses if
4314 there are ACLs or some such. */
4315
4316 char *p;
4317 char *p1;
f93b941b
JK
4318 /* FIXME-maybe: might want "set path" command so user can change what
4319 path is used from within GDB. */
08f74b92
JK
4320 char *path = getenv ("PATH");
4321 int len;
4322 struct stat statbuf;
4323
4324 if (path == NULL)
4325 path = "/bin:/usr/bin";
4326
4327 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
4328 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
4329 {
4330 p1 = strchr (p, ':');
4331 if (p1 != NULL)
4332 len = p1 - p;
4333 else
4334 len = strlen (p);
4335 strncpy (tryname, p, len);
4336 tryname[len] = '\0';
4337 strcat (tryname, "/");
4338 strcat (tryname, shell_file);
4339 if (access (tryname, X_OK) < 0)
4340 continue;
4341 if (stat (tryname, &statbuf) < 0)
4342 continue;
4343 if (!S_ISREG (statbuf.st_mode))
4344 /* We certainly need to reject directories. I'm not quite
4345 as sure about FIFOs, sockets, etc., but I kind of doubt
4346 that people want to exec() these things. */
4347 continue;
4348 break;
4349 }
4350 if (p == NULL)
4351 /* Not found. This must be an error rather than merely passing
4352 the file to execlp(), because execlp() would try all the
4353 exec()s, causing GDB to get confused. */
4354 error ("Can't find shell %s in PATH", shell_file);
4355
4356 shell_file = tryname;
4357 }
4358
3fbdd536 4359 fork_inferior (exec_file, allargs, env,
08f74b92
JK
4360 proc_set_exec_trap, procfs_init_inferior, shell_file);
4361
3fbdd536
JG
4362 /* We are at the first instruction we care about. */
4363 /* Pedal to the metal... */
de43d7d0 4364
67ac9759 4365 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
3fbdd536
JG
4366}
4367
4368/* Clean up after the inferior dies. */
4369
4370static void
4371procfs_mourn_inferior ()
4372{
fb63d460 4373 struct procinfo *pi;
cd4104e0 4374 struct procinfo *next_pi;
fb63d460 4375
cd4104e0
TL
4376 for (pi = procinfo_list; pi; pi = next_pi)
4377 {
4378 next_pi = pi->next;
4379 unconditionally_kill_inferior (pi);
4380 }
fb63d460 4381
3fbdd536
JG
4382 unpush_target (&procfs_ops);
4383 generic_mourn_inferior ();
4384}
4385
cd4104e0 4386
3fbdd536
JG
4387/* Mark our target-struct as eligible for stray "run" and "attach" commands. */
4388static int
4389procfs_can_run ()
4390{
8fc2b417
SG
4391 /* This variable is controlled by modules that sit atop procfs that may layer
4392 their own process structure atop that provided here. sol-thread.c does
4393 this because of the Solaris two-level thread model. */
4394
4395 return !procfs_suppress_run;
3fbdd536 4396}
f5a8f1a6 4397#ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
999dd04b
JL
4398\f
4399/* Insert a watchpoint */
4400int
4401procfs_set_watchpoint(pid, addr, len, rw)
4402 int pid;
4403 CORE_ADDR addr;
4404 int len;
4405 int rw;
4406{
4407 struct procinfo *pi;
4408 prwatch_t wpt;
4409
4410 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
4411 wpt.pr_vaddr = (caddr_t)addr;
4412 wpt.pr_size = len;
4413 wpt.pr_wflags = ((rw & 1) ? MA_READ : 0) | ((rw & 2) ? MA_WRITE : 0);
4414 if (ioctl (pi->fd, PIOCSWATCH, &wpt) < 0)
4415 {
4416 if (errno == E2BIG)
4417 return -1;
4418 /* Currently it sometimes happens that the same watchpoint gets
4419 deleted twice - don't die in this case (FIXME please) */
4420 if (errno == ESRCH && len == 0)
4421 return 0;
4422 print_sys_errmsg (pi->pathname, errno);
4423 error ("PIOCSWATCH failed");
4424 }
4425 return 0;
4426}
4427
4428int
4429procfs_stopped_by_watchpoint(pid)
4430 int pid;
4431{
4432 struct procinfo *pi;
4433 short what;
4434 short why;
4435
4436 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
4437 if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
4438 {
4439 why = pi->prstatus.pr_why;
4440 what = pi->prstatus.pr_what;
4441 if (why == PR_FAULTED
6bc194d2 4442#if defined (FLTWATCH) && defined (FLTKWATCH)
255181a9 4443 && (what == FLTWATCH || what == FLTKWATCH)
6bc194d2
JL
4444#else
4445#ifdef FLTWATCH
4446 && (what == FLTWATCH)
4447#endif
4448#ifdef FLTKWATCH
4449 && (what == FLTKWATCH)
4450#endif
72b8ca51 4451#endif
6bc194d2 4452 )
999dd04b
JL
4453 return what;
4454 }
4455 return 0;
4456}
6bc194d2 4457#endif
999dd04b 4458
8fc2b417
SG
4459/* Why is this necessary? Shouldn't dead threads just be removed from the
4460 thread database? */
4461
9b33e492 4462static int
8fc2b417
SG
4463procfs_thread_alive (pid)
4464 int pid;
4465{
4466 return 1;
4467}
4468
78b459a7
SG
4469/* Send a SIGINT to the process group. This acts just like the user typed a
4470 ^C on the controlling terminal.
4471
4472 XXX - This may not be correct for all systems. Some may want to use
4473 killpg() instead of kill (-pgrp). */
4474
9b33e492 4475static void
2592eef8 4476procfs_stop ()
78b459a7
SG
4477{
4478 extern pid_t inferior_process_group;
4479
4480 kill (-inferior_process_group, SIGINT);
4481}
9b33e492
SG
4482\f
4483/* Convert a pid to printable form. */
78b459a7 4484
9b33e492
SG
4485#ifdef TIDGET
4486char *
4487procfs_pid_to_str (pid)
4488 int pid;
4489{
4490 static char buf[100];
4491
4492 sprintf (buf, "Kernel thread %d", TIDGET (pid));
4493
4494 return buf;
4495}
4496#endif /* TIDGET */
3fbdd536
JG
4497\f
4498struct target_ops procfs_ops = {
4499 "procfs", /* to_shortname */
4500 "Unix /proc child process", /* to_longname */
4501 "Unix /proc child process (started by the \"run\" command).", /* to_doc */
4502 procfs_open, /* to_open */
4503 0, /* to_close */
4504 procfs_attach, /* to_attach */
4505 procfs_detach, /* to_detach */
4506 procfs_resume, /* to_resume */
4507 procfs_wait, /* to_wait */
4508 procfs_fetch_registers, /* to_fetch_registers */
4509 procfs_store_registers, /* to_store_registers */
4510 procfs_prepare_to_store, /* to_prepare_to_store */
4511 procfs_xfer_memory, /* to_xfer_memory */
4512 procfs_files_info, /* to_files_info */
4513 memory_insert_breakpoint, /* to_insert_breakpoint */
4514 memory_remove_breakpoint, /* to_remove_breakpoint */
4515 terminal_init_inferior, /* to_terminal_init */
4516 terminal_inferior, /* to_terminal_inferior */
4517 terminal_ours_for_output, /* to_terminal_ours_for_output */
4518 terminal_ours, /* to_terminal_ours */
4519 child_terminal_info, /* to_terminal_info */
4520 procfs_kill_inferior, /* to_kill */
4521 0, /* to_load */
4522 0, /* to_lookup_symbol */
4523 procfs_create_inferior, /* to_create_inferior */
4524 procfs_mourn_inferior, /* to_mourn_inferior */
4525 procfs_can_run, /* to_can_run */
3950a34e 4526 procfs_notice_signals, /* to_notice_signals */
8fc2b417 4527 procfs_thread_alive, /* to_thread_alive */
2592eef8 4528 procfs_stop, /* to_stop */
3fbdd536
JG
4529 process_stratum, /* to_stratum */
4530 0, /* to_next */
4531 1, /* to_has_all_memory */
4532 1, /* to_has_memory */
4533 1, /* to_has_stack */
4534 1, /* to_has_registers */
4535 1, /* to_has_execution */
4536 0, /* sections */
4537 0, /* sections_end */
4538 OPS_MAGIC /* to_magic */
4539};
4540
3fbdd536
JG
4541void
4542_initialize_procfs ()
4543{
2592eef8
PS
4544#ifdef HAVE_OPTIONAL_PROC_FS
4545 char procname[32];
4546 int fd;
4547
4548 /* If we have an optional /proc filesystem (e.g. under OSF/1),
4549 don't add procfs support if we cannot access the running
4550 GDB via /proc. */
4551 sprintf (procname, PROC_NAME_FMT, getpid ());
4552 if ((fd = open (procname, O_RDONLY)) < 0)
4553 return;
4554 close (fd);
4555#endif
4556
3fbdd536
JG
4557 add_target (&procfs_ops);
4558
4559 add_info ("proc", info_proc,
cc221e76
FF
4560"Show process status information using /proc entry.\n\
4561Specify process id or use current inferior by default.\n\
4562Specify keywords for detailed information; default is summary.\n\
4563Keywords are: `all', `faults', `flags', `id', `mappings', `signals',\n\
4564`status', `syscalls', and `times'.\n\
3fbdd536 4565Unambiguous abbreviations may be used.");
a39ad5ce 4566
cc221e76 4567 init_syscall_table ();
35f5886e 4568}
This page took 0.498562 seconds and 4 git commands to generate.