1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
2 Copyright 1999 Free Software Foundation, Inc.
3 Written by Michael Snyder at Cygnus Solutions.
4 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "gdbthread.h"
29 #if defined (NEW_PROC_API)
30 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
33 #include <sys/procfs.h>
34 #include <sys/fault.h>
35 #include <sys/syscall.h>
36 #include <sys/errno.h>
41 #include "proc-utils.h"
46 * This module provides the interface between GDB and the
47 * /proc file system, which is used on many versions of Unix
48 * as a means for debuggers to control other processes.
49 * Examples of the systems that use this interface are:
55 * /proc works by immitating a file system: you open a simulated file
56 * that represents the process you wish to interact with, and
57 * perform operations on that "file" in order to examine or change
58 * the state of the other process.
60 * The most important thing to know about /proc and this module
61 * is that there are two very different interfaces to /proc:
62 * One that uses the ioctl system call, and
63 * another that uses read and write system calls.
64 * This module has to support both /proc interfaces. This means
65 * that there are two different ways of doing every basic operation.
67 * In order to keep most of the code simple and clean, I have
68 * defined an interface "layer" which hides all these system calls.
69 * An ifdef (NEW_PROC_API) determines which interface we are using,
70 * and most or all occurrances of this ifdef should be confined to
71 * this interface layer.
75 /* Determine which /proc API we are using:
76 The ioctl API defines PIOCSTATUS, while
77 the read/write (multiple fd) API never does. */
80 #include <sys/types.h>
81 #include <dirent.h> /* opendir/readdir, for listing the LWP's */
84 #include <fcntl.h> /* for O_RDONLY */
85 #include <unistd.h> /* for "X_OK" */
86 #include "gdb_stat.h" /* for struct stat */
88 /* =================== TARGET_OPS "MODULE" =================== */
91 * This module defines the GDB target vector and its methods.
94 static void procfs_open
PARAMS((char *, int));
95 static void procfs_attach
PARAMS ((char *, int));
96 static void procfs_detach
PARAMS ((char *, int));
97 static void procfs_resume
PARAMS ((int, int, enum target_signal
));
98 static int procfs_can_run
PARAMS ((void));
99 static void procfs_stop
PARAMS ((void));
100 static void procfs_files_info
PARAMS ((struct target_ops
*));
101 static void procfs_fetch_registers
PARAMS ((int));
102 static void procfs_store_registers
PARAMS ((int));
103 static void procfs_notice_signals
PARAMS ((int));
104 static void procfs_prepare_to_store
PARAMS ((void));
105 static void procfs_kill_inferior
PARAMS ((void));
106 static void procfs_mourn_inferior
PARAMS ((void));
107 static void procfs_create_inferior
PARAMS ((char *, char *, char **));
108 static int procfs_wait
PARAMS ((int,
109 struct target_waitstatus
*));
110 static int procfs_xfer_memory
PARAMS ((CORE_ADDR
,
112 struct target_ops
*));
114 static int procfs_thread_alive
PARAMS ((int));
116 void procfs_find_new_threads
PARAMS ((void));
117 char *procfs_pid_to_str
PARAMS ((int));
119 struct target_ops procfs_ops
; /* the target vector */
124 procfs_ops
.to_shortname
= "procfs";
125 procfs_ops
.to_longname
= "Unix /proc child process";
127 "Unix /proc child process (started by the \"run\" command).";
128 procfs_ops
.to_open
= procfs_open
;
129 procfs_ops
.to_can_run
= procfs_can_run
;
130 procfs_ops
.to_create_inferior
= procfs_create_inferior
;
131 procfs_ops
.to_kill
= procfs_kill_inferior
;
132 procfs_ops
.to_mourn_inferior
= procfs_mourn_inferior
;
133 procfs_ops
.to_attach
= procfs_attach
;
134 procfs_ops
.to_detach
= procfs_detach
;
135 procfs_ops
.to_wait
= procfs_wait
;
136 procfs_ops
.to_resume
= procfs_resume
;
137 procfs_ops
.to_prepare_to_store
= procfs_prepare_to_store
;
138 procfs_ops
.to_fetch_registers
= procfs_fetch_registers
;
139 procfs_ops
.to_store_registers
= procfs_store_registers
;
140 procfs_ops
.to_xfer_memory
= procfs_xfer_memory
;
141 procfs_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
142 procfs_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
143 procfs_ops
.to_notice_signals
= procfs_notice_signals
;
144 procfs_ops
.to_files_info
= procfs_files_info
;
145 procfs_ops
.to_stop
= procfs_stop
;
147 procfs_ops
.to_terminal_init
= terminal_init_inferior
;
148 procfs_ops
.to_terminal_inferior
= terminal_inferior
;
149 procfs_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
150 procfs_ops
.to_terminal_ours
= terminal_ours
;
151 procfs_ops
.to_terminal_info
= child_terminal_info
;
153 procfs_ops
.to_find_new_threads
= procfs_find_new_threads
;
154 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
155 procfs_ops
.to_pid_to_str
= procfs_pid_to_str
;
157 procfs_ops
.to_has_all_memory
= 1;
158 procfs_ops
.to_has_memory
= 1;
159 procfs_ops
.to_has_execution
= 1;
160 procfs_ops
.to_has_stack
= 1;
161 procfs_ops
.to_has_registers
= 1;
162 procfs_ops
.to_stratum
= process_stratum
;
163 procfs_ops
.to_has_thread_control
= tc_schedlock
;
164 procfs_ops
.to_magic
= OPS_MAGIC
;
167 /* =================== END, TARGET_OPS "MODULE" =================== */
170 * Temporary debugging code:
172 * These macros allow me to trace the system calls that we make
173 * to control the child process. This is quite handy for comparing
174 * with the older version of procfs.
179 extern int write_with_trace
PARAMS ((int, void *, size_t, char *, int));
180 extern off_t lseek_with_trace
PARAMS ((int, off_t
, int, char *, int));
181 #define write(X,Y,Z) write_with_trace (X, Y, Z, __FILE__, __LINE__)
182 #define lseek(X,Y,Z) lseek_with_trace (X, Y, Z, __FILE__, __LINE__)
184 extern int ioctl_with_trace
PARAMS ((int, long, void *, char *, int));
185 #define ioctl(X,Y,Z) ioctl_with_trace (X, Y, Z, __FILE__, __LINE__)
187 #define open(X,Y) open_with_trace (X, Y, __FILE__, __LINE__)
188 #define close(X) close_with_trace (X, __FILE__, __LINE__)
189 #define wait(X) wait_with_trace (X, __FILE__, __LINE__)
190 #define PROCFS_NOTE(X) procfs_note (X, __FILE__, __LINE__)
191 #define PROC_PRETTYFPRINT_STATUS(X,Y,Z,T) \
192 proc_prettyfprint_status (X, Y, Z, T)
194 #define PROCFS_NOTE(X)
195 #define PROC_PRETTYFPRINT_STATUS(X,Y,Z,T)
202 * Put any typedefs, defines etc. here that are required for
203 * the unification of code that handles different versions of /proc.
206 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
208 enum { READ_WATCHFLAG
= WA_READ
,
209 WRITE_WATCHFLAG
= WA_WRITE
,
210 EXEC_WATCHFLAG
= WA_EXEC
,
211 AFTER_WATCHFLAG
= WA_TRAPAFTER
214 #else /* Irix method for watchpoints */
215 enum { READ_WATCHFLAG
= MA_READ
,
216 WRITE_WATCHFLAG
= MA_WRITE
,
217 EXEC_WATCHFLAG
= MA_EXEC
,
218 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
225 /* =================== STRUCT PROCINFO "MODULE" =================== */
227 /* FIXME: this comment will soon be out of date W.R.T. threads. */
229 /* The procinfo struct is a wrapper to hold all the state information
230 concerning a /proc process. There should be exactly one procinfo
231 for each process, and since GDB currently can debug only one
232 process at a time, that means there should be only one procinfo.
233 All of the LWP's of a process can be accessed indirectly thru the
234 single process procinfo.
236 However, against the day when GDB may debug more than one process,
237 this data structure is kept in a list (which for now will hold no
238 more than one member), and many functions will have a pointer to a
239 procinfo as an argument.
241 There will be a separate procinfo structure for use by the (not yet
242 implemented) "info proc" command, so that we can print useful
243 information about any random process without interfering with the
244 inferior's procinfo information. */
247 /* format strings for /proc paths */
248 # ifndef CTL_PROC_NAME_FMT
249 # define MAIN_PROC_NAME_FMT "/proc/%d"
250 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
251 # define AS_PROC_NAME_FMT "/proc/%d/as"
252 # define MAP_PROC_NAME_FMT "/proc/%d/map"
253 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
254 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
256 /* the name of the proc status struct depends on the implementation */
257 typedef pstatus_t gdb_prstatus_t
;
258 typedef lwpstatus_t gdb_lwpstatus_t
;
259 #else /* ! NEW_PROC_API */
260 /* format strings for /proc paths */
261 # ifndef CTL_PROC_NAME_FMT
262 # define MAIN_PROC_NAME_FMT "/proc/%05d"
263 # define CTL_PROC_NAME_FMT "/proc/%05d"
264 # define AS_PROC_NAME_FMT "/proc/%05d"
265 # define MAP_PROC_NAME_FMT "/proc/%05d"
266 # define STATUS_PROC_NAME_FMT "/proc/%05d"
267 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
269 /* the name of the proc status struct depends on the implementation */
270 typedef prstatus_t gdb_prstatus_t
;
271 typedef prstatus_t gdb_lwpstatus_t
;
272 #endif /* NEW_PROC_API */
275 /* These #ifdefs are for sol2.x in particular. sol2.x has
276 both a "gregset_t" and a "prgregset_t", which have
277 similar uses but different layouts. sol2.x gdb tries to
278 use prgregset_t (and prfpregset_t) everywhere. */
280 #ifdef GDB_GREGSET_TYPE
281 typedef GDB_GREGSET_TYPE gdb_gregset_t
;
283 typedef gregset_t gdb_gregset_t
;
286 #ifdef GDB_FPREGSET_TYPE
287 typedef GDB_FPREGSET_TYPE gdb_fpregset_t
;
289 typedef fpregset_t gdb_fpregset_t
;
292 /* Provide default composite pid manipulation macros for systems that
293 don't have threads. */
296 #define PIDGET(PID) (PID)
297 #define TIDGET(PID) (PID)
300 #define MERGEPID(PID, TID) (PID)
303 typedef struct procinfo
{
304 struct procinfo
*next
;
305 int pid
; /* Process ID */
306 int tid
; /* Thread/LWP id */
310 int ignore_next_sigstop
;
312 /* The following four fd fields may be identical, or may contain
313 several different fd's, depending on the version of /proc
314 (old ioctl or new read/write). */
316 int ctl_fd
; /* File descriptor for /proc control file */
318 * The next three file descriptors are actually only needed in the
319 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
320 * However, to avoid a bunch of #ifdefs in the code, we will use
321 * them uniformly by (in the case of the ioctl single-file-descriptor
322 * implementation) filling them with copies of the control fd.
324 int status_fd
; /* File descriptor for /proc status file */
325 int as_fd
; /* File descriptor for /proc as file */
327 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
329 fltset_t saved_fltset
; /* Saved traced hardware fault set */
330 sigset_t saved_sigset
; /* Saved traced signal set */
331 sigset_t saved_sighold
; /* Saved held signal set */
332 sysset_t saved_exitset
; /* Saved traced system call exit set */
333 sysset_t saved_entryset
; /* Saved traced system call entry set */
335 gdb_prstatus_t prstatus
; /* Current process status info */
338 gdb_fpregset_t fpregset
; /* Current floating point registers */
341 struct procinfo
*thread_list
;
343 int status_valid
: 1;
345 int fpregs_valid
: 1;
346 int threads_valid
: 1;
349 static char errmsg
[128]; /* shared error msg buffer */
351 /* Function prototypes for procinfo module: */
353 static procinfo
*find_procinfo_or_die
PARAMS ((int pid
, int tid
));
354 static procinfo
*find_procinfo
PARAMS ((int pid
, int tid
));
355 static procinfo
*create_procinfo
PARAMS ((int pid
, int tid
));
356 static void destroy_procinfo
PARAMS ((procinfo
*p
));
357 static void dead_procinfo
PARAMS ((procinfo
*p
,
358 char *msg
, int killp
));
359 static int open_procinfo_files
PARAMS ((procinfo
*p
, int which
));
360 static void close_procinfo_files
PARAMS ((procinfo
*p
));
362 /* The head of the procinfo list: */
363 static procinfo
* procinfo_list
;
366 * Function: find_procinfo
368 * Search the procinfo list.
370 * Returns: pointer to procinfo, or NULL if not found.
374 find_procinfo (pid
, tid
)
380 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
387 /* Don't check threads_valid. If we're updating the
388 thread_list, we want to find whatever threads are already
389 here. This means that in general it is the caller's
390 responsibility to check threads_valid and update before
391 calling find_procinfo, if the caller wants to find a new
394 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
403 * Function: find_procinfo_or_die
405 * Calls find_procinfo, but errors on failure.
409 find_procinfo_or_die (pid
, tid
)
413 procinfo
*pi
= find_procinfo (pid
, tid
);
418 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
421 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
427 * Function: open_procinfo_files
429 * Open the file descriptor for the process or LWP.
430 * ifdef NEW_PROC_API, we only open the control file descriptor;
431 * the others are opened lazily as needed.
432 * else (if not NEW_PROC_API), there is only one real
433 * file descriptor, but we keep multiple copies of it so that
434 * the code that uses them does not have to be #ifdef'd.
436 * Return: file descriptor, or zero for failure.
439 enum { FD_CTL
, FD_STATUS
, FD_AS
};
442 open_procinfo_files (pi
, which
)
447 char tmp
[MAX_PROC_NAME_SIZE
];
452 * This function is getting ALMOST long enough to break up into several.
453 * Here is some rationale:
455 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
456 * There are several file descriptors that may need to be open
457 * for any given process or LWP. The ones we're intereted in are:
458 * - control (ctl) write-only change the state
459 * - status (status) read-only query the state
460 * - address space (as) read/write access memory
461 * - map (map) read-only virtual addr map
462 * Most of these are opened lazily as they are needed.
463 * The pathnames for the 'files' for an LWP look slightly
464 * different from those of a first-class process:
465 * Pathnames for a process (<proc-id>):
466 * /proc/<proc-id>/ctl
467 * /proc/<proc-id>/status
469 * /proc/<proc-id>/map
470 * Pathnames for an LWP (lwp-id):
471 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
472 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
473 * An LWP has no map or address space file descriptor, since
474 * the memory map and address space are shared by all LWPs.
476 * Everyone else (Solaris 2.5, Irix, OSF)
477 * There is only one file descriptor for each process or LWP.
478 * For convenience, we copy the same file descriptor into all
479 * three fields of the procinfo struct (ctl_fd, status_fd, and
480 * as_fd, see NEW_PROC_API above) so that code that uses them
481 * doesn't need any #ifdef's.
486 * Each LWP has an independent file descriptor, but these
487 * are not obtained via the 'open' system call like the rest:
488 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
489 * to the file descriptor of the parent process.
492 * These do not even have their own independent file descriptor.
493 * All operations are carried out on the file descriptor of the
494 * parent process. Therefore we just call open again for each
495 * thread, getting a new handle for the same 'file'.
500 * In this case, there are several different file descriptors that
501 * we might be asked to open. The control file descriptor will be
502 * opened early, but the others will be opened lazily as they are
506 strcpy (tmp
, pi
->pathname
);
507 switch (which
) { /* which file descriptor to open? */
510 strcat (tmp
, "/lwpctl");
512 strcat (tmp
, "/ctl");
513 fd
= open (tmp
, O_WRONLY
);
520 return 0; /* there is no 'as' file descriptor for an lwp */
522 fd
= open (tmp
, O_RDWR
);
529 strcat (tmp
, "/lwpstatus");
531 strcat (tmp
, "/status");
532 fd
= open (tmp
, O_RDONLY
);
538 return 0; /* unknown file descriptor */
540 #else /* not NEW_PROC_API */
542 * In this case, there is only one file descriptor for each procinfo
543 * (ie. each process or LWP). In fact, only the file descriptor for
544 * the process can actually be opened by an 'open' system call.
545 * The ones for the LWPs have to be obtained thru an IOCTL call
546 * on the process's file descriptor.
548 * For convenience, we copy each procinfo's single file descriptor
549 * into all of the fields occupied by the several file descriptors
550 * of the NEW_PROC_API implementation. That way, the code that uses
551 * them can be written without ifdefs.
555 #ifdef PIOCTSTATUS /* OSF */
556 if ((fd
= open (pi
->pathname
, O_RDWR
)) == 0) /* Only one FD; just open it. */
558 #else /* Sol 2.5, Irix, other? */
559 if (pi
->tid
== 0) /* Master procinfo for the process */
561 fd
= open (pi
->pathname
, O_RDWR
);
565 else /* LWP thread procinfo */
567 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
571 /* Find the procinfo for the entire process. */
572 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
575 /* Now obtain the file descriptor for the LWP. */
576 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
578 #else /* Irix, other? */
579 return 0; /* Don't know how to open threads */
580 #endif /* Sol 2.5 PIOCOPENLWP */
582 #endif /* OSF PIOCTSTATUS */
583 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
584 #endif /* NEW_PROC_API */
586 return 1; /* success */
590 * Function: create_procinfo
592 * Allocate a data structure and link it into the procinfo list.
593 * (First tries to find a pre-existing one (FIXME: why?)
595 * Return: pointer to new procinfo struct.
599 create_procinfo (pid
, tid
)
603 procinfo
*pi
, *parent
;
605 if ((pi
= find_procinfo (pid
, tid
)))
606 return pi
; /* Already exists, nothing to do. */
608 /* find parent before doing malloc, to save having to cleanup */
610 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
612 doesn't exist yet? */
614 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
615 memset (pi
, 0, sizeof (procinfo
));
619 /* Chain into list. */
622 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
623 pi
->next
= procinfo_list
;
629 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
631 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
633 pi
->next
= parent
->thread_list
;
634 parent
->thread_list
= pi
;
640 * Function: close_procinfo_files
642 * Close all file descriptors associated with the procinfo
646 close_procinfo_files (pi
)
654 if (pi
->status_fd
> 0)
655 close (pi
->status_fd
);
657 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
661 * Function: destroy_procinfo
663 * Destructor function. Close, unlink and deallocate the object.
667 destroy_one_procinfo (list
, pi
)
673 /* Step one: unlink the procinfo from its list */
677 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
680 ptr
->next
= pi
->next
;
684 /* Step two: close any open file descriptors */
685 close_procinfo_files (pi
);
687 /* Step three: free the memory. */
692 destroy_procinfo (pi
)
697 if (pi
->tid
!= 0) /* destroy a thread procinfo */
699 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
700 destroy_one_procinfo (&tmp
->thread_list
, pi
);
702 else /* destroy a process procinfo and all its threads */
704 /* First destroy the children, if any; */
705 while (pi
->thread_list
!= NULL
)
706 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
707 /* Then destroy the parent. Genocide!!! */
708 destroy_one_procinfo (&procinfo_list
, pi
);
712 enum { NOKILL
, KILL
};
715 * Function: dead_procinfo
717 * To be called on a non_recoverable error for a procinfo.
718 * Prints error messages, optionally sends a SIGKILL to the process,
719 * then destroys the data structure.
723 dead_procinfo (pi
, msg
, kill_p
)
732 print_sys_errmsg (pi
->pathname
, errno
);
736 sprintf (procfile
, "process %d", pi
->pid
);
737 print_sys_errmsg (procfile
, errno
);
740 kill (pi
->pid
, SIGKILL
);
742 destroy_procinfo (pi
);
746 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
748 /* =================== /proc "MODULE" =================== */
751 * This "module" is the interface layer between the /proc system API
752 * and the gdb target vector functions. This layer consists of
753 * access functions that encapsulate each of the basic operations
754 * that we need to use from the /proc API.
756 * The main motivation for this layer is to hide the fact that
757 * there are two very different implementations of the /proc API.
758 * Rather than have a bunch of #ifdefs all thru the gdb target vector
759 * functions, we do our best to hide them all in here.
762 int proc_get_status
PARAMS ((procinfo
*pi
));
763 long proc_flags
PARAMS ((procinfo
*pi
));
764 int proc_why
PARAMS ((procinfo
*pi
));
765 int proc_what
PARAMS ((procinfo
*pi
));
766 int proc_set_run_on_last_close
PARAMS ((procinfo
*pi
));
767 int proc_unset_run_on_last_close
PARAMS ((procinfo
*pi
));
768 int proc_set_inherit_on_fork
PARAMS ((procinfo
*pi
));
769 int proc_unset_inherit_on_fork
PARAMS ((procinfo
*pi
));
770 int proc_set_async
PARAMS ((procinfo
*pi
));
771 int proc_unset_async
PARAMS ((procinfo
*pi
));
772 int proc_stop_process
PARAMS ((procinfo
*pi
));
773 int proc_trace_signal
PARAMS ((procinfo
*pi
, int signo
));
774 int proc_ignore_signal
PARAMS ((procinfo
*pi
, int signo
));
775 int proc_clear_current_fault
PARAMS ((procinfo
*pi
));
776 int proc_set_current_signal
PARAMS ((procinfo
*pi
, int signo
));
777 int proc_clear_current_signal
PARAMS ((procinfo
*pi
));
778 int proc_set_gregs
PARAMS ((procinfo
*pi
));
779 int proc_set_fpregs
PARAMS ((procinfo
*pi
));
780 int proc_wait_for_stop
PARAMS ((procinfo
*pi
));
781 int proc_run_process
PARAMS ((procinfo
*pi
, int step
, int signo
));
782 int proc_kill
PARAMS ((procinfo
*pi
, int signo
));
783 int proc_parent_pid
PARAMS ((procinfo
*pi
));
784 int proc_get_nthreads
PARAMS ((procinfo
*pi
));
785 int proc_get_current_thread
PARAMS ((procinfo
*pi
));
786 int proc_set_held_signals
PARAMS ((procinfo
*pi
, sigset_t
*sighold
));
787 int proc_set_traced_sysexit
PARAMS ((procinfo
*pi
, sysset_t
*sysset
));
788 int proc_set_traced_sysentry
PARAMS ((procinfo
*pi
, sysset_t
*sysset
));
789 int proc_set_traced_faults
PARAMS ((procinfo
*pi
, fltset_t
*fltset
));
790 int proc_set_traced_signals
PARAMS ((procinfo
*pi
, sigset_t
*sigset
));
792 int proc_update_threads
PARAMS ((procinfo
*pi
));
793 int proc_iterate_over_threads
PARAMS ((procinfo
*pi
,
794 int (*func
) PARAMS ((procinfo
*,
799 gdb_gregset_t
*proc_get_gregs
PARAMS ((procinfo
*pi
));
800 gdb_fpregset_t
*proc_get_fpregs
PARAMS ((procinfo
*pi
));
801 sysset_t
*proc_get_traced_sysexit
PARAMS ((procinfo
*pi
, sysset_t
*save
));
802 sysset_t
*proc_get_traced_sysentry
PARAMS ((procinfo
*pi
, sysset_t
*save
));
803 fltset_t
*proc_get_traced_faults
PARAMS ((procinfo
*pi
, fltset_t
*save
));
804 sigset_t
*proc_get_traced_signals
PARAMS ((procinfo
*pi
, sigset_t
*save
));
805 sigset_t
*proc_get_held_signals
PARAMS ((procinfo
*pi
, sigset_t
*save
));
806 sigset_t
*proc_get_pending_signals
PARAMS ((procinfo
*pi
, sigset_t
*save
));
807 struct sigaction
*proc_get_signal_actions
PARAMS ((procinfo
*pi
,
808 struct sigaction
*save
));
810 void proc_warn
PARAMS ((procinfo
*pi
, char *func
, int line
));
811 void proc_error
PARAMS ((procinfo
*pi
, char *func
, int line
));
814 proc_warn (pi
, func
, line
)
819 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
820 print_sys_errmsg (errmsg
, errno
);
824 proc_error (pi
, func
, line
)
829 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
830 perror_with_name (errmsg
);
834 * Function: proc_get_status
836 * Updates the status struct in the procinfo.
837 * There is a 'valid' flag, to let other functions know when
838 * this function needs to be called (so the status is only
839 * read when it is needed). The status file descriptor is
840 * also only opened when it is needed.
842 * Return: non-zero for success, zero for failure.
849 /* Status file descriptor is opened "lazily" */
850 if (pi
->status_fd
== 0 &&
851 open_procinfo_files (pi
, FD_STATUS
) == 0)
853 pi
->status_valid
= 0;
858 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
859 pi
->status_valid
= 0; /* fail */
862 /* Sigh... I have to read a different data structure,
863 depending on whether this is a main process or an LWP. */
865 pi
->status_valid
= (read (pi
->status_fd
,
866 (char *) &pi
->prstatus
.pr_lwp
,
867 sizeof (lwpstatus_t
))
868 == sizeof (lwpstatus_t
));
871 pi
->status_valid
= (read (pi
->status_fd
,
872 (char *) &pi
->prstatus
,
873 sizeof (gdb_prstatus_t
))
874 == sizeof (gdb_prstatus_t
));
875 #if 0 /*def UNIXWARE*/
876 if (pi
->status_valid
&&
877 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
878 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
879 /* Unixware peculiarity -- read the damn thing again! */
880 pi
->status_valid
= (read (pi
->status_fd
,
881 (char *) &pi
->prstatus
,
882 sizeof (gdb_prstatus_t
))
883 == sizeof (gdb_prstatus_t
));
884 #endif /* UNIXWARE */
887 #else /* ioctl method */
888 #ifdef PIOCTSTATUS /* osf */
889 if (pi
->tid
== 0) /* main process */
891 /* Just read the danged status. Now isn't that simple? */
893 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
900 tid_t pr_error_thread
;
901 struct prstatus status
;
904 thread_status
.pr_count
= 1;
905 thread_status
.status
.pr_tid
= pi
->tid
;
906 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
909 memcpy (&pi
->prstatus
, &thread_status
.status
,
910 sizeof (pi
->prstatus
));
911 pi
->status_valid
= 1;
915 /* Just read the danged status. Now isn't that simple? */
916 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
920 if (pi
->status_valid
)
922 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
925 proc_get_current_thread (pi
));
928 /* The status struct includes general regs, so mark them valid too */
929 pi
->gregs_valid
= pi
->status_valid
;
931 /* In the read/write multiple-fd model,
932 the status struct includes the fp regs too, so mark them valid too */
933 pi
->fpregs_valid
= pi
->status_valid
;
935 return pi
->status_valid
; /* True if success, false if failure. */
939 * Function: proc_flags
941 * returns the process flags (pr_flags field).
948 if (!pi
->status_valid
)
949 if (!proc_get_status (pi
))
950 return 0; /* FIXME: not a good failure value (but what is?) */
954 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
955 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
956 The two sets of flags don't overlap. */
957 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
959 return pi
->prstatus
.pr_lwp
.pr_flags
;
962 return pi
->prstatus
.pr_flags
;
969 * returns the pr_why field (why the process stopped).
976 if (!pi
->status_valid
)
977 if (!proc_get_status (pi
))
978 return 0; /* FIXME: not a good failure value (but what is?) */
981 return pi
->prstatus
.pr_lwp
.pr_why
;
983 return pi
->prstatus
.pr_why
;
988 * Function: proc_what
990 * returns the pr_what field (details of why the process stopped).
997 if (!pi
->status_valid
)
998 if (!proc_get_status (pi
))
999 return 0; /* FIXME: not a good failure value (but what is?) */
1002 return pi
->prstatus
.pr_lwp
.pr_what
;
1004 return pi
->prstatus
.pr_what
;
1008 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1010 * Function: proc_nsysarg
1012 * returns the pr_nsysarg field (number of args to the current syscall).
1019 if (!pi
->status_valid
)
1020 if (!proc_get_status (pi
))
1024 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1026 return pi
->prstatus
.pr_nsysarg
;
1031 * Function: proc_sysargs
1033 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1040 if (!pi
->status_valid
)
1041 if (!proc_get_status (pi
))
1045 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1047 return (long *) &pi
->prstatus
.pr_sysarg
;
1052 * Function: proc_syscall
1054 * returns the pr_syscall field (id of current syscall if we are in one).
1061 if (!pi
->status_valid
)
1062 if (!proc_get_status (pi
))
1066 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1068 return pi
->prstatus
.pr_syscall
;
1071 #endif /* PIOCSSPCACT */
1074 * Function: proc_cursig:
1076 * returns the pr_cursig field (current signal).
1080 proc_cursig (struct procinfo
*pi
)
1082 if (!pi
->status_valid
)
1083 if (!proc_get_status (pi
))
1084 return 0; /* FIXME: not a good failure value (but what is?) */
1087 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1089 return pi
->prstatus
.pr_cursig
;
1094 * Function: proc_modify_flag
1096 * === I appologize for the messiness of this function.
1097 * === This is an area where the different versions of
1098 * === /proc are more inconsistent than usual. MVS
1100 * Set or reset any of the following process flags:
1101 * PR_FORK -- forked child will inherit trace flags
1102 * PR_RLC -- traced process runs when last /proc file closed.
1103 * PR_KLC -- traced process is killed when last /proc file closed.
1104 * PR_ASYNC -- LWP's get to run/stop independently.
1106 * There are three methods for doing this function:
1107 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1109 * 2) Middle: PIOCSET/PIOCRESET
1111 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1114 * Note: Irix does not define PR_ASYNC.
1115 * Note: OSF does not define PR_KLC.
1116 * Note: OSF is the only one that can ONLY use the oldest method.
1119 * pi -- the procinfo
1120 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1121 * mode -- 1 for set, 0 for reset.
1123 * Returns non-zero for success, zero for failure.
1126 enum { FLAG_RESET
, FLAG_SET
};
1129 proc_modify_flag (pi
, flag
, mode
)
1134 long win
= 0; /* default to fail */
1137 * These operations affect the process as a whole, and applying
1138 * them to an individual LWP has the same meaning as applying them
1139 * to the main process. Therefore, if we're ever called with a
1140 * pointer to an LWP's procinfo, let's substitute the process's
1141 * procinfo and avoid opening the LWP's file descriptor
1146 pi
= find_procinfo_or_die (pi
->pid
, 0);
1148 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1149 /* First normalize the PCUNSET/PCRESET command opcode
1150 (which for no obvious reason has a different definition
1151 from one operating system to the next...) */
1153 #define GDBRESET PCUNSET
1156 #define GDBRESET PCRESET
1161 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1163 else /* Reset the flag */
1167 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1170 #ifdef PIOCSET /* Irix/Sol5 method */
1171 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1173 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1175 else /* Reset the flag */
1177 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1181 #ifdef PIOCSRLC /* Oldest method: OSF */
1184 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1186 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1188 else /* Clear run-on-last-close */
1190 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1194 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1196 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1198 else /* Clear inherit-on-fork */
1200 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1204 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1211 /* The above operation renders the procinfo's cached pstatus obsolete. */
1212 pi
->status_valid
= 0;
1215 warning ("procfs: modify_flag failed to turn %s %s",
1216 flag
== PR_FORK
? "PR_FORK" :
1217 flag
== PR_RLC
? "PR_RLC" :
1219 flag
== PR_ASYNC
? "PR_ASYNC" :
1222 flag
== PR_KLC
? "PR_KLC" :
1225 mode
== FLAG_RESET
? "off" : "on");
1231 * Function: proc_set_run_on_last_close
1233 * Set the run_on_last_close flag.
1234 * Process with all threads will become runnable
1235 * when debugger closes all /proc fds.
1237 * Returns non-zero for success, zero for failure.
1241 proc_set_run_on_last_close (pi
)
1244 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1248 * Function: proc_unset_run_on_last_close
1250 * Reset the run_on_last_close flag.
1251 * Process will NOT become runnable
1252 * when debugger closes its file handles.
1254 * Returns non-zero for success, zero for failure.
1258 proc_unset_run_on_last_close (pi
)
1261 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1266 * Function: proc_set_kill_on_last_close
1268 * Set the kill_on_last_close flag.
1269 * Process with all threads will be killed when debugger
1270 * closes all /proc fds (or debugger exits or dies).
1272 * Returns non-zero for success, zero for failure.
1276 proc_set_kill_on_last_close (pi
)
1279 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1283 * Function: proc_unset_kill_on_last_close
1285 * Reset the kill_on_last_close flag.
1286 * Process will NOT be killed when debugger
1287 * closes its file handles (or exits or dies).
1289 * Returns non-zero for success, zero for failure.
1293 proc_unset_kill_on_last_close (pi
)
1296 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1301 * Function: proc_set_inherit_on_fork
1303 * Set inherit_on_fork flag.
1304 * If the process forks a child while we are registered for events
1305 * in the parent, then we will also recieve events from the child.
1307 * Returns non-zero for success, zero for failure.
1311 proc_set_inherit_on_fork (pi
)
1314 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1318 * Function: proc_unset_inherit_on_fork
1320 * Reset inherit_on_fork flag.
1321 * If the process forks a child while we are registered for events
1322 * in the parent, then we will NOT recieve events from the child.
1324 * Returns non-zero for success, zero for failure.
1328 proc_unset_inherit_on_fork (pi
)
1331 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1336 * Function: proc_set_async
1338 * Set PR_ASYNC flag.
1339 * If one LWP stops because of a debug event (signal etc.),
1340 * the remaining LWPs will continue to run.
1342 * Returns non-zero for success, zero for failure.
1349 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1353 * Function: proc_unset_async
1355 * Reset PR_ASYNC flag.
1356 * If one LWP stops because of a debug event (signal etc.),
1357 * then all other LWPs will stop as well.
1359 * Returns non-zero for success, zero for failure.
1363 proc_unset_async (pi
)
1366 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1368 #endif /* PR_ASYNC */
1371 * Function: proc_stop_process
1373 * Request the process/LWP to stop. Does not wait.
1374 * Returns non-zero for success, zero for failure.
1378 proc_stop_process (pi
)
1384 * We might conceivably apply this operation to an LWP, and
1385 * the LWP's ctl file descriptor might not be open.
1388 if (pi
->ctl_fd
== 0 &&
1389 open_procinfo_files (pi
, FD_CTL
) == 0)
1395 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1396 #else /* ioctl method */
1397 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1398 /* Note: the call also reads the prstatus. */
1401 pi
->status_valid
= 1;
1402 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1405 proc_get_current_thread (pi
));
1414 * Function: proc_wait_for_stop
1416 * Wait for the process or LWP to stop (block until it does).
1417 * Returns non-zero for success, zero for failure.
1421 proc_wait_for_stop (pi
)
1427 * We should never have to apply this operation to any procinfo
1428 * except the one for the main process. If that ever changes
1429 * for any reason, then take out the following clause and
1430 * replace it with one that makes sure the ctl_fd is open.
1434 pi
= find_procinfo_or_die (pi
->pid
, 0);
1439 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1440 /* We been runnin' and we stopped -- need to update status. */
1441 pi
->status_valid
= 0;
1443 #else /* ioctl method */
1444 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1445 /* Above call also refreshes the prstatus. */
1448 pi
->status_valid
= 1;
1449 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1452 proc_get_current_thread (pi
));
1460 * Function: proc_run_process
1462 * Make the process or LWP runnable.
1463 * Options (not all are implemented):
1465 * - clear current fault
1466 * - clear current signal
1467 * - abort the current system call
1468 * - stop as soon as finished with system call
1469 * - (ioctl): set traced signal set
1470 * - (ioctl): set held signal set
1471 * - (ioctl): set traced fault set
1472 * - (ioctl): set start pc (vaddr)
1473 * Always clear the current fault.
1474 * Clear the current signal if 'signo' is zero.
1477 * pi the process or LWP to operate on.
1478 * step if true, set the process or LWP to trap after one instr.
1479 * signo if zero, clear the current signal if any.
1480 * if non-zero, set the current signal to this one.
1482 * Returns non-zero for success, zero for failure.
1486 proc_run_process (pi
, step
, signo
)
1495 * We will probably have to apply this operation to individual threads,
1496 * so make sure the control file descriptor is open.
1499 if (pi
->ctl_fd
== 0 &&
1500 open_procinfo_files (pi
, FD_CTL
) == 0)
1505 runflags
= PRCFAULT
; /* always clear current fault */
1510 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1511 proc_set_current_signal (pi
, signo
);
1519 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1521 #else /* ioctl method */
1525 memset (&prrun
, 0, sizeof (prrun
));
1526 prrun
.pr_flags
= runflags
;
1527 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1535 * Function: proc_set_traced_signals
1537 * Register to trace signals in the process or LWP.
1538 * Returns non-zero for success, zero for failure.
1542 proc_set_traced_signals (pi
, sigset
)
1549 * We should never have to apply this operation to any procinfo
1550 * except the one for the main process. If that ever changes
1551 * for any reason, then take out the following clause and
1552 * replace it with one that makes sure the ctl_fd is open.
1556 pi
= find_procinfo_or_die (pi
->pid
, 0);
1562 /* Use char array to avoid alignment issues. */
1563 char sigset
[sizeof (sigset_t
)];
1567 memcpy (&arg
.sigset
, sigset
, sizeof (sigset_t
));
1569 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1571 #else /* ioctl method */
1572 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1574 /* The above operation renders the procinfo's cached pstatus obsolete. */
1575 pi
->status_valid
= 0;
1578 warning ("procfs: set_traced_signals failed");
1583 * Function: proc_set_traced_faults
1585 * Register to trace hardware faults in the process or LWP.
1586 * Returns non-zero for success, zero for failure.
1590 proc_set_traced_faults (pi
, fltset
)
1597 * We should never have to apply this operation to any procinfo
1598 * except the one for the main process. If that ever changes
1599 * for any reason, then take out the following clause and
1600 * replace it with one that makes sure the ctl_fd is open.
1604 pi
= find_procinfo_or_die (pi
->pid
, 0);
1610 /* Use char array to avoid alignment issues. */
1611 char fltset
[sizeof (fltset_t
)];
1615 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1617 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1619 #else /* ioctl method */
1620 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1622 /* The above operation renders the procinfo's cached pstatus obsolete. */
1623 pi
->status_valid
= 0;
1629 * Function: proc_set_traced_sysentry
1631 * Register to trace entry to system calls in the process or LWP.
1632 * Returns non-zero for success, zero for failure.
1636 proc_set_traced_sysentry (pi
, sysset
)
1643 * We should never have to apply this operation to any procinfo
1644 * except the one for the main process. If that ever changes
1645 * for any reason, then take out the following clause and
1646 * replace it with one that makes sure the ctl_fd is open.
1650 pi
= find_procinfo_or_die (pi
->pid
, 0);
1656 /* Use char array to avoid alignment issues. */
1657 char sysset
[sizeof (sysset_t
)];
1661 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1663 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1665 #else /* ioctl method */
1666 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1668 /* The above operation renders the procinfo's cached pstatus obsolete. */
1669 pi
->status_valid
= 0;
1675 * Function: proc_set_traced_sysexit
1677 * Register to trace exit from system calls in the process or LWP.
1678 * Returns non-zero for success, zero for failure.
1682 proc_set_traced_sysexit (pi
, sysset
)
1689 * We should never have to apply this operation to any procinfo
1690 * except the one for the main process. If that ever changes
1691 * for any reason, then take out the following clause and
1692 * replace it with one that makes sure the ctl_fd is open.
1696 pi
= find_procinfo_or_die (pi
->pid
, 0);
1702 /* Use char array to avoid alignment issues. */
1703 char sysset
[sizeof (sysset_t
)];
1707 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1709 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1711 #else /* ioctl method */
1712 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1714 /* The above operation renders the procinfo's cached pstatus obsolete. */
1715 pi
->status_valid
= 0;
1721 * Function: proc_set_held_signals
1723 * Specify the set of blocked / held signals in the process or LWP.
1724 * Returns non-zero for success, zero for failure.
1728 proc_set_held_signals (pi
, sighold
)
1735 * We should never have to apply this operation to any procinfo
1736 * except the one for the main process. If that ever changes
1737 * for any reason, then take out the following clause and
1738 * replace it with one that makes sure the ctl_fd is open.
1742 pi
= find_procinfo_or_die (pi
->pid
, 0);
1748 /* Use char array to avoid alignment issues. */
1749 char hold
[sizeof (sigset_t
)];
1753 memcpy (&arg
.hold
, sighold
, sizeof (sigset_t
));
1754 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1757 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1759 /* The above operation renders the procinfo's cached pstatus obsolete. */
1760 pi
->status_valid
= 0;
1766 * Function: proc_get_pending_signals
1768 * returns the set of signals that are pending in the process or LWP.
1769 * Will also copy the sigset if 'save' is non-zero.
1773 proc_get_pending_signals (pi
, save
)
1777 sigset_t
*ret
= NULL
;
1780 * We should never have to apply this operation to any procinfo
1781 * except the one for the main process. If that ever changes
1782 * for any reason, then take out the following clause and
1783 * replace it with one that makes sure the ctl_fd is open.
1787 pi
= find_procinfo_or_die (pi
->pid
, 0);
1789 if (!pi
->status_valid
)
1790 if (!proc_get_status (pi
))
1794 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
1796 ret
= &pi
->prstatus
.pr_sigpend
;
1799 memcpy (save
, ret
, sizeof (sigset_t
));
1805 * Function: proc_get_signal_actions
1807 * returns the set of signal actions.
1808 * Will also copy the sigactionset if 'save' is non-zero.
1812 proc_get_signal_actions (pi
, save
)
1814 struct sigaction
*save
;
1816 struct sigaction
*ret
= NULL
;
1819 * We should never have to apply this operation to any procinfo
1820 * except the one for the main process. If that ever changes
1821 * for any reason, then take out the following clause and
1822 * replace it with one that makes sure the ctl_fd is open.
1826 pi
= find_procinfo_or_die (pi
->pid
, 0);
1828 if (!pi
->status_valid
)
1829 if (!proc_get_status (pi
))
1833 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
1835 ret
= &pi
->prstatus
.pr_action
;
1838 memcpy (save
, ret
, sizeof (struct sigaction
));
1844 * Function: proc_get_held_signals
1846 * returns the set of signals that are held / blocked.
1847 * Will also copy the sigset if 'save' is non-zero.
1851 proc_get_held_signals (pi
, save
)
1855 sigset_t
*ret
= NULL
;
1858 * We should never have to apply this operation to any procinfo
1859 * except the one for the main process. If that ever changes
1860 * for any reason, then take out the following clause and
1861 * replace it with one that makes sure the ctl_fd is open.
1865 pi
= find_procinfo_or_die (pi
->pid
, 0);
1868 if (!pi
->status_valid
)
1869 if (!proc_get_status (pi
))
1873 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
1875 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1876 #endif /* UNIXWARE */
1877 #else /* not NEW_PROC_API */
1879 static sigset_t sigheld
;
1881 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
1884 #endif /* NEW_PROC_API */
1886 memcpy (save
, ret
, sizeof (sigset_t
));
1892 * Function: proc_get_traced_signals
1894 * returns the set of signals that are traced / debugged.
1895 * Will also copy the sigset if 'save' is non-zero.
1899 proc_get_traced_signals (pi
, save
)
1903 sigset_t
*ret
= NULL
;
1906 * We should never have to apply this operation to any procinfo
1907 * except the one for the main process. If that ever changes
1908 * for any reason, then take out the following clause and
1909 * replace it with one that makes sure the ctl_fd is open.
1913 pi
= find_procinfo_or_die (pi
->pid
, 0);
1916 if (!pi
->status_valid
)
1917 if (!proc_get_status (pi
))
1920 ret
= &pi
->prstatus
.pr_sigtrace
;
1923 static sigset_t sigtrace
;
1925 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
1930 memcpy (save
, ret
, sizeof (sigset_t
));
1936 * Function: proc_trace_signal
1938 * Add 'signo' to the set of signals that are traced.
1939 * Returns non-zero for success, zero for failure.
1943 proc_trace_signal (pi
, signo
)
1950 * We should never have to apply this operation to any procinfo
1951 * except the one for the main process. If that ever changes
1952 * for any reason, then take out the following clause and
1953 * replace it with one that makes sure the ctl_fd is open.
1957 pi
= find_procinfo_or_die (pi
->pid
, 0);
1961 if (proc_get_traced_signals (pi
, &temp
))
1963 praddset (&temp
, signo
);
1964 return proc_set_traced_signals (pi
, &temp
);
1968 return 0; /* failure */
1972 * Function: proc_ignore_signal
1974 * Remove 'signo' from the set of signals that are traced.
1975 * Returns non-zero for success, zero for failure.
1979 proc_ignore_signal (pi
, signo
)
1986 * We should never have to apply this operation to any procinfo
1987 * except the one for the main process. If that ever changes
1988 * for any reason, then take out the following clause and
1989 * replace it with one that makes sure the ctl_fd is open.
1993 pi
= find_procinfo_or_die (pi
->pid
, 0);
1997 if (proc_get_traced_signals (pi
, &temp
))
1999 prdelset (&temp
, signo
);
2000 return proc_set_traced_signals (pi
, &temp
);
2004 return 0; /* failure */
2008 * Function: proc_get_traced_faults
2010 * returns the set of hardware faults that are traced /debugged.
2011 * Will also copy the faultset if 'save' is non-zero.
2015 proc_get_traced_faults (pi
, save
)
2019 fltset_t
*ret
= NULL
;
2022 * We should never have to apply this operation to any procinfo
2023 * except the one for the main process. If that ever changes
2024 * for any reason, then take out the following clause and
2025 * replace it with one that makes sure the ctl_fd is open.
2029 pi
= find_procinfo_or_die (pi
->pid
, 0);
2032 if (!pi
->status_valid
)
2033 if (!proc_get_status (pi
))
2036 ret
= &pi
->prstatus
.pr_flttrace
;
2039 static fltset_t flttrace
;
2041 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2046 memcpy (save
, ret
, sizeof (fltset_t
));
2052 * Function: proc_get_traced_sysentry
2054 * returns the set of syscalls that are traced /debugged on entry.
2055 * Will also copy the syscall set if 'save' is non-zero.
2059 proc_get_traced_sysentry (pi
, save
)
2063 sysset_t
*ret
= NULL
;
2066 * We should never have to apply this operation to any procinfo
2067 * except the one for the main process. If that ever changes
2068 * for any reason, then take out the following clause and
2069 * replace it with one that makes sure the ctl_fd is open.
2073 pi
= find_procinfo_or_die (pi
->pid
, 0);
2076 if (!pi
->status_valid
)
2077 if (!proc_get_status (pi
))
2080 ret
= &pi
->prstatus
.pr_sysentry
;
2083 static sysset_t sysentry
;
2085 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2090 memcpy (save
, ret
, sizeof (sysset_t
));
2096 * Function: proc_get_traced_sysexit
2098 * returns the set of syscalls that are traced /debugged on exit.
2099 * Will also copy the syscall set if 'save' is non-zero.
2103 proc_get_traced_sysexit (pi
, save
)
2107 sysset_t
* ret
= NULL
;
2110 * We should never have to apply this operation to any procinfo
2111 * except the one for the main process. If that ever changes
2112 * for any reason, then take out the following clause and
2113 * replace it with one that makes sure the ctl_fd is open.
2117 pi
= find_procinfo_or_die (pi
->pid
, 0);
2120 if (!pi
->status_valid
)
2121 if (!proc_get_status (pi
))
2124 ret
= &pi
->prstatus
.pr_sysexit
;
2127 static sysset_t sysexit
;
2129 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2134 memcpy (save
, ret
, sizeof (sysset_t
));
2140 * Function: proc_clear_current_fault
2142 * The current fault (if any) is cleared; the associated signal
2143 * will not be sent to the process or LWP when it resumes.
2144 * Returns non-zero for success, zero for failure.
2148 proc_clear_current_fault (pi
)
2154 * We should never have to apply this operation to any procinfo
2155 * except the one for the main process. If that ever changes
2156 * for any reason, then take out the following clause and
2157 * replace it with one that makes sure the ctl_fd is open.
2161 pi
= find_procinfo_or_die (pi
->pid
, 0);
2166 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2169 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2176 * Function: proc_set_current_signal
2178 * Set the "current signal" that will be delivered next to the process.
2179 * NOTE: semantics are different from those of KILL.
2180 * This signal will be delivered to the process or LWP
2181 * immediately when it is resumed (even if the signal is held/blocked);
2182 * it will NOT immediately cause another event of interest, and will NOT
2183 * first trap back to the debugger.
2185 * Returns non-zero for success, zero for failure.
2189 proc_set_current_signal (pi
, signo
)
2196 /* Use char array to avoid alignment issues. */
2197 char sinfo
[sizeof (struct siginfo
)];
2199 struct siginfo
*mysinfo
;
2202 * We should never have to apply this operation to any procinfo
2203 * except the one for the main process. If that ever changes
2204 * for any reason, then take out the following clause and
2205 * replace it with one that makes sure the ctl_fd is open.
2209 pi
= find_procinfo_or_die (pi
->pid
, 0);
2211 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2212 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2213 * receives a PIOCSSIG with a signal identical to the current signal,
2214 * it messes up the current signal. Work around the kernel bug.
2217 signo
== proc_cursig (pi
))
2218 return 1; /* I assume this is a success? */
2221 /* The pointer is just a type alias. */
2222 mysinfo
= (struct siginfo
*) &arg
.sinfo
;
2223 mysinfo
->si_signo
= signo
;
2224 mysinfo
->si_code
= 0;
2225 mysinfo
->si_pid
= getpid (); /* ?why? */
2226 mysinfo
->si_uid
= getuid (); /* ?why? */
2230 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2232 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2239 * Function: proc_clear_current_signal
2241 * The current signal (if any) is cleared, and
2242 * is not sent to the process or LWP when it resumes.
2243 * Returns non-zero for success, zero for failure.
2247 proc_clear_current_signal (pi
)
2253 * We should never have to apply this operation to any procinfo
2254 * except the one for the main process. If that ever changes
2255 * for any reason, then take out the following clause and
2256 * replace it with one that makes sure the ctl_fd is open.
2260 pi
= find_procinfo_or_die (pi
->pid
, 0);
2266 /* Use char array to avoid alignment issues. */
2267 char sinfo
[sizeof (struct siginfo
)];
2269 struct siginfo
*mysinfo
;
2272 /* The pointer is just a type alias. */
2273 mysinfo
= (struct siginfo
*) &arg
.sinfo
;
2274 mysinfo
->si_signo
= 0;
2275 mysinfo
->si_code
= 0;
2276 mysinfo
->si_errno
= 0;
2277 mysinfo
->si_pid
= getpid (); /* ?why? */
2278 mysinfo
->si_uid
= getuid (); /* ?why? */
2280 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2283 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2290 * Function: proc_get_gregs
2292 * Get the general registers for the process or LWP.
2293 * Returns non-zero for success, zero for failure.
2300 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2301 if (!proc_get_status (pi
))
2305 * OK, sorry about the ifdef's.
2306 * There's three cases instead of two, because
2307 * in this instance Unixware and Solaris/RW differ.
2311 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2312 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2313 #else /* not Unixware */
2314 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2315 #endif /* Unixware */
2316 #else /* not NEW_PROC_API */
2317 return &pi
->prstatus
.pr_reg
;
2318 #endif /* NEW_PROC_API */
2322 * Function: proc_get_fpregs
2324 * Get the floating point registers for the process or LWP.
2325 * Returns non-zero for success, zero for failure.
2329 proc_get_fpregs (pi
)
2333 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2334 if (!proc_get_status (pi
))
2337 #ifdef UNIXWARE /* a true architecture dependency */
2338 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2340 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2341 #endif /* Unixware */
2343 #else /* not NEW_PROC_API */
2344 if (pi
->fpregs_valid
)
2345 return &pi
->fpregset
; /* already got 'em */
2348 if (pi
->ctl_fd
== 0 &&
2349 open_procinfo_files (pi
, FD_CTL
) == 0)
2358 tid_t pr_error_thread
;
2359 tfpregset_t thread_1
;
2362 thread_fpregs
.pr_count
= 1;
2363 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2366 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2368 pi
->fpregs_valid
= 1;
2369 return &pi
->fpregset
; /* got 'em now! */
2371 else if (pi
->tid
!= 0 &&
2372 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2374 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2375 sizeof (pi
->fpregset
));
2376 pi
->fpregs_valid
= 1;
2377 return &pi
->fpregset
; /* got 'em now! */
2384 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2386 pi
->fpregs_valid
= 1;
2387 return &pi
->fpregset
; /* got 'em now! */
2400 * Function: proc_set_gregs
2402 * Write the general registers back to the process or LWP.
2403 * Returns non-zero for success, zero for failure.
2410 gdb_gregset_t
*gregs
;
2413 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2414 return 0; /* get_regs has already warned */
2416 if (pi
->ctl_fd
== 0 &&
2417 open_procinfo_files (pi
, FD_CTL
) == 0)
2426 /* Use char array to avoid alignment issues. */
2427 char gregs
[sizeof (gdb_gregset_t
)];
2431 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2432 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2434 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2438 /* Policy: writing the regs invalidates our cache. */
2439 pi
->gregs_valid
= 0;
2444 * Function: proc_set_fpregs
2446 * Modify the floating point register set of the process or LWP.
2447 * Returns non-zero for success, zero for failure.
2451 proc_set_fpregs (pi
)
2454 gdb_fpregset_t
*fpregs
;
2457 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2458 return 0; /* get_fpregs has already warned */
2460 if (pi
->ctl_fd
== 0 &&
2461 open_procinfo_files (pi
, FD_CTL
) == 0)
2470 /* Use char array to avoid alignment issues. */
2471 char fpregs
[sizeof (gdb_fpregset_t
)];
2475 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2476 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2480 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2485 tid_t pr_error_thread
;
2486 tfpregset_t thread_1
;
2489 thread_fpregs
.pr_count
= 1;
2490 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2491 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2493 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2496 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2497 #endif /* osf PIOCTSFPREG */
2498 #endif /* NEW_PROC_API */
2501 /* Policy: writing the regs invalidates our cache. */
2502 pi
->fpregs_valid
= 0;
2507 * Function: proc_kill
2509 * Send a signal to the proc or lwp with the semantics of "kill()".
2510 * Returns non-zero for success, zero for failure.
2514 proc_kill (pi
, signo
)
2521 * We might conceivably apply this operation to an LWP, and
2522 * the LWP's ctl file descriptor might not be open.
2525 if (pi
->ctl_fd
== 0 &&
2526 open_procinfo_files (pi
, FD_CTL
) == 0)
2537 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2538 #else /* ioctl method */
2539 /* FIXME: do I need the Alpha OSF fixups present in
2540 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2541 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2549 * Function: proc_parent_pid
2551 * Find the pid of the process that started this one.
2552 * Returns the parent process pid, or zero.
2556 proc_parent_pid (pi
)
2560 * We should never have to apply this operation to any procinfo
2561 * except the one for the main process. If that ever changes
2562 * for any reason, then take out the following clause and
2563 * replace it with one that makes sure the ctl_fd is open.
2567 pi
= find_procinfo_or_die (pi
->pid
, 0);
2569 if (!pi
->status_valid
)
2570 if (!proc_get_status (pi
))
2573 return pi
->prstatus
.pr_ppid
;
2578 * Function: proc_set_watchpoint
2583 proc_set_watchpoint (pi
, addr
, len
, wflags
)
2589 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2592 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2593 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2598 char watch
[sizeof (prwatch_t
)];
2602 pwatch
= (prwatch_t
*) &arg
.watch
;
2603 pwatch
->pr_vaddr
= addr
;
2604 pwatch
->pr_size
= len
;
2605 pwatch
->pr_wflags
= wflags
;
2606 #if defined(NEW_PROC_API) && defined (PCWATCH)
2608 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2610 #if defined (PIOCSWATCH)
2611 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2613 return 0; /* Fail */
2621 * Function: proc_iterate_over_mappings
2623 * Given a pointer to a function, call that function once for every
2624 * mapped address space in the process. The callback function
2625 * receives an open file descriptor for the file corresponding to
2626 * that mapped address space (if there is one), and the base address
2627 * of the mapped space. Quit when the callback function returns a
2628 * nonzero value, or at teh end of the mappings.
2630 * Returns: the first non-zero return value of the callback function,
2634 /* FIXME: it's probably a waste to cache this FD.
2635 It doesn't get called that often... and if I open it
2636 every time, I don't need to lseek it. */
2638 proc_iterate_over_mappings (func
)
2639 int (*func
) PARAMS ((int, CORE_ADDR
));
2643 #ifndef NEW_PROC_API /* avoid compiler warning */
2648 char pathname
[MAX_PROC_NAME_SIZE
];
2653 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
2657 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
2658 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
2659 proc_error (pi
, "proc_iterate_over_mappings (open)", __LINE__
);
2661 /* Make sure it gets closed again. */
2662 make_cleanup ((make_cleanup_func
) close
, (void *) map_fd
);
2664 /* Allocate space for mapping (lifetime only for this function). */
2665 map
= alloca (sizeof (struct prmap
));
2667 /* Now read the mappings from the file,
2668 open a file descriptor for those that have a name,
2669 and call the callback function. */
2670 while (read (map_fd
,
2672 sizeof (struct prmap
)) == sizeof (struct prmap
))
2674 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
2676 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
2679 if (map
->pr_mapname
[0] == 0)
2681 fd
= -1; /* no map file */
2685 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
2686 /* Note: caller's responsibility to close this fd! */
2687 fd
= open (name
, O_RDONLY
);
2688 /* Note: we don't test the above call for failure;
2689 we just pass the FD on as given. Sometimes there is
2690 no file, so the ioctl may return failure, but that's
2694 /* Stop looping if the callback returns non-zero. */
2695 if ((funcstat
= (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
)) != 0)
2699 /* Get the number of mapping entries. */
2700 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmaps
) < 0)
2701 proc_error (pi
, "proc_iterate_over_mappings (PIOCNMAP)", __LINE__
);
2703 /* Allocate space for mappings (lifetime only this function). */
2704 map
= (struct prmap
*) alloca ((nmaps
+ 1) * sizeof (struct prmap
));
2706 /* Read in all the mappings. */
2707 if (ioctl (pi
->ctl_fd
, PIOCMAP
, map
) < 0)
2708 proc_error (pi
, "proc_iterate_over_mappings (PIOCMAP)", __LINE__
);
2710 /* Now loop through the mappings, open an fd for each, and
2711 call the callback function. */
2713 i
< nmaps
&& map
[i
].pr_size
!= 0;
2716 /* Note: caller's responsibility to close this fd! */
2717 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
[i
].pr_vaddr
);
2718 /* Note: we don't test the above call for failure;
2719 we just pass the FD on as given. Sometimes there is
2720 no file, so the ioctl may return failure, but that's
2723 /* Stop looping if the callback returns non-zero. */
2724 if ((funcstat
= (*func
) (fd
, (CORE_ADDR
) map
[i
].pr_vaddr
)) != 0)
2732 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2734 #include <sys/sysi86.h>
2737 * Function: proc_get_LDT_entry
2743 * The 'key' is actually the value of the lower 16 bits of
2744 * the GS register for the LWP that we're interested in.
2746 * Return: matching ssh struct (LDT entry).
2750 proc_get_LDT_entry (pi
, key
)
2754 static struct ssd
*ldt_entry
= NULL
;
2756 char pathname
[MAX_PROC_NAME_SIZE
];
2757 struct cleanup
*old_chain
= NULL
;
2760 /* Allocate space for one LDT entry.
2761 This alloc must persist, because we return a pointer to it. */
2762 if (ldt_entry
== NULL
)
2763 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2765 /* Open the file descriptor for the LDT table. */
2766 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2767 if ((fd
= open (pathname
, O_RDONLY
)) < 0)
2769 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2772 /* Make sure it gets closed again! */
2773 old_chain
= make_cleanup ((make_cleanup_func
) close
, (void *) fd
);
2775 /* Now 'read' thru the table, find a match and return it. */
2776 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2778 if (ldt_entry
->sel
== 0 &&
2779 ldt_entry
->bo
== 0 &&
2780 ldt_entry
->acc1
== 0 &&
2781 ldt_entry
->acc2
== 0)
2782 break; /* end of table */
2783 /* If key matches, return this entry. */
2784 if (ldt_entry
->sel
== key
)
2787 /* Loop ended, match not found. */
2791 static int nalloc
= 0;
2793 /* Get the number of LDT entries. */
2794 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2796 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2800 /* Allocate space for the number of LDT entries. */
2801 /* This alloc has to persist, 'cause we return a pointer to it. */
2804 ldt_entry
= (struct ssd
*)
2805 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2809 /* Read the whole table in one gulp. */
2810 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2812 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2816 /* Search the table and return the (first) entry matching 'key'. */
2817 for (i
= 0; i
< nldt
; i
++)
2818 if (ldt_entry
[i
].sel
== key
)
2819 return &ldt_entry
[i
];
2821 /* Loop ended, match not found. */
2826 #endif /* TM_I386SOL2_H */
2828 /* =============== END, non-thread part of /proc "MODULE" =============== */
2830 /* =================== Thread "MODULE" =================== */
2832 /* NOTE: you'll see more ifdefs and duplication of functions here,
2833 since there is a different way to do threads on every OS. */
2836 * Function: proc_get_nthreads
2838 * Return the number of threads for the process
2841 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2846 proc_get_nthreads (pi
)
2851 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
2852 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
2858 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2860 * Solaris and Unixware version
2863 proc_get_nthreads (pi
)
2866 if (!pi
->status_valid
)
2867 if (!proc_get_status (pi
))
2871 * NEW_PROC_API: only works for the process procinfo,
2872 * because the LWP procinfos do not get prstatus filled in.
2875 if (pi
->tid
!= 0) /* find the parent process procinfo */
2876 pi
= find_procinfo_or_die (pi
->pid
, 0);
2878 return pi
->prstatus
.pr_nlwp
;
2886 proc_get_nthreads (pi
)
2895 * Function: proc_get_current_thread (LWP version)
2897 * Return the ID of the thread that had an event of interest.
2898 * (ie. the one that hit a breakpoint or other traced event).
2899 * All other things being equal, this should be the ID of a
2900 * thread that is currently executing.
2903 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2905 * Solaris and Unixware version
2908 proc_get_current_thread (pi
)
2912 * Note: this should be applied to the root procinfo for the process,
2913 * not to the procinfo for an LWP. If applied to the procinfo for
2914 * an LWP, it will simply return that LWP's ID. In that case,
2915 * find the parent process procinfo.
2919 pi
= find_procinfo_or_die (pi
->pid
, 0);
2921 if (!pi
->status_valid
)
2922 if (!proc_get_status (pi
))
2926 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
2928 return pi
->prstatus
.pr_who
;
2933 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2938 proc_get_current_thread (pi
)
2941 #if 0 /* FIXME: not ready for prime time? */
2942 return pi
->prstatus
.pr_tid
;
2953 proc_get_current_thread (pi
)
2963 * Function: proc_update_threads
2965 * Discover the IDs of all the threads within the process, and
2966 * create a procinfo for each of them (chained to the parent).
2968 * This unfortunately requires a different method on every OS.
2970 * Return: non-zero for success, zero for failure.
2974 proc_delete_dead_threads (parent
, thread
, ignore
)
2979 if (thread
&& parent
) /* sanity */
2981 thread
->status_valid
= 0;
2982 if (!proc_get_status (thread
))
2983 destroy_one_procinfo (&parent
->thread_list
, thread
);
2985 return 0; /* keep iterating */
2988 #if defined (PIOCLSTATUS)
2990 * Solaris 2.5 (ioctl) version
2993 proc_update_threads (pi
)
2996 gdb_prstatus_t
*prstatus
;
2997 struct cleanup
*old_chain
= NULL
;
3002 * We should never have to apply this operation to any procinfo
3003 * except the one for the main process. If that ever changes
3004 * for any reason, then take out the following clause and
3005 * replace it with one that makes sure the ctl_fd is open.
3009 pi
= find_procinfo_or_die (pi
->pid
, 0);
3011 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3013 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
3014 return 1; /* Process is not multi-threaded; nothing to do. */
3016 if ((prstatus
= (gdb_prstatus_t
*)
3017 malloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1))) == 0)
3018 perror_with_name ("procfs: malloc failed in update_threads");
3020 old_chain
= make_cleanup (free
, prstatus
);
3021 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
3022 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
3024 /* Skip element zero, which represents the process as a whole. */
3025 for (i
= 1; i
< nlwp
+ 1; i
++)
3027 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
3028 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3030 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
3031 thread
->status_valid
= 1;
3033 pi
->threads_valid
= 1;
3034 do_cleanups (old_chain
);
3040 * Unixware and Solaris 6 (and later) version
3043 proc_update_threads (pi
)
3046 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3047 struct dirent
*direntry
;
3048 struct cleanup
*old_chain
= NULL
;
3054 * We should never have to apply this operation to any procinfo
3055 * except the one for the main process. If that ever changes
3056 * for any reason, then take out the following clause and
3057 * replace it with one that makes sure the ctl_fd is open.
3061 pi
= find_procinfo_or_die (pi
->pid
, 0);
3063 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3068 * Note: this brute-force method is the only way I know of
3069 * to accomplish this task on Unixware. This method will
3070 * also work on Solaris 2.6 and 2.7. There is a much simpler
3071 * and more elegant way to do this on Solaris, but the margins
3072 * of this manuscript are too small to write it here... ;-)
3075 strcpy (pathname
, pi
->pathname
);
3076 strcat (pathname
, "/lwp");
3077 if ((dirp
= opendir (pathname
)) == NULL
)
3078 proc_error (pi
, "update_threads, opendir", __LINE__
);
3080 old_chain
= make_cleanup ((make_cleanup_func
) closedir
, dirp
);
3081 while ((direntry
= readdir (dirp
)) != NULL
)
3082 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3084 lwpid
= atoi (&direntry
->d_name
[0]);
3085 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3086 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3088 pi
->threads_valid
= 1;
3089 do_cleanups (old_chain
);
3098 proc_update_threads (pi
)
3105 * We should never have to apply this operation to any procinfo
3106 * except the one for the main process. If that ever changes
3107 * for any reason, then take out the following clause and
3108 * replace it with one that makes sure the ctl_fd is open.
3112 pi
= find_procinfo_or_die (pi
->pid
, 0);
3114 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3116 nthreads
= proc_get_nthreads (pi
);
3118 return 0; /* nothing to do for 1 or fewer threads */
3120 if ((threads
= malloc (nthreads
* sizeof (tid_t
))) == NULL
)
3121 proc_error (pi
, "update_threads, malloc", __LINE__
);
3123 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3124 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3126 for (i
= 0; i
< nthreads
; i
++)
3128 if (!find_procinfo (pi
->pid
, threads
[i
]))
3129 if (!create_procinfo (pi
->pid
, threads
[i
]))
3130 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3132 pi
->threads_valid
= 1;
3140 proc_update_threads (pi
)
3145 #endif /* OSF PIOCTLIST */
3146 #endif /* NEW_PROC_API */
3147 #endif /* SOL 2.5 PIOCLSTATUS */
3150 * Function: proc_iterate_over_threads
3153 * Given a pointer to a function, call that function once
3154 * for each lwp in the procinfo list, until the function
3155 * returns non-zero, in which event return the value
3156 * returned by the function.
3158 * Note: this function does NOT call update_threads.
3159 * If you want to discover new threads first, you must
3160 * call that function explicitly. This function just makes
3161 * a quick pass over the currently-known procinfos.
3164 * pi - parent process procinfo
3165 * func - per-thread function
3166 * ptr - opaque parameter for function.
3169 * First non-zero return value from the callee, or zero.
3173 proc_iterate_over_threads (pi
, func
, ptr
)
3175 int (*func
) PARAMS ((procinfo
*, procinfo
*, void *));
3178 procinfo
*thread
, *next
;
3182 * We should never have to apply this operation to any procinfo
3183 * except the one for the main process. If that ever changes
3184 * for any reason, then take out the following clause and
3185 * replace it with one that makes sure the ctl_fd is open.
3189 pi
= find_procinfo_or_die (pi
->pid
, 0);
3191 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3193 next
= thread
->next
; /* in case thread is destroyed */
3194 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3201 /* =================== END, Thread "MODULE" =================== */
3203 /* =================== END, /proc "MODULE" =================== */
3205 /* =================== GDB "MODULE" =================== */
3208 * Here are all of the gdb target vector functions and their friends.
3211 static int do_attach
PARAMS ((int pid
));
3212 static void do_detach
PARAMS ((int signo
));
3213 static int register_gdb_signals
PARAMS ((procinfo
*, sigset_t
*));
3216 * Function: procfs_debug_inferior
3218 * Sets up the inferior to be debugged.
3219 * Registers to trace signals, hardware faults, and syscalls.
3220 * Note: does not set RLC flag: caller may want to customize that.
3222 * Returns: zero for success (note! unlike most functions in this module)
3223 * On failure, returns the LINE NUMBER where it failed!
3227 procfs_debug_inferior (pi
)
3230 fltset_t traced_faults
;
3231 sigset_t traced_signals
;
3232 sysset_t traced_syscall_entries
;
3233 sysset_t traced_syscall_exits
;
3235 #ifdef PROCFS_DONT_TRACE_FAULTS
3236 /* On some systems (OSF), we don't trace hardware faults.
3237 Apparently it's enough that we catch them as signals.
3238 Wonder why we don't just do that in general? */
3239 premptyset (&traced_faults
); /* don't trace faults. */
3241 /* Register to trace hardware faults in the child. */
3242 prfillset (&traced_faults
); /* trace all faults... */
3243 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3245 if (!proc_set_traced_faults (pi
, &traced_faults
))
3248 /* Register to trace selected signals in the child. */
3249 premptyset (&traced_signals
);
3250 if (!register_gdb_signals (pi
, &traced_signals
))
3253 /* Register to trace the 'exit' system call (on entry). */
3254 premptyset (&traced_syscall_entries
);
3255 praddset (&traced_syscall_entries
, SYS_exit
);
3257 praddset (&traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3260 praddset (&traced_syscall_entries
, SYS_lwp_exit
);
3263 if (!proc_set_traced_sysentry (pi
, &traced_syscall_entries
))
3266 #ifdef PRFS_STOPEXEC /* defined on OSF */
3267 /* OSF method for tracing exec syscalls. Quoting:
3268 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3269 exits from exec system calls because of the user level loader. */
3270 /* FIXME: make nice and maybe move into an access function. */
3274 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3277 prfs_flags
|= PRFS_STOPEXEC
;
3279 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3282 #else /* not PRFS_STOPEXEC */
3283 /* Everyone else's (except OSF) method for tracing exec syscalls */
3285 Not all systems with /proc have all the exec* syscalls with the same
3286 names. On the SGI, for example, there is no SYS_exec, but there
3287 *is* a SYS_execv. So, we try to account for that. */
3289 premptyset (&traced_syscall_exits
);
3291 praddset (&traced_syscall_exits
, SYS_exec
);
3294 praddset (&traced_syscall_exits
, SYS_execve
);
3297 praddset (&traced_syscall_exits
, SYS_execv
);
3300 #ifdef SYS_lwpcreate
3301 praddset (&traced_syscall_exits
, SYS_lwpcreate
);
3302 praddset (&traced_syscall_exits
, SYS_lwpexit
);
3305 #ifdef SYS_lwp_create /* FIXME: once only, please */
3306 praddset (&traced_syscall_exits
, SYS_lwp_create
);
3307 praddset (&traced_syscall_exits
, SYS_lwp_exit
);
3311 if (!proc_set_traced_sysexit (pi
, &traced_syscall_exits
))
3314 #endif /* PRFS_STOPEXEC */
3319 procfs_attach (args
, from_tty
)
3327 error_no_arg ("process-id to attach");
3330 if (pid
== getpid ())
3331 error ("Attaching GDB to itself is not a good idea...");
3335 exec_file
= get_exec_file (0);
3338 printf_filtered ("Attaching to program `%s', %s\n",
3339 exec_file
, target_pid_to_str (pid
));
3341 printf_filtered ("Attaching to %s\n", target_pid_to_str (pid
));
3345 inferior_pid
= do_attach (pid
);
3346 push_target (&procfs_ops
);
3350 procfs_detach (args
, from_tty
)
3359 exec_file
= get_exec_file (0);
3362 printf_filtered ("Detaching from program: %s %s\n",
3363 exec_file
, target_pid_to_str (inferior_pid
));
3367 signo
= atoi (args
);
3371 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3381 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
3382 perror ("procfs: out of memory in 'attach'");
3384 if (!open_procinfo_files (pi
, FD_CTL
))
3386 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3387 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3389 dead_procinfo (pi
, errmsg
, NOKILL
);
3392 /* Stop the process (if it isn't already stopped). */
3393 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3395 pi
->was_stopped
= 1;
3396 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3400 pi
->was_stopped
= 0;
3401 /* Set the process to run again when we close it. */
3402 if (!proc_set_run_on_last_close (pi
))
3403 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3405 /* Now stop the process. */
3406 if (!proc_stop_process (pi
))
3407 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3408 pi
->ignore_next_sigstop
= 1;
3410 /* Save some of the /proc state to be restored if we detach. */
3411 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3412 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3413 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3414 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3415 if (!proc_get_traced_sysentry (pi
, &pi
->saved_entryset
))
3416 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3418 if (!proc_get_traced_sysexit (pi
, &pi
->saved_exitset
))
3419 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3421 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3422 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3424 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3425 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3427 /* Let GDB know that the inferior was attached. */
3429 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3438 /* Find procinfo for the main process */
3439 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0); /* FIXME: threads */
3441 if (!proc_set_current_signal (pi
, signo
))
3442 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3444 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3445 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3447 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3448 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3450 if (!proc_set_traced_sysentry (pi
, &pi
->saved_entryset
))
3451 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3453 if (!proc_set_traced_sysexit (pi
, &pi
->saved_exitset
))
3454 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3456 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3457 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3459 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3460 if (signo
|| !(pi
->was_stopped
) ||
3461 query ("Was stopped when attached, make it runnable again? "))
3463 /* Clear any pending signal. */
3464 if (!proc_clear_current_fault (pi
))
3465 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3467 if (!proc_set_run_on_last_close (pi
))
3468 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3472 destroy_procinfo (pi
);
3478 * Since the /proc interface cannot give us individual registers,
3479 * we pay no attention to the (regno) argument, and just fetch them all.
3480 * This results in the possibility that we will do unnecessarily many
3481 * fetches, since we may be called repeatedly for individual registers.
3482 * So we cache the results, and mark the cache invalid when the process
3486 /* These could go in a header file, but the many and various
3487 definitions of gregset_t would make it tricky and ugly. Several
3488 different native operating systems (notably Solaris and Linux) have
3489 various different definitions for gregset_t and fpregset_t. We
3490 have been kludging around this problem for a while, it would be
3491 nice if someday we came up with a prettier way of handling it
3494 extern void fill_gregset (gdb_gregset_t
*, int);
3495 extern void fill_fpregset (gdb_fpregset_t
*, int);
3496 extern void supply_gregset (gdb_gregset_t
*);
3497 extern void supply_fpregset (gdb_fpregset_t
*);
3500 procfs_fetch_registers (regno
)
3503 gdb_fpregset_t
*fpregs
;
3504 gdb_gregset_t
*gregs
;
3509 pid
= PIDGET (inferior_pid
);
3510 tid
= TIDGET (inferior_pid
);
3512 /* First look up procinfo for the main process. */
3513 pi
= find_procinfo_or_die (pid
, 0);
3515 /* If the event thread is not the same as GDB's requested thread
3516 (ie. inferior_pid), then look up procinfo for the requested
3519 (tid
!= proc_get_current_thread (pi
)))
3520 pi
= find_procinfo_or_die (pid
, tid
);
3523 error ("procfs: fetch_registers failed to find procinfo for %s",
3524 target_pid_to_str (inferior_pid
));
3526 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3527 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3529 supply_gregset (gregs
);
3531 #if defined (FP0_REGNUM) /* need floating point? */
3532 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3533 regno
== PC_REGNUM
||
3535 regno
== NPC_REGNUM
||
3537 regno
== FP_REGNUM
||
3539 return; /* not a floating point register */
3541 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3542 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3544 supply_fpregset (fpregs
);
3548 /* Get ready to modify the registers array. On machines which store
3549 individual registers, this doesn't need to do anything. On
3550 machines which store all the registers in one fell swoop, such as
3551 /proc, this makes sure that registers contains all the registers
3552 from the program being debugged. */
3555 procfs_prepare_to_store ()
3557 #ifdef CHILD_PREPARE_TO_STORE
3558 CHILD_PREPARE_TO_STORE ();
3565 * Since the /proc interface will not read individual registers,
3566 * we will cache these requests until the process is resumed, and
3567 * only then write them back to the inferior process.
3569 * FIXME: is that a really bad idea? Have to think about cases
3570 * where writing one register might affect the value of others, etc.
3574 procfs_store_registers (regno
)
3577 gdb_fpregset_t
*fpregs
;
3578 gdb_gregset_t
*gregs
;
3583 pid
= PIDGET (inferior_pid
);
3584 tid
= TIDGET (inferior_pid
);
3586 /* First find procinfo for main process */
3587 pi
= find_procinfo_or_die (pid
, 0);
3589 /* If current lwp for process is not the same as requested thread
3590 (ie. inferior_pid), then find procinfo for the requested thread. */
3593 (tid
!= proc_get_current_thread (pi
)))
3594 pi
= find_procinfo_or_die (pid
, tid
);
3597 error ("procfs: store_registers: failed to find procinfo for %s",
3598 target_pid_to_str (inferior_pid
));
3600 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3601 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3603 fill_gregset (gregs
, regno
);
3604 if (!proc_set_gregs (pi
))
3605 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3607 #if defined (FP0_REGNUM) /* need floating point? */
3608 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3609 regno
== PC_REGNUM
||
3611 regno
== NPC_REGNUM
||
3613 regno
== FP_REGNUM
||
3615 return; /* not a floating point register */
3617 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3618 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3620 fill_fpregset (fpregs
, regno
);
3621 if (!proc_set_fpregs (pi
))
3622 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3627 * Function: target_wait
3629 * Retrieve the next stop event from the child process.
3630 * If child has not stopped yet, wait for it to stop.
3631 * Translate /proc eventcodes (or possibly wait eventcodes)
3632 * into gdb internal event codes.
3634 * Return: id of process (and possibly thread) that incurred the event.
3635 * event codes are returned thru a pointer parameter.
3639 procfs_wait (pid
, status
)
3641 struct target_waitstatus
*status
;
3643 /* First cut: loosely based on original version 2.1 */
3647 int why
, what
, flags
;
3656 /* Find procinfo for main process */
3657 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
3660 /* We must assume that the status is stale now... */
3661 pi
->status_valid
= 0;
3662 pi
->gregs_valid
= 0;
3663 pi
->fpregs_valid
= 0;
3665 #if 0 /* just try this out... */
3666 flags
= proc_flags (pi
);
3667 why
= proc_why (pi
);
3668 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3669 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3671 /* If child is not stopped, wait for it to stop. */
3672 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3673 !proc_wait_for_stop (pi
))
3675 /* wait_for_stop failed: has the child terminated? */
3676 if (errno
== ENOENT
)
3678 /* /proc file not found; presumably child has terminated. */
3679 retval
= wait (&wstat
); /* "wait" for the child's exit */
3681 if (retval
!= PIDGET (inferior_pid
)) /* wrong child? */
3682 error ("procfs: couldn't stop process %d: wait returned %d\n",
3683 inferior_pid
, retval
);
3684 /* FIXME: might I not just use waitpid?
3685 Or try find_procinfo to see if I know about this child? */
3689 /* Unknown error from wait_for_stop. */
3690 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3695 /* This long block is reached if either:
3696 a) the child was already stopped, or
3697 b) we successfully waited for the child with wait_for_stop.
3698 This block will analyze the /proc status, and translate it
3699 into a waitstatus for GDB.
3701 If we actually had to call wait because the /proc file
3702 is gone (child terminated), then we skip this block,
3703 because we already have a waitstatus. */
3705 flags
= proc_flags (pi
);
3706 why
= proc_why (pi
);
3707 what
= proc_what (pi
);
3709 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3712 /* If it's running async (for single_thread control),
3713 set it back to normal again. */
3714 if (flags
& PR_ASYNC
)
3715 if (!proc_unset_async (pi
))
3716 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3720 proc_prettyprint_why (why
, what
, 1);
3722 /* The 'pid' we will return to GDB is composed of
3723 the process ID plus the lwp ID. */
3724 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3728 wstat
= (what
<< 8) | 0177;
3738 #if defined (SYS_lwp_exit) || defined (SYS_lwpexit)
3739 printf_filtered ("[%s exited]\n",
3740 target_pid_to_str (retval
));
3741 delete_thread (retval
);
3742 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3744 #endif /* _lwp_exit */
3747 /* Handle SYS_exit call only */
3748 /* Stopped at entry to SYS_exit.
3749 Make it runnable, resume it, then use
3750 the wait system call to get its exit code.
3751 Proc_run_process always clears the current
3753 Then return its exit status. */
3754 pi
->status_valid
= 0;
3756 /* FIXME: what we should do is return
3757 TARGET_WAITKIND_SPURIOUS. */
3758 if (!proc_run_process (pi
, 0, 0))
3759 proc_error (pi
, "target_wait, run_process", __LINE__
);
3762 /* Don't call wait: simulate waiting for exit,
3763 return a "success" exit code. Bogus: what if
3764 it returns something else? */
3766 retval
= inferior_pid
; /* ? ? ? */
3770 int temp
= wait (&wstat
);
3772 /* FIXME: shouldn't I make sure I get the right
3773 event from the right process? If (for
3774 instance) I have killed an earlier inferior
3775 process but failed to clean up after it
3776 somehow, I could get its termination event
3779 /* If wait returns -1, that's what we return to GDB. */
3785 printf_filtered ("procfs: trapped on entry to ");
3786 proc_prettyprint_syscall (proc_what (pi
), 0);
3787 printf_filtered ("\n");
3790 long i
, nsysargs
, *sysargs
;
3792 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3793 (sysargs
= proc_sysargs (pi
)) != NULL
)
3795 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
3796 for (i
= 0; i
< nsysargs
; i
++)
3797 printf_filtered ("#%ld: 0x%08lx\n",
3805 /* How to exit gracefully, returning "unknown event" */
3806 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3807 return inferior_pid
;
3811 /* How to keep going without returning to wfi: */
3812 target_resume (pid
, 0, TARGET_SIGNAL_0
);
3829 /* Hopefully this is our own "fork-child" execing
3830 the real child. Hoax this event into a trap, and
3831 GDB will see the child about to execute its start
3833 wstat
= (SIGTRAP
<< 8) | 0177;
3835 #ifdef SYS_lwp_create
3836 case SYS_lwp_create
:
3838 #ifdef SYS_lwpcreate
3841 #if defined(SYS_lwp_create) || defined(SYS_lwpcreate)
3843 * This syscall is somewhat like fork/exec.
3844 * We will get the event twice: once for the parent LWP,
3845 * and once for the child. We should already know about
3846 * the parent LWP, but the child will be new to us. So,
3847 * whenever we get this event, if it represents a new
3848 * thread, simply add the thread to the list.
3851 /* If not in procinfo list, add it. */
3852 temp
= proc_get_current_thread (pi
);
3853 if (!find_procinfo (pi
->pid
, temp
))
3854 create_procinfo (pi
->pid
, temp
);
3856 temp
= MERGEPID (pi
->pid
, temp
);
3857 /* If not in GDB's thread list, add it. */
3858 if (!in_thread_list (temp
))
3860 printf_filtered ("[New %s]\n", target_pid_to_str (temp
));
3863 /* Return to WFI, but tell it to immediately resume. */
3864 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3865 return inferior_pid
;
3866 #endif /* _lwp_create */
3874 #if defined (SYS_lwp_exit) || defined (SYS_lwpexit)
3875 printf_filtered ("[%s exited]\n",
3876 target_pid_to_str (retval
));
3877 delete_thread (retval
);
3878 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3880 #endif /* _lwp_exit */
3884 /* Nothing to do here for now. The old procfs
3885 seemed to use this event to handle threads on
3886 older (non-LWP) systems, where I'm assuming that
3887 threads were actually separate processes. Irix,
3888 maybe? Anyway, low priority for now. */
3892 /* FIXME: do we need to handle this? Investigate. */
3896 /* FIXME: see above. */
3899 printf_filtered ("procfs: trapped on exit from ");
3900 proc_prettyprint_syscall (proc_what (pi
), 0);
3901 printf_filtered ("\n");
3904 long i
, nsysargs
, *sysargs
;
3906 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3907 (sysargs
= proc_sysargs (pi
)) != NULL
)
3909 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
3910 for (i
= 0; i
< nsysargs
; i
++)
3911 printf_filtered ("#%ld: 0x%08lx\n",
3916 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3917 return inferior_pid
;
3922 wstat
= (SIGSTOP
<< 8) | 0177;
3927 printf_filtered ("Retry #%d:\n", retry
);
3928 pi
->status_valid
= 0;
3933 /* If not in procinfo list, add it. */
3934 temp
= proc_get_current_thread (pi
);
3935 if (!find_procinfo (pi
->pid
, temp
))
3936 create_procinfo (pi
->pid
, temp
);
3938 /* If not in GDB's thread list, add it. */
3939 temp
= MERGEPID (pi
->pid
, temp
);
3940 if (!in_thread_list (temp
))
3942 printf_filtered ("[New %s]\n",
3943 target_pid_to_str (temp
));
3947 status
->kind
= TARGET_WAITKIND_STOPPED
;
3948 status
->value
.sig
= 0;
3953 wstat
= (what
<< 8) | 0177;
3956 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
3959 wstat
= (SIGTRAP
<< 8) | 0177;
3964 wstat
= (SIGTRAP
<< 8) | 0177;
3967 /* FIXME: use si_signo where possible. */
3969 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
3972 wstat
= (SIGILL
<< 8) | 0177;
3975 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
3978 wstat
= (SIGTRAP
<< 8) | 0177;
3982 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
3985 wstat
= (SIGSEGV
<< 8) | 0177;
3989 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
3992 wstat
= (SIGFPE
<< 8) | 0177;
3994 case FLTPAGE
: /* Recoverable page fault */
3995 default: /* FIXME: use si_signo if possible for fault */
3997 printf_filtered ("procfs:%d -- ", __LINE__
);
3998 printf_filtered ("child stopped for unknown reason:\n");
3999 proc_prettyprint_why (why
, what
, 1);
4000 error ("... giving up...");
4003 break; /* case PR_FAULTED: */
4004 default: /* switch (why) unmatched */
4005 printf_filtered ("procfs:%d -- ", __LINE__
);
4006 printf_filtered ("child stopped for unknown reason:\n");
4007 proc_prettyprint_why (why
, what
, 1);
4008 error ("... giving up...");
4012 * Got this far without error:
4013 * If retval isn't in the threads database, add it.
4016 retval
!= inferior_pid
&&
4017 !in_thread_list (retval
))
4020 * We have a new thread.
4021 * We need to add it both to GDB's list and to our own.
4022 * If we don't create a procinfo, resume may be unhappy
4025 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
4026 add_thread (retval
);
4027 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
4028 create_procinfo (PIDGET (retval
), TIDGET (retval
));
4030 /* In addition, it's possible that this is the first
4031 * new thread we've seen, in which case we may not
4032 * have created entries for inferior_pid yet.
4034 if (TIDGET (inferior_pid
) != 0)
4036 if (!in_thread_list (inferior_pid
))
4037 add_thread (inferior_pid
);
4038 if (find_procinfo (PIDGET (inferior_pid
),
4039 TIDGET (inferior_pid
)) == NULL
)
4040 create_procinfo (PIDGET (inferior_pid
),
4041 TIDGET (inferior_pid
));
4045 else /* flags do not indicate STOPPED */
4047 /* surely this can't happen... */
4048 printf_filtered ("procfs:%d -- process not stopped.\n",
4050 proc_prettyprint_flags (flags
, 1);
4051 error ("procfs: ...giving up...");
4056 store_waitstatus (status
, wstat
);
4063 procfs_xfer_memory (memaddr
, myaddr
, len
, dowrite
, target
)
4068 struct target_ops
*target
; /* ignored */
4073 /* Find procinfo for main process */
4074 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4075 if (pi
->as_fd
== 0 &&
4076 open_procinfo_files (pi
, FD_AS
) == 0)
4078 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4082 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4087 PROCFS_NOTE ("write memory: ");
4089 PROCFS_NOTE ("write memory: \n");
4091 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4095 PROCFS_NOTE ("read memory: \n");
4096 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4107 * Function: invalidate_cache
4109 * Called by target_resume before making child runnable.
4110 * Mark cached registers and status's invalid.
4111 * If there are "dirty" caches that need to be written back
4112 * to the child process, do that.
4114 * File descriptors are also cached.
4115 * As they are a limited resource, we cannot hold onto them indefinitely.
4116 * However, as they are expensive to open, we don't want to throw them
4117 * away indescriminately either. As a compromise, we will keep the
4118 * file descriptors for the parent process, but discard any file
4119 * descriptors we may have accumulated for the threads.
4122 * As this function is called by iterate_over_threads, it always
4123 * returns zero (so that iterate_over_threads will keep iterating).
4128 invalidate_cache (parent
, pi
, ptr
)
4134 * About to run the child; invalidate caches and do any other cleanup.
4138 if (pi
->gregs_dirty
)
4139 if (parent
== NULL
||
4140 proc_get_current_thread (parent
) != pi
->tid
)
4141 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4142 proc_warn (pi
, "target_resume, set_gregs",
4145 if (pi
->fpregs_dirty
)
4146 if (parent
== NULL
||
4147 proc_get_current_thread (parent
) != pi
->tid
)
4148 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4149 proc_warn (pi
, "target_resume, set_fpregs",
4156 /* The presence of a parent indicates that this is an LWP.
4157 Close any file descriptors that it might have open.
4158 We don't do this to the master (parent) procinfo. */
4160 close_procinfo_files (pi
);
4162 pi
->gregs_valid
= 0;
4163 pi
->fpregs_valid
= 0;
4165 pi
->gregs_dirty
= 0;
4166 pi
->fpregs_dirty
= 0;
4168 pi
->status_valid
= 0;
4169 pi
->threads_valid
= 0;
4176 * Function: make_signal_thread_runnable
4178 * A callback function for iterate_over_threads.
4179 * Find the asynchronous signal thread, and make it runnable.
4180 * See if that helps matters any.
4184 make_signal_thread_runnable (process
, pi
, ptr
)
4190 if (proc_flags (pi
) & PR_ASLWP
)
4192 if (!proc_run_process (pi
, 0, -1))
4193 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4202 * Function: target_resume
4204 * Make the child process runnable. Normally we will then call
4205 * procfs_wait and wait for it to stop again (unles gdb is async).
4208 * step: if true, then arrange for the child to stop again
4209 * after executing a single instruction.
4210 * signo: if zero, then cancel any pending signal.
4211 * If non-zero, then arrange for the indicated signal
4212 * to be delivered to the child when it runs.
4213 * pid: if -1, then allow any child thread to run.
4214 * if non-zero, then allow only the indicated thread to run.
4215 ******* (not implemented yet)
4219 procfs_resume (pid
, step
, signo
)
4222 enum target_signal signo
;
4224 procinfo
*pi
, *thread
;
4228 prrun.prflags |= PRSVADDR;
4229 prrun.pr_vaddr = $PC; set resume address
4230 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4231 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4232 prrun.prflags |= PRCFAULT; clear current fault.
4234 PRSTRACE and PRSFAULT can be done by other means
4235 (proc_trace_signals, proc_trace_faults)
4236 PRSVADDR is unnecessary.
4237 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4238 This basically leaves PRSTEP and PRCSIG.
4239 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4240 So basically PR_STEP is the sole argument that must be passed
4241 to proc_run_process (for use in the prrun struct by ioctl). */
4243 /* Find procinfo for main process */
4244 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4246 /* First cut: ignore pid argument */
4249 /* Convert signal to host numbering. */
4251 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4254 native_signo
= target_signal_to_host (signo
);
4256 pi
->ignore_next_sigstop
= 0;
4258 /* Running the process voids all cached registers and status. */
4259 /* Void the threads' caches first */
4260 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4261 /* Void the process procinfo's caches. */
4262 invalidate_cache (NULL
, pi
, NULL
);
4266 /* Resume a specific thread, presumably suppressing the others. */
4267 thread
= find_procinfo (PIDGET (pid
), TIDGET (pid
));
4269 warning ("procfs: resume can't find thread %d -- resuming all.",
4273 if (thread
->tid
!= 0)
4275 /* We're to resume a specific thread, and not the others.
4276 * Set the child process's PR_ASYNC flag.
4279 if (!proc_set_async (pi
))
4280 proc_error (pi
, "target_resume, set_async", __LINE__
);
4283 proc_iterate_over_threads (pi
,
4284 make_signal_thread_runnable
,
4287 pi
= thread
; /* substitute the thread's procinfo for run */
4292 if (!proc_run_process (pi
, step
, native_signo
))
4295 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4297 proc_error (pi
, "target_resume", __LINE__
);
4302 * Function: register_gdb_signals
4304 * Traverse the list of signals that GDB knows about
4305 * (see "handle" command), and arrange for the target
4306 * to be stopped or not, according to these settings.
4308 * Returns non-zero for success, zero for failure.
4312 register_gdb_signals (pi
, signals
)
4318 for (signo
= 0; signo
< NSIG
; signo
++)
4319 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4320 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4321 signal_pass_state (target_signal_from_host (signo
)) == 1)
4322 prdelset (signals
, signo
);
4324 praddset (signals
, signo
);
4326 return proc_set_traced_signals (pi
, signals
);
4330 * Function: target_notice_signals
4332 * Set up to trace signals in the child process.
4336 procfs_notice_signals (pid
)
4340 procinfo
*pi
= find_procinfo_or_die (PIDGET (pid
), 0);
4342 if (proc_get_traced_signals (pi
, &signals
) &&
4343 register_gdb_signals (pi
, &signals
))
4346 proc_error (pi
, "notice_signals", __LINE__
);
4350 * Function: target_files_info
4352 * Print status information about the child process.
4356 procfs_files_info (ignore
)
4357 struct target_ops
*ignore
;
4359 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4360 attach_flag
? "attached": "child",
4361 target_pid_to_str (inferior_pid
));
4365 * Function: target_open
4367 * A dummy: you don't open procfs.
4371 procfs_open (args
, from_tty
)
4375 error ("Use the \"run\" command to start a Unix child process.");
4379 * Function: target_can_run
4381 * This tells GDB that this target vector can be invoked
4382 * for "run" or "attach".
4385 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4386 be a runnable target. Used by targets
4387 that can sit atop procfs, such as solaris
4394 /* This variable is controlled by modules that sit atop procfs that
4395 may layer their own process structure atop that provided here.
4396 sol-thread.c does this because of the Solaris two-level thread
4399 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4401 return !procfs_suppress_run
;
4405 * Function: target_stop
4407 * Stop the child process asynchronously, as when the
4408 * gdb user types control-c or presses a "stop" button.
4410 * Works by sending kill(SIGINT) to the child's process group.
4416 extern pid_t inferior_process_group
;
4418 kill (-inferior_process_group
, SIGINT
);
4422 * Function: unconditionally_kill_inferior
4424 * Make it die. Wait for it to die. Clean up after it.
4425 * Note: this should only be applied to the real process,
4426 * not to an LWP, because of the check for parent-process.
4427 * If we need this to work for an LWP, it needs some more logic.
4431 unconditionally_kill_inferior (pi
)
4436 parent_pid
= proc_parent_pid (pi
);
4437 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4438 /* FIXME: use access functions */
4439 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4440 before the PIOCKILL, otherwise it might generate a corrupted core
4441 file for the inferior. */
4442 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4444 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4447 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4448 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4449 to kill the inferior, otherwise it might remain stopped with a
4451 We do not check the result of the PIOCSSIG, the inferior might have
4454 struct siginfo newsiginfo
;
4456 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4457 newsiginfo
.si_signo
= SIGKILL
;
4458 newsiginfo
.si_code
= 0;
4459 newsiginfo
.si_errno
= 0;
4460 newsiginfo
.si_pid
= getpid ();
4461 newsiginfo
.si_uid
= getuid ();
4462 /* FIXME: use proc_set_current_signal */
4463 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4465 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4466 if (!proc_kill (pi
, SIGKILL
))
4467 proc_warn (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4468 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4469 destroy_procinfo (pi
);
4471 /* If pi is GDB's child, wait for it to die. */
4472 if (parent_pid
== getpid ())
4473 /* FIXME: should we use waitpid to make sure we get the right event?
4474 Should we check the returned event? */
4479 ret
= waitpid (pi
->pid
, &status
, 0);
4487 * Function: target_kill_inferior
4489 * We're done debugging it, and we want it to go away.
4490 * Then we want GDB to forget all about it.
4494 procfs_kill_inferior ()
4496 if (inferior_pid
!= 0) /* ? */
4498 /* Find procinfo for main process */
4499 procinfo
*pi
= find_procinfo (PIDGET (inferior_pid
), 0);
4502 unconditionally_kill_inferior (pi
);
4503 target_mourn_inferior ();
4508 * Function: target_mourn_inferior
4510 * Forget we ever debugged this thing!
4514 procfs_mourn_inferior ()
4518 if (inferior_pid
!= 0)
4520 /* Find procinfo for main process */
4521 pi
= find_procinfo (PIDGET (inferior_pid
), 0);
4523 destroy_procinfo (pi
);
4525 unpush_target (&procfs_ops
);
4526 generic_mourn_inferior ();
4530 * Function: init_inferior
4532 * When GDB forks to create a runnable inferior process,
4533 * this function is called on the parent side of the fork.
4534 * It's job is to do whatever is necessary to make the child
4535 * ready to be debugged, and then wait for the child to synchronize.
4539 procfs_init_inferior (pid
)
4546 /* This routine called on the parent side (GDB side)
4547 after GDB forks the inferior. */
4549 push_target (&procfs_ops
);
4551 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4552 perror ("procfs: out of memory in 'init_inferior'");
4554 if (!open_procinfo_files (pi
, FD_CTL
))
4555 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4559 open_procinfo_files // done
4562 procfs_notice_signals
4569 /* If not stopped yet, wait for it to stop. */
4570 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4571 !(proc_wait_for_stop (pi
)))
4572 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4574 /* Save some of the /proc state to be restored if we detach. */
4575 /* FIXME: Why? In case another debugger was debugging it?
4576 We're it's parent, for Ghu's sake! */
4577 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4578 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4579 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4580 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4581 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4582 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4583 if (!proc_get_traced_sysentry (pi
, &pi
->saved_entryset
))
4584 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4585 if (!proc_get_traced_sysexit (pi
, &pi
->saved_exitset
))
4586 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4588 /* Register to trace selected signals in the child. */
4589 prfillset (&signals
);
4590 if (!register_gdb_signals (pi
, &signals
))
4591 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4593 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4594 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4596 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4597 and possibly even turning ON kill-on-last-close at this point. But
4598 I can't make that change without careful testing which I don't have
4599 time to do right now... */
4600 /* Turn on run-on-last-close flag so that the child
4601 will die if GDB goes away for some reason. */
4602 if (!proc_set_run_on_last_close (pi
))
4603 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4605 /* The 'process ID' we return to GDB is composed of
4606 the actual process ID plus the lwp ID. */
4607 inferior_pid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4609 #ifdef START_INFERIOR_TRAPS_EXPECTED
4610 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4612 /* One trap to exec the shell, one to exec the program being debugged. */
4613 startup_inferior (2);
4614 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4618 * Function: set_exec_trap
4620 * When GDB forks to create a new process, this function is called
4621 * on the child side of the fork before GDB exec's the user program.
4622 * Its job is to make the child minimally debuggable, so that the
4623 * parent GDB process can connect to the child and take over.
4624 * This function should do only the minimum to make that possible,
4625 * and to synchronize with the parent process. The parent process
4626 * should take care of the details.
4630 procfs_set_exec_trap ()
4632 /* This routine called on the child side (inferior side)
4633 after GDB forks the inferior. It must use only local variables,
4634 because it may be sharing data space with its parent. */
4639 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4640 perror_with_name ("procfs: create_procinfo failed in child.");
4642 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4644 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4645 gdb_flush (gdb_stderr
);
4646 /* no need to call "dead_procinfo", because we're going to exit. */
4650 #ifdef PRFS_STOPEXEC /* defined on OSF */
4651 /* OSF method for tracing exec syscalls. Quoting:
4652 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4653 exits from exec system calls because of the user level loader. */
4654 /* FIXME: make nice and maybe move into an access function. */
4658 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4660 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4661 gdb_flush (gdb_stderr
);
4664 prfs_flags
|= PRFS_STOPEXEC
;
4666 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4668 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4669 gdb_flush (gdb_stderr
);
4673 #else /* not PRFS_STOPEXEC */
4674 /* Everyone else's (except OSF) method for tracing exec syscalls */
4676 Not all systems with /proc have all the exec* syscalls with the same
4677 names. On the SGI, for example, there is no SYS_exec, but there
4678 *is* a SYS_execv. So, we try to account for that. */
4680 premptyset (&exitset
);
4682 praddset (&exitset
, SYS_exec
);
4685 praddset (&exitset
, SYS_execve
);
4688 praddset (&exitset
, SYS_execv
);
4691 if (!proc_set_traced_sysexit (pi
, &exitset
))
4693 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4694 gdb_flush (gdb_stderr
);
4697 #endif /* PRFS_STOPEXEC */
4699 /* FIXME: should this be done in the parent instead? */
4700 /* Turn off inherit on fork flag so that all grand-children
4701 of gdb start with tracing flags cleared. */
4702 if (!proc_unset_inherit_on_fork (pi
))
4703 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4705 /* Turn off run on last close flag, so that the child process
4706 cannot run away just because we close our handle on it.
4707 We want it to wait for the parent to attach. */
4708 if (!proc_unset_run_on_last_close (pi
))
4709 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4711 /* FIXME: No need to destroy the procinfo --
4712 we have our own address space, and we're about to do an exec! */
4713 /*destroy_procinfo (pi);*/
4717 * Function: create_inferior
4719 * This function is called BEFORE gdb forks the inferior process.
4720 * Its only real responsibility is to set things up for the fork,
4721 * and tell GDB which two functions to call after the fork (one
4722 * for the parent, and one for the child).
4724 * This function does a complicated search for a unix shell program,
4725 * which it then uses to parse arguments and environment variables
4726 * to be sent to the child. I wonder whether this code could not
4727 * be abstracted out and shared with other unix targets such as
4732 procfs_create_inferior (exec_file
, allargs
, env
)
4737 char *shell_file
= getenv ("SHELL");
4739 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4742 /* We will be looking down the PATH to find shell_file. If we
4743 just do this the normal way (via execlp, which operates by
4744 attempting an exec for each element of the PATH until it
4745 finds one which succeeds), then there will be an exec for
4746 each failed attempt, each of which will cause a PR_SYSEXIT
4747 stop, and we won't know how to distinguish the PR_SYSEXIT's
4748 for these failed execs with the ones for successful execs
4749 (whether the exec has succeeded is stored at that time in the
4750 carry bit or some such architecture-specific and
4751 non-ABI-specified place).
4753 So I can't think of anything better than to search the PATH
4754 now. This has several disadvantages: (1) There is a race
4755 condition; if we find a file now and it is deleted before we
4756 exec it, we lose, even if the deletion leaves a valid file
4757 further down in the PATH, (2) there is no way to know exactly
4758 what an executable (in the sense of "capable of being
4759 exec'd") file is. Using access() loses because it may lose
4760 if the caller is the superuser; failing to use it loses if
4761 there are ACLs or some such. */
4765 /* FIXME-maybe: might want "set path" command so user can change what
4766 path is used from within GDB. */
4767 char *path
= getenv ("PATH");
4769 struct stat statbuf
;
4772 path
= "/bin:/usr/bin";
4774 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4775 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4777 p1
= strchr (p
, ':');
4782 strncpy (tryname
, p
, len
);
4783 tryname
[len
] = '\0';
4784 strcat (tryname
, "/");
4785 strcat (tryname
, shell_file
);
4786 if (access (tryname
, X_OK
) < 0)
4788 if (stat (tryname
, &statbuf
) < 0)
4790 if (!S_ISREG (statbuf
.st_mode
))
4791 /* We certainly need to reject directories. I'm not quite
4792 as sure about FIFOs, sockets, etc., but I kind of doubt
4793 that people want to exec() these things. */
4798 /* Not found. This must be an error rather than merely passing
4799 the file to execlp(), because execlp() would try all the
4800 exec()s, causing GDB to get confused. */
4801 error ("procfs:%d -- Can't find shell %s in PATH",
4802 __LINE__
, shell_file
);
4804 shell_file
= tryname
;
4807 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4808 procfs_init_inferior
, NULL
, shell_file
);
4810 /* We are at the first instruction we care about. */
4811 /* Pedal to the metal... */
4813 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
4817 * Function: notice_thread
4819 * Callback for find_new_threads.
4820 * Calls "add_thread".
4824 procfs_notice_thread (pi
, thread
, ptr
)
4829 int gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
4831 if (!in_thread_list (gdb_threadid
))
4832 add_thread (gdb_threadid
);
4838 * Function: target_find_new_threads
4840 * Query all the threads that the target knows about,
4841 * and give them back to GDB to add to its list.
4845 procfs_find_new_threads ()
4849 /* Find procinfo for main process */
4850 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4851 proc_update_threads (pi
);
4852 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
4856 * Function: target_thread_alive
4858 * Return true if the thread is still 'alive'.
4860 * This guy doesn't really seem to be doing his job.
4861 * Got to investigate how to tell when a thread is really gone.
4865 procfs_thread_alive (pid
)
4871 proc
= PIDGET (pid
);
4872 thread
= TIDGET (pid
);
4873 /* If I don't know it, it ain't alive! */
4874 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
4877 /* If I can't get its status, it ain't alive!
4878 What's more, I need to forget about it! */
4879 if (!proc_get_status (pi
))
4881 destroy_procinfo (pi
);
4884 /* I couldn't have got its status if it weren't alive, so it's alive. */
4889 * Function: target_pid_to_str
4891 * Return a string to be used to identify the thread in
4892 * the "info threads" display.
4896 procfs_pid_to_str (pid
)
4899 static char buf
[80];
4903 proc
= PIDGET (pid
);
4904 thread
= TIDGET (pid
);
4905 pi
= find_procinfo (proc
, thread
);
4908 sprintf (buf
, "Process %d", proc
);
4910 sprintf (buf
, "LWP %d", thread
);
4915 * Function: procfs_set_watchpoint
4916 * Insert a watchpoint
4920 procfs_set_watchpoint (pid
, addr
, len
, rwflag
, after
)
4931 pi
= find_procinfo_or_die (pid
== -1 ?
4932 PIDGET (inferior_pid
) : PIDGET (pid
), 0);
4934 /* Translate from GDB's flags to /proc's */
4935 if (len
> 0) /* len == 0 means delete watchpoint */
4937 switch (rwflag
) { /* FIXME: need an enum! */
4938 case hw_write
: /* default watchpoint (write) */
4939 pflags
= WRITE_WATCHFLAG
;
4941 case hw_read
: /* read watchpoint */
4942 pflags
= READ_WATCHFLAG
;
4944 case hw_access
: /* access watchpoint */
4945 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
4947 case hw_execute
: /* execution HW breakpoint */
4948 pflags
= EXEC_WATCHFLAG
;
4950 default: /* Something weird. Return error. */
4953 if (after
) /* Stop after r/w access is completed. */
4954 pflags
|= AFTER_WATCHFLAG
;
4957 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
4959 if (errno
== E2BIG
) /* Typical error for no resources */
4960 return -1; /* fail */
4961 /* GDB may try to remove the same watchpoint twice.
4962 If a remove request returns no match, don't error. */
4963 if (errno
== ESRCH
&& len
== 0)
4964 return 0; /* ignore */
4965 proc_error (pi
, "set_watchpoint", __LINE__
);
4972 * Function: stopped_by_watchpoint
4974 * Returns non-zero if process is stopped on a hardware watchpoint fault,
4975 * else returns zero.
4979 procfs_stopped_by_watchpoint (pid
)
4984 pi
= find_procinfo_or_die (pid
== -1 ?
4985 PIDGET (inferior_pid
) : PIDGET (pid
), 0);
4986 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
4988 if (proc_why (pi
) == PR_FAULTED
)
4991 if (proc_what (pi
) == FLTWATCH
)
4995 if (proc_what (pi
) == FLTKWATCH
)
5003 #ifdef TM_I386SOL2_H
5005 * Function: procfs_find_LDT_entry
5008 * int pid; // The GDB-style pid-plus-LWP.
5011 * pointer to the corresponding LDT entry.
5015 procfs_find_LDT_entry (pid
)
5018 gdb_gregset_t
*gregs
;
5022 /* Find procinfo for the lwp. */
5023 if ((pi
= find_procinfo (PIDGET (pid
), TIDGET (pid
))) == NULL
)
5025 warning ("procfs_find_LDT_entry: could not find procinfi for %d.",
5029 /* get its general registers. */
5030 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
5032 warning ("procfs_find_LDT_entry: could not read gregs for %d.",
5036 /* Now extract the GS register's lower 16 bits. */
5037 key
= (*gregs
)[GS
] & 0xffff;
5039 /* Find the matching entry and return it. */
5040 return proc_get_LDT_entry (pi
, key
);
5042 #endif /* TM_I386SOL2_H */
5047 info_proc_cmd (args
, from_tty
)
5051 struct cleanup
*old_chain
;
5052 procinfo
*process
= NULL
;
5053 procinfo
*thread
= NULL
;
5059 old_chain
= make_cleanup (null_cleanup
, 0);
5062 if ((argv
= buildargv (args
)) == NULL
)
5065 make_cleanup ((make_cleanup_func
) freeargv
, argv
);
5067 while (argv
!= NULL
&& *argv
!= NULL
)
5069 if (isdigit (argv
[0][0]))
5071 pid
= strtoul (argv
[0], &tmp
, 10);
5073 tid
= strtoul (++tmp
, NULL
, 10);
5075 else if (argv
[0][0] == '/')
5077 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5086 pid
= PIDGET (inferior_pid
);
5088 error ("No current process: you must name one.");
5091 /* Have pid, will travel.
5092 First see if it's a process we're already debugging. */
5093 process
= find_procinfo (pid
, 0);
5094 if (process
== NULL
)
5096 /* No. So open a procinfo for it, but
5097 remember to close it again when finished. */
5098 process
= create_procinfo (pid
, 0);
5099 make_cleanup ((make_cleanup_func
) destroy_procinfo
, process
);
5100 if (!open_procinfo_files (process
, FD_CTL
))
5101 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5105 thread
= create_procinfo (pid
, tid
);
5109 printf_filtered ("process %d flags:\n", process
->pid
);
5110 proc_prettyprint_flags (proc_flags (process
), 1);
5111 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5112 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5113 if (proc_get_nthreads (process
) > 1)
5114 printf_filtered ("Process has %d threads.\n",
5115 proc_get_nthreads (process
));
5119 printf_filtered ("thread %d flags:\n", thread
->tid
);
5120 proc_prettyprint_flags (proc_flags (thread
), 1);
5121 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5122 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5125 do_cleanups (old_chain
);
5129 proc_trace_syscalls (args
, from_tty
, entry_or_exit
, mode
)
5139 if (inferior_pid
<= 0)
5140 error ("you must be debugging a process to use this command.");
5142 if (args
== NULL
|| args
[0] == 0)
5143 error_no_arg ("system call to trace");
5145 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
5146 if (isdigit (args
[0]))
5148 syscallnum
= atoi (args
);
5149 if (entry_or_exit
== PR_SYSENTRY
)
5150 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5152 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5155 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5157 if (mode
== FLAG_SET
)
5158 praddset (sysset
, syscallnum
);
5160 prdelset (sysset
, syscallnum
);
5162 if (entry_or_exit
== PR_SYSENTRY
)
5164 if (!proc_set_traced_sysentry (pi
, sysset
))
5165 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5169 if (!proc_set_traced_sysexit (pi
, sysset
))
5170 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5176 proc_trace_sysentry_cmd (args
, from_tty
)
5180 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5184 proc_trace_sysexit_cmd (args
, from_tty
)
5188 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5192 proc_untrace_sysentry_cmd (args
, from_tty
)
5196 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5200 proc_untrace_sysexit_cmd (args
, from_tty
)
5204 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5209 _initialize_procfs ()
5212 add_target (&procfs_ops
);
5213 add_info ("proc", info_proc_cmd
,
5214 "Show /proc process information about any running process.\
5215 Default is the process being debugged.");
5216 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5217 "Give a trace of entries into the syscall.");
5218 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5219 "Give a trace of exits from the syscall.");
5220 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5221 "Cancel a trace of entries into the syscall.");
5222 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5223 "Cancel a trace of exits from the syscall.");
5226 /* =================== END, GDB "MODULE" =================== */
5230 /* miscelaneous stubs: */
5231 /* The following satisfy a few random symbols mostly created by */
5232 /* the solaris threads implementation, which I will chase down */
5236 * Return a pid for which we guarantee
5237 * we will be able to find a 'live' procinfo.
5241 procfs_first_available ()
5244 return procinfo_list
->pid
;