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