Back out change to signals.exp (test_handle_all_print): Add setup_xfail for "alpha...
[deliverable/binutils-gdb.git] / gdb / procfs.c
CommitLineData
35f5886e 1/* Machine independent support for SVR4 /proc (process file system) for GDB.
255181a9 2 Copyright 1991, 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
35f5886e
FF
3 Written by Fred Fish at Cygnus Support.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
6c9638b4 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
35f5886e
FF
20
21
22/* N O T E S
23
24For information on the details of using /proc consult section proc(4)
25in the UNIX System V Release 4 System Administrator's Reference Manual.
26
27The general register and floating point register sets are manipulated by
28separate ioctl's. This file makes the assumption that if FP0_REGNUM is
29defined, then support for the floating point register set is desired,
30regardless of whether or not the actual target has floating point hardware.
31
32 */
33
34
5129100c 35#include "defs.h"
35f5886e 36
08f74b92 37#include <sys/types.h>
407a8389 38#include <time.h>
2592eef8
PS
39#include <sys/fault.h>
40#include <sys/syscall.h>
35f5886e
FF
41#include <sys/procfs.h>
42#include <fcntl.h>
43#include <errno.h>
2b576293 44#include "gdb_string.h"
de43d7d0
SG
45#include <stropts.h>
46#include <poll.h>
08f74b92 47#include <unistd.h>
2b576293 48#include "gdb_stat.h"
35f5886e 49
35f5886e
FF
50#include "inferior.h"
51#include "target.h"
51b57ded 52#include "command.h"
3fbdd536 53#include "gdbcore.h"
e3be225e 54#include "thread.h"
cc221e76
FF
55
56#define MAX_SYSCALLS 256 /* Maximum number of syscalls for table */
35f5886e
FF
57
58#ifndef PROC_NAME_FMT
6c316cfd 59#define PROC_NAME_FMT "/proc/%05d"
35f5886e
FF
60#endif
61
3fbdd536
JG
62extern struct target_ops procfs_ops; /* Forward declaration */
63
35f5886e
FF
64#if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */
65CORE_ADDR kernel_u_addr;
66#endif
67
cc221e76
FF
68#ifdef BROKEN_SIGINFO_H /* Workaround broken SGS <sys/siginfo.h> */
69#undef si_pid
70#define si_pid _data._proc.pid
71#undef si_uid
72#define si_uid _data._proc._pdata._kill.uid
73#endif /* BROKEN_SIGINFO_H */
74
35f5886e
FF
75/* All access to the inferior, either one started by gdb or one that has
76 been attached to, is controlled by an instance of a procinfo structure,
77 defined below. Since gdb currently only handles one inferior at a time,
a39ad5ce
FF
78 the procinfo structure for the inferior is statically allocated and
79 only one exists at any given time. There is a separate procinfo
80 structure for use by the "info proc" command, so that we can print
81 useful information about any random process without interfering with
82 the inferior's procinfo information. */
35f5886e
FF
83
84struct procinfo {
de43d7d0 85 struct procinfo *next;
35f5886e
FF
86 int pid; /* Process ID of inferior */
87 int fd; /* File descriptor for /proc entry */
88 char *pathname; /* Pathname to /proc entry */
de43d7d0 89 int had_event; /* poll/select says something happened */
35f5886e 90 int was_stopped; /* Nonzero if was stopped prior to attach */
d65eee73 91 int nopass_next_sigstop; /* Don't pass a sigstop on next resume */
35f5886e
FF
92 prrun_t prrun; /* Control state when it is run */
93 prstatus_t prstatus; /* Current process status info */
94 gregset_t gregset; /* General register set */
95 fpregset_t fpregset; /* Floating point register set */
96 fltset_t fltset; /* Current traced hardware fault set */
97 sigset_t trace; /* Current traced signal set */
98 sysset_t exitset; /* Current traced system call exit set */
99 sysset_t entryset; /* Current traced system call entry set */
cc221e76
FF
100 fltset_t saved_fltset; /* Saved traced hardware fault set */
101 sigset_t saved_trace; /* Saved traced signal set */
102 sigset_t saved_sighold; /* Saved held signal set */
103 sysset_t saved_exitset; /* Saved traced system call exit set */
104 sysset_t saved_entryset; /* Saved traced system call entry set */
a39ad5ce
FF
105};
106
de43d7d0
SG
107/* List of inferior process information */
108static struct procinfo *procinfo_list = NULL;
109
110static struct pollfd *poll_list; /* pollfds used for waiting on /proc */
111
112static int num_poll_list = 0; /* Number of entries in poll_list */
113
114static int last_resume_pid = -1; /* Last pid used with procfs_resume */
35f5886e 115
cc221e76
FF
116/* Much of the information used in the /proc interface, particularly for
117 printing status information, is kept as tables of structures of the
118 following form. These tables can be used to map numeric values to
119 their symbolic names and to a string that describes their specific use. */
120
121struct trans {
122 int value; /* The numeric value */
123 char *name; /* The equivalent symbolic value */
124 char *desc; /* Short description of value */
125};
126
127/* Translate bits in the pr_flags member of the prstatus structure, into the
128 names and desc information. */
129
130static struct trans pr_flag_table[] =
131{
132#if defined (PR_STOPPED)
e3be225e 133 { PR_STOPPED, "PR_STOPPED", "Process is stopped" },
cc221e76
FF
134#endif
135#if defined (PR_ISTOP)
e3be225e 136 { PR_ISTOP, "PR_ISTOP", "Stopped on an event of interest" },
cc221e76
FF
137#endif
138#if defined (PR_DSTOP)
e3be225e 139 { PR_DSTOP, "PR_DSTOP", "A stop directive is in effect" },
cc221e76
FF
140#endif
141#if defined (PR_ASLEEP)
e3be225e 142 { PR_ASLEEP, "PR_ASLEEP", "Sleeping in an interruptible system call" },
cc221e76
FF
143#endif
144#if defined (PR_FORK)
e3be225e 145 { PR_FORK, "PR_FORK", "Inherit-on-fork is in effect" },
cc221e76
FF
146#endif
147#if defined (PR_RLC)
e3be225e 148 { PR_RLC, "PR_RLC", "Run-on-last-close is in effect" },
cc221e76
FF
149#endif
150#if defined (PR_PTRACE)
e3be225e 151 { PR_PTRACE, "PR_PTRACE", "Process is being controlled by ptrace" },
cc221e76
FF
152#endif
153#if defined (PR_PCINVAL)
e3be225e 154 { PR_PCINVAL, "PR_PCINVAL", "PC refers to an invalid virtual address" },
cc221e76
FF
155#endif
156#if defined (PR_ISSYS)
e3be225e 157 { PR_ISSYS, "PR_ISSYS", "Is a system process" },
5c1c5e67
FF
158#endif
159#if defined (PR_STEP)
e3be225e 160 { PR_STEP, "PR_STEP", "Process has single step pending" },
5c1c5e67
FF
161#endif
162#if defined (PR_KLC)
e3be225e 163 { PR_KLC, "PR_KLC", "Kill-on-last-close is in effect" },
5c1c5e67
FF
164#endif
165#if defined (PR_ASYNC)
e3be225e 166 { PR_ASYNC, "PR_ASYNC", "Asynchronous stop is in effect" },
5c1c5e67
FF
167#endif
168#if defined (PR_PCOMPAT)
e3be225e 169 { PR_PCOMPAT, "PR_PCOMPAT", "Ptrace compatibility mode in effect" },
cc221e76 170#endif
e3be225e 171 { 0, NULL, NULL }
cc221e76
FF
172};
173
174/* Translate values in the pr_why field of the prstatus struct. */
175
176static struct trans pr_why_table[] =
177{
178#if defined (PR_REQUESTED)
e3be225e 179 { PR_REQUESTED, "PR_REQUESTED", "Directed to stop via PIOCSTOP/PIOCWSTOP" },
cc221e76
FF
180#endif
181#if defined (PR_SIGNALLED)
e3be225e 182 { PR_SIGNALLED, "PR_SIGNALLED", "Receipt of a traced signal" },
cc221e76
FF
183#endif
184#if defined (PR_FAULTED)
e3be225e 185 { PR_FAULTED, "PR_FAULTED", "Incurred a traced hardware fault" },
cc221e76
FF
186#endif
187#if defined (PR_SYSENTRY)
e3be225e 188 { PR_SYSENTRY, "PR_SYSENTRY", "Entry to a traced system call" },
cc221e76
FF
189#endif
190#if defined (PR_SYSEXIT)
e3be225e 191 { PR_SYSEXIT, "PR_SYSEXIT", "Exit from a traced system call" },
cc221e76
FF
192#endif
193#if defined (PR_JOBCONTROL)
e3be225e 194 { PR_JOBCONTROL, "PR_JOBCONTROL", "Default job control stop signal action" },
5c1c5e67
FF
195#endif
196#if defined (PR_SUSPENDED)
e3be225e 197 { PR_SUSPENDED, "PR_SUSPENDED", "Process suspended" },
cc221e76 198#endif
e3be225e 199 { 0, NULL, NULL }
cc221e76
FF
200};
201
202/* Hardware fault translation table. */
203
204static struct trans faults_table[] =
205{
206#if defined (FLTILL)
e3be225e 207 { FLTILL, "FLTILL", "Illegal instruction" },
cc221e76
FF
208#endif
209#if defined (FLTPRIV)
e3be225e 210 { FLTPRIV, "FLTPRIV", "Privileged instruction" },
cc221e76
FF
211#endif
212#if defined (FLTBPT)
e3be225e 213 { FLTBPT, "FLTBPT", "Breakpoint trap" },
cc221e76
FF
214#endif
215#if defined (FLTTRACE)
e3be225e 216 { FLTTRACE, "FLTTRACE", "Trace trap" },
cc221e76
FF
217#endif
218#if defined (FLTACCESS)
e3be225e 219 { FLTACCESS, "FLTACCESS", "Memory access fault" },
cc221e76
FF
220#endif
221#if defined (FLTBOUNDS)
e3be225e 222 { FLTBOUNDS, "FLTBOUNDS", "Memory bounds violation" },
cc221e76
FF
223#endif
224#if defined (FLTIOVF)
e3be225e 225 { FLTIOVF, "FLTIOVF", "Integer overflow" },
cc221e76
FF
226#endif
227#if defined (FLTIZDIV)
e3be225e 228 { FLTIZDIV, "FLTIZDIV", "Integer zero divide" },
cc221e76
FF
229#endif
230#if defined (FLTFPE)
e3be225e 231 { FLTFPE, "FLTFPE", "Floating-point exception" },
cc221e76
FF
232#endif
233#if defined (FLTSTACK)
e3be225e 234 { FLTSTACK, "FLTSTACK", "Unrecoverable stack fault" },
cc221e76
FF
235#endif
236#if defined (FLTPAGE)
e3be225e 237 { FLTPAGE, "FLTPAGE", "Recoverable page fault" },
cc221e76 238#endif
e3be225e 239 { 0, NULL, NULL }
cc221e76
FF
240};
241
242/* Translation table for signal generation information. See UNIX System
243 V Release 4 Programmer's Reference Manual, siginfo(5). */
244
245static struct sigcode {
246 int signo;
247 int code;
248 char *codename;
249 char *desc;
250} siginfo_table[] = {
251#if defined (SIGILL) && defined (ILL_ILLOPC)
e3be225e 252 { SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode" },
cc221e76
FF
253#endif
254#if defined (SIGILL) && defined (ILL_ILLOPN)
e3be225e 255 { SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand", },
cc221e76
FF
256#endif
257#if defined (SIGILL) && defined (ILL_ILLADR)
e3be225e 258 { SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode" },
cc221e76
FF
259#endif
260#if defined (SIGILL) && defined (ILL_ILLTRP)
e3be225e 261 { SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap" },
cc221e76
FF
262#endif
263#if defined (SIGILL) && defined (ILL_PRVOPC)
e3be225e 264 { SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode" },
cc221e76
FF
265#endif
266#if defined (SIGILL) && defined (ILL_PRVREG)
e3be225e 267 { SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register" },
cc221e76
FF
268#endif
269#if defined (SIGILL) && defined (ILL_COPROC)
e3be225e 270 { SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error" },
cc221e76
FF
271#endif
272#if defined (SIGILL) && defined (ILL_BADSTK)
e3be225e 273 { SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error" },
cc221e76
FF
274#endif
275#if defined (SIGFPE) && defined (FPE_INTDIV)
e3be225e 276 { SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero" },
cc221e76
FF
277#endif
278#if defined (SIGFPE) && defined (FPE_INTOVF)
e3be225e 279 { SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow" },
cc221e76
FF
280#endif
281#if defined (SIGFPE) && defined (FPE_FLTDIV)
e3be225e 282 { SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating point divide by zero" },
cc221e76
FF
283#endif
284#if defined (SIGFPE) && defined (FPE_FLTOVF)
e3be225e 285 { SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating point overflow" },
cc221e76
FF
286#endif
287#if defined (SIGFPE) && defined (FPE_FLTUND)
e3be225e 288 { SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating point underflow" },
cc221e76
FF
289#endif
290#if defined (SIGFPE) && defined (FPE_FLTRES)
e3be225e 291 { SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating point inexact result" },
cc221e76
FF
292#endif
293#if defined (SIGFPE) && defined (FPE_FLTINV)
e3be225e 294 { SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating point operation" },
cc221e76
FF
295#endif
296#if defined (SIGFPE) && defined (FPE_FLTSUB)
e3be225e 297 { SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range" },
cc221e76
FF
298#endif
299#if defined (SIGSEGV) && defined (SEGV_MAPERR)
e3be225e 300 { SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object" },
cc221e76
FF
301#endif
302#if defined (SIGSEGV) && defined (SEGV_ACCERR)
e3be225e 303 { SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for object" },
cc221e76
FF
304#endif
305#if defined (SIGBUS) && defined (BUS_ADRALN)
e3be225e 306 { SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment" },
cc221e76
FF
307#endif
308#if defined (SIGBUS) && defined (BUS_ADRERR)
e3be225e 309 { SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Non-existent physical address" },
cc221e76
FF
310#endif
311#if defined (SIGBUS) && defined (BUS_OBJERR)
e3be225e 312 { SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object specific hardware error" },
cc221e76
FF
313#endif
314#if defined (SIGTRAP) && defined (TRAP_BRKPT)
e3be225e 315 { SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint" },
cc221e76
FF
316#endif
317#if defined (SIGTRAP) && defined (TRAP_TRACE)
e3be225e 318 { SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap" },
cc221e76
FF
319#endif
320#if defined (SIGCLD) && defined (CLD_EXITED)
e3be225e 321 { SIGCLD, CLD_EXITED, "CLD_EXITED", "Child has exited" },
cc221e76
FF
322#endif
323#if defined (SIGCLD) && defined (CLD_KILLED)
e3be225e 324 { SIGCLD, CLD_KILLED, "CLD_KILLED", "Child was killed" },
cc221e76
FF
325#endif
326#if defined (SIGCLD) && defined (CLD_DUMPED)
e3be225e 327 { SIGCLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally" },
cc221e76
FF
328#endif
329#if defined (SIGCLD) && defined (CLD_TRAPPED)
e3be225e 330 { SIGCLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped" },
cc221e76
FF
331#endif
332#if defined (SIGCLD) && defined (CLD_STOPPED)
e3be225e 333 { SIGCLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped" },
cc221e76
FF
334#endif
335#if defined (SIGCLD) && defined (CLD_CONTINUED)
e3be225e 336 { SIGCLD, CLD_CONTINUED, "CLD_CONTINUED", "Stopped child had continued" },
cc221e76
FF
337#endif
338#if defined (SIGPOLL) && defined (POLL_IN)
e3be225e 339 { SIGPOLL, POLL_IN, "POLL_IN", "Input input available" },
cc221e76
FF
340#endif
341#if defined (SIGPOLL) && defined (POLL_OUT)
e3be225e 342 { SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available" },
cc221e76
FF
343#endif
344#if defined (SIGPOLL) && defined (POLL_MSG)
e3be225e 345 { SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available" },
cc221e76
FF
346#endif
347#if defined (SIGPOLL) && defined (POLL_ERR)
e3be225e 348 { SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error" },
cc221e76
FF
349#endif
350#if defined (SIGPOLL) && defined (POLL_PRI)
e3be225e 351 { SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available" },
cc221e76
FF
352#endif
353#if defined (SIGPOLL) && defined (POLL_HUP)
e3be225e 354 { SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected" },
cc221e76 355#endif
e3be225e 356 { 0, 0, NULL, NULL }
cc221e76
FF
357};
358
cc221e76
FF
359static char *syscall_table[MAX_SYSCALLS];
360
1ab3bf1b
JG
361/* Prototypes for local functions */
362
e3be225e 363static void set_proc_siginfo PARAMS ((struct procinfo *, int));
6b801388 364
e3be225e 365static void init_syscall_table PARAMS ((void));
cc221e76 366
e3be225e 367static char *syscallname PARAMS ((int));
cc221e76 368
e3be225e 369static char *signalname PARAMS ((int));
cc221e76 370
e3be225e 371static char *errnoname PARAMS ((int));
4ace50a5 372
e3be225e 373static int proc_address_to_fd PARAMS ((struct procinfo *, CORE_ADDR, int));
1ab3bf1b 374
e3be225e 375static int open_proc_file PARAMS ((int, struct procinfo *, int));
1ab3bf1b 376
e3be225e 377static void close_proc_file PARAMS ((struct procinfo *));
1ab3bf1b 378
e3be225e 379static void unconditionally_kill_inferior PARAMS ((struct procinfo *));
1ab3bf1b 380
e3be225e 381static NORETURN void proc_init_failed PARAMS ((struct procinfo *, char *)) ATTR_NORETURN;
1ab3bf1b 382
e3be225e 383static void info_proc PARAMS ((char *, int));
cc221e76 384
e3be225e 385static void info_proc_flags PARAMS ((struct procinfo *, int));
cc221e76 386
e3be225e 387static void info_proc_stop PARAMS ((struct procinfo *, int));
1ab3bf1b 388
e3be225e 389static void info_proc_siginfo PARAMS ((struct procinfo *, int));
cc221e76 390
e3be225e 391static void info_proc_syscalls PARAMS ((struct procinfo *, int));
cc221e76 392
e3be225e 393static void info_proc_mappings PARAMS ((struct procinfo *, int));
cc221e76 394
e3be225e 395static void info_proc_signals PARAMS ((struct procinfo *, int));
cc221e76 396
e3be225e 397static void info_proc_faults PARAMS ((struct procinfo *, int));
1ab3bf1b 398
e3be225e 399static char *mappingflags PARAMS ((long));
1ab3bf1b 400
e3be225e 401static char *lookupname PARAMS ((struct trans *, unsigned int, char *));
cc221e76 402
e3be225e 403static char *lookupdesc PARAMS ((struct trans *, unsigned int));
cc221e76 404
e3be225e 405static int do_attach PARAMS ((int pid));
3fbdd536 406
e3be225e 407static void do_detach PARAMS ((int siggnal));
3fbdd536 408
e3be225e 409static void procfs_create_inferior PARAMS ((char *, char *, char **));
3fbdd536 410
e3be225e 411static void procfs_notice_signals PARAMS ((int pid));
de43d7d0 412
e3be225e 413static struct procinfo *find_procinfo PARAMS ((pid_t pid, int okfail));
3950a34e 414
1ab3bf1b
JG
415/* External function prototypes that can't be easily included in any
416 header file because the args are typedefs in system include files. */
417
e3be225e 418extern void supply_gregset PARAMS ((gregset_t *));
1ab3bf1b 419
e3be225e 420extern void fill_gregset PARAMS ((gregset_t *, int));
1ab3bf1b 421
e3be225e 422extern void supply_fpregset PARAMS ((fpregset_t *));
1ab3bf1b 423
e3be225e 424extern void fill_fpregset PARAMS ((fpregset_t *, int));
35f5886e 425
cc221e76
FF
426/*
427
de43d7d0
SG
428LOCAL FUNCTION
429
430 find_procinfo -- convert a process id to a struct procinfo
431
432SYNOPSIS
433
434 static struct procinfo * find_procinfo (pid_t pid, int okfail);
435
436DESCRIPTION
437
438 Given a process id, look it up in the procinfo chain. Returns
439 a struct procinfo *. If can't find pid, then call error(),
440 unless okfail is set, in which case, return NULL;
441 */
442
443static struct procinfo *
444find_procinfo (pid, okfail)
445 pid_t pid;
446 int okfail;
447{
448 struct procinfo *procinfo;
449
450 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
451 if (procinfo->pid == pid)
452 return procinfo;
453
454 if (okfail)
455 return NULL;
456
457 error ("procfs (find_procinfo): Couldn't locate pid %d", pid);
458}
459
460/*
461
7c5d526e
SG
462LOCAL MACRO
463
464 current_procinfo -- convert inferior_pid to a struct procinfo
465
466SYNOPSIS
467
468 static struct procinfo * current_procinfo;
469
470DESCRIPTION
471
472 Looks up inferior_pid in the procinfo chain. Always returns a
473 struct procinfo *. If process can't be found, we error() out.
474 */
475
476#define current_procinfo find_procinfo (inferior_pid, 0)
477
478/*
479
de43d7d0
SG
480LOCAL FUNCTION
481
482 add_fd -- Add the fd to the poll/select list
483
484SYNOPSIS
485
486 static void add_fd (struct procinfo *);
487
488DESCRIPTION
489
490 Add the fd of the supplied procinfo to the list of fds used for
491 poll/select operations.
492 */
493
494static void
495add_fd (pi)
496 struct procinfo *pi;
497{
498 if (num_poll_list <= 0)
499 poll_list = (struct pollfd *) xmalloc (sizeof (struct pollfd));
500 else
501 poll_list = (struct pollfd *) xrealloc (poll_list,
502 (num_poll_list + 1)
503 * sizeof (struct pollfd));
504 poll_list[num_poll_list].fd = pi->fd;
505 poll_list[num_poll_list].events = POLLPRI;
506
507 num_poll_list++;
508}
509
510static void
511remove_fd (pi)
512 struct procinfo *pi;
513{
514 int i;
515
516 for (i = 0; i < num_poll_list; i++)
517 {
518 if (poll_list[i].fd == pi->fd)
519 {
520 if (i != num_poll_list - 1)
521 memcpy (poll_list, poll_list + i + 1,
522 (num_poll_list - i - 1) * sizeof (struct pollfd));
523
524 num_poll_list--;
525
526 if (num_poll_list == 0)
527 free (poll_list);
528 else
529 poll_list = (struct pollfd *) xrealloc (poll_list,
530 num_poll_list
531 * sizeof (struct pollfd));
532 return;
533 }
534 }
535}
536
7c5d526e 537#define LOSING_POLL unixware_sux
de43d7d0 538
7c5d526e
SG
539static struct procinfo *
540wait_fd ()
541{
542 struct procinfo *pi;
543 int num_fds;
544 int i;
de43d7d0 545
1e75b5f5 546 set_sigint_trap (); /* Causes SIGINT to be passed on to the
7c5d526e 547 attached process. */
429f1c9f 548 set_sigio_trap ();
de43d7d0 549
7c5d526e
SG
550#ifndef LOSING_POLL
551 num_fds = poll (poll_list, num_poll_list, -1);
552#else
553 pi = current_procinfo;
de43d7d0 554
8afd05c0 555 while (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0)
7c5d526e 556 {
cef0333e
PS
557 if (errno == ENOENT)
558 {
559 /* Process exited. */
560 pi->prstatus.pr_flags = 0;
561 break;
562 }
563 else if (errno != EINTR)
8afd05c0
JK
564 {
565 print_sys_errmsg (pi->pathname, errno);
566 error ("PIOCWSTOP failed");
567 }
7c5d526e 568 }
fb63d460 569 pi->had_event = 1;
7c5d526e
SG
570#endif
571
1e75b5f5 572 clear_sigint_trap ();
429f1c9f 573 clear_sigio_trap ();
de43d7d0 574
7c5d526e 575#ifndef LOSING_POLL
de43d7d0 576
7c5d526e
SG
577 if (num_fds <= 0)
578 {
579 print_sys_errmsg ("poll failed\n", errno);
580 error ("Poll failed, returned %d", num_fds);
581 }
582
583 for (i = 0; i < num_poll_list && num_fds > 0; i++)
584 {
585 if ((poll_list[i].revents & (POLLPRI|POLLERR|POLLHUP|POLLNVAL)) == 0)
586 continue;
587 for (pi = procinfo_list; pi; pi = pi->next)
588 {
589 if (poll_list[i].fd == pi->fd)
590 {
591 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
592 {
593 print_sys_errmsg (pi->pathname, errno);
594 error ("PIOCSTATUS failed");
595 }
596 num_fds--;
597 pi->had_event = 1;
598 break;
599 }
600 }
601 if (!pi)
602 error ("procfs_wait: Couldn't find procinfo for fd %d\n",
603 poll_list[i].fd);
604 }
605#endif /* LOSING_POLL */
606
607 return pi;
608}
de43d7d0
SG
609
610/*
611
cc221e76
FF
612LOCAL FUNCTION
613
614 lookupdesc -- translate a value to a summary desc string
615
616SYNOPSIS
617
618 static char *lookupdesc (struct trans *transp, unsigned int val);
619
620DESCRIPTION
621
622 Given a pointer to a translation table and a value to be translated,
623 lookup the desc string and return it.
624 */
625
626static char *
627lookupdesc (transp, val)
628 struct trans *transp;
629 unsigned int val;
630{
631 char *desc;
632
633 for (desc = NULL; transp -> name != NULL; transp++)
634 {
635 if (transp -> value == val)
636 {
637 desc = transp -> desc;
638 break;
639 }
640 }
641
642 /* Didn't find a translation for the specified value, set a default one. */
643
644 if (desc == NULL)
645 {
646 desc = "Unknown";
647 }
648 return (desc);
649}
650
651/*
652
653LOCAL FUNCTION
654
655 lookupname -- translate a value to symbolic name
656
657SYNOPSIS
658
659 static char *lookupname (struct trans *transp, unsigned int val,
660 char *prefix);
661
662DESCRIPTION
663
664 Given a pointer to a translation table, a value to be translated,
665 and a default prefix to return if the value can't be translated,
666 match the value with one of the translation table entries and
667 return a pointer to the symbolic name.
668
669 If no match is found it just returns the value as a printable string,
670 with the given prefix. The previous such value, if any, is freed
671 at this time.
672 */
673
674static char *
675lookupname (transp, val, prefix)
676 struct trans *transp;
677 unsigned int val;
678 char *prefix;
679{
680 static char *locbuf;
681 char *name;
682
683 for (name = NULL; transp -> name != NULL; transp++)
684 {
685 if (transp -> value == val)
686 {
687 name = transp -> name;
688 break;
689 }
690 }
691
692 /* Didn't find a translation for the specified value, build a default
693 one using the specified prefix and return it. The lifetime of
694 the value is only until the next one is needed. */
695
696 if (name == NULL)
697 {
698 if (locbuf != NULL)
699 {
700 free (locbuf);
701 }
702 locbuf = xmalloc (strlen (prefix) + 16);
4ed3a9ea 703 sprintf (locbuf, "%s %u", prefix, val);
cc221e76
FF
704 name = locbuf;
705 }
706 return (name);
707}
708
709static char *
710sigcodename (sip)
711 siginfo_t *sip;
712{
713 struct sigcode *scp;
714 char *name = NULL;
715 static char locbuf[32];
716
717 for (scp = siginfo_table; scp -> codename != NULL; scp++)
718 {
719 if ((scp -> signo == sip -> si_signo) &&
720 (scp -> code == sip -> si_code))
721 {
722 name = scp -> codename;
723 break;
724 }
725 }
726 if (name == NULL)
727 {
4ed3a9ea 728 sprintf (locbuf, "sigcode %u", sip -> si_signo);
cc221e76
FF
729 name = locbuf;
730 }
731 return (name);
732}
733
3fbdd536
JG
734static char *
735sigcodedesc (sip)
cc221e76
FF
736 siginfo_t *sip;
737{
738 struct sigcode *scp;
739 char *desc = NULL;
740
741 for (scp = siginfo_table; scp -> codename != NULL; scp++)
742 {
743 if ((scp -> signo == sip -> si_signo) &&
744 (scp -> code == sip -> si_code))
745 {
746 desc = scp -> desc;
747 break;
748 }
749 }
750 if (desc == NULL)
751 {
752 desc = "Unrecognized signal or trap use";
753 }
754 return (desc);
755}
756
757/*
758
759LOCAL FUNCTION
760
761 syscallname - translate a system call number into a system call name
762
763SYNOPSIS
764
765 char *syscallname (int syscallnum)
766
767DESCRIPTION
768
769 Given a system call number, translate it into the printable name
770 of a system call, or into "syscall <num>" if it is an unknown
771 number.
772 */
773
774static char *
775syscallname (syscallnum)
776 int syscallnum;
777{
778 static char locbuf[32];
779 char *rtnval;
780
781 if (syscallnum >= 0 && syscallnum < MAX_SYSCALLS)
782 {
783 rtnval = syscall_table[syscallnum];
784 }
785 else
786 {
4ed3a9ea 787 sprintf (locbuf, "syscall %u", syscallnum);
cc221e76
FF
788 rtnval = locbuf;
789 }
790 return (rtnval);
791}
792
793/*
794
795LOCAL FUNCTION
796
797 init_syscall_table - initialize syscall translation table
798
799SYNOPSIS
800
801 void init_syscall_table (void)
802
803DESCRIPTION
804
805 Dynamically initialize the translation table to convert system
806 call numbers into printable system call names. Done once per
807 gdb run, on initialization.
808
809NOTES
810
811 This is awfully ugly, but preprocessor tricks to make it prettier
812 tend to be nonportable.
813 */
814
815static void
816init_syscall_table ()
817{
cc221e76
FF
818#if defined (SYS_exit)
819 syscall_table[SYS_exit] = "exit";
820#endif
821#if defined (SYS_fork)
822 syscall_table[SYS_fork] = "fork";
823#endif
824#if defined (SYS_read)
825 syscall_table[SYS_read] = "read";
826#endif
827#if defined (SYS_write)
828 syscall_table[SYS_write] = "write";
829#endif
830#if defined (SYS_open)
831 syscall_table[SYS_open] = "open";
832#endif
833#if defined (SYS_close)
834 syscall_table[SYS_close] = "close";
835#endif
836#if defined (SYS_wait)
837 syscall_table[SYS_wait] = "wait";
838#endif
839#if defined (SYS_creat)
840 syscall_table[SYS_creat] = "creat";
841#endif
842#if defined (SYS_link)
843 syscall_table[SYS_link] = "link";
844#endif
845#if defined (SYS_unlink)
846 syscall_table[SYS_unlink] = "unlink";
847#endif
848#if defined (SYS_exec)
849 syscall_table[SYS_exec] = "exec";
850#endif
851#if defined (SYS_execv)
852 syscall_table[SYS_execv] = "execv";
853#endif
854#if defined (SYS_execve)
855 syscall_table[SYS_execve] = "execve";
856#endif
857#if defined (SYS_chdir)
858 syscall_table[SYS_chdir] = "chdir";
859#endif
860#if defined (SYS_time)
861 syscall_table[SYS_time] = "time";
862#endif
863#if defined (SYS_mknod)
864 syscall_table[SYS_mknod] = "mknod";
865#endif
866#if defined (SYS_chmod)
867 syscall_table[SYS_chmod] = "chmod";
868#endif
869#if defined (SYS_chown)
870 syscall_table[SYS_chown] = "chown";
871#endif
872#if defined (SYS_brk)
873 syscall_table[SYS_brk] = "brk";
874#endif
875#if defined (SYS_stat)
876 syscall_table[SYS_stat] = "stat";
877#endif
878#if defined (SYS_lseek)
879 syscall_table[SYS_lseek] = "lseek";
880#endif
881#if defined (SYS_getpid)
882 syscall_table[SYS_getpid] = "getpid";
883#endif
884#if defined (SYS_mount)
885 syscall_table[SYS_mount] = "mount";
886#endif
887#if defined (SYS_umount)
888 syscall_table[SYS_umount] = "umount";
889#endif
890#if defined (SYS_setuid)
891 syscall_table[SYS_setuid] = "setuid";
892#endif
893#if defined (SYS_getuid)
894 syscall_table[SYS_getuid] = "getuid";
895#endif
896#if defined (SYS_stime)
897 syscall_table[SYS_stime] = "stime";
898#endif
899#if defined (SYS_ptrace)
900 syscall_table[SYS_ptrace] = "ptrace";
901#endif
902#if defined (SYS_alarm)
903 syscall_table[SYS_alarm] = "alarm";
904#endif
905#if defined (SYS_fstat)
906 syscall_table[SYS_fstat] = "fstat";
907#endif
908#if defined (SYS_pause)
909 syscall_table[SYS_pause] = "pause";
910#endif
911#if defined (SYS_utime)
912 syscall_table[SYS_utime] = "utime";
913#endif
914#if defined (SYS_stty)
915 syscall_table[SYS_stty] = "stty";
916#endif
917#if defined (SYS_gtty)
918 syscall_table[SYS_gtty] = "gtty";
919#endif
920#if defined (SYS_access)
921 syscall_table[SYS_access] = "access";
922#endif
923#if defined (SYS_nice)
924 syscall_table[SYS_nice] = "nice";
925#endif
926#if defined (SYS_statfs)
927 syscall_table[SYS_statfs] = "statfs";
928#endif
929#if defined (SYS_sync)
930 syscall_table[SYS_sync] = "sync";
931#endif
932#if defined (SYS_kill)
933 syscall_table[SYS_kill] = "kill";
934#endif
935#if defined (SYS_fstatfs)
936 syscall_table[SYS_fstatfs] = "fstatfs";
937#endif
938#if defined (SYS_pgrpsys)
939 syscall_table[SYS_pgrpsys] = "pgrpsys";
940#endif
941#if defined (SYS_xenix)
942 syscall_table[SYS_xenix] = "xenix";
943#endif
944#if defined (SYS_dup)
945 syscall_table[SYS_dup] = "dup";
946#endif
947#if defined (SYS_pipe)
948 syscall_table[SYS_pipe] = "pipe";
949#endif
950#if defined (SYS_times)
951 syscall_table[SYS_times] = "times";
952#endif
953#if defined (SYS_profil)
954 syscall_table[SYS_profil] = "profil";
955#endif
956#if defined (SYS_plock)
957 syscall_table[SYS_plock] = "plock";
958#endif
959#if defined (SYS_setgid)
960 syscall_table[SYS_setgid] = "setgid";
961#endif
962#if defined (SYS_getgid)
963 syscall_table[SYS_getgid] = "getgid";
964#endif
965#if defined (SYS_signal)
966 syscall_table[SYS_signal] = "signal";
967#endif
968#if defined (SYS_msgsys)
969 syscall_table[SYS_msgsys] = "msgsys";
970#endif
971#if defined (SYS_sys3b)
972 syscall_table[SYS_sys3b] = "sys3b";
973#endif
974#if defined (SYS_acct)
975 syscall_table[SYS_acct] = "acct";
976#endif
977#if defined (SYS_shmsys)
978 syscall_table[SYS_shmsys] = "shmsys";
979#endif
980#if defined (SYS_semsys)
981 syscall_table[SYS_semsys] = "semsys";
982#endif
983#if defined (SYS_ioctl)
984 syscall_table[SYS_ioctl] = "ioctl";
985#endif
986#if defined (SYS_uadmin)
987 syscall_table[SYS_uadmin] = "uadmin";
988#endif
989#if defined (SYS_utssys)
990 syscall_table[SYS_utssys] = "utssys";
991#endif
992#if defined (SYS_fsync)
993 syscall_table[SYS_fsync] = "fsync";
994#endif
995#if defined (SYS_umask)
996 syscall_table[SYS_umask] = "umask";
997#endif
998#if defined (SYS_chroot)
999 syscall_table[SYS_chroot] = "chroot";
1000#endif
1001#if defined (SYS_fcntl)
1002 syscall_table[SYS_fcntl] = "fcntl";
1003#endif
1004#if defined (SYS_ulimit)
1005 syscall_table[SYS_ulimit] = "ulimit";
1006#endif
1007#if defined (SYS_rfsys)
1008 syscall_table[SYS_rfsys] = "rfsys";
1009#endif
1010#if defined (SYS_rmdir)
1011 syscall_table[SYS_rmdir] = "rmdir";
1012#endif
1013#if defined (SYS_mkdir)
1014 syscall_table[SYS_mkdir] = "mkdir";
1015#endif
1016#if defined (SYS_getdents)
1017 syscall_table[SYS_getdents] = "getdents";
1018#endif
1019#if defined (SYS_sysfs)
1020 syscall_table[SYS_sysfs] = "sysfs";
1021#endif
1022#if defined (SYS_getmsg)
1023 syscall_table[SYS_getmsg] = "getmsg";
1024#endif
1025#if defined (SYS_putmsg)
1026 syscall_table[SYS_putmsg] = "putmsg";
1027#endif
1028#if defined (SYS_poll)
1029 syscall_table[SYS_poll] = "poll";
1030#endif
1031#if defined (SYS_lstat)
1032 syscall_table[SYS_lstat] = "lstat";
1033#endif
1034#if defined (SYS_symlink)
1035 syscall_table[SYS_symlink] = "symlink";
1036#endif
1037#if defined (SYS_readlink)
1038 syscall_table[SYS_readlink] = "readlink";
1039#endif
1040#if defined (SYS_setgroups)
1041 syscall_table[SYS_setgroups] = "setgroups";
1042#endif
1043#if defined (SYS_getgroups)
1044 syscall_table[SYS_getgroups] = "getgroups";
1045#endif
1046#if defined (SYS_fchmod)
1047 syscall_table[SYS_fchmod] = "fchmod";
1048#endif
1049#if defined (SYS_fchown)
1050 syscall_table[SYS_fchown] = "fchown";
1051#endif
1052#if defined (SYS_sigprocmask)
1053 syscall_table[SYS_sigprocmask] = "sigprocmask";
1054#endif
1055#if defined (SYS_sigsuspend)
1056 syscall_table[SYS_sigsuspend] = "sigsuspend";
1057#endif
1058#if defined (SYS_sigaltstack)
1059 syscall_table[SYS_sigaltstack] = "sigaltstack";
1060#endif
1061#if defined (SYS_sigaction)
1062 syscall_table[SYS_sigaction] = "sigaction";
1063#endif
1064#if defined (SYS_sigpending)
1065 syscall_table[SYS_sigpending] = "sigpending";
1066#endif
1067#if defined (SYS_context)
1068 syscall_table[SYS_context] = "context";
1069#endif
1070#if defined (SYS_evsys)
1071 syscall_table[SYS_evsys] = "evsys";
1072#endif
1073#if defined (SYS_evtrapret)
1074 syscall_table[SYS_evtrapret] = "evtrapret";
1075#endif
1076#if defined (SYS_statvfs)
1077 syscall_table[SYS_statvfs] = "statvfs";
1078#endif
1079#if defined (SYS_fstatvfs)
1080 syscall_table[SYS_fstatvfs] = "fstatvfs";
1081#endif
1082#if defined (SYS_nfssys)
1083 syscall_table[SYS_nfssys] = "nfssys";
1084#endif
1085#if defined (SYS_waitsys)
1086 syscall_table[SYS_waitsys] = "waitsys";
1087#endif
1088#if defined (SYS_sigsendsys)
1089 syscall_table[SYS_sigsendsys] = "sigsendsys";
1090#endif
1091#if defined (SYS_hrtsys)
1092 syscall_table[SYS_hrtsys] = "hrtsys";
1093#endif
1094#if defined (SYS_acancel)
1095 syscall_table[SYS_acancel] = "acancel";
1096#endif
1097#if defined (SYS_async)
1098 syscall_table[SYS_async] = "async";
1099#endif
1100#if defined (SYS_priocntlsys)
1101 syscall_table[SYS_priocntlsys] = "priocntlsys";
1102#endif
1103#if defined (SYS_pathconf)
1104 syscall_table[SYS_pathconf] = "pathconf";
1105#endif
1106#if defined (SYS_mincore)
1107 syscall_table[SYS_mincore] = "mincore";
1108#endif
1109#if defined (SYS_mmap)
1110 syscall_table[SYS_mmap] = "mmap";
1111#endif
1112#if defined (SYS_mprotect)
1113 syscall_table[SYS_mprotect] = "mprotect";
1114#endif
1115#if defined (SYS_munmap)
1116 syscall_table[SYS_munmap] = "munmap";
1117#endif
1118#if defined (SYS_fpathconf)
1119 syscall_table[SYS_fpathconf] = "fpathconf";
1120#endif
1121#if defined (SYS_vfork)
1122 syscall_table[SYS_vfork] = "vfork";
1123#endif
1124#if defined (SYS_fchdir)
1125 syscall_table[SYS_fchdir] = "fchdir";
1126#endif
1127#if defined (SYS_readv)
1128 syscall_table[SYS_readv] = "readv";
1129#endif
1130#if defined (SYS_writev)
1131 syscall_table[SYS_writev] = "writev";
1132#endif
1133#if defined (SYS_xstat)
1134 syscall_table[SYS_xstat] = "xstat";
1135#endif
1136#if defined (SYS_lxstat)
1137 syscall_table[SYS_lxstat] = "lxstat";
1138#endif
1139#if defined (SYS_fxstat)
1140 syscall_table[SYS_fxstat] = "fxstat";
1141#endif
1142#if defined (SYS_xmknod)
1143 syscall_table[SYS_xmknod] = "xmknod";
1144#endif
1145#if defined (SYS_clocal)
1146 syscall_table[SYS_clocal] = "clocal";
1147#endif
1148#if defined (SYS_setrlimit)
1149 syscall_table[SYS_setrlimit] = "setrlimit";
1150#endif
1151#if defined (SYS_getrlimit)
1152 syscall_table[SYS_getrlimit] = "getrlimit";
1153#endif
1154#if defined (SYS_lchown)
1155 syscall_table[SYS_lchown] = "lchown";
1156#endif
1157#if defined (SYS_memcntl)
1158 syscall_table[SYS_memcntl] = "memcntl";
1159#endif
1160#if defined (SYS_getpmsg)
1161 syscall_table[SYS_getpmsg] = "getpmsg";
1162#endif
1163#if defined (SYS_putpmsg)
1164 syscall_table[SYS_putpmsg] = "putpmsg";
1165#endif
1166#if defined (SYS_rename)
1167 syscall_table[SYS_rename] = "rename";
1168#endif
1169#if defined (SYS_uname)
1170 syscall_table[SYS_uname] = "uname";
1171#endif
1172#if defined (SYS_setegid)
1173 syscall_table[SYS_setegid] = "setegid";
1174#endif
1175#if defined (SYS_sysconfig)
1176 syscall_table[SYS_sysconfig] = "sysconfig";
1177#endif
1178#if defined (SYS_adjtime)
1179 syscall_table[SYS_adjtime] = "adjtime";
1180#endif
1181#if defined (SYS_systeminfo)
1182 syscall_table[SYS_systeminfo] = "systeminfo";
1183#endif
1184#if defined (SYS_seteuid)
1185 syscall_table[SYS_seteuid] = "seteuid";
1186#endif
de43d7d0
SG
1187#if defined (SYS_sproc)
1188 syscall_table[SYS_sproc] = "sproc";
1189#endif
cc221e76 1190}
35f5886e
FF
1191
1192/*
1193
3fbdd536 1194LOCAL FUNCTION
35f5886e 1195
3fbdd536 1196 procfs_kill_inferior - kill any currently inferior
35f5886e
FF
1197
1198SYNOPSIS
1199
3fbdd536 1200 void procfs_kill_inferior (void)
35f5886e
FF
1201
1202DESCRIPTION
1203
1204 Kill any current inferior.
1205
1206NOTES
1207
1208 Kills even attached inferiors. Presumably the user has already
1209 been prompted that the inferior is an attached one rather than
1210 one started by gdb. (FIXME?)
1211
1212*/
1213
3fbdd536
JG
1214static void
1215procfs_kill_inferior ()
35f5886e 1216{
de43d7d0 1217 target_mourn_inferior ();
35f5886e
FF
1218}
1219
1220/*
1221
1222LOCAL FUNCTION
1223
1224 unconditionally_kill_inferior - terminate the inferior
1225
1226SYNOPSIS
1227
de43d7d0 1228 static void unconditionally_kill_inferior (struct procinfo *)
35f5886e
FF
1229
1230DESCRIPTION
1231
de43d7d0 1232 Kill the specified inferior.
35f5886e
FF
1233
1234NOTE
1235
1236 A possibly useful enhancement would be to first try sending
1237 the inferior a terminate signal, politely asking it to commit
de43d7d0
SG
1238 suicide, before we murder it (we could call that
1239 politely_kill_inferior()).
35f5886e
FF
1240
1241*/
1242
1243static void
de43d7d0
SG
1244unconditionally_kill_inferior (pi)
1245 struct procinfo *pi;
35f5886e
FF
1246{
1247 int signo;
de43d7d0 1248 int ppid;
35f5886e 1249
de43d7d0
SG
1250 ppid = pi->prstatus.pr_ppid;
1251
35f5886e 1252 signo = SIGKILL;
2592eef8 1253
b6753b3f
PS
1254#ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
1255 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
1256 before the PIOCKILL, otherwise it might generate a corrupted core
1257 file for the inferior. */
1258 ioctl (pi->fd, PIOCSSIG, NULL);
1259#endif
2592eef8 1260#ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
b6753b3f 1261 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
f5de4904
PS
1262 to kill the inferior, otherwise it might remain stopped with a
1263 pending SIGKILL.
2592eef8
PS
1264 We do not check the result of the PIOCSSIG, the inferior might have
1265 died already. */
1266 {
1267 struct siginfo newsiginfo;
1268
1269 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
1270 newsiginfo.si_signo = signo;
1271 newsiginfo.si_code = 0;
1272 newsiginfo.si_errno = 0;
1273 newsiginfo.si_pid = getpid ();
1274 newsiginfo.si_uid = getuid ();
1275 ioctl (pi->fd, PIOCSSIG, &newsiginfo);
1276 }
f5de4904
PS
1277#else
1278 ioctl (pi->fd, PIOCKILL, &signo);
2592eef8
PS
1279#endif
1280
de43d7d0
SG
1281 close_proc_file (pi);
1282
1283/* Only wait() for our direct children. Our grandchildren zombies are killed
1284 by the death of their parents. */
1285
1286 if (ppid == getpid())
1287 wait ((int *) 0);
35f5886e
FF
1288}
1289
1290/*
1291
3fbdd536 1292LOCAL FUNCTION
35f5886e 1293
3fbdd536 1294 procfs_xfer_memory -- copy data to or from inferior memory space
35f5886e
FF
1295
1296SYNOPSIS
1297
3fbdd536 1298 int procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
35f5886e
FF
1299 int dowrite, struct target_ops target)
1300
1301DESCRIPTION
1302
1303 Copy LEN bytes to/from inferior's memory starting at MEMADDR
1304 from/to debugger memory starting at MYADDR. Copy from inferior
1305 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
1306
1307 Returns the length copied, which is either the LEN argument or
3fbdd536 1308 zero. This xfer function does not do partial moves, since procfs_ops
35f5886e
FF
1309 doesn't allow memory operations to cross below us in the target stack
1310 anyway.
1311
1312NOTES
1313
1314 The /proc interface makes this an almost trivial task.
1315 */
1316
3fbdd536
JG
1317static int
1318procfs_xfer_memory (memaddr, myaddr, len, dowrite, target)
1ab3bf1b
JG
1319 CORE_ADDR memaddr;
1320 char *myaddr;
1321 int len;
1322 int dowrite;
1323 struct target_ops *target; /* ignored */
35f5886e
FF
1324{
1325 int nbytes = 0;
de43d7d0 1326 struct procinfo *pi;
35f5886e 1327
de43d7d0
SG
1328 pi = current_procinfo;
1329
1330 if (lseek(pi->fd, (off_t) memaddr, 0) == (off_t) memaddr)
35f5886e
FF
1331 {
1332 if (dowrite)
1333 {
de43d7d0 1334 nbytes = write (pi->fd, myaddr, len);
35f5886e
FF
1335 }
1336 else
1337 {
de43d7d0 1338 nbytes = read (pi->fd, myaddr, len);
35f5886e
FF
1339 }
1340 if (nbytes < 0)
1341 {
1342 nbytes = 0;
1343 }
1344 }
1345 return (nbytes);
1346}
1347
1348/*
1349
3fbdd536 1350LOCAL FUNCTION
35f5886e 1351
3fbdd536 1352 procfs_store_registers -- copy register values back to inferior
35f5886e
FF
1353
1354SYNOPSIS
1355
3fbdd536 1356 void procfs_store_registers (int regno)
35f5886e
FF
1357
1358DESCRIPTION
1359
1360 Store our current register values back into the inferior. If
1361 REGNO is -1 then store all the register, otherwise store just
1362 the value specified by REGNO.
1363
1364NOTES
1365
1366 If we are storing only a single register, we first have to get all
1367 the current values from the process, overwrite the desired register
1368 in the gregset with the one we want from gdb's registers, and then
1369 send the whole set back to the process. For writing all the
1370 registers, all we have to do is generate the gregset and send it to
1371 the process.
1372
1373 Also note that the process has to be stopped on an event of interest
1374 for this to work, which basically means that it has to have been
1375 run under the control of one of the other /proc ioctl calls and not
1376 ptrace. Since we don't use ptrace anyway, we don't worry about this
1377 fine point, but it is worth noting for future reference.
1378
1379 Gdb is confused about what this function is supposed to return.
1380 Some versions return a value, others return nothing. Some are
1381 declared to return a value and actually return nothing. Gdb ignores
1382 anything returned. (FIXME)
1383
1384 */
1385
3fbdd536
JG
1386static void
1387procfs_store_registers (regno)
1ab3bf1b 1388 int regno;
35f5886e 1389{
de43d7d0
SG
1390 struct procinfo *pi;
1391
1392 pi = current_procinfo;
1393
35f5886e
FF
1394 if (regno != -1)
1395 {
de43d7d0 1396 ioctl (pi->fd, PIOCGREG, &pi->gregset);
35f5886e 1397 }
de43d7d0
SG
1398 fill_gregset (&pi->gregset, regno);
1399 ioctl (pi->fd, PIOCSREG, &pi->gregset);
35f5886e
FF
1400
1401#if defined (FP0_REGNUM)
1402
1403 /* Now repeat everything using the floating point register set, if the
1404 target has floating point hardware. Since we ignore the returned value,
1405 we'll never know whether it worked or not anyway. */
1406
1407 if (regno != -1)
1408 {
de43d7d0 1409 ioctl (pi->fd, PIOCGFPREG, &pi->fpregset);
35f5886e 1410 }
de43d7d0
SG
1411 fill_fpregset (&pi->fpregset, regno);
1412 ioctl (pi->fd, PIOCSFPREG, &pi->fpregset);
35f5886e
FF
1413
1414#endif /* FP0_REGNUM */
1415
1416}
1417
1418/*
1419
3fbdd536 1420LOCAL FUNCTION
35f5886e 1421
de43d7d0
SG
1422 create_procinfo - initialize access to a /proc entry
1423
1424SYNOPSIS
1425
eca4a350 1426 struct procinfo * create_procinfo (int pid)
de43d7d0
SG
1427
1428DESCRIPTION
1429
eca4a350
SG
1430 Allocate a procinfo structure, open the /proc file and then set up the
1431 set of signals and faults that are to be traced. Returns a pointer to
1432 the new procinfo structure.
de43d7d0
SG
1433
1434NOTES
1435
1436 If proc_init_failed ever gets called, control returns to the command
1437 processing loop via the standard error handling code.
1438
1439 */
1440
eca4a350 1441static struct procinfo *
de43d7d0
SG
1442create_procinfo (pid)
1443 int pid;
1444{
1445 struct procinfo *pi;
1446
2592eef8
PS
1447 pi = find_procinfo (pid, 1);
1448 if (pi != NULL)
1449 return pi; /* All done! It already exists */
de43d7d0
SG
1450
1451 pi = (struct procinfo *) xmalloc (sizeof (struct procinfo));
1452
1453 if (!open_proc_file (pid, pi, O_RDWR))
1454 proc_init_failed (pi, "can't open process file");
1455
1456 /* Add new process to process info list */
1457
1458 pi->next = procinfo_list;
1459 procinfo_list = pi;
1460
1461 add_fd (pi); /* Add to list for poll/select */
1462
1463 memset ((char *) &pi->prrun, 0, sizeof (pi->prrun));
1464 prfillset (&pi->prrun.pr_trace);
1465 procfs_notice_signals (pid);
1466 prfillset (&pi->prrun.pr_fault);
1467 prdelset (&pi->prrun.pr_fault, FLTPAGE);
1468
b9e58503
PS
1469#ifdef PROCFS_DONT_TRACE_FAULTS
1470 premptyset (&pi->prrun.pr_fault);
2592eef8
PS
1471#endif
1472
de43d7d0
SG
1473 if (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0)
1474 proc_init_failed (pi, "PIOCWSTOP failed");
1475
1476 if (ioctl (pi->fd, PIOCSFAULT, &pi->prrun.pr_fault) < 0)
1477 proc_init_failed (pi, "PIOCSFAULT failed");
eca4a350
SG
1478
1479 return pi;
de43d7d0
SG
1480}
1481
1482/*
1483
1484LOCAL FUNCTION
1485
1486 procfs_init_inferior - initialize target vector and access to a
1487 /proc entry
35f5886e
FF
1488
1489SYNOPSIS
1490
3fbdd536 1491 void procfs_init_inferior (int pid)
35f5886e
FF
1492
1493DESCRIPTION
1494
1495 When gdb starts an inferior, this function is called in the parent
1496 process immediately after the fork. It waits for the child to stop
1497 on the return from the exec system call (the child itself takes care
1498 of ensuring that this is set up), then sets up the set of signals
1499 and faults that are to be traced.
1500
1501NOTES
1502
1503 If proc_init_failed ever gets called, control returns to the command
1504 processing loop via the standard error handling code.
cc221e76 1505
35f5886e
FF
1506 */
1507
3fbdd536
JG
1508static void
1509procfs_init_inferior (pid)
1ab3bf1b 1510 int pid;
35f5886e 1511{
3fbdd536
JG
1512 push_target (&procfs_ops);
1513
de43d7d0
SG
1514 create_procinfo (pid);
1515 add_thread (pid); /* Setup initial thread */
bc28a06c 1516
2592eef8
PS
1517#ifdef START_INFERIOR_TRAPS_EXPECTED
1518 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
1519#else
bc28a06c
JK
1520 /* One trap to exec the shell, one to exec the program being debugged. */
1521 startup_inferior (2);
2592eef8 1522#endif
35f5886e
FF
1523}
1524
1525/*
1526
cc221e76
FF
1527GLOBAL FUNCTION
1528
3950a34e 1529 procfs_notice_signals
cc221e76
FF
1530
1531SYNOPSIS
1532
952a820e 1533 static void procfs_notice_signals (int pid);
cc221e76
FF
1534
1535DESCRIPTION
1536
1537 When the user changes the state of gdb's signal handling via the
1538 "handle" command, this function gets called to see if any change
1539 in the /proc interface is required. It is also called internally
1540 by other /proc interface functions to initialize the state of
1541 the traced signal set.
1542
1543 One thing it does is that signals for which the state is "nostop",
1544 "noprint", and "pass", have their trace bits reset in the pr_trace
1545 field, so that they are no longer traced. This allows them to be
1546 delivered directly to the inferior without the debugger ever being
1547 involved.
1548 */
1549
3950a34e 1550static void
de43d7d0 1551procfs_notice_signals (pid)
952a820e 1552 int pid;
cc221e76
FF
1553{
1554 int signo;
de43d7d0 1555 struct procinfo *pi;
cc221e76 1556
de43d7d0
SG
1557 pi = find_procinfo (pid, 0);
1558
1559 for (signo = 0; signo < NSIG; signo++)
cc221e76 1560 {
67ac9759
JK
1561 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
1562 signal_print_state (target_signal_from_host (signo)) == 0 &&
1563 signal_pass_state (target_signal_from_host (signo)) == 1)
cc221e76 1564 {
de43d7d0 1565 prdelset (&pi->prrun.pr_trace, signo);
cc221e76 1566 }
de43d7d0 1567 else
cc221e76 1568 {
de43d7d0 1569 praddset (&pi->prrun.pr_trace, signo);
cc221e76
FF
1570 }
1571 }
de43d7d0
SG
1572 if (ioctl (pi->fd, PIOCSTRACE, &pi->prrun.pr_trace))
1573 {
1574 print_sys_errmsg ("PIOCSTRACE failed", errno);
1575 }
cc221e76
FF
1576}
1577
1578/*
1579
3fbdd536 1580LOCAL FUNCTION
35f5886e
FF
1581
1582 proc_set_exec_trap -- arrange for exec'd child to halt at startup
1583
1584SYNOPSIS
1585
1586 void proc_set_exec_trap (void)
1587
1588DESCRIPTION
1589
1590 This function is called in the child process when starting up
1591 an inferior, prior to doing the exec of the actual inferior.
1592 It sets the child process's exitset to make exit from the exec
1593 system call an event of interest to stop on, and then simply
1594 returns. The child does the exec, the system call returns, and
1595 the child stops at the first instruction, ready for the gdb
1596 parent process to take control of it.
1597
1598NOTE
1599
1600 We need to use all local variables since the child may be sharing
1601 it's data space with the parent, if vfork was used rather than
1602 fork.
cc221e76
FF
1603
1604 Also note that we want to turn off the inherit-on-fork flag in
1605 the child process so that any grand-children start with all
1606 tracing flags cleared.
35f5886e
FF
1607 */
1608
3fbdd536 1609static void
1ab3bf1b 1610proc_set_exec_trap ()
35f5886e
FF
1611{
1612 sysset_t exitset;
fb63d460 1613 sysset_t entryset;
35f5886e
FF
1614 auto char procname[32];
1615 int fd;
1616
4ed3a9ea 1617 sprintf (procname, PROC_NAME_FMT, getpid ());
35f5886e
FF
1618 if ((fd = open (procname, O_RDWR)) < 0)
1619 {
1620 perror (procname);
199b2450 1621 gdb_flush (gdb_stderr);
35f5886e
FF
1622 _exit (127);
1623 }
1624 premptyset (&exitset);
fb63d460 1625 premptyset (&entryset);
407a8389 1626
2592eef8
PS
1627#ifdef PIOCSSPCACT
1628 /* Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
1629 exits from exec system calls because of the user level loader. */
1630 {
1631 int prfs_flags;
1632
1633 if (ioctl (fd, PIOCGSPCACT, &prfs_flags) < 0)
1634 {
1635 perror (procname);
1636 gdb_flush (gdb_stderr);
1637 _exit (127);
1638 }
1639 prfs_flags |= PRFS_STOPEXEC;
1640 if (ioctl (fd, PIOCSSPCACT, &prfs_flags) < 0)
1641 {
1642 perror (procname);
1643 gdb_flush (gdb_stderr);
1644 _exit (127);
1645 }
1646 }
1647#else
cc221e76
FF
1648 /* GW: Rationale...
1649 Not all systems with /proc have all the exec* syscalls with the same
1650 names. On the SGI, for example, there is no SYS_exec, but there
1651 *is* a SYS_execv. So, we try to account for that. */
1652
407a8389 1653#ifdef SYS_exec
35f5886e 1654 praddset (&exitset, SYS_exec);
407a8389
SG
1655#endif
1656#ifdef SYS_execve
35f5886e 1657 praddset (&exitset, SYS_execve);
407a8389
SG
1658#endif
1659#ifdef SYS_execv
fb63d460 1660 praddset (&exitset, SYS_execv);
407a8389
SG
1661#endif
1662
35f5886e
FF
1663 if (ioctl (fd, PIOCSEXIT, &exitset) < 0)
1664 {
1665 perror (procname);
199b2450 1666 gdb_flush (gdb_stderr);
35f5886e
FF
1667 _exit (127);
1668 }
2592eef8 1669#endif
cc221e76 1670
fb63d460
SG
1671 praddset (&entryset, SYS_exit);
1672
1673 if (ioctl (fd, PIOCSENTRY, &entryset) < 0)
1674 {
1675 perror (procname);
199b2450 1676 gdb_flush (gdb_stderr);
fb63d460
SG
1677 _exit (126);
1678 }
1679
cc221e76
FF
1680 /* Turn off inherit-on-fork flag so that all grand-children of gdb
1681 start with tracing flags cleared. */
1682
5c1c5e67 1683#if defined (PIOCRESET) /* New method */
cc221e76
FF
1684 {
1685 long pr_flags;
1686 pr_flags = PR_FORK;
4ed3a9ea 1687 ioctl (fd, PIOCRESET, &pr_flags);
cc221e76 1688 }
5c1c5e67
FF
1689#else
1690#if defined (PIOCRFORK) /* Original method */
4ed3a9ea 1691 ioctl (fd, PIOCRFORK, NULL);
cc221e76 1692#endif
ec8ceca3
JG
1693#endif
1694
1695 /* Turn on run-on-last-close flag so that this process will not hang
1696 if GDB goes away for some reason. */
1697
1698#if defined (PIOCSET) /* New method */
1699 {
1700 long pr_flags;
1701 pr_flags = PR_RLC;
1702 (void) ioctl (fd, PIOCSET, &pr_flags);
1703 }
1704#else
1705#if defined (PIOCSRLC) /* Original method */
1706 (void) ioctl (fd, PIOCSRLC, 0);
1707#endif
cc221e76 1708#endif
35f5886e
FF
1709}
1710
f8b76e70
FF
1711/*
1712
a39ad5ce
FF
1713GLOBAL FUNCTION
1714
1715 proc_iterate_over_mappings -- call function for every mapped space
1716
1717SYNOPSIS
1718
1719 int proc_iterate_over_mappings (int (*func)())
1720
1721DESCRIPTION
1722
1723 Given a pointer to a function, call that function for every
1724 mapped address space, passing it an open file descriptor for
1725 the file corresponding to that mapped address space (if any)
1726 and the base address of the mapped space. Quit when we hit
1727 the end of the mappings or the function returns nonzero.
1728 */
1729
1730int
1ab3bf1b
JG
1731proc_iterate_over_mappings (func)
1732 int (*func) PARAMS ((int, CORE_ADDR));
a39ad5ce
FF
1733{
1734 int nmap;
1735 int fd;
1736 int funcstat = 0;
1737 struct prmap *prmaps;
1738 struct prmap *prmap;
de43d7d0
SG
1739 struct procinfo *pi;
1740
1741 pi = current_procinfo;
a39ad5ce 1742
de43d7d0 1743 if (ioctl (pi->fd, PIOCNMAP, &nmap) == 0)
a39ad5ce 1744 {
1ab3bf1b 1745 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
de43d7d0 1746 if (ioctl (pi->fd, PIOCMAP, prmaps) == 0)
a39ad5ce
FF
1747 {
1748 for (prmap = prmaps; prmap -> pr_size && funcstat == 0; ++prmap)
1749 {
de43d7d0 1750 fd = proc_address_to_fd (pi, (CORE_ADDR) prmap -> pr_vaddr, 0);
1ab3bf1b 1751 funcstat = (*func) (fd, (CORE_ADDR) prmap -> pr_vaddr);
a39ad5ce
FF
1752 close (fd);
1753 }
1754 }
1755 }
1756 return (funcstat);
1757}
1758
3fbdd536 1759#if 0 /* Currently unused */
a39ad5ce
FF
1760/*
1761
f8b76e70
FF
1762GLOBAL FUNCTION
1763
1764 proc_base_address -- find base address for segment containing address
1765
1766SYNOPSIS
1767
1768 CORE_ADDR proc_base_address (CORE_ADDR addr)
1769
1770DESCRIPTION
1771
1772 Given an address of a location in the inferior, find and return
1773 the base address of the mapped segment containing that address.
1774
1775 This is used for example, by the shared library support code,
1776 where we have the pc value for some location in the shared library
1777 where we are stopped, and need to know the base address of the
1778 segment containing that address.
1779*/
1780
f8b76e70 1781CORE_ADDR
1ab3bf1b 1782proc_base_address (addr)
cc221e76 1783 CORE_ADDR addr;
f8b76e70
FF
1784{
1785 int nmap;
1786 struct prmap *prmaps;
1787 struct prmap *prmap;
1788 CORE_ADDR baseaddr = 0;
de43d7d0 1789 struct procinfo *pi;
f8b76e70 1790
de43d7d0
SG
1791 pi = current_procinfo;
1792
1793 if (ioctl (pi->fd, PIOCNMAP, &nmap) == 0)
f8b76e70 1794 {
1ab3bf1b 1795 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
de43d7d0 1796 if (ioctl (pi->fd, PIOCMAP, prmaps) == 0)
f8b76e70
FF
1797 {
1798 for (prmap = prmaps; prmap -> pr_size; ++prmap)
1799 {
1800 if ((prmap -> pr_vaddr <= (caddr_t) addr) &&
1801 (prmap -> pr_vaddr + prmap -> pr_size > (caddr_t) addr))
1802 {
1803 baseaddr = (CORE_ADDR) prmap -> pr_vaddr;
1804 break;
1805 }
1806 }
1807 }
1808 }
1809 return (baseaddr);
1810}
1811
1ab3bf1b
JG
1812#endif /* 0 */
1813
f8b76e70
FF
1814/*
1815
cc221e76 1816LOCAL FUNCTION
f8b76e70
FF
1817
1818 proc_address_to_fd -- return open fd for file mapped to address
1819
1820SYNOPSIS
1821
de43d7d0 1822 int proc_address_to_fd (struct procinfo *pi, CORE_ADDR addr, complain)
f8b76e70
FF
1823
1824DESCRIPTION
1825
1826 Given an address in the current inferior's address space, use the
1827 /proc interface to find an open file descriptor for the file that
1828 this address was mapped in from. Return -1 if there is no current
1829 inferior. Print a warning message if there is an inferior but
1830 the address corresponds to no file (IE a bogus address).
1831
1832*/
1833
1ab3bf1b 1834static int
de43d7d0
SG
1835proc_address_to_fd (pi, addr, complain)
1836 struct procinfo *pi;
1ab3bf1b
JG
1837 CORE_ADDR addr;
1838 int complain;
f8b76e70
FF
1839{
1840 int fd = -1;
1841
de43d7d0 1842 if ((fd = ioctl (pi->fd, PIOCOPENM, (caddr_t *) &addr)) < 0)
f8b76e70 1843 {
de43d7d0 1844 if (complain)
f8b76e70 1845 {
de43d7d0
SG
1846 print_sys_errmsg (pi->pathname, errno);
1847 warning ("can't find mapped file for address 0x%x", addr);
f8b76e70
FF
1848 }
1849 }
1850 return (fd);
1851}
1852
35f5886e 1853
3fbdd536
JG
1854/* Attach to process PID, then initialize for debugging it
1855 and wait for the trace-trap that results from attaching. */
1856
1857static void
1858procfs_attach (args, from_tty)
1859 char *args;
1860 int from_tty;
1861{
1862 char *exec_file;
1863 int pid;
1864
1865 if (!args)
1866 error_no_arg ("process-id to attach");
1867
1868 pid = atoi (args);
1869
1870 if (pid == getpid()) /* Trying to masturbate? */
1871 error ("I refuse to debug myself!");
1872
1873 if (from_tty)
1874 {
1875 exec_file = (char *) get_exec_file (0);
1876
1877 if (exec_file)
199b2450 1878 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, target_pid_to_str (pid));
3fbdd536 1879 else
199b2450 1880 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid));
3fbdd536 1881
199b2450 1882 gdb_flush (gdb_stdout);
3fbdd536
JG
1883 }
1884
1885 do_attach (pid);
1886 inferior_pid = pid;
1887 push_target (&procfs_ops);
1888}
1889
1890
1891/* Take a program previously attached to and detaches it.
1892 The program resumes execution and will no longer stop
1893 on signals, etc. We'd better not have left any breakpoints
1894 in the program or it'll die when it hits one. For this
1895 to work, it may be necessary for the process to have been
1896 previously attached. It *might* work if the program was
1897 started via the normal ptrace (PTRACE_TRACEME). */
1898
1899static void
1900procfs_detach (args, from_tty)
1901 char *args;
1902 int from_tty;
1903{
1904 int siggnal = 0;
1905
1906 if (from_tty)
1907 {
1908 char *exec_file = get_exec_file (0);
1909 if (exec_file == 0)
1910 exec_file = "";
199b2450 1911 printf_unfiltered ("Detaching from program: %s %s\n",
25286543 1912 exec_file, target_pid_to_str (inferior_pid));
199b2450 1913 gdb_flush (gdb_stdout);
3fbdd536
JG
1914 }
1915 if (args)
1916 siggnal = atoi (args);
1917
1918 do_detach (siggnal);
1919 inferior_pid = 0;
1920 unpush_target (&procfs_ops); /* Pop out of handling an inferior */
1921}
1922
1923/* Get ready to modify the registers array. On machines which store
1924 individual registers, this doesn't need to do anything. On machines
1925 which store all the registers in one fell swoop, this makes sure
1926 that registers contains all the registers from the program being
1927 debugged. */
1928
1929static void
1930procfs_prepare_to_store ()
1931{
1932#ifdef CHILD_PREPARE_TO_STORE
1933 CHILD_PREPARE_TO_STORE ();
1934#endif
1935}
1936
1937/* Print status information about what we're accessing. */
1938
1939static void
1940procfs_files_info (ignore)
1941 struct target_ops *ignore;
1942{
199b2450 1943 printf_unfiltered ("\tUsing the running image of %s %s via /proc.\n",
25286543 1944 attach_flag? "attached": "child", target_pid_to_str (inferior_pid));
3fbdd536
JG
1945}
1946
1947/* ARGSUSED */
1948static void
1949procfs_open (arg, from_tty)
1950 char *arg;
1951 int from_tty;
1952{
1953 error ("Use the \"run\" command to start a Unix child process.");
1954}
35f5886e
FF
1955
1956/*
1957
3fbdd536 1958LOCAL FUNCTION
35f5886e 1959
3fbdd536 1960 do_attach -- attach to an already existing process
35f5886e
FF
1961
1962SYNOPSIS
1963
3fbdd536 1964 int do_attach (int pid)
35f5886e
FF
1965
1966DESCRIPTION
1967
1968 Attach to an already existing process with the specified process
1969 id. If the process is not already stopped, query whether to
1970 stop it or not.
1971
1972NOTES
1973
1974 The option of stopping at attach time is specific to the /proc
1975 versions of gdb. Versions using ptrace force the attachee
ec8ceca3
JG
1976 to stop. (I have changed this version to do so, too. All you
1977 have to do is "continue" to make it go on. -- gnu@cygnus.com)
35f5886e
FF
1978
1979*/
1980
3fbdd536
JG
1981static int
1982do_attach (pid)
1ab3bf1b 1983 int pid;
35f5886e 1984{
ec8ceca3 1985 int result;
de43d7d0
SG
1986 struct procinfo *pi;
1987
1988 pi = (struct procinfo *) xmalloc (sizeof (struct procinfo));
ec8ceca3 1989
de43d7d0 1990 if (!open_proc_file (pid, pi, O_RDWR))
35f5886e 1991 {
de43d7d0
SG
1992 free (pi);
1993 perror_with_name (pi->pathname);
35f5886e
FF
1994 /* NOTREACHED */
1995 }
1996
de43d7d0
SG
1997 /* Add new process to process info list */
1998
1999 pi->next = procinfo_list;
2000 procinfo_list = pi;
2001
2002 add_fd (pi); /* Add to list for poll/select */
2003
35f5886e
FF
2004 /* Get current status of process and if it is not already stopped,
2005 then stop it. Remember whether or not it was stopped when we first
2006 examined it. */
2007
de43d7d0 2008 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
35f5886e 2009 {
de43d7d0
SG
2010 print_sys_errmsg (pi->pathname, errno);
2011 close_proc_file (pi);
35f5886e
FF
2012 error ("PIOCSTATUS failed");
2013 }
de43d7d0 2014 if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
35f5886e 2015 {
de43d7d0 2016 pi->was_stopped = 1;
35f5886e
FF
2017 }
2018 else
2019 {
de43d7d0 2020 pi->was_stopped = 0;
ec8ceca3 2021 if (1 || query ("Process is currently running, stop it? "))
35f5886e 2022 {
ec8ceca3
JG
2023 /* Make it run again when we close it. */
2024#if defined (PIOCSET) /* New method */
2025 {
2026 long pr_flags;
2027 pr_flags = PR_RLC;
de43d7d0 2028 result = ioctl (pi->fd, PIOCSET, &pr_flags);
ec8ceca3
JG
2029 }
2030#else
2031#if defined (PIOCSRLC) /* Original method */
de43d7d0 2032 result = ioctl (pi->fd, PIOCSRLC, 0);
ec8ceca3
JG
2033#endif
2034#endif
2035 if (result < 0)
2036 {
de43d7d0
SG
2037 print_sys_errmsg (pi->pathname, errno);
2038 close_proc_file (pi);
ec8ceca3
JG
2039 error ("PIOCSRLC or PIOCSET failed");
2040 }
de43d7d0 2041 if (ioctl (pi->fd, PIOCSTOP, &pi->prstatus) < 0)
35f5886e 2042 {
de43d7d0
SG
2043 print_sys_errmsg (pi->pathname, errno);
2044 close_proc_file (pi);
35f5886e
FF
2045 error ("PIOCSTOP failed");
2046 }
de43d7d0 2047 pi->nopass_next_sigstop = 1;
d65eee73
FF
2048 }
2049 else
2050 {
199b2450 2051 printf_unfiltered ("Ok, gdb will wait for %s to stop.\n", target_pid_to_str (pid));
35f5886e
FF
2052 }
2053 }
ec8ceca3 2054
35f5886e
FF
2055 /* Remember some things about the inferior that we will, or might, change
2056 so that we can restore them when we detach. */
2057
de43d7d0
SG
2058 ioctl (pi->fd, PIOCGTRACE, &pi->saved_trace);
2059 ioctl (pi->fd, PIOCGHOLD, &pi->saved_sighold);
2060 ioctl (pi->fd, PIOCGFAULT, &pi->saved_fltset);
2061 ioctl (pi->fd, PIOCGENTRY, &pi->saved_entryset);
2062 ioctl (pi->fd, PIOCGEXIT, &pi->saved_exitset);
35f5886e
FF
2063
2064 /* Set up trace and fault sets, as gdb expects them. */
2065
de43d7d0
SG
2066 memset (&pi->prrun, 0, sizeof (pi->prrun));
2067 prfillset (&pi->prrun.pr_trace);
2068 procfs_notice_signals (pid);
2069 prfillset (&pi->prrun.pr_fault);
2070 prdelset (&pi->prrun.pr_fault, FLTPAGE);
2592eef8 2071
b9e58503
PS
2072#ifdef PROCFS_DONT_TRACE_FAULTS
2073 premptyset (&pi->prrun.pr_fault);
2592eef8
PS
2074#endif
2075
de43d7d0 2076 if (ioctl (pi->fd, PIOCSFAULT, &pi->prrun.pr_fault))
35f5886e 2077 {
f66f459f 2078 print_sys_errmsg ("PIOCSFAULT failed", errno);
35f5886e 2079 }
de43d7d0 2080 if (ioctl (pi->fd, PIOCSTRACE, &pi->prrun.pr_trace))
35f5886e 2081 {
f66f459f 2082 print_sys_errmsg ("PIOCSTRACE failed", errno);
35f5886e
FF
2083 }
2084 attach_flag = 1;
2085 return (pid);
2086}
2087
2088/*
2089
3fbdd536 2090LOCAL FUNCTION
35f5886e 2091
3fbdd536 2092 do_detach -- detach from an attached-to process
35f5886e
FF
2093
2094SYNOPSIS
2095
3fbdd536 2096 void do_detach (int signal)
35f5886e
FF
2097
2098DESCRIPTION
2099
2100 Detach from the current attachee.
2101
2102 If signal is non-zero, the attachee is started running again and sent
2103 the specified signal.
2104
2105 If signal is zero and the attachee was not already stopped when we
2106 attached to it, then we make it runnable again when we detach.
2107
2108 Otherwise, we query whether or not to make the attachee runnable
2109 again, since we may simply want to leave it in the state it was in
2110 when we attached.
2111
2112 We report any problems, but do not consider them errors, since we
2113 MUST detach even if some things don't seem to go right. This may not
2114 be the ideal situation. (FIXME).
2115 */
2116
3fbdd536
JG
2117static void
2118do_detach (signal)
1ab3bf1b 2119 int signal;
35f5886e 2120{
ec8ceca3 2121 int result;
de43d7d0
SG
2122 struct procinfo *pi;
2123
2124 pi = current_procinfo;
ec8ceca3 2125
35f5886e
FF
2126 if (signal)
2127 {
de43d7d0 2128 set_proc_siginfo (pi, signal);
35f5886e 2129 }
de43d7d0 2130 if (ioctl (pi->fd, PIOCSEXIT, &pi->saved_exitset) < 0)
35f5886e 2131 {
de43d7d0 2132 print_sys_errmsg (pi->pathname, errno);
199b2450 2133 printf_unfiltered ("PIOCSEXIT failed.\n");
35f5886e 2134 }
de43d7d0 2135 if (ioctl (pi->fd, PIOCSENTRY, &pi->saved_entryset) < 0)
35f5886e 2136 {
de43d7d0 2137 print_sys_errmsg (pi->pathname, errno);
199b2450 2138 printf_unfiltered ("PIOCSENTRY failed.\n");
35f5886e 2139 }
de43d7d0 2140 if (ioctl (pi->fd, PIOCSTRACE, &pi->saved_trace) < 0)
35f5886e 2141 {
de43d7d0 2142 print_sys_errmsg (pi->pathname, errno);
199b2450 2143 printf_unfiltered ("PIOCSTRACE failed.\n");
35f5886e 2144 }
de43d7d0 2145 if (ioctl (pi->fd, PIOCSHOLD, &pi->saved_sighold) < 0)
cc221e76 2146 {
de43d7d0 2147 print_sys_errmsg (pi->pathname, errno);
199b2450 2148 printf_unfiltered ("PIOSCHOLD failed.\n");
cc221e76 2149 }
de43d7d0 2150 if (ioctl (pi->fd, PIOCSFAULT, &pi->saved_fltset) < 0)
35f5886e 2151 {
de43d7d0 2152 print_sys_errmsg (pi->pathname, errno);
199b2450 2153 printf_unfiltered ("PIOCSFAULT failed.\n");
35f5886e 2154 }
de43d7d0 2155 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
35f5886e 2156 {
de43d7d0 2157 print_sys_errmsg (pi->pathname, errno);
199b2450 2158 printf_unfiltered ("PIOCSTATUS failed.\n");
35f5886e
FF
2159 }
2160 else
2161 {
de43d7d0 2162 if (signal || (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
35f5886e 2163 {
de43d7d0 2164 if (signal || !pi->was_stopped ||
35f5886e
FF
2165 query ("Was stopped when attached, make it runnable again? "))
2166 {
2592eef8
PS
2167 /* Clear any pending signal if we want to detach without
2168 a signal. */
2169 if (signal == 0)
2170 set_proc_siginfo (pi, signal);
2171
ec8ceca3 2172 /* Clear any fault that might have stopped it. */
de43d7d0 2173 if (ioctl (pi->fd, PIOCCFAULT, 0))
eca4a350
SG
2174 {
2175 print_sys_errmsg (pi->pathname, errno);
199b2450 2176 printf_unfiltered ("PIOCCFAULT failed.\n");
eca4a350 2177 }
ec8ceca3
JG
2178
2179 /* Make it run again when we close it. */
eca4a350 2180#if defined (PIOCSET) /* New method */
ec8ceca3 2181 {
eca4a350
SG
2182 long pr_flags;
2183 pr_flags = PR_RLC;
2184 result = ioctl (pi->fd, PIOCSET, &pr_flags);
ec8ceca3
JG
2185 }
2186#else
eca4a350 2187#if defined (PIOCSRLC) /* Original method */
de43d7d0 2188 result = ioctl (pi->fd, PIOCSRLC, 0);
ec8ceca3
JG
2189#endif
2190#endif
2191 if (result)
35f5886e 2192 {
de43d7d0 2193 print_sys_errmsg (pi->pathname, errno);
199b2450 2194 printf_unfiltered ("PIOCSRLC or PIOCSET failed.\n");
35f5886e
FF
2195 }
2196 }
2197 }
2198 }
de43d7d0 2199 close_proc_file (pi);
35f5886e
FF
2200 attach_flag = 0;
2201}
2202
45dc9be3
JK
2203/* emulate wait() as much as possible.
2204 Wait for child to do something. Return pid of child, or -1 in case
2205 of error; store status in *OURSTATUS.
2206
2207 Not sure why we can't
2208 just use wait(), but it seems to have problems when applied to a
2209 process being controlled with the /proc interface.
2210
2211 We have a race problem here with no obvious solution. We need to let
2212 the inferior run until it stops on an event of interest, which means
2213 that we need to use the PIOCWSTOP ioctl. However, we cannot use this
2214 ioctl if the process is already stopped on something that is not an
2215 event of interest, or the call will hang indefinitely. Thus we first
2216 use PIOCSTATUS to see if the process is not stopped. If not, then we
2217 use PIOCWSTOP. But during the window between the two, if the process
2218 stops for any reason that is not an event of interest (such as a job
2219 control signal) then gdb will hang. One possible workaround is to set
2220 an alarm to wake up every minute of so and check to see if the process
2221 is still running, and if so, then reissue the PIOCWSTOP. But this is
2222 a real kludge, so has not been implemented. FIXME: investigate
2223 alternatives.
2224
2225 FIXME: Investigate why wait() seems to have problems with programs
2226 being control by /proc routines. */
35f5886e 2227
3fbdd536 2228static int
45dc9be3 2229procfs_wait (pid, ourstatus)
de43d7d0 2230 int pid;
67ac9759 2231 struct target_waitstatus *ourstatus;
35f5886e
FF
2232{
2233 short what;
2234 short why;
2235 int statval = 0;
2236 int checkerr = 0;
2237 int rtnval = -1;
de43d7d0
SG
2238 struct procinfo *pi;
2239
2240 if (pid != -1) /* Non-specific process? */
2241 pi = NULL;
2242 else
2243 for (pi = procinfo_list; pi; pi = pi->next)
2244 if (pi->had_event)
2245 break;
2246
de43d7d0 2247 if (!pi)
eca4a350
SG
2248 {
2249 wait_again:
2250
2251 pi = wait_fd ();
2252 }
de43d7d0
SG
2253
2254 if (pid != -1)
2255 for (pi = procinfo_list; pi; pi = pi->next)
2256 if (pi->pid == pid && pi->had_event)
2257 break;
2258
2259 if (!pi && !checkerr)
2260 goto wait_again;
2261
2262 if (!checkerr && !(pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
2263 {
2264 if (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0)
2265 {
2266 checkerr++;
2267 }
35f5886e
FF
2268 }
2269 if (checkerr)
2270 {
2271 if (errno == ENOENT)
2272 {
2273 rtnval = wait (&statval);
2274 if (rtnval != inferior_pid)
2275 {
de43d7d0 2276 print_sys_errmsg (pi->pathname, errno);
35f5886e
FF
2277 error ("PIOCWSTOP, wait failed, returned %d", rtnval);
2278 /* NOTREACHED */
2279 }
2280 }
2281 else
2282 {
de43d7d0 2283 print_sys_errmsg (pi->pathname, errno);
35f5886e
FF
2284 error ("PIOCSTATUS or PIOCWSTOP failed.");
2285 /* NOTREACHED */
2286 }
2287 }
de43d7d0 2288 else if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
35f5886e 2289 {
de43d7d0
SG
2290 rtnval = pi->prstatus.pr_pid;
2291 why = pi->prstatus.pr_why;
2292 what = pi->prstatus.pr_what;
2293
2294 switch (why)
35f5886e 2295 {
de43d7d0 2296 case PR_SIGNALLED:
35f5886e 2297 statval = (what << 8) | 0177;
fb63d460
SG
2298 break;
2299 case PR_SYSENTRY:
2300 if (what != SYS_exit)
2301 error ("PR_SYSENTRY, unknown system call %d", what);
2302
2303 pi->prrun.pr_flags = PRCFAULT;
2304
2305 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
2306 perror_with_name (pi->pathname);
2307
2308 rtnval = wait (&statval);
2309
de43d7d0
SG
2310 break;
2311 case PR_SYSEXIT:
2312 switch (what)
2313 {
407a8389 2314#ifdef SYS_exec
de43d7d0 2315 case SYS_exec:
407a8389
SG
2316#endif
2317#ifdef SYS_execve
de43d7d0 2318 case SYS_execve:
407a8389
SG
2319#endif
2320#ifdef SYS_execv
de43d7d0 2321 case SYS_execv:
407a8389 2322#endif
de43d7d0
SG
2323 statval = (SIGTRAP << 8) | 0177;
2324 break;
2325#ifdef SYS_sproc
2326 case SYS_sproc:
2327/* We've just detected the completion of an sproc system call. Now we need to
2328 setup a procinfo struct for this thread, and notify the thread system of the
2329 new arrival. */
2330
2331/* If sproc failed, then nothing interesting happened. Continue the process and
2332 go back to sleep. */
2333
2334 if (pi->prstatus.pr_errno != 0)
2335 {
2336 pi->prrun.pr_flags &= PRSTEP;
2337 pi->prrun.pr_flags |= PRCFAULT;
2338
2339 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
2340 perror_with_name (pi->pathname);
2341
2342 goto wait_again;
2343 }
2344
2345/* At this point, the new thread is stopped at it's first instruction, and
2346 the parent is stopped at the exit from sproc. */
2347
2348/* Notify the caller of the arrival of a new thread. */
2349 create_procinfo (pi->prstatus.pr_rval1);
2350
2351 rtnval = pi->prstatus.pr_rval1;
2352 statval = (SIGTRAP << 8) | 0177;
2353
2354 break;
eca4a350
SG
2355 case SYS_fork:
2356#ifdef SYS_vfork
2357 case SYS_vfork:
2358#endif
2359/* At this point, we've detected the completion of a fork (or vfork) call in
2360 our child. The grandchild is also stopped because we set inherit-on-fork
2361 earlier. (Note that nobody has the grandchilds' /proc file open at this
2362 point.) We will release the grandchild from the debugger by opening it's
2363 /proc file and then closing it. Since run-on-last-close is set, the
2364 grandchild continues on its' merry way. */
2365
2366 {
2367 struct procinfo *pitemp;
2368
2369 pitemp = create_procinfo (pi->prstatus.pr_rval1);
2370 if (pitemp)
2371 close_proc_file (pitemp);
2372
2373 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
2374 perror_with_name (pi->pathname);
2375 }
2376 goto wait_again;
de43d7d0
SG
2377#endif /* SYS_sproc */
2378
2379 default:
2380 error ("PIOCSTATUS (PR_SYSEXIT): Unknown system call %d", what);
2381 }
2382 break;
2383 case PR_REQUESTED:
35f5886e 2384 statval = (SIGSTOP << 8) | 0177;
de43d7d0
SG
2385 break;
2386 case PR_JOBCONTROL:
35f5886e 2387 statval = (what << 8) | 0177;
de43d7d0
SG
2388 break;
2389 case PR_FAULTED:
35f5886e
FF
2390 switch (what)
2391 {
e6b8a171 2392#ifdef FLTWATCH
999dd04b 2393 case FLTWATCH:
e6b8a171
JL
2394 statval = (SIGTRAP << 8) | 0177;
2395 break;
2396#endif
2397#ifdef FLTKWATCH
999dd04b 2398 case FLTKWATCH:
35f5886e
FF
2399 statval = (SIGTRAP << 8) | 0177;
2400 break;
e6b8a171 2401#endif
3f5e2fb5
JK
2402#ifndef FAULTED_USE_SIGINFO
2403 /* Irix, contrary to the documentation, fills in 0 for si_signo.
2404 Solaris fills in si_signo. I'm not sure about others. */
2405 case FLTPRIV:
2406 case FLTILL:
2407 statval = (SIGILL << 8) | 0177;
2408 break;
2409 case FLTBPT:
2410 case FLTTRACE:
2411 statval = (SIGTRAP << 8) | 0177;
2412 break;
2413 case FLTSTACK:
2414 case FLTACCESS:
2415 case FLTBOUNDS:
2416 statval = (SIGSEGV << 8) | 0177;
2417 break;
2418 case FLTIOVF:
2419 case FLTIZDIV:
2420 case FLTFPE:
2421 statval = (SIGFPE << 8) | 0177;
2422 break;
2423 case FLTPAGE: /* Recoverable page fault */
2424#endif /* not FAULTED_USE_SIGINFO */
35f5886e 2425 default:
890634ed
JK
2426 /* Use the signal which the kernel assigns. This is better than
2427 trying to second-guess it from the fault. In fact, I suspect
2428 that FLTACCESS can be either SIGSEGV or SIGBUS. */
2429 statval = ((pi->prstatus.pr_info.si_signo) << 8) | 0177;
2430 break;
35f5886e 2431 }
de43d7d0
SG
2432 break;
2433 default:
35f5886e 2434 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
35f5886e 2435 }
de43d7d0
SG
2436/* Stop all the other threads when any of them stops. */
2437
2438 {
2439 struct procinfo *procinfo;
2440
2441 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
2442 {
2443 if (!procinfo->had_event)
2444 if (ioctl (procinfo->fd, PIOCSTOP, &procinfo->prstatus) < 0)
2445 {
2446 print_sys_errmsg (procinfo->pathname, errno);
2447 error ("PIOCSTOP failed");
2448 }
2449 }
2450 }
35f5886e
FF
2451 }
2452 else
2453 {
2454 error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x",
de43d7d0 2455 pi->prstatus.pr_flags);
35f5886e 2456 }
3fbdd536 2457
67ac9759 2458 store_waitstatus (ourstatus, statval);
3fbdd536
JG
2459
2460 if (rtnval == -1) /* No more children to wait for */
2461 {
199b2450 2462 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing.\n");
67ac9759
JK
2463 /* Claim it exited with unknown signal. */
2464 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2465 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
3fbdd536
JG
2466 return rtnval;
2467 }
2468
de43d7d0 2469 pi->had_event = 0; /* Indicate that we've seen this one */
35f5886e
FF
2470 return (rtnval);
2471}
2472
2473/*
2474
6b801388
FF
2475LOCAL FUNCTION
2476
2477 set_proc_siginfo - set a process's current signal info
2478
2479SYNOPSIS
2480
2481 void set_proc_siginfo (struct procinfo *pip, int signo);
2482
2483DESCRIPTION
2484
2485 Given a pointer to a process info struct in PIP and a signal number
2486 in SIGNO, set the process's current signal and its associated signal
2487 information. The signal will be delivered to the process immediately
2488 after execution is resumed, even if it is being held. In addition,
2489 this particular delivery will not cause another PR_SIGNALLED stop
2490 even if the signal is being traced.
2491
2492 If we are not delivering the same signal that the prstatus siginfo
2493 struct contains information about, then synthesize a siginfo struct
2494 to match the signal we are doing to deliver, make it of the type
2495 "generated by a user process", and send this synthesized copy. When
2496 used to set the inferior's signal state, this will be required if we
2497 are not currently stopped because of a traced signal, or if we decide
2498 to continue with a different signal.
2499
2500 Note that when continuing the inferior from a stop due to receipt
2501 of a traced signal, we either have set PRCSIG to clear the existing
2502 signal, or we have to call this function to do a PIOCSSIG with either
2503 the existing siginfo struct from pr_info, or one we have synthesized
2504 appropriately for the signal we want to deliver. Otherwise if the
2505 signal is still being traced, the inferior will immediately stop
2506 again.
2507
2508 See siginfo(5) for more details.
2509*/
2510
2511static void
2512set_proc_siginfo (pip, signo)
cc221e76
FF
2513 struct procinfo *pip;
2514 int signo;
6b801388
FF
2515{
2516 struct siginfo newsiginfo;
2517 struct siginfo *sip;
2518
2592eef8
PS
2519#ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2520 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
a1a0d974 2521 receives a PIOCSSIG with a signal identical to the current signal,
2592eef8
PS
2522 it messes up the current signal. Work around the kernel bug. */
2523 if (signo == pip -> prstatus.pr_cursig)
2524 return;
2525#endif
2526
de43d7d0 2527 if (signo == pip -> prstatus.pr_info.si_signo)
6b801388 2528 {
de43d7d0
SG
2529 sip = &pip -> prstatus.pr_info;
2530 }
2531 else
2532 {
2533 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
2534 sip = &newsiginfo;
2535 sip -> si_signo = signo;
2536 sip -> si_code = 0;
2537 sip -> si_errno = 0;
2538 sip -> si_pid = getpid ();
2539 sip -> si_uid = getuid ();
2540 }
2541 if (ioctl (pip -> fd, PIOCSSIG, sip) < 0)
2542 {
2543 print_sys_errmsg (pip -> pathname, errno);
2544 warning ("PIOCSSIG failed");
6b801388
FF
2545 }
2546}
2547
25286543 2548/* Resume execution of process PID. If STEP is nozero, then
59ba57da
JK
2549 just single step it. If SIGNAL is nonzero, restart it with that
2550 signal activated. */
35f5886e 2551
3fbdd536 2552static void
25286543
SG
2553procfs_resume (pid, step, signo)
2554 int pid;
1ab3bf1b 2555 int step;
67ac9759 2556 enum target_signal signo;
35f5886e 2557{
59ba57da 2558 int signal_to_pass;
de43d7d0
SG
2559 struct procinfo *pi, *procinfo;
2560
2561 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
59ba57da 2562
35f5886e 2563 errno = 0;
de43d7d0 2564 pi->prrun.pr_flags = PRSTRACE | PRSFAULT | PRCFAULT;
99fd9e3e 2565
59ba57da
JK
2566#if 0
2567 /* It should not be necessary. If the user explicitly changes the value,
2568 value_assign calls write_register_bytes, which writes it. */
2569/* It may not be absolutely necessary to specify the PC value for
2570 restarting, but to be safe we use the value that gdb considers
2571 to be current. One case where this might be necessary is if the
2572 user explicitly changes the PC value that gdb considers to be
2573 current. FIXME: Investigate if this is necessary or not. */
2574
c3192172 2575#ifdef PRSVADDR_BROKEN
99fd9e3e
SG
2576/* Can't do this under Solaris running on a Sparc, as there seems to be no
2577 place to put nPC. In fact, if you use this, nPC seems to be set to some
2578 random garbage. We have to rely on the fact that PC and nPC have been
2579 written previously via PIOCSREG during a register flush. */
2580
de43d7d0
SG
2581 pi->prrun.pr_vaddr = (caddr_t) *(int *) &registers[REGISTER_BYTE (PC_REGNUM)];
2582 pi->prrun.pr_flags != PRSVADDR;
99fd9e3e 2583#endif
59ba57da
JK
2584#endif
2585
67ac9759 2586 if (signo == TARGET_SIGNAL_STOP && pi->nopass_next_sigstop)
59ba57da
JK
2587 /* When attaching to a child process, if we forced it to stop with
2588 a PIOCSTOP, then we will have set the nopass_next_sigstop flag.
2589 Upon resuming the first time after such a stop, we explicitly
2590 inhibit sending it another SIGSTOP, which would be the normal
2591 result of default signal handling. One potential drawback to
2592 this is that we will also ignore any attempt to by the user
2593 to explicitly continue after the attach with a SIGSTOP. Ultimately
2594 this problem should be dealt with by making the routines that
2595 deal with the inferior a little smarter, and possibly even allow
2596 an inferior to continue running at the same time as gdb. (FIXME?) */
2597 signal_to_pass = 0;
67ac9759 2598 else if (signo == TARGET_SIGNAL_TSTP
de43d7d0
SG
2599 && pi->prstatus.pr_cursig == SIGTSTP
2600 && pi->prstatus.pr_action.sa_handler == SIG_DFL)
59ba57da
JK
2601
2602 /* We are about to pass the inferior a SIGTSTP whose action is
2603 SIG_DFL. The SIG_DFL action for a SIGTSTP is to stop
2604 (notifying the parent via wait()), and then keep going from the
2605 same place when the parent is ready for you to keep going. So
2606 under the debugger, it should do nothing (as if the program had
2607 been stopped and then later resumed. Under ptrace, this
2608 happens for us, but under /proc, the system obligingly stops
2609 the process, and wait_for_inferior would have no way of
2610 distinguishing that type of stop (which indicates that we
2611 should just start it again), with a stop due to the pr_trace
2612 field of the prrun_t struct.
2613
2614 Note that if the SIGTSTP is being caught, we *do* need to pass it,
2615 because the handler needs to get executed. */
2616 signal_to_pass = 0;
2617 else
67ac9759 2618 signal_to_pass = target_signal_to_host (signo);
99fd9e3e 2619
59ba57da 2620 if (signal_to_pass)
35f5886e 2621 {
de43d7d0 2622 set_proc_siginfo (pi, signal_to_pass);
35f5886e
FF
2623 }
2624 else
2625 {
de43d7d0 2626 pi->prrun.pr_flags |= PRCSIG;
35f5886e 2627 }
de43d7d0 2628 pi->nopass_next_sigstop = 0;
35f5886e
FF
2629 if (step)
2630 {
de43d7d0 2631 pi->prrun.pr_flags |= PRSTEP;
35f5886e 2632 }
de43d7d0 2633 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
35f5886e 2634 {
de43d7d0 2635 perror_with_name (pi->pathname);
35f5886e
FF
2636 /* NOTREACHED */
2637 }
de43d7d0
SG
2638
2639 pi->had_event = 0;
2640
2641 /* Continue all the other threads that haven't had an event of
2642 interest. */
2643
2644 if (pid == -1)
2645 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
2646 {
2647 if (pi != procinfo && !procinfo->had_event)
2648 {
2649 procinfo->prrun.pr_flags &= PRSTEP;
2650 procinfo->prrun.pr_flags |= PRCFAULT | PRCSIG;
2651 ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus);
2652 if (ioctl (procinfo->fd, PIOCRUN, &procinfo->prrun) < 0)
2653 {
2654 if (ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus) < 0)
2655 {
199b2450 2656 fprintf_unfiltered(gdb_stderr, "PIOCSTATUS failed, errno=%d\n", errno);
de43d7d0
SG
2657 }
2658 print_sys_errmsg (procinfo->pathname, errno);
2659 error ("PIOCRUN failed");
2660 }
2661 ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus);
2662 }
2663 }
35f5886e
FF
2664}
2665
2666/*
2667
3fbdd536 2668LOCAL FUNCTION
35f5886e 2669
3fbdd536 2670 procfs_fetch_registers -- fetch current registers from inferior
35f5886e
FF
2671
2672SYNOPSIS
2673
3fbdd536 2674 void procfs_fetch_registers (int regno)
35f5886e
FF
2675
2676DESCRIPTION
2677
2678 Read the current values of the inferior's registers, both the
2679 general register set and floating point registers (if supported)
2680 and update gdb's idea of their current values.
2681
2682*/
2683
3fbdd536
JG
2684static void
2685procfs_fetch_registers (regno)
1ab3bf1b 2686 int regno;
35f5886e 2687{
de43d7d0
SG
2688 struct procinfo *pi;
2689
2690 pi = current_procinfo;
2691
2692 if (ioctl (pi->fd, PIOCGREG, &pi->gregset) != -1)
35f5886e 2693 {
de43d7d0 2694 supply_gregset (&pi->gregset);
35f5886e
FF
2695 }
2696#if defined (FP0_REGNUM)
de43d7d0 2697 if (ioctl (pi->fd, PIOCGFPREG, &pi->fpregset) != -1)
35f5886e 2698 {
de43d7d0 2699 supply_fpregset (&pi->fpregset);
35f5886e
FF
2700 }
2701#endif
2702}
2703
fb182850
FF
2704/*
2705
35f5886e
FF
2706LOCAL FUNCTION
2707
de43d7d0
SG
2708 proc_init_failed - called whenever /proc access initialization
2709fails
35f5886e
FF
2710
2711SYNOPSIS
2712
de43d7d0 2713 static void proc_init_failed (struct procinfo *pi, char *why)
35f5886e
FF
2714
2715DESCRIPTION
2716
2717 This function is called whenever initialization of access to a /proc
2718 entry fails. It prints a suitable error message, does some cleanup,
2719 and then invokes the standard error processing routine which dumps
2720 us back into the command loop.
2721 */
2722
2723static void
de43d7d0
SG
2724proc_init_failed (pi, why)
2725 struct procinfo *pi;
1ab3bf1b 2726 char *why;
35f5886e 2727{
de43d7d0
SG
2728 print_sys_errmsg (pi->pathname, errno);
2729 kill (pi->pid, SIGKILL);
2730 close_proc_file (pi);
35f5886e
FF
2731 error (why);
2732 /* NOTREACHED */
2733}
2734
2735/*
2736
2737LOCAL FUNCTION
2738
2739 close_proc_file - close any currently open /proc entry
2740
2741SYNOPSIS
2742
a39ad5ce 2743 static void close_proc_file (struct procinfo *pip)
35f5886e
FF
2744
2745DESCRIPTION
2746
2747 Close any currently open /proc entry and mark the process information
2748 entry as invalid. In order to ensure that we don't try to reuse any
2749 stale information, the pid, fd, and pathnames are explicitly
2750 invalidated, which may be overkill.
2751
2752 */
2753
2754static void
1ab3bf1b
JG
2755close_proc_file (pip)
2756 struct procinfo *pip;
35f5886e 2757{
de43d7d0
SG
2758 struct procinfo *procinfo;
2759
2760 remove_fd (pip); /* Remove fd from poll/select list */
2761
2762 close (pip -> fd);
2763
2764 free (pip -> pathname);
2765
2766 /* Unlink pip from the procinfo chain. Note pip might not be on the list. */
2767
2768 if (procinfo_list == pip)
2769 procinfo_list = pip->next;
2770 else
2771 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
2772 if (procinfo->next == pip)
2773 procinfo->next = pip->next;
2774
2775 free (pip);
35f5886e
FF
2776}
2777
2778/*
2779
2780LOCAL FUNCTION
2781
2782 open_proc_file - open a /proc entry for a given process id
2783
2784SYNOPSIS
2785
ec8ceca3 2786 static int open_proc_file (int pid, struct procinfo *pip, int mode)
35f5886e
FF
2787
2788DESCRIPTION
2789
ec8ceca3
JG
2790 Given a process id and a mode, close the existing open /proc
2791 entry (if any) and open one for the new process id, in the
2792 specified mode. Once it is open, then mark the local process
2793 information structure as valid, which guarantees that the pid,
2794 fd, and pathname fields match an open /proc entry. Returns
2795 zero if the open fails, nonzero otherwise.
35f5886e
FF
2796
2797 Note that the pathname is left intact, even when the open fails,
2798 so that callers can use it to construct meaningful error messages
2799 rather than just "file open failed".
2800 */
2801
2802static int
ec8ceca3 2803open_proc_file (pid, pip, mode)
1ab3bf1b
JG
2804 int pid;
2805 struct procinfo *pip;
ec8ceca3 2806 int mode;
35f5886e 2807{
de43d7d0
SG
2808 pip -> next = NULL;
2809 pip -> had_event = 0;
2810 pip -> pathname = xmalloc (32);
2811 pip -> pid = pid;
2812
a39ad5ce 2813 sprintf (pip -> pathname, PROC_NAME_FMT, pid);
de43d7d0
SG
2814 if ((pip -> fd = open (pip -> pathname, mode)) < 0)
2815 return 0;
2816
2817 return 1;
a39ad5ce
FF
2818}
2819
f66f459f 2820static char *
1ab3bf1b
JG
2821mappingflags (flags)
2822 long flags;
a39ad5ce 2823{
5c1c5e67 2824 static char asciiflags[8];
a39ad5ce 2825
5c1c5e67
FF
2826 strcpy (asciiflags, "-------");
2827#if defined (MA_PHYS)
2828 if (flags & MA_PHYS) asciiflags[0] = 'd';
2829#endif
2830 if (flags & MA_STACK) asciiflags[1] = 's';
2831 if (flags & MA_BREAK) asciiflags[2] = 'b';
2832 if (flags & MA_SHARED) asciiflags[3] = 's';
2833 if (flags & MA_READ) asciiflags[4] = 'r';
2834 if (flags & MA_WRITE) asciiflags[5] = 'w';
2835 if (flags & MA_EXEC) asciiflags[6] = 'x';
a39ad5ce
FF
2836 return (asciiflags);
2837}
2838
2839static void
cc221e76
FF
2840info_proc_flags (pip, summary)
2841 struct procinfo *pip;
2842 int summary;
2843{
2844 struct trans *transp;
2845
2846 printf_filtered ("%-32s", "Process status flags:");
2847 if (!summary)
2848 {
2849 printf_filtered ("\n\n");
2850 }
2851 for (transp = pr_flag_table; transp -> name != NULL; transp++)
2852 {
2853 if (pip -> prstatus.pr_flags & transp -> value)
2854 {
2855 if (summary)
2856 {
2857 printf_filtered ("%s ", transp -> name);
2858 }
2859 else
2860 {
2861 printf_filtered ("\t%-16s %s.\n", transp -> name, transp -> desc);
2862 }
2863 }
2864 }
2865 printf_filtered ("\n");
2866}
2867
2868static void
2869info_proc_stop (pip, summary)
2870 struct procinfo *pip;
2871 int summary;
2872{
2873 struct trans *transp;
2874 int why;
2875 int what;
2876
2877 why = pip -> prstatus.pr_why;
2878 what = pip -> prstatus.pr_what;
2879
2880 if (pip -> prstatus.pr_flags & PR_STOPPED)
2881 {
2882 printf_filtered ("%-32s", "Reason for stopping:");
2883 if (!summary)
2884 {
2885 printf_filtered ("\n\n");
2886 }
2887 for (transp = pr_why_table; transp -> name != NULL; transp++)
2888 {
2889 if (why == transp -> value)
2890 {
2891 if (summary)
2892 {
2893 printf_filtered ("%s ", transp -> name);
2894 }
2895 else
2896 {
2897 printf_filtered ("\t%-16s %s.\n",
2898 transp -> name, transp -> desc);
2899 }
2900 break;
2901 }
2902 }
2903
2904 /* Use the pr_why field to determine what the pr_what field means, and
2905 print more information. */
2906
2907 switch (why)
2908 {
2909 case PR_REQUESTED:
2910 /* pr_what is unused for this case */
2911 break;
2912 case PR_JOBCONTROL:
2913 case PR_SIGNALLED:
2914 if (summary)
2915 {
2916 printf_filtered ("%s ", signalname (what));
2917 }
2918 else
2919 {
2920 printf_filtered ("\t%-16s %s.\n", signalname (what),
4ace50a5 2921 safe_strsignal (what));
cc221e76
FF
2922 }
2923 break;
2924 case PR_SYSENTRY:
2925 if (summary)
2926 {
2927 printf_filtered ("%s ", syscallname (what));
2928 }
2929 else
2930 {
2931 printf_filtered ("\t%-16s %s.\n", syscallname (what),
2932 "Entered this system call");
2933 }
2934 break;
2935 case PR_SYSEXIT:
2936 if (summary)
2937 {
2938 printf_filtered ("%s ", syscallname (what));
2939 }
2940 else
2941 {
2942 printf_filtered ("\t%-16s %s.\n", syscallname (what),
2943 "Returned from this system call");
2944 }
2945 break;
2946 case PR_FAULTED:
2947 if (summary)
2948 {
2949 printf_filtered ("%s ",
2950 lookupname (faults_table, what, "fault"));
2951 }
2952 else
2953 {
2954 printf_filtered ("\t%-16s %s.\n",
2955 lookupname (faults_table, what, "fault"),
2956 lookupdesc (faults_table, what));
2957 }
2958 break;
2959 }
2960 printf_filtered ("\n");
2961 }
2962}
2963
2964static void
2965info_proc_siginfo (pip, summary)
2966 struct procinfo *pip;
2967 int summary;
2968{
2969 struct siginfo *sip;
2970
2971 if ((pip -> prstatus.pr_flags & PR_STOPPED) &&
2972 (pip -> prstatus.pr_why == PR_SIGNALLED ||
2973 pip -> prstatus.pr_why == PR_FAULTED))
2974 {
2975 printf_filtered ("%-32s", "Additional signal/fault info:");
2976 sip = &pip -> prstatus.pr_info;
2977 if (summary)
2978 {
2979 printf_filtered ("%s ", signalname (sip -> si_signo));
2980 if (sip -> si_errno > 0)
2981 {
4ace50a5 2982 printf_filtered ("%s ", errnoname (sip -> si_errno));
cc221e76
FF
2983 }
2984 if (sip -> si_code <= 0)
2985 {
25286543
SG
2986 printf_filtered ("sent by %s, uid %d ",
2987 target_pid_to_str (sip -> si_pid),
cc221e76
FF
2988 sip -> si_uid);
2989 }
2990 else
2991 {
2992 printf_filtered ("%s ", sigcodename (sip));
2993 if ((sip -> si_signo == SIGILL) ||
2994 (sip -> si_signo == SIGFPE) ||
2995 (sip -> si_signo == SIGSEGV) ||
2996 (sip -> si_signo == SIGBUS))
2997 {
b9e58503
PS
2998 printf_filtered ("addr=%#lx ",
2999 (unsigned long) sip -> si_addr);
cc221e76
FF
3000 }
3001 else if ((sip -> si_signo == SIGCHLD))
3002 {
25286543
SG
3003 printf_filtered ("child %s, status %u ",
3004 target_pid_to_str (sip -> si_pid),
cc221e76
FF
3005 sip -> si_status);
3006 }
3007 else if ((sip -> si_signo == SIGPOLL))
3008 {
3009 printf_filtered ("band %u ", sip -> si_band);
3010 }
3011 }
3012 }
3013 else
3014 {
3015 printf_filtered ("\n\n");
3016 printf_filtered ("\t%-16s %s.\n", signalname (sip -> si_signo),
4ace50a5 3017 safe_strsignal (sip -> si_signo));
cc221e76
FF
3018 if (sip -> si_errno > 0)
3019 {
3020 printf_filtered ("\t%-16s %s.\n",
4ace50a5
FF
3021 errnoname (sip -> si_errno),
3022 safe_strerror (sip -> si_errno));
cc221e76
FF
3023 }
3024 if (sip -> si_code <= 0)
3025 {
25286543 3026 printf_filtered ("\t%-16u %s\n", sip -> si_pid, /* XXX need target_pid_to_str() */
cc221e76
FF
3027 "PID of process sending signal");
3028 printf_filtered ("\t%-16u %s\n", sip -> si_uid,
3029 "UID of process sending signal");
3030 }
3031 else
3032 {
3033 printf_filtered ("\t%-16s %s.\n", sigcodename (sip),
3034 sigcodedesc (sip));
3035 if ((sip -> si_signo == SIGILL) ||
3036 (sip -> si_signo == SIGFPE))
3037 {
b9e58503
PS
3038 printf_filtered ("\t%#-16lx %s.\n",
3039 (unsigned long) sip -> si_addr,
cc221e76
FF
3040 "Address of faulting instruction");
3041 }
3042 else if ((sip -> si_signo == SIGSEGV) ||
3043 (sip -> si_signo == SIGBUS))
3044 {
b9e58503
PS
3045 printf_filtered ("\t%#-16lx %s.\n",
3046 (unsigned long) sip -> si_addr,
cc221e76
FF
3047 "Address of faulting memory reference");
3048 }
3049 else if ((sip -> si_signo == SIGCHLD))
3050 {
25286543 3051 printf_filtered ("\t%-16u %s.\n", sip -> si_pid, /* XXX need target_pid_to_str() */
cc221e76
FF
3052 "Child process ID");
3053 printf_filtered ("\t%-16u %s.\n", sip -> si_status,
3054 "Child process exit value or signal");
3055 }
3056 else if ((sip -> si_signo == SIGPOLL))
3057 {
3058 printf_filtered ("\t%-16u %s.\n", sip -> si_band,
3059 "Band event for POLL_{IN,OUT,MSG}");
3060 }
3061 }
3062 }
3063 printf_filtered ("\n");
3064 }
3065}
3066
3067static void
3068info_proc_syscalls (pip, summary)
3069 struct procinfo *pip;
3070 int summary;
3071{
3072 int syscallnum;
3073
3074 if (!summary)
3075 {
3076
3077#if 0 /* FIXME: Needs to use gdb-wide configured info about system calls. */
3078 if (pip -> prstatus.pr_flags & PR_ASLEEP)
3079 {
3080 int syscallnum = pip -> prstatus.pr_reg[R_D0];
3081 if (summary)
3082 {
3083 printf_filtered ("%-32s", "Sleeping in system call:");
3084 printf_filtered ("%s", syscallname (syscallnum));
3085 }
3086 else
3087 {
3088 printf_filtered ("Sleeping in system call '%s'.\n",
3089 syscallname (syscallnum));
3090 }
3091 }
3092#endif
3093
3094 if (ioctl (pip -> fd, PIOCGENTRY, &pip -> entryset) < 0)
3095 {
3096 print_sys_errmsg (pip -> pathname, errno);
3097 error ("PIOCGENTRY failed");
3098 }
3099
3100 if (ioctl (pip -> fd, PIOCGEXIT, &pip -> exitset) < 0)
3101 {
3102 print_sys_errmsg (pip -> pathname, errno);
3103 error ("PIOCGEXIT failed");
3104 }
3105
3106 printf_filtered ("System call tracing information:\n\n");
3107
3108 printf_filtered ("\t%-12s %-8s %-8s\n",
3109 "System call",
3110 "Entry",
3111 "Exit");
3112 for (syscallnum = 0; syscallnum < MAX_SYSCALLS; syscallnum++)
3113 {
3114 QUIT;
3115 if (syscall_table[syscallnum] != NULL)
3116 {
3117 printf_filtered ("\t%-12s ", syscall_table[syscallnum]);
3118 printf_filtered ("%-8s ",
3119 prismember (&pip -> entryset, syscallnum)
3120 ? "on" : "off");
3121 printf_filtered ("%-8s ",
3122 prismember (&pip -> exitset, syscallnum)
3123 ? "on" : "off");
3124 printf_filtered ("\n");
3125 }
3126 }
3127 printf_filtered ("\n");
3128 }
3129}
3130
3131static char *
3132signalname (signo)
3133 int signo;
3134{
26a859ec 3135 const char *name;
4ace50a5
FF
3136 static char locbuf[32];
3137
3138 name = strsigno (signo);
3139 if (name == NULL)
3140 {
3141 sprintf (locbuf, "Signal %d", signo);
3142 }
3143 else
3144 {
3145 sprintf (locbuf, "%s (%d)", name, signo);
3146 }
3147 return (locbuf);
3148}
3149
3150static char *
3151errnoname (errnum)
3152 int errnum;
3153{
26a859ec 3154 const char *name;
cc221e76
FF
3155 static char locbuf[32];
3156
4ace50a5
FF
3157 name = strerrno (errnum);
3158 if (name == NULL)
cc221e76 3159 {
4ace50a5 3160 sprintf (locbuf, "Errno %d", errnum);
cc221e76
FF
3161 }
3162 else
3163 {
4ace50a5 3164 sprintf (locbuf, "%s (%d)", name, errnum);
cc221e76
FF
3165 }
3166 return (locbuf);
3167}
3168
3169static void
3170info_proc_signals (pip, summary)
3171 struct procinfo *pip;
3172 int summary;
3173{
3174 int signo;
3175
3176 if (!summary)
3177 {
3178 if (ioctl (pip -> fd, PIOCGTRACE, &pip -> trace) < 0)
3179 {
3180 print_sys_errmsg (pip -> pathname, errno);
3181 error ("PIOCGTRACE failed");
3182 }
3183
3184 printf_filtered ("Disposition of signals:\n\n");
3185 printf_filtered ("\t%-15s %-8s %-8s %-8s %s\n\n",
3186 "Signal", "Trace", "Hold", "Pending", "Description");
3187 for (signo = 0; signo < NSIG; signo++)
3188 {
3189 QUIT;
3190 printf_filtered ("\t%-15s ", signalname (signo));
3191 printf_filtered ("%-8s ",
3192 prismember (&pip -> trace, signo)
3193 ? "on" : "off");
3194 printf_filtered ("%-8s ",
3195 prismember (&pip -> prstatus.pr_sighold, signo)
3196 ? "on" : "off");
2592eef8
PS
3197
3198#ifdef PROCFS_SIGPEND_OFFSET
3199 /* Alpha OSF/1 numbers the pending signals from 1. */
3200 printf_filtered ("%-8s ",
3201 (signo ? prismember (&pip -> prstatus.pr_sigpend,
3202 signo - 1)
3203 : 0)
3204 ? "yes" : "no");
3205#else
cc221e76
FF
3206 printf_filtered ("%-8s ",
3207 prismember (&pip -> prstatus.pr_sigpend, signo)
3208 ? "yes" : "no");
2592eef8 3209#endif
4ace50a5 3210 printf_filtered (" %s\n", safe_strsignal (signo));
cc221e76
FF
3211 }
3212 printf_filtered ("\n");
3213 }
3214}
3215
3216static void
3217info_proc_faults (pip, summary)
3218 struct procinfo *pip;
3219 int summary;
3220{
3221 struct trans *transp;
3222
3223 if (!summary)
3224 {
3225 if (ioctl (pip -> fd, PIOCGFAULT, &pip -> fltset) < 0)
3226 {
3227 print_sys_errmsg (pip -> pathname, errno);
3228 error ("PIOCGFAULT failed");
3229 }
3230
3231 printf_filtered ("Current traced hardware fault set:\n\n");
3232 printf_filtered ("\t%-12s %-8s\n", "Fault", "Trace");
3233
3234 for (transp = faults_table; transp -> name != NULL; transp++)
3235 {
3236 QUIT;
3237 printf_filtered ("\t%-12s ", transp -> name);
3238 printf_filtered ("%-8s", prismember (&pip -> fltset, transp -> value)
3239 ? "on" : "off");
3240 printf_filtered ("\n");
3241 }
3242 printf_filtered ("\n");
3243 }
3244}
3245
3246static void
3247info_proc_mappings (pip, summary)
1ab3bf1b 3248 struct procinfo *pip;
cc221e76 3249 int summary;
a39ad5ce
FF
3250{
3251 int nmap;
3252 struct prmap *prmaps;
3253 struct prmap *prmap;
3254
cc221e76 3255 if (!summary)
a39ad5ce 3256 {
cc221e76 3257 printf_filtered ("Mapped address spaces:\n\n");
2592eef8
PS
3258#ifdef BFD_HOST_64_BIT
3259 printf_filtered (" %18s %18s %10s %10s %7s\n",
3260#else
5c1c5e67 3261 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2592eef8 3262#endif
cc221e76
FF
3263 "Start Addr",
3264 " End Addr",
3265 " Size",
3266 " Offset",
3267 "Flags");
3268 if (ioctl (pip -> fd, PIOCNMAP, &nmap) == 0)
a39ad5ce 3269 {
cc221e76
FF
3270 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
3271 if (ioctl (pip -> fd, PIOCMAP, prmaps) == 0)
a39ad5ce 3272 {
cc221e76
FF
3273 for (prmap = prmaps; prmap -> pr_size; ++prmap)
3274 {
2592eef8
PS
3275#ifdef BFD_HOST_64_BIT
3276 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3277#else
3278 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3279#endif
3280 (unsigned long)prmap -> pr_vaddr,
3281 (unsigned long)prmap -> pr_vaddr
3282 + prmap -> pr_size - 1,
cc221e76
FF
3283 prmap -> pr_size,
3284 prmap -> pr_off,
3285 mappingflags (prmap -> pr_mflags));
3286 }
a39ad5ce
FF
3287 }
3288 }
cc221e76 3289 printf_filtered ("\n");
a39ad5ce 3290 }
a39ad5ce
FF
3291}
3292
3293/*
3294
3295LOCAL FUNCTION
3296
cc221e76 3297 info_proc -- implement the "info proc" command
a39ad5ce
FF
3298
3299SYNOPSIS
3300
cc221e76 3301 void info_proc (char *args, int from_tty)
a39ad5ce
FF
3302
3303DESCRIPTION
3304
3305 Implement gdb's "info proc" command by using the /proc interface
3306 to print status information about any currently running process.
3307
3308 Examples of the use of "info proc" are:
3309
cc221e76
FF
3310 info proc (prints summary info for current inferior)
3311 info proc 123 (prints summary info for process with pid 123)
3312 info proc mappings (prints address mappings)
3313 info proc times (prints process/children times)
3314 info proc id (prints pid, ppid, gid, sid, etc)
3fbdd536 3315 FIXME: i proc id not implemented.
cc221e76 3316 info proc status (prints general process state info)
3fbdd536 3317 FIXME: i proc status not implemented.
cc221e76
FF
3318 info proc signals (prints info about signal handling)
3319 info proc all (prints all info)
a39ad5ce
FF
3320
3321 */
3322
3323static void
cc221e76 3324info_proc (args, from_tty)
1ab3bf1b
JG
3325 char *args;
3326 int from_tty;
a39ad5ce 3327{
a39ad5ce 3328 int pid;
a39ad5ce
FF
3329 struct procinfo *pip;
3330 struct cleanup *old_chain;
cc221e76
FF
3331 char **argv;
3332 int argsize;
3333 int summary = 1;
3334 int flags = 0;
3335 int syscalls = 0;
3336 int signals = 0;
3337 int faults = 0;
3338 int mappings = 0;
3339 int times = 0;
3340 int id = 0;
3341 int status = 0;
3342 int all = 0;
a39ad5ce
FF
3343
3344 old_chain = make_cleanup (null_cleanup, 0);
3345
de43d7d0
SG
3346 /* Default to using the current inferior if no pid specified. Note
3347 that inferior_pid may be 0, hence we set okerr. */
a39ad5ce 3348
de43d7d0 3349 pip = find_procinfo (inferior_pid, 1);
a39ad5ce 3350
a39ad5ce 3351 if (args != NULL)
35f5886e 3352 {
cc221e76 3353 if ((argv = buildargv (args)) == NULL)
a39ad5ce 3354 {
cc221e76
FF
3355 nomem (0);
3356 }
3357 make_cleanup (freeargv, (char *) argv);
3358
3359 while (*argv != NULL)
3360 {
3361 argsize = strlen (*argv);
3362 if (argsize >= 1 && strncmp (*argv, "all", argsize) == 0)
3363 {
3364 summary = 0;
3365 all = 1;
3366 }
3367 else if (argsize >= 2 && strncmp (*argv, "faults", argsize) == 0)
3368 {
3369 summary = 0;
3370 faults = 1;
3371 }
3372 else if (argsize >= 2 && strncmp (*argv, "flags", argsize) == 0)
3373 {
3374 summary = 0;
3375 flags = 1;
3376 }
3377 else if (argsize >= 1 && strncmp (*argv, "id", argsize) == 0)
3378 {
3379 summary = 0;
3380 id = 1;
3381 }
3382 else if (argsize >= 1 && strncmp (*argv, "mappings", argsize) == 0)
3383 {
3384 summary = 0;
3385 mappings = 1;
3386 }
3387 else if (argsize >= 2 && strncmp (*argv, "signals", argsize) == 0)
3388 {
3389 summary = 0;
3390 signals = 1;
3391 }
3392 else if (argsize >= 2 && strncmp (*argv, "status", argsize) == 0)
3393 {
3394 summary = 0;
3395 status = 1;
3396 }
3397 else if (argsize >= 2 && strncmp (*argv, "syscalls", argsize) == 0)
3398 {
3399 summary = 0;
3400 syscalls = 1;
3401 }
3402 else if (argsize >= 1 && strncmp (*argv, "times", argsize) == 0)
a39ad5ce 3403 {
cc221e76
FF
3404 summary = 0;
3405 times = 1;
a39ad5ce 3406 }
de43d7d0 3407 else if ((pid = atoi (*argv)) > 0)
a39ad5ce 3408 {
de43d7d0
SG
3409 pip = (struct procinfo *) xmalloc (sizeof (struct procinfo));
3410 memset (pip, 0, sizeof (*pip));
3411
3412 pip->pid = pid;
ec8ceca3 3413 if (!open_proc_file (pid, pip, O_RDONLY))
a39ad5ce
FF
3414 {
3415 perror_with_name (pip -> pathname);
3416 /* NOTREACHED */
3417 }
3418 make_cleanup (close_proc_file, pip);
3419 }
cc221e76
FF
3420 else if (**argv != '\000')
3421 {
3422 error ("Unrecognized or ambiguous keyword `%s'.", *argv);
3423 }
3424 argv++;
a39ad5ce 3425 }
35f5886e 3426 }
a39ad5ce
FF
3427
3428 /* If we don't have a valid open process at this point, then we have no
3429 inferior or didn't specify a specific pid. */
3430
de43d7d0 3431 if (!pip)
35f5886e 3432 {
6fe90fc8
JK
3433 error ("\
3434No process. Start debugging a program or specify an explicit process ID.");
35f5886e 3435 }
a39ad5ce 3436 if (ioctl (pip -> fd, PIOCSTATUS, &(pip -> prstatus)) < 0)
35f5886e 3437 {
a39ad5ce
FF
3438 print_sys_errmsg (pip -> pathname, errno);
3439 error ("PIOCSTATUS failed");
35f5886e 3440 }
a39ad5ce 3441
cc221e76
FF
3442 /* Print verbose information of the requested type(s), or just a summary
3443 of the information for all types. */
3444
3445 printf_filtered ("\nInformation for %s:\n\n", pip -> pathname);
3446 if (summary || all || flags)
3447 {
3448 info_proc_flags (pip, summary);
3449 }
3450 if (summary || all)
3451 {
3452 info_proc_stop (pip, summary);
3453 }
3454 if (summary || all || signals || faults)
3455 {
3456 info_proc_siginfo (pip, summary);
3457 }
3458 if (summary || all || syscalls)
3459 {
3460 info_proc_syscalls (pip, summary);
3461 }
3462 if (summary || all || mappings)
3463 {
3464 info_proc_mappings (pip, summary);
3465 }
3466 if (summary || all || signals)
3467 {
3468 info_proc_signals (pip, summary);
3469 }
3470 if (summary || all || faults)
3471 {
3472 info_proc_faults (pip, summary);
3473 }
3474 printf_filtered ("\n");
a39ad5ce
FF
3475
3476 /* All done, deal with closing any temporary process info structure,
3477 freeing temporary memory , etc. */
3478
3479 do_cleanups (old_chain);
3480}
3481
de43d7d0
SG
3482/*
3483
3484LOCAL FUNCTION
3485
eca4a350 3486 procfs_set_sproc_trap -- arrange for child to stop on sproc().
de43d7d0
SG
3487
3488SYNOPSIS
3489
eca4a350 3490 void procfs_set_sproc_trap (struct procinfo *)
de43d7d0
SG
3491
3492DESCRIPTION
3493
3494 This function sets up a trap on sproc system call exits so that we can
eca4a350 3495 detect the arrival of a new thread. We are called with the new thread
de43d7d0
SG
3496 stopped prior to it's first instruction.
3497
3498 Also note that we turn on the inherit-on-fork flag in the child process
3499 so that any grand-children start with all tracing flags set.
3500 */
3501
952a820e
SG
3502#ifdef SYS_sproc
3503
de43d7d0
SG
3504static void
3505procfs_set_sproc_trap (pi)
3506 struct procinfo *pi;
3507{
3508 sysset_t exitset;
3509
3510 if (ioctl (pi->fd, PIOCGEXIT, &exitset) < 0)
3511 {
3512 print_sys_errmsg (pi->pathname, errno);
3513 error ("PIOCGEXIT failed");
3514 }
3515
de43d7d0 3516 praddset (&exitset, SYS_sproc);
de43d7d0 3517
eca4a350
SG
3518 /* We trap on fork() and vfork() in order to disable debugging in our grand-
3519 children and descendant processes. At this time, GDB can only handle
3520 threads (multiple processes, one address space). forks (and execs) result
3521 in the creation of multiple address spaces, which GDB can't handle yet. */
3522
3523 praddset (&exitset, SYS_fork);
3524#ifdef SYS_vfork
3525 praddset (&exitset, SYS_vfork);
3526#endif
3527
de43d7d0
SG
3528 if (ioctl (pi->fd, PIOCSEXIT, &exitset) < 0)
3529 {
3530 print_sys_errmsg (pi->pathname, errno);
3531 error ("PIOCSEXIT failed");
3532 }
3533
3534 /* Turn on inherit-on-fork flag so that all grand-children of gdb start with
3535 tracing flags set. */
3536
3537#ifdef PIOCSET /* New method */
3538 {
3539 long pr_flags;
3540 pr_flags = PR_FORK;
3541 ioctl (pi->fd, PIOCSET, &pr_flags);
3542 }
3543#else
3544#ifdef PIOCSFORK /* Original method */
3545 ioctl (pi->fd, PIOCSFORK, NULL);
3546#endif
3547#endif
3548}
952a820e 3549#endif /* SYS_sproc */
de43d7d0 3550
3fbdd536
JG
3551/* Fork an inferior process, and start debugging it with /proc. */
3552
3553static void
3554procfs_create_inferior (exec_file, allargs, env)
3555 char *exec_file;
3556 char *allargs;
3557 char **env;
3558{
08f74b92
JK
3559 char *shell_file = getenv ("SHELL");
3560 char *tryname;
3561 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
3562 {
3563
3564 /* We will be looking down the PATH to find shell_file. If we
3565 just do this the normal way (via execlp, which operates by
3566 attempting an exec for each element of the PATH until it
3567 finds one which succeeds), then there will be an exec for
3568 each failed attempt, each of which will cause a PR_SYSEXIT
3569 stop, and we won't know how to distinguish the PR_SYSEXIT's
3570 for these failed execs with the ones for successful execs
3571 (whether the exec has succeeded is stored at that time in the
3572 carry bit or some such architecture-specific and
3573 non-ABI-specified place).
3574
3575 So I can't think of anything better than to search the PATH
3576 now. This has several disadvantages: (1) There is a race
3577 condition; if we find a file now and it is deleted before we
3578 exec it, we lose, even if the deletion leaves a valid file
3579 further down in the PATH, (2) there is no way to know exactly
3580 what an executable (in the sense of "capable of being
3581 exec'd") file is. Using access() loses because it may lose
3582 if the caller is the superuser; failing to use it loses if
3583 there are ACLs or some such. */
3584
3585 char *p;
3586 char *p1;
f93b941b
JK
3587 /* FIXME-maybe: might want "set path" command so user can change what
3588 path is used from within GDB. */
08f74b92
JK
3589 char *path = getenv ("PATH");
3590 int len;
3591 struct stat statbuf;
3592
3593 if (path == NULL)
3594 path = "/bin:/usr/bin";
3595
3596 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
3597 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
3598 {
3599 p1 = strchr (p, ':');
3600 if (p1 != NULL)
3601 len = p1 - p;
3602 else
3603 len = strlen (p);
3604 strncpy (tryname, p, len);
3605 tryname[len] = '\0';
3606 strcat (tryname, "/");
3607 strcat (tryname, shell_file);
3608 if (access (tryname, X_OK) < 0)
3609 continue;
3610 if (stat (tryname, &statbuf) < 0)
3611 continue;
3612 if (!S_ISREG (statbuf.st_mode))
3613 /* We certainly need to reject directories. I'm not quite
3614 as sure about FIFOs, sockets, etc., but I kind of doubt
3615 that people want to exec() these things. */
3616 continue;
3617 break;
3618 }
3619 if (p == NULL)
3620 /* Not found. This must be an error rather than merely passing
3621 the file to execlp(), because execlp() would try all the
3622 exec()s, causing GDB to get confused. */
3623 error ("Can't find shell %s in PATH", shell_file);
3624
3625 shell_file = tryname;
3626 }
3627
3fbdd536 3628 fork_inferior (exec_file, allargs, env,
08f74b92
JK
3629 proc_set_exec_trap, procfs_init_inferior, shell_file);
3630
3fbdd536
JG
3631 /* We are at the first instruction we care about. */
3632 /* Pedal to the metal... */
de43d7d0
SG
3633
3634 /* Setup traps on exit from sproc() */
3635
952a820e
SG
3636#ifdef SYS_sproc
3637 procfs_set_sproc_trap (current_procinfo);
3638#endif
de43d7d0 3639
67ac9759 3640 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
3fbdd536
JG
3641}
3642
3643/* Clean up after the inferior dies. */
3644
3645static void
3646procfs_mourn_inferior ()
3647{
fb63d460 3648 struct procinfo *pi;
cd4104e0 3649 struct procinfo *next_pi;
fb63d460 3650
cd4104e0
TL
3651 for (pi = procinfo_list; pi; pi = next_pi)
3652 {
3653 next_pi = pi->next;
3654 unconditionally_kill_inferior (pi);
3655 }
fb63d460 3656
3fbdd536
JG
3657 unpush_target (&procfs_ops);
3658 generic_mourn_inferior ();
3659}
3660
cd4104e0 3661
3fbdd536
JG
3662/* Mark our target-struct as eligible for stray "run" and "attach" commands. */
3663static int
3664procfs_can_run ()
3665{
3666 return(1);
3667}
f5a8f1a6 3668#ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
999dd04b
JL
3669\f
3670/* Insert a watchpoint */
3671int
3672procfs_set_watchpoint(pid, addr, len, rw)
3673 int pid;
3674 CORE_ADDR addr;
3675 int len;
3676 int rw;
3677{
3678 struct procinfo *pi;
3679 prwatch_t wpt;
3680
3681 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
3682 wpt.pr_vaddr = (caddr_t)addr;
3683 wpt.pr_size = len;
3684 wpt.pr_wflags = ((rw & 1) ? MA_READ : 0) | ((rw & 2) ? MA_WRITE : 0);
3685 if (ioctl (pi->fd, PIOCSWATCH, &wpt) < 0)
3686 {
3687 if (errno == E2BIG)
3688 return -1;
3689 /* Currently it sometimes happens that the same watchpoint gets
3690 deleted twice - don't die in this case (FIXME please) */
3691 if (errno == ESRCH && len == 0)
3692 return 0;
3693 print_sys_errmsg (pi->pathname, errno);
3694 error ("PIOCSWATCH failed");
3695 }
3696 return 0;
3697}
3698
3699int
3700procfs_stopped_by_watchpoint(pid)
3701 int pid;
3702{
3703 struct procinfo *pi;
3704 short what;
3705 short why;
3706
3707 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
3708 if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
3709 {
3710 why = pi->prstatus.pr_why;
3711 what = pi->prstatus.pr_what;
3712 if (why == PR_FAULTED
6bc194d2 3713#if defined (FLTWATCH) && defined (FLTKWATCH)
255181a9 3714 && (what == FLTWATCH || what == FLTKWATCH)
6bc194d2
JL
3715#else
3716#ifdef FLTWATCH
3717 && (what == FLTWATCH)
3718#endif
3719#ifdef FLTKWATCH
3720 && (what == FLTKWATCH)
3721#endif
72b8ca51 3722#endif
6bc194d2 3723 )
999dd04b
JL
3724 return what;
3725 }
3726 return 0;
3727}
6bc194d2 3728#endif
999dd04b 3729
78b459a7
SG
3730/* Send a SIGINT to the process group. This acts just like the user typed a
3731 ^C on the controlling terminal.
3732
3733 XXX - This may not be correct for all systems. Some may want to use
3734 killpg() instead of kill (-pgrp). */
3735
3736void
2592eef8 3737procfs_stop ()
78b459a7
SG
3738{
3739 extern pid_t inferior_process_group;
3740
3741 kill (-inferior_process_group, SIGINT);
3742}
3743
3fbdd536
JG
3744\f
3745struct target_ops procfs_ops = {
3746 "procfs", /* to_shortname */
3747 "Unix /proc child process", /* to_longname */
3748 "Unix /proc child process (started by the \"run\" command).", /* to_doc */
3749 procfs_open, /* to_open */
3750 0, /* to_close */
3751 procfs_attach, /* to_attach */
3752 procfs_detach, /* to_detach */
3753 procfs_resume, /* to_resume */
3754 procfs_wait, /* to_wait */
3755 procfs_fetch_registers, /* to_fetch_registers */
3756 procfs_store_registers, /* to_store_registers */
3757 procfs_prepare_to_store, /* to_prepare_to_store */
3758 procfs_xfer_memory, /* to_xfer_memory */
3759 procfs_files_info, /* to_files_info */
3760 memory_insert_breakpoint, /* to_insert_breakpoint */
3761 memory_remove_breakpoint, /* to_remove_breakpoint */
3762 terminal_init_inferior, /* to_terminal_init */
3763 terminal_inferior, /* to_terminal_inferior */
3764 terminal_ours_for_output, /* to_terminal_ours_for_output */
3765 terminal_ours, /* to_terminal_ours */
3766 child_terminal_info, /* to_terminal_info */
3767 procfs_kill_inferior, /* to_kill */
3768 0, /* to_load */
3769 0, /* to_lookup_symbol */
3770 procfs_create_inferior, /* to_create_inferior */
3771 procfs_mourn_inferior, /* to_mourn_inferior */
3772 procfs_can_run, /* to_can_run */
3950a34e 3773 procfs_notice_signals, /* to_notice_signals */
10f644eb 3774 0, /* to_thread_alive */
2592eef8 3775 procfs_stop, /* to_stop */
3fbdd536
JG
3776 process_stratum, /* to_stratum */
3777 0, /* to_next */
3778 1, /* to_has_all_memory */
3779 1, /* to_has_memory */
3780 1, /* to_has_stack */
3781 1, /* to_has_registers */
3782 1, /* to_has_execution */
3783 0, /* sections */
3784 0, /* sections_end */
3785 OPS_MAGIC /* to_magic */
3786};
3787
3fbdd536
JG
3788void
3789_initialize_procfs ()
3790{
2592eef8
PS
3791#ifdef HAVE_OPTIONAL_PROC_FS
3792 char procname[32];
3793 int fd;
3794
3795 /* If we have an optional /proc filesystem (e.g. under OSF/1),
3796 don't add procfs support if we cannot access the running
3797 GDB via /proc. */
3798 sprintf (procname, PROC_NAME_FMT, getpid ());
3799 if ((fd = open (procname, O_RDONLY)) < 0)
3800 return;
3801 close (fd);
3802#endif
3803
3fbdd536
JG
3804 add_target (&procfs_ops);
3805
3806 add_info ("proc", info_proc,
cc221e76
FF
3807"Show process status information using /proc entry.\n\
3808Specify process id or use current inferior by default.\n\
3809Specify keywords for detailed information; default is summary.\n\
3810Keywords are: `all', `faults', `flags', `id', `mappings', `signals',\n\
3811`status', `syscalls', and `times'.\n\
3fbdd536 3812Unambiguous abbreviations may be used.");
a39ad5ce 3813
cc221e76 3814 init_syscall_table ();
35f5886e 3815}
This page took 0.417261 seconds and 4 git commands to generate.