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