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 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"
58 * This module provides the interface between GDB and the
59 * /proc file system, which is used on many versions of Unix
60 * as a means for debuggers to control other processes.
61 * Examples of the systems that use this interface are:
68 * /proc works by imitating a file system: you open a simulated file
69 * that represents the process you wish to interact with, and
70 * perform operations on that "file" in order to examine or change
71 * the state of the other process.
73 * The most important thing to know about /proc and this module
74 * is that there are two very different interfaces to /proc:
75 * One that uses the ioctl system call, and
76 * another that uses read and write system calls.
77 * This module has to support both /proc interfaces. This means
78 * that there are two different ways of doing every basic operation.
80 * In order to keep most of the code simple and clean, I have
81 * defined an interface "layer" which hides all these system calls.
82 * An ifdef (NEW_PROC_API) determines which interface we are using,
83 * and most or all occurrances of this ifdef should be confined to
84 * this interface layer.
88 /* Determine which /proc API we are using:
89 The ioctl API defines PIOCSTATUS, while
90 the read/write (multiple fd) API never does. */
93 #include <sys/types.h>
94 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
97 #include <fcntl.h> /* for O_RDONLY */
98 #include <unistd.h> /* for "X_OK" */
99 #include "gdb_stat.h" /* for struct stat */
101 /* Note: procfs-utils.h must be included after the above system header
102 files, because it redefines various system calls using macros.
103 This may be incompatible with the prototype declarations. */
105 #include "proc-utils.h"
107 /* Prototypes for supply_gregset etc. */
110 /* =================== TARGET_OPS "MODULE" =================== */
113 * This module defines the GDB target vector and its methods.
116 static void procfs_attach (struct target_ops *, char *, int);
117 static void procfs_detach (struct target_ops *, char *, int);
118 static void procfs_resume (struct target_ops *,
119 ptid_t, int, enum target_signal);
120 static void procfs_stop (ptid_t);
121 static void procfs_files_info (struct target_ops *);
122 static void procfs_fetch_registers (struct target_ops *,
123 struct regcache *, int);
124 static void procfs_store_registers (struct target_ops *,
125 struct regcache *, int);
126 static void procfs_notice_signals (ptid_t);
127 static void procfs_kill_inferior (struct target_ops *ops);
128 static void procfs_mourn_inferior (struct target_ops *ops);
129 static void procfs_create_inferior (struct target_ops *, char *,
130 char *, char **, int);
131 static ptid_t procfs_wait (struct target_ops *,
132 ptid_t, struct target_waitstatus *, int);
133 static int procfs_xfer_memory (CORE_ADDR, gdb_byte *, int, int,
134 struct mem_attrib *attrib,
135 struct target_ops *);
136 static LONGEST procfs_xfer_partial (struct target_ops *ops,
137 enum target_object object,
139 gdb_byte *readbuf, const gdb_byte *writebuf,
140 ULONGEST offset, LONGEST len);
142 static int procfs_thread_alive (struct target_ops *ops, ptid_t);
144 void procfs_find_new_threads (struct target_ops *ops);
145 char *procfs_pid_to_str (struct target_ops *, ptid_t);
147 static int proc_find_memory_regions (int (*) (CORE_ADDR,
153 static char * procfs_make_note_section (bfd *, int *);
155 static int procfs_can_use_hw_breakpoint (int, int, int);
157 #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
158 /* When GDB is built as 64-bit application on Solaris, the auxv data is
159 presented in 64-bit format. We need to provide a custom parser to handle
162 procfs_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
163 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
165 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
166 gdb_byte *ptr = *readptr;
171 if (endptr - ptr < 8 * 2)
174 *typep = extract_unsigned_integer (ptr, 4, byte_order);
176 /* The size of data is always 64-bit. If the application is 32-bit,
177 it will be zero extended, as expected. */
178 *valp = extract_unsigned_integer (ptr, 8, byte_order);
189 struct target_ops *t = inf_child_target ();
191 t->to_shortname = "procfs";
192 t->to_longname = "Unix /proc child process";
194 "Unix /proc child process (started by the \"run\" command).";
195 t->to_create_inferior = procfs_create_inferior;
196 t->to_kill = procfs_kill_inferior;
197 t->to_mourn_inferior = procfs_mourn_inferior;
198 t->to_attach = procfs_attach;
199 t->to_detach = procfs_detach;
200 t->to_wait = procfs_wait;
201 t->to_resume = procfs_resume;
202 t->to_fetch_registers = procfs_fetch_registers;
203 t->to_store_registers = procfs_store_registers;
204 t->to_xfer_partial = procfs_xfer_partial;
205 t->deprecated_xfer_memory = procfs_xfer_memory;
206 t->to_notice_signals = procfs_notice_signals;
207 t->to_files_info = procfs_files_info;
208 t->to_stop = procfs_stop;
210 t->to_find_new_threads = procfs_find_new_threads;
211 t->to_thread_alive = procfs_thread_alive;
212 t->to_pid_to_str = procfs_pid_to_str;
214 t->to_has_thread_control = tc_schedlock;
215 t->to_find_memory_regions = proc_find_memory_regions;
216 t->to_make_corefile_notes = procfs_make_note_section;
218 #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
219 t->to_auxv_parse = procfs_auxv_parse;
222 t->to_magic = OPS_MAGIC;
227 /* =================== END, TARGET_OPS "MODULE" =================== */
232 * Put any typedefs, defines etc. here that are required for
233 * the unification of code that handles different versions of /proc.
236 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
238 enum { READ_WATCHFLAG = WA_READ,
239 WRITE_WATCHFLAG = WA_WRITE,
240 EXEC_WATCHFLAG = WA_EXEC,
241 AFTER_WATCHFLAG = WA_TRAPAFTER
244 #else /* Irix method for watchpoints */
245 enum { READ_WATCHFLAG = MA_READ,
246 WRITE_WATCHFLAG = MA_WRITE,
247 EXEC_WATCHFLAG = MA_EXEC,
248 AFTER_WATCHFLAG = 0 /* trapafter not implemented */
253 #ifdef HAVE_PR_SIGSET_T
254 typedef pr_sigset_t gdb_sigset_t;
256 typedef sigset_t gdb_sigset_t;
260 #ifdef HAVE_PR_SIGACTION64_T
261 typedef pr_sigaction64_t gdb_sigaction_t;
263 typedef struct sigaction gdb_sigaction_t;
267 #ifdef HAVE_PR_SIGINFO64_T
268 typedef pr_siginfo64_t gdb_siginfo_t;
270 typedef struct siginfo gdb_siginfo_t;
273 /* On mips-irix, praddset and prdelset are defined in such a way that
274 they return a value, which causes GCC to emit a -Wunused error
275 because the returned value is not used. Prevent this warning
276 by casting the return value to void. On sparc-solaris, this issue
277 does not exist because the definition of these macros already include
278 that cast to void. */
279 #define gdb_praddset(sp, flag) ((void) praddset (sp, flag))
280 #define gdb_prdelset(sp, flag) ((void) prdelset (sp, flag))
282 /* gdb_premptysysset */
284 #define gdb_premptysysset premptysysset
286 #define gdb_premptysysset premptyset
291 #define gdb_praddsysset praddsysset
293 #define gdb_praddsysset gdb_praddset
298 #define gdb_prdelsysset prdelsysset
300 #define gdb_prdelsysset gdb_prdelset
303 /* prissyssetmember */
304 #ifdef prissyssetmember
305 #define gdb_pr_issyssetmember prissyssetmember
307 #define gdb_pr_issyssetmember prismember
310 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
311 as intuitively descriptive as it could be, so we'll define
312 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
313 this writing, this feature is only found on AIX5 systems and
314 basically means that the set of syscalls is not fixed. I.e,
315 there's no nice table that one can #include to get all of the
316 syscall numbers. Instead, they're stored in /proc/PID/sysent
317 for each process. We are at least guaranteed that they won't
318 change over the lifetime of the process. But each process could
319 (in theory) have different syscall numbers.
321 #ifdef HAVE_PRSYSENT_T
322 #define DYNAMIC_SYSCALLS
327 /* =================== STRUCT PROCINFO "MODULE" =================== */
329 /* FIXME: this comment will soon be out of date W.R.T. threads. */
331 /* The procinfo struct is a wrapper to hold all the state information
332 concerning a /proc process. There should be exactly one procinfo
333 for each process, and since GDB currently can debug only one
334 process at a time, that means there should be only one procinfo.
335 All of the LWP's of a process can be accessed indirectly thru the
336 single process procinfo.
338 However, against the day when GDB may debug more than one process,
339 this data structure is kept in a list (which for now will hold no
340 more than one member), and many functions will have a pointer to a
341 procinfo as an argument.
343 There will be a separate procinfo structure for use by the (not yet
344 implemented) "info proc" command, so that we can print useful
345 information about any random process without interfering with the
346 inferior's procinfo information. */
349 /* format strings for /proc paths */
350 # ifndef CTL_PROC_NAME_FMT
351 # define MAIN_PROC_NAME_FMT "/proc/%d"
352 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
353 # define AS_PROC_NAME_FMT "/proc/%d/as"
354 # define MAP_PROC_NAME_FMT "/proc/%d/map"
355 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
356 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
358 /* the name of the proc status struct depends on the implementation */
359 typedef pstatus_t gdb_prstatus_t;
360 typedef lwpstatus_t gdb_lwpstatus_t;
361 #else /* ! NEW_PROC_API */
362 /* format strings for /proc paths */
363 # ifndef CTL_PROC_NAME_FMT
364 # define MAIN_PROC_NAME_FMT "/proc/%05d"
365 # define CTL_PROC_NAME_FMT "/proc/%05d"
366 # define AS_PROC_NAME_FMT "/proc/%05d"
367 # define MAP_PROC_NAME_FMT "/proc/%05d"
368 # define STATUS_PROC_NAME_FMT "/proc/%05d"
369 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
371 /* the name of the proc status struct depends on the implementation */
372 typedef prstatus_t gdb_prstatus_t;
373 typedef prstatus_t gdb_lwpstatus_t;
374 #endif /* NEW_PROC_API */
376 typedef struct procinfo {
377 struct procinfo *next;
378 int pid; /* Process ID */
379 int tid; /* Thread/LWP id */
383 int ignore_next_sigstop;
385 /* The following four fd fields may be identical, or may contain
386 several different fd's, depending on the version of /proc
387 (old ioctl or new read/write). */
389 int ctl_fd; /* File descriptor for /proc control file */
391 * The next three file descriptors are actually only needed in the
392 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
393 * However, to avoid a bunch of #ifdefs in the code, we will use
394 * them uniformly by (in the case of the ioctl single-file-descriptor
395 * implementation) filling them with copies of the control fd.
397 int status_fd; /* File descriptor for /proc status file */
398 int as_fd; /* File descriptor for /proc as file */
400 char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */
402 fltset_t saved_fltset; /* Saved traced hardware fault set */
403 gdb_sigset_t saved_sigset; /* Saved traced signal set */
404 gdb_sigset_t saved_sighold; /* Saved held signal set */
405 sysset_t *saved_exitset; /* Saved traced system call exit set */
406 sysset_t *saved_entryset; /* Saved traced system call entry set */
408 gdb_prstatus_t prstatus; /* Current process status info */
411 gdb_fpregset_t fpregset; /* Current floating point registers */
414 #ifdef DYNAMIC_SYSCALLS
415 int num_syscalls; /* Total number of syscalls */
416 char **syscall_names; /* Syscall number to name map */
419 struct procinfo *thread_list;
421 int status_valid : 1;
423 int fpregs_valid : 1;
424 int threads_valid: 1;
427 static char errmsg[128]; /* shared error msg buffer */
429 /* Function prototypes for procinfo module: */
431 static procinfo *find_procinfo_or_die (int pid, int tid);
432 static procinfo *find_procinfo (int pid, int tid);
433 static procinfo *create_procinfo (int pid, int tid);
434 static void destroy_procinfo (procinfo * p);
435 static void do_destroy_procinfo_cleanup (void *);
436 static void dead_procinfo (procinfo * p, char *msg, int killp);
437 static int open_procinfo_files (procinfo * p, int which);
438 static void close_procinfo_files (procinfo * p);
439 static int sysset_t_size (procinfo *p);
440 static sysset_t *sysset_t_alloc (procinfo * pi);
441 #ifdef DYNAMIC_SYSCALLS
442 static void load_syscalls (procinfo *pi);
443 static void free_syscalls (procinfo *pi);
444 static int find_syscall (procinfo *pi, char *name);
445 #endif /* DYNAMIC_SYSCALLS */
447 /* The head of the procinfo list: */
448 static procinfo * procinfo_list;
451 * Function: find_procinfo
453 * Search the procinfo list.
455 * Returns: pointer to procinfo, or NULL if not found.
459 find_procinfo (int pid, int tid)
463 for (pi = procinfo_list; pi; pi = pi->next)
470 /* Don't check threads_valid. If we're updating the
471 thread_list, we want to find whatever threads are already
472 here. This means that in general it is the caller's
473 responsibility to check threads_valid and update before
474 calling find_procinfo, if the caller wants to find a new
477 for (pi = pi->thread_list; pi; pi = pi->next)
486 * Function: find_procinfo_or_die
488 * Calls find_procinfo, but errors on failure.
492 find_procinfo_or_die (int pid, int tid)
494 procinfo *pi = find_procinfo (pid, tid);
499 error (_("procfs: couldn't find pid %d (kernel thread %d) in procinfo list."),
502 error (_("procfs: couldn't find pid %d in procinfo list."), pid);
507 /* open_with_retry() is a wrapper for open(). The appropriate
508 open() call is attempted; if unsuccessful, it will be retried as
509 many times as needed for the EAGAIN and EINTR conditions.
511 For other conditions, open_with_retry() will retry the open() a
512 limited number of times. In addition, a short sleep is imposed
513 prior to retrying the open(). The reason for this sleep is to give
514 the kernel a chance to catch up and create the file in question in
515 the event that GDB "wins" the race to open a file before the kernel
519 open_with_retry (const char *pathname, int flags)
521 int retries_remaining, status;
523 retries_remaining = 2;
527 status = open (pathname, flags);
529 if (status >= 0 || retries_remaining == 0)
531 else if (errno != EINTR && errno != EAGAIN)
542 * Function: open_procinfo_files
544 * Open the file descriptor for the process or LWP.
545 * ifdef NEW_PROC_API, we only open the control file descriptor;
546 * the others are opened lazily as needed.
547 * else (if not NEW_PROC_API), there is only one real
548 * file descriptor, but we keep multiple copies of it so that
549 * the code that uses them does not have to be #ifdef'd.
551 * Return: file descriptor, or zero for failure.
554 enum { FD_CTL, FD_STATUS, FD_AS };
557 open_procinfo_files (procinfo *pi, int which)
560 char tmp[MAX_PROC_NAME_SIZE];
565 * This function is getting ALMOST long enough to break up into several.
566 * Here is some rationale:
568 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
569 * There are several file descriptors that may need to be open
570 * for any given process or LWP. The ones we're intereted in are:
571 * - control (ctl) write-only change the state
572 * - status (status) read-only query the state
573 * - address space (as) read/write access memory
574 * - map (map) read-only virtual addr map
575 * Most of these are opened lazily as they are needed.
576 * The pathnames for the 'files' for an LWP look slightly
577 * different from those of a first-class process:
578 * Pathnames for a process (<proc-id>):
579 * /proc/<proc-id>/ctl
580 * /proc/<proc-id>/status
582 * /proc/<proc-id>/map
583 * Pathnames for an LWP (lwp-id):
584 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
585 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
586 * An LWP has no map or address space file descriptor, since
587 * the memory map and address space are shared by all LWPs.
589 * Everyone else (Solaris 2.5, Irix, OSF)
590 * There is only one file descriptor for each process or LWP.
591 * For convenience, we copy the same file descriptor into all
592 * three fields of the procinfo struct (ctl_fd, status_fd, and
593 * as_fd, see NEW_PROC_API above) so that code that uses them
594 * doesn't need any #ifdef's.
599 * Each LWP has an independent file descriptor, but these
600 * are not obtained via the 'open' system call like the rest:
601 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
602 * to the file descriptor of the parent process.
605 * These do not even have their own independent file descriptor.
606 * All operations are carried out on the file descriptor of the
607 * parent process. Therefore we just call open again for each
608 * thread, getting a new handle for the same 'file'.
613 * In this case, there are several different file descriptors that
614 * we might be asked to open. The control file descriptor will be
615 * opened early, but the others will be opened lazily as they are
619 strcpy (tmp, pi->pathname);
620 switch (which) { /* which file descriptor to open? */
623 strcat (tmp, "/lwpctl");
625 strcat (tmp, "/ctl");
626 fd = open_with_retry (tmp, O_WRONLY);
633 return 0; /* there is no 'as' file descriptor for an lwp */
635 fd = open_with_retry (tmp, O_RDWR);
642 strcat (tmp, "/lwpstatus");
644 strcat (tmp, "/status");
645 fd = open_with_retry (tmp, O_RDONLY);
651 return 0; /* unknown file descriptor */
653 #else /* not NEW_PROC_API */
655 * In this case, there is only one file descriptor for each procinfo
656 * (ie. each process or LWP). In fact, only the file descriptor for
657 * the process can actually be opened by an 'open' system call.
658 * The ones for the LWPs have to be obtained thru an IOCTL call
659 * on the process's file descriptor.
661 * For convenience, we copy each procinfo's single file descriptor
662 * into all of the fields occupied by the several file descriptors
663 * of the NEW_PROC_API implementation. That way, the code that uses
664 * them can be written without ifdefs.
668 #ifdef PIOCTSTATUS /* OSF */
669 /* Only one FD; just open it. */
670 if ((fd = open_with_retry (pi->pathname, O_RDWR)) == 0)
672 #else /* Sol 2.5, Irix, other? */
673 if (pi->tid == 0) /* Master procinfo for the process */
675 fd = open_with_retry (pi->pathname, O_RDWR);
679 else /* LWP thread procinfo */
681 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
685 /* Find the procinfo for the entire process. */
686 if ((process = find_procinfo (pi->pid, 0)) == NULL)
689 /* Now obtain the file descriptor for the LWP. */
690 if ((fd = ioctl (process->ctl_fd, PIOCOPENLWP, &lwpid)) <= 0)
692 #else /* Irix, other? */
693 return 0; /* Don't know how to open threads */
694 #endif /* Sol 2.5 PIOCOPENLWP */
696 #endif /* OSF PIOCTSTATUS */
697 pi->ctl_fd = pi->as_fd = pi->status_fd = fd;
698 #endif /* NEW_PROC_API */
700 return 1; /* success */
704 * Function: create_procinfo
706 * Allocate a data structure and link it into the procinfo list.
707 * (First tries to find a pre-existing one (FIXME: why?)
709 * Return: pointer to new procinfo struct.
713 create_procinfo (int pid, int tid)
715 procinfo *pi, *parent = NULL;
717 if ((pi = find_procinfo (pid, tid)))
718 return pi; /* Already exists, nothing to do. */
720 /* find parent before doing malloc, to save having to cleanup */
722 parent = find_procinfo_or_die (pid, 0); /* FIXME: should I
724 doesn't exist yet? */
726 pi = (procinfo *) xmalloc (sizeof (procinfo));
727 memset (pi, 0, sizeof (procinfo));
731 #ifdef DYNAMIC_SYSCALLS
735 pi->saved_entryset = sysset_t_alloc (pi);
736 pi->saved_exitset = sysset_t_alloc (pi);
738 /* Chain into list. */
741 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
742 pi->next = procinfo_list;
748 sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid);
750 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
752 pi->next = parent->thread_list;
753 parent->thread_list = pi;
759 * Function: close_procinfo_files
761 * Close all file descriptors associated with the procinfo
765 close_procinfo_files (procinfo *pi)
772 if (pi->status_fd > 0)
773 close (pi->status_fd);
775 pi->ctl_fd = pi->as_fd = pi->status_fd = 0;
779 * Function: destroy_procinfo
781 * Destructor function. Close, unlink and deallocate the object.
785 destroy_one_procinfo (procinfo **list, procinfo *pi)
789 /* Step one: unlink the procinfo from its list */
793 for (ptr = *list; ptr; ptr = ptr->next)
796 ptr->next = pi->next;
800 /* Step two: close any open file descriptors */
801 close_procinfo_files (pi);
803 /* Step three: free the memory. */
804 #ifdef DYNAMIC_SYSCALLS
807 xfree (pi->saved_entryset);
808 xfree (pi->saved_exitset);
813 destroy_procinfo (procinfo *pi)
817 if (pi->tid != 0) /* destroy a thread procinfo */
819 tmp = find_procinfo (pi->pid, 0); /* find the parent process */
820 destroy_one_procinfo (&tmp->thread_list, pi);
822 else /* destroy a process procinfo and all its threads */
824 /* First destroy the children, if any; */
825 while (pi->thread_list != NULL)
826 destroy_one_procinfo (&pi->thread_list, pi->thread_list);
827 /* Then destroy the parent. Genocide!!! */
828 destroy_one_procinfo (&procinfo_list, pi);
833 do_destroy_procinfo_cleanup (void *pi)
835 destroy_procinfo (pi);
838 enum { NOKILL, KILL };
841 * Function: dead_procinfo
843 * To be called on a non_recoverable error for a procinfo.
844 * Prints error messages, optionally sends a SIGKILL to the process,
845 * then destroys the data structure.
849 dead_procinfo (procinfo *pi, char *msg, int kill_p)
855 print_sys_errmsg (pi->pathname, errno);
859 sprintf (procfile, "process %d", pi->pid);
860 print_sys_errmsg (procfile, errno);
863 kill (pi->pid, SIGKILL);
865 destroy_procinfo (pi);
870 * Function: sysset_t_size
872 * Returns the (complete) size of a sysset_t struct. Normally, this
873 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
874 * size of sysset_t isn't known until runtime.
878 sysset_t_size (procinfo * pi)
880 #ifndef DYNAMIC_SYSCALLS
881 return sizeof (sysset_t);
883 return sizeof (sysset_t) - sizeof (uint64_t)
884 + sizeof (uint64_t) * ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
885 / (8 * sizeof (uint64_t)));
889 /* Function: sysset_t_alloc
891 Allocate and (partially) initialize a sysset_t struct. */
894 sysset_t_alloc (procinfo * pi)
897 int size = sysset_t_size (pi);
898 ret = xmalloc (size);
899 #ifdef DYNAMIC_SYSCALLS
900 ret->pr_size = (pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
901 / (8 * sizeof (uint64_t));
906 #ifdef DYNAMIC_SYSCALLS
908 /* Function: load_syscalls
910 Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
911 pi->num_syscalls with the number of syscalls and pi->syscall_names
912 with the names. (Certain numbers may be skipped in which case the
913 names for these numbers will be left as NULL.) */
915 #define MAX_SYSCALL_NAME_LENGTH 256
916 #define MAX_SYSCALLS 65536
919 load_syscalls (procinfo *pi)
921 char pathname[MAX_PROC_NAME_SIZE];
924 prsyscall_t *syscalls;
925 int i, size, maxcall;
927 pi->num_syscalls = 0;
928 pi->syscall_names = 0;
930 /* Open the file descriptor for the sysent file */
931 sprintf (pathname, "/proc/%d/sysent", pi->pid);
932 sysent_fd = open_with_retry (pathname, O_RDONLY);
935 error (_("load_syscalls: Can't open /proc/%d/sysent"), pi->pid);
938 size = sizeof header - sizeof (prsyscall_t);
939 if (read (sysent_fd, &header, size) != size)
941 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
944 if (header.pr_nsyscalls == 0)
946 error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"), pi->pid);
949 size = header.pr_nsyscalls * sizeof (prsyscall_t);
950 syscalls = xmalloc (size);
952 if (read (sysent_fd, syscalls, size) != size)
955 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
958 /* Find maximum syscall number. This may not be the same as
959 pr_nsyscalls since that value refers to the number of entries
960 in the table. (Also, the docs indicate that some system
961 call numbers may be skipped.) */
963 maxcall = syscalls[0].pr_number;
965 for (i = 1; i < header.pr_nsyscalls; i++)
966 if (syscalls[i].pr_number > maxcall
967 && syscalls[i].pr_nameoff > 0
968 && syscalls[i].pr_number < MAX_SYSCALLS)
969 maxcall = syscalls[i].pr_number;
971 pi->num_syscalls = maxcall+1;
972 pi->syscall_names = xmalloc (pi->num_syscalls * sizeof (char *));
974 for (i = 0; i < pi->num_syscalls; i++)
975 pi->syscall_names[i] = NULL;
977 /* Read the syscall names in */
978 for (i = 0; i < header.pr_nsyscalls; i++)
980 char namebuf[MAX_SYSCALL_NAME_LENGTH];
984 if (syscalls[i].pr_number >= MAX_SYSCALLS
985 || syscalls[i].pr_number < 0
986 || syscalls[i].pr_nameoff <= 0
987 || (lseek (sysent_fd, (off_t) syscalls[i].pr_nameoff, SEEK_SET)
988 != (off_t) syscalls[i].pr_nameoff))
991 nread = read (sysent_fd, namebuf, sizeof namebuf);
995 callnum = syscalls[i].pr_number;
997 if (pi->syscall_names[callnum] != NULL)
999 /* FIXME: Generate warning */
1003 namebuf[nread-1] = '\0';
1004 size = strlen (namebuf) + 1;
1005 pi->syscall_names[callnum] = xmalloc (size);
1006 strncpy (pi->syscall_names[callnum], namebuf, size-1);
1007 pi->syscall_names[callnum][size-1] = '\0';
1014 /* Function: free_syscalls
1016 Free the space allocated for the syscall names from the procinfo
1020 free_syscalls (procinfo *pi)
1022 if (pi->syscall_names)
1026 for (i = 0; i < pi->num_syscalls; i++)
1027 if (pi->syscall_names[i] != NULL)
1028 xfree (pi->syscall_names[i]);
1030 xfree (pi->syscall_names);
1031 pi->syscall_names = 0;
1035 /* Function: find_syscall
1037 Given a name, look up (and return) the corresponding syscall number.
1038 If no match is found, return -1. */
1041 find_syscall (procinfo *pi, char *name)
1044 for (i = 0; i < pi->num_syscalls; i++)
1046 if (pi->syscall_names[i] && strcmp (name, pi->syscall_names[i]) == 0)
1053 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1055 /* =================== /proc "MODULE" =================== */
1058 * This "module" is the interface layer between the /proc system API
1059 * and the gdb target vector functions. This layer consists of
1060 * access functions that encapsulate each of the basic operations
1061 * that we need to use from the /proc API.
1063 * The main motivation for this layer is to hide the fact that
1064 * there are two very different implementations of the /proc API.
1065 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1066 * functions, we do our best to hide them all in here.
1069 int proc_get_status (procinfo * pi);
1070 long proc_flags (procinfo * pi);
1071 int proc_why (procinfo * pi);
1072 int proc_what (procinfo * pi);
1073 int proc_set_run_on_last_close (procinfo * pi);
1074 int proc_unset_run_on_last_close (procinfo * pi);
1075 int proc_set_inherit_on_fork (procinfo * pi);
1076 int proc_unset_inherit_on_fork (procinfo * pi);
1077 int proc_set_async (procinfo * pi);
1078 int proc_unset_async (procinfo * pi);
1079 int proc_stop_process (procinfo * pi);
1080 int proc_trace_signal (procinfo * pi, int signo);
1081 int proc_ignore_signal (procinfo * pi, int signo);
1082 int proc_clear_current_fault (procinfo * pi);
1083 int proc_set_current_signal (procinfo * pi, int signo);
1084 int proc_clear_current_signal (procinfo * pi);
1085 int proc_set_gregs (procinfo * pi);
1086 int proc_set_fpregs (procinfo * pi);
1087 int proc_wait_for_stop (procinfo * pi);
1088 int proc_run_process (procinfo * pi, int step, int signo);
1089 int proc_kill (procinfo * pi, int signo);
1090 int proc_parent_pid (procinfo * pi);
1091 int proc_get_nthreads (procinfo * pi);
1092 int proc_get_current_thread (procinfo * pi);
1093 int proc_set_held_signals (procinfo * pi, gdb_sigset_t * sighold);
1094 int proc_set_traced_sysexit (procinfo * pi, sysset_t * sysset);
1095 int proc_set_traced_sysentry (procinfo * pi, sysset_t * sysset);
1096 int proc_set_traced_faults (procinfo * pi, fltset_t * fltset);
1097 int proc_set_traced_signals (procinfo * pi, gdb_sigset_t * sigset);
1099 int proc_update_threads (procinfo * pi);
1100 int proc_iterate_over_threads (procinfo * pi,
1101 int (*func) (procinfo *, procinfo *, void *),
1104 gdb_gregset_t *proc_get_gregs (procinfo * pi);
1105 gdb_fpregset_t *proc_get_fpregs (procinfo * pi);
1106 sysset_t *proc_get_traced_sysexit (procinfo * pi, sysset_t * save);
1107 sysset_t *proc_get_traced_sysentry (procinfo * pi, sysset_t * save);
1108 fltset_t *proc_get_traced_faults (procinfo * pi, fltset_t * save);
1109 gdb_sigset_t *proc_get_traced_signals (procinfo * pi, gdb_sigset_t * save);
1110 gdb_sigset_t *proc_get_held_signals (procinfo * pi, gdb_sigset_t * save);
1111 gdb_sigset_t *proc_get_pending_signals (procinfo * pi, gdb_sigset_t * save);
1112 gdb_sigaction_t *proc_get_signal_actions (procinfo * pi, gdb_sigaction_t *save);
1114 void proc_warn (procinfo * pi, char *func, int line);
1115 void proc_error (procinfo * pi, char *func, int line);
1118 proc_warn (procinfo *pi, char *func, int line)
1120 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1121 print_sys_errmsg (errmsg, errno);
1125 proc_error (procinfo *pi, char *func, int line)
1127 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1128 perror_with_name (errmsg);
1132 * Function: proc_get_status
1134 * Updates the status struct in the procinfo.
1135 * There is a 'valid' flag, to let other functions know when
1136 * this function needs to be called (so the status is only
1137 * read when it is needed). The status file descriptor is
1138 * also only opened when it is needed.
1140 * Return: non-zero for success, zero for failure.
1144 proc_get_status (procinfo *pi)
1146 /* Status file descriptor is opened "lazily" */
1147 if (pi->status_fd == 0 &&
1148 open_procinfo_files (pi, FD_STATUS) == 0)
1150 pi->status_valid = 0;
1155 if (lseek (pi->status_fd, 0, SEEK_SET) < 0)
1156 pi->status_valid = 0; /* fail */
1159 /* Sigh... I have to read a different data structure,
1160 depending on whether this is a main process or an LWP. */
1162 pi->status_valid = (read (pi->status_fd,
1163 (char *) &pi->prstatus.pr_lwp,
1164 sizeof (lwpstatus_t))
1165 == sizeof (lwpstatus_t));
1168 pi->status_valid = (read (pi->status_fd,
1169 (char *) &pi->prstatus,
1170 sizeof (gdb_prstatus_t))
1171 == sizeof (gdb_prstatus_t));
1172 #if 0 /*def UNIXWARE*/
1173 if (pi->status_valid &&
1174 (pi->prstatus.pr_lwp.pr_flags & PR_ISTOP) &&
1175 pi->prstatus.pr_lwp.pr_why == PR_REQUESTED)
1176 /* Unixware peculiarity -- read the damn thing again! */
1177 pi->status_valid = (read (pi->status_fd,
1178 (char *) &pi->prstatus,
1179 sizeof (gdb_prstatus_t))
1180 == sizeof (gdb_prstatus_t));
1181 #endif /* UNIXWARE */
1184 #else /* ioctl method */
1185 #ifdef PIOCTSTATUS /* osf */
1186 if (pi->tid == 0) /* main process */
1188 /* Just read the danged status. Now isn't that simple? */
1190 (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1197 tid_t pr_error_thread;
1198 struct prstatus status;
1201 thread_status.pr_count = 1;
1202 thread_status.status.pr_tid = pi->tid;
1203 win = (ioctl (pi->status_fd, PIOCTSTATUS, &thread_status) >= 0);
1206 memcpy (&pi->prstatus, &thread_status.status,
1207 sizeof (pi->prstatus));
1208 pi->status_valid = 1;
1212 /* Just read the danged status. Now isn't that simple? */
1213 pi->status_valid = (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1217 if (pi->status_valid)
1219 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1222 proc_get_current_thread (pi));
1225 /* The status struct includes general regs, so mark them valid too */
1226 pi->gregs_valid = pi->status_valid;
1228 /* In the read/write multiple-fd model,
1229 the status struct includes the fp regs too, so mark them valid too */
1230 pi->fpregs_valid = pi->status_valid;
1232 return pi->status_valid; /* True if success, false if failure. */
1236 * Function: proc_flags
1238 * returns the process flags (pr_flags field).
1242 proc_flags (procinfo *pi)
1244 if (!pi->status_valid)
1245 if (!proc_get_status (pi))
1246 return 0; /* FIXME: not a good failure value (but what is?) */
1250 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1251 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1252 The two sets of flags don't overlap. */
1253 return pi->prstatus.pr_flags | pi->prstatus.pr_lwp.pr_flags;
1255 return pi->prstatus.pr_lwp.pr_flags;
1258 return pi->prstatus.pr_flags;
1263 * Function: proc_why
1265 * returns the pr_why field (why the process stopped).
1269 proc_why (procinfo *pi)
1271 if (!pi->status_valid)
1272 if (!proc_get_status (pi))
1273 return 0; /* FIXME: not a good failure value (but what is?) */
1276 return pi->prstatus.pr_lwp.pr_why;
1278 return pi->prstatus.pr_why;
1283 * Function: proc_what
1285 * returns the pr_what field (details of why the process stopped).
1289 proc_what (procinfo *pi)
1291 if (!pi->status_valid)
1292 if (!proc_get_status (pi))
1293 return 0; /* FIXME: not a good failure value (but what is?) */
1296 return pi->prstatus.pr_lwp.pr_what;
1298 return pi->prstatus.pr_what;
1302 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1304 * Function: proc_nsysarg
1306 * returns the pr_nsysarg field (number of args to the current syscall).
1310 proc_nsysarg (procinfo *pi)
1312 if (!pi->status_valid)
1313 if (!proc_get_status (pi))
1317 return pi->prstatus.pr_lwp.pr_nsysarg;
1319 return pi->prstatus.pr_nsysarg;
1324 * Function: proc_sysargs
1326 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1330 proc_sysargs (procinfo *pi)
1332 if (!pi->status_valid)
1333 if (!proc_get_status (pi))
1337 return (long *) &pi->prstatus.pr_lwp.pr_sysarg;
1339 return (long *) &pi->prstatus.pr_sysarg;
1344 * Function: proc_syscall
1346 * returns the pr_syscall field (id of current syscall if we are in one).
1350 proc_syscall (procinfo *pi)
1352 if (!pi->status_valid)
1353 if (!proc_get_status (pi))
1357 return pi->prstatus.pr_lwp.pr_syscall;
1359 return pi->prstatus.pr_syscall;
1362 #endif /* PIOCSSPCACT */
1365 * Function: proc_cursig:
1367 * returns the pr_cursig field (current signal).
1371 proc_cursig (struct procinfo *pi)
1373 if (!pi->status_valid)
1374 if (!proc_get_status (pi))
1375 return 0; /* FIXME: not a good failure value (but what is?) */
1378 return pi->prstatus.pr_lwp.pr_cursig;
1380 return pi->prstatus.pr_cursig;
1385 * Function: proc_modify_flag
1387 * === I appologize for the messiness of this function.
1388 * === This is an area where the different versions of
1389 * === /proc are more inconsistent than usual. MVS
1391 * Set or reset any of the following process flags:
1392 * PR_FORK -- forked child will inherit trace flags
1393 * PR_RLC -- traced process runs when last /proc file closed.
1394 * PR_KLC -- traced process is killed when last /proc file closed.
1395 * PR_ASYNC -- LWP's get to run/stop independently.
1397 * There are three methods for doing this function:
1398 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1400 * 2) Middle: PIOCSET/PIOCRESET
1402 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1405 * Note: Irix does not define PR_ASYNC.
1406 * Note: OSF does not define PR_KLC.
1407 * Note: OSF is the only one that can ONLY use the oldest method.
1410 * pi -- the procinfo
1411 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1412 * mode -- 1 for set, 0 for reset.
1414 * Returns non-zero for success, zero for failure.
1417 enum { FLAG_RESET, FLAG_SET };
1420 proc_modify_flag (procinfo *pi, long flag, long mode)
1422 long win = 0; /* default to fail */
1425 * These operations affect the process as a whole, and applying
1426 * them to an individual LWP has the same meaning as applying them
1427 * to the main process. Therefore, if we're ever called with a
1428 * pointer to an LWP's procinfo, let's substitute the process's
1429 * procinfo and avoid opening the LWP's file descriptor
1434 pi = find_procinfo_or_die (pi->pid, 0);
1436 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1437 /* First normalize the PCUNSET/PCRESET command opcode
1438 (which for no obvious reason has a different definition
1439 from one operating system to the next...) */
1441 #define GDBRESET PCUNSET
1444 #define GDBRESET PCRESET
1448 procfs_ctl_t arg[2];
1450 if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC) */
1452 else /* Reset the flag */
1456 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1459 #ifdef PIOCSET /* Irix/Sol5 method */
1460 if (mode == FLAG_SET) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1462 win = (ioctl (pi->ctl_fd, PIOCSET, &flag) >= 0);
1464 else /* Reset the flag */
1466 win = (ioctl (pi->ctl_fd, PIOCRESET, &flag) >= 0);
1470 #ifdef PIOCSRLC /* Oldest method: OSF */
1473 if (mode == FLAG_SET) /* Set run-on-last-close */
1475 win = (ioctl (pi->ctl_fd, PIOCSRLC, NULL) >= 0);
1477 else /* Clear run-on-last-close */
1479 win = (ioctl (pi->ctl_fd, PIOCRRLC, NULL) >= 0);
1483 if (mode == FLAG_SET) /* Set inherit-on-fork */
1485 win = (ioctl (pi->ctl_fd, PIOCSFORK, NULL) >= 0);
1487 else /* Clear inherit-on-fork */
1489 win = (ioctl (pi->ctl_fd, PIOCRFORK, NULL) >= 0);
1493 win = 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1500 /* The above operation renders the procinfo's cached pstatus obsolete. */
1501 pi->status_valid = 0;
1504 warning (_("procfs: modify_flag failed to turn %s %s"),
1505 flag == PR_FORK ? "PR_FORK" :
1506 flag == PR_RLC ? "PR_RLC" :
1508 flag == PR_ASYNC ? "PR_ASYNC" :
1511 flag == PR_KLC ? "PR_KLC" :
1514 mode == FLAG_RESET ? "off" : "on");
1520 * Function: proc_set_run_on_last_close
1522 * Set the run_on_last_close flag.
1523 * Process with all threads will become runnable
1524 * when debugger closes all /proc fds.
1526 * Returns non-zero for success, zero for failure.
1530 proc_set_run_on_last_close (procinfo *pi)
1532 return proc_modify_flag (pi, PR_RLC, FLAG_SET);
1536 * Function: proc_unset_run_on_last_close
1538 * Reset the run_on_last_close flag.
1539 * Process will NOT become runnable
1540 * when debugger closes its file handles.
1542 * Returns non-zero for success, zero for failure.
1546 proc_unset_run_on_last_close (procinfo *pi)
1548 return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
1553 * Function: proc_set_kill_on_last_close
1555 * Set the kill_on_last_close flag.
1556 * Process with all threads will be killed when debugger
1557 * closes all /proc fds (or debugger exits or dies).
1559 * Returns non-zero for success, zero for failure.
1563 proc_set_kill_on_last_close (procinfo *pi)
1565 return proc_modify_flag (pi, PR_KLC, FLAG_SET);
1569 * Function: proc_unset_kill_on_last_close
1571 * Reset the kill_on_last_close flag.
1572 * Process will NOT be killed when debugger
1573 * closes its file handles (or exits or dies).
1575 * Returns non-zero for success, zero for failure.
1579 proc_unset_kill_on_last_close (procinfo *pi)
1581 return proc_modify_flag (pi, PR_KLC, FLAG_RESET);
1586 * Function: proc_set_inherit_on_fork
1588 * Set inherit_on_fork flag.
1589 * If the process forks a child while we are registered for events
1590 * in the parent, then we will also recieve events from the child.
1592 * Returns non-zero for success, zero for failure.
1596 proc_set_inherit_on_fork (procinfo *pi)
1598 return proc_modify_flag (pi, PR_FORK, FLAG_SET);
1602 * Function: proc_unset_inherit_on_fork
1604 * Reset inherit_on_fork flag.
1605 * If the process forks a child while we are registered for events
1606 * in the parent, then we will NOT recieve events from the child.
1608 * Returns non-zero for success, zero for failure.
1612 proc_unset_inherit_on_fork (procinfo *pi)
1614 return proc_modify_flag (pi, PR_FORK, FLAG_RESET);
1619 * Function: proc_set_async
1621 * Set PR_ASYNC flag.
1622 * If one LWP stops because of a debug event (signal etc.),
1623 * the remaining LWPs will continue to run.
1625 * Returns non-zero for success, zero for failure.
1629 proc_set_async (procinfo *pi)
1631 return proc_modify_flag (pi, PR_ASYNC, FLAG_SET);
1635 * Function: proc_unset_async
1637 * Reset PR_ASYNC flag.
1638 * If one LWP stops because of a debug event (signal etc.),
1639 * then all other LWPs will stop as well.
1641 * Returns non-zero for success, zero for failure.
1645 proc_unset_async (procinfo *pi)
1647 return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET);
1649 #endif /* PR_ASYNC */
1652 * Function: proc_stop_process
1654 * Request the process/LWP to stop. Does not wait.
1655 * Returns non-zero for success, zero for failure.
1659 proc_stop_process (procinfo *pi)
1664 * We might conceivably apply this operation to an LWP, and
1665 * the LWP's ctl file descriptor might not be open.
1668 if (pi->ctl_fd == 0 &&
1669 open_procinfo_files (pi, FD_CTL) == 0)
1674 procfs_ctl_t cmd = PCSTOP;
1675 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1676 #else /* ioctl method */
1677 win = (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) >= 0);
1678 /* Note: the call also reads the prstatus. */
1681 pi->status_valid = 1;
1682 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1685 proc_get_current_thread (pi));
1694 * Function: proc_wait_for_stop
1696 * Wait for the process or LWP to stop (block until it does).
1697 * Returns non-zero for success, zero for failure.
1701 proc_wait_for_stop (procinfo *pi)
1706 * We should never have to apply this operation to any procinfo
1707 * except the one for the main process. If that ever changes
1708 * for any reason, then take out the following clause and
1709 * replace it with one that makes sure the ctl_fd is open.
1713 pi = find_procinfo_or_die (pi->pid, 0);
1717 procfs_ctl_t cmd = PCWSTOP;
1718 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1719 /* We been runnin' and we stopped -- need to update status. */
1720 pi->status_valid = 0;
1722 #else /* ioctl method */
1723 win = (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) >= 0);
1724 /* Above call also refreshes the prstatus. */
1727 pi->status_valid = 1;
1728 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1731 proc_get_current_thread (pi));
1739 * Function: proc_run_process
1741 * Make the process or LWP runnable.
1742 * Options (not all are implemented):
1744 * - clear current fault
1745 * - clear current signal
1746 * - abort the current system call
1747 * - stop as soon as finished with system call
1748 * - (ioctl): set traced signal set
1749 * - (ioctl): set held signal set
1750 * - (ioctl): set traced fault set
1751 * - (ioctl): set start pc (vaddr)
1752 * Always clear the current fault.
1753 * Clear the current signal if 'signo' is zero.
1756 * pi the process or LWP to operate on.
1757 * step if true, set the process or LWP to trap after one instr.
1758 * signo if zero, clear the current signal if any.
1759 * if non-zero, set the current signal to this one.
1761 * Returns non-zero for success, zero for failure.
1765 proc_run_process (procinfo *pi, int step, int signo)
1771 * We will probably have to apply this operation to individual threads,
1772 * so make sure the control file descriptor is open.
1775 if (pi->ctl_fd == 0 &&
1776 open_procinfo_files (pi, FD_CTL) == 0)
1781 runflags = PRCFAULT; /* always clear current fault */
1786 else if (signo != -1) /* -1 means do nothing W.R.T. signals */
1787 proc_set_current_signal (pi, signo);
1791 procfs_ctl_t cmd[2];
1795 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1797 #else /* ioctl method */
1801 memset (&prrun, 0, sizeof (prrun));
1802 prrun.pr_flags = runflags;
1803 win = (ioctl (pi->ctl_fd, PIOCRUN, &prrun) >= 0);
1811 * Function: proc_set_traced_signals
1813 * Register to trace signals in the process or LWP.
1814 * Returns non-zero for success, zero for failure.
1818 proc_set_traced_signals (procinfo *pi, gdb_sigset_t *sigset)
1823 * We should never have to apply this operation to any procinfo
1824 * except the one for the main process. If that ever changes
1825 * for any reason, then take out the following clause and
1826 * replace it with one that makes sure the ctl_fd is open.
1830 pi = find_procinfo_or_die (pi->pid, 0);
1836 /* Use char array to avoid alignment issues. */
1837 char sigset[sizeof (gdb_sigset_t)];
1841 memcpy (&arg.sigset, sigset, sizeof (gdb_sigset_t));
1843 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1845 #else /* ioctl method */
1846 win = (ioctl (pi->ctl_fd, PIOCSTRACE, sigset) >= 0);
1848 /* The above operation renders the procinfo's cached pstatus obsolete. */
1849 pi->status_valid = 0;
1852 warning (_("procfs: set_traced_signals failed"));
1857 * Function: proc_set_traced_faults
1859 * Register to trace hardware faults in the process or LWP.
1860 * Returns non-zero for success, zero for failure.
1864 proc_set_traced_faults (procinfo *pi, fltset_t *fltset)
1869 * We should never have to apply this operation to any procinfo
1870 * except the one for the main process. If that ever changes
1871 * for any reason, then take out the following clause and
1872 * replace it with one that makes sure the ctl_fd is open.
1876 pi = find_procinfo_or_die (pi->pid, 0);
1882 /* Use char array to avoid alignment issues. */
1883 char fltset[sizeof (fltset_t)];
1887 memcpy (&arg.fltset, fltset, sizeof (fltset_t));
1889 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1891 #else /* ioctl method */
1892 win = (ioctl (pi->ctl_fd, PIOCSFAULT, fltset) >= 0);
1894 /* The above operation renders the procinfo's cached pstatus obsolete. */
1895 pi->status_valid = 0;
1901 * Function: proc_set_traced_sysentry
1903 * Register to trace entry to system calls in the process or LWP.
1904 * Returns non-zero for success, zero for failure.
1908 proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset)
1913 * We should never have to apply this operation to any procinfo
1914 * except the one for the main process. If that ever changes
1915 * for any reason, then take out the following clause and
1916 * replace it with one that makes sure the ctl_fd is open.
1920 pi = find_procinfo_or_die (pi->pid, 0);
1924 struct gdb_proc_ctl_pcsentry {
1926 /* Use char array to avoid alignment issues. */
1927 char sysset[sizeof (sysset_t)];
1929 int argp_size = sizeof (struct gdb_proc_ctl_pcsentry)
1931 + sysset_t_size (pi);
1933 argp = xmalloc (argp_size);
1935 argp->cmd = PCSENTRY;
1936 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1938 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1941 #else /* ioctl method */
1942 win = (ioctl (pi->ctl_fd, PIOCSENTRY, sysset) >= 0);
1944 /* The above operation renders the procinfo's cached pstatus obsolete. */
1945 pi->status_valid = 0;
1951 * Function: proc_set_traced_sysexit
1953 * Register to trace exit from system calls in the process or LWP.
1954 * Returns non-zero for success, zero for failure.
1958 proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset)
1963 * We should never have to apply this operation to any procinfo
1964 * except the one for the main process. If that ever changes
1965 * for any reason, then take out the following clause and
1966 * replace it with one that makes sure the ctl_fd is open.
1970 pi = find_procinfo_or_die (pi->pid, 0);
1974 struct gdb_proc_ctl_pcsexit {
1976 /* Use char array to avoid alignment issues. */
1977 char sysset[sizeof (sysset_t)];
1979 int argp_size = sizeof (struct gdb_proc_ctl_pcsexit)
1981 + sysset_t_size (pi);
1983 argp = xmalloc (argp_size);
1985 argp->cmd = PCSEXIT;
1986 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1988 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1991 #else /* ioctl method */
1992 win = (ioctl (pi->ctl_fd, PIOCSEXIT, sysset) >= 0);
1994 /* The above operation renders the procinfo's cached pstatus obsolete. */
1995 pi->status_valid = 0;
2001 * Function: proc_set_held_signals
2003 * Specify the set of blocked / held signals in the process or LWP.
2004 * Returns non-zero for success, zero for failure.
2008 proc_set_held_signals (procinfo *pi, gdb_sigset_t *sighold)
2013 * We should never have to apply this operation to any procinfo
2014 * except the one for the main process. If that ever changes
2015 * for any reason, then take out the following clause and
2016 * replace it with one that makes sure the ctl_fd is open.
2020 pi = find_procinfo_or_die (pi->pid, 0);
2026 /* Use char array to avoid alignment issues. */
2027 char hold[sizeof (gdb_sigset_t)];
2031 memcpy (&arg.hold, sighold, sizeof (gdb_sigset_t));
2032 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2035 win = (ioctl (pi->ctl_fd, PIOCSHOLD, sighold) >= 0);
2037 /* The above operation renders the procinfo's cached pstatus obsolete. */
2038 pi->status_valid = 0;
2044 * Function: proc_get_pending_signals
2046 * returns the set of signals that are pending in the process or LWP.
2047 * Will also copy the sigset if 'save' is non-zero.
2051 proc_get_pending_signals (procinfo *pi, gdb_sigset_t *save)
2053 gdb_sigset_t *ret = NULL;
2056 * We should never have to apply this operation to any procinfo
2057 * except the one for the main process. If that ever changes
2058 * for any reason, then take out the following clause and
2059 * replace it with one that makes sure the ctl_fd is open.
2063 pi = find_procinfo_or_die (pi->pid, 0);
2065 if (!pi->status_valid)
2066 if (!proc_get_status (pi))
2070 ret = &pi->prstatus.pr_lwp.pr_lwppend;
2072 ret = &pi->prstatus.pr_sigpend;
2075 memcpy (save, ret, sizeof (gdb_sigset_t));
2081 * Function: proc_get_signal_actions
2083 * returns the set of signal actions.
2084 * Will also copy the sigactionset if 'save' is non-zero.
2088 proc_get_signal_actions (procinfo *pi, gdb_sigaction_t *save)
2090 gdb_sigaction_t *ret = NULL;
2093 * We should never have to apply this operation to any procinfo
2094 * except the one for the main process. If that ever changes
2095 * for any reason, then take out the following clause and
2096 * replace it with one that makes sure the ctl_fd is open.
2100 pi = find_procinfo_or_die (pi->pid, 0);
2102 if (!pi->status_valid)
2103 if (!proc_get_status (pi))
2107 ret = &pi->prstatus.pr_lwp.pr_action;
2109 ret = &pi->prstatus.pr_action;
2112 memcpy (save, ret, sizeof (gdb_sigaction_t));
2118 * Function: proc_get_held_signals
2120 * returns the set of signals that are held / blocked.
2121 * Will also copy the sigset if 'save' is non-zero.
2125 proc_get_held_signals (procinfo *pi, gdb_sigset_t *save)
2127 gdb_sigset_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
2132 * for any reason, then take out the following clause and
2133 * replace it with one that makes sure the ctl_fd is open.
2137 pi = find_procinfo_or_die (pi->pid, 0);
2140 if (!pi->status_valid)
2141 if (!proc_get_status (pi))
2145 ret = &pi->prstatus.pr_lwp.pr_context.uc_sigmask;
2147 ret = &pi->prstatus.pr_lwp.pr_lwphold;
2148 #endif /* UNIXWARE */
2149 #else /* not NEW_PROC_API */
2151 static gdb_sigset_t sigheld;
2153 if (ioctl (pi->ctl_fd, PIOCGHOLD, &sigheld) >= 0)
2156 #endif /* NEW_PROC_API */
2158 memcpy (save, ret, sizeof (gdb_sigset_t));
2164 * Function: proc_get_traced_signals
2166 * returns the set of signals that are traced / debugged.
2167 * Will also copy the sigset if 'save' is non-zero.
2171 proc_get_traced_signals (procinfo *pi, gdb_sigset_t *save)
2173 gdb_sigset_t *ret = NULL;
2176 * We should never have to apply this operation to any procinfo
2177 * except the one for the main process. If that ever changes
2178 * for any reason, then take out the following clause and
2179 * replace it with one that makes sure the ctl_fd is open.
2183 pi = find_procinfo_or_die (pi->pid, 0);
2186 if (!pi->status_valid)
2187 if (!proc_get_status (pi))
2190 ret = &pi->prstatus.pr_sigtrace;
2193 static gdb_sigset_t sigtrace;
2195 if (ioctl (pi->ctl_fd, PIOCGTRACE, &sigtrace) >= 0)
2200 memcpy (save, ret, sizeof (gdb_sigset_t));
2206 * Function: proc_trace_signal
2208 * Add 'signo' to the set of signals that are traced.
2209 * Returns non-zero for success, zero for failure.
2213 proc_trace_signal (procinfo *pi, int signo)
2218 * We should never have to apply this operation to any procinfo
2219 * except the one for the main process. If that ever changes
2220 * for any reason, then take out the following clause and
2221 * replace it with one that makes sure the ctl_fd is open.
2225 pi = find_procinfo_or_die (pi->pid, 0);
2229 if (proc_get_traced_signals (pi, &temp))
2231 gdb_praddset (&temp, signo);
2232 return proc_set_traced_signals (pi, &temp);
2236 return 0; /* failure */
2240 * Function: proc_ignore_signal
2242 * Remove 'signo' from the set of signals that are traced.
2243 * Returns non-zero for success, zero for failure.
2247 proc_ignore_signal (procinfo *pi, int signo)
2252 * We should never have to apply this operation to any procinfo
2253 * except the one for the main process. If that ever changes
2254 * for any reason, then take out the following clause and
2255 * replace it with one that makes sure the ctl_fd is open.
2259 pi = find_procinfo_or_die (pi->pid, 0);
2263 if (proc_get_traced_signals (pi, &temp))
2265 gdb_prdelset (&temp, signo);
2266 return proc_set_traced_signals (pi, &temp);
2270 return 0; /* failure */
2274 * Function: proc_get_traced_faults
2276 * returns the set of hardware faults that are traced /debugged.
2277 * Will also copy the faultset if 'save' is non-zero.
2281 proc_get_traced_faults (procinfo *pi, fltset_t *save)
2283 fltset_t *ret = NULL;
2286 * We should never have to apply this operation to any procinfo
2287 * except the one for the main process. If that ever changes
2288 * for any reason, then take out the following clause and
2289 * replace it with one that makes sure the ctl_fd is open.
2293 pi = find_procinfo_or_die (pi->pid, 0);
2296 if (!pi->status_valid)
2297 if (!proc_get_status (pi))
2300 ret = &pi->prstatus.pr_flttrace;
2303 static fltset_t flttrace;
2305 if (ioctl (pi->ctl_fd, PIOCGFAULT, &flttrace) >= 0)
2310 memcpy (save, ret, sizeof (fltset_t));
2316 * Function: proc_get_traced_sysentry
2318 * returns the set of syscalls that are traced /debugged on entry.
2319 * Will also copy the syscall set if 'save' is non-zero.
2323 proc_get_traced_sysentry (procinfo *pi, sysset_t *save)
2325 sysset_t *ret = NULL;
2328 * We should never have to apply this operation to any procinfo
2329 * except the one for the main process. If that ever changes
2330 * for any reason, then take out the following clause and
2331 * replace it with one that makes sure the ctl_fd is open.
2335 pi = find_procinfo_or_die (pi->pid, 0);
2338 if (!pi->status_valid)
2339 if (!proc_get_status (pi))
2342 #ifndef DYNAMIC_SYSCALLS
2343 ret = &pi->prstatus.pr_sysentry;
2344 #else /* DYNAMIC_SYSCALLS */
2346 static sysset_t *sysentry;
2350 sysentry = sysset_t_alloc (pi);
2352 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2354 if (pi->prstatus.pr_sysentry_offset == 0)
2356 gdb_premptysysset (sysentry);
2362 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysentry_offset,
2364 != (off_t) pi->prstatus.pr_sysentry_offset)
2366 size = sysset_t_size (pi);
2367 gdb_premptysysset (sysentry);
2368 rsize = read (pi->status_fd, sysentry, size);
2373 #endif /* DYNAMIC_SYSCALLS */
2374 #else /* !NEW_PROC_API */
2376 static sysset_t sysentry;
2378 if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysentry) >= 0)
2381 #endif /* NEW_PROC_API */
2383 memcpy (save, ret, sysset_t_size (pi));
2389 * Function: proc_get_traced_sysexit
2391 * returns the set of syscalls that are traced /debugged on exit.
2392 * Will also copy the syscall set if 'save' is non-zero.
2396 proc_get_traced_sysexit (procinfo *pi, sysset_t *save)
2398 sysset_t * ret = NULL;
2401 * We should never have to apply this operation to any procinfo
2402 * except the one for the main process. If that ever changes
2403 * for any reason, then take out the following clause and
2404 * replace it with one that makes sure the ctl_fd is open.
2408 pi = find_procinfo_or_die (pi->pid, 0);
2411 if (!pi->status_valid)
2412 if (!proc_get_status (pi))
2415 #ifndef DYNAMIC_SYSCALLS
2416 ret = &pi->prstatus.pr_sysexit;
2417 #else /* DYNAMIC_SYSCALLS */
2419 static sysset_t *sysexit;
2423 sysexit = sysset_t_alloc (pi);
2425 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2427 if (pi->prstatus.pr_sysexit_offset == 0)
2429 gdb_premptysysset (sysexit);
2435 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysexit_offset, SEEK_SET)
2436 != (off_t) pi->prstatus.pr_sysexit_offset)
2438 size = sysset_t_size (pi);
2439 gdb_premptysysset (sysexit);
2440 rsize = read (pi->status_fd, sysexit, size);
2445 #endif /* DYNAMIC_SYSCALLS */
2448 static sysset_t sysexit;
2450 if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysexit) >= 0)
2455 memcpy (save, ret, sysset_t_size (pi));
2461 * Function: proc_clear_current_fault
2463 * The current fault (if any) is cleared; the associated signal
2464 * will not be sent to the process or LWP when it resumes.
2465 * Returns non-zero for success, zero for failure.
2469 proc_clear_current_fault (procinfo *pi)
2474 * We should never have to apply this operation to any procinfo
2475 * except the one for the main process. If that ever changes
2476 * for any reason, then take out the following clause and
2477 * replace it with one that makes sure the ctl_fd is open.
2481 pi = find_procinfo_or_die (pi->pid, 0);
2485 procfs_ctl_t cmd = PCCFAULT;
2486 win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd));
2489 win = (ioctl (pi->ctl_fd, PIOCCFAULT, 0) >= 0);
2496 * Function: proc_set_current_signal
2498 * Set the "current signal" that will be delivered next to the process.
2499 * NOTE: semantics are different from those of KILL.
2500 * This signal will be delivered to the process or LWP
2501 * immediately when it is resumed (even if the signal is held/blocked);
2502 * it will NOT immediately cause another event of interest, and will NOT
2503 * first trap back to the debugger.
2505 * Returns non-zero for success, zero for failure.
2509 proc_set_current_signal (procinfo *pi, int signo)
2514 /* Use char array to avoid alignment issues. */
2515 char sinfo[sizeof (gdb_siginfo_t)];
2517 gdb_siginfo_t mysinfo;
2519 struct target_waitstatus wait_status;
2522 * We should never have to apply this operation to any procinfo
2523 * except the one for the main process. If that ever changes
2524 * for any reason, then take out the following clause and
2525 * replace it with one that makes sure the ctl_fd is open.
2529 pi = find_procinfo_or_die (pi->pid, 0);
2531 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2532 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2533 * receives a PIOCSSIG with a signal identical to the current signal,
2534 * it messes up the current signal. Work around the kernel bug.
2537 signo == proc_cursig (pi))
2538 return 1; /* I assume this is a success? */
2541 /* The pointer is just a type alias. */
2542 get_last_target_status (&wait_ptid, &wait_status);
2543 if (ptid_equal (wait_ptid, inferior_ptid)
2544 && wait_status.kind == TARGET_WAITKIND_STOPPED
2545 && wait_status.value.sig == target_signal_from_host (signo)
2546 && proc_get_status (pi)
2548 && pi->prstatus.pr_lwp.pr_info.si_signo == signo
2550 && pi->prstatus.pr_info.si_signo == signo
2553 /* Use the siginfo associated with the signal being
2556 memcpy (arg.sinfo, &pi->prstatus.pr_lwp.pr_info, sizeof (gdb_siginfo_t));
2558 memcpy (arg.sinfo, &pi->prstatus.pr_info, sizeof (gdb_siginfo_t));
2562 mysinfo.si_signo = signo;
2563 mysinfo.si_code = 0;
2564 mysinfo.si_pid = getpid (); /* ?why? */
2565 mysinfo.si_uid = getuid (); /* ?why? */
2566 memcpy (arg.sinfo, &mysinfo, sizeof (gdb_siginfo_t));
2571 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2573 win = (ioctl (pi->ctl_fd, PIOCSSIG, (void *) &arg.sinfo) >= 0);
2580 * Function: proc_clear_current_signal
2582 * The current signal (if any) is cleared, and
2583 * is not sent to the process or LWP when it resumes.
2584 * Returns non-zero for success, zero for failure.
2588 proc_clear_current_signal (procinfo *pi)
2593 * We should never have to apply this operation to any procinfo
2594 * except the one for the main process. If that ever changes
2595 * for any reason, then take out the following clause and
2596 * replace it with one that makes sure the ctl_fd is open.
2600 pi = find_procinfo_or_die (pi->pid, 0);
2606 /* Use char array to avoid alignment issues. */
2607 char sinfo[sizeof (gdb_siginfo_t)];
2609 gdb_siginfo_t mysinfo;
2612 /* The pointer is just a type alias. */
2613 mysinfo.si_signo = 0;
2614 mysinfo.si_code = 0;
2615 mysinfo.si_errno = 0;
2616 mysinfo.si_pid = getpid (); /* ?why? */
2617 mysinfo.si_uid = getuid (); /* ?why? */
2618 memcpy (arg.sinfo, &mysinfo, sizeof (gdb_siginfo_t));
2620 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2623 win = (ioctl (pi->ctl_fd, PIOCSSIG, 0) >= 0);
2629 /* Return the general-purpose registers for the process or LWP
2630 corresponding to PI. Upon failure, return NULL. */
2633 proc_get_gregs (procinfo *pi)
2635 if (!pi->status_valid || !pi->gregs_valid)
2636 if (!proc_get_status (pi))
2639 /* OK, sorry about the ifdef's. There's three cases instead of two,
2640 because in this case Unixware and Solaris/RW differ. */
2643 # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */
2644 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs;
2646 return &pi->prstatus.pr_lwp.pr_reg;
2649 return &pi->prstatus.pr_reg;
2653 /* Return the general-purpose registers for the process or LWP
2654 corresponding to PI. Upon failure, return NULL. */
2657 proc_get_fpregs (procinfo *pi)
2660 if (!pi->status_valid || !pi->fpregs_valid)
2661 if (!proc_get_status (pi))
2664 # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */
2665 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs;
2667 return &pi->prstatus.pr_lwp.pr_fpreg;
2670 #else /* not NEW_PROC_API */
2671 if (pi->fpregs_valid)
2672 return &pi->fpregset; /* Already got 'em. */
2675 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2684 tid_t pr_error_thread;
2685 tfpregset_t thread_1;
2688 thread_fpregs.pr_count = 1;
2689 thread_fpregs.thread_1.tid = pi->tid;
2692 && ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2694 pi->fpregs_valid = 1;
2695 return &pi->fpregset; /* Got 'em now! */
2697 else if (pi->tid != 0
2698 && ioctl (pi->ctl_fd, PIOCTGFPREG, &thread_fpregs) >= 0)
2700 memcpy (&pi->fpregset, &thread_fpregs.thread_1.pr_fpregs,
2701 sizeof (pi->fpregset));
2702 pi->fpregs_valid = 1;
2703 return &pi->fpregset; /* Got 'em now! */
2710 if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2712 pi->fpregs_valid = 1;
2713 return &pi->fpregset; /* Got 'em now! */
2722 #endif /* NEW_PROC_API */
2725 /* Write the general-purpose registers back to the process or LWP
2726 corresponding to PI. Return non-zero for success, zero for
2730 proc_set_gregs (procinfo *pi)
2732 gdb_gregset_t *gregs;
2735 gregs = proc_get_gregs (pi);
2737 return 0; /* proc_get_regs has already warned. */
2739 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2748 /* Use char array to avoid alignment issues. */
2749 char gregs[sizeof (gdb_gregset_t)];
2753 memcpy (&arg.gregs, gregs, sizeof (arg.gregs));
2754 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2756 win = (ioctl (pi->ctl_fd, PIOCSREG, gregs) >= 0);
2760 /* Policy: writing the registers invalidates our cache. */
2761 pi->gregs_valid = 0;
2765 /* Write the floating-pointer registers back to the process or LWP
2766 corresponding to PI. Return non-zero for success, zero for
2770 proc_set_fpregs (procinfo *pi)
2772 gdb_fpregset_t *fpregs;
2775 fpregs = proc_get_fpregs (pi);
2777 return 0; /* proc_get_fpregs has already warned. */
2779 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2788 /* Use char array to avoid alignment issues. */
2789 char fpregs[sizeof (gdb_fpregset_t)];
2793 memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs));
2794 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2798 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2803 tid_t pr_error_thread;
2804 tfpregset_t thread_1;
2807 thread_fpregs.pr_count = 1;
2808 thread_fpregs.thread_1.tid = pi->tid;
2809 memcpy (&thread_fpregs.thread_1.pr_fpregs, fpregs,
2811 win = (ioctl (pi->ctl_fd, PIOCTSFPREG, &thread_fpregs) >= 0);
2814 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2816 #endif /* NEW_PROC_API */
2819 /* Policy: writing the registers invalidates our cache. */
2820 pi->fpregs_valid = 0;
2825 * Function: proc_kill
2827 * Send a signal to the proc or lwp with the semantics of "kill()".
2828 * Returns non-zero for success, zero for failure.
2832 proc_kill (procinfo *pi, int signo)
2837 * We might conceivably apply this operation to an LWP, and
2838 * the LWP's ctl file descriptor might not be open.
2841 if (pi->ctl_fd == 0 &&
2842 open_procinfo_files (pi, FD_CTL) == 0)
2849 procfs_ctl_t cmd[2];
2853 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
2854 #else /* ioctl method */
2855 /* FIXME: do I need the Alpha OSF fixups present in
2856 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2857 win = (ioctl (pi->ctl_fd, PIOCKILL, &signo) >= 0);
2865 * Function: proc_parent_pid
2867 * Find the pid of the process that started this one.
2868 * Returns the parent process pid, or zero.
2872 proc_parent_pid (procinfo *pi)
2875 * We should never have to apply this operation to any procinfo
2876 * except the one for the main process. If that ever changes
2877 * for any reason, then take out the following clause and
2878 * replace it with one that makes sure the ctl_fd is open.
2882 pi = find_procinfo_or_die (pi->pid, 0);
2884 if (!pi->status_valid)
2885 if (!proc_get_status (pi))
2888 return pi->prstatus.pr_ppid;
2892 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2893 (a.k.a void pointer)! */
2896 procfs_address_to_host_pointer (CORE_ADDR addr)
2898 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
2901 gdb_assert (sizeof (ptr) == TYPE_LENGTH (ptr_type));
2902 gdbarch_address_to_pointer (target_gdbarch, ptr_type,
2903 (gdb_byte *) &ptr, addr);
2908 * Function: proc_set_watchpoint
2913 proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags)
2915 #if !defined (PCWATCH) && !defined (PIOCSWATCH)
2916 /* If neither or these is defined, we can't support watchpoints.
2917 This just avoids possibly failing to compile the below on such
2921 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2922 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2927 char watch[sizeof (prwatch_t)];
2931 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2932 convert a target address into something that can be stored in a
2933 native data structure. */
2934 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2935 pwatch.pr_vaddr = (uintptr_t) procfs_address_to_host_pointer (addr);
2937 pwatch.pr_vaddr = (caddr_t) procfs_address_to_host_pointer (addr);
2939 pwatch.pr_size = len;
2940 pwatch.pr_wflags = wflags;
2941 #if defined(NEW_PROC_API) && defined (PCWATCH)
2943 memcpy (arg.watch, &pwatch, sizeof (prwatch_t));
2944 return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg));
2946 #if defined (PIOCSWATCH)
2947 return (ioctl (pi->ctl_fd, PIOCSWATCH, &pwatch) >= 0);
2949 return 0; /* Fail */
2956 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
2958 #include <sys/sysi86.h>
2961 * Function: proc_get_LDT_entry
2967 * The 'key' is actually the value of the lower 16 bits of
2968 * the GS register for the LWP that we're interested in.
2970 * Return: matching ssh struct (LDT entry).
2974 proc_get_LDT_entry (procinfo *pi, int key)
2976 static struct ssd *ldt_entry = NULL;
2978 char pathname[MAX_PROC_NAME_SIZE];
2979 struct cleanup *old_chain = NULL;
2982 /* Allocate space for one LDT entry.
2983 This alloc must persist, because we return a pointer to it. */
2984 if (ldt_entry == NULL)
2985 ldt_entry = (struct ssd *) xmalloc (sizeof (struct ssd));
2987 /* Open the file descriptor for the LDT table. */
2988 sprintf (pathname, "/proc/%d/ldt", pi->pid);
2989 if ((fd = open_with_retry (pathname, O_RDONLY)) < 0)
2991 proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__);
2994 /* Make sure it gets closed again! */
2995 old_chain = make_cleanup_close (fd);
2997 /* Now 'read' thru the table, find a match and return it. */
2998 while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd))
3000 if (ldt_entry->sel == 0 &&
3001 ldt_entry->bo == 0 &&
3002 ldt_entry->acc1 == 0 &&
3003 ldt_entry->acc2 == 0)
3004 break; /* end of table */
3005 /* If key matches, return this entry. */
3006 if (ldt_entry->sel == key)
3009 /* Loop ended, match not found. */
3013 static int nalloc = 0;
3015 /* Get the number of LDT entries. */
3016 if (ioctl (pi->ctl_fd, PIOCNLDT, &nldt) < 0)
3018 proc_warn (pi, "proc_get_LDT_entry (PIOCNLDT)", __LINE__);
3022 /* Allocate space for the number of LDT entries. */
3023 /* This alloc has to persist, 'cause we return a pointer to it. */
3026 ldt_entry = (struct ssd *)
3027 xrealloc (ldt_entry, (nldt + 1) * sizeof (struct ssd));
3031 /* Read the whole table in one gulp. */
3032 if (ioctl (pi->ctl_fd, PIOCLDT, ldt_entry) < 0)
3034 proc_warn (pi, "proc_get_LDT_entry (PIOCLDT)", __LINE__);
3038 /* Search the table and return the (first) entry matching 'key'. */
3039 for (i = 0; i < nldt; i++)
3040 if (ldt_entry[i].sel == key)
3041 return &ldt_entry[i];
3043 /* Loop ended, match not found. */
3049 * Function: procfs_find_LDT_entry
3052 * ptid_t ptid; // The GDB-style pid-plus-LWP.
3055 * pointer to the corresponding LDT entry.
3059 procfs_find_LDT_entry (ptid_t ptid)
3061 gdb_gregset_t *gregs;
3065 /* Find procinfo for the lwp. */
3066 if ((pi = find_procinfo (PIDGET (ptid), TIDGET (ptid))) == NULL)
3068 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
3069 PIDGET (ptid), TIDGET (ptid));
3072 /* get its general registers. */
3073 if ((gregs = proc_get_gregs (pi)) == NULL)
3075 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
3076 PIDGET (ptid), TIDGET (ptid));
3079 /* Now extract the GS register's lower 16 bits. */
3080 key = (*gregs)[GS] & 0xffff;
3082 /* Find the matching entry and return it. */
3083 return proc_get_LDT_entry (pi, key);
3088 /* =============== END, non-thread part of /proc "MODULE" =============== */
3090 /* =================== Thread "MODULE" =================== */
3092 /* NOTE: you'll see more ifdefs and duplication of functions here,
3093 since there is a different way to do threads on every OS. */
3096 * Function: proc_get_nthreads
3098 * Return the number of threads for the process
3101 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3106 proc_get_nthreads (procinfo *pi)
3110 if (ioctl (pi->ctl_fd, PIOCNTHR, &nthreads) < 0)
3111 proc_warn (pi, "procfs: PIOCNTHR failed", __LINE__);
3117 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3119 * Solaris and Unixware version
3122 proc_get_nthreads (procinfo *pi)
3124 if (!pi->status_valid)
3125 if (!proc_get_status (pi))
3129 * NEW_PROC_API: only works for the process procinfo,
3130 * because the LWP procinfos do not get prstatus filled in.
3133 if (pi->tid != 0) /* find the parent process procinfo */
3134 pi = find_procinfo_or_die (pi->pid, 0);
3136 return pi->prstatus.pr_nlwp;
3144 proc_get_nthreads (procinfo *pi)
3152 * Function: proc_get_current_thread (LWP version)
3154 * Return the ID of the thread that had an event of interest.
3155 * (ie. the one that hit a breakpoint or other traced event).
3156 * All other things being equal, this should be the ID of a
3157 * thread that is currently executing.
3160 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3162 * Solaris and Unixware version
3165 proc_get_current_thread (procinfo *pi)
3168 * Note: this should be applied to the root procinfo for the process,
3169 * not to the procinfo for an LWP. If applied to the procinfo for
3170 * an LWP, it will simply return that LWP's ID. In that case,
3171 * find the parent process procinfo.
3175 pi = find_procinfo_or_die (pi->pid, 0);
3177 if (!pi->status_valid)
3178 if (!proc_get_status (pi))
3182 return pi->prstatus.pr_lwp.pr_lwpid;
3184 return pi->prstatus.pr_who;
3189 #if defined (PIOCNTHR) && defined (PIOCTLIST)
3194 proc_get_current_thread (procinfo *pi)
3196 #if 0 /* FIXME: not ready for prime time? */
3197 return pi->prstatus.pr_tid;
3208 proc_get_current_thread (procinfo *pi)
3217 * Function: proc_update_threads
3219 * Discover the IDs of all the threads within the process, and
3220 * create a procinfo for each of them (chained to the parent).
3222 * This unfortunately requires a different method on every OS.
3224 * Return: non-zero for success, zero for failure.
3228 proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore)
3230 if (thread && parent) /* sanity */
3232 thread->status_valid = 0;
3233 if (!proc_get_status (thread))
3234 destroy_one_procinfo (&parent->thread_list, thread);
3236 return 0; /* keep iterating */
3239 #if defined (PIOCLSTATUS)
3241 * Solaris 2.5 (ioctl) version
3244 proc_update_threads (procinfo *pi)
3246 gdb_prstatus_t *prstatus;
3247 struct cleanup *old_chain = NULL;
3252 * We should never have to apply this operation to any procinfo
3253 * except the one for the main process. If that ever changes
3254 * for any reason, then take out the following clause and
3255 * replace it with one that makes sure the ctl_fd is open.
3259 pi = find_procinfo_or_die (pi->pid, 0);
3261 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3263 if ((nlwp = proc_get_nthreads (pi)) <= 1)
3264 return 1; /* Process is not multi-threaded; nothing to do. */
3266 prstatus = xmalloc (sizeof (gdb_prstatus_t) * (nlwp + 1));
3268 old_chain = make_cleanup (xfree, prstatus);
3269 if (ioctl (pi->ctl_fd, PIOCLSTATUS, prstatus) < 0)
3270 proc_error (pi, "update_threads (PIOCLSTATUS)", __LINE__);
3272 /* Skip element zero, which represents the process as a whole. */
3273 for (i = 1; i < nlwp + 1; i++)
3275 if ((thread = create_procinfo (pi->pid, prstatus[i].pr_who)) == NULL)
3276 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3278 memcpy (&thread->prstatus, &prstatus[i], sizeof (*prstatus));
3279 thread->status_valid = 1;
3281 pi->threads_valid = 1;
3282 do_cleanups (old_chain);
3288 * Unixware and Solaris 6 (and later) version
3291 do_closedir_cleanup (void *dir)
3297 proc_update_threads (procinfo *pi)
3299 char pathname[MAX_PROC_NAME_SIZE + 16];
3300 struct dirent *direntry;
3301 struct cleanup *old_chain = NULL;
3307 * We should never have to apply this operation to any procinfo
3308 * except the one for the main process. If that ever changes
3309 * for any reason, then take out the following clause and
3310 * replace it with one that makes sure the ctl_fd is open.
3314 pi = find_procinfo_or_die (pi->pid, 0);
3316 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3321 * Note: this brute-force method is the only way I know of
3322 * to accomplish this task on Unixware. This method will
3323 * also work on Solaris 2.6 and 2.7. There is a much simpler
3324 * and more elegant way to do this on Solaris, but the margins
3325 * of this manuscript are too small to write it here... ;-)
3328 strcpy (pathname, pi->pathname);
3329 strcat (pathname, "/lwp");
3330 if ((dirp = opendir (pathname)) == NULL)
3331 proc_error (pi, "update_threads, opendir", __LINE__);
3333 old_chain = make_cleanup (do_closedir_cleanup, dirp);
3334 while ((direntry = readdir (dirp)) != NULL)
3335 if (direntry->d_name[0] != '.') /* skip '.' and '..' */
3337 lwpid = atoi (&direntry->d_name[0]);
3338 if ((thread = create_procinfo (pi->pid, lwpid)) == NULL)
3339 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3341 pi->threads_valid = 1;
3342 do_cleanups (old_chain);
3351 proc_update_threads (procinfo *pi)
3357 * We should never have to apply this operation to any procinfo
3358 * except the one for the main process. If that ever changes
3359 * for any reason, then take out the following clause and
3360 * replace it with one that makes sure the ctl_fd is open.
3364 pi = find_procinfo_or_die (pi->pid, 0);
3366 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3368 nthreads = proc_get_nthreads (pi);
3370 return 0; /* nothing to do for 1 or fewer threads */
3372 threads = xmalloc (nthreads * sizeof (tid_t));
3374 if (ioctl (pi->ctl_fd, PIOCTLIST, threads) < 0)
3375 proc_error (pi, "procfs: update_threads (PIOCTLIST)", __LINE__);
3377 for (i = 0; i < nthreads; i++)
3379 if (!find_procinfo (pi->pid, threads[i]))
3380 if (!create_procinfo (pi->pid, threads[i]))
3381 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3383 pi->threads_valid = 1;
3391 proc_update_threads (procinfo *pi)
3395 #endif /* OSF PIOCTLIST */
3396 #endif /* NEW_PROC_API */
3397 #endif /* SOL 2.5 PIOCLSTATUS */
3400 * Function: proc_iterate_over_threads
3403 * Given a pointer to a function, call that function once
3404 * for each lwp in the procinfo list, until the function
3405 * returns non-zero, in which event return the value
3406 * returned by the function.
3408 * Note: this function does NOT call update_threads.
3409 * If you want to discover new threads first, you must
3410 * call that function explicitly. This function just makes
3411 * a quick pass over the currently-known procinfos.
3414 * pi - parent process procinfo
3415 * func - per-thread function
3416 * ptr - opaque parameter for function.
3419 * First non-zero return value from the callee, or zero.
3423 proc_iterate_over_threads (procinfo *pi,
3424 int (*func) (procinfo *, procinfo *, void *),
3427 procinfo *thread, *next;
3431 * We should never have to apply this operation to any procinfo
3432 * except the one for the main process. If that ever changes
3433 * for any reason, then take out the following clause and
3434 * replace it with one that makes sure the ctl_fd is open.
3438 pi = find_procinfo_or_die (pi->pid, 0);
3440 for (thread = pi->thread_list; thread != NULL; thread = next)
3442 next = thread->next; /* in case thread is destroyed */
3443 if ((retval = (*func) (pi, thread, ptr)) != 0)
3450 /* =================== END, Thread "MODULE" =================== */
3452 /* =================== END, /proc "MODULE" =================== */
3454 /* =================== GDB "MODULE" =================== */
3457 * Here are all of the gdb target vector functions and their friends.
3460 static ptid_t do_attach (ptid_t ptid);
3461 static void do_detach (int signo);
3462 static int register_gdb_signals (procinfo *, gdb_sigset_t *);
3463 static void proc_trace_syscalls_1 (procinfo *pi, int syscallnum,
3464 int entry_or_exit, int mode, int from_tty);
3465 static int insert_dbx_link_breakpoint (procinfo *pi);
3466 static void remove_dbx_link_breakpoint (void);
3468 /* On mips-irix, we need to insert a breakpoint at __dbx_link during
3469 the startup phase. The following two variables are used to record
3470 the address of the breakpoint, and the code that was replaced by
3472 static int dbx_link_bpt_addr = 0;
3473 static void *dbx_link_bpt;
3476 * Function: procfs_debug_inferior
3478 * Sets up the inferior to be debugged.
3479 * Registers to trace signals, hardware faults, and syscalls.
3480 * Note: does not set RLC flag: caller may want to customize that.
3482 * Returns: zero for success (note! unlike most functions in this module)
3483 * On failure, returns the LINE NUMBER where it failed!
3487 procfs_debug_inferior (procinfo *pi)
3489 fltset_t traced_faults;
3490 gdb_sigset_t traced_signals;
3491 sysset_t *traced_syscall_entries;
3492 sysset_t *traced_syscall_exits;
3495 #ifdef PROCFS_DONT_TRACE_FAULTS
3496 /* On some systems (OSF), we don't trace hardware faults.
3497 Apparently it's enough that we catch them as signals.
3498 Wonder why we don't just do that in general? */
3499 premptyset (&traced_faults); /* don't trace faults. */
3501 /* Register to trace hardware faults in the child. */
3502 prfillset (&traced_faults); /* trace all faults... */
3503 gdb_prdelset (&traced_faults, FLTPAGE); /* except page fault. */
3505 if (!proc_set_traced_faults (pi, &traced_faults))
3508 /* Register to trace selected signals in the child. */
3509 premptyset (&traced_signals);
3510 if (!register_gdb_signals (pi, &traced_signals))
3514 /* Register to trace the 'exit' system call (on entry). */
3515 traced_syscall_entries = sysset_t_alloc (pi);
3516 gdb_premptysysset (traced_syscall_entries);
3518 gdb_praddsysset (traced_syscall_entries, SYS_exit);
3521 gdb_praddsysset (traced_syscall_entries, SYS_lwpexit); /* And _lwp_exit... */
3524 gdb_praddsysset (traced_syscall_entries, SYS_lwp_exit);
3526 #ifdef DYNAMIC_SYSCALLS
3528 int callnum = find_syscall (pi, "_exit");
3530 gdb_praddsysset (traced_syscall_entries, callnum);
3534 status = proc_set_traced_sysentry (pi, traced_syscall_entries);
3535 xfree (traced_syscall_entries);
3539 #ifdef PRFS_STOPEXEC /* defined on OSF */
3540 /* OSF method for tracing exec syscalls. Quoting:
3541 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3542 exits from exec system calls because of the user level loader. */
3543 /* FIXME: make nice and maybe move into an access function. */
3547 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
3550 prfs_flags |= PRFS_STOPEXEC;
3552 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
3555 #else /* not PRFS_STOPEXEC */
3556 /* Everyone else's (except OSF) method for tracing exec syscalls */
3558 Not all systems with /proc have all the exec* syscalls with the same
3559 names. On the SGI, for example, there is no SYS_exec, but there
3560 *is* a SYS_execv. So, we try to account for that. */
3562 traced_syscall_exits = sysset_t_alloc (pi);
3563 gdb_premptysysset (traced_syscall_exits);
3565 gdb_praddsysset (traced_syscall_exits, SYS_exec);
3568 gdb_praddsysset (traced_syscall_exits, SYS_execve);
3571 gdb_praddsysset (traced_syscall_exits, SYS_execv);
3574 #ifdef SYS_lwpcreate
3575 gdb_praddsysset (traced_syscall_exits, SYS_lwpcreate);
3576 gdb_praddsysset (traced_syscall_exits, SYS_lwpexit);
3579 #ifdef SYS_lwp_create /* FIXME: once only, please */
3580 gdb_praddsysset (traced_syscall_exits, SYS_lwp_create);
3581 gdb_praddsysset (traced_syscall_exits, SYS_lwp_exit);
3584 #ifdef DYNAMIC_SYSCALLS
3586 int callnum = find_syscall (pi, "execve");
3588 gdb_praddsysset (traced_syscall_exits, callnum);
3589 callnum = find_syscall (pi, "ra_execve");
3591 gdb_praddsysset (traced_syscall_exits, callnum);
3595 status = proc_set_traced_sysexit (pi, traced_syscall_exits);
3596 xfree (traced_syscall_exits);
3600 #endif /* PRFS_STOPEXEC */
3605 procfs_attach (struct target_ops *ops, char *args, int from_tty)
3611 error_no_arg (_("process-id to attach"));
3614 if (pid == getpid ())
3615 error (_("Attaching GDB to itself is not a good idea..."));
3619 exec_file = get_exec_file (0);
3622 printf_filtered (_("Attaching to program `%s', %s\n"),
3623 exec_file, target_pid_to_str (pid_to_ptid (pid)));
3625 printf_filtered (_("Attaching to %s\n"),
3626 target_pid_to_str (pid_to_ptid (pid)));
3630 inferior_ptid = do_attach (pid_to_ptid (pid));
3635 procfs_detach (struct target_ops *ops, char *args, int from_tty)
3638 int pid = PIDGET (inferior_ptid);
3647 exec_file = get_exec_file (0);
3648 if (exec_file == NULL)
3651 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file,
3652 target_pid_to_str (pid_to_ptid (pid)));
3653 gdb_flush (gdb_stdout);
3658 inferior_ptid = null_ptid;
3659 detach_inferior (pid);
3660 unpush_target (ops);
3664 do_attach (ptid_t ptid)
3667 struct inferior *inf;
3671 if ((pi = create_procinfo (PIDGET (ptid), 0)) == NULL)
3672 perror (_("procfs: out of memory in 'attach'"));
3674 if (!open_procinfo_files (pi, FD_CTL))
3676 fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__);
3677 sprintf (errmsg, "do_attach: couldn't open /proc file for process %d",
3679 dead_procinfo (pi, errmsg, NOKILL);
3682 /* Stop the process (if it isn't already stopped). */
3683 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
3685 pi->was_stopped = 1;
3686 proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
3690 pi->was_stopped = 0;
3691 /* Set the process to run again when we close it. */
3692 if (!proc_set_run_on_last_close (pi))
3693 dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
3695 /* Now stop the process. */
3696 if (!proc_stop_process (pi))
3697 dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
3698 pi->ignore_next_sigstop = 1;
3700 /* Save some of the /proc state to be restored if we detach. */
3701 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
3702 dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
3703 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
3704 dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
3705 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
3706 dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
3708 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
3709 dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
3711 if (!proc_get_held_signals (pi, &pi->saved_sighold))
3712 dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
3714 if ((fail = procfs_debug_inferior (pi)) != 0)
3715 dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
3717 inf = current_inferior ();
3718 inferior_appeared (inf, pi->pid);
3719 /* Let GDB know that the inferior was attached. */
3720 inf->attach_flag = 1;
3722 /* Create a procinfo for the current lwp. */
3723 lwpid = proc_get_current_thread (pi);
3724 create_procinfo (pi->pid, lwpid);
3726 /* Add it to gdb's thread list. */
3727 ptid = MERGEPID (pi->pid, lwpid);
3734 do_detach (int signo)
3738 /* Find procinfo for the main process */
3739 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); /* FIXME: threads */
3741 if (!proc_set_current_signal (pi, signo))
3742 proc_warn (pi, "do_detach, set_current_signal", __LINE__);
3744 if (!proc_set_traced_signals (pi, &pi->saved_sigset))
3745 proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
3747 if (!proc_set_traced_faults (pi, &pi->saved_fltset))
3748 proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
3750 if (!proc_set_traced_sysentry (pi, pi->saved_entryset))
3751 proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
3753 if (!proc_set_traced_sysexit (pi, pi->saved_exitset))
3754 proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
3756 if (!proc_set_held_signals (pi, &pi->saved_sighold))
3757 proc_warn (pi, "do_detach, set_held_signals", __LINE__);
3759 if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)))
3760 if (signo || !(pi->was_stopped) ||
3761 query (_("Was stopped when attached, make it runnable again? ")))
3763 /* Clear any pending signal. */
3764 if (!proc_clear_current_fault (pi))
3765 proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
3767 if (signo == 0 && !proc_clear_current_signal (pi))
3768 proc_warn (pi, "do_detach, clear_current_signal", __LINE__);
3770 if (!proc_set_run_on_last_close (pi))
3771 proc_warn (pi, "do_detach, set_rlc", __LINE__);
3774 destroy_procinfo (pi);
3777 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
3780 ??? Is the following note still relevant? We can't get individual
3781 registers with the PT_GETREGS ptrace(2) request either, yet we
3782 don't bother with caching at all in that case.
3784 NOTE: Since the /proc interface cannot give us individual
3785 registers, we pay no attention to REGNUM, and just fetch them all.
3786 This results in the possibility that we will do unnecessarily many
3787 fetches, since we may be called repeatedly for individual
3788 registers. So we cache the results, and mark the cache invalid
3789 when the process is resumed. */
3792 procfs_fetch_registers (struct target_ops *ops,
3793 struct regcache *regcache, int regnum)
3795 gdb_gregset_t *gregs;
3797 int pid = PIDGET (inferior_ptid);
3798 int tid = TIDGET (inferior_ptid);
3799 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3801 pi = find_procinfo_or_die (pid, tid);
3804 error (_("procfs: fetch_registers failed to find procinfo for %s"),
3805 target_pid_to_str (inferior_ptid));
3807 gregs = proc_get_gregs (pi);
3809 proc_error (pi, "fetch_registers, get_gregs", __LINE__);
3811 supply_gregset (regcache, (const gdb_gregset_t *) gregs);
3813 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
3815 gdb_fpregset_t *fpregs;
3817 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
3818 || regnum == gdbarch_pc_regnum (gdbarch)
3819 || regnum == gdbarch_sp_regnum (gdbarch))
3820 return; /* Not a floating point register. */
3822 fpregs = proc_get_fpregs (pi);
3824 proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
3826 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregs);
3830 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
3831 this for all registers.
3833 NOTE: Since the /proc interface will not read individual registers,
3834 we will cache these requests until the process is resumed, and only
3835 then write them back to the inferior process.
3837 FIXME: is that a really bad idea? Have to think about cases where
3838 writing one register might affect the value of others, etc. */
3841 procfs_store_registers (struct target_ops *ops,
3842 struct regcache *regcache, int regnum)
3844 gdb_gregset_t *gregs;
3846 int pid = PIDGET (inferior_ptid);
3847 int tid = TIDGET (inferior_ptid);
3848 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3850 pi = find_procinfo_or_die (pid, tid);
3853 error (_("procfs: store_registers: failed to find procinfo for %s"),
3854 target_pid_to_str (inferior_ptid));
3856 gregs = proc_get_gregs (pi);
3858 proc_error (pi, "store_registers, get_gregs", __LINE__);
3860 fill_gregset (regcache, gregs, regnum);
3861 if (!proc_set_gregs (pi))
3862 proc_error (pi, "store_registers, set_gregs", __LINE__);
3864 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
3866 gdb_fpregset_t *fpregs;
3868 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
3869 || regnum == gdbarch_pc_regnum (gdbarch)
3870 || regnum == gdbarch_sp_regnum (gdbarch))
3871 return; /* Not a floating point register. */
3873 fpregs = proc_get_fpregs (pi);
3875 proc_error (pi, "store_registers, get_fpregs", __LINE__);
3877 fill_fpregset (regcache, fpregs, regnum);
3878 if (!proc_set_fpregs (pi))
3879 proc_error (pi, "store_registers, set_fpregs", __LINE__);
3884 syscall_is_lwp_exit (procinfo *pi, int scall)
3888 if (scall == SYS_lwp_exit)
3892 if (scall == SYS_lwpexit)
3899 syscall_is_exit (procinfo *pi, int scall)
3902 if (scall == SYS_exit)
3905 #ifdef DYNAMIC_SYSCALLS
3906 if (find_syscall (pi, "_exit") == scall)
3913 syscall_is_exec (procinfo *pi, int scall)
3916 if (scall == SYS_exec)
3920 if (scall == SYS_execv)
3924 if (scall == SYS_execve)
3927 #ifdef DYNAMIC_SYSCALLS
3928 if (find_syscall (pi, "_execve"))
3930 if (find_syscall (pi, "ra_execve"))
3937 syscall_is_lwp_create (procinfo *pi, int scall)
3939 #ifdef SYS_lwp_create
3940 if (scall == SYS_lwp_create)
3943 #ifdef SYS_lwpcreate
3944 if (scall == SYS_lwpcreate)
3951 * Function: target_wait
3953 * Retrieve the next stop event from the child process.
3954 * If child has not stopped yet, wait for it to stop.
3955 * Translate /proc eventcodes (or possibly wait eventcodes)
3956 * into gdb internal event codes.
3958 * Return: id of process (and possibly thread) that incurred the event.
3959 * event codes are returned thru a pointer parameter.
3963 procfs_wait (struct target_ops *ops,
3964 ptid_t ptid, struct target_waitstatus *status, int options)
3966 /* First cut: loosely based on original version 2.1 */
3970 ptid_t retval, temp_ptid;
3971 int why, what, flags;
3978 retval = pid_to_ptid (-1);
3980 /* Find procinfo for main process */
3981 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
3984 /* We must assume that the status is stale now... */
3985 pi->status_valid = 0;
3986 pi->gregs_valid = 0;
3987 pi->fpregs_valid = 0;
3989 #if 0 /* just try this out... */
3990 flags = proc_flags (pi);
3991 why = proc_why (pi);
3992 if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
3993 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
3995 /* If child is not stopped, wait for it to stop. */
3996 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) &&
3997 !proc_wait_for_stop (pi))
3999 /* wait_for_stop failed: has the child terminated? */
4000 if (errno == ENOENT)
4004 /* /proc file not found; presumably child has terminated. */
4005 wait_retval = wait (&wstat); /* "wait" for the child's exit */
4007 if (wait_retval != PIDGET (inferior_ptid)) /* wrong child? */
4008 error (_("procfs: couldn't stop process %d: wait returned %d."),
4009 PIDGET (inferior_ptid), wait_retval);
4010 /* FIXME: might I not just use waitpid?
4011 Or try find_procinfo to see if I know about this child? */
4012 retval = pid_to_ptid (wait_retval);
4014 else if (errno == EINTR)
4018 /* Unknown error from wait_for_stop. */
4019 proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
4024 /* This long block is reached if either:
4025 a) the child was already stopped, or
4026 b) we successfully waited for the child with wait_for_stop.
4027 This block will analyze the /proc status, and translate it
4028 into a waitstatus for GDB.
4030 If we actually had to call wait because the /proc file
4031 is gone (child terminated), then we skip this block,
4032 because we already have a waitstatus. */
4034 flags = proc_flags (pi);
4035 why = proc_why (pi);
4036 what = proc_what (pi);
4038 if (flags & (PR_STOPPED | PR_ISTOP))
4041 /* If it's running async (for single_thread control),
4042 set it back to normal again. */
4043 if (flags & PR_ASYNC)
4044 if (!proc_unset_async (pi))
4045 proc_error (pi, "target_wait, unset_async", __LINE__);
4049 proc_prettyprint_why (why, what, 1);
4051 /* The 'pid' we will return to GDB is composed of
4052 the process ID plus the lwp ID. */
4053 retval = MERGEPID (pi->pid, proc_get_current_thread (pi));
4057 wstat = (what << 8) | 0177;
4060 if (syscall_is_lwp_exit (pi, what))
4062 if (print_thread_events)
4063 printf_unfiltered (_("[%s exited]\n"),
4064 target_pid_to_str (retval));
4065 delete_thread (retval);
4066 status->kind = TARGET_WAITKIND_SPURIOUS;
4069 else if (syscall_is_exit (pi, what))
4071 struct inferior *inf;
4073 /* Handle SYS_exit call only */
4074 /* Stopped at entry to SYS_exit.
4075 Make it runnable, resume it, then use
4076 the wait system call to get its exit code.
4077 Proc_run_process always clears the current
4079 Then return its exit status. */
4080 pi->status_valid = 0;
4082 /* FIXME: what we should do is return
4083 TARGET_WAITKIND_SPURIOUS. */
4084 if (!proc_run_process (pi, 0, 0))
4085 proc_error (pi, "target_wait, run_process", __LINE__);
4087 inf = find_inferior_pid (pi->pid);
4088 if (inf->attach_flag)
4090 /* Don't call wait: simulate waiting for exit,
4091 return a "success" exit code. Bogus: what if
4092 it returns something else? */
4094 retval = inferior_ptid; /* ? ? ? */
4098 int temp = wait (&wstat);
4100 /* FIXME: shouldn't I make sure I get the right
4101 event from the right process? If (for
4102 instance) I have killed an earlier inferior
4103 process but failed to clean up after it
4104 somehow, I could get its termination event
4107 /* If wait returns -1, that's what we return to GDB. */
4109 retval = pid_to_ptid (temp);
4114 printf_filtered (_("procfs: trapped on entry to "));
4115 proc_prettyprint_syscall (proc_what (pi), 0);
4116 printf_filtered ("\n");
4119 long i, nsysargs, *sysargs;
4121 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4122 (sysargs = proc_sysargs (pi)) != NULL)
4124 printf_filtered (_("%ld syscall arguments:\n"), nsysargs);
4125 for (i = 0; i < nsysargs; i++)
4126 printf_filtered ("#%ld: 0x%08lx\n",
4134 /* How to exit gracefully, returning "unknown event" */
4135 status->kind = TARGET_WAITKIND_SPURIOUS;
4136 return inferior_ptid;
4140 /* How to keep going without returning to wfi: */
4141 target_resume (ptid, 0, TARGET_SIGNAL_0);
4147 if (syscall_is_exec (pi, what))
4149 /* Hopefully this is our own "fork-child" execing
4150 the real child. Hoax this event into a trap, and
4151 GDB will see the child about to execute its start
4153 wstat = (SIGTRAP << 8) | 0177;
4156 else if (what == SYS_syssgi)
4158 /* see if we can break on dbx_link(). If yes, then
4159 we no longer need the SYS_syssgi notifications. */
4160 if (insert_dbx_link_breakpoint (pi))
4161 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT,
4164 /* This is an internal event and should be transparent
4165 to wfi, so resume the execution and wait again. See
4166 comment in procfs_init_inferior() for more details. */
4167 target_resume (ptid, 0, TARGET_SIGNAL_0);
4171 else if (syscall_is_lwp_create (pi, what))
4174 * This syscall is somewhat like fork/exec.
4175 * We will get the event twice: once for the parent LWP,
4176 * and once for the child. We should already know about
4177 * the parent LWP, but the child will be new to us. So,
4178 * whenever we get this event, if it represents a new
4179 * thread, simply add the thread to the list.
4182 /* If not in procinfo list, add it. */
4183 temp_tid = proc_get_current_thread (pi);
4184 if (!find_procinfo (pi->pid, temp_tid))
4185 create_procinfo (pi->pid, temp_tid);
4187 temp_ptid = MERGEPID (pi->pid, temp_tid);
4188 /* If not in GDB's thread list, add it. */
4189 if (!in_thread_list (temp_ptid))
4190 add_thread (temp_ptid);
4192 /* Return to WFI, but tell it to immediately resume. */
4193 status->kind = TARGET_WAITKIND_SPURIOUS;
4194 return inferior_ptid;
4196 else if (syscall_is_lwp_exit (pi, what))
4198 if (print_thread_events)
4199 printf_unfiltered (_("[%s exited]\n"),
4200 target_pid_to_str (retval));
4201 delete_thread (retval);
4202 status->kind = TARGET_WAITKIND_SPURIOUS;
4207 /* FIXME: Do we need to handle SYS_sproc,
4208 SYS_fork, or SYS_vfork here? The old procfs
4209 seemed to use this event to handle threads on
4210 older (non-LWP) systems, where I'm assuming
4211 that threads were actually separate processes.
4212 Irix, maybe? Anyway, low priority for now. */
4216 printf_filtered (_("procfs: trapped on exit from "));
4217 proc_prettyprint_syscall (proc_what (pi), 0);
4218 printf_filtered ("\n");
4221 long i, nsysargs, *sysargs;
4223 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4224 (sysargs = proc_sysargs (pi)) != NULL)
4226 printf_filtered (_("%ld syscall arguments:\n"), nsysargs);
4227 for (i = 0; i < nsysargs; i++)
4228 printf_filtered ("#%ld: 0x%08lx\n",
4233 status->kind = TARGET_WAITKIND_SPURIOUS;
4234 return inferior_ptid;
4239 wstat = (SIGSTOP << 8) | 0177;
4244 printf_filtered (_("Retry #%d:\n"), retry);
4245 pi->status_valid = 0;
4250 /* If not in procinfo list, add it. */
4251 temp_tid = proc_get_current_thread (pi);
4252 if (!find_procinfo (pi->pid, temp_tid))
4253 create_procinfo (pi->pid, temp_tid);
4255 /* If not in GDB's thread list, add it. */
4256 temp_ptid = MERGEPID (pi->pid, temp_tid);
4257 if (!in_thread_list (temp_ptid))
4258 add_thread (temp_ptid);
4260 status->kind = TARGET_WAITKIND_STOPPED;
4261 status->value.sig = 0;
4266 wstat = (what << 8) | 0177;
4272 wstat = (SIGTRAP << 8) | 0177;
4277 wstat = (SIGTRAP << 8) | 0177;
4280 /* FIXME: use si_signo where possible. */
4282 #if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4285 wstat = (SIGILL << 8) | 0177;
4288 #if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4291 /* If we hit our __dbx_link() internal breakpoint,
4292 then remove it. See comments in procfs_init_inferior()
4293 for more details. */
4294 if (dbx_link_bpt_addr != 0
4295 && dbx_link_bpt_addr
4296 == regcache_read_pc (get_current_regcache ()))
4297 remove_dbx_link_breakpoint ();
4299 wstat = (SIGTRAP << 8) | 0177;
4303 #if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4306 wstat = (SIGSEGV << 8) | 0177;
4310 #if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4313 wstat = (SIGFPE << 8) | 0177;
4315 case FLTPAGE: /* Recoverable page fault */
4316 default: /* FIXME: use si_signo if possible for fault */
4317 retval = pid_to_ptid (-1);
4318 printf_filtered ("procfs:%d -- ", __LINE__);
4319 printf_filtered (_("child stopped for unknown reason:\n"));
4320 proc_prettyprint_why (why, what, 1);
4321 error (_("... giving up..."));
4324 break; /* case PR_FAULTED: */
4325 default: /* switch (why) unmatched */
4326 printf_filtered ("procfs:%d -- ", __LINE__);
4327 printf_filtered (_("child stopped for unknown reason:\n"));
4328 proc_prettyprint_why (why, what, 1);
4329 error (_("... giving up..."));
4333 * Got this far without error:
4334 * If retval isn't in the threads database, add it.
4336 if (PIDGET (retval) > 0 &&
4337 !ptid_equal (retval, inferior_ptid) &&
4338 !in_thread_list (retval))
4341 * We have a new thread.
4342 * We need to add it both to GDB's list and to our own.
4343 * If we don't create a procinfo, resume may be unhappy
4346 add_thread (retval);
4347 if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL)
4348 create_procinfo (PIDGET (retval), TIDGET (retval));
4351 else /* flags do not indicate STOPPED */
4353 /* surely this can't happen... */
4354 printf_filtered ("procfs:%d -- process not stopped.\n",
4356 proc_prettyprint_flags (flags, 1);
4357 error (_("procfs: ...giving up..."));
4362 store_waitstatus (status, wstat);
4368 /* Perform a partial transfer to/from the specified object. For
4369 memory transfers, fall back to the old memory xfer functions. */
4372 procfs_xfer_partial (struct target_ops *ops, enum target_object object,
4373 const char *annex, gdb_byte *readbuf,
4374 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4378 case TARGET_OBJECT_MEMORY:
4380 return (*ops->deprecated_xfer_memory) (offset, readbuf,
4381 len, 0/*read*/, NULL, ops);
4383 return (*ops->deprecated_xfer_memory) (offset, (gdb_byte *) writebuf,
4384 len, 1/*write*/, NULL, ops);
4388 case TARGET_OBJECT_AUXV:
4389 return procfs_xfer_auxv (ops, object, annex, readbuf, writebuf,
4394 if (ops->beneath != NULL)
4395 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
4396 readbuf, writebuf, offset, len);
4402 /* Transfer LEN bytes between GDB address MYADDR and target address
4403 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4404 otherwise transfer them from the target. TARGET is unused.
4406 The return value is 0 if an error occurred or no bytes were
4407 transferred. Otherwise, it will be a positive value which
4408 indicates the number of bytes transferred between gdb and the
4409 target. (Note that the interface also makes provisions for
4410 negative values, but this capability isn't implemented here.) */
4413 procfs_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int dowrite,
4414 struct mem_attrib *attrib, struct target_ops *target)
4419 /* Find procinfo for main process */
4420 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4421 if (pi->as_fd == 0 &&
4422 open_procinfo_files (pi, FD_AS) == 0)
4424 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
4428 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
4433 PROCFS_NOTE ("write memory: ");
4435 PROCFS_NOTE ("write memory: \n");
4437 nbytes = write (pi->as_fd, myaddr, len);
4441 PROCFS_NOTE ("read memory: \n");
4442 nbytes = read (pi->as_fd, myaddr, len);
4453 * Function: invalidate_cache
4455 * Called by target_resume before making child runnable.
4456 * Mark cached registers and status's invalid.
4457 * If there are "dirty" caches that need to be written back
4458 * to the child process, do that.
4460 * File descriptors are also cached.
4461 * As they are a limited resource, we cannot hold onto them indefinitely.
4462 * However, as they are expensive to open, we don't want to throw them
4463 * away indescriminately either. As a compromise, we will keep the
4464 * file descriptors for the parent process, but discard any file
4465 * descriptors we may have accumulated for the threads.
4468 * As this function is called by iterate_over_threads, it always
4469 * returns zero (so that iterate_over_threads will keep iterating).
4474 invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
4477 * About to run the child; invalidate caches and do any other cleanup.
4481 if (pi->gregs_dirty)
4482 if (parent == NULL ||
4483 proc_get_current_thread (parent) != pi->tid)
4484 if (!proc_set_gregs (pi)) /* flush gregs cache */
4485 proc_warn (pi, "target_resume, set_gregs",
4487 if (gdbarch_fp0_regnum (target_gdbarch) >= 0)
4488 if (pi->fpregs_dirty)
4489 if (parent == NULL ||
4490 proc_get_current_thread (parent) != pi->tid)
4491 if (!proc_set_fpregs (pi)) /* flush fpregs cache */
4492 proc_warn (pi, "target_resume, set_fpregs",
4498 /* The presence of a parent indicates that this is an LWP.
4499 Close any file descriptors that it might have open.
4500 We don't do this to the master (parent) procinfo. */
4502 close_procinfo_files (pi);
4504 pi->gregs_valid = 0;
4505 pi->fpregs_valid = 0;
4507 pi->gregs_dirty = 0;
4508 pi->fpregs_dirty = 0;
4510 pi->status_valid = 0;
4511 pi->threads_valid = 0;
4518 * Function: make_signal_thread_runnable
4520 * A callback function for iterate_over_threads.
4521 * Find the asynchronous signal thread, and make it runnable.
4522 * See if that helps matters any.
4526 make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr)
4529 if (proc_flags (pi) & PR_ASLWP)
4531 if (!proc_run_process (pi, 0, -1))
4532 proc_error (pi, "make_signal_thread_runnable", __LINE__);
4541 * Function: target_resume
4543 * Make the child process runnable. Normally we will then call
4544 * procfs_wait and wait for it to stop again (unles gdb is async).
4547 * step: if true, then arrange for the child to stop again
4548 * after executing a single instruction.
4549 * signo: if zero, then cancel any pending signal.
4550 * If non-zero, then arrange for the indicated signal
4551 * to be delivered to the child when it runs.
4552 * pid: if -1, then allow any child thread to run.
4553 * if non-zero, then allow only the indicated thread to run.
4554 ******* (not implemented yet)
4558 procfs_resume (struct target_ops *ops,
4559 ptid_t ptid, int step, enum target_signal signo)
4561 procinfo *pi, *thread;
4565 prrun.prflags |= PRSVADDR;
4566 prrun.pr_vaddr = $PC; set resume address
4567 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4568 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4569 prrun.prflags |= PRCFAULT; clear current fault.
4571 PRSTRACE and PRSFAULT can be done by other means
4572 (proc_trace_signals, proc_trace_faults)
4573 PRSVADDR is unnecessary.
4574 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4575 This basically leaves PRSTEP and PRCSIG.
4576 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4577 So basically PR_STEP is the sole argument that must be passed
4578 to proc_run_process (for use in the prrun struct by ioctl). */
4580 /* Find procinfo for main process */
4581 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4583 /* First cut: ignore pid argument */
4586 /* Convert signal to host numbering. */
4588 (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop))
4591 native_signo = target_signal_to_host (signo);
4593 pi->ignore_next_sigstop = 0;
4595 /* Running the process voids all cached registers and status. */
4596 /* Void the threads' caches first */
4597 proc_iterate_over_threads (pi, invalidate_cache, NULL);
4598 /* Void the process procinfo's caches. */
4599 invalidate_cache (NULL, pi, NULL);
4601 if (PIDGET (ptid) != -1)
4603 /* Resume a specific thread, presumably suppressing the others. */
4604 thread = find_procinfo (PIDGET (ptid), TIDGET (ptid));
4607 if (thread->tid != 0)
4609 /* We're to resume a specific thread, and not the others.
4610 * Set the child process's PR_ASYNC flag.
4613 if (!proc_set_async (pi))
4614 proc_error (pi, "target_resume, set_async", __LINE__);
4617 proc_iterate_over_threads (pi,
4618 make_signal_thread_runnable,
4621 pi = thread; /* substitute the thread's procinfo for run */
4626 if (!proc_run_process (pi, step, native_signo))
4629 warning (_("resume: target already running. Pretend to resume, and hope for the best!"));
4631 proc_error (pi, "target_resume", __LINE__);
4636 * Function: register_gdb_signals
4638 * Traverse the list of signals that GDB knows about
4639 * (see "handle" command), and arrange for the target
4640 * to be stopped or not, according to these settings.
4642 * Returns non-zero for success, zero for failure.
4646 register_gdb_signals (procinfo *pi, gdb_sigset_t *signals)
4650 for (signo = 0; signo < NSIG; signo ++)
4651 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
4652 signal_print_state (target_signal_from_host (signo)) == 0 &&
4653 signal_pass_state (target_signal_from_host (signo)) == 1)
4654 gdb_prdelset (signals, signo);
4656 gdb_praddset (signals, signo);
4658 return proc_set_traced_signals (pi, signals);
4662 * Function: target_notice_signals
4664 * Set up to trace signals in the child process.
4668 procfs_notice_signals (ptid_t ptid)
4670 gdb_sigset_t signals;
4671 procinfo *pi = find_procinfo_or_die (PIDGET (ptid), 0);
4673 if (proc_get_traced_signals (pi, &signals) &&
4674 register_gdb_signals (pi, &signals))
4677 proc_error (pi, "notice_signals", __LINE__);
4681 * Function: target_files_info
4683 * Print status information about the child process.
4687 procfs_files_info (struct target_ops *ignore)
4689 struct inferior *inf = current_inferior ();
4690 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
4691 inf->attach_flag? "attached": "child",
4692 target_pid_to_str (inferior_ptid));
4696 * Function: target_stop
4698 * Stop the child process asynchronously, as when the
4699 * gdb user types control-c or presses a "stop" button.
4701 * Works by sending kill(SIGINT) to the child's process group.
4705 procfs_stop (ptid_t ptid)
4707 kill (-inferior_process_group (), SIGINT);
4711 * Function: unconditionally_kill_inferior
4713 * Make it die. Wait for it to die. Clean up after it.
4714 * Note: this should only be applied to the real process,
4715 * not to an LWP, because of the check for parent-process.
4716 * If we need this to work for an LWP, it needs some more logic.
4720 unconditionally_kill_inferior (procinfo *pi)
4724 parent_pid = proc_parent_pid (pi);
4725 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4726 /* FIXME: use access functions */
4727 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4728 before the PIOCKILL, otherwise it might generate a corrupted core
4729 file for the inferior. */
4730 if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0)
4732 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4735 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4736 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4737 to kill the inferior, otherwise it might remain stopped with a
4739 We do not check the result of the PIOCSSIG, the inferior might have
4742 gdb_siginfo_t newsiginfo;
4744 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
4745 newsiginfo.si_signo = SIGKILL;
4746 newsiginfo.si_code = 0;
4747 newsiginfo.si_errno = 0;
4748 newsiginfo.si_pid = getpid ();
4749 newsiginfo.si_uid = getuid ();
4750 /* FIXME: use proc_set_current_signal */
4751 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
4753 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4754 if (!proc_kill (pi, SIGKILL))
4755 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
4756 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4757 destroy_procinfo (pi);
4759 /* If pi is GDB's child, wait for it to die. */
4760 if (parent_pid == getpid ())
4761 /* FIXME: should we use waitpid to make sure we get the right event?
4762 Should we check the returned event? */
4767 ret = waitpid (pi->pid, &status, 0);
4775 * Function: target_kill_inferior
4777 * We're done debugging it, and we want it to go away.
4778 * Then we want GDB to forget all about it.
4782 procfs_kill_inferior (struct target_ops *ops)
4784 if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */
4786 /* Find procinfo for main process */
4787 procinfo *pi = find_procinfo (PIDGET (inferior_ptid), 0);
4790 unconditionally_kill_inferior (pi);
4791 target_mourn_inferior ();
4796 * Function: target_mourn_inferior
4798 * Forget we ever debugged this thing!
4802 procfs_mourn_inferior (struct target_ops *ops)
4806 if (!ptid_equal (inferior_ptid, null_ptid))
4808 /* Find procinfo for main process */
4809 pi = find_procinfo (PIDGET (inferior_ptid), 0);
4811 destroy_procinfo (pi);
4813 unpush_target (ops);
4815 if (dbx_link_bpt != NULL)
4817 deprecated_remove_raw_breakpoint (target_gdbarch, dbx_link_bpt);
4818 dbx_link_bpt_addr = 0;
4819 dbx_link_bpt = NULL;
4822 generic_mourn_inferior ();
4826 * Function: init_inferior
4828 * When GDB forks to create a runnable inferior process,
4829 * this function is called on the parent side of the fork.
4830 * It's job is to do whatever is necessary to make the child
4831 * ready to be debugged, and then wait for the child to synchronize.
4835 procfs_init_inferior (struct target_ops *ops, int pid)
4838 gdb_sigset_t signals;
4842 /* This routine called on the parent side (GDB side)
4843 after GDB forks the inferior. */
4846 if ((pi = create_procinfo (pid, 0)) == NULL)
4847 perror ("procfs: out of memory in 'init_inferior'");
4849 if (!open_procinfo_files (pi, FD_CTL))
4850 proc_error (pi, "init_inferior, open_proc_files", __LINE__);
4854 open_procinfo_files // done
4857 procfs_notice_signals
4864 /* If not stopped yet, wait for it to stop. */
4865 if (!(proc_flags (pi) & PR_STOPPED) &&
4866 !(proc_wait_for_stop (pi)))
4867 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
4869 /* Save some of the /proc state to be restored if we detach. */
4870 /* FIXME: Why? In case another debugger was debugging it?
4871 We're it's parent, for Ghu's sake! */
4872 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
4873 proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
4874 if (!proc_get_held_signals (pi, &pi->saved_sighold))
4875 proc_error (pi, "init_inferior, get_held_signals", __LINE__);
4876 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
4877 proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
4878 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
4879 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
4880 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
4881 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
4883 /* Register to trace selected signals in the child. */
4884 prfillset (&signals);
4885 if (!register_gdb_signals (pi, &signals))
4886 proc_error (pi, "init_inferior, register_signals", __LINE__);
4888 if ((fail = procfs_debug_inferior (pi)) != 0)
4889 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
4891 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4892 and possibly even turning ON kill-on-last-close at this point. But
4893 I can't make that change without careful testing which I don't have
4894 time to do right now... */
4895 /* Turn on run-on-last-close flag so that the child
4896 will die if GDB goes away for some reason. */
4897 if (!proc_set_run_on_last_close (pi))
4898 proc_error (pi, "init_inferior, set_RLC", __LINE__);
4900 /* We now have have access to the lwpid of the main thread/lwp. */
4901 lwpid = proc_get_current_thread (pi);
4903 /* Create a procinfo for the main lwp. */
4904 create_procinfo (pid, lwpid);
4906 /* We already have a main thread registered in the thread table at
4907 this point, but it didn't have any lwp info yet. Notify the core
4908 about it. This changes inferior_ptid as well. */
4909 thread_change_ptid (pid_to_ptid (pid),
4910 MERGEPID (pid, lwpid));
4912 /* Typically two, one trap to exec the shell, one to exec the
4913 program being debugged. Defined by "inferior.h". */
4914 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
4917 /* On mips-irix, we need to stop the inferior early enough during
4918 the startup phase in order to be able to load the shared library
4919 symbols and insert the breakpoints that are located in these shared
4920 libraries. Stopping at the program entry point is not good enough
4921 because the -init code is executed before the execution reaches
4924 So what we need to do is to insert a breakpoint in the runtime
4925 loader (rld), more precisely in __dbx_link(). This procedure is
4926 called by rld once all shared libraries have been mapped, but before
4927 the -init code is executed. Unfortuantely, this is not straightforward,
4928 as rld is not part of the executable we are running, and thus we need
4929 the inferior to run until rld itself has been mapped in memory.
4931 For this, we trace all syssgi() syscall exit events. Each time
4932 we detect such an event, we iterate over each text memory maps,
4933 get its associated fd, and scan the symbol table for __dbx_link().
4934 When found, we know that rld has been mapped, and that we can insert
4935 the breakpoint at the symbol address. Once the dbx_link() breakpoint
4936 has been inserted, the syssgi() notifications are no longer necessary,
4937 so they should be canceled. */
4938 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT, FLAG_SET, 0);
4943 * Function: set_exec_trap
4945 * When GDB forks to create a new process, this function is called
4946 * on the child side of the fork before GDB exec's the user program.
4947 * Its job is to make the child minimally debuggable, so that the
4948 * parent GDB process can connect to the child and take over.
4949 * This function should do only the minimum to make that possible,
4950 * and to synchronize with the parent process. The parent process
4951 * should take care of the details.
4955 procfs_set_exec_trap (void)
4957 /* This routine called on the child side (inferior side)
4958 after GDB forks the inferior. It must use only local variables,
4959 because it may be sharing data space with its parent. */
4964 if ((pi = create_procinfo (getpid (), 0)) == NULL)
4965 perror_with_name (_("procfs: create_procinfo failed in child."));
4967 if (open_procinfo_files (pi, FD_CTL) == 0)
4969 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
4970 gdb_flush (gdb_stderr);
4971 /* no need to call "dead_procinfo", because we're going to exit. */
4975 #ifdef PRFS_STOPEXEC /* defined on OSF */
4976 /* OSF method for tracing exec syscalls. Quoting:
4977 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4978 exits from exec system calls because of the user level loader. */
4979 /* FIXME: make nice and maybe move into an access function. */
4983 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
4985 proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__);
4986 gdb_flush (gdb_stderr);
4989 prfs_flags |= PRFS_STOPEXEC;
4991 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
4993 proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__);
4994 gdb_flush (gdb_stderr);
4998 #else /* not PRFS_STOPEXEC */
4999 /* Everyone else's (except OSF) method for tracing exec syscalls */
5001 Not all systems with /proc have all the exec* syscalls with the same
5002 names. On the SGI, for example, there is no SYS_exec, but there
5003 *is* a SYS_execv. So, we try to account for that. */
5005 exitset = sysset_t_alloc (pi);
5006 gdb_premptysysset (exitset);
5008 gdb_praddsysset (exitset, SYS_exec);
5011 gdb_praddsysset (exitset, SYS_execve);
5014 gdb_praddsysset (exitset, SYS_execv);
5016 #ifdef DYNAMIC_SYSCALLS
5018 int callnum = find_syscall (pi, "execve");
5021 gdb_praddsysset (exitset, callnum);
5023 callnum = find_syscall (pi, "ra_execve");
5025 gdb_praddsysset (exitset, callnum);
5027 #endif /* DYNAMIC_SYSCALLS */
5029 if (!proc_set_traced_sysexit (pi, exitset))
5031 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
5032 gdb_flush (gdb_stderr);
5035 #endif /* PRFS_STOPEXEC */
5037 /* FIXME: should this be done in the parent instead? */
5038 /* Turn off inherit on fork flag so that all grand-children
5039 of gdb start with tracing flags cleared. */
5040 if (!proc_unset_inherit_on_fork (pi))
5041 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
5043 /* Turn off run on last close flag, so that the child process
5044 cannot run away just because we close our handle on it.
5045 We want it to wait for the parent to attach. */
5046 if (!proc_unset_run_on_last_close (pi))
5047 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
5049 /* FIXME: No need to destroy the procinfo --
5050 we have our own address space, and we're about to do an exec! */
5051 /*destroy_procinfo (pi);*/
5055 * Function: create_inferior
5057 * This function is called BEFORE gdb forks the inferior process.
5058 * Its only real responsibility is to set things up for the fork,
5059 * and tell GDB which two functions to call after the fork (one
5060 * for the parent, and one for the child).
5062 * This function does a complicated search for a unix shell program,
5063 * which it then uses to parse arguments and environment variables
5064 * to be sent to the child. I wonder whether this code could not
5065 * be abstracted out and shared with other unix targets such as
5070 procfs_create_inferior (struct target_ops *ops, char *exec_file,
5071 char *allargs, char **env, int from_tty)
5073 char *shell_file = getenv ("SHELL");
5077 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
5080 /* We will be looking down the PATH to find shell_file. If we
5081 just do this the normal way (via execlp, which operates by
5082 attempting an exec for each element of the PATH until it
5083 finds one which succeeds), then there will be an exec for
5084 each failed attempt, each of which will cause a PR_SYSEXIT
5085 stop, and we won't know how to distinguish the PR_SYSEXIT's
5086 for these failed execs with the ones for successful execs
5087 (whether the exec has succeeded is stored at that time in the
5088 carry bit or some such architecture-specific and
5089 non-ABI-specified place).
5091 So I can't think of anything better than to search the PATH
5092 now. This has several disadvantages: (1) There is a race
5093 condition; if we find a file now and it is deleted before we
5094 exec it, we lose, even if the deletion leaves a valid file
5095 further down in the PATH, (2) there is no way to know exactly
5096 what an executable (in the sense of "capable of being
5097 exec'd") file is. Using access() loses because it may lose
5098 if the caller is the superuser; failing to use it loses if
5099 there are ACLs or some such. */
5103 /* FIXME-maybe: might want "set path" command so user can change what
5104 path is used from within GDB. */
5105 char *path = getenv ("PATH");
5107 struct stat statbuf;
5110 path = "/bin:/usr/bin";
5112 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
5113 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
5115 p1 = strchr (p, ':');
5120 strncpy (tryname, p, len);
5121 tryname[len] = '\0';
5122 strcat (tryname, "/");
5123 strcat (tryname, shell_file);
5124 if (access (tryname, X_OK) < 0)
5126 if (stat (tryname, &statbuf) < 0)
5128 if (!S_ISREG (statbuf.st_mode))
5129 /* We certainly need to reject directories. I'm not quite
5130 as sure about FIFOs, sockets, etc., but I kind of doubt
5131 that people want to exec() these things. */
5136 /* Not found. This must be an error rather than merely passing
5137 the file to execlp(), because execlp() would try all the
5138 exec()s, causing GDB to get confused. */
5139 error (_("procfs:%d -- Can't find shell %s in PATH"),
5140 __LINE__, shell_file);
5142 shell_file = tryname;
5145 pid = fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
5146 NULL, NULL, shell_file);
5148 procfs_init_inferior (ops, pid);
5151 /* Make sure to cancel the syssgi() syscall-exit notifications.
5152 They should normally have been removed by now, but they may still
5153 be activated if the inferior doesn't use shared libraries, or if
5154 we didn't locate __dbx_link, or if we never stopped in __dbx_link.
5155 See procfs_init_inferior() for more details. */
5156 proc_trace_syscalls_1 (find_procinfo_or_die (PIDGET (inferior_ptid), 0),
5157 SYS_syssgi, PR_SYSEXIT, FLAG_RESET, 0);
5162 * Function: notice_thread
5164 * Callback for find_new_threads.
5165 * Calls "add_thread".
5169 procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
5171 ptid_t gdb_threadid = MERGEPID (pi->pid, thread->tid);
5173 if (!in_thread_list (gdb_threadid) || is_exited (gdb_threadid))
5174 add_thread (gdb_threadid);
5180 * Function: target_find_new_threads
5182 * Query all the threads that the target knows about,
5183 * and give them back to GDB to add to its list.
5187 procfs_find_new_threads (struct target_ops *ops)
5191 /* Find procinfo for main process */
5192 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5193 proc_update_threads (pi);
5194 proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
5198 * Function: target_thread_alive
5200 * Return true if the thread is still 'alive'.
5202 * This guy doesn't really seem to be doing his job.
5203 * Got to investigate how to tell when a thread is really gone.
5207 procfs_thread_alive (struct target_ops *ops, ptid_t ptid)
5212 proc = PIDGET (ptid);
5213 thread = TIDGET (ptid);
5214 /* If I don't know it, it ain't alive! */
5215 if ((pi = find_procinfo (proc, thread)) == NULL)
5218 /* If I can't get its status, it ain't alive!
5219 What's more, I need to forget about it! */
5220 if (!proc_get_status (pi))
5222 destroy_procinfo (pi);
5225 /* I couldn't have got its status if it weren't alive, so it's alive. */
5229 /* Convert PTID to a string. Returns the string in a static buffer. */
5232 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
5234 static char buf[80];
5236 if (TIDGET (ptid) == 0)
5237 sprintf (buf, "process %d", PIDGET (ptid));
5239 sprintf (buf, "LWP %ld", TIDGET (ptid));
5245 * Function: procfs_set_watchpoint
5246 * Insert a watchpoint
5250 procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag,
5258 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5259 PIDGET (inferior_ptid) : PIDGET (ptid), 0);
5261 /* Translate from GDB's flags to /proc's */
5262 if (len > 0) /* len == 0 means delete watchpoint */
5264 switch (rwflag) { /* FIXME: need an enum! */
5265 case hw_write: /* default watchpoint (write) */
5266 pflags = WRITE_WATCHFLAG;
5268 case hw_read: /* read watchpoint */
5269 pflags = READ_WATCHFLAG;
5271 case hw_access: /* access watchpoint */
5272 pflags = READ_WATCHFLAG | WRITE_WATCHFLAG;
5274 case hw_execute: /* execution HW breakpoint */
5275 pflags = EXEC_WATCHFLAG;
5277 default: /* Something weird. Return error. */
5280 if (after) /* Stop after r/w access is completed. */
5281 pflags |= AFTER_WATCHFLAG;
5284 if (!proc_set_watchpoint (pi, addr, len, pflags))
5286 if (errno == E2BIG) /* Typical error for no resources */
5287 return -1; /* fail */
5288 /* GDB may try to remove the same watchpoint twice.
5289 If a remove request returns no match, don't error. */
5290 if (errno == ESRCH && len == 0)
5291 return 0; /* ignore */
5292 proc_error (pi, "set_watchpoint", __LINE__);
5295 #endif /* UNIXWARE */
5299 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
5300 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
5301 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
5304 Note: procfs_can_use_hw_breakpoint() is not yet used by all
5305 procfs.c targets due to the fact that some of them still define
5306 target_can_use_hardware_watchpoint. */
5309 procfs_can_use_hw_breakpoint (int type, int cnt, int othertype)
5311 /* Due to the way that proc_set_watchpoint() is implemented, host
5312 and target pointers must be of the same size. If they are not,
5313 we can't use hardware watchpoints. This limitation is due to the
5314 fact that proc_set_watchpoint() calls
5315 procfs_address_to_host_pointer(); a close inspection of
5316 procfs_address_to_host_pointer will reveal that an internal error
5317 will be generated when the host and target pointer sizes are
5319 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
5320 if (sizeof (void *) != TYPE_LENGTH (ptr_type))
5323 /* Other tests here??? */
5329 * Function: stopped_by_watchpoint
5331 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5332 * else returns zero.
5336 procfs_stopped_by_watchpoint (void)
5340 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5342 if (!pi) /* If no process, then not stopped by watchpoint! */
5345 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
5347 if (proc_why (pi) == PR_FAULTED)
5350 if (proc_what (pi) == FLTWATCH)
5354 if (proc_what (pi) == FLTKWATCH)
5363 procfs_insert_watchpoint (CORE_ADDR addr, int len, int type)
5365 if (!target_have_steppable_watchpoint
5366 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch))
5368 /* When a hardware watchpoint fires off the PC will be left at
5369 the instruction following the one which caused the
5370 watchpoint. It will *NOT* be necessary for GDB to step over
5372 return procfs_set_watchpoint (inferior_ptid, addr, len, type, 1);
5376 /* When a hardware watchpoint fires off the PC will be left at
5377 the instruction which caused the watchpoint. It will be
5378 necessary for GDB to step over the watchpoint. */
5379 return procfs_set_watchpoint (inferior_ptid, addr, len, type, 0);
5384 procfs_remove_watchpoint (CORE_ADDR addr, int len, int type)
5386 return procfs_set_watchpoint (inferior_ptid, addr, 0, 0, 0);
5390 procfs_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
5392 /* The man page for proc(4) on Solaris 2.6 and up says that the
5393 system can support "thousands" of hardware watchpoints, but gives
5394 no method for finding out how many; It doesn't say anything about
5395 the allowed size for the watched area either. So we just tell
5401 procfs_use_watchpoints (struct target_ops *t)
5403 t->to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
5404 t->to_insert_watchpoint = procfs_insert_watchpoint;
5405 t->to_remove_watchpoint = procfs_remove_watchpoint;
5406 t->to_region_ok_for_hw_watchpoint = procfs_region_ok_for_hw_watchpoint;
5407 t->to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
5411 * Memory Mappings Functions:
5415 * Function: iterate_over_mappings
5417 * Call a callback function once for each mapping, passing it the mapping,
5418 * an optional secondary callback function, and some optional opaque data.
5419 * Quit and return the first non-zero value returned from the callback.
5422 * pi -- procinfo struct for the process to be mapped.
5423 * func -- callback function to be called by this iterator.
5424 * data -- optional opaque data to be passed to the callback function.
5425 * child_func -- optional secondary function pointer to be passed
5426 * to the child function.
5428 * Return: First non-zero return value from the callback function,
5433 iterate_over_mappings (procinfo *pi, int (*child_func) (), void *data,
5434 int (*func) (struct prmap *map,
5435 int (*child_func) (),
5438 char pathname[MAX_PROC_NAME_SIZE];
5439 struct prmap *prmaps;
5440 struct prmap *prmap;
5448 /* Get the number of mappings, allocate space,
5449 and read the mappings into prmaps. */
5452 sprintf (pathname, "/proc/%d/map", pi->pid);
5453 if ((map_fd = open (pathname, O_RDONLY)) < 0)
5454 proc_error (pi, "iterate_over_mappings (open)", __LINE__);
5456 /* Make sure it gets closed again. */
5457 make_cleanup_close (map_fd);
5459 /* Use stat to determine the file size, and compute
5460 the number of prmap_t objects it contains. */
5461 if (fstat (map_fd, &sbuf) != 0)
5462 proc_error (pi, "iterate_over_mappings (fstat)", __LINE__);
5464 nmap = sbuf.st_size / sizeof (prmap_t);
5465 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5466 if (read (map_fd, (char *) prmaps, nmap * sizeof (*prmaps))
5467 != (nmap * sizeof (*prmaps)))
5468 proc_error (pi, "iterate_over_mappings (read)", __LINE__);
5470 /* Use ioctl command PIOCNMAP to get number of mappings. */
5471 if (ioctl (pi->ctl_fd, PIOCNMAP, &nmap) != 0)
5472 proc_error (pi, "iterate_over_mappings (PIOCNMAP)", __LINE__);
5474 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5475 if (ioctl (pi->ctl_fd, PIOCMAP, prmaps) != 0)
5476 proc_error (pi, "iterate_over_mappings (PIOCMAP)", __LINE__);
5479 for (prmap = prmaps; nmap > 0; prmap++, nmap--)
5480 if ((funcstat = (*func) (prmap, child_func, data)) != 0)
5487 * Function: solib_mappings_callback
5489 * Calls the supplied callback function once for each mapped address
5490 * space in the process. The callback function receives an open
5491 * file descriptor for the file corresponding to that mapped
5492 * address space (if there is one), and the base address of the
5493 * mapped space. Quit when the callback function returns a
5494 * nonzero value, or at teh end of the mappings.
5496 * Returns: the first non-zero return value of the callback function,
5500 int solib_mappings_callback (struct prmap *map,
5501 int (*func) (int, CORE_ADDR),
5504 procinfo *pi = data;
5508 char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)];
5510 if (map->pr_vaddr == 0 && map->pr_size == 0)
5511 return -1; /* sanity */
5513 if (map->pr_mapname[0] == 0)
5515 fd = -1; /* no map file */
5519 sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname);
5520 /* Note: caller's responsibility to close this fd! */
5521 fd = open_with_retry (name, O_RDONLY);
5522 /* Note: we don't test the above call for failure;
5523 we just pass the FD on as given. Sometimes there is
5524 no file, so the open may return failure, but that's
5528 fd = ioctl (pi->ctl_fd, PIOCOPENM, &map->pr_vaddr);
5529 /* Note: we don't test the above call for failure;
5530 we just pass the FD on as given. Sometimes there is
5531 no file, so the ioctl may return failure, but that's
5534 return (*func) (fd, (CORE_ADDR) map->pr_vaddr);
5538 * Function: find_memory_regions_callback
5540 * Implements the to_find_memory_regions method.
5541 * Calls an external function for each memory region.
5542 * External function will have the signiture:
5544 * int callback (CORE_ADDR vaddr,
5545 * unsigned long size,
5546 * int read, int write, int execute,
5549 * Returns the integer value returned by the callback.
5553 find_memory_regions_callback (struct prmap *map,
5554 int (*func) (CORE_ADDR,
5560 return (*func) ((CORE_ADDR) map->pr_vaddr,
5562 (map->pr_mflags & MA_READ) != 0,
5563 (map->pr_mflags & MA_WRITE) != 0,
5564 (map->pr_mflags & MA_EXEC) != 0,
5569 * Function: proc_find_memory_regions
5571 * External interface. Calls a callback function once for each
5572 * mapped memory region in the child process, passing as arguments
5573 * CORE_ADDR virtual_address,
5574 * unsigned long size,
5575 * int read, TRUE if region is readable by the child
5576 * int write, TRUE if region is writable by the child
5577 * int execute TRUE if region is executable by the child.
5579 * Stops iterating and returns the first non-zero value
5580 * returned by the callback.
5584 proc_find_memory_regions (int (*func) (CORE_ADDR,
5590 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5592 return iterate_over_mappings (pi, func, data,
5593 find_memory_regions_callback);
5596 /* Remove the breakpoint that we inserted in __dbx_link().
5597 Does nothing if the breakpoint hasn't been inserted or has already
5601 remove_dbx_link_breakpoint (void)
5603 if (dbx_link_bpt_addr == 0)
5606 if (deprecated_remove_raw_breakpoint (target_gdbarch, dbx_link_bpt) != 0)
5607 warning (_("Unable to remove __dbx_link breakpoint."));
5609 dbx_link_bpt_addr = 0;
5610 dbx_link_bpt = NULL;
5613 /* Return the address of the __dbx_link() function in the file
5614 refernced by ABFD by scanning its symbol table. Return 0 if
5615 the symbol was not found. */
5618 dbx_link_addr (bfd *abfd)
5620 long storage_needed;
5621 asymbol **symbol_table;
5622 long number_of_symbols;
5625 storage_needed = bfd_get_symtab_upper_bound (abfd);
5626 if (storage_needed <= 0)
5629 symbol_table = (asymbol **) xmalloc (storage_needed);
5630 make_cleanup (xfree, symbol_table);
5632 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
5634 for (i = 0; i < number_of_symbols; i++)
5636 asymbol *sym = symbol_table[i];
5638 if ((sym->flags & BSF_GLOBAL)
5639 && sym->name != NULL && strcmp (sym->name, "__dbx_link") == 0)
5640 return (sym->value + sym->section->vma);
5643 /* Symbol not found, return NULL. */
5647 /* Search the symbol table of the file referenced by FD for a symbol
5648 named __dbx_link(). If found, then insert a breakpoint at this location,
5649 and return nonzero. Return zero otherwise. */
5652 insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
5655 long storage_needed;
5658 abfd = bfd_fdopenr ("unamed", 0, fd);
5661 warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
5665 if (!bfd_check_format (abfd, bfd_object))
5667 /* Not the correct format, so we can not possibly find the dbx_link
5673 sym_addr = dbx_link_addr (abfd);
5676 /* Insert the breakpoint. */
5677 dbx_link_bpt_addr = sym_addr;
5678 dbx_link_bpt = deprecated_insert_raw_breakpoint (target_gdbarch, NULL,
5680 if (dbx_link_bpt == NULL)
5682 warning (_("Failed to insert dbx_link breakpoint."));
5694 /* If the given memory region MAP contains a symbol named __dbx_link,
5695 insert a breakpoint at this location and return nonzero. Return
5699 insert_dbx_link_bpt_in_region (struct prmap *map,
5700 int (*child_func) (),
5703 procinfo *pi = (procinfo *) data;
5705 /* We know the symbol we're looking for is in a text region, so
5706 only look for it if the region is a text one. */
5707 if (map->pr_mflags & MA_EXEC)
5708 return solib_mappings_callback (map, insert_dbx_link_bpt_in_file, pi);
5713 /* Search all memory regions for a symbol named __dbx_link. If found,
5714 insert a breakpoint at its location, and return nonzero. Return zero
5718 insert_dbx_link_breakpoint (procinfo *pi)
5720 return iterate_over_mappings (pi, NULL, pi, insert_dbx_link_bpt_in_region);
5724 * Function: mappingflags
5726 * Returns an ascii representation of a memory mapping's flags.
5730 mappingflags (long flags)
5732 static char asciiflags[8];
5734 strcpy (asciiflags, "-------");
5735 #if defined (MA_PHYS)
5736 if (flags & MA_PHYS)
5737 asciiflags[0] = 'd';
5739 if (flags & MA_STACK)
5740 asciiflags[1] = 's';
5741 if (flags & MA_BREAK)
5742 asciiflags[2] = 'b';
5743 if (flags & MA_SHARED)
5744 asciiflags[3] = 's';
5745 if (flags & MA_READ)
5746 asciiflags[4] = 'r';
5747 if (flags & MA_WRITE)
5748 asciiflags[5] = 'w';
5749 if (flags & MA_EXEC)
5750 asciiflags[6] = 'x';
5751 return (asciiflags);
5755 * Function: info_mappings_callback
5757 * Callback function, does the actual work for 'info proc mappings'.
5761 info_mappings_callback (struct prmap *map, int (*ignore) (), void *unused)
5763 unsigned int pr_off;
5765 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5766 pr_off = (unsigned int) map->pr_offset;
5768 pr_off = map->pr_off;
5771 if (gdbarch_addr_bit (target_gdbarch) == 32)
5772 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
5773 (unsigned long) map->pr_vaddr,
5774 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5775 (unsigned long) map->pr_size,
5777 mappingflags (map->pr_mflags));
5779 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
5780 (unsigned long) map->pr_vaddr,
5781 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5782 (unsigned long) map->pr_size,
5784 mappingflags (map->pr_mflags));
5790 * Function: info_proc_mappings
5792 * Implement the "info proc mappings" subcommand.
5796 info_proc_mappings (procinfo *pi, int summary)
5799 return; /* No output for summary mode. */
5801 printf_filtered (_("Mapped address spaces:\n\n"));
5802 if (gdbarch_ptr_bit (target_gdbarch) == 32)
5803 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
5810 printf_filtered (" %18s %18s %10s %10s %7s\n",
5817 iterate_over_mappings (pi, NULL, NULL, info_mappings_callback);
5818 printf_filtered ("\n");
5822 * Function: info_proc_cmd
5824 * Implement the "info proc" command.
5828 info_proc_cmd (char *args, int from_tty)
5830 struct cleanup *old_chain;
5831 procinfo *process = NULL;
5832 procinfo *thread = NULL;
5839 old_chain = make_cleanup (null_cleanup, 0);
5842 argv = gdb_buildargv (args);
5843 make_cleanup_freeargv (argv);
5845 while (argv != NULL && *argv != NULL)
5847 if (isdigit (argv[0][0]))
5849 pid = strtoul (argv[0], &tmp, 10);
5851 tid = strtoul (++tmp, NULL, 10);
5853 else if (argv[0][0] == '/')
5855 tid = strtoul (argv[0] + 1, NULL, 10);
5857 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
5868 pid = PIDGET (inferior_ptid);
5870 error (_("No current process: you must name one."));
5873 /* Have pid, will travel.
5874 First see if it's a process we're already debugging. */
5875 process = find_procinfo (pid, 0);
5876 if (process == NULL)
5878 /* No. So open a procinfo for it, but
5879 remember to close it again when finished. */
5880 process = create_procinfo (pid, 0);
5881 make_cleanup (do_destroy_procinfo_cleanup, process);
5882 if (!open_procinfo_files (process, FD_CTL))
5883 proc_error (process, "info proc, open_procinfo_files", __LINE__);
5887 thread = create_procinfo (pid, tid);
5891 printf_filtered (_("process %d flags:\n"), process->pid);
5892 proc_prettyprint_flags (proc_flags (process), 1);
5893 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
5894 proc_prettyprint_why (proc_why (process), proc_what (process), 1);
5895 if (proc_get_nthreads (process) > 1)
5896 printf_filtered ("Process has %d threads.\n",
5897 proc_get_nthreads (process));
5901 printf_filtered (_("thread %d flags:\n"), thread->tid);
5902 proc_prettyprint_flags (proc_flags (thread), 1);
5903 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
5904 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
5909 info_proc_mappings (process, 0);
5912 do_cleanups (old_chain);
5915 /* Modify the status of the system call identified by SYSCALLNUM in
5916 the set of syscalls that are currently traced/debugged.
5918 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
5919 will be updated. Otherwise, the exit syscalls set will be updated.
5921 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
5922 will be disabled. */
5925 proc_trace_syscalls_1 (procinfo *pi, int syscallnum, int entry_or_exit,
5926 int mode, int from_tty)
5930 if (entry_or_exit == PR_SYSENTRY)
5931 sysset = proc_get_traced_sysentry (pi, NULL);
5933 sysset = proc_get_traced_sysexit (pi, NULL);
5936 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
5938 if (mode == FLAG_SET)
5939 gdb_praddsysset (sysset, syscallnum);
5941 gdb_prdelsysset (sysset, syscallnum);
5943 if (entry_or_exit == PR_SYSENTRY)
5945 if (!proc_set_traced_sysentry (pi, sysset))
5946 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
5950 if (!proc_set_traced_sysexit (pi, sysset))
5951 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
5956 proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode)
5960 if (PIDGET (inferior_ptid) <= 0)
5961 error (_("you must be debugging a process to use this command."));
5963 if (args == NULL || args[0] == 0)
5964 error_no_arg (_("system call to trace"));
5966 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5967 if (isdigit (args[0]))
5969 const int syscallnum = atoi (args);
5971 proc_trace_syscalls_1 (pi, syscallnum, entry_or_exit, mode, from_tty);
5976 proc_trace_sysentry_cmd (char *args, int from_tty)
5978 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
5982 proc_trace_sysexit_cmd (char *args, int from_tty)
5984 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
5988 proc_untrace_sysentry_cmd (char *args, int from_tty)
5990 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
5994 proc_untrace_sysexit_cmd (char *args, int from_tty)
5996 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
6001 _initialize_procfs (void)
6003 add_info ("proc", info_proc_cmd, _("\
6004 Show /proc process information about any running process.\n\
6005 Specify process id, or use the program being debugged by default.\n\
6006 Specify keyword 'mappings' for detailed info on memory mappings."));
6007 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
6008 _("Give a trace of entries into the syscall."));
6009 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
6010 _("Give a trace of exits from the syscall."));
6011 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
6012 _("Cancel a trace of entries into the syscall."));
6013 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
6014 _("Cancel a trace of exits from the syscall."));
6017 /* =================== END, GDB "MODULE" =================== */
6021 /* miscellaneous stubs: */
6022 /* The following satisfy a few random symbols mostly created by */
6023 /* the solaris threads implementation, which I will chase down */
6027 * Return a pid for which we guarantee
6028 * we will be able to find a 'live' procinfo.
6032 procfs_first_available (void)
6034 return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1);
6038 find_signalled_thread (struct thread_info *info, void *data)
6040 if (info->stop_signal != TARGET_SIGNAL_0
6041 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
6047 static enum target_signal
6048 find_stop_signal (void)
6050 struct thread_info *info =
6051 iterate_over_threads (find_signalled_thread, NULL);
6054 return info->stop_signal;
6056 return TARGET_SIGNAL_0;
6059 /* =================== GCORE .NOTE "MODULE" =================== */
6060 #if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT)
6061 /* gcore only implemented on solaris and unixware (so far) */
6064 procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
6065 char *note_data, int *note_size,
6066 enum target_signal stop_signal)
6068 struct regcache *regcache = get_thread_regcache (ptid);
6069 gdb_gregset_t gregs;
6070 gdb_fpregset_t fpregs;
6071 unsigned long merged_pid;
6072 struct cleanup *old_chain;
6074 merged_pid = TIDGET (ptid) << 16 | PIDGET (ptid);
6076 /* This part is the old method for fetching registers.
6077 It should be replaced by the newer one using regsets
6078 once it is implemented in this platform:
6079 gdbarch_regset_from_core_section() and regset->collect_regset(). */
6081 old_chain = save_inferior_ptid ();
6082 inferior_ptid = ptid;
6083 target_fetch_registers (regcache, -1);
6085 fill_gregset (regcache, &gregs, -1);
6086 #if defined (NEW_PROC_API)
6087 note_data = (char *) elfcore_write_lwpstatus (obfd,
6094 note_data = (char *) elfcore_write_prstatus (obfd,
6101 fill_fpregset (regcache, &fpregs, -1);
6102 note_data = (char *) elfcore_write_prfpreg (obfd,
6108 do_cleanups (old_chain);
6113 struct procfs_corefile_thread_data {
6117 enum target_signal stop_signal;
6121 procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data)
6123 struct procfs_corefile_thread_data *args = data;
6127 ptid_t ptid = MERGEPID (pi->pid, thread->tid);
6128 args->note_data = procfs_do_thread_registers (args->obfd, ptid,
6137 procfs_make_note_section (bfd *obfd, int *note_size)
6139 struct cleanup *old_chain;
6140 gdb_gregset_t gregs;
6141 gdb_fpregset_t fpregs;
6142 char fname[16] = {'\0'};
6143 char psargs[80] = {'\0'};
6144 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
6145 char *note_data = NULL;
6147 struct procfs_corefile_thread_data thread_args;
6150 enum target_signal stop_signal;
6152 if (get_exec_file (0))
6154 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
6155 strncpy (psargs, get_exec_file (0),
6158 inf_args = get_inferior_args ();
6159 if (inf_args && *inf_args &&
6160 strlen (inf_args) < ((int) sizeof (psargs) - (int) strlen (psargs)))
6162 strncat (psargs, " ",
6163 sizeof (psargs) - strlen (psargs));
6164 strncat (psargs, inf_args,
6165 sizeof (psargs) - strlen (psargs));
6169 note_data = (char *) elfcore_write_prpsinfo (obfd,
6175 stop_signal = find_stop_signal ();
6178 fill_gregset (get_current_regcache (), &gregs, -1);
6179 note_data = elfcore_write_pstatus (obfd, note_data, note_size,
6180 PIDGET (inferior_ptid),
6181 stop_signal, &gregs);
6184 thread_args.obfd = obfd;
6185 thread_args.note_data = note_data;
6186 thread_args.note_size = note_size;
6187 thread_args.stop_signal = stop_signal;
6188 proc_iterate_over_threads (pi, procfs_corefile_thread_callback, &thread_args);
6190 /* There should be always at least one thread. */
6191 gdb_assert (thread_args.note_data != note_data);
6192 note_data = thread_args.note_data;
6194 auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV,
6198 note_data = elfcore_write_note (obfd, note_data, note_size,
6199 "CORE", NT_AUXV, auxv, auxv_len);
6203 make_cleanup (xfree, note_data);
6206 #else /* !(Solaris or Unixware) */
6208 procfs_make_note_section (bfd *obfd, int *note_size)
6210 error (_("gcore not implemented for this host."));
6211 return NULL; /* lint */
6213 #endif /* Solaris or Unixware */
6214 /* =================== END GCORE .NOTE "MODULE" =================== */