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