1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2006, 2007, 2008, 2009, 2010,
4 2011 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"
56 /* This module provides the interface between GDB and the
57 /proc file system, which is used on many versions of Unix
58 as a means for debuggers to control other processes.
60 Examples of the systems that use this interface are:
68 /proc works by imitating a file system: you open a simulated file
69 that represents the process you wish to interact with, and perform
70 operations on that "file" in order to examine or change the state
73 The most important thing to know about /proc and this module is
74 that there are two very different interfaces to /proc:
76 One that uses the ioctl system call, and another that uses read
77 and write system calls.
79 This module has to support both /proc interfaces. This means that
80 there are two different ways of doing every basic operation.
82 In order to keep most of the code simple and clean, I have defined
83 an interface "layer" which hides all these system calls. An ifdef
84 (NEW_PROC_API) determines which interface we are using, and most or
85 all occurrances of this ifdef should be confined to this interface
88 /* Determine which /proc API we are using: The ioctl API defines
89 PIOCSTATUS, while 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" =================== */
111 /* This module defines the GDB target vector and its methods. */
113 static void procfs_attach (struct target_ops *, char *, int);
114 static void procfs_detach (struct target_ops *, char *, int);
115 static void procfs_resume (struct target_ops *,
116 ptid_t, int, enum target_signal);
117 static void procfs_stop (ptid_t);
118 static void procfs_files_info (struct target_ops *);
119 static void procfs_fetch_registers (struct target_ops *,
120 struct regcache *, int);
121 static void procfs_store_registers (struct target_ops *,
122 struct regcache *, int);
123 static void procfs_notice_signals (ptid_t);
124 static void procfs_kill_inferior (struct target_ops *ops);
125 static void procfs_mourn_inferior (struct target_ops *ops);
126 static void procfs_create_inferior (struct target_ops *, char *,
127 char *, char **, int);
128 static ptid_t procfs_wait (struct target_ops *,
129 ptid_t, struct target_waitstatus *, int);
130 static int procfs_xfer_memory (CORE_ADDR, gdb_byte *, int, int,
131 struct mem_attrib *attrib,
132 struct target_ops *);
133 static LONGEST procfs_xfer_partial (struct target_ops *ops,
134 enum target_object object,
137 const gdb_byte *writebuf,
138 ULONGEST offset, LONGEST len);
140 static int procfs_thread_alive (struct target_ops *ops, ptid_t);
142 void procfs_find_new_threads (struct target_ops *ops);
143 char *procfs_pid_to_str (struct target_ops *, ptid_t);
145 static int proc_find_memory_regions (int (*) (CORE_ADDR,
151 static char * procfs_make_note_section (bfd *, int *);
153 static int procfs_can_use_hw_breakpoint (int, int, int);
155 #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
156 /* When GDB is built as 64-bit application on Solaris, the auxv data
157 is presented in 64-bit format. We need to provide a custom parser
160 procfs_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
161 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
163 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
164 gdb_byte *ptr = *readptr;
169 if (endptr - ptr < 8 * 2)
172 *typep = extract_unsigned_integer (ptr, 4, byte_order);
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, byte_order);
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;
216 #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
217 t->to_auxv_parse = procfs_auxv_parse;
220 t->to_magic = OPS_MAGIC;
225 /* =================== END, TARGET_OPS "MODULE" =================== */
227 /* World Unification:
229 Put any typedefs, defines etc. here that are required for the
230 unification of code that handles different versions of /proc. */
232 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
234 enum { READ_WATCHFLAG = WA_READ,
235 WRITE_WATCHFLAG = WA_WRITE,
236 EXEC_WATCHFLAG = WA_EXEC,
237 AFTER_WATCHFLAG = WA_TRAPAFTER
240 #else /* Irix method for watchpoints */
241 enum { READ_WATCHFLAG = MA_READ,
242 WRITE_WATCHFLAG = MA_WRITE,
243 EXEC_WATCHFLAG = MA_EXEC,
244 AFTER_WATCHFLAG = 0 /* trapafter not implemented */
249 #ifdef HAVE_PR_SIGSET_T
250 typedef pr_sigset_t gdb_sigset_t;
252 typedef sigset_t gdb_sigset_t;
256 #ifdef HAVE_PR_SIGACTION64_T
257 typedef pr_sigaction64_t gdb_sigaction_t;
259 typedef struct sigaction gdb_sigaction_t;
263 #ifdef HAVE_PR_SIGINFO64_T
264 typedef pr_siginfo64_t gdb_siginfo_t;
266 typedef struct siginfo gdb_siginfo_t;
269 /* On mips-irix, praddset and prdelset are defined in such a way that
270 they return a value, which causes GCC to emit a -Wunused error
271 because the returned value is not used. Prevent this warning
272 by casting the return value to void. On sparc-solaris, this issue
273 does not exist because the definition of these macros already include
274 that cast to void. */
275 #define gdb_praddset(sp, flag) ((void) praddset (sp, flag))
276 #define gdb_prdelset(sp, flag) ((void) prdelset (sp, flag))
278 /* gdb_premptysysset */
280 #define gdb_premptysysset premptysysset
282 #define gdb_premptysysset premptyset
287 #define gdb_praddsysset praddsysset
289 #define gdb_praddsysset gdb_praddset
294 #define gdb_prdelsysset prdelsysset
296 #define gdb_prdelsysset gdb_prdelset
299 /* prissyssetmember */
300 #ifdef prissyssetmember
301 #define gdb_pr_issyssetmember prissyssetmember
303 #define gdb_pr_issyssetmember prismember
306 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
307 as intuitively descriptive as it could be, so we'll define
308 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
309 this writing, this feature is only found on AIX5 systems and
310 basically means that the set of syscalls is not fixed. I.e,
311 there's no nice table that one can #include to get all of the
312 syscall numbers. Instead, they're stored in /proc/PID/sysent
313 for each process. We are at least guaranteed that they won't
314 change over the lifetime of the process. But each process could
315 (in theory) have different syscall numbers. */
316 #ifdef HAVE_PRSYSENT_T
317 #define DYNAMIC_SYSCALLS
322 /* =================== STRUCT PROCINFO "MODULE" =================== */
324 /* FIXME: this comment will soon be out of date W.R.T. threads. */
326 /* The procinfo struct is a wrapper to hold all the state information
327 concerning a /proc process. There should be exactly one procinfo
328 for each process, and since GDB currently can debug only one
329 process at a time, that means there should be only one procinfo.
330 All of the LWP's of a process can be accessed indirectly thru the
331 single process procinfo.
333 However, against the day when GDB may debug more than one process,
334 this data structure is kept in a list (which for now will hold no
335 more than one member), and many functions will have a pointer to a
336 procinfo as an argument.
338 There will be a separate procinfo structure for use by the (not yet
339 implemented) "info proc" command, so that we can print useful
340 information about any random process without interfering with the
341 inferior's procinfo information. */
344 /* format strings for /proc paths */
345 # ifndef CTL_PROC_NAME_FMT
346 # define MAIN_PROC_NAME_FMT "/proc/%d"
347 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
348 # define AS_PROC_NAME_FMT "/proc/%d/as"
349 # define MAP_PROC_NAME_FMT "/proc/%d/map"
350 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
351 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
353 /* the name of the proc status struct depends on the implementation */
354 typedef pstatus_t gdb_prstatus_t;
355 typedef lwpstatus_t gdb_lwpstatus_t;
356 #else /* ! NEW_PROC_API */
357 /* format strings for /proc paths */
358 # ifndef CTL_PROC_NAME_FMT
359 # define MAIN_PROC_NAME_FMT "/proc/%05d"
360 # define CTL_PROC_NAME_FMT "/proc/%05d"
361 # define AS_PROC_NAME_FMT "/proc/%05d"
362 # define MAP_PROC_NAME_FMT "/proc/%05d"
363 # define STATUS_PROC_NAME_FMT "/proc/%05d"
364 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
366 /* the name of the proc status struct depends on the implementation */
367 typedef prstatus_t gdb_prstatus_t;
368 typedef prstatus_t gdb_lwpstatus_t;
369 #endif /* NEW_PROC_API */
371 typedef struct procinfo {
372 struct procinfo *next;
373 int pid; /* Process ID */
374 int tid; /* Thread/LWP id */
378 int ignore_next_sigstop;
380 /* The following four fd fields may be identical, or may contain
381 several different fd's, depending on the version of /proc
382 (old ioctl or new read/write). */
384 int ctl_fd; /* File descriptor for /proc control file */
386 /* The next three file descriptors are actually only needed in the
387 read/write, multiple-file-descriptor implemenation
388 (NEW_PROC_API). However, to avoid a bunch of #ifdefs in the
389 code, we will use them uniformly by (in the case of the ioctl
390 single-file-descriptor implementation) filling them with copies
391 of the control fd. */
392 int status_fd; /* File descriptor for /proc status file */
393 int as_fd; /* File descriptor for /proc as file */
395 char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */
397 fltset_t saved_fltset; /* Saved traced hardware fault set */
398 gdb_sigset_t saved_sigset; /* Saved traced signal set */
399 gdb_sigset_t saved_sighold; /* Saved held signal set */
400 sysset_t *saved_exitset; /* Saved traced system call exit set */
401 sysset_t *saved_entryset; /* Saved traced system call entry set */
403 gdb_prstatus_t prstatus; /* Current process status info */
406 gdb_fpregset_t fpregset; /* Current floating point registers */
409 #ifdef DYNAMIC_SYSCALLS
410 int num_syscalls; /* Total number of syscalls */
411 char **syscall_names; /* Syscall number to name map */
414 struct procinfo *thread_list;
416 int status_valid : 1;
418 int fpregs_valid : 1;
419 int threads_valid: 1;
422 static char errmsg[128]; /* shared error msg buffer */
424 /* Function prototypes for procinfo module: */
426 static procinfo *find_procinfo_or_die (int pid, int tid);
427 static procinfo *find_procinfo (int pid, int tid);
428 static procinfo *create_procinfo (int pid, int tid);
429 static void destroy_procinfo (procinfo * p);
430 static void do_destroy_procinfo_cleanup (void *);
431 static void dead_procinfo (procinfo * p, char *msg, int killp);
432 static int open_procinfo_files (procinfo * p, int which);
433 static void close_procinfo_files (procinfo * p);
434 static int sysset_t_size (procinfo *p);
435 static sysset_t *sysset_t_alloc (procinfo * pi);
436 #ifdef DYNAMIC_SYSCALLS
437 static void load_syscalls (procinfo *pi);
438 static void free_syscalls (procinfo *pi);
439 static int find_syscall (procinfo *pi, char *name);
440 #endif /* DYNAMIC_SYSCALLS */
442 static int iterate_over_mappings
443 (procinfo *pi, find_memory_region_ftype child_func, void *data,
444 int (*func) (struct prmap *map, find_memory_region_ftype child_func,
447 /* The head of the procinfo list: */
448 static procinfo * procinfo_list;
450 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
454 find_procinfo (int pid, int tid)
458 for (pi = procinfo_list; pi; pi = pi->next)
465 /* Don't check threads_valid. If we're updating the
466 thread_list, we want to find whatever threads are already
467 here. This means that in general it is the caller's
468 responsibility to check threads_valid and update before
469 calling find_procinfo, if the caller wants to find a new
472 for (pi = pi->thread_list; pi; pi = pi->next)
480 /* Calls find_procinfo, but errors on failure. */
483 find_procinfo_or_die (int pid, int tid)
485 procinfo *pi = find_procinfo (pid, tid);
490 error (_("procfs: couldn't find pid %d "
491 "(kernel thread %d) in procinfo list."),
494 error (_("procfs: couldn't find pid %d in procinfo list."), pid);
499 /* Wrapper for `open'. The appropriate open call is attempted; if
500 unsuccessful, it will be retried as many times as needed for the
501 EAGAIN and EINTR conditions.
503 For other conditions, retry the open a limited number of times. In
504 addition, a short sleep is imposed prior to retrying the open. The
505 reason for this sleep is to give the kernel a chance to catch up
506 and create the file in question in the event that GDB "wins" the
507 race to open a file before the kernel has created it. */
510 open_with_retry (const char *pathname, int flags)
512 int retries_remaining, status;
514 retries_remaining = 2;
518 status = open (pathname, flags);
520 if (status >= 0 || retries_remaining == 0)
522 else if (errno != EINTR && errno != EAGAIN)
532 /* Open the file descriptor for the process or LWP. If NEW_PROC_API
533 is defined, we only open the control file descriptor; the others
534 are opened lazily as needed. Otherwise (if not NEW_PROC_API),
535 there is only one real file descriptor, but we keep multiple copies
536 of it so that the code that uses them does not have to be #ifdef'd.
537 Returns the file descriptor, or zero for failure. */
539 enum { FD_CTL, FD_STATUS, FD_AS };
542 open_procinfo_files (procinfo *pi, int which)
545 char tmp[MAX_PROC_NAME_SIZE];
549 /* This function is getting ALMOST long enough to break up into
550 several. Here is some rationale:
552 NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
553 There are several file descriptors that may need to be open
554 for any given process or LWP. The ones we're intereted in are:
555 - control (ctl) write-only change the state
556 - status (status) read-only query the state
557 - address space (as) read/write access memory
558 - map (map) read-only virtual addr map
559 Most of these are opened lazily as they are needed.
560 The pathnames for the 'files' for an LWP look slightly
561 different from those of a first-class process:
562 Pathnames for a process (<proc-id>):
564 /proc/<proc-id>/status
567 Pathnames for an LWP (lwp-id):
568 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
569 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
570 An LWP has no map or address space file descriptor, since
571 the memory map and address space are shared by all LWPs.
573 Everyone else (Solaris 2.5, Irix, OSF)
574 There is only one file descriptor for each process or LWP.
575 For convenience, we copy the same file descriptor into all
576 three fields of the procinfo struct (ctl_fd, status_fd, and
577 as_fd, see NEW_PROC_API above) so that code that uses them
578 doesn't need any #ifdef's.
583 Each LWP has an independent file descriptor, but these
584 are not obtained via the 'open' system call like the rest:
585 instead, they're obtained thru an ioctl call (PIOCOPENLWP)
586 to the file descriptor of the parent process.
589 These do not even have their own independent file descriptor.
590 All operations are carried out on the file descriptor of the
591 parent process. Therefore we just call open again for each
592 thread, getting a new handle for the same 'file'.
596 /* In this case, there are several different file descriptors that
597 we might be asked to open. The control file descriptor will be
598 opened early, but the others will be opened lazily as they are
601 strcpy (tmp, pi->pathname);
602 switch (which) { /* which file descriptor to open? */
605 strcat (tmp, "/lwpctl");
607 strcat (tmp, "/ctl");
608 fd = open_with_retry (tmp, O_WRONLY);
615 return 0; /* there is no 'as' file descriptor for an lwp */
617 fd = open_with_retry (tmp, O_RDWR);
624 strcat (tmp, "/lwpstatus");
626 strcat (tmp, "/status");
627 fd = open_with_retry (tmp, O_RDONLY);
633 return 0; /* unknown file descriptor */
635 #else /* not NEW_PROC_API */
636 /* In this case, there is only one file descriptor for each procinfo
637 (ie. each process or LWP). In fact, only the file descriptor for
638 the process can actually be opened by an 'open' system call. The
639 ones for the LWPs have to be obtained thru an IOCTL call on the
640 process's file descriptor.
642 For convenience, we copy each procinfo's single file descriptor
643 into all of the fields occupied by the several file descriptors
644 of the NEW_PROC_API implementation. That way, the code that uses
645 them can be written without ifdefs. */
648 #ifdef PIOCTSTATUS /* OSF */
649 /* Only one FD; just open it. */
650 if ((fd = open_with_retry (pi->pathname, O_RDWR)) == 0)
652 #else /* Sol 2.5, Irix, other? */
653 if (pi->tid == 0) /* Master procinfo for the process */
655 fd = open_with_retry (pi->pathname, O_RDWR);
659 else /* LWP thread procinfo */
661 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
665 /* Find the procinfo for the entire process. */
666 if ((process = find_procinfo (pi->pid, 0)) == NULL)
669 /* Now obtain the file descriptor for the LWP. */
670 if ((fd = ioctl (process->ctl_fd, PIOCOPENLWP, &lwpid)) <= 0)
672 #else /* Irix, other? */
673 return 0; /* Don't know how to open threads */
674 #endif /* Sol 2.5 PIOCOPENLWP */
676 #endif /* OSF PIOCTSTATUS */
677 pi->ctl_fd = pi->as_fd = pi->status_fd = fd;
678 #endif /* NEW_PROC_API */
680 return 1; /* success */
683 /* Allocate a data structure and link it into the procinfo list.
684 First tries to find a pre-existing one (FIXME: why?). Returns the
685 pointer to new procinfo struct. */
688 create_procinfo (int pid, int tid)
690 procinfo *pi, *parent = NULL;
692 if ((pi = find_procinfo (pid, tid)))
693 return pi; /* Already exists, nothing to do. */
695 /* find parent before doing malloc, to save having to cleanup */
697 parent = find_procinfo_or_die (pid, 0); /* FIXME: should I
699 doesn't exist yet? */
701 pi = (procinfo *) xmalloc (sizeof (procinfo));
702 memset (pi, 0, sizeof (procinfo));
706 #ifdef DYNAMIC_SYSCALLS
710 pi->saved_entryset = sysset_t_alloc (pi);
711 pi->saved_exitset = sysset_t_alloc (pi);
713 /* Chain into list. */
716 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
717 pi->next = procinfo_list;
723 sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid);
725 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
727 pi->next = parent->thread_list;
728 parent->thread_list = pi;
733 /* Close all file descriptors associated with the procinfo. */
736 close_procinfo_files (procinfo *pi)
743 if (pi->status_fd > 0)
744 close (pi->status_fd);
746 pi->ctl_fd = pi->as_fd = pi->status_fd = 0;
749 /* Destructor function. Close, unlink and deallocate the object. */
752 destroy_one_procinfo (procinfo **list, procinfo *pi)
756 /* Step one: unlink the procinfo from its list. */
760 for (ptr = *list; ptr; ptr = ptr->next)
763 ptr->next = pi->next;
767 /* Step two: close any open file descriptors. */
768 close_procinfo_files (pi);
770 /* Step three: free the memory. */
771 #ifdef DYNAMIC_SYSCALLS
774 xfree (pi->saved_entryset);
775 xfree (pi->saved_exitset);
780 destroy_procinfo (procinfo *pi)
784 if (pi->tid != 0) /* destroy a thread procinfo */
786 tmp = find_procinfo (pi->pid, 0); /* find the parent process */
787 destroy_one_procinfo (&tmp->thread_list, pi);
789 else /* destroy a process procinfo and all its threads */
791 /* First destroy the children, if any; */
792 while (pi->thread_list != NULL)
793 destroy_one_procinfo (&pi->thread_list, pi->thread_list);
794 /* Then destroy the parent. Genocide!!! */
795 destroy_one_procinfo (&procinfo_list, pi);
800 do_destroy_procinfo_cleanup (void *pi)
802 destroy_procinfo (pi);
805 enum { NOKILL, KILL };
807 /* To be called on a non_recoverable error for a procinfo. Prints
808 error messages, optionally sends a SIGKILL to the process, then
809 destroys the data structure. */
812 dead_procinfo (procinfo *pi, char *msg, int kill_p)
818 print_sys_errmsg (pi->pathname, errno);
822 sprintf (procfile, "process %d", pi->pid);
823 print_sys_errmsg (procfile, errno);
826 kill (pi->pid, SIGKILL);
828 destroy_procinfo (pi);
832 /* Returns the (complete) size of a sysset_t struct. Normally, this
833 is just sizeof (sysset_t), but in the case of Monterey/64, the
834 actual size of sysset_t isn't known until runtime. */
837 sysset_t_size (procinfo * pi)
839 #ifndef DYNAMIC_SYSCALLS
840 return sizeof (sysset_t);
842 return sizeof (sysset_t) - sizeof (uint64_t)
843 + sizeof (uint64_t) * ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
844 / (8 * sizeof (uint64_t)));
848 /* Allocate and (partially) initialize a sysset_t struct. */
851 sysset_t_alloc (procinfo * pi)
854 int size = sysset_t_size (pi);
856 ret = xmalloc (size);
857 #ifdef DYNAMIC_SYSCALLS
858 ret->pr_size = ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
859 / (8 * sizeof (uint64_t)));
864 #ifdef DYNAMIC_SYSCALLS
866 /* Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
867 pi->num_syscalls with the number of syscalls and pi->syscall_names
868 with the names. (Certain numbers may be skipped in which case the
869 names for these numbers will be left as NULL.) */
871 #define MAX_SYSCALL_NAME_LENGTH 256
872 #define MAX_SYSCALLS 65536
875 load_syscalls (procinfo *pi)
877 char pathname[MAX_PROC_NAME_SIZE];
880 prsyscall_t *syscalls;
881 int i, size, maxcall;
883 pi->num_syscalls = 0;
884 pi->syscall_names = 0;
886 /* Open the file descriptor for the sysent file. */
887 sprintf (pathname, "/proc/%d/sysent", pi->pid);
888 sysent_fd = open_with_retry (pathname, O_RDONLY);
891 error (_("load_syscalls: Can't open /proc/%d/sysent"), pi->pid);
894 size = sizeof header - sizeof (prsyscall_t);
895 if (read (sysent_fd, &header, size) != size)
897 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
900 if (header.pr_nsyscalls == 0)
902 error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"),
906 size = header.pr_nsyscalls * sizeof (prsyscall_t);
907 syscalls = xmalloc (size);
909 if (read (sysent_fd, syscalls, size) != size)
912 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
915 /* Find maximum syscall number. This may not be the same as
916 pr_nsyscalls since that value refers to the number of entries
917 in the table. (Also, the docs indicate that some system
918 call numbers may be skipped.) */
920 maxcall = syscalls[0].pr_number;
922 for (i = 1; i < header.pr_nsyscalls; i++)
923 if (syscalls[i].pr_number > maxcall
924 && syscalls[i].pr_nameoff > 0
925 && syscalls[i].pr_number < MAX_SYSCALLS)
926 maxcall = syscalls[i].pr_number;
928 pi->num_syscalls = maxcall+1;
929 pi->syscall_names = xmalloc (pi->num_syscalls * sizeof (char *));
931 for (i = 0; i < pi->num_syscalls; i++)
932 pi->syscall_names[i] = NULL;
934 /* Read the syscall names in. */
935 for (i = 0; i < header.pr_nsyscalls; i++)
937 char namebuf[MAX_SYSCALL_NAME_LENGTH];
941 if (syscalls[i].pr_number >= MAX_SYSCALLS
942 || syscalls[i].pr_number < 0
943 || syscalls[i].pr_nameoff <= 0
944 || (lseek (sysent_fd, (off_t) syscalls[i].pr_nameoff, SEEK_SET)
945 != (off_t) syscalls[i].pr_nameoff))
948 nread = read (sysent_fd, namebuf, sizeof namebuf);
952 callnum = syscalls[i].pr_number;
954 if (pi->syscall_names[callnum] != NULL)
956 /* FIXME: Generate warning */
960 namebuf[nread-1] = '\0';
961 size = strlen (namebuf) + 1;
962 pi->syscall_names[callnum] = xmalloc (size);
963 strncpy (pi->syscall_names[callnum], namebuf, size-1);
964 pi->syscall_names[callnum][size-1] = '\0';
971 /* Free the space allocated for the syscall names from the procinfo
975 free_syscalls (procinfo *pi)
977 if (pi->syscall_names)
981 for (i = 0; i < pi->num_syscalls; i++)
982 if (pi->syscall_names[i] != NULL)
983 xfree (pi->syscall_names[i]);
985 xfree (pi->syscall_names);
986 pi->syscall_names = 0;
990 /* Given a name, look up (and return) the corresponding syscall number.
991 If no match is found, return -1. */
994 find_syscall (procinfo *pi, char *name)
998 for (i = 0; i < pi->num_syscalls; i++)
1000 if (pi->syscall_names[i] && strcmp (name, pi->syscall_names[i]) == 0)
1007 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1009 /* =================== /proc "MODULE" =================== */
1011 /* This "module" is the interface layer between the /proc system API
1012 and the gdb target vector functions. This layer consists of access
1013 functions that encapsulate each of the basic operations that we
1014 need to use from the /proc API.
1016 The main motivation for this layer is to hide the fact that there
1017 are two very different implementations of the /proc API. Rather
1018 than have a bunch of #ifdefs all thru the gdb target vector
1019 functions, we do our best to hide them all in here. */
1021 int proc_get_status (procinfo * pi);
1022 long proc_flags (procinfo * pi);
1023 int proc_why (procinfo * pi);
1024 int proc_what (procinfo * pi);
1025 int proc_set_run_on_last_close (procinfo * pi);
1026 int proc_unset_run_on_last_close (procinfo * pi);
1027 int proc_set_inherit_on_fork (procinfo * pi);
1028 int proc_unset_inherit_on_fork (procinfo * pi);
1029 int proc_set_async (procinfo * pi);
1030 int proc_unset_async (procinfo * pi);
1031 int proc_stop_process (procinfo * pi);
1032 int proc_trace_signal (procinfo * pi, int signo);
1033 int proc_ignore_signal (procinfo * pi, int signo);
1034 int proc_clear_current_fault (procinfo * pi);
1035 int proc_set_current_signal (procinfo * pi, int signo);
1036 int proc_clear_current_signal (procinfo * pi);
1037 int proc_set_gregs (procinfo * pi);
1038 int proc_set_fpregs (procinfo * pi);
1039 int proc_wait_for_stop (procinfo * pi);
1040 int proc_run_process (procinfo * pi, int step, int signo);
1041 int proc_kill (procinfo * pi, int signo);
1042 int proc_parent_pid (procinfo * pi);
1043 int proc_get_nthreads (procinfo * pi);
1044 int proc_get_current_thread (procinfo * pi);
1045 int proc_set_held_signals (procinfo * pi, gdb_sigset_t * sighold);
1046 int proc_set_traced_sysexit (procinfo * pi, sysset_t * sysset);
1047 int proc_set_traced_sysentry (procinfo * pi, sysset_t * sysset);
1048 int proc_set_traced_faults (procinfo * pi, fltset_t * fltset);
1049 int proc_set_traced_signals (procinfo * pi, gdb_sigset_t * sigset);
1051 int proc_update_threads (procinfo * pi);
1052 int proc_iterate_over_threads (procinfo * pi,
1053 int (*func) (procinfo *, procinfo *, void *),
1056 gdb_gregset_t *proc_get_gregs (procinfo * pi);
1057 gdb_fpregset_t *proc_get_fpregs (procinfo * pi);
1058 sysset_t *proc_get_traced_sysexit (procinfo * pi, sysset_t * save);
1059 sysset_t *proc_get_traced_sysentry (procinfo * pi, sysset_t * save);
1060 fltset_t *proc_get_traced_faults (procinfo * pi, fltset_t * save);
1061 gdb_sigset_t *proc_get_traced_signals (procinfo * pi, gdb_sigset_t * save);
1062 gdb_sigset_t *proc_get_held_signals (procinfo * pi, gdb_sigset_t * save);
1063 gdb_sigset_t *proc_get_pending_signals (procinfo * pi, gdb_sigset_t * save);
1064 gdb_sigaction_t *proc_get_signal_actions (procinfo * pi,
1065 gdb_sigaction_t *save);
1067 void proc_warn (procinfo * pi, char *func, int line);
1068 void proc_error (procinfo * pi, char *func, int line);
1071 proc_warn (procinfo *pi, char *func, int line)
1073 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1074 print_sys_errmsg (errmsg, errno);
1078 proc_error (procinfo *pi, char *func, int line)
1080 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1081 perror_with_name (errmsg);
1084 /* Updates the status struct in the procinfo. There is a 'valid'
1085 flag, to let other functions know when this function needs to be
1086 called (so the status is only read when it is needed). The status
1087 file descriptor is also only opened when it is needed. Returns
1088 non-zero for success, zero for failure. */
1091 proc_get_status (procinfo *pi)
1093 /* Status file descriptor is opened "lazily" */
1094 if (pi->status_fd == 0 &&
1095 open_procinfo_files (pi, FD_STATUS) == 0)
1097 pi->status_valid = 0;
1102 if (lseek (pi->status_fd, 0, SEEK_SET) < 0)
1103 pi->status_valid = 0; /* fail */
1106 /* Sigh... I have to read a different data structure,
1107 depending on whether this is a main process or an LWP. */
1109 pi->status_valid = (read (pi->status_fd,
1110 (char *) &pi->prstatus.pr_lwp,
1111 sizeof (lwpstatus_t))
1112 == sizeof (lwpstatus_t));
1115 pi->status_valid = (read (pi->status_fd,
1116 (char *) &pi->prstatus,
1117 sizeof (gdb_prstatus_t))
1118 == sizeof (gdb_prstatus_t));
1119 #if 0 /*def UNIXWARE*/
1120 if (pi->status_valid &&
1121 (pi->prstatus.pr_lwp.pr_flags & PR_ISTOP) &&
1122 pi->prstatus.pr_lwp.pr_why == PR_REQUESTED)
1123 /* Unixware peculiarity -- read the damn thing again! */
1124 pi->status_valid = (read (pi->status_fd,
1125 (char *) &pi->prstatus,
1126 sizeof (gdb_prstatus_t))
1127 == sizeof (gdb_prstatus_t));
1128 #endif /* UNIXWARE */
1131 #else /* ioctl method */
1132 #ifdef PIOCTSTATUS /* osf */
1133 if (pi->tid == 0) /* main process */
1135 /* Just read the danged status. Now isn't that simple? */
1137 (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1144 tid_t pr_error_thread;
1145 struct prstatus status;
1148 thread_status.pr_count = 1;
1149 thread_status.status.pr_tid = pi->tid;
1150 win = (ioctl (pi->status_fd, PIOCTSTATUS, &thread_status) >= 0);
1153 memcpy (&pi->prstatus, &thread_status.status,
1154 sizeof (pi->prstatus));
1155 pi->status_valid = 1;
1159 /* Just read the danged status. Now isn't that simple? */
1160 pi->status_valid = (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1164 if (pi->status_valid)
1166 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1169 proc_get_current_thread (pi));
1172 /* The status struct includes general regs, so mark them valid too. */
1173 pi->gregs_valid = pi->status_valid;
1175 /* In the read/write multiple-fd model, the status struct includes
1176 the fp regs too, so mark them valid too. */
1177 pi->fpregs_valid = pi->status_valid;
1179 return pi->status_valid; /* True if success, false if failure. */
1182 /* Returns the process flags (pr_flags field). */
1185 proc_flags (procinfo *pi)
1187 if (!pi->status_valid)
1188 if (!proc_get_status (pi))
1189 return 0; /* FIXME: not a good failure value (but what is?) */
1193 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1194 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1195 The two sets of flags don't overlap. */
1196 return pi->prstatus.pr_flags | pi->prstatus.pr_lwp.pr_flags;
1198 return pi->prstatus.pr_lwp.pr_flags;
1201 return pi->prstatus.pr_flags;
1205 /* Returns the pr_why field (why the process stopped). */
1208 proc_why (procinfo *pi)
1210 if (!pi->status_valid)
1211 if (!proc_get_status (pi))
1212 return 0; /* FIXME: not a good failure value (but what is?) */
1215 return pi->prstatus.pr_lwp.pr_why;
1217 return pi->prstatus.pr_why;
1221 /* Returns the pr_what field (details of why the process stopped). */
1224 proc_what (procinfo *pi)
1226 if (!pi->status_valid)
1227 if (!proc_get_status (pi))
1228 return 0; /* FIXME: not a good failure value (but what is?) */
1231 return pi->prstatus.pr_lwp.pr_what;
1233 return pi->prstatus.pr_what;
1237 /* This function is only called when PI is stopped by a watchpoint.
1238 Assuming the OS supports it, write to *ADDR the data address which
1239 triggered it and return 1. Return 0 if it is not possible to know
1243 proc_watchpoint_address (procinfo *pi, CORE_ADDR *addr)
1245 if (!pi->status_valid)
1246 if (!proc_get_status (pi))
1250 *addr = (CORE_ADDR) gdbarch_pointer_to_address (target_gdbarch,
1251 builtin_type (target_gdbarch)->builtin_data_ptr,
1252 (gdb_byte *) &pi->prstatus.pr_lwp.pr_info.si_addr);
1254 *addr = (CORE_ADDR) gdbarch_pointer_to_address (target_gdbarch,
1255 builtin_type (target_gdbarch)->builtin_data_ptr,
1256 (gdb_byte *) &pi->prstatus.pr_info.si_addr);
1261 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1263 /* Returns the pr_nsysarg field (number of args to the current
1267 proc_nsysarg (procinfo *pi)
1269 if (!pi->status_valid)
1270 if (!proc_get_status (pi))
1274 return pi->prstatus.pr_lwp.pr_nsysarg;
1276 return pi->prstatus.pr_nsysarg;
1280 /* Returns the pr_sysarg field (pointer to the arguments of current
1284 proc_sysargs (procinfo *pi)
1286 if (!pi->status_valid)
1287 if (!proc_get_status (pi))
1291 return (long *) &pi->prstatus.pr_lwp.pr_sysarg;
1293 return (long *) &pi->prstatus.pr_sysarg;
1297 /* Returns the pr_syscall field (id of current syscall if we are in
1301 proc_syscall (procinfo *pi)
1303 if (!pi->status_valid)
1304 if (!proc_get_status (pi))
1308 return pi->prstatus.pr_lwp.pr_syscall;
1310 return pi->prstatus.pr_syscall;
1313 #endif /* PIOCSSPCACT */
1315 /* Returns the pr_cursig field (current signal). */
1318 proc_cursig (struct procinfo *pi)
1320 if (!pi->status_valid)
1321 if (!proc_get_status (pi))
1322 return 0; /* FIXME: not a good failure value (but what is?) */
1325 return pi->prstatus.pr_lwp.pr_cursig;
1327 return pi->prstatus.pr_cursig;
1331 /* === I appologize for the messiness of this function.
1332 === This is an area where the different versions of
1333 === /proc are more inconsistent than usual.
1335 Set or reset any of the following process flags:
1336 PR_FORK -- forked child will inherit trace flags
1337 PR_RLC -- traced process runs when last /proc file closed.
1338 PR_KLC -- traced process is killed when last /proc file closed.
1339 PR_ASYNC -- LWP's get to run/stop independently.
1341 There are three methods for doing this function:
1342 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1344 2) Middle: PIOCSET/PIOCRESET
1346 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1349 Note: Irix does not define PR_ASYNC.
1350 Note: OSF does not define PR_KLC.
1351 Note: OSF is the only one that can ONLY use the oldest method.
1355 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1356 mode -- 1 for set, 0 for reset.
1358 Returns non-zero for success, zero for failure. */
1360 enum { FLAG_RESET, FLAG_SET };
1363 proc_modify_flag (procinfo *pi, long flag, long mode)
1365 long win = 0; /* default to fail */
1367 /* These operations affect the process as a whole, and applying them
1368 to an individual LWP has the same meaning as applying them to the
1369 main process. Therefore, if we're ever called with a pointer to
1370 an LWP's procinfo, let's substitute the process's procinfo and
1371 avoid opening the LWP's file descriptor unnecessarily. */
1374 pi = find_procinfo_or_die (pi->pid, 0);
1376 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1377 /* First normalize the PCUNSET/PCRESET command opcode
1378 (which for no obvious reason has a different definition
1379 from one operating system to the next...) */
1381 #define GDBRESET PCUNSET
1384 #define GDBRESET PCRESET
1388 procfs_ctl_t arg[2];
1390 if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC) */
1392 else /* Reset the flag */
1396 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1399 #ifdef PIOCSET /* Irix/Sol5 method */
1400 if (mode == FLAG_SET) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1402 win = (ioctl (pi->ctl_fd, PIOCSET, &flag) >= 0);
1404 else /* Reset the flag */
1406 win = (ioctl (pi->ctl_fd, PIOCRESET, &flag) >= 0);
1410 #ifdef PIOCSRLC /* Oldest method: OSF */
1413 if (mode == FLAG_SET) /* Set run-on-last-close */
1415 win = (ioctl (pi->ctl_fd, PIOCSRLC, NULL) >= 0);
1417 else /* Clear run-on-last-close */
1419 win = (ioctl (pi->ctl_fd, PIOCRRLC, NULL) >= 0);
1423 if (mode == FLAG_SET) /* Set inherit-on-fork */
1425 win = (ioctl (pi->ctl_fd, PIOCSFORK, NULL) >= 0);
1427 else /* Clear inherit-on-fork */
1429 win = (ioctl (pi->ctl_fd, PIOCRFORK, NULL) >= 0);
1433 win = 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1440 /* The above operation renders the procinfo's cached pstatus
1442 pi->status_valid = 0;
1445 warning (_("procfs: modify_flag failed to turn %s %s"),
1446 flag == PR_FORK ? "PR_FORK" :
1447 flag == PR_RLC ? "PR_RLC" :
1449 flag == PR_ASYNC ? "PR_ASYNC" :
1452 flag == PR_KLC ? "PR_KLC" :
1455 mode == FLAG_RESET ? "off" : "on");
1460 /* Set the run_on_last_close flag. Process with all threads will
1461 become runnable when debugger closes all /proc fds. Returns
1462 non-zero for success, zero for failure. */
1465 proc_set_run_on_last_close (procinfo *pi)
1467 return proc_modify_flag (pi, PR_RLC, FLAG_SET);
1470 /* Reset the run_on_last_close flag. The process will NOT become
1471 runnable when debugger closes its file handles. Returns non-zero
1472 for success, zero for failure. */
1475 proc_unset_run_on_last_close (procinfo *pi)
1477 return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
1481 /* Set the kill_on_last_close flag. Process with all threads will be
1482 killed when debugger closes all /proc fds (or debugger exits or
1483 dies). Returns non-zero for success, zero for failure. */
1486 proc_set_kill_on_last_close (procinfo *pi)
1488 return proc_modify_flag (pi, PR_KLC, FLAG_SET);
1491 /* Reset the kill_on_last_close flag. Process will NOT be killed when
1492 debugger closes its file handles (or exits or dies). Returns
1493 non-zero for success, zero for failure. */
1496 proc_unset_kill_on_last_close (procinfo *pi)
1498 return proc_modify_flag (pi, PR_KLC, FLAG_RESET);
1502 /* Set inherit_on_fork flag. If the process forks a child while we
1503 are registered for events in the parent, then we will also recieve
1504 events from the child. Returns non-zero for success, zero for
1508 proc_set_inherit_on_fork (procinfo *pi)
1510 return proc_modify_flag (pi, PR_FORK, FLAG_SET);
1513 /* Reset inherit_on_fork flag. If the process forks a child while we
1514 are registered for events in the parent, then we will NOT recieve
1515 events from the child. Returns non-zero for success, zero for
1519 proc_unset_inherit_on_fork (procinfo *pi)
1521 return proc_modify_flag (pi, PR_FORK, FLAG_RESET);
1525 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
1526 (signal etc.), the remaining LWPs will continue to run. Returns
1527 non-zero for success, zero for failure. */
1530 proc_set_async (procinfo *pi)
1532 return proc_modify_flag (pi, PR_ASYNC, FLAG_SET);
1535 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
1536 (signal etc.), then all other LWPs will stop as well. Returns
1537 non-zero for success, zero for failure. */
1540 proc_unset_async (procinfo *pi)
1542 return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET);
1544 #endif /* PR_ASYNC */
1546 /* Request the process/LWP to stop. Does not wait. Returns non-zero
1547 for success, zero for failure. */
1550 proc_stop_process (procinfo *pi)
1554 /* We might conceivably apply this operation to an LWP, and the
1555 LWP's ctl file descriptor might not be open. */
1557 if (pi->ctl_fd == 0 &&
1558 open_procinfo_files (pi, FD_CTL) == 0)
1563 procfs_ctl_t cmd = PCSTOP;
1565 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1566 #else /* ioctl method */
1567 win = (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) >= 0);
1568 /* Note: the call also reads the prstatus. */
1571 pi->status_valid = 1;
1572 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1575 proc_get_current_thread (pi));
1583 /* Wait for the process or LWP to stop (block until it does). Returns
1584 non-zero for success, zero for failure. */
1587 proc_wait_for_stop (procinfo *pi)
1591 /* We should never have to apply this operation to any procinfo
1592 except the one for the main process. If that ever changes for
1593 any reason, then take out the following clause and replace it
1594 with one that makes sure the ctl_fd is open. */
1597 pi = find_procinfo_or_die (pi->pid, 0);
1601 procfs_ctl_t cmd = PCWSTOP;
1603 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1604 /* We been runnin' and we stopped -- need to update status. */
1605 pi->status_valid = 0;
1607 #else /* ioctl method */
1608 win = (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) >= 0);
1609 /* Above call also refreshes the prstatus. */
1612 pi->status_valid = 1;
1613 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1616 proc_get_current_thread (pi));
1623 /* Make the process or LWP runnable.
1625 Options (not all are implemented):
1627 - clear current fault
1628 - clear current signal
1629 - abort the current system call
1630 - stop as soon as finished with system call
1631 - (ioctl): set traced signal set
1632 - (ioctl): set held signal set
1633 - (ioctl): set traced fault set
1634 - (ioctl): set start pc (vaddr)
1636 Always clears the current fault. PI is the process or LWP to
1637 operate on. If STEP is true, set the process or LWP to trap after
1638 one instruction. If SIGNO is zero, clear the current signal if
1639 any; if non-zero, set the current signal to this one. Returns
1640 non-zero for success, zero for failure. */
1643 proc_run_process (procinfo *pi, int step, int signo)
1648 /* We will probably have to apply this operation to individual
1649 threads, so make sure the control file descriptor is open. */
1651 if (pi->ctl_fd == 0 &&
1652 open_procinfo_files (pi, FD_CTL) == 0)
1657 runflags = PRCFAULT; /* always clear current fault */
1662 else if (signo != -1) /* -1 means do nothing W.R.T. signals */
1663 proc_set_current_signal (pi, signo);
1667 procfs_ctl_t cmd[2];
1671 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1673 #else /* ioctl method */
1677 memset (&prrun, 0, sizeof (prrun));
1678 prrun.pr_flags = runflags;
1679 win = (ioctl (pi->ctl_fd, PIOCRUN, &prrun) >= 0);
1686 /* Register to trace signals in the process or LWP. Returns non-zero
1687 for success, zero for failure. */
1690 proc_set_traced_signals (procinfo *pi, gdb_sigset_t *sigset)
1694 /* We should never have to apply this operation to any procinfo
1695 except the one for the main process. If that ever changes for
1696 any reason, then take out the following clause and replace it
1697 with one that makes sure the ctl_fd is open. */
1700 pi = find_procinfo_or_die (pi->pid, 0);
1706 /* Use char array to avoid alignment issues. */
1707 char sigset[sizeof (gdb_sigset_t)];
1711 memcpy (&arg.sigset, sigset, sizeof (gdb_sigset_t));
1713 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1715 #else /* ioctl method */
1716 win = (ioctl (pi->ctl_fd, PIOCSTRACE, sigset) >= 0);
1718 /* The above operation renders the procinfo's cached pstatus obsolete. */
1719 pi->status_valid = 0;
1722 warning (_("procfs: set_traced_signals failed"));
1726 /* Register to trace hardware faults in the process or LWP. Returns
1727 non-zero for success, zero for failure. */
1730 proc_set_traced_faults (procinfo *pi, fltset_t *fltset)
1734 /* We should never have to apply this operation to any procinfo
1735 except the one for the main process. If that ever changes for
1736 any reason, then take out the following clause and replace it
1737 with one that makes sure the ctl_fd is open. */
1740 pi = find_procinfo_or_die (pi->pid, 0);
1746 /* Use char array to avoid alignment issues. */
1747 char fltset[sizeof (fltset_t)];
1751 memcpy (&arg.fltset, fltset, sizeof (fltset_t));
1753 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1755 #else /* ioctl method */
1756 win = (ioctl (pi->ctl_fd, PIOCSFAULT, fltset) >= 0);
1758 /* The above operation renders the procinfo's cached pstatus obsolete. */
1759 pi->status_valid = 0;
1764 /* Register to trace entry to system calls in the process or LWP.
1765 Returns non-zero for success, zero for failure. */
1768 proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset)
1772 /* We should never have to apply this operation to any procinfo
1773 except the one for the main process. If that ever changes for
1774 any reason, then take out the following clause and replace it
1775 with one that makes sure the ctl_fd is open. */
1778 pi = find_procinfo_or_die (pi->pid, 0);
1782 struct gdb_proc_ctl_pcsentry {
1784 /* Use char array to avoid alignment issues. */
1785 char sysset[sizeof (sysset_t)];
1787 int argp_size = sizeof (struct gdb_proc_ctl_pcsentry)
1789 + sysset_t_size (pi);
1791 argp = xmalloc (argp_size);
1793 argp->cmd = PCSENTRY;
1794 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1796 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1799 #else /* ioctl method */
1800 win = (ioctl (pi->ctl_fd, PIOCSENTRY, sysset) >= 0);
1802 /* The above operation renders the procinfo's cached pstatus
1804 pi->status_valid = 0;
1809 /* Register to trace exit from system calls in the process or LWP.
1810 Returns non-zero for success, zero for failure. */
1813 proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset)
1817 /* We should never have to apply this operation to any procinfo
1818 except the one for the main process. If that ever changes for
1819 any reason, then take out the following clause and replace it
1820 with one that makes sure the ctl_fd is open. */
1823 pi = find_procinfo_or_die (pi->pid, 0);
1827 struct gdb_proc_ctl_pcsexit {
1829 /* Use char array to avoid alignment issues. */
1830 char sysset[sizeof (sysset_t)];
1832 int argp_size = sizeof (struct gdb_proc_ctl_pcsexit)
1834 + sysset_t_size (pi);
1836 argp = xmalloc (argp_size);
1838 argp->cmd = PCSEXIT;
1839 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1841 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1844 #else /* ioctl method */
1845 win = (ioctl (pi->ctl_fd, PIOCSEXIT, sysset) >= 0);
1847 /* The above operation renders the procinfo's cached pstatus
1849 pi->status_valid = 0;
1854 /* Specify the set of blocked / held signals in the process or LWP.
1855 Returns non-zero for success, zero for failure. */
1858 proc_set_held_signals (procinfo *pi, gdb_sigset_t *sighold)
1862 /* We should never have to apply this operation to any procinfo
1863 except the one for the main process. If that ever changes for
1864 any reason, then take out the following clause and replace it
1865 with one that makes sure the ctl_fd is open. */
1868 pi = find_procinfo_or_die (pi->pid, 0);
1874 /* Use char array to avoid alignment issues. */
1875 char hold[sizeof (gdb_sigset_t)];
1879 memcpy (&arg.hold, sighold, sizeof (gdb_sigset_t));
1880 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1883 win = (ioctl (pi->ctl_fd, PIOCSHOLD, sighold) >= 0);
1885 /* The above operation renders the procinfo's cached pstatus
1887 pi->status_valid = 0;
1892 /* Returns the set of signals that are pending in the process or LWP.
1893 Will also copy the sigset if SAVE is non-zero. */
1896 proc_get_pending_signals (procinfo *pi, gdb_sigset_t *save)
1898 gdb_sigset_t *ret = NULL;
1900 /* We should never have to apply this operation to any procinfo
1901 except the one for the main process. If that ever changes for
1902 any reason, then take out the following clause and replace it
1903 with one that makes sure the ctl_fd is open. */
1906 pi = find_procinfo_or_die (pi->pid, 0);
1908 if (!pi->status_valid)
1909 if (!proc_get_status (pi))
1913 ret = &pi->prstatus.pr_lwp.pr_lwppend;
1915 ret = &pi->prstatus.pr_sigpend;
1918 memcpy (save, ret, sizeof (gdb_sigset_t));
1923 /* Returns the set of signal actions. Will also copy the sigactionset
1924 if SAVE is non-zero. */
1927 proc_get_signal_actions (procinfo *pi, gdb_sigaction_t *save)
1929 gdb_sigaction_t *ret = NULL;
1931 /* We should never have to apply this operation to any procinfo
1932 except the one for the main process. If that ever changes for
1933 any reason, then take out the following clause and replace it
1934 with one that makes sure the ctl_fd is open. */
1937 pi = find_procinfo_or_die (pi->pid, 0);
1939 if (!pi->status_valid)
1940 if (!proc_get_status (pi))
1944 ret = &pi->prstatus.pr_lwp.pr_action;
1946 ret = &pi->prstatus.pr_action;
1949 memcpy (save, ret, sizeof (gdb_sigaction_t));
1954 /* Returns the set of signals that are held / blocked. Will also copy
1955 the sigset if SAVE is non-zero. */
1958 proc_get_held_signals (procinfo *pi, gdb_sigset_t *save)
1960 gdb_sigset_t *ret = NULL;
1962 /* We should never have to apply this operation to any procinfo
1963 except the one for the main process. If that ever changes for
1964 any reason, then take out the following clause and replace it
1965 with one that makes sure the ctl_fd is open. */
1968 pi = find_procinfo_or_die (pi->pid, 0);
1971 if (!pi->status_valid)
1972 if (!proc_get_status (pi))
1976 ret = &pi->prstatus.pr_lwp.pr_context.uc_sigmask;
1978 ret = &pi->prstatus.pr_lwp.pr_lwphold;
1979 #endif /* UNIXWARE */
1980 #else /* not NEW_PROC_API */
1982 static gdb_sigset_t sigheld;
1984 if (ioctl (pi->ctl_fd, PIOCGHOLD, &sigheld) >= 0)
1987 #endif /* NEW_PROC_API */
1989 memcpy (save, ret, sizeof (gdb_sigset_t));
1994 /* Returns the set of signals that are traced / debugged. Will also
1995 copy the sigset if SAVE is non-zero. */
1998 proc_get_traced_signals (procinfo *pi, gdb_sigset_t *save)
2000 gdb_sigset_t *ret = NULL;
2002 /* We should never have to apply this operation to any procinfo
2003 except the one for the main process. If that ever changes for
2004 any reason, then take out the following clause and replace it
2005 with one that makes sure the ctl_fd is open. */
2008 pi = find_procinfo_or_die (pi->pid, 0);
2011 if (!pi->status_valid)
2012 if (!proc_get_status (pi))
2015 ret = &pi->prstatus.pr_sigtrace;
2018 static gdb_sigset_t sigtrace;
2020 if (ioctl (pi->ctl_fd, PIOCGTRACE, &sigtrace) >= 0)
2025 memcpy (save, ret, sizeof (gdb_sigset_t));
2030 /* Add SIGNO to the set of signals that are traced. Returns non-zero
2031 for success, zero for failure. */
2034 proc_trace_signal (procinfo *pi, int signo)
2038 /* We should never have to apply this operation to any procinfo
2039 except the one for the main process. If that ever changes for
2040 any reason, then take out the following clause and replace it
2041 with one that makes sure the ctl_fd is open. */
2044 pi = find_procinfo_or_die (pi->pid, 0);
2048 if (proc_get_traced_signals (pi, &temp))
2050 gdb_praddset (&temp, signo);
2051 return proc_set_traced_signals (pi, &temp);
2055 return 0; /* failure */
2058 /* Remove SIGNO from the set of signals that are traced. Returns
2059 non-zero for success, zero for failure. */
2062 proc_ignore_signal (procinfo *pi, int signo)
2066 /* We should never have to apply this operation to any procinfo
2067 except the one for the main process. If that ever changes for
2068 any reason, then take out the following clause and replace it
2069 with one that makes sure the ctl_fd is open. */
2072 pi = find_procinfo_or_die (pi->pid, 0);
2076 if (proc_get_traced_signals (pi, &temp))
2078 gdb_prdelset (&temp, signo);
2079 return proc_set_traced_signals (pi, &temp);
2083 return 0; /* failure */
2086 /* Returns the set of hardware faults that are traced /debugged. Will
2087 also copy the faultset if SAVE is non-zero. */
2090 proc_get_traced_faults (procinfo *pi, fltset_t *save)
2092 fltset_t *ret = NULL;
2094 /* We should never have to apply this operation to any procinfo
2095 except the one for the main process. If that ever changes for
2096 any reason, then take out the following clause and replace it
2097 with one that makes sure the ctl_fd is open. */
2100 pi = find_procinfo_or_die (pi->pid, 0);
2103 if (!pi->status_valid)
2104 if (!proc_get_status (pi))
2107 ret = &pi->prstatus.pr_flttrace;
2110 static fltset_t flttrace;
2112 if (ioctl (pi->ctl_fd, PIOCGFAULT, &flttrace) >= 0)
2117 memcpy (save, ret, sizeof (fltset_t));
2122 /* Returns the set of syscalls that are traced /debugged on entry.
2123 Will also copy the syscall set if SAVE is non-zero. */
2126 proc_get_traced_sysentry (procinfo *pi, sysset_t *save)
2128 sysset_t *ret = NULL;
2130 /* We should never have to apply this operation to any procinfo
2131 except the one for the main process. If that ever changes for
2132 any reason, then take out the following clause and replace it
2133 with one that makes sure the ctl_fd is open. */
2136 pi = find_procinfo_or_die (pi->pid, 0);
2139 if (!pi->status_valid)
2140 if (!proc_get_status (pi))
2143 #ifndef DYNAMIC_SYSCALLS
2144 ret = &pi->prstatus.pr_sysentry;
2145 #else /* DYNAMIC_SYSCALLS */
2147 static sysset_t *sysentry;
2151 sysentry = sysset_t_alloc (pi);
2153 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2155 if (pi->prstatus.pr_sysentry_offset == 0)
2157 gdb_premptysysset (sysentry);
2163 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysentry_offset,
2165 != (off_t) pi->prstatus.pr_sysentry_offset)
2167 size = sysset_t_size (pi);
2168 gdb_premptysysset (sysentry);
2169 rsize = read (pi->status_fd, sysentry, size);
2174 #endif /* DYNAMIC_SYSCALLS */
2175 #else /* !NEW_PROC_API */
2177 static sysset_t sysentry;
2179 if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysentry) >= 0)
2182 #endif /* NEW_PROC_API */
2184 memcpy (save, ret, sysset_t_size (pi));
2189 /* Returns the set of syscalls that are traced /debugged on exit.
2190 Will also copy the syscall set if SAVE is non-zero. */
2193 proc_get_traced_sysexit (procinfo *pi, sysset_t *save)
2195 sysset_t * ret = NULL;
2197 /* We should never have to apply this operation to any procinfo
2198 except the one for the main process. If that ever changes for
2199 any reason, then take out the following clause and replace it
2200 with one that makes sure the ctl_fd is open. */
2203 pi = find_procinfo_or_die (pi->pid, 0);
2206 if (!pi->status_valid)
2207 if (!proc_get_status (pi))
2210 #ifndef DYNAMIC_SYSCALLS
2211 ret = &pi->prstatus.pr_sysexit;
2212 #else /* DYNAMIC_SYSCALLS */
2214 static sysset_t *sysexit;
2218 sysexit = sysset_t_alloc (pi);
2220 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2222 if (pi->prstatus.pr_sysexit_offset == 0)
2224 gdb_premptysysset (sysexit);
2230 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysexit_offset,
2232 != (off_t) pi->prstatus.pr_sysexit_offset)
2234 size = sysset_t_size (pi);
2235 gdb_premptysysset (sysexit);
2236 rsize = read (pi->status_fd, sysexit, size);
2241 #endif /* DYNAMIC_SYSCALLS */
2244 static sysset_t sysexit;
2246 if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysexit) >= 0)
2251 memcpy (save, ret, sysset_t_size (pi));
2256 /* The current fault (if any) is cleared; the associated signal will
2257 not be sent to the process or LWP when it resumes. Returns
2258 non-zero for success, zero for failure. */
2261 proc_clear_current_fault (procinfo *pi)
2265 /* We should never have to apply this operation to any procinfo
2266 except the one for the main process. If that ever changes for
2267 any reason, then take out the following clause and replace it
2268 with one that makes sure the ctl_fd is open. */
2271 pi = find_procinfo_or_die (pi->pid, 0);
2275 procfs_ctl_t cmd = PCCFAULT;
2277 win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd));
2280 win = (ioctl (pi->ctl_fd, PIOCCFAULT, 0) >= 0);
2286 /* Set the "current signal" that will be delivered next to the
2287 process. NOTE: semantics are different from those of KILL. This
2288 signal will be delivered to the process or LWP immediately when it
2289 is resumed (even if the signal is held/blocked); it will NOT
2290 immediately cause another event of interest, and will NOT first
2291 trap back to the debugger. Returns non-zero for success, zero for
2295 proc_set_current_signal (procinfo *pi, int signo)
2300 /* Use char array to avoid alignment issues. */
2301 char sinfo[sizeof (gdb_siginfo_t)];
2303 gdb_siginfo_t mysinfo;
2305 struct target_waitstatus wait_status;
2307 /* We should never have to apply this operation to any procinfo
2308 except the one for the main process. If that ever changes for
2309 any reason, then take out the following clause and replace it
2310 with one that makes sure the ctl_fd is open. */
2313 pi = find_procinfo_or_die (pi->pid, 0);
2315 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2316 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2317 receives a PIOCSSIG with a signal identical to the current
2318 signal, it messes up the current signal. Work around the kernel
2321 signo == proc_cursig (pi))
2322 return 1; /* I assume this is a success? */
2325 /* The pointer is just a type alias. */
2326 get_last_target_status (&wait_ptid, &wait_status);
2327 if (ptid_equal (wait_ptid, inferior_ptid)
2328 && wait_status.kind == TARGET_WAITKIND_STOPPED
2329 && wait_status.value.sig == target_signal_from_host (signo)
2330 && proc_get_status (pi)
2332 && pi->prstatus.pr_lwp.pr_info.si_signo == signo
2334 && pi->prstatus.pr_info.si_signo == signo
2337 /* Use the siginfo associated with the signal being
2340 memcpy (arg.sinfo, &pi->prstatus.pr_lwp.pr_info, sizeof (gdb_siginfo_t));
2342 memcpy (arg.sinfo, &pi->prstatus.pr_info, sizeof (gdb_siginfo_t));
2346 mysinfo.si_signo = signo;
2347 mysinfo.si_code = 0;
2348 mysinfo.si_pid = getpid (); /* ?why? */
2349 mysinfo.si_uid = getuid (); /* ?why? */
2350 memcpy (arg.sinfo, &mysinfo, sizeof (gdb_siginfo_t));
2355 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2357 win = (ioctl (pi->ctl_fd, PIOCSSIG, (void *) &arg.sinfo) >= 0);
2363 /* The current signal (if any) is cleared, and is not sent to the
2364 process or LWP when it resumes. Returns non-zero for success, zero
2368 proc_clear_current_signal (procinfo *pi)
2372 /* We should never have to apply this operation to any procinfo
2373 except the one for the main process. If that ever changes for
2374 any reason, then take out the following clause and replace it
2375 with one that makes sure the ctl_fd is open. */
2378 pi = find_procinfo_or_die (pi->pid, 0);
2384 /* Use char array to avoid alignment issues. */
2385 char sinfo[sizeof (gdb_siginfo_t)];
2387 gdb_siginfo_t mysinfo;
2390 /* The pointer is just a type alias. */
2391 mysinfo.si_signo = 0;
2392 mysinfo.si_code = 0;
2393 mysinfo.si_errno = 0;
2394 mysinfo.si_pid = getpid (); /* ?why? */
2395 mysinfo.si_uid = getuid (); /* ?why? */
2396 memcpy (arg.sinfo, &mysinfo, sizeof (gdb_siginfo_t));
2398 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2401 win = (ioctl (pi->ctl_fd, PIOCSSIG, 0) >= 0);
2407 /* Return the general-purpose registers for the process or LWP
2408 corresponding to PI. Upon failure, return NULL. */
2411 proc_get_gregs (procinfo *pi)
2413 if (!pi->status_valid || !pi->gregs_valid)
2414 if (!proc_get_status (pi))
2417 /* OK, sorry about the ifdef's. There's three cases instead of two,
2418 because in this case Unixware and Solaris/RW differ. */
2421 # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */
2422 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs;
2424 return &pi->prstatus.pr_lwp.pr_reg;
2427 return &pi->prstatus.pr_reg;
2431 /* Return the general-purpose registers for the process or LWP
2432 corresponding to PI. Upon failure, return NULL. */
2435 proc_get_fpregs (procinfo *pi)
2438 if (!pi->status_valid || !pi->fpregs_valid)
2439 if (!proc_get_status (pi))
2442 # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */
2443 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs;
2445 return &pi->prstatus.pr_lwp.pr_fpreg;
2448 #else /* not NEW_PROC_API */
2449 if (pi->fpregs_valid)
2450 return &pi->fpregset; /* Already got 'em. */
2453 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2462 tid_t pr_error_thread;
2463 tfpregset_t thread_1;
2466 thread_fpregs.pr_count = 1;
2467 thread_fpregs.thread_1.tid = pi->tid;
2470 && ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2472 pi->fpregs_valid = 1;
2473 return &pi->fpregset; /* Got 'em now! */
2475 else if (pi->tid != 0
2476 && ioctl (pi->ctl_fd, PIOCTGFPREG, &thread_fpregs) >= 0)
2478 memcpy (&pi->fpregset, &thread_fpregs.thread_1.pr_fpregs,
2479 sizeof (pi->fpregset));
2480 pi->fpregs_valid = 1;
2481 return &pi->fpregset; /* Got 'em now! */
2488 if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2490 pi->fpregs_valid = 1;
2491 return &pi->fpregset; /* Got 'em now! */
2500 #endif /* NEW_PROC_API */
2503 /* Write the general-purpose registers back to the process or LWP
2504 corresponding to PI. Return non-zero for success, zero for
2508 proc_set_gregs (procinfo *pi)
2510 gdb_gregset_t *gregs;
2513 gregs = proc_get_gregs (pi);
2515 return 0; /* proc_get_regs has already warned. */
2517 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2526 /* Use char array to avoid alignment issues. */
2527 char gregs[sizeof (gdb_gregset_t)];
2531 memcpy (&arg.gregs, gregs, sizeof (arg.gregs));
2532 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2534 win = (ioctl (pi->ctl_fd, PIOCSREG, gregs) >= 0);
2538 /* Policy: writing the registers invalidates our cache. */
2539 pi->gregs_valid = 0;
2543 /* Write the floating-pointer registers back to the process or LWP
2544 corresponding to PI. Return non-zero for success, zero for
2548 proc_set_fpregs (procinfo *pi)
2550 gdb_fpregset_t *fpregs;
2553 fpregs = proc_get_fpregs (pi);
2555 return 0; /* proc_get_fpregs has already warned. */
2557 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2566 /* Use char array to avoid alignment issues. */
2567 char fpregs[sizeof (gdb_fpregset_t)];
2571 memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs));
2572 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2576 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2581 tid_t pr_error_thread;
2582 tfpregset_t thread_1;
2585 thread_fpregs.pr_count = 1;
2586 thread_fpregs.thread_1.tid = pi->tid;
2587 memcpy (&thread_fpregs.thread_1.pr_fpregs, fpregs,
2589 win = (ioctl (pi->ctl_fd, PIOCTSFPREG, &thread_fpregs) >= 0);
2592 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2594 #endif /* NEW_PROC_API */
2597 /* Policy: writing the registers invalidates our cache. */
2598 pi->fpregs_valid = 0;
2602 /* Send a signal to the proc or lwp with the semantics of "kill()".
2603 Returns non-zero for success, zero for failure. */
2606 proc_kill (procinfo *pi, int signo)
2610 /* We might conceivably apply this operation to an LWP, and the
2611 LWP's ctl file descriptor might not be open. */
2613 if (pi->ctl_fd == 0 &&
2614 open_procinfo_files (pi, FD_CTL) == 0)
2621 procfs_ctl_t cmd[2];
2625 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
2626 #else /* ioctl method */
2627 /* FIXME: do I need the Alpha OSF fixups present in
2628 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2629 win = (ioctl (pi->ctl_fd, PIOCKILL, &signo) >= 0);
2636 /* Find the pid of the process that started this one. Returns the
2637 parent process pid, or zero. */
2640 proc_parent_pid (procinfo *pi)
2642 /* We should never have to apply this operation to any procinfo
2643 except the one for the main process. If that ever changes for
2644 any reason, then take out the following clause and replace it
2645 with one that makes sure the ctl_fd is open. */
2648 pi = find_procinfo_or_die (pi->pid, 0);
2650 if (!pi->status_valid)
2651 if (!proc_get_status (pi))
2654 return pi->prstatus.pr_ppid;
2657 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2658 (a.k.a void pointer)! */
2660 #if (defined (PCWATCH) || defined (PIOCSWATCH)) \
2661 && !(defined (PIOCOPENLWP) || defined (UNIXWARE))
2663 procfs_address_to_host_pointer (CORE_ADDR addr)
2665 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
2668 gdb_assert (sizeof (ptr) == TYPE_LENGTH (ptr_type));
2669 gdbarch_address_to_pointer (target_gdbarch, ptr_type,
2670 (gdb_byte *) &ptr, addr);
2676 proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags)
2678 #if !defined (PCWATCH) && !defined (PIOCSWATCH)
2679 /* If neither or these is defined, we can't support watchpoints.
2680 This just avoids possibly failing to compile the below on such
2684 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2685 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2690 char watch[sizeof (prwatch_t)];
2694 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2695 convert a target address into something that can be stored in a
2696 native data structure. */
2697 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2698 pwatch.pr_vaddr = (uintptr_t) procfs_address_to_host_pointer (addr);
2700 pwatch.pr_vaddr = (caddr_t) procfs_address_to_host_pointer (addr);
2702 pwatch.pr_size = len;
2703 pwatch.pr_wflags = wflags;
2704 #if defined(NEW_PROC_API) && defined (PCWATCH)
2706 memcpy (arg.watch, &pwatch, sizeof (prwatch_t));
2707 return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg));
2709 #if defined (PIOCSWATCH)
2710 return (ioctl (pi->ctl_fd, PIOCSWATCH, &pwatch) >= 0);
2712 return 0; /* Fail */
2719 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
2721 #include <sys/sysi86.h>
2723 /* The KEY is actually the value of the lower 16 bits of the GS
2724 register for the LWP that we're interested in. Returns the
2725 matching ssh struct (LDT entry). */
2728 proc_get_LDT_entry (procinfo *pi, int key)
2730 static struct ssd *ldt_entry = NULL;
2732 char pathname[MAX_PROC_NAME_SIZE];
2733 struct cleanup *old_chain = NULL;
2736 /* Allocate space for one LDT entry.
2737 This alloc must persist, because we return a pointer to it. */
2738 if (ldt_entry == NULL)
2739 ldt_entry = (struct ssd *) xmalloc (sizeof (struct ssd));
2741 /* Open the file descriptor for the LDT table. */
2742 sprintf (pathname, "/proc/%d/ldt", pi->pid);
2743 if ((fd = open_with_retry (pathname, O_RDONLY)) < 0)
2745 proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__);
2748 /* Make sure it gets closed again! */
2749 old_chain = make_cleanup_close (fd);
2751 /* Now 'read' thru the table, find a match and return it. */
2752 while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd))
2754 if (ldt_entry->sel == 0 &&
2755 ldt_entry->bo == 0 &&
2756 ldt_entry->acc1 == 0 &&
2757 ldt_entry->acc2 == 0)
2758 break; /* end of table */
2759 /* If key matches, return this entry. */
2760 if (ldt_entry->sel == key)
2763 /* Loop ended, match not found. */
2767 static int nalloc = 0;
2769 /* Get the number of LDT entries. */
2770 if (ioctl (pi->ctl_fd, PIOCNLDT, &nldt) < 0)
2772 proc_warn (pi, "proc_get_LDT_entry (PIOCNLDT)", __LINE__);
2776 /* Allocate space for the number of LDT entries. */
2777 /* This alloc has to persist, 'cause we return a pointer to it. */
2780 ldt_entry = (struct ssd *)
2781 xrealloc (ldt_entry, (nldt + 1) * sizeof (struct ssd));
2785 /* Read the whole table in one gulp. */
2786 if (ioctl (pi->ctl_fd, PIOCLDT, ldt_entry) < 0)
2788 proc_warn (pi, "proc_get_LDT_entry (PIOCLDT)", __LINE__);
2792 /* Search the table and return the (first) entry matching 'key'. */
2793 for (i = 0; i < nldt; i++)
2794 if (ldt_entry[i].sel == key)
2795 return &ldt_entry[i];
2797 /* Loop ended, match not found. */
2802 /* Returns the pointer to the LDT entry of PTID. */
2805 procfs_find_LDT_entry (ptid_t ptid)
2807 gdb_gregset_t *gregs;
2811 /* Find procinfo for the lwp. */
2812 if ((pi = find_procinfo (PIDGET (ptid), TIDGET (ptid))) == NULL)
2814 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
2815 PIDGET (ptid), TIDGET (ptid));
2818 /* get its general registers. */
2819 if ((gregs = proc_get_gregs (pi)) == NULL)
2821 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
2822 PIDGET (ptid), TIDGET (ptid));
2825 /* Now extract the GS register's lower 16 bits. */
2826 key = (*gregs)[GS] & 0xffff;
2828 /* Find the matching entry and return it. */
2829 return proc_get_LDT_entry (pi, key);
2834 /* =============== END, non-thread part of /proc "MODULE" =============== */
2836 /* =================== Thread "MODULE" =================== */
2838 /* NOTE: you'll see more ifdefs and duplication of functions here,
2839 since there is a different way to do threads on every OS. */
2841 /* Returns the number of threads for the process. */
2843 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2846 proc_get_nthreads (procinfo *pi)
2850 if (ioctl (pi->ctl_fd, PIOCNTHR, &nthreads) < 0)
2851 proc_warn (pi, "procfs: PIOCNTHR failed", __LINE__);
2857 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2858 /* Solaris and Unixware version */
2860 proc_get_nthreads (procinfo *pi)
2862 if (!pi->status_valid)
2863 if (!proc_get_status (pi))
2866 /* NEW_PROC_API: only works for the process procinfo, because the
2867 LWP procinfos do not get prstatus filled in. */
2869 if (pi->tid != 0) /* find the parent process procinfo */
2870 pi = find_procinfo_or_die (pi->pid, 0);
2872 return pi->prstatus.pr_nlwp;
2876 /* Default version */
2878 proc_get_nthreads (procinfo *pi)
2887 Return the ID of the thread that had an event of interest.
2888 (ie. the one that hit a breakpoint or other traced event). All
2889 other things being equal, this should be the ID of a thread that is
2890 currently executing. */
2892 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2893 /* Solaris and Unixware version */
2895 proc_get_current_thread (procinfo *pi)
2897 /* Note: this should be applied to the root procinfo for the
2898 process, not to the procinfo for an LWP. If applied to the
2899 procinfo for an LWP, it will simply return that LWP's ID. In
2900 that case, find the parent process procinfo. */
2903 pi = find_procinfo_or_die (pi->pid, 0);
2905 if (!pi->status_valid)
2906 if (!proc_get_status (pi))
2910 return pi->prstatus.pr_lwp.pr_lwpid;
2912 return pi->prstatus.pr_who;
2917 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2920 proc_get_current_thread (procinfo *pi)
2922 #if 0 /* FIXME: not ready for prime time? */
2923 return pi->prstatus.pr_tid;
2930 /* Default version */
2932 proc_get_current_thread (procinfo *pi)
2940 /* Discover the IDs of all the threads within the process, and create
2941 a procinfo for each of them (chained to the parent). This
2942 unfortunately requires a different method on every OS. Returns
2943 non-zero for success, zero for failure. */
2946 proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore)
2948 if (thread && parent) /* sanity */
2950 thread->status_valid = 0;
2951 if (!proc_get_status (thread))
2952 destroy_one_procinfo (&parent->thread_list, thread);
2954 return 0; /* keep iterating */
2957 #if defined (PIOCLSTATUS)
2958 /* Solaris 2.5 (ioctl) version */
2960 proc_update_threads (procinfo *pi)
2962 gdb_prstatus_t *prstatus;
2963 struct cleanup *old_chain = NULL;
2967 /* We should never have to apply this operation to any procinfo
2968 except the one for the main process. If that ever changes for
2969 any reason, then take out the following clause and replace it
2970 with one that makes sure the ctl_fd is open. */
2973 pi = find_procinfo_or_die (pi->pid, 0);
2975 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
2977 if ((nlwp = proc_get_nthreads (pi)) <= 1)
2978 return 1; /* Process is not multi-threaded; nothing to do. */
2980 prstatus = xmalloc (sizeof (gdb_prstatus_t) * (nlwp + 1));
2982 old_chain = make_cleanup (xfree, prstatus);
2983 if (ioctl (pi->ctl_fd, PIOCLSTATUS, prstatus) < 0)
2984 proc_error (pi, "update_threads (PIOCLSTATUS)", __LINE__);
2986 /* Skip element zero, which represents the process as a whole. */
2987 for (i = 1; i < nlwp + 1; i++)
2989 if ((thread = create_procinfo (pi->pid, prstatus[i].pr_who)) == NULL)
2990 proc_error (pi, "update_threads, create_procinfo", __LINE__);
2992 memcpy (&thread->prstatus, &prstatus[i], sizeof (*prstatus));
2993 thread->status_valid = 1;
2995 pi->threads_valid = 1;
2996 do_cleanups (old_chain);
3001 /* Unixware and Solaris 6 (and later) version */
3003 do_closedir_cleanup (void *dir)
3009 proc_update_threads (procinfo *pi)
3011 char pathname[MAX_PROC_NAME_SIZE + 16];
3012 struct dirent *direntry;
3013 struct cleanup *old_chain = NULL;
3018 /* We should never have to apply this operation to any procinfo
3019 except the one for the main process. If that ever changes for
3020 any reason, then take out the following clause and replace it
3021 with one that makes sure the ctl_fd is open. */
3024 pi = find_procinfo_or_die (pi->pid, 0);
3026 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3030 Note: this brute-force method is the only way I know of to
3031 accomplish this task on Unixware. This method will also work on
3032 Solaris 2.6 and 2.7. There is a much simpler and more elegant
3033 way to do this on Solaris, but the margins of this manuscript are
3034 too small to write it here... ;-) */
3036 strcpy (pathname, pi->pathname);
3037 strcat (pathname, "/lwp");
3038 if ((dirp = opendir (pathname)) == NULL)
3039 proc_error (pi, "update_threads, opendir", __LINE__);
3041 old_chain = make_cleanup (do_closedir_cleanup, dirp);
3042 while ((direntry = readdir (dirp)) != NULL)
3043 if (direntry->d_name[0] != '.') /* skip '.' and '..' */
3045 lwpid = atoi (&direntry->d_name[0]);
3046 if ((thread = create_procinfo (pi->pid, lwpid)) == NULL)
3047 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3049 pi->threads_valid = 1;
3050 do_cleanups (old_chain);
3057 proc_update_threads (procinfo *pi)
3062 /* We should never have to apply this operation to any procinfo
3063 except the one for the main process. If that ever changes for
3064 any reason, then take out the following clause and replace it
3065 with one that makes sure the ctl_fd is open. */
3068 pi = find_procinfo_or_die (pi->pid, 0);
3070 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3072 nthreads = proc_get_nthreads (pi);
3074 return 0; /* nothing to do for 1 or fewer threads */
3076 threads = xmalloc (nthreads * sizeof (tid_t));
3078 if (ioctl (pi->ctl_fd, PIOCTLIST, threads) < 0)
3079 proc_error (pi, "procfs: update_threads (PIOCTLIST)", __LINE__);
3081 for (i = 0; i < nthreads; i++)
3083 if (!find_procinfo (pi->pid, threads[i]))
3084 if (!create_procinfo (pi->pid, threads[i]))
3085 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3087 pi->threads_valid = 1;
3091 /* Default version */
3093 proc_update_threads (procinfo *pi)
3097 #endif /* OSF PIOCTLIST */
3098 #endif /* NEW_PROC_API */
3099 #endif /* SOL 2.5 PIOCLSTATUS */
3101 /* Given a pointer to a function, call that function once for each lwp
3102 in the procinfo list, until the function returns non-zero, in which
3103 event return the value returned by the function.
3105 Note: this function does NOT call update_threads. If you want to
3106 discover new threads first, you must call that function explicitly.
3107 This function just makes a quick pass over the currently-known
3110 PI is the parent process procinfo. FUNC is the per-thread
3111 function. PTR is an opaque parameter for function. Returns the
3112 first non-zero return value from the callee, or zero. */
3115 proc_iterate_over_threads (procinfo *pi,
3116 int (*func) (procinfo *, procinfo *, void *),
3119 procinfo *thread, *next;
3122 /* We should never have to apply this operation to any procinfo
3123 except the one for the main process. If that ever changes for
3124 any reason, then take out the following clause and replace it
3125 with one that makes sure the ctl_fd is open. */
3128 pi = find_procinfo_or_die (pi->pid, 0);
3130 for (thread = pi->thread_list; thread != NULL; thread = next)
3132 next = thread->next; /* in case thread is destroyed */
3133 if ((retval = (*func) (pi, thread, ptr)) != 0)
3140 /* =================== END, Thread "MODULE" =================== */
3142 /* =================== END, /proc "MODULE" =================== */
3144 /* =================== GDB "MODULE" =================== */
3146 /* Here are all of the gdb target vector functions and their
3149 static ptid_t do_attach (ptid_t ptid);
3150 static void do_detach (int signo);
3151 static int register_gdb_signals (procinfo *, gdb_sigset_t *);
3152 static void proc_trace_syscalls_1 (procinfo *pi, int syscallnum,
3153 int entry_or_exit, int mode, int from_tty);
3155 /* On mips-irix, we need to insert a breakpoint at __dbx_link during
3156 the startup phase. The following two variables are used to record
3157 the address of the breakpoint, and the code that was replaced by
3159 static int dbx_link_bpt_addr = 0;
3160 static void *dbx_link_bpt;
3162 /* Sets up the inferior to be debugged. Registers to trace signals,
3163 hardware faults, and syscalls. Note: does not set RLC flag: caller
3164 may want to customize that. Returns zero for success (note!
3165 unlike most functions in this module); on failure, returns the LINE
3166 NUMBER where it failed! */
3169 procfs_debug_inferior (procinfo *pi)
3171 fltset_t traced_faults;
3172 gdb_sigset_t traced_signals;
3173 sysset_t *traced_syscall_entries;
3174 sysset_t *traced_syscall_exits;
3177 #ifdef PROCFS_DONT_TRACE_FAULTS
3178 /* On some systems (OSF), we don't trace hardware faults.
3179 Apparently it's enough that we catch them as signals.
3180 Wonder why we don't just do that in general? */
3181 premptyset (&traced_faults); /* don't trace faults. */
3183 /* Register to trace hardware faults in the child. */
3184 prfillset (&traced_faults); /* trace all faults... */
3185 gdb_prdelset (&traced_faults, FLTPAGE); /* except page fault. */
3187 if (!proc_set_traced_faults (pi, &traced_faults))
3190 /* Register to trace selected signals in the child. */
3191 premptyset (&traced_signals);
3192 if (!register_gdb_signals (pi, &traced_signals))
3196 /* Register to trace the 'exit' system call (on entry). */
3197 traced_syscall_entries = sysset_t_alloc (pi);
3198 gdb_premptysysset (traced_syscall_entries);
3200 gdb_praddsysset (traced_syscall_entries, SYS_exit);
3203 gdb_praddsysset (traced_syscall_entries, SYS_lwpexit); /* And _lwp_exit... */
3206 gdb_praddsysset (traced_syscall_entries, SYS_lwp_exit);
3208 #ifdef DYNAMIC_SYSCALLS
3210 int callnum = find_syscall (pi, "_exit");
3213 gdb_praddsysset (traced_syscall_entries, callnum);
3217 status = proc_set_traced_sysentry (pi, traced_syscall_entries);
3218 xfree (traced_syscall_entries);
3222 #ifdef PRFS_STOPEXEC /* defined on OSF */
3223 /* OSF method for tracing exec syscalls. Quoting:
3224 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3225 exits from exec system calls because of the user level loader. */
3226 /* FIXME: make nice and maybe move into an access function. */
3230 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
3233 prfs_flags |= PRFS_STOPEXEC;
3235 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
3238 #else /* not PRFS_STOPEXEC */
3239 /* Everyone else's (except OSF) method for tracing exec syscalls */
3241 Not all systems with /proc have all the exec* syscalls with the same
3242 names. On the SGI, for example, there is no SYS_exec, but there
3243 *is* a SYS_execv. So, we try to account for that. */
3245 traced_syscall_exits = sysset_t_alloc (pi);
3246 gdb_premptysysset (traced_syscall_exits);
3248 gdb_praddsysset (traced_syscall_exits, SYS_exec);
3251 gdb_praddsysset (traced_syscall_exits, SYS_execve);
3254 gdb_praddsysset (traced_syscall_exits, SYS_execv);
3257 #ifdef SYS_lwpcreate
3258 gdb_praddsysset (traced_syscall_exits, SYS_lwpcreate);
3259 gdb_praddsysset (traced_syscall_exits, SYS_lwpexit);
3262 #ifdef SYS_lwp_create /* FIXME: once only, please */
3263 gdb_praddsysset (traced_syscall_exits, SYS_lwp_create);
3264 gdb_praddsysset (traced_syscall_exits, SYS_lwp_exit);
3267 #ifdef DYNAMIC_SYSCALLS
3269 int callnum = find_syscall (pi, "execve");
3272 gdb_praddsysset (traced_syscall_exits, callnum);
3273 callnum = find_syscall (pi, "ra_execve");
3275 gdb_praddsysset (traced_syscall_exits, callnum);
3279 status = proc_set_traced_sysexit (pi, traced_syscall_exits);
3280 xfree (traced_syscall_exits);
3284 #endif /* PRFS_STOPEXEC */
3289 procfs_attach (struct target_ops *ops, char *args, int from_tty)
3294 pid = parse_pid_to_attach (args);
3296 if (pid == getpid ())
3297 error (_("Attaching GDB to itself is not a good idea..."));
3301 exec_file = get_exec_file (0);
3304 printf_filtered (_("Attaching to program `%s', %s\n"),
3305 exec_file, target_pid_to_str (pid_to_ptid (pid)));
3307 printf_filtered (_("Attaching to %s\n"),
3308 target_pid_to_str (pid_to_ptid (pid)));
3312 inferior_ptid = do_attach (pid_to_ptid (pid));
3317 procfs_detach (struct target_ops *ops, char *args, int from_tty)
3320 int pid = PIDGET (inferior_ptid);
3329 exec_file = get_exec_file (0);
3330 if (exec_file == NULL)
3333 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file,
3334 target_pid_to_str (pid_to_ptid (pid)));
3335 gdb_flush (gdb_stdout);
3340 inferior_ptid = null_ptid;
3341 detach_inferior (pid);
3342 unpush_target (ops);
3346 do_attach (ptid_t ptid)
3349 struct inferior *inf;
3353 if ((pi = create_procinfo (PIDGET (ptid), 0)) == NULL)
3354 perror (_("procfs: out of memory in 'attach'"));
3356 if (!open_procinfo_files (pi, FD_CTL))
3358 fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__);
3359 sprintf (errmsg, "do_attach: couldn't open /proc file for process %d",
3361 dead_procinfo (pi, errmsg, NOKILL);
3364 /* Stop the process (if it isn't already stopped). */
3365 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
3367 pi->was_stopped = 1;
3368 proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
3372 pi->was_stopped = 0;
3373 /* Set the process to run again when we close it. */
3374 if (!proc_set_run_on_last_close (pi))
3375 dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
3377 /* Now stop the process. */
3378 if (!proc_stop_process (pi))
3379 dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
3380 pi->ignore_next_sigstop = 1;
3382 /* Save some of the /proc state to be restored if we detach. */
3383 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
3384 dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
3385 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
3386 dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
3387 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
3388 dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
3390 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
3391 dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
3393 if (!proc_get_held_signals (pi, &pi->saved_sighold))
3394 dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
3396 if ((fail = procfs_debug_inferior (pi)) != 0)
3397 dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
3399 inf = current_inferior ();
3400 inferior_appeared (inf, pi->pid);
3401 /* Let GDB know that the inferior was attached. */
3402 inf->attach_flag = 1;
3404 /* Create a procinfo for the current lwp. */
3405 lwpid = proc_get_current_thread (pi);
3406 create_procinfo (pi->pid, lwpid);
3408 /* Add it to gdb's thread list. */
3409 ptid = MERGEPID (pi->pid, lwpid);
3416 do_detach (int signo)
3420 /* Find procinfo for the main process */
3421 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); /* FIXME: threads */
3423 if (!proc_set_current_signal (pi, signo))
3424 proc_warn (pi, "do_detach, set_current_signal", __LINE__);
3426 if (!proc_set_traced_signals (pi, &pi->saved_sigset))
3427 proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
3429 if (!proc_set_traced_faults (pi, &pi->saved_fltset))
3430 proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
3432 if (!proc_set_traced_sysentry (pi, pi->saved_entryset))
3433 proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
3435 if (!proc_set_traced_sysexit (pi, pi->saved_exitset))
3436 proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
3438 if (!proc_set_held_signals (pi, &pi->saved_sighold))
3439 proc_warn (pi, "do_detach, set_held_signals", __LINE__);
3441 if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)))
3442 if (signo || !(pi->was_stopped) ||
3443 query (_("Was stopped when attached, make it runnable again? ")))
3445 /* Clear any pending signal. */
3446 if (!proc_clear_current_fault (pi))
3447 proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
3449 if (signo == 0 && !proc_clear_current_signal (pi))
3450 proc_warn (pi, "do_detach, clear_current_signal", __LINE__);
3452 if (!proc_set_run_on_last_close (pi))
3453 proc_warn (pi, "do_detach, set_rlc", __LINE__);
3456 destroy_procinfo (pi);
3459 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
3462 ??? Is the following note still relevant? We can't get individual
3463 registers with the PT_GETREGS ptrace(2) request either, yet we
3464 don't bother with caching at all in that case.
3466 NOTE: Since the /proc interface cannot give us individual
3467 registers, we pay no attention to REGNUM, and just fetch them all.
3468 This results in the possibility that we will do unnecessarily many
3469 fetches, since we may be called repeatedly for individual
3470 registers. So we cache the results, and mark the cache invalid
3471 when the process is resumed. */
3474 procfs_fetch_registers (struct target_ops *ops,
3475 struct regcache *regcache, int regnum)
3477 gdb_gregset_t *gregs;
3479 int pid = PIDGET (inferior_ptid);
3480 int tid = TIDGET (inferior_ptid);
3481 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3483 pi = find_procinfo_or_die (pid, tid);
3486 error (_("procfs: fetch_registers failed to find procinfo for %s"),
3487 target_pid_to_str (inferior_ptid));
3489 gregs = proc_get_gregs (pi);
3491 proc_error (pi, "fetch_registers, get_gregs", __LINE__);
3493 supply_gregset (regcache, (const gdb_gregset_t *) gregs);
3495 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
3497 gdb_fpregset_t *fpregs;
3499 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
3500 || regnum == gdbarch_pc_regnum (gdbarch)
3501 || regnum == gdbarch_sp_regnum (gdbarch))
3502 return; /* Not a floating point register. */
3504 fpregs = proc_get_fpregs (pi);
3506 proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
3508 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregs);
3512 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
3513 this for all registers.
3515 NOTE: Since the /proc interface will not read individual registers,
3516 we will cache these requests until the process is resumed, and only
3517 then write them back to the inferior process.
3519 FIXME: is that a really bad idea? Have to think about cases where
3520 writing one register might affect the value of others, etc. */
3523 procfs_store_registers (struct target_ops *ops,
3524 struct regcache *regcache, int regnum)
3526 gdb_gregset_t *gregs;
3528 int pid = PIDGET (inferior_ptid);
3529 int tid = TIDGET (inferior_ptid);
3530 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3532 pi = find_procinfo_or_die (pid, tid);
3535 error (_("procfs: store_registers: failed to find procinfo for %s"),
3536 target_pid_to_str (inferior_ptid));
3538 gregs = proc_get_gregs (pi);
3540 proc_error (pi, "store_registers, get_gregs", __LINE__);
3542 fill_gregset (regcache, gregs, regnum);
3543 if (!proc_set_gregs (pi))
3544 proc_error (pi, "store_registers, set_gregs", __LINE__);
3546 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
3548 gdb_fpregset_t *fpregs;
3550 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
3551 || regnum == gdbarch_pc_regnum (gdbarch)
3552 || regnum == gdbarch_sp_regnum (gdbarch))
3553 return; /* Not a floating point register. */
3555 fpregs = proc_get_fpregs (pi);
3557 proc_error (pi, "store_registers, get_fpregs", __LINE__);
3559 fill_fpregset (regcache, fpregs, regnum);
3560 if (!proc_set_fpregs (pi))
3561 proc_error (pi, "store_registers, set_fpregs", __LINE__);
3566 syscall_is_lwp_exit (procinfo *pi, int scall)
3569 if (scall == SYS_lwp_exit)
3573 if (scall == SYS_lwpexit)
3580 syscall_is_exit (procinfo *pi, int scall)
3583 if (scall == SYS_exit)
3586 #ifdef DYNAMIC_SYSCALLS
3587 if (find_syscall (pi, "_exit") == scall)
3594 syscall_is_exec (procinfo *pi, int scall)
3597 if (scall == SYS_exec)
3601 if (scall == SYS_execv)
3605 if (scall == SYS_execve)
3608 #ifdef DYNAMIC_SYSCALLS
3609 if (find_syscall (pi, "_execve"))
3611 if (find_syscall (pi, "ra_execve"))
3618 syscall_is_lwp_create (procinfo *pi, int scall)
3620 #ifdef SYS_lwp_create
3621 if (scall == SYS_lwp_create)
3624 #ifdef SYS_lwpcreate
3625 if (scall == SYS_lwpcreate)
3631 /* Remove the breakpoint that we inserted in __dbx_link().
3632 Does nothing if the breakpoint hasn't been inserted or has already
3636 remove_dbx_link_breakpoint (void)
3638 if (dbx_link_bpt_addr == 0)
3641 if (deprecated_remove_raw_breakpoint (target_gdbarch, dbx_link_bpt) != 0)
3642 warning (_("Unable to remove __dbx_link breakpoint."));
3644 dbx_link_bpt_addr = 0;
3645 dbx_link_bpt = NULL;
3649 /* Return the address of the __dbx_link() function in the file
3650 refernced by ABFD by scanning its symbol table. Return 0 if
3651 the symbol was not found. */
3654 dbx_link_addr (bfd *abfd)
3656 long storage_needed;
3657 asymbol **symbol_table;
3658 long number_of_symbols;
3661 storage_needed = bfd_get_symtab_upper_bound (abfd);
3662 if (storage_needed <= 0)
3665 symbol_table = (asymbol **) xmalloc (storage_needed);
3666 make_cleanup (xfree, symbol_table);
3668 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
3670 for (i = 0; i < number_of_symbols; i++)
3672 asymbol *sym = symbol_table[i];
3674 if ((sym->flags & BSF_GLOBAL)
3675 && sym->name != NULL && strcmp (sym->name, "__dbx_link") == 0)
3676 return (sym->value + sym->section->vma);
3679 /* Symbol not found, return NULL. */
3683 /* Search the symbol table of the file referenced by FD for a symbol
3684 named __dbx_link(). If found, then insert a breakpoint at this location,
3685 and return nonzero. Return zero otherwise. */
3688 insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
3691 long storage_needed;
3694 abfd = bfd_fdopenr ("unamed", 0, fd);
3697 warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
3701 if (!bfd_check_format (abfd, bfd_object))
3703 /* Not the correct format, so we can not possibly find the dbx_link
3709 sym_addr = dbx_link_addr (abfd);
3712 /* Insert the breakpoint. */
3713 dbx_link_bpt_addr = sym_addr;
3714 dbx_link_bpt = deprecated_insert_raw_breakpoint (target_gdbarch, NULL,
3716 if (dbx_link_bpt == NULL)
3718 warning (_("Failed to insert dbx_link breakpoint."));
3730 /* Calls the supplied callback function once for each mapped address
3731 space in the process. The callback function receives an open file
3732 descriptor for the file corresponding to that mapped address space
3733 (if there is one), and the base address of the mapped space. Quit
3734 when the callback function returns a nonzero value, or at teh end
3735 of the mappings. Returns the first non-zero return value of the
3736 callback function, or zero. */
3739 solib_mappings_callback (struct prmap *map, int (*func) (int, CORE_ADDR),
3742 procinfo *pi = data;
3746 char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)];
3748 if (map->pr_vaddr == 0 && map->pr_size == 0)
3749 return -1; /* sanity */
3751 if (map->pr_mapname[0] == 0)
3753 fd = -1; /* no map file */
3757 sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname);
3758 /* Note: caller's responsibility to close this fd! */
3759 fd = open_with_retry (name, O_RDONLY);
3760 /* Note: we don't test the above call for failure;
3761 we just pass the FD on as given. Sometimes there is
3762 no file, so the open may return failure, but that's
3766 fd = ioctl (pi->ctl_fd, PIOCOPENM, &map->pr_vaddr);
3767 /* Note: we don't test the above call for failure;
3768 we just pass the FD on as given. Sometimes there is
3769 no file, so the ioctl may return failure, but that's
3772 return (*func) (fd, (CORE_ADDR) map->pr_vaddr);
3775 /* If the given memory region MAP contains a symbol named __dbx_link,
3776 insert a breakpoint at this location and return nonzero. Return
3780 insert_dbx_link_bpt_in_region (struct prmap *map,
3781 find_memory_region_ftype child_func,
3784 procinfo *pi = (procinfo *) data;
3786 /* We know the symbol we're looking for is in a text region, so
3787 only look for it if the region is a text one. */
3788 if (map->pr_mflags & MA_EXEC)
3789 return solib_mappings_callback (map, insert_dbx_link_bpt_in_file, pi);
3794 /* Search all memory regions for a symbol named __dbx_link. If found,
3795 insert a breakpoint at its location, and return nonzero. Return zero
3799 insert_dbx_link_breakpoint (procinfo *pi)
3801 return iterate_over_mappings (pi, NULL, pi, insert_dbx_link_bpt_in_region);
3805 /* Retrieve the next stop event from the child process. If child has
3806 not stopped yet, wait for it to stop. Translate /proc eventcodes
3807 (or possibly wait eventcodes) into gdb internal event codes.
3808 Returns the id of process (and possibly thread) that incurred the
3809 event. Event codes are returned through a pointer parameter. */
3812 procfs_wait (struct target_ops *ops,
3813 ptid_t ptid, struct target_waitstatus *status, int options)
3815 /* First cut: loosely based on original version 2.1 */
3819 ptid_t retval, temp_ptid;
3820 int why, what, flags;
3827 retval = pid_to_ptid (-1);
3829 /* Find procinfo for main process */
3830 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
3833 /* We must assume that the status is stale now... */
3834 pi->status_valid = 0;
3835 pi->gregs_valid = 0;
3836 pi->fpregs_valid = 0;
3838 #if 0 /* just try this out... */
3839 flags = proc_flags (pi);
3840 why = proc_why (pi);
3841 if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
3842 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
3844 /* If child is not stopped, wait for it to stop. */
3845 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) &&
3846 !proc_wait_for_stop (pi))
3848 /* wait_for_stop failed: has the child terminated? */
3849 if (errno == ENOENT)
3853 /* /proc file not found; presumably child has terminated. */
3854 wait_retval = wait (&wstat); /* "wait" for the child's exit */
3856 if (wait_retval != PIDGET (inferior_ptid)) /* wrong child? */
3857 error (_("procfs: couldn't stop "
3858 "process %d: wait returned %d."),
3859 PIDGET (inferior_ptid), wait_retval);
3860 /* FIXME: might I not just use waitpid?
3861 Or try find_procinfo to see if I know about this child? */
3862 retval = pid_to_ptid (wait_retval);
3864 else if (errno == EINTR)
3868 /* Unknown error from wait_for_stop. */
3869 proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
3874 /* This long block is reached if either:
3875 a) the child was already stopped, or
3876 b) we successfully waited for the child with wait_for_stop.
3877 This block will analyze the /proc status, and translate it
3878 into a waitstatus for GDB.
3880 If we actually had to call wait because the /proc file
3881 is gone (child terminated), then we skip this block,
3882 because we already have a waitstatus. */
3884 flags = proc_flags (pi);
3885 why = proc_why (pi);
3886 what = proc_what (pi);
3888 if (flags & (PR_STOPPED | PR_ISTOP))
3891 /* If it's running async (for single_thread control),
3892 set it back to normal again. */
3893 if (flags & PR_ASYNC)
3894 if (!proc_unset_async (pi))
3895 proc_error (pi, "target_wait, unset_async", __LINE__);
3899 proc_prettyprint_why (why, what, 1);
3901 /* The 'pid' we will return to GDB is composed of
3902 the process ID plus the lwp ID. */
3903 retval = MERGEPID (pi->pid, proc_get_current_thread (pi));
3907 wstat = (what << 8) | 0177;
3910 if (syscall_is_lwp_exit (pi, what))
3912 if (print_thread_events)
3913 printf_unfiltered (_("[%s exited]\n"),
3914 target_pid_to_str (retval));
3915 delete_thread (retval);
3916 status->kind = TARGET_WAITKIND_SPURIOUS;
3919 else if (syscall_is_exit (pi, what))
3921 struct inferior *inf;
3923 /* Handle SYS_exit call only */
3924 /* Stopped at entry to SYS_exit.
3925 Make it runnable, resume it, then use
3926 the wait system call to get its exit code.
3927 Proc_run_process always clears the current
3929 Then return its exit status. */
3930 pi->status_valid = 0;
3932 /* FIXME: what we should do is return
3933 TARGET_WAITKIND_SPURIOUS. */
3934 if (!proc_run_process (pi, 0, 0))
3935 proc_error (pi, "target_wait, run_process", __LINE__);
3937 inf = find_inferior_pid (pi->pid);
3938 if (inf->attach_flag)
3940 /* Don't call wait: simulate waiting for exit,
3941 return a "success" exit code. Bogus: what if
3942 it returns something else? */
3944 retval = inferior_ptid; /* ? ? ? */
3948 int temp = wait (&wstat);
3950 /* FIXME: shouldn't I make sure I get the right
3951 event from the right process? If (for
3952 instance) I have killed an earlier inferior
3953 process but failed to clean up after it
3954 somehow, I could get its termination event
3957 /* If wait returns -1, that's what we return to GDB. */
3959 retval = pid_to_ptid (temp);
3964 printf_filtered (_("procfs: trapped on entry to "));
3965 proc_prettyprint_syscall (proc_what (pi), 0);
3966 printf_filtered ("\n");
3969 long i, nsysargs, *sysargs;
3971 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
3972 (sysargs = proc_sysargs (pi)) != NULL)
3974 printf_filtered (_("%ld syscall arguments:\n"),
3976 for (i = 0; i < nsysargs; i++)
3977 printf_filtered ("#%ld: 0x%08lx\n",
3985 /* How to exit gracefully, returning "unknown event" */
3986 status->kind = TARGET_WAITKIND_SPURIOUS;
3987 return inferior_ptid;
3991 /* How to keep going without returning to wfi: */
3992 target_resume (ptid, 0, TARGET_SIGNAL_0);
3998 if (syscall_is_exec (pi, what))
4000 /* Hopefully this is our own "fork-child" execing
4001 the real child. Hoax this event into a trap, and
4002 GDB will see the child about to execute its start
4004 wstat = (SIGTRAP << 8) | 0177;
4007 else if (what == SYS_syssgi)
4009 /* see if we can break on dbx_link(). If yes, then
4010 we no longer need the SYS_syssgi notifications. */
4011 if (insert_dbx_link_breakpoint (pi))
4012 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT,
4015 /* This is an internal event and should be transparent
4016 to wfi, so resume the execution and wait again. See
4017 comment in procfs_init_inferior() for more details. */
4018 target_resume (ptid, 0, TARGET_SIGNAL_0);
4022 else if (syscall_is_lwp_create (pi, what))
4024 /* This syscall is somewhat like fork/exec. We
4025 will get the event twice: once for the parent
4026 LWP, and once for the child. We should already
4027 know about the parent LWP, but the child will
4028 be new to us. So, whenever we get this event,
4029 if it represents a new thread, simply add the
4030 thread to the list. */
4032 /* If not in procinfo list, add it. */
4033 temp_tid = proc_get_current_thread (pi);
4034 if (!find_procinfo (pi->pid, temp_tid))
4035 create_procinfo (pi->pid, temp_tid);
4037 temp_ptid = MERGEPID (pi->pid, temp_tid);
4038 /* If not in GDB's thread list, add it. */
4039 if (!in_thread_list (temp_ptid))
4040 add_thread (temp_ptid);
4042 /* Return to WFI, but tell it to immediately resume. */
4043 status->kind = TARGET_WAITKIND_SPURIOUS;
4044 return inferior_ptid;
4046 else 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;
4057 /* FIXME: Do we need to handle SYS_sproc,
4058 SYS_fork, or SYS_vfork here? The old procfs
4059 seemed to use this event to handle threads on
4060 older (non-LWP) systems, where I'm assuming
4061 that threads were actually separate processes.
4062 Irix, maybe? Anyway, low priority for now. */
4066 printf_filtered (_("procfs: trapped on exit from "));
4067 proc_prettyprint_syscall (proc_what (pi), 0);
4068 printf_filtered ("\n");
4071 long i, nsysargs, *sysargs;
4073 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4074 (sysargs = proc_sysargs (pi)) != NULL)
4076 printf_filtered (_("%ld syscall arguments:\n"),
4078 for (i = 0; i < nsysargs; i++)
4079 printf_filtered ("#%ld: 0x%08lx\n",
4084 status->kind = TARGET_WAITKIND_SPURIOUS;
4085 return inferior_ptid;
4090 wstat = (SIGSTOP << 8) | 0177;
4095 printf_filtered (_("Retry #%d:\n"), retry);
4096 pi->status_valid = 0;
4101 /* If not in procinfo list, add it. */
4102 temp_tid = proc_get_current_thread (pi);
4103 if (!find_procinfo (pi->pid, temp_tid))
4104 create_procinfo (pi->pid, temp_tid);
4106 /* If not in GDB's thread list, add it. */
4107 temp_ptid = MERGEPID (pi->pid, temp_tid);
4108 if (!in_thread_list (temp_ptid))
4109 add_thread (temp_ptid);
4111 status->kind = TARGET_WAITKIND_STOPPED;
4112 status->value.sig = 0;
4117 wstat = (what << 8) | 0177;
4123 wstat = (SIGTRAP << 8) | 0177;
4128 wstat = (SIGTRAP << 8) | 0177;
4131 /* FIXME: use si_signo where possible. */
4133 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4136 wstat = (SIGILL << 8) | 0177;
4139 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4142 /* If we hit our __dbx_link() internal breakpoint,
4143 then remove it. See comments in procfs_init_inferior()
4144 for more details. */
4145 if (dbx_link_bpt_addr != 0
4146 && dbx_link_bpt_addr
4147 == regcache_read_pc (get_current_regcache ()))
4148 remove_dbx_link_breakpoint ();
4150 wstat = (SIGTRAP << 8) | 0177;
4154 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4157 wstat = (SIGSEGV << 8) | 0177;
4161 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4164 wstat = (SIGFPE << 8) | 0177;
4166 case FLTPAGE: /* Recoverable page fault */
4167 default: /* FIXME: use si_signo if possible for fault */
4168 retval = pid_to_ptid (-1);
4169 printf_filtered ("procfs:%d -- ", __LINE__);
4170 printf_filtered (_("child stopped for unknown reason:\n"));
4171 proc_prettyprint_why (why, what, 1);
4172 error (_("... giving up..."));
4175 break; /* case PR_FAULTED: */
4176 default: /* switch (why) unmatched */
4177 printf_filtered ("procfs:%d -- ", __LINE__);
4178 printf_filtered (_("child stopped for unknown reason:\n"));
4179 proc_prettyprint_why (why, what, 1);
4180 error (_("... giving up..."));
4183 /* Got this far without error: If retval isn't in the
4184 threads database, add it. */
4185 if (PIDGET (retval) > 0 &&
4186 !ptid_equal (retval, inferior_ptid) &&
4187 !in_thread_list (retval))
4189 /* We have a new thread. We need to add it both to
4190 GDB's list and to our own. If we don't create a
4191 procinfo, resume may be unhappy later. */
4192 add_thread (retval);
4193 if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL)
4194 create_procinfo (PIDGET (retval), TIDGET (retval));
4197 else /* flags do not indicate STOPPED */
4199 /* surely this can't happen... */
4200 printf_filtered ("procfs:%d -- process not stopped.\n",
4202 proc_prettyprint_flags (flags, 1);
4203 error (_("procfs: ...giving up..."));
4208 store_waitstatus (status, wstat);
4214 /* Perform a partial transfer to/from the specified object. For
4215 memory transfers, fall back to the old memory xfer functions. */
4218 procfs_xfer_partial (struct target_ops *ops, enum target_object object,
4219 const char *annex, gdb_byte *readbuf,
4220 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4224 case TARGET_OBJECT_MEMORY:
4226 return (*ops->deprecated_xfer_memory) (offset, readbuf,
4227 len, 0/*read*/, NULL, ops);
4229 return (*ops->deprecated_xfer_memory) (offset, (gdb_byte *) writebuf,
4230 len, 1/*write*/, NULL, ops);
4234 case TARGET_OBJECT_AUXV:
4235 return memory_xfer_auxv (ops, object, annex, readbuf, writebuf,
4240 if (ops->beneath != NULL)
4241 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
4242 readbuf, writebuf, offset, len);
4248 /* Transfer LEN bytes between GDB address MYADDR and target address
4249 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4250 otherwise transfer them from the target. TARGET is unused.
4252 The return value is 0 if an error occurred or no bytes were
4253 transferred. Otherwise, it will be a positive value which
4254 indicates the number of bytes transferred between gdb and the
4255 target. (Note that the interface also makes provisions for
4256 negative values, but this capability isn't implemented here.) */
4259 procfs_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int dowrite,
4260 struct mem_attrib *attrib, struct target_ops *target)
4265 /* Find procinfo for main process */
4266 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4267 if (pi->as_fd == 0 &&
4268 open_procinfo_files (pi, FD_AS) == 0)
4270 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
4274 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
4279 PROCFS_NOTE ("write memory:\n");
4281 PROCFS_NOTE ("write memory:\n");
4283 nbytes = write (pi->as_fd, myaddr, len);
4287 PROCFS_NOTE ("read memory:\n");
4288 nbytes = read (pi->as_fd, myaddr, len);
4298 /* Called by target_resume before making child runnable. Mark cached
4299 registers and status's invalid. If there are "dirty" caches that
4300 need to be written back to the child process, do that.
4302 File descriptors are also cached. As they are a limited resource,
4303 we cannot hold onto them indefinitely. However, as they are
4304 expensive to open, we don't want to throw them away
4305 indescriminately either. As a compromise, we will keep the file
4306 descriptors for the parent process, but discard any file
4307 descriptors we may have accumulated for the threads.
4309 As this function is called by iterate_over_threads, it always
4310 returns zero (so that iterate_over_threads will keep
4314 invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
4316 /* About to run the child; invalidate caches and do any other
4320 if (pi->gregs_dirty)
4321 if (parent == NULL ||
4322 proc_get_current_thread (parent) != pi->tid)
4323 if (!proc_set_gregs (pi)) /* flush gregs cache */
4324 proc_warn (pi, "target_resume, set_gregs",
4326 if (gdbarch_fp0_regnum (target_gdbarch) >= 0)
4327 if (pi->fpregs_dirty)
4328 if (parent == NULL ||
4329 proc_get_current_thread (parent) != pi->tid)
4330 if (!proc_set_fpregs (pi)) /* flush fpregs cache */
4331 proc_warn (pi, "target_resume, set_fpregs",
4337 /* The presence of a parent indicates that this is an LWP.
4338 Close any file descriptors that it might have open.
4339 We don't do this to the master (parent) procinfo. */
4341 close_procinfo_files (pi);
4343 pi->gregs_valid = 0;
4344 pi->fpregs_valid = 0;
4346 pi->gregs_dirty = 0;
4347 pi->fpregs_dirty = 0;
4349 pi->status_valid = 0;
4350 pi->threads_valid = 0;
4356 /* A callback function for iterate_over_threads. Find the
4357 asynchronous signal thread, and make it runnable. See if that
4358 helps matters any. */
4361 make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr)
4364 if (proc_flags (pi) & PR_ASLWP)
4366 if (!proc_run_process (pi, 0, -1))
4367 proc_error (pi, "make_signal_thread_runnable", __LINE__);
4375 /* Make the child process runnable. Normally we will then call
4376 procfs_wait and wait for it to stop again (unless gdb is async).
4378 If STEP is true, then arrange for the child to stop again after
4379 executing a single instruction. If SIGNO is zero, then cancel any
4380 pending signal; if non-zero, then arrange for the indicated signal
4381 to be delivered to the child when it runs. If PID is -1, then
4382 allow any child thread to run; if non-zero, then allow only the
4383 indicated thread to run. (not implemented yet). */
4386 procfs_resume (struct target_ops *ops,
4387 ptid_t ptid, int step, enum target_signal signo)
4389 procinfo *pi, *thread;
4393 prrun.prflags |= PRSVADDR;
4394 prrun.pr_vaddr = $PC; set resume address
4395 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4396 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4397 prrun.prflags |= PRCFAULT; clear current fault.
4399 PRSTRACE and PRSFAULT can be done by other means
4400 (proc_trace_signals, proc_trace_faults)
4401 PRSVADDR is unnecessary.
4402 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4403 This basically leaves PRSTEP and PRCSIG.
4404 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4405 So basically PR_STEP is the sole argument that must be passed
4406 to proc_run_process (for use in the prrun struct by ioctl). */
4408 /* Find procinfo for main process */
4409 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4411 /* First cut: ignore pid argument. */
4414 /* Convert signal to host numbering. */
4416 (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop))
4419 native_signo = target_signal_to_host (signo);
4421 pi->ignore_next_sigstop = 0;
4423 /* Running the process voids all cached registers and status. */
4424 /* Void the threads' caches first. */
4425 proc_iterate_over_threads (pi, invalidate_cache, NULL);
4426 /* Void the process procinfo's caches. */
4427 invalidate_cache (NULL, pi, NULL);
4429 if (PIDGET (ptid) != -1)
4431 /* Resume a specific thread, presumably suppressing the
4433 thread = find_procinfo (PIDGET (ptid), TIDGET (ptid));
4436 if (thread->tid != 0)
4438 /* We're to resume a specific thread, and not the
4439 others. Set the child process's PR_ASYNC flag. */
4441 if (!proc_set_async (pi))
4442 proc_error (pi, "target_resume, set_async", __LINE__);
4445 proc_iterate_over_threads (pi,
4446 make_signal_thread_runnable,
4449 pi = thread; /* substitute the thread's procinfo for run */
4454 if (!proc_run_process (pi, step, native_signo))
4457 warning (_("resume: target already running. "
4458 "Pretend to resume, and hope for the best!"));
4460 proc_error (pi, "target_resume", __LINE__);
4464 /* Traverse the list of signals that GDB knows about (see "handle"
4465 command), and arrange for the target to be stopped or not,
4466 according to these settings. Returns non-zero for success, zero
4470 register_gdb_signals (procinfo *pi, gdb_sigset_t *signals)
4474 for (signo = 0; signo < NSIG; signo ++)
4475 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
4476 signal_print_state (target_signal_from_host (signo)) == 0 &&
4477 signal_pass_state (target_signal_from_host (signo)) == 1)
4478 gdb_prdelset (signals, signo);
4480 gdb_praddset (signals, signo);
4482 return proc_set_traced_signals (pi, signals);
4485 /* Set up to trace signals in the child process. */
4488 procfs_notice_signals (ptid_t ptid)
4490 gdb_sigset_t signals;
4491 procinfo *pi = find_procinfo_or_die (PIDGET (ptid), 0);
4493 if (proc_get_traced_signals (pi, &signals) &&
4494 register_gdb_signals (pi, &signals))
4497 proc_error (pi, "notice_signals", __LINE__);
4500 /* Print status information about the child process. */
4503 procfs_files_info (struct target_ops *ignore)
4505 struct inferior *inf = current_inferior ();
4507 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
4508 inf->attach_flag? "attached": "child",
4509 target_pid_to_str (inferior_ptid));
4512 /* Stop the child process asynchronously, as when the gdb user types
4513 control-c or presses a "stop" button. Works by sending
4514 kill(SIGINT) to the child's process group. */
4517 procfs_stop (ptid_t ptid)
4519 kill (-inferior_process_group (), SIGINT);
4522 /* Make it die. Wait for it to die. Clean up after it. Note: this
4523 should only be applied to the real process, not to an LWP, because
4524 of the check for parent-process. If we need this to work for an
4525 LWP, it needs some more logic. */
4528 unconditionally_kill_inferior (procinfo *pi)
4532 parent_pid = proc_parent_pid (pi);
4533 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4534 /* FIXME: use access functions */
4535 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4536 before the PIOCKILL, otherwise it might generate a corrupted core
4537 file for the inferior. */
4538 if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0)
4540 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4543 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4544 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4545 to kill the inferior, otherwise it might remain stopped with a
4547 We do not check the result of the PIOCSSIG, the inferior might have
4550 gdb_siginfo_t newsiginfo;
4552 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
4553 newsiginfo.si_signo = SIGKILL;
4554 newsiginfo.si_code = 0;
4555 newsiginfo.si_errno = 0;
4556 newsiginfo.si_pid = getpid ();
4557 newsiginfo.si_uid = getuid ();
4558 /* FIXME: use proc_set_current_signal */
4559 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
4561 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4562 if (!proc_kill (pi, SIGKILL))
4563 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
4564 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4565 destroy_procinfo (pi);
4567 /* If pi is GDB's child, wait for it to die. */
4568 if (parent_pid == getpid ())
4569 /* FIXME: should we use waitpid to make sure we get the right event?
4570 Should we check the returned event? */
4575 ret = waitpid (pi->pid, &status, 0);
4582 /* We're done debugging it, and we want it to go away. Then we want
4583 GDB to forget all about it. */
4586 procfs_kill_inferior (struct target_ops *ops)
4588 if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */
4590 /* Find procinfo for main process */
4591 procinfo *pi = find_procinfo (PIDGET (inferior_ptid), 0);
4594 unconditionally_kill_inferior (pi);
4595 target_mourn_inferior ();
4599 /* Forget we ever debugged this thing! */
4602 procfs_mourn_inferior (struct target_ops *ops)
4606 if (!ptid_equal (inferior_ptid, null_ptid))
4608 /* Find procinfo for main process */
4609 pi = find_procinfo (PIDGET (inferior_ptid), 0);
4611 destroy_procinfo (pi);
4613 unpush_target (ops);
4615 if (dbx_link_bpt != NULL)
4617 deprecated_remove_raw_breakpoint (target_gdbarch, dbx_link_bpt);
4618 dbx_link_bpt_addr = 0;
4619 dbx_link_bpt = NULL;
4622 generic_mourn_inferior ();
4625 /* When GDB forks to create a runnable inferior process, this function
4626 is called on the parent side of the fork. It's job is to do
4627 whatever is necessary to make the child ready to be debugged, and
4628 then wait for the child to synchronize. */
4631 procfs_init_inferior (struct target_ops *ops, int pid)
4634 gdb_sigset_t signals;
4638 /* This routine called on the parent side (GDB side)
4639 after GDB forks the inferior. */
4642 if ((pi = create_procinfo (pid, 0)) == NULL)
4643 perror ("procfs: out of memory in 'init_inferior'");
4645 if (!open_procinfo_files (pi, FD_CTL))
4646 proc_error (pi, "init_inferior, open_proc_files", __LINE__);
4650 open_procinfo_files // done
4653 procfs_notice_signals
4660 /* If not stopped yet, wait for it to stop. */
4661 if (!(proc_flags (pi) & PR_STOPPED) &&
4662 !(proc_wait_for_stop (pi)))
4663 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
4665 /* Save some of the /proc state to be restored if we detach. */
4666 /* FIXME: Why? In case another debugger was debugging it?
4667 We're it's parent, for Ghu's sake! */
4668 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
4669 proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
4670 if (!proc_get_held_signals (pi, &pi->saved_sighold))
4671 proc_error (pi, "init_inferior, get_held_signals", __LINE__);
4672 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
4673 proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
4674 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
4675 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
4676 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
4677 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
4679 /* Register to trace selected signals in the child. */
4680 prfillset (&signals);
4681 if (!register_gdb_signals (pi, &signals))
4682 proc_error (pi, "init_inferior, register_signals", __LINE__);
4684 if ((fail = procfs_debug_inferior (pi)) != 0)
4685 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
4687 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4688 and possibly even turning ON kill-on-last-close at this point. But
4689 I can't make that change without careful testing which I don't have
4690 time to do right now... */
4691 /* Turn on run-on-last-close flag so that the child
4692 will die if GDB goes away for some reason. */
4693 if (!proc_set_run_on_last_close (pi))
4694 proc_error (pi, "init_inferior, set_RLC", __LINE__);
4696 /* We now have have access to the lwpid of the main thread/lwp. */
4697 lwpid = proc_get_current_thread (pi);
4699 /* Create a procinfo for the main lwp. */
4700 create_procinfo (pid, lwpid);
4702 /* We already have a main thread registered in the thread table at
4703 this point, but it didn't have any lwp info yet. Notify the core
4704 about it. This changes inferior_ptid as well. */
4705 thread_change_ptid (pid_to_ptid (pid),
4706 MERGEPID (pid, lwpid));
4708 /* Typically two, one trap to exec the shell, one to exec the
4709 program being debugged. Defined by "inferior.h". */
4710 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
4713 /* On mips-irix, we need to stop the inferior early enough during
4714 the startup phase in order to be able to load the shared library
4715 symbols and insert the breakpoints that are located in these shared
4716 libraries. Stopping at the program entry point is not good enough
4717 because the -init code is executed before the execution reaches
4720 So what we need to do is to insert a breakpoint in the runtime
4721 loader (rld), more precisely in __dbx_link(). This procedure is
4722 called by rld once all shared libraries have been mapped, but before
4723 the -init code is executed. Unfortuantely, this is not straightforward,
4724 as rld is not part of the executable we are running, and thus we need
4725 the inferior to run until rld itself has been mapped in memory.
4727 For this, we trace all syssgi() syscall exit events. Each time
4728 we detect such an event, we iterate over each text memory maps,
4729 get its associated fd, and scan the symbol table for __dbx_link().
4730 When found, we know that rld has been mapped, and that we can insert
4731 the breakpoint at the symbol address. Once the dbx_link() breakpoint
4732 has been inserted, the syssgi() notifications are no longer necessary,
4733 so they should be canceled. */
4734 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT, FLAG_SET, 0);
4738 /* When GDB forks to create a new process, this function is called on
4739 the child side of the fork before GDB exec's the user program. Its
4740 job is to make the child minimally debuggable, so that the parent
4741 GDB process can connect to the child and take over. This function
4742 should do only the minimum to make that possible, and to
4743 synchronize with the parent process. The parent process should
4744 take care of the details. */
4747 procfs_set_exec_trap (void)
4749 /* This routine called on the child side (inferior side)
4750 after GDB forks the inferior. It must use only local variables,
4751 because it may be sharing data space with its parent. */
4756 if ((pi = create_procinfo (getpid (), 0)) == NULL)
4757 perror_with_name (_("procfs: create_procinfo failed in child."));
4759 if (open_procinfo_files (pi, FD_CTL) == 0)
4761 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
4762 gdb_flush (gdb_stderr);
4763 /* No need to call "dead_procinfo", because we're going to
4768 #ifdef PRFS_STOPEXEC /* defined on OSF */
4769 /* OSF method for tracing exec syscalls. Quoting:
4770 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4771 exits from exec system calls because of the user level loader. */
4772 /* FIXME: make nice and maybe move into an access function. */
4776 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
4778 proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__);
4779 gdb_flush (gdb_stderr);
4782 prfs_flags |= PRFS_STOPEXEC;
4784 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
4786 proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__);
4787 gdb_flush (gdb_stderr);
4791 #else /* not PRFS_STOPEXEC */
4792 /* Everyone else's (except OSF) method for tracing exec syscalls. */
4794 Not all systems with /proc have all the exec* syscalls with the same
4795 names. On the SGI, for example, there is no SYS_exec, but there
4796 *is* a SYS_execv. So, we try to account for that. */
4798 exitset = sysset_t_alloc (pi);
4799 gdb_premptysysset (exitset);
4801 gdb_praddsysset (exitset, SYS_exec);
4804 gdb_praddsysset (exitset, SYS_execve);
4807 gdb_praddsysset (exitset, SYS_execv);
4809 #ifdef DYNAMIC_SYSCALLS
4811 int callnum = find_syscall (pi, "execve");
4814 gdb_praddsysset (exitset, callnum);
4816 callnum = find_syscall (pi, "ra_execve");
4818 gdb_praddsysset (exitset, callnum);
4820 #endif /* DYNAMIC_SYSCALLS */
4822 if (!proc_set_traced_sysexit (pi, exitset))
4824 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
4825 gdb_flush (gdb_stderr);
4828 #endif /* PRFS_STOPEXEC */
4830 /* FIXME: should this be done in the parent instead? */
4831 /* Turn off inherit on fork flag so that all grand-children
4832 of gdb start with tracing flags cleared. */
4833 if (!proc_unset_inherit_on_fork (pi))
4834 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
4836 /* Turn off run on last close flag, so that the child process
4837 cannot run away just because we close our handle on it.
4838 We want it to wait for the parent to attach. */
4839 if (!proc_unset_run_on_last_close (pi))
4840 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
4842 /* FIXME: No need to destroy the procinfo --
4843 we have our own address space, and we're about to do an exec! */
4844 /*destroy_procinfo (pi);*/
4847 /* This function is called BEFORE gdb forks the inferior process. Its
4848 only real responsibility is to set things up for the fork, and tell
4849 GDB which two functions to call after the fork (one for the parent,
4850 and one for the child).
4852 This function does a complicated search for a unix shell program,
4853 which it then uses to parse arguments and environment variables to
4854 be sent to the child. I wonder whether this code could not be
4855 abstracted out and shared with other unix targets such as
4859 procfs_create_inferior (struct target_ops *ops, char *exec_file,
4860 char *allargs, char **env, int from_tty)
4862 char *shell_file = getenv ("SHELL");
4866 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
4869 /* We will be looking down the PATH to find shell_file. If we
4870 just do this the normal way (via execlp, which operates by
4871 attempting an exec for each element of the PATH until it
4872 finds one which succeeds), then there will be an exec for
4873 each failed attempt, each of which will cause a PR_SYSEXIT
4874 stop, and we won't know how to distinguish the PR_SYSEXIT's
4875 for these failed execs with the ones for successful execs
4876 (whether the exec has succeeded is stored at that time in the
4877 carry bit or some such architecture-specific and
4878 non-ABI-specified place).
4880 So I can't think of anything better than to search the PATH
4881 now. This has several disadvantages: (1) There is a race
4882 condition; if we find a file now and it is deleted before we
4883 exec it, we lose, even if the deletion leaves a valid file
4884 further down in the PATH, (2) there is no way to know exactly
4885 what an executable (in the sense of "capable of being
4886 exec'd") file is. Using access() loses because it may lose
4887 if the caller is the superuser; failing to use it loses if
4888 there are ACLs or some such. */
4892 /* FIXME-maybe: might want "set path" command so user can change what
4893 path is used from within GDB. */
4894 char *path = getenv ("PATH");
4896 struct stat statbuf;
4899 path = "/bin:/usr/bin";
4901 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
4902 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
4904 p1 = strchr (p, ':');
4909 strncpy (tryname, p, len);
4910 tryname[len] = '\0';
4911 strcat (tryname, "/");
4912 strcat (tryname, shell_file);
4913 if (access (tryname, X_OK) < 0)
4915 if (stat (tryname, &statbuf) < 0)
4917 if (!S_ISREG (statbuf.st_mode))
4918 /* We certainly need to reject directories. I'm not quite
4919 as sure about FIFOs, sockets, etc., but I kind of doubt
4920 that people want to exec() these things. */
4925 /* Not found. This must be an error rather than merely passing
4926 the file to execlp(), because execlp() would try all the
4927 exec()s, causing GDB to get confused. */
4928 error (_("procfs:%d -- Can't find shell %s in PATH"),
4929 __LINE__, shell_file);
4931 shell_file = tryname;
4934 pid = fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
4935 NULL, NULL, shell_file);
4937 procfs_init_inferior (ops, pid);
4940 /* An observer for the "inferior_created" event. */
4943 procfs_inferior_created (struct target_ops *ops, int from_tty)
4946 /* Make sure to cancel the syssgi() syscall-exit notifications.
4947 They should normally have been removed by now, but they may still
4948 be activated if the inferior doesn't use shared libraries, or if
4949 we didn't locate __dbx_link, or if we never stopped in __dbx_link.
4950 See procfs_init_inferior() for more details.
4952 Since these notifications are only ever enabled when we spawned
4953 the inferior ourselves, there is nothing to do when the inferior
4954 was created by attaching to an already running process, or when
4955 debugging a core file. */
4956 if (current_inferior ()->attach_flag || !target_can_run (¤t_target))
4959 proc_trace_syscalls_1 (find_procinfo_or_die (PIDGET (inferior_ptid), 0),
4960 SYS_syssgi, PR_SYSEXIT, FLAG_RESET, 0);
4964 /* Callback for find_new_threads. Calls "add_thread". */
4967 procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
4969 ptid_t gdb_threadid = MERGEPID (pi->pid, thread->tid);
4971 if (!in_thread_list (gdb_threadid) || is_exited (gdb_threadid))
4972 add_thread (gdb_threadid);
4977 /* Query all the threads that the target knows about, and give them
4978 back to GDB to add to its list. */
4981 procfs_find_new_threads (struct target_ops *ops)
4985 /* Find procinfo for main process */
4986 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4987 proc_update_threads (pi);
4988 proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
4991 /* Return true if the thread is still 'alive'. This guy doesn't
4992 really seem to be doing his job. Got to investigate how to tell
4993 when a thread is really gone. */
4996 procfs_thread_alive (struct target_ops *ops, ptid_t ptid)
5001 proc = PIDGET (ptid);
5002 thread = TIDGET (ptid);
5003 /* If I don't know it, it ain't alive! */
5004 if ((pi = find_procinfo (proc, thread)) == NULL)
5007 /* If I can't get its status, it ain't alive!
5008 What's more, I need to forget about it! */
5009 if (!proc_get_status (pi))
5011 destroy_procinfo (pi);
5014 /* I couldn't have got its status if it weren't alive, so it's
5019 /* Convert PTID to a string. Returns the string in a static
5023 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
5025 static char buf[80];
5027 if (TIDGET (ptid) == 0)
5028 sprintf (buf, "process %d", PIDGET (ptid));
5030 sprintf (buf, "LWP %ld", TIDGET (ptid));
5035 /* Insert a watchpoint. */
5038 procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag,
5046 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5047 PIDGET (inferior_ptid) : PIDGET (ptid), 0);
5049 /* Translate from GDB's flags to /proc's */
5050 if (len > 0) /* len == 0 means delete watchpoint */
5052 switch (rwflag) { /* FIXME: need an enum! */
5053 case hw_write: /* default watchpoint (write) */
5054 pflags = WRITE_WATCHFLAG;
5056 case hw_read: /* read watchpoint */
5057 pflags = READ_WATCHFLAG;
5059 case hw_access: /* access watchpoint */
5060 pflags = READ_WATCHFLAG | WRITE_WATCHFLAG;
5062 case hw_execute: /* execution HW breakpoint */
5063 pflags = EXEC_WATCHFLAG;
5065 default: /* Something weird. Return error. */
5068 if (after) /* Stop after r/w access is completed. */
5069 pflags |= AFTER_WATCHFLAG;
5072 if (!proc_set_watchpoint (pi, addr, len, pflags))
5074 if (errno == E2BIG) /* Typical error for no resources */
5075 return -1; /* fail */
5076 /* GDB may try to remove the same watchpoint twice.
5077 If a remove request returns no match, don't error. */
5078 if (errno == ESRCH && len == 0)
5079 return 0; /* ignore */
5080 proc_error (pi, "set_watchpoint", __LINE__);
5083 #endif /* UNIXWARE */
5087 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
5088 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
5089 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
5092 Note: procfs_can_use_hw_breakpoint() is not yet used by all
5093 procfs.c targets due to the fact that some of them still define
5094 target_can_use_hardware_watchpoint. */
5097 procfs_can_use_hw_breakpoint (int type, int cnt, int othertype)
5099 /* Due to the way that proc_set_watchpoint() is implemented, host
5100 and target pointers must be of the same size. If they are not,
5101 we can't use hardware watchpoints. This limitation is due to the
5102 fact that proc_set_watchpoint() calls
5103 procfs_address_to_host_pointer(); a close inspection of
5104 procfs_address_to_host_pointer will reveal that an internal error
5105 will be generated when the host and target pointer sizes are
5107 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
5109 if (sizeof (void *) != TYPE_LENGTH (ptr_type))
5112 /* Other tests here??? */
5117 /* Returns non-zero if process is stopped on a hardware watchpoint
5118 fault, else returns zero. */
5121 procfs_stopped_by_watchpoint (void)
5125 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5127 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
5129 if (proc_why (pi) == PR_FAULTED)
5132 if (proc_what (pi) == FLTWATCH)
5136 if (proc_what (pi) == FLTKWATCH)
5144 /* Returns 1 if the OS knows the position of the triggered watchpoint,
5145 and sets *ADDR to that address. Returns 0 if OS cannot report that
5146 address. This function is only called if
5147 procfs_stopped_by_watchpoint returned 1, thus no further checks are
5148 done. The function also assumes that ADDR is not NULL. */
5151 procfs_stopped_data_address (struct target_ops *targ, CORE_ADDR *addr)
5155 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5156 return proc_watchpoint_address (pi, addr);
5160 procfs_insert_watchpoint (CORE_ADDR addr, int len, int type,
5161 struct expression *cond)
5163 if (!target_have_steppable_watchpoint
5164 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch))
5166 /* When a hardware watchpoint fires off the PC will be left at
5167 the instruction following the one which caused the
5168 watchpoint. It will *NOT* be necessary for GDB to step over
5170 return procfs_set_watchpoint (inferior_ptid, addr, len, type, 1);
5174 /* When a hardware watchpoint fires off the PC will be left at
5175 the instruction which caused the watchpoint. It will be
5176 necessary for GDB to step over the watchpoint. */
5177 return procfs_set_watchpoint (inferior_ptid, addr, len, type, 0);
5182 procfs_remove_watchpoint (CORE_ADDR addr, int len, int type,
5183 struct expression *cond)
5185 return procfs_set_watchpoint (inferior_ptid, addr, 0, 0, 0);
5189 procfs_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
5191 /* The man page for proc(4) on Solaris 2.6 and up says that the
5192 system can support "thousands" of hardware watchpoints, but gives
5193 no method for finding out how many; It doesn't say anything about
5194 the allowed size for the watched area either. So we just tell
5200 procfs_use_watchpoints (struct target_ops *t)
5202 t->to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
5203 t->to_insert_watchpoint = procfs_insert_watchpoint;
5204 t->to_remove_watchpoint = procfs_remove_watchpoint;
5205 t->to_region_ok_for_hw_watchpoint = procfs_region_ok_for_hw_watchpoint;
5206 t->to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
5207 t->to_stopped_data_address = procfs_stopped_data_address;
5210 /* Memory Mappings Functions: */
5212 /* Call a callback function once for each mapping, passing it the
5213 mapping, an optional secondary callback function, and some optional
5214 opaque data. Quit and return the first non-zero value returned
5217 PI is the procinfo struct for the process to be mapped. FUNC is
5218 the callback function to be called by this iterator. DATA is the
5219 optional opaque data to be passed to the callback function.
5220 CHILD_FUNC is the optional secondary function pointer to be passed
5221 to the child function. Returns the first non-zero return value
5222 from the callback function, or zero. */
5225 iterate_over_mappings (procinfo *pi, find_memory_region_ftype child_func,
5227 int (*func) (struct prmap *map,
5228 find_memory_region_ftype child_func,
5231 char pathname[MAX_PROC_NAME_SIZE];
5232 struct prmap *prmaps;
5233 struct prmap *prmap;
5241 /* Get the number of mappings, allocate space,
5242 and read the mappings into prmaps. */
5245 sprintf (pathname, "/proc/%d/map", pi->pid);
5246 if ((map_fd = open (pathname, O_RDONLY)) < 0)
5247 proc_error (pi, "iterate_over_mappings (open)", __LINE__);
5249 /* Make sure it gets closed again. */
5250 make_cleanup_close (map_fd);
5252 /* Use stat to determine the file size, and compute
5253 the number of prmap_t objects it contains. */
5254 if (fstat (map_fd, &sbuf) != 0)
5255 proc_error (pi, "iterate_over_mappings (fstat)", __LINE__);
5257 nmap = sbuf.st_size / sizeof (prmap_t);
5258 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5259 if (read (map_fd, (char *) prmaps, nmap * sizeof (*prmaps))
5260 != (nmap * sizeof (*prmaps)))
5261 proc_error (pi, "iterate_over_mappings (read)", __LINE__);
5263 /* Use ioctl command PIOCNMAP to get number of mappings. */
5264 if (ioctl (pi->ctl_fd, PIOCNMAP, &nmap) != 0)
5265 proc_error (pi, "iterate_over_mappings (PIOCNMAP)", __LINE__);
5267 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5268 if (ioctl (pi->ctl_fd, PIOCMAP, prmaps) != 0)
5269 proc_error (pi, "iterate_over_mappings (PIOCMAP)", __LINE__);
5272 for (prmap = prmaps; nmap > 0; prmap++, nmap--)
5273 if ((funcstat = (*func) (prmap, child_func, data)) != 0)
5279 /* Implements the to_find_memory_regions method. Calls an external
5280 function for each memory region.
5281 Returns the integer value returned by the callback. */
5284 find_memory_regions_callback (struct prmap *map,
5285 find_memory_region_ftype func, void *data)
5287 return (*func) ((CORE_ADDR) map->pr_vaddr,
5289 (map->pr_mflags & MA_READ) != 0,
5290 (map->pr_mflags & MA_WRITE) != 0,
5291 (map->pr_mflags & MA_EXEC) != 0,
5295 /* External interface. Calls a callback function once for each
5296 mapped memory region in the child process, passing as arguments:
5298 CORE_ADDR virtual_address,
5300 int read, TRUE if region is readable by the child
5301 int write, TRUE if region is writable by the child
5302 int execute TRUE if region is executable by the child.
5304 Stops iterating and returns the first non-zero value returned by
5308 proc_find_memory_regions (find_memory_region_ftype func, void *data)
5310 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5312 return iterate_over_mappings (pi, func, data,
5313 find_memory_regions_callback);
5316 /* Returns an ascii representation of a memory mapping's flags. */
5319 mappingflags (long flags)
5321 static char asciiflags[8];
5323 strcpy (asciiflags, "-------");
5324 #if defined (MA_PHYS)
5325 if (flags & MA_PHYS)
5326 asciiflags[0] = 'd';
5328 if (flags & MA_STACK)
5329 asciiflags[1] = 's';
5330 if (flags & MA_BREAK)
5331 asciiflags[2] = 'b';
5332 if (flags & MA_SHARED)
5333 asciiflags[3] = 's';
5334 if (flags & MA_READ)
5335 asciiflags[4] = 'r';
5336 if (flags & MA_WRITE)
5337 asciiflags[5] = 'w';
5338 if (flags & MA_EXEC)
5339 asciiflags[6] = 'x';
5340 return (asciiflags);
5343 /* Callback function, does the actual work for 'info proc
5347 info_mappings_callback (struct prmap *map, find_memory_region_ftype ignore,
5350 unsigned int pr_off;
5352 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5353 pr_off = (unsigned int) map->pr_offset;
5355 pr_off = map->pr_off;
5358 if (gdbarch_addr_bit (target_gdbarch) == 32)
5359 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
5360 (unsigned long) map->pr_vaddr,
5361 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5362 (unsigned long) map->pr_size,
5364 mappingflags (map->pr_mflags));
5366 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
5367 (unsigned long) map->pr_vaddr,
5368 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5369 (unsigned long) map->pr_size,
5371 mappingflags (map->pr_mflags));
5376 /* Implement the "info proc mappings" subcommand. */
5379 info_proc_mappings (procinfo *pi, int summary)
5382 return; /* No output for summary mode. */
5384 printf_filtered (_("Mapped address spaces:\n\n"));
5385 if (gdbarch_ptr_bit (target_gdbarch) == 32)
5386 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
5393 printf_filtered (" %18s %18s %10s %10s %7s\n",
5400 iterate_over_mappings (pi, NULL, NULL, info_mappings_callback);
5401 printf_filtered ("\n");
5404 /* Implement the "info proc" command. */
5407 info_proc_cmd (char *args, int from_tty)
5409 struct cleanup *old_chain;
5410 procinfo *process = NULL;
5411 procinfo *thread = NULL;
5418 old_chain = make_cleanup (null_cleanup, 0);
5421 argv = gdb_buildargv (args);
5422 make_cleanup_freeargv (argv);
5424 while (argv != NULL && *argv != NULL)
5426 if (isdigit (argv[0][0]))
5428 pid = strtoul (argv[0], &tmp, 10);
5430 tid = strtoul (++tmp, NULL, 10);
5432 else if (argv[0][0] == '/')
5434 tid = strtoul (argv[0] + 1, NULL, 10);
5436 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
5447 pid = PIDGET (inferior_ptid);
5449 error (_("No current process: you must name one."));
5452 /* Have pid, will travel.
5453 First see if it's a process we're already debugging. */
5454 process = find_procinfo (pid, 0);
5455 if (process == NULL)
5457 /* No. So open a procinfo for it, but
5458 remember to close it again when finished. */
5459 process = create_procinfo (pid, 0);
5460 make_cleanup (do_destroy_procinfo_cleanup, process);
5461 if (!open_procinfo_files (process, FD_CTL))
5462 proc_error (process, "info proc, open_procinfo_files", __LINE__);
5466 thread = create_procinfo (pid, tid);
5470 printf_filtered (_("process %d flags:\n"), process->pid);
5471 proc_prettyprint_flags (proc_flags (process), 1);
5472 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
5473 proc_prettyprint_why (proc_why (process), proc_what (process), 1);
5474 if (proc_get_nthreads (process) > 1)
5475 printf_filtered ("Process has %d threads.\n",
5476 proc_get_nthreads (process));
5480 printf_filtered (_("thread %d flags:\n"), thread->tid);
5481 proc_prettyprint_flags (proc_flags (thread), 1);
5482 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
5483 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
5488 info_proc_mappings (process, 0);
5491 do_cleanups (old_chain);
5494 /* Modify the status of the system call identified by SYSCALLNUM in
5495 the set of syscalls that are currently traced/debugged.
5497 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
5498 will be updated. Otherwise, the exit syscalls set will be updated.
5500 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
5501 will be disabled. */
5504 proc_trace_syscalls_1 (procinfo *pi, int syscallnum, int entry_or_exit,
5505 int mode, int from_tty)
5509 if (entry_or_exit == PR_SYSENTRY)
5510 sysset = proc_get_traced_sysentry (pi, NULL);
5512 sysset = proc_get_traced_sysexit (pi, NULL);
5515 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
5517 if (mode == FLAG_SET)
5518 gdb_praddsysset (sysset, syscallnum);
5520 gdb_prdelsysset (sysset, syscallnum);
5522 if (entry_or_exit == PR_SYSENTRY)
5524 if (!proc_set_traced_sysentry (pi, sysset))
5525 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
5529 if (!proc_set_traced_sysexit (pi, sysset))
5530 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
5535 proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode)
5539 if (PIDGET (inferior_ptid) <= 0)
5540 error (_("you must be debugging a process to use this command."));
5542 if (args == NULL || args[0] == 0)
5543 error_no_arg (_("system call to trace"));
5545 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5546 if (isdigit (args[0]))
5548 const int syscallnum = atoi (args);
5550 proc_trace_syscalls_1 (pi, syscallnum, entry_or_exit, mode, from_tty);
5555 proc_trace_sysentry_cmd (char *args, int from_tty)
5557 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
5561 proc_trace_sysexit_cmd (char *args, int from_tty)
5563 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
5567 proc_untrace_sysentry_cmd (char *args, int from_tty)
5569 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
5573 proc_untrace_sysexit_cmd (char *args, int from_tty)
5575 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
5580 _initialize_procfs (void)
5582 observer_attach_inferior_created (procfs_inferior_created);
5584 add_info ("proc", info_proc_cmd, _("\
5585 Show /proc process information about any running process.\n\
5586 Specify process id, or use the program being debugged by default.\n\
5587 Specify keyword 'mappings' for detailed info on memory mappings."));
5588 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
5589 _("Give a trace of entries into the syscall."));
5590 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
5591 _("Give a trace of exits from the syscall."));
5592 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
5593 _("Cancel a trace of entries into the syscall."));
5594 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
5595 _("Cancel a trace of exits from the syscall."));
5598 /* =================== END, GDB "MODULE" =================== */
5602 /* miscellaneous stubs: */
5604 /* The following satisfy a few random symbols mostly created by the
5605 solaris threads implementation, which I will chase down later. */
5607 /* Return a pid for which we guarantee we will be able to find a
5611 procfs_first_available (void)
5613 return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1);
5616 /* =================== GCORE .NOTE "MODULE" =================== */
5617 #if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT)
5618 /* gcore only implemented on solaris and unixware (so far) */
5621 procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
5622 char *note_data, int *note_size,
5623 enum target_signal stop_signal)
5625 struct regcache *regcache = get_thread_regcache (ptid);
5626 gdb_gregset_t gregs;
5627 gdb_fpregset_t fpregs;
5628 unsigned long merged_pid;
5629 struct cleanup *old_chain;
5631 merged_pid = TIDGET (ptid) << 16 | PIDGET (ptid);
5633 /* This part is the old method for fetching registers.
5634 It should be replaced by the newer one using regsets
5635 once it is implemented in this platform:
5636 gdbarch_regset_from_core_section() and regset->collect_regset(). */
5638 old_chain = save_inferior_ptid ();
5639 inferior_ptid = ptid;
5640 target_fetch_registers (regcache, -1);
5642 fill_gregset (regcache, &gregs, -1);
5643 #if defined (NEW_PROC_API)
5644 note_data = (char *) elfcore_write_lwpstatus (obfd,
5651 note_data = (char *) elfcore_write_prstatus (obfd,
5658 fill_fpregset (regcache, &fpregs, -1);
5659 note_data = (char *) elfcore_write_prfpreg (obfd,
5665 do_cleanups (old_chain);
5670 struct procfs_corefile_thread_data {
5674 enum target_signal stop_signal;
5678 procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data)
5680 struct procfs_corefile_thread_data *args = data;
5684 ptid_t ptid = MERGEPID (pi->pid, thread->tid);
5686 args->note_data = procfs_do_thread_registers (args->obfd, ptid,
5695 find_signalled_thread (struct thread_info *info, void *data)
5697 if (info->suspend.stop_signal != TARGET_SIGNAL_0
5698 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
5704 static enum target_signal
5705 find_stop_signal (void)
5707 struct thread_info *info =
5708 iterate_over_threads (find_signalled_thread, NULL);
5711 return info->suspend.stop_signal;
5713 return TARGET_SIGNAL_0;
5717 procfs_make_note_section (bfd *obfd, int *note_size)
5719 struct cleanup *old_chain;
5720 gdb_gregset_t gregs;
5721 gdb_fpregset_t fpregs;
5722 char fname[16] = {'\0'};
5723 char psargs[80] = {'\0'};
5724 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5725 char *note_data = NULL;
5727 struct procfs_corefile_thread_data thread_args;
5730 enum target_signal stop_signal;
5732 if (get_exec_file (0))
5734 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
5735 strncpy (psargs, get_exec_file (0),
5738 inf_args = get_inferior_args ();
5739 if (inf_args && *inf_args &&
5740 strlen (inf_args) < ((int) sizeof (psargs) - (int) strlen (psargs)))
5742 strncat (psargs, " ",
5743 sizeof (psargs) - strlen (psargs));
5744 strncat (psargs, inf_args,
5745 sizeof (psargs) - strlen (psargs));
5749 note_data = (char *) elfcore_write_prpsinfo (obfd,
5755 stop_signal = find_stop_signal ();
5758 fill_gregset (get_current_regcache (), &gregs, -1);
5759 note_data = elfcore_write_pstatus (obfd, note_data, note_size,
5760 PIDGET (inferior_ptid),
5761 stop_signal, &gregs);
5764 thread_args.obfd = obfd;
5765 thread_args.note_data = note_data;
5766 thread_args.note_size = note_size;
5767 thread_args.stop_signal = stop_signal;
5768 proc_iterate_over_threads (pi, procfs_corefile_thread_callback,
5771 /* There should be always at least one thread. */
5772 gdb_assert (thread_args.note_data != note_data);
5773 note_data = thread_args.note_data;
5775 auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV,
5779 note_data = elfcore_write_note (obfd, note_data, note_size,
5780 "CORE", NT_AUXV, auxv, auxv_len);
5784 make_cleanup (xfree, note_data);
5787 #else /* !(Solaris or Unixware) */
5789 procfs_make_note_section (bfd *obfd, int *note_size)
5791 error (_("gcore not implemented for this host."));
5792 return NULL; /* lint */
5794 #endif /* Solaris or Unixware */
5795 /* =================== END GCORE .NOTE "MODULE" =================== */