1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
2 Copyright 1999-2000 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>
44 * This module provides the interface between GDB and the
45 * /proc file system, which is used on many versions of Unix
46 * as a means for debuggers to control other processes.
47 * Examples of the systems that use this interface are:
53 * /proc works by immitating a file system: you open a simulated file
54 * that represents the process you wish to interact with, and
55 * perform operations on that "file" in order to examine or change
56 * the state of the other process.
58 * The most important thing to know about /proc and this module
59 * is that there are two very different interfaces to /proc:
60 * One that uses the ioctl system call, and
61 * another that uses read and write system calls.
62 * This module has to support both /proc interfaces. This means
63 * that there are two different ways of doing every basic operation.
65 * In order to keep most of the code simple and clean, I have
66 * defined an interface "layer" which hides all these system calls.
67 * An ifdef (NEW_PROC_API) determines which interface we are using,
68 * and most or all occurrances of this ifdef should be confined to
69 * this interface layer.
73 /* Determine which /proc API we are using:
74 The ioctl API defines PIOCSTATUS, while
75 the read/write (multiple fd) API never does. */
78 #include <sys/types.h>
79 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
82 #include <fcntl.h> /* for O_RDONLY */
83 #include <unistd.h> /* for "X_OK" */
84 #include "gdb_stat.h" /* for struct stat */
86 /* Note: procfs-utils.h must be included after the above system header
87 files, because it redefines various system calls using macros.
88 This may be incompatible with the prototype declarations. */
90 #include "proc-utils.h"
92 /* Prototypes for supply_gregset etc. */
95 /* =================== TARGET_OPS "MODULE" =================== */
98 * This module defines the GDB target vector and its methods.
101 static void procfs_open
PARAMS((char *, int));
102 static void procfs_attach
PARAMS ((char *, int));
103 static void procfs_detach
PARAMS ((char *, int));
104 static void procfs_resume
PARAMS ((int, int, enum target_signal
));
105 static int procfs_can_run
PARAMS ((void));
106 static void procfs_stop
PARAMS ((void));
107 static void procfs_files_info
PARAMS ((struct target_ops
*));
108 static void procfs_fetch_registers
PARAMS ((int));
109 static void procfs_store_registers
PARAMS ((int));
110 static void procfs_notice_signals
PARAMS ((int));
111 static void procfs_prepare_to_store
PARAMS ((void));
112 static void procfs_kill_inferior
PARAMS ((void));
113 static void procfs_mourn_inferior
PARAMS ((void));
114 static void procfs_create_inferior
PARAMS ((char *, char *, char **));
115 static int procfs_wait
PARAMS ((int,
116 struct target_waitstatus
*));
117 static int procfs_xfer_memory
PARAMS ((CORE_ADDR
,
119 struct target_ops
*));
121 static int procfs_thread_alive
PARAMS ((int));
123 void procfs_find_new_threads
PARAMS ((void));
124 char *procfs_pid_to_str
PARAMS ((int));
126 struct target_ops procfs_ops
; /* the target vector */
131 procfs_ops
.to_shortname
= "procfs";
132 procfs_ops
.to_longname
= "Unix /proc child process";
134 "Unix /proc child process (started by the \"run\" command).";
135 procfs_ops
.to_open
= procfs_open
;
136 procfs_ops
.to_can_run
= procfs_can_run
;
137 procfs_ops
.to_create_inferior
= procfs_create_inferior
;
138 procfs_ops
.to_kill
= procfs_kill_inferior
;
139 procfs_ops
.to_mourn_inferior
= procfs_mourn_inferior
;
140 procfs_ops
.to_attach
= procfs_attach
;
141 procfs_ops
.to_detach
= procfs_detach
;
142 procfs_ops
.to_wait
= procfs_wait
;
143 procfs_ops
.to_resume
= procfs_resume
;
144 procfs_ops
.to_prepare_to_store
= procfs_prepare_to_store
;
145 procfs_ops
.to_fetch_registers
= procfs_fetch_registers
;
146 procfs_ops
.to_store_registers
= procfs_store_registers
;
147 procfs_ops
.to_xfer_memory
= procfs_xfer_memory
;
148 procfs_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
149 procfs_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
150 procfs_ops
.to_notice_signals
= procfs_notice_signals
;
151 procfs_ops
.to_files_info
= procfs_files_info
;
152 procfs_ops
.to_stop
= procfs_stop
;
154 procfs_ops
.to_terminal_init
= terminal_init_inferior
;
155 procfs_ops
.to_terminal_inferior
= terminal_inferior
;
156 procfs_ops
.to_terminal_ours_for_output
= terminal_ours_for_output
;
157 procfs_ops
.to_terminal_ours
= terminal_ours
;
158 procfs_ops
.to_terminal_info
= child_terminal_info
;
160 procfs_ops
.to_find_new_threads
= procfs_find_new_threads
;
161 procfs_ops
.to_thread_alive
= procfs_thread_alive
;
162 procfs_ops
.to_pid_to_str
= procfs_pid_to_str
;
164 procfs_ops
.to_has_all_memory
= 1;
165 procfs_ops
.to_has_memory
= 1;
166 procfs_ops
.to_has_execution
= 1;
167 procfs_ops
.to_has_stack
= 1;
168 procfs_ops
.to_has_registers
= 1;
169 procfs_ops
.to_stratum
= process_stratum
;
170 procfs_ops
.to_has_thread_control
= tc_schedlock
;
171 procfs_ops
.to_magic
= OPS_MAGIC
;
174 /* =================== END, TARGET_OPS "MODULE" =================== */
179 * Put any typedefs, defines etc. here that are required for
180 * the unification of code that handles different versions of /proc.
183 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
185 enum { READ_WATCHFLAG
= WA_READ
,
186 WRITE_WATCHFLAG
= WA_WRITE
,
187 EXEC_WATCHFLAG
= WA_EXEC
,
188 AFTER_WATCHFLAG
= WA_TRAPAFTER
191 #else /* Irix method for watchpoints */
192 enum { READ_WATCHFLAG
= MA_READ
,
193 WRITE_WATCHFLAG
= MA_WRITE
,
194 EXEC_WATCHFLAG
= MA_EXEC
,
195 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
202 /* =================== STRUCT PROCINFO "MODULE" =================== */
204 /* FIXME: this comment will soon be out of date W.R.T. threads. */
206 /* The procinfo struct is a wrapper to hold all the state information
207 concerning a /proc process. There should be exactly one procinfo
208 for each process, and since GDB currently can debug only one
209 process at a time, that means there should be only one procinfo.
210 All of the LWP's of a process can be accessed indirectly thru the
211 single process procinfo.
213 However, against the day when GDB may debug more than one process,
214 this data structure is kept in a list (which for now will hold no
215 more than one member), and many functions will have a pointer to a
216 procinfo as an argument.
218 There will be a separate procinfo structure for use by the (not yet
219 implemented) "info proc" command, so that we can print useful
220 information about any random process without interfering with the
221 inferior's procinfo information. */
224 /* format strings for /proc paths */
225 # ifndef CTL_PROC_NAME_FMT
226 # define MAIN_PROC_NAME_FMT "/proc/%d"
227 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
228 # define AS_PROC_NAME_FMT "/proc/%d/as"
229 # define MAP_PROC_NAME_FMT "/proc/%d/map"
230 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
231 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
233 /* the name of the proc status struct depends on the implementation */
234 typedef pstatus_t gdb_prstatus_t
;
235 typedef lwpstatus_t gdb_lwpstatus_t
;
236 #else /* ! NEW_PROC_API */
237 /* format strings for /proc paths */
238 # ifndef CTL_PROC_NAME_FMT
239 # define MAIN_PROC_NAME_FMT "/proc/%05d"
240 # define CTL_PROC_NAME_FMT "/proc/%05d"
241 # define AS_PROC_NAME_FMT "/proc/%05d"
242 # define MAP_PROC_NAME_FMT "/proc/%05d"
243 # define STATUS_PROC_NAME_FMT "/proc/%05d"
244 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
246 /* the name of the proc status struct depends on the implementation */
247 typedef prstatus_t gdb_prstatus_t
;
248 typedef prstatus_t gdb_lwpstatus_t
;
249 #endif /* NEW_PROC_API */
251 /* Provide default composite pid manipulation macros for systems that
252 don't have threads. */
255 #define PIDGET(PID) (PID)
256 #define TIDGET(PID) (PID)
259 #define MERGEPID(PID, TID) (PID)
262 typedef struct procinfo
{
263 struct procinfo
*next
;
264 int pid
; /* Process ID */
265 int tid
; /* Thread/LWP id */
269 int ignore_next_sigstop
;
271 /* The following four fd fields may be identical, or may contain
272 several different fd's, depending on the version of /proc
273 (old ioctl or new read/write). */
275 int ctl_fd
; /* File descriptor for /proc control file */
277 * The next three file descriptors are actually only needed in the
278 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
279 * However, to avoid a bunch of #ifdefs in the code, we will use
280 * them uniformly by (in the case of the ioctl single-file-descriptor
281 * implementation) filling them with copies of the control fd.
283 int status_fd
; /* File descriptor for /proc status file */
284 int as_fd
; /* File descriptor for /proc as file */
286 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
288 fltset_t saved_fltset
; /* Saved traced hardware fault set */
289 sigset_t saved_sigset
; /* Saved traced signal set */
290 sigset_t saved_sighold
; /* Saved held signal set */
291 sysset_t saved_exitset
; /* Saved traced system call exit set */
292 sysset_t saved_entryset
; /* Saved traced system call entry set */
294 gdb_prstatus_t prstatus
; /* Current process status info */
297 gdb_fpregset_t fpregset
; /* Current floating point registers */
300 struct procinfo
*thread_list
;
302 int status_valid
: 1;
304 int fpregs_valid
: 1;
305 int threads_valid
: 1;
308 static char errmsg
[128]; /* shared error msg buffer */
310 /* Function prototypes for procinfo module: */
312 static procinfo
*find_procinfo_or_die
PARAMS ((int pid
, int tid
));
313 static procinfo
*find_procinfo
PARAMS ((int pid
, int tid
));
314 static procinfo
*create_procinfo
PARAMS ((int pid
, int tid
));
315 static void destroy_procinfo
PARAMS ((procinfo
*p
));
316 static void do_destroy_procinfo_cleanup (void *);
317 static void dead_procinfo
PARAMS ((procinfo
*p
,
318 char *msg
, int killp
));
319 static int open_procinfo_files
PARAMS ((procinfo
*p
, int which
));
320 static void close_procinfo_files
PARAMS ((procinfo
*p
));
322 /* The head of the procinfo list: */
323 static procinfo
* procinfo_list
;
326 * Function: find_procinfo
328 * Search the procinfo list.
330 * Returns: pointer to procinfo, or NULL if not found.
334 find_procinfo (pid
, tid
)
340 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
347 /* Don't check threads_valid. If we're updating the
348 thread_list, we want to find whatever threads are already
349 here. This means that in general it is the caller's
350 responsibility to check threads_valid and update before
351 calling find_procinfo, if the caller wants to find a new
354 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
363 * Function: find_procinfo_or_die
365 * Calls find_procinfo, but errors on failure.
369 find_procinfo_or_die (pid
, tid
)
373 procinfo
*pi
= find_procinfo (pid
, tid
);
378 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
381 error ("procfs: couldn't find pid %d in procinfo list.", pid
);
387 * Function: open_procinfo_files
389 * Open the file descriptor for the process or LWP.
390 * ifdef NEW_PROC_API, we only open the control file descriptor;
391 * the others are opened lazily as needed.
392 * else (if not NEW_PROC_API), there is only one real
393 * file descriptor, but we keep multiple copies of it so that
394 * the code that uses them does not have to be #ifdef'd.
396 * Return: file descriptor, or zero for failure.
399 enum { FD_CTL
, FD_STATUS
, FD_AS
};
402 open_procinfo_files (pi
, which
)
407 char tmp
[MAX_PROC_NAME_SIZE
];
412 * This function is getting ALMOST long enough to break up into several.
413 * Here is some rationale:
415 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
416 * There are several file descriptors that may need to be open
417 * for any given process or LWP. The ones we're intereted in are:
418 * - control (ctl) write-only change the state
419 * - status (status) read-only query the state
420 * - address space (as) read/write access memory
421 * - map (map) read-only virtual addr map
422 * Most of these are opened lazily as they are needed.
423 * The pathnames for the 'files' for an LWP look slightly
424 * different from those of a first-class process:
425 * Pathnames for a process (<proc-id>):
426 * /proc/<proc-id>/ctl
427 * /proc/<proc-id>/status
429 * /proc/<proc-id>/map
430 * Pathnames for an LWP (lwp-id):
431 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
432 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
433 * An LWP has no map or address space file descriptor, since
434 * the memory map and address space are shared by all LWPs.
436 * Everyone else (Solaris 2.5, Irix, OSF)
437 * There is only one file descriptor for each process or LWP.
438 * For convenience, we copy the same file descriptor into all
439 * three fields of the procinfo struct (ctl_fd, status_fd, and
440 * as_fd, see NEW_PROC_API above) so that code that uses them
441 * doesn't need any #ifdef's.
446 * Each LWP has an independent file descriptor, but these
447 * are not obtained via the 'open' system call like the rest:
448 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
449 * to the file descriptor of the parent process.
452 * These do not even have their own independent file descriptor.
453 * All operations are carried out on the file descriptor of the
454 * parent process. Therefore we just call open again for each
455 * thread, getting a new handle for the same 'file'.
460 * In this case, there are several different file descriptors that
461 * we might be asked to open. The control file descriptor will be
462 * opened early, but the others will be opened lazily as they are
466 strcpy (tmp
, pi
->pathname
);
467 switch (which
) { /* which file descriptor to open? */
470 strcat (tmp
, "/lwpctl");
472 strcat (tmp
, "/ctl");
473 fd
= open (tmp
, O_WRONLY
);
480 return 0; /* there is no 'as' file descriptor for an lwp */
482 fd
= open (tmp
, O_RDWR
);
489 strcat (tmp
, "/lwpstatus");
491 strcat (tmp
, "/status");
492 fd
= open (tmp
, O_RDONLY
);
498 return 0; /* unknown file descriptor */
500 #else /* not NEW_PROC_API */
502 * In this case, there is only one file descriptor for each procinfo
503 * (ie. each process or LWP). In fact, only the file descriptor for
504 * the process can actually be opened by an 'open' system call.
505 * The ones for the LWPs have to be obtained thru an IOCTL call
506 * on the process's file descriptor.
508 * For convenience, we copy each procinfo's single file descriptor
509 * into all of the fields occupied by the several file descriptors
510 * of the NEW_PROC_API implementation. That way, the code that uses
511 * them can be written without ifdefs.
515 #ifdef PIOCTSTATUS /* OSF */
516 if ((fd
= open (pi
->pathname
, O_RDWR
)) == 0) /* Only one FD; just open it. */
518 #else /* Sol 2.5, Irix, other? */
519 if (pi
->tid
== 0) /* Master procinfo for the process */
521 fd
= open (pi
->pathname
, O_RDWR
);
525 else /* LWP thread procinfo */
527 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
531 /* Find the procinfo for the entire process. */
532 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
535 /* Now obtain the file descriptor for the LWP. */
536 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) <= 0)
538 #else /* Irix, other? */
539 return 0; /* Don't know how to open threads */
540 #endif /* Sol 2.5 PIOCOPENLWP */
542 #endif /* OSF PIOCTSTATUS */
543 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
544 #endif /* NEW_PROC_API */
546 return 1; /* success */
550 * Function: create_procinfo
552 * Allocate a data structure and link it into the procinfo list.
553 * (First tries to find a pre-existing one (FIXME: why?)
555 * Return: pointer to new procinfo struct.
559 create_procinfo (pid
, tid
)
563 procinfo
*pi
, *parent
;
565 if ((pi
= find_procinfo (pid
, tid
)))
566 return pi
; /* Already exists, nothing to do. */
568 /* find parent before doing malloc, to save having to cleanup */
570 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
572 doesn't exist yet? */
574 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
575 memset (pi
, 0, sizeof (procinfo
));
579 /* Chain into list. */
582 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
583 pi
->next
= procinfo_list
;
589 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
591 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
593 pi
->next
= parent
->thread_list
;
594 parent
->thread_list
= pi
;
600 * Function: close_procinfo_files
602 * Close all file descriptors associated with the procinfo
606 close_procinfo_files (pi
)
614 if (pi
->status_fd
> 0)
615 close (pi
->status_fd
);
617 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
621 * Function: destroy_procinfo
623 * Destructor function. Close, unlink and deallocate the object.
627 destroy_one_procinfo (list
, pi
)
633 /* Step one: unlink the procinfo from its list */
637 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
640 ptr
->next
= pi
->next
;
644 /* Step two: close any open file descriptors */
645 close_procinfo_files (pi
);
647 /* Step three: free the memory. */
652 destroy_procinfo (pi
)
657 if (pi
->tid
!= 0) /* destroy a thread procinfo */
659 tmp
= find_procinfo (pi
->pid
, 0); /* find the parent process */
660 destroy_one_procinfo (&tmp
->thread_list
, pi
);
662 else /* destroy a process procinfo and all its threads */
664 /* First destroy the children, if any; */
665 while (pi
->thread_list
!= NULL
)
666 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
667 /* Then destroy the parent. Genocide!!! */
668 destroy_one_procinfo (&procinfo_list
, pi
);
673 do_destroy_procinfo_cleanup (void *pi
)
675 destroy_procinfo (pi
);
678 enum { NOKILL
, KILL
};
681 * Function: dead_procinfo
683 * To be called on a non_recoverable error for a procinfo.
684 * Prints error messages, optionally sends a SIGKILL to the process,
685 * then destroys the data structure.
689 dead_procinfo (pi
, msg
, kill_p
)
698 print_sys_errmsg (pi
->pathname
, errno
);
702 sprintf (procfile
, "process %d", pi
->pid
);
703 print_sys_errmsg (procfile
, errno
);
706 kill (pi
->pid
, SIGKILL
);
708 destroy_procinfo (pi
);
712 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
714 /* =================== /proc "MODULE" =================== */
717 * This "module" is the interface layer between the /proc system API
718 * and the gdb target vector functions. This layer consists of
719 * access functions that encapsulate each of the basic operations
720 * that we need to use from the /proc API.
722 * The main motivation for this layer is to hide the fact that
723 * there are two very different implementations of the /proc API.
724 * Rather than have a bunch of #ifdefs all thru the gdb target vector
725 * functions, we do our best to hide them all in here.
728 int proc_get_status
PARAMS ((procinfo
*pi
));
729 long proc_flags
PARAMS ((procinfo
*pi
));
730 int proc_why
PARAMS ((procinfo
*pi
));
731 int proc_what
PARAMS ((procinfo
*pi
));
732 int proc_set_run_on_last_close
PARAMS ((procinfo
*pi
));
733 int proc_unset_run_on_last_close
PARAMS ((procinfo
*pi
));
734 int proc_set_inherit_on_fork
PARAMS ((procinfo
*pi
));
735 int proc_unset_inherit_on_fork
PARAMS ((procinfo
*pi
));
736 int proc_set_async
PARAMS ((procinfo
*pi
));
737 int proc_unset_async
PARAMS ((procinfo
*pi
));
738 int proc_stop_process
PARAMS ((procinfo
*pi
));
739 int proc_trace_signal
PARAMS ((procinfo
*pi
, int signo
));
740 int proc_ignore_signal
PARAMS ((procinfo
*pi
, int signo
));
741 int proc_clear_current_fault
PARAMS ((procinfo
*pi
));
742 int proc_set_current_signal
PARAMS ((procinfo
*pi
, int signo
));
743 int proc_clear_current_signal
PARAMS ((procinfo
*pi
));
744 int proc_set_gregs
PARAMS ((procinfo
*pi
));
745 int proc_set_fpregs
PARAMS ((procinfo
*pi
));
746 int proc_wait_for_stop
PARAMS ((procinfo
*pi
));
747 int proc_run_process
PARAMS ((procinfo
*pi
, int step
, int signo
));
748 int proc_kill
PARAMS ((procinfo
*pi
, int signo
));
749 int proc_parent_pid
PARAMS ((procinfo
*pi
));
750 int proc_get_nthreads
PARAMS ((procinfo
*pi
));
751 int proc_get_current_thread
PARAMS ((procinfo
*pi
));
752 int proc_set_held_signals
PARAMS ((procinfo
*pi
, sigset_t
*sighold
));
753 int proc_set_traced_sysexit
PARAMS ((procinfo
*pi
, sysset_t
*sysset
));
754 int proc_set_traced_sysentry
PARAMS ((procinfo
*pi
, sysset_t
*sysset
));
755 int proc_set_traced_faults
PARAMS ((procinfo
*pi
, fltset_t
*fltset
));
756 int proc_set_traced_signals
PARAMS ((procinfo
*pi
, sigset_t
*sigset
));
758 int proc_update_threads
PARAMS ((procinfo
*pi
));
759 int proc_iterate_over_threads
PARAMS ((procinfo
*pi
,
760 int (*func
) PARAMS ((procinfo
*,
765 gdb_gregset_t
*proc_get_gregs
PARAMS ((procinfo
*pi
));
766 gdb_fpregset_t
*proc_get_fpregs
PARAMS ((procinfo
*pi
));
767 sysset_t
*proc_get_traced_sysexit
PARAMS ((procinfo
*pi
, sysset_t
*save
));
768 sysset_t
*proc_get_traced_sysentry
PARAMS ((procinfo
*pi
, sysset_t
*save
));
769 fltset_t
*proc_get_traced_faults
PARAMS ((procinfo
*pi
, fltset_t
*save
));
770 sigset_t
*proc_get_traced_signals
PARAMS ((procinfo
*pi
, sigset_t
*save
));
771 sigset_t
*proc_get_held_signals
PARAMS ((procinfo
*pi
, sigset_t
*save
));
772 sigset_t
*proc_get_pending_signals
PARAMS ((procinfo
*pi
, sigset_t
*save
));
773 struct sigaction
*proc_get_signal_actions
PARAMS ((procinfo
*pi
,
774 struct sigaction
*save
));
776 void proc_warn
PARAMS ((procinfo
*pi
, char *func
, int line
));
777 void proc_error
PARAMS ((procinfo
*pi
, char *func
, int line
));
780 proc_warn (pi
, func
, line
)
785 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
786 print_sys_errmsg (errmsg
, errno
);
790 proc_error (pi
, func
, line
)
795 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
796 perror_with_name (errmsg
);
800 * Function: proc_get_status
802 * Updates the status struct in the procinfo.
803 * There is a 'valid' flag, to let other functions know when
804 * this function needs to be called (so the status is only
805 * read when it is needed). The status file descriptor is
806 * also only opened when it is needed.
808 * Return: non-zero for success, zero for failure.
815 /* Status file descriptor is opened "lazily" */
816 if (pi
->status_fd
== 0 &&
817 open_procinfo_files (pi
, FD_STATUS
) == 0)
819 pi
->status_valid
= 0;
824 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
825 pi
->status_valid
= 0; /* fail */
828 /* Sigh... I have to read a different data structure,
829 depending on whether this is a main process or an LWP. */
831 pi
->status_valid
= (read (pi
->status_fd
,
832 (char *) &pi
->prstatus
.pr_lwp
,
833 sizeof (lwpstatus_t
))
834 == sizeof (lwpstatus_t
));
837 pi
->status_valid
= (read (pi
->status_fd
,
838 (char *) &pi
->prstatus
,
839 sizeof (gdb_prstatus_t
))
840 == sizeof (gdb_prstatus_t
));
841 #if 0 /*def UNIXWARE*/
842 if (pi
->status_valid
&&
843 (pi
->prstatus
.pr_lwp
.pr_flags
& PR_ISTOP
) &&
844 pi
->prstatus
.pr_lwp
.pr_why
== PR_REQUESTED
)
845 /* Unixware peculiarity -- read the damn thing again! */
846 pi
->status_valid
= (read (pi
->status_fd
,
847 (char *) &pi
->prstatus
,
848 sizeof (gdb_prstatus_t
))
849 == sizeof (gdb_prstatus_t
));
850 #endif /* UNIXWARE */
853 #else /* ioctl method */
854 #ifdef PIOCTSTATUS /* osf */
855 if (pi
->tid
== 0) /* main process */
857 /* Just read the danged status. Now isn't that simple? */
859 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
866 tid_t pr_error_thread
;
867 struct prstatus status
;
870 thread_status
.pr_count
= 1;
871 thread_status
.status
.pr_tid
= pi
->tid
;
872 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
875 memcpy (&pi
->prstatus
, &thread_status
.status
,
876 sizeof (pi
->prstatus
));
877 pi
->status_valid
= 1;
881 /* Just read the danged status. Now isn't that simple? */
882 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
886 if (pi
->status_valid
)
888 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
891 proc_get_current_thread (pi
));
894 /* The status struct includes general regs, so mark them valid too */
895 pi
->gregs_valid
= pi
->status_valid
;
897 /* In the read/write multiple-fd model,
898 the status struct includes the fp regs too, so mark them valid too */
899 pi
->fpregs_valid
= pi
->status_valid
;
901 return pi
->status_valid
; /* True if success, false if failure. */
905 * Function: proc_flags
907 * returns the process flags (pr_flags field).
914 if (!pi
->status_valid
)
915 if (!proc_get_status (pi
))
916 return 0; /* FIXME: not a good failure value (but what is?) */
920 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
921 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
922 The two sets of flags don't overlap. */
923 return pi
->prstatus
.pr_flags
| pi
->prstatus
.pr_lwp
.pr_flags
;
925 return pi
->prstatus
.pr_lwp
.pr_flags
;
928 return pi
->prstatus
.pr_flags
;
935 * returns the pr_why field (why the process stopped).
942 if (!pi
->status_valid
)
943 if (!proc_get_status (pi
))
944 return 0; /* FIXME: not a good failure value (but what is?) */
947 return pi
->prstatus
.pr_lwp
.pr_why
;
949 return pi
->prstatus
.pr_why
;
954 * Function: proc_what
956 * returns the pr_what field (details of why the process stopped).
963 if (!pi
->status_valid
)
964 if (!proc_get_status (pi
))
965 return 0; /* FIXME: not a good failure value (but what is?) */
968 return pi
->prstatus
.pr_lwp
.pr_what
;
970 return pi
->prstatus
.pr_what
;
974 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
976 * Function: proc_nsysarg
978 * returns the pr_nsysarg field (number of args to the current syscall).
985 if (!pi
->status_valid
)
986 if (!proc_get_status (pi
))
990 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
992 return pi
->prstatus
.pr_nsysarg
;
997 * Function: proc_sysargs
999 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1006 if (!pi
->status_valid
)
1007 if (!proc_get_status (pi
))
1011 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1013 return (long *) &pi
->prstatus
.pr_sysarg
;
1018 * Function: proc_syscall
1020 * returns the pr_syscall field (id of current syscall if we are in one).
1027 if (!pi
->status_valid
)
1028 if (!proc_get_status (pi
))
1032 return pi
->prstatus
.pr_lwp
.pr_syscall
;
1034 return pi
->prstatus
.pr_syscall
;
1037 #endif /* PIOCSSPCACT */
1040 * Function: proc_cursig:
1042 * returns the pr_cursig field (current signal).
1046 proc_cursig (struct procinfo
*pi
)
1048 if (!pi
->status_valid
)
1049 if (!proc_get_status (pi
))
1050 return 0; /* FIXME: not a good failure value (but what is?) */
1053 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1055 return pi
->prstatus
.pr_cursig
;
1060 * Function: proc_modify_flag
1062 * === I appologize for the messiness of this function.
1063 * === This is an area where the different versions of
1064 * === /proc are more inconsistent than usual. MVS
1066 * Set or reset any of the following process flags:
1067 * PR_FORK -- forked child will inherit trace flags
1068 * PR_RLC -- traced process runs when last /proc file closed.
1069 * PR_KLC -- traced process is killed when last /proc file closed.
1070 * PR_ASYNC -- LWP's get to run/stop independently.
1072 * There are three methods for doing this function:
1073 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1075 * 2) Middle: PIOCSET/PIOCRESET
1077 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1080 * Note: Irix does not define PR_ASYNC.
1081 * Note: OSF does not define PR_KLC.
1082 * Note: OSF is the only one that can ONLY use the oldest method.
1085 * pi -- the procinfo
1086 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1087 * mode -- 1 for set, 0 for reset.
1089 * Returns non-zero for success, zero for failure.
1092 enum { FLAG_RESET
, FLAG_SET
};
1095 proc_modify_flag (pi
, flag
, mode
)
1100 long win
= 0; /* default to fail */
1103 * These operations affect the process as a whole, and applying
1104 * them to an individual LWP has the same meaning as applying them
1105 * to the main process. Therefore, if we're ever called with a
1106 * pointer to an LWP's procinfo, let's substitute the process's
1107 * procinfo and avoid opening the LWP's file descriptor
1112 pi
= find_procinfo_or_die (pi
->pid
, 0);
1114 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1115 /* First normalize the PCUNSET/PCRESET command opcode
1116 (which for no obvious reason has a different definition
1117 from one operating system to the next...) */
1119 #define GDBRESET PCUNSET
1122 #define GDBRESET PCRESET
1127 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC) */
1129 else /* Reset the flag */
1133 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1136 #ifdef PIOCSET /* Irix/Sol5 method */
1137 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1139 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1141 else /* Reset the flag */
1143 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1147 #ifdef PIOCSRLC /* Oldest method: OSF */
1150 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1152 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1154 else /* Clear run-on-last-close */
1156 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1160 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1162 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1164 else /* Clear inherit-on-fork */
1166 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1170 win
= 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1177 /* The above operation renders the procinfo's cached pstatus obsolete. */
1178 pi
->status_valid
= 0;
1181 warning ("procfs: modify_flag failed to turn %s %s",
1182 flag
== PR_FORK
? "PR_FORK" :
1183 flag
== PR_RLC
? "PR_RLC" :
1185 flag
== PR_ASYNC
? "PR_ASYNC" :
1188 flag
== PR_KLC
? "PR_KLC" :
1191 mode
== FLAG_RESET
? "off" : "on");
1197 * Function: proc_set_run_on_last_close
1199 * Set the run_on_last_close flag.
1200 * Process with all threads will become runnable
1201 * when debugger closes all /proc fds.
1203 * Returns non-zero for success, zero for failure.
1207 proc_set_run_on_last_close (pi
)
1210 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1214 * Function: proc_unset_run_on_last_close
1216 * Reset the run_on_last_close flag.
1217 * Process will NOT become runnable
1218 * when debugger closes its file handles.
1220 * Returns non-zero for success, zero for failure.
1224 proc_unset_run_on_last_close (pi
)
1227 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1232 * Function: proc_set_kill_on_last_close
1234 * Set the kill_on_last_close flag.
1235 * Process with all threads will be killed when debugger
1236 * closes all /proc fds (or debugger exits or dies).
1238 * Returns non-zero for success, zero for failure.
1242 proc_set_kill_on_last_close (pi
)
1245 return proc_modify_flag (pi
, PR_KLC
, FLAG_SET
);
1249 * Function: proc_unset_kill_on_last_close
1251 * Reset the kill_on_last_close flag.
1252 * Process will NOT be killed when debugger
1253 * closes its file handles (or exits or dies).
1255 * Returns non-zero for success, zero for failure.
1259 proc_unset_kill_on_last_close (pi
)
1262 return proc_modify_flag (pi
, PR_KLC
, FLAG_RESET
);
1267 * Function: proc_set_inherit_on_fork
1269 * Set inherit_on_fork flag.
1270 * If the process forks a child while we are registered for events
1271 * in the parent, then we will also recieve events from the child.
1273 * Returns non-zero for success, zero for failure.
1277 proc_set_inherit_on_fork (pi
)
1280 return proc_modify_flag (pi
, PR_FORK
, FLAG_SET
);
1284 * Function: proc_unset_inherit_on_fork
1286 * Reset inherit_on_fork flag.
1287 * If the process forks a child while we are registered for events
1288 * in the parent, then we will NOT recieve events from the child.
1290 * Returns non-zero for success, zero for failure.
1294 proc_unset_inherit_on_fork (pi
)
1297 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1302 * Function: proc_set_async
1304 * Set PR_ASYNC flag.
1305 * If one LWP stops because of a debug event (signal etc.),
1306 * the remaining LWPs will continue to run.
1308 * Returns non-zero for success, zero for failure.
1315 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1319 * Function: proc_unset_async
1321 * Reset PR_ASYNC flag.
1322 * If one LWP stops because of a debug event (signal etc.),
1323 * then all other LWPs will stop as well.
1325 * Returns non-zero for success, zero for failure.
1329 proc_unset_async (pi
)
1332 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1334 #endif /* PR_ASYNC */
1337 * Function: proc_stop_process
1339 * Request the process/LWP to stop. Does not wait.
1340 * Returns non-zero for success, zero for failure.
1344 proc_stop_process (pi
)
1350 * We might conceivably apply this operation to an LWP, and
1351 * the LWP's ctl file descriptor might not be open.
1354 if (pi
->ctl_fd
== 0 &&
1355 open_procinfo_files (pi
, FD_CTL
) == 0)
1361 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1362 #else /* ioctl method */
1363 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1364 /* Note: the call also reads the prstatus. */
1367 pi
->status_valid
= 1;
1368 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1371 proc_get_current_thread (pi
));
1380 * Function: proc_wait_for_stop
1382 * Wait for the process or LWP to stop (block until it does).
1383 * Returns non-zero for success, zero for failure.
1387 proc_wait_for_stop (pi
)
1393 * We should never have to apply this operation to any procinfo
1394 * except the one for the main process. If that ever changes
1395 * for any reason, then take out the following clause and
1396 * replace it with one that makes sure the ctl_fd is open.
1400 pi
= find_procinfo_or_die (pi
->pid
, 0);
1405 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1406 /* We been runnin' and we stopped -- need to update status. */
1407 pi
->status_valid
= 0;
1409 #else /* ioctl method */
1410 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1411 /* Above call also refreshes the prstatus. */
1414 pi
->status_valid
= 1;
1415 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1418 proc_get_current_thread (pi
));
1426 * Function: proc_run_process
1428 * Make the process or LWP runnable.
1429 * Options (not all are implemented):
1431 * - clear current fault
1432 * - clear current signal
1433 * - abort the current system call
1434 * - stop as soon as finished with system call
1435 * - (ioctl): set traced signal set
1436 * - (ioctl): set held signal set
1437 * - (ioctl): set traced fault set
1438 * - (ioctl): set start pc (vaddr)
1439 * Always clear the current fault.
1440 * Clear the current signal if 'signo' is zero.
1443 * pi the process or LWP to operate on.
1444 * step if true, set the process or LWP to trap after one instr.
1445 * signo if zero, clear the current signal if any.
1446 * if non-zero, set the current signal to this one.
1448 * Returns non-zero for success, zero for failure.
1452 proc_run_process (pi
, step
, signo
)
1461 * We will probably have to apply this operation to individual threads,
1462 * so make sure the control file descriptor is open.
1465 if (pi
->ctl_fd
== 0 &&
1466 open_procinfo_files (pi
, FD_CTL
) == 0)
1471 runflags
= PRCFAULT
; /* always clear current fault */
1476 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals */
1477 proc_set_current_signal (pi
, signo
);
1485 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1487 #else /* ioctl method */
1491 memset (&prrun
, 0, sizeof (prrun
));
1492 prrun
.pr_flags
= runflags
;
1493 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1501 * Function: proc_set_traced_signals
1503 * Register to trace signals in the process or LWP.
1504 * Returns non-zero for success, zero for failure.
1508 proc_set_traced_signals (pi
, sigset
)
1515 * We should never have to apply this operation to any procinfo
1516 * except the one for the main process. If that ever changes
1517 * for any reason, then take out the following clause and
1518 * replace it with one that makes sure the ctl_fd is open.
1522 pi
= find_procinfo_or_die (pi
->pid
, 0);
1528 /* Use char array to avoid alignment issues. */
1529 char sigset
[sizeof (sigset_t
)];
1533 memcpy (&arg
.sigset
, sigset
, sizeof (sigset_t
));
1535 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1537 #else /* ioctl method */
1538 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1540 /* The above operation renders the procinfo's cached pstatus obsolete. */
1541 pi
->status_valid
= 0;
1544 warning ("procfs: set_traced_signals failed");
1549 * Function: proc_set_traced_faults
1551 * Register to trace hardware faults in the process or LWP.
1552 * Returns non-zero for success, zero for failure.
1556 proc_set_traced_faults (pi
, fltset
)
1563 * We should never have to apply this operation to any procinfo
1564 * except the one for the main process. If that ever changes
1565 * for any reason, then take out the following clause and
1566 * replace it with one that makes sure the ctl_fd is open.
1570 pi
= find_procinfo_or_die (pi
->pid
, 0);
1576 /* Use char array to avoid alignment issues. */
1577 char fltset
[sizeof (fltset_t
)];
1581 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1583 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1585 #else /* ioctl method */
1586 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1588 /* The above operation renders the procinfo's cached pstatus obsolete. */
1589 pi
->status_valid
= 0;
1595 * Function: proc_set_traced_sysentry
1597 * Register to trace entry to system calls in the process or LWP.
1598 * Returns non-zero for success, zero for failure.
1602 proc_set_traced_sysentry (pi
, sysset
)
1609 * We should never have to apply this operation to any procinfo
1610 * except the one for the main process. If that ever changes
1611 * for any reason, then take out the following clause and
1612 * replace it with one that makes sure the ctl_fd is open.
1616 pi
= find_procinfo_or_die (pi
->pid
, 0);
1622 /* Use char array to avoid alignment issues. */
1623 char sysset
[sizeof (sysset_t
)];
1627 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1629 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1631 #else /* ioctl method */
1632 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1634 /* The above operation renders the procinfo's cached pstatus obsolete. */
1635 pi
->status_valid
= 0;
1641 * Function: proc_set_traced_sysexit
1643 * Register to trace exit from system calls in the process or LWP.
1644 * Returns non-zero for success, zero for failure.
1648 proc_set_traced_sysexit (pi
, sysset
)
1655 * We should never have to apply this operation to any procinfo
1656 * except the one for the main process. If that ever changes
1657 * for any reason, then take out the following clause and
1658 * replace it with one that makes sure the ctl_fd is open.
1662 pi
= find_procinfo_or_die (pi
->pid
, 0);
1668 /* Use char array to avoid alignment issues. */
1669 char sysset
[sizeof (sysset_t
)];
1673 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1675 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1677 #else /* ioctl method */
1678 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1680 /* The above operation renders the procinfo's cached pstatus obsolete. */
1681 pi
->status_valid
= 0;
1687 * Function: proc_set_held_signals
1689 * Specify the set of blocked / held signals in the process or LWP.
1690 * Returns non-zero for success, zero for failure.
1694 proc_set_held_signals (pi
, sighold
)
1701 * We should never have to apply this operation to any procinfo
1702 * except the one for the main process. If that ever changes
1703 * for any reason, then take out the following clause and
1704 * replace it with one that makes sure the ctl_fd is open.
1708 pi
= find_procinfo_or_die (pi
->pid
, 0);
1714 /* Use char array to avoid alignment issues. */
1715 char hold
[sizeof (sigset_t
)];
1719 memcpy (&arg
.hold
, sighold
, sizeof (sigset_t
));
1720 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1723 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1725 /* The above operation renders the procinfo's cached pstatus obsolete. */
1726 pi
->status_valid
= 0;
1732 * Function: proc_get_pending_signals
1734 * returns the set of signals that are pending in the process or LWP.
1735 * Will also copy the sigset if 'save' is non-zero.
1739 proc_get_pending_signals (pi
, save
)
1743 sigset_t
*ret
= NULL
;
1746 * We should never have to apply this operation to any procinfo
1747 * except the one for the main process. If that ever changes
1748 * for any reason, then take out the following clause and
1749 * replace it with one that makes sure the ctl_fd is open.
1753 pi
= find_procinfo_or_die (pi
->pid
, 0);
1755 if (!pi
->status_valid
)
1756 if (!proc_get_status (pi
))
1760 ret
= &pi
->prstatus
.pr_lwp
.pr_lwppend
;
1762 ret
= &pi
->prstatus
.pr_sigpend
;
1765 memcpy (save
, ret
, sizeof (sigset_t
));
1771 * Function: proc_get_signal_actions
1773 * returns the set of signal actions.
1774 * Will also copy the sigactionset if 'save' is non-zero.
1778 proc_get_signal_actions (pi
, save
)
1780 struct sigaction
*save
;
1782 struct sigaction
*ret
= NULL
;
1785 * We should never have to apply this operation to any procinfo
1786 * except the one for the main process. If that ever changes
1787 * for any reason, then take out the following clause and
1788 * replace it with one that makes sure the ctl_fd is open.
1792 pi
= find_procinfo_or_die (pi
->pid
, 0);
1794 if (!pi
->status_valid
)
1795 if (!proc_get_status (pi
))
1799 ret
= &pi
->prstatus
.pr_lwp
.pr_action
;
1801 ret
= &pi
->prstatus
.pr_action
;
1804 memcpy (save
, ret
, sizeof (struct sigaction
));
1810 * Function: proc_get_held_signals
1812 * returns the set of signals that are held / blocked.
1813 * Will also copy the sigset if 'save' is non-zero.
1817 proc_get_held_signals (pi
, save
)
1821 sigset_t
*ret
= NULL
;
1824 * We should never have to apply this operation to any procinfo
1825 * except the one for the main process. If that ever changes
1826 * for any reason, then take out the following clause and
1827 * replace it with one that makes sure the ctl_fd is open.
1831 pi
= find_procinfo_or_die (pi
->pid
, 0);
1834 if (!pi
->status_valid
)
1835 if (!proc_get_status (pi
))
1839 ret
= &pi
->prstatus
.pr_lwp
.pr_context
.uc_sigmask
;
1841 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1842 #endif /* UNIXWARE */
1843 #else /* not NEW_PROC_API */
1845 static sigset_t sigheld
;
1847 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
1850 #endif /* NEW_PROC_API */
1852 memcpy (save
, ret
, sizeof (sigset_t
));
1858 * Function: proc_get_traced_signals
1860 * returns the set of signals that are traced / debugged.
1861 * Will also copy the sigset if 'save' is non-zero.
1865 proc_get_traced_signals (pi
, save
)
1869 sigset_t
*ret
= NULL
;
1872 * We should never have to apply this operation to any procinfo
1873 * except the one for the main process. If that ever changes
1874 * for any reason, then take out the following clause and
1875 * replace it with one that makes sure the ctl_fd is open.
1879 pi
= find_procinfo_or_die (pi
->pid
, 0);
1882 if (!pi
->status_valid
)
1883 if (!proc_get_status (pi
))
1886 ret
= &pi
->prstatus
.pr_sigtrace
;
1889 static sigset_t sigtrace
;
1891 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
1896 memcpy (save
, ret
, sizeof (sigset_t
));
1902 * Function: proc_trace_signal
1904 * Add 'signo' to the set of signals that are traced.
1905 * Returns non-zero for success, zero for failure.
1909 proc_trace_signal (pi
, signo
)
1916 * We should never have to apply this operation to any procinfo
1917 * except the one for the main process. If that ever changes
1918 * for any reason, then take out the following clause and
1919 * replace it with one that makes sure the ctl_fd is open.
1923 pi
= find_procinfo_or_die (pi
->pid
, 0);
1927 if (proc_get_traced_signals (pi
, &temp
))
1929 praddset (&temp
, signo
);
1930 return proc_set_traced_signals (pi
, &temp
);
1934 return 0; /* failure */
1938 * Function: proc_ignore_signal
1940 * Remove 'signo' from the set of signals that are traced.
1941 * Returns non-zero for success, zero for failure.
1945 proc_ignore_signal (pi
, signo
)
1952 * We should never have to apply this operation to any procinfo
1953 * except the one for the main process. If that ever changes
1954 * for any reason, then take out the following clause and
1955 * replace it with one that makes sure the ctl_fd is open.
1959 pi
= find_procinfo_or_die (pi
->pid
, 0);
1963 if (proc_get_traced_signals (pi
, &temp
))
1965 prdelset (&temp
, signo
);
1966 return proc_set_traced_signals (pi
, &temp
);
1970 return 0; /* failure */
1974 * Function: proc_get_traced_faults
1976 * returns the set of hardware faults that are traced /debugged.
1977 * Will also copy the faultset if 'save' is non-zero.
1981 proc_get_traced_faults (pi
, save
)
1985 fltset_t
*ret
= NULL
;
1988 * We should never have to apply this operation to any procinfo
1989 * except the one for the main process. If that ever changes
1990 * for any reason, then take out the following clause and
1991 * replace it with one that makes sure the ctl_fd is open.
1995 pi
= find_procinfo_or_die (pi
->pid
, 0);
1998 if (!pi
->status_valid
)
1999 if (!proc_get_status (pi
))
2002 ret
= &pi
->prstatus
.pr_flttrace
;
2005 static fltset_t flttrace
;
2007 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
2012 memcpy (save
, ret
, sizeof (fltset_t
));
2018 * Function: proc_get_traced_sysentry
2020 * returns the set of syscalls that are traced /debugged on entry.
2021 * Will also copy the syscall set if 'save' is non-zero.
2025 proc_get_traced_sysentry (pi
, save
)
2029 sysset_t
*ret
= NULL
;
2032 * We should never have to apply this operation to any procinfo
2033 * except the one for the main process. If that ever changes
2034 * for any reason, then take out the following clause and
2035 * replace it with one that makes sure the ctl_fd is open.
2039 pi
= find_procinfo_or_die (pi
->pid
, 0);
2042 if (!pi
->status_valid
)
2043 if (!proc_get_status (pi
))
2046 ret
= &pi
->prstatus
.pr_sysentry
;
2049 static sysset_t sysentry
;
2051 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
2056 memcpy (save
, ret
, sizeof (sysset_t
));
2062 * Function: proc_get_traced_sysexit
2064 * returns the set of syscalls that are traced /debugged on exit.
2065 * Will also copy the syscall set if 'save' is non-zero.
2069 proc_get_traced_sysexit (pi
, save
)
2073 sysset_t
* ret
= NULL
;
2076 * We should never have to apply this operation to any procinfo
2077 * except the one for the main process. If that ever changes
2078 * for any reason, then take out the following clause and
2079 * replace it with one that makes sure the ctl_fd is open.
2083 pi
= find_procinfo_or_die (pi
->pid
, 0);
2086 if (!pi
->status_valid
)
2087 if (!proc_get_status (pi
))
2090 ret
= &pi
->prstatus
.pr_sysexit
;
2093 static sysset_t sysexit
;
2095 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2100 memcpy (save
, ret
, sizeof (sysset_t
));
2106 * Function: proc_clear_current_fault
2108 * The current fault (if any) is cleared; the associated signal
2109 * will not be sent to the process or LWP when it resumes.
2110 * Returns non-zero for success, zero for failure.
2114 proc_clear_current_fault (pi
)
2120 * We should never have to apply this operation to any procinfo
2121 * except the one for the main process. If that ever changes
2122 * for any reason, then take out the following clause and
2123 * replace it with one that makes sure the ctl_fd is open.
2127 pi
= find_procinfo_or_die (pi
->pid
, 0);
2131 long cmd
= PCCFAULT
;
2132 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2135 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2142 * Function: proc_set_current_signal
2144 * Set the "current signal" that will be delivered next to the process.
2145 * NOTE: semantics are different from those of KILL.
2146 * This signal will be delivered to the process or LWP
2147 * immediately when it is resumed (even if the signal is held/blocked);
2148 * it will NOT immediately cause another event of interest, and will NOT
2149 * first trap back to the debugger.
2151 * Returns non-zero for success, zero for failure.
2155 proc_set_current_signal (pi
, signo
)
2162 /* Use char array to avoid alignment issues. */
2163 char sinfo
[sizeof (struct siginfo
)];
2165 struct siginfo
*mysinfo
;
2168 * We should never have to apply this operation to any procinfo
2169 * except the one for the main process. If that ever changes
2170 * for any reason, then take out the following clause and
2171 * replace it with one that makes sure the ctl_fd is open.
2175 pi
= find_procinfo_or_die (pi
->pid
, 0);
2177 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2178 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2179 * receives a PIOCSSIG with a signal identical to the current signal,
2180 * it messes up the current signal. Work around the kernel bug.
2183 signo
== proc_cursig (pi
))
2184 return 1; /* I assume this is a success? */
2187 /* The pointer is just a type alias. */
2188 mysinfo
= (struct siginfo
*) &arg
.sinfo
;
2189 mysinfo
->si_signo
= signo
;
2190 mysinfo
->si_code
= 0;
2191 mysinfo
->si_pid
= getpid (); /* ?why? */
2192 mysinfo
->si_uid
= getuid (); /* ?why? */
2196 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2198 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2205 * Function: proc_clear_current_signal
2207 * The current signal (if any) is cleared, and
2208 * is not sent to the process or LWP when it resumes.
2209 * Returns non-zero for success, zero for failure.
2213 proc_clear_current_signal (pi
)
2219 * We should never have to apply this operation to any procinfo
2220 * except the one for the main process. If that ever changes
2221 * for any reason, then take out the following clause and
2222 * replace it with one that makes sure the ctl_fd is open.
2226 pi
= find_procinfo_or_die (pi
->pid
, 0);
2232 /* Use char array to avoid alignment issues. */
2233 char sinfo
[sizeof (struct siginfo
)];
2235 struct siginfo
*mysinfo
;
2238 /* The pointer is just a type alias. */
2239 mysinfo
= (struct siginfo
*) &arg
.sinfo
;
2240 mysinfo
->si_signo
= 0;
2241 mysinfo
->si_code
= 0;
2242 mysinfo
->si_errno
= 0;
2243 mysinfo
->si_pid
= getpid (); /* ?why? */
2244 mysinfo
->si_uid
= getuid (); /* ?why? */
2246 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2249 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2256 * Function: proc_get_gregs
2258 * Get the general registers for the process or LWP.
2259 * Returns non-zero for success, zero for failure.
2266 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2267 if (!proc_get_status (pi
))
2271 * OK, sorry about the ifdef's.
2272 * There's three cases instead of two, because
2273 * in this instance Unixware and Solaris/RW differ.
2277 #ifdef UNIXWARE /* ugh, a true architecture dependency */
2278 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.gregs
;
2279 #else /* not Unixware */
2280 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2281 #endif /* Unixware */
2282 #else /* not NEW_PROC_API */
2283 return &pi
->prstatus
.pr_reg
;
2284 #endif /* NEW_PROC_API */
2288 * Function: proc_get_fpregs
2290 * Get the floating point registers for the process or LWP.
2291 * Returns non-zero for success, zero for failure.
2295 proc_get_fpregs (pi
)
2299 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2300 if (!proc_get_status (pi
))
2303 #ifdef UNIXWARE /* a true architecture dependency */
2304 return &pi
->prstatus
.pr_lwp
.pr_context
.uc_mcontext
.fpregs
;
2306 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2307 #endif /* Unixware */
2309 #else /* not NEW_PROC_API */
2310 if (pi
->fpregs_valid
)
2311 return &pi
->fpregset
; /* already got 'em */
2314 if (pi
->ctl_fd
== 0 &&
2315 open_procinfo_files (pi
, FD_CTL
) == 0)
2324 tid_t pr_error_thread
;
2325 tfpregset_t thread_1
;
2328 thread_fpregs
.pr_count
= 1;
2329 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2332 ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2334 pi
->fpregs_valid
= 1;
2335 return &pi
->fpregset
; /* got 'em now! */
2337 else if (pi
->tid
!= 0 &&
2338 ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2340 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2341 sizeof (pi
->fpregset
));
2342 pi
->fpregs_valid
= 1;
2343 return &pi
->fpregset
; /* got 'em now! */
2350 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2352 pi
->fpregs_valid
= 1;
2353 return &pi
->fpregset
; /* got 'em now! */
2366 * Function: proc_set_gregs
2368 * Write the general registers back to the process or LWP.
2369 * Returns non-zero for success, zero for failure.
2376 gdb_gregset_t
*gregs
;
2379 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2380 return 0; /* get_regs has already warned */
2382 if (pi
->ctl_fd
== 0 &&
2383 open_procinfo_files (pi
, FD_CTL
) == 0)
2392 /* Use char array to avoid alignment issues. */
2393 char gregs
[sizeof (gdb_gregset_t
)];
2397 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2398 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2400 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2404 /* Policy: writing the regs invalidates our cache. */
2405 pi
->gregs_valid
= 0;
2410 * Function: proc_set_fpregs
2412 * Modify the floating point register set of the process or LWP.
2413 * Returns non-zero for success, zero for failure.
2417 proc_set_fpregs (pi
)
2420 gdb_fpregset_t
*fpregs
;
2423 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
2424 return 0; /* get_fpregs has already warned */
2426 if (pi
->ctl_fd
== 0 &&
2427 open_procinfo_files (pi
, FD_CTL
) == 0)
2436 /* Use char array to avoid alignment issues. */
2437 char fpregs
[sizeof (gdb_fpregset_t
)];
2441 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2442 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2446 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2451 tid_t pr_error_thread
;
2452 tfpregset_t thread_1
;
2455 thread_fpregs
.pr_count
= 1;
2456 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2457 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2459 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2462 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2463 #endif /* osf PIOCTSFPREG */
2464 #endif /* NEW_PROC_API */
2467 /* Policy: writing the regs invalidates our cache. */
2468 pi
->fpregs_valid
= 0;
2473 * Function: proc_kill
2475 * Send a signal to the proc or lwp with the semantics of "kill()".
2476 * Returns non-zero for success, zero for failure.
2480 proc_kill (pi
, signo
)
2487 * We might conceivably apply this operation to an LWP, and
2488 * the LWP's ctl file descriptor might not be open.
2491 if (pi
->ctl_fd
== 0 &&
2492 open_procinfo_files (pi
, FD_CTL
) == 0)
2503 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2504 #else /* ioctl method */
2505 /* FIXME: do I need the Alpha OSF fixups present in
2506 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2507 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2515 * Function: proc_parent_pid
2517 * Find the pid of the process that started this one.
2518 * Returns the parent process pid, or zero.
2522 proc_parent_pid (pi
)
2526 * We should never have to apply this operation to any procinfo
2527 * except the one for the main process. If that ever changes
2528 * for any reason, then take out the following clause and
2529 * replace it with one that makes sure the ctl_fd is open.
2533 pi
= find_procinfo_or_die (pi
->pid
, 0);
2535 if (!pi
->status_valid
)
2536 if (!proc_get_status (pi
))
2539 return pi
->prstatus
.pr_ppid
;
2544 * Function: proc_set_watchpoint
2549 proc_set_watchpoint (pi
, addr
, len
, wflags
)
2555 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2558 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2559 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2564 char watch
[sizeof (prwatch_t
)];
2568 pwatch
= (prwatch_t
*) &arg
.watch
;
2569 pwatch
->pr_vaddr
= addr
;
2570 pwatch
->pr_size
= len
;
2571 pwatch
->pr_wflags
= wflags
;
2572 #if defined(NEW_PROC_API) && defined (PCWATCH)
2574 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2576 #if defined (PIOCSWATCH)
2577 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, pwatch
) >= 0);
2579 return 0; /* Fail */
2587 * Function: proc_iterate_over_mappings
2589 * Given a pointer to a function, call that function once for every
2590 * mapped address space in the process. The callback function
2591 * receives an open file descriptor for the file corresponding to
2592 * that mapped address space (if there is one), and the base address
2593 * of the mapped space. Quit when the callback function returns a
2594 * nonzero value, or at teh end of the mappings.
2596 * Returns: the first non-zero return value of the callback function,
2600 /* FIXME: it's probably a waste to cache this FD.
2601 It doesn't get called that often... and if I open it
2602 every time, I don't need to lseek it. */
2604 proc_iterate_over_mappings (func
)
2605 int (*func
) PARAMS ((int, CORE_ADDR
));
2609 #ifndef NEW_PROC_API /* avoid compiler warning */
2614 char pathname
[MAX_PROC_NAME_SIZE
];
2619 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
2623 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
2624 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
2625 proc_error (pi
, "proc_iterate_over_mappings (open)", __LINE__
);
2627 /* Make sure it gets closed again. */
2628 make_cleanup_close (map_fd
);
2630 /* Allocate space for mapping (lifetime only for this function). */
2631 map
= alloca (sizeof (struct prmap
));
2633 /* Now read the mappings from the file,
2634 open a file descriptor for those that have a name,
2635 and call the callback function. */
2636 while (read (map_fd
,
2638 sizeof (struct prmap
)) == sizeof (struct prmap
))
2640 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
2642 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
2645 if (map
->pr_mapname
[0] == 0)
2647 fd
= -1; /* no map file */
2651 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
2652 /* Note: caller's responsibility to close this fd! */
2653 fd
= open (name
, O_RDONLY
);
2654 /* Note: we don't test the above call for failure;
2655 we just pass the FD on as given. Sometimes there is
2656 no file, so the ioctl may return failure, but that's
2660 /* Stop looping if the callback returns non-zero. */
2661 if ((funcstat
= (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
)) != 0)
2665 /* Get the number of mapping entries. */
2666 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmaps
) < 0)
2667 proc_error (pi
, "proc_iterate_over_mappings (PIOCNMAP)", __LINE__
);
2669 /* Allocate space for mappings (lifetime only this function). */
2670 map
= (struct prmap
*) alloca ((nmaps
+ 1) * sizeof (struct prmap
));
2672 /* Read in all the mappings. */
2673 if (ioctl (pi
->ctl_fd
, PIOCMAP
, map
) < 0)
2674 proc_error (pi
, "proc_iterate_over_mappings (PIOCMAP)", __LINE__
);
2676 /* Now loop through the mappings, open an fd for each, and
2677 call the callback function. */
2679 i
< nmaps
&& map
[i
].pr_size
!= 0;
2682 /* Note: caller's responsibility to close this fd! */
2683 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
[i
].pr_vaddr
);
2684 /* Note: we don't test the above call for failure;
2685 we just pass the FD on as given. Sometimes there is
2686 no file, so the ioctl may return failure, but that's
2689 /* Stop looping if the callback returns non-zero. */
2690 if ((funcstat
= (*func
) (fd
, (CORE_ADDR
) map
[i
].pr_vaddr
)) != 0)
2698 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2700 #include <sys/sysi86.h>
2703 * Function: proc_get_LDT_entry
2709 * The 'key' is actually the value of the lower 16 bits of
2710 * the GS register for the LWP that we're interested in.
2712 * Return: matching ssh struct (LDT entry).
2716 proc_get_LDT_entry (pi
, key
)
2720 static struct ssd
*ldt_entry
= NULL
;
2722 char pathname
[MAX_PROC_NAME_SIZE
];
2723 struct cleanup
*old_chain
= NULL
;
2726 /* Allocate space for one LDT entry.
2727 This alloc must persist, because we return a pointer to it. */
2728 if (ldt_entry
== NULL
)
2729 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2731 /* Open the file descriptor for the LDT table. */
2732 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2733 if ((fd
= open (pathname
, O_RDONLY
)) < 0)
2735 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2738 /* Make sure it gets closed again! */
2739 old_chain
= make_cleanup_close (fd
);
2741 /* Now 'read' thru the table, find a match and return it. */
2742 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2744 if (ldt_entry
->sel
== 0 &&
2745 ldt_entry
->bo
== 0 &&
2746 ldt_entry
->acc1
== 0 &&
2747 ldt_entry
->acc2
== 0)
2748 break; /* end of table */
2749 /* If key matches, return this entry. */
2750 if (ldt_entry
->sel
== key
)
2753 /* Loop ended, match not found. */
2757 static int nalloc
= 0;
2759 /* Get the number of LDT entries. */
2760 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2762 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2766 /* Allocate space for the number of LDT entries. */
2767 /* This alloc has to persist, 'cause we return a pointer to it. */
2770 ldt_entry
= (struct ssd
*)
2771 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2775 /* Read the whole table in one gulp. */
2776 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2778 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2782 /* Search the table and return the (first) entry matching 'key'. */
2783 for (i
= 0; i
< nldt
; i
++)
2784 if (ldt_entry
[i
].sel
== key
)
2785 return &ldt_entry
[i
];
2787 /* Loop ended, match not found. */
2792 #endif /* TM_I386SOL2_H */
2794 /* =============== END, non-thread part of /proc "MODULE" =============== */
2796 /* =================== Thread "MODULE" =================== */
2798 /* NOTE: you'll see more ifdefs and duplication of functions here,
2799 since there is a different way to do threads on every OS. */
2802 * Function: proc_get_nthreads
2804 * Return the number of threads for the process
2807 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2812 proc_get_nthreads (pi
)
2817 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
2818 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
2824 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2826 * Solaris and Unixware version
2829 proc_get_nthreads (pi
)
2832 if (!pi
->status_valid
)
2833 if (!proc_get_status (pi
))
2837 * NEW_PROC_API: only works for the process procinfo,
2838 * because the LWP procinfos do not get prstatus filled in.
2841 if (pi
->tid
!= 0) /* find the parent process procinfo */
2842 pi
= find_procinfo_or_die (pi
->pid
, 0);
2844 return pi
->prstatus
.pr_nlwp
;
2852 proc_get_nthreads (pi
)
2861 * Function: proc_get_current_thread (LWP version)
2863 * Return the ID of the thread that had an event of interest.
2864 * (ie. the one that hit a breakpoint or other traced event).
2865 * All other things being equal, this should be the ID of a
2866 * thread that is currently executing.
2869 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2871 * Solaris and Unixware version
2874 proc_get_current_thread (pi
)
2878 * Note: this should be applied to the root procinfo for the process,
2879 * not to the procinfo for an LWP. If applied to the procinfo for
2880 * an LWP, it will simply return that LWP's ID. In that case,
2881 * find the parent process procinfo.
2885 pi
= find_procinfo_or_die (pi
->pid
, 0);
2887 if (!pi
->status_valid
)
2888 if (!proc_get_status (pi
))
2892 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
2894 return pi
->prstatus
.pr_who
;
2899 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2904 proc_get_current_thread (pi
)
2907 #if 0 /* FIXME: not ready for prime time? */
2908 return pi
->prstatus
.pr_tid
;
2919 proc_get_current_thread (pi
)
2929 * Function: proc_update_threads
2931 * Discover the IDs of all the threads within the process, and
2932 * create a procinfo for each of them (chained to the parent).
2934 * This unfortunately requires a different method on every OS.
2936 * Return: non-zero for success, zero for failure.
2940 proc_delete_dead_threads (parent
, thread
, ignore
)
2945 if (thread
&& parent
) /* sanity */
2947 thread
->status_valid
= 0;
2948 if (!proc_get_status (thread
))
2949 destroy_one_procinfo (&parent
->thread_list
, thread
);
2951 return 0; /* keep iterating */
2954 #if defined (PIOCLSTATUS)
2956 * Solaris 2.5 (ioctl) version
2959 proc_update_threads (pi
)
2962 gdb_prstatus_t
*prstatus
;
2963 struct cleanup
*old_chain
= NULL
;
2968 * We should never have to apply this operation to any procinfo
2969 * except the one for the main process. If that ever changes
2970 * for any reason, then take out the following clause and
2971 * replace it with one that makes sure the ctl_fd is open.
2975 pi
= find_procinfo_or_die (pi
->pid
, 0);
2977 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2979 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
2980 return 1; /* Process is not multi-threaded; nothing to do. */
2982 if ((prstatus
= (gdb_prstatus_t
*)
2983 malloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1))) == 0)
2984 perror_with_name ("procfs: malloc failed in update_threads");
2986 old_chain
= make_cleanup (free
, prstatus
);
2987 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
2988 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
2990 /* Skip element zero, which represents the process as a whole. */
2991 for (i
= 1; i
< nlwp
+ 1; i
++)
2993 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
2994 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2996 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
2997 thread
->status_valid
= 1;
2999 pi
->threads_valid
= 1;
3000 do_cleanups (old_chain
);
3006 * Unixware and Solaris 6 (and later) version
3009 do_closedir_cleanup (void *dir
)
3015 proc_update_threads (pi
)
3018 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
3019 struct dirent
*direntry
;
3020 struct cleanup
*old_chain
= NULL
;
3026 * We should never have to apply this operation to any procinfo
3027 * except the one for the main process. If that ever changes
3028 * for any reason, then take out the following clause and
3029 * replace it with one that makes sure the ctl_fd is open.
3033 pi
= find_procinfo_or_die (pi
->pid
, 0);
3035 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3040 * Note: this brute-force method is the only way I know of
3041 * to accomplish this task on Unixware. This method will
3042 * also work on Solaris 2.6 and 2.7. There is a much simpler
3043 * and more elegant way to do this on Solaris, but the margins
3044 * of this manuscript are too small to write it here... ;-)
3047 strcpy (pathname
, pi
->pathname
);
3048 strcat (pathname
, "/lwp");
3049 if ((dirp
= opendir (pathname
)) == NULL
)
3050 proc_error (pi
, "update_threads, opendir", __LINE__
);
3052 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
3053 while ((direntry
= readdir (dirp
)) != NULL
)
3054 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
3056 lwpid
= atoi (&direntry
->d_name
[0]);
3057 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
3058 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3060 pi
->threads_valid
= 1;
3061 do_cleanups (old_chain
);
3070 proc_update_threads (pi
)
3077 * We should never have to apply this operation to any procinfo
3078 * except the one for the main process. If that ever changes
3079 * for any reason, then take out the following clause and
3080 * replace it with one that makes sure the ctl_fd is open.
3084 pi
= find_procinfo_or_die (pi
->pid
, 0);
3086 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
3088 nthreads
= proc_get_nthreads (pi
);
3090 return 0; /* nothing to do for 1 or fewer threads */
3092 if ((threads
= malloc (nthreads
* sizeof (tid_t
))) == NULL
)
3093 proc_error (pi
, "update_threads, malloc", __LINE__
);
3095 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
3096 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
3098 for (i
= 0; i
< nthreads
; i
++)
3100 if (!find_procinfo (pi
->pid
, threads
[i
]))
3101 if (!create_procinfo (pi
->pid
, threads
[i
]))
3102 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
3104 pi
->threads_valid
= 1;
3112 proc_update_threads (pi
)
3117 #endif /* OSF PIOCTLIST */
3118 #endif /* NEW_PROC_API */
3119 #endif /* SOL 2.5 PIOCLSTATUS */
3122 * Function: proc_iterate_over_threads
3125 * Given a pointer to a function, call that function once
3126 * for each lwp in the procinfo list, until the function
3127 * returns non-zero, in which event return the value
3128 * returned by the function.
3130 * Note: this function does NOT call update_threads.
3131 * If you want to discover new threads first, you must
3132 * call that function explicitly. This function just makes
3133 * a quick pass over the currently-known procinfos.
3136 * pi - parent process procinfo
3137 * func - per-thread function
3138 * ptr - opaque parameter for function.
3141 * First non-zero return value from the callee, or zero.
3145 proc_iterate_over_threads (pi
, func
, ptr
)
3147 int (*func
) PARAMS ((procinfo
*, procinfo
*, void *));
3150 procinfo
*thread
, *next
;
3154 * We should never have to apply this operation to any procinfo
3155 * except the one for the main process. If that ever changes
3156 * for any reason, then take out the following clause and
3157 * replace it with one that makes sure the ctl_fd is open.
3161 pi
= find_procinfo_or_die (pi
->pid
, 0);
3163 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
3165 next
= thread
->next
; /* in case thread is destroyed */
3166 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
3173 /* =================== END, Thread "MODULE" =================== */
3175 /* =================== END, /proc "MODULE" =================== */
3177 /* =================== GDB "MODULE" =================== */
3180 * Here are all of the gdb target vector functions and their friends.
3183 static int do_attach
PARAMS ((int pid
));
3184 static void do_detach
PARAMS ((int signo
));
3185 static int register_gdb_signals
PARAMS ((procinfo
*, sigset_t
*));
3188 * Function: procfs_debug_inferior
3190 * Sets up the inferior to be debugged.
3191 * Registers to trace signals, hardware faults, and syscalls.
3192 * Note: does not set RLC flag: caller may want to customize that.
3194 * Returns: zero for success (note! unlike most functions in this module)
3195 * On failure, returns the LINE NUMBER where it failed!
3199 procfs_debug_inferior (pi
)
3202 fltset_t traced_faults
;
3203 sigset_t traced_signals
;
3204 sysset_t traced_syscall_entries
;
3205 sysset_t traced_syscall_exits
;
3207 #ifdef PROCFS_DONT_TRACE_FAULTS
3208 /* On some systems (OSF), we don't trace hardware faults.
3209 Apparently it's enough that we catch them as signals.
3210 Wonder why we don't just do that in general? */
3211 premptyset (&traced_faults
); /* don't trace faults. */
3213 /* Register to trace hardware faults in the child. */
3214 prfillset (&traced_faults
); /* trace all faults... */
3215 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
3217 if (!proc_set_traced_faults (pi
, &traced_faults
))
3220 /* Register to trace selected signals in the child. */
3221 premptyset (&traced_signals
);
3222 if (!register_gdb_signals (pi
, &traced_signals
))
3225 /* Register to trace the 'exit' system call (on entry). */
3226 premptyset (&traced_syscall_entries
);
3227 praddset (&traced_syscall_entries
, SYS_exit
);
3229 praddset (&traced_syscall_entries
, SYS_lwpexit
); /* And _lwp_exit... */
3232 praddset (&traced_syscall_entries
, SYS_lwp_exit
);
3235 if (!proc_set_traced_sysentry (pi
, &traced_syscall_entries
))
3238 #ifdef PRFS_STOPEXEC /* defined on OSF */
3239 /* OSF method for tracing exec syscalls. Quoting:
3240 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3241 exits from exec system calls because of the user level loader. */
3242 /* FIXME: make nice and maybe move into an access function. */
3246 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
3249 prfs_flags
|= PRFS_STOPEXEC
;
3251 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
3254 #else /* not PRFS_STOPEXEC */
3255 /* Everyone else's (except OSF) method for tracing exec syscalls */
3257 Not all systems with /proc have all the exec* syscalls with the same
3258 names. On the SGI, for example, there is no SYS_exec, but there
3259 *is* a SYS_execv. So, we try to account for that. */
3261 premptyset (&traced_syscall_exits
);
3263 praddset (&traced_syscall_exits
, SYS_exec
);
3266 praddset (&traced_syscall_exits
, SYS_execve
);
3269 praddset (&traced_syscall_exits
, SYS_execv
);
3272 #ifdef SYS_lwpcreate
3273 praddset (&traced_syscall_exits
, SYS_lwpcreate
);
3274 praddset (&traced_syscall_exits
, SYS_lwpexit
);
3277 #ifdef SYS_lwp_create /* FIXME: once only, please */
3278 praddset (&traced_syscall_exits
, SYS_lwp_create
);
3279 praddset (&traced_syscall_exits
, SYS_lwp_exit
);
3283 if (!proc_set_traced_sysexit (pi
, &traced_syscall_exits
))
3286 #endif /* PRFS_STOPEXEC */
3291 procfs_attach (args
, from_tty
)
3299 error_no_arg ("process-id to attach");
3302 if (pid
== getpid ())
3303 error ("Attaching GDB to itself is not a good idea...");
3307 exec_file
= get_exec_file (0);
3310 printf_filtered ("Attaching to program `%s', %s\n",
3311 exec_file
, target_pid_to_str (pid
));
3313 printf_filtered ("Attaching to %s\n", target_pid_to_str (pid
));
3317 inferior_pid
= do_attach (pid
);
3318 push_target (&procfs_ops
);
3322 procfs_detach (args
, from_tty
)
3331 exec_file
= get_exec_file (0);
3334 printf_filtered ("Detaching from program: %s %s\n",
3335 exec_file
, target_pid_to_str (inferior_pid
));
3339 signo
= atoi (args
);
3343 unpush_target (&procfs_ops
); /* Pop out of handling an inferior */
3353 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
3354 perror ("procfs: out of memory in 'attach'");
3356 if (!open_procinfo_files (pi
, FD_CTL
))
3358 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3359 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3361 dead_procinfo (pi
, errmsg
, NOKILL
);
3364 /* Stop the process (if it isn't already stopped). */
3365 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3367 pi
->was_stopped
= 1;
3368 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3372 pi
->was_stopped
= 0;
3373 /* Set the process to run again when we close it. */
3374 if (!proc_set_run_on_last_close (pi
))
3375 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3377 /* Now stop the process. */
3378 if (!proc_stop_process (pi
))
3379 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3380 pi
->ignore_next_sigstop
= 1;
3382 /* Save some of the /proc state to be restored if we detach. */
3383 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3384 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3385 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3386 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3387 if (!proc_get_traced_sysentry (pi
, &pi
->saved_entryset
))
3388 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3390 if (!proc_get_traced_sysexit (pi
, &pi
->saved_exitset
))
3391 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3393 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3394 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3396 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3397 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3399 /* Let GDB know that the inferior was attached. */
3401 return MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3410 /* Find procinfo for the main process */
3411 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0); /* FIXME: threads */
3413 if (!proc_set_current_signal (pi
, signo
))
3414 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3416 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3417 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3419 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3420 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3422 if (!proc_set_traced_sysentry (pi
, &pi
->saved_entryset
))
3423 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3425 if (!proc_set_traced_sysexit (pi
, &pi
->saved_exitset
))
3426 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3428 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3429 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3431 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3432 if (signo
|| !(pi
->was_stopped
) ||
3433 query ("Was stopped when attached, make it runnable again? "))
3435 /* Clear any pending signal. */
3436 if (!proc_clear_current_fault (pi
))
3437 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3439 if (!proc_set_run_on_last_close (pi
))
3440 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3444 destroy_procinfo (pi
);
3450 * Since the /proc interface cannot give us individual registers,
3451 * we pay no attention to the (regno) argument, and just fetch them all.
3452 * This results in the possibility that we will do unnecessarily many
3453 * fetches, since we may be called repeatedly for individual registers.
3454 * So we cache the results, and mark the cache invalid when the process
3459 procfs_fetch_registers (regno
)
3462 gdb_fpregset_t
*fpregs
;
3463 gdb_gregset_t
*gregs
;
3468 pid
= PIDGET (inferior_pid
);
3469 tid
= TIDGET (inferior_pid
);
3471 /* First look up procinfo for the main process. */
3472 pi
= find_procinfo_or_die (pid
, 0);
3474 /* If the event thread is not the same as GDB's requested thread
3475 (ie. inferior_pid), then look up procinfo for the requested
3478 (tid
!= proc_get_current_thread (pi
)))
3479 pi
= find_procinfo_or_die (pid
, tid
);
3482 error ("procfs: fetch_registers failed to find procinfo for %s",
3483 target_pid_to_str (inferior_pid
));
3485 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3486 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3488 supply_gregset (gregs
);
3490 if (FP0_REGNUM
>= 0) /* need floating point? */
3492 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3493 regno
== PC_REGNUM
||
3494 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3495 regno
== FP_REGNUM
||
3497 return; /* not a floating point register */
3499 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3500 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3502 supply_fpregset (fpregs
);
3506 /* Get ready to modify the registers array. On machines which store
3507 individual registers, this doesn't need to do anything. On
3508 machines which store all the registers in one fell swoop, such as
3509 /proc, this makes sure that registers contains all the registers
3510 from the program being debugged. */
3513 procfs_prepare_to_store ()
3515 #ifdef CHILD_PREPARE_TO_STORE
3516 CHILD_PREPARE_TO_STORE ();
3523 * Since the /proc interface will not read individual registers,
3524 * we will cache these requests until the process is resumed, and
3525 * only then write them back to the inferior process.
3527 * FIXME: is that a really bad idea? Have to think about cases
3528 * where writing one register might affect the value of others, etc.
3532 procfs_store_registers (regno
)
3535 gdb_fpregset_t
*fpregs
;
3536 gdb_gregset_t
*gregs
;
3541 pid
= PIDGET (inferior_pid
);
3542 tid
= TIDGET (inferior_pid
);
3544 /* First find procinfo for main process */
3545 pi
= find_procinfo_or_die (pid
, 0);
3547 /* If current lwp for process is not the same as requested thread
3548 (ie. inferior_pid), then find procinfo for the requested thread. */
3551 (tid
!= proc_get_current_thread (pi
)))
3552 pi
= find_procinfo_or_die (pid
, tid
);
3555 error ("procfs: store_registers: failed to find procinfo for %s",
3556 target_pid_to_str (inferior_pid
));
3558 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
3559 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3561 fill_gregset (gregs
, regno
);
3562 if (!proc_set_gregs (pi
))
3563 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3565 if (FP0_REGNUM
>= 0) /* need floating point? */
3567 if ((regno
>= 0 && regno
< FP0_REGNUM
) ||
3568 regno
== PC_REGNUM
||
3569 (NPC_REGNUM
>= 0 && regno
== NPC_REGNUM
) ||
3570 regno
== FP_REGNUM
||
3572 return; /* not a floating point register */
3574 if ((fpregs
= proc_get_fpregs (pi
)) == NULL
)
3575 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3577 fill_fpregset (fpregs
, regno
);
3578 if (!proc_set_fpregs (pi
))
3579 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3584 * Function: target_wait
3586 * Retrieve the next stop event from the child process.
3587 * If child has not stopped yet, wait for it to stop.
3588 * Translate /proc eventcodes (or possibly wait eventcodes)
3589 * into gdb internal event codes.
3591 * Return: id of process (and possibly thread) that incurred the event.
3592 * event codes are returned thru a pointer parameter.
3596 procfs_wait (pid
, status
)
3598 struct target_waitstatus
*status
;
3600 /* First cut: loosely based on original version 2.1 */
3604 int why
, what
, flags
;
3613 /* Find procinfo for main process */
3614 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
3617 /* We must assume that the status is stale now... */
3618 pi
->status_valid
= 0;
3619 pi
->gregs_valid
= 0;
3620 pi
->fpregs_valid
= 0;
3622 #if 0 /* just try this out... */
3623 flags
= proc_flags (pi
);
3624 why
= proc_why (pi
);
3625 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3626 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3628 /* If child is not stopped, wait for it to stop. */
3629 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3630 !proc_wait_for_stop (pi
))
3632 /* wait_for_stop failed: has the child terminated? */
3633 if (errno
== ENOENT
)
3635 /* /proc file not found; presumably child has terminated. */
3636 retval
= wait (&wstat
); /* "wait" for the child's exit */
3638 if (retval
!= PIDGET (inferior_pid
)) /* wrong child? */
3639 error ("procfs: couldn't stop process %d: wait returned %d\n",
3640 inferior_pid
, retval
);
3641 /* FIXME: might I not just use waitpid?
3642 Or try find_procinfo to see if I know about this child? */
3646 /* Unknown error from wait_for_stop. */
3647 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3652 /* This long block is reached if either:
3653 a) the child was already stopped, or
3654 b) we successfully waited for the child with wait_for_stop.
3655 This block will analyze the /proc status, and translate it
3656 into a waitstatus for GDB.
3658 If we actually had to call wait because the /proc file
3659 is gone (child terminated), then we skip this block,
3660 because we already have a waitstatus. */
3662 flags
= proc_flags (pi
);
3663 why
= proc_why (pi
);
3664 what
= proc_what (pi
);
3666 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3669 /* If it's running async (for single_thread control),
3670 set it back to normal again. */
3671 if (flags
& PR_ASYNC
)
3672 if (!proc_unset_async (pi
))
3673 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3677 proc_prettyprint_why (why
, what
, 1);
3679 /* The 'pid' we will return to GDB is composed of
3680 the process ID plus the lwp ID. */
3681 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3685 wstat
= (what
<< 8) | 0177;
3695 #if defined (SYS_lwp_exit) || defined (SYS_lwpexit)
3696 printf_filtered ("[%s exited]\n",
3697 target_pid_to_str (retval
));
3698 delete_thread (retval
);
3699 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3701 #endif /* _lwp_exit */
3704 /* Handle SYS_exit call only */
3705 /* Stopped at entry to SYS_exit.
3706 Make it runnable, resume it, then use
3707 the wait system call to get its exit code.
3708 Proc_run_process always clears the current
3710 Then return its exit status. */
3711 pi
->status_valid
= 0;
3713 /* FIXME: what we should do is return
3714 TARGET_WAITKIND_SPURIOUS. */
3715 if (!proc_run_process (pi
, 0, 0))
3716 proc_error (pi
, "target_wait, run_process", __LINE__
);
3719 /* Don't call wait: simulate waiting for exit,
3720 return a "success" exit code. Bogus: what if
3721 it returns something else? */
3723 retval
= inferior_pid
; /* ? ? ? */
3727 int temp
= wait (&wstat
);
3729 /* FIXME: shouldn't I make sure I get the right
3730 event from the right process? If (for
3731 instance) I have killed an earlier inferior
3732 process but failed to clean up after it
3733 somehow, I could get its termination event
3736 /* If wait returns -1, that's what we return to GDB. */
3742 printf_filtered ("procfs: trapped on entry to ");
3743 proc_prettyprint_syscall (proc_what (pi
), 0);
3744 printf_filtered ("\n");
3747 long i
, nsysargs
, *sysargs
;
3749 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3750 (sysargs
= proc_sysargs (pi
)) != NULL
)
3752 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
3753 for (i
= 0; i
< nsysargs
; i
++)
3754 printf_filtered ("#%ld: 0x%08lx\n",
3762 /* How to exit gracefully, returning "unknown event" */
3763 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3764 return inferior_pid
;
3768 /* How to keep going without returning to wfi: */
3769 target_resume (pid
, 0, TARGET_SIGNAL_0
);
3786 /* Hopefully this is our own "fork-child" execing
3787 the real child. Hoax this event into a trap, and
3788 GDB will see the child about to execute its start
3790 wstat
= (SIGTRAP
<< 8) | 0177;
3792 #ifdef SYS_lwp_create
3793 case SYS_lwp_create
:
3795 #ifdef SYS_lwpcreate
3798 #if defined(SYS_lwp_create) || defined(SYS_lwpcreate)
3800 * This syscall is somewhat like fork/exec.
3801 * We will get the event twice: once for the parent LWP,
3802 * and once for the child. We should already know about
3803 * the parent LWP, but the child will be new to us. So,
3804 * whenever we get this event, if it represents a new
3805 * thread, simply add the thread to the list.
3808 /* If not in procinfo list, add it. */
3809 temp
= proc_get_current_thread (pi
);
3810 if (!find_procinfo (pi
->pid
, temp
))
3811 create_procinfo (pi
->pid
, temp
);
3813 temp
= MERGEPID (pi
->pid
, temp
);
3814 /* If not in GDB's thread list, add it. */
3815 if (!in_thread_list (temp
))
3817 printf_filtered ("[New %s]\n", target_pid_to_str (temp
));
3820 /* Return to WFI, but tell it to immediately resume. */
3821 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3822 return inferior_pid
;
3823 #endif /* _lwp_create */
3831 #if defined (SYS_lwp_exit) || defined (SYS_lwpexit)
3832 printf_filtered ("[%s exited]\n",
3833 target_pid_to_str (retval
));
3834 delete_thread (retval
);
3835 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3837 #endif /* _lwp_exit */
3841 /* Nothing to do here for now. The old procfs
3842 seemed to use this event to handle threads on
3843 older (non-LWP) systems, where I'm assuming that
3844 threads were actually separate processes. Irix,
3845 maybe? Anyway, low priority for now. */
3849 /* FIXME: do we need to handle this? Investigate. */
3853 /* FIXME: see above. */
3856 printf_filtered ("procfs: trapped on exit from ");
3857 proc_prettyprint_syscall (proc_what (pi
), 0);
3858 printf_filtered ("\n");
3861 long i
, nsysargs
, *sysargs
;
3863 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3864 (sysargs
= proc_sysargs (pi
)) != NULL
)
3866 printf_filtered ("%ld syscall arguments:\n", nsysargs
);
3867 for (i
= 0; i
< nsysargs
; i
++)
3868 printf_filtered ("#%ld: 0x%08lx\n",
3873 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3874 return inferior_pid
;
3879 wstat
= (SIGSTOP
<< 8) | 0177;
3884 printf_filtered ("Retry #%d:\n", retry
);
3885 pi
->status_valid
= 0;
3890 /* If not in procinfo list, add it. */
3891 temp
= proc_get_current_thread (pi
);
3892 if (!find_procinfo (pi
->pid
, temp
))
3893 create_procinfo (pi
->pid
, temp
);
3895 /* If not in GDB's thread list, add it. */
3896 temp
= MERGEPID (pi
->pid
, temp
);
3897 if (!in_thread_list (temp
))
3899 printf_filtered ("[New %s]\n",
3900 target_pid_to_str (temp
));
3904 status
->kind
= TARGET_WAITKIND_STOPPED
;
3905 status
->value
.sig
= 0;
3910 wstat
= (what
<< 8) | 0177;
3913 switch (what
) { /* FIXME: FAULTED_USE_SIGINFO */
3916 wstat
= (SIGTRAP
<< 8) | 0177;
3921 wstat
= (SIGTRAP
<< 8) | 0177;
3924 /* FIXME: use si_signo where possible. */
3926 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
3929 wstat
= (SIGILL
<< 8) | 0177;
3932 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
3935 wstat
= (SIGTRAP
<< 8) | 0177;
3939 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
3942 wstat
= (SIGSEGV
<< 8) | 0177;
3946 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
3949 wstat
= (SIGFPE
<< 8) | 0177;
3951 case FLTPAGE
: /* Recoverable page fault */
3952 default: /* FIXME: use si_signo if possible for fault */
3954 printf_filtered ("procfs:%d -- ", __LINE__
);
3955 printf_filtered ("child stopped for unknown reason:\n");
3956 proc_prettyprint_why (why
, what
, 1);
3957 error ("... giving up...");
3960 break; /* case PR_FAULTED: */
3961 default: /* switch (why) unmatched */
3962 printf_filtered ("procfs:%d -- ", __LINE__
);
3963 printf_filtered ("child stopped for unknown reason:\n");
3964 proc_prettyprint_why (why
, what
, 1);
3965 error ("... giving up...");
3969 * Got this far without error:
3970 * If retval isn't in the threads database, add it.
3973 retval
!= inferior_pid
&&
3974 !in_thread_list (retval
))
3977 * We have a new thread.
3978 * We need to add it both to GDB's list and to our own.
3979 * If we don't create a procinfo, resume may be unhappy
3982 printf_filtered ("[New %s]\n", target_pid_to_str (retval
));
3983 add_thread (retval
);
3984 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
3985 create_procinfo (PIDGET (retval
), TIDGET (retval
));
3987 /* In addition, it's possible that this is the first
3988 * new thread we've seen, in which case we may not
3989 * have created entries for inferior_pid yet.
3991 if (TIDGET (inferior_pid
) != 0)
3993 if (!in_thread_list (inferior_pid
))
3994 add_thread (inferior_pid
);
3995 if (find_procinfo (PIDGET (inferior_pid
),
3996 TIDGET (inferior_pid
)) == NULL
)
3997 create_procinfo (PIDGET (inferior_pid
),
3998 TIDGET (inferior_pid
));
4002 else /* flags do not indicate STOPPED */
4004 /* surely this can't happen... */
4005 printf_filtered ("procfs:%d -- process not stopped.\n",
4007 proc_prettyprint_flags (flags
, 1);
4008 error ("procfs: ...giving up...");
4013 store_waitstatus (status
, wstat
);
4020 procfs_xfer_memory (memaddr
, myaddr
, len
, dowrite
, target
)
4025 struct target_ops
*target
; /* ignored */
4030 /* Find procinfo for main process */
4031 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4032 if (pi
->as_fd
== 0 &&
4033 open_procinfo_files (pi
, FD_AS
) == 0)
4035 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4039 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4044 PROCFS_NOTE ("write memory: ");
4046 PROCFS_NOTE ("write memory: \n");
4048 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4052 PROCFS_NOTE ("read memory: \n");
4053 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4064 * Function: invalidate_cache
4066 * Called by target_resume before making child runnable.
4067 * Mark cached registers and status's invalid.
4068 * If there are "dirty" caches that need to be written back
4069 * to the child process, do that.
4071 * File descriptors are also cached.
4072 * As they are a limited resource, we cannot hold onto them indefinitely.
4073 * However, as they are expensive to open, we don't want to throw them
4074 * away indescriminately either. As a compromise, we will keep the
4075 * file descriptors for the parent process, but discard any file
4076 * descriptors we may have accumulated for the threads.
4079 * As this function is called by iterate_over_threads, it always
4080 * returns zero (so that iterate_over_threads will keep iterating).
4085 invalidate_cache (parent
, pi
, ptr
)
4091 * About to run the child; invalidate caches and do any other cleanup.
4095 if (pi
->gregs_dirty
)
4096 if (parent
== NULL
||
4097 proc_get_current_thread (parent
) != pi
->tid
)
4098 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4099 proc_warn (pi
, "target_resume, set_gregs",
4101 if (FP0_REGNUM
>= 0)
4102 if (pi
->fpregs_dirty
)
4103 if (parent
== NULL
||
4104 proc_get_current_thread (parent
) != pi
->tid
)
4105 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4106 proc_warn (pi
, "target_resume, set_fpregs",
4112 /* The presence of a parent indicates that this is an LWP.
4113 Close any file descriptors that it might have open.
4114 We don't do this to the master (parent) procinfo. */
4116 close_procinfo_files (pi
);
4118 pi
->gregs_valid
= 0;
4119 pi
->fpregs_valid
= 0;
4121 pi
->gregs_dirty
= 0;
4122 pi
->fpregs_dirty
= 0;
4124 pi
->status_valid
= 0;
4125 pi
->threads_valid
= 0;
4132 * Function: make_signal_thread_runnable
4134 * A callback function for iterate_over_threads.
4135 * Find the asynchronous signal thread, and make it runnable.
4136 * See if that helps matters any.
4140 make_signal_thread_runnable (process
, pi
, ptr
)
4146 if (proc_flags (pi
) & PR_ASLWP
)
4148 if (!proc_run_process (pi
, 0, -1))
4149 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4158 * Function: target_resume
4160 * Make the child process runnable. Normally we will then call
4161 * procfs_wait and wait for it to stop again (unles gdb is async).
4164 * step: if true, then arrange for the child to stop again
4165 * after executing a single instruction.
4166 * signo: if zero, then cancel any pending signal.
4167 * If non-zero, then arrange for the indicated signal
4168 * to be delivered to the child when it runs.
4169 * pid: if -1, then allow any child thread to run.
4170 * if non-zero, then allow only the indicated thread to run.
4171 ******* (not implemented yet)
4175 procfs_resume (pid
, step
, signo
)
4178 enum target_signal signo
;
4180 procinfo
*pi
, *thread
;
4184 prrun.prflags |= PRSVADDR;
4185 prrun.pr_vaddr = $PC; set resume address
4186 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4187 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4188 prrun.prflags |= PRCFAULT; clear current fault.
4190 PRSTRACE and PRSFAULT can be done by other means
4191 (proc_trace_signals, proc_trace_faults)
4192 PRSVADDR is unnecessary.
4193 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4194 This basically leaves PRSTEP and PRCSIG.
4195 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4196 So basically PR_STEP is the sole argument that must be passed
4197 to proc_run_process (for use in the prrun struct by ioctl). */
4199 /* Find procinfo for main process */
4200 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4202 /* First cut: ignore pid argument */
4205 /* Convert signal to host numbering. */
4207 (signo
== TARGET_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4210 native_signo
= target_signal_to_host (signo
);
4212 pi
->ignore_next_sigstop
= 0;
4214 /* Running the process voids all cached registers and status. */
4215 /* Void the threads' caches first */
4216 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4217 /* Void the process procinfo's caches. */
4218 invalidate_cache (NULL
, pi
, NULL
);
4222 /* Resume a specific thread, presumably suppressing the others. */
4223 thread
= find_procinfo (PIDGET (pid
), TIDGET (pid
));
4225 warning ("procfs: resume can't find thread %d -- resuming all.",
4229 if (thread
->tid
!= 0)
4231 /* We're to resume a specific thread, and not the others.
4232 * Set the child process's PR_ASYNC flag.
4235 if (!proc_set_async (pi
))
4236 proc_error (pi
, "target_resume, set_async", __LINE__
);
4239 proc_iterate_over_threads (pi
,
4240 make_signal_thread_runnable
,
4243 pi
= thread
; /* substitute the thread's procinfo for run */
4248 if (!proc_run_process (pi
, step
, native_signo
))
4251 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4253 proc_error (pi
, "target_resume", __LINE__
);
4258 * Function: register_gdb_signals
4260 * Traverse the list of signals that GDB knows about
4261 * (see "handle" command), and arrange for the target
4262 * to be stopped or not, according to these settings.
4264 * Returns non-zero for success, zero for failure.
4268 register_gdb_signals (pi
, signals
)
4274 for (signo
= 0; signo
< NSIG
; signo
++)
4275 if (signal_stop_state (target_signal_from_host (signo
)) == 0 &&
4276 signal_print_state (target_signal_from_host (signo
)) == 0 &&
4277 signal_pass_state (target_signal_from_host (signo
)) == 1)
4278 prdelset (signals
, signo
);
4280 praddset (signals
, signo
);
4282 return proc_set_traced_signals (pi
, signals
);
4286 * Function: target_notice_signals
4288 * Set up to trace signals in the child process.
4292 procfs_notice_signals (pid
)
4296 procinfo
*pi
= find_procinfo_or_die (PIDGET (pid
), 0);
4298 if (proc_get_traced_signals (pi
, &signals
) &&
4299 register_gdb_signals (pi
, &signals
))
4302 proc_error (pi
, "notice_signals", __LINE__
);
4306 * Function: target_files_info
4308 * Print status information about the child process.
4312 procfs_files_info (ignore
)
4313 struct target_ops
*ignore
;
4315 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4316 attach_flag
? "attached": "child",
4317 target_pid_to_str (inferior_pid
));
4321 * Function: target_open
4323 * A dummy: you don't open procfs.
4327 procfs_open (args
, from_tty
)
4331 error ("Use the \"run\" command to start a Unix child process.");
4335 * Function: target_can_run
4337 * This tells GDB that this target vector can be invoked
4338 * for "run" or "attach".
4341 int procfs_suppress_run
= 0; /* Non-zero if procfs should pretend not to
4342 be a runnable target. Used by targets
4343 that can sit atop procfs, such as solaris
4350 /* This variable is controlled by modules that sit atop procfs that
4351 may layer their own process structure atop that provided here.
4352 sol-thread.c does this because of the Solaris two-level thread
4355 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4357 return !procfs_suppress_run
;
4361 * Function: target_stop
4363 * Stop the child process asynchronously, as when the
4364 * gdb user types control-c or presses a "stop" button.
4366 * Works by sending kill(SIGINT) to the child's process group.
4372 extern pid_t inferior_process_group
;
4374 kill (-inferior_process_group
, SIGINT
);
4378 * Function: unconditionally_kill_inferior
4380 * Make it die. Wait for it to die. Clean up after it.
4381 * Note: this should only be applied to the real process,
4382 * not to an LWP, because of the check for parent-process.
4383 * If we need this to work for an LWP, it needs some more logic.
4387 unconditionally_kill_inferior (pi
)
4392 parent_pid
= proc_parent_pid (pi
);
4393 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4394 /* FIXME: use access functions */
4395 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4396 before the PIOCKILL, otherwise it might generate a corrupted core
4397 file for the inferior. */
4398 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4400 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4403 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4404 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4405 to kill the inferior, otherwise it might remain stopped with a
4407 We do not check the result of the PIOCSSIG, the inferior might have
4410 struct siginfo newsiginfo
;
4412 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4413 newsiginfo
.si_signo
= SIGKILL
;
4414 newsiginfo
.si_code
= 0;
4415 newsiginfo
.si_errno
= 0;
4416 newsiginfo
.si_pid
= getpid ();
4417 newsiginfo
.si_uid
= getuid ();
4418 /* FIXME: use proc_set_current_signal */
4419 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4421 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4422 if (!proc_kill (pi
, SIGKILL
))
4423 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4424 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4425 destroy_procinfo (pi
);
4427 /* If pi is GDB's child, wait for it to die. */
4428 if (parent_pid
== getpid ())
4429 /* FIXME: should we use waitpid to make sure we get the right event?
4430 Should we check the returned event? */
4435 ret
= waitpid (pi
->pid
, &status
, 0);
4443 * Function: target_kill_inferior
4445 * We're done debugging it, and we want it to go away.
4446 * Then we want GDB to forget all about it.
4450 procfs_kill_inferior ()
4452 if (inferior_pid
!= 0) /* ? */
4454 /* Find procinfo for main process */
4455 procinfo
*pi
= find_procinfo (PIDGET (inferior_pid
), 0);
4458 unconditionally_kill_inferior (pi
);
4459 target_mourn_inferior ();
4464 * Function: target_mourn_inferior
4466 * Forget we ever debugged this thing!
4470 procfs_mourn_inferior ()
4474 if (inferior_pid
!= 0)
4476 /* Find procinfo for main process */
4477 pi
= find_procinfo (PIDGET (inferior_pid
), 0);
4479 destroy_procinfo (pi
);
4481 unpush_target (&procfs_ops
);
4482 generic_mourn_inferior ();
4486 * Function: init_inferior
4488 * When GDB forks to create a runnable inferior process,
4489 * this function is called on the parent side of the fork.
4490 * It's job is to do whatever is necessary to make the child
4491 * ready to be debugged, and then wait for the child to synchronize.
4495 procfs_init_inferior (pid
)
4502 /* This routine called on the parent side (GDB side)
4503 after GDB forks the inferior. */
4505 push_target (&procfs_ops
);
4507 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4508 perror ("procfs: out of memory in 'init_inferior'");
4510 if (!open_procinfo_files (pi
, FD_CTL
))
4511 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4515 open_procinfo_files // done
4518 procfs_notice_signals
4525 /* If not stopped yet, wait for it to stop. */
4526 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4527 !(proc_wait_for_stop (pi
)))
4528 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4530 /* Save some of the /proc state to be restored if we detach. */
4531 /* FIXME: Why? In case another debugger was debugging it?
4532 We're it's parent, for Ghu's sake! */
4533 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4534 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4535 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4536 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4537 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4538 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4539 if (!proc_get_traced_sysentry (pi
, &pi
->saved_entryset
))
4540 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4541 if (!proc_get_traced_sysexit (pi
, &pi
->saved_exitset
))
4542 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4544 /* Register to trace selected signals in the child. */
4545 prfillset (&signals
);
4546 if (!register_gdb_signals (pi
, &signals
))
4547 proc_error (pi
, "init_inferior, register_signals", __LINE__
);
4549 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4550 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4552 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4553 and possibly even turning ON kill-on-last-close at this point. But
4554 I can't make that change without careful testing which I don't have
4555 time to do right now... */
4556 /* Turn on run-on-last-close flag so that the child
4557 will die if GDB goes away for some reason. */
4558 if (!proc_set_run_on_last_close (pi
))
4559 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4561 /* The 'process ID' we return to GDB is composed of
4562 the actual process ID plus the lwp ID. */
4563 inferior_pid
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
4565 #ifdef START_INFERIOR_TRAPS_EXPECTED
4566 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4568 /* One trap to exec the shell, one to exec the program being debugged. */
4569 startup_inferior (2);
4570 #endif /* START_INFERIOR_TRAPS_EXPECTED */
4574 * Function: set_exec_trap
4576 * When GDB forks to create a new process, this function is called
4577 * on the child side of the fork before GDB exec's the user program.
4578 * Its job is to make the child minimally debuggable, so that the
4579 * parent GDB process can connect to the child and take over.
4580 * This function should do only the minimum to make that possible,
4581 * and to synchronize with the parent process. The parent process
4582 * should take care of the details.
4586 procfs_set_exec_trap ()
4588 /* This routine called on the child side (inferior side)
4589 after GDB forks the inferior. It must use only local variables,
4590 because it may be sharing data space with its parent. */
4595 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4596 perror_with_name ("procfs: create_procinfo failed in child.");
4598 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4600 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4601 gdb_flush (gdb_stderr
);
4602 /* no need to call "dead_procinfo", because we're going to exit. */
4606 #ifdef PRFS_STOPEXEC /* defined on OSF */
4607 /* OSF method for tracing exec syscalls. Quoting:
4608 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4609 exits from exec system calls because of the user level loader. */
4610 /* FIXME: make nice and maybe move into an access function. */
4614 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4616 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4617 gdb_flush (gdb_stderr
);
4620 prfs_flags
|= PRFS_STOPEXEC
;
4622 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4624 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4625 gdb_flush (gdb_stderr
);
4629 #else /* not PRFS_STOPEXEC */
4630 /* Everyone else's (except OSF) method for tracing exec syscalls */
4632 Not all systems with /proc have all the exec* syscalls with the same
4633 names. On the SGI, for example, there is no SYS_exec, but there
4634 *is* a SYS_execv. So, we try to account for that. */
4636 premptyset (&exitset
);
4638 praddset (&exitset
, SYS_exec
);
4641 praddset (&exitset
, SYS_execve
);
4644 praddset (&exitset
, SYS_execv
);
4647 if (!proc_set_traced_sysexit (pi
, &exitset
))
4649 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4650 gdb_flush (gdb_stderr
);
4653 #endif /* PRFS_STOPEXEC */
4655 /* FIXME: should this be done in the parent instead? */
4656 /* Turn off inherit on fork flag so that all grand-children
4657 of gdb start with tracing flags cleared. */
4658 if (!proc_unset_inherit_on_fork (pi
))
4659 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4661 /* Turn off run on last close flag, so that the child process
4662 cannot run away just because we close our handle on it.
4663 We want it to wait for the parent to attach. */
4664 if (!proc_unset_run_on_last_close (pi
))
4665 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4667 /* FIXME: No need to destroy the procinfo --
4668 we have our own address space, and we're about to do an exec! */
4669 /*destroy_procinfo (pi);*/
4673 * Function: create_inferior
4675 * This function is called BEFORE gdb forks the inferior process.
4676 * Its only real responsibility is to set things up for the fork,
4677 * and tell GDB which two functions to call after the fork (one
4678 * for the parent, and one for the child).
4680 * This function does a complicated search for a unix shell program,
4681 * which it then uses to parse arguments and environment variables
4682 * to be sent to the child. I wonder whether this code could not
4683 * be abstracted out and shared with other unix targets such as
4688 procfs_create_inferior (exec_file
, allargs
, env
)
4693 char *shell_file
= getenv ("SHELL");
4695 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4698 /* We will be looking down the PATH to find shell_file. If we
4699 just do this the normal way (via execlp, which operates by
4700 attempting an exec for each element of the PATH until it
4701 finds one which succeeds), then there will be an exec for
4702 each failed attempt, each of which will cause a PR_SYSEXIT
4703 stop, and we won't know how to distinguish the PR_SYSEXIT's
4704 for these failed execs with the ones for successful execs
4705 (whether the exec has succeeded is stored at that time in the
4706 carry bit or some such architecture-specific and
4707 non-ABI-specified place).
4709 So I can't think of anything better than to search the PATH
4710 now. This has several disadvantages: (1) There is a race
4711 condition; if we find a file now and it is deleted before we
4712 exec it, we lose, even if the deletion leaves a valid file
4713 further down in the PATH, (2) there is no way to know exactly
4714 what an executable (in the sense of "capable of being
4715 exec'd") file is. Using access() loses because it may lose
4716 if the caller is the superuser; failing to use it loses if
4717 there are ACLs or some such. */
4721 /* FIXME-maybe: might want "set path" command so user can change what
4722 path is used from within GDB. */
4723 char *path
= getenv ("PATH");
4725 struct stat statbuf
;
4728 path
= "/bin:/usr/bin";
4730 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4731 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4733 p1
= strchr (p
, ':');
4738 strncpy (tryname
, p
, len
);
4739 tryname
[len
] = '\0';
4740 strcat (tryname
, "/");
4741 strcat (tryname
, shell_file
);
4742 if (access (tryname
, X_OK
) < 0)
4744 if (stat (tryname
, &statbuf
) < 0)
4746 if (!S_ISREG (statbuf
.st_mode
))
4747 /* We certainly need to reject directories. I'm not quite
4748 as sure about FIFOs, sockets, etc., but I kind of doubt
4749 that people want to exec() these things. */
4754 /* Not found. This must be an error rather than merely passing
4755 the file to execlp(), because execlp() would try all the
4756 exec()s, causing GDB to get confused. */
4757 error ("procfs:%d -- Can't find shell %s in PATH",
4758 __LINE__
, shell_file
);
4760 shell_file
= tryname
;
4763 fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4764 procfs_init_inferior
, NULL
, shell_file
);
4766 /* We are at the first instruction we care about. */
4767 /* Pedal to the metal... */
4769 proceed ((CORE_ADDR
) -1, TARGET_SIGNAL_0
, 0);
4773 * Function: notice_thread
4775 * Callback for find_new_threads.
4776 * Calls "add_thread".
4780 procfs_notice_thread (pi
, thread
, ptr
)
4785 int gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
4787 if (!in_thread_list (gdb_threadid
))
4788 add_thread (gdb_threadid
);
4794 * Function: target_find_new_threads
4796 * Query all the threads that the target knows about,
4797 * and give them back to GDB to add to its list.
4801 procfs_find_new_threads ()
4805 /* Find procinfo for main process */
4806 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
4807 proc_update_threads (pi
);
4808 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
4812 * Function: target_thread_alive
4814 * Return true if the thread is still 'alive'.
4816 * This guy doesn't really seem to be doing his job.
4817 * Got to investigate how to tell when a thread is really gone.
4821 procfs_thread_alive (pid
)
4827 proc
= PIDGET (pid
);
4828 thread
= TIDGET (pid
);
4829 /* If I don't know it, it ain't alive! */
4830 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
4833 /* If I can't get its status, it ain't alive!
4834 What's more, I need to forget about it! */
4835 if (!proc_get_status (pi
))
4837 destroy_procinfo (pi
);
4840 /* I couldn't have got its status if it weren't alive, so it's alive. */
4845 * Function: target_pid_to_str
4847 * Return a string to be used to identify the thread in
4848 * the "info threads" display.
4852 procfs_pid_to_str (pid
)
4855 static char buf
[80];
4859 proc
= PIDGET (pid
);
4860 thread
= TIDGET (pid
);
4861 pi
= find_procinfo (proc
, thread
);
4864 sprintf (buf
, "Process %d", proc
);
4866 sprintf (buf
, "LWP %d", thread
);
4871 * Function: procfs_set_watchpoint
4872 * Insert a watchpoint
4876 procfs_set_watchpoint (pid
, addr
, len
, rwflag
, after
)
4887 pi
= find_procinfo_or_die (pid
== -1 ?
4888 PIDGET (inferior_pid
) : PIDGET (pid
), 0);
4890 /* Translate from GDB's flags to /proc's */
4891 if (len
> 0) /* len == 0 means delete watchpoint */
4893 switch (rwflag
) { /* FIXME: need an enum! */
4894 case hw_write
: /* default watchpoint (write) */
4895 pflags
= WRITE_WATCHFLAG
;
4897 case hw_read
: /* read watchpoint */
4898 pflags
= READ_WATCHFLAG
;
4900 case hw_access
: /* access watchpoint */
4901 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
4903 case hw_execute
: /* execution HW breakpoint */
4904 pflags
= EXEC_WATCHFLAG
;
4906 default: /* Something weird. Return error. */
4909 if (after
) /* Stop after r/w access is completed. */
4910 pflags
|= AFTER_WATCHFLAG
;
4913 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
4915 if (errno
== E2BIG
) /* Typical error for no resources */
4916 return -1; /* fail */
4917 /* GDB may try to remove the same watchpoint twice.
4918 If a remove request returns no match, don't error. */
4919 if (errno
== ESRCH
&& len
== 0)
4920 return 0; /* ignore */
4921 proc_error (pi
, "set_watchpoint", __LINE__
);
4928 * Function: stopped_by_watchpoint
4930 * Returns non-zero if process is stopped on a hardware watchpoint fault,
4931 * else returns zero.
4935 procfs_stopped_by_watchpoint (pid
)
4940 pi
= find_procinfo_or_die (pid
== -1 ?
4941 PIDGET (inferior_pid
) : PIDGET (pid
), 0);
4942 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
4944 if (proc_why (pi
) == PR_FAULTED
)
4947 if (proc_what (pi
) == FLTWATCH
)
4951 if (proc_what (pi
) == FLTKWATCH
)
4959 #ifdef TM_I386SOL2_H
4961 * Function: procfs_find_LDT_entry
4964 * int pid; // The GDB-style pid-plus-LWP.
4967 * pointer to the corresponding LDT entry.
4971 procfs_find_LDT_entry (pid
)
4974 gdb_gregset_t
*gregs
;
4978 /* Find procinfo for the lwp. */
4979 if ((pi
= find_procinfo (PIDGET (pid
), TIDGET (pid
))) == NULL
)
4981 warning ("procfs_find_LDT_entry: could not find procinfi for %d.",
4985 /* get its general registers. */
4986 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
4988 warning ("procfs_find_LDT_entry: could not read gregs for %d.",
4992 /* Now extract the GS register's lower 16 bits. */
4993 key
= (*gregs
)[GS
] & 0xffff;
4995 /* Find the matching entry and return it. */
4996 return proc_get_LDT_entry (pi
, key
);
4998 #endif /* TM_I386SOL2_H */
5003 info_proc_cmd (args
, from_tty
)
5007 struct cleanup
*old_chain
;
5008 procinfo
*process
= NULL
;
5009 procinfo
*thread
= NULL
;
5015 old_chain
= make_cleanup (null_cleanup
, 0);
5018 if ((argv
= buildargv (args
)) == NULL
)
5021 make_cleanup_freeargv (argv
);
5023 while (argv
!= NULL
&& *argv
!= NULL
)
5025 if (isdigit (argv
[0][0]))
5027 pid
= strtoul (argv
[0], &tmp
, 10);
5029 tid
= strtoul (++tmp
, NULL
, 10);
5031 else if (argv
[0][0] == '/')
5033 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5042 pid
= PIDGET (inferior_pid
);
5044 error ("No current process: you must name one.");
5047 /* Have pid, will travel.
5048 First see if it's a process we're already debugging. */
5049 process
= find_procinfo (pid
, 0);
5050 if (process
== NULL
)
5052 /* No. So open a procinfo for it, but
5053 remember to close it again when finished. */
5054 process
= create_procinfo (pid
, 0);
5055 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5056 if (!open_procinfo_files (process
, FD_CTL
))
5057 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5061 thread
= create_procinfo (pid
, tid
);
5065 printf_filtered ("process %d flags:\n", process
->pid
);
5066 proc_prettyprint_flags (proc_flags (process
), 1);
5067 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5068 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5069 if (proc_get_nthreads (process
) > 1)
5070 printf_filtered ("Process has %d threads.\n",
5071 proc_get_nthreads (process
));
5075 printf_filtered ("thread %d flags:\n", thread
->tid
);
5076 proc_prettyprint_flags (proc_flags (thread
), 1);
5077 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5078 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5081 do_cleanups (old_chain
);
5085 proc_trace_syscalls (args
, from_tty
, entry_or_exit
, mode
)
5095 if (inferior_pid
<= 0)
5096 error ("you must be debugging a process to use this command.");
5098 if (args
== NULL
|| args
[0] == 0)
5099 error_no_arg ("system call to trace");
5101 pi
= find_procinfo_or_die (PIDGET (inferior_pid
), 0);
5102 if (isdigit (args
[0]))
5104 syscallnum
= atoi (args
);
5105 if (entry_or_exit
== PR_SYSENTRY
)
5106 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5108 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5111 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5113 if (mode
== FLAG_SET
)
5114 praddset (sysset
, syscallnum
);
5116 prdelset (sysset
, syscallnum
);
5118 if (entry_or_exit
== PR_SYSENTRY
)
5120 if (!proc_set_traced_sysentry (pi
, sysset
))
5121 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5125 if (!proc_set_traced_sysexit (pi
, sysset
))
5126 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5132 proc_trace_sysentry_cmd (args
, from_tty
)
5136 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5140 proc_trace_sysexit_cmd (args
, from_tty
)
5144 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5148 proc_untrace_sysentry_cmd (args
, from_tty
)
5152 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5156 proc_untrace_sysexit_cmd (args
, from_tty
)
5160 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5165 _initialize_procfs ()
5168 add_target (&procfs_ops
);
5169 add_info ("proc", info_proc_cmd
,
5170 "Show /proc process information about any running process.\
5171 Default is the process being debugged.");
5172 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5173 "Give a trace of entries into the syscall.");
5174 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5175 "Give a trace of exits from the syscall.");
5176 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5177 "Cancel a trace of entries into the syscall.");
5178 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5179 "Cancel a trace of exits from the syscall.");
5182 /* =================== END, GDB "MODULE" =================== */
5186 /* miscelaneous stubs: */
5187 /* The following satisfy a few random symbols mostly created by */
5188 /* the solaris threads implementation, which I will chase down */
5192 * Return a pid for which we guarantee
5193 * we will be able to find a 'live' procinfo.
5197 procfs_first_available ()
5200 return procinfo_list
->pid
;