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