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