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