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 (int, 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 (int);
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 int procfs_wait (int, 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 (int);
127 void procfs_find_new_threads (void);
128 char *procfs_pid_to_str (int);
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 */
319 /* Provide default composite pid manipulation macros for systems that
320 don't have threads. */
323 #define PIDGET(PID) (PID)
324 #define TIDGET(PID) (PID)
327 #define MERGEPID(PID, TID) (PID)
330 typedef struct procinfo
{
331 struct procinfo
*next
;
332 int pid
; /* Process ID */
333 int tid
; /* Thread/LWP id */
337 int ignore_next_sigstop
;
339 /* The following four fd fields may be identical, or may contain
340 several different fd's, depending on the version of /proc
341 (old ioctl or new read/write). */
343 int ctl_fd
; /* File descriptor for /proc control file */
345 * The next three file descriptors are actually only needed in the
346 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
347 * However, to avoid a bunch of #ifdefs in the code, we will use
348 * them uniformly by (in the case of the ioctl single-file-descriptor
349 * implementation) filling them with copies of the control fd.
351 int status_fd
; /* File descriptor for /proc status file */
352 int as_fd
; /* File descriptor for /proc as file */
354 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
356 fltset_t saved_fltset
; /* Saved traced hardware fault set */
357 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
358 gdb_sigset_t saved_sighold
; /* Saved held signal set */
359 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
360 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
362 gdb_prstatus_t prstatus
; /* Current process status info */
365 gdb_fpregset_t fpregset
; /* Current floating point registers */
368 #ifdef DYNAMIC_SYSCALLS
369 int num_syscalls
; /* Total number of syscalls */
370 char **syscall_names
; /* Syscall number to name map */
373 struct procinfo
*thread_list
;
375 int status_valid
: 1;
377 int fpregs_valid
: 1;
378 int threads_valid
: 1;
381 static char errmsg
[128]; /* shared error msg buffer */
383 /* Function prototypes for procinfo module: */
385 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
386 static procinfo
*find_procinfo (int pid
, int tid
);
387 static procinfo
*create_procinfo (int pid
, int tid
);
388 static void destroy_procinfo (procinfo
* p
);
389 static void do_destroy_procinfo_cleanup (void *);
390 static void dead_procinfo (procinfo
* p
, char *msg
, int killp
);
391 static int open_procinfo_files (procinfo
* p
, int which
);
392 static void close_procinfo_files (procinfo
* p
);
393 static int sysset_t_size (procinfo
*p
);
394 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
395 #ifdef DYNAMIC_SYSCALLS
396 static void load_syscalls (procinfo
*pi
);
397 static void free_syscalls (procinfo
*pi
);
398 static int find_syscall (procinfo
*pi
, char *name
);
399 #endif /* DYNAMIC_SYSCALLS */
401 /* The head of the procinfo list: */
402 static procinfo
* procinfo_list
;
405 * Function: find_procinfo
407 * Search the procinfo list.
409 * Returns: pointer to procinfo, or NULL if not found.
413 find_procinfo (int pid
, int tid
)
417 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
424 /* Don't check threads_valid. If we're updating the
425 thread_list, we want to find whatever threads are already
426 here. This means that in general it is the caller's
427 responsibility to check threads_valid and update before
428 calling find_procinfo, if the caller wants to find a new
431 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
440 * Function: find_procinfo_or_die
442 * Calls find_procinfo, but errors on failure.
446 find_procinfo_or_die (int pid
, int tid
)
448 procinfo
*pi
= find_procinfo (pid
, tid
);
453 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
456 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
462 * Function: open_procinfo_files
464 * Open the file descriptor for the process or LWP.
465 * ifdef NEW_PROC_API, we only open the control file descriptor;
466 * the others are opened lazily as needed.
467 * else (if not NEW_PROC_API), there is only one real
468 * file descriptor, but we keep multiple copies of it so that
469 * the code that uses them does not have to be #ifdef'd.
471 * Return: file descriptor, or zero for failure.
474 enum { FD_CTL
, FD_STATUS
, FD_AS
};
477 open_procinfo_files (procinfo
*pi
, int which
)
480 char tmp
[MAX_PROC_NAME_SIZE
];
485 * This function is getting ALMOST long enough to break up into several.
486 * Here is some rationale:
488 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
489 * There are several file descriptors that may need to be open
490 * for any given process or LWP. The ones we're intereted in are:
491 * - control (ctl) write-only change the state
492 * - status (status) read-only query the state
493 * - address space (as) read/write access memory
494 * - map (map) read-only virtual addr map
495 * Most of these are opened lazily as they are needed.
496 * The pathnames for the 'files' for an LWP look slightly
497 * different from those of a first-class process:
498 * Pathnames for a process (<proc-id>):
499 * /proc/<proc-id>/ctl
500 * /proc/<proc-id>/status
502 * /proc/<proc-id>/map
503 * Pathnames for an LWP (lwp-id):
504 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
505 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
506 * An LWP has no map or address space file descriptor, since
507 * the memory map and address space are shared by all LWPs.
509 * Everyone else (Solaris 2.5, Irix, OSF)
510 * There is only one file descriptor for each process or LWP.
511 * For convenience, we copy the same file descriptor into all
512 * three fields of the procinfo struct (ctl_fd, status_fd, and
513 * as_fd, see NEW_PROC_API above) so that code that uses them
514 * doesn't need any #ifdef's.
519 * Each LWP has an independent file descriptor, but these
520 * are not obtained via the 'open' system call like the rest:
521 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
522 * to the file descriptor of the parent process.
525 * These do not even have their own independent file descriptor.
526 * All operations are carried out on the file descriptor of the
527 * parent process. Therefore we just call open again for each
528 * thread, getting a new handle for the same 'file'.
533 * In this case, there are several different file descriptors that
534 * we might be asked to open. The control file descriptor will be
535 * opened early, but the others will be opened lazily as they are
539 strcpy (tmp
, pi
->pathname
);
540 switch (which
) { /* which file descriptor to open? */
543 strcat (tmp
, "/lwpctl");
545 strcat (tmp
, "/ctl");
546 fd
= open (tmp
, O_WRONLY
);
553 return 0; /* there is no 'as' file descriptor for an lwp */
555 fd
= open (tmp
, O_RDWR
);
562 strcat (tmp
, "/lwpstatus");
564 strcat (tmp
, "/status");
565 fd
= open (tmp
, O_RDONLY
);
571 return 0; /* unknown file descriptor */
573 #else /* not NEW_PROC_API */
575 * In this case, there is only one file descriptor for each procinfo
576 * (ie. each process or LWP). In fact, only the file descriptor for
577 * the process can actually be opened by an 'open' system call.
578 * The ones for the LWPs have to be obtained thru an IOCTL call
579 * on the process's file descriptor.
581 * For convenience, we copy each procinfo's single file descriptor
582 * into all of the fields occupied by the several file descriptors
583 * of the NEW_PROC_API implementation. That way, the code that uses
584 * them can be written without ifdefs.
588 #ifdef PIOCTSTATUS /* OSF */
589 if ((fd
= open (pi
->pathname
, O_RDWR
)) == 0) /* Only one FD; just open it. */
591 #else /* Sol 2.5, Irix, other? */
592 if (pi
->tid
== 0) /* Master procinfo for the process */
594 fd
= open (pi
->pathname
, O_RDWR
);
598 else /* LWP thread procinfo */
600 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
604 /* Find the procinfo for the entire process. */
605 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
608 /* Now obtain the file descriptor for the LWP. */
609 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
611 #else /* Irix, other? */
612 return 0; /* Don't know how to open threads */
613 #endif /* Sol 2.5 PIOCOPENLWP */
615 #endif /* OSF PIOCTSTATUS */
616 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
617 #endif /* NEW_PROC_API */
619 return 1; /* success */
623 * Function: create_procinfo
625 * Allocate a data structure and link it into the procinfo list.
626 * (First tries to find a pre-existing one (FIXME: why?)
628 * Return: pointer to new procinfo struct.
632 create_procinfo (int pid
, int tid
)
634 procinfo
*pi
, *parent
;
636 if ((pi
= find_procinfo (pid
, tid
)))
637 return pi
; /* Already exists, nothing to do. */
639 /* find parent before doing malloc, to save having to cleanup */
641 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
643 doesn't exist yet? */
645 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
646 memset (pi
, 0, sizeof (procinfo
));
650 #ifdef DYNAMIC_SYSCALLS
654 /* Chain into list. */
657 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
658 pi
->next
= procinfo_list
;
664 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
666 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
668 pi
->next
= parent
->thread_list
;
669 parent
->thread_list
= pi
;
675 * Function: close_procinfo_files
677 * Close all file descriptors associated with the procinfo
681 close_procinfo_files (procinfo
*pi
)
688 if (pi
->status_fd
> 0)
689 close (pi
->status_fd
);
691 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
695 * Function: destroy_procinfo
697 * Destructor function. Close, unlink and deallocate the object.
701 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
705 /* Step one: unlink the procinfo from its list */
709 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
712 ptr
->next
= pi
->next
;
716 /* Step two: close any open file descriptors */
717 close_procinfo_files (pi
);
719 /* Step three: free the memory. */
720 #ifdef DYNAMIC_SYSCALLS
727 destroy_procinfo (procinfo
*pi
)
731 if (pi
->tid
!= 0) /* destroy a thread procinfo */
733 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
734 destroy_one_procinfo (&tmp
->thread_list
, pi
);
736 else /* destroy a process procinfo and all its threads */
738 /* First destroy the children, if any; */
739 while (pi
->thread_list
!= NULL
)
740 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
741 /* Then destroy the parent. Genocide!!! */
742 destroy_one_procinfo (&procinfo_list
, pi
);
747 do_destroy_procinfo_cleanup (void *pi
)
749 destroy_procinfo (pi
);
752 enum { NOKILL
, KILL
};
755 * Function: dead_procinfo
757 * To be called on a non_recoverable error for a procinfo.
758 * Prints error messages, optionally sends a SIGKILL to the process,
759 * then destroys the data structure.
763 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
769 print_sys_errmsg (pi
->pathname
, errno
);
773 sprintf (procfile
, "process %d", pi
->pid
);
774 print_sys_errmsg (procfile
, errno
);
777 kill (pi
->pid
, SIGKILL
);
779 destroy_procinfo (pi
);
784 * Function: sysset_t_size
786 * Returns the (complete) size of a sysset_t struct. Normally, this
787 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
788 * size of sysset_t isn't known until runtime.
792 sysset_t_size (procinfo
* pi
)
794 #ifndef DYNAMIC_SYSCALLS
795 return sizeof (sysset_t
);
797 return sizeof (sysset_t
) - sizeof (uint64_t)
798 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
799 / (8 * sizeof (uint64_t)));
803 /* Function: sysset_t_alloc
805 Allocate and (partially) initialize a sysset_t struct. */
808 sysset_t_alloc (procinfo
* pi
)
811 int size
= sysset_t_size (pi
);
812 ret
= xmalloc (size
);
813 #ifdef DYNAMIC_SYSCALLS
814 ret
->pr_size
= (pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
815 / (8 * sizeof (uint64_t));
820 #ifdef DYNAMIC_SYSCALLS
822 /* Function: load_syscalls
824 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
825 pi->num_syscalls with the number of syscalls and pi->syscall_names
826 with the names. (Certain numbers may be skipped in which case the
827 names for these numbers will be left as NULL.) */
829 #define MAX_SYSCALL_NAME_LENGTH 256
830 #define MAX_SYSCALLS 65536
833 load_syscalls (procinfo
*pi
)
835 char pathname
[MAX_PROC_NAME_SIZE
];
838 prsyscall_t
*syscalls
;
839 int i
, size
, maxcall
;
841 pi
->num_syscalls
= 0;
842 pi
->syscall_names
= 0;
844 /* Open the file descriptor for the sysent file */
845 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
846 sysent_fd
= open (pathname
, O_RDONLY
);
849 error ("load_syscalls: Can't open /proc/%d/sysent", pi
->pid
);
852 size
= sizeof header
- sizeof (prsyscall_t
);
853 if (read (sysent_fd
, &header
, size
) != size
)
855 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
858 if (header
.pr_nsyscalls
== 0)
860 error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi
->pid
);
863 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
864 syscalls
= xmalloc (size
);
866 if (read (sysent_fd
, syscalls
, size
) != size
)
869 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
872 /* Find maximum syscall number. This may not be the same as
873 pr_nsyscalls since that value refers to the number of entries
874 in the table. (Also, the docs indicate that some system
875 call numbers may be skipped.) */
877 maxcall
= syscalls
[0].pr_number
;
879 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
880 if (syscalls
[i
].pr_number
> maxcall
881 && syscalls
[i
].pr_nameoff
> 0
882 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
883 maxcall
= syscalls
[i
].pr_number
;
885 pi
->num_syscalls
= maxcall
+1;
886 pi
->syscall_names
= xmalloc (pi
->num_syscalls
* sizeof (char *));
888 for (i
= 0; i
< pi
->num_syscalls
; i
++)
889 pi
->syscall_names
[i
] = NULL
;
891 /* Read the syscall names in */
892 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
894 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
898 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
899 || syscalls
[i
].pr_number
< 0
900 || syscalls
[i
].pr_nameoff
<= 0
901 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
902 != (off_t
) syscalls
[i
].pr_nameoff
))
905 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
909 callnum
= syscalls
[i
].pr_number
;
911 if (pi
->syscall_names
[callnum
] != NULL
)
913 /* FIXME: Generate warning */
917 namebuf
[nread
-1] = '\0';
918 size
= strlen (namebuf
) + 1;
919 pi
->syscall_names
[callnum
] = xmalloc (size
);
920 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
921 pi
->syscall_names
[callnum
][size
-1] = '\0';
928 /* Function: free_syscalls
930 Free the space allocated for the syscall names from the procinfo
934 free_syscalls (procinfo
*pi
)
936 if (pi
->syscall_names
)
940 for (i
= 0; i
< pi
->num_syscalls
; i
++)
941 if (pi
->syscall_names
[i
] != NULL
)
942 xfree (pi
->syscall_names
[i
]);
944 xfree (pi
->syscall_names
);
945 pi
->syscall_names
= 0;
949 /* Function: find_syscall
951 Given a name, look up (and return) the corresponding syscall number.
952 If no match is found, return -1. */
955 find_syscall (procinfo
*pi
, char *name
)
958 for (i
= 0; i
< pi
->num_syscalls
; i
++)
960 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
967 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
969 /* =================== /proc "MODULE" =================== */
972 * This "module" is the interface layer between the /proc system API
973 * and the gdb target vector functions. This layer consists of
974 * access functions that encapsulate each of the basic operations
975 * that we need to use from the /proc API.
977 * The main motivation for this layer is to hide the fact that
978 * there are two very different implementations of the /proc API.
979 * Rather than have a bunch of #ifdefs all thru the gdb target vector
980 * functions, we do our best to hide them all in here.
983 int proc_get_status (procinfo
* pi
);
984 long proc_flags (procinfo
* pi
);
985 int proc_why (procinfo
* pi
);
986 int proc_what (procinfo
* pi
);
987 int proc_set_run_on_last_close (procinfo
* pi
);
988 int proc_unset_run_on_last_close (procinfo
* pi
);
989 int proc_set_inherit_on_fork (procinfo
* pi
);
990 int proc_unset_inherit_on_fork (procinfo
* pi
);
991 int proc_set_async (procinfo
* pi
);
992 int proc_unset_async (procinfo
* pi
);
993 int proc_stop_process (procinfo
* pi
);
994 int proc_trace_signal (procinfo
* pi
, int signo
);
995 int proc_ignore_signal (procinfo
* pi
, int signo
);
996 int proc_clear_current_fault (procinfo
* pi
);
997 int proc_set_current_signal (procinfo
* pi
, int signo
);
998 int proc_clear_current_signal (procinfo
* pi
);
999 int proc_set_gregs (procinfo
* pi
);
1000 int proc_set_fpregs (procinfo
* pi
);
1001 int proc_wait_for_stop (procinfo
* pi
);
1002 int proc_run_process (procinfo
* pi
, int step
, int signo
);
1003 int proc_kill (procinfo
* pi
, int signo
);
1004 int proc_parent_pid (procinfo
* pi
);
1005 int proc_get_nthreads (procinfo
* pi
);
1006 int proc_get_current_thread (procinfo
* pi
);
1007 int proc_set_held_signals (procinfo
* pi
, gdb_sigset_t
* sighold
);
1008 int proc_set_traced_sysexit (procinfo
* pi
, sysset_t
* sysset
);
1009 int proc_set_traced_sysentry (procinfo
* pi
, sysset_t
* sysset
);
1010 int proc_set_traced_faults (procinfo
* pi
, fltset_t
* fltset
);
1011 int proc_set_traced_signals (procinfo
* pi
, gdb_sigset_t
* sigset
);
1013 int proc_update_threads (procinfo
* pi
);
1014 int proc_iterate_over_threads (procinfo
* pi
,
1015 int (*func
) (procinfo
*, procinfo
*, void *),
1018 gdb_gregset_t
*proc_get_gregs (procinfo
* pi
);
1019 gdb_fpregset_t
*proc_get_fpregs (procinfo
* pi
);
1020 sysset_t
*proc_get_traced_sysexit (procinfo
* pi
, sysset_t
* save
);
1021 sysset_t
*proc_get_traced_sysentry (procinfo
* pi
, sysset_t
* save
);
1022 fltset_t
*proc_get_traced_faults (procinfo
* pi
, fltset_t
* save
);
1023 gdb_sigset_t
*proc_get_traced_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1024 gdb_sigset_t
*proc_get_held_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1025 gdb_sigset_t
*proc_get_pending_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1026 gdb_sigaction_t
*proc_get_signal_actions (procinfo
* pi
, gdb_sigaction_t
*save
);
1028 void proc_warn (procinfo
* pi
, char *func
, int line
);
1029 void proc_error (procinfo
* pi
, char *func
, int line
);
1032 proc_warn (procinfo
*pi
, char *func
, int line
)
1034 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1035 print_sys_errmsg (errmsg
, errno
);
1039 proc_error (procinfo
*pi
, char *func
, int line
)
1041 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1042 perror_with_name (errmsg
);
1046 * Function: proc_get_status
1048 * Updates the status struct in the procinfo.
1049 * There is a 'valid' flag, to let other functions know when
1050 * this function needs to be called (so the status is only
1051 * read when it is needed). The status file descriptor is
1052 * also only opened when it is needed.
1054 * Return: non-zero for success, zero for failure.
1058 proc_get_status (procinfo
*pi
)
1060 /* Status file descriptor is opened "lazily" */
1061 if (pi
->status_fd
== 0 &&
1062 open_procinfo_files (pi
, FD_STATUS
) == 0)
1064 pi
->status_valid
= 0;
1069 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1070 pi
->status_valid
= 0; /* fail */
1073 /* Sigh... I have to read a different data structure,
1074 depending on whether this is a main process or an LWP. */
1076 pi
->status_valid
= (read (pi
->status_fd
,
1077 (char *) &pi
->prstatus
.pr_lwp
,
1078 sizeof (lwpstatus_t
))
1079 == sizeof (lwpstatus_t
));
1082 pi
->status_valid
= (read (pi
->status_fd
,
1083 (char *) &pi
->prstatus
,
1084 sizeof (gdb_prstatus_t
))
1085 == sizeof (gdb_prstatus_t
));
1086 #if 0 /*def UNIXWARE*/
1087 if (pi
->status_valid
&&
1088 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
1089 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
1090 /* Unixware peculiarity -- read the damn thing again! */
1091 pi
->status_valid
= (read (pi
->status_fd
,
1092 (char *) &pi
->prstatus
,
1093 sizeof (gdb_prstatus_t
))
1094 == sizeof (gdb_prstatus_t
));
1095 #endif /* UNIXWARE */
1098 #else /* ioctl method */
1099 #ifdef PIOCTSTATUS /* osf */
1100 if (pi
->tid
== 0) /* main process */
1102 /* Just read the danged status. Now isn't that simple? */
1104 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1111 tid_t pr_error_thread
;
1112 struct prstatus status
;
1115 thread_status
.pr_count
= 1;
1116 thread_status
.status
.pr_tid
= pi
->tid
;
1117 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1120 memcpy (&pi
->prstatus
, &thread_status
.status
,
1121 sizeof (pi
->prstatus
));
1122 pi
->status_valid
= 1;
1126 /* Just read the danged status. Now isn't that simple? */
1127 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1131 if (pi
->status_valid
)
1133 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1136 proc_get_current_thread (pi
));
1139 /* The status struct includes general regs, so mark them valid too */
1140 pi
->gregs_valid
= pi
->status_valid
;
1142 /* In the read/write multiple-fd model,
1143 the status struct includes the fp regs too, so mark them valid too */
1144 pi
->fpregs_valid
= pi
->status_valid
;
1146 return pi
->status_valid
; /* True if success, false if failure. */
1150 * Function: proc_flags
1152 * returns the process flags (pr_flags field).
1156 proc_flags (procinfo
*pi
)
1158 if (!pi
->status_valid
)
1159 if (!proc_get_status (pi
))
1160 return 0; /* FIXME: not a good failure value (but what is?) */
1164 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1165 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1166 The two sets of flags don't overlap. */
1167 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
1169 return pi
->prstatus
.pr_lwp
.pr_flags
;
1172 return pi
->prstatus
.pr_flags
;
1177 * Function: proc_why
1179 * returns the pr_why field (why the process stopped).
1183 proc_why (procinfo
*pi
)
1185 if (!pi
->status_valid
)
1186 if (!proc_get_status (pi
))
1187 return 0; /* FIXME: not a good failure value (but what is?) */
1190 return pi
->prstatus
.pr_lwp
.pr_why
;
1192 return pi
->prstatus
.pr_why
;
1197 * Function: proc_what
1199 * returns the pr_what field (details of why the process stopped).
1203 proc_what (procinfo
*pi
)
1205 if (!pi
->status_valid
)
1206 if (!proc_get_status (pi
))
1207 return 0; /* FIXME: not a good failure value (but what is?) */
1210 return pi
->prstatus
.pr_lwp
.pr_what
;
1212 return pi
->prstatus
.pr_what
;
1216 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1218 * Function: proc_nsysarg
1220 * returns the pr_nsysarg field (number of args to the current syscall).
1224 proc_nsysarg (procinfo
*pi
)
1226 if (!pi
->status_valid
)
1227 if (!proc_get_status (pi
))
1231 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1233 return pi
->prstatus
.pr_nsysarg
;
1238 * Function: proc_sysargs
1240 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1244 proc_sysargs (procinfo
*pi
)
1246 if (!pi
->status_valid
)
1247 if (!proc_get_status (pi
))
1251 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1253 return (long *) &pi
->prstatus
.pr_sysarg
;
1258 * Function: proc_syscall
1260 * returns the pr_syscall field (id of current syscall if we are in one).
1264 proc_syscall (procinfo
*pi
)
1266 if (!pi
->status_valid
)
1267 if (!proc_get_status (pi
))
1271 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1273 return pi
->prstatus
.pr_syscall
;
1276 #endif /* PIOCSSPCACT */
1279 * Function: proc_cursig:
1281 * returns the pr_cursig field (current signal).
1285 proc_cursig (struct procinfo
*pi
)
1287 if (!pi
->status_valid
)
1288 if (!proc_get_status (pi
))
1289 return 0; /* FIXME: not a good failure value (but what is?) */
1292 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1294 return pi
->prstatus
.pr_cursig
;
1299 * Function: proc_modify_flag
1301 * === I appologize for the messiness of this function.
1302 * === This is an area where the different versions of
1303 * === /proc are more inconsistent than usual. MVS
1305 * Set or reset any of the following process flags:
1306 * PR_FORK -- forked child will inherit trace flags
1307 * PR_RLC -- traced process runs when last /proc file closed.
1308 * PR_KLC -- traced process is killed when last /proc file closed.
1309 * PR_ASYNC -- LWP's get to run/stop independently.
1311 * There are three methods for doing this function:
1312 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1314 * 2) Middle: PIOCSET/PIOCRESET
1316 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1319 * Note: Irix does not define PR_ASYNC.
1320 * Note: OSF does not define PR_KLC.
1321 * Note: OSF is the only one that can ONLY use the oldest method.
1324 * pi -- the procinfo
1325 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1326 * mode -- 1 for set, 0 for reset.
1328 * Returns non-zero for success, zero for failure.
1331 enum { FLAG_RESET
, FLAG_SET
};
1334 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1336 long win
= 0; /* default to fail */
1339 * These operations affect the process as a whole, and applying
1340 * them to an individual LWP has the same meaning as applying them
1341 * to the main process. Therefore, if we're ever called with a
1342 * pointer to an LWP's procinfo, let's substitute the process's
1343 * procinfo and avoid opening the LWP's file descriptor
1348 pi
= find_procinfo_or_die (pi
->pid
, 0);
1350 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1351 /* First normalize the PCUNSET/PCRESET command opcode
1352 (which for no obvious reason has a different definition
1353 from one operating system to the next...) */
1355 #define GDBRESET PCUNSET
1358 #define GDBRESET PCRESET
1362 procfs_ctl_t arg
[2];
1364 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1366 else /* Reset the flag */
1370 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1373 #ifdef PIOCSET /* Irix/Sol5 method */
1374 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1376 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1378 else /* Reset the flag */
1380 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1384 #ifdef PIOCSRLC /* Oldest method: OSF */
1387 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1389 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1391 else /* Clear run-on-last-close */
1393 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1397 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1399 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1401 else /* Clear inherit-on-fork */
1403 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1407 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1414 /* The above operation renders the procinfo's cached pstatus obsolete. */
1415 pi
->status_valid
= 0;
1418 warning ("procfs: modify_flag failed to turn %s %s",
1419 flag
== PR_FORK
? "PR_FORK" :
1420 flag
== PR_RLC
? "PR_RLC" :
1422 flag
== PR_ASYNC
? "PR_ASYNC" :
1425 flag
== PR_KLC
? "PR_KLC" :
1428 mode
== FLAG_RESET
? "off" : "on");
1434 * Function: proc_set_run_on_last_close
1436 * Set the run_on_last_close flag.
1437 * Process with all threads will become runnable
1438 * when debugger closes all /proc fds.
1440 * Returns non-zero for success, zero for failure.
1444 proc_set_run_on_last_close (procinfo
*pi
)
1446 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1450 * Function: proc_unset_run_on_last_close
1452 * Reset the run_on_last_close flag.
1453 * Process will NOT become runnable
1454 * when debugger closes its file handles.
1456 * Returns non-zero for success, zero for failure.
1460 proc_unset_run_on_last_close (procinfo
*pi
)
1462 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1467 * Function: proc_set_kill_on_last_close
1469 * Set the kill_on_last_close flag.
1470 * Process with all threads will be killed when debugger
1471 * closes all /proc fds (or debugger exits or dies).
1473 * Returns non-zero for success, zero for failure.
1477 proc_set_kill_on_last_close (procinfo
*pi
)
1479 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1483 * Function: proc_unset_kill_on_last_close
1485 * Reset the kill_on_last_close flag.
1486 * Process will NOT be killed when debugger
1487 * closes its file handles (or exits or dies).
1489 * Returns non-zero for success, zero for failure.
1493 proc_unset_kill_on_last_close (procinfo
*pi
)
1495 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1500 * Function: proc_set_inherit_on_fork
1502 * Set inherit_on_fork flag.
1503 * If the process forks a child while we are registered for events
1504 * in the parent, then we will also recieve events from the child.
1506 * Returns non-zero for success, zero for failure.
1510 proc_set_inherit_on_fork (procinfo
*pi
)
1512 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1516 * Function: proc_unset_inherit_on_fork
1518 * Reset inherit_on_fork flag.
1519 * If the process forks a child while we are registered for events
1520 * in the parent, then we will NOT recieve events from the child.
1522 * Returns non-zero for success, zero for failure.
1526 proc_unset_inherit_on_fork (procinfo
*pi
)
1528 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1533 * Function: proc_set_async
1535 * Set PR_ASYNC flag.
1536 * If one LWP stops because of a debug event (signal etc.),
1537 * the remaining LWPs will continue to run.
1539 * Returns non-zero for success, zero for failure.
1543 proc_set_async (procinfo
*pi
)
1545 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1549 * Function: proc_unset_async
1551 * Reset PR_ASYNC flag.
1552 * If one LWP stops because of a debug event (signal etc.),
1553 * then all other LWPs will stop as well.
1555 * Returns non-zero for success, zero for failure.
1559 proc_unset_async (procinfo
*pi
)
1561 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1563 #endif /* PR_ASYNC */
1566 * Function: proc_stop_process
1568 * Request the process/LWP to stop. Does not wait.
1569 * Returns non-zero for success, zero for failure.
1573 proc_stop_process (procinfo
*pi
)
1578 * We might conceivably apply this operation to an LWP, and
1579 * the LWP's ctl file descriptor might not be open.
1582 if (pi
->ctl_fd
== 0 &&
1583 open_procinfo_files (pi
, FD_CTL
) == 0)
1588 procfs_ctl_t cmd
= PCSTOP
;
1589 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1590 #else /* ioctl method */
1591 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1592 /* Note: the call also reads the prstatus. */
1595 pi
->status_valid
= 1;
1596 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1599 proc_get_current_thread (pi
));
1608 * Function: proc_wait_for_stop
1610 * Wait for the process or LWP to stop (block until it does).
1611 * Returns non-zero for success, zero for failure.
1615 proc_wait_for_stop (procinfo
*pi
)
1620 * We should never have to apply this operation to any procinfo
1621 * except the one for the main process. If that ever changes
1622 * for any reason, then take out the following clause and
1623 * replace it with one that makes sure the ctl_fd is open.
1627 pi
= find_procinfo_or_die (pi
->pid
, 0);
1631 procfs_ctl_t cmd
= PCWSTOP
;
1632 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1633 /* We been runnin' and we stopped -- need to update status. */
1634 pi
->status_valid
= 0;
1636 #else /* ioctl method */
1637 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1638 /* Above call also refreshes the prstatus. */
1641 pi
->status_valid
= 1;
1642 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1645 proc_get_current_thread (pi
));
1653 * Function: proc_run_process
1655 * Make the process or LWP runnable.
1656 * Options (not all are implemented):
1658 * - clear current fault
1659 * - clear current signal
1660 * - abort the current system call
1661 * - stop as soon as finished with system call
1662 * - (ioctl): set traced signal set
1663 * - (ioctl): set held signal set
1664 * - (ioctl): set traced fault set
1665 * - (ioctl): set start pc (vaddr)
1666 * Always clear the current fault.
1667 * Clear the current signal if 'signo' is zero.
1670 * pi the process or LWP to operate on.
1671 * step if true, set the process or LWP to trap after one instr.
1672 * signo if zero, clear the current signal if any.
1673 * if non-zero, set the current signal to this one.
1675 * Returns non-zero for success, zero for failure.
1679 proc_run_process (procinfo
*pi
, int step
, int signo
)
1685 * We will probably have to apply this operation to individual threads,
1686 * so make sure the control file descriptor is open.
1689 if (pi
->ctl_fd
== 0 &&
1690 open_procinfo_files (pi
, FD_CTL
) == 0)
1695 runflags
= PRCFAULT
; /* always clear current fault */
1700 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1701 proc_set_current_signal (pi
, signo
);
1705 procfs_ctl_t cmd
[2];
1709 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1711 #else /* ioctl method */
1715 memset (&prrun
, 0, sizeof (prrun
));
1716 prrun
.pr_flags
= runflags
;
1717 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1725 * Function: proc_set_traced_signals
1727 * Register to trace signals in the process or LWP.
1728 * Returns non-zero for success, zero for failure.
1732 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1737 * We should never have to apply this operation to any procinfo
1738 * except the one for the main process. If that ever changes
1739 * for any reason, then take out the following clause and
1740 * replace it with one that makes sure the ctl_fd is open.
1744 pi
= find_procinfo_or_die (pi
->pid
, 0);
1750 /* Use char array to avoid alignment issues. */
1751 char sigset
[sizeof (gdb_sigset_t
)];
1755 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1757 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1759 #else /* ioctl method */
1760 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1762 /* The above operation renders the procinfo's cached pstatus obsolete. */
1763 pi
->status_valid
= 0;
1766 warning ("procfs: set_traced_signals failed");
1771 * Function: proc_set_traced_faults
1773 * Register to trace hardware faults in the process or LWP.
1774 * Returns non-zero for success, zero for failure.
1778 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1783 * We should never have to apply this operation to any procinfo
1784 * except the one for the main process. If that ever changes
1785 * for any reason, then take out the following clause and
1786 * replace it with one that makes sure the ctl_fd is open.
1790 pi
= find_procinfo_or_die (pi
->pid
, 0);
1796 /* Use char array to avoid alignment issues. */
1797 char fltset
[sizeof (fltset_t
)];
1801 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1803 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1805 #else /* ioctl method */
1806 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1808 /* The above operation renders the procinfo's cached pstatus obsolete. */
1809 pi
->status_valid
= 0;
1815 * Function: proc_set_traced_sysentry
1817 * Register to trace entry to system calls in the process or LWP.
1818 * Returns non-zero for success, zero for failure.
1822 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1827 * We should never have to apply this operation to any procinfo
1828 * except the one for the main process. If that ever changes
1829 * for any reason, then take out the following clause and
1830 * replace it with one that makes sure the ctl_fd is open.
1834 pi
= find_procinfo_or_die (pi
->pid
, 0);
1838 struct gdb_proc_ctl_pcsentry
{
1840 /* Use char array to avoid alignment issues. */
1841 char sysset
[sizeof (sysset_t
)];
1843 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1845 + sysset_t_size (pi
);
1847 argp
= xmalloc (argp_size
);
1849 argp
->cmd
= PCSENTRY
;
1850 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1852 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1855 #else /* ioctl method */
1856 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1858 /* The above operation renders the procinfo's cached pstatus obsolete. */
1859 pi
->status_valid
= 0;
1865 * Function: proc_set_traced_sysexit
1867 * Register to trace exit from system calls in the process or LWP.
1868 * Returns non-zero for success, zero for failure.
1872 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1877 * We should never have to apply this operation to any procinfo
1878 * except the one for the main process. If that ever changes
1879 * for any reason, then take out the following clause and
1880 * replace it with one that makes sure the ctl_fd is open.
1884 pi
= find_procinfo_or_die (pi
->pid
, 0);
1888 struct gdb_proc_ctl_pcsexit
{
1890 /* Use char array to avoid alignment issues. */
1891 char sysset
[sizeof (sysset_t
)];
1893 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1895 + sysset_t_size (pi
);
1897 argp
= xmalloc (argp_size
);
1899 argp
->cmd
= PCSEXIT
;
1900 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1902 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1905 #else /* ioctl method */
1906 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1908 /* The above operation renders the procinfo's cached pstatus obsolete. */
1909 pi
->status_valid
= 0;
1915 * Function: proc_set_held_signals
1917 * Specify the set of blocked / held signals in the process or LWP.
1918 * Returns non-zero for success, zero for failure.
1922 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1927 * We should never have to apply this operation to any procinfo
1928 * except the one for the main process. If that ever changes
1929 * for any reason, then take out the following clause and
1930 * replace it with one that makes sure the ctl_fd is open.
1934 pi
= find_procinfo_or_die (pi
->pid
, 0);
1940 /* Use char array to avoid alignment issues. */
1941 char hold
[sizeof (gdb_sigset_t
)];
1945 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1946 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1949 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1951 /* The above operation renders the procinfo's cached pstatus obsolete. */
1952 pi
->status_valid
= 0;
1958 * Function: proc_get_pending_signals
1960 * returns the set of signals that are pending in the process or LWP.
1961 * Will also copy the sigset if 'save' is non-zero.
1965 proc_get_pending_signals (procinfo
*pi
, gdb_sigset_t
*save
)
1967 gdb_sigset_t
*ret
= NULL
;
1970 * We should never have to apply this operation to any procinfo
1971 * except the one for the main process. If that ever changes
1972 * for any reason, then take out the following clause and
1973 * replace it with one that makes sure the ctl_fd is open.
1977 pi
= find_procinfo_or_die (pi
->pid
, 0);
1979 if (!pi
->status_valid
)
1980 if (!proc_get_status (pi
))
1984 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
1986 ret
= &pi
->prstatus
.pr_sigpend
;
1989 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
1995 * Function: proc_get_signal_actions
1997 * returns the set of signal actions.
1998 * Will also copy the sigactionset if 'save' is non-zero.
2002 proc_get_signal_actions (procinfo
*pi
, gdb_sigaction_t
*save
)
2004 gdb_sigaction_t
*ret
= NULL
;
2007 * We should never have to apply this operation to any procinfo
2008 * except the one for the main process. If that ever changes
2009 * for any reason, then take out the following clause and
2010 * replace it with one that makes sure the ctl_fd is open.
2014 pi
= find_procinfo_or_die (pi
->pid
, 0);
2016 if (!pi
->status_valid
)
2017 if (!proc_get_status (pi
))
2021 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
2023 ret
= &pi
->prstatus
.pr_action
;
2026 memcpy (save
, ret
, sizeof (gdb_sigaction_t
));
2032 * Function: proc_get_held_signals
2034 * returns the set of signals that are held / blocked.
2035 * Will also copy the sigset if 'save' is non-zero.
2039 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2041 gdb_sigset_t
*ret
= NULL
;
2044 * We should never have to apply this operation to any procinfo
2045 * except the one for the main process. If that ever changes
2046 * for any reason, then take out the following clause and
2047 * replace it with one that makes sure the ctl_fd is open.
2051 pi
= find_procinfo_or_die (pi
->pid
, 0);
2054 if (!pi
->status_valid
)
2055 if (!proc_get_status (pi
))
2059 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
2061 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
2062 #endif /* UNIXWARE */
2063 #else /* not NEW_PROC_API */
2065 static gdb_sigset_t sigheld
;
2067 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
2070 #endif /* NEW_PROC_API */
2072 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2078 * Function: proc_get_traced_signals
2080 * returns the set of signals that are traced / debugged.
2081 * Will also copy the sigset if 'save' is non-zero.
2085 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2087 gdb_sigset_t
*ret
= NULL
;
2090 * We should never have to apply this operation to any procinfo
2091 * except the one for the main process. If that ever changes
2092 * for any reason, then take out the following clause and
2093 * replace it with one that makes sure the ctl_fd is open.
2097 pi
= find_procinfo_or_die (pi
->pid
, 0);
2100 if (!pi
->status_valid
)
2101 if (!proc_get_status (pi
))
2104 ret
= &pi
->prstatus
.pr_sigtrace
;
2107 static gdb_sigset_t sigtrace
;
2109 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
2114 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2120 * Function: proc_trace_signal
2122 * Add 'signo' to the set of signals that are traced.
2123 * Returns non-zero for success, zero for failure.
2127 proc_trace_signal (procinfo
*pi
, int signo
)
2132 * We should never have to apply this operation to any procinfo
2133 * except the one for the main process. If that ever changes
2134 * for any reason, then take out the following clause and
2135 * replace it with one that makes sure the ctl_fd is open.
2139 pi
= find_procinfo_or_die (pi
->pid
, 0);
2143 if (proc_get_traced_signals (pi
, &temp
))
2145 praddset (&temp
, signo
);
2146 return proc_set_traced_signals (pi
, &temp
);
2150 return 0; /* failure */
2154 * Function: proc_ignore_signal
2156 * Remove 'signo' from the set of signals that are traced.
2157 * Returns non-zero for success, zero for failure.
2161 proc_ignore_signal (procinfo
*pi
, int signo
)
2166 * We should never have to apply this operation to any procinfo
2167 * except the one for the main process. If that ever changes
2168 * for any reason, then take out the following clause and
2169 * replace it with one that makes sure the ctl_fd is open.
2173 pi
= find_procinfo_or_die (pi
->pid
, 0);
2177 if (proc_get_traced_signals (pi
, &temp
))
2179 prdelset (&temp
, signo
);
2180 return proc_set_traced_signals (pi
, &temp
);
2184 return 0; /* failure */
2188 * Function: proc_get_traced_faults
2190 * returns the set of hardware faults that are traced /debugged.
2191 * Will also copy the faultset if 'save' is non-zero.
2195 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
2197 fltset_t
*ret
= NULL
;
2200 * We should never have to apply this operation to any procinfo
2201 * except the one for the main process. If that ever changes
2202 * for any reason, then take out the following clause and
2203 * replace it with one that makes sure the ctl_fd is open.
2207 pi
= find_procinfo_or_die (pi
->pid
, 0);
2210 if (!pi
->status_valid
)
2211 if (!proc_get_status (pi
))
2214 ret
= &pi
->prstatus
.pr_flttrace
;
2217 static fltset_t flttrace
;
2219 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2224 memcpy (save
, ret
, sizeof (fltset_t
));
2230 * Function: proc_get_traced_sysentry
2232 * returns the set of syscalls that are traced /debugged on entry.
2233 * Will also copy the syscall set if 'save' is non-zero.
2237 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
2239 sysset_t
*ret
= NULL
;
2242 * We should never have to apply this operation to any procinfo
2243 * except the one for the main process. If that ever changes
2244 * for any reason, then take out the following clause and
2245 * replace it with one that makes sure the ctl_fd is open.
2249 pi
= find_procinfo_or_die (pi
->pid
, 0);
2252 if (!pi
->status_valid
)
2253 if (!proc_get_status (pi
))
2256 #ifndef DYNAMIC_SYSCALLS
2257 ret
= &pi
->prstatus
.pr_sysentry
;
2258 #else /* DYNAMIC_SYSCALLS */
2260 static sysset_t
*sysentry
;
2264 sysentry
= sysset_t_alloc (pi
);
2266 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2268 if (pi
->prstatus
.pr_sysentry_offset
== 0)
2270 gdb_premptysysset (sysentry
);
2276 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
2278 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
2280 size
= sysset_t_size (pi
);
2281 gdb_premptysysset (sysentry
);
2282 rsize
= read (pi
->status_fd
, sysentry
, size
);
2287 #endif /* DYNAMIC_SYSCALLS */
2288 #else /* !NEW_PROC_API */
2290 static sysset_t sysentry
;
2292 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2295 #endif /* NEW_PROC_API */
2297 memcpy (save
, ret
, sysset_t_size (pi
));
2303 * Function: proc_get_traced_sysexit
2305 * returns the set of syscalls that are traced /debugged on exit.
2306 * Will also copy the syscall set if 'save' is non-zero.
2310 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
2312 sysset_t
* ret
= NULL
;
2315 * We should never have to apply this operation to any procinfo
2316 * except the one for the main process. If that ever changes
2317 * for any reason, then take out the following clause and
2318 * replace it with one that makes sure the ctl_fd is open.
2322 pi
= find_procinfo_or_die (pi
->pid
, 0);
2325 if (!pi
->status_valid
)
2326 if (!proc_get_status (pi
))
2329 #ifndef DYNAMIC_SYSCALLS
2330 ret
= &pi
->prstatus
.pr_sysexit
;
2331 #else /* DYNAMIC_SYSCALLS */
2333 static sysset_t
*sysexit
;
2337 sysexit
= sysset_t_alloc (pi
);
2339 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2341 if (pi
->prstatus
.pr_sysexit_offset
== 0)
2343 gdb_premptysysset (sysexit
);
2349 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
, SEEK_SET
)
2350 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2352 size
= sysset_t_size (pi
);
2353 gdb_premptysysset (sysexit
);
2354 rsize
= read (pi
->status_fd
, sysexit
, size
);
2359 #endif /* DYNAMIC_SYSCALLS */
2362 static sysset_t sysexit
;
2364 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2369 memcpy (save
, ret
, sysset_t_size (pi
));
2375 * Function: proc_clear_current_fault
2377 * The current fault (if any) is cleared; the associated signal
2378 * will not be sent to the process or LWP when it resumes.
2379 * Returns non-zero for success, zero for failure.
2383 proc_clear_current_fault (procinfo
*pi
)
2388 * We should never have to apply this operation to any procinfo
2389 * except the one for the main process. If that ever changes
2390 * for any reason, then take out the following clause and
2391 * replace it with one that makes sure the ctl_fd is open.
2395 pi
= find_procinfo_or_die (pi
->pid
, 0);
2399 procfs_ctl_t cmd
= PCCFAULT
;
2400 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2403 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2410 * Function: proc_set_current_signal
2412 * Set the "current signal" that will be delivered next to the process.
2413 * NOTE: semantics are different from those of KILL.
2414 * This signal will be delivered to the process or LWP
2415 * immediately when it is resumed (even if the signal is held/blocked);
2416 * it will NOT immediately cause another event of interest, and will NOT
2417 * first trap back to the debugger.
2419 * Returns non-zero for success, zero for failure.
2423 proc_set_current_signal (procinfo
*pi
, int signo
)
2428 /* Use char array to avoid alignment issues. */
2429 char sinfo
[sizeof (gdb_siginfo_t
)];
2431 gdb_siginfo_t
*mysinfo
;
2434 * We should never have to apply this operation to any procinfo
2435 * except the one for the main process. If that ever changes
2436 * for any reason, then take out the following clause and
2437 * replace it with one that makes sure the ctl_fd is open.
2441 pi
= find_procinfo_or_die (pi
->pid
, 0);
2443 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2444 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2445 * receives a PIOCSSIG with a signal identical to the current signal,
2446 * it messes up the current signal. Work around the kernel bug.
2449 signo
== proc_cursig (pi
))
2450 return 1; /* I assume this is a success? */
2453 /* The pointer is just a type alias. */
2454 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2455 mysinfo
->si_signo
= signo
;
2456 mysinfo
->si_code
= 0;
2457 mysinfo
->si_pid
= getpid (); /* ?why? */
2458 mysinfo
->si_uid
= getuid (); /* ?why? */
2462 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2464 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2471 * Function: proc_clear_current_signal
2473 * The current signal (if any) is cleared, and
2474 * is not sent to the process or LWP when it resumes.
2475 * Returns non-zero for success, zero for failure.
2479 proc_clear_current_signal (procinfo
*pi
)
2484 * We should never have to apply this operation to any procinfo
2485 * except the one for the main process. If that ever changes
2486 * for any reason, then take out the following clause and
2487 * replace it with one that makes sure the ctl_fd is open.
2491 pi
= find_procinfo_or_die (pi
->pid
, 0);
2497 /* Use char array to avoid alignment issues. */
2498 char sinfo
[sizeof (gdb_siginfo_t
)];
2500 gdb_siginfo_t
*mysinfo
;
2503 /* The pointer is just a type alias. */
2504 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2505 mysinfo
->si_signo
= 0;
2506 mysinfo
->si_code
= 0;
2507 mysinfo
->si_errno
= 0;
2508 mysinfo
->si_pid
= getpid (); /* ?why? */
2509 mysinfo
->si_uid
= getuid (); /* ?why? */
2511 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2514 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2521 * Function: proc_get_gregs
2523 * Get the general registers for the process or LWP.
2524 * Returns non-zero for success, zero for failure.
2528 proc_get_gregs (procinfo
*pi
)
2530 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2531 if (!proc_get_status (pi
))
2535 * OK, sorry about the ifdef's.
2536 * There's three cases instead of two, because
2537 * in this instance Unixware and Solaris/RW differ.
2541 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2542 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2543 #else /* not Unixware */
2544 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2545 #endif /* Unixware */
2546 #else /* not NEW_PROC_API */
2547 return &pi
->prstatus
.pr_reg
;
2548 #endif /* NEW_PROC_API */
2552 * Function: proc_get_fpregs
2554 * Get the floating point registers for the process or LWP.
2555 * Returns non-zero for success, zero for failure.
2559 proc_get_fpregs (procinfo
*pi
)
2562 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2563 if (!proc_get_status (pi
))
2566 #ifdef UNIXWARE /* a true architecture dependency */
2567 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2569 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2570 #endif /* Unixware */
2572 #else /* not NEW_PROC_API */
2573 if (pi
->fpregs_valid
)
2574 return &pi
->fpregset
; /* already got 'em */
2577 if (pi
->ctl_fd
== 0 &&
2578 open_procinfo_files (pi
, FD_CTL
) == 0)
2587 tid_t pr_error_thread
;
2588 tfpregset_t thread_1
;
2591 thread_fpregs
.pr_count
= 1;
2592 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2595 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2597 pi
->fpregs_valid
= 1;
2598 return &pi
->fpregset
; /* got 'em now! */
2600 else if (pi
->tid
!= 0 &&
2601 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2603 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2604 sizeof (pi
->fpregset
));
2605 pi
->fpregs_valid
= 1;
2606 return &pi
->fpregset
; /* got 'em now! */
2613 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2615 pi
->fpregs_valid
= 1;
2616 return &pi
->fpregset
; /* got 'em now! */
2629 * Function: proc_set_gregs
2631 * Write the general registers back to the process or LWP.
2632 * Returns non-zero for success, zero for failure.
2636 proc_set_gregs (procinfo
*pi
)
2638 gdb_gregset_t
*gregs
;
2641 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2642 return 0; /* get_regs has already warned */
2644 if (pi
->ctl_fd
== 0 &&
2645 open_procinfo_files (pi
, FD_CTL
) == 0)
2654 /* Use char array to avoid alignment issues. */
2655 char gregs
[sizeof (gdb_gregset_t
)];
2659 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2660 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2662 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2666 /* Policy: writing the regs invalidates our cache. */
2667 pi
->gregs_valid
= 0;
2672 * Function: proc_set_fpregs
2674 * Modify the floating point register set of the process or LWP.
2675 * Returns non-zero for success, zero for failure.
2679 proc_set_fpregs (procinfo
*pi
)
2681 gdb_fpregset_t
*fpregs
;
2684 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2685 return 0; /* get_fpregs has already warned */
2687 if (pi
->ctl_fd
== 0 &&
2688 open_procinfo_files (pi
, FD_CTL
) == 0)
2697 /* Use char array to avoid alignment issues. */
2698 char fpregs
[sizeof (gdb_fpregset_t
)];
2702 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2703 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2707 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2712 tid_t pr_error_thread
;
2713 tfpregset_t thread_1
;
2716 thread_fpregs
.pr_count
= 1;
2717 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2718 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2720 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2723 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2724 #endif /* osf PIOCTSFPREG */
2725 #endif /* NEW_PROC_API */
2728 /* Policy: writing the regs invalidates our cache. */
2729 pi
->fpregs_valid
= 0;
2734 * Function: proc_kill
2736 * Send a signal to the proc or lwp with the semantics of "kill()".
2737 * Returns non-zero for success, zero for failure.
2741 proc_kill (procinfo
*pi
, int signo
)
2746 * We might conceivably apply this operation to an LWP, and
2747 * the LWP's ctl file descriptor might not be open.
2750 if (pi
->ctl_fd
== 0 &&
2751 open_procinfo_files (pi
, FD_CTL
) == 0)
2758 procfs_ctl_t cmd
[2];
2762 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2763 #else /* ioctl method */
2764 /* FIXME: do I need the Alpha OSF fixups present in
2765 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2766 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2774 * Function: proc_parent_pid
2776 * Find the pid of the process that started this one.
2777 * Returns the parent process pid, or zero.
2781 proc_parent_pid (procinfo
*pi
)
2784 * We should never have to apply this operation to any procinfo
2785 * except the one for the main process. If that ever changes
2786 * for any reason, then take out the following clause and
2787 * replace it with one that makes sure the ctl_fd is open.
2791 pi
= find_procinfo_or_die (pi
->pid
, 0);
2793 if (!pi
->status_valid
)
2794 if (!proc_get_status (pi
))
2797 return pi
->prstatus
.pr_ppid
;
2802 * Function: proc_set_watchpoint
2807 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2809 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2812 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2813 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2818 char watch
[sizeof (prwatch_t
)];
2822 pwatch
= (prwatch_t
*) &arg
.watch
;
2823 pwatch
->pr_vaddr
= address_to_host_pointer (addr
);
2824 pwatch
->pr_size
= len
;
2825 pwatch
->pr_wflags
= wflags
;
2826 #if defined(NEW_PROC_API) && defined (PCWATCH)
2828 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2830 #if defined (PIOCSWATCH)
2831 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2833 return 0; /* Fail */
2841 * Function: proc_iterate_over_mappings
2843 * Given a pointer to a function, call that function once for every
2844 * mapped address space in the process. The callback function
2845 * receives an open file descriptor for the file corresponding to
2846 * that mapped address space (if there is one), and the base address
2847 * of the mapped space. Quit when the callback function returns a
2848 * nonzero value, or at teh end of the mappings.
2850 * Returns: the first non-zero return value of the callback function,
2854 /* FIXME: it's probably a waste to cache this FD.
2855 It doesn't get called that often... and if I open it
2856 every time, I don't need to lseek it. */
2858 proc_iterate_over_mappings (int (*func
) (int, CORE_ADDR
))
2862 #ifndef NEW_PROC_API /* avoid compiler warning */
2867 char pathname
[MAX_PROC_NAME_SIZE
];
2872 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
2876 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
2877 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
2878 proc_error (pi
, "proc_iterate_over_mappings (open)", __LINE__
);
2880 /* Make sure it gets closed again. */
2881 make_cleanup_close (map_fd
);
2883 /* Allocate space for mapping (lifetime only for this function). */
2884 map
= alloca (sizeof (struct prmap
));
2886 /* Now read the mappings from the file,
2887 open a file descriptor for those that have a name,
2888 and call the callback function. */
2889 while (read (map_fd
,
2891 sizeof (struct prmap
)) == sizeof (struct prmap
))
2893 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
2895 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
2898 if (map
->pr_mapname
[0] == 0)
2900 fd
= -1; /* no map file */
2904 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
2905 /* Note: caller's responsibility to close this fd! */
2906 fd
= open (name
, O_RDONLY
);
2907 /* Note: we don't test the above call for failure;
2908 we just pass the FD on as given. Sometimes there is
2909 no file, so the ioctl may return failure, but that's
2913 /* Stop looping if the callback returns non-zero. */
2914 if ((funcstat
= (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
)) != 0)
2918 /* Get the number of mapping entries. */
2919 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmaps
) < 0)
2920 proc_error (pi
, "proc_iterate_over_mappings (PIOCNMAP)", __LINE__
);
2922 /* Allocate space for mappings (lifetime only this function). */
2923 map
= (struct prmap
*) alloca ((nmaps
+ 1) * sizeof (struct prmap
));
2925 /* Read in all the mappings. */
2926 if (ioctl (pi
->ctl_fd
, PIOCMAP
, map
) < 0)
2927 proc_error (pi
, "proc_iterate_over_mappings (PIOCMAP)", __LINE__
);
2929 /* Now loop through the mappings, open an fd for each, and
2930 call the callback function. */
2932 i
< nmaps
&& map
[i
].pr_size
!= 0;
2935 /* Note: caller's responsibility to close this fd! */
2936 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
[i
].pr_vaddr
);
2937 /* Note: we don't test the above call for failure;
2938 we just pass the FD on as given. Sometimes there is
2939 no file, so the ioctl may return failure, but that's
2942 /* Stop looping if the callback returns non-zero. */
2943 funcstat
= (*func
) (fd
, host_pointer_to_address (map
[i
].pr_vaddr
));
2952 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2954 #include <sys/sysi86.h>
2957 * Function: proc_get_LDT_entry
2963 * The 'key' is actually the value of the lower 16 bits of
2964 * the GS register for the LWP that we're interested in.
2966 * Return: matching ssh struct (LDT entry).
2970 proc_get_LDT_entry (procinfo
*pi
, int key
)
2972 static struct ssd
*ldt_entry
= NULL
;
2974 char pathname
[MAX_PROC_NAME_SIZE
];
2975 struct cleanup
*old_chain
= NULL
;
2978 /* Allocate space for one LDT entry.
2979 This alloc must persist, because we return a pointer to it. */
2980 if (ldt_entry
== NULL
)
2981 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2983 /* Open the file descriptor for the LDT table. */
2984 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2985 if ((fd
= open (pathname
, O_RDONLY
)) < 0)
2987 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2990 /* Make sure it gets closed again! */
2991 old_chain
= make_cleanup_close (fd
);
2993 /* Now 'read' thru the table, find a match and return it. */
2994 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2996 if (ldt_entry
->sel
== 0 &&
2997 ldt_entry
->bo
== 0 &&
2998 ldt_entry
->acc1
== 0 &&
2999 ldt_entry
->acc2
== 0)
3000 break; /* end of table */
3001 /* If key matches, return this entry. */
3002 if (ldt_entry
->sel
== key
)
3005 /* Loop ended, match not found. */
3009 static int nalloc
= 0;
3011 /* Get the number of LDT entries. */
3012 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
3014 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
3018 /* Allocate space for the number of LDT entries. */
3019 /* This alloc has to persist, 'cause we return a pointer to it. */
3022 ldt_entry
= (struct ssd
*)
3023 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
3027 /* Read the whole table in one gulp. */
3028 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
3030 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
3034 /* Search the table and return the (first) entry matching 'key'. */
3035 for (i
= 0; i
< nldt
; i
++)
3036 if (ldt_entry
[i
].sel
== key
)
3037 return &ldt_entry
[i
];
3039 /* Loop ended, match not found. */
3044 #endif /* TM_I386SOL2_H */
3046 /* =============== END, non-thread part of /proc "MODULE" =============== */
3048 /* =================== Thread "MODULE" =================== */
3050 /* NOTE: you'll see more ifdefs and duplication of functions here,
3051 since there is a different way to do threads on every OS. */
3054 * Function: proc_get_nthreads
3056 * Return the number of threads for the process
3059 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3064 proc_get_nthreads (procinfo
*pi
)
3068 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
3069 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
3075 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3077 * Solaris and Unixware version
3080 proc_get_nthreads (procinfo
*pi
)
3082 if (!pi
->status_valid
)
3083 if (!proc_get_status (pi
))
3087 * NEW_PROC_API: only works for the process procinfo,
3088 * because the LWP procinfos do not get prstatus filled in.
3091 if (pi
->tid
!= 0) /* find the parent process procinfo */
3092 pi
= find_procinfo_or_die (pi
->pid
, 0);
3094 return pi
->prstatus
.pr_nlwp
;
3102 proc_get_nthreads (procinfo
*pi
)
3110 * Function: proc_get_current_thread (LWP version)
3112 * Return the ID of the thread that had an event of interest.
3113 * (ie. the one that hit a breakpoint or other traced event).
3114 * All other things being equal, this should be the ID of a
3115 * thread that is currently executing.
3118 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3120 * Solaris and Unixware version
3123 proc_get_current_thread (procinfo
*pi
)
3126 * Note: this should be applied to the root procinfo for the process,
3127 * not to the procinfo for an LWP. If applied to the procinfo for
3128 * an LWP, it will simply return that LWP's ID. In that case,
3129 * find the parent process procinfo.
3133 pi
= find_procinfo_or_die (pi
->pid
, 0);
3135 if (!pi
->status_valid
)
3136 if (!proc_get_status (pi
))
3140 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
3142 return pi
->prstatus
.pr_who
;
3147 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3152 proc_get_current_thread (procinfo
*pi
)
3154 #if 0 /* FIXME: not ready for prime time? */
3155 return pi
->prstatus
.pr_tid
;
3166 proc_get_current_thread (procinfo
*pi
)
3175 * Function: proc_update_threads
3177 * Discover the IDs of all the threads within the process, and
3178 * create a procinfo for each of them (chained to the parent).
3180 * This unfortunately requires a different method on every OS.
3182 * Return: non-zero for success, zero for failure.
3186 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
3188 if (thread
&& parent
) /* sanity */
3190 thread
->status_valid
= 0;
3191 if (!proc_get_status (thread
))
3192 destroy_one_procinfo (&parent
->thread_list
, thread
);
3194 return 0; /* keep iterating */
3197 #if defined (PIOCLSTATUS)
3199 * Solaris 2.5 (ioctl) version
3202 proc_update_threads (procinfo
*pi
)
3204 gdb_prstatus_t
*prstatus
;
3205 struct cleanup
*old_chain
= NULL
;
3210 * We should never have to apply this operation to any procinfo
3211 * except the one for the main process. If that ever changes
3212 * for any reason, then take out the following clause and
3213 * replace it with one that makes sure the ctl_fd is open.
3217 pi
= find_procinfo_or_die (pi
->pid
, 0);
3219 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3221 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
3222 return 1; /* Process is not multi-threaded; nothing to do. */
3224 prstatus
= xmalloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1));
3226 old_chain
= make_cleanup (xfree
, prstatus
);
3227 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
3228 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
3230 /* Skip element zero, which represents the process as a whole. */
3231 for (i
= 1; i
< nlwp
+ 1; i
++)
3233 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
3234 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3236 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
3237 thread
->status_valid
= 1;
3239 pi
->threads_valid
= 1;
3240 do_cleanups (old_chain
);
3246 * Unixware and Solaris 6 (and later) version
3249 do_closedir_cleanup (void *dir
)
3255 proc_update_threads (procinfo
*pi
)
3257 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3258 struct dirent
*direntry
;
3259 struct cleanup
*old_chain
= NULL
;
3265 * We should never have to apply this operation to any procinfo
3266 * except the one for the main process. If that ever changes
3267 * for any reason, then take out the following clause and
3268 * replace it with one that makes sure the ctl_fd is open.
3272 pi
= find_procinfo_or_die (pi
->pid
, 0);
3274 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3279 * Note: this brute-force method is the only way I know of
3280 * to accomplish this task on Unixware. This method will
3281 * also work on Solaris 2.6 and 2.7. There is a much simpler
3282 * and more elegant way to do this on Solaris, but the margins
3283 * of this manuscript are too small to write it here... ;-)
3286 strcpy (pathname
, pi
->pathname
);
3287 strcat (pathname
, "/lwp");
3288 if ((dirp
= opendir (pathname
)) == NULL
)
3289 proc_error (pi
, "update_threads, opendir", __LINE__
);
3291 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
3292 while ((direntry
= readdir (dirp
)) != NULL
)
3293 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3295 lwpid
= atoi (&direntry
->d_name
[0]);
3296 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3297 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3299 pi
->threads_valid
= 1;
3300 do_cleanups (old_chain
);
3309 proc_update_threads (procinfo
*pi
)
3315 * We should never have to apply this operation to any procinfo
3316 * except the one for the main process. If that ever changes
3317 * for any reason, then take out the following clause and
3318 * replace it with one that makes sure the ctl_fd is open.
3322 pi
= find_procinfo_or_die (pi
->pid
, 0);
3324 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3326 nthreads
= proc_get_nthreads (pi
);
3328 return 0; /* nothing to do for 1 or fewer threads */
3330 threads
= xmalloc (nthreads
* sizeof (tid_t
));
3332 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3333 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3335 for (i
= 0; i
< nthreads
; i
++)
3337 if (!find_procinfo (pi
->pid
, threads
[i
]))
3338 if (!create_procinfo (pi
->pid
, threads
[i
]))
3339 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3341 pi
->threads_valid
= 1;
3349 proc_update_threads (procinfo
*pi
)
3353 #endif /* OSF PIOCTLIST */
3354 #endif /* NEW_PROC_API */
3355 #endif /* SOL 2.5 PIOCLSTATUS */
3358 * Function: proc_iterate_over_threads
3361 * Given a pointer to a function, call that function once
3362 * for each lwp in the procinfo list, until the function
3363 * returns non-zero, in which event return the value
3364 * returned by the function.
3366 * Note: this function does NOT call update_threads.
3367 * If you want to discover new threads first, you must
3368 * call that function explicitly. This function just makes
3369 * a quick pass over the currently-known procinfos.
3372 * pi - parent process procinfo
3373 * func - per-thread function
3374 * ptr - opaque parameter for function.
3377 * First non-zero return value from the callee, or zero.
3381 proc_iterate_over_threads (procinfo
*pi
,
3382 int (*func
) (procinfo
*, procinfo
*, void *),
3385 procinfo
*thread
, *next
;
3389 * We should never have to apply this operation to any procinfo
3390 * except the one for the main process. If that ever changes
3391 * for any reason, then take out the following clause and
3392 * replace it with one that makes sure the ctl_fd is open.
3396 pi
= find_procinfo_or_die (pi
->pid
, 0);
3398 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3400 next
= thread
->next
; /* in case thread is destroyed */
3401 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3408 /* =================== END, Thread "MODULE" =================== */
3410 /* =================== END, /proc "MODULE" =================== */
3412 /* =================== GDB "MODULE" =================== */
3415 * Here are all of the gdb target vector functions and their friends.
3418 static int do_attach (int pid
);
3419 static void do_detach (int signo
);
3420 static int register_gdb_signals (procinfo
*, gdb_sigset_t
*);
3423 * Function: procfs_debug_inferior
3425 * Sets up the inferior to be debugged.
3426 * Registers to trace signals, hardware faults, and syscalls.
3427 * Note: does not set RLC flag: caller may want to customize that.
3429 * Returns: zero for success (note! unlike most functions in this module)
3430 * On failure, returns the LINE NUMBER where it failed!
3434 procfs_debug_inferior (procinfo
*pi
)
3436 fltset_t traced_faults
;
3437 gdb_sigset_t traced_signals
;
3438 sysset_t
*traced_syscall_entries
;
3439 sysset_t
*traced_syscall_exits
;
3442 #ifdef PROCFS_DONT_TRACE_FAULTS
3443 /* On some systems (OSF), we don't trace hardware faults.
3444 Apparently it's enough that we catch them as signals.
3445 Wonder why we don't just do that in general? */
3446 premptyset (&traced_faults
); /* don't trace faults. */
3448 /* Register to trace hardware faults in the child. */
3449 prfillset (&traced_faults
); /* trace all faults... */
3450 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3452 if (!proc_set_traced_faults (pi
, &traced_faults
))
3455 /* Register to trace selected signals in the child. */
3456 premptyset (&traced_signals
);
3457 if (!register_gdb_signals (pi
, &traced_signals
))
3461 /* Register to trace the 'exit' system call (on entry). */
3462 traced_syscall_entries
= sysset_t_alloc (pi
);
3463 gdb_premptysysset (traced_syscall_entries
);
3465 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
3468 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3471 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
3473 #ifdef DYNAMIC_SYSCALLS
3475 int callnum
= find_syscall (pi
, "_exit");
3477 gdb_praddsysset (traced_syscall_entries
, callnum
);
3481 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
3482 xfree (traced_syscall_entries
);
3486 #ifdef PRFS_STOPEXEC /* defined on OSF */
3487 /* OSF method for tracing exec syscalls. Quoting:
3488 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3489 exits from exec system calls because of the user level loader. */
3490 /* FIXME: make nice and maybe move into an access function. */
3494 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3497 prfs_flags
|= PRFS_STOPEXEC
;
3499 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3502 #else /* not PRFS_STOPEXEC */
3503 /* Everyone else's (except OSF) method for tracing exec syscalls */
3505 Not all systems with /proc have all the exec* syscalls with the same
3506 names. On the SGI, for example, there is no SYS_exec, but there
3507 *is* a SYS_execv. So, we try to account for that. */
3509 traced_syscall_exits
= sysset_t_alloc (pi
);
3510 gdb_premptysysset (traced_syscall_exits
);
3512 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
3515 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
3518 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
3521 #ifdef SYS_lwpcreate
3522 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3523 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3526 #ifdef SYS_lwp_create /* FIXME: once only, please */
3527 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3528 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3531 #ifdef DYNAMIC_SYSCALLS
3533 int callnum
= find_syscall (pi
, "execve");
3535 gdb_praddsysset (traced_syscall_exits
, callnum
);
3536 callnum
= find_syscall (pi
, "ra_execve");
3538 gdb_praddsysset (traced_syscall_exits
, callnum
);
3542 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3543 xfree (traced_syscall_exits
);
3547 #endif /* PRFS_STOPEXEC */
3552 procfs_attach (char *args
, int from_tty
)
3558 error_no_arg ("process-id to attach");
3561 if (pid
== getpid ())
3562 error ("Attaching GDB to itself is not a good idea...");
3566 exec_file
= get_exec_file (0);
3569 printf_filtered ("Attaching to program `%s', %s\n",
3570 exec_file
, target_pid_to_str (pid
));
3572 printf_filtered ("Attaching to %s\n", target_pid_to_str (pid
));
3576 inferior_pid
= do_attach (pid
);
3577 push_target (&procfs_ops
);
3581 procfs_detach (char *args
, int from_tty
)
3588 exec_file
= get_exec_file (0);
3591 printf_filtered ("Detaching from program: %s %s\n",
3592 exec_file
, target_pid_to_str (inferior_pid
));
3596 signo
= atoi (args
);
3600 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3609 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
3610 perror ("procfs: out of memory in 'attach'");
3612 if (!open_procinfo_files (pi
, FD_CTL
))
3614 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3615 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3617 dead_procinfo (pi
, errmsg
, NOKILL
);
3620 /* Stop the process (if it isn't already stopped). */
3621 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3623 pi
->was_stopped
= 1;
3624 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3628 pi
->was_stopped
= 0;
3629 /* Set the process to run again when we close it. */
3630 if (!proc_set_run_on_last_close (pi
))
3631 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3633 /* Now stop the process. */
3634 if (!proc_stop_process (pi
))
3635 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3636 pi
->ignore_next_sigstop
= 1;
3638 /* Save some of the /proc state to be restored if we detach. */
3639 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3640 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3641 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3642 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3643 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3644 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3646 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3647 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3649 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3650 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3652 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3653 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3655 /* Let GDB know that the inferior was attached. */
3657 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3661 do_detach (int signo
)
3665 /* Find procinfo for the main process */
3666 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0); /* FIXME: threads */
3668 if (!proc_set_current_signal (pi
, signo
))
3669 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3671 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3672 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3674 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3675 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3677 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3678 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3680 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3681 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3683 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3684 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3686 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3687 if (signo
|| !(pi
->was_stopped
) ||
3688 query ("Was stopped when attached, make it runnable again? "))
3690 /* Clear any pending signal. */
3691 if (!proc_clear_current_fault (pi
))
3692 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3694 if (!proc_set_run_on_last_close (pi
))
3695 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3699 destroy_procinfo (pi
);
3705 * Since the /proc interface cannot give us individual registers,
3706 * we pay no attention to the (regno) argument, and just fetch them all.
3707 * This results in the possibility that we will do unnecessarily many
3708 * fetches, since we may be called repeatedly for individual registers.
3709 * So we cache the results, and mark the cache invalid when the process
3714 procfs_fetch_registers (int regno
)
3716 gdb_fpregset_t
*fpregs
;
3717 gdb_gregset_t
*gregs
;
3722 pid
= PIDGET (inferior_pid
);
3723 tid
= TIDGET (inferior_pid
);
3725 /* First look up procinfo for the main process. */
3726 pi
= find_procinfo_or_die (pid
, 0);
3728 /* If the event thread is not the same as GDB's requested thread
3729 (ie. inferior_pid), then look up procinfo for the requested
3732 (tid
!= proc_get_current_thread (pi
)))
3733 pi
= find_procinfo_or_die (pid
, tid
);
3736 error ("procfs: fetch_registers failed to find procinfo for %s",
3737 target_pid_to_str (inferior_pid
));
3739 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3740 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3742 supply_gregset (gregs
);
3744 if (FP0_REGNUM
>= 0) /* need floating point? */
3746 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3747 regno
== PC_REGNUM
||
3748 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3749 regno
== FP_REGNUM
||
3751 return; /* not a floating point register */
3753 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3754 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3756 supply_fpregset (fpregs
);
3760 /* Get ready to modify the registers array. On machines which store
3761 individual registers, this doesn't need to do anything. On
3762 machines which store all the registers in one fell swoop, such as
3763 /proc, this makes sure that registers contains all the registers
3764 from the program being debugged. */
3767 procfs_prepare_to_store (void)
3769 #ifdef CHILD_PREPARE_TO_STORE
3770 CHILD_PREPARE_TO_STORE ();
3777 * Since the /proc interface will not read individual registers,
3778 * we will cache these requests until the process is resumed, and
3779 * only then write them back to the inferior process.
3781 * FIXME: is that a really bad idea? Have to think about cases
3782 * where writing one register might affect the value of others, etc.
3786 procfs_store_registers (int regno
)
3788 gdb_fpregset_t
*fpregs
;
3789 gdb_gregset_t
*gregs
;
3794 pid
= PIDGET (inferior_pid
);
3795 tid
= TIDGET (inferior_pid
);
3797 /* First find procinfo for main process */
3798 pi
= find_procinfo_or_die (pid
, 0);
3800 /* If current lwp for process is not the same as requested thread
3801 (ie. inferior_pid), then find procinfo for the requested thread. */
3804 (tid
!= proc_get_current_thread (pi
)))
3805 pi
= find_procinfo_or_die (pid
, tid
);
3808 error ("procfs: store_registers: failed to find procinfo for %s",
3809 target_pid_to_str (inferior_pid
));
3811 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3812 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3814 fill_gregset (gregs
, regno
);
3815 if (!proc_set_gregs (pi
))
3816 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3818 if (FP0_REGNUM
>= 0) /* need floating point? */
3820 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3821 regno
== PC_REGNUM
||
3822 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3823 regno
== FP_REGNUM
||
3825 return; /* not a floating point register */
3827 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3828 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3830 fill_fpregset (fpregs
, regno
);
3831 if (!proc_set_fpregs (pi
))
3832 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3837 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3841 if (scall
== SYS_lwp_exit
)
3845 if (scall
== SYS_lwpexit
)
3852 syscall_is_exit (procinfo
*pi
, int scall
)
3855 if (scall
== SYS_exit
)
3858 #ifdef DYNAMIC_SYSCALLS
3859 if (find_syscall (pi
, "_exit") == scall
)
3866 syscall_is_exec (procinfo
*pi
, int scall
)
3869 if (scall
== SYS_exec
)
3873 if (scall
== SYS_execv
)
3877 if (scall
== SYS_execve
)
3880 #ifdef DYNAMIC_SYSCALLS
3881 if (find_syscall (pi
, "_execve"))
3883 if (find_syscall (pi
, "ra_execve"))
3890 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3892 #ifdef SYS_lwp_create
3893 if (scall
== SYS_lwp_create
)
3896 #ifdef SYS_lwpcreate
3897 if (scall
== SYS_lwpcreate
)
3904 * Function: target_wait
3906 * Retrieve the next stop event from the child process.
3907 * If child has not stopped yet, wait for it to stop.
3908 * Translate /proc eventcodes (or possibly wait eventcodes)
3909 * into gdb internal event codes.
3911 * Return: id of process (and possibly thread) that incurred the event.
3912 * event codes are returned thru a pointer parameter.
3916 procfs_wait (int pid
, struct target_waitstatus
*status
)
3918 /* First cut: loosely based on original version 2.1 */
3922 int why
, what
, flags
;
3931 /* Find procinfo for main process */
3932 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
3935 /* We must assume that the status is stale now... */
3936 pi
->status_valid
= 0;
3937 pi
->gregs_valid
= 0;
3938 pi
->fpregs_valid
= 0;
3940 #if 0 /* just try this out... */
3941 flags
= proc_flags (pi
);
3942 why
= proc_why (pi
);
3943 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3944 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3946 /* If child is not stopped, wait for it to stop. */
3947 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3948 !proc_wait_for_stop (pi
))
3950 /* wait_for_stop failed: has the child terminated? */
3951 if (errno
== ENOENT
)
3953 /* /proc file not found; presumably child has terminated. */
3954 retval
= wait (&wstat
); /* "wait" for the child's exit */
3956 if (retval
!= PIDGET (inferior_pid
)) /* wrong child? */
3957 error ("procfs: couldn't stop process %d: wait returned %d\n",
3958 inferior_pid
, retval
);
3959 /* FIXME: might I not just use waitpid?
3960 Or try find_procinfo to see if I know about this child? */
3964 /* Unknown error from wait_for_stop. */
3965 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3970 /* This long block is reached if either:
3971 a) the child was already stopped, or
3972 b) we successfully waited for the child with wait_for_stop.
3973 This block will analyze the /proc status, and translate it
3974 into a waitstatus for GDB.
3976 If we actually had to call wait because the /proc file
3977 is gone (child terminated), then we skip this block,
3978 because we already have a waitstatus. */
3980 flags
= proc_flags (pi
);
3981 why
= proc_why (pi
);
3982 what
= proc_what (pi
);
3984 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3987 /* If it's running async (for single_thread control),
3988 set it back to normal again. */
3989 if (flags
& PR_ASYNC
)
3990 if (!proc_unset_async (pi
))
3991 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3995 proc_prettyprint_why (why
, what
, 1);
3997 /* The 'pid' we will return to GDB is composed of
3998 the process ID plus the lwp ID. */
3999 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4003 wstat
= (what
<< 8) | 0177;
4006 if (syscall_is_lwp_exit (pi
, what
))
4008 printf_filtered ("[%s exited]\n",
4009 target_pid_to_str (retval
));
4010 delete_thread (retval
);
4011 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4014 else if (syscall_is_exit (pi
, what
))
4016 /* Handle SYS_exit call only */
4017 /* Stopped at entry to SYS_exit.
4018 Make it runnable, resume it, then use
4019 the wait system call to get its exit code.
4020 Proc_run_process always clears the current
4022 Then return its exit status. */
4023 pi
->status_valid
= 0;
4025 /* FIXME: what we should do is return
4026 TARGET_WAITKIND_SPURIOUS. */
4027 if (!proc_run_process (pi
, 0, 0))
4028 proc_error (pi
, "target_wait, run_process", __LINE__
);
4031 /* Don't call wait: simulate waiting for exit,
4032 return a "success" exit code. Bogus: what if
4033 it returns something else? */
4035 retval
= inferior_pid
; /* ? ? ? */
4039 int temp
= wait (&wstat
);
4041 /* FIXME: shouldn't I make sure I get the right
4042 event from the right process? If (for
4043 instance) I have killed an earlier inferior
4044 process but failed to clean up after it
4045 somehow, I could get its termination event
4048 /* If wait returns -1, that's what we return to GDB. */
4055 printf_filtered ("procfs: trapped on entry to ");
4056 proc_prettyprint_syscall (proc_what (pi
), 0);
4057 printf_filtered ("\n");
4060 long i
, nsysargs
, *sysargs
;
4062 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4063 (sysargs
= proc_sysargs (pi
)) != NULL
)
4065 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4066 for (i
= 0; i
< nsysargs
; i
++)
4067 printf_filtered ("#%ld: 0x%08lx\n",
4075 /* How to exit gracefully, returning "unknown event" */
4076 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4077 return inferior_pid
;
4081 /* How to keep going without returning to wfi: */
4082 target_resume (pid
, 0, TARGET_SIGNAL_0
);
4088 if (syscall_is_exec (pi
, what
))
4090 /* Hopefully this is our own "fork-child" execing
4091 the real child. Hoax this event into a trap, and
4092 GDB will see the child about to execute its start
4094 wstat
= (SIGTRAP
<< 8) | 0177;
4096 else if (syscall_is_lwp_create (pi
, what
))
4099 * This syscall is somewhat like fork/exec.
4100 * We will get the event twice: once for the parent LWP,
4101 * and once for the child. We should already know about
4102 * the parent LWP, but the child will be new to us. So,
4103 * whenever we get this event, if it represents a new
4104 * thread, simply add the thread to the list.
4107 /* If not in procinfo list, add it. */
4108 temp
= proc_get_current_thread (pi
);
4109 if (!find_procinfo (pi
->pid
, temp
))
4110 create_procinfo (pi
->pid
, temp
);
4112 temp
= MERGEPID (pi
->pid
, temp
);
4113 /* If not in GDB's thread list, add it. */
4114 if (!in_thread_list (temp
))
4116 printf_filtered ("[New %s]\n", target_pid_to_str (temp
));
4119 /* Return to WFI, but tell it to immediately resume. */
4120 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4121 return inferior_pid
;
4123 else if (syscall_is_lwp_exit (pi
, what
))
4125 printf_filtered ("[%s exited]\n",
4126 target_pid_to_str (retval
));
4127 delete_thread (retval
);
4128 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4133 /* FIXME: Do we need to handle SYS_sproc,
4134 SYS_fork, or SYS_vfork here? The old procfs
4135 seemed to use this event to handle threads on
4136 older (non-LWP) systems, where I'm assuming
4137 that threads were actually separate processes.
4138 Irix, maybe? Anyway, low priority for now. */
4142 printf_filtered ("procfs: trapped on exit from ");
4143 proc_prettyprint_syscall (proc_what (pi
), 0);
4144 printf_filtered ("\n");
4147 long i
, nsysargs
, *sysargs
;
4149 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4150 (sysargs
= proc_sysargs (pi
)) != NULL
)
4152 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4153 for (i
= 0; i
< nsysargs
; i
++)
4154 printf_filtered ("#%ld: 0x%08lx\n",
4159 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4160 return inferior_pid
;
4165 wstat
= (SIGSTOP
<< 8) | 0177;
4170 printf_filtered ("Retry #%d:\n", retry
);
4171 pi
->status_valid
= 0;
4176 /* If not in procinfo list, add it. */
4177 temp
= proc_get_current_thread (pi
);
4178 if (!find_procinfo (pi
->pid
, temp
))
4179 create_procinfo (pi
->pid
, temp
);
4181 /* If not in GDB's thread list, add it. */
4182 temp
= MERGEPID (pi
->pid
, temp
);
4183 if (!in_thread_list (temp
))
4185 printf_filtered ("[New %s]\n",
4186 target_pid_to_str (temp
));
4190 status
->kind
= TARGET_WAITKIND_STOPPED
;
4191 status
->value
.sig
= 0;
4196 wstat
= (what
<< 8) | 0177;
4199 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
4202 wstat
= (SIGTRAP
<< 8) | 0177;
4207 wstat
= (SIGTRAP
<< 8) | 0177;
4210 /* FIXME: use si_signo where possible. */
4212 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4215 wstat
= (SIGILL
<< 8) | 0177;
4218 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4221 wstat
= (SIGTRAP
<< 8) | 0177;
4225 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4228 wstat
= (SIGSEGV
<< 8) | 0177;
4232 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4235 wstat
= (SIGFPE
<< 8) | 0177;
4237 case FLTPAGE
: /* Recoverable page fault */
4238 default: /* FIXME: use si_signo if possible for fault */
4240 printf_filtered ("procfs:%d -- ", __LINE__
);
4241 printf_filtered ("child stopped for unknown reason:\n");
4242 proc_prettyprint_why (why
, what
, 1);
4243 error ("... giving up...");
4246 break; /* case PR_FAULTED: */
4247 default: /* switch (why) unmatched */
4248 printf_filtered ("procfs:%d -- ", __LINE__
);
4249 printf_filtered ("child stopped for unknown reason:\n");
4250 proc_prettyprint_why (why
, what
, 1);
4251 error ("... giving up...");
4255 * Got this far without error:
4256 * If retval isn't in the threads database, add it.
4259 retval
!= inferior_pid
&&
4260 !in_thread_list (retval
))
4263 * We have a new thread.
4264 * We need to add it both to GDB's list and to our own.
4265 * If we don't create a procinfo, resume may be unhappy
4268 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
4269 add_thread (retval
);
4270 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
4271 create_procinfo (PIDGET (retval
), TIDGET (retval
));
4273 /* In addition, it's possible that this is the first
4274 * new thread we've seen, in which case we may not
4275 * have created entries for inferior_pid yet.
4277 if (TIDGET (inferior_pid
) != 0)
4279 if (!in_thread_list (inferior_pid
))
4280 add_thread (inferior_pid
);
4281 if (find_procinfo (PIDGET (inferior_pid
),
4282 TIDGET (inferior_pid
)) == NULL
)
4283 create_procinfo (PIDGET (inferior_pid
),
4284 TIDGET (inferior_pid
));
4288 else /* flags do not indicate STOPPED */
4290 /* surely this can't happen... */
4291 printf_filtered ("procfs:%d -- process not stopped.\n",
4293 proc_prettyprint_flags (flags
, 1);
4294 error ("procfs: ...giving up...");
4299 store_waitstatus (status
, wstat
);
4305 /* Transfer LEN bytes between GDB address MYADDR and target address
4306 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4307 otherwise transfer them from the target. TARGET is unused.
4309 The return value is 0 if an error occurred or no bytes were
4310 transferred. Otherwise, it will be a positive value which
4311 indicates the number of bytes transferred between gdb and the
4312 target. (Note that the interface also makes provisions for
4313 negative values, but this capability isn't implemented here.) */
4316 procfs_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int dowrite
,
4317 struct mem_attrib
*attrib
, struct target_ops
*target
)
4322 /* Find procinfo for main process */
4323 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4324 if (pi
->as_fd
== 0 &&
4325 open_procinfo_files (pi
, FD_AS
) == 0)
4327 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4331 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4336 PROCFS_NOTE ("write memory: ");
4338 PROCFS_NOTE ("write memory: \n");
4340 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4344 PROCFS_NOTE ("read memory: \n");
4345 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4356 * Function: invalidate_cache
4358 * Called by target_resume before making child runnable.
4359 * Mark cached registers and status's invalid.
4360 * If there are "dirty" caches that need to be written back
4361 * to the child process, do that.
4363 * File descriptors are also cached.
4364 * As they are a limited resource, we cannot hold onto them indefinitely.
4365 * However, as they are expensive to open, we don't want to throw them
4366 * away indescriminately either. As a compromise, we will keep the
4367 * file descriptors for the parent process, but discard any file
4368 * descriptors we may have accumulated for the threads.
4371 * As this function is called by iterate_over_threads, it always
4372 * returns zero (so that iterate_over_threads will keep iterating).
4377 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4380 * About to run the child; invalidate caches and do any other cleanup.
4384 if (pi
->gregs_dirty
)
4385 if (parent
== NULL
||
4386 proc_get_current_thread (parent
) != pi
->tid
)
4387 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4388 proc_warn (pi
, "target_resume, set_gregs",
4390 if (FP0_REGNUM
>= 0)
4391 if (pi
->fpregs_dirty
)
4392 if (parent
== NULL
||
4393 proc_get_current_thread (parent
) != pi
->tid
)
4394 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4395 proc_warn (pi
, "target_resume, set_fpregs",
4401 /* The presence of a parent indicates that this is an LWP.
4402 Close any file descriptors that it might have open.
4403 We don't do this to the master (parent) procinfo. */
4405 close_procinfo_files (pi
);
4407 pi
->gregs_valid
= 0;
4408 pi
->fpregs_valid
= 0;
4410 pi
->gregs_dirty
= 0;
4411 pi
->fpregs_dirty
= 0;
4413 pi
->status_valid
= 0;
4414 pi
->threads_valid
= 0;
4421 * Function: make_signal_thread_runnable
4423 * A callback function for iterate_over_threads.
4424 * Find the asynchronous signal thread, and make it runnable.
4425 * See if that helps matters any.
4429 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4432 if (proc_flags (pi
) & PR_ASLWP
)
4434 if (!proc_run_process (pi
, 0, -1))
4435 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4444 * Function: target_resume
4446 * Make the child process runnable. Normally we will then call
4447 * procfs_wait and wait for it to stop again (unles gdb is async).
4450 * step: if true, then arrange for the child to stop again
4451 * after executing a single instruction.
4452 * signo: if zero, then cancel any pending signal.
4453 * If non-zero, then arrange for the indicated signal
4454 * to be delivered to the child when it runs.
4455 * pid: if -1, then allow any child thread to run.
4456 * if non-zero, then allow only the indicated thread to run.
4457 ******* (not implemented yet)
4461 procfs_resume (int pid
, int step
, enum target_signal signo
)
4463 procinfo
*pi
, *thread
;
4467 prrun.prflags |= PRSVADDR;
4468 prrun.pr_vaddr = $PC; set resume address
4469 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4470 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4471 prrun.prflags |= PRCFAULT; clear current fault.
4473 PRSTRACE and PRSFAULT can be done by other means
4474 (proc_trace_signals, proc_trace_faults)
4475 PRSVADDR is unnecessary.
4476 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4477 This basically leaves PRSTEP and PRCSIG.
4478 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4479 So basically PR_STEP is the sole argument that must be passed
4480 to proc_run_process (for use in the prrun struct by ioctl). */
4482 /* Find procinfo for main process */
4483 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4485 /* First cut: ignore pid argument */
4488 /* Convert signal to host numbering. */
4490 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4493 native_signo
= target_signal_to_host (signo
);
4495 pi
->ignore_next_sigstop
= 0;
4497 /* Running the process voids all cached registers and status. */
4498 /* Void the threads' caches first */
4499 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4500 /* Void the process procinfo's caches. */
4501 invalidate_cache (NULL
, pi
, NULL
);
4505 /* Resume a specific thread, presumably suppressing the others. */
4506 thread
= find_procinfo (PIDGET (pid
), TIDGET (pid
));
4508 warning ("procfs: resume can't find thread %d -- resuming all.",
4512 if (thread
->tid
!= 0)
4514 /* We're to resume a specific thread, and not the others.
4515 * Set the child process's PR_ASYNC flag.
4518 if (!proc_set_async (pi
))
4519 proc_error (pi
, "target_resume, set_async", __LINE__
);
4522 proc_iterate_over_threads (pi
,
4523 make_signal_thread_runnable
,
4526 pi
= thread
; /* substitute the thread's procinfo for run */
4531 if (!proc_run_process (pi
, step
, native_signo
))
4534 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4536 proc_error (pi
, "target_resume", __LINE__
);
4541 * Function: register_gdb_signals
4543 * Traverse the list of signals that GDB knows about
4544 * (see "handle" command), and arrange for the target
4545 * to be stopped or not, according to these settings.
4547 * Returns non-zero for success, zero for failure.
4551 register_gdb_signals (procinfo
*pi
, gdb_sigset_t
*signals
)
4555 for (signo
= 0; signo
< NSIG
; signo
++)
4556 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4557 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4558 signal_pass_state (target_signal_from_host (signo
)) == 1)
4559 prdelset (signals
, signo
);
4561 praddset (signals
, signo
);
4563 return proc_set_traced_signals (pi
, signals
);
4567 * Function: target_notice_signals
4569 * Set up to trace signals in the child process.
4573 procfs_notice_signals (int pid
)
4575 gdb_sigset_t signals
;
4576 procinfo
*pi
= find_procinfo_or_die (PIDGET (pid
), 0);
4578 if (proc_get_traced_signals (pi
, &signals
) &&
4579 register_gdb_signals (pi
, &signals
))
4582 proc_error (pi
, "notice_signals", __LINE__
);
4586 * Function: target_files_info
4588 * Print status information about the child process.
4592 procfs_files_info (struct target_ops
*ignore
)
4594 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4595 attach_flag
? "attached": "child",
4596 target_pid_to_str (inferior_pid
));
4600 * Function: target_open
4602 * A dummy: you don't open procfs.
4606 procfs_open (char *args
, int from_tty
)
4608 error ("Use the \"run\" command to start a Unix child process.");
4612 * Function: target_can_run
4614 * This tells GDB that this target vector can be invoked
4615 * for "run" or "attach".
4618 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4619 be a runnable target. Used by targets
4620 that can sit atop procfs, such as solaris
4625 procfs_can_run (void)
4627 /* This variable is controlled by modules that sit atop procfs that
4628 may layer their own process structure atop that provided here.
4629 sol-thread.c does this because of the Solaris two-level thread
4632 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4634 return !procfs_suppress_run
;
4638 * Function: target_stop
4640 * Stop the child process asynchronously, as when the
4641 * gdb user types control-c or presses a "stop" button.
4643 * Works by sending kill(SIGINT) to the child's process group.
4649 extern pid_t inferior_process_group
;
4651 kill (-inferior_process_group
, SIGINT
);
4655 * Function: unconditionally_kill_inferior
4657 * Make it die. Wait for it to die. Clean up after it.
4658 * Note: this should only be applied to the real process,
4659 * not to an LWP, because of the check for parent-process.
4660 * If we need this to work for an LWP, it needs some more logic.
4664 unconditionally_kill_inferior (procinfo
*pi
)
4668 parent_pid
= proc_parent_pid (pi
);
4669 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4670 /* FIXME: use access functions */
4671 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4672 before the PIOCKILL, otherwise it might generate a corrupted core
4673 file for the inferior. */
4674 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4676 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4679 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4680 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4681 to kill the inferior, otherwise it might remain stopped with a
4683 We do not check the result of the PIOCSSIG, the inferior might have
4686 gdb_siginfo_t newsiginfo
;
4688 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4689 newsiginfo
.si_signo
= SIGKILL
;
4690 newsiginfo
.si_code
= 0;
4691 newsiginfo
.si_errno
= 0;
4692 newsiginfo
.si_pid
= getpid ();
4693 newsiginfo
.si_uid
= getuid ();
4694 /* FIXME: use proc_set_current_signal */
4695 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4697 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4698 if (!proc_kill (pi
, SIGKILL
))
4699 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4700 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4701 destroy_procinfo (pi
);
4703 /* If pi is GDB's child, wait for it to die. */
4704 if (parent_pid
== getpid ())
4705 /* FIXME: should we use waitpid to make sure we get the right event?
4706 Should we check the returned event? */
4711 ret
= waitpid (pi
->pid
, &status
, 0);
4719 * Function: target_kill_inferior
4721 * We're done debugging it, and we want it to go away.
4722 * Then we want GDB to forget all about it.
4726 procfs_kill_inferior (void)
4728 if (inferior_pid
!= 0) /* ? */
4730 /* Find procinfo for main process */
4731 procinfo
*pi
= find_procinfo (PIDGET (inferior_pid
), 0);
4734 unconditionally_kill_inferior (pi
);
4735 target_mourn_inferior ();
4740 * Function: target_mourn_inferior
4742 * Forget we ever debugged this thing!
4746 procfs_mourn_inferior (void)
4750 if (inferior_pid
!= 0)
4752 /* Find procinfo for main process */
4753 pi
= find_procinfo (PIDGET (inferior_pid
), 0);
4755 destroy_procinfo (pi
);
4757 unpush_target (&procfs_ops
);
4758 generic_mourn_inferior ();
4762 * Function: init_inferior
4764 * When GDB forks to create a runnable inferior process,
4765 * this function is called on the parent side of the fork.
4766 * It's job is to do whatever is necessary to make the child
4767 * ready to be debugged, and then wait for the child to synchronize.
4771 procfs_init_inferior (int pid
)
4774 gdb_sigset_t signals
;
4777 /* This routine called on the parent side (GDB side)
4778 after GDB forks the inferior. */
4780 push_target (&procfs_ops
);
4782 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4783 perror ("procfs: out of memory in 'init_inferior'");
4785 if (!open_procinfo_files (pi
, FD_CTL
))
4786 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4790 open_procinfo_files // done
4793 procfs_notice_signals
4800 /* If not stopped yet, wait for it to stop. */
4801 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4802 !(proc_wait_for_stop (pi
)))
4803 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4805 /* Save some of the /proc state to be restored if we detach. */
4806 /* FIXME: Why? In case another debugger was debugging it?
4807 We're it's parent, for Ghu's sake! */
4808 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4809 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4810 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4811 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4812 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4813 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4814 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4815 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4816 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4817 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4819 /* Register to trace selected signals in the child. */
4820 prfillset (&signals
);
4821 if (!register_gdb_signals (pi
, &signals
))
4822 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4824 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4825 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4827 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4828 and possibly even turning ON kill-on-last-close at this point. But
4829 I can't make that change without careful testing which I don't have
4830 time to do right now... */
4831 /* Turn on run-on-last-close flag so that the child
4832 will die if GDB goes away for some reason. */
4833 if (!proc_set_run_on_last_close (pi
))
4834 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4836 /* The 'process ID' we return to GDB is composed of
4837 the actual process ID plus the lwp ID. */
4838 inferior_pid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4840 #ifdef START_INFERIOR_TRAPS_EXPECTED
4841 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4843 /* One trap to exec the shell, one to exec the program being debugged. */
4844 startup_inferior (2);
4845 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4849 * Function: set_exec_trap
4851 * When GDB forks to create a new process, this function is called
4852 * on the child side of the fork before GDB exec's the user program.
4853 * Its job is to make the child minimally debuggable, so that the
4854 * parent GDB process can connect to the child and take over.
4855 * This function should do only the minimum to make that possible,
4856 * and to synchronize with the parent process. The parent process
4857 * should take care of the details.
4861 procfs_set_exec_trap (void)
4863 /* This routine called on the child side (inferior side)
4864 after GDB forks the inferior. It must use only local variables,
4865 because it may be sharing data space with its parent. */
4870 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4871 perror_with_name ("procfs: create_procinfo failed in child.");
4873 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4875 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4876 gdb_flush (gdb_stderr
);
4877 /* no need to call "dead_procinfo", because we're going to exit. */
4881 #ifdef PRFS_STOPEXEC /* defined on OSF */
4882 /* OSF method for tracing exec syscalls. Quoting:
4883 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4884 exits from exec system calls because of the user level loader. */
4885 /* FIXME: make nice and maybe move into an access function. */
4889 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4891 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4892 gdb_flush (gdb_stderr
);
4895 prfs_flags
|= PRFS_STOPEXEC
;
4897 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4899 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4900 gdb_flush (gdb_stderr
);
4904 #else /* not PRFS_STOPEXEC */
4905 /* Everyone else's (except OSF) method for tracing exec syscalls */
4907 Not all systems with /proc have all the exec* syscalls with the same
4908 names. On the SGI, for example, there is no SYS_exec, but there
4909 *is* a SYS_execv. So, we try to account for that. */
4911 exitset
= sysset_t_alloc (pi
);
4912 gdb_premptysysset (exitset
);
4914 gdb_praddsysset (exitset
, SYS_exec
);
4917 gdb_praddsysset (exitset
, SYS_execve
);
4920 gdb_praddsysset (exitset
, SYS_execv
);
4922 #ifdef DYNAMIC_SYSCALLS
4924 int callnum
= find_syscall (pi
, "execve");
4927 gdb_praddsysset (exitset
, callnum
);
4929 callnum
= find_syscall (pi
, "ra_execve");
4931 gdb_praddsysset (exitset
, callnum
);
4933 #endif /* DYNAMIC_SYSCALLS */
4935 if (!proc_set_traced_sysexit (pi
, exitset
))
4937 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4938 gdb_flush (gdb_stderr
);
4941 #endif /* PRFS_STOPEXEC */
4943 /* FIXME: should this be done in the parent instead? */
4944 /* Turn off inherit on fork flag so that all grand-children
4945 of gdb start with tracing flags cleared. */
4946 if (!proc_unset_inherit_on_fork (pi
))
4947 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4949 /* Turn off run on last close flag, so that the child process
4950 cannot run away just because we close our handle on it.
4951 We want it to wait for the parent to attach. */
4952 if (!proc_unset_run_on_last_close (pi
))
4953 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4955 /* FIXME: No need to destroy the procinfo --
4956 we have our own address space, and we're about to do an exec! */
4957 /*destroy_procinfo (pi);*/
4961 * Function: create_inferior
4963 * This function is called BEFORE gdb forks the inferior process.
4964 * Its only real responsibility is to set things up for the fork,
4965 * and tell GDB which two functions to call after the fork (one
4966 * for the parent, and one for the child).
4968 * This function does a complicated search for a unix shell program,
4969 * which it then uses to parse arguments and environment variables
4970 * to be sent to the child. I wonder whether this code could not
4971 * be abstracted out and shared with other unix targets such as
4976 procfs_create_inferior (char *exec_file
, char *allargs
, char **env
)
4978 char *shell_file
= getenv ("SHELL");
4980 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4983 /* We will be looking down the PATH to find shell_file. If we
4984 just do this the normal way (via execlp, which operates by
4985 attempting an exec for each element of the PATH until it
4986 finds one which succeeds), then there will be an exec for
4987 each failed attempt, each of which will cause a PR_SYSEXIT
4988 stop, and we won't know how to distinguish the PR_SYSEXIT's
4989 for these failed execs with the ones for successful execs
4990 (whether the exec has succeeded is stored at that time in the
4991 carry bit or some such architecture-specific and
4992 non-ABI-specified place).
4994 So I can't think of anything better than to search the PATH
4995 now. This has several disadvantages: (1) There is a race
4996 condition; if we find a file now and it is deleted before we
4997 exec it, we lose, even if the deletion leaves a valid file
4998 further down in the PATH, (2) there is no way to know exactly
4999 what an executable (in the sense of "capable of being
5000 exec'd") file is. Using access() loses because it may lose
5001 if the caller is the superuser; failing to use it loses if
5002 there are ACLs or some such. */
5006 /* FIXME-maybe: might want "set path" command so user can change what
5007 path is used from within GDB. */
5008 char *path
= getenv ("PATH");
5010 struct stat statbuf
;
5013 path
= "/bin:/usr/bin";
5015 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
5016 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
5018 p1
= strchr (p
, ':');
5023 strncpy (tryname
, p
, len
);
5024 tryname
[len
] = '\0';
5025 strcat (tryname
, "/");
5026 strcat (tryname
, shell_file
);
5027 if (access (tryname
, X_OK
) < 0)
5029 if (stat (tryname
, &statbuf
) < 0)
5031 if (!S_ISREG (statbuf
.st_mode
))
5032 /* We certainly need to reject directories. I'm not quite
5033 as sure about FIFOs, sockets, etc., but I kind of doubt
5034 that people want to exec() these things. */
5039 /* Not found. This must be an error rather than merely passing
5040 the file to execlp(), because execlp() would try all the
5041 exec()s, causing GDB to get confused. */
5042 error ("procfs:%d -- Can't find shell %s in PATH",
5043 __LINE__
, shell_file
);
5045 shell_file
= tryname
;
5048 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
5049 procfs_init_inferior
, NULL
, shell_file
);
5051 /* We are at the first instruction we care about. */
5052 /* Pedal to the metal... */
5054 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
5058 * Function: notice_thread
5060 * Callback for find_new_threads.
5061 * Calls "add_thread".
5065 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
5067 int gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
5069 if (!in_thread_list (gdb_threadid
))
5070 add_thread (gdb_threadid
);
5076 * Function: target_find_new_threads
5078 * Query all the threads that the target knows about,
5079 * and give them back to GDB to add to its list.
5083 procfs_find_new_threads (void)
5087 /* Find procinfo for main process */
5088 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
5089 proc_update_threads (pi
);
5090 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
5094 * Function: target_thread_alive
5096 * Return true if the thread is still 'alive'.
5098 * This guy doesn't really seem to be doing his job.
5099 * Got to investigate how to tell when a thread is really gone.
5103 procfs_thread_alive (int pid
)
5108 proc
= PIDGET (pid
);
5109 thread
= TIDGET (pid
);
5110 /* If I don't know it, it ain't alive! */
5111 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
5114 /* If I can't get its status, it ain't alive!
5115 What's more, I need to forget about it! */
5116 if (!proc_get_status (pi
))
5118 destroy_procinfo (pi
);
5121 /* I couldn't have got its status if it weren't alive, so it's alive. */
5126 * Function: target_pid_to_str
5128 * Return a string to be used to identify the thread in
5129 * the "info threads" display.
5133 procfs_pid_to_str (int pid
)
5135 static char buf
[80];
5139 proc
= PIDGET (pid
);
5140 thread
= TIDGET (pid
);
5141 pi
= find_procinfo (proc
, thread
);
5144 sprintf (buf
, "Process %d", proc
);
5146 sprintf (buf
, "LWP %d", thread
);
5151 * Function: procfs_set_watchpoint
5152 * Insert a watchpoint
5156 procfs_set_watchpoint (int pid
, CORE_ADDR addr
, int len
, int rwflag
, int after
)
5163 pi
= find_procinfo_or_die (pid
== -1 ?
5164 PIDGET (inferior_pid
) : PIDGET (pid
), 0);
5166 /* Translate from GDB's flags to /proc's */
5167 if (len
> 0) /* len == 0 means delete watchpoint */
5169 switch (rwflag
) { /* FIXME: need an enum! */
5170 case hw_write
: /* default watchpoint (write) */
5171 pflags
= WRITE_WATCHFLAG
;
5173 case hw_read
: /* read watchpoint */
5174 pflags
= READ_WATCHFLAG
;
5176 case hw_access
: /* access watchpoint */
5177 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
5179 case hw_execute
: /* execution HW breakpoint */
5180 pflags
= EXEC_WATCHFLAG
;
5182 default: /* Something weird. Return error. */
5185 if (after
) /* Stop after r/w access is completed. */
5186 pflags
|= AFTER_WATCHFLAG
;
5189 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
5191 if (errno
== E2BIG
) /* Typical error for no resources */
5192 return -1; /* fail */
5193 /* GDB may try to remove the same watchpoint twice.
5194 If a remove request returns no match, don't error. */
5195 if (errno
== ESRCH
&& len
== 0)
5196 return 0; /* ignore */
5197 proc_error (pi
, "set_watchpoint", __LINE__
);
5200 #endif /* UNIXWARE */
5205 * Function: stopped_by_watchpoint
5207 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5208 * else returns zero.
5212 procfs_stopped_by_watchpoint (int pid
)
5216 pi
= find_procinfo (pid
== -1 ?
5217 PIDGET (inferior_pid
) : PIDGET (pid
), 0);
5219 if (!pi
) /* If no process, then not stopped by watchpoint! */
5222 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
5224 if (proc_why (pi
) == PR_FAULTED
)
5227 if (proc_what (pi
) == FLTWATCH
)
5231 if (proc_what (pi
) == FLTKWATCH
)
5239 #ifdef TM_I386SOL2_H
5241 * Function: procfs_find_LDT_entry
5244 * int pid; // The GDB-style pid-plus-LWP.
5247 * pointer to the corresponding LDT entry.
5251 procfs_find_LDT_entry (int pid
)
5253 gdb_gregset_t
*gregs
;
5257 /* Find procinfo for the lwp. */
5258 if ((pi
= find_procinfo (PIDGET (pid
), TIDGET (pid
))) == NULL
)
5260 warning ("procfs_find_LDT_entry: could not find procinfi for %d.",
5264 /* get its general registers. */
5265 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
5267 warning ("procfs_find_LDT_entry: could not read gregs for %d.",
5271 /* Now extract the GS register's lower 16 bits. */
5272 key
= (*gregs
)[GS
] & 0xffff;
5274 /* Find the matching entry and return it. */
5275 return proc_get_LDT_entry (pi
, key
);
5277 #endif /* TM_I386SOL2_H */
5282 info_proc_cmd (char *args
, int from_tty
)
5284 struct cleanup
*old_chain
;
5285 procinfo
*process
= NULL
;
5286 procinfo
*thread
= NULL
;
5292 old_chain
= make_cleanup (null_cleanup
, 0);
5295 if ((argv
= buildargv (args
)) == NULL
)
5298 make_cleanup_freeargv (argv
);
5300 while (argv
!= NULL
&& *argv
!= NULL
)
5302 if (isdigit (argv
[0][0]))
5304 pid
= strtoul (argv
[0], &tmp
, 10);
5306 tid
= strtoul (++tmp
, NULL
, 10);
5308 else if (argv
[0][0] == '/')
5310 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5319 pid
= PIDGET (inferior_pid
);
5321 error ("No current process: you must name one.");
5324 /* Have pid, will travel.
5325 First see if it's a process we're already debugging. */
5326 process
= find_procinfo (pid
, 0);
5327 if (process
== NULL
)
5329 /* No. So open a procinfo for it, but
5330 remember to close it again when finished. */
5331 process
= create_procinfo (pid
, 0);
5332 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5333 if (!open_procinfo_files (process
, FD_CTL
))
5334 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5338 thread
= create_procinfo (pid
, tid
);
5342 printf_filtered ("process %d flags:\n", process
->pid
);
5343 proc_prettyprint_flags (proc_flags (process
), 1);
5344 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5345 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5346 if (proc_get_nthreads (process
) > 1)
5347 printf_filtered ("Process has %d threads.\n",
5348 proc_get_nthreads (process
));
5352 printf_filtered ("thread %d flags:\n", thread
->tid
);
5353 proc_prettyprint_flags (proc_flags (thread
), 1);
5354 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5355 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5358 do_cleanups (old_chain
);
5362 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5368 if (inferior_pid
<= 0)
5369 error ("you must be debugging a process to use this command.");
5371 if (args
== NULL
|| args
[0] == 0)
5372 error_no_arg ("system call to trace");
5374 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
5375 if (isdigit (args
[0]))
5377 syscallnum
= atoi (args
);
5378 if (entry_or_exit
== PR_SYSENTRY
)
5379 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5381 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5384 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5386 if (mode
== FLAG_SET
)
5387 gdb_praddsysset (sysset
, syscallnum
);
5389 gdb_prdelsysset (sysset
, syscallnum
);
5391 if (entry_or_exit
== PR_SYSENTRY
)
5393 if (!proc_set_traced_sysentry (pi
, sysset
))
5394 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5398 if (!proc_set_traced_sysexit (pi
, sysset
))
5399 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5405 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5407 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5411 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5413 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5417 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5419 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5423 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5425 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5430 _initialize_procfs (void)
5433 add_target (&procfs_ops
);
5434 add_info ("proc", info_proc_cmd
,
5435 "Show /proc process information about any running process.\
5436 Default is the process being debugged.");
5437 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5438 "Give a trace of entries into the syscall.");
5439 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5440 "Give a trace of exits from the syscall.");
5441 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5442 "Cancel a trace of entries into the syscall.");
5443 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5444 "Cancel a trace of exits from the syscall.");
5447 /* =================== END, GDB "MODULE" =================== */
5451 /* miscelaneous stubs: */
5452 /* The following satisfy a few random symbols mostly created by */
5453 /* the solaris threads implementation, which I will chase down */
5457 * Return a pid for which we guarantee
5458 * we will be able to find a 'live' procinfo.
5462 procfs_first_available (void)
5465 return procinfo_list
->pid
;