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