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