1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
3 Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 Written by Michael Snyder at Cygnus Solutions.
6 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 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>
54 /* This module provides the interface between GDB and the
55 /proc file system, which is used on many versions of Unix
56 as a means for debuggers to control other processes.
58 Examples of the systems that use this interface are:
65 /proc works by imitating a file system: you open a simulated file
66 that represents the process you wish to interact with, and perform
67 operations on that "file" in order to examine or change the state
70 The most important thing to know about /proc and this module is
71 that there are two very different interfaces to /proc:
73 One that uses the ioctl system call, and another that uses read
74 and write system calls.
76 This module has to support both /proc interfaces. This means that
77 there are two different ways of doing every basic operation.
79 In order to keep most of the code simple and clean, I have defined
80 an interface "layer" which hides all these system calls. An ifdef
81 (NEW_PROC_API) determines which interface we are using, and most or
82 all occurrances of this ifdef should be confined to this interface
85 /* Determine which /proc API we are using: The ioctl API defines
86 PIOCSTATUS, while the read/write (multiple fd) API never does. */
89 #include <sys/types.h>
90 #include <dirent.h> /* opendir/readdir, for listing the LWP's */
93 #include <fcntl.h> /* for O_RDONLY */
94 #include <unistd.h> /* for "X_OK" */
95 #include <sys/stat.h> /* for struct stat */
97 /* Note: procfs-utils.h must be included after the above system header
98 files, because it redefines various system calls using macros.
99 This may be incompatible with the prototype declarations. */
101 #include "proc-utils.h"
103 /* Prototypes for supply_gregset etc. */
106 /* =================== TARGET_OPS "MODULE" =================== */
108 /* This module defines the GDB target vector and its methods. */
110 static void procfs_attach (struct target_ops *, const char *, int);
111 static void procfs_detach (struct target_ops *, const char *, int);
112 static void procfs_resume (struct target_ops *,
113 ptid_t, int, enum gdb_signal);
114 static void procfs_stop (struct target_ops *self, ptid_t);
115 static void procfs_files_info (struct target_ops *);
116 static void procfs_fetch_registers (struct target_ops *,
117 struct regcache *, int);
118 static void procfs_store_registers (struct target_ops *,
119 struct regcache *, int);
120 static void procfs_pass_signals (struct target_ops *self,
121 int, unsigned char *);
122 static void procfs_kill_inferior (struct target_ops *ops);
123 static void procfs_mourn_inferior (struct target_ops *ops);
124 static void procfs_create_inferior (struct target_ops *, char *,
125 char *, char **, int);
126 static ptid_t procfs_wait (struct target_ops *,
127 ptid_t, struct target_waitstatus *, int);
128 static enum target_xfer_status procfs_xfer_memory (gdb_byte *,
132 static target_xfer_partial_ftype procfs_xfer_partial;
134 static int procfs_thread_alive (struct target_ops *ops, ptid_t);
136 static void procfs_find_new_threads (struct target_ops *ops);
137 static char *procfs_pid_to_str (struct target_ops *, ptid_t);
139 static int proc_find_memory_regions (struct target_ops *self,
140 find_memory_region_ftype, void *);
142 static char * procfs_make_note_section (struct target_ops *self,
145 static int procfs_can_use_hw_breakpoint (struct target_ops *self,
148 static void procfs_info_proc (struct target_ops *, const char *,
149 enum info_proc_what);
151 #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
152 /* When GDB is built as 64-bit application on Solaris, the auxv data
153 is presented in 64-bit format. We need to provide a custom parser
156 procfs_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
157 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
159 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
160 gdb_byte *ptr = *readptr;
165 if (endptr - ptr < 8 * 2)
168 *typep = extract_unsigned_integer (ptr, 4, byte_order);
170 /* The size of data is always 64-bit. If the application is 32-bit,
171 it will be zero extended, as expected. */
172 *valp = extract_unsigned_integer (ptr, 8, byte_order);
183 struct target_ops *t = inf_child_target ();
185 t->to_create_inferior = procfs_create_inferior;
186 t->to_kill = procfs_kill_inferior;
187 t->to_mourn_inferior = procfs_mourn_inferior;
188 t->to_attach = procfs_attach;
189 t->to_detach = procfs_detach;
190 t->to_wait = procfs_wait;
191 t->to_resume = procfs_resume;
192 t->to_fetch_registers = procfs_fetch_registers;
193 t->to_store_registers = procfs_store_registers;
194 t->to_xfer_partial = procfs_xfer_partial;
195 t->to_pass_signals = procfs_pass_signals;
196 t->to_files_info = procfs_files_info;
197 t->to_stop = procfs_stop;
199 t->to_find_new_threads = procfs_find_new_threads;
200 t->to_thread_alive = procfs_thread_alive;
201 t->to_pid_to_str = procfs_pid_to_str;
203 t->to_has_thread_control = tc_schedlock;
204 t->to_find_memory_regions = proc_find_memory_regions;
205 t->to_make_corefile_notes = procfs_make_note_section;
206 t->to_info_proc = procfs_info_proc;
208 #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
209 t->to_auxv_parse = procfs_auxv_parse;
212 t->to_magic = OPS_MAGIC;
217 /* =================== END, TARGET_OPS "MODULE" =================== */
219 /* World Unification:
221 Put any typedefs, defines etc. here that are required for the
222 unification of code that handles different versions of /proc. */
224 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
226 enum { READ_WATCHFLAG = WA_READ,
227 WRITE_WATCHFLAG = WA_WRITE,
228 EXEC_WATCHFLAG = WA_EXEC,
229 AFTER_WATCHFLAG = WA_TRAPAFTER
232 #else /* Irix method for watchpoints */
233 enum { READ_WATCHFLAG = MA_READ,
234 WRITE_WATCHFLAG = MA_WRITE,
235 EXEC_WATCHFLAG = MA_EXEC,
236 AFTER_WATCHFLAG = 0 /* trapafter not implemented */
241 #ifdef HAVE_PR_SIGSET_T
242 typedef pr_sigset_t gdb_sigset_t;
244 typedef sigset_t gdb_sigset_t;
248 #ifdef HAVE_PR_SIGACTION64_T
249 typedef pr_sigaction64_t gdb_sigaction_t;
251 typedef struct sigaction gdb_sigaction_t;
255 #ifdef HAVE_PR_SIGINFO64_T
256 typedef pr_siginfo64_t gdb_siginfo_t;
258 typedef siginfo_t gdb_siginfo_t;
261 /* On mips-irix, praddset and prdelset are defined in such a way that
262 they return a value, which causes GCC to emit a -Wunused error
263 because the returned value is not used. Prevent this warning
264 by casting the return value to void. On sparc-solaris, this issue
265 does not exist because the definition of these macros already include
266 that cast to void. */
267 #define gdb_praddset(sp, flag) ((void) praddset (sp, flag))
268 #define gdb_prdelset(sp, flag) ((void) prdelset (sp, flag))
270 /* gdb_premptysysset */
272 #define gdb_premptysysset premptysysset
274 #define gdb_premptysysset premptyset
279 #define gdb_praddsysset praddsysset
281 #define gdb_praddsysset gdb_praddset
286 #define gdb_prdelsysset prdelsysset
288 #define gdb_prdelsysset gdb_prdelset
291 /* prissyssetmember */
292 #ifdef prissyssetmember
293 #define gdb_pr_issyssetmember prissyssetmember
295 #define gdb_pr_issyssetmember prismember
298 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
299 as intuitively descriptive as it could be, so we'll define
300 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
301 this writing, this feature is only found on AIX5 systems and
302 basically means that the set of syscalls is not fixed. I.e,
303 there's no nice table that one can #include to get all of the
304 syscall numbers. Instead, they're stored in /proc/PID/sysent
305 for each process. We are at least guaranteed that they won't
306 change over the lifetime of the process. But each process could
307 (in theory) have different syscall numbers. */
308 #ifdef HAVE_PRSYSENT_T
309 #define DYNAMIC_SYSCALLS
314 /* =================== STRUCT PROCINFO "MODULE" =================== */
316 /* FIXME: this comment will soon be out of date W.R.T. threads. */
318 /* The procinfo struct is a wrapper to hold all the state information
319 concerning a /proc process. There should be exactly one procinfo
320 for each process, and since GDB currently can debug only one
321 process at a time, that means there should be only one procinfo.
322 All of the LWP's of a process can be accessed indirectly thru the
323 single process procinfo.
325 However, against the day when GDB may debug more than one process,
326 this data structure is kept in a list (which for now will hold no
327 more than one member), and many functions will have a pointer to a
328 procinfo as an argument.
330 There will be a separate procinfo structure for use by the (not yet
331 implemented) "info proc" command, so that we can print useful
332 information about any random process without interfering with the
333 inferior's procinfo information. */
336 /* format strings for /proc paths */
337 # ifndef CTL_PROC_NAME_FMT
338 # define MAIN_PROC_NAME_FMT "/proc/%d"
339 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
340 # define AS_PROC_NAME_FMT "/proc/%d/as"
341 # define MAP_PROC_NAME_FMT "/proc/%d/map"
342 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
343 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
345 /* the name of the proc status struct depends on the implementation */
346 typedef pstatus_t gdb_prstatus_t;
347 typedef lwpstatus_t gdb_lwpstatus_t;
348 #else /* ! NEW_PROC_API */
349 /* format strings for /proc paths */
350 # ifndef CTL_PROC_NAME_FMT
351 # define MAIN_PROC_NAME_FMT "/proc/%05d"
352 # define CTL_PROC_NAME_FMT "/proc/%05d"
353 # define AS_PROC_NAME_FMT "/proc/%05d"
354 # define MAP_PROC_NAME_FMT "/proc/%05d"
355 # define STATUS_PROC_NAME_FMT "/proc/%05d"
356 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
358 /* The name of the proc status struct depends on the implementation. */
359 typedef prstatus_t gdb_prstatus_t;
360 typedef prstatus_t gdb_lwpstatus_t;
361 #endif /* NEW_PROC_API */
363 typedef struct procinfo {
364 struct procinfo *next;
365 int pid; /* Process ID */
366 int tid; /* Thread/LWP id */
370 int ignore_next_sigstop;
372 /* The following four fd fields may be identical, or may contain
373 several different fd's, depending on the version of /proc
374 (old ioctl or new read/write). */
376 int ctl_fd; /* File descriptor for /proc control file */
378 /* The next three file descriptors are actually only needed in the
379 read/write, multiple-file-descriptor implemenation
380 (NEW_PROC_API). However, to avoid a bunch of #ifdefs in the
381 code, we will use them uniformly by (in the case of the ioctl
382 single-file-descriptor implementation) filling them with copies
383 of the control fd. */
384 int status_fd; /* File descriptor for /proc status file */
385 int as_fd; /* File descriptor for /proc as file */
387 char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */
389 fltset_t saved_fltset; /* Saved traced hardware fault set */
390 gdb_sigset_t saved_sigset; /* Saved traced signal set */
391 gdb_sigset_t saved_sighold; /* Saved held signal set */
392 sysset_t *saved_exitset; /* Saved traced system call exit set */
393 sysset_t *saved_entryset; /* Saved traced system call entry set */
395 gdb_prstatus_t prstatus; /* Current process status info */
398 gdb_fpregset_t fpregset; /* Current floating point registers */
401 #ifdef DYNAMIC_SYSCALLS
402 int num_syscalls; /* Total number of syscalls */
403 char **syscall_names; /* Syscall number to name map */
406 struct procinfo *thread_list;
408 int status_valid : 1;
410 int fpregs_valid : 1;
411 int threads_valid: 1;
414 static char errmsg[128]; /* shared error msg buffer */
416 /* Function prototypes for procinfo module: */
418 static procinfo *find_procinfo_or_die (int pid, int tid);
419 static procinfo *find_procinfo (int pid, int tid);
420 static procinfo *create_procinfo (int pid, int tid);
421 static void destroy_procinfo (procinfo * p);
422 static void do_destroy_procinfo_cleanup (void *);
423 static void dead_procinfo (procinfo * p, char *msg, int killp);
424 static int open_procinfo_files (procinfo * p, int which);
425 static void close_procinfo_files (procinfo * p);
426 static int sysset_t_size (procinfo *p);
427 static sysset_t *sysset_t_alloc (procinfo * pi);
428 #ifdef DYNAMIC_SYSCALLS
429 static void load_syscalls (procinfo *pi);
430 static void free_syscalls (procinfo *pi);
431 static int find_syscall (procinfo *pi, char *name);
432 #endif /* DYNAMIC_SYSCALLS */
434 static int iterate_over_mappings
435 (procinfo *pi, find_memory_region_ftype child_func, void *data,
436 int (*func) (struct prmap *map, find_memory_region_ftype child_func,
439 /* The head of the procinfo list: */
440 static procinfo * procinfo_list;
442 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
446 find_procinfo (int pid, int tid)
450 for (pi = procinfo_list; pi; pi = pi->next)
457 /* Don't check threads_valid. If we're updating the
458 thread_list, we want to find whatever threads are already
459 here. This means that in general it is the caller's
460 responsibility to check threads_valid and update before
461 calling find_procinfo, if the caller wants to find a new
464 for (pi = pi->thread_list; pi; pi = pi->next)
472 /* Calls find_procinfo, but errors on failure. */
475 find_procinfo_or_die (int pid, int tid)
477 procinfo *pi = find_procinfo (pid, tid);
482 error (_("procfs: couldn't find pid %d "
483 "(kernel thread %d) in procinfo list."),
486 error (_("procfs: couldn't find pid %d in procinfo list."), pid);
491 /* Wrapper for `open'. The appropriate open call is attempted; if
492 unsuccessful, it will be retried as many times as needed for the
493 EAGAIN and EINTR conditions.
495 For other conditions, retry the open a limited number of times. In
496 addition, a short sleep is imposed prior to retrying the open. The
497 reason for this sleep is to give the kernel a chance to catch up
498 and create the file in question in the event that GDB "wins" the
499 race to open a file before the kernel has created it. */
502 open_with_retry (const char *pathname, int flags)
504 int retries_remaining, status;
506 retries_remaining = 2;
510 status = open (pathname, flags);
512 if (status >= 0 || retries_remaining == 0)
514 else if (errno != EINTR && errno != EAGAIN)
524 /* Open the file descriptor for the process or LWP. If NEW_PROC_API
525 is defined, we only open the control file descriptor; the others
526 are opened lazily as needed. Otherwise (if not NEW_PROC_API),
527 there is only one real file descriptor, but we keep multiple copies
528 of it so that the code that uses them does not have to be #ifdef'd.
529 Returns the file descriptor, or zero for failure. */
531 enum { FD_CTL, FD_STATUS, FD_AS };
534 open_procinfo_files (procinfo *pi, int which)
537 char tmp[MAX_PROC_NAME_SIZE];
541 /* This function is getting ALMOST long enough to break up into
542 several. Here is some rationale:
544 NEW_PROC_API (Solaris 2.6, Solaris 2.7):
545 There are several file descriptors that may need to be open
546 for any given process or LWP. The ones we're intereted in are:
547 - control (ctl) write-only change the state
548 - status (status) read-only query the state
549 - address space (as) read/write access memory
550 - map (map) read-only virtual addr map
551 Most of these are opened lazily as they are needed.
552 The pathnames for the 'files' for an LWP look slightly
553 different from those of a first-class process:
554 Pathnames for a process (<proc-id>):
556 /proc/<proc-id>/status
559 Pathnames for an LWP (lwp-id):
560 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
561 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
562 An LWP has no map or address space file descriptor, since
563 the memory map and address space are shared by all LWPs.
565 Everyone else (Solaris 2.5, Irix, OSF)
566 There is only one file descriptor for each process or LWP.
567 For convenience, we copy the same file descriptor into all
568 three fields of the procinfo struct (ctl_fd, status_fd, and
569 as_fd, see NEW_PROC_API above) so that code that uses them
570 doesn't need any #ifdef's.
575 Each LWP has an independent file descriptor, but these
576 are not obtained via the 'open' system call like the rest:
577 instead, they're obtained thru an ioctl call (PIOCOPENLWP)
578 to the file descriptor of the parent process.
581 These do not even have their own independent file descriptor.
582 All operations are carried out on the file descriptor of the
583 parent process. Therefore we just call open again for each
584 thread, getting a new handle for the same 'file'. */
587 /* In this case, there are several different file descriptors that
588 we might be asked to open. The control file descriptor will be
589 opened early, but the others will be opened lazily as they are
592 strcpy (tmp, pi->pathname);
593 switch (which) { /* Which file descriptor to open? */
596 strcat (tmp, "/lwpctl");
598 strcat (tmp, "/ctl");
599 fd = open_with_retry (tmp, O_WRONLY);
606 return 0; /* There is no 'as' file descriptor for an lwp. */
608 fd = open_with_retry (tmp, O_RDWR);
615 strcat (tmp, "/lwpstatus");
617 strcat (tmp, "/status");
618 fd = open_with_retry (tmp, O_RDONLY);
624 return 0; /* unknown file descriptor */
626 #else /* not NEW_PROC_API */
627 /* In this case, there is only one file descriptor for each procinfo
628 (ie. each process or LWP). In fact, only the file descriptor for
629 the process can actually be opened by an 'open' system call. The
630 ones for the LWPs have to be obtained thru an IOCTL call on the
631 process's file descriptor.
633 For convenience, we copy each procinfo's single file descriptor
634 into all of the fields occupied by the several file descriptors
635 of the NEW_PROC_API implementation. That way, the code that uses
636 them can be written without ifdefs. */
639 #ifdef PIOCTSTATUS /* OSF */
640 /* Only one FD; just open it. */
641 if ((fd = open_with_retry (pi->pathname, O_RDWR)) < 0)
643 #else /* Sol 2.5, Irix, other? */
644 if (pi->tid == 0) /* Master procinfo for the process */
646 fd = open_with_retry (pi->pathname, O_RDWR);
650 else /* LWP thread procinfo */
652 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
656 /* Find the procinfo for the entire process. */
657 if ((process = find_procinfo (pi->pid, 0)) == NULL)
660 /* Now obtain the file descriptor for the LWP. */
661 if ((fd = ioctl (process->ctl_fd, PIOCOPENLWP, &lwpid)) < 0)
663 #else /* Irix, other? */
664 return 0; /* Don't know how to open threads. */
665 #endif /* Sol 2.5 PIOCOPENLWP */
667 #endif /* OSF PIOCTSTATUS */
668 pi->ctl_fd = pi->as_fd = pi->status_fd = fd;
669 #endif /* NEW_PROC_API */
671 return 1; /* success */
674 /* Allocate a data structure and link it into the procinfo list.
675 First tries to find a pre-existing one (FIXME: why?). Returns the
676 pointer to new procinfo struct. */
679 create_procinfo (int pid, int tid)
681 procinfo *pi, *parent = NULL;
683 if ((pi = find_procinfo (pid, tid)))
684 return pi; /* Already exists, nothing to do. */
686 /* Find parent before doing malloc, to save having to cleanup. */
688 parent = find_procinfo_or_die (pid, 0); /* FIXME: should I
690 doesn't exist yet? */
692 pi = (procinfo *) xmalloc (sizeof (procinfo));
693 memset (pi, 0, sizeof (procinfo));
697 #ifdef DYNAMIC_SYSCALLS
701 pi->saved_entryset = sysset_t_alloc (pi);
702 pi->saved_exitset = sysset_t_alloc (pi);
704 /* Chain into list. */
707 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
708 pi->next = procinfo_list;
714 sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid);
716 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
718 pi->next = parent->thread_list;
719 parent->thread_list = pi;
724 /* Close all file descriptors associated with the procinfo. */
727 close_procinfo_files (procinfo *pi)
734 if (pi->status_fd > 0)
735 close (pi->status_fd);
737 pi->ctl_fd = pi->as_fd = pi->status_fd = 0;
740 /* Destructor function. Close, unlink and deallocate the object. */
743 destroy_one_procinfo (procinfo **list, procinfo *pi)
747 /* Step one: unlink the procinfo from its list. */
751 for (ptr = *list; ptr; ptr = ptr->next)
754 ptr->next = pi->next;
758 /* Step two: close any open file descriptors. */
759 close_procinfo_files (pi);
761 /* Step three: free the memory. */
762 #ifdef DYNAMIC_SYSCALLS
765 xfree (pi->saved_entryset);
766 xfree (pi->saved_exitset);
771 destroy_procinfo (procinfo *pi)
775 if (pi->tid != 0) /* Destroy a thread procinfo. */
777 tmp = find_procinfo (pi->pid, 0); /* Find the parent process. */
778 destroy_one_procinfo (&tmp->thread_list, pi);
780 else /* Destroy a process procinfo and all its threads. */
782 /* First destroy the children, if any; */
783 while (pi->thread_list != NULL)
784 destroy_one_procinfo (&pi->thread_list, pi->thread_list);
785 /* Then destroy the parent. Genocide!!! */
786 destroy_one_procinfo (&procinfo_list, pi);
791 do_destroy_procinfo_cleanup (void *pi)
793 destroy_procinfo (pi);
796 enum { NOKILL, KILL };
798 /* To be called on a non_recoverable error for a procinfo. Prints
799 error messages, optionally sends a SIGKILL to the process, then
800 destroys the data structure. */
803 dead_procinfo (procinfo *pi, char *msg, int kill_p)
809 print_sys_errmsg (pi->pathname, errno);
813 sprintf (procfile, "process %d", pi->pid);
814 print_sys_errmsg (procfile, errno);
817 kill (pi->pid, SIGKILL);
819 destroy_procinfo (pi);
823 /* Returns the (complete) size of a sysset_t struct. Normally, this
824 is just sizeof (sysset_t), but in the case of Monterey/64, the
825 actual size of sysset_t isn't known until runtime. */
828 sysset_t_size (procinfo * pi)
830 #ifndef DYNAMIC_SYSCALLS
831 return sizeof (sysset_t);
833 return sizeof (sysset_t) - sizeof (uint64_t)
834 + sizeof (uint64_t) * ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
835 / (8 * sizeof (uint64_t)));
839 /* Allocate and (partially) initialize a sysset_t struct. */
842 sysset_t_alloc (procinfo * pi)
845 int size = sysset_t_size (pi);
847 ret = xmalloc (size);
848 #ifdef DYNAMIC_SYSCALLS
849 ret->pr_size = ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
850 / (8 * sizeof (uint64_t)));
855 #ifdef DYNAMIC_SYSCALLS
857 /* Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
858 pi->num_syscalls with the number of syscalls and pi->syscall_names
859 with the names. (Certain numbers may be skipped in which case the
860 names for these numbers will be left as NULL.) */
862 #define MAX_SYSCALL_NAME_LENGTH 256
863 #define MAX_SYSCALLS 65536
866 load_syscalls (procinfo *pi)
868 char pathname[MAX_PROC_NAME_SIZE];
871 prsyscall_t *syscalls;
872 int i, size, maxcall;
873 struct cleanup *cleanups;
875 pi->num_syscalls = 0;
876 pi->syscall_names = 0;
878 /* Open the file descriptor for the sysent file. */
879 sprintf (pathname, "/proc/%d/sysent", pi->pid);
880 sysent_fd = open_with_retry (pathname, O_RDONLY);
883 error (_("load_syscalls: Can't open /proc/%d/sysent"), pi->pid);
885 cleanups = make_cleanup_close (sysent_fd);
887 size = sizeof header - sizeof (prsyscall_t);
888 if (read (sysent_fd, &header, size) != size)
890 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
893 if (header.pr_nsyscalls == 0)
895 error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"),
899 size = header.pr_nsyscalls * sizeof (prsyscall_t);
900 syscalls = xmalloc (size);
901 make_cleanup (free_current_contents, &syscalls);
903 if (read (sysent_fd, syscalls, size) != size)
904 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
906 /* Find maximum syscall number. This may not be the same as
907 pr_nsyscalls since that value refers to the number of entries
908 in the table. (Also, the docs indicate that some system
909 call numbers may be skipped.) */
911 maxcall = syscalls[0].pr_number;
913 for (i = 1; i < header.pr_nsyscalls; i++)
914 if (syscalls[i].pr_number > maxcall
915 && syscalls[i].pr_nameoff > 0
916 && syscalls[i].pr_number < MAX_SYSCALLS)
917 maxcall = syscalls[i].pr_number;
919 pi->num_syscalls = maxcall+1;
920 pi->syscall_names = xmalloc (pi->num_syscalls * sizeof (char *));
922 for (i = 0; i < pi->num_syscalls; i++)
923 pi->syscall_names[i] = NULL;
925 /* Read the syscall names in. */
926 for (i = 0; i < header.pr_nsyscalls; i++)
928 char namebuf[MAX_SYSCALL_NAME_LENGTH];
932 if (syscalls[i].pr_number >= MAX_SYSCALLS
933 || syscalls[i].pr_number < 0
934 || syscalls[i].pr_nameoff <= 0
935 || (lseek (sysent_fd, (off_t) syscalls[i].pr_nameoff, SEEK_SET)
936 != (off_t) syscalls[i].pr_nameoff))
939 nread = read (sysent_fd, namebuf, sizeof namebuf);
943 callnum = syscalls[i].pr_number;
945 if (pi->syscall_names[callnum] != NULL)
947 /* FIXME: Generate warning. */
951 namebuf[nread-1] = '\0';
952 size = strlen (namebuf) + 1;
953 pi->syscall_names[callnum] = xmalloc (size);
954 strncpy (pi->syscall_names[callnum], namebuf, size-1);
955 pi->syscall_names[callnum][size-1] = '\0';
958 do_cleanups (cleanups);
961 /* Free the space allocated for the syscall names from the procinfo
965 free_syscalls (procinfo *pi)
967 if (pi->syscall_names)
971 for (i = 0; i < pi->num_syscalls; i++)
972 if (pi->syscall_names[i] != NULL)
973 xfree (pi->syscall_names[i]);
975 xfree (pi->syscall_names);
976 pi->syscall_names = 0;
980 /* Given a name, look up (and return) the corresponding syscall number.
981 If no match is found, return -1. */
984 find_syscall (procinfo *pi, char *name)
988 for (i = 0; i < pi->num_syscalls; i++)
990 if (pi->syscall_names[i] && strcmp (name, pi->syscall_names[i]) == 0)
997 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
999 /* =================== /proc "MODULE" =================== */
1001 /* This "module" is the interface layer between the /proc system API
1002 and the gdb target vector functions. This layer consists of access
1003 functions that encapsulate each of the basic operations that we
1004 need to use from the /proc API.
1006 The main motivation for this layer is to hide the fact that there
1007 are two very different implementations of the /proc API. Rather
1008 than have a bunch of #ifdefs all thru the gdb target vector
1009 functions, we do our best to hide them all in here. */
1011 static long proc_flags (procinfo * pi);
1012 static int proc_why (procinfo * pi);
1013 static int proc_what (procinfo * pi);
1014 static int proc_set_current_signal (procinfo * pi, int signo);
1015 static int proc_get_current_thread (procinfo * pi);
1016 static int proc_iterate_over_threads
1018 int (*func) (procinfo *, procinfo *, void *),
1022 proc_warn (procinfo *pi, char *func, int line)
1024 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1025 print_sys_errmsg (errmsg, errno);
1029 proc_error (procinfo *pi, char *func, int line)
1031 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1032 perror_with_name (errmsg);
1035 /* Updates the status struct in the procinfo. There is a 'valid'
1036 flag, to let other functions know when this function needs to be
1037 called (so the status is only read when it is needed). The status
1038 file descriptor is also only opened when it is needed. Returns
1039 non-zero for success, zero for failure. */
1042 proc_get_status (procinfo *pi)
1044 /* Status file descriptor is opened "lazily". */
1045 if (pi->status_fd == 0 &&
1046 open_procinfo_files (pi, FD_STATUS) == 0)
1048 pi->status_valid = 0;
1053 if (lseek (pi->status_fd, 0, SEEK_SET) < 0)
1054 pi->status_valid = 0; /* fail */
1057 /* Sigh... I have to read a different data structure,
1058 depending on whether this is a main process or an LWP. */
1060 pi->status_valid = (read (pi->status_fd,
1061 (char *) &pi->prstatus.pr_lwp,
1062 sizeof (lwpstatus_t))
1063 == sizeof (lwpstatus_t));
1066 pi->status_valid = (read (pi->status_fd,
1067 (char *) &pi->prstatus,
1068 sizeof (gdb_prstatus_t))
1069 == sizeof (gdb_prstatus_t));
1072 #else /* ioctl method */
1073 #ifdef PIOCTSTATUS /* osf */
1074 if (pi->tid == 0) /* main process */
1076 /* Just read the danged status. Now isn't that simple? */
1078 (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1085 tid_t pr_error_thread;
1086 struct prstatus status;
1089 thread_status.pr_count = 1;
1090 thread_status.status.pr_tid = pi->tid;
1091 win = (ioctl (pi->status_fd, PIOCTSTATUS, &thread_status) >= 0);
1094 memcpy (&pi->prstatus, &thread_status.status,
1095 sizeof (pi->prstatus));
1096 pi->status_valid = 1;
1100 /* Just read the danged status. Now isn't that simple? */
1101 pi->status_valid = (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1105 if (pi->status_valid)
1107 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1110 proc_get_current_thread (pi));
1113 /* The status struct includes general regs, so mark them valid too. */
1114 pi->gregs_valid = pi->status_valid;
1116 /* In the read/write multiple-fd model, the status struct includes
1117 the fp regs too, so mark them valid too. */
1118 pi->fpregs_valid = pi->status_valid;
1120 return pi->status_valid; /* True if success, false if failure. */
1123 /* Returns the process flags (pr_flags field). */
1126 proc_flags (procinfo *pi)
1128 if (!pi->status_valid)
1129 if (!proc_get_status (pi))
1130 return 0; /* FIXME: not a good failure value (but what is?) */
1133 return pi->prstatus.pr_lwp.pr_flags;
1135 return pi->prstatus.pr_flags;
1139 /* Returns the pr_why field (why the process stopped). */
1142 proc_why (procinfo *pi)
1144 if (!pi->status_valid)
1145 if (!proc_get_status (pi))
1146 return 0; /* FIXME: not a good failure value (but what is?) */
1149 return pi->prstatus.pr_lwp.pr_why;
1151 return pi->prstatus.pr_why;
1155 /* Returns the pr_what field (details of why the process stopped). */
1158 proc_what (procinfo *pi)
1160 if (!pi->status_valid)
1161 if (!proc_get_status (pi))
1162 return 0; /* FIXME: not a good failure value (but what is?) */
1165 return pi->prstatus.pr_lwp.pr_what;
1167 return pi->prstatus.pr_what;
1171 /* This function is only called when PI is stopped by a watchpoint.
1172 Assuming the OS supports it, write to *ADDR the data address which
1173 triggered it and return 1. Return 0 if it is not possible to know
1177 proc_watchpoint_address (procinfo *pi, CORE_ADDR *addr)
1179 if (!pi->status_valid)
1180 if (!proc_get_status (pi))
1184 *addr = (CORE_ADDR) gdbarch_pointer_to_address (target_gdbarch (),
1185 builtin_type (target_gdbarch ())->builtin_data_ptr,
1186 (gdb_byte *) &pi->prstatus.pr_lwp.pr_info.si_addr);
1188 *addr = (CORE_ADDR) gdbarch_pointer_to_address (target_gdbarch (),
1189 builtin_type (target_gdbarch ())->builtin_data_ptr,
1190 (gdb_byte *) &pi->prstatus.pr_info.si_addr);
1195 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1197 /* Returns the pr_nsysarg field (number of args to the current
1201 proc_nsysarg (procinfo *pi)
1203 if (!pi->status_valid)
1204 if (!proc_get_status (pi))
1208 return pi->prstatus.pr_lwp.pr_nsysarg;
1210 return pi->prstatus.pr_nsysarg;
1214 /* Returns the pr_sysarg field (pointer to the arguments of current
1218 proc_sysargs (procinfo *pi)
1220 if (!pi->status_valid)
1221 if (!proc_get_status (pi))
1225 return (long *) &pi->prstatus.pr_lwp.pr_sysarg;
1227 return (long *) &pi->prstatus.pr_sysarg;
1230 #endif /* PIOCSSPCACT */
1232 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
1233 /* Returns the pr_cursig field (current signal). */
1236 proc_cursig (struct procinfo *pi)
1238 if (!pi->status_valid)
1239 if (!proc_get_status (pi))
1240 return 0; /* FIXME: not a good failure value (but what is?) */
1243 return pi->prstatus.pr_lwp.pr_cursig;
1245 return pi->prstatus.pr_cursig;
1248 #endif /* PROCFS_DONT_PIOCSSIG_CURSIG */
1250 /* === I appologize for the messiness of this function.
1251 === This is an area where the different versions of
1252 === /proc are more inconsistent than usual.
1254 Set or reset any of the following process flags:
1255 PR_FORK -- forked child will inherit trace flags
1256 PR_RLC -- traced process runs when last /proc file closed.
1257 PR_KLC -- traced process is killed when last /proc file closed.
1258 PR_ASYNC -- LWP's get to run/stop independently.
1260 There are three methods for doing this function:
1261 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1263 2) Middle: PIOCSET/PIOCRESET
1265 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1268 Note: Irix does not define PR_ASYNC.
1269 Note: OSF does not define PR_KLC.
1270 Note: OSF is the only one that can ONLY use the oldest method.
1274 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1275 mode -- 1 for set, 0 for reset.
1277 Returns non-zero for success, zero for failure. */
1279 enum { FLAG_RESET, FLAG_SET };
1282 proc_modify_flag (procinfo *pi, long flag, long mode)
1284 long win = 0; /* default to fail */
1286 /* These operations affect the process as a whole, and applying them
1287 to an individual LWP has the same meaning as applying them to the
1288 main process. Therefore, if we're ever called with a pointer to
1289 an LWP's procinfo, let's substitute the process's procinfo and
1290 avoid opening the LWP's file descriptor unnecessarily. */
1293 pi = find_procinfo_or_die (pi->pid, 0);
1295 #ifdef NEW_PROC_API /* Newest method: Newer Solarii. */
1296 /* First normalize the PCUNSET/PCRESET command opcode
1297 (which for no obvious reason has a different definition
1298 from one operating system to the next...) */
1300 #define GDBRESET PCUNSET
1303 #define GDBRESET PCRESET
1307 procfs_ctl_t arg[2];
1309 if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC). */
1311 else /* Reset the flag. */
1315 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1318 #ifdef PIOCSET /* Irix/Sol5 method */
1319 if (mode == FLAG_SET) /* Set the flag (hopefully RLC, FORK, or ASYNC). */
1321 win = (ioctl (pi->ctl_fd, PIOCSET, &flag) >= 0);
1323 else /* Reset the flag. */
1325 win = (ioctl (pi->ctl_fd, PIOCRESET, &flag) >= 0);
1329 #ifdef PIOCSRLC /* Oldest method: OSF */
1332 if (mode == FLAG_SET) /* Set run-on-last-close */
1334 win = (ioctl (pi->ctl_fd, PIOCSRLC, NULL) >= 0);
1336 else /* Clear run-on-last-close */
1338 win = (ioctl (pi->ctl_fd, PIOCRRLC, NULL) >= 0);
1342 if (mode == FLAG_SET) /* Set inherit-on-fork */
1344 win = (ioctl (pi->ctl_fd, PIOCSFORK, NULL) >= 0);
1346 else /* Clear inherit-on-fork */
1348 win = (ioctl (pi->ctl_fd, PIOCRFORK, NULL) >= 0);
1352 win = 0; /* Fail -- unknown flag (can't do PR_ASYNC). */
1359 /* The above operation renders the procinfo's cached pstatus
1361 pi->status_valid = 0;
1364 warning (_("procfs: modify_flag failed to turn %s %s"),
1365 flag == PR_FORK ? "PR_FORK" :
1366 flag == PR_RLC ? "PR_RLC" :
1368 flag == PR_ASYNC ? "PR_ASYNC" :
1371 flag == PR_KLC ? "PR_KLC" :
1374 mode == FLAG_RESET ? "off" : "on");
1379 /* Set the run_on_last_close flag. Process with all threads will
1380 become runnable when debugger closes all /proc fds. Returns
1381 non-zero for success, zero for failure. */
1384 proc_set_run_on_last_close (procinfo *pi)
1386 return proc_modify_flag (pi, PR_RLC, FLAG_SET);
1389 /* Reset the run_on_last_close flag. The process will NOT become
1390 runnable when debugger closes its file handles. Returns non-zero
1391 for success, zero for failure. */
1394 proc_unset_run_on_last_close (procinfo *pi)
1396 return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
1399 /* Reset inherit_on_fork flag. If the process forks a child while we
1400 are registered for events in the parent, then we will NOT recieve
1401 events from the child. Returns non-zero for success, zero for
1405 proc_unset_inherit_on_fork (procinfo *pi)
1407 return proc_modify_flag (pi, PR_FORK, FLAG_RESET);
1411 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
1412 (signal etc.), the remaining LWPs will continue to run. Returns
1413 non-zero for success, zero for failure. */
1416 proc_set_async (procinfo *pi)
1418 return proc_modify_flag (pi, PR_ASYNC, FLAG_SET);
1421 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
1422 (signal etc.), then all other LWPs will stop as well. Returns
1423 non-zero for success, zero for failure. */
1426 proc_unset_async (procinfo *pi)
1428 return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET);
1430 #endif /* PR_ASYNC */
1432 /* Request the process/LWP to stop. Does not wait. Returns non-zero
1433 for success, zero for failure. */
1436 proc_stop_process (procinfo *pi)
1440 /* We might conceivably apply this operation to an LWP, and the
1441 LWP's ctl file descriptor might not be open. */
1443 if (pi->ctl_fd == 0 &&
1444 open_procinfo_files (pi, FD_CTL) == 0)
1449 procfs_ctl_t cmd = PCSTOP;
1451 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1452 #else /* ioctl method */
1453 win = (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) >= 0);
1454 /* Note: the call also reads the prstatus. */
1457 pi->status_valid = 1;
1458 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1461 proc_get_current_thread (pi));
1469 /* Wait for the process or LWP to stop (block until it does). Returns
1470 non-zero for success, zero for failure. */
1473 proc_wait_for_stop (procinfo *pi)
1477 /* We should never have to apply this operation to any procinfo
1478 except the one for the main process. If that ever changes for
1479 any reason, then take out the following clause and replace it
1480 with one that makes sure the ctl_fd is open. */
1483 pi = find_procinfo_or_die (pi->pid, 0);
1487 procfs_ctl_t cmd = PCWSTOP;
1489 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1490 /* We been runnin' and we stopped -- need to update status. */
1491 pi->status_valid = 0;
1493 #else /* ioctl method */
1494 win = (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) >= 0);
1495 /* Above call also refreshes the prstatus. */
1498 pi->status_valid = 1;
1499 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1502 proc_get_current_thread (pi));
1509 /* Make the process or LWP runnable.
1511 Options (not all are implemented):
1513 - clear current fault
1514 - clear current signal
1515 - abort the current system call
1516 - stop as soon as finished with system call
1517 - (ioctl): set traced signal set
1518 - (ioctl): set held signal set
1519 - (ioctl): set traced fault set
1520 - (ioctl): set start pc (vaddr)
1522 Always clears the current fault. PI is the process or LWP to
1523 operate on. If STEP is true, set the process or LWP to trap after
1524 one instruction. If SIGNO is zero, clear the current signal if
1525 any; if non-zero, set the current signal to this one. Returns
1526 non-zero for success, zero for failure. */
1529 proc_run_process (procinfo *pi, int step, int signo)
1534 /* We will probably have to apply this operation to individual
1535 threads, so make sure the control file descriptor is open. */
1537 if (pi->ctl_fd == 0 &&
1538 open_procinfo_files (pi, FD_CTL) == 0)
1543 runflags = PRCFAULT; /* Always clear current fault. */
1548 else if (signo != -1) /* -1 means do nothing W.R.T. signals. */
1549 proc_set_current_signal (pi, signo);
1553 procfs_ctl_t cmd[2];
1557 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1559 #else /* ioctl method */
1563 memset (&prrun, 0, sizeof (prrun));
1564 prrun.pr_flags = runflags;
1565 win = (ioctl (pi->ctl_fd, PIOCRUN, &prrun) >= 0);
1572 /* Register to trace signals in the process or LWP. Returns non-zero
1573 for success, zero for failure. */
1576 proc_set_traced_signals (procinfo *pi, gdb_sigset_t *sigset)
1580 /* We should never have to apply this operation to any procinfo
1581 except the one for the main process. If that ever changes for
1582 any reason, then take out the following clause and replace it
1583 with one that makes sure the ctl_fd is open. */
1586 pi = find_procinfo_or_die (pi->pid, 0);
1592 /* Use char array to avoid alignment issues. */
1593 char sigset[sizeof (gdb_sigset_t)];
1597 memcpy (&arg.sigset, sigset, sizeof (gdb_sigset_t));
1599 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1601 #else /* ioctl method */
1602 win = (ioctl (pi->ctl_fd, PIOCSTRACE, sigset) >= 0);
1604 /* The above operation renders the procinfo's cached pstatus obsolete. */
1605 pi->status_valid = 0;
1608 warning (_("procfs: set_traced_signals failed"));
1612 /* Register to trace hardware faults in the process or LWP. Returns
1613 non-zero for success, zero for failure. */
1616 proc_set_traced_faults (procinfo *pi, fltset_t *fltset)
1620 /* We should never have to apply this operation to any procinfo
1621 except the one for the main process. If that ever changes for
1622 any reason, then take out the following clause and replace it
1623 with one that makes sure the ctl_fd is open. */
1626 pi = find_procinfo_or_die (pi->pid, 0);
1632 /* Use char array to avoid alignment issues. */
1633 char fltset[sizeof (fltset_t)];
1637 memcpy (&arg.fltset, fltset, sizeof (fltset_t));
1639 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1641 #else /* ioctl method */
1642 win = (ioctl (pi->ctl_fd, PIOCSFAULT, fltset) >= 0);
1644 /* The above operation renders the procinfo's cached pstatus obsolete. */
1645 pi->status_valid = 0;
1650 /* Register to trace entry to system calls in the process or LWP.
1651 Returns non-zero for success, zero for failure. */
1654 proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset)
1658 /* We should never have to apply this operation to any procinfo
1659 except the one for the main process. If that ever changes for
1660 any reason, then take out the following clause and replace it
1661 with one that makes sure the ctl_fd is open. */
1664 pi = find_procinfo_or_die (pi->pid, 0);
1668 struct gdb_proc_ctl_pcsentry {
1670 /* Use char array to avoid alignment issues. */
1671 char sysset[sizeof (sysset_t)];
1673 int argp_size = sizeof (struct gdb_proc_ctl_pcsentry)
1675 + sysset_t_size (pi);
1677 argp = xmalloc (argp_size);
1679 argp->cmd = PCSENTRY;
1680 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1682 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1685 #else /* ioctl method */
1686 win = (ioctl (pi->ctl_fd, PIOCSENTRY, sysset) >= 0);
1688 /* The above operation renders the procinfo's cached pstatus
1690 pi->status_valid = 0;
1695 /* Register to trace exit from system calls in the process or LWP.
1696 Returns non-zero for success, zero for failure. */
1699 proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset)
1703 /* We should never have to apply this operation to any procinfo
1704 except the one for the main process. If that ever changes for
1705 any reason, then take out the following clause and replace it
1706 with one that makes sure the ctl_fd is open. */
1709 pi = find_procinfo_or_die (pi->pid, 0);
1713 struct gdb_proc_ctl_pcsexit {
1715 /* Use char array to avoid alignment issues. */
1716 char sysset[sizeof (sysset_t)];
1718 int argp_size = sizeof (struct gdb_proc_ctl_pcsexit)
1720 + sysset_t_size (pi);
1722 argp = xmalloc (argp_size);
1724 argp->cmd = PCSEXIT;
1725 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1727 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1730 #else /* ioctl method */
1731 win = (ioctl (pi->ctl_fd, PIOCSEXIT, sysset) >= 0);
1733 /* The above operation renders the procinfo's cached pstatus
1735 pi->status_valid = 0;
1740 /* Specify the set of blocked / held signals in the process or LWP.
1741 Returns non-zero for success, zero for failure. */
1744 proc_set_held_signals (procinfo *pi, gdb_sigset_t *sighold)
1748 /* We should never have to apply this operation to any procinfo
1749 except the one for the main process. If that ever changes for
1750 any reason, then take out the following clause and replace it
1751 with one that makes sure the ctl_fd is open. */
1754 pi = find_procinfo_or_die (pi->pid, 0);
1760 /* Use char array to avoid alignment issues. */
1761 char hold[sizeof (gdb_sigset_t)];
1765 memcpy (&arg.hold, sighold, sizeof (gdb_sigset_t));
1766 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1769 win = (ioctl (pi->ctl_fd, PIOCSHOLD, sighold) >= 0);
1771 /* The above operation renders the procinfo's cached pstatus
1773 pi->status_valid = 0;
1778 /* Returns the set of signals that are held / blocked. Will also copy
1779 the sigset if SAVE is non-zero. */
1781 static gdb_sigset_t *
1782 proc_get_held_signals (procinfo *pi, gdb_sigset_t *save)
1784 gdb_sigset_t *ret = NULL;
1786 /* We should never have to apply this operation to any procinfo
1787 except the one for the main process. If that ever changes for
1788 any reason, then take out the following clause and replace it
1789 with one that makes sure the ctl_fd is open. */
1792 pi = find_procinfo_or_die (pi->pid, 0);
1795 if (!pi->status_valid)
1796 if (!proc_get_status (pi))
1799 ret = &pi->prstatus.pr_lwp.pr_lwphold;
1800 #else /* not NEW_PROC_API */
1802 static gdb_sigset_t sigheld;
1804 if (ioctl (pi->ctl_fd, PIOCGHOLD, &sigheld) >= 0)
1807 #endif /* NEW_PROC_API */
1809 memcpy (save, ret, sizeof (gdb_sigset_t));
1814 /* Returns the set of signals that are traced / debugged. Will also
1815 copy the sigset if SAVE is non-zero. */
1817 static gdb_sigset_t *
1818 proc_get_traced_signals (procinfo *pi, gdb_sigset_t *save)
1820 gdb_sigset_t *ret = NULL;
1822 /* We should never have to apply this operation to any procinfo
1823 except the one for the main process. If that ever changes for
1824 any reason, then take out the following clause and replace it
1825 with one that makes sure the ctl_fd is open. */
1828 pi = find_procinfo_or_die (pi->pid, 0);
1831 if (!pi->status_valid)
1832 if (!proc_get_status (pi))
1835 ret = &pi->prstatus.pr_sigtrace;
1838 static gdb_sigset_t sigtrace;
1840 if (ioctl (pi->ctl_fd, PIOCGTRACE, &sigtrace) >= 0)
1845 memcpy (save, ret, sizeof (gdb_sigset_t));
1850 /* Returns the set of hardware faults that are traced /debugged. Will
1851 also copy the faultset if SAVE is non-zero. */
1854 proc_get_traced_faults (procinfo *pi, fltset_t *save)
1856 fltset_t *ret = NULL;
1858 /* We should never have to apply this operation to any procinfo
1859 except the one for the main process. If that ever changes for
1860 any reason, then take out the following clause and replace it
1861 with one that makes sure the ctl_fd is open. */
1864 pi = find_procinfo_or_die (pi->pid, 0);
1867 if (!pi->status_valid)
1868 if (!proc_get_status (pi))
1871 ret = &pi->prstatus.pr_flttrace;
1874 static fltset_t flttrace;
1876 if (ioctl (pi->ctl_fd, PIOCGFAULT, &flttrace) >= 0)
1881 memcpy (save, ret, sizeof (fltset_t));
1886 /* Returns the set of syscalls that are traced /debugged on entry.
1887 Will also copy the syscall set if SAVE is non-zero. */
1890 proc_get_traced_sysentry (procinfo *pi, sysset_t *save)
1892 sysset_t *ret = NULL;
1894 /* We should never have to apply this operation to any procinfo
1895 except the one for the main process. If that ever changes for
1896 any reason, then take out the following clause and replace it
1897 with one that makes sure the ctl_fd is open. */
1900 pi = find_procinfo_or_die (pi->pid, 0);
1903 if (!pi->status_valid)
1904 if (!proc_get_status (pi))
1907 #ifndef DYNAMIC_SYSCALLS
1908 ret = &pi->prstatus.pr_sysentry;
1909 #else /* DYNAMIC_SYSCALLS */
1911 static sysset_t *sysentry;
1915 sysentry = sysset_t_alloc (pi);
1917 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
1919 if (pi->prstatus.pr_sysentry_offset == 0)
1921 gdb_premptysysset (sysentry);
1927 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysentry_offset,
1929 != (off_t) pi->prstatus.pr_sysentry_offset)
1931 size = sysset_t_size (pi);
1932 gdb_premptysysset (sysentry);
1933 rsize = read (pi->status_fd, sysentry, size);
1938 #endif /* DYNAMIC_SYSCALLS */
1939 #else /* !NEW_PROC_API */
1941 static sysset_t sysentry;
1943 if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysentry) >= 0)
1946 #endif /* NEW_PROC_API */
1948 memcpy (save, ret, sysset_t_size (pi));
1953 /* Returns the set of syscalls that are traced /debugged on exit.
1954 Will also copy the syscall set if SAVE is non-zero. */
1957 proc_get_traced_sysexit (procinfo *pi, sysset_t *save)
1959 sysset_t * ret = NULL;
1961 /* We should never have to apply this operation to any procinfo
1962 except the one for the main process. If that ever changes for
1963 any reason, then take out the following clause and replace it
1964 with one that makes sure the ctl_fd is open. */
1967 pi = find_procinfo_or_die (pi->pid, 0);
1970 if (!pi->status_valid)
1971 if (!proc_get_status (pi))
1974 #ifndef DYNAMIC_SYSCALLS
1975 ret = &pi->prstatus.pr_sysexit;
1976 #else /* DYNAMIC_SYSCALLS */
1978 static sysset_t *sysexit;
1982 sysexit = sysset_t_alloc (pi);
1984 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
1986 if (pi->prstatus.pr_sysexit_offset == 0)
1988 gdb_premptysysset (sysexit);
1994 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysexit_offset,
1996 != (off_t) pi->prstatus.pr_sysexit_offset)
1998 size = sysset_t_size (pi);
1999 gdb_premptysysset (sysexit);
2000 rsize = read (pi->status_fd, sysexit, size);
2005 #endif /* DYNAMIC_SYSCALLS */
2008 static sysset_t sysexit;
2010 if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysexit) >= 0)
2015 memcpy (save, ret, sysset_t_size (pi));
2020 /* The current fault (if any) is cleared; the associated signal will
2021 not be sent to the process or LWP when it resumes. Returns
2022 non-zero for success, zero for failure. */
2025 proc_clear_current_fault (procinfo *pi)
2029 /* We should never have to apply this operation to any procinfo
2030 except the one for the main process. If that ever changes for
2031 any reason, then take out the following clause and replace it
2032 with one that makes sure the ctl_fd is open. */
2035 pi = find_procinfo_or_die (pi->pid, 0);
2039 procfs_ctl_t cmd = PCCFAULT;
2041 win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd));
2044 win = (ioctl (pi->ctl_fd, PIOCCFAULT, 0) >= 0);
2050 /* Set the "current signal" that will be delivered next to the
2051 process. NOTE: semantics are different from those of KILL. This
2052 signal will be delivered to the process or LWP immediately when it
2053 is resumed (even if the signal is held/blocked); it will NOT
2054 immediately cause another event of interest, and will NOT first
2055 trap back to the debugger. Returns non-zero for success, zero for
2059 proc_set_current_signal (procinfo *pi, int signo)
2064 /* Use char array to avoid alignment issues. */
2065 char sinfo[sizeof (gdb_siginfo_t)];
2067 gdb_siginfo_t mysinfo;
2069 struct target_waitstatus wait_status;
2071 /* We should never have to apply this operation to any procinfo
2072 except the one for the main process. If that ever changes for
2073 any reason, then take out the following clause and replace it
2074 with one that makes sure the ctl_fd is open. */
2077 pi = find_procinfo_or_die (pi->pid, 0);
2079 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2080 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2081 receives a PIOCSSIG with a signal identical to the current
2082 signal, it messes up the current signal. Work around the kernel
2085 signo == proc_cursig (pi))
2086 return 1; /* I assume this is a success? */
2089 /* The pointer is just a type alias. */
2090 get_last_target_status (&wait_ptid, &wait_status);
2091 if (ptid_equal (wait_ptid, inferior_ptid)
2092 && wait_status.kind == TARGET_WAITKIND_STOPPED
2093 && wait_status.value.sig == gdb_signal_from_host (signo)
2094 && proc_get_status (pi)
2096 && pi->prstatus.pr_lwp.pr_info.si_signo == signo
2098 && pi->prstatus.pr_info.si_signo == signo
2101 /* Use the siginfo associated with the signal being
2104 memcpy (arg.sinfo, &pi->prstatus.pr_lwp.pr_info, sizeof (gdb_siginfo_t));
2106 memcpy (arg.sinfo, &pi->prstatus.pr_info, sizeof (gdb_siginfo_t));
2110 mysinfo.si_signo = signo;
2111 mysinfo.si_code = 0;
2112 mysinfo.si_pid = getpid (); /* ?why? */
2113 mysinfo.si_uid = getuid (); /* ?why? */
2114 memcpy (arg.sinfo, &mysinfo, sizeof (gdb_siginfo_t));
2119 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2121 win = (ioctl (pi->ctl_fd, PIOCSSIG, (void *) &arg.sinfo) >= 0);
2127 /* The current signal (if any) is cleared, and is not sent to the
2128 process or LWP when it resumes. Returns non-zero for success, zero
2132 proc_clear_current_signal (procinfo *pi)
2136 /* We should never have to apply this operation to any procinfo
2137 except the one for the main process. If that ever changes for
2138 any reason, then take out the following clause and replace it
2139 with one that makes sure the ctl_fd is open. */
2142 pi = find_procinfo_or_die (pi->pid, 0);
2148 /* Use char array to avoid alignment issues. */
2149 char sinfo[sizeof (gdb_siginfo_t)];
2151 gdb_siginfo_t mysinfo;
2154 /* The pointer is just a type alias. */
2155 mysinfo.si_signo = 0;
2156 mysinfo.si_code = 0;
2157 mysinfo.si_errno = 0;
2158 mysinfo.si_pid = getpid (); /* ?why? */
2159 mysinfo.si_uid = getuid (); /* ?why? */
2160 memcpy (arg.sinfo, &mysinfo, sizeof (gdb_siginfo_t));
2162 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2165 win = (ioctl (pi->ctl_fd, PIOCSSIG, 0) >= 0);
2171 /* Return the general-purpose registers for the process or LWP
2172 corresponding to PI. Upon failure, return NULL. */
2174 static gdb_gregset_t *
2175 proc_get_gregs (procinfo *pi)
2177 if (!pi->status_valid || !pi->gregs_valid)
2178 if (!proc_get_status (pi))
2182 return &pi->prstatus.pr_lwp.pr_reg;
2184 return &pi->prstatus.pr_reg;
2188 /* Return the general-purpose registers for the process or LWP
2189 corresponding to PI. Upon failure, return NULL. */
2191 static gdb_fpregset_t *
2192 proc_get_fpregs (procinfo *pi)
2195 if (!pi->status_valid || !pi->fpregs_valid)
2196 if (!proc_get_status (pi))
2199 return &pi->prstatus.pr_lwp.pr_fpreg;
2201 #else /* not NEW_PROC_API */
2202 if (pi->fpregs_valid)
2203 return &pi->fpregset; /* Already got 'em. */
2206 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2215 tid_t pr_error_thread;
2216 tfpregset_t thread_1;
2219 thread_fpregs.pr_count = 1;
2220 thread_fpregs.thread_1.tid = pi->tid;
2223 && ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2225 pi->fpregs_valid = 1;
2226 return &pi->fpregset; /* Got 'em now! */
2228 else if (pi->tid != 0
2229 && ioctl (pi->ctl_fd, PIOCTGFPREG, &thread_fpregs) >= 0)
2231 memcpy (&pi->fpregset, &thread_fpregs.thread_1.pr_fpregs,
2232 sizeof (pi->fpregset));
2233 pi->fpregs_valid = 1;
2234 return &pi->fpregset; /* Got 'em now! */
2241 if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2243 pi->fpregs_valid = 1;
2244 return &pi->fpregset; /* Got 'em now! */
2253 #endif /* NEW_PROC_API */
2256 /* Write the general-purpose registers back to the process or LWP
2257 corresponding to PI. Return non-zero for success, zero for
2261 proc_set_gregs (procinfo *pi)
2263 gdb_gregset_t *gregs;
2266 gregs = proc_get_gregs (pi);
2268 return 0; /* proc_get_regs has already warned. */
2270 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2279 /* Use char array to avoid alignment issues. */
2280 char gregs[sizeof (gdb_gregset_t)];
2284 memcpy (&arg.gregs, gregs, sizeof (arg.gregs));
2285 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2287 win = (ioctl (pi->ctl_fd, PIOCSREG, gregs) >= 0);
2291 /* Policy: writing the registers invalidates our cache. */
2292 pi->gregs_valid = 0;
2296 /* Write the floating-pointer registers back to the process or LWP
2297 corresponding to PI. Return non-zero for success, zero for
2301 proc_set_fpregs (procinfo *pi)
2303 gdb_fpregset_t *fpregs;
2306 fpregs = proc_get_fpregs (pi);
2308 return 0; /* proc_get_fpregs has already warned. */
2310 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2319 /* Use char array to avoid alignment issues. */
2320 char fpregs[sizeof (gdb_fpregset_t)];
2324 memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs));
2325 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2329 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2334 tid_t pr_error_thread;
2335 tfpregset_t thread_1;
2338 thread_fpregs.pr_count = 1;
2339 thread_fpregs.thread_1.tid = pi->tid;
2340 memcpy (&thread_fpregs.thread_1.pr_fpregs, fpregs,
2342 win = (ioctl (pi->ctl_fd, PIOCTSFPREG, &thread_fpregs) >= 0);
2345 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2347 #endif /* NEW_PROC_API */
2350 /* Policy: writing the registers invalidates our cache. */
2351 pi->fpregs_valid = 0;
2355 /* Send a signal to the proc or lwp with the semantics of "kill()".
2356 Returns non-zero for success, zero for failure. */
2359 proc_kill (procinfo *pi, int signo)
2363 /* We might conceivably apply this operation to an LWP, and the
2364 LWP's ctl file descriptor might not be open. */
2366 if (pi->ctl_fd == 0 &&
2367 open_procinfo_files (pi, FD_CTL) == 0)
2374 procfs_ctl_t cmd[2];
2378 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
2379 #else /* ioctl method */
2380 /* FIXME: do I need the Alpha OSF fixups present in
2381 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2382 win = (ioctl (pi->ctl_fd, PIOCKILL, &signo) >= 0);
2389 /* Find the pid of the process that started this one. Returns the
2390 parent process pid, or zero. */
2393 proc_parent_pid (procinfo *pi)
2395 /* We should never have to apply this operation to any procinfo
2396 except the one for the main process. If that ever changes for
2397 any reason, then take out the following clause and replace it
2398 with one that makes sure the ctl_fd is open. */
2401 pi = find_procinfo_or_die (pi->pid, 0);
2403 if (!pi->status_valid)
2404 if (!proc_get_status (pi))
2407 return pi->prstatus.pr_ppid;
2410 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2411 (a.k.a void pointer)! */
2413 #if (defined (PCWATCH) || defined (PIOCSWATCH)) \
2414 && !(defined (PIOCOPENLWP))
2416 procfs_address_to_host_pointer (CORE_ADDR addr)
2418 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
2421 gdb_assert (sizeof (ptr) == TYPE_LENGTH (ptr_type));
2422 gdbarch_address_to_pointer (target_gdbarch (), ptr_type,
2423 (gdb_byte *) &ptr, addr);
2429 proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags)
2431 #if !defined (PCWATCH) && !defined (PIOCSWATCH)
2432 /* If neither or these is defined, we can't support watchpoints.
2433 This just avoids possibly failing to compile the below on such
2437 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5. */
2438 #if defined (PIOCOPENLWP) /* Solaris 2.5: bail out. */
2443 char watch[sizeof (prwatch_t)];
2447 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2448 convert a target address into something that can be stored in a
2449 native data structure. */
2450 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2451 pwatch.pr_vaddr = (uintptr_t) procfs_address_to_host_pointer (addr);
2453 pwatch.pr_vaddr = (caddr_t) procfs_address_to_host_pointer (addr);
2455 pwatch.pr_size = len;
2456 pwatch.pr_wflags = wflags;
2457 #if defined(NEW_PROC_API) && defined (PCWATCH)
2459 memcpy (arg.watch, &pwatch, sizeof (prwatch_t));
2460 return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg));
2462 #if defined (PIOCSWATCH)
2463 return (ioctl (pi->ctl_fd, PIOCSWATCH, &pwatch) >= 0);
2465 return 0; /* Fail */
2472 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
2474 #include <sys/sysi86.h>
2476 /* The KEY is actually the value of the lower 16 bits of the GS
2477 register for the LWP that we're interested in. Returns the
2478 matching ssh struct (LDT entry). */
2481 proc_get_LDT_entry (procinfo *pi, int key)
2483 static struct ssd *ldt_entry = NULL;
2485 char pathname[MAX_PROC_NAME_SIZE];
2486 struct cleanup *old_chain = NULL;
2489 /* Allocate space for one LDT entry.
2490 This alloc must persist, because we return a pointer to it. */
2491 if (ldt_entry == NULL)
2492 ldt_entry = (struct ssd *) xmalloc (sizeof (struct ssd));
2494 /* Open the file descriptor for the LDT table. */
2495 sprintf (pathname, "/proc/%d/ldt", pi->pid);
2496 if ((fd = open_with_retry (pathname, O_RDONLY)) < 0)
2498 proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__);
2501 /* Make sure it gets closed again! */
2502 old_chain = make_cleanup_close (fd);
2504 /* Now 'read' thru the table, find a match and return it. */
2505 while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd))
2507 if (ldt_entry->sel == 0 &&
2508 ldt_entry->bo == 0 &&
2509 ldt_entry->acc1 == 0 &&
2510 ldt_entry->acc2 == 0)
2511 break; /* end of table */
2512 /* If key matches, return this entry. */
2513 if (ldt_entry->sel == key)
2516 /* Loop ended, match not found. */
2520 static int nalloc = 0;
2522 /* Get the number of LDT entries. */
2523 if (ioctl (pi->ctl_fd, PIOCNLDT, &nldt) < 0)
2525 proc_warn (pi, "proc_get_LDT_entry (PIOCNLDT)", __LINE__);
2529 /* Allocate space for the number of LDT entries. */
2530 /* This alloc has to persist, 'cause we return a pointer to it. */
2533 ldt_entry = (struct ssd *)
2534 xrealloc (ldt_entry, (nldt + 1) * sizeof (struct ssd));
2538 /* Read the whole table in one gulp. */
2539 if (ioctl (pi->ctl_fd, PIOCLDT, ldt_entry) < 0)
2541 proc_warn (pi, "proc_get_LDT_entry (PIOCLDT)", __LINE__);
2545 /* Search the table and return the (first) entry matching 'key'. */
2546 for (i = 0; i < nldt; i++)
2547 if (ldt_entry[i].sel == key)
2548 return &ldt_entry[i];
2550 /* Loop ended, match not found. */
2555 /* Returns the pointer to the LDT entry of PTID. */
2558 procfs_find_LDT_entry (ptid_t ptid)
2560 gdb_gregset_t *gregs;
2564 /* Find procinfo for the lwp. */
2565 if ((pi = find_procinfo (ptid_get_pid (ptid), ptid_get_lwp (ptid))) == NULL)
2567 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
2568 ptid_get_pid (ptid), ptid_get_lwp (ptid));
2571 /* get its general registers. */
2572 if ((gregs = proc_get_gregs (pi)) == NULL)
2574 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
2575 ptid_get_pid (ptid), ptid_get_lwp (ptid));
2578 /* Now extract the GS register's lower 16 bits. */
2579 key = (*gregs)[GS] & 0xffff;
2581 /* Find the matching entry and return it. */
2582 return proc_get_LDT_entry (pi, key);
2587 /* =============== END, non-thread part of /proc "MODULE" =============== */
2589 /* =================== Thread "MODULE" =================== */
2591 /* NOTE: you'll see more ifdefs and duplication of functions here,
2592 since there is a different way to do threads on every OS. */
2594 /* Returns the number of threads for the process. */
2596 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2599 proc_get_nthreads (procinfo *pi)
2603 if (ioctl (pi->ctl_fd, PIOCNTHR, &nthreads) < 0)
2604 proc_warn (pi, "procfs: PIOCNTHR failed", __LINE__);
2610 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2611 /* Solaris version */
2613 proc_get_nthreads (procinfo *pi)
2615 if (!pi->status_valid)
2616 if (!proc_get_status (pi))
2619 /* NEW_PROC_API: only works for the process procinfo, because the
2620 LWP procinfos do not get prstatus filled in. */
2622 if (pi->tid != 0) /* Find the parent process procinfo. */
2623 pi = find_procinfo_or_die (pi->pid, 0);
2625 return pi->prstatus.pr_nlwp;
2629 /* Default version */
2631 proc_get_nthreads (procinfo *pi)
2640 Return the ID of the thread that had an event of interest.
2641 (ie. the one that hit a breakpoint or other traced event). All
2642 other things being equal, this should be the ID of a thread that is
2643 currently executing. */
2645 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2646 /* Solaris version */
2648 proc_get_current_thread (procinfo *pi)
2650 /* Note: this should be applied to the root procinfo for the
2651 process, not to the procinfo for an LWP. If applied to the
2652 procinfo for an LWP, it will simply return that LWP's ID. In
2653 that case, find the parent process procinfo. */
2656 pi = find_procinfo_or_die (pi->pid, 0);
2658 if (!pi->status_valid)
2659 if (!proc_get_status (pi))
2663 return pi->prstatus.pr_lwp.pr_lwpid;
2665 return pi->prstatus.pr_who;
2670 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2673 proc_get_current_thread (procinfo *pi)
2675 #if 0 /* FIXME: not ready for prime time? */
2676 return pi->prstatus.pr_tid;
2683 /* Default version */
2685 proc_get_current_thread (procinfo *pi)
2693 /* Discover the IDs of all the threads within the process, and create
2694 a procinfo for each of them (chained to the parent). This
2695 unfortunately requires a different method on every OS. Returns
2696 non-zero for success, zero for failure. */
2699 proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore)
2701 if (thread && parent) /* sanity */
2703 thread->status_valid = 0;
2704 if (!proc_get_status (thread))
2705 destroy_one_procinfo (&parent->thread_list, thread);
2707 return 0; /* keep iterating */
2710 #if defined (PIOCLSTATUS)
2711 /* Solaris 2.5 (ioctl) version */
2713 proc_update_threads (procinfo *pi)
2715 gdb_prstatus_t *prstatus;
2716 struct cleanup *old_chain = NULL;
2720 /* We should never have to apply this operation to any procinfo
2721 except the one for the main process. If that ever changes for
2722 any reason, then take out the following clause and replace it
2723 with one that makes sure the ctl_fd is open. */
2726 pi = find_procinfo_or_die (pi->pid, 0);
2728 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
2730 if ((nlwp = proc_get_nthreads (pi)) <= 1)
2731 return 1; /* Process is not multi-threaded; nothing to do. */
2733 prstatus = xmalloc (sizeof (gdb_prstatus_t) * (nlwp + 1));
2735 old_chain = make_cleanup (xfree, prstatus);
2736 if (ioctl (pi->ctl_fd, PIOCLSTATUS, prstatus) < 0)
2737 proc_error (pi, "update_threads (PIOCLSTATUS)", __LINE__);
2739 /* Skip element zero, which represents the process as a whole. */
2740 for (i = 1; i < nlwp + 1; i++)
2742 if ((thread = create_procinfo (pi->pid, prstatus[i].pr_who)) == NULL)
2743 proc_error (pi, "update_threads, create_procinfo", __LINE__);
2745 memcpy (&thread->prstatus, &prstatus[i], sizeof (*prstatus));
2746 thread->status_valid = 1;
2748 pi->threads_valid = 1;
2749 do_cleanups (old_chain);
2754 /* Solaris 6 (and later) version. */
2756 do_closedir_cleanup (void *dir)
2762 proc_update_threads (procinfo *pi)
2764 char pathname[MAX_PROC_NAME_SIZE + 16];
2765 struct dirent *direntry;
2766 struct cleanup *old_chain = NULL;
2771 /* We should never have to apply this operation to any procinfo
2772 except the one for the main process. If that ever changes for
2773 any reason, then take out the following clause and replace it
2774 with one that makes sure the ctl_fd is open. */
2777 pi = find_procinfo_or_die (pi->pid, 0);
2779 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
2781 /* Note: this brute-force method was originally devised for Unixware
2782 (support removed since), and will also work on Solaris 2.6 and
2783 2.7. The original comment mentioned the existence of a much
2784 simpler and more elegant way to do this on Solaris, but didn't
2785 point out what that was. */
2787 strcpy (pathname, pi->pathname);
2788 strcat (pathname, "/lwp");
2789 if ((dirp = opendir (pathname)) == NULL)
2790 proc_error (pi, "update_threads, opendir", __LINE__);
2792 old_chain = make_cleanup (do_closedir_cleanup, dirp);
2793 while ((direntry = readdir (dirp)) != NULL)
2794 if (direntry->d_name[0] != '.') /* skip '.' and '..' */
2796 lwpid = atoi (&direntry->d_name[0]);
2797 if ((thread = create_procinfo (pi->pid, lwpid)) == NULL)
2798 proc_error (pi, "update_threads, create_procinfo", __LINE__);
2800 pi->threads_valid = 1;
2801 do_cleanups (old_chain);
2808 proc_update_threads (procinfo *pi)
2813 /* We should never have to apply this operation to any procinfo
2814 except the one for the main process. If that ever changes for
2815 any reason, then take out the following clause and replace it
2816 with one that makes sure the ctl_fd is open. */
2819 pi = find_procinfo_or_die (pi->pid, 0);
2821 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
2823 nthreads = proc_get_nthreads (pi);
2825 return 0; /* Nothing to do for 1 or fewer threads. */
2827 threads = xmalloc (nthreads * sizeof (tid_t));
2829 if (ioctl (pi->ctl_fd, PIOCTLIST, threads) < 0)
2830 proc_error (pi, "procfs: update_threads (PIOCTLIST)", __LINE__);
2832 for (i = 0; i < nthreads; i++)
2834 if (!find_procinfo (pi->pid, threads[i]))
2835 if (!create_procinfo (pi->pid, threads[i]))
2836 proc_error (pi, "update_threads, create_procinfo", __LINE__);
2838 pi->threads_valid = 1;
2842 /* Default version */
2844 proc_update_threads (procinfo *pi)
2848 #endif /* OSF PIOCTLIST */
2849 #endif /* NEW_PROC_API */
2850 #endif /* SOL 2.5 PIOCLSTATUS */
2852 /* Given a pointer to a function, call that function once for each lwp
2853 in the procinfo list, until the function returns non-zero, in which
2854 event return the value returned by the function.
2856 Note: this function does NOT call update_threads. If you want to
2857 discover new threads first, you must call that function explicitly.
2858 This function just makes a quick pass over the currently-known
2861 PI is the parent process procinfo. FUNC is the per-thread
2862 function. PTR is an opaque parameter for function. Returns the
2863 first non-zero return value from the callee, or zero. */
2866 proc_iterate_over_threads (procinfo *pi,
2867 int (*func) (procinfo *, procinfo *, void *),
2870 procinfo *thread, *next;
2873 /* We should never have to apply this operation to any procinfo
2874 except the one for the main process. If that ever changes for
2875 any reason, then take out the following clause and replace it
2876 with one that makes sure the ctl_fd is open. */
2879 pi = find_procinfo_or_die (pi->pid, 0);
2881 for (thread = pi->thread_list; thread != NULL; thread = next)
2883 next = thread->next; /* In case thread is destroyed. */
2884 if ((retval = (*func) (pi, thread, ptr)) != 0)
2891 /* =================== END, Thread "MODULE" =================== */
2893 /* =================== END, /proc "MODULE" =================== */
2895 /* =================== GDB "MODULE" =================== */
2897 /* Here are all of the gdb target vector functions and their
2900 static ptid_t do_attach (ptid_t ptid);
2901 static void do_detach (int signo);
2902 static void proc_trace_syscalls_1 (procinfo *pi, int syscallnum,
2903 int entry_or_exit, int mode, int from_tty);
2905 /* On mips-irix, we need to insert a breakpoint at __dbx_link during
2906 the startup phase. The following two variables are used to record
2907 the address of the breakpoint, and the code that was replaced by
2909 static int dbx_link_bpt_addr = 0;
2910 static void *dbx_link_bpt;
2912 /* Sets up the inferior to be debugged. Registers to trace signals,
2913 hardware faults, and syscalls. Note: does not set RLC flag: caller
2914 may want to customize that. Returns zero for success (note!
2915 unlike most functions in this module); on failure, returns the LINE
2916 NUMBER where it failed! */
2919 procfs_debug_inferior (procinfo *pi)
2921 fltset_t traced_faults;
2922 gdb_sigset_t traced_signals;
2923 sysset_t *traced_syscall_entries;
2924 sysset_t *traced_syscall_exits;
2927 #ifdef PROCFS_DONT_TRACE_FAULTS
2928 /* On some systems (OSF), we don't trace hardware faults.
2929 Apparently it's enough that we catch them as signals.
2930 Wonder why we don't just do that in general? */
2931 premptyset (&traced_faults); /* don't trace faults. */
2933 /* Register to trace hardware faults in the child. */
2934 prfillset (&traced_faults); /* trace all faults... */
2935 gdb_prdelset (&traced_faults, FLTPAGE); /* except page fault. */
2937 if (!proc_set_traced_faults (pi, &traced_faults))
2940 /* Initially, register to trace all signals in the child. */
2941 prfillset (&traced_signals);
2942 if (!proc_set_traced_signals (pi, &traced_signals))
2946 /* Register to trace the 'exit' system call (on entry). */
2947 traced_syscall_entries = sysset_t_alloc (pi);
2948 gdb_premptysysset (traced_syscall_entries);
2950 gdb_praddsysset (traced_syscall_entries, SYS_exit);
2953 gdb_praddsysset (traced_syscall_entries, SYS_lwpexit);/* And _lwp_exit... */
2956 gdb_praddsysset (traced_syscall_entries, SYS_lwp_exit);
2958 #ifdef DYNAMIC_SYSCALLS
2960 int callnum = find_syscall (pi, "_exit");
2963 gdb_praddsysset (traced_syscall_entries, callnum);
2967 status = proc_set_traced_sysentry (pi, traced_syscall_entries);
2968 xfree (traced_syscall_entries);
2972 #ifdef PRFS_STOPEXEC /* defined on OSF */
2973 /* OSF method for tracing exec syscalls. Quoting:
2974 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
2975 exits from exec system calls because of the user level loader. */
2976 /* FIXME: make nice and maybe move into an access function. */
2980 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
2983 prfs_flags |= PRFS_STOPEXEC;
2985 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
2988 #else /* not PRFS_STOPEXEC */
2989 /* Everyone else's (except OSF) method for tracing exec syscalls. */
2991 Not all systems with /proc have all the exec* syscalls with the same
2992 names. On the SGI, for example, there is no SYS_exec, but there
2993 *is* a SYS_execv. So, we try to account for that. */
2995 traced_syscall_exits = sysset_t_alloc (pi);
2996 gdb_premptysysset (traced_syscall_exits);
2998 gdb_praddsysset (traced_syscall_exits, SYS_exec);
3001 gdb_praddsysset (traced_syscall_exits, SYS_execve);
3004 gdb_praddsysset (traced_syscall_exits, SYS_execv);
3007 #ifdef SYS_lwpcreate
3008 gdb_praddsysset (traced_syscall_exits, SYS_lwpcreate);
3009 gdb_praddsysset (traced_syscall_exits, SYS_lwpexit);
3012 #ifdef SYS_lwp_create /* FIXME: once only, please. */
3013 gdb_praddsysset (traced_syscall_exits, SYS_lwp_create);
3014 gdb_praddsysset (traced_syscall_exits, SYS_lwp_exit);
3017 #ifdef DYNAMIC_SYSCALLS
3019 int callnum = find_syscall (pi, "execve");
3022 gdb_praddsysset (traced_syscall_exits, callnum);
3023 callnum = find_syscall (pi, "ra_execve");
3025 gdb_praddsysset (traced_syscall_exits, callnum);
3029 status = proc_set_traced_sysexit (pi, traced_syscall_exits);
3030 xfree (traced_syscall_exits);
3034 #endif /* PRFS_STOPEXEC */
3039 procfs_attach (struct target_ops *ops, const char *args, int from_tty)
3044 pid = parse_pid_to_attach (args);
3046 if (pid == getpid ())
3047 error (_("Attaching GDB to itself is not a good idea..."));
3051 exec_file = get_exec_file (0);
3054 printf_filtered (_("Attaching to program `%s', %s\n"),
3055 exec_file, target_pid_to_str (pid_to_ptid (pid)));
3057 printf_filtered (_("Attaching to %s\n"),
3058 target_pid_to_str (pid_to_ptid (pid)));
3062 inferior_ptid = do_attach (pid_to_ptid (pid));
3063 if (!target_is_pushed (ops))
3068 procfs_detach (struct target_ops *ops, const char *args, int from_tty)
3071 int pid = ptid_get_pid (inferior_ptid);
3080 exec_file = get_exec_file (0);
3081 if (exec_file == NULL)
3084 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file,
3085 target_pid_to_str (pid_to_ptid (pid)));
3086 gdb_flush (gdb_stdout);
3091 inferior_ptid = null_ptid;
3092 detach_inferior (pid);
3093 inf_child_maybe_unpush_target (ops);
3097 do_attach (ptid_t ptid)
3100 struct inferior *inf;
3104 if ((pi = create_procinfo (ptid_get_pid (ptid), 0)) == NULL)
3105 perror (_("procfs: out of memory in 'attach'"));
3107 if (!open_procinfo_files (pi, FD_CTL))
3109 fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__);
3110 sprintf (errmsg, "do_attach: couldn't open /proc file for process %d",
3111 ptid_get_pid (ptid));
3112 dead_procinfo (pi, errmsg, NOKILL);
3115 /* Stop the process (if it isn't already stopped). */
3116 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
3118 pi->was_stopped = 1;
3119 proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
3123 pi->was_stopped = 0;
3124 /* Set the process to run again when we close it. */
3125 if (!proc_set_run_on_last_close (pi))
3126 dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
3128 /* Now stop the process. */
3129 if (!proc_stop_process (pi))
3130 dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
3131 pi->ignore_next_sigstop = 1;
3133 /* Save some of the /proc state to be restored if we detach. */
3134 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
3135 dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
3136 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
3137 dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
3138 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
3139 dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
3141 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
3142 dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
3144 if (!proc_get_held_signals (pi, &pi->saved_sighold))
3145 dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
3147 if ((fail = procfs_debug_inferior (pi)) != 0)
3148 dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
3150 inf = current_inferior ();
3151 inferior_appeared (inf, pi->pid);
3152 /* Let GDB know that the inferior was attached. */
3153 inf->attach_flag = 1;
3155 /* Create a procinfo for the current lwp. */
3156 lwpid = proc_get_current_thread (pi);
3157 create_procinfo (pi->pid, lwpid);
3159 /* Add it to gdb's thread list. */
3160 ptid = ptid_build (pi->pid, lwpid, 0);
3167 do_detach (int signo)
3171 /* Find procinfo for the main process. */
3172 pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid),
3173 0); /* FIXME: threads */
3175 if (!proc_set_current_signal (pi, signo))
3176 proc_warn (pi, "do_detach, set_current_signal", __LINE__);
3178 if (!proc_set_traced_signals (pi, &pi->saved_sigset))
3179 proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
3181 if (!proc_set_traced_faults (pi, &pi->saved_fltset))
3182 proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
3184 if (!proc_set_traced_sysentry (pi, pi->saved_entryset))
3185 proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
3187 if (!proc_set_traced_sysexit (pi, pi->saved_exitset))
3188 proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
3190 if (!proc_set_held_signals (pi, &pi->saved_sighold))
3191 proc_warn (pi, "do_detach, set_held_signals", __LINE__);
3193 if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)))
3194 if (signo || !(pi->was_stopped) ||
3195 query (_("Was stopped when attached, make it runnable again? ")))
3197 /* Clear any pending signal. */
3198 if (!proc_clear_current_fault (pi))
3199 proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
3201 if (signo == 0 && !proc_clear_current_signal (pi))
3202 proc_warn (pi, "do_detach, clear_current_signal", __LINE__);
3204 if (!proc_set_run_on_last_close (pi))
3205 proc_warn (pi, "do_detach, set_rlc", __LINE__);
3208 destroy_procinfo (pi);
3211 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
3214 ??? Is the following note still relevant? We can't get individual
3215 registers with the PT_GETREGS ptrace(2) request either, yet we
3216 don't bother with caching at all in that case.
3218 NOTE: Since the /proc interface cannot give us individual
3219 registers, we pay no attention to REGNUM, and just fetch them all.
3220 This results in the possibility that we will do unnecessarily many
3221 fetches, since we may be called repeatedly for individual
3222 registers. So we cache the results, and mark the cache invalid
3223 when the process is resumed. */
3226 procfs_fetch_registers (struct target_ops *ops,
3227 struct regcache *regcache, int regnum)
3229 gdb_gregset_t *gregs;
3231 int pid = ptid_get_pid (inferior_ptid);
3232 int tid = ptid_get_lwp (inferior_ptid);
3233 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3235 pi = find_procinfo_or_die (pid, tid);
3238 error (_("procfs: fetch_registers failed to find procinfo for %s"),
3239 target_pid_to_str (inferior_ptid));
3241 gregs = proc_get_gregs (pi);
3243 proc_error (pi, "fetch_registers, get_gregs", __LINE__);
3245 supply_gregset (regcache, (const gdb_gregset_t *) gregs);
3247 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
3249 gdb_fpregset_t *fpregs;
3251 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
3252 || regnum == gdbarch_pc_regnum (gdbarch)
3253 || regnum == gdbarch_sp_regnum (gdbarch))
3254 return; /* Not a floating point register. */
3256 fpregs = proc_get_fpregs (pi);
3258 proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
3260 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregs);
3264 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
3265 this for all registers.
3267 NOTE: Since the /proc interface will not read individual registers,
3268 we will cache these requests until the process is resumed, and only
3269 then write them back to the inferior process.
3271 FIXME: is that a really bad idea? Have to think about cases where
3272 writing one register might affect the value of others, etc. */
3275 procfs_store_registers (struct target_ops *ops,
3276 struct regcache *regcache, int regnum)
3278 gdb_gregset_t *gregs;
3280 int pid = ptid_get_pid (inferior_ptid);
3281 int tid = ptid_get_lwp (inferior_ptid);
3282 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3284 pi = find_procinfo_or_die (pid, tid);
3287 error (_("procfs: store_registers: failed to find procinfo for %s"),
3288 target_pid_to_str (inferior_ptid));
3290 gregs = proc_get_gregs (pi);
3292 proc_error (pi, "store_registers, get_gregs", __LINE__);
3294 fill_gregset (regcache, gregs, regnum);
3295 if (!proc_set_gregs (pi))
3296 proc_error (pi, "store_registers, set_gregs", __LINE__);
3298 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
3300 gdb_fpregset_t *fpregs;
3302 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
3303 || regnum == gdbarch_pc_regnum (gdbarch)
3304 || regnum == gdbarch_sp_regnum (gdbarch))
3305 return; /* Not a floating point register. */
3307 fpregs = proc_get_fpregs (pi);
3309 proc_error (pi, "store_registers, get_fpregs", __LINE__);
3311 fill_fpregset (regcache, fpregs, regnum);
3312 if (!proc_set_fpregs (pi))
3313 proc_error (pi, "store_registers, set_fpregs", __LINE__);
3318 syscall_is_lwp_exit (procinfo *pi, int scall)
3321 if (scall == SYS_lwp_exit)
3325 if (scall == SYS_lwpexit)
3332 syscall_is_exit (procinfo *pi, int scall)
3335 if (scall == SYS_exit)
3338 #ifdef DYNAMIC_SYSCALLS
3339 if (find_syscall (pi, "_exit") == scall)
3346 syscall_is_exec (procinfo *pi, int scall)
3349 if (scall == SYS_exec)
3353 if (scall == SYS_execv)
3357 if (scall == SYS_execve)
3360 #ifdef DYNAMIC_SYSCALLS
3361 if (find_syscall (pi, "_execve"))
3363 if (find_syscall (pi, "ra_execve"))
3370 syscall_is_lwp_create (procinfo *pi, int scall)
3372 #ifdef SYS_lwp_create
3373 if (scall == SYS_lwp_create)
3376 #ifdef SYS_lwpcreate
3377 if (scall == SYS_lwpcreate)
3383 /* Remove the breakpoint that we inserted in __dbx_link().
3384 Does nothing if the breakpoint hasn't been inserted or has already
3388 remove_dbx_link_breakpoint (void)
3390 if (dbx_link_bpt_addr == 0)
3393 if (deprecated_remove_raw_breakpoint (target_gdbarch (), dbx_link_bpt) != 0)
3394 warning (_("Unable to remove __dbx_link breakpoint."));
3396 dbx_link_bpt_addr = 0;
3397 dbx_link_bpt = NULL;
3401 /* Return the address of the __dbx_link() function in the file
3402 refernced by ABFD by scanning its symbol table. Return 0 if
3403 the symbol was not found. */
3406 dbx_link_addr (bfd *abfd)
3408 long storage_needed;
3409 asymbol **symbol_table;
3410 long number_of_symbols;
3413 storage_needed = bfd_get_symtab_upper_bound (abfd);
3414 if (storage_needed <= 0)
3417 symbol_table = (asymbol **) xmalloc (storage_needed);
3418 make_cleanup (xfree, symbol_table);
3420 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
3422 for (i = 0; i < number_of_symbols; i++)
3424 asymbol *sym = symbol_table[i];
3426 if ((sym->flags & BSF_GLOBAL)
3427 && sym->name != NULL && strcmp (sym->name, "__dbx_link") == 0)
3428 return (sym->value + sym->section->vma);
3431 /* Symbol not found, return NULL. */
3435 /* Search the symbol table of the file referenced by FD for a symbol
3436 named __dbx_link(). If found, then insert a breakpoint at this location,
3437 and return nonzero. Return zero otherwise. */
3440 insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
3443 long storage_needed;
3446 abfd = gdb_bfd_fdopenr ("unamed", 0, fd);
3449 warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
3453 if (!bfd_check_format (abfd, bfd_object))
3455 /* Not the correct format, so we can not possibly find the dbx_link
3457 gdb_bfd_unref (abfd);
3461 sym_addr = dbx_link_addr (abfd);
3464 /* Insert the breakpoint. */
3465 dbx_link_bpt_addr = sym_addr;
3466 dbx_link_bpt = deprecated_insert_raw_breakpoint (target_gdbarch (), NULL,
3468 if (dbx_link_bpt == NULL)
3470 warning (_("Failed to insert dbx_link breakpoint."));
3471 gdb_bfd_unref (abfd);
3474 gdb_bfd_unref (abfd);
3478 gdb_bfd_unref (abfd);
3482 /* Calls the supplied callback function once for each mapped address
3483 space in the process. The callback function receives an open file
3484 descriptor for the file corresponding to that mapped address space
3485 (if there is one), and the base address of the mapped space. Quit
3486 when the callback function returns a nonzero value, or at teh end
3487 of the mappings. Returns the first non-zero return value of the
3488 callback function, or zero. */
3491 solib_mappings_callback (struct prmap *map, int (*func) (int, CORE_ADDR),
3494 procinfo *pi = data;
3498 char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)];
3500 if (map->pr_vaddr == 0 && map->pr_size == 0)
3501 return -1; /* sanity */
3503 if (map->pr_mapname[0] == 0)
3505 fd = -1; /* no map file */
3509 sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname);
3510 /* Note: caller's responsibility to close this fd! */
3511 fd = open_with_retry (name, O_RDONLY);
3512 /* Note: we don't test the above call for failure;
3513 we just pass the FD on as given. Sometimes there is
3514 no file, so the open may return failure, but that's
3518 fd = ioctl (pi->ctl_fd, PIOCOPENM, &map->pr_vaddr);
3519 /* Note: we don't test the above call for failure;
3520 we just pass the FD on as given. Sometimes there is
3521 no file, so the ioctl may return failure, but that's
3524 return (*func) (fd, (CORE_ADDR) map->pr_vaddr);
3527 /* If the given memory region MAP contains a symbol named __dbx_link,
3528 insert a breakpoint at this location and return nonzero. Return
3532 insert_dbx_link_bpt_in_region (struct prmap *map,
3533 find_memory_region_ftype child_func,
3536 procinfo *pi = (procinfo *) data;
3538 /* We know the symbol we're looking for is in a text region, so
3539 only look for it if the region is a text one. */
3540 if (map->pr_mflags & MA_EXEC)
3541 return solib_mappings_callback (map, insert_dbx_link_bpt_in_file, pi);
3546 /* Search all memory regions for a symbol named __dbx_link. If found,
3547 insert a breakpoint at its location, and return nonzero. Return zero
3551 insert_dbx_link_breakpoint (procinfo *pi)
3553 return iterate_over_mappings (pi, NULL, pi, insert_dbx_link_bpt_in_region);
3557 /* Retrieve the next stop event from the child process. If child has
3558 not stopped yet, wait for it to stop. Translate /proc eventcodes
3559 (or possibly wait eventcodes) into gdb internal event codes.
3560 Returns the id of process (and possibly thread) that incurred the
3561 event. Event codes are returned through a pointer parameter. */
3564 procfs_wait (struct target_ops *ops,
3565 ptid_t ptid, struct target_waitstatus *status, int options)
3567 /* First cut: loosely based on original version 2.1. */
3571 ptid_t retval, temp_ptid;
3572 int why, what, flags;
3579 retval = pid_to_ptid (-1);
3581 /* Find procinfo for main process. */
3582 pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0);
3585 /* We must assume that the status is stale now... */
3586 pi->status_valid = 0;
3587 pi->gregs_valid = 0;
3588 pi->fpregs_valid = 0;
3590 #if 0 /* just try this out... */
3591 flags = proc_flags (pi);
3592 why = proc_why (pi);
3593 if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
3594 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
3596 /* If child is not stopped, wait for it to stop. */
3597 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) &&
3598 !proc_wait_for_stop (pi))
3600 /* wait_for_stop failed: has the child terminated? */
3601 if (errno == ENOENT)
3605 /* /proc file not found; presumably child has terminated. */
3606 wait_retval = wait (&wstat); /* "wait" for the child's exit. */
3609 if (wait_retval != ptid_get_pid (inferior_ptid))
3610 error (_("procfs: couldn't stop "
3611 "process %d: wait returned %d."),
3612 ptid_get_pid (inferior_ptid), wait_retval);
3613 /* FIXME: might I not just use waitpid?
3614 Or try find_procinfo to see if I know about this child? */
3615 retval = pid_to_ptid (wait_retval);
3617 else if (errno == EINTR)
3621 /* Unknown error from wait_for_stop. */
3622 proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
3627 /* This long block is reached if either:
3628 a) the child was already stopped, or
3629 b) we successfully waited for the child with wait_for_stop.
3630 This block will analyze the /proc status, and translate it
3631 into a waitstatus for GDB.
3633 If we actually had to call wait because the /proc file
3634 is gone (child terminated), then we skip this block,
3635 because we already have a waitstatus. */
3637 flags = proc_flags (pi);
3638 why = proc_why (pi);
3639 what = proc_what (pi);
3641 if (flags & (PR_STOPPED | PR_ISTOP))
3644 /* If it's running async (for single_thread control),
3645 set it back to normal again. */
3646 if (flags & PR_ASYNC)
3647 if (!proc_unset_async (pi))
3648 proc_error (pi, "target_wait, unset_async", __LINE__);
3652 proc_prettyprint_why (why, what, 1);
3654 /* The 'pid' we will return to GDB is composed of
3655 the process ID plus the lwp ID. */
3656 retval = ptid_build (pi->pid, proc_get_current_thread (pi), 0);
3660 wstat = (what << 8) | 0177;
3663 if (syscall_is_lwp_exit (pi, what))
3665 if (print_thread_events)
3666 printf_unfiltered (_("[%s exited]\n"),
3667 target_pid_to_str (retval));
3668 delete_thread (retval);
3669 status->kind = TARGET_WAITKIND_SPURIOUS;
3672 else if (syscall_is_exit (pi, what))
3674 struct inferior *inf;
3676 /* Handle SYS_exit call only. */
3677 /* Stopped at entry to SYS_exit.
3678 Make it runnable, resume it, then use
3679 the wait system call to get its exit code.
3680 Proc_run_process always clears the current
3682 Then return its exit status. */
3683 pi->status_valid = 0;
3685 /* FIXME: what we should do is return
3686 TARGET_WAITKIND_SPURIOUS. */
3687 if (!proc_run_process (pi, 0, 0))
3688 proc_error (pi, "target_wait, run_process", __LINE__);
3690 inf = find_inferior_pid (pi->pid);
3691 if (inf->attach_flag)
3693 /* Don't call wait: simulate waiting for exit,
3694 return a "success" exit code. Bogus: what if
3695 it returns something else? */
3697 retval = inferior_ptid; /* ? ? ? */
3701 int temp = wait (&wstat);
3703 /* FIXME: shouldn't I make sure I get the right
3704 event from the right process? If (for
3705 instance) I have killed an earlier inferior
3706 process but failed to clean up after it
3707 somehow, I could get its termination event
3710 /* If wait returns -1, that's what we return
3713 retval = pid_to_ptid (temp);
3718 printf_filtered (_("procfs: trapped on entry to "));
3719 proc_prettyprint_syscall (proc_what (pi), 0);
3720 printf_filtered ("\n");
3723 long i, nsysargs, *sysargs;
3725 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
3726 (sysargs = proc_sysargs (pi)) != NULL)
3728 printf_filtered (_("%ld syscall arguments:\n"),
3730 for (i = 0; i < nsysargs; i++)
3731 printf_filtered ("#%ld: 0x%08lx\n",
3739 /* How to exit gracefully, returning "unknown
3741 status->kind = TARGET_WAITKIND_SPURIOUS;
3742 return inferior_ptid;
3746 /* How to keep going without returning to wfi: */
3747 target_resume (ptid, 0, GDB_SIGNAL_0);
3753 if (syscall_is_exec (pi, what))
3755 /* Hopefully this is our own "fork-child" execing
3756 the real child. Hoax this event into a trap, and
3757 GDB will see the child about to execute its start
3759 wstat = (SIGTRAP << 8) | 0177;
3762 else if (what == SYS_syssgi)
3764 /* see if we can break on dbx_link(). If yes, then
3765 we no longer need the SYS_syssgi notifications. */
3766 if (insert_dbx_link_breakpoint (pi))
3767 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT,
3770 /* This is an internal event and should be transparent
3771 to wfi, so resume the execution and wait again. See
3772 comment in procfs_init_inferior() for more details. */
3773 target_resume (ptid, 0, GDB_SIGNAL_0);
3777 else if (syscall_is_lwp_create (pi, what))
3779 /* This syscall is somewhat like fork/exec. We
3780 will get the event twice: once for the parent
3781 LWP, and once for the child. We should already
3782 know about the parent LWP, but the child will
3783 be new to us. So, whenever we get this event,
3784 if it represents a new thread, simply add the
3785 thread to the list. */
3787 /* If not in procinfo list, add it. */
3788 temp_tid = proc_get_current_thread (pi);
3789 if (!find_procinfo (pi->pid, temp_tid))
3790 create_procinfo (pi->pid, temp_tid);
3792 temp_ptid = ptid_build (pi->pid, temp_tid, 0);
3793 /* If not in GDB's thread list, add it. */
3794 if (!in_thread_list (temp_ptid))
3795 add_thread (temp_ptid);
3797 /* Return to WFI, but tell it to immediately resume. */
3798 status->kind = TARGET_WAITKIND_SPURIOUS;
3799 return inferior_ptid;
3801 else if (syscall_is_lwp_exit (pi, what))
3803 if (print_thread_events)
3804 printf_unfiltered (_("[%s exited]\n"),
3805 target_pid_to_str (retval));
3806 delete_thread (retval);
3807 status->kind = TARGET_WAITKIND_SPURIOUS;
3812 /* FIXME: Do we need to handle SYS_sproc,
3813 SYS_fork, or SYS_vfork here? The old procfs
3814 seemed to use this event to handle threads on
3815 older (non-LWP) systems, where I'm assuming
3816 that threads were actually separate processes.
3817 Irix, maybe? Anyway, low priority for now. */
3821 printf_filtered (_("procfs: trapped on exit from "));
3822 proc_prettyprint_syscall (proc_what (pi), 0);
3823 printf_filtered ("\n");
3826 long i, nsysargs, *sysargs;
3828 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
3829 (sysargs = proc_sysargs (pi)) != NULL)
3831 printf_filtered (_("%ld syscall arguments:\n"),
3833 for (i = 0; i < nsysargs; i++)
3834 printf_filtered ("#%ld: 0x%08lx\n",
3839 status->kind = TARGET_WAITKIND_SPURIOUS;
3840 return inferior_ptid;
3845 wstat = (SIGSTOP << 8) | 0177;
3850 printf_filtered (_("Retry #%d:\n"), retry);
3851 pi->status_valid = 0;
3856 /* If not in procinfo list, add it. */
3857 temp_tid = proc_get_current_thread (pi);
3858 if (!find_procinfo (pi->pid, temp_tid))
3859 create_procinfo (pi->pid, temp_tid);
3861 /* If not in GDB's thread list, add it. */
3862 temp_ptid = ptid_build (pi->pid, temp_tid, 0);
3863 if (!in_thread_list (temp_ptid))
3864 add_thread (temp_ptid);
3866 status->kind = TARGET_WAITKIND_STOPPED;
3867 status->value.sig = 0;
3872 wstat = (what << 8) | 0177;
3878 wstat = (SIGTRAP << 8) | 0177;
3883 wstat = (SIGTRAP << 8) | 0177;
3886 /* FIXME: use si_signo where possible. */
3888 #if (FLTILL != FLTPRIV) /* Avoid "duplicate case" error. */
3891 wstat = (SIGILL << 8) | 0177;
3894 #if (FLTTRACE != FLTBPT) /* Avoid "duplicate case" error. */
3897 /* If we hit our __dbx_link() internal breakpoint,
3898 then remove it. See comments in procfs_init_inferior()
3899 for more details. */
3900 if (dbx_link_bpt_addr != 0
3901 && dbx_link_bpt_addr
3902 == regcache_read_pc (get_current_regcache ()))
3903 remove_dbx_link_breakpoint ();
3905 wstat = (SIGTRAP << 8) | 0177;
3909 #if (FLTBOUNDS != FLTSTACK) /* Avoid "duplicate case" error. */
3912 wstat = (SIGSEGV << 8) | 0177;
3916 #if (FLTFPE != FLTIOVF) /* Avoid "duplicate case" error. */
3919 wstat = (SIGFPE << 8) | 0177;
3921 case FLTPAGE: /* Recoverable page fault */
3922 default: /* FIXME: use si_signo if possible for
3924 retval = pid_to_ptid (-1);
3925 printf_filtered ("procfs:%d -- ", __LINE__);
3926 printf_filtered (_("child stopped for unknown reason:\n"));
3927 proc_prettyprint_why (why, what, 1);
3928 error (_("... giving up..."));
3931 break; /* case PR_FAULTED: */
3932 default: /* switch (why) unmatched */
3933 printf_filtered ("procfs:%d -- ", __LINE__);
3934 printf_filtered (_("child stopped for unknown reason:\n"));
3935 proc_prettyprint_why (why, what, 1);
3936 error (_("... giving up..."));
3939 /* Got this far without error: If retval isn't in the
3940 threads database, add it. */
3941 if (ptid_get_pid (retval) > 0 &&
3942 !ptid_equal (retval, inferior_ptid) &&
3943 !in_thread_list (retval))
3945 /* We have a new thread. We need to add it both to
3946 GDB's list and to our own. If we don't create a
3947 procinfo, resume may be unhappy later. */
3948 add_thread (retval);
3949 if (find_procinfo (ptid_get_pid (retval),
3950 ptid_get_lwp (retval)) == NULL)
3951 create_procinfo (ptid_get_pid (retval),
3952 ptid_get_lwp (retval));
3955 else /* Flags do not indicate STOPPED. */
3957 /* surely this can't happen... */
3958 printf_filtered ("procfs:%d -- process not stopped.\n",
3960 proc_prettyprint_flags (flags, 1);
3961 error (_("procfs: ...giving up..."));
3966 store_waitstatus (status, wstat);
3972 /* Perform a partial transfer to/from the specified object. For
3973 memory transfers, fall back to the old memory xfer functions. */
3975 static enum target_xfer_status
3976 procfs_xfer_partial (struct target_ops *ops, enum target_object object,
3977 const char *annex, gdb_byte *readbuf,
3978 const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
3979 ULONGEST *xfered_len)
3983 case TARGET_OBJECT_MEMORY:
3984 return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
3987 case TARGET_OBJECT_AUXV:
3988 return memory_xfer_auxv (ops, object, annex, readbuf, writebuf,
3989 offset, len, xfered_len);
3993 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
3994 readbuf, writebuf, offset, len,
3999 /* Helper for procfs_xfer_partial that handles memory transfers.
4000 Arguments are like target_xfer_partial. */
4002 static enum target_xfer_status
4003 procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
4004 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
4009 /* Find procinfo for main process. */
4010 pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0);
4011 if (pi->as_fd == 0 &&
4012 open_procinfo_files (pi, FD_AS) == 0)
4014 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
4015 return TARGET_XFER_E_IO;
4018 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr)
4019 return TARGET_XFER_E_IO;
4021 if (writebuf != NULL)
4023 PROCFS_NOTE ("write memory:\n");
4024 nbytes = write (pi->as_fd, writebuf, len);
4028 PROCFS_NOTE ("read memory:\n");
4029 nbytes = read (pi->as_fd, readbuf, len);
4032 return TARGET_XFER_E_IO;
4033 *xfered_len = nbytes;
4034 return TARGET_XFER_OK;
4037 /* Called by target_resume before making child runnable. Mark cached
4038 registers and status's invalid. If there are "dirty" caches that
4039 need to be written back to the child process, do that.
4041 File descriptors are also cached. As they are a limited resource,
4042 we cannot hold onto them indefinitely. However, as they are
4043 expensive to open, we don't want to throw them away
4044 indescriminately either. As a compromise, we will keep the file
4045 descriptors for the parent process, but discard any file
4046 descriptors we may have accumulated for the threads.
4048 As this function is called by iterate_over_threads, it always
4049 returns zero (so that iterate_over_threads will keep
4053 invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
4055 /* About to run the child; invalidate caches and do any other
4059 if (pi->gregs_dirty)
4060 if (parent == NULL ||
4061 proc_get_current_thread (parent) != pi->tid)
4062 if (!proc_set_gregs (pi)) /* flush gregs cache */
4063 proc_warn (pi, "target_resume, set_gregs",
4065 if (gdbarch_fp0_regnum (target_gdbarch ()) >= 0)
4066 if (pi->fpregs_dirty)
4067 if (parent == NULL ||
4068 proc_get_current_thread (parent) != pi->tid)
4069 if (!proc_set_fpregs (pi)) /* flush fpregs cache */
4070 proc_warn (pi, "target_resume, set_fpregs",
4076 /* The presence of a parent indicates that this is an LWP.
4077 Close any file descriptors that it might have open.
4078 We don't do this to the master (parent) procinfo. */
4080 close_procinfo_files (pi);
4082 pi->gregs_valid = 0;
4083 pi->fpregs_valid = 0;
4085 pi->gregs_dirty = 0;
4086 pi->fpregs_dirty = 0;
4088 pi->status_valid = 0;
4089 pi->threads_valid = 0;
4095 /* A callback function for iterate_over_threads. Find the
4096 asynchronous signal thread, and make it runnable. See if that
4097 helps matters any. */
4100 make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr)
4103 if (proc_flags (pi) & PR_ASLWP)
4105 if (!proc_run_process (pi, 0, -1))
4106 proc_error (pi, "make_signal_thread_runnable", __LINE__);
4114 /* Make the child process runnable. Normally we will then call
4115 procfs_wait and wait for it to stop again (unless gdb is async).
4117 If STEP is true, then arrange for the child to stop again after
4118 executing a single instruction. If SIGNO is zero, then cancel any
4119 pending signal; if non-zero, then arrange for the indicated signal
4120 to be delivered to the child when it runs. If PID is -1, then
4121 allow any child thread to run; if non-zero, then allow only the
4122 indicated thread to run. (not implemented yet). */
4125 procfs_resume (struct target_ops *ops,
4126 ptid_t ptid, int step, enum gdb_signal signo)
4128 procinfo *pi, *thread;
4132 prrun.prflags |= PRSVADDR;
4133 prrun.pr_vaddr = $PC; set resume address
4134 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4135 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4136 prrun.prflags |= PRCFAULT; clear current fault.
4138 PRSTRACE and PRSFAULT can be done by other means
4139 (proc_trace_signals, proc_trace_faults)
4140 PRSVADDR is unnecessary.
4141 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4142 This basically leaves PRSTEP and PRCSIG.
4143 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4144 So basically PR_STEP is the sole argument that must be passed
4145 to proc_run_process (for use in the prrun struct by ioctl). */
4147 /* Find procinfo for main process. */
4148 pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0);
4150 /* First cut: ignore pid argument. */
4153 /* Convert signal to host numbering. */
4155 (signo == GDB_SIGNAL_STOP && pi->ignore_next_sigstop))
4158 native_signo = gdb_signal_to_host (signo);
4160 pi->ignore_next_sigstop = 0;
4162 /* Running the process voids all cached registers and status. */
4163 /* Void the threads' caches first. */
4164 proc_iterate_over_threads (pi, invalidate_cache, NULL);
4165 /* Void the process procinfo's caches. */
4166 invalidate_cache (NULL, pi, NULL);
4168 if (ptid_get_pid (ptid) != -1)
4170 /* Resume a specific thread, presumably suppressing the
4172 thread = find_procinfo (ptid_get_pid (ptid), ptid_get_lwp (ptid));
4175 if (thread->tid != 0)
4177 /* We're to resume a specific thread, and not the
4178 others. Set the child process's PR_ASYNC flag. */
4180 if (!proc_set_async (pi))
4181 proc_error (pi, "target_resume, set_async", __LINE__);
4184 proc_iterate_over_threads (pi,
4185 make_signal_thread_runnable,
4188 pi = thread; /* Substitute the thread's procinfo
4194 if (!proc_run_process (pi, step, native_signo))
4197 warning (_("resume: target already running. "
4198 "Pretend to resume, and hope for the best!"));
4200 proc_error (pi, "target_resume", __LINE__);
4204 /* Set up to trace signals in the child process. */
4207 procfs_pass_signals (struct target_ops *self,
4208 int numsigs, unsigned char *pass_signals)
4210 gdb_sigset_t signals;
4211 procinfo *pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0);
4214 prfillset (&signals);
4216 for (signo = 0; signo < NSIG; signo++)
4218 int target_signo = gdb_signal_from_host (signo);
4219 if (target_signo < numsigs && pass_signals[target_signo])
4220 gdb_prdelset (&signals, signo);
4223 if (!proc_set_traced_signals (pi, &signals))
4224 proc_error (pi, "pass_signals", __LINE__);
4227 /* Print status information about the child process. */
4230 procfs_files_info (struct target_ops *ignore)
4232 struct inferior *inf = current_inferior ();
4234 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
4235 inf->attach_flag? "attached": "child",
4236 target_pid_to_str (inferior_ptid));
4239 /* Stop the child process asynchronously, as when the gdb user types
4240 control-c or presses a "stop" button. Works by sending
4241 kill(SIGINT) to the child's process group. */
4244 procfs_stop (struct target_ops *self, ptid_t ptid)
4246 kill (-inferior_process_group (), SIGINT);
4249 /* Make it die. Wait for it to die. Clean up after it. Note: this
4250 should only be applied to the real process, not to an LWP, because
4251 of the check for parent-process. If we need this to work for an
4252 LWP, it needs some more logic. */
4255 unconditionally_kill_inferior (procinfo *pi)
4259 parent_pid = proc_parent_pid (pi);
4260 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4261 /* FIXME: use access functions. */
4262 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4263 before the PIOCKILL, otherwise it might generate a corrupted core
4264 file for the inferior. */
4265 if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0)
4267 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4270 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4271 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4272 to kill the inferior, otherwise it might remain stopped with a
4274 We do not check the result of the PIOCSSIG, the inferior might have
4277 gdb_siginfo_t newsiginfo;
4279 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
4280 newsiginfo.si_signo = SIGKILL;
4281 newsiginfo.si_code = 0;
4282 newsiginfo.si_errno = 0;
4283 newsiginfo.si_pid = getpid ();
4284 newsiginfo.si_uid = getuid ();
4285 /* FIXME: use proc_set_current_signal. */
4286 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
4288 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4289 if (!proc_kill (pi, SIGKILL))
4290 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
4291 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4292 destroy_procinfo (pi);
4294 /* If pi is GDB's child, wait for it to die. */
4295 if (parent_pid == getpid ())
4296 /* FIXME: should we use waitpid to make sure we get the right event?
4297 Should we check the returned event? */
4302 ret = waitpid (pi->pid, &status, 0);
4309 /* We're done debugging it, and we want it to go away. Then we want
4310 GDB to forget all about it. */
4313 procfs_kill_inferior (struct target_ops *ops)
4315 if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */
4317 /* Find procinfo for main process. */
4318 procinfo *pi = find_procinfo (ptid_get_pid (inferior_ptid), 0);
4321 unconditionally_kill_inferior (pi);
4322 target_mourn_inferior ();
4326 /* Forget we ever debugged this thing! */
4329 procfs_mourn_inferior (struct target_ops *ops)
4333 if (!ptid_equal (inferior_ptid, null_ptid))
4335 /* Find procinfo for main process. */
4336 pi = find_procinfo (ptid_get_pid (inferior_ptid), 0);
4338 destroy_procinfo (pi);
4341 generic_mourn_inferior ();
4343 if (dbx_link_bpt != NULL)
4345 deprecated_remove_raw_breakpoint (target_gdbarch (), dbx_link_bpt);
4346 dbx_link_bpt_addr = 0;
4347 dbx_link_bpt = NULL;
4350 inf_child_maybe_unpush_target (ops);
4353 /* When GDB forks to create a runnable inferior process, this function
4354 is called on the parent side of the fork. It's job is to do
4355 whatever is necessary to make the child ready to be debugged, and
4356 then wait for the child to synchronize. */
4359 procfs_init_inferior (struct target_ops *ops, int pid)
4362 gdb_sigset_t signals;
4366 /* This routine called on the parent side (GDB side)
4367 after GDB forks the inferior. */
4368 if (!target_is_pushed (ops))
4371 if ((pi = create_procinfo (pid, 0)) == NULL)
4372 perror (_("procfs: out of memory in 'init_inferior'"));
4374 if (!open_procinfo_files (pi, FD_CTL))
4375 proc_error (pi, "init_inferior, open_proc_files", __LINE__);
4379 open_procinfo_files // done
4382 procfs_notice_signals
4389 /* If not stopped yet, wait for it to stop. */
4390 if (!(proc_flags (pi) & PR_STOPPED) &&
4391 !(proc_wait_for_stop (pi)))
4392 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
4394 /* Save some of the /proc state to be restored if we detach. */
4395 /* FIXME: Why? In case another debugger was debugging it?
4396 We're it's parent, for Ghu's sake! */
4397 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
4398 proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
4399 if (!proc_get_held_signals (pi, &pi->saved_sighold))
4400 proc_error (pi, "init_inferior, get_held_signals", __LINE__);
4401 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
4402 proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
4403 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
4404 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
4405 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
4406 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
4408 if ((fail = procfs_debug_inferior (pi)) != 0)
4409 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
4411 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4412 and possibly even turning ON kill-on-last-close at this point. But
4413 I can't make that change without careful testing which I don't have
4414 time to do right now... */
4415 /* Turn on run-on-last-close flag so that the child
4416 will die if GDB goes away for some reason. */
4417 if (!proc_set_run_on_last_close (pi))
4418 proc_error (pi, "init_inferior, set_RLC", __LINE__);
4420 /* We now have have access to the lwpid of the main thread/lwp. */
4421 lwpid = proc_get_current_thread (pi);
4423 /* Create a procinfo for the main lwp. */
4424 create_procinfo (pid, lwpid);
4426 /* We already have a main thread registered in the thread table at
4427 this point, but it didn't have any lwp info yet. Notify the core
4428 about it. This changes inferior_ptid as well. */
4429 thread_change_ptid (pid_to_ptid (pid),
4430 ptid_build (pid, lwpid, 0));
4432 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
4435 /* On mips-irix, we need to stop the inferior early enough during
4436 the startup phase in order to be able to load the shared library
4437 symbols and insert the breakpoints that are located in these shared
4438 libraries. Stopping at the program entry point is not good enough
4439 because the -init code is executed before the execution reaches
4442 So what we need to do is to insert a breakpoint in the runtime
4443 loader (rld), more precisely in __dbx_link(). This procedure is
4444 called by rld once all shared libraries have been mapped, but before
4445 the -init code is executed. Unfortuantely, this is not straightforward,
4446 as rld is not part of the executable we are running, and thus we need
4447 the inferior to run until rld itself has been mapped in memory.
4449 For this, we trace all syssgi() syscall exit events. Each time
4450 we detect such an event, we iterate over each text memory maps,
4451 get its associated fd, and scan the symbol table for __dbx_link().
4452 When found, we know that rld has been mapped, and that we can insert
4453 the breakpoint at the symbol address. Once the dbx_link() breakpoint
4454 has been inserted, the syssgi() notifications are no longer necessary,
4455 so they should be canceled. */
4456 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT, FLAG_SET, 0);
4460 /* When GDB forks to create a new process, this function is called on
4461 the child side of the fork before GDB exec's the user program. Its
4462 job is to make the child minimally debuggable, so that the parent
4463 GDB process can connect to the child and take over. This function
4464 should do only the minimum to make that possible, and to
4465 synchronize with the parent process. The parent process should
4466 take care of the details. */
4469 procfs_set_exec_trap (void)
4471 /* This routine called on the child side (inferior side)
4472 after GDB forks the inferior. It must use only local variables,
4473 because it may be sharing data space with its parent. */
4478 if ((pi = create_procinfo (getpid (), 0)) == NULL)
4479 perror_with_name (_("procfs: create_procinfo failed in child."));
4481 if (open_procinfo_files (pi, FD_CTL) == 0)
4483 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
4484 gdb_flush (gdb_stderr);
4485 /* No need to call "dead_procinfo", because we're going to
4490 #ifdef PRFS_STOPEXEC /* defined on OSF */
4491 /* OSF method for tracing exec syscalls. Quoting:
4492 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4493 exits from exec system calls because of the user level loader. */
4494 /* FIXME: make nice and maybe move into an access function. */
4498 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
4500 proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__);
4501 gdb_flush (gdb_stderr);
4504 prfs_flags |= PRFS_STOPEXEC;
4506 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
4508 proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__);
4509 gdb_flush (gdb_stderr);
4513 #else /* not PRFS_STOPEXEC */
4514 /* Everyone else's (except OSF) method for tracing exec syscalls. */
4516 Not all systems with /proc have all the exec* syscalls with the same
4517 names. On the SGI, for example, there is no SYS_exec, but there
4518 *is* a SYS_execv. So, we try to account for that. */
4520 exitset = sysset_t_alloc (pi);
4521 gdb_premptysysset (exitset);
4523 gdb_praddsysset (exitset, SYS_exec);
4526 gdb_praddsysset (exitset, SYS_execve);
4529 gdb_praddsysset (exitset, SYS_execv);
4531 #ifdef DYNAMIC_SYSCALLS
4533 int callnum = find_syscall (pi, "execve");
4536 gdb_praddsysset (exitset, callnum);
4538 callnum = find_syscall (pi, "ra_execve");
4540 gdb_praddsysset (exitset, callnum);
4542 #endif /* DYNAMIC_SYSCALLS */
4544 if (!proc_set_traced_sysexit (pi, exitset))
4546 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
4547 gdb_flush (gdb_stderr);
4550 #endif /* PRFS_STOPEXEC */
4552 /* FIXME: should this be done in the parent instead? */
4553 /* Turn off inherit on fork flag so that all grand-children
4554 of gdb start with tracing flags cleared. */
4555 if (!proc_unset_inherit_on_fork (pi))
4556 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
4558 /* Turn off run on last close flag, so that the child process
4559 cannot run away just because we close our handle on it.
4560 We want it to wait for the parent to attach. */
4561 if (!proc_unset_run_on_last_close (pi))
4562 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
4564 /* FIXME: No need to destroy the procinfo --
4565 we have our own address space, and we're about to do an exec! */
4566 /*destroy_procinfo (pi);*/
4569 /* This function is called BEFORE gdb forks the inferior process. Its
4570 only real responsibility is to set things up for the fork, and tell
4571 GDB which two functions to call after the fork (one for the parent,
4572 and one for the child).
4574 This function does a complicated search for a unix shell program,
4575 which it then uses to parse arguments and environment variables to
4576 be sent to the child. I wonder whether this code could not be
4577 abstracted out and shared with other unix targets such as
4581 procfs_create_inferior (struct target_ops *ops, char *exec_file,
4582 char *allargs, char **env, int from_tty)
4584 char *shell_file = getenv ("SHELL");
4588 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
4591 /* We will be looking down the PATH to find shell_file. If we
4592 just do this the normal way (via execlp, which operates by
4593 attempting an exec for each element of the PATH until it
4594 finds one which succeeds), then there will be an exec for
4595 each failed attempt, each of which will cause a PR_SYSEXIT
4596 stop, and we won't know how to distinguish the PR_SYSEXIT's
4597 for these failed execs with the ones for successful execs
4598 (whether the exec has succeeded is stored at that time in the
4599 carry bit or some such architecture-specific and
4600 non-ABI-specified place).
4602 So I can't think of anything better than to search the PATH
4603 now. This has several disadvantages: (1) There is a race
4604 condition; if we find a file now and it is deleted before we
4605 exec it, we lose, even if the deletion leaves a valid file
4606 further down in the PATH, (2) there is no way to know exactly
4607 what an executable (in the sense of "capable of being
4608 exec'd") file is. Using access() loses because it may lose
4609 if the caller is the superuser; failing to use it loses if
4610 there are ACLs or some such. */
4614 /* FIXME-maybe: might want "set path" command so user can change what
4615 path is used from within GDB. */
4616 char *path = getenv ("PATH");
4618 struct stat statbuf;
4621 path = "/bin:/usr/bin";
4623 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
4624 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
4626 p1 = strchr (p, ':');
4631 strncpy (tryname, p, len);
4632 tryname[len] = '\0';
4633 strcat (tryname, "/");
4634 strcat (tryname, shell_file);
4635 if (access (tryname, X_OK) < 0)
4637 if (stat (tryname, &statbuf) < 0)
4639 if (!S_ISREG (statbuf.st_mode))
4640 /* We certainly need to reject directories. I'm not quite
4641 as sure about FIFOs, sockets, etc., but I kind of doubt
4642 that people want to exec() these things. */
4647 /* Not found. This must be an error rather than merely passing
4648 the file to execlp(), because execlp() would try all the
4649 exec()s, causing GDB to get confused. */
4650 error (_("procfs:%d -- Can't find shell %s in PATH"),
4651 __LINE__, shell_file);
4653 shell_file = tryname;
4656 pid = fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
4657 NULL, NULL, shell_file, NULL);
4659 procfs_init_inferior (ops, pid);
4662 /* An observer for the "inferior_created" event. */
4665 procfs_inferior_created (struct target_ops *ops, int from_tty)
4668 /* Make sure to cancel the syssgi() syscall-exit notifications.
4669 They should normally have been removed by now, but they may still
4670 be activated if the inferior doesn't use shared libraries, or if
4671 we didn't locate __dbx_link, or if we never stopped in __dbx_link.
4672 See procfs_init_inferior() for more details.
4674 Since these notifications are only ever enabled when we spawned
4675 the inferior ourselves, there is nothing to do when the inferior
4676 was created by attaching to an already running process, or when
4677 debugging a core file. */
4678 if (current_inferior ()->attach_flag || !target_can_run (¤t_target))
4681 proc_trace_syscalls_1 (find_procinfo_or_die (ptid_get_pid (inferior_ptid),
4682 0), SYS_syssgi, PR_SYSEXIT, FLAG_RESET, 0);
4686 /* Callback for find_new_threads. Calls "add_thread". */
4689 procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
4691 ptid_t gdb_threadid = ptid_build (pi->pid, thread->tid, 0);
4693 if (!in_thread_list (gdb_threadid) || is_exited (gdb_threadid))
4694 add_thread (gdb_threadid);
4699 /* Query all the threads that the target knows about, and give them
4700 back to GDB to add to its list. */
4703 procfs_find_new_threads (struct target_ops *ops)
4707 /* Find procinfo for main process. */
4708 pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0);
4709 proc_update_threads (pi);
4710 proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
4713 /* Return true if the thread is still 'alive'. This guy doesn't
4714 really seem to be doing his job. Got to investigate how to tell
4715 when a thread is really gone. */
4718 procfs_thread_alive (struct target_ops *ops, ptid_t ptid)
4723 proc = ptid_get_pid (ptid);
4724 thread = ptid_get_lwp (ptid);
4725 /* If I don't know it, it ain't alive! */
4726 if ((pi = find_procinfo (proc, thread)) == NULL)
4729 /* If I can't get its status, it ain't alive!
4730 What's more, I need to forget about it! */
4731 if (!proc_get_status (pi))
4733 destroy_procinfo (pi);
4736 /* I couldn't have got its status if it weren't alive, so it's
4741 /* Convert PTID to a string. Returns the string in a static
4745 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
4747 static char buf[80];
4749 if (ptid_get_lwp (ptid) == 0)
4750 sprintf (buf, "process %d", ptid_get_pid (ptid));
4752 sprintf (buf, "LWP %ld", ptid_get_lwp (ptid));
4757 /* Insert a watchpoint. */
4760 procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag,
4767 pi = find_procinfo_or_die (ptid_get_pid (ptid) == -1 ?
4768 ptid_get_pid (inferior_ptid) : ptid_get_pid (ptid),
4771 /* Translate from GDB's flags to /proc's. */
4772 if (len > 0) /* len == 0 means delete watchpoint. */
4774 switch (rwflag) { /* FIXME: need an enum! */
4775 case hw_write: /* default watchpoint (write) */
4776 pflags = WRITE_WATCHFLAG;
4778 case hw_read: /* read watchpoint */
4779 pflags = READ_WATCHFLAG;
4781 case hw_access: /* access watchpoint */
4782 pflags = READ_WATCHFLAG | WRITE_WATCHFLAG;
4784 case hw_execute: /* execution HW breakpoint */
4785 pflags = EXEC_WATCHFLAG;
4787 default: /* Something weird. Return error. */
4790 if (after) /* Stop after r/w access is completed. */
4791 pflags |= AFTER_WATCHFLAG;
4794 if (!proc_set_watchpoint (pi, addr, len, pflags))
4796 if (errno == E2BIG) /* Typical error for no resources. */
4797 return -1; /* fail */
4798 /* GDB may try to remove the same watchpoint twice.
4799 If a remove request returns no match, don't error. */
4800 if (errno == ESRCH && len == 0)
4801 return 0; /* ignore */
4802 proc_error (pi, "set_watchpoint", __LINE__);
4808 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
4809 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
4810 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
4813 Note: procfs_can_use_hw_breakpoint() is not yet used by all
4814 procfs.c targets due to the fact that some of them still define
4815 target_can_use_hardware_watchpoint. */
4818 procfs_can_use_hw_breakpoint (struct target_ops *self,
4819 int type, int cnt, int othertype)
4821 /* Due to the way that proc_set_watchpoint() is implemented, host
4822 and target pointers must be of the same size. If they are not,
4823 we can't use hardware watchpoints. This limitation is due to the
4824 fact that proc_set_watchpoint() calls
4825 procfs_address_to_host_pointer(); a close inspection of
4826 procfs_address_to_host_pointer will reveal that an internal error
4827 will be generated when the host and target pointer sizes are
4829 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
4831 if (sizeof (void *) != TYPE_LENGTH (ptr_type))
4834 /* Other tests here??? */
4839 /* Returns non-zero if process is stopped on a hardware watchpoint
4840 fault, else returns zero. */
4843 procfs_stopped_by_watchpoint (struct target_ops *ops)
4847 pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0);
4849 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
4851 if (proc_why (pi) == PR_FAULTED)
4854 if (proc_what (pi) == FLTWATCH)
4858 if (proc_what (pi) == FLTKWATCH)
4866 /* Returns 1 if the OS knows the position of the triggered watchpoint,
4867 and sets *ADDR to that address. Returns 0 if OS cannot report that
4868 address. This function is only called if
4869 procfs_stopped_by_watchpoint returned 1, thus no further checks are
4870 done. The function also assumes that ADDR is not NULL. */
4873 procfs_stopped_data_address (struct target_ops *targ, CORE_ADDR *addr)
4877 pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0);
4878 return proc_watchpoint_address (pi, addr);
4882 procfs_insert_watchpoint (struct target_ops *self,
4883 CORE_ADDR addr, int len, int type,
4884 struct expression *cond)
4886 if (!target_have_steppable_watchpoint
4887 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
4889 /* When a hardware watchpoint fires off the PC will be left at
4890 the instruction following the one which caused the
4891 watchpoint. It will *NOT* be necessary for GDB to step over
4893 return procfs_set_watchpoint (inferior_ptid, addr, len, type, 1);
4897 /* When a hardware watchpoint fires off the PC will be left at
4898 the instruction which caused the watchpoint. It will be
4899 necessary for GDB to step over the watchpoint. */
4900 return procfs_set_watchpoint (inferior_ptid, addr, len, type, 0);
4905 procfs_remove_watchpoint (struct target_ops *self,
4906 CORE_ADDR addr, int len, int type,
4907 struct expression *cond)
4909 return procfs_set_watchpoint (inferior_ptid, addr, 0, 0, 0);
4913 procfs_region_ok_for_hw_watchpoint (struct target_ops *self,
4914 CORE_ADDR addr, int len)
4916 /* The man page for proc(4) on Solaris 2.6 and up says that the
4917 system can support "thousands" of hardware watchpoints, but gives
4918 no method for finding out how many; It doesn't say anything about
4919 the allowed size for the watched area either. So we just tell
4925 procfs_use_watchpoints (struct target_ops *t)
4927 t->to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
4928 t->to_insert_watchpoint = procfs_insert_watchpoint;
4929 t->to_remove_watchpoint = procfs_remove_watchpoint;
4930 t->to_region_ok_for_hw_watchpoint = procfs_region_ok_for_hw_watchpoint;
4931 t->to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
4932 t->to_stopped_data_address = procfs_stopped_data_address;
4935 /* Memory Mappings Functions: */
4937 /* Call a callback function once for each mapping, passing it the
4938 mapping, an optional secondary callback function, and some optional
4939 opaque data. Quit and return the first non-zero value returned
4942 PI is the procinfo struct for the process to be mapped. FUNC is
4943 the callback function to be called by this iterator. DATA is the
4944 optional opaque data to be passed to the callback function.
4945 CHILD_FUNC is the optional secondary function pointer to be passed
4946 to the child function. Returns the first non-zero return value
4947 from the callback function, or zero. */
4950 iterate_over_mappings (procinfo *pi, find_memory_region_ftype child_func,
4952 int (*func) (struct prmap *map,
4953 find_memory_region_ftype child_func,
4956 char pathname[MAX_PROC_NAME_SIZE];
4957 struct prmap *prmaps;
4958 struct prmap *prmap;
4962 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
4967 /* Get the number of mappings, allocate space,
4968 and read the mappings into prmaps. */
4971 sprintf (pathname, "/proc/%d/map", pi->pid);
4972 if ((map_fd = open (pathname, O_RDONLY)) < 0)
4973 proc_error (pi, "iterate_over_mappings (open)", __LINE__);
4975 /* Make sure it gets closed again. */
4976 make_cleanup_close (map_fd);
4978 /* Use stat to determine the file size, and compute
4979 the number of prmap_t objects it contains. */
4980 if (fstat (map_fd, &sbuf) != 0)
4981 proc_error (pi, "iterate_over_mappings (fstat)", __LINE__);
4983 nmap = sbuf.st_size / sizeof (prmap_t);
4984 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
4985 if (read (map_fd, (char *) prmaps, nmap * sizeof (*prmaps))
4986 != (nmap * sizeof (*prmaps)))
4987 proc_error (pi, "iterate_over_mappings (read)", __LINE__);
4989 /* Use ioctl command PIOCNMAP to get number of mappings. */
4990 if (ioctl (pi->ctl_fd, PIOCNMAP, &nmap) != 0)
4991 proc_error (pi, "iterate_over_mappings (PIOCNMAP)", __LINE__);
4993 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
4994 if (ioctl (pi->ctl_fd, PIOCMAP, prmaps) != 0)
4995 proc_error (pi, "iterate_over_mappings (PIOCMAP)", __LINE__);
4998 for (prmap = prmaps; nmap > 0; prmap++, nmap--)
4999 if ((funcstat = (*func) (prmap, child_func, data)) != 0)
5001 do_cleanups (cleanups);
5005 do_cleanups (cleanups);
5009 /* Implements the to_find_memory_regions method. Calls an external
5010 function for each memory region.
5011 Returns the integer value returned by the callback. */
5014 find_memory_regions_callback (struct prmap *map,
5015 find_memory_region_ftype func, void *data)
5017 return (*func) ((CORE_ADDR) map->pr_vaddr,
5019 (map->pr_mflags & MA_READ) != 0,
5020 (map->pr_mflags & MA_WRITE) != 0,
5021 (map->pr_mflags & MA_EXEC) != 0,
5022 1, /* MODIFIED is unknown, pass it as true. */
5026 /* External interface. Calls a callback function once for each
5027 mapped memory region in the child process, passing as arguments:
5029 CORE_ADDR virtual_address,
5031 int read, TRUE if region is readable by the child
5032 int write, TRUE if region is writable by the child
5033 int execute TRUE if region is executable by the child.
5035 Stops iterating and returns the first non-zero value returned by
5039 proc_find_memory_regions (struct target_ops *self,
5040 find_memory_region_ftype func, void *data)
5042 procinfo *pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0);
5044 return iterate_over_mappings (pi, func, data,
5045 find_memory_regions_callback);
5048 /* Returns an ascii representation of a memory mapping's flags. */
5051 mappingflags (long flags)
5053 static char asciiflags[8];
5055 strcpy (asciiflags, "-------");
5056 #if defined (MA_PHYS)
5057 if (flags & MA_PHYS)
5058 asciiflags[0] = 'd';
5060 if (flags & MA_STACK)
5061 asciiflags[1] = 's';
5062 if (flags & MA_BREAK)
5063 asciiflags[2] = 'b';
5064 if (flags & MA_SHARED)
5065 asciiflags[3] = 's';
5066 if (flags & MA_READ)
5067 asciiflags[4] = 'r';
5068 if (flags & MA_WRITE)
5069 asciiflags[5] = 'w';
5070 if (flags & MA_EXEC)
5071 asciiflags[6] = 'x';
5072 return (asciiflags);
5075 /* Callback function, does the actual work for 'info proc
5079 info_mappings_callback (struct prmap *map, find_memory_region_ftype ignore,
5082 unsigned int pr_off;
5084 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5085 pr_off = (unsigned int) map->pr_offset;
5087 pr_off = map->pr_off;
5090 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
5091 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
5092 (unsigned long) map->pr_vaddr,
5093 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5094 (unsigned long) map->pr_size,
5096 mappingflags (map->pr_mflags));
5098 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
5099 (unsigned long) map->pr_vaddr,
5100 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5101 (unsigned long) map->pr_size,
5103 mappingflags (map->pr_mflags));
5108 /* Implement the "info proc mappings" subcommand. */
5111 info_proc_mappings (procinfo *pi, int summary)
5114 return; /* No output for summary mode. */
5116 printf_filtered (_("Mapped address spaces:\n\n"));
5117 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
5118 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
5125 printf_filtered (" %18s %18s %10s %10s %7s\n",
5132 iterate_over_mappings (pi, NULL, NULL, info_mappings_callback);
5133 printf_filtered ("\n");
5136 /* Implement the "info proc" command. */
5139 procfs_info_proc (struct target_ops *ops, const char *args,
5140 enum info_proc_what what)
5142 struct cleanup *old_chain;
5143 procinfo *process = NULL;
5144 procinfo *thread = NULL;
5162 error (_("Not supported on this target."));
5165 old_chain = make_cleanup (null_cleanup, 0);
5168 argv = gdb_buildargv (args);
5169 make_cleanup_freeargv (argv);
5171 while (argv != NULL && *argv != NULL)
5173 if (isdigit (argv[0][0]))
5175 pid = strtoul (argv[0], &tmp, 10);
5177 tid = strtoul (++tmp, NULL, 10);
5179 else if (argv[0][0] == '/')
5181 tid = strtoul (argv[0] + 1, NULL, 10);
5186 pid = ptid_get_pid (inferior_ptid);
5188 error (_("No current process: you must name one."));
5191 /* Have pid, will travel.
5192 First see if it's a process we're already debugging. */
5193 process = find_procinfo (pid, 0);
5194 if (process == NULL)
5196 /* No. So open a procinfo for it, but
5197 remember to close it again when finished. */
5198 process = create_procinfo (pid, 0);
5199 make_cleanup (do_destroy_procinfo_cleanup, process);
5200 if (!open_procinfo_files (process, FD_CTL))
5201 proc_error (process, "info proc, open_procinfo_files", __LINE__);
5205 thread = create_procinfo (pid, tid);
5209 printf_filtered (_("process %d flags:\n"), process->pid);
5210 proc_prettyprint_flags (proc_flags (process), 1);
5211 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
5212 proc_prettyprint_why (proc_why (process), proc_what (process), 1);
5213 if (proc_get_nthreads (process) > 1)
5214 printf_filtered ("Process has %d threads.\n",
5215 proc_get_nthreads (process));
5219 printf_filtered (_("thread %d flags:\n"), thread->tid);
5220 proc_prettyprint_flags (proc_flags (thread), 1);
5221 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
5222 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
5227 info_proc_mappings (process, 0);
5230 do_cleanups (old_chain);
5233 /* Modify the status of the system call identified by SYSCALLNUM in
5234 the set of syscalls that are currently traced/debugged.
5236 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
5237 will be updated. Otherwise, the exit syscalls set will be updated.
5239 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
5240 will be disabled. */
5243 proc_trace_syscalls_1 (procinfo *pi, int syscallnum, int entry_or_exit,
5244 int mode, int from_tty)
5248 if (entry_or_exit == PR_SYSENTRY)
5249 sysset = proc_get_traced_sysentry (pi, NULL);
5251 sysset = proc_get_traced_sysexit (pi, NULL);
5254 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
5256 if (mode == FLAG_SET)
5257 gdb_praddsysset (sysset, syscallnum);
5259 gdb_prdelsysset (sysset, syscallnum);
5261 if (entry_or_exit == PR_SYSENTRY)
5263 if (!proc_set_traced_sysentry (pi, sysset))
5264 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
5268 if (!proc_set_traced_sysexit (pi, sysset))
5269 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
5274 proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode)
5278 if (ptid_get_pid (inferior_ptid) <= 0)
5279 error (_("you must be debugging a process to use this command."));
5281 if (args == NULL || args[0] == 0)
5282 error_no_arg (_("system call to trace"));
5284 pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0);
5285 if (isdigit (args[0]))
5287 const int syscallnum = atoi (args);
5289 proc_trace_syscalls_1 (pi, syscallnum, entry_or_exit, mode, from_tty);
5294 proc_trace_sysentry_cmd (char *args, int from_tty)
5296 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
5300 proc_trace_sysexit_cmd (char *args, int from_tty)
5302 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
5306 proc_untrace_sysentry_cmd (char *args, int from_tty)
5308 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
5312 proc_untrace_sysexit_cmd (char *args, int from_tty)
5314 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
5318 /* Provide a prototype to silence -Wmissing-prototypes. */
5319 extern void _initialize_procfs (void);
5322 _initialize_procfs (void)
5324 observer_attach_inferior_created (procfs_inferior_created);
5326 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
5327 _("Give a trace of entries into the syscall."));
5328 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
5329 _("Give a trace of exits from the syscall."));
5330 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
5331 _("Cancel a trace of entries into the syscall."));
5332 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
5333 _("Cancel a trace of exits from the syscall."));
5336 /* =================== END, GDB "MODULE" =================== */
5340 /* miscellaneous stubs: */
5342 /* The following satisfy a few random symbols mostly created by the
5343 solaris threads implementation, which I will chase down later. */
5345 /* Return a pid for which we guarantee we will be able to find a
5349 procfs_first_available (void)
5351 return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1);
5354 /* =================== GCORE .NOTE "MODULE" =================== */
5355 #if defined (PIOCOPENLWP) || defined (PCAGENT)
5356 /* gcore only implemented on solaris (so far) */
5359 procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
5360 char *note_data, int *note_size,
5361 enum gdb_signal stop_signal)
5363 struct regcache *regcache = get_thread_regcache (ptid);
5364 gdb_gregset_t gregs;
5365 gdb_fpregset_t fpregs;
5366 unsigned long merged_pid;
5367 struct cleanup *old_chain;
5369 merged_pid = ptid_get_lwp (ptid) << 16 | ptid_get_pid (ptid);
5371 /* This part is the old method for fetching registers.
5372 It should be replaced by the newer one using regsets
5373 once it is implemented in this platform:
5374 gdbarch_regset_from_core_section() and regset->collect_regset(). */
5376 old_chain = save_inferior_ptid ();
5377 inferior_ptid = ptid;
5378 target_fetch_registers (regcache, -1);
5380 fill_gregset (regcache, &gregs, -1);
5381 #if defined (NEW_PROC_API)
5382 note_data = (char *) elfcore_write_lwpstatus (obfd,
5389 note_data = (char *) elfcore_write_prstatus (obfd,
5396 fill_fpregset (regcache, &fpregs, -1);
5397 note_data = (char *) elfcore_write_prfpreg (obfd,
5403 do_cleanups (old_chain);
5408 struct procfs_corefile_thread_data {
5412 enum gdb_signal stop_signal;
5416 procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data)
5418 struct procfs_corefile_thread_data *args = data;
5422 ptid_t ptid = ptid_build (pi->pid, thread->tid, 0);
5424 args->note_data = procfs_do_thread_registers (args->obfd, ptid,
5433 find_signalled_thread (struct thread_info *info, void *data)
5435 if (info->suspend.stop_signal != GDB_SIGNAL_0
5436 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
5442 static enum gdb_signal
5443 find_stop_signal (void)
5445 struct thread_info *info =
5446 iterate_over_threads (find_signalled_thread, NULL);
5449 return info->suspend.stop_signal;
5451 return GDB_SIGNAL_0;
5455 procfs_make_note_section (struct target_ops *self, bfd *obfd, int *note_size)
5457 struct cleanup *old_chain;
5458 gdb_gregset_t gregs;
5459 gdb_fpregset_t fpregs;
5460 char fname[16] = {'\0'};
5461 char psargs[80] = {'\0'};
5462 procinfo *pi = find_procinfo_or_die (ptid_get_pid (inferior_ptid), 0);
5463 char *note_data = NULL;
5465 struct procfs_corefile_thread_data thread_args;
5468 enum gdb_signal stop_signal;
5470 if (get_exec_file (0))
5472 strncpy (fname, lbasename (get_exec_file (0)), sizeof (fname));
5473 fname[sizeof (fname) - 1] = 0;
5474 strncpy (psargs, get_exec_file (0), sizeof (psargs));
5475 psargs[sizeof (psargs) - 1] = 0;
5477 inf_args = get_inferior_args ();
5478 if (inf_args && *inf_args &&
5479 strlen (inf_args) < ((int) sizeof (psargs) - (int) strlen (psargs)))
5481 strncat (psargs, " ",
5482 sizeof (psargs) - strlen (psargs));
5483 strncat (psargs, inf_args,
5484 sizeof (psargs) - strlen (psargs));
5488 note_data = (char *) elfcore_write_prpsinfo (obfd,
5494 stop_signal = find_stop_signal ();
5497 fill_gregset (get_current_regcache (), &gregs, -1);
5498 note_data = elfcore_write_pstatus (obfd, note_data, note_size,
5499 ptid_get_pid (inferior_ptid),
5500 stop_signal, &gregs);
5503 thread_args.obfd = obfd;
5504 thread_args.note_data = note_data;
5505 thread_args.note_size = note_size;
5506 thread_args.stop_signal = stop_signal;
5507 proc_iterate_over_threads (pi, procfs_corefile_thread_callback,
5509 note_data = thread_args.note_data;
5511 auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV,
5515 note_data = elfcore_write_note (obfd, note_data, note_size,
5516 "CORE", NT_AUXV, auxv, auxv_len);
5522 #else /* !Solaris */
5524 procfs_make_note_section (struct target_ops *self, bfd *obfd, int *note_size)
5526 error (_("gcore not implemented for this host."));
5527 return NULL; /* lint */
5529 #endif /* Solaris */
5530 /* =================== END GCORE .NOTE "MODULE" =================== */