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