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_execution
= 1;
158 procfs_ops
.to_has_stack
= 1;
159 procfs_ops
.to_has_registers
= 1;
160 procfs_ops
.to_stratum
= process_stratum
;
161 procfs_ops
.to_has_thread_control
= tc_schedlock
;
162 procfs_ops
.to_magic
= OPS_MAGIC
;
165 /* =================== END, TARGET_OPS "MODULE" =================== */
168 * Temporary debugging code:
170 * These macros allow me to trace the system calls that we make
171 * to control the child process. This is quite handy for comparing
172 * with the older version of procfs.
177 extern int write_with_trace
PARAMS ((int, void *, size_t, char *, int));
178 extern off_t lseek_with_trace
PARAMS ((int, off_t
, int, char *, int));
179 #define write(X,Y,Z) write_with_trace (X, Y, Z, __FILE__, __LINE__)
180 #define lseek(X,Y,Z) lseek_with_trace (X, Y, Z, __FILE__, __LINE__)
182 extern int ioctl_with_trace
PARAMS ((int, long, void *, char *, int));
183 #define ioctl(X,Y,Z) ioctl_with_trace (X, Y, Z, __FILE__, __LINE__)
185 #define open(X,Y) open_with_trace (X, Y, __FILE__, __LINE__)
186 #define close(X) close_with_trace (X, __FILE__, __LINE__)
187 #define wait(X) wait_with_trace (X, __FILE__, __LINE__)
188 #define PROCFS_NOTE(X) procfs_note (X, __FILE__, __LINE__)
189 #define PROC_PRETTYFPRINT_STATUS(X,Y,Z,T) \
190 proc_prettyfprint_status (X, Y, Z, T)
192 #define PROCFS_NOTE(X)
193 #define PROC_PRETTYFPRINT_STATUS(X,Y,Z,T)
200 * Put any typedefs, defines etc. here that are required for
201 * the unification of code that handles different versions of /proc.
204 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
206 enum { READ_WATCHFLAG
= WA_READ
,
207 WRITE_WATCHFLAG
= WA_WRITE
,
208 EXEC_WATCHFLAG
= WA_EXEC
,
209 AFTER_WATCHFLAG
= WA_TRAPAFTER
212 #else /* Irix method for watchpoints */
213 enum { READ_WATCHFLAG
= MA_READ
,
214 WRITE_WATCHFLAG
= MA_WRITE
,
215 EXEC_WATCHFLAG
= MA_EXEC
,
216 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
223 /* =================== STRUCT PROCINFO "MODULE" =================== */
225 /* FIXME: this comment will soon be out of date W.R.T. threads. */
227 /* The procinfo struct is a wrapper to hold all the state information
228 concerning a /proc process. There should be exactly one procinfo
229 for each process, and since GDB currently can debug only one
230 process at a time, that means there should be only one procinfo.
231 All of the LWP's of a process can be accessed indirectly thru the
232 single process procinfo.
234 However, against the day when GDB may debug more than one process,
235 this data structure is kept in a list (which for now will hold no
236 more than one member), and many functions will have a pointer to a
237 procinfo as an argument.
239 There will be a separate procinfo structure for use by the (not yet
240 implemented) "info proc" command, so that we can print useful
241 information about any random process without interfering with the
242 inferior's procinfo information. */
245 /* format strings for /proc paths */
246 # ifndef CTL_PROC_NAME_FMT
247 # define MAIN_PROC_NAME_FMT "/proc/%d"
248 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
249 # define AS_PROC_NAME_FMT "/proc/%d/as"
250 # define MAP_PROC_NAME_FMT "/proc/%d/map"
251 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
252 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
254 /* the name of the proc status struct depends on the implementation */
255 typedef pstatus_t gdb_prstatus_t
;
256 typedef lwpstatus_t gdb_lwpstatus_t
;
257 #else /* ! NEW_PROC_API */
258 /* format strings for /proc paths */
259 # ifndef CTL_PROC_NAME_FMT
260 # define MAIN_PROC_NAME_FMT "/proc/%05d"
261 # define CTL_PROC_NAME_FMT "/proc/%05d"
262 # define AS_PROC_NAME_FMT "/proc/%05d"
263 # define MAP_PROC_NAME_FMT "/proc/%05d"
264 # define STATUS_PROC_NAME_FMT "/proc/%05d"
265 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
267 /* the name of the proc status struct depends on the implementation */
268 typedef prstatus_t gdb_prstatus_t
;
269 typedef prstatus_t gdb_lwpstatus_t
;
270 #endif /* NEW_PROC_API */
273 /* These #ifdefs are for sol2.x in particular. sol2.x has
274 both a "gregset_t" and a "prgregset_t", which have
275 similar uses but different layouts. sol2.x gdb tries to
276 use prgregset_t (and prfpregset_t) everywhere. */
278 #ifdef GDB_GREGSET_TYPE
279 typedef GDB_GREGSET_TYPE gdb_gregset_t
;
281 typedef gregset_t gdb_gregset_t
;
284 #ifdef GDB_FPREGSET_TYPE
285 typedef GDB_FPREGSET_TYPE gdb_fpregset_t
;
287 typedef fpregset_t gdb_fpregset_t
;
290 /* Provide default composite pid manipulation macros for systems that
291 don't have threads. */
294 #define PIDGET(PID) (PID)
295 #define TIDGET(PID) (PID)
298 #define MERGEPID(PID, TID) (PID)
301 typedef struct procinfo
{
302 struct procinfo
*next
;
303 int pid
; /* Process ID */
304 int tid
; /* Thread/LWP id */
308 int ignore_next_sigstop
;
310 /* The following four fd fields may be identical, or may contain
311 several different fd's, depending on the version of /proc
312 (old ioctl or new read/write). */
314 int ctl_fd
; /* File descriptor for /proc control file */
316 * The next three file descriptors are actually only needed in the
317 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
318 * However, to avoid a bunch of #ifdefs in the code, we will use
319 * them uniformly by (in the case of the ioctl single-file-descriptor
320 * implementation) filling them with copies of the control fd.
322 int status_fd
; /* File descriptor for /proc status file */
323 int as_fd
; /* File descriptor for /proc as file */
325 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
327 fltset_t saved_fltset
; /* Saved traced hardware fault set */
328 sigset_t saved_sigset
; /* Saved traced signal set */
329 sigset_t saved_sighold
; /* Saved held signal set */
330 sysset_t saved_exitset
; /* Saved traced system call exit set */
331 sysset_t saved_entryset
; /* Saved traced system call entry set */
333 gdb_prstatus_t prstatus
; /* Current process status info */
336 gdb_fpregset_t fpregset
; /* Current floating point registers */
339 struct procinfo
*thread_list
;
341 int status_valid
: 1;
343 int fpregs_valid
: 1;
344 int threads_valid
: 1;
347 static char errmsg
[128]; /* shared error msg buffer */
349 /* Function prototypes for procinfo module: */
351 static procinfo
*find_procinfo_or_die
PARAMS ((int pid
, int tid
));
352 static procinfo
*find_procinfo
PARAMS ((int pid
, int tid
));
353 static procinfo
*create_procinfo
PARAMS ((int pid
, int tid
));
354 static void destroy_procinfo
PARAMS ((procinfo
*p
));
355 static void dead_procinfo
PARAMS ((procinfo
*p
,
356 char *msg
, int killp
));
357 static int open_procinfo_files
PARAMS ((procinfo
*p
, int which
));
358 static void close_procinfo_files
PARAMS ((procinfo
*p
));
360 /* The head of the procinfo list: */
361 static procinfo
* procinfo_list
;
364 * Function: find_procinfo
366 * Search the procinfo list.
368 * Returns: pointer to procinfo, or NULL if not found.
372 find_procinfo (pid
, tid
)
378 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
385 /* Don't check threads_valid. If we're updating the
386 thread_list, we want to find whatever threads are already
387 here. This means that in general it is the caller's
388 responsibility to check threads_valid and update before
389 calling find_procinfo, if the caller wants to find a new
392 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
401 * Function: find_procinfo_or_die
403 * Calls find_procinfo, but errors on failure.
407 find_procinfo_or_die (pid
, tid
)
411 procinfo
*pi
= find_procinfo (pid
, tid
);
416 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
419 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
425 * Function: open_procinfo_files
427 * Open the file descriptor for the process or LWP.
428 * ifdef NEW_PROC_API, we only open the control file descriptor;
429 * the others are opened lazily as needed.
430 * else (if not NEW_PROC_API), there is only one real
431 * file descriptor, but we keep multiple copies of it so that
432 * the code that uses them does not have to be #ifdef'd.
434 * Return: file descriptor, or zero for failure.
437 enum { FD_CTL
, FD_STATUS
, FD_AS
};
440 open_procinfo_files (pi
, which
)
445 char tmp
[MAX_PROC_NAME_SIZE
];
450 * This function is getting ALMOST long enough to break up into several.
451 * Here is some rationale:
453 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
454 * There are several file descriptors that may need to be open
455 * for any given process or LWP. The ones we're intereted in are:
456 * - control (ctl) write-only change the state
457 * - status (status) read-only query the state
458 * - address space (as) read/write access memory
459 * - map (map) read-only virtual addr map
460 * Most of these are opened lazily as they are needed.
461 * The pathnames for the 'files' for an LWP look slightly
462 * different from those of a first-class process:
463 * Pathnames for a process (<proc-id>):
464 * /proc/<proc-id>/ctl
465 * /proc/<proc-id>/status
467 * /proc/<proc-id>/map
468 * Pathnames for an LWP (lwp-id):
469 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
470 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
471 * An LWP has no map or address space file descriptor, since
472 * the memory map and address space are shared by all LWPs.
474 * Everyone else (Solaris 2.5, Irix, OSF)
475 * There is only one file descriptor for each process or LWP.
476 * For convenience, we copy the same file descriptor into all
477 * three fields of the procinfo struct (ctl_fd, status_fd, and
478 * as_fd, see NEW_PROC_API above) so that code that uses them
479 * doesn't need any #ifdef's.
484 * Each LWP has an independent file descriptor, but these
485 * are not obtained via the 'open' system call like the rest:
486 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
487 * to the file descriptor of the parent process.
490 * These do not even have their own independent file descriptor.
491 * All operations are carried out on the file descriptor of the
492 * parent process. Therefore we just call open again for each
493 * thread, getting a new handle for the same 'file'.
498 * In this case, there are several different file descriptors that
499 * we might be asked to open. The control file descriptor will be
500 * opened early, but the others will be opened lazily as they are
504 strcpy (tmp
, pi
->pathname
);
505 switch (which
) { /* which file descriptor to open? */
508 strcat (tmp
, "/lwpctl");
510 strcat (tmp
, "/ctl");
511 fd
= open (tmp
, O_WRONLY
);
518 return 0; /* there is no 'as' file descriptor for an lwp */
520 fd
= open (tmp
, O_RDWR
);
527 strcat (tmp
, "/lwpstatus");
529 strcat (tmp
, "/status");
530 fd
= open (tmp
, O_RDONLY
);
536 return 0; /* unknown file descriptor */
538 #else /* not NEW_PROC_API */
540 * In this case, there is only one file descriptor for each procinfo
541 * (ie. each process or LWP). In fact, only the file descriptor for
542 * the process can actually be opened by an 'open' system call.
543 * The ones for the LWPs have to be obtained thru an IOCTL call
544 * on the process's file descriptor.
546 * For convenience, we copy each procinfo's single file descriptor
547 * into all of the fields occupied by the several file descriptors
548 * of the NEW_PROC_API implementation. That way, the code that uses
549 * them can be written without ifdefs.
553 #ifdef PIOCTSTATUS /* OSF */
554 if ((fd
= open (pi
->pathname
, O_RDWR
)) == 0) /* Only one FD; just open it. */
556 #else /* Sol 2.5, Irix, other? */
557 if (pi
->tid
== 0) /* Master procinfo for the process */
559 fd
= open (pi
->pathname
, O_RDWR
);
563 else /* LWP thread procinfo */
565 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
569 /* Find the procinfo for the entire process. */
570 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
573 /* Now obtain the file descriptor for the LWP. */
574 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
576 #else /* Irix, other? */
577 return 0; /* Don't know how to open threads */
578 #endif /* Sol 2.5 PIOCOPENLWP */
580 #endif /* OSF PIOCTSTATUS */
581 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
582 #endif /* NEW_PROC_API */
584 return 1; /* success */
588 * Function: create_procinfo
590 * Allocate a data structure and link it into the procinfo list.
591 * (First tries to find a pre-existing one (FIXME: why???)
593 * Return: pointer to new procinfo struct.
597 create_procinfo (pid
, tid
)
601 procinfo
*pi
, *parent
;
603 if ((pi
= find_procinfo (pid
, tid
)))
604 return pi
; /* Already exists, nothing to do. */
606 /* find parent before doing malloc, to save having to cleanup */
608 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
610 doesn't exist yet? */
612 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
613 memset (pi
, 0, sizeof (procinfo
));
617 /* Chain into list. */
620 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
621 pi
->next
= procinfo_list
;
627 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
629 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
631 pi
->next
= parent
->thread_list
;
632 parent
->thread_list
= pi
;
638 * Function: close_procinfo_files
640 * Close all file descriptors associated with the procinfo
644 close_procinfo_files (pi
)
652 if (pi
->status_fd
> 0)
653 close (pi
->status_fd
);
655 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
659 * Function: destroy_procinfo
661 * Destructor function. Close, unlink and deallocate the object.
665 destroy_one_procinfo (list
, pi
)
671 /* Step one: unlink the procinfo from its list */
675 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
678 ptr
->next
= pi
->next
;
682 /* Step two: close any open file descriptors */
683 close_procinfo_files (pi
);
685 /* Step three: free the memory. */
690 destroy_procinfo (pi
)
695 if (pi
->tid
!= 0) /* destroy a thread procinfo */
697 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
698 destroy_one_procinfo (&tmp
->thread_list
, pi
);
700 else /* destroy a process procinfo and all its threads */
702 /* First destroy the children, if any; */
703 while (pi
->thread_list
!= NULL
)
704 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
705 /* Then destroy the parent. Genocide!!! */
706 destroy_one_procinfo (&procinfo_list
, pi
);
710 enum { NOKILL
, KILL
};
713 * Function: dead_procinfo
715 * To be called on a non_recoverable error for a procinfo.
716 * Prints error messages, optionally sends a SIGKILL to the process,
717 * then destroys the data structure.
721 dead_procinfo (pi
, msg
, kill_p
)
730 print_sys_errmsg (pi
->pathname
, errno
);
734 sprintf (procfile
, "process %d", pi
->pid
);
735 print_sys_errmsg (procfile
, errno
);
738 kill (pi
->pid
, SIGKILL
);
740 destroy_procinfo (pi
);
744 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
746 /* =================== /proc "MODULE" =================== */
749 * This "module" is the interface layer between the /proc system API
750 * and the gdb target vector functions. This layer consists of
751 * access functions that encapsulate each of the basic operations
752 * that we need to use from the /proc API.
754 * The main motivation for this layer is to hide the fact that
755 * there are two very different implementations of the /proc API.
756 * Rather than have a bunch of #ifdefs all thru the gdb target vector
757 * functions, we do our best to hide them all in here.
760 int proc_get_status
PARAMS ((procinfo
*pi
));
761 long proc_flags
PARAMS ((procinfo
*pi
));
762 int proc_why
PARAMS ((procinfo
*pi
));
763 int proc_what
PARAMS ((procinfo
*pi
));
764 int proc_set_run_on_last_close
PARAMS ((procinfo
*pi
));
765 int proc_unset_run_on_last_close
PARAMS ((procinfo
*pi
));
766 int proc_set_inherit_on_fork
PARAMS ((procinfo
*pi
));
767 int proc_unset_inherit_on_fork
PARAMS ((procinfo
*pi
));
768 int proc_set_async
PARAMS ((procinfo
*pi
));
769 int proc_unset_async
PARAMS ((procinfo
*pi
));
770 int proc_stop_process
PARAMS ((procinfo
*pi
));
771 int proc_trace_signal
PARAMS ((procinfo
*pi
, int signo
));
772 int proc_ignore_signal
PARAMS ((procinfo
*pi
, int signo
));
773 int proc_clear_current_fault
PARAMS ((procinfo
*pi
));
774 int proc_set_current_signal
PARAMS ((procinfo
*pi
, int signo
));
775 int proc_clear_current_signal
PARAMS ((procinfo
*pi
));
776 int proc_set_gregs
PARAMS ((procinfo
*pi
));
777 int proc_set_fpregs
PARAMS ((procinfo
*pi
));
778 int proc_wait_for_stop
PARAMS ((procinfo
*pi
));
779 int proc_run_process
PARAMS ((procinfo
*pi
, int step
, int signo
));
780 int proc_kill
PARAMS ((procinfo
*pi
, int signo
));
781 int proc_parent_pid
PARAMS ((procinfo
*pi
));
782 int proc_get_nthreads
PARAMS ((procinfo
*pi
));
783 int proc_get_current_thread
PARAMS ((procinfo
*pi
));
784 int proc_set_held_signals
PARAMS ((procinfo
*pi
, sigset_t
*sighold
));
785 int proc_set_traced_sysexit
PARAMS ((procinfo
*pi
, sysset_t
*sysset
));
786 int proc_set_traced_sysentry
PARAMS ((procinfo
*pi
, sysset_t
*sysset
));
787 int proc_set_traced_faults
PARAMS ((procinfo
*pi
, fltset_t
*fltset
));
788 int proc_set_traced_signals
PARAMS ((procinfo
*pi
, sigset_t
*sigset
));
790 int proc_update_threads
PARAMS ((procinfo
*pi
));
791 int proc_iterate_over_threads
PARAMS ((procinfo
*pi
,
792 int (*func
) PARAMS ((procinfo
*,
797 gdb_gregset_t
*proc_get_gregs
PARAMS ((procinfo
*pi
));
798 gdb_fpregset_t
*proc_get_fpregs
PARAMS ((procinfo
*pi
));
799 sysset_t
*proc_get_traced_sysexit
PARAMS ((procinfo
*pi
, sysset_t
*save
));
800 sysset_t
*proc_get_traced_sysentry
PARAMS ((procinfo
*pi
, sysset_t
*save
));
801 fltset_t
*proc_get_traced_faults
PARAMS ((procinfo
*pi
, fltset_t
*save
));
802 sigset_t
*proc_get_traced_signals
PARAMS ((procinfo
*pi
, sigset_t
*save
));
803 sigset_t
*proc_get_held_signals
PARAMS ((procinfo
*pi
, sigset_t
*save
));
804 sigset_t
*proc_get_pending_signals
PARAMS ((procinfo
*pi
, sigset_t
*save
));
805 struct sigaction
*proc_get_signal_actions
PARAMS ((procinfo
*pi
,
806 struct sigaction
*save
));
808 void proc_warn
PARAMS ((procinfo
*pi
, char *func
, int line
));
809 void proc_error
PARAMS ((procinfo
*pi
, char *func
, int line
));
812 proc_warn (pi
, func
, line
)
817 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
818 print_sys_errmsg (errmsg
, errno
);
822 proc_error (pi
, func
, line
)
827 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
828 perror_with_name (errmsg
);
832 * Function: proc_get_status
834 * Updates the status struct in the procinfo.
835 * There is a 'valid' flag, to let other functions know when
836 * this function needs to be called (so the status is only
837 * read when it is needed). The status file descriptor is
838 * also only opened when it is needed.
840 * Return: non-zero for success, zero for failure.
847 /* Status file descriptor is opened "lazily" */
848 if (pi
->status_fd
== 0 &&
849 open_procinfo_files (pi
, FD_STATUS
) == 0)
851 pi
->status_valid
= 0;
856 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
857 pi
->status_valid
= 0; /* fail */
860 /* Sigh... I have to read a different data structure,
861 depending on whether this is a main process or an LWP. */
863 pi
->status_valid
= (read (pi
->status_fd
,
864 (char *) &pi
->prstatus
.pr_lwp
,
865 sizeof (lwpstatus_t
))
866 == sizeof (lwpstatus_t
));
869 pi
->status_valid
= (read (pi
->status_fd
,
870 (char *) &pi
->prstatus
,
871 sizeof (gdb_prstatus_t
))
872 == sizeof (gdb_prstatus_t
));
873 #if 0 /*def UNIXWARE*/
874 if (pi
->status_valid
&&
875 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
876 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
877 /* Unixware peculiarity -- read the damn thing again! */
878 pi
->status_valid
= (read (pi
->status_fd
,
879 (char *) &pi
->prstatus
,
880 sizeof (gdb_prstatus_t
))
881 == sizeof (gdb_prstatus_t
));
882 #endif /* UNIXWARE */
885 #else /* ioctl method */
886 #ifdef PIOCTSTATUS /* osf */
887 if (pi
->tid
== 0) /* main process */
889 /* Just read the danged status. Now isn't that simple? */
891 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
898 tid_t pr_error_thread
;
899 struct prstatus status
;
902 thread_status
.pr_count
= 1;
903 thread_status
.status
.pr_tid
= pi
->tid
;
904 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
907 memcpy (&pi
->prstatus
, &thread_status
.status
,
908 sizeof (pi
->prstatus
));
909 pi
->status_valid
= 1;
913 /* Just read the danged status. Now isn't that simple? */
914 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
918 if (pi
->status_valid
)
920 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
923 proc_get_current_thread (pi
));
926 /* The status struct includes general regs, so mark them valid too */
927 pi
->gregs_valid
= pi
->status_valid
;
929 /* In the read/write multiple-fd model,
930 the status struct includes the fp regs too, so mark them valid too */
931 pi
->fpregs_valid
= pi
->status_valid
;
933 return pi
->status_valid
; /* True if success, false if failure. */
937 * Function: proc_flags
939 * returns the process flags (pr_flags field).
946 if (!pi
->status_valid
)
947 if (!proc_get_status (pi
))
948 return 0; /* FIXME: not a good failure value (but what is?) */
952 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
953 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
954 The two sets of flags don't overlap. */
955 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
957 return pi
->prstatus
.pr_lwp
.pr_flags
;
960 return pi
->prstatus
.pr_flags
;
967 * returns the pr_why field (why the process stopped).
974 if (!pi
->status_valid
)
975 if (!proc_get_status (pi
))
976 return 0; /* FIXME: not a good failure value (but what is?) */
979 return pi
->prstatus
.pr_lwp
.pr_why
;
981 return pi
->prstatus
.pr_why
;
986 * Function: proc_what
988 * returns the pr_what field (details of why the process stopped).
995 if (!pi
->status_valid
)
996 if (!proc_get_status (pi
))
997 return 0; /* FIXME: not a good failure value (but what is?) */
1000 return pi
->prstatus
.pr_lwp
.pr_what
;
1002 return pi
->prstatus
.pr_what
;
1006 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1008 * Function: proc_nsysarg
1010 * returns the pr_nsysarg field (number of args to the current syscall).
1017 if (!pi
->status_valid
)
1018 if (!proc_get_status (pi
))
1022 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1024 return pi
->prstatus
.pr_nsysarg
;
1029 * Function: proc_sysargs
1031 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1038 if (!pi
->status_valid
)
1039 if (!proc_get_status (pi
))
1043 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1045 return (long *) &pi
->prstatus
.pr_sysarg
;
1050 * Function: proc_syscall
1052 * returns the pr_syscall field (id of current syscall if we are in one).
1059 if (!pi
->status_valid
)
1060 if (!proc_get_status (pi
))
1064 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1066 return pi
->prstatus
.pr_syscall
;
1069 #endif /* PIOCSSPCACT */
1072 * Function: proc_cursig:
1074 * returns the pr_cursig field (current signal).
1078 proc_cursig (struct procinfo
*pi
)
1080 if (!pi
->status_valid
)
1081 if (!proc_get_status (pi
))
1082 return 0; /* FIXME: not a good failure value (but what is?) */
1085 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1087 return pi
->prstatus
.pr_cursig
;
1092 * Function: proc_modify_flag
1094 * === I appologize for the messiness of this function.
1095 * === This is an area where the different versions of
1096 * === /proc are more inconsistent than usual. MVS
1098 * Set or reset any of the following process flags:
1099 * PR_FORK -- forked child will inherit trace flags
1100 * PR_RLC -- traced process runs when last /proc file closed.
1101 * PR_KLC -- traced process is killed when last /proc file closed.
1102 * PR_ASYNC -- LWP's get to run/stop independently.
1104 * There are three methods for doing this function:
1105 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1107 * 2) Middle: PIOCSET/PIOCRESET
1109 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1112 * Note: Irix does not define PR_ASYNC.
1113 * Note: OSF does not define PR_KLC.
1114 * Note: OSF is the only one that can ONLY use the oldest method.
1117 * pi -- the procinfo
1118 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1119 * mode -- 1 for set, 0 for reset.
1121 * Returns non-zero for success, zero for failure.
1124 enum { FLAG_RESET
, FLAG_SET
};
1127 proc_modify_flag (pi
, flag
, mode
)
1132 long win
= 0; /* default to fail */
1135 * These operations affect the process as a whole, and applying
1136 * them to an individual LWP has the same meaning as applying them
1137 * to the main process. Therefore, if we're ever called with a
1138 * pointer to an LWP's procinfo, let's substitute the process's
1139 * procinfo and avoid opening the LWP's file descriptor
1144 pi
= find_procinfo_or_die (pi
->pid
, 0);
1146 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1147 /* First normalize the PCUNSET/PCRESET command opcode
1148 (which for no obvious reason has a different definition
1149 from one operating system to the next...) */
1151 #define GDBRESET PCUNSET
1154 #define GDBRESET PCRESET
1159 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1161 else /* Reset the flag */
1165 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1168 #ifdef PIOCSET /* Irix/Sol5 method */
1169 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1171 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1173 else /* Reset the flag */
1175 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1179 #ifdef PIOCSRLC /* Oldest method: OSF */
1182 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1184 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1186 else /* Clear run-on-last-close */
1188 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1192 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1194 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1196 else /* Clear inherit-on-fork */
1198 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1202 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1209 /* The above operation renders the procinfo's cached pstatus obsolete. */
1210 pi
->status_valid
= 0;
1213 warning ("procfs: modify_flag failed to turn %s %s",
1214 flag
== PR_FORK
? "PR_FORK" :
1215 flag
== PR_RLC
? "PR_RLC" :
1217 flag
== PR_ASYNC
? "PR_ASYNC" :
1220 flag
== PR_KLC
? "PR_KLC" :
1223 mode
== FLAG_RESET
? "off" : "on");
1229 * Function: proc_set_run_on_last_close
1231 * Set the run_on_last_close flag.
1232 * Process with all threads will become runnable
1233 * when debugger closes all /proc fds.
1235 * Returns non-zero for success, zero for failure.
1239 proc_set_run_on_last_close (pi
)
1242 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1246 * Function: proc_unset_run_on_last_close
1248 * Reset the run_on_last_close flag.
1249 * Process will NOT become runnable
1250 * when debugger closes its file handles.
1252 * Returns non-zero for success, zero for failure.
1256 proc_unset_run_on_last_close (pi
)
1259 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1264 * Function: proc_set_kill_on_last_close
1266 * Set the kill_on_last_close flag.
1267 * Process with all threads will be killed when debugger
1268 * closes all /proc fds (or debugger exits or dies).
1270 * Returns non-zero for success, zero for failure.
1274 proc_set_kill_on_last_close (pi
)
1277 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1281 * Function: proc_unset_kill_on_last_close
1283 * Reset the kill_on_last_close flag.
1284 * Process will NOT be killed when debugger
1285 * closes its file handles (or exits or dies).
1287 * Returns non-zero for success, zero for failure.
1291 proc_unset_kill_on_last_close (pi
)
1294 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1299 * Function: proc_set_inherit_on_fork
1301 * Set inherit_on_fork flag.
1302 * If the process forks a child while we are registered for events
1303 * in the parent, then we will also recieve events from the child.
1305 * Returns non-zero for success, zero for failure.
1309 proc_set_inherit_on_fork (pi
)
1312 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1316 * Function: proc_unset_inherit_on_fork
1318 * Reset inherit_on_fork flag.
1319 * If the process forks a child while we are registered for events
1320 * in the parent, then we will NOT recieve events from the child.
1322 * Returns non-zero for success, zero for failure.
1326 proc_unset_inherit_on_fork (pi
)
1329 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1334 * Function: proc_set_async
1336 * Set PR_ASYNC flag.
1337 * If one LWP stops because of a debug event (signal etc.),
1338 * the remaining LWPs will continue to run.
1340 * Returns non-zero for success, zero for failure.
1347 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1351 * Function: proc_unset_async
1353 * Reset PR_ASYNC flag.
1354 * If one LWP stops because of a debug event (signal etc.),
1355 * then all other LWPs will stop as well.
1357 * Returns non-zero for success, zero for failure.
1361 proc_unset_async (pi
)
1364 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1366 #endif /* PR_ASYNC */
1369 * Function: proc_stop_process
1371 * Request the process/LWP to stop. Does not wait.
1372 * Returns non-zero for success, zero for failure.
1376 proc_stop_process (pi
)
1382 * We might conceivably apply this operation to an LWP, and
1383 * the LWP's ctl file descriptor might not be open.
1386 if (pi
->ctl_fd
== 0 &&
1387 open_procinfo_files (pi
, FD_CTL
) == 0)
1393 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1394 #else /* ioctl method */
1395 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1396 /* Note: the call also reads the prstatus. */
1399 pi
->status_valid
= 1;
1400 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1403 proc_get_current_thread (pi
));
1412 * Function: proc_wait_for_stop
1414 * Wait for the process or LWP to stop (block until it does).
1415 * Returns non-zero for success, zero for failure.
1419 proc_wait_for_stop (pi
)
1425 * We should never have to apply this operation to any procinfo
1426 * except the one for the main process. If that ever changes
1427 * for any reason, then take out the following clause and
1428 * replace it with one that makes sure the ctl_fd is open.
1432 pi
= find_procinfo_or_die (pi
->pid
, 0);
1437 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1438 /* We been runnin' and we stopped -- need to update status. */
1439 pi
->status_valid
= 0;
1441 #else /* ioctl method */
1442 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1443 /* Above call also refreshes the prstatus. */
1446 pi
->status_valid
= 1;
1447 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1450 proc_get_current_thread (pi
));
1458 * Function: proc_run_process
1460 * Make the process or LWP runnable.
1461 * Options (not all are implemented):
1463 * - clear current fault
1464 * - clear current signal
1465 * - abort the current system call
1466 * - stop as soon as finished with system call
1467 * - (ioctl): set traced signal set
1468 * - (ioctl): set held signal set
1469 * - (ioctl): set traced fault set
1470 * - (ioctl): set start pc (vaddr)
1471 * Always clear the current fault.
1472 * Clear the current signal if 'signo' is zero.
1475 * pi the process or LWP to operate on.
1476 * step if true, set the process or LWP to trap after one instr.
1477 * signo if zero, clear the current signal if any.
1478 * if non-zero, set the current signal to this one.
1480 * Returns non-zero for success, zero for failure.
1484 proc_run_process (pi
, step
, signo
)
1493 * We will probably have to apply this operation to individual threads,
1494 * so make sure the control file descriptor is open.
1497 if (pi
->ctl_fd
== 0 &&
1498 open_procinfo_files (pi
, FD_CTL
) == 0)
1503 runflags
= PRCFAULT
; /* always clear current fault */
1508 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1509 proc_set_current_signal (pi
, signo
);
1517 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1519 #else /* ioctl method */
1523 memset (&prrun
, 0, sizeof (prrun
));
1524 prrun
.pr_flags
= runflags
;
1525 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1533 * Function: proc_set_traced_signals
1535 * Register to trace signals in the process or LWP.
1536 * Returns non-zero for success, zero for failure.
1540 proc_set_traced_signals (pi
, sigset
)
1547 * We should never have to apply this operation to any procinfo
1548 * except the one for the main process. If that ever changes
1549 * for any reason, then take out the following clause and
1550 * replace it with one that makes sure the ctl_fd is open.
1554 pi
= find_procinfo_or_die (pi
->pid
, 0);
1560 /* Use char array to avoid alignment issues. */
1561 char sigset
[sizeof (sigset_t
)];
1565 memcpy (&arg
.sigset
, sigset
, sizeof (sigset_t
));
1567 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1569 #else /* ioctl method */
1570 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1572 /* The above operation renders the procinfo's cached pstatus obsolete. */
1573 pi
->status_valid
= 0;
1576 warning ("procfs: set_traced_signals failed");
1581 * Function: proc_set_traced_faults
1583 * Register to trace hardware faults in the process or LWP.
1584 * Returns non-zero for success, zero for failure.
1588 proc_set_traced_faults (pi
, fltset
)
1595 * We should never have to apply this operation to any procinfo
1596 * except the one for the main process. If that ever changes
1597 * for any reason, then take out the following clause and
1598 * replace it with one that makes sure the ctl_fd is open.
1602 pi
= find_procinfo_or_die (pi
->pid
, 0);
1608 /* Use char array to avoid alignment issues. */
1609 char fltset
[sizeof (fltset_t
)];
1613 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1615 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1617 #else /* ioctl method */
1618 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1620 /* The above operation renders the procinfo's cached pstatus obsolete. */
1621 pi
->status_valid
= 0;
1627 * Function: proc_set_traced_sysentry
1629 * Register to trace entry to system calls in the process or LWP.
1630 * Returns non-zero for success, zero for failure.
1634 proc_set_traced_sysentry (pi
, sysset
)
1641 * We should never have to apply this operation to any procinfo
1642 * except the one for the main process. If that ever changes
1643 * for any reason, then take out the following clause and
1644 * replace it with one that makes sure the ctl_fd is open.
1648 pi
= find_procinfo_or_die (pi
->pid
, 0);
1654 /* Use char array to avoid alignment issues. */
1655 char sysset
[sizeof (sysset_t
)];
1659 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1661 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1663 #else /* ioctl method */
1664 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1666 /* The above operation renders the procinfo's cached pstatus obsolete. */
1667 pi
->status_valid
= 0;
1673 * Function: proc_set_traced_sysexit
1675 * Register to trace exit from system calls in the process or LWP.
1676 * Returns non-zero for success, zero for failure.
1680 proc_set_traced_sysexit (pi
, sysset
)
1687 * We should never have to apply this operation to any procinfo
1688 * except the one for the main process. If that ever changes
1689 * for any reason, then take out the following clause and
1690 * replace it with one that makes sure the ctl_fd is open.
1694 pi
= find_procinfo_or_die (pi
->pid
, 0);
1700 /* Use char array to avoid alignment issues. */
1701 char sysset
[sizeof (sysset_t
)];
1705 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1707 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1709 #else /* ioctl method */
1710 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1712 /* The above operation renders the procinfo's cached pstatus obsolete. */
1713 pi
->status_valid
= 0;
1719 * Function: proc_set_held_signals
1721 * Specify the set of blocked / held signals in the process or LWP.
1722 * Returns non-zero for success, zero for failure.
1726 proc_set_held_signals (pi
, sighold
)
1733 * We should never have to apply this operation to any procinfo
1734 * except the one for the main process. If that ever changes
1735 * for any reason, then take out the following clause and
1736 * replace it with one that makes sure the ctl_fd is open.
1740 pi
= find_procinfo_or_die (pi
->pid
, 0);
1746 /* Use char array to avoid alignment issues. */
1747 char hold
[sizeof (sigset_t
)];
1751 memcpy (&arg
.hold
, sighold
, sizeof (sigset_t
));
1752 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1755 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1757 /* The above operation renders the procinfo's cached pstatus obsolete. */
1758 pi
->status_valid
= 0;
1764 * Function: proc_get_pending_signals
1766 * returns the set of signals that are pending in the process or LWP.
1767 * Will also copy the sigset if 'save' is non-zero.
1771 proc_get_pending_signals (pi
, save
)
1775 sigset_t
*ret
= NULL
;
1778 * We should never have to apply this operation to any procinfo
1779 * except the one for the main process. If that ever changes
1780 * for any reason, then take out the following clause and
1781 * replace it with one that makes sure the ctl_fd is open.
1785 pi
= find_procinfo_or_die (pi
->pid
, 0);
1787 if (!pi
->status_valid
)
1788 if (!proc_get_status (pi
))
1792 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
1794 ret
= &pi
->prstatus
.pr_sigpend
;
1797 memcpy (save
, ret
, sizeof (sigset_t
));
1803 * Function: proc_get_signal_actions
1805 * returns the set of signal actions.
1806 * Will also copy the sigactionset if 'save' is non-zero.
1810 proc_get_signal_actions (pi
, save
)
1812 struct sigaction
*save
;
1814 struct sigaction
*ret
= NULL
;
1817 * We should never have to apply this operation to any procinfo
1818 * except the one for the main process. If that ever changes
1819 * for any reason, then take out the following clause and
1820 * replace it with one that makes sure the ctl_fd is open.
1824 pi
= find_procinfo_or_die (pi
->pid
, 0);
1826 if (!pi
->status_valid
)
1827 if (!proc_get_status (pi
))
1831 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
1833 ret
= &pi
->prstatus
.pr_action
;
1836 memcpy (save
, ret
, sizeof (struct sigaction
));
1842 * Function: proc_get_held_signals
1844 * returns the set of signals that are held / blocked.
1845 * Will also copy the sigset if 'save' is non-zero.
1849 proc_get_held_signals (pi
, save
)
1853 sigset_t
*ret
= NULL
;
1856 * We should never have to apply this operation to any procinfo
1857 * except the one for the main process. If that ever changes
1858 * for any reason, then take out the following clause and
1859 * replace it with one that makes sure the ctl_fd is open.
1863 pi
= find_procinfo_or_die (pi
->pid
, 0);
1866 if (!pi
->status_valid
)
1867 if (!proc_get_status (pi
))
1871 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
1873 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1874 #endif /* UNIXWARE */
1875 #else /* not NEW_PROC_API */
1877 static sigset_t sigheld
;
1879 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
1882 #endif /* NEW_PROC_API */
1884 memcpy (save
, ret
, sizeof (sigset_t
));
1890 * Function: proc_get_traced_signals
1892 * returns the set of signals that are traced / debugged.
1893 * Will also copy the sigset if 'save' is non-zero.
1897 proc_get_traced_signals (pi
, save
)
1901 sigset_t
*ret
= NULL
;
1904 * We should never have to apply this operation to any procinfo
1905 * except the one for the main process. If that ever changes
1906 * for any reason, then take out the following clause and
1907 * replace it with one that makes sure the ctl_fd is open.
1911 pi
= find_procinfo_or_die (pi
->pid
, 0);
1914 if (!pi
->status_valid
)
1915 if (!proc_get_status (pi
))
1918 ret
= &pi
->prstatus
.pr_sigtrace
;
1921 static sigset_t sigtrace
;
1923 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
1928 memcpy (save
, ret
, sizeof (sigset_t
));
1934 * Function: proc_trace_signal
1936 * Add 'signo' to the set of signals that are traced.
1937 * Returns non-zero for success, zero for failure.
1941 proc_trace_signal (pi
, signo
)
1948 * We should never have to apply this operation to any procinfo
1949 * except the one for the main process. If that ever changes
1950 * for any reason, then take out the following clause and
1951 * replace it with one that makes sure the ctl_fd is open.
1955 pi
= find_procinfo_or_die (pi
->pid
, 0);
1959 if (proc_get_traced_signals (pi
, &temp
))
1961 praddset (&temp
, signo
);
1962 return proc_set_traced_signals (pi
, &temp
);
1966 return 0; /* failure */
1970 * Function: proc_ignore_signal
1972 * Remove 'signo' from the set of signals that are traced.
1973 * Returns non-zero for success, zero for failure.
1977 proc_ignore_signal (pi
, signo
)
1984 * We should never have to apply this operation to any procinfo
1985 * except the one for the main process. If that ever changes
1986 * for any reason, then take out the following clause and
1987 * replace it with one that makes sure the ctl_fd is open.
1991 pi
= find_procinfo_or_die (pi
->pid
, 0);
1995 if (proc_get_traced_signals (pi
, &temp
))
1997 prdelset (&temp
, signo
);
1998 return proc_set_traced_signals (pi
, &temp
);
2002 return 0; /* failure */
2006 * Function: proc_get_traced_faults
2008 * returns the set of hardware faults that are traced /debugged.
2009 * Will also copy the faultset if 'save' is non-zero.
2013 proc_get_traced_faults (pi
, save
)
2017 fltset_t
*ret
= NULL
;
2020 * We should never have to apply this operation to any procinfo
2021 * except the one for the main process. If that ever changes
2022 * for any reason, then take out the following clause and
2023 * replace it with one that makes sure the ctl_fd is open.
2027 pi
= find_procinfo_or_die (pi
->pid
, 0);
2030 if (!pi
->status_valid
)
2031 if (!proc_get_status (pi
))
2034 ret
= &pi
->prstatus
.pr_flttrace
;
2037 static fltset_t flttrace
;
2039 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2044 memcpy (save
, ret
, sizeof (fltset_t
));
2050 * Function: proc_get_traced_sysentry
2052 * returns the set of syscalls that are traced /debugged on entry.
2053 * Will also copy the syscall set if 'save' is non-zero.
2057 proc_get_traced_sysentry (pi
, save
)
2061 sysset_t
*ret
= NULL
;
2064 * We should never have to apply this operation to any procinfo
2065 * except the one for the main process. If that ever changes
2066 * for any reason, then take out the following clause and
2067 * replace it with one that makes sure the ctl_fd is open.
2071 pi
= find_procinfo_or_die (pi
->pid
, 0);
2074 if (!pi
->status_valid
)
2075 if (!proc_get_status (pi
))
2078 ret
= &pi
->prstatus
.pr_sysentry
;
2081 static sysset_t sysentry
;
2083 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2088 memcpy (save
, ret
, sizeof (sysset_t
));
2094 * Function: proc_get_traced_sysexit
2096 * returns the set of syscalls that are traced /debugged on exit.
2097 * Will also copy the syscall set if 'save' is non-zero.
2101 proc_get_traced_sysexit (pi
, save
)
2105 sysset_t
* ret
= NULL
;
2108 * We should never have to apply this operation to any procinfo
2109 * except the one for the main process. If that ever changes
2110 * for any reason, then take out the following clause and
2111 * replace it with one that makes sure the ctl_fd is open.
2115 pi
= find_procinfo_or_die (pi
->pid
, 0);
2118 if (!pi
->status_valid
)
2119 if (!proc_get_status (pi
))
2122 ret
= &pi
->prstatus
.pr_sysexit
;
2125 static sysset_t sysexit
;
2127 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2132 memcpy (save
, ret
, sizeof (sysset_t
));
2138 * Function: proc_clear_current_fault
2140 * The current fault (if any) is cleared; the associated signal
2141 * will not be sent to the process or LWP when it resumes.
2142 * Returns non-zero for success, zero for failure.
2146 proc_clear_current_fault (pi
)
2152 * We should never have to apply this operation to any procinfo
2153 * except the one for the main process. If that ever changes
2154 * for any reason, then take out the following clause and
2155 * replace it with one that makes sure the ctl_fd is open.
2159 pi
= find_procinfo_or_die (pi
->pid
, 0);
2164 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2167 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2174 * Function: proc_set_current_signal
2176 * Set the "current signal" that will be delivered next to the process.
2177 * NOTE: semantics are different from those of KILL.
2178 * This signal will be delivered to the process or LWP
2179 * immediately when it is resumed (even if the signal is held/blocked);
2180 * it will NOT immediately cause another event of interest, and will NOT
2181 * first trap back to the debugger.
2183 * Returns non-zero for success, zero for failure.
2187 proc_set_current_signal (pi
, signo
)
2194 /* Use char array to avoid alignment issues. */
2195 char sinfo
[sizeof (struct siginfo
)];
2197 struct siginfo
*mysinfo
;
2200 * We should never have to apply this operation to any procinfo
2201 * except the one for the main process. If that ever changes
2202 * for any reason, then take out the following clause and
2203 * replace it with one that makes sure the ctl_fd is open.
2207 pi
= find_procinfo_or_die (pi
->pid
, 0);
2209 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2210 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2211 * receives a PIOCSSIG with a signal identical to the current signal,
2212 * it messes up the current signal. Work around the kernel bug.
2215 signo
== proc_cursig (pi
))
2216 return 1; /* I assume this is a success? */
2219 /* The pointer is just a type alias. */
2220 mysinfo
= (struct siginfo
*) &arg
.sinfo
;
2221 mysinfo
->si_signo
= signo
;
2222 mysinfo
->si_code
= 0;
2223 mysinfo
->si_pid
= getpid (); /* ?why? */
2224 mysinfo
->si_uid
= getuid (); /* ?why? */
2228 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2230 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2237 * Function: proc_clear_current_signal
2239 * The current signal (if any) is cleared, and
2240 * is not sent to the process or LWP when it resumes.
2241 * Returns non-zero for success, zero for failure.
2245 proc_clear_current_signal (pi
)
2251 * We should never have to apply this operation to any procinfo
2252 * except the one for the main process. If that ever changes
2253 * for any reason, then take out the following clause and
2254 * replace it with one that makes sure the ctl_fd is open.
2258 pi
= find_procinfo_or_die (pi
->pid
, 0);
2264 /* Use char array to avoid alignment issues. */
2265 char sinfo
[sizeof (struct siginfo
)];
2267 struct siginfo
*mysinfo
;
2270 /* The pointer is just a type alias. */
2271 mysinfo
= (struct siginfo
*) &arg
.sinfo
;
2272 mysinfo
->si_signo
= 0;
2273 mysinfo
->si_code
= 0;
2274 mysinfo
->si_errno
= 0;
2275 mysinfo
->si_pid
= getpid (); /* ?why? */
2276 mysinfo
->si_uid
= getuid (); /* ?why? */
2278 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2281 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2288 * Function: proc_get_gregs
2290 * Get the general registers for the process or LWP.
2291 * Returns non-zero for success, zero for failure.
2298 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2299 if (!proc_get_status (pi
))
2303 * OK, sorry about the ifdef's.
2304 * There's three cases instead of two, because
2305 * in this instance Unixware and Solaris/RW differ.
2309 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2310 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2311 #else /* not Unixware */
2312 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2313 #endif /* Unixware */
2314 #else /* not NEW_PROC_API */
2315 return &pi
->prstatus
.pr_reg
;
2316 #endif /* NEW_PROC_API */
2320 * Function: proc_get_fpregs
2322 * Get the floating point registers for the process or LWP.
2323 * Returns non-zero for success, zero for failure.
2327 proc_get_fpregs (pi
)
2331 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2332 if (!proc_get_status (pi
))
2335 #ifdef UNIXWARE /* a true architecture dependency */
2336 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2338 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2339 #endif /* Unixware */
2341 #else /* not NEW_PROC_API */
2342 if (pi
->fpregs_valid
)
2343 return &pi
->fpregset
; /* already got 'em */
2346 if (pi
->ctl_fd
== 0 &&
2347 open_procinfo_files (pi
, FD_CTL
) == 0)
2356 tid_t pr_error_thread
;
2357 tfpregset_t thread_1
;
2360 thread_fpregs
.pr_count
= 1;
2361 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2364 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2366 pi
->fpregs_valid
= 1;
2367 return &pi
->fpregset
; /* got 'em now! */
2369 else if (pi
->tid
!= 0 &&
2370 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2372 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2373 sizeof (pi
->fpregset
));
2374 pi
->fpregs_valid
= 1;
2375 return &pi
->fpregset
; /* got 'em now! */
2382 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2384 pi
->fpregs_valid
= 1;
2385 return &pi
->fpregset
; /* got 'em now! */
2398 * Function: proc_set_gregs
2400 * Write the general registers back to the process or LWP.
2401 * Returns non-zero for success, zero for failure.
2408 gdb_gregset_t
*gregs
;
2411 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2412 return 0; /* get_regs has already warned */
2414 if (pi
->ctl_fd
== 0 &&
2415 open_procinfo_files (pi
, FD_CTL
) == 0)
2424 /* Use char array to avoid alignment issues. */
2425 char gregs
[sizeof (gdb_gregset_t
)];
2429 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2430 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2432 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2436 /* Policy: writing the regs invalidates our cache. */
2437 pi
->gregs_valid
= 0;
2442 * Function: proc_set_fpregs
2444 * Modify the floating point register set of the process or LWP.
2445 * Returns non-zero for success, zero for failure.
2449 proc_set_fpregs (pi
)
2452 gdb_fpregset_t
*fpregs
;
2455 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2456 return 0; /* get_fpregs has already warned */
2458 if (pi
->ctl_fd
== 0 &&
2459 open_procinfo_files (pi
, FD_CTL
) == 0)
2468 /* Use char array to avoid alignment issues. */
2469 char fpregs
[sizeof (gdb_fpregset_t
)];
2473 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2474 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2478 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2483 tid_t pr_error_thread
;
2484 tfpregset_t thread_1
;
2487 thread_fpregs
.pr_count
= 1;
2488 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2489 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2491 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2494 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2495 #endif /* osf PIOCTSFPREG */
2496 #endif /* NEW_PROC_API */
2499 /* Policy: writing the regs invalidates our cache. */
2500 pi
->fpregs_valid
= 0;
2505 * Function: proc_kill
2507 * Send a signal to the proc or lwp with the semantics of "kill()".
2508 * Returns non-zero for success, zero for failure.
2512 proc_kill (pi
, signo
)
2519 * We might conceivably apply this operation to an LWP, and
2520 * the LWP's ctl file descriptor might not be open.
2523 if (pi
->ctl_fd
== 0 &&
2524 open_procinfo_files (pi
, FD_CTL
) == 0)
2535 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2536 #else /* ioctl method */
2537 /* FIXME: do I need the Alpha OSF fixups present in
2538 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2539 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2547 * Function: proc_parent_pid
2549 * Find the pid of the process that started this one.
2550 * Returns the parent process pid, or zero.
2554 proc_parent_pid (pi
)
2558 * We should never have to apply this operation to any procinfo
2559 * except the one for the main process. If that ever changes
2560 * for any reason, then take out the following clause and
2561 * replace it with one that makes sure the ctl_fd is open.
2565 pi
= find_procinfo_or_die (pi
->pid
, 0);
2567 if (!pi
->status_valid
)
2568 if (!proc_get_status (pi
))
2571 return pi
->prstatus
.pr_ppid
;
2576 * Function: proc_set_watchpoint
2581 proc_set_watchpoint (pi
, addr
, len
, wflags
)
2587 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2590 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2591 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2596 char watch
[sizeof (prwatch_t
)];
2600 pwatch
= (prwatch_t
*) &arg
.watch
;
2601 pwatch
->pr_vaddr
= addr
;
2602 pwatch
->pr_size
= len
;
2603 pwatch
->pr_wflags
= wflags
;
2604 #if defined(NEW_PROC_API) && defined (PCWATCH)
2606 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2608 #if defined (PIOCSWATCH)
2609 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2611 return 0; /* Fail */
2619 * Function: proc_iterate_over_mappings
2621 * Given a pointer to a function, call that function once for every
2622 * mapped address space in the process. The callback function
2623 * receives an open file descriptor for the file corresponding to
2624 * that mapped address space (if there is one), and the base address
2625 * of the mapped space. Quit when the callback function returns a
2626 * nonzero value, or at teh end of the mappings.
2628 * Returns: the first non-zero return value of the callback function,
2632 /* FIXME: it's probably a waste to cache this FD.
2633 It doesn't get called that often... and if I open it
2634 every time, I don't need to lseek it. */
2636 proc_iterate_over_mappings (func
)
2637 int (*func
) PARAMS ((int, CORE_ADDR
));
2641 #ifndef NEW_PROC_API /* avoid compiler warning */
2646 char pathname
[MAX_PROC_NAME_SIZE
];
2651 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
2655 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
2656 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
2657 proc_error (pi
, "proc_iterate_over_mappings (open)", __LINE__
);
2659 /* Make sure it gets closed again. */
2660 make_cleanup ((make_cleanup_func
) close
, (void *) map_fd
);
2662 /* Allocate space for mapping (lifetime only for this function). */
2663 map
= alloca (sizeof (struct prmap
));
2665 /* Now read the mappings from the file,
2666 open a file descriptor for those that have a name,
2667 and call the callback function. */
2668 while (read (map_fd
,
2670 sizeof (struct prmap
)) == sizeof (struct prmap
))
2672 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
2674 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
2677 if (map
->pr_mapname
[0] == 0)
2679 fd
= -1; /* no map file */
2683 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
2684 /* Note: caller's responsibility to close this fd! */
2685 fd
= open (name
, O_RDONLY
);
2686 /* Note: we don't test the above call for failure;
2687 we just pass the FD on as given. Sometimes there is
2688 no file, so the ioctl may return failure, but that's
2692 /* Stop looping if the callback returns non-zero. */
2693 if ((funcstat
= (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
)) != 0)
2697 /* Get the number of mapping entries. */
2698 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmaps
) < 0)
2699 proc_error (pi
, "proc_iterate_over_mappings (PIOCNMAP)", __LINE__
);
2701 /* Allocate space for mappings (lifetime only this function). */
2702 map
= (struct prmap
*) alloca ((nmaps
+ 1) * sizeof (struct prmap
));
2704 /* Read in all the mappings. */
2705 if (ioctl (pi
->ctl_fd
, PIOCMAP
, map
) < 0)
2706 proc_error (pi
, "proc_iterate_over_mappings (PIOCMAP)", __LINE__
);
2708 /* Now loop through the mappings, open an fd for each, and
2709 call the callback function. */
2711 i
< nmaps
&& map
[i
].pr_size
!= 0;
2714 /* Note: caller's responsibility to close this fd! */
2715 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
[i
].pr_vaddr
);
2716 /* Note: we don't test the above call for failure;
2717 we just pass the FD on as given. Sometimes there is
2718 no file, so the ioctl may return failure, but that's
2721 /* Stop looping if the callback returns non-zero. */
2722 if ((funcstat
= (*func
) (fd
, (CORE_ADDR
) map
[i
].pr_vaddr
)) != 0)
2730 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2732 #include <sys/sysi86.h>
2735 * Function: proc_get_LDT_entry
2741 * The 'key' is actually the value of the lower 16 bits of
2742 * the GS register for the LWP that we're interested in.
2744 * Return: matching ssh struct (LDT entry).
2748 proc_get_LDT_entry (pi
, key
)
2752 static struct ssd
*ldt_entry
= NULL
;
2754 char pathname
[MAX_PROC_NAME_SIZE
];
2755 struct cleanup
*old_chain
= NULL
;
2758 /* Allocate space for one LDT entry.
2759 This alloc must persist, because we return a pointer to it. */
2760 if (ldt_entry
== NULL
)
2761 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2763 /* Open the file descriptor for the LDT table. */
2764 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2765 if ((fd
= open (pathname
, O_RDONLY
)) < 0)
2767 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2770 /* Make sure it gets closed again! */
2771 old_chain
= make_cleanup ((make_cleanup_func
) close
, (void *) fd
);
2773 /* Now 'read' thru the table, find a match and return it. */
2774 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2776 if (ldt_entry
->sel
== 0 &&
2777 ldt_entry
->bo
== 0 &&
2778 ldt_entry
->acc1
== 0 &&
2779 ldt_entry
->acc2
== 0)
2780 break; /* end of table */
2781 /* If key matches, return this entry. */
2782 if (ldt_entry
->sel
== key
)
2785 /* Loop ended, match not found. */
2789 static int nalloc
= 0;
2791 /* Get the number of LDT entries. */
2792 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2794 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2798 /* Allocate space for the number of LDT entries. */
2799 /* This alloc has to persist, 'cause we return a pointer to it. */
2802 ldt_entry
= (struct ssd
*)
2803 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2807 /* Read the whole table in one gulp. */
2808 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2810 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2814 /* Search the table and return the (first) entry matching 'key'. */
2815 for (i
= 0; i
< nldt
; i
++)
2816 if (ldt_entry
[i
].sel
== key
)
2817 return &ldt_entry
[i
];
2819 /* Loop ended, match not found. */
2824 #endif /* TM_I386SOL2_H */
2826 /* =============== END, non-thread part of /proc "MODULE" =============== */
2828 /* =================== Thread "MODULE" =================== */
2830 /* NOTE: you'll see more ifdefs and duplication of functions here,
2831 since there is a different way to do threads on every OS. */
2834 * Function: proc_get_nthreads
2836 * Return the number of threads for the process
2839 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2844 proc_get_nthreads (pi
)
2849 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
2850 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
2856 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2858 * Solaris and Unixware version
2861 proc_get_nthreads (pi
)
2864 if (!pi
->status_valid
)
2865 if (!proc_get_status (pi
))
2869 * NEW_PROC_API: only works for the process procinfo,
2870 * because the LWP procinfos do not get prstatus filled in.
2873 if (pi
->tid
!= 0) /* find the parent process procinfo */
2874 pi
= find_procinfo_or_die (pi
->pid
, 0);
2876 return pi
->prstatus
.pr_nlwp
;
2884 proc_get_nthreads (pi
)
2893 * Function: proc_get_current_thread (LWP version)
2895 * Return the ID of the thread that had an event of interest.
2896 * (ie. the one that hit a breakpoint or other traced event).
2897 * All other things being equal, this should be the ID of a
2898 * thread that is currently executing.
2901 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2903 * Solaris and Unixware version
2906 proc_get_current_thread (pi
)
2910 * Note: this should be applied to the root procinfo for the process,
2911 * not to the procinfo for an LWP. If applied to the procinfo for
2912 * an LWP, it will simply return that LWP's ID. In that case,
2913 * find the parent process procinfo.
2917 pi
= find_procinfo_or_die (pi
->pid
, 0);
2919 if (!pi
->status_valid
)
2920 if (!proc_get_status (pi
))
2924 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
2926 return pi
->prstatus
.pr_who
;
2931 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2936 proc_get_current_thread (pi
)
2939 #if 0 /* FIXME: not ready for prime time? */
2940 return pi
->prstatus
.pr_tid
;
2951 proc_get_current_thread (pi
)
2961 * Function: proc_update_threads
2963 * Discover the IDs of all the threads within the process, and
2964 * create a procinfo for each of them (chained to the parent).
2966 * This unfortunately requires a different method on every OS.
2968 * Return: non-zero for success, zero for failure.
2972 proc_delete_dead_threads (parent
, thread
, ignore
)
2977 if (thread
&& parent
) /* sanity */
2979 thread
->status_valid
= 0;
2980 if (!proc_get_status (thread
))
2981 destroy_one_procinfo (&parent
->thread_list
, thread
);
2983 return 0; /* keep iterating */
2986 #if defined (PIOCLSTATUS)
2988 * Solaris 2.5 (ioctl) version
2991 proc_update_threads (pi
)
2994 gdb_prstatus_t
*prstatus
;
2995 struct cleanup
*old_chain
= NULL
;
3000 * We should never have to apply this operation to any procinfo
3001 * except the one for the main process. If that ever changes
3002 * for any reason, then take out the following clause and
3003 * replace it with one that makes sure the ctl_fd is open.
3007 pi
= find_procinfo_or_die (pi
->pid
, 0);
3009 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3011 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
3012 return 1; /* Process is not multi-threaded; nothing to do. */
3014 if ((prstatus
= (gdb_prstatus_t
*)
3015 malloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1))) == 0)
3016 perror_with_name ("procfs: malloc failed in update_threads");
3018 old_chain
= make_cleanup (free
, prstatus
);
3019 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
3020 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
3022 /* Skip element zero, which represents the process as a whole. */
3023 for (i
= 1; i
< nlwp
+ 1; i
++)
3025 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
3026 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3028 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
3029 thread
->status_valid
= 1;
3031 pi
->threads_valid
= 1;
3032 do_cleanups (old_chain
);
3038 * Unixware and Solaris 6 (and later) version
3041 proc_update_threads (pi
)
3044 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3045 struct dirent
*direntry
;
3046 struct cleanup
*old_chain
= NULL
;
3052 * We should never have to apply this operation to any procinfo
3053 * except the one for the main process. If that ever changes
3054 * for any reason, then take out the following clause and
3055 * replace it with one that makes sure the ctl_fd is open.
3059 pi
= find_procinfo_or_die (pi
->pid
, 0);
3061 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3066 * Note: this brute-force method is the only way I know of
3067 * to accomplish this task on Unixware. This method will
3068 * also work on Solaris 2.6 and 2.7. There is a much simpler
3069 * and more elegant way to do this on Solaris, but the margins
3070 * of this manuscript are too small to write it here... ;-)
3073 strcpy (pathname
, pi
->pathname
);
3074 strcat (pathname
, "/lwp");
3075 if ((dirp
= opendir (pathname
)) == NULL
)
3076 proc_error (pi
, "update_threads, opendir", __LINE__
);
3078 old_chain
= make_cleanup ((make_cleanup_func
) closedir
, dirp
);
3079 while ((direntry
= readdir (dirp
)) != NULL
)
3080 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3082 lwpid
= atoi (&direntry
->d_name
[0]);
3083 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3084 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3086 pi
->threads_valid
= 1;
3087 do_cleanups (old_chain
);
3096 proc_update_threads (pi
)
3103 * We should never have to apply this operation to any procinfo
3104 * except the one for the main process. If that ever changes
3105 * for any reason, then take out the following clause and
3106 * replace it with one that makes sure the ctl_fd is open.
3110 pi
= find_procinfo_or_die (pi
->pid
, 0);
3112 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3114 nthreads
= proc_get_nthreads (pi
);
3116 return 0; /* nothing to do for 1 or fewer threads */
3118 if ((threads
= malloc (nthreads
* sizeof (tid_t
))) == NULL
)
3119 proc_error (pi
, "update_threads, malloc", __LINE__
);
3121 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3122 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3124 for (i
= 0; i
< nthreads
; i
++)
3126 if (!find_procinfo (pi
->pid
, threads
[i
]))
3127 if (!create_procinfo (pi
->pid
, threads
[i
]))
3128 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3130 pi
->threads_valid
= 1;
3138 proc_update_threads (pi
)
3143 #endif /* OSF PIOCTLIST */
3144 #endif /* NEW_PROC_API */
3145 #endif /* SOL 2.5 PIOCLSTATUS */
3148 * Function: proc_iterate_over_threads
3151 * Given a pointer to a function, call that function once
3152 * for each lwp in the procinfo list, until the function
3153 * returns non-zero, in which event return the value
3154 * returned by the function.
3156 * Note: this function does NOT call update_threads.
3157 * If you want to discover new threads first, you must
3158 * call that function explicitly. This function just makes
3159 * a quick pass over the currently-known procinfos.
3162 * pi - parent process procinfo
3163 * func - per-thread function
3164 * ptr - opaque parameter for function.
3167 * First non-zero return value from the callee, or zero.
3171 proc_iterate_over_threads (pi
, func
, ptr
)
3173 int (*func
) PARAMS ((procinfo
*, procinfo
*, void *));
3176 procinfo
*thread
, *next
;
3180 * We should never have to apply this operation to any procinfo
3181 * except the one for the main process. If that ever changes
3182 * for any reason, then take out the following clause and
3183 * replace it with one that makes sure the ctl_fd is open.
3187 pi
= find_procinfo_or_die (pi
->pid
, 0);
3189 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3191 next
= thread
->next
; /* in case thread is destroyed */
3192 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3199 /* =================== END, Thread "MODULE" =================== */
3201 /* =================== END, /proc "MODULE" =================== */
3203 /* =================== GDB "MODULE" =================== */
3206 * Here are all of the gdb target vector functions and their friends.
3209 static int do_attach
PARAMS ((int pid
));
3210 static void do_detach
PARAMS ((int signo
));
3211 static int register_gdb_signals
PARAMS ((procinfo
*, sigset_t
*));
3214 * Function: procfs_debug_inferior
3216 * Sets up the inferior to be debugged.
3217 * Registers to trace signals, hardware faults, and syscalls.
3218 * Note: does not set RLC flag: caller may want to customize that.
3220 * Returns: zero for success (note! unlike most functions in this module)
3221 * On failure, returns the LINE NUMBER where it failed!
3225 procfs_debug_inferior (pi
)
3228 fltset_t traced_faults
;
3229 sigset_t traced_signals
;
3230 sysset_t traced_syscall_entries
;
3231 sysset_t traced_syscall_exits
;
3233 #ifdef PROCFS_DONT_TRACE_FAULTS
3234 /* On some systems (OSF), we don't trace hardware faults.
3235 Apparently it's enough that we catch them as signals.
3236 Wonder why we don't just do that in general? */
3237 premptyset (&traced_faults
); /* don't trace faults. */
3239 /* Register to trace hardware faults in the child. */
3240 prfillset (&traced_faults
); /* trace all faults... */
3241 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3243 if (!proc_set_traced_faults (pi
, &traced_faults
))
3246 /* Register to trace selected signals in the child. */
3247 premptyset (&traced_signals
);
3248 if (!register_gdb_signals (pi
, &traced_signals
))
3251 /* Register to trace the 'exit' system call (on entry). */
3252 premptyset (&traced_syscall_entries
);
3253 praddset (&traced_syscall_entries
, SYS_exit
);
3255 praddset (&traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3258 praddset (&traced_syscall_entries
, SYS_lwp_exit
);
3261 if (!proc_set_traced_sysentry (pi
, &traced_syscall_entries
))
3264 #ifdef PRFS_STOPEXEC /* defined on OSF */
3265 /* OSF method for tracing exec syscalls. Quoting:
3266 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3267 exits from exec system calls because of the user level loader. */
3268 /* FIXME: make nice and maybe move into an access function. */
3272 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3275 prfs_flags
|= PRFS_STOPEXEC
;
3277 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3280 #else /* not PRFS_STOPEXEC */
3281 /* Everyone else's (except OSF) method for tracing exec syscalls */
3283 Not all systems with /proc have all the exec* syscalls with the same
3284 names. On the SGI, for example, there is no SYS_exec, but there
3285 *is* a SYS_execv. So, we try to account for that. */
3287 premptyset (&traced_syscall_exits
);
3289 praddset (&traced_syscall_exits
, SYS_exec
);
3292 praddset (&traced_syscall_exits
, SYS_execve
);
3295 praddset (&traced_syscall_exits
, SYS_execv
);
3298 #ifdef SYS_lwpcreate
3299 praddset (&traced_syscall_exits
, SYS_lwpcreate
);
3300 praddset (&traced_syscall_exits
, SYS_lwpexit
);
3303 #ifdef SYS_lwp_create /* FIXME: once only, please */
3304 praddset (&traced_syscall_exits
, SYS_lwp_create
);
3305 praddset (&traced_syscall_exits
, SYS_lwp_exit
);
3309 if (!proc_set_traced_sysexit (pi
, &traced_syscall_exits
))
3312 #endif /* PRFS_STOPEXEC */
3317 procfs_attach (args
, from_tty
)
3325 error_no_arg ("process-id to attach");
3328 if (pid
== getpid ())
3329 error ("Attaching GDB to itself is not a good idea...");
3333 exec_file
= get_exec_file (0);
3336 printf_filtered ("Attaching to program `%s', %s\n",
3337 exec_file
, target_pid_to_str (pid
));
3339 printf_filtered ("Attaching to %s\n", target_pid_to_str (pid
));
3343 inferior_pid
= do_attach (pid
);
3344 push_target (&procfs_ops
);
3348 procfs_detach (args
, from_tty
)
3357 exec_file
= get_exec_file (0);
3360 printf_filtered ("Detaching from program: %s %s\n",
3361 exec_file
, target_pid_to_str (inferior_pid
));
3365 signo
= atoi (args
);
3369 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3379 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
3380 perror ("procfs: out of memory in 'attach'");
3382 if (!open_procinfo_files (pi
, FD_CTL
))
3384 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3385 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3387 dead_procinfo (pi
, errmsg
, NOKILL
);
3390 /* Stop the process (if it isn't already stopped). */
3391 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3393 pi
->was_stopped
= 1;
3394 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3398 pi
->was_stopped
= 0;
3399 /* Set the process to run again when we close it. */
3400 if (!proc_set_run_on_last_close (pi
))
3401 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3403 /* Now stop the process. */
3404 if (!proc_stop_process (pi
))
3405 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3406 pi
->ignore_next_sigstop
= 1;
3408 /* Save some of the /proc state to be restored if we detach. */
3409 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3410 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3411 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3412 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3413 if (!proc_get_traced_sysentry (pi
, &pi
->saved_entryset
))
3414 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3416 if (!proc_get_traced_sysexit (pi
, &pi
->saved_exitset
))
3417 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3419 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3420 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3422 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3423 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3425 /* Let GDB know that the inferior was attached. */
3427 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3436 /* Find procinfo for the main process */
3437 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0); /* FIXME: threads */
3439 if (!proc_set_current_signal (pi
, signo
))
3440 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3442 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3443 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3445 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3446 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3448 if (!proc_set_traced_sysentry (pi
, &pi
->saved_entryset
))
3449 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3451 if (!proc_set_traced_sysexit (pi
, &pi
->saved_exitset
))
3452 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3454 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3455 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3457 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3458 if (signo
|| !(pi
->was_stopped
) ||
3459 query ("Was stopped when attached, make it runnable again? "))
3461 /* Clear any pending signal. */
3462 if (!proc_clear_current_fault (pi
))
3463 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3465 if (!proc_set_run_on_last_close (pi
))
3466 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3470 destroy_procinfo (pi
);
3476 * Since the /proc interface cannot give us individual registers,
3477 * we pay no attention to the (regno) argument, and just fetch them all.
3478 * This results in the possibility that we will do unnecessarily many
3479 * fetches, since we may be called repeatedly for individual registers.
3480 * So we cache the results, and mark the cache invalid when the process
3485 procfs_fetch_registers (regno
)
3488 gdb_fpregset_t
*fpregs
;
3489 gdb_gregset_t
*gregs
;
3494 pid
= PIDGET (inferior_pid
);
3495 tid
= TIDGET (inferior_pid
);
3497 /* First look up procinfo for the main process. */
3498 pi
= find_procinfo_or_die (pid
, 0);
3500 /* If the event thread is not the same as GDB's requested thread
3501 (ie. inferior_pid), then look up procinfo for the requested
3504 (tid
!= proc_get_current_thread (pi
)))
3505 pi
= find_procinfo_or_die (pid
, tid
);
3508 error ("procfs: fetch_registers failed to find procinfo for %s",
3509 target_pid_to_str (inferior_pid
));
3511 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3512 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3514 supply_gregset (gregs
);
3516 #if defined (FP0_REGNUM) /* need floating point? */
3517 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3518 regno
== PC_REGNUM
||
3520 regno
== NPC_REGNUM
||
3522 regno
== FP_REGNUM
||
3524 return; /* not a floating point register */
3526 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3527 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3529 supply_fpregset (fpregs
);
3533 /* Get ready to modify the registers array. On machines which store
3534 individual registers, this doesn't need to do anything. On
3535 machines which store all the registers in one fell swoop, such as
3536 /proc, this makes sure that registers contains all the registers
3537 from the program being debugged. */
3540 procfs_prepare_to_store ()
3542 #ifdef CHILD_PREPARE_TO_STORE
3543 CHILD_PREPARE_TO_STORE ();
3550 * Since the /proc interface will not read individual registers,
3551 * we will cache these requests until the process is resumed, and
3552 * only then write them back to the inferior process.
3554 * FIXME: is that a really bad idea? Have to think about cases
3555 * where writing one register might affect the value of others, etc.
3559 procfs_store_registers (regno
)
3562 gdb_fpregset_t
*fpregs
;
3563 gdb_gregset_t
*gregs
;
3568 pid
= PIDGET (inferior_pid
);
3569 tid
= TIDGET (inferior_pid
);
3571 /* First find procinfo for main process */
3572 pi
= find_procinfo_or_die (pid
, 0);
3574 /* If current lwp for process is not the same as requested thread
3575 (ie. inferior_pid), then find procinfo for the requested thread. */
3578 (tid
!= proc_get_current_thread (pi
)))
3579 pi
= find_procinfo_or_die (pid
, tid
);
3582 error ("procfs: store_registers: failed to find procinfo for %s",
3583 target_pid_to_str (inferior_pid
));
3585 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3586 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3588 fill_gregset (gregs
, regno
);
3589 if (!proc_set_gregs (pi
))
3590 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3592 #if defined (FP0_REGNUM) /* need floating point? */
3593 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3594 regno
== PC_REGNUM
||
3596 regno
== NPC_REGNUM
||
3598 regno
== FP_REGNUM
||
3600 return; /* not a floating point register */
3602 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3603 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3605 fill_fpregset (fpregs
, regno
);
3606 if (!proc_set_fpregs (pi
))
3607 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3612 * Function: target_wait
3614 * Retrieve the next stop event from the child process.
3615 * If child has not stopped yet, wait for it to stop.
3616 * Translate /proc eventcodes (or possibly wait eventcodes)
3617 * into gdb internal event codes.
3619 * Return: id of process (and possibly thread) that incurred the event.
3620 * event codes are returned thru a pointer parameter.
3624 procfs_wait (pid
, status
)
3626 struct target_waitstatus
*status
;
3628 /* First cut: loosely based on original version 2.1 */
3632 int why
, what
, flags
;
3641 /* Find procinfo for main process */
3642 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
3645 /* We must assume that the status is stale now... */
3646 pi
->status_valid
= 0;
3647 pi
->gregs_valid
= 0;
3648 pi
->fpregs_valid
= 0;
3650 #if 0 /* just try this out... */
3651 flags
= proc_flags (pi
);
3652 why
= proc_why (pi
);
3653 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3654 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3656 /* If child is not stopped, wait for it to stop. */
3657 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3658 !proc_wait_for_stop (pi
))
3660 /* wait_for_stop failed: has the child terminated? */
3661 if (errno
== ENOENT
)
3663 /* /proc file not found; presumably child has terminated. */
3664 retval
= wait (&wstat
); /* "wait" for the child's exit */
3666 if (retval
!= PIDGET (inferior_pid
)) /* wrong child? */
3667 error ("procfs: couldn't stop process %d: wait returned %d\n",
3668 inferior_pid
, retval
);
3669 /* FIXME: might I not just use waitpid?
3670 Or try find_procinfo to see if I know about this child? */
3674 /* Unknown error from wait_for_stop. */
3675 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3680 /* This long block is reached if either:
3681 a) the child was already stopped, or
3682 b) we successfully waited for the child with wait_for_stop.
3683 This block will analyze the /proc status, and translate it
3684 into a waitstatus for GDB.
3686 If we actually had to call wait because the /proc file
3687 is gone (child terminated), then we skip this block,
3688 because we already have a waitstatus. */
3690 flags
= proc_flags (pi
);
3691 why
= proc_why (pi
);
3692 what
= proc_what (pi
);
3694 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3697 /* If it's running async (for single_thread control),
3698 set it back to normal again. */
3699 if (flags
& PR_ASYNC
)
3700 if (!proc_unset_async (pi
))
3701 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3705 proc_prettyprint_why (why
, what
, 1);
3707 /* The 'pid' we will return to GDB is composed of
3708 the process ID plus the lwp ID. */
3709 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3713 wstat
= (what
<< 8) | 0177;
3723 #if defined (SYS_lwp_exit) || defined (SYS_lwpexit)
3724 printf_filtered ("[%s exited]\n",
3725 target_pid_to_str (retval
));
3726 delete_thread (retval
);
3727 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3729 #endif /* _lwp_exit */
3732 /* Handle SYS_exit call only */
3733 /* Stopped at entry to SYS_exit.
3734 Make it runnable, resume it, then use
3735 the wait system call to get its exit code.
3736 Proc_run_process always clears the current
3738 Then return its exit status. */
3739 pi
->status_valid
= 0;
3741 /* FIXME: what we should do is return
3742 TARGET_WAITKIND_SPURIOUS. */
3743 if (!proc_run_process (pi
, 0, 0))
3744 proc_error (pi
, "target_wait, run_process", __LINE__
);
3747 /* Don't call wait: simulate waiting for exit,
3748 return a "success" exit code. Bogus: what if
3749 it returns something else? */
3751 retval
= inferior_pid
; /* ??? */
3755 int temp
= wait (&wstat
);
3757 /* FIXME: shouldn't I make sure I get the right
3758 event from the right process? If (for
3759 instance) I have killed an earlier inferior
3760 process but failed to clean up after it
3761 somehow, I could get its termination event
3764 /* If wait returns -1, that's what we return to GDB. */
3770 printf_filtered ("procfs: trapped on entry to ");
3771 proc_prettyprint_syscall (proc_what (pi
), 0);
3772 printf_filtered ("\n");
3775 long i
, nsysargs
, *sysargs
;
3777 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3778 (sysargs
= proc_sysargs (pi
)) != NULL
)
3780 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
3781 for (i
= 0; i
< nsysargs
; i
++)
3782 printf_filtered ("#%ld: 0x%08x\n",
3790 /* How to exit gracefully, returning "unknown event" */
3791 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3792 return inferior_pid
;
3796 /* How to keep going without returning to wfi: */
3797 target_resume (pid
, 0, TARGET_SIGNAL_0
);
3814 /* Hopefully this is our own "fork-child" execing
3815 the real child. Hoax this event into a trap, and
3816 GDB will see the child about to execute its start
3818 wstat
= (SIGTRAP
<< 8) | 0177;
3820 #ifdef SYS_lwp_create
3821 case SYS_lwp_create
:
3823 #ifdef SYS_lwpcreate
3826 #if defined(SYS_lwp_create) || defined(SYS_lwpcreate)
3828 * This syscall is somewhat like fork/exec.
3829 * We will get the event twice: once for the parent LWP,
3830 * and once for the child. We should already know about
3831 * the parent LWP, but the child will be new to us. So,
3832 * whenever we get this event, if it represents a new
3833 * thread, simply add the thread to the list.
3836 /* If not in procinfo list, add it. */
3837 temp
= proc_get_current_thread (pi
);
3838 if (!find_procinfo (pi
->pid
, temp
))
3839 create_procinfo (pi
->pid
, temp
);
3841 temp
= MERGEPID (pi
->pid
, temp
);
3842 /* If not in GDB's thread list, add it. */
3843 if (!in_thread_list (temp
))
3845 printf_filtered ("[New %s]\n", target_pid_to_str (temp
));
3848 /* Return to WFI, but tell it to immediately resume. */
3849 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3850 return inferior_pid
;
3851 #endif /* _lwp_create */
3859 #if defined (SYS_lwp_exit) || defined (SYS_lwpexit)
3860 printf_filtered ("[%s exited]\n",
3861 target_pid_to_str (retval
));
3862 delete_thread (retval
);
3863 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3865 #endif /* _lwp_exit */
3869 /* Nothing to do here for now. The old procfs
3870 seemed to use this event to handle threads on
3871 older (non-LWP) systems, where I'm assuming that
3872 threads were actually separate processes. Irix,
3873 maybe? Anyway, low priority for now. */
3877 /* FIXME: do we need to handle this? Investigate. */
3881 /* FIXME: see above. */
3884 printf_filtered ("procfs: trapped on exit from ");
3885 proc_prettyprint_syscall (proc_what (pi
), 0);
3886 printf_filtered ("\n");
3889 long i
, nsysargs
, *sysargs
;
3891 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3892 (sysargs
= proc_sysargs (pi
)) != NULL
)
3894 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
3895 for (i
= 0; i
< nsysargs
; i
++)
3896 printf_filtered ("#%ld: 0x%08x\n",
3901 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3902 return inferior_pid
;
3907 wstat
= (SIGSTOP
<< 8) | 0177;
3912 printf_filtered ("Retry #%d:\n", retry
);
3913 pi
->status_valid
= 0;
3918 /* If not in procinfo list, add it. */
3919 temp
= proc_get_current_thread (pi
);
3920 if (!find_procinfo (pi
->pid
, temp
))
3921 create_procinfo (pi
->pid
, temp
);
3923 /* If not in GDB's thread list, add it. */
3924 temp
= MERGEPID (pi
->pid
, temp
);
3925 if (!in_thread_list (temp
))
3927 printf_filtered ("[New %s]\n",
3928 target_pid_to_str (temp
));
3932 status
->kind
= TARGET_WAITKIND_STOPPED
;
3933 status
->value
.sig
= 0;
3938 wstat
= (what
<< 8) | 0177;
3941 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
3944 wstat
= (SIGTRAP
<< 8) | 0177;
3949 wstat
= (SIGTRAP
<< 8) | 0177;
3952 /* FIXME: use si_signo where possible. */
3954 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
3957 wstat
= (SIGILL
<< 8) | 0177;
3960 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
3963 wstat
= (SIGTRAP
<< 8) | 0177;
3967 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
3970 wstat
= (SIGSEGV
<< 8) | 0177;
3974 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
3977 wstat
= (SIGFPE
<< 8) | 0177;
3979 case FLTPAGE
: /* Recoverable page fault */
3980 default: /* FIXME: use si_signo if possible for fault */
3982 printf_filtered ("procfs:%d -- ", __LINE__
);
3983 printf_filtered ("child stopped for unknown reason:\n");
3984 proc_prettyprint_why (why
, what
, 1);
3985 error ("... giving up...");
3988 break; /* case PR_FAULTED: */
3989 default: /* switch (why) unmatched */
3990 printf_filtered ("procfs:%d -- ", __LINE__
);
3991 printf_filtered ("child stopped for unknown reason:\n");
3992 proc_prettyprint_why (why
, what
, 1);
3993 error ("... giving up...");
3997 * Got this far without error:
3998 * If retval isn't in the threads database, add it.
4001 retval
!= inferior_pid
&&
4002 !in_thread_list (retval
))
4005 * We have a new thread.
4006 * We need to add it both to GDB's list and to our own.
4007 * If we don't create a procinfo, resume may be unhappy
4010 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
4011 add_thread (retval
);
4012 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
4013 create_procinfo (PIDGET (retval
), TIDGET (retval
));
4015 /* In addition, it's possible that this is the first
4016 * new thread we've seen, in which case we may not
4017 * have created entries for inferior_pid yet.
4019 if (TIDGET (inferior_pid
) != 0)
4021 if (!in_thread_list (inferior_pid
))
4022 add_thread (inferior_pid
);
4023 if (find_procinfo (PIDGET (inferior_pid
),
4024 TIDGET (inferior_pid
)) == NULL
)
4025 create_procinfo (PIDGET (inferior_pid
),
4026 TIDGET (inferior_pid
));
4030 else /* flags do not indicate STOPPED */
4032 /* surely this can't happen... */
4033 printf_filtered ("procfs:%d -- process not stopped.\n",
4035 proc_prettyprint_flags (flags
, 1);
4036 error ("procfs: ...giving up...");
4041 store_waitstatus (status
, wstat
);
4048 procfs_xfer_memory (memaddr
, myaddr
, len
, dowrite
, target
)
4053 struct target_ops
*target
; /* ignored */
4058 /* Find procinfo for main process */
4059 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4060 if (pi
->as_fd
== 0 &&
4061 open_procinfo_files (pi
, FD_AS
) == 0)
4063 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4067 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4072 PROCFS_NOTE ("write memory: ");
4074 PROCFS_NOTE ("write memory: \n");
4076 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4080 PROCFS_NOTE ("read memory: \n");
4081 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4092 * Function: invalidate_cache
4094 * Called by target_resume before making child runnable.
4095 * Mark cached registers and status's invalid.
4096 * If there are "dirty" caches that need to be written back
4097 * to the child process, do that.
4099 * File descriptors are also cached.
4100 * As they are a limited resource, we cannot hold onto them indefinitely.
4101 * However, as they are expensive to open, we don't want to throw them
4102 * away indescriminately either. As a compromise, we will keep the
4103 * file descriptors for the parent process, but discard any file
4104 * descriptors we may have accumulated for the threads.
4107 * As this function is called by iterate_over_threads, it always
4108 * returns zero (so that iterate_over_threads will keep iterating).
4113 invalidate_cache (parent
, pi
, ptr
)
4119 * About to run the child; invalidate caches and do any other cleanup.
4123 if (pi
->gregs_dirty
)
4124 if (parent
== NULL
||
4125 proc_get_current_thread (parent
) != pi
->tid
)
4126 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4127 proc_warn (pi
, "target_resume, set_gregs",
4130 if (pi
->fpregs_dirty
)
4131 if (parent
== NULL
||
4132 proc_get_current_thread (parent
) != pi
->tid
)
4133 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4134 proc_warn (pi
, "target_resume, set_fpregs",
4141 /* The presence of a parent indicates that this is an LWP.
4142 Close any file descriptors that it might have open.
4143 We don't do this to the master (parent) procinfo. */
4145 close_procinfo_files (pi
);
4147 pi
->gregs_valid
= 0;
4148 pi
->fpregs_valid
= 0;
4150 pi
->gregs_dirty
= 0;
4151 pi
->fpregs_dirty
= 0;
4153 pi
->status_valid
= 0;
4154 pi
->threads_valid
= 0;
4161 * Function: make_signal_thread_runnable
4163 * A callback function for iterate_over_threads.
4164 * Find the asynchronous signal thread, and make it runnable.
4165 * See if that helps matters any.
4169 make_signal_thread_runnable (process
, pi
, ptr
)
4175 if (proc_flags (pi
) & PR_ASLWP
)
4177 if (!proc_run_process (pi
, 0, -1))
4178 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4187 * Function: target_resume
4189 * Make the child process runnable. Normally we will then call
4190 * procfs_wait and wait for it to stop again (unles gdb is async).
4193 * step: if true, then arrange for the child to stop again
4194 * after executing a single instruction.
4195 * signo: if zero, then cancel any pending signal.
4196 * If non-zero, then arrange for the indicated signal
4197 * to be delivered to the child when it runs.
4198 * pid: if -1, then allow any child thread to run.
4199 * if non-zero, then allow only the indicated thread to run.
4200 ******* (not implemented yet)
4204 procfs_resume (pid
, step
, signo
)
4207 enum target_signal signo
;
4209 procinfo
*pi
, *thread
;
4213 prrun.prflags |= PRSVADDR;
4214 prrun.pr_vaddr = $PC; set resume address
4215 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4216 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4217 prrun.prflags |= PRCFAULT; clear current fault.
4219 PRSTRACE and PRSFAULT can be done by other means
4220 (proc_trace_signals, proc_trace_faults)
4221 PRSVADDR is unnecessary.
4222 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4223 This basically leaves PRSTEP and PRCSIG.
4224 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4225 So basically PR_STEP is the sole argument that must be passed
4226 to proc_run_process (for use in the prrun struct by ioctl). */
4228 /* Find procinfo for main process */
4229 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4231 /* First cut: ignore pid argument */
4234 /* Convert signal to host numbering. */
4236 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4239 native_signo
= target_signal_to_host (signo
);
4241 pi
->ignore_next_sigstop
= 0;
4243 /* Running the process voids all cached registers and status. */
4244 /* Void the threads' caches first */
4245 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4246 /* Void the process procinfo's caches. */
4247 invalidate_cache (NULL
, pi
, NULL
);
4251 /* Resume a specific thread, presumably suppressing the others. */
4252 thread
= find_procinfo (PIDGET (pid
), TIDGET (pid
));
4254 warning ("procfs: resume can't find thread %d -- resuming all.",
4258 if (thread
->tid
!= 0)
4260 /* We're to resume a specific thread, and not the others.
4261 * Set the child process's PR_ASYNC flag.
4264 if (!proc_set_async (pi
))
4265 proc_error (pi
, "target_resume, set_async", __LINE__
);
4268 proc_iterate_over_threads (pi
,
4269 make_signal_thread_runnable
,
4272 pi
= thread
; /* substitute the thread's procinfo for run */
4277 if (!proc_run_process (pi
, step
, native_signo
))
4280 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4282 proc_error (pi
, "target_resume", __LINE__
);
4287 * Function: register_gdb_signals
4289 * Traverse the list of signals that GDB knows about
4290 * (see "handle" command), and arrange for the target
4291 * to be stopped or not, according to these settings.
4293 * Returns non-zero for success, zero for failure.
4297 register_gdb_signals (pi
, signals
)
4303 for (signo
= 0; signo
< NSIG
; signo
++)
4304 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4305 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4306 signal_pass_state (target_signal_from_host (signo
)) == 1)
4307 prdelset (signals
, signo
);
4309 praddset (signals
, signo
);
4311 return proc_set_traced_signals (pi
, signals
);
4315 * Function: target_notice_signals
4317 * Set up to trace signals in the child process.
4321 procfs_notice_signals (pid
)
4325 procinfo
*pi
= find_procinfo_or_die (PIDGET (pid
), 0);
4327 if (proc_get_traced_signals (pi
, &signals
) &&
4328 register_gdb_signals (pi
, &signals
))
4331 proc_error (pi
, "notice_signals", __LINE__
);
4335 * Function: target_files_info
4337 * Print status information about the child process.
4341 procfs_files_info (ignore
)
4342 struct target_ops
*ignore
;
4344 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4345 attach_flag
? "attached": "child",
4346 target_pid_to_str (inferior_pid
));
4350 * Function: target_open
4352 * A dummy: you don't open procfs.
4356 procfs_open (args
, from_tty
)
4360 error ("Use the \"run\" command to start a Unix child process.");
4364 * Function: target_can_run
4366 * This tells GDB that this target vector can be invoked
4367 * for "run" or "attach".
4370 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4371 be a runnable target. Used by targets
4372 that can sit atop procfs, such as solaris
4379 /* This variable is controlled by modules that sit atop procfs that
4380 may layer their own process structure atop that provided here.
4381 sol-thread.c does this because of the Solaris two-level thread
4384 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4386 return !procfs_suppress_run
;
4390 * Function: target_stop
4392 * Stop the child process asynchronously, as when the
4393 * gdb user types control-c or presses a "stop" button.
4395 * Works by sending kill(SIGINT) to the child's process group.
4401 extern pid_t inferior_process_group
;
4403 kill (-inferior_process_group
, SIGINT
);
4407 * Function: unconditionally_kill_inferior
4409 * Make it die. Wait for it to die. Clean up after it.
4410 * Note: this should only be applied to the real process,
4411 * not to an LWP, because of the check for parent-process.
4412 * If we need this to work for an LWP, it needs some more logic.
4416 unconditionally_kill_inferior (pi
)
4421 parent_pid
= proc_parent_pid (pi
);
4422 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4423 /* FIXME: use access functions */
4424 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4425 before the PIOCKILL, otherwise it might generate a corrupted core
4426 file for the inferior. */
4427 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4429 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4432 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4433 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4434 to kill the inferior, otherwise it might remain stopped with a
4436 We do not check the result of the PIOCSSIG, the inferior might have
4439 struct siginfo newsiginfo
;
4441 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4442 newsiginfo
.si_signo
= SIGKILL
;
4443 newsiginfo
.si_code
= 0;
4444 newsiginfo
.si_errno
= 0;
4445 newsiginfo
.si_pid
= getpid ();
4446 newsiginfo
.si_uid
= getuid ();
4447 /* FIXME: use proc_set_current_signal */
4448 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4450 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4451 if (!proc_kill (pi
, SIGKILL
))
4452 proc_warn (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4453 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4454 destroy_procinfo (pi
);
4456 /* If pi is GDB's child, wait for it to die. */
4457 if (parent_pid
== getpid ())
4458 /* FIXME: should we use waitpid to make sure we get the right event?
4459 Should we check the returned event? */
4464 ret
= waitpid (pi
->pid
, &status
, 0);
4472 * Function: target_kill_inferior
4474 * We're done debugging it, and we want it to go away.
4475 * Then we want GDB to forget all about it.
4479 procfs_kill_inferior ()
4481 if (inferior_pid
!= 0) /* ? */
4483 /* Find procinfo for main process */
4484 procinfo
*pi
= find_procinfo (PIDGET (inferior_pid
), 0);
4487 unconditionally_kill_inferior (pi
);
4488 target_mourn_inferior ();
4493 * Function: target_mourn_inferior
4495 * Forget we ever debugged this thing!
4499 procfs_mourn_inferior ()
4503 if (inferior_pid
!= 0)
4505 /* Find procinfo for main process */
4506 pi
= find_procinfo (PIDGET (inferior_pid
), 0);
4508 destroy_procinfo (pi
);
4510 unpush_target (&procfs_ops
);
4511 generic_mourn_inferior ();
4515 * Function: init_inferior
4517 * When GDB forks to create a runnable inferior process,
4518 * this function is called on the parent side of the fork.
4519 * It's job is to do whatever is necessary to make the child
4520 * ready to be debugged, and then wait for the child to synchronize.
4524 procfs_init_inferior (pid
)
4531 /* This routine called on the parent side (GDB side)
4532 after GDB forks the inferior. */
4534 push_target (&procfs_ops
);
4536 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4537 perror ("procfs: out of memory in 'init_inferior'");
4539 if (!open_procinfo_files (pi
, FD_CTL
))
4540 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4544 open_procinfo_files // done
4547 procfs_notice_signals
4554 /* If not stopped yet, wait for it to stop. */
4555 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4556 !(proc_wait_for_stop (pi
)))
4557 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4559 /* Save some of the /proc state to be restored if we detach. */
4560 /* FIXME: Why? In case another debugger was debugging it?
4561 We're it's parent, for Ghu's sake! */
4562 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4563 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4564 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4565 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4566 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4567 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4568 if (!proc_get_traced_sysentry (pi
, &pi
->saved_entryset
))
4569 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4570 if (!proc_get_traced_sysexit (pi
, &pi
->saved_exitset
))
4571 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4573 /* Register to trace selected signals in the child. */
4574 prfillset (&signals
);
4575 if (!register_gdb_signals (pi
, &signals
))
4576 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4578 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4579 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4581 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4582 and possibly even turning ON kill-on-last-close at this point. But
4583 I can't make that change without careful testing which I don't have
4584 time to do right now... */
4585 /* Turn on run-on-last-close flag so that the child
4586 will die if GDB goes away for some reason. */
4587 if (!proc_set_run_on_last_close (pi
))
4588 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4590 /* The 'process ID' we return to GDB is composed of
4591 the actual process ID plus the lwp ID. */
4592 inferior_pid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4594 #ifdef START_INFERIOR_TRAPS_EXPECTED
4595 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4597 /* One trap to exec the shell, one to exec the program being debugged. */
4598 startup_inferior (2);
4599 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4603 * Function: set_exec_trap
4605 * When GDB forks to create a new process, this function is called
4606 * on the child side of the fork before GDB exec's the user program.
4607 * Its job is to make the child minimally debuggable, so that the
4608 * parent GDB process can connect to the child and take over.
4609 * This function should do only the minimum to make that possible,
4610 * and to synchronize with the parent process. The parent process
4611 * should take care of the details.
4615 procfs_set_exec_trap ()
4617 /* This routine called on the child side (inferior side)
4618 after GDB forks the inferior. It must use only local variables,
4619 because it may be sharing data space with its parent. */
4624 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4625 perror_with_name ("procfs: create_procinfo failed in child.");
4627 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4629 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4630 gdb_flush (gdb_stderr
);
4631 /* no need to call "dead_procinfo", because we're going to exit. */
4635 #ifdef PRFS_STOPEXEC /* defined on OSF */
4636 /* OSF method for tracing exec syscalls. Quoting:
4637 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4638 exits from exec system calls because of the user level loader. */
4639 /* FIXME: make nice and maybe move into an access function. */
4643 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4645 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4646 gdb_flush (gdb_stderr
);
4649 prfs_flags
|= PRFS_STOPEXEC
;
4651 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4653 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4654 gdb_flush (gdb_stderr
);
4658 #else /* not PRFS_STOPEXEC */
4659 /* Everyone else's (except OSF) method for tracing exec syscalls */
4661 Not all systems with /proc have all the exec* syscalls with the same
4662 names. On the SGI, for example, there is no SYS_exec, but there
4663 *is* a SYS_execv. So, we try to account for that. */
4665 premptyset (&exitset
);
4667 praddset (&exitset
, SYS_exec
);
4670 praddset (&exitset
, SYS_execve
);
4673 praddset (&exitset
, SYS_execv
);
4676 if (!proc_set_traced_sysexit (pi
, &exitset
))
4678 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4679 gdb_flush (gdb_stderr
);
4682 #endif /* PRFS_STOPEXEC */
4684 /* FIXME: should this be done in the parent instead? */
4685 /* Turn off inherit on fork flag so that all grand-children
4686 of gdb start with tracing flags cleared. */
4687 if (!proc_unset_inherit_on_fork (pi
))
4688 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4690 /* Turn off run on last close flag, so that the child process
4691 cannot run away just because we close our handle on it.
4692 We want it to wait for the parent to attach. */
4693 if (!proc_unset_run_on_last_close (pi
))
4694 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4696 /* FIXME: No need to destroy the procinfo --
4697 we have our own address space, and we're about to do an exec! */
4698 /*destroy_procinfo (pi);*/
4702 * Function: create_inferior
4704 * This function is called BEFORE gdb forks the inferior process.
4705 * Its only real responsibility is to set things up for the fork,
4706 * and tell GDB which two functions to call after the fork (one
4707 * for the parent, and one for the child).
4709 * This function does a complicated search for a unix shell program,
4710 * which it then uses to parse arguments and environment variables
4711 * to be sent to the child. I wonder whether this code could not
4712 * be abstracted out and shared with other unix targets such as
4717 procfs_create_inferior (exec_file
, allargs
, env
)
4722 char *shell_file
= getenv ("SHELL");
4724 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4727 /* We will be looking down the PATH to find shell_file. If we
4728 just do this the normal way (via execlp, which operates by
4729 attempting an exec for each element of the PATH until it
4730 finds one which succeeds), then there will be an exec for
4731 each failed attempt, each of which will cause a PR_SYSEXIT
4732 stop, and we won't know how to distinguish the PR_SYSEXIT's
4733 for these failed execs with the ones for successful execs
4734 (whether the exec has succeeded is stored at that time in the
4735 carry bit or some such architecture-specific and
4736 non-ABI-specified place).
4738 So I can't think of anything better than to search the PATH
4739 now. This has several disadvantages: (1) There is a race
4740 condition; if we find a file now and it is deleted before we
4741 exec it, we lose, even if the deletion leaves a valid file
4742 further down in the PATH, (2) there is no way to know exactly
4743 what an executable (in the sense of "capable of being
4744 exec'd") file is. Using access() loses because it may lose
4745 if the caller is the superuser; failing to use it loses if
4746 there are ACLs or some such. */
4750 /* FIXME-maybe: might want "set path" command so user can change what
4751 path is used from within GDB. */
4752 char *path
= getenv ("PATH");
4754 struct stat statbuf
;
4757 path
= "/bin:/usr/bin";
4759 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4760 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4762 p1
= strchr (p
, ':');
4767 strncpy (tryname
, p
, len
);
4768 tryname
[len
] = '\0';
4769 strcat (tryname
, "/");
4770 strcat (tryname
, shell_file
);
4771 if (access (tryname
, X_OK
) < 0)
4773 if (stat (tryname
, &statbuf
) < 0)
4775 if (!S_ISREG (statbuf
.st_mode
))
4776 /* We certainly need to reject directories. I'm not quite
4777 as sure about FIFOs, sockets, etc., but I kind of doubt
4778 that people want to exec() these things. */
4783 /* Not found. This must be an error rather than merely passing
4784 the file to execlp(), because execlp() would try all the
4785 exec()s, causing GDB to get confused. */
4786 error ("procfs:%d -- Can't find shell %s in PATH",
4787 __LINE__
, shell_file
);
4789 shell_file
= tryname
;
4792 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4793 procfs_init_inferior
, NULL
, shell_file
);
4795 /* We are at the first instruction we care about. */
4796 /* Pedal to the metal... */
4798 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
4802 * Function: notice_thread
4804 * Callback for find_new_threads.
4805 * Calls "add_thread".
4809 procfs_notice_thread (pi
, thread
, ptr
)
4814 int gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
4816 if (!in_thread_list (gdb_threadid
))
4817 add_thread (gdb_threadid
);
4823 * Function: target_find_new_threads
4825 * Query all the threads that the target knows about,
4826 * and give them back to GDB to add to its list.
4830 procfs_find_new_threads ()
4834 /* Find procinfo for main process */
4835 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4836 proc_update_threads (pi
);
4837 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
4841 * Function: target_thread_alive
4843 * Return true if the thread is still 'alive'.
4845 * This guy doesn't really seem to be doing his job.
4846 * Got to investigate how to tell when a thread is really gone.
4850 procfs_thread_alive (pid
)
4856 proc
= PIDGET (pid
);
4857 thread
= TIDGET (pid
);
4858 /* If I don't know it, it ain't alive! */
4859 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
4862 /* If I can't get its status, it ain't alive!
4863 What's more, I need to forget about it! */
4864 if (!proc_get_status (pi
))
4866 destroy_procinfo (pi
);
4869 /* I couldn't have got its status if it weren't alive, so it's alive. */
4874 * Function: target_pid_to_str
4876 * Return a string to be used to identify the thread in
4877 * the "info threads" display.
4881 procfs_pid_to_str (pid
)
4884 static char buf
[80];
4888 proc
= PIDGET (pid
);
4889 thread
= TIDGET (pid
);
4890 pi
= find_procinfo (proc
, thread
);
4893 sprintf (buf
, "Process %d", proc
);
4895 sprintf (buf
, "LWP %d", thread
);
4900 * Function: procfs_set_watchpoint
4901 * Insert a watchpoint
4905 procfs_set_watchpoint (pid
, addr
, len
, rwflag
, after
)
4916 pi
= find_procinfo_or_die (pid
== -1 ?
4917 PIDGET (inferior_pid
) : PIDGET (pid
), 0);
4919 /* Translate from GDB's flags to /proc's */
4920 if (len
> 0) /* len == 0 means delete watchpoint */
4922 switch (rwflag
) { /* FIXME: need an enum! */
4923 case hw_write
: /* default watchpoint (write) */
4924 pflags
= WRITE_WATCHFLAG
;
4926 case hw_read
: /* read watchpoint */
4927 pflags
= READ_WATCHFLAG
;
4929 case hw_access
: /* access watchpoint */
4930 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
4932 case hw_execute
: /* execution HW breakpoint */
4933 pflags
= EXEC_WATCHFLAG
;
4935 default: /* Something weird. Return error. */
4938 if (after
) /* Stop after r/w access is completed. */
4939 pflags
|= AFTER_WATCHFLAG
;
4942 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
4944 if (errno
== E2BIG
) /* Typical error for no resources */
4945 return -1; /* fail */
4946 /* GDB may try to remove the same watchpoint twice.
4947 If a remove request returns no match, don't error. */
4948 if (errno
== ESRCH
&& len
== 0)
4949 return 0; /* ignore */
4950 proc_error (pi
, "set_watchpoint", __LINE__
);
4957 * Function: stopped_by_watchpoint
4959 * Returns non-zero if process is stopped on a hardware watchpoint fault,
4960 * else returns zero.
4964 procfs_stopped_by_watchpoint (pid
)
4969 pi
= find_procinfo_or_die (pid
== -1 ?
4970 PIDGET (inferior_pid
) : PIDGET (pid
), 0);
4971 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
4973 if (proc_why (pi
) == PR_FAULTED
)
4976 if (proc_what (pi
) == FLTWATCH
)
4980 if (proc_what (pi
) == FLTKWATCH
)
4988 #ifdef TM_I386SOL2_H
4990 * Function: procfs_find_LDT_entry
4993 * int pid; // The GDB-style pid-plus-LWP.
4996 * pointer to the corresponding LDT entry.
5000 procfs_find_LDT_entry (pid
)
5003 gdb_gregset_t
*gregs
;
5007 /* Find procinfo for the lwp. */
5008 if ((pi
= find_procinfo (PIDGET (pid
), TIDGET (pid
))) == NULL
)
5010 warning ("procfs_find_LDT_entry: could not find procinfi for %d.",
5014 /* get its general registers. */
5015 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
5017 warning ("procfs_find_LDT_entry: could not read gregs for %d.",
5021 /* Now extract the GS register's lower 16 bits. */
5022 key
= (*gregs
)[GS
] & 0xffff;
5024 /* Find the matching entry and return it. */
5025 return proc_get_LDT_entry (pi
, key
);
5027 #endif /* TM_I386SOL2_H */
5032 info_proc_cmd (args
, from_tty
)
5036 struct cleanup
*old_chain
;
5037 procinfo
*process
= NULL
;
5038 procinfo
*thread
= NULL
;
5044 old_chain
= make_cleanup (null_cleanup
, 0);
5047 if ((argv
= buildargv (args
)) == NULL
)
5050 make_cleanup ((make_cleanup_func
) freeargv
, argv
);
5052 while (argv
!= NULL
&& *argv
!= NULL
)
5054 if (isdigit (argv
[0][0]))
5056 pid
= strtoul (argv
[0], &tmp
, 10);
5058 tid
= strtoul (++tmp
, NULL
, 10);
5060 else if (argv
[0][0] == '/')
5062 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5071 pid
= PIDGET (inferior_pid
);
5073 error ("No current process: you must name one.");
5076 /* Have pid, will travel.
5077 First see if it's a process we're already debugging. */
5078 process
= find_procinfo (pid
, 0);
5079 if (process
== NULL
)
5081 /* No. So open a procinfo for it, but
5082 remember to close it again when finished. */
5083 process
= create_procinfo (pid
, 0);
5084 make_cleanup ((make_cleanup_func
) destroy_procinfo
, process
);
5085 if (!open_procinfo_files (process
, FD_CTL
))
5086 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5090 thread
= create_procinfo (pid
, tid
);
5094 printf_filtered ("process %d flags:\n", process
->pid
);
5095 proc_prettyprint_flags (proc_flags (process
), 1);
5096 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5097 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5098 if (proc_get_nthreads (process
) > 1)
5099 printf_filtered ("Process has %d threads.\n",
5100 proc_get_nthreads (process
));
5104 printf_filtered ("thread %d flags:\n", thread
->tid
);
5105 proc_prettyprint_flags (proc_flags (thread
), 1);
5106 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5107 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5110 do_cleanups (old_chain
);
5114 proc_trace_syscalls (args
, from_tty
, entry_or_exit
, mode
)
5124 if (inferior_pid
<= 0)
5125 error ("you must be debugging a process to use this command.");
5127 if (args
== NULL
|| args
[0] == 0)
5128 error_no_arg ("system call to trace");
5130 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
5131 if (isdigit (args
[0]))
5133 syscallnum
= atoi (args
);
5134 if (entry_or_exit
== PR_SYSENTRY
)
5135 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5137 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5140 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5142 if (mode
== FLAG_SET
)
5143 praddset (sysset
, syscallnum
);
5145 prdelset (sysset
, syscallnum
);
5147 if (entry_or_exit
== PR_SYSENTRY
)
5149 if (!proc_set_traced_sysentry (pi
, sysset
))
5150 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5154 if (!proc_set_traced_sysexit (pi
, sysset
))
5155 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5161 proc_trace_sysentry_cmd (args
, from_tty
)
5165 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5169 proc_trace_sysexit_cmd (args
, from_tty
)
5173 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5177 proc_untrace_sysentry_cmd (args
, from_tty
)
5181 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5185 proc_untrace_sysexit_cmd (args
, from_tty
)
5189 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5194 mapping_test (fd
, core_addr
)
5196 CORE_ADDR core_addr
;
5198 printf ("File descriptor %d, base address 0x%08x\n", fd
, core_addr
);
5205 test_mapping_cmd (args
, from_tty
)
5210 ret
= proc_iterate_over_mappings (mapping_test
);
5211 printf ("iterate_over_mappings returned %d.\n", ret
);
5215 _initialize_procfs ()
5218 add_target (&procfs_ops
);
5219 add_info ("proc", info_proc_cmd
,
5220 "Show /proc process information about any running process.\
5221 Default is the process being debugged.");
5222 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5223 "Give a trace of entries into the syscall.");
5224 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5225 "Give a trace of exits from the syscall.");
5226 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5227 "Cancel a trace of entries into the syscall.");
5228 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5229 "Cancel a trace of exits from the syscall.");
5231 add_com ("test-mapping", no_class
, test_mapping_cmd
,
5232 "test iterate-over-mappings");
5235 /* =================== END, GDB "MODULE" =================== */
5239 /* miscelaneous stubs: */
5240 /* The following satisfy a few random symbols mostly created by */
5241 /* the solaris threads implementation, which I will chase down */
5245 * Return a pid for which we guarantee
5246 * we will be able to find a 'live' procinfo.
5250 procfs_first_available ()
5253 return procinfo_list
->pid
;