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_info
= child_terminal_info
;
175 procfs_ops
.to_find_new_threads
= procfs_find_new_threads
;
176 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
177 procfs_ops
.to_pid_to_str
= procfs_pid_to_str
;
179 procfs_ops
.to_has_all_memory
= 1;
180 procfs_ops
.to_has_memory
= 1;
181 procfs_ops
.to_has_execution
= 1;
182 procfs_ops
.to_has_stack
= 1;
183 procfs_ops
.to_has_registers
= 1;
184 procfs_ops
.to_stratum
= process_stratum
;
185 procfs_ops
.to_has_thread_control
= tc_schedlock
;
186 procfs_ops
.to_find_memory_regions
= proc_find_memory_regions
;
187 procfs_ops
.to_make_corefile_notes
= procfs_make_note_section
;
188 procfs_ops
.to_can_use_hw_breakpoint
= procfs_can_use_hw_breakpoint
;
189 procfs_ops
.to_magic
= OPS_MAGIC
;
192 /* =================== END, TARGET_OPS "MODULE" =================== */
197 * Put any typedefs, defines etc. here that are required for
198 * the unification of code that handles different versions of /proc.
201 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
203 enum { READ_WATCHFLAG
= WA_READ
,
204 WRITE_WATCHFLAG
= WA_WRITE
,
205 EXEC_WATCHFLAG
= WA_EXEC
,
206 AFTER_WATCHFLAG
= WA_TRAPAFTER
209 #else /* Irix method for watchpoints */
210 enum { READ_WATCHFLAG
= MA_READ
,
211 WRITE_WATCHFLAG
= MA_WRITE
,
212 EXEC_WATCHFLAG
= MA_EXEC
,
213 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
218 #ifdef HAVE_PR_SIGSET_T
219 typedef pr_sigset_t gdb_sigset_t
;
221 typedef sigset_t gdb_sigset_t
;
225 #ifdef HAVE_PR_SIGACTION64_T
226 typedef pr_sigaction64_t gdb_sigaction_t
;
228 typedef struct sigaction gdb_sigaction_t
;
232 #ifdef HAVE_PR_SIGINFO64_T
233 typedef pr_siginfo64_t gdb_siginfo_t
;
235 typedef struct siginfo gdb_siginfo_t
;
238 /* gdb_premptysysset */
240 #define gdb_premptysysset premptysysset
242 #define gdb_premptysysset premptyset
247 #define gdb_praddsysset praddsysset
249 #define gdb_praddsysset praddset
254 #define gdb_prdelsysset prdelsysset
256 #define gdb_prdelsysset prdelset
259 /* prissyssetmember */
260 #ifdef prissyssetmember
261 #define gdb_pr_issyssetmember prissyssetmember
263 #define gdb_pr_issyssetmember prismember
266 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
267 as intuitively descriptive as it could be, so we'll define
268 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
269 this writing, this feature is only found on AIX5 systems and
270 basically means that the set of syscalls is not fixed. I.e,
271 there's no nice table that one can #include to get all of the
272 syscall numbers. Instead, they're stored in /proc/PID/sysent
273 for each process. We are at least guaranteed that they won't
274 change over the lifetime of the process. But each process could
275 (in theory) have different syscall numbers.
277 #ifdef HAVE_PRSYSENT_T
278 #define DYNAMIC_SYSCALLS
283 /* =================== STRUCT PROCINFO "MODULE" =================== */
285 /* FIXME: this comment will soon be out of date W.R.T. threads. */
287 /* The procinfo struct is a wrapper to hold all the state information
288 concerning a /proc process. There should be exactly one procinfo
289 for each process, and since GDB currently can debug only one
290 process at a time, that means there should be only one procinfo.
291 All of the LWP's of a process can be accessed indirectly thru the
292 single process procinfo.
294 However, against the day when GDB may debug more than one process,
295 this data structure is kept in a list (which for now will hold no
296 more than one member), and many functions will have a pointer to a
297 procinfo as an argument.
299 There will be a separate procinfo structure for use by the (not yet
300 implemented) "info proc" command, so that we can print useful
301 information about any random process without interfering with the
302 inferior's procinfo information. */
305 /* format strings for /proc paths */
306 # ifndef CTL_PROC_NAME_FMT
307 # define MAIN_PROC_NAME_FMT "/proc/%d"
308 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
309 # define AS_PROC_NAME_FMT "/proc/%d/as"
310 # define MAP_PROC_NAME_FMT "/proc/%d/map"
311 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
312 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
314 /* the name of the proc status struct depends on the implementation */
315 typedef pstatus_t gdb_prstatus_t
;
316 typedef lwpstatus_t gdb_lwpstatus_t
;
317 #else /* ! NEW_PROC_API */
318 /* format strings for /proc paths */
319 # ifndef CTL_PROC_NAME_FMT
320 # define MAIN_PROC_NAME_FMT "/proc/%05d"
321 # define CTL_PROC_NAME_FMT "/proc/%05d"
322 # define AS_PROC_NAME_FMT "/proc/%05d"
323 # define MAP_PROC_NAME_FMT "/proc/%05d"
324 # define STATUS_PROC_NAME_FMT "/proc/%05d"
325 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
327 /* the name of the proc status struct depends on the implementation */
328 typedef prstatus_t gdb_prstatus_t
;
329 typedef prstatus_t gdb_lwpstatus_t
;
330 #endif /* NEW_PROC_API */
332 typedef struct procinfo
{
333 struct procinfo
*next
;
334 int pid
; /* Process ID */
335 int tid
; /* Thread/LWP id */
339 int ignore_next_sigstop
;
341 /* The following four fd fields may be identical, or may contain
342 several different fd's, depending on the version of /proc
343 (old ioctl or new read/write). */
345 int ctl_fd
; /* File descriptor for /proc control file */
347 * The next three file descriptors are actually only needed in the
348 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
349 * However, to avoid a bunch of #ifdefs in the code, we will use
350 * them uniformly by (in the case of the ioctl single-file-descriptor
351 * implementation) filling them with copies of the control fd.
353 int status_fd
; /* File descriptor for /proc status file */
354 int as_fd
; /* File descriptor for /proc as file */
356 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
358 fltset_t saved_fltset
; /* Saved traced hardware fault set */
359 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
360 gdb_sigset_t saved_sighold
; /* Saved held signal set */
361 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
362 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
364 gdb_prstatus_t prstatus
; /* Current process status info */
367 gdb_fpregset_t fpregset
; /* Current floating point registers */
370 #ifdef DYNAMIC_SYSCALLS
371 int num_syscalls
; /* Total number of syscalls */
372 char **syscall_names
; /* Syscall number to name map */
375 struct procinfo
*thread_list
;
377 int status_valid
: 1;
379 int fpregs_valid
: 1;
380 int threads_valid
: 1;
383 static char errmsg
[128]; /* shared error msg buffer */
385 /* Function prototypes for procinfo module: */
387 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
388 static procinfo
*find_procinfo (int pid
, int tid
);
389 static procinfo
*create_procinfo (int pid
, int tid
);
390 static void destroy_procinfo (procinfo
* p
);
391 static void do_destroy_procinfo_cleanup (void *);
392 static void dead_procinfo (procinfo
* p
, char *msg
, int killp
);
393 static int open_procinfo_files (procinfo
* p
, int which
);
394 static void close_procinfo_files (procinfo
* p
);
395 static int sysset_t_size (procinfo
*p
);
396 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
397 #ifdef DYNAMIC_SYSCALLS
398 static void load_syscalls (procinfo
*pi
);
399 static void free_syscalls (procinfo
*pi
);
400 static int find_syscall (procinfo
*pi
, char *name
);
401 #endif /* DYNAMIC_SYSCALLS */
403 /* The head of the procinfo list: */
404 static procinfo
* procinfo_list
;
407 * Function: find_procinfo
409 * Search the procinfo list.
411 * Returns: pointer to procinfo, or NULL if not found.
415 find_procinfo (int pid
, int tid
)
419 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
426 /* Don't check threads_valid. If we're updating the
427 thread_list, we want to find whatever threads are already
428 here. This means that in general it is the caller's
429 responsibility to check threads_valid and update before
430 calling find_procinfo, if the caller wants to find a new
433 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
442 * Function: find_procinfo_or_die
444 * Calls find_procinfo, but errors on failure.
448 find_procinfo_or_die (int pid
, int tid
)
450 procinfo
*pi
= find_procinfo (pid
, tid
);
455 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
458 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
463 /* open_with_retry() is a wrapper for open(). The appropriate
464 open() call is attempted; if unsuccessful, it will be retried as
465 many times as needed for the EAGAIN and EINTR conditions.
467 For other conditions, open_with_retry() will retry the open() a
468 limited number of times. In addition, a short sleep is imposed
469 prior to retrying the open(). The reason for this sleep is to give
470 the kernel a chance to catch up and create the file in question in
471 the event that GDB "wins" the race to open a file before the kernel
475 open_with_retry (const char *pathname
, int flags
)
477 int retries_remaining
, status
;
479 retries_remaining
= 2;
483 status
= open (pathname
, flags
);
485 if (status
>= 0 || retries_remaining
== 0)
487 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
498 * Function: open_procinfo_files
500 * Open the file descriptor for the process or LWP.
501 * ifdef NEW_PROC_API, we only open the control file descriptor;
502 * the others are opened lazily as needed.
503 * else (if not NEW_PROC_API), there is only one real
504 * file descriptor, but we keep multiple copies of it so that
505 * the code that uses them does not have to be #ifdef'd.
507 * Return: file descriptor, or zero for failure.
510 enum { FD_CTL
, FD_STATUS
, FD_AS
};
513 open_procinfo_files (procinfo
*pi
, int which
)
516 char tmp
[MAX_PROC_NAME_SIZE
];
521 * This function is getting ALMOST long enough to break up into several.
522 * Here is some rationale:
524 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
525 * There are several file descriptors that may need to be open
526 * for any given process or LWP. The ones we're intereted in are:
527 * - control (ctl) write-only change the state
528 * - status (status) read-only query the state
529 * - address space (as) read/write access memory
530 * - map (map) read-only virtual addr map
531 * Most of these are opened lazily as they are needed.
532 * The pathnames for the 'files' for an LWP look slightly
533 * different from those of a first-class process:
534 * Pathnames for a process (<proc-id>):
535 * /proc/<proc-id>/ctl
536 * /proc/<proc-id>/status
538 * /proc/<proc-id>/map
539 * Pathnames for an LWP (lwp-id):
540 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
541 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
542 * An LWP has no map or address space file descriptor, since
543 * the memory map and address space are shared by all LWPs.
545 * Everyone else (Solaris 2.5, Irix, OSF)
546 * There is only one file descriptor for each process or LWP.
547 * For convenience, we copy the same file descriptor into all
548 * three fields of the procinfo struct (ctl_fd, status_fd, and
549 * as_fd, see NEW_PROC_API above) so that code that uses them
550 * doesn't need any #ifdef's.
555 * Each LWP has an independent file descriptor, but these
556 * are not obtained via the 'open' system call like the rest:
557 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
558 * to the file descriptor of the parent process.
561 * These do not even have their own independent file descriptor.
562 * All operations are carried out on the file descriptor of the
563 * parent process. Therefore we just call open again for each
564 * thread, getting a new handle for the same 'file'.
569 * In this case, there are several different file descriptors that
570 * we might be asked to open. The control file descriptor will be
571 * opened early, but the others will be opened lazily as they are
575 strcpy (tmp
, pi
->pathname
);
576 switch (which
) { /* which file descriptor to open? */
579 strcat (tmp
, "/lwpctl");
581 strcat (tmp
, "/ctl");
582 fd
= open_with_retry (tmp
, O_WRONLY
);
589 return 0; /* there is no 'as' file descriptor for an lwp */
591 fd
= open_with_retry (tmp
, O_RDWR
);
598 strcat (tmp
, "/lwpstatus");
600 strcat (tmp
, "/status");
601 fd
= open_with_retry (tmp
, O_RDONLY
);
607 return 0; /* unknown file descriptor */
609 #else /* not NEW_PROC_API */
611 * In this case, there is only one file descriptor for each procinfo
612 * (ie. each process or LWP). In fact, only the file descriptor for
613 * the process can actually be opened by an 'open' system call.
614 * The ones for the LWPs have to be obtained thru an IOCTL call
615 * on the process's file descriptor.
617 * For convenience, we copy each procinfo's single file descriptor
618 * into all of the fields occupied by the several file descriptors
619 * of the NEW_PROC_API implementation. That way, the code that uses
620 * them can be written without ifdefs.
624 #ifdef PIOCTSTATUS /* OSF */
625 /* Only one FD; just open it. */
626 if ((fd
= open_with_retry (pi
->pathname
, O_RDWR
)) == 0)
628 #else /* Sol 2.5, Irix, other? */
629 if (pi
->tid
== 0) /* Master procinfo for the process */
631 fd
= open_with_retry (pi
->pathname
, O_RDWR
);
635 else /* LWP thread procinfo */
637 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
641 /* Find the procinfo for the entire process. */
642 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
645 /* Now obtain the file descriptor for the LWP. */
646 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
648 #else /* Irix, other? */
649 return 0; /* Don't know how to open threads */
650 #endif /* Sol 2.5 PIOCOPENLWP */
652 #endif /* OSF PIOCTSTATUS */
653 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
654 #endif /* NEW_PROC_API */
656 return 1; /* success */
660 * Function: create_procinfo
662 * Allocate a data structure and link it into the procinfo list.
663 * (First tries to find a pre-existing one (FIXME: why?)
665 * Return: pointer to new procinfo struct.
669 create_procinfo (int pid
, int tid
)
671 procinfo
*pi
, *parent
;
673 if ((pi
= find_procinfo (pid
, tid
)))
674 return pi
; /* Already exists, nothing to do. */
676 /* find parent before doing malloc, to save having to cleanup */
678 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
680 doesn't exist yet? */
682 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
683 memset (pi
, 0, sizeof (procinfo
));
687 #ifdef DYNAMIC_SYSCALLS
691 pi
->saved_entryset
= sysset_t_alloc (pi
);
692 pi
->saved_exitset
= sysset_t_alloc (pi
);
694 /* Chain into list. */
697 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
698 pi
->next
= procinfo_list
;
704 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
706 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
708 pi
->next
= parent
->thread_list
;
709 parent
->thread_list
= pi
;
715 * Function: close_procinfo_files
717 * Close all file descriptors associated with the procinfo
721 close_procinfo_files (procinfo
*pi
)
728 if (pi
->status_fd
> 0)
729 close (pi
->status_fd
);
731 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
735 * Function: destroy_procinfo
737 * Destructor function. Close, unlink and deallocate the object.
741 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
745 /* Step one: unlink the procinfo from its list */
749 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
752 ptr
->next
= pi
->next
;
756 /* Step two: close any open file descriptors */
757 close_procinfo_files (pi
);
759 /* Step three: free the memory. */
760 #ifdef DYNAMIC_SYSCALLS
763 xfree (pi
->saved_entryset
);
764 xfree (pi
->saved_exitset
);
769 destroy_procinfo (procinfo
*pi
)
773 if (pi
->tid
!= 0) /* destroy a thread procinfo */
775 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
776 destroy_one_procinfo (&tmp
->thread_list
, pi
);
778 else /* destroy a process procinfo and all its threads */
780 /* First destroy the children, if any; */
781 while (pi
->thread_list
!= NULL
)
782 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
783 /* Then destroy the parent. Genocide!!! */
784 destroy_one_procinfo (&procinfo_list
, pi
);
789 do_destroy_procinfo_cleanup (void *pi
)
791 destroy_procinfo (pi
);
794 enum { NOKILL
, KILL
};
797 * Function: dead_procinfo
799 * To be called on a non_recoverable error for a procinfo.
800 * Prints error messages, optionally sends a SIGKILL to the process,
801 * then destroys the data structure.
805 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
811 print_sys_errmsg (pi
->pathname
, errno
);
815 sprintf (procfile
, "process %d", pi
->pid
);
816 print_sys_errmsg (procfile
, errno
);
819 kill (pi
->pid
, SIGKILL
);
821 destroy_procinfo (pi
);
826 * Function: sysset_t_size
828 * Returns the (complete) size of a sysset_t struct. Normally, this
829 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
830 * size of sysset_t isn't known until runtime.
834 sysset_t_size (procinfo
* pi
)
836 #ifndef DYNAMIC_SYSCALLS
837 return sizeof (sysset_t
);
839 return sizeof (sysset_t
) - sizeof (uint64_t)
840 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
841 / (8 * sizeof (uint64_t)));
845 /* Function: sysset_t_alloc
847 Allocate and (partially) initialize a sysset_t struct. */
850 sysset_t_alloc (procinfo
* pi
)
853 int size
= sysset_t_size (pi
);
854 ret
= xmalloc (size
);
855 #ifdef DYNAMIC_SYSCALLS
856 ret
->pr_size
= (pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
857 / (8 * sizeof (uint64_t));
862 #ifdef DYNAMIC_SYSCALLS
864 /* Function: load_syscalls
866 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
867 pi->num_syscalls with the number of syscalls and pi->syscall_names
868 with the names. (Certain numbers may be skipped in which case the
869 names for these numbers will be left as NULL.) */
871 #define MAX_SYSCALL_NAME_LENGTH 256
872 #define MAX_SYSCALLS 65536
875 load_syscalls (procinfo
*pi
)
877 char pathname
[MAX_PROC_NAME_SIZE
];
880 prsyscall_t
*syscalls
;
881 int i
, size
, maxcall
;
883 pi
->num_syscalls
= 0;
884 pi
->syscall_names
= 0;
886 /* Open the file descriptor for the sysent file */
887 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
888 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
891 error ("load_syscalls: Can't open /proc/%d/sysent", pi
->pid
);
894 size
= sizeof header
- sizeof (prsyscall_t
);
895 if (read (sysent_fd
, &header
, size
) != size
)
897 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
900 if (header
.pr_nsyscalls
== 0)
902 error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi
->pid
);
905 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
906 syscalls
= xmalloc (size
);
908 if (read (sysent_fd
, syscalls
, size
) != size
)
911 error ("load_syscalls: Error reading /proc/%d/sysent", pi
->pid
);
914 /* Find maximum syscall number. This may not be the same as
915 pr_nsyscalls since that value refers to the number of entries
916 in the table. (Also, the docs indicate that some system
917 call numbers may be skipped.) */
919 maxcall
= syscalls
[0].pr_number
;
921 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
922 if (syscalls
[i
].pr_number
> maxcall
923 && syscalls
[i
].pr_nameoff
> 0
924 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
925 maxcall
= syscalls
[i
].pr_number
;
927 pi
->num_syscalls
= maxcall
+1;
928 pi
->syscall_names
= xmalloc (pi
->num_syscalls
* sizeof (char *));
930 for (i
= 0; i
< pi
->num_syscalls
; i
++)
931 pi
->syscall_names
[i
] = NULL
;
933 /* Read the syscall names in */
934 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
936 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
940 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
941 || syscalls
[i
].pr_number
< 0
942 || syscalls
[i
].pr_nameoff
<= 0
943 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
944 != (off_t
) syscalls
[i
].pr_nameoff
))
947 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
951 callnum
= syscalls
[i
].pr_number
;
953 if (pi
->syscall_names
[callnum
] != NULL
)
955 /* FIXME: Generate warning */
959 namebuf
[nread
-1] = '\0';
960 size
= strlen (namebuf
) + 1;
961 pi
->syscall_names
[callnum
] = xmalloc (size
);
962 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
963 pi
->syscall_names
[callnum
][size
-1] = '\0';
970 /* Function: free_syscalls
972 Free the space allocated for the syscall names from the procinfo
976 free_syscalls (procinfo
*pi
)
978 if (pi
->syscall_names
)
982 for (i
= 0; i
< pi
->num_syscalls
; i
++)
983 if (pi
->syscall_names
[i
] != NULL
)
984 xfree (pi
->syscall_names
[i
]);
986 xfree (pi
->syscall_names
);
987 pi
->syscall_names
= 0;
991 /* Function: find_syscall
993 Given a name, look up (and return) the corresponding syscall number.
994 If no match is found, return -1. */
997 find_syscall (procinfo
*pi
, char *name
)
1000 for (i
= 0; i
< pi
->num_syscalls
; i
++)
1002 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
1009 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1011 /* =================== /proc "MODULE" =================== */
1014 * This "module" is the interface layer between the /proc system API
1015 * and the gdb target vector functions. This layer consists of
1016 * access functions that encapsulate each of the basic operations
1017 * that we need to use from the /proc API.
1019 * The main motivation for this layer is to hide the fact that
1020 * there are two very different implementations of the /proc API.
1021 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1022 * functions, we do our best to hide them all in here.
1025 int proc_get_status (procinfo
* pi
);
1026 long proc_flags (procinfo
* pi
);
1027 int proc_why (procinfo
* pi
);
1028 int proc_what (procinfo
* pi
);
1029 int proc_set_run_on_last_close (procinfo
* pi
);
1030 int proc_unset_run_on_last_close (procinfo
* pi
);
1031 int proc_set_inherit_on_fork (procinfo
* pi
);
1032 int proc_unset_inherit_on_fork (procinfo
* pi
);
1033 int proc_set_async (procinfo
* pi
);
1034 int proc_unset_async (procinfo
* pi
);
1035 int proc_stop_process (procinfo
* pi
);
1036 int proc_trace_signal (procinfo
* pi
, int signo
);
1037 int proc_ignore_signal (procinfo
* pi
, int signo
);
1038 int proc_clear_current_fault (procinfo
* pi
);
1039 int proc_set_current_signal (procinfo
* pi
, int signo
);
1040 int proc_clear_current_signal (procinfo
* pi
);
1041 int proc_set_gregs (procinfo
* pi
);
1042 int proc_set_fpregs (procinfo
* pi
);
1043 int proc_wait_for_stop (procinfo
* pi
);
1044 int proc_run_process (procinfo
* pi
, int step
, int signo
);
1045 int proc_kill (procinfo
* pi
, int signo
);
1046 int proc_parent_pid (procinfo
* pi
);
1047 int proc_get_nthreads (procinfo
* pi
);
1048 int proc_get_current_thread (procinfo
* pi
);
1049 int proc_set_held_signals (procinfo
* pi
, gdb_sigset_t
* sighold
);
1050 int proc_set_traced_sysexit (procinfo
* pi
, sysset_t
* sysset
);
1051 int proc_set_traced_sysentry (procinfo
* pi
, sysset_t
* sysset
);
1052 int proc_set_traced_faults (procinfo
* pi
, fltset_t
* fltset
);
1053 int proc_set_traced_signals (procinfo
* pi
, gdb_sigset_t
* sigset
);
1055 int proc_update_threads (procinfo
* pi
);
1056 int proc_iterate_over_threads (procinfo
* pi
,
1057 int (*func
) (procinfo
*, procinfo
*, void *),
1060 gdb_gregset_t
*proc_get_gregs (procinfo
* pi
);
1061 gdb_fpregset_t
*proc_get_fpregs (procinfo
* pi
);
1062 sysset_t
*proc_get_traced_sysexit (procinfo
* pi
, sysset_t
* save
);
1063 sysset_t
*proc_get_traced_sysentry (procinfo
* pi
, sysset_t
* save
);
1064 fltset_t
*proc_get_traced_faults (procinfo
* pi
, fltset_t
* save
);
1065 gdb_sigset_t
*proc_get_traced_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1066 gdb_sigset_t
*proc_get_held_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1067 gdb_sigset_t
*proc_get_pending_signals (procinfo
* pi
, gdb_sigset_t
* save
);
1068 gdb_sigaction_t
*proc_get_signal_actions (procinfo
* pi
, gdb_sigaction_t
*save
);
1070 void proc_warn (procinfo
* pi
, char *func
, int line
);
1071 void proc_error (procinfo
* pi
, char *func
, int line
);
1074 proc_warn (procinfo
*pi
, char *func
, int line
)
1076 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1077 print_sys_errmsg (errmsg
, errno
);
1081 proc_error (procinfo
*pi
, char *func
, int line
)
1083 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1084 perror_with_name (errmsg
);
1088 * Function: proc_get_status
1090 * Updates the status struct in the procinfo.
1091 * There is a 'valid' flag, to let other functions know when
1092 * this function needs to be called (so the status is only
1093 * read when it is needed). The status file descriptor is
1094 * also only opened when it is needed.
1096 * Return: non-zero for success, zero for failure.
1100 proc_get_status (procinfo
*pi
)
1102 /* Status file descriptor is opened "lazily" */
1103 if (pi
->status_fd
== 0 &&
1104 open_procinfo_files (pi
, FD_STATUS
) == 0)
1106 pi
->status_valid
= 0;
1111 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1112 pi
->status_valid
= 0; /* fail */
1115 /* Sigh... I have to read a different data structure,
1116 depending on whether this is a main process or an LWP. */
1118 pi
->status_valid
= (read (pi
->status_fd
,
1119 (char *) &pi
->prstatus
.pr_lwp
,
1120 sizeof (lwpstatus_t
))
1121 == sizeof (lwpstatus_t
));
1124 pi
->status_valid
= (read (pi
->status_fd
,
1125 (char *) &pi
->prstatus
,
1126 sizeof (gdb_prstatus_t
))
1127 == sizeof (gdb_prstatus_t
));
1128 #if 0 /*def UNIXWARE*/
1129 if (pi
->status_valid
&&
1130 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
1131 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
1132 /* Unixware peculiarity -- read the damn thing again! */
1133 pi
->status_valid
= (read (pi
->status_fd
,
1134 (char *) &pi
->prstatus
,
1135 sizeof (gdb_prstatus_t
))
1136 == sizeof (gdb_prstatus_t
));
1137 #endif /* UNIXWARE */
1140 #else /* ioctl method */
1141 #ifdef PIOCTSTATUS /* osf */
1142 if (pi
->tid
== 0) /* main process */
1144 /* Just read the danged status. Now isn't that simple? */
1146 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1153 tid_t pr_error_thread
;
1154 struct prstatus status
;
1157 thread_status
.pr_count
= 1;
1158 thread_status
.status
.pr_tid
= pi
->tid
;
1159 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1162 memcpy (&pi
->prstatus
, &thread_status
.status
,
1163 sizeof (pi
->prstatus
));
1164 pi
->status_valid
= 1;
1168 /* Just read the danged status. Now isn't that simple? */
1169 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1173 if (pi
->status_valid
)
1175 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1178 proc_get_current_thread (pi
));
1181 /* The status struct includes general regs, so mark them valid too */
1182 pi
->gregs_valid
= pi
->status_valid
;
1184 /* In the read/write multiple-fd model,
1185 the status struct includes the fp regs too, so mark them valid too */
1186 pi
->fpregs_valid
= pi
->status_valid
;
1188 return pi
->status_valid
; /* True if success, false if failure. */
1192 * Function: proc_flags
1194 * returns the process flags (pr_flags field).
1198 proc_flags (procinfo
*pi
)
1200 if (!pi
->status_valid
)
1201 if (!proc_get_status (pi
))
1202 return 0; /* FIXME: not a good failure value (but what is?) */
1206 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1207 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1208 The two sets of flags don't overlap. */
1209 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
1211 return pi
->prstatus
.pr_lwp
.pr_flags
;
1214 return pi
->prstatus
.pr_flags
;
1219 * Function: proc_why
1221 * returns the pr_why field (why the process stopped).
1225 proc_why (procinfo
*pi
)
1227 if (!pi
->status_valid
)
1228 if (!proc_get_status (pi
))
1229 return 0; /* FIXME: not a good failure value (but what is?) */
1232 return pi
->prstatus
.pr_lwp
.pr_why
;
1234 return pi
->prstatus
.pr_why
;
1239 * Function: proc_what
1241 * returns the pr_what field (details of why the process stopped).
1245 proc_what (procinfo
*pi
)
1247 if (!pi
->status_valid
)
1248 if (!proc_get_status (pi
))
1249 return 0; /* FIXME: not a good failure value (but what is?) */
1252 return pi
->prstatus
.pr_lwp
.pr_what
;
1254 return pi
->prstatus
.pr_what
;
1258 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1260 * Function: proc_nsysarg
1262 * returns the pr_nsysarg field (number of args to the current syscall).
1266 proc_nsysarg (procinfo
*pi
)
1268 if (!pi
->status_valid
)
1269 if (!proc_get_status (pi
))
1273 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1275 return pi
->prstatus
.pr_nsysarg
;
1280 * Function: proc_sysargs
1282 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1286 proc_sysargs (procinfo
*pi
)
1288 if (!pi
->status_valid
)
1289 if (!proc_get_status (pi
))
1293 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1295 return (long *) &pi
->prstatus
.pr_sysarg
;
1300 * Function: proc_syscall
1302 * returns the pr_syscall field (id of current syscall if we are in one).
1306 proc_syscall (procinfo
*pi
)
1308 if (!pi
->status_valid
)
1309 if (!proc_get_status (pi
))
1313 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1315 return pi
->prstatus
.pr_syscall
;
1318 #endif /* PIOCSSPCACT */
1321 * Function: proc_cursig:
1323 * returns the pr_cursig field (current signal).
1327 proc_cursig (struct procinfo
*pi
)
1329 if (!pi
->status_valid
)
1330 if (!proc_get_status (pi
))
1331 return 0; /* FIXME: not a good failure value (but what is?) */
1334 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1336 return pi
->prstatus
.pr_cursig
;
1341 * Function: proc_modify_flag
1343 * === I appologize for the messiness of this function.
1344 * === This is an area where the different versions of
1345 * === /proc are more inconsistent than usual. MVS
1347 * Set or reset any of the following process flags:
1348 * PR_FORK -- forked child will inherit trace flags
1349 * PR_RLC -- traced process runs when last /proc file closed.
1350 * PR_KLC -- traced process is killed when last /proc file closed.
1351 * PR_ASYNC -- LWP's get to run/stop independently.
1353 * There are three methods for doing this function:
1354 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1356 * 2) Middle: PIOCSET/PIOCRESET
1358 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1361 * Note: Irix does not define PR_ASYNC.
1362 * Note: OSF does not define PR_KLC.
1363 * Note: OSF is the only one that can ONLY use the oldest method.
1366 * pi -- the procinfo
1367 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1368 * mode -- 1 for set, 0 for reset.
1370 * Returns non-zero for success, zero for failure.
1373 enum { FLAG_RESET
, FLAG_SET
};
1376 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1378 long win
= 0; /* default to fail */
1381 * These operations affect the process as a whole, and applying
1382 * them to an individual LWP has the same meaning as applying them
1383 * to the main process. Therefore, if we're ever called with a
1384 * pointer to an LWP's procinfo, let's substitute the process's
1385 * procinfo and avoid opening the LWP's file descriptor
1390 pi
= find_procinfo_or_die (pi
->pid
, 0);
1392 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1393 /* First normalize the PCUNSET/PCRESET command opcode
1394 (which for no obvious reason has a different definition
1395 from one operating system to the next...) */
1397 #define GDBRESET PCUNSET
1400 #define GDBRESET PCRESET
1404 procfs_ctl_t arg
[2];
1406 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1408 else /* Reset the flag */
1412 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1415 #ifdef PIOCSET /* Irix/Sol5 method */
1416 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1418 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1420 else /* Reset the flag */
1422 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1426 #ifdef PIOCSRLC /* Oldest method: OSF */
1429 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1431 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1433 else /* Clear run-on-last-close */
1435 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1439 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1441 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1443 else /* Clear inherit-on-fork */
1445 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1449 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1456 /* The above operation renders the procinfo's cached pstatus obsolete. */
1457 pi
->status_valid
= 0;
1460 warning ("procfs: modify_flag failed to turn %s %s",
1461 flag
== PR_FORK
? "PR_FORK" :
1462 flag
== PR_RLC
? "PR_RLC" :
1464 flag
== PR_ASYNC
? "PR_ASYNC" :
1467 flag
== PR_KLC
? "PR_KLC" :
1470 mode
== FLAG_RESET
? "off" : "on");
1476 * Function: proc_set_run_on_last_close
1478 * Set the run_on_last_close flag.
1479 * Process with all threads will become runnable
1480 * when debugger closes all /proc fds.
1482 * Returns non-zero for success, zero for failure.
1486 proc_set_run_on_last_close (procinfo
*pi
)
1488 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1492 * Function: proc_unset_run_on_last_close
1494 * Reset the run_on_last_close flag.
1495 * Process will NOT become runnable
1496 * when debugger closes its file handles.
1498 * Returns non-zero for success, zero for failure.
1502 proc_unset_run_on_last_close (procinfo
*pi
)
1504 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1509 * Function: proc_set_kill_on_last_close
1511 * Set the kill_on_last_close flag.
1512 * Process with all threads will be killed when debugger
1513 * closes all /proc fds (or debugger exits or dies).
1515 * Returns non-zero for success, zero for failure.
1519 proc_set_kill_on_last_close (procinfo
*pi
)
1521 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1525 * Function: proc_unset_kill_on_last_close
1527 * Reset the kill_on_last_close flag.
1528 * Process will NOT be killed when debugger
1529 * closes its file handles (or exits or dies).
1531 * Returns non-zero for success, zero for failure.
1535 proc_unset_kill_on_last_close (procinfo
*pi
)
1537 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1542 * Function: proc_set_inherit_on_fork
1544 * Set inherit_on_fork flag.
1545 * If the process forks a child while we are registered for events
1546 * in the parent, then we will also recieve events from the child.
1548 * Returns non-zero for success, zero for failure.
1552 proc_set_inherit_on_fork (procinfo
*pi
)
1554 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1558 * Function: proc_unset_inherit_on_fork
1560 * Reset inherit_on_fork flag.
1561 * If the process forks a child while we are registered for events
1562 * in the parent, then we will NOT recieve events from the child.
1564 * Returns non-zero for success, zero for failure.
1568 proc_unset_inherit_on_fork (procinfo
*pi
)
1570 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1575 * Function: proc_set_async
1577 * Set PR_ASYNC flag.
1578 * If one LWP stops because of a debug event (signal etc.),
1579 * the remaining LWPs will continue to run.
1581 * Returns non-zero for success, zero for failure.
1585 proc_set_async (procinfo
*pi
)
1587 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1591 * Function: proc_unset_async
1593 * Reset PR_ASYNC flag.
1594 * If one LWP stops because of a debug event (signal etc.),
1595 * then all other LWPs will stop as well.
1597 * Returns non-zero for success, zero for failure.
1601 proc_unset_async (procinfo
*pi
)
1603 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1605 #endif /* PR_ASYNC */
1608 * Function: proc_stop_process
1610 * Request the process/LWP to stop. Does not wait.
1611 * Returns non-zero for success, zero for failure.
1615 proc_stop_process (procinfo
*pi
)
1620 * We might conceivably apply this operation to an LWP, and
1621 * the LWP's ctl file descriptor might not be open.
1624 if (pi
->ctl_fd
== 0 &&
1625 open_procinfo_files (pi
, FD_CTL
) == 0)
1630 procfs_ctl_t cmd
= PCSTOP
;
1631 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1632 #else /* ioctl method */
1633 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1634 /* Note: the call also reads the prstatus. */
1637 pi
->status_valid
= 1;
1638 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1641 proc_get_current_thread (pi
));
1650 * Function: proc_wait_for_stop
1652 * Wait for the process or LWP to stop (block until it does).
1653 * Returns non-zero for success, zero for failure.
1657 proc_wait_for_stop (procinfo
*pi
)
1662 * We should never have to apply this operation to any procinfo
1663 * except the one for the main process. If that ever changes
1664 * for any reason, then take out the following clause and
1665 * replace it with one that makes sure the ctl_fd is open.
1669 pi
= find_procinfo_or_die (pi
->pid
, 0);
1673 procfs_ctl_t cmd
= PCWSTOP
;
1674 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1675 /* We been runnin' and we stopped -- need to update status. */
1676 pi
->status_valid
= 0;
1678 #else /* ioctl method */
1679 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1680 /* Above call also refreshes the prstatus. */
1683 pi
->status_valid
= 1;
1684 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1687 proc_get_current_thread (pi
));
1695 * Function: proc_run_process
1697 * Make the process or LWP runnable.
1698 * Options (not all are implemented):
1700 * - clear current fault
1701 * - clear current signal
1702 * - abort the current system call
1703 * - stop as soon as finished with system call
1704 * - (ioctl): set traced signal set
1705 * - (ioctl): set held signal set
1706 * - (ioctl): set traced fault set
1707 * - (ioctl): set start pc (vaddr)
1708 * Always clear the current fault.
1709 * Clear the current signal if 'signo' is zero.
1712 * pi the process or LWP to operate on.
1713 * step if true, set the process or LWP to trap after one instr.
1714 * signo if zero, clear the current signal if any.
1715 * if non-zero, set the current signal to this one.
1717 * Returns non-zero for success, zero for failure.
1721 proc_run_process (procinfo
*pi
, int step
, int signo
)
1727 * We will probably have to apply this operation to individual threads,
1728 * so make sure the control file descriptor is open.
1731 if (pi
->ctl_fd
== 0 &&
1732 open_procinfo_files (pi
, FD_CTL
) == 0)
1737 runflags
= PRCFAULT
; /* always clear current fault */
1742 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1743 proc_set_current_signal (pi
, signo
);
1747 procfs_ctl_t cmd
[2];
1751 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1753 #else /* ioctl method */
1757 memset (&prrun
, 0, sizeof (prrun
));
1758 prrun
.pr_flags
= runflags
;
1759 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1767 * Function: proc_set_traced_signals
1769 * Register to trace signals in the process or LWP.
1770 * Returns non-zero for success, zero for failure.
1774 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1779 * We should never have to apply this operation to any procinfo
1780 * except the one for the main process. If that ever changes
1781 * for any reason, then take out the following clause and
1782 * replace it with one that makes sure the ctl_fd is open.
1786 pi
= find_procinfo_or_die (pi
->pid
, 0);
1792 /* Use char array to avoid alignment issues. */
1793 char sigset
[sizeof (gdb_sigset_t
)];
1797 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1799 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1801 #else /* ioctl method */
1802 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1804 /* The above operation renders the procinfo's cached pstatus obsolete. */
1805 pi
->status_valid
= 0;
1808 warning ("procfs: set_traced_signals failed");
1813 * Function: proc_set_traced_faults
1815 * Register to trace hardware faults in the process or LWP.
1816 * Returns non-zero for success, zero for failure.
1820 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1825 * We should never have to apply this operation to any procinfo
1826 * except the one for the main process. If that ever changes
1827 * for any reason, then take out the following clause and
1828 * replace it with one that makes sure the ctl_fd is open.
1832 pi
= find_procinfo_or_die (pi
->pid
, 0);
1838 /* Use char array to avoid alignment issues. */
1839 char fltset
[sizeof (fltset_t
)];
1843 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1845 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1847 #else /* ioctl method */
1848 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1850 /* The above operation renders the procinfo's cached pstatus obsolete. */
1851 pi
->status_valid
= 0;
1857 * Function: proc_set_traced_sysentry
1859 * Register to trace entry to system calls in the process or LWP.
1860 * Returns non-zero for success, zero for failure.
1864 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1869 * We should never have to apply this operation to any procinfo
1870 * except the one for the main process. If that ever changes
1871 * for any reason, then take out the following clause and
1872 * replace it with one that makes sure the ctl_fd is open.
1876 pi
= find_procinfo_or_die (pi
->pid
, 0);
1880 struct gdb_proc_ctl_pcsentry
{
1882 /* Use char array to avoid alignment issues. */
1883 char sysset
[sizeof (sysset_t
)];
1885 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1887 + sysset_t_size (pi
);
1889 argp
= xmalloc (argp_size
);
1891 argp
->cmd
= PCSENTRY
;
1892 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1894 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1897 #else /* ioctl method */
1898 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1900 /* The above operation renders the procinfo's cached pstatus obsolete. */
1901 pi
->status_valid
= 0;
1907 * Function: proc_set_traced_sysexit
1909 * Register to trace exit from system calls in the process or LWP.
1910 * Returns non-zero for success, zero for failure.
1914 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1919 * We should never have to apply this operation to any procinfo
1920 * except the one for the main process. If that ever changes
1921 * for any reason, then take out the following clause and
1922 * replace it with one that makes sure the ctl_fd is open.
1926 pi
= find_procinfo_or_die (pi
->pid
, 0);
1930 struct gdb_proc_ctl_pcsexit
{
1932 /* Use char array to avoid alignment issues. */
1933 char sysset
[sizeof (sysset_t
)];
1935 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1937 + sysset_t_size (pi
);
1939 argp
= xmalloc (argp_size
);
1941 argp
->cmd
= PCSEXIT
;
1942 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1944 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1947 #else /* ioctl method */
1948 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1950 /* The above operation renders the procinfo's cached pstatus obsolete. */
1951 pi
->status_valid
= 0;
1957 * Function: proc_set_held_signals
1959 * Specify the set of blocked / held signals in the process or LWP.
1960 * Returns non-zero for success, zero for failure.
1964 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1969 * We should never have to apply this operation to any procinfo
1970 * except the one for the main process. If that ever changes
1971 * for any reason, then take out the following clause and
1972 * replace it with one that makes sure the ctl_fd is open.
1976 pi
= find_procinfo_or_die (pi
->pid
, 0);
1982 /* Use char array to avoid alignment issues. */
1983 char hold
[sizeof (gdb_sigset_t
)];
1987 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1988 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1991 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1993 /* The above operation renders the procinfo's cached pstatus obsolete. */
1994 pi
->status_valid
= 0;
2000 * Function: proc_get_pending_signals
2002 * returns the set of signals that are pending in the process or LWP.
2003 * Will also copy the sigset if 'save' is non-zero.
2007 proc_get_pending_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2009 gdb_sigset_t
*ret
= NULL
;
2012 * We should never have to apply this operation to any procinfo
2013 * except the one for the main process. If that ever changes
2014 * for any reason, then take out the following clause and
2015 * replace it with one that makes sure the ctl_fd is open.
2019 pi
= find_procinfo_or_die (pi
->pid
, 0);
2021 if (!pi
->status_valid
)
2022 if (!proc_get_status (pi
))
2026 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
2028 ret
= &pi
->prstatus
.pr_sigpend
;
2031 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2037 * Function: proc_get_signal_actions
2039 * returns the set of signal actions.
2040 * Will also copy the sigactionset if 'save' is non-zero.
2044 proc_get_signal_actions (procinfo
*pi
, gdb_sigaction_t
*save
)
2046 gdb_sigaction_t
*ret
= NULL
;
2049 * We should never have to apply this operation to any procinfo
2050 * except the one for the main process. If that ever changes
2051 * for any reason, then take out the following clause and
2052 * replace it with one that makes sure the ctl_fd is open.
2056 pi
= find_procinfo_or_die (pi
->pid
, 0);
2058 if (!pi
->status_valid
)
2059 if (!proc_get_status (pi
))
2063 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
2065 ret
= &pi
->prstatus
.pr_action
;
2068 memcpy (save
, ret
, sizeof (gdb_sigaction_t
));
2074 * Function: proc_get_held_signals
2076 * returns the set of signals that are held / blocked.
2077 * Will also copy the sigset if 'save' is non-zero.
2081 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2083 gdb_sigset_t
*ret
= NULL
;
2086 * We should never have to apply this operation to any procinfo
2087 * except the one for the main process. If that ever changes
2088 * for any reason, then take out the following clause and
2089 * replace it with one that makes sure the ctl_fd is open.
2093 pi
= find_procinfo_or_die (pi
->pid
, 0);
2096 if (!pi
->status_valid
)
2097 if (!proc_get_status (pi
))
2101 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
2103 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
2104 #endif /* UNIXWARE */
2105 #else /* not NEW_PROC_API */
2107 static gdb_sigset_t sigheld
;
2109 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
2112 #endif /* NEW_PROC_API */
2114 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2120 * Function: proc_get_traced_signals
2122 * returns the set of signals that are traced / debugged.
2123 * Will also copy the sigset if 'save' is non-zero.
2127 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
2129 gdb_sigset_t
*ret
= NULL
;
2132 * We should never have to apply this operation to any procinfo
2133 * except the one for the main process. If that ever changes
2134 * for any reason, then take out the following clause and
2135 * replace it with one that makes sure the ctl_fd is open.
2139 pi
= find_procinfo_or_die (pi
->pid
, 0);
2142 if (!pi
->status_valid
)
2143 if (!proc_get_status (pi
))
2146 ret
= &pi
->prstatus
.pr_sigtrace
;
2149 static gdb_sigset_t sigtrace
;
2151 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
2156 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
2162 * Function: proc_trace_signal
2164 * Add 'signo' to the set of signals that are traced.
2165 * Returns non-zero for success, zero for failure.
2169 proc_trace_signal (procinfo
*pi
, int signo
)
2174 * We should never have to apply this operation to any procinfo
2175 * except the one for the main process. If that ever changes
2176 * for any reason, then take out the following clause and
2177 * replace it with one that makes sure the ctl_fd is open.
2181 pi
= find_procinfo_or_die (pi
->pid
, 0);
2185 if (proc_get_traced_signals (pi
, &temp
))
2187 praddset (&temp
, signo
);
2188 return proc_set_traced_signals (pi
, &temp
);
2192 return 0; /* failure */
2196 * Function: proc_ignore_signal
2198 * Remove 'signo' from the set of signals that are traced.
2199 * Returns non-zero for success, zero for failure.
2203 proc_ignore_signal (procinfo
*pi
, int signo
)
2208 * We should never have to apply this operation to any procinfo
2209 * except the one for the main process. If that ever changes
2210 * for any reason, then take out the following clause and
2211 * replace it with one that makes sure the ctl_fd is open.
2215 pi
= find_procinfo_or_die (pi
->pid
, 0);
2219 if (proc_get_traced_signals (pi
, &temp
))
2221 prdelset (&temp
, signo
);
2222 return proc_set_traced_signals (pi
, &temp
);
2226 return 0; /* failure */
2230 * Function: proc_get_traced_faults
2232 * returns the set of hardware faults that are traced /debugged.
2233 * Will also copy the faultset if 'save' is non-zero.
2237 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
2239 fltset_t
*ret
= NULL
;
2242 * We should never have to apply this operation to any procinfo
2243 * except the one for the main process. If that ever changes
2244 * for any reason, then take out the following clause and
2245 * replace it with one that makes sure the ctl_fd is open.
2249 pi
= find_procinfo_or_die (pi
->pid
, 0);
2252 if (!pi
->status_valid
)
2253 if (!proc_get_status (pi
))
2256 ret
= &pi
->prstatus
.pr_flttrace
;
2259 static fltset_t flttrace
;
2261 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2266 memcpy (save
, ret
, sizeof (fltset_t
));
2272 * Function: proc_get_traced_sysentry
2274 * returns the set of syscalls that are traced /debugged on entry.
2275 * Will also copy the syscall set if 'save' is non-zero.
2279 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
2281 sysset_t
*ret
= NULL
;
2284 * We should never have to apply this operation to any procinfo
2285 * except the one for the main process. If that ever changes
2286 * for any reason, then take out the following clause and
2287 * replace it with one that makes sure the ctl_fd is open.
2291 pi
= find_procinfo_or_die (pi
->pid
, 0);
2294 if (!pi
->status_valid
)
2295 if (!proc_get_status (pi
))
2298 #ifndef DYNAMIC_SYSCALLS
2299 ret
= &pi
->prstatus
.pr_sysentry
;
2300 #else /* DYNAMIC_SYSCALLS */
2302 static sysset_t
*sysentry
;
2306 sysentry
= sysset_t_alloc (pi
);
2308 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2310 if (pi
->prstatus
.pr_sysentry_offset
== 0)
2312 gdb_premptysysset (sysentry
);
2318 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
2320 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
2322 size
= sysset_t_size (pi
);
2323 gdb_premptysysset (sysentry
);
2324 rsize
= read (pi
->status_fd
, sysentry
, size
);
2329 #endif /* DYNAMIC_SYSCALLS */
2330 #else /* !NEW_PROC_API */
2332 static sysset_t sysentry
;
2334 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2337 #endif /* NEW_PROC_API */
2339 memcpy (save
, ret
, sysset_t_size (pi
));
2345 * Function: proc_get_traced_sysexit
2347 * returns the set of syscalls that are traced /debugged on exit.
2348 * Will also copy the syscall set if 'save' is non-zero.
2352 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
2354 sysset_t
* ret
= NULL
;
2357 * We should never have to apply this operation to any procinfo
2358 * except the one for the main process. If that ever changes
2359 * for any reason, then take out the following clause and
2360 * replace it with one that makes sure the ctl_fd is open.
2364 pi
= find_procinfo_or_die (pi
->pid
, 0);
2367 if (!pi
->status_valid
)
2368 if (!proc_get_status (pi
))
2371 #ifndef DYNAMIC_SYSCALLS
2372 ret
= &pi
->prstatus
.pr_sysexit
;
2373 #else /* DYNAMIC_SYSCALLS */
2375 static sysset_t
*sysexit
;
2379 sysexit
= sysset_t_alloc (pi
);
2381 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
2383 if (pi
->prstatus
.pr_sysexit_offset
== 0)
2385 gdb_premptysysset (sysexit
);
2391 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
, SEEK_SET
)
2392 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2394 size
= sysset_t_size (pi
);
2395 gdb_premptysysset (sysexit
);
2396 rsize
= read (pi
->status_fd
, sysexit
, size
);
2401 #endif /* DYNAMIC_SYSCALLS */
2404 static sysset_t sysexit
;
2406 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2411 memcpy (save
, ret
, sysset_t_size (pi
));
2417 * Function: proc_clear_current_fault
2419 * The current fault (if any) is cleared; the associated signal
2420 * will not be sent to the process or LWP when it resumes.
2421 * Returns non-zero for success, zero for failure.
2425 proc_clear_current_fault (procinfo
*pi
)
2430 * We should never have to apply this operation to any procinfo
2431 * except the one for the main process. If that ever changes
2432 * for any reason, then take out the following clause and
2433 * replace it with one that makes sure the ctl_fd is open.
2437 pi
= find_procinfo_or_die (pi
->pid
, 0);
2441 procfs_ctl_t cmd
= PCCFAULT
;
2442 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2445 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2452 * Function: proc_set_current_signal
2454 * Set the "current signal" that will be delivered next to the process.
2455 * NOTE: semantics are different from those of KILL.
2456 * This signal will be delivered to the process or LWP
2457 * immediately when it is resumed (even if the signal is held/blocked);
2458 * it will NOT immediately cause another event of interest, and will NOT
2459 * first trap back to the debugger.
2461 * Returns non-zero for success, zero for failure.
2465 proc_set_current_signal (procinfo
*pi
, int signo
)
2470 /* Use char array to avoid alignment issues. */
2471 char sinfo
[sizeof (gdb_siginfo_t
)];
2473 gdb_siginfo_t
*mysinfo
;
2476 * We should never have to apply this operation to any procinfo
2477 * except the one for the main process. If that ever changes
2478 * for any reason, then take out the following clause and
2479 * replace it with one that makes sure the ctl_fd is open.
2483 pi
= find_procinfo_or_die (pi
->pid
, 0);
2485 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2486 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2487 * receives a PIOCSSIG with a signal identical to the current signal,
2488 * it messes up the current signal. Work around the kernel bug.
2491 signo
== proc_cursig (pi
))
2492 return 1; /* I assume this is a success? */
2495 /* The pointer is just a type alias. */
2496 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2497 mysinfo
->si_signo
= signo
;
2498 mysinfo
->si_code
= 0;
2499 mysinfo
->si_pid
= getpid (); /* ?why? */
2500 mysinfo
->si_uid
= getuid (); /* ?why? */
2504 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2506 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2513 * Function: proc_clear_current_signal
2515 * The current signal (if any) is cleared, and
2516 * is not sent to the process or LWP when it resumes.
2517 * Returns non-zero for success, zero for failure.
2521 proc_clear_current_signal (procinfo
*pi
)
2526 * We should never have to apply this operation to any procinfo
2527 * except the one for the main process. If that ever changes
2528 * for any reason, then take out the following clause and
2529 * replace it with one that makes sure the ctl_fd is open.
2533 pi
= find_procinfo_or_die (pi
->pid
, 0);
2539 /* Use char array to avoid alignment issues. */
2540 char sinfo
[sizeof (gdb_siginfo_t
)];
2542 gdb_siginfo_t
*mysinfo
;
2545 /* The pointer is just a type alias. */
2546 mysinfo
= (gdb_siginfo_t
*) &arg
.sinfo
;
2547 mysinfo
->si_signo
= 0;
2548 mysinfo
->si_code
= 0;
2549 mysinfo
->si_errno
= 0;
2550 mysinfo
->si_pid
= getpid (); /* ?why? */
2551 mysinfo
->si_uid
= getuid (); /* ?why? */
2553 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2556 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2563 * Function: proc_get_gregs
2565 * Get the general registers for the process or LWP.
2566 * Returns non-zero for success, zero for failure.
2570 proc_get_gregs (procinfo
*pi
)
2572 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2573 if (!proc_get_status (pi
))
2577 * OK, sorry about the ifdef's.
2578 * There's three cases instead of two, because
2579 * in this instance Unixware and Solaris/RW differ.
2583 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2584 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2585 #else /* not Unixware */
2586 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2587 #endif /* Unixware */
2588 #else /* not NEW_PROC_API */
2589 return &pi
->prstatus
.pr_reg
;
2590 #endif /* NEW_PROC_API */
2594 * Function: proc_get_fpregs
2596 * Get the floating point registers for the process or LWP.
2597 * Returns non-zero for success, zero for failure.
2601 proc_get_fpregs (procinfo
*pi
)
2604 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2605 if (!proc_get_status (pi
))
2608 #ifdef UNIXWARE /* a true architecture dependency */
2609 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2611 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2612 #endif /* Unixware */
2614 #else /* not NEW_PROC_API */
2615 if (pi
->fpregs_valid
)
2616 return &pi
->fpregset
; /* already got 'em */
2619 if (pi
->ctl_fd
== 0 &&
2620 open_procinfo_files (pi
, FD_CTL
) == 0)
2629 tid_t pr_error_thread
;
2630 tfpregset_t thread_1
;
2633 thread_fpregs
.pr_count
= 1;
2634 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2637 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2639 pi
->fpregs_valid
= 1;
2640 return &pi
->fpregset
; /* got 'em now! */
2642 else if (pi
->tid
!= 0 &&
2643 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2645 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2646 sizeof (pi
->fpregset
));
2647 pi
->fpregs_valid
= 1;
2648 return &pi
->fpregset
; /* got 'em now! */
2655 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2657 pi
->fpregs_valid
= 1;
2658 return &pi
->fpregset
; /* got 'em now! */
2671 * Function: proc_set_gregs
2673 * Write the general registers back to the process or LWP.
2674 * Returns non-zero for success, zero for failure.
2678 proc_set_gregs (procinfo
*pi
)
2680 gdb_gregset_t
*gregs
;
2683 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2684 return 0; /* get_regs has already warned */
2686 if (pi
->ctl_fd
== 0 &&
2687 open_procinfo_files (pi
, FD_CTL
) == 0)
2696 /* Use char array to avoid alignment issues. */
2697 char gregs
[sizeof (gdb_gregset_t
)];
2701 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2702 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2704 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2708 /* Policy: writing the regs invalidates our cache. */
2709 pi
->gregs_valid
= 0;
2714 * Function: proc_set_fpregs
2716 * Modify the floating point register set of the process or LWP.
2717 * Returns non-zero for success, zero for failure.
2721 proc_set_fpregs (procinfo
*pi
)
2723 gdb_fpregset_t
*fpregs
;
2726 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2727 return 0; /* get_fpregs has already warned */
2729 if (pi
->ctl_fd
== 0 &&
2730 open_procinfo_files (pi
, FD_CTL
) == 0)
2739 /* Use char array to avoid alignment issues. */
2740 char fpregs
[sizeof (gdb_fpregset_t
)];
2744 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2745 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2749 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2754 tid_t pr_error_thread
;
2755 tfpregset_t thread_1
;
2758 thread_fpregs
.pr_count
= 1;
2759 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2760 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2762 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2765 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2766 #endif /* osf PIOCTSFPREG */
2767 #endif /* NEW_PROC_API */
2770 /* Policy: writing the regs invalidates our cache. */
2771 pi
->fpregs_valid
= 0;
2776 * Function: proc_kill
2778 * Send a signal to the proc or lwp with the semantics of "kill()".
2779 * Returns non-zero for success, zero for failure.
2783 proc_kill (procinfo
*pi
, int signo
)
2788 * We might conceivably apply this operation to an LWP, and
2789 * the LWP's ctl file descriptor might not be open.
2792 if (pi
->ctl_fd
== 0 &&
2793 open_procinfo_files (pi
, FD_CTL
) == 0)
2800 procfs_ctl_t cmd
[2];
2804 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2805 #else /* ioctl method */
2806 /* FIXME: do I need the Alpha OSF fixups present in
2807 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2808 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2816 * Function: proc_parent_pid
2818 * Find the pid of the process that started this one.
2819 * Returns the parent process pid, or zero.
2823 proc_parent_pid (procinfo
*pi
)
2826 * We should never have to apply this operation to any procinfo
2827 * except the one for the main process. If that ever changes
2828 * for any reason, then take out the following clause and
2829 * replace it with one that makes sure the ctl_fd is open.
2833 pi
= find_procinfo_or_die (pi
->pid
, 0);
2835 if (!pi
->status_valid
)
2836 if (!proc_get_status (pi
))
2839 return pi
->prstatus
.pr_ppid
;
2844 * Function: proc_set_watchpoint
2849 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2851 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2854 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2855 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2860 char watch
[sizeof (prwatch_t
)];
2864 pwatch
= (prwatch_t
*) &arg
.watch
;
2865 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2866 pwatch
->pr_vaddr
= (uintptr_t) address_to_host_pointer (addr
);
2868 pwatch
->pr_vaddr
= (caddr_t
) address_to_host_pointer (addr
);
2870 pwatch
->pr_size
= len
;
2871 pwatch
->pr_wflags
= wflags
;
2872 #if defined(NEW_PROC_API) && defined (PCWATCH)
2874 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2876 #if defined (PIOCSWATCH)
2877 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2879 return 0; /* Fail */
2886 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2888 #include <sys/sysi86.h>
2891 * Function: proc_get_LDT_entry
2897 * The 'key' is actually the value of the lower 16 bits of
2898 * the GS register for the LWP that we're interested in.
2900 * Return: matching ssh struct (LDT entry).
2904 proc_get_LDT_entry (procinfo
*pi
, int key
)
2906 static struct ssd
*ldt_entry
= NULL
;
2908 char pathname
[MAX_PROC_NAME_SIZE
];
2909 struct cleanup
*old_chain
= NULL
;
2912 /* Allocate space for one LDT entry.
2913 This alloc must persist, because we return a pointer to it. */
2914 if (ldt_entry
== NULL
)
2915 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2917 /* Open the file descriptor for the LDT table. */
2918 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2919 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2921 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2924 /* Make sure it gets closed again! */
2925 old_chain
= make_cleanup_close (fd
);
2927 /* Now 'read' thru the table, find a match and return it. */
2928 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2930 if (ldt_entry
->sel
== 0 &&
2931 ldt_entry
->bo
== 0 &&
2932 ldt_entry
->acc1
== 0 &&
2933 ldt_entry
->acc2
== 0)
2934 break; /* end of table */
2935 /* If key matches, return this entry. */
2936 if (ldt_entry
->sel
== key
)
2939 /* Loop ended, match not found. */
2943 static int nalloc
= 0;
2945 /* Get the number of LDT entries. */
2946 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2948 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2952 /* Allocate space for the number of LDT entries. */
2953 /* This alloc has to persist, 'cause we return a pointer to it. */
2956 ldt_entry
= (struct ssd
*)
2957 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2961 /* Read the whole table in one gulp. */
2962 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2964 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2968 /* Search the table and return the (first) entry matching 'key'. */
2969 for (i
= 0; i
< nldt
; i
++)
2970 if (ldt_entry
[i
].sel
== key
)
2971 return &ldt_entry
[i
];
2973 /* Loop ended, match not found. */
2978 #endif /* TM_I386SOL2_H */
2980 /* =============== END, non-thread part of /proc "MODULE" =============== */
2982 /* =================== Thread "MODULE" =================== */
2984 /* NOTE: you'll see more ifdefs and duplication of functions here,
2985 since there is a different way to do threads on every OS. */
2988 * Function: proc_get_nthreads
2990 * Return the number of threads for the process
2993 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2998 proc_get_nthreads (procinfo
*pi
)
3002 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
3003 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
3009 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3011 * Solaris and Unixware version
3014 proc_get_nthreads (procinfo
*pi
)
3016 if (!pi
->status_valid
)
3017 if (!proc_get_status (pi
))
3021 * NEW_PROC_API: only works for the process procinfo,
3022 * because the LWP procinfos do not get prstatus filled in.
3025 if (pi
->tid
!= 0) /* find the parent process procinfo */
3026 pi
= find_procinfo_or_die (pi
->pid
, 0);
3028 return pi
->prstatus
.pr_nlwp
;
3036 proc_get_nthreads (procinfo
*pi
)
3044 * Function: proc_get_current_thread (LWP version)
3046 * Return the ID of the thread that had an event of interest.
3047 * (ie. the one that hit a breakpoint or other traced event).
3048 * All other things being equal, this should be the ID of a
3049 * thread that is currently executing.
3052 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3054 * Solaris and Unixware version
3057 proc_get_current_thread (procinfo
*pi
)
3060 * Note: this should be applied to the root procinfo for the process,
3061 * not to the procinfo for an LWP. If applied to the procinfo for
3062 * an LWP, it will simply return that LWP's ID. In that case,
3063 * find the parent process procinfo.
3067 pi
= find_procinfo_or_die (pi
->pid
, 0);
3069 if (!pi
->status_valid
)
3070 if (!proc_get_status (pi
))
3074 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
3076 return pi
->prstatus
.pr_who
;
3081 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3086 proc_get_current_thread (procinfo
*pi
)
3088 #if 0 /* FIXME: not ready for prime time? */
3089 return pi
->prstatus
.pr_tid
;
3100 proc_get_current_thread (procinfo
*pi
)
3109 * Function: proc_update_threads
3111 * Discover the IDs of all the threads within the process, and
3112 * create a procinfo for each of them (chained to the parent).
3114 * This unfortunately requires a different method on every OS.
3116 * Return: non-zero for success, zero for failure.
3120 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
3122 if (thread
&& parent
) /* sanity */
3124 thread
->status_valid
= 0;
3125 if (!proc_get_status (thread
))
3126 destroy_one_procinfo (&parent
->thread_list
, thread
);
3128 return 0; /* keep iterating */
3131 #if defined (PIOCLSTATUS)
3133 * Solaris 2.5 (ioctl) version
3136 proc_update_threads (procinfo
*pi
)
3138 gdb_prstatus_t
*prstatus
;
3139 struct cleanup
*old_chain
= NULL
;
3144 * We should never have to apply this operation to any procinfo
3145 * except the one for the main process. If that ever changes
3146 * for any reason, then take out the following clause and
3147 * replace it with one that makes sure the ctl_fd is open.
3151 pi
= find_procinfo_or_die (pi
->pid
, 0);
3153 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3155 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
3156 return 1; /* Process is not multi-threaded; nothing to do. */
3158 prstatus
= xmalloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1));
3160 old_chain
= make_cleanup (xfree
, prstatus
);
3161 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
3162 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
3164 /* Skip element zero, which represents the process as a whole. */
3165 for (i
= 1; i
< nlwp
+ 1; i
++)
3167 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
3168 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3170 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
3171 thread
->status_valid
= 1;
3173 pi
->threads_valid
= 1;
3174 do_cleanups (old_chain
);
3180 * Unixware and Solaris 6 (and later) version
3183 do_closedir_cleanup (void *dir
)
3189 proc_update_threads (procinfo
*pi
)
3191 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3192 struct dirent
*direntry
;
3193 struct cleanup
*old_chain
= NULL
;
3199 * We should never have to apply this operation to any procinfo
3200 * except the one for the main process. If that ever changes
3201 * for any reason, then take out the following clause and
3202 * replace it with one that makes sure the ctl_fd is open.
3206 pi
= find_procinfo_or_die (pi
->pid
, 0);
3208 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3213 * Note: this brute-force method is the only way I know of
3214 * to accomplish this task on Unixware. This method will
3215 * also work on Solaris 2.6 and 2.7. There is a much simpler
3216 * and more elegant way to do this on Solaris, but the margins
3217 * of this manuscript are too small to write it here... ;-)
3220 strcpy (pathname
, pi
->pathname
);
3221 strcat (pathname
, "/lwp");
3222 if ((dirp
= opendir (pathname
)) == NULL
)
3223 proc_error (pi
, "update_threads, opendir", __LINE__
);
3225 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
3226 while ((direntry
= readdir (dirp
)) != NULL
)
3227 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3229 lwpid
= atoi (&direntry
->d_name
[0]);
3230 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3231 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3233 pi
->threads_valid
= 1;
3234 do_cleanups (old_chain
);
3243 proc_update_threads (procinfo
*pi
)
3249 * We should never have to apply this operation to any procinfo
3250 * except the one for the main process. If that ever changes
3251 * for any reason, then take out the following clause and
3252 * replace it with one that makes sure the ctl_fd is open.
3256 pi
= find_procinfo_or_die (pi
->pid
, 0);
3258 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3260 nthreads
= proc_get_nthreads (pi
);
3262 return 0; /* nothing to do for 1 or fewer threads */
3264 threads
= xmalloc (nthreads
* sizeof (tid_t
));
3266 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3267 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3269 for (i
= 0; i
< nthreads
; i
++)
3271 if (!find_procinfo (pi
->pid
, threads
[i
]))
3272 if (!create_procinfo (pi
->pid
, threads
[i
]))
3273 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3275 pi
->threads_valid
= 1;
3283 proc_update_threads (procinfo
*pi
)
3287 #endif /* OSF PIOCTLIST */
3288 #endif /* NEW_PROC_API */
3289 #endif /* SOL 2.5 PIOCLSTATUS */
3292 * Function: proc_iterate_over_threads
3295 * Given a pointer to a function, call that function once
3296 * for each lwp in the procinfo list, until the function
3297 * returns non-zero, in which event return the value
3298 * returned by the function.
3300 * Note: this function does NOT call update_threads.
3301 * If you want to discover new threads first, you must
3302 * call that function explicitly. This function just makes
3303 * a quick pass over the currently-known procinfos.
3306 * pi - parent process procinfo
3307 * func - per-thread function
3308 * ptr - opaque parameter for function.
3311 * First non-zero return value from the callee, or zero.
3315 proc_iterate_over_threads (procinfo
*pi
,
3316 int (*func
) (procinfo
*, procinfo
*, void *),
3319 procinfo
*thread
, *next
;
3323 * We should never have to apply this operation to any procinfo
3324 * except the one for the main process. If that ever changes
3325 * for any reason, then take out the following clause and
3326 * replace it with one that makes sure the ctl_fd is open.
3330 pi
= find_procinfo_or_die (pi
->pid
, 0);
3332 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3334 next
= thread
->next
; /* in case thread is destroyed */
3335 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3342 /* =================== END, Thread "MODULE" =================== */
3344 /* =================== END, /proc "MODULE" =================== */
3346 /* =================== GDB "MODULE" =================== */
3349 * Here are all of the gdb target vector functions and their friends.
3352 static ptid_t
do_attach (ptid_t ptid
);
3353 static void do_detach (int signo
);
3354 static int register_gdb_signals (procinfo
*, gdb_sigset_t
*);
3357 * Function: procfs_debug_inferior
3359 * Sets up the inferior to be debugged.
3360 * Registers to trace signals, hardware faults, and syscalls.
3361 * Note: does not set RLC flag: caller may want to customize that.
3363 * Returns: zero for success (note! unlike most functions in this module)
3364 * On failure, returns the LINE NUMBER where it failed!
3368 procfs_debug_inferior (procinfo
*pi
)
3370 fltset_t traced_faults
;
3371 gdb_sigset_t traced_signals
;
3372 sysset_t
*traced_syscall_entries
;
3373 sysset_t
*traced_syscall_exits
;
3376 #ifdef PROCFS_DONT_TRACE_FAULTS
3377 /* On some systems (OSF), we don't trace hardware faults.
3378 Apparently it's enough that we catch them as signals.
3379 Wonder why we don't just do that in general? */
3380 premptyset (&traced_faults
); /* don't trace faults. */
3382 /* Register to trace hardware faults in the child. */
3383 prfillset (&traced_faults
); /* trace all faults... */
3384 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3386 if (!proc_set_traced_faults (pi
, &traced_faults
))
3389 /* Register to trace selected signals in the child. */
3390 premptyset (&traced_signals
);
3391 if (!register_gdb_signals (pi
, &traced_signals
))
3395 /* Register to trace the 'exit' system call (on entry). */
3396 traced_syscall_entries
= sysset_t_alloc (pi
);
3397 gdb_premptysysset (traced_syscall_entries
);
3399 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
3402 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3405 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
3407 #ifdef DYNAMIC_SYSCALLS
3409 int callnum
= find_syscall (pi
, "_exit");
3411 gdb_praddsysset (traced_syscall_entries
, callnum
);
3415 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
3416 xfree (traced_syscall_entries
);
3420 #ifdef PRFS_STOPEXEC /* defined on OSF */
3421 /* OSF method for tracing exec syscalls. Quoting:
3422 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3423 exits from exec system calls because of the user level loader. */
3424 /* FIXME: make nice and maybe move into an access function. */
3428 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3431 prfs_flags
|= PRFS_STOPEXEC
;
3433 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3436 #else /* not PRFS_STOPEXEC */
3437 /* Everyone else's (except OSF) method for tracing exec syscalls */
3439 Not all systems with /proc have all the exec* syscalls with the same
3440 names. On the SGI, for example, there is no SYS_exec, but there
3441 *is* a SYS_execv. So, we try to account for that. */
3443 traced_syscall_exits
= sysset_t_alloc (pi
);
3444 gdb_premptysysset (traced_syscall_exits
);
3446 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
3449 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
3452 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
3455 #ifdef SYS_lwpcreate
3456 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3457 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3460 #ifdef SYS_lwp_create /* FIXME: once only, please */
3461 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3462 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3465 #ifdef DYNAMIC_SYSCALLS
3467 int callnum
= find_syscall (pi
, "execve");
3469 gdb_praddsysset (traced_syscall_exits
, callnum
);
3470 callnum
= find_syscall (pi
, "ra_execve");
3472 gdb_praddsysset (traced_syscall_exits
, callnum
);
3476 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3477 xfree (traced_syscall_exits
);
3481 #endif /* PRFS_STOPEXEC */
3486 procfs_attach (char *args
, int from_tty
)
3492 error_no_arg ("process-id to attach");
3495 if (pid
== getpid ())
3496 error ("Attaching GDB to itself is not a good idea...");
3500 exec_file
= get_exec_file (0);
3503 printf_filtered ("Attaching to program `%s', %s\n",
3504 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3506 printf_filtered ("Attaching to %s\n",
3507 target_pid_to_str (pid_to_ptid (pid
)));
3511 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3512 push_target (&procfs_ops
);
3516 procfs_detach (char *args
, int from_tty
)
3523 exec_file
= get_exec_file (0);
3526 printf_filtered ("Detaching from program: %s %s\n",
3527 exec_file
, target_pid_to_str (inferior_ptid
));
3531 signo
= atoi (args
);
3534 inferior_ptid
= null_ptid
;
3535 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3539 do_attach (ptid_t ptid
)
3544 if ((pi
= create_procinfo (PIDGET (ptid
), 0)) == NULL
)
3545 perror ("procfs: out of memory in 'attach'");
3547 if (!open_procinfo_files (pi
, FD_CTL
))
3549 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3550 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3552 dead_procinfo (pi
, errmsg
, NOKILL
);
3555 /* Stop the process (if it isn't already stopped). */
3556 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3558 pi
->was_stopped
= 1;
3559 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3563 pi
->was_stopped
= 0;
3564 /* Set the process to run again when we close it. */
3565 if (!proc_set_run_on_last_close (pi
))
3566 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3568 /* Now stop the process. */
3569 if (!proc_stop_process (pi
))
3570 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3571 pi
->ignore_next_sigstop
= 1;
3573 /* Save some of the /proc state to be restored if we detach. */
3574 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3575 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3576 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3577 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3578 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3579 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3581 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3582 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3584 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3585 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3587 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3588 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3590 /* Let GDB know that the inferior was attached. */
3592 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3596 do_detach (int signo
)
3600 /* Find procinfo for the main process */
3601 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0); /* FIXME: threads */
3603 if (!proc_set_current_signal (pi
, signo
))
3604 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3606 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3607 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3609 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3610 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3612 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3613 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3615 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3616 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3618 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3619 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3621 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3622 if (signo
|| !(pi
->was_stopped
) ||
3623 query ("Was stopped when attached, make it runnable again? "))
3625 /* Clear any pending signal. */
3626 if (!proc_clear_current_fault (pi
))
3627 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3629 if (!proc_set_run_on_last_close (pi
))
3630 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3634 destroy_procinfo (pi
);
3640 * Since the /proc interface cannot give us individual registers,
3641 * we pay no attention to the (regno) argument, and just fetch them all.
3642 * This results in the possibility that we will do unnecessarily many
3643 * fetches, since we may be called repeatedly for individual registers.
3644 * So we cache the results, and mark the cache invalid when the process
3649 procfs_fetch_registers (int regno
)
3651 gdb_fpregset_t
*fpregs
;
3652 gdb_gregset_t
*gregs
;
3657 pid
= PIDGET (inferior_ptid
);
3658 tid
= TIDGET (inferior_ptid
);
3660 /* First look up procinfo for the main process. */
3661 pi
= find_procinfo_or_die (pid
, 0);
3663 /* If the event thread is not the same as GDB's requested thread
3664 (ie. inferior_ptid), then look up procinfo for the requested
3667 (tid
!= proc_get_current_thread (pi
)))
3668 pi
= find_procinfo_or_die (pid
, tid
);
3671 error ("procfs: fetch_registers failed to find procinfo for %s",
3672 target_pid_to_str (inferior_ptid
));
3674 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3675 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3677 supply_gregset (gregs
);
3679 if (FP0_REGNUM
>= 0) /* need floating point? */
3681 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3682 regno
== PC_REGNUM
||
3683 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3684 regno
== FP_REGNUM
||
3686 return; /* not a floating point register */
3688 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3689 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3691 supply_fpregset (fpregs
);
3695 /* Get ready to modify the registers array. On machines which store
3696 individual registers, this doesn't need to do anything. On
3697 machines which store all the registers in one fell swoop, such as
3698 /proc, this makes sure that registers contains all the registers
3699 from the program being debugged. */
3702 procfs_prepare_to_store (void)
3704 #ifdef CHILD_PREPARE_TO_STORE
3705 CHILD_PREPARE_TO_STORE ();
3712 * Since the /proc interface will not read individual registers,
3713 * we will cache these requests until the process is resumed, and
3714 * only then write them back to the inferior process.
3716 * FIXME: is that a really bad idea? Have to think about cases
3717 * where writing one register might affect the value of others, etc.
3721 procfs_store_registers (int regno
)
3723 gdb_fpregset_t
*fpregs
;
3724 gdb_gregset_t
*gregs
;
3729 pid
= PIDGET (inferior_ptid
);
3730 tid
= TIDGET (inferior_ptid
);
3732 /* First find procinfo for main process */
3733 pi
= find_procinfo_or_die (pid
, 0);
3735 /* If current lwp for process is not the same as requested thread
3736 (ie. inferior_ptid), then find procinfo for the requested thread. */
3739 (tid
!= proc_get_current_thread (pi
)))
3740 pi
= find_procinfo_or_die (pid
, tid
);
3743 error ("procfs: store_registers: failed to find procinfo for %s",
3744 target_pid_to_str (inferior_ptid
));
3746 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3747 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3749 fill_gregset (gregs
, regno
);
3750 if (!proc_set_gregs (pi
))
3751 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3753 if (FP0_REGNUM
>= 0) /* need floating point? */
3755 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3756 regno
== PC_REGNUM
||
3757 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3758 regno
== FP_REGNUM
||
3760 return; /* not a floating point register */
3762 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3763 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3765 fill_fpregset (fpregs
, regno
);
3766 if (!proc_set_fpregs (pi
))
3767 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3772 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3776 if (scall
== SYS_lwp_exit
)
3780 if (scall
== SYS_lwpexit
)
3787 syscall_is_exit (procinfo
*pi
, int scall
)
3790 if (scall
== SYS_exit
)
3793 #ifdef DYNAMIC_SYSCALLS
3794 if (find_syscall (pi
, "_exit") == scall
)
3801 syscall_is_exec (procinfo
*pi
, int scall
)
3804 if (scall
== SYS_exec
)
3808 if (scall
== SYS_execv
)
3812 if (scall
== SYS_execve
)
3815 #ifdef DYNAMIC_SYSCALLS
3816 if (find_syscall (pi
, "_execve"))
3818 if (find_syscall (pi
, "ra_execve"))
3825 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3827 #ifdef SYS_lwp_create
3828 if (scall
== SYS_lwp_create
)
3831 #ifdef SYS_lwpcreate
3832 if (scall
== SYS_lwpcreate
)
3839 * Function: target_wait
3841 * Retrieve the next stop event from the child process.
3842 * If child has not stopped yet, wait for it to stop.
3843 * Translate /proc eventcodes (or possibly wait eventcodes)
3844 * into gdb internal event codes.
3846 * Return: id of process (and possibly thread) that incurred the event.
3847 * event codes are returned thru a pointer parameter.
3851 procfs_wait (ptid_t ptid
, struct target_waitstatus
*status
)
3853 /* First cut: loosely based on original version 2.1 */
3857 ptid_t retval
, temp_ptid
;
3858 int why
, what
, flags
;
3865 retval
= pid_to_ptid (-1);
3867 /* Find procinfo for main process */
3868 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
3871 /* We must assume that the status is stale now... */
3872 pi
->status_valid
= 0;
3873 pi
->gregs_valid
= 0;
3874 pi
->fpregs_valid
= 0;
3876 #if 0 /* just try this out... */
3877 flags
= proc_flags (pi
);
3878 why
= proc_why (pi
);
3879 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3880 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3882 /* If child is not stopped, wait for it to stop. */
3883 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3884 !proc_wait_for_stop (pi
))
3886 /* wait_for_stop failed: has the child terminated? */
3887 if (errno
== ENOENT
)
3891 /* /proc file not found; presumably child has terminated. */
3892 wait_retval
= wait (&wstat
); /* "wait" for the child's exit */
3894 if (wait_retval
!= PIDGET (inferior_ptid
)) /* wrong child? */
3895 error ("procfs: couldn't stop process %d: wait returned %d\n",
3896 PIDGET (inferior_ptid
), wait_retval
);
3897 /* FIXME: might I not just use waitpid?
3898 Or try find_procinfo to see if I know about this child? */
3899 retval
= pid_to_ptid (wait_retval
);
3901 else if (errno
== EINTR
)
3905 /* Unknown error from wait_for_stop. */
3906 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3911 /* This long block is reached if either:
3912 a) the child was already stopped, or
3913 b) we successfully waited for the child with wait_for_stop.
3914 This block will analyze the /proc status, and translate it
3915 into a waitstatus for GDB.
3917 If we actually had to call wait because the /proc file
3918 is gone (child terminated), then we skip this block,
3919 because we already have a waitstatus. */
3921 flags
= proc_flags (pi
);
3922 why
= proc_why (pi
);
3923 what
= proc_what (pi
);
3925 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3928 /* If it's running async (for single_thread control),
3929 set it back to normal again. */
3930 if (flags
& PR_ASYNC
)
3931 if (!proc_unset_async (pi
))
3932 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3936 proc_prettyprint_why (why
, what
, 1);
3938 /* The 'pid' we will return to GDB is composed of
3939 the process ID plus the lwp ID. */
3940 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3944 wstat
= (what
<< 8) | 0177;
3947 if (syscall_is_lwp_exit (pi
, what
))
3949 printf_filtered ("[%s exited]\n",
3950 target_pid_to_str (retval
));
3951 delete_thread (retval
);
3952 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3955 else if (syscall_is_exit (pi
, what
))
3957 /* Handle SYS_exit call only */
3958 /* Stopped at entry to SYS_exit.
3959 Make it runnable, resume it, then use
3960 the wait system call to get its exit code.
3961 Proc_run_process always clears the current
3963 Then return its exit status. */
3964 pi
->status_valid
= 0;
3966 /* FIXME: what we should do is return
3967 TARGET_WAITKIND_SPURIOUS. */
3968 if (!proc_run_process (pi
, 0, 0))
3969 proc_error (pi
, "target_wait, run_process", __LINE__
);
3972 /* Don't call wait: simulate waiting for exit,
3973 return a "success" exit code. Bogus: what if
3974 it returns something else? */
3976 retval
= inferior_ptid
; /* ? ? ? */
3980 int temp
= wait (&wstat
);
3982 /* FIXME: shouldn't I make sure I get the right
3983 event from the right process? If (for
3984 instance) I have killed an earlier inferior
3985 process but failed to clean up after it
3986 somehow, I could get its termination event
3989 /* If wait returns -1, that's what we return to GDB. */
3991 retval
= pid_to_ptid (temp
);
3996 printf_filtered ("procfs: trapped on entry to ");
3997 proc_prettyprint_syscall (proc_what (pi
), 0);
3998 printf_filtered ("\n");
4001 long i
, nsysargs
, *sysargs
;
4003 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4004 (sysargs
= proc_sysargs (pi
)) != NULL
)
4006 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4007 for (i
= 0; i
< nsysargs
; i
++)
4008 printf_filtered ("#%ld: 0x%08lx\n",
4016 /* How to exit gracefully, returning "unknown event" */
4017 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4018 return inferior_ptid
;
4022 /* How to keep going without returning to wfi: */
4023 target_resume (ptid
, 0, TARGET_SIGNAL_0
);
4029 if (syscall_is_exec (pi
, what
))
4031 /* Hopefully this is our own "fork-child" execing
4032 the real child. Hoax this event into a trap, and
4033 GDB will see the child about to execute its start
4035 wstat
= (SIGTRAP
<< 8) | 0177;
4037 else if (syscall_is_lwp_create (pi
, what
))
4040 * This syscall is somewhat like fork/exec.
4041 * We will get the event twice: once for the parent LWP,
4042 * and once for the child. We should already know about
4043 * the parent LWP, but the child will be new to us. So,
4044 * whenever we get this event, if it represents a new
4045 * thread, simply add the thread to the list.
4048 /* If not in procinfo list, add it. */
4049 temp_tid
= proc_get_current_thread (pi
);
4050 if (!find_procinfo (pi
->pid
, temp_tid
))
4051 create_procinfo (pi
->pid
, temp_tid
);
4053 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4054 /* If not in GDB's thread list, add it. */
4055 if (!in_thread_list (temp_ptid
))
4057 printf_filtered ("[New %s]\n",
4058 target_pid_to_str (temp_ptid
));
4059 add_thread (temp_ptid
);
4061 /* Return to WFI, but tell it to immediately resume. */
4062 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4063 return inferior_ptid
;
4065 else if (syscall_is_lwp_exit (pi
, what
))
4067 printf_filtered ("[%s exited]\n",
4068 target_pid_to_str (retval
));
4069 delete_thread (retval
);
4070 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4075 /* FIXME: Do we need to handle SYS_sproc,
4076 SYS_fork, or SYS_vfork here? The old procfs
4077 seemed to use this event to handle threads on
4078 older (non-LWP) systems, where I'm assuming
4079 that threads were actually separate processes.
4080 Irix, maybe? Anyway, low priority for now. */
4084 printf_filtered ("procfs: trapped on exit from ");
4085 proc_prettyprint_syscall (proc_what (pi
), 0);
4086 printf_filtered ("\n");
4089 long i
, nsysargs
, *sysargs
;
4091 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
4092 (sysargs
= proc_sysargs (pi
)) != NULL
)
4094 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
4095 for (i
= 0; i
< nsysargs
; i
++)
4096 printf_filtered ("#%ld: 0x%08lx\n",
4101 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
4102 return inferior_ptid
;
4107 wstat
= (SIGSTOP
<< 8) | 0177;
4112 printf_filtered ("Retry #%d:\n", retry
);
4113 pi
->status_valid
= 0;
4118 /* If not in procinfo list, add it. */
4119 temp_tid
= proc_get_current_thread (pi
);
4120 if (!find_procinfo (pi
->pid
, temp_tid
))
4121 create_procinfo (pi
->pid
, temp_tid
);
4123 /* If not in GDB's thread list, add it. */
4124 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
4125 if (!in_thread_list (temp_ptid
))
4127 printf_filtered ("[New %s]\n",
4128 target_pid_to_str (temp_ptid
));
4129 add_thread (temp_ptid
);
4132 status
->kind
= TARGET_WAITKIND_STOPPED
;
4133 status
->value
.sig
= 0;
4138 wstat
= (what
<< 8) | 0177;
4141 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
4144 wstat
= (SIGTRAP
<< 8) | 0177;
4149 wstat
= (SIGTRAP
<< 8) | 0177;
4152 /* FIXME: use si_signo where possible. */
4154 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4157 wstat
= (SIGILL
<< 8) | 0177;
4160 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4163 wstat
= (SIGTRAP
<< 8) | 0177;
4167 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4170 wstat
= (SIGSEGV
<< 8) | 0177;
4174 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4177 wstat
= (SIGFPE
<< 8) | 0177;
4179 case FLTPAGE
: /* Recoverable page fault */
4180 default: /* FIXME: use si_signo if possible for fault */
4181 retval
= pid_to_ptid (-1);
4182 printf_filtered ("procfs:%d -- ", __LINE__
);
4183 printf_filtered ("child stopped for unknown reason:\n");
4184 proc_prettyprint_why (why
, what
, 1);
4185 error ("... giving up...");
4188 break; /* case PR_FAULTED: */
4189 default: /* switch (why) unmatched */
4190 printf_filtered ("procfs:%d -- ", __LINE__
);
4191 printf_filtered ("child stopped for unknown reason:\n");
4192 proc_prettyprint_why (why
, what
, 1);
4193 error ("... giving up...");
4197 * Got this far without error:
4198 * If retval isn't in the threads database, add it.
4200 if (PIDGET (retval
) > 0 &&
4201 !ptid_equal (retval
, inferior_ptid
) &&
4202 !in_thread_list (retval
))
4205 * We have a new thread.
4206 * We need to add it both to GDB's list and to our own.
4207 * If we don't create a procinfo, resume may be unhappy
4210 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
4211 add_thread (retval
);
4212 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
4213 create_procinfo (PIDGET (retval
), TIDGET (retval
));
4215 /* In addition, it's possible that this is the first
4216 * new thread we've seen, in which case we may not
4217 * have created entries for inferior_ptid yet.
4219 if (TIDGET (inferior_ptid
) != 0)
4221 if (!in_thread_list (inferior_ptid
))
4222 add_thread (inferior_ptid
);
4223 if (find_procinfo (PIDGET (inferior_ptid
),
4224 TIDGET (inferior_ptid
)) == NULL
)
4225 create_procinfo (PIDGET (inferior_ptid
),
4226 TIDGET (inferior_ptid
));
4230 else /* flags do not indicate STOPPED */
4232 /* surely this can't happen... */
4233 printf_filtered ("procfs:%d -- process not stopped.\n",
4235 proc_prettyprint_flags (flags
, 1);
4236 error ("procfs: ...giving up...");
4241 store_waitstatus (status
, wstat
);
4247 /* Transfer LEN bytes between GDB address MYADDR and target address
4248 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4249 otherwise transfer them from the target. TARGET is unused.
4251 The return value is 0 if an error occurred or no bytes were
4252 transferred. Otherwise, it will be a positive value which
4253 indicates the number of bytes transferred between gdb and the
4254 target. (Note that the interface also makes provisions for
4255 negative values, but this capability isn't implemented here.) */
4258 procfs_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int dowrite
,
4259 struct mem_attrib
*attrib
, struct target_ops
*target
)
4264 /* Find procinfo for main process */
4265 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4266 if (pi
->as_fd
== 0 &&
4267 open_procinfo_files (pi
, FD_AS
) == 0)
4269 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4273 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4278 PROCFS_NOTE ("write memory: ");
4280 PROCFS_NOTE ("write memory: \n");
4282 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4286 PROCFS_NOTE ("read memory: \n");
4287 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4298 * Function: invalidate_cache
4300 * Called by target_resume before making child runnable.
4301 * Mark cached registers and status's invalid.
4302 * If there are "dirty" caches that need to be written back
4303 * to the child process, do that.
4305 * File descriptors are also cached.
4306 * As they are a limited resource, we cannot hold onto them indefinitely.
4307 * However, as they are expensive to open, we don't want to throw them
4308 * away indescriminately either. As a compromise, we will keep the
4309 * file descriptors for the parent process, but discard any file
4310 * descriptors we may have accumulated for the threads.
4313 * As this function is called by iterate_over_threads, it always
4314 * returns zero (so that iterate_over_threads will keep iterating).
4319 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4322 * About to run the child; invalidate caches and do any other cleanup.
4326 if (pi
->gregs_dirty
)
4327 if (parent
== NULL
||
4328 proc_get_current_thread (parent
) != pi
->tid
)
4329 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4330 proc_warn (pi
, "target_resume, set_gregs",
4332 if (FP0_REGNUM
>= 0)
4333 if (pi
->fpregs_dirty
)
4334 if (parent
== NULL
||
4335 proc_get_current_thread (parent
) != pi
->tid
)
4336 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4337 proc_warn (pi
, "target_resume, set_fpregs",
4343 /* The presence of a parent indicates that this is an LWP.
4344 Close any file descriptors that it might have open.
4345 We don't do this to the master (parent) procinfo. */
4347 close_procinfo_files (pi
);
4349 pi
->gregs_valid
= 0;
4350 pi
->fpregs_valid
= 0;
4352 pi
->gregs_dirty
= 0;
4353 pi
->fpregs_dirty
= 0;
4355 pi
->status_valid
= 0;
4356 pi
->threads_valid
= 0;
4363 * Function: make_signal_thread_runnable
4365 * A callback function for iterate_over_threads.
4366 * Find the asynchronous signal thread, and make it runnable.
4367 * See if that helps matters any.
4371 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4374 if (proc_flags (pi
) & PR_ASLWP
)
4376 if (!proc_run_process (pi
, 0, -1))
4377 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4386 * Function: target_resume
4388 * Make the child process runnable. Normally we will then call
4389 * procfs_wait and wait for it to stop again (unles gdb is async).
4392 * step: if true, then arrange for the child to stop again
4393 * after executing a single instruction.
4394 * signo: if zero, then cancel any pending signal.
4395 * If non-zero, then arrange for the indicated signal
4396 * to be delivered to the child when it runs.
4397 * pid: if -1, then allow any child thread to run.
4398 * if non-zero, then allow only the indicated thread to run.
4399 ******* (not implemented yet)
4403 procfs_resume (ptid_t ptid
, int step
, enum target_signal signo
)
4405 procinfo
*pi
, *thread
;
4409 prrun.prflags |= PRSVADDR;
4410 prrun.pr_vaddr = $PC; set resume address
4411 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4412 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4413 prrun.prflags |= PRCFAULT; clear current fault.
4415 PRSTRACE and PRSFAULT can be done by other means
4416 (proc_trace_signals, proc_trace_faults)
4417 PRSVADDR is unnecessary.
4418 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4419 This basically leaves PRSTEP and PRCSIG.
4420 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4421 So basically PR_STEP is the sole argument that must be passed
4422 to proc_run_process (for use in the prrun struct by ioctl). */
4424 /* Find procinfo for main process */
4425 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4427 /* First cut: ignore pid argument */
4430 /* Convert signal to host numbering. */
4432 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4435 native_signo
= target_signal_to_host (signo
);
4437 pi
->ignore_next_sigstop
= 0;
4439 /* Running the process voids all cached registers and status. */
4440 /* Void the threads' caches first */
4441 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4442 /* Void the process procinfo's caches. */
4443 invalidate_cache (NULL
, pi
, NULL
);
4445 if (PIDGET (ptid
) != -1)
4447 /* Resume a specific thread, presumably suppressing the others. */
4448 thread
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
));
4451 if (thread
->tid
!= 0)
4453 /* We're to resume a specific thread, and not the others.
4454 * Set the child process's PR_ASYNC flag.
4457 if (!proc_set_async (pi
))
4458 proc_error (pi
, "target_resume, set_async", __LINE__
);
4461 proc_iterate_over_threads (pi
,
4462 make_signal_thread_runnable
,
4465 pi
= thread
; /* substitute the thread's procinfo for run */
4470 if (!proc_run_process (pi
, step
, native_signo
))
4473 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4475 proc_error (pi
, "target_resume", __LINE__
);
4480 * Function: register_gdb_signals
4482 * Traverse the list of signals that GDB knows about
4483 * (see "handle" command), and arrange for the target
4484 * to be stopped or not, according to these settings.
4486 * Returns non-zero for success, zero for failure.
4490 register_gdb_signals (procinfo
*pi
, gdb_sigset_t
*signals
)
4494 for (signo
= 0; signo
< NSIG
; signo
++)
4495 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4496 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4497 signal_pass_state (target_signal_from_host (signo
)) == 1)
4498 prdelset (signals
, signo
);
4500 praddset (signals
, signo
);
4502 return proc_set_traced_signals (pi
, signals
);
4506 * Function: target_notice_signals
4508 * Set up to trace signals in the child process.
4512 procfs_notice_signals (ptid_t ptid
)
4514 gdb_sigset_t signals
;
4515 procinfo
*pi
= find_procinfo_or_die (PIDGET (ptid
), 0);
4517 if (proc_get_traced_signals (pi
, &signals
) &&
4518 register_gdb_signals (pi
, &signals
))
4521 proc_error (pi
, "notice_signals", __LINE__
);
4525 * Function: target_files_info
4527 * Print status information about the child process.
4531 procfs_files_info (struct target_ops
*ignore
)
4533 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4534 attach_flag
? "attached": "child",
4535 target_pid_to_str (inferior_ptid
));
4539 * Function: target_open
4541 * A dummy: you don't open procfs.
4545 procfs_open (char *args
, int from_tty
)
4547 error ("Use the \"run\" command to start a Unix child process.");
4551 * Function: target_can_run
4553 * This tells GDB that this target vector can be invoked
4554 * for "run" or "attach".
4557 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4558 be a runnable target. Used by targets
4559 that can sit atop procfs, such as solaris
4564 procfs_can_run (void)
4566 /* This variable is controlled by modules that sit atop procfs that
4567 may layer their own process structure atop that provided here.
4568 sol-thread.c does this because of the Solaris two-level thread
4571 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4573 return !procfs_suppress_run
;
4577 * Function: target_stop
4579 * Stop the child process asynchronously, as when the
4580 * gdb user types control-c or presses a "stop" button.
4582 * Works by sending kill(SIGINT) to the child's process group.
4588 extern pid_t inferior_process_group
;
4590 kill (-inferior_process_group
, SIGINT
);
4594 * Function: unconditionally_kill_inferior
4596 * Make it die. Wait for it to die. Clean up after it.
4597 * Note: this should only be applied to the real process,
4598 * not to an LWP, because of the check for parent-process.
4599 * If we need this to work for an LWP, it needs some more logic.
4603 unconditionally_kill_inferior (procinfo
*pi
)
4607 parent_pid
= proc_parent_pid (pi
);
4608 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4609 /* FIXME: use access functions */
4610 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4611 before the PIOCKILL, otherwise it might generate a corrupted core
4612 file for the inferior. */
4613 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4615 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4618 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4619 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4620 to kill the inferior, otherwise it might remain stopped with a
4622 We do not check the result of the PIOCSSIG, the inferior might have
4625 gdb_siginfo_t newsiginfo
;
4627 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4628 newsiginfo
.si_signo
= SIGKILL
;
4629 newsiginfo
.si_code
= 0;
4630 newsiginfo
.si_errno
= 0;
4631 newsiginfo
.si_pid
= getpid ();
4632 newsiginfo
.si_uid
= getuid ();
4633 /* FIXME: use proc_set_current_signal */
4634 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4636 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4637 if (!proc_kill (pi
, SIGKILL
))
4638 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4639 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4640 destroy_procinfo (pi
);
4642 /* If pi is GDB's child, wait for it to die. */
4643 if (parent_pid
== getpid ())
4644 /* FIXME: should we use waitpid to make sure we get the right event?
4645 Should we check the returned event? */
4650 ret
= waitpid (pi
->pid
, &status
, 0);
4658 * Function: target_kill_inferior
4660 * We're done debugging it, and we want it to go away.
4661 * Then we want GDB to forget all about it.
4665 procfs_kill_inferior (void)
4667 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4669 /* Find procinfo for main process */
4670 procinfo
*pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4673 unconditionally_kill_inferior (pi
);
4674 target_mourn_inferior ();
4679 * Function: target_mourn_inferior
4681 * Forget we ever debugged this thing!
4685 procfs_mourn_inferior (void)
4689 if (!ptid_equal (inferior_ptid
, null_ptid
))
4691 /* Find procinfo for main process */
4692 pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4694 destroy_procinfo (pi
);
4696 unpush_target (&procfs_ops
);
4697 generic_mourn_inferior ();
4701 * Function: init_inferior
4703 * When GDB forks to create a runnable inferior process,
4704 * this function is called on the parent side of the fork.
4705 * It's job is to do whatever is necessary to make the child
4706 * ready to be debugged, and then wait for the child to synchronize.
4710 procfs_init_inferior (int pid
)
4713 gdb_sigset_t signals
;
4716 /* This routine called on the parent side (GDB side)
4717 after GDB forks the inferior. */
4719 push_target (&procfs_ops
);
4721 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4722 perror ("procfs: out of memory in 'init_inferior'");
4724 if (!open_procinfo_files (pi
, FD_CTL
))
4725 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4729 open_procinfo_files // done
4732 procfs_notice_signals
4739 /* If not stopped yet, wait for it to stop. */
4740 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4741 !(proc_wait_for_stop (pi
)))
4742 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4744 /* Save some of the /proc state to be restored if we detach. */
4745 /* FIXME: Why? In case another debugger was debugging it?
4746 We're it's parent, for Ghu's sake! */
4747 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4748 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4749 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4750 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4751 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4752 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4753 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4754 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4755 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4756 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4758 /* Register to trace selected signals in the child. */
4759 prfillset (&signals
);
4760 if (!register_gdb_signals (pi
, &signals
))
4761 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4763 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4764 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4766 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4767 and possibly even turning ON kill-on-last-close at this point. But
4768 I can't make that change without careful testing which I don't have
4769 time to do right now... */
4770 /* Turn on run-on-last-close flag so that the child
4771 will die if GDB goes away for some reason. */
4772 if (!proc_set_run_on_last_close (pi
))
4773 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4775 /* The 'process ID' we return to GDB is composed of
4776 the actual process ID plus the lwp ID. */
4777 inferior_ptid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4779 #ifdef START_INFERIOR_TRAPS_EXPECTED
4780 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4782 /* One trap to exec the shell, one to exec the program being debugged. */
4783 startup_inferior (2);
4784 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4788 * Function: set_exec_trap
4790 * When GDB forks to create a new process, this function is called
4791 * on the child side of the fork before GDB exec's the user program.
4792 * Its job is to make the child minimally debuggable, so that the
4793 * parent GDB process can connect to the child and take over.
4794 * This function should do only the minimum to make that possible,
4795 * and to synchronize with the parent process. The parent process
4796 * should take care of the details.
4800 procfs_set_exec_trap (void)
4802 /* This routine called on the child side (inferior side)
4803 after GDB forks the inferior. It must use only local variables,
4804 because it may be sharing data space with its parent. */
4809 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4810 perror_with_name ("procfs: create_procinfo failed in child.");
4812 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4814 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4815 gdb_flush (gdb_stderr
);
4816 /* no need to call "dead_procinfo", because we're going to exit. */
4820 #ifdef PRFS_STOPEXEC /* defined on OSF */
4821 /* OSF method for tracing exec syscalls. Quoting:
4822 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4823 exits from exec system calls because of the user level loader. */
4824 /* FIXME: make nice and maybe move into an access function. */
4828 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4830 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4831 gdb_flush (gdb_stderr
);
4834 prfs_flags
|= PRFS_STOPEXEC
;
4836 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4838 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4839 gdb_flush (gdb_stderr
);
4843 #else /* not PRFS_STOPEXEC */
4844 /* Everyone else's (except OSF) method for tracing exec syscalls */
4846 Not all systems with /proc have all the exec* syscalls with the same
4847 names. On the SGI, for example, there is no SYS_exec, but there
4848 *is* a SYS_execv. So, we try to account for that. */
4850 exitset
= sysset_t_alloc (pi
);
4851 gdb_premptysysset (exitset
);
4853 gdb_praddsysset (exitset
, SYS_exec
);
4856 gdb_praddsysset (exitset
, SYS_execve
);
4859 gdb_praddsysset (exitset
, SYS_execv
);
4861 #ifdef DYNAMIC_SYSCALLS
4863 int callnum
= find_syscall (pi
, "execve");
4866 gdb_praddsysset (exitset
, callnum
);
4868 callnum
= find_syscall (pi
, "ra_execve");
4870 gdb_praddsysset (exitset
, callnum
);
4872 #endif /* DYNAMIC_SYSCALLS */
4874 if (!proc_set_traced_sysexit (pi
, exitset
))
4876 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4877 gdb_flush (gdb_stderr
);
4880 #endif /* PRFS_STOPEXEC */
4882 /* FIXME: should this be done in the parent instead? */
4883 /* Turn off inherit on fork flag so that all grand-children
4884 of gdb start with tracing flags cleared. */
4885 if (!proc_unset_inherit_on_fork (pi
))
4886 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4888 /* Turn off run on last close flag, so that the child process
4889 cannot run away just because we close our handle on it.
4890 We want it to wait for the parent to attach. */
4891 if (!proc_unset_run_on_last_close (pi
))
4892 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4894 /* FIXME: No need to destroy the procinfo --
4895 we have our own address space, and we're about to do an exec! */
4896 /*destroy_procinfo (pi);*/
4900 * Function: create_inferior
4902 * This function is called BEFORE gdb forks the inferior process.
4903 * Its only real responsibility is to set things up for the fork,
4904 * and tell GDB which two functions to call after the fork (one
4905 * for the parent, and one for the child).
4907 * This function does a complicated search for a unix shell program,
4908 * which it then uses to parse arguments and environment variables
4909 * to be sent to the child. I wonder whether this code could not
4910 * be abstracted out and shared with other unix targets such as
4915 procfs_create_inferior (char *exec_file
, char *allargs
, char **env
)
4917 char *shell_file
= getenv ("SHELL");
4919 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4922 /* We will be looking down the PATH to find shell_file. If we
4923 just do this the normal way (via execlp, which operates by
4924 attempting an exec for each element of the PATH until it
4925 finds one which succeeds), then there will be an exec for
4926 each failed attempt, each of which will cause a PR_SYSEXIT
4927 stop, and we won't know how to distinguish the PR_SYSEXIT's
4928 for these failed execs with the ones for successful execs
4929 (whether the exec has succeeded is stored at that time in the
4930 carry bit or some such architecture-specific and
4931 non-ABI-specified place).
4933 So I can't think of anything better than to search the PATH
4934 now. This has several disadvantages: (1) There is a race
4935 condition; if we find a file now and it is deleted before we
4936 exec it, we lose, even if the deletion leaves a valid file
4937 further down in the PATH, (2) there is no way to know exactly
4938 what an executable (in the sense of "capable of being
4939 exec'd") file is. Using access() loses because it may lose
4940 if the caller is the superuser; failing to use it loses if
4941 there are ACLs or some such. */
4945 /* FIXME-maybe: might want "set path" command so user can change what
4946 path is used from within GDB. */
4947 char *path
= getenv ("PATH");
4949 struct stat statbuf
;
4952 path
= "/bin:/usr/bin";
4954 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4955 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4957 p1
= strchr (p
, ':');
4962 strncpy (tryname
, p
, len
);
4963 tryname
[len
] = '\0';
4964 strcat (tryname
, "/");
4965 strcat (tryname
, shell_file
);
4966 if (access (tryname
, X_OK
) < 0)
4968 if (stat (tryname
, &statbuf
) < 0)
4970 if (!S_ISREG (statbuf
.st_mode
))
4971 /* We certainly need to reject directories. I'm not quite
4972 as sure about FIFOs, sockets, etc., but I kind of doubt
4973 that people want to exec() these things. */
4978 /* Not found. This must be an error rather than merely passing
4979 the file to execlp(), because execlp() would try all the
4980 exec()s, causing GDB to get confused. */
4981 error ("procfs:%d -- Can't find shell %s in PATH",
4982 __LINE__
, shell_file
);
4984 shell_file
= tryname
;
4987 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4988 procfs_init_inferior
, NULL
, shell_file
);
4990 /* We are at the first instruction we care about. */
4991 /* Pedal to the metal... */
4993 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
4997 * Function: notice_thread
4999 * Callback for find_new_threads.
5000 * Calls "add_thread".
5004 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
5006 ptid_t gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
5008 if (!in_thread_list (gdb_threadid
))
5009 add_thread (gdb_threadid
);
5015 * Function: target_find_new_threads
5017 * Query all the threads that the target knows about,
5018 * and give them back to GDB to add to its list.
5022 procfs_find_new_threads (void)
5026 /* Find procinfo for main process */
5027 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5028 proc_update_threads (pi
);
5029 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
5033 * Function: target_thread_alive
5035 * Return true if the thread is still 'alive'.
5037 * This guy doesn't really seem to be doing his job.
5038 * Got to investigate how to tell when a thread is really gone.
5042 procfs_thread_alive (ptid_t ptid
)
5047 proc
= PIDGET (ptid
);
5048 thread
= TIDGET (ptid
);
5049 /* If I don't know it, it ain't alive! */
5050 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
5053 /* If I can't get its status, it ain't alive!
5054 What's more, I need to forget about it! */
5055 if (!proc_get_status (pi
))
5057 destroy_procinfo (pi
);
5060 /* I couldn't have got its status if it weren't alive, so it's alive. */
5065 * Function: target_pid_to_str
5067 * Return a string to be used to identify the thread in
5068 * the "info threads" display.
5072 procfs_pid_to_str (ptid_t ptid
)
5074 static char buf
[80];
5078 proc
= PIDGET (ptid
);
5079 thread
= TIDGET (ptid
);
5080 pi
= find_procinfo (proc
, thread
);
5083 sprintf (buf
, "Process %d", proc
);
5085 sprintf (buf
, "LWP %d", thread
);
5090 * Function: procfs_set_watchpoint
5091 * Insert a watchpoint
5095 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
5103 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5104 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5106 /* Translate from GDB's flags to /proc's */
5107 if (len
> 0) /* len == 0 means delete watchpoint */
5109 switch (rwflag
) { /* FIXME: need an enum! */
5110 case hw_write
: /* default watchpoint (write) */
5111 pflags
= WRITE_WATCHFLAG
;
5113 case hw_read
: /* read watchpoint */
5114 pflags
= READ_WATCHFLAG
;
5116 case hw_access
: /* access watchpoint */
5117 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
5119 case hw_execute
: /* execution HW breakpoint */
5120 pflags
= EXEC_WATCHFLAG
;
5122 default: /* Something weird. Return error. */
5125 if (after
) /* Stop after r/w access is completed. */
5126 pflags
|= AFTER_WATCHFLAG
;
5129 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
5131 if (errno
== E2BIG
) /* Typical error for no resources */
5132 return -1; /* fail */
5133 /* GDB may try to remove the same watchpoint twice.
5134 If a remove request returns no match, don't error. */
5135 if (errno
== ESRCH
&& len
== 0)
5136 return 0; /* ignore */
5137 proc_error (pi
, "set_watchpoint", __LINE__
);
5140 #endif /* UNIXWARE */
5144 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
5145 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
5146 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
5149 Note: procfs_can_use_hw_breakpoint() is not yet used by all
5150 procfs.c targets due to the fact that some of them still define
5151 TARGET_CAN_USE_HARDWARE_WATCHPOINT. */
5154 procfs_can_use_hw_breakpoint (int type
, int cnt
, int othertype
)
5156 #ifndef TARGET_HAS_HARDWARE_WATCHPOINTS
5159 /* Due to the way that proc_set_watchpoint() is implemented, host
5160 and target pointers must be of the same size. If they are not,
5161 we can't use hardware watchpoints. This limitation is due to the
5162 fact that proc_set_watchpoint() calls address_to_host_pointer();
5163 a close inspection of address_to_host_pointer will reveal that
5164 an internal error will be generated when the host and target
5165 pointer sizes are different. */
5166 if (sizeof (void *) != TYPE_LENGTH (builtin_type_void_data_ptr
))
5169 /* Other tests here??? */
5176 * Function: stopped_by_watchpoint
5178 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5179 * else returns zero.
5183 procfs_stopped_by_watchpoint (ptid_t ptid
)
5187 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
5188 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
5190 if (!pi
) /* If no process, then not stopped by watchpoint! */
5193 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
5195 if (proc_why (pi
) == PR_FAULTED
)
5198 if (proc_what (pi
) == FLTWATCH
)
5202 if (proc_what (pi
) == FLTKWATCH
)
5210 #ifdef TM_I386SOL2_H
5212 * Function: procfs_find_LDT_entry
5215 * ptid_t ptid; // The GDB-style pid-plus-LWP.
5218 * pointer to the corresponding LDT entry.
5222 procfs_find_LDT_entry (ptid_t ptid
)
5224 gdb_gregset_t
*gregs
;
5228 /* Find procinfo for the lwp. */
5229 if ((pi
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
))) == NULL
)
5231 warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.",
5232 PIDGET (ptid
), TIDGET (ptid
));
5235 /* get its general registers. */
5236 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
5238 warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.",
5239 PIDGET (ptid
), TIDGET (ptid
));
5242 /* Now extract the GS register's lower 16 bits. */
5243 key
= (*gregs
)[GS
] & 0xffff;
5245 /* Find the matching entry and return it. */
5246 return proc_get_LDT_entry (pi
, key
);
5248 #endif /* TM_I386SOL2_H */
5251 * Memory Mappings Functions:
5255 * Function: iterate_over_mappings
5257 * Call a callback function once for each mapping, passing it the mapping,
5258 * an optional secondary callback function, and some optional opaque data.
5259 * Quit and return the first non-zero value returned from the callback.
5262 * pi -- procinfo struct for the process to be mapped.
5263 * func -- callback function to be called by this iterator.
5264 * data -- optional opaque data to be passed to the callback function.
5265 * child_func -- optional secondary function pointer to be passed
5266 * to the child function.
5268 * Return: First non-zero return value from the callback function,
5273 iterate_over_mappings (procinfo
*pi
, int (*child_func
) (), void *data
,
5274 int (*func
) (struct prmap
*map
,
5275 int (*child_func
) (),
5278 char pathname
[MAX_PROC_NAME_SIZE
];
5279 struct prmap
*prmaps
;
5280 struct prmap
*prmap
;
5288 /* Get the number of mappings, allocate space,
5289 and read the mappings into prmaps. */
5292 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
5293 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
5294 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
5296 /* Make sure it gets closed again. */
5297 make_cleanup_close (map_fd
);
5299 /* Use stat to determine the file size, and compute
5300 the number of prmap_t objects it contains. */
5301 if (fstat (map_fd
, &sbuf
) != 0)
5302 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
5304 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
5305 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5306 if (read (map_fd
, (char *) prmaps
, nmap
* sizeof (*prmaps
))
5307 != (nmap
* sizeof (*prmaps
)))
5308 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
5310 /* Use ioctl command PIOCNMAP to get number of mappings. */
5311 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmap
) != 0)
5312 proc_error (pi
, "iterate_over_mappings (PIOCNMAP)", __LINE__
);
5314 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5315 if (ioctl (pi
->ctl_fd
, PIOCMAP
, prmaps
) != 0)
5316 proc_error (pi
, "iterate_over_mappings (PIOCMAP)", __LINE__
);
5319 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
5320 if ((funcstat
= (*func
) (prmap
, child_func
, data
)) != 0)
5327 * Function: solib_mappings_callback
5329 * Calls the supplied callback function once for each mapped address
5330 * space in the process. The callback function receives an open
5331 * file descriptor for the file corresponding to that mapped
5332 * address space (if there is one), and the base address of the
5333 * mapped space. Quit when the callback function returns a
5334 * nonzero value, or at teh end of the mappings.
5336 * Returns: the first non-zero return value of the callback function,
5340 int solib_mappings_callback (struct prmap
*map
,
5341 int (*func
) (int, CORE_ADDR
),
5344 procinfo
*pi
= data
;
5348 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
5350 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
5351 return -1; /* sanity */
5353 if (map
->pr_mapname
[0] == 0)
5355 fd
= -1; /* no map file */
5359 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
5360 /* Note: caller's responsibility to close this fd! */
5361 fd
= open_with_retry (name
, O_RDONLY
);
5362 /* Note: we don't test the above call for failure;
5363 we just pass the FD on as given. Sometimes there is
5364 no file, so the open may return failure, but that's
5368 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
->pr_vaddr
);
5369 /* Note: we don't test the above call for failure;
5370 we just pass the FD on as given. Sometimes there is
5371 no file, so the ioctl may return failure, but that's
5374 return (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
);
5378 * Function: proc_iterate_over_mappings
5380 * Uses the unified "iterate_over_mappings" function
5381 * to implement the exported interface to solib-svr4.c.
5383 * Given a pointer to a function, call that function once for every
5384 * mapped address space in the process. The callback function
5385 * receives an open file descriptor for the file corresponding to
5386 * that mapped address space (if there is one), and the base address
5387 * of the mapped space. Quit when the callback function returns a
5388 * nonzero value, or at teh end of the mappings.
5390 * Returns: the first non-zero return value of the callback function,
5395 proc_iterate_over_mappings (int (*func
) (int, CORE_ADDR
))
5397 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5399 return iterate_over_mappings (pi
, func
, pi
, solib_mappings_callback
);
5403 * Function: find_memory_regions_callback
5405 * Implements the to_find_memory_regions method.
5406 * Calls an external function for each memory region.
5407 * External function will have the signiture:
5409 * int callback (CORE_ADDR vaddr,
5410 * unsigned long size,
5411 * int read, int write, int execute,
5414 * Returns the integer value returned by the callback.
5418 find_memory_regions_callback (struct prmap
*map
,
5419 int (*func
) (CORE_ADDR
,
5425 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
5427 (map
->pr_mflags
& MA_READ
) != 0,
5428 (map
->pr_mflags
& MA_WRITE
) != 0,
5429 (map
->pr_mflags
& MA_EXEC
) != 0,
5434 * Function: proc_find_memory_regions
5436 * External interface. Calls a callback function once for each
5437 * mapped memory region in the child process, passing as arguments
5438 * CORE_ADDR virtual_address,
5439 * unsigned long size,
5440 * int read, TRUE if region is readable by the child
5441 * int write, TRUE if region is writable by the child
5442 * int execute TRUE if region is executable by the child.
5444 * Stops iterating and returns the first non-zero value
5445 * returned by the callback.
5449 proc_find_memory_regions (int (*func
) (CORE_ADDR
,
5455 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5457 return iterate_over_mappings (pi
, func
, data
,
5458 find_memory_regions_callback
);
5462 * Function: mappingflags
5464 * Returns an ascii representation of a memory mapping's flags.
5468 mappingflags (long flags
)
5470 static char asciiflags
[8];
5472 strcpy (asciiflags
, "-------");
5473 #if defined (MA_PHYS)
5474 if (flags
& MA_PHYS
)
5475 asciiflags
[0] = 'd';
5477 if (flags
& MA_STACK
)
5478 asciiflags
[1] = 's';
5479 if (flags
& MA_BREAK
)
5480 asciiflags
[2] = 'b';
5481 if (flags
& MA_SHARED
)
5482 asciiflags
[3] = 's';
5483 if (flags
& MA_READ
)
5484 asciiflags
[4] = 'r';
5485 if (flags
& MA_WRITE
)
5486 asciiflags
[5] = 'w';
5487 if (flags
& MA_EXEC
)
5488 asciiflags
[6] = 'x';
5489 return (asciiflags
);
5493 * Function: info_mappings_callback
5495 * Callback function, does the actual work for 'info proc mappings'.
5500 info_mappings_callback (struct prmap
*map
, int (*ignore
) (), void *unused
)
5502 char *data_fmt_string
;
5504 if (TARGET_ADDR_BIT
== 32)
5505 data_fmt_string
= "\t%#10lx %#10lx %#10x %#10x %7s\n";
5507 data_fmt_string
= " %#18lx %#18lx %#10x %#10x %7s\n";
5509 printf_filtered (data_fmt_string
,
5510 (unsigned long) map
->pr_vaddr
,
5511 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5513 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5514 (unsigned int) map
->pr_offset
,
5518 mappingflags (map
->pr_mflags
));
5524 * Function: info_proc_mappings
5526 * Implement the "info proc mappings" subcommand.
5530 info_proc_mappings (procinfo
*pi
, int summary
)
5532 char *header_fmt_string
;
5534 if (TARGET_PTR_BIT
== 32)
5535 header_fmt_string
= "\t%10s %10s %10s %10s %7s\n";
5537 header_fmt_string
= " %18s %18s %10s %10s %7s\n";
5540 return; /* No output for summary mode. */
5542 printf_filtered ("Mapped address spaces:\n\n");
5543 printf_filtered (header_fmt_string
,
5550 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
5551 printf_filtered ("\n");
5555 * Function: info_proc_cmd
5557 * Implement the "info proc" command.
5561 info_proc_cmd (char *args
, int from_tty
)
5563 struct cleanup
*old_chain
;
5564 procinfo
*process
= NULL
;
5565 procinfo
*thread
= NULL
;
5572 old_chain
= make_cleanup (null_cleanup
, 0);
5575 if ((argv
= buildargv (args
)) == NULL
)
5578 make_cleanup_freeargv (argv
);
5580 while (argv
!= NULL
&& *argv
!= NULL
)
5582 if (isdigit (argv
[0][0]))
5584 pid
= strtoul (argv
[0], &tmp
, 10);
5586 tid
= strtoul (++tmp
, NULL
, 10);
5588 else if (argv
[0][0] == '/')
5590 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5592 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
5603 pid
= PIDGET (inferior_ptid
);
5605 error ("No current process: you must name one.");
5608 /* Have pid, will travel.
5609 First see if it's a process we're already debugging. */
5610 process
= find_procinfo (pid
, 0);
5611 if (process
== NULL
)
5613 /* No. So open a procinfo for it, but
5614 remember to close it again when finished. */
5615 process
= create_procinfo (pid
, 0);
5616 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5617 if (!open_procinfo_files (process
, FD_CTL
))
5618 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5622 thread
= create_procinfo (pid
, tid
);
5626 printf_filtered ("process %d flags:\n", process
->pid
);
5627 proc_prettyprint_flags (proc_flags (process
), 1);
5628 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5629 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5630 if (proc_get_nthreads (process
) > 1)
5631 printf_filtered ("Process has %d threads.\n",
5632 proc_get_nthreads (process
));
5636 printf_filtered ("thread %d flags:\n", thread
->tid
);
5637 proc_prettyprint_flags (proc_flags (thread
), 1);
5638 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5639 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5644 info_proc_mappings (process
, 0);
5647 do_cleanups (old_chain
);
5651 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5657 if (PIDGET (inferior_ptid
) <= 0)
5658 error ("you must be debugging a process to use this command.");
5660 if (args
== NULL
|| args
[0] == 0)
5661 error_no_arg ("system call to trace");
5663 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5664 if (isdigit (args
[0]))
5666 syscallnum
= atoi (args
);
5667 if (entry_or_exit
== PR_SYSENTRY
)
5668 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5670 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5673 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5675 if (mode
== FLAG_SET
)
5676 gdb_praddsysset (sysset
, syscallnum
);
5678 gdb_prdelsysset (sysset
, syscallnum
);
5680 if (entry_or_exit
== PR_SYSENTRY
)
5682 if (!proc_set_traced_sysentry (pi
, sysset
))
5683 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5687 if (!proc_set_traced_sysexit (pi
, sysset
))
5688 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5694 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5696 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5700 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5702 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5706 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5708 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5712 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5714 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5719 _initialize_procfs (void)
5722 add_target (&procfs_ops
);
5723 add_info ("proc", info_proc_cmd
,
5724 "Show /proc process information about any running process.\n\
5725 Specify process id, or use the program being debugged by default.\n\
5726 Specify keyword 'mappings' for detailed info on memory mappings.");
5727 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5728 "Give a trace of entries into the syscall.");
5729 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5730 "Give a trace of exits from the syscall.");
5731 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5732 "Cancel a trace of entries into the syscall.");
5733 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5734 "Cancel a trace of exits from the syscall.");
5737 /* =================== END, GDB "MODULE" =================== */
5741 /* miscellaneous stubs: */
5742 /* The following satisfy a few random symbols mostly created by */
5743 /* the solaris threads implementation, which I will chase down */
5747 * Return a pid for which we guarantee
5748 * we will be able to find a 'live' procinfo.
5752 procfs_first_available (void)
5754 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);
5757 /* =================== GCORE .NOTE "MODULE" =================== */
5758 #if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT)
5759 /* gcore only implemented on solaris and unixware (so far) */
5762 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
5763 char *note_data
, int *note_size
)
5765 gdb_gregset_t gregs
;
5766 gdb_fpregset_t fpregs
;
5767 unsigned long merged_pid
;
5769 merged_pid
= TIDGET (ptid
) << 16 | PIDGET (ptid
);
5771 fill_gregset (&gregs
, -1);
5772 #if defined (UNIXWARE)
5773 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
5780 note_data
= (char *) elfcore_write_prstatus (obfd
,
5787 fill_fpregset (&fpregs
, -1);
5788 note_data
= (char *) elfcore_write_prfpreg (obfd
,
5796 struct procfs_corefile_thread_data
{
5803 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
5805 struct procfs_corefile_thread_data
*args
= data
;
5807 if (pi
!= NULL
&& thread
->tid
!= 0)
5809 ptid_t saved_ptid
= inferior_ptid
;
5810 inferior_ptid
= MERGEPID (pi
->pid
, thread
->tid
);
5811 args
->note_data
= procfs_do_thread_registers (args
->obfd
, inferior_ptid
,
5814 inferior_ptid
= saved_ptid
;
5820 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5822 struct cleanup
*old_chain
;
5823 gdb_gregset_t gregs
;
5824 gdb_fpregset_t fpregs
;
5825 char fname
[16] = {'\0'};
5826 char psargs
[80] = {'\0'};
5827 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5828 char *note_data
= NULL
;
5830 struct procfs_corefile_thread_data thread_args
;
5832 if (get_exec_file (0))
5834 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
5835 strncpy (psargs
, get_exec_file (0),
5838 inf_args
= get_inferior_args ();
5839 if (inf_args
&& *inf_args
&&
5840 strlen (inf_args
) < ((int) sizeof (psargs
) - (int) strlen (psargs
)))
5842 strncat (psargs
, " ",
5843 sizeof (psargs
) - strlen (psargs
));
5844 strncat (psargs
, inf_args
,
5845 sizeof (psargs
) - strlen (psargs
));
5849 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
5856 fill_gregset (&gregs
, -1);
5857 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
5858 PIDGET (inferior_ptid
),
5859 stop_signal
, &gregs
);
5862 thread_args
.obfd
= obfd
;
5863 thread_args
.note_data
= note_data
;
5864 thread_args
.note_size
= note_size
;
5865 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
, &thread_args
);
5867 if (thread_args
.note_data
== note_data
)
5869 /* iterate_over_threads didn't come up with any threads;
5870 just use inferior_ptid. */
5871 note_data
= procfs_do_thread_registers (obfd
, inferior_ptid
,
5872 note_data
, note_size
);
5876 note_data
= thread_args
.note_data
;
5879 make_cleanup (xfree
, note_data
);
5882 #else /* !(Solaris or Unixware) */
5884 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5886 error ("gcore not implemented for this host.");
5887 return NULL
; /* lint */
5889 #endif /* Solaris or Unixware */
5890 /* =================== END GCORE .NOTE "MODULE" =================== */