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 pi
->saved_entryset
= sysset_t_alloc (pi
);
678 pi
->saved_exitset
= sysset_t_alloc (pi
);
680 /* Chain into list. */
683 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
684 pi
->next
= procinfo_list
;
690 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
692 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
694 pi
->next
= parent
->thread_list
;
695 parent
->thread_list
= pi
;
701 * Function: close_procinfo_files
703 * Close all file descriptors associated with the procinfo
707 close_procinfo_files (procinfo
*pi
)
714 if (pi
->status_fd
> 0)
715 close (pi
->status_fd
);
717 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
721 * Function: destroy_procinfo
723 * Destructor function. Close, unlink and deallocate the object.
727 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
731 /* Step one: unlink the procinfo from its list */
735 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
738 ptr
->next
= pi
->next
;
742 /* Step two: close any open file descriptors */
743 close_procinfo_files (pi
);
745 /* Step three: free the memory. */
746 #ifdef DYNAMIC_SYSCALLS
749 xfree (pi
->saved_entryset
);
750 xfree (pi
->saved_exitset
);
755 destroy_procinfo (procinfo
*pi
)
759 if (pi
->tid
!= 0) /* destroy a thread procinfo */
761 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
762 destroy_one_procinfo (&tmp
->thread_list
, pi
);
764 else /* destroy a process procinfo and all its threads */
766 /* First destroy the children, if any; */
767 while (pi
->thread_list
!= NULL
)
768 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
769 /* Then destroy the parent. Genocide!!! */
770 destroy_one_procinfo (&procinfo_list
, pi
);
775 do_destroy_procinfo_cleanup (void *pi
)
777 destroy_procinfo (pi
);
780 enum { NOKILL
, KILL
};
783 * Function: dead_procinfo
785 * To be called on a non_recoverable error for a procinfo.
786 * Prints error messages, optionally sends a SIGKILL to the process,
787 * then destroys the data structure.
791 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
797 print_sys_errmsg (pi
->pathname
, errno
);
801 sprintf (procfile
, "process %d", pi
->pid
);
802 print_sys_errmsg (procfile
, errno
);
805 kill (pi
->pid
, SIGKILL
);
807 destroy_procinfo (pi
);
812 * Function: sysset_t_size
814 * Returns the (complete) size of a sysset_t struct. Normally, this
815 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
816 * size of sysset_t isn't known until runtime.
820 sysset_t_size (procinfo
* pi
)
822 #ifndef DYNAMIC_SYSCALLS
823 return sizeof (sysset_t
);
825 return sizeof (sysset_t
) - sizeof (uint64_t)
826 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
827 / (8 * sizeof (uint64_t)));
831 /* Function: sysset_t_alloc
833 Allocate and (partially) initialize a sysset_t struct. */
836 sysset_t_alloc (procinfo
* pi
)
839 int size
= sysset_t_size (pi
);
840 ret
= xmalloc (size
);
841 #ifdef DYNAMIC_SYSCALLS
842 ret
->pr_size
= (pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
843 / (8 * sizeof (uint64_t));
848 #ifdef DYNAMIC_SYSCALLS
850 /* Function: load_syscalls
852 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
853 pi->num_syscalls with the number of syscalls and pi->syscall_names
854 with the names. (Certain numbers may be skipped in which case the
855 names for these numbers will be left as NULL.) */
857 #define MAX_SYSCALL_NAME_LENGTH 256
858 #define MAX_SYSCALLS 65536
861 load_syscalls (procinfo
*pi
)
863 char pathname
[MAX_PROC_NAME_SIZE
];
866 prsyscall_t
*syscalls
;
867 int i
, size
, maxcall
;
869 pi
->num_syscalls
= 0;
870 pi
->syscall_names
= 0;
872 /* Open the file descriptor for the sysent file */
873 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
874 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
877 error ("load_syscalls: Can't open /proc/%d/sysent", pi
->pid
);
880 size
= sizeof header
- sizeof (prsyscall_t
);
881 if (read (sysent_fd
, &header
, size
) != size
)
883 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
886 if (header
.pr_nsyscalls
== 0)
888 error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi
->pid
);
891 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
892 syscalls
= xmalloc (size
);
894 if (read (sysent_fd
, syscalls
, size
) != size
)
897 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
900 /* Find maximum syscall number. This may not be the same as
901 pr_nsyscalls since that value refers to the number of entries
902 in the table. (Also, the docs indicate that some system
903 call numbers may be skipped.) */
905 maxcall
= syscalls
[0].pr_number
;
907 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
908 if (syscalls
[i
].pr_number
> maxcall
909 && syscalls
[i
].pr_nameoff
> 0
910 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
911 maxcall
= syscalls
[i
].pr_number
;
913 pi
->num_syscalls
= maxcall
+1;
914 pi
->syscall_names
= xmalloc (pi
->num_syscalls
* sizeof (char *));
916 for (i
= 0; i
< pi
->num_syscalls
; i
++)
917 pi
->syscall_names
[i
] = NULL
;
919 /* Read the syscall names in */
920 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
922 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
926 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
927 || syscalls
[i
].pr_number
< 0
928 || syscalls
[i
].pr_nameoff
<= 0
929 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
930 != (off_t
) syscalls
[i
].pr_nameoff
))
933 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
937 callnum
= syscalls
[i
].pr_number
;
939 if (pi
->syscall_names
[callnum
] != NULL
)
941 /* FIXME: Generate warning */
945 namebuf
[nread
-1] = '\0';
946 size
= strlen (namebuf
) + 1;
947 pi
->syscall_names
[callnum
] = xmalloc (size
);
948 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
949 pi
->syscall_names
[callnum
][size
-1] = '\0';
956 /* Function: free_syscalls
958 Free the space allocated for the syscall names from the procinfo
962 free_syscalls (procinfo
*pi
)
964 if (pi
->syscall_names
)
968 for (i
= 0; i
< pi
->num_syscalls
; i
++)
969 if (pi
->syscall_names
[i
] != NULL
)
970 xfree (pi
->syscall_names
[i
]);
972 xfree (pi
->syscall_names
);
973 pi
->syscall_names
= 0;
977 /* Function: find_syscall
979 Given a name, look up (and return) the corresponding syscall number.
980 If no match is found, return -1. */
983 find_syscall (procinfo
*pi
, char *name
)
986 for (i
= 0; i
< pi
->num_syscalls
; i
++)
988 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
995 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
997 /* =================== /proc "MODULE" =================== */
1000 * This "module" is the interface layer between the /proc system API
1001 * and the gdb target vector functions. This layer consists of
1002 * access functions that encapsulate each of the basic operations
1003 * that we need to use from the /proc API.
1005 * The main motivation for this layer is to hide the fact that
1006 * there are two very different implementations of the /proc API.
1007 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1008 * functions, we do our best to hide them all in here.
1011 int proc_get_status (procinfo
* pi
);
1012 long proc_flags (procinfo
* pi
);
1013 int proc_why (procinfo
* pi
);
1014 int proc_what (procinfo
* pi
);
1015 int proc_set_run_on_last_close (procinfo
* pi
);
1016 int proc_unset_run_on_last_close (procinfo
* pi
);
1017 int proc_set_inherit_on_fork (procinfo
* pi
);
1018 int proc_unset_inherit_on_fork (procinfo
* pi
);
1019 int proc_set_async (procinfo
* pi
);
1020 int proc_unset_async (procinfo
* pi
);
1021 int proc_stop_process (procinfo
* pi
);
1022 int proc_trace_signal (procinfo
* pi
, int signo
);
1023 int proc_ignore_signal (procinfo
* pi
, int signo
);
1024 int proc_clear_current_fault (procinfo
* pi
);
1025 int proc_set_current_signal (procinfo
* pi
, int signo
);
1026 int proc_clear_current_signal (procinfo
* pi
);
1027 int proc_set_gregs (procinfo
* pi
);
1028 int proc_set_fpregs (procinfo
* pi
);
1029 int proc_wait_for_stop (procinfo
* pi
);
1030 int proc_run_process (procinfo
* pi
, int step
, int signo
);
1031 int proc_kill (procinfo
* pi
, int signo
);
1032 int proc_parent_pid (procinfo
* pi
);
1033 int proc_get_nthreads (procinfo
* pi
);
1034 int proc_get_current_thread (procinfo
* pi
);
1035 int proc_set_held_signals (procinfo
* pi
, gdb_sigset_t
* sighold
);
1036 int proc_set_traced_sysexit (procinfo
* pi
, sysset_t
* sysset
);
1037 int proc_set_traced_sysentry (procinfo
* pi
, sysset_t
* sysset
);
1038 int proc_set_traced_faults (procinfo
* pi
, fltset_t
* fltset
);
1039 int proc_set_traced_signals (procinfo
* pi
, gdb_sigset_t
* sigset
);
1041 int proc_update_threads (procinfo
* pi
);
1042 int proc_iterate_over_threads (procinfo
* pi
,
1043 int (*func
) (procinfo
*, procinfo
*, void *),
1046 gdb_gregset_t
*proc_get_gregs (procinfo
* pi
);
1047 gdb_fpregset_t
*proc_get_fpregs (procinfo
* pi
);
1048 sysset_t
*proc_get_traced_sysexit (procinfo
* pi
, sysset_t
* save
);
1049 sysset_t
*proc_get_traced_sysentry (procinfo
* pi
, sysset_t
* save
);
1050 fltset_t
*proc_get_traced_faults (procinfo
* pi
, fltset_t
* save
);
1051 gdb_sigset_t
*proc_get_traced_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1052 gdb_sigset_t
*proc_get_held_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1053 gdb_sigset_t
*proc_get_pending_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1054 gdb_sigaction_t
*proc_get_signal_actions (procinfo
* pi
, gdb_sigaction_t
*save
);
1056 void proc_warn (procinfo
* pi
, char *func
, int line
);
1057 void proc_error (procinfo
* pi
, char *func
, int line
);
1060 proc_warn (procinfo
*pi
, char *func
, int line
)
1062 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1063 print_sys_errmsg (errmsg
, errno
);
1067 proc_error (procinfo
*pi
, char *func
, int line
)
1069 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1070 perror_with_name (errmsg
);
1074 * Function: proc_get_status
1076 * Updates the status struct in the procinfo.
1077 * There is a 'valid' flag, to let other functions know when
1078 * this function needs to be called (so the status is only
1079 * read when it is needed). The status file descriptor is
1080 * also only opened when it is needed.
1082 * Return: non-zero for success, zero for failure.
1086 proc_get_status (procinfo
*pi
)
1088 /* Status file descriptor is opened "lazily" */
1089 if (pi
->status_fd
== 0 &&
1090 open_procinfo_files (pi
, FD_STATUS
) == 0)
1092 pi
->status_valid
= 0;
1097 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1098 pi
->status_valid
= 0; /* fail */
1101 /* Sigh... I have to read a different data structure,
1102 depending on whether this is a main process or an LWP. */
1104 pi
->status_valid
= (read (pi
->status_fd
,
1105 (char *) &pi
->prstatus
.pr_lwp
,
1106 sizeof (lwpstatus_t
))
1107 == sizeof (lwpstatus_t
));
1110 pi
->status_valid
= (read (pi
->status_fd
,
1111 (char *) &pi
->prstatus
,
1112 sizeof (gdb_prstatus_t
))
1113 == sizeof (gdb_prstatus_t
));
1114 #if 0 /*def UNIXWARE*/
1115 if (pi
->status_valid
&&
1116 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
1117 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
1118 /* Unixware peculiarity -- read the damn thing again! */
1119 pi
->status_valid
= (read (pi
->status_fd
,
1120 (char *) &pi
->prstatus
,
1121 sizeof (gdb_prstatus_t
))
1122 == sizeof (gdb_prstatus_t
));
1123 #endif /* UNIXWARE */
1126 #else /* ioctl method */
1127 #ifdef PIOCTSTATUS /* osf */
1128 if (pi
->tid
== 0) /* main process */
1130 /* Just read the danged status. Now isn't that simple? */
1132 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1139 tid_t pr_error_thread
;
1140 struct prstatus status
;
1143 thread_status
.pr_count
= 1;
1144 thread_status
.status
.pr_tid
= pi
->tid
;
1145 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1148 memcpy (&pi
->prstatus
, &thread_status
.status
,
1149 sizeof (pi
->prstatus
));
1150 pi
->status_valid
= 1;
1154 /* Just read the danged status. Now isn't that simple? */
1155 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1159 if (pi
->status_valid
)
1161 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1164 proc_get_current_thread (pi
));
1167 /* The status struct includes general regs, so mark them valid too */
1168 pi
->gregs_valid
= pi
->status_valid
;
1170 /* In the read/write multiple-fd model,
1171 the status struct includes the fp regs too, so mark them valid too */
1172 pi
->fpregs_valid
= pi
->status_valid
;
1174 return pi
->status_valid
; /* True if success, false if failure. */
1178 * Function: proc_flags
1180 * returns the process flags (pr_flags field).
1184 proc_flags (procinfo
*pi
)
1186 if (!pi
->status_valid
)
1187 if (!proc_get_status (pi
))
1188 return 0; /* FIXME: not a good failure value (but what is?) */
1192 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1193 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1194 The two sets of flags don't overlap. */
1195 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
1197 return pi
->prstatus
.pr_lwp
.pr_flags
;
1200 return pi
->prstatus
.pr_flags
;
1205 * Function: proc_why
1207 * returns the pr_why field (why the process stopped).
1211 proc_why (procinfo
*pi
)
1213 if (!pi
->status_valid
)
1214 if (!proc_get_status (pi
))
1215 return 0; /* FIXME: not a good failure value (but what is?) */
1218 return pi
->prstatus
.pr_lwp
.pr_why
;
1220 return pi
->prstatus
.pr_why
;
1225 * Function: proc_what
1227 * returns the pr_what field (details of why the process stopped).
1231 proc_what (procinfo
*pi
)
1233 if (!pi
->status_valid
)
1234 if (!proc_get_status (pi
))
1235 return 0; /* FIXME: not a good failure value (but what is?) */
1238 return pi
->prstatus
.pr_lwp
.pr_what
;
1240 return pi
->prstatus
.pr_what
;
1244 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1246 * Function: proc_nsysarg
1248 * returns the pr_nsysarg field (number of args to the current syscall).
1252 proc_nsysarg (procinfo
*pi
)
1254 if (!pi
->status_valid
)
1255 if (!proc_get_status (pi
))
1259 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1261 return pi
->prstatus
.pr_nsysarg
;
1266 * Function: proc_sysargs
1268 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1272 proc_sysargs (procinfo
*pi
)
1274 if (!pi
->status_valid
)
1275 if (!proc_get_status (pi
))
1279 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1281 return (long *) &pi
->prstatus
.pr_sysarg
;
1286 * Function: proc_syscall
1288 * returns the pr_syscall field (id of current syscall if we are in one).
1292 proc_syscall (procinfo
*pi
)
1294 if (!pi
->status_valid
)
1295 if (!proc_get_status (pi
))
1299 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1301 return pi
->prstatus
.pr_syscall
;
1304 #endif /* PIOCSSPCACT */
1307 * Function: proc_cursig:
1309 * returns the pr_cursig field (current signal).
1313 proc_cursig (struct procinfo
*pi
)
1315 if (!pi
->status_valid
)
1316 if (!proc_get_status (pi
))
1317 return 0; /* FIXME: not a good failure value (but what is?) */
1320 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1322 return pi
->prstatus
.pr_cursig
;
1327 * Function: proc_modify_flag
1329 * === I appologize for the messiness of this function.
1330 * === This is an area where the different versions of
1331 * === /proc are more inconsistent than usual. MVS
1333 * Set or reset any of the following process flags:
1334 * PR_FORK -- forked child will inherit trace flags
1335 * PR_RLC -- traced process runs when last /proc file closed.
1336 * PR_KLC -- traced process is killed when last /proc file closed.
1337 * PR_ASYNC -- LWP's get to run/stop independently.
1339 * There are three methods for doing this function:
1340 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1342 * 2) Middle: PIOCSET/PIOCRESET
1344 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1347 * Note: Irix does not define PR_ASYNC.
1348 * Note: OSF does not define PR_KLC.
1349 * Note: OSF is the only one that can ONLY use the oldest method.
1352 * pi -- the procinfo
1353 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1354 * mode -- 1 for set, 0 for reset.
1356 * Returns non-zero for success, zero for failure.
1359 enum { FLAG_RESET
, FLAG_SET
};
1362 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1364 long win
= 0; /* default to fail */
1367 * These operations affect the process as a whole, and applying
1368 * them to an individual LWP has the same meaning as applying them
1369 * to the main process. Therefore, if we're ever called with a
1370 * pointer to an LWP's procinfo, let's substitute the process's
1371 * procinfo and avoid opening the LWP's file descriptor
1376 pi
= find_procinfo_or_die (pi
->pid
, 0);
1378 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1379 /* First normalize the PCUNSET/PCRESET command opcode
1380 (which for no obvious reason has a different definition
1381 from one operating system to the next...) */
1383 #define GDBRESET PCUNSET
1386 #define GDBRESET PCRESET
1390 procfs_ctl_t arg
[2];
1392 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1394 else /* Reset the flag */
1398 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1401 #ifdef PIOCSET /* Irix/Sol5 method */
1402 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1404 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1406 else /* Reset the flag */
1408 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1412 #ifdef PIOCSRLC /* Oldest method: OSF */
1415 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1417 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1419 else /* Clear run-on-last-close */
1421 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1425 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1427 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1429 else /* Clear inherit-on-fork */
1431 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1435 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1442 /* The above operation renders the procinfo's cached pstatus obsolete. */
1443 pi
->status_valid
= 0;
1446 warning ("procfs: modify_flag failed to turn %s %s",
1447 flag
== PR_FORK
? "PR_FORK" :
1448 flag
== PR_RLC
? "PR_RLC" :
1450 flag
== PR_ASYNC
? "PR_ASYNC" :
1453 flag
== PR_KLC
? "PR_KLC" :
1456 mode
== FLAG_RESET
? "off" : "on");
1462 * Function: proc_set_run_on_last_close
1464 * Set the run_on_last_close flag.
1465 * Process with all threads will become runnable
1466 * when debugger closes all /proc fds.
1468 * Returns non-zero for success, zero for failure.
1472 proc_set_run_on_last_close (procinfo
*pi
)
1474 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1478 * Function: proc_unset_run_on_last_close
1480 * Reset the run_on_last_close flag.
1481 * Process will NOT become runnable
1482 * when debugger closes its file handles.
1484 * Returns non-zero for success, zero for failure.
1488 proc_unset_run_on_last_close (procinfo
*pi
)
1490 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1495 * Function: proc_set_kill_on_last_close
1497 * Set the kill_on_last_close flag.
1498 * Process with all threads will be killed when debugger
1499 * closes all /proc fds (or debugger exits or dies).
1501 * Returns non-zero for success, zero for failure.
1505 proc_set_kill_on_last_close (procinfo
*pi
)
1507 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1511 * Function: proc_unset_kill_on_last_close
1513 * Reset the kill_on_last_close flag.
1514 * Process will NOT be killed when debugger
1515 * closes its file handles (or exits or dies).
1517 * Returns non-zero for success, zero for failure.
1521 proc_unset_kill_on_last_close (procinfo
*pi
)
1523 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1528 * Function: proc_set_inherit_on_fork
1530 * Set inherit_on_fork flag.
1531 * If the process forks a child while we are registered for events
1532 * in the parent, then we will also recieve events from the child.
1534 * Returns non-zero for success, zero for failure.
1538 proc_set_inherit_on_fork (procinfo
*pi
)
1540 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1544 * Function: proc_unset_inherit_on_fork
1546 * Reset inherit_on_fork flag.
1547 * If the process forks a child while we are registered for events
1548 * in the parent, then we will NOT recieve events from the child.
1550 * Returns non-zero for success, zero for failure.
1554 proc_unset_inherit_on_fork (procinfo
*pi
)
1556 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1561 * Function: proc_set_async
1563 * Set PR_ASYNC flag.
1564 * If one LWP stops because of a debug event (signal etc.),
1565 * the remaining LWPs will continue to run.
1567 * Returns non-zero for success, zero for failure.
1571 proc_set_async (procinfo
*pi
)
1573 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1577 * Function: proc_unset_async
1579 * Reset PR_ASYNC flag.
1580 * If one LWP stops because of a debug event (signal etc.),
1581 * then all other LWPs will stop as well.
1583 * Returns non-zero for success, zero for failure.
1587 proc_unset_async (procinfo
*pi
)
1589 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1591 #endif /* PR_ASYNC */
1594 * Function: proc_stop_process
1596 * Request the process/LWP to stop. Does not wait.
1597 * Returns non-zero for success, zero for failure.
1601 proc_stop_process (procinfo
*pi
)
1606 * We might conceivably apply this operation to an LWP, and
1607 * the LWP's ctl file descriptor might not be open.
1610 if (pi
->ctl_fd
== 0 &&
1611 open_procinfo_files (pi
, FD_CTL
) == 0)
1616 procfs_ctl_t cmd
= PCSTOP
;
1617 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1618 #else /* ioctl method */
1619 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1620 /* Note: the call also reads the prstatus. */
1623 pi
->status_valid
= 1;
1624 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1627 proc_get_current_thread (pi
));
1636 * Function: proc_wait_for_stop
1638 * Wait for the process or LWP to stop (block until it does).
1639 * Returns non-zero for success, zero for failure.
1643 proc_wait_for_stop (procinfo
*pi
)
1648 * We should never have to apply this operation to any procinfo
1649 * except the one for the main process. If that ever changes
1650 * for any reason, then take out the following clause and
1651 * replace it with one that makes sure the ctl_fd is open.
1655 pi
= find_procinfo_or_die (pi
->pid
, 0);
1659 procfs_ctl_t cmd
= PCWSTOP
;
1660 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1661 /* We been runnin' and we stopped -- need to update status. */
1662 pi
->status_valid
= 0;
1664 #else /* ioctl method */
1665 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1666 /* Above call also refreshes the prstatus. */
1669 pi
->status_valid
= 1;
1670 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1673 proc_get_current_thread (pi
));
1681 * Function: proc_run_process
1683 * Make the process or LWP runnable.
1684 * Options (not all are implemented):
1686 * - clear current fault
1687 * - clear current signal
1688 * - abort the current system call
1689 * - stop as soon as finished with system call
1690 * - (ioctl): set traced signal set
1691 * - (ioctl): set held signal set
1692 * - (ioctl): set traced fault set
1693 * - (ioctl): set start pc (vaddr)
1694 * Always clear the current fault.
1695 * Clear the current signal if 'signo' is zero.
1698 * pi the process or LWP to operate on.
1699 * step if true, set the process or LWP to trap after one instr.
1700 * signo if zero, clear the current signal if any.
1701 * if non-zero, set the current signal to this one.
1703 * Returns non-zero for success, zero for failure.
1707 proc_run_process (procinfo
*pi
, int step
, int signo
)
1713 * We will probably have to apply this operation to individual threads,
1714 * so make sure the control file descriptor is open.
1717 if (pi
->ctl_fd
== 0 &&
1718 open_procinfo_files (pi
, FD_CTL
) == 0)
1723 runflags
= PRCFAULT
; /* always clear current fault */
1728 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1729 proc_set_current_signal (pi
, signo
);
1733 procfs_ctl_t cmd
[2];
1737 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1739 #else /* ioctl method */
1743 memset (&prrun
, 0, sizeof (prrun
));
1744 prrun
.pr_flags
= runflags
;
1745 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1753 * Function: proc_set_traced_signals
1755 * Register to trace signals in the process or LWP.
1756 * Returns non-zero for success, zero for failure.
1760 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1765 * We should never have to apply this operation to any procinfo
1766 * except the one for the main process. If that ever changes
1767 * for any reason, then take out the following clause and
1768 * replace it with one that makes sure the ctl_fd is open.
1772 pi
= find_procinfo_or_die (pi
->pid
, 0);
1778 /* Use char array to avoid alignment issues. */
1779 char sigset
[sizeof (gdb_sigset_t
)];
1783 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1785 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1787 #else /* ioctl method */
1788 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1790 /* The above operation renders the procinfo's cached pstatus obsolete. */
1791 pi
->status_valid
= 0;
1794 warning ("procfs: set_traced_signals failed");
1799 * Function: proc_set_traced_faults
1801 * Register to trace hardware faults in the process or LWP.
1802 * Returns non-zero for success, zero for failure.
1806 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1811 * We should never have to apply this operation to any procinfo
1812 * except the one for the main process. If that ever changes
1813 * for any reason, then take out the following clause and
1814 * replace it with one that makes sure the ctl_fd is open.
1818 pi
= find_procinfo_or_die (pi
->pid
, 0);
1824 /* Use char array to avoid alignment issues. */
1825 char fltset
[sizeof (fltset_t
)];
1829 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1831 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1833 #else /* ioctl method */
1834 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1836 /* The above operation renders the procinfo's cached pstatus obsolete. */
1837 pi
->status_valid
= 0;
1843 * Function: proc_set_traced_sysentry
1845 * Register to trace entry to system calls in the process or LWP.
1846 * Returns non-zero for success, zero for failure.
1850 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1855 * We should never have to apply this operation to any procinfo
1856 * except the one for the main process. If that ever changes
1857 * for any reason, then take out the following clause and
1858 * replace it with one that makes sure the ctl_fd is open.
1862 pi
= find_procinfo_or_die (pi
->pid
, 0);
1866 struct gdb_proc_ctl_pcsentry
{
1868 /* Use char array to avoid alignment issues. */
1869 char sysset
[sizeof (sysset_t
)];
1871 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1873 + sysset_t_size (pi
);
1875 argp
= xmalloc (argp_size
);
1877 argp
->cmd
= PCSENTRY
;
1878 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1880 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1883 #else /* ioctl method */
1884 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1886 /* The above operation renders the procinfo's cached pstatus obsolete. */
1887 pi
->status_valid
= 0;
1893 * Function: proc_set_traced_sysexit
1895 * Register to trace exit from system calls in the process or LWP.
1896 * Returns non-zero for success, zero for failure.
1900 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1905 * We should never have to apply this operation to any procinfo
1906 * except the one for the main process. If that ever changes
1907 * for any reason, then take out the following clause and
1908 * replace it with one that makes sure the ctl_fd is open.
1912 pi
= find_procinfo_or_die (pi
->pid
, 0);
1916 struct gdb_proc_ctl_pcsexit
{
1918 /* Use char array to avoid alignment issues. */
1919 char sysset
[sizeof (sysset_t
)];
1921 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1923 + sysset_t_size (pi
);
1925 argp
= xmalloc (argp_size
);
1927 argp
->cmd
= PCSEXIT
;
1928 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1930 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1933 #else /* ioctl method */
1934 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1936 /* The above operation renders the procinfo's cached pstatus obsolete. */
1937 pi
->status_valid
= 0;
1943 * Function: proc_set_held_signals
1945 * Specify the set of blocked / held signals in the process or LWP.
1946 * Returns non-zero for success, zero for failure.
1950 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1955 * We should never have to apply this operation to any procinfo
1956 * except the one for the main process. If that ever changes
1957 * for any reason, then take out the following clause and
1958 * replace it with one that makes sure the ctl_fd is open.
1962 pi
= find_procinfo_or_die (pi
->pid
, 0);
1968 /* Use char array to avoid alignment issues. */
1969 char hold
[sizeof (gdb_sigset_t
)];
1973 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1974 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1977 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1979 /* The above operation renders the procinfo's cached pstatus obsolete. */
1980 pi
->status_valid
= 0;
1986 * Function: proc_get_pending_signals
1988 * returns the set of signals that are pending in the process or LWP.
1989 * Will also copy the sigset if 'save' is non-zero.
1993 proc_get_pending_signals (procinfo
*pi
, gdb_sigset_t
*save
)
1995 gdb_sigset_t
*ret
= NULL
;
1998 * We should never have to apply this operation to any procinfo
1999 * except the one for the main process. If that ever changes
2000 * for any reason, then take out the following clause and
2001 * replace it with one that makes sure the ctl_fd is open.
2005 pi
= find_procinfo_or_die (pi
->pid
, 0);
2007 if (!pi
->status_valid
)
2008 if (!proc_get_status (pi
))
2012 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
2014 ret
= &pi
->prstatus
.pr_sigpend
;
2017 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2023 * Function: proc_get_signal_actions
2025 * returns the set of signal actions.
2026 * Will also copy the sigactionset if 'save' is non-zero.
2030 proc_get_signal_actions (procinfo
*pi
, gdb_sigaction_t
*save
)
2032 gdb_sigaction_t
*ret
= NULL
;
2035 * We should never have to apply this operation to any procinfo
2036 * except the one for the main process. If that ever changes
2037 * for any reason, then take out the following clause and
2038 * replace it with one that makes sure the ctl_fd is open.
2042 pi
= find_procinfo_or_die (pi
->pid
, 0);
2044 if (!pi
->status_valid
)
2045 if (!proc_get_status (pi
))
2049 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
2051 ret
= &pi
->prstatus
.pr_action
;
2054 memcpy (save
, ret
, sizeof (gdb_sigaction_t
));
2060 * Function: proc_get_held_signals
2062 * returns the set of signals that are held / blocked.
2063 * Will also copy the sigset if 'save' is non-zero.
2067 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2069 gdb_sigset_t
*ret
= NULL
;
2072 * We should never have to apply this operation to any procinfo
2073 * except the one for the main process. If that ever changes
2074 * for any reason, then take out the following clause and
2075 * replace it with one that makes sure the ctl_fd is open.
2079 pi
= find_procinfo_or_die (pi
->pid
, 0);
2082 if (!pi
->status_valid
)
2083 if (!proc_get_status (pi
))
2087 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
2089 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
2090 #endif /* UNIXWARE */
2091 #else /* not NEW_PROC_API */
2093 static gdb_sigset_t sigheld
;
2095 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
2098 #endif /* NEW_PROC_API */
2100 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2106 * Function: proc_get_traced_signals
2108 * returns the set of signals that are traced / debugged.
2109 * Will also copy the sigset if 'save' is non-zero.
2113 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2115 gdb_sigset_t
*ret
= NULL
;
2118 * We should never have to apply this operation to any procinfo
2119 * except the one for the main process. If that ever changes
2120 * for any reason, then take out the following clause and
2121 * replace it with one that makes sure the ctl_fd is open.
2125 pi
= find_procinfo_or_die (pi
->pid
, 0);
2128 if (!pi
->status_valid
)
2129 if (!proc_get_status (pi
))
2132 ret
= &pi
->prstatus
.pr_sigtrace
;
2135 static gdb_sigset_t sigtrace
;
2137 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
2142 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2148 * Function: proc_trace_signal
2150 * Add 'signo' to the set of signals that are traced.
2151 * Returns non-zero for success, zero for failure.
2155 proc_trace_signal (procinfo
*pi
, int signo
)
2160 * We should never have to apply this operation to any procinfo
2161 * except the one for the main process. If that ever changes
2162 * for any reason, then take out the following clause and
2163 * replace it with one that makes sure the ctl_fd is open.
2167 pi
= find_procinfo_or_die (pi
->pid
, 0);
2171 if (proc_get_traced_signals (pi
, &temp
))
2173 praddset (&temp
, signo
);
2174 return proc_set_traced_signals (pi
, &temp
);
2178 return 0; /* failure */
2182 * Function: proc_ignore_signal
2184 * Remove 'signo' from the set of signals that are traced.
2185 * Returns non-zero for success, zero for failure.
2189 proc_ignore_signal (procinfo
*pi
, int signo
)
2194 * We should never have to apply this operation to any procinfo
2195 * except the one for the main process. If that ever changes
2196 * for any reason, then take out the following clause and
2197 * replace it with one that makes sure the ctl_fd is open.
2201 pi
= find_procinfo_or_die (pi
->pid
, 0);
2205 if (proc_get_traced_signals (pi
, &temp
))
2207 prdelset (&temp
, signo
);
2208 return proc_set_traced_signals (pi
, &temp
);
2212 return 0; /* failure */
2216 * Function: proc_get_traced_faults
2218 * returns the set of hardware faults that are traced /debugged.
2219 * Will also copy the faultset if 'save' is non-zero.
2223 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
2225 fltset_t
*ret
= NULL
;
2228 * We should never have to apply this operation to any procinfo
2229 * except the one for the main process. If that ever changes
2230 * for any reason, then take out the following clause and
2231 * replace it with one that makes sure the ctl_fd is open.
2235 pi
= find_procinfo_or_die (pi
->pid
, 0);
2238 if (!pi
->status_valid
)
2239 if (!proc_get_status (pi
))
2242 ret
= &pi
->prstatus
.pr_flttrace
;
2245 static fltset_t flttrace
;
2247 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2252 memcpy (save
, ret
, sizeof (fltset_t
));
2258 * Function: proc_get_traced_sysentry
2260 * returns the set of syscalls that are traced /debugged on entry.
2261 * Will also copy the syscall set if 'save' is non-zero.
2265 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
2267 sysset_t
*ret
= NULL
;
2270 * We should never have to apply this operation to any procinfo
2271 * except the one for the main process. If that ever changes
2272 * for any reason, then take out the following clause and
2273 * replace it with one that makes sure the ctl_fd is open.
2277 pi
= find_procinfo_or_die (pi
->pid
, 0);
2280 if (!pi
->status_valid
)
2281 if (!proc_get_status (pi
))
2284 #ifndef DYNAMIC_SYSCALLS
2285 ret
= &pi
->prstatus
.pr_sysentry
;
2286 #else /* DYNAMIC_SYSCALLS */
2288 static sysset_t
*sysentry
;
2292 sysentry
= sysset_t_alloc (pi
);
2294 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2296 if (pi
->prstatus
.pr_sysentry_offset
== 0)
2298 gdb_premptysysset (sysentry
);
2304 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
2306 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
2308 size
= sysset_t_size (pi
);
2309 gdb_premptysysset (sysentry
);
2310 rsize
= read (pi
->status_fd
, sysentry
, size
);
2315 #endif /* DYNAMIC_SYSCALLS */
2316 #else /* !NEW_PROC_API */
2318 static sysset_t sysentry
;
2320 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2323 #endif /* NEW_PROC_API */
2325 memcpy (save
, ret
, sysset_t_size (pi
));
2331 * Function: proc_get_traced_sysexit
2333 * returns the set of syscalls that are traced /debugged on exit.
2334 * Will also copy the syscall set if 'save' is non-zero.
2338 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
2340 sysset_t
* ret
= NULL
;
2343 * We should never have to apply this operation to any procinfo
2344 * except the one for the main process. If that ever changes
2345 * for any reason, then take out the following clause and
2346 * replace it with one that makes sure the ctl_fd is open.
2350 pi
= find_procinfo_or_die (pi
->pid
, 0);
2353 if (!pi
->status_valid
)
2354 if (!proc_get_status (pi
))
2357 #ifndef DYNAMIC_SYSCALLS
2358 ret
= &pi
->prstatus
.pr_sysexit
;
2359 #else /* DYNAMIC_SYSCALLS */
2361 static sysset_t
*sysexit
;
2365 sysexit
= sysset_t_alloc (pi
);
2367 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2369 if (pi
->prstatus
.pr_sysexit_offset
== 0)
2371 gdb_premptysysset (sysexit
);
2377 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
, SEEK_SET
)
2378 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2380 size
= sysset_t_size (pi
);
2381 gdb_premptysysset (sysexit
);
2382 rsize
= read (pi
->status_fd
, sysexit
, size
);
2387 #endif /* DYNAMIC_SYSCALLS */
2390 static sysset_t sysexit
;
2392 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2397 memcpy (save
, ret
, sysset_t_size (pi
));
2403 * Function: proc_clear_current_fault
2405 * The current fault (if any) is cleared; the associated signal
2406 * will not be sent to the process or LWP when it resumes.
2407 * Returns non-zero for success, zero for failure.
2411 proc_clear_current_fault (procinfo
*pi
)
2416 * We should never have to apply this operation to any procinfo
2417 * except the one for the main process. If that ever changes
2418 * for any reason, then take out the following clause and
2419 * replace it with one that makes sure the ctl_fd is open.
2423 pi
= find_procinfo_or_die (pi
->pid
, 0);
2427 procfs_ctl_t cmd
= PCCFAULT
;
2428 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2431 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2438 * Function: proc_set_current_signal
2440 * Set the "current signal" that will be delivered next to the process.
2441 * NOTE: semantics are different from those of KILL.
2442 * This signal will be delivered to the process or LWP
2443 * immediately when it is resumed (even if the signal is held/blocked);
2444 * it will NOT immediately cause another event of interest, and will NOT
2445 * first trap back to the debugger.
2447 * Returns non-zero for success, zero for failure.
2451 proc_set_current_signal (procinfo
*pi
, int signo
)
2456 /* Use char array to avoid alignment issues. */
2457 char sinfo
[sizeof (gdb_siginfo_t
)];
2459 gdb_siginfo_t
*mysinfo
;
2462 * We should never have to apply this operation to any procinfo
2463 * except the one for the main process. If that ever changes
2464 * for any reason, then take out the following clause and
2465 * replace it with one that makes sure the ctl_fd is open.
2469 pi
= find_procinfo_or_die (pi
->pid
, 0);
2471 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2472 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2473 * receives a PIOCSSIG with a signal identical to the current signal,
2474 * it messes up the current signal. Work around the kernel bug.
2477 signo
== proc_cursig (pi
))
2478 return 1; /* I assume this is a success? */
2481 /* The pointer is just a type alias. */
2482 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2483 mysinfo
->si_signo
= signo
;
2484 mysinfo
->si_code
= 0;
2485 mysinfo
->si_pid
= getpid (); /* ?why? */
2486 mysinfo
->si_uid
= getuid (); /* ?why? */
2490 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2492 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2499 * Function: proc_clear_current_signal
2501 * The current signal (if any) is cleared, and
2502 * is not sent to the process or LWP when it resumes.
2503 * Returns non-zero for success, zero for failure.
2507 proc_clear_current_signal (procinfo
*pi
)
2512 * We should never have to apply this operation to any procinfo
2513 * except the one for the main process. If that ever changes
2514 * for any reason, then take out the following clause and
2515 * replace it with one that makes sure the ctl_fd is open.
2519 pi
= find_procinfo_or_die (pi
->pid
, 0);
2525 /* Use char array to avoid alignment issues. */
2526 char sinfo
[sizeof (gdb_siginfo_t
)];
2528 gdb_siginfo_t
*mysinfo
;
2531 /* The pointer is just a type alias. */
2532 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2533 mysinfo
->si_signo
= 0;
2534 mysinfo
->si_code
= 0;
2535 mysinfo
->si_errno
= 0;
2536 mysinfo
->si_pid
= getpid (); /* ?why? */
2537 mysinfo
->si_uid
= getuid (); /* ?why? */
2539 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2542 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2549 * Function: proc_get_gregs
2551 * Get the general registers for the process or LWP.
2552 * Returns non-zero for success, zero for failure.
2556 proc_get_gregs (procinfo
*pi
)
2558 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2559 if (!proc_get_status (pi
))
2563 * OK, sorry about the ifdef's.
2564 * There's three cases instead of two, because
2565 * in this instance Unixware and Solaris/RW differ.
2569 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2570 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2571 #else /* not Unixware */
2572 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2573 #endif /* Unixware */
2574 #else /* not NEW_PROC_API */
2575 return &pi
->prstatus
.pr_reg
;
2576 #endif /* NEW_PROC_API */
2580 * Function: proc_get_fpregs
2582 * Get the floating point registers for the process or LWP.
2583 * Returns non-zero for success, zero for failure.
2587 proc_get_fpregs (procinfo
*pi
)
2590 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2591 if (!proc_get_status (pi
))
2594 #ifdef UNIXWARE /* a true architecture dependency */
2595 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2597 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2598 #endif /* Unixware */
2600 #else /* not NEW_PROC_API */
2601 if (pi
->fpregs_valid
)
2602 return &pi
->fpregset
; /* already got 'em */
2605 if (pi
->ctl_fd
== 0 &&
2606 open_procinfo_files (pi
, FD_CTL
) == 0)
2615 tid_t pr_error_thread
;
2616 tfpregset_t thread_1
;
2619 thread_fpregs
.pr_count
= 1;
2620 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2623 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2625 pi
->fpregs_valid
= 1;
2626 return &pi
->fpregset
; /* got 'em now! */
2628 else if (pi
->tid
!= 0 &&
2629 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2631 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2632 sizeof (pi
->fpregset
));
2633 pi
->fpregs_valid
= 1;
2634 return &pi
->fpregset
; /* got 'em now! */
2641 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2643 pi
->fpregs_valid
= 1;
2644 return &pi
->fpregset
; /* got 'em now! */
2657 * Function: proc_set_gregs
2659 * Write the general registers back to the process or LWP.
2660 * Returns non-zero for success, zero for failure.
2664 proc_set_gregs (procinfo
*pi
)
2666 gdb_gregset_t
*gregs
;
2669 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2670 return 0; /* get_regs has already warned */
2672 if (pi
->ctl_fd
== 0 &&
2673 open_procinfo_files (pi
, FD_CTL
) == 0)
2682 /* Use char array to avoid alignment issues. */
2683 char gregs
[sizeof (gdb_gregset_t
)];
2687 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2688 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2690 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2694 /* Policy: writing the regs invalidates our cache. */
2695 pi
->gregs_valid
= 0;
2700 * Function: proc_set_fpregs
2702 * Modify the floating point register set of the process or LWP.
2703 * Returns non-zero for success, zero for failure.
2707 proc_set_fpregs (procinfo
*pi
)
2709 gdb_fpregset_t
*fpregs
;
2712 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2713 return 0; /* get_fpregs has already warned */
2715 if (pi
->ctl_fd
== 0 &&
2716 open_procinfo_files (pi
, FD_CTL
) == 0)
2725 /* Use char array to avoid alignment issues. */
2726 char fpregs
[sizeof (gdb_fpregset_t
)];
2730 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2731 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2735 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2740 tid_t pr_error_thread
;
2741 tfpregset_t thread_1
;
2744 thread_fpregs
.pr_count
= 1;
2745 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2746 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2748 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2751 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2752 #endif /* osf PIOCTSFPREG */
2753 #endif /* NEW_PROC_API */
2756 /* Policy: writing the regs invalidates our cache. */
2757 pi
->fpregs_valid
= 0;
2762 * Function: proc_kill
2764 * Send a signal to the proc or lwp with the semantics of "kill()".
2765 * Returns non-zero for success, zero for failure.
2769 proc_kill (procinfo
*pi
, int signo
)
2774 * We might conceivably apply this operation to an LWP, and
2775 * the LWP's ctl file descriptor might not be open.
2778 if (pi
->ctl_fd
== 0 &&
2779 open_procinfo_files (pi
, FD_CTL
) == 0)
2786 procfs_ctl_t cmd
[2];
2790 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2791 #else /* ioctl method */
2792 /* FIXME: do I need the Alpha OSF fixups present in
2793 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2794 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2802 * Function: proc_parent_pid
2804 * Find the pid of the process that started this one.
2805 * Returns the parent process pid, or zero.
2809 proc_parent_pid (procinfo
*pi
)
2812 * We should never have to apply this operation to any procinfo
2813 * except the one for the main process. If that ever changes
2814 * for any reason, then take out the following clause and
2815 * replace it with one that makes sure the ctl_fd is open.
2819 pi
= find_procinfo_or_die (pi
->pid
, 0);
2821 if (!pi
->status_valid
)
2822 if (!proc_get_status (pi
))
2825 return pi
->prstatus
.pr_ppid
;
2830 * Function: proc_set_watchpoint
2835 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2837 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2840 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2841 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2846 char watch
[sizeof (prwatch_t
)];
2850 pwatch
= (prwatch_t
*) &arg
.watch
;
2851 pwatch
->pr_vaddr
= address_to_host_pointer (addr
);
2852 pwatch
->pr_size
= len
;
2853 pwatch
->pr_wflags
= wflags
;
2854 #if defined(NEW_PROC_API) && defined (PCWATCH)
2856 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2858 #if defined (PIOCSWATCH)
2859 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2861 return 0; /* Fail */
2869 * Function: proc_iterate_over_mappings
2871 * Given a pointer to a function, call that function once for every
2872 * mapped address space in the process. The callback function
2873 * receives an open file descriptor for the file corresponding to
2874 * that mapped address space (if there is one), and the base address
2875 * of the mapped space. Quit when the callback function returns a
2876 * nonzero value, or at teh end of the mappings.
2878 * Returns: the first non-zero return value of the callback function,
2882 /* FIXME: it's probably a waste to cache this FD.
2883 It doesn't get called that often... and if I open it
2884 every time, I don't need to lseek it. */
2886 proc_iterate_over_mappings (int (*func
) (int, CORE_ADDR
))
2890 #ifndef NEW_PROC_API /* avoid compiler warning */
2895 char pathname
[MAX_PROC_NAME_SIZE
];
2900 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
2904 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
2905 if ((map_fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2906 proc_error (pi
, "proc_iterate_over_mappings (open)", __LINE__
);
2908 /* Make sure it gets closed again. */
2909 make_cleanup_close (map_fd
);
2911 /* Allocate space for mapping (lifetime only for this function). */
2912 map
= alloca (sizeof (struct prmap
));
2914 /* Now read the mappings from the file,
2915 open a file descriptor for those that have a name,
2916 and call the callback function. */
2917 while (read (map_fd
,
2919 sizeof (struct prmap
)) == sizeof (struct prmap
))
2921 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
2923 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
2926 if (map
->pr_mapname
[0] == 0)
2928 fd
= -1; /* no map file */
2932 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
2933 /* Note: caller's responsibility to close this fd! */
2934 fd
= open_with_retry (name
, O_RDONLY
);
2935 /* Note: we don't test the above call for failure;
2936 we just pass the FD on as given. Sometimes there is
2937 no file, so the ioctl may return failure, but that's
2941 /* Stop looping if the callback returns non-zero. */
2942 if ((funcstat
= (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
)) != 0)
2946 /* Get the number of mapping entries. */
2947 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmaps
) < 0)
2948 proc_error (pi
, "proc_iterate_over_mappings (PIOCNMAP)", __LINE__
);
2950 /* Allocate space for mappings (lifetime only this function). */
2951 map
= (struct prmap
*) alloca ((nmaps
+ 1) * sizeof (struct prmap
));
2953 /* Read in all the mappings. */
2954 if (ioctl (pi
->ctl_fd
, PIOCMAP
, map
) < 0)
2955 proc_error (pi
, "proc_iterate_over_mappings (PIOCMAP)", __LINE__
);
2957 /* Now loop through the mappings, open an fd for each, and
2958 call the callback function. */
2960 i
< nmaps
&& map
[i
].pr_size
!= 0;
2963 /* Note: caller's responsibility to close this fd! */
2964 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
[i
].pr_vaddr
);
2965 /* Note: we don't test the above call for failure;
2966 we just pass the FD on as given. Sometimes there is
2967 no file, so the ioctl may return failure, but that's
2970 /* Stop looping if the callback returns non-zero. */
2971 funcstat
= (*func
) (fd
, host_pointer_to_address (map
[i
].pr_vaddr
));
2980 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2982 #include <sys/sysi86.h>
2985 * Function: proc_get_LDT_entry
2991 * The 'key' is actually the value of the lower 16 bits of
2992 * the GS register for the LWP that we're interested in.
2994 * Return: matching ssh struct (LDT entry).
2998 proc_get_LDT_entry (procinfo
*pi
, int key
)
3000 static struct ssd
*ldt_entry
= NULL
;
3002 char pathname
[MAX_PROC_NAME_SIZE
];
3003 struct cleanup
*old_chain
= NULL
;
3006 /* Allocate space for one LDT entry.
3007 This alloc must persist, because we return a pointer to it. */
3008 if (ldt_entry
== NULL
)
3009 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
3011 /* Open the file descriptor for the LDT table. */
3012 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
3013 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
3015 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
3018 /* Make sure it gets closed again! */
3019 old_chain
= make_cleanup_close (fd
);
3021 /* Now 'read' thru the table, find a match and return it. */
3022 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
3024 if (ldt_entry
->sel
== 0 &&
3025 ldt_entry
->bo
== 0 &&
3026 ldt_entry
->acc1
== 0 &&
3027 ldt_entry
->acc2
== 0)
3028 break; /* end of table */
3029 /* If key matches, return this entry. */
3030 if (ldt_entry
->sel
== key
)
3033 /* Loop ended, match not found. */
3037 static int nalloc
= 0;
3039 /* Get the number of LDT entries. */
3040 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
3042 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
3046 /* Allocate space for the number of LDT entries. */
3047 /* This alloc has to persist, 'cause we return a pointer to it. */
3050 ldt_entry
= (struct ssd
*)
3051 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
3055 /* Read the whole table in one gulp. */
3056 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
3058 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
3062 /* Search the table and return the (first) entry matching 'key'. */
3063 for (i
= 0; i
< nldt
; i
++)
3064 if (ldt_entry
[i
].sel
== key
)
3065 return &ldt_entry
[i
];
3067 /* Loop ended, match not found. */
3072 #endif /* TM_I386SOL2_H */
3074 /* =============== END, non-thread part of /proc "MODULE" =============== */
3076 /* =================== Thread "MODULE" =================== */
3078 /* NOTE: you'll see more ifdefs and duplication of functions here,
3079 since there is a different way to do threads on every OS. */
3082 * Function: proc_get_nthreads
3084 * Return the number of threads for the process
3087 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3092 proc_get_nthreads (procinfo
*pi
)
3096 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
3097 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
3103 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3105 * Solaris and Unixware version
3108 proc_get_nthreads (procinfo
*pi
)
3110 if (!pi
->status_valid
)
3111 if (!proc_get_status (pi
))
3115 * NEW_PROC_API: only works for the process procinfo,
3116 * because the LWP procinfos do not get prstatus filled in.
3119 if (pi
->tid
!= 0) /* find the parent process procinfo */
3120 pi
= find_procinfo_or_die (pi
->pid
, 0);
3122 return pi
->prstatus
.pr_nlwp
;
3130 proc_get_nthreads (procinfo
*pi
)
3138 * Function: proc_get_current_thread (LWP version)
3140 * Return the ID of the thread that had an event of interest.
3141 * (ie. the one that hit a breakpoint or other traced event).
3142 * All other things being equal, this should be the ID of a
3143 * thread that is currently executing.
3146 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3148 * Solaris and Unixware version
3151 proc_get_current_thread (procinfo
*pi
)
3154 * Note: this should be applied to the root procinfo for the process,
3155 * not to the procinfo for an LWP. If applied to the procinfo for
3156 * an LWP, it will simply return that LWP's ID. In that case,
3157 * find the parent process procinfo.
3161 pi
= find_procinfo_or_die (pi
->pid
, 0);
3163 if (!pi
->status_valid
)
3164 if (!proc_get_status (pi
))
3168 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
3170 return pi
->prstatus
.pr_who
;
3175 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3180 proc_get_current_thread (procinfo
*pi
)
3182 #if 0 /* FIXME: not ready for prime time? */
3183 return pi
->prstatus
.pr_tid
;
3194 proc_get_current_thread (procinfo
*pi
)
3203 * Function: proc_update_threads
3205 * Discover the IDs of all the threads within the process, and
3206 * create a procinfo for each of them (chained to the parent).
3208 * This unfortunately requires a different method on every OS.
3210 * Return: non-zero for success, zero for failure.
3214 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
3216 if (thread
&& parent
) /* sanity */
3218 thread
->status_valid
= 0;
3219 if (!proc_get_status (thread
))
3220 destroy_one_procinfo (&parent
->thread_list
, thread
);
3222 return 0; /* keep iterating */
3225 #if defined (PIOCLSTATUS)
3227 * Solaris 2.5 (ioctl) version
3230 proc_update_threads (procinfo
*pi
)
3232 gdb_prstatus_t
*prstatus
;
3233 struct cleanup
*old_chain
= NULL
;
3238 * We should never have to apply this operation to any procinfo
3239 * except the one for the main process. If that ever changes
3240 * for any reason, then take out the following clause and
3241 * replace it with one that makes sure the ctl_fd is open.
3245 pi
= find_procinfo_or_die (pi
->pid
, 0);
3247 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3249 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
3250 return 1; /* Process is not multi-threaded; nothing to do. */
3252 prstatus
= xmalloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1));
3254 old_chain
= make_cleanup (xfree
, prstatus
);
3255 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
3256 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
3258 /* Skip element zero, which represents the process as a whole. */
3259 for (i
= 1; i
< nlwp
+ 1; i
++)
3261 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
3262 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3264 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
3265 thread
->status_valid
= 1;
3267 pi
->threads_valid
= 1;
3268 do_cleanups (old_chain
);
3274 * Unixware and Solaris 6 (and later) version
3277 do_closedir_cleanup (void *dir
)
3283 proc_update_threads (procinfo
*pi
)
3285 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3286 struct dirent
*direntry
;
3287 struct cleanup
*old_chain
= NULL
;
3293 * We should never have to apply this operation to any procinfo
3294 * except the one for the main process. If that ever changes
3295 * for any reason, then take out the following clause and
3296 * replace it with one that makes sure the ctl_fd is open.
3300 pi
= find_procinfo_or_die (pi
->pid
, 0);
3302 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3307 * Note: this brute-force method is the only way I know of
3308 * to accomplish this task on Unixware. This method will
3309 * also work on Solaris 2.6 and 2.7. There is a much simpler
3310 * and more elegant way to do this on Solaris, but the margins
3311 * of this manuscript are too small to write it here... ;-)
3314 strcpy (pathname
, pi
->pathname
);
3315 strcat (pathname
, "/lwp");
3316 if ((dirp
= opendir (pathname
)) == NULL
)
3317 proc_error (pi
, "update_threads, opendir", __LINE__
);
3319 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
3320 while ((direntry
= readdir (dirp
)) != NULL
)
3321 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3323 lwpid
= atoi (&direntry
->d_name
[0]);
3324 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3325 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3327 pi
->threads_valid
= 1;
3328 do_cleanups (old_chain
);
3337 proc_update_threads (procinfo
*pi
)
3343 * We should never have to apply this operation to any procinfo
3344 * except the one for the main process. If that ever changes
3345 * for any reason, then take out the following clause and
3346 * replace it with one that makes sure the ctl_fd is open.
3350 pi
= find_procinfo_or_die (pi
->pid
, 0);
3352 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3354 nthreads
= proc_get_nthreads (pi
);
3356 return 0; /* nothing to do for 1 or fewer threads */
3358 threads
= xmalloc (nthreads
* sizeof (tid_t
));
3360 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3361 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3363 for (i
= 0; i
< nthreads
; i
++)
3365 if (!find_procinfo (pi
->pid
, threads
[i
]))
3366 if (!create_procinfo (pi
->pid
, threads
[i
]))
3367 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3369 pi
->threads_valid
= 1;
3377 proc_update_threads (procinfo
*pi
)
3381 #endif /* OSF PIOCTLIST */
3382 #endif /* NEW_PROC_API */
3383 #endif /* SOL 2.5 PIOCLSTATUS */
3386 * Function: proc_iterate_over_threads
3389 * Given a pointer to a function, call that function once
3390 * for each lwp in the procinfo list, until the function
3391 * returns non-zero, in which event return the value
3392 * returned by the function.
3394 * Note: this function does NOT call update_threads.
3395 * If you want to discover new threads first, you must
3396 * call that function explicitly. This function just makes
3397 * a quick pass over the currently-known procinfos.
3400 * pi - parent process procinfo
3401 * func - per-thread function
3402 * ptr - opaque parameter for function.
3405 * First non-zero return value from the callee, or zero.
3409 proc_iterate_over_threads (procinfo
*pi
,
3410 int (*func
) (procinfo
*, procinfo
*, void *),
3413 procinfo
*thread
, *next
;
3417 * We should never have to apply this operation to any procinfo
3418 * except the one for the main process. If that ever changes
3419 * for any reason, then take out the following clause and
3420 * replace it with one that makes sure the ctl_fd is open.
3424 pi
= find_procinfo_or_die (pi
->pid
, 0);
3426 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3428 next
= thread
->next
; /* in case thread is destroyed */
3429 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3436 /* =================== END, Thread "MODULE" =================== */
3438 /* =================== END, /proc "MODULE" =================== */
3440 /* =================== GDB "MODULE" =================== */
3443 * Here are all of the gdb target vector functions and their friends.
3446 static ptid_t
do_attach (ptid_t ptid
);
3447 static void do_detach (int signo
);
3448 static int register_gdb_signals (procinfo
*, gdb_sigset_t
*);
3451 * Function: procfs_debug_inferior
3453 * Sets up the inferior to be debugged.
3454 * Registers to trace signals, hardware faults, and syscalls.
3455 * Note: does not set RLC flag: caller may want to customize that.
3457 * Returns: zero for success (note! unlike most functions in this module)
3458 * On failure, returns the LINE NUMBER where it failed!
3462 procfs_debug_inferior (procinfo
*pi
)
3464 fltset_t traced_faults
;
3465 gdb_sigset_t traced_signals
;
3466 sysset_t
*traced_syscall_entries
;
3467 sysset_t
*traced_syscall_exits
;
3470 #ifdef PROCFS_DONT_TRACE_FAULTS
3471 /* On some systems (OSF), we don't trace hardware faults.
3472 Apparently it's enough that we catch them as signals.
3473 Wonder why we don't just do that in general? */
3474 premptyset (&traced_faults
); /* don't trace faults. */
3476 /* Register to trace hardware faults in the child. */
3477 prfillset (&traced_faults
); /* trace all faults... */
3478 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3480 if (!proc_set_traced_faults (pi
, &traced_faults
))
3483 /* Register to trace selected signals in the child. */
3484 premptyset (&traced_signals
);
3485 if (!register_gdb_signals (pi
, &traced_signals
))
3489 /* Register to trace the 'exit' system call (on entry). */
3490 traced_syscall_entries
= sysset_t_alloc (pi
);
3491 gdb_premptysysset (traced_syscall_entries
);
3493 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
3496 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3499 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
3501 #ifdef DYNAMIC_SYSCALLS
3503 int callnum
= find_syscall (pi
, "_exit");
3505 gdb_praddsysset (traced_syscall_entries
, callnum
);
3509 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
3510 xfree (traced_syscall_entries
);
3514 #ifdef PRFS_STOPEXEC /* defined on OSF */
3515 /* OSF method for tracing exec syscalls. Quoting:
3516 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3517 exits from exec system calls because of the user level loader. */
3518 /* FIXME: make nice and maybe move into an access function. */
3522 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3525 prfs_flags
|= PRFS_STOPEXEC
;
3527 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3530 #else /* not PRFS_STOPEXEC */
3531 /* Everyone else's (except OSF) method for tracing exec syscalls */
3533 Not all systems with /proc have all the exec* syscalls with the same
3534 names. On the SGI, for example, there is no SYS_exec, but there
3535 *is* a SYS_execv. So, we try to account for that. */
3537 traced_syscall_exits
= sysset_t_alloc (pi
);
3538 gdb_premptysysset (traced_syscall_exits
);
3540 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
3543 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
3546 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
3549 #ifdef SYS_lwpcreate
3550 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3551 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3554 #ifdef SYS_lwp_create /* FIXME: once only, please */
3555 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3556 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3559 #ifdef DYNAMIC_SYSCALLS
3561 int callnum
= find_syscall (pi
, "execve");
3563 gdb_praddsysset (traced_syscall_exits
, callnum
);
3564 callnum
= find_syscall (pi
, "ra_execve");
3566 gdb_praddsysset (traced_syscall_exits
, callnum
);
3570 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3571 xfree (traced_syscall_exits
);
3575 #endif /* PRFS_STOPEXEC */
3580 procfs_attach (char *args
, int from_tty
)
3586 error_no_arg ("process-id to attach");
3589 if (pid
== getpid ())
3590 error ("Attaching GDB to itself is not a good idea...");
3594 exec_file
= get_exec_file (0);
3597 printf_filtered ("Attaching to program `%s', %s\n",
3598 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3600 printf_filtered ("Attaching to %s\n",
3601 target_pid_to_str (pid_to_ptid (pid
)));
3605 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3606 push_target (&procfs_ops
);
3610 procfs_detach (char *args
, int from_tty
)
3617 exec_file
= get_exec_file (0);
3620 printf_filtered ("Detaching from program: %s %s\n",
3621 exec_file
, target_pid_to_str (inferior_ptid
));
3625 signo
= atoi (args
);
3628 inferior_ptid
= null_ptid
;
3629 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3633 do_attach (ptid_t ptid
)
3638 if ((pi
= create_procinfo (PIDGET (ptid
), 0)) == NULL
)
3639 perror ("procfs: out of memory in 'attach'");
3641 if (!open_procinfo_files (pi
, FD_CTL
))
3643 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3644 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3646 dead_procinfo (pi
, errmsg
, NOKILL
);
3649 /* Stop the process (if it isn't already stopped). */
3650 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3652 pi
->was_stopped
= 1;
3653 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3657 pi
->was_stopped
= 0;
3658 /* Set the process to run again when we close it. */
3659 if (!proc_set_run_on_last_close (pi
))
3660 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3662 /* Now stop the process. */
3663 if (!proc_stop_process (pi
))
3664 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3665 pi
->ignore_next_sigstop
= 1;
3667 /* Save some of the /proc state to be restored if we detach. */
3668 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3669 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3670 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3671 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3672 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3673 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3675 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3676 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3678 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3679 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3681 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3682 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3684 /* Let GDB know that the inferior was attached. */
3686 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3690 do_detach (int signo
)
3694 /* Find procinfo for the main process */
3695 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0); /* FIXME: threads */
3697 if (!proc_set_current_signal (pi
, signo
))
3698 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3700 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3701 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3703 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3704 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3706 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3707 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3709 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3710 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3712 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3713 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3715 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3716 if (signo
|| !(pi
->was_stopped
) ||
3717 query ("Was stopped when attached, make it runnable again? "))
3719 /* Clear any pending signal. */
3720 if (!proc_clear_current_fault (pi
))
3721 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3723 if (!proc_set_run_on_last_close (pi
))
3724 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3728 destroy_procinfo (pi
);
3734 * Since the /proc interface cannot give us individual registers,
3735 * we pay no attention to the (regno) argument, and just fetch them all.
3736 * This results in the possibility that we will do unnecessarily many
3737 * fetches, since we may be called repeatedly for individual registers.
3738 * So we cache the results, and mark the cache invalid when the process
3743 procfs_fetch_registers (int regno
)
3745 gdb_fpregset_t
*fpregs
;
3746 gdb_gregset_t
*gregs
;
3751 pid
= PIDGET (inferior_ptid
);
3752 tid
= TIDGET (inferior_ptid
);
3754 /* First look up procinfo for the main process. */
3755 pi
= find_procinfo_or_die (pid
, 0);
3757 /* If the event thread is not the same as GDB's requested thread
3758 (ie. inferior_ptid), then look up procinfo for the requested
3761 (tid
!= proc_get_current_thread (pi
)))
3762 pi
= find_procinfo_or_die (pid
, tid
);
3765 error ("procfs: fetch_registers failed to find procinfo for %s",
3766 target_pid_to_str (inferior_ptid
));
3768 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3769 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3771 supply_gregset (gregs
);
3773 if (FP0_REGNUM
>= 0) /* need floating point? */
3775 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3776 regno
== PC_REGNUM
||
3777 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3778 regno
== FP_REGNUM
||
3780 return; /* not a floating point register */
3782 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3783 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3785 supply_fpregset (fpregs
);
3789 /* Get ready to modify the registers array. On machines which store
3790 individual registers, this doesn't need to do anything. On
3791 machines which store all the registers in one fell swoop, such as
3792 /proc, this makes sure that registers contains all the registers
3793 from the program being debugged. */
3796 procfs_prepare_to_store (void)
3798 #ifdef CHILD_PREPARE_TO_STORE
3799 CHILD_PREPARE_TO_STORE ();
3806 * Since the /proc interface will not read individual registers,
3807 * we will cache these requests until the process is resumed, and
3808 * only then write them back to the inferior process.
3810 * FIXME: is that a really bad idea? Have to think about cases
3811 * where writing one register might affect the value of others, etc.
3815 procfs_store_registers (int regno
)
3817 gdb_fpregset_t
*fpregs
;
3818 gdb_gregset_t
*gregs
;
3823 pid
= PIDGET (inferior_ptid
);
3824 tid
= TIDGET (inferior_ptid
);
3826 /* First find procinfo for main process */
3827 pi
= find_procinfo_or_die (pid
, 0);
3829 /* If current lwp for process is not the same as requested thread
3830 (ie. inferior_ptid), then find procinfo for the requested thread. */
3833 (tid
!= proc_get_current_thread (pi
)))
3834 pi
= find_procinfo_or_die (pid
, tid
);
3837 error ("procfs: store_registers: failed to find procinfo for %s",
3838 target_pid_to_str (inferior_ptid
));
3840 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3841 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3843 fill_gregset (gregs
, regno
);
3844 if (!proc_set_gregs (pi
))
3845 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3847 if (FP0_REGNUM
>= 0) /* need floating point? */
3849 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3850 regno
== PC_REGNUM
||
3851 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3852 regno
== FP_REGNUM
||
3854 return; /* not a floating point register */
3856 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3857 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3859 fill_fpregset (fpregs
, regno
);
3860 if (!proc_set_fpregs (pi
))
3861 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3866 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3870 if (scall
== SYS_lwp_exit
)
3874 if (scall
== SYS_lwpexit
)
3881 syscall_is_exit (procinfo
*pi
, int scall
)
3884 if (scall
== SYS_exit
)
3887 #ifdef DYNAMIC_SYSCALLS
3888 if (find_syscall (pi
, "_exit") == scall
)
3895 syscall_is_exec (procinfo
*pi
, int scall
)
3898 if (scall
== SYS_exec
)
3902 if (scall
== SYS_execv
)
3906 if (scall
== SYS_execve
)
3909 #ifdef DYNAMIC_SYSCALLS
3910 if (find_syscall (pi
, "_execve"))
3912 if (find_syscall (pi
, "ra_execve"))
3919 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3921 #ifdef SYS_lwp_create
3922 if (scall
== SYS_lwp_create
)
3925 #ifdef SYS_lwpcreate
3926 if (scall
== SYS_lwpcreate
)
3933 * Function: target_wait
3935 * Retrieve the next stop event from the child process.
3936 * If child has not stopped yet, wait for it to stop.
3937 * Translate /proc eventcodes (or possibly wait eventcodes)
3938 * into gdb internal event codes.
3940 * Return: id of process (and possibly thread) that incurred the event.
3941 * event codes are returned thru a pointer parameter.
3945 procfs_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3947 /* First cut: loosely based on original version 2.1 */
3951 ptid_t retval
, temp_ptid
;
3952 int why
, what
, flags
;
3959 retval
= pid_to_ptid (-1);
3961 /* Find procinfo for main process */
3962 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
3965 /* We must assume that the status is stale now... */
3966 pi
->status_valid
= 0;
3967 pi
->gregs_valid
= 0;
3968 pi
->fpregs_valid
= 0;
3970 #if 0 /* just try this out... */
3971 flags
= proc_flags (pi
);
3972 why
= proc_why (pi
);
3973 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3974 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3976 /* If child is not stopped, wait for it to stop. */
3977 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3978 !proc_wait_for_stop (pi
))
3980 /* wait_for_stop failed: has the child terminated? */
3981 if (errno
== ENOENT
)
3985 /* /proc file not found; presumably child has terminated. */
3986 wait_retval
= wait (&wstat
); /* "wait" for the child's exit */
3988 if (wait_retval
!= PIDGET (inferior_ptid
)) /* wrong child? */
3989 error ("procfs: couldn't stop process %d: wait returned %d\n",
3990 PIDGET (inferior_ptid
), wait_retval
);
3991 /* FIXME: might I not just use waitpid?
3992 Or try find_procinfo to see if I know about this child? */
3993 retval
= pid_to_ptid (wait_retval
);
3995 else if (errno
== EINTR
)
3999 /* Unknown error from wait_for_stop. */
4000 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
4005 /* This long block is reached if either:
4006 a) the child was already stopped, or
4007 b) we successfully waited for the child with wait_for_stop.
4008 This block will analyze the /proc status, and translate it
4009 into a waitstatus for GDB.
4011 If we actually had to call wait because the /proc file
4012 is gone (child terminated), then we skip this block,
4013 because we already have a waitstatus. */
4015 flags
= proc_flags (pi
);
4016 why
= proc_why (pi
);
4017 what
= proc_what (pi
);
4019 if (flags
& (PR_STOPPED
| PR_ISTOP
))
4022 /* If it's running async (for single_thread control),
4023 set it back to normal again. */
4024 if (flags
& PR_ASYNC
)
4025 if (!proc_unset_async (pi
))
4026 proc_error (pi
, "target_wait, unset_async", __LINE__
);
4030 proc_prettyprint_why (why
, what
, 1);
4032 /* The 'pid' we will return to GDB is composed of
4033 the process ID plus the lwp ID. */
4034 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4038 wstat
= (what
<< 8) | 0177;
4041 if (syscall_is_lwp_exit (pi
, what
))
4043 printf_filtered ("[%s exited]\n",
4044 target_pid_to_str (retval
));
4045 delete_thread (retval
);
4046 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4049 else if (syscall_is_exit (pi
, what
))
4051 /* Handle SYS_exit call only */
4052 /* Stopped at entry to SYS_exit.
4053 Make it runnable, resume it, then use
4054 the wait system call to get its exit code.
4055 Proc_run_process always clears the current
4057 Then return its exit status. */
4058 pi
->status_valid
= 0;
4060 /* FIXME: what we should do is return
4061 TARGET_WAITKIND_SPURIOUS. */
4062 if (!proc_run_process (pi
, 0, 0))
4063 proc_error (pi
, "target_wait, run_process", __LINE__
);
4066 /* Don't call wait: simulate waiting for exit,
4067 return a "success" exit code. Bogus: what if
4068 it returns something else? */
4070 retval
= inferior_ptid
; /* ? ? ? */
4074 int temp
= wait (&wstat
);
4076 /* FIXME: shouldn't I make sure I get the right
4077 event from the right process? If (for
4078 instance) I have killed an earlier inferior
4079 process but failed to clean up after it
4080 somehow, I could get its termination event
4083 /* If wait returns -1, that's what we return to GDB. */
4085 retval
= pid_to_ptid (temp
);
4090 printf_filtered ("procfs: trapped on entry to ");
4091 proc_prettyprint_syscall (proc_what (pi
), 0);
4092 printf_filtered ("\n");
4095 long i
, nsysargs
, *sysargs
;
4097 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4098 (sysargs
= proc_sysargs (pi
)) != NULL
)
4100 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4101 for (i
= 0; i
< nsysargs
; i
++)
4102 printf_filtered ("#%ld: 0x%08lx\n",
4110 /* How to exit gracefully, returning "unknown event" */
4111 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4112 return inferior_ptid
;
4116 /* How to keep going without returning to wfi: */
4117 target_resume (ptid
, 0, TARGET_SIGNAL_0
);
4123 if (syscall_is_exec (pi
, what
))
4125 /* Hopefully this is our own "fork-child" execing
4126 the real child. Hoax this event into a trap, and
4127 GDB will see the child about to execute its start
4129 wstat
= (SIGTRAP
<< 8) | 0177;
4131 else if (syscall_is_lwp_create (pi
, what
))
4134 * This syscall is somewhat like fork/exec.
4135 * We will get the event twice: once for the parent LWP,
4136 * and once for the child. We should already know about
4137 * the parent LWP, but the child will be new to us. So,
4138 * whenever we get this event, if it represents a new
4139 * thread, simply add the thread to the list.
4142 /* If not in procinfo list, add it. */
4143 temp_tid
= proc_get_current_thread (pi
);
4144 if (!find_procinfo (pi
->pid
, temp_tid
))
4145 create_procinfo (pi
->pid
, temp_tid
);
4147 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4148 /* If not in GDB's thread list, add it. */
4149 if (!in_thread_list (temp_ptid
))
4151 printf_filtered ("[New %s]\n",
4152 target_pid_to_str (temp_ptid
));
4153 add_thread (temp_ptid
);
4155 /* Return to WFI, but tell it to immediately resume. */
4156 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4157 return inferior_ptid
;
4159 else if (syscall_is_lwp_exit (pi
, what
))
4161 printf_filtered ("[%s exited]\n",
4162 target_pid_to_str (retval
));
4163 delete_thread (retval
);
4164 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4169 /* FIXME: Do we need to handle SYS_sproc,
4170 SYS_fork, or SYS_vfork here? The old procfs
4171 seemed to use this event to handle threads on
4172 older (non-LWP) systems, where I'm assuming
4173 that threads were actually separate processes.
4174 Irix, maybe? Anyway, low priority for now. */
4178 printf_filtered ("procfs: trapped on exit from ");
4179 proc_prettyprint_syscall (proc_what (pi
), 0);
4180 printf_filtered ("\n");
4183 long i
, nsysargs
, *sysargs
;
4185 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4186 (sysargs
= proc_sysargs (pi
)) != NULL
)
4188 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4189 for (i
= 0; i
< nsysargs
; i
++)
4190 printf_filtered ("#%ld: 0x%08lx\n",
4195 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4196 return inferior_ptid
;
4201 wstat
= (SIGSTOP
<< 8) | 0177;
4206 printf_filtered ("Retry #%d:\n", retry
);
4207 pi
->status_valid
= 0;
4212 /* If not in procinfo list, add it. */
4213 temp_tid
= proc_get_current_thread (pi
);
4214 if (!find_procinfo (pi
->pid
, temp_tid
))
4215 create_procinfo (pi
->pid
, temp_tid
);
4217 /* If not in GDB's thread list, add it. */
4218 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4219 if (!in_thread_list (temp_ptid
))
4221 printf_filtered ("[New %s]\n",
4222 target_pid_to_str (temp_ptid
));
4223 add_thread (temp_ptid
);
4226 status
->kind
= TARGET_WAITKIND_STOPPED
;
4227 status
->value
.sig
= 0;
4232 wstat
= (what
<< 8) | 0177;
4235 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
4238 wstat
= (SIGTRAP
<< 8) | 0177;
4243 wstat
= (SIGTRAP
<< 8) | 0177;
4246 /* FIXME: use si_signo where possible. */
4248 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4251 wstat
= (SIGILL
<< 8) | 0177;
4254 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4257 wstat
= (SIGTRAP
<< 8) | 0177;
4261 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4264 wstat
= (SIGSEGV
<< 8) | 0177;
4268 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4271 wstat
= (SIGFPE
<< 8) | 0177;
4273 case FLTPAGE
: /* Recoverable page fault */
4274 default: /* FIXME: use si_signo if possible for fault */
4275 retval
= pid_to_ptid (-1);
4276 printf_filtered ("procfs:%d -- ", __LINE__
);
4277 printf_filtered ("child stopped for unknown reason:\n");
4278 proc_prettyprint_why (why
, what
, 1);
4279 error ("... giving up...");
4282 break; /* case PR_FAULTED: */
4283 default: /* switch (why) unmatched */
4284 printf_filtered ("procfs:%d -- ", __LINE__
);
4285 printf_filtered ("child stopped for unknown reason:\n");
4286 proc_prettyprint_why (why
, what
, 1);
4287 error ("... giving up...");
4291 * Got this far without error:
4292 * If retval isn't in the threads database, add it.
4294 if (PIDGET (retval
) > 0 &&
4295 !ptid_equal (retval
, inferior_ptid
) &&
4296 !in_thread_list (retval
))
4299 * We have a new thread.
4300 * We need to add it both to GDB's list and to our own.
4301 * If we don't create a procinfo, resume may be unhappy
4304 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
4305 add_thread (retval
);
4306 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
4307 create_procinfo (PIDGET (retval
), TIDGET (retval
));
4309 /* In addition, it's possible that this is the first
4310 * new thread we've seen, in which case we may not
4311 * have created entries for inferior_ptid yet.
4313 if (TIDGET (inferior_ptid
) != 0)
4315 if (!in_thread_list (inferior_ptid
))
4316 add_thread (inferior_ptid
);
4317 if (find_procinfo (PIDGET (inferior_ptid
),
4318 TIDGET (inferior_ptid
)) == NULL
)
4319 create_procinfo (PIDGET (inferior_ptid
),
4320 TIDGET (inferior_ptid
));
4324 else /* flags do not indicate STOPPED */
4326 /* surely this can't happen... */
4327 printf_filtered ("procfs:%d -- process not stopped.\n",
4329 proc_prettyprint_flags (flags
, 1);
4330 error ("procfs: ...giving up...");
4335 store_waitstatus (status
, wstat
);
4341 /* Transfer LEN bytes between GDB address MYADDR and target address
4342 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4343 otherwise transfer them from the target. TARGET is unused.
4345 The return value is 0 if an error occurred or no bytes were
4346 transferred. Otherwise, it will be a positive value which
4347 indicates the number of bytes transferred between gdb and the
4348 target. (Note that the interface also makes provisions for
4349 negative values, but this capability isn't implemented here.) */
4352 procfs_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int dowrite
,
4353 struct mem_attrib
*attrib
, struct target_ops
*target
)
4358 /* Find procinfo for main process */
4359 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4360 if (pi
->as_fd
== 0 &&
4361 open_procinfo_files (pi
, FD_AS
) == 0)
4363 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4367 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4372 PROCFS_NOTE ("write memory: ");
4374 PROCFS_NOTE ("write memory: \n");
4376 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4380 PROCFS_NOTE ("read memory: \n");
4381 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4392 * Function: invalidate_cache
4394 * Called by target_resume before making child runnable.
4395 * Mark cached registers and status's invalid.
4396 * If there are "dirty" caches that need to be written back
4397 * to the child process, do that.
4399 * File descriptors are also cached.
4400 * As they are a limited resource, we cannot hold onto them indefinitely.
4401 * However, as they are expensive to open, we don't want to throw them
4402 * away indescriminately either. As a compromise, we will keep the
4403 * file descriptors for the parent process, but discard any file
4404 * descriptors we may have accumulated for the threads.
4407 * As this function is called by iterate_over_threads, it always
4408 * returns zero (so that iterate_over_threads will keep iterating).
4413 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4416 * About to run the child; invalidate caches and do any other cleanup.
4420 if (pi
->gregs_dirty
)
4421 if (parent
== NULL
||
4422 proc_get_current_thread (parent
) != pi
->tid
)
4423 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4424 proc_warn (pi
, "target_resume, set_gregs",
4426 if (FP0_REGNUM
>= 0)
4427 if (pi
->fpregs_dirty
)
4428 if (parent
== NULL
||
4429 proc_get_current_thread (parent
) != pi
->tid
)
4430 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4431 proc_warn (pi
, "target_resume, set_fpregs",
4437 /* The presence of a parent indicates that this is an LWP.
4438 Close any file descriptors that it might have open.
4439 We don't do this to the master (parent) procinfo. */
4441 close_procinfo_files (pi
);
4443 pi
->gregs_valid
= 0;
4444 pi
->fpregs_valid
= 0;
4446 pi
->gregs_dirty
= 0;
4447 pi
->fpregs_dirty
= 0;
4449 pi
->status_valid
= 0;
4450 pi
->threads_valid
= 0;
4457 * Function: make_signal_thread_runnable
4459 * A callback function for iterate_over_threads.
4460 * Find the asynchronous signal thread, and make it runnable.
4461 * See if that helps matters any.
4465 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4468 if (proc_flags (pi
) & PR_ASLWP
)
4470 if (!proc_run_process (pi
, 0, -1))
4471 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4480 * Function: target_resume
4482 * Make the child process runnable. Normally we will then call
4483 * procfs_wait and wait for it to stop again (unles gdb is async).
4486 * step: if true, then arrange for the child to stop again
4487 * after executing a single instruction.
4488 * signo: if zero, then cancel any pending signal.
4489 * If non-zero, then arrange for the indicated signal
4490 * to be delivered to the child when it runs.
4491 * pid: if -1, then allow any child thread to run.
4492 * if non-zero, then allow only the indicated thread to run.
4493 ******* (not implemented yet)
4497 procfs_resume (ptid_t ptid
, int step
, enum target_signal signo
)
4499 procinfo
*pi
, *thread
;
4503 prrun.prflags |= PRSVADDR;
4504 prrun.pr_vaddr = $PC; set resume address
4505 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4506 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4507 prrun.prflags |= PRCFAULT; clear current fault.
4509 PRSTRACE and PRSFAULT can be done by other means
4510 (proc_trace_signals, proc_trace_faults)
4511 PRSVADDR is unnecessary.
4512 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4513 This basically leaves PRSTEP and PRCSIG.
4514 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4515 So basically PR_STEP is the sole argument that must be passed
4516 to proc_run_process (for use in the prrun struct by ioctl). */
4518 /* Find procinfo for main process */
4519 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4521 /* First cut: ignore pid argument */
4524 /* Convert signal to host numbering. */
4526 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4529 native_signo
= target_signal_to_host (signo
);
4531 pi
->ignore_next_sigstop
= 0;
4533 /* Running the process voids all cached registers and status. */
4534 /* Void the threads' caches first */
4535 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4536 /* Void the process procinfo's caches. */
4537 invalidate_cache (NULL
, pi
, NULL
);
4539 if (PIDGET (ptid
) != -1)
4541 /* Resume a specific thread, presumably suppressing the others. */
4542 thread
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
));
4545 if (thread
->tid
!= 0)
4547 /* We're to resume a specific thread, and not the others.
4548 * Set the child process's PR_ASYNC flag.
4551 if (!proc_set_async (pi
))
4552 proc_error (pi
, "target_resume, set_async", __LINE__
);
4555 proc_iterate_over_threads (pi
,
4556 make_signal_thread_runnable
,
4559 pi
= thread
; /* substitute the thread's procinfo for run */
4564 if (!proc_run_process (pi
, step
, native_signo
))
4567 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4569 proc_error (pi
, "target_resume", __LINE__
);
4574 * Function: register_gdb_signals
4576 * Traverse the list of signals that GDB knows about
4577 * (see "handle" command), and arrange for the target
4578 * to be stopped or not, according to these settings.
4580 * Returns non-zero for success, zero for failure.
4584 register_gdb_signals (procinfo
*pi
, gdb_sigset_t
*signals
)
4588 for (signo
= 0; signo
< NSIG
; signo
++)
4589 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4590 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4591 signal_pass_state (target_signal_from_host (signo
)) == 1)
4592 prdelset (signals
, signo
);
4594 praddset (signals
, signo
);
4596 return proc_set_traced_signals (pi
, signals
);
4600 * Function: target_notice_signals
4602 * Set up to trace signals in the child process.
4606 procfs_notice_signals (ptid_t ptid
)
4608 gdb_sigset_t signals
;
4609 procinfo
*pi
= find_procinfo_or_die (PIDGET (ptid
), 0);
4611 if (proc_get_traced_signals (pi
, &signals
) &&
4612 register_gdb_signals (pi
, &signals
))
4615 proc_error (pi
, "notice_signals", __LINE__
);
4619 * Function: target_files_info
4621 * Print status information about the child process.
4625 procfs_files_info (struct target_ops
*ignore
)
4627 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4628 attach_flag
? "attached": "child",
4629 target_pid_to_str (inferior_ptid
));
4633 * Function: target_open
4635 * A dummy: you don't open procfs.
4639 procfs_open (char *args
, int from_tty
)
4641 error ("Use the \"run\" command to start a Unix child process.");
4645 * Function: target_can_run
4647 * This tells GDB that this target vector can be invoked
4648 * for "run" or "attach".
4651 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4652 be a runnable target. Used by targets
4653 that can sit atop procfs, such as solaris
4658 procfs_can_run (void)
4660 /* This variable is controlled by modules that sit atop procfs that
4661 may layer their own process structure atop that provided here.
4662 sol-thread.c does this because of the Solaris two-level thread
4665 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4667 return !procfs_suppress_run
;
4671 * Function: target_stop
4673 * Stop the child process asynchronously, as when the
4674 * gdb user types control-c or presses a "stop" button.
4676 * Works by sending kill(SIGINT) to the child's process group.
4682 extern pid_t inferior_process_group
;
4684 kill (-inferior_process_group
, SIGINT
);
4688 * Function: unconditionally_kill_inferior
4690 * Make it die. Wait for it to die. Clean up after it.
4691 * Note: this should only be applied to the real process,
4692 * not to an LWP, because of the check for parent-process.
4693 * If we need this to work for an LWP, it needs some more logic.
4697 unconditionally_kill_inferior (procinfo
*pi
)
4701 parent_pid
= proc_parent_pid (pi
);
4702 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4703 /* FIXME: use access functions */
4704 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4705 before the PIOCKILL, otherwise it might generate a corrupted core
4706 file for the inferior. */
4707 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4709 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4712 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4713 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4714 to kill the inferior, otherwise it might remain stopped with a
4716 We do not check the result of the PIOCSSIG, the inferior might have
4719 gdb_siginfo_t newsiginfo
;
4721 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4722 newsiginfo
.si_signo
= SIGKILL
;
4723 newsiginfo
.si_code
= 0;
4724 newsiginfo
.si_errno
= 0;
4725 newsiginfo
.si_pid
= getpid ();
4726 newsiginfo
.si_uid
= getuid ();
4727 /* FIXME: use proc_set_current_signal */
4728 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4730 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4731 if (!proc_kill (pi
, SIGKILL
))
4732 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4733 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4734 destroy_procinfo (pi
);
4736 /* If pi is GDB's child, wait for it to die. */
4737 if (parent_pid
== getpid ())
4738 /* FIXME: should we use waitpid to make sure we get the right event?
4739 Should we check the returned event? */
4744 ret
= waitpid (pi
->pid
, &status
, 0);
4752 * Function: target_kill_inferior
4754 * We're done debugging it, and we want it to go away.
4755 * Then we want GDB to forget all about it.
4759 procfs_kill_inferior (void)
4761 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4763 /* Find procinfo for main process */
4764 procinfo
*pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4767 unconditionally_kill_inferior (pi
);
4768 target_mourn_inferior ();
4773 * Function: target_mourn_inferior
4775 * Forget we ever debugged this thing!
4779 procfs_mourn_inferior (void)
4783 if (!ptid_equal (inferior_ptid
, null_ptid
))
4785 /* Find procinfo for main process */
4786 pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4788 destroy_procinfo (pi
);
4790 unpush_target (&procfs_ops
);
4791 generic_mourn_inferior ();
4795 * Function: init_inferior
4797 * When GDB forks to create a runnable inferior process,
4798 * this function is called on the parent side of the fork.
4799 * It's job is to do whatever is necessary to make the child
4800 * ready to be debugged, and then wait for the child to synchronize.
4804 procfs_init_inferior (int pid
)
4807 gdb_sigset_t signals
;
4810 /* This routine called on the parent side (GDB side)
4811 after GDB forks the inferior. */
4813 push_target (&procfs_ops
);
4815 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4816 perror ("procfs: out of memory in 'init_inferior'");
4818 if (!open_procinfo_files (pi
, FD_CTL
))
4819 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4823 open_procinfo_files // done
4826 procfs_notice_signals
4833 /* If not stopped yet, wait for it to stop. */
4834 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4835 !(proc_wait_for_stop (pi
)))
4836 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4838 /* Save some of the /proc state to be restored if we detach. */
4839 /* FIXME: Why? In case another debugger was debugging it?
4840 We're it's parent, for Ghu's sake! */
4841 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4842 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4843 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4844 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4845 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4846 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4847 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4848 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4849 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4850 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4852 /* Register to trace selected signals in the child. */
4853 prfillset (&signals
);
4854 if (!register_gdb_signals (pi
, &signals
))
4855 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4857 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4858 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4860 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4861 and possibly even turning ON kill-on-last-close at this point. But
4862 I can't make that change without careful testing which I don't have
4863 time to do right now... */
4864 /* Turn on run-on-last-close flag so that the child
4865 will die if GDB goes away for some reason. */
4866 if (!proc_set_run_on_last_close (pi
))
4867 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4869 /* The 'process ID' we return to GDB is composed of
4870 the actual process ID plus the lwp ID. */
4871 inferior_ptid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4873 #ifdef START_INFERIOR_TRAPS_EXPECTED
4874 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4876 /* One trap to exec the shell, one to exec the program being debugged. */
4877 startup_inferior (2);
4878 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4882 * Function: set_exec_trap
4884 * When GDB forks to create a new process, this function is called
4885 * on the child side of the fork before GDB exec's the user program.
4886 * Its job is to make the child minimally debuggable, so that the
4887 * parent GDB process can connect to the child and take over.
4888 * This function should do only the minimum to make that possible,
4889 * and to synchronize with the parent process. The parent process
4890 * should take care of the details.
4894 procfs_set_exec_trap (void)
4896 /* This routine called on the child side (inferior side)
4897 after GDB forks the inferior. It must use only local variables,
4898 because it may be sharing data space with its parent. */
4903 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4904 perror_with_name ("procfs: create_procinfo failed in child.");
4906 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4908 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4909 gdb_flush (gdb_stderr
);
4910 /* no need to call "dead_procinfo", because we're going to exit. */
4914 #ifdef PRFS_STOPEXEC /* defined on OSF */
4915 /* OSF method for tracing exec syscalls. Quoting:
4916 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4917 exits from exec system calls because of the user level loader. */
4918 /* FIXME: make nice and maybe move into an access function. */
4922 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4924 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4925 gdb_flush (gdb_stderr
);
4928 prfs_flags
|= PRFS_STOPEXEC
;
4930 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4932 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4933 gdb_flush (gdb_stderr
);
4937 #else /* not PRFS_STOPEXEC */
4938 /* Everyone else's (except OSF) method for tracing exec syscalls */
4940 Not all systems with /proc have all the exec* syscalls with the same
4941 names. On the SGI, for example, there is no SYS_exec, but there
4942 *is* a SYS_execv. So, we try to account for that. */
4944 exitset
= sysset_t_alloc (pi
);
4945 gdb_premptysysset (exitset
);
4947 gdb_praddsysset (exitset
, SYS_exec
);
4950 gdb_praddsysset (exitset
, SYS_execve
);
4953 gdb_praddsysset (exitset
, SYS_execv
);
4955 #ifdef DYNAMIC_SYSCALLS
4957 int callnum
= find_syscall (pi
, "execve");
4960 gdb_praddsysset (exitset
, callnum
);
4962 callnum
= find_syscall (pi
, "ra_execve");
4964 gdb_praddsysset (exitset
, callnum
);
4966 #endif /* DYNAMIC_SYSCALLS */
4968 if (!proc_set_traced_sysexit (pi
, exitset
))
4970 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4971 gdb_flush (gdb_stderr
);
4974 #endif /* PRFS_STOPEXEC */
4976 /* FIXME: should this be done in the parent instead? */
4977 /* Turn off inherit on fork flag so that all grand-children
4978 of gdb start with tracing flags cleared. */
4979 if (!proc_unset_inherit_on_fork (pi
))
4980 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4982 /* Turn off run on last close flag, so that the child process
4983 cannot run away just because we close our handle on it.
4984 We want it to wait for the parent to attach. */
4985 if (!proc_unset_run_on_last_close (pi
))
4986 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4988 /* FIXME: No need to destroy the procinfo --
4989 we have our own address space, and we're about to do an exec! */
4990 /*destroy_procinfo (pi);*/
4994 * Function: create_inferior
4996 * This function is called BEFORE gdb forks the inferior process.
4997 * Its only real responsibility is to set things up for the fork,
4998 * and tell GDB which two functions to call after the fork (one
4999 * for the parent, and one for the child).
5001 * This function does a complicated search for a unix shell program,
5002 * which it then uses to parse arguments and environment variables
5003 * to be sent to the child. I wonder whether this code could not
5004 * be abstracted out and shared with other unix targets such as
5009 procfs_create_inferior (char *exec_file
, char *allargs
, char **env
)
5011 char *shell_file
= getenv ("SHELL");
5013 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
5016 /* We will be looking down the PATH to find shell_file. If we
5017 just do this the normal way (via execlp, which operates by
5018 attempting an exec for each element of the PATH until it
5019 finds one which succeeds), then there will be an exec for
5020 each failed attempt, each of which will cause a PR_SYSEXIT
5021 stop, and we won't know how to distinguish the PR_SYSEXIT's
5022 for these failed execs with the ones for successful execs
5023 (whether the exec has succeeded is stored at that time in the
5024 carry bit or some such architecture-specific and
5025 non-ABI-specified place).
5027 So I can't think of anything better than to search the PATH
5028 now. This has several disadvantages: (1) There is a race
5029 condition; if we find a file now and it is deleted before we
5030 exec it, we lose, even if the deletion leaves a valid file
5031 further down in the PATH, (2) there is no way to know exactly
5032 what an executable (in the sense of "capable of being
5033 exec'd") file is. Using access() loses because it may lose
5034 if the caller is the superuser; failing to use it loses if
5035 there are ACLs or some such. */
5039 /* FIXME-maybe: might want "set path" command so user can change what
5040 path is used from within GDB. */
5041 char *path
= getenv ("PATH");
5043 struct stat statbuf
;
5046 path
= "/bin:/usr/bin";
5048 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
5049 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
5051 p1
= strchr (p
, ':');
5056 strncpy (tryname
, p
, len
);
5057 tryname
[len
] = '\0';
5058 strcat (tryname
, "/");
5059 strcat (tryname
, shell_file
);
5060 if (access (tryname
, X_OK
) < 0)
5062 if (stat (tryname
, &statbuf
) < 0)
5064 if (!S_ISREG (statbuf
.st_mode
))
5065 /* We certainly need to reject directories. I'm not quite
5066 as sure about FIFOs, sockets, etc., but I kind of doubt
5067 that people want to exec() these things. */
5072 /* Not found. This must be an error rather than merely passing
5073 the file to execlp(), because execlp() would try all the
5074 exec()s, causing GDB to get confused. */
5075 error ("procfs:%d -- Can't find shell %s in PATH",
5076 __LINE__
, shell_file
);
5078 shell_file
= tryname
;
5081 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
5082 procfs_init_inferior
, NULL
, shell_file
);
5084 /* We are at the first instruction we care about. */
5085 /* Pedal to the metal... */
5087 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
5091 * Function: notice_thread
5093 * Callback for find_new_threads.
5094 * Calls "add_thread".
5098 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
5100 ptid_t gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
5102 if (!in_thread_list (gdb_threadid
))
5103 add_thread (gdb_threadid
);
5109 * Function: target_find_new_threads
5111 * Query all the threads that the target knows about,
5112 * and give them back to GDB to add to its list.
5116 procfs_find_new_threads (void)
5120 /* Find procinfo for main process */
5121 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5122 proc_update_threads (pi
);
5123 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
5127 * Function: target_thread_alive
5129 * Return true if the thread is still 'alive'.
5131 * This guy doesn't really seem to be doing his job.
5132 * Got to investigate how to tell when a thread is really gone.
5136 procfs_thread_alive (ptid_t ptid
)
5141 proc
= PIDGET (ptid
);
5142 thread
= TIDGET (ptid
);
5143 /* If I don't know it, it ain't alive! */
5144 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
5147 /* If I can't get its status, it ain't alive!
5148 What's more, I need to forget about it! */
5149 if (!proc_get_status (pi
))
5151 destroy_procinfo (pi
);
5154 /* I couldn't have got its status if it weren't alive, so it's alive. */
5159 * Function: target_pid_to_str
5161 * Return a string to be used to identify the thread in
5162 * the "info threads" display.
5166 procfs_pid_to_str (ptid_t ptid
)
5168 static char buf
[80];
5172 proc
= PIDGET (ptid
);
5173 thread
= TIDGET (ptid
);
5174 pi
= find_procinfo (proc
, thread
);
5177 sprintf (buf
, "Process %d", proc
);
5179 sprintf (buf
, "LWP %d", thread
);
5184 * Function: procfs_set_watchpoint
5185 * Insert a watchpoint
5189 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
5197 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5198 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5200 /* Translate from GDB's flags to /proc's */
5201 if (len
> 0) /* len == 0 means delete watchpoint */
5203 switch (rwflag
) { /* FIXME: need an enum! */
5204 case hw_write
: /* default watchpoint (write) */
5205 pflags
= WRITE_WATCHFLAG
;
5207 case hw_read
: /* read watchpoint */
5208 pflags
= READ_WATCHFLAG
;
5210 case hw_access
: /* access watchpoint */
5211 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
5213 case hw_execute
: /* execution HW breakpoint */
5214 pflags
= EXEC_WATCHFLAG
;
5216 default: /* Something weird. Return error. */
5219 if (after
) /* Stop after r/w access is completed. */
5220 pflags
|= AFTER_WATCHFLAG
;
5223 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
5225 if (errno
== E2BIG
) /* Typical error for no resources */
5226 return -1; /* fail */
5227 /* GDB may try to remove the same watchpoint twice.
5228 If a remove request returns no match, don't error. */
5229 if (errno
== ESRCH
&& len
== 0)
5230 return 0; /* ignore */
5231 proc_error (pi
, "set_watchpoint", __LINE__
);
5234 #endif /* UNIXWARE */
5239 * Function: stopped_by_watchpoint
5241 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5242 * else returns zero.
5246 procfs_stopped_by_watchpoint (ptid_t ptid
)
5250 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5251 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5253 if (!pi
) /* If no process, then not stopped by watchpoint! */
5256 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
5258 if (proc_why (pi
) == PR_FAULTED
)
5261 if (proc_what (pi
) == FLTWATCH
)
5265 if (proc_what (pi
) == FLTKWATCH
)
5273 #ifdef TM_I386SOL2_H
5275 * Function: procfs_find_LDT_entry
5278 * ptid_t ptid; // The GDB-style pid-plus-LWP.
5281 * pointer to the corresponding LDT entry.
5285 procfs_find_LDT_entry (ptid_t ptid
)
5287 gdb_gregset_t
*gregs
;
5291 /* Find procinfo for the lwp. */
5292 if ((pi
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
))) == NULL
)
5294 warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.",
5295 PIDGET (ptid
), TIDGET (ptid
));
5298 /* get its general registers. */
5299 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
5301 warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.",
5302 PIDGET (ptid
), TIDGET (ptid
));
5305 /* Now extract the GS register's lower 16 bits. */
5306 key
= (*gregs
)[GS
] & 0xffff;
5308 /* Find the matching entry and return it. */
5309 return proc_get_LDT_entry (pi
, key
);
5311 #endif /* TM_I386SOL2_H */
5316 info_proc_cmd (char *args
, int from_tty
)
5318 struct cleanup
*old_chain
;
5319 procinfo
*process
= NULL
;
5320 procinfo
*thread
= NULL
;
5326 old_chain
= make_cleanup (null_cleanup
, 0);
5329 if ((argv
= buildargv (args
)) == NULL
)
5332 make_cleanup_freeargv (argv
);
5334 while (argv
!= NULL
&& *argv
!= NULL
)
5336 if (isdigit (argv
[0][0]))
5338 pid
= strtoul (argv
[0], &tmp
, 10);
5340 tid
= strtoul (++tmp
, NULL
, 10);
5342 else if (argv
[0][0] == '/')
5344 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5353 pid
= PIDGET (inferior_ptid
);
5355 error ("No current process: you must name one.");
5358 /* Have pid, will travel.
5359 First see if it's a process we're already debugging. */
5360 process
= find_procinfo (pid
, 0);
5361 if (process
== NULL
)
5363 /* No. So open a procinfo for it, but
5364 remember to close it again when finished. */
5365 process
= create_procinfo (pid
, 0);
5366 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5367 if (!open_procinfo_files (process
, FD_CTL
))
5368 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5372 thread
= create_procinfo (pid
, tid
);
5376 printf_filtered ("process %d flags:\n", process
->pid
);
5377 proc_prettyprint_flags (proc_flags (process
), 1);
5378 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5379 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5380 if (proc_get_nthreads (process
) > 1)
5381 printf_filtered ("Process has %d threads.\n",
5382 proc_get_nthreads (process
));
5386 printf_filtered ("thread %d flags:\n", thread
->tid
);
5387 proc_prettyprint_flags (proc_flags (thread
), 1);
5388 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5389 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5392 do_cleanups (old_chain
);
5396 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5402 if (PIDGET (inferior_ptid
) <= 0)
5403 error ("you must be debugging a process to use this command.");
5405 if (args
== NULL
|| args
[0] == 0)
5406 error_no_arg ("system call to trace");
5408 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5409 if (isdigit (args
[0]))
5411 syscallnum
= atoi (args
);
5412 if (entry_or_exit
== PR_SYSENTRY
)
5413 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5415 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5418 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5420 if (mode
== FLAG_SET
)
5421 gdb_praddsysset (sysset
, syscallnum
);
5423 gdb_prdelsysset (sysset
, syscallnum
);
5425 if (entry_or_exit
== PR_SYSENTRY
)
5427 if (!proc_set_traced_sysentry (pi
, sysset
))
5428 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5432 if (!proc_set_traced_sysexit (pi
, sysset
))
5433 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5439 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5441 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5445 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5447 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5451 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5453 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5457 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5459 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5464 _initialize_procfs (void)
5467 add_target (&procfs_ops
);
5468 add_info ("proc", info_proc_cmd
,
5469 "Show /proc process information about any running process.\
5470 Default is the process being debugged.");
5471 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5472 "Give a trace of entries into the syscall.");
5473 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5474 "Give a trace of exits from the syscall.");
5475 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5476 "Cancel a trace of entries into the syscall.");
5477 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5478 "Cancel a trace of exits from the syscall.");
5481 /* =================== END, GDB "MODULE" =================== */
5485 /* miscelaneous stubs: */
5486 /* The following satisfy a few random symbols mostly created by */
5487 /* the solaris threads implementation, which I will chase down */
5491 * Return a pid for which we guarantee
5492 * we will be able to find a 'live' procinfo.
5496 procfs_first_available (void)
5498 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);