1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
3 Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 Written by Michael Snyder at Cygnus Solutions.
6 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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>
50 #include "gdb_assert.h"
56 /* This module provides the interface between GDB and the
57 /proc file system, which is used on many versions of Unix
58 as a means for debuggers to control other processes.
60 Examples of the systems that use this interface are:
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 <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 <sys/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
*, const char *, int);
114 static void procfs_resume (struct target_ops
*,
115 ptid_t
, int, enum gdb_signal
);
116 static void procfs_stop (struct target_ops
*self
, 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 (struct target_ops
*self
,
123 int, unsigned char *);
124 static void procfs_kill_inferior (struct target_ops
*ops
);
125 static void procfs_mourn_inferior (struct target_ops
*ops
);
126 static void procfs_create_inferior (struct target_ops
*, char *,
127 char *, char **, int);
128 static ptid_t
procfs_wait (struct target_ops
*,
129 ptid_t
, struct target_waitstatus
*, int);
130 static int procfs_xfer_memory (CORE_ADDR
, gdb_byte
*, int, int,
131 struct mem_attrib
*attrib
,
132 struct target_ops
*);
133 static target_xfer_partial_ftype procfs_xfer_partial
;
135 static int procfs_thread_alive (struct target_ops
*ops
, ptid_t
);
137 static void procfs_find_new_threads (struct target_ops
*ops
);
138 static char *procfs_pid_to_str (struct target_ops
*, ptid_t
);
140 static int proc_find_memory_regions (struct target_ops
*self
,
141 find_memory_region_ftype
, void *);
143 static char * procfs_make_note_section (struct target_ops
*self
,
146 static int procfs_can_use_hw_breakpoint (struct target_ops
*self
,
149 static void procfs_info_proc (struct target_ops
*, char *,
150 enum info_proc_what
);
152 #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
153 /* When GDB is built as 64-bit application on Solaris, the auxv data
154 is presented in 64-bit format. We need to provide a custom parser
157 procfs_auxv_parse (struct target_ops
*ops
, gdb_byte
**readptr
,
158 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
160 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
161 gdb_byte
*ptr
= *readptr
;
166 if (endptr
- ptr
< 8 * 2)
169 *typep
= extract_unsigned_integer (ptr
, 4, byte_order
);
171 /* The size of data is always 64-bit. If the application is 32-bit,
172 it will be zero extended, as expected. */
173 *valp
= extract_unsigned_integer (ptr
, 8, byte_order
);
184 struct target_ops
*t
= inf_child_target ();
186 t
->to_shortname
= "procfs";
187 t
->to_longname
= "Unix /proc child process";
189 "Unix /proc child process (started by the \"run\" command).";
190 t
->to_create_inferior
= procfs_create_inferior
;
191 t
->to_kill
= procfs_kill_inferior
;
192 t
->to_mourn_inferior
= procfs_mourn_inferior
;
193 t
->to_attach
= procfs_attach
;
194 t
->to_detach
= procfs_detach
;
195 t
->to_wait
= procfs_wait
;
196 t
->to_resume
= procfs_resume
;
197 t
->to_fetch_registers
= procfs_fetch_registers
;
198 t
->to_store_registers
= procfs_store_registers
;
199 t
->to_xfer_partial
= procfs_xfer_partial
;
200 t
->deprecated_xfer_memory
= procfs_xfer_memory
;
201 t
->to_pass_signals
= procfs_pass_signals
;
202 t
->to_files_info
= procfs_files_info
;
203 t
->to_stop
= procfs_stop
;
205 t
->to_find_new_threads
= procfs_find_new_threads
;
206 t
->to_thread_alive
= procfs_thread_alive
;
207 t
->to_pid_to_str
= procfs_pid_to_str
;
209 t
->to_has_thread_control
= tc_schedlock
;
210 t
->to_find_memory_regions
= proc_find_memory_regions
;
211 t
->to_make_corefile_notes
= procfs_make_note_section
;
212 t
->to_info_proc
= procfs_info_proc
;
214 #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
215 t
->to_auxv_parse
= procfs_auxv_parse
;
218 t
->to_magic
= OPS_MAGIC
;
223 /* =================== END, TARGET_OPS "MODULE" =================== */
225 /* World Unification:
227 Put any typedefs, defines etc. here that are required for the
228 unification of code that handles different versions of /proc. */
230 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
232 enum { READ_WATCHFLAG
= WA_READ
,
233 WRITE_WATCHFLAG
= WA_WRITE
,
234 EXEC_WATCHFLAG
= WA_EXEC
,
235 AFTER_WATCHFLAG
= WA_TRAPAFTER
238 #else /* Irix method for watchpoints */
239 enum { READ_WATCHFLAG
= MA_READ
,
240 WRITE_WATCHFLAG
= MA_WRITE
,
241 EXEC_WATCHFLAG
= MA_EXEC
,
242 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
247 #ifdef HAVE_PR_SIGSET_T
248 typedef pr_sigset_t gdb_sigset_t
;
250 typedef sigset_t gdb_sigset_t
;
254 #ifdef HAVE_PR_SIGACTION64_T
255 typedef pr_sigaction64_t gdb_sigaction_t
;
257 typedef struct sigaction gdb_sigaction_t
;
261 #ifdef HAVE_PR_SIGINFO64_T
262 typedef pr_siginfo64_t gdb_siginfo_t
;
264 typedef siginfo_t gdb_siginfo_t
;
267 /* On mips-irix, praddset and prdelset are defined in such a way that
268 they return a value, which causes GCC to emit a -Wunused error
269 because the returned value is not used. Prevent this warning
270 by casting the return value to void. On sparc-solaris, this issue
271 does not exist because the definition of these macros already include
272 that cast to void. */
273 #define gdb_praddset(sp, flag) ((void) praddset (sp, flag))
274 #define gdb_prdelset(sp, flag) ((void) prdelset (sp, flag))
276 /* gdb_premptysysset */
278 #define gdb_premptysysset premptysysset
280 #define gdb_premptysysset premptyset
285 #define gdb_praddsysset praddsysset
287 #define gdb_praddsysset gdb_praddset
292 #define gdb_prdelsysset prdelsysset
294 #define gdb_prdelsysset gdb_prdelset
297 /* prissyssetmember */
298 #ifdef prissyssetmember
299 #define gdb_pr_issyssetmember prissyssetmember
301 #define gdb_pr_issyssetmember prismember
304 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
305 as intuitively descriptive as it could be, so we'll define
306 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
307 this writing, this feature is only found on AIX5 systems and
308 basically means that the set of syscalls is not fixed. I.e,
309 there's no nice table that one can #include to get all of the
310 syscall numbers. Instead, they're stored in /proc/PID/sysent
311 for each process. We are at least guaranteed that they won't
312 change over the lifetime of the process. But each process could
313 (in theory) have different syscall numbers. */
314 #ifdef HAVE_PRSYSENT_T
315 #define DYNAMIC_SYSCALLS
320 /* =================== STRUCT PROCINFO "MODULE" =================== */
322 /* FIXME: this comment will soon be out of date W.R.T. threads. */
324 /* The procinfo struct is a wrapper to hold all the state information
325 concerning a /proc process. There should be exactly one procinfo
326 for each process, and since GDB currently can debug only one
327 process at a time, that means there should be only one procinfo.
328 All of the LWP's of a process can be accessed indirectly thru the
329 single process procinfo.
331 However, against the day when GDB may debug more than one process,
332 this data structure is kept in a list (which for now will hold no
333 more than one member), and many functions will have a pointer to a
334 procinfo as an argument.
336 There will be a separate procinfo structure for use by the (not yet
337 implemented) "info proc" command, so that we can print useful
338 information about any random process without interfering with the
339 inferior's procinfo information. */
342 /* format strings for /proc paths */
343 # ifndef CTL_PROC_NAME_FMT
344 # define MAIN_PROC_NAME_FMT "/proc/%d"
345 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
346 # define AS_PROC_NAME_FMT "/proc/%d/as"
347 # define MAP_PROC_NAME_FMT "/proc/%d/map"
348 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
349 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
351 /* the name of the proc status struct depends on the implementation */
352 typedef pstatus_t gdb_prstatus_t
;
353 typedef lwpstatus_t gdb_lwpstatus_t
;
354 #else /* ! NEW_PROC_API */
355 /* format strings for /proc paths */
356 # ifndef CTL_PROC_NAME_FMT
357 # define MAIN_PROC_NAME_FMT "/proc/%05d"
358 # define CTL_PROC_NAME_FMT "/proc/%05d"
359 # define AS_PROC_NAME_FMT "/proc/%05d"
360 # define MAP_PROC_NAME_FMT "/proc/%05d"
361 # define STATUS_PROC_NAME_FMT "/proc/%05d"
362 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
364 /* The name of the proc status struct depends on the implementation. */
365 typedef prstatus_t gdb_prstatus_t
;
366 typedef prstatus_t gdb_lwpstatus_t
;
367 #endif /* NEW_PROC_API */
369 typedef struct procinfo
{
370 struct procinfo
*next
;
371 int pid
; /* Process ID */
372 int tid
; /* Thread/LWP id */
376 int ignore_next_sigstop
;
378 /* The following four fd fields may be identical, or may contain
379 several different fd's, depending on the version of /proc
380 (old ioctl or new read/write). */
382 int ctl_fd
; /* File descriptor for /proc control file */
384 /* The next three file descriptors are actually only needed in the
385 read/write, multiple-file-descriptor implemenation
386 (NEW_PROC_API). However, to avoid a bunch of #ifdefs in the
387 code, we will use them uniformly by (in the case of the ioctl
388 single-file-descriptor implementation) filling them with copies
389 of the control fd. */
390 int status_fd
; /* File descriptor for /proc status file */
391 int as_fd
; /* File descriptor for /proc as file */
393 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
395 fltset_t saved_fltset
; /* Saved traced hardware fault set */
396 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
397 gdb_sigset_t saved_sighold
; /* Saved held signal set */
398 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
399 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
401 gdb_prstatus_t prstatus
; /* Current process status info */
404 gdb_fpregset_t fpregset
; /* Current floating point registers */
407 #ifdef DYNAMIC_SYSCALLS
408 int num_syscalls
; /* Total number of syscalls */
409 char **syscall_names
; /* Syscall number to name map */
412 struct procinfo
*thread_list
;
414 int status_valid
: 1;
416 int fpregs_valid
: 1;
417 int threads_valid
: 1;
420 static char errmsg
[128]; /* shared error msg buffer */
422 /* Function prototypes for procinfo module: */
424 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
425 static procinfo
*find_procinfo (int pid
, int tid
);
426 static procinfo
*create_procinfo (int pid
, int tid
);
427 static void destroy_procinfo (procinfo
* p
);
428 static void do_destroy_procinfo_cleanup (void *);
429 static void dead_procinfo (procinfo
* p
, char *msg
, int killp
);
430 static int open_procinfo_files (procinfo
* p
, int which
);
431 static void close_procinfo_files (procinfo
* p
);
432 static int sysset_t_size (procinfo
*p
);
433 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
434 #ifdef DYNAMIC_SYSCALLS
435 static void load_syscalls (procinfo
*pi
);
436 static void free_syscalls (procinfo
*pi
);
437 static int find_syscall (procinfo
*pi
, char *name
);
438 #endif /* DYNAMIC_SYSCALLS */
440 static int iterate_over_mappings
441 (procinfo
*pi
, find_memory_region_ftype child_func
, void *data
,
442 int (*func
) (struct prmap
*map
, find_memory_region_ftype child_func
,
445 /* The head of the procinfo list: */
446 static procinfo
* procinfo_list
;
448 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
452 find_procinfo (int pid
, int tid
)
456 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
463 /* Don't check threads_valid. If we're updating the
464 thread_list, we want to find whatever threads are already
465 here. This means that in general it is the caller's
466 responsibility to check threads_valid and update before
467 calling find_procinfo, if the caller wants to find a new
470 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
478 /* Calls find_procinfo, but errors on failure. */
481 find_procinfo_or_die (int pid
, int tid
)
483 procinfo
*pi
= find_procinfo (pid
, tid
);
488 error (_("procfs: couldn't find pid %d "
489 "(kernel thread %d) in procinfo list."),
492 error (_("procfs: couldn't find pid %d in procinfo list."), pid
);
497 /* Wrapper for `open'. The appropriate open call is attempted; if
498 unsuccessful, it will be retried as many times as needed for the
499 EAGAIN and EINTR conditions.
501 For other conditions, retry the open a limited number of times. In
502 addition, a short sleep is imposed prior to retrying the open. The
503 reason for this sleep is to give the kernel a chance to catch up
504 and create the file in question in the event that GDB "wins" the
505 race to open a file before the kernel has created it. */
508 open_with_retry (const char *pathname
, int flags
)
510 int retries_remaining
, status
;
512 retries_remaining
= 2;
516 status
= open (pathname
, flags
);
518 if (status
>= 0 || retries_remaining
== 0)
520 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
530 /* Open the file descriptor for the process or LWP. If NEW_PROC_API
531 is defined, we only open the control file descriptor; the others
532 are opened lazily as needed. Otherwise (if not NEW_PROC_API),
533 there is only one real file descriptor, but we keep multiple copies
534 of it so that the code that uses them does not have to be #ifdef'd.
535 Returns the file descriptor, or zero for failure. */
537 enum { FD_CTL
, FD_STATUS
, FD_AS
};
540 open_procinfo_files (procinfo
*pi
, int which
)
543 char tmp
[MAX_PROC_NAME_SIZE
];
547 /* This function is getting ALMOST long enough to break up into
548 several. Here is some rationale:
550 NEW_PROC_API (Solaris 2.6, Solaris 2.7):
551 There are several file descriptors that may need to be open
552 for any given process or LWP. The ones we're intereted in are:
553 - control (ctl) write-only change the state
554 - status (status) read-only query the state
555 - address space (as) read/write access memory
556 - map (map) read-only virtual addr map
557 Most of these are opened lazily as they are needed.
558 The pathnames for the 'files' for an LWP look slightly
559 different from those of a first-class process:
560 Pathnames for a process (<proc-id>):
562 /proc/<proc-id>/status
565 Pathnames for an LWP (lwp-id):
566 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
567 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
568 An LWP has no map or address space file descriptor, since
569 the memory map and address space are shared by all LWPs.
571 Everyone else (Solaris 2.5, Irix, OSF)
572 There is only one file descriptor for each process or LWP.
573 For convenience, we copy the same file descriptor into all
574 three fields of the procinfo struct (ctl_fd, status_fd, and
575 as_fd, see NEW_PROC_API above) so that code that uses them
576 doesn't need any #ifdef's.
581 Each LWP has an independent file descriptor, but these
582 are not obtained via the 'open' system call like the rest:
583 instead, they're obtained thru an ioctl call (PIOCOPENLWP)
584 to the file descriptor of the parent process.
587 These do not even have their own independent file descriptor.
588 All operations are carried out on the file descriptor of the
589 parent process. Therefore we just call open again for each
590 thread, getting a new handle for the same 'file'. */
593 /* In this case, there are several different file descriptors that
594 we might be asked to open. The control file descriptor will be
595 opened early, but the others will be opened lazily as they are
598 strcpy (tmp
, pi
->pathname
);
599 switch (which
) { /* Which file descriptor to open? */
602 strcat (tmp
, "/lwpctl");
604 strcat (tmp
, "/ctl");
605 fd
= open_with_retry (tmp
, O_WRONLY
);
612 return 0; /* There is no 'as' file descriptor for an lwp. */
614 fd
= open_with_retry (tmp
, O_RDWR
);
621 strcat (tmp
, "/lwpstatus");
623 strcat (tmp
, "/status");
624 fd
= open_with_retry (tmp
, O_RDONLY
);
630 return 0; /* unknown file descriptor */
632 #else /* not NEW_PROC_API */
633 /* In this case, there is only one file descriptor for each procinfo
634 (ie. each process or LWP). In fact, only the file descriptor for
635 the process can actually be opened by an 'open' system call. The
636 ones for the LWPs have to be obtained thru an IOCTL call on the
637 process's file descriptor.
639 For convenience, we copy each procinfo's single file descriptor
640 into all of the fields occupied by the several file descriptors
641 of the NEW_PROC_API implementation. That way, the code that uses
642 them can be written without ifdefs. */
645 #ifdef PIOCTSTATUS /* OSF */
646 /* Only one FD; just open it. */
647 if ((fd
= open_with_retry (pi
->pathname
, O_RDWR
)) < 0)
649 #else /* Sol 2.5, Irix, other? */
650 if (pi
->tid
== 0) /* Master procinfo for the process */
652 fd
= open_with_retry (pi
->pathname
, O_RDWR
);
656 else /* LWP thread procinfo */
658 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
662 /* Find the procinfo for the entire process. */
663 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
666 /* Now obtain the file descriptor for the LWP. */
667 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) < 0)
669 #else /* Irix, other? */
670 return 0; /* Don't know how to open threads. */
671 #endif /* Sol 2.5 PIOCOPENLWP */
673 #endif /* OSF PIOCTSTATUS */
674 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
675 #endif /* NEW_PROC_API */
677 return 1; /* success */
680 /* Allocate a data structure and link it into the procinfo list.
681 First tries to find a pre-existing one (FIXME: why?). Returns the
682 pointer to new procinfo struct. */
685 create_procinfo (int pid
, int tid
)
687 procinfo
*pi
, *parent
= NULL
;
689 if ((pi
= find_procinfo (pid
, tid
)))
690 return pi
; /* Already exists, nothing to do. */
692 /* Find parent before doing malloc, to save having to cleanup. */
694 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
696 doesn't exist yet? */
698 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
699 memset (pi
, 0, sizeof (procinfo
));
703 #ifdef DYNAMIC_SYSCALLS
707 pi
->saved_entryset
= sysset_t_alloc (pi
);
708 pi
->saved_exitset
= sysset_t_alloc (pi
);
710 /* Chain into list. */
713 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
714 pi
->next
= procinfo_list
;
720 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
722 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
724 pi
->next
= parent
->thread_list
;
725 parent
->thread_list
= pi
;
730 /* Close all file descriptors associated with the procinfo. */
733 close_procinfo_files (procinfo
*pi
)
740 if (pi
->status_fd
> 0)
741 close (pi
->status_fd
);
743 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
746 /* Destructor function. Close, unlink and deallocate the object. */
749 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
753 /* Step one: unlink the procinfo from its list. */
757 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
760 ptr
->next
= pi
->next
;
764 /* Step two: close any open file descriptors. */
765 close_procinfo_files (pi
);
767 /* Step three: free the memory. */
768 #ifdef DYNAMIC_SYSCALLS
771 xfree (pi
->saved_entryset
);
772 xfree (pi
->saved_exitset
);
777 destroy_procinfo (procinfo
*pi
)
781 if (pi
->tid
!= 0) /* Destroy a thread procinfo. */
783 tmp
= find_procinfo (pi
->pid
, 0); /* Find the parent process. */
784 destroy_one_procinfo (&tmp
->thread_list
, pi
);
786 else /* Destroy a process procinfo and all its threads. */
788 /* First destroy the children, if any; */
789 while (pi
->thread_list
!= NULL
)
790 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
791 /* Then destroy the parent. Genocide!!! */
792 destroy_one_procinfo (&procinfo_list
, pi
);
797 do_destroy_procinfo_cleanup (void *pi
)
799 destroy_procinfo (pi
);
802 enum { NOKILL
, KILL
};
804 /* To be called on a non_recoverable error for a procinfo. Prints
805 error messages, optionally sends a SIGKILL to the process, then
806 destroys the data structure. */
809 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
815 print_sys_errmsg (pi
->pathname
, errno
);
819 sprintf (procfile
, "process %d", pi
->pid
);
820 print_sys_errmsg (procfile
, errno
);
823 kill (pi
->pid
, SIGKILL
);
825 destroy_procinfo (pi
);
829 /* Returns the (complete) size of a sysset_t struct. Normally, this
830 is just sizeof (sysset_t), but in the case of Monterey/64, the
831 actual size of sysset_t isn't known until runtime. */
834 sysset_t_size (procinfo
* pi
)
836 #ifndef DYNAMIC_SYSCALLS
837 return sizeof (sysset_t
);
839 return sizeof (sysset_t
) - sizeof (uint64_t)
840 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
841 / (8 * sizeof (uint64_t)));
845 /* Allocate and (partially) initialize a sysset_t struct. */
848 sysset_t_alloc (procinfo
* pi
)
851 int size
= sysset_t_size (pi
);
853 ret
= xmalloc (size
);
854 #ifdef DYNAMIC_SYSCALLS
855 ret
->pr_size
= ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
856 / (8 * sizeof (uint64_t)));
861 #ifdef DYNAMIC_SYSCALLS
863 /* Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
864 pi->num_syscalls with the number of syscalls and pi->syscall_names
865 with the names. (Certain numbers may be skipped in which case the
866 names for these numbers will be left as NULL.) */
868 #define MAX_SYSCALL_NAME_LENGTH 256
869 #define MAX_SYSCALLS 65536
872 load_syscalls (procinfo
*pi
)
874 char pathname
[MAX_PROC_NAME_SIZE
];
877 prsyscall_t
*syscalls
;
878 int i
, size
, maxcall
;
879 struct cleanup
*cleanups
;
881 pi
->num_syscalls
= 0;
882 pi
->syscall_names
= 0;
884 /* Open the file descriptor for the sysent file. */
885 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
886 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
889 error (_("load_syscalls: Can't open /proc/%d/sysent"), pi
->pid
);
891 cleanups
= make_cleanup_close (sysent_fd
);
893 size
= sizeof header
- sizeof (prsyscall_t
);
894 if (read (sysent_fd
, &header
, size
) != size
)
896 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi
->pid
);
899 if (header
.pr_nsyscalls
== 0)
901 error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"),
905 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
906 syscalls
= xmalloc (size
);
907 make_cleanup (free_current_contents
, &syscalls
);
909 if (read (sysent_fd
, syscalls
, size
) != size
)
910 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi
->pid
);
912 /* Find maximum syscall number. This may not be the same as
913 pr_nsyscalls since that value refers to the number of entries
914 in the table. (Also, the docs indicate that some system
915 call numbers may be skipped.) */
917 maxcall
= syscalls
[0].pr_number
;
919 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
920 if (syscalls
[i
].pr_number
> maxcall
921 && syscalls
[i
].pr_nameoff
> 0
922 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
923 maxcall
= syscalls
[i
].pr_number
;
925 pi
->num_syscalls
= maxcall
+1;
926 pi
->syscall_names
= xmalloc (pi
->num_syscalls
* sizeof (char *));
928 for (i
= 0; i
< pi
->num_syscalls
; i
++)
929 pi
->syscall_names
[i
] = NULL
;
931 /* Read the syscall names in. */
932 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
934 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
938 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
939 || syscalls
[i
].pr_number
< 0
940 || syscalls
[i
].pr_nameoff
<= 0
941 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
942 != (off_t
) syscalls
[i
].pr_nameoff
))
945 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
949 callnum
= syscalls
[i
].pr_number
;
951 if (pi
->syscall_names
[callnum
] != NULL
)
953 /* FIXME: Generate warning. */
957 namebuf
[nread
-1] = '\0';
958 size
= strlen (namebuf
) + 1;
959 pi
->syscall_names
[callnum
] = xmalloc (size
);
960 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
961 pi
->syscall_names
[callnum
][size
-1] = '\0';
964 do_cleanups (cleanups
);
967 /* Free the space allocated for the syscall names from the procinfo
971 free_syscalls (procinfo
*pi
)
973 if (pi
->syscall_names
)
977 for (i
= 0; i
< pi
->num_syscalls
; i
++)
978 if (pi
->syscall_names
[i
] != NULL
)
979 xfree (pi
->syscall_names
[i
]);
981 xfree (pi
->syscall_names
);
982 pi
->syscall_names
= 0;
986 /* Given a name, look up (and return) the corresponding syscall number.
987 If no match is found, return -1. */
990 find_syscall (procinfo
*pi
, char *name
)
994 for (i
= 0; i
< pi
->num_syscalls
; i
++)
996 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
1003 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1005 /* =================== /proc "MODULE" =================== */
1007 /* This "module" is the interface layer between the /proc system API
1008 and the gdb target vector functions. This layer consists of access
1009 functions that encapsulate each of the basic operations that we
1010 need to use from the /proc API.
1012 The main motivation for this layer is to hide the fact that there
1013 are two very different implementations of the /proc API. Rather
1014 than have a bunch of #ifdefs all thru the gdb target vector
1015 functions, we do our best to hide them all in here. */
1017 static long proc_flags (procinfo
* pi
);
1018 static int proc_why (procinfo
* pi
);
1019 static int proc_what (procinfo
* pi
);
1020 static int proc_set_current_signal (procinfo
* pi
, int signo
);
1021 static int proc_get_current_thread (procinfo
* pi
);
1022 static int proc_iterate_over_threads
1024 int (*func
) (procinfo
*, procinfo
*, void *),
1028 proc_warn (procinfo
*pi
, char *func
, int line
)
1030 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1031 print_sys_errmsg (errmsg
, errno
);
1035 proc_error (procinfo
*pi
, char *func
, int line
)
1037 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1038 perror_with_name (errmsg
);
1041 /* Updates the status struct in the procinfo. There is a 'valid'
1042 flag, to let other functions know when this function needs to be
1043 called (so the status is only read when it is needed). The status
1044 file descriptor is also only opened when it is needed. Returns
1045 non-zero for success, zero for failure. */
1048 proc_get_status (procinfo
*pi
)
1050 /* Status file descriptor is opened "lazily". */
1051 if (pi
->status_fd
== 0 &&
1052 open_procinfo_files (pi
, FD_STATUS
) == 0)
1054 pi
->status_valid
= 0;
1059 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1060 pi
->status_valid
= 0; /* fail */
1063 /* Sigh... I have to read a different data structure,
1064 depending on whether this is a main process or an LWP. */
1066 pi
->status_valid
= (read (pi
->status_fd
,
1067 (char *) &pi
->prstatus
.pr_lwp
,
1068 sizeof (lwpstatus_t
))
1069 == sizeof (lwpstatus_t
));
1072 pi
->status_valid
= (read (pi
->status_fd
,
1073 (char *) &pi
->prstatus
,
1074 sizeof (gdb_prstatus_t
))
1075 == sizeof (gdb_prstatus_t
));
1078 #else /* ioctl method */
1079 #ifdef PIOCTSTATUS /* osf */
1080 if (pi
->tid
== 0) /* main process */
1082 /* Just read the danged status. Now isn't that simple? */
1084 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1091 tid_t pr_error_thread
;
1092 struct prstatus status
;
1095 thread_status
.pr_count
= 1;
1096 thread_status
.status
.pr_tid
= pi
->tid
;
1097 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1100 memcpy (&pi
->prstatus
, &thread_status
.status
,
1101 sizeof (pi
->prstatus
));
1102 pi
->status_valid
= 1;
1106 /* Just read the danged status. Now isn't that simple? */
1107 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1111 if (pi
->status_valid
)
1113 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1116 proc_get_current_thread (pi
));
1119 /* The status struct includes general regs, so mark them valid too. */
1120 pi
->gregs_valid
= pi
->status_valid
;
1122 /* In the read/write multiple-fd model, the status struct includes
1123 the fp regs too, so mark them valid too. */
1124 pi
->fpregs_valid
= pi
->status_valid
;
1126 return pi
->status_valid
; /* True if success, false if failure. */
1129 /* Returns the process flags (pr_flags field). */
1132 proc_flags (procinfo
*pi
)
1134 if (!pi
->status_valid
)
1135 if (!proc_get_status (pi
))
1136 return 0; /* FIXME: not a good failure value (but what is?) */
1139 return pi
->prstatus
.pr_lwp
.pr_flags
;
1141 return pi
->prstatus
.pr_flags
;
1145 /* Returns the pr_why field (why the process stopped). */
1148 proc_why (procinfo
*pi
)
1150 if (!pi
->status_valid
)
1151 if (!proc_get_status (pi
))
1152 return 0; /* FIXME: not a good failure value (but what is?) */
1155 return pi
->prstatus
.pr_lwp
.pr_why
;
1157 return pi
->prstatus
.pr_why
;
1161 /* Returns the pr_what field (details of why the process stopped). */
1164 proc_what (procinfo
*pi
)
1166 if (!pi
->status_valid
)
1167 if (!proc_get_status (pi
))
1168 return 0; /* FIXME: not a good failure value (but what is?) */
1171 return pi
->prstatus
.pr_lwp
.pr_what
;
1173 return pi
->prstatus
.pr_what
;
1177 /* This function is only called when PI is stopped by a watchpoint.
1178 Assuming the OS supports it, write to *ADDR the data address which
1179 triggered it and return 1. Return 0 if it is not possible to know
1183 proc_watchpoint_address (procinfo
*pi
, CORE_ADDR
*addr
)
1185 if (!pi
->status_valid
)
1186 if (!proc_get_status (pi
))
1190 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
1191 builtin_type (target_gdbarch ())->builtin_data_ptr
,
1192 (gdb_byte
*) &pi
->prstatus
.pr_lwp
.pr_info
.si_addr
);
1194 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
1195 builtin_type (target_gdbarch ())->builtin_data_ptr
,
1196 (gdb_byte
*) &pi
->prstatus
.pr_info
.si_addr
);
1201 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1203 /* Returns the pr_nsysarg field (number of args to the current
1207 proc_nsysarg (procinfo
*pi
)
1209 if (!pi
->status_valid
)
1210 if (!proc_get_status (pi
))
1214 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1216 return pi
->prstatus
.pr_nsysarg
;
1220 /* Returns the pr_sysarg field (pointer to the arguments of current
1224 proc_sysargs (procinfo
*pi
)
1226 if (!pi
->status_valid
)
1227 if (!proc_get_status (pi
))
1231 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1233 return (long *) &pi
->prstatus
.pr_sysarg
;
1236 #endif /* PIOCSSPCACT */
1238 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
1239 /* Returns the pr_cursig field (current signal). */
1242 proc_cursig (struct procinfo
*pi
)
1244 if (!pi
->status_valid
)
1245 if (!proc_get_status (pi
))
1246 return 0; /* FIXME: not a good failure value (but what is?) */
1249 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1251 return pi
->prstatus
.pr_cursig
;
1254 #endif /* PROCFS_DONT_PIOCSSIG_CURSIG */
1256 /* === I appologize for the messiness of this function.
1257 === This is an area where the different versions of
1258 === /proc are more inconsistent than usual.
1260 Set or reset any of the following process flags:
1261 PR_FORK -- forked child will inherit trace flags
1262 PR_RLC -- traced process runs when last /proc file closed.
1263 PR_KLC -- traced process is killed when last /proc file closed.
1264 PR_ASYNC -- LWP's get to run/stop independently.
1266 There are three methods for doing this function:
1267 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1269 2) Middle: PIOCSET/PIOCRESET
1271 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1274 Note: Irix does not define PR_ASYNC.
1275 Note: OSF does not define PR_KLC.
1276 Note: OSF is the only one that can ONLY use the oldest method.
1280 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1281 mode -- 1 for set, 0 for reset.
1283 Returns non-zero for success, zero for failure. */
1285 enum { FLAG_RESET
, FLAG_SET
};
1288 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1290 long win
= 0; /* default to fail */
1292 /* These operations affect the process as a whole, and applying them
1293 to an individual LWP has the same meaning as applying them to the
1294 main process. Therefore, if we're ever called with a pointer to
1295 an LWP's procinfo, let's substitute the process's procinfo and
1296 avoid opening the LWP's file descriptor unnecessarily. */
1299 pi
= find_procinfo_or_die (pi
->pid
, 0);
1301 #ifdef NEW_PROC_API /* Newest method: Newer Solarii. */
1302 /* First normalize the PCUNSET/PCRESET command opcode
1303 (which for no obvious reason has a different definition
1304 from one operating system to the next...) */
1306 #define GDBRESET PCUNSET
1309 #define GDBRESET PCRESET
1313 procfs_ctl_t arg
[2];
1315 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC). */
1317 else /* Reset the flag. */
1321 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1324 #ifdef PIOCSET /* Irix/Sol5 method */
1325 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC). */
1327 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1329 else /* Reset the flag. */
1331 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1335 #ifdef PIOCSRLC /* Oldest method: OSF */
1338 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1340 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1342 else /* Clear run-on-last-close */
1344 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1348 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1350 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1352 else /* Clear inherit-on-fork */
1354 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1358 win
= 0; /* Fail -- unknown flag (can't do PR_ASYNC). */
1365 /* The above operation renders the procinfo's cached pstatus
1367 pi
->status_valid
= 0;
1370 warning (_("procfs: modify_flag failed to turn %s %s"),
1371 flag
== PR_FORK
? "PR_FORK" :
1372 flag
== PR_RLC
? "PR_RLC" :
1374 flag
== PR_ASYNC
? "PR_ASYNC" :
1377 flag
== PR_KLC
? "PR_KLC" :
1380 mode
== FLAG_RESET
? "off" : "on");
1385 /* Set the run_on_last_close flag. Process with all threads will
1386 become runnable when debugger closes all /proc fds. Returns
1387 non-zero for success, zero for failure. */
1390 proc_set_run_on_last_close (procinfo
*pi
)
1392 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1395 /* Reset the run_on_last_close flag. The process will NOT become
1396 runnable when debugger closes its file handles. Returns non-zero
1397 for success, zero for failure. */
1400 proc_unset_run_on_last_close (procinfo
*pi
)
1402 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1405 /* Reset inherit_on_fork flag. If the process forks a child while we
1406 are registered for events in the parent, then we will NOT recieve
1407 events from the child. Returns non-zero for success, zero for
1411 proc_unset_inherit_on_fork (procinfo
*pi
)
1413 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1417 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
1418 (signal etc.), the remaining LWPs will continue to run. Returns
1419 non-zero for success, zero for failure. */
1422 proc_set_async (procinfo
*pi
)
1424 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1427 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
1428 (signal etc.), then all other LWPs will stop as well. Returns
1429 non-zero for success, zero for failure. */
1432 proc_unset_async (procinfo
*pi
)
1434 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1436 #endif /* PR_ASYNC */
1438 /* Request the process/LWP to stop. Does not wait. Returns non-zero
1439 for success, zero for failure. */
1442 proc_stop_process (procinfo
*pi
)
1446 /* We might conceivably apply this operation to an LWP, and the
1447 LWP's ctl file descriptor might not be open. */
1449 if (pi
->ctl_fd
== 0 &&
1450 open_procinfo_files (pi
, FD_CTL
) == 0)
1455 procfs_ctl_t cmd
= PCSTOP
;
1457 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1458 #else /* ioctl method */
1459 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1460 /* Note: the call also reads the prstatus. */
1463 pi
->status_valid
= 1;
1464 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1467 proc_get_current_thread (pi
));
1475 /* Wait for the process or LWP to stop (block until it does). Returns
1476 non-zero for success, zero for failure. */
1479 proc_wait_for_stop (procinfo
*pi
)
1483 /* We should never have to apply this operation to any procinfo
1484 except the one for the main process. If that ever changes for
1485 any reason, then take out the following clause and replace it
1486 with one that makes sure the ctl_fd is open. */
1489 pi
= find_procinfo_or_die (pi
->pid
, 0);
1493 procfs_ctl_t cmd
= PCWSTOP
;
1495 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1496 /* We been runnin' and we stopped -- need to update status. */
1497 pi
->status_valid
= 0;
1499 #else /* ioctl method */
1500 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1501 /* Above call also refreshes the prstatus. */
1504 pi
->status_valid
= 1;
1505 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1508 proc_get_current_thread (pi
));
1515 /* Make the process or LWP runnable.
1517 Options (not all are implemented):
1519 - clear current fault
1520 - clear current signal
1521 - abort the current system call
1522 - stop as soon as finished with system call
1523 - (ioctl): set traced signal set
1524 - (ioctl): set held signal set
1525 - (ioctl): set traced fault set
1526 - (ioctl): set start pc (vaddr)
1528 Always clears the current fault. PI is the process or LWP to
1529 operate on. If STEP is true, set the process or LWP to trap after
1530 one instruction. If SIGNO is zero, clear the current signal if
1531 any; if non-zero, set the current signal to this one. Returns
1532 non-zero for success, zero for failure. */
1535 proc_run_process (procinfo
*pi
, int step
, int signo
)
1540 /* We will probably have to apply this operation to individual
1541 threads, so make sure the control file descriptor is open. */
1543 if (pi
->ctl_fd
== 0 &&
1544 open_procinfo_files (pi
, FD_CTL
) == 0)
1549 runflags
= PRCFAULT
; /* Always clear current fault. */
1554 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals. */
1555 proc_set_current_signal (pi
, signo
);
1559 procfs_ctl_t cmd
[2];
1563 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1565 #else /* ioctl method */
1569 memset (&prrun
, 0, sizeof (prrun
));
1570 prrun
.pr_flags
= runflags
;
1571 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1578 /* Register to trace signals in the process or LWP. Returns non-zero
1579 for success, zero for failure. */
1582 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1586 /* We should never have to apply this operation to any procinfo
1587 except the one for the main process. If that ever changes for
1588 any reason, then take out the following clause and replace it
1589 with one that makes sure the ctl_fd is open. */
1592 pi
= find_procinfo_or_die (pi
->pid
, 0);
1598 /* Use char array to avoid alignment issues. */
1599 char sigset
[sizeof (gdb_sigset_t
)];
1603 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1605 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1607 #else /* ioctl method */
1608 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1610 /* The above operation renders the procinfo's cached pstatus obsolete. */
1611 pi
->status_valid
= 0;
1614 warning (_("procfs: set_traced_signals failed"));
1618 /* Register to trace hardware faults in the process or LWP. Returns
1619 non-zero for success, zero for failure. */
1622 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1626 /* We should never have to apply this operation to any procinfo
1627 except the one for the main process. If that ever changes for
1628 any reason, then take out the following clause and replace it
1629 with one that makes sure the ctl_fd is open. */
1632 pi
= find_procinfo_or_die (pi
->pid
, 0);
1638 /* Use char array to avoid alignment issues. */
1639 char fltset
[sizeof (fltset_t
)];
1643 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1645 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1647 #else /* ioctl method */
1648 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1650 /* The above operation renders the procinfo's cached pstatus obsolete. */
1651 pi
->status_valid
= 0;
1656 /* Register to trace entry to system calls in the process or LWP.
1657 Returns non-zero for success, zero for failure. */
1660 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1664 /* We should never have to apply this operation to any procinfo
1665 except the one for the main process. If that ever changes for
1666 any reason, then take out the following clause and replace it
1667 with one that makes sure the ctl_fd is open. */
1670 pi
= find_procinfo_or_die (pi
->pid
, 0);
1674 struct gdb_proc_ctl_pcsentry
{
1676 /* Use char array to avoid alignment issues. */
1677 char sysset
[sizeof (sysset_t
)];
1679 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1681 + sysset_t_size (pi
);
1683 argp
= xmalloc (argp_size
);
1685 argp
->cmd
= PCSENTRY
;
1686 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1688 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1691 #else /* ioctl method */
1692 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1694 /* The above operation renders the procinfo's cached pstatus
1696 pi
->status_valid
= 0;
1701 /* Register to trace exit from system calls in the process or LWP.
1702 Returns non-zero for success, zero for failure. */
1705 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1709 /* We should never have to apply this operation to any procinfo
1710 except the one for the main process. If that ever changes for
1711 any reason, then take out the following clause and replace it
1712 with one that makes sure the ctl_fd is open. */
1715 pi
= find_procinfo_or_die (pi
->pid
, 0);
1719 struct gdb_proc_ctl_pcsexit
{
1721 /* Use char array to avoid alignment issues. */
1722 char sysset
[sizeof (sysset_t
)];
1724 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1726 + sysset_t_size (pi
);
1728 argp
= xmalloc (argp_size
);
1730 argp
->cmd
= PCSEXIT
;
1731 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1733 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1736 #else /* ioctl method */
1737 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1739 /* The above operation renders the procinfo's cached pstatus
1741 pi
->status_valid
= 0;
1746 /* Specify the set of blocked / held signals in the process or LWP.
1747 Returns non-zero for success, zero for failure. */
1750 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1754 /* We should never have to apply this operation to any procinfo
1755 except the one for the main process. If that ever changes for
1756 any reason, then take out the following clause and replace it
1757 with one that makes sure the ctl_fd is open. */
1760 pi
= find_procinfo_or_die (pi
->pid
, 0);
1766 /* Use char array to avoid alignment issues. */
1767 char hold
[sizeof (gdb_sigset_t
)];
1771 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1772 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1775 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1777 /* The above operation renders the procinfo's cached pstatus
1779 pi
->status_valid
= 0;
1784 /* Returns the set of signals that are held / blocked. Will also copy
1785 the sigset if SAVE is non-zero. */
1787 static gdb_sigset_t
*
1788 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
1790 gdb_sigset_t
*ret
= NULL
;
1792 /* We should never have to apply this operation to any procinfo
1793 except the one for the main process. If that ever changes for
1794 any reason, then take out the following clause and replace it
1795 with one that makes sure the ctl_fd is open. */
1798 pi
= find_procinfo_or_die (pi
->pid
, 0);
1801 if (!pi
->status_valid
)
1802 if (!proc_get_status (pi
))
1805 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1806 #else /* not NEW_PROC_API */
1808 static gdb_sigset_t sigheld
;
1810 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
1813 #endif /* NEW_PROC_API */
1815 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
1820 /* Returns the set of signals that are traced / debugged. Will also
1821 copy the sigset if SAVE is non-zero. */
1823 static gdb_sigset_t
*
1824 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
1826 gdb_sigset_t
*ret
= NULL
;
1828 /* We should never have to apply this operation to any procinfo
1829 except the one for the main process. If that ever changes for
1830 any reason, then take out the following clause and replace it
1831 with one that makes sure the ctl_fd is open. */
1834 pi
= find_procinfo_or_die (pi
->pid
, 0);
1837 if (!pi
->status_valid
)
1838 if (!proc_get_status (pi
))
1841 ret
= &pi
->prstatus
.pr_sigtrace
;
1844 static gdb_sigset_t sigtrace
;
1846 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
1851 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
1856 /* Returns the set of hardware faults that are traced /debugged. Will
1857 also copy the faultset if SAVE is non-zero. */
1860 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1862 fltset_t
*ret
= NULL
;
1864 /* We should never have to apply this operation to any procinfo
1865 except the one for the main process. If that ever changes for
1866 any reason, then take out the following clause and replace it
1867 with one that makes sure the ctl_fd is open. */
1870 pi
= find_procinfo_or_die (pi
->pid
, 0);
1873 if (!pi
->status_valid
)
1874 if (!proc_get_status (pi
))
1877 ret
= &pi
->prstatus
.pr_flttrace
;
1880 static fltset_t flttrace
;
1882 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
1887 memcpy (save
, ret
, sizeof (fltset_t
));
1892 /* Returns the set of syscalls that are traced /debugged on entry.
1893 Will also copy the syscall set if SAVE is non-zero. */
1896 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1898 sysset_t
*ret
= NULL
;
1900 /* We should never have to apply this operation to any procinfo
1901 except the one for the main process. If that ever changes for
1902 any reason, then take out the following clause and replace it
1903 with one that makes sure the ctl_fd is open. */
1906 pi
= find_procinfo_or_die (pi
->pid
, 0);
1909 if (!pi
->status_valid
)
1910 if (!proc_get_status (pi
))
1913 #ifndef DYNAMIC_SYSCALLS
1914 ret
= &pi
->prstatus
.pr_sysentry
;
1915 #else /* DYNAMIC_SYSCALLS */
1917 static sysset_t
*sysentry
;
1921 sysentry
= sysset_t_alloc (pi
);
1923 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
1925 if (pi
->prstatus
.pr_sysentry_offset
== 0)
1927 gdb_premptysysset (sysentry
);
1933 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
1935 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
1937 size
= sysset_t_size (pi
);
1938 gdb_premptysysset (sysentry
);
1939 rsize
= read (pi
->status_fd
, sysentry
, size
);
1944 #endif /* DYNAMIC_SYSCALLS */
1945 #else /* !NEW_PROC_API */
1947 static sysset_t sysentry
;
1949 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
1952 #endif /* NEW_PROC_API */
1954 memcpy (save
, ret
, sysset_t_size (pi
));
1959 /* Returns the set of syscalls that are traced /debugged on exit.
1960 Will also copy the syscall set if SAVE is non-zero. */
1963 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1965 sysset_t
* ret
= NULL
;
1967 /* We should never have to apply this operation to any procinfo
1968 except the one for the main process. If that ever changes for
1969 any reason, then take out the following clause and replace it
1970 with one that makes sure the ctl_fd is open. */
1973 pi
= find_procinfo_or_die (pi
->pid
, 0);
1976 if (!pi
->status_valid
)
1977 if (!proc_get_status (pi
))
1980 #ifndef DYNAMIC_SYSCALLS
1981 ret
= &pi
->prstatus
.pr_sysexit
;
1982 #else /* DYNAMIC_SYSCALLS */
1984 static sysset_t
*sysexit
;
1988 sysexit
= sysset_t_alloc (pi
);
1990 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
1992 if (pi
->prstatus
.pr_sysexit_offset
== 0)
1994 gdb_premptysysset (sysexit
);
2000 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
,
2002 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2004 size
= sysset_t_size (pi
);
2005 gdb_premptysysset (sysexit
);
2006 rsize
= read (pi
->status_fd
, sysexit
, size
);
2011 #endif /* DYNAMIC_SYSCALLS */
2014 static sysset_t sysexit
;
2016 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2021 memcpy (save
, ret
, sysset_t_size (pi
));
2026 /* The current fault (if any) is cleared; the associated signal will
2027 not be sent to the process or LWP when it resumes. Returns
2028 non-zero for success, zero for failure. */
2031 proc_clear_current_fault (procinfo
*pi
)
2035 /* We should never have to apply this operation to any procinfo
2036 except the one for the main process. If that ever changes for
2037 any reason, then take out the following clause and replace it
2038 with one that makes sure the ctl_fd is open. */
2041 pi
= find_procinfo_or_die (pi
->pid
, 0);
2045 procfs_ctl_t cmd
= PCCFAULT
;
2047 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2050 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2056 /* Set the "current signal" that will be delivered next to the
2057 process. NOTE: semantics are different from those of KILL. This
2058 signal will be delivered to the process or LWP immediately when it
2059 is resumed (even if the signal is held/blocked); it will NOT
2060 immediately cause another event of interest, and will NOT first
2061 trap back to the debugger. Returns non-zero for success, zero for
2065 proc_set_current_signal (procinfo
*pi
, int signo
)
2070 /* Use char array to avoid alignment issues. */
2071 char sinfo
[sizeof (gdb_siginfo_t
)];
2073 gdb_siginfo_t mysinfo
;
2075 struct target_waitstatus wait_status
;
2077 /* We should never have to apply this operation to any procinfo
2078 except the one for the main process. If that ever changes for
2079 any reason, then take out the following clause and replace it
2080 with one that makes sure the ctl_fd is open. */
2083 pi
= find_procinfo_or_die (pi
->pid
, 0);
2085 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2086 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2087 receives a PIOCSSIG with a signal identical to the current
2088 signal, it messes up the current signal. Work around the kernel
2091 signo
== proc_cursig (pi
))
2092 return 1; /* I assume this is a success? */
2095 /* The pointer is just a type alias. */
2096 get_last_target_status (&wait_ptid
, &wait_status
);
2097 if (ptid_equal (wait_ptid
, inferior_ptid
)
2098 && wait_status
.kind
== TARGET_WAITKIND_STOPPED
2099 && wait_status
.value
.sig
== gdb_signal_from_host (signo
)
2100 && proc_get_status (pi
)
2102 && pi
->prstatus
.pr_lwp
.pr_info
.si_signo
== signo
2104 && pi
->prstatus
.pr_info
.si_signo
== signo
2107 /* Use the siginfo associated with the signal being
2110 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_lwp
.pr_info
, sizeof (gdb_siginfo_t
));
2112 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_info
, sizeof (gdb_siginfo_t
));
2116 mysinfo
.si_signo
= signo
;
2117 mysinfo
.si_code
= 0;
2118 mysinfo
.si_pid
= getpid (); /* ?why? */
2119 mysinfo
.si_uid
= getuid (); /* ?why? */
2120 memcpy (arg
.sinfo
, &mysinfo
, sizeof (gdb_siginfo_t
));
2125 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2127 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2133 /* The current signal (if any) is cleared, and is not sent to the
2134 process or LWP when it resumes. Returns non-zero for success, zero
2138 proc_clear_current_signal (procinfo
*pi
)
2142 /* We should never have to apply this operation to any procinfo
2143 except the one for the main process. If that ever changes for
2144 any reason, then take out the following clause and replace it
2145 with one that makes sure the ctl_fd is open. */
2148 pi
= find_procinfo_or_die (pi
->pid
, 0);
2154 /* Use char array to avoid alignment issues. */
2155 char sinfo
[sizeof (gdb_siginfo_t
)];
2157 gdb_siginfo_t mysinfo
;
2160 /* The pointer is just a type alias. */
2161 mysinfo
.si_signo
= 0;
2162 mysinfo
.si_code
= 0;
2163 mysinfo
.si_errno
= 0;
2164 mysinfo
.si_pid
= getpid (); /* ?why? */
2165 mysinfo
.si_uid
= getuid (); /* ?why? */
2166 memcpy (arg
.sinfo
, &mysinfo
, sizeof (gdb_siginfo_t
));
2168 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2171 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2177 /* Return the general-purpose registers for the process or LWP
2178 corresponding to PI. Upon failure, return NULL. */
2180 static gdb_gregset_t
*
2181 proc_get_gregs (procinfo
*pi
)
2183 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2184 if (!proc_get_status (pi
))
2188 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2190 return &pi
->prstatus
.pr_reg
;
2194 /* Return the general-purpose registers for the process or LWP
2195 corresponding to PI. Upon failure, return NULL. */
2197 static gdb_fpregset_t
*
2198 proc_get_fpregs (procinfo
*pi
)
2201 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2202 if (!proc_get_status (pi
))
2205 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2207 #else /* not NEW_PROC_API */
2208 if (pi
->fpregs_valid
)
2209 return &pi
->fpregset
; /* Already got 'em. */
2212 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2221 tid_t pr_error_thread
;
2222 tfpregset_t thread_1
;
2225 thread_fpregs
.pr_count
= 1;
2226 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2229 && ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2231 pi
->fpregs_valid
= 1;
2232 return &pi
->fpregset
; /* Got 'em now! */
2234 else if (pi
->tid
!= 0
2235 && ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2237 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2238 sizeof (pi
->fpregset
));
2239 pi
->fpregs_valid
= 1;
2240 return &pi
->fpregset
; /* Got 'em now! */
2247 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2249 pi
->fpregs_valid
= 1;
2250 return &pi
->fpregset
; /* Got 'em now! */
2259 #endif /* NEW_PROC_API */
2262 /* Write the general-purpose registers back to the process or LWP
2263 corresponding to PI. Return non-zero for success, zero for
2267 proc_set_gregs (procinfo
*pi
)
2269 gdb_gregset_t
*gregs
;
2272 gregs
= proc_get_gregs (pi
);
2274 return 0; /* proc_get_regs has already warned. */
2276 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2285 /* Use char array to avoid alignment issues. */
2286 char gregs
[sizeof (gdb_gregset_t
)];
2290 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2291 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2293 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2297 /* Policy: writing the registers invalidates our cache. */
2298 pi
->gregs_valid
= 0;
2302 /* Write the floating-pointer registers back to the process or LWP
2303 corresponding to PI. Return non-zero for success, zero for
2307 proc_set_fpregs (procinfo
*pi
)
2309 gdb_fpregset_t
*fpregs
;
2312 fpregs
= proc_get_fpregs (pi
);
2314 return 0; /* proc_get_fpregs has already warned. */
2316 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2325 /* Use char array to avoid alignment issues. */
2326 char fpregs
[sizeof (gdb_fpregset_t
)];
2330 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2331 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2335 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2340 tid_t pr_error_thread
;
2341 tfpregset_t thread_1
;
2344 thread_fpregs
.pr_count
= 1;
2345 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2346 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2348 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2351 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2353 #endif /* NEW_PROC_API */
2356 /* Policy: writing the registers invalidates our cache. */
2357 pi
->fpregs_valid
= 0;
2361 /* Send a signal to the proc or lwp with the semantics of "kill()".
2362 Returns non-zero for success, zero for failure. */
2365 proc_kill (procinfo
*pi
, int signo
)
2369 /* We might conceivably apply this operation to an LWP, and the
2370 LWP's ctl file descriptor might not be open. */
2372 if (pi
->ctl_fd
== 0 &&
2373 open_procinfo_files (pi
, FD_CTL
) == 0)
2380 procfs_ctl_t cmd
[2];
2384 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2385 #else /* ioctl method */
2386 /* FIXME: do I need the Alpha OSF fixups present in
2387 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2388 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2395 /* Find the pid of the process that started this one. Returns the
2396 parent process pid, or zero. */
2399 proc_parent_pid (procinfo
*pi
)
2401 /* We should never have to apply this operation to any procinfo
2402 except the one for the main process. If that ever changes for
2403 any reason, then take out the following clause and replace it
2404 with one that makes sure the ctl_fd is open. */
2407 pi
= find_procinfo_or_die (pi
->pid
, 0);
2409 if (!pi
->status_valid
)
2410 if (!proc_get_status (pi
))
2413 return pi
->prstatus
.pr_ppid
;
2416 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2417 (a.k.a void pointer)! */
2419 #if (defined (PCWATCH) || defined (PIOCSWATCH)) \
2420 && !(defined (PIOCOPENLWP))
2422 procfs_address_to_host_pointer (CORE_ADDR addr
)
2424 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
2427 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (ptr_type
));
2428 gdbarch_address_to_pointer (target_gdbarch (), ptr_type
,
2429 (gdb_byte
*) &ptr
, addr
);
2435 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2437 #if !defined (PCWATCH) && !defined (PIOCSWATCH)
2438 /* If neither or these is defined, we can't support watchpoints.
2439 This just avoids possibly failing to compile the below on such
2443 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5. */
2444 #if defined (PIOCOPENLWP) /* Solaris 2.5: bail out. */
2449 char watch
[sizeof (prwatch_t
)];
2453 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2454 convert a target address into something that can be stored in a
2455 native data structure. */
2456 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2457 pwatch
.pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
2459 pwatch
.pr_vaddr
= (caddr_t
) procfs_address_to_host_pointer (addr
);
2461 pwatch
.pr_size
= len
;
2462 pwatch
.pr_wflags
= wflags
;
2463 #if defined(NEW_PROC_API) && defined (PCWATCH)
2465 memcpy (arg
.watch
, &pwatch
, sizeof (prwatch_t
));
2466 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2468 #if defined (PIOCSWATCH)
2469 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, &pwatch
) >= 0);
2471 return 0; /* Fail */
2478 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
2480 #include <sys/sysi86.h>
2482 /* The KEY is actually the value of the lower 16 bits of the GS
2483 register for the LWP that we're interested in. Returns the
2484 matching ssh struct (LDT entry). */
2487 proc_get_LDT_entry (procinfo
*pi
, int key
)
2489 static struct ssd
*ldt_entry
= NULL
;
2491 char pathname
[MAX_PROC_NAME_SIZE
];
2492 struct cleanup
*old_chain
= NULL
;
2495 /* Allocate space for one LDT entry.
2496 This alloc must persist, because we return a pointer to it. */
2497 if (ldt_entry
== NULL
)
2498 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2500 /* Open the file descriptor for the LDT table. */
2501 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2502 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2504 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2507 /* Make sure it gets closed again! */
2508 old_chain
= make_cleanup_close (fd
);
2510 /* Now 'read' thru the table, find a match and return it. */
2511 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2513 if (ldt_entry
->sel
== 0 &&
2514 ldt_entry
->bo
== 0 &&
2515 ldt_entry
->acc1
== 0 &&
2516 ldt_entry
->acc2
== 0)
2517 break; /* end of table */
2518 /* If key matches, return this entry. */
2519 if (ldt_entry
->sel
== key
)
2522 /* Loop ended, match not found. */
2526 static int nalloc
= 0;
2528 /* Get the number of LDT entries. */
2529 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2531 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2535 /* Allocate space for the number of LDT entries. */
2536 /* This alloc has to persist, 'cause we return a pointer to it. */
2539 ldt_entry
= (struct ssd
*)
2540 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2544 /* Read the whole table in one gulp. */
2545 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2547 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2551 /* Search the table and return the (first) entry matching 'key'. */
2552 for (i
= 0; i
< nldt
; i
++)
2553 if (ldt_entry
[i
].sel
== key
)
2554 return &ldt_entry
[i
];
2556 /* Loop ended, match not found. */
2561 /* Returns the pointer to the LDT entry of PTID. */
2564 procfs_find_LDT_entry (ptid_t ptid
)
2566 gdb_gregset_t
*gregs
;
2570 /* Find procinfo for the lwp. */
2571 if ((pi
= find_procinfo (ptid_get_pid (ptid
), ptid_get_lwp (ptid
))) == NULL
)
2573 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
2574 ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
2577 /* get its general registers. */
2578 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2580 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
2581 ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
2584 /* Now extract the GS register's lower 16 bits. */
2585 key
= (*gregs
)[GS
] & 0xffff;
2587 /* Find the matching entry and return it. */
2588 return proc_get_LDT_entry (pi
, key
);
2593 /* =============== END, non-thread part of /proc "MODULE" =============== */
2595 /* =================== Thread "MODULE" =================== */
2597 /* NOTE: you'll see more ifdefs and duplication of functions here,
2598 since there is a different way to do threads on every OS. */
2600 /* Returns the number of threads for the process. */
2602 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2605 proc_get_nthreads (procinfo
*pi
)
2609 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
2610 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
2616 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2617 /* Solaris version */
2619 proc_get_nthreads (procinfo
*pi
)
2621 if (!pi
->status_valid
)
2622 if (!proc_get_status (pi
))
2625 /* NEW_PROC_API: only works for the process procinfo, because the
2626 LWP procinfos do not get prstatus filled in. */
2628 if (pi
->tid
!= 0) /* Find the parent process procinfo. */
2629 pi
= find_procinfo_or_die (pi
->pid
, 0);
2631 return pi
->prstatus
.pr_nlwp
;
2635 /* Default version */
2637 proc_get_nthreads (procinfo
*pi
)
2646 Return the ID of the thread that had an event of interest.
2647 (ie. the one that hit a breakpoint or other traced event). All
2648 other things being equal, this should be the ID of a thread that is
2649 currently executing. */
2651 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2652 /* Solaris version */
2654 proc_get_current_thread (procinfo
*pi
)
2656 /* Note: this should be applied to the root procinfo for the
2657 process, not to the procinfo for an LWP. If applied to the
2658 procinfo for an LWP, it will simply return that LWP's ID. In
2659 that case, find the parent process procinfo. */
2662 pi
= find_procinfo_or_die (pi
->pid
, 0);
2664 if (!pi
->status_valid
)
2665 if (!proc_get_status (pi
))
2669 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
2671 return pi
->prstatus
.pr_who
;
2676 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2679 proc_get_current_thread (procinfo
*pi
)
2681 #if 0 /* FIXME: not ready for prime time? */
2682 return pi
->prstatus
.pr_tid
;
2689 /* Default version */
2691 proc_get_current_thread (procinfo
*pi
)
2699 /* Discover the IDs of all the threads within the process, and create
2700 a procinfo for each of them (chained to the parent). This
2701 unfortunately requires a different method on every OS. Returns
2702 non-zero for success, zero for failure. */
2705 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
2707 if (thread
&& parent
) /* sanity */
2709 thread
->status_valid
= 0;
2710 if (!proc_get_status (thread
))
2711 destroy_one_procinfo (&parent
->thread_list
, thread
);
2713 return 0; /* keep iterating */
2716 #if defined (PIOCLSTATUS)
2717 /* Solaris 2.5 (ioctl) version */
2719 proc_update_threads (procinfo
*pi
)
2721 gdb_prstatus_t
*prstatus
;
2722 struct cleanup
*old_chain
= NULL
;
2726 /* We should never have to apply this operation to any procinfo
2727 except the one for the main process. If that ever changes for
2728 any reason, then take out the following clause and replace it
2729 with one that makes sure the ctl_fd is open. */
2732 pi
= find_procinfo_or_die (pi
->pid
, 0);
2734 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2736 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
2737 return 1; /* Process is not multi-threaded; nothing to do. */
2739 prstatus
= xmalloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1));
2741 old_chain
= make_cleanup (xfree
, prstatus
);
2742 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
2743 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
2745 /* Skip element zero, which represents the process as a whole. */
2746 for (i
= 1; i
< nlwp
+ 1; i
++)
2748 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
2749 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2751 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
2752 thread
->status_valid
= 1;
2754 pi
->threads_valid
= 1;
2755 do_cleanups (old_chain
);
2760 /* Solaris 6 (and later) version. */
2762 do_closedir_cleanup (void *dir
)
2768 proc_update_threads (procinfo
*pi
)
2770 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
2771 struct dirent
*direntry
;
2772 struct cleanup
*old_chain
= NULL
;
2777 /* We should never have to apply this operation to any procinfo
2778 except the one for the main process. If that ever changes for
2779 any reason, then take out the following clause and replace it
2780 with one that makes sure the ctl_fd is open. */
2783 pi
= find_procinfo_or_die (pi
->pid
, 0);
2785 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2787 /* Note: this brute-force method was originally devised for Unixware
2788 (support removed since), and will also work on Solaris 2.6 and
2789 2.7. The original comment mentioned the existence of a much
2790 simpler and more elegant way to do this on Solaris, but didn't
2791 point out what that was. */
2793 strcpy (pathname
, pi
->pathname
);
2794 strcat (pathname
, "/lwp");
2795 if ((dirp
= opendir (pathname
)) == NULL
)
2796 proc_error (pi
, "update_threads, opendir", __LINE__
);
2798 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
2799 while ((direntry
= readdir (dirp
)) != NULL
)
2800 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
2802 lwpid
= atoi (&direntry
->d_name
[0]);
2803 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
2804 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2806 pi
->threads_valid
= 1;
2807 do_cleanups (old_chain
);
2814 proc_update_threads (procinfo
*pi
)
2819 /* We should never have to apply this operation to any procinfo
2820 except the one for the main process. If that ever changes for
2821 any reason, then take out the following clause and replace it
2822 with one that makes sure the ctl_fd is open. */
2825 pi
= find_procinfo_or_die (pi
->pid
, 0);
2827 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2829 nthreads
= proc_get_nthreads (pi
);
2831 return 0; /* Nothing to do for 1 or fewer threads. */
2833 threads
= xmalloc (nthreads
* sizeof (tid_t
));
2835 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
2836 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
2838 for (i
= 0; i
< nthreads
; i
++)
2840 if (!find_procinfo (pi
->pid
, threads
[i
]))
2841 if (!create_procinfo (pi
->pid
, threads
[i
]))
2842 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2844 pi
->threads_valid
= 1;
2848 /* Default version */
2850 proc_update_threads (procinfo
*pi
)
2854 #endif /* OSF PIOCTLIST */
2855 #endif /* NEW_PROC_API */
2856 #endif /* SOL 2.5 PIOCLSTATUS */
2858 /* Given a pointer to a function, call that function once for each lwp
2859 in the procinfo list, until the function returns non-zero, in which
2860 event return the value returned by the function.
2862 Note: this function does NOT call update_threads. If you want to
2863 discover new threads first, you must call that function explicitly.
2864 This function just makes a quick pass over the currently-known
2867 PI is the parent process procinfo. FUNC is the per-thread
2868 function. PTR is an opaque parameter for function. Returns the
2869 first non-zero return value from the callee, or zero. */
2872 proc_iterate_over_threads (procinfo
*pi
,
2873 int (*func
) (procinfo
*, procinfo
*, void *),
2876 procinfo
*thread
, *next
;
2879 /* We should never have to apply this operation to any procinfo
2880 except the one for the main process. If that ever changes for
2881 any reason, then take out the following clause and replace it
2882 with one that makes sure the ctl_fd is open. */
2885 pi
= find_procinfo_or_die (pi
->pid
, 0);
2887 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
2889 next
= thread
->next
; /* In case thread is destroyed. */
2890 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
2897 /* =================== END, Thread "MODULE" =================== */
2899 /* =================== END, /proc "MODULE" =================== */
2901 /* =================== GDB "MODULE" =================== */
2903 /* Here are all of the gdb target vector functions and their
2906 static ptid_t
do_attach (ptid_t ptid
);
2907 static void do_detach (int signo
);
2908 static void proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
,
2909 int entry_or_exit
, int mode
, int from_tty
);
2911 /* On mips-irix, we need to insert a breakpoint at __dbx_link during
2912 the startup phase. The following two variables are used to record
2913 the address of the breakpoint, and the code that was replaced by
2915 static int dbx_link_bpt_addr
= 0;
2916 static void *dbx_link_bpt
;
2918 /* Sets up the inferior to be debugged. Registers to trace signals,
2919 hardware faults, and syscalls. Note: does not set RLC flag: caller
2920 may want to customize that. Returns zero for success (note!
2921 unlike most functions in this module); on failure, returns the LINE
2922 NUMBER where it failed! */
2925 procfs_debug_inferior (procinfo
*pi
)
2927 fltset_t traced_faults
;
2928 gdb_sigset_t traced_signals
;
2929 sysset_t
*traced_syscall_entries
;
2930 sysset_t
*traced_syscall_exits
;
2933 #ifdef PROCFS_DONT_TRACE_FAULTS
2934 /* On some systems (OSF), we don't trace hardware faults.
2935 Apparently it's enough that we catch them as signals.
2936 Wonder why we don't just do that in general? */
2937 premptyset (&traced_faults
); /* don't trace faults. */
2939 /* Register to trace hardware faults in the child. */
2940 prfillset (&traced_faults
); /* trace all faults... */
2941 gdb_prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
2943 if (!proc_set_traced_faults (pi
, &traced_faults
))
2946 /* Initially, register to trace all signals in the child. */
2947 prfillset (&traced_signals
);
2948 if (!proc_set_traced_signals (pi
, &traced_signals
))
2952 /* Register to trace the 'exit' system call (on entry). */
2953 traced_syscall_entries
= sysset_t_alloc (pi
);
2954 gdb_premptysysset (traced_syscall_entries
);
2956 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
2959 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
);/* And _lwp_exit... */
2962 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
2964 #ifdef DYNAMIC_SYSCALLS
2966 int callnum
= find_syscall (pi
, "_exit");
2969 gdb_praddsysset (traced_syscall_entries
, callnum
);
2973 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
2974 xfree (traced_syscall_entries
);
2978 #ifdef PRFS_STOPEXEC /* defined on OSF */
2979 /* OSF method for tracing exec syscalls. Quoting:
2980 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
2981 exits from exec system calls because of the user level loader. */
2982 /* FIXME: make nice and maybe move into an access function. */
2986 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
2989 prfs_flags
|= PRFS_STOPEXEC
;
2991 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
2994 #else /* not PRFS_STOPEXEC */
2995 /* Everyone else's (except OSF) method for tracing exec syscalls. */
2997 Not all systems with /proc have all the exec* syscalls with the same
2998 names. On the SGI, for example, there is no SYS_exec, but there
2999 *is* a SYS_execv. So, we try to account for that. */
3001 traced_syscall_exits
= sysset_t_alloc (pi
);
3002 gdb_premptysysset (traced_syscall_exits
);
3004 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
3007 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
3010 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
3013 #ifdef SYS_lwpcreate
3014 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3015 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3018 #ifdef SYS_lwp_create /* FIXME: once only, please. */
3019 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3020 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3023 #ifdef DYNAMIC_SYSCALLS
3025 int callnum
= find_syscall (pi
, "execve");
3028 gdb_praddsysset (traced_syscall_exits
, callnum
);
3029 callnum
= find_syscall (pi
, "ra_execve");
3031 gdb_praddsysset (traced_syscall_exits
, callnum
);
3035 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3036 xfree (traced_syscall_exits
);
3040 #endif /* PRFS_STOPEXEC */
3045 procfs_attach (struct target_ops
*ops
, char *args
, int from_tty
)
3050 pid
= parse_pid_to_attach (args
);
3052 if (pid
== getpid ())
3053 error (_("Attaching GDB to itself is not a good idea..."));
3057 exec_file
= get_exec_file (0);
3060 printf_filtered (_("Attaching to program `%s', %s\n"),
3061 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3063 printf_filtered (_("Attaching to %s\n"),
3064 target_pid_to_str (pid_to_ptid (pid
)));
3068 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3073 procfs_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
3076 int pid
= ptid_get_pid (inferior_ptid
);
3085 exec_file
= get_exec_file (0);
3086 if (exec_file
== NULL
)
3089 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file
,
3090 target_pid_to_str (pid_to_ptid (pid
)));
3091 gdb_flush (gdb_stdout
);
3096 inferior_ptid
= null_ptid
;
3097 detach_inferior (pid
);
3098 unpush_target (ops
);
3102 do_attach (ptid_t ptid
)
3105 struct inferior
*inf
;
3109 if ((pi
= create_procinfo (ptid_get_pid (ptid
), 0)) == NULL
)
3110 perror (_("procfs: out of memory in 'attach'"));
3112 if (!open_procinfo_files (pi
, FD_CTL
))
3114 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3115 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3116 ptid_get_pid (ptid
));
3117 dead_procinfo (pi
, errmsg
, NOKILL
);
3120 /* Stop the process (if it isn't already stopped). */
3121 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3123 pi
->was_stopped
= 1;
3124 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3128 pi
->was_stopped
= 0;
3129 /* Set the process to run again when we close it. */
3130 if (!proc_set_run_on_last_close (pi
))
3131 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3133 /* Now stop the process. */
3134 if (!proc_stop_process (pi
))
3135 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3136 pi
->ignore_next_sigstop
= 1;
3138 /* Save some of the /proc state to be restored if we detach. */
3139 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3140 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3141 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3142 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3143 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3144 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3146 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3147 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3149 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3150 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3152 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3153 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3155 inf
= current_inferior ();
3156 inferior_appeared (inf
, pi
->pid
);
3157 /* Let GDB know that the inferior was attached. */
3158 inf
->attach_flag
= 1;
3160 /* Create a procinfo for the current lwp. */
3161 lwpid
= proc_get_current_thread (pi
);
3162 create_procinfo (pi
->pid
, lwpid
);
3164 /* Add it to gdb's thread list. */
3165 ptid
= ptid_build (pi
->pid
, lwpid
, 0);
3172 do_detach (int signo
)
3176 /* Find procinfo for the main process. */
3177 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
),
3178 0); /* FIXME: threads */
3180 if (!proc_set_current_signal (pi
, signo
))
3181 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3183 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3184 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3186 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3187 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3189 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3190 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3192 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3193 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3195 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3196 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3198 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3199 if (signo
|| !(pi
->was_stopped
) ||
3200 query (_("Was stopped when attached, make it runnable again? ")))
3202 /* Clear any pending signal. */
3203 if (!proc_clear_current_fault (pi
))
3204 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3206 if (signo
== 0 && !proc_clear_current_signal (pi
))
3207 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
3209 if (!proc_set_run_on_last_close (pi
))
3210 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3213 destroy_procinfo (pi
);
3216 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
3219 ??? Is the following note still relevant? We can't get individual
3220 registers with the PT_GETREGS ptrace(2) request either, yet we
3221 don't bother with caching at all in that case.
3223 NOTE: Since the /proc interface cannot give us individual
3224 registers, we pay no attention to REGNUM, and just fetch them all.
3225 This results in the possibility that we will do unnecessarily many
3226 fetches, since we may be called repeatedly for individual
3227 registers. So we cache the results, and mark the cache invalid
3228 when the process is resumed. */
3231 procfs_fetch_registers (struct target_ops
*ops
,
3232 struct regcache
*regcache
, int regnum
)
3234 gdb_gregset_t
*gregs
;
3236 int pid
= ptid_get_pid (inferior_ptid
);
3237 int tid
= ptid_get_lwp (inferior_ptid
);
3238 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3240 pi
= find_procinfo_or_die (pid
, tid
);
3243 error (_("procfs: fetch_registers failed to find procinfo for %s"),
3244 target_pid_to_str (inferior_ptid
));
3246 gregs
= proc_get_gregs (pi
);
3248 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3250 supply_gregset (regcache
, (const gdb_gregset_t
*) gregs
);
3252 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
3254 gdb_fpregset_t
*fpregs
;
3256 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
3257 || regnum
== gdbarch_pc_regnum (gdbarch
)
3258 || regnum
== gdbarch_sp_regnum (gdbarch
))
3259 return; /* Not a floating point register. */
3261 fpregs
= proc_get_fpregs (pi
);
3263 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3265 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregs
);
3269 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
3270 this for all registers.
3272 NOTE: Since the /proc interface will not read individual registers,
3273 we will cache these requests until the process is resumed, and only
3274 then write them back to the inferior process.
3276 FIXME: is that a really bad idea? Have to think about cases where
3277 writing one register might affect the value of others, etc. */
3280 procfs_store_registers (struct target_ops
*ops
,
3281 struct regcache
*regcache
, int regnum
)
3283 gdb_gregset_t
*gregs
;
3285 int pid
= ptid_get_pid (inferior_ptid
);
3286 int tid
= ptid_get_lwp (inferior_ptid
);
3287 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3289 pi
= find_procinfo_or_die (pid
, tid
);
3292 error (_("procfs: store_registers: failed to find procinfo for %s"),
3293 target_pid_to_str (inferior_ptid
));
3295 gregs
= proc_get_gregs (pi
);
3297 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3299 fill_gregset (regcache
, gregs
, regnum
);
3300 if (!proc_set_gregs (pi
))
3301 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3303 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
3305 gdb_fpregset_t
*fpregs
;
3307 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
3308 || regnum
== gdbarch_pc_regnum (gdbarch
)
3309 || regnum
== gdbarch_sp_regnum (gdbarch
))
3310 return; /* Not a floating point register. */
3312 fpregs
= proc_get_fpregs (pi
);
3314 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3316 fill_fpregset (regcache
, fpregs
, regnum
);
3317 if (!proc_set_fpregs (pi
))
3318 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3323 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3326 if (scall
== SYS_lwp_exit
)
3330 if (scall
== SYS_lwpexit
)
3337 syscall_is_exit (procinfo
*pi
, int scall
)
3340 if (scall
== SYS_exit
)
3343 #ifdef DYNAMIC_SYSCALLS
3344 if (find_syscall (pi
, "_exit") == scall
)
3351 syscall_is_exec (procinfo
*pi
, int scall
)
3354 if (scall
== SYS_exec
)
3358 if (scall
== SYS_execv
)
3362 if (scall
== SYS_execve
)
3365 #ifdef DYNAMIC_SYSCALLS
3366 if (find_syscall (pi
, "_execve"))
3368 if (find_syscall (pi
, "ra_execve"))
3375 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3377 #ifdef SYS_lwp_create
3378 if (scall
== SYS_lwp_create
)
3381 #ifdef SYS_lwpcreate
3382 if (scall
== SYS_lwpcreate
)
3388 /* Remove the breakpoint that we inserted in __dbx_link().
3389 Does nothing if the breakpoint hasn't been inserted or has already
3393 remove_dbx_link_breakpoint (void)
3395 if (dbx_link_bpt_addr
== 0)
3398 if (deprecated_remove_raw_breakpoint (target_gdbarch (), dbx_link_bpt
) != 0)
3399 warning (_("Unable to remove __dbx_link breakpoint."));
3401 dbx_link_bpt_addr
= 0;
3402 dbx_link_bpt
= NULL
;
3406 /* Return the address of the __dbx_link() function in the file
3407 refernced by ABFD by scanning its symbol table. Return 0 if
3408 the symbol was not found. */
3411 dbx_link_addr (bfd
*abfd
)
3413 long storage_needed
;
3414 asymbol
**symbol_table
;
3415 long number_of_symbols
;
3418 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
3419 if (storage_needed
<= 0)
3422 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
3423 make_cleanup (xfree
, symbol_table
);
3425 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
3427 for (i
= 0; i
< number_of_symbols
; i
++)
3429 asymbol
*sym
= symbol_table
[i
];
3431 if ((sym
->flags
& BSF_GLOBAL
)
3432 && sym
->name
!= NULL
&& strcmp (sym
->name
, "__dbx_link") == 0)
3433 return (sym
->value
+ sym
->section
->vma
);
3436 /* Symbol not found, return NULL. */
3440 /* Search the symbol table of the file referenced by FD for a symbol
3441 named __dbx_link(). If found, then insert a breakpoint at this location,
3442 and return nonzero. Return zero otherwise. */
3445 insert_dbx_link_bpt_in_file (int fd
, CORE_ADDR ignored
)
3448 long storage_needed
;
3451 abfd
= gdb_bfd_fdopenr ("unamed", 0, fd
);
3454 warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
3458 if (!bfd_check_format (abfd
, bfd_object
))
3460 /* Not the correct format, so we can not possibly find the dbx_link
3462 gdb_bfd_unref (abfd
);
3466 sym_addr
= dbx_link_addr (abfd
);
3469 /* Insert the breakpoint. */
3470 dbx_link_bpt_addr
= sym_addr
;
3471 dbx_link_bpt
= deprecated_insert_raw_breakpoint (target_gdbarch (), NULL
,
3473 if (dbx_link_bpt
== NULL
)
3475 warning (_("Failed to insert dbx_link breakpoint."));
3476 gdb_bfd_unref (abfd
);
3479 gdb_bfd_unref (abfd
);
3483 gdb_bfd_unref (abfd
);
3487 /* Calls the supplied callback function once for each mapped address
3488 space in the process. The callback function receives an open file
3489 descriptor for the file corresponding to that mapped address space
3490 (if there is one), and the base address of the mapped space. Quit
3491 when the callback function returns a nonzero value, or at teh end
3492 of the mappings. Returns the first non-zero return value of the
3493 callback function, or zero. */
3496 solib_mappings_callback (struct prmap
*map
, int (*func
) (int, CORE_ADDR
),
3499 procinfo
*pi
= data
;
3503 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
3505 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
3506 return -1; /* sanity */
3508 if (map
->pr_mapname
[0] == 0)
3510 fd
= -1; /* no map file */
3514 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
3515 /* Note: caller's responsibility to close this fd! */
3516 fd
= open_with_retry (name
, O_RDONLY
);
3517 /* Note: we don't test the above call for failure;
3518 we just pass the FD on as given. Sometimes there is
3519 no file, so the open may return failure, but that's
3523 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
->pr_vaddr
);
3524 /* Note: we don't test the above call for failure;
3525 we just pass the FD on as given. Sometimes there is
3526 no file, so the ioctl may return failure, but that's
3529 return (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
);
3532 /* If the given memory region MAP contains a symbol named __dbx_link,
3533 insert a breakpoint at this location and return nonzero. Return
3537 insert_dbx_link_bpt_in_region (struct prmap
*map
,
3538 find_memory_region_ftype child_func
,
3541 procinfo
*pi
= (procinfo
*) data
;
3543 /* We know the symbol we're looking for is in a text region, so
3544 only look for it if the region is a text one. */
3545 if (map
->pr_mflags
& MA_EXEC
)
3546 return solib_mappings_callback (map
, insert_dbx_link_bpt_in_file
, pi
);
3551 /* Search all memory regions for a symbol named __dbx_link. If found,
3552 insert a breakpoint at its location, and return nonzero. Return zero
3556 insert_dbx_link_breakpoint (procinfo
*pi
)
3558 return iterate_over_mappings (pi
, NULL
, pi
, insert_dbx_link_bpt_in_region
);
3562 /* Retrieve the next stop event from the child process. If child has
3563 not stopped yet, wait for it to stop. Translate /proc eventcodes
3564 (or possibly wait eventcodes) into gdb internal event codes.
3565 Returns the id of process (and possibly thread) that incurred the
3566 event. Event codes are returned through a pointer parameter. */
3569 procfs_wait (struct target_ops
*ops
,
3570 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
3572 /* First cut: loosely based on original version 2.1. */
3576 ptid_t retval
, temp_ptid
;
3577 int why
, what
, flags
;
3584 retval
= pid_to_ptid (-1);
3586 /* Find procinfo for main process. */
3587 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
3590 /* We must assume that the status is stale now... */
3591 pi
->status_valid
= 0;
3592 pi
->gregs_valid
= 0;
3593 pi
->fpregs_valid
= 0;
3595 #if 0 /* just try this out... */
3596 flags
= proc_flags (pi
);
3597 why
= proc_why (pi
);
3598 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3599 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3601 /* If child is not stopped, wait for it to stop. */
3602 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3603 !proc_wait_for_stop (pi
))
3605 /* wait_for_stop failed: has the child terminated? */
3606 if (errno
== ENOENT
)
3610 /* /proc file not found; presumably child has terminated. */
3611 wait_retval
= wait (&wstat
); /* "wait" for the child's exit. */
3614 if (wait_retval
!= ptid_get_pid (inferior_ptid
))
3615 error (_("procfs: couldn't stop "
3616 "process %d: wait returned %d."),
3617 ptid_get_pid (inferior_ptid
), wait_retval
);
3618 /* FIXME: might I not just use waitpid?
3619 Or try find_procinfo to see if I know about this child? */
3620 retval
= pid_to_ptid (wait_retval
);
3622 else if (errno
== EINTR
)
3626 /* Unknown error from wait_for_stop. */
3627 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3632 /* This long block is reached if either:
3633 a) the child was already stopped, or
3634 b) we successfully waited for the child with wait_for_stop.
3635 This block will analyze the /proc status, and translate it
3636 into a waitstatus for GDB.
3638 If we actually had to call wait because the /proc file
3639 is gone (child terminated), then we skip this block,
3640 because we already have a waitstatus. */
3642 flags
= proc_flags (pi
);
3643 why
= proc_why (pi
);
3644 what
= proc_what (pi
);
3646 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3649 /* If it's running async (for single_thread control),
3650 set it back to normal again. */
3651 if (flags
& PR_ASYNC
)
3652 if (!proc_unset_async (pi
))
3653 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3657 proc_prettyprint_why (why
, what
, 1);
3659 /* The 'pid' we will return to GDB is composed of
3660 the process ID plus the lwp ID. */
3661 retval
= ptid_build (pi
->pid
, proc_get_current_thread (pi
), 0);
3665 wstat
= (what
<< 8) | 0177;
3668 if (syscall_is_lwp_exit (pi
, what
))
3670 if (print_thread_events
)
3671 printf_unfiltered (_("[%s exited]\n"),
3672 target_pid_to_str (retval
));
3673 delete_thread (retval
);
3674 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3677 else if (syscall_is_exit (pi
, what
))
3679 struct inferior
*inf
;
3681 /* Handle SYS_exit call only. */
3682 /* Stopped at entry to SYS_exit.
3683 Make it runnable, resume it, then use
3684 the wait system call to get its exit code.
3685 Proc_run_process always clears the current
3687 Then return its exit status. */
3688 pi
->status_valid
= 0;
3690 /* FIXME: what we should do is return
3691 TARGET_WAITKIND_SPURIOUS. */
3692 if (!proc_run_process (pi
, 0, 0))
3693 proc_error (pi
, "target_wait, run_process", __LINE__
);
3695 inf
= find_inferior_pid (pi
->pid
);
3696 if (inf
->attach_flag
)
3698 /* Don't call wait: simulate waiting for exit,
3699 return a "success" exit code. Bogus: what if
3700 it returns something else? */
3702 retval
= inferior_ptid
; /* ? ? ? */
3706 int temp
= wait (&wstat
);
3708 /* FIXME: shouldn't I make sure I get the right
3709 event from the right process? If (for
3710 instance) I have killed an earlier inferior
3711 process but failed to clean up after it
3712 somehow, I could get its termination event
3715 /* If wait returns -1, that's what we return
3718 retval
= pid_to_ptid (temp
);
3723 printf_filtered (_("procfs: trapped on entry to "));
3724 proc_prettyprint_syscall (proc_what (pi
), 0);
3725 printf_filtered ("\n");
3728 long i
, nsysargs
, *sysargs
;
3730 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3731 (sysargs
= proc_sysargs (pi
)) != NULL
)
3733 printf_filtered (_("%ld syscall arguments:\n"),
3735 for (i
= 0; i
< nsysargs
; i
++)
3736 printf_filtered ("#%ld: 0x%08lx\n",
3744 /* How to exit gracefully, returning "unknown
3746 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3747 return inferior_ptid
;
3751 /* How to keep going without returning to wfi: */
3752 target_resume (ptid
, 0, GDB_SIGNAL_0
);
3758 if (syscall_is_exec (pi
, what
))
3760 /* Hopefully this is our own "fork-child" execing
3761 the real child. Hoax this event into a trap, and
3762 GDB will see the child about to execute its start
3764 wstat
= (SIGTRAP
<< 8) | 0177;
3767 else if (what
== SYS_syssgi
)
3769 /* see if we can break on dbx_link(). If yes, then
3770 we no longer need the SYS_syssgi notifications. */
3771 if (insert_dbx_link_breakpoint (pi
))
3772 proc_trace_syscalls_1 (pi
, SYS_syssgi
, PR_SYSEXIT
,
3775 /* This is an internal event and should be transparent
3776 to wfi, so resume the execution and wait again. See
3777 comment in procfs_init_inferior() for more details. */
3778 target_resume (ptid
, 0, GDB_SIGNAL_0
);
3782 else if (syscall_is_lwp_create (pi
, what
))
3784 /* This syscall is somewhat like fork/exec. We
3785 will get the event twice: once for the parent
3786 LWP, and once for the child. We should already
3787 know about the parent LWP, but the child will
3788 be new to us. So, whenever we get this event,
3789 if it represents a new thread, simply add the
3790 thread to the list. */
3792 /* If not in procinfo list, add it. */
3793 temp_tid
= proc_get_current_thread (pi
);
3794 if (!find_procinfo (pi
->pid
, temp_tid
))
3795 create_procinfo (pi
->pid
, temp_tid
);
3797 temp_ptid
= ptid_build (pi
->pid
, temp_tid
, 0);
3798 /* If not in GDB's thread list, add it. */
3799 if (!in_thread_list (temp_ptid
))
3800 add_thread (temp_ptid
);
3802 /* Return to WFI, but tell it to immediately resume. */
3803 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3804 return inferior_ptid
;
3806 else if (syscall_is_lwp_exit (pi
, what
))
3808 if (print_thread_events
)
3809 printf_unfiltered (_("[%s exited]\n"),
3810 target_pid_to_str (retval
));
3811 delete_thread (retval
);
3812 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3817 /* FIXME: Do we need to handle SYS_sproc,
3818 SYS_fork, or SYS_vfork here? The old procfs
3819 seemed to use this event to handle threads on
3820 older (non-LWP) systems, where I'm assuming
3821 that threads were actually separate processes.
3822 Irix, maybe? Anyway, low priority for now. */
3826 printf_filtered (_("procfs: trapped on exit from "));
3827 proc_prettyprint_syscall (proc_what (pi
), 0);
3828 printf_filtered ("\n");
3831 long i
, nsysargs
, *sysargs
;
3833 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3834 (sysargs
= proc_sysargs (pi
)) != NULL
)
3836 printf_filtered (_("%ld syscall arguments:\n"),
3838 for (i
= 0; i
< nsysargs
; i
++)
3839 printf_filtered ("#%ld: 0x%08lx\n",
3844 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3845 return inferior_ptid
;
3850 wstat
= (SIGSTOP
<< 8) | 0177;
3855 printf_filtered (_("Retry #%d:\n"), retry
);
3856 pi
->status_valid
= 0;
3861 /* If not in procinfo list, add it. */
3862 temp_tid
= proc_get_current_thread (pi
);
3863 if (!find_procinfo (pi
->pid
, temp_tid
))
3864 create_procinfo (pi
->pid
, temp_tid
);
3866 /* If not in GDB's thread list, add it. */
3867 temp_ptid
= ptid_build (pi
->pid
, temp_tid
, 0);
3868 if (!in_thread_list (temp_ptid
))
3869 add_thread (temp_ptid
);
3871 status
->kind
= TARGET_WAITKIND_STOPPED
;
3872 status
->value
.sig
= 0;
3877 wstat
= (what
<< 8) | 0177;
3883 wstat
= (SIGTRAP
<< 8) | 0177;
3888 wstat
= (SIGTRAP
<< 8) | 0177;
3891 /* FIXME: use si_signo where possible. */
3893 #if (FLTILL != FLTPRIV) /* Avoid "duplicate case" error. */
3896 wstat
= (SIGILL
<< 8) | 0177;
3899 #if (FLTTRACE != FLTBPT) /* Avoid "duplicate case" error. */
3902 /* If we hit our __dbx_link() internal breakpoint,
3903 then remove it. See comments in procfs_init_inferior()
3904 for more details. */
3905 if (dbx_link_bpt_addr
!= 0
3906 && dbx_link_bpt_addr
3907 == regcache_read_pc (get_current_regcache ()))
3908 remove_dbx_link_breakpoint ();
3910 wstat
= (SIGTRAP
<< 8) | 0177;
3914 #if (FLTBOUNDS != FLTSTACK) /* Avoid "duplicate case" error. */
3917 wstat
= (SIGSEGV
<< 8) | 0177;
3921 #if (FLTFPE != FLTIOVF) /* Avoid "duplicate case" error. */
3924 wstat
= (SIGFPE
<< 8) | 0177;
3926 case FLTPAGE
: /* Recoverable page fault */
3927 default: /* FIXME: use si_signo if possible for
3929 retval
= pid_to_ptid (-1);
3930 printf_filtered ("procfs:%d -- ", __LINE__
);
3931 printf_filtered (_("child stopped for unknown reason:\n"));
3932 proc_prettyprint_why (why
, what
, 1);
3933 error (_("... giving up..."));
3936 break; /* case PR_FAULTED: */
3937 default: /* switch (why) unmatched */
3938 printf_filtered ("procfs:%d -- ", __LINE__
);
3939 printf_filtered (_("child stopped for unknown reason:\n"));
3940 proc_prettyprint_why (why
, what
, 1);
3941 error (_("... giving up..."));
3944 /* Got this far without error: If retval isn't in the
3945 threads database, add it. */
3946 if (ptid_get_pid (retval
) > 0 &&
3947 !ptid_equal (retval
, inferior_ptid
) &&
3948 !in_thread_list (retval
))
3950 /* We have a new thread. We need to add it both to
3951 GDB's list and to our own. If we don't create a
3952 procinfo, resume may be unhappy later. */
3953 add_thread (retval
);
3954 if (find_procinfo (ptid_get_pid (retval
),
3955 ptid_get_lwp (retval
)) == NULL
)
3956 create_procinfo (ptid_get_pid (retval
),
3957 ptid_get_lwp (retval
));
3960 else /* Flags do not indicate STOPPED. */
3962 /* surely this can't happen... */
3963 printf_filtered ("procfs:%d -- process not stopped.\n",
3965 proc_prettyprint_flags (flags
, 1);
3966 error (_("procfs: ...giving up..."));
3971 store_waitstatus (status
, wstat
);
3977 /* Perform a partial transfer to/from the specified object. For
3978 memory transfers, fall back to the old memory xfer functions. */
3980 static enum target_xfer_status
3981 procfs_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3982 const char *annex
, gdb_byte
*readbuf
,
3983 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
3984 ULONGEST
*xfered_len
)
3988 case TARGET_OBJECT_MEMORY
:
3990 return (*ops
->deprecated_xfer_memory
) (offset
, readbuf
,
3991 len
, 0/*read*/, NULL
, ops
);
3993 return (*ops
->deprecated_xfer_memory
) (offset
, (gdb_byte
*) writebuf
,
3994 len
, 1/*write*/, NULL
, ops
);
3995 return TARGET_XFER_E_IO
;
3998 case TARGET_OBJECT_AUXV
:
3999 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4000 offset
, len
, xfered_len
);
4004 if (ops
->beneath
!= NULL
)
4005 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
,
4006 readbuf
, writebuf
, offset
, len
,
4008 return TARGET_XFER_E_IO
;
4013 /* Transfer LEN bytes between GDB address MYADDR and target address
4014 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4015 otherwise transfer them from the target. TARGET is unused.
4017 The return value is 0 if an error occurred or no bytes were
4018 transferred. Otherwise, it will be a positive value which
4019 indicates the number of bytes transferred between gdb and the
4020 target. (Note that the interface also makes provisions for
4021 negative values, but this capability isn't implemented here.) */
4024 procfs_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
, int dowrite
,
4025 struct mem_attrib
*attrib
, struct target_ops
*target
)
4030 /* Find procinfo for main process. */
4031 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4032 if (pi
->as_fd
== 0 &&
4033 open_procinfo_files (pi
, FD_AS
) == 0)
4035 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4039 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4044 PROCFS_NOTE ("write memory:\n");
4046 PROCFS_NOTE ("write memory:\n");
4048 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4052 PROCFS_NOTE ("read memory:\n");
4053 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4063 /* Called by target_resume before making child runnable. Mark cached
4064 registers and status's invalid. If there are "dirty" caches that
4065 need to be written back to the child process, do that.
4067 File descriptors are also cached. As they are a limited resource,
4068 we cannot hold onto them indefinitely. However, as they are
4069 expensive to open, we don't want to throw them away
4070 indescriminately either. As a compromise, we will keep the file
4071 descriptors for the parent process, but discard any file
4072 descriptors we may have accumulated for the threads.
4074 As this function is called by iterate_over_threads, it always
4075 returns zero (so that iterate_over_threads will keep
4079 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4081 /* About to run the child; invalidate caches and do any other
4085 if (pi
->gregs_dirty
)
4086 if (parent
== NULL
||
4087 proc_get_current_thread (parent
) != pi
->tid
)
4088 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4089 proc_warn (pi
, "target_resume, set_gregs",
4091 if (gdbarch_fp0_regnum (target_gdbarch ()) >= 0)
4092 if (pi
->fpregs_dirty
)
4093 if (parent
== NULL
||
4094 proc_get_current_thread (parent
) != pi
->tid
)
4095 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4096 proc_warn (pi
, "target_resume, set_fpregs",
4102 /* The presence of a parent indicates that this is an LWP.
4103 Close any file descriptors that it might have open.
4104 We don't do this to the master (parent) procinfo. */
4106 close_procinfo_files (pi
);
4108 pi
->gregs_valid
= 0;
4109 pi
->fpregs_valid
= 0;
4111 pi
->gregs_dirty
= 0;
4112 pi
->fpregs_dirty
= 0;
4114 pi
->status_valid
= 0;
4115 pi
->threads_valid
= 0;
4121 /* A callback function for iterate_over_threads. Find the
4122 asynchronous signal thread, and make it runnable. See if that
4123 helps matters any. */
4126 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4129 if (proc_flags (pi
) & PR_ASLWP
)
4131 if (!proc_run_process (pi
, 0, -1))
4132 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4140 /* Make the child process runnable. Normally we will then call
4141 procfs_wait and wait for it to stop again (unless gdb is async).
4143 If STEP is true, then arrange for the child to stop again after
4144 executing a single instruction. If SIGNO is zero, then cancel any
4145 pending signal; if non-zero, then arrange for the indicated signal
4146 to be delivered to the child when it runs. If PID is -1, then
4147 allow any child thread to run; if non-zero, then allow only the
4148 indicated thread to run. (not implemented yet). */
4151 procfs_resume (struct target_ops
*ops
,
4152 ptid_t ptid
, int step
, enum gdb_signal signo
)
4154 procinfo
*pi
, *thread
;
4158 prrun.prflags |= PRSVADDR;
4159 prrun.pr_vaddr = $PC; set resume address
4160 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4161 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4162 prrun.prflags |= PRCFAULT; clear current fault.
4164 PRSTRACE and PRSFAULT can be done by other means
4165 (proc_trace_signals, proc_trace_faults)
4166 PRSVADDR is unnecessary.
4167 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4168 This basically leaves PRSTEP and PRCSIG.
4169 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4170 So basically PR_STEP is the sole argument that must be passed
4171 to proc_run_process (for use in the prrun struct by ioctl). */
4173 /* Find procinfo for main process. */
4174 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4176 /* First cut: ignore pid argument. */
4179 /* Convert signal to host numbering. */
4181 (signo
== GDB_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4184 native_signo
= gdb_signal_to_host (signo
);
4186 pi
->ignore_next_sigstop
= 0;
4188 /* Running the process voids all cached registers and status. */
4189 /* Void the threads' caches first. */
4190 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4191 /* Void the process procinfo's caches. */
4192 invalidate_cache (NULL
, pi
, NULL
);
4194 if (ptid_get_pid (ptid
) != -1)
4196 /* Resume a specific thread, presumably suppressing the
4198 thread
= find_procinfo (ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
4201 if (thread
->tid
!= 0)
4203 /* We're to resume a specific thread, and not the
4204 others. Set the child process's PR_ASYNC flag. */
4206 if (!proc_set_async (pi
))
4207 proc_error (pi
, "target_resume, set_async", __LINE__
);
4210 proc_iterate_over_threads (pi
,
4211 make_signal_thread_runnable
,
4214 pi
= thread
; /* Substitute the thread's procinfo
4220 if (!proc_run_process (pi
, step
, native_signo
))
4223 warning (_("resume: target already running. "
4224 "Pretend to resume, and hope for the best!"));
4226 proc_error (pi
, "target_resume", __LINE__
);
4230 /* Set up to trace signals in the child process. */
4233 procfs_pass_signals (struct target_ops
*self
,
4234 int numsigs
, unsigned char *pass_signals
)
4236 gdb_sigset_t signals
;
4237 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4240 prfillset (&signals
);
4242 for (signo
= 0; signo
< NSIG
; signo
++)
4244 int target_signo
= gdb_signal_from_host (signo
);
4245 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
4246 gdb_prdelset (&signals
, signo
);
4249 if (!proc_set_traced_signals (pi
, &signals
))
4250 proc_error (pi
, "pass_signals", __LINE__
);
4253 /* Print status information about the child process. */
4256 procfs_files_info (struct target_ops
*ignore
)
4258 struct inferior
*inf
= current_inferior ();
4260 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
4261 inf
->attach_flag
? "attached": "child",
4262 target_pid_to_str (inferior_ptid
));
4265 /* Stop the child process asynchronously, as when the gdb user types
4266 control-c or presses a "stop" button. Works by sending
4267 kill(SIGINT) to the child's process group. */
4270 procfs_stop (struct target_ops
*self
, ptid_t ptid
)
4272 kill (-inferior_process_group (), SIGINT
);
4275 /* Make it die. Wait for it to die. Clean up after it. Note: this
4276 should only be applied to the real process, not to an LWP, because
4277 of the check for parent-process. If we need this to work for an
4278 LWP, it needs some more logic. */
4281 unconditionally_kill_inferior (procinfo
*pi
)
4285 parent_pid
= proc_parent_pid (pi
);
4286 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4287 /* FIXME: use access functions. */
4288 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4289 before the PIOCKILL, otherwise it might generate a corrupted core
4290 file for the inferior. */
4291 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4293 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4296 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4297 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4298 to kill the inferior, otherwise it might remain stopped with a
4300 We do not check the result of the PIOCSSIG, the inferior might have
4303 gdb_siginfo_t newsiginfo
;
4305 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4306 newsiginfo
.si_signo
= SIGKILL
;
4307 newsiginfo
.si_code
= 0;
4308 newsiginfo
.si_errno
= 0;
4309 newsiginfo
.si_pid
= getpid ();
4310 newsiginfo
.si_uid
= getuid ();
4311 /* FIXME: use proc_set_current_signal. */
4312 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4314 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4315 if (!proc_kill (pi
, SIGKILL
))
4316 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4317 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4318 destroy_procinfo (pi
);
4320 /* If pi is GDB's child, wait for it to die. */
4321 if (parent_pid
== getpid ())
4322 /* FIXME: should we use waitpid to make sure we get the right event?
4323 Should we check the returned event? */
4328 ret
= waitpid (pi
->pid
, &status
, 0);
4335 /* We're done debugging it, and we want it to go away. Then we want
4336 GDB to forget all about it. */
4339 procfs_kill_inferior (struct target_ops
*ops
)
4341 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4343 /* Find procinfo for main process. */
4344 procinfo
*pi
= find_procinfo (ptid_get_pid (inferior_ptid
), 0);
4347 unconditionally_kill_inferior (pi
);
4348 target_mourn_inferior ();
4352 /* Forget we ever debugged this thing! */
4355 procfs_mourn_inferior (struct target_ops
*ops
)
4359 if (!ptid_equal (inferior_ptid
, null_ptid
))
4361 /* Find procinfo for main process. */
4362 pi
= find_procinfo (ptid_get_pid (inferior_ptid
), 0);
4364 destroy_procinfo (pi
);
4366 unpush_target (ops
);
4368 if (dbx_link_bpt
!= NULL
)
4370 deprecated_remove_raw_breakpoint (target_gdbarch (), dbx_link_bpt
);
4371 dbx_link_bpt_addr
= 0;
4372 dbx_link_bpt
= NULL
;
4375 generic_mourn_inferior ();
4378 /* When GDB forks to create a runnable inferior process, this function
4379 is called on the parent side of the fork. It's job is to do
4380 whatever is necessary to make the child ready to be debugged, and
4381 then wait for the child to synchronize. */
4384 procfs_init_inferior (struct target_ops
*ops
, int pid
)
4387 gdb_sigset_t signals
;
4391 /* This routine called on the parent side (GDB side)
4392 after GDB forks the inferior. */
4395 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4396 perror (_("procfs: out of memory in 'init_inferior'"));
4398 if (!open_procinfo_files (pi
, FD_CTL
))
4399 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4403 open_procinfo_files // done
4406 procfs_notice_signals
4413 /* If not stopped yet, wait for it to stop. */
4414 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4415 !(proc_wait_for_stop (pi
)))
4416 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4418 /* Save some of the /proc state to be restored if we detach. */
4419 /* FIXME: Why? In case another debugger was debugging it?
4420 We're it's parent, for Ghu's sake! */
4421 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4422 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4423 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4424 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4425 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4426 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4427 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4428 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4429 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4430 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4432 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4433 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4435 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4436 and possibly even turning ON kill-on-last-close at this point. But
4437 I can't make that change without careful testing which I don't have
4438 time to do right now... */
4439 /* Turn on run-on-last-close flag so that the child
4440 will die if GDB goes away for some reason. */
4441 if (!proc_set_run_on_last_close (pi
))
4442 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4444 /* We now have have access to the lwpid of the main thread/lwp. */
4445 lwpid
= proc_get_current_thread (pi
);
4447 /* Create a procinfo for the main lwp. */
4448 create_procinfo (pid
, lwpid
);
4450 /* We already have a main thread registered in the thread table at
4451 this point, but it didn't have any lwp info yet. Notify the core
4452 about it. This changes inferior_ptid as well. */
4453 thread_change_ptid (pid_to_ptid (pid
),
4454 ptid_build (pid
, lwpid
, 0));
4456 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4459 /* On mips-irix, we need to stop the inferior early enough during
4460 the startup phase in order to be able to load the shared library
4461 symbols and insert the breakpoints that are located in these shared
4462 libraries. Stopping at the program entry point is not good enough
4463 because the -init code is executed before the execution reaches
4466 So what we need to do is to insert a breakpoint in the runtime
4467 loader (rld), more precisely in __dbx_link(). This procedure is
4468 called by rld once all shared libraries have been mapped, but before
4469 the -init code is executed. Unfortuantely, this is not straightforward,
4470 as rld is not part of the executable we are running, and thus we need
4471 the inferior to run until rld itself has been mapped in memory.
4473 For this, we trace all syssgi() syscall exit events. Each time
4474 we detect such an event, we iterate over each text memory maps,
4475 get its associated fd, and scan the symbol table for __dbx_link().
4476 When found, we know that rld has been mapped, and that we can insert
4477 the breakpoint at the symbol address. Once the dbx_link() breakpoint
4478 has been inserted, the syssgi() notifications are no longer necessary,
4479 so they should be canceled. */
4480 proc_trace_syscalls_1 (pi
, SYS_syssgi
, PR_SYSEXIT
, FLAG_SET
, 0);
4484 /* When GDB forks to create a new process, this function is called on
4485 the child side of the fork before GDB exec's the user program. Its
4486 job is to make the child minimally debuggable, so that the parent
4487 GDB process can connect to the child and take over. This function
4488 should do only the minimum to make that possible, and to
4489 synchronize with the parent process. The parent process should
4490 take care of the details. */
4493 procfs_set_exec_trap (void)
4495 /* This routine called on the child side (inferior side)
4496 after GDB forks the inferior. It must use only local variables,
4497 because it may be sharing data space with its parent. */
4502 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4503 perror_with_name (_("procfs: create_procinfo failed in child."));
4505 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4507 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4508 gdb_flush (gdb_stderr
);
4509 /* No need to call "dead_procinfo", because we're going to
4514 #ifdef PRFS_STOPEXEC /* defined on OSF */
4515 /* OSF method for tracing exec syscalls. Quoting:
4516 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4517 exits from exec system calls because of the user level loader. */
4518 /* FIXME: make nice and maybe move into an access function. */
4522 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4524 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4525 gdb_flush (gdb_stderr
);
4528 prfs_flags
|= PRFS_STOPEXEC
;
4530 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4532 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4533 gdb_flush (gdb_stderr
);
4537 #else /* not PRFS_STOPEXEC */
4538 /* Everyone else's (except OSF) method for tracing exec syscalls. */
4540 Not all systems with /proc have all the exec* syscalls with the same
4541 names. On the SGI, for example, there is no SYS_exec, but there
4542 *is* a SYS_execv. So, we try to account for that. */
4544 exitset
= sysset_t_alloc (pi
);
4545 gdb_premptysysset (exitset
);
4547 gdb_praddsysset (exitset
, SYS_exec
);
4550 gdb_praddsysset (exitset
, SYS_execve
);
4553 gdb_praddsysset (exitset
, SYS_execv
);
4555 #ifdef DYNAMIC_SYSCALLS
4557 int callnum
= find_syscall (pi
, "execve");
4560 gdb_praddsysset (exitset
, callnum
);
4562 callnum
= find_syscall (pi
, "ra_execve");
4564 gdb_praddsysset (exitset
, callnum
);
4566 #endif /* DYNAMIC_SYSCALLS */
4568 if (!proc_set_traced_sysexit (pi
, exitset
))
4570 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4571 gdb_flush (gdb_stderr
);
4574 #endif /* PRFS_STOPEXEC */
4576 /* FIXME: should this be done in the parent instead? */
4577 /* Turn off inherit on fork flag so that all grand-children
4578 of gdb start with tracing flags cleared. */
4579 if (!proc_unset_inherit_on_fork (pi
))
4580 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4582 /* Turn off run on last close flag, so that the child process
4583 cannot run away just because we close our handle on it.
4584 We want it to wait for the parent to attach. */
4585 if (!proc_unset_run_on_last_close (pi
))
4586 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4588 /* FIXME: No need to destroy the procinfo --
4589 we have our own address space, and we're about to do an exec! */
4590 /*destroy_procinfo (pi);*/
4593 /* This function is called BEFORE gdb forks the inferior process. Its
4594 only real responsibility is to set things up for the fork, and tell
4595 GDB which two functions to call after the fork (one for the parent,
4596 and one for the child).
4598 This function does a complicated search for a unix shell program,
4599 which it then uses to parse arguments and environment variables to
4600 be sent to the child. I wonder whether this code could not be
4601 abstracted out and shared with other unix targets such as
4605 procfs_create_inferior (struct target_ops
*ops
, char *exec_file
,
4606 char *allargs
, char **env
, int from_tty
)
4608 char *shell_file
= getenv ("SHELL");
4612 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4615 /* We will be looking down the PATH to find shell_file. If we
4616 just do this the normal way (via execlp, which operates by
4617 attempting an exec for each element of the PATH until it
4618 finds one which succeeds), then there will be an exec for
4619 each failed attempt, each of which will cause a PR_SYSEXIT
4620 stop, and we won't know how to distinguish the PR_SYSEXIT's
4621 for these failed execs with the ones for successful execs
4622 (whether the exec has succeeded is stored at that time in the
4623 carry bit or some such architecture-specific and
4624 non-ABI-specified place).
4626 So I can't think of anything better than to search the PATH
4627 now. This has several disadvantages: (1) There is a race
4628 condition; if we find a file now and it is deleted before we
4629 exec it, we lose, even if the deletion leaves a valid file
4630 further down in the PATH, (2) there is no way to know exactly
4631 what an executable (in the sense of "capable of being
4632 exec'd") file is. Using access() loses because it may lose
4633 if the caller is the superuser; failing to use it loses if
4634 there are ACLs or some such. */
4638 /* FIXME-maybe: might want "set path" command so user can change what
4639 path is used from within GDB. */
4640 char *path
= getenv ("PATH");
4642 struct stat statbuf
;
4645 path
= "/bin:/usr/bin";
4647 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4648 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4650 p1
= strchr (p
, ':');
4655 strncpy (tryname
, p
, len
);
4656 tryname
[len
] = '\0';
4657 strcat (tryname
, "/");
4658 strcat (tryname
, shell_file
);
4659 if (access (tryname
, X_OK
) < 0)
4661 if (stat (tryname
, &statbuf
) < 0)
4663 if (!S_ISREG (statbuf
.st_mode
))
4664 /* We certainly need to reject directories. I'm not quite
4665 as sure about FIFOs, sockets, etc., but I kind of doubt
4666 that people want to exec() these things. */
4671 /* Not found. This must be an error rather than merely passing
4672 the file to execlp(), because execlp() would try all the
4673 exec()s, causing GDB to get confused. */
4674 error (_("procfs:%d -- Can't find shell %s in PATH"),
4675 __LINE__
, shell_file
);
4677 shell_file
= tryname
;
4680 pid
= fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4681 NULL
, NULL
, shell_file
, NULL
);
4683 procfs_init_inferior (ops
, pid
);
4686 /* An observer for the "inferior_created" event. */
4689 procfs_inferior_created (struct target_ops
*ops
, int from_tty
)
4692 /* Make sure to cancel the syssgi() syscall-exit notifications.
4693 They should normally have been removed by now, but they may still
4694 be activated if the inferior doesn't use shared libraries, or if
4695 we didn't locate __dbx_link, or if we never stopped in __dbx_link.
4696 See procfs_init_inferior() for more details.
4698 Since these notifications are only ever enabled when we spawned
4699 the inferior ourselves, there is nothing to do when the inferior
4700 was created by attaching to an already running process, or when
4701 debugging a core file. */
4702 if (current_inferior ()->attach_flag
|| !target_can_run (¤t_target
))
4705 proc_trace_syscalls_1 (find_procinfo_or_die (ptid_get_pid (inferior_ptid
),
4706 0), SYS_syssgi
, PR_SYSEXIT
, FLAG_RESET
, 0);
4710 /* Callback for find_new_threads. Calls "add_thread". */
4713 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
4715 ptid_t gdb_threadid
= ptid_build (pi
->pid
, thread
->tid
, 0);
4717 if (!in_thread_list (gdb_threadid
) || is_exited (gdb_threadid
))
4718 add_thread (gdb_threadid
);
4723 /* Query all the threads that the target knows about, and give them
4724 back to GDB to add to its list. */
4727 procfs_find_new_threads (struct target_ops
*ops
)
4731 /* Find procinfo for main process. */
4732 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4733 proc_update_threads (pi
);
4734 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
4737 /* Return true if the thread is still 'alive'. This guy doesn't
4738 really seem to be doing his job. Got to investigate how to tell
4739 when a thread is really gone. */
4742 procfs_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
4747 proc
= ptid_get_pid (ptid
);
4748 thread
= ptid_get_lwp (ptid
);
4749 /* If I don't know it, it ain't alive! */
4750 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
4753 /* If I can't get its status, it ain't alive!
4754 What's more, I need to forget about it! */
4755 if (!proc_get_status (pi
))
4757 destroy_procinfo (pi
);
4760 /* I couldn't have got its status if it weren't alive, so it's
4765 /* Convert PTID to a string. Returns the string in a static
4769 procfs_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
4771 static char buf
[80];
4773 if (ptid_get_lwp (ptid
) == 0)
4774 sprintf (buf
, "process %d", ptid_get_pid (ptid
));
4776 sprintf (buf
, "LWP %ld", ptid_get_lwp (ptid
));
4781 /* Insert a watchpoint. */
4784 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
4791 pi
= find_procinfo_or_die (ptid_get_pid (ptid
) == -1 ?
4792 ptid_get_pid (inferior_ptid
) : ptid_get_pid (ptid
),
4795 /* Translate from GDB's flags to /proc's. */
4796 if (len
> 0) /* len == 0 means delete watchpoint. */
4798 switch (rwflag
) { /* FIXME: need an enum! */
4799 case hw_write
: /* default watchpoint (write) */
4800 pflags
= WRITE_WATCHFLAG
;
4802 case hw_read
: /* read watchpoint */
4803 pflags
= READ_WATCHFLAG
;
4805 case hw_access
: /* access watchpoint */
4806 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
4808 case hw_execute
: /* execution HW breakpoint */
4809 pflags
= EXEC_WATCHFLAG
;
4811 default: /* Something weird. Return error. */
4814 if (after
) /* Stop after r/w access is completed. */
4815 pflags
|= AFTER_WATCHFLAG
;
4818 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
4820 if (errno
== E2BIG
) /* Typical error for no resources. */
4821 return -1; /* fail */
4822 /* GDB may try to remove the same watchpoint twice.
4823 If a remove request returns no match, don't error. */
4824 if (errno
== ESRCH
&& len
== 0)
4825 return 0; /* ignore */
4826 proc_error (pi
, "set_watchpoint", __LINE__
);
4832 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
4833 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
4834 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
4837 Note: procfs_can_use_hw_breakpoint() is not yet used by all
4838 procfs.c targets due to the fact that some of them still define
4839 target_can_use_hardware_watchpoint. */
4842 procfs_can_use_hw_breakpoint (struct target_ops
*self
,
4843 int type
, int cnt
, int othertype
)
4845 /* Due to the way that proc_set_watchpoint() is implemented, host
4846 and target pointers must be of the same size. If they are not,
4847 we can't use hardware watchpoints. This limitation is due to the
4848 fact that proc_set_watchpoint() calls
4849 procfs_address_to_host_pointer(); a close inspection of
4850 procfs_address_to_host_pointer will reveal that an internal error
4851 will be generated when the host and target pointer sizes are
4853 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
4855 if (sizeof (void *) != TYPE_LENGTH (ptr_type
))
4858 /* Other tests here??? */
4863 /* Returns non-zero if process is stopped on a hardware watchpoint
4864 fault, else returns zero. */
4867 procfs_stopped_by_watchpoint (struct target_ops
*ops
)
4871 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4873 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
4875 if (proc_why (pi
) == PR_FAULTED
)
4878 if (proc_what (pi
) == FLTWATCH
)
4882 if (proc_what (pi
) == FLTKWATCH
)
4890 /* Returns 1 if the OS knows the position of the triggered watchpoint,
4891 and sets *ADDR to that address. Returns 0 if OS cannot report that
4892 address. This function is only called if
4893 procfs_stopped_by_watchpoint returned 1, thus no further checks are
4894 done. The function also assumes that ADDR is not NULL. */
4897 procfs_stopped_data_address (struct target_ops
*targ
, CORE_ADDR
*addr
)
4901 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4902 return proc_watchpoint_address (pi
, addr
);
4906 procfs_insert_watchpoint (struct target_ops
*self
,
4907 CORE_ADDR addr
, int len
, int type
,
4908 struct expression
*cond
)
4910 if (!target_have_steppable_watchpoint
4911 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
4913 /* When a hardware watchpoint fires off the PC will be left at
4914 the instruction following the one which caused the
4915 watchpoint. It will *NOT* be necessary for GDB to step over
4917 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 1);
4921 /* When a hardware watchpoint fires off the PC will be left at
4922 the instruction which caused the watchpoint. It will be
4923 necessary for GDB to step over the watchpoint. */
4924 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 0);
4929 procfs_remove_watchpoint (struct target_ops
*self
,
4930 CORE_ADDR addr
, int len
, int type
,
4931 struct expression
*cond
)
4933 return procfs_set_watchpoint (inferior_ptid
, addr
, 0, 0, 0);
4937 procfs_region_ok_for_hw_watchpoint (struct target_ops
*self
,
4938 CORE_ADDR addr
, int len
)
4940 /* The man page for proc(4) on Solaris 2.6 and up says that the
4941 system can support "thousands" of hardware watchpoints, but gives
4942 no method for finding out how many; It doesn't say anything about
4943 the allowed size for the watched area either. So we just tell
4949 procfs_use_watchpoints (struct target_ops
*t
)
4951 t
->to_stopped_by_watchpoint
= procfs_stopped_by_watchpoint
;
4952 t
->to_insert_watchpoint
= procfs_insert_watchpoint
;
4953 t
->to_remove_watchpoint
= procfs_remove_watchpoint
;
4954 t
->to_region_ok_for_hw_watchpoint
= procfs_region_ok_for_hw_watchpoint
;
4955 t
->to_can_use_hw_breakpoint
= procfs_can_use_hw_breakpoint
;
4956 t
->to_stopped_data_address
= procfs_stopped_data_address
;
4959 /* Memory Mappings Functions: */
4961 /* Call a callback function once for each mapping, passing it the
4962 mapping, an optional secondary callback function, and some optional
4963 opaque data. Quit and return the first non-zero value returned
4966 PI is the procinfo struct for the process to be mapped. FUNC is
4967 the callback function to be called by this iterator. DATA is the
4968 optional opaque data to be passed to the callback function.
4969 CHILD_FUNC is the optional secondary function pointer to be passed
4970 to the child function. Returns the first non-zero return value
4971 from the callback function, or zero. */
4974 iterate_over_mappings (procinfo
*pi
, find_memory_region_ftype child_func
,
4976 int (*func
) (struct prmap
*map
,
4977 find_memory_region_ftype child_func
,
4980 char pathname
[MAX_PROC_NAME_SIZE
];
4981 struct prmap
*prmaps
;
4982 struct prmap
*prmap
;
4986 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
4991 /* Get the number of mappings, allocate space,
4992 and read the mappings into prmaps. */
4995 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
4996 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
4997 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
4999 /* Make sure it gets closed again. */
5000 make_cleanup_close (map_fd
);
5002 /* Use stat to determine the file size, and compute
5003 the number of prmap_t objects it contains. */
5004 if (fstat (map_fd
, &sbuf
) != 0)
5005 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
5007 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
5008 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5009 if (read (map_fd
, (char *) prmaps
, nmap
* sizeof (*prmaps
))
5010 != (nmap
* sizeof (*prmaps
)))
5011 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
5013 /* Use ioctl command PIOCNMAP to get number of mappings. */
5014 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmap
) != 0)
5015 proc_error (pi
, "iterate_over_mappings (PIOCNMAP)", __LINE__
);
5017 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5018 if (ioctl (pi
->ctl_fd
, PIOCMAP
, prmaps
) != 0)
5019 proc_error (pi
, "iterate_over_mappings (PIOCMAP)", __LINE__
);
5022 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
5023 if ((funcstat
= (*func
) (prmap
, child_func
, data
)) != 0)
5025 do_cleanups (cleanups
);
5029 do_cleanups (cleanups
);
5033 /* Implements the to_find_memory_regions method. Calls an external
5034 function for each memory region.
5035 Returns the integer value returned by the callback. */
5038 find_memory_regions_callback (struct prmap
*map
,
5039 find_memory_region_ftype func
, void *data
)
5041 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
5043 (map
->pr_mflags
& MA_READ
) != 0,
5044 (map
->pr_mflags
& MA_WRITE
) != 0,
5045 (map
->pr_mflags
& MA_EXEC
) != 0,
5046 1, /* MODIFIED is unknown, pass it as true. */
5050 /* External interface. Calls a callback function once for each
5051 mapped memory region in the child process, passing as arguments:
5053 CORE_ADDR virtual_address,
5055 int read, TRUE if region is readable by the child
5056 int write, TRUE if region is writable by the child
5057 int execute TRUE if region is executable by the child.
5059 Stops iterating and returns the first non-zero value returned by
5063 proc_find_memory_regions (struct target_ops
*self
,
5064 find_memory_region_ftype func
, void *data
)
5066 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
5068 return iterate_over_mappings (pi
, func
, data
,
5069 find_memory_regions_callback
);
5072 /* Returns an ascii representation of a memory mapping's flags. */
5075 mappingflags (long flags
)
5077 static char asciiflags
[8];
5079 strcpy (asciiflags
, "-------");
5080 #if defined (MA_PHYS)
5081 if (flags
& MA_PHYS
)
5082 asciiflags
[0] = 'd';
5084 if (flags
& MA_STACK
)
5085 asciiflags
[1] = 's';
5086 if (flags
& MA_BREAK
)
5087 asciiflags
[2] = 'b';
5088 if (flags
& MA_SHARED
)
5089 asciiflags
[3] = 's';
5090 if (flags
& MA_READ
)
5091 asciiflags
[4] = 'r';
5092 if (flags
& MA_WRITE
)
5093 asciiflags
[5] = 'w';
5094 if (flags
& MA_EXEC
)
5095 asciiflags
[6] = 'x';
5096 return (asciiflags
);
5099 /* Callback function, does the actual work for 'info proc
5103 info_mappings_callback (struct prmap
*map
, find_memory_region_ftype ignore
,
5106 unsigned int pr_off
;
5108 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5109 pr_off
= (unsigned int) map
->pr_offset
;
5111 pr_off
= map
->pr_off
;
5114 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
5115 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
5116 (unsigned long) map
->pr_vaddr
,
5117 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5118 (unsigned long) map
->pr_size
,
5120 mappingflags (map
->pr_mflags
));
5122 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
5123 (unsigned long) map
->pr_vaddr
,
5124 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5125 (unsigned long) map
->pr_size
,
5127 mappingflags (map
->pr_mflags
));
5132 /* Implement the "info proc mappings" subcommand. */
5135 info_proc_mappings (procinfo
*pi
, int summary
)
5138 return; /* No output for summary mode. */
5140 printf_filtered (_("Mapped address spaces:\n\n"));
5141 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
5142 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
5149 printf_filtered (" %18s %18s %10s %10s %7s\n",
5156 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
5157 printf_filtered ("\n");
5160 /* Implement the "info proc" command. */
5163 procfs_info_proc (struct target_ops
*ops
, char *args
,
5164 enum info_proc_what what
)
5166 struct cleanup
*old_chain
;
5167 procinfo
*process
= NULL
;
5168 procinfo
*thread
= NULL
;
5186 error (_("Not supported on this target."));
5189 old_chain
= make_cleanup (null_cleanup
, 0);
5192 argv
= gdb_buildargv (args
);
5193 make_cleanup_freeargv (argv
);
5195 while (argv
!= NULL
&& *argv
!= NULL
)
5197 if (isdigit (argv
[0][0]))
5199 pid
= strtoul (argv
[0], &tmp
, 10);
5201 tid
= strtoul (++tmp
, NULL
, 10);
5203 else if (argv
[0][0] == '/')
5205 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5210 pid
= ptid_get_pid (inferior_ptid
);
5212 error (_("No current process: you must name one."));
5215 /* Have pid, will travel.
5216 First see if it's a process we're already debugging. */
5217 process
= find_procinfo (pid
, 0);
5218 if (process
== NULL
)
5220 /* No. So open a procinfo for it, but
5221 remember to close it again when finished. */
5222 process
= create_procinfo (pid
, 0);
5223 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5224 if (!open_procinfo_files (process
, FD_CTL
))
5225 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5229 thread
= create_procinfo (pid
, tid
);
5233 printf_filtered (_("process %d flags:\n"), process
->pid
);
5234 proc_prettyprint_flags (proc_flags (process
), 1);
5235 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5236 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5237 if (proc_get_nthreads (process
) > 1)
5238 printf_filtered ("Process has %d threads.\n",
5239 proc_get_nthreads (process
));
5243 printf_filtered (_("thread %d flags:\n"), thread
->tid
);
5244 proc_prettyprint_flags (proc_flags (thread
), 1);
5245 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5246 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5251 info_proc_mappings (process
, 0);
5254 do_cleanups (old_chain
);
5257 /* Modify the status of the system call identified by SYSCALLNUM in
5258 the set of syscalls that are currently traced/debugged.
5260 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
5261 will be updated. Otherwise, the exit syscalls set will be updated.
5263 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
5264 will be disabled. */
5267 proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
, int entry_or_exit
,
5268 int mode
, int from_tty
)
5272 if (entry_or_exit
== PR_SYSENTRY
)
5273 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5275 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5278 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5280 if (mode
== FLAG_SET
)
5281 gdb_praddsysset (sysset
, syscallnum
);
5283 gdb_prdelsysset (sysset
, syscallnum
);
5285 if (entry_or_exit
== PR_SYSENTRY
)
5287 if (!proc_set_traced_sysentry (pi
, sysset
))
5288 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5292 if (!proc_set_traced_sysexit (pi
, sysset
))
5293 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5298 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5302 if (ptid_get_pid (inferior_ptid
) <= 0)
5303 error (_("you must be debugging a process to use this command."));
5305 if (args
== NULL
|| args
[0] == 0)
5306 error_no_arg (_("system call to trace"));
5308 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
5309 if (isdigit (args
[0]))
5311 const int syscallnum
= atoi (args
);
5313 proc_trace_syscalls_1 (pi
, syscallnum
, entry_or_exit
, mode
, from_tty
);
5318 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5320 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5324 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5326 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5330 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5332 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5336 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5338 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5342 /* Provide a prototype to silence -Wmissing-prototypes. */
5343 extern void _initialize_procfs (void);
5346 _initialize_procfs (void)
5348 observer_attach_inferior_created (procfs_inferior_created
);
5350 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5351 _("Give a trace of entries into the syscall."));
5352 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5353 _("Give a trace of exits from the syscall."));
5354 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5355 _("Cancel a trace of entries into the syscall."));
5356 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5357 _("Cancel a trace of exits from the syscall."));
5360 /* =================== END, GDB "MODULE" =================== */
5364 /* miscellaneous stubs: */
5366 /* The following satisfy a few random symbols mostly created by the
5367 solaris threads implementation, which I will chase down later. */
5369 /* Return a pid for which we guarantee we will be able to find a
5373 procfs_first_available (void)
5375 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);
5378 /* =================== GCORE .NOTE "MODULE" =================== */
5379 #if defined (PIOCOPENLWP) || defined (PCAGENT)
5380 /* gcore only implemented on solaris (so far) */
5383 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
5384 char *note_data
, int *note_size
,
5385 enum gdb_signal stop_signal
)
5387 struct regcache
*regcache
= get_thread_regcache (ptid
);
5388 gdb_gregset_t gregs
;
5389 gdb_fpregset_t fpregs
;
5390 unsigned long merged_pid
;
5391 struct cleanup
*old_chain
;
5393 merged_pid
= ptid_get_lwp (ptid
) << 16 | ptid_get_pid (ptid
);
5395 /* This part is the old method for fetching registers.
5396 It should be replaced by the newer one using regsets
5397 once it is implemented in this platform:
5398 gdbarch_regset_from_core_section() and regset->collect_regset(). */
5400 old_chain
= save_inferior_ptid ();
5401 inferior_ptid
= ptid
;
5402 target_fetch_registers (regcache
, -1);
5404 fill_gregset (regcache
, &gregs
, -1);
5405 #if defined (NEW_PROC_API)
5406 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
5413 note_data
= (char *) elfcore_write_prstatus (obfd
,
5420 fill_fpregset (regcache
, &fpregs
, -1);
5421 note_data
= (char *) elfcore_write_prfpreg (obfd
,
5427 do_cleanups (old_chain
);
5432 struct procfs_corefile_thread_data
{
5436 enum gdb_signal stop_signal
;
5440 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
5442 struct procfs_corefile_thread_data
*args
= data
;
5446 ptid_t ptid
= ptid_build (pi
->pid
, thread
->tid
, 0);
5448 args
->note_data
= procfs_do_thread_registers (args
->obfd
, ptid
,
5457 find_signalled_thread (struct thread_info
*info
, void *data
)
5459 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
5460 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
5466 static enum gdb_signal
5467 find_stop_signal (void)
5469 struct thread_info
*info
=
5470 iterate_over_threads (find_signalled_thread
, NULL
);
5473 return info
->suspend
.stop_signal
;
5475 return GDB_SIGNAL_0
;
5479 procfs_make_note_section (struct target_ops
*self
, bfd
*obfd
, int *note_size
)
5481 struct cleanup
*old_chain
;
5482 gdb_gregset_t gregs
;
5483 gdb_fpregset_t fpregs
;
5484 char fname
[16] = {'\0'};
5485 char psargs
[80] = {'\0'};
5486 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
5487 char *note_data
= NULL
;
5489 struct procfs_corefile_thread_data thread_args
;
5492 enum gdb_signal stop_signal
;
5494 if (get_exec_file (0))
5496 strncpy (fname
, lbasename (get_exec_file (0)), sizeof (fname
));
5497 fname
[sizeof (fname
) - 1] = 0;
5498 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
5499 psargs
[sizeof (psargs
) - 1] = 0;
5501 inf_args
= get_inferior_args ();
5502 if (inf_args
&& *inf_args
&&
5503 strlen (inf_args
) < ((int) sizeof (psargs
) - (int) strlen (psargs
)))
5505 strncat (psargs
, " ",
5506 sizeof (psargs
) - strlen (psargs
));
5507 strncat (psargs
, inf_args
,
5508 sizeof (psargs
) - strlen (psargs
));
5512 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
5518 stop_signal
= find_stop_signal ();
5521 fill_gregset (get_current_regcache (), &gregs
, -1);
5522 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
5523 ptid_get_pid (inferior_ptid
),
5524 stop_signal
, &gregs
);
5527 thread_args
.obfd
= obfd
;
5528 thread_args
.note_data
= note_data
;
5529 thread_args
.note_size
= note_size
;
5530 thread_args
.stop_signal
= stop_signal
;
5531 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
,
5533 note_data
= thread_args
.note_data
;
5535 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
5539 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
5540 "CORE", NT_AUXV
, auxv
, auxv_len
);
5544 make_cleanup (xfree
, note_data
);
5547 #else /* !Solaris */
5549 procfs_make_note_section (struct target_ops
*self
, bfd
*obfd
, int *note_size
)
5551 error (_("gcore not implemented for this host."));
5552 return NULL
; /* lint */
5554 #endif /* Solaris */
5555 /* =================== END GCORE .NOTE "MODULE" =================== */