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