1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
3 Written by Michael Snyder at Cygnus Solutions.
4 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "gdbthread.h"
29 #if defined (NEW_PROC_API)
30 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
33 #include <sys/procfs.h>
34 #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 struct target_ops procfs_ops
; /* the target vector */
133 init_procfs_ops (void)
135 procfs_ops
.to_shortname
= "procfs";
136 procfs_ops
.to_longname
= "Unix /proc child process";
138 "Unix /proc child process (started by the \"run\" command).";
139 procfs_ops
.to_open
= procfs_open
;
140 procfs_ops
.to_can_run
= procfs_can_run
;
141 procfs_ops
.to_create_inferior
= procfs_create_inferior
;
142 procfs_ops
.to_kill
= procfs_kill_inferior
;
143 procfs_ops
.to_mourn_inferior
= procfs_mourn_inferior
;
144 procfs_ops
.to_attach
= procfs_attach
;
145 procfs_ops
.to_detach
= procfs_detach
;
146 procfs_ops
.to_wait
= procfs_wait
;
147 procfs_ops
.to_resume
= procfs_resume
;
148 procfs_ops
.to_prepare_to_store
= procfs_prepare_to_store
;
149 procfs_ops
.to_fetch_registers
= procfs_fetch_registers
;
150 procfs_ops
.to_store_registers
= procfs_store_registers
;
151 procfs_ops
.to_xfer_memory
= procfs_xfer_memory
;
152 procfs_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
153 procfs_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
154 procfs_ops
.to_notice_signals
= procfs_notice_signals
;
155 procfs_ops
.to_files_info
= procfs_files_info
;
156 procfs_ops
.to_stop
= procfs_stop
;
158 procfs_ops
.to_terminal_init
= terminal_init_inferior
;
159 procfs_ops
.to_terminal_inferior
= terminal_inferior
;
160 procfs_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
161 procfs_ops
.to_terminal_ours
= terminal_ours
;
162 procfs_ops
.to_terminal_info
= child_terminal_info
;
164 procfs_ops
.to_find_new_threads
= procfs_find_new_threads
;
165 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
166 procfs_ops
.to_pid_to_str
= procfs_pid_to_str
;
168 procfs_ops
.to_has_all_memory
= 1;
169 procfs_ops
.to_has_memory
= 1;
170 procfs_ops
.to_has_execution
= 1;
171 procfs_ops
.to_has_stack
= 1;
172 procfs_ops
.to_has_registers
= 1;
173 procfs_ops
.to_stratum
= process_stratum
;
174 procfs_ops
.to_has_thread_control
= tc_schedlock
;
175 procfs_ops
.to_magic
= OPS_MAGIC
;
178 /* =================== END, TARGET_OPS "MODULE" =================== */
183 * Put any typedefs, defines etc. here that are required for
184 * the unification of code that handles different versions of /proc.
187 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
189 enum { READ_WATCHFLAG
= WA_READ
,
190 WRITE_WATCHFLAG
= WA_WRITE
,
191 EXEC_WATCHFLAG
= WA_EXEC
,
192 AFTER_WATCHFLAG
= WA_TRAPAFTER
195 #else /* Irix method for watchpoints */
196 enum { READ_WATCHFLAG
= MA_READ
,
197 WRITE_WATCHFLAG
= MA_WRITE
,
198 EXEC_WATCHFLAG
= MA_EXEC
,
199 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
204 #ifdef HAVE_PR_SIGSET_T
205 typedef pr_sigset_t gdb_sigset_t
;
207 typedef sigset_t gdb_sigset_t
;
211 #ifdef HAVE_PR_SIGACTION64_T
212 typedef pr_sigaction64_t gdb_sigaction_t
;
214 typedef struct sigaction gdb_sigaction_t
;
218 #ifdef HAVE_PR_SIGINFO64_T
219 typedef pr_siginfo64_t gdb_siginfo_t
;
221 typedef struct siginfo gdb_siginfo_t
;
224 /* gdb_premptysysset */
226 #define gdb_premptysysset premptysysset
228 #define gdb_premptysysset premptyset
233 #define gdb_praddsysset praddsysset
235 #define gdb_praddsysset praddset
240 #define gdb_prdelsysset prdelsysset
242 #define gdb_prdelsysset prdelset
245 /* prissyssetmember */
246 #ifdef prissyssetmember
247 #define gdb_pr_issyssetmember prissyssetmember
249 #define gdb_pr_issyssetmember prismember
252 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
253 as intuitively descriptive as it could be, so we'll define
254 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
255 this writing, this feature is only found on AIX5 systems and
256 basically means that the set of syscalls is not fixed. I.e,
257 there's no nice table that one can #include to get all of the
258 syscall numbers. Instead, they're stored in /proc/PID/sysent
259 for each process. We are at least guaranteed that they won't
260 change over the lifetime of the process. But each process could
261 (in theory) have different syscall numbers.
263 #ifdef HAVE_PRSYSENT_T
264 #define DYNAMIC_SYSCALLS
269 /* =================== STRUCT PROCINFO "MODULE" =================== */
271 /* FIXME: this comment will soon be out of date W.R.T. threads. */
273 /* The procinfo struct is a wrapper to hold all the state information
274 concerning a /proc process. There should be exactly one procinfo
275 for each process, and since GDB currently can debug only one
276 process at a time, that means there should be only one procinfo.
277 All of the LWP's of a process can be accessed indirectly thru the
278 single process procinfo.
280 However, against the day when GDB may debug more than one process,
281 this data structure is kept in a list (which for now will hold no
282 more than one member), and many functions will have a pointer to a
283 procinfo as an argument.
285 There will be a separate procinfo structure for use by the (not yet
286 implemented) "info proc" command, so that we can print useful
287 information about any random process without interfering with the
288 inferior's procinfo information. */
291 /* format strings for /proc paths */
292 # ifndef CTL_PROC_NAME_FMT
293 # define MAIN_PROC_NAME_FMT "/proc/%d"
294 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
295 # define AS_PROC_NAME_FMT "/proc/%d/as"
296 # define MAP_PROC_NAME_FMT "/proc/%d/map"
297 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
298 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
300 /* the name of the proc status struct depends on the implementation */
301 typedef pstatus_t gdb_prstatus_t
;
302 typedef lwpstatus_t gdb_lwpstatus_t
;
303 #else /* ! NEW_PROC_API */
304 /* format strings for /proc paths */
305 # ifndef CTL_PROC_NAME_FMT
306 # define MAIN_PROC_NAME_FMT "/proc/%05d"
307 # define CTL_PROC_NAME_FMT "/proc/%05d"
308 # define AS_PROC_NAME_FMT "/proc/%05d"
309 # define MAP_PROC_NAME_FMT "/proc/%05d"
310 # define STATUS_PROC_NAME_FMT "/proc/%05d"
311 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
313 /* the name of the proc status struct depends on the implementation */
314 typedef prstatus_t gdb_prstatus_t
;
315 typedef prstatus_t gdb_lwpstatus_t
;
316 #endif /* NEW_PROC_API */
318 typedef struct procinfo
{
319 struct procinfo
*next
;
320 int pid
; /* Process ID */
321 int tid
; /* Thread/LWP id */
325 int ignore_next_sigstop
;
327 /* The following four fd fields may be identical, or may contain
328 several different fd's, depending on the version of /proc
329 (old ioctl or new read/write). */
331 int ctl_fd
; /* File descriptor for /proc control file */
333 * The next three file descriptors are actually only needed in the
334 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
335 * However, to avoid a bunch of #ifdefs in the code, we will use
336 * them uniformly by (in the case of the ioctl single-file-descriptor
337 * implementation) filling them with copies of the control fd.
339 int status_fd
; /* File descriptor for /proc status file */
340 int as_fd
; /* File descriptor for /proc as file */
342 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
344 fltset_t saved_fltset
; /* Saved traced hardware fault set */
345 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
346 gdb_sigset_t saved_sighold
; /* Saved held signal set */
347 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
348 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
350 gdb_prstatus_t prstatus
; /* Current process status info */
353 gdb_fpregset_t fpregset
; /* Current floating point registers */
356 #ifdef DYNAMIC_SYSCALLS
357 int num_syscalls
; /* Total number of syscalls */
358 char **syscall_names
; /* Syscall number to name map */
361 struct procinfo
*thread_list
;
363 int status_valid
: 1;
365 int fpregs_valid
: 1;
366 int threads_valid
: 1;
369 static char errmsg
[128]; /* shared error msg buffer */
371 /* Function prototypes for procinfo module: */
373 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
374 static procinfo
*find_procinfo (int pid
, int tid
);
375 static procinfo
*create_procinfo (int pid
, int tid
);
376 static void destroy_procinfo (procinfo
* p
);
377 static void do_destroy_procinfo_cleanup (void *);
378 static void dead_procinfo (procinfo
* p
, char *msg
, int killp
);
379 static int open_procinfo_files (procinfo
* p
, int which
);
380 static void close_procinfo_files (procinfo
* p
);
381 static int sysset_t_size (procinfo
*p
);
382 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
383 #ifdef DYNAMIC_SYSCALLS
384 static void load_syscalls (procinfo
*pi
);
385 static void free_syscalls (procinfo
*pi
);
386 static int find_syscall (procinfo
*pi
, char *name
);
387 #endif /* DYNAMIC_SYSCALLS */
389 /* The head of the procinfo list: */
390 static procinfo
* procinfo_list
;
393 * Function: find_procinfo
395 * Search the procinfo list.
397 * Returns: pointer to procinfo, or NULL if not found.
401 find_procinfo (int pid
, int tid
)
405 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
412 /* Don't check threads_valid. If we're updating the
413 thread_list, we want to find whatever threads are already
414 here. This means that in general it is the caller's
415 responsibility to check threads_valid and update before
416 calling find_procinfo, if the caller wants to find a new
419 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
428 * Function: find_procinfo_or_die
430 * Calls find_procinfo, but errors on failure.
434 find_procinfo_or_die (int pid
, int tid
)
436 procinfo
*pi
= find_procinfo (pid
, tid
);
441 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
444 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
449 /* open_with_retry() is a wrapper for open(). The appropriate
450 open() call is attempted; if unsuccessful, it will be retried as
451 many times as needed for the EAGAIN and EINTR conditions.
453 For other conditions, open_with_retry() will retry the open() a
454 limited number of times. In addition, a short sleep is imposed
455 prior to retrying the open(). The reason for this sleep is to give
456 the kernel a chance to catch up and create the file in question in
457 the event that GDB "wins" the race to open a file before the kernel
461 open_with_retry (const char *pathname
, int flags
)
463 int retries_remaining
, status
;
465 retries_remaining
= 2;
469 status
= open (pathname
, flags
);
471 if (status
>= 0 || retries_remaining
== 0)
473 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
484 * Function: open_procinfo_files
486 * Open the file descriptor for the process or LWP.
487 * ifdef NEW_PROC_API, we only open the control file descriptor;
488 * the others are opened lazily as needed.
489 * else (if not NEW_PROC_API), there is only one real
490 * file descriptor, but we keep multiple copies of it so that
491 * the code that uses them does not have to be #ifdef'd.
493 * Return: file descriptor, or zero for failure.
496 enum { FD_CTL
, FD_STATUS
, FD_AS
};
499 open_procinfo_files (procinfo
*pi
, int which
)
502 char tmp
[MAX_PROC_NAME_SIZE
];
507 * This function is getting ALMOST long enough to break up into several.
508 * Here is some rationale:
510 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
511 * There are several file descriptors that may need to be open
512 * for any given process or LWP. The ones we're intereted in are:
513 * - control (ctl) write-only change the state
514 * - status (status) read-only query the state
515 * - address space (as) read/write access memory
516 * - map (map) read-only virtual addr map
517 * Most of these are opened lazily as they are needed.
518 * The pathnames for the 'files' for an LWP look slightly
519 * different from those of a first-class process:
520 * Pathnames for a process (<proc-id>):
521 * /proc/<proc-id>/ctl
522 * /proc/<proc-id>/status
524 * /proc/<proc-id>/map
525 * Pathnames for an LWP (lwp-id):
526 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
527 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
528 * An LWP has no map or address space file descriptor, since
529 * the memory map and address space are shared by all LWPs.
531 * Everyone else (Solaris 2.5, Irix, OSF)
532 * There is only one file descriptor for each process or LWP.
533 * For convenience, we copy the same file descriptor into all
534 * three fields of the procinfo struct (ctl_fd, status_fd, and
535 * as_fd, see NEW_PROC_API above) so that code that uses them
536 * doesn't need any #ifdef's.
541 * Each LWP has an independent file descriptor, but these
542 * are not obtained via the 'open' system call like the rest:
543 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
544 * to the file descriptor of the parent process.
547 * These do not even have their own independent file descriptor.
548 * All operations are carried out on the file descriptor of the
549 * parent process. Therefore we just call open again for each
550 * thread, getting a new handle for the same 'file'.
555 * In this case, there are several different file descriptors that
556 * we might be asked to open. The control file descriptor will be
557 * opened early, but the others will be opened lazily as they are
561 strcpy (tmp
, pi
->pathname
);
562 switch (which
) { /* which file descriptor to open? */
565 strcat (tmp
, "/lwpctl");
567 strcat (tmp
, "/ctl");
568 fd
= open_with_retry (tmp
, O_WRONLY
);
575 return 0; /* there is no 'as' file descriptor for an lwp */
577 fd
= open_with_retry (tmp
, O_RDWR
);
584 strcat (tmp
, "/lwpstatus");
586 strcat (tmp
, "/status");
587 fd
= open_with_retry (tmp
, O_RDONLY
);
593 return 0; /* unknown file descriptor */
595 #else /* not NEW_PROC_API */
597 * In this case, there is only one file descriptor for each procinfo
598 * (ie. each process or LWP). In fact, only the file descriptor for
599 * the process can actually be opened by an 'open' system call.
600 * The ones for the LWPs have to be obtained thru an IOCTL call
601 * on the process's file descriptor.
603 * For convenience, we copy each procinfo's single file descriptor
604 * into all of the fields occupied by the several file descriptors
605 * of the NEW_PROC_API implementation. That way, the code that uses
606 * them can be written without ifdefs.
610 #ifdef PIOCTSTATUS /* OSF */
611 /* Only one FD; just open it. */
612 if ((fd
= open_with_retry (pi
->pathname
, O_RDWR
)) == 0)
614 #else /* Sol 2.5, Irix, other? */
615 if (pi
->tid
== 0) /* Master procinfo for the process */
617 fd
= open_with_retry (pi
->pathname
, O_RDWR
);
621 else /* LWP thread procinfo */
623 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
627 /* Find the procinfo for the entire process. */
628 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
631 /* Now obtain the file descriptor for the LWP. */
632 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
634 #else /* Irix, other? */
635 return 0; /* Don't know how to open threads */
636 #endif /* Sol 2.5 PIOCOPENLWP */
638 #endif /* OSF PIOCTSTATUS */
639 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
640 #endif /* NEW_PROC_API */
642 return 1; /* success */
646 * Function: create_procinfo
648 * Allocate a data structure and link it into the procinfo list.
649 * (First tries to find a pre-existing one (FIXME: why?)
651 * Return: pointer to new procinfo struct.
655 create_procinfo (int pid
, int tid
)
657 procinfo
*pi
, *parent
;
659 if ((pi
= find_procinfo (pid
, tid
)))
660 return pi
; /* Already exists, nothing to do. */
662 /* find parent before doing malloc, to save having to cleanup */
664 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
666 doesn't exist yet? */
668 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
669 memset (pi
, 0, sizeof (procinfo
));
673 #ifdef DYNAMIC_SYSCALLS
677 /* Chain into list. */
680 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
681 pi
->next
= procinfo_list
;
687 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
689 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
691 pi
->next
= parent
->thread_list
;
692 parent
->thread_list
= pi
;
698 * Function: close_procinfo_files
700 * Close all file descriptors associated with the procinfo
704 close_procinfo_files (procinfo
*pi
)
711 if (pi
->status_fd
> 0)
712 close (pi
->status_fd
);
714 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
718 * Function: destroy_procinfo
720 * Destructor function. Close, unlink and deallocate the object.
724 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
728 /* Step one: unlink the procinfo from its list */
732 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
735 ptr
->next
= pi
->next
;
739 /* Step two: close any open file descriptors */
740 close_procinfo_files (pi
);
742 /* Step three: free the memory. */
743 #ifdef DYNAMIC_SYSCALLS
750 destroy_procinfo (procinfo
*pi
)
754 if (pi
->tid
!= 0) /* destroy a thread procinfo */
756 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
757 destroy_one_procinfo (&tmp
->thread_list
, pi
);
759 else /* destroy a process procinfo and all its threads */
761 /* First destroy the children, if any; */
762 while (pi
->thread_list
!= NULL
)
763 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
764 /* Then destroy the parent. Genocide!!! */
765 destroy_one_procinfo (&procinfo_list
, pi
);
770 do_destroy_procinfo_cleanup (void *pi
)
772 destroy_procinfo (pi
);
775 enum { NOKILL
, KILL
};
778 * Function: dead_procinfo
780 * To be called on a non_recoverable error for a procinfo.
781 * Prints error messages, optionally sends a SIGKILL to the process,
782 * then destroys the data structure.
786 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
792 print_sys_errmsg (pi
->pathname
, errno
);
796 sprintf (procfile
, "process %d", pi
->pid
);
797 print_sys_errmsg (procfile
, errno
);
800 kill (pi
->pid
, SIGKILL
);
802 destroy_procinfo (pi
);
807 * Function: sysset_t_size
809 * Returns the (complete) size of a sysset_t struct. Normally, this
810 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
811 * size of sysset_t isn't known until runtime.
815 sysset_t_size (procinfo
* pi
)
817 #ifndef DYNAMIC_SYSCALLS
818 return sizeof (sysset_t
);
820 return sizeof (sysset_t
) - sizeof (uint64_t)
821 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
822 / (8 * sizeof (uint64_t)));
826 /* Function: sysset_t_alloc
828 Allocate and (partially) initialize a sysset_t struct. */
831 sysset_t_alloc (procinfo
* pi
)
834 int size
= sysset_t_size (pi
);
835 ret
= xmalloc (size
);
836 #ifdef DYNAMIC_SYSCALLS
837 ret
->pr_size
= (pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
838 / (8 * sizeof (uint64_t));
843 #ifdef DYNAMIC_SYSCALLS
845 /* Function: load_syscalls
847 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
848 pi->num_syscalls with the number of syscalls and pi->syscall_names
849 with the names. (Certain numbers may be skipped in which case the
850 names for these numbers will be left as NULL.) */
852 #define MAX_SYSCALL_NAME_LENGTH 256
853 #define MAX_SYSCALLS 65536
856 load_syscalls (procinfo
*pi
)
858 char pathname
[MAX_PROC_NAME_SIZE
];
861 prsyscall_t
*syscalls
;
862 int i
, size
, maxcall
;
864 pi
->num_syscalls
= 0;
865 pi
->syscall_names
= 0;
867 /* Open the file descriptor for the sysent file */
868 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
869 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
872 error ("load_syscalls: Can't open /proc/%d/sysent", pi
->pid
);
875 size
= sizeof header
- sizeof (prsyscall_t
);
876 if (read (sysent_fd
, &header
, size
) != size
)
878 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
881 if (header
.pr_nsyscalls
== 0)
883 error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi
->pid
);
886 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
887 syscalls
= xmalloc (size
);
889 if (read (sysent_fd
, syscalls
, size
) != size
)
892 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
895 /* Find maximum syscall number. This may not be the same as
896 pr_nsyscalls since that value refers to the number of entries
897 in the table. (Also, the docs indicate that some system
898 call numbers may be skipped.) */
900 maxcall
= syscalls
[0].pr_number
;
902 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
903 if (syscalls
[i
].pr_number
> maxcall
904 && syscalls
[i
].pr_nameoff
> 0
905 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
906 maxcall
= syscalls
[i
].pr_number
;
908 pi
->num_syscalls
= maxcall
+1;
909 pi
->syscall_names
= xmalloc (pi
->num_syscalls
* sizeof (char *));
911 for (i
= 0; i
< pi
->num_syscalls
; i
++)
912 pi
->syscall_names
[i
] = NULL
;
914 /* Read the syscall names in */
915 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
917 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
921 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
922 || syscalls
[i
].pr_number
< 0
923 || syscalls
[i
].pr_nameoff
<= 0
924 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
925 != (off_t
) syscalls
[i
].pr_nameoff
))
928 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
932 callnum
= syscalls
[i
].pr_number
;
934 if (pi
->syscall_names
[callnum
] != NULL
)
936 /* FIXME: Generate warning */
940 namebuf
[nread
-1] = '\0';
941 size
= strlen (namebuf
) + 1;
942 pi
->syscall_names
[callnum
] = xmalloc (size
);
943 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
944 pi
->syscall_names
[callnum
][size
-1] = '\0';
951 /* Function: free_syscalls
953 Free the space allocated for the syscall names from the procinfo
957 free_syscalls (procinfo
*pi
)
959 if (pi
->syscall_names
)
963 for (i
= 0; i
< pi
->num_syscalls
; i
++)
964 if (pi
->syscall_names
[i
] != NULL
)
965 xfree (pi
->syscall_names
[i
]);
967 xfree (pi
->syscall_names
);
968 pi
->syscall_names
= 0;
972 /* Function: find_syscall
974 Given a name, look up (and return) the corresponding syscall number.
975 If no match is found, return -1. */
978 find_syscall (procinfo
*pi
, char *name
)
981 for (i
= 0; i
< pi
->num_syscalls
; i
++)
983 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
990 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
992 /* =================== /proc "MODULE" =================== */
995 * This "module" is the interface layer between the /proc system API
996 * and the gdb target vector functions. This layer consists of
997 * access functions that encapsulate each of the basic operations
998 * that we need to use from the /proc API.
1000 * The main motivation for this layer is to hide the fact that
1001 * there are two very different implementations of the /proc API.
1002 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1003 * functions, we do our best to hide them all in here.
1006 int proc_get_status (procinfo
* pi
);
1007 long proc_flags (procinfo
* pi
);
1008 int proc_why (procinfo
* pi
);
1009 int proc_what (procinfo
* pi
);
1010 int proc_set_run_on_last_close (procinfo
* pi
);
1011 int proc_unset_run_on_last_close (procinfo
* pi
);
1012 int proc_set_inherit_on_fork (procinfo
* pi
);
1013 int proc_unset_inherit_on_fork (procinfo
* pi
);
1014 int proc_set_async (procinfo
* pi
);
1015 int proc_unset_async (procinfo
* pi
);
1016 int proc_stop_process (procinfo
* pi
);
1017 int proc_trace_signal (procinfo
* pi
, int signo
);
1018 int proc_ignore_signal (procinfo
* pi
, int signo
);
1019 int proc_clear_current_fault (procinfo
* pi
);
1020 int proc_set_current_signal (procinfo
* pi
, int signo
);
1021 int proc_clear_current_signal (procinfo
* pi
);
1022 int proc_set_gregs (procinfo
* pi
);
1023 int proc_set_fpregs (procinfo
* pi
);
1024 int proc_wait_for_stop (procinfo
* pi
);
1025 int proc_run_process (procinfo
* pi
, int step
, int signo
);
1026 int proc_kill (procinfo
* pi
, int signo
);
1027 int proc_parent_pid (procinfo
* pi
);
1028 int proc_get_nthreads (procinfo
* pi
);
1029 int proc_get_current_thread (procinfo
* pi
);
1030 int proc_set_held_signals (procinfo
* pi
, gdb_sigset_t
* sighold
);
1031 int proc_set_traced_sysexit (procinfo
* pi
, sysset_t
* sysset
);
1032 int proc_set_traced_sysentry (procinfo
* pi
, sysset_t
* sysset
);
1033 int proc_set_traced_faults (procinfo
* pi
, fltset_t
* fltset
);
1034 int proc_set_traced_signals (procinfo
* pi
, gdb_sigset_t
* sigset
);
1036 int proc_update_threads (procinfo
* pi
);
1037 int proc_iterate_over_threads (procinfo
* pi
,
1038 int (*func
) (procinfo
*, procinfo
*, void *),
1041 gdb_gregset_t
*proc_get_gregs (procinfo
* pi
);
1042 gdb_fpregset_t
*proc_get_fpregs (procinfo
* pi
);
1043 sysset_t
*proc_get_traced_sysexit (procinfo
* pi
, sysset_t
* save
);
1044 sysset_t
*proc_get_traced_sysentry (procinfo
* pi
, sysset_t
* save
);
1045 fltset_t
*proc_get_traced_faults (procinfo
* pi
, fltset_t
* save
);
1046 gdb_sigset_t
*proc_get_traced_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1047 gdb_sigset_t
*proc_get_held_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1048 gdb_sigset_t
*proc_get_pending_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1049 gdb_sigaction_t
*proc_get_signal_actions (procinfo
* pi
, gdb_sigaction_t
*save
);
1051 void proc_warn (procinfo
* pi
, char *func
, int line
);
1052 void proc_error (procinfo
* pi
, char *func
, int line
);
1055 proc_warn (procinfo
*pi
, char *func
, int line
)
1057 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1058 print_sys_errmsg (errmsg
, errno
);
1062 proc_error (procinfo
*pi
, char *func
, int line
)
1064 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1065 perror_with_name (errmsg
);
1069 * Function: proc_get_status
1071 * Updates the status struct in the procinfo.
1072 * There is a 'valid' flag, to let other functions know when
1073 * this function needs to be called (so the status is only
1074 * read when it is needed). The status file descriptor is
1075 * also only opened when it is needed.
1077 * Return: non-zero for success, zero for failure.
1081 proc_get_status (procinfo
*pi
)
1083 /* Status file descriptor is opened "lazily" */
1084 if (pi
->status_fd
== 0 &&
1085 open_procinfo_files (pi
, FD_STATUS
) == 0)
1087 pi
->status_valid
= 0;
1092 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1093 pi
->status_valid
= 0; /* fail */
1096 /* Sigh... I have to read a different data structure,
1097 depending on whether this is a main process or an LWP. */
1099 pi
->status_valid
= (read (pi
->status_fd
,
1100 (char *) &pi
->prstatus
.pr_lwp
,
1101 sizeof (lwpstatus_t
))
1102 == sizeof (lwpstatus_t
));
1105 pi
->status_valid
= (read (pi
->status_fd
,
1106 (char *) &pi
->prstatus
,
1107 sizeof (gdb_prstatus_t
))
1108 == sizeof (gdb_prstatus_t
));
1109 #if 0 /*def UNIXWARE*/
1110 if (pi
->status_valid
&&
1111 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
1112 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
1113 /* Unixware peculiarity -- read the damn thing again! */
1114 pi
->status_valid
= (read (pi
->status_fd
,
1115 (char *) &pi
->prstatus
,
1116 sizeof (gdb_prstatus_t
))
1117 == sizeof (gdb_prstatus_t
));
1118 #endif /* UNIXWARE */
1121 #else /* ioctl method */
1122 #ifdef PIOCTSTATUS /* osf */
1123 if (pi
->tid
== 0) /* main process */
1125 /* Just read the danged status. Now isn't that simple? */
1127 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1134 tid_t pr_error_thread
;
1135 struct prstatus status
;
1138 thread_status
.pr_count
= 1;
1139 thread_status
.status
.pr_tid
= pi
->tid
;
1140 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1143 memcpy (&pi
->prstatus
, &thread_status
.status
,
1144 sizeof (pi
->prstatus
));
1145 pi
->status_valid
= 1;
1149 /* Just read the danged status. Now isn't that simple? */
1150 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1154 if (pi
->status_valid
)
1156 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1159 proc_get_current_thread (pi
));
1162 /* The status struct includes general regs, so mark them valid too */
1163 pi
->gregs_valid
= pi
->status_valid
;
1165 /* In the read/write multiple-fd model,
1166 the status struct includes the fp regs too, so mark them valid too */
1167 pi
->fpregs_valid
= pi
->status_valid
;
1169 return pi
->status_valid
; /* True if success, false if failure. */
1173 * Function: proc_flags
1175 * returns the process flags (pr_flags field).
1179 proc_flags (procinfo
*pi
)
1181 if (!pi
->status_valid
)
1182 if (!proc_get_status (pi
))
1183 return 0; /* FIXME: not a good failure value (but what is?) */
1187 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1188 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1189 The two sets of flags don't overlap. */
1190 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
1192 return pi
->prstatus
.pr_lwp
.pr_flags
;
1195 return pi
->prstatus
.pr_flags
;
1200 * Function: proc_why
1202 * returns the pr_why field (why the process stopped).
1206 proc_why (procinfo
*pi
)
1208 if (!pi
->status_valid
)
1209 if (!proc_get_status (pi
))
1210 return 0; /* FIXME: not a good failure value (but what is?) */
1213 return pi
->prstatus
.pr_lwp
.pr_why
;
1215 return pi
->prstatus
.pr_why
;
1220 * Function: proc_what
1222 * returns the pr_what field (details of why the process stopped).
1226 proc_what (procinfo
*pi
)
1228 if (!pi
->status_valid
)
1229 if (!proc_get_status (pi
))
1230 return 0; /* FIXME: not a good failure value (but what is?) */
1233 return pi
->prstatus
.pr_lwp
.pr_what
;
1235 return pi
->prstatus
.pr_what
;
1239 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1241 * Function: proc_nsysarg
1243 * returns the pr_nsysarg field (number of args to the current syscall).
1247 proc_nsysarg (procinfo
*pi
)
1249 if (!pi
->status_valid
)
1250 if (!proc_get_status (pi
))
1254 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1256 return pi
->prstatus
.pr_nsysarg
;
1261 * Function: proc_sysargs
1263 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1267 proc_sysargs (procinfo
*pi
)
1269 if (!pi
->status_valid
)
1270 if (!proc_get_status (pi
))
1274 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1276 return (long *) &pi
->prstatus
.pr_sysarg
;
1281 * Function: proc_syscall
1283 * returns the pr_syscall field (id of current syscall if we are in one).
1287 proc_syscall (procinfo
*pi
)
1289 if (!pi
->status_valid
)
1290 if (!proc_get_status (pi
))
1294 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1296 return pi
->prstatus
.pr_syscall
;
1299 #endif /* PIOCSSPCACT */
1302 * Function: proc_cursig:
1304 * returns the pr_cursig field (current signal).
1308 proc_cursig (struct procinfo
*pi
)
1310 if (!pi
->status_valid
)
1311 if (!proc_get_status (pi
))
1312 return 0; /* FIXME: not a good failure value (but what is?) */
1315 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1317 return pi
->prstatus
.pr_cursig
;
1322 * Function: proc_modify_flag
1324 * === I appologize for the messiness of this function.
1325 * === This is an area where the different versions of
1326 * === /proc are more inconsistent than usual. MVS
1328 * Set or reset any of the following process flags:
1329 * PR_FORK -- forked child will inherit trace flags
1330 * PR_RLC -- traced process runs when last /proc file closed.
1331 * PR_KLC -- traced process is killed when last /proc file closed.
1332 * PR_ASYNC -- LWP's get to run/stop independently.
1334 * There are three methods for doing this function:
1335 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1337 * 2) Middle: PIOCSET/PIOCRESET
1339 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1342 * Note: Irix does not define PR_ASYNC.
1343 * Note: OSF does not define PR_KLC.
1344 * Note: OSF is the only one that can ONLY use the oldest method.
1347 * pi -- the procinfo
1348 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1349 * mode -- 1 for set, 0 for reset.
1351 * Returns non-zero for success, zero for failure.
1354 enum { FLAG_RESET
, FLAG_SET
};
1357 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1359 long win
= 0; /* default to fail */
1362 * These operations affect the process as a whole, and applying
1363 * them to an individual LWP has the same meaning as applying them
1364 * to the main process. Therefore, if we're ever called with a
1365 * pointer to an LWP's procinfo, let's substitute the process's
1366 * procinfo and avoid opening the LWP's file descriptor
1371 pi
= find_procinfo_or_die (pi
->pid
, 0);
1373 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1374 /* First normalize the PCUNSET/PCRESET command opcode
1375 (which for no obvious reason has a different definition
1376 from one operating system to the next...) */
1378 #define GDBRESET PCUNSET
1381 #define GDBRESET PCRESET
1385 procfs_ctl_t arg
[2];
1387 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1389 else /* Reset the flag */
1393 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1396 #ifdef PIOCSET /* Irix/Sol5 method */
1397 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1399 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1401 else /* Reset the flag */
1403 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1407 #ifdef PIOCSRLC /* Oldest method: OSF */
1410 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1412 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1414 else /* Clear run-on-last-close */
1416 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1420 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1422 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1424 else /* Clear inherit-on-fork */
1426 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1430 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1437 /* The above operation renders the procinfo's cached pstatus obsolete. */
1438 pi
->status_valid
= 0;
1441 warning ("procfs: modify_flag failed to turn %s %s",
1442 flag
== PR_FORK
? "PR_FORK" :
1443 flag
== PR_RLC
? "PR_RLC" :
1445 flag
== PR_ASYNC
? "PR_ASYNC" :
1448 flag
== PR_KLC
? "PR_KLC" :
1451 mode
== FLAG_RESET
? "off" : "on");
1457 * Function: proc_set_run_on_last_close
1459 * Set the run_on_last_close flag.
1460 * Process with all threads will become runnable
1461 * when debugger closes all /proc fds.
1463 * Returns non-zero for success, zero for failure.
1467 proc_set_run_on_last_close (procinfo
*pi
)
1469 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1473 * Function: proc_unset_run_on_last_close
1475 * Reset the run_on_last_close flag.
1476 * Process will NOT become runnable
1477 * when debugger closes its file handles.
1479 * Returns non-zero for success, zero for failure.
1483 proc_unset_run_on_last_close (procinfo
*pi
)
1485 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1490 * Function: proc_set_kill_on_last_close
1492 * Set the kill_on_last_close flag.
1493 * Process with all threads will be killed when debugger
1494 * closes all /proc fds (or debugger exits or dies).
1496 * Returns non-zero for success, zero for failure.
1500 proc_set_kill_on_last_close (procinfo
*pi
)
1502 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1506 * Function: proc_unset_kill_on_last_close
1508 * Reset the kill_on_last_close flag.
1509 * Process will NOT be killed when debugger
1510 * closes its file handles (or exits or dies).
1512 * Returns non-zero for success, zero for failure.
1516 proc_unset_kill_on_last_close (procinfo
*pi
)
1518 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1523 * Function: proc_set_inherit_on_fork
1525 * Set inherit_on_fork flag.
1526 * If the process forks a child while we are registered for events
1527 * in the parent, then we will also recieve events from the child.
1529 * Returns non-zero for success, zero for failure.
1533 proc_set_inherit_on_fork (procinfo
*pi
)
1535 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1539 * Function: proc_unset_inherit_on_fork
1541 * Reset inherit_on_fork flag.
1542 * If the process forks a child while we are registered for events
1543 * in the parent, then we will NOT recieve events from the child.
1545 * Returns non-zero for success, zero for failure.
1549 proc_unset_inherit_on_fork (procinfo
*pi
)
1551 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1556 * Function: proc_set_async
1558 * Set PR_ASYNC flag.
1559 * If one LWP stops because of a debug event (signal etc.),
1560 * the remaining LWPs will continue to run.
1562 * Returns non-zero for success, zero for failure.
1566 proc_set_async (procinfo
*pi
)
1568 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1572 * Function: proc_unset_async
1574 * Reset PR_ASYNC flag.
1575 * If one LWP stops because of a debug event (signal etc.),
1576 * then all other LWPs will stop as well.
1578 * Returns non-zero for success, zero for failure.
1582 proc_unset_async (procinfo
*pi
)
1584 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1586 #endif /* PR_ASYNC */
1589 * Function: proc_stop_process
1591 * Request the process/LWP to stop. Does not wait.
1592 * Returns non-zero for success, zero for failure.
1596 proc_stop_process (procinfo
*pi
)
1601 * We might conceivably apply this operation to an LWP, and
1602 * the LWP's ctl file descriptor might not be open.
1605 if (pi
->ctl_fd
== 0 &&
1606 open_procinfo_files (pi
, FD_CTL
) == 0)
1611 procfs_ctl_t cmd
= PCSTOP
;
1612 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1613 #else /* ioctl method */
1614 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1615 /* Note: the call also reads the prstatus. */
1618 pi
->status_valid
= 1;
1619 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1622 proc_get_current_thread (pi
));
1631 * Function: proc_wait_for_stop
1633 * Wait for the process or LWP to stop (block until it does).
1634 * Returns non-zero for success, zero for failure.
1638 proc_wait_for_stop (procinfo
*pi
)
1643 * We should never have to apply this operation to any procinfo
1644 * except the one for the main process. If that ever changes
1645 * for any reason, then take out the following clause and
1646 * replace it with one that makes sure the ctl_fd is open.
1650 pi
= find_procinfo_or_die (pi
->pid
, 0);
1654 procfs_ctl_t cmd
= PCWSTOP
;
1655 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1656 /* We been runnin' and we stopped -- need to update status. */
1657 pi
->status_valid
= 0;
1659 #else /* ioctl method */
1660 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1661 /* Above call also refreshes the prstatus. */
1664 pi
->status_valid
= 1;
1665 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1668 proc_get_current_thread (pi
));
1676 * Function: proc_run_process
1678 * Make the process or LWP runnable.
1679 * Options (not all are implemented):
1681 * - clear current fault
1682 * - clear current signal
1683 * - abort the current system call
1684 * - stop as soon as finished with system call
1685 * - (ioctl): set traced signal set
1686 * - (ioctl): set held signal set
1687 * - (ioctl): set traced fault set
1688 * - (ioctl): set start pc (vaddr)
1689 * Always clear the current fault.
1690 * Clear the current signal if 'signo' is zero.
1693 * pi the process or LWP to operate on.
1694 * step if true, set the process or LWP to trap after one instr.
1695 * signo if zero, clear the current signal if any.
1696 * if non-zero, set the current signal to this one.
1698 * Returns non-zero for success, zero for failure.
1702 proc_run_process (procinfo
*pi
, int step
, int signo
)
1708 * We will probably have to apply this operation to individual threads,
1709 * so make sure the control file descriptor is open.
1712 if (pi
->ctl_fd
== 0 &&
1713 open_procinfo_files (pi
, FD_CTL
) == 0)
1718 runflags
= PRCFAULT
; /* always clear current fault */
1723 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1724 proc_set_current_signal (pi
, signo
);
1728 procfs_ctl_t cmd
[2];
1732 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1734 #else /* ioctl method */
1738 memset (&prrun
, 0, sizeof (prrun
));
1739 prrun
.pr_flags
= runflags
;
1740 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1748 * Function: proc_set_traced_signals
1750 * Register to trace signals in the process or LWP.
1751 * Returns non-zero for success, zero for failure.
1755 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1760 * We should never have to apply this operation to any procinfo
1761 * except the one for the main process. If that ever changes
1762 * for any reason, then take out the following clause and
1763 * replace it with one that makes sure the ctl_fd is open.
1767 pi
= find_procinfo_or_die (pi
->pid
, 0);
1773 /* Use char array to avoid alignment issues. */
1774 char sigset
[sizeof (gdb_sigset_t
)];
1778 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1780 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1782 #else /* ioctl method */
1783 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1785 /* The above operation renders the procinfo's cached pstatus obsolete. */
1786 pi
->status_valid
= 0;
1789 warning ("procfs: set_traced_signals failed");
1794 * Function: proc_set_traced_faults
1796 * Register to trace hardware faults in the process or LWP.
1797 * Returns non-zero for success, zero for failure.
1801 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1806 * We should never have to apply this operation to any procinfo
1807 * except the one for the main process. If that ever changes
1808 * for any reason, then take out the following clause and
1809 * replace it with one that makes sure the ctl_fd is open.
1813 pi
= find_procinfo_or_die (pi
->pid
, 0);
1819 /* Use char array to avoid alignment issues. */
1820 char fltset
[sizeof (fltset_t
)];
1824 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1826 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1828 #else /* ioctl method */
1829 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1831 /* The above operation renders the procinfo's cached pstatus obsolete. */
1832 pi
->status_valid
= 0;
1838 * Function: proc_set_traced_sysentry
1840 * Register to trace entry to system calls in the process or LWP.
1841 * Returns non-zero for success, zero for failure.
1845 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1850 * We should never have to apply this operation to any procinfo
1851 * except the one for the main process. If that ever changes
1852 * for any reason, then take out the following clause and
1853 * replace it with one that makes sure the ctl_fd is open.
1857 pi
= find_procinfo_or_die (pi
->pid
, 0);
1861 struct gdb_proc_ctl_pcsentry
{
1863 /* Use char array to avoid alignment issues. */
1864 char sysset
[sizeof (sysset_t
)];
1866 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1868 + sysset_t_size (pi
);
1870 argp
= xmalloc (argp_size
);
1872 argp
->cmd
= PCSENTRY
;
1873 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1875 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1878 #else /* ioctl method */
1879 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1881 /* The above operation renders the procinfo's cached pstatus obsolete. */
1882 pi
->status_valid
= 0;
1888 * Function: proc_set_traced_sysexit
1890 * Register to trace exit from system calls in the process or LWP.
1891 * Returns non-zero for success, zero for failure.
1895 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1900 * We should never have to apply this operation to any procinfo
1901 * except the one for the main process. If that ever changes
1902 * for any reason, then take out the following clause and
1903 * replace it with one that makes sure the ctl_fd is open.
1907 pi
= find_procinfo_or_die (pi
->pid
, 0);
1911 struct gdb_proc_ctl_pcsexit
{
1913 /* Use char array to avoid alignment issues. */
1914 char sysset
[sizeof (sysset_t
)];
1916 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1918 + sysset_t_size (pi
);
1920 argp
= xmalloc (argp_size
);
1922 argp
->cmd
= PCSEXIT
;
1923 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1925 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1928 #else /* ioctl method */
1929 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1931 /* The above operation renders the procinfo's cached pstatus obsolete. */
1932 pi
->status_valid
= 0;
1938 * Function: proc_set_held_signals
1940 * Specify the set of blocked / held signals in the process or LWP.
1941 * Returns non-zero for success, zero for failure.
1945 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1950 * We should never have to apply this operation to any procinfo
1951 * except the one for the main process. If that ever changes
1952 * for any reason, then take out the following clause and
1953 * replace it with one that makes sure the ctl_fd is open.
1957 pi
= find_procinfo_or_die (pi
->pid
, 0);
1963 /* Use char array to avoid alignment issues. */
1964 char hold
[sizeof (gdb_sigset_t
)];
1968 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1969 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1972 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1974 /* The above operation renders the procinfo's cached pstatus obsolete. */
1975 pi
->status_valid
= 0;
1981 * Function: proc_get_pending_signals
1983 * returns the set of signals that are pending in the process or LWP.
1984 * Will also copy the sigset if 'save' is non-zero.
1988 proc_get_pending_signals (procinfo
*pi
, gdb_sigset_t
*save
)
1990 gdb_sigset_t
*ret
= NULL
;
1993 * We should never have to apply this operation to any procinfo
1994 * except the one for the main process. If that ever changes
1995 * for any reason, then take out the following clause and
1996 * replace it with one that makes sure the ctl_fd is open.
2000 pi
= find_procinfo_or_die (pi
->pid
, 0);
2002 if (!pi
->status_valid
)
2003 if (!proc_get_status (pi
))
2007 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
2009 ret
= &pi
->prstatus
.pr_sigpend
;
2012 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2018 * Function: proc_get_signal_actions
2020 * returns the set of signal actions.
2021 * Will also copy the sigactionset if 'save' is non-zero.
2025 proc_get_signal_actions (procinfo
*pi
, gdb_sigaction_t
*save
)
2027 gdb_sigaction_t
*ret
= NULL
;
2030 * We should never have to apply this operation to any procinfo
2031 * except the one for the main process. If that ever changes
2032 * for any reason, then take out the following clause and
2033 * replace it with one that makes sure the ctl_fd is open.
2037 pi
= find_procinfo_or_die (pi
->pid
, 0);
2039 if (!pi
->status_valid
)
2040 if (!proc_get_status (pi
))
2044 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
2046 ret
= &pi
->prstatus
.pr_action
;
2049 memcpy (save
, ret
, sizeof (gdb_sigaction_t
));
2055 * Function: proc_get_held_signals
2057 * returns the set of signals that are held / blocked.
2058 * Will also copy the sigset if 'save' is non-zero.
2062 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2064 gdb_sigset_t
*ret
= NULL
;
2067 * We should never have to apply this operation to any procinfo
2068 * except the one for the main process. If that ever changes
2069 * for any reason, then take out the following clause and
2070 * replace it with one that makes sure the ctl_fd is open.
2074 pi
= find_procinfo_or_die (pi
->pid
, 0);
2077 if (!pi
->status_valid
)
2078 if (!proc_get_status (pi
))
2082 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
2084 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
2085 #endif /* UNIXWARE */
2086 #else /* not NEW_PROC_API */
2088 static gdb_sigset_t sigheld
;
2090 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
2093 #endif /* NEW_PROC_API */
2095 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2101 * Function: proc_get_traced_signals
2103 * returns the set of signals that are traced / debugged.
2104 * Will also copy the sigset if 'save' is non-zero.
2108 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2110 gdb_sigset_t
*ret
= NULL
;
2113 * We should never have to apply this operation to any procinfo
2114 * except the one for the main process. If that ever changes
2115 * for any reason, then take out the following clause and
2116 * replace it with one that makes sure the ctl_fd is open.
2120 pi
= find_procinfo_or_die (pi
->pid
, 0);
2123 if (!pi
->status_valid
)
2124 if (!proc_get_status (pi
))
2127 ret
= &pi
->prstatus
.pr_sigtrace
;
2130 static gdb_sigset_t sigtrace
;
2132 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
2137 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2143 * Function: proc_trace_signal
2145 * Add 'signo' to the set of signals that are traced.
2146 * Returns non-zero for success, zero for failure.
2150 proc_trace_signal (procinfo
*pi
, int signo
)
2155 * We should never have to apply this operation to any procinfo
2156 * except the one for the main process. If that ever changes
2157 * for any reason, then take out the following clause and
2158 * replace it with one that makes sure the ctl_fd is open.
2162 pi
= find_procinfo_or_die (pi
->pid
, 0);
2166 if (proc_get_traced_signals (pi
, &temp
))
2168 praddset (&temp
, signo
);
2169 return proc_set_traced_signals (pi
, &temp
);
2173 return 0; /* failure */
2177 * Function: proc_ignore_signal
2179 * Remove 'signo' from the set of signals that are traced.
2180 * Returns non-zero for success, zero for failure.
2184 proc_ignore_signal (procinfo
*pi
, int signo
)
2189 * We should never have to apply this operation to any procinfo
2190 * except the one for the main process. If that ever changes
2191 * for any reason, then take out the following clause and
2192 * replace it with one that makes sure the ctl_fd is open.
2196 pi
= find_procinfo_or_die (pi
->pid
, 0);
2200 if (proc_get_traced_signals (pi
, &temp
))
2202 prdelset (&temp
, signo
);
2203 return proc_set_traced_signals (pi
, &temp
);
2207 return 0; /* failure */
2211 * Function: proc_get_traced_faults
2213 * returns the set of hardware faults that are traced /debugged.
2214 * Will also copy the faultset if 'save' is non-zero.
2218 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
2220 fltset_t
*ret
= NULL
;
2223 * We should never have to apply this operation to any procinfo
2224 * except the one for the main process. If that ever changes
2225 * for any reason, then take out the following clause and
2226 * replace it with one that makes sure the ctl_fd is open.
2230 pi
= find_procinfo_or_die (pi
->pid
, 0);
2233 if (!pi
->status_valid
)
2234 if (!proc_get_status (pi
))
2237 ret
= &pi
->prstatus
.pr_flttrace
;
2240 static fltset_t flttrace
;
2242 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2247 memcpy (save
, ret
, sizeof (fltset_t
));
2253 * Function: proc_get_traced_sysentry
2255 * returns the set of syscalls that are traced /debugged on entry.
2256 * Will also copy the syscall set if 'save' is non-zero.
2260 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
2262 sysset_t
*ret
= NULL
;
2265 * We should never have to apply this operation to any procinfo
2266 * except the one for the main process. If that ever changes
2267 * for any reason, then take out the following clause and
2268 * replace it with one that makes sure the ctl_fd is open.
2272 pi
= find_procinfo_or_die (pi
->pid
, 0);
2275 if (!pi
->status_valid
)
2276 if (!proc_get_status (pi
))
2279 #ifndef DYNAMIC_SYSCALLS
2280 ret
= &pi
->prstatus
.pr_sysentry
;
2281 #else /* DYNAMIC_SYSCALLS */
2283 static sysset_t
*sysentry
;
2287 sysentry
= sysset_t_alloc (pi
);
2289 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2291 if (pi
->prstatus
.pr_sysentry_offset
== 0)
2293 gdb_premptysysset (sysentry
);
2299 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
2301 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
2303 size
= sysset_t_size (pi
);
2304 gdb_premptysysset (sysentry
);
2305 rsize
= read (pi
->status_fd
, sysentry
, size
);
2310 #endif /* DYNAMIC_SYSCALLS */
2311 #else /* !NEW_PROC_API */
2313 static sysset_t sysentry
;
2315 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2318 #endif /* NEW_PROC_API */
2320 memcpy (save
, ret
, sysset_t_size (pi
));
2326 * Function: proc_get_traced_sysexit
2328 * returns the set of syscalls that are traced /debugged on exit.
2329 * Will also copy the syscall set if 'save' is non-zero.
2333 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
2335 sysset_t
* ret
= NULL
;
2338 * We should never have to apply this operation to any procinfo
2339 * except the one for the main process. If that ever changes
2340 * for any reason, then take out the following clause and
2341 * replace it with one that makes sure the ctl_fd is open.
2345 pi
= find_procinfo_or_die (pi
->pid
, 0);
2348 if (!pi
->status_valid
)
2349 if (!proc_get_status (pi
))
2352 #ifndef DYNAMIC_SYSCALLS
2353 ret
= &pi
->prstatus
.pr_sysexit
;
2354 #else /* DYNAMIC_SYSCALLS */
2356 static sysset_t
*sysexit
;
2360 sysexit
= sysset_t_alloc (pi
);
2362 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2364 if (pi
->prstatus
.pr_sysexit_offset
== 0)
2366 gdb_premptysysset (sysexit
);
2372 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
, SEEK_SET
)
2373 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2375 size
= sysset_t_size (pi
);
2376 gdb_premptysysset (sysexit
);
2377 rsize
= read (pi
->status_fd
, sysexit
, size
);
2382 #endif /* DYNAMIC_SYSCALLS */
2385 static sysset_t sysexit
;
2387 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2392 memcpy (save
, ret
, sysset_t_size (pi
));
2398 * Function: proc_clear_current_fault
2400 * The current fault (if any) is cleared; the associated signal
2401 * will not be sent to the process or LWP when it resumes.
2402 * Returns non-zero for success, zero for failure.
2406 proc_clear_current_fault (procinfo
*pi
)
2411 * We should never have to apply this operation to any procinfo
2412 * except the one for the main process. If that ever changes
2413 * for any reason, then take out the following clause and
2414 * replace it with one that makes sure the ctl_fd is open.
2418 pi
= find_procinfo_or_die (pi
->pid
, 0);
2422 procfs_ctl_t cmd
= PCCFAULT
;
2423 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2426 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2433 * Function: proc_set_current_signal
2435 * Set the "current signal" that will be delivered next to the process.
2436 * NOTE: semantics are different from those of KILL.
2437 * This signal will be delivered to the process or LWP
2438 * immediately when it is resumed (even if the signal is held/blocked);
2439 * it will NOT immediately cause another event of interest, and will NOT
2440 * first trap back to the debugger.
2442 * Returns non-zero for success, zero for failure.
2446 proc_set_current_signal (procinfo
*pi
, int signo
)
2451 /* Use char array to avoid alignment issues. */
2452 char sinfo
[sizeof (gdb_siginfo_t
)];
2454 gdb_siginfo_t
*mysinfo
;
2457 * We should never have to apply this operation to any procinfo
2458 * except the one for the main process. If that ever changes
2459 * for any reason, then take out the following clause and
2460 * replace it with one that makes sure the ctl_fd is open.
2464 pi
= find_procinfo_or_die (pi
->pid
, 0);
2466 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2467 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2468 * receives a PIOCSSIG with a signal identical to the current signal,
2469 * it messes up the current signal. Work around the kernel bug.
2472 signo
== proc_cursig (pi
))
2473 return 1; /* I assume this is a success? */
2476 /* The pointer is just a type alias. */
2477 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2478 mysinfo
->si_signo
= signo
;
2479 mysinfo
->si_code
= 0;
2480 mysinfo
->si_pid
= getpid (); /* ?why? */
2481 mysinfo
->si_uid
= getuid (); /* ?why? */
2485 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2487 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2494 * Function: proc_clear_current_signal
2496 * The current signal (if any) is cleared, and
2497 * is not sent to the process or LWP when it resumes.
2498 * Returns non-zero for success, zero for failure.
2502 proc_clear_current_signal (procinfo
*pi
)
2507 * We should never have to apply this operation to any procinfo
2508 * except the one for the main process. If that ever changes
2509 * for any reason, then take out the following clause and
2510 * replace it with one that makes sure the ctl_fd is open.
2514 pi
= find_procinfo_or_die (pi
->pid
, 0);
2520 /* Use char array to avoid alignment issues. */
2521 char sinfo
[sizeof (gdb_siginfo_t
)];
2523 gdb_siginfo_t
*mysinfo
;
2526 /* The pointer is just a type alias. */
2527 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2528 mysinfo
->si_signo
= 0;
2529 mysinfo
->si_code
= 0;
2530 mysinfo
->si_errno
= 0;
2531 mysinfo
->si_pid
= getpid (); /* ?why? */
2532 mysinfo
->si_uid
= getuid (); /* ?why? */
2534 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2537 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2544 * Function: proc_get_gregs
2546 * Get the general registers for the process or LWP.
2547 * Returns non-zero for success, zero for failure.
2551 proc_get_gregs (procinfo
*pi
)
2553 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2554 if (!proc_get_status (pi
))
2558 * OK, sorry about the ifdef's.
2559 * There's three cases instead of two, because
2560 * in this instance Unixware and Solaris/RW differ.
2564 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2565 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2566 #else /* not Unixware */
2567 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2568 #endif /* Unixware */
2569 #else /* not NEW_PROC_API */
2570 return &pi
->prstatus
.pr_reg
;
2571 #endif /* NEW_PROC_API */
2575 * Function: proc_get_fpregs
2577 * Get the floating point registers for the process or LWP.
2578 * Returns non-zero for success, zero for failure.
2582 proc_get_fpregs (procinfo
*pi
)
2585 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2586 if (!proc_get_status (pi
))
2589 #ifdef UNIXWARE /* a true architecture dependency */
2590 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2592 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2593 #endif /* Unixware */
2595 #else /* not NEW_PROC_API */
2596 if (pi
->fpregs_valid
)
2597 return &pi
->fpregset
; /* already got 'em */
2600 if (pi
->ctl_fd
== 0 &&
2601 open_procinfo_files (pi
, FD_CTL
) == 0)
2610 tid_t pr_error_thread
;
2611 tfpregset_t thread_1
;
2614 thread_fpregs
.pr_count
= 1;
2615 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2618 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2620 pi
->fpregs_valid
= 1;
2621 return &pi
->fpregset
; /* got 'em now! */
2623 else if (pi
->tid
!= 0 &&
2624 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2626 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2627 sizeof (pi
->fpregset
));
2628 pi
->fpregs_valid
= 1;
2629 return &pi
->fpregset
; /* got 'em now! */
2636 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2638 pi
->fpregs_valid
= 1;
2639 return &pi
->fpregset
; /* got 'em now! */
2652 * Function: proc_set_gregs
2654 * Write the general registers back to the process or LWP.
2655 * Returns non-zero for success, zero for failure.
2659 proc_set_gregs (procinfo
*pi
)
2661 gdb_gregset_t
*gregs
;
2664 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2665 return 0; /* get_regs has already warned */
2667 if (pi
->ctl_fd
== 0 &&
2668 open_procinfo_files (pi
, FD_CTL
) == 0)
2677 /* Use char array to avoid alignment issues. */
2678 char gregs
[sizeof (gdb_gregset_t
)];
2682 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2683 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2685 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2689 /* Policy: writing the regs invalidates our cache. */
2690 pi
->gregs_valid
= 0;
2695 * Function: proc_set_fpregs
2697 * Modify the floating point register set of the process or LWP.
2698 * Returns non-zero for success, zero for failure.
2702 proc_set_fpregs (procinfo
*pi
)
2704 gdb_fpregset_t
*fpregs
;
2707 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2708 return 0; /* get_fpregs has already warned */
2710 if (pi
->ctl_fd
== 0 &&
2711 open_procinfo_files (pi
, FD_CTL
) == 0)
2720 /* Use char array to avoid alignment issues. */
2721 char fpregs
[sizeof (gdb_fpregset_t
)];
2725 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2726 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2730 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2735 tid_t pr_error_thread
;
2736 tfpregset_t thread_1
;
2739 thread_fpregs
.pr_count
= 1;
2740 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2741 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2743 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2746 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2747 #endif /* osf PIOCTSFPREG */
2748 #endif /* NEW_PROC_API */
2751 /* Policy: writing the regs invalidates our cache. */
2752 pi
->fpregs_valid
= 0;
2757 * Function: proc_kill
2759 * Send a signal to the proc or lwp with the semantics of "kill()".
2760 * Returns non-zero for success, zero for failure.
2764 proc_kill (procinfo
*pi
, int signo
)
2769 * We might conceivably apply this operation to an LWP, and
2770 * the LWP's ctl file descriptor might not be open.
2773 if (pi
->ctl_fd
== 0 &&
2774 open_procinfo_files (pi
, FD_CTL
) == 0)
2781 procfs_ctl_t cmd
[2];
2785 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2786 #else /* ioctl method */
2787 /* FIXME: do I need the Alpha OSF fixups present in
2788 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2789 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2797 * Function: proc_parent_pid
2799 * Find the pid of the process that started this one.
2800 * Returns the parent process pid, or zero.
2804 proc_parent_pid (procinfo
*pi
)
2807 * We should never have to apply this operation to any procinfo
2808 * except the one for the main process. If that ever changes
2809 * for any reason, then take out the following clause and
2810 * replace it with one that makes sure the ctl_fd is open.
2814 pi
= find_procinfo_or_die (pi
->pid
, 0);
2816 if (!pi
->status_valid
)
2817 if (!proc_get_status (pi
))
2820 return pi
->prstatus
.pr_ppid
;
2825 * Function: proc_set_watchpoint
2830 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2832 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2835 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2836 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2841 char watch
[sizeof (prwatch_t
)];
2845 pwatch
= (prwatch_t
*) &arg
.watch
;
2846 pwatch
->pr_vaddr
= address_to_host_pointer (addr
);
2847 pwatch
->pr_size
= len
;
2848 pwatch
->pr_wflags
= wflags
;
2849 #if defined(NEW_PROC_API) && defined (PCWATCH)
2851 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2853 #if defined (PIOCSWATCH)
2854 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2856 return 0; /* Fail */
2864 * Function: proc_iterate_over_mappings
2866 * Given a pointer to a function, call that function once for every
2867 * mapped address space in the process. The callback function
2868 * receives an open file descriptor for the file corresponding to
2869 * that mapped address space (if there is one), and the base address
2870 * of the mapped space. Quit when the callback function returns a
2871 * nonzero value, or at teh end of the mappings.
2873 * Returns: the first non-zero return value of the callback function,
2877 /* FIXME: it's probably a waste to cache this FD.
2878 It doesn't get called that often... and if I open it
2879 every time, I don't need to lseek it. */
2881 proc_iterate_over_mappings (int (*func
) (int, CORE_ADDR
))
2885 #ifndef NEW_PROC_API /* avoid compiler warning */
2890 char pathname
[MAX_PROC_NAME_SIZE
];
2895 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
2899 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
2900 if ((map_fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2901 proc_error (pi
, "proc_iterate_over_mappings (open)", __LINE__
);
2903 /* Make sure it gets closed again. */
2904 make_cleanup_close (map_fd
);
2906 /* Allocate space for mapping (lifetime only for this function). */
2907 map
= alloca (sizeof (struct prmap
));
2909 /* Now read the mappings from the file,
2910 open a file descriptor for those that have a name,
2911 and call the callback function. */
2912 while (read (map_fd
,
2914 sizeof (struct prmap
)) == sizeof (struct prmap
))
2916 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
2918 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
2921 if (map
->pr_mapname
[0] == 0)
2923 fd
= -1; /* no map file */
2927 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
2928 /* Note: caller's responsibility to close this fd! */
2929 fd
= open_with_retry (name
, O_RDONLY
);
2930 /* Note: we don't test the above call for failure;
2931 we just pass the FD on as given. Sometimes there is
2932 no file, so the ioctl may return failure, but that's
2936 /* Stop looping if the callback returns non-zero. */
2937 if ((funcstat
= (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
)) != 0)
2941 /* Get the number of mapping entries. */
2942 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmaps
) < 0)
2943 proc_error (pi
, "proc_iterate_over_mappings (PIOCNMAP)", __LINE__
);
2945 /* Allocate space for mappings (lifetime only this function). */
2946 map
= (struct prmap
*) alloca ((nmaps
+ 1) * sizeof (struct prmap
));
2948 /* Read in all the mappings. */
2949 if (ioctl (pi
->ctl_fd
, PIOCMAP
, map
) < 0)
2950 proc_error (pi
, "proc_iterate_over_mappings (PIOCMAP)", __LINE__
);
2952 /* Now loop through the mappings, open an fd for each, and
2953 call the callback function. */
2955 i
< nmaps
&& map
[i
].pr_size
!= 0;
2958 /* Note: caller's responsibility to close this fd! */
2959 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
[i
].pr_vaddr
);
2960 /* Note: we don't test the above call for failure;
2961 we just pass the FD on as given. Sometimes there is
2962 no file, so the ioctl may return failure, but that's
2965 /* Stop looping if the callback returns non-zero. */
2966 funcstat
= (*func
) (fd
, host_pointer_to_address (map
[i
].pr_vaddr
));
2975 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2977 #include <sys/sysi86.h>
2980 * Function: proc_get_LDT_entry
2986 * The 'key' is actually the value of the lower 16 bits of
2987 * the GS register for the LWP that we're interested in.
2989 * Return: matching ssh struct (LDT entry).
2993 proc_get_LDT_entry (procinfo
*pi
, int key
)
2995 static struct ssd
*ldt_entry
= NULL
;
2997 char pathname
[MAX_PROC_NAME_SIZE
];
2998 struct cleanup
*old_chain
= NULL
;
3001 /* Allocate space for one LDT entry.
3002 This alloc must persist, because we return a pointer to it. */
3003 if (ldt_entry
== NULL
)
3004 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
3006 /* Open the file descriptor for the LDT table. */
3007 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
3008 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
3010 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
3013 /* Make sure it gets closed again! */
3014 old_chain
= make_cleanup_close (fd
);
3016 /* Now 'read' thru the table, find a match and return it. */
3017 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
3019 if (ldt_entry
->sel
== 0 &&
3020 ldt_entry
->bo
== 0 &&
3021 ldt_entry
->acc1
== 0 &&
3022 ldt_entry
->acc2
== 0)
3023 break; /* end of table */
3024 /* If key matches, return this entry. */
3025 if (ldt_entry
->sel
== key
)
3028 /* Loop ended, match not found. */
3032 static int nalloc
= 0;
3034 /* Get the number of LDT entries. */
3035 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
3037 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
3041 /* Allocate space for the number of LDT entries. */
3042 /* This alloc has to persist, 'cause we return a pointer to it. */
3045 ldt_entry
= (struct ssd
*)
3046 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
3050 /* Read the whole table in one gulp. */
3051 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
3053 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
3057 /* Search the table and return the (first) entry matching 'key'. */
3058 for (i
= 0; i
< nldt
; i
++)
3059 if (ldt_entry
[i
].sel
== key
)
3060 return &ldt_entry
[i
];
3062 /* Loop ended, match not found. */
3067 #endif /* TM_I386SOL2_H */
3069 /* =============== END, non-thread part of /proc "MODULE" =============== */
3071 /* =================== Thread "MODULE" =================== */
3073 /* NOTE: you'll see more ifdefs and duplication of functions here,
3074 since there is a different way to do threads on every OS. */
3077 * Function: proc_get_nthreads
3079 * Return the number of threads for the process
3082 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3087 proc_get_nthreads (procinfo
*pi
)
3091 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
3092 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
3098 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3100 * Solaris and Unixware version
3103 proc_get_nthreads (procinfo
*pi
)
3105 if (!pi
->status_valid
)
3106 if (!proc_get_status (pi
))
3110 * NEW_PROC_API: only works for the process procinfo,
3111 * because the LWP procinfos do not get prstatus filled in.
3114 if (pi
->tid
!= 0) /* find the parent process procinfo */
3115 pi
= find_procinfo_or_die (pi
->pid
, 0);
3117 return pi
->prstatus
.pr_nlwp
;
3125 proc_get_nthreads (procinfo
*pi
)
3133 * Function: proc_get_current_thread (LWP version)
3135 * Return the ID of the thread that had an event of interest.
3136 * (ie. the one that hit a breakpoint or other traced event).
3137 * All other things being equal, this should be the ID of a
3138 * thread that is currently executing.
3141 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3143 * Solaris and Unixware version
3146 proc_get_current_thread (procinfo
*pi
)
3149 * Note: this should be applied to the root procinfo for the process,
3150 * not to the procinfo for an LWP. If applied to the procinfo for
3151 * an LWP, it will simply return that LWP's ID. In that case,
3152 * find the parent process procinfo.
3156 pi
= find_procinfo_or_die (pi
->pid
, 0);
3158 if (!pi
->status_valid
)
3159 if (!proc_get_status (pi
))
3163 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
3165 return pi
->prstatus
.pr_who
;
3170 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3175 proc_get_current_thread (procinfo
*pi
)
3177 #if 0 /* FIXME: not ready for prime time? */
3178 return pi
->prstatus
.pr_tid
;
3189 proc_get_current_thread (procinfo
*pi
)
3198 * Function: proc_update_threads
3200 * Discover the IDs of all the threads within the process, and
3201 * create a procinfo for each of them (chained to the parent).
3203 * This unfortunately requires a different method on every OS.
3205 * Return: non-zero for success, zero for failure.
3209 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
3211 if (thread
&& parent
) /* sanity */
3213 thread
->status_valid
= 0;
3214 if (!proc_get_status (thread
))
3215 destroy_one_procinfo (&parent
->thread_list
, thread
);
3217 return 0; /* keep iterating */
3220 #if defined (PIOCLSTATUS)
3222 * Solaris 2.5 (ioctl) version
3225 proc_update_threads (procinfo
*pi
)
3227 gdb_prstatus_t
*prstatus
;
3228 struct cleanup
*old_chain
= NULL
;
3233 * We should never have to apply this operation to any procinfo
3234 * except the one for the main process. If that ever changes
3235 * for any reason, then take out the following clause and
3236 * replace it with one that makes sure the ctl_fd is open.
3240 pi
= find_procinfo_or_die (pi
->pid
, 0);
3242 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3244 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
3245 return 1; /* Process is not multi-threaded; nothing to do. */
3247 prstatus
= xmalloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1));
3249 old_chain
= make_cleanup (xfree
, prstatus
);
3250 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
3251 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
3253 /* Skip element zero, which represents the process as a whole. */
3254 for (i
= 1; i
< nlwp
+ 1; i
++)
3256 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
3257 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3259 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
3260 thread
->status_valid
= 1;
3262 pi
->threads_valid
= 1;
3263 do_cleanups (old_chain
);
3269 * Unixware and Solaris 6 (and later) version
3272 do_closedir_cleanup (void *dir
)
3278 proc_update_threads (procinfo
*pi
)
3280 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3281 struct dirent
*direntry
;
3282 struct cleanup
*old_chain
= NULL
;
3288 * We should never have to apply this operation to any procinfo
3289 * except the one for the main process. If that ever changes
3290 * for any reason, then take out the following clause and
3291 * replace it with one that makes sure the ctl_fd is open.
3295 pi
= find_procinfo_or_die (pi
->pid
, 0);
3297 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3302 * Note: this brute-force method is the only way I know of
3303 * to accomplish this task on Unixware. This method will
3304 * also work on Solaris 2.6 and 2.7. There is a much simpler
3305 * and more elegant way to do this on Solaris, but the margins
3306 * of this manuscript are too small to write it here... ;-)
3309 strcpy (pathname
, pi
->pathname
);
3310 strcat (pathname
, "/lwp");
3311 if ((dirp
= opendir (pathname
)) == NULL
)
3312 proc_error (pi
, "update_threads, opendir", __LINE__
);
3314 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
3315 while ((direntry
= readdir (dirp
)) != NULL
)
3316 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3318 lwpid
= atoi (&direntry
->d_name
[0]);
3319 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3320 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3322 pi
->threads_valid
= 1;
3323 do_cleanups (old_chain
);
3332 proc_update_threads (procinfo
*pi
)
3338 * We should never have to apply this operation to any procinfo
3339 * except the one for the main process. If that ever changes
3340 * for any reason, then take out the following clause and
3341 * replace it with one that makes sure the ctl_fd is open.
3345 pi
= find_procinfo_or_die (pi
->pid
, 0);
3347 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3349 nthreads
= proc_get_nthreads (pi
);
3351 return 0; /* nothing to do for 1 or fewer threads */
3353 threads
= xmalloc (nthreads
* sizeof (tid_t
));
3355 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3356 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3358 for (i
= 0; i
< nthreads
; i
++)
3360 if (!find_procinfo (pi
->pid
, threads
[i
]))
3361 if (!create_procinfo (pi
->pid
, threads
[i
]))
3362 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3364 pi
->threads_valid
= 1;
3372 proc_update_threads (procinfo
*pi
)
3376 #endif /* OSF PIOCTLIST */
3377 #endif /* NEW_PROC_API */
3378 #endif /* SOL 2.5 PIOCLSTATUS */
3381 * Function: proc_iterate_over_threads
3384 * Given a pointer to a function, call that function once
3385 * for each lwp in the procinfo list, until the function
3386 * returns non-zero, in which event return the value
3387 * returned by the function.
3389 * Note: this function does NOT call update_threads.
3390 * If you want to discover new threads first, you must
3391 * call that function explicitly. This function just makes
3392 * a quick pass over the currently-known procinfos.
3395 * pi - parent process procinfo
3396 * func - per-thread function
3397 * ptr - opaque parameter for function.
3400 * First non-zero return value from the callee, or zero.
3404 proc_iterate_over_threads (procinfo
*pi
,
3405 int (*func
) (procinfo
*, procinfo
*, void *),
3408 procinfo
*thread
, *next
;
3412 * We should never have to apply this operation to any procinfo
3413 * except the one for the main process. If that ever changes
3414 * for any reason, then take out the following clause and
3415 * replace it with one that makes sure the ctl_fd is open.
3419 pi
= find_procinfo_or_die (pi
->pid
, 0);
3421 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3423 next
= thread
->next
; /* in case thread is destroyed */
3424 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3431 /* =================== END, Thread "MODULE" =================== */
3433 /* =================== END, /proc "MODULE" =================== */
3435 /* =================== GDB "MODULE" =================== */
3438 * Here are all of the gdb target vector functions and their friends.
3441 static ptid_t
do_attach (ptid_t ptid
);
3442 static void do_detach (int signo
);
3443 static int register_gdb_signals (procinfo
*, gdb_sigset_t
*);
3446 * Function: procfs_debug_inferior
3448 * Sets up the inferior to be debugged.
3449 * Registers to trace signals, hardware faults, and syscalls.
3450 * Note: does not set RLC flag: caller may want to customize that.
3452 * Returns: zero for success (note! unlike most functions in this module)
3453 * On failure, returns the LINE NUMBER where it failed!
3457 procfs_debug_inferior (procinfo
*pi
)
3459 fltset_t traced_faults
;
3460 gdb_sigset_t traced_signals
;
3461 sysset_t
*traced_syscall_entries
;
3462 sysset_t
*traced_syscall_exits
;
3465 #ifdef PROCFS_DONT_TRACE_FAULTS
3466 /* On some systems (OSF), we don't trace hardware faults.
3467 Apparently it's enough that we catch them as signals.
3468 Wonder why we don't just do that in general? */
3469 premptyset (&traced_faults
); /* don't trace faults. */
3471 /* Register to trace hardware faults in the child. */
3472 prfillset (&traced_faults
); /* trace all faults... */
3473 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3475 if (!proc_set_traced_faults (pi
, &traced_faults
))
3478 /* Register to trace selected signals in the child. */
3479 premptyset (&traced_signals
);
3480 if (!register_gdb_signals (pi
, &traced_signals
))
3484 /* Register to trace the 'exit' system call (on entry). */
3485 traced_syscall_entries
= sysset_t_alloc (pi
);
3486 gdb_premptysysset (traced_syscall_entries
);
3488 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
3491 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3494 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
3496 #ifdef DYNAMIC_SYSCALLS
3498 int callnum
= find_syscall (pi
, "_exit");
3500 gdb_praddsysset (traced_syscall_entries
, callnum
);
3504 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
3505 xfree (traced_syscall_entries
);
3509 #ifdef PRFS_STOPEXEC /* defined on OSF */
3510 /* OSF method for tracing exec syscalls. Quoting:
3511 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3512 exits from exec system calls because of the user level loader. */
3513 /* FIXME: make nice and maybe move into an access function. */
3517 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3520 prfs_flags
|= PRFS_STOPEXEC
;
3522 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3525 #else /* not PRFS_STOPEXEC */
3526 /* Everyone else's (except OSF) method for tracing exec syscalls */
3528 Not all systems with /proc have all the exec* syscalls with the same
3529 names. On the SGI, for example, there is no SYS_exec, but there
3530 *is* a SYS_execv. So, we try to account for that. */
3532 traced_syscall_exits
= sysset_t_alloc (pi
);
3533 gdb_premptysysset (traced_syscall_exits
);
3535 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
3538 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
3541 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
3544 #ifdef SYS_lwpcreate
3545 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3546 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3549 #ifdef SYS_lwp_create /* FIXME: once only, please */
3550 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3551 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3554 #ifdef DYNAMIC_SYSCALLS
3556 int callnum
= find_syscall (pi
, "execve");
3558 gdb_praddsysset (traced_syscall_exits
, callnum
);
3559 callnum
= find_syscall (pi
, "ra_execve");
3561 gdb_praddsysset (traced_syscall_exits
, callnum
);
3565 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3566 xfree (traced_syscall_exits
);
3570 #endif /* PRFS_STOPEXEC */
3575 procfs_attach (char *args
, int from_tty
)
3581 error_no_arg ("process-id to attach");
3584 if (pid
== getpid ())
3585 error ("Attaching GDB to itself is not a good idea...");
3589 exec_file
= get_exec_file (0);
3592 printf_filtered ("Attaching to program `%s', %s\n",
3593 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3595 printf_filtered ("Attaching to %s\n",
3596 target_pid_to_str (pid_to_ptid (pid
)));
3600 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3601 push_target (&procfs_ops
);
3605 procfs_detach (char *args
, int from_tty
)
3612 exec_file
= get_exec_file (0);
3615 printf_filtered ("Detaching from program: %s %s\n",
3616 exec_file
, target_pid_to_str (inferior_ptid
));
3620 signo
= atoi (args
);
3623 inferior_ptid
= null_ptid
;
3624 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3628 do_attach (ptid_t ptid
)
3633 if ((pi
= create_procinfo (PIDGET (ptid
), 0)) == NULL
)
3634 perror ("procfs: out of memory in 'attach'");
3636 if (!open_procinfo_files (pi
, FD_CTL
))
3638 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3639 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3641 dead_procinfo (pi
, errmsg
, NOKILL
);
3644 /* Stop the process (if it isn't already stopped). */
3645 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3647 pi
->was_stopped
= 1;
3648 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3652 pi
->was_stopped
= 0;
3653 /* Set the process to run again when we close it. */
3654 if (!proc_set_run_on_last_close (pi
))
3655 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3657 /* Now stop the process. */
3658 if (!proc_stop_process (pi
))
3659 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3660 pi
->ignore_next_sigstop
= 1;
3662 /* Save some of the /proc state to be restored if we detach. */
3663 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3664 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3665 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3666 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3667 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3668 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3670 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3671 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3673 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3674 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3676 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3677 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3679 /* Let GDB know that the inferior was attached. */
3681 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3685 do_detach (int signo
)
3689 /* Find procinfo for the main process */
3690 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0); /* FIXME: threads */
3692 if (!proc_set_current_signal (pi
, signo
))
3693 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3695 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3696 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3698 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3699 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3701 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3702 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3704 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3705 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3707 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3708 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3710 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3711 if (signo
|| !(pi
->was_stopped
) ||
3712 query ("Was stopped when attached, make it runnable again? "))
3714 /* Clear any pending signal. */
3715 if (!proc_clear_current_fault (pi
))
3716 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3718 if (!proc_set_run_on_last_close (pi
))
3719 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3723 destroy_procinfo (pi
);
3729 * Since the /proc interface cannot give us individual registers,
3730 * we pay no attention to the (regno) argument, and just fetch them all.
3731 * This results in the possibility that we will do unnecessarily many
3732 * fetches, since we may be called repeatedly for individual registers.
3733 * So we cache the results, and mark the cache invalid when the process
3738 procfs_fetch_registers (int regno
)
3740 gdb_fpregset_t
*fpregs
;
3741 gdb_gregset_t
*gregs
;
3746 pid
= PIDGET (inferior_ptid
);
3747 tid
= TIDGET (inferior_ptid
);
3749 /* First look up procinfo for the main process. */
3750 pi
= find_procinfo_or_die (pid
, 0);
3752 /* If the event thread is not the same as GDB's requested thread
3753 (ie. inferior_ptid), then look up procinfo for the requested
3756 (tid
!= proc_get_current_thread (pi
)))
3757 pi
= find_procinfo_or_die (pid
, tid
);
3760 error ("procfs: fetch_registers failed to find procinfo for %s",
3761 target_pid_to_str (inferior_ptid
));
3763 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3764 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3766 supply_gregset (gregs
);
3768 if (FP0_REGNUM
>= 0) /* need floating point? */
3770 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3771 regno
== PC_REGNUM
||
3772 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3773 regno
== FP_REGNUM
||
3775 return; /* not a floating point register */
3777 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3778 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3780 supply_fpregset (fpregs
);
3784 /* Get ready to modify the registers array. On machines which store
3785 individual registers, this doesn't need to do anything. On
3786 machines which store all the registers in one fell swoop, such as
3787 /proc, this makes sure that registers contains all the registers
3788 from the program being debugged. */
3791 procfs_prepare_to_store (void)
3793 #ifdef CHILD_PREPARE_TO_STORE
3794 CHILD_PREPARE_TO_STORE ();
3801 * Since the /proc interface will not read individual registers,
3802 * we will cache these requests until the process is resumed, and
3803 * only then write them back to the inferior process.
3805 * FIXME: is that a really bad idea? Have to think about cases
3806 * where writing one register might affect the value of others, etc.
3810 procfs_store_registers (int regno
)
3812 gdb_fpregset_t
*fpregs
;
3813 gdb_gregset_t
*gregs
;
3818 pid
= PIDGET (inferior_ptid
);
3819 tid
= TIDGET (inferior_ptid
);
3821 /* First find procinfo for main process */
3822 pi
= find_procinfo_or_die (pid
, 0);
3824 /* If current lwp for process is not the same as requested thread
3825 (ie. inferior_ptid), then find procinfo for the requested thread. */
3828 (tid
!= proc_get_current_thread (pi
)))
3829 pi
= find_procinfo_or_die (pid
, tid
);
3832 error ("procfs: store_registers: failed to find procinfo for %s",
3833 target_pid_to_str (inferior_ptid
));
3835 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3836 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3838 fill_gregset (gregs
, regno
);
3839 if (!proc_set_gregs (pi
))
3840 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3842 if (FP0_REGNUM
>= 0) /* need floating point? */
3844 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3845 regno
== PC_REGNUM
||
3846 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3847 regno
== FP_REGNUM
||
3849 return; /* not a floating point register */
3851 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3852 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3854 fill_fpregset (fpregs
, regno
);
3855 if (!proc_set_fpregs (pi
))
3856 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3861 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3865 if (scall
== SYS_lwp_exit
)
3869 if (scall
== SYS_lwpexit
)
3876 syscall_is_exit (procinfo
*pi
, int scall
)
3879 if (scall
== SYS_exit
)
3882 #ifdef DYNAMIC_SYSCALLS
3883 if (find_syscall (pi
, "_exit") == scall
)
3890 syscall_is_exec (procinfo
*pi
, int scall
)
3893 if (scall
== SYS_exec
)
3897 if (scall
== SYS_execv
)
3901 if (scall
== SYS_execve
)
3904 #ifdef DYNAMIC_SYSCALLS
3905 if (find_syscall (pi
, "_execve"))
3907 if (find_syscall (pi
, "ra_execve"))
3914 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3916 #ifdef SYS_lwp_create
3917 if (scall
== SYS_lwp_create
)
3920 #ifdef SYS_lwpcreate
3921 if (scall
== SYS_lwpcreate
)
3928 * Function: target_wait
3930 * Retrieve the next stop event from the child process.
3931 * If child has not stopped yet, wait for it to stop.
3932 * Translate /proc eventcodes (or possibly wait eventcodes)
3933 * into gdb internal event codes.
3935 * Return: id of process (and possibly thread) that incurred the event.
3936 * event codes are returned thru a pointer parameter.
3940 procfs_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3942 /* First cut: loosely based on original version 2.1 */
3946 ptid_t retval
, temp_ptid
;
3947 int why
, what
, flags
;
3954 retval
= pid_to_ptid (-1);
3956 /* Find procinfo for main process */
3957 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
3960 /* We must assume that the status is stale now... */
3961 pi
->status_valid
= 0;
3962 pi
->gregs_valid
= 0;
3963 pi
->fpregs_valid
= 0;
3965 #if 0 /* just try this out... */
3966 flags
= proc_flags (pi
);
3967 why
= proc_why (pi
);
3968 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3969 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3971 /* If child is not stopped, wait for it to stop. */
3972 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3973 !proc_wait_for_stop (pi
))
3975 /* wait_for_stop failed: has the child terminated? */
3976 if (errno
== ENOENT
)
3980 /* /proc file not found; presumably child has terminated. */
3981 wait_retval
= wait (&wstat
); /* "wait" for the child's exit */
3983 if (wait_retval
!= PIDGET (inferior_ptid
)) /* wrong child? */
3984 error ("procfs: couldn't stop process %d: wait returned %d\n",
3985 PIDGET (inferior_ptid
), wait_retval
);
3986 /* FIXME: might I not just use waitpid?
3987 Or try find_procinfo to see if I know about this child? */
3988 retval
= pid_to_ptid (wait_retval
);
3990 else if (errno
== EINTR
)
3994 /* Unknown error from wait_for_stop. */
3995 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
4000 /* This long block is reached if either:
4001 a) the child was already stopped, or
4002 b) we successfully waited for the child with wait_for_stop.
4003 This block will analyze the /proc status, and translate it
4004 into a waitstatus for GDB.
4006 If we actually had to call wait because the /proc file
4007 is gone (child terminated), then we skip this block,
4008 because we already have a waitstatus. */
4010 flags
= proc_flags (pi
);
4011 why
= proc_why (pi
);
4012 what
= proc_what (pi
);
4014 if (flags
& (PR_STOPPED
| PR_ISTOP
))
4017 /* If it's running async (for single_thread control),
4018 set it back to normal again. */
4019 if (flags
& PR_ASYNC
)
4020 if (!proc_unset_async (pi
))
4021 proc_error (pi
, "target_wait, unset_async", __LINE__
);
4025 proc_prettyprint_why (why
, what
, 1);
4027 /* The 'pid' we will return to GDB is composed of
4028 the process ID plus the lwp ID. */
4029 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4033 wstat
= (what
<< 8) | 0177;
4036 if (syscall_is_lwp_exit (pi
, what
))
4038 printf_filtered ("[%s exited]\n",
4039 target_pid_to_str (retval
));
4040 delete_thread (retval
);
4041 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4044 else if (syscall_is_exit (pi
, what
))
4046 /* Handle SYS_exit call only */
4047 /* Stopped at entry to SYS_exit.
4048 Make it runnable, resume it, then use
4049 the wait system call to get its exit code.
4050 Proc_run_process always clears the current
4052 Then return its exit status. */
4053 pi
->status_valid
= 0;
4055 /* FIXME: what we should do is return
4056 TARGET_WAITKIND_SPURIOUS. */
4057 if (!proc_run_process (pi
, 0, 0))
4058 proc_error (pi
, "target_wait, run_process", __LINE__
);
4061 /* Don't call wait: simulate waiting for exit,
4062 return a "success" exit code. Bogus: what if
4063 it returns something else? */
4065 retval
= inferior_ptid
; /* ? ? ? */
4069 int temp
= wait (&wstat
);
4071 /* FIXME: shouldn't I make sure I get the right
4072 event from the right process? If (for
4073 instance) I have killed an earlier inferior
4074 process but failed to clean up after it
4075 somehow, I could get its termination event
4078 /* If wait returns -1, that's what we return to GDB. */
4080 retval
= pid_to_ptid (temp
);
4085 printf_filtered ("procfs: trapped on entry to ");
4086 proc_prettyprint_syscall (proc_what (pi
), 0);
4087 printf_filtered ("\n");
4090 long i
, nsysargs
, *sysargs
;
4092 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4093 (sysargs
= proc_sysargs (pi
)) != NULL
)
4095 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4096 for (i
= 0; i
< nsysargs
; i
++)
4097 printf_filtered ("#%ld: 0x%08lx\n",
4105 /* How to exit gracefully, returning "unknown event" */
4106 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4107 return inferior_ptid
;
4111 /* How to keep going without returning to wfi: */
4112 target_resume (ptid
, 0, TARGET_SIGNAL_0
);
4118 if (syscall_is_exec (pi
, what
))
4120 /* Hopefully this is our own "fork-child" execing
4121 the real child. Hoax this event into a trap, and
4122 GDB will see the child about to execute its start
4124 wstat
= (SIGTRAP
<< 8) | 0177;
4126 else if (syscall_is_lwp_create (pi
, what
))
4129 * This syscall is somewhat like fork/exec.
4130 * We will get the event twice: once for the parent LWP,
4131 * and once for the child. We should already know about
4132 * the parent LWP, but the child will be new to us. So,
4133 * whenever we get this event, if it represents a new
4134 * thread, simply add the thread to the list.
4137 /* If not in procinfo list, add it. */
4138 temp_tid
= proc_get_current_thread (pi
);
4139 if (!find_procinfo (pi
->pid
, temp_tid
))
4140 create_procinfo (pi
->pid
, temp_tid
);
4142 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4143 /* If not in GDB's thread list, add it. */
4144 if (!in_thread_list (temp_ptid
))
4146 printf_filtered ("[New %s]\n",
4147 target_pid_to_str (temp_ptid
));
4148 add_thread (temp_ptid
);
4150 /* Return to WFI, but tell it to immediately resume. */
4151 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4152 return inferior_ptid
;
4154 else if (syscall_is_lwp_exit (pi
, what
))
4156 printf_filtered ("[%s exited]\n",
4157 target_pid_to_str (retval
));
4158 delete_thread (retval
);
4159 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4164 /* FIXME: Do we need to handle SYS_sproc,
4165 SYS_fork, or SYS_vfork here? The old procfs
4166 seemed to use this event to handle threads on
4167 older (non-LWP) systems, where I'm assuming
4168 that threads were actually separate processes.
4169 Irix, maybe? Anyway, low priority for now. */
4173 printf_filtered ("procfs: trapped on exit from ");
4174 proc_prettyprint_syscall (proc_what (pi
), 0);
4175 printf_filtered ("\n");
4178 long i
, nsysargs
, *sysargs
;
4180 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4181 (sysargs
= proc_sysargs (pi
)) != NULL
)
4183 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4184 for (i
= 0; i
< nsysargs
; i
++)
4185 printf_filtered ("#%ld: 0x%08lx\n",
4190 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4191 return inferior_ptid
;
4196 wstat
= (SIGSTOP
<< 8) | 0177;
4201 printf_filtered ("Retry #%d:\n", retry
);
4202 pi
->status_valid
= 0;
4207 /* If not in procinfo list, add it. */
4208 temp_tid
= proc_get_current_thread (pi
);
4209 if (!find_procinfo (pi
->pid
, temp_tid
))
4210 create_procinfo (pi
->pid
, temp_tid
);
4212 /* If not in GDB's thread list, add it. */
4213 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4214 if (!in_thread_list (temp_ptid
))
4216 printf_filtered ("[New %s]\n",
4217 target_pid_to_str (temp_ptid
));
4218 add_thread (temp_ptid
);
4221 status
->kind
= TARGET_WAITKIND_STOPPED
;
4222 status
->value
.sig
= 0;
4227 wstat
= (what
<< 8) | 0177;
4230 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
4233 wstat
= (SIGTRAP
<< 8) | 0177;
4238 wstat
= (SIGTRAP
<< 8) | 0177;
4241 /* FIXME: use si_signo where possible. */
4243 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4246 wstat
= (SIGILL
<< 8) | 0177;
4249 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4252 wstat
= (SIGTRAP
<< 8) | 0177;
4256 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4259 wstat
= (SIGSEGV
<< 8) | 0177;
4263 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4266 wstat
= (SIGFPE
<< 8) | 0177;
4268 case FLTPAGE
: /* Recoverable page fault */
4269 default: /* FIXME: use si_signo if possible for fault */
4270 retval
= pid_to_ptid (-1);
4271 printf_filtered ("procfs:%d -- ", __LINE__
);
4272 printf_filtered ("child stopped for unknown reason:\n");
4273 proc_prettyprint_why (why
, what
, 1);
4274 error ("... giving up...");
4277 break; /* case PR_FAULTED: */
4278 default: /* switch (why) unmatched */
4279 printf_filtered ("procfs:%d -- ", __LINE__
);
4280 printf_filtered ("child stopped for unknown reason:\n");
4281 proc_prettyprint_why (why
, what
, 1);
4282 error ("... giving up...");
4286 * Got this far without error:
4287 * If retval isn't in the threads database, add it.
4289 if (PIDGET (retval
) > 0 &&
4290 !ptid_equal (retval
, inferior_ptid
) &&
4291 !in_thread_list (retval
))
4294 * We have a new thread.
4295 * We need to add it both to GDB's list and to our own.
4296 * If we don't create a procinfo, resume may be unhappy
4299 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
4300 add_thread (retval
);
4301 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
4302 create_procinfo (PIDGET (retval
), TIDGET (retval
));
4304 /* In addition, it's possible that this is the first
4305 * new thread we've seen, in which case we may not
4306 * have created entries for inferior_ptid yet.
4308 if (TIDGET (inferior_ptid
) != 0)
4310 if (!in_thread_list (inferior_ptid
))
4311 add_thread (inferior_ptid
);
4312 if (find_procinfo (PIDGET (inferior_ptid
),
4313 TIDGET (inferior_ptid
)) == NULL
)
4314 create_procinfo (PIDGET (inferior_ptid
),
4315 TIDGET (inferior_ptid
));
4319 else /* flags do not indicate STOPPED */
4321 /* surely this can't happen... */
4322 printf_filtered ("procfs:%d -- process not stopped.\n",
4324 proc_prettyprint_flags (flags
, 1);
4325 error ("procfs: ...giving up...");
4330 store_waitstatus (status
, wstat
);
4336 /* Transfer LEN bytes between GDB address MYADDR and target address
4337 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4338 otherwise transfer them from the target. TARGET is unused.
4340 The return value is 0 if an error occurred or no bytes were
4341 transferred. Otherwise, it will be a positive value which
4342 indicates the number of bytes transferred between gdb and the
4343 target. (Note that the interface also makes provisions for
4344 negative values, but this capability isn't implemented here.) */
4347 procfs_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int dowrite
,
4348 struct mem_attrib
*attrib
, struct target_ops
*target
)
4353 /* Find procinfo for main process */
4354 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4355 if (pi
->as_fd
== 0 &&
4356 open_procinfo_files (pi
, FD_AS
) == 0)
4358 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4362 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4367 PROCFS_NOTE ("write memory: ");
4369 PROCFS_NOTE ("write memory: \n");
4371 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4375 PROCFS_NOTE ("read memory: \n");
4376 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4387 * Function: invalidate_cache
4389 * Called by target_resume before making child runnable.
4390 * Mark cached registers and status's invalid.
4391 * If there are "dirty" caches that need to be written back
4392 * to the child process, do that.
4394 * File descriptors are also cached.
4395 * As they are a limited resource, we cannot hold onto them indefinitely.
4396 * However, as they are expensive to open, we don't want to throw them
4397 * away indescriminately either. As a compromise, we will keep the
4398 * file descriptors for the parent process, but discard any file
4399 * descriptors we may have accumulated for the threads.
4402 * As this function is called by iterate_over_threads, it always
4403 * returns zero (so that iterate_over_threads will keep iterating).
4408 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4411 * About to run the child; invalidate caches and do any other cleanup.
4415 if (pi
->gregs_dirty
)
4416 if (parent
== NULL
||
4417 proc_get_current_thread (parent
) != pi
->tid
)
4418 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4419 proc_warn (pi
, "target_resume, set_gregs",
4421 if (FP0_REGNUM
>= 0)
4422 if (pi
->fpregs_dirty
)
4423 if (parent
== NULL
||
4424 proc_get_current_thread (parent
) != pi
->tid
)
4425 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4426 proc_warn (pi
, "target_resume, set_fpregs",
4432 /* The presence of a parent indicates that this is an LWP.
4433 Close any file descriptors that it might have open.
4434 We don't do this to the master (parent) procinfo. */
4436 close_procinfo_files (pi
);
4438 pi
->gregs_valid
= 0;
4439 pi
->fpregs_valid
= 0;
4441 pi
->gregs_dirty
= 0;
4442 pi
->fpregs_dirty
= 0;
4444 pi
->status_valid
= 0;
4445 pi
->threads_valid
= 0;
4452 * Function: make_signal_thread_runnable
4454 * A callback function for iterate_over_threads.
4455 * Find the asynchronous signal thread, and make it runnable.
4456 * See if that helps matters any.
4460 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4463 if (proc_flags (pi
) & PR_ASLWP
)
4465 if (!proc_run_process (pi
, 0, -1))
4466 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4475 * Function: target_resume
4477 * Make the child process runnable. Normally we will then call
4478 * procfs_wait and wait for it to stop again (unles gdb is async).
4481 * step: if true, then arrange for the child to stop again
4482 * after executing a single instruction.
4483 * signo: if zero, then cancel any pending signal.
4484 * If non-zero, then arrange for the indicated signal
4485 * to be delivered to the child when it runs.
4486 * pid: if -1, then allow any child thread to run.
4487 * if non-zero, then allow only the indicated thread to run.
4488 ******* (not implemented yet)
4492 procfs_resume (ptid_t ptid
, int step
, enum target_signal signo
)
4494 procinfo
*pi
, *thread
;
4498 prrun.prflags |= PRSVADDR;
4499 prrun.pr_vaddr = $PC; set resume address
4500 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4501 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4502 prrun.prflags |= PRCFAULT; clear current fault.
4504 PRSTRACE and PRSFAULT can be done by other means
4505 (proc_trace_signals, proc_trace_faults)
4506 PRSVADDR is unnecessary.
4507 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4508 This basically leaves PRSTEP and PRCSIG.
4509 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4510 So basically PR_STEP is the sole argument that must be passed
4511 to proc_run_process (for use in the prrun struct by ioctl). */
4513 /* Find procinfo for main process */
4514 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4516 /* First cut: ignore pid argument */
4519 /* Convert signal to host numbering. */
4521 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4524 native_signo
= target_signal_to_host (signo
);
4526 pi
->ignore_next_sigstop
= 0;
4528 /* Running the process voids all cached registers and status. */
4529 /* Void the threads' caches first */
4530 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4531 /* Void the process procinfo's caches. */
4532 invalidate_cache (NULL
, pi
, NULL
);
4534 if (PIDGET (ptid
) != -1)
4536 /* Resume a specific thread, presumably suppressing the others. */
4537 thread
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
));
4539 warning ("procfs: resume can't find thread %ld -- resuming all.",
4543 if (thread
->tid
!= 0)
4545 /* We're to resume a specific thread, and not the others.
4546 * Set the child process's PR_ASYNC flag.
4549 if (!proc_set_async (pi
))
4550 proc_error (pi
, "target_resume, set_async", __LINE__
);
4553 proc_iterate_over_threads (pi
,
4554 make_signal_thread_runnable
,
4557 pi
= thread
; /* substitute the thread's procinfo for run */
4562 if (!proc_run_process (pi
, step
, native_signo
))
4565 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4567 proc_error (pi
, "target_resume", __LINE__
);
4572 * Function: register_gdb_signals
4574 * Traverse the list of signals that GDB knows about
4575 * (see "handle" command), and arrange for the target
4576 * to be stopped or not, according to these settings.
4578 * Returns non-zero for success, zero for failure.
4582 register_gdb_signals (procinfo
*pi
, gdb_sigset_t
*signals
)
4586 for (signo
= 0; signo
< NSIG
; signo
++)
4587 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4588 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4589 signal_pass_state (target_signal_from_host (signo
)) == 1)
4590 prdelset (signals
, signo
);
4592 praddset (signals
, signo
);
4594 return proc_set_traced_signals (pi
, signals
);
4598 * Function: target_notice_signals
4600 * Set up to trace signals in the child process.
4604 procfs_notice_signals (ptid_t ptid
)
4606 gdb_sigset_t signals
;
4607 procinfo
*pi
= find_procinfo_or_die (PIDGET (ptid
), 0);
4609 if (proc_get_traced_signals (pi
, &signals
) &&
4610 register_gdb_signals (pi
, &signals
))
4613 proc_error (pi
, "notice_signals", __LINE__
);
4617 * Function: target_files_info
4619 * Print status information about the child process.
4623 procfs_files_info (struct target_ops
*ignore
)
4625 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4626 attach_flag
? "attached": "child",
4627 target_pid_to_str (inferior_ptid
));
4631 * Function: target_open
4633 * A dummy: you don't open procfs.
4637 procfs_open (char *args
, int from_tty
)
4639 error ("Use the \"run\" command to start a Unix child process.");
4643 * Function: target_can_run
4645 * This tells GDB that this target vector can be invoked
4646 * for "run" or "attach".
4649 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4650 be a runnable target. Used by targets
4651 that can sit atop procfs, such as solaris
4656 procfs_can_run (void)
4658 /* This variable is controlled by modules that sit atop procfs that
4659 may layer their own process structure atop that provided here.
4660 sol-thread.c does this because of the Solaris two-level thread
4663 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4665 return !procfs_suppress_run
;
4669 * Function: target_stop
4671 * Stop the child process asynchronously, as when the
4672 * gdb user types control-c or presses a "stop" button.
4674 * Works by sending kill(SIGINT) to the child's process group.
4680 extern pid_t inferior_process_group
;
4682 kill (-inferior_process_group
, SIGINT
);
4686 * Function: unconditionally_kill_inferior
4688 * Make it die. Wait for it to die. Clean up after it.
4689 * Note: this should only be applied to the real process,
4690 * not to an LWP, because of the check for parent-process.
4691 * If we need this to work for an LWP, it needs some more logic.
4695 unconditionally_kill_inferior (procinfo
*pi
)
4699 parent_pid
= proc_parent_pid (pi
);
4700 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4701 /* FIXME: use access functions */
4702 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4703 before the PIOCKILL, otherwise it might generate a corrupted core
4704 file for the inferior. */
4705 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4707 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4710 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4711 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4712 to kill the inferior, otherwise it might remain stopped with a
4714 We do not check the result of the PIOCSSIG, the inferior might have
4717 gdb_siginfo_t newsiginfo
;
4719 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4720 newsiginfo
.si_signo
= SIGKILL
;
4721 newsiginfo
.si_code
= 0;
4722 newsiginfo
.si_errno
= 0;
4723 newsiginfo
.si_pid
= getpid ();
4724 newsiginfo
.si_uid
= getuid ();
4725 /* FIXME: use proc_set_current_signal */
4726 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4728 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4729 if (!proc_kill (pi
, SIGKILL
))
4730 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4731 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4732 destroy_procinfo (pi
);
4734 /* If pi is GDB's child, wait for it to die. */
4735 if (parent_pid
== getpid ())
4736 /* FIXME: should we use waitpid to make sure we get the right event?
4737 Should we check the returned event? */
4742 ret
= waitpid (pi
->pid
, &status
, 0);
4750 * Function: target_kill_inferior
4752 * We're done debugging it, and we want it to go away.
4753 * Then we want GDB to forget all about it.
4757 procfs_kill_inferior (void)
4759 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4761 /* Find procinfo for main process */
4762 procinfo
*pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4765 unconditionally_kill_inferior (pi
);
4766 target_mourn_inferior ();
4771 * Function: target_mourn_inferior
4773 * Forget we ever debugged this thing!
4777 procfs_mourn_inferior (void)
4781 if (!ptid_equal (inferior_ptid
, null_ptid
))
4783 /* Find procinfo for main process */
4784 pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4786 destroy_procinfo (pi
);
4788 unpush_target (&procfs_ops
);
4789 generic_mourn_inferior ();
4793 * Function: init_inferior
4795 * When GDB forks to create a runnable inferior process,
4796 * this function is called on the parent side of the fork.
4797 * It's job is to do whatever is necessary to make the child
4798 * ready to be debugged, and then wait for the child to synchronize.
4802 procfs_init_inferior (int pid
)
4805 gdb_sigset_t signals
;
4808 /* This routine called on the parent side (GDB side)
4809 after GDB forks the inferior. */
4811 push_target (&procfs_ops
);
4813 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4814 perror ("procfs: out of memory in 'init_inferior'");
4816 if (!open_procinfo_files (pi
, FD_CTL
))
4817 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4821 open_procinfo_files // done
4824 procfs_notice_signals
4831 /* If not stopped yet, wait for it to stop. */
4832 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4833 !(proc_wait_for_stop (pi
)))
4834 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4836 /* Save some of the /proc state to be restored if we detach. */
4837 /* FIXME: Why? In case another debugger was debugging it?
4838 We're it's parent, for Ghu's sake! */
4839 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4840 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4841 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4842 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4843 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4844 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4845 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4846 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4847 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4848 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4850 /* Register to trace selected signals in the child. */
4851 prfillset (&signals
);
4852 if (!register_gdb_signals (pi
, &signals
))
4853 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4855 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4856 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4858 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4859 and possibly even turning ON kill-on-last-close at this point. But
4860 I can't make that change without careful testing which I don't have
4861 time to do right now... */
4862 /* Turn on run-on-last-close flag so that the child
4863 will die if GDB goes away for some reason. */
4864 if (!proc_set_run_on_last_close (pi
))
4865 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4867 /* The 'process ID' we return to GDB is composed of
4868 the actual process ID plus the lwp ID. */
4869 inferior_ptid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4871 #ifdef START_INFERIOR_TRAPS_EXPECTED
4872 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4874 /* One trap to exec the shell, one to exec the program being debugged. */
4875 startup_inferior (2);
4876 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4880 * Function: set_exec_trap
4882 * When GDB forks to create a new process, this function is called
4883 * on the child side of the fork before GDB exec's the user program.
4884 * Its job is to make the child minimally debuggable, so that the
4885 * parent GDB process can connect to the child and take over.
4886 * This function should do only the minimum to make that possible,
4887 * and to synchronize with the parent process. The parent process
4888 * should take care of the details.
4892 procfs_set_exec_trap (void)
4894 /* This routine called on the child side (inferior side)
4895 after GDB forks the inferior. It must use only local variables,
4896 because it may be sharing data space with its parent. */
4901 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4902 perror_with_name ("procfs: create_procinfo failed in child.");
4904 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4906 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4907 gdb_flush (gdb_stderr
);
4908 /* no need to call "dead_procinfo", because we're going to exit. */
4912 #ifdef PRFS_STOPEXEC /* defined on OSF */
4913 /* OSF method for tracing exec syscalls. Quoting:
4914 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4915 exits from exec system calls because of the user level loader. */
4916 /* FIXME: make nice and maybe move into an access function. */
4920 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4922 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4923 gdb_flush (gdb_stderr
);
4926 prfs_flags
|= PRFS_STOPEXEC
;
4928 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4930 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4931 gdb_flush (gdb_stderr
);
4935 #else /* not PRFS_STOPEXEC */
4936 /* Everyone else's (except OSF) method for tracing exec syscalls */
4938 Not all systems with /proc have all the exec* syscalls with the same
4939 names. On the SGI, for example, there is no SYS_exec, but there
4940 *is* a SYS_execv. So, we try to account for that. */
4942 exitset
= sysset_t_alloc (pi
);
4943 gdb_premptysysset (exitset
);
4945 gdb_praddsysset (exitset
, SYS_exec
);
4948 gdb_praddsysset (exitset
, SYS_execve
);
4951 gdb_praddsysset (exitset
, SYS_execv
);
4953 #ifdef DYNAMIC_SYSCALLS
4955 int callnum
= find_syscall (pi
, "execve");
4958 gdb_praddsysset (exitset
, callnum
);
4960 callnum
= find_syscall (pi
, "ra_execve");
4962 gdb_praddsysset (exitset
, callnum
);
4964 #endif /* DYNAMIC_SYSCALLS */
4966 if (!proc_set_traced_sysexit (pi
, exitset
))
4968 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4969 gdb_flush (gdb_stderr
);
4972 #endif /* PRFS_STOPEXEC */
4974 /* FIXME: should this be done in the parent instead? */
4975 /* Turn off inherit on fork flag so that all grand-children
4976 of gdb start with tracing flags cleared. */
4977 if (!proc_unset_inherit_on_fork (pi
))
4978 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4980 /* Turn off run on last close flag, so that the child process
4981 cannot run away just because we close our handle on it.
4982 We want it to wait for the parent to attach. */
4983 if (!proc_unset_run_on_last_close (pi
))
4984 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4986 /* FIXME: No need to destroy the procinfo --
4987 we have our own address space, and we're about to do an exec! */
4988 /*destroy_procinfo (pi);*/
4992 * Function: create_inferior
4994 * This function is called BEFORE gdb forks the inferior process.
4995 * Its only real responsibility is to set things up for the fork,
4996 * and tell GDB which two functions to call after the fork (one
4997 * for the parent, and one for the child).
4999 * This function does a complicated search for a unix shell program,
5000 * which it then uses to parse arguments and environment variables
5001 * to be sent to the child. I wonder whether this code could not
5002 * be abstracted out and shared with other unix targets such as
5007 procfs_create_inferior (char *exec_file
, char *allargs
, char **env
)
5009 char *shell_file
= getenv ("SHELL");
5011 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
5014 /* We will be looking down the PATH to find shell_file. If we
5015 just do this the normal way (via execlp, which operates by
5016 attempting an exec for each element of the PATH until it
5017 finds one which succeeds), then there will be an exec for
5018 each failed attempt, each of which will cause a PR_SYSEXIT
5019 stop, and we won't know how to distinguish the PR_SYSEXIT's
5020 for these failed execs with the ones for successful execs
5021 (whether the exec has succeeded is stored at that time in the
5022 carry bit or some such architecture-specific and
5023 non-ABI-specified place).
5025 So I can't think of anything better than to search the PATH
5026 now. This has several disadvantages: (1) There is a race
5027 condition; if we find a file now and it is deleted before we
5028 exec it, we lose, even if the deletion leaves a valid file
5029 further down in the PATH, (2) there is no way to know exactly
5030 what an executable (in the sense of "capable of being
5031 exec'd") file is. Using access() loses because it may lose
5032 if the caller is the superuser; failing to use it loses if
5033 there are ACLs or some such. */
5037 /* FIXME-maybe: might want "set path" command so user can change what
5038 path is used from within GDB. */
5039 char *path
= getenv ("PATH");
5041 struct stat statbuf
;
5044 path
= "/bin:/usr/bin";
5046 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
5047 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
5049 p1
= strchr (p
, ':');
5054 strncpy (tryname
, p
, len
);
5055 tryname
[len
] = '\0';
5056 strcat (tryname
, "/");
5057 strcat (tryname
, shell_file
);
5058 if (access (tryname
, X_OK
) < 0)
5060 if (stat (tryname
, &statbuf
) < 0)
5062 if (!S_ISREG (statbuf
.st_mode
))
5063 /* We certainly need to reject directories. I'm not quite
5064 as sure about FIFOs, sockets, etc., but I kind of doubt
5065 that people want to exec() these things. */
5070 /* Not found. This must be an error rather than merely passing
5071 the file to execlp(), because execlp() would try all the
5072 exec()s, causing GDB to get confused. */
5073 error ("procfs:%d -- Can't find shell %s in PATH",
5074 __LINE__
, shell_file
);
5076 shell_file
= tryname
;
5079 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
5080 procfs_init_inferior
, NULL
, shell_file
);
5082 /* We are at the first instruction we care about. */
5083 /* Pedal to the metal... */
5085 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
5089 * Function: notice_thread
5091 * Callback for find_new_threads.
5092 * Calls "add_thread".
5096 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
5098 ptid_t gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
5100 if (!in_thread_list (gdb_threadid
))
5101 add_thread (gdb_threadid
);
5107 * Function: target_find_new_threads
5109 * Query all the threads that the target knows about,
5110 * and give them back to GDB to add to its list.
5114 procfs_find_new_threads (void)
5118 /* Find procinfo for main process */
5119 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5120 proc_update_threads (pi
);
5121 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
5125 * Function: target_thread_alive
5127 * Return true if the thread is still 'alive'.
5129 * This guy doesn't really seem to be doing his job.
5130 * Got to investigate how to tell when a thread is really gone.
5134 procfs_thread_alive (ptid_t ptid
)
5139 proc
= PIDGET (ptid
);
5140 thread
= TIDGET (ptid
);
5141 /* If I don't know it, it ain't alive! */
5142 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
5145 /* If I can't get its status, it ain't alive!
5146 What's more, I need to forget about it! */
5147 if (!proc_get_status (pi
))
5149 destroy_procinfo (pi
);
5152 /* I couldn't have got its status if it weren't alive, so it's alive. */
5157 * Function: target_pid_to_str
5159 * Return a string to be used to identify the thread in
5160 * the "info threads" display.
5164 procfs_pid_to_str (ptid_t ptid
)
5166 static char buf
[80];
5170 proc
= PIDGET (ptid
);
5171 thread
= TIDGET (ptid
);
5172 pi
= find_procinfo (proc
, thread
);
5175 sprintf (buf
, "Process %d", proc
);
5177 sprintf (buf
, "LWP %d", thread
);
5182 * Function: procfs_set_watchpoint
5183 * Insert a watchpoint
5187 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
5195 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5196 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5198 /* Translate from GDB's flags to /proc's */
5199 if (len
> 0) /* len == 0 means delete watchpoint */
5201 switch (rwflag
) { /* FIXME: need an enum! */
5202 case hw_write
: /* default watchpoint (write) */
5203 pflags
= WRITE_WATCHFLAG
;
5205 case hw_read
: /* read watchpoint */
5206 pflags
= READ_WATCHFLAG
;
5208 case hw_access
: /* access watchpoint */
5209 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
5211 case hw_execute
: /* execution HW breakpoint */
5212 pflags
= EXEC_WATCHFLAG
;
5214 default: /* Something weird. Return error. */
5217 if (after
) /* Stop after r/w access is completed. */
5218 pflags
|= AFTER_WATCHFLAG
;
5221 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
5223 if (errno
== E2BIG
) /* Typical error for no resources */
5224 return -1; /* fail */
5225 /* GDB may try to remove the same watchpoint twice.
5226 If a remove request returns no match, don't error. */
5227 if (errno
== ESRCH
&& len
== 0)
5228 return 0; /* ignore */
5229 proc_error (pi
, "set_watchpoint", __LINE__
);
5232 #endif /* UNIXWARE */
5237 * Function: stopped_by_watchpoint
5239 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5240 * else returns zero.
5244 procfs_stopped_by_watchpoint (ptid_t ptid
)
5248 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5249 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5251 if (!pi
) /* If no process, then not stopped by watchpoint! */
5254 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
5256 if (proc_why (pi
) == PR_FAULTED
)
5259 if (proc_what (pi
) == FLTWATCH
)
5263 if (proc_what (pi
) == FLTKWATCH
)
5271 #ifdef TM_I386SOL2_H
5273 * Function: procfs_find_LDT_entry
5276 * ptid_t ptid; // The GDB-style pid-plus-LWP.
5279 * pointer to the corresponding LDT entry.
5283 procfs_find_LDT_entry (ptid_t ptid
)
5285 gdb_gregset_t
*gregs
;
5289 /* Find procinfo for the lwp. */
5290 if ((pi
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
))) == NULL
)
5292 warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.",
5293 PIDGET (ptid
), TIDGET (ptid
));
5296 /* get its general registers. */
5297 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
5299 warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.",
5300 PIDGET (ptid
), TIDGET (ptid
));
5303 /* Now extract the GS register's lower 16 bits. */
5304 key
= (*gregs
)[GS
] & 0xffff;
5306 /* Find the matching entry and return it. */
5307 return proc_get_LDT_entry (pi
, key
);
5309 #endif /* TM_I386SOL2_H */
5314 info_proc_cmd (char *args
, int from_tty
)
5316 struct cleanup
*old_chain
;
5317 procinfo
*process
= NULL
;
5318 procinfo
*thread
= NULL
;
5324 old_chain
= make_cleanup (null_cleanup
, 0);
5327 if ((argv
= buildargv (args
)) == NULL
)
5330 make_cleanup_freeargv (argv
);
5332 while (argv
!= NULL
&& *argv
!= NULL
)
5334 if (isdigit (argv
[0][0]))
5336 pid
= strtoul (argv
[0], &tmp
, 10);
5338 tid
= strtoul (++tmp
, NULL
, 10);
5340 else if (argv
[0][0] == '/')
5342 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5351 pid
= PIDGET (inferior_ptid
);
5353 error ("No current process: you must name one.");
5356 /* Have pid, will travel.
5357 First see if it's a process we're already debugging. */
5358 process
= find_procinfo (pid
, 0);
5359 if (process
== NULL
)
5361 /* No. So open a procinfo for it, but
5362 remember to close it again when finished. */
5363 process
= create_procinfo (pid
, 0);
5364 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5365 if (!open_procinfo_files (process
, FD_CTL
))
5366 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5370 thread
= create_procinfo (pid
, tid
);
5374 printf_filtered ("process %d flags:\n", process
->pid
);
5375 proc_prettyprint_flags (proc_flags (process
), 1);
5376 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5377 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5378 if (proc_get_nthreads (process
) > 1)
5379 printf_filtered ("Process has %d threads.\n",
5380 proc_get_nthreads (process
));
5384 printf_filtered ("thread %d flags:\n", thread
->tid
);
5385 proc_prettyprint_flags (proc_flags (thread
), 1);
5386 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5387 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5390 do_cleanups (old_chain
);
5394 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5400 if (PIDGET (inferior_ptid
) <= 0)
5401 error ("you must be debugging a process to use this command.");
5403 if (args
== NULL
|| args
[0] == 0)
5404 error_no_arg ("system call to trace");
5406 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5407 if (isdigit (args
[0]))
5409 syscallnum
= atoi (args
);
5410 if (entry_or_exit
== PR_SYSENTRY
)
5411 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5413 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5416 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5418 if (mode
== FLAG_SET
)
5419 gdb_praddsysset (sysset
, syscallnum
);
5421 gdb_prdelsysset (sysset
, syscallnum
);
5423 if (entry_or_exit
== PR_SYSENTRY
)
5425 if (!proc_set_traced_sysentry (pi
, sysset
))
5426 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5430 if (!proc_set_traced_sysexit (pi
, sysset
))
5431 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5437 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5439 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5443 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5445 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5449 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5451 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5455 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5457 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5462 _initialize_procfs (void)
5465 add_target (&procfs_ops
);
5466 add_info ("proc", info_proc_cmd
,
5467 "Show /proc process information about any running process.\
5468 Default is the process being debugged.");
5469 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5470 "Give a trace of entries into the syscall.");
5471 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5472 "Give a trace of exits from the syscall.");
5473 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5474 "Cancel a trace of entries into the syscall.");
5475 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5476 "Cancel a trace of exits from the syscall.");
5479 /* =================== END, GDB "MODULE" =================== */
5483 /* miscelaneous stubs: */
5484 /* The following satisfy a few random symbols mostly created by */
5485 /* the solaris threads implementation, which I will chase down */
5489 * Return a pid for which we guarantee
5490 * we will be able to find a 'live' procinfo.
5494 procfs_first_available (void)
5496 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);