1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
2 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Written by Michael Snyder at Cygnus Solutions.
4 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "elf-bfd.h" /* for elfcore_write_* */
28 #include "gdbthread.h"
30 #if defined (NEW_PROC_API)
31 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
34 #include <sys/procfs.h>
35 #ifdef HAVE_SYS_FAULT_H
36 #include <sys/fault.h>
38 #ifdef HAVE_SYS_SYSCALL_H
39 #include <sys/syscall.h>
41 #include <sys/errno.h>
49 * This module provides the interface between GDB and the
50 * /proc file system, which is used on many versions of Unix
51 * as a means for debuggers to control other processes.
52 * Examples of the systems that use this interface are:
59 * /proc works by imitating a file system: you open a simulated file
60 * that represents the process you wish to interact with, and
61 * perform operations on that "file" in order to examine or change
62 * the state of the other process.
64 * The most important thing to know about /proc and this module
65 * is that there are two very different interfaces to /proc:
66 * One that uses the ioctl system call, and
67 * another that uses read and write system calls.
68 * This module has to support both /proc interfaces. This means
69 * that there are two different ways of doing every basic operation.
71 * In order to keep most of the code simple and clean, I have
72 * defined an interface "layer" which hides all these system calls.
73 * An ifdef (NEW_PROC_API) determines which interface we are using,
74 * and most or all occurrances of this ifdef should be confined to
75 * this interface layer.
79 /* Determine which /proc API we are using:
80 The ioctl API defines PIOCSTATUS, while
81 the read/write (multiple fd) API never does. */
84 #include <sys/types.h>
85 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
88 #include <fcntl.h> /* for O_RDONLY */
89 #include <unistd.h> /* for "X_OK" */
90 #include "gdb_stat.h" /* for struct stat */
92 /* Note: procfs-utils.h must be included after the above system header
93 files, because it redefines various system calls using macros.
94 This may be incompatible with the prototype declarations. */
96 #include "proc-utils.h"
98 /* Prototypes for supply_gregset etc. */
101 /* =================== TARGET_OPS "MODULE" =================== */
104 * This module defines the GDB target vector and its methods.
107 static void procfs_open (char *, int);
108 static void procfs_attach (char *, int);
109 static void procfs_detach (char *, int);
110 static void procfs_resume (ptid_t
, int, enum target_signal
);
111 static int procfs_can_run (void);
112 static void procfs_stop (void);
113 static void procfs_files_info (struct target_ops
*);
114 static void procfs_fetch_registers (int);
115 static void procfs_store_registers (int);
116 static void procfs_notice_signals (ptid_t
);
117 static void procfs_prepare_to_store (void);
118 static void procfs_kill_inferior (void);
119 static void procfs_mourn_inferior (void);
120 static void procfs_create_inferior (char *, char *, char **);
121 static ptid_t
procfs_wait (ptid_t
, struct target_waitstatus
*);
122 static int procfs_xfer_memory (CORE_ADDR
, char *, int, int,
123 struct mem_attrib
*attrib
,
124 struct target_ops
*);
126 static int procfs_thread_alive (ptid_t
);
128 void procfs_find_new_threads (void);
129 char *procfs_pid_to_str (ptid_t
);
131 static int proc_find_memory_regions (int (*) (CORE_ADDR
,
137 static char * procfs_make_note_section (bfd
*, int *);
139 static int procfs_can_use_hw_breakpoint (int, int, int);
141 struct target_ops procfs_ops
; /* the target vector */
144 init_procfs_ops (void)
146 procfs_ops
.to_shortname
= "procfs";
147 procfs_ops
.to_longname
= "Unix /proc child process";
149 "Unix /proc child process (started by the \"run\" command).";
150 procfs_ops
.to_open
= procfs_open
;
151 procfs_ops
.to_can_run
= procfs_can_run
;
152 procfs_ops
.to_create_inferior
= procfs_create_inferior
;
153 procfs_ops
.to_kill
= procfs_kill_inferior
;
154 procfs_ops
.to_mourn_inferior
= procfs_mourn_inferior
;
155 procfs_ops
.to_attach
= procfs_attach
;
156 procfs_ops
.to_detach
= procfs_detach
;
157 procfs_ops
.to_wait
= procfs_wait
;
158 procfs_ops
.to_resume
= procfs_resume
;
159 procfs_ops
.to_prepare_to_store
= procfs_prepare_to_store
;
160 procfs_ops
.to_fetch_registers
= procfs_fetch_registers
;
161 procfs_ops
.to_store_registers
= procfs_store_registers
;
162 procfs_ops
.to_xfer_memory
= procfs_xfer_memory
;
163 procfs_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
164 procfs_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
165 procfs_ops
.to_notice_signals
= procfs_notice_signals
;
166 procfs_ops
.to_files_info
= procfs_files_info
;
167 procfs_ops
.to_stop
= procfs_stop
;
169 procfs_ops
.to_terminal_init
= terminal_init_inferior
;
170 procfs_ops
.to_terminal_inferior
= terminal_inferior
;
171 procfs_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
172 procfs_ops
.to_terminal_ours
= terminal_ours
;
173 procfs_ops
.to_terminal_save_ours
= terminal_save_ours
;
174 procfs_ops
.to_terminal_info
= child_terminal_info
;
176 procfs_ops
.to_find_new_threads
= procfs_find_new_threads
;
177 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
178 procfs_ops
.to_pid_to_str
= procfs_pid_to_str
;
180 procfs_ops
.to_has_all_memory
= 1;
181 procfs_ops
.to_has_memory
= 1;
182 procfs_ops
.to_has_execution
= 1;
183 procfs_ops
.to_has_stack
= 1;
184 procfs_ops
.to_has_registers
= 1;
185 procfs_ops
.to_stratum
= process_stratum
;
186 procfs_ops
.to_has_thread_control
= tc_schedlock
;
187 procfs_ops
.to_find_memory_regions
= proc_find_memory_regions
;
188 procfs_ops
.to_make_corefile_notes
= procfs_make_note_section
;
189 procfs_ops
.to_can_use_hw_breakpoint
= procfs_can_use_hw_breakpoint
;
190 procfs_ops
.to_magic
= OPS_MAGIC
;
193 /* =================== END, TARGET_OPS "MODULE" =================== */
198 * Put any typedefs, defines etc. here that are required for
199 * the unification of code that handles different versions of /proc.
202 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
204 enum { READ_WATCHFLAG
= WA_READ
,
205 WRITE_WATCHFLAG
= WA_WRITE
,
206 EXEC_WATCHFLAG
= WA_EXEC
,
207 AFTER_WATCHFLAG
= WA_TRAPAFTER
210 #else /* Irix method for watchpoints */
211 enum { READ_WATCHFLAG
= MA_READ
,
212 WRITE_WATCHFLAG
= MA_WRITE
,
213 EXEC_WATCHFLAG
= MA_EXEC
,
214 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
219 #ifdef HAVE_PR_SIGSET_T
220 typedef pr_sigset_t gdb_sigset_t
;
222 typedef sigset_t gdb_sigset_t
;
226 #ifdef HAVE_PR_SIGACTION64_T
227 typedef pr_sigaction64_t gdb_sigaction_t
;
229 typedef struct sigaction gdb_sigaction_t
;
233 #ifdef HAVE_PR_SIGINFO64_T
234 typedef pr_siginfo64_t gdb_siginfo_t
;
236 typedef struct siginfo gdb_siginfo_t
;
239 /* gdb_premptysysset */
241 #define gdb_premptysysset premptysysset
243 #define gdb_premptysysset premptyset
248 #define gdb_praddsysset praddsysset
250 #define gdb_praddsysset praddset
255 #define gdb_prdelsysset prdelsysset
257 #define gdb_prdelsysset prdelset
260 /* prissyssetmember */
261 #ifdef prissyssetmember
262 #define gdb_pr_issyssetmember prissyssetmember
264 #define gdb_pr_issyssetmember prismember
267 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
268 as intuitively descriptive as it could be, so we'll define
269 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
270 this writing, this feature is only found on AIX5 systems and
271 basically means that the set of syscalls is not fixed. I.e,
272 there's no nice table that one can #include to get all of the
273 syscall numbers. Instead, they're stored in /proc/PID/sysent
274 for each process. We are at least guaranteed that they won't
275 change over the lifetime of the process. But each process could
276 (in theory) have different syscall numbers.
278 #ifdef HAVE_PRSYSENT_T
279 #define DYNAMIC_SYSCALLS
284 /* =================== STRUCT PROCINFO "MODULE" =================== */
286 /* FIXME: this comment will soon be out of date W.R.T. threads. */
288 /* The procinfo struct is a wrapper to hold all the state information
289 concerning a /proc process. There should be exactly one procinfo
290 for each process, and since GDB currently can debug only one
291 process at a time, that means there should be only one procinfo.
292 All of the LWP's of a process can be accessed indirectly thru the
293 single process procinfo.
295 However, against the day when GDB may debug more than one process,
296 this data structure is kept in a list (which for now will hold no
297 more than one member), and many functions will have a pointer to a
298 procinfo as an argument.
300 There will be a separate procinfo structure for use by the (not yet
301 implemented) "info proc" command, so that we can print useful
302 information about any random process without interfering with the
303 inferior's procinfo information. */
306 /* format strings for /proc paths */
307 # ifndef CTL_PROC_NAME_FMT
308 # define MAIN_PROC_NAME_FMT "/proc/%d"
309 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
310 # define AS_PROC_NAME_FMT "/proc/%d/as"
311 # define MAP_PROC_NAME_FMT "/proc/%d/map"
312 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
313 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
315 /* the name of the proc status struct depends on the implementation */
316 typedef pstatus_t gdb_prstatus_t
;
317 typedef lwpstatus_t gdb_lwpstatus_t
;
318 #else /* ! NEW_PROC_API */
319 /* format strings for /proc paths */
320 # ifndef CTL_PROC_NAME_FMT
321 # define MAIN_PROC_NAME_FMT "/proc/%05d"
322 # define CTL_PROC_NAME_FMT "/proc/%05d"
323 # define AS_PROC_NAME_FMT "/proc/%05d"
324 # define MAP_PROC_NAME_FMT "/proc/%05d"
325 # define STATUS_PROC_NAME_FMT "/proc/%05d"
326 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
328 /* the name of the proc status struct depends on the implementation */
329 typedef prstatus_t gdb_prstatus_t
;
330 typedef prstatus_t gdb_lwpstatus_t
;
331 #endif /* NEW_PROC_API */
333 typedef struct procinfo
{
334 struct procinfo
*next
;
335 int pid
; /* Process ID */
336 int tid
; /* Thread/LWP id */
340 int ignore_next_sigstop
;
342 /* The following four fd fields may be identical, or may contain
343 several different fd's, depending on the version of /proc
344 (old ioctl or new read/write). */
346 int ctl_fd
; /* File descriptor for /proc control file */
348 * The next three file descriptors are actually only needed in the
349 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
350 * However, to avoid a bunch of #ifdefs in the code, we will use
351 * them uniformly by (in the case of the ioctl single-file-descriptor
352 * implementation) filling them with copies of the control fd.
354 int status_fd
; /* File descriptor for /proc status file */
355 int as_fd
; /* File descriptor for /proc as file */
357 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
359 fltset_t saved_fltset
; /* Saved traced hardware fault set */
360 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
361 gdb_sigset_t saved_sighold
; /* Saved held signal set */
362 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
363 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
365 gdb_prstatus_t prstatus
; /* Current process status info */
368 gdb_fpregset_t fpregset
; /* Current floating point registers */
371 #ifdef DYNAMIC_SYSCALLS
372 int num_syscalls
; /* Total number of syscalls */
373 char **syscall_names
; /* Syscall number to name map */
376 struct procinfo
*thread_list
;
378 int status_valid
: 1;
380 int fpregs_valid
: 1;
381 int threads_valid
: 1;
384 static char errmsg
[128]; /* shared error msg buffer */
386 /* Function prototypes for procinfo module: */
388 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
389 static procinfo
*find_procinfo (int pid
, int tid
);
390 static procinfo
*create_procinfo (int pid
, int tid
);
391 static void destroy_procinfo (procinfo
* p
);
392 static void do_destroy_procinfo_cleanup (void *);
393 static void dead_procinfo (procinfo
* p
, char *msg
, int killp
);
394 static int open_procinfo_files (procinfo
* p
, int which
);
395 static void close_procinfo_files (procinfo
* p
);
396 static int sysset_t_size (procinfo
*p
);
397 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
398 #ifdef DYNAMIC_SYSCALLS
399 static void load_syscalls (procinfo
*pi
);
400 static void free_syscalls (procinfo
*pi
);
401 static int find_syscall (procinfo
*pi
, char *name
);
402 #endif /* DYNAMIC_SYSCALLS */
404 /* The head of the procinfo list: */
405 static procinfo
* procinfo_list
;
408 * Function: find_procinfo
410 * Search the procinfo list.
412 * Returns: pointer to procinfo, or NULL if not found.
416 find_procinfo (int pid
, int tid
)
420 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
427 /* Don't check threads_valid. If we're updating the
428 thread_list, we want to find whatever threads are already
429 here. This means that in general it is the caller's
430 responsibility to check threads_valid and update before
431 calling find_procinfo, if the caller wants to find a new
434 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
443 * Function: find_procinfo_or_die
445 * Calls find_procinfo, but errors on failure.
449 find_procinfo_or_die (int pid
, int tid
)
451 procinfo
*pi
= find_procinfo (pid
, tid
);
456 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
459 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
464 /* open_with_retry() is a wrapper for open(). The appropriate
465 open() call is attempted; if unsuccessful, it will be retried as
466 many times as needed for the EAGAIN and EINTR conditions.
468 For other conditions, open_with_retry() will retry the open() a
469 limited number of times. In addition, a short sleep is imposed
470 prior to retrying the open(). The reason for this sleep is to give
471 the kernel a chance to catch up and create the file in question in
472 the event that GDB "wins" the race to open a file before the kernel
476 open_with_retry (const char *pathname
, int flags
)
478 int retries_remaining
, status
;
480 retries_remaining
= 2;
484 status
= open (pathname
, flags
);
486 if (status
>= 0 || retries_remaining
== 0)
488 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
499 * Function: open_procinfo_files
501 * Open the file descriptor for the process or LWP.
502 * ifdef NEW_PROC_API, we only open the control file descriptor;
503 * the others are opened lazily as needed.
504 * else (if not NEW_PROC_API), there is only one real
505 * file descriptor, but we keep multiple copies of it so that
506 * the code that uses them does not have to be #ifdef'd.
508 * Return: file descriptor, or zero for failure.
511 enum { FD_CTL
, FD_STATUS
, FD_AS
};
514 open_procinfo_files (procinfo
*pi
, int which
)
517 char tmp
[MAX_PROC_NAME_SIZE
];
522 * This function is getting ALMOST long enough to break up into several.
523 * Here is some rationale:
525 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
526 * There are several file descriptors that may need to be open
527 * for any given process or LWP. The ones we're intereted in are:
528 * - control (ctl) write-only change the state
529 * - status (status) read-only query the state
530 * - address space (as) read/write access memory
531 * - map (map) read-only virtual addr map
532 * Most of these are opened lazily as they are needed.
533 * The pathnames for the 'files' for an LWP look slightly
534 * different from those of a first-class process:
535 * Pathnames for a process (<proc-id>):
536 * /proc/<proc-id>/ctl
537 * /proc/<proc-id>/status
539 * /proc/<proc-id>/map
540 * Pathnames for an LWP (lwp-id):
541 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
542 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
543 * An LWP has no map or address space file descriptor, since
544 * the memory map and address space are shared by all LWPs.
546 * Everyone else (Solaris 2.5, Irix, OSF)
547 * There is only one file descriptor for each process or LWP.
548 * For convenience, we copy the same file descriptor into all
549 * three fields of the procinfo struct (ctl_fd, status_fd, and
550 * as_fd, see NEW_PROC_API above) so that code that uses them
551 * doesn't need any #ifdef's.
556 * Each LWP has an independent file descriptor, but these
557 * are not obtained via the 'open' system call like the rest:
558 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
559 * to the file descriptor of the parent process.
562 * These do not even have their own independent file descriptor.
563 * All operations are carried out on the file descriptor of the
564 * parent process. Therefore we just call open again for each
565 * thread, getting a new handle for the same 'file'.
570 * In this case, there are several different file descriptors that
571 * we might be asked to open. The control file descriptor will be
572 * opened early, but the others will be opened lazily as they are
576 strcpy (tmp
, pi
->pathname
);
577 switch (which
) { /* which file descriptor to open? */
580 strcat (tmp
, "/lwpctl");
582 strcat (tmp
, "/ctl");
583 fd
= open_with_retry (tmp
, O_WRONLY
);
590 return 0; /* there is no 'as' file descriptor for an lwp */
592 fd
= open_with_retry (tmp
, O_RDWR
);
599 strcat (tmp
, "/lwpstatus");
601 strcat (tmp
, "/status");
602 fd
= open_with_retry (tmp
, O_RDONLY
);
608 return 0; /* unknown file descriptor */
610 #else /* not NEW_PROC_API */
612 * In this case, there is only one file descriptor for each procinfo
613 * (ie. each process or LWP). In fact, only the file descriptor for
614 * the process can actually be opened by an 'open' system call.
615 * The ones for the LWPs have to be obtained thru an IOCTL call
616 * on the process's file descriptor.
618 * For convenience, we copy each procinfo's single file descriptor
619 * into all of the fields occupied by the several file descriptors
620 * of the NEW_PROC_API implementation. That way, the code that uses
621 * them can be written without ifdefs.
625 #ifdef PIOCTSTATUS /* OSF */
626 /* Only one FD; just open it. */
627 if ((fd
= open_with_retry (pi
->pathname
, O_RDWR
)) == 0)
629 #else /* Sol 2.5, Irix, other? */
630 if (pi
->tid
== 0) /* Master procinfo for the process */
632 fd
= open_with_retry (pi
->pathname
, O_RDWR
);
636 else /* LWP thread procinfo */
638 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
642 /* Find the procinfo for the entire process. */
643 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
646 /* Now obtain the file descriptor for the LWP. */
647 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
649 #else /* Irix, other? */
650 return 0; /* Don't know how to open threads */
651 #endif /* Sol 2.5 PIOCOPENLWP */
653 #endif /* OSF PIOCTSTATUS */
654 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
655 #endif /* NEW_PROC_API */
657 return 1; /* success */
661 * Function: create_procinfo
663 * Allocate a data structure and link it into the procinfo list.
664 * (First tries to find a pre-existing one (FIXME: why?)
666 * Return: pointer to new procinfo struct.
670 create_procinfo (int pid
, int tid
)
672 procinfo
*pi
, *parent
;
674 if ((pi
= find_procinfo (pid
, tid
)))
675 return pi
; /* Already exists, nothing to do. */
677 /* find parent before doing malloc, to save having to cleanup */
679 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
681 doesn't exist yet? */
683 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
684 memset (pi
, 0, sizeof (procinfo
));
688 #ifdef DYNAMIC_SYSCALLS
692 pi
->saved_entryset
= sysset_t_alloc (pi
);
693 pi
->saved_exitset
= sysset_t_alloc (pi
);
695 /* Chain into list. */
698 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
699 pi
->next
= procinfo_list
;
705 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
707 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
709 pi
->next
= parent
->thread_list
;
710 parent
->thread_list
= pi
;
716 * Function: close_procinfo_files
718 * Close all file descriptors associated with the procinfo
722 close_procinfo_files (procinfo
*pi
)
729 if (pi
->status_fd
> 0)
730 close (pi
->status_fd
);
732 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
736 * Function: destroy_procinfo
738 * Destructor function. Close, unlink and deallocate the object.
742 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
746 /* Step one: unlink the procinfo from its list */
750 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
753 ptr
->next
= pi
->next
;
757 /* Step two: close any open file descriptors */
758 close_procinfo_files (pi
);
760 /* Step three: free the memory. */
761 #ifdef DYNAMIC_SYSCALLS
764 xfree (pi
->saved_entryset
);
765 xfree (pi
->saved_exitset
);
770 destroy_procinfo (procinfo
*pi
)
774 if (pi
->tid
!= 0) /* destroy a thread procinfo */
776 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
777 destroy_one_procinfo (&tmp
->thread_list
, pi
);
779 else /* destroy a process procinfo and all its threads */
781 /* First destroy the children, if any; */
782 while (pi
->thread_list
!= NULL
)
783 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
784 /* Then destroy the parent. Genocide!!! */
785 destroy_one_procinfo (&procinfo_list
, pi
);
790 do_destroy_procinfo_cleanup (void *pi
)
792 destroy_procinfo (pi
);
795 enum { NOKILL
, KILL
};
798 * Function: dead_procinfo
800 * To be called on a non_recoverable error for a procinfo.
801 * Prints error messages, optionally sends a SIGKILL to the process,
802 * then destroys the data structure.
806 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
812 print_sys_errmsg (pi
->pathname
, errno
);
816 sprintf (procfile
, "process %d", pi
->pid
);
817 print_sys_errmsg (procfile
, errno
);
820 kill (pi
->pid
, SIGKILL
);
822 destroy_procinfo (pi
);
827 * Function: sysset_t_size
829 * Returns the (complete) size of a sysset_t struct. Normally, this
830 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
831 * size of sysset_t isn't known until runtime.
835 sysset_t_size (procinfo
* pi
)
837 #ifndef DYNAMIC_SYSCALLS
838 return sizeof (sysset_t
);
840 return sizeof (sysset_t
) - sizeof (uint64_t)
841 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
842 / (8 * sizeof (uint64_t)));
846 /* Function: sysset_t_alloc
848 Allocate and (partially) initialize a sysset_t struct. */
851 sysset_t_alloc (procinfo
* pi
)
854 int size
= sysset_t_size (pi
);
855 ret
= xmalloc (size
);
856 #ifdef DYNAMIC_SYSCALLS
857 ret
->pr_size
= (pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
858 / (8 * sizeof (uint64_t));
863 #ifdef DYNAMIC_SYSCALLS
865 /* Function: load_syscalls
867 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
868 pi->num_syscalls with the number of syscalls and pi->syscall_names
869 with the names. (Certain numbers may be skipped in which case the
870 names for these numbers will be left as NULL.) */
872 #define MAX_SYSCALL_NAME_LENGTH 256
873 #define MAX_SYSCALLS 65536
876 load_syscalls (procinfo
*pi
)
878 char pathname
[MAX_PROC_NAME_SIZE
];
881 prsyscall_t
*syscalls
;
882 int i
, size
, maxcall
;
884 pi
->num_syscalls
= 0;
885 pi
->syscall_names
= 0;
887 /* Open the file descriptor for the sysent file */
888 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
889 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
892 error ("load_syscalls: Can't open /proc/%d/sysent", pi
->pid
);
895 size
= sizeof header
- sizeof (prsyscall_t
);
896 if (read (sysent_fd
, &header
, size
) != size
)
898 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
901 if (header
.pr_nsyscalls
== 0)
903 error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi
->pid
);
906 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
907 syscalls
= xmalloc (size
);
909 if (read (sysent_fd
, syscalls
, size
) != size
)
912 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
915 /* Find maximum syscall number. This may not be the same as
916 pr_nsyscalls since that value refers to the number of entries
917 in the table. (Also, the docs indicate that some system
918 call numbers may be skipped.) */
920 maxcall
= syscalls
[0].pr_number
;
922 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
923 if (syscalls
[i
].pr_number
> maxcall
924 && syscalls
[i
].pr_nameoff
> 0
925 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
926 maxcall
= syscalls
[i
].pr_number
;
928 pi
->num_syscalls
= maxcall
+1;
929 pi
->syscall_names
= xmalloc (pi
->num_syscalls
* sizeof (char *));
931 for (i
= 0; i
< pi
->num_syscalls
; i
++)
932 pi
->syscall_names
[i
] = NULL
;
934 /* Read the syscall names in */
935 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
937 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
941 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
942 || syscalls
[i
].pr_number
< 0
943 || syscalls
[i
].pr_nameoff
<= 0
944 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
945 != (off_t
) syscalls
[i
].pr_nameoff
))
948 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
952 callnum
= syscalls
[i
].pr_number
;
954 if (pi
->syscall_names
[callnum
] != NULL
)
956 /* FIXME: Generate warning */
960 namebuf
[nread
-1] = '\0';
961 size
= strlen (namebuf
) + 1;
962 pi
->syscall_names
[callnum
] = xmalloc (size
);
963 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
964 pi
->syscall_names
[callnum
][size
-1] = '\0';
971 /* Function: free_syscalls
973 Free the space allocated for the syscall names from the procinfo
977 free_syscalls (procinfo
*pi
)
979 if (pi
->syscall_names
)
983 for (i
= 0; i
< pi
->num_syscalls
; i
++)
984 if (pi
->syscall_names
[i
] != NULL
)
985 xfree (pi
->syscall_names
[i
]);
987 xfree (pi
->syscall_names
);
988 pi
->syscall_names
= 0;
992 /* Function: find_syscall
994 Given a name, look up (and return) the corresponding syscall number.
995 If no match is found, return -1. */
998 find_syscall (procinfo
*pi
, char *name
)
1001 for (i
= 0; i
< pi
->num_syscalls
; i
++)
1003 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
1010 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1012 /* =================== /proc "MODULE" =================== */
1015 * This "module" is the interface layer between the /proc system API
1016 * and the gdb target vector functions. This layer consists of
1017 * access functions that encapsulate each of the basic operations
1018 * that we need to use from the /proc API.
1020 * The main motivation for this layer is to hide the fact that
1021 * there are two very different implementations of the /proc API.
1022 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1023 * functions, we do our best to hide them all in here.
1026 int proc_get_status (procinfo
* pi
);
1027 long proc_flags (procinfo
* pi
);
1028 int proc_why (procinfo
* pi
);
1029 int proc_what (procinfo
* pi
);
1030 int proc_set_run_on_last_close (procinfo
* pi
);
1031 int proc_unset_run_on_last_close (procinfo
* pi
);
1032 int proc_set_inherit_on_fork (procinfo
* pi
);
1033 int proc_unset_inherit_on_fork (procinfo
* pi
);
1034 int proc_set_async (procinfo
* pi
);
1035 int proc_unset_async (procinfo
* pi
);
1036 int proc_stop_process (procinfo
* pi
);
1037 int proc_trace_signal (procinfo
* pi
, int signo
);
1038 int proc_ignore_signal (procinfo
* pi
, int signo
);
1039 int proc_clear_current_fault (procinfo
* pi
);
1040 int proc_set_current_signal (procinfo
* pi
, int signo
);
1041 int proc_clear_current_signal (procinfo
* pi
);
1042 int proc_set_gregs (procinfo
* pi
);
1043 int proc_set_fpregs (procinfo
* pi
);
1044 int proc_wait_for_stop (procinfo
* pi
);
1045 int proc_run_process (procinfo
* pi
, int step
, int signo
);
1046 int proc_kill (procinfo
* pi
, int signo
);
1047 int proc_parent_pid (procinfo
* pi
);
1048 int proc_get_nthreads (procinfo
* pi
);
1049 int proc_get_current_thread (procinfo
* pi
);
1050 int proc_set_held_signals (procinfo
* pi
, gdb_sigset_t
* sighold
);
1051 int proc_set_traced_sysexit (procinfo
* pi
, sysset_t
* sysset
);
1052 int proc_set_traced_sysentry (procinfo
* pi
, sysset_t
* sysset
);
1053 int proc_set_traced_faults (procinfo
* pi
, fltset_t
* fltset
);
1054 int proc_set_traced_signals (procinfo
* pi
, gdb_sigset_t
* sigset
);
1056 int proc_update_threads (procinfo
* pi
);
1057 int proc_iterate_over_threads (procinfo
* pi
,
1058 int (*func
) (procinfo
*, procinfo
*, void *),
1061 gdb_gregset_t
*proc_get_gregs (procinfo
* pi
);
1062 gdb_fpregset_t
*proc_get_fpregs (procinfo
* pi
);
1063 sysset_t
*proc_get_traced_sysexit (procinfo
* pi
, sysset_t
* save
);
1064 sysset_t
*proc_get_traced_sysentry (procinfo
* pi
, sysset_t
* save
);
1065 fltset_t
*proc_get_traced_faults (procinfo
* pi
, fltset_t
* save
);
1066 gdb_sigset_t
*proc_get_traced_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1067 gdb_sigset_t
*proc_get_held_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1068 gdb_sigset_t
*proc_get_pending_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1069 gdb_sigaction_t
*proc_get_signal_actions (procinfo
* pi
, gdb_sigaction_t
*save
);
1071 void proc_warn (procinfo
* pi
, char *func
, int line
);
1072 void proc_error (procinfo
* pi
, char *func
, int line
);
1075 proc_warn (procinfo
*pi
, char *func
, int line
)
1077 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1078 print_sys_errmsg (errmsg
, errno
);
1082 proc_error (procinfo
*pi
, char *func
, int line
)
1084 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1085 perror_with_name (errmsg
);
1089 * Function: proc_get_status
1091 * Updates the status struct in the procinfo.
1092 * There is a 'valid' flag, to let other functions know when
1093 * this function needs to be called (so the status is only
1094 * read when it is needed). The status file descriptor is
1095 * also only opened when it is needed.
1097 * Return: non-zero for success, zero for failure.
1101 proc_get_status (procinfo
*pi
)
1103 /* Status file descriptor is opened "lazily" */
1104 if (pi
->status_fd
== 0 &&
1105 open_procinfo_files (pi
, FD_STATUS
) == 0)
1107 pi
->status_valid
= 0;
1112 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1113 pi
->status_valid
= 0; /* fail */
1116 /* Sigh... I have to read a different data structure,
1117 depending on whether this is a main process or an LWP. */
1119 pi
->status_valid
= (read (pi
->status_fd
,
1120 (char *) &pi
->prstatus
.pr_lwp
,
1121 sizeof (lwpstatus_t
))
1122 == sizeof (lwpstatus_t
));
1125 pi
->status_valid
= (read (pi
->status_fd
,
1126 (char *) &pi
->prstatus
,
1127 sizeof (gdb_prstatus_t
))
1128 == sizeof (gdb_prstatus_t
));
1129 #if 0 /*def UNIXWARE*/
1130 if (pi
->status_valid
&&
1131 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
1132 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
1133 /* Unixware peculiarity -- read the damn thing again! */
1134 pi
->status_valid
= (read (pi
->status_fd
,
1135 (char *) &pi
->prstatus
,
1136 sizeof (gdb_prstatus_t
))
1137 == sizeof (gdb_prstatus_t
));
1138 #endif /* UNIXWARE */
1141 #else /* ioctl method */
1142 #ifdef PIOCTSTATUS /* osf */
1143 if (pi
->tid
== 0) /* main process */
1145 /* Just read the danged status. Now isn't that simple? */
1147 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1154 tid_t pr_error_thread
;
1155 struct prstatus status
;
1158 thread_status
.pr_count
= 1;
1159 thread_status
.status
.pr_tid
= pi
->tid
;
1160 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1163 memcpy (&pi
->prstatus
, &thread_status
.status
,
1164 sizeof (pi
->prstatus
));
1165 pi
->status_valid
= 1;
1169 /* Just read the danged status. Now isn't that simple? */
1170 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1174 if (pi
->status_valid
)
1176 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1179 proc_get_current_thread (pi
));
1182 /* The status struct includes general regs, so mark them valid too */
1183 pi
->gregs_valid
= pi
->status_valid
;
1185 /* In the read/write multiple-fd model,
1186 the status struct includes the fp regs too, so mark them valid too */
1187 pi
->fpregs_valid
= pi
->status_valid
;
1189 return pi
->status_valid
; /* True if success, false if failure. */
1193 * Function: proc_flags
1195 * returns the process flags (pr_flags field).
1199 proc_flags (procinfo
*pi
)
1201 if (!pi
->status_valid
)
1202 if (!proc_get_status (pi
))
1203 return 0; /* FIXME: not a good failure value (but what is?) */
1207 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1208 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1209 The two sets of flags don't overlap. */
1210 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
1212 return pi
->prstatus
.pr_lwp
.pr_flags
;
1215 return pi
->prstatus
.pr_flags
;
1220 * Function: proc_why
1222 * returns the pr_why field (why the process stopped).
1226 proc_why (procinfo
*pi
)
1228 if (!pi
->status_valid
)
1229 if (!proc_get_status (pi
))
1230 return 0; /* FIXME: not a good failure value (but what is?) */
1233 return pi
->prstatus
.pr_lwp
.pr_why
;
1235 return pi
->prstatus
.pr_why
;
1240 * Function: proc_what
1242 * returns the pr_what field (details of why the process stopped).
1246 proc_what (procinfo
*pi
)
1248 if (!pi
->status_valid
)
1249 if (!proc_get_status (pi
))
1250 return 0; /* FIXME: not a good failure value (but what is?) */
1253 return pi
->prstatus
.pr_lwp
.pr_what
;
1255 return pi
->prstatus
.pr_what
;
1259 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1261 * Function: proc_nsysarg
1263 * returns the pr_nsysarg field (number of args to the current syscall).
1267 proc_nsysarg (procinfo
*pi
)
1269 if (!pi
->status_valid
)
1270 if (!proc_get_status (pi
))
1274 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1276 return pi
->prstatus
.pr_nsysarg
;
1281 * Function: proc_sysargs
1283 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1287 proc_sysargs (procinfo
*pi
)
1289 if (!pi
->status_valid
)
1290 if (!proc_get_status (pi
))
1294 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1296 return (long *) &pi
->prstatus
.pr_sysarg
;
1301 * Function: proc_syscall
1303 * returns the pr_syscall field (id of current syscall if we are in one).
1307 proc_syscall (procinfo
*pi
)
1309 if (!pi
->status_valid
)
1310 if (!proc_get_status (pi
))
1314 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1316 return pi
->prstatus
.pr_syscall
;
1319 #endif /* PIOCSSPCACT */
1322 * Function: proc_cursig:
1324 * returns the pr_cursig field (current signal).
1328 proc_cursig (struct procinfo
*pi
)
1330 if (!pi
->status_valid
)
1331 if (!proc_get_status (pi
))
1332 return 0; /* FIXME: not a good failure value (but what is?) */
1335 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1337 return pi
->prstatus
.pr_cursig
;
1342 * Function: proc_modify_flag
1344 * === I appologize for the messiness of this function.
1345 * === This is an area where the different versions of
1346 * === /proc are more inconsistent than usual. MVS
1348 * Set or reset any of the following process flags:
1349 * PR_FORK -- forked child will inherit trace flags
1350 * PR_RLC -- traced process runs when last /proc file closed.
1351 * PR_KLC -- traced process is killed when last /proc file closed.
1352 * PR_ASYNC -- LWP's get to run/stop independently.
1354 * There are three methods for doing this function:
1355 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1357 * 2) Middle: PIOCSET/PIOCRESET
1359 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1362 * Note: Irix does not define PR_ASYNC.
1363 * Note: OSF does not define PR_KLC.
1364 * Note: OSF is the only one that can ONLY use the oldest method.
1367 * pi -- the procinfo
1368 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1369 * mode -- 1 for set, 0 for reset.
1371 * Returns non-zero for success, zero for failure.
1374 enum { FLAG_RESET
, FLAG_SET
};
1377 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1379 long win
= 0; /* default to fail */
1382 * These operations affect the process as a whole, and applying
1383 * them to an individual LWP has the same meaning as applying them
1384 * to the main process. Therefore, if we're ever called with a
1385 * pointer to an LWP's procinfo, let's substitute the process's
1386 * procinfo and avoid opening the LWP's file descriptor
1391 pi
= find_procinfo_or_die (pi
->pid
, 0);
1393 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1394 /* First normalize the PCUNSET/PCRESET command opcode
1395 (which for no obvious reason has a different definition
1396 from one operating system to the next...) */
1398 #define GDBRESET PCUNSET
1401 #define GDBRESET PCRESET
1405 procfs_ctl_t arg
[2];
1407 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1409 else /* Reset the flag */
1413 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1416 #ifdef PIOCSET /* Irix/Sol5 method */
1417 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1419 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1421 else /* Reset the flag */
1423 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1427 #ifdef PIOCSRLC /* Oldest method: OSF */
1430 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1432 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1434 else /* Clear run-on-last-close */
1436 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1440 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1442 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1444 else /* Clear inherit-on-fork */
1446 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1450 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1457 /* The above operation renders the procinfo's cached pstatus obsolete. */
1458 pi
->status_valid
= 0;
1461 warning ("procfs: modify_flag failed to turn %s %s",
1462 flag
== PR_FORK
? "PR_FORK" :
1463 flag
== PR_RLC
? "PR_RLC" :
1465 flag
== PR_ASYNC
? "PR_ASYNC" :
1468 flag
== PR_KLC
? "PR_KLC" :
1471 mode
== FLAG_RESET
? "off" : "on");
1477 * Function: proc_set_run_on_last_close
1479 * Set the run_on_last_close flag.
1480 * Process with all threads will become runnable
1481 * when debugger closes all /proc fds.
1483 * Returns non-zero for success, zero for failure.
1487 proc_set_run_on_last_close (procinfo
*pi
)
1489 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1493 * Function: proc_unset_run_on_last_close
1495 * Reset the run_on_last_close flag.
1496 * Process will NOT become runnable
1497 * when debugger closes its file handles.
1499 * Returns non-zero for success, zero for failure.
1503 proc_unset_run_on_last_close (procinfo
*pi
)
1505 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1510 * Function: proc_set_kill_on_last_close
1512 * Set the kill_on_last_close flag.
1513 * Process with all threads will be killed when debugger
1514 * closes all /proc fds (or debugger exits or dies).
1516 * Returns non-zero for success, zero for failure.
1520 proc_set_kill_on_last_close (procinfo
*pi
)
1522 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1526 * Function: proc_unset_kill_on_last_close
1528 * Reset the kill_on_last_close flag.
1529 * Process will NOT be killed when debugger
1530 * closes its file handles (or exits or dies).
1532 * Returns non-zero for success, zero for failure.
1536 proc_unset_kill_on_last_close (procinfo
*pi
)
1538 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1543 * Function: proc_set_inherit_on_fork
1545 * Set inherit_on_fork flag.
1546 * If the process forks a child while we are registered for events
1547 * in the parent, then we will also recieve events from the child.
1549 * Returns non-zero for success, zero for failure.
1553 proc_set_inherit_on_fork (procinfo
*pi
)
1555 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1559 * Function: proc_unset_inherit_on_fork
1561 * Reset inherit_on_fork flag.
1562 * If the process forks a child while we are registered for events
1563 * in the parent, then we will NOT recieve events from the child.
1565 * Returns non-zero for success, zero for failure.
1569 proc_unset_inherit_on_fork (procinfo
*pi
)
1571 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1576 * Function: proc_set_async
1578 * Set PR_ASYNC flag.
1579 * If one LWP stops because of a debug event (signal etc.),
1580 * the remaining LWPs will continue to run.
1582 * Returns non-zero for success, zero for failure.
1586 proc_set_async (procinfo
*pi
)
1588 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1592 * Function: proc_unset_async
1594 * Reset PR_ASYNC flag.
1595 * If one LWP stops because of a debug event (signal etc.),
1596 * then all other LWPs will stop as well.
1598 * Returns non-zero for success, zero for failure.
1602 proc_unset_async (procinfo
*pi
)
1604 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1606 #endif /* PR_ASYNC */
1609 * Function: proc_stop_process
1611 * Request the process/LWP to stop. Does not wait.
1612 * Returns non-zero for success, zero for failure.
1616 proc_stop_process (procinfo
*pi
)
1621 * We might conceivably apply this operation to an LWP, and
1622 * the LWP's ctl file descriptor might not be open.
1625 if (pi
->ctl_fd
== 0 &&
1626 open_procinfo_files (pi
, FD_CTL
) == 0)
1631 procfs_ctl_t cmd
= PCSTOP
;
1632 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1633 #else /* ioctl method */
1634 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1635 /* Note: the call also reads the prstatus. */
1638 pi
->status_valid
= 1;
1639 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1642 proc_get_current_thread (pi
));
1651 * Function: proc_wait_for_stop
1653 * Wait for the process or LWP to stop (block until it does).
1654 * Returns non-zero for success, zero for failure.
1658 proc_wait_for_stop (procinfo
*pi
)
1663 * We should never have to apply this operation to any procinfo
1664 * except the one for the main process. If that ever changes
1665 * for any reason, then take out the following clause and
1666 * replace it with one that makes sure the ctl_fd is open.
1670 pi
= find_procinfo_or_die (pi
->pid
, 0);
1674 procfs_ctl_t cmd
= PCWSTOP
;
1675 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1676 /* We been runnin' and we stopped -- need to update status. */
1677 pi
->status_valid
= 0;
1679 #else /* ioctl method */
1680 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1681 /* Above call also refreshes the prstatus. */
1684 pi
->status_valid
= 1;
1685 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1688 proc_get_current_thread (pi
));
1696 * Function: proc_run_process
1698 * Make the process or LWP runnable.
1699 * Options (not all are implemented):
1701 * - clear current fault
1702 * - clear current signal
1703 * - abort the current system call
1704 * - stop as soon as finished with system call
1705 * - (ioctl): set traced signal set
1706 * - (ioctl): set held signal set
1707 * - (ioctl): set traced fault set
1708 * - (ioctl): set start pc (vaddr)
1709 * Always clear the current fault.
1710 * Clear the current signal if 'signo' is zero.
1713 * pi the process or LWP to operate on.
1714 * step if true, set the process or LWP to trap after one instr.
1715 * signo if zero, clear the current signal if any.
1716 * if non-zero, set the current signal to this one.
1718 * Returns non-zero for success, zero for failure.
1722 proc_run_process (procinfo
*pi
, int step
, int signo
)
1728 * We will probably have to apply this operation to individual threads,
1729 * so make sure the control file descriptor is open.
1732 if (pi
->ctl_fd
== 0 &&
1733 open_procinfo_files (pi
, FD_CTL
) == 0)
1738 runflags
= PRCFAULT
; /* always clear current fault */
1743 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1744 proc_set_current_signal (pi
, signo
);
1748 procfs_ctl_t cmd
[2];
1752 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1754 #else /* ioctl method */
1758 memset (&prrun
, 0, sizeof (prrun
));
1759 prrun
.pr_flags
= runflags
;
1760 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1768 * Function: proc_set_traced_signals
1770 * Register to trace signals in the process or LWP.
1771 * Returns non-zero for success, zero for failure.
1775 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1780 * We should never have to apply this operation to any procinfo
1781 * except the one for the main process. If that ever changes
1782 * for any reason, then take out the following clause and
1783 * replace it with one that makes sure the ctl_fd is open.
1787 pi
= find_procinfo_or_die (pi
->pid
, 0);
1793 /* Use char array to avoid alignment issues. */
1794 char sigset
[sizeof (gdb_sigset_t
)];
1798 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1800 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1802 #else /* ioctl method */
1803 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1805 /* The above operation renders the procinfo's cached pstatus obsolete. */
1806 pi
->status_valid
= 0;
1809 warning ("procfs: set_traced_signals failed");
1814 * Function: proc_set_traced_faults
1816 * Register to trace hardware faults in the process or LWP.
1817 * Returns non-zero for success, zero for failure.
1821 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1826 * We should never have to apply this operation to any procinfo
1827 * except the one for the main process. If that ever changes
1828 * for any reason, then take out the following clause and
1829 * replace it with one that makes sure the ctl_fd is open.
1833 pi
= find_procinfo_or_die (pi
->pid
, 0);
1839 /* Use char array to avoid alignment issues. */
1840 char fltset
[sizeof (fltset_t
)];
1844 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1846 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1848 #else /* ioctl method */
1849 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1851 /* The above operation renders the procinfo's cached pstatus obsolete. */
1852 pi
->status_valid
= 0;
1858 * Function: proc_set_traced_sysentry
1860 * Register to trace entry to system calls in the process or LWP.
1861 * Returns non-zero for success, zero for failure.
1865 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1870 * We should never have to apply this operation to any procinfo
1871 * except the one for the main process. If that ever changes
1872 * for any reason, then take out the following clause and
1873 * replace it with one that makes sure the ctl_fd is open.
1877 pi
= find_procinfo_or_die (pi
->pid
, 0);
1881 struct gdb_proc_ctl_pcsentry
{
1883 /* Use char array to avoid alignment issues. */
1884 char sysset
[sizeof (sysset_t
)];
1886 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1888 + sysset_t_size (pi
);
1890 argp
= xmalloc (argp_size
);
1892 argp
->cmd
= PCSENTRY
;
1893 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1895 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1898 #else /* ioctl method */
1899 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1901 /* The above operation renders the procinfo's cached pstatus obsolete. */
1902 pi
->status_valid
= 0;
1908 * Function: proc_set_traced_sysexit
1910 * Register to trace exit from system calls in the process or LWP.
1911 * Returns non-zero for success, zero for failure.
1915 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1920 * We should never have to apply this operation to any procinfo
1921 * except the one for the main process. If that ever changes
1922 * for any reason, then take out the following clause and
1923 * replace it with one that makes sure the ctl_fd is open.
1927 pi
= find_procinfo_or_die (pi
->pid
, 0);
1931 struct gdb_proc_ctl_pcsexit
{
1933 /* Use char array to avoid alignment issues. */
1934 char sysset
[sizeof (sysset_t
)];
1936 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1938 + sysset_t_size (pi
);
1940 argp
= xmalloc (argp_size
);
1942 argp
->cmd
= PCSEXIT
;
1943 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1945 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1948 #else /* ioctl method */
1949 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1951 /* The above operation renders the procinfo's cached pstatus obsolete. */
1952 pi
->status_valid
= 0;
1958 * Function: proc_set_held_signals
1960 * Specify the set of blocked / held signals in the process or LWP.
1961 * Returns non-zero for success, zero for failure.
1965 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1970 * We should never have to apply this operation to any procinfo
1971 * except the one for the main process. If that ever changes
1972 * for any reason, then take out the following clause and
1973 * replace it with one that makes sure the ctl_fd is open.
1977 pi
= find_procinfo_or_die (pi
->pid
, 0);
1983 /* Use char array to avoid alignment issues. */
1984 char hold
[sizeof (gdb_sigset_t
)];
1988 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1989 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1992 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1994 /* The above operation renders the procinfo's cached pstatus obsolete. */
1995 pi
->status_valid
= 0;
2001 * Function: proc_get_pending_signals
2003 * returns the set of signals that are pending in the process or LWP.
2004 * Will also copy the sigset if 'save' is non-zero.
2008 proc_get_pending_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2010 gdb_sigset_t
*ret
= NULL
;
2013 * We should never have to apply this operation to any procinfo
2014 * except the one for the main process. If that ever changes
2015 * for any reason, then take out the following clause and
2016 * replace it with one that makes sure the ctl_fd is open.
2020 pi
= find_procinfo_or_die (pi
->pid
, 0);
2022 if (!pi
->status_valid
)
2023 if (!proc_get_status (pi
))
2027 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
2029 ret
= &pi
->prstatus
.pr_sigpend
;
2032 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2038 * Function: proc_get_signal_actions
2040 * returns the set of signal actions.
2041 * Will also copy the sigactionset if 'save' is non-zero.
2045 proc_get_signal_actions (procinfo
*pi
, gdb_sigaction_t
*save
)
2047 gdb_sigaction_t
*ret
= NULL
;
2050 * We should never have to apply this operation to any procinfo
2051 * except the one for the main process. If that ever changes
2052 * for any reason, then take out the following clause and
2053 * replace it with one that makes sure the ctl_fd is open.
2057 pi
= find_procinfo_or_die (pi
->pid
, 0);
2059 if (!pi
->status_valid
)
2060 if (!proc_get_status (pi
))
2064 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
2066 ret
= &pi
->prstatus
.pr_action
;
2069 memcpy (save
, ret
, sizeof (gdb_sigaction_t
));
2075 * Function: proc_get_held_signals
2077 * returns the set of signals that are held / blocked.
2078 * Will also copy the sigset if 'save' is non-zero.
2082 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2084 gdb_sigset_t
*ret
= NULL
;
2087 * We should never have to apply this operation to any procinfo
2088 * except the one for the main process. If that ever changes
2089 * for any reason, then take out the following clause and
2090 * replace it with one that makes sure the ctl_fd is open.
2094 pi
= find_procinfo_or_die (pi
->pid
, 0);
2097 if (!pi
->status_valid
)
2098 if (!proc_get_status (pi
))
2102 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
2104 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
2105 #endif /* UNIXWARE */
2106 #else /* not NEW_PROC_API */
2108 static gdb_sigset_t sigheld
;
2110 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
2113 #endif /* NEW_PROC_API */
2115 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2121 * Function: proc_get_traced_signals
2123 * returns the set of signals that are traced / debugged.
2124 * Will also copy the sigset if 'save' is non-zero.
2128 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2130 gdb_sigset_t
*ret
= NULL
;
2133 * We should never have to apply this operation to any procinfo
2134 * except the one for the main process. If that ever changes
2135 * for any reason, then take out the following clause and
2136 * replace it with one that makes sure the ctl_fd is open.
2140 pi
= find_procinfo_or_die (pi
->pid
, 0);
2143 if (!pi
->status_valid
)
2144 if (!proc_get_status (pi
))
2147 ret
= &pi
->prstatus
.pr_sigtrace
;
2150 static gdb_sigset_t sigtrace
;
2152 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
2157 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2163 * Function: proc_trace_signal
2165 * Add 'signo' to the set of signals that are traced.
2166 * Returns non-zero for success, zero for failure.
2170 proc_trace_signal (procinfo
*pi
, int signo
)
2175 * We should never have to apply this operation to any procinfo
2176 * except the one for the main process. If that ever changes
2177 * for any reason, then take out the following clause and
2178 * replace it with one that makes sure the ctl_fd is open.
2182 pi
= find_procinfo_or_die (pi
->pid
, 0);
2186 if (proc_get_traced_signals (pi
, &temp
))
2188 praddset (&temp
, signo
);
2189 return proc_set_traced_signals (pi
, &temp
);
2193 return 0; /* failure */
2197 * Function: proc_ignore_signal
2199 * Remove 'signo' from the set of signals that are traced.
2200 * Returns non-zero for success, zero for failure.
2204 proc_ignore_signal (procinfo
*pi
, int signo
)
2209 * We should never have to apply this operation to any procinfo
2210 * except the one for the main process. If that ever changes
2211 * for any reason, then take out the following clause and
2212 * replace it with one that makes sure the ctl_fd is open.
2216 pi
= find_procinfo_or_die (pi
->pid
, 0);
2220 if (proc_get_traced_signals (pi
, &temp
))
2222 prdelset (&temp
, signo
);
2223 return proc_set_traced_signals (pi
, &temp
);
2227 return 0; /* failure */
2231 * Function: proc_get_traced_faults
2233 * returns the set of hardware faults that are traced /debugged.
2234 * Will also copy the faultset if 'save' is non-zero.
2238 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
2240 fltset_t
*ret
= NULL
;
2243 * We should never have to apply this operation to any procinfo
2244 * except the one for the main process. If that ever changes
2245 * for any reason, then take out the following clause and
2246 * replace it with one that makes sure the ctl_fd is open.
2250 pi
= find_procinfo_or_die (pi
->pid
, 0);
2253 if (!pi
->status_valid
)
2254 if (!proc_get_status (pi
))
2257 ret
= &pi
->prstatus
.pr_flttrace
;
2260 static fltset_t flttrace
;
2262 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2267 memcpy (save
, ret
, sizeof (fltset_t
));
2273 * Function: proc_get_traced_sysentry
2275 * returns the set of syscalls that are traced /debugged on entry.
2276 * Will also copy the syscall set if 'save' is non-zero.
2280 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
2282 sysset_t
*ret
= NULL
;
2285 * We should never have to apply this operation to any procinfo
2286 * except the one for the main process. If that ever changes
2287 * for any reason, then take out the following clause and
2288 * replace it with one that makes sure the ctl_fd is open.
2292 pi
= find_procinfo_or_die (pi
->pid
, 0);
2295 if (!pi
->status_valid
)
2296 if (!proc_get_status (pi
))
2299 #ifndef DYNAMIC_SYSCALLS
2300 ret
= &pi
->prstatus
.pr_sysentry
;
2301 #else /* DYNAMIC_SYSCALLS */
2303 static sysset_t
*sysentry
;
2307 sysentry
= sysset_t_alloc (pi
);
2309 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2311 if (pi
->prstatus
.pr_sysentry_offset
== 0)
2313 gdb_premptysysset (sysentry
);
2319 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
2321 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
2323 size
= sysset_t_size (pi
);
2324 gdb_premptysysset (sysentry
);
2325 rsize
= read (pi
->status_fd
, sysentry
, size
);
2330 #endif /* DYNAMIC_SYSCALLS */
2331 #else /* !NEW_PROC_API */
2333 static sysset_t sysentry
;
2335 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2338 #endif /* NEW_PROC_API */
2340 memcpy (save
, ret
, sysset_t_size (pi
));
2346 * Function: proc_get_traced_sysexit
2348 * returns the set of syscalls that are traced /debugged on exit.
2349 * Will also copy the syscall set if 'save' is non-zero.
2353 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
2355 sysset_t
* ret
= NULL
;
2358 * We should never have to apply this operation to any procinfo
2359 * except the one for the main process. If that ever changes
2360 * for any reason, then take out the following clause and
2361 * replace it with one that makes sure the ctl_fd is open.
2365 pi
= find_procinfo_or_die (pi
->pid
, 0);
2368 if (!pi
->status_valid
)
2369 if (!proc_get_status (pi
))
2372 #ifndef DYNAMIC_SYSCALLS
2373 ret
= &pi
->prstatus
.pr_sysexit
;
2374 #else /* DYNAMIC_SYSCALLS */
2376 static sysset_t
*sysexit
;
2380 sysexit
= sysset_t_alloc (pi
);
2382 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2384 if (pi
->prstatus
.pr_sysexit_offset
== 0)
2386 gdb_premptysysset (sysexit
);
2392 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
, SEEK_SET
)
2393 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2395 size
= sysset_t_size (pi
);
2396 gdb_premptysysset (sysexit
);
2397 rsize
= read (pi
->status_fd
, sysexit
, size
);
2402 #endif /* DYNAMIC_SYSCALLS */
2405 static sysset_t sysexit
;
2407 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2412 memcpy (save
, ret
, sysset_t_size (pi
));
2418 * Function: proc_clear_current_fault
2420 * The current fault (if any) is cleared; the associated signal
2421 * will not be sent to the process or LWP when it resumes.
2422 * Returns non-zero for success, zero for failure.
2426 proc_clear_current_fault (procinfo
*pi
)
2431 * We should never have to apply this operation to any procinfo
2432 * except the one for the main process. If that ever changes
2433 * for any reason, then take out the following clause and
2434 * replace it with one that makes sure the ctl_fd is open.
2438 pi
= find_procinfo_or_die (pi
->pid
, 0);
2442 procfs_ctl_t cmd
= PCCFAULT
;
2443 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2446 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2453 * Function: proc_set_current_signal
2455 * Set the "current signal" that will be delivered next to the process.
2456 * NOTE: semantics are different from those of KILL.
2457 * This signal will be delivered to the process or LWP
2458 * immediately when it is resumed (even if the signal is held/blocked);
2459 * it will NOT immediately cause another event of interest, and will NOT
2460 * first trap back to the debugger.
2462 * Returns non-zero for success, zero for failure.
2466 proc_set_current_signal (procinfo
*pi
, int signo
)
2471 /* Use char array to avoid alignment issues. */
2472 char sinfo
[sizeof (gdb_siginfo_t
)];
2474 gdb_siginfo_t
*mysinfo
;
2477 * We should never have to apply this operation to any procinfo
2478 * except the one for the main process. If that ever changes
2479 * for any reason, then take out the following clause and
2480 * replace it with one that makes sure the ctl_fd is open.
2484 pi
= find_procinfo_or_die (pi
->pid
, 0);
2486 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2487 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2488 * receives a PIOCSSIG with a signal identical to the current signal,
2489 * it messes up the current signal. Work around the kernel bug.
2492 signo
== proc_cursig (pi
))
2493 return 1; /* I assume this is a success? */
2496 /* The pointer is just a type alias. */
2497 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2498 mysinfo
->si_signo
= signo
;
2499 mysinfo
->si_code
= 0;
2500 mysinfo
->si_pid
= getpid (); /* ?why? */
2501 mysinfo
->si_uid
= getuid (); /* ?why? */
2505 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2507 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2514 * Function: proc_clear_current_signal
2516 * The current signal (if any) is cleared, and
2517 * is not sent to the process or LWP when it resumes.
2518 * Returns non-zero for success, zero for failure.
2522 proc_clear_current_signal (procinfo
*pi
)
2527 * We should never have to apply this operation to any procinfo
2528 * except the one for the main process. If that ever changes
2529 * for any reason, then take out the following clause and
2530 * replace it with one that makes sure the ctl_fd is open.
2534 pi
= find_procinfo_or_die (pi
->pid
, 0);
2540 /* Use char array to avoid alignment issues. */
2541 char sinfo
[sizeof (gdb_siginfo_t
)];
2543 gdb_siginfo_t
*mysinfo
;
2546 /* The pointer is just a type alias. */
2547 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2548 mysinfo
->si_signo
= 0;
2549 mysinfo
->si_code
= 0;
2550 mysinfo
->si_errno
= 0;
2551 mysinfo
->si_pid
= getpid (); /* ?why? */
2552 mysinfo
->si_uid
= getuid (); /* ?why? */
2554 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2557 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2564 * Function: proc_get_gregs
2566 * Get the general registers for the process or LWP.
2567 * Returns non-zero for success, zero for failure.
2571 proc_get_gregs (procinfo
*pi
)
2573 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2574 if (!proc_get_status (pi
))
2578 * OK, sorry about the ifdef's.
2579 * There's three cases instead of two, because
2580 * in this instance Unixware and Solaris/RW differ.
2584 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2585 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2586 #else /* not Unixware */
2587 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2588 #endif /* Unixware */
2589 #else /* not NEW_PROC_API */
2590 return &pi
->prstatus
.pr_reg
;
2591 #endif /* NEW_PROC_API */
2595 * Function: proc_get_fpregs
2597 * Get the floating point registers for the process or LWP.
2598 * Returns non-zero for success, zero for failure.
2602 proc_get_fpregs (procinfo
*pi
)
2605 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2606 if (!proc_get_status (pi
))
2609 #ifdef UNIXWARE /* a true architecture dependency */
2610 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2612 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2613 #endif /* Unixware */
2615 #else /* not NEW_PROC_API */
2616 if (pi
->fpregs_valid
)
2617 return &pi
->fpregset
; /* already got 'em */
2620 if (pi
->ctl_fd
== 0 &&
2621 open_procinfo_files (pi
, FD_CTL
) == 0)
2630 tid_t pr_error_thread
;
2631 tfpregset_t thread_1
;
2634 thread_fpregs
.pr_count
= 1;
2635 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2638 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2640 pi
->fpregs_valid
= 1;
2641 return &pi
->fpregset
; /* got 'em now! */
2643 else if (pi
->tid
!= 0 &&
2644 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2646 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2647 sizeof (pi
->fpregset
));
2648 pi
->fpregs_valid
= 1;
2649 return &pi
->fpregset
; /* got 'em now! */
2656 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2658 pi
->fpregs_valid
= 1;
2659 return &pi
->fpregset
; /* got 'em now! */
2672 * Function: proc_set_gregs
2674 * Write the general registers back to the process or LWP.
2675 * Returns non-zero for success, zero for failure.
2679 proc_set_gregs (procinfo
*pi
)
2681 gdb_gregset_t
*gregs
;
2684 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2685 return 0; /* get_regs has already warned */
2687 if (pi
->ctl_fd
== 0 &&
2688 open_procinfo_files (pi
, FD_CTL
) == 0)
2697 /* Use char array to avoid alignment issues. */
2698 char gregs
[sizeof (gdb_gregset_t
)];
2702 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2703 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2705 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2709 /* Policy: writing the regs invalidates our cache. */
2710 pi
->gregs_valid
= 0;
2715 * Function: proc_set_fpregs
2717 * Modify the floating point register set of the process or LWP.
2718 * Returns non-zero for success, zero for failure.
2722 proc_set_fpregs (procinfo
*pi
)
2724 gdb_fpregset_t
*fpregs
;
2727 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2728 return 0; /* get_fpregs has already warned */
2730 if (pi
->ctl_fd
== 0 &&
2731 open_procinfo_files (pi
, FD_CTL
) == 0)
2740 /* Use char array to avoid alignment issues. */
2741 char fpregs
[sizeof (gdb_fpregset_t
)];
2745 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2746 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2750 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2755 tid_t pr_error_thread
;
2756 tfpregset_t thread_1
;
2759 thread_fpregs
.pr_count
= 1;
2760 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2761 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2763 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2766 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2767 #endif /* osf PIOCTSFPREG */
2768 #endif /* NEW_PROC_API */
2771 /* Policy: writing the regs invalidates our cache. */
2772 pi
->fpregs_valid
= 0;
2777 * Function: proc_kill
2779 * Send a signal to the proc or lwp with the semantics of "kill()".
2780 * Returns non-zero for success, zero for failure.
2784 proc_kill (procinfo
*pi
, int signo
)
2789 * We might conceivably apply this operation to an LWP, and
2790 * the LWP's ctl file descriptor might not be open.
2793 if (pi
->ctl_fd
== 0 &&
2794 open_procinfo_files (pi
, FD_CTL
) == 0)
2801 procfs_ctl_t cmd
[2];
2805 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2806 #else /* ioctl method */
2807 /* FIXME: do I need the Alpha OSF fixups present in
2808 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2809 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2817 * Function: proc_parent_pid
2819 * Find the pid of the process that started this one.
2820 * Returns the parent process pid, or zero.
2824 proc_parent_pid (procinfo
*pi
)
2827 * We should never have to apply this operation to any procinfo
2828 * except the one for the main process. If that ever changes
2829 * for any reason, then take out the following clause and
2830 * replace it with one that makes sure the ctl_fd is open.
2834 pi
= find_procinfo_or_die (pi
->pid
, 0);
2836 if (!pi
->status_valid
)
2837 if (!proc_get_status (pi
))
2840 return pi
->prstatus
.pr_ppid
;
2845 * Function: proc_set_watchpoint
2850 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2852 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2855 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2856 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2861 char watch
[sizeof (prwatch_t
)];
2865 pwatch
= (prwatch_t
*) &arg
.watch
;
2866 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2867 pwatch
->pr_vaddr
= (uintptr_t) address_to_host_pointer (addr
);
2869 pwatch
->pr_vaddr
= (caddr_t
) address_to_host_pointer (addr
);
2871 pwatch
->pr_size
= len
;
2872 pwatch
->pr_wflags
= wflags
;
2873 #if defined(NEW_PROC_API) && defined (PCWATCH)
2875 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2877 #if defined (PIOCSWATCH)
2878 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2880 return 0; /* Fail */
2887 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2889 #include <sys/sysi86.h>
2892 * Function: proc_get_LDT_entry
2898 * The 'key' is actually the value of the lower 16 bits of
2899 * the GS register for the LWP that we're interested in.
2901 * Return: matching ssh struct (LDT entry).
2905 proc_get_LDT_entry (procinfo
*pi
, int key
)
2907 static struct ssd
*ldt_entry
= NULL
;
2909 char pathname
[MAX_PROC_NAME_SIZE
];
2910 struct cleanup
*old_chain
= NULL
;
2913 /* Allocate space for one LDT entry.
2914 This alloc must persist, because we return a pointer to it. */
2915 if (ldt_entry
== NULL
)
2916 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2918 /* Open the file descriptor for the LDT table. */
2919 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2920 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2922 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2925 /* Make sure it gets closed again! */
2926 old_chain
= make_cleanup_close (fd
);
2928 /* Now 'read' thru the table, find a match and return it. */
2929 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2931 if (ldt_entry
->sel
== 0 &&
2932 ldt_entry
->bo
== 0 &&
2933 ldt_entry
->acc1
== 0 &&
2934 ldt_entry
->acc2
== 0)
2935 break; /* end of table */
2936 /* If key matches, return this entry. */
2937 if (ldt_entry
->sel
== key
)
2940 /* Loop ended, match not found. */
2944 static int nalloc
= 0;
2946 /* Get the number of LDT entries. */
2947 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2949 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2953 /* Allocate space for the number of LDT entries. */
2954 /* This alloc has to persist, 'cause we return a pointer to it. */
2957 ldt_entry
= (struct ssd
*)
2958 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2962 /* Read the whole table in one gulp. */
2963 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2965 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2969 /* Search the table and return the (first) entry matching 'key'. */
2970 for (i
= 0; i
< nldt
; i
++)
2971 if (ldt_entry
[i
].sel
== key
)
2972 return &ldt_entry
[i
];
2974 /* Loop ended, match not found. */
2979 #endif /* TM_I386SOL2_H */
2981 /* =============== END, non-thread part of /proc "MODULE" =============== */
2983 /* =================== Thread "MODULE" =================== */
2985 /* NOTE: you'll see more ifdefs and duplication of functions here,
2986 since there is a different way to do threads on every OS. */
2989 * Function: proc_get_nthreads
2991 * Return the number of threads for the process
2994 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2999 proc_get_nthreads (procinfo
*pi
)
3003 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
3004 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
3010 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3012 * Solaris and Unixware version
3015 proc_get_nthreads (procinfo
*pi
)
3017 if (!pi
->status_valid
)
3018 if (!proc_get_status (pi
))
3022 * NEW_PROC_API: only works for the process procinfo,
3023 * because the LWP procinfos do not get prstatus filled in.
3026 if (pi
->tid
!= 0) /* find the parent process procinfo */
3027 pi
= find_procinfo_or_die (pi
->pid
, 0);
3029 return pi
->prstatus
.pr_nlwp
;
3037 proc_get_nthreads (procinfo
*pi
)
3045 * Function: proc_get_current_thread (LWP version)
3047 * Return the ID of the thread that had an event of interest.
3048 * (ie. the one that hit a breakpoint or other traced event).
3049 * All other things being equal, this should be the ID of a
3050 * thread that is currently executing.
3053 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3055 * Solaris and Unixware version
3058 proc_get_current_thread (procinfo
*pi
)
3061 * Note: this should be applied to the root procinfo for the process,
3062 * not to the procinfo for an LWP. If applied to the procinfo for
3063 * an LWP, it will simply return that LWP's ID. In that case,
3064 * find the parent process procinfo.
3068 pi
= find_procinfo_or_die (pi
->pid
, 0);
3070 if (!pi
->status_valid
)
3071 if (!proc_get_status (pi
))
3075 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
3077 return pi
->prstatus
.pr_who
;
3082 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3087 proc_get_current_thread (procinfo
*pi
)
3089 #if 0 /* FIXME: not ready for prime time? */
3090 return pi
->prstatus
.pr_tid
;
3101 proc_get_current_thread (procinfo
*pi
)
3110 * Function: proc_update_threads
3112 * Discover the IDs of all the threads within the process, and
3113 * create a procinfo for each of them (chained to the parent).
3115 * This unfortunately requires a different method on every OS.
3117 * Return: non-zero for success, zero for failure.
3121 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
3123 if (thread
&& parent
) /* sanity */
3125 thread
->status_valid
= 0;
3126 if (!proc_get_status (thread
))
3127 destroy_one_procinfo (&parent
->thread_list
, thread
);
3129 return 0; /* keep iterating */
3132 #if defined (PIOCLSTATUS)
3134 * Solaris 2.5 (ioctl) version
3137 proc_update_threads (procinfo
*pi
)
3139 gdb_prstatus_t
*prstatus
;
3140 struct cleanup
*old_chain
= NULL
;
3145 * We should never have to apply this operation to any procinfo
3146 * except the one for the main process. If that ever changes
3147 * for any reason, then take out the following clause and
3148 * replace it with one that makes sure the ctl_fd is open.
3152 pi
= find_procinfo_or_die (pi
->pid
, 0);
3154 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3156 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
3157 return 1; /* Process is not multi-threaded; nothing to do. */
3159 prstatus
= xmalloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1));
3161 old_chain
= make_cleanup (xfree
, prstatus
);
3162 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
3163 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
3165 /* Skip element zero, which represents the process as a whole. */
3166 for (i
= 1; i
< nlwp
+ 1; i
++)
3168 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
3169 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3171 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
3172 thread
->status_valid
= 1;
3174 pi
->threads_valid
= 1;
3175 do_cleanups (old_chain
);
3181 * Unixware and Solaris 6 (and later) version
3184 do_closedir_cleanup (void *dir
)
3190 proc_update_threads (procinfo
*pi
)
3192 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3193 struct dirent
*direntry
;
3194 struct cleanup
*old_chain
= NULL
;
3200 * We should never have to apply this operation to any procinfo
3201 * except the one for the main process. If that ever changes
3202 * for any reason, then take out the following clause and
3203 * replace it with one that makes sure the ctl_fd is open.
3207 pi
= find_procinfo_or_die (pi
->pid
, 0);
3209 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3214 * Note: this brute-force method is the only way I know of
3215 * to accomplish this task on Unixware. This method will
3216 * also work on Solaris 2.6 and 2.7. There is a much simpler
3217 * and more elegant way to do this on Solaris, but the margins
3218 * of this manuscript are too small to write it here... ;-)
3221 strcpy (pathname
, pi
->pathname
);
3222 strcat (pathname
, "/lwp");
3223 if ((dirp
= opendir (pathname
)) == NULL
)
3224 proc_error (pi
, "update_threads, opendir", __LINE__
);
3226 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
3227 while ((direntry
= readdir (dirp
)) != NULL
)
3228 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3230 lwpid
= atoi (&direntry
->d_name
[0]);
3231 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3232 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3234 pi
->threads_valid
= 1;
3235 do_cleanups (old_chain
);
3244 proc_update_threads (procinfo
*pi
)
3250 * We should never have to apply this operation to any procinfo
3251 * except the one for the main process. If that ever changes
3252 * for any reason, then take out the following clause and
3253 * replace it with one that makes sure the ctl_fd is open.
3257 pi
= find_procinfo_or_die (pi
->pid
, 0);
3259 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3261 nthreads
= proc_get_nthreads (pi
);
3263 return 0; /* nothing to do for 1 or fewer threads */
3265 threads
= xmalloc (nthreads
* sizeof (tid_t
));
3267 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3268 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3270 for (i
= 0; i
< nthreads
; i
++)
3272 if (!find_procinfo (pi
->pid
, threads
[i
]))
3273 if (!create_procinfo (pi
->pid
, threads
[i
]))
3274 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3276 pi
->threads_valid
= 1;
3284 proc_update_threads (procinfo
*pi
)
3288 #endif /* OSF PIOCTLIST */
3289 #endif /* NEW_PROC_API */
3290 #endif /* SOL 2.5 PIOCLSTATUS */
3293 * Function: proc_iterate_over_threads
3296 * Given a pointer to a function, call that function once
3297 * for each lwp in the procinfo list, until the function
3298 * returns non-zero, in which event return the value
3299 * returned by the function.
3301 * Note: this function does NOT call update_threads.
3302 * If you want to discover new threads first, you must
3303 * call that function explicitly. This function just makes
3304 * a quick pass over the currently-known procinfos.
3307 * pi - parent process procinfo
3308 * func - per-thread function
3309 * ptr - opaque parameter for function.
3312 * First non-zero return value from the callee, or zero.
3316 proc_iterate_over_threads (procinfo
*pi
,
3317 int (*func
) (procinfo
*, procinfo
*, void *),
3320 procinfo
*thread
, *next
;
3324 * We should never have to apply this operation to any procinfo
3325 * except the one for the main process. If that ever changes
3326 * for any reason, then take out the following clause and
3327 * replace it with one that makes sure the ctl_fd is open.
3331 pi
= find_procinfo_or_die (pi
->pid
, 0);
3333 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3335 next
= thread
->next
; /* in case thread is destroyed */
3336 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3343 /* =================== END, Thread "MODULE" =================== */
3345 /* =================== END, /proc "MODULE" =================== */
3347 /* =================== GDB "MODULE" =================== */
3350 * Here are all of the gdb target vector functions and their friends.
3353 static ptid_t
do_attach (ptid_t ptid
);
3354 static void do_detach (int signo
);
3355 static int register_gdb_signals (procinfo
*, gdb_sigset_t
*);
3358 * Function: procfs_debug_inferior
3360 * Sets up the inferior to be debugged.
3361 * Registers to trace signals, hardware faults, and syscalls.
3362 * Note: does not set RLC flag: caller may want to customize that.
3364 * Returns: zero for success (note! unlike most functions in this module)
3365 * On failure, returns the LINE NUMBER where it failed!
3369 procfs_debug_inferior (procinfo
*pi
)
3371 fltset_t traced_faults
;
3372 gdb_sigset_t traced_signals
;
3373 sysset_t
*traced_syscall_entries
;
3374 sysset_t
*traced_syscall_exits
;
3377 #ifdef PROCFS_DONT_TRACE_FAULTS
3378 /* On some systems (OSF), we don't trace hardware faults.
3379 Apparently it's enough that we catch them as signals.
3380 Wonder why we don't just do that in general? */
3381 premptyset (&traced_faults
); /* don't trace faults. */
3383 /* Register to trace hardware faults in the child. */
3384 prfillset (&traced_faults
); /* trace all faults... */
3385 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3387 if (!proc_set_traced_faults (pi
, &traced_faults
))
3390 /* Register to trace selected signals in the child. */
3391 premptyset (&traced_signals
);
3392 if (!register_gdb_signals (pi
, &traced_signals
))
3396 /* Register to trace the 'exit' system call (on entry). */
3397 traced_syscall_entries
= sysset_t_alloc (pi
);
3398 gdb_premptysysset (traced_syscall_entries
);
3400 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
3403 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3406 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
3408 #ifdef DYNAMIC_SYSCALLS
3410 int callnum
= find_syscall (pi
, "_exit");
3412 gdb_praddsysset (traced_syscall_entries
, callnum
);
3416 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
3417 xfree (traced_syscall_entries
);
3421 #ifdef PRFS_STOPEXEC /* defined on OSF */
3422 /* OSF method for tracing exec syscalls. Quoting:
3423 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3424 exits from exec system calls because of the user level loader. */
3425 /* FIXME: make nice and maybe move into an access function. */
3429 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3432 prfs_flags
|= PRFS_STOPEXEC
;
3434 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3437 #else /* not PRFS_STOPEXEC */
3438 /* Everyone else's (except OSF) method for tracing exec syscalls */
3440 Not all systems with /proc have all the exec* syscalls with the same
3441 names. On the SGI, for example, there is no SYS_exec, but there
3442 *is* a SYS_execv. So, we try to account for that. */
3444 traced_syscall_exits
= sysset_t_alloc (pi
);
3445 gdb_premptysysset (traced_syscall_exits
);
3447 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
3450 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
3453 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
3456 #ifdef SYS_lwpcreate
3457 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3458 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3461 #ifdef SYS_lwp_create /* FIXME: once only, please */
3462 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3463 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3466 #ifdef DYNAMIC_SYSCALLS
3468 int callnum
= find_syscall (pi
, "execve");
3470 gdb_praddsysset (traced_syscall_exits
, callnum
);
3471 callnum
= find_syscall (pi
, "ra_execve");
3473 gdb_praddsysset (traced_syscall_exits
, callnum
);
3477 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3478 xfree (traced_syscall_exits
);
3482 #endif /* PRFS_STOPEXEC */
3487 procfs_attach (char *args
, int from_tty
)
3493 error_no_arg ("process-id to attach");
3496 if (pid
== getpid ())
3497 error ("Attaching GDB to itself is not a good idea...");
3501 exec_file
= get_exec_file (0);
3504 printf_filtered ("Attaching to program `%s', %s\n",
3505 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3507 printf_filtered ("Attaching to %s\n",
3508 target_pid_to_str (pid_to_ptid (pid
)));
3512 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3513 push_target (&procfs_ops
);
3517 procfs_detach (char *args
, int from_tty
)
3524 exec_file
= get_exec_file (0);
3527 printf_filtered ("Detaching from program: %s %s\n",
3528 exec_file
, target_pid_to_str (inferior_ptid
));
3532 signo
= atoi (args
);
3535 inferior_ptid
= null_ptid
;
3536 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3540 do_attach (ptid_t ptid
)
3545 if ((pi
= create_procinfo (PIDGET (ptid
), 0)) == NULL
)
3546 perror ("procfs: out of memory in 'attach'");
3548 if (!open_procinfo_files (pi
, FD_CTL
))
3550 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3551 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3553 dead_procinfo (pi
, errmsg
, NOKILL
);
3556 /* Stop the process (if it isn't already stopped). */
3557 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3559 pi
->was_stopped
= 1;
3560 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3564 pi
->was_stopped
= 0;
3565 /* Set the process to run again when we close it. */
3566 if (!proc_set_run_on_last_close (pi
))
3567 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3569 /* Now stop the process. */
3570 if (!proc_stop_process (pi
))
3571 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3572 pi
->ignore_next_sigstop
= 1;
3574 /* Save some of the /proc state to be restored if we detach. */
3575 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3576 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3577 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3578 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3579 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3580 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3582 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3583 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3585 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3586 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3588 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3589 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3591 /* Let GDB know that the inferior was attached. */
3593 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3597 do_detach (int signo
)
3601 /* Find procinfo for the main process */
3602 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0); /* FIXME: threads */
3604 if (!proc_set_current_signal (pi
, signo
))
3605 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3607 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3608 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3610 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3611 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3613 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3614 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3616 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3617 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3619 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3620 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3622 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3623 if (signo
|| !(pi
->was_stopped
) ||
3624 query ("Was stopped when attached, make it runnable again? "))
3626 /* Clear any pending signal. */
3627 if (!proc_clear_current_fault (pi
))
3628 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3630 if (!proc_set_run_on_last_close (pi
))
3631 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3635 destroy_procinfo (pi
);
3641 * Since the /proc interface cannot give us individual registers,
3642 * we pay no attention to the (regno) argument, and just fetch them all.
3643 * This results in the possibility that we will do unnecessarily many
3644 * fetches, since we may be called repeatedly for individual registers.
3645 * So we cache the results, and mark the cache invalid when the process
3650 procfs_fetch_registers (int regno
)
3652 gdb_fpregset_t
*fpregs
;
3653 gdb_gregset_t
*gregs
;
3658 pid
= PIDGET (inferior_ptid
);
3659 tid
= TIDGET (inferior_ptid
);
3661 /* First look up procinfo for the main process. */
3662 pi
= find_procinfo_or_die (pid
, 0);
3664 /* If the event thread is not the same as GDB's requested thread
3665 (ie. inferior_ptid), then look up procinfo for the requested
3668 (tid
!= proc_get_current_thread (pi
)))
3669 pi
= find_procinfo_or_die (pid
, tid
);
3672 error ("procfs: fetch_registers failed to find procinfo for %s",
3673 target_pid_to_str (inferior_ptid
));
3675 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3676 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3678 supply_gregset (gregs
);
3680 if (FP0_REGNUM
>= 0) /* need floating point? */
3682 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3683 regno
== PC_REGNUM
||
3684 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3685 regno
== FP_REGNUM
||
3687 return; /* not a floating point register */
3689 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3690 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3692 supply_fpregset (fpregs
);
3696 /* Get ready to modify the registers array. On machines which store
3697 individual registers, this doesn't need to do anything. On
3698 machines which store all the registers in one fell swoop, such as
3699 /proc, this makes sure that registers contains all the registers
3700 from the program being debugged. */
3703 procfs_prepare_to_store (void)
3705 #ifdef CHILD_PREPARE_TO_STORE
3706 CHILD_PREPARE_TO_STORE ();
3713 * Since the /proc interface will not read individual registers,
3714 * we will cache these requests until the process is resumed, and
3715 * only then write them back to the inferior process.
3717 * FIXME: is that a really bad idea? Have to think about cases
3718 * where writing one register might affect the value of others, etc.
3722 procfs_store_registers (int regno
)
3724 gdb_fpregset_t
*fpregs
;
3725 gdb_gregset_t
*gregs
;
3730 pid
= PIDGET (inferior_ptid
);
3731 tid
= TIDGET (inferior_ptid
);
3733 /* First find procinfo for main process */
3734 pi
= find_procinfo_or_die (pid
, 0);
3736 /* If current lwp for process is not the same as requested thread
3737 (ie. inferior_ptid), then find procinfo for the requested thread. */
3740 (tid
!= proc_get_current_thread (pi
)))
3741 pi
= find_procinfo_or_die (pid
, tid
);
3744 error ("procfs: store_registers: failed to find procinfo for %s",
3745 target_pid_to_str (inferior_ptid
));
3747 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3748 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3750 fill_gregset (gregs
, regno
);
3751 if (!proc_set_gregs (pi
))
3752 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3754 if (FP0_REGNUM
>= 0) /* need floating point? */
3756 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3757 regno
== PC_REGNUM
||
3758 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3759 regno
== FP_REGNUM
||
3761 return; /* not a floating point register */
3763 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3764 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3766 fill_fpregset (fpregs
, regno
);
3767 if (!proc_set_fpregs (pi
))
3768 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3773 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3777 if (scall
== SYS_lwp_exit
)
3781 if (scall
== SYS_lwpexit
)
3788 syscall_is_exit (procinfo
*pi
, int scall
)
3791 if (scall
== SYS_exit
)
3794 #ifdef DYNAMIC_SYSCALLS
3795 if (find_syscall (pi
, "_exit") == scall
)
3802 syscall_is_exec (procinfo
*pi
, int scall
)
3805 if (scall
== SYS_exec
)
3809 if (scall
== SYS_execv
)
3813 if (scall
== SYS_execve
)
3816 #ifdef DYNAMIC_SYSCALLS
3817 if (find_syscall (pi
, "_execve"))
3819 if (find_syscall (pi
, "ra_execve"))
3826 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3828 #ifdef SYS_lwp_create
3829 if (scall
== SYS_lwp_create
)
3832 #ifdef SYS_lwpcreate
3833 if (scall
== SYS_lwpcreate
)
3840 * Function: target_wait
3842 * Retrieve the next stop event from the child process.
3843 * If child has not stopped yet, wait for it to stop.
3844 * Translate /proc eventcodes (or possibly wait eventcodes)
3845 * into gdb internal event codes.
3847 * Return: id of process (and possibly thread) that incurred the event.
3848 * event codes are returned thru a pointer parameter.
3852 procfs_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3854 /* First cut: loosely based on original version 2.1 */
3858 ptid_t retval
, temp_ptid
;
3859 int why
, what
, flags
;
3866 retval
= pid_to_ptid (-1);
3868 /* Find procinfo for main process */
3869 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
3872 /* We must assume that the status is stale now... */
3873 pi
->status_valid
= 0;
3874 pi
->gregs_valid
= 0;
3875 pi
->fpregs_valid
= 0;
3877 #if 0 /* just try this out... */
3878 flags
= proc_flags (pi
);
3879 why
= proc_why (pi
);
3880 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3881 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3883 /* If child is not stopped, wait for it to stop. */
3884 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3885 !proc_wait_for_stop (pi
))
3887 /* wait_for_stop failed: has the child terminated? */
3888 if (errno
== ENOENT
)
3892 /* /proc file not found; presumably child has terminated. */
3893 wait_retval
= wait (&wstat
); /* "wait" for the child's exit */
3895 if (wait_retval
!= PIDGET (inferior_ptid
)) /* wrong child? */
3896 error ("procfs: couldn't stop process %d: wait returned %d\n",
3897 PIDGET (inferior_ptid
), wait_retval
);
3898 /* FIXME: might I not just use waitpid?
3899 Or try find_procinfo to see if I know about this child? */
3900 retval
= pid_to_ptid (wait_retval
);
3902 else if (errno
== EINTR
)
3906 /* Unknown error from wait_for_stop. */
3907 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3912 /* This long block is reached if either:
3913 a) the child was already stopped, or
3914 b) we successfully waited for the child with wait_for_stop.
3915 This block will analyze the /proc status, and translate it
3916 into a waitstatus for GDB.
3918 If we actually had to call wait because the /proc file
3919 is gone (child terminated), then we skip this block,
3920 because we already have a waitstatus. */
3922 flags
= proc_flags (pi
);
3923 why
= proc_why (pi
);
3924 what
= proc_what (pi
);
3926 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3929 /* If it's running async (for single_thread control),
3930 set it back to normal again. */
3931 if (flags
& PR_ASYNC
)
3932 if (!proc_unset_async (pi
))
3933 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3937 proc_prettyprint_why (why
, what
, 1);
3939 /* The 'pid' we will return to GDB is composed of
3940 the process ID plus the lwp ID. */
3941 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3945 wstat
= (what
<< 8) | 0177;
3948 if (syscall_is_lwp_exit (pi
, what
))
3950 printf_filtered ("[%s exited]\n",
3951 target_pid_to_str (retval
));
3952 delete_thread (retval
);
3953 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3956 else if (syscall_is_exit (pi
, what
))
3958 /* Handle SYS_exit call only */
3959 /* Stopped at entry to SYS_exit.
3960 Make it runnable, resume it, then use
3961 the wait system call to get its exit code.
3962 Proc_run_process always clears the current
3964 Then return its exit status. */
3965 pi
->status_valid
= 0;
3967 /* FIXME: what we should do is return
3968 TARGET_WAITKIND_SPURIOUS. */
3969 if (!proc_run_process (pi
, 0, 0))
3970 proc_error (pi
, "target_wait, run_process", __LINE__
);
3973 /* Don't call wait: simulate waiting for exit,
3974 return a "success" exit code. Bogus: what if
3975 it returns something else? */
3977 retval
= inferior_ptid
; /* ? ? ? */
3981 int temp
= wait (&wstat
);
3983 /* FIXME: shouldn't I make sure I get the right
3984 event from the right process? If (for
3985 instance) I have killed an earlier inferior
3986 process but failed to clean up after it
3987 somehow, I could get its termination event
3990 /* If wait returns -1, that's what we return to GDB. */
3992 retval
= pid_to_ptid (temp
);
3997 printf_filtered ("procfs: trapped on entry to ");
3998 proc_prettyprint_syscall (proc_what (pi
), 0);
3999 printf_filtered ("\n");
4002 long i
, nsysargs
, *sysargs
;
4004 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4005 (sysargs
= proc_sysargs (pi
)) != NULL
)
4007 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4008 for (i
= 0; i
< nsysargs
; i
++)
4009 printf_filtered ("#%ld: 0x%08lx\n",
4017 /* How to exit gracefully, returning "unknown event" */
4018 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4019 return inferior_ptid
;
4023 /* How to keep going without returning to wfi: */
4024 target_resume (ptid
, 0, TARGET_SIGNAL_0
);
4030 if (syscall_is_exec (pi
, what
))
4032 /* Hopefully this is our own "fork-child" execing
4033 the real child. Hoax this event into a trap, and
4034 GDB will see the child about to execute its start
4036 wstat
= (SIGTRAP
<< 8) | 0177;
4038 else if (syscall_is_lwp_create (pi
, what
))
4041 * This syscall is somewhat like fork/exec.
4042 * We will get the event twice: once for the parent LWP,
4043 * and once for the child. We should already know about
4044 * the parent LWP, but the child will be new to us. So,
4045 * whenever we get this event, if it represents a new
4046 * thread, simply add the thread to the list.
4049 /* If not in procinfo list, add it. */
4050 temp_tid
= proc_get_current_thread (pi
);
4051 if (!find_procinfo (pi
->pid
, temp_tid
))
4052 create_procinfo (pi
->pid
, temp_tid
);
4054 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4055 /* If not in GDB's thread list, add it. */
4056 if (!in_thread_list (temp_ptid
))
4058 printf_filtered ("[New %s]\n",
4059 target_pid_to_str (temp_ptid
));
4060 add_thread (temp_ptid
);
4062 /* Return to WFI, but tell it to immediately resume. */
4063 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4064 return inferior_ptid
;
4066 else if (syscall_is_lwp_exit (pi
, what
))
4068 printf_filtered ("[%s exited]\n",
4069 target_pid_to_str (retval
));
4070 delete_thread (retval
);
4071 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4076 /* FIXME: Do we need to handle SYS_sproc,
4077 SYS_fork, or SYS_vfork here? The old procfs
4078 seemed to use this event to handle threads on
4079 older (non-LWP) systems, where I'm assuming
4080 that threads were actually separate processes.
4081 Irix, maybe? Anyway, low priority for now. */
4085 printf_filtered ("procfs: trapped on exit from ");
4086 proc_prettyprint_syscall (proc_what (pi
), 0);
4087 printf_filtered ("\n");
4090 long i
, nsysargs
, *sysargs
;
4092 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4093 (sysargs
= proc_sysargs (pi
)) != NULL
)
4095 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4096 for (i
= 0; i
< nsysargs
; i
++)
4097 printf_filtered ("#%ld: 0x%08lx\n",
4102 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4103 return inferior_ptid
;
4108 wstat
= (SIGSTOP
<< 8) | 0177;
4113 printf_filtered ("Retry #%d:\n", retry
);
4114 pi
->status_valid
= 0;
4119 /* If not in procinfo list, add it. */
4120 temp_tid
= proc_get_current_thread (pi
);
4121 if (!find_procinfo (pi
->pid
, temp_tid
))
4122 create_procinfo (pi
->pid
, temp_tid
);
4124 /* If not in GDB's thread list, add it. */
4125 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4126 if (!in_thread_list (temp_ptid
))
4128 printf_filtered ("[New %s]\n",
4129 target_pid_to_str (temp_ptid
));
4130 add_thread (temp_ptid
);
4133 status
->kind
= TARGET_WAITKIND_STOPPED
;
4134 status
->value
.sig
= 0;
4139 wstat
= (what
<< 8) | 0177;
4142 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
4145 wstat
= (SIGTRAP
<< 8) | 0177;
4150 wstat
= (SIGTRAP
<< 8) | 0177;
4153 /* FIXME: use si_signo where possible. */
4155 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4158 wstat
= (SIGILL
<< 8) | 0177;
4161 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4164 wstat
= (SIGTRAP
<< 8) | 0177;
4168 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4171 wstat
= (SIGSEGV
<< 8) | 0177;
4175 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4178 wstat
= (SIGFPE
<< 8) | 0177;
4180 case FLTPAGE
: /* Recoverable page fault */
4181 default: /* FIXME: use si_signo if possible for fault */
4182 retval
= pid_to_ptid (-1);
4183 printf_filtered ("procfs:%d -- ", __LINE__
);
4184 printf_filtered ("child stopped for unknown reason:\n");
4185 proc_prettyprint_why (why
, what
, 1);
4186 error ("... giving up...");
4189 break; /* case PR_FAULTED: */
4190 default: /* switch (why) unmatched */
4191 printf_filtered ("procfs:%d -- ", __LINE__
);
4192 printf_filtered ("child stopped for unknown reason:\n");
4193 proc_prettyprint_why (why
, what
, 1);
4194 error ("... giving up...");
4198 * Got this far without error:
4199 * If retval isn't in the threads database, add it.
4201 if (PIDGET (retval
) > 0 &&
4202 !ptid_equal (retval
, inferior_ptid
) &&
4203 !in_thread_list (retval
))
4206 * We have a new thread.
4207 * We need to add it both to GDB's list and to our own.
4208 * If we don't create a procinfo, resume may be unhappy
4211 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
4212 add_thread (retval
);
4213 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
4214 create_procinfo (PIDGET (retval
), TIDGET (retval
));
4216 /* In addition, it's possible that this is the first
4217 * new thread we've seen, in which case we may not
4218 * have created entries for inferior_ptid yet.
4220 if (TIDGET (inferior_ptid
) != 0)
4222 if (!in_thread_list (inferior_ptid
))
4223 add_thread (inferior_ptid
);
4224 if (find_procinfo (PIDGET (inferior_ptid
),
4225 TIDGET (inferior_ptid
)) == NULL
)
4226 create_procinfo (PIDGET (inferior_ptid
),
4227 TIDGET (inferior_ptid
));
4231 else /* flags do not indicate STOPPED */
4233 /* surely this can't happen... */
4234 printf_filtered ("procfs:%d -- process not stopped.\n",
4236 proc_prettyprint_flags (flags
, 1);
4237 error ("procfs: ...giving up...");
4242 store_waitstatus (status
, wstat
);
4248 /* Transfer LEN bytes between GDB address MYADDR and target address
4249 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4250 otherwise transfer them from the target. TARGET is unused.
4252 The return value is 0 if an error occurred or no bytes were
4253 transferred. Otherwise, it will be a positive value which
4254 indicates the number of bytes transferred between gdb and the
4255 target. (Note that the interface also makes provisions for
4256 negative values, but this capability isn't implemented here.) */
4259 procfs_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int dowrite
,
4260 struct mem_attrib
*attrib
, struct target_ops
*target
)
4265 /* Find procinfo for main process */
4266 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4267 if (pi
->as_fd
== 0 &&
4268 open_procinfo_files (pi
, FD_AS
) == 0)
4270 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4274 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4279 PROCFS_NOTE ("write memory: ");
4281 PROCFS_NOTE ("write memory: \n");
4283 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4287 PROCFS_NOTE ("read memory: \n");
4288 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4299 * Function: invalidate_cache
4301 * Called by target_resume before making child runnable.
4302 * Mark cached registers and status's invalid.
4303 * If there are "dirty" caches that need to be written back
4304 * to the child process, do that.
4306 * File descriptors are also cached.
4307 * As they are a limited resource, we cannot hold onto them indefinitely.
4308 * However, as they are expensive to open, we don't want to throw them
4309 * away indescriminately either. As a compromise, we will keep the
4310 * file descriptors for the parent process, but discard any file
4311 * descriptors we may have accumulated for the threads.
4314 * As this function is called by iterate_over_threads, it always
4315 * returns zero (so that iterate_over_threads will keep iterating).
4320 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4323 * About to run the child; invalidate caches and do any other cleanup.
4327 if (pi
->gregs_dirty
)
4328 if (parent
== NULL
||
4329 proc_get_current_thread (parent
) != pi
->tid
)
4330 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4331 proc_warn (pi
, "target_resume, set_gregs",
4333 if (FP0_REGNUM
>= 0)
4334 if (pi
->fpregs_dirty
)
4335 if (parent
== NULL
||
4336 proc_get_current_thread (parent
) != pi
->tid
)
4337 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4338 proc_warn (pi
, "target_resume, set_fpregs",
4344 /* The presence of a parent indicates that this is an LWP.
4345 Close any file descriptors that it might have open.
4346 We don't do this to the master (parent) procinfo. */
4348 close_procinfo_files (pi
);
4350 pi
->gregs_valid
= 0;
4351 pi
->fpregs_valid
= 0;
4353 pi
->gregs_dirty
= 0;
4354 pi
->fpregs_dirty
= 0;
4356 pi
->status_valid
= 0;
4357 pi
->threads_valid
= 0;
4364 * Function: make_signal_thread_runnable
4366 * A callback function for iterate_over_threads.
4367 * Find the asynchronous signal thread, and make it runnable.
4368 * See if that helps matters any.
4372 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4375 if (proc_flags (pi
) & PR_ASLWP
)
4377 if (!proc_run_process (pi
, 0, -1))
4378 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4387 * Function: target_resume
4389 * Make the child process runnable. Normally we will then call
4390 * procfs_wait and wait for it to stop again (unles gdb is async).
4393 * step: if true, then arrange for the child to stop again
4394 * after executing a single instruction.
4395 * signo: if zero, then cancel any pending signal.
4396 * If non-zero, then arrange for the indicated signal
4397 * to be delivered to the child when it runs.
4398 * pid: if -1, then allow any child thread to run.
4399 * if non-zero, then allow only the indicated thread to run.
4400 ******* (not implemented yet)
4404 procfs_resume (ptid_t ptid
, int step
, enum target_signal signo
)
4406 procinfo
*pi
, *thread
;
4410 prrun.prflags |= PRSVADDR;
4411 prrun.pr_vaddr = $PC; set resume address
4412 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4413 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4414 prrun.prflags |= PRCFAULT; clear current fault.
4416 PRSTRACE and PRSFAULT can be done by other means
4417 (proc_trace_signals, proc_trace_faults)
4418 PRSVADDR is unnecessary.
4419 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4420 This basically leaves PRSTEP and PRCSIG.
4421 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4422 So basically PR_STEP is the sole argument that must be passed
4423 to proc_run_process (for use in the prrun struct by ioctl). */
4425 /* Find procinfo for main process */
4426 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4428 /* First cut: ignore pid argument */
4431 /* Convert signal to host numbering. */
4433 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4436 native_signo
= target_signal_to_host (signo
);
4438 pi
->ignore_next_sigstop
= 0;
4440 /* Running the process voids all cached registers and status. */
4441 /* Void the threads' caches first */
4442 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4443 /* Void the process procinfo's caches. */
4444 invalidate_cache (NULL
, pi
, NULL
);
4446 if (PIDGET (ptid
) != -1)
4448 /* Resume a specific thread, presumably suppressing the others. */
4449 thread
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
));
4452 if (thread
->tid
!= 0)
4454 /* We're to resume a specific thread, and not the others.
4455 * Set the child process's PR_ASYNC flag.
4458 if (!proc_set_async (pi
))
4459 proc_error (pi
, "target_resume, set_async", __LINE__
);
4462 proc_iterate_over_threads (pi
,
4463 make_signal_thread_runnable
,
4466 pi
= thread
; /* substitute the thread's procinfo for run */
4471 if (!proc_run_process (pi
, step
, native_signo
))
4474 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4476 proc_error (pi
, "target_resume", __LINE__
);
4481 * Function: register_gdb_signals
4483 * Traverse the list of signals that GDB knows about
4484 * (see "handle" command), and arrange for the target
4485 * to be stopped or not, according to these settings.
4487 * Returns non-zero for success, zero for failure.
4491 register_gdb_signals (procinfo
*pi
, gdb_sigset_t
*signals
)
4495 for (signo
= 0; signo
< NSIG
; signo
++)
4496 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4497 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4498 signal_pass_state (target_signal_from_host (signo
)) == 1)
4499 prdelset (signals
, signo
);
4501 praddset (signals
, signo
);
4503 return proc_set_traced_signals (pi
, signals
);
4507 * Function: target_notice_signals
4509 * Set up to trace signals in the child process.
4513 procfs_notice_signals (ptid_t ptid
)
4515 gdb_sigset_t signals
;
4516 procinfo
*pi
= find_procinfo_or_die (PIDGET (ptid
), 0);
4518 if (proc_get_traced_signals (pi
, &signals
) &&
4519 register_gdb_signals (pi
, &signals
))
4522 proc_error (pi
, "notice_signals", __LINE__
);
4526 * Function: target_files_info
4528 * Print status information about the child process.
4532 procfs_files_info (struct target_ops
*ignore
)
4534 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4535 attach_flag
? "attached": "child",
4536 target_pid_to_str (inferior_ptid
));
4540 * Function: target_open
4542 * A dummy: you don't open procfs.
4546 procfs_open (char *args
, int from_tty
)
4548 error ("Use the \"run\" command to start a Unix child process.");
4552 * Function: target_can_run
4554 * This tells GDB that this target vector can be invoked
4555 * for "run" or "attach".
4558 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4559 be a runnable target. Used by targets
4560 that can sit atop procfs, such as solaris
4565 procfs_can_run (void)
4567 /* This variable is controlled by modules that sit atop procfs that
4568 may layer their own process structure atop that provided here.
4569 sol-thread.c does this because of the Solaris two-level thread
4572 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4574 return !procfs_suppress_run
;
4578 * Function: target_stop
4580 * Stop the child process asynchronously, as when the
4581 * gdb user types control-c or presses a "stop" button.
4583 * Works by sending kill(SIGINT) to the child's process group.
4589 extern pid_t inferior_process_group
;
4591 kill (-inferior_process_group
, SIGINT
);
4595 * Function: unconditionally_kill_inferior
4597 * Make it die. Wait for it to die. Clean up after it.
4598 * Note: this should only be applied to the real process,
4599 * not to an LWP, because of the check for parent-process.
4600 * If we need this to work for an LWP, it needs some more logic.
4604 unconditionally_kill_inferior (procinfo
*pi
)
4608 parent_pid
= proc_parent_pid (pi
);
4609 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4610 /* FIXME: use access functions */
4611 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4612 before the PIOCKILL, otherwise it might generate a corrupted core
4613 file for the inferior. */
4614 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4616 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4619 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4620 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4621 to kill the inferior, otherwise it might remain stopped with a
4623 We do not check the result of the PIOCSSIG, the inferior might have
4626 gdb_siginfo_t newsiginfo
;
4628 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4629 newsiginfo
.si_signo
= SIGKILL
;
4630 newsiginfo
.si_code
= 0;
4631 newsiginfo
.si_errno
= 0;
4632 newsiginfo
.si_pid
= getpid ();
4633 newsiginfo
.si_uid
= getuid ();
4634 /* FIXME: use proc_set_current_signal */
4635 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4637 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4638 if (!proc_kill (pi
, SIGKILL
))
4639 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4640 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4641 destroy_procinfo (pi
);
4643 /* If pi is GDB's child, wait for it to die. */
4644 if (parent_pid
== getpid ())
4645 /* FIXME: should we use waitpid to make sure we get the right event?
4646 Should we check the returned event? */
4651 ret
= waitpid (pi
->pid
, &status
, 0);
4659 * Function: target_kill_inferior
4661 * We're done debugging it, and we want it to go away.
4662 * Then we want GDB to forget all about it.
4666 procfs_kill_inferior (void)
4668 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4670 /* Find procinfo for main process */
4671 procinfo
*pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4674 unconditionally_kill_inferior (pi
);
4675 target_mourn_inferior ();
4680 * Function: target_mourn_inferior
4682 * Forget we ever debugged this thing!
4686 procfs_mourn_inferior (void)
4690 if (!ptid_equal (inferior_ptid
, null_ptid
))
4692 /* Find procinfo for main process */
4693 pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4695 destroy_procinfo (pi
);
4697 unpush_target (&procfs_ops
);
4698 generic_mourn_inferior ();
4702 * Function: init_inferior
4704 * When GDB forks to create a runnable inferior process,
4705 * this function is called on the parent side of the fork.
4706 * It's job is to do whatever is necessary to make the child
4707 * ready to be debugged, and then wait for the child to synchronize.
4711 procfs_init_inferior (int pid
)
4714 gdb_sigset_t signals
;
4717 /* This routine called on the parent side (GDB side)
4718 after GDB forks the inferior. */
4720 push_target (&procfs_ops
);
4722 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4723 perror ("procfs: out of memory in 'init_inferior'");
4725 if (!open_procinfo_files (pi
, FD_CTL
))
4726 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4730 open_procinfo_files // done
4733 procfs_notice_signals
4740 /* If not stopped yet, wait for it to stop. */
4741 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4742 !(proc_wait_for_stop (pi
)))
4743 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4745 /* Save some of the /proc state to be restored if we detach. */
4746 /* FIXME: Why? In case another debugger was debugging it?
4747 We're it's parent, for Ghu's sake! */
4748 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4749 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4750 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4751 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4752 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4753 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4754 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4755 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4756 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4757 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4759 /* Register to trace selected signals in the child. */
4760 prfillset (&signals
);
4761 if (!register_gdb_signals (pi
, &signals
))
4762 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4764 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4765 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4767 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4768 and possibly even turning ON kill-on-last-close at this point. But
4769 I can't make that change without careful testing which I don't have
4770 time to do right now... */
4771 /* Turn on run-on-last-close flag so that the child
4772 will die if GDB goes away for some reason. */
4773 if (!proc_set_run_on_last_close (pi
))
4774 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4776 /* The 'process ID' we return to GDB is composed of
4777 the actual process ID plus the lwp ID. */
4778 inferior_ptid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4780 #ifdef START_INFERIOR_TRAPS_EXPECTED
4781 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4783 /* One trap to exec the shell, one to exec the program being debugged. */
4784 startup_inferior (2);
4785 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4789 * Function: set_exec_trap
4791 * When GDB forks to create a new process, this function is called
4792 * on the child side of the fork before GDB exec's the user program.
4793 * Its job is to make the child minimally debuggable, so that the
4794 * parent GDB process can connect to the child and take over.
4795 * This function should do only the minimum to make that possible,
4796 * and to synchronize with the parent process. The parent process
4797 * should take care of the details.
4801 procfs_set_exec_trap (void)
4803 /* This routine called on the child side (inferior side)
4804 after GDB forks the inferior. It must use only local variables,
4805 because it may be sharing data space with its parent. */
4810 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4811 perror_with_name ("procfs: create_procinfo failed in child.");
4813 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4815 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4816 gdb_flush (gdb_stderr
);
4817 /* no need to call "dead_procinfo", because we're going to exit. */
4821 #ifdef PRFS_STOPEXEC /* defined on OSF */
4822 /* OSF method for tracing exec syscalls. Quoting:
4823 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4824 exits from exec system calls because of the user level loader. */
4825 /* FIXME: make nice and maybe move into an access function. */
4829 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4831 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4832 gdb_flush (gdb_stderr
);
4835 prfs_flags
|= PRFS_STOPEXEC
;
4837 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4839 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4840 gdb_flush (gdb_stderr
);
4844 #else /* not PRFS_STOPEXEC */
4845 /* Everyone else's (except OSF) method for tracing exec syscalls */
4847 Not all systems with /proc have all the exec* syscalls with the same
4848 names. On the SGI, for example, there is no SYS_exec, but there
4849 *is* a SYS_execv. So, we try to account for that. */
4851 exitset
= sysset_t_alloc (pi
);
4852 gdb_premptysysset (exitset
);
4854 gdb_praddsysset (exitset
, SYS_exec
);
4857 gdb_praddsysset (exitset
, SYS_execve
);
4860 gdb_praddsysset (exitset
, SYS_execv
);
4862 #ifdef DYNAMIC_SYSCALLS
4864 int callnum
= find_syscall (pi
, "execve");
4867 gdb_praddsysset (exitset
, callnum
);
4869 callnum
= find_syscall (pi
, "ra_execve");
4871 gdb_praddsysset (exitset
, callnum
);
4873 #endif /* DYNAMIC_SYSCALLS */
4875 if (!proc_set_traced_sysexit (pi
, exitset
))
4877 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4878 gdb_flush (gdb_stderr
);
4881 #endif /* PRFS_STOPEXEC */
4883 /* FIXME: should this be done in the parent instead? */
4884 /* Turn off inherit on fork flag so that all grand-children
4885 of gdb start with tracing flags cleared. */
4886 if (!proc_unset_inherit_on_fork (pi
))
4887 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4889 /* Turn off run on last close flag, so that the child process
4890 cannot run away just because we close our handle on it.
4891 We want it to wait for the parent to attach. */
4892 if (!proc_unset_run_on_last_close (pi
))
4893 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4895 /* FIXME: No need to destroy the procinfo --
4896 we have our own address space, and we're about to do an exec! */
4897 /*destroy_procinfo (pi);*/
4901 * Function: create_inferior
4903 * This function is called BEFORE gdb forks the inferior process.
4904 * Its only real responsibility is to set things up for the fork,
4905 * and tell GDB which two functions to call after the fork (one
4906 * for the parent, and one for the child).
4908 * This function does a complicated search for a unix shell program,
4909 * which it then uses to parse arguments and environment variables
4910 * to be sent to the child. I wonder whether this code could not
4911 * be abstracted out and shared with other unix targets such as
4916 procfs_create_inferior (char *exec_file
, char *allargs
, char **env
)
4918 char *shell_file
= getenv ("SHELL");
4920 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4923 /* We will be looking down the PATH to find shell_file. If we
4924 just do this the normal way (via execlp, which operates by
4925 attempting an exec for each element of the PATH until it
4926 finds one which succeeds), then there will be an exec for
4927 each failed attempt, each of which will cause a PR_SYSEXIT
4928 stop, and we won't know how to distinguish the PR_SYSEXIT's
4929 for these failed execs with the ones for successful execs
4930 (whether the exec has succeeded is stored at that time in the
4931 carry bit or some such architecture-specific and
4932 non-ABI-specified place).
4934 So I can't think of anything better than to search the PATH
4935 now. This has several disadvantages: (1) There is a race
4936 condition; if we find a file now and it is deleted before we
4937 exec it, we lose, even if the deletion leaves a valid file
4938 further down in the PATH, (2) there is no way to know exactly
4939 what an executable (in the sense of "capable of being
4940 exec'd") file is. Using access() loses because it may lose
4941 if the caller is the superuser; failing to use it loses if
4942 there are ACLs or some such. */
4946 /* FIXME-maybe: might want "set path" command so user can change what
4947 path is used from within GDB. */
4948 char *path
= getenv ("PATH");
4950 struct stat statbuf
;
4953 path
= "/bin:/usr/bin";
4955 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4956 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4958 p1
= strchr (p
, ':');
4963 strncpy (tryname
, p
, len
);
4964 tryname
[len
] = '\0';
4965 strcat (tryname
, "/");
4966 strcat (tryname
, shell_file
);
4967 if (access (tryname
, X_OK
) < 0)
4969 if (stat (tryname
, &statbuf
) < 0)
4971 if (!S_ISREG (statbuf
.st_mode
))
4972 /* We certainly need to reject directories. I'm not quite
4973 as sure about FIFOs, sockets, etc., but I kind of doubt
4974 that people want to exec() these things. */
4979 /* Not found. This must be an error rather than merely passing
4980 the file to execlp(), because execlp() would try all the
4981 exec()s, causing GDB to get confused. */
4982 error ("procfs:%d -- Can't find shell %s in PATH",
4983 __LINE__
, shell_file
);
4985 shell_file
= tryname
;
4988 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4989 procfs_init_inferior
, NULL
, shell_file
);
4991 /* We are at the first instruction we care about. */
4992 /* Pedal to the metal... */
4994 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
4998 * Function: notice_thread
5000 * Callback for find_new_threads.
5001 * Calls "add_thread".
5005 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
5007 ptid_t gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
5009 if (!in_thread_list (gdb_threadid
))
5010 add_thread (gdb_threadid
);
5016 * Function: target_find_new_threads
5018 * Query all the threads that the target knows about,
5019 * and give them back to GDB to add to its list.
5023 procfs_find_new_threads (void)
5027 /* Find procinfo for main process */
5028 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5029 proc_update_threads (pi
);
5030 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
5034 * Function: target_thread_alive
5036 * Return true if the thread is still 'alive'.
5038 * This guy doesn't really seem to be doing his job.
5039 * Got to investigate how to tell when a thread is really gone.
5043 procfs_thread_alive (ptid_t ptid
)
5048 proc
= PIDGET (ptid
);
5049 thread
= TIDGET (ptid
);
5050 /* If I don't know it, it ain't alive! */
5051 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
5054 /* If I can't get its status, it ain't alive!
5055 What's more, I need to forget about it! */
5056 if (!proc_get_status (pi
))
5058 destroy_procinfo (pi
);
5061 /* I couldn't have got its status if it weren't alive, so it's alive. */
5066 * Function: target_pid_to_str
5068 * Return a string to be used to identify the thread in
5069 * the "info threads" display.
5073 procfs_pid_to_str (ptid_t ptid
)
5075 static char buf
[80];
5079 proc
= PIDGET (ptid
);
5080 thread
= TIDGET (ptid
);
5081 pi
= find_procinfo (proc
, thread
);
5084 sprintf (buf
, "Process %d", proc
);
5086 sprintf (buf
, "LWP %d", thread
);
5091 * Function: procfs_set_watchpoint
5092 * Insert a watchpoint
5096 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
5104 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5105 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5107 /* Translate from GDB's flags to /proc's */
5108 if (len
> 0) /* len == 0 means delete watchpoint */
5110 switch (rwflag
) { /* FIXME: need an enum! */
5111 case hw_write
: /* default watchpoint (write) */
5112 pflags
= WRITE_WATCHFLAG
;
5114 case hw_read
: /* read watchpoint */
5115 pflags
= READ_WATCHFLAG
;
5117 case hw_access
: /* access watchpoint */
5118 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
5120 case hw_execute
: /* execution HW breakpoint */
5121 pflags
= EXEC_WATCHFLAG
;
5123 default: /* Something weird. Return error. */
5126 if (after
) /* Stop after r/w access is completed. */
5127 pflags
|= AFTER_WATCHFLAG
;
5130 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
5132 if (errno
== E2BIG
) /* Typical error for no resources */
5133 return -1; /* fail */
5134 /* GDB may try to remove the same watchpoint twice.
5135 If a remove request returns no match, don't error. */
5136 if (errno
== ESRCH
&& len
== 0)
5137 return 0; /* ignore */
5138 proc_error (pi
, "set_watchpoint", __LINE__
);
5141 #endif /* UNIXWARE */
5145 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
5146 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
5147 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
5150 Note: procfs_can_use_hw_breakpoint() is not yet used by all
5151 procfs.c targets due to the fact that some of them still define
5152 TARGET_CAN_USE_HARDWARE_WATCHPOINT. */
5155 procfs_can_use_hw_breakpoint (int type
, int cnt
, int othertype
)
5157 #ifndef TARGET_HAS_HARDWARE_WATCHPOINTS
5160 /* Due to the way that proc_set_watchpoint() is implemented, host
5161 and target pointers must be of the same size. If they are not,
5162 we can't use hardware watchpoints. This limitation is due to the
5163 fact that proc_set_watchpoint() calls address_to_host_pointer();
5164 a close inspection of address_to_host_pointer will reveal that
5165 an internal error will be generated when the host and target
5166 pointer sizes are different. */
5167 if (sizeof (void *) != TYPE_LENGTH (builtin_type_void_data_ptr
))
5170 /* Other tests here??? */
5177 * Function: stopped_by_watchpoint
5179 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5180 * else returns zero.
5184 procfs_stopped_by_watchpoint (ptid_t ptid
)
5188 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5189 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5191 if (!pi
) /* If no process, then not stopped by watchpoint! */
5194 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
5196 if (proc_why (pi
) == PR_FAULTED
)
5199 if (proc_what (pi
) == FLTWATCH
)
5203 if (proc_what (pi
) == FLTKWATCH
)
5211 #ifdef TM_I386SOL2_H
5213 * Function: procfs_find_LDT_entry
5216 * ptid_t ptid; // The GDB-style pid-plus-LWP.
5219 * pointer to the corresponding LDT entry.
5223 procfs_find_LDT_entry (ptid_t ptid
)
5225 gdb_gregset_t
*gregs
;
5229 /* Find procinfo for the lwp. */
5230 if ((pi
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
))) == NULL
)
5232 warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.",
5233 PIDGET (ptid
), TIDGET (ptid
));
5236 /* get its general registers. */
5237 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
5239 warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.",
5240 PIDGET (ptid
), TIDGET (ptid
));
5243 /* Now extract the GS register's lower 16 bits. */
5244 key
= (*gregs
)[GS
] & 0xffff;
5246 /* Find the matching entry and return it. */
5247 return proc_get_LDT_entry (pi
, key
);
5249 #endif /* TM_I386SOL2_H */
5252 * Memory Mappings Functions:
5256 * Function: iterate_over_mappings
5258 * Call a callback function once for each mapping, passing it the mapping,
5259 * an optional secondary callback function, and some optional opaque data.
5260 * Quit and return the first non-zero value returned from the callback.
5263 * pi -- procinfo struct for the process to be mapped.
5264 * func -- callback function to be called by this iterator.
5265 * data -- optional opaque data to be passed to the callback function.
5266 * child_func -- optional secondary function pointer to be passed
5267 * to the child function.
5269 * Return: First non-zero return value from the callback function,
5274 iterate_over_mappings (procinfo
*pi
, int (*child_func
) (), void *data
,
5275 int (*func
) (struct prmap
*map
,
5276 int (*child_func
) (),
5279 char pathname
[MAX_PROC_NAME_SIZE
];
5280 struct prmap
*prmaps
;
5281 struct prmap
*prmap
;
5289 /* Get the number of mappings, allocate space,
5290 and read the mappings into prmaps. */
5293 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
5294 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
5295 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
5297 /* Make sure it gets closed again. */
5298 make_cleanup_close (map_fd
);
5300 /* Use stat to determine the file size, and compute
5301 the number of prmap_t objects it contains. */
5302 if (fstat (map_fd
, &sbuf
) != 0)
5303 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
5305 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
5306 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5307 if (read (map_fd
, (char *) prmaps
, nmap
* sizeof (*prmaps
))
5308 != (nmap
* sizeof (*prmaps
)))
5309 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
5311 /* Use ioctl command PIOCNMAP to get number of mappings. */
5312 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmap
) != 0)
5313 proc_error (pi
, "iterate_over_mappings (PIOCNMAP)", __LINE__
);
5315 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5316 if (ioctl (pi
->ctl_fd
, PIOCMAP
, prmaps
) != 0)
5317 proc_error (pi
, "iterate_over_mappings (PIOCMAP)", __LINE__
);
5320 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
5321 if ((funcstat
= (*func
) (prmap
, child_func
, data
)) != 0)
5328 * Function: solib_mappings_callback
5330 * Calls the supplied callback function once for each mapped address
5331 * space in the process. The callback function receives an open
5332 * file descriptor for the file corresponding to that mapped
5333 * address space (if there is one), and the base address of the
5334 * mapped space. Quit when the callback function returns a
5335 * nonzero value, or at teh end of the mappings.
5337 * Returns: the first non-zero return value of the callback function,
5341 int solib_mappings_callback (struct prmap
*map
,
5342 int (*func
) (int, CORE_ADDR
),
5345 procinfo
*pi
= data
;
5349 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
5351 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
5352 return -1; /* sanity */
5354 if (map
->pr_mapname
[0] == 0)
5356 fd
= -1; /* no map file */
5360 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
5361 /* Note: caller's responsibility to close this fd! */
5362 fd
= open_with_retry (name
, O_RDONLY
);
5363 /* Note: we don't test the above call for failure;
5364 we just pass the FD on as given. Sometimes there is
5365 no file, so the open may return failure, but that's
5369 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
->pr_vaddr
);
5370 /* Note: we don't test the above call for failure;
5371 we just pass the FD on as given. Sometimes there is
5372 no file, so the ioctl may return failure, but that's
5375 return (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
);
5379 * Function: proc_iterate_over_mappings
5381 * Uses the unified "iterate_over_mappings" function
5382 * to implement the exported interface to solib-svr4.c.
5384 * Given a pointer to a function, call that function once for every
5385 * mapped address space in the process. The callback function
5386 * receives an open file descriptor for the file corresponding to
5387 * that mapped address space (if there is one), and the base address
5388 * of the mapped space. Quit when the callback function returns a
5389 * nonzero value, or at teh end of the mappings.
5391 * Returns: the first non-zero return value of the callback function,
5396 proc_iterate_over_mappings (int (*func
) (int, CORE_ADDR
))
5398 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5400 return iterate_over_mappings (pi
, func
, pi
, solib_mappings_callback
);
5404 * Function: find_memory_regions_callback
5406 * Implements the to_find_memory_regions method.
5407 * Calls an external function for each memory region.
5408 * External function will have the signiture:
5410 * int callback (CORE_ADDR vaddr,
5411 * unsigned long size,
5412 * int read, int write, int execute,
5415 * Returns the integer value returned by the callback.
5419 find_memory_regions_callback (struct prmap
*map
,
5420 int (*func
) (CORE_ADDR
,
5426 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
5428 (map
->pr_mflags
& MA_READ
) != 0,
5429 (map
->pr_mflags
& MA_WRITE
) != 0,
5430 (map
->pr_mflags
& MA_EXEC
) != 0,
5435 * Function: proc_find_memory_regions
5437 * External interface. Calls a callback function once for each
5438 * mapped memory region in the child process, passing as arguments
5439 * CORE_ADDR virtual_address,
5440 * unsigned long size,
5441 * int read, TRUE if region is readable by the child
5442 * int write, TRUE if region is writable by the child
5443 * int execute TRUE if region is executable by the child.
5445 * Stops iterating and returns the first non-zero value
5446 * returned by the callback.
5450 proc_find_memory_regions (int (*func
) (CORE_ADDR
,
5456 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5458 return iterate_over_mappings (pi
, func
, data
,
5459 find_memory_regions_callback
);
5463 * Function: mappingflags
5465 * Returns an ascii representation of a memory mapping's flags.
5469 mappingflags (long flags
)
5471 static char asciiflags
[8];
5473 strcpy (asciiflags
, "-------");
5474 #if defined (MA_PHYS)
5475 if (flags
& MA_PHYS
)
5476 asciiflags
[0] = 'd';
5478 if (flags
& MA_STACK
)
5479 asciiflags
[1] = 's';
5480 if (flags
& MA_BREAK
)
5481 asciiflags
[2] = 'b';
5482 if (flags
& MA_SHARED
)
5483 asciiflags
[3] = 's';
5484 if (flags
& MA_READ
)
5485 asciiflags
[4] = 'r';
5486 if (flags
& MA_WRITE
)
5487 asciiflags
[5] = 'w';
5488 if (flags
& MA_EXEC
)
5489 asciiflags
[6] = 'x';
5490 return (asciiflags
);
5494 * Function: info_mappings_callback
5496 * Callback function, does the actual work for 'info proc mappings'.
5501 info_mappings_callback (struct prmap
*map
, int (*ignore
) (), void *unused
)
5503 char *data_fmt_string
;
5505 if (TARGET_ADDR_BIT
== 32)
5506 data_fmt_string
= "\t%#10lx %#10lx %#10x %#10x %7s\n";
5508 data_fmt_string
= " %#18lx %#18lx %#10x %#10x %7s\n";
5510 printf_filtered (data_fmt_string
,
5511 (unsigned long) map
->pr_vaddr
,
5512 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5514 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5515 (unsigned int) map
->pr_offset
,
5519 mappingflags (map
->pr_mflags
));
5525 * Function: info_proc_mappings
5527 * Implement the "info proc mappings" subcommand.
5531 info_proc_mappings (procinfo
*pi
, int summary
)
5533 char *header_fmt_string
;
5535 if (TARGET_PTR_BIT
== 32)
5536 header_fmt_string
= "\t%10s %10s %10s %10s %7s\n";
5538 header_fmt_string
= " %18s %18s %10s %10s %7s\n";
5541 return; /* No output for summary mode. */
5543 printf_filtered ("Mapped address spaces:\n\n");
5544 printf_filtered (header_fmt_string
,
5551 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
5552 printf_filtered ("\n");
5556 * Function: info_proc_cmd
5558 * Implement the "info proc" command.
5562 info_proc_cmd (char *args
, int from_tty
)
5564 struct cleanup
*old_chain
;
5565 procinfo
*process
= NULL
;
5566 procinfo
*thread
= NULL
;
5573 old_chain
= make_cleanup (null_cleanup
, 0);
5576 if ((argv
= buildargv (args
)) == NULL
)
5579 make_cleanup_freeargv (argv
);
5581 while (argv
!= NULL
&& *argv
!= NULL
)
5583 if (isdigit (argv
[0][0]))
5585 pid
= strtoul (argv
[0], &tmp
, 10);
5587 tid
= strtoul (++tmp
, NULL
, 10);
5589 else if (argv
[0][0] == '/')
5591 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5593 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
5604 pid
= PIDGET (inferior_ptid
);
5606 error ("No current process: you must name one.");
5609 /* Have pid, will travel.
5610 First see if it's a process we're already debugging. */
5611 process
= find_procinfo (pid
, 0);
5612 if (process
== NULL
)
5614 /* No. So open a procinfo for it, but
5615 remember to close it again when finished. */
5616 process
= create_procinfo (pid
, 0);
5617 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5618 if (!open_procinfo_files (process
, FD_CTL
))
5619 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5623 thread
= create_procinfo (pid
, tid
);
5627 printf_filtered ("process %d flags:\n", process
->pid
);
5628 proc_prettyprint_flags (proc_flags (process
), 1);
5629 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5630 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5631 if (proc_get_nthreads (process
) > 1)
5632 printf_filtered ("Process has %d threads.\n",
5633 proc_get_nthreads (process
));
5637 printf_filtered ("thread %d flags:\n", thread
->tid
);
5638 proc_prettyprint_flags (proc_flags (thread
), 1);
5639 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5640 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5645 info_proc_mappings (process
, 0);
5648 do_cleanups (old_chain
);
5652 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5658 if (PIDGET (inferior_ptid
) <= 0)
5659 error ("you must be debugging a process to use this command.");
5661 if (args
== NULL
|| args
[0] == 0)
5662 error_no_arg ("system call to trace");
5664 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5665 if (isdigit (args
[0]))
5667 syscallnum
= atoi (args
);
5668 if (entry_or_exit
== PR_SYSENTRY
)
5669 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5671 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5674 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5676 if (mode
== FLAG_SET
)
5677 gdb_praddsysset (sysset
, syscallnum
);
5679 gdb_prdelsysset (sysset
, syscallnum
);
5681 if (entry_or_exit
== PR_SYSENTRY
)
5683 if (!proc_set_traced_sysentry (pi
, sysset
))
5684 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5688 if (!proc_set_traced_sysexit (pi
, sysset
))
5689 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5695 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5697 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5701 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5703 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5707 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5709 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5713 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5715 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5720 _initialize_procfs (void)
5723 add_target (&procfs_ops
);
5724 add_info ("proc", info_proc_cmd
,
5725 "Show /proc process information about any running process.\n\
5726 Specify process id, or use the program being debugged by default.\n\
5727 Specify keyword 'mappings' for detailed info on memory mappings.");
5728 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5729 "Give a trace of entries into the syscall.");
5730 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5731 "Give a trace of exits from the syscall.");
5732 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5733 "Cancel a trace of entries into the syscall.");
5734 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5735 "Cancel a trace of exits from the syscall.");
5738 /* =================== END, GDB "MODULE" =================== */
5742 /* miscellaneous stubs: */
5743 /* The following satisfy a few random symbols mostly created by */
5744 /* the solaris threads implementation, which I will chase down */
5748 * Return a pid for which we guarantee
5749 * we will be able to find a 'live' procinfo.
5753 procfs_first_available (void)
5755 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);
5758 /* =================== GCORE .NOTE "MODULE" =================== */
5759 #if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT)
5760 /* gcore only implemented on solaris and unixware (so far) */
5763 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
5764 char *note_data
, int *note_size
)
5766 gdb_gregset_t gregs
;
5767 gdb_fpregset_t fpregs
;
5768 unsigned long merged_pid
;
5770 merged_pid
= TIDGET (ptid
) << 16 | PIDGET (ptid
);
5772 fill_gregset (&gregs
, -1);
5773 #if defined (UNIXWARE)
5774 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
5781 note_data
= (char *) elfcore_write_prstatus (obfd
,
5788 fill_fpregset (&fpregs
, -1);
5789 note_data
= (char *) elfcore_write_prfpreg (obfd
,
5797 struct procfs_corefile_thread_data
{
5804 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
5806 struct procfs_corefile_thread_data
*args
= data
;
5808 if (pi
!= NULL
&& thread
->tid
!= 0)
5810 ptid_t saved_ptid
= inferior_ptid
;
5811 inferior_ptid
= MERGEPID (pi
->pid
, thread
->tid
);
5812 args
->note_data
= procfs_do_thread_registers (args
->obfd
, inferior_ptid
,
5815 inferior_ptid
= saved_ptid
;
5821 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5823 struct cleanup
*old_chain
;
5824 gdb_gregset_t gregs
;
5825 gdb_fpregset_t fpregs
;
5826 char fname
[16] = {'\0'};
5827 char psargs
[80] = {'\0'};
5828 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5829 char *note_data
= NULL
;
5831 struct procfs_corefile_thread_data thread_args
;
5833 if (get_exec_file (0))
5835 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
5836 strncpy (psargs
, get_exec_file (0),
5839 inf_args
= get_inferior_args ();
5840 if (inf_args
&& *inf_args
&&
5841 strlen (inf_args
) < ((int) sizeof (psargs
) - (int) strlen (psargs
)))
5843 strncat (psargs
, " ",
5844 sizeof (psargs
) - strlen (psargs
));
5845 strncat (psargs
, inf_args
,
5846 sizeof (psargs
) - strlen (psargs
));
5850 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
5857 fill_gregset (&gregs
, -1);
5858 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
5859 PIDGET (inferior_ptid
),
5860 stop_signal
, &gregs
);
5863 thread_args
.obfd
= obfd
;
5864 thread_args
.note_data
= note_data
;
5865 thread_args
.note_size
= note_size
;
5866 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
, &thread_args
);
5868 if (thread_args
.note_data
== note_data
)
5870 /* iterate_over_threads didn't come up with any threads;
5871 just use inferior_ptid. */
5872 note_data
= procfs_do_thread_registers (obfd
, inferior_ptid
,
5873 note_data
, note_size
);
5877 note_data
= thread_args
.note_data
;
5880 make_cleanup (xfree
, note_data
);
5883 #else /* !(Solaris or Unixware) */
5885 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5887 error ("gcore not implemented for this host.");
5888 return NULL
; /* lint */
5890 #endif /* Solaris or Unixware */
5891 /* =================== END GCORE .NOTE "MODULE" =================== */