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