(arc_get_disassembler): Renamed from arc_disassembler.
[deliverable/binutils-gdb.git] / gdb / procfs.c
CommitLineData
35f5886e 1/* Machine independent support for SVR4 /proc (process file system) for GDB.
2592eef8 2 Copyright 1991, 1992, 1993, 1994, 1995 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
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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>
51b57ded 44#include <string.h>
de43d7d0
SG
45#include <stropts.h>
46#include <poll.h>
08f74b92
JK
47#include <unistd.h>
48#include <sys/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;
de43d7d0 1253 ioctl (pi->fd, PIOCKILL, &signo);
2592eef8
PS
1254
1255#ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
1256 /* Alpha OSF/1 procfs needs an additional PIOCSSIG call with
1257 a SIGKILL signal to kill the inferior, otherwise it might remain
1258 stopped with a pending SIGKILL.
1259 We do not check the result of the PIOCSSIG, the inferior might have
1260 died already. */
1261 {
1262 struct siginfo newsiginfo;
1263
1264 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
1265 newsiginfo.si_signo = signo;
1266 newsiginfo.si_code = 0;
1267 newsiginfo.si_errno = 0;
1268 newsiginfo.si_pid = getpid ();
1269 newsiginfo.si_uid = getuid ();
1270 ioctl (pi->fd, PIOCSSIG, &newsiginfo);
1271 }
1272#endif
1273
de43d7d0
SG
1274 close_proc_file (pi);
1275
1276/* Only wait() for our direct children. Our grandchildren zombies are killed
1277 by the death of their parents. */
1278
1279 if (ppid == getpid())
1280 wait ((int *) 0);
35f5886e
FF
1281}
1282
1283/*
1284
3fbdd536 1285LOCAL FUNCTION
35f5886e 1286
3fbdd536 1287 procfs_xfer_memory -- copy data to or from inferior memory space
35f5886e
FF
1288
1289SYNOPSIS
1290
3fbdd536 1291 int procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
35f5886e
FF
1292 int dowrite, struct target_ops target)
1293
1294DESCRIPTION
1295
1296 Copy LEN bytes to/from inferior's memory starting at MEMADDR
1297 from/to debugger memory starting at MYADDR. Copy from inferior
1298 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
1299
1300 Returns the length copied, which is either the LEN argument or
3fbdd536 1301 zero. This xfer function does not do partial moves, since procfs_ops
35f5886e
FF
1302 doesn't allow memory operations to cross below us in the target stack
1303 anyway.
1304
1305NOTES
1306
1307 The /proc interface makes this an almost trivial task.
1308 */
1309
3fbdd536
JG
1310static int
1311procfs_xfer_memory (memaddr, myaddr, len, dowrite, target)
1ab3bf1b
JG
1312 CORE_ADDR memaddr;
1313 char *myaddr;
1314 int len;
1315 int dowrite;
1316 struct target_ops *target; /* ignored */
35f5886e
FF
1317{
1318 int nbytes = 0;
de43d7d0 1319 struct procinfo *pi;
35f5886e 1320
de43d7d0
SG
1321 pi = current_procinfo;
1322
1323 if (lseek(pi->fd, (off_t) memaddr, 0) == (off_t) memaddr)
35f5886e
FF
1324 {
1325 if (dowrite)
1326 {
de43d7d0 1327 nbytes = write (pi->fd, myaddr, len);
35f5886e
FF
1328 }
1329 else
1330 {
de43d7d0 1331 nbytes = read (pi->fd, myaddr, len);
35f5886e
FF
1332 }
1333 if (nbytes < 0)
1334 {
1335 nbytes = 0;
1336 }
1337 }
1338 return (nbytes);
1339}
1340
1341/*
1342
3fbdd536 1343LOCAL FUNCTION
35f5886e 1344
3fbdd536 1345 procfs_store_registers -- copy register values back to inferior
35f5886e
FF
1346
1347SYNOPSIS
1348
3fbdd536 1349 void procfs_store_registers (int regno)
35f5886e
FF
1350
1351DESCRIPTION
1352
1353 Store our current register values back into the inferior. If
1354 REGNO is -1 then store all the register, otherwise store just
1355 the value specified by REGNO.
1356
1357NOTES
1358
1359 If we are storing only a single register, we first have to get all
1360 the current values from the process, overwrite the desired register
1361 in the gregset with the one we want from gdb's registers, and then
1362 send the whole set back to the process. For writing all the
1363 registers, all we have to do is generate the gregset and send it to
1364 the process.
1365
1366 Also note that the process has to be stopped on an event of interest
1367 for this to work, which basically means that it has to have been
1368 run under the control of one of the other /proc ioctl calls and not
1369 ptrace. Since we don't use ptrace anyway, we don't worry about this
1370 fine point, but it is worth noting for future reference.
1371
1372 Gdb is confused about what this function is supposed to return.
1373 Some versions return a value, others return nothing. Some are
1374 declared to return a value and actually return nothing. Gdb ignores
1375 anything returned. (FIXME)
1376
1377 */
1378
3fbdd536
JG
1379static void
1380procfs_store_registers (regno)
1ab3bf1b 1381 int regno;
35f5886e 1382{
de43d7d0
SG
1383 struct procinfo *pi;
1384
1385 pi = current_procinfo;
1386
35f5886e
FF
1387 if (regno != -1)
1388 {
de43d7d0 1389 ioctl (pi->fd, PIOCGREG, &pi->gregset);
35f5886e 1390 }
de43d7d0
SG
1391 fill_gregset (&pi->gregset, regno);
1392 ioctl (pi->fd, PIOCSREG, &pi->gregset);
35f5886e
FF
1393
1394#if defined (FP0_REGNUM)
1395
1396 /* Now repeat everything using the floating point register set, if the
1397 target has floating point hardware. Since we ignore the returned value,
1398 we'll never know whether it worked or not anyway. */
1399
1400 if (regno != -1)
1401 {
de43d7d0 1402 ioctl (pi->fd, PIOCGFPREG, &pi->fpregset);
35f5886e 1403 }
de43d7d0
SG
1404 fill_fpregset (&pi->fpregset, regno);
1405 ioctl (pi->fd, PIOCSFPREG, &pi->fpregset);
35f5886e
FF
1406
1407#endif /* FP0_REGNUM */
1408
1409}
1410
1411/*
1412
3fbdd536 1413LOCAL FUNCTION
35f5886e 1414
de43d7d0
SG
1415 create_procinfo - initialize access to a /proc entry
1416
1417SYNOPSIS
1418
eca4a350 1419 struct procinfo * create_procinfo (int pid)
de43d7d0
SG
1420
1421DESCRIPTION
1422
eca4a350
SG
1423 Allocate a procinfo structure, open the /proc file and then set up the
1424 set of signals and faults that are to be traced. Returns a pointer to
1425 the new procinfo structure.
de43d7d0
SG
1426
1427NOTES
1428
1429 If proc_init_failed ever gets called, control returns to the command
1430 processing loop via the standard error handling code.
1431
1432 */
1433
eca4a350 1434static struct procinfo *
de43d7d0
SG
1435create_procinfo (pid)
1436 int pid;
1437{
1438 struct procinfo *pi;
1439
2592eef8
PS
1440 pi = find_procinfo (pid, 1);
1441 if (pi != NULL)
1442 return pi; /* All done! It already exists */
de43d7d0
SG
1443
1444 pi = (struct procinfo *) xmalloc (sizeof (struct procinfo));
1445
1446 if (!open_proc_file (pid, pi, O_RDWR))
1447 proc_init_failed (pi, "can't open process file");
1448
1449 /* Add new process to process info list */
1450
1451 pi->next = procinfo_list;
1452 procinfo_list = pi;
1453
1454 add_fd (pi); /* Add to list for poll/select */
1455
1456 memset ((char *) &pi->prrun, 0, sizeof (pi->prrun));
1457 prfillset (&pi->prrun.pr_trace);
1458 procfs_notice_signals (pid);
1459 prfillset (&pi->prrun.pr_fault);
1460 prdelset (&pi->prrun.pr_fault, FLTPAGE);
1461
2592eef8
PS
1462#ifdef PROCFS_DONT_TRACE_IFAULT
1463 /* Tracing T_IFAULT under Alpha OSF/1 causes a `floating point enable'
1464 fault from which we cannot continue (except by disabling the
1465 tracing). We rely on the delivery of a SIGTRAP signal (which is traced)
1466 for the other T_IFAULT faults if tracing them is disabled. */
1467 prdelset (&pi->prrun.pr_fault, T_IFAULT);
1468#endif
1469
de43d7d0
SG
1470 if (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0)
1471 proc_init_failed (pi, "PIOCWSTOP failed");
1472
1473 if (ioctl (pi->fd, PIOCSFAULT, &pi->prrun.pr_fault) < 0)
1474 proc_init_failed (pi, "PIOCSFAULT failed");
eca4a350
SG
1475
1476 return pi;
de43d7d0
SG
1477}
1478
1479/*
1480
1481LOCAL FUNCTION
1482
1483 procfs_init_inferior - initialize target vector and access to a
1484 /proc entry
35f5886e
FF
1485
1486SYNOPSIS
1487
3fbdd536 1488 void procfs_init_inferior (int pid)
35f5886e
FF
1489
1490DESCRIPTION
1491
1492 When gdb starts an inferior, this function is called in the parent
1493 process immediately after the fork. It waits for the child to stop
1494 on the return from the exec system call (the child itself takes care
1495 of ensuring that this is set up), then sets up the set of signals
1496 and faults that are to be traced.
1497
1498NOTES
1499
1500 If proc_init_failed ever gets called, control returns to the command
1501 processing loop via the standard error handling code.
cc221e76 1502
35f5886e
FF
1503 */
1504
3fbdd536
JG
1505static void
1506procfs_init_inferior (pid)
1ab3bf1b 1507 int pid;
35f5886e 1508{
3fbdd536
JG
1509 push_target (&procfs_ops);
1510
de43d7d0
SG
1511 create_procinfo (pid);
1512 add_thread (pid); /* Setup initial thread */
bc28a06c 1513
2592eef8
PS
1514#ifdef START_INFERIOR_TRAPS_EXPECTED
1515 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
1516#else
bc28a06c
JK
1517 /* One trap to exec the shell, one to exec the program being debugged. */
1518 startup_inferior (2);
2592eef8 1519#endif
35f5886e
FF
1520}
1521
1522/*
1523
cc221e76
FF
1524GLOBAL FUNCTION
1525
3950a34e 1526 procfs_notice_signals
cc221e76
FF
1527
1528SYNOPSIS
1529
952a820e 1530 static void procfs_notice_signals (int pid);
cc221e76
FF
1531
1532DESCRIPTION
1533
1534 When the user changes the state of gdb's signal handling via the
1535 "handle" command, this function gets called to see if any change
1536 in the /proc interface is required. It is also called internally
1537 by other /proc interface functions to initialize the state of
1538 the traced signal set.
1539
1540 One thing it does is that signals for which the state is "nostop",
1541 "noprint", and "pass", have their trace bits reset in the pr_trace
1542 field, so that they are no longer traced. This allows them to be
1543 delivered directly to the inferior without the debugger ever being
1544 involved.
1545 */
1546
3950a34e 1547static void
de43d7d0 1548procfs_notice_signals (pid)
952a820e 1549 int pid;
cc221e76
FF
1550{
1551 int signo;
de43d7d0 1552 struct procinfo *pi;
cc221e76 1553
de43d7d0
SG
1554 pi = find_procinfo (pid, 0);
1555
1556 for (signo = 0; signo < NSIG; signo++)
cc221e76 1557 {
67ac9759
JK
1558 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
1559 signal_print_state (target_signal_from_host (signo)) == 0 &&
1560 signal_pass_state (target_signal_from_host (signo)) == 1)
cc221e76 1561 {
de43d7d0 1562 prdelset (&pi->prrun.pr_trace, signo);
cc221e76 1563 }
de43d7d0 1564 else
cc221e76 1565 {
de43d7d0 1566 praddset (&pi->prrun.pr_trace, signo);
cc221e76
FF
1567 }
1568 }
de43d7d0
SG
1569 if (ioctl (pi->fd, PIOCSTRACE, &pi->prrun.pr_trace))
1570 {
1571 print_sys_errmsg ("PIOCSTRACE failed", errno);
1572 }
cc221e76
FF
1573}
1574
1575/*
1576
3fbdd536 1577LOCAL FUNCTION
35f5886e
FF
1578
1579 proc_set_exec_trap -- arrange for exec'd child to halt at startup
1580
1581SYNOPSIS
1582
1583 void proc_set_exec_trap (void)
1584
1585DESCRIPTION
1586
1587 This function is called in the child process when starting up
1588 an inferior, prior to doing the exec of the actual inferior.
1589 It sets the child process's exitset to make exit from the exec
1590 system call an event of interest to stop on, and then simply
1591 returns. The child does the exec, the system call returns, and
1592 the child stops at the first instruction, ready for the gdb
1593 parent process to take control of it.
1594
1595NOTE
1596
1597 We need to use all local variables since the child may be sharing
1598 it's data space with the parent, if vfork was used rather than
1599 fork.
cc221e76
FF
1600
1601 Also note that we want to turn off the inherit-on-fork flag in
1602 the child process so that any grand-children start with all
1603 tracing flags cleared.
35f5886e
FF
1604 */
1605
3fbdd536 1606static void
1ab3bf1b 1607proc_set_exec_trap ()
35f5886e
FF
1608{
1609 sysset_t exitset;
fb63d460 1610 sysset_t entryset;
35f5886e
FF
1611 auto char procname[32];
1612 int fd;
1613
4ed3a9ea 1614 sprintf (procname, PROC_NAME_FMT, getpid ());
35f5886e
FF
1615 if ((fd = open (procname, O_RDWR)) < 0)
1616 {
1617 perror (procname);
199b2450 1618 gdb_flush (gdb_stderr);
35f5886e
FF
1619 _exit (127);
1620 }
1621 premptyset (&exitset);
fb63d460 1622 premptyset (&entryset);
407a8389 1623
2592eef8
PS
1624#ifdef PIOCSSPCACT
1625 /* Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
1626 exits from exec system calls because of the user level loader. */
1627 {
1628 int prfs_flags;
1629
1630 if (ioctl (fd, PIOCGSPCACT, &prfs_flags) < 0)
1631 {
1632 perror (procname);
1633 gdb_flush (gdb_stderr);
1634 _exit (127);
1635 }
1636 prfs_flags |= PRFS_STOPEXEC;
1637 if (ioctl (fd, PIOCSSPCACT, &prfs_flags) < 0)
1638 {
1639 perror (procname);
1640 gdb_flush (gdb_stderr);
1641 _exit (127);
1642 }
1643 }
1644#else
cc221e76
FF
1645 /* GW: Rationale...
1646 Not all systems with /proc have all the exec* syscalls with the same
1647 names. On the SGI, for example, there is no SYS_exec, but there
1648 *is* a SYS_execv. So, we try to account for that. */
1649
407a8389 1650#ifdef SYS_exec
35f5886e 1651 praddset (&exitset, SYS_exec);
407a8389
SG
1652#endif
1653#ifdef SYS_execve
35f5886e 1654 praddset (&exitset, SYS_execve);
407a8389
SG
1655#endif
1656#ifdef SYS_execv
fb63d460 1657 praddset (&exitset, SYS_execv);
407a8389
SG
1658#endif
1659
35f5886e
FF
1660 if (ioctl (fd, PIOCSEXIT, &exitset) < 0)
1661 {
1662 perror (procname);
199b2450 1663 gdb_flush (gdb_stderr);
35f5886e
FF
1664 _exit (127);
1665 }
2592eef8 1666#endif
cc221e76 1667
fb63d460
SG
1668 praddset (&entryset, SYS_exit);
1669
1670 if (ioctl (fd, PIOCSENTRY, &entryset) < 0)
1671 {
1672 perror (procname);
199b2450 1673 gdb_flush (gdb_stderr);
fb63d460
SG
1674 _exit (126);
1675 }
1676
cc221e76
FF
1677 /* Turn off inherit-on-fork flag so that all grand-children of gdb
1678 start with tracing flags cleared. */
1679
5c1c5e67 1680#if defined (PIOCRESET) /* New method */
cc221e76
FF
1681 {
1682 long pr_flags;
1683 pr_flags = PR_FORK;
4ed3a9ea 1684 ioctl (fd, PIOCRESET, &pr_flags);
cc221e76 1685 }
5c1c5e67
FF
1686#else
1687#if defined (PIOCRFORK) /* Original method */
4ed3a9ea 1688 ioctl (fd, PIOCRFORK, NULL);
cc221e76 1689#endif
ec8ceca3
JG
1690#endif
1691
1692 /* Turn on run-on-last-close flag so that this process will not hang
1693 if GDB goes away for some reason. */
1694
1695#if defined (PIOCSET) /* New method */
1696 {
1697 long pr_flags;
1698 pr_flags = PR_RLC;
1699 (void) ioctl (fd, PIOCSET, &pr_flags);
1700 }
1701#else
1702#if defined (PIOCSRLC) /* Original method */
1703 (void) ioctl (fd, PIOCSRLC, 0);
1704#endif
cc221e76 1705#endif
35f5886e
FF
1706}
1707
f8b76e70
FF
1708/*
1709
a39ad5ce
FF
1710GLOBAL FUNCTION
1711
1712 proc_iterate_over_mappings -- call function for every mapped space
1713
1714SYNOPSIS
1715
1716 int proc_iterate_over_mappings (int (*func)())
1717
1718DESCRIPTION
1719
1720 Given a pointer to a function, call that function for every
1721 mapped address space, passing it an open file descriptor for
1722 the file corresponding to that mapped address space (if any)
1723 and the base address of the mapped space. Quit when we hit
1724 the end of the mappings or the function returns nonzero.
1725 */
1726
1727int
1ab3bf1b
JG
1728proc_iterate_over_mappings (func)
1729 int (*func) PARAMS ((int, CORE_ADDR));
a39ad5ce
FF
1730{
1731 int nmap;
1732 int fd;
1733 int funcstat = 0;
1734 struct prmap *prmaps;
1735 struct prmap *prmap;
de43d7d0
SG
1736 struct procinfo *pi;
1737
1738 pi = current_procinfo;
a39ad5ce 1739
de43d7d0 1740 if (ioctl (pi->fd, PIOCNMAP, &nmap) == 0)
a39ad5ce 1741 {
1ab3bf1b 1742 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
de43d7d0 1743 if (ioctl (pi->fd, PIOCMAP, prmaps) == 0)
a39ad5ce
FF
1744 {
1745 for (prmap = prmaps; prmap -> pr_size && funcstat == 0; ++prmap)
1746 {
de43d7d0 1747 fd = proc_address_to_fd (pi, (CORE_ADDR) prmap -> pr_vaddr, 0);
1ab3bf1b 1748 funcstat = (*func) (fd, (CORE_ADDR) prmap -> pr_vaddr);
a39ad5ce
FF
1749 close (fd);
1750 }
1751 }
1752 }
1753 return (funcstat);
1754}
1755
3fbdd536 1756#if 0 /* Currently unused */
a39ad5ce
FF
1757/*
1758
f8b76e70
FF
1759GLOBAL FUNCTION
1760
1761 proc_base_address -- find base address for segment containing address
1762
1763SYNOPSIS
1764
1765 CORE_ADDR proc_base_address (CORE_ADDR addr)
1766
1767DESCRIPTION
1768
1769 Given an address of a location in the inferior, find and return
1770 the base address of the mapped segment containing that address.
1771
1772 This is used for example, by the shared library support code,
1773 where we have the pc value for some location in the shared library
1774 where we are stopped, and need to know the base address of the
1775 segment containing that address.
1776*/
1777
f8b76e70 1778CORE_ADDR
1ab3bf1b 1779proc_base_address (addr)
cc221e76 1780 CORE_ADDR addr;
f8b76e70
FF
1781{
1782 int nmap;
1783 struct prmap *prmaps;
1784 struct prmap *prmap;
1785 CORE_ADDR baseaddr = 0;
de43d7d0 1786 struct procinfo *pi;
f8b76e70 1787
de43d7d0
SG
1788 pi = current_procinfo;
1789
1790 if (ioctl (pi->fd, PIOCNMAP, &nmap) == 0)
f8b76e70 1791 {
1ab3bf1b 1792 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
de43d7d0 1793 if (ioctl (pi->fd, PIOCMAP, prmaps) == 0)
f8b76e70
FF
1794 {
1795 for (prmap = prmaps; prmap -> pr_size; ++prmap)
1796 {
1797 if ((prmap -> pr_vaddr <= (caddr_t) addr) &&
1798 (prmap -> pr_vaddr + prmap -> pr_size > (caddr_t) addr))
1799 {
1800 baseaddr = (CORE_ADDR) prmap -> pr_vaddr;
1801 break;
1802 }
1803 }
1804 }
1805 }
1806 return (baseaddr);
1807}
1808
1ab3bf1b
JG
1809#endif /* 0 */
1810
f8b76e70
FF
1811/*
1812
cc221e76 1813LOCAL FUNCTION
f8b76e70
FF
1814
1815 proc_address_to_fd -- return open fd for file mapped to address
1816
1817SYNOPSIS
1818
de43d7d0 1819 int proc_address_to_fd (struct procinfo *pi, CORE_ADDR addr, complain)
f8b76e70
FF
1820
1821DESCRIPTION
1822
1823 Given an address in the current inferior's address space, use the
1824 /proc interface to find an open file descriptor for the file that
1825 this address was mapped in from. Return -1 if there is no current
1826 inferior. Print a warning message if there is an inferior but
1827 the address corresponds to no file (IE a bogus address).
1828
1829*/
1830
1ab3bf1b 1831static int
de43d7d0
SG
1832proc_address_to_fd (pi, addr, complain)
1833 struct procinfo *pi;
1ab3bf1b
JG
1834 CORE_ADDR addr;
1835 int complain;
f8b76e70
FF
1836{
1837 int fd = -1;
1838
de43d7d0 1839 if ((fd = ioctl (pi->fd, PIOCOPENM, (caddr_t *) &addr)) < 0)
f8b76e70 1840 {
de43d7d0 1841 if (complain)
f8b76e70 1842 {
de43d7d0
SG
1843 print_sys_errmsg (pi->pathname, errno);
1844 warning ("can't find mapped file for address 0x%x", addr);
f8b76e70
FF
1845 }
1846 }
1847 return (fd);
1848}
1849
35f5886e 1850
3fbdd536
JG
1851/* Attach to process PID, then initialize for debugging it
1852 and wait for the trace-trap that results from attaching. */
1853
1854static void
1855procfs_attach (args, from_tty)
1856 char *args;
1857 int from_tty;
1858{
1859 char *exec_file;
1860 int pid;
1861
1862 if (!args)
1863 error_no_arg ("process-id to attach");
1864
1865 pid = atoi (args);
1866
1867 if (pid == getpid()) /* Trying to masturbate? */
1868 error ("I refuse to debug myself!");
1869
1870 if (from_tty)
1871 {
1872 exec_file = (char *) get_exec_file (0);
1873
1874 if (exec_file)
199b2450 1875 printf_unfiltered ("Attaching to program `%s', %s\n", exec_file, target_pid_to_str (pid));
3fbdd536 1876 else
199b2450 1877 printf_unfiltered ("Attaching to %s\n", target_pid_to_str (pid));
3fbdd536 1878
199b2450 1879 gdb_flush (gdb_stdout);
3fbdd536
JG
1880 }
1881
1882 do_attach (pid);
1883 inferior_pid = pid;
1884 push_target (&procfs_ops);
1885}
1886
1887
1888/* Take a program previously attached to and detaches it.
1889 The program resumes execution and will no longer stop
1890 on signals, etc. We'd better not have left any breakpoints
1891 in the program or it'll die when it hits one. For this
1892 to work, it may be necessary for the process to have been
1893 previously attached. It *might* work if the program was
1894 started via the normal ptrace (PTRACE_TRACEME). */
1895
1896static void
1897procfs_detach (args, from_tty)
1898 char *args;
1899 int from_tty;
1900{
1901 int siggnal = 0;
1902
1903 if (from_tty)
1904 {
1905 char *exec_file = get_exec_file (0);
1906 if (exec_file == 0)
1907 exec_file = "";
199b2450 1908 printf_unfiltered ("Detaching from program: %s %s\n",
25286543 1909 exec_file, target_pid_to_str (inferior_pid));
199b2450 1910 gdb_flush (gdb_stdout);
3fbdd536
JG
1911 }
1912 if (args)
1913 siggnal = atoi (args);
1914
1915 do_detach (siggnal);
1916 inferior_pid = 0;
1917 unpush_target (&procfs_ops); /* Pop out of handling an inferior */
1918}
1919
1920/* Get ready to modify the registers array. On machines which store
1921 individual registers, this doesn't need to do anything. On machines
1922 which store all the registers in one fell swoop, this makes sure
1923 that registers contains all the registers from the program being
1924 debugged. */
1925
1926static void
1927procfs_prepare_to_store ()
1928{
1929#ifdef CHILD_PREPARE_TO_STORE
1930 CHILD_PREPARE_TO_STORE ();
1931#endif
1932}
1933
1934/* Print status information about what we're accessing. */
1935
1936static void
1937procfs_files_info (ignore)
1938 struct target_ops *ignore;
1939{
199b2450 1940 printf_unfiltered ("\tUsing the running image of %s %s via /proc.\n",
25286543 1941 attach_flag? "attached": "child", target_pid_to_str (inferior_pid));
3fbdd536
JG
1942}
1943
1944/* ARGSUSED */
1945static void
1946procfs_open (arg, from_tty)
1947 char *arg;
1948 int from_tty;
1949{
1950 error ("Use the \"run\" command to start a Unix child process.");
1951}
35f5886e
FF
1952
1953/*
1954
3fbdd536 1955LOCAL FUNCTION
35f5886e 1956
3fbdd536 1957 do_attach -- attach to an already existing process
35f5886e
FF
1958
1959SYNOPSIS
1960
3fbdd536 1961 int do_attach (int pid)
35f5886e
FF
1962
1963DESCRIPTION
1964
1965 Attach to an already existing process with the specified process
1966 id. If the process is not already stopped, query whether to
1967 stop it or not.
1968
1969NOTES
1970
1971 The option of stopping at attach time is specific to the /proc
1972 versions of gdb. Versions using ptrace force the attachee
ec8ceca3
JG
1973 to stop. (I have changed this version to do so, too. All you
1974 have to do is "continue" to make it go on. -- gnu@cygnus.com)
35f5886e
FF
1975
1976*/
1977
3fbdd536
JG
1978static int
1979do_attach (pid)
1ab3bf1b 1980 int pid;
35f5886e 1981{
ec8ceca3 1982 int result;
de43d7d0
SG
1983 struct procinfo *pi;
1984
1985 pi = (struct procinfo *) xmalloc (sizeof (struct procinfo));
ec8ceca3 1986
de43d7d0 1987 if (!open_proc_file (pid, pi, O_RDWR))
35f5886e 1988 {
de43d7d0
SG
1989 free (pi);
1990 perror_with_name (pi->pathname);
35f5886e
FF
1991 /* NOTREACHED */
1992 }
1993
de43d7d0
SG
1994 /* Add new process to process info list */
1995
1996 pi->next = procinfo_list;
1997 procinfo_list = pi;
1998
1999 add_fd (pi); /* Add to list for poll/select */
2000
35f5886e
FF
2001 /* Get current status of process and if it is not already stopped,
2002 then stop it. Remember whether or not it was stopped when we first
2003 examined it. */
2004
de43d7d0 2005 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
35f5886e 2006 {
de43d7d0
SG
2007 print_sys_errmsg (pi->pathname, errno);
2008 close_proc_file (pi);
35f5886e
FF
2009 error ("PIOCSTATUS failed");
2010 }
de43d7d0 2011 if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
35f5886e 2012 {
de43d7d0 2013 pi->was_stopped = 1;
35f5886e
FF
2014 }
2015 else
2016 {
de43d7d0 2017 pi->was_stopped = 0;
ec8ceca3 2018 if (1 || query ("Process is currently running, stop it? "))
35f5886e 2019 {
ec8ceca3
JG
2020 /* Make it run again when we close it. */
2021#if defined (PIOCSET) /* New method */
2022 {
2023 long pr_flags;
2024 pr_flags = PR_RLC;
de43d7d0 2025 result = ioctl (pi->fd, PIOCSET, &pr_flags);
ec8ceca3
JG
2026 }
2027#else
2028#if defined (PIOCSRLC) /* Original method */
de43d7d0 2029 result = ioctl (pi->fd, PIOCSRLC, 0);
ec8ceca3
JG
2030#endif
2031#endif
2032 if (result < 0)
2033 {
de43d7d0
SG
2034 print_sys_errmsg (pi->pathname, errno);
2035 close_proc_file (pi);
ec8ceca3
JG
2036 error ("PIOCSRLC or PIOCSET failed");
2037 }
de43d7d0 2038 if (ioctl (pi->fd, PIOCSTOP, &pi->prstatus) < 0)
35f5886e 2039 {
de43d7d0
SG
2040 print_sys_errmsg (pi->pathname, errno);
2041 close_proc_file (pi);
35f5886e
FF
2042 error ("PIOCSTOP failed");
2043 }
de43d7d0 2044 pi->nopass_next_sigstop = 1;
d65eee73
FF
2045 }
2046 else
2047 {
199b2450 2048 printf_unfiltered ("Ok, gdb will wait for %s to stop.\n", target_pid_to_str (pid));
35f5886e
FF
2049 }
2050 }
ec8ceca3 2051
35f5886e
FF
2052 /* Remember some things about the inferior that we will, or might, change
2053 so that we can restore them when we detach. */
2054
de43d7d0
SG
2055 ioctl (pi->fd, PIOCGTRACE, &pi->saved_trace);
2056 ioctl (pi->fd, PIOCGHOLD, &pi->saved_sighold);
2057 ioctl (pi->fd, PIOCGFAULT, &pi->saved_fltset);
2058 ioctl (pi->fd, PIOCGENTRY, &pi->saved_entryset);
2059 ioctl (pi->fd, PIOCGEXIT, &pi->saved_exitset);
35f5886e
FF
2060
2061 /* Set up trace and fault sets, as gdb expects them. */
2062
de43d7d0
SG
2063 memset (&pi->prrun, 0, sizeof (pi->prrun));
2064 prfillset (&pi->prrun.pr_trace);
2065 procfs_notice_signals (pid);
2066 prfillset (&pi->prrun.pr_fault);
2067 prdelset (&pi->prrun.pr_fault, FLTPAGE);
2592eef8
PS
2068
2069#ifdef PROCFS_DONT_TRACE_IFAULT
2070 /* Tracing T_IFAULT under Alpha OSF/1 causes a `floating point enable'
2071 fault from which we cannot continue (except by disabling the
2072 tracing). We rely on the delivery of a SIGTRAP signal (which is traced)
2073 for the other T_IFAULT faults if tracing them is disabled. */
2074 prdelset (&pi->prrun.pr_fault, T_IFAULT);
2075#endif
2076
de43d7d0 2077 if (ioctl (pi->fd, PIOCSFAULT, &pi->prrun.pr_fault))
35f5886e 2078 {
f66f459f 2079 print_sys_errmsg ("PIOCSFAULT failed", errno);
35f5886e 2080 }
de43d7d0 2081 if (ioctl (pi->fd, PIOCSTRACE, &pi->prrun.pr_trace))
35f5886e 2082 {
f66f459f 2083 print_sys_errmsg ("PIOCSTRACE failed", errno);
35f5886e
FF
2084 }
2085 attach_flag = 1;
2086 return (pid);
2087}
2088
2089/*
2090
3fbdd536 2091LOCAL FUNCTION
35f5886e 2092
3fbdd536 2093 do_detach -- detach from an attached-to process
35f5886e
FF
2094
2095SYNOPSIS
2096
3fbdd536 2097 void do_detach (int signal)
35f5886e
FF
2098
2099DESCRIPTION
2100
2101 Detach from the current attachee.
2102
2103 If signal is non-zero, the attachee is started running again and sent
2104 the specified signal.
2105
2106 If signal is zero and the attachee was not already stopped when we
2107 attached to it, then we make it runnable again when we detach.
2108
2109 Otherwise, we query whether or not to make the attachee runnable
2110 again, since we may simply want to leave it in the state it was in
2111 when we attached.
2112
2113 We report any problems, but do not consider them errors, since we
2114 MUST detach even if some things don't seem to go right. This may not
2115 be the ideal situation. (FIXME).
2116 */
2117
3fbdd536
JG
2118static void
2119do_detach (signal)
1ab3bf1b 2120 int signal;
35f5886e 2121{
ec8ceca3 2122 int result;
de43d7d0
SG
2123 struct procinfo *pi;
2124
2125 pi = current_procinfo;
ec8ceca3 2126
35f5886e
FF
2127 if (signal)
2128 {
de43d7d0 2129 set_proc_siginfo (pi, signal);
35f5886e 2130 }
de43d7d0 2131 if (ioctl (pi->fd, PIOCSEXIT, &pi->saved_exitset) < 0)
35f5886e 2132 {
de43d7d0 2133 print_sys_errmsg (pi->pathname, errno);
199b2450 2134 printf_unfiltered ("PIOCSEXIT failed.\n");
35f5886e 2135 }
de43d7d0 2136 if (ioctl (pi->fd, PIOCSENTRY, &pi->saved_entryset) < 0)
35f5886e 2137 {
de43d7d0 2138 print_sys_errmsg (pi->pathname, errno);
199b2450 2139 printf_unfiltered ("PIOCSENTRY failed.\n");
35f5886e 2140 }
de43d7d0 2141 if (ioctl (pi->fd, PIOCSTRACE, &pi->saved_trace) < 0)
35f5886e 2142 {
de43d7d0 2143 print_sys_errmsg (pi->pathname, errno);
199b2450 2144 printf_unfiltered ("PIOCSTRACE failed.\n");
35f5886e 2145 }
de43d7d0 2146 if (ioctl (pi->fd, PIOCSHOLD, &pi->saved_sighold) < 0)
cc221e76 2147 {
de43d7d0 2148 print_sys_errmsg (pi->pathname, errno);
199b2450 2149 printf_unfiltered ("PIOSCHOLD failed.\n");
cc221e76 2150 }
de43d7d0 2151 if (ioctl (pi->fd, PIOCSFAULT, &pi->saved_fltset) < 0)
35f5886e 2152 {
de43d7d0 2153 print_sys_errmsg (pi->pathname, errno);
199b2450 2154 printf_unfiltered ("PIOCSFAULT failed.\n");
35f5886e 2155 }
de43d7d0 2156 if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0)
35f5886e 2157 {
de43d7d0 2158 print_sys_errmsg (pi->pathname, errno);
199b2450 2159 printf_unfiltered ("PIOCSTATUS failed.\n");
35f5886e
FF
2160 }
2161 else
2162 {
de43d7d0 2163 if (signal || (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
35f5886e 2164 {
de43d7d0 2165 if (signal || !pi->was_stopped ||
35f5886e
FF
2166 query ("Was stopped when attached, make it runnable again? "))
2167 {
2592eef8
PS
2168 /* Clear any pending signal if we want to detach without
2169 a signal. */
2170 if (signal == 0)
2171 set_proc_siginfo (pi, signal);
2172
ec8ceca3 2173 /* Clear any fault that might have stopped it. */
de43d7d0 2174 if (ioctl (pi->fd, PIOCCFAULT, 0))
eca4a350
SG
2175 {
2176 print_sys_errmsg (pi->pathname, errno);
199b2450 2177 printf_unfiltered ("PIOCCFAULT failed.\n");
eca4a350 2178 }
ec8ceca3
JG
2179
2180 /* Make it run again when we close it. */
eca4a350 2181#if defined (PIOCSET) /* New method */
ec8ceca3 2182 {
eca4a350
SG
2183 long pr_flags;
2184 pr_flags = PR_RLC;
2185 result = ioctl (pi->fd, PIOCSET, &pr_flags);
ec8ceca3
JG
2186 }
2187#else
eca4a350 2188#if defined (PIOCSRLC) /* Original method */
de43d7d0 2189 result = ioctl (pi->fd, PIOCSRLC, 0);
ec8ceca3
JG
2190#endif
2191#endif
2192 if (result)
35f5886e 2193 {
de43d7d0 2194 print_sys_errmsg (pi->pathname, errno);
199b2450 2195 printf_unfiltered ("PIOCSRLC or PIOCSET failed.\n");
35f5886e
FF
2196 }
2197 }
2198 }
2199 }
de43d7d0 2200 close_proc_file (pi);
35f5886e
FF
2201 attach_flag = 0;
2202}
2203
45dc9be3
JK
2204/* emulate wait() as much as possible.
2205 Wait for child to do something. Return pid of child, or -1 in case
2206 of error; store status in *OURSTATUS.
2207
2208 Not sure why we can't
2209 just use wait(), but it seems to have problems when applied to a
2210 process being controlled with the /proc interface.
2211
2212 We have a race problem here with no obvious solution. We need to let
2213 the inferior run until it stops on an event of interest, which means
2214 that we need to use the PIOCWSTOP ioctl. However, we cannot use this
2215 ioctl if the process is already stopped on something that is not an
2216 event of interest, or the call will hang indefinitely. Thus we first
2217 use PIOCSTATUS to see if the process is not stopped. If not, then we
2218 use PIOCWSTOP. But during the window between the two, if the process
2219 stops for any reason that is not an event of interest (such as a job
2220 control signal) then gdb will hang. One possible workaround is to set
2221 an alarm to wake up every minute of so and check to see if the process
2222 is still running, and if so, then reissue the PIOCWSTOP. But this is
2223 a real kludge, so has not been implemented. FIXME: investigate
2224 alternatives.
2225
2226 FIXME: Investigate why wait() seems to have problems with programs
2227 being control by /proc routines. */
35f5886e 2228
3fbdd536 2229static int
45dc9be3 2230procfs_wait (pid, ourstatus)
de43d7d0 2231 int pid;
67ac9759 2232 struct target_waitstatus *ourstatus;
35f5886e
FF
2233{
2234 short what;
2235 short why;
2236 int statval = 0;
2237 int checkerr = 0;
2238 int rtnval = -1;
de43d7d0
SG
2239 struct procinfo *pi;
2240
2241 if (pid != -1) /* Non-specific process? */
2242 pi = NULL;
2243 else
2244 for (pi = procinfo_list; pi; pi = pi->next)
2245 if (pi->had_event)
2246 break;
2247
de43d7d0 2248 if (!pi)
eca4a350
SG
2249 {
2250 wait_again:
2251
2252 pi = wait_fd ();
2253 }
de43d7d0
SG
2254
2255 if (pid != -1)
2256 for (pi = procinfo_list; pi; pi = pi->next)
2257 if (pi->pid == pid && pi->had_event)
2258 break;
2259
2260 if (!pi && !checkerr)
2261 goto wait_again;
2262
2263 if (!checkerr && !(pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
2264 {
2265 if (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0)
2266 {
2267 checkerr++;
2268 }
35f5886e
FF
2269 }
2270 if (checkerr)
2271 {
2272 if (errno == ENOENT)
2273 {
2274 rtnval = wait (&statval);
2275 if (rtnval != inferior_pid)
2276 {
de43d7d0 2277 print_sys_errmsg (pi->pathname, errno);
35f5886e
FF
2278 error ("PIOCWSTOP, wait failed, returned %d", rtnval);
2279 /* NOTREACHED */
2280 }
2281 }
2282 else
2283 {
de43d7d0 2284 print_sys_errmsg (pi->pathname, errno);
35f5886e
FF
2285 error ("PIOCSTATUS or PIOCWSTOP failed.");
2286 /* NOTREACHED */
2287 }
2288 }
de43d7d0 2289 else if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
35f5886e 2290 {
de43d7d0
SG
2291 rtnval = pi->prstatus.pr_pid;
2292 why = pi->prstatus.pr_why;
2293 what = pi->prstatus.pr_what;
2294
2295 switch (why)
35f5886e 2296 {
de43d7d0 2297 case PR_SIGNALLED:
35f5886e 2298 statval = (what << 8) | 0177;
fb63d460
SG
2299 break;
2300 case PR_SYSENTRY:
2301 if (what != SYS_exit)
2302 error ("PR_SYSENTRY, unknown system call %d", what);
2303
2304 pi->prrun.pr_flags = PRCFAULT;
2305
2306 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
2307 perror_with_name (pi->pathname);
2308
2309 rtnval = wait (&statval);
2310
de43d7d0
SG
2311 break;
2312 case PR_SYSEXIT:
2313 switch (what)
2314 {
407a8389 2315#ifdef SYS_exec
de43d7d0 2316 case SYS_exec:
407a8389
SG
2317#endif
2318#ifdef SYS_execve
de43d7d0 2319 case SYS_execve:
407a8389
SG
2320#endif
2321#ifdef SYS_execv
de43d7d0 2322 case SYS_execv:
407a8389 2323#endif
de43d7d0
SG
2324 statval = (SIGTRAP << 8) | 0177;
2325 break;
2326#ifdef SYS_sproc
2327 case SYS_sproc:
2328/* We've just detected the completion of an sproc system call. Now we need to
2329 setup a procinfo struct for this thread, and notify the thread system of the
2330 new arrival. */
2331
2332/* If sproc failed, then nothing interesting happened. Continue the process and
2333 go back to sleep. */
2334
2335 if (pi->prstatus.pr_errno != 0)
2336 {
2337 pi->prrun.pr_flags &= PRSTEP;
2338 pi->prrun.pr_flags |= PRCFAULT;
2339
2340 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
2341 perror_with_name (pi->pathname);
2342
2343 goto wait_again;
2344 }
2345
2346/* At this point, the new thread is stopped at it's first instruction, and
2347 the parent is stopped at the exit from sproc. */
2348
2349/* Notify the caller of the arrival of a new thread. */
2350 create_procinfo (pi->prstatus.pr_rval1);
2351
2352 rtnval = pi->prstatus.pr_rval1;
2353 statval = (SIGTRAP << 8) | 0177;
2354
2355 break;
eca4a350
SG
2356 case SYS_fork:
2357#ifdef SYS_vfork
2358 case SYS_vfork:
2359#endif
2360/* At this point, we've detected the completion of a fork (or vfork) call in
2361 our child. The grandchild is also stopped because we set inherit-on-fork
2362 earlier. (Note that nobody has the grandchilds' /proc file open at this
2363 point.) We will release the grandchild from the debugger by opening it's
2364 /proc file and then closing it. Since run-on-last-close is set, the
2365 grandchild continues on its' merry way. */
2366
2367 {
2368 struct procinfo *pitemp;
2369
2370 pitemp = create_procinfo (pi->prstatus.pr_rval1);
2371 if (pitemp)
2372 close_proc_file (pitemp);
2373
2374 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
2375 perror_with_name (pi->pathname);
2376 }
2377 goto wait_again;
de43d7d0
SG
2378#endif /* SYS_sproc */
2379
2380 default:
2381 error ("PIOCSTATUS (PR_SYSEXIT): Unknown system call %d", what);
2382 }
2383 break;
2384 case PR_REQUESTED:
35f5886e 2385 statval = (SIGSTOP << 8) | 0177;
de43d7d0
SG
2386 break;
2387 case PR_JOBCONTROL:
35f5886e 2388 statval = (what << 8) | 0177;
de43d7d0
SG
2389 break;
2390 case PR_FAULTED:
35f5886e
FF
2391 switch (what)
2392 {
e6b8a171 2393#ifdef FLTWATCH
999dd04b 2394 case FLTWATCH:
e6b8a171
JL
2395 statval = (SIGTRAP << 8) | 0177;
2396 break;
2397#endif
2398#ifdef FLTKWATCH
999dd04b 2399 case FLTKWATCH:
35f5886e
FF
2400 statval = (SIGTRAP << 8) | 0177;
2401 break;
e6b8a171 2402#endif
3f5e2fb5
JK
2403#ifndef FAULTED_USE_SIGINFO
2404 /* Irix, contrary to the documentation, fills in 0 for si_signo.
2405 Solaris fills in si_signo. I'm not sure about others. */
2406 case FLTPRIV:
2407 case FLTILL:
2408 statval = (SIGILL << 8) | 0177;
2409 break;
2410 case FLTBPT:
2411 case FLTTRACE:
2412 statval = (SIGTRAP << 8) | 0177;
2413 break;
2414 case FLTSTACK:
2415 case FLTACCESS:
2416 case FLTBOUNDS:
2417 statval = (SIGSEGV << 8) | 0177;
2418 break;
2419 case FLTIOVF:
2420 case FLTIZDIV:
2421 case FLTFPE:
2422 statval = (SIGFPE << 8) | 0177;
2423 break;
2424 case FLTPAGE: /* Recoverable page fault */
2425#endif /* not FAULTED_USE_SIGINFO */
35f5886e 2426 default:
890634ed
JK
2427 /* Use the signal which the kernel assigns. This is better than
2428 trying to second-guess it from the fault. In fact, I suspect
2429 that FLTACCESS can be either SIGSEGV or SIGBUS. */
2430 statval = ((pi->prstatus.pr_info.si_signo) << 8) | 0177;
2431 break;
35f5886e 2432 }
de43d7d0
SG
2433 break;
2434 default:
35f5886e 2435 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
35f5886e 2436 }
de43d7d0
SG
2437/* Stop all the other threads when any of them stops. */
2438
2439 {
2440 struct procinfo *procinfo;
2441
2442 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
2443 {
2444 if (!procinfo->had_event)
2445 if (ioctl (procinfo->fd, PIOCSTOP, &procinfo->prstatus) < 0)
2446 {
2447 print_sys_errmsg (procinfo->pathname, errno);
2448 error ("PIOCSTOP failed");
2449 }
2450 }
2451 }
35f5886e
FF
2452 }
2453 else
2454 {
2455 error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x",
de43d7d0 2456 pi->prstatus.pr_flags);
35f5886e 2457 }
3fbdd536 2458
67ac9759 2459 store_waitstatus (ourstatus, statval);
3fbdd536
JG
2460
2461 if (rtnval == -1) /* No more children to wait for */
2462 {
199b2450 2463 fprintf_unfiltered (gdb_stderr, "Child process unexpectedly missing.\n");
67ac9759
JK
2464 /* Claim it exited with unknown signal. */
2465 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
2466 ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN;
3fbdd536
JG
2467 return rtnval;
2468 }
2469
de43d7d0 2470 pi->had_event = 0; /* Indicate that we've seen this one */
35f5886e
FF
2471 return (rtnval);
2472}
2473
2474/*
2475
6b801388
FF
2476LOCAL FUNCTION
2477
2478 set_proc_siginfo - set a process's current signal info
2479
2480SYNOPSIS
2481
2482 void set_proc_siginfo (struct procinfo *pip, int signo);
2483
2484DESCRIPTION
2485
2486 Given a pointer to a process info struct in PIP and a signal number
2487 in SIGNO, set the process's current signal and its associated signal
2488 information. The signal will be delivered to the process immediately
2489 after execution is resumed, even if it is being held. In addition,
2490 this particular delivery will not cause another PR_SIGNALLED stop
2491 even if the signal is being traced.
2492
2493 If we are not delivering the same signal that the prstatus siginfo
2494 struct contains information about, then synthesize a siginfo struct
2495 to match the signal we are doing to deliver, make it of the type
2496 "generated by a user process", and send this synthesized copy. When
2497 used to set the inferior's signal state, this will be required if we
2498 are not currently stopped because of a traced signal, or if we decide
2499 to continue with a different signal.
2500
2501 Note that when continuing the inferior from a stop due to receipt
2502 of a traced signal, we either have set PRCSIG to clear the existing
2503 signal, or we have to call this function to do a PIOCSSIG with either
2504 the existing siginfo struct from pr_info, or one we have synthesized
2505 appropriately for the signal we want to deliver. Otherwise if the
2506 signal is still being traced, the inferior will immediately stop
2507 again.
2508
2509 See siginfo(5) for more details.
2510*/
2511
2512static void
2513set_proc_siginfo (pip, signo)
cc221e76
FF
2514 struct procinfo *pip;
2515 int signo;
6b801388
FF
2516{
2517 struct siginfo newsiginfo;
2518 struct siginfo *sip;
2519
2592eef8
PS
2520#ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2521 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2522 receives a PIOCSSSIG with a signal identical to the current signal,
2523 it messes up the current signal. Work around the kernel bug. */
2524 if (signo == pip -> prstatus.pr_cursig)
2525 return;
2526#endif
2527
de43d7d0 2528 if (signo == pip -> prstatus.pr_info.si_signo)
6b801388 2529 {
de43d7d0
SG
2530 sip = &pip -> prstatus.pr_info;
2531 }
2532 else
2533 {
2534 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
2535 sip = &newsiginfo;
2536 sip -> si_signo = signo;
2537 sip -> si_code = 0;
2538 sip -> si_errno = 0;
2539 sip -> si_pid = getpid ();
2540 sip -> si_uid = getuid ();
2541 }
2542 if (ioctl (pip -> fd, PIOCSSIG, sip) < 0)
2543 {
2544 print_sys_errmsg (pip -> pathname, errno);
2545 warning ("PIOCSSIG failed");
6b801388
FF
2546 }
2547}
2548
25286543 2549/* Resume execution of process PID. If STEP is nozero, then
59ba57da
JK
2550 just single step it. If SIGNAL is nonzero, restart it with that
2551 signal activated. */
35f5886e 2552
3fbdd536 2553static void
25286543
SG
2554procfs_resume (pid, step, signo)
2555 int pid;
1ab3bf1b 2556 int step;
67ac9759 2557 enum target_signal signo;
35f5886e 2558{
59ba57da 2559 int signal_to_pass;
de43d7d0
SG
2560 struct procinfo *pi, *procinfo;
2561
2562 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
59ba57da 2563
35f5886e 2564 errno = 0;
de43d7d0 2565 pi->prrun.pr_flags = PRSTRACE | PRSFAULT | PRCFAULT;
99fd9e3e 2566
59ba57da
JK
2567#if 0
2568 /* It should not be necessary. If the user explicitly changes the value,
2569 value_assign calls write_register_bytes, which writes it. */
2570/* It may not be absolutely necessary to specify the PC value for
2571 restarting, but to be safe we use the value that gdb considers
2572 to be current. One case where this might be necessary is if the
2573 user explicitly changes the PC value that gdb considers to be
2574 current. FIXME: Investigate if this is necessary or not. */
2575
c3192172 2576#ifdef PRSVADDR_BROKEN
99fd9e3e
SG
2577/* Can't do this under Solaris running on a Sparc, as there seems to be no
2578 place to put nPC. In fact, if you use this, nPC seems to be set to some
2579 random garbage. We have to rely on the fact that PC and nPC have been
2580 written previously via PIOCSREG during a register flush. */
2581
de43d7d0
SG
2582 pi->prrun.pr_vaddr = (caddr_t) *(int *) &registers[REGISTER_BYTE (PC_REGNUM)];
2583 pi->prrun.pr_flags != PRSVADDR;
99fd9e3e 2584#endif
59ba57da
JK
2585#endif
2586
67ac9759 2587 if (signo == TARGET_SIGNAL_STOP && pi->nopass_next_sigstop)
59ba57da
JK
2588 /* When attaching to a child process, if we forced it to stop with
2589 a PIOCSTOP, then we will have set the nopass_next_sigstop flag.
2590 Upon resuming the first time after such a stop, we explicitly
2591 inhibit sending it another SIGSTOP, which would be the normal
2592 result of default signal handling. One potential drawback to
2593 this is that we will also ignore any attempt to by the user
2594 to explicitly continue after the attach with a SIGSTOP. Ultimately
2595 this problem should be dealt with by making the routines that
2596 deal with the inferior a little smarter, and possibly even allow
2597 an inferior to continue running at the same time as gdb. (FIXME?) */
2598 signal_to_pass = 0;
67ac9759 2599 else if (signo == TARGET_SIGNAL_TSTP
de43d7d0
SG
2600 && pi->prstatus.pr_cursig == SIGTSTP
2601 && pi->prstatus.pr_action.sa_handler == SIG_DFL)
59ba57da
JK
2602
2603 /* We are about to pass the inferior a SIGTSTP whose action is
2604 SIG_DFL. The SIG_DFL action for a SIGTSTP is to stop
2605 (notifying the parent via wait()), and then keep going from the
2606 same place when the parent is ready for you to keep going. So
2607 under the debugger, it should do nothing (as if the program had
2608 been stopped and then later resumed. Under ptrace, this
2609 happens for us, but under /proc, the system obligingly stops
2610 the process, and wait_for_inferior would have no way of
2611 distinguishing that type of stop (which indicates that we
2612 should just start it again), with a stop due to the pr_trace
2613 field of the prrun_t struct.
2614
2615 Note that if the SIGTSTP is being caught, we *do* need to pass it,
2616 because the handler needs to get executed. */
2617 signal_to_pass = 0;
2618 else
67ac9759 2619 signal_to_pass = target_signal_to_host (signo);
99fd9e3e 2620
59ba57da 2621 if (signal_to_pass)
35f5886e 2622 {
de43d7d0 2623 set_proc_siginfo (pi, signal_to_pass);
35f5886e
FF
2624 }
2625 else
2626 {
de43d7d0 2627 pi->prrun.pr_flags |= PRCSIG;
35f5886e 2628 }
de43d7d0 2629 pi->nopass_next_sigstop = 0;
35f5886e
FF
2630 if (step)
2631 {
de43d7d0 2632 pi->prrun.pr_flags |= PRSTEP;
35f5886e 2633 }
de43d7d0 2634 if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0)
35f5886e 2635 {
de43d7d0 2636 perror_with_name (pi->pathname);
35f5886e
FF
2637 /* NOTREACHED */
2638 }
de43d7d0
SG
2639
2640 pi->had_event = 0;
2641
2642 /* Continue all the other threads that haven't had an event of
2643 interest. */
2644
2645 if (pid == -1)
2646 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
2647 {
2648 if (pi != procinfo && !procinfo->had_event)
2649 {
2650 procinfo->prrun.pr_flags &= PRSTEP;
2651 procinfo->prrun.pr_flags |= PRCFAULT | PRCSIG;
2652 ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus);
2653 if (ioctl (procinfo->fd, PIOCRUN, &procinfo->prrun) < 0)
2654 {
2655 if (ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus) < 0)
2656 {
199b2450 2657 fprintf_unfiltered(gdb_stderr, "PIOCSTATUS failed, errno=%d\n", errno);
de43d7d0
SG
2658 }
2659 print_sys_errmsg (procinfo->pathname, errno);
2660 error ("PIOCRUN failed");
2661 }
2662 ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus);
2663 }
2664 }
35f5886e
FF
2665}
2666
2667/*
2668
3fbdd536 2669LOCAL FUNCTION
35f5886e 2670
3fbdd536 2671 procfs_fetch_registers -- fetch current registers from inferior
35f5886e
FF
2672
2673SYNOPSIS
2674
3fbdd536 2675 void procfs_fetch_registers (int regno)
35f5886e
FF
2676
2677DESCRIPTION
2678
2679 Read the current values of the inferior's registers, both the
2680 general register set and floating point registers (if supported)
2681 and update gdb's idea of their current values.
2682
2683*/
2684
3fbdd536
JG
2685static void
2686procfs_fetch_registers (regno)
1ab3bf1b 2687 int regno;
35f5886e 2688{
de43d7d0
SG
2689 struct procinfo *pi;
2690
2691 pi = current_procinfo;
2692
2693 if (ioctl (pi->fd, PIOCGREG, &pi->gregset) != -1)
35f5886e 2694 {
de43d7d0 2695 supply_gregset (&pi->gregset);
35f5886e
FF
2696 }
2697#if defined (FP0_REGNUM)
de43d7d0 2698 if (ioctl (pi->fd, PIOCGFPREG, &pi->fpregset) != -1)
35f5886e 2699 {
de43d7d0 2700 supply_fpregset (&pi->fpregset);
35f5886e
FF
2701 }
2702#endif
2703}
2704
fb182850
FF
2705/*
2706
35f5886e
FF
2707LOCAL FUNCTION
2708
de43d7d0
SG
2709 proc_init_failed - called whenever /proc access initialization
2710fails
35f5886e
FF
2711
2712SYNOPSIS
2713
de43d7d0 2714 static void proc_init_failed (struct procinfo *pi, char *why)
35f5886e
FF
2715
2716DESCRIPTION
2717
2718 This function is called whenever initialization of access to a /proc
2719 entry fails. It prints a suitable error message, does some cleanup,
2720 and then invokes the standard error processing routine which dumps
2721 us back into the command loop.
2722 */
2723
2724static void
de43d7d0
SG
2725proc_init_failed (pi, why)
2726 struct procinfo *pi;
1ab3bf1b 2727 char *why;
35f5886e 2728{
de43d7d0
SG
2729 print_sys_errmsg (pi->pathname, errno);
2730 kill (pi->pid, SIGKILL);
2731 close_proc_file (pi);
35f5886e
FF
2732 error (why);
2733 /* NOTREACHED */
2734}
2735
2736/*
2737
2738LOCAL FUNCTION
2739
2740 close_proc_file - close any currently open /proc entry
2741
2742SYNOPSIS
2743
a39ad5ce 2744 static void close_proc_file (struct procinfo *pip)
35f5886e
FF
2745
2746DESCRIPTION
2747
2748 Close any currently open /proc entry and mark the process information
2749 entry as invalid. In order to ensure that we don't try to reuse any
2750 stale information, the pid, fd, and pathnames are explicitly
2751 invalidated, which may be overkill.
2752
2753 */
2754
2755static void
1ab3bf1b
JG
2756close_proc_file (pip)
2757 struct procinfo *pip;
35f5886e 2758{
de43d7d0
SG
2759 struct procinfo *procinfo;
2760
2761 remove_fd (pip); /* Remove fd from poll/select list */
2762
2763 close (pip -> fd);
2764
2765 free (pip -> pathname);
2766
2767 /* Unlink pip from the procinfo chain. Note pip might not be on the list. */
2768
2769 if (procinfo_list == pip)
2770 procinfo_list = pip->next;
2771 else
2772 for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next)
2773 if (procinfo->next == pip)
2774 procinfo->next = pip->next;
2775
2776 free (pip);
35f5886e
FF
2777}
2778
2779/*
2780
2781LOCAL FUNCTION
2782
2783 open_proc_file - open a /proc entry for a given process id
2784
2785SYNOPSIS
2786
ec8ceca3 2787 static int open_proc_file (int pid, struct procinfo *pip, int mode)
35f5886e
FF
2788
2789DESCRIPTION
2790
ec8ceca3
JG
2791 Given a process id and a mode, close the existing open /proc
2792 entry (if any) and open one for the new process id, in the
2793 specified mode. Once it is open, then mark the local process
2794 information structure as valid, which guarantees that the pid,
2795 fd, and pathname fields match an open /proc entry. Returns
2796 zero if the open fails, nonzero otherwise.
35f5886e
FF
2797
2798 Note that the pathname is left intact, even when the open fails,
2799 so that callers can use it to construct meaningful error messages
2800 rather than just "file open failed".
2801 */
2802
2803static int
ec8ceca3 2804open_proc_file (pid, pip, mode)
1ab3bf1b
JG
2805 int pid;
2806 struct procinfo *pip;
ec8ceca3 2807 int mode;
35f5886e 2808{
de43d7d0
SG
2809 pip -> next = NULL;
2810 pip -> had_event = 0;
2811 pip -> pathname = xmalloc (32);
2812 pip -> pid = pid;
2813
a39ad5ce 2814 sprintf (pip -> pathname, PROC_NAME_FMT, pid);
de43d7d0
SG
2815 if ((pip -> fd = open (pip -> pathname, mode)) < 0)
2816 return 0;
2817
2818 return 1;
a39ad5ce
FF
2819}
2820
f66f459f 2821static char *
1ab3bf1b
JG
2822mappingflags (flags)
2823 long flags;
a39ad5ce 2824{
5c1c5e67 2825 static char asciiflags[8];
a39ad5ce 2826
5c1c5e67
FF
2827 strcpy (asciiflags, "-------");
2828#if defined (MA_PHYS)
2829 if (flags & MA_PHYS) asciiflags[0] = 'd';
2830#endif
2831 if (flags & MA_STACK) asciiflags[1] = 's';
2832 if (flags & MA_BREAK) asciiflags[2] = 'b';
2833 if (flags & MA_SHARED) asciiflags[3] = 's';
2834 if (flags & MA_READ) asciiflags[4] = 'r';
2835 if (flags & MA_WRITE) asciiflags[5] = 'w';
2836 if (flags & MA_EXEC) asciiflags[6] = 'x';
a39ad5ce
FF
2837 return (asciiflags);
2838}
2839
2840static void
cc221e76
FF
2841info_proc_flags (pip, summary)
2842 struct procinfo *pip;
2843 int summary;
2844{
2845 struct trans *transp;
2846
2847 printf_filtered ("%-32s", "Process status flags:");
2848 if (!summary)
2849 {
2850 printf_filtered ("\n\n");
2851 }
2852 for (transp = pr_flag_table; transp -> name != NULL; transp++)
2853 {
2854 if (pip -> prstatus.pr_flags & transp -> value)
2855 {
2856 if (summary)
2857 {
2858 printf_filtered ("%s ", transp -> name);
2859 }
2860 else
2861 {
2862 printf_filtered ("\t%-16s %s.\n", transp -> name, transp -> desc);
2863 }
2864 }
2865 }
2866 printf_filtered ("\n");
2867}
2868
2869static void
2870info_proc_stop (pip, summary)
2871 struct procinfo *pip;
2872 int summary;
2873{
2874 struct trans *transp;
2875 int why;
2876 int what;
2877
2878 why = pip -> prstatus.pr_why;
2879 what = pip -> prstatus.pr_what;
2880
2881 if (pip -> prstatus.pr_flags & PR_STOPPED)
2882 {
2883 printf_filtered ("%-32s", "Reason for stopping:");
2884 if (!summary)
2885 {
2886 printf_filtered ("\n\n");
2887 }
2888 for (transp = pr_why_table; transp -> name != NULL; transp++)
2889 {
2890 if (why == transp -> value)
2891 {
2892 if (summary)
2893 {
2894 printf_filtered ("%s ", transp -> name);
2895 }
2896 else
2897 {
2898 printf_filtered ("\t%-16s %s.\n",
2899 transp -> name, transp -> desc);
2900 }
2901 break;
2902 }
2903 }
2904
2905 /* Use the pr_why field to determine what the pr_what field means, and
2906 print more information. */
2907
2908 switch (why)
2909 {
2910 case PR_REQUESTED:
2911 /* pr_what is unused for this case */
2912 break;
2913 case PR_JOBCONTROL:
2914 case PR_SIGNALLED:
2915 if (summary)
2916 {
2917 printf_filtered ("%s ", signalname (what));
2918 }
2919 else
2920 {
2921 printf_filtered ("\t%-16s %s.\n", signalname (what),
4ace50a5 2922 safe_strsignal (what));
cc221e76
FF
2923 }
2924 break;
2925 case PR_SYSENTRY:
2926 if (summary)
2927 {
2928 printf_filtered ("%s ", syscallname (what));
2929 }
2930 else
2931 {
2932 printf_filtered ("\t%-16s %s.\n", syscallname (what),
2933 "Entered this system call");
2934 }
2935 break;
2936 case PR_SYSEXIT:
2937 if (summary)
2938 {
2939 printf_filtered ("%s ", syscallname (what));
2940 }
2941 else
2942 {
2943 printf_filtered ("\t%-16s %s.\n", syscallname (what),
2944 "Returned from this system call");
2945 }
2946 break;
2947 case PR_FAULTED:
2948 if (summary)
2949 {
2950 printf_filtered ("%s ",
2951 lookupname (faults_table, what, "fault"));
2952 }
2953 else
2954 {
2955 printf_filtered ("\t%-16s %s.\n",
2956 lookupname (faults_table, what, "fault"),
2957 lookupdesc (faults_table, what));
2958 }
2959 break;
2960 }
2961 printf_filtered ("\n");
2962 }
2963}
2964
2965static void
2966info_proc_siginfo (pip, summary)
2967 struct procinfo *pip;
2968 int summary;
2969{
2970 struct siginfo *sip;
2971
2972 if ((pip -> prstatus.pr_flags & PR_STOPPED) &&
2973 (pip -> prstatus.pr_why == PR_SIGNALLED ||
2974 pip -> prstatus.pr_why == PR_FAULTED))
2975 {
2976 printf_filtered ("%-32s", "Additional signal/fault info:");
2977 sip = &pip -> prstatus.pr_info;
2978 if (summary)
2979 {
2980 printf_filtered ("%s ", signalname (sip -> si_signo));
2981 if (sip -> si_errno > 0)
2982 {
4ace50a5 2983 printf_filtered ("%s ", errnoname (sip -> si_errno));
cc221e76
FF
2984 }
2985 if (sip -> si_code <= 0)
2986 {
25286543
SG
2987 printf_filtered ("sent by %s, uid %d ",
2988 target_pid_to_str (sip -> si_pid),
cc221e76
FF
2989 sip -> si_uid);
2990 }
2991 else
2992 {
2993 printf_filtered ("%s ", sigcodename (sip));
2994 if ((sip -> si_signo == SIGILL) ||
2995 (sip -> si_signo == SIGFPE) ||
2996 (sip -> si_signo == SIGSEGV) ||
2997 (sip -> si_signo == SIGBUS))
2998 {
2999 printf_filtered ("addr=%#x ", sip -> si_addr);
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 {
3038 printf_filtered ("\t%-16#x %s.\n", sip -> si_addr,
3039 "Address of faulting instruction");
3040 }
3041 else if ((sip -> si_signo == SIGSEGV) ||
3042 (sip -> si_signo == SIGBUS))
3043 {
3044 printf_filtered ("\t%-16#x %s.\n", sip -> si_addr,
3045 "Address of faulting memory reference");
3046 }
3047 else if ((sip -> si_signo == SIGCHLD))
3048 {
25286543 3049 printf_filtered ("\t%-16u %s.\n", sip -> si_pid, /* XXX need target_pid_to_str() */
cc221e76
FF
3050 "Child process ID");
3051 printf_filtered ("\t%-16u %s.\n", sip -> si_status,
3052 "Child process exit value or signal");
3053 }
3054 else if ((sip -> si_signo == SIGPOLL))
3055 {
3056 printf_filtered ("\t%-16u %s.\n", sip -> si_band,
3057 "Band event for POLL_{IN,OUT,MSG}");
3058 }
3059 }
3060 }
3061 printf_filtered ("\n");
3062 }
3063}
3064
3065static void
3066info_proc_syscalls (pip, summary)
3067 struct procinfo *pip;
3068 int summary;
3069{
3070 int syscallnum;
3071
3072 if (!summary)
3073 {
3074
3075#if 0 /* FIXME: Needs to use gdb-wide configured info about system calls. */
3076 if (pip -> prstatus.pr_flags & PR_ASLEEP)
3077 {
3078 int syscallnum = pip -> prstatus.pr_reg[R_D0];
3079 if (summary)
3080 {
3081 printf_filtered ("%-32s", "Sleeping in system call:");
3082 printf_filtered ("%s", syscallname (syscallnum));
3083 }
3084 else
3085 {
3086 printf_filtered ("Sleeping in system call '%s'.\n",
3087 syscallname (syscallnum));
3088 }
3089 }
3090#endif
3091
3092 if (ioctl (pip -> fd, PIOCGENTRY, &pip -> entryset) < 0)
3093 {
3094 print_sys_errmsg (pip -> pathname, errno);
3095 error ("PIOCGENTRY failed");
3096 }
3097
3098 if (ioctl (pip -> fd, PIOCGEXIT, &pip -> exitset) < 0)
3099 {
3100 print_sys_errmsg (pip -> pathname, errno);
3101 error ("PIOCGEXIT failed");
3102 }
3103
3104 printf_filtered ("System call tracing information:\n\n");
3105
3106 printf_filtered ("\t%-12s %-8s %-8s\n",
3107 "System call",
3108 "Entry",
3109 "Exit");
3110 for (syscallnum = 0; syscallnum < MAX_SYSCALLS; syscallnum++)
3111 {
3112 QUIT;
3113 if (syscall_table[syscallnum] != NULL)
3114 {
3115 printf_filtered ("\t%-12s ", syscall_table[syscallnum]);
3116 printf_filtered ("%-8s ",
3117 prismember (&pip -> entryset, syscallnum)
3118 ? "on" : "off");
3119 printf_filtered ("%-8s ",
3120 prismember (&pip -> exitset, syscallnum)
3121 ? "on" : "off");
3122 printf_filtered ("\n");
3123 }
3124 }
3125 printf_filtered ("\n");
3126 }
3127}
3128
3129static char *
3130signalname (signo)
3131 int signo;
3132{
26a859ec 3133 const char *name;
4ace50a5
FF
3134 static char locbuf[32];
3135
3136 name = strsigno (signo);
3137 if (name == NULL)
3138 {
3139 sprintf (locbuf, "Signal %d", signo);
3140 }
3141 else
3142 {
3143 sprintf (locbuf, "%s (%d)", name, signo);
3144 }
3145 return (locbuf);
3146}
3147
3148static char *
3149errnoname (errnum)
3150 int errnum;
3151{
26a859ec 3152 const char *name;
cc221e76
FF
3153 static char locbuf[32];
3154
4ace50a5
FF
3155 name = strerrno (errnum);
3156 if (name == NULL)
cc221e76 3157 {
4ace50a5 3158 sprintf (locbuf, "Errno %d", errnum);
cc221e76
FF
3159 }
3160 else
3161 {
4ace50a5 3162 sprintf (locbuf, "%s (%d)", name, errnum);
cc221e76
FF
3163 }
3164 return (locbuf);
3165}
3166
3167static void
3168info_proc_signals (pip, summary)
3169 struct procinfo *pip;
3170 int summary;
3171{
3172 int signo;
3173
3174 if (!summary)
3175 {
3176 if (ioctl (pip -> fd, PIOCGTRACE, &pip -> trace) < 0)
3177 {
3178 print_sys_errmsg (pip -> pathname, errno);
3179 error ("PIOCGTRACE failed");
3180 }
3181
3182 printf_filtered ("Disposition of signals:\n\n");
3183 printf_filtered ("\t%-15s %-8s %-8s %-8s %s\n\n",
3184 "Signal", "Trace", "Hold", "Pending", "Description");
3185 for (signo = 0; signo < NSIG; signo++)
3186 {
3187 QUIT;
3188 printf_filtered ("\t%-15s ", signalname (signo));
3189 printf_filtered ("%-8s ",
3190 prismember (&pip -> trace, signo)
3191 ? "on" : "off");
3192 printf_filtered ("%-8s ",
3193 prismember (&pip -> prstatus.pr_sighold, signo)
3194 ? "on" : "off");
2592eef8
PS
3195
3196#ifdef PROCFS_SIGPEND_OFFSET
3197 /* Alpha OSF/1 numbers the pending signals from 1. */
3198 printf_filtered ("%-8s ",
3199 (signo ? prismember (&pip -> prstatus.pr_sigpend,
3200 signo - 1)
3201 : 0)
3202 ? "yes" : "no");
3203#else
cc221e76
FF
3204 printf_filtered ("%-8s ",
3205 prismember (&pip -> prstatus.pr_sigpend, signo)
3206 ? "yes" : "no");
2592eef8 3207#endif
4ace50a5 3208 printf_filtered (" %s\n", safe_strsignal (signo));
cc221e76
FF
3209 }
3210 printf_filtered ("\n");
3211 }
3212}
3213
3214static void
3215info_proc_faults (pip, summary)
3216 struct procinfo *pip;
3217 int summary;
3218{
3219 struct trans *transp;
3220
3221 if (!summary)
3222 {
3223 if (ioctl (pip -> fd, PIOCGFAULT, &pip -> fltset) < 0)
3224 {
3225 print_sys_errmsg (pip -> pathname, errno);
3226 error ("PIOCGFAULT failed");
3227 }
3228
3229 printf_filtered ("Current traced hardware fault set:\n\n");
3230 printf_filtered ("\t%-12s %-8s\n", "Fault", "Trace");
3231
3232 for (transp = faults_table; transp -> name != NULL; transp++)
3233 {
3234 QUIT;
3235 printf_filtered ("\t%-12s ", transp -> name);
3236 printf_filtered ("%-8s", prismember (&pip -> fltset, transp -> value)
3237 ? "on" : "off");
3238 printf_filtered ("\n");
3239 }
3240 printf_filtered ("\n");
3241 }
3242}
3243
3244static void
3245info_proc_mappings (pip, summary)
1ab3bf1b 3246 struct procinfo *pip;
cc221e76 3247 int summary;
a39ad5ce
FF
3248{
3249 int nmap;
3250 struct prmap *prmaps;
3251 struct prmap *prmap;
3252
cc221e76 3253 if (!summary)
a39ad5ce 3254 {
cc221e76 3255 printf_filtered ("Mapped address spaces:\n\n");
2592eef8
PS
3256#ifdef BFD_HOST_64_BIT
3257 printf_filtered (" %18s %18s %10s %10s %7s\n",
3258#else
5c1c5e67 3259 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
2592eef8 3260#endif
cc221e76
FF
3261 "Start Addr",
3262 " End Addr",
3263 " Size",
3264 " Offset",
3265 "Flags");
3266 if (ioctl (pip -> fd, PIOCNMAP, &nmap) == 0)
a39ad5ce 3267 {
cc221e76
FF
3268 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
3269 if (ioctl (pip -> fd, PIOCMAP, prmaps) == 0)
a39ad5ce 3270 {
cc221e76
FF
3271 for (prmap = prmaps; prmap -> pr_size; ++prmap)
3272 {
2592eef8
PS
3273#ifdef BFD_HOST_64_BIT
3274 printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n",
3275#else
3276 printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n",
3277#endif
3278 (unsigned long)prmap -> pr_vaddr,
3279 (unsigned long)prmap -> pr_vaddr
3280 + prmap -> pr_size - 1,
cc221e76
FF
3281 prmap -> pr_size,
3282 prmap -> pr_off,
3283 mappingflags (prmap -> pr_mflags));
3284 }
a39ad5ce
FF
3285 }
3286 }
cc221e76 3287 printf_filtered ("\n");
a39ad5ce 3288 }
a39ad5ce
FF
3289}
3290
3291/*
3292
3293LOCAL FUNCTION
3294
cc221e76 3295 info_proc -- implement the "info proc" command
a39ad5ce
FF
3296
3297SYNOPSIS
3298
cc221e76 3299 void info_proc (char *args, int from_tty)
a39ad5ce
FF
3300
3301DESCRIPTION
3302
3303 Implement gdb's "info proc" command by using the /proc interface
3304 to print status information about any currently running process.
3305
3306 Examples of the use of "info proc" are:
3307
cc221e76
FF
3308 info proc (prints summary info for current inferior)
3309 info proc 123 (prints summary info for process with pid 123)
3310 info proc mappings (prints address mappings)
3311 info proc times (prints process/children times)
3312 info proc id (prints pid, ppid, gid, sid, etc)
3fbdd536 3313 FIXME: i proc id not implemented.
cc221e76 3314 info proc status (prints general process state info)
3fbdd536 3315 FIXME: i proc status not implemented.
cc221e76
FF
3316 info proc signals (prints info about signal handling)
3317 info proc all (prints all info)
a39ad5ce
FF
3318
3319 */
3320
3321static void
cc221e76 3322info_proc (args, from_tty)
1ab3bf1b
JG
3323 char *args;
3324 int from_tty;
a39ad5ce 3325{
a39ad5ce 3326 int pid;
a39ad5ce
FF
3327 struct procinfo *pip;
3328 struct cleanup *old_chain;
cc221e76
FF
3329 char **argv;
3330 int argsize;
3331 int summary = 1;
3332 int flags = 0;
3333 int syscalls = 0;
3334 int signals = 0;
3335 int faults = 0;
3336 int mappings = 0;
3337 int times = 0;
3338 int id = 0;
3339 int status = 0;
3340 int all = 0;
a39ad5ce
FF
3341
3342 old_chain = make_cleanup (null_cleanup, 0);
3343
de43d7d0
SG
3344 /* Default to using the current inferior if no pid specified. Note
3345 that inferior_pid may be 0, hence we set okerr. */
a39ad5ce 3346
de43d7d0 3347 pip = find_procinfo (inferior_pid, 1);
a39ad5ce 3348
a39ad5ce 3349 if (args != NULL)
35f5886e 3350 {
cc221e76 3351 if ((argv = buildargv (args)) == NULL)
a39ad5ce 3352 {
cc221e76
FF
3353 nomem (0);
3354 }
3355 make_cleanup (freeargv, (char *) argv);
3356
3357 while (*argv != NULL)
3358 {
3359 argsize = strlen (*argv);
3360 if (argsize >= 1 && strncmp (*argv, "all", argsize) == 0)
3361 {
3362 summary = 0;
3363 all = 1;
3364 }
3365 else if (argsize >= 2 && strncmp (*argv, "faults", argsize) == 0)
3366 {
3367 summary = 0;
3368 faults = 1;
3369 }
3370 else if (argsize >= 2 && strncmp (*argv, "flags", argsize) == 0)
3371 {
3372 summary = 0;
3373 flags = 1;
3374 }
3375 else if (argsize >= 1 && strncmp (*argv, "id", argsize) == 0)
3376 {
3377 summary = 0;
3378 id = 1;
3379 }
3380 else if (argsize >= 1 && strncmp (*argv, "mappings", argsize) == 0)
3381 {
3382 summary = 0;
3383 mappings = 1;
3384 }
3385 else if (argsize >= 2 && strncmp (*argv, "signals", argsize) == 0)
3386 {
3387 summary = 0;
3388 signals = 1;
3389 }
3390 else if (argsize >= 2 && strncmp (*argv, "status", argsize) == 0)
3391 {
3392 summary = 0;
3393 status = 1;
3394 }
3395 else if (argsize >= 2 && strncmp (*argv, "syscalls", argsize) == 0)
3396 {
3397 summary = 0;
3398 syscalls = 1;
3399 }
3400 else if (argsize >= 1 && strncmp (*argv, "times", argsize) == 0)
a39ad5ce 3401 {
cc221e76
FF
3402 summary = 0;
3403 times = 1;
a39ad5ce 3404 }
de43d7d0 3405 else if ((pid = atoi (*argv)) > 0)
a39ad5ce 3406 {
de43d7d0
SG
3407 pip = (struct procinfo *) xmalloc (sizeof (struct procinfo));
3408 memset (pip, 0, sizeof (*pip));
3409
3410 pip->pid = pid;
ec8ceca3 3411 if (!open_proc_file (pid, pip, O_RDONLY))
a39ad5ce
FF
3412 {
3413 perror_with_name (pip -> pathname);
3414 /* NOTREACHED */
3415 }
3416 make_cleanup (close_proc_file, pip);
3417 }
cc221e76
FF
3418 else if (**argv != '\000')
3419 {
3420 error ("Unrecognized or ambiguous keyword `%s'.", *argv);
3421 }
3422 argv++;
a39ad5ce 3423 }
35f5886e 3424 }
a39ad5ce
FF
3425
3426 /* If we don't have a valid open process at this point, then we have no
3427 inferior or didn't specify a specific pid. */
3428
de43d7d0 3429 if (!pip)
35f5886e 3430 {
6fe90fc8
JK
3431 error ("\
3432No process. Start debugging a program or specify an explicit process ID.");
35f5886e 3433 }
a39ad5ce 3434 if (ioctl (pip -> fd, PIOCSTATUS, &(pip -> prstatus)) < 0)
35f5886e 3435 {
a39ad5ce
FF
3436 print_sys_errmsg (pip -> pathname, errno);
3437 error ("PIOCSTATUS failed");
35f5886e 3438 }
a39ad5ce 3439
cc221e76
FF
3440 /* Print verbose information of the requested type(s), or just a summary
3441 of the information for all types. */
3442
3443 printf_filtered ("\nInformation for %s:\n\n", pip -> pathname);
3444 if (summary || all || flags)
3445 {
3446 info_proc_flags (pip, summary);
3447 }
3448 if (summary || all)
3449 {
3450 info_proc_stop (pip, summary);
3451 }
3452 if (summary || all || signals || faults)
3453 {
3454 info_proc_siginfo (pip, summary);
3455 }
3456 if (summary || all || syscalls)
3457 {
3458 info_proc_syscalls (pip, summary);
3459 }
3460 if (summary || all || mappings)
3461 {
3462 info_proc_mappings (pip, summary);
3463 }
3464 if (summary || all || signals)
3465 {
3466 info_proc_signals (pip, summary);
3467 }
3468 if (summary || all || faults)
3469 {
3470 info_proc_faults (pip, summary);
3471 }
3472 printf_filtered ("\n");
a39ad5ce
FF
3473
3474 /* All done, deal with closing any temporary process info structure,
3475 freeing temporary memory , etc. */
3476
3477 do_cleanups (old_chain);
3478}
3479
de43d7d0
SG
3480/*
3481
3482LOCAL FUNCTION
3483
eca4a350 3484 procfs_set_sproc_trap -- arrange for child to stop on sproc().
de43d7d0
SG
3485
3486SYNOPSIS
3487
eca4a350 3488 void procfs_set_sproc_trap (struct procinfo *)
de43d7d0
SG
3489
3490DESCRIPTION
3491
3492 This function sets up a trap on sproc system call exits so that we can
eca4a350 3493 detect the arrival of a new thread. We are called with the new thread
de43d7d0
SG
3494 stopped prior to it's first instruction.
3495
3496 Also note that we turn on the inherit-on-fork flag in the child process
3497 so that any grand-children start with all tracing flags set.
3498 */
3499
952a820e
SG
3500#ifdef SYS_sproc
3501
de43d7d0
SG
3502static void
3503procfs_set_sproc_trap (pi)
3504 struct procinfo *pi;
3505{
3506 sysset_t exitset;
3507
3508 if (ioctl (pi->fd, PIOCGEXIT, &exitset) < 0)
3509 {
3510 print_sys_errmsg (pi->pathname, errno);
3511 error ("PIOCGEXIT failed");
3512 }
3513
de43d7d0 3514 praddset (&exitset, SYS_sproc);
de43d7d0 3515
eca4a350
SG
3516 /* We trap on fork() and vfork() in order to disable debugging in our grand-
3517 children and descendant processes. At this time, GDB can only handle
3518 threads (multiple processes, one address space). forks (and execs) result
3519 in the creation of multiple address spaces, which GDB can't handle yet. */
3520
3521 praddset (&exitset, SYS_fork);
3522#ifdef SYS_vfork
3523 praddset (&exitset, SYS_vfork);
3524#endif
3525
de43d7d0
SG
3526 if (ioctl (pi->fd, PIOCSEXIT, &exitset) < 0)
3527 {
3528 print_sys_errmsg (pi->pathname, errno);
3529 error ("PIOCSEXIT failed");
3530 }
3531
3532 /* Turn on inherit-on-fork flag so that all grand-children of gdb start with
3533 tracing flags set. */
3534
3535#ifdef PIOCSET /* New method */
3536 {
3537 long pr_flags;
3538 pr_flags = PR_FORK;
3539 ioctl (pi->fd, PIOCSET, &pr_flags);
3540 }
3541#else
3542#ifdef PIOCSFORK /* Original method */
3543 ioctl (pi->fd, PIOCSFORK, NULL);
3544#endif
3545#endif
3546}
952a820e 3547#endif /* SYS_sproc */
de43d7d0 3548
3fbdd536
JG
3549/* Fork an inferior process, and start debugging it with /proc. */
3550
3551static void
3552procfs_create_inferior (exec_file, allargs, env)
3553 char *exec_file;
3554 char *allargs;
3555 char **env;
3556{
08f74b92
JK
3557 char *shell_file = getenv ("SHELL");
3558 char *tryname;
3559 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
3560 {
3561
3562 /* We will be looking down the PATH to find shell_file. If we
3563 just do this the normal way (via execlp, which operates by
3564 attempting an exec for each element of the PATH until it
3565 finds one which succeeds), then there will be an exec for
3566 each failed attempt, each of which will cause a PR_SYSEXIT
3567 stop, and we won't know how to distinguish the PR_SYSEXIT's
3568 for these failed execs with the ones for successful execs
3569 (whether the exec has succeeded is stored at that time in the
3570 carry bit or some such architecture-specific and
3571 non-ABI-specified place).
3572
3573 So I can't think of anything better than to search the PATH
3574 now. This has several disadvantages: (1) There is a race
3575 condition; if we find a file now and it is deleted before we
3576 exec it, we lose, even if the deletion leaves a valid file
3577 further down in the PATH, (2) there is no way to know exactly
3578 what an executable (in the sense of "capable of being
3579 exec'd") file is. Using access() loses because it may lose
3580 if the caller is the superuser; failing to use it loses if
3581 there are ACLs or some such. */
3582
3583 char *p;
3584 char *p1;
f93b941b
JK
3585 /* FIXME-maybe: might want "set path" command so user can change what
3586 path is used from within GDB. */
08f74b92
JK
3587 char *path = getenv ("PATH");
3588 int len;
3589 struct stat statbuf;
3590
3591 if (path == NULL)
3592 path = "/bin:/usr/bin";
3593
3594 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
3595 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
3596 {
3597 p1 = strchr (p, ':');
3598 if (p1 != NULL)
3599 len = p1 - p;
3600 else
3601 len = strlen (p);
3602 strncpy (tryname, p, len);
3603 tryname[len] = '\0';
3604 strcat (tryname, "/");
3605 strcat (tryname, shell_file);
3606 if (access (tryname, X_OK) < 0)
3607 continue;
3608 if (stat (tryname, &statbuf) < 0)
3609 continue;
3610 if (!S_ISREG (statbuf.st_mode))
3611 /* We certainly need to reject directories. I'm not quite
3612 as sure about FIFOs, sockets, etc., but I kind of doubt
3613 that people want to exec() these things. */
3614 continue;
3615 break;
3616 }
3617 if (p == NULL)
3618 /* Not found. This must be an error rather than merely passing
3619 the file to execlp(), because execlp() would try all the
3620 exec()s, causing GDB to get confused. */
3621 error ("Can't find shell %s in PATH", shell_file);
3622
3623 shell_file = tryname;
3624 }
3625
3fbdd536 3626 fork_inferior (exec_file, allargs, env,
08f74b92
JK
3627 proc_set_exec_trap, procfs_init_inferior, shell_file);
3628
3fbdd536
JG
3629 /* We are at the first instruction we care about. */
3630 /* Pedal to the metal... */
de43d7d0
SG
3631
3632 /* Setup traps on exit from sproc() */
3633
952a820e
SG
3634#ifdef SYS_sproc
3635 procfs_set_sproc_trap (current_procinfo);
3636#endif
de43d7d0 3637
67ac9759 3638 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
3fbdd536
JG
3639}
3640
3641/* Clean up after the inferior dies. */
3642
3643static void
3644procfs_mourn_inferior ()
3645{
fb63d460 3646 struct procinfo *pi;
cd4104e0 3647 struct procinfo *next_pi;
fb63d460 3648
cd4104e0
TL
3649 for (pi = procinfo_list; pi; pi = next_pi)
3650 {
3651 next_pi = pi->next;
3652 unconditionally_kill_inferior (pi);
3653 }
fb63d460 3654
3fbdd536
JG
3655 unpush_target (&procfs_ops);
3656 generic_mourn_inferior ();
3657}
3658
cd4104e0 3659
3fbdd536
JG
3660/* Mark our target-struct as eligible for stray "run" and "attach" commands. */
3661static int
3662procfs_can_run ()
3663{
3664 return(1);
3665}
6bc194d2 3666#ifdef TARGET_CAN_USE_HARDWARE_WATCHPOINT
999dd04b
JL
3667\f
3668/* Insert a watchpoint */
3669int
3670procfs_set_watchpoint(pid, addr, len, rw)
3671 int pid;
3672 CORE_ADDR addr;
3673 int len;
3674 int rw;
3675{
3676 struct procinfo *pi;
3677 prwatch_t wpt;
3678
3679 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
3680 wpt.pr_vaddr = (caddr_t)addr;
3681 wpt.pr_size = len;
3682 wpt.pr_wflags = ((rw & 1) ? MA_READ : 0) | ((rw & 2) ? MA_WRITE : 0);
3683 if (ioctl (pi->fd, PIOCSWATCH, &wpt) < 0)
3684 {
3685 if (errno == E2BIG)
3686 return -1;
3687 /* Currently it sometimes happens that the same watchpoint gets
3688 deleted twice - don't die in this case (FIXME please) */
3689 if (errno == ESRCH && len == 0)
3690 return 0;
3691 print_sys_errmsg (pi->pathname, errno);
3692 error ("PIOCSWATCH failed");
3693 }
3694 return 0;
3695}
3696
3697int
3698procfs_stopped_by_watchpoint(pid)
3699 int pid;
3700{
3701 struct procinfo *pi;
3702 short what;
3703 short why;
3704
3705 pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0);
3706 if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
3707 {
3708 why = pi->prstatus.pr_why;
3709 what = pi->prstatus.pr_what;
3710 if (why == PR_FAULTED
6bc194d2
JL
3711#if defined (FLTWATCH) && defined (FLTKWATCH)
3712 && (what == FLTWATCH) || (what == FLTKWATCH)
3713#else
3714#ifdef FLTWATCH
3715 && (what == FLTWATCH)
3716#endif
3717#ifdef FLTKWATCH
3718 && (what == FLTKWATCH)
3719#endif
72b8ca51 3720#endif
6bc194d2 3721 )
999dd04b
JL
3722 return what;
3723 }
3724 return 0;
3725}
6bc194d2 3726#endif
999dd04b 3727
78b459a7
SG
3728/* Send a SIGINT to the process group. This acts just like the user typed a
3729 ^C on the controlling terminal.
3730
3731 XXX - This may not be correct for all systems. Some may want to use
3732 killpg() instead of kill (-pgrp). */
3733
3734void
2592eef8 3735procfs_stop ()
78b459a7
SG
3736{
3737 extern pid_t inferior_process_group;
3738
3739 kill (-inferior_process_group, SIGINT);
3740}
3741
3fbdd536
JG
3742\f
3743struct target_ops procfs_ops = {
3744 "procfs", /* to_shortname */
3745 "Unix /proc child process", /* to_longname */
3746 "Unix /proc child process (started by the \"run\" command).", /* to_doc */
3747 procfs_open, /* to_open */
3748 0, /* to_close */
3749 procfs_attach, /* to_attach */
3750 procfs_detach, /* to_detach */
3751 procfs_resume, /* to_resume */
3752 procfs_wait, /* to_wait */
3753 procfs_fetch_registers, /* to_fetch_registers */
3754 procfs_store_registers, /* to_store_registers */
3755 procfs_prepare_to_store, /* to_prepare_to_store */
3756 procfs_xfer_memory, /* to_xfer_memory */
3757 procfs_files_info, /* to_files_info */
3758 memory_insert_breakpoint, /* to_insert_breakpoint */
3759 memory_remove_breakpoint, /* to_remove_breakpoint */
3760 terminal_init_inferior, /* to_terminal_init */
3761 terminal_inferior, /* to_terminal_inferior */
3762 terminal_ours_for_output, /* to_terminal_ours_for_output */
3763 terminal_ours, /* to_terminal_ours */
3764 child_terminal_info, /* to_terminal_info */
3765 procfs_kill_inferior, /* to_kill */
3766 0, /* to_load */
3767 0, /* to_lookup_symbol */
3768 procfs_create_inferior, /* to_create_inferior */
3769 procfs_mourn_inferior, /* to_mourn_inferior */
3770 procfs_can_run, /* to_can_run */
3950a34e 3771 procfs_notice_signals, /* to_notice_signals */
2592eef8 3772 procfs_stop, /* to_stop */
3fbdd536
JG
3773 process_stratum, /* to_stratum */
3774 0, /* to_next */
3775 1, /* to_has_all_memory */
3776 1, /* to_has_memory */
3777 1, /* to_has_stack */
3778 1, /* to_has_registers */
3779 1, /* to_has_execution */
3780 0, /* sections */
3781 0, /* sections_end */
3782 OPS_MAGIC /* to_magic */
3783};
3784
3fbdd536
JG
3785void
3786_initialize_procfs ()
3787{
2592eef8
PS
3788#ifdef HAVE_OPTIONAL_PROC_FS
3789 char procname[32];
3790 int fd;
3791
3792 /* If we have an optional /proc filesystem (e.g. under OSF/1),
3793 don't add procfs support if we cannot access the running
3794 GDB via /proc. */
3795 sprintf (procname, PROC_NAME_FMT, getpid ());
3796 if ((fd = open (procname, O_RDONLY)) < 0)
3797 return;
3798 close (fd);
3799#endif
3800
3fbdd536
JG
3801 add_target (&procfs_ops);
3802
3803 add_info ("proc", info_proc,
cc221e76
FF
3804"Show process status information using /proc entry.\n\
3805Specify process id or use current inferior by default.\n\
3806Specify keywords for detailed information; default is summary.\n\
3807Keywords are: `all', `faults', `flags', `id', `mappings', `signals',\n\
3808`status', `syscalls', and `times'.\n\
3fbdd536 3809Unambiguous abbreviations may be used.");
a39ad5ce 3810
cc221e76 3811 init_syscall_table ();
35f5886e 3812}
This page took 0.37862 seconds and 4 git commands to generate.