1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
3 Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation,
6 Written by Michael Snyder at Cygnus Solutions.
7 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software Foundation,
23 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #include "elf-bfd.h" /* for elfcore_write_* */
31 #include "gdbthread.h"
33 #if defined (NEW_PROC_API)
34 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
37 #include <sys/procfs.h>
38 #ifdef HAVE_SYS_FAULT_H
39 #include <sys/fault.h>
41 #ifdef HAVE_SYS_SYSCALL_H
42 #include <sys/syscall.h>
44 #include <sys/errno.h>
48 #include "gdb_assert.h"
54 * This module provides the interface between GDB and the
55 * /proc file system, which is used on many versions of Unix
56 * as a means for debuggers to control other processes.
57 * Examples of the systems that use this interface are:
64 * /proc works by imitating a file system: you open a simulated file
65 * that represents the process you wish to interact with, and
66 * perform operations on that "file" in order to examine or change
67 * the state of the other process.
69 * The most important thing to know about /proc and this module
70 * is that there are two very different interfaces to /proc:
71 * One that uses the ioctl system call, and
72 * another that uses read and write system calls.
73 * This module has to support both /proc interfaces. This means
74 * that there are two different ways of doing every basic operation.
76 * In order to keep most of the code simple and clean, I have
77 * defined an interface "layer" which hides all these system calls.
78 * An ifdef (NEW_PROC_API) determines which interface we are using,
79 * and most or all occurrances of this ifdef should be confined to
80 * this interface layer.
84 /* Determine which /proc API we are using:
85 The ioctl API defines PIOCSTATUS, while
86 the read/write (multiple fd) API never does. */
89 #include <sys/types.h>
90 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
93 #include <fcntl.h> /* for O_RDONLY */
94 #include <unistd.h> /* for "X_OK" */
95 #include "gdb_stat.h" /* for struct stat */
97 /* Note: procfs-utils.h must be included after the above system header
98 files, because it redefines various system calls using macros.
99 This may be incompatible with the prototype declarations. */
101 #include "proc-utils.h"
103 /* Prototypes for supply_gregset etc. */
106 /* =================== TARGET_OPS "MODULE" =================== */
109 * This module defines the GDB target vector and its methods.
112 static void procfs_open (char *, int);
113 static void procfs_attach (char *, int);
114 static void procfs_detach (char *, int);
115 static void procfs_resume (ptid_t
, int, enum target_signal
);
116 static int procfs_can_run (void);
117 static void procfs_stop (void);
118 static void procfs_files_info (struct target_ops
*);
119 static void procfs_fetch_registers (int);
120 static void procfs_store_registers (int);
121 static void procfs_notice_signals (ptid_t
);
122 static void procfs_prepare_to_store (void);
123 static void procfs_kill_inferior (void);
124 static void procfs_mourn_inferior (void);
125 static void procfs_create_inferior (char *, char *, char **);
126 static ptid_t
procfs_wait (ptid_t
, struct target_waitstatus
*);
127 static int procfs_xfer_memory (CORE_ADDR
, char *, int, int,
128 struct mem_attrib
*attrib
,
129 struct target_ops
*);
131 static int procfs_thread_alive (ptid_t
);
133 void procfs_find_new_threads (void);
134 char *procfs_pid_to_str (ptid_t
);
136 static int proc_find_memory_regions (int (*) (CORE_ADDR
,
142 static char * procfs_make_note_section (bfd
*, int *);
144 static int procfs_can_use_hw_breakpoint (int, int, int);
146 struct target_ops procfs_ops
; /* the target vector */
149 init_procfs_ops (void)
151 procfs_ops
.to_shortname
= "procfs";
152 procfs_ops
.to_longname
= "Unix /proc child process";
154 "Unix /proc child process (started by the \"run\" command).";
155 procfs_ops
.to_open
= procfs_open
;
156 procfs_ops
.to_can_run
= procfs_can_run
;
157 procfs_ops
.to_create_inferior
= procfs_create_inferior
;
158 procfs_ops
.to_kill
= procfs_kill_inferior
;
159 procfs_ops
.to_mourn_inferior
= procfs_mourn_inferior
;
160 procfs_ops
.to_attach
= procfs_attach
;
161 procfs_ops
.to_detach
= procfs_detach
;
162 procfs_ops
.to_wait
= procfs_wait
;
163 procfs_ops
.to_resume
= procfs_resume
;
164 procfs_ops
.to_prepare_to_store
= procfs_prepare_to_store
;
165 procfs_ops
.to_fetch_registers
= procfs_fetch_registers
;
166 procfs_ops
.to_store_registers
= procfs_store_registers
;
167 procfs_ops
.to_xfer_memory
= procfs_xfer_memory
;
168 procfs_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
169 procfs_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
170 procfs_ops
.to_notice_signals
= procfs_notice_signals
;
171 procfs_ops
.to_files_info
= procfs_files_info
;
172 procfs_ops
.to_stop
= procfs_stop
;
174 procfs_ops
.to_terminal_init
= terminal_init_inferior
;
175 procfs_ops
.to_terminal_inferior
= terminal_inferior
;
176 procfs_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
177 procfs_ops
.to_terminal_ours
= terminal_ours
;
178 procfs_ops
.to_terminal_save_ours
= terminal_save_ours
;
179 procfs_ops
.to_terminal_info
= child_terminal_info
;
181 procfs_ops
.to_find_new_threads
= procfs_find_new_threads
;
182 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
183 procfs_ops
.to_pid_to_str
= procfs_pid_to_str
;
185 procfs_ops
.to_has_all_memory
= 1;
186 procfs_ops
.to_has_memory
= 1;
187 procfs_ops
.to_has_execution
= 1;
188 procfs_ops
.to_has_stack
= 1;
189 procfs_ops
.to_has_registers
= 1;
190 procfs_ops
.to_stratum
= process_stratum
;
191 procfs_ops
.to_has_thread_control
= tc_schedlock
;
192 procfs_ops
.to_find_memory_regions
= proc_find_memory_regions
;
193 procfs_ops
.to_make_corefile_notes
= procfs_make_note_section
;
194 procfs_ops
.to_can_use_hw_breakpoint
= procfs_can_use_hw_breakpoint
;
195 procfs_ops
.to_magic
= OPS_MAGIC
;
198 /* =================== END, TARGET_OPS "MODULE" =================== */
203 * Put any typedefs, defines etc. here that are required for
204 * the unification of code that handles different versions of /proc.
207 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
209 enum { READ_WATCHFLAG
= WA_READ
,
210 WRITE_WATCHFLAG
= WA_WRITE
,
211 EXEC_WATCHFLAG
= WA_EXEC
,
212 AFTER_WATCHFLAG
= WA_TRAPAFTER
215 #else /* Irix method for watchpoints */
216 enum { READ_WATCHFLAG
= MA_READ
,
217 WRITE_WATCHFLAG
= MA_WRITE
,
218 EXEC_WATCHFLAG
= MA_EXEC
,
219 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
224 #ifdef HAVE_PR_SIGSET_T
225 typedef pr_sigset_t gdb_sigset_t
;
227 typedef sigset_t gdb_sigset_t
;
231 #ifdef HAVE_PR_SIGACTION64_T
232 typedef pr_sigaction64_t gdb_sigaction_t
;
234 typedef struct sigaction gdb_sigaction_t
;
238 #ifdef HAVE_PR_SIGINFO64_T
239 typedef pr_siginfo64_t gdb_siginfo_t
;
241 typedef struct siginfo gdb_siginfo_t
;
244 /* gdb_premptysysset */
246 #define gdb_premptysysset premptysysset
248 #define gdb_premptysysset premptyset
253 #define gdb_praddsysset praddsysset
255 #define gdb_praddsysset praddset
260 #define gdb_prdelsysset prdelsysset
262 #define gdb_prdelsysset prdelset
265 /* prissyssetmember */
266 #ifdef prissyssetmember
267 #define gdb_pr_issyssetmember prissyssetmember
269 #define gdb_pr_issyssetmember prismember
272 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
273 as intuitively descriptive as it could be, so we'll define
274 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
275 this writing, this feature is only found on AIX5 systems and
276 basically means that the set of syscalls is not fixed. I.e,
277 there's no nice table that one can #include to get all of the
278 syscall numbers. Instead, they're stored in /proc/PID/sysent
279 for each process. We are at least guaranteed that they won't
280 change over the lifetime of the process. But each process could
281 (in theory) have different syscall numbers.
283 #ifdef HAVE_PRSYSENT_T
284 #define DYNAMIC_SYSCALLS
289 /* =================== STRUCT PROCINFO "MODULE" =================== */
291 /* FIXME: this comment will soon be out of date W.R.T. threads. */
293 /* The procinfo struct is a wrapper to hold all the state information
294 concerning a /proc process. There should be exactly one procinfo
295 for each process, and since GDB currently can debug only one
296 process at a time, that means there should be only one procinfo.
297 All of the LWP's of a process can be accessed indirectly thru the
298 single process procinfo.
300 However, against the day when GDB may debug more than one process,
301 this data structure is kept in a list (which for now will hold no
302 more than one member), and many functions will have a pointer to a
303 procinfo as an argument.
305 There will be a separate procinfo structure for use by the (not yet
306 implemented) "info proc" command, so that we can print useful
307 information about any random process without interfering with the
308 inferior's procinfo information. */
311 /* format strings for /proc paths */
312 # ifndef CTL_PROC_NAME_FMT
313 # define MAIN_PROC_NAME_FMT "/proc/%d"
314 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
315 # define AS_PROC_NAME_FMT "/proc/%d/as"
316 # define MAP_PROC_NAME_FMT "/proc/%d/map"
317 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
318 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
320 /* the name of the proc status struct depends on the implementation */
321 typedef pstatus_t gdb_prstatus_t
;
322 typedef lwpstatus_t gdb_lwpstatus_t
;
323 #else /* ! NEW_PROC_API */
324 /* format strings for /proc paths */
325 # ifndef CTL_PROC_NAME_FMT
326 # define MAIN_PROC_NAME_FMT "/proc/%05d"
327 # define CTL_PROC_NAME_FMT "/proc/%05d"
328 # define AS_PROC_NAME_FMT "/proc/%05d"
329 # define MAP_PROC_NAME_FMT "/proc/%05d"
330 # define STATUS_PROC_NAME_FMT "/proc/%05d"
331 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
333 /* the name of the proc status struct depends on the implementation */
334 typedef prstatus_t gdb_prstatus_t
;
335 typedef prstatus_t gdb_lwpstatus_t
;
336 #endif /* NEW_PROC_API */
338 typedef struct procinfo
{
339 struct procinfo
*next
;
340 int pid
; /* Process ID */
341 int tid
; /* Thread/LWP id */
345 int ignore_next_sigstop
;
347 /* The following four fd fields may be identical, or may contain
348 several different fd's, depending on the version of /proc
349 (old ioctl or new read/write). */
351 int ctl_fd
; /* File descriptor for /proc control file */
353 * The next three file descriptors are actually only needed in the
354 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
355 * However, to avoid a bunch of #ifdefs in the code, we will use
356 * them uniformly by (in the case of the ioctl single-file-descriptor
357 * implementation) filling them with copies of the control fd.
359 int status_fd
; /* File descriptor for /proc status file */
360 int as_fd
; /* File descriptor for /proc as file */
362 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
364 fltset_t saved_fltset
; /* Saved traced hardware fault set */
365 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
366 gdb_sigset_t saved_sighold
; /* Saved held signal set */
367 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
368 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
370 gdb_prstatus_t prstatus
; /* Current process status info */
373 gdb_fpregset_t fpregset
; /* Current floating point registers */
376 #ifdef DYNAMIC_SYSCALLS
377 int num_syscalls
; /* Total number of syscalls */
378 char **syscall_names
; /* Syscall number to name map */
381 struct procinfo
*thread_list
;
383 int status_valid
: 1;
385 int fpregs_valid
: 1;
386 int threads_valid
: 1;
389 static char errmsg
[128]; /* shared error msg buffer */
391 /* Function prototypes for procinfo module: */
393 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
394 static procinfo
*find_procinfo (int pid
, int tid
);
395 static procinfo
*create_procinfo (int pid
, int tid
);
396 static void destroy_procinfo (procinfo
* p
);
397 static void do_destroy_procinfo_cleanup (void *);
398 static void dead_procinfo (procinfo
* p
, char *msg
, int killp
);
399 static int open_procinfo_files (procinfo
* p
, int which
);
400 static void close_procinfo_files (procinfo
* p
);
401 static int sysset_t_size (procinfo
*p
);
402 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
403 #ifdef DYNAMIC_SYSCALLS
404 static void load_syscalls (procinfo
*pi
);
405 static void free_syscalls (procinfo
*pi
);
406 static int find_syscall (procinfo
*pi
, char *name
);
407 #endif /* DYNAMIC_SYSCALLS */
409 /* The head of the procinfo list: */
410 static procinfo
* procinfo_list
;
413 * Function: find_procinfo
415 * Search the procinfo list.
417 * Returns: pointer to procinfo, or NULL if not found.
421 find_procinfo (int pid
, int tid
)
425 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
432 /* Don't check threads_valid. If we're updating the
433 thread_list, we want to find whatever threads are already
434 here. This means that in general it is the caller's
435 responsibility to check threads_valid and update before
436 calling find_procinfo, if the caller wants to find a new
439 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
448 * Function: find_procinfo_or_die
450 * Calls find_procinfo, but errors on failure.
454 find_procinfo_or_die (int pid
, int tid
)
456 procinfo
*pi
= find_procinfo (pid
, tid
);
461 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
464 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
469 /* open_with_retry() is a wrapper for open(). The appropriate
470 open() call is attempted; if unsuccessful, it will be retried as
471 many times as needed for the EAGAIN and EINTR conditions.
473 For other conditions, open_with_retry() will retry the open() a
474 limited number of times. In addition, a short sleep is imposed
475 prior to retrying the open(). The reason for this sleep is to give
476 the kernel a chance to catch up and create the file in question in
477 the event that GDB "wins" the race to open a file before the kernel
481 open_with_retry (const char *pathname
, int flags
)
483 int retries_remaining
, status
;
485 retries_remaining
= 2;
489 status
= open (pathname
, flags
);
491 if (status
>= 0 || retries_remaining
== 0)
493 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
504 * Function: open_procinfo_files
506 * Open the file descriptor for the process or LWP.
507 * ifdef NEW_PROC_API, we only open the control file descriptor;
508 * the others are opened lazily as needed.
509 * else (if not NEW_PROC_API), there is only one real
510 * file descriptor, but we keep multiple copies of it so that
511 * the code that uses them does not have to be #ifdef'd.
513 * Return: file descriptor, or zero for failure.
516 enum { FD_CTL
, FD_STATUS
, FD_AS
};
519 open_procinfo_files (procinfo
*pi
, int which
)
522 char tmp
[MAX_PROC_NAME_SIZE
];
527 * This function is getting ALMOST long enough to break up into several.
528 * Here is some rationale:
530 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
531 * There are several file descriptors that may need to be open
532 * for any given process or LWP. The ones we're intereted in are:
533 * - control (ctl) write-only change the state
534 * - status (status) read-only query the state
535 * - address space (as) read/write access memory
536 * - map (map) read-only virtual addr map
537 * Most of these are opened lazily as they are needed.
538 * The pathnames for the 'files' for an LWP look slightly
539 * different from those of a first-class process:
540 * Pathnames for a process (<proc-id>):
541 * /proc/<proc-id>/ctl
542 * /proc/<proc-id>/status
544 * /proc/<proc-id>/map
545 * Pathnames for an LWP (lwp-id):
546 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
547 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
548 * An LWP has no map or address space file descriptor, since
549 * the memory map and address space are shared by all LWPs.
551 * Everyone else (Solaris 2.5, Irix, OSF)
552 * There is only one file descriptor for each process or LWP.
553 * For convenience, we copy the same file descriptor into all
554 * three fields of the procinfo struct (ctl_fd, status_fd, and
555 * as_fd, see NEW_PROC_API above) so that code that uses them
556 * doesn't need any #ifdef's.
561 * Each LWP has an independent file descriptor, but these
562 * are not obtained via the 'open' system call like the rest:
563 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
564 * to the file descriptor of the parent process.
567 * These do not even have their own independent file descriptor.
568 * All operations are carried out on the file descriptor of the
569 * parent process. Therefore we just call open again for each
570 * thread, getting a new handle for the same 'file'.
575 * In this case, there are several different file descriptors that
576 * we might be asked to open. The control file descriptor will be
577 * opened early, but the others will be opened lazily as they are
581 strcpy (tmp
, pi
->pathname
);
582 switch (which
) { /* which file descriptor to open? */
585 strcat (tmp
, "/lwpctl");
587 strcat (tmp
, "/ctl");
588 fd
= open_with_retry (tmp
, O_WRONLY
);
595 return 0; /* there is no 'as' file descriptor for an lwp */
597 fd
= open_with_retry (tmp
, O_RDWR
);
604 strcat (tmp
, "/lwpstatus");
606 strcat (tmp
, "/status");
607 fd
= open_with_retry (tmp
, O_RDONLY
);
613 return 0; /* unknown file descriptor */
615 #else /* not NEW_PROC_API */
617 * In this case, there is only one file descriptor for each procinfo
618 * (ie. each process or LWP). In fact, only the file descriptor for
619 * the process can actually be opened by an 'open' system call.
620 * The ones for the LWPs have to be obtained thru an IOCTL call
621 * on the process's file descriptor.
623 * For convenience, we copy each procinfo's single file descriptor
624 * into all of the fields occupied by the several file descriptors
625 * of the NEW_PROC_API implementation. That way, the code that uses
626 * them can be written without ifdefs.
630 #ifdef PIOCTSTATUS /* OSF */
631 /* Only one FD; just open it. */
632 if ((fd
= open_with_retry (pi
->pathname
, O_RDWR
)) == 0)
634 #else /* Sol 2.5, Irix, other? */
635 if (pi
->tid
== 0) /* Master procinfo for the process */
637 fd
= open_with_retry (pi
->pathname
, O_RDWR
);
641 else /* LWP thread procinfo */
643 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
647 /* Find the procinfo for the entire process. */
648 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
651 /* Now obtain the file descriptor for the LWP. */
652 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
654 #else /* Irix, other? */
655 return 0; /* Don't know how to open threads */
656 #endif /* Sol 2.5 PIOCOPENLWP */
658 #endif /* OSF PIOCTSTATUS */
659 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
660 #endif /* NEW_PROC_API */
662 return 1; /* success */
666 * Function: create_procinfo
668 * Allocate a data structure and link it into the procinfo list.
669 * (First tries to find a pre-existing one (FIXME: why?)
671 * Return: pointer to new procinfo struct.
675 create_procinfo (int pid
, int tid
)
677 procinfo
*pi
, *parent
;
679 if ((pi
= find_procinfo (pid
, tid
)))
680 return pi
; /* Already exists, nothing to do. */
682 /* find parent before doing malloc, to save having to cleanup */
684 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
686 doesn't exist yet? */
688 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
689 memset (pi
, 0, sizeof (procinfo
));
693 #ifdef DYNAMIC_SYSCALLS
697 pi
->saved_entryset
= sysset_t_alloc (pi
);
698 pi
->saved_exitset
= sysset_t_alloc (pi
);
700 /* Chain into list. */
703 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
704 pi
->next
= procinfo_list
;
710 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
712 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
714 pi
->next
= parent
->thread_list
;
715 parent
->thread_list
= pi
;
721 * Function: close_procinfo_files
723 * Close all file descriptors associated with the procinfo
727 close_procinfo_files (procinfo
*pi
)
734 if (pi
->status_fd
> 0)
735 close (pi
->status_fd
);
737 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
741 * Function: destroy_procinfo
743 * Destructor function. Close, unlink and deallocate the object.
747 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
751 /* Step one: unlink the procinfo from its list */
755 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
758 ptr
->next
= pi
->next
;
762 /* Step two: close any open file descriptors */
763 close_procinfo_files (pi
);
765 /* Step three: free the memory. */
766 #ifdef DYNAMIC_SYSCALLS
769 xfree (pi
->saved_entryset
);
770 xfree (pi
->saved_exitset
);
775 destroy_procinfo (procinfo
*pi
)
779 if (pi
->tid
!= 0) /* destroy a thread procinfo */
781 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
782 destroy_one_procinfo (&tmp
->thread_list
, pi
);
784 else /* destroy a process procinfo and all its threads */
786 /* First destroy the children, if any; */
787 while (pi
->thread_list
!= NULL
)
788 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
789 /* Then destroy the parent. Genocide!!! */
790 destroy_one_procinfo (&procinfo_list
, pi
);
795 do_destroy_procinfo_cleanup (void *pi
)
797 destroy_procinfo (pi
);
800 enum { NOKILL
, KILL
};
803 * Function: dead_procinfo
805 * To be called on a non_recoverable error for a procinfo.
806 * Prints error messages, optionally sends a SIGKILL to the process,
807 * then destroys the data structure.
811 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
817 print_sys_errmsg (pi
->pathname
, errno
);
821 sprintf (procfile
, "process %d", pi
->pid
);
822 print_sys_errmsg (procfile
, errno
);
825 kill (pi
->pid
, SIGKILL
);
827 destroy_procinfo (pi
);
832 * Function: sysset_t_size
834 * Returns the (complete) size of a sysset_t struct. Normally, this
835 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
836 * size of sysset_t isn't known until runtime.
840 sysset_t_size (procinfo
* pi
)
842 #ifndef DYNAMIC_SYSCALLS
843 return sizeof (sysset_t
);
845 return sizeof (sysset_t
) - sizeof (uint64_t)
846 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
847 / (8 * sizeof (uint64_t)));
851 /* Function: sysset_t_alloc
853 Allocate and (partially) initialize a sysset_t struct. */
856 sysset_t_alloc (procinfo
* pi
)
859 int size
= sysset_t_size (pi
);
860 ret
= xmalloc (size
);
861 #ifdef DYNAMIC_SYSCALLS
862 ret
->pr_size
= (pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
863 / (8 * sizeof (uint64_t));
868 #ifdef DYNAMIC_SYSCALLS
870 /* Function: load_syscalls
872 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
873 pi->num_syscalls with the number of syscalls and pi->syscall_names
874 with the names. (Certain numbers may be skipped in which case the
875 names for these numbers will be left as NULL.) */
877 #define MAX_SYSCALL_NAME_LENGTH 256
878 #define MAX_SYSCALLS 65536
881 load_syscalls (procinfo
*pi
)
883 char pathname
[MAX_PROC_NAME_SIZE
];
886 prsyscall_t
*syscalls
;
887 int i
, size
, maxcall
;
889 pi
->num_syscalls
= 0;
890 pi
->syscall_names
= 0;
892 /* Open the file descriptor for the sysent file */
893 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
894 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
897 error ("load_syscalls: Can't open /proc/%d/sysent", pi
->pid
);
900 size
= sizeof header
- sizeof (prsyscall_t
);
901 if (read (sysent_fd
, &header
, size
) != size
)
903 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
906 if (header
.pr_nsyscalls
== 0)
908 error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi
->pid
);
911 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
912 syscalls
= xmalloc (size
);
914 if (read (sysent_fd
, syscalls
, size
) != size
)
917 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
920 /* Find maximum syscall number. This may not be the same as
921 pr_nsyscalls since that value refers to the number of entries
922 in the table. (Also, the docs indicate that some system
923 call numbers may be skipped.) */
925 maxcall
= syscalls
[0].pr_number
;
927 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
928 if (syscalls
[i
].pr_number
> maxcall
929 && syscalls
[i
].pr_nameoff
> 0
930 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
931 maxcall
= syscalls
[i
].pr_number
;
933 pi
->num_syscalls
= maxcall
+1;
934 pi
->syscall_names
= xmalloc (pi
->num_syscalls
* sizeof (char *));
936 for (i
= 0; i
< pi
->num_syscalls
; i
++)
937 pi
->syscall_names
[i
] = NULL
;
939 /* Read the syscall names in */
940 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
942 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
946 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
947 || syscalls
[i
].pr_number
< 0
948 || syscalls
[i
].pr_nameoff
<= 0
949 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
950 != (off_t
) syscalls
[i
].pr_nameoff
))
953 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
957 callnum
= syscalls
[i
].pr_number
;
959 if (pi
->syscall_names
[callnum
] != NULL
)
961 /* FIXME: Generate warning */
965 namebuf
[nread
-1] = '\0';
966 size
= strlen (namebuf
) + 1;
967 pi
->syscall_names
[callnum
] = xmalloc (size
);
968 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
969 pi
->syscall_names
[callnum
][size
-1] = '\0';
976 /* Function: free_syscalls
978 Free the space allocated for the syscall names from the procinfo
982 free_syscalls (procinfo
*pi
)
984 if (pi
->syscall_names
)
988 for (i
= 0; i
< pi
->num_syscalls
; i
++)
989 if (pi
->syscall_names
[i
] != NULL
)
990 xfree (pi
->syscall_names
[i
]);
992 xfree (pi
->syscall_names
);
993 pi
->syscall_names
= 0;
997 /* Function: find_syscall
999 Given a name, look up (and return) the corresponding syscall number.
1000 If no match is found, return -1. */
1003 find_syscall (procinfo
*pi
, char *name
)
1006 for (i
= 0; i
< pi
->num_syscalls
; i
++)
1008 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
1015 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1017 /* =================== /proc "MODULE" =================== */
1020 * This "module" is the interface layer between the /proc system API
1021 * and the gdb target vector functions. This layer consists of
1022 * access functions that encapsulate each of the basic operations
1023 * that we need to use from the /proc API.
1025 * The main motivation for this layer is to hide the fact that
1026 * there are two very different implementations of the /proc API.
1027 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1028 * functions, we do our best to hide them all in here.
1031 int proc_get_status (procinfo
* pi
);
1032 long proc_flags (procinfo
* pi
);
1033 int proc_why (procinfo
* pi
);
1034 int proc_what (procinfo
* pi
);
1035 int proc_set_run_on_last_close (procinfo
* pi
);
1036 int proc_unset_run_on_last_close (procinfo
* pi
);
1037 int proc_set_inherit_on_fork (procinfo
* pi
);
1038 int proc_unset_inherit_on_fork (procinfo
* pi
);
1039 int proc_set_async (procinfo
* pi
);
1040 int proc_unset_async (procinfo
* pi
);
1041 int proc_stop_process (procinfo
* pi
);
1042 int proc_trace_signal (procinfo
* pi
, int signo
);
1043 int proc_ignore_signal (procinfo
* pi
, int signo
);
1044 int proc_clear_current_fault (procinfo
* pi
);
1045 int proc_set_current_signal (procinfo
* pi
, int signo
);
1046 int proc_clear_current_signal (procinfo
* pi
);
1047 int proc_set_gregs (procinfo
* pi
);
1048 int proc_set_fpregs (procinfo
* pi
);
1049 int proc_wait_for_stop (procinfo
* pi
);
1050 int proc_run_process (procinfo
* pi
, int step
, int signo
);
1051 int proc_kill (procinfo
* pi
, int signo
);
1052 int proc_parent_pid (procinfo
* pi
);
1053 int proc_get_nthreads (procinfo
* pi
);
1054 int proc_get_current_thread (procinfo
* pi
);
1055 int proc_set_held_signals (procinfo
* pi
, gdb_sigset_t
* sighold
);
1056 int proc_set_traced_sysexit (procinfo
* pi
, sysset_t
* sysset
);
1057 int proc_set_traced_sysentry (procinfo
* pi
, sysset_t
* sysset
);
1058 int proc_set_traced_faults (procinfo
* pi
, fltset_t
* fltset
);
1059 int proc_set_traced_signals (procinfo
* pi
, gdb_sigset_t
* sigset
);
1061 int proc_update_threads (procinfo
* pi
);
1062 int proc_iterate_over_threads (procinfo
* pi
,
1063 int (*func
) (procinfo
*, procinfo
*, void *),
1066 gdb_gregset_t
*proc_get_gregs (procinfo
* pi
);
1067 gdb_fpregset_t
*proc_get_fpregs (procinfo
* pi
);
1068 sysset_t
*proc_get_traced_sysexit (procinfo
* pi
, sysset_t
* save
);
1069 sysset_t
*proc_get_traced_sysentry (procinfo
* pi
, sysset_t
* save
);
1070 fltset_t
*proc_get_traced_faults (procinfo
* pi
, fltset_t
* save
);
1071 gdb_sigset_t
*proc_get_traced_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1072 gdb_sigset_t
*proc_get_held_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1073 gdb_sigset_t
*proc_get_pending_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1074 gdb_sigaction_t
*proc_get_signal_actions (procinfo
* pi
, gdb_sigaction_t
*save
);
1076 void proc_warn (procinfo
* pi
, char *func
, int line
);
1077 void proc_error (procinfo
* pi
, char *func
, int line
);
1080 proc_warn (procinfo
*pi
, char *func
, int line
)
1082 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1083 print_sys_errmsg (errmsg
, errno
);
1087 proc_error (procinfo
*pi
, char *func
, int line
)
1089 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1090 perror_with_name (errmsg
);
1094 * Function: proc_get_status
1096 * Updates the status struct in the procinfo.
1097 * There is a 'valid' flag, to let other functions know when
1098 * this function needs to be called (so the status is only
1099 * read when it is needed). The status file descriptor is
1100 * also only opened when it is needed.
1102 * Return: non-zero for success, zero for failure.
1106 proc_get_status (procinfo
*pi
)
1108 /* Status file descriptor is opened "lazily" */
1109 if (pi
->status_fd
== 0 &&
1110 open_procinfo_files (pi
, FD_STATUS
) == 0)
1112 pi
->status_valid
= 0;
1117 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1118 pi
->status_valid
= 0; /* fail */
1121 /* Sigh... I have to read a different data structure,
1122 depending on whether this is a main process or an LWP. */
1124 pi
->status_valid
= (read (pi
->status_fd
,
1125 (char *) &pi
->prstatus
.pr_lwp
,
1126 sizeof (lwpstatus_t
))
1127 == sizeof (lwpstatus_t
));
1130 pi
->status_valid
= (read (pi
->status_fd
,
1131 (char *) &pi
->prstatus
,
1132 sizeof (gdb_prstatus_t
))
1133 == sizeof (gdb_prstatus_t
));
1134 #if 0 /*def UNIXWARE*/
1135 if (pi
->status_valid
&&
1136 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
1137 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
1138 /* Unixware peculiarity -- read the damn thing again! */
1139 pi
->status_valid
= (read (pi
->status_fd
,
1140 (char *) &pi
->prstatus
,
1141 sizeof (gdb_prstatus_t
))
1142 == sizeof (gdb_prstatus_t
));
1143 #endif /* UNIXWARE */
1146 #else /* ioctl method */
1147 #ifdef PIOCTSTATUS /* osf */
1148 if (pi
->tid
== 0) /* main process */
1150 /* Just read the danged status. Now isn't that simple? */
1152 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1159 tid_t pr_error_thread
;
1160 struct prstatus status
;
1163 thread_status
.pr_count
= 1;
1164 thread_status
.status
.pr_tid
= pi
->tid
;
1165 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1168 memcpy (&pi
->prstatus
, &thread_status
.status
,
1169 sizeof (pi
->prstatus
));
1170 pi
->status_valid
= 1;
1174 /* Just read the danged status. Now isn't that simple? */
1175 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1179 if (pi
->status_valid
)
1181 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1184 proc_get_current_thread (pi
));
1187 /* The status struct includes general regs, so mark them valid too */
1188 pi
->gregs_valid
= pi
->status_valid
;
1190 /* In the read/write multiple-fd model,
1191 the status struct includes the fp regs too, so mark them valid too */
1192 pi
->fpregs_valid
= pi
->status_valid
;
1194 return pi
->status_valid
; /* True if success, false if failure. */
1198 * Function: proc_flags
1200 * returns the process flags (pr_flags field).
1204 proc_flags (procinfo
*pi
)
1206 if (!pi
->status_valid
)
1207 if (!proc_get_status (pi
))
1208 return 0; /* FIXME: not a good failure value (but what is?) */
1212 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1213 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1214 The two sets of flags don't overlap. */
1215 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
1217 return pi
->prstatus
.pr_lwp
.pr_flags
;
1220 return pi
->prstatus
.pr_flags
;
1225 * Function: proc_why
1227 * returns the pr_why field (why the process stopped).
1231 proc_why (procinfo
*pi
)
1233 if (!pi
->status_valid
)
1234 if (!proc_get_status (pi
))
1235 return 0; /* FIXME: not a good failure value (but what is?) */
1238 return pi
->prstatus
.pr_lwp
.pr_why
;
1240 return pi
->prstatus
.pr_why
;
1245 * Function: proc_what
1247 * returns the pr_what field (details of why the process stopped).
1251 proc_what (procinfo
*pi
)
1253 if (!pi
->status_valid
)
1254 if (!proc_get_status (pi
))
1255 return 0; /* FIXME: not a good failure value (but what is?) */
1258 return pi
->prstatus
.pr_lwp
.pr_what
;
1260 return pi
->prstatus
.pr_what
;
1264 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1266 * Function: proc_nsysarg
1268 * returns the pr_nsysarg field (number of args to the current syscall).
1272 proc_nsysarg (procinfo
*pi
)
1274 if (!pi
->status_valid
)
1275 if (!proc_get_status (pi
))
1279 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1281 return pi
->prstatus
.pr_nsysarg
;
1286 * Function: proc_sysargs
1288 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1292 proc_sysargs (procinfo
*pi
)
1294 if (!pi
->status_valid
)
1295 if (!proc_get_status (pi
))
1299 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1301 return (long *) &pi
->prstatus
.pr_sysarg
;
1306 * Function: proc_syscall
1308 * returns the pr_syscall field (id of current syscall if we are in one).
1312 proc_syscall (procinfo
*pi
)
1314 if (!pi
->status_valid
)
1315 if (!proc_get_status (pi
))
1319 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1321 return pi
->prstatus
.pr_syscall
;
1324 #endif /* PIOCSSPCACT */
1327 * Function: proc_cursig:
1329 * returns the pr_cursig field (current signal).
1333 proc_cursig (struct procinfo
*pi
)
1335 if (!pi
->status_valid
)
1336 if (!proc_get_status (pi
))
1337 return 0; /* FIXME: not a good failure value (but what is?) */
1340 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1342 return pi
->prstatus
.pr_cursig
;
1347 * Function: proc_modify_flag
1349 * === I appologize for the messiness of this function.
1350 * === This is an area where the different versions of
1351 * === /proc are more inconsistent than usual. MVS
1353 * Set or reset any of the following process flags:
1354 * PR_FORK -- forked child will inherit trace flags
1355 * PR_RLC -- traced process runs when last /proc file closed.
1356 * PR_KLC -- traced process is killed when last /proc file closed.
1357 * PR_ASYNC -- LWP's get to run/stop independently.
1359 * There are three methods for doing this function:
1360 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1362 * 2) Middle: PIOCSET/PIOCRESET
1364 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1367 * Note: Irix does not define PR_ASYNC.
1368 * Note: OSF does not define PR_KLC.
1369 * Note: OSF is the only one that can ONLY use the oldest method.
1372 * pi -- the procinfo
1373 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1374 * mode -- 1 for set, 0 for reset.
1376 * Returns non-zero for success, zero for failure.
1379 enum { FLAG_RESET
, FLAG_SET
};
1382 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1384 long win
= 0; /* default to fail */
1387 * These operations affect the process as a whole, and applying
1388 * them to an individual LWP has the same meaning as applying them
1389 * to the main process. Therefore, if we're ever called with a
1390 * pointer to an LWP's procinfo, let's substitute the process's
1391 * procinfo and avoid opening the LWP's file descriptor
1396 pi
= find_procinfo_or_die (pi
->pid
, 0);
1398 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1399 /* First normalize the PCUNSET/PCRESET command opcode
1400 (which for no obvious reason has a different definition
1401 from one operating system to the next...) */
1403 #define GDBRESET PCUNSET
1406 #define GDBRESET PCRESET
1410 procfs_ctl_t arg
[2];
1412 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1414 else /* Reset the flag */
1418 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1421 #ifdef PIOCSET /* Irix/Sol5 method */
1422 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1424 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1426 else /* Reset the flag */
1428 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1432 #ifdef PIOCSRLC /* Oldest method: OSF */
1435 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1437 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1439 else /* Clear run-on-last-close */
1441 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1445 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1447 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1449 else /* Clear inherit-on-fork */
1451 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1455 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1462 /* The above operation renders the procinfo's cached pstatus obsolete. */
1463 pi
->status_valid
= 0;
1466 warning ("procfs: modify_flag failed to turn %s %s",
1467 flag
== PR_FORK
? "PR_FORK" :
1468 flag
== PR_RLC
? "PR_RLC" :
1470 flag
== PR_ASYNC
? "PR_ASYNC" :
1473 flag
== PR_KLC
? "PR_KLC" :
1476 mode
== FLAG_RESET
? "off" : "on");
1482 * Function: proc_set_run_on_last_close
1484 * Set the run_on_last_close flag.
1485 * Process with all threads will become runnable
1486 * when debugger closes all /proc fds.
1488 * Returns non-zero for success, zero for failure.
1492 proc_set_run_on_last_close (procinfo
*pi
)
1494 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1498 * Function: proc_unset_run_on_last_close
1500 * Reset the run_on_last_close flag.
1501 * Process will NOT become runnable
1502 * when debugger closes its file handles.
1504 * Returns non-zero for success, zero for failure.
1508 proc_unset_run_on_last_close (procinfo
*pi
)
1510 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1515 * Function: proc_set_kill_on_last_close
1517 * Set the kill_on_last_close flag.
1518 * Process with all threads will be killed when debugger
1519 * closes all /proc fds (or debugger exits or dies).
1521 * Returns non-zero for success, zero for failure.
1525 proc_set_kill_on_last_close (procinfo
*pi
)
1527 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1531 * Function: proc_unset_kill_on_last_close
1533 * Reset the kill_on_last_close flag.
1534 * Process will NOT be killed when debugger
1535 * closes its file handles (or exits or dies).
1537 * Returns non-zero for success, zero for failure.
1541 proc_unset_kill_on_last_close (procinfo
*pi
)
1543 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1548 * Function: proc_set_inherit_on_fork
1550 * Set inherit_on_fork flag.
1551 * If the process forks a child while we are registered for events
1552 * in the parent, then we will also recieve events from the child.
1554 * Returns non-zero for success, zero for failure.
1558 proc_set_inherit_on_fork (procinfo
*pi
)
1560 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1564 * Function: proc_unset_inherit_on_fork
1566 * Reset inherit_on_fork flag.
1567 * If the process forks a child while we are registered for events
1568 * in the parent, then we will NOT recieve events from the child.
1570 * Returns non-zero for success, zero for failure.
1574 proc_unset_inherit_on_fork (procinfo
*pi
)
1576 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1581 * Function: proc_set_async
1583 * Set PR_ASYNC flag.
1584 * If one LWP stops because of a debug event (signal etc.),
1585 * the remaining LWPs will continue to run.
1587 * Returns non-zero for success, zero for failure.
1591 proc_set_async (procinfo
*pi
)
1593 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1597 * Function: proc_unset_async
1599 * Reset PR_ASYNC flag.
1600 * If one LWP stops because of a debug event (signal etc.),
1601 * then all other LWPs will stop as well.
1603 * Returns non-zero for success, zero for failure.
1607 proc_unset_async (procinfo
*pi
)
1609 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1611 #endif /* PR_ASYNC */
1614 * Function: proc_stop_process
1616 * Request the process/LWP to stop. Does not wait.
1617 * Returns non-zero for success, zero for failure.
1621 proc_stop_process (procinfo
*pi
)
1626 * We might conceivably apply this operation to an LWP, and
1627 * the LWP's ctl file descriptor might not be open.
1630 if (pi
->ctl_fd
== 0 &&
1631 open_procinfo_files (pi
, FD_CTL
) == 0)
1636 procfs_ctl_t cmd
= PCSTOP
;
1637 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1638 #else /* ioctl method */
1639 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1640 /* Note: the call also reads the prstatus. */
1643 pi
->status_valid
= 1;
1644 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1647 proc_get_current_thread (pi
));
1656 * Function: proc_wait_for_stop
1658 * Wait for the process or LWP to stop (block until it does).
1659 * Returns non-zero for success, zero for failure.
1663 proc_wait_for_stop (procinfo
*pi
)
1668 * We should never have to apply this operation to any procinfo
1669 * except the one for the main process. If that ever changes
1670 * for any reason, then take out the following clause and
1671 * replace it with one that makes sure the ctl_fd is open.
1675 pi
= find_procinfo_or_die (pi
->pid
, 0);
1679 procfs_ctl_t cmd
= PCWSTOP
;
1680 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1681 /* We been runnin' and we stopped -- need to update status. */
1682 pi
->status_valid
= 0;
1684 #else /* ioctl method */
1685 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1686 /* Above call also refreshes the prstatus. */
1689 pi
->status_valid
= 1;
1690 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1693 proc_get_current_thread (pi
));
1701 * Function: proc_run_process
1703 * Make the process or LWP runnable.
1704 * Options (not all are implemented):
1706 * - clear current fault
1707 * - clear current signal
1708 * - abort the current system call
1709 * - stop as soon as finished with system call
1710 * - (ioctl): set traced signal set
1711 * - (ioctl): set held signal set
1712 * - (ioctl): set traced fault set
1713 * - (ioctl): set start pc (vaddr)
1714 * Always clear the current fault.
1715 * Clear the current signal if 'signo' is zero.
1718 * pi the process or LWP to operate on.
1719 * step if true, set the process or LWP to trap after one instr.
1720 * signo if zero, clear the current signal if any.
1721 * if non-zero, set the current signal to this one.
1723 * Returns non-zero for success, zero for failure.
1727 proc_run_process (procinfo
*pi
, int step
, int signo
)
1733 * We will probably have to apply this operation to individual threads,
1734 * so make sure the control file descriptor is open.
1737 if (pi
->ctl_fd
== 0 &&
1738 open_procinfo_files (pi
, FD_CTL
) == 0)
1743 runflags
= PRCFAULT
; /* always clear current fault */
1748 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1749 proc_set_current_signal (pi
, signo
);
1753 procfs_ctl_t cmd
[2];
1757 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1759 #else /* ioctl method */
1763 memset (&prrun
, 0, sizeof (prrun
));
1764 prrun
.pr_flags
= runflags
;
1765 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1773 * Function: proc_set_traced_signals
1775 * Register to trace signals in the process or LWP.
1776 * Returns non-zero for success, zero for failure.
1780 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1785 * We should never have to apply this operation to any procinfo
1786 * except the one for the main process. If that ever changes
1787 * for any reason, then take out the following clause and
1788 * replace it with one that makes sure the ctl_fd is open.
1792 pi
= find_procinfo_or_die (pi
->pid
, 0);
1798 /* Use char array to avoid alignment issues. */
1799 char sigset
[sizeof (gdb_sigset_t
)];
1803 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1805 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1807 #else /* ioctl method */
1808 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1810 /* The above operation renders the procinfo's cached pstatus obsolete. */
1811 pi
->status_valid
= 0;
1814 warning ("procfs: set_traced_signals failed");
1819 * Function: proc_set_traced_faults
1821 * Register to trace hardware faults in the process or LWP.
1822 * Returns non-zero for success, zero for failure.
1826 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1831 * We should never have to apply this operation to any procinfo
1832 * except the one for the main process. If that ever changes
1833 * for any reason, then take out the following clause and
1834 * replace it with one that makes sure the ctl_fd is open.
1838 pi
= find_procinfo_or_die (pi
->pid
, 0);
1844 /* Use char array to avoid alignment issues. */
1845 char fltset
[sizeof (fltset_t
)];
1849 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1851 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1853 #else /* ioctl method */
1854 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1856 /* The above operation renders the procinfo's cached pstatus obsolete. */
1857 pi
->status_valid
= 0;
1863 * Function: proc_set_traced_sysentry
1865 * Register to trace entry to system calls in the process or LWP.
1866 * Returns non-zero for success, zero for failure.
1870 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1875 * We should never have to apply this operation to any procinfo
1876 * except the one for the main process. If that ever changes
1877 * for any reason, then take out the following clause and
1878 * replace it with one that makes sure the ctl_fd is open.
1882 pi
= find_procinfo_or_die (pi
->pid
, 0);
1886 struct gdb_proc_ctl_pcsentry
{
1888 /* Use char array to avoid alignment issues. */
1889 char sysset
[sizeof (sysset_t
)];
1891 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1893 + sysset_t_size (pi
);
1895 argp
= xmalloc (argp_size
);
1897 argp
->cmd
= PCSENTRY
;
1898 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1900 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1903 #else /* ioctl method */
1904 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1906 /* The above operation renders the procinfo's cached pstatus obsolete. */
1907 pi
->status_valid
= 0;
1913 * Function: proc_set_traced_sysexit
1915 * Register to trace exit from system calls in the process or LWP.
1916 * Returns non-zero for success, zero for failure.
1920 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1925 * We should never have to apply this operation to any procinfo
1926 * except the one for the main process. If that ever changes
1927 * for any reason, then take out the following clause and
1928 * replace it with one that makes sure the ctl_fd is open.
1932 pi
= find_procinfo_or_die (pi
->pid
, 0);
1936 struct gdb_proc_ctl_pcsexit
{
1938 /* Use char array to avoid alignment issues. */
1939 char sysset
[sizeof (sysset_t
)];
1941 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1943 + sysset_t_size (pi
);
1945 argp
= xmalloc (argp_size
);
1947 argp
->cmd
= PCSEXIT
;
1948 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1950 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1953 #else /* ioctl method */
1954 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1956 /* The above operation renders the procinfo's cached pstatus obsolete. */
1957 pi
->status_valid
= 0;
1963 * Function: proc_set_held_signals
1965 * Specify the set of blocked / held signals in the process or LWP.
1966 * Returns non-zero for success, zero for failure.
1970 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1975 * We should never have to apply this operation to any procinfo
1976 * except the one for the main process. If that ever changes
1977 * for any reason, then take out the following clause and
1978 * replace it with one that makes sure the ctl_fd is open.
1982 pi
= find_procinfo_or_die (pi
->pid
, 0);
1988 /* Use char array to avoid alignment issues. */
1989 char hold
[sizeof (gdb_sigset_t
)];
1993 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1994 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1997 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1999 /* The above operation renders the procinfo's cached pstatus obsolete. */
2000 pi
->status_valid
= 0;
2006 * Function: proc_get_pending_signals
2008 * returns the set of signals that are pending in the process or LWP.
2009 * Will also copy the sigset if 'save' is non-zero.
2013 proc_get_pending_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2015 gdb_sigset_t
*ret
= NULL
;
2018 * We should never have to apply this operation to any procinfo
2019 * except the one for the main process. If that ever changes
2020 * for any reason, then take out the following clause and
2021 * replace it with one that makes sure the ctl_fd is open.
2025 pi
= find_procinfo_or_die (pi
->pid
, 0);
2027 if (!pi
->status_valid
)
2028 if (!proc_get_status (pi
))
2032 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
2034 ret
= &pi
->prstatus
.pr_sigpend
;
2037 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2043 * Function: proc_get_signal_actions
2045 * returns the set of signal actions.
2046 * Will also copy the sigactionset if 'save' is non-zero.
2050 proc_get_signal_actions (procinfo
*pi
, gdb_sigaction_t
*save
)
2052 gdb_sigaction_t
*ret
= NULL
;
2055 * We should never have to apply this operation to any procinfo
2056 * except the one for the main process. If that ever changes
2057 * for any reason, then take out the following clause and
2058 * replace it with one that makes sure the ctl_fd is open.
2062 pi
= find_procinfo_or_die (pi
->pid
, 0);
2064 if (!pi
->status_valid
)
2065 if (!proc_get_status (pi
))
2069 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
2071 ret
= &pi
->prstatus
.pr_action
;
2074 memcpy (save
, ret
, sizeof (gdb_sigaction_t
));
2080 * Function: proc_get_held_signals
2082 * returns the set of signals that are held / blocked.
2083 * Will also copy the sigset if 'save' is non-zero.
2087 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2089 gdb_sigset_t
*ret
= NULL
;
2092 * We should never have to apply this operation to any procinfo
2093 * except the one for the main process. If that ever changes
2094 * for any reason, then take out the following clause and
2095 * replace it with one that makes sure the ctl_fd is open.
2099 pi
= find_procinfo_or_die (pi
->pid
, 0);
2102 if (!pi
->status_valid
)
2103 if (!proc_get_status (pi
))
2107 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
2109 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
2110 #endif /* UNIXWARE */
2111 #else /* not NEW_PROC_API */
2113 static gdb_sigset_t sigheld
;
2115 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
2118 #endif /* NEW_PROC_API */
2120 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2126 * Function: proc_get_traced_signals
2128 * returns the set of signals that are traced / debugged.
2129 * Will also copy the sigset if 'save' is non-zero.
2133 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2135 gdb_sigset_t
*ret
= NULL
;
2138 * We should never have to apply this operation to any procinfo
2139 * except the one for the main process. If that ever changes
2140 * for any reason, then take out the following clause and
2141 * replace it with one that makes sure the ctl_fd is open.
2145 pi
= find_procinfo_or_die (pi
->pid
, 0);
2148 if (!pi
->status_valid
)
2149 if (!proc_get_status (pi
))
2152 ret
= &pi
->prstatus
.pr_sigtrace
;
2155 static gdb_sigset_t sigtrace
;
2157 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
2162 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2168 * Function: proc_trace_signal
2170 * Add 'signo' to the set of signals that are traced.
2171 * Returns non-zero for success, zero for failure.
2175 proc_trace_signal (procinfo
*pi
, int signo
)
2180 * We should never have to apply this operation to any procinfo
2181 * except the one for the main process. If that ever changes
2182 * for any reason, then take out the following clause and
2183 * replace it with one that makes sure the ctl_fd is open.
2187 pi
= find_procinfo_or_die (pi
->pid
, 0);
2191 if (proc_get_traced_signals (pi
, &temp
))
2193 praddset (&temp
, signo
);
2194 return proc_set_traced_signals (pi
, &temp
);
2198 return 0; /* failure */
2202 * Function: proc_ignore_signal
2204 * Remove 'signo' from the set of signals that are traced.
2205 * Returns non-zero for success, zero for failure.
2209 proc_ignore_signal (procinfo
*pi
, int signo
)
2214 * We should never have to apply this operation to any procinfo
2215 * except the one for the main process. If that ever changes
2216 * for any reason, then take out the following clause and
2217 * replace it with one that makes sure the ctl_fd is open.
2221 pi
= find_procinfo_or_die (pi
->pid
, 0);
2225 if (proc_get_traced_signals (pi
, &temp
))
2227 prdelset (&temp
, signo
);
2228 return proc_set_traced_signals (pi
, &temp
);
2232 return 0; /* failure */
2236 * Function: proc_get_traced_faults
2238 * returns the set of hardware faults that are traced /debugged.
2239 * Will also copy the faultset if 'save' is non-zero.
2243 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
2245 fltset_t
*ret
= NULL
;
2248 * We should never have to apply this operation to any procinfo
2249 * except the one for the main process. If that ever changes
2250 * for any reason, then take out the following clause and
2251 * replace it with one that makes sure the ctl_fd is open.
2255 pi
= find_procinfo_or_die (pi
->pid
, 0);
2258 if (!pi
->status_valid
)
2259 if (!proc_get_status (pi
))
2262 ret
= &pi
->prstatus
.pr_flttrace
;
2265 static fltset_t flttrace
;
2267 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2272 memcpy (save
, ret
, sizeof (fltset_t
));
2278 * Function: proc_get_traced_sysentry
2280 * returns the set of syscalls that are traced /debugged on entry.
2281 * Will also copy the syscall set if 'save' is non-zero.
2285 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
2287 sysset_t
*ret
= NULL
;
2290 * We should never have to apply this operation to any procinfo
2291 * except the one for the main process. If that ever changes
2292 * for any reason, then take out the following clause and
2293 * replace it with one that makes sure the ctl_fd is open.
2297 pi
= find_procinfo_or_die (pi
->pid
, 0);
2300 if (!pi
->status_valid
)
2301 if (!proc_get_status (pi
))
2304 #ifndef DYNAMIC_SYSCALLS
2305 ret
= &pi
->prstatus
.pr_sysentry
;
2306 #else /* DYNAMIC_SYSCALLS */
2308 static sysset_t
*sysentry
;
2312 sysentry
= sysset_t_alloc (pi
);
2314 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2316 if (pi
->prstatus
.pr_sysentry_offset
== 0)
2318 gdb_premptysysset (sysentry
);
2324 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
2326 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
2328 size
= sysset_t_size (pi
);
2329 gdb_premptysysset (sysentry
);
2330 rsize
= read (pi
->status_fd
, sysentry
, size
);
2335 #endif /* DYNAMIC_SYSCALLS */
2336 #else /* !NEW_PROC_API */
2338 static sysset_t sysentry
;
2340 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2343 #endif /* NEW_PROC_API */
2345 memcpy (save
, ret
, sysset_t_size (pi
));
2351 * Function: proc_get_traced_sysexit
2353 * returns the set of syscalls that are traced /debugged on exit.
2354 * Will also copy the syscall set if 'save' is non-zero.
2358 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
2360 sysset_t
* ret
= NULL
;
2363 * We should never have to apply this operation to any procinfo
2364 * except the one for the main process. If that ever changes
2365 * for any reason, then take out the following clause and
2366 * replace it with one that makes sure the ctl_fd is open.
2370 pi
= find_procinfo_or_die (pi
->pid
, 0);
2373 if (!pi
->status_valid
)
2374 if (!proc_get_status (pi
))
2377 #ifndef DYNAMIC_SYSCALLS
2378 ret
= &pi
->prstatus
.pr_sysexit
;
2379 #else /* DYNAMIC_SYSCALLS */
2381 static sysset_t
*sysexit
;
2385 sysexit
= sysset_t_alloc (pi
);
2387 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2389 if (pi
->prstatus
.pr_sysexit_offset
== 0)
2391 gdb_premptysysset (sysexit
);
2397 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
, SEEK_SET
)
2398 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2400 size
= sysset_t_size (pi
);
2401 gdb_premptysysset (sysexit
);
2402 rsize
= read (pi
->status_fd
, sysexit
, size
);
2407 #endif /* DYNAMIC_SYSCALLS */
2410 static sysset_t sysexit
;
2412 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2417 memcpy (save
, ret
, sysset_t_size (pi
));
2423 * Function: proc_clear_current_fault
2425 * The current fault (if any) is cleared; the associated signal
2426 * will not be sent to the process or LWP when it resumes.
2427 * Returns non-zero for success, zero for failure.
2431 proc_clear_current_fault (procinfo
*pi
)
2436 * We should never have to apply this operation to any procinfo
2437 * except the one for the main process. If that ever changes
2438 * for any reason, then take out the following clause and
2439 * replace it with one that makes sure the ctl_fd is open.
2443 pi
= find_procinfo_or_die (pi
->pid
, 0);
2447 procfs_ctl_t cmd
= PCCFAULT
;
2448 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2451 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2458 * Function: proc_set_current_signal
2460 * Set the "current signal" that will be delivered next to the process.
2461 * NOTE: semantics are different from those of KILL.
2462 * This signal will be delivered to the process or LWP
2463 * immediately when it is resumed (even if the signal is held/blocked);
2464 * it will NOT immediately cause another event of interest, and will NOT
2465 * first trap back to the debugger.
2467 * Returns non-zero for success, zero for failure.
2471 proc_set_current_signal (procinfo
*pi
, int signo
)
2476 /* Use char array to avoid alignment issues. */
2477 char sinfo
[sizeof (gdb_siginfo_t
)];
2479 gdb_siginfo_t
*mysinfo
;
2482 * We should never have to apply this operation to any procinfo
2483 * except the one for the main process. If that ever changes
2484 * for any reason, then take out the following clause and
2485 * replace it with one that makes sure the ctl_fd is open.
2489 pi
= find_procinfo_or_die (pi
->pid
, 0);
2491 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2492 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2493 * receives a PIOCSSIG with a signal identical to the current signal,
2494 * it messes up the current signal. Work around the kernel bug.
2497 signo
== proc_cursig (pi
))
2498 return 1; /* I assume this is a success? */
2501 /* The pointer is just a type alias. */
2502 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2503 mysinfo
->si_signo
= signo
;
2504 mysinfo
->si_code
= 0;
2505 mysinfo
->si_pid
= getpid (); /* ?why? */
2506 mysinfo
->si_uid
= getuid (); /* ?why? */
2510 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2512 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2519 * Function: proc_clear_current_signal
2521 * The current signal (if any) is cleared, and
2522 * is not sent to the process or LWP when it resumes.
2523 * Returns non-zero for success, zero for failure.
2527 proc_clear_current_signal (procinfo
*pi
)
2532 * We should never have to apply this operation to any procinfo
2533 * except the one for the main process. If that ever changes
2534 * for any reason, then take out the following clause and
2535 * replace it with one that makes sure the ctl_fd is open.
2539 pi
= find_procinfo_or_die (pi
->pid
, 0);
2545 /* Use char array to avoid alignment issues. */
2546 char sinfo
[sizeof (gdb_siginfo_t
)];
2548 gdb_siginfo_t
*mysinfo
;
2551 /* The pointer is just a type alias. */
2552 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2553 mysinfo
->si_signo
= 0;
2554 mysinfo
->si_code
= 0;
2555 mysinfo
->si_errno
= 0;
2556 mysinfo
->si_pid
= getpid (); /* ?why? */
2557 mysinfo
->si_uid
= getuid (); /* ?why? */
2559 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2562 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2569 * Function: proc_get_gregs
2571 * Get the general registers for the process or LWP.
2572 * Returns non-zero for success, zero for failure.
2576 proc_get_gregs (procinfo
*pi
)
2578 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2579 if (!proc_get_status (pi
))
2583 * OK, sorry about the ifdef's.
2584 * There's three cases instead of two, because
2585 * in this instance Unixware and Solaris/RW differ.
2589 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2590 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2591 #else /* not Unixware */
2592 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2593 #endif /* Unixware */
2594 #else /* not NEW_PROC_API */
2595 return &pi
->prstatus
.pr_reg
;
2596 #endif /* NEW_PROC_API */
2600 * Function: proc_get_fpregs
2602 * Get the floating point registers for the process or LWP.
2603 * Returns non-zero for success, zero for failure.
2607 proc_get_fpregs (procinfo
*pi
)
2610 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2611 if (!proc_get_status (pi
))
2614 #ifdef UNIXWARE /* a true architecture dependency */
2615 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2617 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2618 #endif /* Unixware */
2620 #else /* not NEW_PROC_API */
2621 if (pi
->fpregs_valid
)
2622 return &pi
->fpregset
; /* already got 'em */
2625 if (pi
->ctl_fd
== 0 &&
2626 open_procinfo_files (pi
, FD_CTL
) == 0)
2635 tid_t pr_error_thread
;
2636 tfpregset_t thread_1
;
2639 thread_fpregs
.pr_count
= 1;
2640 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2643 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2645 pi
->fpregs_valid
= 1;
2646 return &pi
->fpregset
; /* got 'em now! */
2648 else if (pi
->tid
!= 0 &&
2649 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2651 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2652 sizeof (pi
->fpregset
));
2653 pi
->fpregs_valid
= 1;
2654 return &pi
->fpregset
; /* got 'em now! */
2661 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2663 pi
->fpregs_valid
= 1;
2664 return &pi
->fpregset
; /* got 'em now! */
2677 * Function: proc_set_gregs
2679 * Write the general registers back to the process or LWP.
2680 * Returns non-zero for success, zero for failure.
2684 proc_set_gregs (procinfo
*pi
)
2686 gdb_gregset_t
*gregs
;
2689 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2690 return 0; /* get_regs has already warned */
2692 if (pi
->ctl_fd
== 0 &&
2693 open_procinfo_files (pi
, FD_CTL
) == 0)
2702 /* Use char array to avoid alignment issues. */
2703 char gregs
[sizeof (gdb_gregset_t
)];
2707 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2708 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2710 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2714 /* Policy: writing the regs invalidates our cache. */
2715 pi
->gregs_valid
= 0;
2720 * Function: proc_set_fpregs
2722 * Modify the floating point register set of the process or LWP.
2723 * Returns non-zero for success, zero for failure.
2727 proc_set_fpregs (procinfo
*pi
)
2729 gdb_fpregset_t
*fpregs
;
2732 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2733 return 0; /* get_fpregs has already warned */
2735 if (pi
->ctl_fd
== 0 &&
2736 open_procinfo_files (pi
, FD_CTL
) == 0)
2745 /* Use char array to avoid alignment issues. */
2746 char fpregs
[sizeof (gdb_fpregset_t
)];
2750 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2751 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2755 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2760 tid_t pr_error_thread
;
2761 tfpregset_t thread_1
;
2764 thread_fpregs
.pr_count
= 1;
2765 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2766 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2768 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2771 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2772 #endif /* osf PIOCTSFPREG */
2773 #endif /* NEW_PROC_API */
2776 /* Policy: writing the regs invalidates our cache. */
2777 pi
->fpregs_valid
= 0;
2782 * Function: proc_kill
2784 * Send a signal to the proc or lwp with the semantics of "kill()".
2785 * Returns non-zero for success, zero for failure.
2789 proc_kill (procinfo
*pi
, int signo
)
2794 * We might conceivably apply this operation to an LWP, and
2795 * the LWP's ctl file descriptor might not be open.
2798 if (pi
->ctl_fd
== 0 &&
2799 open_procinfo_files (pi
, FD_CTL
) == 0)
2806 procfs_ctl_t cmd
[2];
2810 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2811 #else /* ioctl method */
2812 /* FIXME: do I need the Alpha OSF fixups present in
2813 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2814 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2822 * Function: proc_parent_pid
2824 * Find the pid of the process that started this one.
2825 * Returns the parent process pid, or zero.
2829 proc_parent_pid (procinfo
*pi
)
2832 * We should never have to apply this operation to any procinfo
2833 * except the one for the main process. If that ever changes
2834 * for any reason, then take out the following clause and
2835 * replace it with one that makes sure the ctl_fd is open.
2839 pi
= find_procinfo_or_die (pi
->pid
, 0);
2841 if (!pi
->status_valid
)
2842 if (!proc_get_status (pi
))
2845 return pi
->prstatus
.pr_ppid
;
2849 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2850 (a.k.a void pointer)! */
2853 procfs_address_to_host_pointer (CORE_ADDR addr
)
2857 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (builtin_type_void_data_ptr
));
2858 ADDRESS_TO_POINTER (builtin_type_void_data_ptr
, &ptr
, addr
);
2863 * Function: proc_set_watchpoint
2868 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2870 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2873 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2874 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2879 char watch
[sizeof (prwatch_t
)];
2883 pwatch
= (prwatch_t
*) &arg
.watch
;
2884 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2885 convert a target address into something that can be stored in a
2886 native data structure. */
2887 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2888 pwatch
->pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
2890 pwatch
->pr_vaddr
= (caddr_t
) procfs_address_to_host_pointer (addr
);
2892 pwatch
->pr_size
= len
;
2893 pwatch
->pr_wflags
= wflags
;
2894 #if defined(NEW_PROC_API) && defined (PCWATCH)
2896 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2898 #if defined (PIOCSWATCH)
2899 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2901 return 0; /* Fail */
2908 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2910 #include <sys/sysi86.h>
2913 * Function: proc_get_LDT_entry
2919 * The 'key' is actually the value of the lower 16 bits of
2920 * the GS register for the LWP that we're interested in.
2922 * Return: matching ssh struct (LDT entry).
2926 proc_get_LDT_entry (procinfo
*pi
, int key
)
2928 static struct ssd
*ldt_entry
= NULL
;
2930 char pathname
[MAX_PROC_NAME_SIZE
];
2931 struct cleanup
*old_chain
= NULL
;
2934 /* Allocate space for one LDT entry.
2935 This alloc must persist, because we return a pointer to it. */
2936 if (ldt_entry
== NULL
)
2937 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2939 /* Open the file descriptor for the LDT table. */
2940 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2941 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2943 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2946 /* Make sure it gets closed again! */
2947 old_chain
= make_cleanup_close (fd
);
2949 /* Now 'read' thru the table, find a match and return it. */
2950 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2952 if (ldt_entry
->sel
== 0 &&
2953 ldt_entry
->bo
== 0 &&
2954 ldt_entry
->acc1
== 0 &&
2955 ldt_entry
->acc2
== 0)
2956 break; /* end of table */
2957 /* If key matches, return this entry. */
2958 if (ldt_entry
->sel
== key
)
2961 /* Loop ended, match not found. */
2965 static int nalloc
= 0;
2967 /* Get the number of LDT entries. */
2968 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2970 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2974 /* Allocate space for the number of LDT entries. */
2975 /* This alloc has to persist, 'cause we return a pointer to it. */
2978 ldt_entry
= (struct ssd
*)
2979 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2983 /* Read the whole table in one gulp. */
2984 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2986 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2990 /* Search the table and return the (first) entry matching 'key'. */
2991 for (i
= 0; i
< nldt
; i
++)
2992 if (ldt_entry
[i
].sel
== key
)
2993 return &ldt_entry
[i
];
2995 /* Loop ended, match not found. */
3000 #endif /* TM_I386SOL2_H */
3002 /* =============== END, non-thread part of /proc "MODULE" =============== */
3004 /* =================== Thread "MODULE" =================== */
3006 /* NOTE: you'll see more ifdefs and duplication of functions here,
3007 since there is a different way to do threads on every OS. */
3010 * Function: proc_get_nthreads
3012 * Return the number of threads for the process
3015 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3020 proc_get_nthreads (procinfo
*pi
)
3024 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
3025 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
3031 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3033 * Solaris and Unixware version
3036 proc_get_nthreads (procinfo
*pi
)
3038 if (!pi
->status_valid
)
3039 if (!proc_get_status (pi
))
3043 * NEW_PROC_API: only works for the process procinfo,
3044 * because the LWP procinfos do not get prstatus filled in.
3047 if (pi
->tid
!= 0) /* find the parent process procinfo */
3048 pi
= find_procinfo_or_die (pi
->pid
, 0);
3050 return pi
->prstatus
.pr_nlwp
;
3058 proc_get_nthreads (procinfo
*pi
)
3066 * Function: proc_get_current_thread (LWP version)
3068 * Return the ID of the thread that had an event of interest.
3069 * (ie. the one that hit a breakpoint or other traced event).
3070 * All other things being equal, this should be the ID of a
3071 * thread that is currently executing.
3074 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3076 * Solaris and Unixware version
3079 proc_get_current_thread (procinfo
*pi
)
3082 * Note: this should be applied to the root procinfo for the process,
3083 * not to the procinfo for an LWP. If applied to the procinfo for
3084 * an LWP, it will simply return that LWP's ID. In that case,
3085 * find the parent process procinfo.
3089 pi
= find_procinfo_or_die (pi
->pid
, 0);
3091 if (!pi
->status_valid
)
3092 if (!proc_get_status (pi
))
3096 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
3098 return pi
->prstatus
.pr_who
;
3103 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3108 proc_get_current_thread (procinfo
*pi
)
3110 #if 0 /* FIXME: not ready for prime time? */
3111 return pi
->prstatus
.pr_tid
;
3122 proc_get_current_thread (procinfo
*pi
)
3131 * Function: proc_update_threads
3133 * Discover the IDs of all the threads within the process, and
3134 * create a procinfo for each of them (chained to the parent).
3136 * This unfortunately requires a different method on every OS.
3138 * Return: non-zero for success, zero for failure.
3142 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
3144 if (thread
&& parent
) /* sanity */
3146 thread
->status_valid
= 0;
3147 if (!proc_get_status (thread
))
3148 destroy_one_procinfo (&parent
->thread_list
, thread
);
3150 return 0; /* keep iterating */
3153 #if defined (PIOCLSTATUS)
3155 * Solaris 2.5 (ioctl) version
3158 proc_update_threads (procinfo
*pi
)
3160 gdb_prstatus_t
*prstatus
;
3161 struct cleanup
*old_chain
= NULL
;
3166 * We should never have to apply this operation to any procinfo
3167 * except the one for the main process. If that ever changes
3168 * for any reason, then take out the following clause and
3169 * replace it with one that makes sure the ctl_fd is open.
3173 pi
= find_procinfo_or_die (pi
->pid
, 0);
3175 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3177 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
3178 return 1; /* Process is not multi-threaded; nothing to do. */
3180 prstatus
= xmalloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1));
3182 old_chain
= make_cleanup (xfree
, prstatus
);
3183 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
3184 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
3186 /* Skip element zero, which represents the process as a whole. */
3187 for (i
= 1; i
< nlwp
+ 1; i
++)
3189 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
3190 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3192 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
3193 thread
->status_valid
= 1;
3195 pi
->threads_valid
= 1;
3196 do_cleanups (old_chain
);
3202 * Unixware and Solaris 6 (and later) version
3205 do_closedir_cleanup (void *dir
)
3211 proc_update_threads (procinfo
*pi
)
3213 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3214 struct dirent
*direntry
;
3215 struct cleanup
*old_chain
= NULL
;
3221 * We should never have to apply this operation to any procinfo
3222 * except the one for the main process. If that ever changes
3223 * for any reason, then take out the following clause and
3224 * replace it with one that makes sure the ctl_fd is open.
3228 pi
= find_procinfo_or_die (pi
->pid
, 0);
3230 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3235 * Note: this brute-force method is the only way I know of
3236 * to accomplish this task on Unixware. This method will
3237 * also work on Solaris 2.6 and 2.7. There is a much simpler
3238 * and more elegant way to do this on Solaris, but the margins
3239 * of this manuscript are too small to write it here... ;-)
3242 strcpy (pathname
, pi
->pathname
);
3243 strcat (pathname
, "/lwp");
3244 if ((dirp
= opendir (pathname
)) == NULL
)
3245 proc_error (pi
, "update_threads, opendir", __LINE__
);
3247 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
3248 while ((direntry
= readdir (dirp
)) != NULL
)
3249 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3251 lwpid
= atoi (&direntry
->d_name
[0]);
3252 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3253 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3255 pi
->threads_valid
= 1;
3256 do_cleanups (old_chain
);
3265 proc_update_threads (procinfo
*pi
)
3271 * We should never have to apply this operation to any procinfo
3272 * except the one for the main process. If that ever changes
3273 * for any reason, then take out the following clause and
3274 * replace it with one that makes sure the ctl_fd is open.
3278 pi
= find_procinfo_or_die (pi
->pid
, 0);
3280 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3282 nthreads
= proc_get_nthreads (pi
);
3284 return 0; /* nothing to do for 1 or fewer threads */
3286 threads
= xmalloc (nthreads
* sizeof (tid_t
));
3288 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3289 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3291 for (i
= 0; i
< nthreads
; i
++)
3293 if (!find_procinfo (pi
->pid
, threads
[i
]))
3294 if (!create_procinfo (pi
->pid
, threads
[i
]))
3295 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3297 pi
->threads_valid
= 1;
3305 proc_update_threads (procinfo
*pi
)
3309 #endif /* OSF PIOCTLIST */
3310 #endif /* NEW_PROC_API */
3311 #endif /* SOL 2.5 PIOCLSTATUS */
3314 * Function: proc_iterate_over_threads
3317 * Given a pointer to a function, call that function once
3318 * for each lwp in the procinfo list, until the function
3319 * returns non-zero, in which event return the value
3320 * returned by the function.
3322 * Note: this function does NOT call update_threads.
3323 * If you want to discover new threads first, you must
3324 * call that function explicitly. This function just makes
3325 * a quick pass over the currently-known procinfos.
3328 * pi - parent process procinfo
3329 * func - per-thread function
3330 * ptr - opaque parameter for function.
3333 * First non-zero return value from the callee, or zero.
3337 proc_iterate_over_threads (procinfo
*pi
,
3338 int (*func
) (procinfo
*, procinfo
*, void *),
3341 procinfo
*thread
, *next
;
3345 * We should never have to apply this operation to any procinfo
3346 * except the one for the main process. If that ever changes
3347 * for any reason, then take out the following clause and
3348 * replace it with one that makes sure the ctl_fd is open.
3352 pi
= find_procinfo_or_die (pi
->pid
, 0);
3354 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3356 next
= thread
->next
; /* in case thread is destroyed */
3357 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3364 /* =================== END, Thread "MODULE" =================== */
3366 /* =================== END, /proc "MODULE" =================== */
3368 /* =================== GDB "MODULE" =================== */
3371 * Here are all of the gdb target vector functions and their friends.
3374 static ptid_t
do_attach (ptid_t ptid
);
3375 static void do_detach (int signo
);
3376 static int register_gdb_signals (procinfo
*, gdb_sigset_t
*);
3379 * Function: procfs_debug_inferior
3381 * Sets up the inferior to be debugged.
3382 * Registers to trace signals, hardware faults, and syscalls.
3383 * Note: does not set RLC flag: caller may want to customize that.
3385 * Returns: zero for success (note! unlike most functions in this module)
3386 * On failure, returns the LINE NUMBER where it failed!
3390 procfs_debug_inferior (procinfo
*pi
)
3392 fltset_t traced_faults
;
3393 gdb_sigset_t traced_signals
;
3394 sysset_t
*traced_syscall_entries
;
3395 sysset_t
*traced_syscall_exits
;
3398 #ifdef PROCFS_DONT_TRACE_FAULTS
3399 /* On some systems (OSF), we don't trace hardware faults.
3400 Apparently it's enough that we catch them as signals.
3401 Wonder why we don't just do that in general? */
3402 premptyset (&traced_faults
); /* don't trace faults. */
3404 /* Register to trace hardware faults in the child. */
3405 prfillset (&traced_faults
); /* trace all faults... */
3406 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3408 if (!proc_set_traced_faults (pi
, &traced_faults
))
3411 /* Register to trace selected signals in the child. */
3412 premptyset (&traced_signals
);
3413 if (!register_gdb_signals (pi
, &traced_signals
))
3417 /* Register to trace the 'exit' system call (on entry). */
3418 traced_syscall_entries
= sysset_t_alloc (pi
);
3419 gdb_premptysysset (traced_syscall_entries
);
3421 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
3424 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3427 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
3429 #ifdef DYNAMIC_SYSCALLS
3431 int callnum
= find_syscall (pi
, "_exit");
3433 gdb_praddsysset (traced_syscall_entries
, callnum
);
3437 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
3438 xfree (traced_syscall_entries
);
3442 #ifdef PRFS_STOPEXEC /* defined on OSF */
3443 /* OSF method for tracing exec syscalls. Quoting:
3444 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3445 exits from exec system calls because of the user level loader. */
3446 /* FIXME: make nice and maybe move into an access function. */
3450 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3453 prfs_flags
|= PRFS_STOPEXEC
;
3455 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3458 #else /* not PRFS_STOPEXEC */
3459 /* Everyone else's (except OSF) method for tracing exec syscalls */
3461 Not all systems with /proc have all the exec* syscalls with the same
3462 names. On the SGI, for example, there is no SYS_exec, but there
3463 *is* a SYS_execv. So, we try to account for that. */
3465 traced_syscall_exits
= sysset_t_alloc (pi
);
3466 gdb_premptysysset (traced_syscall_exits
);
3468 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
3471 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
3474 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
3477 #ifdef SYS_lwpcreate
3478 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3479 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3482 #ifdef SYS_lwp_create /* FIXME: once only, please */
3483 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3484 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3487 #ifdef DYNAMIC_SYSCALLS
3489 int callnum
= find_syscall (pi
, "execve");
3491 gdb_praddsysset (traced_syscall_exits
, callnum
);
3492 callnum
= find_syscall (pi
, "ra_execve");
3494 gdb_praddsysset (traced_syscall_exits
, callnum
);
3498 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3499 xfree (traced_syscall_exits
);
3503 #endif /* PRFS_STOPEXEC */
3508 procfs_attach (char *args
, int from_tty
)
3514 error_no_arg ("process-id to attach");
3517 if (pid
== getpid ())
3518 error ("Attaching GDB to itself is not a good idea...");
3522 exec_file
= get_exec_file (0);
3525 printf_filtered ("Attaching to program `%s', %s\n",
3526 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3528 printf_filtered ("Attaching to %s\n",
3529 target_pid_to_str (pid_to_ptid (pid
)));
3533 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3534 push_target (&procfs_ops
);
3538 procfs_detach (char *args
, int from_tty
)
3545 exec_file
= get_exec_file (0);
3548 printf_filtered ("Detaching from program: %s %s\n",
3549 exec_file
, target_pid_to_str (inferior_ptid
));
3553 signo
= atoi (args
);
3556 inferior_ptid
= null_ptid
;
3557 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3561 do_attach (ptid_t ptid
)
3566 if ((pi
= create_procinfo (PIDGET (ptid
), 0)) == NULL
)
3567 perror ("procfs: out of memory in 'attach'");
3569 if (!open_procinfo_files (pi
, FD_CTL
))
3571 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3572 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3574 dead_procinfo (pi
, errmsg
, NOKILL
);
3577 /* Stop the process (if it isn't already stopped). */
3578 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3580 pi
->was_stopped
= 1;
3581 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3585 pi
->was_stopped
= 0;
3586 /* Set the process to run again when we close it. */
3587 if (!proc_set_run_on_last_close (pi
))
3588 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3590 /* Now stop the process. */
3591 if (!proc_stop_process (pi
))
3592 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3593 pi
->ignore_next_sigstop
= 1;
3595 /* Save some of the /proc state to be restored if we detach. */
3596 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3597 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3598 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3599 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3600 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3601 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3603 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3604 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3606 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3607 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3609 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3610 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3612 /* Let GDB know that the inferior was attached. */
3614 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3618 do_detach (int signo
)
3622 /* Find procinfo for the main process */
3623 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0); /* FIXME: threads */
3625 if (!proc_set_current_signal (pi
, signo
))
3626 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3628 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3629 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3631 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3632 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3634 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3635 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3637 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3638 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3640 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3641 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3643 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3644 if (signo
|| !(pi
->was_stopped
) ||
3645 query ("Was stopped when attached, make it runnable again? "))
3647 /* Clear any pending signal. */
3648 if (!proc_clear_current_fault (pi
))
3649 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3651 if (signo
== 0 && !proc_clear_current_signal (pi
))
3652 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
3654 if (!proc_set_run_on_last_close (pi
))
3655 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3659 destroy_procinfo (pi
);
3665 * Since the /proc interface cannot give us individual registers,
3666 * we pay no attention to the (regno) argument, and just fetch them all.
3667 * This results in the possibility that we will do unnecessarily many
3668 * fetches, since we may be called repeatedly for individual registers.
3669 * So we cache the results, and mark the cache invalid when the process
3674 procfs_fetch_registers (int regno
)
3676 gdb_fpregset_t
*fpregs
;
3677 gdb_gregset_t
*gregs
;
3682 pid
= PIDGET (inferior_ptid
);
3683 tid
= TIDGET (inferior_ptid
);
3685 /* First look up procinfo for the main process. */
3686 pi
= find_procinfo_or_die (pid
, 0);
3688 /* If the event thread is not the same as GDB's requested thread
3689 (ie. inferior_ptid), then look up procinfo for the requested
3692 (tid
!= proc_get_current_thread (pi
)))
3693 pi
= find_procinfo_or_die (pid
, tid
);
3696 error ("procfs: fetch_registers failed to find procinfo for %s",
3697 target_pid_to_str (inferior_ptid
));
3699 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3700 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3702 supply_gregset (gregs
);
3704 if (FP0_REGNUM
>= 0) /* need floating point? */
3706 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3707 regno
== PC_REGNUM
||
3708 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3709 regno
== FP_REGNUM
||
3711 return; /* not a floating point register */
3713 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3714 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3716 supply_fpregset (fpregs
);
3720 /* Get ready to modify the registers array. On machines which store
3721 individual registers, this doesn't need to do anything. On
3722 machines which store all the registers in one fell swoop, such as
3723 /proc, this makes sure that registers contains all the registers
3724 from the program being debugged. */
3727 procfs_prepare_to_store (void)
3729 #ifdef CHILD_PREPARE_TO_STORE
3730 CHILD_PREPARE_TO_STORE ();
3737 * Since the /proc interface will not read individual registers,
3738 * we will cache these requests until the process is resumed, and
3739 * only then write them back to the inferior process.
3741 * FIXME: is that a really bad idea? Have to think about cases
3742 * where writing one register might affect the value of others, etc.
3746 procfs_store_registers (int regno
)
3748 gdb_fpregset_t
*fpregs
;
3749 gdb_gregset_t
*gregs
;
3754 pid
= PIDGET (inferior_ptid
);
3755 tid
= TIDGET (inferior_ptid
);
3757 /* First find procinfo for main process */
3758 pi
= find_procinfo_or_die (pid
, 0);
3760 /* If current lwp for process is not the same as requested thread
3761 (ie. inferior_ptid), then find procinfo for the requested thread. */
3764 (tid
!= proc_get_current_thread (pi
)))
3765 pi
= find_procinfo_or_die (pid
, tid
);
3768 error ("procfs: store_registers: failed to find procinfo for %s",
3769 target_pid_to_str (inferior_ptid
));
3771 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3772 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3774 fill_gregset (gregs
, regno
);
3775 if (!proc_set_gregs (pi
))
3776 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3778 if (FP0_REGNUM
>= 0) /* need floating point? */
3780 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3781 regno
== PC_REGNUM
||
3782 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3783 regno
== FP_REGNUM
||
3785 return; /* not a floating point register */
3787 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3788 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3790 fill_fpregset (fpregs
, regno
);
3791 if (!proc_set_fpregs (pi
))
3792 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3797 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3801 if (scall
== SYS_lwp_exit
)
3805 if (scall
== SYS_lwpexit
)
3812 syscall_is_exit (procinfo
*pi
, int scall
)
3815 if (scall
== SYS_exit
)
3818 #ifdef DYNAMIC_SYSCALLS
3819 if (find_syscall (pi
, "_exit") == scall
)
3826 syscall_is_exec (procinfo
*pi
, int scall
)
3829 if (scall
== SYS_exec
)
3833 if (scall
== SYS_execv
)
3837 if (scall
== SYS_execve
)
3840 #ifdef DYNAMIC_SYSCALLS
3841 if (find_syscall (pi
, "_execve"))
3843 if (find_syscall (pi
, "ra_execve"))
3850 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3852 #ifdef SYS_lwp_create
3853 if (scall
== SYS_lwp_create
)
3856 #ifdef SYS_lwpcreate
3857 if (scall
== SYS_lwpcreate
)
3864 * Function: target_wait
3866 * Retrieve the next stop event from the child process.
3867 * If child has not stopped yet, wait for it to stop.
3868 * Translate /proc eventcodes (or possibly wait eventcodes)
3869 * into gdb internal event codes.
3871 * Return: id of process (and possibly thread) that incurred the event.
3872 * event codes are returned thru a pointer parameter.
3876 procfs_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3878 /* First cut: loosely based on original version 2.1 */
3882 ptid_t retval
, temp_ptid
;
3883 int why
, what
, flags
;
3890 retval
= pid_to_ptid (-1);
3892 /* Find procinfo for main process */
3893 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
3896 /* We must assume that the status is stale now... */
3897 pi
->status_valid
= 0;
3898 pi
->gregs_valid
= 0;
3899 pi
->fpregs_valid
= 0;
3901 #if 0 /* just try this out... */
3902 flags
= proc_flags (pi
);
3903 why
= proc_why (pi
);
3904 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3905 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3907 /* If child is not stopped, wait for it to stop. */
3908 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3909 !proc_wait_for_stop (pi
))
3911 /* wait_for_stop failed: has the child terminated? */
3912 if (errno
== ENOENT
)
3916 /* /proc file not found; presumably child has terminated. */
3917 wait_retval
= wait (&wstat
); /* "wait" for the child's exit */
3919 if (wait_retval
!= PIDGET (inferior_ptid
)) /* wrong child? */
3920 error ("procfs: couldn't stop process %d: wait returned %d\n",
3921 PIDGET (inferior_ptid
), wait_retval
);
3922 /* FIXME: might I not just use waitpid?
3923 Or try find_procinfo to see if I know about this child? */
3924 retval
= pid_to_ptid (wait_retval
);
3926 else if (errno
== EINTR
)
3930 /* Unknown error from wait_for_stop. */
3931 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3936 /* This long block is reached if either:
3937 a) the child was already stopped, or
3938 b) we successfully waited for the child with wait_for_stop.
3939 This block will analyze the /proc status, and translate it
3940 into a waitstatus for GDB.
3942 If we actually had to call wait because the /proc file
3943 is gone (child terminated), then we skip this block,
3944 because we already have a waitstatus. */
3946 flags
= proc_flags (pi
);
3947 why
= proc_why (pi
);
3948 what
= proc_what (pi
);
3950 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3953 /* If it's running async (for single_thread control),
3954 set it back to normal again. */
3955 if (flags
& PR_ASYNC
)
3956 if (!proc_unset_async (pi
))
3957 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3961 proc_prettyprint_why (why
, what
, 1);
3963 /* The 'pid' we will return to GDB is composed of
3964 the process ID plus the lwp ID. */
3965 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3969 wstat
= (what
<< 8) | 0177;
3972 if (syscall_is_lwp_exit (pi
, what
))
3974 printf_filtered ("[%s exited]\n",
3975 target_pid_to_str (retval
));
3976 delete_thread (retval
);
3977 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3980 else if (syscall_is_exit (pi
, what
))
3982 /* Handle SYS_exit call only */
3983 /* Stopped at entry to SYS_exit.
3984 Make it runnable, resume it, then use
3985 the wait system call to get its exit code.
3986 Proc_run_process always clears the current
3988 Then return its exit status. */
3989 pi
->status_valid
= 0;
3991 /* FIXME: what we should do is return
3992 TARGET_WAITKIND_SPURIOUS. */
3993 if (!proc_run_process (pi
, 0, 0))
3994 proc_error (pi
, "target_wait, run_process", __LINE__
);
3997 /* Don't call wait: simulate waiting for exit,
3998 return a "success" exit code. Bogus: what if
3999 it returns something else? */
4001 retval
= inferior_ptid
; /* ? ? ? */
4005 int temp
= wait (&wstat
);
4007 /* FIXME: shouldn't I make sure I get the right
4008 event from the right process? If (for
4009 instance) I have killed an earlier inferior
4010 process but failed to clean up after it
4011 somehow, I could get its termination event
4014 /* If wait returns -1, that's what we return to GDB. */
4016 retval
= pid_to_ptid (temp
);
4021 printf_filtered ("procfs: trapped on entry to ");
4022 proc_prettyprint_syscall (proc_what (pi
), 0);
4023 printf_filtered ("\n");
4026 long i
, nsysargs
, *sysargs
;
4028 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4029 (sysargs
= proc_sysargs (pi
)) != NULL
)
4031 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4032 for (i
= 0; i
< nsysargs
; i
++)
4033 printf_filtered ("#%ld: 0x%08lx\n",
4041 /* How to exit gracefully, returning "unknown event" */
4042 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4043 return inferior_ptid
;
4047 /* How to keep going without returning to wfi: */
4048 target_resume (ptid
, 0, TARGET_SIGNAL_0
);
4054 if (syscall_is_exec (pi
, what
))
4056 /* Hopefully this is our own "fork-child" execing
4057 the real child. Hoax this event into a trap, and
4058 GDB will see the child about to execute its start
4060 wstat
= (SIGTRAP
<< 8) | 0177;
4062 else if (syscall_is_lwp_create (pi
, what
))
4065 * This syscall is somewhat like fork/exec.
4066 * We will get the event twice: once for the parent LWP,
4067 * and once for the child. We should already know about
4068 * the parent LWP, but the child will be new to us. So,
4069 * whenever we get this event, if it represents a new
4070 * thread, simply add the thread to the list.
4073 /* If not in procinfo list, add it. */
4074 temp_tid
= proc_get_current_thread (pi
);
4075 if (!find_procinfo (pi
->pid
, temp_tid
))
4076 create_procinfo (pi
->pid
, temp_tid
);
4078 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4079 /* If not in GDB's thread list, add it. */
4080 if (!in_thread_list (temp_ptid
))
4082 printf_filtered ("[New %s]\n",
4083 target_pid_to_str (temp_ptid
));
4084 add_thread (temp_ptid
);
4086 /* Return to WFI, but tell it to immediately resume. */
4087 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4088 return inferior_ptid
;
4090 else if (syscall_is_lwp_exit (pi
, what
))
4092 printf_filtered ("[%s exited]\n",
4093 target_pid_to_str (retval
));
4094 delete_thread (retval
);
4095 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4100 /* FIXME: Do we need to handle SYS_sproc,
4101 SYS_fork, or SYS_vfork here? The old procfs
4102 seemed to use this event to handle threads on
4103 older (non-LWP) systems, where I'm assuming
4104 that threads were actually separate processes.
4105 Irix, maybe? Anyway, low priority for now. */
4109 printf_filtered ("procfs: trapped on exit from ");
4110 proc_prettyprint_syscall (proc_what (pi
), 0);
4111 printf_filtered ("\n");
4114 long i
, nsysargs
, *sysargs
;
4116 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4117 (sysargs
= proc_sysargs (pi
)) != NULL
)
4119 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4120 for (i
= 0; i
< nsysargs
; i
++)
4121 printf_filtered ("#%ld: 0x%08lx\n",
4126 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4127 return inferior_ptid
;
4132 wstat
= (SIGSTOP
<< 8) | 0177;
4137 printf_filtered ("Retry #%d:\n", retry
);
4138 pi
->status_valid
= 0;
4143 /* If not in procinfo list, add it. */
4144 temp_tid
= proc_get_current_thread (pi
);
4145 if (!find_procinfo (pi
->pid
, temp_tid
))
4146 create_procinfo (pi
->pid
, temp_tid
);
4148 /* If not in GDB's thread list, add it. */
4149 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4150 if (!in_thread_list (temp_ptid
))
4152 printf_filtered ("[New %s]\n",
4153 target_pid_to_str (temp_ptid
));
4154 add_thread (temp_ptid
);
4157 status
->kind
= TARGET_WAITKIND_STOPPED
;
4158 status
->value
.sig
= 0;
4163 wstat
= (what
<< 8) | 0177;
4166 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
4169 wstat
= (SIGTRAP
<< 8) | 0177;
4174 wstat
= (SIGTRAP
<< 8) | 0177;
4177 /* FIXME: use si_signo where possible. */
4179 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4182 wstat
= (SIGILL
<< 8) | 0177;
4185 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4188 wstat
= (SIGTRAP
<< 8) | 0177;
4192 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4195 wstat
= (SIGSEGV
<< 8) | 0177;
4199 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4202 wstat
= (SIGFPE
<< 8) | 0177;
4204 case FLTPAGE
: /* Recoverable page fault */
4205 default: /* FIXME: use si_signo if possible for fault */
4206 retval
= pid_to_ptid (-1);
4207 printf_filtered ("procfs:%d -- ", __LINE__
);
4208 printf_filtered ("child stopped for unknown reason:\n");
4209 proc_prettyprint_why (why
, what
, 1);
4210 error ("... giving up...");
4213 break; /* case PR_FAULTED: */
4214 default: /* switch (why) unmatched */
4215 printf_filtered ("procfs:%d -- ", __LINE__
);
4216 printf_filtered ("child stopped for unknown reason:\n");
4217 proc_prettyprint_why (why
, what
, 1);
4218 error ("... giving up...");
4222 * Got this far without error:
4223 * If retval isn't in the threads database, add it.
4225 if (PIDGET (retval
) > 0 &&
4226 !ptid_equal (retval
, inferior_ptid
) &&
4227 !in_thread_list (retval
))
4230 * We have a new thread.
4231 * We need to add it both to GDB's list and to our own.
4232 * If we don't create a procinfo, resume may be unhappy
4235 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
4236 add_thread (retval
);
4237 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
4238 create_procinfo (PIDGET (retval
), TIDGET (retval
));
4240 /* In addition, it's possible that this is the first
4241 * new thread we've seen, in which case we may not
4242 * have created entries for inferior_ptid yet.
4244 if (TIDGET (inferior_ptid
) != 0)
4246 if (!in_thread_list (inferior_ptid
))
4247 add_thread (inferior_ptid
);
4248 if (find_procinfo (PIDGET (inferior_ptid
),
4249 TIDGET (inferior_ptid
)) == NULL
)
4250 create_procinfo (PIDGET (inferior_ptid
),
4251 TIDGET (inferior_ptid
));
4255 else /* flags do not indicate STOPPED */
4257 /* surely this can't happen... */
4258 printf_filtered ("procfs:%d -- process not stopped.\n",
4260 proc_prettyprint_flags (flags
, 1);
4261 error ("procfs: ...giving up...");
4266 store_waitstatus (status
, wstat
);
4272 /* Transfer LEN bytes between GDB address MYADDR and target address
4273 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4274 otherwise transfer them from the target. TARGET is unused.
4276 The return value is 0 if an error occurred or no bytes were
4277 transferred. Otherwise, it will be a positive value which
4278 indicates the number of bytes transferred between gdb and the
4279 target. (Note that the interface also makes provisions for
4280 negative values, but this capability isn't implemented here.) */
4283 procfs_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int dowrite
,
4284 struct mem_attrib
*attrib
, struct target_ops
*target
)
4289 /* Find procinfo for main process */
4290 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4291 if (pi
->as_fd
== 0 &&
4292 open_procinfo_files (pi
, FD_AS
) == 0)
4294 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4298 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4303 PROCFS_NOTE ("write memory: ");
4305 PROCFS_NOTE ("write memory: \n");
4307 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4311 PROCFS_NOTE ("read memory: \n");
4312 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4323 * Function: invalidate_cache
4325 * Called by target_resume before making child runnable.
4326 * Mark cached registers and status's invalid.
4327 * If there are "dirty" caches that need to be written back
4328 * to the child process, do that.
4330 * File descriptors are also cached.
4331 * As they are a limited resource, we cannot hold onto them indefinitely.
4332 * However, as they are expensive to open, we don't want to throw them
4333 * away indescriminately either. As a compromise, we will keep the
4334 * file descriptors for the parent process, but discard any file
4335 * descriptors we may have accumulated for the threads.
4338 * As this function is called by iterate_over_threads, it always
4339 * returns zero (so that iterate_over_threads will keep iterating).
4344 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4347 * About to run the child; invalidate caches and do any other cleanup.
4351 if (pi
->gregs_dirty
)
4352 if (parent
== NULL
||
4353 proc_get_current_thread (parent
) != pi
->tid
)
4354 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4355 proc_warn (pi
, "target_resume, set_gregs",
4357 if (FP0_REGNUM
>= 0)
4358 if (pi
->fpregs_dirty
)
4359 if (parent
== NULL
||
4360 proc_get_current_thread (parent
) != pi
->tid
)
4361 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4362 proc_warn (pi
, "target_resume, set_fpregs",
4368 /* The presence of a parent indicates that this is an LWP.
4369 Close any file descriptors that it might have open.
4370 We don't do this to the master (parent) procinfo. */
4372 close_procinfo_files (pi
);
4374 pi
->gregs_valid
= 0;
4375 pi
->fpregs_valid
= 0;
4377 pi
->gregs_dirty
= 0;
4378 pi
->fpregs_dirty
= 0;
4380 pi
->status_valid
= 0;
4381 pi
->threads_valid
= 0;
4388 * Function: make_signal_thread_runnable
4390 * A callback function for iterate_over_threads.
4391 * Find the asynchronous signal thread, and make it runnable.
4392 * See if that helps matters any.
4396 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4399 if (proc_flags (pi
) & PR_ASLWP
)
4401 if (!proc_run_process (pi
, 0, -1))
4402 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4411 * Function: target_resume
4413 * Make the child process runnable. Normally we will then call
4414 * procfs_wait and wait for it to stop again (unles gdb is async).
4417 * step: if true, then arrange for the child to stop again
4418 * after executing a single instruction.
4419 * signo: if zero, then cancel any pending signal.
4420 * If non-zero, then arrange for the indicated signal
4421 * to be delivered to the child when it runs.
4422 * pid: if -1, then allow any child thread to run.
4423 * if non-zero, then allow only the indicated thread to run.
4424 ******* (not implemented yet)
4428 procfs_resume (ptid_t ptid
, int step
, enum target_signal signo
)
4430 procinfo
*pi
, *thread
;
4434 prrun.prflags |= PRSVADDR;
4435 prrun.pr_vaddr = $PC; set resume address
4436 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4437 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4438 prrun.prflags |= PRCFAULT; clear current fault.
4440 PRSTRACE and PRSFAULT can be done by other means
4441 (proc_trace_signals, proc_trace_faults)
4442 PRSVADDR is unnecessary.
4443 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4444 This basically leaves PRSTEP and PRCSIG.
4445 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4446 So basically PR_STEP is the sole argument that must be passed
4447 to proc_run_process (for use in the prrun struct by ioctl). */
4449 /* Find procinfo for main process */
4450 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4452 /* First cut: ignore pid argument */
4455 /* Convert signal to host numbering. */
4457 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4460 native_signo
= target_signal_to_host (signo
);
4462 pi
->ignore_next_sigstop
= 0;
4464 /* Running the process voids all cached registers and status. */
4465 /* Void the threads' caches first */
4466 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4467 /* Void the process procinfo's caches. */
4468 invalidate_cache (NULL
, pi
, NULL
);
4470 if (PIDGET (ptid
) != -1)
4472 /* Resume a specific thread, presumably suppressing the others. */
4473 thread
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
));
4476 if (thread
->tid
!= 0)
4478 /* We're to resume a specific thread, and not the others.
4479 * Set the child process's PR_ASYNC flag.
4482 if (!proc_set_async (pi
))
4483 proc_error (pi
, "target_resume, set_async", __LINE__
);
4486 proc_iterate_over_threads (pi
,
4487 make_signal_thread_runnable
,
4490 pi
= thread
; /* substitute the thread's procinfo for run */
4495 if (!proc_run_process (pi
, step
, native_signo
))
4498 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4500 proc_error (pi
, "target_resume", __LINE__
);
4505 * Function: register_gdb_signals
4507 * Traverse the list of signals that GDB knows about
4508 * (see "handle" command), and arrange for the target
4509 * to be stopped or not, according to these settings.
4511 * Returns non-zero for success, zero for failure.
4515 register_gdb_signals (procinfo
*pi
, gdb_sigset_t
*signals
)
4519 for (signo
= 0; signo
< NSIG
; signo
++)
4520 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4521 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4522 signal_pass_state (target_signal_from_host (signo
)) == 1)
4523 prdelset (signals
, signo
);
4525 praddset (signals
, signo
);
4527 return proc_set_traced_signals (pi
, signals
);
4531 * Function: target_notice_signals
4533 * Set up to trace signals in the child process.
4537 procfs_notice_signals (ptid_t ptid
)
4539 gdb_sigset_t signals
;
4540 procinfo
*pi
= find_procinfo_or_die (PIDGET (ptid
), 0);
4542 if (proc_get_traced_signals (pi
, &signals
) &&
4543 register_gdb_signals (pi
, &signals
))
4546 proc_error (pi
, "notice_signals", __LINE__
);
4550 * Function: target_files_info
4552 * Print status information about the child process.
4556 procfs_files_info (struct target_ops
*ignore
)
4558 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4559 attach_flag
? "attached": "child",
4560 target_pid_to_str (inferior_ptid
));
4564 * Function: target_open
4566 * A dummy: you don't open procfs.
4570 procfs_open (char *args
, int from_tty
)
4572 error ("Use the \"run\" command to start a Unix child process.");
4576 * Function: target_can_run
4578 * This tells GDB that this target vector can be invoked
4579 * for "run" or "attach".
4582 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4583 be a runnable target. Used by targets
4584 that can sit atop procfs, such as solaris
4589 procfs_can_run (void)
4591 /* This variable is controlled by modules that sit atop procfs that
4592 may layer their own process structure atop that provided here.
4593 sol-thread.c does this because of the Solaris two-level thread
4596 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4598 return !procfs_suppress_run
;
4602 * Function: target_stop
4604 * Stop the child process asynchronously, as when the
4605 * gdb user types control-c or presses a "stop" button.
4607 * Works by sending kill(SIGINT) to the child's process group.
4613 kill (-inferior_process_group
, SIGINT
);
4617 * Function: unconditionally_kill_inferior
4619 * Make it die. Wait for it to die. Clean up after it.
4620 * Note: this should only be applied to the real process,
4621 * not to an LWP, because of the check for parent-process.
4622 * If we need this to work for an LWP, it needs some more logic.
4626 unconditionally_kill_inferior (procinfo
*pi
)
4630 parent_pid
= proc_parent_pid (pi
);
4631 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4632 /* FIXME: use access functions */
4633 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4634 before the PIOCKILL, otherwise it might generate a corrupted core
4635 file for the inferior. */
4636 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4638 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4641 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4642 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4643 to kill the inferior, otherwise it might remain stopped with a
4645 We do not check the result of the PIOCSSIG, the inferior might have
4648 gdb_siginfo_t newsiginfo
;
4650 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4651 newsiginfo
.si_signo
= SIGKILL
;
4652 newsiginfo
.si_code
= 0;
4653 newsiginfo
.si_errno
= 0;
4654 newsiginfo
.si_pid
= getpid ();
4655 newsiginfo
.si_uid
= getuid ();
4656 /* FIXME: use proc_set_current_signal */
4657 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4659 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4660 if (!proc_kill (pi
, SIGKILL
))
4661 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4662 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4663 destroy_procinfo (pi
);
4665 /* If pi is GDB's child, wait for it to die. */
4666 if (parent_pid
== getpid ())
4667 /* FIXME: should we use waitpid to make sure we get the right event?
4668 Should we check the returned event? */
4673 ret
= waitpid (pi
->pid
, &status
, 0);
4681 * Function: target_kill_inferior
4683 * We're done debugging it, and we want it to go away.
4684 * Then we want GDB to forget all about it.
4688 procfs_kill_inferior (void)
4690 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4692 /* Find procinfo for main process */
4693 procinfo
*pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4696 unconditionally_kill_inferior (pi
);
4697 target_mourn_inferior ();
4702 * Function: target_mourn_inferior
4704 * Forget we ever debugged this thing!
4708 procfs_mourn_inferior (void)
4712 if (!ptid_equal (inferior_ptid
, null_ptid
))
4714 /* Find procinfo for main process */
4715 pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4717 destroy_procinfo (pi
);
4719 unpush_target (&procfs_ops
);
4720 generic_mourn_inferior ();
4724 * Function: init_inferior
4726 * When GDB forks to create a runnable inferior process,
4727 * this function is called on the parent side of the fork.
4728 * It's job is to do whatever is necessary to make the child
4729 * ready to be debugged, and then wait for the child to synchronize.
4733 procfs_init_inferior (int pid
)
4736 gdb_sigset_t signals
;
4739 /* This routine called on the parent side (GDB side)
4740 after GDB forks the inferior. */
4742 push_target (&procfs_ops
);
4744 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4745 perror ("procfs: out of memory in 'init_inferior'");
4747 if (!open_procinfo_files (pi
, FD_CTL
))
4748 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4752 open_procinfo_files // done
4755 procfs_notice_signals
4762 /* If not stopped yet, wait for it to stop. */
4763 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4764 !(proc_wait_for_stop (pi
)))
4765 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4767 /* Save some of the /proc state to be restored if we detach. */
4768 /* FIXME: Why? In case another debugger was debugging it?
4769 We're it's parent, for Ghu's sake! */
4770 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4771 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4772 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4773 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4774 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4775 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4776 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4777 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4778 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4779 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4781 /* Register to trace selected signals in the child. */
4782 prfillset (&signals
);
4783 if (!register_gdb_signals (pi
, &signals
))
4784 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4786 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4787 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4789 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4790 and possibly even turning ON kill-on-last-close at this point. But
4791 I can't make that change without careful testing which I don't have
4792 time to do right now... */
4793 /* Turn on run-on-last-close flag so that the child
4794 will die if GDB goes away for some reason. */
4795 if (!proc_set_run_on_last_close (pi
))
4796 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4798 /* The 'process ID' we return to GDB is composed of
4799 the actual process ID plus the lwp ID. */
4800 inferior_ptid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4802 #ifdef START_INFERIOR_TRAPS_EXPECTED
4803 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4805 /* One trap to exec the shell, one to exec the program being debugged. */
4806 startup_inferior (2);
4807 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4811 * Function: set_exec_trap
4813 * When GDB forks to create a new process, this function is called
4814 * on the child side of the fork before GDB exec's the user program.
4815 * Its job is to make the child minimally debuggable, so that the
4816 * parent GDB process can connect to the child and take over.
4817 * This function should do only the minimum to make that possible,
4818 * and to synchronize with the parent process. The parent process
4819 * should take care of the details.
4823 procfs_set_exec_trap (void)
4825 /* This routine called on the child side (inferior side)
4826 after GDB forks the inferior. It must use only local variables,
4827 because it may be sharing data space with its parent. */
4832 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4833 perror_with_name ("procfs: create_procinfo failed in child.");
4835 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4837 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4838 gdb_flush (gdb_stderr
);
4839 /* no need to call "dead_procinfo", because we're going to exit. */
4843 #ifdef PRFS_STOPEXEC /* defined on OSF */
4844 /* OSF method for tracing exec syscalls. Quoting:
4845 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4846 exits from exec system calls because of the user level loader. */
4847 /* FIXME: make nice and maybe move into an access function. */
4851 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4853 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4854 gdb_flush (gdb_stderr
);
4857 prfs_flags
|= PRFS_STOPEXEC
;
4859 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4861 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4862 gdb_flush (gdb_stderr
);
4866 #else /* not PRFS_STOPEXEC */
4867 /* Everyone else's (except OSF) method for tracing exec syscalls */
4869 Not all systems with /proc have all the exec* syscalls with the same
4870 names. On the SGI, for example, there is no SYS_exec, but there
4871 *is* a SYS_execv. So, we try to account for that. */
4873 exitset
= sysset_t_alloc (pi
);
4874 gdb_premptysysset (exitset
);
4876 gdb_praddsysset (exitset
, SYS_exec
);
4879 gdb_praddsysset (exitset
, SYS_execve
);
4882 gdb_praddsysset (exitset
, SYS_execv
);
4884 #ifdef DYNAMIC_SYSCALLS
4886 int callnum
= find_syscall (pi
, "execve");
4889 gdb_praddsysset (exitset
, callnum
);
4891 callnum
= find_syscall (pi
, "ra_execve");
4893 gdb_praddsysset (exitset
, callnum
);
4895 #endif /* DYNAMIC_SYSCALLS */
4897 if (!proc_set_traced_sysexit (pi
, exitset
))
4899 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4900 gdb_flush (gdb_stderr
);
4903 #endif /* PRFS_STOPEXEC */
4905 /* FIXME: should this be done in the parent instead? */
4906 /* Turn off inherit on fork flag so that all grand-children
4907 of gdb start with tracing flags cleared. */
4908 if (!proc_unset_inherit_on_fork (pi
))
4909 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4911 /* Turn off run on last close flag, so that the child process
4912 cannot run away just because we close our handle on it.
4913 We want it to wait for the parent to attach. */
4914 if (!proc_unset_run_on_last_close (pi
))
4915 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4917 /* FIXME: No need to destroy the procinfo --
4918 we have our own address space, and we're about to do an exec! */
4919 /*destroy_procinfo (pi);*/
4923 * Function: create_inferior
4925 * This function is called BEFORE gdb forks the inferior process.
4926 * Its only real responsibility is to set things up for the fork,
4927 * and tell GDB which two functions to call after the fork (one
4928 * for the parent, and one for the child).
4930 * This function does a complicated search for a unix shell program,
4931 * which it then uses to parse arguments and environment variables
4932 * to be sent to the child. I wonder whether this code could not
4933 * be abstracted out and shared with other unix targets such as
4938 procfs_create_inferior (char *exec_file
, char *allargs
, char **env
)
4940 char *shell_file
= getenv ("SHELL");
4942 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4945 /* We will be looking down the PATH to find shell_file. If we
4946 just do this the normal way (via execlp, which operates by
4947 attempting an exec for each element of the PATH until it
4948 finds one which succeeds), then there will be an exec for
4949 each failed attempt, each of which will cause a PR_SYSEXIT
4950 stop, and we won't know how to distinguish the PR_SYSEXIT's
4951 for these failed execs with the ones for successful execs
4952 (whether the exec has succeeded is stored at that time in the
4953 carry bit or some such architecture-specific and
4954 non-ABI-specified place).
4956 So I can't think of anything better than to search the PATH
4957 now. This has several disadvantages: (1) There is a race
4958 condition; if we find a file now and it is deleted before we
4959 exec it, we lose, even if the deletion leaves a valid file
4960 further down in the PATH, (2) there is no way to know exactly
4961 what an executable (in the sense of "capable of being
4962 exec'd") file is. Using access() loses because it may lose
4963 if the caller is the superuser; failing to use it loses if
4964 there are ACLs or some such. */
4968 /* FIXME-maybe: might want "set path" command so user can change what
4969 path is used from within GDB. */
4970 char *path
= getenv ("PATH");
4972 struct stat statbuf
;
4975 path
= "/bin:/usr/bin";
4977 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4978 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4980 p1
= strchr (p
, ':');
4985 strncpy (tryname
, p
, len
);
4986 tryname
[len
] = '\0';
4987 strcat (tryname
, "/");
4988 strcat (tryname
, shell_file
);
4989 if (access (tryname
, X_OK
) < 0)
4991 if (stat (tryname
, &statbuf
) < 0)
4993 if (!S_ISREG (statbuf
.st_mode
))
4994 /* We certainly need to reject directories. I'm not quite
4995 as sure about FIFOs, sockets, etc., but I kind of doubt
4996 that people want to exec() these things. */
5001 /* Not found. This must be an error rather than merely passing
5002 the file to execlp(), because execlp() would try all the
5003 exec()s, causing GDB to get confused. */
5004 error ("procfs:%d -- Can't find shell %s in PATH",
5005 __LINE__
, shell_file
);
5007 shell_file
= tryname
;
5010 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
5011 procfs_init_inferior
, NULL
, shell_file
);
5013 /* We are at the first instruction we care about. */
5014 /* Pedal to the metal... */
5016 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
5020 * Function: notice_thread
5022 * Callback for find_new_threads.
5023 * Calls "add_thread".
5027 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
5029 ptid_t gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
5031 if (!in_thread_list (gdb_threadid
))
5032 add_thread (gdb_threadid
);
5038 * Function: target_find_new_threads
5040 * Query all the threads that the target knows about,
5041 * and give them back to GDB to add to its list.
5045 procfs_find_new_threads (void)
5049 /* Find procinfo for main process */
5050 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5051 proc_update_threads (pi
);
5052 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
5056 * Function: target_thread_alive
5058 * Return true if the thread is still 'alive'.
5060 * This guy doesn't really seem to be doing his job.
5061 * Got to investigate how to tell when a thread is really gone.
5065 procfs_thread_alive (ptid_t ptid
)
5070 proc
= PIDGET (ptid
);
5071 thread
= TIDGET (ptid
);
5072 /* If I don't know it, it ain't alive! */
5073 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
5076 /* If I can't get its status, it ain't alive!
5077 What's more, I need to forget about it! */
5078 if (!proc_get_status (pi
))
5080 destroy_procinfo (pi
);
5083 /* I couldn't have got its status if it weren't alive, so it's alive. */
5088 * Function: target_pid_to_str
5090 * Return a string to be used to identify the thread in
5091 * the "info threads" display.
5095 procfs_pid_to_str (ptid_t ptid
)
5097 static char buf
[80];
5101 proc
= PIDGET (ptid
);
5102 thread
= TIDGET (ptid
);
5103 pi
= find_procinfo (proc
, thread
);
5106 sprintf (buf
, "Process %d", proc
);
5108 sprintf (buf
, "LWP %d", thread
);
5113 * Function: procfs_set_watchpoint
5114 * Insert a watchpoint
5118 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
5126 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5127 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5129 /* Translate from GDB's flags to /proc's */
5130 if (len
> 0) /* len == 0 means delete watchpoint */
5132 switch (rwflag
) { /* FIXME: need an enum! */
5133 case hw_write
: /* default watchpoint (write) */
5134 pflags
= WRITE_WATCHFLAG
;
5136 case hw_read
: /* read watchpoint */
5137 pflags
= READ_WATCHFLAG
;
5139 case hw_access
: /* access watchpoint */
5140 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
5142 case hw_execute
: /* execution HW breakpoint */
5143 pflags
= EXEC_WATCHFLAG
;
5145 default: /* Something weird. Return error. */
5148 if (after
) /* Stop after r/w access is completed. */
5149 pflags
|= AFTER_WATCHFLAG
;
5152 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
5154 if (errno
== E2BIG
) /* Typical error for no resources */
5155 return -1; /* fail */
5156 /* GDB may try to remove the same watchpoint twice.
5157 If a remove request returns no match, don't error. */
5158 if (errno
== ESRCH
&& len
== 0)
5159 return 0; /* ignore */
5160 proc_error (pi
, "set_watchpoint", __LINE__
);
5163 #endif /* UNIXWARE */
5167 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
5168 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
5169 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
5172 Note: procfs_can_use_hw_breakpoint() is not yet used by all
5173 procfs.c targets due to the fact that some of them still define
5174 TARGET_CAN_USE_HARDWARE_WATCHPOINT. */
5177 procfs_can_use_hw_breakpoint (int type
, int cnt
, int othertype
)
5179 #ifndef TARGET_HAS_HARDWARE_WATCHPOINTS
5182 /* Due to the way that proc_set_watchpoint() is implemented, host
5183 and target pointers must be of the same size. If they are not,
5184 we can't use hardware watchpoints. This limitation is due to the
5185 fact that proc_set_watchpoint() calls
5186 procfs_address_to_host_pointer(); a close inspection of
5187 procfs_address_to_host_pointer will reveal that an internal error
5188 will be generated when the host and target pointer sizes are
5190 if (sizeof (void *) != TYPE_LENGTH (builtin_type_void_data_ptr
))
5193 /* Other tests here??? */
5200 * Function: stopped_by_watchpoint
5202 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5203 * else returns zero.
5207 procfs_stopped_by_watchpoint (ptid_t ptid
)
5211 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5212 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5214 if (!pi
) /* If no process, then not stopped by watchpoint! */
5217 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
5219 if (proc_why (pi
) == PR_FAULTED
)
5222 if (proc_what (pi
) == FLTWATCH
)
5226 if (proc_what (pi
) == FLTKWATCH
)
5234 #ifdef TM_I386SOL2_H
5236 * Function: procfs_find_LDT_entry
5239 * ptid_t ptid; // The GDB-style pid-plus-LWP.
5242 * pointer to the corresponding LDT entry.
5246 procfs_find_LDT_entry (ptid_t ptid
)
5248 gdb_gregset_t
*gregs
;
5252 /* Find procinfo for the lwp. */
5253 if ((pi
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
))) == NULL
)
5255 warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.",
5256 PIDGET (ptid
), TIDGET (ptid
));
5259 /* get its general registers. */
5260 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
5262 warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.",
5263 PIDGET (ptid
), TIDGET (ptid
));
5266 /* Now extract the GS register's lower 16 bits. */
5267 key
= (*gregs
)[GS
] & 0xffff;
5269 /* Find the matching entry and return it. */
5270 return proc_get_LDT_entry (pi
, key
);
5272 #endif /* TM_I386SOL2_H */
5275 * Memory Mappings Functions:
5279 * Function: iterate_over_mappings
5281 * Call a callback function once for each mapping, passing it the mapping,
5282 * an optional secondary callback function, and some optional opaque data.
5283 * Quit and return the first non-zero value returned from the callback.
5286 * pi -- procinfo struct for the process to be mapped.
5287 * func -- callback function to be called by this iterator.
5288 * data -- optional opaque data to be passed to the callback function.
5289 * child_func -- optional secondary function pointer to be passed
5290 * to the child function.
5292 * Return: First non-zero return value from the callback function,
5297 iterate_over_mappings (procinfo
*pi
, int (*child_func
) (), void *data
,
5298 int (*func
) (struct prmap
*map
,
5299 int (*child_func
) (),
5302 char pathname
[MAX_PROC_NAME_SIZE
];
5303 struct prmap
*prmaps
;
5304 struct prmap
*prmap
;
5312 /* Get the number of mappings, allocate space,
5313 and read the mappings into prmaps. */
5316 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
5317 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
5318 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
5320 /* Make sure it gets closed again. */
5321 make_cleanup_close (map_fd
);
5323 /* Use stat to determine the file size, and compute
5324 the number of prmap_t objects it contains. */
5325 if (fstat (map_fd
, &sbuf
) != 0)
5326 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
5328 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
5329 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5330 if (read (map_fd
, (char *) prmaps
, nmap
* sizeof (*prmaps
))
5331 != (nmap
* sizeof (*prmaps
)))
5332 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
5334 /* Use ioctl command PIOCNMAP to get number of mappings. */
5335 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmap
) != 0)
5336 proc_error (pi
, "iterate_over_mappings (PIOCNMAP)", __LINE__
);
5338 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5339 if (ioctl (pi
->ctl_fd
, PIOCMAP
, prmaps
) != 0)
5340 proc_error (pi
, "iterate_over_mappings (PIOCMAP)", __LINE__
);
5343 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
5344 if ((funcstat
= (*func
) (prmap
, child_func
, data
)) != 0)
5351 * Function: solib_mappings_callback
5353 * Calls the supplied callback function once for each mapped address
5354 * space in the process. The callback function receives an open
5355 * file descriptor for the file corresponding to that mapped
5356 * address space (if there is one), and the base address of the
5357 * mapped space. Quit when the callback function returns a
5358 * nonzero value, or at teh end of the mappings.
5360 * Returns: the first non-zero return value of the callback function,
5364 int solib_mappings_callback (struct prmap
*map
,
5365 int (*func
) (int, CORE_ADDR
),
5368 procinfo
*pi
= data
;
5372 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
5374 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
5375 return -1; /* sanity */
5377 if (map
->pr_mapname
[0] == 0)
5379 fd
= -1; /* no map file */
5383 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
5384 /* Note: caller's responsibility to close this fd! */
5385 fd
= open_with_retry (name
, O_RDONLY
);
5386 /* Note: we don't test the above call for failure;
5387 we just pass the FD on as given. Sometimes there is
5388 no file, so the open may return failure, but that's
5392 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
->pr_vaddr
);
5393 /* Note: we don't test the above call for failure;
5394 we just pass the FD on as given. Sometimes there is
5395 no file, so the ioctl may return failure, but that's
5398 return (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
);
5402 * Function: proc_iterate_over_mappings
5404 * Uses the unified "iterate_over_mappings" function
5405 * to implement the exported interface to solib-svr4.c.
5407 * Given a pointer to a function, call that function once for every
5408 * mapped address space in the process. The callback function
5409 * receives an open file descriptor for the file corresponding to
5410 * that mapped address space (if there is one), and the base address
5411 * of the mapped space. Quit when the callback function returns a
5412 * nonzero value, or at teh end of the mappings.
5414 * Returns: the first non-zero return value of the callback function,
5419 proc_iterate_over_mappings (int (*func
) (int, CORE_ADDR
))
5421 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5423 return iterate_over_mappings (pi
, func
, pi
, solib_mappings_callback
);
5427 * Function: find_memory_regions_callback
5429 * Implements the to_find_memory_regions method.
5430 * Calls an external function for each memory region.
5431 * External function will have the signiture:
5433 * int callback (CORE_ADDR vaddr,
5434 * unsigned long size,
5435 * int read, int write, int execute,
5438 * Returns the integer value returned by the callback.
5442 find_memory_regions_callback (struct prmap
*map
,
5443 int (*func
) (CORE_ADDR
,
5449 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
5451 (map
->pr_mflags
& MA_READ
) != 0,
5452 (map
->pr_mflags
& MA_WRITE
) != 0,
5453 (map
->pr_mflags
& MA_EXEC
) != 0,
5458 * Function: proc_find_memory_regions
5460 * External interface. Calls a callback function once for each
5461 * mapped memory region in the child process, passing as arguments
5462 * CORE_ADDR virtual_address,
5463 * unsigned long size,
5464 * int read, TRUE if region is readable by the child
5465 * int write, TRUE if region is writable by the child
5466 * int execute TRUE if region is executable by the child.
5468 * Stops iterating and returns the first non-zero value
5469 * returned by the callback.
5473 proc_find_memory_regions (int (*func
) (CORE_ADDR
,
5479 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5481 return iterate_over_mappings (pi
, func
, data
,
5482 find_memory_regions_callback
);
5486 * Function: mappingflags
5488 * Returns an ascii representation of a memory mapping's flags.
5492 mappingflags (long flags
)
5494 static char asciiflags
[8];
5496 strcpy (asciiflags
, "-------");
5497 #if defined (MA_PHYS)
5498 if (flags
& MA_PHYS
)
5499 asciiflags
[0] = 'd';
5501 if (flags
& MA_STACK
)
5502 asciiflags
[1] = 's';
5503 if (flags
& MA_BREAK
)
5504 asciiflags
[2] = 'b';
5505 if (flags
& MA_SHARED
)
5506 asciiflags
[3] = 's';
5507 if (flags
& MA_READ
)
5508 asciiflags
[4] = 'r';
5509 if (flags
& MA_WRITE
)
5510 asciiflags
[5] = 'w';
5511 if (flags
& MA_EXEC
)
5512 asciiflags
[6] = 'x';
5513 return (asciiflags
);
5517 * Function: info_mappings_callback
5519 * Callback function, does the actual work for 'info proc mappings'.
5524 info_mappings_callback (struct prmap
*map
, int (*ignore
) (), void *unused
)
5526 char *data_fmt_string
;
5528 if (TARGET_ADDR_BIT
== 32)
5529 data_fmt_string
= "\t%#10lx %#10lx %#10x %#10x %7s\n";
5531 data_fmt_string
= " %#18lx %#18lx %#10x %#10x %7s\n";
5533 printf_filtered (data_fmt_string
,
5534 (unsigned long) map
->pr_vaddr
,
5535 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5537 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5538 (unsigned int) map
->pr_offset
,
5542 mappingflags (map
->pr_mflags
));
5548 * Function: info_proc_mappings
5550 * Implement the "info proc mappings" subcommand.
5554 info_proc_mappings (procinfo
*pi
, int summary
)
5556 char *header_fmt_string
;
5558 if (TARGET_PTR_BIT
== 32)
5559 header_fmt_string
= "\t%10s %10s %10s %10s %7s\n";
5561 header_fmt_string
= " %18s %18s %10s %10s %7s\n";
5564 return; /* No output for summary mode. */
5566 printf_filtered ("Mapped address spaces:\n\n");
5567 printf_filtered (header_fmt_string
,
5574 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
5575 printf_filtered ("\n");
5579 * Function: info_proc_cmd
5581 * Implement the "info proc" command.
5585 info_proc_cmd (char *args
, int from_tty
)
5587 struct cleanup
*old_chain
;
5588 procinfo
*process
= NULL
;
5589 procinfo
*thread
= NULL
;
5596 old_chain
= make_cleanup (null_cleanup
, 0);
5599 if ((argv
= buildargv (args
)) == NULL
)
5602 make_cleanup_freeargv (argv
);
5604 while (argv
!= NULL
&& *argv
!= NULL
)
5606 if (isdigit (argv
[0][0]))
5608 pid
= strtoul (argv
[0], &tmp
, 10);
5610 tid
= strtoul (++tmp
, NULL
, 10);
5612 else if (argv
[0][0] == '/')
5614 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5616 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
5627 pid
= PIDGET (inferior_ptid
);
5629 error ("No current process: you must name one.");
5632 /* Have pid, will travel.
5633 First see if it's a process we're already debugging. */
5634 process
= find_procinfo (pid
, 0);
5635 if (process
== NULL
)
5637 /* No. So open a procinfo for it, but
5638 remember to close it again when finished. */
5639 process
= create_procinfo (pid
, 0);
5640 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5641 if (!open_procinfo_files (process
, FD_CTL
))
5642 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5646 thread
= create_procinfo (pid
, tid
);
5650 printf_filtered ("process %d flags:\n", process
->pid
);
5651 proc_prettyprint_flags (proc_flags (process
), 1);
5652 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5653 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5654 if (proc_get_nthreads (process
) > 1)
5655 printf_filtered ("Process has %d threads.\n",
5656 proc_get_nthreads (process
));
5660 printf_filtered ("thread %d flags:\n", thread
->tid
);
5661 proc_prettyprint_flags (proc_flags (thread
), 1);
5662 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5663 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5668 info_proc_mappings (process
, 0);
5671 do_cleanups (old_chain
);
5675 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5681 if (PIDGET (inferior_ptid
) <= 0)
5682 error ("you must be debugging a process to use this command.");
5684 if (args
== NULL
|| args
[0] == 0)
5685 error_no_arg ("system call to trace");
5687 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5688 if (isdigit (args
[0]))
5690 syscallnum
= atoi (args
);
5691 if (entry_or_exit
== PR_SYSENTRY
)
5692 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5694 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5697 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5699 if (mode
== FLAG_SET
)
5700 gdb_praddsysset (sysset
, syscallnum
);
5702 gdb_prdelsysset (sysset
, syscallnum
);
5704 if (entry_or_exit
== PR_SYSENTRY
)
5706 if (!proc_set_traced_sysentry (pi
, sysset
))
5707 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5711 if (!proc_set_traced_sysexit (pi
, sysset
))
5712 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5718 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5720 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5724 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5726 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5730 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5732 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5736 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5738 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5743 _initialize_procfs (void)
5746 add_target (&procfs_ops
);
5747 add_info ("proc", info_proc_cmd
,
5748 "Show /proc process information about any running process.\n\
5749 Specify process id, or use the program being debugged by default.\n\
5750 Specify keyword 'mappings' for detailed info on memory mappings.");
5751 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5752 "Give a trace of entries into the syscall.");
5753 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5754 "Give a trace of exits from the syscall.");
5755 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5756 "Cancel a trace of entries into the syscall.");
5757 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5758 "Cancel a trace of exits from the syscall.");
5761 /* =================== END, GDB "MODULE" =================== */
5765 /* miscellaneous stubs: */
5766 /* The following satisfy a few random symbols mostly created by */
5767 /* the solaris threads implementation, which I will chase down */
5771 * Return a pid for which we guarantee
5772 * we will be able to find a 'live' procinfo.
5776 procfs_first_available (void)
5778 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);
5781 /* =================== GCORE .NOTE "MODULE" =================== */
5782 #if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT)
5783 /* gcore only implemented on solaris and unixware (so far) */
5786 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
5787 char *note_data
, int *note_size
)
5789 gdb_gregset_t gregs
;
5790 gdb_fpregset_t fpregs
;
5791 unsigned long merged_pid
;
5793 merged_pid
= TIDGET (ptid
) << 16 | PIDGET (ptid
);
5795 fill_gregset (&gregs
, -1);
5796 #if defined (UNIXWARE)
5797 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
5804 note_data
= (char *) elfcore_write_prstatus (obfd
,
5811 fill_fpregset (&fpregs
, -1);
5812 note_data
= (char *) elfcore_write_prfpreg (obfd
,
5820 struct procfs_corefile_thread_data
{
5827 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
5829 struct procfs_corefile_thread_data
*args
= data
;
5831 if (pi
!= NULL
&& thread
->tid
!= 0)
5833 ptid_t saved_ptid
= inferior_ptid
;
5834 inferior_ptid
= MERGEPID (pi
->pid
, thread
->tid
);
5835 args
->note_data
= procfs_do_thread_registers (args
->obfd
, inferior_ptid
,
5838 inferior_ptid
= saved_ptid
;
5844 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5846 struct cleanup
*old_chain
;
5847 gdb_gregset_t gregs
;
5848 gdb_fpregset_t fpregs
;
5849 char fname
[16] = {'\0'};
5850 char psargs
[80] = {'\0'};
5851 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5852 char *note_data
= NULL
;
5854 struct procfs_corefile_thread_data thread_args
;
5856 if (get_exec_file (0))
5858 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
5859 strncpy (psargs
, get_exec_file (0),
5862 inf_args
= get_inferior_args ();
5863 if (inf_args
&& *inf_args
&&
5864 strlen (inf_args
) < ((int) sizeof (psargs
) - (int) strlen (psargs
)))
5866 strncat (psargs
, " ",
5867 sizeof (psargs
) - strlen (psargs
));
5868 strncat (psargs
, inf_args
,
5869 sizeof (psargs
) - strlen (psargs
));
5873 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
5880 fill_gregset (&gregs
, -1);
5881 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
5882 PIDGET (inferior_ptid
),
5883 stop_signal
, &gregs
);
5886 thread_args
.obfd
= obfd
;
5887 thread_args
.note_data
= note_data
;
5888 thread_args
.note_size
= note_size
;
5889 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
, &thread_args
);
5891 if (thread_args
.note_data
== note_data
)
5893 /* iterate_over_threads didn't come up with any threads;
5894 just use inferior_ptid. */
5895 note_data
= procfs_do_thread_registers (obfd
, inferior_ptid
,
5896 note_data
, note_size
);
5900 note_data
= thread_args
.note_data
;
5903 make_cleanup (xfree
, note_data
);
5906 #else /* !(Solaris or Unixware) */
5908 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5910 error ("gcore not implemented for this host.");
5911 return NULL
; /* lint */
5913 #endif /* Solaris or Unixware */
5914 /* =================== END GCORE .NOTE "MODULE" =================== */