1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
2 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Written by Michael Snyder at Cygnus Solutions.
4 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "gdbthread.h"
29 #if defined (NEW_PROC_API)
30 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
33 #include <sys/procfs.h>
34 #ifdef HAVE_SYS_FAULT_H
35 #include <sys/fault.h>
37 #ifdef HAVE_SYS_SYSCALL_H
38 #include <sys/syscall.h>
40 #include <sys/errno.h>
48 * This module provides the interface between GDB and the
49 * /proc file system, which is used on many versions of Unix
50 * as a means for debuggers to control other processes.
51 * Examples of the systems that use this interface are:
58 * /proc works by immitating a file system: you open a simulated file
59 * that represents the process you wish to interact with, and
60 * perform operations on that "file" in order to examine or change
61 * the state of the other process.
63 * The most important thing to know about /proc and this module
64 * is that there are two very different interfaces to /proc:
65 * One that uses the ioctl system call, and
66 * another that uses read and write system calls.
67 * This module has to support both /proc interfaces. This means
68 * that there are two different ways of doing every basic operation.
70 * In order to keep most of the code simple and clean, I have
71 * defined an interface "layer" which hides all these system calls.
72 * An ifdef (NEW_PROC_API) determines which interface we are using,
73 * and most or all occurrances of this ifdef should be confined to
74 * this interface layer.
78 /* Determine which /proc API we are using:
79 The ioctl API defines PIOCSTATUS, while
80 the read/write (multiple fd) API never does. */
83 #include <sys/types.h>
84 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
87 #include <fcntl.h> /* for O_RDONLY */
88 #include <unistd.h> /* for "X_OK" */
89 #include "gdb_stat.h" /* for struct stat */
91 /* Note: procfs-utils.h must be included after the above system header
92 files, because it redefines various system calls using macros.
93 This may be incompatible with the prototype declarations. */
95 #include "proc-utils.h"
97 /* Prototypes for supply_gregset etc. */
100 /* =================== TARGET_OPS "MODULE" =================== */
103 * This module defines the GDB target vector and its methods.
106 static void procfs_open (char *, int);
107 static void procfs_attach (char *, int);
108 static void procfs_detach (char *, int);
109 static void procfs_resume (ptid_t
, int, enum target_signal
);
110 static int procfs_can_run (void);
111 static void procfs_stop (void);
112 static void procfs_files_info (struct target_ops
*);
113 static void procfs_fetch_registers (int);
114 static void procfs_store_registers (int);
115 static void procfs_notice_signals (ptid_t
);
116 static void procfs_prepare_to_store (void);
117 static void procfs_kill_inferior (void);
118 static void procfs_mourn_inferior (void);
119 static void procfs_create_inferior (char *, char *, char **);
120 static ptid_t
procfs_wait (ptid_t
, struct target_waitstatus
*);
121 static int procfs_xfer_memory (CORE_ADDR
, char *, int, int,
122 struct mem_attrib
*attrib
,
123 struct target_ops
*);
125 static int procfs_thread_alive (ptid_t
);
127 void procfs_find_new_threads (void);
128 char *procfs_pid_to_str (ptid_t
);
130 static int proc_find_memory_regions (int (*) (CORE_ADDR
,
136 static char * procfs_make_note_section (bfd
*, int *);
138 struct target_ops procfs_ops
; /* the target vector */
141 init_procfs_ops (void)
143 procfs_ops
.to_shortname
= "procfs";
144 procfs_ops
.to_longname
= "Unix /proc child process";
146 "Unix /proc child process (started by the \"run\" command).";
147 procfs_ops
.to_open
= procfs_open
;
148 procfs_ops
.to_can_run
= procfs_can_run
;
149 procfs_ops
.to_create_inferior
= procfs_create_inferior
;
150 procfs_ops
.to_kill
= procfs_kill_inferior
;
151 procfs_ops
.to_mourn_inferior
= procfs_mourn_inferior
;
152 procfs_ops
.to_attach
= procfs_attach
;
153 procfs_ops
.to_detach
= procfs_detach
;
154 procfs_ops
.to_wait
= procfs_wait
;
155 procfs_ops
.to_resume
= procfs_resume
;
156 procfs_ops
.to_prepare_to_store
= procfs_prepare_to_store
;
157 procfs_ops
.to_fetch_registers
= procfs_fetch_registers
;
158 procfs_ops
.to_store_registers
= procfs_store_registers
;
159 procfs_ops
.to_xfer_memory
= procfs_xfer_memory
;
160 procfs_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
161 procfs_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
162 procfs_ops
.to_notice_signals
= procfs_notice_signals
;
163 procfs_ops
.to_files_info
= procfs_files_info
;
164 procfs_ops
.to_stop
= procfs_stop
;
166 procfs_ops
.to_terminal_init
= terminal_init_inferior
;
167 procfs_ops
.to_terminal_inferior
= terminal_inferior
;
168 procfs_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
169 procfs_ops
.to_terminal_ours
= terminal_ours
;
170 procfs_ops
.to_terminal_info
= child_terminal_info
;
172 procfs_ops
.to_find_new_threads
= procfs_find_new_threads
;
173 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
174 procfs_ops
.to_pid_to_str
= procfs_pid_to_str
;
176 procfs_ops
.to_has_all_memory
= 1;
177 procfs_ops
.to_has_memory
= 1;
178 procfs_ops
.to_has_execution
= 1;
179 procfs_ops
.to_has_stack
= 1;
180 procfs_ops
.to_has_registers
= 1;
181 procfs_ops
.to_stratum
= process_stratum
;
182 procfs_ops
.to_has_thread_control
= tc_schedlock
;
183 procfs_ops
.to_find_memory_regions
= proc_find_memory_regions
;
184 procfs_ops
.to_make_corefile_notes
= procfs_make_note_section
;
185 procfs_ops
.to_magic
= OPS_MAGIC
;
188 /* =================== END, TARGET_OPS "MODULE" =================== */
193 * Put any typedefs, defines etc. here that are required for
194 * the unification of code that handles different versions of /proc.
197 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
199 enum { READ_WATCHFLAG
= WA_READ
,
200 WRITE_WATCHFLAG
= WA_WRITE
,
201 EXEC_WATCHFLAG
= WA_EXEC
,
202 AFTER_WATCHFLAG
= WA_TRAPAFTER
205 #else /* Irix method for watchpoints */
206 enum { READ_WATCHFLAG
= MA_READ
,
207 WRITE_WATCHFLAG
= MA_WRITE
,
208 EXEC_WATCHFLAG
= MA_EXEC
,
209 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
214 #ifdef HAVE_PR_SIGSET_T
215 typedef pr_sigset_t gdb_sigset_t
;
217 typedef sigset_t gdb_sigset_t
;
221 #ifdef HAVE_PR_SIGACTION64_T
222 typedef pr_sigaction64_t gdb_sigaction_t
;
224 typedef struct sigaction gdb_sigaction_t
;
228 #ifdef HAVE_PR_SIGINFO64_T
229 typedef pr_siginfo64_t gdb_siginfo_t
;
231 typedef struct siginfo gdb_siginfo_t
;
234 /* gdb_premptysysset */
236 #define gdb_premptysysset premptysysset
238 #define gdb_premptysysset premptyset
243 #define gdb_praddsysset praddsysset
245 #define gdb_praddsysset praddset
250 #define gdb_prdelsysset prdelsysset
252 #define gdb_prdelsysset prdelset
255 /* prissyssetmember */
256 #ifdef prissyssetmember
257 #define gdb_pr_issyssetmember prissyssetmember
259 #define gdb_pr_issyssetmember prismember
262 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
263 as intuitively descriptive as it could be, so we'll define
264 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
265 this writing, this feature is only found on AIX5 systems and
266 basically means that the set of syscalls is not fixed. I.e,
267 there's no nice table that one can #include to get all of the
268 syscall numbers. Instead, they're stored in /proc/PID/sysent
269 for each process. We are at least guaranteed that they won't
270 change over the lifetime of the process. But each process could
271 (in theory) have different syscall numbers.
273 #ifdef HAVE_PRSYSENT_T
274 #define DYNAMIC_SYSCALLS
279 /* =================== STRUCT PROCINFO "MODULE" =================== */
281 /* FIXME: this comment will soon be out of date W.R.T. threads. */
283 /* The procinfo struct is a wrapper to hold all the state information
284 concerning a /proc process. There should be exactly one procinfo
285 for each process, and since GDB currently can debug only one
286 process at a time, that means there should be only one procinfo.
287 All of the LWP's of a process can be accessed indirectly thru the
288 single process procinfo.
290 However, against the day when GDB may debug more than one process,
291 this data structure is kept in a list (which for now will hold no
292 more than one member), and many functions will have a pointer to a
293 procinfo as an argument.
295 There will be a separate procinfo structure for use by the (not yet
296 implemented) "info proc" command, so that we can print useful
297 information about any random process without interfering with the
298 inferior's procinfo information. */
301 /* format strings for /proc paths */
302 # ifndef CTL_PROC_NAME_FMT
303 # define MAIN_PROC_NAME_FMT "/proc/%d"
304 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
305 # define AS_PROC_NAME_FMT "/proc/%d/as"
306 # define MAP_PROC_NAME_FMT "/proc/%d/map"
307 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
308 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
310 /* the name of the proc status struct depends on the implementation */
311 typedef pstatus_t gdb_prstatus_t
;
312 typedef lwpstatus_t gdb_lwpstatus_t
;
313 #else /* ! NEW_PROC_API */
314 /* format strings for /proc paths */
315 # ifndef CTL_PROC_NAME_FMT
316 # define MAIN_PROC_NAME_FMT "/proc/%05d"
317 # define CTL_PROC_NAME_FMT "/proc/%05d"
318 # define AS_PROC_NAME_FMT "/proc/%05d"
319 # define MAP_PROC_NAME_FMT "/proc/%05d"
320 # define STATUS_PROC_NAME_FMT "/proc/%05d"
321 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
323 /* the name of the proc status struct depends on the implementation */
324 typedef prstatus_t gdb_prstatus_t
;
325 typedef prstatus_t gdb_lwpstatus_t
;
326 #endif /* NEW_PROC_API */
328 typedef struct procinfo
{
329 struct procinfo
*next
;
330 int pid
; /* Process ID */
331 int tid
; /* Thread/LWP id */
335 int ignore_next_sigstop
;
337 /* The following four fd fields may be identical, or may contain
338 several different fd's, depending on the version of /proc
339 (old ioctl or new read/write). */
341 int ctl_fd
; /* File descriptor for /proc control file */
343 * The next three file descriptors are actually only needed in the
344 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
345 * However, to avoid a bunch of #ifdefs in the code, we will use
346 * them uniformly by (in the case of the ioctl single-file-descriptor
347 * implementation) filling them with copies of the control fd.
349 int status_fd
; /* File descriptor for /proc status file */
350 int as_fd
; /* File descriptor for /proc as file */
352 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
354 fltset_t saved_fltset
; /* Saved traced hardware fault set */
355 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
356 gdb_sigset_t saved_sighold
; /* Saved held signal set */
357 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
358 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
360 gdb_prstatus_t prstatus
; /* Current process status info */
363 gdb_fpregset_t fpregset
; /* Current floating point registers */
366 #ifdef DYNAMIC_SYSCALLS
367 int num_syscalls
; /* Total number of syscalls */
368 char **syscall_names
; /* Syscall number to name map */
371 struct procinfo
*thread_list
;
373 int status_valid
: 1;
375 int fpregs_valid
: 1;
376 int threads_valid
: 1;
379 static char errmsg
[128]; /* shared error msg buffer */
381 /* Function prototypes for procinfo module: */
383 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
384 static procinfo
*find_procinfo (int pid
, int tid
);
385 static procinfo
*create_procinfo (int pid
, int tid
);
386 static void destroy_procinfo (procinfo
* p
);
387 static void do_destroy_procinfo_cleanup (void *);
388 static void dead_procinfo (procinfo
* p
, char *msg
, int killp
);
389 static int open_procinfo_files (procinfo
* p
, int which
);
390 static void close_procinfo_files (procinfo
* p
);
391 static int sysset_t_size (procinfo
*p
);
392 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
393 #ifdef DYNAMIC_SYSCALLS
394 static void load_syscalls (procinfo
*pi
);
395 static void free_syscalls (procinfo
*pi
);
396 static int find_syscall (procinfo
*pi
, char *name
);
397 #endif /* DYNAMIC_SYSCALLS */
399 /* The head of the procinfo list: */
400 static procinfo
* procinfo_list
;
403 * Function: find_procinfo
405 * Search the procinfo list.
407 * Returns: pointer to procinfo, or NULL if not found.
411 find_procinfo (int pid
, int tid
)
415 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
422 /* Don't check threads_valid. If we're updating the
423 thread_list, we want to find whatever threads are already
424 here. This means that in general it is the caller's
425 responsibility to check threads_valid and update before
426 calling find_procinfo, if the caller wants to find a new
429 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
438 * Function: find_procinfo_or_die
440 * Calls find_procinfo, but errors on failure.
444 find_procinfo_or_die (int pid
, int tid
)
446 procinfo
*pi
= find_procinfo (pid
, tid
);
451 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
454 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
459 /* open_with_retry() is a wrapper for open(). The appropriate
460 open() call is attempted; if unsuccessful, it will be retried as
461 many times as needed for the EAGAIN and EINTR conditions.
463 For other conditions, open_with_retry() will retry the open() a
464 limited number of times. In addition, a short sleep is imposed
465 prior to retrying the open(). The reason for this sleep is to give
466 the kernel a chance to catch up and create the file in question in
467 the event that GDB "wins" the race to open a file before the kernel
471 open_with_retry (const char *pathname
, int flags
)
473 int retries_remaining
, status
;
475 retries_remaining
= 2;
479 status
= open (pathname
, flags
);
481 if (status
>= 0 || retries_remaining
== 0)
483 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
494 * Function: open_procinfo_files
496 * Open the file descriptor for the process or LWP.
497 * ifdef NEW_PROC_API, we only open the control file descriptor;
498 * the others are opened lazily as needed.
499 * else (if not NEW_PROC_API), there is only one real
500 * file descriptor, but we keep multiple copies of it so that
501 * the code that uses them does not have to be #ifdef'd.
503 * Return: file descriptor, or zero for failure.
506 enum { FD_CTL
, FD_STATUS
, FD_AS
};
509 open_procinfo_files (procinfo
*pi
, int which
)
512 char tmp
[MAX_PROC_NAME_SIZE
];
517 * This function is getting ALMOST long enough to break up into several.
518 * Here is some rationale:
520 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
521 * There are several file descriptors that may need to be open
522 * for any given process or LWP. The ones we're intereted in are:
523 * - control (ctl) write-only change the state
524 * - status (status) read-only query the state
525 * - address space (as) read/write access memory
526 * - map (map) read-only virtual addr map
527 * Most of these are opened lazily as they are needed.
528 * The pathnames for the 'files' for an LWP look slightly
529 * different from those of a first-class process:
530 * Pathnames for a process (<proc-id>):
531 * /proc/<proc-id>/ctl
532 * /proc/<proc-id>/status
534 * /proc/<proc-id>/map
535 * Pathnames for an LWP (lwp-id):
536 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
537 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
538 * An LWP has no map or address space file descriptor, since
539 * the memory map and address space are shared by all LWPs.
541 * Everyone else (Solaris 2.5, Irix, OSF)
542 * There is only one file descriptor for each process or LWP.
543 * For convenience, we copy the same file descriptor into all
544 * three fields of the procinfo struct (ctl_fd, status_fd, and
545 * as_fd, see NEW_PROC_API above) so that code that uses them
546 * doesn't need any #ifdef's.
551 * Each LWP has an independent file descriptor, but these
552 * are not obtained via the 'open' system call like the rest:
553 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
554 * to the file descriptor of the parent process.
557 * These do not even have their own independent file descriptor.
558 * All operations are carried out on the file descriptor of the
559 * parent process. Therefore we just call open again for each
560 * thread, getting a new handle for the same 'file'.
565 * In this case, there are several different file descriptors that
566 * we might be asked to open. The control file descriptor will be
567 * opened early, but the others will be opened lazily as they are
571 strcpy (tmp
, pi
->pathname
);
572 switch (which
) { /* which file descriptor to open? */
575 strcat (tmp
, "/lwpctl");
577 strcat (tmp
, "/ctl");
578 fd
= open_with_retry (tmp
, O_WRONLY
);
585 return 0; /* there is no 'as' file descriptor for an lwp */
587 fd
= open_with_retry (tmp
, O_RDWR
);
594 strcat (tmp
, "/lwpstatus");
596 strcat (tmp
, "/status");
597 fd
= open_with_retry (tmp
, O_RDONLY
);
603 return 0; /* unknown file descriptor */
605 #else /* not NEW_PROC_API */
607 * In this case, there is only one file descriptor for each procinfo
608 * (ie. each process or LWP). In fact, only the file descriptor for
609 * the process can actually be opened by an 'open' system call.
610 * The ones for the LWPs have to be obtained thru an IOCTL call
611 * on the process's file descriptor.
613 * For convenience, we copy each procinfo's single file descriptor
614 * into all of the fields occupied by the several file descriptors
615 * of the NEW_PROC_API implementation. That way, the code that uses
616 * them can be written without ifdefs.
620 #ifdef PIOCTSTATUS /* OSF */
621 /* Only one FD; just open it. */
622 if ((fd
= open_with_retry (pi
->pathname
, O_RDWR
)) == 0)
624 #else /* Sol 2.5, Irix, other? */
625 if (pi
->tid
== 0) /* Master procinfo for the process */
627 fd
= open_with_retry (pi
->pathname
, O_RDWR
);
631 else /* LWP thread procinfo */
633 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
637 /* Find the procinfo for the entire process. */
638 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
641 /* Now obtain the file descriptor for the LWP. */
642 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
644 #else /* Irix, other? */
645 return 0; /* Don't know how to open threads */
646 #endif /* Sol 2.5 PIOCOPENLWP */
648 #endif /* OSF PIOCTSTATUS */
649 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
650 #endif /* NEW_PROC_API */
652 return 1; /* success */
656 * Function: create_procinfo
658 * Allocate a data structure and link it into the procinfo list.
659 * (First tries to find a pre-existing one (FIXME: why?)
661 * Return: pointer to new procinfo struct.
665 create_procinfo (int pid
, int tid
)
667 procinfo
*pi
, *parent
;
669 if ((pi
= find_procinfo (pid
, tid
)))
670 return pi
; /* Already exists, nothing to do. */
672 /* find parent before doing malloc, to save having to cleanup */
674 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
676 doesn't exist yet? */
678 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
679 memset (pi
, 0, sizeof (procinfo
));
683 #ifdef DYNAMIC_SYSCALLS
687 pi
->saved_entryset
= sysset_t_alloc (pi
);
688 pi
->saved_exitset
= sysset_t_alloc (pi
);
690 /* Chain into list. */
693 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
694 pi
->next
= procinfo_list
;
700 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
702 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
704 pi
->next
= parent
->thread_list
;
705 parent
->thread_list
= pi
;
711 * Function: close_procinfo_files
713 * Close all file descriptors associated with the procinfo
717 close_procinfo_files (procinfo
*pi
)
724 if (pi
->status_fd
> 0)
725 close (pi
->status_fd
);
727 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
731 * Function: destroy_procinfo
733 * Destructor function. Close, unlink and deallocate the object.
737 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
741 /* Step one: unlink the procinfo from its list */
745 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
748 ptr
->next
= pi
->next
;
752 /* Step two: close any open file descriptors */
753 close_procinfo_files (pi
);
755 /* Step three: free the memory. */
756 #ifdef DYNAMIC_SYSCALLS
759 xfree (pi
->saved_entryset
);
760 xfree (pi
->saved_exitset
);
765 destroy_procinfo (procinfo
*pi
)
769 if (pi
->tid
!= 0) /* destroy a thread procinfo */
771 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
772 destroy_one_procinfo (&tmp
->thread_list
, pi
);
774 else /* destroy a process procinfo and all its threads */
776 /* First destroy the children, if any; */
777 while (pi
->thread_list
!= NULL
)
778 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
779 /* Then destroy the parent. Genocide!!! */
780 destroy_one_procinfo (&procinfo_list
, pi
);
785 do_destroy_procinfo_cleanup (void *pi
)
787 destroy_procinfo (pi
);
790 enum { NOKILL
, KILL
};
793 * Function: dead_procinfo
795 * To be called on a non_recoverable error for a procinfo.
796 * Prints error messages, optionally sends a SIGKILL to the process,
797 * then destroys the data structure.
801 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
807 print_sys_errmsg (pi
->pathname
, errno
);
811 sprintf (procfile
, "process %d", pi
->pid
);
812 print_sys_errmsg (procfile
, errno
);
815 kill (pi
->pid
, SIGKILL
);
817 destroy_procinfo (pi
);
822 * Function: sysset_t_size
824 * Returns the (complete) size of a sysset_t struct. Normally, this
825 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
826 * size of sysset_t isn't known until runtime.
830 sysset_t_size (procinfo
* pi
)
832 #ifndef DYNAMIC_SYSCALLS
833 return sizeof (sysset_t
);
835 return sizeof (sysset_t
) - sizeof (uint64_t)
836 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
837 / (8 * sizeof (uint64_t)));
841 /* Function: sysset_t_alloc
843 Allocate and (partially) initialize a sysset_t struct. */
846 sysset_t_alloc (procinfo
* pi
)
849 int size
= sysset_t_size (pi
);
850 ret
= xmalloc (size
);
851 #ifdef DYNAMIC_SYSCALLS
852 ret
->pr_size
= (pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
853 / (8 * sizeof (uint64_t));
858 #ifdef DYNAMIC_SYSCALLS
860 /* Function: load_syscalls
862 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
863 pi->num_syscalls with the number of syscalls and pi->syscall_names
864 with the names. (Certain numbers may be skipped in which case the
865 names for these numbers will be left as NULL.) */
867 #define MAX_SYSCALL_NAME_LENGTH 256
868 #define MAX_SYSCALLS 65536
871 load_syscalls (procinfo
*pi
)
873 char pathname
[MAX_PROC_NAME_SIZE
];
876 prsyscall_t
*syscalls
;
877 int i
, size
, maxcall
;
879 pi
->num_syscalls
= 0;
880 pi
->syscall_names
= 0;
882 /* Open the file descriptor for the sysent file */
883 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
884 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
887 error ("load_syscalls: Can't open /proc/%d/sysent", pi
->pid
);
890 size
= sizeof header
- sizeof (prsyscall_t
);
891 if (read (sysent_fd
, &header
, size
) != size
)
893 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
896 if (header
.pr_nsyscalls
== 0)
898 error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi
->pid
);
901 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
902 syscalls
= xmalloc (size
);
904 if (read (sysent_fd
, syscalls
, size
) != size
)
907 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
910 /* Find maximum syscall number. This may not be the same as
911 pr_nsyscalls since that value refers to the number of entries
912 in the table. (Also, the docs indicate that some system
913 call numbers may be skipped.) */
915 maxcall
= syscalls
[0].pr_number
;
917 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
918 if (syscalls
[i
].pr_number
> maxcall
919 && syscalls
[i
].pr_nameoff
> 0
920 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
921 maxcall
= syscalls
[i
].pr_number
;
923 pi
->num_syscalls
= maxcall
+1;
924 pi
->syscall_names
= xmalloc (pi
->num_syscalls
* sizeof (char *));
926 for (i
= 0; i
< pi
->num_syscalls
; i
++)
927 pi
->syscall_names
[i
] = NULL
;
929 /* Read the syscall names in */
930 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
932 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
936 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
937 || syscalls
[i
].pr_number
< 0
938 || syscalls
[i
].pr_nameoff
<= 0
939 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
940 != (off_t
) syscalls
[i
].pr_nameoff
))
943 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
947 callnum
= syscalls
[i
].pr_number
;
949 if (pi
->syscall_names
[callnum
] != NULL
)
951 /* FIXME: Generate warning */
955 namebuf
[nread
-1] = '\0';
956 size
= strlen (namebuf
) + 1;
957 pi
->syscall_names
[callnum
] = xmalloc (size
);
958 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
959 pi
->syscall_names
[callnum
][size
-1] = '\0';
966 /* Function: free_syscalls
968 Free the space allocated for the syscall names from the procinfo
972 free_syscalls (procinfo
*pi
)
974 if (pi
->syscall_names
)
978 for (i
= 0; i
< pi
->num_syscalls
; i
++)
979 if (pi
->syscall_names
[i
] != NULL
)
980 xfree (pi
->syscall_names
[i
]);
982 xfree (pi
->syscall_names
);
983 pi
->syscall_names
= 0;
987 /* Function: find_syscall
989 Given a name, look up (and return) the corresponding syscall number.
990 If no match is found, return -1. */
993 find_syscall (procinfo
*pi
, char *name
)
996 for (i
= 0; i
< pi
->num_syscalls
; i
++)
998 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
1005 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1007 /* =================== /proc "MODULE" =================== */
1010 * This "module" is the interface layer between the /proc system API
1011 * and the gdb target vector functions. This layer consists of
1012 * access functions that encapsulate each of the basic operations
1013 * that we need to use from the /proc API.
1015 * The main motivation for this layer is to hide the fact that
1016 * there are two very different implementations of the /proc API.
1017 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1018 * functions, we do our best to hide them all in here.
1021 int proc_get_status (procinfo
* pi
);
1022 long proc_flags (procinfo
* pi
);
1023 int proc_why (procinfo
* pi
);
1024 int proc_what (procinfo
* pi
);
1025 int proc_set_run_on_last_close (procinfo
* pi
);
1026 int proc_unset_run_on_last_close (procinfo
* pi
);
1027 int proc_set_inherit_on_fork (procinfo
* pi
);
1028 int proc_unset_inherit_on_fork (procinfo
* pi
);
1029 int proc_set_async (procinfo
* pi
);
1030 int proc_unset_async (procinfo
* pi
);
1031 int proc_stop_process (procinfo
* pi
);
1032 int proc_trace_signal (procinfo
* pi
, int signo
);
1033 int proc_ignore_signal (procinfo
* pi
, int signo
);
1034 int proc_clear_current_fault (procinfo
* pi
);
1035 int proc_set_current_signal (procinfo
* pi
, int signo
);
1036 int proc_clear_current_signal (procinfo
* pi
);
1037 int proc_set_gregs (procinfo
* pi
);
1038 int proc_set_fpregs (procinfo
* pi
);
1039 int proc_wait_for_stop (procinfo
* pi
);
1040 int proc_run_process (procinfo
* pi
, int step
, int signo
);
1041 int proc_kill (procinfo
* pi
, int signo
);
1042 int proc_parent_pid (procinfo
* pi
);
1043 int proc_get_nthreads (procinfo
* pi
);
1044 int proc_get_current_thread (procinfo
* pi
);
1045 int proc_set_held_signals (procinfo
* pi
, gdb_sigset_t
* sighold
);
1046 int proc_set_traced_sysexit (procinfo
* pi
, sysset_t
* sysset
);
1047 int proc_set_traced_sysentry (procinfo
* pi
, sysset_t
* sysset
);
1048 int proc_set_traced_faults (procinfo
* pi
, fltset_t
* fltset
);
1049 int proc_set_traced_signals (procinfo
* pi
, gdb_sigset_t
* sigset
);
1051 int proc_update_threads (procinfo
* pi
);
1052 int proc_iterate_over_threads (procinfo
* pi
,
1053 int (*func
) (procinfo
*, procinfo
*, void *),
1056 gdb_gregset_t
*proc_get_gregs (procinfo
* pi
);
1057 gdb_fpregset_t
*proc_get_fpregs (procinfo
* pi
);
1058 sysset_t
*proc_get_traced_sysexit (procinfo
* pi
, sysset_t
* save
);
1059 sysset_t
*proc_get_traced_sysentry (procinfo
* pi
, sysset_t
* save
);
1060 fltset_t
*proc_get_traced_faults (procinfo
* pi
, fltset_t
* save
);
1061 gdb_sigset_t
*proc_get_traced_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1062 gdb_sigset_t
*proc_get_held_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1063 gdb_sigset_t
*proc_get_pending_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1064 gdb_sigaction_t
*proc_get_signal_actions (procinfo
* pi
, gdb_sigaction_t
*save
);
1066 void proc_warn (procinfo
* pi
, char *func
, int line
);
1067 void proc_error (procinfo
* pi
, char *func
, int line
);
1070 proc_warn (procinfo
*pi
, char *func
, int line
)
1072 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1073 print_sys_errmsg (errmsg
, errno
);
1077 proc_error (procinfo
*pi
, char *func
, int line
)
1079 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1080 perror_with_name (errmsg
);
1084 * Function: proc_get_status
1086 * Updates the status struct in the procinfo.
1087 * There is a 'valid' flag, to let other functions know when
1088 * this function needs to be called (so the status is only
1089 * read when it is needed). The status file descriptor is
1090 * also only opened when it is needed.
1092 * Return: non-zero for success, zero for failure.
1096 proc_get_status (procinfo
*pi
)
1098 /* Status file descriptor is opened "lazily" */
1099 if (pi
->status_fd
== 0 &&
1100 open_procinfo_files (pi
, FD_STATUS
) == 0)
1102 pi
->status_valid
= 0;
1107 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1108 pi
->status_valid
= 0; /* fail */
1111 /* Sigh... I have to read a different data structure,
1112 depending on whether this is a main process or an LWP. */
1114 pi
->status_valid
= (read (pi
->status_fd
,
1115 (char *) &pi
->prstatus
.pr_lwp
,
1116 sizeof (lwpstatus_t
))
1117 == sizeof (lwpstatus_t
));
1120 pi
->status_valid
= (read (pi
->status_fd
,
1121 (char *) &pi
->prstatus
,
1122 sizeof (gdb_prstatus_t
))
1123 == sizeof (gdb_prstatus_t
));
1124 #if 0 /*def UNIXWARE*/
1125 if (pi
->status_valid
&&
1126 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
1127 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
1128 /* Unixware peculiarity -- read the damn thing again! */
1129 pi
->status_valid
= (read (pi
->status_fd
,
1130 (char *) &pi
->prstatus
,
1131 sizeof (gdb_prstatus_t
))
1132 == sizeof (gdb_prstatus_t
));
1133 #endif /* UNIXWARE */
1136 #else /* ioctl method */
1137 #ifdef PIOCTSTATUS /* osf */
1138 if (pi
->tid
== 0) /* main process */
1140 /* Just read the danged status. Now isn't that simple? */
1142 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1149 tid_t pr_error_thread
;
1150 struct prstatus status
;
1153 thread_status
.pr_count
= 1;
1154 thread_status
.status
.pr_tid
= pi
->tid
;
1155 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1158 memcpy (&pi
->prstatus
, &thread_status
.status
,
1159 sizeof (pi
->prstatus
));
1160 pi
->status_valid
= 1;
1164 /* Just read the danged status. Now isn't that simple? */
1165 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1169 if (pi
->status_valid
)
1171 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1174 proc_get_current_thread (pi
));
1177 /* The status struct includes general regs, so mark them valid too */
1178 pi
->gregs_valid
= pi
->status_valid
;
1180 /* In the read/write multiple-fd model,
1181 the status struct includes the fp regs too, so mark them valid too */
1182 pi
->fpregs_valid
= pi
->status_valid
;
1184 return pi
->status_valid
; /* True if success, false if failure. */
1188 * Function: proc_flags
1190 * returns the process flags (pr_flags field).
1194 proc_flags (procinfo
*pi
)
1196 if (!pi
->status_valid
)
1197 if (!proc_get_status (pi
))
1198 return 0; /* FIXME: not a good failure value (but what is?) */
1202 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1203 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1204 The two sets of flags don't overlap. */
1205 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
1207 return pi
->prstatus
.pr_lwp
.pr_flags
;
1210 return pi
->prstatus
.pr_flags
;
1215 * Function: proc_why
1217 * returns the pr_why field (why the process stopped).
1221 proc_why (procinfo
*pi
)
1223 if (!pi
->status_valid
)
1224 if (!proc_get_status (pi
))
1225 return 0; /* FIXME: not a good failure value (but what is?) */
1228 return pi
->prstatus
.pr_lwp
.pr_why
;
1230 return pi
->prstatus
.pr_why
;
1235 * Function: proc_what
1237 * returns the pr_what field (details of why the process stopped).
1241 proc_what (procinfo
*pi
)
1243 if (!pi
->status_valid
)
1244 if (!proc_get_status (pi
))
1245 return 0; /* FIXME: not a good failure value (but what is?) */
1248 return pi
->prstatus
.pr_lwp
.pr_what
;
1250 return pi
->prstatus
.pr_what
;
1254 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1256 * Function: proc_nsysarg
1258 * returns the pr_nsysarg field (number of args to the current syscall).
1262 proc_nsysarg (procinfo
*pi
)
1264 if (!pi
->status_valid
)
1265 if (!proc_get_status (pi
))
1269 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1271 return pi
->prstatus
.pr_nsysarg
;
1276 * Function: proc_sysargs
1278 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1282 proc_sysargs (procinfo
*pi
)
1284 if (!pi
->status_valid
)
1285 if (!proc_get_status (pi
))
1289 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1291 return (long *) &pi
->prstatus
.pr_sysarg
;
1296 * Function: proc_syscall
1298 * returns the pr_syscall field (id of current syscall if we are in one).
1302 proc_syscall (procinfo
*pi
)
1304 if (!pi
->status_valid
)
1305 if (!proc_get_status (pi
))
1309 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1311 return pi
->prstatus
.pr_syscall
;
1314 #endif /* PIOCSSPCACT */
1317 * Function: proc_cursig:
1319 * returns the pr_cursig field (current signal).
1323 proc_cursig (struct procinfo
*pi
)
1325 if (!pi
->status_valid
)
1326 if (!proc_get_status (pi
))
1327 return 0; /* FIXME: not a good failure value (but what is?) */
1330 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1332 return pi
->prstatus
.pr_cursig
;
1337 * Function: proc_modify_flag
1339 * === I appologize for the messiness of this function.
1340 * === This is an area where the different versions of
1341 * === /proc are more inconsistent than usual. MVS
1343 * Set or reset any of the following process flags:
1344 * PR_FORK -- forked child will inherit trace flags
1345 * PR_RLC -- traced process runs when last /proc file closed.
1346 * PR_KLC -- traced process is killed when last /proc file closed.
1347 * PR_ASYNC -- LWP's get to run/stop independently.
1349 * There are three methods for doing this function:
1350 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1352 * 2) Middle: PIOCSET/PIOCRESET
1354 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1357 * Note: Irix does not define PR_ASYNC.
1358 * Note: OSF does not define PR_KLC.
1359 * Note: OSF is the only one that can ONLY use the oldest method.
1362 * pi -- the procinfo
1363 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1364 * mode -- 1 for set, 0 for reset.
1366 * Returns non-zero for success, zero for failure.
1369 enum { FLAG_RESET
, FLAG_SET
};
1372 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1374 long win
= 0; /* default to fail */
1377 * These operations affect the process as a whole, and applying
1378 * them to an individual LWP has the same meaning as applying them
1379 * to the main process. Therefore, if we're ever called with a
1380 * pointer to an LWP's procinfo, let's substitute the process's
1381 * procinfo and avoid opening the LWP's file descriptor
1386 pi
= find_procinfo_or_die (pi
->pid
, 0);
1388 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1389 /* First normalize the PCUNSET/PCRESET command opcode
1390 (which for no obvious reason has a different definition
1391 from one operating system to the next...) */
1393 #define GDBRESET PCUNSET
1396 #define GDBRESET PCRESET
1400 procfs_ctl_t arg
[2];
1402 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1404 else /* Reset the flag */
1408 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1411 #ifdef PIOCSET /* Irix/Sol5 method */
1412 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1414 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1416 else /* Reset the flag */
1418 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1422 #ifdef PIOCSRLC /* Oldest method: OSF */
1425 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1427 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1429 else /* Clear run-on-last-close */
1431 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1435 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1437 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1439 else /* Clear inherit-on-fork */
1441 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1445 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1452 /* The above operation renders the procinfo's cached pstatus obsolete. */
1453 pi
->status_valid
= 0;
1456 warning ("procfs: modify_flag failed to turn %s %s",
1457 flag
== PR_FORK
? "PR_FORK" :
1458 flag
== PR_RLC
? "PR_RLC" :
1460 flag
== PR_ASYNC
? "PR_ASYNC" :
1463 flag
== PR_KLC
? "PR_KLC" :
1466 mode
== FLAG_RESET
? "off" : "on");
1472 * Function: proc_set_run_on_last_close
1474 * Set the run_on_last_close flag.
1475 * Process with all threads will become runnable
1476 * when debugger closes all /proc fds.
1478 * Returns non-zero for success, zero for failure.
1482 proc_set_run_on_last_close (procinfo
*pi
)
1484 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1488 * Function: proc_unset_run_on_last_close
1490 * Reset the run_on_last_close flag.
1491 * Process will NOT become runnable
1492 * when debugger closes its file handles.
1494 * Returns non-zero for success, zero for failure.
1498 proc_unset_run_on_last_close (procinfo
*pi
)
1500 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1505 * Function: proc_set_kill_on_last_close
1507 * Set the kill_on_last_close flag.
1508 * Process with all threads will be killed when debugger
1509 * closes all /proc fds (or debugger exits or dies).
1511 * Returns non-zero for success, zero for failure.
1515 proc_set_kill_on_last_close (procinfo
*pi
)
1517 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1521 * Function: proc_unset_kill_on_last_close
1523 * Reset the kill_on_last_close flag.
1524 * Process will NOT be killed when debugger
1525 * closes its file handles (or exits or dies).
1527 * Returns non-zero for success, zero for failure.
1531 proc_unset_kill_on_last_close (procinfo
*pi
)
1533 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1538 * Function: proc_set_inherit_on_fork
1540 * Set inherit_on_fork flag.
1541 * If the process forks a child while we are registered for events
1542 * in the parent, then we will also recieve events from the child.
1544 * Returns non-zero for success, zero for failure.
1548 proc_set_inherit_on_fork (procinfo
*pi
)
1550 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1554 * Function: proc_unset_inherit_on_fork
1556 * Reset inherit_on_fork flag.
1557 * If the process forks a child while we are registered for events
1558 * in the parent, then we will NOT recieve events from the child.
1560 * Returns non-zero for success, zero for failure.
1564 proc_unset_inherit_on_fork (procinfo
*pi
)
1566 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1571 * Function: proc_set_async
1573 * Set PR_ASYNC flag.
1574 * If one LWP stops because of a debug event (signal etc.),
1575 * the remaining LWPs will continue to run.
1577 * Returns non-zero for success, zero for failure.
1581 proc_set_async (procinfo
*pi
)
1583 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1587 * Function: proc_unset_async
1589 * Reset PR_ASYNC flag.
1590 * If one LWP stops because of a debug event (signal etc.),
1591 * then all other LWPs will stop as well.
1593 * Returns non-zero for success, zero for failure.
1597 proc_unset_async (procinfo
*pi
)
1599 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1601 #endif /* PR_ASYNC */
1604 * Function: proc_stop_process
1606 * Request the process/LWP to stop. Does not wait.
1607 * Returns non-zero for success, zero for failure.
1611 proc_stop_process (procinfo
*pi
)
1616 * We might conceivably apply this operation to an LWP, and
1617 * the LWP's ctl file descriptor might not be open.
1620 if (pi
->ctl_fd
== 0 &&
1621 open_procinfo_files (pi
, FD_CTL
) == 0)
1626 procfs_ctl_t cmd
= PCSTOP
;
1627 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1628 #else /* ioctl method */
1629 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1630 /* Note: the call also reads the prstatus. */
1633 pi
->status_valid
= 1;
1634 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1637 proc_get_current_thread (pi
));
1646 * Function: proc_wait_for_stop
1648 * Wait for the process or LWP to stop (block until it does).
1649 * Returns non-zero for success, zero for failure.
1653 proc_wait_for_stop (procinfo
*pi
)
1658 * We should never have to apply this operation to any procinfo
1659 * except the one for the main process. If that ever changes
1660 * for any reason, then take out the following clause and
1661 * replace it with one that makes sure the ctl_fd is open.
1665 pi
= find_procinfo_or_die (pi
->pid
, 0);
1669 procfs_ctl_t cmd
= PCWSTOP
;
1670 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1671 /* We been runnin' and we stopped -- need to update status. */
1672 pi
->status_valid
= 0;
1674 #else /* ioctl method */
1675 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1676 /* Above call also refreshes the prstatus. */
1679 pi
->status_valid
= 1;
1680 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1683 proc_get_current_thread (pi
));
1691 * Function: proc_run_process
1693 * Make the process or LWP runnable.
1694 * Options (not all are implemented):
1696 * - clear current fault
1697 * - clear current signal
1698 * - abort the current system call
1699 * - stop as soon as finished with system call
1700 * - (ioctl): set traced signal set
1701 * - (ioctl): set held signal set
1702 * - (ioctl): set traced fault set
1703 * - (ioctl): set start pc (vaddr)
1704 * Always clear the current fault.
1705 * Clear the current signal if 'signo' is zero.
1708 * pi the process or LWP to operate on.
1709 * step if true, set the process or LWP to trap after one instr.
1710 * signo if zero, clear the current signal if any.
1711 * if non-zero, set the current signal to this one.
1713 * Returns non-zero for success, zero for failure.
1717 proc_run_process (procinfo
*pi
, int step
, int signo
)
1723 * We will probably have to apply this operation to individual threads,
1724 * so make sure the control file descriptor is open.
1727 if (pi
->ctl_fd
== 0 &&
1728 open_procinfo_files (pi
, FD_CTL
) == 0)
1733 runflags
= PRCFAULT
; /* always clear current fault */
1738 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1739 proc_set_current_signal (pi
, signo
);
1743 procfs_ctl_t cmd
[2];
1747 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1749 #else /* ioctl method */
1753 memset (&prrun
, 0, sizeof (prrun
));
1754 prrun
.pr_flags
= runflags
;
1755 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1763 * Function: proc_set_traced_signals
1765 * Register to trace signals in the process or LWP.
1766 * Returns non-zero for success, zero for failure.
1770 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1775 * We should never have to apply this operation to any procinfo
1776 * except the one for the main process. If that ever changes
1777 * for any reason, then take out the following clause and
1778 * replace it with one that makes sure the ctl_fd is open.
1782 pi
= find_procinfo_or_die (pi
->pid
, 0);
1788 /* Use char array to avoid alignment issues. */
1789 char sigset
[sizeof (gdb_sigset_t
)];
1793 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1795 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1797 #else /* ioctl method */
1798 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1800 /* The above operation renders the procinfo's cached pstatus obsolete. */
1801 pi
->status_valid
= 0;
1804 warning ("procfs: set_traced_signals failed");
1809 * Function: proc_set_traced_faults
1811 * Register to trace hardware faults in the process or LWP.
1812 * Returns non-zero for success, zero for failure.
1816 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1821 * We should never have to apply this operation to any procinfo
1822 * except the one for the main process. If that ever changes
1823 * for any reason, then take out the following clause and
1824 * replace it with one that makes sure the ctl_fd is open.
1828 pi
= find_procinfo_or_die (pi
->pid
, 0);
1834 /* Use char array to avoid alignment issues. */
1835 char fltset
[sizeof (fltset_t
)];
1839 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1841 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1843 #else /* ioctl method */
1844 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1846 /* The above operation renders the procinfo's cached pstatus obsolete. */
1847 pi
->status_valid
= 0;
1853 * Function: proc_set_traced_sysentry
1855 * Register to trace entry to system calls in the process or LWP.
1856 * Returns non-zero for success, zero for failure.
1860 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1865 * We should never have to apply this operation to any procinfo
1866 * except the one for the main process. If that ever changes
1867 * for any reason, then take out the following clause and
1868 * replace it with one that makes sure the ctl_fd is open.
1872 pi
= find_procinfo_or_die (pi
->pid
, 0);
1876 struct gdb_proc_ctl_pcsentry
{
1878 /* Use char array to avoid alignment issues. */
1879 char sysset
[sizeof (sysset_t
)];
1881 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1883 + sysset_t_size (pi
);
1885 argp
= xmalloc (argp_size
);
1887 argp
->cmd
= PCSENTRY
;
1888 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1890 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1893 #else /* ioctl method */
1894 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1896 /* The above operation renders the procinfo's cached pstatus obsolete. */
1897 pi
->status_valid
= 0;
1903 * Function: proc_set_traced_sysexit
1905 * Register to trace exit from system calls in the process or LWP.
1906 * Returns non-zero for success, zero for failure.
1910 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1915 * We should never have to apply this operation to any procinfo
1916 * except the one for the main process. If that ever changes
1917 * for any reason, then take out the following clause and
1918 * replace it with one that makes sure the ctl_fd is open.
1922 pi
= find_procinfo_or_die (pi
->pid
, 0);
1926 struct gdb_proc_ctl_pcsexit
{
1928 /* Use char array to avoid alignment issues. */
1929 char sysset
[sizeof (sysset_t
)];
1931 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1933 + sysset_t_size (pi
);
1935 argp
= xmalloc (argp_size
);
1937 argp
->cmd
= PCSEXIT
;
1938 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1940 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1943 #else /* ioctl method */
1944 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1946 /* The above operation renders the procinfo's cached pstatus obsolete. */
1947 pi
->status_valid
= 0;
1953 * Function: proc_set_held_signals
1955 * Specify the set of blocked / held signals in the process or LWP.
1956 * Returns non-zero for success, zero for failure.
1960 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1965 * We should never have to apply this operation to any procinfo
1966 * except the one for the main process. If that ever changes
1967 * for any reason, then take out the following clause and
1968 * replace it with one that makes sure the ctl_fd is open.
1972 pi
= find_procinfo_or_die (pi
->pid
, 0);
1978 /* Use char array to avoid alignment issues. */
1979 char hold
[sizeof (gdb_sigset_t
)];
1983 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1984 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1987 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1989 /* The above operation renders the procinfo's cached pstatus obsolete. */
1990 pi
->status_valid
= 0;
1996 * Function: proc_get_pending_signals
1998 * returns the set of signals that are pending in the process or LWP.
1999 * Will also copy the sigset if 'save' is non-zero.
2003 proc_get_pending_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2005 gdb_sigset_t
*ret
= NULL
;
2008 * We should never have to apply this operation to any procinfo
2009 * except the one for the main process. If that ever changes
2010 * for any reason, then take out the following clause and
2011 * replace it with one that makes sure the ctl_fd is open.
2015 pi
= find_procinfo_or_die (pi
->pid
, 0);
2017 if (!pi
->status_valid
)
2018 if (!proc_get_status (pi
))
2022 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
2024 ret
= &pi
->prstatus
.pr_sigpend
;
2027 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2033 * Function: proc_get_signal_actions
2035 * returns the set of signal actions.
2036 * Will also copy the sigactionset if 'save' is non-zero.
2040 proc_get_signal_actions (procinfo
*pi
, gdb_sigaction_t
*save
)
2042 gdb_sigaction_t
*ret
= NULL
;
2045 * We should never have to apply this operation to any procinfo
2046 * except the one for the main process. If that ever changes
2047 * for any reason, then take out the following clause and
2048 * replace it with one that makes sure the ctl_fd is open.
2052 pi
= find_procinfo_or_die (pi
->pid
, 0);
2054 if (!pi
->status_valid
)
2055 if (!proc_get_status (pi
))
2059 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
2061 ret
= &pi
->prstatus
.pr_action
;
2064 memcpy (save
, ret
, sizeof (gdb_sigaction_t
));
2070 * Function: proc_get_held_signals
2072 * returns the set of signals that are held / blocked.
2073 * Will also copy the sigset if 'save' is non-zero.
2077 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2079 gdb_sigset_t
*ret
= NULL
;
2082 * We should never have to apply this operation to any procinfo
2083 * except the one for the main process. If that ever changes
2084 * for any reason, then take out the following clause and
2085 * replace it with one that makes sure the ctl_fd is open.
2089 pi
= find_procinfo_or_die (pi
->pid
, 0);
2092 if (!pi
->status_valid
)
2093 if (!proc_get_status (pi
))
2097 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
2099 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
2100 #endif /* UNIXWARE */
2101 #else /* not NEW_PROC_API */
2103 static gdb_sigset_t sigheld
;
2105 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
2108 #endif /* NEW_PROC_API */
2110 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2116 * Function: proc_get_traced_signals
2118 * returns the set of signals that are traced / debugged.
2119 * Will also copy the sigset if 'save' is non-zero.
2123 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2125 gdb_sigset_t
*ret
= NULL
;
2128 * We should never have to apply this operation to any procinfo
2129 * except the one for the main process. If that ever changes
2130 * for any reason, then take out the following clause and
2131 * replace it with one that makes sure the ctl_fd is open.
2135 pi
= find_procinfo_or_die (pi
->pid
, 0);
2138 if (!pi
->status_valid
)
2139 if (!proc_get_status (pi
))
2142 ret
= &pi
->prstatus
.pr_sigtrace
;
2145 static gdb_sigset_t sigtrace
;
2147 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
2152 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2158 * Function: proc_trace_signal
2160 * Add 'signo' to the set of signals that are traced.
2161 * Returns non-zero for success, zero for failure.
2165 proc_trace_signal (procinfo
*pi
, int signo
)
2170 * We should never have to apply this operation to any procinfo
2171 * except the one for the main process. If that ever changes
2172 * for any reason, then take out the following clause and
2173 * replace it with one that makes sure the ctl_fd is open.
2177 pi
= find_procinfo_or_die (pi
->pid
, 0);
2181 if (proc_get_traced_signals (pi
, &temp
))
2183 praddset (&temp
, signo
);
2184 return proc_set_traced_signals (pi
, &temp
);
2188 return 0; /* failure */
2192 * Function: proc_ignore_signal
2194 * Remove 'signo' from the set of signals that are traced.
2195 * Returns non-zero for success, zero for failure.
2199 proc_ignore_signal (procinfo
*pi
, int signo
)
2204 * We should never have to apply this operation to any procinfo
2205 * except the one for the main process. If that ever changes
2206 * for any reason, then take out the following clause and
2207 * replace it with one that makes sure the ctl_fd is open.
2211 pi
= find_procinfo_or_die (pi
->pid
, 0);
2215 if (proc_get_traced_signals (pi
, &temp
))
2217 prdelset (&temp
, signo
);
2218 return proc_set_traced_signals (pi
, &temp
);
2222 return 0; /* failure */
2226 * Function: proc_get_traced_faults
2228 * returns the set of hardware faults that are traced /debugged.
2229 * Will also copy the faultset if 'save' is non-zero.
2233 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
2235 fltset_t
*ret
= NULL
;
2238 * We should never have to apply this operation to any procinfo
2239 * except the one for the main process. If that ever changes
2240 * for any reason, then take out the following clause and
2241 * replace it with one that makes sure the ctl_fd is open.
2245 pi
= find_procinfo_or_die (pi
->pid
, 0);
2248 if (!pi
->status_valid
)
2249 if (!proc_get_status (pi
))
2252 ret
= &pi
->prstatus
.pr_flttrace
;
2255 static fltset_t flttrace
;
2257 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2262 memcpy (save
, ret
, sizeof (fltset_t
));
2268 * Function: proc_get_traced_sysentry
2270 * returns the set of syscalls that are traced /debugged on entry.
2271 * Will also copy the syscall set if 'save' is non-zero.
2275 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
2277 sysset_t
*ret
= NULL
;
2280 * We should never have to apply this operation to any procinfo
2281 * except the one for the main process. If that ever changes
2282 * for any reason, then take out the following clause and
2283 * replace it with one that makes sure the ctl_fd is open.
2287 pi
= find_procinfo_or_die (pi
->pid
, 0);
2290 if (!pi
->status_valid
)
2291 if (!proc_get_status (pi
))
2294 #ifndef DYNAMIC_SYSCALLS
2295 ret
= &pi
->prstatus
.pr_sysentry
;
2296 #else /* DYNAMIC_SYSCALLS */
2298 static sysset_t
*sysentry
;
2302 sysentry
= sysset_t_alloc (pi
);
2304 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2306 if (pi
->prstatus
.pr_sysentry_offset
== 0)
2308 gdb_premptysysset (sysentry
);
2314 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
2316 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
2318 size
= sysset_t_size (pi
);
2319 gdb_premptysysset (sysentry
);
2320 rsize
= read (pi
->status_fd
, sysentry
, size
);
2325 #endif /* DYNAMIC_SYSCALLS */
2326 #else /* !NEW_PROC_API */
2328 static sysset_t sysentry
;
2330 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2333 #endif /* NEW_PROC_API */
2335 memcpy (save
, ret
, sysset_t_size (pi
));
2341 * Function: proc_get_traced_sysexit
2343 * returns the set of syscalls that are traced /debugged on exit.
2344 * Will also copy the syscall set if 'save' is non-zero.
2348 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
2350 sysset_t
* ret
= NULL
;
2353 * We should never have to apply this operation to any procinfo
2354 * except the one for the main process. If that ever changes
2355 * for any reason, then take out the following clause and
2356 * replace it with one that makes sure the ctl_fd is open.
2360 pi
= find_procinfo_or_die (pi
->pid
, 0);
2363 if (!pi
->status_valid
)
2364 if (!proc_get_status (pi
))
2367 #ifndef DYNAMIC_SYSCALLS
2368 ret
= &pi
->prstatus
.pr_sysexit
;
2369 #else /* DYNAMIC_SYSCALLS */
2371 static sysset_t
*sysexit
;
2375 sysexit
= sysset_t_alloc (pi
);
2377 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2379 if (pi
->prstatus
.pr_sysexit_offset
== 0)
2381 gdb_premptysysset (sysexit
);
2387 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
, SEEK_SET
)
2388 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2390 size
= sysset_t_size (pi
);
2391 gdb_premptysysset (sysexit
);
2392 rsize
= read (pi
->status_fd
, sysexit
, size
);
2397 #endif /* DYNAMIC_SYSCALLS */
2400 static sysset_t sysexit
;
2402 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2407 memcpy (save
, ret
, sysset_t_size (pi
));
2413 * Function: proc_clear_current_fault
2415 * The current fault (if any) is cleared; the associated signal
2416 * will not be sent to the process or LWP when it resumes.
2417 * Returns non-zero for success, zero for failure.
2421 proc_clear_current_fault (procinfo
*pi
)
2426 * We should never have to apply this operation to any procinfo
2427 * except the one for the main process. If that ever changes
2428 * for any reason, then take out the following clause and
2429 * replace it with one that makes sure the ctl_fd is open.
2433 pi
= find_procinfo_or_die (pi
->pid
, 0);
2437 procfs_ctl_t cmd
= PCCFAULT
;
2438 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2441 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2448 * Function: proc_set_current_signal
2450 * Set the "current signal" that will be delivered next to the process.
2451 * NOTE: semantics are different from those of KILL.
2452 * This signal will be delivered to the process or LWP
2453 * immediately when it is resumed (even if the signal is held/blocked);
2454 * it will NOT immediately cause another event of interest, and will NOT
2455 * first trap back to the debugger.
2457 * Returns non-zero for success, zero for failure.
2461 proc_set_current_signal (procinfo
*pi
, int signo
)
2466 /* Use char array to avoid alignment issues. */
2467 char sinfo
[sizeof (gdb_siginfo_t
)];
2469 gdb_siginfo_t
*mysinfo
;
2472 * We should never have to apply this operation to any procinfo
2473 * except the one for the main process. If that ever changes
2474 * for any reason, then take out the following clause and
2475 * replace it with one that makes sure the ctl_fd is open.
2479 pi
= find_procinfo_or_die (pi
->pid
, 0);
2481 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2482 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2483 * receives a PIOCSSIG with a signal identical to the current signal,
2484 * it messes up the current signal. Work around the kernel bug.
2487 signo
== proc_cursig (pi
))
2488 return 1; /* I assume this is a success? */
2491 /* The pointer is just a type alias. */
2492 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2493 mysinfo
->si_signo
= signo
;
2494 mysinfo
->si_code
= 0;
2495 mysinfo
->si_pid
= getpid (); /* ?why? */
2496 mysinfo
->si_uid
= getuid (); /* ?why? */
2500 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2502 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2509 * Function: proc_clear_current_signal
2511 * The current signal (if any) is cleared, and
2512 * is not sent to the process or LWP when it resumes.
2513 * Returns non-zero for success, zero for failure.
2517 proc_clear_current_signal (procinfo
*pi
)
2522 * We should never have to apply this operation to any procinfo
2523 * except the one for the main process. If that ever changes
2524 * for any reason, then take out the following clause and
2525 * replace it with one that makes sure the ctl_fd is open.
2529 pi
= find_procinfo_or_die (pi
->pid
, 0);
2535 /* Use char array to avoid alignment issues. */
2536 char sinfo
[sizeof (gdb_siginfo_t
)];
2538 gdb_siginfo_t
*mysinfo
;
2541 /* The pointer is just a type alias. */
2542 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2543 mysinfo
->si_signo
= 0;
2544 mysinfo
->si_code
= 0;
2545 mysinfo
->si_errno
= 0;
2546 mysinfo
->si_pid
= getpid (); /* ?why? */
2547 mysinfo
->si_uid
= getuid (); /* ?why? */
2549 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2552 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2559 * Function: proc_get_gregs
2561 * Get the general registers for the process or LWP.
2562 * Returns non-zero for success, zero for failure.
2566 proc_get_gregs (procinfo
*pi
)
2568 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2569 if (!proc_get_status (pi
))
2573 * OK, sorry about the ifdef's.
2574 * There's three cases instead of two, because
2575 * in this instance Unixware and Solaris/RW differ.
2579 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2580 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2581 #else /* not Unixware */
2582 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2583 #endif /* Unixware */
2584 #else /* not NEW_PROC_API */
2585 return &pi
->prstatus
.pr_reg
;
2586 #endif /* NEW_PROC_API */
2590 * Function: proc_get_fpregs
2592 * Get the floating point registers for the process or LWP.
2593 * Returns non-zero for success, zero for failure.
2597 proc_get_fpregs (procinfo
*pi
)
2600 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2601 if (!proc_get_status (pi
))
2604 #ifdef UNIXWARE /* a true architecture dependency */
2605 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2607 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2608 #endif /* Unixware */
2610 #else /* not NEW_PROC_API */
2611 if (pi
->fpregs_valid
)
2612 return &pi
->fpregset
; /* already got 'em */
2615 if (pi
->ctl_fd
== 0 &&
2616 open_procinfo_files (pi
, FD_CTL
) == 0)
2625 tid_t pr_error_thread
;
2626 tfpregset_t thread_1
;
2629 thread_fpregs
.pr_count
= 1;
2630 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2633 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2635 pi
->fpregs_valid
= 1;
2636 return &pi
->fpregset
; /* got 'em now! */
2638 else if (pi
->tid
!= 0 &&
2639 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2641 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2642 sizeof (pi
->fpregset
));
2643 pi
->fpregs_valid
= 1;
2644 return &pi
->fpregset
; /* got 'em now! */
2651 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2653 pi
->fpregs_valid
= 1;
2654 return &pi
->fpregset
; /* got 'em now! */
2667 * Function: proc_set_gregs
2669 * Write the general registers back to the process or LWP.
2670 * Returns non-zero for success, zero for failure.
2674 proc_set_gregs (procinfo
*pi
)
2676 gdb_gregset_t
*gregs
;
2679 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2680 return 0; /* get_regs has already warned */
2682 if (pi
->ctl_fd
== 0 &&
2683 open_procinfo_files (pi
, FD_CTL
) == 0)
2692 /* Use char array to avoid alignment issues. */
2693 char gregs
[sizeof (gdb_gregset_t
)];
2697 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2698 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2700 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2704 /* Policy: writing the regs invalidates our cache. */
2705 pi
->gregs_valid
= 0;
2710 * Function: proc_set_fpregs
2712 * Modify the floating point register set of the process or LWP.
2713 * Returns non-zero for success, zero for failure.
2717 proc_set_fpregs (procinfo
*pi
)
2719 gdb_fpregset_t
*fpregs
;
2722 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2723 return 0; /* get_fpregs has already warned */
2725 if (pi
->ctl_fd
== 0 &&
2726 open_procinfo_files (pi
, FD_CTL
) == 0)
2735 /* Use char array to avoid alignment issues. */
2736 char fpregs
[sizeof (gdb_fpregset_t
)];
2740 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2741 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2745 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2750 tid_t pr_error_thread
;
2751 tfpregset_t thread_1
;
2754 thread_fpregs
.pr_count
= 1;
2755 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2756 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2758 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2761 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2762 #endif /* osf PIOCTSFPREG */
2763 #endif /* NEW_PROC_API */
2766 /* Policy: writing the regs invalidates our cache. */
2767 pi
->fpregs_valid
= 0;
2772 * Function: proc_kill
2774 * Send a signal to the proc or lwp with the semantics of "kill()".
2775 * Returns non-zero for success, zero for failure.
2779 proc_kill (procinfo
*pi
, int signo
)
2784 * We might conceivably apply this operation to an LWP, and
2785 * the LWP's ctl file descriptor might not be open.
2788 if (pi
->ctl_fd
== 0 &&
2789 open_procinfo_files (pi
, FD_CTL
) == 0)
2796 procfs_ctl_t cmd
[2];
2800 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2801 #else /* ioctl method */
2802 /* FIXME: do I need the Alpha OSF fixups present in
2803 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2804 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2812 * Function: proc_parent_pid
2814 * Find the pid of the process that started this one.
2815 * Returns the parent process pid, or zero.
2819 proc_parent_pid (procinfo
*pi
)
2822 * We should never have to apply this operation to any procinfo
2823 * except the one for the main process. If that ever changes
2824 * for any reason, then take out the following clause and
2825 * replace it with one that makes sure the ctl_fd is open.
2829 pi
= find_procinfo_or_die (pi
->pid
, 0);
2831 if (!pi
->status_valid
)
2832 if (!proc_get_status (pi
))
2835 return pi
->prstatus
.pr_ppid
;
2840 * Function: proc_set_watchpoint
2845 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2847 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2850 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2851 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2856 char watch
[sizeof (prwatch_t
)];
2860 pwatch
= (prwatch_t
*) &arg
.watch
;
2861 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2862 pwatch
->pr_vaddr
= (uintptr_t) address_to_host_pointer (addr
);
2864 pwatch
->pr_vaddr
= (caddr_t
) address_to_host_pointer (addr
);
2866 pwatch
->pr_size
= len
;
2867 pwatch
->pr_wflags
= wflags
;
2868 #if defined(NEW_PROC_API) && defined (PCWATCH)
2870 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2872 #if defined (PIOCSWATCH)
2873 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2875 return 0; /* Fail */
2882 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2884 #include <sys/sysi86.h>
2887 * Function: proc_get_LDT_entry
2893 * The 'key' is actually the value of the lower 16 bits of
2894 * the GS register for the LWP that we're interested in.
2896 * Return: matching ssh struct (LDT entry).
2900 proc_get_LDT_entry (procinfo
*pi
, int key
)
2902 static struct ssd
*ldt_entry
= NULL
;
2904 char pathname
[MAX_PROC_NAME_SIZE
];
2905 struct cleanup
*old_chain
= NULL
;
2908 /* Allocate space for one LDT entry.
2909 This alloc must persist, because we return a pointer to it. */
2910 if (ldt_entry
== NULL
)
2911 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2913 /* Open the file descriptor for the LDT table. */
2914 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2915 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2917 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2920 /* Make sure it gets closed again! */
2921 old_chain
= make_cleanup_close (fd
);
2923 /* Now 'read' thru the table, find a match and return it. */
2924 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2926 if (ldt_entry
->sel
== 0 &&
2927 ldt_entry
->bo
== 0 &&
2928 ldt_entry
->acc1
== 0 &&
2929 ldt_entry
->acc2
== 0)
2930 break; /* end of table */
2931 /* If key matches, return this entry. */
2932 if (ldt_entry
->sel
== key
)
2935 /* Loop ended, match not found. */
2939 static int nalloc
= 0;
2941 /* Get the number of LDT entries. */
2942 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2944 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2948 /* Allocate space for the number of LDT entries. */
2949 /* This alloc has to persist, 'cause we return a pointer to it. */
2952 ldt_entry
= (struct ssd
*)
2953 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2957 /* Read the whole table in one gulp. */
2958 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2960 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2964 /* Search the table and return the (first) entry matching 'key'. */
2965 for (i
= 0; i
< nldt
; i
++)
2966 if (ldt_entry
[i
].sel
== key
)
2967 return &ldt_entry
[i
];
2969 /* Loop ended, match not found. */
2974 #endif /* TM_I386SOL2_H */
2976 /* =============== END, non-thread part of /proc "MODULE" =============== */
2978 /* =================== Thread "MODULE" =================== */
2980 /* NOTE: you'll see more ifdefs and duplication of functions here,
2981 since there is a different way to do threads on every OS. */
2984 * Function: proc_get_nthreads
2986 * Return the number of threads for the process
2989 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2994 proc_get_nthreads (procinfo
*pi
)
2998 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
2999 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
3005 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3007 * Solaris and Unixware version
3010 proc_get_nthreads (procinfo
*pi
)
3012 if (!pi
->status_valid
)
3013 if (!proc_get_status (pi
))
3017 * NEW_PROC_API: only works for the process procinfo,
3018 * because the LWP procinfos do not get prstatus filled in.
3021 if (pi
->tid
!= 0) /* find the parent process procinfo */
3022 pi
= find_procinfo_or_die (pi
->pid
, 0);
3024 return pi
->prstatus
.pr_nlwp
;
3032 proc_get_nthreads (procinfo
*pi
)
3040 * Function: proc_get_current_thread (LWP version)
3042 * Return the ID of the thread that had an event of interest.
3043 * (ie. the one that hit a breakpoint or other traced event).
3044 * All other things being equal, this should be the ID of a
3045 * thread that is currently executing.
3048 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3050 * Solaris and Unixware version
3053 proc_get_current_thread (procinfo
*pi
)
3056 * Note: this should be applied to the root procinfo for the process,
3057 * not to the procinfo for an LWP. If applied to the procinfo for
3058 * an LWP, it will simply return that LWP's ID. In that case,
3059 * find the parent process procinfo.
3063 pi
= find_procinfo_or_die (pi
->pid
, 0);
3065 if (!pi
->status_valid
)
3066 if (!proc_get_status (pi
))
3070 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
3072 return pi
->prstatus
.pr_who
;
3077 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3082 proc_get_current_thread (procinfo
*pi
)
3084 #if 0 /* FIXME: not ready for prime time? */
3085 return pi
->prstatus
.pr_tid
;
3096 proc_get_current_thread (procinfo
*pi
)
3105 * Function: proc_update_threads
3107 * Discover the IDs of all the threads within the process, and
3108 * create a procinfo for each of them (chained to the parent).
3110 * This unfortunately requires a different method on every OS.
3112 * Return: non-zero for success, zero for failure.
3116 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
3118 if (thread
&& parent
) /* sanity */
3120 thread
->status_valid
= 0;
3121 if (!proc_get_status (thread
))
3122 destroy_one_procinfo (&parent
->thread_list
, thread
);
3124 return 0; /* keep iterating */
3127 #if defined (PIOCLSTATUS)
3129 * Solaris 2.5 (ioctl) version
3132 proc_update_threads (procinfo
*pi
)
3134 gdb_prstatus_t
*prstatus
;
3135 struct cleanup
*old_chain
= NULL
;
3140 * We should never have to apply this operation to any procinfo
3141 * except the one for the main process. If that ever changes
3142 * for any reason, then take out the following clause and
3143 * replace it with one that makes sure the ctl_fd is open.
3147 pi
= find_procinfo_or_die (pi
->pid
, 0);
3149 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3151 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
3152 return 1; /* Process is not multi-threaded; nothing to do. */
3154 prstatus
= xmalloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1));
3156 old_chain
= make_cleanup (xfree
, prstatus
);
3157 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
3158 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
3160 /* Skip element zero, which represents the process as a whole. */
3161 for (i
= 1; i
< nlwp
+ 1; i
++)
3163 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
3164 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3166 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
3167 thread
->status_valid
= 1;
3169 pi
->threads_valid
= 1;
3170 do_cleanups (old_chain
);
3176 * Unixware and Solaris 6 (and later) version
3179 do_closedir_cleanup (void *dir
)
3185 proc_update_threads (procinfo
*pi
)
3187 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3188 struct dirent
*direntry
;
3189 struct cleanup
*old_chain
= NULL
;
3195 * We should never have to apply this operation to any procinfo
3196 * except the one for the main process. If that ever changes
3197 * for any reason, then take out the following clause and
3198 * replace it with one that makes sure the ctl_fd is open.
3202 pi
= find_procinfo_or_die (pi
->pid
, 0);
3204 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3209 * Note: this brute-force method is the only way I know of
3210 * to accomplish this task on Unixware. This method will
3211 * also work on Solaris 2.6 and 2.7. There is a much simpler
3212 * and more elegant way to do this on Solaris, but the margins
3213 * of this manuscript are too small to write it here... ;-)
3216 strcpy (pathname
, pi
->pathname
);
3217 strcat (pathname
, "/lwp");
3218 if ((dirp
= opendir (pathname
)) == NULL
)
3219 proc_error (pi
, "update_threads, opendir", __LINE__
);
3221 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
3222 while ((direntry
= readdir (dirp
)) != NULL
)
3223 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3225 lwpid
= atoi (&direntry
->d_name
[0]);
3226 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3227 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3229 pi
->threads_valid
= 1;
3230 do_cleanups (old_chain
);
3239 proc_update_threads (procinfo
*pi
)
3245 * We should never have to apply this operation to any procinfo
3246 * except the one for the main process. If that ever changes
3247 * for any reason, then take out the following clause and
3248 * replace it with one that makes sure the ctl_fd is open.
3252 pi
= find_procinfo_or_die (pi
->pid
, 0);
3254 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3256 nthreads
= proc_get_nthreads (pi
);
3258 return 0; /* nothing to do for 1 or fewer threads */
3260 threads
= xmalloc (nthreads
* sizeof (tid_t
));
3262 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3263 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3265 for (i
= 0; i
< nthreads
; i
++)
3267 if (!find_procinfo (pi
->pid
, threads
[i
]))
3268 if (!create_procinfo (pi
->pid
, threads
[i
]))
3269 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3271 pi
->threads_valid
= 1;
3279 proc_update_threads (procinfo
*pi
)
3283 #endif /* OSF PIOCTLIST */
3284 #endif /* NEW_PROC_API */
3285 #endif /* SOL 2.5 PIOCLSTATUS */
3288 * Function: proc_iterate_over_threads
3291 * Given a pointer to a function, call that function once
3292 * for each lwp in the procinfo list, until the function
3293 * returns non-zero, in which event return the value
3294 * returned by the function.
3296 * Note: this function does NOT call update_threads.
3297 * If you want to discover new threads first, you must
3298 * call that function explicitly. This function just makes
3299 * a quick pass over the currently-known procinfos.
3302 * pi - parent process procinfo
3303 * func - per-thread function
3304 * ptr - opaque parameter for function.
3307 * First non-zero return value from the callee, or zero.
3311 proc_iterate_over_threads (procinfo
*pi
,
3312 int (*func
) (procinfo
*, procinfo
*, void *),
3315 procinfo
*thread
, *next
;
3319 * We should never have to apply this operation to any procinfo
3320 * except the one for the main process. If that ever changes
3321 * for any reason, then take out the following clause and
3322 * replace it with one that makes sure the ctl_fd is open.
3326 pi
= find_procinfo_or_die (pi
->pid
, 0);
3328 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3330 next
= thread
->next
; /* in case thread is destroyed */
3331 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3338 /* =================== END, Thread "MODULE" =================== */
3340 /* =================== END, /proc "MODULE" =================== */
3342 /* =================== GDB "MODULE" =================== */
3345 * Here are all of the gdb target vector functions and their friends.
3348 static ptid_t
do_attach (ptid_t ptid
);
3349 static void do_detach (int signo
);
3350 static int register_gdb_signals (procinfo
*, gdb_sigset_t
*);
3353 * Function: procfs_debug_inferior
3355 * Sets up the inferior to be debugged.
3356 * Registers to trace signals, hardware faults, and syscalls.
3357 * Note: does not set RLC flag: caller may want to customize that.
3359 * Returns: zero for success (note! unlike most functions in this module)
3360 * On failure, returns the LINE NUMBER where it failed!
3364 procfs_debug_inferior (procinfo
*pi
)
3366 fltset_t traced_faults
;
3367 gdb_sigset_t traced_signals
;
3368 sysset_t
*traced_syscall_entries
;
3369 sysset_t
*traced_syscall_exits
;
3372 #ifdef PROCFS_DONT_TRACE_FAULTS
3373 /* On some systems (OSF), we don't trace hardware faults.
3374 Apparently it's enough that we catch them as signals.
3375 Wonder why we don't just do that in general? */
3376 premptyset (&traced_faults
); /* don't trace faults. */
3378 /* Register to trace hardware faults in the child. */
3379 prfillset (&traced_faults
); /* trace all faults... */
3380 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3382 if (!proc_set_traced_faults (pi
, &traced_faults
))
3385 /* Register to trace selected signals in the child. */
3386 premptyset (&traced_signals
);
3387 if (!register_gdb_signals (pi
, &traced_signals
))
3391 /* Register to trace the 'exit' system call (on entry). */
3392 traced_syscall_entries
= sysset_t_alloc (pi
);
3393 gdb_premptysysset (traced_syscall_entries
);
3395 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
3398 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3401 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
3403 #ifdef DYNAMIC_SYSCALLS
3405 int callnum
= find_syscall (pi
, "_exit");
3407 gdb_praddsysset (traced_syscall_entries
, callnum
);
3411 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
3412 xfree (traced_syscall_entries
);
3416 #ifdef PRFS_STOPEXEC /* defined on OSF */
3417 /* OSF method for tracing exec syscalls. Quoting:
3418 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3419 exits from exec system calls because of the user level loader. */
3420 /* FIXME: make nice and maybe move into an access function. */
3424 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3427 prfs_flags
|= PRFS_STOPEXEC
;
3429 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3432 #else /* not PRFS_STOPEXEC */
3433 /* Everyone else's (except OSF) method for tracing exec syscalls */
3435 Not all systems with /proc have all the exec* syscalls with the same
3436 names. On the SGI, for example, there is no SYS_exec, but there
3437 *is* a SYS_execv. So, we try to account for that. */
3439 traced_syscall_exits
= sysset_t_alloc (pi
);
3440 gdb_premptysysset (traced_syscall_exits
);
3442 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
3445 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
3448 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
3451 #ifdef SYS_lwpcreate
3452 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3453 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3456 #ifdef SYS_lwp_create /* FIXME: once only, please */
3457 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3458 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3461 #ifdef DYNAMIC_SYSCALLS
3463 int callnum
= find_syscall (pi
, "execve");
3465 gdb_praddsysset (traced_syscall_exits
, callnum
);
3466 callnum
= find_syscall (pi
, "ra_execve");
3468 gdb_praddsysset (traced_syscall_exits
, callnum
);
3472 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3473 xfree (traced_syscall_exits
);
3477 #endif /* PRFS_STOPEXEC */
3482 procfs_attach (char *args
, int from_tty
)
3488 error_no_arg ("process-id to attach");
3491 if (pid
== getpid ())
3492 error ("Attaching GDB to itself is not a good idea...");
3496 exec_file
= get_exec_file (0);
3499 printf_filtered ("Attaching to program `%s', %s\n",
3500 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3502 printf_filtered ("Attaching to %s\n",
3503 target_pid_to_str (pid_to_ptid (pid
)));
3507 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3508 push_target (&procfs_ops
);
3512 procfs_detach (char *args
, int from_tty
)
3519 exec_file
= get_exec_file (0);
3522 printf_filtered ("Detaching from program: %s %s\n",
3523 exec_file
, target_pid_to_str (inferior_ptid
));
3527 signo
= atoi (args
);
3530 inferior_ptid
= null_ptid
;
3531 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3535 do_attach (ptid_t ptid
)
3540 if ((pi
= create_procinfo (PIDGET (ptid
), 0)) == NULL
)
3541 perror ("procfs: out of memory in 'attach'");
3543 if (!open_procinfo_files (pi
, FD_CTL
))
3545 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3546 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3548 dead_procinfo (pi
, errmsg
, NOKILL
);
3551 /* Stop the process (if it isn't already stopped). */
3552 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3554 pi
->was_stopped
= 1;
3555 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3559 pi
->was_stopped
= 0;
3560 /* Set the process to run again when we close it. */
3561 if (!proc_set_run_on_last_close (pi
))
3562 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3564 /* Now stop the process. */
3565 if (!proc_stop_process (pi
))
3566 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3567 pi
->ignore_next_sigstop
= 1;
3569 /* Save some of the /proc state to be restored if we detach. */
3570 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3571 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3572 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3573 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3574 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3575 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3577 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3578 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3580 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3581 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3583 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3584 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3586 /* Let GDB know that the inferior was attached. */
3588 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3592 do_detach (int signo
)
3596 /* Find procinfo for the main process */
3597 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0); /* FIXME: threads */
3599 if (!proc_set_current_signal (pi
, signo
))
3600 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3602 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3603 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3605 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3606 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3608 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3609 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3611 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3612 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3614 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3615 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3617 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3618 if (signo
|| !(pi
->was_stopped
) ||
3619 query ("Was stopped when attached, make it runnable again? "))
3621 /* Clear any pending signal. */
3622 if (!proc_clear_current_fault (pi
))
3623 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3625 if (!proc_set_run_on_last_close (pi
))
3626 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3630 destroy_procinfo (pi
);
3636 * Since the /proc interface cannot give us individual registers,
3637 * we pay no attention to the (regno) argument, and just fetch them all.
3638 * This results in the possibility that we will do unnecessarily many
3639 * fetches, since we may be called repeatedly for individual registers.
3640 * So we cache the results, and mark the cache invalid when the process
3645 procfs_fetch_registers (int regno
)
3647 gdb_fpregset_t
*fpregs
;
3648 gdb_gregset_t
*gregs
;
3653 pid
= PIDGET (inferior_ptid
);
3654 tid
= TIDGET (inferior_ptid
);
3656 /* First look up procinfo for the main process. */
3657 pi
= find_procinfo_or_die (pid
, 0);
3659 /* If the event thread is not the same as GDB's requested thread
3660 (ie. inferior_ptid), then look up procinfo for the requested
3663 (tid
!= proc_get_current_thread (pi
)))
3664 pi
= find_procinfo_or_die (pid
, tid
);
3667 error ("procfs: fetch_registers failed to find procinfo for %s",
3668 target_pid_to_str (inferior_ptid
));
3670 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3671 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3673 supply_gregset (gregs
);
3675 if (FP0_REGNUM
>= 0) /* need floating point? */
3677 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3678 regno
== PC_REGNUM
||
3679 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3680 regno
== FP_REGNUM
||
3682 return; /* not a floating point register */
3684 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3685 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3687 supply_fpregset (fpregs
);
3691 /* Get ready to modify the registers array. On machines which store
3692 individual registers, this doesn't need to do anything. On
3693 machines which store all the registers in one fell swoop, such as
3694 /proc, this makes sure that registers contains all the registers
3695 from the program being debugged. */
3698 procfs_prepare_to_store (void)
3700 #ifdef CHILD_PREPARE_TO_STORE
3701 CHILD_PREPARE_TO_STORE ();
3708 * Since the /proc interface will not read individual registers,
3709 * we will cache these requests until the process is resumed, and
3710 * only then write them back to the inferior process.
3712 * FIXME: is that a really bad idea? Have to think about cases
3713 * where writing one register might affect the value of others, etc.
3717 procfs_store_registers (int regno
)
3719 gdb_fpregset_t
*fpregs
;
3720 gdb_gregset_t
*gregs
;
3725 pid
= PIDGET (inferior_ptid
);
3726 tid
= TIDGET (inferior_ptid
);
3728 /* First find procinfo for main process */
3729 pi
= find_procinfo_or_die (pid
, 0);
3731 /* If current lwp for process is not the same as requested thread
3732 (ie. inferior_ptid), then find procinfo for the requested thread. */
3735 (tid
!= proc_get_current_thread (pi
)))
3736 pi
= find_procinfo_or_die (pid
, tid
);
3739 error ("procfs: store_registers: failed to find procinfo for %s",
3740 target_pid_to_str (inferior_ptid
));
3742 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3743 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3745 fill_gregset (gregs
, regno
);
3746 if (!proc_set_gregs (pi
))
3747 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3749 if (FP0_REGNUM
>= 0) /* need floating point? */
3751 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3752 regno
== PC_REGNUM
||
3753 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3754 regno
== FP_REGNUM
||
3756 return; /* not a floating point register */
3758 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3759 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3761 fill_fpregset (fpregs
, regno
);
3762 if (!proc_set_fpregs (pi
))
3763 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3768 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3772 if (scall
== SYS_lwp_exit
)
3776 if (scall
== SYS_lwpexit
)
3783 syscall_is_exit (procinfo
*pi
, int scall
)
3786 if (scall
== SYS_exit
)
3789 #ifdef DYNAMIC_SYSCALLS
3790 if (find_syscall (pi
, "_exit") == scall
)
3797 syscall_is_exec (procinfo
*pi
, int scall
)
3800 if (scall
== SYS_exec
)
3804 if (scall
== SYS_execv
)
3808 if (scall
== SYS_execve
)
3811 #ifdef DYNAMIC_SYSCALLS
3812 if (find_syscall (pi
, "_execve"))
3814 if (find_syscall (pi
, "ra_execve"))
3821 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3823 #ifdef SYS_lwp_create
3824 if (scall
== SYS_lwp_create
)
3827 #ifdef SYS_lwpcreate
3828 if (scall
== SYS_lwpcreate
)
3835 * Function: target_wait
3837 * Retrieve the next stop event from the child process.
3838 * If child has not stopped yet, wait for it to stop.
3839 * Translate /proc eventcodes (or possibly wait eventcodes)
3840 * into gdb internal event codes.
3842 * Return: id of process (and possibly thread) that incurred the event.
3843 * event codes are returned thru a pointer parameter.
3847 procfs_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3849 /* First cut: loosely based on original version 2.1 */
3853 ptid_t retval
, temp_ptid
;
3854 int why
, what
, flags
;
3861 retval
= pid_to_ptid (-1);
3863 /* Find procinfo for main process */
3864 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
3867 /* We must assume that the status is stale now... */
3868 pi
->status_valid
= 0;
3869 pi
->gregs_valid
= 0;
3870 pi
->fpregs_valid
= 0;
3872 #if 0 /* just try this out... */
3873 flags
= proc_flags (pi
);
3874 why
= proc_why (pi
);
3875 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3876 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3878 /* If child is not stopped, wait for it to stop. */
3879 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3880 !proc_wait_for_stop (pi
))
3882 /* wait_for_stop failed: has the child terminated? */
3883 if (errno
== ENOENT
)
3887 /* /proc file not found; presumably child has terminated. */
3888 wait_retval
= wait (&wstat
); /* "wait" for the child's exit */
3890 if (wait_retval
!= PIDGET (inferior_ptid
)) /* wrong child? */
3891 error ("procfs: couldn't stop process %d: wait returned %d\n",
3892 PIDGET (inferior_ptid
), wait_retval
);
3893 /* FIXME: might I not just use waitpid?
3894 Or try find_procinfo to see if I know about this child? */
3895 retval
= pid_to_ptid (wait_retval
);
3897 else if (errno
== EINTR
)
3901 /* Unknown error from wait_for_stop. */
3902 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3907 /* This long block is reached if either:
3908 a) the child was already stopped, or
3909 b) we successfully waited for the child with wait_for_stop.
3910 This block will analyze the /proc status, and translate it
3911 into a waitstatus for GDB.
3913 If we actually had to call wait because the /proc file
3914 is gone (child terminated), then we skip this block,
3915 because we already have a waitstatus. */
3917 flags
= proc_flags (pi
);
3918 why
= proc_why (pi
);
3919 what
= proc_what (pi
);
3921 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3924 /* If it's running async (for single_thread control),
3925 set it back to normal again. */
3926 if (flags
& PR_ASYNC
)
3927 if (!proc_unset_async (pi
))
3928 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3932 proc_prettyprint_why (why
, what
, 1);
3934 /* The 'pid' we will return to GDB is composed of
3935 the process ID plus the lwp ID. */
3936 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3940 wstat
= (what
<< 8) | 0177;
3943 if (syscall_is_lwp_exit (pi
, what
))
3945 printf_filtered ("[%s exited]\n",
3946 target_pid_to_str (retval
));
3947 delete_thread (retval
);
3948 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3951 else if (syscall_is_exit (pi
, what
))
3953 /* Handle SYS_exit call only */
3954 /* Stopped at entry to SYS_exit.
3955 Make it runnable, resume it, then use
3956 the wait system call to get its exit code.
3957 Proc_run_process always clears the current
3959 Then return its exit status. */
3960 pi
->status_valid
= 0;
3962 /* FIXME: what we should do is return
3963 TARGET_WAITKIND_SPURIOUS. */
3964 if (!proc_run_process (pi
, 0, 0))
3965 proc_error (pi
, "target_wait, run_process", __LINE__
);
3968 /* Don't call wait: simulate waiting for exit,
3969 return a "success" exit code. Bogus: what if
3970 it returns something else? */
3972 retval
= inferior_ptid
; /* ? ? ? */
3976 int temp
= wait (&wstat
);
3978 /* FIXME: shouldn't I make sure I get the right
3979 event from the right process? If (for
3980 instance) I have killed an earlier inferior
3981 process but failed to clean up after it
3982 somehow, I could get its termination event
3985 /* If wait returns -1, that's what we return to GDB. */
3987 retval
= pid_to_ptid (temp
);
3992 printf_filtered ("procfs: trapped on entry to ");
3993 proc_prettyprint_syscall (proc_what (pi
), 0);
3994 printf_filtered ("\n");
3997 long i
, nsysargs
, *sysargs
;
3999 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4000 (sysargs
= proc_sysargs (pi
)) != NULL
)
4002 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4003 for (i
= 0; i
< nsysargs
; i
++)
4004 printf_filtered ("#%ld: 0x%08lx\n",
4012 /* How to exit gracefully, returning "unknown event" */
4013 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4014 return inferior_ptid
;
4018 /* How to keep going without returning to wfi: */
4019 target_resume (ptid
, 0, TARGET_SIGNAL_0
);
4025 if (syscall_is_exec (pi
, what
))
4027 /* Hopefully this is our own "fork-child" execing
4028 the real child. Hoax this event into a trap, and
4029 GDB will see the child about to execute its start
4031 wstat
= (SIGTRAP
<< 8) | 0177;
4033 else if (syscall_is_lwp_create (pi
, what
))
4036 * This syscall is somewhat like fork/exec.
4037 * We will get the event twice: once for the parent LWP,
4038 * and once for the child. We should already know about
4039 * the parent LWP, but the child will be new to us. So,
4040 * whenever we get this event, if it represents a new
4041 * thread, simply add the thread to the list.
4044 /* If not in procinfo list, add it. */
4045 temp_tid
= proc_get_current_thread (pi
);
4046 if (!find_procinfo (pi
->pid
, temp_tid
))
4047 create_procinfo (pi
->pid
, temp_tid
);
4049 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4050 /* If not in GDB's thread list, add it. */
4051 if (!in_thread_list (temp_ptid
))
4053 printf_filtered ("[New %s]\n",
4054 target_pid_to_str (temp_ptid
));
4055 add_thread (temp_ptid
);
4057 /* Return to WFI, but tell it to immediately resume. */
4058 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4059 return inferior_ptid
;
4061 else if (syscall_is_lwp_exit (pi
, what
))
4063 printf_filtered ("[%s exited]\n",
4064 target_pid_to_str (retval
));
4065 delete_thread (retval
);
4066 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4071 /* FIXME: Do we need to handle SYS_sproc,
4072 SYS_fork, or SYS_vfork here? The old procfs
4073 seemed to use this event to handle threads on
4074 older (non-LWP) systems, where I'm assuming
4075 that threads were actually separate processes.
4076 Irix, maybe? Anyway, low priority for now. */
4080 printf_filtered ("procfs: trapped on exit from ");
4081 proc_prettyprint_syscall (proc_what (pi
), 0);
4082 printf_filtered ("\n");
4085 long i
, nsysargs
, *sysargs
;
4087 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4088 (sysargs
= proc_sysargs (pi
)) != NULL
)
4090 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4091 for (i
= 0; i
< nsysargs
; i
++)
4092 printf_filtered ("#%ld: 0x%08lx\n",
4097 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4098 return inferior_ptid
;
4103 wstat
= (SIGSTOP
<< 8) | 0177;
4108 printf_filtered ("Retry #%d:\n", retry
);
4109 pi
->status_valid
= 0;
4114 /* If not in procinfo list, add it. */
4115 temp_tid
= proc_get_current_thread (pi
);
4116 if (!find_procinfo (pi
->pid
, temp_tid
))
4117 create_procinfo (pi
->pid
, temp_tid
);
4119 /* If not in GDB's thread list, add it. */
4120 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4121 if (!in_thread_list (temp_ptid
))
4123 printf_filtered ("[New %s]\n",
4124 target_pid_to_str (temp_ptid
));
4125 add_thread (temp_ptid
);
4128 status
->kind
= TARGET_WAITKIND_STOPPED
;
4129 status
->value
.sig
= 0;
4134 wstat
= (what
<< 8) | 0177;
4137 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
4140 wstat
= (SIGTRAP
<< 8) | 0177;
4145 wstat
= (SIGTRAP
<< 8) | 0177;
4148 /* FIXME: use si_signo where possible. */
4150 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4153 wstat
= (SIGILL
<< 8) | 0177;
4156 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4159 wstat
= (SIGTRAP
<< 8) | 0177;
4163 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4166 wstat
= (SIGSEGV
<< 8) | 0177;
4170 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4173 wstat
= (SIGFPE
<< 8) | 0177;
4175 case FLTPAGE
: /* Recoverable page fault */
4176 default: /* FIXME: use si_signo if possible for fault */
4177 retval
= pid_to_ptid (-1);
4178 printf_filtered ("procfs:%d -- ", __LINE__
);
4179 printf_filtered ("child stopped for unknown reason:\n");
4180 proc_prettyprint_why (why
, what
, 1);
4181 error ("... giving up...");
4184 break; /* case PR_FAULTED: */
4185 default: /* switch (why) unmatched */
4186 printf_filtered ("procfs:%d -- ", __LINE__
);
4187 printf_filtered ("child stopped for unknown reason:\n");
4188 proc_prettyprint_why (why
, what
, 1);
4189 error ("... giving up...");
4193 * Got this far without error:
4194 * If retval isn't in the threads database, add it.
4196 if (PIDGET (retval
) > 0 &&
4197 !ptid_equal (retval
, inferior_ptid
) &&
4198 !in_thread_list (retval
))
4201 * We have a new thread.
4202 * We need to add it both to GDB's list and to our own.
4203 * If we don't create a procinfo, resume may be unhappy
4206 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
4207 add_thread (retval
);
4208 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
4209 create_procinfo (PIDGET (retval
), TIDGET (retval
));
4211 /* In addition, it's possible that this is the first
4212 * new thread we've seen, in which case we may not
4213 * have created entries for inferior_ptid yet.
4215 if (TIDGET (inferior_ptid
) != 0)
4217 if (!in_thread_list (inferior_ptid
))
4218 add_thread (inferior_ptid
);
4219 if (find_procinfo (PIDGET (inferior_ptid
),
4220 TIDGET (inferior_ptid
)) == NULL
)
4221 create_procinfo (PIDGET (inferior_ptid
),
4222 TIDGET (inferior_ptid
));
4226 else /* flags do not indicate STOPPED */
4228 /* surely this can't happen... */
4229 printf_filtered ("procfs:%d -- process not stopped.\n",
4231 proc_prettyprint_flags (flags
, 1);
4232 error ("procfs: ...giving up...");
4237 store_waitstatus (status
, wstat
);
4243 /* Transfer LEN bytes between GDB address MYADDR and target address
4244 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4245 otherwise transfer them from the target. TARGET is unused.
4247 The return value is 0 if an error occurred or no bytes were
4248 transferred. Otherwise, it will be a positive value which
4249 indicates the number of bytes transferred between gdb and the
4250 target. (Note that the interface also makes provisions for
4251 negative values, but this capability isn't implemented here.) */
4254 procfs_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int dowrite
,
4255 struct mem_attrib
*attrib
, struct target_ops
*target
)
4260 /* Find procinfo for main process */
4261 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4262 if (pi
->as_fd
== 0 &&
4263 open_procinfo_files (pi
, FD_AS
) == 0)
4265 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4269 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4274 PROCFS_NOTE ("write memory: ");
4276 PROCFS_NOTE ("write memory: \n");
4278 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4282 PROCFS_NOTE ("read memory: \n");
4283 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4294 * Function: invalidate_cache
4296 * Called by target_resume before making child runnable.
4297 * Mark cached registers and status's invalid.
4298 * If there are "dirty" caches that need to be written back
4299 * to the child process, do that.
4301 * File descriptors are also cached.
4302 * As they are a limited resource, we cannot hold onto them indefinitely.
4303 * However, as they are expensive to open, we don't want to throw them
4304 * away indescriminately either. As a compromise, we will keep the
4305 * file descriptors for the parent process, but discard any file
4306 * descriptors we may have accumulated for the threads.
4309 * As this function is called by iterate_over_threads, it always
4310 * returns zero (so that iterate_over_threads will keep iterating).
4315 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4318 * About to run the child; invalidate caches and do any other cleanup.
4322 if (pi
->gregs_dirty
)
4323 if (parent
== NULL
||
4324 proc_get_current_thread (parent
) != pi
->tid
)
4325 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4326 proc_warn (pi
, "target_resume, set_gregs",
4328 if (FP0_REGNUM
>= 0)
4329 if (pi
->fpregs_dirty
)
4330 if (parent
== NULL
||
4331 proc_get_current_thread (parent
) != pi
->tid
)
4332 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4333 proc_warn (pi
, "target_resume, set_fpregs",
4339 /* The presence of a parent indicates that this is an LWP.
4340 Close any file descriptors that it might have open.
4341 We don't do this to the master (parent) procinfo. */
4343 close_procinfo_files (pi
);
4345 pi
->gregs_valid
= 0;
4346 pi
->fpregs_valid
= 0;
4348 pi
->gregs_dirty
= 0;
4349 pi
->fpregs_dirty
= 0;
4351 pi
->status_valid
= 0;
4352 pi
->threads_valid
= 0;
4359 * Function: make_signal_thread_runnable
4361 * A callback function for iterate_over_threads.
4362 * Find the asynchronous signal thread, and make it runnable.
4363 * See if that helps matters any.
4367 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4370 if (proc_flags (pi
) & PR_ASLWP
)
4372 if (!proc_run_process (pi
, 0, -1))
4373 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4382 * Function: target_resume
4384 * Make the child process runnable. Normally we will then call
4385 * procfs_wait and wait for it to stop again (unles gdb is async).
4388 * step: if true, then arrange for the child to stop again
4389 * after executing a single instruction.
4390 * signo: if zero, then cancel any pending signal.
4391 * If non-zero, then arrange for the indicated signal
4392 * to be delivered to the child when it runs.
4393 * pid: if -1, then allow any child thread to run.
4394 * if non-zero, then allow only the indicated thread to run.
4395 ******* (not implemented yet)
4399 procfs_resume (ptid_t ptid
, int step
, enum target_signal signo
)
4401 procinfo
*pi
, *thread
;
4405 prrun.prflags |= PRSVADDR;
4406 prrun.pr_vaddr = $PC; set resume address
4407 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4408 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4409 prrun.prflags |= PRCFAULT; clear current fault.
4411 PRSTRACE and PRSFAULT can be done by other means
4412 (proc_trace_signals, proc_trace_faults)
4413 PRSVADDR is unnecessary.
4414 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4415 This basically leaves PRSTEP and PRCSIG.
4416 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4417 So basically PR_STEP is the sole argument that must be passed
4418 to proc_run_process (for use in the prrun struct by ioctl). */
4420 /* Find procinfo for main process */
4421 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4423 /* First cut: ignore pid argument */
4426 /* Convert signal to host numbering. */
4428 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4431 native_signo
= target_signal_to_host (signo
);
4433 pi
->ignore_next_sigstop
= 0;
4435 /* Running the process voids all cached registers and status. */
4436 /* Void the threads' caches first */
4437 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4438 /* Void the process procinfo's caches. */
4439 invalidate_cache (NULL
, pi
, NULL
);
4441 if (PIDGET (ptid
) != -1)
4443 /* Resume a specific thread, presumably suppressing the others. */
4444 thread
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
));
4447 if (thread
->tid
!= 0)
4449 /* We're to resume a specific thread, and not the others.
4450 * Set the child process's PR_ASYNC flag.
4453 if (!proc_set_async (pi
))
4454 proc_error (pi
, "target_resume, set_async", __LINE__
);
4457 proc_iterate_over_threads (pi
,
4458 make_signal_thread_runnable
,
4461 pi
= thread
; /* substitute the thread's procinfo for run */
4466 if (!proc_run_process (pi
, step
, native_signo
))
4469 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4471 proc_error (pi
, "target_resume", __LINE__
);
4476 * Function: register_gdb_signals
4478 * Traverse the list of signals that GDB knows about
4479 * (see "handle" command), and arrange for the target
4480 * to be stopped or not, according to these settings.
4482 * Returns non-zero for success, zero for failure.
4486 register_gdb_signals (procinfo
*pi
, gdb_sigset_t
*signals
)
4490 for (signo
= 0; signo
< NSIG
; signo
++)
4491 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4492 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4493 signal_pass_state (target_signal_from_host (signo
)) == 1)
4494 prdelset (signals
, signo
);
4496 praddset (signals
, signo
);
4498 return proc_set_traced_signals (pi
, signals
);
4502 * Function: target_notice_signals
4504 * Set up to trace signals in the child process.
4508 procfs_notice_signals (ptid_t ptid
)
4510 gdb_sigset_t signals
;
4511 procinfo
*pi
= find_procinfo_or_die (PIDGET (ptid
), 0);
4513 if (proc_get_traced_signals (pi
, &signals
) &&
4514 register_gdb_signals (pi
, &signals
))
4517 proc_error (pi
, "notice_signals", __LINE__
);
4521 * Function: target_files_info
4523 * Print status information about the child process.
4527 procfs_files_info (struct target_ops
*ignore
)
4529 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4530 attach_flag
? "attached": "child",
4531 target_pid_to_str (inferior_ptid
));
4535 * Function: target_open
4537 * A dummy: you don't open procfs.
4541 procfs_open (char *args
, int from_tty
)
4543 error ("Use the \"run\" command to start a Unix child process.");
4547 * Function: target_can_run
4549 * This tells GDB that this target vector can be invoked
4550 * for "run" or "attach".
4553 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4554 be a runnable target. Used by targets
4555 that can sit atop procfs, such as solaris
4560 procfs_can_run (void)
4562 /* This variable is controlled by modules that sit atop procfs that
4563 may layer their own process structure atop that provided here.
4564 sol-thread.c does this because of the Solaris two-level thread
4567 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4569 return !procfs_suppress_run
;
4573 * Function: target_stop
4575 * Stop the child process asynchronously, as when the
4576 * gdb user types control-c or presses a "stop" button.
4578 * Works by sending kill(SIGINT) to the child's process group.
4584 extern pid_t inferior_process_group
;
4586 kill (-inferior_process_group
, SIGINT
);
4590 * Function: unconditionally_kill_inferior
4592 * Make it die. Wait for it to die. Clean up after it.
4593 * Note: this should only be applied to the real process,
4594 * not to an LWP, because of the check for parent-process.
4595 * If we need this to work for an LWP, it needs some more logic.
4599 unconditionally_kill_inferior (procinfo
*pi
)
4603 parent_pid
= proc_parent_pid (pi
);
4604 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4605 /* FIXME: use access functions */
4606 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4607 before the PIOCKILL, otherwise it might generate a corrupted core
4608 file for the inferior. */
4609 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4611 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4614 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4615 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4616 to kill the inferior, otherwise it might remain stopped with a
4618 We do not check the result of the PIOCSSIG, the inferior might have
4621 gdb_siginfo_t newsiginfo
;
4623 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4624 newsiginfo
.si_signo
= SIGKILL
;
4625 newsiginfo
.si_code
= 0;
4626 newsiginfo
.si_errno
= 0;
4627 newsiginfo
.si_pid
= getpid ();
4628 newsiginfo
.si_uid
= getuid ();
4629 /* FIXME: use proc_set_current_signal */
4630 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4632 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4633 if (!proc_kill (pi
, SIGKILL
))
4634 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4635 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4636 destroy_procinfo (pi
);
4638 /* If pi is GDB's child, wait for it to die. */
4639 if (parent_pid
== getpid ())
4640 /* FIXME: should we use waitpid to make sure we get the right event?
4641 Should we check the returned event? */
4646 ret
= waitpid (pi
->pid
, &status
, 0);
4654 * Function: target_kill_inferior
4656 * We're done debugging it, and we want it to go away.
4657 * Then we want GDB to forget all about it.
4661 procfs_kill_inferior (void)
4663 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4665 /* Find procinfo for main process */
4666 procinfo
*pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4669 unconditionally_kill_inferior (pi
);
4670 target_mourn_inferior ();
4675 * Function: target_mourn_inferior
4677 * Forget we ever debugged this thing!
4681 procfs_mourn_inferior (void)
4685 if (!ptid_equal (inferior_ptid
, null_ptid
))
4687 /* Find procinfo for main process */
4688 pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4690 destroy_procinfo (pi
);
4692 unpush_target (&procfs_ops
);
4693 generic_mourn_inferior ();
4697 * Function: init_inferior
4699 * When GDB forks to create a runnable inferior process,
4700 * this function is called on the parent side of the fork.
4701 * It's job is to do whatever is necessary to make the child
4702 * ready to be debugged, and then wait for the child to synchronize.
4706 procfs_init_inferior (int pid
)
4709 gdb_sigset_t signals
;
4712 /* This routine called on the parent side (GDB side)
4713 after GDB forks the inferior. */
4715 push_target (&procfs_ops
);
4717 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4718 perror ("procfs: out of memory in 'init_inferior'");
4720 if (!open_procinfo_files (pi
, FD_CTL
))
4721 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4725 open_procinfo_files // done
4728 procfs_notice_signals
4735 /* If not stopped yet, wait for it to stop. */
4736 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4737 !(proc_wait_for_stop (pi
)))
4738 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4740 /* Save some of the /proc state to be restored if we detach. */
4741 /* FIXME: Why? In case another debugger was debugging it?
4742 We're it's parent, for Ghu's sake! */
4743 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4744 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4745 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4746 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4747 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4748 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4749 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4750 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4751 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4752 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4754 /* Register to trace selected signals in the child. */
4755 prfillset (&signals
);
4756 if (!register_gdb_signals (pi
, &signals
))
4757 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4759 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4760 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4762 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4763 and possibly even turning ON kill-on-last-close at this point. But
4764 I can't make that change without careful testing which I don't have
4765 time to do right now... */
4766 /* Turn on run-on-last-close flag so that the child
4767 will die if GDB goes away for some reason. */
4768 if (!proc_set_run_on_last_close (pi
))
4769 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4771 /* The 'process ID' we return to GDB is composed of
4772 the actual process ID plus the lwp ID. */
4773 inferior_ptid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4775 #ifdef START_INFERIOR_TRAPS_EXPECTED
4776 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4778 /* One trap to exec the shell, one to exec the program being debugged. */
4779 startup_inferior (2);
4780 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4784 * Function: set_exec_trap
4786 * When GDB forks to create a new process, this function is called
4787 * on the child side of the fork before GDB exec's the user program.
4788 * Its job is to make the child minimally debuggable, so that the
4789 * parent GDB process can connect to the child and take over.
4790 * This function should do only the minimum to make that possible,
4791 * and to synchronize with the parent process. The parent process
4792 * should take care of the details.
4796 procfs_set_exec_trap (void)
4798 /* This routine called on the child side (inferior side)
4799 after GDB forks the inferior. It must use only local variables,
4800 because it may be sharing data space with its parent. */
4805 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4806 perror_with_name ("procfs: create_procinfo failed in child.");
4808 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4810 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4811 gdb_flush (gdb_stderr
);
4812 /* no need to call "dead_procinfo", because we're going to exit. */
4816 #ifdef PRFS_STOPEXEC /* defined on OSF */
4817 /* OSF method for tracing exec syscalls. Quoting:
4818 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4819 exits from exec system calls because of the user level loader. */
4820 /* FIXME: make nice and maybe move into an access function. */
4824 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4826 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4827 gdb_flush (gdb_stderr
);
4830 prfs_flags
|= PRFS_STOPEXEC
;
4832 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4834 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4835 gdb_flush (gdb_stderr
);
4839 #else /* not PRFS_STOPEXEC */
4840 /* Everyone else's (except OSF) method for tracing exec syscalls */
4842 Not all systems with /proc have all the exec* syscalls with the same
4843 names. On the SGI, for example, there is no SYS_exec, but there
4844 *is* a SYS_execv. So, we try to account for that. */
4846 exitset
= sysset_t_alloc (pi
);
4847 gdb_premptysysset (exitset
);
4849 gdb_praddsysset (exitset
, SYS_exec
);
4852 gdb_praddsysset (exitset
, SYS_execve
);
4855 gdb_praddsysset (exitset
, SYS_execv
);
4857 #ifdef DYNAMIC_SYSCALLS
4859 int callnum
= find_syscall (pi
, "execve");
4862 gdb_praddsysset (exitset
, callnum
);
4864 callnum
= find_syscall (pi
, "ra_execve");
4866 gdb_praddsysset (exitset
, callnum
);
4868 #endif /* DYNAMIC_SYSCALLS */
4870 if (!proc_set_traced_sysexit (pi
, exitset
))
4872 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4873 gdb_flush (gdb_stderr
);
4876 #endif /* PRFS_STOPEXEC */
4878 /* FIXME: should this be done in the parent instead? */
4879 /* Turn off inherit on fork flag so that all grand-children
4880 of gdb start with tracing flags cleared. */
4881 if (!proc_unset_inherit_on_fork (pi
))
4882 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4884 /* Turn off run on last close flag, so that the child process
4885 cannot run away just because we close our handle on it.
4886 We want it to wait for the parent to attach. */
4887 if (!proc_unset_run_on_last_close (pi
))
4888 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4890 /* FIXME: No need to destroy the procinfo --
4891 we have our own address space, and we're about to do an exec! */
4892 /*destroy_procinfo (pi);*/
4896 * Function: create_inferior
4898 * This function is called BEFORE gdb forks the inferior process.
4899 * Its only real responsibility is to set things up for the fork,
4900 * and tell GDB which two functions to call after the fork (one
4901 * for the parent, and one for the child).
4903 * This function does a complicated search for a unix shell program,
4904 * which it then uses to parse arguments and environment variables
4905 * to be sent to the child. I wonder whether this code could not
4906 * be abstracted out and shared with other unix targets such as
4911 procfs_create_inferior (char *exec_file
, char *allargs
, char **env
)
4913 char *shell_file
= getenv ("SHELL");
4915 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4918 /* We will be looking down the PATH to find shell_file. If we
4919 just do this the normal way (via execlp, which operates by
4920 attempting an exec for each element of the PATH until it
4921 finds one which succeeds), then there will be an exec for
4922 each failed attempt, each of which will cause a PR_SYSEXIT
4923 stop, and we won't know how to distinguish the PR_SYSEXIT's
4924 for these failed execs with the ones for successful execs
4925 (whether the exec has succeeded is stored at that time in the
4926 carry bit or some such architecture-specific and
4927 non-ABI-specified place).
4929 So I can't think of anything better than to search the PATH
4930 now. This has several disadvantages: (1) There is a race
4931 condition; if we find a file now and it is deleted before we
4932 exec it, we lose, even if the deletion leaves a valid file
4933 further down in the PATH, (2) there is no way to know exactly
4934 what an executable (in the sense of "capable of being
4935 exec'd") file is. Using access() loses because it may lose
4936 if the caller is the superuser; failing to use it loses if
4937 there are ACLs or some such. */
4941 /* FIXME-maybe: might want "set path" command so user can change what
4942 path is used from within GDB. */
4943 char *path
= getenv ("PATH");
4945 struct stat statbuf
;
4948 path
= "/bin:/usr/bin";
4950 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4951 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4953 p1
= strchr (p
, ':');
4958 strncpy (tryname
, p
, len
);
4959 tryname
[len
] = '\0';
4960 strcat (tryname
, "/");
4961 strcat (tryname
, shell_file
);
4962 if (access (tryname
, X_OK
) < 0)
4964 if (stat (tryname
, &statbuf
) < 0)
4966 if (!S_ISREG (statbuf
.st_mode
))
4967 /* We certainly need to reject directories. I'm not quite
4968 as sure about FIFOs, sockets, etc., but I kind of doubt
4969 that people want to exec() these things. */
4974 /* Not found. This must be an error rather than merely passing
4975 the file to execlp(), because execlp() would try all the
4976 exec()s, causing GDB to get confused. */
4977 error ("procfs:%d -- Can't find shell %s in PATH",
4978 __LINE__
, shell_file
);
4980 shell_file
= tryname
;
4983 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4984 procfs_init_inferior
, NULL
, shell_file
);
4986 /* We are at the first instruction we care about. */
4987 /* Pedal to the metal... */
4989 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
4993 * Function: notice_thread
4995 * Callback for find_new_threads.
4996 * Calls "add_thread".
5000 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
5002 ptid_t gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
5004 if (!in_thread_list (gdb_threadid
))
5005 add_thread (gdb_threadid
);
5011 * Function: target_find_new_threads
5013 * Query all the threads that the target knows about,
5014 * and give them back to GDB to add to its list.
5018 procfs_find_new_threads (void)
5022 /* Find procinfo for main process */
5023 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5024 proc_update_threads (pi
);
5025 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
5029 * Function: target_thread_alive
5031 * Return true if the thread is still 'alive'.
5033 * This guy doesn't really seem to be doing his job.
5034 * Got to investigate how to tell when a thread is really gone.
5038 procfs_thread_alive (ptid_t ptid
)
5043 proc
= PIDGET (ptid
);
5044 thread
= TIDGET (ptid
);
5045 /* If I don't know it, it ain't alive! */
5046 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
5049 /* If I can't get its status, it ain't alive!
5050 What's more, I need to forget about it! */
5051 if (!proc_get_status (pi
))
5053 destroy_procinfo (pi
);
5056 /* I couldn't have got its status if it weren't alive, so it's alive. */
5061 * Function: target_pid_to_str
5063 * Return a string to be used to identify the thread in
5064 * the "info threads" display.
5068 procfs_pid_to_str (ptid_t ptid
)
5070 static char buf
[80];
5074 proc
= PIDGET (ptid
);
5075 thread
= TIDGET (ptid
);
5076 pi
= find_procinfo (proc
, thread
);
5079 sprintf (buf
, "Process %d", proc
);
5081 sprintf (buf
, "LWP %d", thread
);
5086 * Function: procfs_set_watchpoint
5087 * Insert a watchpoint
5091 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
5099 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5100 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5102 /* Translate from GDB's flags to /proc's */
5103 if (len
> 0) /* len == 0 means delete watchpoint */
5105 switch (rwflag
) { /* FIXME: need an enum! */
5106 case hw_write
: /* default watchpoint (write) */
5107 pflags
= WRITE_WATCHFLAG
;
5109 case hw_read
: /* read watchpoint */
5110 pflags
= READ_WATCHFLAG
;
5112 case hw_access
: /* access watchpoint */
5113 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
5115 case hw_execute
: /* execution HW breakpoint */
5116 pflags
= EXEC_WATCHFLAG
;
5118 default: /* Something weird. Return error. */
5121 if (after
) /* Stop after r/w access is completed. */
5122 pflags
|= AFTER_WATCHFLAG
;
5125 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
5127 if (errno
== E2BIG
) /* Typical error for no resources */
5128 return -1; /* fail */
5129 /* GDB may try to remove the same watchpoint twice.
5130 If a remove request returns no match, don't error. */
5131 if (errno
== ESRCH
&& len
== 0)
5132 return 0; /* ignore */
5133 proc_error (pi
, "set_watchpoint", __LINE__
);
5136 #endif /* UNIXWARE */
5141 * Function: stopped_by_watchpoint
5143 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5144 * else returns zero.
5148 procfs_stopped_by_watchpoint (ptid_t ptid
)
5152 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5153 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5155 if (!pi
) /* If no process, then not stopped by watchpoint! */
5158 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
5160 if (proc_why (pi
) == PR_FAULTED
)
5163 if (proc_what (pi
) == FLTWATCH
)
5167 if (proc_what (pi
) == FLTKWATCH
)
5175 #ifdef TM_I386SOL2_H
5177 * Function: procfs_find_LDT_entry
5180 * ptid_t ptid; // The GDB-style pid-plus-LWP.
5183 * pointer to the corresponding LDT entry.
5187 procfs_find_LDT_entry (ptid_t ptid
)
5189 gdb_gregset_t
*gregs
;
5193 /* Find procinfo for the lwp. */
5194 if ((pi
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
))) == NULL
)
5196 warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.",
5197 PIDGET (ptid
), TIDGET (ptid
));
5200 /* get its general registers. */
5201 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
5203 warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.",
5204 PIDGET (ptid
), TIDGET (ptid
));
5207 /* Now extract the GS register's lower 16 bits. */
5208 key
= (*gregs
)[GS
] & 0xffff;
5210 /* Find the matching entry and return it. */
5211 return proc_get_LDT_entry (pi
, key
);
5213 #endif /* TM_I386SOL2_H */
5216 * Memory Mappings Functions:
5220 * Function: iterate_over_mappings
5222 * Call a callback function once for each mapping, passing it the mapping,
5223 * an optional secondary callback function, and some optional opaque data.
5224 * Quit and return the first non-zero value returned from the callback.
5227 * pi -- procinfo struct for the process to be mapped.
5228 * func -- callback function to be called by this iterator.
5229 * data -- optional opaque data to be passed to the callback function.
5230 * child_func -- optional secondary function pointer to be passed
5231 * to the child function.
5233 * Return: First non-zero return value from the callback function,
5238 iterate_over_mappings (procinfo
*pi
, int (*child_func
) (), void *data
,
5239 int (*func
) (struct prmap
*map
,
5240 int (*child_func
) (),
5243 char pathname
[MAX_PROC_NAME_SIZE
];
5244 struct prmap
*prmaps
;
5245 struct prmap
*prmap
;
5253 /* Get the number of mappings, allocate space,
5254 and read the mappings into prmaps. */
5257 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
5258 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
5259 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
5261 /* Make sure it gets closed again. */
5262 make_cleanup_close (map_fd
);
5264 /* Use stat to determine the file size, and compute
5265 the number of prmap_t objects it contains. */
5266 if (fstat (map_fd
, &sbuf
) != 0)
5267 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
5269 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
5270 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5271 if (read (map_fd
, (char *) prmaps
, nmap
* sizeof (*prmaps
))
5272 != (nmap
* sizeof (*prmaps
)))
5273 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
5275 /* Use ioctl command PIOCNMAP to get number of mappings. */
5276 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmap
) != 0)
5277 proc_error (pi
, "iterate_over_mappings (PIOCNMAP)", __LINE__
);
5279 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5280 if (ioctl (pi
->ctl_fd
, PIOCMAP
, prmaps
) != 0)
5281 proc_error (pi
, "iterate_over_mappings (PIOCMAP)", __LINE__
);
5284 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
5285 if ((funcstat
= (*func
) (prmap
, child_func
, data
)) != 0)
5292 * Function: solib_mappings_callback
5294 * Calls the supplied callback function once for each mapped address
5295 * space in the process. The callback function receives an open
5296 * file descriptor for the file corresponding to that mapped
5297 * address space (if there is one), and the base address of the
5298 * mapped space. Quit when the callback function returns a
5299 * nonzero value, or at teh end of the mappings.
5301 * Returns: the first non-zero return value of the callback function,
5305 int solib_mappings_callback (struct prmap
*map
,
5306 int (*func
) (int, CORE_ADDR
),
5309 procinfo
*pi
= data
;
5313 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
5315 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
5316 return -1; /* sanity */
5318 if (map
->pr_mapname
[0] == 0)
5320 fd
= -1; /* no map file */
5324 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
5325 /* Note: caller's responsibility to close this fd! */
5326 fd
= open_with_retry (name
, O_RDONLY
);
5327 /* Note: we don't test the above call for failure;
5328 we just pass the FD on as given. Sometimes there is
5329 no file, so the open may return failure, but that's
5333 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
->pr_vaddr
);
5334 /* Note: we don't test the above call for failure;
5335 we just pass the FD on as given. Sometimes there is
5336 no file, so the ioctl may return failure, but that's
5339 return (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
);
5343 * Function: proc_iterate_over_mappings
5345 * Uses the unified "iterate_over_mappings" function
5346 * to implement the exported interface to solib-svr4.c.
5348 * Given a pointer to a function, call that function once for every
5349 * mapped address space in the process. The callback function
5350 * receives an open file descriptor for the file corresponding to
5351 * that mapped address space (if there is one), and the base address
5352 * of the mapped space. Quit when the callback function returns a
5353 * nonzero value, or at teh end of the mappings.
5355 * Returns: the first non-zero return value of the callback function,
5360 proc_iterate_over_mappings (int (*func
) (int, CORE_ADDR
))
5362 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5364 return iterate_over_mappings (pi
, func
, pi
, solib_mappings_callback
);
5368 * Function: find_memory_regions_callback
5370 * Implements the to_find_memory_regions method.
5371 * Calls an external function for each memory region.
5372 * External function will have the signiture:
5374 * int callback (CORE_ADDR vaddr,
5375 * unsigned long size,
5376 * int read, int write, int execute,
5379 * Returns the integer value returned by the callback.
5383 find_memory_regions_callback (struct prmap
*map
,
5384 int (*func
) (CORE_ADDR
,
5390 return (*func
) (host_pointer_to_address ((void *) map
->pr_vaddr
),
5392 (map
->pr_mflags
& MA_READ
) != 0,
5393 (map
->pr_mflags
& MA_WRITE
) != 0,
5394 (map
->pr_mflags
& MA_EXEC
) != 0,
5399 * Function: proc_find_memory_regions
5401 * External interface. Calls a callback function once for each
5402 * mapped memory region in the child process, passing as arguments
5403 * CORE_ADDR virtual_address,
5404 * unsigned long size,
5405 * int read, TRUE if region is readable by the child
5406 * int write, TRUE if region is writable by the child
5407 * int execute TRUE if region is executable by the child.
5409 * Stops iterating and returns the first non-zero value
5410 * returned by the callback.
5414 proc_find_memory_regions (int (*func
) (CORE_ADDR
,
5420 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5422 return iterate_over_mappings (pi
, func
, data
,
5423 find_memory_regions_callback
);
5427 * Function: mappingflags
5429 * Returns an ascii representation of a memory mapping's flags.
5433 mappingflags (flags
)
5436 static char asciiflags
[8];
5438 strcpy (asciiflags
, "-------");
5439 #if defined (MA_PHYS)
5440 if (flags
& MA_PHYS
)
5441 asciiflags
[0] = 'd';
5443 if (flags
& MA_STACK
)
5444 asciiflags
[1] = 's';
5445 if (flags
& MA_BREAK
)
5446 asciiflags
[2] = 'b';
5447 if (flags
& MA_SHARED
)
5448 asciiflags
[3] = 's';
5449 if (flags
& MA_READ
)
5450 asciiflags
[4] = 'r';
5451 if (flags
& MA_WRITE
)
5452 asciiflags
[5] = 'w';
5453 if (flags
& MA_EXEC
)
5454 asciiflags
[6] = 'x';
5455 return (asciiflags
);
5459 * Function: info_mappings_callback
5461 * Callback function, does the actual work for 'info proc mappings'.
5466 info_mappings_callback (struct prmap
*map
, int (*ignore
) (), void *unused
)
5468 char *data_fmt_string
;
5470 if (TARGET_ADDR_BIT
== 32)
5471 data_fmt_string
= "\t%#10lx %#10lx %#10x %#10x %7s\n";
5473 data_fmt_string
= " %#18lx %#18lx %#10x %#10x %7s\n";
5475 printf_filtered (data_fmt_string
,
5476 (unsigned long) map
->pr_vaddr
,
5477 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5479 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5480 (unsigned int) map
->pr_offset
,
5484 mappingflags (map
->pr_mflags
));
5490 * Function: info_proc_mappings
5492 * Implement the "info proc mappings" subcommand.
5496 info_proc_mappings (procinfo
*pi
, int summary
)
5498 char *header_fmt_string
;
5500 if (TARGET_PTR_BIT
== 32)
5501 header_fmt_string
= "\t%10s %10s %10s %10s %7s\n";
5503 header_fmt_string
= " %18s %18s %10s %10s %7s\n";
5506 return; /* No output for summary mode. */
5508 printf_filtered ("Mapped address spaces:\n\n");
5509 printf_filtered (header_fmt_string
,
5516 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
5517 printf_filtered ("\n");
5521 * Function: info_proc_cmd
5523 * Implement the "info proc" command.
5527 info_proc_cmd (char *args
, int from_tty
)
5529 struct cleanup
*old_chain
;
5530 procinfo
*process
= NULL
;
5531 procinfo
*thread
= NULL
;
5538 old_chain
= make_cleanup (null_cleanup
, 0);
5541 if ((argv
= buildargv (args
)) == NULL
)
5544 make_cleanup_freeargv (argv
);
5546 while (argv
!= NULL
&& *argv
!= NULL
)
5548 if (isdigit (argv
[0][0]))
5550 pid
= strtoul (argv
[0], &tmp
, 10);
5552 tid
= strtoul (++tmp
, NULL
, 10);
5554 else if (argv
[0][0] == '/')
5556 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5558 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
5569 pid
= PIDGET (inferior_ptid
);
5571 error ("No current process: you must name one.");
5574 /* Have pid, will travel.
5575 First see if it's a process we're already debugging. */
5576 process
= find_procinfo (pid
, 0);
5577 if (process
== NULL
)
5579 /* No. So open a procinfo for it, but
5580 remember to close it again when finished. */
5581 process
= create_procinfo (pid
, 0);
5582 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5583 if (!open_procinfo_files (process
, FD_CTL
))
5584 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5588 thread
= create_procinfo (pid
, tid
);
5592 printf_filtered ("process %d flags:\n", process
->pid
);
5593 proc_prettyprint_flags (proc_flags (process
), 1);
5594 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5595 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5596 if (proc_get_nthreads (process
) > 1)
5597 printf_filtered ("Process has %d threads.\n",
5598 proc_get_nthreads (process
));
5602 printf_filtered ("thread %d flags:\n", thread
->tid
);
5603 proc_prettyprint_flags (proc_flags (thread
), 1);
5604 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5605 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5610 info_proc_mappings (process
, 0);
5613 do_cleanups (old_chain
);
5617 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5623 if (PIDGET (inferior_ptid
) <= 0)
5624 error ("you must be debugging a process to use this command.");
5626 if (args
== NULL
|| args
[0] == 0)
5627 error_no_arg ("system call to trace");
5629 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5630 if (isdigit (args
[0]))
5632 syscallnum
= atoi (args
);
5633 if (entry_or_exit
== PR_SYSENTRY
)
5634 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5636 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5639 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5641 if (mode
== FLAG_SET
)
5642 gdb_praddsysset (sysset
, syscallnum
);
5644 gdb_prdelsysset (sysset
, syscallnum
);
5646 if (entry_or_exit
== PR_SYSENTRY
)
5648 if (!proc_set_traced_sysentry (pi
, sysset
))
5649 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5653 if (!proc_set_traced_sysexit (pi
, sysset
))
5654 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5660 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5662 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5666 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5668 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5672 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5674 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5678 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5680 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5685 _initialize_procfs (void)
5688 add_target (&procfs_ops
);
5689 add_info ("proc", info_proc_cmd
,
5690 "Show /proc process information about any running process.\n\
5691 Specify process id, or use the program being debugged by default.\n\
5692 Specify keyword 'mappings' for detailed info on memory mappings.");
5693 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5694 "Give a trace of entries into the syscall.");
5695 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5696 "Give a trace of exits from the syscall.");
5697 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5698 "Cancel a trace of entries into the syscall.");
5699 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5700 "Cancel a trace of exits from the syscall.");
5703 /* =================== END, GDB "MODULE" =================== */
5707 /* miscelaneous stubs: */
5708 /* The following satisfy a few random symbols mostly created by */
5709 /* the solaris threads implementation, which I will chase down */
5713 * Return a pid for which we guarantee
5714 * we will be able to find a 'live' procinfo.
5718 procfs_first_available (void)
5720 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);
5723 /* =================== GCORE .NOTE "MODULE" =================== */
5726 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
5727 char *note_data
, int *note_size
)
5729 gdb_gregset_t gregs
;
5730 gdb_fpregset_t fpregs
;
5731 unsigned long merged_pid
;
5733 merged_pid
= TIDGET (ptid
) << 16 | PIDGET (ptid
);
5735 fill_gregset (&gregs
, -1);
5736 note_data
= (char *) elfcore_write_prstatus (obfd
,
5742 fill_fpregset (&fpregs
, -1);
5743 note_data
= (char *) elfcore_write_prfpreg (obfd
,
5751 struct procfs_corefile_thread_data
{
5758 procfs_corefile_thread_callback (struct thread_info
*ti
, void *data
)
5760 struct procfs_corefile_thread_data
*args
= data
;
5761 procinfo
*pi
= find_procinfo (PIDGET (ti
->ptid
), TIDGET (ti
->ptid
));
5763 if (pi
!= NULL
&& TIDGET (ti
->ptid
) != 0)
5765 ptid_t saved_ptid
= inferior_ptid
;
5766 inferior_ptid
= ti
->ptid
;
5767 args
->note_data
= procfs_do_thread_registers (args
->obfd
, ti
->ptid
,
5770 inferior_ptid
= saved_ptid
;
5776 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5778 struct cleanup
*old_chain
;
5779 gdb_gregset_t gregs
;
5780 gdb_fpregset_t fpregs
;
5781 char fname
[16] = {'\0'};
5782 char psargs
[80] = {'\0'};
5783 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5784 char *note_data
= NULL
;
5785 struct procfs_corefile_thread_data thread_args
;
5787 if (get_exec_file (0))
5789 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
5790 strncpy (psargs
, get_exec_file (0),
5792 if (get_inferior_args ())
5794 strncat (psargs
, " ",
5795 sizeof (psargs
) - strlen (psargs
));
5796 strncat (psargs
, get_inferior_args (),
5797 sizeof (psargs
) - strlen (psargs
));
5801 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
5807 thread_args
.obfd
= obfd
;
5808 thread_args
.note_data
= note_data
;
5809 thread_args
.note_size
= note_size
;
5810 iterate_over_threads (procfs_corefile_thread_callback
, &thread_args
);
5811 if (thread_args
.note_data
== note_data
)
5813 /* iterate_over_threads didn't come up with any threads;
5814 just use inferior_ptid. */
5815 note_data
= procfs_do_thread_registers (obfd
, inferior_ptid
,
5816 note_data
, note_size
);
5820 note_data
= thread_args
.note_data
;
5823 make_cleanup (xfree
, note_data
);
5827 /* =================== END GCORE .NOTE "MODULE" =================== */