1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
3 Copyright (C) 1999-2003, 2006-2012 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/>. */
27 #include "elf-bfd.h" /* for elfcore_write_* */
29 #include "gdbthread.h"
31 #include "inf-child.h"
33 #if defined (NEW_PROC_API)
34 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
37 #include <sys/procfs.h>
38 #ifdef HAVE_SYS_FAULT_H
39 #include <sys/fault.h>
41 #ifdef HAVE_SYS_SYSCALL_H
42 #include <sys/syscall.h>
44 #include <sys/errno.h>
48 #include "gdb_string.h"
49 #include "gdb_assert.h"
55 /* This module provides the interface between GDB and the
56 /proc file system, which is used on many versions of Unix
57 as a means for debuggers to control other processes.
59 Examples of the systems that use this interface are:
67 /proc works by imitating a file system: you open a simulated file
68 that represents the process you wish to interact with, and perform
69 operations on that "file" in order to examine or change the state
72 The most important thing to know about /proc and this module is
73 that there are two very different interfaces to /proc:
75 One that uses the ioctl system call, and another that uses read
76 and write system calls.
78 This module has to support both /proc interfaces. This means that
79 there are two different ways of doing every basic operation.
81 In order to keep most of the code simple and clean, I have defined
82 an interface "layer" which hides all these system calls. An ifdef
83 (NEW_PROC_API) determines which interface we are using, and most or
84 all occurrances of this ifdef should be confined to this interface
87 /* Determine which /proc API we are using: The ioctl API defines
88 PIOCSTATUS, while the read/write (multiple fd) API never does. */
91 #include <sys/types.h>
92 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
95 #include <fcntl.h> /* for O_RDONLY */
96 #include <unistd.h> /* for "X_OK" */
97 #include "gdb_stat.h" /* for struct stat */
99 /* Note: procfs-utils.h must be included after the above system header
100 files, because it redefines various system calls using macros.
101 This may be incompatible with the prototype declarations. */
103 #include "proc-utils.h"
105 /* Prototypes for supply_gregset etc. */
108 /* =================== TARGET_OPS "MODULE" =================== */
110 /* This module defines the GDB target vector and its methods. */
112 static void procfs_attach (struct target_ops *, char *, int);
113 static void procfs_detach (struct target_ops *, char *, int);
114 static void procfs_resume (struct target_ops *,
115 ptid_t, int, enum target_signal);
116 static void procfs_stop (ptid_t);
117 static void procfs_files_info (struct target_ops *);
118 static void procfs_fetch_registers (struct target_ops *,
119 struct regcache *, int);
120 static void procfs_store_registers (struct target_ops *,
121 struct regcache *, int);
122 static void procfs_pass_signals (int, unsigned char *);
123 static void procfs_kill_inferior (struct target_ops *ops);
124 static void procfs_mourn_inferior (struct target_ops *ops);
125 static void procfs_create_inferior (struct target_ops *, char *,
126 char *, char **, int);
127 static ptid_t procfs_wait (struct target_ops *,
128 ptid_t, struct target_waitstatus *, int);
129 static int procfs_xfer_memory (CORE_ADDR, gdb_byte *, int, int,
130 struct mem_attrib *attrib,
131 struct target_ops *);
132 static LONGEST procfs_xfer_partial (struct target_ops *ops,
133 enum target_object object,
136 const gdb_byte *writebuf,
137 ULONGEST offset, LONGEST len);
139 static int procfs_thread_alive (struct target_ops *ops, ptid_t);
141 static void procfs_find_new_threads (struct target_ops *ops);
142 static char *procfs_pid_to_str (struct target_ops *, ptid_t);
144 static int proc_find_memory_regions (int (*) (CORE_ADDR,
150 static char * procfs_make_note_section (bfd *, int *);
152 static int procfs_can_use_hw_breakpoint (int, int, int);
154 static void procfs_info_proc (struct target_ops *, char *,
155 enum info_proc_what);
157 #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
158 /* When GDB is built as 64-bit application on Solaris, the auxv data
159 is presented in 64-bit format. We need to provide a custom parser
162 procfs_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
163 gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
165 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
166 gdb_byte *ptr = *readptr;
171 if (endptr - ptr < 8 * 2)
174 *typep = extract_unsigned_integer (ptr, 4, byte_order);
176 /* The size of data is always 64-bit. If the application is 32-bit,
177 it will be zero extended, as expected. */
178 *valp = extract_unsigned_integer (ptr, 8, byte_order);
189 struct target_ops *t = inf_child_target ();
191 t->to_shortname = "procfs";
192 t->to_longname = "Unix /proc child process";
194 "Unix /proc child process (started by the \"run\" command).";
195 t->to_create_inferior = procfs_create_inferior;
196 t->to_kill = procfs_kill_inferior;
197 t->to_mourn_inferior = procfs_mourn_inferior;
198 t->to_attach = procfs_attach;
199 t->to_detach = procfs_detach;
200 t->to_wait = procfs_wait;
201 t->to_resume = procfs_resume;
202 t->to_fetch_registers = procfs_fetch_registers;
203 t->to_store_registers = procfs_store_registers;
204 t->to_xfer_partial = procfs_xfer_partial;
205 t->deprecated_xfer_memory = procfs_xfer_memory;
206 t->to_pass_signals = procfs_pass_signals;
207 t->to_files_info = procfs_files_info;
208 t->to_stop = procfs_stop;
210 t->to_find_new_threads = procfs_find_new_threads;
211 t->to_thread_alive = procfs_thread_alive;
212 t->to_pid_to_str = procfs_pid_to_str;
214 t->to_has_thread_control = tc_schedlock;
215 t->to_find_memory_regions = proc_find_memory_regions;
216 t->to_make_corefile_notes = procfs_make_note_section;
217 t->to_info_proc = procfs_info_proc;
219 #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
220 t->to_auxv_parse = procfs_auxv_parse;
223 t->to_magic = OPS_MAGIC;
228 /* =================== END, TARGET_OPS "MODULE" =================== */
230 /* World Unification:
232 Put any typedefs, defines etc. here that are required for the
233 unification of code that handles different versions of /proc. */
235 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
237 enum { READ_WATCHFLAG = WA_READ,
238 WRITE_WATCHFLAG = WA_WRITE,
239 EXEC_WATCHFLAG = WA_EXEC,
240 AFTER_WATCHFLAG = WA_TRAPAFTER
243 #else /* Irix method for watchpoints */
244 enum { READ_WATCHFLAG = MA_READ,
245 WRITE_WATCHFLAG = MA_WRITE,
246 EXEC_WATCHFLAG = MA_EXEC,
247 AFTER_WATCHFLAG = 0 /* trapafter not implemented */
252 #ifdef HAVE_PR_SIGSET_T
253 typedef pr_sigset_t gdb_sigset_t;
255 typedef sigset_t gdb_sigset_t;
259 #ifdef HAVE_PR_SIGACTION64_T
260 typedef pr_sigaction64_t gdb_sigaction_t;
262 typedef struct sigaction gdb_sigaction_t;
266 #ifdef HAVE_PR_SIGINFO64_T
267 typedef pr_siginfo64_t gdb_siginfo_t;
269 typedef siginfo_t gdb_siginfo_t;
272 /* On mips-irix, praddset and prdelset are defined in such a way that
273 they return a value, which causes GCC to emit a -Wunused error
274 because the returned value is not used. Prevent this warning
275 by casting the return value to void. On sparc-solaris, this issue
276 does not exist because the definition of these macros already include
277 that cast to void. */
278 #define gdb_praddset(sp, flag) ((void) praddset (sp, flag))
279 #define gdb_prdelset(sp, flag) ((void) prdelset (sp, flag))
281 /* gdb_premptysysset */
283 #define gdb_premptysysset premptysysset
285 #define gdb_premptysysset premptyset
290 #define gdb_praddsysset praddsysset
292 #define gdb_praddsysset gdb_praddset
297 #define gdb_prdelsysset prdelsysset
299 #define gdb_prdelsysset gdb_prdelset
302 /* prissyssetmember */
303 #ifdef prissyssetmember
304 #define gdb_pr_issyssetmember prissyssetmember
306 #define gdb_pr_issyssetmember prismember
309 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
310 as intuitively descriptive as it could be, so we'll define
311 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
312 this writing, this feature is only found on AIX5 systems and
313 basically means that the set of syscalls is not fixed. I.e,
314 there's no nice table that one can #include to get all of the
315 syscall numbers. Instead, they're stored in /proc/PID/sysent
316 for each process. We are at least guaranteed that they won't
317 change over the lifetime of the process. But each process could
318 (in theory) have different syscall numbers. */
319 #ifdef HAVE_PRSYSENT_T
320 #define DYNAMIC_SYSCALLS
325 /* =================== STRUCT PROCINFO "MODULE" =================== */
327 /* FIXME: this comment will soon be out of date W.R.T. threads. */
329 /* The procinfo struct is a wrapper to hold all the state information
330 concerning a /proc process. There should be exactly one procinfo
331 for each process, and since GDB currently can debug only one
332 process at a time, that means there should be only one procinfo.
333 All of the LWP's of a process can be accessed indirectly thru the
334 single process procinfo.
336 However, against the day when GDB may debug more than one process,
337 this data structure is kept in a list (which for now will hold no
338 more than one member), and many functions will have a pointer to a
339 procinfo as an argument.
341 There will be a separate procinfo structure for use by the (not yet
342 implemented) "info proc" command, so that we can print useful
343 information about any random process without interfering with the
344 inferior's procinfo information. */
347 /* format strings for /proc paths */
348 # ifndef CTL_PROC_NAME_FMT
349 # define MAIN_PROC_NAME_FMT "/proc/%d"
350 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
351 # define AS_PROC_NAME_FMT "/proc/%d/as"
352 # define MAP_PROC_NAME_FMT "/proc/%d/map"
353 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
354 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
356 /* the name of the proc status struct depends on the implementation */
357 typedef pstatus_t gdb_prstatus_t;
358 typedef lwpstatus_t gdb_lwpstatus_t;
359 #else /* ! NEW_PROC_API */
360 /* format strings for /proc paths */
361 # ifndef CTL_PROC_NAME_FMT
362 # define MAIN_PROC_NAME_FMT "/proc/%05d"
363 # define CTL_PROC_NAME_FMT "/proc/%05d"
364 # define AS_PROC_NAME_FMT "/proc/%05d"
365 # define MAP_PROC_NAME_FMT "/proc/%05d"
366 # define STATUS_PROC_NAME_FMT "/proc/%05d"
367 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
369 /* The name of the proc status struct depends on the implementation. */
370 typedef prstatus_t gdb_prstatus_t;
371 typedef prstatus_t gdb_lwpstatus_t;
372 #endif /* NEW_PROC_API */
374 typedef struct procinfo {
375 struct procinfo *next;
376 int pid; /* Process ID */
377 int tid; /* Thread/LWP id */
381 int ignore_next_sigstop;
383 /* The following four fd fields may be identical, or may contain
384 several different fd's, depending on the version of /proc
385 (old ioctl or new read/write). */
387 int ctl_fd; /* File descriptor for /proc control file */
389 /* The next three file descriptors are actually only needed in the
390 read/write, multiple-file-descriptor implemenation
391 (NEW_PROC_API). However, to avoid a bunch of #ifdefs in the
392 code, we will use them uniformly by (in the case of the ioctl
393 single-file-descriptor implementation) filling them with copies
394 of the control fd. */
395 int status_fd; /* File descriptor for /proc status file */
396 int as_fd; /* File descriptor for /proc as file */
398 char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */
400 fltset_t saved_fltset; /* Saved traced hardware fault set */
401 gdb_sigset_t saved_sigset; /* Saved traced signal set */
402 gdb_sigset_t saved_sighold; /* Saved held signal set */
403 sysset_t *saved_exitset; /* Saved traced system call exit set */
404 sysset_t *saved_entryset; /* Saved traced system call entry set */
406 gdb_prstatus_t prstatus; /* Current process status info */
409 gdb_fpregset_t fpregset; /* Current floating point registers */
412 #ifdef DYNAMIC_SYSCALLS
413 int num_syscalls; /* Total number of syscalls */
414 char **syscall_names; /* Syscall number to name map */
417 struct procinfo *thread_list;
419 int status_valid : 1;
421 int fpregs_valid : 1;
422 int threads_valid: 1;
425 static char errmsg[128]; /* shared error msg buffer */
427 /* Function prototypes for procinfo module: */
429 static procinfo *find_procinfo_or_die (int pid, int tid);
430 static procinfo *find_procinfo (int pid, int tid);
431 static procinfo *create_procinfo (int pid, int tid);
432 static void destroy_procinfo (procinfo * p);
433 static void do_destroy_procinfo_cleanup (void *);
434 static void dead_procinfo (procinfo * p, char *msg, int killp);
435 static int open_procinfo_files (procinfo * p, int which);
436 static void close_procinfo_files (procinfo * p);
437 static int sysset_t_size (procinfo *p);
438 static sysset_t *sysset_t_alloc (procinfo * pi);
439 #ifdef DYNAMIC_SYSCALLS
440 static void load_syscalls (procinfo *pi);
441 static void free_syscalls (procinfo *pi);
442 static int find_syscall (procinfo *pi, char *name);
443 #endif /* DYNAMIC_SYSCALLS */
445 static int iterate_over_mappings
446 (procinfo *pi, find_memory_region_ftype child_func, void *data,
447 int (*func) (struct prmap *map, find_memory_region_ftype child_func,
450 /* The head of the procinfo list: */
451 static procinfo * procinfo_list;
453 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
457 find_procinfo (int pid, int tid)
461 for (pi = procinfo_list; pi; pi = pi->next)
468 /* Don't check threads_valid. If we're updating the
469 thread_list, we want to find whatever threads are already
470 here. This means that in general it is the caller's
471 responsibility to check threads_valid and update before
472 calling find_procinfo, if the caller wants to find a new
475 for (pi = pi->thread_list; pi; pi = pi->next)
483 /* Calls find_procinfo, but errors on failure. */
486 find_procinfo_or_die (int pid, int tid)
488 procinfo *pi = find_procinfo (pid, tid);
493 error (_("procfs: couldn't find pid %d "
494 "(kernel thread %d) in procinfo list."),
497 error (_("procfs: couldn't find pid %d in procinfo list."), pid);
502 /* Wrapper for `open'. The appropriate open call is attempted; if
503 unsuccessful, it will be retried as many times as needed for the
504 EAGAIN and EINTR conditions.
506 For other conditions, retry the open a limited number of times. In
507 addition, a short sleep is imposed prior to retrying the open. The
508 reason for this sleep is to give the kernel a chance to catch up
509 and create the file in question in the event that GDB "wins" the
510 race to open a file before the kernel has created it. */
513 open_with_retry (const char *pathname, int flags)
515 int retries_remaining, status;
517 retries_remaining = 2;
521 status = open (pathname, flags);
523 if (status >= 0 || retries_remaining == 0)
525 else if (errno != EINTR && errno != EAGAIN)
535 /* Open the file descriptor for the process or LWP. If NEW_PROC_API
536 is defined, we only open the control file descriptor; the others
537 are opened lazily as needed. Otherwise (if not NEW_PROC_API),
538 there is only one real file descriptor, but we keep multiple copies
539 of it so that the code that uses them does not have to be #ifdef'd.
540 Returns the file descriptor, or zero for failure. */
542 enum { FD_CTL, FD_STATUS, FD_AS };
545 open_procinfo_files (procinfo *pi, int which)
548 char tmp[MAX_PROC_NAME_SIZE];
552 /* This function is getting ALMOST long enough to break up into
553 several. Here is some rationale:
555 NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
556 There are several file descriptors that may need to be open
557 for any given process or LWP. The ones we're intereted in are:
558 - control (ctl) write-only change the state
559 - status (status) read-only query the state
560 - address space (as) read/write access memory
561 - map (map) read-only virtual addr map
562 Most of these are opened lazily as they are needed.
563 The pathnames for the 'files' for an LWP look slightly
564 different from those of a first-class process:
565 Pathnames for a process (<proc-id>):
567 /proc/<proc-id>/status
570 Pathnames for an LWP (lwp-id):
571 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
572 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
573 An LWP has no map or address space file descriptor, since
574 the memory map and address space are shared by all LWPs.
576 Everyone else (Solaris 2.5, Irix, OSF)
577 There is only one file descriptor for each process or LWP.
578 For convenience, we copy the same file descriptor into all
579 three fields of the procinfo struct (ctl_fd, status_fd, and
580 as_fd, see NEW_PROC_API above) so that code that uses them
581 doesn't need any #ifdef's.
586 Each LWP has an independent file descriptor, but these
587 are not obtained via the 'open' system call like the rest:
588 instead, they're obtained thru an ioctl call (PIOCOPENLWP)
589 to the file descriptor of the parent process.
592 These do not even have their own independent file descriptor.
593 All operations are carried out on the file descriptor of the
594 parent process. Therefore we just call open again for each
595 thread, getting a new handle for the same 'file'. */
598 /* In this case, there are several different file descriptors that
599 we might be asked to open. The control file descriptor will be
600 opened early, but the others will be opened lazily as they are
603 strcpy (tmp, pi->pathname);
604 switch (which) { /* Which file descriptor to open? */
607 strcat (tmp, "/lwpctl");
609 strcat (tmp, "/ctl");
610 fd = open_with_retry (tmp, O_WRONLY);
617 return 0; /* There is no 'as' file descriptor for an lwp. */
619 fd = open_with_retry (tmp, O_RDWR);
626 strcat (tmp, "/lwpstatus");
628 strcat (tmp, "/status");
629 fd = open_with_retry (tmp, O_RDONLY);
635 return 0; /* unknown file descriptor */
637 #else /* not NEW_PROC_API */
638 /* In this case, there is only one file descriptor for each procinfo
639 (ie. each process or LWP). In fact, only the file descriptor for
640 the process can actually be opened by an 'open' system call. The
641 ones for the LWPs have to be obtained thru an IOCTL call on the
642 process's file descriptor.
644 For convenience, we copy each procinfo's single file descriptor
645 into all of the fields occupied by the several file descriptors
646 of the NEW_PROC_API implementation. That way, the code that uses
647 them can be written without ifdefs. */
650 #ifdef PIOCTSTATUS /* OSF */
651 /* Only one FD; just open it. */
652 if ((fd = open_with_retry (pi->pathname, O_RDWR)) < 0)
654 #else /* Sol 2.5, Irix, other? */
655 if (pi->tid == 0) /* Master procinfo for the process */
657 fd = open_with_retry (pi->pathname, O_RDWR);
661 else /* LWP thread procinfo */
663 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
667 /* Find the procinfo for the entire process. */
668 if ((process = find_procinfo (pi->pid, 0)) == NULL)
671 /* Now obtain the file descriptor for the LWP. */
672 if ((fd = ioctl (process->ctl_fd, PIOCOPENLWP, &lwpid)) < 0)
674 #else /* Irix, other? */
675 return 0; /* Don't know how to open threads. */
676 #endif /* Sol 2.5 PIOCOPENLWP */
678 #endif /* OSF PIOCTSTATUS */
679 pi->ctl_fd = pi->as_fd = pi->status_fd = fd;
680 #endif /* NEW_PROC_API */
682 return 1; /* success */
685 /* Allocate a data structure and link it into the procinfo list.
686 First tries to find a pre-existing one (FIXME: why?). Returns the
687 pointer to new procinfo struct. */
690 create_procinfo (int pid, int tid)
692 procinfo *pi, *parent = NULL;
694 if ((pi = find_procinfo (pid, tid)))
695 return pi; /* Already exists, nothing to do. */
697 /* Find parent before doing malloc, to save having to cleanup. */
699 parent = find_procinfo_or_die (pid, 0); /* FIXME: should I
701 doesn't exist yet? */
703 pi = (procinfo *) xmalloc (sizeof (procinfo));
704 memset (pi, 0, sizeof (procinfo));
708 #ifdef DYNAMIC_SYSCALLS
712 pi->saved_entryset = sysset_t_alloc (pi);
713 pi->saved_exitset = sysset_t_alloc (pi);
715 /* Chain into list. */
718 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
719 pi->next = procinfo_list;
725 sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid);
727 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
729 pi->next = parent->thread_list;
730 parent->thread_list = pi;
735 /* Close all file descriptors associated with the procinfo. */
738 close_procinfo_files (procinfo *pi)
745 if (pi->status_fd > 0)
746 close (pi->status_fd);
748 pi->ctl_fd = pi->as_fd = pi->status_fd = 0;
751 /* Destructor function. Close, unlink and deallocate the object. */
754 destroy_one_procinfo (procinfo **list, procinfo *pi)
758 /* Step one: unlink the procinfo from its list. */
762 for (ptr = *list; ptr; ptr = ptr->next)
765 ptr->next = pi->next;
769 /* Step two: close any open file descriptors. */
770 close_procinfo_files (pi);
772 /* Step three: free the memory. */
773 #ifdef DYNAMIC_SYSCALLS
776 xfree (pi->saved_entryset);
777 xfree (pi->saved_exitset);
782 destroy_procinfo (procinfo *pi)
786 if (pi->tid != 0) /* Destroy a thread procinfo. */
788 tmp = find_procinfo (pi->pid, 0); /* Find the parent process. */
789 destroy_one_procinfo (&tmp->thread_list, pi);
791 else /* Destroy a process procinfo and all its threads. */
793 /* First destroy the children, if any; */
794 while (pi->thread_list != NULL)
795 destroy_one_procinfo (&pi->thread_list, pi->thread_list);
796 /* Then destroy the parent. Genocide!!! */
797 destroy_one_procinfo (&procinfo_list, pi);
802 do_destroy_procinfo_cleanup (void *pi)
804 destroy_procinfo (pi);
807 enum { NOKILL, KILL };
809 /* To be called on a non_recoverable error for a procinfo. Prints
810 error messages, optionally sends a SIGKILL to the process, then
811 destroys the data structure. */
814 dead_procinfo (procinfo *pi, char *msg, int kill_p)
820 print_sys_errmsg (pi->pathname, errno);
824 sprintf (procfile, "process %d", pi->pid);
825 print_sys_errmsg (procfile, errno);
828 kill (pi->pid, SIGKILL);
830 destroy_procinfo (pi);
834 /* Returns the (complete) size of a sysset_t struct. Normally, this
835 is just sizeof (sysset_t), but in the case of Monterey/64, the
836 actual size of sysset_t isn't known until runtime. */
839 sysset_t_size (procinfo * pi)
841 #ifndef DYNAMIC_SYSCALLS
842 return sizeof (sysset_t);
844 return sizeof (sysset_t) - sizeof (uint64_t)
845 + sizeof (uint64_t) * ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
846 / (8 * sizeof (uint64_t)));
850 /* Allocate and (partially) initialize a sysset_t struct. */
853 sysset_t_alloc (procinfo * pi)
856 int size = sysset_t_size (pi);
858 ret = xmalloc (size);
859 #ifdef DYNAMIC_SYSCALLS
860 ret->pr_size = ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
861 / (8 * sizeof (uint64_t)));
866 #ifdef DYNAMIC_SYSCALLS
868 /* Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
869 pi->num_syscalls with the number of syscalls and pi->syscall_names
870 with the names. (Certain numbers may be skipped in which case the
871 names for these numbers will be left as NULL.) */
873 #define MAX_SYSCALL_NAME_LENGTH 256
874 #define MAX_SYSCALLS 65536
877 load_syscalls (procinfo *pi)
879 char pathname[MAX_PROC_NAME_SIZE];
882 prsyscall_t *syscalls;
883 int i, size, maxcall;
884 struct cleanup *cleanups;
886 pi->num_syscalls = 0;
887 pi->syscall_names = 0;
889 /* Open the file descriptor for the sysent file. */
890 sprintf (pathname, "/proc/%d/sysent", pi->pid);
891 sysent_fd = open_with_retry (pathname, O_RDONLY);
894 error (_("load_syscalls: Can't open /proc/%d/sysent"), pi->pid);
896 cleanups = make_cleanup_close (sysent_fd);
898 size = sizeof header - sizeof (prsyscall_t);
899 if (read (sysent_fd, &header, size) != size)
901 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
904 if (header.pr_nsyscalls == 0)
906 error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"),
910 size = header.pr_nsyscalls * sizeof (prsyscall_t);
911 syscalls = xmalloc (size);
912 make_cleanup (free_current_contents, &syscalls);
914 if (read (sysent_fd, syscalls, size) != size)
915 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
917 /* Find maximum syscall number. This may not be the same as
918 pr_nsyscalls since that value refers to the number of entries
919 in the table. (Also, the docs indicate that some system
920 call numbers may be skipped.) */
922 maxcall = syscalls[0].pr_number;
924 for (i = 1; i < header.pr_nsyscalls; i++)
925 if (syscalls[i].pr_number > maxcall
926 && syscalls[i].pr_nameoff > 0
927 && syscalls[i].pr_number < MAX_SYSCALLS)
928 maxcall = syscalls[i].pr_number;
930 pi->num_syscalls = maxcall+1;
931 pi->syscall_names = xmalloc (pi->num_syscalls * sizeof (char *));
933 for (i = 0; i < pi->num_syscalls; i++)
934 pi->syscall_names[i] = NULL;
936 /* Read the syscall names in. */
937 for (i = 0; i < header.pr_nsyscalls; i++)
939 char namebuf[MAX_SYSCALL_NAME_LENGTH];
943 if (syscalls[i].pr_number >= MAX_SYSCALLS
944 || syscalls[i].pr_number < 0
945 || syscalls[i].pr_nameoff <= 0
946 || (lseek (sysent_fd, (off_t) syscalls[i].pr_nameoff, SEEK_SET)
947 != (off_t) syscalls[i].pr_nameoff))
950 nread = read (sysent_fd, namebuf, sizeof namebuf);
954 callnum = syscalls[i].pr_number;
956 if (pi->syscall_names[callnum] != NULL)
958 /* FIXME: Generate warning. */
962 namebuf[nread-1] = '\0';
963 size = strlen (namebuf) + 1;
964 pi->syscall_names[callnum] = xmalloc (size);
965 strncpy (pi->syscall_names[callnum], namebuf, size-1);
966 pi->syscall_names[callnum][size-1] = '\0';
969 do_cleanups (cleanups);
972 /* Free the space allocated for the syscall names from the procinfo
976 free_syscalls (procinfo *pi)
978 if (pi->syscall_names)
982 for (i = 0; i < pi->num_syscalls; i++)
983 if (pi->syscall_names[i] != NULL)
984 xfree (pi->syscall_names[i]);
986 xfree (pi->syscall_names);
987 pi->syscall_names = 0;
991 /* Given a name, look up (and return) the corresponding syscall number.
992 If no match is found, return -1. */
995 find_syscall (procinfo *pi, char *name)
999 for (i = 0; i < pi->num_syscalls; i++)
1001 if (pi->syscall_names[i] && strcmp (name, pi->syscall_names[i]) == 0)
1008 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1010 /* =================== /proc "MODULE" =================== */
1012 /* This "module" is the interface layer between the /proc system API
1013 and the gdb target vector functions. This layer consists of access
1014 functions that encapsulate each of the basic operations that we
1015 need to use from the /proc API.
1017 The main motivation for this layer is to hide the fact that there
1018 are two very different implementations of the /proc API. Rather
1019 than have a bunch of #ifdefs all thru the gdb target vector
1020 functions, we do our best to hide them all in here. */
1022 static long proc_flags (procinfo * pi);
1023 static int proc_why (procinfo * pi);
1024 static int proc_what (procinfo * pi);
1025 static int proc_set_current_signal (procinfo * pi, int signo);
1026 static int proc_get_current_thread (procinfo * pi);
1027 static int proc_iterate_over_threads
1029 int (*func) (procinfo *, procinfo *, void *),
1033 proc_warn (procinfo *pi, char *func, int line)
1035 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1036 print_sys_errmsg (errmsg, errno);
1040 proc_error (procinfo *pi, char *func, int line)
1042 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1043 perror_with_name (errmsg);
1046 /* Updates the status struct in the procinfo. There is a 'valid'
1047 flag, to let other functions know when this function needs to be
1048 called (so the status is only read when it is needed). The status
1049 file descriptor is also only opened when it is needed. Returns
1050 non-zero for success, zero for failure. */
1053 proc_get_status (procinfo *pi)
1055 /* Status file descriptor is opened "lazily". */
1056 if (pi->status_fd == 0 &&
1057 open_procinfo_files (pi, FD_STATUS) == 0)
1059 pi->status_valid = 0;
1064 if (lseek (pi->status_fd, 0, SEEK_SET) < 0)
1065 pi->status_valid = 0; /* fail */
1068 /* Sigh... I have to read a different data structure,
1069 depending on whether this is a main process or an LWP. */
1071 pi->status_valid = (read (pi->status_fd,
1072 (char *) &pi->prstatus.pr_lwp,
1073 sizeof (lwpstatus_t))
1074 == sizeof (lwpstatus_t));
1077 pi->status_valid = (read (pi->status_fd,
1078 (char *) &pi->prstatus,
1079 sizeof (gdb_prstatus_t))
1080 == sizeof (gdb_prstatus_t));
1081 #if 0 /*def UNIXWARE*/
1082 if (pi->status_valid &&
1083 (pi->prstatus.pr_lwp.pr_flags & PR_ISTOP) &&
1084 pi->prstatus.pr_lwp.pr_why == PR_REQUESTED)
1085 /* Unixware peculiarity -- read the damn thing again! */
1086 pi->status_valid = (read (pi->status_fd,
1087 (char *) &pi->prstatus,
1088 sizeof (gdb_prstatus_t))
1089 == sizeof (gdb_prstatus_t));
1090 #endif /* UNIXWARE */
1093 #else /* ioctl method */
1094 #ifdef PIOCTSTATUS /* osf */
1095 if (pi->tid == 0) /* main process */
1097 /* Just read the danged status. Now isn't that simple? */
1099 (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1106 tid_t pr_error_thread;
1107 struct prstatus status;
1110 thread_status.pr_count = 1;
1111 thread_status.status.pr_tid = pi->tid;
1112 win = (ioctl (pi->status_fd, PIOCTSTATUS, &thread_status) >= 0);
1115 memcpy (&pi->prstatus, &thread_status.status,
1116 sizeof (pi->prstatus));
1117 pi->status_valid = 1;
1121 /* Just read the danged status. Now isn't that simple? */
1122 pi->status_valid = (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1126 if (pi->status_valid)
1128 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1131 proc_get_current_thread (pi));
1134 /* The status struct includes general regs, so mark them valid too. */
1135 pi->gregs_valid = pi->status_valid;
1137 /* In the read/write multiple-fd model, the status struct includes
1138 the fp regs too, so mark them valid too. */
1139 pi->fpregs_valid = pi->status_valid;
1141 return pi->status_valid; /* True if success, false if failure. */
1144 /* Returns the process flags (pr_flags field). */
1147 proc_flags (procinfo *pi)
1149 if (!pi->status_valid)
1150 if (!proc_get_status (pi))
1151 return 0; /* FIXME: not a good failure value (but what is?) */
1155 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1156 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1157 The two sets of flags don't overlap. */
1158 return pi->prstatus.pr_flags | pi->prstatus.pr_lwp.pr_flags;
1160 return pi->prstatus.pr_lwp.pr_flags;
1163 return pi->prstatus.pr_flags;
1167 /* Returns the pr_why field (why the process stopped). */
1170 proc_why (procinfo *pi)
1172 if (!pi->status_valid)
1173 if (!proc_get_status (pi))
1174 return 0; /* FIXME: not a good failure value (but what is?) */
1177 return pi->prstatus.pr_lwp.pr_why;
1179 return pi->prstatus.pr_why;
1183 /* Returns the pr_what field (details of why the process stopped). */
1186 proc_what (procinfo *pi)
1188 if (!pi->status_valid)
1189 if (!proc_get_status (pi))
1190 return 0; /* FIXME: not a good failure value (but what is?) */
1193 return pi->prstatus.pr_lwp.pr_what;
1195 return pi->prstatus.pr_what;
1199 /* This function is only called when PI is stopped by a watchpoint.
1200 Assuming the OS supports it, write to *ADDR the data address which
1201 triggered it and return 1. Return 0 if it is not possible to know
1205 proc_watchpoint_address (procinfo *pi, CORE_ADDR *addr)
1207 if (!pi->status_valid)
1208 if (!proc_get_status (pi))
1212 *addr = (CORE_ADDR) gdbarch_pointer_to_address (target_gdbarch,
1213 builtin_type (target_gdbarch)->builtin_data_ptr,
1214 (gdb_byte *) &pi->prstatus.pr_lwp.pr_info.si_addr);
1216 *addr = (CORE_ADDR) gdbarch_pointer_to_address (target_gdbarch,
1217 builtin_type (target_gdbarch)->builtin_data_ptr,
1218 (gdb_byte *) &pi->prstatus.pr_info.si_addr);
1223 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1225 /* Returns the pr_nsysarg field (number of args to the current
1229 proc_nsysarg (procinfo *pi)
1231 if (!pi->status_valid)
1232 if (!proc_get_status (pi))
1236 return pi->prstatus.pr_lwp.pr_nsysarg;
1238 return pi->prstatus.pr_nsysarg;
1242 /* Returns the pr_sysarg field (pointer to the arguments of current
1246 proc_sysargs (procinfo *pi)
1248 if (!pi->status_valid)
1249 if (!proc_get_status (pi))
1253 return (long *) &pi->prstatus.pr_lwp.pr_sysarg;
1255 return (long *) &pi->prstatus.pr_sysarg;
1258 #endif /* PIOCSSPCACT */
1260 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
1261 /* Returns the pr_cursig field (current signal). */
1264 proc_cursig (struct procinfo *pi)
1266 if (!pi->status_valid)
1267 if (!proc_get_status (pi))
1268 return 0; /* FIXME: not a good failure value (but what is?) */
1271 return pi->prstatus.pr_lwp.pr_cursig;
1273 return pi->prstatus.pr_cursig;
1276 #endif /* PROCFS_DONT_PIOCSSIG_CURSIG */
1278 /* === I appologize for the messiness of this function.
1279 === This is an area where the different versions of
1280 === /proc are more inconsistent than usual.
1282 Set or reset any of the following process flags:
1283 PR_FORK -- forked child will inherit trace flags
1284 PR_RLC -- traced process runs when last /proc file closed.
1285 PR_KLC -- traced process is killed when last /proc file closed.
1286 PR_ASYNC -- LWP's get to run/stop independently.
1288 There are three methods for doing this function:
1289 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1291 2) Middle: PIOCSET/PIOCRESET
1293 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1296 Note: Irix does not define PR_ASYNC.
1297 Note: OSF does not define PR_KLC.
1298 Note: OSF is the only one that can ONLY use the oldest method.
1302 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1303 mode -- 1 for set, 0 for reset.
1305 Returns non-zero for success, zero for failure. */
1307 enum { FLAG_RESET, FLAG_SET };
1310 proc_modify_flag (procinfo *pi, long flag, long mode)
1312 long win = 0; /* default to fail */
1314 /* These operations affect the process as a whole, and applying them
1315 to an individual LWP has the same meaning as applying them to the
1316 main process. Therefore, if we're ever called with a pointer to
1317 an LWP's procinfo, let's substitute the process's procinfo and
1318 avoid opening the LWP's file descriptor unnecessarily. */
1321 pi = find_procinfo_or_die (pi->pid, 0);
1323 #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii. */
1324 /* First normalize the PCUNSET/PCRESET command opcode
1325 (which for no obvious reason has a different definition
1326 from one operating system to the next...) */
1328 #define GDBRESET PCUNSET
1331 #define GDBRESET PCRESET
1335 procfs_ctl_t arg[2];
1337 if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC). */
1339 else /* Reset the flag. */
1343 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1346 #ifdef PIOCSET /* Irix/Sol5 method */
1347 if (mode == FLAG_SET) /* Set the flag (hopefully RLC, FORK, or ASYNC). */
1349 win = (ioctl (pi->ctl_fd, PIOCSET, &flag) >= 0);
1351 else /* Reset the flag. */
1353 win = (ioctl (pi->ctl_fd, PIOCRESET, &flag) >= 0);
1357 #ifdef PIOCSRLC /* Oldest method: OSF */
1360 if (mode == FLAG_SET) /* Set run-on-last-close */
1362 win = (ioctl (pi->ctl_fd, PIOCSRLC, NULL) >= 0);
1364 else /* Clear run-on-last-close */
1366 win = (ioctl (pi->ctl_fd, PIOCRRLC, NULL) >= 0);
1370 if (mode == FLAG_SET) /* Set inherit-on-fork */
1372 win = (ioctl (pi->ctl_fd, PIOCSFORK, NULL) >= 0);
1374 else /* Clear inherit-on-fork */
1376 win = (ioctl (pi->ctl_fd, PIOCRFORK, NULL) >= 0);
1380 win = 0; /* Fail -- unknown flag (can't do PR_ASYNC). */
1387 /* The above operation renders the procinfo's cached pstatus
1389 pi->status_valid = 0;
1392 warning (_("procfs: modify_flag failed to turn %s %s"),
1393 flag == PR_FORK ? "PR_FORK" :
1394 flag == PR_RLC ? "PR_RLC" :
1396 flag == PR_ASYNC ? "PR_ASYNC" :
1399 flag == PR_KLC ? "PR_KLC" :
1402 mode == FLAG_RESET ? "off" : "on");
1407 /* Set the run_on_last_close flag. Process with all threads will
1408 become runnable when debugger closes all /proc fds. Returns
1409 non-zero for success, zero for failure. */
1412 proc_set_run_on_last_close (procinfo *pi)
1414 return proc_modify_flag (pi, PR_RLC, FLAG_SET);
1417 /* Reset the run_on_last_close flag. The process will NOT become
1418 runnable when debugger closes its file handles. Returns non-zero
1419 for success, zero for failure. */
1422 proc_unset_run_on_last_close (procinfo *pi)
1424 return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
1427 /* Reset inherit_on_fork flag. If the process forks a child while we
1428 are registered for events in the parent, then we will NOT recieve
1429 events from the child. Returns non-zero for success, zero for
1433 proc_unset_inherit_on_fork (procinfo *pi)
1435 return proc_modify_flag (pi, PR_FORK, FLAG_RESET);
1439 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
1440 (signal etc.), the remaining LWPs will continue to run. Returns
1441 non-zero for success, zero for failure. */
1444 proc_set_async (procinfo *pi)
1446 return proc_modify_flag (pi, PR_ASYNC, FLAG_SET);
1449 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
1450 (signal etc.), then all other LWPs will stop as well. Returns
1451 non-zero for success, zero for failure. */
1454 proc_unset_async (procinfo *pi)
1456 return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET);
1458 #endif /* PR_ASYNC */
1460 /* Request the process/LWP to stop. Does not wait. Returns non-zero
1461 for success, zero for failure. */
1464 proc_stop_process (procinfo *pi)
1468 /* We might conceivably apply this operation to an LWP, and the
1469 LWP's ctl file descriptor might not be open. */
1471 if (pi->ctl_fd == 0 &&
1472 open_procinfo_files (pi, FD_CTL) == 0)
1477 procfs_ctl_t cmd = PCSTOP;
1479 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1480 #else /* ioctl method */
1481 win = (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) >= 0);
1482 /* Note: the call also reads the prstatus. */
1485 pi->status_valid = 1;
1486 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1489 proc_get_current_thread (pi));
1497 /* Wait for the process or LWP to stop (block until it does). Returns
1498 non-zero for success, zero for failure. */
1501 proc_wait_for_stop (procinfo *pi)
1505 /* We should never have to apply this operation to any procinfo
1506 except the one for the main process. If that ever changes for
1507 any reason, then take out the following clause and replace it
1508 with one that makes sure the ctl_fd is open. */
1511 pi = find_procinfo_or_die (pi->pid, 0);
1515 procfs_ctl_t cmd = PCWSTOP;
1517 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1518 /* We been runnin' and we stopped -- need to update status. */
1519 pi->status_valid = 0;
1521 #else /* ioctl method */
1522 win = (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) >= 0);
1523 /* Above call also refreshes the prstatus. */
1526 pi->status_valid = 1;
1527 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1530 proc_get_current_thread (pi));
1537 /* Make the process or LWP runnable.
1539 Options (not all are implemented):
1541 - clear current fault
1542 - clear current signal
1543 - abort the current system call
1544 - stop as soon as finished with system call
1545 - (ioctl): set traced signal set
1546 - (ioctl): set held signal set
1547 - (ioctl): set traced fault set
1548 - (ioctl): set start pc (vaddr)
1550 Always clears the current fault. PI is the process or LWP to
1551 operate on. If STEP is true, set the process or LWP to trap after
1552 one instruction. If SIGNO is zero, clear the current signal if
1553 any; if non-zero, set the current signal to this one. Returns
1554 non-zero for success, zero for failure. */
1557 proc_run_process (procinfo *pi, int step, int signo)
1562 /* We will probably have to apply this operation to individual
1563 threads, so make sure the control file descriptor is open. */
1565 if (pi->ctl_fd == 0 &&
1566 open_procinfo_files (pi, FD_CTL) == 0)
1571 runflags = PRCFAULT; /* Always clear current fault. */
1576 else if (signo != -1) /* -1 means do nothing W.R.T. signals. */
1577 proc_set_current_signal (pi, signo);
1581 procfs_ctl_t cmd[2];
1585 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1587 #else /* ioctl method */
1591 memset (&prrun, 0, sizeof (prrun));
1592 prrun.pr_flags = runflags;
1593 win = (ioctl (pi->ctl_fd, PIOCRUN, &prrun) >= 0);
1600 /* Register to trace signals in the process or LWP. Returns non-zero
1601 for success, zero for failure. */
1604 proc_set_traced_signals (procinfo *pi, gdb_sigset_t *sigset)
1608 /* We should never have to apply this operation to any procinfo
1609 except the one for the main process. If that ever changes for
1610 any reason, then take out the following clause and replace it
1611 with one that makes sure the ctl_fd is open. */
1614 pi = find_procinfo_or_die (pi->pid, 0);
1620 /* Use char array to avoid alignment issues. */
1621 char sigset[sizeof (gdb_sigset_t)];
1625 memcpy (&arg.sigset, sigset, sizeof (gdb_sigset_t));
1627 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1629 #else /* ioctl method */
1630 win = (ioctl (pi->ctl_fd, PIOCSTRACE, sigset) >= 0);
1632 /* The above operation renders the procinfo's cached pstatus obsolete. */
1633 pi->status_valid = 0;
1636 warning (_("procfs: set_traced_signals failed"));
1640 /* Register to trace hardware faults in the process or LWP. Returns
1641 non-zero for success, zero for failure. */
1644 proc_set_traced_faults (procinfo *pi, fltset_t *fltset)
1648 /* We should never have to apply this operation to any procinfo
1649 except the one for the main process. If that ever changes for
1650 any reason, then take out the following clause and replace it
1651 with one that makes sure the ctl_fd is open. */
1654 pi = find_procinfo_or_die (pi->pid, 0);
1660 /* Use char array to avoid alignment issues. */
1661 char fltset[sizeof (fltset_t)];
1665 memcpy (&arg.fltset, fltset, sizeof (fltset_t));
1667 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1669 #else /* ioctl method */
1670 win = (ioctl (pi->ctl_fd, PIOCSFAULT, fltset) >= 0);
1672 /* The above operation renders the procinfo's cached pstatus obsolete. */
1673 pi->status_valid = 0;
1678 /* Register to trace entry to system calls in the process or LWP.
1679 Returns non-zero for success, zero for failure. */
1682 proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset)
1686 /* We should never have to apply this operation to any procinfo
1687 except the one for the main process. If that ever changes for
1688 any reason, then take out the following clause and replace it
1689 with one that makes sure the ctl_fd is open. */
1692 pi = find_procinfo_or_die (pi->pid, 0);
1696 struct gdb_proc_ctl_pcsentry {
1698 /* Use char array to avoid alignment issues. */
1699 char sysset[sizeof (sysset_t)];
1701 int argp_size = sizeof (struct gdb_proc_ctl_pcsentry)
1703 + sysset_t_size (pi);
1705 argp = xmalloc (argp_size);
1707 argp->cmd = PCSENTRY;
1708 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1710 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1713 #else /* ioctl method */
1714 win = (ioctl (pi->ctl_fd, PIOCSENTRY, sysset) >= 0);
1716 /* The above operation renders the procinfo's cached pstatus
1718 pi->status_valid = 0;
1723 /* Register to trace exit from system calls in the process or LWP.
1724 Returns non-zero for success, zero for failure. */
1727 proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset)
1731 /* We should never have to apply this operation to any procinfo
1732 except the one for the main process. If that ever changes for
1733 any reason, then take out the following clause and replace it
1734 with one that makes sure the ctl_fd is open. */
1737 pi = find_procinfo_or_die (pi->pid, 0);
1741 struct gdb_proc_ctl_pcsexit {
1743 /* Use char array to avoid alignment issues. */
1744 char sysset[sizeof (sysset_t)];
1746 int argp_size = sizeof (struct gdb_proc_ctl_pcsexit)
1748 + sysset_t_size (pi);
1750 argp = xmalloc (argp_size);
1752 argp->cmd = PCSEXIT;
1753 memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1755 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1758 #else /* ioctl method */
1759 win = (ioctl (pi->ctl_fd, PIOCSEXIT, sysset) >= 0);
1761 /* The above operation renders the procinfo's cached pstatus
1763 pi->status_valid = 0;
1768 /* Specify the set of blocked / held signals in the process or LWP.
1769 Returns non-zero for success, zero for failure. */
1772 proc_set_held_signals (procinfo *pi, gdb_sigset_t *sighold)
1776 /* We should never have to apply this operation to any procinfo
1777 except the one for the main process. If that ever changes for
1778 any reason, then take out the following clause and replace it
1779 with one that makes sure the ctl_fd is open. */
1782 pi = find_procinfo_or_die (pi->pid, 0);
1788 /* Use char array to avoid alignment issues. */
1789 char hold[sizeof (gdb_sigset_t)];
1793 memcpy (&arg.hold, sighold, sizeof (gdb_sigset_t));
1794 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1797 win = (ioctl (pi->ctl_fd, PIOCSHOLD, sighold) >= 0);
1799 /* The above operation renders the procinfo's cached pstatus
1801 pi->status_valid = 0;
1806 /* Returns the set of signals that are held / blocked. Will also copy
1807 the sigset if SAVE is non-zero. */
1809 static gdb_sigset_t *
1810 proc_get_held_signals (procinfo *pi, gdb_sigset_t *save)
1812 gdb_sigset_t *ret = NULL;
1814 /* We should never have to apply this operation to any procinfo
1815 except the one for the main process. If that ever changes for
1816 any reason, then take out the following clause and replace it
1817 with one that makes sure the ctl_fd is open. */
1820 pi = find_procinfo_or_die (pi->pid, 0);
1823 if (!pi->status_valid)
1824 if (!proc_get_status (pi))
1828 ret = &pi->prstatus.pr_lwp.pr_context.uc_sigmask;
1830 ret = &pi->prstatus.pr_lwp.pr_lwphold;
1831 #endif /* UNIXWARE */
1832 #else /* not NEW_PROC_API */
1834 static gdb_sigset_t sigheld;
1836 if (ioctl (pi->ctl_fd, PIOCGHOLD, &sigheld) >= 0)
1839 #endif /* NEW_PROC_API */
1841 memcpy (save, ret, sizeof (gdb_sigset_t));
1846 /* Returns the set of signals that are traced / debugged. Will also
1847 copy the sigset if SAVE is non-zero. */
1849 static gdb_sigset_t *
1850 proc_get_traced_signals (procinfo *pi, gdb_sigset_t *save)
1852 gdb_sigset_t *ret = NULL;
1854 /* We should never have to apply this operation to any procinfo
1855 except the one for the main process. If that ever changes for
1856 any reason, then take out the following clause and replace it
1857 with one that makes sure the ctl_fd is open. */
1860 pi = find_procinfo_or_die (pi->pid, 0);
1863 if (!pi->status_valid)
1864 if (!proc_get_status (pi))
1867 ret = &pi->prstatus.pr_sigtrace;
1870 static gdb_sigset_t sigtrace;
1872 if (ioctl (pi->ctl_fd, PIOCGTRACE, &sigtrace) >= 0)
1877 memcpy (save, ret, sizeof (gdb_sigset_t));
1882 /* Returns the set of hardware faults that are traced /debugged. Will
1883 also copy the faultset if SAVE is non-zero. */
1886 proc_get_traced_faults (procinfo *pi, fltset_t *save)
1888 fltset_t *ret = NULL;
1890 /* We should never have to apply this operation to any procinfo
1891 except the one for the main process. If that ever changes for
1892 any reason, then take out the following clause and replace it
1893 with one that makes sure the ctl_fd is open. */
1896 pi = find_procinfo_or_die (pi->pid, 0);
1899 if (!pi->status_valid)
1900 if (!proc_get_status (pi))
1903 ret = &pi->prstatus.pr_flttrace;
1906 static fltset_t flttrace;
1908 if (ioctl (pi->ctl_fd, PIOCGFAULT, &flttrace) >= 0)
1913 memcpy (save, ret, sizeof (fltset_t));
1918 /* Returns the set of syscalls that are traced /debugged on entry.
1919 Will also copy the syscall set if SAVE is non-zero. */
1922 proc_get_traced_sysentry (procinfo *pi, sysset_t *save)
1924 sysset_t *ret = NULL;
1926 /* We should never have to apply this operation to any procinfo
1927 except the one for the main process. If that ever changes for
1928 any reason, then take out the following clause and replace it
1929 with one that makes sure the ctl_fd is open. */
1932 pi = find_procinfo_or_die (pi->pid, 0);
1935 if (!pi->status_valid)
1936 if (!proc_get_status (pi))
1939 #ifndef DYNAMIC_SYSCALLS
1940 ret = &pi->prstatus.pr_sysentry;
1941 #else /* DYNAMIC_SYSCALLS */
1943 static sysset_t *sysentry;
1947 sysentry = sysset_t_alloc (pi);
1949 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
1951 if (pi->prstatus.pr_sysentry_offset == 0)
1953 gdb_premptysysset (sysentry);
1959 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysentry_offset,
1961 != (off_t) pi->prstatus.pr_sysentry_offset)
1963 size = sysset_t_size (pi);
1964 gdb_premptysysset (sysentry);
1965 rsize = read (pi->status_fd, sysentry, size);
1970 #endif /* DYNAMIC_SYSCALLS */
1971 #else /* !NEW_PROC_API */
1973 static sysset_t sysentry;
1975 if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysentry) >= 0)
1978 #endif /* NEW_PROC_API */
1980 memcpy (save, ret, sysset_t_size (pi));
1985 /* Returns the set of syscalls that are traced /debugged on exit.
1986 Will also copy the syscall set if SAVE is non-zero. */
1989 proc_get_traced_sysexit (procinfo *pi, sysset_t *save)
1991 sysset_t * ret = NULL;
1993 /* We should never have to apply this operation to any procinfo
1994 except the one for the main process. If that ever changes for
1995 any reason, then take out the following clause and replace it
1996 with one that makes sure the ctl_fd is open. */
1999 pi = find_procinfo_or_die (pi->pid, 0);
2002 if (!pi->status_valid)
2003 if (!proc_get_status (pi))
2006 #ifndef DYNAMIC_SYSCALLS
2007 ret = &pi->prstatus.pr_sysexit;
2008 #else /* DYNAMIC_SYSCALLS */
2010 static sysset_t *sysexit;
2014 sysexit = sysset_t_alloc (pi);
2016 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2018 if (pi->prstatus.pr_sysexit_offset == 0)
2020 gdb_premptysysset (sysexit);
2026 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysexit_offset,
2028 != (off_t) pi->prstatus.pr_sysexit_offset)
2030 size = sysset_t_size (pi);
2031 gdb_premptysysset (sysexit);
2032 rsize = read (pi->status_fd, sysexit, size);
2037 #endif /* DYNAMIC_SYSCALLS */
2040 static sysset_t sysexit;
2042 if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysexit) >= 0)
2047 memcpy (save, ret, sysset_t_size (pi));
2052 /* The current fault (if any) is cleared; the associated signal will
2053 not be sent to the process or LWP when it resumes. Returns
2054 non-zero for success, zero for failure. */
2057 proc_clear_current_fault (procinfo *pi)
2061 /* We should never have to apply this operation to any procinfo
2062 except the one for the main process. If that ever changes for
2063 any reason, then take out the following clause and replace it
2064 with one that makes sure the ctl_fd is open. */
2067 pi = find_procinfo_or_die (pi->pid, 0);
2071 procfs_ctl_t cmd = PCCFAULT;
2073 win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd));
2076 win = (ioctl (pi->ctl_fd, PIOCCFAULT, 0) >= 0);
2082 /* Set the "current signal" that will be delivered next to the
2083 process. NOTE: semantics are different from those of KILL. This
2084 signal will be delivered to the process or LWP immediately when it
2085 is resumed (even if the signal is held/blocked); it will NOT
2086 immediately cause another event of interest, and will NOT first
2087 trap back to the debugger. Returns non-zero for success, zero for
2091 proc_set_current_signal (procinfo *pi, int signo)
2096 /* Use char array to avoid alignment issues. */
2097 char sinfo[sizeof (gdb_siginfo_t)];
2099 gdb_siginfo_t mysinfo;
2101 struct target_waitstatus wait_status;
2103 /* We should never have to apply this operation to any procinfo
2104 except the one for the main process. If that ever changes for
2105 any reason, then take out the following clause and replace it
2106 with one that makes sure the ctl_fd is open. */
2109 pi = find_procinfo_or_die (pi->pid, 0);
2111 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2112 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2113 receives a PIOCSSIG with a signal identical to the current
2114 signal, it messes up the current signal. Work around the kernel
2117 signo == proc_cursig (pi))
2118 return 1; /* I assume this is a success? */
2121 /* The pointer is just a type alias. */
2122 get_last_target_status (&wait_ptid, &wait_status);
2123 if (ptid_equal (wait_ptid, inferior_ptid)
2124 && wait_status.kind == TARGET_WAITKIND_STOPPED
2125 && wait_status.value.sig == target_signal_from_host (signo)
2126 && proc_get_status (pi)
2128 && pi->prstatus.pr_lwp.pr_info.si_signo == signo
2130 && pi->prstatus.pr_info.si_signo == signo
2133 /* Use the siginfo associated with the signal being
2136 memcpy (arg.sinfo, &pi->prstatus.pr_lwp.pr_info, sizeof (gdb_siginfo_t));
2138 memcpy (arg.sinfo, &pi->prstatus.pr_info, sizeof (gdb_siginfo_t));
2142 mysinfo.si_signo = signo;
2143 mysinfo.si_code = 0;
2144 mysinfo.si_pid = getpid (); /* ?why? */
2145 mysinfo.si_uid = getuid (); /* ?why? */
2146 memcpy (arg.sinfo, &mysinfo, sizeof (gdb_siginfo_t));
2151 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2153 win = (ioctl (pi->ctl_fd, PIOCSSIG, (void *) &arg.sinfo) >= 0);
2159 /* The current signal (if any) is cleared, and is not sent to the
2160 process or LWP when it resumes. Returns non-zero for success, zero
2164 proc_clear_current_signal (procinfo *pi)
2168 /* We should never have to apply this operation to any procinfo
2169 except the one for the main process. If that ever changes for
2170 any reason, then take out the following clause and replace it
2171 with one that makes sure the ctl_fd is open. */
2174 pi = find_procinfo_or_die (pi->pid, 0);
2180 /* Use char array to avoid alignment issues. */
2181 char sinfo[sizeof (gdb_siginfo_t)];
2183 gdb_siginfo_t mysinfo;
2186 /* The pointer is just a type alias. */
2187 mysinfo.si_signo = 0;
2188 mysinfo.si_code = 0;
2189 mysinfo.si_errno = 0;
2190 mysinfo.si_pid = getpid (); /* ?why? */
2191 mysinfo.si_uid = getuid (); /* ?why? */
2192 memcpy (arg.sinfo, &mysinfo, sizeof (gdb_siginfo_t));
2194 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2197 win = (ioctl (pi->ctl_fd, PIOCSSIG, 0) >= 0);
2203 /* Return the general-purpose registers for the process or LWP
2204 corresponding to PI. Upon failure, return NULL. */
2206 static gdb_gregset_t *
2207 proc_get_gregs (procinfo *pi)
2209 if (!pi->status_valid || !pi->gregs_valid)
2210 if (!proc_get_status (pi))
2213 /* OK, sorry about the ifdef's. There's three cases instead of two,
2214 because in this case Unixware and Solaris/RW differ. */
2217 # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */
2218 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs;
2220 return &pi->prstatus.pr_lwp.pr_reg;
2223 return &pi->prstatus.pr_reg;
2227 /* Return the general-purpose registers for the process or LWP
2228 corresponding to PI. Upon failure, return NULL. */
2230 static gdb_fpregset_t *
2231 proc_get_fpregs (procinfo *pi)
2234 if (!pi->status_valid || !pi->fpregs_valid)
2235 if (!proc_get_status (pi))
2238 # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */
2239 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs;
2241 return &pi->prstatus.pr_lwp.pr_fpreg;
2244 #else /* not NEW_PROC_API */
2245 if (pi->fpregs_valid)
2246 return &pi->fpregset; /* Already got 'em. */
2249 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2258 tid_t pr_error_thread;
2259 tfpregset_t thread_1;
2262 thread_fpregs.pr_count = 1;
2263 thread_fpregs.thread_1.tid = pi->tid;
2266 && ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2268 pi->fpregs_valid = 1;
2269 return &pi->fpregset; /* Got 'em now! */
2271 else if (pi->tid != 0
2272 && ioctl (pi->ctl_fd, PIOCTGFPREG, &thread_fpregs) >= 0)
2274 memcpy (&pi->fpregset, &thread_fpregs.thread_1.pr_fpregs,
2275 sizeof (pi->fpregset));
2276 pi->fpregs_valid = 1;
2277 return &pi->fpregset; /* Got 'em now! */
2284 if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2286 pi->fpregs_valid = 1;
2287 return &pi->fpregset; /* Got 'em now! */
2296 #endif /* NEW_PROC_API */
2299 /* Write the general-purpose registers back to the process or LWP
2300 corresponding to PI. Return non-zero for success, zero for
2304 proc_set_gregs (procinfo *pi)
2306 gdb_gregset_t *gregs;
2309 gregs = proc_get_gregs (pi);
2311 return 0; /* proc_get_regs has already warned. */
2313 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2322 /* Use char array to avoid alignment issues. */
2323 char gregs[sizeof (gdb_gregset_t)];
2327 memcpy (&arg.gregs, gregs, sizeof (arg.gregs));
2328 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2330 win = (ioctl (pi->ctl_fd, PIOCSREG, gregs) >= 0);
2334 /* Policy: writing the registers invalidates our cache. */
2335 pi->gregs_valid = 0;
2339 /* Write the floating-pointer registers back to the process or LWP
2340 corresponding to PI. Return non-zero for success, zero for
2344 proc_set_fpregs (procinfo *pi)
2346 gdb_fpregset_t *fpregs;
2349 fpregs = proc_get_fpregs (pi);
2351 return 0; /* proc_get_fpregs has already warned. */
2353 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
2362 /* Use char array to avoid alignment issues. */
2363 char fpregs[sizeof (gdb_fpregset_t)];
2367 memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs));
2368 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2372 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2377 tid_t pr_error_thread;
2378 tfpregset_t thread_1;
2381 thread_fpregs.pr_count = 1;
2382 thread_fpregs.thread_1.tid = pi->tid;
2383 memcpy (&thread_fpregs.thread_1.pr_fpregs, fpregs,
2385 win = (ioctl (pi->ctl_fd, PIOCTSFPREG, &thread_fpregs) >= 0);
2388 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2390 #endif /* NEW_PROC_API */
2393 /* Policy: writing the registers invalidates our cache. */
2394 pi->fpregs_valid = 0;
2398 /* Send a signal to the proc or lwp with the semantics of "kill()".
2399 Returns non-zero for success, zero for failure. */
2402 proc_kill (procinfo *pi, int signo)
2406 /* We might conceivably apply this operation to an LWP, and the
2407 LWP's ctl file descriptor might not be open. */
2409 if (pi->ctl_fd == 0 &&
2410 open_procinfo_files (pi, FD_CTL) == 0)
2417 procfs_ctl_t cmd[2];
2421 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
2422 #else /* ioctl method */
2423 /* FIXME: do I need the Alpha OSF fixups present in
2424 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2425 win = (ioctl (pi->ctl_fd, PIOCKILL, &signo) >= 0);
2432 /* Find the pid of the process that started this one. Returns the
2433 parent process pid, or zero. */
2436 proc_parent_pid (procinfo *pi)
2438 /* We should never have to apply this operation to any procinfo
2439 except the one for the main process. If that ever changes for
2440 any reason, then take out the following clause and replace it
2441 with one that makes sure the ctl_fd is open. */
2444 pi = find_procinfo_or_die (pi->pid, 0);
2446 if (!pi->status_valid)
2447 if (!proc_get_status (pi))
2450 return pi->prstatus.pr_ppid;
2453 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2454 (a.k.a void pointer)! */
2456 #if (defined (PCWATCH) || defined (PIOCSWATCH)) \
2457 && !(defined (PIOCOPENLWP) || defined (UNIXWARE))
2459 procfs_address_to_host_pointer (CORE_ADDR addr)
2461 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
2464 gdb_assert (sizeof (ptr) == TYPE_LENGTH (ptr_type));
2465 gdbarch_address_to_pointer (target_gdbarch, ptr_type,
2466 (gdb_byte *) &ptr, addr);
2472 proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags)
2474 #if !defined (PCWATCH) && !defined (PIOCSWATCH)
2475 /* If neither or these is defined, we can't support watchpoints.
2476 This just avoids possibly failing to compile the below on such
2480 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5. */
2481 #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out. */
2486 char watch[sizeof (prwatch_t)];
2490 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2491 convert a target address into something that can be stored in a
2492 native data structure. */
2493 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2494 pwatch.pr_vaddr = (uintptr_t) procfs_address_to_host_pointer (addr);
2496 pwatch.pr_vaddr = (caddr_t) procfs_address_to_host_pointer (addr);
2498 pwatch.pr_size = len;
2499 pwatch.pr_wflags = wflags;
2500 #if defined(NEW_PROC_API) && defined (PCWATCH)
2502 memcpy (arg.watch, &pwatch, sizeof (prwatch_t));
2503 return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg));
2505 #if defined (PIOCSWATCH)
2506 return (ioctl (pi->ctl_fd, PIOCSWATCH, &pwatch) >= 0);
2508 return 0; /* Fail */
2515 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
2517 #include <sys/sysi86.h>
2519 /* The KEY is actually the value of the lower 16 bits of the GS
2520 register for the LWP that we're interested in. Returns the
2521 matching ssh struct (LDT entry). */
2524 proc_get_LDT_entry (procinfo *pi, int key)
2526 static struct ssd *ldt_entry = NULL;
2528 char pathname[MAX_PROC_NAME_SIZE];
2529 struct cleanup *old_chain = NULL;
2532 /* Allocate space for one LDT entry.
2533 This alloc must persist, because we return a pointer to it. */
2534 if (ldt_entry == NULL)
2535 ldt_entry = (struct ssd *) xmalloc (sizeof (struct ssd));
2537 /* Open the file descriptor for the LDT table. */
2538 sprintf (pathname, "/proc/%d/ldt", pi->pid);
2539 if ((fd = open_with_retry (pathname, O_RDONLY)) < 0)
2541 proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__);
2544 /* Make sure it gets closed again! */
2545 old_chain = make_cleanup_close (fd);
2547 /* Now 'read' thru the table, find a match and return it. */
2548 while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd))
2550 if (ldt_entry->sel == 0 &&
2551 ldt_entry->bo == 0 &&
2552 ldt_entry->acc1 == 0 &&
2553 ldt_entry->acc2 == 0)
2554 break; /* end of table */
2555 /* If key matches, return this entry. */
2556 if (ldt_entry->sel == key)
2559 /* Loop ended, match not found. */
2563 static int nalloc = 0;
2565 /* Get the number of LDT entries. */
2566 if (ioctl (pi->ctl_fd, PIOCNLDT, &nldt) < 0)
2568 proc_warn (pi, "proc_get_LDT_entry (PIOCNLDT)", __LINE__);
2572 /* Allocate space for the number of LDT entries. */
2573 /* This alloc has to persist, 'cause we return a pointer to it. */
2576 ldt_entry = (struct ssd *)
2577 xrealloc (ldt_entry, (nldt + 1) * sizeof (struct ssd));
2581 /* Read the whole table in one gulp. */
2582 if (ioctl (pi->ctl_fd, PIOCLDT, ldt_entry) < 0)
2584 proc_warn (pi, "proc_get_LDT_entry (PIOCLDT)", __LINE__);
2588 /* Search the table and return the (first) entry matching 'key'. */
2589 for (i = 0; i < nldt; i++)
2590 if (ldt_entry[i].sel == key)
2591 return &ldt_entry[i];
2593 /* Loop ended, match not found. */
2598 /* Returns the pointer to the LDT entry of PTID. */
2601 procfs_find_LDT_entry (ptid_t ptid)
2603 gdb_gregset_t *gregs;
2607 /* Find procinfo for the lwp. */
2608 if ((pi = find_procinfo (PIDGET (ptid), TIDGET (ptid))) == NULL)
2610 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
2611 PIDGET (ptid), TIDGET (ptid));
2614 /* get its general registers. */
2615 if ((gregs = proc_get_gregs (pi)) == NULL)
2617 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
2618 PIDGET (ptid), TIDGET (ptid));
2621 /* Now extract the GS register's lower 16 bits. */
2622 key = (*gregs)[GS] & 0xffff;
2624 /* Find the matching entry and return it. */
2625 return proc_get_LDT_entry (pi, key);
2630 /* =============== END, non-thread part of /proc "MODULE" =============== */
2632 /* =================== Thread "MODULE" =================== */
2634 /* NOTE: you'll see more ifdefs and duplication of functions here,
2635 since there is a different way to do threads on every OS. */
2637 /* Returns the number of threads for the process. */
2639 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2642 proc_get_nthreads (procinfo *pi)
2646 if (ioctl (pi->ctl_fd, PIOCNTHR, &nthreads) < 0)
2647 proc_warn (pi, "procfs: PIOCNTHR failed", __LINE__);
2653 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2654 /* Solaris and Unixware version */
2656 proc_get_nthreads (procinfo *pi)
2658 if (!pi->status_valid)
2659 if (!proc_get_status (pi))
2662 /* NEW_PROC_API: only works for the process procinfo, because the
2663 LWP procinfos do not get prstatus filled in. */
2665 if (pi->tid != 0) /* Find the parent process procinfo. */
2666 pi = find_procinfo_or_die (pi->pid, 0);
2668 return pi->prstatus.pr_nlwp;
2672 /* Default version */
2674 proc_get_nthreads (procinfo *pi)
2683 Return the ID of the thread that had an event of interest.
2684 (ie. the one that hit a breakpoint or other traced event). All
2685 other things being equal, this should be the ID of a thread that is
2686 currently executing. */
2688 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2689 /* Solaris and Unixware version */
2691 proc_get_current_thread (procinfo *pi)
2693 /* Note: this should be applied to the root procinfo for the
2694 process, not to the procinfo for an LWP. If applied to the
2695 procinfo for an LWP, it will simply return that LWP's ID. In
2696 that case, find the parent process procinfo. */
2699 pi = find_procinfo_or_die (pi->pid, 0);
2701 if (!pi->status_valid)
2702 if (!proc_get_status (pi))
2706 return pi->prstatus.pr_lwp.pr_lwpid;
2708 return pi->prstatus.pr_who;
2713 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2716 proc_get_current_thread (procinfo *pi)
2718 #if 0 /* FIXME: not ready for prime time? */
2719 return pi->prstatus.pr_tid;
2726 /* Default version */
2728 proc_get_current_thread (procinfo *pi)
2736 /* Discover the IDs of all the threads within the process, and create
2737 a procinfo for each of them (chained to the parent). This
2738 unfortunately requires a different method on every OS. Returns
2739 non-zero for success, zero for failure. */
2742 proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore)
2744 if (thread && parent) /* sanity */
2746 thread->status_valid = 0;
2747 if (!proc_get_status (thread))
2748 destroy_one_procinfo (&parent->thread_list, thread);
2750 return 0; /* keep iterating */
2753 #if defined (PIOCLSTATUS)
2754 /* Solaris 2.5 (ioctl) version */
2756 proc_update_threads (procinfo *pi)
2758 gdb_prstatus_t *prstatus;
2759 struct cleanup *old_chain = NULL;
2763 /* We should never have to apply this operation to any procinfo
2764 except the one for the main process. If that ever changes for
2765 any reason, then take out the following clause and replace it
2766 with one that makes sure the ctl_fd is open. */
2769 pi = find_procinfo_or_die (pi->pid, 0);
2771 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
2773 if ((nlwp = proc_get_nthreads (pi)) <= 1)
2774 return 1; /* Process is not multi-threaded; nothing to do. */
2776 prstatus = xmalloc (sizeof (gdb_prstatus_t) * (nlwp + 1));
2778 old_chain = make_cleanup (xfree, prstatus);
2779 if (ioctl (pi->ctl_fd, PIOCLSTATUS, prstatus) < 0)
2780 proc_error (pi, "update_threads (PIOCLSTATUS)", __LINE__);
2782 /* Skip element zero, which represents the process as a whole. */
2783 for (i = 1; i < nlwp + 1; i++)
2785 if ((thread = create_procinfo (pi->pid, prstatus[i].pr_who)) == NULL)
2786 proc_error (pi, "update_threads, create_procinfo", __LINE__);
2788 memcpy (&thread->prstatus, &prstatus[i], sizeof (*prstatus));
2789 thread->status_valid = 1;
2791 pi->threads_valid = 1;
2792 do_cleanups (old_chain);
2797 /* Unixware and Solaris 6 (and later) version. */
2799 do_closedir_cleanup (void *dir)
2805 proc_update_threads (procinfo *pi)
2807 char pathname[MAX_PROC_NAME_SIZE + 16];
2808 struct dirent *direntry;
2809 struct cleanup *old_chain = NULL;
2814 /* We should never have to apply this operation to any procinfo
2815 except the one for the main process. If that ever changes for
2816 any reason, then take out the following clause and replace it
2817 with one that makes sure the ctl_fd is open. */
2820 pi = find_procinfo_or_die (pi->pid, 0);
2822 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
2826 Note: this brute-force method is the only way I know of to
2827 accomplish this task on Unixware. This method will also work on
2828 Solaris 2.6 and 2.7. There is a much simpler and more elegant
2829 way to do this on Solaris, but the margins of this manuscript are
2830 too small to write it here... ;-) */
2832 strcpy (pathname, pi->pathname);
2833 strcat (pathname, "/lwp");
2834 if ((dirp = opendir (pathname)) == NULL)
2835 proc_error (pi, "update_threads, opendir", __LINE__);
2837 old_chain = make_cleanup (do_closedir_cleanup, dirp);
2838 while ((direntry = readdir (dirp)) != NULL)
2839 if (direntry->d_name[0] != '.') /* skip '.' and '..' */
2841 lwpid = atoi (&direntry->d_name[0]);
2842 if ((thread = create_procinfo (pi->pid, lwpid)) == NULL)
2843 proc_error (pi, "update_threads, create_procinfo", __LINE__);
2845 pi->threads_valid = 1;
2846 do_cleanups (old_chain);
2853 proc_update_threads (procinfo *pi)
2858 /* We should never have to apply this operation to any procinfo
2859 except the one for the main process. If that ever changes for
2860 any reason, then take out the following clause and replace it
2861 with one that makes sure the ctl_fd is open. */
2864 pi = find_procinfo_or_die (pi->pid, 0);
2866 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
2868 nthreads = proc_get_nthreads (pi);
2870 return 0; /* Nothing to do for 1 or fewer threads. */
2872 threads = xmalloc (nthreads * sizeof (tid_t));
2874 if (ioctl (pi->ctl_fd, PIOCTLIST, threads) < 0)
2875 proc_error (pi, "procfs: update_threads (PIOCTLIST)", __LINE__);
2877 for (i = 0; i < nthreads; i++)
2879 if (!find_procinfo (pi->pid, threads[i]))
2880 if (!create_procinfo (pi->pid, threads[i]))
2881 proc_error (pi, "update_threads, create_procinfo", __LINE__);
2883 pi->threads_valid = 1;
2887 /* Default version */
2889 proc_update_threads (procinfo *pi)
2893 #endif /* OSF PIOCTLIST */
2894 #endif /* NEW_PROC_API */
2895 #endif /* SOL 2.5 PIOCLSTATUS */
2897 /* Given a pointer to a function, call that function once for each lwp
2898 in the procinfo list, until the function returns non-zero, in which
2899 event return the value returned by the function.
2901 Note: this function does NOT call update_threads. If you want to
2902 discover new threads first, you must call that function explicitly.
2903 This function just makes a quick pass over the currently-known
2906 PI is the parent process procinfo. FUNC is the per-thread
2907 function. PTR is an opaque parameter for function. Returns the
2908 first non-zero return value from the callee, or zero. */
2911 proc_iterate_over_threads (procinfo *pi,
2912 int (*func) (procinfo *, procinfo *, void *),
2915 procinfo *thread, *next;
2918 /* We should never have to apply this operation to any procinfo
2919 except the one for the main process. If that ever changes for
2920 any reason, then take out the following clause and replace it
2921 with one that makes sure the ctl_fd is open. */
2924 pi = find_procinfo_or_die (pi->pid, 0);
2926 for (thread = pi->thread_list; thread != NULL; thread = next)
2928 next = thread->next; /* In case thread is destroyed. */
2929 if ((retval = (*func) (pi, thread, ptr)) != 0)
2936 /* =================== END, Thread "MODULE" =================== */
2938 /* =================== END, /proc "MODULE" =================== */
2940 /* =================== GDB "MODULE" =================== */
2942 /* Here are all of the gdb target vector functions and their
2945 static ptid_t do_attach (ptid_t ptid);
2946 static void do_detach (int signo);
2947 static void proc_trace_syscalls_1 (procinfo *pi, int syscallnum,
2948 int entry_or_exit, int mode, int from_tty);
2950 /* On mips-irix, we need to insert a breakpoint at __dbx_link during
2951 the startup phase. The following two variables are used to record
2952 the address of the breakpoint, and the code that was replaced by
2954 static int dbx_link_bpt_addr = 0;
2955 static void *dbx_link_bpt;
2957 /* Sets up the inferior to be debugged. Registers to trace signals,
2958 hardware faults, and syscalls. Note: does not set RLC flag: caller
2959 may want to customize that. Returns zero for success (note!
2960 unlike most functions in this module); on failure, returns the LINE
2961 NUMBER where it failed! */
2964 procfs_debug_inferior (procinfo *pi)
2966 fltset_t traced_faults;
2967 gdb_sigset_t traced_signals;
2968 sysset_t *traced_syscall_entries;
2969 sysset_t *traced_syscall_exits;
2972 #ifdef PROCFS_DONT_TRACE_FAULTS
2973 /* On some systems (OSF), we don't trace hardware faults.
2974 Apparently it's enough that we catch them as signals.
2975 Wonder why we don't just do that in general? */
2976 premptyset (&traced_faults); /* don't trace faults. */
2978 /* Register to trace hardware faults in the child. */
2979 prfillset (&traced_faults); /* trace all faults... */
2980 gdb_prdelset (&traced_faults, FLTPAGE); /* except page fault. */
2982 if (!proc_set_traced_faults (pi, &traced_faults))
2985 /* Initially, register to trace all signals in the child. */
2986 prfillset (&traced_signals);
2987 if (!proc_set_traced_signals (pi, &traced_signals))
2991 /* Register to trace the 'exit' system call (on entry). */
2992 traced_syscall_entries = sysset_t_alloc (pi);
2993 gdb_premptysysset (traced_syscall_entries);
2995 gdb_praddsysset (traced_syscall_entries, SYS_exit);
2998 gdb_praddsysset (traced_syscall_entries, SYS_lwpexit);/* And _lwp_exit... */
3001 gdb_praddsysset (traced_syscall_entries, SYS_lwp_exit);
3003 #ifdef DYNAMIC_SYSCALLS
3005 int callnum = find_syscall (pi, "_exit");
3008 gdb_praddsysset (traced_syscall_entries, callnum);
3012 status = proc_set_traced_sysentry (pi, traced_syscall_entries);
3013 xfree (traced_syscall_entries);
3017 #ifdef PRFS_STOPEXEC /* defined on OSF */
3018 /* OSF method for tracing exec syscalls. Quoting:
3019 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3020 exits from exec system calls because of the user level loader. */
3021 /* FIXME: make nice and maybe move into an access function. */
3025 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
3028 prfs_flags |= PRFS_STOPEXEC;
3030 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
3033 #else /* not PRFS_STOPEXEC */
3034 /* Everyone else's (except OSF) method for tracing exec syscalls. */
3036 Not all systems with /proc have all the exec* syscalls with the same
3037 names. On the SGI, for example, there is no SYS_exec, but there
3038 *is* a SYS_execv. So, we try to account for that. */
3040 traced_syscall_exits = sysset_t_alloc (pi);
3041 gdb_premptysysset (traced_syscall_exits);
3043 gdb_praddsysset (traced_syscall_exits, SYS_exec);
3046 gdb_praddsysset (traced_syscall_exits, SYS_execve);
3049 gdb_praddsysset (traced_syscall_exits, SYS_execv);
3052 #ifdef SYS_lwpcreate
3053 gdb_praddsysset (traced_syscall_exits, SYS_lwpcreate);
3054 gdb_praddsysset (traced_syscall_exits, SYS_lwpexit);
3057 #ifdef SYS_lwp_create /* FIXME: once only, please. */
3058 gdb_praddsysset (traced_syscall_exits, SYS_lwp_create);
3059 gdb_praddsysset (traced_syscall_exits, SYS_lwp_exit);
3062 #ifdef DYNAMIC_SYSCALLS
3064 int callnum = find_syscall (pi, "execve");
3067 gdb_praddsysset (traced_syscall_exits, callnum);
3068 callnum = find_syscall (pi, "ra_execve");
3070 gdb_praddsysset (traced_syscall_exits, callnum);
3074 status = proc_set_traced_sysexit (pi, traced_syscall_exits);
3075 xfree (traced_syscall_exits);
3079 #endif /* PRFS_STOPEXEC */
3084 procfs_attach (struct target_ops *ops, char *args, int from_tty)
3089 pid = parse_pid_to_attach (args);
3091 if (pid == getpid ())
3092 error (_("Attaching GDB to itself is not a good idea..."));
3096 exec_file = get_exec_file (0);
3099 printf_filtered (_("Attaching to program `%s', %s\n"),
3100 exec_file, target_pid_to_str (pid_to_ptid (pid)));
3102 printf_filtered (_("Attaching to %s\n"),
3103 target_pid_to_str (pid_to_ptid (pid)));
3107 inferior_ptid = do_attach (pid_to_ptid (pid));
3112 procfs_detach (struct target_ops *ops, char *args, int from_tty)
3115 int pid = PIDGET (inferior_ptid);
3124 exec_file = get_exec_file (0);
3125 if (exec_file == NULL)
3128 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file,
3129 target_pid_to_str (pid_to_ptid (pid)));
3130 gdb_flush (gdb_stdout);
3135 inferior_ptid = null_ptid;
3136 detach_inferior (pid);
3137 unpush_target (ops);
3141 do_attach (ptid_t ptid)
3144 struct inferior *inf;
3148 if ((pi = create_procinfo (PIDGET (ptid), 0)) == NULL)
3149 perror (_("procfs: out of memory in 'attach'"));
3151 if (!open_procinfo_files (pi, FD_CTL))
3153 fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__);
3154 sprintf (errmsg, "do_attach: couldn't open /proc file for process %d",
3156 dead_procinfo (pi, errmsg, NOKILL);
3159 /* Stop the process (if it isn't already stopped). */
3160 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
3162 pi->was_stopped = 1;
3163 proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
3167 pi->was_stopped = 0;
3168 /* Set the process to run again when we close it. */
3169 if (!proc_set_run_on_last_close (pi))
3170 dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
3172 /* Now stop the process. */
3173 if (!proc_stop_process (pi))
3174 dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
3175 pi->ignore_next_sigstop = 1;
3177 /* Save some of the /proc state to be restored if we detach. */
3178 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
3179 dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
3180 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
3181 dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
3182 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
3183 dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
3185 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
3186 dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
3188 if (!proc_get_held_signals (pi, &pi->saved_sighold))
3189 dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
3191 if ((fail = procfs_debug_inferior (pi)) != 0)
3192 dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
3194 inf = current_inferior ();
3195 inferior_appeared (inf, pi->pid);
3196 /* Let GDB know that the inferior was attached. */
3197 inf->attach_flag = 1;
3199 /* Create a procinfo for the current lwp. */
3200 lwpid = proc_get_current_thread (pi);
3201 create_procinfo (pi->pid, lwpid);
3203 /* Add it to gdb's thread list. */
3204 ptid = MERGEPID (pi->pid, lwpid);
3211 do_detach (int signo)
3215 /* Find procinfo for the main process. */
3216 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); /* FIXME: threads */
3218 if (!proc_set_current_signal (pi, signo))
3219 proc_warn (pi, "do_detach, set_current_signal", __LINE__);
3221 if (!proc_set_traced_signals (pi, &pi->saved_sigset))
3222 proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
3224 if (!proc_set_traced_faults (pi, &pi->saved_fltset))
3225 proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
3227 if (!proc_set_traced_sysentry (pi, pi->saved_entryset))
3228 proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
3230 if (!proc_set_traced_sysexit (pi, pi->saved_exitset))
3231 proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
3233 if (!proc_set_held_signals (pi, &pi->saved_sighold))
3234 proc_warn (pi, "do_detach, set_held_signals", __LINE__);
3236 if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)))
3237 if (signo || !(pi->was_stopped) ||
3238 query (_("Was stopped when attached, make it runnable again? ")))
3240 /* Clear any pending signal. */
3241 if (!proc_clear_current_fault (pi))
3242 proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
3244 if (signo == 0 && !proc_clear_current_signal (pi))
3245 proc_warn (pi, "do_detach, clear_current_signal", __LINE__);
3247 if (!proc_set_run_on_last_close (pi))
3248 proc_warn (pi, "do_detach, set_rlc", __LINE__);
3251 destroy_procinfo (pi);
3254 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
3257 ??? Is the following note still relevant? We can't get individual
3258 registers with the PT_GETREGS ptrace(2) request either, yet we
3259 don't bother with caching at all in that case.
3261 NOTE: Since the /proc interface cannot give us individual
3262 registers, we pay no attention to REGNUM, and just fetch them all.
3263 This results in the possibility that we will do unnecessarily many
3264 fetches, since we may be called repeatedly for individual
3265 registers. So we cache the results, and mark the cache invalid
3266 when the process is resumed. */
3269 procfs_fetch_registers (struct target_ops *ops,
3270 struct regcache *regcache, int regnum)
3272 gdb_gregset_t *gregs;
3274 int pid = PIDGET (inferior_ptid);
3275 int tid = TIDGET (inferior_ptid);
3276 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3278 pi = find_procinfo_or_die (pid, tid);
3281 error (_("procfs: fetch_registers failed to find procinfo for %s"),
3282 target_pid_to_str (inferior_ptid));
3284 gregs = proc_get_gregs (pi);
3286 proc_error (pi, "fetch_registers, get_gregs", __LINE__);
3288 supply_gregset (regcache, (const gdb_gregset_t *) gregs);
3290 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
3292 gdb_fpregset_t *fpregs;
3294 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
3295 || regnum == gdbarch_pc_regnum (gdbarch)
3296 || regnum == gdbarch_sp_regnum (gdbarch))
3297 return; /* Not a floating point register. */
3299 fpregs = proc_get_fpregs (pi);
3301 proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
3303 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregs);
3307 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
3308 this for all registers.
3310 NOTE: Since the /proc interface will not read individual registers,
3311 we will cache these requests until the process is resumed, and only
3312 then write them back to the inferior process.
3314 FIXME: is that a really bad idea? Have to think about cases where
3315 writing one register might affect the value of others, etc. */
3318 procfs_store_registers (struct target_ops *ops,
3319 struct regcache *regcache, int regnum)
3321 gdb_gregset_t *gregs;
3323 int pid = PIDGET (inferior_ptid);
3324 int tid = TIDGET (inferior_ptid);
3325 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3327 pi = find_procinfo_or_die (pid, tid);
3330 error (_("procfs: store_registers: failed to find procinfo for %s"),
3331 target_pid_to_str (inferior_ptid));
3333 gregs = proc_get_gregs (pi);
3335 proc_error (pi, "store_registers, get_gregs", __LINE__);
3337 fill_gregset (regcache, gregs, regnum);
3338 if (!proc_set_gregs (pi))
3339 proc_error (pi, "store_registers, set_gregs", __LINE__);
3341 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
3343 gdb_fpregset_t *fpregs;
3345 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
3346 || regnum == gdbarch_pc_regnum (gdbarch)
3347 || regnum == gdbarch_sp_regnum (gdbarch))
3348 return; /* Not a floating point register. */
3350 fpregs = proc_get_fpregs (pi);
3352 proc_error (pi, "store_registers, get_fpregs", __LINE__);
3354 fill_fpregset (regcache, fpregs, regnum);
3355 if (!proc_set_fpregs (pi))
3356 proc_error (pi, "store_registers, set_fpregs", __LINE__);
3361 syscall_is_lwp_exit (procinfo *pi, int scall)
3364 if (scall == SYS_lwp_exit)
3368 if (scall == SYS_lwpexit)
3375 syscall_is_exit (procinfo *pi, int scall)
3378 if (scall == SYS_exit)
3381 #ifdef DYNAMIC_SYSCALLS
3382 if (find_syscall (pi, "_exit") == scall)
3389 syscall_is_exec (procinfo *pi, int scall)
3392 if (scall == SYS_exec)
3396 if (scall == SYS_execv)
3400 if (scall == SYS_execve)
3403 #ifdef DYNAMIC_SYSCALLS
3404 if (find_syscall (pi, "_execve"))
3406 if (find_syscall (pi, "ra_execve"))
3413 syscall_is_lwp_create (procinfo *pi, int scall)
3415 #ifdef SYS_lwp_create
3416 if (scall == SYS_lwp_create)
3419 #ifdef SYS_lwpcreate
3420 if (scall == SYS_lwpcreate)
3426 /* Remove the breakpoint that we inserted in __dbx_link().
3427 Does nothing if the breakpoint hasn't been inserted or has already
3431 remove_dbx_link_breakpoint (void)
3433 if (dbx_link_bpt_addr == 0)
3436 if (deprecated_remove_raw_breakpoint (target_gdbarch, dbx_link_bpt) != 0)
3437 warning (_("Unable to remove __dbx_link breakpoint."));
3439 dbx_link_bpt_addr = 0;
3440 dbx_link_bpt = NULL;
3444 /* Return the address of the __dbx_link() function in the file
3445 refernced by ABFD by scanning its symbol table. Return 0 if
3446 the symbol was not found. */
3449 dbx_link_addr (bfd *abfd)
3451 long storage_needed;
3452 asymbol **symbol_table;
3453 long number_of_symbols;
3456 storage_needed = bfd_get_symtab_upper_bound (abfd);
3457 if (storage_needed <= 0)
3460 symbol_table = (asymbol **) xmalloc (storage_needed);
3461 make_cleanup (xfree, symbol_table);
3463 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
3465 for (i = 0; i < number_of_symbols; i++)
3467 asymbol *sym = symbol_table[i];
3469 if ((sym->flags & BSF_GLOBAL)
3470 && sym->name != NULL && strcmp (sym->name, "__dbx_link") == 0)
3471 return (sym->value + sym->section->vma);
3474 /* Symbol not found, return NULL. */
3478 /* Search the symbol table of the file referenced by FD for a symbol
3479 named __dbx_link(). If found, then insert a breakpoint at this location,
3480 and return nonzero. Return zero otherwise. */
3483 insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored)
3486 long storage_needed;
3489 abfd = bfd_fdopenr ("unamed", 0, fd);
3492 warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
3496 if (!bfd_check_format (abfd, bfd_object))
3498 /* Not the correct format, so we can not possibly find the dbx_link
3504 sym_addr = dbx_link_addr (abfd);
3507 /* Insert the breakpoint. */
3508 dbx_link_bpt_addr = sym_addr;
3509 dbx_link_bpt = deprecated_insert_raw_breakpoint (target_gdbarch, NULL,
3511 if (dbx_link_bpt == NULL)
3513 warning (_("Failed to insert dbx_link breakpoint."));
3525 /* Calls the supplied callback function once for each mapped address
3526 space in the process. The callback function receives an open file
3527 descriptor for the file corresponding to that mapped address space
3528 (if there is one), and the base address of the mapped space. Quit
3529 when the callback function returns a nonzero value, or at teh end
3530 of the mappings. Returns the first non-zero return value of the
3531 callback function, or zero. */
3534 solib_mappings_callback (struct prmap *map, int (*func) (int, CORE_ADDR),
3537 procinfo *pi = data;
3541 char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)];
3543 if (map->pr_vaddr == 0 && map->pr_size == 0)
3544 return -1; /* sanity */
3546 if (map->pr_mapname[0] == 0)
3548 fd = -1; /* no map file */
3552 sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname);
3553 /* Note: caller's responsibility to close this fd! */
3554 fd = open_with_retry (name, O_RDONLY);
3555 /* Note: we don't test the above call for failure;
3556 we just pass the FD on as given. Sometimes there is
3557 no file, so the open may return failure, but that's
3561 fd = ioctl (pi->ctl_fd, PIOCOPENM, &map->pr_vaddr);
3562 /* Note: we don't test the above call for failure;
3563 we just pass the FD on as given. Sometimes there is
3564 no file, so the ioctl may return failure, but that's
3567 return (*func) (fd, (CORE_ADDR) map->pr_vaddr);
3570 /* If the given memory region MAP contains a symbol named __dbx_link,
3571 insert a breakpoint at this location and return nonzero. Return
3575 insert_dbx_link_bpt_in_region (struct prmap *map,
3576 find_memory_region_ftype child_func,
3579 procinfo *pi = (procinfo *) data;
3581 /* We know the symbol we're looking for is in a text region, so
3582 only look for it if the region is a text one. */
3583 if (map->pr_mflags & MA_EXEC)
3584 return solib_mappings_callback (map, insert_dbx_link_bpt_in_file, pi);
3589 /* Search all memory regions for a symbol named __dbx_link. If found,
3590 insert a breakpoint at its location, and return nonzero. Return zero
3594 insert_dbx_link_breakpoint (procinfo *pi)
3596 return iterate_over_mappings (pi, NULL, pi, insert_dbx_link_bpt_in_region);
3600 /* Retrieve the next stop event from the child process. If child has
3601 not stopped yet, wait for it to stop. Translate /proc eventcodes
3602 (or possibly wait eventcodes) into gdb internal event codes.
3603 Returns the id of process (and possibly thread) that incurred the
3604 event. Event codes are returned through a pointer parameter. */
3607 procfs_wait (struct target_ops *ops,
3608 ptid_t ptid, struct target_waitstatus *status, int options)
3610 /* First cut: loosely based on original version 2.1. */
3614 ptid_t retval, temp_ptid;
3615 int why, what, flags;
3622 retval = pid_to_ptid (-1);
3624 /* Find procinfo for main process. */
3625 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
3628 /* We must assume that the status is stale now... */
3629 pi->status_valid = 0;
3630 pi->gregs_valid = 0;
3631 pi->fpregs_valid = 0;
3633 #if 0 /* just try this out... */
3634 flags = proc_flags (pi);
3635 why = proc_why (pi);
3636 if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
3637 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
3639 /* If child is not stopped, wait for it to stop. */
3640 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) &&
3641 !proc_wait_for_stop (pi))
3643 /* wait_for_stop failed: has the child terminated? */
3644 if (errno == ENOENT)
3648 /* /proc file not found; presumably child has terminated. */
3649 wait_retval = wait (&wstat); /* "wait" for the child's exit. */
3651 if (wait_retval != PIDGET (inferior_ptid)) /* wrong child? */
3652 error (_("procfs: couldn't stop "
3653 "process %d: wait returned %d."),
3654 PIDGET (inferior_ptid), wait_retval);
3655 /* FIXME: might I not just use waitpid?
3656 Or try find_procinfo to see if I know about this child? */
3657 retval = pid_to_ptid (wait_retval);
3659 else if (errno == EINTR)
3663 /* Unknown error from wait_for_stop. */
3664 proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
3669 /* This long block is reached if either:
3670 a) the child was already stopped, or
3671 b) we successfully waited for the child with wait_for_stop.
3672 This block will analyze the /proc status, and translate it
3673 into a waitstatus for GDB.
3675 If we actually had to call wait because the /proc file
3676 is gone (child terminated), then we skip this block,
3677 because we already have a waitstatus. */
3679 flags = proc_flags (pi);
3680 why = proc_why (pi);
3681 what = proc_what (pi);
3683 if (flags & (PR_STOPPED | PR_ISTOP))
3686 /* If it's running async (for single_thread control),
3687 set it back to normal again. */
3688 if (flags & PR_ASYNC)
3689 if (!proc_unset_async (pi))
3690 proc_error (pi, "target_wait, unset_async", __LINE__);
3694 proc_prettyprint_why (why, what, 1);
3696 /* The 'pid' we will return to GDB is composed of
3697 the process ID plus the lwp ID. */
3698 retval = MERGEPID (pi->pid, proc_get_current_thread (pi));
3702 wstat = (what << 8) | 0177;
3705 if (syscall_is_lwp_exit (pi, what))
3707 if (print_thread_events)
3708 printf_unfiltered (_("[%s exited]\n"),
3709 target_pid_to_str (retval));
3710 delete_thread (retval);
3711 status->kind = TARGET_WAITKIND_SPURIOUS;
3714 else if (syscall_is_exit (pi, what))
3716 struct inferior *inf;
3718 /* Handle SYS_exit call only. */
3719 /* Stopped at entry to SYS_exit.
3720 Make it runnable, resume it, then use
3721 the wait system call to get its exit code.
3722 Proc_run_process always clears the current
3724 Then return its exit status. */
3725 pi->status_valid = 0;
3727 /* FIXME: what we should do is return
3728 TARGET_WAITKIND_SPURIOUS. */
3729 if (!proc_run_process (pi, 0, 0))
3730 proc_error (pi, "target_wait, run_process", __LINE__);
3732 inf = find_inferior_pid (pi->pid);
3733 if (inf->attach_flag)
3735 /* Don't call wait: simulate waiting for exit,
3736 return a "success" exit code. Bogus: what if
3737 it returns something else? */
3739 retval = inferior_ptid; /* ? ? ? */
3743 int temp = wait (&wstat);
3745 /* FIXME: shouldn't I make sure I get the right
3746 event from the right process? If (for
3747 instance) I have killed an earlier inferior
3748 process but failed to clean up after it
3749 somehow, I could get its termination event
3752 /* If wait returns -1, that's what we return
3755 retval = pid_to_ptid (temp);
3760 printf_filtered (_("procfs: trapped on entry to "));
3761 proc_prettyprint_syscall (proc_what (pi), 0);
3762 printf_filtered ("\n");
3765 long i, nsysargs, *sysargs;
3767 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
3768 (sysargs = proc_sysargs (pi)) != NULL)
3770 printf_filtered (_("%ld syscall arguments:\n"),
3772 for (i = 0; i < nsysargs; i++)
3773 printf_filtered ("#%ld: 0x%08lx\n",
3781 /* How to exit gracefully, returning "unknown
3783 status->kind = TARGET_WAITKIND_SPURIOUS;
3784 return inferior_ptid;
3788 /* How to keep going without returning to wfi: */
3789 target_resume (ptid, 0, TARGET_SIGNAL_0);
3795 if (syscall_is_exec (pi, what))
3797 /* Hopefully this is our own "fork-child" execing
3798 the real child. Hoax this event into a trap, and
3799 GDB will see the child about to execute its start
3801 wstat = (SIGTRAP << 8) | 0177;
3804 else if (what == SYS_syssgi)
3806 /* see if we can break on dbx_link(). If yes, then
3807 we no longer need the SYS_syssgi notifications. */
3808 if (insert_dbx_link_breakpoint (pi))
3809 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT,
3812 /* This is an internal event and should be transparent
3813 to wfi, so resume the execution and wait again. See
3814 comment in procfs_init_inferior() for more details. */
3815 target_resume (ptid, 0, TARGET_SIGNAL_0);
3819 else if (syscall_is_lwp_create (pi, what))
3821 /* This syscall is somewhat like fork/exec. We
3822 will get the event twice: once for the parent
3823 LWP, and once for the child. We should already
3824 know about the parent LWP, but the child will
3825 be new to us. So, whenever we get this event,
3826 if it represents a new thread, simply add the
3827 thread to the list. */
3829 /* If not in procinfo list, add it. */
3830 temp_tid = proc_get_current_thread (pi);
3831 if (!find_procinfo (pi->pid, temp_tid))
3832 create_procinfo (pi->pid, temp_tid);
3834 temp_ptid = MERGEPID (pi->pid, temp_tid);
3835 /* If not in GDB's thread list, add it. */
3836 if (!in_thread_list (temp_ptid))
3837 add_thread (temp_ptid);
3839 /* Return to WFI, but tell it to immediately resume. */
3840 status->kind = TARGET_WAITKIND_SPURIOUS;
3841 return inferior_ptid;
3843 else if (syscall_is_lwp_exit (pi, what))
3845 if (print_thread_events)
3846 printf_unfiltered (_("[%s exited]\n"),
3847 target_pid_to_str (retval));
3848 delete_thread (retval);
3849 status->kind = TARGET_WAITKIND_SPURIOUS;
3854 /* FIXME: Do we need to handle SYS_sproc,
3855 SYS_fork, or SYS_vfork here? The old procfs
3856 seemed to use this event to handle threads on
3857 older (non-LWP) systems, where I'm assuming
3858 that threads were actually separate processes.
3859 Irix, maybe? Anyway, low priority for now. */
3863 printf_filtered (_("procfs: trapped on exit from "));
3864 proc_prettyprint_syscall (proc_what (pi), 0);
3865 printf_filtered ("\n");
3868 long i, nsysargs, *sysargs;
3870 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
3871 (sysargs = proc_sysargs (pi)) != NULL)
3873 printf_filtered (_("%ld syscall arguments:\n"),
3875 for (i = 0; i < nsysargs; i++)
3876 printf_filtered ("#%ld: 0x%08lx\n",
3881 status->kind = TARGET_WAITKIND_SPURIOUS;
3882 return inferior_ptid;
3887 wstat = (SIGSTOP << 8) | 0177;
3892 printf_filtered (_("Retry #%d:\n"), retry);
3893 pi->status_valid = 0;
3898 /* If not in procinfo list, add it. */
3899 temp_tid = proc_get_current_thread (pi);
3900 if (!find_procinfo (pi->pid, temp_tid))
3901 create_procinfo (pi->pid, temp_tid);
3903 /* If not in GDB's thread list, add it. */
3904 temp_ptid = MERGEPID (pi->pid, temp_tid);
3905 if (!in_thread_list (temp_ptid))
3906 add_thread (temp_ptid);
3908 status->kind = TARGET_WAITKIND_STOPPED;
3909 status->value.sig = 0;
3914 wstat = (what << 8) | 0177;
3920 wstat = (SIGTRAP << 8) | 0177;
3925 wstat = (SIGTRAP << 8) | 0177;
3928 /* FIXME: use si_signo where possible. */
3930 #if (FLTILL != FLTPRIV) /* Avoid "duplicate case" error. */
3933 wstat = (SIGILL << 8) | 0177;
3936 #if (FLTTRACE != FLTBPT) /* Avoid "duplicate case" error. */
3939 /* If we hit our __dbx_link() internal breakpoint,
3940 then remove it. See comments in procfs_init_inferior()
3941 for more details. */
3942 if (dbx_link_bpt_addr != 0
3943 && dbx_link_bpt_addr
3944 == regcache_read_pc (get_current_regcache ()))
3945 remove_dbx_link_breakpoint ();
3947 wstat = (SIGTRAP << 8) | 0177;
3951 #if (FLTBOUNDS != FLTSTACK) /* Avoid "duplicate case" error. */
3954 wstat = (SIGSEGV << 8) | 0177;
3958 #if (FLTFPE != FLTIOVF) /* Avoid "duplicate case" error. */
3961 wstat = (SIGFPE << 8) | 0177;
3963 case FLTPAGE: /* Recoverable page fault */
3964 default: /* FIXME: use si_signo if possible for
3966 retval = pid_to_ptid (-1);
3967 printf_filtered ("procfs:%d -- ", __LINE__);
3968 printf_filtered (_("child stopped for unknown reason:\n"));
3969 proc_prettyprint_why (why, what, 1);
3970 error (_("... giving up..."));
3973 break; /* case PR_FAULTED: */
3974 default: /* switch (why) unmatched */
3975 printf_filtered ("procfs:%d -- ", __LINE__);
3976 printf_filtered (_("child stopped for unknown reason:\n"));
3977 proc_prettyprint_why (why, what, 1);
3978 error (_("... giving up..."));
3981 /* Got this far without error: If retval isn't in the
3982 threads database, add it. */
3983 if (PIDGET (retval) > 0 &&
3984 !ptid_equal (retval, inferior_ptid) &&
3985 !in_thread_list (retval))
3987 /* We have a new thread. We need to add it both to
3988 GDB's list and to our own. If we don't create a
3989 procinfo, resume may be unhappy later. */
3990 add_thread (retval);
3991 if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL)
3992 create_procinfo (PIDGET (retval), TIDGET (retval));
3995 else /* Flags do not indicate STOPPED. */
3997 /* surely this can't happen... */
3998 printf_filtered ("procfs:%d -- process not stopped.\n",
4000 proc_prettyprint_flags (flags, 1);
4001 error (_("procfs: ...giving up..."));
4006 store_waitstatus (status, wstat);
4012 /* Perform a partial transfer to/from the specified object. For
4013 memory transfers, fall back to the old memory xfer functions. */
4016 procfs_xfer_partial (struct target_ops *ops, enum target_object object,
4017 const char *annex, gdb_byte *readbuf,
4018 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
4022 case TARGET_OBJECT_MEMORY:
4024 return (*ops->deprecated_xfer_memory) (offset, readbuf,
4025 len, 0/*read*/, NULL, ops);
4027 return (*ops->deprecated_xfer_memory) (offset, (gdb_byte *) writebuf,
4028 len, 1/*write*/, NULL, ops);
4032 case TARGET_OBJECT_AUXV:
4033 return memory_xfer_auxv (ops, object, annex, readbuf, writebuf,
4038 if (ops->beneath != NULL)
4039 return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
4040 readbuf, writebuf, offset, len);
4046 /* Transfer LEN bytes between GDB address MYADDR and target address
4047 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4048 otherwise transfer them from the target. TARGET is unused.
4050 The return value is 0 if an error occurred or no bytes were
4051 transferred. Otherwise, it will be a positive value which
4052 indicates the number of bytes transferred between gdb and the
4053 target. (Note that the interface also makes provisions for
4054 negative values, but this capability isn't implemented here.) */
4057 procfs_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int dowrite,
4058 struct mem_attrib *attrib, struct target_ops *target)
4063 /* Find procinfo for main process. */
4064 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4065 if (pi->as_fd == 0 &&
4066 open_procinfo_files (pi, FD_AS) == 0)
4068 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
4072 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
4077 PROCFS_NOTE ("write memory:\n");
4079 PROCFS_NOTE ("write memory:\n");
4081 nbytes = write (pi->as_fd, myaddr, len);
4085 PROCFS_NOTE ("read memory:\n");
4086 nbytes = read (pi->as_fd, myaddr, len);
4096 /* Called by target_resume before making child runnable. Mark cached
4097 registers and status's invalid. If there are "dirty" caches that
4098 need to be written back to the child process, do that.
4100 File descriptors are also cached. As they are a limited resource,
4101 we cannot hold onto them indefinitely. However, as they are
4102 expensive to open, we don't want to throw them away
4103 indescriminately either. As a compromise, we will keep the file
4104 descriptors for the parent process, but discard any file
4105 descriptors we may have accumulated for the threads.
4107 As this function is called by iterate_over_threads, it always
4108 returns zero (so that iterate_over_threads will keep
4112 invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
4114 /* About to run the child; invalidate caches and do any other
4118 if (pi->gregs_dirty)
4119 if (parent == NULL ||
4120 proc_get_current_thread (parent) != pi->tid)
4121 if (!proc_set_gregs (pi)) /* flush gregs cache */
4122 proc_warn (pi, "target_resume, set_gregs",
4124 if (gdbarch_fp0_regnum (target_gdbarch) >= 0)
4125 if (pi->fpregs_dirty)
4126 if (parent == NULL ||
4127 proc_get_current_thread (parent) != pi->tid)
4128 if (!proc_set_fpregs (pi)) /* flush fpregs cache */
4129 proc_warn (pi, "target_resume, set_fpregs",
4135 /* The presence of a parent indicates that this is an LWP.
4136 Close any file descriptors that it might have open.
4137 We don't do this to the master (parent) procinfo. */
4139 close_procinfo_files (pi);
4141 pi->gregs_valid = 0;
4142 pi->fpregs_valid = 0;
4144 pi->gregs_dirty = 0;
4145 pi->fpregs_dirty = 0;
4147 pi->status_valid = 0;
4148 pi->threads_valid = 0;
4154 /* A callback function for iterate_over_threads. Find the
4155 asynchronous signal thread, and make it runnable. See if that
4156 helps matters any. */
4159 make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr)
4162 if (proc_flags (pi) & PR_ASLWP)
4164 if (!proc_run_process (pi, 0, -1))
4165 proc_error (pi, "make_signal_thread_runnable", __LINE__);
4173 /* Make the child process runnable. Normally we will then call
4174 procfs_wait and wait for it to stop again (unless gdb is async).
4176 If STEP is true, then arrange for the child to stop again after
4177 executing a single instruction. If SIGNO is zero, then cancel any
4178 pending signal; if non-zero, then arrange for the indicated signal
4179 to be delivered to the child when it runs. If PID is -1, then
4180 allow any child thread to run; if non-zero, then allow only the
4181 indicated thread to run. (not implemented yet). */
4184 procfs_resume (struct target_ops *ops,
4185 ptid_t ptid, int step, enum target_signal signo)
4187 procinfo *pi, *thread;
4191 prrun.prflags |= PRSVADDR;
4192 prrun.pr_vaddr = $PC; set resume address
4193 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4194 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4195 prrun.prflags |= PRCFAULT; clear current fault.
4197 PRSTRACE and PRSFAULT can be done by other means
4198 (proc_trace_signals, proc_trace_faults)
4199 PRSVADDR is unnecessary.
4200 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4201 This basically leaves PRSTEP and PRCSIG.
4202 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4203 So basically PR_STEP is the sole argument that must be passed
4204 to proc_run_process (for use in the prrun struct by ioctl). */
4206 /* Find procinfo for main process. */
4207 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4209 /* First cut: ignore pid argument. */
4212 /* Convert signal to host numbering. */
4214 (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop))
4217 native_signo = target_signal_to_host (signo);
4219 pi->ignore_next_sigstop = 0;
4221 /* Running the process voids all cached registers and status. */
4222 /* Void the threads' caches first. */
4223 proc_iterate_over_threads (pi, invalidate_cache, NULL);
4224 /* Void the process procinfo's caches. */
4225 invalidate_cache (NULL, pi, NULL);
4227 if (PIDGET (ptid) != -1)
4229 /* Resume a specific thread, presumably suppressing the
4231 thread = find_procinfo (PIDGET (ptid), TIDGET (ptid));
4234 if (thread->tid != 0)
4236 /* We're to resume a specific thread, and not the
4237 others. Set the child process's PR_ASYNC flag. */
4239 if (!proc_set_async (pi))
4240 proc_error (pi, "target_resume, set_async", __LINE__);
4243 proc_iterate_over_threads (pi,
4244 make_signal_thread_runnable,
4247 pi = thread; /* Substitute the thread's procinfo
4253 if (!proc_run_process (pi, step, native_signo))
4256 warning (_("resume: target already running. "
4257 "Pretend to resume, and hope for the best!"));
4259 proc_error (pi, "target_resume", __LINE__);
4263 /* Set up to trace signals in the child process. */
4266 procfs_pass_signals (int numsigs, unsigned char *pass_signals)
4268 gdb_sigset_t signals;
4269 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4272 prfillset (&signals);
4274 for (signo = 0; signo < NSIG; signo++)
4276 int target_signo = target_signal_from_host (signo);
4277 if (target_signo < numsigs && pass_signals[target_signo])
4278 gdb_prdelset (&signals, signo);
4281 if (!proc_set_traced_signals (pi, &signals))
4282 proc_error (pi, "pass_signals", __LINE__);
4285 /* Print status information about the child process. */
4288 procfs_files_info (struct target_ops *ignore)
4290 struct inferior *inf = current_inferior ();
4292 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
4293 inf->attach_flag? "attached": "child",
4294 target_pid_to_str (inferior_ptid));
4297 /* Stop the child process asynchronously, as when the gdb user types
4298 control-c or presses a "stop" button. Works by sending
4299 kill(SIGINT) to the child's process group. */
4302 procfs_stop (ptid_t ptid)
4304 kill (-inferior_process_group (), SIGINT);
4307 /* Make it die. Wait for it to die. Clean up after it. Note: this
4308 should only be applied to the real process, not to an LWP, because
4309 of the check for parent-process. If we need this to work for an
4310 LWP, it needs some more logic. */
4313 unconditionally_kill_inferior (procinfo *pi)
4317 parent_pid = proc_parent_pid (pi);
4318 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4319 /* FIXME: use access functions. */
4320 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4321 before the PIOCKILL, otherwise it might generate a corrupted core
4322 file for the inferior. */
4323 if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0)
4325 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4328 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4329 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4330 to kill the inferior, otherwise it might remain stopped with a
4332 We do not check the result of the PIOCSSIG, the inferior might have
4335 gdb_siginfo_t newsiginfo;
4337 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
4338 newsiginfo.si_signo = SIGKILL;
4339 newsiginfo.si_code = 0;
4340 newsiginfo.si_errno = 0;
4341 newsiginfo.si_pid = getpid ();
4342 newsiginfo.si_uid = getuid ();
4343 /* FIXME: use proc_set_current_signal. */
4344 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
4346 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4347 if (!proc_kill (pi, SIGKILL))
4348 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
4349 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4350 destroy_procinfo (pi);
4352 /* If pi is GDB's child, wait for it to die. */
4353 if (parent_pid == getpid ())
4354 /* FIXME: should we use waitpid to make sure we get the right event?
4355 Should we check the returned event? */
4360 ret = waitpid (pi->pid, &status, 0);
4367 /* We're done debugging it, and we want it to go away. Then we want
4368 GDB to forget all about it. */
4371 procfs_kill_inferior (struct target_ops *ops)
4373 if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */
4375 /* Find procinfo for main process. */
4376 procinfo *pi = find_procinfo (PIDGET (inferior_ptid), 0);
4379 unconditionally_kill_inferior (pi);
4380 target_mourn_inferior ();
4384 /* Forget we ever debugged this thing! */
4387 procfs_mourn_inferior (struct target_ops *ops)
4391 if (!ptid_equal (inferior_ptid, null_ptid))
4393 /* Find procinfo for main process. */
4394 pi = find_procinfo (PIDGET (inferior_ptid), 0);
4396 destroy_procinfo (pi);
4398 unpush_target (ops);
4400 if (dbx_link_bpt != NULL)
4402 deprecated_remove_raw_breakpoint (target_gdbarch, dbx_link_bpt);
4403 dbx_link_bpt_addr = 0;
4404 dbx_link_bpt = NULL;
4407 generic_mourn_inferior ();
4410 /* When GDB forks to create a runnable inferior process, this function
4411 is called on the parent side of the fork. It's job is to do
4412 whatever is necessary to make the child ready to be debugged, and
4413 then wait for the child to synchronize. */
4416 procfs_init_inferior (struct target_ops *ops, int pid)
4419 gdb_sigset_t signals;
4423 /* This routine called on the parent side (GDB side)
4424 after GDB forks the inferior. */
4427 if ((pi = create_procinfo (pid, 0)) == NULL)
4428 perror (_("procfs: out of memory in 'init_inferior'"));
4430 if (!open_procinfo_files (pi, FD_CTL))
4431 proc_error (pi, "init_inferior, open_proc_files", __LINE__);
4435 open_procinfo_files // done
4438 procfs_notice_signals
4445 /* If not stopped yet, wait for it to stop. */
4446 if (!(proc_flags (pi) & PR_STOPPED) &&
4447 !(proc_wait_for_stop (pi)))
4448 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
4450 /* Save some of the /proc state to be restored if we detach. */
4451 /* FIXME: Why? In case another debugger was debugging it?
4452 We're it's parent, for Ghu's sake! */
4453 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
4454 proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
4455 if (!proc_get_held_signals (pi, &pi->saved_sighold))
4456 proc_error (pi, "init_inferior, get_held_signals", __LINE__);
4457 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
4458 proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
4459 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
4460 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
4461 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
4462 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
4464 if ((fail = procfs_debug_inferior (pi)) != 0)
4465 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
4467 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4468 and possibly even turning ON kill-on-last-close at this point. But
4469 I can't make that change without careful testing which I don't have
4470 time to do right now... */
4471 /* Turn on run-on-last-close flag so that the child
4472 will die if GDB goes away for some reason. */
4473 if (!proc_set_run_on_last_close (pi))
4474 proc_error (pi, "init_inferior, set_RLC", __LINE__);
4476 /* We now have have access to the lwpid of the main thread/lwp. */
4477 lwpid = proc_get_current_thread (pi);
4479 /* Create a procinfo for the main lwp. */
4480 create_procinfo (pid, lwpid);
4482 /* We already have a main thread registered in the thread table at
4483 this point, but it didn't have any lwp info yet. Notify the core
4484 about it. This changes inferior_ptid as well. */
4485 thread_change_ptid (pid_to_ptid (pid),
4486 MERGEPID (pid, lwpid));
4488 /* Typically two, one trap to exec the shell, one to exec the
4489 program being debugged. Defined by "inferior.h". */
4490 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
4493 /* On mips-irix, we need to stop the inferior early enough during
4494 the startup phase in order to be able to load the shared library
4495 symbols and insert the breakpoints that are located in these shared
4496 libraries. Stopping at the program entry point is not good enough
4497 because the -init code is executed before the execution reaches
4500 So what we need to do is to insert a breakpoint in the runtime
4501 loader (rld), more precisely in __dbx_link(). This procedure is
4502 called by rld once all shared libraries have been mapped, but before
4503 the -init code is executed. Unfortuantely, this is not straightforward,
4504 as rld is not part of the executable we are running, and thus we need
4505 the inferior to run until rld itself has been mapped in memory.
4507 For this, we trace all syssgi() syscall exit events. Each time
4508 we detect such an event, we iterate over each text memory maps,
4509 get its associated fd, and scan the symbol table for __dbx_link().
4510 When found, we know that rld has been mapped, and that we can insert
4511 the breakpoint at the symbol address. Once the dbx_link() breakpoint
4512 has been inserted, the syssgi() notifications are no longer necessary,
4513 so they should be canceled. */
4514 proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT, FLAG_SET, 0);
4518 /* When GDB forks to create a new process, this function is called on
4519 the child side of the fork before GDB exec's the user program. Its
4520 job is to make the child minimally debuggable, so that the parent
4521 GDB process can connect to the child and take over. This function
4522 should do only the minimum to make that possible, and to
4523 synchronize with the parent process. The parent process should
4524 take care of the details. */
4527 procfs_set_exec_trap (void)
4529 /* This routine called on the child side (inferior side)
4530 after GDB forks the inferior. It must use only local variables,
4531 because it may be sharing data space with its parent. */
4536 if ((pi = create_procinfo (getpid (), 0)) == NULL)
4537 perror_with_name (_("procfs: create_procinfo failed in child."));
4539 if (open_procinfo_files (pi, FD_CTL) == 0)
4541 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
4542 gdb_flush (gdb_stderr);
4543 /* No need to call "dead_procinfo", because we're going to
4548 #ifdef PRFS_STOPEXEC /* defined on OSF */
4549 /* OSF method for tracing exec syscalls. Quoting:
4550 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4551 exits from exec system calls because of the user level loader. */
4552 /* FIXME: make nice and maybe move into an access function. */
4556 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
4558 proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__);
4559 gdb_flush (gdb_stderr);
4562 prfs_flags |= PRFS_STOPEXEC;
4564 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
4566 proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__);
4567 gdb_flush (gdb_stderr);
4571 #else /* not PRFS_STOPEXEC */
4572 /* Everyone else's (except OSF) method for tracing exec syscalls. */
4574 Not all systems with /proc have all the exec* syscalls with the same
4575 names. On the SGI, for example, there is no SYS_exec, but there
4576 *is* a SYS_execv. So, we try to account for that. */
4578 exitset = sysset_t_alloc (pi);
4579 gdb_premptysysset (exitset);
4581 gdb_praddsysset (exitset, SYS_exec);
4584 gdb_praddsysset (exitset, SYS_execve);
4587 gdb_praddsysset (exitset, SYS_execv);
4589 #ifdef DYNAMIC_SYSCALLS
4591 int callnum = find_syscall (pi, "execve");
4594 gdb_praddsysset (exitset, callnum);
4596 callnum = find_syscall (pi, "ra_execve");
4598 gdb_praddsysset (exitset, callnum);
4600 #endif /* DYNAMIC_SYSCALLS */
4602 if (!proc_set_traced_sysexit (pi, exitset))
4604 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
4605 gdb_flush (gdb_stderr);
4608 #endif /* PRFS_STOPEXEC */
4610 /* FIXME: should this be done in the parent instead? */
4611 /* Turn off inherit on fork flag so that all grand-children
4612 of gdb start with tracing flags cleared. */
4613 if (!proc_unset_inherit_on_fork (pi))
4614 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
4616 /* Turn off run on last close flag, so that the child process
4617 cannot run away just because we close our handle on it.
4618 We want it to wait for the parent to attach. */
4619 if (!proc_unset_run_on_last_close (pi))
4620 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
4622 /* FIXME: No need to destroy the procinfo --
4623 we have our own address space, and we're about to do an exec! */
4624 /*destroy_procinfo (pi);*/
4627 /* This function is called BEFORE gdb forks the inferior process. Its
4628 only real responsibility is to set things up for the fork, and tell
4629 GDB which two functions to call after the fork (one for the parent,
4630 and one for the child).
4632 This function does a complicated search for a unix shell program,
4633 which it then uses to parse arguments and environment variables to
4634 be sent to the child. I wonder whether this code could not be
4635 abstracted out and shared with other unix targets such as
4639 procfs_create_inferior (struct target_ops *ops, char *exec_file,
4640 char *allargs, char **env, int from_tty)
4642 char *shell_file = getenv ("SHELL");
4646 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
4649 /* We will be looking down the PATH to find shell_file. If we
4650 just do this the normal way (via execlp, which operates by
4651 attempting an exec for each element of the PATH until it
4652 finds one which succeeds), then there will be an exec for
4653 each failed attempt, each of which will cause a PR_SYSEXIT
4654 stop, and we won't know how to distinguish the PR_SYSEXIT's
4655 for these failed execs with the ones for successful execs
4656 (whether the exec has succeeded is stored at that time in the
4657 carry bit or some such architecture-specific and
4658 non-ABI-specified place).
4660 So I can't think of anything better than to search the PATH
4661 now. This has several disadvantages: (1) There is a race
4662 condition; if we find a file now and it is deleted before we
4663 exec it, we lose, even if the deletion leaves a valid file
4664 further down in the PATH, (2) there is no way to know exactly
4665 what an executable (in the sense of "capable of being
4666 exec'd") file is. Using access() loses because it may lose
4667 if the caller is the superuser; failing to use it loses if
4668 there are ACLs or some such. */
4672 /* FIXME-maybe: might want "set path" command so user can change what
4673 path is used from within GDB. */
4674 char *path = getenv ("PATH");
4676 struct stat statbuf;
4679 path = "/bin:/usr/bin";
4681 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
4682 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
4684 p1 = strchr (p, ':');
4689 strncpy (tryname, p, len);
4690 tryname[len] = '\0';
4691 strcat (tryname, "/");
4692 strcat (tryname, shell_file);
4693 if (access (tryname, X_OK) < 0)
4695 if (stat (tryname, &statbuf) < 0)
4697 if (!S_ISREG (statbuf.st_mode))
4698 /* We certainly need to reject directories. I'm not quite
4699 as sure about FIFOs, sockets, etc., but I kind of doubt
4700 that people want to exec() these things. */
4705 /* Not found. This must be an error rather than merely passing
4706 the file to execlp(), because execlp() would try all the
4707 exec()s, causing GDB to get confused. */
4708 error (_("procfs:%d -- Can't find shell %s in PATH"),
4709 __LINE__, shell_file);
4711 shell_file = tryname;
4714 pid = fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
4715 NULL, NULL, shell_file, NULL);
4717 procfs_init_inferior (ops, pid);
4720 /* An observer for the "inferior_created" event. */
4723 procfs_inferior_created (struct target_ops *ops, int from_tty)
4726 /* Make sure to cancel the syssgi() syscall-exit notifications.
4727 They should normally have been removed by now, but they may still
4728 be activated if the inferior doesn't use shared libraries, or if
4729 we didn't locate __dbx_link, or if we never stopped in __dbx_link.
4730 See procfs_init_inferior() for more details.
4732 Since these notifications are only ever enabled when we spawned
4733 the inferior ourselves, there is nothing to do when the inferior
4734 was created by attaching to an already running process, or when
4735 debugging a core file. */
4736 if (current_inferior ()->attach_flag || !target_can_run (¤t_target))
4739 proc_trace_syscalls_1 (find_procinfo_or_die (PIDGET (inferior_ptid), 0),
4740 SYS_syssgi, PR_SYSEXIT, FLAG_RESET, 0);
4744 /* Callback for find_new_threads. Calls "add_thread". */
4747 procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
4749 ptid_t gdb_threadid = MERGEPID (pi->pid, thread->tid);
4751 if (!in_thread_list (gdb_threadid) || is_exited (gdb_threadid))
4752 add_thread (gdb_threadid);
4757 /* Query all the threads that the target knows about, and give them
4758 back to GDB to add to its list. */
4761 procfs_find_new_threads (struct target_ops *ops)
4765 /* Find procinfo for main process. */
4766 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4767 proc_update_threads (pi);
4768 proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
4771 /* Return true if the thread is still 'alive'. This guy doesn't
4772 really seem to be doing his job. Got to investigate how to tell
4773 when a thread is really gone. */
4776 procfs_thread_alive (struct target_ops *ops, ptid_t ptid)
4781 proc = PIDGET (ptid);
4782 thread = TIDGET (ptid);
4783 /* If I don't know it, it ain't alive! */
4784 if ((pi = find_procinfo (proc, thread)) == NULL)
4787 /* If I can't get its status, it ain't alive!
4788 What's more, I need to forget about it! */
4789 if (!proc_get_status (pi))
4791 destroy_procinfo (pi);
4794 /* I couldn't have got its status if it weren't alive, so it's
4799 /* Convert PTID to a string. Returns the string in a static
4803 procfs_pid_to_str (struct target_ops *ops, ptid_t ptid)
4805 static char buf[80];
4807 if (TIDGET (ptid) == 0)
4808 sprintf (buf, "process %d", PIDGET (ptid));
4810 sprintf (buf, "LWP %ld", TIDGET (ptid));
4815 /* Insert a watchpoint. */
4818 procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag,
4826 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
4827 PIDGET (inferior_ptid) : PIDGET (ptid), 0);
4829 /* Translate from GDB's flags to /proc's. */
4830 if (len > 0) /* len == 0 means delete watchpoint. */
4832 switch (rwflag) { /* FIXME: need an enum! */
4833 case hw_write: /* default watchpoint (write) */
4834 pflags = WRITE_WATCHFLAG;
4836 case hw_read: /* read watchpoint */
4837 pflags = READ_WATCHFLAG;
4839 case hw_access: /* access watchpoint */
4840 pflags = READ_WATCHFLAG | WRITE_WATCHFLAG;
4842 case hw_execute: /* execution HW breakpoint */
4843 pflags = EXEC_WATCHFLAG;
4845 default: /* Something weird. Return error. */
4848 if (after) /* Stop after r/w access is completed. */
4849 pflags |= AFTER_WATCHFLAG;
4852 if (!proc_set_watchpoint (pi, addr, len, pflags))
4854 if (errno == E2BIG) /* Typical error for no resources. */
4855 return -1; /* fail */
4856 /* GDB may try to remove the same watchpoint twice.
4857 If a remove request returns no match, don't error. */
4858 if (errno == ESRCH && len == 0)
4859 return 0; /* ignore */
4860 proc_error (pi, "set_watchpoint", __LINE__);
4863 #endif /* UNIXWARE */
4867 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
4868 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
4869 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
4872 Note: procfs_can_use_hw_breakpoint() is not yet used by all
4873 procfs.c targets due to the fact that some of them still define
4874 target_can_use_hardware_watchpoint. */
4877 procfs_can_use_hw_breakpoint (int type, int cnt, int othertype)
4879 /* Due to the way that proc_set_watchpoint() is implemented, host
4880 and target pointers must be of the same size. If they are not,
4881 we can't use hardware watchpoints. This limitation is due to the
4882 fact that proc_set_watchpoint() calls
4883 procfs_address_to_host_pointer(); a close inspection of
4884 procfs_address_to_host_pointer will reveal that an internal error
4885 will be generated when the host and target pointer sizes are
4887 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
4889 if (sizeof (void *) != TYPE_LENGTH (ptr_type))
4892 /* Other tests here??? */
4897 /* Returns non-zero if process is stopped on a hardware watchpoint
4898 fault, else returns zero. */
4901 procfs_stopped_by_watchpoint (void)
4905 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4907 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
4909 if (proc_why (pi) == PR_FAULTED)
4912 if (proc_what (pi) == FLTWATCH)
4916 if (proc_what (pi) == FLTKWATCH)
4924 /* Returns 1 if the OS knows the position of the triggered watchpoint,
4925 and sets *ADDR to that address. Returns 0 if OS cannot report that
4926 address. This function is only called if
4927 procfs_stopped_by_watchpoint returned 1, thus no further checks are
4928 done. The function also assumes that ADDR is not NULL. */
4931 procfs_stopped_data_address (struct target_ops *targ, CORE_ADDR *addr)
4935 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4936 return proc_watchpoint_address (pi, addr);
4940 procfs_insert_watchpoint (CORE_ADDR addr, int len, int type,
4941 struct expression *cond)
4943 if (!target_have_steppable_watchpoint
4944 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch))
4946 /* When a hardware watchpoint fires off the PC will be left at
4947 the instruction following the one which caused the
4948 watchpoint. It will *NOT* be necessary for GDB to step over
4950 return procfs_set_watchpoint (inferior_ptid, addr, len, type, 1);
4954 /* When a hardware watchpoint fires off the PC will be left at
4955 the instruction which caused the watchpoint. It will be
4956 necessary for GDB to step over the watchpoint. */
4957 return procfs_set_watchpoint (inferior_ptid, addr, len, type, 0);
4962 procfs_remove_watchpoint (CORE_ADDR addr, int len, int type,
4963 struct expression *cond)
4965 return procfs_set_watchpoint (inferior_ptid, addr, 0, 0, 0);
4969 procfs_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len)
4971 /* The man page for proc(4) on Solaris 2.6 and up says that the
4972 system can support "thousands" of hardware watchpoints, but gives
4973 no method for finding out how many; It doesn't say anything about
4974 the allowed size for the watched area either. So we just tell
4980 procfs_use_watchpoints (struct target_ops *t)
4982 t->to_stopped_by_watchpoint = procfs_stopped_by_watchpoint;
4983 t->to_insert_watchpoint = procfs_insert_watchpoint;
4984 t->to_remove_watchpoint = procfs_remove_watchpoint;
4985 t->to_region_ok_for_hw_watchpoint = procfs_region_ok_for_hw_watchpoint;
4986 t->to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint;
4987 t->to_stopped_data_address = procfs_stopped_data_address;
4990 /* Memory Mappings Functions: */
4992 /* Call a callback function once for each mapping, passing it the
4993 mapping, an optional secondary callback function, and some optional
4994 opaque data. Quit and return the first non-zero value returned
4997 PI is the procinfo struct for the process to be mapped. FUNC is
4998 the callback function to be called by this iterator. DATA is the
4999 optional opaque data to be passed to the callback function.
5000 CHILD_FUNC is the optional secondary function pointer to be passed
5001 to the child function. Returns the first non-zero return value
5002 from the callback function, or zero. */
5005 iterate_over_mappings (procinfo *pi, find_memory_region_ftype child_func,
5007 int (*func) (struct prmap *map,
5008 find_memory_region_ftype child_func,
5011 char pathname[MAX_PROC_NAME_SIZE];
5012 struct prmap *prmaps;
5013 struct prmap *prmap;
5017 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
5022 /* Get the number of mappings, allocate space,
5023 and read the mappings into prmaps. */
5026 sprintf (pathname, "/proc/%d/map", pi->pid);
5027 if ((map_fd = open (pathname, O_RDONLY)) < 0)
5028 proc_error (pi, "iterate_over_mappings (open)", __LINE__);
5030 /* Make sure it gets closed again. */
5031 make_cleanup_close (map_fd);
5033 /* Use stat to determine the file size, and compute
5034 the number of prmap_t objects it contains. */
5035 if (fstat (map_fd, &sbuf) != 0)
5036 proc_error (pi, "iterate_over_mappings (fstat)", __LINE__);
5038 nmap = sbuf.st_size / sizeof (prmap_t);
5039 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5040 if (read (map_fd, (char *) prmaps, nmap * sizeof (*prmaps))
5041 != (nmap * sizeof (*prmaps)))
5042 proc_error (pi, "iterate_over_mappings (read)", __LINE__);
5044 /* Use ioctl command PIOCNMAP to get number of mappings. */
5045 if (ioctl (pi->ctl_fd, PIOCNMAP, &nmap) != 0)
5046 proc_error (pi, "iterate_over_mappings (PIOCNMAP)", __LINE__);
5048 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5049 if (ioctl (pi->ctl_fd, PIOCMAP, prmaps) != 0)
5050 proc_error (pi, "iterate_over_mappings (PIOCMAP)", __LINE__);
5053 for (prmap = prmaps; nmap > 0; prmap++, nmap--)
5054 if ((funcstat = (*func) (prmap, child_func, data)) != 0)
5056 do_cleanups (cleanups);
5060 do_cleanups (cleanups);
5064 /* Implements the to_find_memory_regions method. Calls an external
5065 function for each memory region.
5066 Returns the integer value returned by the callback. */
5069 find_memory_regions_callback (struct prmap *map,
5070 find_memory_region_ftype func, void *data)
5072 return (*func) ((CORE_ADDR) map->pr_vaddr,
5074 (map->pr_mflags & MA_READ) != 0,
5075 (map->pr_mflags & MA_WRITE) != 0,
5076 (map->pr_mflags & MA_EXEC) != 0,
5080 /* External interface. Calls a callback function once for each
5081 mapped memory region in the child process, passing as arguments:
5083 CORE_ADDR virtual_address,
5085 int read, TRUE if region is readable by the child
5086 int write, TRUE if region is writable by the child
5087 int execute TRUE if region is executable by the child.
5089 Stops iterating and returns the first non-zero value returned by
5093 proc_find_memory_regions (find_memory_region_ftype func, void *data)
5095 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5097 return iterate_over_mappings (pi, func, data,
5098 find_memory_regions_callback);
5101 /* Returns an ascii representation of a memory mapping's flags. */
5104 mappingflags (long flags)
5106 static char asciiflags[8];
5108 strcpy (asciiflags, "-------");
5109 #if defined (MA_PHYS)
5110 if (flags & MA_PHYS)
5111 asciiflags[0] = 'd';
5113 if (flags & MA_STACK)
5114 asciiflags[1] = 's';
5115 if (flags & MA_BREAK)
5116 asciiflags[2] = 'b';
5117 if (flags & MA_SHARED)
5118 asciiflags[3] = 's';
5119 if (flags & MA_READ)
5120 asciiflags[4] = 'r';
5121 if (flags & MA_WRITE)
5122 asciiflags[5] = 'w';
5123 if (flags & MA_EXEC)
5124 asciiflags[6] = 'x';
5125 return (asciiflags);
5128 /* Callback function, does the actual work for 'info proc
5132 info_mappings_callback (struct prmap *map, find_memory_region_ftype ignore,
5135 unsigned int pr_off;
5137 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5138 pr_off = (unsigned int) map->pr_offset;
5140 pr_off = map->pr_off;
5143 if (gdbarch_addr_bit (target_gdbarch) == 32)
5144 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
5145 (unsigned long) map->pr_vaddr,
5146 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5147 (unsigned long) map->pr_size,
5149 mappingflags (map->pr_mflags));
5151 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
5152 (unsigned long) map->pr_vaddr,
5153 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5154 (unsigned long) map->pr_size,
5156 mappingflags (map->pr_mflags));
5161 /* Implement the "info proc mappings" subcommand. */
5164 info_proc_mappings (procinfo *pi, int summary)
5167 return; /* No output for summary mode. */
5169 printf_filtered (_("Mapped address spaces:\n\n"));
5170 if (gdbarch_ptr_bit (target_gdbarch) == 32)
5171 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
5178 printf_filtered (" %18s %18s %10s %10s %7s\n",
5185 iterate_over_mappings (pi, NULL, NULL, info_mappings_callback);
5186 printf_filtered ("\n");
5189 /* Implement the "info proc" command. */
5192 procfs_info_proc (struct target_ops *ops, char *args,
5193 enum info_proc_what what)
5195 struct cleanup *old_chain;
5196 procinfo *process = NULL;
5197 procinfo *thread = NULL;
5215 error (_("Not supported on this target."));
5218 old_chain = make_cleanup (null_cleanup, 0);
5221 argv = gdb_buildargv (args);
5222 make_cleanup_freeargv (argv);
5224 while (argv != NULL && *argv != NULL)
5226 if (isdigit (argv[0][0]))
5228 pid = strtoul (argv[0], &tmp, 10);
5230 tid = strtoul (++tmp, NULL, 10);
5232 else if (argv[0][0] == '/')
5234 tid = strtoul (argv[0] + 1, NULL, 10);
5239 pid = PIDGET (inferior_ptid);
5241 error (_("No current process: you must name one."));
5244 /* Have pid, will travel.
5245 First see if it's a process we're already debugging. */
5246 process = find_procinfo (pid, 0);
5247 if (process == NULL)
5249 /* No. So open a procinfo for it, but
5250 remember to close it again when finished. */
5251 process = create_procinfo (pid, 0);
5252 make_cleanup (do_destroy_procinfo_cleanup, process);
5253 if (!open_procinfo_files (process, FD_CTL))
5254 proc_error (process, "info proc, open_procinfo_files", __LINE__);
5258 thread = create_procinfo (pid, tid);
5262 printf_filtered (_("process %d flags:\n"), process->pid);
5263 proc_prettyprint_flags (proc_flags (process), 1);
5264 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
5265 proc_prettyprint_why (proc_why (process), proc_what (process), 1);
5266 if (proc_get_nthreads (process) > 1)
5267 printf_filtered ("Process has %d threads.\n",
5268 proc_get_nthreads (process));
5272 printf_filtered (_("thread %d flags:\n"), thread->tid);
5273 proc_prettyprint_flags (proc_flags (thread), 1);
5274 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
5275 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
5280 info_proc_mappings (process, 0);
5283 do_cleanups (old_chain);
5286 /* Modify the status of the system call identified by SYSCALLNUM in
5287 the set of syscalls that are currently traced/debugged.
5289 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
5290 will be updated. Otherwise, the exit syscalls set will be updated.
5292 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
5293 will be disabled. */
5296 proc_trace_syscalls_1 (procinfo *pi, int syscallnum, int entry_or_exit,
5297 int mode, int from_tty)
5301 if (entry_or_exit == PR_SYSENTRY)
5302 sysset = proc_get_traced_sysentry (pi, NULL);
5304 sysset = proc_get_traced_sysexit (pi, NULL);
5307 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
5309 if (mode == FLAG_SET)
5310 gdb_praddsysset (sysset, syscallnum);
5312 gdb_prdelsysset (sysset, syscallnum);
5314 if (entry_or_exit == PR_SYSENTRY)
5316 if (!proc_set_traced_sysentry (pi, sysset))
5317 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
5321 if (!proc_set_traced_sysexit (pi, sysset))
5322 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
5327 proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode)
5331 if (PIDGET (inferior_ptid) <= 0)
5332 error (_("you must be debugging a process to use this command."));
5334 if (args == NULL || args[0] == 0)
5335 error_no_arg (_("system call to trace"));
5337 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5338 if (isdigit (args[0]))
5340 const int syscallnum = atoi (args);
5342 proc_trace_syscalls_1 (pi, syscallnum, entry_or_exit, mode, from_tty);
5347 proc_trace_sysentry_cmd (char *args, int from_tty)
5349 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
5353 proc_trace_sysexit_cmd (char *args, int from_tty)
5355 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
5359 proc_untrace_sysentry_cmd (char *args, int from_tty)
5361 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
5365 proc_untrace_sysexit_cmd (char *args, int from_tty)
5367 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
5371 /* Provide a prototype to silence -Wmissing-prototypes. */
5372 extern void _initialize_procfs (void);
5375 _initialize_procfs (void)
5377 observer_attach_inferior_created (procfs_inferior_created);
5379 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
5380 _("Give a trace of entries into the syscall."));
5381 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
5382 _("Give a trace of exits from the syscall."));
5383 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
5384 _("Cancel a trace of entries into the syscall."));
5385 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
5386 _("Cancel a trace of exits from the syscall."));
5389 /* =================== END, GDB "MODULE" =================== */
5393 /* miscellaneous stubs: */
5395 /* The following satisfy a few random symbols mostly created by the
5396 solaris threads implementation, which I will chase down later. */
5398 /* Return a pid for which we guarantee we will be able to find a
5402 procfs_first_available (void)
5404 return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1);
5407 /* =================== GCORE .NOTE "MODULE" =================== */
5408 #if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT)
5409 /* gcore only implemented on solaris and unixware (so far) */
5412 procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
5413 char *note_data, int *note_size,
5414 enum target_signal stop_signal)
5416 struct regcache *regcache = get_thread_regcache (ptid);
5417 gdb_gregset_t gregs;
5418 gdb_fpregset_t fpregs;
5419 unsigned long merged_pid;
5420 struct cleanup *old_chain;
5422 merged_pid = TIDGET (ptid) << 16 | PIDGET (ptid);
5424 /* This part is the old method for fetching registers.
5425 It should be replaced by the newer one using regsets
5426 once it is implemented in this platform:
5427 gdbarch_regset_from_core_section() and regset->collect_regset(). */
5429 old_chain = save_inferior_ptid ();
5430 inferior_ptid = ptid;
5431 target_fetch_registers (regcache, -1);
5433 fill_gregset (regcache, &gregs, -1);
5434 #if defined (NEW_PROC_API)
5435 note_data = (char *) elfcore_write_lwpstatus (obfd,
5442 note_data = (char *) elfcore_write_prstatus (obfd,
5449 fill_fpregset (regcache, &fpregs, -1);
5450 note_data = (char *) elfcore_write_prfpreg (obfd,
5456 do_cleanups (old_chain);
5461 struct procfs_corefile_thread_data {
5465 enum target_signal stop_signal;
5469 procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data)
5471 struct procfs_corefile_thread_data *args = data;
5475 ptid_t ptid = MERGEPID (pi->pid, thread->tid);
5477 args->note_data = procfs_do_thread_registers (args->obfd, ptid,
5486 find_signalled_thread (struct thread_info *info, void *data)
5488 if (info->suspend.stop_signal != TARGET_SIGNAL_0
5489 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid))
5495 static enum target_signal
5496 find_stop_signal (void)
5498 struct thread_info *info =
5499 iterate_over_threads (find_signalled_thread, NULL);
5502 return info->suspend.stop_signal;
5504 return TARGET_SIGNAL_0;
5508 procfs_make_note_section (bfd *obfd, int *note_size)
5510 struct cleanup *old_chain;
5511 gdb_gregset_t gregs;
5512 gdb_fpregset_t fpregs;
5513 char fname[16] = {'\0'};
5514 char psargs[80] = {'\0'};
5515 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5516 char *note_data = NULL;
5518 struct procfs_corefile_thread_data thread_args;
5521 enum target_signal stop_signal;
5523 if (get_exec_file (0))
5525 strncpy (fname, lbasename (get_exec_file (0)), sizeof (fname));
5526 fname[sizeof (fname) - 1] = 0;
5527 strncpy (psargs, get_exec_file (0), sizeof (psargs));
5528 psargs[sizeof (psargs) - 1] = 0;
5530 inf_args = get_inferior_args ();
5531 if (inf_args && *inf_args &&
5532 strlen (inf_args) < ((int) sizeof (psargs) - (int) strlen (psargs)))
5534 strncat (psargs, " ",
5535 sizeof (psargs) - strlen (psargs));
5536 strncat (psargs, inf_args,
5537 sizeof (psargs) - strlen (psargs));
5541 note_data = (char *) elfcore_write_prpsinfo (obfd,
5547 stop_signal = find_stop_signal ();
5550 fill_gregset (get_current_regcache (), &gregs, -1);
5551 note_data = elfcore_write_pstatus (obfd, note_data, note_size,
5552 PIDGET (inferior_ptid),
5553 stop_signal, &gregs);
5556 thread_args.obfd = obfd;
5557 thread_args.note_data = note_data;
5558 thread_args.note_size = note_size;
5559 thread_args.stop_signal = stop_signal;
5560 proc_iterate_over_threads (pi, procfs_corefile_thread_callback,
5563 /* There should be always at least one thread. */
5564 gdb_assert (thread_args.note_data != note_data);
5565 note_data = thread_args.note_data;
5567 auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV,
5571 note_data = elfcore_write_note (obfd, note_data, note_size,
5572 "CORE", NT_AUXV, auxv, auxv_len);
5576 make_cleanup (xfree, note_data);
5579 #else /* !(Solaris or Unixware) */
5581 procfs_make_note_section (bfd *obfd, int *note_size)
5583 error (_("gcore not implemented for this host."));
5584 return NULL; /* lint */
5586 #endif /* Solaris or Unixware */
5587 /* =================== END GCORE .NOTE "MODULE" =================== */