1c15c367c28c000adeff4bc9cabec332d7c61e0c
[deliverable/binutils-gdb.git] / gdb / procfs.c
1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
3 Written by Michael Snyder at Cygnus Solutions.
4 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
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 Foundation,
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "target.h"
25 #include "gdbcore.h"
26 #include "gdbcmd.h"
27 #include "gdbthread.h"
28
29 #if defined (NEW_PROC_API)
30 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
31 #endif
32
33 #include <sys/procfs.h>
34 #ifdef HAVE_SYS_FAULT_H
35 #include <sys/fault.h>
36 #endif
37 #ifdef HAVE_SYS_SYSCALL_H
38 #include <sys/syscall.h>
39 #endif
40 #include <sys/errno.h>
41 #include <sys/wait.h>
42 #include <signal.h>
43 #include <ctype.h>
44
45 /*
46 * PROCFS.C
47 *
48 * This module provides the interface between GDB and the
49 * /proc file system, which is used on many versions of Unix
50 * as a means for debuggers to control other processes.
51 * Examples of the systems that use this interface are:
52 * Irix
53 * Solaris
54 * OSF
55 * Unixware
56 * AIX5
57 *
58 * /proc works by immitating a file system: you open a simulated file
59 * that represents the process you wish to interact with, and
60 * perform operations on that "file" in order to examine or change
61 * the state of the other process.
62 *
63 * The most important thing to know about /proc and this module
64 * is that there are two very different interfaces to /proc:
65 * One that uses the ioctl system call, and
66 * another that uses read and write system calls.
67 * This module has to support both /proc interfaces. This means
68 * that there are two different ways of doing every basic operation.
69 *
70 * In order to keep most of the code simple and clean, I have
71 * defined an interface "layer" which hides all these system calls.
72 * An ifdef (NEW_PROC_API) determines which interface we are using,
73 * and most or all occurrances of this ifdef should be confined to
74 * this interface layer.
75 */
76
77
78 /* Determine which /proc API we are using:
79 The ioctl API defines PIOCSTATUS, while
80 the read/write (multiple fd) API never does. */
81
82 #ifdef NEW_PROC_API
83 #include <sys/types.h>
84 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
85 #endif
86
87 #include <fcntl.h> /* for O_RDONLY */
88 #include <unistd.h> /* for "X_OK" */
89 #include "gdb_stat.h" /* for struct stat */
90
91 /* Note: procfs-utils.h must be included after the above system header
92 files, because it redefines various system calls using macros.
93 This may be incompatible with the prototype declarations. */
94
95 #include "proc-utils.h"
96
97 /* Prototypes for supply_gregset etc. */
98 #include "gregset.h"
99
100 /* =================== TARGET_OPS "MODULE" =================== */
101
102 /*
103 * This module defines the GDB target vector and its methods.
104 */
105
106 static void procfs_open (char *, int);
107 static void procfs_attach (char *, int);
108 static void procfs_detach (char *, int);
109 static void procfs_resume (int, int, enum target_signal);
110 static int procfs_can_run (void);
111 static void procfs_stop (void);
112 static void procfs_files_info (struct target_ops *);
113 static void procfs_fetch_registers (int);
114 static void procfs_store_registers (int);
115 static void procfs_notice_signals (int);
116 static void procfs_prepare_to_store (void);
117 static void procfs_kill_inferior (void);
118 static void procfs_mourn_inferior (void);
119 static void procfs_create_inferior (char *, char *, char **);
120 static int procfs_wait (int, struct target_waitstatus *);
121 static int procfs_xfer_memory (CORE_ADDR, char *, int, int,
122 struct mem_attrib *attrib,
123 struct target_ops *);
124
125 static int procfs_thread_alive (int);
126
127 void procfs_find_new_threads (void);
128 char *procfs_pid_to_str (int);
129
130 struct target_ops procfs_ops; /* the target vector */
131
132 static void
133 init_procfs_ops (void)
134 {
135 procfs_ops.to_shortname = "procfs";
136 procfs_ops.to_longname = "Unix /proc child process";
137 procfs_ops.to_doc =
138 "Unix /proc child process (started by the \"run\" command).";
139 procfs_ops.to_open = procfs_open;
140 procfs_ops.to_can_run = procfs_can_run;
141 procfs_ops.to_create_inferior = procfs_create_inferior;
142 procfs_ops.to_kill = procfs_kill_inferior;
143 procfs_ops.to_mourn_inferior = procfs_mourn_inferior;
144 procfs_ops.to_attach = procfs_attach;
145 procfs_ops.to_detach = procfs_detach;
146 procfs_ops.to_wait = procfs_wait;
147 procfs_ops.to_resume = procfs_resume;
148 procfs_ops.to_prepare_to_store = procfs_prepare_to_store;
149 procfs_ops.to_fetch_registers = procfs_fetch_registers;
150 procfs_ops.to_store_registers = procfs_store_registers;
151 procfs_ops.to_xfer_memory = procfs_xfer_memory;
152 procfs_ops.to_insert_breakpoint = memory_insert_breakpoint;
153 procfs_ops.to_remove_breakpoint = memory_remove_breakpoint;
154 procfs_ops.to_notice_signals = procfs_notice_signals;
155 procfs_ops.to_files_info = procfs_files_info;
156 procfs_ops.to_stop = procfs_stop;
157
158 procfs_ops.to_terminal_init = terminal_init_inferior;
159 procfs_ops.to_terminal_inferior = terminal_inferior;
160 procfs_ops.to_terminal_ours_for_output = terminal_ours_for_output;
161 procfs_ops.to_terminal_ours = terminal_ours;
162 procfs_ops.to_terminal_info = child_terminal_info;
163
164 procfs_ops.to_find_new_threads = procfs_find_new_threads;
165 procfs_ops.to_thread_alive = procfs_thread_alive;
166 procfs_ops.to_pid_to_str = procfs_pid_to_str;
167
168 procfs_ops.to_has_all_memory = 1;
169 procfs_ops.to_has_memory = 1;
170 procfs_ops.to_has_execution = 1;
171 procfs_ops.to_has_stack = 1;
172 procfs_ops.to_has_registers = 1;
173 procfs_ops.to_stratum = process_stratum;
174 procfs_ops.to_has_thread_control = tc_schedlock;
175 procfs_ops.to_magic = OPS_MAGIC;
176 }
177
178 /* =================== END, TARGET_OPS "MODULE" =================== */
179
180 /*
181 * World Unification:
182 *
183 * Put any typedefs, defines etc. here that are required for
184 * the unification of code that handles different versions of /proc.
185 */
186
187 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
188 #ifdef WA_READ
189 enum { READ_WATCHFLAG = WA_READ,
190 WRITE_WATCHFLAG = WA_WRITE,
191 EXEC_WATCHFLAG = WA_EXEC,
192 AFTER_WATCHFLAG = WA_TRAPAFTER
193 };
194 #endif
195 #else /* Irix method for watchpoints */
196 enum { READ_WATCHFLAG = MA_READ,
197 WRITE_WATCHFLAG = MA_WRITE,
198 EXEC_WATCHFLAG = MA_EXEC,
199 AFTER_WATCHFLAG = 0 /* trapafter not implemented */
200 };
201 #endif
202
203 /* gdb_sigset_t */
204 #ifdef HAVE_PR_SIGSET_T
205 typedef pr_sigset_t gdb_sigset_t;
206 #else
207 typedef sigset_t gdb_sigset_t;
208 #endif
209
210 /* sigaction */
211 #ifdef HAVE_PR_SIGACTION64_T
212 typedef pr_sigaction64_t gdb_sigaction_t;
213 #else
214 typedef struct sigaction gdb_sigaction_t;
215 #endif
216
217 /* siginfo */
218 #ifdef HAVE_PR_SIGINFO64_T
219 typedef pr_siginfo64_t gdb_siginfo_t;
220 #else
221 typedef struct siginfo gdb_siginfo_t;
222 #endif
223
224 /* gdb_premptysysset */
225 #ifdef premptysysset
226 #define gdb_premptysysset premptysysset
227 #else
228 #define gdb_premptysysset premptyset
229 #endif
230
231 /* praddsysset */
232 #ifdef praddsysset
233 #define gdb_praddsysset praddsysset
234 #else
235 #define gdb_praddsysset praddset
236 #endif
237
238 /* prdelsysset */
239 #ifdef prdelsysset
240 #define gdb_prdelsysset prdelsysset
241 #else
242 #define gdb_prdelsysset prdelset
243 #endif
244
245 /* prissyssetmember */
246 #ifdef prissyssetmember
247 #define gdb_pr_issyssetmember prissyssetmember
248 #else
249 #define gdb_pr_issyssetmember prismember
250 #endif
251
252 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
253 as intuitively descriptive as it could be, so we'll define
254 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
255 this writing, this feature is only found on AIX5 systems and
256 basically means that the set of syscalls is not fixed. I.e,
257 there's no nice table that one can #include to get all of the
258 syscall numbers. Instead, they're stored in /proc/PID/sysent
259 for each process. We are at least guaranteed that they won't
260 change over the lifetime of the process. But each process could
261 (in theory) have different syscall numbers.
262 */
263 #ifdef HAVE_PRSYSENT_T
264 #define DYNAMIC_SYSCALLS
265 #endif
266
267
268
269 /* =================== STRUCT PROCINFO "MODULE" =================== */
270
271 /* FIXME: this comment will soon be out of date W.R.T. threads. */
272
273 /* The procinfo struct is a wrapper to hold all the state information
274 concerning a /proc process. There should be exactly one procinfo
275 for each process, and since GDB currently can debug only one
276 process at a time, that means there should be only one procinfo.
277 All of the LWP's of a process can be accessed indirectly thru the
278 single process procinfo.
279
280 However, against the day when GDB may debug more than one process,
281 this data structure is kept in a list (which for now will hold no
282 more than one member), and many functions will have a pointer to a
283 procinfo as an argument.
284
285 There will be a separate procinfo structure for use by the (not yet
286 implemented) "info proc" command, so that we can print useful
287 information about any random process without interfering with the
288 inferior's procinfo information. */
289
290 #ifdef NEW_PROC_API
291 /* format strings for /proc paths */
292 # ifndef CTL_PROC_NAME_FMT
293 # define MAIN_PROC_NAME_FMT "/proc/%d"
294 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
295 # define AS_PROC_NAME_FMT "/proc/%d/as"
296 # define MAP_PROC_NAME_FMT "/proc/%d/map"
297 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
298 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
299 # endif
300 /* the name of the proc status struct depends on the implementation */
301 typedef pstatus_t gdb_prstatus_t;
302 typedef lwpstatus_t gdb_lwpstatus_t;
303 #else /* ! NEW_PROC_API */
304 /* format strings for /proc paths */
305 # ifndef CTL_PROC_NAME_FMT
306 # define MAIN_PROC_NAME_FMT "/proc/%05d"
307 # define CTL_PROC_NAME_FMT "/proc/%05d"
308 # define AS_PROC_NAME_FMT "/proc/%05d"
309 # define MAP_PROC_NAME_FMT "/proc/%05d"
310 # define STATUS_PROC_NAME_FMT "/proc/%05d"
311 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
312 # endif
313 /* the name of the proc status struct depends on the implementation */
314 typedef prstatus_t gdb_prstatus_t;
315 typedef prstatus_t gdb_lwpstatus_t;
316 #endif /* NEW_PROC_API */
317
318
319 /* Provide default composite pid manipulation macros for systems that
320 don't have threads. */
321
322 #ifndef PIDGET
323 #define PIDGET(PID) (PID)
324 #define TIDGET(PID) (PID)
325 #endif
326 #ifndef MERGEPID
327 #define MERGEPID(PID, TID) (PID)
328 #endif
329
330 typedef struct procinfo {
331 struct procinfo *next;
332 int pid; /* Process ID */
333 int tid; /* Thread/LWP id */
334
335 /* process state */
336 int was_stopped;
337 int ignore_next_sigstop;
338
339 /* The following four fd fields may be identical, or may contain
340 several different fd's, depending on the version of /proc
341 (old ioctl or new read/write). */
342
343 int ctl_fd; /* File descriptor for /proc control file */
344 /*
345 * The next three file descriptors are actually only needed in the
346 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
347 * However, to avoid a bunch of #ifdefs in the code, we will use
348 * them uniformly by (in the case of the ioctl single-file-descriptor
349 * implementation) filling them with copies of the control fd.
350 */
351 int status_fd; /* File descriptor for /proc status file */
352 int as_fd; /* File descriptor for /proc as file */
353
354 char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */
355
356 fltset_t saved_fltset; /* Saved traced hardware fault set */
357 gdb_sigset_t saved_sigset; /* Saved traced signal set */
358 gdb_sigset_t saved_sighold; /* Saved held signal set */
359 sysset_t *saved_exitset; /* Saved traced system call exit set */
360 sysset_t *saved_entryset; /* Saved traced system call entry set */
361
362 gdb_prstatus_t prstatus; /* Current process status info */
363
364 #ifndef NEW_PROC_API
365 gdb_fpregset_t fpregset; /* Current floating point registers */
366 #endif
367
368 #ifdef DYNAMIC_SYSCALLS
369 int num_syscalls; /* Total number of syscalls */
370 char **syscall_names; /* Syscall number to name map */
371 #endif
372
373 struct procinfo *thread_list;
374
375 int status_valid : 1;
376 int gregs_valid : 1;
377 int fpregs_valid : 1;
378 int threads_valid: 1;
379 } procinfo;
380
381 static char errmsg[128]; /* shared error msg buffer */
382
383 /* Function prototypes for procinfo module: */
384
385 static procinfo *find_procinfo_or_die (int pid, int tid);
386 static procinfo *find_procinfo (int pid, int tid);
387 static procinfo *create_procinfo (int pid, int tid);
388 static void destroy_procinfo (procinfo * p);
389 static void do_destroy_procinfo_cleanup (void *);
390 static void dead_procinfo (procinfo * p, char *msg, int killp);
391 static int open_procinfo_files (procinfo * p, int which);
392 static void close_procinfo_files (procinfo * p);
393 static int sysset_t_size (procinfo *p);
394 static sysset_t *sysset_t_alloc (procinfo * pi);
395 #ifdef DYNAMIC_SYSCALLS
396 static void load_syscalls (procinfo *pi);
397 static void free_syscalls (procinfo *pi);
398 static int find_syscall (procinfo *pi, char *name);
399 #endif /* DYNAMIC_SYSCALLS */
400
401 /* The head of the procinfo list: */
402 static procinfo * procinfo_list;
403
404 /*
405 * Function: find_procinfo
406 *
407 * Search the procinfo list.
408 *
409 * Returns: pointer to procinfo, or NULL if not found.
410 */
411
412 static procinfo *
413 find_procinfo (int pid, int tid)
414 {
415 procinfo *pi;
416
417 for (pi = procinfo_list; pi; pi = pi->next)
418 if (pi->pid == pid)
419 break;
420
421 if (pi)
422 if (tid)
423 {
424 /* Don't check threads_valid. If we're updating the
425 thread_list, we want to find whatever threads are already
426 here. This means that in general it is the caller's
427 responsibility to check threads_valid and update before
428 calling find_procinfo, if the caller wants to find a new
429 thread. */
430
431 for (pi = pi->thread_list; pi; pi = pi->next)
432 if (pi->tid == tid)
433 break;
434 }
435
436 return pi;
437 }
438
439 /*
440 * Function: find_procinfo_or_die
441 *
442 * Calls find_procinfo, but errors on failure.
443 */
444
445 static procinfo *
446 find_procinfo_or_die (int pid, int tid)
447 {
448 procinfo *pi = find_procinfo (pid, tid);
449
450 if (pi == NULL)
451 {
452 if (tid)
453 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
454 pid, tid);
455 else
456 error ("procfs: couldn't find pid %d in procinfo list.", pid);
457 }
458 return pi;
459 }
460
461 /*
462 * Function: open_procinfo_files
463 *
464 * Open the file descriptor for the process or LWP.
465 * ifdef NEW_PROC_API, we only open the control file descriptor;
466 * the others are opened lazily as needed.
467 * else (if not NEW_PROC_API), there is only one real
468 * file descriptor, but we keep multiple copies of it so that
469 * the code that uses them does not have to be #ifdef'd.
470 *
471 * Return: file descriptor, or zero for failure.
472 */
473
474 enum { FD_CTL, FD_STATUS, FD_AS };
475
476 static int
477 open_procinfo_files (procinfo *pi, int which)
478 {
479 #ifdef NEW_PROC_API
480 char tmp[MAX_PROC_NAME_SIZE];
481 #endif
482 int fd;
483
484 /*
485 * This function is getting ALMOST long enough to break up into several.
486 * Here is some rationale:
487 *
488 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
489 * There are several file descriptors that may need to be open
490 * for any given process or LWP. The ones we're intereted in are:
491 * - control (ctl) write-only change the state
492 * - status (status) read-only query the state
493 * - address space (as) read/write access memory
494 * - map (map) read-only virtual addr map
495 * Most of these are opened lazily as they are needed.
496 * The pathnames for the 'files' for an LWP look slightly
497 * different from those of a first-class process:
498 * Pathnames for a process (<proc-id>):
499 * /proc/<proc-id>/ctl
500 * /proc/<proc-id>/status
501 * /proc/<proc-id>/as
502 * /proc/<proc-id>/map
503 * Pathnames for an LWP (lwp-id):
504 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
505 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
506 * An LWP has no map or address space file descriptor, since
507 * the memory map and address space are shared by all LWPs.
508 *
509 * Everyone else (Solaris 2.5, Irix, OSF)
510 * There is only one file descriptor for each process or LWP.
511 * For convenience, we copy the same file descriptor into all
512 * three fields of the procinfo struct (ctl_fd, status_fd, and
513 * as_fd, see NEW_PROC_API above) so that code that uses them
514 * doesn't need any #ifdef's.
515 * Pathname for all:
516 * /proc/<proc-id>
517 *
518 * Solaris 2.5 LWP's:
519 * Each LWP has an independent file descriptor, but these
520 * are not obtained via the 'open' system call like the rest:
521 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
522 * to the file descriptor of the parent process.
523 *
524 * OSF threads:
525 * These do not even have their own independent file descriptor.
526 * All operations are carried out on the file descriptor of the
527 * parent process. Therefore we just call open again for each
528 * thread, getting a new handle for the same 'file'.
529 */
530
531 #ifdef NEW_PROC_API
532 /*
533 * In this case, there are several different file descriptors that
534 * we might be asked to open. The control file descriptor will be
535 * opened early, but the others will be opened lazily as they are
536 * needed.
537 */
538
539 strcpy (tmp, pi->pathname);
540 switch (which) { /* which file descriptor to open? */
541 case FD_CTL:
542 if (pi->tid)
543 strcat (tmp, "/lwpctl");
544 else
545 strcat (tmp, "/ctl");
546 fd = open (tmp, O_WRONLY);
547 if (fd <= 0)
548 return 0; /* fail */
549 pi->ctl_fd = fd;
550 break;
551 case FD_AS:
552 if (pi->tid)
553 return 0; /* there is no 'as' file descriptor for an lwp */
554 strcat (tmp, "/as");
555 fd = open (tmp, O_RDWR);
556 if (fd <= 0)
557 return 0; /* fail */
558 pi->as_fd = fd;
559 break;
560 case FD_STATUS:
561 if (pi->tid)
562 strcat (tmp, "/lwpstatus");
563 else
564 strcat (tmp, "/status");
565 fd = open (tmp, O_RDONLY);
566 if (fd <= 0)
567 return 0; /* fail */
568 pi->status_fd = fd;
569 break;
570 default:
571 return 0; /* unknown file descriptor */
572 }
573 #else /* not NEW_PROC_API */
574 /*
575 * In this case, there is only one file descriptor for each procinfo
576 * (ie. each process or LWP). In fact, only the file descriptor for
577 * the process can actually be opened by an 'open' system call.
578 * The ones for the LWPs have to be obtained thru an IOCTL call
579 * on the process's file descriptor.
580 *
581 * For convenience, we copy each procinfo's single file descriptor
582 * into all of the fields occupied by the several file descriptors
583 * of the NEW_PROC_API implementation. That way, the code that uses
584 * them can be written without ifdefs.
585 */
586
587
588 #ifdef PIOCTSTATUS /* OSF */
589 if ((fd = open (pi->pathname, O_RDWR)) == 0) /* Only one FD; just open it. */
590 return 0;
591 #else /* Sol 2.5, Irix, other? */
592 if (pi->tid == 0) /* Master procinfo for the process */
593 {
594 fd = open (pi->pathname, O_RDWR);
595 if (fd <= 0)
596 return 0; /* fail */
597 }
598 else /* LWP thread procinfo */
599 {
600 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
601 procinfo *process;
602 int lwpid = pi->tid;
603
604 /* Find the procinfo for the entire process. */
605 if ((process = find_procinfo (pi->pid, 0)) == NULL)
606 return 0; /* fail */
607
608 /* Now obtain the file descriptor for the LWP. */
609 if ((fd = ioctl (process->ctl_fd, PIOCOPENLWP, &lwpid)) <= 0)
610 return 0; /* fail */
611 #else /* Irix, other? */
612 return 0; /* Don't know how to open threads */
613 #endif /* Sol 2.5 PIOCOPENLWP */
614 }
615 #endif /* OSF PIOCTSTATUS */
616 pi->ctl_fd = pi->as_fd = pi->status_fd = fd;
617 #endif /* NEW_PROC_API */
618
619 return 1; /* success */
620 }
621
622 /*
623 * Function: create_procinfo
624 *
625 * Allocate a data structure and link it into the procinfo list.
626 * (First tries to find a pre-existing one (FIXME: why?)
627 *
628 * Return: pointer to new procinfo struct.
629 */
630
631 static procinfo *
632 create_procinfo (int pid, int tid)
633 {
634 procinfo *pi, *parent;
635
636 if ((pi = find_procinfo (pid, tid)))
637 return pi; /* Already exists, nothing to do. */
638
639 /* find parent before doing malloc, to save having to cleanup */
640 if (tid != 0)
641 parent = find_procinfo_or_die (pid, 0); /* FIXME: should I
642 create it if it
643 doesn't exist yet? */
644
645 pi = (procinfo *) xmalloc (sizeof (procinfo));
646 memset (pi, 0, sizeof (procinfo));
647 pi->pid = pid;
648 pi->tid = tid;
649
650 #ifdef DYNAMIC_SYSCALLS
651 load_syscalls (pi);
652 #endif
653
654 /* Chain into list. */
655 if (tid == 0)
656 {
657 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
658 pi->next = procinfo_list;
659 procinfo_list = pi;
660 }
661 else
662 {
663 #ifdef NEW_PROC_API
664 sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid);
665 #else
666 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
667 #endif
668 pi->next = parent->thread_list;
669 parent->thread_list = pi;
670 }
671 return pi;
672 }
673
674 /*
675 * Function: close_procinfo_files
676 *
677 * Close all file descriptors associated with the procinfo
678 */
679
680 static void
681 close_procinfo_files (procinfo *pi)
682 {
683 if (pi->ctl_fd > 0)
684 close (pi->ctl_fd);
685 #ifdef NEW_PROC_API
686 if (pi->as_fd > 0)
687 close (pi->as_fd);
688 if (pi->status_fd > 0)
689 close (pi->status_fd);
690 #endif
691 pi->ctl_fd = pi->as_fd = pi->status_fd = 0;
692 }
693
694 /*
695 * Function: destroy_procinfo
696 *
697 * Destructor function. Close, unlink and deallocate the object.
698 */
699
700 static void
701 destroy_one_procinfo (procinfo **list, procinfo *pi)
702 {
703 procinfo *ptr;
704
705 /* Step one: unlink the procinfo from its list */
706 if (pi == *list)
707 *list = pi->next;
708 else
709 for (ptr = *list; ptr; ptr = ptr->next)
710 if (ptr->next == pi)
711 {
712 ptr->next = pi->next;
713 break;
714 }
715
716 /* Step two: close any open file descriptors */
717 close_procinfo_files (pi);
718
719 /* Step three: free the memory. */
720 #ifdef DYNAMIC_SYSCALLS
721 free_syscalls (pi);
722 #endif
723 xfree (pi);
724 }
725
726 static void
727 destroy_procinfo (procinfo *pi)
728 {
729 procinfo *tmp;
730
731 if (pi->tid != 0) /* destroy a thread procinfo */
732 {
733 tmp = find_procinfo (pi->pid, 0); /* find the parent process */
734 destroy_one_procinfo (&tmp->thread_list, pi);
735 }
736 else /* destroy a process procinfo and all its threads */
737 {
738 /* First destroy the children, if any; */
739 while (pi->thread_list != NULL)
740 destroy_one_procinfo (&pi->thread_list, pi->thread_list);
741 /* Then destroy the parent. Genocide!!! */
742 destroy_one_procinfo (&procinfo_list, pi);
743 }
744 }
745
746 static void
747 do_destroy_procinfo_cleanup (void *pi)
748 {
749 destroy_procinfo (pi);
750 }
751
752 enum { NOKILL, KILL };
753
754 /*
755 * Function: dead_procinfo
756 *
757 * To be called on a non_recoverable error for a procinfo.
758 * Prints error messages, optionally sends a SIGKILL to the process,
759 * then destroys the data structure.
760 */
761
762 static void
763 dead_procinfo (procinfo *pi, char *msg, int kill_p)
764 {
765 char procfile[80];
766
767 if (pi->pathname)
768 {
769 print_sys_errmsg (pi->pathname, errno);
770 }
771 else
772 {
773 sprintf (procfile, "process %d", pi->pid);
774 print_sys_errmsg (procfile, errno);
775 }
776 if (kill_p == KILL)
777 kill (pi->pid, SIGKILL);
778
779 destroy_procinfo (pi);
780 error (msg);
781 }
782
783 /*
784 * Function: sysset_t_size
785 *
786 * Returns the (complete) size of a sysset_t struct. Normally, this
787 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
788 * size of sysset_t isn't known until runtime.
789 */
790
791 static int
792 sysset_t_size (procinfo * pi)
793 {
794 #ifndef DYNAMIC_SYSCALLS
795 return sizeof (sysset_t);
796 #else
797 return sizeof (sysset_t) - sizeof (uint64_t)
798 + sizeof (uint64_t) * ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
799 / (8 * sizeof (uint64_t)));
800 #endif
801 }
802
803 /* Function: sysset_t_alloc
804
805 Allocate and (partially) initialize a sysset_t struct. */
806
807 static sysset_t *
808 sysset_t_alloc (procinfo * pi)
809 {
810 sysset_t *ret;
811 int size = sysset_t_size (pi);
812 ret = xmalloc (size);
813 #ifdef DYNAMIC_SYSCALLS
814 ret->pr_size = (pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
815 / (8 * sizeof (uint64_t));
816 #endif
817 return ret;
818 }
819
820 #ifdef DYNAMIC_SYSCALLS
821
822 /* Function: load_syscalls
823
824 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
825 pi->num_syscalls with the number of syscalls and pi->syscall_names
826 with the names. (Certain numbers may be skipped in which case the
827 names for these numbers will be left as NULL.) */
828
829 #define MAX_SYSCALL_NAME_LENGTH 256
830 #define MAX_SYSCALLS 65536
831
832 static void
833 load_syscalls (procinfo *pi)
834 {
835 char pathname[MAX_PROC_NAME_SIZE];
836 int sysent_fd;
837 prsysent_t header;
838 prsyscall_t *syscalls;
839 int i, size, maxcall;
840
841 pi->num_syscalls = 0;
842 pi->syscall_names = 0;
843
844 /* Open the file descriptor for the sysent file */
845 sprintf (pathname, "/proc/%d/sysent", pi->pid);
846 sysent_fd = open (pathname, O_RDONLY);
847 if (sysent_fd < 0)
848 {
849 error ("load_syscalls: Can't open /proc/%d/sysent", pi->pid);
850 }
851
852 size = sizeof header - sizeof (prsyscall_t);
853 if (read (sysent_fd, &header, size) != size)
854 {
855 error ("load_syscalls: Error reading /proc/%d/sysent", pi->pid);
856 }
857
858 if (header.pr_nsyscalls == 0)
859 {
860 error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi->pid);
861 }
862
863 size = header.pr_nsyscalls * sizeof (prsyscall_t);
864 syscalls = xmalloc (size);
865
866 if (read (sysent_fd, syscalls, size) != size)
867 {
868 xfree (syscalls);
869 error ("load_syscalls: Error reading /proc/%d/sysent", pi->pid);
870 }
871
872 /* Find maximum syscall number. This may not be the same as
873 pr_nsyscalls since that value refers to the number of entries
874 in the table. (Also, the docs indicate that some system
875 call numbers may be skipped.) */
876
877 maxcall = syscalls[0].pr_number;
878
879 for (i = 1; i < header.pr_nsyscalls; i++)
880 if (syscalls[i].pr_number > maxcall
881 && syscalls[i].pr_nameoff > 0
882 && syscalls[i].pr_number < MAX_SYSCALLS)
883 maxcall = syscalls[i].pr_number;
884
885 pi->num_syscalls = maxcall+1;
886 pi->syscall_names = xmalloc (pi->num_syscalls * sizeof (char *));
887
888 for (i = 0; i < pi->num_syscalls; i++)
889 pi->syscall_names[i] = NULL;
890
891 /* Read the syscall names in */
892 for (i = 0; i < header.pr_nsyscalls; i++)
893 {
894 char namebuf[MAX_SYSCALL_NAME_LENGTH];
895 int nread;
896 int callnum;
897
898 if (syscalls[i].pr_number >= MAX_SYSCALLS
899 || syscalls[i].pr_number < 0
900 || syscalls[i].pr_nameoff <= 0
901 || (lseek (sysent_fd, (off_t) syscalls[i].pr_nameoff, SEEK_SET)
902 != (off_t) syscalls[i].pr_nameoff))
903 continue;
904
905 nread = read (sysent_fd, namebuf, sizeof namebuf);
906 if (nread <= 0)
907 continue;
908
909 callnum = syscalls[i].pr_number;
910
911 if (pi->syscall_names[callnum] != NULL)
912 {
913 /* FIXME: Generate warning */
914 continue;
915 }
916
917 namebuf[nread-1] = '\0';
918 size = strlen (namebuf) + 1;
919 pi->syscall_names[callnum] = xmalloc (size);
920 strncpy (pi->syscall_names[callnum], namebuf, size-1);
921 pi->syscall_names[callnum][size-1] = '\0';
922 }
923
924 close (sysent_fd);
925 xfree (syscalls);
926 }
927
928 /* Function: free_syscalls
929
930 Free the space allocated for the syscall names from the procinfo
931 structure. */
932
933 static void
934 free_syscalls (procinfo *pi)
935 {
936 if (pi->syscall_names)
937 {
938 int i;
939
940 for (i = 0; i < pi->num_syscalls; i++)
941 if (pi->syscall_names[i] != NULL)
942 xfree (pi->syscall_names[i]);
943
944 xfree (pi->syscall_names);
945 pi->syscall_names = 0;
946 }
947 }
948
949 /* Function: find_syscall
950
951 Given a name, look up (and return) the corresponding syscall number.
952 If no match is found, return -1. */
953
954 static int
955 find_syscall (procinfo *pi, char *name)
956 {
957 int i;
958 for (i = 0; i < pi->num_syscalls; i++)
959 {
960 if (pi->syscall_names[i] && strcmp (name, pi->syscall_names[i]) == 0)
961 return i;
962 }
963 return -1;
964 }
965 #endif
966
967 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
968
969 /* =================== /proc "MODULE" =================== */
970
971 /*
972 * This "module" is the interface layer between the /proc system API
973 * and the gdb target vector functions. This layer consists of
974 * access functions that encapsulate each of the basic operations
975 * that we need to use from the /proc API.
976 *
977 * The main motivation for this layer is to hide the fact that
978 * there are two very different implementations of the /proc API.
979 * Rather than have a bunch of #ifdefs all thru the gdb target vector
980 * functions, we do our best to hide them all in here.
981 */
982
983 int proc_get_status (procinfo * pi);
984 long proc_flags (procinfo * pi);
985 int proc_why (procinfo * pi);
986 int proc_what (procinfo * pi);
987 int proc_set_run_on_last_close (procinfo * pi);
988 int proc_unset_run_on_last_close (procinfo * pi);
989 int proc_set_inherit_on_fork (procinfo * pi);
990 int proc_unset_inherit_on_fork (procinfo * pi);
991 int proc_set_async (procinfo * pi);
992 int proc_unset_async (procinfo * pi);
993 int proc_stop_process (procinfo * pi);
994 int proc_trace_signal (procinfo * pi, int signo);
995 int proc_ignore_signal (procinfo * pi, int signo);
996 int proc_clear_current_fault (procinfo * pi);
997 int proc_set_current_signal (procinfo * pi, int signo);
998 int proc_clear_current_signal (procinfo * pi);
999 int proc_set_gregs (procinfo * pi);
1000 int proc_set_fpregs (procinfo * pi);
1001 int proc_wait_for_stop (procinfo * pi);
1002 int proc_run_process (procinfo * pi, int step, int signo);
1003 int proc_kill (procinfo * pi, int signo);
1004 int proc_parent_pid (procinfo * pi);
1005 int proc_get_nthreads (procinfo * pi);
1006 int proc_get_current_thread (procinfo * pi);
1007 int proc_set_held_signals (procinfo * pi, gdb_sigset_t * sighold);
1008 int proc_set_traced_sysexit (procinfo * pi, sysset_t * sysset);
1009 int proc_set_traced_sysentry (procinfo * pi, sysset_t * sysset);
1010 int proc_set_traced_faults (procinfo * pi, fltset_t * fltset);
1011 int proc_set_traced_signals (procinfo * pi, gdb_sigset_t * sigset);
1012
1013 int proc_update_threads (procinfo * pi);
1014 int proc_iterate_over_threads (procinfo * pi,
1015 int (*func) (procinfo *, procinfo *, void *),
1016 void *ptr);
1017
1018 gdb_gregset_t *proc_get_gregs (procinfo * pi);
1019 gdb_fpregset_t *proc_get_fpregs (procinfo * pi);
1020 sysset_t *proc_get_traced_sysexit (procinfo * pi, sysset_t * save);
1021 sysset_t *proc_get_traced_sysentry (procinfo * pi, sysset_t * save);
1022 fltset_t *proc_get_traced_faults (procinfo * pi, fltset_t * save);
1023 gdb_sigset_t *proc_get_traced_signals (procinfo * pi, gdb_sigset_t * save);
1024 gdb_sigset_t *proc_get_held_signals (procinfo * pi, gdb_sigset_t * save);
1025 gdb_sigset_t *proc_get_pending_signals (procinfo * pi, gdb_sigset_t * save);
1026 gdb_sigaction_t *proc_get_signal_actions (procinfo * pi, gdb_sigaction_t *save);
1027
1028 void proc_warn (procinfo * pi, char *func, int line);
1029 void proc_error (procinfo * pi, char *func, int line);
1030
1031 void
1032 proc_warn (procinfo *pi, char *func, int line)
1033 {
1034 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1035 print_sys_errmsg (errmsg, errno);
1036 }
1037
1038 void
1039 proc_error (procinfo *pi, char *func, int line)
1040 {
1041 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1042 perror_with_name (errmsg);
1043 }
1044
1045 /*
1046 * Function: proc_get_status
1047 *
1048 * Updates the status struct in the procinfo.
1049 * There is a 'valid' flag, to let other functions know when
1050 * this function needs to be called (so the status is only
1051 * read when it is needed). The status file descriptor is
1052 * also only opened when it is needed.
1053 *
1054 * Return: non-zero for success, zero for failure.
1055 */
1056
1057 int
1058 proc_get_status (procinfo *pi)
1059 {
1060 /* Status file descriptor is opened "lazily" */
1061 if (pi->status_fd == 0 &&
1062 open_procinfo_files (pi, FD_STATUS) == 0)
1063 {
1064 pi->status_valid = 0;
1065 return 0;
1066 }
1067
1068 #ifdef NEW_PROC_API
1069 if (lseek (pi->status_fd, 0, SEEK_SET) < 0)
1070 pi->status_valid = 0; /* fail */
1071 else
1072 {
1073 /* Sigh... I have to read a different data structure,
1074 depending on whether this is a main process or an LWP. */
1075 if (pi->tid)
1076 pi->status_valid = (read (pi->status_fd,
1077 (char *) &pi->prstatus.pr_lwp,
1078 sizeof (lwpstatus_t))
1079 == sizeof (lwpstatus_t));
1080 else
1081 {
1082 pi->status_valid = (read (pi->status_fd,
1083 (char *) &pi->prstatus,
1084 sizeof (gdb_prstatus_t))
1085 == sizeof (gdb_prstatus_t));
1086 #if 0 /*def UNIXWARE*/
1087 if (pi->status_valid &&
1088 (pi->prstatus.pr_lwp.pr_flags & PR_ISTOP) &&
1089 pi->prstatus.pr_lwp.pr_why == PR_REQUESTED)
1090 /* Unixware peculiarity -- read the damn thing again! */
1091 pi->status_valid = (read (pi->status_fd,
1092 (char *) &pi->prstatus,
1093 sizeof (gdb_prstatus_t))
1094 == sizeof (gdb_prstatus_t));
1095 #endif /* UNIXWARE */
1096 }
1097 }
1098 #else /* ioctl method */
1099 #ifdef PIOCTSTATUS /* osf */
1100 if (pi->tid == 0) /* main process */
1101 {
1102 /* Just read the danged status. Now isn't that simple? */
1103 pi->status_valid =
1104 (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1105 }
1106 else
1107 {
1108 int win;
1109 struct {
1110 long pr_count;
1111 tid_t pr_error_thread;
1112 struct prstatus status;
1113 } thread_status;
1114
1115 thread_status.pr_count = 1;
1116 thread_status.status.pr_tid = pi->tid;
1117 win = (ioctl (pi->status_fd, PIOCTSTATUS, &thread_status) >= 0);
1118 if (win)
1119 {
1120 memcpy (&pi->prstatus, &thread_status.status,
1121 sizeof (pi->prstatus));
1122 pi->status_valid = 1;
1123 }
1124 }
1125 #else
1126 /* Just read the danged status. Now isn't that simple? */
1127 pi->status_valid = (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1128 #endif
1129 #endif
1130
1131 if (pi->status_valid)
1132 {
1133 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1134 proc_why (pi),
1135 proc_what (pi),
1136 proc_get_current_thread (pi));
1137 }
1138
1139 /* The status struct includes general regs, so mark them valid too */
1140 pi->gregs_valid = pi->status_valid;
1141 #ifdef NEW_PROC_API
1142 /* In the read/write multiple-fd model,
1143 the status struct includes the fp regs too, so mark them valid too */
1144 pi->fpregs_valid = pi->status_valid;
1145 #endif
1146 return pi->status_valid; /* True if success, false if failure. */
1147 }
1148
1149 /*
1150 * Function: proc_flags
1151 *
1152 * returns the process flags (pr_flags field).
1153 */
1154
1155 long
1156 proc_flags (procinfo *pi)
1157 {
1158 if (!pi->status_valid)
1159 if (!proc_get_status (pi))
1160 return 0; /* FIXME: not a good failure value (but what is?) */
1161
1162 #ifdef NEW_PROC_API
1163 # ifdef UNIXWARE
1164 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1165 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1166 The two sets of flags don't overlap. */
1167 return pi->prstatus.pr_flags | pi->prstatus.pr_lwp.pr_flags;
1168 # else
1169 return pi->prstatus.pr_lwp.pr_flags;
1170 # endif
1171 #else
1172 return pi->prstatus.pr_flags;
1173 #endif
1174 }
1175
1176 /*
1177 * Function: proc_why
1178 *
1179 * returns the pr_why field (why the process stopped).
1180 */
1181
1182 int
1183 proc_why (procinfo *pi)
1184 {
1185 if (!pi->status_valid)
1186 if (!proc_get_status (pi))
1187 return 0; /* FIXME: not a good failure value (but what is?) */
1188
1189 #ifdef NEW_PROC_API
1190 return pi->prstatus.pr_lwp.pr_why;
1191 #else
1192 return pi->prstatus.pr_why;
1193 #endif
1194 }
1195
1196 /*
1197 * Function: proc_what
1198 *
1199 * returns the pr_what field (details of why the process stopped).
1200 */
1201
1202 int
1203 proc_what (procinfo *pi)
1204 {
1205 if (!pi->status_valid)
1206 if (!proc_get_status (pi))
1207 return 0; /* FIXME: not a good failure value (but what is?) */
1208
1209 #ifdef NEW_PROC_API
1210 return pi->prstatus.pr_lwp.pr_what;
1211 #else
1212 return pi->prstatus.pr_what;
1213 #endif
1214 }
1215
1216 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1217 /*
1218 * Function: proc_nsysarg
1219 *
1220 * returns the pr_nsysarg field (number of args to the current syscall).
1221 */
1222
1223 int
1224 proc_nsysarg (procinfo *pi)
1225 {
1226 if (!pi->status_valid)
1227 if (!proc_get_status (pi))
1228 return 0;
1229
1230 #ifdef NEW_PROC_API
1231 return pi->prstatus.pr_lwp.pr_nsysarg;
1232 #else
1233 return pi->prstatus.pr_nsysarg;
1234 #endif
1235 }
1236
1237 /*
1238 * Function: proc_sysargs
1239 *
1240 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1241 */
1242
1243 long *
1244 proc_sysargs (procinfo *pi)
1245 {
1246 if (!pi->status_valid)
1247 if (!proc_get_status (pi))
1248 return NULL;
1249
1250 #ifdef NEW_PROC_API
1251 return (long *) &pi->prstatus.pr_lwp.pr_sysarg;
1252 #else
1253 return (long *) &pi->prstatus.pr_sysarg;
1254 #endif
1255 }
1256
1257 /*
1258 * Function: proc_syscall
1259 *
1260 * returns the pr_syscall field (id of current syscall if we are in one).
1261 */
1262
1263 int
1264 proc_syscall (procinfo *pi)
1265 {
1266 if (!pi->status_valid)
1267 if (!proc_get_status (pi))
1268 return 0;
1269
1270 #ifdef NEW_PROC_API
1271 return pi->prstatus.pr_lwp.pr_syscall;
1272 #else
1273 return pi->prstatus.pr_syscall;
1274 #endif
1275 }
1276 #endif /* PIOCSSPCACT */
1277
1278 /*
1279 * Function: proc_cursig:
1280 *
1281 * returns the pr_cursig field (current signal).
1282 */
1283
1284 long
1285 proc_cursig (struct procinfo *pi)
1286 {
1287 if (!pi->status_valid)
1288 if (!proc_get_status (pi))
1289 return 0; /* FIXME: not a good failure value (but what is?) */
1290
1291 #ifdef NEW_PROC_API
1292 return pi->prstatus.pr_lwp.pr_cursig;
1293 #else
1294 return pi->prstatus.pr_cursig;
1295 #endif
1296 }
1297
1298 /*
1299 * Function: proc_modify_flag
1300 *
1301 * === I appologize for the messiness of this function.
1302 * === This is an area where the different versions of
1303 * === /proc are more inconsistent than usual. MVS
1304 *
1305 * Set or reset any of the following process flags:
1306 * PR_FORK -- forked child will inherit trace flags
1307 * PR_RLC -- traced process runs when last /proc file closed.
1308 * PR_KLC -- traced process is killed when last /proc file closed.
1309 * PR_ASYNC -- LWP's get to run/stop independently.
1310 *
1311 * There are three methods for doing this function:
1312 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1313 * [Sol6, Sol7, UW]
1314 * 2) Middle: PIOCSET/PIOCRESET
1315 * [Irix, Sol5]
1316 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1317 * [OSF, Sol5]
1318 *
1319 * Note: Irix does not define PR_ASYNC.
1320 * Note: OSF does not define PR_KLC.
1321 * Note: OSF is the only one that can ONLY use the oldest method.
1322 *
1323 * Arguments:
1324 * pi -- the procinfo
1325 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1326 * mode -- 1 for set, 0 for reset.
1327 *
1328 * Returns non-zero for success, zero for failure.
1329 */
1330
1331 enum { FLAG_RESET, FLAG_SET };
1332
1333 static int
1334 proc_modify_flag (procinfo *pi, long flag, long mode)
1335 {
1336 long win = 0; /* default to fail */
1337
1338 /*
1339 * These operations affect the process as a whole, and applying
1340 * them to an individual LWP has the same meaning as applying them
1341 * to the main process. Therefore, if we're ever called with a
1342 * pointer to an LWP's procinfo, let's substitute the process's
1343 * procinfo and avoid opening the LWP's file descriptor
1344 * unnecessarily.
1345 */
1346
1347 if (pi->pid != 0)
1348 pi = find_procinfo_or_die (pi->pid, 0);
1349
1350 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1351 /* First normalize the PCUNSET/PCRESET command opcode
1352 (which for no obvious reason has a different definition
1353 from one operating system to the next...) */
1354 #ifdef PCUNSET
1355 #define GDBRESET PCUNSET
1356 #else
1357 #ifdef PCRESET
1358 #define GDBRESET PCRESET
1359 #endif
1360 #endif
1361 {
1362 procfs_ctl_t arg[2];
1363
1364 if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC) */
1365 arg[0] = PCSET;
1366 else /* Reset the flag */
1367 arg[0] = GDBRESET;
1368
1369 arg[1] = flag;
1370 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1371 }
1372 #else
1373 #ifdef PIOCSET /* Irix/Sol5 method */
1374 if (mode == FLAG_SET) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1375 {
1376 win = (ioctl (pi->ctl_fd, PIOCSET, &flag) >= 0);
1377 }
1378 else /* Reset the flag */
1379 {
1380 win = (ioctl (pi->ctl_fd, PIOCRESET, &flag) >= 0);
1381 }
1382
1383 #else
1384 #ifdef PIOCSRLC /* Oldest method: OSF */
1385 switch (flag) {
1386 case PR_RLC:
1387 if (mode == FLAG_SET) /* Set run-on-last-close */
1388 {
1389 win = (ioctl (pi->ctl_fd, PIOCSRLC, NULL) >= 0);
1390 }
1391 else /* Clear run-on-last-close */
1392 {
1393 win = (ioctl (pi->ctl_fd, PIOCRRLC, NULL) >= 0);
1394 }
1395 break;
1396 case PR_FORK:
1397 if (mode == FLAG_SET) /* Set inherit-on-fork */
1398 {
1399 win = (ioctl (pi->ctl_fd, PIOCSFORK, NULL) >= 0);
1400 }
1401 else /* Clear inherit-on-fork */
1402 {
1403 win = (ioctl (pi->ctl_fd, PIOCRFORK, NULL) >= 0);
1404 }
1405 break;
1406 default:
1407 win = 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1408 break;
1409 }
1410 #endif
1411 #endif
1412 #endif
1413 #undef GDBRESET
1414 /* The above operation renders the procinfo's cached pstatus obsolete. */
1415 pi->status_valid = 0;
1416
1417 if (!win)
1418 warning ("procfs: modify_flag failed to turn %s %s",
1419 flag == PR_FORK ? "PR_FORK" :
1420 flag == PR_RLC ? "PR_RLC" :
1421 #ifdef PR_ASYNC
1422 flag == PR_ASYNC ? "PR_ASYNC" :
1423 #endif
1424 #ifdef PR_KLC
1425 flag == PR_KLC ? "PR_KLC" :
1426 #endif
1427 "<unknown flag>",
1428 mode == FLAG_RESET ? "off" : "on");
1429
1430 return win;
1431 }
1432
1433 /*
1434 * Function: proc_set_run_on_last_close
1435 *
1436 * Set the run_on_last_close flag.
1437 * Process with all threads will become runnable
1438 * when debugger closes all /proc fds.
1439 *
1440 * Returns non-zero for success, zero for failure.
1441 */
1442
1443 int
1444 proc_set_run_on_last_close (procinfo *pi)
1445 {
1446 return proc_modify_flag (pi, PR_RLC, FLAG_SET);
1447 }
1448
1449 /*
1450 * Function: proc_unset_run_on_last_close
1451 *
1452 * Reset the run_on_last_close flag.
1453 * Process will NOT become runnable
1454 * when debugger closes its file handles.
1455 *
1456 * Returns non-zero for success, zero for failure.
1457 */
1458
1459 int
1460 proc_unset_run_on_last_close (procinfo *pi)
1461 {
1462 return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
1463 }
1464
1465 #ifdef PR_KLC
1466 /*
1467 * Function: proc_set_kill_on_last_close
1468 *
1469 * Set the kill_on_last_close flag.
1470 * Process with all threads will be killed when debugger
1471 * closes all /proc fds (or debugger exits or dies).
1472 *
1473 * Returns non-zero for success, zero for failure.
1474 */
1475
1476 int
1477 proc_set_kill_on_last_close (procinfo *pi)
1478 {
1479 return proc_modify_flag (pi, PR_KLC, FLAG_SET);
1480 }
1481
1482 /*
1483 * Function: proc_unset_kill_on_last_close
1484 *
1485 * Reset the kill_on_last_close flag.
1486 * Process will NOT be killed when debugger
1487 * closes its file handles (or exits or dies).
1488 *
1489 * Returns non-zero for success, zero for failure.
1490 */
1491
1492 int
1493 proc_unset_kill_on_last_close (procinfo *pi)
1494 {
1495 return proc_modify_flag (pi, PR_KLC, FLAG_RESET);
1496 }
1497 #endif /* PR_KLC */
1498
1499 /*
1500 * Function: proc_set_inherit_on_fork
1501 *
1502 * Set inherit_on_fork flag.
1503 * If the process forks a child while we are registered for events
1504 * in the parent, then we will also recieve events from the child.
1505 *
1506 * Returns non-zero for success, zero for failure.
1507 */
1508
1509 int
1510 proc_set_inherit_on_fork (procinfo *pi)
1511 {
1512 return proc_modify_flag (pi, PR_FORK, FLAG_SET);
1513 }
1514
1515 /*
1516 * Function: proc_unset_inherit_on_fork
1517 *
1518 * Reset inherit_on_fork flag.
1519 * If the process forks a child while we are registered for events
1520 * in the parent, then we will NOT recieve events from the child.
1521 *
1522 * Returns non-zero for success, zero for failure.
1523 */
1524
1525 int
1526 proc_unset_inherit_on_fork (procinfo *pi)
1527 {
1528 return proc_modify_flag (pi, PR_FORK, FLAG_RESET);
1529 }
1530
1531 #ifdef PR_ASYNC
1532 /*
1533 * Function: proc_set_async
1534 *
1535 * Set PR_ASYNC flag.
1536 * If one LWP stops because of a debug event (signal etc.),
1537 * the remaining LWPs will continue to run.
1538 *
1539 * Returns non-zero for success, zero for failure.
1540 */
1541
1542 int
1543 proc_set_async (procinfo *pi)
1544 {
1545 return proc_modify_flag (pi, PR_ASYNC, FLAG_SET);
1546 }
1547
1548 /*
1549 * Function: proc_unset_async
1550 *
1551 * Reset PR_ASYNC flag.
1552 * If one LWP stops because of a debug event (signal etc.),
1553 * then all other LWPs will stop as well.
1554 *
1555 * Returns non-zero for success, zero for failure.
1556 */
1557
1558 int
1559 proc_unset_async (procinfo *pi)
1560 {
1561 return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET);
1562 }
1563 #endif /* PR_ASYNC */
1564
1565 /*
1566 * Function: proc_stop_process
1567 *
1568 * Request the process/LWP to stop. Does not wait.
1569 * Returns non-zero for success, zero for failure.
1570 */
1571
1572 int
1573 proc_stop_process (procinfo *pi)
1574 {
1575 int win;
1576
1577 /*
1578 * We might conceivably apply this operation to an LWP, and
1579 * the LWP's ctl file descriptor might not be open.
1580 */
1581
1582 if (pi->ctl_fd == 0 &&
1583 open_procinfo_files (pi, FD_CTL) == 0)
1584 return 0;
1585 else
1586 {
1587 #ifdef NEW_PROC_API
1588 procfs_ctl_t cmd = PCSTOP;
1589 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1590 #else /* ioctl method */
1591 win = (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) >= 0);
1592 /* Note: the call also reads the prstatus. */
1593 if (win)
1594 {
1595 pi->status_valid = 1;
1596 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1597 proc_why (pi),
1598 proc_what (pi),
1599 proc_get_current_thread (pi));
1600 }
1601 #endif
1602 }
1603
1604 return win;
1605 }
1606
1607 /*
1608 * Function: proc_wait_for_stop
1609 *
1610 * Wait for the process or LWP to stop (block until it does).
1611 * Returns non-zero for success, zero for failure.
1612 */
1613
1614 int
1615 proc_wait_for_stop (procinfo *pi)
1616 {
1617 int win;
1618
1619 /*
1620 * We should never have to apply this operation to any procinfo
1621 * except the one for the main process. If that ever changes
1622 * for any reason, then take out the following clause and
1623 * replace it with one that makes sure the ctl_fd is open.
1624 */
1625
1626 if (pi->tid != 0)
1627 pi = find_procinfo_or_die (pi->pid, 0);
1628
1629 #ifdef NEW_PROC_API
1630 {
1631 procfs_ctl_t cmd = PCWSTOP;
1632 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1633 /* We been runnin' and we stopped -- need to update status. */
1634 pi->status_valid = 0;
1635 }
1636 #else /* ioctl method */
1637 win = (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) >= 0);
1638 /* Above call also refreshes the prstatus. */
1639 if (win)
1640 {
1641 pi->status_valid = 1;
1642 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1643 proc_why (pi),
1644 proc_what (pi),
1645 proc_get_current_thread (pi));
1646 }
1647 #endif
1648
1649 return win;
1650 }
1651
1652 /*
1653 * Function: proc_run_process
1654 *
1655 * Make the process or LWP runnable.
1656 * Options (not all are implemented):
1657 * - single-step
1658 * - clear current fault
1659 * - clear current signal
1660 * - abort the current system call
1661 * - stop as soon as finished with system call
1662 * - (ioctl): set traced signal set
1663 * - (ioctl): set held signal set
1664 * - (ioctl): set traced fault set
1665 * - (ioctl): set start pc (vaddr)
1666 * Always clear the current fault.
1667 * Clear the current signal if 'signo' is zero.
1668 *
1669 * Arguments:
1670 * pi the process or LWP to operate on.
1671 * step if true, set the process or LWP to trap after one instr.
1672 * signo if zero, clear the current signal if any.
1673 * if non-zero, set the current signal to this one.
1674 *
1675 * Returns non-zero for success, zero for failure.
1676 */
1677
1678 int
1679 proc_run_process (procinfo *pi, int step, int signo)
1680 {
1681 int win;
1682 int runflags;
1683
1684 /*
1685 * We will probably have to apply this operation to individual threads,
1686 * so make sure the control file descriptor is open.
1687 */
1688
1689 if (pi->ctl_fd == 0 &&
1690 open_procinfo_files (pi, FD_CTL) == 0)
1691 {
1692 return 0;
1693 }
1694
1695 runflags = PRCFAULT; /* always clear current fault */
1696 if (step)
1697 runflags |= PRSTEP;
1698 if (signo == 0)
1699 runflags |= PRCSIG;
1700 else if (signo != -1) /* -1 means do nothing W.R.T. signals */
1701 proc_set_current_signal (pi, signo);
1702
1703 #ifdef NEW_PROC_API
1704 {
1705 procfs_ctl_t cmd[2];
1706
1707 cmd[0] = PCRUN;
1708 cmd[1] = runflags;
1709 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1710 }
1711 #else /* ioctl method */
1712 {
1713 prrun_t prrun;
1714
1715 memset (&prrun, 0, sizeof (prrun));
1716 prrun.pr_flags = runflags;
1717 win = (ioctl (pi->ctl_fd, PIOCRUN, &prrun) >= 0);
1718 }
1719 #endif
1720
1721 return win;
1722 }
1723
1724 /*
1725 * Function: proc_set_traced_signals
1726 *
1727 * Register to trace signals in the process or LWP.
1728 * Returns non-zero for success, zero for failure.
1729 */
1730
1731 int
1732 proc_set_traced_signals (procinfo *pi, gdb_sigset_t *sigset)
1733 {
1734 int win;
1735
1736 /*
1737 * We should never have to apply this operation to any procinfo
1738 * except the one for the main process. If that ever changes
1739 * for any reason, then take out the following clause and
1740 * replace it with one that makes sure the ctl_fd is open.
1741 */
1742
1743 if (pi->tid != 0)
1744 pi = find_procinfo_or_die (pi->pid, 0);
1745
1746 #ifdef NEW_PROC_API
1747 {
1748 struct {
1749 procfs_ctl_t cmd;
1750 /* Use char array to avoid alignment issues. */
1751 char sigset[sizeof (gdb_sigset_t)];
1752 } arg;
1753
1754 arg.cmd = PCSTRACE;
1755 memcpy (&arg.sigset, sigset, sizeof (gdb_sigset_t));
1756
1757 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1758 }
1759 #else /* ioctl method */
1760 win = (ioctl (pi->ctl_fd, PIOCSTRACE, sigset) >= 0);
1761 #endif
1762 /* The above operation renders the procinfo's cached pstatus obsolete. */
1763 pi->status_valid = 0;
1764
1765 if (!win)
1766 warning ("procfs: set_traced_signals failed");
1767 return win;
1768 }
1769
1770 /*
1771 * Function: proc_set_traced_faults
1772 *
1773 * Register to trace hardware faults in the process or LWP.
1774 * Returns non-zero for success, zero for failure.
1775 */
1776
1777 int
1778 proc_set_traced_faults (procinfo *pi, fltset_t *fltset)
1779 {
1780 int win;
1781
1782 /*
1783 * We should never have to apply this operation to any procinfo
1784 * except the one for the main process. If that ever changes
1785 * for any reason, then take out the following clause and
1786 * replace it with one that makes sure the ctl_fd is open.
1787 */
1788
1789 if (pi->tid != 0)
1790 pi = find_procinfo_or_die (pi->pid, 0);
1791
1792 #ifdef NEW_PROC_API
1793 {
1794 struct {
1795 procfs_ctl_t cmd;
1796 /* Use char array to avoid alignment issues. */
1797 char fltset[sizeof (fltset_t)];
1798 } arg;
1799
1800 arg.cmd = PCSFAULT;
1801 memcpy (&arg.fltset, fltset, sizeof (fltset_t));
1802
1803 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1804 }
1805 #else /* ioctl method */
1806 win = (ioctl (pi->ctl_fd, PIOCSFAULT, fltset) >= 0);
1807 #endif
1808 /* The above operation renders the procinfo's cached pstatus obsolete. */
1809 pi->status_valid = 0;
1810
1811 return win;
1812 }
1813
1814 /*
1815 * Function: proc_set_traced_sysentry
1816 *
1817 * Register to trace entry to system calls in the process or LWP.
1818 * Returns non-zero for success, zero for failure.
1819 */
1820
1821 int
1822 proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset)
1823 {
1824 int win;
1825
1826 /*
1827 * We should never have to apply this operation to any procinfo
1828 * except the one for the main process. If that ever changes
1829 * for any reason, then take out the following clause and
1830 * replace it with one that makes sure the ctl_fd is open.
1831 */
1832
1833 if (pi->tid != 0)
1834 pi = find_procinfo_or_die (pi->pid, 0);
1835
1836 #ifdef NEW_PROC_API
1837 {
1838 struct gdb_proc_ctl_pcsentry {
1839 procfs_ctl_t cmd;
1840 /* Use char array to avoid alignment issues. */
1841 char sysset[sizeof (sysset_t)];
1842 } *argp;
1843 int argp_size = sizeof (struct gdb_proc_ctl_pcsentry)
1844 - sizeof (sysset_t)
1845 + sysset_t_size (pi);
1846
1847 argp = xmalloc (argp_size);
1848
1849 argp->cmd = PCSENTRY;
1850 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1851
1852 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1853 xfree (argp);
1854 }
1855 #else /* ioctl method */
1856 win = (ioctl (pi->ctl_fd, PIOCSENTRY, sysset) >= 0);
1857 #endif
1858 /* The above operation renders the procinfo's cached pstatus obsolete. */
1859 pi->status_valid = 0;
1860
1861 return win;
1862 }
1863
1864 /*
1865 * Function: proc_set_traced_sysexit
1866 *
1867 * Register to trace exit from system calls in the process or LWP.
1868 * Returns non-zero for success, zero for failure.
1869 */
1870
1871 int
1872 proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset)
1873 {
1874 int win;
1875
1876 /*
1877 * We should never have to apply this operation to any procinfo
1878 * except the one for the main process. If that ever changes
1879 * for any reason, then take out the following clause and
1880 * replace it with one that makes sure the ctl_fd is open.
1881 */
1882
1883 if (pi->tid != 0)
1884 pi = find_procinfo_or_die (pi->pid, 0);
1885
1886 #ifdef NEW_PROC_API
1887 {
1888 struct gdb_proc_ctl_pcsexit {
1889 procfs_ctl_t cmd;
1890 /* Use char array to avoid alignment issues. */
1891 char sysset[sizeof (sysset_t)];
1892 } *argp;
1893 int argp_size = sizeof (struct gdb_proc_ctl_pcsexit)
1894 - sizeof (sysset_t)
1895 + sysset_t_size (pi);
1896
1897 argp = xmalloc (argp_size);
1898
1899 argp->cmd = PCSEXIT;
1900 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1901
1902 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1903 xfree (argp);
1904 }
1905 #else /* ioctl method */
1906 win = (ioctl (pi->ctl_fd, PIOCSEXIT, sysset) >= 0);
1907 #endif
1908 /* The above operation renders the procinfo's cached pstatus obsolete. */
1909 pi->status_valid = 0;
1910
1911 return win;
1912 }
1913
1914 /*
1915 * Function: proc_set_held_signals
1916 *
1917 * Specify the set of blocked / held signals in the process or LWP.
1918 * Returns non-zero for success, zero for failure.
1919 */
1920
1921 int
1922 proc_set_held_signals (procinfo *pi, gdb_sigset_t *sighold)
1923 {
1924 int win;
1925
1926 /*
1927 * We should never have to apply this operation to any procinfo
1928 * except the one for the main process. If that ever changes
1929 * for any reason, then take out the following clause and
1930 * replace it with one that makes sure the ctl_fd is open.
1931 */
1932
1933 if (pi->tid != 0)
1934 pi = find_procinfo_or_die (pi->pid, 0);
1935
1936 #ifdef NEW_PROC_API
1937 {
1938 struct {
1939 procfs_ctl_t cmd;
1940 /* Use char array to avoid alignment issues. */
1941 char hold[sizeof (gdb_sigset_t)];
1942 } arg;
1943
1944 arg.cmd = PCSHOLD;
1945 memcpy (&arg.hold, sighold, sizeof (gdb_sigset_t));
1946 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1947 }
1948 #else
1949 win = (ioctl (pi->ctl_fd, PIOCSHOLD, sighold) >= 0);
1950 #endif
1951 /* The above operation renders the procinfo's cached pstatus obsolete. */
1952 pi->status_valid = 0;
1953
1954 return win;
1955 }
1956
1957 /*
1958 * Function: proc_get_pending_signals
1959 *
1960 * returns the set of signals that are pending in the process or LWP.
1961 * Will also copy the sigset if 'save' is non-zero.
1962 */
1963
1964 gdb_sigset_t *
1965 proc_get_pending_signals (procinfo *pi, gdb_sigset_t *save)
1966 {
1967 gdb_sigset_t *ret = NULL;
1968
1969 /*
1970 * We should never have to apply this operation to any procinfo
1971 * except the one for the main process. If that ever changes
1972 * for any reason, then take out the following clause and
1973 * replace it with one that makes sure the ctl_fd is open.
1974 */
1975
1976 if (pi->tid != 0)
1977 pi = find_procinfo_or_die (pi->pid, 0);
1978
1979 if (!pi->status_valid)
1980 if (!proc_get_status (pi))
1981 return NULL;
1982
1983 #ifdef NEW_PROC_API
1984 ret = &pi->prstatus.pr_lwp.pr_lwppend;
1985 #else
1986 ret = &pi->prstatus.pr_sigpend;
1987 #endif
1988 if (save && ret)
1989 memcpy (save, ret, sizeof (gdb_sigset_t));
1990
1991 return ret;
1992 }
1993
1994 /*
1995 * Function: proc_get_signal_actions
1996 *
1997 * returns the set of signal actions.
1998 * Will also copy the sigactionset if 'save' is non-zero.
1999 */
2000
2001 gdb_sigaction_t *
2002 proc_get_signal_actions (procinfo *pi, gdb_sigaction_t *save)
2003 {
2004 gdb_sigaction_t *ret = NULL;
2005
2006 /*
2007 * We should never have to apply this operation to any procinfo
2008 * except the one for the main process. If that ever changes
2009 * for any reason, then take out the following clause and
2010 * replace it with one that makes sure the ctl_fd is open.
2011 */
2012
2013 if (pi->tid != 0)
2014 pi = find_procinfo_or_die (pi->pid, 0);
2015
2016 if (!pi->status_valid)
2017 if (!proc_get_status (pi))
2018 return NULL;
2019
2020 #ifdef NEW_PROC_API
2021 ret = &pi->prstatus.pr_lwp.pr_action;
2022 #else
2023 ret = &pi->prstatus.pr_action;
2024 #endif
2025 if (save && ret)
2026 memcpy (save, ret, sizeof (gdb_sigaction_t));
2027
2028 return ret;
2029 }
2030
2031 /*
2032 * Function: proc_get_held_signals
2033 *
2034 * returns the set of signals that are held / blocked.
2035 * Will also copy the sigset if 'save' is non-zero.
2036 */
2037
2038 gdb_sigset_t *
2039 proc_get_held_signals (procinfo *pi, gdb_sigset_t *save)
2040 {
2041 gdb_sigset_t *ret = NULL;
2042
2043 /*
2044 * We should never have to apply this operation to any procinfo
2045 * except the one for the main process. If that ever changes
2046 * for any reason, then take out the following clause and
2047 * replace it with one that makes sure the ctl_fd is open.
2048 */
2049
2050 if (pi->tid != 0)
2051 pi = find_procinfo_or_die (pi->pid, 0);
2052
2053 #ifdef NEW_PROC_API
2054 if (!pi->status_valid)
2055 if (!proc_get_status (pi))
2056 return NULL;
2057
2058 #ifdef UNIXWARE
2059 ret = &pi->prstatus.pr_lwp.pr_context.uc_sigmask;
2060 #else
2061 ret = &pi->prstatus.pr_lwp.pr_lwphold;
2062 #endif /* UNIXWARE */
2063 #else /* not NEW_PROC_API */
2064 {
2065 static gdb_sigset_t sigheld;
2066
2067 if (ioctl (pi->ctl_fd, PIOCGHOLD, &sigheld) >= 0)
2068 ret = &sigheld;
2069 }
2070 #endif /* NEW_PROC_API */
2071 if (save && ret)
2072 memcpy (save, ret, sizeof (gdb_sigset_t));
2073
2074 return ret;
2075 }
2076
2077 /*
2078 * Function: proc_get_traced_signals
2079 *
2080 * returns the set of signals that are traced / debugged.
2081 * Will also copy the sigset if 'save' is non-zero.
2082 */
2083
2084 gdb_sigset_t *
2085 proc_get_traced_signals (procinfo *pi, gdb_sigset_t *save)
2086 {
2087 gdb_sigset_t *ret = NULL;
2088
2089 /*
2090 * We should never have to apply this operation to any procinfo
2091 * except the one for the main process. If that ever changes
2092 * for any reason, then take out the following clause and
2093 * replace it with one that makes sure the ctl_fd is open.
2094 */
2095
2096 if (pi->tid != 0)
2097 pi = find_procinfo_or_die (pi->pid, 0);
2098
2099 #ifdef NEW_PROC_API
2100 if (!pi->status_valid)
2101 if (!proc_get_status (pi))
2102 return NULL;
2103
2104 ret = &pi->prstatus.pr_sigtrace;
2105 #else
2106 {
2107 static gdb_sigset_t sigtrace;
2108
2109 if (ioctl (pi->ctl_fd, PIOCGTRACE, &sigtrace) >= 0)
2110 ret = &sigtrace;
2111 }
2112 #endif
2113 if (save && ret)
2114 memcpy (save, ret, sizeof (gdb_sigset_t));
2115
2116 return ret;
2117 }
2118
2119 /*
2120 * Function: proc_trace_signal
2121 *
2122 * Add 'signo' to the set of signals that are traced.
2123 * Returns non-zero for success, zero for failure.
2124 */
2125
2126 int
2127 proc_trace_signal (procinfo *pi, int signo)
2128 {
2129 gdb_sigset_t temp;
2130
2131 /*
2132 * We should never have to apply this operation to any procinfo
2133 * except the one for the main process. If that ever changes
2134 * for any reason, then take out the following clause and
2135 * replace it with one that makes sure the ctl_fd is open.
2136 */
2137
2138 if (pi->tid != 0)
2139 pi = find_procinfo_or_die (pi->pid, 0);
2140
2141 if (pi)
2142 {
2143 if (proc_get_traced_signals (pi, &temp))
2144 {
2145 praddset (&temp, signo);
2146 return proc_set_traced_signals (pi, &temp);
2147 }
2148 }
2149
2150 return 0; /* failure */
2151 }
2152
2153 /*
2154 * Function: proc_ignore_signal
2155 *
2156 * Remove 'signo' from the set of signals that are traced.
2157 * Returns non-zero for success, zero for failure.
2158 */
2159
2160 int
2161 proc_ignore_signal (procinfo *pi, int signo)
2162 {
2163 gdb_sigset_t temp;
2164
2165 /*
2166 * We should never have to apply this operation to any procinfo
2167 * except the one for the main process. If that ever changes
2168 * for any reason, then take out the following clause and
2169 * replace it with one that makes sure the ctl_fd is open.
2170 */
2171
2172 if (pi->tid != 0)
2173 pi = find_procinfo_or_die (pi->pid, 0);
2174
2175 if (pi)
2176 {
2177 if (proc_get_traced_signals (pi, &temp))
2178 {
2179 prdelset (&temp, signo);
2180 return proc_set_traced_signals (pi, &temp);
2181 }
2182 }
2183
2184 return 0; /* failure */
2185 }
2186
2187 /*
2188 * Function: proc_get_traced_faults
2189 *
2190 * returns the set of hardware faults that are traced /debugged.
2191 * Will also copy the faultset if 'save' is non-zero.
2192 */
2193
2194 fltset_t *
2195 proc_get_traced_faults (procinfo *pi, fltset_t *save)
2196 {
2197 fltset_t *ret = NULL;
2198
2199 /*
2200 * We should never have to apply this operation to any procinfo
2201 * except the one for the main process. If that ever changes
2202 * for any reason, then take out the following clause and
2203 * replace it with one that makes sure the ctl_fd is open.
2204 */
2205
2206 if (pi->tid != 0)
2207 pi = find_procinfo_or_die (pi->pid, 0);
2208
2209 #ifdef NEW_PROC_API
2210 if (!pi->status_valid)
2211 if (!proc_get_status (pi))
2212 return NULL;
2213
2214 ret = &pi->prstatus.pr_flttrace;
2215 #else
2216 {
2217 static fltset_t flttrace;
2218
2219 if (ioctl (pi->ctl_fd, PIOCGFAULT, &flttrace) >= 0)
2220 ret = &flttrace;
2221 }
2222 #endif
2223 if (save && ret)
2224 memcpy (save, ret, sizeof (fltset_t));
2225
2226 return ret;
2227 }
2228
2229 /*
2230 * Function: proc_get_traced_sysentry
2231 *
2232 * returns the set of syscalls that are traced /debugged on entry.
2233 * Will also copy the syscall set if 'save' is non-zero.
2234 */
2235
2236 sysset_t *
2237 proc_get_traced_sysentry (procinfo *pi, sysset_t *save)
2238 {
2239 sysset_t *ret = NULL;
2240
2241 /*
2242 * We should never have to apply this operation to any procinfo
2243 * except the one for the main process. If that ever changes
2244 * for any reason, then take out the following clause and
2245 * replace it with one that makes sure the ctl_fd is open.
2246 */
2247
2248 if (pi->tid != 0)
2249 pi = find_procinfo_or_die (pi->pid, 0);
2250
2251 #ifdef NEW_PROC_API
2252 if (!pi->status_valid)
2253 if (!proc_get_status (pi))
2254 return NULL;
2255
2256 #ifndef DYNAMIC_SYSCALLS
2257 ret = &pi->prstatus.pr_sysentry;
2258 #else /* DYNAMIC_SYSCALLS */
2259 {
2260 static sysset_t *sysentry;
2261 size_t size;
2262
2263 if (!sysentry)
2264 sysentry = sysset_t_alloc (pi);
2265 ret = sysentry;
2266 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2267 return NULL;
2268 if (pi->prstatus.pr_sysentry_offset == 0)
2269 {
2270 gdb_premptysysset (sysentry);
2271 }
2272 else
2273 {
2274 int rsize;
2275
2276 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysentry_offset,
2277 SEEK_SET)
2278 != (off_t) pi->prstatus.pr_sysentry_offset)
2279 return NULL;
2280 size = sysset_t_size (pi);
2281 gdb_premptysysset (sysentry);
2282 rsize = read (pi->status_fd, sysentry, size);
2283 if (rsize < 0)
2284 return NULL;
2285 }
2286 }
2287 #endif /* DYNAMIC_SYSCALLS */
2288 #else /* !NEW_PROC_API */
2289 {
2290 static sysset_t sysentry;
2291
2292 if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysentry) >= 0)
2293 ret = &sysentry;
2294 }
2295 #endif /* NEW_PROC_API */
2296 if (save && ret)
2297 memcpy (save, ret, sysset_t_size (pi));
2298
2299 return ret;
2300 }
2301
2302 /*
2303 * Function: proc_get_traced_sysexit
2304 *
2305 * returns the set of syscalls that are traced /debugged on exit.
2306 * Will also copy the syscall set if 'save' is non-zero.
2307 */
2308
2309 sysset_t *
2310 proc_get_traced_sysexit (procinfo *pi, sysset_t *save)
2311 {
2312 sysset_t * ret = NULL;
2313
2314 /*
2315 * We should never have to apply this operation to any procinfo
2316 * except the one for the main process. If that ever changes
2317 * for any reason, then take out the following clause and
2318 * replace it with one that makes sure the ctl_fd is open.
2319 */
2320
2321 if (pi->tid != 0)
2322 pi = find_procinfo_or_die (pi->pid, 0);
2323
2324 #ifdef NEW_PROC_API
2325 if (!pi->status_valid)
2326 if (!proc_get_status (pi))
2327 return NULL;
2328
2329 #ifndef DYNAMIC_SYSCALLS
2330 ret = &pi->prstatus.pr_sysexit;
2331 #else /* DYNAMIC_SYSCALLS */
2332 {
2333 static sysset_t *sysexit;
2334 size_t size;
2335
2336 if (!sysexit)
2337 sysexit = sysset_t_alloc (pi);
2338 ret = sysexit;
2339 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2340 return NULL;
2341 if (pi->prstatus.pr_sysexit_offset == 0)
2342 {
2343 gdb_premptysysset (sysexit);
2344 }
2345 else
2346 {
2347 int rsize;
2348
2349 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysexit_offset, SEEK_SET)
2350 != (off_t) pi->prstatus.pr_sysexit_offset)
2351 return NULL;
2352 size = sysset_t_size (pi);
2353 gdb_premptysysset (sysexit);
2354 rsize = read (pi->status_fd, sysexit, size);
2355 if (rsize < 0)
2356 return NULL;
2357 }
2358 }
2359 #endif /* DYNAMIC_SYSCALLS */
2360 #else
2361 {
2362 static sysset_t sysexit;
2363
2364 if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysexit) >= 0)
2365 ret = &sysexit;
2366 }
2367 #endif
2368 if (save && ret)
2369 memcpy (save, ret, sysset_t_size (pi));
2370
2371 return ret;
2372 }
2373
2374 /*
2375 * Function: proc_clear_current_fault
2376 *
2377 * The current fault (if any) is cleared; the associated signal
2378 * will not be sent to the process or LWP when it resumes.
2379 * Returns non-zero for success, zero for failure.
2380 */
2381
2382 int
2383 proc_clear_current_fault (procinfo *pi)
2384 {
2385 int win;
2386
2387 /*
2388 * We should never have to apply this operation to any procinfo
2389 * except the one for the main process. If that ever changes
2390 * for any reason, then take out the following clause and
2391 * replace it with one that makes sure the ctl_fd is open.
2392 */
2393
2394 if (pi->tid != 0)
2395 pi = find_procinfo_or_die (pi->pid, 0);
2396
2397 #ifdef NEW_PROC_API
2398 {
2399 procfs_ctl_t cmd = PCCFAULT;
2400 win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd));
2401 }
2402 #else
2403 win = (ioctl (pi->ctl_fd, PIOCCFAULT, 0) >= 0);
2404 #endif
2405
2406 return win;
2407 }
2408
2409 /*
2410 * Function: proc_set_current_signal
2411 *
2412 * Set the "current signal" that will be delivered next to the process.
2413 * NOTE: semantics are different from those of KILL.
2414 * This signal will be delivered to the process or LWP
2415 * immediately when it is resumed (even if the signal is held/blocked);
2416 * it will NOT immediately cause another event of interest, and will NOT
2417 * first trap back to the debugger.
2418 *
2419 * Returns non-zero for success, zero for failure.
2420 */
2421
2422 int
2423 proc_set_current_signal (procinfo *pi, int signo)
2424 {
2425 int win;
2426 struct {
2427 procfs_ctl_t cmd;
2428 /* Use char array to avoid alignment issues. */
2429 char sinfo[sizeof (gdb_siginfo_t)];
2430 } arg;
2431 gdb_siginfo_t *mysinfo;
2432
2433 /*
2434 * We should never have to apply this operation to any procinfo
2435 * except the one for the main process. If that ever changes
2436 * for any reason, then take out the following clause and
2437 * replace it with one that makes sure the ctl_fd is open.
2438 */
2439
2440 if (pi->tid != 0)
2441 pi = find_procinfo_or_die (pi->pid, 0);
2442
2443 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2444 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2445 * receives a PIOCSSIG with a signal identical to the current signal,
2446 * it messes up the current signal. Work around the kernel bug.
2447 */
2448 if (signo > 0 &&
2449 signo == proc_cursig (pi))
2450 return 1; /* I assume this is a success? */
2451 #endif
2452
2453 /* The pointer is just a type alias. */
2454 mysinfo = (gdb_siginfo_t *) &arg.sinfo;
2455 mysinfo->si_signo = signo;
2456 mysinfo->si_code = 0;
2457 mysinfo->si_pid = getpid (); /* ?why? */
2458 mysinfo->si_uid = getuid (); /* ?why? */
2459
2460 #ifdef NEW_PROC_API
2461 arg.cmd = PCSSIG;
2462 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2463 #else
2464 win = (ioctl (pi->ctl_fd, PIOCSSIG, (void *) &arg.sinfo) >= 0);
2465 #endif
2466
2467 return win;
2468 }
2469
2470 /*
2471 * Function: proc_clear_current_signal
2472 *
2473 * The current signal (if any) is cleared, and
2474 * is not sent to the process or LWP when it resumes.
2475 * Returns non-zero for success, zero for failure.
2476 */
2477
2478 int
2479 proc_clear_current_signal (procinfo *pi)
2480 {
2481 int win;
2482
2483 /*
2484 * We should never have to apply this operation to any procinfo
2485 * except the one for the main process. If that ever changes
2486 * for any reason, then take out the following clause and
2487 * replace it with one that makes sure the ctl_fd is open.
2488 */
2489
2490 if (pi->tid != 0)
2491 pi = find_procinfo_or_die (pi->pid, 0);
2492
2493 #ifdef NEW_PROC_API
2494 {
2495 struct {
2496 procfs_ctl_t cmd;
2497 /* Use char array to avoid alignment issues. */
2498 char sinfo[sizeof (gdb_siginfo_t)];
2499 } arg;
2500 gdb_siginfo_t *mysinfo;
2501
2502 arg.cmd = PCSSIG;
2503 /* The pointer is just a type alias. */
2504 mysinfo = (gdb_siginfo_t *) &arg.sinfo;
2505 mysinfo->si_signo = 0;
2506 mysinfo->si_code = 0;
2507 mysinfo->si_errno = 0;
2508 mysinfo->si_pid = getpid (); /* ?why? */
2509 mysinfo->si_uid = getuid (); /* ?why? */
2510
2511 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2512 }
2513 #else
2514 win = (ioctl (pi->ctl_fd, PIOCSSIG, 0) >= 0);
2515 #endif
2516
2517 return win;
2518 }
2519
2520 /*
2521 * Function: proc_get_gregs
2522 *
2523 * Get the general registers for the process or LWP.
2524 * Returns non-zero for success, zero for failure.
2525 */
2526
2527 gdb_gregset_t *
2528 proc_get_gregs (procinfo *pi)
2529 {
2530 if (!pi->status_valid || !pi->gregs_valid)
2531 if (!proc_get_status (pi))
2532 return NULL;
2533
2534 /*
2535 * OK, sorry about the ifdef's.
2536 * There's three cases instead of two, because
2537 * in this instance Unixware and Solaris/RW differ.
2538 */
2539
2540 #ifdef NEW_PROC_API
2541 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2542 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs;
2543 #else /* not Unixware */
2544 return &pi->prstatus.pr_lwp.pr_reg;
2545 #endif /* Unixware */
2546 #else /* not NEW_PROC_API */
2547 return &pi->prstatus.pr_reg;
2548 #endif /* NEW_PROC_API */
2549 }
2550
2551 /*
2552 * Function: proc_get_fpregs
2553 *
2554 * Get the floating point registers for the process or LWP.
2555 * Returns non-zero for success, zero for failure.
2556 */
2557
2558 gdb_fpregset_t *
2559 proc_get_fpregs (procinfo *pi)
2560 {
2561 #ifdef NEW_PROC_API
2562 if (!pi->status_valid || !pi->fpregs_valid)
2563 if (!proc_get_status (pi))
2564 return NULL;
2565
2566 #ifdef UNIXWARE /* a true architecture dependency */
2567 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs;
2568 #else
2569 return &pi->prstatus.pr_lwp.pr_fpreg;
2570 #endif /* Unixware */
2571
2572 #else /* not NEW_PROC_API */
2573 if (pi->fpregs_valid)
2574 return &pi->fpregset; /* already got 'em */
2575 else
2576 {
2577 if (pi->ctl_fd == 0 &&
2578 open_procinfo_files (pi, FD_CTL) == 0)
2579 {
2580 return NULL;
2581 }
2582 else
2583 {
2584 #ifdef PIOCTGFPREG
2585 struct {
2586 long pr_count;
2587 tid_t pr_error_thread;
2588 tfpregset_t thread_1;
2589 } thread_fpregs;
2590
2591 thread_fpregs.pr_count = 1;
2592 thread_fpregs.thread_1.tid = pi->tid;
2593
2594 if (pi->tid == 0 &&
2595 ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2596 {
2597 pi->fpregs_valid = 1;
2598 return &pi->fpregset; /* got 'em now! */
2599 }
2600 else if (pi->tid != 0 &&
2601 ioctl (pi->ctl_fd, PIOCTGFPREG, &thread_fpregs) >= 0)
2602 {
2603 memcpy (&pi->fpregset, &thread_fpregs.thread_1.pr_fpregs,
2604 sizeof (pi->fpregset));
2605 pi->fpregs_valid = 1;
2606 return &pi->fpregset; /* got 'em now! */
2607 }
2608 else
2609 {
2610 return NULL;
2611 }
2612 #else
2613 if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2614 {
2615 pi->fpregs_valid = 1;
2616 return &pi->fpregset; /* got 'em now! */
2617 }
2618 else
2619 {
2620 return NULL;
2621 }
2622 #endif
2623 }
2624 }
2625 #endif
2626 }
2627
2628 /*
2629 * Function: proc_set_gregs
2630 *
2631 * Write the general registers back to the process or LWP.
2632 * Returns non-zero for success, zero for failure.
2633 */
2634
2635 int
2636 proc_set_gregs (procinfo *pi)
2637 {
2638 gdb_gregset_t *gregs;
2639 int win;
2640
2641 if ((gregs = proc_get_gregs (pi)) == NULL)
2642 return 0; /* get_regs has already warned */
2643
2644 if (pi->ctl_fd == 0 &&
2645 open_procinfo_files (pi, FD_CTL) == 0)
2646 {
2647 return 0;
2648 }
2649 else
2650 {
2651 #ifdef NEW_PROC_API
2652 struct {
2653 procfs_ctl_t cmd;
2654 /* Use char array to avoid alignment issues. */
2655 char gregs[sizeof (gdb_gregset_t)];
2656 } arg;
2657
2658 arg.cmd = PCSREG;
2659 memcpy (&arg.gregs, gregs, sizeof (arg.gregs));
2660 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2661 #else
2662 win = (ioctl (pi->ctl_fd, PIOCSREG, gregs) >= 0);
2663 #endif
2664 }
2665
2666 /* Policy: writing the regs invalidates our cache. */
2667 pi->gregs_valid = 0;
2668 return win;
2669 }
2670
2671 /*
2672 * Function: proc_set_fpregs
2673 *
2674 * Modify the floating point register set of the process or LWP.
2675 * Returns non-zero for success, zero for failure.
2676 */
2677
2678 int
2679 proc_set_fpregs (procinfo *pi)
2680 {
2681 gdb_fpregset_t *fpregs;
2682 int win;
2683
2684 if ((fpregs = proc_get_fpregs (pi)) == NULL)
2685 return 0; /* get_fpregs has already warned */
2686
2687 if (pi->ctl_fd == 0 &&
2688 open_procinfo_files (pi, FD_CTL) == 0)
2689 {
2690 return 0;
2691 }
2692 else
2693 {
2694 #ifdef NEW_PROC_API
2695 struct {
2696 procfs_ctl_t cmd;
2697 /* Use char array to avoid alignment issues. */
2698 char fpregs[sizeof (gdb_fpregset_t)];
2699 } arg;
2700
2701 arg.cmd = PCSFPREG;
2702 memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs));
2703 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2704 #else
2705 #ifdef PIOCTSFPREG
2706 if (pi->tid == 0)
2707 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2708 else
2709 {
2710 struct {
2711 long pr_count;
2712 tid_t pr_error_thread;
2713 tfpregset_t thread_1;
2714 } thread_fpregs;
2715
2716 thread_fpregs.pr_count = 1;
2717 thread_fpregs.thread_1.tid = pi->tid;
2718 memcpy (&thread_fpregs.thread_1.pr_fpregs, fpregs,
2719 sizeof (*fpregs));
2720 win = (ioctl (pi->ctl_fd, PIOCTSFPREG, &thread_fpregs) >= 0);
2721 }
2722 #else
2723 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2724 #endif /* osf PIOCTSFPREG */
2725 #endif /* NEW_PROC_API */
2726 }
2727
2728 /* Policy: writing the regs invalidates our cache. */
2729 pi->fpregs_valid = 0;
2730 return win;
2731 }
2732
2733 /*
2734 * Function: proc_kill
2735 *
2736 * Send a signal to the proc or lwp with the semantics of "kill()".
2737 * Returns non-zero for success, zero for failure.
2738 */
2739
2740 int
2741 proc_kill (procinfo *pi, int signo)
2742 {
2743 int win;
2744
2745 /*
2746 * We might conceivably apply this operation to an LWP, and
2747 * the LWP's ctl file descriptor might not be open.
2748 */
2749
2750 if (pi->ctl_fd == 0 &&
2751 open_procinfo_files (pi, FD_CTL) == 0)
2752 {
2753 return 0;
2754 }
2755 else
2756 {
2757 #ifdef NEW_PROC_API
2758 procfs_ctl_t cmd[2];
2759
2760 cmd[0] = PCKILL;
2761 cmd[1] = signo;
2762 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
2763 #else /* ioctl method */
2764 /* FIXME: do I need the Alpha OSF fixups present in
2765 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2766 win = (ioctl (pi->ctl_fd, PIOCKILL, &signo) >= 0);
2767 #endif
2768 }
2769
2770 return win;
2771 }
2772
2773 /*
2774 * Function: proc_parent_pid
2775 *
2776 * Find the pid of the process that started this one.
2777 * Returns the parent process pid, or zero.
2778 */
2779
2780 int
2781 proc_parent_pid (procinfo *pi)
2782 {
2783 /*
2784 * We should never have to apply this operation to any procinfo
2785 * except the one for the main process. If that ever changes
2786 * for any reason, then take out the following clause and
2787 * replace it with one that makes sure the ctl_fd is open.
2788 */
2789
2790 if (pi->tid != 0)
2791 pi = find_procinfo_or_die (pi->pid, 0);
2792
2793 if (!pi->status_valid)
2794 if (!proc_get_status (pi))
2795 return 0;
2796
2797 return pi->prstatus.pr_ppid;
2798 }
2799
2800
2801 /*
2802 * Function: proc_set_watchpoint
2803 *
2804 */
2805
2806 int
2807 proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags)
2808 {
2809 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2810 return 0;
2811 #else
2812 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2813 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2814 return 0;
2815 #else
2816 struct {
2817 procfs_ctl_t cmd;
2818 char watch[sizeof (prwatch_t)];
2819 } arg;
2820 prwatch_t *pwatch;
2821
2822 pwatch = (prwatch_t *) &arg.watch;
2823 pwatch->pr_vaddr = address_to_host_pointer (addr);
2824 pwatch->pr_size = len;
2825 pwatch->pr_wflags = wflags;
2826 #if defined(NEW_PROC_API) && defined (PCWATCH)
2827 arg.cmd = PCWATCH;
2828 return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg));
2829 #else
2830 #if defined (PIOCSWATCH)
2831 return (ioctl (pi->ctl_fd, PIOCSWATCH, pwatch) >= 0);
2832 #else
2833 return 0; /* Fail */
2834 #endif
2835 #endif
2836 #endif
2837 #endif
2838 }
2839
2840 /*
2841 * Function: proc_iterate_over_mappings
2842 *
2843 * Given a pointer to a function, call that function once for every
2844 * mapped address space in the process. The callback function
2845 * receives an open file descriptor for the file corresponding to
2846 * that mapped address space (if there is one), and the base address
2847 * of the mapped space. Quit when the callback function returns a
2848 * nonzero value, or at teh end of the mappings.
2849 *
2850 * Returns: the first non-zero return value of the callback function,
2851 * or zero.
2852 */
2853
2854 /* FIXME: it's probably a waste to cache this FD.
2855 It doesn't get called that often... and if I open it
2856 every time, I don't need to lseek it. */
2857 int
2858 proc_iterate_over_mappings (int (*func) (int, CORE_ADDR))
2859 {
2860 struct prmap *map;
2861 procinfo *pi;
2862 #ifndef NEW_PROC_API /* avoid compiler warning */
2863 int nmaps = 0;
2864 int i;
2865 #else
2866 int map_fd;
2867 char pathname[MAX_PROC_NAME_SIZE];
2868 #endif
2869 int funcstat = 0;
2870 int fd;
2871
2872 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
2873
2874 #ifdef NEW_PROC_API
2875 /* Open map fd. */
2876 sprintf (pathname, "/proc/%d/map", pi->pid);
2877 if ((map_fd = open (pathname, O_RDONLY)) < 0)
2878 proc_error (pi, "proc_iterate_over_mappings (open)", __LINE__);
2879
2880 /* Make sure it gets closed again. */
2881 make_cleanup_close (map_fd);
2882
2883 /* Allocate space for mapping (lifetime only for this function). */
2884 map = alloca (sizeof (struct prmap));
2885
2886 /* Now read the mappings from the file,
2887 open a file descriptor for those that have a name,
2888 and call the callback function. */
2889 while (read (map_fd,
2890 (void *) map,
2891 sizeof (struct prmap)) == sizeof (struct prmap))
2892 {
2893 char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)];
2894
2895 if (map->pr_vaddr == 0 && map->pr_size == 0)
2896 break; /* sanity */
2897
2898 if (map->pr_mapname[0] == 0)
2899 {
2900 fd = -1; /* no map file */
2901 }
2902 else
2903 {
2904 sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname);
2905 /* Note: caller's responsibility to close this fd! */
2906 fd = open (name, O_RDONLY);
2907 /* Note: we don't test the above call for failure;
2908 we just pass the FD on as given. Sometimes there is
2909 no file, so the ioctl may return failure, but that's
2910 not a problem. */
2911 }
2912
2913 /* Stop looping if the callback returns non-zero. */
2914 if ((funcstat = (*func) (fd, (CORE_ADDR) map->pr_vaddr)) != 0)
2915 break;
2916 }
2917 #else
2918 /* Get the number of mapping entries. */
2919 if (ioctl (pi->ctl_fd, PIOCNMAP, &nmaps) < 0)
2920 proc_error (pi, "proc_iterate_over_mappings (PIOCNMAP)", __LINE__);
2921
2922 /* Allocate space for mappings (lifetime only this function). */
2923 map = (struct prmap *) alloca ((nmaps + 1) * sizeof (struct prmap));
2924
2925 /* Read in all the mappings. */
2926 if (ioctl (pi->ctl_fd, PIOCMAP, map) < 0)
2927 proc_error (pi, "proc_iterate_over_mappings (PIOCMAP)", __LINE__);
2928
2929 /* Now loop through the mappings, open an fd for each, and
2930 call the callback function. */
2931 for (i = 0;
2932 i < nmaps && map[i].pr_size != 0;
2933 i++)
2934 {
2935 /* Note: caller's responsibility to close this fd! */
2936 fd = ioctl (pi->ctl_fd, PIOCOPENM, &map[i].pr_vaddr);
2937 /* Note: we don't test the above call for failure;
2938 we just pass the FD on as given. Sometimes there is
2939 no file, so the ioctl may return failure, but that's
2940 not a problem. */
2941
2942 /* Stop looping if the callback returns non-zero. */
2943 funcstat = (*func) (fd, host_pointer_to_address (map[i].pr_vaddr));
2944 if (funcstat != 0)
2945 break;
2946 }
2947 #endif
2948
2949 return funcstat;
2950 }
2951
2952 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2953
2954 #include <sys/sysi86.h>
2955
2956 /*
2957 * Function: proc_get_LDT_entry
2958 *
2959 * Inputs:
2960 * procinfo *pi;
2961 * int key;
2962 *
2963 * The 'key' is actually the value of the lower 16 bits of
2964 * the GS register for the LWP that we're interested in.
2965 *
2966 * Return: matching ssh struct (LDT entry).
2967 */
2968
2969 struct ssd *
2970 proc_get_LDT_entry (procinfo *pi, int key)
2971 {
2972 static struct ssd *ldt_entry = NULL;
2973 #ifdef NEW_PROC_API
2974 char pathname[MAX_PROC_NAME_SIZE];
2975 struct cleanup *old_chain = NULL;
2976 int fd;
2977
2978 /* Allocate space for one LDT entry.
2979 This alloc must persist, because we return a pointer to it. */
2980 if (ldt_entry == NULL)
2981 ldt_entry = (struct ssd *) xmalloc (sizeof (struct ssd));
2982
2983 /* Open the file descriptor for the LDT table. */
2984 sprintf (pathname, "/proc/%d/ldt", pi->pid);
2985 if ((fd = open (pathname, O_RDONLY)) < 0)
2986 {
2987 proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__);
2988 return NULL;
2989 }
2990 /* Make sure it gets closed again! */
2991 old_chain = make_cleanup_close (fd);
2992
2993 /* Now 'read' thru the table, find a match and return it. */
2994 while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd))
2995 {
2996 if (ldt_entry->sel == 0 &&
2997 ldt_entry->bo == 0 &&
2998 ldt_entry->acc1 == 0 &&
2999 ldt_entry->acc2 == 0)
3000 break; /* end of table */
3001 /* If key matches, return this entry. */
3002 if (ldt_entry->sel == key)
3003 return ldt_entry;
3004 }
3005 /* Loop ended, match not found. */
3006 return NULL;
3007 #else
3008 int nldt, i;
3009 static int nalloc = 0;
3010
3011 /* Get the number of LDT entries. */
3012 if (ioctl (pi->ctl_fd, PIOCNLDT, &nldt) < 0)
3013 {
3014 proc_warn (pi, "proc_get_LDT_entry (PIOCNLDT)", __LINE__);
3015 return NULL;
3016 }
3017
3018 /* Allocate space for the number of LDT entries. */
3019 /* This alloc has to persist, 'cause we return a pointer to it. */
3020 if (nldt > nalloc)
3021 {
3022 ldt_entry = (struct ssd *)
3023 xrealloc (ldt_entry, (nldt + 1) * sizeof (struct ssd));
3024 nalloc = nldt;
3025 }
3026
3027 /* Read the whole table in one gulp. */
3028 if (ioctl (pi->ctl_fd, PIOCLDT, ldt_entry) < 0)
3029 {
3030 proc_warn (pi, "proc_get_LDT_entry (PIOCLDT)", __LINE__);
3031 return NULL;
3032 }
3033
3034 /* Search the table and return the (first) entry matching 'key'. */
3035 for (i = 0; i < nldt; i++)
3036 if (ldt_entry[i].sel == key)
3037 return &ldt_entry[i];
3038
3039 /* Loop ended, match not found. */
3040 return NULL;
3041 #endif
3042 }
3043
3044 #endif /* TM_I386SOL2_H */
3045
3046 /* =============== END, non-thread part of /proc "MODULE" =============== */
3047
3048 /* =================== Thread "MODULE" =================== */
3049
3050 /* NOTE: you'll see more ifdefs and duplication of functions here,
3051 since there is a different way to do threads on every OS. */
3052
3053 /*
3054 * Function: proc_get_nthreads
3055 *
3056 * Return the number of threads for the process
3057 */
3058
3059 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3060 /*
3061 * OSF version
3062 */
3063 int
3064 proc_get_nthreads (procinfo *pi)
3065 {
3066 int nthreads = 0;
3067
3068 if (ioctl (pi->ctl_fd, PIOCNTHR, &nthreads) < 0)
3069 proc_warn (pi, "procfs: PIOCNTHR failed", __LINE__);
3070
3071 return nthreads;
3072 }
3073
3074 #else
3075 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3076 /*
3077 * Solaris and Unixware version
3078 */
3079 int
3080 proc_get_nthreads (procinfo *pi)
3081 {
3082 if (!pi->status_valid)
3083 if (!proc_get_status (pi))
3084 return 0;
3085
3086 /*
3087 * NEW_PROC_API: only works for the process procinfo,
3088 * because the LWP procinfos do not get prstatus filled in.
3089 */
3090 #ifdef NEW_PROC_API
3091 if (pi->tid != 0) /* find the parent process procinfo */
3092 pi = find_procinfo_or_die (pi->pid, 0);
3093 #endif
3094 return pi->prstatus.pr_nlwp;
3095 }
3096
3097 #else
3098 /*
3099 * Default version
3100 */
3101 int
3102 proc_get_nthreads (procinfo *pi)
3103 {
3104 return 0;
3105 }
3106 #endif
3107 #endif
3108
3109 /*
3110 * Function: proc_get_current_thread (LWP version)
3111 *
3112 * Return the ID of the thread that had an event of interest.
3113 * (ie. the one that hit a breakpoint or other traced event).
3114 * All other things being equal, this should be the ID of a
3115 * thread that is currently executing.
3116 */
3117
3118 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3119 /*
3120 * Solaris and Unixware version
3121 */
3122 int
3123 proc_get_current_thread (procinfo *pi)
3124 {
3125 /*
3126 * Note: this should be applied to the root procinfo for the process,
3127 * not to the procinfo for an LWP. If applied to the procinfo for
3128 * an LWP, it will simply return that LWP's ID. In that case,
3129 * find the parent process procinfo.
3130 */
3131
3132 if (pi->tid != 0)
3133 pi = find_procinfo_or_die (pi->pid, 0);
3134
3135 if (!pi->status_valid)
3136 if (!proc_get_status (pi))
3137 return 0;
3138
3139 #ifdef NEW_PROC_API
3140 return pi->prstatus.pr_lwp.pr_lwpid;
3141 #else
3142 return pi->prstatus.pr_who;
3143 #endif
3144 }
3145
3146 #else
3147 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3148 /*
3149 * OSF version
3150 */
3151 int
3152 proc_get_current_thread (procinfo *pi)
3153 {
3154 #if 0 /* FIXME: not ready for prime time? */
3155 return pi->prstatus.pr_tid;
3156 #else
3157 return 0;
3158 #endif
3159 }
3160
3161 #else
3162 /*
3163 * Default version
3164 */
3165 int
3166 proc_get_current_thread (procinfo *pi)
3167 {
3168 return 0;
3169 }
3170
3171 #endif
3172 #endif
3173
3174 /*
3175 * Function: proc_update_threads
3176 *
3177 * Discover the IDs of all the threads within the process, and
3178 * create a procinfo for each of them (chained to the parent).
3179 *
3180 * This unfortunately requires a different method on every OS.
3181 *
3182 * Return: non-zero for success, zero for failure.
3183 */
3184
3185 int
3186 proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore)
3187 {
3188 if (thread && parent) /* sanity */
3189 {
3190 thread->status_valid = 0;
3191 if (!proc_get_status (thread))
3192 destroy_one_procinfo (&parent->thread_list, thread);
3193 }
3194 return 0; /* keep iterating */
3195 }
3196
3197 #if defined (PIOCLSTATUS)
3198 /*
3199 * Solaris 2.5 (ioctl) version
3200 */
3201 int
3202 proc_update_threads (procinfo *pi)
3203 {
3204 gdb_prstatus_t *prstatus;
3205 struct cleanup *old_chain = NULL;
3206 procinfo *thread;
3207 int nlwp, i;
3208
3209 /*
3210 * We should never have to apply this operation to any procinfo
3211 * except the one for the main process. If that ever changes
3212 * for any reason, then take out the following clause and
3213 * replace it with one that makes sure the ctl_fd is open.
3214 */
3215
3216 if (pi->tid != 0)
3217 pi = find_procinfo_or_die (pi->pid, 0);
3218
3219 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3220
3221 if ((nlwp = proc_get_nthreads (pi)) <= 1)
3222 return 1; /* Process is not multi-threaded; nothing to do. */
3223
3224 prstatus = xmalloc (sizeof (gdb_prstatus_t) * (nlwp + 1));
3225
3226 old_chain = make_cleanup (xfree, prstatus);
3227 if (ioctl (pi->ctl_fd, PIOCLSTATUS, prstatus) < 0)
3228 proc_error (pi, "update_threads (PIOCLSTATUS)", __LINE__);
3229
3230 /* Skip element zero, which represents the process as a whole. */
3231 for (i = 1; i < nlwp + 1; i++)
3232 {
3233 if ((thread = create_procinfo (pi->pid, prstatus[i].pr_who)) == NULL)
3234 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3235
3236 memcpy (&thread->prstatus, &prstatus[i], sizeof (*prstatus));
3237 thread->status_valid = 1;
3238 }
3239 pi->threads_valid = 1;
3240 do_cleanups (old_chain);
3241 return 1;
3242 }
3243 #else
3244 #ifdef NEW_PROC_API
3245 /*
3246 * Unixware and Solaris 6 (and later) version
3247 */
3248 static void
3249 do_closedir_cleanup (void *dir)
3250 {
3251 closedir (dir);
3252 }
3253
3254 int
3255 proc_update_threads (procinfo *pi)
3256 {
3257 char pathname[MAX_PROC_NAME_SIZE + 16];
3258 struct dirent *direntry;
3259 struct cleanup *old_chain = NULL;
3260 procinfo *thread;
3261 DIR *dirp;
3262 int lwpid;
3263
3264 /*
3265 * We should never have to apply this operation to any procinfo
3266 * except the one for the main process. If that ever changes
3267 * for any reason, then take out the following clause and
3268 * replace it with one that makes sure the ctl_fd is open.
3269 */
3270
3271 if (pi->tid != 0)
3272 pi = find_procinfo_or_die (pi->pid, 0);
3273
3274 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3275
3276 /*
3277 * Unixware
3278 *
3279 * Note: this brute-force method is the only way I know of
3280 * to accomplish this task on Unixware. This method will
3281 * also work on Solaris 2.6 and 2.7. There is a much simpler
3282 * and more elegant way to do this on Solaris, but the margins
3283 * of this manuscript are too small to write it here... ;-)
3284 */
3285
3286 strcpy (pathname, pi->pathname);
3287 strcat (pathname, "/lwp");
3288 if ((dirp = opendir (pathname)) == NULL)
3289 proc_error (pi, "update_threads, opendir", __LINE__);
3290
3291 old_chain = make_cleanup (do_closedir_cleanup, dirp);
3292 while ((direntry = readdir (dirp)) != NULL)
3293 if (direntry->d_name[0] != '.') /* skip '.' and '..' */
3294 {
3295 lwpid = atoi (&direntry->d_name[0]);
3296 if ((thread = create_procinfo (pi->pid, lwpid)) == NULL)
3297 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3298 }
3299 pi->threads_valid = 1;
3300 do_cleanups (old_chain);
3301 return 1;
3302 }
3303 #else
3304 #ifdef PIOCTLIST
3305 /*
3306 * OSF version
3307 */
3308 int
3309 proc_update_threads (procinfo *pi)
3310 {
3311 int nthreads, i;
3312 tid_t *threads;
3313
3314 /*
3315 * We should never have to apply this operation to any procinfo
3316 * except the one for the main process. If that ever changes
3317 * for any reason, then take out the following clause and
3318 * replace it with one that makes sure the ctl_fd is open.
3319 */
3320
3321 if (pi->tid != 0)
3322 pi = find_procinfo_or_die (pi->pid, 0);
3323
3324 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3325
3326 nthreads = proc_get_nthreads (pi);
3327 if (nthreads < 2)
3328 return 0; /* nothing to do for 1 or fewer threads */
3329
3330 threads = xmalloc (nthreads * sizeof (tid_t));
3331
3332 if (ioctl (pi->ctl_fd, PIOCTLIST, threads) < 0)
3333 proc_error (pi, "procfs: update_threads (PIOCTLIST)", __LINE__);
3334
3335 for (i = 0; i < nthreads; i++)
3336 {
3337 if (!find_procinfo (pi->pid, threads[i]))
3338 if (!create_procinfo (pi->pid, threads[i]))
3339 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3340 }
3341 pi->threads_valid = 1;
3342 return 1;
3343 }
3344 #else
3345 /*
3346 * Default version
3347 */
3348 int
3349 proc_update_threads (procinfo *pi)
3350 {
3351 return 0;
3352 }
3353 #endif /* OSF PIOCTLIST */
3354 #endif /* NEW_PROC_API */
3355 #endif /* SOL 2.5 PIOCLSTATUS */
3356
3357 /*
3358 * Function: proc_iterate_over_threads
3359 *
3360 * Description:
3361 * Given a pointer to a function, call that function once
3362 * for each lwp in the procinfo list, until the function
3363 * returns non-zero, in which event return the value
3364 * returned by the function.
3365 *
3366 * Note: this function does NOT call update_threads.
3367 * If you want to discover new threads first, you must
3368 * call that function explicitly. This function just makes
3369 * a quick pass over the currently-known procinfos.
3370 *
3371 * Arguments:
3372 * pi - parent process procinfo
3373 * func - per-thread function
3374 * ptr - opaque parameter for function.
3375 *
3376 * Return:
3377 * First non-zero return value from the callee, or zero.
3378 */
3379
3380 int
3381 proc_iterate_over_threads (procinfo *pi,
3382 int (*func) (procinfo *, procinfo *, void *),
3383 void *ptr)
3384 {
3385 procinfo *thread, *next;
3386 int retval = 0;
3387
3388 /*
3389 * We should never have to apply this operation to any procinfo
3390 * except the one for the main process. If that ever changes
3391 * for any reason, then take out the following clause and
3392 * replace it with one that makes sure the ctl_fd is open.
3393 */
3394
3395 if (pi->tid != 0)
3396 pi = find_procinfo_or_die (pi->pid, 0);
3397
3398 for (thread = pi->thread_list; thread != NULL; thread = next)
3399 {
3400 next = thread->next; /* in case thread is destroyed */
3401 if ((retval = (*func) (pi, thread, ptr)) != 0)
3402 break;
3403 }
3404
3405 return retval;
3406 }
3407
3408 /* =================== END, Thread "MODULE" =================== */
3409
3410 /* =================== END, /proc "MODULE" =================== */
3411
3412 /* =================== GDB "MODULE" =================== */
3413
3414 /*
3415 * Here are all of the gdb target vector functions and their friends.
3416 */
3417
3418 static int do_attach (int pid);
3419 static void do_detach (int signo);
3420 static int register_gdb_signals (procinfo *, gdb_sigset_t *);
3421
3422 /*
3423 * Function: procfs_debug_inferior
3424 *
3425 * Sets up the inferior to be debugged.
3426 * Registers to trace signals, hardware faults, and syscalls.
3427 * Note: does not set RLC flag: caller may want to customize that.
3428 *
3429 * Returns: zero for success (note! unlike most functions in this module)
3430 * On failure, returns the LINE NUMBER where it failed!
3431 */
3432
3433 static int
3434 procfs_debug_inferior (procinfo *pi)
3435 {
3436 fltset_t traced_faults;
3437 gdb_sigset_t traced_signals;
3438 sysset_t *traced_syscall_entries;
3439 sysset_t *traced_syscall_exits;
3440 int status;
3441
3442 #ifdef PROCFS_DONT_TRACE_FAULTS
3443 /* On some systems (OSF), we don't trace hardware faults.
3444 Apparently it's enough that we catch them as signals.
3445 Wonder why we don't just do that in general? */
3446 premptyset (&traced_faults); /* don't trace faults. */
3447 #else
3448 /* Register to trace hardware faults in the child. */
3449 prfillset (&traced_faults); /* trace all faults... */
3450 prdelset (&traced_faults, FLTPAGE); /* except page fault. */
3451 #endif
3452 if (!proc_set_traced_faults (pi, &traced_faults))
3453 return __LINE__;
3454
3455 /* Register to trace selected signals in the child. */
3456 premptyset (&traced_signals);
3457 if (!register_gdb_signals (pi, &traced_signals))
3458 return __LINE__;
3459
3460
3461 /* Register to trace the 'exit' system call (on entry). */
3462 traced_syscall_entries = sysset_t_alloc (pi);
3463 gdb_premptysysset (traced_syscall_entries);
3464 #ifdef SYS_exit
3465 gdb_praddsysset (traced_syscall_entries, SYS_exit);
3466 #endif
3467 #ifdef SYS_lwpexit
3468 gdb_praddsysset (traced_syscall_entries, SYS_lwpexit); /* And _lwp_exit... */
3469 #endif
3470 #ifdef SYS_lwp_exit
3471 gdb_praddsysset (traced_syscall_entries, SYS_lwp_exit);
3472 #endif
3473 #ifdef DYNAMIC_SYSCALLS
3474 {
3475 int callnum = find_syscall (pi, "_exit");
3476 if (callnum >= 0)
3477 gdb_praddsysset (traced_syscall_entries, callnum);
3478 }
3479 #endif
3480
3481 status = proc_set_traced_sysentry (pi, traced_syscall_entries);
3482 xfree (traced_syscall_entries);
3483 if (!status)
3484 return __LINE__;
3485
3486 #ifdef PRFS_STOPEXEC /* defined on OSF */
3487 /* OSF method for tracing exec syscalls. Quoting:
3488 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3489 exits from exec system calls because of the user level loader. */
3490 /* FIXME: make nice and maybe move into an access function. */
3491 {
3492 int prfs_flags;
3493
3494 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
3495 return __LINE__;
3496
3497 prfs_flags |= PRFS_STOPEXEC;
3498
3499 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
3500 return __LINE__;
3501 }
3502 #else /* not PRFS_STOPEXEC */
3503 /* Everyone else's (except OSF) method for tracing exec syscalls */
3504 /* GW: Rationale...
3505 Not all systems with /proc have all the exec* syscalls with the same
3506 names. On the SGI, for example, there is no SYS_exec, but there
3507 *is* a SYS_execv. So, we try to account for that. */
3508
3509 traced_syscall_exits = sysset_t_alloc (pi);
3510 gdb_premptysysset (traced_syscall_exits);
3511 #ifdef SYS_exec
3512 gdb_praddsysset (traced_syscall_exits, SYS_exec);
3513 #endif
3514 #ifdef SYS_execve
3515 gdb_praddsysset (traced_syscall_exits, SYS_execve);
3516 #endif
3517 #ifdef SYS_execv
3518 gdb_praddsysset (traced_syscall_exits, SYS_execv);
3519 #endif
3520
3521 #ifdef SYS_lwpcreate
3522 gdb_praddsysset (traced_syscall_exits, SYS_lwpcreate);
3523 gdb_praddsysset (traced_syscall_exits, SYS_lwpexit);
3524 #endif
3525
3526 #ifdef SYS_lwp_create /* FIXME: once only, please */
3527 gdb_praddsysset (traced_syscall_exits, SYS_lwp_create);
3528 gdb_praddsysset (traced_syscall_exits, SYS_lwp_exit);
3529 #endif
3530
3531 #ifdef DYNAMIC_SYSCALLS
3532 {
3533 int callnum = find_syscall (pi, "execve");
3534 if (callnum >= 0)
3535 gdb_praddsysset (traced_syscall_exits, callnum);
3536 callnum = find_syscall (pi, "ra_execve");
3537 if (callnum >= 0)
3538 gdb_praddsysset (traced_syscall_exits, callnum);
3539 }
3540 #endif
3541
3542 status = proc_set_traced_sysexit (pi, traced_syscall_exits);
3543 xfree (traced_syscall_exits);
3544 if (!status)
3545 return __LINE__;
3546
3547 #endif /* PRFS_STOPEXEC */
3548 return 0;
3549 }
3550
3551 static void
3552 procfs_attach (char *args, int from_tty)
3553 {
3554 char *exec_file;
3555 int pid;
3556
3557 if (!args)
3558 error_no_arg ("process-id to attach");
3559
3560 pid = atoi (args);
3561 if (pid == getpid ())
3562 error ("Attaching GDB to itself is not a good idea...");
3563
3564 if (from_tty)
3565 {
3566 exec_file = get_exec_file (0);
3567
3568 if (exec_file)
3569 printf_filtered ("Attaching to program `%s', %s\n",
3570 exec_file, target_pid_to_str (pid));
3571 else
3572 printf_filtered ("Attaching to %s\n", target_pid_to_str (pid));
3573
3574 fflush (stdout);
3575 }
3576 inferior_pid = do_attach (pid);
3577 push_target (&procfs_ops);
3578 }
3579
3580 static void
3581 procfs_detach (char *args, int from_tty)
3582 {
3583 char *exec_file;
3584 int signo = 0;
3585
3586 if (from_tty)
3587 {
3588 exec_file = get_exec_file (0);
3589 if (exec_file == 0)
3590 exec_file = "";
3591 printf_filtered ("Detaching from program: %s %s\n",
3592 exec_file, target_pid_to_str (inferior_pid));
3593 fflush (stdout);
3594 }
3595 if (args)
3596 signo = atoi (args);
3597
3598 do_detach (signo);
3599 inferior_pid = 0;
3600 unpush_target (&procfs_ops); /* Pop out of handling an inferior */
3601 }
3602
3603 static int
3604 do_attach (int pid)
3605 {
3606 procinfo *pi;
3607 int fail;
3608
3609 if ((pi = create_procinfo (pid, 0)) == NULL)
3610 perror ("procfs: out of memory in 'attach'");
3611
3612 if (!open_procinfo_files (pi, FD_CTL))
3613 {
3614 fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__);
3615 sprintf (errmsg, "do_attach: couldn't open /proc file for process %d",
3616 pid);
3617 dead_procinfo (pi, errmsg, NOKILL);
3618 }
3619
3620 /* Stop the process (if it isn't already stopped). */
3621 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
3622 {
3623 pi->was_stopped = 1;
3624 proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
3625 }
3626 else
3627 {
3628 pi->was_stopped = 0;
3629 /* Set the process to run again when we close it. */
3630 if (!proc_set_run_on_last_close (pi))
3631 dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
3632
3633 /* Now stop the process. */
3634 if (!proc_stop_process (pi))
3635 dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
3636 pi->ignore_next_sigstop = 1;
3637 }
3638 /* Save some of the /proc state to be restored if we detach. */
3639 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
3640 dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
3641 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
3642 dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
3643 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
3644 dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
3645 NOKILL);
3646 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
3647 dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
3648 NOKILL);
3649 if (!proc_get_held_signals (pi, &pi->saved_sighold))
3650 dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
3651
3652 if ((fail = procfs_debug_inferior (pi)) != 0)
3653 dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
3654
3655 /* Let GDB know that the inferior was attached. */
3656 attach_flag = 1;
3657 return MERGEPID (pi->pid, proc_get_current_thread (pi));
3658 }
3659
3660 static void
3661 do_detach (int signo)
3662 {
3663 procinfo *pi;
3664
3665 /* Find procinfo for the main process */
3666 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0); /* FIXME: threads */
3667 if (signo)
3668 if (!proc_set_current_signal (pi, signo))
3669 proc_warn (pi, "do_detach, set_current_signal", __LINE__);
3670
3671 if (!proc_set_traced_signals (pi, &pi->saved_sigset))
3672 proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
3673
3674 if (!proc_set_traced_faults (pi, &pi->saved_fltset))
3675 proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
3676
3677 if (!proc_set_traced_sysentry (pi, pi->saved_entryset))
3678 proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
3679
3680 if (!proc_set_traced_sysexit (pi, pi->saved_exitset))
3681 proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
3682
3683 if (!proc_set_held_signals (pi, &pi->saved_sighold))
3684 proc_warn (pi, "do_detach, set_held_signals", __LINE__);
3685
3686 if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)))
3687 if (signo || !(pi->was_stopped) ||
3688 query ("Was stopped when attached, make it runnable again? "))
3689 {
3690 /* Clear any pending signal. */
3691 if (!proc_clear_current_fault (pi))
3692 proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
3693
3694 if (!proc_set_run_on_last_close (pi))
3695 proc_warn (pi, "do_detach, set_rlc", __LINE__);
3696 }
3697
3698 attach_flag = 0;
3699 destroy_procinfo (pi);
3700 }
3701
3702 /*
3703 * fetch_registers
3704 *
3705 * Since the /proc interface cannot give us individual registers,
3706 * we pay no attention to the (regno) argument, and just fetch them all.
3707 * This results in the possibility that we will do unnecessarily many
3708 * fetches, since we may be called repeatedly for individual registers.
3709 * So we cache the results, and mark the cache invalid when the process
3710 * is resumed.
3711 */
3712
3713 static void
3714 procfs_fetch_registers (int regno)
3715 {
3716 gdb_fpregset_t *fpregs;
3717 gdb_gregset_t *gregs;
3718 procinfo *pi;
3719 int pid;
3720 int tid;
3721
3722 pid = PIDGET (inferior_pid);
3723 tid = TIDGET (inferior_pid);
3724
3725 /* First look up procinfo for the main process. */
3726 pi = find_procinfo_or_die (pid, 0);
3727
3728 /* If the event thread is not the same as GDB's requested thread
3729 (ie. inferior_pid), then look up procinfo for the requested
3730 thread. */
3731 if ((tid != 0) &&
3732 (tid != proc_get_current_thread (pi)))
3733 pi = find_procinfo_or_die (pid, tid);
3734
3735 if (pi == NULL)
3736 error ("procfs: fetch_registers failed to find procinfo for %s",
3737 target_pid_to_str (inferior_pid));
3738
3739 if ((gregs = proc_get_gregs (pi)) == NULL)
3740 proc_error (pi, "fetch_registers, get_gregs", __LINE__);
3741
3742 supply_gregset (gregs);
3743
3744 if (FP0_REGNUM >= 0) /* need floating point? */
3745 {
3746 if ((regno >= 0 && regno < FP0_REGNUM) ||
3747 regno == PC_REGNUM ||
3748 (NPC_REGNUM >= 0 && regno == NPC_REGNUM) ||
3749 regno == FP_REGNUM ||
3750 regno == SP_REGNUM)
3751 return; /* not a floating point register */
3752
3753 if ((fpregs = proc_get_fpregs (pi)) == NULL)
3754 proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
3755
3756 supply_fpregset (fpregs);
3757 }
3758 }
3759
3760 /* Get ready to modify the registers array. On machines which store
3761 individual registers, this doesn't need to do anything. On
3762 machines which store all the registers in one fell swoop, such as
3763 /proc, this makes sure that registers contains all the registers
3764 from the program being debugged. */
3765
3766 static void
3767 procfs_prepare_to_store (void)
3768 {
3769 #ifdef CHILD_PREPARE_TO_STORE
3770 CHILD_PREPARE_TO_STORE ();
3771 #endif
3772 }
3773
3774 /*
3775 * store_registers
3776 *
3777 * Since the /proc interface will not read individual registers,
3778 * we will cache these requests until the process is resumed, and
3779 * only then write them back to the inferior process.
3780 *
3781 * FIXME: is that a really bad idea? Have to think about cases
3782 * where writing one register might affect the value of others, etc.
3783 */
3784
3785 static void
3786 procfs_store_registers (int regno)
3787 {
3788 gdb_fpregset_t *fpregs;
3789 gdb_gregset_t *gregs;
3790 procinfo *pi;
3791 int pid;
3792 int tid;
3793
3794 pid = PIDGET (inferior_pid);
3795 tid = TIDGET (inferior_pid);
3796
3797 /* First find procinfo for main process */
3798 pi = find_procinfo_or_die (pid, 0);
3799
3800 /* If current lwp for process is not the same as requested thread
3801 (ie. inferior_pid), then find procinfo for the requested thread. */
3802
3803 if ((tid != 0) &&
3804 (tid != proc_get_current_thread (pi)))
3805 pi = find_procinfo_or_die (pid, tid);
3806
3807 if (pi == NULL)
3808 error ("procfs: store_registers: failed to find procinfo for %s",
3809 target_pid_to_str (inferior_pid));
3810
3811 if ((gregs = proc_get_gregs (pi)) == NULL)
3812 proc_error (pi, "store_registers, get_gregs", __LINE__);
3813
3814 fill_gregset (gregs, regno);
3815 if (!proc_set_gregs (pi))
3816 proc_error (pi, "store_registers, set_gregs", __LINE__);
3817
3818 if (FP0_REGNUM >= 0) /* need floating point? */
3819 {
3820 if ((regno >= 0 && regno < FP0_REGNUM) ||
3821 regno == PC_REGNUM ||
3822 (NPC_REGNUM >= 0 && regno == NPC_REGNUM) ||
3823 regno == FP_REGNUM ||
3824 regno == SP_REGNUM)
3825 return; /* not a floating point register */
3826
3827 if ((fpregs = proc_get_fpregs (pi)) == NULL)
3828 proc_error (pi, "store_registers, get_fpregs", __LINE__);
3829
3830 fill_fpregset (fpregs, regno);
3831 if (!proc_set_fpregs (pi))
3832 proc_error (pi, "store_registers, set_fpregs", __LINE__);
3833 }
3834 }
3835
3836 static int
3837 syscall_is_lwp_exit (procinfo *pi, int scall)
3838 {
3839
3840 #ifdef SYS_lwp_exit
3841 if (scall == SYS_lwp_exit)
3842 return 1;
3843 #endif
3844 #ifdef SYS_lwpexit
3845 if (scall == SYS_lwpexit)
3846 return 1;
3847 #endif
3848 return 0;
3849 }
3850
3851 static int
3852 syscall_is_exit (procinfo *pi, int scall)
3853 {
3854 #ifdef SYS_exit
3855 if (scall == SYS_exit)
3856 return 1;
3857 #endif
3858 #ifdef DYNAMIC_SYSCALLS
3859 if (find_syscall (pi, "_exit") == scall)
3860 return 1;
3861 #endif
3862 return 0;
3863 }
3864
3865 static int
3866 syscall_is_exec (procinfo *pi, int scall)
3867 {
3868 #ifdef SYS_exec
3869 if (scall == SYS_exec)
3870 return 1;
3871 #endif
3872 #ifdef SYS_execv
3873 if (scall == SYS_execv)
3874 return 1;
3875 #endif
3876 #ifdef SYS_execve
3877 if (scall == SYS_execve)
3878 return 1;
3879 #endif
3880 #ifdef DYNAMIC_SYSCALLS
3881 if (find_syscall (pi, "_execve"))
3882 return 1;
3883 if (find_syscall (pi, "ra_execve"))
3884 return 1;
3885 #endif
3886 return 0;
3887 }
3888
3889 static int
3890 syscall_is_lwp_create (procinfo *pi, int scall)
3891 {
3892 #ifdef SYS_lwp_create
3893 if (scall == SYS_lwp_create)
3894 return 1;
3895 #endif
3896 #ifdef SYS_lwpcreate
3897 if (scall == SYS_lwpcreate)
3898 return 1;
3899 #endif
3900 return 0;
3901 }
3902
3903 /*
3904 * Function: target_wait
3905 *
3906 * Retrieve the next stop event from the child process.
3907 * If child has not stopped yet, wait for it to stop.
3908 * Translate /proc eventcodes (or possibly wait eventcodes)
3909 * into gdb internal event codes.
3910 *
3911 * Return: id of process (and possibly thread) that incurred the event.
3912 * event codes are returned thru a pointer parameter.
3913 */
3914
3915 static int
3916 procfs_wait (int pid, struct target_waitstatus *status)
3917 {
3918 /* First cut: loosely based on original version 2.1 */
3919 procinfo *pi;
3920 int temp, wstat;
3921 int retval;
3922 int why, what, flags;
3923 int retry = 0;
3924
3925 wait_again:
3926
3927 retry++;
3928 wstat = 0;
3929 retval = -1;
3930
3931 /* Find procinfo for main process */
3932 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
3933 if (pi)
3934 {
3935 /* We must assume that the status is stale now... */
3936 pi->status_valid = 0;
3937 pi->gregs_valid = 0;
3938 pi->fpregs_valid = 0;
3939
3940 #if 0 /* just try this out... */
3941 flags = proc_flags (pi);
3942 why = proc_why (pi);
3943 if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
3944 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
3945 #endif
3946 /* If child is not stopped, wait for it to stop. */
3947 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) &&
3948 !proc_wait_for_stop (pi))
3949 {
3950 /* wait_for_stop failed: has the child terminated? */
3951 if (errno == ENOENT)
3952 {
3953 /* /proc file not found; presumably child has terminated. */
3954 retval = wait (&wstat); /* "wait" for the child's exit */
3955
3956 if (retval != PIDGET (inferior_pid)) /* wrong child? */
3957 error ("procfs: couldn't stop process %d: wait returned %d\n",
3958 inferior_pid, retval);
3959 /* FIXME: might I not just use waitpid?
3960 Or try find_procinfo to see if I know about this child? */
3961 }
3962 else if (errno == EINTR)
3963 goto wait_again;
3964 else
3965 {
3966 /* Unknown error from wait_for_stop. */
3967 proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
3968 }
3969 }
3970 else
3971 {
3972 /* This long block is reached if either:
3973 a) the child was already stopped, or
3974 b) we successfully waited for the child with wait_for_stop.
3975 This block will analyze the /proc status, and translate it
3976 into a waitstatus for GDB.
3977
3978 If we actually had to call wait because the /proc file
3979 is gone (child terminated), then we skip this block,
3980 because we already have a waitstatus. */
3981
3982 flags = proc_flags (pi);
3983 why = proc_why (pi);
3984 what = proc_what (pi);
3985
3986 if (flags & (PR_STOPPED | PR_ISTOP))
3987 {
3988 #ifdef PR_ASYNC
3989 /* If it's running async (for single_thread control),
3990 set it back to normal again. */
3991 if (flags & PR_ASYNC)
3992 if (!proc_unset_async (pi))
3993 proc_error (pi, "target_wait, unset_async", __LINE__);
3994 #endif
3995
3996 if (info_verbose)
3997 proc_prettyprint_why (why, what, 1);
3998
3999 /* The 'pid' we will return to GDB is composed of
4000 the process ID plus the lwp ID. */
4001 retval = MERGEPID (pi->pid, proc_get_current_thread (pi));
4002
4003 switch (why) {
4004 case PR_SIGNALLED:
4005 wstat = (what << 8) | 0177;
4006 break;
4007 case PR_SYSENTRY:
4008 if (syscall_is_lwp_exit (pi, what))
4009 {
4010 printf_filtered ("[%s exited]\n",
4011 target_pid_to_str (retval));
4012 delete_thread (retval);
4013 status->kind = TARGET_WAITKIND_SPURIOUS;
4014 return retval;
4015 }
4016 else if (syscall_is_exit (pi, what))
4017 {
4018 /* Handle SYS_exit call only */
4019 /* Stopped at entry to SYS_exit.
4020 Make it runnable, resume it, then use
4021 the wait system call to get its exit code.
4022 Proc_run_process always clears the current
4023 fault and signal.
4024 Then return its exit status. */
4025 pi->status_valid = 0;
4026 wstat = 0;
4027 /* FIXME: what we should do is return
4028 TARGET_WAITKIND_SPURIOUS. */
4029 if (!proc_run_process (pi, 0, 0))
4030 proc_error (pi, "target_wait, run_process", __LINE__);
4031 if (attach_flag)
4032 {
4033 /* Don't call wait: simulate waiting for exit,
4034 return a "success" exit code. Bogus: what if
4035 it returns something else? */
4036 wstat = 0;
4037 retval = inferior_pid; /* ? ? ? */
4038 }
4039 else
4040 {
4041 int temp = wait (&wstat);
4042
4043 /* FIXME: shouldn't I make sure I get the right
4044 event from the right process? If (for
4045 instance) I have killed an earlier inferior
4046 process but failed to clean up after it
4047 somehow, I could get its termination event
4048 here. */
4049
4050 /* If wait returns -1, that's what we return to GDB. */
4051 if (temp < 0)
4052 retval = temp;
4053 }
4054 }
4055 else
4056 {
4057 printf_filtered ("procfs: trapped on entry to ");
4058 proc_prettyprint_syscall (proc_what (pi), 0);
4059 printf_filtered ("\n");
4060 #ifndef PIOCSSPCACT
4061 {
4062 long i, nsysargs, *sysargs;
4063
4064 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4065 (sysargs = proc_sysargs (pi)) != NULL)
4066 {
4067 printf_filtered ("%ld syscall arguments:\n", nsysargs);
4068 for (i = 0; i < nsysargs; i++)
4069 printf_filtered ("#%ld: 0x%08lx\n",
4070 i, sysargs[i]);
4071 }
4072
4073 }
4074 #endif
4075 if (status)
4076 {
4077 /* How to exit gracefully, returning "unknown event" */
4078 status->kind = TARGET_WAITKIND_SPURIOUS;
4079 return inferior_pid;
4080 }
4081 else
4082 {
4083 /* How to keep going without returning to wfi: */
4084 target_resume (pid, 0, TARGET_SIGNAL_0);
4085 goto wait_again;
4086 }
4087 }
4088 break;
4089 case PR_SYSEXIT:
4090 if (syscall_is_exec (pi, what))
4091 {
4092 /* Hopefully this is our own "fork-child" execing
4093 the real child. Hoax this event into a trap, and
4094 GDB will see the child about to execute its start
4095 address. */
4096 wstat = (SIGTRAP << 8) | 0177;
4097 }
4098 else if (syscall_is_lwp_create (pi, what))
4099 {
4100 /*
4101 * This syscall is somewhat like fork/exec.
4102 * We will get the event twice: once for the parent LWP,
4103 * and once for the child. We should already know about
4104 * the parent LWP, but the child will be new to us. So,
4105 * whenever we get this event, if it represents a new
4106 * thread, simply add the thread to the list.
4107 */
4108
4109 /* If not in procinfo list, add it. */
4110 temp = proc_get_current_thread (pi);
4111 if (!find_procinfo (pi->pid, temp))
4112 create_procinfo (pi->pid, temp);
4113
4114 temp = MERGEPID (pi->pid, temp);
4115 /* If not in GDB's thread list, add it. */
4116 if (!in_thread_list (temp))
4117 {
4118 printf_filtered ("[New %s]\n", target_pid_to_str (temp));
4119 add_thread (temp);
4120 }
4121 /* Return to WFI, but tell it to immediately resume. */
4122 status->kind = TARGET_WAITKIND_SPURIOUS;
4123 return inferior_pid;
4124 }
4125 else if (syscall_is_lwp_exit (pi, what))
4126 {
4127 printf_filtered ("[%s exited]\n",
4128 target_pid_to_str (retval));
4129 delete_thread (retval);
4130 status->kind = TARGET_WAITKIND_SPURIOUS;
4131 return retval;
4132 }
4133 else if (0)
4134 {
4135 /* FIXME: Do we need to handle SYS_sproc,
4136 SYS_fork, or SYS_vfork here? The old procfs
4137 seemed to use this event to handle threads on
4138 older (non-LWP) systems, where I'm assuming
4139 that threads were actually separate processes.
4140 Irix, maybe? Anyway, low priority for now. */
4141 }
4142 else
4143 {
4144 printf_filtered ("procfs: trapped on exit from ");
4145 proc_prettyprint_syscall (proc_what (pi), 0);
4146 printf_filtered ("\n");
4147 #ifndef PIOCSSPCACT
4148 {
4149 long i, nsysargs, *sysargs;
4150
4151 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4152 (sysargs = proc_sysargs (pi)) != NULL)
4153 {
4154 printf_filtered ("%ld syscall arguments:\n", nsysargs);
4155 for (i = 0; i < nsysargs; i++)
4156 printf_filtered ("#%ld: 0x%08lx\n",
4157 i, sysargs[i]);
4158 }
4159 }
4160 #endif
4161 status->kind = TARGET_WAITKIND_SPURIOUS;
4162 return inferior_pid;
4163 }
4164 break;
4165 case PR_REQUESTED:
4166 #if 0 /* FIXME */
4167 wstat = (SIGSTOP << 8) | 0177;
4168 break;
4169 #else
4170 if (retry < 5)
4171 {
4172 printf_filtered ("Retry #%d:\n", retry);
4173 pi->status_valid = 0;
4174 goto wait_again;
4175 }
4176 else
4177 {
4178 /* If not in procinfo list, add it. */
4179 temp = proc_get_current_thread (pi);
4180 if (!find_procinfo (pi->pid, temp))
4181 create_procinfo (pi->pid, temp);
4182
4183 /* If not in GDB's thread list, add it. */
4184 temp = MERGEPID (pi->pid, temp);
4185 if (!in_thread_list (temp))
4186 {
4187 printf_filtered ("[New %s]\n",
4188 target_pid_to_str (temp));
4189 add_thread (temp);
4190 }
4191
4192 status->kind = TARGET_WAITKIND_STOPPED;
4193 status->value.sig = 0;
4194 return retval;
4195 }
4196 #endif
4197 case PR_JOBCONTROL:
4198 wstat = (what << 8) | 0177;
4199 break;
4200 case PR_FAULTED:
4201 switch (what) { /* FIXME: FAULTED_USE_SIGINFO */
4202 #ifdef FLTWATCH
4203 case FLTWATCH:
4204 wstat = (SIGTRAP << 8) | 0177;
4205 break;
4206 #endif
4207 #ifdef FLTKWATCH
4208 case FLTKWATCH:
4209 wstat = (SIGTRAP << 8) | 0177;
4210 break;
4211 #endif
4212 /* FIXME: use si_signo where possible. */
4213 case FLTPRIV:
4214 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4215 case FLTILL:
4216 #endif
4217 wstat = (SIGILL << 8) | 0177;
4218 break;
4219 case FLTBPT:
4220 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4221 case FLTTRACE:
4222 #endif
4223 wstat = (SIGTRAP << 8) | 0177;
4224 break;
4225 case FLTSTACK:
4226 case FLTACCESS:
4227 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4228 case FLTBOUNDS:
4229 #endif
4230 wstat = (SIGSEGV << 8) | 0177;
4231 break;
4232 case FLTIOVF:
4233 case FLTIZDIV:
4234 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4235 case FLTFPE:
4236 #endif
4237 wstat = (SIGFPE << 8) | 0177;
4238 break;
4239 case FLTPAGE: /* Recoverable page fault */
4240 default: /* FIXME: use si_signo if possible for fault */
4241 retval = -1;
4242 printf_filtered ("procfs:%d -- ", __LINE__);
4243 printf_filtered ("child stopped for unknown reason:\n");
4244 proc_prettyprint_why (why, what, 1);
4245 error ("... giving up...");
4246 break;
4247 }
4248 break; /* case PR_FAULTED: */
4249 default: /* switch (why) unmatched */
4250 printf_filtered ("procfs:%d -- ", __LINE__);
4251 printf_filtered ("child stopped for unknown reason:\n");
4252 proc_prettyprint_why (why, what, 1);
4253 error ("... giving up...");
4254 break;
4255 }
4256 /*
4257 * Got this far without error:
4258 * If retval isn't in the threads database, add it.
4259 */
4260 if (retval > 0 &&
4261 retval != inferior_pid &&
4262 !in_thread_list (retval))
4263 {
4264 /*
4265 * We have a new thread.
4266 * We need to add it both to GDB's list and to our own.
4267 * If we don't create a procinfo, resume may be unhappy
4268 * later.
4269 */
4270 printf_filtered ("[New %s]\n", target_pid_to_str (retval));
4271 add_thread (retval);
4272 if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL)
4273 create_procinfo (PIDGET (retval), TIDGET (retval));
4274
4275 /* In addition, it's possible that this is the first
4276 * new thread we've seen, in which case we may not
4277 * have created entries for inferior_pid yet.
4278 */
4279 if (TIDGET (inferior_pid) != 0)
4280 {
4281 if (!in_thread_list (inferior_pid))
4282 add_thread (inferior_pid);
4283 if (find_procinfo (PIDGET (inferior_pid),
4284 TIDGET (inferior_pid)) == NULL)
4285 create_procinfo (PIDGET (inferior_pid),
4286 TIDGET (inferior_pid));
4287 }
4288 }
4289 }
4290 else /* flags do not indicate STOPPED */
4291 {
4292 /* surely this can't happen... */
4293 printf_filtered ("procfs:%d -- process not stopped.\n",
4294 __LINE__);
4295 proc_prettyprint_flags (flags, 1);
4296 error ("procfs: ...giving up...");
4297 }
4298 }
4299
4300 if (status)
4301 store_waitstatus (status, wstat);
4302 }
4303
4304 return retval;
4305 }
4306
4307 /* Transfer LEN bytes between GDB address MYADDR and target address
4308 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4309 otherwise transfer them from the target. TARGET is unused.
4310
4311 The return value is 0 if an error occurred or no bytes were
4312 transferred. Otherwise, it will be a positive value which
4313 indicates the number of bytes transferred between gdb and the
4314 target. (Note that the interface also makes provisions for
4315 negative values, but this capability isn't implemented here.) */
4316
4317 static int
4318 procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
4319 struct mem_attrib *attrib, struct target_ops *target)
4320 {
4321 procinfo *pi;
4322 int nbytes = 0;
4323
4324 /* Find procinfo for main process */
4325 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
4326 if (pi->as_fd == 0 &&
4327 open_procinfo_files (pi, FD_AS) == 0)
4328 {
4329 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
4330 return 0;
4331 }
4332
4333 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
4334 {
4335 if (dowrite)
4336 {
4337 #ifdef NEW_PROC_API
4338 PROCFS_NOTE ("write memory: ");
4339 #else
4340 PROCFS_NOTE ("write memory: \n");
4341 #endif
4342 nbytes = write (pi->as_fd, myaddr, len);
4343 }
4344 else
4345 {
4346 PROCFS_NOTE ("read memory: \n");
4347 nbytes = read (pi->as_fd, myaddr, len);
4348 }
4349 if (nbytes < 0)
4350 {
4351 nbytes = 0;
4352 }
4353 }
4354 return nbytes;
4355 }
4356
4357 /*
4358 * Function: invalidate_cache
4359 *
4360 * Called by target_resume before making child runnable.
4361 * Mark cached registers and status's invalid.
4362 * If there are "dirty" caches that need to be written back
4363 * to the child process, do that.
4364 *
4365 * File descriptors are also cached.
4366 * As they are a limited resource, we cannot hold onto them indefinitely.
4367 * However, as they are expensive to open, we don't want to throw them
4368 * away indescriminately either. As a compromise, we will keep the
4369 * file descriptors for the parent process, but discard any file
4370 * descriptors we may have accumulated for the threads.
4371 *
4372 * Return value:
4373 * As this function is called by iterate_over_threads, it always
4374 * returns zero (so that iterate_over_threads will keep iterating).
4375 */
4376
4377
4378 static int
4379 invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
4380 {
4381 /*
4382 * About to run the child; invalidate caches and do any other cleanup.
4383 */
4384
4385 #if 0
4386 if (pi->gregs_dirty)
4387 if (parent == NULL ||
4388 proc_get_current_thread (parent) != pi->tid)
4389 if (!proc_set_gregs (pi)) /* flush gregs cache */
4390 proc_warn (pi, "target_resume, set_gregs",
4391 __LINE__);
4392 if (FP0_REGNUM >= 0)
4393 if (pi->fpregs_dirty)
4394 if (parent == NULL ||
4395 proc_get_current_thread (parent) != pi->tid)
4396 if (!proc_set_fpregs (pi)) /* flush fpregs cache */
4397 proc_warn (pi, "target_resume, set_fpregs",
4398 __LINE__);
4399 #endif
4400
4401 if (parent != NULL)
4402 {
4403 /* The presence of a parent indicates that this is an LWP.
4404 Close any file descriptors that it might have open.
4405 We don't do this to the master (parent) procinfo. */
4406
4407 close_procinfo_files (pi);
4408 }
4409 pi->gregs_valid = 0;
4410 pi->fpregs_valid = 0;
4411 #if 0
4412 pi->gregs_dirty = 0;
4413 pi->fpregs_dirty = 0;
4414 #endif
4415 pi->status_valid = 0;
4416 pi->threads_valid = 0;
4417
4418 return 0;
4419 }
4420
4421 #if 0
4422 /*
4423 * Function: make_signal_thread_runnable
4424 *
4425 * A callback function for iterate_over_threads.
4426 * Find the asynchronous signal thread, and make it runnable.
4427 * See if that helps matters any.
4428 */
4429
4430 static int
4431 make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr)
4432 {
4433 #ifdef PR_ASLWP
4434 if (proc_flags (pi) & PR_ASLWP)
4435 {
4436 if (!proc_run_process (pi, 0, -1))
4437 proc_error (pi, "make_signal_thread_runnable", __LINE__);
4438 return 1;
4439 }
4440 #endif
4441 return 0;
4442 }
4443 #endif
4444
4445 /*
4446 * Function: target_resume
4447 *
4448 * Make the child process runnable. Normally we will then call
4449 * procfs_wait and wait for it to stop again (unles gdb is async).
4450 *
4451 * Arguments:
4452 * step: if true, then arrange for the child to stop again
4453 * after executing a single instruction.
4454 * signo: if zero, then cancel any pending signal.
4455 * If non-zero, then arrange for the indicated signal
4456 * to be delivered to the child when it runs.
4457 * pid: if -1, then allow any child thread to run.
4458 * if non-zero, then allow only the indicated thread to run.
4459 ******* (not implemented yet)
4460 */
4461
4462 static void
4463 procfs_resume (int pid, int step, enum target_signal signo)
4464 {
4465 procinfo *pi, *thread;
4466 int native_signo;
4467
4468 /* 2.1:
4469 prrun.prflags |= PRSVADDR;
4470 prrun.pr_vaddr = $PC; set resume address
4471 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4472 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4473 prrun.prflags |= PRCFAULT; clear current fault.
4474
4475 PRSTRACE and PRSFAULT can be done by other means
4476 (proc_trace_signals, proc_trace_faults)
4477 PRSVADDR is unnecessary.
4478 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4479 This basically leaves PRSTEP and PRCSIG.
4480 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4481 So basically PR_STEP is the sole argument that must be passed
4482 to proc_run_process (for use in the prrun struct by ioctl). */
4483
4484 /* Find procinfo for main process */
4485 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
4486
4487 /* First cut: ignore pid argument */
4488 errno = 0;
4489
4490 /* Convert signal to host numbering. */
4491 if (signo == 0 ||
4492 (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop))
4493 native_signo = 0;
4494 else
4495 native_signo = target_signal_to_host (signo);
4496
4497 pi->ignore_next_sigstop = 0;
4498
4499 /* Running the process voids all cached registers and status. */
4500 /* Void the threads' caches first */
4501 proc_iterate_over_threads (pi, invalidate_cache, NULL);
4502 /* Void the process procinfo's caches. */
4503 invalidate_cache (NULL, pi, NULL);
4504
4505 if (pid != -1)
4506 {
4507 /* Resume a specific thread, presumably suppressing the others. */
4508 thread = find_procinfo (PIDGET (pid), TIDGET (pid));
4509 if (thread == NULL)
4510 warning ("procfs: resume can't find thread %d -- resuming all.",
4511 TIDGET (pid));
4512 else
4513 {
4514 if (thread->tid != 0)
4515 {
4516 /* We're to resume a specific thread, and not the others.
4517 * Set the child process's PR_ASYNC flag.
4518 */
4519 #ifdef PR_ASYNC
4520 if (!proc_set_async (pi))
4521 proc_error (pi, "target_resume, set_async", __LINE__);
4522 #endif
4523 #if 0
4524 proc_iterate_over_threads (pi,
4525 make_signal_thread_runnable,
4526 NULL);
4527 #endif
4528 pi = thread; /* substitute the thread's procinfo for run */
4529 }
4530 }
4531 }
4532
4533 if (!proc_run_process (pi, step, native_signo))
4534 {
4535 if (errno == EBUSY)
4536 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4537 else
4538 proc_error (pi, "target_resume", __LINE__);
4539 }
4540 }
4541
4542 /*
4543 * Function: register_gdb_signals
4544 *
4545 * Traverse the list of signals that GDB knows about
4546 * (see "handle" command), and arrange for the target
4547 * to be stopped or not, according to these settings.
4548 *
4549 * Returns non-zero for success, zero for failure.
4550 */
4551
4552 static int
4553 register_gdb_signals (procinfo *pi, gdb_sigset_t *signals)
4554 {
4555 int signo;
4556
4557 for (signo = 0; signo < NSIG; signo ++)
4558 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
4559 signal_print_state (target_signal_from_host (signo)) == 0 &&
4560 signal_pass_state (target_signal_from_host (signo)) == 1)
4561 prdelset (signals, signo);
4562 else
4563 praddset (signals, signo);
4564
4565 return proc_set_traced_signals (pi, signals);
4566 }
4567
4568 /*
4569 * Function: target_notice_signals
4570 *
4571 * Set up to trace signals in the child process.
4572 */
4573
4574 static void
4575 procfs_notice_signals (int pid)
4576 {
4577 gdb_sigset_t signals;
4578 procinfo *pi = find_procinfo_or_die (PIDGET (pid), 0);
4579
4580 if (proc_get_traced_signals (pi, &signals) &&
4581 register_gdb_signals (pi, &signals))
4582 return;
4583 else
4584 proc_error (pi, "notice_signals", __LINE__);
4585 }
4586
4587 /*
4588 * Function: target_files_info
4589 *
4590 * Print status information about the child process.
4591 */
4592
4593 static void
4594 procfs_files_info (struct target_ops *ignore)
4595 {
4596 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4597 attach_flag? "attached": "child",
4598 target_pid_to_str (inferior_pid));
4599 }
4600
4601 /*
4602 * Function: target_open
4603 *
4604 * A dummy: you don't open procfs.
4605 */
4606
4607 static void
4608 procfs_open (char *args, int from_tty)
4609 {
4610 error ("Use the \"run\" command to start a Unix child process.");
4611 }
4612
4613 /*
4614 * Function: target_can_run
4615 *
4616 * This tells GDB that this target vector can be invoked
4617 * for "run" or "attach".
4618 */
4619
4620 int procfs_suppress_run = 0; /* Non-zero if procfs should pretend not to
4621 be a runnable target. Used by targets
4622 that can sit atop procfs, such as solaris
4623 thread support. */
4624
4625
4626 static int
4627 procfs_can_run (void)
4628 {
4629 /* This variable is controlled by modules that sit atop procfs that
4630 may layer their own process structure atop that provided here.
4631 sol-thread.c does this because of the Solaris two-level thread
4632 model. */
4633
4634 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4635
4636 return !procfs_suppress_run;
4637 }
4638
4639 /*
4640 * Function: target_stop
4641 *
4642 * Stop the child process asynchronously, as when the
4643 * gdb user types control-c or presses a "stop" button.
4644 *
4645 * Works by sending kill(SIGINT) to the child's process group.
4646 */
4647
4648 static void
4649 procfs_stop (void)
4650 {
4651 extern pid_t inferior_process_group;
4652
4653 kill (-inferior_process_group, SIGINT);
4654 }
4655
4656 /*
4657 * Function: unconditionally_kill_inferior
4658 *
4659 * Make it die. Wait for it to die. Clean up after it.
4660 * Note: this should only be applied to the real process,
4661 * not to an LWP, because of the check for parent-process.
4662 * If we need this to work for an LWP, it needs some more logic.
4663 */
4664
4665 static void
4666 unconditionally_kill_inferior (procinfo *pi)
4667 {
4668 int parent_pid;
4669
4670 parent_pid = proc_parent_pid (pi);
4671 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4672 /* FIXME: use access functions */
4673 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4674 before the PIOCKILL, otherwise it might generate a corrupted core
4675 file for the inferior. */
4676 if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0)
4677 {
4678 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4679 }
4680 #endif
4681 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4682 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4683 to kill the inferior, otherwise it might remain stopped with a
4684 pending SIGKILL.
4685 We do not check the result of the PIOCSSIG, the inferior might have
4686 died already. */
4687 {
4688 gdb_siginfo_t newsiginfo;
4689
4690 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
4691 newsiginfo.si_signo = SIGKILL;
4692 newsiginfo.si_code = 0;
4693 newsiginfo.si_errno = 0;
4694 newsiginfo.si_pid = getpid ();
4695 newsiginfo.si_uid = getuid ();
4696 /* FIXME: use proc_set_current_signal */
4697 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
4698 }
4699 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4700 if (!proc_kill (pi, SIGKILL))
4701 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
4702 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4703 destroy_procinfo (pi);
4704
4705 /* If pi is GDB's child, wait for it to die. */
4706 if (parent_pid == getpid ())
4707 /* FIXME: should we use waitpid to make sure we get the right event?
4708 Should we check the returned event? */
4709 {
4710 #if 0
4711 int status, ret;
4712
4713 ret = waitpid (pi->pid, &status, 0);
4714 #else
4715 wait (NULL);
4716 #endif
4717 }
4718 }
4719
4720 /*
4721 * Function: target_kill_inferior
4722 *
4723 * We're done debugging it, and we want it to go away.
4724 * Then we want GDB to forget all about it.
4725 */
4726
4727 static void
4728 procfs_kill_inferior (void)
4729 {
4730 if (inferior_pid != 0) /* ? */
4731 {
4732 /* Find procinfo for main process */
4733 procinfo *pi = find_procinfo (PIDGET (inferior_pid), 0);
4734
4735 if (pi)
4736 unconditionally_kill_inferior (pi);
4737 target_mourn_inferior ();
4738 }
4739 }
4740
4741 /*
4742 * Function: target_mourn_inferior
4743 *
4744 * Forget we ever debugged this thing!
4745 */
4746
4747 static void
4748 procfs_mourn_inferior (void)
4749 {
4750 procinfo *pi;
4751
4752 if (inferior_pid != 0)
4753 {
4754 /* Find procinfo for main process */
4755 pi = find_procinfo (PIDGET (inferior_pid), 0);
4756 if (pi)
4757 destroy_procinfo (pi);
4758 }
4759 unpush_target (&procfs_ops);
4760 generic_mourn_inferior ();
4761 }
4762
4763 /*
4764 * Function: init_inferior
4765 *
4766 * When GDB forks to create a runnable inferior process,
4767 * this function is called on the parent side of the fork.
4768 * It's job is to do whatever is necessary to make the child
4769 * ready to be debugged, and then wait for the child to synchronize.
4770 */
4771
4772 static void
4773 procfs_init_inferior (int pid)
4774 {
4775 procinfo *pi;
4776 gdb_sigset_t signals;
4777 int fail;
4778
4779 /* This routine called on the parent side (GDB side)
4780 after GDB forks the inferior. */
4781
4782 push_target (&procfs_ops);
4783
4784 if ((pi = create_procinfo (pid, 0)) == NULL)
4785 perror ("procfs: out of memory in 'init_inferior'");
4786
4787 if (!open_procinfo_files (pi, FD_CTL))
4788 proc_error (pi, "init_inferior, open_proc_files", __LINE__);
4789
4790 /*
4791 xmalloc // done
4792 open_procinfo_files // done
4793 link list // done
4794 prfillset (trace)
4795 procfs_notice_signals
4796 prfillset (fault)
4797 prdelset (FLTPAGE)
4798 PIOCWSTOP
4799 PIOCSFAULT
4800 */
4801
4802 /* If not stopped yet, wait for it to stop. */
4803 if (!(proc_flags (pi) & PR_STOPPED) &&
4804 !(proc_wait_for_stop (pi)))
4805 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
4806
4807 /* Save some of the /proc state to be restored if we detach. */
4808 /* FIXME: Why? In case another debugger was debugging it?
4809 We're it's parent, for Ghu's sake! */
4810 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
4811 proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
4812 if (!proc_get_held_signals (pi, &pi->saved_sighold))
4813 proc_error (pi, "init_inferior, get_held_signals", __LINE__);
4814 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
4815 proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
4816 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
4817 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
4818 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
4819 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
4820
4821 /* Register to trace selected signals in the child. */
4822 prfillset (&signals);
4823 if (!register_gdb_signals (pi, &signals))
4824 proc_error (pi, "init_inferior, register_signals", __LINE__);
4825
4826 if ((fail = procfs_debug_inferior (pi)) != 0)
4827 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
4828
4829 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4830 and possibly even turning ON kill-on-last-close at this point. But
4831 I can't make that change without careful testing which I don't have
4832 time to do right now... */
4833 /* Turn on run-on-last-close flag so that the child
4834 will die if GDB goes away for some reason. */
4835 if (!proc_set_run_on_last_close (pi))
4836 proc_error (pi, "init_inferior, set_RLC", __LINE__);
4837
4838 /* The 'process ID' we return to GDB is composed of
4839 the actual process ID plus the lwp ID. */
4840 inferior_pid = MERGEPID (pi->pid, proc_get_current_thread (pi));
4841
4842 #ifdef START_INFERIOR_TRAPS_EXPECTED
4843 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
4844 #else
4845 /* One trap to exec the shell, one to exec the program being debugged. */
4846 startup_inferior (2);
4847 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4848 }
4849
4850 /*
4851 * Function: set_exec_trap
4852 *
4853 * When GDB forks to create a new process, this function is called
4854 * on the child side of the fork before GDB exec's the user program.
4855 * Its job is to make the child minimally debuggable, so that the
4856 * parent GDB process can connect to the child and take over.
4857 * This function should do only the minimum to make that possible,
4858 * and to synchronize with the parent process. The parent process
4859 * should take care of the details.
4860 */
4861
4862 static void
4863 procfs_set_exec_trap (void)
4864 {
4865 /* This routine called on the child side (inferior side)
4866 after GDB forks the inferior. It must use only local variables,
4867 because it may be sharing data space with its parent. */
4868
4869 procinfo *pi;
4870 sysset_t *exitset;
4871
4872 if ((pi = create_procinfo (getpid (), 0)) == NULL)
4873 perror_with_name ("procfs: create_procinfo failed in child.");
4874
4875 if (open_procinfo_files (pi, FD_CTL) == 0)
4876 {
4877 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
4878 gdb_flush (gdb_stderr);
4879 /* no need to call "dead_procinfo", because we're going to exit. */
4880 _exit (127);
4881 }
4882
4883 #ifdef PRFS_STOPEXEC /* defined on OSF */
4884 /* OSF method for tracing exec syscalls. Quoting:
4885 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4886 exits from exec system calls because of the user level loader. */
4887 /* FIXME: make nice and maybe move into an access function. */
4888 {
4889 int prfs_flags;
4890
4891 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
4892 {
4893 proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__);
4894 gdb_flush (gdb_stderr);
4895 _exit (127);
4896 }
4897 prfs_flags |= PRFS_STOPEXEC;
4898
4899 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
4900 {
4901 proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__);
4902 gdb_flush (gdb_stderr);
4903 _exit (127);
4904 }
4905 }
4906 #else /* not PRFS_STOPEXEC */
4907 /* Everyone else's (except OSF) method for tracing exec syscalls */
4908 /* GW: Rationale...
4909 Not all systems with /proc have all the exec* syscalls with the same
4910 names. On the SGI, for example, there is no SYS_exec, but there
4911 *is* a SYS_execv. So, we try to account for that. */
4912
4913 exitset = sysset_t_alloc (pi);
4914 gdb_premptysysset (exitset);
4915 #ifdef SYS_exec
4916 gdb_praddsysset (exitset, SYS_exec);
4917 #endif
4918 #ifdef SYS_execve
4919 gdb_praddsysset (exitset, SYS_execve);
4920 #endif
4921 #ifdef SYS_execv
4922 gdb_praddsysset (exitset, SYS_execv);
4923 #endif
4924 #ifdef DYNAMIC_SYSCALLS
4925 {
4926 int callnum = find_syscall (pi, "execve");
4927
4928 if (callnum >= 0)
4929 gdb_praddsysset (exitset, callnum);
4930
4931 callnum = find_syscall (pi, "ra_execve");
4932 if (callnum >= 0)
4933 gdb_praddsysset (exitset, callnum);
4934 }
4935 #endif /* DYNAMIC_SYSCALLS */
4936
4937 if (!proc_set_traced_sysexit (pi, exitset))
4938 {
4939 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
4940 gdb_flush (gdb_stderr);
4941 _exit (127);
4942 }
4943 #endif /* PRFS_STOPEXEC */
4944
4945 /* FIXME: should this be done in the parent instead? */
4946 /* Turn off inherit on fork flag so that all grand-children
4947 of gdb start with tracing flags cleared. */
4948 if (!proc_unset_inherit_on_fork (pi))
4949 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
4950
4951 /* Turn off run on last close flag, so that the child process
4952 cannot run away just because we close our handle on it.
4953 We want it to wait for the parent to attach. */
4954 if (!proc_unset_run_on_last_close (pi))
4955 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
4956
4957 /* FIXME: No need to destroy the procinfo --
4958 we have our own address space, and we're about to do an exec! */
4959 /*destroy_procinfo (pi);*/
4960 }
4961
4962 /*
4963 * Function: create_inferior
4964 *
4965 * This function is called BEFORE gdb forks the inferior process.
4966 * Its only real responsibility is to set things up for the fork,
4967 * and tell GDB which two functions to call after the fork (one
4968 * for the parent, and one for the child).
4969 *
4970 * This function does a complicated search for a unix shell program,
4971 * which it then uses to parse arguments and environment variables
4972 * to be sent to the child. I wonder whether this code could not
4973 * be abstracted out and shared with other unix targets such as
4974 * infptrace?
4975 */
4976
4977 static void
4978 procfs_create_inferior (char *exec_file, char *allargs, char **env)
4979 {
4980 char *shell_file = getenv ("SHELL");
4981 char *tryname;
4982 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
4983 {
4984
4985 /* We will be looking down the PATH to find shell_file. If we
4986 just do this the normal way (via execlp, which operates by
4987 attempting an exec for each element of the PATH until it
4988 finds one which succeeds), then there will be an exec for
4989 each failed attempt, each of which will cause a PR_SYSEXIT
4990 stop, and we won't know how to distinguish the PR_SYSEXIT's
4991 for these failed execs with the ones for successful execs
4992 (whether the exec has succeeded is stored at that time in the
4993 carry bit or some such architecture-specific and
4994 non-ABI-specified place).
4995
4996 So I can't think of anything better than to search the PATH
4997 now. This has several disadvantages: (1) There is a race
4998 condition; if we find a file now and it is deleted before we
4999 exec it, we lose, even if the deletion leaves a valid file
5000 further down in the PATH, (2) there is no way to know exactly
5001 what an executable (in the sense of "capable of being
5002 exec'd") file is. Using access() loses because it may lose
5003 if the caller is the superuser; failing to use it loses if
5004 there are ACLs or some such. */
5005
5006 char *p;
5007 char *p1;
5008 /* FIXME-maybe: might want "set path" command so user can change what
5009 path is used from within GDB. */
5010 char *path = getenv ("PATH");
5011 int len;
5012 struct stat statbuf;
5013
5014 if (path == NULL)
5015 path = "/bin:/usr/bin";
5016
5017 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
5018 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
5019 {
5020 p1 = strchr (p, ':');
5021 if (p1 != NULL)
5022 len = p1 - p;
5023 else
5024 len = strlen (p);
5025 strncpy (tryname, p, len);
5026 tryname[len] = '\0';
5027 strcat (tryname, "/");
5028 strcat (tryname, shell_file);
5029 if (access (tryname, X_OK) < 0)
5030 continue;
5031 if (stat (tryname, &statbuf) < 0)
5032 continue;
5033 if (!S_ISREG (statbuf.st_mode))
5034 /* We certainly need to reject directories. I'm not quite
5035 as sure about FIFOs, sockets, etc., but I kind of doubt
5036 that people want to exec() these things. */
5037 continue;
5038 break;
5039 }
5040 if (p == NULL)
5041 /* Not found. This must be an error rather than merely passing
5042 the file to execlp(), because execlp() would try all the
5043 exec()s, causing GDB to get confused. */
5044 error ("procfs:%d -- Can't find shell %s in PATH",
5045 __LINE__, shell_file);
5046
5047 shell_file = tryname;
5048 }
5049
5050 fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
5051 procfs_init_inferior, NULL, shell_file);
5052
5053 /* We are at the first instruction we care about. */
5054 /* Pedal to the metal... */
5055
5056 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
5057 }
5058
5059 /*
5060 * Function: notice_thread
5061 *
5062 * Callback for find_new_threads.
5063 * Calls "add_thread".
5064 */
5065
5066 static int
5067 procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
5068 {
5069 int gdb_threadid = MERGEPID (pi->pid, thread->tid);
5070
5071 if (!in_thread_list (gdb_threadid))
5072 add_thread (gdb_threadid);
5073
5074 return 0;
5075 }
5076
5077 /*
5078 * Function: target_find_new_threads
5079 *
5080 * Query all the threads that the target knows about,
5081 * and give them back to GDB to add to its list.
5082 */
5083
5084 void
5085 procfs_find_new_threads (void)
5086 {
5087 procinfo *pi;
5088
5089 /* Find procinfo for main process */
5090 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
5091 proc_update_threads (pi);
5092 proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
5093 }
5094
5095 /*
5096 * Function: target_thread_alive
5097 *
5098 * Return true if the thread is still 'alive'.
5099 *
5100 * This guy doesn't really seem to be doing his job.
5101 * Got to investigate how to tell when a thread is really gone.
5102 */
5103
5104 static int
5105 procfs_thread_alive (int pid)
5106 {
5107 int proc, thread;
5108 procinfo *pi;
5109
5110 proc = PIDGET (pid);
5111 thread = TIDGET (pid);
5112 /* If I don't know it, it ain't alive! */
5113 if ((pi = find_procinfo (proc, thread)) == NULL)
5114 return 0;
5115
5116 /* If I can't get its status, it ain't alive!
5117 What's more, I need to forget about it! */
5118 if (!proc_get_status (pi))
5119 {
5120 destroy_procinfo (pi);
5121 return 0;
5122 }
5123 /* I couldn't have got its status if it weren't alive, so it's alive. */
5124 return 1;
5125 }
5126
5127 /*
5128 * Function: target_pid_to_str
5129 *
5130 * Return a string to be used to identify the thread in
5131 * the "info threads" display.
5132 */
5133
5134 char *
5135 procfs_pid_to_str (int pid)
5136 {
5137 static char buf[80];
5138 int proc, thread;
5139 procinfo *pi;
5140
5141 proc = PIDGET (pid);
5142 thread = TIDGET (pid);
5143 pi = find_procinfo (proc, thread);
5144
5145 if (thread == 0)
5146 sprintf (buf, "Process %d", proc);
5147 else
5148 sprintf (buf, "LWP %d", thread);
5149 return &buf[0];
5150 }
5151
5152 /*
5153 * Function: procfs_set_watchpoint
5154 * Insert a watchpoint
5155 */
5156
5157 int
5158 procfs_set_watchpoint (int pid, CORE_ADDR addr, int len, int rwflag, int after)
5159 {
5160 #ifndef UNIXWARE
5161 #ifndef AIX5
5162 int pflags = 0;
5163 procinfo *pi;
5164
5165 pi = find_procinfo_or_die (pid == -1 ?
5166 PIDGET (inferior_pid) : PIDGET (pid), 0);
5167
5168 /* Translate from GDB's flags to /proc's */
5169 if (len > 0) /* len == 0 means delete watchpoint */
5170 {
5171 switch (rwflag) { /* FIXME: need an enum! */
5172 case hw_write: /* default watchpoint (write) */
5173 pflags = WRITE_WATCHFLAG;
5174 break;
5175 case hw_read: /* read watchpoint */
5176 pflags = READ_WATCHFLAG;
5177 break;
5178 case hw_access: /* access watchpoint */
5179 pflags = READ_WATCHFLAG | WRITE_WATCHFLAG;
5180 break;
5181 case hw_execute: /* execution HW breakpoint */
5182 pflags = EXEC_WATCHFLAG;
5183 break;
5184 default: /* Something weird. Return error. */
5185 return -1;
5186 }
5187 if (after) /* Stop after r/w access is completed. */
5188 pflags |= AFTER_WATCHFLAG;
5189 }
5190
5191 if (!proc_set_watchpoint (pi, addr, len, pflags))
5192 {
5193 if (errno == E2BIG) /* Typical error for no resources */
5194 return -1; /* fail */
5195 /* GDB may try to remove the same watchpoint twice.
5196 If a remove request returns no match, don't error. */
5197 if (errno == ESRCH && len == 0)
5198 return 0; /* ignore */
5199 proc_error (pi, "set_watchpoint", __LINE__);
5200 }
5201 #endif /* AIX5 */
5202 #endif /* UNIXWARE */
5203 return 0;
5204 }
5205
5206 /*
5207 * Function: stopped_by_watchpoint
5208 *
5209 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5210 * else returns zero.
5211 */
5212
5213 int
5214 procfs_stopped_by_watchpoint (int pid)
5215 {
5216 procinfo *pi;
5217
5218 pi = find_procinfo (pid == -1 ?
5219 PIDGET (inferior_pid) : PIDGET (pid), 0);
5220
5221 if (!pi) /* If no process, then not stopped by watchpoint! */
5222 return 0;
5223
5224 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
5225 {
5226 if (proc_why (pi) == PR_FAULTED)
5227 {
5228 #ifdef FLTWATCH
5229 if (proc_what (pi) == FLTWATCH)
5230 return 1;
5231 #endif
5232 #ifdef FLTKWATCH
5233 if (proc_what (pi) == FLTKWATCH)
5234 return 1;
5235 #endif
5236 }
5237 }
5238 return 0;
5239 }
5240
5241 #ifdef TM_I386SOL2_H
5242 /*
5243 * Function: procfs_find_LDT_entry
5244 *
5245 * Input:
5246 * int pid; // The GDB-style pid-plus-LWP.
5247 *
5248 * Return:
5249 * pointer to the corresponding LDT entry.
5250 */
5251
5252 struct ssd *
5253 procfs_find_LDT_entry (int pid)
5254 {
5255 gdb_gregset_t *gregs;
5256 int key;
5257 procinfo *pi;
5258
5259 /* Find procinfo for the lwp. */
5260 if ((pi = find_procinfo (PIDGET (pid), TIDGET (pid))) == NULL)
5261 {
5262 warning ("procfs_find_LDT_entry: could not find procinfi for %d.",
5263 pid);
5264 return NULL;
5265 }
5266 /* get its general registers. */
5267 if ((gregs = proc_get_gregs (pi)) == NULL)
5268 {
5269 warning ("procfs_find_LDT_entry: could not read gregs for %d.",
5270 pid);
5271 return NULL;
5272 }
5273 /* Now extract the GS register's lower 16 bits. */
5274 key = (*gregs)[GS] & 0xffff;
5275
5276 /* Find the matching entry and return it. */
5277 return proc_get_LDT_entry (pi, key);
5278 }
5279 #endif /* TM_I386SOL2_H */
5280
5281
5282
5283 static void
5284 info_proc_cmd (char *args, int from_tty)
5285 {
5286 struct cleanup *old_chain;
5287 procinfo *process = NULL;
5288 procinfo *thread = NULL;
5289 char **argv = NULL;
5290 char *tmp = NULL;
5291 int pid = 0;
5292 int tid = 0;
5293
5294 old_chain = make_cleanup (null_cleanup, 0);
5295 if (args)
5296 {
5297 if ((argv = buildargv (args)) == NULL)
5298 nomem (0);
5299 else
5300 make_cleanup_freeargv (argv);
5301 }
5302 while (argv != NULL && *argv != NULL)
5303 {
5304 if (isdigit (argv[0][0]))
5305 {
5306 pid = strtoul (argv[0], &tmp, 10);
5307 if (*tmp == '/')
5308 tid = strtoul (++tmp, NULL, 10);
5309 }
5310 else if (argv[0][0] == '/')
5311 {
5312 tid = strtoul (argv[0] + 1, NULL, 10);
5313 }
5314 else
5315 {
5316 /* [...] */
5317 }
5318 argv++;
5319 }
5320 if (pid == 0)
5321 pid = PIDGET (inferior_pid);
5322 if (pid == 0)
5323 error ("No current process: you must name one.");
5324 else
5325 {
5326 /* Have pid, will travel.
5327 First see if it's a process we're already debugging. */
5328 process = find_procinfo (pid, 0);
5329 if (process == NULL)
5330 {
5331 /* No. So open a procinfo for it, but
5332 remember to close it again when finished. */
5333 process = create_procinfo (pid, 0);
5334 make_cleanup (do_destroy_procinfo_cleanup, process);
5335 if (!open_procinfo_files (process, FD_CTL))
5336 proc_error (process, "info proc, open_procinfo_files", __LINE__);
5337 }
5338 }
5339 if (tid != 0)
5340 thread = create_procinfo (pid, tid);
5341
5342 if (process)
5343 {
5344 printf_filtered ("process %d flags:\n", process->pid);
5345 proc_prettyprint_flags (proc_flags (process), 1);
5346 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
5347 proc_prettyprint_why (proc_why (process), proc_what (process), 1);
5348 if (proc_get_nthreads (process) > 1)
5349 printf_filtered ("Process has %d threads.\n",
5350 proc_get_nthreads (process));
5351 }
5352 if (thread)
5353 {
5354 printf_filtered ("thread %d flags:\n", thread->tid);
5355 proc_prettyprint_flags (proc_flags (thread), 1);
5356 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
5357 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
5358 }
5359
5360 do_cleanups (old_chain);
5361 }
5362
5363 static void
5364 proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode)
5365 {
5366 procinfo *pi;
5367 sysset_t *sysset;
5368 int syscallnum = 0;
5369
5370 if (inferior_pid <= 0)
5371 error ("you must be debugging a process to use this command.");
5372
5373 if (args == NULL || args[0] == 0)
5374 error_no_arg ("system call to trace");
5375
5376 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
5377 if (isdigit (args[0]))
5378 {
5379 syscallnum = atoi (args);
5380 if (entry_or_exit == PR_SYSENTRY)
5381 sysset = proc_get_traced_sysentry (pi, NULL);
5382 else
5383 sysset = proc_get_traced_sysexit (pi, NULL);
5384
5385 if (sysset == NULL)
5386 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
5387
5388 if (mode == FLAG_SET)
5389 gdb_praddsysset (sysset, syscallnum);
5390 else
5391 gdb_prdelsysset (sysset, syscallnum);
5392
5393 if (entry_or_exit == PR_SYSENTRY)
5394 {
5395 if (!proc_set_traced_sysentry (pi, sysset))
5396 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
5397 }
5398 else
5399 {
5400 if (!proc_set_traced_sysexit (pi, sysset))
5401 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
5402 }
5403 }
5404 }
5405
5406 static void
5407 proc_trace_sysentry_cmd (char *args, int from_tty)
5408 {
5409 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
5410 }
5411
5412 static void
5413 proc_trace_sysexit_cmd (char *args, int from_tty)
5414 {
5415 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
5416 }
5417
5418 static void
5419 proc_untrace_sysentry_cmd (char *args, int from_tty)
5420 {
5421 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
5422 }
5423
5424 static void
5425 proc_untrace_sysexit_cmd (char *args, int from_tty)
5426 {
5427 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
5428 }
5429
5430
5431 void
5432 _initialize_procfs (void)
5433 {
5434 init_procfs_ops ();
5435 add_target (&procfs_ops);
5436 add_info ("proc", info_proc_cmd,
5437 "Show /proc process information about any running process.\
5438 Default is the process being debugged.");
5439 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
5440 "Give a trace of entries into the syscall.");
5441 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
5442 "Give a trace of exits from the syscall.");
5443 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
5444 "Cancel a trace of entries into the syscall.");
5445 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
5446 "Cancel a trace of exits from the syscall.");
5447 }
5448
5449 /* =================== END, GDB "MODULE" =================== */
5450
5451
5452
5453 /* miscelaneous stubs: */
5454 /* The following satisfy a few random symbols mostly created by */
5455 /* the solaris threads implementation, which I will chase down */
5456 /* later. */
5457
5458 /*
5459 * Return a pid for which we guarantee
5460 * we will be able to find a 'live' procinfo.
5461 */
5462
5463 int
5464 procfs_first_available (void)
5465 {
5466 if (procinfo_list)
5467 return procinfo_list->pid;
5468 else
5469 return -1;
5470 }
This page took 0.153223 seconds and 3 git commands to generate.