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