1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
3 Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation,
6 Written by Michael Snyder at Cygnus Solutions.
7 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software Foundation,
23 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #include "elf-bfd.h" /* for elfcore_write_* */
31 #include "gdbthread.h"
33 #if defined (NEW_PROC_API)
34 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
37 #include <sys/procfs.h>
38 #ifdef HAVE_SYS_FAULT_H
39 #include <sys/fault.h>
41 #ifdef HAVE_SYS_SYSCALL_H
42 #include <sys/syscall.h>
44 #include <sys/errno.h>
48 #include "gdb_string.h"
49 #include "gdb_assert.h"
55 * This module provides the interface between GDB and the
56 * /proc file system, which is used on many versions of Unix
57 * as a means for debuggers to control other processes.
58 * Examples of the systems that use this interface are:
65 * /proc works by imitating a file system: you open a simulated file
66 * that represents the process you wish to interact with, and
67 * perform operations on that "file" in order to examine or change
68 * the state of the other process.
70 * The most important thing to know about /proc and this module
71 * is that there are two very different interfaces to /proc:
72 * One that uses the ioctl system call, and
73 * another that uses read and write system calls.
74 * This module has to support both /proc interfaces. This means
75 * that there are two different ways of doing every basic operation.
77 * In order to keep most of the code simple and clean, I have
78 * defined an interface "layer" which hides all these system calls.
79 * An ifdef (NEW_PROC_API) determines which interface we are using,
80 * and most or all occurrances of this ifdef should be confined to
81 * this interface layer.
85 /* Determine which /proc API we are using:
86 The ioctl API defines PIOCSTATUS, while
87 the read/write (multiple fd) API never does. */
90 #include <sys/types.h>
91 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
94 #include <fcntl.h> /* for O_RDONLY */
95 #include <unistd.h> /* for "X_OK" */
96 #include "gdb_stat.h" /* for struct stat */
98 /* Note: procfs-utils.h must be included after the above system header
99 files, because it redefines various system calls using macros.
100 This may be incompatible with the prototype declarations. */
102 #include "proc-utils.h"
104 /* Prototypes for supply_gregset etc. */
107 /* =================== TARGET_OPS "MODULE" =================== */
110 * This module defines the GDB target vector and its methods.
113 static void procfs_open (char *, int);
114 static void procfs_attach (char *, int);
115 static void procfs_detach (char *, int);
116 static void procfs_resume (ptid_t
, int, enum target_signal
);
117 static int procfs_can_run (void);
118 static void procfs_stop (void);
119 static void procfs_files_info (struct target_ops
*);
120 static void procfs_fetch_registers (int);
121 static void procfs_store_registers (int);
122 static void procfs_notice_signals (ptid_t
);
123 static void procfs_prepare_to_store (void);
124 static void procfs_kill_inferior (void);
125 static void procfs_mourn_inferior (void);
126 static void procfs_create_inferior (char *, char *, char **);
127 static ptid_t
procfs_wait (ptid_t
, struct target_waitstatus
*);
128 static int procfs_xfer_memory (CORE_ADDR
, char *, int, int,
129 struct mem_attrib
*attrib
,
130 struct target_ops
*);
132 static int procfs_thread_alive (ptid_t
);
134 void procfs_find_new_threads (void);
135 char *procfs_pid_to_str (ptid_t
);
137 static int proc_find_memory_regions (int (*) (CORE_ADDR
,
143 static char * procfs_make_note_section (bfd
*, int *);
145 static int procfs_can_use_hw_breakpoint (int, int, int);
147 struct target_ops procfs_ops
; /* the target vector */
150 init_procfs_ops (void)
152 procfs_ops
.to_shortname
= "procfs";
153 procfs_ops
.to_longname
= "Unix /proc child process";
155 "Unix /proc child process (started by the \"run\" command).";
156 procfs_ops
.to_open
= procfs_open
;
157 procfs_ops
.to_can_run
= procfs_can_run
;
158 procfs_ops
.to_create_inferior
= procfs_create_inferior
;
159 procfs_ops
.to_kill
= procfs_kill_inferior
;
160 procfs_ops
.to_mourn_inferior
= procfs_mourn_inferior
;
161 procfs_ops
.to_attach
= procfs_attach
;
162 procfs_ops
.to_detach
= procfs_detach
;
163 procfs_ops
.to_wait
= procfs_wait
;
164 procfs_ops
.to_resume
= procfs_resume
;
165 procfs_ops
.to_prepare_to_store
= procfs_prepare_to_store
;
166 procfs_ops
.to_fetch_registers
= procfs_fetch_registers
;
167 procfs_ops
.to_store_registers
= procfs_store_registers
;
168 procfs_ops
.to_xfer_memory
= procfs_xfer_memory
;
169 procfs_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
170 procfs_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
171 procfs_ops
.to_notice_signals
= procfs_notice_signals
;
172 procfs_ops
.to_files_info
= procfs_files_info
;
173 procfs_ops
.to_stop
= procfs_stop
;
175 procfs_ops
.to_terminal_init
= terminal_init_inferior
;
176 procfs_ops
.to_terminal_inferior
= terminal_inferior
;
177 procfs_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
178 procfs_ops
.to_terminal_ours
= terminal_ours
;
179 procfs_ops
.to_terminal_save_ours
= terminal_save_ours
;
180 procfs_ops
.to_terminal_info
= child_terminal_info
;
182 procfs_ops
.to_find_new_threads
= procfs_find_new_threads
;
183 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
184 procfs_ops
.to_pid_to_str
= procfs_pid_to_str
;
186 procfs_ops
.to_has_all_memory
= 1;
187 procfs_ops
.to_has_memory
= 1;
188 procfs_ops
.to_has_execution
= 1;
189 procfs_ops
.to_has_stack
= 1;
190 procfs_ops
.to_has_registers
= 1;
191 procfs_ops
.to_stratum
= process_stratum
;
192 procfs_ops
.to_has_thread_control
= tc_schedlock
;
193 procfs_ops
.to_find_memory_regions
= proc_find_memory_regions
;
194 procfs_ops
.to_make_corefile_notes
= procfs_make_note_section
;
195 procfs_ops
.to_can_use_hw_breakpoint
= procfs_can_use_hw_breakpoint
;
196 procfs_ops
.to_magic
= OPS_MAGIC
;
199 /* =================== END, TARGET_OPS "MODULE" =================== */
204 * Put any typedefs, defines etc. here that are required for
205 * the unification of code that handles different versions of /proc.
208 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
210 enum { READ_WATCHFLAG
= WA_READ
,
211 WRITE_WATCHFLAG
= WA_WRITE
,
212 EXEC_WATCHFLAG
= WA_EXEC
,
213 AFTER_WATCHFLAG
= WA_TRAPAFTER
216 #else /* Irix method for watchpoints */
217 enum { READ_WATCHFLAG
= MA_READ
,
218 WRITE_WATCHFLAG
= MA_WRITE
,
219 EXEC_WATCHFLAG
= MA_EXEC
,
220 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
225 #ifdef HAVE_PR_SIGSET_T
226 typedef pr_sigset_t gdb_sigset_t
;
228 typedef sigset_t gdb_sigset_t
;
232 #ifdef HAVE_PR_SIGACTION64_T
233 typedef pr_sigaction64_t gdb_sigaction_t
;
235 typedef struct sigaction gdb_sigaction_t
;
239 #ifdef HAVE_PR_SIGINFO64_T
240 typedef pr_siginfo64_t gdb_siginfo_t
;
242 typedef struct siginfo gdb_siginfo_t
;
245 /* gdb_premptysysset */
247 #define gdb_premptysysset premptysysset
249 #define gdb_premptysysset premptyset
254 #define gdb_praddsysset praddsysset
256 #define gdb_praddsysset praddset
261 #define gdb_prdelsysset prdelsysset
263 #define gdb_prdelsysset prdelset
266 /* prissyssetmember */
267 #ifdef prissyssetmember
268 #define gdb_pr_issyssetmember prissyssetmember
270 #define gdb_pr_issyssetmember prismember
273 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
274 as intuitively descriptive as it could be, so we'll define
275 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
276 this writing, this feature is only found on AIX5 systems and
277 basically means that the set of syscalls is not fixed. I.e,
278 there's no nice table that one can #include to get all of the
279 syscall numbers. Instead, they're stored in /proc/PID/sysent
280 for each process. We are at least guaranteed that they won't
281 change over the lifetime of the process. But each process could
282 (in theory) have different syscall numbers.
284 #ifdef HAVE_PRSYSENT_T
285 #define DYNAMIC_SYSCALLS
290 /* =================== STRUCT PROCINFO "MODULE" =================== */
292 /* FIXME: this comment will soon be out of date W.R.T. threads. */
294 /* The procinfo struct is a wrapper to hold all the state information
295 concerning a /proc process. There should be exactly one procinfo
296 for each process, and since GDB currently can debug only one
297 process at a time, that means there should be only one procinfo.
298 All of the LWP's of a process can be accessed indirectly thru the
299 single process procinfo.
301 However, against the day when GDB may debug more than one process,
302 this data structure is kept in a list (which for now will hold no
303 more than one member), and many functions will have a pointer to a
304 procinfo as an argument.
306 There will be a separate procinfo structure for use by the (not yet
307 implemented) "info proc" command, so that we can print useful
308 information about any random process without interfering with the
309 inferior's procinfo information. */
312 /* format strings for /proc paths */
313 # ifndef CTL_PROC_NAME_FMT
314 # define MAIN_PROC_NAME_FMT "/proc/%d"
315 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
316 # define AS_PROC_NAME_FMT "/proc/%d/as"
317 # define MAP_PROC_NAME_FMT "/proc/%d/map"
318 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
319 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
321 /* the name of the proc status struct depends on the implementation */
322 typedef pstatus_t gdb_prstatus_t
;
323 typedef lwpstatus_t gdb_lwpstatus_t
;
324 #else /* ! NEW_PROC_API */
325 /* format strings for /proc paths */
326 # ifndef CTL_PROC_NAME_FMT
327 # define MAIN_PROC_NAME_FMT "/proc/%05d"
328 # define CTL_PROC_NAME_FMT "/proc/%05d"
329 # define AS_PROC_NAME_FMT "/proc/%05d"
330 # define MAP_PROC_NAME_FMT "/proc/%05d"
331 # define STATUS_PROC_NAME_FMT "/proc/%05d"
332 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
334 /* the name of the proc status struct depends on the implementation */
335 typedef prstatus_t gdb_prstatus_t
;
336 typedef prstatus_t gdb_lwpstatus_t
;
337 #endif /* NEW_PROC_API */
339 typedef struct procinfo
{
340 struct procinfo
*next
;
341 int pid
; /* Process ID */
342 int tid
; /* Thread/LWP id */
346 int ignore_next_sigstop
;
348 /* The following four fd fields may be identical, or may contain
349 several different fd's, depending on the version of /proc
350 (old ioctl or new read/write). */
352 int ctl_fd
; /* File descriptor for /proc control file */
354 * The next three file descriptors are actually only needed in the
355 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
356 * However, to avoid a bunch of #ifdefs in the code, we will use
357 * them uniformly by (in the case of the ioctl single-file-descriptor
358 * implementation) filling them with copies of the control fd.
360 int status_fd
; /* File descriptor for /proc status file */
361 int as_fd
; /* File descriptor for /proc as file */
363 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
365 fltset_t saved_fltset
; /* Saved traced hardware fault set */
366 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
367 gdb_sigset_t saved_sighold
; /* Saved held signal set */
368 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
369 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
371 gdb_prstatus_t prstatus
; /* Current process status info */
374 gdb_fpregset_t fpregset
; /* Current floating point registers */
377 #ifdef DYNAMIC_SYSCALLS
378 int num_syscalls
; /* Total number of syscalls */
379 char **syscall_names
; /* Syscall number to name map */
382 struct procinfo
*thread_list
;
384 int status_valid
: 1;
386 int fpregs_valid
: 1;
387 int threads_valid
: 1;
390 static char errmsg
[128]; /* shared error msg buffer */
392 /* Function prototypes for procinfo module: */
394 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
395 static procinfo
*find_procinfo (int pid
, int tid
);
396 static procinfo
*create_procinfo (int pid
, int tid
);
397 static void destroy_procinfo (procinfo
* p
);
398 static void do_destroy_procinfo_cleanup (void *);
399 static void dead_procinfo (procinfo
* p
, char *msg
, int killp
);
400 static int open_procinfo_files (procinfo
* p
, int which
);
401 static void close_procinfo_files (procinfo
* p
);
402 static int sysset_t_size (procinfo
*p
);
403 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
404 #ifdef DYNAMIC_SYSCALLS
405 static void load_syscalls (procinfo
*pi
);
406 static void free_syscalls (procinfo
*pi
);
407 static int find_syscall (procinfo
*pi
, char *name
);
408 #endif /* DYNAMIC_SYSCALLS */
410 /* The head of the procinfo list: */
411 static procinfo
* procinfo_list
;
414 * Function: find_procinfo
416 * Search the procinfo list.
418 * Returns: pointer to procinfo, or NULL if not found.
422 find_procinfo (int pid
, int tid
)
426 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
433 /* Don't check threads_valid. If we're updating the
434 thread_list, we want to find whatever threads are already
435 here. This means that in general it is the caller's
436 responsibility to check threads_valid and update before
437 calling find_procinfo, if the caller wants to find a new
440 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
449 * Function: find_procinfo_or_die
451 * Calls find_procinfo, but errors on failure.
455 find_procinfo_or_die (int pid
, int tid
)
457 procinfo
*pi
= find_procinfo (pid
, tid
);
462 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
465 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
470 /* open_with_retry() is a wrapper for open(). The appropriate
471 open() call is attempted; if unsuccessful, it will be retried as
472 many times as needed for the EAGAIN and EINTR conditions.
474 For other conditions, open_with_retry() will retry the open() a
475 limited number of times. In addition, a short sleep is imposed
476 prior to retrying the open(). The reason for this sleep is to give
477 the kernel a chance to catch up and create the file in question in
478 the event that GDB "wins" the race to open a file before the kernel
482 open_with_retry (const char *pathname
, int flags
)
484 int retries_remaining
, status
;
486 retries_remaining
= 2;
490 status
= open (pathname
, flags
);
492 if (status
>= 0 || retries_remaining
== 0)
494 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
505 * Function: open_procinfo_files
507 * Open the file descriptor for the process or LWP.
508 * ifdef NEW_PROC_API, we only open the control file descriptor;
509 * the others are opened lazily as needed.
510 * else (if not NEW_PROC_API), there is only one real
511 * file descriptor, but we keep multiple copies of it so that
512 * the code that uses them does not have to be #ifdef'd.
514 * Return: file descriptor, or zero for failure.
517 enum { FD_CTL
, FD_STATUS
, FD_AS
};
520 open_procinfo_files (procinfo
*pi
, int which
)
523 char tmp
[MAX_PROC_NAME_SIZE
];
528 * This function is getting ALMOST long enough to break up into several.
529 * Here is some rationale:
531 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
532 * There are several file descriptors that may need to be open
533 * for any given process or LWP. The ones we're intereted in are:
534 * - control (ctl) write-only change the state
535 * - status (status) read-only query the state
536 * - address space (as) read/write access memory
537 * - map (map) read-only virtual addr map
538 * Most of these are opened lazily as they are needed.
539 * The pathnames for the 'files' for an LWP look slightly
540 * different from those of a first-class process:
541 * Pathnames for a process (<proc-id>):
542 * /proc/<proc-id>/ctl
543 * /proc/<proc-id>/status
545 * /proc/<proc-id>/map
546 * Pathnames for an LWP (lwp-id):
547 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
548 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
549 * An LWP has no map or address space file descriptor, since
550 * the memory map and address space are shared by all LWPs.
552 * Everyone else (Solaris 2.5, Irix, OSF)
553 * There is only one file descriptor for each process or LWP.
554 * For convenience, we copy the same file descriptor into all
555 * three fields of the procinfo struct (ctl_fd, status_fd, and
556 * as_fd, see NEW_PROC_API above) so that code that uses them
557 * doesn't need any #ifdef's.
562 * Each LWP has an independent file descriptor, but these
563 * are not obtained via the 'open' system call like the rest:
564 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
565 * to the file descriptor of the parent process.
568 * These do not even have their own independent file descriptor.
569 * All operations are carried out on the file descriptor of the
570 * parent process. Therefore we just call open again for each
571 * thread, getting a new handle for the same 'file'.
576 * In this case, there are several different file descriptors that
577 * we might be asked to open. The control file descriptor will be
578 * opened early, but the others will be opened lazily as they are
582 strcpy (tmp
, pi
->pathname
);
583 switch (which
) { /* which file descriptor to open? */
586 strcat (tmp
, "/lwpctl");
588 strcat (tmp
, "/ctl");
589 fd
= open_with_retry (tmp
, O_WRONLY
);
596 return 0; /* there is no 'as' file descriptor for an lwp */
598 fd
= open_with_retry (tmp
, O_RDWR
);
605 strcat (tmp
, "/lwpstatus");
607 strcat (tmp
, "/status");
608 fd
= open_with_retry (tmp
, O_RDONLY
);
614 return 0; /* unknown file descriptor */
616 #else /* not NEW_PROC_API */
618 * In this case, there is only one file descriptor for each procinfo
619 * (ie. each process or LWP). In fact, only the file descriptor for
620 * the process can actually be opened by an 'open' system call.
621 * The ones for the LWPs have to be obtained thru an IOCTL call
622 * on the process's file descriptor.
624 * For convenience, we copy each procinfo's single file descriptor
625 * into all of the fields occupied by the several file descriptors
626 * of the NEW_PROC_API implementation. That way, the code that uses
627 * them can be written without ifdefs.
631 #ifdef PIOCTSTATUS /* OSF */
632 /* Only one FD; just open it. */
633 if ((fd
= open_with_retry (pi
->pathname
, O_RDWR
)) == 0)
635 #else /* Sol 2.5, Irix, other? */
636 if (pi
->tid
== 0) /* Master procinfo for the process */
638 fd
= open_with_retry (pi
->pathname
, O_RDWR
);
642 else /* LWP thread procinfo */
644 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
648 /* Find the procinfo for the entire process. */
649 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
652 /* Now obtain the file descriptor for the LWP. */
653 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
655 #else /* Irix, other? */
656 return 0; /* Don't know how to open threads */
657 #endif /* Sol 2.5 PIOCOPENLWP */
659 #endif /* OSF PIOCTSTATUS */
660 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
661 #endif /* NEW_PROC_API */
663 return 1; /* success */
667 * Function: create_procinfo
669 * Allocate a data structure and link it into the procinfo list.
670 * (First tries to find a pre-existing one (FIXME: why?)
672 * Return: pointer to new procinfo struct.
676 create_procinfo (int pid
, int tid
)
678 procinfo
*pi
, *parent
;
680 if ((pi
= find_procinfo (pid
, tid
)))
681 return pi
; /* Already exists, nothing to do. */
683 /* find parent before doing malloc, to save having to cleanup */
685 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
687 doesn't exist yet? */
689 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
690 memset (pi
, 0, sizeof (procinfo
));
694 #ifdef DYNAMIC_SYSCALLS
698 pi
->saved_entryset
= sysset_t_alloc (pi
);
699 pi
->saved_exitset
= sysset_t_alloc (pi
);
701 /* Chain into list. */
704 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
705 pi
->next
= procinfo_list
;
711 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
713 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
715 pi
->next
= parent
->thread_list
;
716 parent
->thread_list
= pi
;
722 * Function: close_procinfo_files
724 * Close all file descriptors associated with the procinfo
728 close_procinfo_files (procinfo
*pi
)
735 if (pi
->status_fd
> 0)
736 close (pi
->status_fd
);
738 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
742 * Function: destroy_procinfo
744 * Destructor function. Close, unlink and deallocate the object.
748 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
752 /* Step one: unlink the procinfo from its list */
756 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
759 ptr
->next
= pi
->next
;
763 /* Step two: close any open file descriptors */
764 close_procinfo_files (pi
);
766 /* Step three: free the memory. */
767 #ifdef DYNAMIC_SYSCALLS
770 xfree (pi
->saved_entryset
);
771 xfree (pi
->saved_exitset
);
776 destroy_procinfo (procinfo
*pi
)
780 if (pi
->tid
!= 0) /* destroy a thread procinfo */
782 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
783 destroy_one_procinfo (&tmp
->thread_list
, pi
);
785 else /* destroy a process procinfo and all its threads */
787 /* First destroy the children, if any; */
788 while (pi
->thread_list
!= NULL
)
789 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
790 /* Then destroy the parent. Genocide!!! */
791 destroy_one_procinfo (&procinfo_list
, pi
);
796 do_destroy_procinfo_cleanup (void *pi
)
798 destroy_procinfo (pi
);
801 enum { NOKILL
, KILL
};
804 * Function: dead_procinfo
806 * To be called on a non_recoverable error for a procinfo.
807 * Prints error messages, optionally sends a SIGKILL to the process,
808 * then destroys the data structure.
812 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
818 print_sys_errmsg (pi
->pathname
, errno
);
822 sprintf (procfile
, "process %d", pi
->pid
);
823 print_sys_errmsg (procfile
, errno
);
826 kill (pi
->pid
, SIGKILL
);
828 destroy_procinfo (pi
);
833 * Function: sysset_t_size
835 * Returns the (complete) size of a sysset_t struct. Normally, this
836 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
837 * size of sysset_t isn't known until runtime.
841 sysset_t_size (procinfo
* pi
)
843 #ifndef DYNAMIC_SYSCALLS
844 return sizeof (sysset_t
);
846 return sizeof (sysset_t
) - sizeof (uint64_t)
847 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
848 / (8 * sizeof (uint64_t)));
852 /* Function: sysset_t_alloc
854 Allocate and (partially) initialize a sysset_t struct. */
857 sysset_t_alloc (procinfo
* pi
)
860 int size
= sysset_t_size (pi
);
861 ret
= xmalloc (size
);
862 #ifdef DYNAMIC_SYSCALLS
863 ret
->pr_size
= (pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
864 / (8 * sizeof (uint64_t));
869 #ifdef DYNAMIC_SYSCALLS
871 /* Function: load_syscalls
873 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
874 pi->num_syscalls with the number of syscalls and pi->syscall_names
875 with the names. (Certain numbers may be skipped in which case the
876 names for these numbers will be left as NULL.) */
878 #define MAX_SYSCALL_NAME_LENGTH 256
879 #define MAX_SYSCALLS 65536
882 load_syscalls (procinfo
*pi
)
884 char pathname
[MAX_PROC_NAME_SIZE
];
887 prsyscall_t
*syscalls
;
888 int i
, size
, maxcall
;
890 pi
->num_syscalls
= 0;
891 pi
->syscall_names
= 0;
893 /* Open the file descriptor for the sysent file */
894 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
895 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
898 error ("load_syscalls: Can't open /proc/%d/sysent", pi
->pid
);
901 size
= sizeof header
- sizeof (prsyscall_t
);
902 if (read (sysent_fd
, &header
, size
) != size
)
904 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
907 if (header
.pr_nsyscalls
== 0)
909 error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi
->pid
);
912 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
913 syscalls
= xmalloc (size
);
915 if (read (sysent_fd
, syscalls
, size
) != size
)
918 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
921 /* Find maximum syscall number. This may not be the same as
922 pr_nsyscalls since that value refers to the number of entries
923 in the table. (Also, the docs indicate that some system
924 call numbers may be skipped.) */
926 maxcall
= syscalls
[0].pr_number
;
928 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
929 if (syscalls
[i
].pr_number
> maxcall
930 && syscalls
[i
].pr_nameoff
> 0
931 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
932 maxcall
= syscalls
[i
].pr_number
;
934 pi
->num_syscalls
= maxcall
+1;
935 pi
->syscall_names
= xmalloc (pi
->num_syscalls
* sizeof (char *));
937 for (i
= 0; i
< pi
->num_syscalls
; i
++)
938 pi
->syscall_names
[i
] = NULL
;
940 /* Read the syscall names in */
941 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
943 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
947 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
948 || syscalls
[i
].pr_number
< 0
949 || syscalls
[i
].pr_nameoff
<= 0
950 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
951 != (off_t
) syscalls
[i
].pr_nameoff
))
954 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
958 callnum
= syscalls
[i
].pr_number
;
960 if (pi
->syscall_names
[callnum
] != NULL
)
962 /* FIXME: Generate warning */
966 namebuf
[nread
-1] = '\0';
967 size
= strlen (namebuf
) + 1;
968 pi
->syscall_names
[callnum
] = xmalloc (size
);
969 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
970 pi
->syscall_names
[callnum
][size
-1] = '\0';
977 /* Function: free_syscalls
979 Free the space allocated for the syscall names from the procinfo
983 free_syscalls (procinfo
*pi
)
985 if (pi
->syscall_names
)
989 for (i
= 0; i
< pi
->num_syscalls
; i
++)
990 if (pi
->syscall_names
[i
] != NULL
)
991 xfree (pi
->syscall_names
[i
]);
993 xfree (pi
->syscall_names
);
994 pi
->syscall_names
= 0;
998 /* Function: find_syscall
1000 Given a name, look up (and return) the corresponding syscall number.
1001 If no match is found, return -1. */
1004 find_syscall (procinfo
*pi
, char *name
)
1007 for (i
= 0; i
< pi
->num_syscalls
; i
++)
1009 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
1016 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1018 /* =================== /proc "MODULE" =================== */
1021 * This "module" is the interface layer between the /proc system API
1022 * and the gdb target vector functions. This layer consists of
1023 * access functions that encapsulate each of the basic operations
1024 * that we need to use from the /proc API.
1026 * The main motivation for this layer is to hide the fact that
1027 * there are two very different implementations of the /proc API.
1028 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1029 * functions, we do our best to hide them all in here.
1032 int proc_get_status (procinfo
* pi
);
1033 long proc_flags (procinfo
* pi
);
1034 int proc_why (procinfo
* pi
);
1035 int proc_what (procinfo
* pi
);
1036 int proc_set_run_on_last_close (procinfo
* pi
);
1037 int proc_unset_run_on_last_close (procinfo
* pi
);
1038 int proc_set_inherit_on_fork (procinfo
* pi
);
1039 int proc_unset_inherit_on_fork (procinfo
* pi
);
1040 int proc_set_async (procinfo
* pi
);
1041 int proc_unset_async (procinfo
* pi
);
1042 int proc_stop_process (procinfo
* pi
);
1043 int proc_trace_signal (procinfo
* pi
, int signo
);
1044 int proc_ignore_signal (procinfo
* pi
, int signo
);
1045 int proc_clear_current_fault (procinfo
* pi
);
1046 int proc_set_current_signal (procinfo
* pi
, int signo
);
1047 int proc_clear_current_signal (procinfo
* pi
);
1048 int proc_set_gregs (procinfo
* pi
);
1049 int proc_set_fpregs (procinfo
* pi
);
1050 int proc_wait_for_stop (procinfo
* pi
);
1051 int proc_run_process (procinfo
* pi
, int step
, int signo
);
1052 int proc_kill (procinfo
* pi
, int signo
);
1053 int proc_parent_pid (procinfo
* pi
);
1054 int proc_get_nthreads (procinfo
* pi
);
1055 int proc_get_current_thread (procinfo
* pi
);
1056 int proc_set_held_signals (procinfo
* pi
, gdb_sigset_t
* sighold
);
1057 int proc_set_traced_sysexit (procinfo
* pi
, sysset_t
* sysset
);
1058 int proc_set_traced_sysentry (procinfo
* pi
, sysset_t
* sysset
);
1059 int proc_set_traced_faults (procinfo
* pi
, fltset_t
* fltset
);
1060 int proc_set_traced_signals (procinfo
* pi
, gdb_sigset_t
* sigset
);
1062 int proc_update_threads (procinfo
* pi
);
1063 int proc_iterate_over_threads (procinfo
* pi
,
1064 int (*func
) (procinfo
*, procinfo
*, void *),
1067 gdb_gregset_t
*proc_get_gregs (procinfo
* pi
);
1068 gdb_fpregset_t
*proc_get_fpregs (procinfo
* pi
);
1069 sysset_t
*proc_get_traced_sysexit (procinfo
* pi
, sysset_t
* save
);
1070 sysset_t
*proc_get_traced_sysentry (procinfo
* pi
, sysset_t
* save
);
1071 fltset_t
*proc_get_traced_faults (procinfo
* pi
, fltset_t
* save
);
1072 gdb_sigset_t
*proc_get_traced_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1073 gdb_sigset_t
*proc_get_held_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1074 gdb_sigset_t
*proc_get_pending_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1075 gdb_sigaction_t
*proc_get_signal_actions (procinfo
* pi
, gdb_sigaction_t
*save
);
1077 void proc_warn (procinfo
* pi
, char *func
, int line
);
1078 void proc_error (procinfo
* pi
, char *func
, int line
);
1081 proc_warn (procinfo
*pi
, char *func
, int line
)
1083 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1084 print_sys_errmsg (errmsg
, errno
);
1088 proc_error (procinfo
*pi
, char *func
, int line
)
1090 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1091 perror_with_name (errmsg
);
1095 * Function: proc_get_status
1097 * Updates the status struct in the procinfo.
1098 * There is a 'valid' flag, to let other functions know when
1099 * this function needs to be called (so the status is only
1100 * read when it is needed). The status file descriptor is
1101 * also only opened when it is needed.
1103 * Return: non-zero for success, zero for failure.
1107 proc_get_status (procinfo
*pi
)
1109 /* Status file descriptor is opened "lazily" */
1110 if (pi
->status_fd
== 0 &&
1111 open_procinfo_files (pi
, FD_STATUS
) == 0)
1113 pi
->status_valid
= 0;
1118 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1119 pi
->status_valid
= 0; /* fail */
1122 /* Sigh... I have to read a different data structure,
1123 depending on whether this is a main process or an LWP. */
1125 pi
->status_valid
= (read (pi
->status_fd
,
1126 (char *) &pi
->prstatus
.pr_lwp
,
1127 sizeof (lwpstatus_t
))
1128 == sizeof (lwpstatus_t
));
1131 pi
->status_valid
= (read (pi
->status_fd
,
1132 (char *) &pi
->prstatus
,
1133 sizeof (gdb_prstatus_t
))
1134 == sizeof (gdb_prstatus_t
));
1135 #if 0 /*def UNIXWARE*/
1136 if (pi
->status_valid
&&
1137 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
1138 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
1139 /* Unixware peculiarity -- read the damn thing again! */
1140 pi
->status_valid
= (read (pi
->status_fd
,
1141 (char *) &pi
->prstatus
,
1142 sizeof (gdb_prstatus_t
))
1143 == sizeof (gdb_prstatus_t
));
1144 #endif /* UNIXWARE */
1147 #else /* ioctl method */
1148 #ifdef PIOCTSTATUS /* osf */
1149 if (pi
->tid
== 0) /* main process */
1151 /* Just read the danged status. Now isn't that simple? */
1153 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1160 tid_t pr_error_thread
;
1161 struct prstatus status
;
1164 thread_status
.pr_count
= 1;
1165 thread_status
.status
.pr_tid
= pi
->tid
;
1166 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1169 memcpy (&pi
->prstatus
, &thread_status
.status
,
1170 sizeof (pi
->prstatus
));
1171 pi
->status_valid
= 1;
1175 /* Just read the danged status. Now isn't that simple? */
1176 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1180 if (pi
->status_valid
)
1182 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1185 proc_get_current_thread (pi
));
1188 /* The status struct includes general regs, so mark them valid too */
1189 pi
->gregs_valid
= pi
->status_valid
;
1191 /* In the read/write multiple-fd model,
1192 the status struct includes the fp regs too, so mark them valid too */
1193 pi
->fpregs_valid
= pi
->status_valid
;
1195 return pi
->status_valid
; /* True if success, false if failure. */
1199 * Function: proc_flags
1201 * returns the process flags (pr_flags field).
1205 proc_flags (procinfo
*pi
)
1207 if (!pi
->status_valid
)
1208 if (!proc_get_status (pi
))
1209 return 0; /* FIXME: not a good failure value (but what is?) */
1213 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1214 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1215 The two sets of flags don't overlap. */
1216 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
1218 return pi
->prstatus
.pr_lwp
.pr_flags
;
1221 return pi
->prstatus
.pr_flags
;
1226 * Function: proc_why
1228 * returns the pr_why field (why the process stopped).
1232 proc_why (procinfo
*pi
)
1234 if (!pi
->status_valid
)
1235 if (!proc_get_status (pi
))
1236 return 0; /* FIXME: not a good failure value (but what is?) */
1239 return pi
->prstatus
.pr_lwp
.pr_why
;
1241 return pi
->prstatus
.pr_why
;
1246 * Function: proc_what
1248 * returns the pr_what field (details of why the process stopped).
1252 proc_what (procinfo
*pi
)
1254 if (!pi
->status_valid
)
1255 if (!proc_get_status (pi
))
1256 return 0; /* FIXME: not a good failure value (but what is?) */
1259 return pi
->prstatus
.pr_lwp
.pr_what
;
1261 return pi
->prstatus
.pr_what
;
1265 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1267 * Function: proc_nsysarg
1269 * returns the pr_nsysarg field (number of args to the current syscall).
1273 proc_nsysarg (procinfo
*pi
)
1275 if (!pi
->status_valid
)
1276 if (!proc_get_status (pi
))
1280 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1282 return pi
->prstatus
.pr_nsysarg
;
1287 * Function: proc_sysargs
1289 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1293 proc_sysargs (procinfo
*pi
)
1295 if (!pi
->status_valid
)
1296 if (!proc_get_status (pi
))
1300 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1302 return (long *) &pi
->prstatus
.pr_sysarg
;
1307 * Function: proc_syscall
1309 * returns the pr_syscall field (id of current syscall if we are in one).
1313 proc_syscall (procinfo
*pi
)
1315 if (!pi
->status_valid
)
1316 if (!proc_get_status (pi
))
1320 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1322 return pi
->prstatus
.pr_syscall
;
1325 #endif /* PIOCSSPCACT */
1328 * Function: proc_cursig:
1330 * returns the pr_cursig field (current signal).
1334 proc_cursig (struct procinfo
*pi
)
1336 if (!pi
->status_valid
)
1337 if (!proc_get_status (pi
))
1338 return 0; /* FIXME: not a good failure value (but what is?) */
1341 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1343 return pi
->prstatus
.pr_cursig
;
1348 * Function: proc_modify_flag
1350 * === I appologize for the messiness of this function.
1351 * === This is an area where the different versions of
1352 * === /proc are more inconsistent than usual. MVS
1354 * Set or reset any of the following process flags:
1355 * PR_FORK -- forked child will inherit trace flags
1356 * PR_RLC -- traced process runs when last /proc file closed.
1357 * PR_KLC -- traced process is killed when last /proc file closed.
1358 * PR_ASYNC -- LWP's get to run/stop independently.
1360 * There are three methods for doing this function:
1361 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1363 * 2) Middle: PIOCSET/PIOCRESET
1365 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1368 * Note: Irix does not define PR_ASYNC.
1369 * Note: OSF does not define PR_KLC.
1370 * Note: OSF is the only one that can ONLY use the oldest method.
1373 * pi -- the procinfo
1374 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1375 * mode -- 1 for set, 0 for reset.
1377 * Returns non-zero for success, zero for failure.
1380 enum { FLAG_RESET
, FLAG_SET
};
1383 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1385 long win
= 0; /* default to fail */
1388 * These operations affect the process as a whole, and applying
1389 * them to an individual LWP has the same meaning as applying them
1390 * to the main process. Therefore, if we're ever called with a
1391 * pointer to an LWP's procinfo, let's substitute the process's
1392 * procinfo and avoid opening the LWP's file descriptor
1397 pi
= find_procinfo_or_die (pi
->pid
, 0);
1399 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1400 /* First normalize the PCUNSET/PCRESET command opcode
1401 (which for no obvious reason has a different definition
1402 from one operating system to the next...) */
1404 #define GDBRESET PCUNSET
1407 #define GDBRESET PCRESET
1411 procfs_ctl_t arg
[2];
1413 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1415 else /* Reset the flag */
1419 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1422 #ifdef PIOCSET /* Irix/Sol5 method */
1423 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1425 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1427 else /* Reset the flag */
1429 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1433 #ifdef PIOCSRLC /* Oldest method: OSF */
1436 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1438 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1440 else /* Clear run-on-last-close */
1442 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1446 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1448 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1450 else /* Clear inherit-on-fork */
1452 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1456 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1463 /* The above operation renders the procinfo's cached pstatus obsolete. */
1464 pi
->status_valid
= 0;
1467 warning ("procfs: modify_flag failed to turn %s %s",
1468 flag
== PR_FORK
? "PR_FORK" :
1469 flag
== PR_RLC
? "PR_RLC" :
1471 flag
== PR_ASYNC
? "PR_ASYNC" :
1474 flag
== PR_KLC
? "PR_KLC" :
1477 mode
== FLAG_RESET
? "off" : "on");
1483 * Function: proc_set_run_on_last_close
1485 * Set the run_on_last_close flag.
1486 * Process with all threads will become runnable
1487 * when debugger closes all /proc fds.
1489 * Returns non-zero for success, zero for failure.
1493 proc_set_run_on_last_close (procinfo
*pi
)
1495 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1499 * Function: proc_unset_run_on_last_close
1501 * Reset the run_on_last_close flag.
1502 * Process will NOT become runnable
1503 * when debugger closes its file handles.
1505 * Returns non-zero for success, zero for failure.
1509 proc_unset_run_on_last_close (procinfo
*pi
)
1511 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1516 * Function: proc_set_kill_on_last_close
1518 * Set the kill_on_last_close flag.
1519 * Process with all threads will be killed when debugger
1520 * closes all /proc fds (or debugger exits or dies).
1522 * Returns non-zero for success, zero for failure.
1526 proc_set_kill_on_last_close (procinfo
*pi
)
1528 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1532 * Function: proc_unset_kill_on_last_close
1534 * Reset the kill_on_last_close flag.
1535 * Process will NOT be killed when debugger
1536 * closes its file handles (or exits or dies).
1538 * Returns non-zero for success, zero for failure.
1542 proc_unset_kill_on_last_close (procinfo
*pi
)
1544 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1549 * Function: proc_set_inherit_on_fork
1551 * Set inherit_on_fork flag.
1552 * If the process forks a child while we are registered for events
1553 * in the parent, then we will also recieve events from the child.
1555 * Returns non-zero for success, zero for failure.
1559 proc_set_inherit_on_fork (procinfo
*pi
)
1561 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1565 * Function: proc_unset_inherit_on_fork
1567 * Reset inherit_on_fork flag.
1568 * If the process forks a child while we are registered for events
1569 * in the parent, then we will NOT recieve events from the child.
1571 * Returns non-zero for success, zero for failure.
1575 proc_unset_inherit_on_fork (procinfo
*pi
)
1577 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1582 * Function: proc_set_async
1584 * Set PR_ASYNC flag.
1585 * If one LWP stops because of a debug event (signal etc.),
1586 * the remaining LWPs will continue to run.
1588 * Returns non-zero for success, zero for failure.
1592 proc_set_async (procinfo
*pi
)
1594 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1598 * Function: proc_unset_async
1600 * Reset PR_ASYNC flag.
1601 * If one LWP stops because of a debug event (signal etc.),
1602 * then all other LWPs will stop as well.
1604 * Returns non-zero for success, zero for failure.
1608 proc_unset_async (procinfo
*pi
)
1610 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1612 #endif /* PR_ASYNC */
1615 * Function: proc_stop_process
1617 * Request the process/LWP to stop. Does not wait.
1618 * Returns non-zero for success, zero for failure.
1622 proc_stop_process (procinfo
*pi
)
1627 * We might conceivably apply this operation to an LWP, and
1628 * the LWP's ctl file descriptor might not be open.
1631 if (pi
->ctl_fd
== 0 &&
1632 open_procinfo_files (pi
, FD_CTL
) == 0)
1637 procfs_ctl_t cmd
= PCSTOP
;
1638 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1639 #else /* ioctl method */
1640 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1641 /* Note: the call also reads the prstatus. */
1644 pi
->status_valid
= 1;
1645 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1648 proc_get_current_thread (pi
));
1657 * Function: proc_wait_for_stop
1659 * Wait for the process or LWP to stop (block until it does).
1660 * Returns non-zero for success, zero for failure.
1664 proc_wait_for_stop (procinfo
*pi
)
1669 * We should never have to apply this operation to any procinfo
1670 * except the one for the main process. If that ever changes
1671 * for any reason, then take out the following clause and
1672 * replace it with one that makes sure the ctl_fd is open.
1676 pi
= find_procinfo_or_die (pi
->pid
, 0);
1680 procfs_ctl_t cmd
= PCWSTOP
;
1681 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1682 /* We been runnin' and we stopped -- need to update status. */
1683 pi
->status_valid
= 0;
1685 #else /* ioctl method */
1686 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1687 /* Above call also refreshes the prstatus. */
1690 pi
->status_valid
= 1;
1691 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1694 proc_get_current_thread (pi
));
1702 * Function: proc_run_process
1704 * Make the process or LWP runnable.
1705 * Options (not all are implemented):
1707 * - clear current fault
1708 * - clear current signal
1709 * - abort the current system call
1710 * - stop as soon as finished with system call
1711 * - (ioctl): set traced signal set
1712 * - (ioctl): set held signal set
1713 * - (ioctl): set traced fault set
1714 * - (ioctl): set start pc (vaddr)
1715 * Always clear the current fault.
1716 * Clear the current signal if 'signo' is zero.
1719 * pi the process or LWP to operate on.
1720 * step if true, set the process or LWP to trap after one instr.
1721 * signo if zero, clear the current signal if any.
1722 * if non-zero, set the current signal to this one.
1724 * Returns non-zero for success, zero for failure.
1728 proc_run_process (procinfo
*pi
, int step
, int signo
)
1734 * We will probably have to apply this operation to individual threads,
1735 * so make sure the control file descriptor is open.
1738 if (pi
->ctl_fd
== 0 &&
1739 open_procinfo_files (pi
, FD_CTL
) == 0)
1744 runflags
= PRCFAULT
; /* always clear current fault */
1749 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1750 proc_set_current_signal (pi
, signo
);
1754 procfs_ctl_t cmd
[2];
1758 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1760 #else /* ioctl method */
1764 memset (&prrun
, 0, sizeof (prrun
));
1765 prrun
.pr_flags
= runflags
;
1766 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1774 * Function: proc_set_traced_signals
1776 * Register to trace signals in the process or LWP.
1777 * Returns non-zero for success, zero for failure.
1781 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1786 * We should never have to apply this operation to any procinfo
1787 * except the one for the main process. If that ever changes
1788 * for any reason, then take out the following clause and
1789 * replace it with one that makes sure the ctl_fd is open.
1793 pi
= find_procinfo_or_die (pi
->pid
, 0);
1799 /* Use char array to avoid alignment issues. */
1800 char sigset
[sizeof (gdb_sigset_t
)];
1804 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1806 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1808 #else /* ioctl method */
1809 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1811 /* The above operation renders the procinfo's cached pstatus obsolete. */
1812 pi
->status_valid
= 0;
1815 warning ("procfs: set_traced_signals failed");
1820 * Function: proc_set_traced_faults
1822 * Register to trace hardware faults in the process or LWP.
1823 * Returns non-zero for success, zero for failure.
1827 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1832 * We should never have to apply this operation to any procinfo
1833 * except the one for the main process. If that ever changes
1834 * for any reason, then take out the following clause and
1835 * replace it with one that makes sure the ctl_fd is open.
1839 pi
= find_procinfo_or_die (pi
->pid
, 0);
1845 /* Use char array to avoid alignment issues. */
1846 char fltset
[sizeof (fltset_t
)];
1850 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1852 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1854 #else /* ioctl method */
1855 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1857 /* The above operation renders the procinfo's cached pstatus obsolete. */
1858 pi
->status_valid
= 0;
1864 * Function: proc_set_traced_sysentry
1866 * Register to trace entry to system calls in the process or LWP.
1867 * Returns non-zero for success, zero for failure.
1871 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1876 * We should never have to apply this operation to any procinfo
1877 * except the one for the main process. If that ever changes
1878 * for any reason, then take out the following clause and
1879 * replace it with one that makes sure the ctl_fd is open.
1883 pi
= find_procinfo_or_die (pi
->pid
, 0);
1887 struct gdb_proc_ctl_pcsentry
{
1889 /* Use char array to avoid alignment issues. */
1890 char sysset
[sizeof (sysset_t
)];
1892 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1894 + sysset_t_size (pi
);
1896 argp
= xmalloc (argp_size
);
1898 argp
->cmd
= PCSENTRY
;
1899 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1901 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1904 #else /* ioctl method */
1905 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1907 /* The above operation renders the procinfo's cached pstatus obsolete. */
1908 pi
->status_valid
= 0;
1914 * Function: proc_set_traced_sysexit
1916 * Register to trace exit from system calls in the process or LWP.
1917 * Returns non-zero for success, zero for failure.
1921 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1926 * We should never have to apply this operation to any procinfo
1927 * except the one for the main process. If that ever changes
1928 * for any reason, then take out the following clause and
1929 * replace it with one that makes sure the ctl_fd is open.
1933 pi
= find_procinfo_or_die (pi
->pid
, 0);
1937 struct gdb_proc_ctl_pcsexit
{
1939 /* Use char array to avoid alignment issues. */
1940 char sysset
[sizeof (sysset_t
)];
1942 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1944 + sysset_t_size (pi
);
1946 argp
= xmalloc (argp_size
);
1948 argp
->cmd
= PCSEXIT
;
1949 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1951 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1954 #else /* ioctl method */
1955 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1957 /* The above operation renders the procinfo's cached pstatus obsolete. */
1958 pi
->status_valid
= 0;
1964 * Function: proc_set_held_signals
1966 * Specify the set of blocked / held signals in the process or LWP.
1967 * Returns non-zero for success, zero for failure.
1971 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1976 * We should never have to apply this operation to any procinfo
1977 * except the one for the main process. If that ever changes
1978 * for any reason, then take out the following clause and
1979 * replace it with one that makes sure the ctl_fd is open.
1983 pi
= find_procinfo_or_die (pi
->pid
, 0);
1989 /* Use char array to avoid alignment issues. */
1990 char hold
[sizeof (gdb_sigset_t
)];
1994 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1995 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1998 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
2000 /* The above operation renders the procinfo's cached pstatus obsolete. */
2001 pi
->status_valid
= 0;
2007 * Function: proc_get_pending_signals
2009 * returns the set of signals that are pending in the process or LWP.
2010 * Will also copy the sigset if 'save' is non-zero.
2014 proc_get_pending_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2016 gdb_sigset_t
*ret
= NULL
;
2019 * We should never have to apply this operation to any procinfo
2020 * except the one for the main process. If that ever changes
2021 * for any reason, then take out the following clause and
2022 * replace it with one that makes sure the ctl_fd is open.
2026 pi
= find_procinfo_or_die (pi
->pid
, 0);
2028 if (!pi
->status_valid
)
2029 if (!proc_get_status (pi
))
2033 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
2035 ret
= &pi
->prstatus
.pr_sigpend
;
2038 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2044 * Function: proc_get_signal_actions
2046 * returns the set of signal actions.
2047 * Will also copy the sigactionset if 'save' is non-zero.
2051 proc_get_signal_actions (procinfo
*pi
, gdb_sigaction_t
*save
)
2053 gdb_sigaction_t
*ret
= NULL
;
2056 * We should never have to apply this operation to any procinfo
2057 * except the one for the main process. If that ever changes
2058 * for any reason, then take out the following clause and
2059 * replace it with one that makes sure the ctl_fd is open.
2063 pi
= find_procinfo_or_die (pi
->pid
, 0);
2065 if (!pi
->status_valid
)
2066 if (!proc_get_status (pi
))
2070 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
2072 ret
= &pi
->prstatus
.pr_action
;
2075 memcpy (save
, ret
, sizeof (gdb_sigaction_t
));
2081 * Function: proc_get_held_signals
2083 * returns the set of signals that are held / blocked.
2084 * Will also copy the sigset if 'save' is non-zero.
2088 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2090 gdb_sigset_t
*ret
= NULL
;
2093 * We should never have to apply this operation to any procinfo
2094 * except the one for the main process. If that ever changes
2095 * for any reason, then take out the following clause and
2096 * replace it with one that makes sure the ctl_fd is open.
2100 pi
= find_procinfo_or_die (pi
->pid
, 0);
2103 if (!pi
->status_valid
)
2104 if (!proc_get_status (pi
))
2108 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
2110 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
2111 #endif /* UNIXWARE */
2112 #else /* not NEW_PROC_API */
2114 static gdb_sigset_t sigheld
;
2116 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
2119 #endif /* NEW_PROC_API */
2121 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2127 * Function: proc_get_traced_signals
2129 * returns the set of signals that are traced / debugged.
2130 * Will also copy the sigset if 'save' is non-zero.
2134 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2136 gdb_sigset_t
*ret
= NULL
;
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);
2149 if (!pi
->status_valid
)
2150 if (!proc_get_status (pi
))
2153 ret
= &pi
->prstatus
.pr_sigtrace
;
2156 static gdb_sigset_t sigtrace
;
2158 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
2163 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2169 * Function: proc_trace_signal
2171 * Add 'signo' to the set of signals that are traced.
2172 * Returns non-zero for success, zero for failure.
2176 proc_trace_signal (procinfo
*pi
, int signo
)
2181 * We should never have to apply this operation to any procinfo
2182 * except the one for the main process. If that ever changes
2183 * for any reason, then take out the following clause and
2184 * replace it with one that makes sure the ctl_fd is open.
2188 pi
= find_procinfo_or_die (pi
->pid
, 0);
2192 if (proc_get_traced_signals (pi
, &temp
))
2194 praddset (&temp
, signo
);
2195 return proc_set_traced_signals (pi
, &temp
);
2199 return 0; /* failure */
2203 * Function: proc_ignore_signal
2205 * Remove 'signo' from the set of signals that are traced.
2206 * Returns non-zero for success, zero for failure.
2210 proc_ignore_signal (procinfo
*pi
, int signo
)
2215 * We should never have to apply this operation to any procinfo
2216 * except the one for the main process. If that ever changes
2217 * for any reason, then take out the following clause and
2218 * replace it with one that makes sure the ctl_fd is open.
2222 pi
= find_procinfo_or_die (pi
->pid
, 0);
2226 if (proc_get_traced_signals (pi
, &temp
))
2228 prdelset (&temp
, signo
);
2229 return proc_set_traced_signals (pi
, &temp
);
2233 return 0; /* failure */
2237 * Function: proc_get_traced_faults
2239 * returns the set of hardware faults that are traced /debugged.
2240 * Will also copy the faultset if 'save' is non-zero.
2244 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
2246 fltset_t
*ret
= NULL
;
2249 * We should never have to apply this operation to any procinfo
2250 * except the one for the main process. If that ever changes
2251 * for any reason, then take out the following clause and
2252 * replace it with one that makes sure the ctl_fd is open.
2256 pi
= find_procinfo_or_die (pi
->pid
, 0);
2259 if (!pi
->status_valid
)
2260 if (!proc_get_status (pi
))
2263 ret
= &pi
->prstatus
.pr_flttrace
;
2266 static fltset_t flttrace
;
2268 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2273 memcpy (save
, ret
, sizeof (fltset_t
));
2279 * Function: proc_get_traced_sysentry
2281 * returns the set of syscalls that are traced /debugged on entry.
2282 * Will also copy the syscall set if 'save' is non-zero.
2286 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
2288 sysset_t
*ret
= NULL
;
2291 * We should never have to apply this operation to any procinfo
2292 * except the one for the main process. If that ever changes
2293 * for any reason, then take out the following clause and
2294 * replace it with one that makes sure the ctl_fd is open.
2298 pi
= find_procinfo_or_die (pi
->pid
, 0);
2301 if (!pi
->status_valid
)
2302 if (!proc_get_status (pi
))
2305 #ifndef DYNAMIC_SYSCALLS
2306 ret
= &pi
->prstatus
.pr_sysentry
;
2307 #else /* DYNAMIC_SYSCALLS */
2309 static sysset_t
*sysentry
;
2313 sysentry
= sysset_t_alloc (pi
);
2315 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2317 if (pi
->prstatus
.pr_sysentry_offset
== 0)
2319 gdb_premptysysset (sysentry
);
2325 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
2327 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
2329 size
= sysset_t_size (pi
);
2330 gdb_premptysysset (sysentry
);
2331 rsize
= read (pi
->status_fd
, sysentry
, size
);
2336 #endif /* DYNAMIC_SYSCALLS */
2337 #else /* !NEW_PROC_API */
2339 static sysset_t sysentry
;
2341 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2344 #endif /* NEW_PROC_API */
2346 memcpy (save
, ret
, sysset_t_size (pi
));
2352 * Function: proc_get_traced_sysexit
2354 * returns the set of syscalls that are traced /debugged on exit.
2355 * Will also copy the syscall set if 'save' is non-zero.
2359 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
2361 sysset_t
* ret
= NULL
;
2364 * We should never have to apply this operation to any procinfo
2365 * except the one for the main process. If that ever changes
2366 * for any reason, then take out the following clause and
2367 * replace it with one that makes sure the ctl_fd is open.
2371 pi
= find_procinfo_or_die (pi
->pid
, 0);
2374 if (!pi
->status_valid
)
2375 if (!proc_get_status (pi
))
2378 #ifndef DYNAMIC_SYSCALLS
2379 ret
= &pi
->prstatus
.pr_sysexit
;
2380 #else /* DYNAMIC_SYSCALLS */
2382 static sysset_t
*sysexit
;
2386 sysexit
= sysset_t_alloc (pi
);
2388 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2390 if (pi
->prstatus
.pr_sysexit_offset
== 0)
2392 gdb_premptysysset (sysexit
);
2398 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
, SEEK_SET
)
2399 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2401 size
= sysset_t_size (pi
);
2402 gdb_premptysysset (sysexit
);
2403 rsize
= read (pi
->status_fd
, sysexit
, size
);
2408 #endif /* DYNAMIC_SYSCALLS */
2411 static sysset_t sysexit
;
2413 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2418 memcpy (save
, ret
, sysset_t_size (pi
));
2424 * Function: proc_clear_current_fault
2426 * The current fault (if any) is cleared; the associated signal
2427 * will not be sent to the process or LWP when it resumes.
2428 * Returns non-zero for success, zero for failure.
2432 proc_clear_current_fault (procinfo
*pi
)
2437 * We should never have to apply this operation to any procinfo
2438 * except the one for the main process. If that ever changes
2439 * for any reason, then take out the following clause and
2440 * replace it with one that makes sure the ctl_fd is open.
2444 pi
= find_procinfo_or_die (pi
->pid
, 0);
2448 procfs_ctl_t cmd
= PCCFAULT
;
2449 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2452 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2459 * Function: proc_set_current_signal
2461 * Set the "current signal" that will be delivered next to the process.
2462 * NOTE: semantics are different from those of KILL.
2463 * This signal will be delivered to the process or LWP
2464 * immediately when it is resumed (even if the signal is held/blocked);
2465 * it will NOT immediately cause another event of interest, and will NOT
2466 * first trap back to the debugger.
2468 * Returns non-zero for success, zero for failure.
2472 proc_set_current_signal (procinfo
*pi
, int signo
)
2477 /* Use char array to avoid alignment issues. */
2478 char sinfo
[sizeof (gdb_siginfo_t
)];
2480 gdb_siginfo_t
*mysinfo
;
2483 * We should never have to apply this operation to any procinfo
2484 * except the one for the main process. If that ever changes
2485 * for any reason, then take out the following clause and
2486 * replace it with one that makes sure the ctl_fd is open.
2490 pi
= find_procinfo_or_die (pi
->pid
, 0);
2492 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2493 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2494 * receives a PIOCSSIG with a signal identical to the current signal,
2495 * it messes up the current signal. Work around the kernel bug.
2498 signo
== proc_cursig (pi
))
2499 return 1; /* I assume this is a success? */
2502 /* The pointer is just a type alias. */
2503 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2504 mysinfo
->si_signo
= signo
;
2505 mysinfo
->si_code
= 0;
2506 mysinfo
->si_pid
= getpid (); /* ?why? */
2507 mysinfo
->si_uid
= getuid (); /* ?why? */
2511 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2513 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2520 * Function: proc_clear_current_signal
2522 * The current signal (if any) is cleared, and
2523 * is not sent to the process or LWP when it resumes.
2524 * Returns non-zero for success, zero for failure.
2528 proc_clear_current_signal (procinfo
*pi
)
2533 * We should never have to apply this operation to any procinfo
2534 * except the one for the main process. If that ever changes
2535 * for any reason, then take out the following clause and
2536 * replace it with one that makes sure the ctl_fd is open.
2540 pi
= find_procinfo_or_die (pi
->pid
, 0);
2546 /* Use char array to avoid alignment issues. */
2547 char sinfo
[sizeof (gdb_siginfo_t
)];
2549 gdb_siginfo_t
*mysinfo
;
2552 /* The pointer is just a type alias. */
2553 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2554 mysinfo
->si_signo
= 0;
2555 mysinfo
->si_code
= 0;
2556 mysinfo
->si_errno
= 0;
2557 mysinfo
->si_pid
= getpid (); /* ?why? */
2558 mysinfo
->si_uid
= getuid (); /* ?why? */
2560 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2563 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2570 * Function: proc_get_gregs
2572 * Get the general registers for the process or LWP.
2573 * Returns non-zero for success, zero for failure.
2577 proc_get_gregs (procinfo
*pi
)
2579 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2580 if (!proc_get_status (pi
))
2584 * OK, sorry about the ifdef's.
2585 * There's three cases instead of two, because
2586 * in this instance Unixware and Solaris/RW differ.
2590 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2591 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2592 #else /* not Unixware */
2593 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2594 #endif /* Unixware */
2595 #else /* not NEW_PROC_API */
2596 return &pi
->prstatus
.pr_reg
;
2597 #endif /* NEW_PROC_API */
2601 * Function: proc_get_fpregs
2603 * Get the floating point registers for the process or LWP.
2604 * Returns non-zero for success, zero for failure.
2608 proc_get_fpregs (procinfo
*pi
)
2611 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2612 if (!proc_get_status (pi
))
2615 #ifdef UNIXWARE /* a true architecture dependency */
2616 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2618 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2619 #endif /* Unixware */
2621 #else /* not NEW_PROC_API */
2622 if (pi
->fpregs_valid
)
2623 return &pi
->fpregset
; /* already got 'em */
2626 if (pi
->ctl_fd
== 0 &&
2627 open_procinfo_files (pi
, FD_CTL
) == 0)
2636 tid_t pr_error_thread
;
2637 tfpregset_t thread_1
;
2640 thread_fpregs
.pr_count
= 1;
2641 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2644 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2646 pi
->fpregs_valid
= 1;
2647 return &pi
->fpregset
; /* got 'em now! */
2649 else if (pi
->tid
!= 0 &&
2650 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2652 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2653 sizeof (pi
->fpregset
));
2654 pi
->fpregs_valid
= 1;
2655 return &pi
->fpregset
; /* got 'em now! */
2662 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2664 pi
->fpregs_valid
= 1;
2665 return &pi
->fpregset
; /* got 'em now! */
2678 * Function: proc_set_gregs
2680 * Write the general registers back to the process or LWP.
2681 * Returns non-zero for success, zero for failure.
2685 proc_set_gregs (procinfo
*pi
)
2687 gdb_gregset_t
*gregs
;
2690 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2691 return 0; /* get_regs has already warned */
2693 if (pi
->ctl_fd
== 0 &&
2694 open_procinfo_files (pi
, FD_CTL
) == 0)
2703 /* Use char array to avoid alignment issues. */
2704 char gregs
[sizeof (gdb_gregset_t
)];
2708 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2709 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2711 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2715 /* Policy: writing the regs invalidates our cache. */
2716 pi
->gregs_valid
= 0;
2721 * Function: proc_set_fpregs
2723 * Modify the floating point register set of the process or LWP.
2724 * Returns non-zero for success, zero for failure.
2728 proc_set_fpregs (procinfo
*pi
)
2730 gdb_fpregset_t
*fpregs
;
2733 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2734 return 0; /* get_fpregs has already warned */
2736 if (pi
->ctl_fd
== 0 &&
2737 open_procinfo_files (pi
, FD_CTL
) == 0)
2746 /* Use char array to avoid alignment issues. */
2747 char fpregs
[sizeof (gdb_fpregset_t
)];
2751 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2752 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2756 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2761 tid_t pr_error_thread
;
2762 tfpregset_t thread_1
;
2765 thread_fpregs
.pr_count
= 1;
2766 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2767 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2769 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2772 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2773 #endif /* osf PIOCTSFPREG */
2774 #endif /* NEW_PROC_API */
2777 /* Policy: writing the regs invalidates our cache. */
2778 pi
->fpregs_valid
= 0;
2783 * Function: proc_kill
2785 * Send a signal to the proc or lwp with the semantics of "kill()".
2786 * Returns non-zero for success, zero for failure.
2790 proc_kill (procinfo
*pi
, int signo
)
2795 * We might conceivably apply this operation to an LWP, and
2796 * the LWP's ctl file descriptor might not be open.
2799 if (pi
->ctl_fd
== 0 &&
2800 open_procinfo_files (pi
, FD_CTL
) == 0)
2807 procfs_ctl_t cmd
[2];
2811 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2812 #else /* ioctl method */
2813 /* FIXME: do I need the Alpha OSF fixups present in
2814 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2815 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2823 * Function: proc_parent_pid
2825 * Find the pid of the process that started this one.
2826 * Returns the parent process pid, or zero.
2830 proc_parent_pid (procinfo
*pi
)
2833 * We should never have to apply this operation to any procinfo
2834 * except the one for the main process. If that ever changes
2835 * for any reason, then take out the following clause and
2836 * replace it with one that makes sure the ctl_fd is open.
2840 pi
= find_procinfo_or_die (pi
->pid
, 0);
2842 if (!pi
->status_valid
)
2843 if (!proc_get_status (pi
))
2846 return pi
->prstatus
.pr_ppid
;
2850 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2851 (a.k.a void pointer)! */
2854 procfs_address_to_host_pointer (CORE_ADDR addr
)
2858 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (builtin_type_void_data_ptr
));
2859 ADDRESS_TO_POINTER (builtin_type_void_data_ptr
, &ptr
, addr
);
2864 * Function: proc_set_watchpoint
2869 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2871 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2874 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2875 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2880 char watch
[sizeof (prwatch_t
)];
2884 pwatch
= (prwatch_t
*) &arg
.watch
;
2885 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2886 convert a target address into something that can be stored in a
2887 native data structure. */
2888 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2889 pwatch
->pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
2891 pwatch
->pr_vaddr
= (caddr_t
) procfs_address_to_host_pointer (addr
);
2893 pwatch
->pr_size
= len
;
2894 pwatch
->pr_wflags
= wflags
;
2895 #if defined(NEW_PROC_API) && defined (PCWATCH)
2897 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2899 #if defined (PIOCSWATCH)
2900 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2902 return 0; /* Fail */
2909 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2911 #include <sys/sysi86.h>
2914 * Function: proc_get_LDT_entry
2920 * The 'key' is actually the value of the lower 16 bits of
2921 * the GS register for the LWP that we're interested in.
2923 * Return: matching ssh struct (LDT entry).
2927 proc_get_LDT_entry (procinfo
*pi
, int key
)
2929 static struct ssd
*ldt_entry
= NULL
;
2931 char pathname
[MAX_PROC_NAME_SIZE
];
2932 struct cleanup
*old_chain
= NULL
;
2935 /* Allocate space for one LDT entry.
2936 This alloc must persist, because we return a pointer to it. */
2937 if (ldt_entry
== NULL
)
2938 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2940 /* Open the file descriptor for the LDT table. */
2941 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2942 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2944 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2947 /* Make sure it gets closed again! */
2948 old_chain
= make_cleanup_close (fd
);
2950 /* Now 'read' thru the table, find a match and return it. */
2951 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2953 if (ldt_entry
->sel
== 0 &&
2954 ldt_entry
->bo
== 0 &&
2955 ldt_entry
->acc1
== 0 &&
2956 ldt_entry
->acc2
== 0)
2957 break; /* end of table */
2958 /* If key matches, return this entry. */
2959 if (ldt_entry
->sel
== key
)
2962 /* Loop ended, match not found. */
2966 static int nalloc
= 0;
2968 /* Get the number of LDT entries. */
2969 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2971 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2975 /* Allocate space for the number of LDT entries. */
2976 /* This alloc has to persist, 'cause we return a pointer to it. */
2979 ldt_entry
= (struct ssd
*)
2980 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2984 /* Read the whole table in one gulp. */
2985 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2987 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2991 /* Search the table and return the (first) entry matching 'key'. */
2992 for (i
= 0; i
< nldt
; i
++)
2993 if (ldt_entry
[i
].sel
== key
)
2994 return &ldt_entry
[i
];
2996 /* Loop ended, match not found. */
3001 #endif /* TM_I386SOL2_H */
3003 /* =============== END, non-thread part of /proc "MODULE" =============== */
3005 /* =================== Thread "MODULE" =================== */
3007 /* NOTE: you'll see more ifdefs and duplication of functions here,
3008 since there is a different way to do threads on every OS. */
3011 * Function: proc_get_nthreads
3013 * Return the number of threads for the process
3016 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3021 proc_get_nthreads (procinfo
*pi
)
3025 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
3026 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
3032 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3034 * Solaris and Unixware version
3037 proc_get_nthreads (procinfo
*pi
)
3039 if (!pi
->status_valid
)
3040 if (!proc_get_status (pi
))
3044 * NEW_PROC_API: only works for the process procinfo,
3045 * because the LWP procinfos do not get prstatus filled in.
3048 if (pi
->tid
!= 0) /* find the parent process procinfo */
3049 pi
= find_procinfo_or_die (pi
->pid
, 0);
3051 return pi
->prstatus
.pr_nlwp
;
3059 proc_get_nthreads (procinfo
*pi
)
3067 * Function: proc_get_current_thread (LWP version)
3069 * Return the ID of the thread that had an event of interest.
3070 * (ie. the one that hit a breakpoint or other traced event).
3071 * All other things being equal, this should be the ID of a
3072 * thread that is currently executing.
3075 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3077 * Solaris and Unixware version
3080 proc_get_current_thread (procinfo
*pi
)
3083 * Note: this should be applied to the root procinfo for the process,
3084 * not to the procinfo for an LWP. If applied to the procinfo for
3085 * an LWP, it will simply return that LWP's ID. In that case,
3086 * find the parent process procinfo.
3090 pi
= find_procinfo_or_die (pi
->pid
, 0);
3092 if (!pi
->status_valid
)
3093 if (!proc_get_status (pi
))
3097 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
3099 return pi
->prstatus
.pr_who
;
3104 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3109 proc_get_current_thread (procinfo
*pi
)
3111 #if 0 /* FIXME: not ready for prime time? */
3112 return pi
->prstatus
.pr_tid
;
3123 proc_get_current_thread (procinfo
*pi
)
3132 * Function: proc_update_threads
3134 * Discover the IDs of all the threads within the process, and
3135 * create a procinfo for each of them (chained to the parent).
3137 * This unfortunately requires a different method on every OS.
3139 * Return: non-zero for success, zero for failure.
3143 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
3145 if (thread
&& parent
) /* sanity */
3147 thread
->status_valid
= 0;
3148 if (!proc_get_status (thread
))
3149 destroy_one_procinfo (&parent
->thread_list
, thread
);
3151 return 0; /* keep iterating */
3154 #if defined (PIOCLSTATUS)
3156 * Solaris 2.5 (ioctl) version
3159 proc_update_threads (procinfo
*pi
)
3161 gdb_prstatus_t
*prstatus
;
3162 struct cleanup
*old_chain
= NULL
;
3167 * We should never have to apply this operation to any procinfo
3168 * except the one for the main process. If that ever changes
3169 * for any reason, then take out the following clause and
3170 * replace it with one that makes sure the ctl_fd is open.
3174 pi
= find_procinfo_or_die (pi
->pid
, 0);
3176 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3178 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
3179 return 1; /* Process is not multi-threaded; nothing to do. */
3181 prstatus
= xmalloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1));
3183 old_chain
= make_cleanup (xfree
, prstatus
);
3184 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
3185 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
3187 /* Skip element zero, which represents the process as a whole. */
3188 for (i
= 1; i
< nlwp
+ 1; i
++)
3190 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
3191 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3193 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
3194 thread
->status_valid
= 1;
3196 pi
->threads_valid
= 1;
3197 do_cleanups (old_chain
);
3203 * Unixware and Solaris 6 (and later) version
3206 do_closedir_cleanup (void *dir
)
3212 proc_update_threads (procinfo
*pi
)
3214 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3215 struct dirent
*direntry
;
3216 struct cleanup
*old_chain
= NULL
;
3222 * We should never have to apply this operation to any procinfo
3223 * except the one for the main process. If that ever changes
3224 * for any reason, then take out the following clause and
3225 * replace it with one that makes sure the ctl_fd is open.
3229 pi
= find_procinfo_or_die (pi
->pid
, 0);
3231 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3236 * Note: this brute-force method is the only way I know of
3237 * to accomplish this task on Unixware. This method will
3238 * also work on Solaris 2.6 and 2.7. There is a much simpler
3239 * and more elegant way to do this on Solaris, but the margins
3240 * of this manuscript are too small to write it here... ;-)
3243 strcpy (pathname
, pi
->pathname
);
3244 strcat (pathname
, "/lwp");
3245 if ((dirp
= opendir (pathname
)) == NULL
)
3246 proc_error (pi
, "update_threads, opendir", __LINE__
);
3248 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
3249 while ((direntry
= readdir (dirp
)) != NULL
)
3250 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3252 lwpid
= atoi (&direntry
->d_name
[0]);
3253 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3254 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3256 pi
->threads_valid
= 1;
3257 do_cleanups (old_chain
);
3266 proc_update_threads (procinfo
*pi
)
3272 * We should never have to apply this operation to any procinfo
3273 * except the one for the main process. If that ever changes
3274 * for any reason, then take out the following clause and
3275 * replace it with one that makes sure the ctl_fd is open.
3279 pi
= find_procinfo_or_die (pi
->pid
, 0);
3281 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3283 nthreads
= proc_get_nthreads (pi
);
3285 return 0; /* nothing to do for 1 or fewer threads */
3287 threads
= xmalloc (nthreads
* sizeof (tid_t
));
3289 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3290 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3292 for (i
= 0; i
< nthreads
; i
++)
3294 if (!find_procinfo (pi
->pid
, threads
[i
]))
3295 if (!create_procinfo (pi
->pid
, threads
[i
]))
3296 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3298 pi
->threads_valid
= 1;
3306 proc_update_threads (procinfo
*pi
)
3310 #endif /* OSF PIOCTLIST */
3311 #endif /* NEW_PROC_API */
3312 #endif /* SOL 2.5 PIOCLSTATUS */
3315 * Function: proc_iterate_over_threads
3318 * Given a pointer to a function, call that function once
3319 * for each lwp in the procinfo list, until the function
3320 * returns non-zero, in which event return the value
3321 * returned by the function.
3323 * Note: this function does NOT call update_threads.
3324 * If you want to discover new threads first, you must
3325 * call that function explicitly. This function just makes
3326 * a quick pass over the currently-known procinfos.
3329 * pi - parent process procinfo
3330 * func - per-thread function
3331 * ptr - opaque parameter for function.
3334 * First non-zero return value from the callee, or zero.
3338 proc_iterate_over_threads (procinfo
*pi
,
3339 int (*func
) (procinfo
*, procinfo
*, void *),
3342 procinfo
*thread
, *next
;
3346 * We should never have to apply this operation to any procinfo
3347 * except the one for the main process. If that ever changes
3348 * for any reason, then take out the following clause and
3349 * replace it with one that makes sure the ctl_fd is open.
3353 pi
= find_procinfo_or_die (pi
->pid
, 0);
3355 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3357 next
= thread
->next
; /* in case thread is destroyed */
3358 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3365 /* =================== END, Thread "MODULE" =================== */
3367 /* =================== END, /proc "MODULE" =================== */
3369 /* =================== GDB "MODULE" =================== */
3372 * Here are all of the gdb target vector functions and their friends.
3375 static ptid_t
do_attach (ptid_t ptid
);
3376 static void do_detach (int signo
);
3377 static int register_gdb_signals (procinfo
*, gdb_sigset_t
*);
3380 * Function: procfs_debug_inferior
3382 * Sets up the inferior to be debugged.
3383 * Registers to trace signals, hardware faults, and syscalls.
3384 * Note: does not set RLC flag: caller may want to customize that.
3386 * Returns: zero for success (note! unlike most functions in this module)
3387 * On failure, returns the LINE NUMBER where it failed!
3391 procfs_debug_inferior (procinfo
*pi
)
3393 fltset_t traced_faults
;
3394 gdb_sigset_t traced_signals
;
3395 sysset_t
*traced_syscall_entries
;
3396 sysset_t
*traced_syscall_exits
;
3399 #ifdef PROCFS_DONT_TRACE_FAULTS
3400 /* On some systems (OSF), we don't trace hardware faults.
3401 Apparently it's enough that we catch them as signals.
3402 Wonder why we don't just do that in general? */
3403 premptyset (&traced_faults
); /* don't trace faults. */
3405 /* Register to trace hardware faults in the child. */
3406 prfillset (&traced_faults
); /* trace all faults... */
3407 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3409 if (!proc_set_traced_faults (pi
, &traced_faults
))
3412 /* Register to trace selected signals in the child. */
3413 premptyset (&traced_signals
);
3414 if (!register_gdb_signals (pi
, &traced_signals
))
3418 /* Register to trace the 'exit' system call (on entry). */
3419 traced_syscall_entries
= sysset_t_alloc (pi
);
3420 gdb_premptysysset (traced_syscall_entries
);
3422 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
3425 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3428 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
3430 #ifdef DYNAMIC_SYSCALLS
3432 int callnum
= find_syscall (pi
, "_exit");
3434 gdb_praddsysset (traced_syscall_entries
, callnum
);
3438 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
3439 xfree (traced_syscall_entries
);
3443 #ifdef PRFS_STOPEXEC /* defined on OSF */
3444 /* OSF method for tracing exec syscalls. Quoting:
3445 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3446 exits from exec system calls because of the user level loader. */
3447 /* FIXME: make nice and maybe move into an access function. */
3451 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3454 prfs_flags
|= PRFS_STOPEXEC
;
3456 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3459 #else /* not PRFS_STOPEXEC */
3460 /* Everyone else's (except OSF) method for tracing exec syscalls */
3462 Not all systems with /proc have all the exec* syscalls with the same
3463 names. On the SGI, for example, there is no SYS_exec, but there
3464 *is* a SYS_execv. So, we try to account for that. */
3466 traced_syscall_exits
= sysset_t_alloc (pi
);
3467 gdb_premptysysset (traced_syscall_exits
);
3469 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
3472 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
3475 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
3478 #ifdef SYS_lwpcreate
3479 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3480 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3483 #ifdef SYS_lwp_create /* FIXME: once only, please */
3484 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3485 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3488 #ifdef DYNAMIC_SYSCALLS
3490 int callnum
= find_syscall (pi
, "execve");
3492 gdb_praddsysset (traced_syscall_exits
, callnum
);
3493 callnum
= find_syscall (pi
, "ra_execve");
3495 gdb_praddsysset (traced_syscall_exits
, callnum
);
3499 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3500 xfree (traced_syscall_exits
);
3504 #endif /* PRFS_STOPEXEC */
3509 procfs_attach (char *args
, int from_tty
)
3515 error_no_arg ("process-id to attach");
3518 if (pid
== getpid ())
3519 error ("Attaching GDB to itself is not a good idea...");
3523 exec_file
= get_exec_file (0);
3526 printf_filtered ("Attaching to program `%s', %s\n",
3527 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3529 printf_filtered ("Attaching to %s\n",
3530 target_pid_to_str (pid_to_ptid (pid
)));
3534 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3535 push_target (&procfs_ops
);
3539 procfs_detach (char *args
, int from_tty
)
3546 exec_file
= get_exec_file (0);
3549 printf_filtered ("Detaching from program: %s %s\n",
3550 exec_file
, target_pid_to_str (inferior_ptid
));
3554 signo
= atoi (args
);
3557 inferior_ptid
= null_ptid
;
3558 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3562 do_attach (ptid_t ptid
)
3567 if ((pi
= create_procinfo (PIDGET (ptid
), 0)) == NULL
)
3568 perror ("procfs: out of memory in 'attach'");
3570 if (!open_procinfo_files (pi
, FD_CTL
))
3572 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3573 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3575 dead_procinfo (pi
, errmsg
, NOKILL
);
3578 /* Stop the process (if it isn't already stopped). */
3579 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3581 pi
->was_stopped
= 1;
3582 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3586 pi
->was_stopped
= 0;
3587 /* Set the process to run again when we close it. */
3588 if (!proc_set_run_on_last_close (pi
))
3589 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3591 /* Now stop the process. */
3592 if (!proc_stop_process (pi
))
3593 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3594 pi
->ignore_next_sigstop
= 1;
3596 /* Save some of the /proc state to be restored if we detach. */
3597 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3598 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3599 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3600 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3601 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3602 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3604 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3605 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3607 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3608 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3610 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3611 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3613 /* Let GDB know that the inferior was attached. */
3615 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3619 do_detach (int signo
)
3623 /* Find procinfo for the main process */
3624 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0); /* FIXME: threads */
3626 if (!proc_set_current_signal (pi
, signo
))
3627 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3629 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3630 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3632 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3633 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3635 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3636 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3638 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3639 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3641 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3642 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3644 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3645 if (signo
|| !(pi
->was_stopped
) ||
3646 query ("Was stopped when attached, make it runnable again? "))
3648 /* Clear any pending signal. */
3649 if (!proc_clear_current_fault (pi
))
3650 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3652 if (signo
== 0 && !proc_clear_current_signal (pi
))
3653 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
3655 if (!proc_set_run_on_last_close (pi
))
3656 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3660 destroy_procinfo (pi
);
3666 * Since the /proc interface cannot give us individual registers,
3667 * we pay no attention to the (regno) argument, and just fetch them all.
3668 * This results in the possibility that we will do unnecessarily many
3669 * fetches, since we may be called repeatedly for individual registers.
3670 * So we cache the results, and mark the cache invalid when the process
3675 procfs_fetch_registers (int regno
)
3677 gdb_fpregset_t
*fpregs
;
3678 gdb_gregset_t
*gregs
;
3683 pid
= PIDGET (inferior_ptid
);
3684 tid
= TIDGET (inferior_ptid
);
3686 /* First look up procinfo for the main process. */
3687 pi
= find_procinfo_or_die (pid
, 0);
3689 /* If the event thread is not the same as GDB's requested thread
3690 (ie. inferior_ptid), then look up procinfo for the requested
3693 (tid
!= proc_get_current_thread (pi
)))
3694 pi
= find_procinfo_or_die (pid
, tid
);
3697 error ("procfs: fetch_registers failed to find procinfo for %s",
3698 target_pid_to_str (inferior_ptid
));
3700 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3701 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3703 supply_gregset (gregs
);
3705 if (FP0_REGNUM
>= 0) /* need floating point? */
3707 if ((regno
>= 0 && regno
< FP0_REGNUM
)
3708 || regno
== PC_REGNUM
3709 || regno
== DEPRECATED_FP_REGNUM
3710 || regno
== SP_REGNUM
)
3711 return; /* not a floating point register */
3713 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3714 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3716 supply_fpregset (fpregs
);
3720 /* Get ready to modify the registers array. On machines which store
3721 individual registers, this doesn't need to do anything. On
3722 machines which store all the registers in one fell swoop, such as
3723 /proc, this makes sure that registers contains all the registers
3724 from the program being debugged. */
3727 procfs_prepare_to_store (void)
3729 #ifdef CHILD_PREPARE_TO_STORE
3730 CHILD_PREPARE_TO_STORE ();
3737 * Since the /proc interface will not read individual registers,
3738 * we will cache these requests until the process is resumed, and
3739 * only then write them back to the inferior process.
3741 * FIXME: is that a really bad idea? Have to think about cases
3742 * where writing one register might affect the value of others, etc.
3746 procfs_store_registers (int regno
)
3748 gdb_fpregset_t
*fpregs
;
3749 gdb_gregset_t
*gregs
;
3754 pid
= PIDGET (inferior_ptid
);
3755 tid
= TIDGET (inferior_ptid
);
3757 /* First find procinfo for main process */
3758 pi
= find_procinfo_or_die (pid
, 0);
3760 /* If current lwp for process is not the same as requested thread
3761 (ie. inferior_ptid), then find procinfo for the requested thread. */
3764 (tid
!= proc_get_current_thread (pi
)))
3765 pi
= find_procinfo_or_die (pid
, tid
);
3768 error ("procfs: store_registers: failed to find procinfo for %s",
3769 target_pid_to_str (inferior_ptid
));
3771 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3772 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3774 fill_gregset (gregs
, regno
);
3775 if (!proc_set_gregs (pi
))
3776 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3778 if (FP0_REGNUM
>= 0) /* need floating point? */
3780 if ((regno
>= 0 && regno
< FP0_REGNUM
)
3781 || regno
== PC_REGNUM
3782 || regno
== DEPRECATED_FP_REGNUM
3783 || regno
== SP_REGNUM
)
3784 return; /* not a floating point register */
3786 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3787 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3789 fill_fpregset (fpregs
, regno
);
3790 if (!proc_set_fpregs (pi
))
3791 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3796 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3800 if (scall
== SYS_lwp_exit
)
3804 if (scall
== SYS_lwpexit
)
3811 syscall_is_exit (procinfo
*pi
, int scall
)
3814 if (scall
== SYS_exit
)
3817 #ifdef DYNAMIC_SYSCALLS
3818 if (find_syscall (pi
, "_exit") == scall
)
3825 syscall_is_exec (procinfo
*pi
, int scall
)
3828 if (scall
== SYS_exec
)
3832 if (scall
== SYS_execv
)
3836 if (scall
== SYS_execve
)
3839 #ifdef DYNAMIC_SYSCALLS
3840 if (find_syscall (pi
, "_execve"))
3842 if (find_syscall (pi
, "ra_execve"))
3849 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3851 #ifdef SYS_lwp_create
3852 if (scall
== SYS_lwp_create
)
3855 #ifdef SYS_lwpcreate
3856 if (scall
== SYS_lwpcreate
)
3863 * Function: target_wait
3865 * Retrieve the next stop event from the child process.
3866 * If child has not stopped yet, wait for it to stop.
3867 * Translate /proc eventcodes (or possibly wait eventcodes)
3868 * into gdb internal event codes.
3870 * Return: id of process (and possibly thread) that incurred the event.
3871 * event codes are returned thru a pointer parameter.
3875 procfs_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3877 /* First cut: loosely based on original version 2.1 */
3881 ptid_t retval
, temp_ptid
;
3882 int why
, what
, flags
;
3889 retval
= pid_to_ptid (-1);
3891 /* Find procinfo for main process */
3892 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
3895 /* We must assume that the status is stale now... */
3896 pi
->status_valid
= 0;
3897 pi
->gregs_valid
= 0;
3898 pi
->fpregs_valid
= 0;
3900 #if 0 /* just try this out... */
3901 flags
= proc_flags (pi
);
3902 why
= proc_why (pi
);
3903 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3904 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3906 /* If child is not stopped, wait for it to stop. */
3907 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3908 !proc_wait_for_stop (pi
))
3910 /* wait_for_stop failed: has the child terminated? */
3911 if (errno
== ENOENT
)
3915 /* /proc file not found; presumably child has terminated. */
3916 wait_retval
= wait (&wstat
); /* "wait" for the child's exit */
3918 if (wait_retval
!= PIDGET (inferior_ptid
)) /* wrong child? */
3919 error ("procfs: couldn't stop process %d: wait returned %d\n",
3920 PIDGET (inferior_ptid
), wait_retval
);
3921 /* FIXME: might I not just use waitpid?
3922 Or try find_procinfo to see if I know about this child? */
3923 retval
= pid_to_ptid (wait_retval
);
3925 else if (errno
== EINTR
)
3929 /* Unknown error from wait_for_stop. */
3930 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3935 /* This long block is reached if either:
3936 a) the child was already stopped, or
3937 b) we successfully waited for the child with wait_for_stop.
3938 This block will analyze the /proc status, and translate it
3939 into a waitstatus for GDB.
3941 If we actually had to call wait because the /proc file
3942 is gone (child terminated), then we skip this block,
3943 because we already have a waitstatus. */
3945 flags
= proc_flags (pi
);
3946 why
= proc_why (pi
);
3947 what
= proc_what (pi
);
3949 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3952 /* If it's running async (for single_thread control),
3953 set it back to normal again. */
3954 if (flags
& PR_ASYNC
)
3955 if (!proc_unset_async (pi
))
3956 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3960 proc_prettyprint_why (why
, what
, 1);
3962 /* The 'pid' we will return to GDB is composed of
3963 the process ID plus the lwp ID. */
3964 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3968 wstat
= (what
<< 8) | 0177;
3971 if (syscall_is_lwp_exit (pi
, what
))
3973 printf_filtered ("[%s exited]\n",
3974 target_pid_to_str (retval
));
3975 delete_thread (retval
);
3976 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3979 else if (syscall_is_exit (pi
, what
))
3981 /* Handle SYS_exit call only */
3982 /* Stopped at entry to SYS_exit.
3983 Make it runnable, resume it, then use
3984 the wait system call to get its exit code.
3985 Proc_run_process always clears the current
3987 Then return its exit status. */
3988 pi
->status_valid
= 0;
3990 /* FIXME: what we should do is return
3991 TARGET_WAITKIND_SPURIOUS. */
3992 if (!proc_run_process (pi
, 0, 0))
3993 proc_error (pi
, "target_wait, run_process", __LINE__
);
3996 /* Don't call wait: simulate waiting for exit,
3997 return a "success" exit code. Bogus: what if
3998 it returns something else? */
4000 retval
= inferior_ptid
; /* ? ? ? */
4004 int temp
= wait (&wstat
);
4006 /* FIXME: shouldn't I make sure I get the right
4007 event from the right process? If (for
4008 instance) I have killed an earlier inferior
4009 process but failed to clean up after it
4010 somehow, I could get its termination event
4013 /* If wait returns -1, that's what we return to GDB. */
4015 retval
= pid_to_ptid (temp
);
4020 printf_filtered ("procfs: trapped on entry to ");
4021 proc_prettyprint_syscall (proc_what (pi
), 0);
4022 printf_filtered ("\n");
4025 long i
, nsysargs
, *sysargs
;
4027 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4028 (sysargs
= proc_sysargs (pi
)) != NULL
)
4030 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4031 for (i
= 0; i
< nsysargs
; i
++)
4032 printf_filtered ("#%ld: 0x%08lx\n",
4040 /* How to exit gracefully, returning "unknown event" */
4041 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4042 return inferior_ptid
;
4046 /* How to keep going without returning to wfi: */
4047 target_resume (ptid
, 0, TARGET_SIGNAL_0
);
4053 if (syscall_is_exec (pi
, what
))
4055 /* Hopefully this is our own "fork-child" execing
4056 the real child. Hoax this event into a trap, and
4057 GDB will see the child about to execute its start
4059 wstat
= (SIGTRAP
<< 8) | 0177;
4061 else if (syscall_is_lwp_create (pi
, what
))
4064 * This syscall is somewhat like fork/exec.
4065 * We will get the event twice: once for the parent LWP,
4066 * and once for the child. We should already know about
4067 * the parent LWP, but the child will be new to us. So,
4068 * whenever we get this event, if it represents a new
4069 * thread, simply add the thread to the list.
4072 /* If not in procinfo list, add it. */
4073 temp_tid
= proc_get_current_thread (pi
);
4074 if (!find_procinfo (pi
->pid
, temp_tid
))
4075 create_procinfo (pi
->pid
, temp_tid
);
4077 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4078 /* If not in GDB's thread list, add it. */
4079 if (!in_thread_list (temp_ptid
))
4081 printf_filtered ("[New %s]\n",
4082 target_pid_to_str (temp_ptid
));
4083 add_thread (temp_ptid
);
4085 /* Return to WFI, but tell it to immediately resume. */
4086 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4087 return inferior_ptid
;
4089 else if (syscall_is_lwp_exit (pi
, what
))
4091 printf_filtered ("[%s exited]\n",
4092 target_pid_to_str (retval
));
4093 delete_thread (retval
);
4094 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4099 /* FIXME: Do we need to handle SYS_sproc,
4100 SYS_fork, or SYS_vfork here? The old procfs
4101 seemed to use this event to handle threads on
4102 older (non-LWP) systems, where I'm assuming
4103 that threads were actually separate processes.
4104 Irix, maybe? Anyway, low priority for now. */
4108 printf_filtered ("procfs: trapped on exit from ");
4109 proc_prettyprint_syscall (proc_what (pi
), 0);
4110 printf_filtered ("\n");
4113 long i
, nsysargs
, *sysargs
;
4115 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4116 (sysargs
= proc_sysargs (pi
)) != NULL
)
4118 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4119 for (i
= 0; i
< nsysargs
; i
++)
4120 printf_filtered ("#%ld: 0x%08lx\n",
4125 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4126 return inferior_ptid
;
4131 wstat
= (SIGSTOP
<< 8) | 0177;
4136 printf_filtered ("Retry #%d:\n", retry
);
4137 pi
->status_valid
= 0;
4142 /* If not in procinfo list, add it. */
4143 temp_tid
= proc_get_current_thread (pi
);
4144 if (!find_procinfo (pi
->pid
, temp_tid
))
4145 create_procinfo (pi
->pid
, temp_tid
);
4147 /* If not in GDB's thread list, add it. */
4148 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4149 if (!in_thread_list (temp_ptid
))
4151 printf_filtered ("[New %s]\n",
4152 target_pid_to_str (temp_ptid
));
4153 add_thread (temp_ptid
);
4156 status
->kind
= TARGET_WAITKIND_STOPPED
;
4157 status
->value
.sig
= 0;
4162 wstat
= (what
<< 8) | 0177;
4168 wstat
= (SIGTRAP
<< 8) | 0177;
4173 wstat
= (SIGTRAP
<< 8) | 0177;
4176 /* FIXME: use si_signo where possible. */
4178 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4181 wstat
= (SIGILL
<< 8) | 0177;
4184 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4187 wstat
= (SIGTRAP
<< 8) | 0177;
4191 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4194 wstat
= (SIGSEGV
<< 8) | 0177;
4198 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4201 wstat
= (SIGFPE
<< 8) | 0177;
4203 case FLTPAGE
: /* Recoverable page fault */
4204 default: /* FIXME: use si_signo if possible for fault */
4205 retval
= pid_to_ptid (-1);
4206 printf_filtered ("procfs:%d -- ", __LINE__
);
4207 printf_filtered ("child stopped for unknown reason:\n");
4208 proc_prettyprint_why (why
, what
, 1);
4209 error ("... giving up...");
4212 break; /* case PR_FAULTED: */
4213 default: /* switch (why) unmatched */
4214 printf_filtered ("procfs:%d -- ", __LINE__
);
4215 printf_filtered ("child stopped for unknown reason:\n");
4216 proc_prettyprint_why (why
, what
, 1);
4217 error ("... giving up...");
4221 * Got this far without error:
4222 * If retval isn't in the threads database, add it.
4224 if (PIDGET (retval
) > 0 &&
4225 !ptid_equal (retval
, inferior_ptid
) &&
4226 !in_thread_list (retval
))
4229 * We have a new thread.
4230 * We need to add it both to GDB's list and to our own.
4231 * If we don't create a procinfo, resume may be unhappy
4234 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
4235 add_thread (retval
);
4236 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
4237 create_procinfo (PIDGET (retval
), TIDGET (retval
));
4239 /* In addition, it's possible that this is the first
4240 * new thread we've seen, in which case we may not
4241 * have created entries for inferior_ptid yet.
4243 if (TIDGET (inferior_ptid
) != 0)
4245 if (!in_thread_list (inferior_ptid
))
4246 add_thread (inferior_ptid
);
4247 if (find_procinfo (PIDGET (inferior_ptid
),
4248 TIDGET (inferior_ptid
)) == NULL
)
4249 create_procinfo (PIDGET (inferior_ptid
),
4250 TIDGET (inferior_ptid
));
4254 else /* flags do not indicate STOPPED */
4256 /* surely this can't happen... */
4257 printf_filtered ("procfs:%d -- process not stopped.\n",
4259 proc_prettyprint_flags (flags
, 1);
4260 error ("procfs: ...giving up...");
4265 store_waitstatus (status
, wstat
);
4271 /* Transfer LEN bytes between GDB address MYADDR and target address
4272 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4273 otherwise transfer them from the target. TARGET is unused.
4275 The return value is 0 if an error occurred or no bytes were
4276 transferred. Otherwise, it will be a positive value which
4277 indicates the number of bytes transferred between gdb and the
4278 target. (Note that the interface also makes provisions for
4279 negative values, but this capability isn't implemented here.) */
4282 procfs_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int dowrite
,
4283 struct mem_attrib
*attrib
, struct target_ops
*target
)
4288 /* Find procinfo for main process */
4289 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4290 if (pi
->as_fd
== 0 &&
4291 open_procinfo_files (pi
, FD_AS
) == 0)
4293 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4297 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4302 PROCFS_NOTE ("write memory: ");
4304 PROCFS_NOTE ("write memory: \n");
4306 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4310 PROCFS_NOTE ("read memory: \n");
4311 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4322 * Function: invalidate_cache
4324 * Called by target_resume before making child runnable.
4325 * Mark cached registers and status's invalid.
4326 * If there are "dirty" caches that need to be written back
4327 * to the child process, do that.
4329 * File descriptors are also cached.
4330 * As they are a limited resource, we cannot hold onto them indefinitely.
4331 * However, as they are expensive to open, we don't want to throw them
4332 * away indescriminately either. As a compromise, we will keep the
4333 * file descriptors for the parent process, but discard any file
4334 * descriptors we may have accumulated for the threads.
4337 * As this function is called by iterate_over_threads, it always
4338 * returns zero (so that iterate_over_threads will keep iterating).
4343 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4346 * About to run the child; invalidate caches and do any other cleanup.
4350 if (pi
->gregs_dirty
)
4351 if (parent
== NULL
||
4352 proc_get_current_thread (parent
) != pi
->tid
)
4353 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4354 proc_warn (pi
, "target_resume, set_gregs",
4356 if (FP0_REGNUM
>= 0)
4357 if (pi
->fpregs_dirty
)
4358 if (parent
== NULL
||
4359 proc_get_current_thread (parent
) != pi
->tid
)
4360 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4361 proc_warn (pi
, "target_resume, set_fpregs",
4367 /* The presence of a parent indicates that this is an LWP.
4368 Close any file descriptors that it might have open.
4369 We don't do this to the master (parent) procinfo. */
4371 close_procinfo_files (pi
);
4373 pi
->gregs_valid
= 0;
4374 pi
->fpregs_valid
= 0;
4376 pi
->gregs_dirty
= 0;
4377 pi
->fpregs_dirty
= 0;
4379 pi
->status_valid
= 0;
4380 pi
->threads_valid
= 0;
4387 * Function: make_signal_thread_runnable
4389 * A callback function for iterate_over_threads.
4390 * Find the asynchronous signal thread, and make it runnable.
4391 * See if that helps matters any.
4395 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4398 if (proc_flags (pi
) & PR_ASLWP
)
4400 if (!proc_run_process (pi
, 0, -1))
4401 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4410 * Function: target_resume
4412 * Make the child process runnable. Normally we will then call
4413 * procfs_wait and wait for it to stop again (unles gdb is async).
4416 * step: if true, then arrange for the child to stop again
4417 * after executing a single instruction.
4418 * signo: if zero, then cancel any pending signal.
4419 * If non-zero, then arrange for the indicated signal
4420 * to be delivered to the child when it runs.
4421 * pid: if -1, then allow any child thread to run.
4422 * if non-zero, then allow only the indicated thread to run.
4423 ******* (not implemented yet)
4427 procfs_resume (ptid_t ptid
, int step
, enum target_signal signo
)
4429 procinfo
*pi
, *thread
;
4433 prrun.prflags |= PRSVADDR;
4434 prrun.pr_vaddr = $PC; set resume address
4435 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4436 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4437 prrun.prflags |= PRCFAULT; clear current fault.
4439 PRSTRACE and PRSFAULT can be done by other means
4440 (proc_trace_signals, proc_trace_faults)
4441 PRSVADDR is unnecessary.
4442 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4443 This basically leaves PRSTEP and PRCSIG.
4444 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4445 So basically PR_STEP is the sole argument that must be passed
4446 to proc_run_process (for use in the prrun struct by ioctl). */
4448 /* Find procinfo for main process */
4449 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4451 /* First cut: ignore pid argument */
4454 /* Convert signal to host numbering. */
4456 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4459 native_signo
= target_signal_to_host (signo
);
4461 pi
->ignore_next_sigstop
= 0;
4463 /* Running the process voids all cached registers and status. */
4464 /* Void the threads' caches first */
4465 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4466 /* Void the process procinfo's caches. */
4467 invalidate_cache (NULL
, pi
, NULL
);
4469 if (PIDGET (ptid
) != -1)
4471 /* Resume a specific thread, presumably suppressing the others. */
4472 thread
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
));
4475 if (thread
->tid
!= 0)
4477 /* We're to resume a specific thread, and not the others.
4478 * Set the child process's PR_ASYNC flag.
4481 if (!proc_set_async (pi
))
4482 proc_error (pi
, "target_resume, set_async", __LINE__
);
4485 proc_iterate_over_threads (pi
,
4486 make_signal_thread_runnable
,
4489 pi
= thread
; /* substitute the thread's procinfo for run */
4494 if (!proc_run_process (pi
, step
, native_signo
))
4497 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4499 proc_error (pi
, "target_resume", __LINE__
);
4504 * Function: register_gdb_signals
4506 * Traverse the list of signals that GDB knows about
4507 * (see "handle" command), and arrange for the target
4508 * to be stopped or not, according to these settings.
4510 * Returns non-zero for success, zero for failure.
4514 register_gdb_signals (procinfo
*pi
, gdb_sigset_t
*signals
)
4518 for (signo
= 0; signo
< NSIG
; signo
++)
4519 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4520 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4521 signal_pass_state (target_signal_from_host (signo
)) == 1)
4522 prdelset (signals
, signo
);
4524 praddset (signals
, signo
);
4526 return proc_set_traced_signals (pi
, signals
);
4530 * Function: target_notice_signals
4532 * Set up to trace signals in the child process.
4536 procfs_notice_signals (ptid_t ptid
)
4538 gdb_sigset_t signals
;
4539 procinfo
*pi
= find_procinfo_or_die (PIDGET (ptid
), 0);
4541 if (proc_get_traced_signals (pi
, &signals
) &&
4542 register_gdb_signals (pi
, &signals
))
4545 proc_error (pi
, "notice_signals", __LINE__
);
4549 * Function: target_files_info
4551 * Print status information about the child process.
4555 procfs_files_info (struct target_ops
*ignore
)
4557 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4558 attach_flag
? "attached": "child",
4559 target_pid_to_str (inferior_ptid
));
4563 * Function: target_open
4565 * A dummy: you don't open procfs.
4569 procfs_open (char *args
, int from_tty
)
4571 error ("Use the \"run\" command to start a Unix child process.");
4575 * Function: target_can_run
4577 * This tells GDB that this target vector can be invoked
4578 * for "run" or "attach".
4581 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4582 be a runnable target. Used by targets
4583 that can sit atop procfs, such as solaris
4588 procfs_can_run (void)
4590 /* This variable is controlled by modules that sit atop procfs that
4591 may layer their own process structure atop that provided here.
4592 sol-thread.c does this because of the Solaris two-level thread
4595 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4597 return !procfs_suppress_run
;
4601 * Function: target_stop
4603 * Stop the child process asynchronously, as when the
4604 * gdb user types control-c or presses a "stop" button.
4606 * Works by sending kill(SIGINT) to the child's process group.
4612 kill (-inferior_process_group
, SIGINT
);
4616 * Function: unconditionally_kill_inferior
4618 * Make it die. Wait for it to die. Clean up after it.
4619 * Note: this should only be applied to the real process,
4620 * not to an LWP, because of the check for parent-process.
4621 * If we need this to work for an LWP, it needs some more logic.
4625 unconditionally_kill_inferior (procinfo
*pi
)
4629 parent_pid
= proc_parent_pid (pi
);
4630 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4631 /* FIXME: use access functions */
4632 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4633 before the PIOCKILL, otherwise it might generate a corrupted core
4634 file for the inferior. */
4635 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4637 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4640 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4641 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4642 to kill the inferior, otherwise it might remain stopped with a
4644 We do not check the result of the PIOCSSIG, the inferior might have
4647 gdb_siginfo_t newsiginfo
;
4649 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4650 newsiginfo
.si_signo
= SIGKILL
;
4651 newsiginfo
.si_code
= 0;
4652 newsiginfo
.si_errno
= 0;
4653 newsiginfo
.si_pid
= getpid ();
4654 newsiginfo
.si_uid
= getuid ();
4655 /* FIXME: use proc_set_current_signal */
4656 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4658 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4659 if (!proc_kill (pi
, SIGKILL
))
4660 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4661 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4662 destroy_procinfo (pi
);
4664 /* If pi is GDB's child, wait for it to die. */
4665 if (parent_pid
== getpid ())
4666 /* FIXME: should we use waitpid to make sure we get the right event?
4667 Should we check the returned event? */
4672 ret
= waitpid (pi
->pid
, &status
, 0);
4680 * Function: target_kill_inferior
4682 * We're done debugging it, and we want it to go away.
4683 * Then we want GDB to forget all about it.
4687 procfs_kill_inferior (void)
4689 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4691 /* Find procinfo for main process */
4692 procinfo
*pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4695 unconditionally_kill_inferior (pi
);
4696 target_mourn_inferior ();
4701 * Function: target_mourn_inferior
4703 * Forget we ever debugged this thing!
4707 procfs_mourn_inferior (void)
4711 if (!ptid_equal (inferior_ptid
, null_ptid
))
4713 /* Find procinfo for main process */
4714 pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4716 destroy_procinfo (pi
);
4718 unpush_target (&procfs_ops
);
4719 generic_mourn_inferior ();
4723 * Function: init_inferior
4725 * When GDB forks to create a runnable inferior process,
4726 * this function is called on the parent side of the fork.
4727 * It's job is to do whatever is necessary to make the child
4728 * ready to be debugged, and then wait for the child to synchronize.
4732 procfs_init_inferior (int pid
)
4735 gdb_sigset_t signals
;
4738 /* This routine called on the parent side (GDB side)
4739 after GDB forks the inferior. */
4741 push_target (&procfs_ops
);
4743 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4744 perror ("procfs: out of memory in 'init_inferior'");
4746 if (!open_procinfo_files (pi
, FD_CTL
))
4747 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4751 open_procinfo_files // done
4754 procfs_notice_signals
4761 /* If not stopped yet, wait for it to stop. */
4762 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4763 !(proc_wait_for_stop (pi
)))
4764 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4766 /* Save some of the /proc state to be restored if we detach. */
4767 /* FIXME: Why? In case another debugger was debugging it?
4768 We're it's parent, for Ghu's sake! */
4769 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4770 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4771 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4772 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4773 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4774 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4775 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4776 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4777 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4778 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4780 /* Register to trace selected signals in the child. */
4781 prfillset (&signals
);
4782 if (!register_gdb_signals (pi
, &signals
))
4783 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4785 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4786 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4788 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4789 and possibly even turning ON kill-on-last-close at this point. But
4790 I can't make that change without careful testing which I don't have
4791 time to do right now... */
4792 /* Turn on run-on-last-close flag so that the child
4793 will die if GDB goes away for some reason. */
4794 if (!proc_set_run_on_last_close (pi
))
4795 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4797 /* The 'process ID' we return to GDB is composed of
4798 the actual process ID plus the lwp ID. */
4799 inferior_ptid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4801 #ifdef START_INFERIOR_TRAPS_EXPECTED
4802 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4804 /* One trap to exec the shell, one to exec the program being debugged. */
4805 startup_inferior (2);
4806 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4810 * Function: set_exec_trap
4812 * When GDB forks to create a new process, this function is called
4813 * on the child side of the fork before GDB exec's the user program.
4814 * Its job is to make the child minimally debuggable, so that the
4815 * parent GDB process can connect to the child and take over.
4816 * This function should do only the minimum to make that possible,
4817 * and to synchronize with the parent process. The parent process
4818 * should take care of the details.
4822 procfs_set_exec_trap (void)
4824 /* This routine called on the child side (inferior side)
4825 after GDB forks the inferior. It must use only local variables,
4826 because it may be sharing data space with its parent. */
4831 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4832 perror_with_name ("procfs: create_procinfo failed in child.");
4834 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4836 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4837 gdb_flush (gdb_stderr
);
4838 /* no need to call "dead_procinfo", because we're going to exit. */
4842 #ifdef PRFS_STOPEXEC /* defined on OSF */
4843 /* OSF method for tracing exec syscalls. Quoting:
4844 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4845 exits from exec system calls because of the user level loader. */
4846 /* FIXME: make nice and maybe move into an access function. */
4850 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4852 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4853 gdb_flush (gdb_stderr
);
4856 prfs_flags
|= PRFS_STOPEXEC
;
4858 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4860 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4861 gdb_flush (gdb_stderr
);
4865 #else /* not PRFS_STOPEXEC */
4866 /* Everyone else's (except OSF) method for tracing exec syscalls */
4868 Not all systems with /proc have all the exec* syscalls with the same
4869 names. On the SGI, for example, there is no SYS_exec, but there
4870 *is* a SYS_execv. So, we try to account for that. */
4872 exitset
= sysset_t_alloc (pi
);
4873 gdb_premptysysset (exitset
);
4875 gdb_praddsysset (exitset
, SYS_exec
);
4878 gdb_praddsysset (exitset
, SYS_execve
);
4881 gdb_praddsysset (exitset
, SYS_execv
);
4883 #ifdef DYNAMIC_SYSCALLS
4885 int callnum
= find_syscall (pi
, "execve");
4888 gdb_praddsysset (exitset
, callnum
);
4890 callnum
= find_syscall (pi
, "ra_execve");
4892 gdb_praddsysset (exitset
, callnum
);
4894 #endif /* DYNAMIC_SYSCALLS */
4896 if (!proc_set_traced_sysexit (pi
, exitset
))
4898 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4899 gdb_flush (gdb_stderr
);
4902 #endif /* PRFS_STOPEXEC */
4904 /* FIXME: should this be done in the parent instead? */
4905 /* Turn off inherit on fork flag so that all grand-children
4906 of gdb start with tracing flags cleared. */
4907 if (!proc_unset_inherit_on_fork (pi
))
4908 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4910 /* Turn off run on last close flag, so that the child process
4911 cannot run away just because we close our handle on it.
4912 We want it to wait for the parent to attach. */
4913 if (!proc_unset_run_on_last_close (pi
))
4914 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4916 /* FIXME: No need to destroy the procinfo --
4917 we have our own address space, and we're about to do an exec! */
4918 /*destroy_procinfo (pi);*/
4922 * Function: create_inferior
4924 * This function is called BEFORE gdb forks the inferior process.
4925 * Its only real responsibility is to set things up for the fork,
4926 * and tell GDB which two functions to call after the fork (one
4927 * for the parent, and one for the child).
4929 * This function does a complicated search for a unix shell program,
4930 * which it then uses to parse arguments and environment variables
4931 * to be sent to the child. I wonder whether this code could not
4932 * be abstracted out and shared with other unix targets such as
4937 procfs_create_inferior (char *exec_file
, char *allargs
, char **env
)
4939 char *shell_file
= getenv ("SHELL");
4941 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4944 /* We will be looking down the PATH to find shell_file. If we
4945 just do this the normal way (via execlp, which operates by
4946 attempting an exec for each element of the PATH until it
4947 finds one which succeeds), then there will be an exec for
4948 each failed attempt, each of which will cause a PR_SYSEXIT
4949 stop, and we won't know how to distinguish the PR_SYSEXIT's
4950 for these failed execs with the ones for successful execs
4951 (whether the exec has succeeded is stored at that time in the
4952 carry bit or some such architecture-specific and
4953 non-ABI-specified place).
4955 So I can't think of anything better than to search the PATH
4956 now. This has several disadvantages: (1) There is a race
4957 condition; if we find a file now and it is deleted before we
4958 exec it, we lose, even if the deletion leaves a valid file
4959 further down in the PATH, (2) there is no way to know exactly
4960 what an executable (in the sense of "capable of being
4961 exec'd") file is. Using access() loses because it may lose
4962 if the caller is the superuser; failing to use it loses if
4963 there are ACLs or some such. */
4967 /* FIXME-maybe: might want "set path" command so user can change what
4968 path is used from within GDB. */
4969 char *path
= getenv ("PATH");
4971 struct stat statbuf
;
4974 path
= "/bin:/usr/bin";
4976 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4977 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4979 p1
= strchr (p
, ':');
4984 strncpy (tryname
, p
, len
);
4985 tryname
[len
] = '\0';
4986 strcat (tryname
, "/");
4987 strcat (tryname
, shell_file
);
4988 if (access (tryname
, X_OK
) < 0)
4990 if (stat (tryname
, &statbuf
) < 0)
4992 if (!S_ISREG (statbuf
.st_mode
))
4993 /* We certainly need to reject directories. I'm not quite
4994 as sure about FIFOs, sockets, etc., but I kind of doubt
4995 that people want to exec() these things. */
5000 /* Not found. This must be an error rather than merely passing
5001 the file to execlp(), because execlp() would try all the
5002 exec()s, causing GDB to get confused. */
5003 error ("procfs:%d -- Can't find shell %s in PATH",
5004 __LINE__
, shell_file
);
5006 shell_file
= tryname
;
5009 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
5010 procfs_init_inferior
, NULL
, shell_file
);
5012 /* We are at the first instruction we care about. */
5013 /* Pedal to the metal... */
5015 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
5019 * Function: notice_thread
5021 * Callback for find_new_threads.
5022 * Calls "add_thread".
5026 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
5028 ptid_t gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
5030 if (!in_thread_list (gdb_threadid
))
5031 add_thread (gdb_threadid
);
5037 * Function: target_find_new_threads
5039 * Query all the threads that the target knows about,
5040 * and give them back to GDB to add to its list.
5044 procfs_find_new_threads (void)
5048 /* Find procinfo for main process */
5049 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5050 proc_update_threads (pi
);
5051 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
5055 * Function: target_thread_alive
5057 * Return true if the thread is still 'alive'.
5059 * This guy doesn't really seem to be doing his job.
5060 * Got to investigate how to tell when a thread is really gone.
5064 procfs_thread_alive (ptid_t ptid
)
5069 proc
= PIDGET (ptid
);
5070 thread
= TIDGET (ptid
);
5071 /* If I don't know it, it ain't alive! */
5072 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
5075 /* If I can't get its status, it ain't alive!
5076 What's more, I need to forget about it! */
5077 if (!proc_get_status (pi
))
5079 destroy_procinfo (pi
);
5082 /* I couldn't have got its status if it weren't alive, so it's alive. */
5087 * Function: target_pid_to_str
5089 * Return a string to be used to identify the thread in
5090 * the "info threads" display.
5094 procfs_pid_to_str (ptid_t ptid
)
5096 static char buf
[80];
5100 proc
= PIDGET (ptid
);
5101 thread
= TIDGET (ptid
);
5102 pi
= find_procinfo (proc
, thread
);
5105 sprintf (buf
, "Process %d", proc
);
5107 sprintf (buf
, "LWP %d", thread
);
5112 * Function: procfs_set_watchpoint
5113 * Insert a watchpoint
5117 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
5125 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5126 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5128 /* Translate from GDB's flags to /proc's */
5129 if (len
> 0) /* len == 0 means delete watchpoint */
5131 switch (rwflag
) { /* FIXME: need an enum! */
5132 case hw_write
: /* default watchpoint (write) */
5133 pflags
= WRITE_WATCHFLAG
;
5135 case hw_read
: /* read watchpoint */
5136 pflags
= READ_WATCHFLAG
;
5138 case hw_access
: /* access watchpoint */
5139 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
5141 case hw_execute
: /* execution HW breakpoint */
5142 pflags
= EXEC_WATCHFLAG
;
5144 default: /* Something weird. Return error. */
5147 if (after
) /* Stop after r/w access is completed. */
5148 pflags
|= AFTER_WATCHFLAG
;
5151 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
5153 if (errno
== E2BIG
) /* Typical error for no resources */
5154 return -1; /* fail */
5155 /* GDB may try to remove the same watchpoint twice.
5156 If a remove request returns no match, don't error. */
5157 if (errno
== ESRCH
&& len
== 0)
5158 return 0; /* ignore */
5159 proc_error (pi
, "set_watchpoint", __LINE__
);
5162 #endif /* UNIXWARE */
5166 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
5167 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
5168 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
5171 Note: procfs_can_use_hw_breakpoint() is not yet used by all
5172 procfs.c targets due to the fact that some of them still define
5173 TARGET_CAN_USE_HARDWARE_WATCHPOINT. */
5176 procfs_can_use_hw_breakpoint (int type
, int cnt
, int othertype
)
5178 #ifndef TARGET_HAS_HARDWARE_WATCHPOINTS
5181 /* Due to the way that proc_set_watchpoint() is implemented, host
5182 and target pointers must be of the same size. If they are not,
5183 we can't use hardware watchpoints. This limitation is due to the
5184 fact that proc_set_watchpoint() calls
5185 procfs_address_to_host_pointer(); a close inspection of
5186 procfs_address_to_host_pointer will reveal that an internal error
5187 will be generated when the host and target pointer sizes are
5189 if (sizeof (void *) != TYPE_LENGTH (builtin_type_void_data_ptr
))
5192 /* Other tests here??? */
5199 * Function: stopped_by_watchpoint
5201 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5202 * else returns zero.
5206 procfs_stopped_by_watchpoint (ptid_t ptid
)
5210 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5211 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5213 if (!pi
) /* If no process, then not stopped by watchpoint! */
5216 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
5218 if (proc_why (pi
) == PR_FAULTED
)
5221 if (proc_what (pi
) == FLTWATCH
)
5225 if (proc_what (pi
) == FLTKWATCH
)
5233 #ifdef TM_I386SOL2_H
5235 * Function: procfs_find_LDT_entry
5238 * ptid_t ptid; // The GDB-style pid-plus-LWP.
5241 * pointer to the corresponding LDT entry.
5245 procfs_find_LDT_entry (ptid_t ptid
)
5247 gdb_gregset_t
*gregs
;
5251 /* Find procinfo for the lwp. */
5252 if ((pi
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
))) == NULL
)
5254 warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.",
5255 PIDGET (ptid
), TIDGET (ptid
));
5258 /* get its general registers. */
5259 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
5261 warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.",
5262 PIDGET (ptid
), TIDGET (ptid
));
5265 /* Now extract the GS register's lower 16 bits. */
5266 key
= (*gregs
)[GS
] & 0xffff;
5268 /* Find the matching entry and return it. */
5269 return proc_get_LDT_entry (pi
, key
);
5271 #endif /* TM_I386SOL2_H */
5274 * Memory Mappings Functions:
5278 * Function: iterate_over_mappings
5280 * Call a callback function once for each mapping, passing it the mapping,
5281 * an optional secondary callback function, and some optional opaque data.
5282 * Quit and return the first non-zero value returned from the callback.
5285 * pi -- procinfo struct for the process to be mapped.
5286 * func -- callback function to be called by this iterator.
5287 * data -- optional opaque data to be passed to the callback function.
5288 * child_func -- optional secondary function pointer to be passed
5289 * to the child function.
5291 * Return: First non-zero return value from the callback function,
5296 iterate_over_mappings (procinfo
*pi
, int (*child_func
) (), void *data
,
5297 int (*func
) (struct prmap
*map
,
5298 int (*child_func
) (),
5301 char pathname
[MAX_PROC_NAME_SIZE
];
5302 struct prmap
*prmaps
;
5303 struct prmap
*prmap
;
5311 /* Get the number of mappings, allocate space,
5312 and read the mappings into prmaps. */
5315 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
5316 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
5317 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
5319 /* Make sure it gets closed again. */
5320 make_cleanup_close (map_fd
);
5322 /* Use stat to determine the file size, and compute
5323 the number of prmap_t objects it contains. */
5324 if (fstat (map_fd
, &sbuf
) != 0)
5325 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
5327 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
5328 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5329 if (read (map_fd
, (char *) prmaps
, nmap
* sizeof (*prmaps
))
5330 != (nmap
* sizeof (*prmaps
)))
5331 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
5333 /* Use ioctl command PIOCNMAP to get number of mappings. */
5334 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmap
) != 0)
5335 proc_error (pi
, "iterate_over_mappings (PIOCNMAP)", __LINE__
);
5337 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5338 if (ioctl (pi
->ctl_fd
, PIOCMAP
, prmaps
) != 0)
5339 proc_error (pi
, "iterate_over_mappings (PIOCMAP)", __LINE__
);
5342 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
5343 if ((funcstat
= (*func
) (prmap
, child_func
, data
)) != 0)
5350 * Function: solib_mappings_callback
5352 * Calls the supplied callback function once for each mapped address
5353 * space in the process. The callback function receives an open
5354 * file descriptor for the file corresponding to that mapped
5355 * address space (if there is one), and the base address of the
5356 * mapped space. Quit when the callback function returns a
5357 * nonzero value, or at teh end of the mappings.
5359 * Returns: the first non-zero return value of the callback function,
5363 int solib_mappings_callback (struct prmap
*map
,
5364 int (*func
) (int, CORE_ADDR
),
5367 procinfo
*pi
= data
;
5371 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
5373 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
5374 return -1; /* sanity */
5376 if (map
->pr_mapname
[0] == 0)
5378 fd
= -1; /* no map file */
5382 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
5383 /* Note: caller's responsibility to close this fd! */
5384 fd
= open_with_retry (name
, O_RDONLY
);
5385 /* Note: we don't test the above call for failure;
5386 we just pass the FD on as given. Sometimes there is
5387 no file, so the open may return failure, but that's
5391 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
->pr_vaddr
);
5392 /* Note: we don't test the above call for failure;
5393 we just pass the FD on as given. Sometimes there is
5394 no file, so the ioctl may return failure, but that's
5397 return (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
);
5401 * Function: proc_iterate_over_mappings
5403 * Uses the unified "iterate_over_mappings" function
5404 * to implement the exported interface to solib-svr4.c.
5406 * Given a pointer to a function, call that function once for every
5407 * mapped address space in the process. The callback function
5408 * receives an open file descriptor for the file corresponding to
5409 * that mapped address space (if there is one), and the base address
5410 * of the mapped space. Quit when the callback function returns a
5411 * nonzero value, or at teh end of the mappings.
5413 * Returns: the first non-zero return value of the callback function,
5418 proc_iterate_over_mappings (int (*func
) (int, CORE_ADDR
))
5420 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5422 return iterate_over_mappings (pi
, func
, pi
, solib_mappings_callback
);
5426 * Function: find_memory_regions_callback
5428 * Implements the to_find_memory_regions method.
5429 * Calls an external function for each memory region.
5430 * External function will have the signiture:
5432 * int callback (CORE_ADDR vaddr,
5433 * unsigned long size,
5434 * int read, int write, int execute,
5437 * Returns the integer value returned by the callback.
5441 find_memory_regions_callback (struct prmap
*map
,
5442 int (*func
) (CORE_ADDR
,
5448 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
5450 (map
->pr_mflags
& MA_READ
) != 0,
5451 (map
->pr_mflags
& MA_WRITE
) != 0,
5452 (map
->pr_mflags
& MA_EXEC
) != 0,
5457 * Function: proc_find_memory_regions
5459 * External interface. Calls a callback function once for each
5460 * mapped memory region in the child process, passing as arguments
5461 * CORE_ADDR virtual_address,
5462 * unsigned long size,
5463 * int read, TRUE if region is readable by the child
5464 * int write, TRUE if region is writable by the child
5465 * int execute TRUE if region is executable by the child.
5467 * Stops iterating and returns the first non-zero value
5468 * returned by the callback.
5472 proc_find_memory_regions (int (*func
) (CORE_ADDR
,
5478 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5480 return iterate_over_mappings (pi
, func
, data
,
5481 find_memory_regions_callback
);
5485 * Function: mappingflags
5487 * Returns an ascii representation of a memory mapping's flags.
5491 mappingflags (long flags
)
5493 static char asciiflags
[8];
5495 strcpy (asciiflags
, "-------");
5496 #if defined (MA_PHYS)
5497 if (flags
& MA_PHYS
)
5498 asciiflags
[0] = 'd';
5500 if (flags
& MA_STACK
)
5501 asciiflags
[1] = 's';
5502 if (flags
& MA_BREAK
)
5503 asciiflags
[2] = 'b';
5504 if (flags
& MA_SHARED
)
5505 asciiflags
[3] = 's';
5506 if (flags
& MA_READ
)
5507 asciiflags
[4] = 'r';
5508 if (flags
& MA_WRITE
)
5509 asciiflags
[5] = 'w';
5510 if (flags
& MA_EXEC
)
5511 asciiflags
[6] = 'x';
5512 return (asciiflags
);
5516 * Function: info_mappings_callback
5518 * Callback function, does the actual work for 'info proc mappings'.
5522 info_mappings_callback (struct prmap
*map
, int (*ignore
) (), void *unused
)
5524 char *data_fmt_string
;
5526 if (TARGET_ADDR_BIT
== 32)
5527 data_fmt_string
= "\t%#10lx %#10lx %#10x %#10x %7s\n";
5529 data_fmt_string
= " %#18lx %#18lx %#10x %#10x %7s\n";
5531 printf_filtered (data_fmt_string
,
5532 (unsigned long) map
->pr_vaddr
,
5533 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5535 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5536 (unsigned int) map
->pr_offset
,
5540 mappingflags (map
->pr_mflags
));
5546 * Function: info_proc_mappings
5548 * Implement the "info proc mappings" subcommand.
5552 info_proc_mappings (procinfo
*pi
, int summary
)
5554 char *header_fmt_string
;
5556 if (TARGET_PTR_BIT
== 32)
5557 header_fmt_string
= "\t%10s %10s %10s %10s %7s\n";
5559 header_fmt_string
= " %18s %18s %10s %10s %7s\n";
5562 return; /* No output for summary mode. */
5564 printf_filtered ("Mapped address spaces:\n\n");
5565 printf_filtered (header_fmt_string
,
5572 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
5573 printf_filtered ("\n");
5577 * Function: info_proc_cmd
5579 * Implement the "info proc" command.
5583 info_proc_cmd (char *args
, int from_tty
)
5585 struct cleanup
*old_chain
;
5586 procinfo
*process
= NULL
;
5587 procinfo
*thread
= NULL
;
5594 old_chain
= make_cleanup (null_cleanup
, 0);
5597 if ((argv
= buildargv (args
)) == NULL
)
5600 make_cleanup_freeargv (argv
);
5602 while (argv
!= NULL
&& *argv
!= NULL
)
5604 if (isdigit (argv
[0][0]))
5606 pid
= strtoul (argv
[0], &tmp
, 10);
5608 tid
= strtoul (++tmp
, NULL
, 10);
5610 else if (argv
[0][0] == '/')
5612 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5614 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
5625 pid
= PIDGET (inferior_ptid
);
5627 error ("No current process: you must name one.");
5630 /* Have pid, will travel.
5631 First see if it's a process we're already debugging. */
5632 process
= find_procinfo (pid
, 0);
5633 if (process
== NULL
)
5635 /* No. So open a procinfo for it, but
5636 remember to close it again when finished. */
5637 process
= create_procinfo (pid
, 0);
5638 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5639 if (!open_procinfo_files (process
, FD_CTL
))
5640 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5644 thread
= create_procinfo (pid
, tid
);
5648 printf_filtered ("process %d flags:\n", process
->pid
);
5649 proc_prettyprint_flags (proc_flags (process
), 1);
5650 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5651 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5652 if (proc_get_nthreads (process
) > 1)
5653 printf_filtered ("Process has %d threads.\n",
5654 proc_get_nthreads (process
));
5658 printf_filtered ("thread %d flags:\n", thread
->tid
);
5659 proc_prettyprint_flags (proc_flags (thread
), 1);
5660 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5661 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5666 info_proc_mappings (process
, 0);
5669 do_cleanups (old_chain
);
5673 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5679 if (PIDGET (inferior_ptid
) <= 0)
5680 error ("you must be debugging a process to use this command.");
5682 if (args
== NULL
|| args
[0] == 0)
5683 error_no_arg ("system call to trace");
5685 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5686 if (isdigit (args
[0]))
5688 syscallnum
= atoi (args
);
5689 if (entry_or_exit
== PR_SYSENTRY
)
5690 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5692 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5695 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5697 if (mode
== FLAG_SET
)
5698 gdb_praddsysset (sysset
, syscallnum
);
5700 gdb_prdelsysset (sysset
, syscallnum
);
5702 if (entry_or_exit
== PR_SYSENTRY
)
5704 if (!proc_set_traced_sysentry (pi
, sysset
))
5705 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5709 if (!proc_set_traced_sysexit (pi
, sysset
))
5710 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5716 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5718 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5722 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5724 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5728 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5730 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5734 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5736 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5741 _initialize_procfs (void)
5744 add_target (&procfs_ops
);
5745 add_info ("proc", info_proc_cmd
,
5746 "Show /proc process information about any running process.\n\
5747 Specify process id, or use the program being debugged by default.\n\
5748 Specify keyword 'mappings' for detailed info on memory mappings.");
5749 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5750 "Give a trace of entries into the syscall.");
5751 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5752 "Give a trace of exits from the syscall.");
5753 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5754 "Cancel a trace of entries into the syscall.");
5755 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5756 "Cancel a trace of exits from the syscall.");
5759 /* =================== END, GDB "MODULE" =================== */
5763 /* miscellaneous stubs: */
5764 /* The following satisfy a few random symbols mostly created by */
5765 /* the solaris threads implementation, which I will chase down */
5769 * Return a pid for which we guarantee
5770 * we will be able to find a 'live' procinfo.
5774 procfs_first_available (void)
5776 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);
5779 /* =================== GCORE .NOTE "MODULE" =================== */
5780 #if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT)
5781 /* gcore only implemented on solaris and unixware (so far) */
5784 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
5785 char *note_data
, int *note_size
)
5787 gdb_gregset_t gregs
;
5788 gdb_fpregset_t fpregs
;
5789 unsigned long merged_pid
;
5791 merged_pid
= TIDGET (ptid
) << 16 | PIDGET (ptid
);
5793 fill_gregset (&gregs
, -1);
5794 #if defined (UNIXWARE)
5795 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
5802 note_data
= (char *) elfcore_write_prstatus (obfd
,
5809 fill_fpregset (&fpregs
, -1);
5810 note_data
= (char *) elfcore_write_prfpreg (obfd
,
5818 struct procfs_corefile_thread_data
{
5825 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
5827 struct procfs_corefile_thread_data
*args
= data
;
5829 if (pi
!= NULL
&& thread
->tid
!= 0)
5831 ptid_t saved_ptid
= inferior_ptid
;
5832 inferior_ptid
= MERGEPID (pi
->pid
, thread
->tid
);
5833 args
->note_data
= procfs_do_thread_registers (args
->obfd
, inferior_ptid
,
5836 inferior_ptid
= saved_ptid
;
5842 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5844 struct cleanup
*old_chain
;
5845 gdb_gregset_t gregs
;
5846 gdb_fpregset_t fpregs
;
5847 char fname
[16] = {'\0'};
5848 char psargs
[80] = {'\0'};
5849 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5850 char *note_data
= NULL
;
5852 struct procfs_corefile_thread_data thread_args
;
5854 if (get_exec_file (0))
5856 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
5857 strncpy (psargs
, get_exec_file (0),
5860 inf_args
= get_inferior_args ();
5861 if (inf_args
&& *inf_args
&&
5862 strlen (inf_args
) < ((int) sizeof (psargs
) - (int) strlen (psargs
)))
5864 strncat (psargs
, " ",
5865 sizeof (psargs
) - strlen (psargs
));
5866 strncat (psargs
, inf_args
,
5867 sizeof (psargs
) - strlen (psargs
));
5871 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
5878 fill_gregset (&gregs
, -1);
5879 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
5880 PIDGET (inferior_ptid
),
5881 stop_signal
, &gregs
);
5884 thread_args
.obfd
= obfd
;
5885 thread_args
.note_data
= note_data
;
5886 thread_args
.note_size
= note_size
;
5887 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
, &thread_args
);
5889 if (thread_args
.note_data
== note_data
)
5891 /* iterate_over_threads didn't come up with any threads;
5892 just use inferior_ptid. */
5893 note_data
= procfs_do_thread_registers (obfd
, inferior_ptid
,
5894 note_data
, note_size
);
5898 note_data
= thread_args
.note_data
;
5901 make_cleanup (xfree
, note_data
);
5904 #else /* !(Solaris or Unixware) */
5906 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5908 error ("gcore not implemented for this host.");
5909 return NULL
; /* lint */
5911 #endif /* Solaris or Unixware */
5912 /* =================== END GCORE .NOTE "MODULE" =================== */