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