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