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.
6 This file is part of GDB.
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.
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.
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. */
27 #include "gdbthread.h"
29 #if defined (NEW_PROC_API)
30 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
33 #include <sys/procfs.h>
34 #include <sys/fault.h>
35 #include <sys/syscall.h>
36 #include <sys/errno.h>
44 * This module provides the interface between GDB and the
45 * /proc file system, which is used on many versions of Unix
46 * as a means for debuggers to control other processes.
47 * Examples of the systems that use this interface are:
53 * /proc works by immitating a file system: you open a simulated file
54 * that represents the process you wish to interact with, and
55 * perform operations on that "file" in order to examine or change
56 * the state of the other process.
58 * The most important thing to know about /proc and this module
59 * is that there are two very different interfaces to /proc:
60 * One that uses the ioctl system call, and
61 * another that uses read and write system calls.
62 * This module has to support both /proc interfaces. This means
63 * that there are two different ways of doing every basic operation.
65 * In order to keep most of the code simple and clean, I have
66 * defined an interface "layer" which hides all these system calls.
67 * An ifdef (NEW_PROC_API) determines which interface we are using,
68 * and most or all occurrances of this ifdef should be confined to
69 * this interface layer.
73 /* Determine which /proc API we are using:
74 The ioctl API defines PIOCSTATUS, while
75 the read/write (multiple fd) API never does. */
78 #include <sys/types.h>
79 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
82 #include <fcntl.h> /* for O_RDONLY */
83 #include <unistd.h> /* for "X_OK" */
84 #include "gdb_stat.h" /* for struct stat */
86 /* Note: procfs-utils.h must be included after the above system header
87 files, because it redefines various system calls using macros.
88 This may be incompatible with the prototype declarations. */
90 #include "proc-utils.h"
92 /* Prototypes for supply_gregset etc. */
95 /* =================== TARGET_OPS "MODULE" =================== */
98 * This module defines the GDB target vector and its methods.
101 static void procfs_open (char *, int);
102 static void procfs_attach (char *, int);
103 static void procfs_detach (char *, int);
104 static void procfs_resume (int, int, enum target_signal
);
105 static int procfs_can_run (void);
106 static void procfs_stop (void);
107 static void procfs_files_info (struct target_ops
*);
108 static void procfs_fetch_registers (int);
109 static void procfs_store_registers (int);
110 static void procfs_notice_signals (int);
111 static void procfs_prepare_to_store (void);
112 static void procfs_kill_inferior (void);
113 static void procfs_mourn_inferior (void);
114 static void procfs_create_inferior (char *, char *, char **);
115 static int procfs_wait (int, struct target_waitstatus
*);
116 static int procfs_xfer_memory (CORE_ADDR
, char *, int, int,
117 struct mem_attrib
*attrib
,
118 struct target_ops
*);
120 static int procfs_thread_alive (int);
122 void procfs_find_new_threads (void);
123 char *procfs_pid_to_str (int);
125 struct target_ops procfs_ops
; /* the target vector */
128 init_procfs_ops (void)
130 procfs_ops
.to_shortname
= "procfs";
131 procfs_ops
.to_longname
= "Unix /proc child process";
133 "Unix /proc child process (started by the \"run\" command).";
134 procfs_ops
.to_open
= procfs_open
;
135 procfs_ops
.to_can_run
= procfs_can_run
;
136 procfs_ops
.to_create_inferior
= procfs_create_inferior
;
137 procfs_ops
.to_kill
= procfs_kill_inferior
;
138 procfs_ops
.to_mourn_inferior
= procfs_mourn_inferior
;
139 procfs_ops
.to_attach
= procfs_attach
;
140 procfs_ops
.to_detach
= procfs_detach
;
141 procfs_ops
.to_wait
= procfs_wait
;
142 procfs_ops
.to_resume
= procfs_resume
;
143 procfs_ops
.to_prepare_to_store
= procfs_prepare_to_store
;
144 procfs_ops
.to_fetch_registers
= procfs_fetch_registers
;
145 procfs_ops
.to_store_registers
= procfs_store_registers
;
146 procfs_ops
.to_xfer_memory
= procfs_xfer_memory
;
147 procfs_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
148 procfs_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
149 procfs_ops
.to_notice_signals
= procfs_notice_signals
;
150 procfs_ops
.to_files_info
= procfs_files_info
;
151 procfs_ops
.to_stop
= procfs_stop
;
153 procfs_ops
.to_terminal_init
= terminal_init_inferior
;
154 procfs_ops
.to_terminal_inferior
= terminal_inferior
;
155 procfs_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
156 procfs_ops
.to_terminal_ours
= terminal_ours
;
157 procfs_ops
.to_terminal_info
= child_terminal_info
;
159 procfs_ops
.to_find_new_threads
= procfs_find_new_threads
;
160 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
161 procfs_ops
.to_pid_to_str
= procfs_pid_to_str
;
163 procfs_ops
.to_has_all_memory
= 1;
164 procfs_ops
.to_has_memory
= 1;
165 procfs_ops
.to_has_execution
= 1;
166 procfs_ops
.to_has_stack
= 1;
167 procfs_ops
.to_has_registers
= 1;
168 procfs_ops
.to_stratum
= process_stratum
;
169 procfs_ops
.to_has_thread_control
= tc_schedlock
;
170 procfs_ops
.to_magic
= OPS_MAGIC
;
173 /* =================== END, TARGET_OPS "MODULE" =================== */
178 * Put any typedefs, defines etc. here that are required for
179 * the unification of code that handles different versions of /proc.
182 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
184 enum { READ_WATCHFLAG
= WA_READ
,
185 WRITE_WATCHFLAG
= WA_WRITE
,
186 EXEC_WATCHFLAG
= WA_EXEC
,
187 AFTER_WATCHFLAG
= WA_TRAPAFTER
190 #else /* Irix method for watchpoints */
191 enum { READ_WATCHFLAG
= MA_READ
,
192 WRITE_WATCHFLAG
= MA_WRITE
,
193 EXEC_WATCHFLAG
= MA_EXEC
,
194 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
201 /* =================== STRUCT PROCINFO "MODULE" =================== */
203 /* FIXME: this comment will soon be out of date W.R.T. threads. */
205 /* The procinfo struct is a wrapper to hold all the state information
206 concerning a /proc process. There should be exactly one procinfo
207 for each process, and since GDB currently can debug only one
208 process at a time, that means there should be only one procinfo.
209 All of the LWP's of a process can be accessed indirectly thru the
210 single process procinfo.
212 However, against the day when GDB may debug more than one process,
213 this data structure is kept in a list (which for now will hold no
214 more than one member), and many functions will have a pointer to a
215 procinfo as an argument.
217 There will be a separate procinfo structure for use by the (not yet
218 implemented) "info proc" command, so that we can print useful
219 information about any random process without interfering with the
220 inferior's procinfo information. */
223 /* format strings for /proc paths */
224 # ifndef CTL_PROC_NAME_FMT
225 # define MAIN_PROC_NAME_FMT "/proc/%d"
226 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
227 # define AS_PROC_NAME_FMT "/proc/%d/as"
228 # define MAP_PROC_NAME_FMT "/proc/%d/map"
229 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
230 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
232 /* the name of the proc status struct depends on the implementation */
233 typedef pstatus_t gdb_prstatus_t
;
234 typedef lwpstatus_t gdb_lwpstatus_t
;
235 #else /* ! NEW_PROC_API */
236 /* format strings for /proc paths */
237 # ifndef CTL_PROC_NAME_FMT
238 # define MAIN_PROC_NAME_FMT "/proc/%05d"
239 # define CTL_PROC_NAME_FMT "/proc/%05d"
240 # define AS_PROC_NAME_FMT "/proc/%05d"
241 # define MAP_PROC_NAME_FMT "/proc/%05d"
242 # define STATUS_PROC_NAME_FMT "/proc/%05d"
243 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
245 /* the name of the proc status struct depends on the implementation */
246 typedef prstatus_t gdb_prstatus_t
;
247 typedef prstatus_t gdb_lwpstatus_t
;
248 #endif /* NEW_PROC_API */
250 /* Provide default composite pid manipulation macros for systems that
251 don't have threads. */
254 #define PIDGET(PID) (PID)
255 #define TIDGET(PID) (PID)
258 #define MERGEPID(PID, TID) (PID)
261 typedef struct procinfo
{
262 struct procinfo
*next
;
263 int pid
; /* Process ID */
264 int tid
; /* Thread/LWP id */
268 int ignore_next_sigstop
;
270 /* The following four fd fields may be identical, or may contain
271 several different fd's, depending on the version of /proc
272 (old ioctl or new read/write). */
274 int ctl_fd
; /* File descriptor for /proc control file */
276 * The next three file descriptors are actually only needed in the
277 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
278 * However, to avoid a bunch of #ifdefs in the code, we will use
279 * them uniformly by (in the case of the ioctl single-file-descriptor
280 * implementation) filling them with copies of the control fd.
282 int status_fd
; /* File descriptor for /proc status file */
283 int as_fd
; /* File descriptor for /proc as file */
285 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
287 fltset_t saved_fltset
; /* Saved traced hardware fault set */
288 sigset_t saved_sigset
; /* Saved traced signal set */
289 sigset_t saved_sighold
; /* Saved held signal set */
290 sysset_t saved_exitset
; /* Saved traced system call exit set */
291 sysset_t saved_entryset
; /* Saved traced system call entry set */
293 gdb_prstatus_t prstatus
; /* Current process status info */
296 gdb_fpregset_t fpregset
; /* Current floating point registers */
299 struct procinfo
*thread_list
;
301 int status_valid
: 1;
303 int fpregs_valid
: 1;
304 int threads_valid
: 1;
307 static char errmsg
[128]; /* shared error msg buffer */
309 /* Function prototypes for procinfo module: */
311 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
312 static procinfo
*find_procinfo (int pid
, int tid
);
313 static procinfo
*create_procinfo (int pid
, int tid
);
314 static void destroy_procinfo (procinfo
* p
);
315 static void do_destroy_procinfo_cleanup (void *);
316 static void dead_procinfo (procinfo
* p
, char *msg
, int killp
);
317 static int open_procinfo_files (procinfo
* p
, int which
);
318 static void close_procinfo_files (procinfo
* p
);
320 /* The head of the procinfo list: */
321 static procinfo
* procinfo_list
;
324 * Function: find_procinfo
326 * Search the procinfo list.
328 * Returns: pointer to procinfo, or NULL if not found.
332 find_procinfo (int pid
, int tid
)
336 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
343 /* Don't check threads_valid. If we're updating the
344 thread_list, we want to find whatever threads are already
345 here. This means that in general it is the caller's
346 responsibility to check threads_valid and update before
347 calling find_procinfo, if the caller wants to find a new
350 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
359 * Function: find_procinfo_or_die
361 * Calls find_procinfo, but errors on failure.
365 find_procinfo_or_die (int pid
, int tid
)
367 procinfo
*pi
= find_procinfo (pid
, tid
);
372 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
375 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
381 * Function: open_procinfo_files
383 * Open the file descriptor for the process or LWP.
384 * ifdef NEW_PROC_API, we only open the control file descriptor;
385 * the others are opened lazily as needed.
386 * else (if not NEW_PROC_API), there is only one real
387 * file descriptor, but we keep multiple copies of it so that
388 * the code that uses them does not have to be #ifdef'd.
390 * Return: file descriptor, or zero for failure.
393 enum { FD_CTL
, FD_STATUS
, FD_AS
};
396 open_procinfo_files (procinfo
*pi
, int which
)
399 char tmp
[MAX_PROC_NAME_SIZE
];
404 * This function is getting ALMOST long enough to break up into several.
405 * Here is some rationale:
407 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
408 * There are several file descriptors that may need to be open
409 * for any given process or LWP. The ones we're intereted in are:
410 * - control (ctl) write-only change the state
411 * - status (status) read-only query the state
412 * - address space (as) read/write access memory
413 * - map (map) read-only virtual addr map
414 * Most of these are opened lazily as they are needed.
415 * The pathnames for the 'files' for an LWP look slightly
416 * different from those of a first-class process:
417 * Pathnames for a process (<proc-id>):
418 * /proc/<proc-id>/ctl
419 * /proc/<proc-id>/status
421 * /proc/<proc-id>/map
422 * Pathnames for an LWP (lwp-id):
423 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
424 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
425 * An LWP has no map or address space file descriptor, since
426 * the memory map and address space are shared by all LWPs.
428 * Everyone else (Solaris 2.5, Irix, OSF)
429 * There is only one file descriptor for each process or LWP.
430 * For convenience, we copy the same file descriptor into all
431 * three fields of the procinfo struct (ctl_fd, status_fd, and
432 * as_fd, see NEW_PROC_API above) so that code that uses them
433 * doesn't need any #ifdef's.
438 * Each LWP has an independent file descriptor, but these
439 * are not obtained via the 'open' system call like the rest:
440 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
441 * to the file descriptor of the parent process.
444 * These do not even have their own independent file descriptor.
445 * All operations are carried out on the file descriptor of the
446 * parent process. Therefore we just call open again for each
447 * thread, getting a new handle for the same 'file'.
452 * In this case, there are several different file descriptors that
453 * we might be asked to open. The control file descriptor will be
454 * opened early, but the others will be opened lazily as they are
458 strcpy (tmp
, pi
->pathname
);
459 switch (which
) { /* which file descriptor to open? */
462 strcat (tmp
, "/lwpctl");
464 strcat (tmp
, "/ctl");
465 fd
= open (tmp
, O_WRONLY
);
472 return 0; /* there is no 'as' file descriptor for an lwp */
474 fd
= open (tmp
, O_RDWR
);
481 strcat (tmp
, "/lwpstatus");
483 strcat (tmp
, "/status");
484 fd
= open (tmp
, O_RDONLY
);
490 return 0; /* unknown file descriptor */
492 #else /* not NEW_PROC_API */
494 * In this case, there is only one file descriptor for each procinfo
495 * (ie. each process or LWP). In fact, only the file descriptor for
496 * the process can actually be opened by an 'open' system call.
497 * The ones for the LWPs have to be obtained thru an IOCTL call
498 * on the process's file descriptor.
500 * For convenience, we copy each procinfo's single file descriptor
501 * into all of the fields occupied by the several file descriptors
502 * of the NEW_PROC_API implementation. That way, the code that uses
503 * them can be written without ifdefs.
507 #ifdef PIOCTSTATUS /* OSF */
508 if ((fd
= open (pi
->pathname
, O_RDWR
)) == 0) /* Only one FD; just open it. */
510 #else /* Sol 2.5, Irix, other? */
511 if (pi
->tid
== 0) /* Master procinfo for the process */
513 fd
= open (pi
->pathname
, O_RDWR
);
517 else /* LWP thread procinfo */
519 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
523 /* Find the procinfo for the entire process. */
524 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
527 /* Now obtain the file descriptor for the LWP. */
528 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
530 #else /* Irix, other? */
531 return 0; /* Don't know how to open threads */
532 #endif /* Sol 2.5 PIOCOPENLWP */
534 #endif /* OSF PIOCTSTATUS */
535 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
536 #endif /* NEW_PROC_API */
538 return 1; /* success */
542 * Function: create_procinfo
544 * Allocate a data structure and link it into the procinfo list.
545 * (First tries to find a pre-existing one (FIXME: why?)
547 * Return: pointer to new procinfo struct.
551 create_procinfo (int pid
, int tid
)
553 procinfo
*pi
, *parent
;
555 if ((pi
= find_procinfo (pid
, tid
)))
556 return pi
; /* Already exists, nothing to do. */
558 /* find parent before doing malloc, to save having to cleanup */
560 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
562 doesn't exist yet? */
564 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
565 memset (pi
, 0, sizeof (procinfo
));
569 /* Chain into list. */
572 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
573 pi
->next
= procinfo_list
;
579 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
581 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
583 pi
->next
= parent
->thread_list
;
584 parent
->thread_list
= pi
;
590 * Function: close_procinfo_files
592 * Close all file descriptors associated with the procinfo
596 close_procinfo_files (procinfo
*pi
)
603 if (pi
->status_fd
> 0)
604 close (pi
->status_fd
);
606 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
610 * Function: destroy_procinfo
612 * Destructor function. Close, unlink and deallocate the object.
616 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
620 /* Step one: unlink the procinfo from its list */
624 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
627 ptr
->next
= pi
->next
;
631 /* Step two: close any open file descriptors */
632 close_procinfo_files (pi
);
634 /* Step three: free the memory. */
639 destroy_procinfo (procinfo
*pi
)
643 if (pi
->tid
!= 0) /* destroy a thread procinfo */
645 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
646 destroy_one_procinfo (&tmp
->thread_list
, pi
);
648 else /* destroy a process procinfo and all its threads */
650 /* First destroy the children, if any; */
651 while (pi
->thread_list
!= NULL
)
652 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
653 /* Then destroy the parent. Genocide!!! */
654 destroy_one_procinfo (&procinfo_list
, pi
);
659 do_destroy_procinfo_cleanup (void *pi
)
661 destroy_procinfo (pi
);
664 enum { NOKILL
, KILL
};
667 * Function: dead_procinfo
669 * To be called on a non_recoverable error for a procinfo.
670 * Prints error messages, optionally sends a SIGKILL to the process,
671 * then destroys the data structure.
675 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
681 print_sys_errmsg (pi
->pathname
, errno
);
685 sprintf (procfile
, "process %d", pi
->pid
);
686 print_sys_errmsg (procfile
, errno
);
689 kill (pi
->pid
, SIGKILL
);
691 destroy_procinfo (pi
);
695 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
697 /* =================== /proc "MODULE" =================== */
700 * This "module" is the interface layer between the /proc system API
701 * and the gdb target vector functions. This layer consists of
702 * access functions that encapsulate each of the basic operations
703 * that we need to use from the /proc API.
705 * The main motivation for this layer is to hide the fact that
706 * there are two very different implementations of the /proc API.
707 * Rather than have a bunch of #ifdefs all thru the gdb target vector
708 * functions, we do our best to hide them all in here.
711 int proc_get_status (procinfo
* pi
);
712 long proc_flags (procinfo
* pi
);
713 int proc_why (procinfo
* pi
);
714 int proc_what (procinfo
* pi
);
715 int proc_set_run_on_last_close (procinfo
* pi
);
716 int proc_unset_run_on_last_close (procinfo
* pi
);
717 int proc_set_inherit_on_fork (procinfo
* pi
);
718 int proc_unset_inherit_on_fork (procinfo
* pi
);
719 int proc_set_async (procinfo
* pi
);
720 int proc_unset_async (procinfo
* pi
);
721 int proc_stop_process (procinfo
* pi
);
722 int proc_trace_signal (procinfo
* pi
, int signo
);
723 int proc_ignore_signal (procinfo
* pi
, int signo
);
724 int proc_clear_current_fault (procinfo
* pi
);
725 int proc_set_current_signal (procinfo
* pi
, int signo
);
726 int proc_clear_current_signal (procinfo
* pi
);
727 int proc_set_gregs (procinfo
* pi
);
728 int proc_set_fpregs (procinfo
* pi
);
729 int proc_wait_for_stop (procinfo
* pi
);
730 int proc_run_process (procinfo
* pi
, int step
, int signo
);
731 int proc_kill (procinfo
* pi
, int signo
);
732 int proc_parent_pid (procinfo
* pi
);
733 int proc_get_nthreads (procinfo
* pi
);
734 int proc_get_current_thread (procinfo
* pi
);
735 int proc_set_held_signals (procinfo
* pi
, sigset_t
* sighold
);
736 int proc_set_traced_sysexit (procinfo
* pi
, sysset_t
* sysset
);
737 int proc_set_traced_sysentry (procinfo
* pi
, sysset_t
* sysset
);
738 int proc_set_traced_faults (procinfo
* pi
, fltset_t
* fltset
);
739 int proc_set_traced_signals (procinfo
* pi
, sigset_t
* sigset
);
741 int proc_update_threads (procinfo
* pi
);
742 int proc_iterate_over_threads (procinfo
* pi
,
743 int (*func
) (procinfo
*, procinfo
*, void *),
746 gdb_gregset_t
*proc_get_gregs (procinfo
* pi
);
747 gdb_fpregset_t
*proc_get_fpregs (procinfo
* pi
);
748 sysset_t
*proc_get_traced_sysexit (procinfo
* pi
, sysset_t
* save
);
749 sysset_t
*proc_get_traced_sysentry (procinfo
* pi
, sysset_t
* save
);
750 fltset_t
*proc_get_traced_faults (procinfo
* pi
, fltset_t
* save
);
751 sigset_t
*proc_get_traced_signals (procinfo
* pi
, sigset_t
* save
);
752 sigset_t
*proc_get_held_signals (procinfo
* pi
, sigset_t
* save
);
753 sigset_t
*proc_get_pending_signals (procinfo
* pi
, sigset_t
* save
);
754 struct sigaction
*proc_get_signal_actions (procinfo
* pi
,
755 struct sigaction
*save
);
757 void proc_warn (procinfo
* pi
, char *func
, int line
);
758 void proc_error (procinfo
* pi
, char *func
, int line
);
761 proc_warn (procinfo
*pi
, char *func
, int line
)
763 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
764 print_sys_errmsg (errmsg
, errno
);
768 proc_error (procinfo
*pi
, char *func
, int line
)
770 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
771 perror_with_name (errmsg
);
775 * Function: proc_get_status
777 * Updates the status struct in the procinfo.
778 * There is a 'valid' flag, to let other functions know when
779 * this function needs to be called (so the status is only
780 * read when it is needed). The status file descriptor is
781 * also only opened when it is needed.
783 * Return: non-zero for success, zero for failure.
787 proc_get_status (procinfo
*pi
)
789 /* Status file descriptor is opened "lazily" */
790 if (pi
->status_fd
== 0 &&
791 open_procinfo_files (pi
, FD_STATUS
) == 0)
793 pi
->status_valid
= 0;
798 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
799 pi
->status_valid
= 0; /* fail */
802 /* Sigh... I have to read a different data structure,
803 depending on whether this is a main process or an LWP. */
805 pi
->status_valid
= (read (pi
->status_fd
,
806 (char *) &pi
->prstatus
.pr_lwp
,
807 sizeof (lwpstatus_t
))
808 == sizeof (lwpstatus_t
));
811 pi
->status_valid
= (read (pi
->status_fd
,
812 (char *) &pi
->prstatus
,
813 sizeof (gdb_prstatus_t
))
814 == sizeof (gdb_prstatus_t
));
815 #if 0 /*def UNIXWARE*/
816 if (pi
->status_valid
&&
817 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
818 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
819 /* Unixware peculiarity -- read the damn thing again! */
820 pi
->status_valid
= (read (pi
->status_fd
,
821 (char *) &pi
->prstatus
,
822 sizeof (gdb_prstatus_t
))
823 == sizeof (gdb_prstatus_t
));
824 #endif /* UNIXWARE */
827 #else /* ioctl method */
828 #ifdef PIOCTSTATUS /* osf */
829 if (pi
->tid
== 0) /* main process */
831 /* Just read the danged status. Now isn't that simple? */
833 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
840 tid_t pr_error_thread
;
841 struct prstatus status
;
844 thread_status
.pr_count
= 1;
845 thread_status
.status
.pr_tid
= pi
->tid
;
846 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
849 memcpy (&pi
->prstatus
, &thread_status
.status
,
850 sizeof (pi
->prstatus
));
851 pi
->status_valid
= 1;
855 /* Just read the danged status. Now isn't that simple? */
856 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
860 if (pi
->status_valid
)
862 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
865 proc_get_current_thread (pi
));
868 /* The status struct includes general regs, so mark them valid too */
869 pi
->gregs_valid
= pi
->status_valid
;
871 /* In the read/write multiple-fd model,
872 the status struct includes the fp regs too, so mark them valid too */
873 pi
->fpregs_valid
= pi
->status_valid
;
875 return pi
->status_valid
; /* True if success, false if failure. */
879 * Function: proc_flags
881 * returns the process flags (pr_flags field).
885 proc_flags (procinfo
*pi
)
887 if (!pi
->status_valid
)
888 if (!proc_get_status (pi
))
889 return 0; /* FIXME: not a good failure value (but what is?) */
893 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
894 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
895 The two sets of flags don't overlap. */
896 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
898 return pi
->prstatus
.pr_lwp
.pr_flags
;
901 return pi
->prstatus
.pr_flags
;
908 * returns the pr_why field (why the process stopped).
912 proc_why (procinfo
*pi
)
914 if (!pi
->status_valid
)
915 if (!proc_get_status (pi
))
916 return 0; /* FIXME: not a good failure value (but what is?) */
919 return pi
->prstatus
.pr_lwp
.pr_why
;
921 return pi
->prstatus
.pr_why
;
926 * Function: proc_what
928 * returns the pr_what field (details of why the process stopped).
932 proc_what (procinfo
*pi
)
934 if (!pi
->status_valid
)
935 if (!proc_get_status (pi
))
936 return 0; /* FIXME: not a good failure value (but what is?) */
939 return pi
->prstatus
.pr_lwp
.pr_what
;
941 return pi
->prstatus
.pr_what
;
945 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
947 * Function: proc_nsysarg
949 * returns the pr_nsysarg field (number of args to the current syscall).
953 proc_nsysarg (procinfo
*pi
)
955 if (!pi
->status_valid
)
956 if (!proc_get_status (pi
))
960 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
962 return pi
->prstatus
.pr_nsysarg
;
967 * Function: proc_sysargs
969 * returns the pr_sysarg field (pointer to the arguments of current syscall).
973 proc_sysargs (procinfo
*pi
)
975 if (!pi
->status_valid
)
976 if (!proc_get_status (pi
))
980 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
982 return (long *) &pi
->prstatus
.pr_sysarg
;
987 * Function: proc_syscall
989 * returns the pr_syscall field (id of current syscall if we are in one).
993 proc_syscall (procinfo
*pi
)
995 if (!pi
->status_valid
)
996 if (!proc_get_status (pi
))
1000 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1002 return pi
->prstatus
.pr_syscall
;
1005 #endif /* PIOCSSPCACT */
1008 * Function: proc_cursig:
1010 * returns the pr_cursig field (current signal).
1014 proc_cursig (struct procinfo
*pi
)
1016 if (!pi
->status_valid
)
1017 if (!proc_get_status (pi
))
1018 return 0; /* FIXME: not a good failure value (but what is?) */
1021 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1023 return pi
->prstatus
.pr_cursig
;
1028 * Function: proc_modify_flag
1030 * === I appologize for the messiness of this function.
1031 * === This is an area where the different versions of
1032 * === /proc are more inconsistent than usual. MVS
1034 * Set or reset any of the following process flags:
1035 * PR_FORK -- forked child will inherit trace flags
1036 * PR_RLC -- traced process runs when last /proc file closed.
1037 * PR_KLC -- traced process is killed when last /proc file closed.
1038 * PR_ASYNC -- LWP's get to run/stop independently.
1040 * There are three methods for doing this function:
1041 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1043 * 2) Middle: PIOCSET/PIOCRESET
1045 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1048 * Note: Irix does not define PR_ASYNC.
1049 * Note: OSF does not define PR_KLC.
1050 * Note: OSF is the only one that can ONLY use the oldest method.
1053 * pi -- the procinfo
1054 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1055 * mode -- 1 for set, 0 for reset.
1057 * Returns non-zero for success, zero for failure.
1060 enum { FLAG_RESET
, FLAG_SET
};
1063 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1065 long win
= 0; /* default to fail */
1068 * These operations affect the process as a whole, and applying
1069 * them to an individual LWP has the same meaning as applying them
1070 * to the main process. Therefore, if we're ever called with a
1071 * pointer to an LWP's procinfo, let's substitute the process's
1072 * procinfo and avoid opening the LWP's file descriptor
1077 pi
= find_procinfo_or_die (pi
->pid
, 0);
1079 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1080 /* First normalize the PCUNSET/PCRESET command opcode
1081 (which for no obvious reason has a different definition
1082 from one operating system to the next...) */
1084 #define GDBRESET PCUNSET
1087 #define GDBRESET PCRESET
1092 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1094 else /* Reset the flag */
1098 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1101 #ifdef PIOCSET /* Irix/Sol5 method */
1102 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1104 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1106 else /* Reset the flag */
1108 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1112 #ifdef PIOCSRLC /* Oldest method: OSF */
1115 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1117 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1119 else /* Clear run-on-last-close */
1121 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1125 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1127 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1129 else /* Clear inherit-on-fork */
1131 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1135 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1142 /* The above operation renders the procinfo's cached pstatus obsolete. */
1143 pi
->status_valid
= 0;
1146 warning ("procfs: modify_flag failed to turn %s %s",
1147 flag
== PR_FORK
? "PR_FORK" :
1148 flag
== PR_RLC
? "PR_RLC" :
1150 flag
== PR_ASYNC
? "PR_ASYNC" :
1153 flag
== PR_KLC
? "PR_KLC" :
1156 mode
== FLAG_RESET
? "off" : "on");
1162 * Function: proc_set_run_on_last_close
1164 * Set the run_on_last_close flag.
1165 * Process with all threads will become runnable
1166 * when debugger closes all /proc fds.
1168 * Returns non-zero for success, zero for failure.
1172 proc_set_run_on_last_close (procinfo
*pi
)
1174 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1178 * Function: proc_unset_run_on_last_close
1180 * Reset the run_on_last_close flag.
1181 * Process will NOT become runnable
1182 * when debugger closes its file handles.
1184 * Returns non-zero for success, zero for failure.
1188 proc_unset_run_on_last_close (procinfo
*pi
)
1190 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1195 * Function: proc_set_kill_on_last_close
1197 * Set the kill_on_last_close flag.
1198 * Process with all threads will be killed when debugger
1199 * closes all /proc fds (or debugger exits or dies).
1201 * Returns non-zero for success, zero for failure.
1205 proc_set_kill_on_last_close (procinfo
*pi
)
1207 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1211 * Function: proc_unset_kill_on_last_close
1213 * Reset the kill_on_last_close flag.
1214 * Process will NOT be killed when debugger
1215 * closes its file handles (or exits or dies).
1217 * Returns non-zero for success, zero for failure.
1221 proc_unset_kill_on_last_close (procinfo
*pi
)
1223 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1228 * Function: proc_set_inherit_on_fork
1230 * Set inherit_on_fork flag.
1231 * If the process forks a child while we are registered for events
1232 * in the parent, then we will also recieve events from the child.
1234 * Returns non-zero for success, zero for failure.
1238 proc_set_inherit_on_fork (procinfo
*pi
)
1240 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1244 * Function: proc_unset_inherit_on_fork
1246 * Reset inherit_on_fork flag.
1247 * If the process forks a child while we are registered for events
1248 * in the parent, then we will NOT recieve events from the child.
1250 * Returns non-zero for success, zero for failure.
1254 proc_unset_inherit_on_fork (procinfo
*pi
)
1256 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1261 * Function: proc_set_async
1263 * Set PR_ASYNC flag.
1264 * If one LWP stops because of a debug event (signal etc.),
1265 * the remaining LWPs will continue to run.
1267 * Returns non-zero for success, zero for failure.
1271 proc_set_async (procinfo
*pi
)
1273 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1277 * Function: proc_unset_async
1279 * Reset PR_ASYNC flag.
1280 * If one LWP stops because of a debug event (signal etc.),
1281 * then all other LWPs will stop as well.
1283 * Returns non-zero for success, zero for failure.
1287 proc_unset_async (procinfo
*pi
)
1289 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1291 #endif /* PR_ASYNC */
1294 * Function: proc_stop_process
1296 * Request the process/LWP to stop. Does not wait.
1297 * Returns non-zero for success, zero for failure.
1301 proc_stop_process (procinfo
*pi
)
1306 * We might conceivably apply this operation to an LWP, and
1307 * the LWP's ctl file descriptor might not be open.
1310 if (pi
->ctl_fd
== 0 &&
1311 open_procinfo_files (pi
, FD_CTL
) == 0)
1317 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1318 #else /* ioctl method */
1319 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1320 /* Note: the call also reads the prstatus. */
1323 pi
->status_valid
= 1;
1324 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1327 proc_get_current_thread (pi
));
1336 * Function: proc_wait_for_stop
1338 * Wait for the process or LWP to stop (block until it does).
1339 * Returns non-zero for success, zero for failure.
1343 proc_wait_for_stop (procinfo
*pi
)
1348 * We should never have to apply this operation to any procinfo
1349 * except the one for the main process. If that ever changes
1350 * for any reason, then take out the following clause and
1351 * replace it with one that makes sure the ctl_fd is open.
1355 pi
= find_procinfo_or_die (pi
->pid
, 0);
1360 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1361 /* We been runnin' and we stopped -- need to update status. */
1362 pi
->status_valid
= 0;
1364 #else /* ioctl method */
1365 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1366 /* Above call also refreshes the prstatus. */
1369 pi
->status_valid
= 1;
1370 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1373 proc_get_current_thread (pi
));
1381 * Function: proc_run_process
1383 * Make the process or LWP runnable.
1384 * Options (not all are implemented):
1386 * - clear current fault
1387 * - clear current signal
1388 * - abort the current system call
1389 * - stop as soon as finished with system call
1390 * - (ioctl): set traced signal set
1391 * - (ioctl): set held signal set
1392 * - (ioctl): set traced fault set
1393 * - (ioctl): set start pc (vaddr)
1394 * Always clear the current fault.
1395 * Clear the current signal if 'signo' is zero.
1398 * pi the process or LWP to operate on.
1399 * step if true, set the process or LWP to trap after one instr.
1400 * signo if zero, clear the current signal if any.
1401 * if non-zero, set the current signal to this one.
1403 * Returns non-zero for success, zero for failure.
1407 proc_run_process (procinfo
*pi
, int step
, int signo
)
1413 * We will probably have to apply this operation to individual threads,
1414 * so make sure the control file descriptor is open.
1417 if (pi
->ctl_fd
== 0 &&
1418 open_procinfo_files (pi
, FD_CTL
) == 0)
1423 runflags
= PRCFAULT
; /* always clear current fault */
1428 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1429 proc_set_current_signal (pi
, signo
);
1437 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1439 #else /* ioctl method */
1443 memset (&prrun
, 0, sizeof (prrun
));
1444 prrun
.pr_flags
= runflags
;
1445 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1453 * Function: proc_set_traced_signals
1455 * Register to trace signals in the process or LWP.
1456 * Returns non-zero for success, zero for failure.
1460 proc_set_traced_signals (procinfo
*pi
, sigset_t
*sigset
)
1465 * We should never have to apply this operation to any procinfo
1466 * except the one for the main process. If that ever changes
1467 * for any reason, then take out the following clause and
1468 * replace it with one that makes sure the ctl_fd is open.
1472 pi
= find_procinfo_or_die (pi
->pid
, 0);
1478 /* Use char array to avoid alignment issues. */
1479 char sigset
[sizeof (sigset_t
)];
1483 memcpy (&arg
.sigset
, sigset
, sizeof (sigset_t
));
1485 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1487 #else /* ioctl method */
1488 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1490 /* The above operation renders the procinfo's cached pstatus obsolete. */
1491 pi
->status_valid
= 0;
1494 warning ("procfs: set_traced_signals failed");
1499 * Function: proc_set_traced_faults
1501 * Register to trace hardware faults in the process or LWP.
1502 * Returns non-zero for success, zero for failure.
1506 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1511 * We should never have to apply this operation to any procinfo
1512 * except the one for the main process. If that ever changes
1513 * for any reason, then take out the following clause and
1514 * replace it with one that makes sure the ctl_fd is open.
1518 pi
= find_procinfo_or_die (pi
->pid
, 0);
1524 /* Use char array to avoid alignment issues. */
1525 char fltset
[sizeof (fltset_t
)];
1529 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1531 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1533 #else /* ioctl method */
1534 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1536 /* The above operation renders the procinfo's cached pstatus obsolete. */
1537 pi
->status_valid
= 0;
1543 * Function: proc_set_traced_sysentry
1545 * Register to trace entry to system calls in the process or LWP.
1546 * Returns non-zero for success, zero for failure.
1550 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1555 * We should never have to apply this operation to any procinfo
1556 * except the one for the main process. If that ever changes
1557 * for any reason, then take out the following clause and
1558 * replace it with one that makes sure the ctl_fd is open.
1562 pi
= find_procinfo_or_die (pi
->pid
, 0);
1568 /* Use char array to avoid alignment issues. */
1569 char sysset
[sizeof (sysset_t
)];
1573 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1575 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1577 #else /* ioctl method */
1578 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1580 /* The above operation renders the procinfo's cached pstatus obsolete. */
1581 pi
->status_valid
= 0;
1587 * Function: proc_set_traced_sysexit
1589 * Register to trace exit from system calls in the process or LWP.
1590 * Returns non-zero for success, zero for failure.
1594 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1599 * We should never have to apply this operation to any procinfo
1600 * except the one for the main process. If that ever changes
1601 * for any reason, then take out the following clause and
1602 * replace it with one that makes sure the ctl_fd is open.
1606 pi
= find_procinfo_or_die (pi
->pid
, 0);
1612 /* Use char array to avoid alignment issues. */
1613 char sysset
[sizeof (sysset_t
)];
1617 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1619 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1621 #else /* ioctl method */
1622 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1624 /* The above operation renders the procinfo's cached pstatus obsolete. */
1625 pi
->status_valid
= 0;
1631 * Function: proc_set_held_signals
1633 * Specify the set of blocked / held signals in the process or LWP.
1634 * Returns non-zero for success, zero for failure.
1638 proc_set_held_signals (procinfo
*pi
, sigset_t
*sighold
)
1643 * We should never have to apply this operation to any procinfo
1644 * except the one for the main process. If that ever changes
1645 * for any reason, then take out the following clause and
1646 * replace it with one that makes sure the ctl_fd is open.
1650 pi
= find_procinfo_or_die (pi
->pid
, 0);
1656 /* Use char array to avoid alignment issues. */
1657 char hold
[sizeof (sigset_t
)];
1661 memcpy (&arg
.hold
, sighold
, sizeof (sigset_t
));
1662 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1665 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1667 /* The above operation renders the procinfo's cached pstatus obsolete. */
1668 pi
->status_valid
= 0;
1674 * Function: proc_get_pending_signals
1676 * returns the set of signals that are pending in the process or LWP.
1677 * Will also copy the sigset if 'save' is non-zero.
1681 proc_get_pending_signals (procinfo
*pi
, sigset_t
*save
)
1683 sigset_t
*ret
= NULL
;
1686 * We should never have to apply this operation to any procinfo
1687 * except the one for the main process. If that ever changes
1688 * for any reason, then take out the following clause and
1689 * replace it with one that makes sure the ctl_fd is open.
1693 pi
= find_procinfo_or_die (pi
->pid
, 0);
1695 if (!pi
->status_valid
)
1696 if (!proc_get_status (pi
))
1700 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
1702 ret
= &pi
->prstatus
.pr_sigpend
;
1705 memcpy (save
, ret
, sizeof (sigset_t
));
1711 * Function: proc_get_signal_actions
1713 * returns the set of signal actions.
1714 * Will also copy the sigactionset if 'save' is non-zero.
1718 proc_get_signal_actions (procinfo
*pi
, struct sigaction
*save
)
1720 struct sigaction
*ret
= NULL
;
1723 * We should never have to apply this operation to any procinfo
1724 * except the one for the main process. If that ever changes
1725 * for any reason, then take out the following clause and
1726 * replace it with one that makes sure the ctl_fd is open.
1730 pi
= find_procinfo_or_die (pi
->pid
, 0);
1732 if (!pi
->status_valid
)
1733 if (!proc_get_status (pi
))
1737 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
1739 ret
= &pi
->prstatus
.pr_action
;
1742 memcpy (save
, ret
, sizeof (struct sigaction
));
1748 * Function: proc_get_held_signals
1750 * returns the set of signals that are held / blocked.
1751 * Will also copy the sigset if 'save' is non-zero.
1755 proc_get_held_signals (procinfo
*pi
, sigset_t
*save
)
1757 sigset_t
*ret
= NULL
;
1760 * We should never have to apply this operation to any procinfo
1761 * except the one for the main process. If that ever changes
1762 * for any reason, then take out the following clause and
1763 * replace it with one that makes sure the ctl_fd is open.
1767 pi
= find_procinfo_or_die (pi
->pid
, 0);
1770 if (!pi
->status_valid
)
1771 if (!proc_get_status (pi
))
1775 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
1777 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1778 #endif /* UNIXWARE */
1779 #else /* not NEW_PROC_API */
1781 static sigset_t sigheld
;
1783 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
1786 #endif /* NEW_PROC_API */
1788 memcpy (save
, ret
, sizeof (sigset_t
));
1794 * Function: proc_get_traced_signals
1796 * returns the set of signals that are traced / debugged.
1797 * Will also copy the sigset if 'save' is non-zero.
1801 proc_get_traced_signals (procinfo
*pi
, sigset_t
*save
)
1803 sigset_t
*ret
= NULL
;
1806 * We should never have to apply this operation to any procinfo
1807 * except the one for the main process. If that ever changes
1808 * for any reason, then take out the following clause and
1809 * replace it with one that makes sure the ctl_fd is open.
1813 pi
= find_procinfo_or_die (pi
->pid
, 0);
1816 if (!pi
->status_valid
)
1817 if (!proc_get_status (pi
))
1820 ret
= &pi
->prstatus
.pr_sigtrace
;
1823 static sigset_t sigtrace
;
1825 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
1830 memcpy (save
, ret
, sizeof (sigset_t
));
1836 * Function: proc_trace_signal
1838 * Add 'signo' to the set of signals that are traced.
1839 * Returns non-zero for success, zero for failure.
1843 proc_trace_signal (procinfo
*pi
, int signo
)
1848 * We should never have to apply this operation to any procinfo
1849 * except the one for the main process. If that ever changes
1850 * for any reason, then take out the following clause and
1851 * replace it with one that makes sure the ctl_fd is open.
1855 pi
= find_procinfo_or_die (pi
->pid
, 0);
1859 if (proc_get_traced_signals (pi
, &temp
))
1861 praddset (&temp
, signo
);
1862 return proc_set_traced_signals (pi
, &temp
);
1866 return 0; /* failure */
1870 * Function: proc_ignore_signal
1872 * Remove 'signo' from the set of signals that are traced.
1873 * Returns non-zero for success, zero for failure.
1877 proc_ignore_signal (procinfo
*pi
, int signo
)
1882 * We should never have to apply this operation to any procinfo
1883 * except the one for the main process. If that ever changes
1884 * for any reason, then take out the following clause and
1885 * replace it with one that makes sure the ctl_fd is open.
1889 pi
= find_procinfo_or_die (pi
->pid
, 0);
1893 if (proc_get_traced_signals (pi
, &temp
))
1895 prdelset (&temp
, signo
);
1896 return proc_set_traced_signals (pi
, &temp
);
1900 return 0; /* failure */
1904 * Function: proc_get_traced_faults
1906 * returns the set of hardware faults that are traced /debugged.
1907 * Will also copy the faultset if 'save' is non-zero.
1911 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1913 fltset_t
*ret
= NULL
;
1916 * We should never have to apply this operation to any procinfo
1917 * except the one for the main process. If that ever changes
1918 * for any reason, then take out the following clause and
1919 * replace it with one that makes sure the ctl_fd is open.
1923 pi
= find_procinfo_or_die (pi
->pid
, 0);
1926 if (!pi
->status_valid
)
1927 if (!proc_get_status (pi
))
1930 ret
= &pi
->prstatus
.pr_flttrace
;
1933 static fltset_t flttrace
;
1935 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
1940 memcpy (save
, ret
, sizeof (fltset_t
));
1946 * Function: proc_get_traced_sysentry
1948 * returns the set of syscalls that are traced /debugged on entry.
1949 * Will also copy the syscall set if 'save' is non-zero.
1953 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1955 sysset_t
*ret
= NULL
;
1958 * We should never have to apply this operation to any procinfo
1959 * except the one for the main process. If that ever changes
1960 * for any reason, then take out the following clause and
1961 * replace it with one that makes sure the ctl_fd is open.
1965 pi
= find_procinfo_or_die (pi
->pid
, 0);
1968 if (!pi
->status_valid
)
1969 if (!proc_get_status (pi
))
1972 ret
= &pi
->prstatus
.pr_sysentry
;
1975 static sysset_t sysentry
;
1977 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
1982 memcpy (save
, ret
, sizeof (sysset_t
));
1988 * Function: proc_get_traced_sysexit
1990 * returns the set of syscalls that are traced /debugged on exit.
1991 * Will also copy the syscall set if 'save' is non-zero.
1995 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1997 sysset_t
* ret
= NULL
;
2000 * We should never have to apply this operation to any procinfo
2001 * except the one for the main process. If that ever changes
2002 * for any reason, then take out the following clause and
2003 * replace it with one that makes sure the ctl_fd is open.
2007 pi
= find_procinfo_or_die (pi
->pid
, 0);
2010 if (!pi
->status_valid
)
2011 if (!proc_get_status (pi
))
2014 ret
= &pi
->prstatus
.pr_sysexit
;
2017 static sysset_t sysexit
;
2019 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2024 memcpy (save
, ret
, sizeof (sysset_t
));
2030 * Function: proc_clear_current_fault
2032 * The current fault (if any) is cleared; the associated signal
2033 * will not be sent to the process or LWP when it resumes.
2034 * Returns non-zero for success, zero for failure.
2038 proc_clear_current_fault (procinfo
*pi
)
2043 * We should never have to apply this operation to any procinfo
2044 * except the one for the main process. If that ever changes
2045 * for any reason, then take out the following clause and
2046 * replace it with one that makes sure the ctl_fd is open.
2050 pi
= find_procinfo_or_die (pi
->pid
, 0);
2054 long cmd
= PCCFAULT
;
2055 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2058 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2065 * Function: proc_set_current_signal
2067 * Set the "current signal" that will be delivered next to the process.
2068 * NOTE: semantics are different from those of KILL.
2069 * This signal will be delivered to the process or LWP
2070 * immediately when it is resumed (even if the signal is held/blocked);
2071 * it will NOT immediately cause another event of interest, and will NOT
2072 * first trap back to the debugger.
2074 * Returns non-zero for success, zero for failure.
2078 proc_set_current_signal (procinfo
*pi
, int signo
)
2083 /* Use char array to avoid alignment issues. */
2084 char sinfo
[sizeof (struct siginfo
)];
2086 struct siginfo
*mysinfo
;
2089 * We should never have to apply this operation to any procinfo
2090 * except the one for the main process. If that ever changes
2091 * for any reason, then take out the following clause and
2092 * replace it with one that makes sure the ctl_fd is open.
2096 pi
= find_procinfo_or_die (pi
->pid
, 0);
2098 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2099 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2100 * receives a PIOCSSIG with a signal identical to the current signal,
2101 * it messes up the current signal. Work around the kernel bug.
2104 signo
== proc_cursig (pi
))
2105 return 1; /* I assume this is a success? */
2108 /* The pointer is just a type alias. */
2109 mysinfo
= (struct siginfo
*) &arg
.sinfo
;
2110 mysinfo
->si_signo
= signo
;
2111 mysinfo
->si_code
= 0;
2112 mysinfo
->si_pid
= getpid (); /* ?why? */
2113 mysinfo
->si_uid
= getuid (); /* ?why? */
2117 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2119 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2126 * Function: proc_clear_current_signal
2128 * The current signal (if any) is cleared, and
2129 * is not sent to the process or LWP when it resumes.
2130 * Returns non-zero for success, zero for failure.
2134 proc_clear_current_signal (procinfo
*pi
)
2139 * We should never have to apply this operation to any procinfo
2140 * except the one for the main process. If that ever changes
2141 * for any reason, then take out the following clause and
2142 * replace it with one that makes sure the ctl_fd is open.
2146 pi
= find_procinfo_or_die (pi
->pid
, 0);
2152 /* Use char array to avoid alignment issues. */
2153 char sinfo
[sizeof (struct siginfo
)];
2155 struct siginfo
*mysinfo
;
2158 /* The pointer is just a type alias. */
2159 mysinfo
= (struct siginfo
*) &arg
.sinfo
;
2160 mysinfo
->si_signo
= 0;
2161 mysinfo
->si_code
= 0;
2162 mysinfo
->si_errno
= 0;
2163 mysinfo
->si_pid
= getpid (); /* ?why? */
2164 mysinfo
->si_uid
= getuid (); /* ?why? */
2166 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2169 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2176 * Function: proc_get_gregs
2178 * Get the general registers for the process or LWP.
2179 * Returns non-zero for success, zero for failure.
2183 proc_get_gregs (procinfo
*pi
)
2185 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2186 if (!proc_get_status (pi
))
2190 * OK, sorry about the ifdef's.
2191 * There's three cases instead of two, because
2192 * in this instance Unixware and Solaris/RW differ.
2196 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2197 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2198 #else /* not Unixware */
2199 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2200 #endif /* Unixware */
2201 #else /* not NEW_PROC_API */
2202 return &pi
->prstatus
.pr_reg
;
2203 #endif /* NEW_PROC_API */
2207 * Function: proc_get_fpregs
2209 * Get the floating point registers for the process or LWP.
2210 * Returns non-zero for success, zero for failure.
2214 proc_get_fpregs (procinfo
*pi
)
2217 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2218 if (!proc_get_status (pi
))
2221 #ifdef UNIXWARE /* a true architecture dependency */
2222 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2224 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2225 #endif /* Unixware */
2227 #else /* not NEW_PROC_API */
2228 if (pi
->fpregs_valid
)
2229 return &pi
->fpregset
; /* already got 'em */
2232 if (pi
->ctl_fd
== 0 &&
2233 open_procinfo_files (pi
, FD_CTL
) == 0)
2242 tid_t pr_error_thread
;
2243 tfpregset_t thread_1
;
2246 thread_fpregs
.pr_count
= 1;
2247 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2250 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2252 pi
->fpregs_valid
= 1;
2253 return &pi
->fpregset
; /* got 'em now! */
2255 else if (pi
->tid
!= 0 &&
2256 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2258 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2259 sizeof (pi
->fpregset
));
2260 pi
->fpregs_valid
= 1;
2261 return &pi
->fpregset
; /* got 'em now! */
2268 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2270 pi
->fpregs_valid
= 1;
2271 return &pi
->fpregset
; /* got 'em now! */
2284 * Function: proc_set_gregs
2286 * Write the general registers back to the process or LWP.
2287 * Returns non-zero for success, zero for failure.
2291 proc_set_gregs (procinfo
*pi
)
2293 gdb_gregset_t
*gregs
;
2296 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2297 return 0; /* get_regs has already warned */
2299 if (pi
->ctl_fd
== 0 &&
2300 open_procinfo_files (pi
, FD_CTL
) == 0)
2309 /* Use char array to avoid alignment issues. */
2310 char gregs
[sizeof (gdb_gregset_t
)];
2314 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2315 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2317 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2321 /* Policy: writing the regs invalidates our cache. */
2322 pi
->gregs_valid
= 0;
2327 * Function: proc_set_fpregs
2329 * Modify the floating point register set of the process or LWP.
2330 * Returns non-zero for success, zero for failure.
2334 proc_set_fpregs (procinfo
*pi
)
2336 gdb_fpregset_t
*fpregs
;
2339 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2340 return 0; /* get_fpregs has already warned */
2342 if (pi
->ctl_fd
== 0 &&
2343 open_procinfo_files (pi
, FD_CTL
) == 0)
2352 /* Use char array to avoid alignment issues. */
2353 char fpregs
[sizeof (gdb_fpregset_t
)];
2357 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2358 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2362 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2367 tid_t pr_error_thread
;
2368 tfpregset_t thread_1
;
2371 thread_fpregs
.pr_count
= 1;
2372 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2373 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2375 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2378 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2379 #endif /* osf PIOCTSFPREG */
2380 #endif /* NEW_PROC_API */
2383 /* Policy: writing the regs invalidates our cache. */
2384 pi
->fpregs_valid
= 0;
2389 * Function: proc_kill
2391 * Send a signal to the proc or lwp with the semantics of "kill()".
2392 * Returns non-zero for success, zero for failure.
2396 proc_kill (procinfo
*pi
, int signo
)
2401 * We might conceivably apply this operation to an LWP, and
2402 * the LWP's ctl file descriptor might not be open.
2405 if (pi
->ctl_fd
== 0 &&
2406 open_procinfo_files (pi
, FD_CTL
) == 0)
2417 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2418 #else /* ioctl method */
2419 /* FIXME: do I need the Alpha OSF fixups present in
2420 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2421 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2429 * Function: proc_parent_pid
2431 * Find the pid of the process that started this one.
2432 * Returns the parent process pid, or zero.
2436 proc_parent_pid (procinfo
*pi
)
2439 * We should never have to apply this operation to any procinfo
2440 * except the one for the main process. If that ever changes
2441 * for any reason, then take out the following clause and
2442 * replace it with one that makes sure the ctl_fd is open.
2446 pi
= find_procinfo_or_die (pi
->pid
, 0);
2448 if (!pi
->status_valid
)
2449 if (!proc_get_status (pi
))
2452 return pi
->prstatus
.pr_ppid
;
2457 * Function: proc_set_watchpoint
2462 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2464 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2467 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2468 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2473 char watch
[sizeof (prwatch_t
)];
2477 pwatch
= (prwatch_t
*) &arg
.watch
;
2478 pwatch
->pr_vaddr
= address_to_host_pointer (addr
);
2479 pwatch
->pr_size
= len
;
2480 pwatch
->pr_wflags
= wflags
;
2481 #if defined(NEW_PROC_API) && defined (PCWATCH)
2483 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2485 #if defined (PIOCSWATCH)
2486 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2488 return 0; /* Fail */
2496 * Function: proc_iterate_over_mappings
2498 * Given a pointer to a function, call that function once for every
2499 * mapped address space in the process. The callback function
2500 * receives an open file descriptor for the file corresponding to
2501 * that mapped address space (if there is one), and the base address
2502 * of the mapped space. Quit when the callback function returns a
2503 * nonzero value, or at teh end of the mappings.
2505 * Returns: the first non-zero return value of the callback function,
2509 /* FIXME: it's probably a waste to cache this FD.
2510 It doesn't get called that often... and if I open it
2511 every time, I don't need to lseek it. */
2513 proc_iterate_over_mappings (int (*func
) (int, CORE_ADDR
))
2517 #ifndef NEW_PROC_API /* avoid compiler warning */
2522 char pathname
[MAX_PROC_NAME_SIZE
];
2527 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
2531 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
2532 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
2533 proc_error (pi
, "proc_iterate_over_mappings (open)", __LINE__
);
2535 /* Make sure it gets closed again. */
2536 make_cleanup_close (map_fd
);
2538 /* Allocate space for mapping (lifetime only for this function). */
2539 map
= alloca (sizeof (struct prmap
));
2541 /* Now read the mappings from the file,
2542 open a file descriptor for those that have a name,
2543 and call the callback function. */
2544 while (read (map_fd
,
2546 sizeof (struct prmap
)) == sizeof (struct prmap
))
2548 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
2550 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
2553 if (map
->pr_mapname
[0] == 0)
2555 fd
= -1; /* no map file */
2559 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
2560 /* Note: caller's responsibility to close this fd! */
2561 fd
= open (name
, O_RDONLY
);
2562 /* Note: we don't test the above call for failure;
2563 we just pass the FD on as given. Sometimes there is
2564 no file, so the ioctl may return failure, but that's
2568 /* Stop looping if the callback returns non-zero. */
2569 if ((funcstat
= (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
)) != 0)
2573 /* Get the number of mapping entries. */
2574 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmaps
) < 0)
2575 proc_error (pi
, "proc_iterate_over_mappings (PIOCNMAP)", __LINE__
);
2577 /* Allocate space for mappings (lifetime only this function). */
2578 map
= (struct prmap
*) alloca ((nmaps
+ 1) * sizeof (struct prmap
));
2580 /* Read in all the mappings. */
2581 if (ioctl (pi
->ctl_fd
, PIOCMAP
, map
) < 0)
2582 proc_error (pi
, "proc_iterate_over_mappings (PIOCMAP)", __LINE__
);
2584 /* Now loop through the mappings, open an fd for each, and
2585 call the callback function. */
2587 i
< nmaps
&& map
[i
].pr_size
!= 0;
2590 /* Note: caller's responsibility to close this fd! */
2591 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
[i
].pr_vaddr
);
2592 /* Note: we don't test the above call for failure;
2593 we just pass the FD on as given. Sometimes there is
2594 no file, so the ioctl may return failure, but that's
2597 /* Stop looping if the callback returns non-zero. */
2598 funcstat
= (*func
) (fd
, host_pointer_to_address (map
[i
].pr_vaddr
));
2607 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2609 #include <sys/sysi86.h>
2612 * Function: proc_get_LDT_entry
2618 * The 'key' is actually the value of the lower 16 bits of
2619 * the GS register for the LWP that we're interested in.
2621 * Return: matching ssh struct (LDT entry).
2625 proc_get_LDT_entry (procinfo
*pi
, int key
)
2627 static struct ssd
*ldt_entry
= NULL
;
2629 char pathname
[MAX_PROC_NAME_SIZE
];
2630 struct cleanup
*old_chain
= NULL
;
2633 /* Allocate space for one LDT entry.
2634 This alloc must persist, because we return a pointer to it. */
2635 if (ldt_entry
== NULL
)
2636 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2638 /* Open the file descriptor for the LDT table. */
2639 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2640 if ((fd
= open (pathname
, O_RDONLY
)) < 0)
2642 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2645 /* Make sure it gets closed again! */
2646 old_chain
= make_cleanup_close (fd
);
2648 /* Now 'read' thru the table, find a match and return it. */
2649 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2651 if (ldt_entry
->sel
== 0 &&
2652 ldt_entry
->bo
== 0 &&
2653 ldt_entry
->acc1
== 0 &&
2654 ldt_entry
->acc2
== 0)
2655 break; /* end of table */
2656 /* If key matches, return this entry. */
2657 if (ldt_entry
->sel
== key
)
2660 /* Loop ended, match not found. */
2664 static int nalloc
= 0;
2666 /* Get the number of LDT entries. */
2667 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2669 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2673 /* Allocate space for the number of LDT entries. */
2674 /* This alloc has to persist, 'cause we return a pointer to it. */
2677 ldt_entry
= (struct ssd
*)
2678 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2682 /* Read the whole table in one gulp. */
2683 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2685 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2689 /* Search the table and return the (first) entry matching 'key'. */
2690 for (i
= 0; i
< nldt
; i
++)
2691 if (ldt_entry
[i
].sel
== key
)
2692 return &ldt_entry
[i
];
2694 /* Loop ended, match not found. */
2699 #endif /* TM_I386SOL2_H */
2701 /* =============== END, non-thread part of /proc "MODULE" =============== */
2703 /* =================== Thread "MODULE" =================== */
2705 /* NOTE: you'll see more ifdefs and duplication of functions here,
2706 since there is a different way to do threads on every OS. */
2709 * Function: proc_get_nthreads
2711 * Return the number of threads for the process
2714 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2719 proc_get_nthreads (procinfo
*pi
)
2723 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
2724 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
2730 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2732 * Solaris and Unixware version
2735 proc_get_nthreads (procinfo
*pi
)
2737 if (!pi
->status_valid
)
2738 if (!proc_get_status (pi
))
2742 * NEW_PROC_API: only works for the process procinfo,
2743 * because the LWP procinfos do not get prstatus filled in.
2746 if (pi
->tid
!= 0) /* find the parent process procinfo */
2747 pi
= find_procinfo_or_die (pi
->pid
, 0);
2749 return pi
->prstatus
.pr_nlwp
;
2757 proc_get_nthreads (procinfo
*pi
)
2765 * Function: proc_get_current_thread (LWP version)
2767 * Return the ID of the thread that had an event of interest.
2768 * (ie. the one that hit a breakpoint or other traced event).
2769 * All other things being equal, this should be the ID of a
2770 * thread that is currently executing.
2773 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2775 * Solaris and Unixware version
2778 proc_get_current_thread (procinfo
*pi
)
2781 * Note: this should be applied to the root procinfo for the process,
2782 * not to the procinfo for an LWP. If applied to the procinfo for
2783 * an LWP, it will simply return that LWP's ID. In that case,
2784 * find the parent process procinfo.
2788 pi
= find_procinfo_or_die (pi
->pid
, 0);
2790 if (!pi
->status_valid
)
2791 if (!proc_get_status (pi
))
2795 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
2797 return pi
->prstatus
.pr_who
;
2802 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2807 proc_get_current_thread (procinfo
*pi
)
2809 #if 0 /* FIXME: not ready for prime time? */
2810 return pi
->prstatus
.pr_tid
;
2821 proc_get_current_thread (procinfo
*pi
)
2830 * Function: proc_update_threads
2832 * Discover the IDs of all the threads within the process, and
2833 * create a procinfo for each of them (chained to the parent).
2835 * This unfortunately requires a different method on every OS.
2837 * Return: non-zero for success, zero for failure.
2841 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
2843 if (thread
&& parent
) /* sanity */
2845 thread
->status_valid
= 0;
2846 if (!proc_get_status (thread
))
2847 destroy_one_procinfo (&parent
->thread_list
, thread
);
2849 return 0; /* keep iterating */
2852 #if defined (PIOCLSTATUS)
2854 * Solaris 2.5 (ioctl) version
2857 proc_update_threads (procinfo
*pi
)
2859 gdb_prstatus_t
*prstatus
;
2860 struct cleanup
*old_chain
= NULL
;
2865 * We should never have to apply this operation to any procinfo
2866 * except the one for the main process. If that ever changes
2867 * for any reason, then take out the following clause and
2868 * replace it with one that makes sure the ctl_fd is open.
2872 pi
= find_procinfo_or_die (pi
->pid
, 0);
2874 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2876 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
2877 return 1; /* Process is not multi-threaded; nothing to do. */
2879 if ((prstatus
= (gdb_prstatus_t
*)
2880 malloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1))) == 0)
2881 perror_with_name ("procfs: malloc failed in update_threads");
2883 old_chain
= make_cleanup (xfree
, prstatus
);
2884 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
2885 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
2887 /* Skip element zero, which represents the process as a whole. */
2888 for (i
= 1; i
< nlwp
+ 1; i
++)
2890 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
2891 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2893 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
2894 thread
->status_valid
= 1;
2896 pi
->threads_valid
= 1;
2897 do_cleanups (old_chain
);
2903 * Unixware and Solaris 6 (and later) version
2906 do_closedir_cleanup (void *dir
)
2912 proc_update_threads (procinfo
*pi
)
2914 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
2915 struct dirent
*direntry
;
2916 struct cleanup
*old_chain
= NULL
;
2922 * We should never have to apply this operation to any procinfo
2923 * except the one for the main process. If that ever changes
2924 * for any reason, then take out the following clause and
2925 * replace it with one that makes sure the ctl_fd is open.
2929 pi
= find_procinfo_or_die (pi
->pid
, 0);
2931 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2936 * Note: this brute-force method is the only way I know of
2937 * to accomplish this task on Unixware. This method will
2938 * also work on Solaris 2.6 and 2.7. There is a much simpler
2939 * and more elegant way to do this on Solaris, but the margins
2940 * of this manuscript are too small to write it here... ;-)
2943 strcpy (pathname
, pi
->pathname
);
2944 strcat (pathname
, "/lwp");
2945 if ((dirp
= opendir (pathname
)) == NULL
)
2946 proc_error (pi
, "update_threads, opendir", __LINE__
);
2948 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
2949 while ((direntry
= readdir (dirp
)) != NULL
)
2950 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
2952 lwpid
= atoi (&direntry
->d_name
[0]);
2953 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
2954 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2956 pi
->threads_valid
= 1;
2957 do_cleanups (old_chain
);
2966 proc_update_threads (procinfo
*pi
)
2972 * We should never have to apply this operation to any procinfo
2973 * except the one for the main process. If that ever changes
2974 * for any reason, then take out the following clause and
2975 * replace it with one that makes sure the ctl_fd is open.
2979 pi
= find_procinfo_or_die (pi
->pid
, 0);
2981 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2983 nthreads
= proc_get_nthreads (pi
);
2985 return 0; /* nothing to do for 1 or fewer threads */
2987 if ((threads
= malloc (nthreads
* sizeof (tid_t
))) == NULL
)
2988 proc_error (pi
, "update_threads, malloc", __LINE__
);
2990 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
2991 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
2993 for (i
= 0; i
< nthreads
; i
++)
2995 if (!find_procinfo (pi
->pid
, threads
[i
]))
2996 if (!create_procinfo (pi
->pid
, threads
[i
]))
2997 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2999 pi
->threads_valid
= 1;
3007 proc_update_threads (procinfo
*pi
)
3011 #endif /* OSF PIOCTLIST */
3012 #endif /* NEW_PROC_API */
3013 #endif /* SOL 2.5 PIOCLSTATUS */
3016 * Function: proc_iterate_over_threads
3019 * Given a pointer to a function, call that function once
3020 * for each lwp in the procinfo list, until the function
3021 * returns non-zero, in which event return the value
3022 * returned by the function.
3024 * Note: this function does NOT call update_threads.
3025 * If you want to discover new threads first, you must
3026 * call that function explicitly. This function just makes
3027 * a quick pass over the currently-known procinfos.
3030 * pi - parent process procinfo
3031 * func - per-thread function
3032 * ptr - opaque parameter for function.
3035 * First non-zero return value from the callee, or zero.
3039 proc_iterate_over_threads (procinfo
*pi
,
3040 int (*func
) (procinfo
*, procinfo
*, void *),
3043 procinfo
*thread
, *next
;
3047 * We should never have to apply this operation to any procinfo
3048 * except the one for the main process. If that ever changes
3049 * for any reason, then take out the following clause and
3050 * replace it with one that makes sure the ctl_fd is open.
3054 pi
= find_procinfo_or_die (pi
->pid
, 0);
3056 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3058 next
= thread
->next
; /* in case thread is destroyed */
3059 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3066 /* =================== END, Thread "MODULE" =================== */
3068 /* =================== END, /proc "MODULE" =================== */
3070 /* =================== GDB "MODULE" =================== */
3073 * Here are all of the gdb target vector functions and their friends.
3076 static int do_attach (int pid
);
3077 static void do_detach (int signo
);
3078 static int register_gdb_signals (procinfo
*, sigset_t
*);
3081 * Function: procfs_debug_inferior
3083 * Sets up the inferior to be debugged.
3084 * Registers to trace signals, hardware faults, and syscalls.
3085 * Note: does not set RLC flag: caller may want to customize that.
3087 * Returns: zero for success (note! unlike most functions in this module)
3088 * On failure, returns the LINE NUMBER where it failed!
3092 procfs_debug_inferior (procinfo
*pi
)
3094 fltset_t traced_faults
;
3095 sigset_t traced_signals
;
3096 sysset_t traced_syscall_entries
;
3097 sysset_t traced_syscall_exits
;
3099 #ifdef PROCFS_DONT_TRACE_FAULTS
3100 /* On some systems (OSF), we don't trace hardware faults.
3101 Apparently it's enough that we catch them as signals.
3102 Wonder why we don't just do that in general? */
3103 premptyset (&traced_faults
); /* don't trace faults. */
3105 /* Register to trace hardware faults in the child. */
3106 prfillset (&traced_faults
); /* trace all faults... */
3107 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3109 if (!proc_set_traced_faults (pi
, &traced_faults
))
3112 /* Register to trace selected signals in the child. */
3113 premptyset (&traced_signals
);
3114 if (!register_gdb_signals (pi
, &traced_signals
))
3117 /* Register to trace the 'exit' system call (on entry). */
3118 premptyset (&traced_syscall_entries
);
3119 praddset (&traced_syscall_entries
, SYS_exit
);
3121 praddset (&traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3124 praddset (&traced_syscall_entries
, SYS_lwp_exit
);
3127 if (!proc_set_traced_sysentry (pi
, &traced_syscall_entries
))
3130 #ifdef PRFS_STOPEXEC /* defined on OSF */
3131 /* OSF method for tracing exec syscalls. Quoting:
3132 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3133 exits from exec system calls because of the user level loader. */
3134 /* FIXME: make nice and maybe move into an access function. */
3138 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3141 prfs_flags
|= PRFS_STOPEXEC
;
3143 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3146 #else /* not PRFS_STOPEXEC */
3147 /* Everyone else's (except OSF) method for tracing exec syscalls */
3149 Not all systems with /proc have all the exec* syscalls with the same
3150 names. On the SGI, for example, there is no SYS_exec, but there
3151 *is* a SYS_execv. So, we try to account for that. */
3153 premptyset (&traced_syscall_exits
);
3155 praddset (&traced_syscall_exits
, SYS_exec
);
3158 praddset (&traced_syscall_exits
, SYS_execve
);
3161 praddset (&traced_syscall_exits
, SYS_execv
);
3164 #ifdef SYS_lwpcreate
3165 praddset (&traced_syscall_exits
, SYS_lwpcreate
);
3166 praddset (&traced_syscall_exits
, SYS_lwpexit
);
3169 #ifdef SYS_lwp_create /* FIXME: once only, please */
3170 praddset (&traced_syscall_exits
, SYS_lwp_create
);
3171 praddset (&traced_syscall_exits
, SYS_lwp_exit
);
3175 if (!proc_set_traced_sysexit (pi
, &traced_syscall_exits
))
3178 #endif /* PRFS_STOPEXEC */
3183 procfs_attach (char *args
, int from_tty
)
3189 error_no_arg ("process-id to attach");
3192 if (pid
== getpid ())
3193 error ("Attaching GDB to itself is not a good idea...");
3197 exec_file
= get_exec_file (0);
3200 printf_filtered ("Attaching to program `%s', %s\n",
3201 exec_file
, target_pid_to_str (pid
));
3203 printf_filtered ("Attaching to %s\n", target_pid_to_str (pid
));
3207 inferior_pid
= do_attach (pid
);
3208 push_target (&procfs_ops
);
3212 procfs_detach (char *args
, int from_tty
)
3219 exec_file
= get_exec_file (0);
3222 printf_filtered ("Detaching from program: %s %s\n",
3223 exec_file
, target_pid_to_str (inferior_pid
));
3227 signo
= atoi (args
);
3231 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3240 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
3241 perror ("procfs: out of memory in 'attach'");
3243 if (!open_procinfo_files (pi
, FD_CTL
))
3245 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3246 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3248 dead_procinfo (pi
, errmsg
, NOKILL
);
3251 /* Stop the process (if it isn't already stopped). */
3252 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3254 pi
->was_stopped
= 1;
3255 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3259 pi
->was_stopped
= 0;
3260 /* Set the process to run again when we close it. */
3261 if (!proc_set_run_on_last_close (pi
))
3262 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3264 /* Now stop the process. */
3265 if (!proc_stop_process (pi
))
3266 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3267 pi
->ignore_next_sigstop
= 1;
3269 /* Save some of the /proc state to be restored if we detach. */
3270 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3271 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3272 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3273 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3274 if (!proc_get_traced_sysentry (pi
, &pi
->saved_entryset
))
3275 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3277 if (!proc_get_traced_sysexit (pi
, &pi
->saved_exitset
))
3278 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3280 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3281 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3283 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3284 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3286 /* Let GDB know that the inferior was attached. */
3288 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3292 do_detach (int signo
)
3296 /* Find procinfo for the main process */
3297 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0); /* FIXME: threads */
3299 if (!proc_set_current_signal (pi
, signo
))
3300 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3302 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3303 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3305 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3306 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3308 if (!proc_set_traced_sysentry (pi
, &pi
->saved_entryset
))
3309 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3311 if (!proc_set_traced_sysexit (pi
, &pi
->saved_exitset
))
3312 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3314 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3315 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3317 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3318 if (signo
|| !(pi
->was_stopped
) ||
3319 query ("Was stopped when attached, make it runnable again? "))
3321 /* Clear any pending signal. */
3322 if (!proc_clear_current_fault (pi
))
3323 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3325 if (!proc_set_run_on_last_close (pi
))
3326 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3330 destroy_procinfo (pi
);
3336 * Since the /proc interface cannot give us individual registers,
3337 * we pay no attention to the (regno) argument, and just fetch them all.
3338 * This results in the possibility that we will do unnecessarily many
3339 * fetches, since we may be called repeatedly for individual registers.
3340 * So we cache the results, and mark the cache invalid when the process
3345 procfs_fetch_registers (int regno
)
3347 gdb_fpregset_t
*fpregs
;
3348 gdb_gregset_t
*gregs
;
3353 pid
= PIDGET (inferior_pid
);
3354 tid
= TIDGET (inferior_pid
);
3356 /* First look up procinfo for the main process. */
3357 pi
= find_procinfo_or_die (pid
, 0);
3359 /* If the event thread is not the same as GDB's requested thread
3360 (ie. inferior_pid), then look up procinfo for the requested
3363 (tid
!= proc_get_current_thread (pi
)))
3364 pi
= find_procinfo_or_die (pid
, tid
);
3367 error ("procfs: fetch_registers failed to find procinfo for %s",
3368 target_pid_to_str (inferior_pid
));
3370 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3371 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3373 supply_gregset (gregs
);
3375 if (FP0_REGNUM
>= 0) /* need floating point? */
3377 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3378 regno
== PC_REGNUM
||
3379 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3380 regno
== FP_REGNUM
||
3382 return; /* not a floating point register */
3384 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3385 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3387 supply_fpregset (fpregs
);
3391 /* Get ready to modify the registers array. On machines which store
3392 individual registers, this doesn't need to do anything. On
3393 machines which store all the registers in one fell swoop, such as
3394 /proc, this makes sure that registers contains all the registers
3395 from the program being debugged. */
3398 procfs_prepare_to_store (void)
3400 #ifdef CHILD_PREPARE_TO_STORE
3401 CHILD_PREPARE_TO_STORE ();
3408 * Since the /proc interface will not read individual registers,
3409 * we will cache these requests until the process is resumed, and
3410 * only then write them back to the inferior process.
3412 * FIXME: is that a really bad idea? Have to think about cases
3413 * where writing one register might affect the value of others, etc.
3417 procfs_store_registers (int regno
)
3419 gdb_fpregset_t
*fpregs
;
3420 gdb_gregset_t
*gregs
;
3425 pid
= PIDGET (inferior_pid
);
3426 tid
= TIDGET (inferior_pid
);
3428 /* First find procinfo for main process */
3429 pi
= find_procinfo_or_die (pid
, 0);
3431 /* If current lwp for process is not the same as requested thread
3432 (ie. inferior_pid), then find procinfo for the requested thread. */
3435 (tid
!= proc_get_current_thread (pi
)))
3436 pi
= find_procinfo_or_die (pid
, tid
);
3439 error ("procfs: store_registers: failed to find procinfo for %s",
3440 target_pid_to_str (inferior_pid
));
3442 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3443 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3445 fill_gregset (gregs
, regno
);
3446 if (!proc_set_gregs (pi
))
3447 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3449 if (FP0_REGNUM
>= 0) /* need floating point? */
3451 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3452 regno
== PC_REGNUM
||
3453 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3454 regno
== FP_REGNUM
||
3456 return; /* not a floating point register */
3458 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3459 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3461 fill_fpregset (fpregs
, regno
);
3462 if (!proc_set_fpregs (pi
))
3463 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3468 * Function: target_wait
3470 * Retrieve the next stop event from the child process.
3471 * If child has not stopped yet, wait for it to stop.
3472 * Translate /proc eventcodes (or possibly wait eventcodes)
3473 * into gdb internal event codes.
3475 * Return: id of process (and possibly thread) that incurred the event.
3476 * event codes are returned thru a pointer parameter.
3480 procfs_wait (int pid
, struct target_waitstatus
*status
)
3482 /* First cut: loosely based on original version 2.1 */
3486 int why
, what
, flags
;
3495 /* Find procinfo for main process */
3496 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
3499 /* We must assume that the status is stale now... */
3500 pi
->status_valid
= 0;
3501 pi
->gregs_valid
= 0;
3502 pi
->fpregs_valid
= 0;
3504 #if 0 /* just try this out... */
3505 flags
= proc_flags (pi
);
3506 why
= proc_why (pi
);
3507 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3508 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3510 /* If child is not stopped, wait for it to stop. */
3511 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3512 !proc_wait_for_stop (pi
))
3514 /* wait_for_stop failed: has the child terminated? */
3515 if (errno
== ENOENT
)
3517 /* /proc file not found; presumably child has terminated. */
3518 retval
= wait (&wstat
); /* "wait" for the child's exit */
3520 if (retval
!= PIDGET (inferior_pid
)) /* wrong child? */
3521 error ("procfs: couldn't stop process %d: wait returned %d\n",
3522 inferior_pid
, retval
);
3523 /* FIXME: might I not just use waitpid?
3524 Or try find_procinfo to see if I know about this child? */
3528 /* Unknown error from wait_for_stop. */
3529 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3534 /* This long block is reached if either:
3535 a) the child was already stopped, or
3536 b) we successfully waited for the child with wait_for_stop.
3537 This block will analyze the /proc status, and translate it
3538 into a waitstatus for GDB.
3540 If we actually had to call wait because the /proc file
3541 is gone (child terminated), then we skip this block,
3542 because we already have a waitstatus. */
3544 flags
= proc_flags (pi
);
3545 why
= proc_why (pi
);
3546 what
= proc_what (pi
);
3548 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3551 /* If it's running async (for single_thread control),
3552 set it back to normal again. */
3553 if (flags
& PR_ASYNC
)
3554 if (!proc_unset_async (pi
))
3555 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3559 proc_prettyprint_why (why
, what
, 1);
3561 /* The 'pid' we will return to GDB is composed of
3562 the process ID plus the lwp ID. */
3563 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3567 wstat
= (what
<< 8) | 0177;
3577 #if defined (SYS_lwp_exit) || defined (SYS_lwpexit)
3578 printf_filtered ("[%s exited]\n",
3579 target_pid_to_str (retval
));
3580 delete_thread (retval
);
3581 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3583 #endif /* _lwp_exit */
3586 /* Handle SYS_exit call only */
3587 /* Stopped at entry to SYS_exit.
3588 Make it runnable, resume it, then use
3589 the wait system call to get its exit code.
3590 Proc_run_process always clears the current
3592 Then return its exit status. */
3593 pi
->status_valid
= 0;
3595 /* FIXME: what we should do is return
3596 TARGET_WAITKIND_SPURIOUS. */
3597 if (!proc_run_process (pi
, 0, 0))
3598 proc_error (pi
, "target_wait, run_process", __LINE__
);
3601 /* Don't call wait: simulate waiting for exit,
3602 return a "success" exit code. Bogus: what if
3603 it returns something else? */
3605 retval
= inferior_pid
; /* ? ? ? */
3609 int temp
= wait (&wstat
);
3611 /* FIXME: shouldn't I make sure I get the right
3612 event from the right process? If (for
3613 instance) I have killed an earlier inferior
3614 process but failed to clean up after it
3615 somehow, I could get its termination event
3618 /* If wait returns -1, that's what we return to GDB. */
3624 printf_filtered ("procfs: trapped on entry to ");
3625 proc_prettyprint_syscall (proc_what (pi
), 0);
3626 printf_filtered ("\n");
3629 long i
, nsysargs
, *sysargs
;
3631 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3632 (sysargs
= proc_sysargs (pi
)) != NULL
)
3634 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
3635 for (i
= 0; i
< nsysargs
; i
++)
3636 printf_filtered ("#%ld: 0x%08lx\n",
3644 /* How to exit gracefully, returning "unknown event" */
3645 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3646 return inferior_pid
;
3650 /* How to keep going without returning to wfi: */
3651 target_resume (pid
, 0, TARGET_SIGNAL_0
);
3668 /* Hopefully this is our own "fork-child" execing
3669 the real child. Hoax this event into a trap, and
3670 GDB will see the child about to execute its start
3672 wstat
= (SIGTRAP
<< 8) | 0177;
3674 #ifdef SYS_lwp_create
3675 case SYS_lwp_create
:
3677 #ifdef SYS_lwpcreate
3680 #if defined(SYS_lwp_create) || defined(SYS_lwpcreate)
3682 * This syscall is somewhat like fork/exec.
3683 * We will get the event twice: once for the parent LWP,
3684 * and once for the child. We should already know about
3685 * the parent LWP, but the child will be new to us. So,
3686 * whenever we get this event, if it represents a new
3687 * thread, simply add the thread to the list.
3690 /* If not in procinfo list, add it. */
3691 temp
= proc_get_current_thread (pi
);
3692 if (!find_procinfo (pi
->pid
, temp
))
3693 create_procinfo (pi
->pid
, temp
);
3695 temp
= MERGEPID (pi
->pid
, temp
);
3696 /* If not in GDB's thread list, add it. */
3697 if (!in_thread_list (temp
))
3699 printf_filtered ("[New %s]\n", target_pid_to_str (temp
));
3702 /* Return to WFI, but tell it to immediately resume. */
3703 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3704 return inferior_pid
;
3705 #endif /* _lwp_create */
3713 #if defined (SYS_lwp_exit) || defined (SYS_lwpexit)
3714 printf_filtered ("[%s exited]\n",
3715 target_pid_to_str (retval
));
3716 delete_thread (retval
);
3717 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3719 #endif /* _lwp_exit */
3723 /* Nothing to do here for now. The old procfs
3724 seemed to use this event to handle threads on
3725 older (non-LWP) systems, where I'm assuming that
3726 threads were actually separate processes. Irix,
3727 maybe? Anyway, low priority for now. */
3731 /* FIXME: do we need to handle this? Investigate. */
3735 /* FIXME: see above. */
3738 printf_filtered ("procfs: trapped on exit from ");
3739 proc_prettyprint_syscall (proc_what (pi
), 0);
3740 printf_filtered ("\n");
3743 long i
, nsysargs
, *sysargs
;
3745 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3746 (sysargs
= proc_sysargs (pi
)) != NULL
)
3748 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
3749 for (i
= 0; i
< nsysargs
; i
++)
3750 printf_filtered ("#%ld: 0x%08lx\n",
3755 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3756 return inferior_pid
;
3761 wstat
= (SIGSTOP
<< 8) | 0177;
3766 printf_filtered ("Retry #%d:\n", retry
);
3767 pi
->status_valid
= 0;
3772 /* If not in procinfo list, add it. */
3773 temp
= proc_get_current_thread (pi
);
3774 if (!find_procinfo (pi
->pid
, temp
))
3775 create_procinfo (pi
->pid
, temp
);
3777 /* If not in GDB's thread list, add it. */
3778 temp
= MERGEPID (pi
->pid
, temp
);
3779 if (!in_thread_list (temp
))
3781 printf_filtered ("[New %s]\n",
3782 target_pid_to_str (temp
));
3786 status
->kind
= TARGET_WAITKIND_STOPPED
;
3787 status
->value
.sig
= 0;
3792 wstat
= (what
<< 8) | 0177;
3795 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
3798 wstat
= (SIGTRAP
<< 8) | 0177;
3803 wstat
= (SIGTRAP
<< 8) | 0177;
3806 /* FIXME: use si_signo where possible. */
3808 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
3811 wstat
= (SIGILL
<< 8) | 0177;
3814 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
3817 wstat
= (SIGTRAP
<< 8) | 0177;
3821 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
3824 wstat
= (SIGSEGV
<< 8) | 0177;
3828 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
3831 wstat
= (SIGFPE
<< 8) | 0177;
3833 case FLTPAGE
: /* Recoverable page fault */
3834 default: /* FIXME: use si_signo if possible for fault */
3836 printf_filtered ("procfs:%d -- ", __LINE__
);
3837 printf_filtered ("child stopped for unknown reason:\n");
3838 proc_prettyprint_why (why
, what
, 1);
3839 error ("... giving up...");
3842 break; /* case PR_FAULTED: */
3843 default: /* switch (why) unmatched */
3844 printf_filtered ("procfs:%d -- ", __LINE__
);
3845 printf_filtered ("child stopped for unknown reason:\n");
3846 proc_prettyprint_why (why
, what
, 1);
3847 error ("... giving up...");
3851 * Got this far without error:
3852 * If retval isn't in the threads database, add it.
3855 retval
!= inferior_pid
&&
3856 !in_thread_list (retval
))
3859 * We have a new thread.
3860 * We need to add it both to GDB's list and to our own.
3861 * If we don't create a procinfo, resume may be unhappy
3864 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
3865 add_thread (retval
);
3866 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
3867 create_procinfo (PIDGET (retval
), TIDGET (retval
));
3869 /* In addition, it's possible that this is the first
3870 * new thread we've seen, in which case we may not
3871 * have created entries for inferior_pid yet.
3873 if (TIDGET (inferior_pid
) != 0)
3875 if (!in_thread_list (inferior_pid
))
3876 add_thread (inferior_pid
);
3877 if (find_procinfo (PIDGET (inferior_pid
),
3878 TIDGET (inferior_pid
)) == NULL
)
3879 create_procinfo (PIDGET (inferior_pid
),
3880 TIDGET (inferior_pid
));
3884 else /* flags do not indicate STOPPED */
3886 /* surely this can't happen... */
3887 printf_filtered ("procfs:%d -- process not stopped.\n",
3889 proc_prettyprint_flags (flags
, 1);
3890 error ("procfs: ...giving up...");
3895 store_waitstatus (status
, wstat
);
3901 /* Transfer LEN bytes between GDB address MYADDR and target address
3902 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
3903 otherwise transfer them from the target. TARGET is unused.
3905 The return value is 0 if an error occurred or no bytes were
3906 transferred. Otherwise, it will be a positive value which
3907 indicates the number of bytes transferred between gdb and the
3908 target. (Note that the interface also makes provisions for
3909 negative values, but this capability isn't implemented here.) */
3912 procfs_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int dowrite
,
3913 struct mem_attrib
*attrib
,
3914 struct target_ops
*target
)
3919 /* Find procinfo for main process */
3920 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
3921 if (pi
->as_fd
== 0 &&
3922 open_procinfo_files (pi
, FD_AS
) == 0)
3924 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
3928 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
3933 PROCFS_NOTE ("write memory: ");
3935 PROCFS_NOTE ("write memory: \n");
3937 nbytes
= write (pi
->as_fd
, myaddr
, len
);
3941 PROCFS_NOTE ("read memory: \n");
3942 nbytes
= read (pi
->as_fd
, myaddr
, len
);
3953 * Function: invalidate_cache
3955 * Called by target_resume before making child runnable.
3956 * Mark cached registers and status's invalid.
3957 * If there are "dirty" caches that need to be written back
3958 * to the child process, do that.
3960 * File descriptors are also cached.
3961 * As they are a limited resource, we cannot hold onto them indefinitely.
3962 * However, as they are expensive to open, we don't want to throw them
3963 * away indescriminately either. As a compromise, we will keep the
3964 * file descriptors for the parent process, but discard any file
3965 * descriptors we may have accumulated for the threads.
3968 * As this function is called by iterate_over_threads, it always
3969 * returns zero (so that iterate_over_threads will keep iterating).
3974 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
3977 * About to run the child; invalidate caches and do any other cleanup.
3981 if (pi
->gregs_dirty
)
3982 if (parent
== NULL
||
3983 proc_get_current_thread (parent
) != pi
->tid
)
3984 if (!proc_set_gregs (pi
)) /* flush gregs cache */
3985 proc_warn (pi
, "target_resume, set_gregs",
3987 if (FP0_REGNUM
>= 0)
3988 if (pi
->fpregs_dirty
)
3989 if (parent
== NULL
||
3990 proc_get_current_thread (parent
) != pi
->tid
)
3991 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
3992 proc_warn (pi
, "target_resume, set_fpregs",
3998 /* The presence of a parent indicates that this is an LWP.
3999 Close any file descriptors that it might have open.
4000 We don't do this to the master (parent) procinfo. */
4002 close_procinfo_files (pi
);
4004 pi
->gregs_valid
= 0;
4005 pi
->fpregs_valid
= 0;
4007 pi
->gregs_dirty
= 0;
4008 pi
->fpregs_dirty
= 0;
4010 pi
->status_valid
= 0;
4011 pi
->threads_valid
= 0;
4018 * Function: make_signal_thread_runnable
4020 * A callback function for iterate_over_threads.
4021 * Find the asynchronous signal thread, and make it runnable.
4022 * See if that helps matters any.
4026 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4029 if (proc_flags (pi
) & PR_ASLWP
)
4031 if (!proc_run_process (pi
, 0, -1))
4032 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4041 * Function: target_resume
4043 * Make the child process runnable. Normally we will then call
4044 * procfs_wait and wait for it to stop again (unles gdb is async).
4047 * step: if true, then arrange for the child to stop again
4048 * after executing a single instruction.
4049 * signo: if zero, then cancel any pending signal.
4050 * If non-zero, then arrange for the indicated signal
4051 * to be delivered to the child when it runs.
4052 * pid: if -1, then allow any child thread to run.
4053 * if non-zero, then allow only the indicated thread to run.
4054 ******* (not implemented yet)
4058 procfs_resume (int pid
, int step
, enum target_signal signo
)
4060 procinfo
*pi
, *thread
;
4064 prrun.prflags |= PRSVADDR;
4065 prrun.pr_vaddr = $PC; set resume address
4066 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4067 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4068 prrun.prflags |= PRCFAULT; clear current fault.
4070 PRSTRACE and PRSFAULT can be done by other means
4071 (proc_trace_signals, proc_trace_faults)
4072 PRSVADDR is unnecessary.
4073 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4074 This basically leaves PRSTEP and PRCSIG.
4075 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4076 So basically PR_STEP is the sole argument that must be passed
4077 to proc_run_process (for use in the prrun struct by ioctl). */
4079 /* Find procinfo for main process */
4080 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4082 /* First cut: ignore pid argument */
4085 /* Convert signal to host numbering. */
4087 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4090 native_signo
= target_signal_to_host (signo
);
4092 pi
->ignore_next_sigstop
= 0;
4094 /* Running the process voids all cached registers and status. */
4095 /* Void the threads' caches first */
4096 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4097 /* Void the process procinfo's caches. */
4098 invalidate_cache (NULL
, pi
, NULL
);
4102 /* Resume a specific thread, presumably suppressing the others. */
4103 thread
= find_procinfo (PIDGET (pid
), TIDGET (pid
));
4105 warning ("procfs: resume can't find thread %d -- resuming all.",
4109 if (thread
->tid
!= 0)
4111 /* We're to resume a specific thread, and not the others.
4112 * Set the child process's PR_ASYNC flag.
4115 if (!proc_set_async (pi
))
4116 proc_error (pi
, "target_resume, set_async", __LINE__
);
4119 proc_iterate_over_threads (pi
,
4120 make_signal_thread_runnable
,
4123 pi
= thread
; /* substitute the thread's procinfo for run */
4128 if (!proc_run_process (pi
, step
, native_signo
))
4131 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4133 proc_error (pi
, "target_resume", __LINE__
);
4138 * Function: register_gdb_signals
4140 * Traverse the list of signals that GDB knows about
4141 * (see "handle" command), and arrange for the target
4142 * to be stopped or not, according to these settings.
4144 * Returns non-zero for success, zero for failure.
4148 register_gdb_signals (procinfo
*pi
, sigset_t
*signals
)
4152 for (signo
= 0; signo
< NSIG
; signo
++)
4153 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4154 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4155 signal_pass_state (target_signal_from_host (signo
)) == 1)
4156 prdelset (signals
, signo
);
4158 praddset (signals
, signo
);
4160 return proc_set_traced_signals (pi
, signals
);
4164 * Function: target_notice_signals
4166 * Set up to trace signals in the child process.
4170 procfs_notice_signals (int pid
)
4173 procinfo
*pi
= find_procinfo_or_die (PIDGET (pid
), 0);
4175 if (proc_get_traced_signals (pi
, &signals
) &&
4176 register_gdb_signals (pi
, &signals
))
4179 proc_error (pi
, "notice_signals", __LINE__
);
4183 * Function: target_files_info
4185 * Print status information about the child process.
4189 procfs_files_info (struct target_ops
*ignore
)
4191 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4192 attach_flag
? "attached": "child",
4193 target_pid_to_str (inferior_pid
));
4197 * Function: target_open
4199 * A dummy: you don't open procfs.
4203 procfs_open (char *args
, int from_tty
)
4205 error ("Use the \"run\" command to start a Unix child process.");
4209 * Function: target_can_run
4211 * This tells GDB that this target vector can be invoked
4212 * for "run" or "attach".
4215 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4216 be a runnable target. Used by targets
4217 that can sit atop procfs, such as solaris
4222 procfs_can_run (void)
4224 /* This variable is controlled by modules that sit atop procfs that
4225 may layer their own process structure atop that provided here.
4226 sol-thread.c does this because of the Solaris two-level thread
4229 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4231 return !procfs_suppress_run
;
4235 * Function: target_stop
4237 * Stop the child process asynchronously, as when the
4238 * gdb user types control-c or presses a "stop" button.
4240 * Works by sending kill(SIGINT) to the child's process group.
4246 extern pid_t inferior_process_group
;
4248 kill (-inferior_process_group
, SIGINT
);
4252 * Function: unconditionally_kill_inferior
4254 * Make it die. Wait for it to die. Clean up after it.
4255 * Note: this should only be applied to the real process,
4256 * not to an LWP, because of the check for parent-process.
4257 * If we need this to work for an LWP, it needs some more logic.
4261 unconditionally_kill_inferior (procinfo
*pi
)
4265 parent_pid
= proc_parent_pid (pi
);
4266 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4267 /* FIXME: use access functions */
4268 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4269 before the PIOCKILL, otherwise it might generate a corrupted core
4270 file for the inferior. */
4271 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4273 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4276 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4277 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4278 to kill the inferior, otherwise it might remain stopped with a
4280 We do not check the result of the PIOCSSIG, the inferior might have
4283 struct siginfo newsiginfo
;
4285 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4286 newsiginfo
.si_signo
= SIGKILL
;
4287 newsiginfo
.si_code
= 0;
4288 newsiginfo
.si_errno
= 0;
4289 newsiginfo
.si_pid
= getpid ();
4290 newsiginfo
.si_uid
= getuid ();
4291 /* FIXME: use proc_set_current_signal */
4292 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4294 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4295 if (!proc_kill (pi
, SIGKILL
))
4296 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4297 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4298 destroy_procinfo (pi
);
4300 /* If pi is GDB's child, wait for it to die. */
4301 if (parent_pid
== getpid ())
4302 /* FIXME: should we use waitpid to make sure we get the right event?
4303 Should we check the returned event? */
4308 ret
= waitpid (pi
->pid
, &status
, 0);
4316 * Function: target_kill_inferior
4318 * We're done debugging it, and we want it to go away.
4319 * Then we want GDB to forget all about it.
4323 procfs_kill_inferior (void)
4325 if (inferior_pid
!= 0) /* ? */
4327 /* Find procinfo for main process */
4328 procinfo
*pi
= find_procinfo (PIDGET (inferior_pid
), 0);
4331 unconditionally_kill_inferior (pi
);
4332 target_mourn_inferior ();
4337 * Function: target_mourn_inferior
4339 * Forget we ever debugged this thing!
4343 procfs_mourn_inferior (void)
4347 if (inferior_pid
!= 0)
4349 /* Find procinfo for main process */
4350 pi
= find_procinfo (PIDGET (inferior_pid
), 0);
4352 destroy_procinfo (pi
);
4354 unpush_target (&procfs_ops
);
4355 generic_mourn_inferior ();
4359 * Function: init_inferior
4361 * When GDB forks to create a runnable inferior process,
4362 * this function is called on the parent side of the fork.
4363 * It's job is to do whatever is necessary to make the child
4364 * ready to be debugged, and then wait for the child to synchronize.
4368 procfs_init_inferior (int pid
)
4374 /* This routine called on the parent side (GDB side)
4375 after GDB forks the inferior. */
4377 push_target (&procfs_ops
);
4379 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4380 perror ("procfs: out of memory in 'init_inferior'");
4382 if (!open_procinfo_files (pi
, FD_CTL
))
4383 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4387 open_procinfo_files // done
4390 procfs_notice_signals
4397 /* If not stopped yet, wait for it to stop. */
4398 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4399 !(proc_wait_for_stop (pi
)))
4400 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4402 /* Save some of the /proc state to be restored if we detach. */
4403 /* FIXME: Why? In case another debugger was debugging it?
4404 We're it's parent, for Ghu's sake! */
4405 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4406 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4407 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4408 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4409 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4410 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4411 if (!proc_get_traced_sysentry (pi
, &pi
->saved_entryset
))
4412 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4413 if (!proc_get_traced_sysexit (pi
, &pi
->saved_exitset
))
4414 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4416 /* Register to trace selected signals in the child. */
4417 prfillset (&signals
);
4418 if (!register_gdb_signals (pi
, &signals
))
4419 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4421 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4422 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4424 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4425 and possibly even turning ON kill-on-last-close at this point. But
4426 I can't make that change without careful testing which I don't have
4427 time to do right now... */
4428 /* Turn on run-on-last-close flag so that the child
4429 will die if GDB goes away for some reason. */
4430 if (!proc_set_run_on_last_close (pi
))
4431 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4433 /* The 'process ID' we return to GDB is composed of
4434 the actual process ID plus the lwp ID. */
4435 inferior_pid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4437 #ifdef START_INFERIOR_TRAPS_EXPECTED
4438 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4440 /* One trap to exec the shell, one to exec the program being debugged. */
4441 startup_inferior (2);
4442 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4446 * Function: set_exec_trap
4448 * When GDB forks to create a new process, this function is called
4449 * on the child side of the fork before GDB exec's the user program.
4450 * Its job is to make the child minimally debuggable, so that the
4451 * parent GDB process can connect to the child and take over.
4452 * This function should do only the minimum to make that possible,
4453 * and to synchronize with the parent process. The parent process
4454 * should take care of the details.
4458 procfs_set_exec_trap (void)
4460 /* This routine called on the child side (inferior side)
4461 after GDB forks the inferior. It must use only local variables,
4462 because it may be sharing data space with its parent. */
4467 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4468 perror_with_name ("procfs: create_procinfo failed in child.");
4470 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4472 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4473 gdb_flush (gdb_stderr
);
4474 /* no need to call "dead_procinfo", because we're going to exit. */
4478 #ifdef PRFS_STOPEXEC /* defined on OSF */
4479 /* OSF method for tracing exec syscalls. Quoting:
4480 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4481 exits from exec system calls because of the user level loader. */
4482 /* FIXME: make nice and maybe move into an access function. */
4486 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4488 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4489 gdb_flush (gdb_stderr
);
4492 prfs_flags
|= PRFS_STOPEXEC
;
4494 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4496 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4497 gdb_flush (gdb_stderr
);
4501 #else /* not PRFS_STOPEXEC */
4502 /* Everyone else's (except OSF) method for tracing exec syscalls */
4504 Not all systems with /proc have all the exec* syscalls with the same
4505 names. On the SGI, for example, there is no SYS_exec, but there
4506 *is* a SYS_execv. So, we try to account for that. */
4508 premptyset (&exitset
);
4510 praddset (&exitset
, SYS_exec
);
4513 praddset (&exitset
, SYS_execve
);
4516 praddset (&exitset
, SYS_execv
);
4519 if (!proc_set_traced_sysexit (pi
, &exitset
))
4521 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4522 gdb_flush (gdb_stderr
);
4525 #endif /* PRFS_STOPEXEC */
4527 /* FIXME: should this be done in the parent instead? */
4528 /* Turn off inherit on fork flag so that all grand-children
4529 of gdb start with tracing flags cleared. */
4530 if (!proc_unset_inherit_on_fork (pi
))
4531 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4533 /* Turn off run on last close flag, so that the child process
4534 cannot run away just because we close our handle on it.
4535 We want it to wait for the parent to attach. */
4536 if (!proc_unset_run_on_last_close (pi
))
4537 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4539 /* FIXME: No need to destroy the procinfo --
4540 we have our own address space, and we're about to do an exec! */
4541 /*destroy_procinfo (pi);*/
4545 * Function: create_inferior
4547 * This function is called BEFORE gdb forks the inferior process.
4548 * Its only real responsibility is to set things up for the fork,
4549 * and tell GDB which two functions to call after the fork (one
4550 * for the parent, and one for the child).
4552 * This function does a complicated search for a unix shell program,
4553 * which it then uses to parse arguments and environment variables
4554 * to be sent to the child. I wonder whether this code could not
4555 * be abstracted out and shared with other unix targets such as
4560 procfs_create_inferior (char *exec_file
, char *allargs
, char **env
)
4562 char *shell_file
= getenv ("SHELL");
4564 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4567 /* We will be looking down the PATH to find shell_file. If we
4568 just do this the normal way (via execlp, which operates by
4569 attempting an exec for each element of the PATH until it
4570 finds one which succeeds), then there will be an exec for
4571 each failed attempt, each of which will cause a PR_SYSEXIT
4572 stop, and we won't know how to distinguish the PR_SYSEXIT's
4573 for these failed execs with the ones for successful execs
4574 (whether the exec has succeeded is stored at that time in the
4575 carry bit or some such architecture-specific and
4576 non-ABI-specified place).
4578 So I can't think of anything better than to search the PATH
4579 now. This has several disadvantages: (1) There is a race
4580 condition; if we find a file now and it is deleted before we
4581 exec it, we lose, even if the deletion leaves a valid file
4582 further down in the PATH, (2) there is no way to know exactly
4583 what an executable (in the sense of "capable of being
4584 exec'd") file is. Using access() loses because it may lose
4585 if the caller is the superuser; failing to use it loses if
4586 there are ACLs or some such. */
4590 /* FIXME-maybe: might want "set path" command so user can change what
4591 path is used from within GDB. */
4592 char *path
= getenv ("PATH");
4594 struct stat statbuf
;
4597 path
= "/bin:/usr/bin";
4599 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4600 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4602 p1
= strchr (p
, ':');
4607 strncpy (tryname
, p
, len
);
4608 tryname
[len
] = '\0';
4609 strcat (tryname
, "/");
4610 strcat (tryname
, shell_file
);
4611 if (access (tryname
, X_OK
) < 0)
4613 if (stat (tryname
, &statbuf
) < 0)
4615 if (!S_ISREG (statbuf
.st_mode
))
4616 /* We certainly need to reject directories. I'm not quite
4617 as sure about FIFOs, sockets, etc., but I kind of doubt
4618 that people want to exec() these things. */
4623 /* Not found. This must be an error rather than merely passing
4624 the file to execlp(), because execlp() would try all the
4625 exec()s, causing GDB to get confused. */
4626 error ("procfs:%d -- Can't find shell %s in PATH",
4627 __LINE__
, shell_file
);
4629 shell_file
= tryname
;
4632 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4633 procfs_init_inferior
, NULL
, shell_file
);
4635 /* We are at the first instruction we care about. */
4636 /* Pedal to the metal... */
4638 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
4642 * Function: notice_thread
4644 * Callback for find_new_threads.
4645 * Calls "add_thread".
4649 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
4651 int gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
4653 if (!in_thread_list (gdb_threadid
))
4654 add_thread (gdb_threadid
);
4660 * Function: target_find_new_threads
4662 * Query all the threads that the target knows about,
4663 * and give them back to GDB to add to its list.
4667 procfs_find_new_threads (void)
4671 /* Find procinfo for main process */
4672 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4673 proc_update_threads (pi
);
4674 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
4678 * Function: target_thread_alive
4680 * Return true if the thread is still 'alive'.
4682 * This guy doesn't really seem to be doing his job.
4683 * Got to investigate how to tell when a thread is really gone.
4687 procfs_thread_alive (int pid
)
4692 proc
= PIDGET (pid
);
4693 thread
= TIDGET (pid
);
4694 /* If I don't know it, it ain't alive! */
4695 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
4698 /* If I can't get its status, it ain't alive!
4699 What's more, I need to forget about it! */
4700 if (!proc_get_status (pi
))
4702 destroy_procinfo (pi
);
4705 /* I couldn't have got its status if it weren't alive, so it's alive. */
4710 * Function: target_pid_to_str
4712 * Return a string to be used to identify the thread in
4713 * the "info threads" display.
4717 procfs_pid_to_str (int pid
)
4719 static char buf
[80];
4723 proc
= PIDGET (pid
);
4724 thread
= TIDGET (pid
);
4725 pi
= find_procinfo (proc
, thread
);
4728 sprintf (buf
, "Process %d", proc
);
4730 sprintf (buf
, "LWP %d", thread
);
4735 * Function: procfs_set_watchpoint
4736 * Insert a watchpoint
4740 procfs_set_watchpoint (int pid
, CORE_ADDR addr
, int len
, int rwflag
, int after
)
4746 pi
= find_procinfo_or_die (pid
== -1 ?
4747 PIDGET (inferior_pid
) : PIDGET (pid
), 0);
4749 /* Translate from GDB's flags to /proc's */
4750 if (len
> 0) /* len == 0 means delete watchpoint */
4752 switch (rwflag
) { /* FIXME: need an enum! */
4753 case hw_write
: /* default watchpoint (write) */
4754 pflags
= WRITE_WATCHFLAG
;
4756 case hw_read
: /* read watchpoint */
4757 pflags
= READ_WATCHFLAG
;
4759 case hw_access
: /* access watchpoint */
4760 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
4762 case hw_execute
: /* execution HW breakpoint */
4763 pflags
= EXEC_WATCHFLAG
;
4765 default: /* Something weird. Return error. */
4768 if (after
) /* Stop after r/w access is completed. */
4769 pflags
|= AFTER_WATCHFLAG
;
4772 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
4774 if (errno
== E2BIG
) /* Typical error for no resources */
4775 return -1; /* fail */
4776 /* GDB may try to remove the same watchpoint twice.
4777 If a remove request returns no match, don't error. */
4778 if (errno
== ESRCH
&& len
== 0)
4779 return 0; /* ignore */
4780 proc_error (pi
, "set_watchpoint", __LINE__
);
4787 * Function: stopped_by_watchpoint
4789 * Returns non-zero if process is stopped on a hardware watchpoint fault,
4790 * else returns zero.
4794 procfs_stopped_by_watchpoint (int pid
)
4798 pi
= find_procinfo (pid
== -1 ?
4799 PIDGET (inferior_pid
) : PIDGET (pid
), 0);
4801 if (!pi
) /* If no process, then not stopped by watchpoint! */
4804 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
4806 if (proc_why (pi
) == PR_FAULTED
)
4809 if (proc_what (pi
) == FLTWATCH
)
4813 if (proc_what (pi
) == FLTKWATCH
)
4821 #ifdef TM_I386SOL2_H
4823 * Function: procfs_find_LDT_entry
4826 * int pid; // The GDB-style pid-plus-LWP.
4829 * pointer to the corresponding LDT entry.
4833 procfs_find_LDT_entry (int pid
)
4835 gdb_gregset_t
*gregs
;
4839 /* Find procinfo for the lwp. */
4840 if ((pi
= find_procinfo (PIDGET (pid
), TIDGET (pid
))) == NULL
)
4842 warning ("procfs_find_LDT_entry: could not find procinfi for %d.",
4846 /* get its general registers. */
4847 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
4849 warning ("procfs_find_LDT_entry: could not read gregs for %d.",
4853 /* Now extract the GS register's lower 16 bits. */
4854 key
= (*gregs
)[GS
] & 0xffff;
4856 /* Find the matching entry and return it. */
4857 return proc_get_LDT_entry (pi
, key
);
4859 #endif /* TM_I386SOL2_H */
4864 info_proc_cmd (char *args
, int from_tty
)
4866 struct cleanup
*old_chain
;
4867 procinfo
*process
= NULL
;
4868 procinfo
*thread
= NULL
;
4874 old_chain
= make_cleanup (null_cleanup
, 0);
4877 if ((argv
= buildargv (args
)) == NULL
)
4880 make_cleanup_freeargv (argv
);
4882 while (argv
!= NULL
&& *argv
!= NULL
)
4884 if (isdigit (argv
[0][0]))
4886 pid
= strtoul (argv
[0], &tmp
, 10);
4888 tid
= strtoul (++tmp
, NULL
, 10);
4890 else if (argv
[0][0] == '/')
4892 tid
= strtoul (argv
[0] + 1, NULL
, 10);
4901 pid
= PIDGET (inferior_pid
);
4903 error ("No current process: you must name one.");
4906 /* Have pid, will travel.
4907 First see if it's a process we're already debugging. */
4908 process
= find_procinfo (pid
, 0);
4909 if (process
== NULL
)
4911 /* No. So open a procinfo for it, but
4912 remember to close it again when finished. */
4913 process
= create_procinfo (pid
, 0);
4914 make_cleanup (do_destroy_procinfo_cleanup
, process
);
4915 if (!open_procinfo_files (process
, FD_CTL
))
4916 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
4920 thread
= create_procinfo (pid
, tid
);
4924 printf_filtered ("process %d flags:\n", process
->pid
);
4925 proc_prettyprint_flags (proc_flags (process
), 1);
4926 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
4927 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
4928 if (proc_get_nthreads (process
) > 1)
4929 printf_filtered ("Process has %d threads.\n",
4930 proc_get_nthreads (process
));
4934 printf_filtered ("thread %d flags:\n", thread
->tid
);
4935 proc_prettyprint_flags (proc_flags (thread
), 1);
4936 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
4937 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
4940 do_cleanups (old_chain
);
4944 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
4950 if (inferior_pid
<= 0)
4951 error ("you must be debugging a process to use this command.");
4953 if (args
== NULL
|| args
[0] == 0)
4954 error_no_arg ("system call to trace");
4956 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4957 if (isdigit (args
[0]))
4959 syscallnum
= atoi (args
);
4960 if (entry_or_exit
== PR_SYSENTRY
)
4961 sysset
= proc_get_traced_sysentry (pi
, NULL
);
4963 sysset
= proc_get_traced_sysexit (pi
, NULL
);
4966 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
4968 if (mode
== FLAG_SET
)
4969 praddset (sysset
, syscallnum
);
4971 prdelset (sysset
, syscallnum
);
4973 if (entry_or_exit
== PR_SYSENTRY
)
4975 if (!proc_set_traced_sysentry (pi
, sysset
))
4976 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
4980 if (!proc_set_traced_sysexit (pi
, sysset
))
4981 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
4987 proc_trace_sysentry_cmd (char *args
, int from_tty
)
4989 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
4993 proc_trace_sysexit_cmd (char *args
, int from_tty
)
4995 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
4999 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5001 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5005 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5007 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5012 _initialize_procfs (void)
5015 add_target (&procfs_ops
);
5016 add_info ("proc", info_proc_cmd
,
5017 "Show /proc process information about any running process.\
5018 Default is the process being debugged.");
5019 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5020 "Give a trace of entries into the syscall.");
5021 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5022 "Give a trace of exits from the syscall.");
5023 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5024 "Cancel a trace of entries into the syscall.");
5025 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5026 "Cancel a trace of exits from the syscall.");
5029 /* =================== END, GDB "MODULE" =================== */
5033 /* miscelaneous stubs: */
5034 /* The following satisfy a few random symbols mostly created by */
5035 /* the solaris threads implementation, which I will chase down */
5039 * Return a pid for which we guarantee
5040 * we will be able to find a 'live' procinfo.
5044 procfs_first_available (void)
5047 return procinfo_list
->pid
;