1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2006, 2007
4 Free Software Foundation, Inc.
6 Written by Michael Snyder at Cygnus Solutions.
7 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin Street, Fifth Floor,
24 Boston, MA 02110-1301, USA. */
30 #include "elf-bfd.h" /* for elfcore_write_* */
32 #include "gdbthread.h"
35 #if defined (NEW_PROC_API)
36 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
39 #include <sys/procfs.h>
40 #ifdef HAVE_SYS_FAULT_H
41 #include <sys/fault.h>
43 #ifdef HAVE_SYS_SYSCALL_H
44 #include <sys/syscall.h>
46 #include <sys/errno.h>
50 #include "gdb_string.h"
51 #include "gdb_assert.h"
58 * This module provides the interface between GDB and the
59 * /proc file system, which is used on many versions of Unix
60 * as a means for debuggers to control other processes.
61 * Examples of the systems that use this interface are:
68 * /proc works by imitating a file system: you open a simulated file
69 * that represents the process you wish to interact with, and
70 * perform operations on that "file" in order to examine or change
71 * the state of the other process.
73 * The most important thing to know about /proc and this module
74 * is that there are two very different interfaces to /proc:
75 * One that uses the ioctl system call, and
76 * another that uses read and write system calls.
77 * This module has to support both /proc interfaces. This means
78 * that there are two different ways of doing every basic operation.
80 * In order to keep most of the code simple and clean, I have
81 * defined an interface "layer" which hides all these system calls.
82 * An ifdef (NEW_PROC_API) determines which interface we are using,
83 * and most or all occurrances of this ifdef should be confined to
84 * this interface layer.
88 /* Determine which /proc API we are using:
89 The ioctl API defines PIOCSTATUS, while
90 the read/write (multiple fd) API never does. */
93 #include <sys/types.h>
94 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
97 #include <fcntl.h> /* for O_RDONLY */
98 #include <unistd.h> /* for "X_OK" */
99 #include "gdb_stat.h" /* for struct stat */
101 /* Note: procfs-utils.h must be included after the above system header
102 files, because it redefines various system calls using macros.
103 This may be incompatible with the prototype declarations. */
105 #include "proc-utils.h"
107 /* Prototypes for supply_gregset etc. */
110 /* =================== TARGET_OPS "MODULE" =================== */
113 * This module defines the GDB target vector and its methods.
116 static void procfs_open (char *, int);
117 static void procfs_attach (char *, int);
118 static void procfs_detach (char *, int);
119 static void procfs_resume (ptid_t, int, enum target_signal);
120 static int procfs_can_run (void);
121 static void procfs_stop (void);
122 static void procfs_files_info (struct target_ops *);
123 static void procfs_fetch_registers (struct regcache *, int);
124 static void procfs_store_registers (struct regcache *, int);
125 static void procfs_notice_signals (ptid_t);
126 static void procfs_prepare_to_store (struct regcache *);
127 static void procfs_kill_inferior (void);
128 static void procfs_mourn_inferior (void);
129 static void procfs_create_inferior (char *, char *, char **, int);
130 static ptid_t procfs_wait (ptid_t, struct target_waitstatus *);
131 static int procfs_xfer_memory (CORE_ADDR, char *, int, int,
132 struct mem_attrib *attrib,
133 struct target_ops *);
134 static LONGEST procfs_xfer_partial (struct target_ops *ops,
135 enum target_object object,
137 void *readbuf, const void *writebuf,
138 ULONGEST offset, LONGEST len);
140 static int procfs_thread_alive (ptid_t);
142 void procfs_find_new_threads (void);
143 char *procfs_pid_to_str (ptid_t);
145 static int proc_find_memory_regions (int (*) (CORE_ADDR,
151 static char * procfs_make_note_section (bfd *, int *);
153 static int procfs_can_use_hw_breakpoint (int, int, int);
155 struct target_ops procfs_ops; /* the target vector */
158 init_procfs_ops (void)
160 procfs_ops.to_shortname = "procfs";
161 procfs_ops.to_longname = "Unix /proc child process";
163 "Unix /proc child process (started by the \"run\" command).";
164 procfs_ops.to_open = procfs_open;
165 procfs_ops.to_can_run = procfs_can_run;
166 procfs_ops.to_create_inferior = procfs_create_inferior;
167 procfs_ops.to_kill = procfs_kill_inferior;
168 procfs_ops.to_mourn_inferior = procfs_mourn_inferior;
169 procfs_ops.to_attach = procfs_attach;
170 procfs_ops.to_detach = procfs_detach;
171 procfs_ops.to_wait = procfs_wait;
172 procfs_ops.to_resume = procfs_resume;
173 procfs_ops.to_prepare_to_store = procfs_prepare_to_store;
174 procfs_ops.to_fetch_registers = procfs_fetch_registers;
175 procfs_ops.to_store_registers = procfs_store_registers;
176 procfs_ops.to_xfer_partial = procfs_xfer_partial;
177 procfs_ops.deprecated_xfer_memory = procfs_xfer_memory;
178 procfs_ops.to_insert_breakpoint = memory_insert_breakpoint;
179 procfs_ops.to_remove_breakpoint = memory_remove_breakpoint;
180 procfs_ops.to_notice_signals = procfs_notice_signals;
181 procfs_ops.to_files_info = procfs_files_info;
182 procfs_ops.to_stop = procfs_stop;
184 procfs_ops.to_terminal_init = terminal_init_inferior;
185 procfs_ops.to_terminal_inferior = terminal_inferior;
186 procfs_ops.to_terminal_ours_for_output = terminal_ours_for_output;
187 procfs_ops.to_terminal_ours = terminal_ours;
188 procfs_ops.to_terminal_save_ours = terminal_save_ours;
189 procfs_ops.to_terminal_info = child_terminal_info;
191 procfs_ops.to_find_new_threads = procfs_find_new_threads;
192 procfs_ops.to_thread_alive = procfs_thread_alive;
193 procfs_ops.to_pid_to_str = procfs_pid_to_str;
195 procfs_ops.to_has_all_memory = 1;
196 procfs_ops.to_has_memory = 1;
197 procfs_ops.to_has_execution = 1;
198 procfs_ops.to_has_stack = 1;
199 procfs_ops.to_has_registers = 1;
200 procfs_ops.to_stratum = process_stratum;
201 procfs_ops.to_has_thread_control = tc_schedlock;
202 procfs_ops.to_find_memory_regions = proc_find_memory_regions;
203 procfs_ops.to_make_corefile_notes = procfs_make_note_section;
204 procfs_ops.to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
205 procfs_ops.to_magic = OPS_MAGIC;
208 /* =================== END, TARGET_OPS "MODULE" =================== */
213 * Put any typedefs, defines etc. here that are required for
214 * the unification of code that handles different versions of /proc.
217 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
219 enum { READ_WATCHFLAG = WA_READ,
220 WRITE_WATCHFLAG = WA_WRITE,
221 EXEC_WATCHFLAG = WA_EXEC,
222 AFTER_WATCHFLAG = WA_TRAPAFTER
225 #else /* Irix method for watchpoints */
226 enum { READ_WATCHFLAG = MA_READ,
227 WRITE_WATCHFLAG = MA_WRITE,
228 EXEC_WATCHFLAG = MA_EXEC,
229 AFTER_WATCHFLAG = 0 /* trapafter not implemented */
234 #ifdef HAVE_PR_SIGSET_T
235 typedef pr_sigset_t gdb_sigset_t;
237 typedef sigset_t gdb_sigset_t;
241 #ifdef HAVE_PR_SIGACTION64_T
242 typedef pr_sigaction64_t gdb_sigaction_t;
244 typedef struct sigaction gdb_sigaction_t;
248 #ifdef HAVE_PR_SIGINFO64_T
249 typedef pr_siginfo64_t gdb_siginfo_t;
251 typedef struct siginfo gdb_siginfo_t;
254 /* gdb_premptysysset */
256 #define gdb_premptysysset premptysysset
258 #define gdb_premptysysset premptyset
263 #define gdb_praddsysset praddsysset
265 #define gdb_praddsysset praddset
270 #define gdb_prdelsysset prdelsysset
272 #define gdb_prdelsysset prdelset
275 /* prissyssetmember */
276 #ifdef prissyssetmember
277 #define gdb_pr_issyssetmember prissyssetmember
279 #define gdb_pr_issyssetmember prismember
282 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
283 as intuitively descriptive as it could be, so we'll define
284 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
285 this writing, this feature is only found on AIX5 systems and
286 basically means that the set of syscalls is not fixed. I.e,
287 there's no nice table that one can #include to get all of the
288 syscall numbers. Instead, they're stored in /proc/PID/sysent
289 for each process. We are at least guaranteed that they won't
290 change over the lifetime of the process. But each process could
291 (in theory) have different syscall numbers.
293 #ifdef HAVE_PRSYSENT_T
294 #define DYNAMIC_SYSCALLS
299 /* =================== STRUCT PROCINFO "MODULE" =================== */
301 /* FIXME: this comment will soon be out of date W.R.T. threads. */
303 /* The procinfo struct is a wrapper to hold all the state information
304 concerning a /proc process. There should be exactly one procinfo
305 for each process, and since GDB currently can debug only one
306 process at a time, that means there should be only one procinfo.
307 All of the LWP's of a process can be accessed indirectly thru the
308 single process procinfo.
310 However, against the day when GDB may debug more than one process,
311 this data structure is kept in a list (which for now will hold no
312 more than one member), and many functions will have a pointer to a
313 procinfo as an argument.
315 There will be a separate procinfo structure for use by the (not yet
316 implemented) "info proc" command, so that we can print useful
317 information about any random process without interfering with the
318 inferior's procinfo information. */
321 /* format strings for /proc paths */
322 # ifndef CTL_PROC_NAME_FMT
323 # define MAIN_PROC_NAME_FMT "/proc/%d"
324 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
325 # define AS_PROC_NAME_FMT "/proc/%d/as"
326 # define MAP_PROC_NAME_FMT "/proc/%d/map"
327 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
328 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
330 /* the name of the proc status struct depends on the implementation */
331 typedef pstatus_t gdb_prstatus_t;
332 typedef lwpstatus_t gdb_lwpstatus_t;
333 #else /* ! NEW_PROC_API */
334 /* format strings for /proc paths */
335 # ifndef CTL_PROC_NAME_FMT
336 # define MAIN_PROC_NAME_FMT "/proc/%05d"
337 # define CTL_PROC_NAME_FMT "/proc/%05d"
338 # define AS_PROC_NAME_FMT "/proc/%05d"
339 # define MAP_PROC_NAME_FMT "/proc/%05d"
340 # define STATUS_PROC_NAME_FMT "/proc/%05d"
341 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
343 /* the name of the proc status struct depends on the implementation */
344 typedef prstatus_t gdb_prstatus_t;
345 typedef prstatus_t gdb_lwpstatus_t;
346 #endif /* NEW_PROC_API */
348 typedef struct procinfo {
349 struct procinfo *next;
350 int pid; /* Process ID */
351 int tid; /* Thread/LWP id */
355 int ignore_next_sigstop;
357 /* The following four fd fields may be identical, or may contain
358 several different fd's, depending on the version of /proc
359 (old ioctl or new read/write). */
361 int ctl_fd; /* File descriptor for /proc control file */
363 * The next three file descriptors are actually only needed in the
364 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
365 * However, to avoid a bunch of #ifdefs in the code, we will use
366 * them uniformly by (in the case of the ioctl single-file-descriptor
367 * implementation) filling them with copies of the control fd.
369 int status_fd; /* File descriptor for /proc status file */
370 int as_fd; /* File descriptor for /proc as file */
372 char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */
374 fltset_t saved_fltset; /* Saved traced hardware fault set */
375 gdb_sigset_t saved_sigset; /* Saved traced signal set */
376 gdb_sigset_t saved_sighold; /* Saved held signal set */
377 sysset_t *saved_exitset; /* Saved traced system call exit set */
378 sysset_t *saved_entryset; /* Saved traced system call entry set */
380 gdb_prstatus_t prstatus; /* Current process status info */
383 gdb_fpregset_t fpregset; /* Current floating point registers */
386 #ifdef DYNAMIC_SYSCALLS
387 int num_syscalls; /* Total number of syscalls */
388 char **syscall_names; /* Syscall number to name map */
391 struct procinfo *thread_list;
393 int status_valid : 1;
395 int fpregs_valid : 1;
396 int threads_valid: 1;
399 static char errmsg[128]; /* shared error msg buffer */
401 /* Function prototypes for procinfo module: */
403 static procinfo *find_procinfo_or_die (int pid, int tid);
404 static procinfo *find_procinfo (int pid, int tid);
405 static procinfo *create_procinfo (int pid, int tid);
406 static void destroy_procinfo (procinfo * p);
407 static void do_destroy_procinfo_cleanup (void *);
408 static void dead_procinfo (procinfo * p, char *msg, int killp);
409 static int open_procinfo_files (procinfo * p, int which);
410 static void close_procinfo_files (procinfo * p);
411 static int sysset_t_size (procinfo *p);
412 static sysset_t *sysset_t_alloc (procinfo * pi);
413 #ifdef DYNAMIC_SYSCALLS
414 static void load_syscalls (procinfo *pi);
415 static void free_syscalls (procinfo *pi);
416 static int find_syscall (procinfo *pi, char *name);
417 #endif /* DYNAMIC_SYSCALLS */
419 /* The head of the procinfo list: */
420 static procinfo * procinfo_list;
423 * Function: find_procinfo
425 * Search the procinfo list.
427 * Returns: pointer to procinfo, or NULL if not found.
431 find_procinfo (int pid, int tid)
435 for (pi = procinfo_list; pi; pi = pi->next)
442 /* Don't check threads_valid. If we're updating the
443 thread_list, we want to find whatever threads are already
444 here. This means that in general it is the caller's
445 responsibility to check threads_valid and update before
446 calling find_procinfo, if the caller wants to find a new
449 for (pi = pi->thread_list; pi; pi = pi->next)
458 * Function: find_procinfo_or_die
460 * Calls find_procinfo, but errors on failure.
464 find_procinfo_or_die (int pid, int tid)
466 procinfo *pi = find_procinfo (pid, tid);
471 error (_("procfs: couldn't find pid %d (kernel thread %d) in procinfo list."),
474 error (_("procfs: couldn't find pid %d in procinfo list."), pid);
479 /* open_with_retry() is a wrapper for open(). The appropriate
480 open() call is attempted; if unsuccessful, it will be retried as
481 many times as needed for the EAGAIN and EINTR conditions.
483 For other conditions, open_with_retry() will retry the open() a
484 limited number of times. In addition, a short sleep is imposed
485 prior to retrying the open(). The reason for this sleep is to give
486 the kernel a chance to catch up and create the file in question in
487 the event that GDB "wins" the race to open a file before the kernel
491 open_with_retry (const char *pathname, int flags)
493 int retries_remaining, status;
495 retries_remaining = 2;
499 status = open (pathname, flags);
501 if (status >= 0 || retries_remaining == 0)
503 else if (errno != EINTR && errno != EAGAIN)
514 * Function: open_procinfo_files
516 * Open the file descriptor for the process or LWP.
517 * ifdef NEW_PROC_API, we only open the control file descriptor;
518 * the others are opened lazily as needed.
519 * else (if not NEW_PROC_API), there is only one real
520 * file descriptor, but we keep multiple copies of it so that
521 * the code that uses them does not have to be #ifdef'd.
523 * Return: file descriptor, or zero for failure.
526 enum { FD_CTL, FD_STATUS, FD_AS };
529 open_procinfo_files (procinfo *pi, int which)
532 char tmp[MAX_PROC_NAME_SIZE];
537 * This function is getting ALMOST long enough to break up into several.
538 * Here is some rationale:
540 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
541 * There are several file descriptors that may need to be open
542 * for any given process or LWP. The ones we're intereted in are:
543 * - control (ctl) write-only change the state
544 * - status (status) read-only query the state
545 * - address space (as) read/write access memory
546 * - map (map) read-only virtual addr map
547 * Most of these are opened lazily as they are needed.
548 * The pathnames for the 'files' for an LWP look slightly
549 * different from those of a first-class process:
550 * Pathnames for a process (<proc-id>):
551 * /proc/<proc-id>/ctl
552 * /proc/<proc-id>/status
554 * /proc/<proc-id>/map
555 * Pathnames for an LWP (lwp-id):
556 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
557 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
558 * An LWP has no map or address space file descriptor, since
559 * the memory map and address space are shared by all LWPs.
561 * Everyone else (Solaris 2.5, Irix, OSF)
562 * There is only one file descriptor for each process or LWP.
563 * For convenience, we copy the same file descriptor into all
564 * three fields of the procinfo struct (ctl_fd, status_fd, and
565 * as_fd, see NEW_PROC_API above) so that code that uses them
566 * doesn't need any #ifdef's.
571 * Each LWP has an independent file descriptor, but these
572 * are not obtained via the 'open' system call like the rest:
573 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
574 * to the file descriptor of the parent process.
577 * These do not even have their own independent file descriptor.
578 * All operations are carried out on the file descriptor of the
579 * parent process. Therefore we just call open again for each
580 * thread, getting a new handle for the same 'file'.
585 * In this case, there are several different file descriptors that
586 * we might be asked to open. The control file descriptor will be
587 * opened early, but the others will be opened lazily as they are
591 strcpy (tmp, pi->pathname);
592 switch (which) { /* which file descriptor to open? */
595 strcat (tmp, "/lwpctl");
597 strcat (tmp, "/ctl");
598 fd = open_with_retry (tmp, O_WRONLY);
605 return 0; /* there is no 'as' file descriptor for an lwp */
607 fd = open_with_retry (tmp, O_RDWR);
614 strcat (tmp, "/lwpstatus");
616 strcat (tmp, "/status");
617 fd = open_with_retry (tmp, O_RDONLY);
623 return 0; /* unknown file descriptor */
625 #else /* not NEW_PROC_API */
627 * In this case, there is only one file descriptor for each procinfo
628 * (ie. each process or LWP). In fact, only the file descriptor for
629 * the process can actually be opened by an 'open' system call.
630 * The ones for the LWPs have to be obtained thru an IOCTL call
631 * on the process's file descriptor.
633 * For convenience, we copy each procinfo's single file descriptor
634 * into all of the fields occupied by the several file descriptors
635 * of the NEW_PROC_API implementation. That way, the code that uses
636 * them can be written without ifdefs.
640 #ifdef PIOCTSTATUS /* OSF */
641 /* Only one FD; just open it. */
642 if ((fd = open_with_retry (pi->pathname, O_RDWR)) == 0)
644 #else /* Sol 2.5, Irix, other? */
645 if (pi->tid == 0) /* Master procinfo for the process */
647 fd = open_with_retry (pi->pathname, O_RDWR);
651 else /* LWP thread procinfo */
653 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
657 /* Find the procinfo for the entire process. */
658 if ((process = find_procinfo (pi->pid, 0)) == NULL)
661 /* Now obtain the file descriptor for the LWP. */
662 if ((fd = ioctl (process->ctl_fd, PIOCOPENLWP, &lwpid)) <= 0)
664 #else /* Irix, other? */
665 return 0; /* Don't know how to open threads */
666 #endif /* Sol 2.5 PIOCOPENLWP */
668 #endif /* OSF PIOCTSTATUS */
669 pi->ctl_fd = pi->as_fd = pi->status_fd = fd;
670 #endif /* NEW_PROC_API */
672 return 1; /* success */
676 * Function: create_procinfo
678 * Allocate a data structure and link it into the procinfo list.
679 * (First tries to find a pre-existing one (FIXME: why?)
681 * Return: pointer to new procinfo struct.
685 create_procinfo (int pid, int tid)
687 procinfo *pi, *parent;
689 if ((pi = find_procinfo (pid, tid)))
690 return pi; /* Already exists, nothing to do. */
692 /* find parent before doing malloc, to save having to cleanup */
694 parent = find_procinfo_or_die (pid, 0); /* FIXME: should I
696 doesn't exist yet? */
698 pi = (procinfo *) xmalloc (sizeof (procinfo));
699 memset (pi, 0, sizeof (procinfo));
703 #ifdef DYNAMIC_SYSCALLS
707 pi->saved_entryset = sysset_t_alloc (pi);
708 pi->saved_exitset = sysset_t_alloc (pi);
710 /* Chain into list. */
713 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
714 pi->next = procinfo_list;
720 sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid);
722 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
724 pi->next = parent->thread_list;
725 parent->thread_list = pi;
731 * Function: close_procinfo_files
733 * Close all file descriptors associated with the procinfo
737 close_procinfo_files (procinfo *pi)
744 if (pi->status_fd > 0)
745 close (pi->status_fd);
747 pi->ctl_fd = pi->as_fd = pi->status_fd = 0;
751 * Function: destroy_procinfo
753 * Destructor function. Close, unlink and deallocate the object.
757 destroy_one_procinfo (procinfo **list, procinfo *pi)
761 /* Step one: unlink the procinfo from its list */
765 for (ptr = *list; ptr; ptr = ptr->next)
768 ptr->next = pi->next;
772 /* Step two: close any open file descriptors */
773 close_procinfo_files (pi);
775 /* Step three: free the memory. */
776 #ifdef DYNAMIC_SYSCALLS
779 xfree (pi->saved_entryset);
780 xfree (pi->saved_exitset);
785 destroy_procinfo (procinfo *pi)
789 if (pi->tid != 0) /* destroy a thread procinfo */
791 tmp = find_procinfo (pi->pid, 0); /* find the parent process */
792 destroy_one_procinfo (&tmp->thread_list, pi);
794 else /* destroy a process procinfo and all its threads */
796 /* First destroy the children, if any; */
797 while (pi->thread_list != NULL)
798 destroy_one_procinfo (&pi->thread_list, pi->thread_list);
799 /* Then destroy the parent. Genocide!!! */
800 destroy_one_procinfo (&procinfo_list, pi);
805 do_destroy_procinfo_cleanup (void *pi)
807 destroy_procinfo (pi);
810 enum { NOKILL, KILL };
813 * Function: dead_procinfo
815 * To be called on a non_recoverable error for a procinfo.
816 * Prints error messages, optionally sends a SIGKILL to the process,
817 * then destroys the data structure.
821 dead_procinfo (procinfo *pi, char *msg, int kill_p)
827 print_sys_errmsg (pi->pathname, errno);
831 sprintf (procfile, "process %d", pi->pid);
832 print_sys_errmsg (procfile, errno);
835 kill (pi->pid, SIGKILL);
837 destroy_procinfo (pi);
842 * Function: sysset_t_size
844 * Returns the (complete) size of a sysset_t struct. Normally, this
845 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
846 * size of sysset_t isn't known until runtime.
850 sysset_t_size (procinfo * pi)
852 #ifndef DYNAMIC_SYSCALLS
853 return sizeof (sysset_t);
855 return sizeof (sysset_t) - sizeof (uint64_t)
856 + sizeof (uint64_t) * ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
857 / (8 * sizeof (uint64_t)));
861 /* Function: sysset_t_alloc
863 Allocate and (partially) initialize a sysset_t struct. */
866 sysset_t_alloc (procinfo * pi)
869 int size = sysset_t_size (pi);
870 ret = xmalloc (size);
871 #ifdef DYNAMIC_SYSCALLS
872 ret->pr_size = (pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
873 / (8 * sizeof (uint64_t));
878 #ifdef DYNAMIC_SYSCALLS
880 /* Function: load_syscalls
882 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
883 pi->num_syscalls with the number of syscalls and pi->syscall_names
884 with the names. (Certain numbers may be skipped in which case the
885 names for these numbers will be left as NULL.) */
887 #define MAX_SYSCALL_NAME_LENGTH 256
888 #define MAX_SYSCALLS 65536
891 load_syscalls (procinfo *pi)
893 char pathname[MAX_PROC_NAME_SIZE];
896 prsyscall_t *syscalls;
897 int i, size, maxcall;
899 pi->num_syscalls = 0;
900 pi->syscall_names = 0;
902 /* Open the file descriptor for the sysent file */
903 sprintf (pathname, "/proc/%d/sysent", pi->pid);
904 sysent_fd = open_with_retry (pathname, O_RDONLY);
907 error (_("load_syscalls: Can't open /proc/%d/sysent"), pi->pid);
910 size = sizeof header - sizeof (prsyscall_t);
911 if (read (sysent_fd, &header, size) != size)
913 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
916 if (header.pr_nsyscalls == 0)
918 error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"), pi->pid);
921 size = header.pr_nsyscalls * sizeof (prsyscall_t);
922 syscalls = xmalloc (size);
924 if (read (sysent_fd, syscalls, size) != size)
927 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
930 /* Find maximum syscall number. This may not be the same as
931 pr_nsyscalls since that value refers to the number of entries
932 in the table. (Also, the docs indicate that some system
933 call numbers may be skipped.) */
935 maxcall = syscalls[0].pr_number;
937 for (i = 1; i < header.pr_nsyscalls; i++)
938 if (syscalls[i].pr_number > maxcall
939 && syscalls[i].pr_nameoff > 0
940 && syscalls[i].pr_number < MAX_SYSCALLS)
941 maxcall = syscalls[i].pr_number;
943 pi->num_syscalls = maxcall+1;
944 pi->syscall_names = xmalloc (pi->num_syscalls * sizeof (char *));
946 for (i = 0; i < pi->num_syscalls; i++)
947 pi->syscall_names[i] = NULL;
949 /* Read the syscall names in */
950 for (i = 0; i < header.pr_nsyscalls; i++)
952 char namebuf[MAX_SYSCALL_NAME_LENGTH];
956 if (syscalls[i].pr_number >= MAX_SYSCALLS
957 || syscalls[i].pr_number < 0
958 || syscalls[i].pr_nameoff <= 0
959 || (lseek (sysent_fd, (off_t) syscalls[i].pr_nameoff, SEEK_SET)
960 != (off_t) syscalls[i].pr_nameoff))
963 nread = read (sysent_fd, namebuf, sizeof namebuf);
967 callnum = syscalls[i].pr_number;
969 if (pi->syscall_names[callnum] != NULL)
971 /* FIXME: Generate warning */
975 namebuf[nread-1] = '\0';
976 size = strlen (namebuf) + 1;
977 pi->syscall_names[callnum] = xmalloc (size);
978 strncpy (pi->syscall_names[callnum], namebuf, size-1);
979 pi->syscall_names[callnum][size-1] = '\0';
986 /* Function: free_syscalls
988 Free the space allocated for the syscall names from the procinfo
992 free_syscalls (procinfo *pi)
994 if (pi->syscall_names)
998 for (i = 0; i < pi->num_syscalls; i++)
999 if (pi->syscall_names[i] != NULL)
1000 xfree (pi->syscall_names[i]);
1002 xfree (pi->syscall_names);
1003 pi->syscall_names = 0;
1007 /* Function: find_syscall
1009 Given a name, look up (and return) the corresponding syscall number.
1010 If no match is found, return -1. */
1013 find_syscall (procinfo *pi, char *name)
1016 for (i = 0; i < pi->num_syscalls; i++)
1018 if (pi->syscall_names[i] && strcmp (name, pi->syscall_names[i]) == 0)
1025 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1027 /* =================== /proc "MODULE" =================== */
1030 * This "module" is the interface layer between the /proc system API
1031 * and the gdb target vector functions. This layer consists of
1032 * access functions that encapsulate each of the basic operations
1033 * that we need to use from the /proc API.
1035 * The main motivation for this layer is to hide the fact that
1036 * there are two very different implementations of the /proc API.
1037 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1038 * functions, we do our best to hide them all in here.
1041 int proc_get_status (procinfo * pi);
1042 long proc_flags (procinfo * pi);
1043 int proc_why (procinfo * pi);
1044 int proc_what (procinfo * pi);
1045 int proc_set_run_on_last_close (procinfo * pi);
1046 int proc_unset_run_on_last_close (procinfo * pi);
1047 int proc_set_inherit_on_fork (procinfo * pi);
1048 int proc_unset_inherit_on_fork (procinfo * pi);
1049 int proc_set_async (procinfo * pi);
1050 int proc_unset_async (procinfo * pi);
1051 int proc_stop_process (procinfo * pi);
1052 int proc_trace_signal (procinfo * pi, int signo);
1053 int proc_ignore_signal (procinfo * pi, int signo);
1054 int proc_clear_current_fault (procinfo * pi);
1055 int proc_set_current_signal (procinfo * pi, int signo);
1056 int proc_clear_current_signal (procinfo * pi);
1057 int proc_set_gregs (procinfo * pi);
1058 int proc_set_fpregs (procinfo * pi);
1059 int proc_wait_for_stop (procinfo * pi);
1060 int proc_run_process (procinfo * pi, int step, int signo);
1061 int proc_kill (procinfo * pi, int signo);
1062 int proc_parent_pid (procinfo * pi);
1063 int proc_get_nthreads (procinfo * pi);
1064 int proc_get_current_thread (procinfo * pi);
1065 int proc_set_held_signals (procinfo * pi, gdb_sigset_t * sighold);
1066 int proc_set_traced_sysexit (procinfo * pi, sysset_t * sysset);
1067 int proc_set_traced_sysentry (procinfo * pi, sysset_t * sysset);
1068 int proc_set_traced_faults (procinfo * pi, fltset_t * fltset);
1069 int proc_set_traced_signals (procinfo * pi, gdb_sigset_t * sigset);
1071 int proc_update_threads (procinfo * pi);
1072 int proc_iterate_over_threads (procinfo * pi,
1073 int (*func) (procinfo *, procinfo *, void *),
1076 gdb_gregset_t *proc_get_gregs (procinfo * pi);
1077 gdb_fpregset_t *proc_get_fpregs (procinfo * pi);
1078 sysset_t *proc_get_traced_sysexit (procinfo * pi, sysset_t * save);
1079 sysset_t *proc_get_traced_sysentry (procinfo * pi, sysset_t * save);
1080 fltset_t *proc_get_traced_faults (procinfo * pi, fltset_t * save);
1081 gdb_sigset_t *proc_get_traced_signals (procinfo * pi, gdb_sigset_t * save);
1082 gdb_sigset_t *proc_get_held_signals (procinfo * pi, gdb_sigset_t * save);
1083 gdb_sigset_t *proc_get_pending_signals (procinfo * pi, gdb_sigset_t * save);
1084 gdb_sigaction_t *proc_get_signal_actions (procinfo * pi, gdb_sigaction_t *save);
1086 void proc_warn (procinfo * pi, char *func, int line);
1087 void proc_error (procinfo * pi, char *func, int line);
1090 proc_warn (procinfo *pi, char *func, int line)
1092 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1093 print_sys_errmsg (errmsg, errno);
1097 proc_error (procinfo *pi, char *func, int line)
1099 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1100 perror_with_name (errmsg);
1104 * Function: proc_get_status
1106 * Updates the status struct in the procinfo.
1107 * There is a 'valid' flag, to let other functions know when
1108 * this function needs to be called (so the status is only
1109 * read when it is needed). The status file descriptor is
1110 * also only opened when it is needed.
1112 * Return: non-zero for success, zero for failure.
1116 proc_get_status (procinfo *pi)
1118 /* Status file descriptor is opened "lazily" */
1119 if (pi->status_fd == 0 &&
1120 open_procinfo_files (pi, FD_STATUS) == 0)
1122 pi->status_valid = 0;
1127 if (lseek (pi->status_fd, 0, SEEK_SET) < 0)
1128 pi->status_valid = 0; /* fail */
1131 /* Sigh... I have to read a different data structure,
1132 depending on whether this is a main process or an LWP. */
1134 pi->status_valid = (read (pi->status_fd,
1135 (char *) &pi->prstatus.pr_lwp,
1136 sizeof (lwpstatus_t))
1137 == sizeof (lwpstatus_t));
1140 pi->status_valid = (read (pi->status_fd,
1141 (char *) &pi->prstatus,
1142 sizeof (gdb_prstatus_t))
1143 == sizeof (gdb_prstatus_t));
1144 #if 0 /*def UNIXWARE*/
1145 if (pi->status_valid &&
1146 (pi->prstatus.pr_lwp.pr_flags & PR_ISTOP) &&
1147 pi->prstatus.pr_lwp.pr_why == PR_REQUESTED)
1148 /* Unixware peculiarity -- read the damn thing again! */
1149 pi->status_valid = (read (pi->status_fd,
1150 (char *) &pi->prstatus,
1151 sizeof (gdb_prstatus_t))
1152 == sizeof (gdb_prstatus_t));
1153 #endif /* UNIXWARE */
1156 #else /* ioctl method */
1157 #ifdef PIOCTSTATUS /* osf */
1158 if (pi->tid == 0) /* main process */
1160 /* Just read the danged status. Now isn't that simple? */
1162 (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1169 tid_t pr_error_thread;
1170 struct prstatus status;
1173 thread_status.pr_count = 1;
1174 thread_status.status.pr_tid = pi->tid;
1175 win = (ioctl (pi->status_fd, PIOCTSTATUS, &thread_status) >= 0);
1178 memcpy (&pi->prstatus, &thread_status.status,
1179 sizeof (pi->prstatus));
1180 pi->status_valid = 1;
1184 /* Just read the danged status. Now isn't that simple? */
1185 pi->status_valid = (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1189 if (pi->status_valid)
1191 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1194 proc_get_current_thread (pi));
1197 /* The status struct includes general regs, so mark them valid too */
1198 pi->gregs_valid = pi->status_valid;
1200 /* In the read/write multiple-fd model,
1201 the status struct includes the fp regs too, so mark them valid too */
1202 pi->fpregs_valid = pi->status_valid;
1204 return pi->status_valid; /* True if success, false if failure. */
1208 * Function: proc_flags
1210 * returns the process flags (pr_flags field).
1214 proc_flags (procinfo *pi)
1216 if (!pi->status_valid)
1217 if (!proc_get_status (pi))
1218 return 0; /* FIXME: not a good failure value (but what is?) */
1222 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1223 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1224 The two sets of flags don't overlap. */
1225 return pi->prstatus.pr_flags | pi->prstatus.pr_lwp.pr_flags;
1227 return pi->prstatus.pr_lwp.pr_flags;
1230 return pi->prstatus.pr_flags;
1235 * Function: proc_why
1237 * returns the pr_why field (why the process stopped).
1241 proc_why (procinfo *pi)
1243 if (!pi->status_valid)
1244 if (!proc_get_status (pi))
1245 return 0; /* FIXME: not a good failure value (but what is?) */
1248 return pi->prstatus.pr_lwp.pr_why;
1250 return pi->prstatus.pr_why;
1255 * Function: proc_what
1257 * returns the pr_what field (details of why the process stopped).
1261 proc_what (procinfo *pi)
1263 if (!pi->status_valid)
1264 if (!proc_get_status (pi))
1265 return 0; /* FIXME: not a good failure value (but what is?) */
1268 return pi->prstatus.pr_lwp.pr_what;
1270 return pi->prstatus.pr_what;
1274 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1276 * Function: proc_nsysarg
1278 * returns the pr_nsysarg field (number of args to the current syscall).
1282 proc_nsysarg (procinfo *pi)
1284 if (!pi->status_valid)
1285 if (!proc_get_status (pi))
1289 return pi->prstatus.pr_lwp.pr_nsysarg;
1291 return pi->prstatus.pr_nsysarg;
1296 * Function: proc_sysargs
1298 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1302 proc_sysargs (procinfo *pi)
1304 if (!pi->status_valid)
1305 if (!proc_get_status (pi))
1309 return (long *) &pi->prstatus.pr_lwp.pr_sysarg;
1311 return (long *) &pi->prstatus.pr_sysarg;
1316 * Function: proc_syscall
1318 * returns the pr_syscall field (id of current syscall if we are in one).
1322 proc_syscall (procinfo *pi)
1324 if (!pi->status_valid)
1325 if (!proc_get_status (pi))
1329 return pi->prstatus.pr_lwp.pr_syscall;
1331 return pi->prstatus.pr_syscall;
1334 #endif /* PIOCSSPCACT */
1337 * Function: proc_cursig:
1339 * returns the pr_cursig field (current signal).
1343 proc_cursig (struct procinfo *pi)
1345 if (!pi->status_valid)
1346 if (!proc_get_status (pi))
1347 return 0; /* FIXME: not a good failure value (but what is?) */
1350 return pi->prstatus.pr_lwp.pr_cursig;
1352 return pi->prstatus.pr_cursig;
1357 * Function: proc_modify_flag
1359 * === I appologize for the messiness of this function.
1360 * === This is an area where the different versions of
1361 * === /proc are more inconsistent than usual. MVS
1363 * Set or reset any of the following process flags:
1364 * PR_FORK -- forked child will inherit trace flags
1365 * PR_RLC -- traced process runs when last /proc file closed.
1366 * PR_KLC -- traced process is killed when last /proc file closed.
1367 * PR_ASYNC -- LWP's get to run/stop independently.
1369 * There are three methods for doing this function:
1370 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1372 * 2) Middle: PIOCSET/PIOCRESET
1374 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1377 * Note: Irix does not define PR_ASYNC.
1378 * Note: OSF does not define PR_KLC.
1379 * Note: OSF is the only one that can ONLY use the oldest method.
1382 * pi -- the procinfo
1383 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1384 * mode -- 1 for set, 0 for reset.
1386 * Returns non-zero for success, zero for failure.
1389 enum { FLAG_RESET, FLAG_SET };
1392 proc_modify_flag (procinfo *pi, long flag, long mode)
1394 long win = 0; /* default to fail */
1397 * These operations affect the process as a whole, and applying
1398 * them to an individual LWP has the same meaning as applying them
1399 * to the main process. Therefore, if we're ever called with a
1400 * pointer to an LWP's procinfo, let's substitute the process's
1401 * procinfo and avoid opening the LWP's file descriptor
1406 pi = find_procinfo_or_die (pi->pid, 0);
1408 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1409 /* First normalize the PCUNSET/PCRESET command opcode
1410 (which for no obvious reason has a different definition
1411 from one operating system to the next...) */
1413 #define GDBRESET PCUNSET
1416 #define GDBRESET PCRESET
1420 procfs_ctl_t arg[2];
1422 if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC) */
1424 else /* Reset the flag */
1428 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1431 #ifdef PIOCSET /* Irix/Sol5 method */
1432 if (mode == FLAG_SET) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1434 win = (ioctl (pi->ctl_fd, PIOCSET, &flag) >= 0);
1436 else /* Reset the flag */
1438 win = (ioctl (pi->ctl_fd, PIOCRESET, &flag) >= 0);
1442 #ifdef PIOCSRLC /* Oldest method: OSF */
1445 if (mode == FLAG_SET) /* Set run-on-last-close */
1447 win = (ioctl (pi->ctl_fd, PIOCSRLC, NULL) >= 0);
1449 else /* Clear run-on-last-close */
1451 win = (ioctl (pi->ctl_fd, PIOCRRLC, NULL) >= 0);
1455 if (mode == FLAG_SET) /* Set inherit-on-fork */
1457 win = (ioctl (pi->ctl_fd, PIOCSFORK, NULL) >= 0);
1459 else /* Clear inherit-on-fork */
1461 win = (ioctl (pi->ctl_fd, PIOCRFORK, NULL) >= 0);
1465 win = 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1472 /* The above operation renders the procinfo's cached pstatus obsolete. */
1473 pi->status_valid = 0;
1476 warning (_("procfs: modify_flag failed to turn %s %s"),
1477 flag == PR_FORK ? "PR_FORK" :
1478 flag == PR_RLC ? "PR_RLC" :
1480 flag == PR_ASYNC ? "PR_ASYNC" :
1483 flag == PR_KLC ? "PR_KLC" :
1486 mode == FLAG_RESET ? "off" : "on");
1492 * Function: proc_set_run_on_last_close
1494 * Set the run_on_last_close flag.
1495 * Process with all threads will become runnable
1496 * when debugger closes all /proc fds.
1498 * Returns non-zero for success, zero for failure.
1502 proc_set_run_on_last_close (procinfo *pi)
1504 return proc_modify_flag (pi, PR_RLC, FLAG_SET);
1508 * Function: proc_unset_run_on_last_close
1510 * Reset the run_on_last_close flag.
1511 * Process will NOT become runnable
1512 * when debugger closes its file handles.
1514 * Returns non-zero for success, zero for failure.
1518 proc_unset_run_on_last_close (procinfo *pi)
1520 return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
1525 * Function: proc_set_kill_on_last_close
1527 * Set the kill_on_last_close flag.
1528 * Process with all threads will be killed when debugger
1529 * closes all /proc fds (or debugger exits or dies).
1531 * Returns non-zero for success, zero for failure.
1535 proc_set_kill_on_last_close (procinfo *pi)
1537 return proc_modify_flag (pi, PR_KLC, FLAG_SET);
1541 * Function: proc_unset_kill_on_last_close
1543 * Reset the kill_on_last_close flag.
1544 * Process will NOT be killed when debugger
1545 * closes its file handles (or exits or dies).
1547 * Returns non-zero for success, zero for failure.
1551 proc_unset_kill_on_last_close (procinfo *pi)
1553 return proc_modify_flag (pi, PR_KLC, FLAG_RESET);
1558 * Function: proc_set_inherit_on_fork
1560 * Set inherit_on_fork flag.
1561 * If the process forks a child while we are registered for events
1562 * in the parent, then we will also recieve events from the child.
1564 * Returns non-zero for success, zero for failure.
1568 proc_set_inherit_on_fork (procinfo *pi)
1570 return proc_modify_flag (pi, PR_FORK, FLAG_SET);
1574 * Function: proc_unset_inherit_on_fork
1576 * Reset inherit_on_fork flag.
1577 * If the process forks a child while we are registered for events
1578 * in the parent, then we will NOT recieve events from the child.
1580 * Returns non-zero for success, zero for failure.
1584 proc_unset_inherit_on_fork (procinfo *pi)
1586 return proc_modify_flag (pi, PR_FORK, FLAG_RESET);
1591 * Function: proc_set_async
1593 * Set PR_ASYNC flag.
1594 * If one LWP stops because of a debug event (signal etc.),
1595 * the remaining LWPs will continue to run.
1597 * Returns non-zero for success, zero for failure.
1601 proc_set_async (procinfo *pi)
1603 return proc_modify_flag (pi, PR_ASYNC, FLAG_SET);
1607 * Function: proc_unset_async
1609 * Reset PR_ASYNC flag.
1610 * If one LWP stops because of a debug event (signal etc.),
1611 * then all other LWPs will stop as well.
1613 * Returns non-zero for success, zero for failure.
1617 proc_unset_async (procinfo *pi)
1619 return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET);
1621 #endif /* PR_ASYNC */
1624 * Function: proc_stop_process
1626 * Request the process/LWP to stop. Does not wait.
1627 * Returns non-zero for success, zero for failure.
1631 proc_stop_process (procinfo *pi)
1636 * We might conceivably apply this operation to an LWP, and
1637 * the LWP's ctl file descriptor might not be open.
1640 if (pi->ctl_fd == 0 &&
1641 open_procinfo_files (pi, FD_CTL) == 0)
1646 procfs_ctl_t cmd = PCSTOP;
1647 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1648 #else /* ioctl method */
1649 win = (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) >= 0);
1650 /* Note: the call also reads the prstatus. */
1653 pi->status_valid = 1;
1654 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1657 proc_get_current_thread (pi));
1666 * Function: proc_wait_for_stop
1668 * Wait for the process or LWP to stop (block until it does).
1669 * Returns non-zero for success, zero for failure.
1673 proc_wait_for_stop (procinfo *pi)
1678 * We should never have to apply this operation to any procinfo
1679 * except the one for the main process. If that ever changes
1680 * for any reason, then take out the following clause and
1681 * replace it with one that makes sure the ctl_fd is open.
1685 pi = find_procinfo_or_die (pi->pid, 0);
1689 procfs_ctl_t cmd = PCWSTOP;
1690 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1691 /* We been runnin' and we stopped -- need to update status. */
1692 pi->status_valid = 0;
1694 #else /* ioctl method */
1695 win = (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) >= 0);
1696 /* Above call also refreshes the prstatus. */
1699 pi->status_valid = 1;
1700 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1703 proc_get_current_thread (pi));
1711 * Function: proc_run_process
1713 * Make the process or LWP runnable.
1714 * Options (not all are implemented):
1716 * - clear current fault
1717 * - clear current signal
1718 * - abort the current system call
1719 * - stop as soon as finished with system call
1720 * - (ioctl): set traced signal set
1721 * - (ioctl): set held signal set
1722 * - (ioctl): set traced fault set
1723 * - (ioctl): set start pc (vaddr)
1724 * Always clear the current fault.
1725 * Clear the current signal if 'signo' is zero.
1728 * pi the process or LWP to operate on.
1729 * step if true, set the process or LWP to trap after one instr.
1730 * signo if zero, clear the current signal if any.
1731 * if non-zero, set the current signal to this one.
1733 * Returns non-zero for success, zero for failure.
1737 proc_run_process (procinfo *pi, int step, int signo)
1743 * We will probably have to apply this operation to individual threads,
1744 * so make sure the control file descriptor is open.
1747 if (pi->ctl_fd == 0 &&
1748 open_procinfo_files (pi, FD_CTL) == 0)
1753 runflags = PRCFAULT; /* always clear current fault */
1758 else if (signo != -1) /* -1 means do nothing W.R.T. signals */
1759 proc_set_current_signal (pi, signo);
1763 procfs_ctl_t cmd[2];
1767 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1769 #else /* ioctl method */
1773 memset (&prrun, 0, sizeof (prrun));
1774 prrun.pr_flags = runflags;
1775 win = (ioctl (pi->ctl_fd, PIOCRUN, &prrun) >= 0);
1783 * Function: proc_set_traced_signals
1785 * Register to trace signals in the process or LWP.
1786 * Returns non-zero for success, zero for failure.
1790 proc_set_traced_signals (procinfo *pi, gdb_sigset_t *sigset)
1795 * We should never have to apply this operation to any procinfo
1796 * except the one for the main process. If that ever changes
1797 * for any reason, then take out the following clause and
1798 * replace it with one that makes sure the ctl_fd is open.
1802 pi = find_procinfo_or_die (pi->pid, 0);
1808 /* Use char array to avoid alignment issues. */
1809 char sigset[sizeof (gdb_sigset_t)];
1813 memcpy (&arg.sigset, sigset, sizeof (gdb_sigset_t));
1815 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1817 #else /* ioctl method */
1818 win = (ioctl (pi->ctl_fd, PIOCSTRACE, sigset) >= 0);
1820 /* The above operation renders the procinfo's cached pstatus obsolete. */
1821 pi->status_valid = 0;
1824 warning (_("procfs: set_traced_signals failed"));
1829 * Function: proc_set_traced_faults
1831 * Register to trace hardware faults in the process or LWP.
1832 * Returns non-zero for success, zero for failure.
1836 proc_set_traced_faults (procinfo *pi, fltset_t *fltset)
1841 * We should never have to apply this operation to any procinfo
1842 * except the one for the main process. If that ever changes
1843 * for any reason, then take out the following clause and
1844 * replace it with one that makes sure the ctl_fd is open.
1848 pi = find_procinfo_or_die (pi->pid, 0);
1854 /* Use char array to avoid alignment issues. */
1855 char fltset[sizeof (fltset_t)];
1859 memcpy (&arg.fltset, fltset, sizeof (fltset_t));
1861 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1863 #else /* ioctl method */
1864 win = (ioctl (pi->ctl_fd, PIOCSFAULT, fltset) >= 0);
1866 /* The above operation renders the procinfo's cached pstatus obsolete. */
1867 pi->status_valid = 0;
1873 * Function: proc_set_traced_sysentry
1875 * Register to trace entry to system calls in the process or LWP.
1876 * Returns non-zero for success, zero for failure.
1880 proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset)
1885 * We should never have to apply this operation to any procinfo
1886 * except the one for the main process. If that ever changes
1887 * for any reason, then take out the following clause and
1888 * replace it with one that makes sure the ctl_fd is open.
1892 pi = find_procinfo_or_die (pi->pid, 0);
1896 struct gdb_proc_ctl_pcsentry {
1898 /* Use char array to avoid alignment issues. */
1899 char sysset[sizeof (sysset_t)];
1901 int argp_size = sizeof (struct gdb_proc_ctl_pcsentry)
1903 + sysset_t_size (pi);
1905 argp = xmalloc (argp_size);
1907 argp->cmd = PCSENTRY;
1908 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1910 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1913 #else /* ioctl method */
1914 win = (ioctl (pi->ctl_fd, PIOCSENTRY, sysset) >= 0);
1916 /* The above operation renders the procinfo's cached pstatus obsolete. */
1917 pi->status_valid = 0;
1923 * Function: proc_set_traced_sysexit
1925 * Register to trace exit from system calls in the process or LWP.
1926 * Returns non-zero for success, zero for failure.
1930 proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset)
1935 * We should never have to apply this operation to any procinfo
1936 * except the one for the main process. If that ever changes
1937 * for any reason, then take out the following clause and
1938 * replace it with one that makes sure the ctl_fd is open.
1942 pi = find_procinfo_or_die (pi->pid, 0);
1946 struct gdb_proc_ctl_pcsexit {
1948 /* Use char array to avoid alignment issues. */
1949 char sysset[sizeof (sysset_t)];
1951 int argp_size = sizeof (struct gdb_proc_ctl_pcsexit)
1953 + sysset_t_size (pi);
1955 argp = xmalloc (argp_size);
1957 argp->cmd = PCSEXIT;
1958 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1960 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1963 #else /* ioctl method */
1964 win = (ioctl (pi->ctl_fd, PIOCSEXIT, sysset) >= 0);
1966 /* The above operation renders the procinfo's cached pstatus obsolete. */
1967 pi->status_valid = 0;
1973 * Function: proc_set_held_signals
1975 * Specify the set of blocked / held signals in the process or LWP.
1976 * Returns non-zero for success, zero for failure.
1980 proc_set_held_signals (procinfo *pi, gdb_sigset_t *sighold)
1985 * We should never have to apply this operation to any procinfo
1986 * except the one for the main process. If that ever changes
1987 * for any reason, then take out the following clause and
1988 * replace it with one that makes sure the ctl_fd is open.
1992 pi = find_procinfo_or_die (pi->pid, 0);
1998 /* Use char array to avoid alignment issues. */
1999 char hold[sizeof (gdb_sigset_t)];
2003 memcpy (&arg.hold, sighold, sizeof (gdb_sigset_t));
2004 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2007 win = (ioctl (pi->ctl_fd, PIOCSHOLD, sighold) >= 0);
2009 /* The above operation renders the procinfo's cached pstatus obsolete. */
2010 pi->status_valid = 0;
2016 * Function: proc_get_pending_signals
2018 * returns the set of signals that are pending in the process or LWP.
2019 * Will also copy the sigset if 'save' is non-zero.
2023 proc_get_pending_signals (procinfo *pi, gdb_sigset_t *save)
2025 gdb_sigset_t *ret = NULL;
2028 * We should never have to apply this operation to any procinfo
2029 * except the one for the main process. If that ever changes
2030 * for any reason, then take out the following clause and
2031 * replace it with one that makes sure the ctl_fd is open.
2035 pi = find_procinfo_or_die (pi->pid, 0);
2037 if (!pi->status_valid)
2038 if (!proc_get_status (pi))
2042 ret = &pi->prstatus.pr_lwp.pr_lwppend;
2044 ret = &pi->prstatus.pr_sigpend;
2047 memcpy (save, ret, sizeof (gdb_sigset_t));
2053 * Function: proc_get_signal_actions
2055 * returns the set of signal actions.
2056 * Will also copy the sigactionset if 'save' is non-zero.
2060 proc_get_signal_actions (procinfo *pi, gdb_sigaction_t *save)
2062 gdb_sigaction_t *ret = NULL;
2065 * We should never have to apply this operation to any procinfo
2066 * except the one for the main process. If that ever changes
2067 * for any reason, then take out the following clause and
2068 * replace it with one that makes sure the ctl_fd is open.
2072 pi = find_procinfo_or_die (pi->pid, 0);
2074 if (!pi->status_valid)
2075 if (!proc_get_status (pi))
2079 ret = &pi->prstatus.pr_lwp.pr_action;
2081 ret = &pi->prstatus.pr_action;
2084 memcpy (save, ret, sizeof (gdb_sigaction_t));
2090 * Function: proc_get_held_signals
2092 * returns the set of signals that are held / blocked.
2093 * Will also copy the sigset if 'save' is non-zero.
2097 proc_get_held_signals (procinfo *pi, gdb_sigset_t *save)
2099 gdb_sigset_t *ret = NULL;
2102 * We should never have to apply this operation to any procinfo
2103 * except the one for the main process. If that ever changes
2104 * for any reason, then take out the following clause and
2105 * replace it with one that makes sure the ctl_fd is open.
2109 pi = find_procinfo_or_die (pi->pid, 0);
2112 if (!pi->status_valid)
2113 if (!proc_get_status (pi))
2117 ret = &pi->prstatus.pr_lwp.pr_context.uc_sigmask;
2119 ret = &pi->prstatus.pr_lwp.pr_lwphold;
2120 #endif /* UNIXWARE */
2121 #else /* not NEW_PROC_API */
2123 static gdb_sigset_t sigheld;
2125 if (ioctl (pi->ctl_fd, PIOCGHOLD, &sigheld) >= 0)
2128 #endif /* NEW_PROC_API */
2130 memcpy (save, ret, sizeof (gdb_sigset_t));
2136 * Function: proc_get_traced_signals
2138 * returns the set of signals that are traced / debugged.
2139 * Will also copy the sigset if 'save' is non-zero.
2143 proc_get_traced_signals (procinfo *pi, gdb_sigset_t *save)
2145 gdb_sigset_t *ret = NULL;
2148 * We should never have to apply this operation to any procinfo
2149 * except the one for the main process. If that ever changes
2150 * for any reason, then take out the following clause and
2151 * replace it with one that makes sure the ctl_fd is open.
2155 pi = find_procinfo_or_die (pi->pid, 0);
2158 if (!pi->status_valid)
2159 if (!proc_get_status (pi))
2162 ret = &pi->prstatus.pr_sigtrace;
2165 static gdb_sigset_t sigtrace;
2167 if (ioctl (pi->ctl_fd, PIOCGTRACE, &sigtrace) >= 0)
2172 memcpy (save, ret, sizeof (gdb_sigset_t));
2178 * Function: proc_trace_signal
2180 * Add 'signo' to the set of signals that are traced.
2181 * Returns non-zero for success, zero for failure.
2185 proc_trace_signal (procinfo *pi, int signo)
2190 * We should never have to apply this operation to any procinfo
2191 * except the one for the main process. If that ever changes
2192 * for any reason, then take out the following clause and
2193 * replace it with one that makes sure the ctl_fd is open.
2197 pi = find_procinfo_or_die (pi->pid, 0);
2201 if (proc_get_traced_signals (pi, &temp))
2203 praddset (&temp, signo);
2204 return proc_set_traced_signals (pi, &temp);
2208 return 0; /* failure */
2212 * Function: proc_ignore_signal
2214 * Remove 'signo' from the set of signals that are traced.
2215 * Returns non-zero for success, zero for failure.
2219 proc_ignore_signal (procinfo *pi, int signo)
2224 * We should never have to apply this operation to any procinfo
2225 * except the one for the main process. If that ever changes
2226 * for any reason, then take out the following clause and
2227 * replace it with one that makes sure the ctl_fd is open.
2231 pi = find_procinfo_or_die (pi->pid, 0);
2235 if (proc_get_traced_signals (pi, &temp))
2237 prdelset (&temp, signo);
2238 return proc_set_traced_signals (pi, &temp);
2242 return 0; /* failure */
2246 * Function: proc_get_traced_faults
2248 * returns the set of hardware faults that are traced /debugged.
2249 * Will also copy the faultset if 'save' is non-zero.
2253 proc_get_traced_faults (procinfo *pi, fltset_t *save)
2255 fltset_t *ret = NULL;
2258 * We should never have to apply this operation to any procinfo
2259 * except the one for the main process. If that ever changes
2260 * for any reason, then take out the following clause and
2261 * replace it with one that makes sure the ctl_fd is open.
2265 pi = find_procinfo_or_die (pi->pid, 0);
2268 if (!pi->status_valid)
2269 if (!proc_get_status (pi))
2272 ret = &pi->prstatus.pr_flttrace;
2275 static fltset_t flttrace;
2277 if (ioctl (pi->ctl_fd, PIOCGFAULT, &flttrace) >= 0)
2282 memcpy (save, ret, sizeof (fltset_t));
2288 * Function: proc_get_traced_sysentry
2290 * returns the set of syscalls that are traced /debugged on entry.
2291 * Will also copy the syscall set if 'save' is non-zero.
2295 proc_get_traced_sysentry (procinfo *pi, sysset_t *save)
2297 sysset_t *ret = NULL;
2300 * We should never have to apply this operation to any procinfo
2301 * except the one for the main process. If that ever changes
2302 * for any reason, then take out the following clause and
2303 * replace it with one that makes sure the ctl_fd is open.
2307 pi = find_procinfo_or_die (pi->pid, 0);
2310 if (!pi->status_valid)
2311 if (!proc_get_status (pi))
2314 #ifndef DYNAMIC_SYSCALLS
2315 ret = &pi->prstatus.pr_sysentry;
2316 #else /* DYNAMIC_SYSCALLS */
2318 static sysset_t *sysentry;
2322 sysentry = sysset_t_alloc (pi);
2324 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2326 if (pi->prstatus.pr_sysentry_offset == 0)
2328 gdb_premptysysset (sysentry);
2334 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysentry_offset,
2336 != (off_t) pi->prstatus.pr_sysentry_offset)
2338 size = sysset_t_size (pi);
2339 gdb_premptysysset (sysentry);
2340 rsize = read (pi->status_fd, sysentry, size);
2345 #endif /* DYNAMIC_SYSCALLS */
2346 #else /* !NEW_PROC_API */
2348 static sysset_t sysentry;
2350 if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysentry) >= 0)
2353 #endif /* NEW_PROC_API */
2355 memcpy (save, ret, sysset_t_size (pi));
2361 * Function: proc_get_traced_sysexit
2363 * returns the set of syscalls that are traced /debugged on exit.
2364 * Will also copy the syscall set if 'save' is non-zero.
2368 proc_get_traced_sysexit (procinfo *pi, sysset_t *save)
2370 sysset_t * ret = NULL;
2373 * We should never have to apply this operation to any procinfo
2374 * except the one for the main process. If that ever changes
2375 * for any reason, then take out the following clause and
2376 * replace it with one that makes sure the ctl_fd is open.
2380 pi = find_procinfo_or_die (pi->pid, 0);
2383 if (!pi->status_valid)
2384 if (!proc_get_status (pi))
2387 #ifndef DYNAMIC_SYSCALLS
2388 ret = &pi->prstatus.pr_sysexit;
2389 #else /* DYNAMIC_SYSCALLS */
2391 static sysset_t *sysexit;
2395 sysexit = sysset_t_alloc (pi);
2397 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2399 if (pi->prstatus.pr_sysexit_offset == 0)
2401 gdb_premptysysset (sysexit);
2407 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysexit_offset, SEEK_SET)
2408 != (off_t) pi->prstatus.pr_sysexit_offset)
2410 size = sysset_t_size (pi);
2411 gdb_premptysysset (sysexit);
2412 rsize = read (pi->status_fd, sysexit, size);
2417 #endif /* DYNAMIC_SYSCALLS */
2420 static sysset_t sysexit;
2422 if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysexit) >= 0)
2427 memcpy (save, ret, sysset_t_size (pi));
2433 * Function: proc_clear_current_fault
2435 * The current fault (if any) is cleared; the associated signal
2436 * will not be sent to the process or LWP when it resumes.
2437 * Returns non-zero for success, zero for failure.
2441 proc_clear_current_fault (procinfo *pi)
2446 * We should never have to apply this operation to any procinfo
2447 * except the one for the main process. If that ever changes
2448 * for any reason, then take out the following clause and
2449 * replace it with one that makes sure the ctl_fd is open.
2453 pi = find_procinfo_or_die (pi->pid, 0);
2457 procfs_ctl_t cmd = PCCFAULT;
2458 win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd));
2461 win = (ioctl (pi->ctl_fd, PIOCCFAULT, 0) >= 0);
2468 * Function: proc_set_current_signal
2470 * Set the "current signal" that will be delivered next to the process.
2471 * NOTE: semantics are different from those of KILL.
2472 * This signal will be delivered to the process or LWP
2473 * immediately when it is resumed (even if the signal is held/blocked);
2474 * it will NOT immediately cause another event of interest, and will NOT
2475 * first trap back to the debugger.
2477 * Returns non-zero for success, zero for failure.
2481 proc_set_current_signal (procinfo *pi, int signo)
2486 /* Use char array to avoid alignment issues. */
2487 char sinfo[sizeof (gdb_siginfo_t)];
2489 gdb_siginfo_t *mysinfo;
2492 * We should never have to apply this operation to any procinfo
2493 * except the one for the main process. If that ever changes
2494 * for any reason, then take out the following clause and
2495 * replace it with one that makes sure the ctl_fd is open.
2499 pi = find_procinfo_or_die (pi->pid, 0);
2501 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2502 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2503 * receives a PIOCSSIG with a signal identical to the current signal,
2504 * it messes up the current signal. Work around the kernel bug.
2507 signo == proc_cursig (pi))
2508 return 1; /* I assume this is a success? */
2511 /* The pointer is just a type alias. */
2512 mysinfo = (gdb_siginfo_t *) &arg.sinfo;
2513 mysinfo->si_signo = signo;
2514 mysinfo->si_code = 0;
2515 mysinfo->si_pid = getpid (); /* ?why? */
2516 mysinfo->si_uid = getuid (); /* ?why? */
2520 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2522 win = (ioctl (pi->ctl_fd, PIOCSSIG, (void *) &arg.sinfo) >= 0);
2529 * Function: proc_clear_current_signal
2531 * The current signal (if any) is cleared, and
2532 * is not sent to the process or LWP when it resumes.
2533 * Returns non-zero for success, zero for failure.
2537 proc_clear_current_signal (procinfo *pi)
2542 * We should never have to apply this operation to any procinfo
2543 * except the one for the main process. If that ever changes
2544 * for any reason, then take out the following clause and
2545 * replace it with one that makes sure the ctl_fd is open.
2549 pi = find_procinfo_or_die (pi->pid, 0);
2555 /* Use char array to avoid alignment issues. */
2556 char sinfo[sizeof (gdb_siginfo_t)];
2558 gdb_siginfo_t *mysinfo;
2561 /* The pointer is just a type alias. */
2562 mysinfo = (gdb_siginfo_t *) &arg.sinfo;
2563 mysinfo->si_signo = 0;
2564 mysinfo->si_code = 0;
2565 mysinfo->si_errno = 0;
2566 mysinfo->si_pid = getpid (); /* ?why? */
2567 mysinfo->si_uid = getuid (); /* ?why? */
2569 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2572 win = (ioctl (pi->ctl_fd, PIOCSSIG, 0) >= 0);
2578 /* Return the general-purpose registers for the process or LWP
2579 corresponding to PI. Upon failure, return NULL. */
2582 proc_get_gregs (procinfo *pi)
2584 if (!pi->status_valid || !pi->gregs_valid)
2585 if (!proc_get_status (pi))
2588 /* OK, sorry about the ifdef's. There's three cases instead of two,
2589 because in this case Unixware and Solaris/RW differ. */
2592 # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */
2593 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs;
2595 return &pi->prstatus.pr_lwp.pr_reg;
2598 return &pi->prstatus.pr_reg;
2602 /* Return the general-purpose registers for the process or LWP
2603 corresponding to PI. Upon failure, return NULL. */
2606 proc_get_fpregs (procinfo *pi)
2609 if (!pi->status_valid || !pi->fpregs_valid)
2610 if (!proc_get_status (pi))
2613 # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */
2614 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs;
2616 return &pi->prstatus.pr_lwp.pr_fpreg;
2619 #else /* not NEW_PROC_API */
2620 if (pi->fpregs_valid)
2621 return &pi->fpregset; /* Already got 'em. */
2624 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2633 tid_t pr_error_thread;
2634 tfpregset_t thread_1;
2637 thread_fpregs.pr_count = 1;
2638 thread_fpregs.thread_1.tid = pi->tid;
2641 && ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2643 pi->fpregs_valid = 1;
2644 return &pi->fpregset; /* Got 'em now! */
2646 else if (pi->tid != 0
2647 && ioctl (pi->ctl_fd, PIOCTGFPREG, &thread_fpregs) >= 0)
2649 memcpy (&pi->fpregset, &thread_fpregs.thread_1.pr_fpregs,
2650 sizeof (pi->fpregset));
2651 pi->fpregs_valid = 1;
2652 return &pi->fpregset; /* Got 'em now! */
2659 if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2661 pi->fpregs_valid = 1;
2662 return &pi->fpregset; /* Got 'em now! */
2671 #endif /* NEW_PROC_API */
2674 /* Write the general-purpose registers back to the process or LWP
2675 corresponding to PI. Return non-zero for success, zero for
2679 proc_set_gregs (procinfo *pi)
2681 gdb_gregset_t *gregs;
2684 gregs = proc_get_gregs (pi);
2686 return 0; /* proc_get_regs has already warned. */
2688 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2697 /* Use char array to avoid alignment issues. */
2698 char gregs[sizeof (gdb_gregset_t)];
2702 memcpy (&arg.gregs, gregs, sizeof (arg.gregs));
2703 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2705 win = (ioctl (pi->ctl_fd, PIOCSREG, gregs) >= 0);
2709 /* Policy: writing the registers invalidates our cache. */
2710 pi->gregs_valid = 0;
2714 /* Write the floating-pointer registers back to the process or LWP
2715 corresponding to PI. Return non-zero for success, zero for
2719 proc_set_fpregs (procinfo *pi)
2721 gdb_fpregset_t *fpregs;
2724 fpregs = proc_get_fpregs (pi);
2726 return 0; /* proc_get_fpregs has already warned. */
2728 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2737 /* Use char array to avoid alignment issues. */
2738 char fpregs[sizeof (gdb_fpregset_t)];
2742 memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs));
2743 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2747 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2752 tid_t pr_error_thread;
2753 tfpregset_t thread_1;
2756 thread_fpregs.pr_count = 1;
2757 thread_fpregs.thread_1.tid = pi->tid;
2758 memcpy (&thread_fpregs.thread_1.pr_fpregs, fpregs,
2760 win = (ioctl (pi->ctl_fd, PIOCTSFPREG, &thread_fpregs) >= 0);
2763 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2765 #endif /* NEW_PROC_API */
2768 /* Policy: writing the registers invalidates our cache. */
2769 pi->fpregs_valid = 0;
2774 * Function: proc_kill
2776 * Send a signal to the proc or lwp with the semantics of "kill()".
2777 * Returns non-zero for success, zero for failure.
2781 proc_kill (procinfo *pi, int signo)
2786 * We might conceivably apply this operation to an LWP, and
2787 * the LWP's ctl file descriptor might not be open.
2790 if (pi->ctl_fd == 0 &&
2791 open_procinfo_files (pi, FD_CTL) == 0)
2798 procfs_ctl_t cmd[2];
2802 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
2803 #else /* ioctl method */
2804 /* FIXME: do I need the Alpha OSF fixups present in
2805 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2806 win = (ioctl (pi->ctl_fd, PIOCKILL, &signo) >= 0);
2814 * Function: proc_parent_pid
2816 * Find the pid of the process that started this one.
2817 * Returns the parent process pid, or zero.
2821 proc_parent_pid (procinfo *pi)
2824 * We should never have to apply this operation to any procinfo
2825 * except the one for the main process. If that ever changes
2826 * for any reason, then take out the following clause and
2827 * replace it with one that makes sure the ctl_fd is open.
2831 pi = find_procinfo_or_die (pi->pid, 0);
2833 if (!pi->status_valid)
2834 if (!proc_get_status (pi))
2837 return pi->prstatus.pr_ppid;
2841 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2842 (a.k.a void pointer)! */
2845 procfs_address_to_host_pointer (CORE_ADDR addr)
2849 gdb_assert (sizeof (ptr) == TYPE_LENGTH (builtin_type_void_data_ptr));
2850 ADDRESS_TO_POINTER (builtin_type_void_data_ptr, &ptr, addr);
2855 * Function: proc_set_watchpoint
2860 proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags)
2862 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2865 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2866 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2871 char watch[sizeof (prwatch_t)];
2875 pwatch = (prwatch_t *) &arg.watch;
2876 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2877 convert a target address into something that can be stored in a
2878 native data structure. */
2879 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2880 pwatch->pr_vaddr = (uintptr_t) procfs_address_to_host_pointer (addr);
2882 pwatch->pr_vaddr = (caddr_t) procfs_address_to_host_pointer (addr);
2884 pwatch->pr_size = len;
2885 pwatch->pr_wflags = wflags;
2886 #if defined(NEW_PROC_API) && defined (PCWATCH)
2888 return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg));
2890 #if defined (PIOCSWATCH)
2891 return (ioctl (pi->ctl_fd, PIOCSWATCH, pwatch) >= 0);
2893 return 0; /* Fail */
2900 #ifdef TM_I386SOL2_H /* Is it hokey to use this? */
2902 #include <sys/sysi86.h>
2905 * Function: proc_get_LDT_entry
2911 * The 'key' is actually the value of the lower 16 bits of
2912 * the GS register for the LWP that we're interested in.
2914 * Return: matching ssh struct (LDT entry).
2918 proc_get_LDT_entry (procinfo *pi, int key)
2920 static struct ssd *ldt_entry = NULL;
2922 char pathname[MAX_PROC_NAME_SIZE];
2923 struct cleanup *old_chain = NULL;
2926 /* Allocate space for one LDT entry.
2927 This alloc must persist, because we return a pointer to it. */
2928 if (ldt_entry == NULL)
2929 ldt_entry = (struct ssd *) xmalloc (sizeof (struct ssd));
2931 /* Open the file descriptor for the LDT table. */
2932 sprintf (pathname, "/proc/%d/ldt", pi->pid);
2933 if ((fd = open_with_retry (pathname, O_RDONLY)) < 0)
2935 proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__);
2938 /* Make sure it gets closed again! */
2939 old_chain = make_cleanup_close (fd);
2941 /* Now 'read' thru the table, find a match and return it. */
2942 while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd))
2944 if (ldt_entry->sel == 0 &&
2945 ldt_entry->bo == 0 &&
2946 ldt_entry->acc1 == 0 &&
2947 ldt_entry->acc2 == 0)
2948 break; /* end of table */
2949 /* If key matches, return this entry. */
2950 if (ldt_entry->sel == key)
2953 /* Loop ended, match not found. */
2957 static int nalloc = 0;
2959 /* Get the number of LDT entries. */
2960 if (ioctl (pi->ctl_fd, PIOCNLDT, &nldt) < 0)
2962 proc_warn (pi, "proc_get_LDT_entry (PIOCNLDT)", __LINE__);
2966 /* Allocate space for the number of LDT entries. */
2967 /* This alloc has to persist, 'cause we return a pointer to it. */
2970 ldt_entry = (struct ssd *)
2971 xrealloc (ldt_entry, (nldt + 1) * sizeof (struct ssd));
2975 /* Read the whole table in one gulp. */
2976 if (ioctl (pi->ctl_fd, PIOCLDT, ldt_entry) < 0)
2978 proc_warn (pi, "proc_get_LDT_entry (PIOCLDT)", __LINE__);
2982 /* Search the table and return the (first) entry matching 'key'. */
2983 for (i = 0; i < nldt; i++)
2984 if (ldt_entry[i].sel == key)
2985 return &ldt_entry[i];
2987 /* Loop ended, match not found. */
2992 #endif /* TM_I386SOL2_H */
2994 /* =============== END, non-thread part of /proc "MODULE" =============== */
2996 /* =================== Thread "MODULE" =================== */
2998 /* NOTE: you'll see more ifdefs and duplication of functions here,
2999 since there is a different way to do threads on every OS. */
3002 * Function: proc_get_nthreads
3004 * Return the number of threads for the process
3007 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3012 proc_get_nthreads (procinfo *pi)
3016 if (ioctl (pi->ctl_fd, PIOCNTHR, &nthreads) < 0)
3017 proc_warn (pi, "procfs: PIOCNTHR failed", __LINE__);
3023 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3025 * Solaris and Unixware version
3028 proc_get_nthreads (procinfo *pi)
3030 if (!pi->status_valid)
3031 if (!proc_get_status (pi))
3035 * NEW_PROC_API: only works for the process procinfo,
3036 * because the LWP procinfos do not get prstatus filled in.
3039 if (pi->tid != 0) /* find the parent process procinfo */
3040 pi = find_procinfo_or_die (pi->pid, 0);
3042 return pi->prstatus.pr_nlwp;
3050 proc_get_nthreads (procinfo *pi)
3058 * Function: proc_get_current_thread (LWP version)
3060 * Return the ID of the thread that had an event of interest.
3061 * (ie. the one that hit a breakpoint or other traced event).
3062 * All other things being equal, this should be the ID of a
3063 * thread that is currently executing.
3066 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3068 * Solaris and Unixware version
3071 proc_get_current_thread (procinfo *pi)
3074 * Note: this should be applied to the root procinfo for the process,
3075 * not to the procinfo for an LWP. If applied to the procinfo for
3076 * an LWP, it will simply return that LWP's ID. In that case,
3077 * find the parent process procinfo.
3081 pi = find_procinfo_or_die (pi->pid, 0);
3083 if (!pi->status_valid)
3084 if (!proc_get_status (pi))
3088 return pi->prstatus.pr_lwp.pr_lwpid;
3090 return pi->prstatus.pr_who;
3095 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3100 proc_get_current_thread (procinfo *pi)
3102 #if 0 /* FIXME: not ready for prime time? */
3103 return pi->prstatus.pr_tid;
3114 proc_get_current_thread (procinfo *pi)
3123 * Function: proc_update_threads
3125 * Discover the IDs of all the threads within the process, and
3126 * create a procinfo for each of them (chained to the parent).
3128 * This unfortunately requires a different method on every OS.
3130 * Return: non-zero for success, zero for failure.
3134 proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore)
3136 if (thread && parent) /* sanity */
3138 thread->status_valid = 0;
3139 if (!proc_get_status (thread))
3140 destroy_one_procinfo (&parent->thread_list, thread);
3142 return 0; /* keep iterating */
3145 #if defined (PIOCLSTATUS)
3147 * Solaris 2.5 (ioctl) version
3150 proc_update_threads (procinfo *pi)
3152 gdb_prstatus_t *prstatus;
3153 struct cleanup *old_chain = NULL;
3158 * We should never have to apply this operation to any procinfo
3159 * except the one for the main process. If that ever changes
3160 * for any reason, then take out the following clause and
3161 * replace it with one that makes sure the ctl_fd is open.
3165 pi = find_procinfo_or_die (pi->pid, 0);
3167 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3169 if ((nlwp = proc_get_nthreads (pi)) <= 1)
3170 return 1; /* Process is not multi-threaded; nothing to do. */
3172 prstatus = xmalloc (sizeof (gdb_prstatus_t) * (nlwp + 1));
3174 old_chain = make_cleanup (xfree, prstatus);
3175 if (ioctl (pi->ctl_fd, PIOCLSTATUS, prstatus) < 0)
3176 proc_error (pi, "update_threads (PIOCLSTATUS)", __LINE__);
3178 /* Skip element zero, which represents the process as a whole. */
3179 for (i = 1; i < nlwp + 1; i++)
3181 if ((thread = create_procinfo (pi->pid, prstatus[i].pr_who)) == NULL)
3182 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3184 memcpy (&thread->prstatus, &prstatus[i], sizeof (*prstatus));
3185 thread->status_valid = 1;
3187 pi->threads_valid = 1;
3188 do_cleanups (old_chain);
3194 * Unixware and Solaris 6 (and later) version
3197 do_closedir_cleanup (void *dir)
3203 proc_update_threads (procinfo *pi)
3205 char pathname[MAX_PROC_NAME_SIZE + 16];
3206 struct dirent *direntry;
3207 struct cleanup *old_chain = NULL;
3213 * We should never have to apply this operation to any procinfo
3214 * except the one for the main process. If that ever changes
3215 * for any reason, then take out the following clause and
3216 * replace it with one that makes sure the ctl_fd is open.
3220 pi = find_procinfo_or_die (pi->pid, 0);
3222 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3227 * Note: this brute-force method is the only way I know of
3228 * to accomplish this task on Unixware. This method will
3229 * also work on Solaris 2.6 and 2.7. There is a much simpler
3230 * and more elegant way to do this on Solaris, but the margins
3231 * of this manuscript are too small to write it here... ;-)
3234 strcpy (pathname, pi->pathname);
3235 strcat (pathname, "/lwp");
3236 if ((dirp = opendir (pathname)) == NULL)
3237 proc_error (pi, "update_threads, opendir", __LINE__);
3239 old_chain = make_cleanup (do_closedir_cleanup, dirp);
3240 while ((direntry = readdir (dirp)) != NULL)
3241 if (direntry->d_name[0] != '.') /* skip '.' and '..' */
3243 lwpid = atoi (&direntry->d_name[0]);
3244 if ((thread = create_procinfo (pi->pid, lwpid)) == NULL)
3245 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3247 pi->threads_valid = 1;
3248 do_cleanups (old_chain);
3257 proc_update_threads (procinfo *pi)
3263 * We should never have to apply this operation to any procinfo
3264 * except the one for the main process. If that ever changes
3265 * for any reason, then take out the following clause and
3266 * replace it with one that makes sure the ctl_fd is open.
3270 pi = find_procinfo_or_die (pi->pid, 0);
3272 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3274 nthreads = proc_get_nthreads (pi);
3276 return 0; /* nothing to do for 1 or fewer threads */
3278 threads = xmalloc (nthreads * sizeof (tid_t));
3280 if (ioctl (pi->ctl_fd, PIOCTLIST, threads) < 0)
3281 proc_error (pi, "procfs: update_threads (PIOCTLIST)", __LINE__);
3283 for (i = 0; i < nthreads; i++)
3285 if (!find_procinfo (pi->pid, threads[i]))
3286 if (!create_procinfo (pi->pid, threads[i]))
3287 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3289 pi->threads_valid = 1;
3297 proc_update_threads (procinfo *pi)
3301 #endif /* OSF PIOCTLIST */
3302 #endif /* NEW_PROC_API */
3303 #endif /* SOL 2.5 PIOCLSTATUS */
3306 * Function: proc_iterate_over_threads
3309 * Given a pointer to a function, call that function once
3310 * for each lwp in the procinfo list, until the function
3311 * returns non-zero, in which event return the value
3312 * returned by the function.
3314 * Note: this function does NOT call update_threads.
3315 * If you want to discover new threads first, you must
3316 * call that function explicitly. This function just makes
3317 * a quick pass over the currently-known procinfos.
3320 * pi - parent process procinfo
3321 * func - per-thread function
3322 * ptr - opaque parameter for function.
3325 * First non-zero return value from the callee, or zero.
3329 proc_iterate_over_threads (procinfo *pi,
3330 int (*func) (procinfo *, procinfo *, void *),
3333 procinfo *thread, *next;
3337 * We should never have to apply this operation to any procinfo
3338 * except the one for the main process. If that ever changes
3339 * for any reason, then take out the following clause and
3340 * replace it with one that makes sure the ctl_fd is open.
3344 pi = find_procinfo_or_die (pi->pid, 0);
3346 for (thread = pi->thread_list; thread != NULL; thread = next)
3348 next = thread->next; /* in case thread is destroyed */
3349 if ((retval = (*func) (pi, thread, ptr)) != 0)
3356 /* =================== END, Thread "MODULE" =================== */
3358 /* =================== END, /proc "MODULE" =================== */
3360 /* =================== GDB "MODULE" =================== */
3363 * Here are all of the gdb target vector functions and their friends.
3366 static ptid_t do_attach (ptid_t ptid);
3367 static void do_detach (int signo);
3368 static int register_gdb_signals (procinfo *, gdb_sigset_t *);
3369 static void proc_trace_syscalls_1 (procinfo *pi, int syscallnum,
3370 int entry_or_exit, int mode, int from_tty);
3371 static int insert_dbx_link_breakpoint (procinfo *pi);
3372 static void remove_dbx_link_breakpoint (void);
3374 /* On mips-irix, we need to insert a breakpoint at __dbx_link during
3375 the startup phase. The following two variables are used to record
3376 the address of the breakpoint, and the code that was replaced by
3378 static int dbx_link_bpt_addr = 0;
3379 static void *dbx_link_bpt;
3382 * Function: procfs_debug_inferior
3384 * Sets up the inferior to be debugged.
3385 * Registers to trace signals, hardware faults, and syscalls.
3386 * Note: does not set RLC flag: caller may want to customize that.
3388 * Returns: zero for success (note! unlike most functions in this module)
3389 * On failure, returns the LINE NUMBER where it failed!
3393 procfs_debug_inferior (procinfo *pi)
3395 fltset_t traced_faults;
3396 gdb_sigset_t traced_signals;
3397 sysset_t *traced_syscall_entries;
3398 sysset_t *traced_syscall_exits;
3401 #ifdef PROCFS_DONT_TRACE_FAULTS
3402 /* On some systems (OSF), we don't trace hardware faults.
3403 Apparently it's enough that we catch them as signals.
3404 Wonder why we don't just do that in general? */
3405 premptyset (&traced_faults); /* don't trace faults. */
3407 /* Register to trace hardware faults in the child. */
3408 prfillset (&traced_faults); /* trace all faults... */
3409 prdelset (&traced_faults, FLTPAGE); /* except page fault. */
3411 if (!proc_set_traced_faults (pi, &traced_faults))
3414 /* Register to trace selected signals in the child. */
3415 premptyset (&traced_signals);
3416 if (!register_gdb_signals (pi, &traced_signals))
3420 /* Register to trace the 'exit' system call (on entry). */
3421 traced_syscall_entries = sysset_t_alloc (pi);
3422 gdb_premptysysset (traced_syscall_entries);
3424 gdb_praddsysset (traced_syscall_entries, SYS_exit);
3427 gdb_praddsysset (traced_syscall_entries, SYS_lwpexit); /* And _lwp_exit... */
3430 gdb_praddsysset (traced_syscall_entries, SYS_lwp_exit);
3432 #ifdef DYNAMIC_SYSCALLS
3434 int callnum = find_syscall (pi, "_exit");
3436 gdb_praddsysset (traced_syscall_entries, callnum);
3440 status = proc_set_traced_sysentry (pi, traced_syscall_entries);
3441 xfree (traced_syscall_entries);
3445 #ifdef PRFS_STOPEXEC /* defined on OSF */
3446 /* OSF method for tracing exec syscalls. Quoting:
3447 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3448 exits from exec system calls because of the user level loader. */
3449 /* FIXME: make nice and maybe move into an access function. */
3453 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
3456 prfs_flags |= PRFS_STOPEXEC;
3458 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
3461 #else /* not PRFS_STOPEXEC */
3462 /* Everyone else's (except OSF) method for tracing exec syscalls */
3464 Not all systems with /proc have all the exec* syscalls with the same
3465 names. On the SGI, for example, there is no SYS_exec, but there
3466 *is* a SYS_execv. So, we try to account for that. */
3468 traced_syscall_exits = sysset_t_alloc (pi);
3469 gdb_premptysysset (traced_syscall_exits);
3471 gdb_praddsysset (traced_syscall_exits, SYS_exec);
3474 gdb_praddsysset (traced_syscall_exits, SYS_execve);
3477 gdb_praddsysset (traced_syscall_exits, SYS_execv);
3480 #ifdef SYS_lwpcreate
3481 gdb_praddsysset (traced_syscall_exits, SYS_lwpcreate);
3482 gdb_praddsysset (traced_syscall_exits, SYS_lwpexit);
3485 #ifdef SYS_lwp_create /* FIXME: once only, please */
3486 gdb_praddsysset (traced_syscall_exits, SYS_lwp_create);
3487 gdb_praddsysset (traced_syscall_exits, SYS_lwp_exit);
3490 #ifdef DYNAMIC_SYSCALLS
3492 int callnum = find_syscall (pi, "execve");
3494 gdb_praddsysset (traced_syscall_exits, callnum);
3495 callnum = find_syscall (pi, "ra_execve");
3497 gdb_praddsysset (traced_syscall_exits, callnum);
3501 status = proc_set_traced_sysexit (pi, traced_syscall_exits);
3502 xfree (traced_syscall_exits);
3506 #endif /* PRFS_STOPEXEC */
3511 procfs_attach (char *args, int from_tty)
3517 error_no_arg (_("process-id to attach"));
3520 if (pid == getpid ())
3521 error (_("Attaching GDB to itself is not a good idea..."));
3525 exec_file = get_exec_file (0);
3528 printf_filtered (_("Attaching to program `%s', %s\n"),
3529 exec_file, target_pid_to_str (pid_to_ptid (pid)));
3531 printf_filtered (_("Attaching to %s\n"),
3532 target_pid_to_str (pid_to_ptid (pid)));
3536 inferior_ptid = do_attach (pid_to_ptid (pid));
3537 push_target (&procfs_ops);
3541 procfs_detach (char *args, int from_tty)
3550 int pid = PIDGET (inferior_ptid);
3553 exec_file = get_exec_file (0);
3554 if (exec_file == NULL)
3557 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file,
3558 target_pid_to_str (pid_to_ptid (pid)));
3559 gdb_flush (gdb_stdout);
3564 inferior_ptid = null_ptid;
3565 unpush_target (&procfs_ops);
3569 do_attach (ptid_t ptid)
3574 if ((pi = create_procinfo (PIDGET (ptid), 0)) == NULL)
3575 perror (_("procfs: out of memory in 'attach'"));
3577 if (!open_procinfo_files (pi, FD_CTL))
3579 fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__);
3580 sprintf (errmsg, "do_attach: couldn't open /proc file for process %d",
3582 dead_procinfo (pi, errmsg, NOKILL);
3585 /* Stop the process (if it isn't already stopped). */
3586 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
3588 pi->was_stopped = 1;
3589 proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
3593 pi->was_stopped = 0;
3594 /* Set the process to run again when we close it. */
3595 if (!proc_set_run_on_last_close (pi))
3596 dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
3598 /* Now stop the process. */
3599 if (!proc_stop_process (pi))
3600 dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
3601 pi->ignore_next_sigstop = 1;
3603 /* Save some of the /proc state to be restored if we detach. */
3604 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
3605 dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
3606 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
3607 dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
3608 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
3609 dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
3611 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
3612 dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
3614 if (!proc_get_held_signals (pi, &pi->saved_sighold))
3615 dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
3617 if ((fail = procfs_debug_inferior (pi)) != 0)
3618 dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
3620 /* Let GDB know that the inferior was attached. */
3622 return MERGEPID (pi->pid, proc_get_current_thread (pi));
3626 do_detach (int signo)
3630 /* Find procinfo for the main process */
3631 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); /* FIXME: threads */
3633 if (!proc_set_current_signal (pi, signo))
3634 proc_warn (pi, "do_detach, set_current_signal", __LINE__);
3636 if (!proc_set_traced_signals (pi, &pi->saved_sigset))
3637 proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
3639 if (!proc_set_traced_faults (pi, &pi->saved_fltset))
3640 proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
3642 if (!proc_set_traced_sysentry (pi, pi->saved_entryset))
3643 proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
3645 if (!proc_set_traced_sysexit (pi, pi->saved_exitset))
3646 proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
3648 if (!proc_set_held_signals (pi, &pi->saved_sighold))
3649 proc_warn (pi, "do_detach, set_held_signals", __LINE__);
3651 if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)))
3652 if (signo || !(pi->was_stopped) ||
3653 query (_("Was stopped when attached, make it runnable again? ")))
3655 /* Clear any pending signal. */
3656 if (!proc_clear_current_fault (pi))
3657 proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
3659 if (signo == 0 && !proc_clear_current_signal (pi))
3660 proc_warn (pi, "do_detach, clear_current_signal", __LINE__);
3662 if (!proc_set_run_on_last_close (pi))
3663 proc_warn (pi, "do_detach, set_rlc", __LINE__);
3667 destroy_procinfo (pi);
3670 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
3673 ??? Is the following note still relevant? We can't get individual
3674 registers with the PT_GETREGS ptrace(2) request either, yet we
3675 don't bother with caching at all in that case.
3677 NOTE: Since the /proc interface cannot give us individual
3678 registers, we pay no attention to REGNUM, and just fetch them all.
3679 This results in the possibility that we will do unnecessarily many
3680 fetches, since we may be called repeatedly for individual
3681 registers. So we cache the results, and mark the cache invalid
3682 when the process is resumed. */
3685 procfs_fetch_registers (struct regcache *regcache, int regnum)
3687 gdb_gregset_t *gregs;
3689 int pid = PIDGET (inferior_ptid);
3690 int tid = TIDGET (inferior_ptid);
3692 /* First look up procinfo for the main process. */
3693 pi = find_procinfo_or_die (pid, 0);
3695 /* If the event thread is not the same as GDB's requested thread
3696 (ie. inferior_ptid), then look up procinfo for the requested
3698 if (tid != 0 && tid != proc_get_current_thread (pi))
3699 pi = find_procinfo_or_die (pid, tid);
3702 error (_("procfs: fetch_registers failed to find procinfo for %s"),
3703 target_pid_to_str (inferior_ptid));
3705 gregs = proc_get_gregs (pi);
3707 proc_error (pi, "fetch_registers, get_gregs", __LINE__);
3709 supply_gregset (regcache, (const gdb_gregset_t *) gregs);
3711 if (FP0_REGNUM >= 0) /* Do we have an FPU? */
3713 gdb_fpregset_t *fpregs;
3715 if ((regnum >= 0 && regnum < FP0_REGNUM)
3716 || regnum == PC_REGNUM
3717 || regnum == SP_REGNUM)
3718 return; /* Not a floating point register. */
3720 fpregs = proc_get_fpregs (pi);
3722 proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
3724 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregs);
3728 /* Get ready to modify the registers array. On machines which store
3729 individual registers, this doesn't need to do anything. On
3730 machines which store all the registers in one fell swoop, such as
3731 /proc, this makes sure that registers contains all the registers
3732 from the program being debugged. */
3735 procfs_prepare_to_store (struct regcache *regcache)
3739 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
3740 this for all registers.
3742 NOTE: Since the /proc interface will not read individual registers,
3743 we will cache these requests until the process is resumed, and only
3744 then write them back to the inferior process.
3746 FIXME: is that a really bad idea? Have to think about cases where
3747 writing one register might affect the value of others, etc. */
3750 procfs_store_registers (struct regcache *regcache, int regnum)
3752 gdb_gregset_t *gregs;
3754 int pid = PIDGET (inferior_ptid);
3755 int tid = TIDGET (inferior_ptid);
3757 /* First find procinfo for main process. */
3758 pi = find_procinfo_or_die (pid, 0);
3760 /* If the event thread is not the same as GDB's requested thread
3761 (ie. inferior_ptid), then look up procinfo for the requested
3763 if (tid != 0 && tid != proc_get_current_thread (pi))
3764 pi = find_procinfo_or_die (pid, tid);
3767 error (_("procfs: store_registers: failed to find procinfo for %s"),
3768 target_pid_to_str (inferior_ptid));
3770 gregs = proc_get_gregs (pi);
3772 proc_error (pi, "store_registers, get_gregs", __LINE__);
3774 fill_gregset (regcache, gregs, regnum);
3775 if (!proc_set_gregs (pi))
3776 proc_error (pi, "store_registers, set_gregs", __LINE__);
3778 if (FP0_REGNUM >= 0) /* Do we have an FPU? */
3780 gdb_fpregset_t *fpregs;
3782 if ((regnum >= 0 && regnum < FP0_REGNUM)
3783 || regnum == PC_REGNUM
3784 || regnum == SP_REGNUM)
3785 return; /* Not a floating point register. */
3787 fpregs = proc_get_fpregs (pi);
3789 proc_error (pi, "store_registers, get_fpregs", __LINE__);
3791 fill_fpregset (regcache, fpregs, regnum);
3792 if (!proc_set_fpregs (pi))
3793 proc_error (pi, "store_registers, set_fpregs", __LINE__);
3798 syscall_is_lwp_exit (procinfo *pi, int scall)
3802 if (scall == SYS_lwp_exit)
3806 if (scall == SYS_lwpexit)
3813 syscall_is_exit (procinfo *pi, int scall)
3816 if (scall == SYS_exit)
3819 #ifdef DYNAMIC_SYSCALLS
3820 if (find_syscall (pi, "_exit") == scall)
3827 syscall_is_exec (procinfo *pi, int scall)
3830 if (scall == SYS_exec)
3834 if (scall == SYS_execv)
3838 if (scall == SYS_execve)
3841 #ifdef DYNAMIC_SYSCALLS
3842 if (find_syscall (pi, "_execve"))
3844 if (find_syscall (pi, "ra_execve"))
3851 syscall_is_lwp_create (procinfo *pi, int scall)
3853 #ifdef SYS_lwp_create
3854 if (scall == SYS_lwp_create)
3857 #ifdef SYS_lwpcreate
3858 if (scall == SYS_lwpcreate)
3865 * Function: target_wait
3867 * Retrieve the next stop event from the child process.
3868 * If child has not stopped yet, wait for it to stop.
3869 * Translate /proc eventcodes (or possibly wait eventcodes)
3870 * into gdb internal event codes.
3872 * Return: id of process (and possibly thread) that incurred the event.
3873 * event codes are returned thru a pointer parameter.
3877 procfs_wait (ptid_t ptid, struct target_waitstatus *status)
3879 /* First cut: loosely based on original version 2.1 */
3883 ptid_t retval, temp_ptid;
3884 int why, what, flags;
3891 retval = pid_to_ptid (-1);
3893 /* Find procinfo for main process */
3894 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
3897 /* We must assume that the status is stale now... */
3898 pi->status_valid = 0;
3899 pi->gregs_valid = 0;
3900 pi->fpregs_valid = 0;
3902 #if 0 /* just try this out... */
3903 flags = proc_flags (pi);
3904 why = proc_why (pi);
3905 if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
3906 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
3908 /* If child is not stopped, wait for it to stop. */
3909 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) &&
3910 !proc_wait_for_stop (pi))
3912 /* wait_for_stop failed: has the child terminated? */
3913 if (errno == ENOENT)
3917 /* /proc file not found; presumably child has terminated. */
3918 wait_retval = wait (&wstat); /* "wait" for the child's exit */
3920 if (wait_retval != PIDGET (inferior_ptid)) /* wrong child? */
3921 error (_("procfs: couldn't stop process %d: wait returned %d."),
3922 PIDGET (inferior_ptid), wait_retval);
3923 /* FIXME: might I not just use waitpid?
3924 Or try find_procinfo to see if I know about this child? */
3925 retval = pid_to_ptid (wait_retval);
3927 else if (errno == EINTR)
3931 /* Unknown error from wait_for_stop. */
3932 proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
3937 /* This long block is reached if either:
3938 a) the child was already stopped, or
3939 b) we successfully waited for the child with wait_for_stop.
3940 This block will analyze the /proc status, and translate it
3941 into a waitstatus for GDB.
3943 If we actually had to call wait because the /proc file
3944 is gone (child terminated), then we skip this block,
3945 because we already have a waitstatus. */
3947 flags = proc_flags (pi);
3948 why = proc_why (pi);
3949 what = proc_what (pi);
3951 if (flags & (PR_STOPPED | PR_ISTOP))
3954 /* If it's running async (for single_thread control),
3955 set it back to normal again. */
3956 if (flags & PR_ASYNC)
3957 if (!proc_unset_async (pi))
3958 proc_error (pi, "target_wait, unset_async", __LINE__);
3962 proc_prettyprint_why (why, what, 1);
3964 /* The 'pid' we will return to GDB is composed of
3965 the process ID plus the lwp ID. */
3966 retval = MERGEPID (pi->pid, proc_get_current_thread (pi));
3970 wstat = (what << 8) | 0177;
3973 if (syscall_is_lwp_exit (pi, what))
3975 printf_filtered (_("[%s exited]\n"),
3976 target_pid_to_str (retval));
3977 delete_thread (retval);
3978 status->kind = TARGET_WAITKIND_SPURIOUS;
3981 else if (syscall_is_exit (pi, what))
3983 /* Handle SYS_exit call only */
3984 /* Stopped at entry to SYS_exit.
3985 Make it runnable, resume it, then use
3986 the wait system call to get its exit code.
3987 Proc_run_process always clears the current
3989 Then return its exit status. */
3990 pi->status_valid = 0;
3992 /* FIXME: what we should do is return
3993 TARGET_WAITKIND_SPURIOUS. */
3994 if (!proc_run_process (pi, 0, 0))
3995 proc_error (pi, "target_wait, run_process", __LINE__);
3998 /* Don't call wait: simulate waiting for exit,
3999 return a "success" exit code. Bogus: what if
4000 it returns something else? */
4002 retval = inferior_ptid; /* ? ? ? */
4006 int temp = wait (&wstat);
4008 /* FIXME: shouldn't I make sure I get the right
4009 event from the right process? If (for
4010 instance) I have killed an earlier inferior
4011 process but failed to clean up after it
4012 somehow, I could get its termination event
4015 /* If wait returns -1, that's what we return to GDB. */
4017 retval = pid_to_ptid (temp);
4022 printf_filtered (_("procfs: trapped on entry to "));
4023 proc_prettyprint_syscall (proc_what (pi), 0);
4024 printf_filtered ("\n");
4027 long i, nsysargs, *sysargs;
4029 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4030 (sysargs = proc_sysargs (pi)) != NULL)
4032 printf_filtered (_("%ld syscall arguments:\n"), nsysargs);
4033 for (i = 0; i < nsysargs; i++)
4034 printf_filtered ("#%ld: 0x%08lx\n",
4042 /* How to exit gracefully, returning "unknown event" */
4043 status->kind = TARGET_WAITKIND_SPURIOUS;
4044 return inferior_ptid;
4048 /* How to keep going without returning to wfi: */
4049 target_resume (ptid, 0, TARGET_SIGNAL_0);
4055 if (syscall_is_exec (pi, what))
4057 /* Hopefully this is our own "fork-child" execing
4058 the real child. Hoax this event into a trap, and
4059 GDB will see the child about to execute its start
4061 wstat = (SIGTRAP << 8) | 0177;
4064 else if (what == SYS_syssgi)
4066 /* see if we can break on dbx_link(). If yes, then
4067 we no longer need the SYS_syssgi notifications. */
4068 if (insert_dbx_link_breakpoint (pi))
4069 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT,
4072 /* This is an internal event and should be transparent
4073 to wfi, so resume the execution and wait again. See
4074 comment in procfs_init_inferior() for more details. */
4075 target_resume (ptid, 0, TARGET_SIGNAL_0);
4079 else if (syscall_is_lwp_create (pi, what))
4082 * This syscall is somewhat like fork/exec.
4083 * We will get the event twice: once for the parent LWP,
4084 * and once for the child. We should already know about
4085 * the parent LWP, but the child will be new to us. So,
4086 * whenever we get this event, if it represents a new
4087 * thread, simply add the thread to the list.
4090 /* If not in procinfo list, add it. */
4091 temp_tid = proc_get_current_thread (pi);
4092 if (!find_procinfo (pi->pid, temp_tid))
4093 create_procinfo (pi->pid, temp_tid);
4095 temp_ptid = MERGEPID (pi->pid, temp_tid);
4096 /* If not in GDB's thread list, add it. */
4097 if (!in_thread_list (temp_ptid))
4099 printf_filtered (_("[New %s]\n"),
4100 target_pid_to_str (temp_ptid));
4101 add_thread (temp_ptid);
4103 /* Return to WFI, but tell it to immediately resume. */
4104 status->kind = TARGET_WAITKIND_SPURIOUS;
4105 return inferior_ptid;
4107 else if (syscall_is_lwp_exit (pi, what))
4109 printf_filtered (_("[%s exited]\n"),
4110 target_pid_to_str (retval));
4111 delete_thread (retval);
4112 status->kind = TARGET_WAITKIND_SPURIOUS;
4117 /* FIXME: Do we need to handle SYS_sproc,
4118 SYS_fork, or SYS_vfork here? The old procfs
4119 seemed to use this event to handle threads on
4120 older (non-LWP) systems, where I'm assuming
4121 that threads were actually separate processes.
4122 Irix, maybe? Anyway, low priority for now. */
4126 printf_filtered (_("procfs: trapped on exit from "));
4127 proc_prettyprint_syscall (proc_what (pi), 0);
4128 printf_filtered ("\n");
4131 long i, nsysargs, *sysargs;
4133 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4134 (sysargs = proc_sysargs (pi)) != NULL)
4136 printf_filtered (_("%ld syscall arguments:\n"), nsysargs);
4137 for (i = 0; i < nsysargs; i++)
4138 printf_filtered ("#%ld: 0x%08lx\n",
4143 status->kind = TARGET_WAITKIND_SPURIOUS;
4144 return inferior_ptid;
4149 wstat = (SIGSTOP << 8) | 0177;
4154 printf_filtered (_("Retry #%d:\n"), retry);
4155 pi->status_valid = 0;
4160 /* If not in procinfo list, add it. */
4161 temp_tid = proc_get_current_thread (pi);
4162 if (!find_procinfo (pi->pid, temp_tid))
4163 create_procinfo (pi->pid, temp_tid);
4165 /* If not in GDB's thread list, add it. */
4166 temp_ptid = MERGEPID (pi->pid, temp_tid);
4167 if (!in_thread_list (temp_ptid))
4169 printf_filtered (_("[New %s]\n"),
4170 target_pid_to_str (temp_ptid));
4171 add_thread (temp_ptid);
4174 status->kind = TARGET_WAITKIND_STOPPED;
4175 status->value.sig = 0;
4180 wstat = (what << 8) | 0177;
4186 wstat = (SIGTRAP << 8) | 0177;
4191 wstat = (SIGTRAP << 8) | 0177;
4194 /* FIXME: use si_signo where possible. */
4196 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4199 wstat = (SIGILL << 8) | 0177;
4202 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4205 /* If we hit our __dbx_link() internal breakpoint,
4206 then remove it. See comments in procfs_init_inferior()
4207 for more details. */
4208 if (dbx_link_bpt_addr != 0
4209 && dbx_link_bpt_addr == read_pc ())
4210 remove_dbx_link_breakpoint ();
4212 wstat = (SIGTRAP << 8) | 0177;
4216 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4219 wstat = (SIGSEGV << 8) | 0177;
4223 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4226 wstat = (SIGFPE << 8) | 0177;
4228 case FLTPAGE: /* Recoverable page fault */
4229 default: /* FIXME: use si_signo if possible for fault */
4230 retval = pid_to_ptid (-1);
4231 printf_filtered ("procfs:%d -- ", __LINE__);
4232 printf_filtered (_("child stopped for unknown reason:\n"));
4233 proc_prettyprint_why (why, what, 1);
4234 error (_("... giving up..."));
4237 break; /* case PR_FAULTED: */
4238 default: /* switch (why) unmatched */
4239 printf_filtered ("procfs:%d -- ", __LINE__);
4240 printf_filtered (_("child stopped for unknown reason:\n"));
4241 proc_prettyprint_why (why, what, 1);
4242 error (_("... giving up..."));
4246 * Got this far without error:
4247 * If retval isn't in the threads database, add it.
4249 if (PIDGET (retval) > 0 &&
4250 !ptid_equal (retval, inferior_ptid) &&
4251 !in_thread_list (retval))
4254 * We have a new thread.
4255 * We need to add it both to GDB's list and to our own.
4256 * If we don't create a procinfo, resume may be unhappy
4259 printf_filtered (_("[New %s]\n"), target_pid_to_str (retval));
4260 add_thread (retval);
4261 if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL)
4262 create_procinfo (PIDGET (retval), TIDGET (retval));
4264 /* In addition, it's possible that this is the first
4265 * new thread we've seen, in which case we may not
4266 * have created entries for inferior_ptid yet.
4268 if (TIDGET (inferior_ptid) != 0)
4270 if (!in_thread_list (inferior_ptid))
4271 add_thread (inferior_ptid);
4272 if (find_procinfo (PIDGET (inferior_ptid),
4273 TIDGET (inferior_ptid)) == NULL)
4274 create_procinfo (PIDGET (inferior_ptid),
4275 TIDGET (inferior_ptid));
4279 else /* flags do not indicate STOPPED */
4281 /* surely this can't happen... */
4282 printf_filtered ("procfs:%d -- process not stopped.\n",
4284 proc_prettyprint_flags (flags, 1);
4285 error (_("procfs: ...giving up..."));
4290 store_waitstatus (status, wstat);
4296 /* Perform a partial transfer to/from the specified object. For
4297 memory transfers, fall back to the old memory xfer functions. */
4300 procfs_xfer_partial (struct target_ops *ops, enum target_object object,
4301 const char *annex, void *readbuf,
4302 const void *writebuf, ULONGEST offset, LONGEST len)
4306 case TARGET_OBJECT_MEMORY:
4308 return (*ops->deprecated_xfer_memory) (offset, readbuf, len,
4309 0/*write*/, NULL, ops);
4311 return (*ops->deprecated_xfer_memory) (offset, writebuf, len,
4312 1/*write*/, NULL, ops);
4316 case TARGET_OBJECT_AUXV:
4317 return procfs_xfer_auxv (ops, object, annex, readbuf, writebuf,
4322 if (ops->beneath != NULL)
4323 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
4324 readbuf, writebuf, offset, len);
4330 /* Transfer LEN bytes between GDB address MYADDR and target address
4331 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4332 otherwise transfer them from the target. TARGET is unused.
4334 The return value is 0 if an error occurred or no bytes were
4335 transferred. Otherwise, it will be a positive value which
4336 indicates the number of bytes transferred between gdb and the
4337 target. (Note that the interface also makes provisions for
4338 negative values, but this capability isn't implemented here.) */
4341 procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
4342 struct mem_attrib *attrib, struct target_ops *target)
4347 /* Find procinfo for main process */
4348 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4349 if (pi->as_fd == 0 &&
4350 open_procinfo_files (pi, FD_AS) == 0)
4352 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
4356 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
4361 PROCFS_NOTE ("write memory: ");
4363 PROCFS_NOTE ("write memory: \n");
4365 nbytes = write (pi->as_fd, myaddr, len);
4369 PROCFS_NOTE ("read memory: \n");
4370 nbytes = read (pi->as_fd, myaddr, len);
4381 * Function: invalidate_cache
4383 * Called by target_resume before making child runnable.
4384 * Mark cached registers and status's invalid.
4385 * If there are "dirty" caches that need to be written back
4386 * to the child process, do that.
4388 * File descriptors are also cached.
4389 * As they are a limited resource, we cannot hold onto them indefinitely.
4390 * However, as they are expensive to open, we don't want to throw them
4391 * away indescriminately either. As a compromise, we will keep the
4392 * file descriptors for the parent process, but discard any file
4393 * descriptors we may have accumulated for the threads.
4396 * As this function is called by iterate_over_threads, it always
4397 * returns zero (so that iterate_over_threads will keep iterating).
4402 invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
4405 * About to run the child; invalidate caches and do any other cleanup.
4409 if (pi->gregs_dirty)
4410 if (parent == NULL ||
4411 proc_get_current_thread (parent) != pi->tid)
4412 if (!proc_set_gregs (pi)) /* flush gregs cache */
4413 proc_warn (pi, "target_resume, set_gregs",
4415 if (FP0_REGNUM >= 0)
4416 if (pi->fpregs_dirty)
4417 if (parent == NULL ||
4418 proc_get_current_thread (parent) != pi->tid)
4419 if (!proc_set_fpregs (pi)) /* flush fpregs cache */
4420 proc_warn (pi, "target_resume, set_fpregs",
4426 /* The presence of a parent indicates that this is an LWP.
4427 Close any file descriptors that it might have open.
4428 We don't do this to the master (parent) procinfo. */
4430 close_procinfo_files (pi);
4432 pi->gregs_valid = 0;
4433 pi->fpregs_valid = 0;
4435 pi->gregs_dirty = 0;
4436 pi->fpregs_dirty = 0;
4438 pi->status_valid = 0;
4439 pi->threads_valid = 0;
4446 * Function: make_signal_thread_runnable
4448 * A callback function for iterate_over_threads.
4449 * Find the asynchronous signal thread, and make it runnable.
4450 * See if that helps matters any.
4454 make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr)
4457 if (proc_flags (pi) & PR_ASLWP)
4459 if (!proc_run_process (pi, 0, -1))
4460 proc_error (pi, "make_signal_thread_runnable", __LINE__);
4469 * Function: target_resume
4471 * Make the child process runnable. Normally we will then call
4472 * procfs_wait and wait for it to stop again (unles gdb is async).
4475 * step: if true, then arrange for the child to stop again
4476 * after executing a single instruction.
4477 * signo: if zero, then cancel any pending signal.
4478 * If non-zero, then arrange for the indicated signal
4479 * to be delivered to the child when it runs.
4480 * pid: if -1, then allow any child thread to run.
4481 * if non-zero, then allow only the indicated thread to run.
4482 ******* (not implemented yet)
4486 procfs_resume (ptid_t ptid, int step, enum target_signal signo)
4488 procinfo *pi, *thread;
4492 prrun.prflags |= PRSVADDR;
4493 prrun.pr_vaddr = $PC; set resume address
4494 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4495 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4496 prrun.prflags |= PRCFAULT; clear current fault.
4498 PRSTRACE and PRSFAULT can be done by other means
4499 (proc_trace_signals, proc_trace_faults)
4500 PRSVADDR is unnecessary.
4501 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4502 This basically leaves PRSTEP and PRCSIG.
4503 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4504 So basically PR_STEP is the sole argument that must be passed
4505 to proc_run_process (for use in the prrun struct by ioctl). */
4507 /* Find procinfo for main process */
4508 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4510 /* First cut: ignore pid argument */
4513 /* Convert signal to host numbering. */
4515 (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop))
4518 native_signo = target_signal_to_host (signo);
4520 pi->ignore_next_sigstop = 0;
4522 /* Running the process voids all cached registers and status. */
4523 /* Void the threads' caches first */
4524 proc_iterate_over_threads (pi, invalidate_cache, NULL);
4525 /* Void the process procinfo's caches. */
4526 invalidate_cache (NULL, pi, NULL);
4528 if (PIDGET (ptid) != -1)
4530 /* Resume a specific thread, presumably suppressing the others. */
4531 thread = find_procinfo (PIDGET (ptid), TIDGET (ptid));
4534 if (thread->tid != 0)
4536 /* We're to resume a specific thread, and not the others.
4537 * Set the child process's PR_ASYNC flag.
4540 if (!proc_set_async (pi))
4541 proc_error (pi, "target_resume, set_async", __LINE__);
4544 proc_iterate_over_threads (pi,
4545 make_signal_thread_runnable,
4548 pi = thread; /* substitute the thread's procinfo for run */
4553 if (!proc_run_process (pi, step, native_signo))
4556 warning (_("resume: target already running. Pretend to resume, and hope for the best!"));
4558 proc_error (pi, "target_resume", __LINE__);
4563 * Function: register_gdb_signals
4565 * Traverse the list of signals that GDB knows about
4566 * (see "handle" command), and arrange for the target
4567 * to be stopped or not, according to these settings.
4569 * Returns non-zero for success, zero for failure.
4573 register_gdb_signals (procinfo *pi, gdb_sigset_t *signals)
4577 for (signo = 0; signo < NSIG; signo ++)
4578 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
4579 signal_print_state (target_signal_from_host (signo)) == 0 &&
4580 signal_pass_state (target_signal_from_host (signo)) == 1)
4581 prdelset (signals, signo);
4583 praddset (signals, signo);
4585 return proc_set_traced_signals (pi, signals);
4589 * Function: target_notice_signals
4591 * Set up to trace signals in the child process.
4595 procfs_notice_signals (ptid_t ptid)
4597 gdb_sigset_t signals;
4598 procinfo *pi = find_procinfo_or_die (PIDGET (ptid), 0);
4600 if (proc_get_traced_signals (pi, &signals) &&
4601 register_gdb_signals (pi, &signals))
4604 proc_error (pi, "notice_signals", __LINE__);
4608 * Function: target_files_info
4610 * Print status information about the child process.
4614 procfs_files_info (struct target_ops *ignore)
4616 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
4617 attach_flag? "attached": "child",
4618 target_pid_to_str (inferior_ptid));
4622 * Function: target_open
4624 * A dummy: you don't open procfs.
4628 procfs_open (char *args, int from_tty)
4630 error (_("Use the \"run\" command to start a Unix child process."));
4634 * Function: target_can_run
4636 * This tells GDB that this target vector can be invoked
4637 * for "run" or "attach".
4640 int procfs_suppress_run = 0; /* Non-zero if procfs should pretend not to
4641 be a runnable target. Used by targets
4642 that can sit atop procfs, such as solaris
4647 procfs_can_run (void)
4649 /* This variable is controlled by modules that sit atop procfs that
4650 may layer their own process structure atop that provided here.
4651 sol-thread.c does this because of the Solaris two-level thread
4654 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4656 return !procfs_suppress_run;
4660 * Function: target_stop
4662 * Stop the child process asynchronously, as when the
4663 * gdb user types control-c or presses a "stop" button.
4665 * Works by sending kill(SIGINT) to the child's process group.
4671 kill (-inferior_process_group, SIGINT);
4675 * Function: unconditionally_kill_inferior
4677 * Make it die. Wait for it to die. Clean up after it.
4678 * Note: this should only be applied to the real process,
4679 * not to an LWP, because of the check for parent-process.
4680 * If we need this to work for an LWP, it needs some more logic.
4684 unconditionally_kill_inferior (procinfo *pi)
4688 parent_pid = proc_parent_pid (pi);
4689 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4690 /* FIXME: use access functions */
4691 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4692 before the PIOCKILL, otherwise it might generate a corrupted core
4693 file for the inferior. */
4694 if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0)
4696 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4699 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4700 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4701 to kill the inferior, otherwise it might remain stopped with a
4703 We do not check the result of the PIOCSSIG, the inferior might have
4706 gdb_siginfo_t newsiginfo;
4708 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
4709 newsiginfo.si_signo = SIGKILL;
4710 newsiginfo.si_code = 0;
4711 newsiginfo.si_errno = 0;
4712 newsiginfo.si_pid = getpid ();
4713 newsiginfo.si_uid = getuid ();
4714 /* FIXME: use proc_set_current_signal */
4715 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
4717 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4718 if (!proc_kill (pi, SIGKILL))
4719 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
4720 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4721 destroy_procinfo (pi);
4723 /* If pi is GDB's child, wait for it to die. */
4724 if (parent_pid == getpid ())
4725 /* FIXME: should we use waitpid to make sure we get the right event?
4726 Should we check the returned event? */
4731 ret = waitpid (pi->pid, &status, 0);
4739 * Function: target_kill_inferior
4741 * We're done debugging it, and we want it to go away.
4742 * Then we want GDB to forget all about it.
4746 procfs_kill_inferior (void)
4748 if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */
4750 /* Find procinfo for main process */
4751 procinfo *pi = find_procinfo (PIDGET (inferior_ptid), 0);
4754 unconditionally_kill_inferior (pi);
4755 target_mourn_inferior ();
4760 * Function: target_mourn_inferior
4762 * Forget we ever debugged this thing!
4766 procfs_mourn_inferior (void)
4770 if (!ptid_equal (inferior_ptid, null_ptid))
4772 /* Find procinfo for main process */
4773 pi = find_procinfo (PIDGET (inferior_ptid), 0);
4775 destroy_procinfo (pi);
4777 unpush_target (&procfs_ops);
4779 if (dbx_link_bpt != NULL)
4781 deprecated_remove_raw_breakpoint (dbx_link_bpt);
4782 dbx_link_bpt_addr = 0;
4783 dbx_link_bpt = NULL;
4786 generic_mourn_inferior ();
4790 * Function: init_inferior
4792 * When GDB forks to create a runnable inferior process,
4793 * this function is called on the parent side of the fork.
4794 * It's job is to do whatever is necessary to make the child
4795 * ready to be debugged, and then wait for the child to synchronize.
4799 procfs_init_inferior (int pid)
4802 gdb_sigset_t signals;
4805 /* This routine called on the parent side (GDB side)
4806 after GDB forks the inferior. */
4808 push_target (&procfs_ops);
4810 if ((pi = create_procinfo (pid, 0)) == NULL)
4811 perror ("procfs: out of memory in 'init_inferior'");
4813 if (!open_procinfo_files (pi, FD_CTL))
4814 proc_error (pi, "init_inferior, open_proc_files", __LINE__);
4818 open_procinfo_files // done
4821 procfs_notice_signals
4828 /* If not stopped yet, wait for it to stop. */
4829 if (!(proc_flags (pi) & PR_STOPPED) &&
4830 !(proc_wait_for_stop (pi)))
4831 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
4833 /* Save some of the /proc state to be restored if we detach. */
4834 /* FIXME: Why? In case another debugger was debugging it?
4835 We're it's parent, for Ghu's sake! */
4836 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
4837 proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
4838 if (!proc_get_held_signals (pi, &pi->saved_sighold))
4839 proc_error (pi, "init_inferior, get_held_signals", __LINE__);
4840 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
4841 proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
4842 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
4843 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
4844 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
4845 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
4847 /* Register to trace selected signals in the child. */
4848 prfillset (&signals);
4849 if (!register_gdb_signals (pi, &signals))
4850 proc_error (pi, "init_inferior, register_signals", __LINE__);
4852 if ((fail = procfs_debug_inferior (pi)) != 0)
4853 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
4855 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4856 and possibly even turning ON kill-on-last-close at this point. But
4857 I can't make that change without careful testing which I don't have
4858 time to do right now... */
4859 /* Turn on run-on-last-close flag so that the child
4860 will die if GDB goes away for some reason. */
4861 if (!proc_set_run_on_last_close (pi))
4862 proc_error (pi, "init_inferior, set_RLC", __LINE__);
4864 /* The 'process ID' we return to GDB is composed of
4865 the actual process ID plus the lwp ID. */
4866 inferior_ptid = MERGEPID (pi->pid, proc_get_current_thread (pi));
4868 /* Typically two, one trap to exec the shell, one to exec the
4869 program being debugged. Defined by "inferior.h". */
4870 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
4873 /* On mips-irix, we need to stop the inferior early enough during
4874 the startup phase in order to be able to load the shared library
4875 symbols and insert the breakpoints that are located in these shared
4876 libraries. Stopping at the program entry point is not good enough
4877 because the -init code is executed before the execution reaches
4880 So what we need to do is to insert a breakpoint in the runtime
4881 loader (rld), more precisely in __dbx_link(). This procedure is
4882 called by rld once all shared libraries have been mapped, but before
4883 the -init code is executed. Unfortuantely, this is not straightforward,
4884 as rld is not part of the executable we are running, and thus we need
4885 the inferior to run until rld itself has been mapped in memory.
4887 For this, we trace all syssgi() syscall exit events. Each time
4888 we detect such an event, we iterate over each text memory maps,
4889 get its associated fd, and scan the symbol table for __dbx_link().
4890 When found, we know that rld has been mapped, and that we can insert
4891 the breakpoint at the symbol address. Once the dbx_link() breakpoint
4892 has been inserted, the syssgi() notifications are no longer necessary,
4893 so they should be canceled. */
4894 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT, FLAG_SET, 0);
4899 * Function: set_exec_trap
4901 * When GDB forks to create a new process, this function is called
4902 * on the child side of the fork before GDB exec's the user program.
4903 * Its job is to make the child minimally debuggable, so that the
4904 * parent GDB process can connect to the child and take over.
4905 * This function should do only the minimum to make that possible,
4906 * and to synchronize with the parent process. The parent process
4907 * should take care of the details.
4911 procfs_set_exec_trap (void)
4913 /* This routine called on the child side (inferior side)
4914 after GDB forks the inferior. It must use only local variables,
4915 because it may be sharing data space with its parent. */
4920 if ((pi = create_procinfo (getpid (), 0)) == NULL)
4921 perror_with_name (_("procfs: create_procinfo failed in child."));
4923 if (open_procinfo_files (pi, FD_CTL) == 0)
4925 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
4926 gdb_flush (gdb_stderr);
4927 /* no need to call "dead_procinfo", because we're going to exit. */
4931 #ifdef PRFS_STOPEXEC /* defined on OSF */
4932 /* OSF method for tracing exec syscalls. Quoting:
4933 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4934 exits from exec system calls because of the user level loader. */
4935 /* FIXME: make nice and maybe move into an access function. */
4939 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
4941 proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__);
4942 gdb_flush (gdb_stderr);
4945 prfs_flags |= PRFS_STOPEXEC;
4947 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
4949 proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__);
4950 gdb_flush (gdb_stderr);
4954 #else /* not PRFS_STOPEXEC */
4955 /* Everyone else's (except OSF) method for tracing exec syscalls */
4957 Not all systems with /proc have all the exec* syscalls with the same
4958 names. On the SGI, for example, there is no SYS_exec, but there
4959 *is* a SYS_execv. So, we try to account for that. */
4961 exitset = sysset_t_alloc (pi);
4962 gdb_premptysysset (exitset);
4964 gdb_praddsysset (exitset, SYS_exec);
4967 gdb_praddsysset (exitset, SYS_execve);
4970 gdb_praddsysset (exitset, SYS_execv);
4972 #ifdef DYNAMIC_SYSCALLS
4974 int callnum = find_syscall (pi, "execve");
4977 gdb_praddsysset (exitset, callnum);
4979 callnum = find_syscall (pi, "ra_execve");
4981 gdb_praddsysset (exitset, callnum);
4983 #endif /* DYNAMIC_SYSCALLS */
4985 if (!proc_set_traced_sysexit (pi, exitset))
4987 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
4988 gdb_flush (gdb_stderr);
4991 #endif /* PRFS_STOPEXEC */
4993 /* FIXME: should this be done in the parent instead? */
4994 /* Turn off inherit on fork flag so that all grand-children
4995 of gdb start with tracing flags cleared. */
4996 if (!proc_unset_inherit_on_fork (pi))
4997 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
4999 /* Turn off run on last close flag, so that the child process
5000 cannot run away just because we close our handle on it.
5001 We want it to wait for the parent to attach. */
5002 if (!proc_unset_run_on_last_close (pi))
5003 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
5005 /* FIXME: No need to destroy the procinfo --
5006 we have our own address space, and we're about to do an exec! */
5007 /*destroy_procinfo (pi);*/
5011 * Function: create_inferior
5013 * This function is called BEFORE gdb forks the inferior process.
5014 * Its only real responsibility is to set things up for the fork,
5015 * and tell GDB which two functions to call after the fork (one
5016 * for the parent, and one for the child).
5018 * This function does a complicated search for a unix shell program,
5019 * which it then uses to parse arguments and environment variables
5020 * to be sent to the child. I wonder whether this code could not
5021 * be abstracted out and shared with other unix targets such as
5026 procfs_create_inferior (char *exec_file, char *allargs, char **env,
5029 char *shell_file = getenv ("SHELL");
5031 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
5034 /* We will be looking down the PATH to find shell_file. If we
5035 just do this the normal way (via execlp, which operates by
5036 attempting an exec for each element of the PATH until it
5037 finds one which succeeds), then there will be an exec for
5038 each failed attempt, each of which will cause a PR_SYSEXIT
5039 stop, and we won't know how to distinguish the PR_SYSEXIT's
5040 for these failed execs with the ones for successful execs
5041 (whether the exec has succeeded is stored at that time in the
5042 carry bit or some such architecture-specific and
5043 non-ABI-specified place).
5045 So I can't think of anything better than to search the PATH
5046 now. This has several disadvantages: (1) There is a race
5047 condition; if we find a file now and it is deleted before we
5048 exec it, we lose, even if the deletion leaves a valid file
5049 further down in the PATH, (2) there is no way to know exactly
5050 what an executable (in the sense of "capable of being
5051 exec'd") file is. Using access() loses because it may lose
5052 if the caller is the superuser; failing to use it loses if
5053 there are ACLs or some such. */
5057 /* FIXME-maybe: might want "set path" command so user can change what
5058 path is used from within GDB. */
5059 char *path = getenv ("PATH");
5061 struct stat statbuf;
5064 path = "/bin:/usr/bin";
5066 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
5067 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
5069 p1 = strchr (p, ':');
5074 strncpy (tryname, p, len);
5075 tryname[len] = '\0';
5076 strcat (tryname, "/");
5077 strcat (tryname, shell_file);
5078 if (access (tryname, X_OK) < 0)
5080 if (stat (tryname, &statbuf) < 0)
5082 if (!S_ISREG (statbuf.st_mode))
5083 /* We certainly need to reject directories. I'm not quite
5084 as sure about FIFOs, sockets, etc., but I kind of doubt
5085 that people want to exec() these things. */
5090 /* Not found. This must be an error rather than merely passing
5091 the file to execlp(), because execlp() would try all the
5092 exec()s, causing GDB to get confused. */
5093 error (_("procfs:%d -- Can't find shell %s in PATH"),
5094 __LINE__, shell_file);
5096 shell_file = tryname;
5099 fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
5100 procfs_init_inferior, NULL, shell_file);
5103 /* Make sure to cancel the syssgi() syscall-exit notifications.
5104 They should normally have been removed by now, but they may still
5105 be activated if the inferior doesn't use shared libraries, or if
5106 we didn't locate __dbx_link, or if we never stopped in __dbx_link.
5107 See procfs_init_inferior() for more details. */
5108 proc_trace_syscalls_1 (find_procinfo_or_die (PIDGET (inferior_ptid), 0),
5109 SYS_syssgi, PR_SYSEXIT, FLAG_RESET, 0);
5114 * Function: notice_thread
5116 * Callback for find_new_threads.
5117 * Calls "add_thread".
5121 procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
5123 ptid_t gdb_threadid = MERGEPID (pi->pid, thread->tid);
5125 if (!in_thread_list (gdb_threadid))
5126 add_thread (gdb_threadid);
5132 * Function: target_find_new_threads
5134 * Query all the threads that the target knows about,
5135 * and give them back to GDB to add to its list.
5139 procfs_find_new_threads (void)
5143 /* Find procinfo for main process */
5144 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5145 proc_update_threads (pi);
5146 proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
5150 * Function: target_thread_alive
5152 * Return true if the thread is still 'alive'.
5154 * This guy doesn't really seem to be doing his job.
5155 * Got to investigate how to tell when a thread is really gone.
5159 procfs_thread_alive (ptid_t ptid)
5164 proc = PIDGET (ptid);
5165 thread = TIDGET (ptid);
5166 /* If I don't know it, it ain't alive! */
5167 if ((pi = find_procinfo (proc, thread)) == NULL)
5170 /* If I can't get its status, it ain't alive!
5171 What's more, I need to forget about it! */
5172 if (!proc_get_status (pi))
5174 destroy_procinfo (pi);
5177 /* I couldn't have got its status if it weren't alive, so it's alive. */
5181 /* Convert PTID to a string. Returns the string in a static buffer. */
5184 procfs_pid_to_str (ptid_t ptid)
5186 static char buf[80];
5188 if (TIDGET (ptid) == 0)
5189 sprintf (buf, "process %d", PIDGET (ptid));
5191 sprintf (buf, "LWP %ld", TIDGET (ptid));
5197 * Function: procfs_set_watchpoint
5198 * Insert a watchpoint
5202 procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag,
5210 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5211 PIDGET (inferior_ptid) : PIDGET (ptid), 0);
5213 /* Translate from GDB's flags to /proc's */
5214 if (len > 0) /* len == 0 means delete watchpoint */
5216 switch (rwflag) { /* FIXME: need an enum! */
5217 case hw_write: /* default watchpoint (write) */
5218 pflags = WRITE_WATCHFLAG;
5220 case hw_read: /* read watchpoint */
5221 pflags = READ_WATCHFLAG;
5223 case hw_access: /* access watchpoint */
5224 pflags = READ_WATCHFLAG | WRITE_WATCHFLAG;
5226 case hw_execute: /* execution HW breakpoint */
5227 pflags = EXEC_WATCHFLAG;
5229 default: /* Something weird. Return error. */
5232 if (after) /* Stop after r/w access is completed. */
5233 pflags |= AFTER_WATCHFLAG;
5236 if (!proc_set_watchpoint (pi, addr, len, pflags))
5238 if (errno == E2BIG) /* Typical error for no resources */
5239 return -1; /* fail */
5240 /* GDB may try to remove the same watchpoint twice.
5241 If a remove request returns no match, don't error. */
5242 if (errno == ESRCH && len == 0)
5243 return 0; /* ignore */
5244 proc_error (pi, "set_watchpoint", __LINE__);
5247 #endif /* UNIXWARE */
5251 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
5252 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
5253 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
5256 Note: procfs_can_use_hw_breakpoint() is not yet used by all
5257 procfs.c targets due to the fact that some of them still define
5258 TARGET_CAN_USE_HARDWARE_WATCHPOINT. */
5261 procfs_can_use_hw_breakpoint (int type, int cnt, int othertype)
5263 #ifndef TARGET_HAS_HARDWARE_WATCHPOINTS
5266 /* Due to the way that proc_set_watchpoint() is implemented, host
5267 and target pointers must be of the same size. If they are not,
5268 we can't use hardware watchpoints. This limitation is due to the
5269 fact that proc_set_watchpoint() calls
5270 procfs_address_to_host_pointer(); a close inspection of
5271 procfs_address_to_host_pointer will reveal that an internal error
5272 will be generated when the host and target pointer sizes are
5274 if (sizeof (void *) != TYPE_LENGTH (builtin_type_void_data_ptr))
5277 /* Other tests here??? */
5284 * Function: stopped_by_watchpoint
5286 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5287 * else returns zero.
5291 procfs_stopped_by_watchpoint (ptid_t ptid)
5295 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5296 PIDGET (inferior_ptid) : PIDGET (ptid), 0);
5298 if (!pi) /* If no process, then not stopped by watchpoint! */
5301 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
5303 if (proc_why (pi) == PR_FAULTED)
5306 if (proc_what (pi) == FLTWATCH)
5310 if (proc_what (pi) == FLTKWATCH)
5318 #ifdef TM_I386SOL2_H
5320 * Function: procfs_find_LDT_entry
5323 * ptid_t ptid; // The GDB-style pid-plus-LWP.
5326 * pointer to the corresponding LDT entry.
5330 procfs_find_LDT_entry (ptid_t ptid)
5332 gdb_gregset_t *gregs;
5336 /* Find procinfo for the lwp. */
5337 if ((pi = find_procinfo (PIDGET (ptid), TIDGET (ptid))) == NULL)
5339 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%d."),
5340 PIDGET (ptid), TIDGET (ptid));
5343 /* get its general registers. */
5344 if ((gregs = proc_get_gregs (pi)) == NULL)
5346 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%d."),
5347 PIDGET (ptid), TIDGET (ptid));
5350 /* Now extract the GS register's lower 16 bits. */
5351 key = (*gregs)[GS] & 0xffff;
5353 /* Find the matching entry and return it. */
5354 return proc_get_LDT_entry (pi, key);
5356 #endif /* TM_I386SOL2_H */
5359 * Memory Mappings Functions:
5363 * Function: iterate_over_mappings
5365 * Call a callback function once for each mapping, passing it the mapping,
5366 * an optional secondary callback function, and some optional opaque data.
5367 * Quit and return the first non-zero value returned from the callback.
5370 * pi -- procinfo struct for the process to be mapped.
5371 * func -- callback function to be called by this iterator.
5372 * data -- optional opaque data to be passed to the callback function.
5373 * child_func -- optional secondary function pointer to be passed
5374 * to the child function.
5376 * Return: First non-zero return value from the callback function,
5381 iterate_over_mappings (procinfo *pi, int (*child_func) (), void *data,
5382 int (*func) (struct prmap *map,
5383 int (*child_func) (),
5386 char pathname[MAX_PROC_NAME_SIZE];
5387 struct prmap *prmaps;
5388 struct prmap *prmap;
5396 /* Get the number of mappings, allocate space,
5397 and read the mappings into prmaps. */
5400 sprintf (pathname, "/proc/%d/map", pi->pid);
5401 if ((map_fd = open (pathname, O_RDONLY)) < 0)
5402 proc_error (pi, "iterate_over_mappings (open)", __LINE__);
5404 /* Make sure it gets closed again. */
5405 make_cleanup_close (map_fd);
5407 /* Use stat to determine the file size, and compute
5408 the number of prmap_t objects it contains. */
5409 if (fstat (map_fd, &sbuf) != 0)
5410 proc_error (pi, "iterate_over_mappings (fstat)", __LINE__);
5412 nmap = sbuf.st_size / sizeof (prmap_t);
5413 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5414 if (read (map_fd, (char *) prmaps, nmap * sizeof (*prmaps))
5415 != (nmap * sizeof (*prmaps)))
5416 proc_error (pi, "iterate_over_mappings (read)", __LINE__);
5418 /* Use ioctl command PIOCNMAP to get number of mappings. */
5419 if (ioctl (pi->ctl_fd, PIOCNMAP, &nmap) != 0)
5420 proc_error (pi, "iterate_over_mappings (PIOCNMAP)", __LINE__);
5422 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5423 if (ioctl (pi->ctl_fd, PIOCMAP, prmaps) != 0)
5424 proc_error (pi, "iterate_over_mappings (PIOCMAP)", __LINE__);
5427 for (prmap = prmaps; nmap > 0; prmap++, nmap--)
5428 if ((funcstat = (*func) (prmap, child_func, data)) != 0)
5435 * Function: solib_mappings_callback
5437 * Calls the supplied callback function once for each mapped address
5438 * space in the process. The callback function receives an open
5439 * file descriptor for the file corresponding to that mapped
5440 * address space (if there is one), and the base address of the
5441 * mapped space. Quit when the callback function returns a
5442 * nonzero value, or at teh end of the mappings.
5444 * Returns: the first non-zero return value of the callback function,
5448 int solib_mappings_callback (struct prmap *map,
5449 int (*func) (int, CORE_ADDR),
5452 procinfo *pi = data;
5456 char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)];
5458 if (map->pr_vaddr == 0 && map->pr_size == 0)
5459 return -1; /* sanity */
5461 if (map->pr_mapname[0] == 0)
5463 fd = -1; /* no map file */
5467 sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname);
5468 /* Note: caller's responsibility to close this fd! */
5469 fd = open_with_retry (name, O_RDONLY);
5470 /* Note: we don't test the above call for failure;
5471 we just pass the FD on as given. Sometimes there is
5472 no file, so the open may return failure, but that's
5476 fd = ioctl (pi->ctl_fd, PIOCOPENM, &map->pr_vaddr);
5477 /* Note: we don't test the above call for failure;
5478 we just pass the FD on as given. Sometimes there is
5479 no file, so the ioctl may return failure, but that's
5482 return (*func) (fd, (CORE_ADDR) map->pr_vaddr);
5486 * Function: proc_iterate_over_mappings
5488 * Uses the unified "iterate_over_mappings" function
5489 * to implement the exported interface to solib-svr4.c.
5491 * Given a pointer to a function, call that function once for every
5492 * mapped address space in the process. The callback function
5493 * receives an open file descriptor for the file corresponding to
5494 * that mapped address space (if there is one), and the base address
5495 * of the mapped space. Quit when the callback function returns a
5496 * nonzero value, or at teh end of the mappings.
5498 * Returns: the first non-zero return value of the callback function,
5503 proc_iterate_over_mappings (int (*func) (int, CORE_ADDR))
5505 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5507 return iterate_over_mappings (pi, func, pi, solib_mappings_callback);
5511 * Function: find_memory_regions_callback
5513 * Implements the to_find_memory_regions method.
5514 * Calls an external function for each memory region.
5515 * External function will have the signiture:
5517 * int callback (CORE_ADDR vaddr,
5518 * unsigned long size,
5519 * int read, int write, int execute,
5522 * Returns the integer value returned by the callback.
5526 find_memory_regions_callback (struct prmap *map,
5527 int (*func) (CORE_ADDR,
5533 return (*func) ((CORE_ADDR) map->pr_vaddr,
5535 (map->pr_mflags & MA_READ) != 0,
5536 (map->pr_mflags & MA_WRITE) != 0,
5537 (map->pr_mflags & MA_EXEC) != 0,
5542 * Function: proc_find_memory_regions
5544 * External interface. Calls a callback function once for each
5545 * mapped memory region in the child process, passing as arguments
5546 * CORE_ADDR virtual_address,
5547 * unsigned long size,
5548 * int read, TRUE if region is readable by the child
5549 * int write, TRUE if region is writable by the child
5550 * int execute TRUE if region is executable by the child.
5552 * Stops iterating and returns the first non-zero value
5553 * returned by the callback.
5557 proc_find_memory_regions (int (*func) (CORE_ADDR,
5563 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5565 return iterate_over_mappings (pi, func, data,
5566 find_memory_regions_callback);
5569 /* Remove the breakpoint that we inserted in __dbx_link().
5570 Does nothing if the breakpoint hasn't been inserted or has already
5574 remove_dbx_link_breakpoint (void)
5576 if (dbx_link_bpt_addr == 0)
5579 if (deprecated_remove_raw_breakpoint (dbx_link_bpt) != 0)
5580 warning (_("Unable to remove __dbx_link breakpoint."));
5582 dbx_link_bpt_addr = 0;
5583 dbx_link_bpt = NULL;
5586 /* Return the address of the __dbx_link() function in the file
5587 refernced by ABFD by scanning its symbol table. Return 0 if
5588 the symbol was not found. */
5591 dbx_link_addr (bfd *abfd)
5593 long storage_needed;
5594 asymbol **symbol_table;
5595 long number_of_symbols;
5598 storage_needed = bfd_get_symtab_upper_bound (abfd);
5599 if (storage_needed <= 0)
5602 symbol_table = (asymbol **) xmalloc (storage_needed);
5603 make_cleanup (xfree, symbol_table);
5605 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
5607 for (i = 0; i < number_of_symbols; i++)
5609 asymbol *sym = symbol_table[i];
5611 if ((sym->flags & BSF_GLOBAL)
5612 && sym->name != NULL && strcmp (sym->name, "__dbx_link") == 0)
5613 return (sym->value + sym->section->vma);
5616 /* Symbol not found, return NULL. */
5620 /* Search the symbol table of the file referenced by FD for a symbol
5621 named __dbx_link(). If found, then insert a breakpoint at this location,
5622 and return nonzero. Return zero otherwise. */
5625 insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
5628 long storage_needed;
5631 abfd = bfd_fdopenr ("unamed", 0, fd);
5634 warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
5638 if (!bfd_check_format (abfd, bfd_object))
5640 /* Not the correct format, so we can not possibly find the dbx_link
5646 sym_addr = dbx_link_addr (abfd);
5649 /* Insert the breakpoint. */
5650 dbx_link_bpt_addr = sym_addr;
5651 dbx_link_bpt = deprecated_insert_raw_breakpoint (sym_addr);
5652 if (dbx_link_bpt == NULL)
5654 warning (_("Failed to insert dbx_link breakpoint."));
5666 /* If the given memory region MAP contains a symbol named __dbx_link,
5667 insert a breakpoint at this location and return nonzero. Return
5671 insert_dbx_link_bpt_in_region (struct prmap *map,
5672 int (*child_func) (),
5675 procinfo *pi = (procinfo *) data;
5677 /* We know the symbol we're looking for is in a text region, so
5678 only look for it if the region is a text one. */
5679 if (map->pr_mflags & MA_EXEC)
5680 return solib_mappings_callback (map, insert_dbx_link_bpt_in_file, pi);
5685 /* Search all memory regions for a symbol named __dbx_link. If found,
5686 insert a breakpoint at its location, and return nonzero. Return zero
5690 insert_dbx_link_breakpoint (procinfo *pi)
5692 return iterate_over_mappings (pi, NULL, pi, insert_dbx_link_bpt_in_region);
5696 * Function: mappingflags
5698 * Returns an ascii representation of a memory mapping's flags.
5702 mappingflags (long flags)
5704 static char asciiflags[8];
5706 strcpy (asciiflags, "-------");
5707 #if defined (MA_PHYS)
5708 if (flags & MA_PHYS)
5709 asciiflags[0] = 'd';
5711 if (flags & MA_STACK)
5712 asciiflags[1] = 's';
5713 if (flags & MA_BREAK)
5714 asciiflags[2] = 'b';
5715 if (flags & MA_SHARED)
5716 asciiflags[3] = 's';
5717 if (flags & MA_READ)
5718 asciiflags[4] = 'r';
5719 if (flags & MA_WRITE)
5720 asciiflags[5] = 'w';
5721 if (flags & MA_EXEC)
5722 asciiflags[6] = 'x';
5723 return (asciiflags);
5727 * Function: info_mappings_callback
5729 * Callback function, does the actual work for 'info proc mappings'.
5733 info_mappings_callback (struct prmap *map, int (*ignore) (), void *unused)
5735 char *data_fmt_string;
5737 if (TARGET_ADDR_BIT == 32)
5738 data_fmt_string = "\t%#10lx %#10lx %#10x %#10x %7s\n";
5740 data_fmt_string = " %#18lx %#18lx %#10x %#10x %7s\n";
5742 printf_filtered (data_fmt_string,
5743 (unsigned long) map->pr_vaddr,
5744 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5746 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5747 (unsigned int) map->pr_offset,
5751 mappingflags (map->pr_mflags));
5757 * Function: info_proc_mappings
5759 * Implement the "info proc mappings" subcommand.
5763 info_proc_mappings (procinfo *pi, int summary)
5765 char *header_fmt_string;
5767 if (TARGET_PTR_BIT == 32)
5768 header_fmt_string = "\t%10s %10s %10s %10s %7s\n";
5770 header_fmt_string = " %18s %18s %10s %10s %7s\n";
5773 return; /* No output for summary mode. */
5775 printf_filtered (_("Mapped address spaces:\n\n"));
5776 printf_filtered (header_fmt_string,
5783 iterate_over_mappings (pi, NULL, NULL, info_mappings_callback);
5784 printf_filtered ("\n");
5788 * Function: info_proc_cmd
5790 * Implement the "info proc" command.
5794 info_proc_cmd (char *args, int from_tty)
5796 struct cleanup *old_chain;
5797 procinfo *process = NULL;
5798 procinfo *thread = NULL;
5805 old_chain = make_cleanup (null_cleanup, 0);
5808 if ((argv = buildargv (args)) == NULL)
5811 make_cleanup_freeargv (argv);
5813 while (argv != NULL && *argv != NULL)
5815 if (isdigit (argv[0][0]))
5817 pid = strtoul (argv[0], &tmp, 10);
5819 tid = strtoul (++tmp, NULL, 10);
5821 else if (argv[0][0] == '/')
5823 tid = strtoul (argv[0] + 1, NULL, 10);
5825 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
5836 pid = PIDGET (inferior_ptid);
5838 error (_("No current process: you must name one."));
5841 /* Have pid, will travel.
5842 First see if it's a process we're already debugging. */
5843 process = find_procinfo (pid, 0);
5844 if (process == NULL)
5846 /* No. So open a procinfo for it, but
5847 remember to close it again when finished. */
5848 process = create_procinfo (pid, 0);
5849 make_cleanup (do_destroy_procinfo_cleanup, process);
5850 if (!open_procinfo_files (process, FD_CTL))
5851 proc_error (process, "info proc, open_procinfo_files", __LINE__);
5855 thread = create_procinfo (pid, tid);
5859 printf_filtered (_("process %d flags:\n"), process->pid);
5860 proc_prettyprint_flags (proc_flags (process), 1);
5861 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
5862 proc_prettyprint_why (proc_why (process), proc_what (process), 1);
5863 if (proc_get_nthreads (process) > 1)
5864 printf_filtered ("Process has %d threads.\n",
5865 proc_get_nthreads (process));
5869 printf_filtered (_("thread %d flags:\n"), thread->tid);
5870 proc_prettyprint_flags (proc_flags (thread), 1);
5871 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
5872 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
5877 info_proc_mappings (process, 0);
5880 do_cleanups (old_chain);
5883 /* Modify the status of the system call identified by SYSCALLNUM in
5884 the set of syscalls that are currently traced/debugged.
5886 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
5887 will be updated. Otherwise, the exit syscalls set will be updated.
5889 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
5890 will be disabled. */
5893 proc_trace_syscalls_1 (procinfo *pi, int syscallnum, int entry_or_exit,
5894 int mode, int from_tty)
5898 if (entry_or_exit == PR_SYSENTRY)
5899 sysset = proc_get_traced_sysentry (pi, NULL);
5901 sysset = proc_get_traced_sysexit (pi, NULL);
5904 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
5906 if (mode == FLAG_SET)
5907 gdb_praddsysset (sysset, syscallnum);
5909 gdb_prdelsysset (sysset, syscallnum);
5911 if (entry_or_exit == PR_SYSENTRY)
5913 if (!proc_set_traced_sysentry (pi, sysset))
5914 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
5918 if (!proc_set_traced_sysexit (pi, sysset))
5919 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
5924 proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode)
5928 if (PIDGET (inferior_ptid) <= 0)
5929 error (_("you must be debugging a process to use this command."));
5931 if (args == NULL || args[0] == 0)
5932 error_no_arg (_("system call to trace"));
5934 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5935 if (isdigit (args[0]))
5937 const int syscallnum = atoi (args);
5939 proc_trace_syscalls_1 (pi, syscallnum, entry_or_exit, mode, from_tty);
5944 proc_trace_sysentry_cmd (char *args, int from_tty)
5946 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
5950 proc_trace_sysexit_cmd (char *args, int from_tty)
5952 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
5956 proc_untrace_sysentry_cmd (char *args, int from_tty)
5958 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
5962 proc_untrace_sysexit_cmd (char *args, int from_tty)
5964 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
5969 _initialize_procfs (void)
5972 add_target (&procfs_ops);
5973 add_info ("proc", info_proc_cmd, _("\
5974 Show /proc process information about any running process.\n\
5975 Specify process id, or use the program being debugged by default.\n\
5976 Specify keyword 'mappings' for detailed info on memory mappings."));
5977 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
5978 _("Give a trace of entries into the syscall."));
5979 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
5980 _("Give a trace of exits from the syscall."));
5981 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
5982 _("Cancel a trace of entries into the syscall."));
5983 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
5984 _("Cancel a trace of exits from the syscall."));
5987 /* =================== END, GDB "MODULE" =================== */
5991 /* miscellaneous stubs: */
5992 /* The following satisfy a few random symbols mostly created by */
5993 /* the solaris threads implementation, which I will chase down */
5997 * Return a pid for which we guarantee
5998 * we will be able to find a 'live' procinfo.
6002 procfs_first_available (void)
6004 return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1);
6007 /* =================== GCORE .NOTE "MODULE" =================== */
6008 #if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT)
6009 /* gcore only implemented on solaris and unixware (so far) */
6012 procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
6013 char *note_data, int *note_size)
6015 gdb_gregset_t gregs;
6016 gdb_fpregset_t fpregs;
6017 unsigned long merged_pid;
6019 merged_pid = TIDGET (ptid) << 16 | PIDGET (ptid);
6021 fill_gregset (current_regcache, &gregs, -1);
6022 #if defined (UNIXWARE)
6023 note_data = (char *) elfcore_write_lwpstatus (obfd,
6030 note_data = (char *) elfcore_write_prstatus (obfd,
6037 fill_fpregset (current_regcache, &fpregs, -1);
6038 note_data = (char *) elfcore_write_prfpreg (obfd,
6046 struct procfs_corefile_thread_data {
6053 procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data)
6055 struct procfs_corefile_thread_data *args = data;
6057 if (pi != NULL && thread->tid != 0)
6059 ptid_t saved_ptid = inferior_ptid;
6060 inferior_ptid = MERGEPID (pi->pid, thread->tid);
6061 args->note_data = procfs_do_thread_registers (args->obfd, inferior_ptid,
6064 inferior_ptid = saved_ptid;
6070 procfs_make_note_section (bfd *obfd, int *note_size)
6072 struct cleanup *old_chain;
6073 gdb_gregset_t gregs;
6074 gdb_fpregset_t fpregs;
6075 char fname[16] = {'\0'};
6076 char psargs[80] = {'\0'};
6077 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
6078 char *note_data = NULL;
6080 struct procfs_corefile_thread_data thread_args;
6084 if (get_exec_file (0))
6086 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
6087 strncpy (psargs, get_exec_file (0),
6090 inf_args = get_inferior_args ();
6091 if (inf_args && *inf_args &&
6092 strlen (inf_args) < ((int) sizeof (psargs) - (int) strlen (psargs)))
6094 strncat (psargs, " ",
6095 sizeof (psargs) - strlen (psargs));
6096 strncat (psargs, inf_args,
6097 sizeof (psargs) - strlen (psargs));
6101 note_data = (char *) elfcore_write_prpsinfo (obfd,
6108 fill_gregset (current_regcache, &gregs, -1);
6109 note_data = elfcore_write_pstatus (obfd, note_data, note_size,
6110 PIDGET (inferior_ptid),
6111 stop_signal, &gregs);
6114 thread_args.obfd = obfd;
6115 thread_args.note_data = note_data;
6116 thread_args.note_size = note_size;
6117 proc_iterate_over_threads (pi, procfs_corefile_thread_callback, &thread_args);
6119 if (thread_args.note_data == note_data)
6121 /* iterate_over_threads didn't come up with any threads;
6122 just use inferior_ptid. */
6123 note_data = procfs_do_thread_registers (obfd, inferior_ptid,
6124 note_data, note_size);
6128 note_data = thread_args.note_data;
6131 auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV,
6135 note_data = elfcore_write_note (obfd, note_data, note_size,
6136 "CORE", NT_AUXV, auxv, auxv_len);
6140 make_cleanup (xfree, note_data);
6143 #else /* !(Solaris or Unixware) */
6145 procfs_make_note_section (bfd *obfd, int *note_size)
6147 error (_("gcore not implemented for this host."));
6148 return NULL; /* lint */
6150 #endif /* Solaris or Unixware */
6151 /* =================== END GCORE .NOTE "MODULE" =================== */