1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
2 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Written by Michael Snyder at Cygnus Solutions.
4 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "elf-bfd.h" /* for elfcore_write_* */
28 #include "gdbthread.h"
30 #if defined (NEW_PROC_API)
31 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
34 #include <sys/procfs.h>
35 #ifdef HAVE_SYS_FAULT_H
36 #include <sys/fault.h>
38 #ifdef HAVE_SYS_SYSCALL_H
39 #include <sys/syscall.h>
41 #include <sys/errno.h>
49 * This module provides the interface between GDB and the
50 * /proc file system, which is used on many versions of Unix
51 * as a means for debuggers to control other processes.
52 * Examples of the systems that use this interface are:
59 * /proc works by imitating a file system: you open a simulated file
60 * that represents the process you wish to interact with, and
61 * perform operations on that "file" in order to examine or change
62 * the state of the other process.
64 * The most important thing to know about /proc and this module
65 * is that there are two very different interfaces to /proc:
66 * One that uses the ioctl system call, and
67 * another that uses read and write system calls.
68 * This module has to support both /proc interfaces. This means
69 * that there are two different ways of doing every basic operation.
71 * In order to keep most of the code simple and clean, I have
72 * defined an interface "layer" which hides all these system calls.
73 * An ifdef (NEW_PROC_API) determines which interface we are using,
74 * and most or all occurrances of this ifdef should be confined to
75 * this interface layer.
79 /* Determine which /proc API we are using:
80 The ioctl API defines PIOCSTATUS, while
81 the read/write (multiple fd) API never does. */
84 #include <sys/types.h>
85 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
88 #include <fcntl.h> /* for O_RDONLY */
89 #include <unistd.h> /* for "X_OK" */
90 #include "gdb_stat.h" /* for struct stat */
92 /* Note: procfs-utils.h must be included after the above system header
93 files, because it redefines various system calls using macros.
94 This may be incompatible with the prototype declarations. */
96 #include "proc-utils.h"
98 /* Prototypes for supply_gregset etc. */
101 /* =================== TARGET_OPS "MODULE" =================== */
104 * This module defines the GDB target vector and its methods.
107 static void procfs_open (char *, int);
108 static void procfs_attach (char *, int);
109 static void procfs_detach (char *, int);
110 static void procfs_resume (ptid_t
, int, enum target_signal
);
111 static int procfs_can_run (void);
112 static void procfs_stop (void);
113 static void procfs_files_info (struct target_ops
*);
114 static void procfs_fetch_registers (int);
115 static void procfs_store_registers (int);
116 static void procfs_notice_signals (ptid_t
);
117 static void procfs_prepare_to_store (void);
118 static void procfs_kill_inferior (void);
119 static void procfs_mourn_inferior (void);
120 static void procfs_create_inferior (char *, char *, char **);
121 static ptid_t
procfs_wait (ptid_t
, struct target_waitstatus
*);
122 static int procfs_xfer_memory (CORE_ADDR
, char *, int, int,
123 struct mem_attrib
*attrib
,
124 struct target_ops
*);
126 static int procfs_thread_alive (ptid_t
);
128 void procfs_find_new_threads (void);
129 char *procfs_pid_to_str (ptid_t
);
131 static int proc_find_memory_regions (int (*) (CORE_ADDR
,
137 static char * procfs_make_note_section (bfd
*, int *);
139 struct target_ops procfs_ops
; /* the target vector */
142 init_procfs_ops (void)
144 procfs_ops
.to_shortname
= "procfs";
145 procfs_ops
.to_longname
= "Unix /proc child process";
147 "Unix /proc child process (started by the \"run\" command).";
148 procfs_ops
.to_open
= procfs_open
;
149 procfs_ops
.to_can_run
= procfs_can_run
;
150 procfs_ops
.to_create_inferior
= procfs_create_inferior
;
151 procfs_ops
.to_kill
= procfs_kill_inferior
;
152 procfs_ops
.to_mourn_inferior
= procfs_mourn_inferior
;
153 procfs_ops
.to_attach
= procfs_attach
;
154 procfs_ops
.to_detach
= procfs_detach
;
155 procfs_ops
.to_wait
= procfs_wait
;
156 procfs_ops
.to_resume
= procfs_resume
;
157 procfs_ops
.to_prepare_to_store
= procfs_prepare_to_store
;
158 procfs_ops
.to_fetch_registers
= procfs_fetch_registers
;
159 procfs_ops
.to_store_registers
= procfs_store_registers
;
160 procfs_ops
.to_xfer_memory
= procfs_xfer_memory
;
161 procfs_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
162 procfs_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
163 procfs_ops
.to_notice_signals
= procfs_notice_signals
;
164 procfs_ops
.to_files_info
= procfs_files_info
;
165 procfs_ops
.to_stop
= procfs_stop
;
167 procfs_ops
.to_terminal_init
= terminal_init_inferior
;
168 procfs_ops
.to_terminal_inferior
= terminal_inferior
;
169 procfs_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
170 procfs_ops
.to_terminal_ours
= terminal_ours
;
171 procfs_ops
.to_terminal_info
= child_terminal_info
;
173 procfs_ops
.to_find_new_threads
= procfs_find_new_threads
;
174 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
175 procfs_ops
.to_pid_to_str
= procfs_pid_to_str
;
177 procfs_ops
.to_has_all_memory
= 1;
178 procfs_ops
.to_has_memory
= 1;
179 procfs_ops
.to_has_execution
= 1;
180 procfs_ops
.to_has_stack
= 1;
181 procfs_ops
.to_has_registers
= 1;
182 procfs_ops
.to_stratum
= process_stratum
;
183 procfs_ops
.to_has_thread_control
= tc_schedlock
;
184 procfs_ops
.to_find_memory_regions
= proc_find_memory_regions
;
185 procfs_ops
.to_make_corefile_notes
= procfs_make_note_section
;
186 procfs_ops
.to_magic
= OPS_MAGIC
;
189 /* =================== END, TARGET_OPS "MODULE" =================== */
194 * Put any typedefs, defines etc. here that are required for
195 * the unification of code that handles different versions of /proc.
198 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
200 enum { READ_WATCHFLAG
= WA_READ
,
201 WRITE_WATCHFLAG
= WA_WRITE
,
202 EXEC_WATCHFLAG
= WA_EXEC
,
203 AFTER_WATCHFLAG
= WA_TRAPAFTER
206 #else /* Irix method for watchpoints */
207 enum { READ_WATCHFLAG
= MA_READ
,
208 WRITE_WATCHFLAG
= MA_WRITE
,
209 EXEC_WATCHFLAG
= MA_EXEC
,
210 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
215 #ifdef HAVE_PR_SIGSET_T
216 typedef pr_sigset_t gdb_sigset_t
;
218 typedef sigset_t gdb_sigset_t
;
222 #ifdef HAVE_PR_SIGACTION64_T
223 typedef pr_sigaction64_t gdb_sigaction_t
;
225 typedef struct sigaction gdb_sigaction_t
;
229 #ifdef HAVE_PR_SIGINFO64_T
230 typedef pr_siginfo64_t gdb_siginfo_t
;
232 typedef struct siginfo gdb_siginfo_t
;
235 /* gdb_premptysysset */
237 #define gdb_premptysysset premptysysset
239 #define gdb_premptysysset premptyset
244 #define gdb_praddsysset praddsysset
246 #define gdb_praddsysset praddset
251 #define gdb_prdelsysset prdelsysset
253 #define gdb_prdelsysset prdelset
256 /* prissyssetmember */
257 #ifdef prissyssetmember
258 #define gdb_pr_issyssetmember prissyssetmember
260 #define gdb_pr_issyssetmember prismember
263 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
264 as intuitively descriptive as it could be, so we'll define
265 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
266 this writing, this feature is only found on AIX5 systems and
267 basically means that the set of syscalls is not fixed. I.e,
268 there's no nice table that one can #include to get all of the
269 syscall numbers. Instead, they're stored in /proc/PID/sysent
270 for each process. We are at least guaranteed that they won't
271 change over the lifetime of the process. But each process could
272 (in theory) have different syscall numbers.
274 #ifdef HAVE_PRSYSENT_T
275 #define DYNAMIC_SYSCALLS
280 /* =================== STRUCT PROCINFO "MODULE" =================== */
282 /* FIXME: this comment will soon be out of date W.R.T. threads. */
284 /* The procinfo struct is a wrapper to hold all the state information
285 concerning a /proc process. There should be exactly one procinfo
286 for each process, and since GDB currently can debug only one
287 process at a time, that means there should be only one procinfo.
288 All of the LWP's of a process can be accessed indirectly thru the
289 single process procinfo.
291 However, against the day when GDB may debug more than one process,
292 this data structure is kept in a list (which for now will hold no
293 more than one member), and many functions will have a pointer to a
294 procinfo as an argument.
296 There will be a separate procinfo structure for use by the (not yet
297 implemented) "info proc" command, so that we can print useful
298 information about any random process without interfering with the
299 inferior's procinfo information. */
302 /* format strings for /proc paths */
303 # ifndef CTL_PROC_NAME_FMT
304 # define MAIN_PROC_NAME_FMT "/proc/%d"
305 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
306 # define AS_PROC_NAME_FMT "/proc/%d/as"
307 # define MAP_PROC_NAME_FMT "/proc/%d/map"
308 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
309 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
311 /* the name of the proc status struct depends on the implementation */
312 typedef pstatus_t gdb_prstatus_t
;
313 typedef lwpstatus_t gdb_lwpstatus_t
;
314 #else /* ! NEW_PROC_API */
315 /* format strings for /proc paths */
316 # ifndef CTL_PROC_NAME_FMT
317 # define MAIN_PROC_NAME_FMT "/proc/%05d"
318 # define CTL_PROC_NAME_FMT "/proc/%05d"
319 # define AS_PROC_NAME_FMT "/proc/%05d"
320 # define MAP_PROC_NAME_FMT "/proc/%05d"
321 # define STATUS_PROC_NAME_FMT "/proc/%05d"
322 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
324 /* the name of the proc status struct depends on the implementation */
325 typedef prstatus_t gdb_prstatus_t
;
326 typedef prstatus_t gdb_lwpstatus_t
;
327 #endif /* NEW_PROC_API */
329 typedef struct procinfo
{
330 struct procinfo
*next
;
331 int pid
; /* Process ID */
332 int tid
; /* Thread/LWP id */
336 int ignore_next_sigstop
;
338 /* The following four fd fields may be identical, or may contain
339 several different fd's, depending on the version of /proc
340 (old ioctl or new read/write). */
342 int ctl_fd
; /* File descriptor for /proc control file */
344 * The next three file descriptors are actually only needed in the
345 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
346 * However, to avoid a bunch of #ifdefs in the code, we will use
347 * them uniformly by (in the case of the ioctl single-file-descriptor
348 * implementation) filling them with copies of the control fd.
350 int status_fd
; /* File descriptor for /proc status file */
351 int as_fd
; /* File descriptor for /proc as file */
353 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
355 fltset_t saved_fltset
; /* Saved traced hardware fault set */
356 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
357 gdb_sigset_t saved_sighold
; /* Saved held signal set */
358 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
359 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
361 gdb_prstatus_t prstatus
; /* Current process status info */
364 gdb_fpregset_t fpregset
; /* Current floating point registers */
367 #ifdef DYNAMIC_SYSCALLS
368 int num_syscalls
; /* Total number of syscalls */
369 char **syscall_names
; /* Syscall number to name map */
372 struct procinfo
*thread_list
;
374 int status_valid
: 1;
376 int fpregs_valid
: 1;
377 int threads_valid
: 1;
380 static char errmsg
[128]; /* shared error msg buffer */
382 /* Function prototypes for procinfo module: */
384 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
385 static procinfo
*find_procinfo (int pid
, int tid
);
386 static procinfo
*create_procinfo (int pid
, int tid
);
387 static void destroy_procinfo (procinfo
* p
);
388 static void do_destroy_procinfo_cleanup (void *);
389 static void dead_procinfo (procinfo
* p
, char *msg
, int killp
);
390 static int open_procinfo_files (procinfo
* p
, int which
);
391 static void close_procinfo_files (procinfo
* p
);
392 static int sysset_t_size (procinfo
*p
);
393 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
394 #ifdef DYNAMIC_SYSCALLS
395 static void load_syscalls (procinfo
*pi
);
396 static void free_syscalls (procinfo
*pi
);
397 static int find_syscall (procinfo
*pi
, char *name
);
398 #endif /* DYNAMIC_SYSCALLS */
400 /* The head of the procinfo list: */
401 static procinfo
* procinfo_list
;
404 * Function: find_procinfo
406 * Search the procinfo list.
408 * Returns: pointer to procinfo, or NULL if not found.
412 find_procinfo (int pid
, int tid
)
416 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
423 /* Don't check threads_valid. If we're updating the
424 thread_list, we want to find whatever threads are already
425 here. This means that in general it is the caller's
426 responsibility to check threads_valid and update before
427 calling find_procinfo, if the caller wants to find a new
430 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
439 * Function: find_procinfo_or_die
441 * Calls find_procinfo, but errors on failure.
445 find_procinfo_or_die (int pid
, int tid
)
447 procinfo
*pi
= find_procinfo (pid
, tid
);
452 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
455 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
460 /* open_with_retry() is a wrapper for open(). The appropriate
461 open() call is attempted; if unsuccessful, it will be retried as
462 many times as needed for the EAGAIN and EINTR conditions.
464 For other conditions, open_with_retry() will retry the open() a
465 limited number of times. In addition, a short sleep is imposed
466 prior to retrying the open(). The reason for this sleep is to give
467 the kernel a chance to catch up and create the file in question in
468 the event that GDB "wins" the race to open a file before the kernel
472 open_with_retry (const char *pathname
, int flags
)
474 int retries_remaining
, status
;
476 retries_remaining
= 2;
480 status
= open (pathname
, flags
);
482 if (status
>= 0 || retries_remaining
== 0)
484 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
495 * Function: open_procinfo_files
497 * Open the file descriptor for the process or LWP.
498 * ifdef NEW_PROC_API, we only open the control file descriptor;
499 * the others are opened lazily as needed.
500 * else (if not NEW_PROC_API), there is only one real
501 * file descriptor, but we keep multiple copies of it so that
502 * the code that uses them does not have to be #ifdef'd.
504 * Return: file descriptor, or zero for failure.
507 enum { FD_CTL
, FD_STATUS
, FD_AS
};
510 open_procinfo_files (procinfo
*pi
, int which
)
513 char tmp
[MAX_PROC_NAME_SIZE
];
518 * This function is getting ALMOST long enough to break up into several.
519 * Here is some rationale:
521 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
522 * There are several file descriptors that may need to be open
523 * for any given process or LWP. The ones we're intereted in are:
524 * - control (ctl) write-only change the state
525 * - status (status) read-only query the state
526 * - address space (as) read/write access memory
527 * - map (map) read-only virtual addr map
528 * Most of these are opened lazily as they are needed.
529 * The pathnames for the 'files' for an LWP look slightly
530 * different from those of a first-class process:
531 * Pathnames for a process (<proc-id>):
532 * /proc/<proc-id>/ctl
533 * /proc/<proc-id>/status
535 * /proc/<proc-id>/map
536 * Pathnames for an LWP (lwp-id):
537 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
538 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
539 * An LWP has no map or address space file descriptor, since
540 * the memory map and address space are shared by all LWPs.
542 * Everyone else (Solaris 2.5, Irix, OSF)
543 * There is only one file descriptor for each process or LWP.
544 * For convenience, we copy the same file descriptor into all
545 * three fields of the procinfo struct (ctl_fd, status_fd, and
546 * as_fd, see NEW_PROC_API above) so that code that uses them
547 * doesn't need any #ifdef's.
552 * Each LWP has an independent file descriptor, but these
553 * are not obtained via the 'open' system call like the rest:
554 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
555 * to the file descriptor of the parent process.
558 * These do not even have their own independent file descriptor.
559 * All operations are carried out on the file descriptor of the
560 * parent process. Therefore we just call open again for each
561 * thread, getting a new handle for the same 'file'.
566 * In this case, there are several different file descriptors that
567 * we might be asked to open. The control file descriptor will be
568 * opened early, but the others will be opened lazily as they are
572 strcpy (tmp
, pi
->pathname
);
573 switch (which
) { /* which file descriptor to open? */
576 strcat (tmp
, "/lwpctl");
578 strcat (tmp
, "/ctl");
579 fd
= open_with_retry (tmp
, O_WRONLY
);
586 return 0; /* there is no 'as' file descriptor for an lwp */
588 fd
= open_with_retry (tmp
, O_RDWR
);
595 strcat (tmp
, "/lwpstatus");
597 strcat (tmp
, "/status");
598 fd
= open_with_retry (tmp
, O_RDONLY
);
604 return 0; /* unknown file descriptor */
606 #else /* not NEW_PROC_API */
608 * In this case, there is only one file descriptor for each procinfo
609 * (ie. each process or LWP). In fact, only the file descriptor for
610 * the process can actually be opened by an 'open' system call.
611 * The ones for the LWPs have to be obtained thru an IOCTL call
612 * on the process's file descriptor.
614 * For convenience, we copy each procinfo's single file descriptor
615 * into all of the fields occupied by the several file descriptors
616 * of the NEW_PROC_API implementation. That way, the code that uses
617 * them can be written without ifdefs.
621 #ifdef PIOCTSTATUS /* OSF */
622 /* Only one FD; just open it. */
623 if ((fd
= open_with_retry (pi
->pathname
, O_RDWR
)) == 0)
625 #else /* Sol 2.5, Irix, other? */
626 if (pi
->tid
== 0) /* Master procinfo for the process */
628 fd
= open_with_retry (pi
->pathname
, O_RDWR
);
632 else /* LWP thread procinfo */
634 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
638 /* Find the procinfo for the entire process. */
639 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
642 /* Now obtain the file descriptor for the LWP. */
643 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
645 #else /* Irix, other? */
646 return 0; /* Don't know how to open threads */
647 #endif /* Sol 2.5 PIOCOPENLWP */
649 #endif /* OSF PIOCTSTATUS */
650 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
651 #endif /* NEW_PROC_API */
653 return 1; /* success */
657 * Function: create_procinfo
659 * Allocate a data structure and link it into the procinfo list.
660 * (First tries to find a pre-existing one (FIXME: why?)
662 * Return: pointer to new procinfo struct.
666 create_procinfo (int pid
, int tid
)
668 procinfo
*pi
, *parent
;
670 if ((pi
= find_procinfo (pid
, tid
)))
671 return pi
; /* Already exists, nothing to do. */
673 /* find parent before doing malloc, to save having to cleanup */
675 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
677 doesn't exist yet? */
679 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
680 memset (pi
, 0, sizeof (procinfo
));
684 #ifdef DYNAMIC_SYSCALLS
688 pi
->saved_entryset
= sysset_t_alloc (pi
);
689 pi
->saved_exitset
= sysset_t_alloc (pi
);
691 /* Chain into list. */
694 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
695 pi
->next
= procinfo_list
;
701 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
703 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
705 pi
->next
= parent
->thread_list
;
706 parent
->thread_list
= pi
;
712 * Function: close_procinfo_files
714 * Close all file descriptors associated with the procinfo
718 close_procinfo_files (procinfo
*pi
)
725 if (pi
->status_fd
> 0)
726 close (pi
->status_fd
);
728 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
732 * Function: destroy_procinfo
734 * Destructor function. Close, unlink and deallocate the object.
738 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
742 /* Step one: unlink the procinfo from its list */
746 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
749 ptr
->next
= pi
->next
;
753 /* Step two: close any open file descriptors */
754 close_procinfo_files (pi
);
756 /* Step three: free the memory. */
757 #ifdef DYNAMIC_SYSCALLS
760 xfree (pi
->saved_entryset
);
761 xfree (pi
->saved_exitset
);
766 destroy_procinfo (procinfo
*pi
)
770 if (pi
->tid
!= 0) /* destroy a thread procinfo */
772 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
773 destroy_one_procinfo (&tmp
->thread_list
, pi
);
775 else /* destroy a process procinfo and all its threads */
777 /* First destroy the children, if any; */
778 while (pi
->thread_list
!= NULL
)
779 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
780 /* Then destroy the parent. Genocide!!! */
781 destroy_one_procinfo (&procinfo_list
, pi
);
786 do_destroy_procinfo_cleanup (void *pi
)
788 destroy_procinfo (pi
);
791 enum { NOKILL
, KILL
};
794 * Function: dead_procinfo
796 * To be called on a non_recoverable error for a procinfo.
797 * Prints error messages, optionally sends a SIGKILL to the process,
798 * then destroys the data structure.
802 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
808 print_sys_errmsg (pi
->pathname
, errno
);
812 sprintf (procfile
, "process %d", pi
->pid
);
813 print_sys_errmsg (procfile
, errno
);
816 kill (pi
->pid
, SIGKILL
);
818 destroy_procinfo (pi
);
823 * Function: sysset_t_size
825 * Returns the (complete) size of a sysset_t struct. Normally, this
826 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
827 * size of sysset_t isn't known until runtime.
831 sysset_t_size (procinfo
* pi
)
833 #ifndef DYNAMIC_SYSCALLS
834 return sizeof (sysset_t
);
836 return sizeof (sysset_t
) - sizeof (uint64_t)
837 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
838 / (8 * sizeof (uint64_t)));
842 /* Function: sysset_t_alloc
844 Allocate and (partially) initialize a sysset_t struct. */
847 sysset_t_alloc (procinfo
* pi
)
850 int size
= sysset_t_size (pi
);
851 ret
= xmalloc (size
);
852 #ifdef DYNAMIC_SYSCALLS
853 ret
->pr_size
= (pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
854 / (8 * sizeof (uint64_t));
859 #ifdef DYNAMIC_SYSCALLS
861 /* Function: load_syscalls
863 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
864 pi->num_syscalls with the number of syscalls and pi->syscall_names
865 with the names. (Certain numbers may be skipped in which case the
866 names for these numbers will be left as NULL.) */
868 #define MAX_SYSCALL_NAME_LENGTH 256
869 #define MAX_SYSCALLS 65536
872 load_syscalls (procinfo
*pi
)
874 char pathname
[MAX_PROC_NAME_SIZE
];
877 prsyscall_t
*syscalls
;
878 int i
, size
, maxcall
;
880 pi
->num_syscalls
= 0;
881 pi
->syscall_names
= 0;
883 /* Open the file descriptor for the sysent file */
884 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
885 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
888 error ("load_syscalls: Can't open /proc/%d/sysent", pi
->pid
);
891 size
= sizeof header
- sizeof (prsyscall_t
);
892 if (read (sysent_fd
, &header
, size
) != size
)
894 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
897 if (header
.pr_nsyscalls
== 0)
899 error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi
->pid
);
902 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
903 syscalls
= xmalloc (size
);
905 if (read (sysent_fd
, syscalls
, size
) != size
)
908 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
911 /* Find maximum syscall number. This may not be the same as
912 pr_nsyscalls since that value refers to the number of entries
913 in the table. (Also, the docs indicate that some system
914 call numbers may be skipped.) */
916 maxcall
= syscalls
[0].pr_number
;
918 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
919 if (syscalls
[i
].pr_number
> maxcall
920 && syscalls
[i
].pr_nameoff
> 0
921 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
922 maxcall
= syscalls
[i
].pr_number
;
924 pi
->num_syscalls
= maxcall
+1;
925 pi
->syscall_names
= xmalloc (pi
->num_syscalls
* sizeof (char *));
927 for (i
= 0; i
< pi
->num_syscalls
; i
++)
928 pi
->syscall_names
[i
] = NULL
;
930 /* Read the syscall names in */
931 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
933 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
937 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
938 || syscalls
[i
].pr_number
< 0
939 || syscalls
[i
].pr_nameoff
<= 0
940 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
941 != (off_t
) syscalls
[i
].pr_nameoff
))
944 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
948 callnum
= syscalls
[i
].pr_number
;
950 if (pi
->syscall_names
[callnum
] != NULL
)
952 /* FIXME: Generate warning */
956 namebuf
[nread
-1] = '\0';
957 size
= strlen (namebuf
) + 1;
958 pi
->syscall_names
[callnum
] = xmalloc (size
);
959 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
960 pi
->syscall_names
[callnum
][size
-1] = '\0';
967 /* Function: free_syscalls
969 Free the space allocated for the syscall names from the procinfo
973 free_syscalls (procinfo
*pi
)
975 if (pi
->syscall_names
)
979 for (i
= 0; i
< pi
->num_syscalls
; i
++)
980 if (pi
->syscall_names
[i
] != NULL
)
981 xfree (pi
->syscall_names
[i
]);
983 xfree (pi
->syscall_names
);
984 pi
->syscall_names
= 0;
988 /* Function: find_syscall
990 Given a name, look up (and return) the corresponding syscall number.
991 If no match is found, return -1. */
994 find_syscall (procinfo
*pi
, char *name
)
997 for (i
= 0; i
< pi
->num_syscalls
; i
++)
999 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
1006 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1008 /* =================== /proc "MODULE" =================== */
1011 * This "module" is the interface layer between the /proc system API
1012 * and the gdb target vector functions. This layer consists of
1013 * access functions that encapsulate each of the basic operations
1014 * that we need to use from the /proc API.
1016 * The main motivation for this layer is to hide the fact that
1017 * there are two very different implementations of the /proc API.
1018 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1019 * functions, we do our best to hide them all in here.
1022 int proc_get_status (procinfo
* pi
);
1023 long proc_flags (procinfo
* pi
);
1024 int proc_why (procinfo
* pi
);
1025 int proc_what (procinfo
* pi
);
1026 int proc_set_run_on_last_close (procinfo
* pi
);
1027 int proc_unset_run_on_last_close (procinfo
* pi
);
1028 int proc_set_inherit_on_fork (procinfo
* pi
);
1029 int proc_unset_inherit_on_fork (procinfo
* pi
);
1030 int proc_set_async (procinfo
* pi
);
1031 int proc_unset_async (procinfo
* pi
);
1032 int proc_stop_process (procinfo
* pi
);
1033 int proc_trace_signal (procinfo
* pi
, int signo
);
1034 int proc_ignore_signal (procinfo
* pi
, int signo
);
1035 int proc_clear_current_fault (procinfo
* pi
);
1036 int proc_set_current_signal (procinfo
* pi
, int signo
);
1037 int proc_clear_current_signal (procinfo
* pi
);
1038 int proc_set_gregs (procinfo
* pi
);
1039 int proc_set_fpregs (procinfo
* pi
);
1040 int proc_wait_for_stop (procinfo
* pi
);
1041 int proc_run_process (procinfo
* pi
, int step
, int signo
);
1042 int proc_kill (procinfo
* pi
, int signo
);
1043 int proc_parent_pid (procinfo
* pi
);
1044 int proc_get_nthreads (procinfo
* pi
);
1045 int proc_get_current_thread (procinfo
* pi
);
1046 int proc_set_held_signals (procinfo
* pi
, gdb_sigset_t
* sighold
);
1047 int proc_set_traced_sysexit (procinfo
* pi
, sysset_t
* sysset
);
1048 int proc_set_traced_sysentry (procinfo
* pi
, sysset_t
* sysset
);
1049 int proc_set_traced_faults (procinfo
* pi
, fltset_t
* fltset
);
1050 int proc_set_traced_signals (procinfo
* pi
, gdb_sigset_t
* sigset
);
1052 int proc_update_threads (procinfo
* pi
);
1053 int proc_iterate_over_threads (procinfo
* pi
,
1054 int (*func
) (procinfo
*, procinfo
*, void *),
1057 gdb_gregset_t
*proc_get_gregs (procinfo
* pi
);
1058 gdb_fpregset_t
*proc_get_fpregs (procinfo
* pi
);
1059 sysset_t
*proc_get_traced_sysexit (procinfo
* pi
, sysset_t
* save
);
1060 sysset_t
*proc_get_traced_sysentry (procinfo
* pi
, sysset_t
* save
);
1061 fltset_t
*proc_get_traced_faults (procinfo
* pi
, fltset_t
* save
);
1062 gdb_sigset_t
*proc_get_traced_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1063 gdb_sigset_t
*proc_get_held_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1064 gdb_sigset_t
*proc_get_pending_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1065 gdb_sigaction_t
*proc_get_signal_actions (procinfo
* pi
, gdb_sigaction_t
*save
);
1067 void proc_warn (procinfo
* pi
, char *func
, int line
);
1068 void proc_error (procinfo
* pi
, char *func
, int line
);
1071 proc_warn (procinfo
*pi
, char *func
, int line
)
1073 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1074 print_sys_errmsg (errmsg
, errno
);
1078 proc_error (procinfo
*pi
, char *func
, int line
)
1080 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1081 perror_with_name (errmsg
);
1085 * Function: proc_get_status
1087 * Updates the status struct in the procinfo.
1088 * There is a 'valid' flag, to let other functions know when
1089 * this function needs to be called (so the status is only
1090 * read when it is needed). The status file descriptor is
1091 * also only opened when it is needed.
1093 * Return: non-zero for success, zero for failure.
1097 proc_get_status (procinfo
*pi
)
1099 /* Status file descriptor is opened "lazily" */
1100 if (pi
->status_fd
== 0 &&
1101 open_procinfo_files (pi
, FD_STATUS
) == 0)
1103 pi
->status_valid
= 0;
1108 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1109 pi
->status_valid
= 0; /* fail */
1112 /* Sigh... I have to read a different data structure,
1113 depending on whether this is a main process or an LWP. */
1115 pi
->status_valid
= (read (pi
->status_fd
,
1116 (char *) &pi
->prstatus
.pr_lwp
,
1117 sizeof (lwpstatus_t
))
1118 == sizeof (lwpstatus_t
));
1121 pi
->status_valid
= (read (pi
->status_fd
,
1122 (char *) &pi
->prstatus
,
1123 sizeof (gdb_prstatus_t
))
1124 == sizeof (gdb_prstatus_t
));
1125 #if 0 /*def UNIXWARE*/
1126 if (pi
->status_valid
&&
1127 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
1128 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
1129 /* Unixware peculiarity -- read the damn thing again! */
1130 pi
->status_valid
= (read (pi
->status_fd
,
1131 (char *) &pi
->prstatus
,
1132 sizeof (gdb_prstatus_t
))
1133 == sizeof (gdb_prstatus_t
));
1134 #endif /* UNIXWARE */
1137 #else /* ioctl method */
1138 #ifdef PIOCTSTATUS /* osf */
1139 if (pi
->tid
== 0) /* main process */
1141 /* Just read the danged status. Now isn't that simple? */
1143 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1150 tid_t pr_error_thread
;
1151 struct prstatus status
;
1154 thread_status
.pr_count
= 1;
1155 thread_status
.status
.pr_tid
= pi
->tid
;
1156 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1159 memcpy (&pi
->prstatus
, &thread_status
.status
,
1160 sizeof (pi
->prstatus
));
1161 pi
->status_valid
= 1;
1165 /* Just read the danged status. Now isn't that simple? */
1166 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1170 if (pi
->status_valid
)
1172 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1175 proc_get_current_thread (pi
));
1178 /* The status struct includes general regs, so mark them valid too */
1179 pi
->gregs_valid
= pi
->status_valid
;
1181 /* In the read/write multiple-fd model,
1182 the status struct includes the fp regs too, so mark them valid too */
1183 pi
->fpregs_valid
= pi
->status_valid
;
1185 return pi
->status_valid
; /* True if success, false if failure. */
1189 * Function: proc_flags
1191 * returns the process flags (pr_flags field).
1195 proc_flags (procinfo
*pi
)
1197 if (!pi
->status_valid
)
1198 if (!proc_get_status (pi
))
1199 return 0; /* FIXME: not a good failure value (but what is?) */
1203 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1204 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1205 The two sets of flags don't overlap. */
1206 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
1208 return pi
->prstatus
.pr_lwp
.pr_flags
;
1211 return pi
->prstatus
.pr_flags
;
1216 * Function: proc_why
1218 * returns the pr_why field (why the process stopped).
1222 proc_why (procinfo
*pi
)
1224 if (!pi
->status_valid
)
1225 if (!proc_get_status (pi
))
1226 return 0; /* FIXME: not a good failure value (but what is?) */
1229 return pi
->prstatus
.pr_lwp
.pr_why
;
1231 return pi
->prstatus
.pr_why
;
1236 * Function: proc_what
1238 * returns the pr_what field (details of why the process stopped).
1242 proc_what (procinfo
*pi
)
1244 if (!pi
->status_valid
)
1245 if (!proc_get_status (pi
))
1246 return 0; /* FIXME: not a good failure value (but what is?) */
1249 return pi
->prstatus
.pr_lwp
.pr_what
;
1251 return pi
->prstatus
.pr_what
;
1255 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1257 * Function: proc_nsysarg
1259 * returns the pr_nsysarg field (number of args to the current syscall).
1263 proc_nsysarg (procinfo
*pi
)
1265 if (!pi
->status_valid
)
1266 if (!proc_get_status (pi
))
1270 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1272 return pi
->prstatus
.pr_nsysarg
;
1277 * Function: proc_sysargs
1279 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1283 proc_sysargs (procinfo
*pi
)
1285 if (!pi
->status_valid
)
1286 if (!proc_get_status (pi
))
1290 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1292 return (long *) &pi
->prstatus
.pr_sysarg
;
1297 * Function: proc_syscall
1299 * returns the pr_syscall field (id of current syscall if we are in one).
1303 proc_syscall (procinfo
*pi
)
1305 if (!pi
->status_valid
)
1306 if (!proc_get_status (pi
))
1310 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1312 return pi
->prstatus
.pr_syscall
;
1315 #endif /* PIOCSSPCACT */
1318 * Function: proc_cursig:
1320 * returns the pr_cursig field (current signal).
1324 proc_cursig (struct procinfo
*pi
)
1326 if (!pi
->status_valid
)
1327 if (!proc_get_status (pi
))
1328 return 0; /* FIXME: not a good failure value (but what is?) */
1331 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1333 return pi
->prstatus
.pr_cursig
;
1338 * Function: proc_modify_flag
1340 * === I appologize for the messiness of this function.
1341 * === This is an area where the different versions of
1342 * === /proc are more inconsistent than usual. MVS
1344 * Set or reset any of the following process flags:
1345 * PR_FORK -- forked child will inherit trace flags
1346 * PR_RLC -- traced process runs when last /proc file closed.
1347 * PR_KLC -- traced process is killed when last /proc file closed.
1348 * PR_ASYNC -- LWP's get to run/stop independently.
1350 * There are three methods for doing this function:
1351 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1353 * 2) Middle: PIOCSET/PIOCRESET
1355 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1358 * Note: Irix does not define PR_ASYNC.
1359 * Note: OSF does not define PR_KLC.
1360 * Note: OSF is the only one that can ONLY use the oldest method.
1363 * pi -- the procinfo
1364 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1365 * mode -- 1 for set, 0 for reset.
1367 * Returns non-zero for success, zero for failure.
1370 enum { FLAG_RESET
, FLAG_SET
};
1373 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1375 long win
= 0; /* default to fail */
1378 * These operations affect the process as a whole, and applying
1379 * them to an individual LWP has the same meaning as applying them
1380 * to the main process. Therefore, if we're ever called with a
1381 * pointer to an LWP's procinfo, let's substitute the process's
1382 * procinfo and avoid opening the LWP's file descriptor
1387 pi
= find_procinfo_or_die (pi
->pid
, 0);
1389 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1390 /* First normalize the PCUNSET/PCRESET command opcode
1391 (which for no obvious reason has a different definition
1392 from one operating system to the next...) */
1394 #define GDBRESET PCUNSET
1397 #define GDBRESET PCRESET
1401 procfs_ctl_t arg
[2];
1403 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1405 else /* Reset the flag */
1409 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1412 #ifdef PIOCSET /* Irix/Sol5 method */
1413 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1415 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1417 else /* Reset the flag */
1419 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1423 #ifdef PIOCSRLC /* Oldest method: OSF */
1426 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1428 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1430 else /* Clear run-on-last-close */
1432 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1436 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1438 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1440 else /* Clear inherit-on-fork */
1442 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1446 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1453 /* The above operation renders the procinfo's cached pstatus obsolete. */
1454 pi
->status_valid
= 0;
1457 warning ("procfs: modify_flag failed to turn %s %s",
1458 flag
== PR_FORK
? "PR_FORK" :
1459 flag
== PR_RLC
? "PR_RLC" :
1461 flag
== PR_ASYNC
? "PR_ASYNC" :
1464 flag
== PR_KLC
? "PR_KLC" :
1467 mode
== FLAG_RESET
? "off" : "on");
1473 * Function: proc_set_run_on_last_close
1475 * Set the run_on_last_close flag.
1476 * Process with all threads will become runnable
1477 * when debugger closes all /proc fds.
1479 * Returns non-zero for success, zero for failure.
1483 proc_set_run_on_last_close (procinfo
*pi
)
1485 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1489 * Function: proc_unset_run_on_last_close
1491 * Reset the run_on_last_close flag.
1492 * Process will NOT become runnable
1493 * when debugger closes its file handles.
1495 * Returns non-zero for success, zero for failure.
1499 proc_unset_run_on_last_close (procinfo
*pi
)
1501 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1506 * Function: proc_set_kill_on_last_close
1508 * Set the kill_on_last_close flag.
1509 * Process with all threads will be killed when debugger
1510 * closes all /proc fds (or debugger exits or dies).
1512 * Returns non-zero for success, zero for failure.
1516 proc_set_kill_on_last_close (procinfo
*pi
)
1518 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1522 * Function: proc_unset_kill_on_last_close
1524 * Reset the kill_on_last_close flag.
1525 * Process will NOT be killed when debugger
1526 * closes its file handles (or exits or dies).
1528 * Returns non-zero for success, zero for failure.
1532 proc_unset_kill_on_last_close (procinfo
*pi
)
1534 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1539 * Function: proc_set_inherit_on_fork
1541 * Set inherit_on_fork flag.
1542 * If the process forks a child while we are registered for events
1543 * in the parent, then we will also recieve events from the child.
1545 * Returns non-zero for success, zero for failure.
1549 proc_set_inherit_on_fork (procinfo
*pi
)
1551 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1555 * Function: proc_unset_inherit_on_fork
1557 * Reset inherit_on_fork flag.
1558 * If the process forks a child while we are registered for events
1559 * in the parent, then we will NOT recieve events from the child.
1561 * Returns non-zero for success, zero for failure.
1565 proc_unset_inherit_on_fork (procinfo
*pi
)
1567 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1572 * Function: proc_set_async
1574 * Set PR_ASYNC flag.
1575 * If one LWP stops because of a debug event (signal etc.),
1576 * the remaining LWPs will continue to run.
1578 * Returns non-zero for success, zero for failure.
1582 proc_set_async (procinfo
*pi
)
1584 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1588 * Function: proc_unset_async
1590 * Reset PR_ASYNC flag.
1591 * If one LWP stops because of a debug event (signal etc.),
1592 * then all other LWPs will stop as well.
1594 * Returns non-zero for success, zero for failure.
1598 proc_unset_async (procinfo
*pi
)
1600 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1602 #endif /* PR_ASYNC */
1605 * Function: proc_stop_process
1607 * Request the process/LWP to stop. Does not wait.
1608 * Returns non-zero for success, zero for failure.
1612 proc_stop_process (procinfo
*pi
)
1617 * We might conceivably apply this operation to an LWP, and
1618 * the LWP's ctl file descriptor might not be open.
1621 if (pi
->ctl_fd
== 0 &&
1622 open_procinfo_files (pi
, FD_CTL
) == 0)
1627 procfs_ctl_t cmd
= PCSTOP
;
1628 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1629 #else /* ioctl method */
1630 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1631 /* Note: the call also reads the prstatus. */
1634 pi
->status_valid
= 1;
1635 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1638 proc_get_current_thread (pi
));
1647 * Function: proc_wait_for_stop
1649 * Wait for the process or LWP to stop (block until it does).
1650 * Returns non-zero for success, zero for failure.
1654 proc_wait_for_stop (procinfo
*pi
)
1659 * We should never have to apply this operation to any procinfo
1660 * except the one for the main process. If that ever changes
1661 * for any reason, then take out the following clause and
1662 * replace it with one that makes sure the ctl_fd is open.
1666 pi
= find_procinfo_or_die (pi
->pid
, 0);
1670 procfs_ctl_t cmd
= PCWSTOP
;
1671 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1672 /* We been runnin' and we stopped -- need to update status. */
1673 pi
->status_valid
= 0;
1675 #else /* ioctl method */
1676 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1677 /* Above call also refreshes the prstatus. */
1680 pi
->status_valid
= 1;
1681 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1684 proc_get_current_thread (pi
));
1692 * Function: proc_run_process
1694 * Make the process or LWP runnable.
1695 * Options (not all are implemented):
1697 * - clear current fault
1698 * - clear current signal
1699 * - abort the current system call
1700 * - stop as soon as finished with system call
1701 * - (ioctl): set traced signal set
1702 * - (ioctl): set held signal set
1703 * - (ioctl): set traced fault set
1704 * - (ioctl): set start pc (vaddr)
1705 * Always clear the current fault.
1706 * Clear the current signal if 'signo' is zero.
1709 * pi the process or LWP to operate on.
1710 * step if true, set the process or LWP to trap after one instr.
1711 * signo if zero, clear the current signal if any.
1712 * if non-zero, set the current signal to this one.
1714 * Returns non-zero for success, zero for failure.
1718 proc_run_process (procinfo
*pi
, int step
, int signo
)
1724 * We will probably have to apply this operation to individual threads,
1725 * so make sure the control file descriptor is open.
1728 if (pi
->ctl_fd
== 0 &&
1729 open_procinfo_files (pi
, FD_CTL
) == 0)
1734 runflags
= PRCFAULT
; /* always clear current fault */
1739 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1740 proc_set_current_signal (pi
, signo
);
1744 procfs_ctl_t cmd
[2];
1748 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1750 #else /* ioctl method */
1754 memset (&prrun
, 0, sizeof (prrun
));
1755 prrun
.pr_flags
= runflags
;
1756 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1764 * Function: proc_set_traced_signals
1766 * Register to trace signals in the process or LWP.
1767 * Returns non-zero for success, zero for failure.
1771 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1776 * We should never have to apply this operation to any procinfo
1777 * except the one for the main process. If that ever changes
1778 * for any reason, then take out the following clause and
1779 * replace it with one that makes sure the ctl_fd is open.
1783 pi
= find_procinfo_or_die (pi
->pid
, 0);
1789 /* Use char array to avoid alignment issues. */
1790 char sigset
[sizeof (gdb_sigset_t
)];
1794 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1796 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1798 #else /* ioctl method */
1799 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1801 /* The above operation renders the procinfo's cached pstatus obsolete. */
1802 pi
->status_valid
= 0;
1805 warning ("procfs: set_traced_signals failed");
1810 * Function: proc_set_traced_faults
1812 * Register to trace hardware faults in the process or LWP.
1813 * Returns non-zero for success, zero for failure.
1817 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1822 * We should never have to apply this operation to any procinfo
1823 * except the one for the main process. If that ever changes
1824 * for any reason, then take out the following clause and
1825 * replace it with one that makes sure the ctl_fd is open.
1829 pi
= find_procinfo_or_die (pi
->pid
, 0);
1835 /* Use char array to avoid alignment issues. */
1836 char fltset
[sizeof (fltset_t
)];
1840 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1842 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1844 #else /* ioctl method */
1845 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1847 /* The above operation renders the procinfo's cached pstatus obsolete. */
1848 pi
->status_valid
= 0;
1854 * Function: proc_set_traced_sysentry
1856 * Register to trace entry to system calls in the process or LWP.
1857 * Returns non-zero for success, zero for failure.
1861 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1866 * We should never have to apply this operation to any procinfo
1867 * except the one for the main process. If that ever changes
1868 * for any reason, then take out the following clause and
1869 * replace it with one that makes sure the ctl_fd is open.
1873 pi
= find_procinfo_or_die (pi
->pid
, 0);
1877 struct gdb_proc_ctl_pcsentry
{
1879 /* Use char array to avoid alignment issues. */
1880 char sysset
[sizeof (sysset_t
)];
1882 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1884 + sysset_t_size (pi
);
1886 argp
= xmalloc (argp_size
);
1888 argp
->cmd
= PCSENTRY
;
1889 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1891 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1894 #else /* ioctl method */
1895 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1897 /* The above operation renders the procinfo's cached pstatus obsolete. */
1898 pi
->status_valid
= 0;
1904 * Function: proc_set_traced_sysexit
1906 * Register to trace exit from system calls in the process or LWP.
1907 * Returns non-zero for success, zero for failure.
1911 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1916 * We should never have to apply this operation to any procinfo
1917 * except the one for the main process. If that ever changes
1918 * for any reason, then take out the following clause and
1919 * replace it with one that makes sure the ctl_fd is open.
1923 pi
= find_procinfo_or_die (pi
->pid
, 0);
1927 struct gdb_proc_ctl_pcsexit
{
1929 /* Use char array to avoid alignment issues. */
1930 char sysset
[sizeof (sysset_t
)];
1932 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1934 + sysset_t_size (pi
);
1936 argp
= xmalloc (argp_size
);
1938 argp
->cmd
= PCSEXIT
;
1939 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1941 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1944 #else /* ioctl method */
1945 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1947 /* The above operation renders the procinfo's cached pstatus obsolete. */
1948 pi
->status_valid
= 0;
1954 * Function: proc_set_held_signals
1956 * Specify the set of blocked / held signals in the process or LWP.
1957 * Returns non-zero for success, zero for failure.
1961 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1966 * We should never have to apply this operation to any procinfo
1967 * except the one for the main process. If that ever changes
1968 * for any reason, then take out the following clause and
1969 * replace it with one that makes sure the ctl_fd is open.
1973 pi
= find_procinfo_or_die (pi
->pid
, 0);
1979 /* Use char array to avoid alignment issues. */
1980 char hold
[sizeof (gdb_sigset_t
)];
1984 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1985 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1988 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1990 /* The above operation renders the procinfo's cached pstatus obsolete. */
1991 pi
->status_valid
= 0;
1997 * Function: proc_get_pending_signals
1999 * returns the set of signals that are pending in the process or LWP.
2000 * Will also copy the sigset if 'save' is non-zero.
2004 proc_get_pending_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2006 gdb_sigset_t
*ret
= NULL
;
2009 * We should never have to apply this operation to any procinfo
2010 * except the one for the main process. If that ever changes
2011 * for any reason, then take out the following clause and
2012 * replace it with one that makes sure the ctl_fd is open.
2016 pi
= find_procinfo_or_die (pi
->pid
, 0);
2018 if (!pi
->status_valid
)
2019 if (!proc_get_status (pi
))
2023 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
2025 ret
= &pi
->prstatus
.pr_sigpend
;
2028 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2034 * Function: proc_get_signal_actions
2036 * returns the set of signal actions.
2037 * Will also copy the sigactionset if 'save' is non-zero.
2041 proc_get_signal_actions (procinfo
*pi
, gdb_sigaction_t
*save
)
2043 gdb_sigaction_t
*ret
= NULL
;
2046 * We should never have to apply this operation to any procinfo
2047 * except the one for the main process. If that ever changes
2048 * for any reason, then take out the following clause and
2049 * replace it with one that makes sure the ctl_fd is open.
2053 pi
= find_procinfo_or_die (pi
->pid
, 0);
2055 if (!pi
->status_valid
)
2056 if (!proc_get_status (pi
))
2060 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
2062 ret
= &pi
->prstatus
.pr_action
;
2065 memcpy (save
, ret
, sizeof (gdb_sigaction_t
));
2071 * Function: proc_get_held_signals
2073 * returns the set of signals that are held / blocked.
2074 * Will also copy the sigset if 'save' is non-zero.
2078 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2080 gdb_sigset_t
*ret
= NULL
;
2083 * We should never have to apply this operation to any procinfo
2084 * except the one for the main process. If that ever changes
2085 * for any reason, then take out the following clause and
2086 * replace it with one that makes sure the ctl_fd is open.
2090 pi
= find_procinfo_or_die (pi
->pid
, 0);
2093 if (!pi
->status_valid
)
2094 if (!proc_get_status (pi
))
2098 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
2100 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
2101 #endif /* UNIXWARE */
2102 #else /* not NEW_PROC_API */
2104 static gdb_sigset_t sigheld
;
2106 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
2109 #endif /* NEW_PROC_API */
2111 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2117 * Function: proc_get_traced_signals
2119 * returns the set of signals that are traced / debugged.
2120 * Will also copy the sigset if 'save' is non-zero.
2124 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2126 gdb_sigset_t
*ret
= NULL
;
2129 * We should never have to apply this operation to any procinfo
2130 * except the one for the main process. If that ever changes
2131 * for any reason, then take out the following clause and
2132 * replace it with one that makes sure the ctl_fd is open.
2136 pi
= find_procinfo_or_die (pi
->pid
, 0);
2139 if (!pi
->status_valid
)
2140 if (!proc_get_status (pi
))
2143 ret
= &pi
->prstatus
.pr_sigtrace
;
2146 static gdb_sigset_t sigtrace
;
2148 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
2153 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2159 * Function: proc_trace_signal
2161 * Add 'signo' to the set of signals that are traced.
2162 * Returns non-zero for success, zero for failure.
2166 proc_trace_signal (procinfo
*pi
, int signo
)
2171 * We should never have to apply this operation to any procinfo
2172 * except the one for the main process. If that ever changes
2173 * for any reason, then take out the following clause and
2174 * replace it with one that makes sure the ctl_fd is open.
2178 pi
= find_procinfo_or_die (pi
->pid
, 0);
2182 if (proc_get_traced_signals (pi
, &temp
))
2184 praddset (&temp
, signo
);
2185 return proc_set_traced_signals (pi
, &temp
);
2189 return 0; /* failure */
2193 * Function: proc_ignore_signal
2195 * Remove 'signo' from the set of signals that are traced.
2196 * Returns non-zero for success, zero for failure.
2200 proc_ignore_signal (procinfo
*pi
, int signo
)
2205 * We should never have to apply this operation to any procinfo
2206 * except the one for the main process. If that ever changes
2207 * for any reason, then take out the following clause and
2208 * replace it with one that makes sure the ctl_fd is open.
2212 pi
= find_procinfo_or_die (pi
->pid
, 0);
2216 if (proc_get_traced_signals (pi
, &temp
))
2218 prdelset (&temp
, signo
);
2219 return proc_set_traced_signals (pi
, &temp
);
2223 return 0; /* failure */
2227 * Function: proc_get_traced_faults
2229 * returns the set of hardware faults that are traced /debugged.
2230 * Will also copy the faultset if 'save' is non-zero.
2234 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
2236 fltset_t
*ret
= NULL
;
2239 * We should never have to apply this operation to any procinfo
2240 * except the one for the main process. If that ever changes
2241 * for any reason, then take out the following clause and
2242 * replace it with one that makes sure the ctl_fd is open.
2246 pi
= find_procinfo_or_die (pi
->pid
, 0);
2249 if (!pi
->status_valid
)
2250 if (!proc_get_status (pi
))
2253 ret
= &pi
->prstatus
.pr_flttrace
;
2256 static fltset_t flttrace
;
2258 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2263 memcpy (save
, ret
, sizeof (fltset_t
));
2269 * Function: proc_get_traced_sysentry
2271 * returns the set of syscalls that are traced /debugged on entry.
2272 * Will also copy the syscall set if 'save' is non-zero.
2276 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
2278 sysset_t
*ret
= NULL
;
2281 * We should never have to apply this operation to any procinfo
2282 * except the one for the main process. If that ever changes
2283 * for any reason, then take out the following clause and
2284 * replace it with one that makes sure the ctl_fd is open.
2288 pi
= find_procinfo_or_die (pi
->pid
, 0);
2291 if (!pi
->status_valid
)
2292 if (!proc_get_status (pi
))
2295 #ifndef DYNAMIC_SYSCALLS
2296 ret
= &pi
->prstatus
.pr_sysentry
;
2297 #else /* DYNAMIC_SYSCALLS */
2299 static sysset_t
*sysentry
;
2303 sysentry
= sysset_t_alloc (pi
);
2305 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2307 if (pi
->prstatus
.pr_sysentry_offset
== 0)
2309 gdb_premptysysset (sysentry
);
2315 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
2317 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
2319 size
= sysset_t_size (pi
);
2320 gdb_premptysysset (sysentry
);
2321 rsize
= read (pi
->status_fd
, sysentry
, size
);
2326 #endif /* DYNAMIC_SYSCALLS */
2327 #else /* !NEW_PROC_API */
2329 static sysset_t sysentry
;
2331 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2334 #endif /* NEW_PROC_API */
2336 memcpy (save
, ret
, sysset_t_size (pi
));
2342 * Function: proc_get_traced_sysexit
2344 * returns the set of syscalls that are traced /debugged on exit.
2345 * Will also copy the syscall set if 'save' is non-zero.
2349 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
2351 sysset_t
* ret
= NULL
;
2354 * We should never have to apply this operation to any procinfo
2355 * except the one for the main process. If that ever changes
2356 * for any reason, then take out the following clause and
2357 * replace it with one that makes sure the ctl_fd is open.
2361 pi
= find_procinfo_or_die (pi
->pid
, 0);
2364 if (!pi
->status_valid
)
2365 if (!proc_get_status (pi
))
2368 #ifndef DYNAMIC_SYSCALLS
2369 ret
= &pi
->prstatus
.pr_sysexit
;
2370 #else /* DYNAMIC_SYSCALLS */
2372 static sysset_t
*sysexit
;
2376 sysexit
= sysset_t_alloc (pi
);
2378 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2380 if (pi
->prstatus
.pr_sysexit_offset
== 0)
2382 gdb_premptysysset (sysexit
);
2388 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
, SEEK_SET
)
2389 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2391 size
= sysset_t_size (pi
);
2392 gdb_premptysysset (sysexit
);
2393 rsize
= read (pi
->status_fd
, sysexit
, size
);
2398 #endif /* DYNAMIC_SYSCALLS */
2401 static sysset_t sysexit
;
2403 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2408 memcpy (save
, ret
, sysset_t_size (pi
));
2414 * Function: proc_clear_current_fault
2416 * The current fault (if any) is cleared; the associated signal
2417 * will not be sent to the process or LWP when it resumes.
2418 * Returns non-zero for success, zero for failure.
2422 proc_clear_current_fault (procinfo
*pi
)
2427 * We should never have to apply this operation to any procinfo
2428 * except the one for the main process. If that ever changes
2429 * for any reason, then take out the following clause and
2430 * replace it with one that makes sure the ctl_fd is open.
2434 pi
= find_procinfo_or_die (pi
->pid
, 0);
2438 procfs_ctl_t cmd
= PCCFAULT
;
2439 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2442 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2449 * Function: proc_set_current_signal
2451 * Set the "current signal" that will be delivered next to the process.
2452 * NOTE: semantics are different from those of KILL.
2453 * This signal will be delivered to the process or LWP
2454 * immediately when it is resumed (even if the signal is held/blocked);
2455 * it will NOT immediately cause another event of interest, and will NOT
2456 * first trap back to the debugger.
2458 * Returns non-zero for success, zero for failure.
2462 proc_set_current_signal (procinfo
*pi
, int signo
)
2467 /* Use char array to avoid alignment issues. */
2468 char sinfo
[sizeof (gdb_siginfo_t
)];
2470 gdb_siginfo_t
*mysinfo
;
2473 * We should never have to apply this operation to any procinfo
2474 * except the one for the main process. If that ever changes
2475 * for any reason, then take out the following clause and
2476 * replace it with one that makes sure the ctl_fd is open.
2480 pi
= find_procinfo_or_die (pi
->pid
, 0);
2482 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2483 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2484 * receives a PIOCSSIG with a signal identical to the current signal,
2485 * it messes up the current signal. Work around the kernel bug.
2488 signo
== proc_cursig (pi
))
2489 return 1; /* I assume this is a success? */
2492 /* The pointer is just a type alias. */
2493 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2494 mysinfo
->si_signo
= signo
;
2495 mysinfo
->si_code
= 0;
2496 mysinfo
->si_pid
= getpid (); /* ?why? */
2497 mysinfo
->si_uid
= getuid (); /* ?why? */
2501 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2503 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2510 * Function: proc_clear_current_signal
2512 * The current signal (if any) is cleared, and
2513 * is not sent to the process or LWP when it resumes.
2514 * Returns non-zero for success, zero for failure.
2518 proc_clear_current_signal (procinfo
*pi
)
2523 * We should never have to apply this operation to any procinfo
2524 * except the one for the main process. If that ever changes
2525 * for any reason, then take out the following clause and
2526 * replace it with one that makes sure the ctl_fd is open.
2530 pi
= find_procinfo_or_die (pi
->pid
, 0);
2536 /* Use char array to avoid alignment issues. */
2537 char sinfo
[sizeof (gdb_siginfo_t
)];
2539 gdb_siginfo_t
*mysinfo
;
2542 /* The pointer is just a type alias. */
2543 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2544 mysinfo
->si_signo
= 0;
2545 mysinfo
->si_code
= 0;
2546 mysinfo
->si_errno
= 0;
2547 mysinfo
->si_pid
= getpid (); /* ?why? */
2548 mysinfo
->si_uid
= getuid (); /* ?why? */
2550 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2553 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2560 * Function: proc_get_gregs
2562 * Get the general registers for the process or LWP.
2563 * Returns non-zero for success, zero for failure.
2567 proc_get_gregs (procinfo
*pi
)
2569 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2570 if (!proc_get_status (pi
))
2574 * OK, sorry about the ifdef's.
2575 * There's three cases instead of two, because
2576 * in this instance Unixware and Solaris/RW differ.
2580 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2581 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2582 #else /* not Unixware */
2583 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2584 #endif /* Unixware */
2585 #else /* not NEW_PROC_API */
2586 return &pi
->prstatus
.pr_reg
;
2587 #endif /* NEW_PROC_API */
2591 * Function: proc_get_fpregs
2593 * Get the floating point registers for the process or LWP.
2594 * Returns non-zero for success, zero for failure.
2598 proc_get_fpregs (procinfo
*pi
)
2601 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2602 if (!proc_get_status (pi
))
2605 #ifdef UNIXWARE /* a true architecture dependency */
2606 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2608 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2609 #endif /* Unixware */
2611 #else /* not NEW_PROC_API */
2612 if (pi
->fpregs_valid
)
2613 return &pi
->fpregset
; /* already got 'em */
2616 if (pi
->ctl_fd
== 0 &&
2617 open_procinfo_files (pi
, FD_CTL
) == 0)
2626 tid_t pr_error_thread
;
2627 tfpregset_t thread_1
;
2630 thread_fpregs
.pr_count
= 1;
2631 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2634 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2636 pi
->fpregs_valid
= 1;
2637 return &pi
->fpregset
; /* got 'em now! */
2639 else if (pi
->tid
!= 0 &&
2640 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2642 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2643 sizeof (pi
->fpregset
));
2644 pi
->fpregs_valid
= 1;
2645 return &pi
->fpregset
; /* got 'em now! */
2652 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2654 pi
->fpregs_valid
= 1;
2655 return &pi
->fpregset
; /* got 'em now! */
2668 * Function: proc_set_gregs
2670 * Write the general registers back to the process or LWP.
2671 * Returns non-zero for success, zero for failure.
2675 proc_set_gregs (procinfo
*pi
)
2677 gdb_gregset_t
*gregs
;
2680 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2681 return 0; /* get_regs has already warned */
2683 if (pi
->ctl_fd
== 0 &&
2684 open_procinfo_files (pi
, FD_CTL
) == 0)
2693 /* Use char array to avoid alignment issues. */
2694 char gregs
[sizeof (gdb_gregset_t
)];
2698 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2699 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2701 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2705 /* Policy: writing the regs invalidates our cache. */
2706 pi
->gregs_valid
= 0;
2711 * Function: proc_set_fpregs
2713 * Modify the floating point register set of the process or LWP.
2714 * Returns non-zero for success, zero for failure.
2718 proc_set_fpregs (procinfo
*pi
)
2720 gdb_fpregset_t
*fpregs
;
2723 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2724 return 0; /* get_fpregs has already warned */
2726 if (pi
->ctl_fd
== 0 &&
2727 open_procinfo_files (pi
, FD_CTL
) == 0)
2736 /* Use char array to avoid alignment issues. */
2737 char fpregs
[sizeof (gdb_fpregset_t
)];
2741 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2742 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2746 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2751 tid_t pr_error_thread
;
2752 tfpregset_t thread_1
;
2755 thread_fpregs
.pr_count
= 1;
2756 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2757 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2759 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2762 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2763 #endif /* osf PIOCTSFPREG */
2764 #endif /* NEW_PROC_API */
2767 /* Policy: writing the regs invalidates our cache. */
2768 pi
->fpregs_valid
= 0;
2773 * Function: proc_kill
2775 * Send a signal to the proc or lwp with the semantics of "kill()".
2776 * Returns non-zero for success, zero for failure.
2780 proc_kill (procinfo
*pi
, int signo
)
2785 * We might conceivably apply this operation to an LWP, and
2786 * the LWP's ctl file descriptor might not be open.
2789 if (pi
->ctl_fd
== 0 &&
2790 open_procinfo_files (pi
, FD_CTL
) == 0)
2797 procfs_ctl_t cmd
[2];
2801 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2802 #else /* ioctl method */
2803 /* FIXME: do I need the Alpha OSF fixups present in
2804 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2805 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2813 * Function: proc_parent_pid
2815 * Find the pid of the process that started this one.
2816 * Returns the parent process pid, or zero.
2820 proc_parent_pid (procinfo
*pi
)
2823 * We should never have to apply this operation to any procinfo
2824 * except the one for the main process. If that ever changes
2825 * for any reason, then take out the following clause and
2826 * replace it with one that makes sure the ctl_fd is open.
2830 pi
= find_procinfo_or_die (pi
->pid
, 0);
2832 if (!pi
->status_valid
)
2833 if (!proc_get_status (pi
))
2836 return pi
->prstatus
.pr_ppid
;
2841 * Function: proc_set_watchpoint
2846 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2848 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2851 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2852 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2857 char watch
[sizeof (prwatch_t
)];
2861 pwatch
= (prwatch_t
*) &arg
.watch
;
2862 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2863 pwatch
->pr_vaddr
= (uintptr_t) address_to_host_pointer (addr
);
2865 pwatch
->pr_vaddr
= (caddr_t
) address_to_host_pointer (addr
);
2867 pwatch
->pr_size
= len
;
2868 pwatch
->pr_wflags
= wflags
;
2869 #if defined(NEW_PROC_API) && defined (PCWATCH)
2871 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2873 #if defined (PIOCSWATCH)
2874 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2876 return 0; /* Fail */
2883 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2885 #include <sys/sysi86.h>
2888 * Function: proc_get_LDT_entry
2894 * The 'key' is actually the value of the lower 16 bits of
2895 * the GS register for the LWP that we're interested in.
2897 * Return: matching ssh struct (LDT entry).
2901 proc_get_LDT_entry (procinfo
*pi
, int key
)
2903 static struct ssd
*ldt_entry
= NULL
;
2905 char pathname
[MAX_PROC_NAME_SIZE
];
2906 struct cleanup
*old_chain
= NULL
;
2909 /* Allocate space for one LDT entry.
2910 This alloc must persist, because we return a pointer to it. */
2911 if (ldt_entry
== NULL
)
2912 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2914 /* Open the file descriptor for the LDT table. */
2915 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2916 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2918 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2921 /* Make sure it gets closed again! */
2922 old_chain
= make_cleanup_close (fd
);
2924 /* Now 'read' thru the table, find a match and return it. */
2925 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2927 if (ldt_entry
->sel
== 0 &&
2928 ldt_entry
->bo
== 0 &&
2929 ldt_entry
->acc1
== 0 &&
2930 ldt_entry
->acc2
== 0)
2931 break; /* end of table */
2932 /* If key matches, return this entry. */
2933 if (ldt_entry
->sel
== key
)
2936 /* Loop ended, match not found. */
2940 static int nalloc
= 0;
2942 /* Get the number of LDT entries. */
2943 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2945 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2949 /* Allocate space for the number of LDT entries. */
2950 /* This alloc has to persist, 'cause we return a pointer to it. */
2953 ldt_entry
= (struct ssd
*)
2954 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2958 /* Read the whole table in one gulp. */
2959 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2961 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2965 /* Search the table and return the (first) entry matching 'key'. */
2966 for (i
= 0; i
< nldt
; i
++)
2967 if (ldt_entry
[i
].sel
== key
)
2968 return &ldt_entry
[i
];
2970 /* Loop ended, match not found. */
2975 #endif /* TM_I386SOL2_H */
2977 /* =============== END, non-thread part of /proc "MODULE" =============== */
2979 /* =================== Thread "MODULE" =================== */
2981 /* NOTE: you'll see more ifdefs and duplication of functions here,
2982 since there is a different way to do threads on every OS. */
2985 * Function: proc_get_nthreads
2987 * Return the number of threads for the process
2990 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2995 proc_get_nthreads (procinfo
*pi
)
2999 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
3000 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
3006 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3008 * Solaris and Unixware version
3011 proc_get_nthreads (procinfo
*pi
)
3013 if (!pi
->status_valid
)
3014 if (!proc_get_status (pi
))
3018 * NEW_PROC_API: only works for the process procinfo,
3019 * because the LWP procinfos do not get prstatus filled in.
3022 if (pi
->tid
!= 0) /* find the parent process procinfo */
3023 pi
= find_procinfo_or_die (pi
->pid
, 0);
3025 return pi
->prstatus
.pr_nlwp
;
3033 proc_get_nthreads (procinfo
*pi
)
3041 * Function: proc_get_current_thread (LWP version)
3043 * Return the ID of the thread that had an event of interest.
3044 * (ie. the one that hit a breakpoint or other traced event).
3045 * All other things being equal, this should be the ID of a
3046 * thread that is currently executing.
3049 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3051 * Solaris and Unixware version
3054 proc_get_current_thread (procinfo
*pi
)
3057 * Note: this should be applied to the root procinfo for the process,
3058 * not to the procinfo for an LWP. If applied to the procinfo for
3059 * an LWP, it will simply return that LWP's ID. In that case,
3060 * find the parent process procinfo.
3064 pi
= find_procinfo_or_die (pi
->pid
, 0);
3066 if (!pi
->status_valid
)
3067 if (!proc_get_status (pi
))
3071 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
3073 return pi
->prstatus
.pr_who
;
3078 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3083 proc_get_current_thread (procinfo
*pi
)
3085 #if 0 /* FIXME: not ready for prime time? */
3086 return pi
->prstatus
.pr_tid
;
3097 proc_get_current_thread (procinfo
*pi
)
3106 * Function: proc_update_threads
3108 * Discover the IDs of all the threads within the process, and
3109 * create a procinfo for each of them (chained to the parent).
3111 * This unfortunately requires a different method on every OS.
3113 * Return: non-zero for success, zero for failure.
3117 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
3119 if (thread
&& parent
) /* sanity */
3121 thread
->status_valid
= 0;
3122 if (!proc_get_status (thread
))
3123 destroy_one_procinfo (&parent
->thread_list
, thread
);
3125 return 0; /* keep iterating */
3128 #if defined (PIOCLSTATUS)
3130 * Solaris 2.5 (ioctl) version
3133 proc_update_threads (procinfo
*pi
)
3135 gdb_prstatus_t
*prstatus
;
3136 struct cleanup
*old_chain
= NULL
;
3141 * We should never have to apply this operation to any procinfo
3142 * except the one for the main process. If that ever changes
3143 * for any reason, then take out the following clause and
3144 * replace it with one that makes sure the ctl_fd is open.
3148 pi
= find_procinfo_or_die (pi
->pid
, 0);
3150 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3152 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
3153 return 1; /* Process is not multi-threaded; nothing to do. */
3155 prstatus
= xmalloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1));
3157 old_chain
= make_cleanup (xfree
, prstatus
);
3158 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
3159 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
3161 /* Skip element zero, which represents the process as a whole. */
3162 for (i
= 1; i
< nlwp
+ 1; i
++)
3164 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
3165 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3167 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
3168 thread
->status_valid
= 1;
3170 pi
->threads_valid
= 1;
3171 do_cleanups (old_chain
);
3177 * Unixware and Solaris 6 (and later) version
3180 do_closedir_cleanup (void *dir
)
3186 proc_update_threads (procinfo
*pi
)
3188 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3189 struct dirent
*direntry
;
3190 struct cleanup
*old_chain
= NULL
;
3196 * We should never have to apply this operation to any procinfo
3197 * except the one for the main process. If that ever changes
3198 * for any reason, then take out the following clause and
3199 * replace it with one that makes sure the ctl_fd is open.
3203 pi
= find_procinfo_or_die (pi
->pid
, 0);
3205 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3210 * Note: this brute-force method is the only way I know of
3211 * to accomplish this task on Unixware. This method will
3212 * also work on Solaris 2.6 and 2.7. There is a much simpler
3213 * and more elegant way to do this on Solaris, but the margins
3214 * of this manuscript are too small to write it here... ;-)
3217 strcpy (pathname
, pi
->pathname
);
3218 strcat (pathname
, "/lwp");
3219 if ((dirp
= opendir (pathname
)) == NULL
)
3220 proc_error (pi
, "update_threads, opendir", __LINE__
);
3222 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
3223 while ((direntry
= readdir (dirp
)) != NULL
)
3224 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3226 lwpid
= atoi (&direntry
->d_name
[0]);
3227 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3228 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3230 pi
->threads_valid
= 1;
3231 do_cleanups (old_chain
);
3240 proc_update_threads (procinfo
*pi
)
3246 * We should never have to apply this operation to any procinfo
3247 * except the one for the main process. If that ever changes
3248 * for any reason, then take out the following clause and
3249 * replace it with one that makes sure the ctl_fd is open.
3253 pi
= find_procinfo_or_die (pi
->pid
, 0);
3255 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3257 nthreads
= proc_get_nthreads (pi
);
3259 return 0; /* nothing to do for 1 or fewer threads */
3261 threads
= xmalloc (nthreads
* sizeof (tid_t
));
3263 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3264 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3266 for (i
= 0; i
< nthreads
; i
++)
3268 if (!find_procinfo (pi
->pid
, threads
[i
]))
3269 if (!create_procinfo (pi
->pid
, threads
[i
]))
3270 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3272 pi
->threads_valid
= 1;
3280 proc_update_threads (procinfo
*pi
)
3284 #endif /* OSF PIOCTLIST */
3285 #endif /* NEW_PROC_API */
3286 #endif /* SOL 2.5 PIOCLSTATUS */
3289 * Function: proc_iterate_over_threads
3292 * Given a pointer to a function, call that function once
3293 * for each lwp in the procinfo list, until the function
3294 * returns non-zero, in which event return the value
3295 * returned by the function.
3297 * Note: this function does NOT call update_threads.
3298 * If you want to discover new threads first, you must
3299 * call that function explicitly. This function just makes
3300 * a quick pass over the currently-known procinfos.
3303 * pi - parent process procinfo
3304 * func - per-thread function
3305 * ptr - opaque parameter for function.
3308 * First non-zero return value from the callee, or zero.
3312 proc_iterate_over_threads (procinfo
*pi
,
3313 int (*func
) (procinfo
*, procinfo
*, void *),
3316 procinfo
*thread
, *next
;
3320 * We should never have to apply this operation to any procinfo
3321 * except the one for the main process. If that ever changes
3322 * for any reason, then take out the following clause and
3323 * replace it with one that makes sure the ctl_fd is open.
3327 pi
= find_procinfo_or_die (pi
->pid
, 0);
3329 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3331 next
= thread
->next
; /* in case thread is destroyed */
3332 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3339 /* =================== END, Thread "MODULE" =================== */
3341 /* =================== END, /proc "MODULE" =================== */
3343 /* =================== GDB "MODULE" =================== */
3346 * Here are all of the gdb target vector functions and their friends.
3349 static ptid_t
do_attach (ptid_t ptid
);
3350 static void do_detach (int signo
);
3351 static int register_gdb_signals (procinfo
*, gdb_sigset_t
*);
3354 * Function: procfs_debug_inferior
3356 * Sets up the inferior to be debugged.
3357 * Registers to trace signals, hardware faults, and syscalls.
3358 * Note: does not set RLC flag: caller may want to customize that.
3360 * Returns: zero for success (note! unlike most functions in this module)
3361 * On failure, returns the LINE NUMBER where it failed!
3365 procfs_debug_inferior (procinfo
*pi
)
3367 fltset_t traced_faults
;
3368 gdb_sigset_t traced_signals
;
3369 sysset_t
*traced_syscall_entries
;
3370 sysset_t
*traced_syscall_exits
;
3373 #ifdef PROCFS_DONT_TRACE_FAULTS
3374 /* On some systems (OSF), we don't trace hardware faults.
3375 Apparently it's enough that we catch them as signals.
3376 Wonder why we don't just do that in general? */
3377 premptyset (&traced_faults
); /* don't trace faults. */
3379 /* Register to trace hardware faults in the child. */
3380 prfillset (&traced_faults
); /* trace all faults... */
3381 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3383 if (!proc_set_traced_faults (pi
, &traced_faults
))
3386 /* Register to trace selected signals in the child. */
3387 premptyset (&traced_signals
);
3388 if (!register_gdb_signals (pi
, &traced_signals
))
3392 /* Register to trace the 'exit' system call (on entry). */
3393 traced_syscall_entries
= sysset_t_alloc (pi
);
3394 gdb_premptysysset (traced_syscall_entries
);
3396 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
3399 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3402 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
3404 #ifdef DYNAMIC_SYSCALLS
3406 int callnum
= find_syscall (pi
, "_exit");
3408 gdb_praddsysset (traced_syscall_entries
, callnum
);
3412 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
3413 xfree (traced_syscall_entries
);
3417 #ifdef PRFS_STOPEXEC /* defined on OSF */
3418 /* OSF method for tracing exec syscalls. Quoting:
3419 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3420 exits from exec system calls because of the user level loader. */
3421 /* FIXME: make nice and maybe move into an access function. */
3425 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3428 prfs_flags
|= PRFS_STOPEXEC
;
3430 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3433 #else /* not PRFS_STOPEXEC */
3434 /* Everyone else's (except OSF) method for tracing exec syscalls */
3436 Not all systems with /proc have all the exec* syscalls with the same
3437 names. On the SGI, for example, there is no SYS_exec, but there
3438 *is* a SYS_execv. So, we try to account for that. */
3440 traced_syscall_exits
= sysset_t_alloc (pi
);
3441 gdb_premptysysset (traced_syscall_exits
);
3443 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
3446 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
3449 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
3452 #ifdef SYS_lwpcreate
3453 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3454 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3457 #ifdef SYS_lwp_create /* FIXME: once only, please */
3458 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3459 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3462 #ifdef DYNAMIC_SYSCALLS
3464 int callnum
= find_syscall (pi
, "execve");
3466 gdb_praddsysset (traced_syscall_exits
, callnum
);
3467 callnum
= find_syscall (pi
, "ra_execve");
3469 gdb_praddsysset (traced_syscall_exits
, callnum
);
3473 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3474 xfree (traced_syscall_exits
);
3478 #endif /* PRFS_STOPEXEC */
3483 procfs_attach (char *args
, int from_tty
)
3489 error_no_arg ("process-id to attach");
3492 if (pid
== getpid ())
3493 error ("Attaching GDB to itself is not a good idea...");
3497 exec_file
= get_exec_file (0);
3500 printf_filtered ("Attaching to program `%s', %s\n",
3501 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3503 printf_filtered ("Attaching to %s\n",
3504 target_pid_to_str (pid_to_ptid (pid
)));
3508 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3509 push_target (&procfs_ops
);
3513 procfs_detach (char *args
, int from_tty
)
3520 exec_file
= get_exec_file (0);
3523 printf_filtered ("Detaching from program: %s %s\n",
3524 exec_file
, target_pid_to_str (inferior_ptid
));
3528 signo
= atoi (args
);
3531 inferior_ptid
= null_ptid
;
3532 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3536 do_attach (ptid_t ptid
)
3541 if ((pi
= create_procinfo (PIDGET (ptid
), 0)) == NULL
)
3542 perror ("procfs: out of memory in 'attach'");
3544 if (!open_procinfo_files (pi
, FD_CTL
))
3546 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3547 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3549 dead_procinfo (pi
, errmsg
, NOKILL
);
3552 /* Stop the process (if it isn't already stopped). */
3553 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3555 pi
->was_stopped
= 1;
3556 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3560 pi
->was_stopped
= 0;
3561 /* Set the process to run again when we close it. */
3562 if (!proc_set_run_on_last_close (pi
))
3563 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3565 /* Now stop the process. */
3566 if (!proc_stop_process (pi
))
3567 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3568 pi
->ignore_next_sigstop
= 1;
3570 /* Save some of the /proc state to be restored if we detach. */
3571 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3572 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3573 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3574 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3575 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3576 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3578 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3579 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3581 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3582 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3584 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3585 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3587 /* Let GDB know that the inferior was attached. */
3589 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3593 do_detach (int signo
)
3597 /* Find procinfo for the main process */
3598 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0); /* FIXME: threads */
3600 if (!proc_set_current_signal (pi
, signo
))
3601 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3603 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3604 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3606 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3607 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3609 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3610 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3612 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3613 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3615 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3616 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3618 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3619 if (signo
|| !(pi
->was_stopped
) ||
3620 query ("Was stopped when attached, make it runnable again? "))
3622 /* Clear any pending signal. */
3623 if (!proc_clear_current_fault (pi
))
3624 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3626 if (!proc_set_run_on_last_close (pi
))
3627 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3631 destroy_procinfo (pi
);
3637 * Since the /proc interface cannot give us individual registers,
3638 * we pay no attention to the (regno) argument, and just fetch them all.
3639 * This results in the possibility that we will do unnecessarily many
3640 * fetches, since we may be called repeatedly for individual registers.
3641 * So we cache the results, and mark the cache invalid when the process
3646 procfs_fetch_registers (int regno
)
3648 gdb_fpregset_t
*fpregs
;
3649 gdb_gregset_t
*gregs
;
3654 pid
= PIDGET (inferior_ptid
);
3655 tid
= TIDGET (inferior_ptid
);
3657 /* First look up procinfo for the main process. */
3658 pi
= find_procinfo_or_die (pid
, 0);
3660 /* If the event thread is not the same as GDB's requested thread
3661 (ie. inferior_ptid), then look up procinfo for the requested
3664 (tid
!= proc_get_current_thread (pi
)))
3665 pi
= find_procinfo_or_die (pid
, tid
);
3668 error ("procfs: fetch_registers failed to find procinfo for %s",
3669 target_pid_to_str (inferior_ptid
));
3671 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3672 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3674 supply_gregset (gregs
);
3676 if (FP0_REGNUM
>= 0) /* need floating point? */
3678 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3679 regno
== PC_REGNUM
||
3680 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3681 regno
== FP_REGNUM
||
3683 return; /* not a floating point register */
3685 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3686 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3688 supply_fpregset (fpregs
);
3692 /* Get ready to modify the registers array. On machines which store
3693 individual registers, this doesn't need to do anything. On
3694 machines which store all the registers in one fell swoop, such as
3695 /proc, this makes sure that registers contains all the registers
3696 from the program being debugged. */
3699 procfs_prepare_to_store (void)
3701 #ifdef CHILD_PREPARE_TO_STORE
3702 CHILD_PREPARE_TO_STORE ();
3709 * Since the /proc interface will not read individual registers,
3710 * we will cache these requests until the process is resumed, and
3711 * only then write them back to the inferior process.
3713 * FIXME: is that a really bad idea? Have to think about cases
3714 * where writing one register might affect the value of others, etc.
3718 procfs_store_registers (int regno
)
3720 gdb_fpregset_t
*fpregs
;
3721 gdb_gregset_t
*gregs
;
3726 pid
= PIDGET (inferior_ptid
);
3727 tid
= TIDGET (inferior_ptid
);
3729 /* First find procinfo for main process */
3730 pi
= find_procinfo_or_die (pid
, 0);
3732 /* If current lwp for process is not the same as requested thread
3733 (ie. inferior_ptid), then find procinfo for the requested thread. */
3736 (tid
!= proc_get_current_thread (pi
)))
3737 pi
= find_procinfo_or_die (pid
, tid
);
3740 error ("procfs: store_registers: failed to find procinfo for %s",
3741 target_pid_to_str (inferior_ptid
));
3743 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3744 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3746 fill_gregset (gregs
, regno
);
3747 if (!proc_set_gregs (pi
))
3748 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3750 if (FP0_REGNUM
>= 0) /* need floating point? */
3752 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3753 regno
== PC_REGNUM
||
3754 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3755 regno
== FP_REGNUM
||
3757 return; /* not a floating point register */
3759 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3760 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3762 fill_fpregset (fpregs
, regno
);
3763 if (!proc_set_fpregs (pi
))
3764 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3769 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3773 if (scall
== SYS_lwp_exit
)
3777 if (scall
== SYS_lwpexit
)
3784 syscall_is_exit (procinfo
*pi
, int scall
)
3787 if (scall
== SYS_exit
)
3790 #ifdef DYNAMIC_SYSCALLS
3791 if (find_syscall (pi
, "_exit") == scall
)
3798 syscall_is_exec (procinfo
*pi
, int scall
)
3801 if (scall
== SYS_exec
)
3805 if (scall
== SYS_execv
)
3809 if (scall
== SYS_execve
)
3812 #ifdef DYNAMIC_SYSCALLS
3813 if (find_syscall (pi
, "_execve"))
3815 if (find_syscall (pi
, "ra_execve"))
3822 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3824 #ifdef SYS_lwp_create
3825 if (scall
== SYS_lwp_create
)
3828 #ifdef SYS_lwpcreate
3829 if (scall
== SYS_lwpcreate
)
3836 * Function: target_wait
3838 * Retrieve the next stop event from the child process.
3839 * If child has not stopped yet, wait for it to stop.
3840 * Translate /proc eventcodes (or possibly wait eventcodes)
3841 * into gdb internal event codes.
3843 * Return: id of process (and possibly thread) that incurred the event.
3844 * event codes are returned thru a pointer parameter.
3848 procfs_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3850 /* First cut: loosely based on original version 2.1 */
3854 ptid_t retval
, temp_ptid
;
3855 int why
, what
, flags
;
3862 retval
= pid_to_ptid (-1);
3864 /* Find procinfo for main process */
3865 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
3868 /* We must assume that the status is stale now... */
3869 pi
->status_valid
= 0;
3870 pi
->gregs_valid
= 0;
3871 pi
->fpregs_valid
= 0;
3873 #if 0 /* just try this out... */
3874 flags
= proc_flags (pi
);
3875 why
= proc_why (pi
);
3876 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3877 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3879 /* If child is not stopped, wait for it to stop. */
3880 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3881 !proc_wait_for_stop (pi
))
3883 /* wait_for_stop failed: has the child terminated? */
3884 if (errno
== ENOENT
)
3888 /* /proc file not found; presumably child has terminated. */
3889 wait_retval
= wait (&wstat
); /* "wait" for the child's exit */
3891 if (wait_retval
!= PIDGET (inferior_ptid
)) /* wrong child? */
3892 error ("procfs: couldn't stop process %d: wait returned %d\n",
3893 PIDGET (inferior_ptid
), wait_retval
);
3894 /* FIXME: might I not just use waitpid?
3895 Or try find_procinfo to see if I know about this child? */
3896 retval
= pid_to_ptid (wait_retval
);
3898 else if (errno
== EINTR
)
3902 /* Unknown error from wait_for_stop. */
3903 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3908 /* This long block is reached if either:
3909 a) the child was already stopped, or
3910 b) we successfully waited for the child with wait_for_stop.
3911 This block will analyze the /proc status, and translate it
3912 into a waitstatus for GDB.
3914 If we actually had to call wait because the /proc file
3915 is gone (child terminated), then we skip this block,
3916 because we already have a waitstatus. */
3918 flags
= proc_flags (pi
);
3919 why
= proc_why (pi
);
3920 what
= proc_what (pi
);
3922 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3925 /* If it's running async (for single_thread control),
3926 set it back to normal again. */
3927 if (flags
& PR_ASYNC
)
3928 if (!proc_unset_async (pi
))
3929 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3933 proc_prettyprint_why (why
, what
, 1);
3935 /* The 'pid' we will return to GDB is composed of
3936 the process ID plus the lwp ID. */
3937 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3941 wstat
= (what
<< 8) | 0177;
3944 if (syscall_is_lwp_exit (pi
, what
))
3946 printf_filtered ("[%s exited]\n",
3947 target_pid_to_str (retval
));
3948 delete_thread (retval
);
3949 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3952 else if (syscall_is_exit (pi
, what
))
3954 /* Handle SYS_exit call only */
3955 /* Stopped at entry to SYS_exit.
3956 Make it runnable, resume it, then use
3957 the wait system call to get its exit code.
3958 Proc_run_process always clears the current
3960 Then return its exit status. */
3961 pi
->status_valid
= 0;
3963 /* FIXME: what we should do is return
3964 TARGET_WAITKIND_SPURIOUS. */
3965 if (!proc_run_process (pi
, 0, 0))
3966 proc_error (pi
, "target_wait, run_process", __LINE__
);
3969 /* Don't call wait: simulate waiting for exit,
3970 return a "success" exit code. Bogus: what if
3971 it returns something else? */
3973 retval
= inferior_ptid
; /* ? ? ? */
3977 int temp
= wait (&wstat
);
3979 /* FIXME: shouldn't I make sure I get the right
3980 event from the right process? If (for
3981 instance) I have killed an earlier inferior
3982 process but failed to clean up after it
3983 somehow, I could get its termination event
3986 /* If wait returns -1, that's what we return to GDB. */
3988 retval
= pid_to_ptid (temp
);
3993 printf_filtered ("procfs: trapped on entry to ");
3994 proc_prettyprint_syscall (proc_what (pi
), 0);
3995 printf_filtered ("\n");
3998 long i
, nsysargs
, *sysargs
;
4000 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4001 (sysargs
= proc_sysargs (pi
)) != NULL
)
4003 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4004 for (i
= 0; i
< nsysargs
; i
++)
4005 printf_filtered ("#%ld: 0x%08lx\n",
4013 /* How to exit gracefully, returning "unknown event" */
4014 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4015 return inferior_ptid
;
4019 /* How to keep going without returning to wfi: */
4020 target_resume (ptid
, 0, TARGET_SIGNAL_0
);
4026 if (syscall_is_exec (pi
, what
))
4028 /* Hopefully this is our own "fork-child" execing
4029 the real child. Hoax this event into a trap, and
4030 GDB will see the child about to execute its start
4032 wstat
= (SIGTRAP
<< 8) | 0177;
4034 else if (syscall_is_lwp_create (pi
, what
))
4037 * This syscall is somewhat like fork/exec.
4038 * We will get the event twice: once for the parent LWP,
4039 * and once for the child. We should already know about
4040 * the parent LWP, but the child will be new to us. So,
4041 * whenever we get this event, if it represents a new
4042 * thread, simply add the thread to the list.
4045 /* If not in procinfo list, add it. */
4046 temp_tid
= proc_get_current_thread (pi
);
4047 if (!find_procinfo (pi
->pid
, temp_tid
))
4048 create_procinfo (pi
->pid
, temp_tid
);
4050 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4051 /* If not in GDB's thread list, add it. */
4052 if (!in_thread_list (temp_ptid
))
4054 printf_filtered ("[New %s]\n",
4055 target_pid_to_str (temp_ptid
));
4056 add_thread (temp_ptid
);
4058 /* Return to WFI, but tell it to immediately resume. */
4059 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4060 return inferior_ptid
;
4062 else if (syscall_is_lwp_exit (pi
, what
))
4064 printf_filtered ("[%s exited]\n",
4065 target_pid_to_str (retval
));
4066 delete_thread (retval
);
4067 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4072 /* FIXME: Do we need to handle SYS_sproc,
4073 SYS_fork, or SYS_vfork here? The old procfs
4074 seemed to use this event to handle threads on
4075 older (non-LWP) systems, where I'm assuming
4076 that threads were actually separate processes.
4077 Irix, maybe? Anyway, low priority for now. */
4081 printf_filtered ("procfs: trapped on exit from ");
4082 proc_prettyprint_syscall (proc_what (pi
), 0);
4083 printf_filtered ("\n");
4086 long i
, nsysargs
, *sysargs
;
4088 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4089 (sysargs
= proc_sysargs (pi
)) != NULL
)
4091 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4092 for (i
= 0; i
< nsysargs
; i
++)
4093 printf_filtered ("#%ld: 0x%08lx\n",
4098 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4099 return inferior_ptid
;
4104 wstat
= (SIGSTOP
<< 8) | 0177;
4109 printf_filtered ("Retry #%d:\n", retry
);
4110 pi
->status_valid
= 0;
4115 /* If not in procinfo list, add it. */
4116 temp_tid
= proc_get_current_thread (pi
);
4117 if (!find_procinfo (pi
->pid
, temp_tid
))
4118 create_procinfo (pi
->pid
, temp_tid
);
4120 /* If not in GDB's thread list, add it. */
4121 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4122 if (!in_thread_list (temp_ptid
))
4124 printf_filtered ("[New %s]\n",
4125 target_pid_to_str (temp_ptid
));
4126 add_thread (temp_ptid
);
4129 status
->kind
= TARGET_WAITKIND_STOPPED
;
4130 status
->value
.sig
= 0;
4135 wstat
= (what
<< 8) | 0177;
4138 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
4141 wstat
= (SIGTRAP
<< 8) | 0177;
4146 wstat
= (SIGTRAP
<< 8) | 0177;
4149 /* FIXME: use si_signo where possible. */
4151 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4154 wstat
= (SIGILL
<< 8) | 0177;
4157 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4160 wstat
= (SIGTRAP
<< 8) | 0177;
4164 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4167 wstat
= (SIGSEGV
<< 8) | 0177;
4171 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4174 wstat
= (SIGFPE
<< 8) | 0177;
4176 case FLTPAGE
: /* Recoverable page fault */
4177 default: /* FIXME: use si_signo if possible for fault */
4178 retval
= pid_to_ptid (-1);
4179 printf_filtered ("procfs:%d -- ", __LINE__
);
4180 printf_filtered ("child stopped for unknown reason:\n");
4181 proc_prettyprint_why (why
, what
, 1);
4182 error ("... giving up...");
4185 break; /* case PR_FAULTED: */
4186 default: /* switch (why) unmatched */
4187 printf_filtered ("procfs:%d -- ", __LINE__
);
4188 printf_filtered ("child stopped for unknown reason:\n");
4189 proc_prettyprint_why (why
, what
, 1);
4190 error ("... giving up...");
4194 * Got this far without error:
4195 * If retval isn't in the threads database, add it.
4197 if (PIDGET (retval
) > 0 &&
4198 !ptid_equal (retval
, inferior_ptid
) &&
4199 !in_thread_list (retval
))
4202 * We have a new thread.
4203 * We need to add it both to GDB's list and to our own.
4204 * If we don't create a procinfo, resume may be unhappy
4207 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
4208 add_thread (retval
);
4209 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
4210 create_procinfo (PIDGET (retval
), TIDGET (retval
));
4212 /* In addition, it's possible that this is the first
4213 * new thread we've seen, in which case we may not
4214 * have created entries for inferior_ptid yet.
4216 if (TIDGET (inferior_ptid
) != 0)
4218 if (!in_thread_list (inferior_ptid
))
4219 add_thread (inferior_ptid
);
4220 if (find_procinfo (PIDGET (inferior_ptid
),
4221 TIDGET (inferior_ptid
)) == NULL
)
4222 create_procinfo (PIDGET (inferior_ptid
),
4223 TIDGET (inferior_ptid
));
4227 else /* flags do not indicate STOPPED */
4229 /* surely this can't happen... */
4230 printf_filtered ("procfs:%d -- process not stopped.\n",
4232 proc_prettyprint_flags (flags
, 1);
4233 error ("procfs: ...giving up...");
4238 store_waitstatus (status
, wstat
);
4244 /* Transfer LEN bytes between GDB address MYADDR and target address
4245 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4246 otherwise transfer them from the target. TARGET is unused.
4248 The return value is 0 if an error occurred or no bytes were
4249 transferred. Otherwise, it will be a positive value which
4250 indicates the number of bytes transferred between gdb and the
4251 target. (Note that the interface also makes provisions for
4252 negative values, but this capability isn't implemented here.) */
4255 procfs_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int dowrite
,
4256 struct mem_attrib
*attrib
, struct target_ops
*target
)
4261 /* Find procinfo for main process */
4262 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4263 if (pi
->as_fd
== 0 &&
4264 open_procinfo_files (pi
, FD_AS
) == 0)
4266 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4270 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4275 PROCFS_NOTE ("write memory: ");
4277 PROCFS_NOTE ("write memory: \n");
4279 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4283 PROCFS_NOTE ("read memory: \n");
4284 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4295 * Function: invalidate_cache
4297 * Called by target_resume before making child runnable.
4298 * Mark cached registers and status's invalid.
4299 * If there are "dirty" caches that need to be written back
4300 * to the child process, do that.
4302 * File descriptors are also cached.
4303 * As they are a limited resource, we cannot hold onto them indefinitely.
4304 * However, as they are expensive to open, we don't want to throw them
4305 * away indescriminately either. As a compromise, we will keep the
4306 * file descriptors for the parent process, but discard any file
4307 * descriptors we may have accumulated for the threads.
4310 * As this function is called by iterate_over_threads, it always
4311 * returns zero (so that iterate_over_threads will keep iterating).
4316 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4319 * About to run the child; invalidate caches and do any other cleanup.
4323 if (pi
->gregs_dirty
)
4324 if (parent
== NULL
||
4325 proc_get_current_thread (parent
) != pi
->tid
)
4326 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4327 proc_warn (pi
, "target_resume, set_gregs",
4329 if (FP0_REGNUM
>= 0)
4330 if (pi
->fpregs_dirty
)
4331 if (parent
== NULL
||
4332 proc_get_current_thread (parent
) != pi
->tid
)
4333 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4334 proc_warn (pi
, "target_resume, set_fpregs",
4340 /* The presence of a parent indicates that this is an LWP.
4341 Close any file descriptors that it might have open.
4342 We don't do this to the master (parent) procinfo. */
4344 close_procinfo_files (pi
);
4346 pi
->gregs_valid
= 0;
4347 pi
->fpregs_valid
= 0;
4349 pi
->gregs_dirty
= 0;
4350 pi
->fpregs_dirty
= 0;
4352 pi
->status_valid
= 0;
4353 pi
->threads_valid
= 0;
4360 * Function: make_signal_thread_runnable
4362 * A callback function for iterate_over_threads.
4363 * Find the asynchronous signal thread, and make it runnable.
4364 * See if that helps matters any.
4368 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4371 if (proc_flags (pi
) & PR_ASLWP
)
4373 if (!proc_run_process (pi
, 0, -1))
4374 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4383 * Function: target_resume
4385 * Make the child process runnable. Normally we will then call
4386 * procfs_wait and wait for it to stop again (unles gdb is async).
4389 * step: if true, then arrange for the child to stop again
4390 * after executing a single instruction.
4391 * signo: if zero, then cancel any pending signal.
4392 * If non-zero, then arrange for the indicated signal
4393 * to be delivered to the child when it runs.
4394 * pid: if -1, then allow any child thread to run.
4395 * if non-zero, then allow only the indicated thread to run.
4396 ******* (not implemented yet)
4400 procfs_resume (ptid_t ptid
, int step
, enum target_signal signo
)
4402 procinfo
*pi
, *thread
;
4406 prrun.prflags |= PRSVADDR;
4407 prrun.pr_vaddr = $PC; set resume address
4408 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4409 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4410 prrun.prflags |= PRCFAULT; clear current fault.
4412 PRSTRACE and PRSFAULT can be done by other means
4413 (proc_trace_signals, proc_trace_faults)
4414 PRSVADDR is unnecessary.
4415 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4416 This basically leaves PRSTEP and PRCSIG.
4417 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4418 So basically PR_STEP is the sole argument that must be passed
4419 to proc_run_process (for use in the prrun struct by ioctl). */
4421 /* Find procinfo for main process */
4422 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4424 /* First cut: ignore pid argument */
4427 /* Convert signal to host numbering. */
4429 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4432 native_signo
= target_signal_to_host (signo
);
4434 pi
->ignore_next_sigstop
= 0;
4436 /* Running the process voids all cached registers and status. */
4437 /* Void the threads' caches first */
4438 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4439 /* Void the process procinfo's caches. */
4440 invalidate_cache (NULL
, pi
, NULL
);
4442 if (PIDGET (ptid
) != -1)
4444 /* Resume a specific thread, presumably suppressing the others. */
4445 thread
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
));
4448 if (thread
->tid
!= 0)
4450 /* We're to resume a specific thread, and not the others.
4451 * Set the child process's PR_ASYNC flag.
4454 if (!proc_set_async (pi
))
4455 proc_error (pi
, "target_resume, set_async", __LINE__
);
4458 proc_iterate_over_threads (pi
,
4459 make_signal_thread_runnable
,
4462 pi
= thread
; /* substitute the thread's procinfo for run */
4467 if (!proc_run_process (pi
, step
, native_signo
))
4470 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4472 proc_error (pi
, "target_resume", __LINE__
);
4477 * Function: register_gdb_signals
4479 * Traverse the list of signals that GDB knows about
4480 * (see "handle" command), and arrange for the target
4481 * to be stopped or not, according to these settings.
4483 * Returns non-zero for success, zero for failure.
4487 register_gdb_signals (procinfo
*pi
, gdb_sigset_t
*signals
)
4491 for (signo
= 0; signo
< NSIG
; signo
++)
4492 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4493 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4494 signal_pass_state (target_signal_from_host (signo
)) == 1)
4495 prdelset (signals
, signo
);
4497 praddset (signals
, signo
);
4499 return proc_set_traced_signals (pi
, signals
);
4503 * Function: target_notice_signals
4505 * Set up to trace signals in the child process.
4509 procfs_notice_signals (ptid_t ptid
)
4511 gdb_sigset_t signals
;
4512 procinfo
*pi
= find_procinfo_or_die (PIDGET (ptid
), 0);
4514 if (proc_get_traced_signals (pi
, &signals
) &&
4515 register_gdb_signals (pi
, &signals
))
4518 proc_error (pi
, "notice_signals", __LINE__
);
4522 * Function: target_files_info
4524 * Print status information about the child process.
4528 procfs_files_info (struct target_ops
*ignore
)
4530 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4531 attach_flag
? "attached": "child",
4532 target_pid_to_str (inferior_ptid
));
4536 * Function: target_open
4538 * A dummy: you don't open procfs.
4542 procfs_open (char *args
, int from_tty
)
4544 error ("Use the \"run\" command to start a Unix child process.");
4548 * Function: target_can_run
4550 * This tells GDB that this target vector can be invoked
4551 * for "run" or "attach".
4554 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4555 be a runnable target. Used by targets
4556 that can sit atop procfs, such as solaris
4561 procfs_can_run (void)
4563 /* This variable is controlled by modules that sit atop procfs that
4564 may layer their own process structure atop that provided here.
4565 sol-thread.c does this because of the Solaris two-level thread
4568 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4570 return !procfs_suppress_run
;
4574 * Function: target_stop
4576 * Stop the child process asynchronously, as when the
4577 * gdb user types control-c or presses a "stop" button.
4579 * Works by sending kill(SIGINT) to the child's process group.
4585 extern pid_t inferior_process_group
;
4587 kill (-inferior_process_group
, SIGINT
);
4591 * Function: unconditionally_kill_inferior
4593 * Make it die. Wait for it to die. Clean up after it.
4594 * Note: this should only be applied to the real process,
4595 * not to an LWP, because of the check for parent-process.
4596 * If we need this to work for an LWP, it needs some more logic.
4600 unconditionally_kill_inferior (procinfo
*pi
)
4604 parent_pid
= proc_parent_pid (pi
);
4605 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4606 /* FIXME: use access functions */
4607 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4608 before the PIOCKILL, otherwise it might generate a corrupted core
4609 file for the inferior. */
4610 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4612 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4615 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4616 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4617 to kill the inferior, otherwise it might remain stopped with a
4619 We do not check the result of the PIOCSSIG, the inferior might have
4622 gdb_siginfo_t newsiginfo
;
4624 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4625 newsiginfo
.si_signo
= SIGKILL
;
4626 newsiginfo
.si_code
= 0;
4627 newsiginfo
.si_errno
= 0;
4628 newsiginfo
.si_pid
= getpid ();
4629 newsiginfo
.si_uid
= getuid ();
4630 /* FIXME: use proc_set_current_signal */
4631 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4633 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4634 if (!proc_kill (pi
, SIGKILL
))
4635 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4636 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4637 destroy_procinfo (pi
);
4639 /* If pi is GDB's child, wait for it to die. */
4640 if (parent_pid
== getpid ())
4641 /* FIXME: should we use waitpid to make sure we get the right event?
4642 Should we check the returned event? */
4647 ret
= waitpid (pi
->pid
, &status
, 0);
4655 * Function: target_kill_inferior
4657 * We're done debugging it, and we want it to go away.
4658 * Then we want GDB to forget all about it.
4662 procfs_kill_inferior (void)
4664 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4666 /* Find procinfo for main process */
4667 procinfo
*pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4670 unconditionally_kill_inferior (pi
);
4671 target_mourn_inferior ();
4676 * Function: target_mourn_inferior
4678 * Forget we ever debugged this thing!
4682 procfs_mourn_inferior (void)
4686 if (!ptid_equal (inferior_ptid
, null_ptid
))
4688 /* Find procinfo for main process */
4689 pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4691 destroy_procinfo (pi
);
4693 unpush_target (&procfs_ops
);
4694 generic_mourn_inferior ();
4698 * Function: init_inferior
4700 * When GDB forks to create a runnable inferior process,
4701 * this function is called on the parent side of the fork.
4702 * It's job is to do whatever is necessary to make the child
4703 * ready to be debugged, and then wait for the child to synchronize.
4707 procfs_init_inferior (int pid
)
4710 gdb_sigset_t signals
;
4713 /* This routine called on the parent side (GDB side)
4714 after GDB forks the inferior. */
4716 push_target (&procfs_ops
);
4718 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4719 perror ("procfs: out of memory in 'init_inferior'");
4721 if (!open_procinfo_files (pi
, FD_CTL
))
4722 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4726 open_procinfo_files // done
4729 procfs_notice_signals
4736 /* If not stopped yet, wait for it to stop. */
4737 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4738 !(proc_wait_for_stop (pi
)))
4739 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4741 /* Save some of the /proc state to be restored if we detach. */
4742 /* FIXME: Why? In case another debugger was debugging it?
4743 We're it's parent, for Ghu's sake! */
4744 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4745 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4746 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4747 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4748 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4749 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4750 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4751 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4752 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4753 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4755 /* Register to trace selected signals in the child. */
4756 prfillset (&signals
);
4757 if (!register_gdb_signals (pi
, &signals
))
4758 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4760 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4761 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4763 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4764 and possibly even turning ON kill-on-last-close at this point. But
4765 I can't make that change without careful testing which I don't have
4766 time to do right now... */
4767 /* Turn on run-on-last-close flag so that the child
4768 will die if GDB goes away for some reason. */
4769 if (!proc_set_run_on_last_close (pi
))
4770 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4772 /* The 'process ID' we return to GDB is composed of
4773 the actual process ID plus the lwp ID. */
4774 inferior_ptid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4776 #ifdef START_INFERIOR_TRAPS_EXPECTED
4777 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4779 /* One trap to exec the shell, one to exec the program being debugged. */
4780 startup_inferior (2);
4781 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4785 * Function: set_exec_trap
4787 * When GDB forks to create a new process, this function is called
4788 * on the child side of the fork before GDB exec's the user program.
4789 * Its job is to make the child minimally debuggable, so that the
4790 * parent GDB process can connect to the child and take over.
4791 * This function should do only the minimum to make that possible,
4792 * and to synchronize with the parent process. The parent process
4793 * should take care of the details.
4797 procfs_set_exec_trap (void)
4799 /* This routine called on the child side (inferior side)
4800 after GDB forks the inferior. It must use only local variables,
4801 because it may be sharing data space with its parent. */
4806 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4807 perror_with_name ("procfs: create_procinfo failed in child.");
4809 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4811 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4812 gdb_flush (gdb_stderr
);
4813 /* no need to call "dead_procinfo", because we're going to exit. */
4817 #ifdef PRFS_STOPEXEC /* defined on OSF */
4818 /* OSF method for tracing exec syscalls. Quoting:
4819 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4820 exits from exec system calls because of the user level loader. */
4821 /* FIXME: make nice and maybe move into an access function. */
4825 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4827 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4828 gdb_flush (gdb_stderr
);
4831 prfs_flags
|= PRFS_STOPEXEC
;
4833 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4835 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4836 gdb_flush (gdb_stderr
);
4840 #else /* not PRFS_STOPEXEC */
4841 /* Everyone else's (except OSF) method for tracing exec syscalls */
4843 Not all systems with /proc have all the exec* syscalls with the same
4844 names. On the SGI, for example, there is no SYS_exec, but there
4845 *is* a SYS_execv. So, we try to account for that. */
4847 exitset
= sysset_t_alloc (pi
);
4848 gdb_premptysysset (exitset
);
4850 gdb_praddsysset (exitset
, SYS_exec
);
4853 gdb_praddsysset (exitset
, SYS_execve
);
4856 gdb_praddsysset (exitset
, SYS_execv
);
4858 #ifdef DYNAMIC_SYSCALLS
4860 int callnum
= find_syscall (pi
, "execve");
4863 gdb_praddsysset (exitset
, callnum
);
4865 callnum
= find_syscall (pi
, "ra_execve");
4867 gdb_praddsysset (exitset
, callnum
);
4869 #endif /* DYNAMIC_SYSCALLS */
4871 if (!proc_set_traced_sysexit (pi
, exitset
))
4873 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4874 gdb_flush (gdb_stderr
);
4877 #endif /* PRFS_STOPEXEC */
4879 /* FIXME: should this be done in the parent instead? */
4880 /* Turn off inherit on fork flag so that all grand-children
4881 of gdb start with tracing flags cleared. */
4882 if (!proc_unset_inherit_on_fork (pi
))
4883 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4885 /* Turn off run on last close flag, so that the child process
4886 cannot run away just because we close our handle on it.
4887 We want it to wait for the parent to attach. */
4888 if (!proc_unset_run_on_last_close (pi
))
4889 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4891 /* FIXME: No need to destroy the procinfo --
4892 we have our own address space, and we're about to do an exec! */
4893 /*destroy_procinfo (pi);*/
4897 * Function: create_inferior
4899 * This function is called BEFORE gdb forks the inferior process.
4900 * Its only real responsibility is to set things up for the fork,
4901 * and tell GDB which two functions to call after the fork (one
4902 * for the parent, and one for the child).
4904 * This function does a complicated search for a unix shell program,
4905 * which it then uses to parse arguments and environment variables
4906 * to be sent to the child. I wonder whether this code could not
4907 * be abstracted out and shared with other unix targets such as
4912 procfs_create_inferior (char *exec_file
, char *allargs
, char **env
)
4914 char *shell_file
= getenv ("SHELL");
4916 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4919 /* We will be looking down the PATH to find shell_file. If we
4920 just do this the normal way (via execlp, which operates by
4921 attempting an exec for each element of the PATH until it
4922 finds one which succeeds), then there will be an exec for
4923 each failed attempt, each of which will cause a PR_SYSEXIT
4924 stop, and we won't know how to distinguish the PR_SYSEXIT's
4925 for these failed execs with the ones for successful execs
4926 (whether the exec has succeeded is stored at that time in the
4927 carry bit or some such architecture-specific and
4928 non-ABI-specified place).
4930 So I can't think of anything better than to search the PATH
4931 now. This has several disadvantages: (1) There is a race
4932 condition; if we find a file now and it is deleted before we
4933 exec it, we lose, even if the deletion leaves a valid file
4934 further down in the PATH, (2) there is no way to know exactly
4935 what an executable (in the sense of "capable of being
4936 exec'd") file is. Using access() loses because it may lose
4937 if the caller is the superuser; failing to use it loses if
4938 there are ACLs or some such. */
4942 /* FIXME-maybe: might want "set path" command so user can change what
4943 path is used from within GDB. */
4944 char *path
= getenv ("PATH");
4946 struct stat statbuf
;
4949 path
= "/bin:/usr/bin";
4951 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4952 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4954 p1
= strchr (p
, ':');
4959 strncpy (tryname
, p
, len
);
4960 tryname
[len
] = '\0';
4961 strcat (tryname
, "/");
4962 strcat (tryname
, shell_file
);
4963 if (access (tryname
, X_OK
) < 0)
4965 if (stat (tryname
, &statbuf
) < 0)
4967 if (!S_ISREG (statbuf
.st_mode
))
4968 /* We certainly need to reject directories. I'm not quite
4969 as sure about FIFOs, sockets, etc., but I kind of doubt
4970 that people want to exec() these things. */
4975 /* Not found. This must be an error rather than merely passing
4976 the file to execlp(), because execlp() would try all the
4977 exec()s, causing GDB to get confused. */
4978 error ("procfs:%d -- Can't find shell %s in PATH",
4979 __LINE__
, shell_file
);
4981 shell_file
= tryname
;
4984 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4985 procfs_init_inferior
, NULL
, shell_file
);
4987 /* We are at the first instruction we care about. */
4988 /* Pedal to the metal... */
4990 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
4994 * Function: notice_thread
4996 * Callback for find_new_threads.
4997 * Calls "add_thread".
5001 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
5003 ptid_t gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
5005 if (!in_thread_list (gdb_threadid
))
5006 add_thread (gdb_threadid
);
5012 * Function: target_find_new_threads
5014 * Query all the threads that the target knows about,
5015 * and give them back to GDB to add to its list.
5019 procfs_find_new_threads (void)
5023 /* Find procinfo for main process */
5024 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5025 proc_update_threads (pi
);
5026 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
5030 * Function: target_thread_alive
5032 * Return true if the thread is still 'alive'.
5034 * This guy doesn't really seem to be doing his job.
5035 * Got to investigate how to tell when a thread is really gone.
5039 procfs_thread_alive (ptid_t ptid
)
5044 proc
= PIDGET (ptid
);
5045 thread
= TIDGET (ptid
);
5046 /* If I don't know it, it ain't alive! */
5047 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
5050 /* If I can't get its status, it ain't alive!
5051 What's more, I need to forget about it! */
5052 if (!proc_get_status (pi
))
5054 destroy_procinfo (pi
);
5057 /* I couldn't have got its status if it weren't alive, so it's alive. */
5062 * Function: target_pid_to_str
5064 * Return a string to be used to identify the thread in
5065 * the "info threads" display.
5069 procfs_pid_to_str (ptid_t ptid
)
5071 static char buf
[80];
5075 proc
= PIDGET (ptid
);
5076 thread
= TIDGET (ptid
);
5077 pi
= find_procinfo (proc
, thread
);
5080 sprintf (buf
, "Process %d", proc
);
5082 sprintf (buf
, "LWP %d", thread
);
5087 * Function: procfs_set_watchpoint
5088 * Insert a watchpoint
5092 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
5100 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5101 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5103 /* Translate from GDB's flags to /proc's */
5104 if (len
> 0) /* len == 0 means delete watchpoint */
5106 switch (rwflag
) { /* FIXME: need an enum! */
5107 case hw_write
: /* default watchpoint (write) */
5108 pflags
= WRITE_WATCHFLAG
;
5110 case hw_read
: /* read watchpoint */
5111 pflags
= READ_WATCHFLAG
;
5113 case hw_access
: /* access watchpoint */
5114 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
5116 case hw_execute
: /* execution HW breakpoint */
5117 pflags
= EXEC_WATCHFLAG
;
5119 default: /* Something weird. Return error. */
5122 if (after
) /* Stop after r/w access is completed. */
5123 pflags
|= AFTER_WATCHFLAG
;
5126 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
5128 if (errno
== E2BIG
) /* Typical error for no resources */
5129 return -1; /* fail */
5130 /* GDB may try to remove the same watchpoint twice.
5131 If a remove request returns no match, don't error. */
5132 if (errno
== ESRCH
&& len
== 0)
5133 return 0; /* ignore */
5134 proc_error (pi
, "set_watchpoint", __LINE__
);
5137 #endif /* UNIXWARE */
5142 * Function: stopped_by_watchpoint
5144 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5145 * else returns zero.
5149 procfs_stopped_by_watchpoint (ptid_t ptid
)
5153 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5154 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5156 if (!pi
) /* If no process, then not stopped by watchpoint! */
5159 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
5161 if (proc_why (pi
) == PR_FAULTED
)
5164 if (proc_what (pi
) == FLTWATCH
)
5168 if (proc_what (pi
) == FLTKWATCH
)
5176 #ifdef TM_I386SOL2_H
5178 * Function: procfs_find_LDT_entry
5181 * ptid_t ptid; // The GDB-style pid-plus-LWP.
5184 * pointer to the corresponding LDT entry.
5188 procfs_find_LDT_entry (ptid_t ptid
)
5190 gdb_gregset_t
*gregs
;
5194 /* Find procinfo for the lwp. */
5195 if ((pi
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
))) == NULL
)
5197 warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.",
5198 PIDGET (ptid
), TIDGET (ptid
));
5201 /* get its general registers. */
5202 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
5204 warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.",
5205 PIDGET (ptid
), TIDGET (ptid
));
5208 /* Now extract the GS register's lower 16 bits. */
5209 key
= (*gregs
)[GS
] & 0xffff;
5211 /* Find the matching entry and return it. */
5212 return proc_get_LDT_entry (pi
, key
);
5214 #endif /* TM_I386SOL2_H */
5217 * Memory Mappings Functions:
5221 * Function: iterate_over_mappings
5223 * Call a callback function once for each mapping, passing it the mapping,
5224 * an optional secondary callback function, and some optional opaque data.
5225 * Quit and return the first non-zero value returned from the callback.
5228 * pi -- procinfo struct for the process to be mapped.
5229 * func -- callback function to be called by this iterator.
5230 * data -- optional opaque data to be passed to the callback function.
5231 * child_func -- optional secondary function pointer to be passed
5232 * to the child function.
5234 * Return: First non-zero return value from the callback function,
5239 iterate_over_mappings (procinfo
*pi
, int (*child_func
) (), void *data
,
5240 int (*func
) (struct prmap
*map
,
5241 int (*child_func
) (),
5244 char pathname
[MAX_PROC_NAME_SIZE
];
5245 struct prmap
*prmaps
;
5246 struct prmap
*prmap
;
5254 /* Get the number of mappings, allocate space,
5255 and read the mappings into prmaps. */
5258 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
5259 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
5260 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
5262 /* Make sure it gets closed again. */
5263 make_cleanup_close (map_fd
);
5265 /* Use stat to determine the file size, and compute
5266 the number of prmap_t objects it contains. */
5267 if (fstat (map_fd
, &sbuf
) != 0)
5268 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
5270 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
5271 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5272 if (read (map_fd
, (char *) prmaps
, nmap
* sizeof (*prmaps
))
5273 != (nmap
* sizeof (*prmaps
)))
5274 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
5276 /* Use ioctl command PIOCNMAP to get number of mappings. */
5277 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmap
) != 0)
5278 proc_error (pi
, "iterate_over_mappings (PIOCNMAP)", __LINE__
);
5280 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5281 if (ioctl (pi
->ctl_fd
, PIOCMAP
, prmaps
) != 0)
5282 proc_error (pi
, "iterate_over_mappings (PIOCMAP)", __LINE__
);
5285 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
5286 if ((funcstat
= (*func
) (prmap
, child_func
, data
)) != 0)
5293 * Function: solib_mappings_callback
5295 * Calls the supplied callback function once for each mapped address
5296 * space in the process. The callback function receives an open
5297 * file descriptor for the file corresponding to that mapped
5298 * address space (if there is one), and the base address of the
5299 * mapped space. Quit when the callback function returns a
5300 * nonzero value, or at teh end of the mappings.
5302 * Returns: the first non-zero return value of the callback function,
5306 int solib_mappings_callback (struct prmap
*map
,
5307 int (*func
) (int, CORE_ADDR
),
5310 procinfo
*pi
= data
;
5314 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
5316 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
5317 return -1; /* sanity */
5319 if (map
->pr_mapname
[0] == 0)
5321 fd
= -1; /* no map file */
5325 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
5326 /* Note: caller's responsibility to close this fd! */
5327 fd
= open_with_retry (name
, O_RDONLY
);
5328 /* Note: we don't test the above call for failure;
5329 we just pass the FD on as given. Sometimes there is
5330 no file, so the open may return failure, but that's
5334 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
->pr_vaddr
);
5335 /* Note: we don't test the above call for failure;
5336 we just pass the FD on as given. Sometimes there is
5337 no file, so the ioctl may return failure, but that's
5340 return (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
);
5344 * Function: proc_iterate_over_mappings
5346 * Uses the unified "iterate_over_mappings" function
5347 * to implement the exported interface to solib-svr4.c.
5349 * Given a pointer to a function, call that function once for every
5350 * mapped address space in the process. The callback function
5351 * receives an open file descriptor for the file corresponding to
5352 * that mapped address space (if there is one), and the base address
5353 * of the mapped space. Quit when the callback function returns a
5354 * nonzero value, or at teh end of the mappings.
5356 * Returns: the first non-zero return value of the callback function,
5361 proc_iterate_over_mappings (int (*func
) (int, CORE_ADDR
))
5363 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5365 return iterate_over_mappings (pi
, func
, pi
, solib_mappings_callback
);
5369 * Function: find_memory_regions_callback
5371 * Implements the to_find_memory_regions method.
5372 * Calls an external function for each memory region.
5373 * External function will have the signiture:
5375 * int callback (CORE_ADDR vaddr,
5376 * unsigned long size,
5377 * int read, int write, int execute,
5380 * Returns the integer value returned by the callback.
5384 find_memory_regions_callback (struct prmap
*map
,
5385 int (*func
) (CORE_ADDR
,
5391 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
5393 (map
->pr_mflags
& MA_READ
) != 0,
5394 (map
->pr_mflags
& MA_WRITE
) != 0,
5395 (map
->pr_mflags
& MA_EXEC
) != 0,
5400 * Function: proc_find_memory_regions
5402 * External interface. Calls a callback function once for each
5403 * mapped memory region in the child process, passing as arguments
5404 * CORE_ADDR virtual_address,
5405 * unsigned long size,
5406 * int read, TRUE if region is readable by the child
5407 * int write, TRUE if region is writable by the child
5408 * int execute TRUE if region is executable by the child.
5410 * Stops iterating and returns the first non-zero value
5411 * returned by the callback.
5415 proc_find_memory_regions (int (*func
) (CORE_ADDR
,
5421 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5423 return iterate_over_mappings (pi
, func
, data
,
5424 find_memory_regions_callback
);
5428 * Function: mappingflags
5430 * Returns an ascii representation of a memory mapping's flags.
5434 mappingflags (flags
)
5437 static char asciiflags
[8];
5439 strcpy (asciiflags
, "-------");
5440 #if defined (MA_PHYS)
5441 if (flags
& MA_PHYS
)
5442 asciiflags
[0] = 'd';
5444 if (flags
& MA_STACK
)
5445 asciiflags
[1] = 's';
5446 if (flags
& MA_BREAK
)
5447 asciiflags
[2] = 'b';
5448 if (flags
& MA_SHARED
)
5449 asciiflags
[3] = 's';
5450 if (flags
& MA_READ
)
5451 asciiflags
[4] = 'r';
5452 if (flags
& MA_WRITE
)
5453 asciiflags
[5] = 'w';
5454 if (flags
& MA_EXEC
)
5455 asciiflags
[6] = 'x';
5456 return (asciiflags
);
5460 * Function: info_mappings_callback
5462 * Callback function, does the actual work for 'info proc mappings'.
5467 info_mappings_callback (struct prmap
*map
, int (*ignore
) (), void *unused
)
5469 char *data_fmt_string
;
5471 if (TARGET_ADDR_BIT
== 32)
5472 data_fmt_string
= "\t%#10lx %#10lx %#10x %#10x %7s\n";
5474 data_fmt_string
= " %#18lx %#18lx %#10x %#10x %7s\n";
5476 printf_filtered (data_fmt_string
,
5477 (unsigned long) map
->pr_vaddr
,
5478 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5480 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5481 (unsigned int) map
->pr_offset
,
5485 mappingflags (map
->pr_mflags
));
5491 * Function: info_proc_mappings
5493 * Implement the "info proc mappings" subcommand.
5497 info_proc_mappings (procinfo
*pi
, int summary
)
5499 char *header_fmt_string
;
5501 if (TARGET_PTR_BIT
== 32)
5502 header_fmt_string
= "\t%10s %10s %10s %10s %7s\n";
5504 header_fmt_string
= " %18s %18s %10s %10s %7s\n";
5507 return; /* No output for summary mode. */
5509 printf_filtered ("Mapped address spaces:\n\n");
5510 printf_filtered (header_fmt_string
,
5517 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
5518 printf_filtered ("\n");
5522 * Function: info_proc_cmd
5524 * Implement the "info proc" command.
5528 info_proc_cmd (char *args
, int from_tty
)
5530 struct cleanup
*old_chain
;
5531 procinfo
*process
= NULL
;
5532 procinfo
*thread
= NULL
;
5539 old_chain
= make_cleanup (null_cleanup
, 0);
5542 if ((argv
= buildargv (args
)) == NULL
)
5545 make_cleanup_freeargv (argv
);
5547 while (argv
!= NULL
&& *argv
!= NULL
)
5549 if (isdigit (argv
[0][0]))
5551 pid
= strtoul (argv
[0], &tmp
, 10);
5553 tid
= strtoul (++tmp
, NULL
, 10);
5555 else if (argv
[0][0] == '/')
5557 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5559 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
5570 pid
= PIDGET (inferior_ptid
);
5572 error ("No current process: you must name one.");
5575 /* Have pid, will travel.
5576 First see if it's a process we're already debugging. */
5577 process
= find_procinfo (pid
, 0);
5578 if (process
== NULL
)
5580 /* No. So open a procinfo for it, but
5581 remember to close it again when finished. */
5582 process
= create_procinfo (pid
, 0);
5583 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5584 if (!open_procinfo_files (process
, FD_CTL
))
5585 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5589 thread
= create_procinfo (pid
, tid
);
5593 printf_filtered ("process %d flags:\n", process
->pid
);
5594 proc_prettyprint_flags (proc_flags (process
), 1);
5595 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5596 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5597 if (proc_get_nthreads (process
) > 1)
5598 printf_filtered ("Process has %d threads.\n",
5599 proc_get_nthreads (process
));
5603 printf_filtered ("thread %d flags:\n", thread
->tid
);
5604 proc_prettyprint_flags (proc_flags (thread
), 1);
5605 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5606 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5611 info_proc_mappings (process
, 0);
5614 do_cleanups (old_chain
);
5618 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5624 if (PIDGET (inferior_ptid
) <= 0)
5625 error ("you must be debugging a process to use this command.");
5627 if (args
== NULL
|| args
[0] == 0)
5628 error_no_arg ("system call to trace");
5630 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5631 if (isdigit (args
[0]))
5633 syscallnum
= atoi (args
);
5634 if (entry_or_exit
== PR_SYSENTRY
)
5635 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5637 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5640 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5642 if (mode
== FLAG_SET
)
5643 gdb_praddsysset (sysset
, syscallnum
);
5645 gdb_prdelsysset (sysset
, syscallnum
);
5647 if (entry_or_exit
== PR_SYSENTRY
)
5649 if (!proc_set_traced_sysentry (pi
, sysset
))
5650 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5654 if (!proc_set_traced_sysexit (pi
, sysset
))
5655 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5661 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5663 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5667 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5669 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5673 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5675 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5679 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5681 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5686 _initialize_procfs (void)
5689 add_target (&procfs_ops
);
5690 add_info ("proc", info_proc_cmd
,
5691 "Show /proc process information about any running process.\n\
5692 Specify process id, or use the program being debugged by default.\n\
5693 Specify keyword 'mappings' for detailed info on memory mappings.");
5694 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5695 "Give a trace of entries into the syscall.");
5696 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5697 "Give a trace of exits from the syscall.");
5698 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5699 "Cancel a trace of entries into the syscall.");
5700 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5701 "Cancel a trace of exits from the syscall.");
5704 /* =================== END, GDB "MODULE" =================== */
5708 /* miscellaneous stubs: */
5709 /* The following satisfy a few random symbols mostly created by */
5710 /* the solaris threads implementation, which I will chase down */
5714 * Return a pid for which we guarantee
5715 * we will be able to find a 'live' procinfo.
5719 procfs_first_available (void)
5721 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);
5724 /* =================== GCORE .NOTE "MODULE" =================== */
5725 #if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT)
5726 /* gcore only implemented on solaris and unixware (so far) */
5729 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
5730 char *note_data
, int *note_size
)
5732 gdb_gregset_t gregs
;
5733 gdb_fpregset_t fpregs
;
5734 unsigned long merged_pid
;
5736 merged_pid
= TIDGET (ptid
) << 16 | PIDGET (ptid
);
5738 fill_gregset (&gregs
, -1);
5739 #if defined (UNIXWARE)
5740 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
5747 note_data
= (char *) elfcore_write_prstatus (obfd
,
5754 fill_fpregset (&fpregs
, -1);
5755 note_data
= (char *) elfcore_write_prfpreg (obfd
,
5763 struct procfs_corefile_thread_data
{
5770 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
5772 struct procfs_corefile_thread_data
*args
= data
;
5774 if (pi
!= NULL
&& thread
->tid
!= 0)
5776 ptid_t saved_ptid
= inferior_ptid
;
5777 inferior_ptid
= MERGEPID (pi
->pid
, thread
->tid
);
5778 args
->note_data
= procfs_do_thread_registers (args
->obfd
, inferior_ptid
,
5781 inferior_ptid
= saved_ptid
;
5787 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5789 struct cleanup
*old_chain
;
5790 gdb_gregset_t gregs
;
5791 gdb_fpregset_t fpregs
;
5792 char fname
[16] = {'\0'};
5793 char psargs
[80] = {'\0'};
5794 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5795 char *note_data
= NULL
;
5797 struct procfs_corefile_thread_data thread_args
;
5799 if (get_exec_file (0))
5801 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
5802 strncpy (psargs
, get_exec_file (0),
5805 inf_args
= get_inferior_args ();
5806 if (inf_args
&& *inf_args
&&
5807 strlen (inf_args
) < ((int) sizeof (psargs
) - (int) strlen (psargs
)))
5809 strncat (psargs
, " ",
5810 sizeof (psargs
) - strlen (psargs
));
5811 strncat (psargs
, inf_args
,
5812 sizeof (psargs
) - strlen (psargs
));
5816 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
5823 fill_gregset (&gregs
, -1);
5824 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
5825 PIDGET (inferior_ptid
),
5826 stop_signal
, &gregs
);
5829 thread_args
.obfd
= obfd
;
5830 thread_args
.note_data
= note_data
;
5831 thread_args
.note_size
= note_size
;
5832 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
, &thread_args
);
5834 if (thread_args
.note_data
== note_data
)
5836 /* iterate_over_threads didn't come up with any threads;
5837 just use inferior_ptid. */
5838 note_data
= procfs_do_thread_registers (obfd
, inferior_ptid
,
5839 note_data
, note_size
);
5843 note_data
= thread_args
.note_data
;
5846 make_cleanup (xfree
, note_data
);
5849 #else /* !(Solaris or Unixware) */
5851 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5853 error ("gcore not implemented for this host.");
5854 return NULL
; /* lint */
5856 #endif /* Solaris or Unixware */
5857 /* =================== END GCORE .NOTE "MODULE" =================== */