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