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