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