1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2006, 2007, 2008, 2009
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 3 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, see <http://www.gnu.org/licenses/>. */
28 #include "elf-bfd.h" /* for elfcore_write_* */
30 #include "gdbthread.h"
32 #include "inf-child.h"
34 #if defined (NEW_PROC_API)
35 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
38 #include <sys/procfs.h>
39 #ifdef HAVE_SYS_FAULT_H
40 #include <sys/fault.h>
42 #ifdef HAVE_SYS_SYSCALL_H
43 #include <sys/syscall.h>
45 #include <sys/errno.h>
49 #include "gdb_string.h"
50 #include "gdb_assert.h"
57 * This module provides the interface between GDB and the
58 * /proc file system, which is used on many versions of Unix
59 * as a means for debuggers to control other processes.
60 * Examples of the systems that use this interface are:
67 * /proc works by imitating a file system: you open a simulated file
68 * that represents the process you wish to interact with, and
69 * perform operations on that "file" in order to examine or change
70 * the state of the other process.
72 * The most important thing to know about /proc and this module
73 * is that there are two very different interfaces to /proc:
74 * One that uses the ioctl system call, and
75 * another that uses read and write system calls.
76 * This module has to support both /proc interfaces. This means
77 * that there are two different ways of doing every basic operation.
79 * In order to keep most of the code simple and clean, I have
80 * defined an interface "layer" which hides all these system calls.
81 * An ifdef (NEW_PROC_API) determines which interface we are using,
82 * and most or all occurrances of this ifdef should be confined to
83 * this interface layer.
87 /* Determine which /proc API we are using:
88 The ioctl API defines PIOCSTATUS, while
89 the read/write (multiple fd) API never does. */
92 #include <sys/types.h>
93 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
96 #include <fcntl.h> /* for O_RDONLY */
97 #include <unistd.h> /* for "X_OK" */
98 #include "gdb_stat.h" /* for struct stat */
100 /* Note: procfs-utils.h must be included after the above system header
101 files, because it redefines various system calls using macros.
102 This may be incompatible with the prototype declarations. */
104 #include "proc-utils.h"
106 /* Prototypes for supply_gregset etc. */
109 /* =================== TARGET_OPS "MODULE" =================== */
112 * This module defines the GDB target vector and its methods.
115 static void procfs_attach (struct target_ops *, char *, int);
116 static void procfs_detach (struct target_ops *, char *, int);
117 static void procfs_resume (struct target_ops *,
118 ptid_t, int, enum target_signal);
119 static void procfs_stop (ptid_t);
120 static void procfs_files_info (struct target_ops *);
121 static void procfs_fetch_registers (struct target_ops *,
122 struct regcache *, int);
123 static void procfs_store_registers (struct target_ops *,
124 struct regcache *, int);
125 static void procfs_notice_signals (ptid_t);
126 static void procfs_kill_inferior (struct target_ops *ops);
127 static void procfs_mourn_inferior (struct target_ops *ops);
128 static void procfs_create_inferior (struct target_ops *, char *,
129 char *, char **, int);
130 static ptid_t procfs_wait (struct target_ops *,
131 ptid_t, struct target_waitstatus *);
132 static int procfs_xfer_memory (CORE_ADDR, gdb_byte *, int, int,
133 struct mem_attrib *attrib,
134 struct target_ops *);
135 static LONGEST procfs_xfer_partial (struct target_ops *ops,
136 enum target_object object,
138 gdb_byte *readbuf, const gdb_byte *writebuf,
139 ULONGEST offset, LONGEST len);
141 static int procfs_thread_alive (struct target_ops *ops, ptid_t);
143 void procfs_find_new_threads (struct target_ops *ops);
144 char *procfs_pid_to_str (struct target_ops *, ptid_t);
146 static int proc_find_memory_regions (int (*) (CORE_ADDR,
152 static char * procfs_make_note_section (bfd *, int *);
154 static int procfs_can_use_hw_breakpoint (int, int, int);
156 #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
157 /* When GDB is built as 64-bit application on Solaris, the auxv data is
158 presented in 64-bit format. We need to provide a custom parser to handle
161 procfs_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
162 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
164 gdb_byte *ptr = *readptr;
169 if (endptr - ptr < 8 * 2)
172 *typep = extract_unsigned_integer (ptr, 4);
174 /* The size of data is always 64-bit. If the application is 32-bit,
175 it will be zero extended, as expected. */
176 *valp = extract_unsigned_integer (ptr, 8);
184 static struct target_ops *
187 struct target_ops *t = inf_child_target ();
189 t->to_shortname = "procfs";
190 t->to_longname = "Unix /proc child process";
192 "Unix /proc child process (started by the \"run\" command).";
193 t->to_create_inferior = procfs_create_inferior;
194 t->to_kill = procfs_kill_inferior;
195 t->to_mourn_inferior = procfs_mourn_inferior;
196 t->to_attach = procfs_attach;
197 t->to_detach = procfs_detach;
198 t->to_wait = procfs_wait;
199 t->to_resume = procfs_resume;
200 t->to_fetch_registers = procfs_fetch_registers;
201 t->to_store_registers = procfs_store_registers;
202 t->to_xfer_partial = procfs_xfer_partial;
203 t->deprecated_xfer_memory = procfs_xfer_memory;
204 t->to_notice_signals = procfs_notice_signals;
205 t->to_files_info = procfs_files_info;
206 t->to_stop = procfs_stop;
208 t->to_find_new_threads = procfs_find_new_threads;
209 t->to_thread_alive = procfs_thread_alive;
210 t->to_pid_to_str = procfs_pid_to_str;
212 t->to_has_thread_control = tc_schedlock;
213 t->to_find_memory_regions = proc_find_memory_regions;
214 t->to_make_corefile_notes = procfs_make_note_section;
215 t->to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
217 #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
218 t->to_auxv_parse = procfs_auxv_parse;
221 t->to_magic = OPS_MAGIC;
226 /* =================== END, TARGET_OPS "MODULE" =================== */
231 * Put any typedefs, defines etc. here that are required for
232 * the unification of code that handles different versions of /proc.
235 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
237 enum { READ_WATCHFLAG = WA_READ,
238 WRITE_WATCHFLAG = WA_WRITE,
239 EXEC_WATCHFLAG = WA_EXEC,
240 AFTER_WATCHFLAG = WA_TRAPAFTER
243 #else /* Irix method for watchpoints */
244 enum { READ_WATCHFLAG = MA_READ,
245 WRITE_WATCHFLAG = MA_WRITE,
246 EXEC_WATCHFLAG = MA_EXEC,
247 AFTER_WATCHFLAG = 0 /* trapafter not implemented */
252 #ifdef HAVE_PR_SIGSET_T
253 typedef pr_sigset_t gdb_sigset_t;
255 typedef sigset_t gdb_sigset_t;
259 #ifdef HAVE_PR_SIGACTION64_T
260 typedef pr_sigaction64_t gdb_sigaction_t;
262 typedef struct sigaction gdb_sigaction_t;
266 #ifdef HAVE_PR_SIGINFO64_T
267 typedef pr_siginfo64_t gdb_siginfo_t;
269 typedef struct siginfo gdb_siginfo_t;
272 /* gdb_premptysysset */
274 #define gdb_premptysysset premptysysset
276 #define gdb_premptysysset premptyset
281 #define gdb_praddsysset praddsysset
283 #define gdb_praddsysset praddset
288 #define gdb_prdelsysset prdelsysset
290 #define gdb_prdelsysset prdelset
293 /* prissyssetmember */
294 #ifdef prissyssetmember
295 #define gdb_pr_issyssetmember prissyssetmember
297 #define gdb_pr_issyssetmember prismember
300 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
301 as intuitively descriptive as it could be, so we'll define
302 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
303 this writing, this feature is only found on AIX5 systems and
304 basically means that the set of syscalls is not fixed. I.e,
305 there's no nice table that one can #include to get all of the
306 syscall numbers. Instead, they're stored in /proc/PID/sysent
307 for each process. We are at least guaranteed that they won't
308 change over the lifetime of the process. But each process could
309 (in theory) have different syscall numbers.
311 #ifdef HAVE_PRSYSENT_T
312 #define DYNAMIC_SYSCALLS
317 /* =================== STRUCT PROCINFO "MODULE" =================== */
319 /* FIXME: this comment will soon be out of date W.R.T. threads. */
321 /* The procinfo struct is a wrapper to hold all the state information
322 concerning a /proc process. There should be exactly one procinfo
323 for each process, and since GDB currently can debug only one
324 process at a time, that means there should be only one procinfo.
325 All of the LWP's of a process can be accessed indirectly thru the
326 single process procinfo.
328 However, against the day when GDB may debug more than one process,
329 this data structure is kept in a list (which for now will hold no
330 more than one member), and many functions will have a pointer to a
331 procinfo as an argument.
333 There will be a separate procinfo structure for use by the (not yet
334 implemented) "info proc" command, so that we can print useful
335 information about any random process without interfering with the
336 inferior's procinfo information. */
339 /* format strings for /proc paths */
340 # ifndef CTL_PROC_NAME_FMT
341 # define MAIN_PROC_NAME_FMT "/proc/%d"
342 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
343 # define AS_PROC_NAME_FMT "/proc/%d/as"
344 # define MAP_PROC_NAME_FMT "/proc/%d/map"
345 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
346 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
348 /* the name of the proc status struct depends on the implementation */
349 typedef pstatus_t gdb_prstatus_t;
350 typedef lwpstatus_t gdb_lwpstatus_t;
351 #else /* ! NEW_PROC_API */
352 /* format strings for /proc paths */
353 # ifndef CTL_PROC_NAME_FMT
354 # define MAIN_PROC_NAME_FMT "/proc/%05d"
355 # define CTL_PROC_NAME_FMT "/proc/%05d"
356 # define AS_PROC_NAME_FMT "/proc/%05d"
357 # define MAP_PROC_NAME_FMT "/proc/%05d"
358 # define STATUS_PROC_NAME_FMT "/proc/%05d"
359 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
361 /* the name of the proc status struct depends on the implementation */
362 typedef prstatus_t gdb_prstatus_t;
363 typedef prstatus_t gdb_lwpstatus_t;
364 #endif /* NEW_PROC_API */
366 typedef struct procinfo {
367 struct procinfo *next;
368 int pid; /* Process ID */
369 int tid; /* Thread/LWP id */
373 int ignore_next_sigstop;
375 /* The following four fd fields may be identical, or may contain
376 several different fd's, depending on the version of /proc
377 (old ioctl or new read/write). */
379 int ctl_fd; /* File descriptor for /proc control file */
381 * The next three file descriptors are actually only needed in the
382 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
383 * However, to avoid a bunch of #ifdefs in the code, we will use
384 * them uniformly by (in the case of the ioctl single-file-descriptor
385 * implementation) filling them with copies of the control fd.
387 int status_fd; /* File descriptor for /proc status file */
388 int as_fd; /* File descriptor for /proc as file */
390 char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */
392 fltset_t saved_fltset; /* Saved traced hardware fault set */
393 gdb_sigset_t saved_sigset; /* Saved traced signal set */
394 gdb_sigset_t saved_sighold; /* Saved held signal set */
395 sysset_t *saved_exitset; /* Saved traced system call exit set */
396 sysset_t *saved_entryset; /* Saved traced system call entry set */
398 gdb_prstatus_t prstatus; /* Current process status info */
401 gdb_fpregset_t fpregset; /* Current floating point registers */
404 #ifdef DYNAMIC_SYSCALLS
405 int num_syscalls; /* Total number of syscalls */
406 char **syscall_names; /* Syscall number to name map */
409 struct procinfo *thread_list;
411 int status_valid : 1;
413 int fpregs_valid : 1;
414 int threads_valid: 1;
417 static char errmsg[128]; /* shared error msg buffer */
419 /* Function prototypes for procinfo module: */
421 static procinfo *find_procinfo_or_die (int pid, int tid);
422 static procinfo *find_procinfo (int pid, int tid);
423 static procinfo *create_procinfo (int pid, int tid);
424 static void destroy_procinfo (procinfo * p);
425 static void do_destroy_procinfo_cleanup (void *);
426 static void dead_procinfo (procinfo * p, char *msg, int killp);
427 static int open_procinfo_files (procinfo * p, int which);
428 static void close_procinfo_files (procinfo * p);
429 static int sysset_t_size (procinfo *p);
430 static sysset_t *sysset_t_alloc (procinfo * pi);
431 #ifdef DYNAMIC_SYSCALLS
432 static void load_syscalls (procinfo *pi);
433 static void free_syscalls (procinfo *pi);
434 static int find_syscall (procinfo *pi, char *name);
435 #endif /* DYNAMIC_SYSCALLS */
437 /* The head of the procinfo list: */
438 static procinfo * procinfo_list;
441 * Function: find_procinfo
443 * Search the procinfo list.
445 * Returns: pointer to procinfo, or NULL if not found.
449 find_procinfo (int pid, int tid)
453 for (pi = procinfo_list; pi; pi = pi->next)
460 /* Don't check threads_valid. If we're updating the
461 thread_list, we want to find whatever threads are already
462 here. This means that in general it is the caller's
463 responsibility to check threads_valid and update before
464 calling find_procinfo, if the caller wants to find a new
467 for (pi = pi->thread_list; pi; pi = pi->next)
476 * Function: find_procinfo_or_die
478 * Calls find_procinfo, but errors on failure.
482 find_procinfo_or_die (int pid, int tid)
484 procinfo *pi = find_procinfo (pid, tid);
489 error (_("procfs: couldn't find pid %d (kernel thread %d) in procinfo list."),
492 error (_("procfs: couldn't find pid %d in procinfo list."), pid);
497 /* open_with_retry() is a wrapper for open(). The appropriate
498 open() call is attempted; if unsuccessful, it will be retried as
499 many times as needed for the EAGAIN and EINTR conditions.
501 For other conditions, open_with_retry() will retry the open() a
502 limited number of times. In addition, a short sleep is imposed
503 prior to retrying the open(). The reason for this sleep is to give
504 the kernel a chance to catch up and create the file in question in
505 the event that GDB "wins" the race to open a file before the kernel
509 open_with_retry (const char *pathname, int flags)
511 int retries_remaining, status;
513 retries_remaining = 2;
517 status = open (pathname, flags);
519 if (status >= 0 || retries_remaining == 0)
521 else if (errno != EINTR && errno != EAGAIN)
532 * Function: open_procinfo_files
534 * Open the file descriptor for the process or LWP.
535 * ifdef NEW_PROC_API, we only open the control file descriptor;
536 * the others are opened lazily as needed.
537 * else (if not NEW_PROC_API), there is only one real
538 * file descriptor, but we keep multiple copies of it so that
539 * the code that uses them does not have to be #ifdef'd.
541 * Return: file descriptor, or zero for failure.
544 enum { FD_CTL, FD_STATUS, FD_AS };
547 open_procinfo_files (procinfo *pi, int which)
550 char tmp[MAX_PROC_NAME_SIZE];
555 * This function is getting ALMOST long enough to break up into several.
556 * Here is some rationale:
558 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
559 * There are several file descriptors that may need to be open
560 * for any given process or LWP. The ones we're intereted in are:
561 * - control (ctl) write-only change the state
562 * - status (status) read-only query the state
563 * - address space (as) read/write access memory
564 * - map (map) read-only virtual addr map
565 * Most of these are opened lazily as they are needed.
566 * The pathnames for the 'files' for an LWP look slightly
567 * different from those of a first-class process:
568 * Pathnames for a process (<proc-id>):
569 * /proc/<proc-id>/ctl
570 * /proc/<proc-id>/status
572 * /proc/<proc-id>/map
573 * Pathnames for an LWP (lwp-id):
574 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
575 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
576 * An LWP has no map or address space file descriptor, since
577 * the memory map and address space are shared by all LWPs.
579 * Everyone else (Solaris 2.5, Irix, OSF)
580 * There is only one file descriptor for each process or LWP.
581 * For convenience, we copy the same file descriptor into all
582 * three fields of the procinfo struct (ctl_fd, status_fd, and
583 * as_fd, see NEW_PROC_API above) so that code that uses them
584 * doesn't need any #ifdef's.
589 * Each LWP has an independent file descriptor, but these
590 * are not obtained via the 'open' system call like the rest:
591 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
592 * to the file descriptor of the parent process.
595 * These do not even have their own independent file descriptor.
596 * All operations are carried out on the file descriptor of the
597 * parent process. Therefore we just call open again for each
598 * thread, getting a new handle for the same 'file'.
603 * In this case, there are several different file descriptors that
604 * we might be asked to open. The control file descriptor will be
605 * opened early, but the others will be opened lazily as they are
609 strcpy (tmp, pi->pathname);
610 switch (which) { /* which file descriptor to open? */
613 strcat (tmp, "/lwpctl");
615 strcat (tmp, "/ctl");
616 fd = open_with_retry (tmp, O_WRONLY);
623 return 0; /* there is no 'as' file descriptor for an lwp */
625 fd = open_with_retry (tmp, O_RDWR);
632 strcat (tmp, "/lwpstatus");
634 strcat (tmp, "/status");
635 fd = open_with_retry (tmp, O_RDONLY);
641 return 0; /* unknown file descriptor */
643 #else /* not NEW_PROC_API */
645 * In this case, there is only one file descriptor for each procinfo
646 * (ie. each process or LWP). In fact, only the file descriptor for
647 * the process can actually be opened by an 'open' system call.
648 * The ones for the LWPs have to be obtained thru an IOCTL call
649 * on the process's file descriptor.
651 * For convenience, we copy each procinfo's single file descriptor
652 * into all of the fields occupied by the several file descriptors
653 * of the NEW_PROC_API implementation. That way, the code that uses
654 * them can be written without ifdefs.
658 #ifdef PIOCTSTATUS /* OSF */
659 /* Only one FD; just open it. */
660 if ((fd = open_with_retry (pi->pathname, O_RDWR)) == 0)
662 #else /* Sol 2.5, Irix, other? */
663 if (pi->tid == 0) /* Master procinfo for the process */
665 fd = open_with_retry (pi->pathname, O_RDWR);
669 else /* LWP thread procinfo */
671 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
675 /* Find the procinfo for the entire process. */
676 if ((process = find_procinfo (pi->pid, 0)) == NULL)
679 /* Now obtain the file descriptor for the LWP. */
680 if ((fd = ioctl (process->ctl_fd, PIOCOPENLWP, &lwpid)) <= 0)
682 #else /* Irix, other? */
683 return 0; /* Don't know how to open threads */
684 #endif /* Sol 2.5 PIOCOPENLWP */
686 #endif /* OSF PIOCTSTATUS */
687 pi->ctl_fd = pi->as_fd = pi->status_fd = fd;
688 #endif /* NEW_PROC_API */
690 return 1; /* success */
694 * Function: create_procinfo
696 * Allocate a data structure and link it into the procinfo list.
697 * (First tries to find a pre-existing one (FIXME: why?)
699 * Return: pointer to new procinfo struct.
703 create_procinfo (int pid, int tid)
705 procinfo *pi, *parent = NULL;
707 if ((pi = find_procinfo (pid, tid)))
708 return pi; /* Already exists, nothing to do. */
710 /* find parent before doing malloc, to save having to cleanup */
712 parent = find_procinfo_or_die (pid, 0); /* FIXME: should I
714 doesn't exist yet? */
716 pi = (procinfo *) xmalloc (sizeof (procinfo));
717 memset (pi, 0, sizeof (procinfo));
721 #ifdef DYNAMIC_SYSCALLS
725 pi->saved_entryset = sysset_t_alloc (pi);
726 pi->saved_exitset = sysset_t_alloc (pi);
728 /* Chain into list. */
731 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
732 pi->next = procinfo_list;
738 sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid);
740 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
742 pi->next = parent->thread_list;
743 parent->thread_list = pi;
749 * Function: close_procinfo_files
751 * Close all file descriptors associated with the procinfo
755 close_procinfo_files (procinfo *pi)
762 if (pi->status_fd > 0)
763 close (pi->status_fd);
765 pi->ctl_fd = pi->as_fd = pi->status_fd = 0;
769 * Function: destroy_procinfo
771 * Destructor function. Close, unlink and deallocate the object.
775 destroy_one_procinfo (procinfo **list, procinfo *pi)
779 /* Step one: unlink the procinfo from its list */
783 for (ptr = *list; ptr; ptr = ptr->next)
786 ptr->next = pi->next;
790 /* Step two: close any open file descriptors */
791 close_procinfo_files (pi);
793 /* Step three: free the memory. */
794 #ifdef DYNAMIC_SYSCALLS
797 xfree (pi->saved_entryset);
798 xfree (pi->saved_exitset);
803 destroy_procinfo (procinfo *pi)
807 if (pi->tid != 0) /* destroy a thread procinfo */
809 tmp = find_procinfo (pi->pid, 0); /* find the parent process */
810 destroy_one_procinfo (&tmp->thread_list, pi);
812 else /* destroy a process procinfo and all its threads */
814 /* First destroy the children, if any; */
815 while (pi->thread_list != NULL)
816 destroy_one_procinfo (&pi->thread_list, pi->thread_list);
817 /* Then destroy the parent. Genocide!!! */
818 destroy_one_procinfo (&procinfo_list, pi);
823 do_destroy_procinfo_cleanup (void *pi)
825 destroy_procinfo (pi);
828 enum { NOKILL, KILL };
831 * Function: dead_procinfo
833 * To be called on a non_recoverable error for a procinfo.
834 * Prints error messages, optionally sends a SIGKILL to the process,
835 * then destroys the data structure.
839 dead_procinfo (procinfo *pi, char *msg, int kill_p)
845 print_sys_errmsg (pi->pathname, errno);
849 sprintf (procfile, "process %d", pi->pid);
850 print_sys_errmsg (procfile, errno);
853 kill (pi->pid, SIGKILL);
855 destroy_procinfo (pi);
860 * Function: sysset_t_size
862 * Returns the (complete) size of a sysset_t struct. Normally, this
863 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
864 * size of sysset_t isn't known until runtime.
868 sysset_t_size (procinfo * pi)
870 #ifndef DYNAMIC_SYSCALLS
871 return sizeof (sysset_t);
873 return sizeof (sysset_t) - sizeof (uint64_t)
874 + sizeof (uint64_t) * ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
875 / (8 * sizeof (uint64_t)));
879 /* Function: sysset_t_alloc
881 Allocate and (partially) initialize a sysset_t struct. */
884 sysset_t_alloc (procinfo * pi)
887 int size = sysset_t_size (pi);
888 ret = xmalloc (size);
889 #ifdef DYNAMIC_SYSCALLS
890 ret->pr_size = (pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
891 / (8 * sizeof (uint64_t));
896 #ifdef DYNAMIC_SYSCALLS
898 /* Function: load_syscalls
900 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
901 pi->num_syscalls with the number of syscalls and pi->syscall_names
902 with the names. (Certain numbers may be skipped in which case the
903 names for these numbers will be left as NULL.) */
905 #define MAX_SYSCALL_NAME_LENGTH 256
906 #define MAX_SYSCALLS 65536
909 load_syscalls (procinfo *pi)
911 char pathname[MAX_PROC_NAME_SIZE];
914 prsyscall_t *syscalls;
915 int i, size, maxcall;
917 pi->num_syscalls = 0;
918 pi->syscall_names = 0;
920 /* Open the file descriptor for the sysent file */
921 sprintf (pathname, "/proc/%d/sysent", pi->pid);
922 sysent_fd = open_with_retry (pathname, O_RDONLY);
925 error (_("load_syscalls: Can't open /proc/%d/sysent"), pi->pid);
928 size = sizeof header - sizeof (prsyscall_t);
929 if (read (sysent_fd, &header, size) != size)
931 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
934 if (header.pr_nsyscalls == 0)
936 error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"), pi->pid);
939 size = header.pr_nsyscalls * sizeof (prsyscall_t);
940 syscalls = xmalloc (size);
942 if (read (sysent_fd, syscalls, size) != size)
945 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
948 /* Find maximum syscall number. This may not be the same as
949 pr_nsyscalls since that value refers to the number of entries
950 in the table. (Also, the docs indicate that some system
951 call numbers may be skipped.) */
953 maxcall = syscalls[0].pr_number;
955 for (i = 1; i < header.pr_nsyscalls; i++)
956 if (syscalls[i].pr_number > maxcall
957 && syscalls[i].pr_nameoff > 0
958 && syscalls[i].pr_number < MAX_SYSCALLS)
959 maxcall = syscalls[i].pr_number;
961 pi->num_syscalls = maxcall+1;
962 pi->syscall_names = xmalloc (pi->num_syscalls * sizeof (char *));
964 for (i = 0; i < pi->num_syscalls; i++)
965 pi->syscall_names[i] = NULL;
967 /* Read the syscall names in */
968 for (i = 0; i < header.pr_nsyscalls; i++)
970 char namebuf[MAX_SYSCALL_NAME_LENGTH];
974 if (syscalls[i].pr_number >= MAX_SYSCALLS
975 || syscalls[i].pr_number < 0
976 || syscalls[i].pr_nameoff <= 0
977 || (lseek (sysent_fd, (off_t) syscalls[i].pr_nameoff, SEEK_SET)
978 != (off_t) syscalls[i].pr_nameoff))
981 nread = read (sysent_fd, namebuf, sizeof namebuf);
985 callnum = syscalls[i].pr_number;
987 if (pi->syscall_names[callnum] != NULL)
989 /* FIXME: Generate warning */
993 namebuf[nread-1] = '\0';
994 size = strlen (namebuf) + 1;
995 pi->syscall_names[callnum] = xmalloc (size);
996 strncpy (pi->syscall_names[callnum], namebuf, size-1);
997 pi->syscall_names[callnum][size-1] = '\0';
1004 /* Function: free_syscalls
1006 Free the space allocated for the syscall names from the procinfo
1010 free_syscalls (procinfo *pi)
1012 if (pi->syscall_names)
1016 for (i = 0; i < pi->num_syscalls; i++)
1017 if (pi->syscall_names[i] != NULL)
1018 xfree (pi->syscall_names[i]);
1020 xfree (pi->syscall_names);
1021 pi->syscall_names = 0;
1025 /* Function: find_syscall
1027 Given a name, look up (and return) the corresponding syscall number.
1028 If no match is found, return -1. */
1031 find_syscall (procinfo *pi, char *name)
1034 for (i = 0; i < pi->num_syscalls; i++)
1036 if (pi->syscall_names[i] && strcmp (name, pi->syscall_names[i]) == 0)
1043 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1045 /* =================== /proc "MODULE" =================== */
1048 * This "module" is the interface layer between the /proc system API
1049 * and the gdb target vector functions. This layer consists of
1050 * access functions that encapsulate each of the basic operations
1051 * that we need to use from the /proc API.
1053 * The main motivation for this layer is to hide the fact that
1054 * there are two very different implementations of the /proc API.
1055 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1056 * functions, we do our best to hide them all in here.
1059 int proc_get_status (procinfo * pi);
1060 long proc_flags (procinfo * pi);
1061 int proc_why (procinfo * pi);
1062 int proc_what (procinfo * pi);
1063 int proc_set_run_on_last_close (procinfo * pi);
1064 int proc_unset_run_on_last_close (procinfo * pi);
1065 int proc_set_inherit_on_fork (procinfo * pi);
1066 int proc_unset_inherit_on_fork (procinfo * pi);
1067 int proc_set_async (procinfo * pi);
1068 int proc_unset_async (procinfo * pi);
1069 int proc_stop_process (procinfo * pi);
1070 int proc_trace_signal (procinfo * pi, int signo);
1071 int proc_ignore_signal (procinfo * pi, int signo);
1072 int proc_clear_current_fault (procinfo * pi);
1073 int proc_set_current_signal (procinfo * pi, int signo);
1074 int proc_clear_current_signal (procinfo * pi);
1075 int proc_set_gregs (procinfo * pi);
1076 int proc_set_fpregs (procinfo * pi);
1077 int proc_wait_for_stop (procinfo * pi);
1078 int proc_run_process (procinfo * pi, int step, int signo);
1079 int proc_kill (procinfo * pi, int signo);
1080 int proc_parent_pid (procinfo * pi);
1081 int proc_get_nthreads (procinfo * pi);
1082 int proc_get_current_thread (procinfo * pi);
1083 int proc_set_held_signals (procinfo * pi, gdb_sigset_t * sighold);
1084 int proc_set_traced_sysexit (procinfo * pi, sysset_t * sysset);
1085 int proc_set_traced_sysentry (procinfo * pi, sysset_t * sysset);
1086 int proc_set_traced_faults (procinfo * pi, fltset_t * fltset);
1087 int proc_set_traced_signals (procinfo * pi, gdb_sigset_t * sigset);
1089 int proc_update_threads (procinfo * pi);
1090 int proc_iterate_over_threads (procinfo * pi,
1091 int (*func) (procinfo *, procinfo *, void *),
1094 gdb_gregset_t *proc_get_gregs (procinfo * pi);
1095 gdb_fpregset_t *proc_get_fpregs (procinfo * pi);
1096 sysset_t *proc_get_traced_sysexit (procinfo * pi, sysset_t * save);
1097 sysset_t *proc_get_traced_sysentry (procinfo * pi, sysset_t * save);
1098 fltset_t *proc_get_traced_faults (procinfo * pi, fltset_t * save);
1099 gdb_sigset_t *proc_get_traced_signals (procinfo * pi, gdb_sigset_t * save);
1100 gdb_sigset_t *proc_get_held_signals (procinfo * pi, gdb_sigset_t * save);
1101 gdb_sigset_t *proc_get_pending_signals (procinfo * pi, gdb_sigset_t * save);
1102 gdb_sigaction_t *proc_get_signal_actions (procinfo * pi, gdb_sigaction_t *save);
1104 void proc_warn (procinfo * pi, char *func, int line);
1105 void proc_error (procinfo * pi, char *func, int line);
1108 proc_warn (procinfo *pi, char *func, int line)
1110 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1111 print_sys_errmsg (errmsg, errno);
1115 proc_error (procinfo *pi, char *func, int line)
1117 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1118 perror_with_name (errmsg);
1122 * Function: proc_get_status
1124 * Updates the status struct in the procinfo.
1125 * There is a 'valid' flag, to let other functions know when
1126 * this function needs to be called (so the status is only
1127 * read when it is needed). The status file descriptor is
1128 * also only opened when it is needed.
1130 * Return: non-zero for success, zero for failure.
1134 proc_get_status (procinfo *pi)
1136 /* Status file descriptor is opened "lazily" */
1137 if (pi->status_fd == 0 &&
1138 open_procinfo_files (pi, FD_STATUS) == 0)
1140 pi->status_valid = 0;
1145 if (lseek (pi->status_fd, 0, SEEK_SET) < 0)
1146 pi->status_valid = 0; /* fail */
1149 /* Sigh... I have to read a different data structure,
1150 depending on whether this is a main process or an LWP. */
1152 pi->status_valid = (read (pi->status_fd,
1153 (char *) &pi->prstatus.pr_lwp,
1154 sizeof (lwpstatus_t))
1155 == sizeof (lwpstatus_t));
1158 pi->status_valid = (read (pi->status_fd,
1159 (char *) &pi->prstatus,
1160 sizeof (gdb_prstatus_t))
1161 == sizeof (gdb_prstatus_t));
1162 #if 0 /*def UNIXWARE*/
1163 if (pi->status_valid &&
1164 (pi->prstatus.pr_lwp.pr_flags & PR_ISTOP) &&
1165 pi->prstatus.pr_lwp.pr_why == PR_REQUESTED)
1166 /* Unixware peculiarity -- read the damn thing again! */
1167 pi->status_valid = (read (pi->status_fd,
1168 (char *) &pi->prstatus,
1169 sizeof (gdb_prstatus_t))
1170 == sizeof (gdb_prstatus_t));
1171 #endif /* UNIXWARE */
1174 #else /* ioctl method */
1175 #ifdef PIOCTSTATUS /* osf */
1176 if (pi->tid == 0) /* main process */
1178 /* Just read the danged status. Now isn't that simple? */
1180 (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1187 tid_t pr_error_thread;
1188 struct prstatus status;
1191 thread_status.pr_count = 1;
1192 thread_status.status.pr_tid = pi->tid;
1193 win = (ioctl (pi->status_fd, PIOCTSTATUS, &thread_status) >= 0);
1196 memcpy (&pi->prstatus, &thread_status.status,
1197 sizeof (pi->prstatus));
1198 pi->status_valid = 1;
1202 /* Just read the danged status. Now isn't that simple? */
1203 pi->status_valid = (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1207 if (pi->status_valid)
1209 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1212 proc_get_current_thread (pi));
1215 /* The status struct includes general regs, so mark them valid too */
1216 pi->gregs_valid = pi->status_valid;
1218 /* In the read/write multiple-fd model,
1219 the status struct includes the fp regs too, so mark them valid too */
1220 pi->fpregs_valid = pi->status_valid;
1222 return pi->status_valid; /* True if success, false if failure. */
1226 * Function: proc_flags
1228 * returns the process flags (pr_flags field).
1232 proc_flags (procinfo *pi)
1234 if (!pi->status_valid)
1235 if (!proc_get_status (pi))
1236 return 0; /* FIXME: not a good failure value (but what is?) */
1240 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1241 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1242 The two sets of flags don't overlap. */
1243 return pi->prstatus.pr_flags | pi->prstatus.pr_lwp.pr_flags;
1245 return pi->prstatus.pr_lwp.pr_flags;
1248 return pi->prstatus.pr_flags;
1253 * Function: proc_why
1255 * returns the pr_why field (why the process stopped).
1259 proc_why (procinfo *pi)
1261 if (!pi->status_valid)
1262 if (!proc_get_status (pi))
1263 return 0; /* FIXME: not a good failure value (but what is?) */
1266 return pi->prstatus.pr_lwp.pr_why;
1268 return pi->prstatus.pr_why;
1273 * Function: proc_what
1275 * returns the pr_what field (details of why the process stopped).
1279 proc_what (procinfo *pi)
1281 if (!pi->status_valid)
1282 if (!proc_get_status (pi))
1283 return 0; /* FIXME: not a good failure value (but what is?) */
1286 return pi->prstatus.pr_lwp.pr_what;
1288 return pi->prstatus.pr_what;
1292 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1294 * Function: proc_nsysarg
1296 * returns the pr_nsysarg field (number of args to the current syscall).
1300 proc_nsysarg (procinfo *pi)
1302 if (!pi->status_valid)
1303 if (!proc_get_status (pi))
1307 return pi->prstatus.pr_lwp.pr_nsysarg;
1309 return pi->prstatus.pr_nsysarg;
1314 * Function: proc_sysargs
1316 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1320 proc_sysargs (procinfo *pi)
1322 if (!pi->status_valid)
1323 if (!proc_get_status (pi))
1327 return (long *) &pi->prstatus.pr_lwp.pr_sysarg;
1329 return (long *) &pi->prstatus.pr_sysarg;
1334 * Function: proc_syscall
1336 * returns the pr_syscall field (id of current syscall if we are in one).
1340 proc_syscall (procinfo *pi)
1342 if (!pi->status_valid)
1343 if (!proc_get_status (pi))
1347 return pi->prstatus.pr_lwp.pr_syscall;
1349 return pi->prstatus.pr_syscall;
1352 #endif /* PIOCSSPCACT */
1355 * Function: proc_cursig:
1357 * returns the pr_cursig field (current signal).
1361 proc_cursig (struct procinfo *pi)
1363 if (!pi->status_valid)
1364 if (!proc_get_status (pi))
1365 return 0; /* FIXME: not a good failure value (but what is?) */
1368 return pi->prstatus.pr_lwp.pr_cursig;
1370 return pi->prstatus.pr_cursig;
1375 * Function: proc_modify_flag
1377 * === I appologize for the messiness of this function.
1378 * === This is an area where the different versions of
1379 * === /proc are more inconsistent than usual. MVS
1381 * Set or reset any of the following process flags:
1382 * PR_FORK -- forked child will inherit trace flags
1383 * PR_RLC -- traced process runs when last /proc file closed.
1384 * PR_KLC -- traced process is killed when last /proc file closed.
1385 * PR_ASYNC -- LWP's get to run/stop independently.
1387 * There are three methods for doing this function:
1388 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1390 * 2) Middle: PIOCSET/PIOCRESET
1392 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1395 * Note: Irix does not define PR_ASYNC.
1396 * Note: OSF does not define PR_KLC.
1397 * Note: OSF is the only one that can ONLY use the oldest method.
1400 * pi -- the procinfo
1401 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1402 * mode -- 1 for set, 0 for reset.
1404 * Returns non-zero for success, zero for failure.
1407 enum { FLAG_RESET, FLAG_SET };
1410 proc_modify_flag (procinfo *pi, long flag, long mode)
1412 long win = 0; /* default to fail */
1415 * These operations affect the process as a whole, and applying
1416 * them to an individual LWP has the same meaning as applying them
1417 * to the main process. Therefore, if we're ever called with a
1418 * pointer to an LWP's procinfo, let's substitute the process's
1419 * procinfo and avoid opening the LWP's file descriptor
1424 pi = find_procinfo_or_die (pi->pid, 0);
1426 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1427 /* First normalize the PCUNSET/PCRESET command opcode
1428 (which for no obvious reason has a different definition
1429 from one operating system to the next...) */
1431 #define GDBRESET PCUNSET
1434 #define GDBRESET PCRESET
1438 procfs_ctl_t arg[2];
1440 if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC) */
1442 else /* Reset the flag */
1446 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1449 #ifdef PIOCSET /* Irix/Sol5 method */
1450 if (mode == FLAG_SET) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1452 win = (ioctl (pi->ctl_fd, PIOCSET, &flag) >= 0);
1454 else /* Reset the flag */
1456 win = (ioctl (pi->ctl_fd, PIOCRESET, &flag) >= 0);
1460 #ifdef PIOCSRLC /* Oldest method: OSF */
1463 if (mode == FLAG_SET) /* Set run-on-last-close */
1465 win = (ioctl (pi->ctl_fd, PIOCSRLC, NULL) >= 0);
1467 else /* Clear run-on-last-close */
1469 win = (ioctl (pi->ctl_fd, PIOCRRLC, NULL) >= 0);
1473 if (mode == FLAG_SET) /* Set inherit-on-fork */
1475 win = (ioctl (pi->ctl_fd, PIOCSFORK, NULL) >= 0);
1477 else /* Clear inherit-on-fork */
1479 win = (ioctl (pi->ctl_fd, PIOCRFORK, NULL) >= 0);
1483 win = 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1490 /* The above operation renders the procinfo's cached pstatus obsolete. */
1491 pi->status_valid = 0;
1494 warning (_("procfs: modify_flag failed to turn %s %s"),
1495 flag == PR_FORK ? "PR_FORK" :
1496 flag == PR_RLC ? "PR_RLC" :
1498 flag == PR_ASYNC ? "PR_ASYNC" :
1501 flag == PR_KLC ? "PR_KLC" :
1504 mode == FLAG_RESET ? "off" : "on");
1510 * Function: proc_set_run_on_last_close
1512 * Set the run_on_last_close flag.
1513 * Process with all threads will become runnable
1514 * when debugger closes all /proc fds.
1516 * Returns non-zero for success, zero for failure.
1520 proc_set_run_on_last_close (procinfo *pi)
1522 return proc_modify_flag (pi, PR_RLC, FLAG_SET);
1526 * Function: proc_unset_run_on_last_close
1528 * Reset the run_on_last_close flag.
1529 * Process will NOT become runnable
1530 * when debugger closes its file handles.
1532 * Returns non-zero for success, zero for failure.
1536 proc_unset_run_on_last_close (procinfo *pi)
1538 return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
1543 * Function: proc_set_kill_on_last_close
1545 * Set the kill_on_last_close flag.
1546 * Process with all threads will be killed when debugger
1547 * closes all /proc fds (or debugger exits or dies).
1549 * Returns non-zero for success, zero for failure.
1553 proc_set_kill_on_last_close (procinfo *pi)
1555 return proc_modify_flag (pi, PR_KLC, FLAG_SET);
1559 * Function: proc_unset_kill_on_last_close
1561 * Reset the kill_on_last_close flag.
1562 * Process will NOT be killed when debugger
1563 * closes its file handles (or exits or dies).
1565 * Returns non-zero for success, zero for failure.
1569 proc_unset_kill_on_last_close (procinfo *pi)
1571 return proc_modify_flag (pi, PR_KLC, FLAG_RESET);
1576 * Function: proc_set_inherit_on_fork
1578 * Set inherit_on_fork flag.
1579 * If the process forks a child while we are registered for events
1580 * in the parent, then we will also recieve events from the child.
1582 * Returns non-zero for success, zero for failure.
1586 proc_set_inherit_on_fork (procinfo *pi)
1588 return proc_modify_flag (pi, PR_FORK, FLAG_SET);
1592 * Function: proc_unset_inherit_on_fork
1594 * Reset inherit_on_fork flag.
1595 * If the process forks a child while we are registered for events
1596 * in the parent, then we will NOT recieve events from the child.
1598 * Returns non-zero for success, zero for failure.
1602 proc_unset_inherit_on_fork (procinfo *pi)
1604 return proc_modify_flag (pi, PR_FORK, FLAG_RESET);
1609 * Function: proc_set_async
1611 * Set PR_ASYNC flag.
1612 * If one LWP stops because of a debug event (signal etc.),
1613 * the remaining LWPs will continue to run.
1615 * Returns non-zero for success, zero for failure.
1619 proc_set_async (procinfo *pi)
1621 return proc_modify_flag (pi, PR_ASYNC, FLAG_SET);
1625 * Function: proc_unset_async
1627 * Reset PR_ASYNC flag.
1628 * If one LWP stops because of a debug event (signal etc.),
1629 * then all other LWPs will stop as well.
1631 * Returns non-zero for success, zero for failure.
1635 proc_unset_async (procinfo *pi)
1637 return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET);
1639 #endif /* PR_ASYNC */
1642 * Function: proc_stop_process
1644 * Request the process/LWP to stop. Does not wait.
1645 * Returns non-zero for success, zero for failure.
1649 proc_stop_process (procinfo *pi)
1654 * We might conceivably apply this operation to an LWP, and
1655 * the LWP's ctl file descriptor might not be open.
1658 if (pi->ctl_fd == 0 &&
1659 open_procinfo_files (pi, FD_CTL) == 0)
1664 procfs_ctl_t cmd = PCSTOP;
1665 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1666 #else /* ioctl method */
1667 win = (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) >= 0);
1668 /* Note: the call also reads the prstatus. */
1671 pi->status_valid = 1;
1672 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1675 proc_get_current_thread (pi));
1684 * Function: proc_wait_for_stop
1686 * Wait for the process or LWP to stop (block until it does).
1687 * Returns non-zero for success, zero for failure.
1691 proc_wait_for_stop (procinfo *pi)
1696 * We should never have to apply this operation to any procinfo
1697 * except the one for the main process. If that ever changes
1698 * for any reason, then take out the following clause and
1699 * replace it with one that makes sure the ctl_fd is open.
1703 pi = find_procinfo_or_die (pi->pid, 0);
1707 procfs_ctl_t cmd = PCWSTOP;
1708 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1709 /* We been runnin' and we stopped -- need to update status. */
1710 pi->status_valid = 0;
1712 #else /* ioctl method */
1713 win = (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) >= 0);
1714 /* Above call also refreshes the prstatus. */
1717 pi->status_valid = 1;
1718 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1721 proc_get_current_thread (pi));
1729 * Function: proc_run_process
1731 * Make the process or LWP runnable.
1732 * Options (not all are implemented):
1734 * - clear current fault
1735 * - clear current signal
1736 * - abort the current system call
1737 * - stop as soon as finished with system call
1738 * - (ioctl): set traced signal set
1739 * - (ioctl): set held signal set
1740 * - (ioctl): set traced fault set
1741 * - (ioctl): set start pc (vaddr)
1742 * Always clear the current fault.
1743 * Clear the current signal if 'signo' is zero.
1746 * pi the process or LWP to operate on.
1747 * step if true, set the process or LWP to trap after one instr.
1748 * signo if zero, clear the current signal if any.
1749 * if non-zero, set the current signal to this one.
1751 * Returns non-zero for success, zero for failure.
1755 proc_run_process (procinfo *pi, int step, int signo)
1761 * We will probably have to apply this operation to individual threads,
1762 * so make sure the control file descriptor is open.
1765 if (pi->ctl_fd == 0 &&
1766 open_procinfo_files (pi, FD_CTL) == 0)
1771 runflags = PRCFAULT; /* always clear current fault */
1776 else if (signo != -1) /* -1 means do nothing W.R.T. signals */
1777 proc_set_current_signal (pi, signo);
1781 procfs_ctl_t cmd[2];
1785 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1787 #else /* ioctl method */
1791 memset (&prrun, 0, sizeof (prrun));
1792 prrun.pr_flags = runflags;
1793 win = (ioctl (pi->ctl_fd, PIOCRUN, &prrun) >= 0);
1801 * Function: proc_set_traced_signals
1803 * Register to trace signals in the process or LWP.
1804 * Returns non-zero for success, zero for failure.
1808 proc_set_traced_signals (procinfo *pi, gdb_sigset_t *sigset)
1813 * We should never have to apply this operation to any procinfo
1814 * except the one for the main process. If that ever changes
1815 * for any reason, then take out the following clause and
1816 * replace it with one that makes sure the ctl_fd is open.
1820 pi = find_procinfo_or_die (pi->pid, 0);
1826 /* Use char array to avoid alignment issues. */
1827 char sigset[sizeof (gdb_sigset_t)];
1831 memcpy (&arg.sigset, sigset, sizeof (gdb_sigset_t));
1833 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1835 #else /* ioctl method */
1836 win = (ioctl (pi->ctl_fd, PIOCSTRACE, sigset) >= 0);
1838 /* The above operation renders the procinfo's cached pstatus obsolete. */
1839 pi->status_valid = 0;
1842 warning (_("procfs: set_traced_signals failed"));
1847 * Function: proc_set_traced_faults
1849 * Register to trace hardware faults in the process or LWP.
1850 * Returns non-zero for success, zero for failure.
1854 proc_set_traced_faults (procinfo *pi, fltset_t *fltset)
1859 * We should never have to apply this operation to any procinfo
1860 * except the one for the main process. If that ever changes
1861 * for any reason, then take out the following clause and
1862 * replace it with one that makes sure the ctl_fd is open.
1866 pi = find_procinfo_or_die (pi->pid, 0);
1872 /* Use char array to avoid alignment issues. */
1873 char fltset[sizeof (fltset_t)];
1877 memcpy (&arg.fltset, fltset, sizeof (fltset_t));
1879 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1881 #else /* ioctl method */
1882 win = (ioctl (pi->ctl_fd, PIOCSFAULT, fltset) >= 0);
1884 /* The above operation renders the procinfo's cached pstatus obsolete. */
1885 pi->status_valid = 0;
1891 * Function: proc_set_traced_sysentry
1893 * Register to trace entry to system calls in the process or LWP.
1894 * Returns non-zero for success, zero for failure.
1898 proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset)
1903 * We should never have to apply this operation to any procinfo
1904 * except the one for the main process. If that ever changes
1905 * for any reason, then take out the following clause and
1906 * replace it with one that makes sure the ctl_fd is open.
1910 pi = find_procinfo_or_die (pi->pid, 0);
1914 struct gdb_proc_ctl_pcsentry {
1916 /* Use char array to avoid alignment issues. */
1917 char sysset[sizeof (sysset_t)];
1919 int argp_size = sizeof (struct gdb_proc_ctl_pcsentry)
1921 + sysset_t_size (pi);
1923 argp = xmalloc (argp_size);
1925 argp->cmd = PCSENTRY;
1926 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1928 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1931 #else /* ioctl method */
1932 win = (ioctl (pi->ctl_fd, PIOCSENTRY, sysset) >= 0);
1934 /* The above operation renders the procinfo's cached pstatus obsolete. */
1935 pi->status_valid = 0;
1941 * Function: proc_set_traced_sysexit
1943 * Register to trace exit from system calls in the process or LWP.
1944 * Returns non-zero for success, zero for failure.
1948 proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset)
1953 * We should never have to apply this operation to any procinfo
1954 * except the one for the main process. If that ever changes
1955 * for any reason, then take out the following clause and
1956 * replace it with one that makes sure the ctl_fd is open.
1960 pi = find_procinfo_or_die (pi->pid, 0);
1964 struct gdb_proc_ctl_pcsexit {
1966 /* Use char array to avoid alignment issues. */
1967 char sysset[sizeof (sysset_t)];
1969 int argp_size = sizeof (struct gdb_proc_ctl_pcsexit)
1971 + sysset_t_size (pi);
1973 argp = xmalloc (argp_size);
1975 argp->cmd = PCSEXIT;
1976 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1978 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1981 #else /* ioctl method */
1982 win = (ioctl (pi->ctl_fd, PIOCSEXIT, sysset) >= 0);
1984 /* The above operation renders the procinfo's cached pstatus obsolete. */
1985 pi->status_valid = 0;
1991 * Function: proc_set_held_signals
1993 * Specify the set of blocked / held signals in the process or LWP.
1994 * Returns non-zero for success, zero for failure.
1998 proc_set_held_signals (procinfo *pi, gdb_sigset_t *sighold)
2003 * We should never have to apply this operation to any procinfo
2004 * except the one for the main process. If that ever changes
2005 * for any reason, then take out the following clause and
2006 * replace it with one that makes sure the ctl_fd is open.
2010 pi = find_procinfo_or_die (pi->pid, 0);
2016 /* Use char array to avoid alignment issues. */
2017 char hold[sizeof (gdb_sigset_t)];
2021 memcpy (&arg.hold, sighold, sizeof (gdb_sigset_t));
2022 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2025 win = (ioctl (pi->ctl_fd, PIOCSHOLD, sighold) >= 0);
2027 /* The above operation renders the procinfo's cached pstatus obsolete. */
2028 pi->status_valid = 0;
2034 * Function: proc_get_pending_signals
2036 * returns the set of signals that are pending in the process or LWP.
2037 * Will also copy the sigset if 'save' is non-zero.
2041 proc_get_pending_signals (procinfo *pi, gdb_sigset_t *save)
2043 gdb_sigset_t *ret = NULL;
2046 * We should never have to apply this operation to any procinfo
2047 * except the one for the main process. If that ever changes
2048 * for any reason, then take out the following clause and
2049 * replace it with one that makes sure the ctl_fd is open.
2053 pi = find_procinfo_or_die (pi->pid, 0);
2055 if (!pi->status_valid)
2056 if (!proc_get_status (pi))
2060 ret = &pi->prstatus.pr_lwp.pr_lwppend;
2062 ret = &pi->prstatus.pr_sigpend;
2065 memcpy (save, ret, sizeof (gdb_sigset_t));
2071 * Function: proc_get_signal_actions
2073 * returns the set of signal actions.
2074 * Will also copy the sigactionset if 'save' is non-zero.
2078 proc_get_signal_actions (procinfo *pi, gdb_sigaction_t *save)
2080 gdb_sigaction_t *ret = NULL;
2083 * We should never have to apply this operation to any procinfo
2084 * except the one for the main process. If that ever changes
2085 * for any reason, then take out the following clause and
2086 * replace it with one that makes sure the ctl_fd is open.
2090 pi = find_procinfo_or_die (pi->pid, 0);
2092 if (!pi->status_valid)
2093 if (!proc_get_status (pi))
2097 ret = &pi->prstatus.pr_lwp.pr_action;
2099 ret = &pi->prstatus.pr_action;
2102 memcpy (save, ret, sizeof (gdb_sigaction_t));
2108 * Function: proc_get_held_signals
2110 * returns the set of signals that are held / blocked.
2111 * Will also copy the sigset if 'save' is non-zero.
2115 proc_get_held_signals (procinfo *pi, gdb_sigset_t *save)
2117 gdb_sigset_t *ret = NULL;
2120 * We should never have to apply this operation to any procinfo
2121 * except the one for the main process. If that ever changes
2122 * for any reason, then take out the following clause and
2123 * replace it with one that makes sure the ctl_fd is open.
2127 pi = find_procinfo_or_die (pi->pid, 0);
2130 if (!pi->status_valid)
2131 if (!proc_get_status (pi))
2135 ret = &pi->prstatus.pr_lwp.pr_context.uc_sigmask;
2137 ret = &pi->prstatus.pr_lwp.pr_lwphold;
2138 #endif /* UNIXWARE */
2139 #else /* not NEW_PROC_API */
2141 static gdb_sigset_t sigheld;
2143 if (ioctl (pi->ctl_fd, PIOCGHOLD, &sigheld) >= 0)
2146 #endif /* NEW_PROC_API */
2148 memcpy (save, ret, sizeof (gdb_sigset_t));
2154 * Function: proc_get_traced_signals
2156 * returns the set of signals that are traced / debugged.
2157 * Will also copy the sigset if 'save' is non-zero.
2161 proc_get_traced_signals (procinfo *pi, gdb_sigset_t *save)
2163 gdb_sigset_t *ret = NULL;
2166 * We should never have to apply this operation to any procinfo
2167 * except the one for the main process. If that ever changes
2168 * for any reason, then take out the following clause and
2169 * replace it with one that makes sure the ctl_fd is open.
2173 pi = find_procinfo_or_die (pi->pid, 0);
2176 if (!pi->status_valid)
2177 if (!proc_get_status (pi))
2180 ret = &pi->prstatus.pr_sigtrace;
2183 static gdb_sigset_t sigtrace;
2185 if (ioctl (pi->ctl_fd, PIOCGTRACE, &sigtrace) >= 0)
2190 memcpy (save, ret, sizeof (gdb_sigset_t));
2196 * Function: proc_trace_signal
2198 * Add 'signo' to the set of signals that are traced.
2199 * Returns non-zero for success, zero for failure.
2203 proc_trace_signal (procinfo *pi, int signo)
2208 * We should never have to apply this operation to any procinfo
2209 * except the one for the main process. If that ever changes
2210 * for any reason, then take out the following clause and
2211 * replace it with one that makes sure the ctl_fd is open.
2215 pi = find_procinfo_or_die (pi->pid, 0);
2219 if (proc_get_traced_signals (pi, &temp))
2221 praddset (&temp, signo);
2222 return proc_set_traced_signals (pi, &temp);
2226 return 0; /* failure */
2230 * Function: proc_ignore_signal
2232 * Remove 'signo' from the set of signals that are traced.
2233 * Returns non-zero for success, zero for failure.
2237 proc_ignore_signal (procinfo *pi, int signo)
2242 * We should never have to apply this operation to any procinfo
2243 * except the one for the main process. If that ever changes
2244 * for any reason, then take out the following clause and
2245 * replace it with one that makes sure the ctl_fd is open.
2249 pi = find_procinfo_or_die (pi->pid, 0);
2253 if (proc_get_traced_signals (pi, &temp))
2255 prdelset (&temp, signo);
2256 return proc_set_traced_signals (pi, &temp);
2260 return 0; /* failure */
2264 * Function: proc_get_traced_faults
2266 * returns the set of hardware faults that are traced /debugged.
2267 * Will also copy the faultset if 'save' is non-zero.
2271 proc_get_traced_faults (procinfo *pi, fltset_t *save)
2273 fltset_t *ret = NULL;
2276 * We should never have to apply this operation to any procinfo
2277 * except the one for the main process. If that ever changes
2278 * for any reason, then take out the following clause and
2279 * replace it with one that makes sure the ctl_fd is open.
2283 pi = find_procinfo_or_die (pi->pid, 0);
2286 if (!pi->status_valid)
2287 if (!proc_get_status (pi))
2290 ret = &pi->prstatus.pr_flttrace;
2293 static fltset_t flttrace;
2295 if (ioctl (pi->ctl_fd, PIOCGFAULT, &flttrace) >= 0)
2300 memcpy (save, ret, sizeof (fltset_t));
2306 * Function: proc_get_traced_sysentry
2308 * returns the set of syscalls that are traced /debugged on entry.
2309 * Will also copy the syscall set if 'save' is non-zero.
2313 proc_get_traced_sysentry (procinfo *pi, sysset_t *save)
2315 sysset_t *ret = NULL;
2318 * We should never have to apply this operation to any procinfo
2319 * except the one for the main process. If that ever changes
2320 * for any reason, then take out the following clause and
2321 * replace it with one that makes sure the ctl_fd is open.
2325 pi = find_procinfo_or_die (pi->pid, 0);
2328 if (!pi->status_valid)
2329 if (!proc_get_status (pi))
2332 #ifndef DYNAMIC_SYSCALLS
2333 ret = &pi->prstatus.pr_sysentry;
2334 #else /* DYNAMIC_SYSCALLS */
2336 static sysset_t *sysentry;
2340 sysentry = sysset_t_alloc (pi);
2342 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2344 if (pi->prstatus.pr_sysentry_offset == 0)
2346 gdb_premptysysset (sysentry);
2352 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysentry_offset,
2354 != (off_t) pi->prstatus.pr_sysentry_offset)
2356 size = sysset_t_size (pi);
2357 gdb_premptysysset (sysentry);
2358 rsize = read (pi->status_fd, sysentry, size);
2363 #endif /* DYNAMIC_SYSCALLS */
2364 #else /* !NEW_PROC_API */
2366 static sysset_t sysentry;
2368 if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysentry) >= 0)
2371 #endif /* NEW_PROC_API */
2373 memcpy (save, ret, sysset_t_size (pi));
2379 * Function: proc_get_traced_sysexit
2381 * returns the set of syscalls that are traced /debugged on exit.
2382 * Will also copy the syscall set if 'save' is non-zero.
2386 proc_get_traced_sysexit (procinfo *pi, sysset_t *save)
2388 sysset_t * ret = NULL;
2391 * We should never have to apply this operation to any procinfo
2392 * except the one for the main process. If that ever changes
2393 * for any reason, then take out the following clause and
2394 * replace it with one that makes sure the ctl_fd is open.
2398 pi = find_procinfo_or_die (pi->pid, 0);
2401 if (!pi->status_valid)
2402 if (!proc_get_status (pi))
2405 #ifndef DYNAMIC_SYSCALLS
2406 ret = &pi->prstatus.pr_sysexit;
2407 #else /* DYNAMIC_SYSCALLS */
2409 static sysset_t *sysexit;
2413 sysexit = sysset_t_alloc (pi);
2415 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2417 if (pi->prstatus.pr_sysexit_offset == 0)
2419 gdb_premptysysset (sysexit);
2425 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysexit_offset, SEEK_SET)
2426 != (off_t) pi->prstatus.pr_sysexit_offset)
2428 size = sysset_t_size (pi);
2429 gdb_premptysysset (sysexit);
2430 rsize = read (pi->status_fd, sysexit, size);
2435 #endif /* DYNAMIC_SYSCALLS */
2438 static sysset_t sysexit;
2440 if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysexit) >= 0)
2445 memcpy (save, ret, sysset_t_size (pi));
2451 * Function: proc_clear_current_fault
2453 * The current fault (if any) is cleared; the associated signal
2454 * will not be sent to the process or LWP when it resumes.
2455 * Returns non-zero for success, zero for failure.
2459 proc_clear_current_fault (procinfo *pi)
2464 * We should never have to apply this operation to any procinfo
2465 * except the one for the main process. If that ever changes
2466 * for any reason, then take out the following clause and
2467 * replace it with one that makes sure the ctl_fd is open.
2471 pi = find_procinfo_or_die (pi->pid, 0);
2475 procfs_ctl_t cmd = PCCFAULT;
2476 win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd));
2479 win = (ioctl (pi->ctl_fd, PIOCCFAULT, 0) >= 0);
2486 * Function: proc_set_current_signal
2488 * Set the "current signal" that will be delivered next to the process.
2489 * NOTE: semantics are different from those of KILL.
2490 * This signal will be delivered to the process or LWP
2491 * immediately when it is resumed (even if the signal is held/blocked);
2492 * it will NOT immediately cause another event of interest, and will NOT
2493 * first trap back to the debugger.
2495 * Returns non-zero for success, zero for failure.
2499 proc_set_current_signal (procinfo *pi, int signo)
2504 /* Use char array to avoid alignment issues. */
2505 char sinfo[sizeof (gdb_siginfo_t)];
2507 gdb_siginfo_t *mysinfo;
2509 struct target_waitstatus wait_status;
2512 * We should never have to apply this operation to any procinfo
2513 * except the one for the main process. If that ever changes
2514 * for any reason, then take out the following clause and
2515 * replace it with one that makes sure the ctl_fd is open.
2519 pi = find_procinfo_or_die (pi->pid, 0);
2521 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2522 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2523 * receives a PIOCSSIG with a signal identical to the current signal,
2524 * it messes up the current signal. Work around the kernel bug.
2527 signo == proc_cursig (pi))
2528 return 1; /* I assume this is a success? */
2531 /* The pointer is just a type alias. */
2532 mysinfo = (gdb_siginfo_t *) &arg.sinfo;
2533 get_last_target_status (&wait_ptid, &wait_status);
2534 if (ptid_equal (wait_ptid, inferior_ptid)
2535 && wait_status.kind == TARGET_WAITKIND_STOPPED
2536 && wait_status.value.sig == target_signal_from_host (signo)
2537 && proc_get_status (pi)
2539 && pi->prstatus.pr_lwp.pr_info.si_signo == signo
2541 && pi->prstatus.pr_info.si_signo == signo
2544 /* Use the siginfo associated with the signal being
2547 memcpy (mysinfo, &pi->prstatus.pr_lwp.pr_info, sizeof (gdb_siginfo_t));
2549 memcpy (mysinfo, &pi->prstatus.pr_info, sizeof (gdb_siginfo_t));
2553 mysinfo->si_signo = signo;
2554 mysinfo->si_code = 0;
2555 mysinfo->si_pid = getpid (); /* ?why? */
2556 mysinfo->si_uid = getuid (); /* ?why? */
2561 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2563 win = (ioctl (pi->ctl_fd, PIOCSSIG, (void *) &arg.sinfo) >= 0);
2570 * Function: proc_clear_current_signal
2572 * The current signal (if any) is cleared, and
2573 * is not sent to the process or LWP when it resumes.
2574 * Returns non-zero for success, zero for failure.
2578 proc_clear_current_signal (procinfo *pi)
2583 * We should never have to apply this operation to any procinfo
2584 * except the one for the main process. If that ever changes
2585 * for any reason, then take out the following clause and
2586 * replace it with one that makes sure the ctl_fd is open.
2590 pi = find_procinfo_or_die (pi->pid, 0);
2596 /* Use char array to avoid alignment issues. */
2597 char sinfo[sizeof (gdb_siginfo_t)];
2599 gdb_siginfo_t *mysinfo;
2602 /* The pointer is just a type alias. */
2603 mysinfo = (gdb_siginfo_t *) &arg.sinfo;
2604 mysinfo->si_signo = 0;
2605 mysinfo->si_code = 0;
2606 mysinfo->si_errno = 0;
2607 mysinfo->si_pid = getpid (); /* ?why? */
2608 mysinfo->si_uid = getuid (); /* ?why? */
2610 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2613 win = (ioctl (pi->ctl_fd, PIOCSSIG, 0) >= 0);
2619 /* Return the general-purpose registers for the process or LWP
2620 corresponding to PI. Upon failure, return NULL. */
2623 proc_get_gregs (procinfo *pi)
2625 if (!pi->status_valid || !pi->gregs_valid)
2626 if (!proc_get_status (pi))
2629 /* OK, sorry about the ifdef's. There's three cases instead of two,
2630 because in this case Unixware and Solaris/RW differ. */
2633 # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */
2634 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs;
2636 return &pi->prstatus.pr_lwp.pr_reg;
2639 return &pi->prstatus.pr_reg;
2643 /* Return the general-purpose registers for the process or LWP
2644 corresponding to PI. Upon failure, return NULL. */
2647 proc_get_fpregs (procinfo *pi)
2650 if (!pi->status_valid || !pi->fpregs_valid)
2651 if (!proc_get_status (pi))
2654 # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */
2655 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs;
2657 return &pi->prstatus.pr_lwp.pr_fpreg;
2660 #else /* not NEW_PROC_API */
2661 if (pi->fpregs_valid)
2662 return &pi->fpregset; /* Already got 'em. */
2665 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2674 tid_t pr_error_thread;
2675 tfpregset_t thread_1;
2678 thread_fpregs.pr_count = 1;
2679 thread_fpregs.thread_1.tid = pi->tid;
2682 && ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2684 pi->fpregs_valid = 1;
2685 return &pi->fpregset; /* Got 'em now! */
2687 else if (pi->tid != 0
2688 && ioctl (pi->ctl_fd, PIOCTGFPREG, &thread_fpregs) >= 0)
2690 memcpy (&pi->fpregset, &thread_fpregs.thread_1.pr_fpregs,
2691 sizeof (pi->fpregset));
2692 pi->fpregs_valid = 1;
2693 return &pi->fpregset; /* Got 'em now! */
2700 if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2702 pi->fpregs_valid = 1;
2703 return &pi->fpregset; /* Got 'em now! */
2712 #endif /* NEW_PROC_API */
2715 /* Write the general-purpose registers back to the process or LWP
2716 corresponding to PI. Return non-zero for success, zero for
2720 proc_set_gregs (procinfo *pi)
2722 gdb_gregset_t *gregs;
2725 gregs = proc_get_gregs (pi);
2727 return 0; /* proc_get_regs has already warned. */
2729 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2738 /* Use char array to avoid alignment issues. */
2739 char gregs[sizeof (gdb_gregset_t)];
2743 memcpy (&arg.gregs, gregs, sizeof (arg.gregs));
2744 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2746 win = (ioctl (pi->ctl_fd, PIOCSREG, gregs) >= 0);
2750 /* Policy: writing the registers invalidates our cache. */
2751 pi->gregs_valid = 0;
2755 /* Write the floating-pointer registers back to the process or LWP
2756 corresponding to PI. Return non-zero for success, zero for
2760 proc_set_fpregs (procinfo *pi)
2762 gdb_fpregset_t *fpregs;
2765 fpregs = proc_get_fpregs (pi);
2767 return 0; /* proc_get_fpregs has already warned. */
2769 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2778 /* Use char array to avoid alignment issues. */
2779 char fpregs[sizeof (gdb_fpregset_t)];
2783 memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs));
2784 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2788 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2793 tid_t pr_error_thread;
2794 tfpregset_t thread_1;
2797 thread_fpregs.pr_count = 1;
2798 thread_fpregs.thread_1.tid = pi->tid;
2799 memcpy (&thread_fpregs.thread_1.pr_fpregs, fpregs,
2801 win = (ioctl (pi->ctl_fd, PIOCTSFPREG, &thread_fpregs) >= 0);
2804 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2806 #endif /* NEW_PROC_API */
2809 /* Policy: writing the registers invalidates our cache. */
2810 pi->fpregs_valid = 0;
2815 * Function: proc_kill
2817 * Send a signal to the proc or lwp with the semantics of "kill()".
2818 * Returns non-zero for success, zero for failure.
2822 proc_kill (procinfo *pi, int signo)
2827 * We might conceivably apply this operation to an LWP, and
2828 * the LWP's ctl file descriptor might not be open.
2831 if (pi->ctl_fd == 0 &&
2832 open_procinfo_files (pi, FD_CTL) == 0)
2839 procfs_ctl_t cmd[2];
2843 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
2844 #else /* ioctl method */
2845 /* FIXME: do I need the Alpha OSF fixups present in
2846 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2847 win = (ioctl (pi->ctl_fd, PIOCKILL, &signo) >= 0);
2855 * Function: proc_parent_pid
2857 * Find the pid of the process that started this one.
2858 * Returns the parent process pid, or zero.
2862 proc_parent_pid (procinfo *pi)
2865 * We should never have to apply this operation to any procinfo
2866 * except the one for the main process. If that ever changes
2867 * for any reason, then take out the following clause and
2868 * replace it with one that makes sure the ctl_fd is open.
2872 pi = find_procinfo_or_die (pi->pid, 0);
2874 if (!pi->status_valid)
2875 if (!proc_get_status (pi))
2878 return pi->prstatus.pr_ppid;
2882 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2883 (a.k.a void pointer)! */
2886 procfs_address_to_host_pointer (CORE_ADDR addr)
2888 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
2891 gdb_assert (sizeof (ptr) == TYPE_LENGTH (ptr_type));
2892 gdbarch_address_to_pointer (target_gdbarch, ptr_type,
2893 (gdb_byte *) &ptr, addr);
2898 * Function: proc_set_watchpoint
2903 proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags)
2905 #if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2908 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2909 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2914 char watch[sizeof (prwatch_t)];
2918 pwatch = (prwatch_t *) &arg.watch;
2919 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2920 convert a target address into something that can be stored in a
2921 native data structure. */
2922 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2923 pwatch->pr_vaddr = (uintptr_t) procfs_address_to_host_pointer (addr);
2925 pwatch->pr_vaddr = (caddr_t) procfs_address_to_host_pointer (addr);
2927 pwatch->pr_size = len;
2928 pwatch->pr_wflags = wflags;
2929 #if defined(NEW_PROC_API) && defined (PCWATCH)
2931 return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg));
2933 #if defined (PIOCSWATCH)
2934 return (ioctl (pi->ctl_fd, PIOCSWATCH, pwatch) >= 0);
2936 return 0; /* Fail */
2943 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
2945 #include <sys/sysi86.h>
2948 * Function: proc_get_LDT_entry
2954 * The 'key' is actually the value of the lower 16 bits of
2955 * the GS register for the LWP that we're interested in.
2957 * Return: matching ssh struct (LDT entry).
2961 proc_get_LDT_entry (procinfo *pi, int key)
2963 static struct ssd *ldt_entry = NULL;
2965 char pathname[MAX_PROC_NAME_SIZE];
2966 struct cleanup *old_chain = NULL;
2969 /* Allocate space for one LDT entry.
2970 This alloc must persist, because we return a pointer to it. */
2971 if (ldt_entry == NULL)
2972 ldt_entry = (struct ssd *) xmalloc (sizeof (struct ssd));
2974 /* Open the file descriptor for the LDT table. */
2975 sprintf (pathname, "/proc/%d/ldt", pi->pid);
2976 if ((fd = open_with_retry (pathname, O_RDONLY)) < 0)
2978 proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__);
2981 /* Make sure it gets closed again! */
2982 old_chain = make_cleanup_close (fd);
2984 /* Now 'read' thru the table, find a match and return it. */
2985 while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd))
2987 if (ldt_entry->sel == 0 &&
2988 ldt_entry->bo == 0 &&
2989 ldt_entry->acc1 == 0 &&
2990 ldt_entry->acc2 == 0)
2991 break; /* end of table */
2992 /* If key matches, return this entry. */
2993 if (ldt_entry->sel == key)
2996 /* Loop ended, match not found. */
3000 static int nalloc = 0;
3002 /* Get the number of LDT entries. */
3003 if (ioctl (pi->ctl_fd, PIOCNLDT, &nldt) < 0)
3005 proc_warn (pi, "proc_get_LDT_entry (PIOCNLDT)", __LINE__);
3009 /* Allocate space for the number of LDT entries. */
3010 /* This alloc has to persist, 'cause we return a pointer to it. */
3013 ldt_entry = (struct ssd *)
3014 xrealloc (ldt_entry, (nldt + 1) * sizeof (struct ssd));
3018 /* Read the whole table in one gulp. */
3019 if (ioctl (pi->ctl_fd, PIOCLDT, ldt_entry) < 0)
3021 proc_warn (pi, "proc_get_LDT_entry (PIOCLDT)", __LINE__);
3025 /* Search the table and return the (first) entry matching 'key'. */
3026 for (i = 0; i < nldt; i++)
3027 if (ldt_entry[i].sel == key)
3028 return &ldt_entry[i];
3030 /* Loop ended, match not found. */
3036 * Function: procfs_find_LDT_entry
3039 * ptid_t ptid; // The GDB-style pid-plus-LWP.
3042 * pointer to the corresponding LDT entry.
3046 procfs_find_LDT_entry (ptid_t ptid)
3048 gdb_gregset_t *gregs;
3052 /* Find procinfo for the lwp. */
3053 if ((pi = find_procinfo (PIDGET (ptid), TIDGET (ptid))) == NULL)
3055 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
3056 PIDGET (ptid), TIDGET (ptid));
3059 /* get its general registers. */
3060 if ((gregs = proc_get_gregs (pi)) == NULL)
3062 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
3063 PIDGET (ptid), TIDGET (ptid));
3066 /* Now extract the GS register's lower 16 bits. */
3067 key = (*gregs)[GS] & 0xffff;
3069 /* Find the matching entry and return it. */
3070 return proc_get_LDT_entry (pi, key);
3075 /* =============== END, non-thread part of /proc "MODULE" =============== */
3077 /* =================== Thread "MODULE" =================== */
3079 /* NOTE: you'll see more ifdefs and duplication of functions here,
3080 since there is a different way to do threads on every OS. */
3083 * Function: proc_get_nthreads
3085 * Return the number of threads for the process
3088 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3093 proc_get_nthreads (procinfo *pi)
3097 if (ioctl (pi->ctl_fd, PIOCNTHR, &nthreads) < 0)
3098 proc_warn (pi, "procfs: PIOCNTHR failed", __LINE__);
3104 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3106 * Solaris and Unixware version
3109 proc_get_nthreads (procinfo *pi)
3111 if (!pi->status_valid)
3112 if (!proc_get_status (pi))
3116 * NEW_PROC_API: only works for the process procinfo,
3117 * because the LWP procinfos do not get prstatus filled in.
3120 if (pi->tid != 0) /* find the parent process procinfo */
3121 pi = find_procinfo_or_die (pi->pid, 0);
3123 return pi->prstatus.pr_nlwp;
3131 proc_get_nthreads (procinfo *pi)
3139 * Function: proc_get_current_thread (LWP version)
3141 * Return the ID of the thread that had an event of interest.
3142 * (ie. the one that hit a breakpoint or other traced event).
3143 * All other things being equal, this should be the ID of a
3144 * thread that is currently executing.
3147 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3149 * Solaris and Unixware version
3152 proc_get_current_thread (procinfo *pi)
3155 * Note: this should be applied to the root procinfo for the process,
3156 * not to the procinfo for an LWP. If applied to the procinfo for
3157 * an LWP, it will simply return that LWP's ID. In that case,
3158 * find the parent process procinfo.
3162 pi = find_procinfo_or_die (pi->pid, 0);
3164 if (!pi->status_valid)
3165 if (!proc_get_status (pi))
3169 return pi->prstatus.pr_lwp.pr_lwpid;
3171 return pi->prstatus.pr_who;
3176 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3181 proc_get_current_thread (procinfo *pi)
3183 #if 0 /* FIXME: not ready for prime time? */
3184 return pi->prstatus.pr_tid;
3195 proc_get_current_thread (procinfo *pi)
3204 * Function: proc_update_threads
3206 * Discover the IDs of all the threads within the process, and
3207 * create a procinfo for each of them (chained to the parent).
3209 * This unfortunately requires a different method on every OS.
3211 * Return: non-zero for success, zero for failure.
3215 proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore)
3217 if (thread && parent) /* sanity */
3219 thread->status_valid = 0;
3220 if (!proc_get_status (thread))
3221 destroy_one_procinfo (&parent->thread_list, thread);
3223 return 0; /* keep iterating */
3226 #if defined (PIOCLSTATUS)
3228 * Solaris 2.5 (ioctl) version
3231 proc_update_threads (procinfo *pi)
3233 gdb_prstatus_t *prstatus;
3234 struct cleanup *old_chain = NULL;
3239 * We should never have to apply this operation to any procinfo
3240 * except the one for the main process. If that ever changes
3241 * for any reason, then take out the following clause and
3242 * replace it with one that makes sure the ctl_fd is open.
3246 pi = find_procinfo_or_die (pi->pid, 0);
3248 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3250 if ((nlwp = proc_get_nthreads (pi)) <= 1)
3251 return 1; /* Process is not multi-threaded; nothing to do. */
3253 prstatus = xmalloc (sizeof (gdb_prstatus_t) * (nlwp + 1));
3255 old_chain = make_cleanup (xfree, prstatus);
3256 if (ioctl (pi->ctl_fd, PIOCLSTATUS, prstatus) < 0)
3257 proc_error (pi, "update_threads (PIOCLSTATUS)", __LINE__);
3259 /* Skip element zero, which represents the process as a whole. */
3260 for (i = 1; i < nlwp + 1; i++)
3262 if ((thread = create_procinfo (pi->pid, prstatus[i].pr_who)) == NULL)
3263 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3265 memcpy (&thread->prstatus, &prstatus[i], sizeof (*prstatus));
3266 thread->status_valid = 1;
3268 pi->threads_valid = 1;
3269 do_cleanups (old_chain);
3275 * Unixware and Solaris 6 (and later) version
3278 do_closedir_cleanup (void *dir)
3284 proc_update_threads (procinfo *pi)
3286 char pathname[MAX_PROC_NAME_SIZE + 16];
3287 struct dirent *direntry;
3288 struct cleanup *old_chain = NULL;
3294 * We should never have to apply this operation to any procinfo
3295 * except the one for the main process. If that ever changes
3296 * for any reason, then take out the following clause and
3297 * replace it with one that makes sure the ctl_fd is open.
3301 pi = find_procinfo_or_die (pi->pid, 0);
3303 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3308 * Note: this brute-force method is the only way I know of
3309 * to accomplish this task on Unixware. This method will
3310 * also work on Solaris 2.6 and 2.7. There is a much simpler
3311 * and more elegant way to do this on Solaris, but the margins
3312 * of this manuscript are too small to write it here... ;-)
3315 strcpy (pathname, pi->pathname);
3316 strcat (pathname, "/lwp");
3317 if ((dirp = opendir (pathname)) == NULL)
3318 proc_error (pi, "update_threads, opendir", __LINE__);
3320 old_chain = make_cleanup (do_closedir_cleanup, dirp);
3321 while ((direntry = readdir (dirp)) != NULL)
3322 if (direntry->d_name[0] != '.') /* skip '.' and '..' */
3324 lwpid = atoi (&direntry->d_name[0]);
3325 if ((thread = create_procinfo (pi->pid, lwpid)) == NULL)
3326 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3328 pi->threads_valid = 1;
3329 do_cleanups (old_chain);
3338 proc_update_threads (procinfo *pi)
3344 * We should never have to apply this operation to any procinfo
3345 * except the one for the main process. If that ever changes
3346 * for any reason, then take out the following clause and
3347 * replace it with one that makes sure the ctl_fd is open.
3351 pi = find_procinfo_or_die (pi->pid, 0);
3353 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3355 nthreads = proc_get_nthreads (pi);
3357 return 0; /* nothing to do for 1 or fewer threads */
3359 threads = xmalloc (nthreads * sizeof (tid_t));
3361 if (ioctl (pi->ctl_fd, PIOCTLIST, threads) < 0)
3362 proc_error (pi, "procfs: update_threads (PIOCTLIST)", __LINE__);
3364 for (i = 0; i < nthreads; i++)
3366 if (!find_procinfo (pi->pid, threads[i]))
3367 if (!create_procinfo (pi->pid, threads[i]))
3368 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3370 pi->threads_valid = 1;
3378 proc_update_threads (procinfo *pi)
3382 #endif /* OSF PIOCTLIST */
3383 #endif /* NEW_PROC_API */
3384 #endif /* SOL 2.5 PIOCLSTATUS */
3387 * Function: proc_iterate_over_threads
3390 * Given a pointer to a function, call that function once
3391 * for each lwp in the procinfo list, until the function
3392 * returns non-zero, in which event return the value
3393 * returned by the function.
3395 * Note: this function does NOT call update_threads.
3396 * If you want to discover new threads first, you must
3397 * call that function explicitly. This function just makes
3398 * a quick pass over the currently-known procinfos.
3401 * pi - parent process procinfo
3402 * func - per-thread function
3403 * ptr - opaque parameter for function.
3406 * First non-zero return value from the callee, or zero.
3410 proc_iterate_over_threads (procinfo *pi,
3411 int (*func) (procinfo *, procinfo *, void *),
3414 procinfo *thread, *next;
3418 * We should never have to apply this operation to any procinfo
3419 * except the one for the main process. If that ever changes
3420 * for any reason, then take out the following clause and
3421 * replace it with one that makes sure the ctl_fd is open.
3425 pi = find_procinfo_or_die (pi->pid, 0);
3427 for (thread = pi->thread_list; thread != NULL; thread = next)
3429 next = thread->next; /* in case thread is destroyed */
3430 if ((retval = (*func) (pi, thread, ptr)) != 0)
3437 /* =================== END, Thread "MODULE" =================== */
3439 /* =================== END, /proc "MODULE" =================== */
3441 /* =================== GDB "MODULE" =================== */
3444 * Here are all of the gdb target vector functions and their friends.
3447 static ptid_t do_attach (ptid_t ptid);
3448 static void do_detach (int signo);
3449 static int register_gdb_signals (procinfo *, gdb_sigset_t *);
3450 static void proc_trace_syscalls_1 (procinfo *pi, int syscallnum,
3451 int entry_or_exit, int mode, int from_tty);
3452 static int insert_dbx_link_breakpoint (procinfo *pi);
3453 static void remove_dbx_link_breakpoint (void);
3455 /* On mips-irix, we need to insert a breakpoint at __dbx_link during
3456 the startup phase. The following two variables are used to record
3457 the address of the breakpoint, and the code that was replaced by
3459 static int dbx_link_bpt_addr = 0;
3460 static void *dbx_link_bpt;
3463 * Function: procfs_debug_inferior
3465 * Sets up the inferior to be debugged.
3466 * Registers to trace signals, hardware faults, and syscalls.
3467 * Note: does not set RLC flag: caller may want to customize that.
3469 * Returns: zero for success (note! unlike most functions in this module)
3470 * On failure, returns the LINE NUMBER where it failed!
3474 procfs_debug_inferior (procinfo *pi)
3476 fltset_t traced_faults;
3477 gdb_sigset_t traced_signals;
3478 sysset_t *traced_syscall_entries;
3479 sysset_t *traced_syscall_exits;
3482 #ifdef PROCFS_DONT_TRACE_FAULTS
3483 /* On some systems (OSF), we don't trace hardware faults.
3484 Apparently it's enough that we catch them as signals.
3485 Wonder why we don't just do that in general? */
3486 premptyset (&traced_faults); /* don't trace faults. */
3488 /* Register to trace hardware faults in the child. */
3489 prfillset (&traced_faults); /* trace all faults... */
3490 prdelset (&traced_faults, FLTPAGE); /* except page fault. */
3492 if (!proc_set_traced_faults (pi, &traced_faults))
3495 /* Register to trace selected signals in the child. */
3496 premptyset (&traced_signals);
3497 if (!register_gdb_signals (pi, &traced_signals))
3501 /* Register to trace the 'exit' system call (on entry). */
3502 traced_syscall_entries = sysset_t_alloc (pi);
3503 gdb_premptysysset (traced_syscall_entries);
3505 gdb_praddsysset (traced_syscall_entries, SYS_exit);
3508 gdb_praddsysset (traced_syscall_entries, SYS_lwpexit); /* And _lwp_exit... */
3511 gdb_praddsysset (traced_syscall_entries, SYS_lwp_exit);
3513 #ifdef DYNAMIC_SYSCALLS
3515 int callnum = find_syscall (pi, "_exit");
3517 gdb_praddsysset (traced_syscall_entries, callnum);
3521 status = proc_set_traced_sysentry (pi, traced_syscall_entries);
3522 xfree (traced_syscall_entries);
3526 #ifdef PRFS_STOPEXEC /* defined on OSF */
3527 /* OSF method for tracing exec syscalls. Quoting:
3528 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3529 exits from exec system calls because of the user level loader. */
3530 /* FIXME: make nice and maybe move into an access function. */
3534 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
3537 prfs_flags |= PRFS_STOPEXEC;
3539 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
3542 #else /* not PRFS_STOPEXEC */
3543 /* Everyone else's (except OSF) method for tracing exec syscalls */
3545 Not all systems with /proc have all the exec* syscalls with the same
3546 names. On the SGI, for example, there is no SYS_exec, but there
3547 *is* a SYS_execv. So, we try to account for that. */
3549 traced_syscall_exits = sysset_t_alloc (pi);
3550 gdb_premptysysset (traced_syscall_exits);
3552 gdb_praddsysset (traced_syscall_exits, SYS_exec);
3555 gdb_praddsysset (traced_syscall_exits, SYS_execve);
3558 gdb_praddsysset (traced_syscall_exits, SYS_execv);
3561 #ifdef SYS_lwpcreate
3562 gdb_praddsysset (traced_syscall_exits, SYS_lwpcreate);
3563 gdb_praddsysset (traced_syscall_exits, SYS_lwpexit);
3566 #ifdef SYS_lwp_create /* FIXME: once only, please */
3567 gdb_praddsysset (traced_syscall_exits, SYS_lwp_create);
3568 gdb_praddsysset (traced_syscall_exits, SYS_lwp_exit);
3571 #ifdef DYNAMIC_SYSCALLS
3573 int callnum = find_syscall (pi, "execve");
3575 gdb_praddsysset (traced_syscall_exits, callnum);
3576 callnum = find_syscall (pi, "ra_execve");
3578 gdb_praddsysset (traced_syscall_exits, callnum);
3582 status = proc_set_traced_sysexit (pi, traced_syscall_exits);
3583 xfree (traced_syscall_exits);
3587 #endif /* PRFS_STOPEXEC */
3592 procfs_attach (struct target_ops *ops, char *args, int from_tty)
3598 error_no_arg (_("process-id to attach"));
3601 if (pid == getpid ())
3602 error (_("Attaching GDB to itself is not a good idea..."));
3606 exec_file = get_exec_file (0);
3609 printf_filtered (_("Attaching to program `%s', %s\n"),
3610 exec_file, target_pid_to_str (pid_to_ptid (pid)));
3612 printf_filtered (_("Attaching to %s\n"),
3613 target_pid_to_str (pid_to_ptid (pid)));
3617 inferior_ptid = do_attach (pid_to_ptid (pid));
3622 procfs_detach (struct target_ops *ops, char *args, int from_tty)
3625 int pid = PIDGET (inferior_ptid);
3634 exec_file = get_exec_file (0);
3635 if (exec_file == NULL)
3638 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file,
3639 target_pid_to_str (pid_to_ptid (pid)));
3640 gdb_flush (gdb_stdout);
3645 inferior_ptid = null_ptid;
3646 detach_inferior (pid);
3647 unpush_target (ops);
3651 do_attach (ptid_t ptid)
3654 struct inferior *inf;
3658 if ((pi = create_procinfo (PIDGET (ptid), 0)) == NULL)
3659 perror (_("procfs: out of memory in 'attach'"));
3661 if (!open_procinfo_files (pi, FD_CTL))
3663 fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__);
3664 sprintf (errmsg, "do_attach: couldn't open /proc file for process %d",
3666 dead_procinfo (pi, errmsg, NOKILL);
3669 /* Stop the process (if it isn't already stopped). */
3670 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
3672 pi->was_stopped = 1;
3673 proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
3677 pi->was_stopped = 0;
3678 /* Set the process to run again when we close it. */
3679 if (!proc_set_run_on_last_close (pi))
3680 dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
3682 /* Now stop the process. */
3683 if (!proc_stop_process (pi))
3684 dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
3685 pi->ignore_next_sigstop = 1;
3687 /* Save some of the /proc state to be restored if we detach. */
3688 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
3689 dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
3690 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
3691 dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
3692 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
3693 dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
3695 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
3696 dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
3698 if (!proc_get_held_signals (pi, &pi->saved_sighold))
3699 dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
3701 if ((fail = procfs_debug_inferior (pi)) != 0)
3702 dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
3704 inf = add_inferior (pi->pid);
3705 /* Let GDB know that the inferior was attached. */
3706 inf->attach_flag = 1;
3708 /* Create a procinfo for the current lwp. */
3709 lwpid = proc_get_current_thread (pi);
3710 create_procinfo (pi->pid, lwpid);
3712 /* Add it to gdb's thread list. */
3713 ptid = MERGEPID (pi->pid, lwpid);
3720 do_detach (int signo)
3724 /* Find procinfo for the main process */
3725 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); /* FIXME: threads */
3727 if (!proc_set_current_signal (pi, signo))
3728 proc_warn (pi, "do_detach, set_current_signal", __LINE__);
3730 if (!proc_set_traced_signals (pi, &pi->saved_sigset))
3731 proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
3733 if (!proc_set_traced_faults (pi, &pi->saved_fltset))
3734 proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
3736 if (!proc_set_traced_sysentry (pi, pi->saved_entryset))
3737 proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
3739 if (!proc_set_traced_sysexit (pi, pi->saved_exitset))
3740 proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
3742 if (!proc_set_held_signals (pi, &pi->saved_sighold))
3743 proc_warn (pi, "do_detach, set_held_signals", __LINE__);
3745 if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)))
3746 if (signo || !(pi->was_stopped) ||
3747 query (_("Was stopped when attached, make it runnable again? ")))
3749 /* Clear any pending signal. */
3750 if (!proc_clear_current_fault (pi))
3751 proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
3753 if (signo == 0 && !proc_clear_current_signal (pi))
3754 proc_warn (pi, "do_detach, clear_current_signal", __LINE__);
3756 if (!proc_set_run_on_last_close (pi))
3757 proc_warn (pi, "do_detach, set_rlc", __LINE__);
3760 destroy_procinfo (pi);
3763 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
3766 ??? Is the following note still relevant? We can't get individual
3767 registers with the PT_GETREGS ptrace(2) request either, yet we
3768 don't bother with caching at all in that case.
3770 NOTE: Since the /proc interface cannot give us individual
3771 registers, we pay no attention to REGNUM, and just fetch them all.
3772 This results in the possibility that we will do unnecessarily many
3773 fetches, since we may be called repeatedly for individual
3774 registers. So we cache the results, and mark the cache invalid
3775 when the process is resumed. */
3778 procfs_fetch_registers (struct target_ops *ops,
3779 struct regcache *regcache, int regnum)
3781 gdb_gregset_t *gregs;
3783 int pid = PIDGET (inferior_ptid);
3784 int tid = TIDGET (inferior_ptid);
3785 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3787 pi = find_procinfo_or_die (pid, tid);
3790 error (_("procfs: fetch_registers failed to find procinfo for %s"),
3791 target_pid_to_str (inferior_ptid));
3793 gregs = proc_get_gregs (pi);
3795 proc_error (pi, "fetch_registers, get_gregs", __LINE__);
3797 supply_gregset (regcache, (const gdb_gregset_t *) gregs);
3799 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
3801 gdb_fpregset_t *fpregs;
3803 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
3804 || regnum == gdbarch_pc_regnum (gdbarch)
3805 || regnum == gdbarch_sp_regnum (gdbarch))
3806 return; /* Not a floating point register. */
3808 fpregs = proc_get_fpregs (pi);
3810 proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
3812 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregs);
3816 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
3817 this for all registers.
3819 NOTE: Since the /proc interface will not read individual registers,
3820 we will cache these requests until the process is resumed, and only
3821 then write them back to the inferior process.
3823 FIXME: is that a really bad idea? Have to think about cases where
3824 writing one register might affect the value of others, etc. */
3827 procfs_store_registers (struct target_ops *ops,
3828 struct regcache *regcache, int regnum)
3830 gdb_gregset_t *gregs;
3832 int pid = PIDGET (inferior_ptid);
3833 int tid = TIDGET (inferior_ptid);
3834 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3836 pi = find_procinfo_or_die (pid, tid);
3839 error (_("procfs: store_registers: failed to find procinfo for %s"),
3840 target_pid_to_str (inferior_ptid));
3842 gregs = proc_get_gregs (pi);
3844 proc_error (pi, "store_registers, get_gregs", __LINE__);
3846 fill_gregset (regcache, gregs, regnum);
3847 if (!proc_set_gregs (pi))
3848 proc_error (pi, "store_registers, set_gregs", __LINE__);
3850 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
3852 gdb_fpregset_t *fpregs;
3854 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
3855 || regnum == gdbarch_pc_regnum (gdbarch)
3856 || regnum == gdbarch_sp_regnum (gdbarch))
3857 return; /* Not a floating point register. */
3859 fpregs = proc_get_fpregs (pi);
3861 proc_error (pi, "store_registers, get_fpregs", __LINE__);
3863 fill_fpregset (regcache, fpregs, regnum);
3864 if (!proc_set_fpregs (pi))
3865 proc_error (pi, "store_registers, set_fpregs", __LINE__);
3870 syscall_is_lwp_exit (procinfo *pi, int scall)
3874 if (scall == SYS_lwp_exit)
3878 if (scall == SYS_lwpexit)
3885 syscall_is_exit (procinfo *pi, int scall)
3888 if (scall == SYS_exit)
3891 #ifdef DYNAMIC_SYSCALLS
3892 if (find_syscall (pi, "_exit") == scall)
3899 syscall_is_exec (procinfo *pi, int scall)
3902 if (scall == SYS_exec)
3906 if (scall == SYS_execv)
3910 if (scall == SYS_execve)
3913 #ifdef DYNAMIC_SYSCALLS
3914 if (find_syscall (pi, "_execve"))
3916 if (find_syscall (pi, "ra_execve"))
3923 syscall_is_lwp_create (procinfo *pi, int scall)
3925 #ifdef SYS_lwp_create
3926 if (scall == SYS_lwp_create)
3929 #ifdef SYS_lwpcreate
3930 if (scall == SYS_lwpcreate)
3937 * Function: target_wait
3939 * Retrieve the next stop event from the child process.
3940 * If child has not stopped yet, wait for it to stop.
3941 * Translate /proc eventcodes (or possibly wait eventcodes)
3942 * into gdb internal event codes.
3944 * Return: id of process (and possibly thread) that incurred the event.
3945 * event codes are returned thru a pointer parameter.
3949 procfs_wait (struct target_ops *ops,
3950 ptid_t ptid, struct target_waitstatus *status)
3952 /* First cut: loosely based on original version 2.1 */
3956 ptid_t retval, temp_ptid;
3957 int why, what, flags;
3964 retval = pid_to_ptid (-1);
3966 /* Find procinfo for main process */
3967 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
3970 /* We must assume that the status is stale now... */
3971 pi->status_valid = 0;
3972 pi->gregs_valid = 0;
3973 pi->fpregs_valid = 0;
3975 #if 0 /* just try this out... */
3976 flags = proc_flags (pi);
3977 why = proc_why (pi);
3978 if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
3979 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
3981 /* If child is not stopped, wait for it to stop. */
3982 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) &&
3983 !proc_wait_for_stop (pi))
3985 /* wait_for_stop failed: has the child terminated? */
3986 if (errno == ENOENT)
3990 /* /proc file not found; presumably child has terminated. */
3991 wait_retval = wait (&wstat); /* "wait" for the child's exit */
3993 if (wait_retval != PIDGET (inferior_ptid)) /* wrong child? */
3994 error (_("procfs: couldn't stop process %d: wait returned %d."),
3995 PIDGET (inferior_ptid), wait_retval);
3996 /* FIXME: might I not just use waitpid?
3997 Or try find_procinfo to see if I know about this child? */
3998 retval = pid_to_ptid (wait_retval);
4000 else if (errno == EINTR)
4004 /* Unknown error from wait_for_stop. */
4005 proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
4010 /* This long block is reached if either:
4011 a) the child was already stopped, or
4012 b) we successfully waited for the child with wait_for_stop.
4013 This block will analyze the /proc status, and translate it
4014 into a waitstatus for GDB.
4016 If we actually had to call wait because the /proc file
4017 is gone (child terminated), then we skip this block,
4018 because we already have a waitstatus. */
4020 flags = proc_flags (pi);
4021 why = proc_why (pi);
4022 what = proc_what (pi);
4024 if (flags & (PR_STOPPED | PR_ISTOP))
4027 /* If it's running async (for single_thread control),
4028 set it back to normal again. */
4029 if (flags & PR_ASYNC)
4030 if (!proc_unset_async (pi))
4031 proc_error (pi, "target_wait, unset_async", __LINE__);
4035 proc_prettyprint_why (why, what, 1);
4037 /* The 'pid' we will return to GDB is composed of
4038 the process ID plus the lwp ID. */
4039 retval = MERGEPID (pi->pid, proc_get_current_thread (pi));
4043 wstat = (what << 8) | 0177;
4046 if (syscall_is_lwp_exit (pi, what))
4048 if (print_thread_events)
4049 printf_unfiltered (_("[%s exited]\n"),
4050 target_pid_to_str (retval));
4051 delete_thread (retval);
4052 status->kind = TARGET_WAITKIND_SPURIOUS;
4055 else if (syscall_is_exit (pi, what))
4057 struct inferior *inf;
4059 /* Handle SYS_exit call only */
4060 /* Stopped at entry to SYS_exit.
4061 Make it runnable, resume it, then use
4062 the wait system call to get its exit code.
4063 Proc_run_process always clears the current
4065 Then return its exit status. */
4066 pi->status_valid = 0;
4068 /* FIXME: what we should do is return
4069 TARGET_WAITKIND_SPURIOUS. */
4070 if (!proc_run_process (pi, 0, 0))
4071 proc_error (pi, "target_wait, run_process", __LINE__);
4073 inf = find_inferior_pid (pi->pid);
4074 if (inf->attach_flag)
4076 /* Don't call wait: simulate waiting for exit,
4077 return a "success" exit code. Bogus: what if
4078 it returns something else? */
4080 retval = inferior_ptid; /* ? ? ? */
4084 int temp = wait (&wstat);
4086 /* FIXME: shouldn't I make sure I get the right
4087 event from the right process? If (for
4088 instance) I have killed an earlier inferior
4089 process but failed to clean up after it
4090 somehow, I could get its termination event
4093 /* If wait returns -1, that's what we return to GDB. */
4095 retval = pid_to_ptid (temp);
4100 printf_filtered (_("procfs: trapped on entry to "));
4101 proc_prettyprint_syscall (proc_what (pi), 0);
4102 printf_filtered ("\n");
4105 long i, nsysargs, *sysargs;
4107 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4108 (sysargs = proc_sysargs (pi)) != NULL)
4110 printf_filtered (_("%ld syscall arguments:\n"), nsysargs);
4111 for (i = 0; i < nsysargs; i++)
4112 printf_filtered ("#%ld: 0x%08lx\n",
4120 /* How to exit gracefully, returning "unknown event" */
4121 status->kind = TARGET_WAITKIND_SPURIOUS;
4122 return inferior_ptid;
4126 /* How to keep going without returning to wfi: */
4127 target_resume (ptid, 0, TARGET_SIGNAL_0);
4133 if (syscall_is_exec (pi, what))
4135 /* Hopefully this is our own "fork-child" execing
4136 the real child. Hoax this event into a trap, and
4137 GDB will see the child about to execute its start
4139 wstat = (SIGTRAP << 8) | 0177;
4142 else if (what == SYS_syssgi)
4144 /* see if we can break on dbx_link(). If yes, then
4145 we no longer need the SYS_syssgi notifications. */
4146 if (insert_dbx_link_breakpoint (pi))
4147 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT,
4150 /* This is an internal event and should be transparent
4151 to wfi, so resume the execution and wait again. See
4152 comment in procfs_init_inferior() for more details. */
4153 target_resume (ptid, 0, TARGET_SIGNAL_0);
4157 else if (syscall_is_lwp_create (pi, what))
4160 * This syscall is somewhat like fork/exec.
4161 * We will get the event twice: once for the parent LWP,
4162 * and once for the child. We should already know about
4163 * the parent LWP, but the child will be new to us. So,
4164 * whenever we get this event, if it represents a new
4165 * thread, simply add the thread to the list.
4168 /* If not in procinfo list, add it. */
4169 temp_tid = proc_get_current_thread (pi);
4170 if (!find_procinfo (pi->pid, temp_tid))
4171 create_procinfo (pi->pid, temp_tid);
4173 temp_ptid = MERGEPID (pi->pid, temp_tid);
4174 /* If not in GDB's thread list, add it. */
4175 if (!in_thread_list (temp_ptid))
4176 add_thread (temp_ptid);
4178 /* Return to WFI, but tell it to immediately resume. */
4179 status->kind = TARGET_WAITKIND_SPURIOUS;
4180 return inferior_ptid;
4182 else if (syscall_is_lwp_exit (pi, what))
4184 if (print_thread_events)
4185 printf_unfiltered (_("[%s exited]\n"),
4186 target_pid_to_str (retval));
4187 delete_thread (retval);
4188 status->kind = TARGET_WAITKIND_SPURIOUS;
4193 /* FIXME: Do we need to handle SYS_sproc,
4194 SYS_fork, or SYS_vfork here? The old procfs
4195 seemed to use this event to handle threads on
4196 older (non-LWP) systems, where I'm assuming
4197 that threads were actually separate processes.
4198 Irix, maybe? Anyway, low priority for now. */
4202 printf_filtered (_("procfs: trapped on exit from "));
4203 proc_prettyprint_syscall (proc_what (pi), 0);
4204 printf_filtered ("\n");
4207 long i, nsysargs, *sysargs;
4209 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4210 (sysargs = proc_sysargs (pi)) != NULL)
4212 printf_filtered (_("%ld syscall arguments:\n"), nsysargs);
4213 for (i = 0; i < nsysargs; i++)
4214 printf_filtered ("#%ld: 0x%08lx\n",
4219 status->kind = TARGET_WAITKIND_SPURIOUS;
4220 return inferior_ptid;
4225 wstat = (SIGSTOP << 8) | 0177;
4230 printf_filtered (_("Retry #%d:\n"), retry);
4231 pi->status_valid = 0;
4236 /* If not in procinfo list, add it. */
4237 temp_tid = proc_get_current_thread (pi);
4238 if (!find_procinfo (pi->pid, temp_tid))
4239 create_procinfo (pi->pid, temp_tid);
4241 /* If not in GDB's thread list, add it. */
4242 temp_ptid = MERGEPID (pi->pid, temp_tid);
4243 if (!in_thread_list (temp_ptid))
4244 add_thread (temp_ptid);
4246 status->kind = TARGET_WAITKIND_STOPPED;
4247 status->value.sig = 0;
4252 wstat = (what << 8) | 0177;
4258 wstat = (SIGTRAP << 8) | 0177;
4263 wstat = (SIGTRAP << 8) | 0177;
4266 /* FIXME: use si_signo where possible. */
4268 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4271 wstat = (SIGILL << 8) | 0177;
4274 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4277 /* If we hit our __dbx_link() internal breakpoint,
4278 then remove it. See comments in procfs_init_inferior()
4279 for more details. */
4280 if (dbx_link_bpt_addr != 0
4281 && dbx_link_bpt_addr == read_pc ())
4282 remove_dbx_link_breakpoint ();
4284 wstat = (SIGTRAP << 8) | 0177;
4288 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4291 wstat = (SIGSEGV << 8) | 0177;
4295 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4298 wstat = (SIGFPE << 8) | 0177;
4300 case FLTPAGE: /* Recoverable page fault */
4301 default: /* FIXME: use si_signo if possible for fault */
4302 retval = pid_to_ptid (-1);
4303 printf_filtered ("procfs:%d -- ", __LINE__);
4304 printf_filtered (_("child stopped for unknown reason:\n"));
4305 proc_prettyprint_why (why, what, 1);
4306 error (_("... giving up..."));
4309 break; /* case PR_FAULTED: */
4310 default: /* switch (why) unmatched */
4311 printf_filtered ("procfs:%d -- ", __LINE__);
4312 printf_filtered (_("child stopped for unknown reason:\n"));
4313 proc_prettyprint_why (why, what, 1);
4314 error (_("... giving up..."));
4318 * Got this far without error:
4319 * If retval isn't in the threads database, add it.
4321 if (PIDGET (retval) > 0 &&
4322 !ptid_equal (retval, inferior_ptid) &&
4323 !in_thread_list (retval))
4326 * We have a new thread.
4327 * We need to add it both to GDB's list and to our own.
4328 * If we don't create a procinfo, resume may be unhappy
4331 add_thread (retval);
4332 if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL)
4333 create_procinfo (PIDGET (retval), TIDGET (retval));
4336 else /* flags do not indicate STOPPED */
4338 /* surely this can't happen... */
4339 printf_filtered ("procfs:%d -- process not stopped.\n",
4341 proc_prettyprint_flags (flags, 1);
4342 error (_("procfs: ...giving up..."));
4347 store_waitstatus (status, wstat);
4353 /* Perform a partial transfer to/from the specified object. For
4354 memory transfers, fall back to the old memory xfer functions. */
4357 procfs_xfer_partial (struct target_ops *ops, enum target_object object,
4358 const char *annex, gdb_byte *readbuf,
4359 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4363 case TARGET_OBJECT_MEMORY:
4365 return (*ops->deprecated_xfer_memory) (offset, readbuf,
4366 len, 0/*read*/, NULL, ops);
4368 return (*ops->deprecated_xfer_memory) (offset, (gdb_byte *) writebuf,
4369 len, 1/*write*/, NULL, ops);
4373 case TARGET_OBJECT_AUXV:
4374 return procfs_xfer_auxv (ops, object, annex, readbuf, writebuf,
4379 if (ops->beneath != NULL)
4380 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
4381 readbuf, writebuf, offset, len);
4387 /* Transfer LEN bytes between GDB address MYADDR and target address
4388 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4389 otherwise transfer them from the target. TARGET is unused.
4391 The return value is 0 if an error occurred or no bytes were
4392 transferred. Otherwise, it will be a positive value which
4393 indicates the number of bytes transferred between gdb and the
4394 target. (Note that the interface also makes provisions for
4395 negative values, but this capability isn't implemented here.) */
4398 procfs_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int dowrite,
4399 struct mem_attrib *attrib, struct target_ops *target)
4404 /* Find procinfo for main process */
4405 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4406 if (pi->as_fd == 0 &&
4407 open_procinfo_files (pi, FD_AS) == 0)
4409 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
4413 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
4418 PROCFS_NOTE ("write memory: ");
4420 PROCFS_NOTE ("write memory: \n");
4422 nbytes = write (pi->as_fd, myaddr, len);
4426 PROCFS_NOTE ("read memory: \n");
4427 nbytes = read (pi->as_fd, myaddr, len);
4438 * Function: invalidate_cache
4440 * Called by target_resume before making child runnable.
4441 * Mark cached registers and status's invalid.
4442 * If there are "dirty" caches that need to be written back
4443 * to the child process, do that.
4445 * File descriptors are also cached.
4446 * As they are a limited resource, we cannot hold onto them indefinitely.
4447 * However, as they are expensive to open, we don't want to throw them
4448 * away indescriminately either. As a compromise, we will keep the
4449 * file descriptors for the parent process, but discard any file
4450 * descriptors we may have accumulated for the threads.
4453 * As this function is called by iterate_over_threads, it always
4454 * returns zero (so that iterate_over_threads will keep iterating).
4459 invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
4462 * About to run the child; invalidate caches and do any other cleanup.
4466 if (pi->gregs_dirty)
4467 if (parent == NULL ||
4468 proc_get_current_thread (parent) != pi->tid)
4469 if (!proc_set_gregs (pi)) /* flush gregs cache */
4470 proc_warn (pi, "target_resume, set_gregs",
4472 if (gdbarch_fp0_regnum (current_gdbarch) >= 0)
4473 if (pi->fpregs_dirty)
4474 if (parent == NULL ||
4475 proc_get_current_thread (parent) != pi->tid)
4476 if (!proc_set_fpregs (pi)) /* flush fpregs cache */
4477 proc_warn (pi, "target_resume, set_fpregs",
4483 /* The presence of a parent indicates that this is an LWP.
4484 Close any file descriptors that it might have open.
4485 We don't do this to the master (parent) procinfo. */
4487 close_procinfo_files (pi);
4489 pi->gregs_valid = 0;
4490 pi->fpregs_valid = 0;
4492 pi->gregs_dirty = 0;
4493 pi->fpregs_dirty = 0;
4495 pi->status_valid = 0;
4496 pi->threads_valid = 0;
4503 * Function: make_signal_thread_runnable
4505 * A callback function for iterate_over_threads.
4506 * Find the asynchronous signal thread, and make it runnable.
4507 * See if that helps matters any.
4511 make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr)
4514 if (proc_flags (pi) & PR_ASLWP)
4516 if (!proc_run_process (pi, 0, -1))
4517 proc_error (pi, "make_signal_thread_runnable", __LINE__);
4526 * Function: target_resume
4528 * Make the child process runnable. Normally we will then call
4529 * procfs_wait and wait for it to stop again (unles gdb is async).
4532 * step: if true, then arrange for the child to stop again
4533 * after executing a single instruction.
4534 * signo: if zero, then cancel any pending signal.
4535 * If non-zero, then arrange for the indicated signal
4536 * to be delivered to the child when it runs.
4537 * pid: if -1, then allow any child thread to run.
4538 * if non-zero, then allow only the indicated thread to run.
4539 ******* (not implemented yet)
4543 procfs_resume (struct target_ops *ops,
4544 ptid_t ptid, int step, enum target_signal signo)
4546 procinfo *pi, *thread;
4550 prrun.prflags |= PRSVADDR;
4551 prrun.pr_vaddr = $PC; set resume address
4552 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4553 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4554 prrun.prflags |= PRCFAULT; clear current fault.
4556 PRSTRACE and PRSFAULT can be done by other means
4557 (proc_trace_signals, proc_trace_faults)
4558 PRSVADDR is unnecessary.
4559 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4560 This basically leaves PRSTEP and PRCSIG.
4561 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4562 So basically PR_STEP is the sole argument that must be passed
4563 to proc_run_process (for use in the prrun struct by ioctl). */
4565 /* Find procinfo for main process */
4566 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4568 /* First cut: ignore pid argument */
4571 /* Convert signal to host numbering. */
4573 (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop))
4576 native_signo = target_signal_to_host (signo);
4578 pi->ignore_next_sigstop = 0;
4580 /* Running the process voids all cached registers and status. */
4581 /* Void the threads' caches first */
4582 proc_iterate_over_threads (pi, invalidate_cache, NULL);
4583 /* Void the process procinfo's caches. */
4584 invalidate_cache (NULL, pi, NULL);
4586 if (PIDGET (ptid) != -1)
4588 /* Resume a specific thread, presumably suppressing the others. */
4589 thread = find_procinfo (PIDGET (ptid), TIDGET (ptid));
4592 if (thread->tid != 0)
4594 /* We're to resume a specific thread, and not the others.
4595 * Set the child process's PR_ASYNC flag.
4598 if (!proc_set_async (pi))
4599 proc_error (pi, "target_resume, set_async", __LINE__);
4602 proc_iterate_over_threads (pi,
4603 make_signal_thread_runnable,
4606 pi = thread; /* substitute the thread's procinfo for run */
4611 if (!proc_run_process (pi, step, native_signo))
4614 warning (_("resume: target already running. Pretend to resume, and hope for the best!"));
4616 proc_error (pi, "target_resume", __LINE__);
4621 * Function: register_gdb_signals
4623 * Traverse the list of signals that GDB knows about
4624 * (see "handle" command), and arrange for the target
4625 * to be stopped or not, according to these settings.
4627 * Returns non-zero for success, zero for failure.
4631 register_gdb_signals (procinfo *pi, gdb_sigset_t *signals)
4635 for (signo = 0; signo < NSIG; signo ++)
4636 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
4637 signal_print_state (target_signal_from_host (signo)) == 0 &&
4638 signal_pass_state (target_signal_from_host (signo)) == 1)
4639 prdelset (signals, signo);
4641 praddset (signals, signo);
4643 return proc_set_traced_signals (pi, signals);
4647 * Function: target_notice_signals
4649 * Set up to trace signals in the child process.
4653 procfs_notice_signals (ptid_t ptid)
4655 gdb_sigset_t signals;
4656 procinfo *pi = find_procinfo_or_die (PIDGET (ptid), 0);
4658 if (proc_get_traced_signals (pi, &signals) &&
4659 register_gdb_signals (pi, &signals))
4662 proc_error (pi, "notice_signals", __LINE__);
4666 * Function: target_files_info
4668 * Print status information about the child process.
4672 procfs_files_info (struct target_ops *ignore)
4674 struct inferior *inf = current_inferior ();
4675 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
4676 inf->attach_flag? "attached": "child",
4677 target_pid_to_str (inferior_ptid));
4681 * Function: target_stop
4683 * Stop the child process asynchronously, as when the
4684 * gdb user types control-c or presses a "stop" button.
4686 * Works by sending kill(SIGINT) to the child's process group.
4690 procfs_stop (ptid_t ptid)
4692 kill (-inferior_process_group, SIGINT);
4696 * Function: unconditionally_kill_inferior
4698 * Make it die. Wait for it to die. Clean up after it.
4699 * Note: this should only be applied to the real process,
4700 * not to an LWP, because of the check for parent-process.
4701 * If we need this to work for an LWP, it needs some more logic.
4705 unconditionally_kill_inferior (procinfo *pi)
4709 parent_pid = proc_parent_pid (pi);
4710 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4711 /* FIXME: use access functions */
4712 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4713 before the PIOCKILL, otherwise it might generate a corrupted core
4714 file for the inferior. */
4715 if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0)
4717 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4720 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4721 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4722 to kill the inferior, otherwise it might remain stopped with a
4724 We do not check the result of the PIOCSSIG, the inferior might have
4727 gdb_siginfo_t newsiginfo;
4729 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
4730 newsiginfo.si_signo = SIGKILL;
4731 newsiginfo.si_code = 0;
4732 newsiginfo.si_errno = 0;
4733 newsiginfo.si_pid = getpid ();
4734 newsiginfo.si_uid = getuid ();
4735 /* FIXME: use proc_set_current_signal */
4736 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
4738 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4739 if (!proc_kill (pi, SIGKILL))
4740 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
4741 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4742 destroy_procinfo (pi);
4744 /* If pi is GDB's child, wait for it to die. */
4745 if (parent_pid == getpid ())
4746 /* FIXME: should we use waitpid to make sure we get the right event?
4747 Should we check the returned event? */
4752 ret = waitpid (pi->pid, &status, 0);
4760 * Function: target_kill_inferior
4762 * We're done debugging it, and we want it to go away.
4763 * Then we want GDB to forget all about it.
4767 procfs_kill_inferior (struct target_ops *ops)
4769 if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */
4771 /* Find procinfo for main process */
4772 procinfo *pi = find_procinfo (PIDGET (inferior_ptid), 0);
4775 unconditionally_kill_inferior (pi);
4776 target_mourn_inferior ();
4781 * Function: target_mourn_inferior
4783 * Forget we ever debugged this thing!
4787 procfs_mourn_inferior (struct target_ops *ops)
4791 if (!ptid_equal (inferior_ptid, null_ptid))
4793 /* Find procinfo for main process */
4794 pi = find_procinfo (PIDGET (inferior_ptid), 0);
4796 destroy_procinfo (pi);
4798 unpush_target (ops);
4800 if (dbx_link_bpt != NULL)
4802 deprecated_remove_raw_breakpoint (dbx_link_bpt);
4803 dbx_link_bpt_addr = 0;
4804 dbx_link_bpt = NULL;
4807 generic_mourn_inferior ();
4811 * Function: init_inferior
4813 * When GDB forks to create a runnable inferior process,
4814 * this function is called on the parent side of the fork.
4815 * It's job is to do whatever is necessary to make the child
4816 * ready to be debugged, and then wait for the child to synchronize.
4820 procfs_init_inferior (struct target_ops *ops, int pid)
4823 gdb_sigset_t signals;
4827 /* This routine called on the parent side (GDB side)
4828 after GDB forks the inferior. */
4831 if ((pi = create_procinfo (pid, 0)) == NULL)
4832 perror ("procfs: out of memory in 'init_inferior'");
4834 if (!open_procinfo_files (pi, FD_CTL))
4835 proc_error (pi, "init_inferior, open_proc_files", __LINE__);
4839 open_procinfo_files // done
4842 procfs_notice_signals
4849 /* If not stopped yet, wait for it to stop. */
4850 if (!(proc_flags (pi) & PR_STOPPED) &&
4851 !(proc_wait_for_stop (pi)))
4852 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
4854 /* Save some of the /proc state to be restored if we detach. */
4855 /* FIXME: Why? In case another debugger was debugging it?
4856 We're it's parent, for Ghu's sake! */
4857 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
4858 proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
4859 if (!proc_get_held_signals (pi, &pi->saved_sighold))
4860 proc_error (pi, "init_inferior, get_held_signals", __LINE__);
4861 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
4862 proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
4863 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
4864 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
4865 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
4866 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
4868 /* Register to trace selected signals in the child. */
4869 prfillset (&signals);
4870 if (!register_gdb_signals (pi, &signals))
4871 proc_error (pi, "init_inferior, register_signals", __LINE__);
4873 if ((fail = procfs_debug_inferior (pi)) != 0)
4874 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
4876 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4877 and possibly even turning ON kill-on-last-close at this point. But
4878 I can't make that change without careful testing which I don't have
4879 time to do right now... */
4880 /* Turn on run-on-last-close flag so that the child
4881 will die if GDB goes away for some reason. */
4882 if (!proc_set_run_on_last_close (pi))
4883 proc_error (pi, "init_inferior, set_RLC", __LINE__);
4885 /* We now have have access to the lwpid of the main thread/lwp. */
4886 lwpid = proc_get_current_thread (pi);
4888 /* Create a procinfo for the main lwp. */
4889 create_procinfo (pid, lwpid);
4891 /* We already have a main thread registered in the thread table at
4892 this point, but it didn't have any lwp info yet. Notify the core
4893 about it. This changes inferior_ptid as well. */
4894 thread_change_ptid (pid_to_ptid (pid),
4895 MERGEPID (pid, lwpid));
4897 /* Typically two, one trap to exec the shell, one to exec the
4898 program being debugged. Defined by "inferior.h". */
4899 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
4902 /* On mips-irix, we need to stop the inferior early enough during
4903 the startup phase in order to be able to load the shared library
4904 symbols and insert the breakpoints that are located in these shared
4905 libraries. Stopping at the program entry point is not good enough
4906 because the -init code is executed before the execution reaches
4909 So what we need to do is to insert a breakpoint in the runtime
4910 loader (rld), more precisely in __dbx_link(). This procedure is
4911 called by rld once all shared libraries have been mapped, but before
4912 the -init code is executed. Unfortuantely, this is not straightforward,
4913 as rld is not part of the executable we are running, and thus we need
4914 the inferior to run until rld itself has been mapped in memory.
4916 For this, we trace all syssgi() syscall exit events. Each time
4917 we detect such an event, we iterate over each text memory maps,
4918 get its associated fd, and scan the symbol table for __dbx_link().
4919 When found, we know that rld has been mapped, and that we can insert
4920 the breakpoint at the symbol address. Once the dbx_link() breakpoint
4921 has been inserted, the syssgi() notifications are no longer necessary,
4922 so they should be canceled. */
4923 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT, FLAG_SET, 0);
4928 * Function: set_exec_trap
4930 * When GDB forks to create a new process, this function is called
4931 * on the child side of the fork before GDB exec's the user program.
4932 * Its job is to make the child minimally debuggable, so that the
4933 * parent GDB process can connect to the child and take over.
4934 * This function should do only the minimum to make that possible,
4935 * and to synchronize with the parent process. The parent process
4936 * should take care of the details.
4940 procfs_set_exec_trap (void)
4942 /* This routine called on the child side (inferior side)
4943 after GDB forks the inferior. It must use only local variables,
4944 because it may be sharing data space with its parent. */
4949 if ((pi = create_procinfo (getpid (), 0)) == NULL)
4950 perror_with_name (_("procfs: create_procinfo failed in child."));
4952 if (open_procinfo_files (pi, FD_CTL) == 0)
4954 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
4955 gdb_flush (gdb_stderr);
4956 /* no need to call "dead_procinfo", because we're going to exit. */
4960 #ifdef PRFS_STOPEXEC /* defined on OSF */
4961 /* OSF method for tracing exec syscalls. Quoting:
4962 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4963 exits from exec system calls because of the user level loader. */
4964 /* FIXME: make nice and maybe move into an access function. */
4968 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
4970 proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__);
4971 gdb_flush (gdb_stderr);
4974 prfs_flags |= PRFS_STOPEXEC;
4976 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
4978 proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__);
4979 gdb_flush (gdb_stderr);
4983 #else /* not PRFS_STOPEXEC */
4984 /* Everyone else's (except OSF) method for tracing exec syscalls */
4986 Not all systems with /proc have all the exec* syscalls with the same
4987 names. On the SGI, for example, there is no SYS_exec, but there
4988 *is* a SYS_execv. So, we try to account for that. */
4990 exitset = sysset_t_alloc (pi);
4991 gdb_premptysysset (exitset);
4993 gdb_praddsysset (exitset, SYS_exec);
4996 gdb_praddsysset (exitset, SYS_execve);
4999 gdb_praddsysset (exitset, SYS_execv);
5001 #ifdef DYNAMIC_SYSCALLS
5003 int callnum = find_syscall (pi, "execve");
5006 gdb_praddsysset (exitset, callnum);
5008 callnum = find_syscall (pi, "ra_execve");
5010 gdb_praddsysset (exitset, callnum);
5012 #endif /* DYNAMIC_SYSCALLS */
5014 if (!proc_set_traced_sysexit (pi, exitset))
5016 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
5017 gdb_flush (gdb_stderr);
5020 #endif /* PRFS_STOPEXEC */
5022 /* FIXME: should this be done in the parent instead? */
5023 /* Turn off inherit on fork flag so that all grand-children
5024 of gdb start with tracing flags cleared. */
5025 if (!proc_unset_inherit_on_fork (pi))
5026 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
5028 /* Turn off run on last close flag, so that the child process
5029 cannot run away just because we close our handle on it.
5030 We want it to wait for the parent to attach. */
5031 if (!proc_unset_run_on_last_close (pi))
5032 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
5034 /* FIXME: No need to destroy the procinfo --
5035 we have our own address space, and we're about to do an exec! */
5036 /*destroy_procinfo (pi);*/
5040 * Function: create_inferior
5042 * This function is called BEFORE gdb forks the inferior process.
5043 * Its only real responsibility is to set things up for the fork,
5044 * and tell GDB which two functions to call after the fork (one
5045 * for the parent, and one for the child).
5047 * This function does a complicated search for a unix shell program,
5048 * which it then uses to parse arguments and environment variables
5049 * to be sent to the child. I wonder whether this code could not
5050 * be abstracted out and shared with other unix targets such as
5055 procfs_create_inferior (struct target_ops *ops, char *exec_file,
5056 char *allargs, char **env, int from_tty)
5058 char *shell_file = getenv ("SHELL");
5062 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
5065 /* We will be looking down the PATH to find shell_file. If we
5066 just do this the normal way (via execlp, which operates by
5067 attempting an exec for each element of the PATH until it
5068 finds one which succeeds), then there will be an exec for
5069 each failed attempt, each of which will cause a PR_SYSEXIT
5070 stop, and we won't know how to distinguish the PR_SYSEXIT's
5071 for these failed execs with the ones for successful execs
5072 (whether the exec has succeeded is stored at that time in the
5073 carry bit or some such architecture-specific and
5074 non-ABI-specified place).
5076 So I can't think of anything better than to search the PATH
5077 now. This has several disadvantages: (1) There is a race
5078 condition; if we find a file now and it is deleted before we
5079 exec it, we lose, even if the deletion leaves a valid file
5080 further down in the PATH, (2) there is no way to know exactly
5081 what an executable (in the sense of "capable of being
5082 exec'd") file is. Using access() loses because it may lose
5083 if the caller is the superuser; failing to use it loses if
5084 there are ACLs or some such. */
5088 /* FIXME-maybe: might want "set path" command so user can change what
5089 path is used from within GDB. */
5090 char *path = getenv ("PATH");
5092 struct stat statbuf;
5095 path = "/bin:/usr/bin";
5097 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
5098 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
5100 p1 = strchr (p, ':');
5105 strncpy (tryname, p, len);
5106 tryname[len] = '\0';
5107 strcat (tryname, "/");
5108 strcat (tryname, shell_file);
5109 if (access (tryname, X_OK) < 0)
5111 if (stat (tryname, &statbuf) < 0)
5113 if (!S_ISREG (statbuf.st_mode))
5114 /* We certainly need to reject directories. I'm not quite
5115 as sure about FIFOs, sockets, etc., but I kind of doubt
5116 that people want to exec() these things. */
5121 /* Not found. This must be an error rather than merely passing
5122 the file to execlp(), because execlp() would try all the
5123 exec()s, causing GDB to get confused. */
5124 error (_("procfs:%d -- Can't find shell %s in PATH"),
5125 __LINE__, shell_file);
5127 shell_file = tryname;
5130 pid = fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
5131 NULL, NULL, shell_file);
5133 procfs_init_inferior (ops, pid);
5136 /* Make sure to cancel the syssgi() syscall-exit notifications.
5137 They should normally have been removed by now, but they may still
5138 be activated if the inferior doesn't use shared libraries, or if
5139 we didn't locate __dbx_link, or if we never stopped in __dbx_link.
5140 See procfs_init_inferior() for more details. */
5141 proc_trace_syscalls_1 (find_procinfo_or_die (PIDGET (inferior_ptid), 0),
5142 SYS_syssgi, PR_SYSEXIT, FLAG_RESET, 0);
5147 * Function: notice_thread
5149 * Callback for find_new_threads.
5150 * Calls "add_thread".
5154 procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
5156 ptid_t gdb_threadid = MERGEPID (pi->pid, thread->tid);
5158 if (!in_thread_list (gdb_threadid) || is_exited (gdb_threadid))
5159 add_thread (gdb_threadid);
5165 * Function: target_find_new_threads
5167 * Query all the threads that the target knows about,
5168 * and give them back to GDB to add to its list.
5172 procfs_find_new_threads (struct target_ops *ops)
5176 /* Find procinfo for main process */
5177 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5178 proc_update_threads (pi);
5179 proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
5183 * Function: target_thread_alive
5185 * Return true if the thread is still 'alive'.
5187 * This guy doesn't really seem to be doing his job.
5188 * Got to investigate how to tell when a thread is really gone.
5192 procfs_thread_alive (struct target_ops *ops, ptid_t ptid)
5197 proc = PIDGET (ptid);
5198 thread = TIDGET (ptid);
5199 /* If I don't know it, it ain't alive! */
5200 if ((pi = find_procinfo (proc, thread)) == NULL)
5203 /* If I can't get its status, it ain't alive!
5204 What's more, I need to forget about it! */
5205 if (!proc_get_status (pi))
5207 destroy_procinfo (pi);
5210 /* I couldn't have got its status if it weren't alive, so it's alive. */
5214 /* Convert PTID to a string. Returns the string in a static buffer. */
5217 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
5219 static char buf[80];
5221 if (TIDGET (ptid) == 0)
5222 sprintf (buf, "process %d", PIDGET (ptid));
5224 sprintf (buf, "LWP %ld", TIDGET (ptid));
5230 * Function: procfs_set_watchpoint
5231 * Insert a watchpoint
5235 procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag,
5243 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5244 PIDGET (inferior_ptid) : PIDGET (ptid), 0);
5246 /* Translate from GDB's flags to /proc's */
5247 if (len > 0) /* len == 0 means delete watchpoint */
5249 switch (rwflag) { /* FIXME: need an enum! */
5250 case hw_write: /* default watchpoint (write) */
5251 pflags = WRITE_WATCHFLAG;
5253 case hw_read: /* read watchpoint */
5254 pflags = READ_WATCHFLAG;
5256 case hw_access: /* access watchpoint */
5257 pflags = READ_WATCHFLAG | WRITE_WATCHFLAG;
5259 case hw_execute: /* execution HW breakpoint */
5260 pflags = EXEC_WATCHFLAG;
5262 default: /* Something weird. Return error. */
5265 if (after) /* Stop after r/w access is completed. */
5266 pflags |= AFTER_WATCHFLAG;
5269 if (!proc_set_watchpoint (pi, addr, len, pflags))
5271 if (errno == E2BIG) /* Typical error for no resources */
5272 return -1; /* fail */
5273 /* GDB may try to remove the same watchpoint twice.
5274 If a remove request returns no match, don't error. */
5275 if (errno == ESRCH && len == 0)
5276 return 0; /* ignore */
5277 proc_error (pi, "set_watchpoint", __LINE__);
5280 #endif /* UNIXWARE */
5284 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
5285 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
5286 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
5289 Note: procfs_can_use_hw_breakpoint() is not yet used by all
5290 procfs.c targets due to the fact that some of them still define
5291 TARGET_CAN_USE_HARDWARE_WATCHPOINT. */
5294 procfs_can_use_hw_breakpoint (int type, int cnt, int othertype)
5296 #ifndef TARGET_HAS_HARDWARE_WATCHPOINTS
5299 /* Due to the way that proc_set_watchpoint() is implemented, host
5300 and target pointers must be of the same size. If they are not,
5301 we can't use hardware watchpoints. This limitation is due to the
5302 fact that proc_set_watchpoint() calls
5303 procfs_address_to_host_pointer(); a close inspection of
5304 procfs_address_to_host_pointer will reveal that an internal error
5305 will be generated when the host and target pointer sizes are
5307 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
5308 if (sizeof (void *) != TYPE_LENGTH (ptr_type))
5311 /* Other tests here??? */
5318 * Function: stopped_by_watchpoint
5320 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5321 * else returns zero.
5325 procfs_stopped_by_watchpoint (ptid_t ptid)
5329 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5330 PIDGET (inferior_ptid) : PIDGET (ptid), 0);
5332 if (!pi) /* If no process, then not stopped by watchpoint! */
5335 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
5337 if (proc_why (pi) == PR_FAULTED)
5340 if (proc_what (pi) == FLTWATCH)
5344 if (proc_what (pi) == FLTKWATCH)
5353 * Memory Mappings Functions:
5357 * Function: iterate_over_mappings
5359 * Call a callback function once for each mapping, passing it the mapping,
5360 * an optional secondary callback function, and some optional opaque data.
5361 * Quit and return the first non-zero value returned from the callback.
5364 * pi -- procinfo struct for the process to be mapped.
5365 * func -- callback function to be called by this iterator.
5366 * data -- optional opaque data to be passed to the callback function.
5367 * child_func -- optional secondary function pointer to be passed
5368 * to the child function.
5370 * Return: First non-zero return value from the callback function,
5375 iterate_over_mappings (procinfo *pi, int (*child_func) (), void *data,
5376 int (*func) (struct prmap *map,
5377 int (*child_func) (),
5380 char pathname[MAX_PROC_NAME_SIZE];
5381 struct prmap *prmaps;
5382 struct prmap *prmap;
5390 /* Get the number of mappings, allocate space,
5391 and read the mappings into prmaps. */
5394 sprintf (pathname, "/proc/%d/map", pi->pid);
5395 if ((map_fd = open (pathname, O_RDONLY)) < 0)
5396 proc_error (pi, "iterate_over_mappings (open)", __LINE__);
5398 /* Make sure it gets closed again. */
5399 make_cleanup_close (map_fd);
5401 /* Use stat to determine the file size, and compute
5402 the number of prmap_t objects it contains. */
5403 if (fstat (map_fd, &sbuf) != 0)
5404 proc_error (pi, "iterate_over_mappings (fstat)", __LINE__);
5406 nmap = sbuf.st_size / sizeof (prmap_t);
5407 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5408 if (read (map_fd, (char *) prmaps, nmap * sizeof (*prmaps))
5409 != (nmap * sizeof (*prmaps)))
5410 proc_error (pi, "iterate_over_mappings (read)", __LINE__);
5412 /* Use ioctl command PIOCNMAP to get number of mappings. */
5413 if (ioctl (pi->ctl_fd, PIOCNMAP, &nmap) != 0)
5414 proc_error (pi, "iterate_over_mappings (PIOCNMAP)", __LINE__);
5416 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5417 if (ioctl (pi->ctl_fd, PIOCMAP, prmaps) != 0)
5418 proc_error (pi, "iterate_over_mappings (PIOCMAP)", __LINE__);
5421 for (prmap = prmaps; nmap > 0; prmap++, nmap--)
5422 if ((funcstat = (*func) (prmap, child_func, data)) != 0)
5429 * Function: solib_mappings_callback
5431 * Calls the supplied callback function once for each mapped address
5432 * space in the process. The callback function receives an open
5433 * file descriptor for the file corresponding to that mapped
5434 * address space (if there is one), and the base address of the
5435 * mapped space. Quit when the callback function returns a
5436 * nonzero value, or at teh end of the mappings.
5438 * Returns: the first non-zero return value of the callback function,
5442 int solib_mappings_callback (struct prmap *map,
5443 int (*func) (int, CORE_ADDR),
5446 procinfo *pi = data;
5450 char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)];
5452 if (map->pr_vaddr == 0 && map->pr_size == 0)
5453 return -1; /* sanity */
5455 if (map->pr_mapname[0] == 0)
5457 fd = -1; /* no map file */
5461 sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname);
5462 /* Note: caller's responsibility to close this fd! */
5463 fd = open_with_retry (name, O_RDONLY);
5464 /* Note: we don't test the above call for failure;
5465 we just pass the FD on as given. Sometimes there is
5466 no file, so the open may return failure, but that's
5470 fd = ioctl (pi->ctl_fd, PIOCOPENM, &map->pr_vaddr);
5471 /* Note: we don't test the above call for failure;
5472 we just pass the FD on as given. Sometimes there is
5473 no file, so the ioctl may return failure, but that's
5476 return (*func) (fd, (CORE_ADDR) map->pr_vaddr);
5480 * Function: proc_iterate_over_mappings
5482 * Uses the unified "iterate_over_mappings" function
5483 * to implement the exported interface to solib-svr4.c.
5485 * Given a pointer to a function, call that function once for every
5486 * mapped address space in the process. The callback function
5487 * receives an open file descriptor for the file corresponding to
5488 * that mapped address space (if there is one), and the base address
5489 * of the mapped space. Quit when the callback function returns a
5490 * nonzero value, or at teh end of the mappings.
5492 * Returns: the first non-zero return value of the callback function,
5497 proc_iterate_over_mappings (int (*func) (int, CORE_ADDR))
5499 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5501 return iterate_over_mappings (pi, func, pi, solib_mappings_callback);
5505 * Function: find_memory_regions_callback
5507 * Implements the to_find_memory_regions method.
5508 * Calls an external function for each memory region.
5509 * External function will have the signiture:
5511 * int callback (CORE_ADDR vaddr,
5512 * unsigned long size,
5513 * int read, int write, int execute,
5516 * Returns the integer value returned by the callback.
5520 find_memory_regions_callback (struct prmap *map,
5521 int (*func) (CORE_ADDR,
5527 return (*func) ((CORE_ADDR) map->pr_vaddr,
5529 (map->pr_mflags & MA_READ) != 0,
5530 (map->pr_mflags & MA_WRITE) != 0,
5531 (map->pr_mflags & MA_EXEC) != 0,
5536 * Function: proc_find_memory_regions
5538 * External interface. Calls a callback function once for each
5539 * mapped memory region in the child process, passing as arguments
5540 * CORE_ADDR virtual_address,
5541 * unsigned long size,
5542 * int read, TRUE if region is readable by the child
5543 * int write, TRUE if region is writable by the child
5544 * int execute TRUE if region is executable by the child.
5546 * Stops iterating and returns the first non-zero value
5547 * returned by the callback.
5551 proc_find_memory_regions (int (*func) (CORE_ADDR,
5557 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5559 return iterate_over_mappings (pi, func, data,
5560 find_memory_regions_callback);
5563 /* Remove the breakpoint that we inserted in __dbx_link().
5564 Does nothing if the breakpoint hasn't been inserted or has already
5568 remove_dbx_link_breakpoint (void)
5570 if (dbx_link_bpt_addr == 0)
5573 if (deprecated_remove_raw_breakpoint (dbx_link_bpt) != 0)
5574 warning (_("Unable to remove __dbx_link breakpoint."));
5576 dbx_link_bpt_addr = 0;
5577 dbx_link_bpt = NULL;
5580 /* Return the address of the __dbx_link() function in the file
5581 refernced by ABFD by scanning its symbol table. Return 0 if
5582 the symbol was not found. */
5585 dbx_link_addr (bfd *abfd)
5587 long storage_needed;
5588 asymbol **symbol_table;
5589 long number_of_symbols;
5592 storage_needed = bfd_get_symtab_upper_bound (abfd);
5593 if (storage_needed <= 0)
5596 symbol_table = (asymbol **) xmalloc (storage_needed);
5597 make_cleanup (xfree, symbol_table);
5599 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
5601 for (i = 0; i < number_of_symbols; i++)
5603 asymbol *sym = symbol_table[i];
5605 if ((sym->flags & BSF_GLOBAL)
5606 && sym->name != NULL && strcmp (sym->name, "__dbx_link") == 0)
5607 return (sym->value + sym->section->vma);
5610 /* Symbol not found, return NULL. */
5614 /* Search the symbol table of the file referenced by FD for a symbol
5615 named __dbx_link(). If found, then insert a breakpoint at this location,
5616 and return nonzero. Return zero otherwise. */
5619 insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
5622 long storage_needed;
5625 abfd = bfd_fdopenr ("unamed", 0, fd);
5628 warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
5632 if (!bfd_check_format (abfd, bfd_object))
5634 /* Not the correct format, so we can not possibly find the dbx_link
5640 sym_addr = dbx_link_addr (abfd);
5643 /* Insert the breakpoint. */
5644 dbx_link_bpt_addr = sym_addr;
5645 dbx_link_bpt = deprecated_insert_raw_breakpoint (sym_addr);
5646 if (dbx_link_bpt == NULL)
5648 warning (_("Failed to insert dbx_link breakpoint."));
5660 /* If the given memory region MAP contains a symbol named __dbx_link,
5661 insert a breakpoint at this location and return nonzero. Return
5665 insert_dbx_link_bpt_in_region (struct prmap *map,
5666 int (*child_func) (),
5669 procinfo *pi = (procinfo *) data;
5671 /* We know the symbol we're looking for is in a text region, so
5672 only look for it if the region is a text one. */
5673 if (map->pr_mflags & MA_EXEC)
5674 return solib_mappings_callback (map, insert_dbx_link_bpt_in_file, pi);
5679 /* Search all memory regions for a symbol named __dbx_link. If found,
5680 insert a breakpoint at its location, and return nonzero. Return zero
5684 insert_dbx_link_breakpoint (procinfo *pi)
5686 return iterate_over_mappings (pi, NULL, pi, insert_dbx_link_bpt_in_region);
5690 * Function: mappingflags
5692 * Returns an ascii representation of a memory mapping's flags.
5696 mappingflags (long flags)
5698 static char asciiflags[8];
5700 strcpy (asciiflags, "-------");
5701 #if defined (MA_PHYS)
5702 if (flags & MA_PHYS)
5703 asciiflags[0] = 'd';
5705 if (flags & MA_STACK)
5706 asciiflags[1] = 's';
5707 if (flags & MA_BREAK)
5708 asciiflags[2] = 'b';
5709 if (flags & MA_SHARED)
5710 asciiflags[3] = 's';
5711 if (flags & MA_READ)
5712 asciiflags[4] = 'r';
5713 if (flags & MA_WRITE)
5714 asciiflags[5] = 'w';
5715 if (flags & MA_EXEC)
5716 asciiflags[6] = 'x';
5717 return (asciiflags);
5721 * Function: info_mappings_callback
5723 * Callback function, does the actual work for 'info proc mappings'.
5727 info_mappings_callback (struct prmap *map, int (*ignore) (), void *unused)
5729 unsigned int pr_off;
5731 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5732 pr_off = (unsigned int) map->pr_offset;
5734 pr_off = map->pr_off;
5737 if (gdbarch_addr_bit (current_gdbarch) == 32)
5738 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
5739 (unsigned long) map->pr_vaddr,
5740 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5741 (unsigned long) map->pr_size,
5743 mappingflags (map->pr_mflags));
5745 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
5746 (unsigned long) map->pr_vaddr,
5747 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5748 (unsigned long) map->pr_size,
5750 mappingflags (map->pr_mflags));
5756 * Function: info_proc_mappings
5758 * Implement the "info proc mappings" subcommand.
5762 info_proc_mappings (procinfo *pi, int summary)
5765 return; /* No output for summary mode. */
5767 printf_filtered (_("Mapped address spaces:\n\n"));
5768 if (gdbarch_ptr_bit (current_gdbarch) == 32)
5769 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
5776 printf_filtered (" %18s %18s %10s %10s %7s\n",
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 argv = gdb_buildargv (args);
5809 make_cleanup_freeargv (argv);
5811 while (argv != NULL && *argv != NULL)
5813 if (isdigit (argv[0][0]))
5815 pid = strtoul (argv[0], &tmp, 10);
5817 tid = strtoul (++tmp, NULL, 10);
5819 else if (argv[0][0] == '/')
5821 tid = strtoul (argv[0] + 1, NULL, 10);
5823 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
5834 pid = PIDGET (inferior_ptid);
5836 error (_("No current process: you must name one."));
5839 /* Have pid, will travel.
5840 First see if it's a process we're already debugging. */
5841 process = find_procinfo (pid, 0);
5842 if (process == NULL)
5844 /* No. So open a procinfo for it, but
5845 remember to close it again when finished. */
5846 process = create_procinfo (pid, 0);
5847 make_cleanup (do_destroy_procinfo_cleanup, process);
5848 if (!open_procinfo_files (process, FD_CTL))
5849 proc_error (process, "info proc, open_procinfo_files", __LINE__);
5853 thread = create_procinfo (pid, tid);
5857 printf_filtered (_("process %d flags:\n"), process->pid);
5858 proc_prettyprint_flags (proc_flags (process), 1);
5859 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
5860 proc_prettyprint_why (proc_why (process), proc_what (process), 1);
5861 if (proc_get_nthreads (process) > 1)
5862 printf_filtered ("Process has %d threads.\n",
5863 proc_get_nthreads (process));
5867 printf_filtered (_("thread %d flags:\n"), thread->tid);
5868 proc_prettyprint_flags (proc_flags (thread), 1);
5869 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
5870 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
5875 info_proc_mappings (process, 0);
5878 do_cleanups (old_chain);
5881 /* Modify the status of the system call identified by SYSCALLNUM in
5882 the set of syscalls that are currently traced/debugged.
5884 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
5885 will be updated. Otherwise, the exit syscalls set will be updated.
5887 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
5888 will be disabled. */
5891 proc_trace_syscalls_1 (procinfo *pi, int syscallnum, int entry_or_exit,
5892 int mode, int from_tty)
5896 if (entry_or_exit == PR_SYSENTRY)
5897 sysset = proc_get_traced_sysentry (pi, NULL);
5899 sysset = proc_get_traced_sysexit (pi, NULL);
5902 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
5904 if (mode == FLAG_SET)
5905 gdb_praddsysset (sysset, syscallnum);
5907 gdb_prdelsysset (sysset, syscallnum);
5909 if (entry_or_exit == PR_SYSENTRY)
5911 if (!proc_set_traced_sysentry (pi, sysset))
5912 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
5916 if (!proc_set_traced_sysexit (pi, sysset))
5917 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
5922 proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode)
5926 if (PIDGET (inferior_ptid) <= 0)
5927 error (_("you must be debugging a process to use this command."));
5929 if (args == NULL || args[0] == 0)
5930 error_no_arg (_("system call to trace"));
5932 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5933 if (isdigit (args[0]))
5935 const int syscallnum = atoi (args);
5937 proc_trace_syscalls_1 (pi, syscallnum, entry_or_exit, mode, from_tty);
5942 proc_trace_sysentry_cmd (char *args, int from_tty)
5944 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
5948 proc_trace_sysexit_cmd (char *args, int from_tty)
5950 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
5954 proc_untrace_sysentry_cmd (char *args, int from_tty)
5956 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
5960 proc_untrace_sysexit_cmd (char *args, int from_tty)
5962 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
5967 _initialize_procfs (void)
5969 struct target_ops * t;
5971 t = procfs_target ();
5975 add_info ("proc", info_proc_cmd, _("\
5976 Show /proc process information about any running process.\n\
5977 Specify process id, or use the program being debugged by default.\n\
5978 Specify keyword 'mappings' for detailed info on memory mappings."));
5979 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
5980 _("Give a trace of entries into the syscall."));
5981 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
5982 _("Give a trace of exits from the syscall."));
5983 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
5984 _("Cancel a trace of entries into the syscall."));
5985 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
5986 _("Cancel a trace of exits from the syscall."));
5989 /* =================== END, GDB "MODULE" =================== */
5993 /* miscellaneous stubs: */
5994 /* The following satisfy a few random symbols mostly created by */
5995 /* the solaris threads implementation, which I will chase down */
5999 * Return a pid for which we guarantee
6000 * we will be able to find a 'live' procinfo.
6004 procfs_first_available (void)
6006 return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1);
6010 find_signalled_thread (struct thread_info *info, void *data)
6012 if (info->stop_signal != TARGET_SIGNAL_0
6013 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
6019 static enum target_signal
6020 find_stop_signal (void)
6022 struct thread_info *info =
6023 iterate_over_threads (find_signalled_thread, NULL);
6026 return info->stop_signal;
6028 return TARGET_SIGNAL_0;
6031 /* =================== GCORE .NOTE "MODULE" =================== */
6032 #if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT)
6033 /* gcore only implemented on solaris and unixware (so far) */
6036 procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
6037 char *note_data, int *note_size,
6038 enum target_signal stop_signal)
6040 struct regcache *regcache = get_thread_regcache (ptid);
6041 gdb_gregset_t gregs;
6042 gdb_fpregset_t fpregs;
6043 unsigned long merged_pid;
6045 merged_pid = TIDGET (ptid) << 16 | PIDGET (ptid);
6047 fill_gregset (regcache, &gregs, -1);
6048 #if defined (UNIXWARE)
6049 note_data = (char *) elfcore_write_lwpstatus (obfd,
6056 note_data = (char *) elfcore_write_prstatus (obfd,
6063 fill_fpregset (regcache, &fpregs, -1);
6064 note_data = (char *) elfcore_write_prfpreg (obfd,
6072 struct procfs_corefile_thread_data {
6076 enum target_signal stop_signal;
6080 procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data)
6082 struct procfs_corefile_thread_data *args = data;
6086 ptid_t saved_ptid = inferior_ptid;
6087 inferior_ptid = MERGEPID (pi->pid, thread->tid);
6088 args->note_data = procfs_do_thread_registers (args->obfd, inferior_ptid,
6092 inferior_ptid = saved_ptid;
6098 procfs_make_note_section (bfd *obfd, int *note_size)
6100 struct cleanup *old_chain;
6101 gdb_gregset_t gregs;
6102 gdb_fpregset_t fpregs;
6103 char fname[16] = {'\0'};
6104 char psargs[80] = {'\0'};
6105 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
6106 char *note_data = NULL;
6108 struct procfs_corefile_thread_data thread_args;
6112 if (get_exec_file (0))
6114 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
6115 strncpy (psargs, get_exec_file (0),
6118 inf_args = get_inferior_args ();
6119 if (inf_args && *inf_args &&
6120 strlen (inf_args) < ((int) sizeof (psargs) - (int) strlen (psargs)))
6122 strncat (psargs, " ",
6123 sizeof (psargs) - strlen (psargs));
6124 strncat (psargs, inf_args,
6125 sizeof (psargs) - strlen (psargs));
6129 note_data = (char *) elfcore_write_prpsinfo (obfd,
6136 fill_gregset (get_current_regcache (), &gregs, -1);
6137 note_data = elfcore_write_pstatus (obfd, note_data, note_size,
6138 PIDGET (inferior_ptid),
6139 stop_signal, &gregs);
6142 thread_args.obfd = obfd;
6143 thread_args.note_data = note_data;
6144 thread_args.note_size = note_size;
6145 thread_args.stop_signal = find_stop_signal ();
6146 proc_iterate_over_threads (pi, procfs_corefile_thread_callback, &thread_args);
6148 /* There should be always at least one thread. */
6149 gdb_assert (thread_args.note_data != note_data);
6150 note_data = thread_args.note_data;
6152 auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV,
6156 note_data = elfcore_write_note (obfd, note_data, note_size,
6157 "CORE", NT_AUXV, auxv, auxv_len);
6161 make_cleanup (xfree, note_data);
6164 #else /* !(Solaris or Unixware) */
6166 procfs_make_note_section (bfd *obfd, int *note_size)
6168 error (_("gcore not implemented for this host."));
6169 return NULL; /* lint */
6171 #endif /* Solaris or Unixware */
6172 /* =================== END GCORE .NOTE "MODULE" =================== */