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