1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
3 Copyright (C) 1999-2017 Free Software Foundation, Inc.
5 Written by Michael Snyder at Cygnus Solutions.
6 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "elf-bfd.h" /* for elfcore_write_* */
30 #include "gdbthread.h"
32 #include "inf-child.h"
33 #include "nat/fork-inferior.h"
34 #include "filestuff.h"
36 #if defined (NEW_PROC_API)
37 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
40 #include <sys/procfs.h>
41 #ifdef HAVE_SYS_FAULT_H
42 #include <sys/fault.h>
44 #ifdef HAVE_SYS_SYSCALL_H
45 #include <sys/syscall.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
*, const 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_interrupt (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
*, const char *,
127 const std::string
&, char **, int);
128 static ptid_t
procfs_wait (struct target_ops
*,
129 ptid_t
, struct target_waitstatus
*, int);
130 static enum target_xfer_status
procfs_xfer_memory (gdb_byte
*,
134 static target_xfer_partial_ftype procfs_xfer_partial
;
136 static int procfs_thread_alive (struct target_ops
*ops
, ptid_t
);
138 static void procfs_update_thread_list (struct target_ops
*ops
);
139 static const char *procfs_pid_to_str (struct target_ops
*, ptid_t
);
141 static int proc_find_memory_regions (struct target_ops
*self
,
142 find_memory_region_ftype
, void *);
144 static char * procfs_make_note_section (struct target_ops
*self
,
147 static int procfs_can_use_hw_breakpoint (struct target_ops
*self
,
148 enum bptype
, int, int);
150 static void procfs_info_proc (struct target_ops
*, const char *,
151 enum info_proc_what
);
153 #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
154 /* When GDB is built as 64-bit application on Solaris, the auxv data
155 is presented in 64-bit format. We need to provide a custom parser
158 procfs_auxv_parse (struct target_ops
*ops
, gdb_byte
**readptr
,
159 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
161 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
162 gdb_byte
*ptr
= *readptr
;
167 if (endptr
- ptr
< 8 * 2)
170 *typep
= extract_unsigned_integer (ptr
, 4, byte_order
);
172 /* The size of data is always 64-bit. If the application is 32-bit,
173 it will be zero extended, as expected. */
174 *valp
= extract_unsigned_integer (ptr
, 8, byte_order
);
185 struct target_ops
*t
= inf_child_target ();
187 t
->to_create_inferior
= procfs_create_inferior
;
188 t
->to_kill
= procfs_kill_inferior
;
189 t
->to_mourn_inferior
= procfs_mourn_inferior
;
190 t
->to_attach
= procfs_attach
;
191 t
->to_detach
= procfs_detach
;
192 t
->to_wait
= procfs_wait
;
193 t
->to_resume
= procfs_resume
;
194 t
->to_fetch_registers
= procfs_fetch_registers
;
195 t
->to_store_registers
= procfs_store_registers
;
196 t
->to_xfer_partial
= procfs_xfer_partial
;
197 t
->to_pass_signals
= procfs_pass_signals
;
198 t
->to_files_info
= procfs_files_info
;
199 t
->to_interrupt
= procfs_interrupt
;
201 t
->to_update_thread_list
= procfs_update_thread_list
;
202 t
->to_thread_alive
= procfs_thread_alive
;
203 t
->to_pid_to_str
= procfs_pid_to_str
;
205 t
->to_has_thread_control
= tc_schedlock
;
206 t
->to_find_memory_regions
= proc_find_memory_regions
;
207 t
->to_make_corefile_notes
= procfs_make_note_section
;
208 t
->to_info_proc
= procfs_info_proc
;
210 #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
211 t
->to_auxv_parse
= procfs_auxv_parse
;
214 t
->to_magic
= OPS_MAGIC
;
219 /* =================== END, TARGET_OPS "MODULE" =================== */
221 /* World Unification:
223 Put any typedefs, defines etc. here that are required for the
224 unification of code that handles different versions of /proc. */
226 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
228 enum { READ_WATCHFLAG
= WA_READ
,
229 WRITE_WATCHFLAG
= WA_WRITE
,
230 EXEC_WATCHFLAG
= WA_EXEC
,
231 AFTER_WATCHFLAG
= WA_TRAPAFTER
234 #else /* Irix method for watchpoints */
235 enum { READ_WATCHFLAG
= MA_READ
,
236 WRITE_WATCHFLAG
= MA_WRITE
,
237 EXEC_WATCHFLAG
= MA_EXEC
,
238 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
243 #ifdef HAVE_PR_SIGSET_T
244 typedef pr_sigset_t gdb_sigset_t
;
246 typedef sigset_t gdb_sigset_t
;
250 #ifdef HAVE_PR_SIGACTION64_T
251 typedef pr_sigaction64_t gdb_sigaction_t
;
253 typedef struct sigaction gdb_sigaction_t
;
257 #ifdef HAVE_PR_SIGINFO64_T
258 typedef pr_siginfo64_t gdb_siginfo_t
;
260 typedef siginfo_t gdb_siginfo_t
;
263 /* On mips-irix, praddset and prdelset are defined in such a way that
264 they return a value, which causes GCC to emit a -Wunused error
265 because the returned value is not used. Prevent this warning
266 by casting the return value to void. On sparc-solaris, this issue
267 does not exist because the definition of these macros already include
268 that cast to void. */
269 #define gdb_praddset(sp, flag) ((void) praddset (sp, flag))
270 #define gdb_prdelset(sp, flag) ((void) prdelset (sp, flag))
272 /* gdb_premptysysset */
274 #define gdb_premptysysset premptysysset
276 #define gdb_premptysysset premptyset
281 #define gdb_praddsysset praddsysset
283 #define gdb_praddsysset gdb_praddset
288 #define gdb_prdelsysset prdelsysset
290 #define gdb_prdelsysset gdb_prdelset
293 /* prissyssetmember */
294 #ifdef prissyssetmember
295 #define gdb_pr_issyssetmember prissyssetmember
297 #define gdb_pr_issyssetmember prismember
300 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
301 as intuitively descriptive as it could be, so we'll define
302 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
303 this writing, this feature is only found on AIX5 systems and
304 basically means that the set of syscalls is not fixed. I.e,
305 there's no nice table that one can #include to get all of the
306 syscall numbers. Instead, they're stored in /proc/PID/sysent
307 for each process. We are at least guaranteed that they won't
308 change over the lifetime of the process. But each process could
309 (in theory) have different syscall numbers. */
310 #ifdef HAVE_PRSYSENT_T
311 #define DYNAMIC_SYSCALLS
316 /* =================== STRUCT PROCINFO "MODULE" =================== */
318 /* FIXME: this comment will soon be out of date W.R.T. threads. */
320 /* The procinfo struct is a wrapper to hold all the state information
321 concerning a /proc process. There should be exactly one procinfo
322 for each process, and since GDB currently can debug only one
323 process at a time, that means there should be only one procinfo.
324 All of the LWP's of a process can be accessed indirectly thru the
325 single process procinfo.
327 However, against the day when GDB may debug more than one process,
328 this data structure is kept in a list (which for now will hold no
329 more than one member), and many functions will have a pointer to a
330 procinfo as an argument.
332 There will be a separate procinfo structure for use by the (not yet
333 implemented) "info proc" command, so that we can print useful
334 information about any random process without interfering with the
335 inferior's procinfo information. */
338 /* format strings for /proc paths */
339 # ifndef CTL_PROC_NAME_FMT
340 # define MAIN_PROC_NAME_FMT "/proc/%d"
341 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
342 # define AS_PROC_NAME_FMT "/proc/%d/as"
343 # define MAP_PROC_NAME_FMT "/proc/%d/map"
344 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
345 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
347 /* the name of the proc status struct depends on the implementation */
348 typedef pstatus_t gdb_prstatus_t
;
349 typedef lwpstatus_t gdb_lwpstatus_t
;
350 #else /* ! NEW_PROC_API */
351 /* format strings for /proc paths */
352 # ifndef CTL_PROC_NAME_FMT
353 # define MAIN_PROC_NAME_FMT "/proc/%05d"
354 # define CTL_PROC_NAME_FMT "/proc/%05d"
355 # define AS_PROC_NAME_FMT "/proc/%05d"
356 # define MAP_PROC_NAME_FMT "/proc/%05d"
357 # define STATUS_PROC_NAME_FMT "/proc/%05d"
358 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
360 /* The name of the proc status struct depends on the implementation. */
361 typedef prstatus_t gdb_prstatus_t
;
362 typedef prstatus_t gdb_lwpstatus_t
;
363 #endif /* NEW_PROC_API */
365 typedef struct procinfo
{
366 struct procinfo
*next
;
367 int pid
; /* Process ID */
368 int tid
; /* Thread/LWP id */
372 int ignore_next_sigstop
;
374 /* The following four fd fields may be identical, or may contain
375 several different fd's, depending on the version of /proc
376 (old ioctl or new read/write). */
378 int ctl_fd
; /* File descriptor for /proc control file */
380 /* The next three file descriptors are actually only needed in the
381 read/write, multiple-file-descriptor implemenation
382 (NEW_PROC_API). However, to avoid a bunch of #ifdefs in the
383 code, we will use them uniformly by (in the case of the ioctl
384 single-file-descriptor implementation) filling them with copies
385 of the control fd. */
386 int status_fd
; /* File descriptor for /proc status file */
387 int as_fd
; /* File descriptor for /proc as file */
389 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
391 fltset_t saved_fltset
; /* Saved traced hardware fault set */
392 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
393 gdb_sigset_t saved_sighold
; /* Saved held signal set */
394 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
395 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
397 gdb_prstatus_t prstatus
; /* Current process status info */
400 gdb_fpregset_t fpregset
; /* Current floating point registers */
403 #ifdef DYNAMIC_SYSCALLS
404 int num_syscalls
; /* Total number of syscalls */
405 char **syscall_names
; /* Syscall number to name map */
408 struct procinfo
*thread_list
;
410 int status_valid
: 1;
412 int fpregs_valid
: 1;
413 int threads_valid
: 1;
416 static char errmsg
[128]; /* shared error msg buffer */
418 /* Function prototypes for procinfo module: */
420 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
421 static procinfo
*find_procinfo (int pid
, int tid
);
422 static procinfo
*create_procinfo (int pid
, int tid
);
423 static void destroy_procinfo (procinfo
* p
);
424 static void do_destroy_procinfo_cleanup (void *);
425 static void dead_procinfo (procinfo
* p
, const char *msg
, int killp
);
426 static int open_procinfo_files (procinfo
* p
, int which
);
427 static void close_procinfo_files (procinfo
* p
);
428 static int sysset_t_size (procinfo
*p
);
429 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
430 #ifdef DYNAMIC_SYSCALLS
431 static void load_syscalls (procinfo
*pi
);
432 static void free_syscalls (procinfo
*pi
);
433 static int find_syscall (procinfo
*pi
, const char *name
);
434 #endif /* DYNAMIC_SYSCALLS */
436 static int iterate_over_mappings
437 (procinfo
*pi
, find_memory_region_ftype child_func
, void *data
,
438 int (*func
) (struct prmap
*map
, find_memory_region_ftype child_func
,
441 /* The head of the procinfo list: */
442 static procinfo
* procinfo_list
;
444 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
448 find_procinfo (int pid
, int tid
)
452 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
459 /* Don't check threads_valid. If we're updating the
460 thread_list, we want to find whatever threads are already
461 here. This means that in general it is the caller's
462 responsibility to check threads_valid and update before
463 calling find_procinfo, if the caller wants to find a new
466 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
474 /* Calls find_procinfo, but errors on failure. */
477 find_procinfo_or_die (int pid
, int tid
)
479 procinfo
*pi
= find_procinfo (pid
, tid
);
484 error (_("procfs: couldn't find pid %d "
485 "(kernel thread %d) in procinfo list."),
488 error (_("procfs: couldn't find pid %d in procinfo list."), pid
);
493 /* Wrapper for `open'. The appropriate open call is attempted; if
494 unsuccessful, it will be retried as many times as needed for the
495 EAGAIN and EINTR conditions.
497 For other conditions, retry the open a limited number of times. In
498 addition, a short sleep is imposed prior to retrying the open. The
499 reason for this sleep is to give the kernel a chance to catch up
500 and create the file in question in the event that GDB "wins" the
501 race to open a file before the kernel has created it. */
504 open_with_retry (const char *pathname
, int flags
)
506 int retries_remaining
, status
;
508 retries_remaining
= 2;
512 status
= open (pathname
, flags
);
514 if (status
>= 0 || retries_remaining
== 0)
516 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
526 /* Open the file descriptor for the process or LWP. If NEW_PROC_API
527 is defined, we only open the control file descriptor; the others
528 are opened lazily as needed. Otherwise (if not NEW_PROC_API),
529 there is only one real file descriptor, but we keep multiple copies
530 of it so that the code that uses them does not have to be #ifdef'd.
531 Returns the file descriptor, or zero for failure. */
533 enum { FD_CTL
, FD_STATUS
, FD_AS
};
536 open_procinfo_files (procinfo
*pi
, int which
)
539 char tmp
[MAX_PROC_NAME_SIZE
];
543 /* This function is getting ALMOST long enough to break up into
544 several. Here is some rationale:
546 NEW_PROC_API (Solaris 2.6, Solaris 2.7):
547 There are several file descriptors that may need to be open
548 for any given process or LWP. The ones we're intereted in are:
549 - control (ctl) write-only change the state
550 - status (status) read-only query the state
551 - address space (as) read/write access memory
552 - map (map) read-only virtual addr map
553 Most of these are opened lazily as they are needed.
554 The pathnames for the 'files' for an LWP look slightly
555 different from those of a first-class process:
556 Pathnames for a process (<proc-id>):
558 /proc/<proc-id>/status
561 Pathnames for an LWP (lwp-id):
562 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
563 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
564 An LWP has no map or address space file descriptor, since
565 the memory map and address space are shared by all LWPs.
567 Everyone else (Solaris 2.5, Irix, OSF)
568 There is only one file descriptor for each process or LWP.
569 For convenience, we copy the same file descriptor into all
570 three fields of the procinfo struct (ctl_fd, status_fd, and
571 as_fd, see NEW_PROC_API above) so that code that uses them
572 doesn't need any #ifdef's.
577 Each LWP has an independent file descriptor, but these
578 are not obtained via the 'open' system call like the rest:
579 instead, they're obtained thru an ioctl call (PIOCOPENLWP)
580 to the file descriptor of the parent process.
583 These do not even have their own independent file descriptor.
584 All operations are carried out on the file descriptor of the
585 parent process. Therefore we just call open again for each
586 thread, getting a new handle for the same 'file'. */
589 /* In this case, there are several different file descriptors that
590 we might be asked to open. The control file descriptor will be
591 opened early, but the others will be opened lazily as they are
594 strcpy (tmp
, pi
->pathname
);
595 switch (which
) { /* Which file descriptor to open? */
598 strcat (tmp
, "/lwpctl");
600 strcat (tmp
, "/ctl");
601 fd
= open_with_retry (tmp
, O_WRONLY
);
608 return 0; /* There is no 'as' file descriptor for an lwp. */
610 fd
= open_with_retry (tmp
, O_RDWR
);
617 strcat (tmp
, "/lwpstatus");
619 strcat (tmp
, "/status");
620 fd
= open_with_retry (tmp
, O_RDONLY
);
626 return 0; /* unknown file descriptor */
628 #else /* not NEW_PROC_API */
629 /* In this case, there is only one file descriptor for each procinfo
630 (ie. each process or LWP). In fact, only the file descriptor for
631 the process can actually be opened by an 'open' system call. The
632 ones for the LWPs have to be obtained thru an IOCTL call on the
633 process's file descriptor.
635 For convenience, we copy each procinfo's single file descriptor
636 into all of the fields occupied by the several file descriptors
637 of the NEW_PROC_API implementation. That way, the code that uses
638 them can be written without ifdefs. */
641 #ifdef PIOCTSTATUS /* OSF */
642 /* Only one FD; just open it. */
643 if ((fd
= open_with_retry (pi
->pathname
, O_RDWR
)) < 0)
645 #else /* Sol 2.5, Irix, other? */
646 if (pi
->tid
== 0) /* Master procinfo for the process */
648 fd
= open_with_retry (pi
->pathname
, O_RDWR
);
652 else /* LWP thread procinfo */
654 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
658 /* Find the procinfo for the entire process. */
659 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
662 /* Now obtain the file descriptor for the LWP. */
663 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) < 0)
665 #else /* Irix, other? */
666 return 0; /* Don't know how to open threads. */
667 #endif /* Sol 2.5 PIOCOPENLWP */
669 #endif /* OSF PIOCTSTATUS */
670 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
671 #endif /* NEW_PROC_API */
673 return 1; /* success */
676 /* Allocate a data structure and link it into the procinfo list.
677 First tries to find a pre-existing one (FIXME: why?). Returns the
678 pointer to new procinfo struct. */
681 create_procinfo (int pid
, int tid
)
683 procinfo
*pi
, *parent
= NULL
;
685 if ((pi
= find_procinfo (pid
, tid
)))
686 return pi
; /* Already exists, nothing to do. */
688 /* Find parent before doing malloc, to save having to cleanup. */
690 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
692 doesn't exist yet? */
694 pi
= XNEW (procinfo
);
695 memset (pi
, 0, sizeof (procinfo
));
699 #ifdef DYNAMIC_SYSCALLS
703 pi
->saved_entryset
= sysset_t_alloc (pi
);
704 pi
->saved_exitset
= sysset_t_alloc (pi
);
706 /* Chain into list. */
709 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
710 pi
->next
= procinfo_list
;
716 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
718 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
720 pi
->next
= parent
->thread_list
;
721 parent
->thread_list
= pi
;
726 /* Close all file descriptors associated with the procinfo. */
729 close_procinfo_files (procinfo
*pi
)
736 if (pi
->status_fd
> 0)
737 close (pi
->status_fd
);
739 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
742 /* Destructor function. Close, unlink and deallocate the object. */
745 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
749 /* Step one: unlink the procinfo from its list. */
753 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
756 ptr
->next
= pi
->next
;
760 /* Step two: close any open file descriptors. */
761 close_procinfo_files (pi
);
763 /* Step three: free the memory. */
764 #ifdef DYNAMIC_SYSCALLS
767 xfree (pi
->saved_entryset
);
768 xfree (pi
->saved_exitset
);
773 destroy_procinfo (procinfo
*pi
)
777 if (pi
->tid
!= 0) /* Destroy a thread procinfo. */
779 tmp
= find_procinfo (pi
->pid
, 0); /* Find the parent process. */
780 destroy_one_procinfo (&tmp
->thread_list
, pi
);
782 else /* Destroy a process procinfo and all its threads. */
784 /* First destroy the children, if any; */
785 while (pi
->thread_list
!= NULL
)
786 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
787 /* Then destroy the parent. Genocide!!! */
788 destroy_one_procinfo (&procinfo_list
, pi
);
793 do_destroy_procinfo_cleanup (void *pi
)
795 destroy_procinfo ((procinfo
*) pi
);
798 enum { NOKILL
, KILL
};
800 /* To be called on a non_recoverable error for a procinfo. Prints
801 error messages, optionally sends a SIGKILL to the process, then
802 destroys the data structure. */
805 dead_procinfo (procinfo
*pi
, const char *msg
, int kill_p
)
811 print_sys_errmsg (pi
->pathname
, errno
);
815 sprintf (procfile
, "process %d", pi
->pid
);
816 print_sys_errmsg (procfile
, errno
);
819 kill (pi
->pid
, SIGKILL
);
821 destroy_procinfo (pi
);
825 /* Returns the (complete) size of a sysset_t struct. Normally, this
826 is just sizeof (sysset_t), but in the case of Monterey/64, the
827 actual size of sysset_t isn't known until runtime. */
830 sysset_t_size (procinfo
* pi
)
832 #ifndef DYNAMIC_SYSCALLS
833 return sizeof (sysset_t
);
835 return sizeof (sysset_t
) - sizeof (uint64_t)
836 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
837 / (8 * sizeof (uint64_t)));
841 /* Allocate and (partially) initialize a sysset_t struct. */
844 sysset_t_alloc (procinfo
* pi
)
847 int size
= sysset_t_size (pi
);
849 ret
= (sysset_t
*) xmalloc (size
);
850 #ifdef DYNAMIC_SYSCALLS
851 ret
->pr_size
= ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
852 / (8 * sizeof (uint64_t)));
857 #ifdef DYNAMIC_SYSCALLS
859 /* Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
860 pi->num_syscalls with the number of syscalls and pi->syscall_names
861 with the names. (Certain numbers may be skipped in which case the
862 names for these numbers will be left as NULL.) */
864 #define MAX_SYSCALL_NAME_LENGTH 256
865 #define MAX_SYSCALLS 65536
868 load_syscalls (procinfo
*pi
)
870 char pathname
[MAX_PROC_NAME_SIZE
];
873 prsyscall_t
*syscalls
;
874 int i
, size
, maxcall
;
875 struct cleanup
*cleanups
;
877 pi
->num_syscalls
= 0;
878 pi
->syscall_names
= 0;
880 /* Open the file descriptor for the sysent file. */
881 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
882 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
885 error (_("load_syscalls: Can't open /proc/%d/sysent"), pi
->pid
);
887 cleanups
= make_cleanup_close (sysent_fd
);
889 size
= sizeof header
- sizeof (prsyscall_t
);
890 if (read (sysent_fd
, &header
, size
) != size
)
892 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi
->pid
);
895 if (header
.pr_nsyscalls
== 0)
897 error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"),
901 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
902 syscalls
= xmalloc (size
);
903 make_cleanup (free_current_contents
, &syscalls
);
905 if (read (sysent_fd
, syscalls
, size
) != size
)
906 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi
->pid
);
908 /* Find maximum syscall number. This may not be the same as
909 pr_nsyscalls since that value refers to the number of entries
910 in the table. (Also, the docs indicate that some system
911 call numbers may be skipped.) */
913 maxcall
= syscalls
[0].pr_number
;
915 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
916 if (syscalls
[i
].pr_number
> maxcall
917 && syscalls
[i
].pr_nameoff
> 0
918 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
919 maxcall
= syscalls
[i
].pr_number
;
921 pi
->num_syscalls
= maxcall
+1;
922 pi
->syscall_names
= XNEWVEC (char *, pi
->num_syscalls
);
924 for (i
= 0; i
< pi
->num_syscalls
; i
++)
925 pi
->syscall_names
[i
] = NULL
;
927 /* Read the syscall names in. */
928 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
930 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
934 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
935 || syscalls
[i
].pr_number
< 0
936 || syscalls
[i
].pr_nameoff
<= 0
937 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
938 != (off_t
) syscalls
[i
].pr_nameoff
))
941 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
945 callnum
= syscalls
[i
].pr_number
;
947 if (pi
->syscall_names
[callnum
] != NULL
)
949 /* FIXME: Generate warning. */
953 namebuf
[nread
-1] = '\0';
954 size
= strlen (namebuf
) + 1;
955 pi
->syscall_names
[callnum
] = xmalloc (size
);
956 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
957 pi
->syscall_names
[callnum
][size
-1] = '\0';
960 do_cleanups (cleanups
);
963 /* Free the space allocated for the syscall names from the procinfo
967 free_syscalls (procinfo
*pi
)
969 if (pi
->syscall_names
)
973 for (i
= 0; i
< pi
->num_syscalls
; i
++)
974 if (pi
->syscall_names
[i
] != NULL
)
975 xfree (pi
->syscall_names
[i
]);
977 xfree (pi
->syscall_names
);
978 pi
->syscall_names
= 0;
982 /* Given a name, look up (and return) the corresponding syscall number.
983 If no match is found, return -1. */
986 find_syscall (procinfo
*pi
, const char *name
)
990 for (i
= 0; i
< pi
->num_syscalls
; i
++)
992 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
999 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1001 /* =================== /proc "MODULE" =================== */
1003 /* This "module" is the interface layer between the /proc system API
1004 and the gdb target vector functions. This layer consists of access
1005 functions that encapsulate each of the basic operations that we
1006 need to use from the /proc API.
1008 The main motivation for this layer is to hide the fact that there
1009 are two very different implementations of the /proc API. Rather
1010 than have a bunch of #ifdefs all thru the gdb target vector
1011 functions, we do our best to hide them all in here. */
1013 static long proc_flags (procinfo
* pi
);
1014 static int proc_why (procinfo
* pi
);
1015 static int proc_what (procinfo
* pi
);
1016 static int proc_set_current_signal (procinfo
* pi
, int signo
);
1017 static int proc_get_current_thread (procinfo
* pi
);
1018 static int proc_iterate_over_threads
1020 int (*func
) (procinfo
*, procinfo
*, void *),
1024 proc_warn (procinfo
*pi
, const char *func
, int line
)
1026 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1027 print_sys_errmsg (errmsg
, errno
);
1031 proc_error (procinfo
*pi
, const char *func
, int line
)
1033 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1034 perror_with_name (errmsg
);
1037 /* Updates the status struct in the procinfo. There is a 'valid'
1038 flag, to let other functions know when this function needs to be
1039 called (so the status is only read when it is needed). The status
1040 file descriptor is also only opened when it is needed. Returns
1041 non-zero for success, zero for failure. */
1044 proc_get_status (procinfo
*pi
)
1046 /* Status file descriptor is opened "lazily". */
1047 if (pi
->status_fd
== 0 &&
1048 open_procinfo_files (pi
, FD_STATUS
) == 0)
1050 pi
->status_valid
= 0;
1055 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1056 pi
->status_valid
= 0; /* fail */
1059 /* Sigh... I have to read a different data structure,
1060 depending on whether this is a main process or an LWP. */
1062 pi
->status_valid
= (read (pi
->status_fd
,
1063 (char *) &pi
->prstatus
.pr_lwp
,
1064 sizeof (lwpstatus_t
))
1065 == sizeof (lwpstatus_t
));
1068 pi
->status_valid
= (read (pi
->status_fd
,
1069 (char *) &pi
->prstatus
,
1070 sizeof (gdb_prstatus_t
))
1071 == sizeof (gdb_prstatus_t
));
1074 #else /* ioctl method */
1075 #ifdef PIOCTSTATUS /* osf */
1076 if (pi
->tid
== 0) /* main process */
1078 /* Just read the danged status. Now isn't that simple? */
1080 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1087 tid_t pr_error_thread
;
1088 struct prstatus status
;
1091 thread_status
.pr_count
= 1;
1092 thread_status
.status
.pr_tid
= pi
->tid
;
1093 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1096 memcpy (&pi
->prstatus
, &thread_status
.status
,
1097 sizeof (pi
->prstatus
));
1098 pi
->status_valid
= 1;
1102 /* Just read the danged status. Now isn't that simple? */
1103 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1107 if (pi
->status_valid
)
1109 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1112 proc_get_current_thread (pi
));
1115 /* The status struct includes general regs, so mark them valid too. */
1116 pi
->gregs_valid
= pi
->status_valid
;
1118 /* In the read/write multiple-fd model, the status struct includes
1119 the fp regs too, so mark them valid too. */
1120 pi
->fpregs_valid
= pi
->status_valid
;
1122 return pi
->status_valid
; /* True if success, false if failure. */
1125 /* Returns the process flags (pr_flags field). */
1128 proc_flags (procinfo
*pi
)
1130 if (!pi
->status_valid
)
1131 if (!proc_get_status (pi
))
1132 return 0; /* FIXME: not a good failure value (but what is?) */
1135 return pi
->prstatus
.pr_lwp
.pr_flags
;
1137 return pi
->prstatus
.pr_flags
;
1141 /* Returns the pr_why field (why the process stopped). */
1144 proc_why (procinfo
*pi
)
1146 if (!pi
->status_valid
)
1147 if (!proc_get_status (pi
))
1148 return 0; /* FIXME: not a good failure value (but what is?) */
1151 return pi
->prstatus
.pr_lwp
.pr_why
;
1153 return pi
->prstatus
.pr_why
;
1157 /* Returns the pr_what field (details of why the process stopped). */
1160 proc_what (procinfo
*pi
)
1162 if (!pi
->status_valid
)
1163 if (!proc_get_status (pi
))
1164 return 0; /* FIXME: not a good failure value (but what is?) */
1167 return pi
->prstatus
.pr_lwp
.pr_what
;
1169 return pi
->prstatus
.pr_what
;
1173 /* This function is only called when PI is stopped by a watchpoint.
1174 Assuming the OS supports it, write to *ADDR the data address which
1175 triggered it and return 1. Return 0 if it is not possible to know
1179 proc_watchpoint_address (procinfo
*pi
, CORE_ADDR
*addr
)
1181 if (!pi
->status_valid
)
1182 if (!proc_get_status (pi
))
1186 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
1187 builtin_type (target_gdbarch ())->builtin_data_ptr
,
1188 (gdb_byte
*) &pi
->prstatus
.pr_lwp
.pr_info
.si_addr
);
1190 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
1191 builtin_type (target_gdbarch ())->builtin_data_ptr
,
1192 (gdb_byte
*) &pi
->prstatus
.pr_info
.si_addr
);
1197 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1199 /* Returns the pr_nsysarg field (number of args to the current
1203 proc_nsysarg (procinfo
*pi
)
1205 if (!pi
->status_valid
)
1206 if (!proc_get_status (pi
))
1210 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1212 return pi
->prstatus
.pr_nsysarg
;
1216 /* Returns the pr_sysarg field (pointer to the arguments of current
1220 proc_sysargs (procinfo
*pi
)
1222 if (!pi
->status_valid
)
1223 if (!proc_get_status (pi
))
1227 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1229 return (long *) &pi
->prstatus
.pr_sysarg
;
1232 #endif /* PIOCSSPCACT */
1234 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
1235 /* Returns the pr_cursig field (current signal). */
1238 proc_cursig (struct procinfo
*pi
)
1240 if (!pi
->status_valid
)
1241 if (!proc_get_status (pi
))
1242 return 0; /* FIXME: not a good failure value (but what is?) */
1245 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1247 return pi
->prstatus
.pr_cursig
;
1250 #endif /* PROCFS_DONT_PIOCSSIG_CURSIG */
1252 /* === I appologize for the messiness of this function.
1253 === This is an area where the different versions of
1254 === /proc are more inconsistent than usual.
1256 Set or reset any of the following process flags:
1257 PR_FORK -- forked child will inherit trace flags
1258 PR_RLC -- traced process runs when last /proc file closed.
1259 PR_KLC -- traced process is killed when last /proc file closed.
1260 PR_ASYNC -- LWP's get to run/stop independently.
1262 There are three methods for doing this function:
1263 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1265 2) Middle: PIOCSET/PIOCRESET
1267 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1270 Note: Irix does not define PR_ASYNC.
1271 Note: OSF does not define PR_KLC.
1272 Note: OSF is the only one that can ONLY use the oldest method.
1276 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1277 mode -- 1 for set, 0 for reset.
1279 Returns non-zero for success, zero for failure. */
1281 enum { FLAG_RESET
, FLAG_SET
};
1284 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1286 long win
= 0; /* default to fail */
1288 /* These operations affect the process as a whole, and applying them
1289 to an individual LWP has the same meaning as applying them to the
1290 main process. Therefore, if we're ever called with a pointer to
1291 an LWP's procinfo, let's substitute the process's procinfo and
1292 avoid opening the LWP's file descriptor unnecessarily. */
1295 pi
= find_procinfo_or_die (pi
->pid
, 0);
1297 #ifdef NEW_PROC_API /* Newest method: Newer Solarii. */
1298 /* First normalize the PCUNSET/PCRESET command opcode
1299 (which for no obvious reason has a different definition
1300 from one operating system to the next...) */
1302 #define GDBRESET PCUNSET
1305 #define GDBRESET PCRESET
1309 procfs_ctl_t arg
[2];
1311 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC). */
1313 else /* Reset the flag. */
1317 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1320 #ifdef PIOCSET /* Irix/Sol5 method */
1321 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC). */
1323 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1325 else /* Reset the flag. */
1327 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1331 #ifdef PIOCSRLC /* Oldest method: OSF */
1334 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1336 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1338 else /* Clear run-on-last-close */
1340 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1344 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1346 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1348 else /* Clear inherit-on-fork */
1350 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1354 win
= 0; /* Fail -- unknown flag (can't do PR_ASYNC). */
1361 /* The above operation renders the procinfo's cached pstatus
1363 pi
->status_valid
= 0;
1366 warning (_("procfs: modify_flag failed to turn %s %s"),
1367 flag
== PR_FORK
? "PR_FORK" :
1368 flag
== PR_RLC
? "PR_RLC" :
1370 flag
== PR_ASYNC
? "PR_ASYNC" :
1373 flag
== PR_KLC
? "PR_KLC" :
1376 mode
== FLAG_RESET
? "off" : "on");
1381 /* Set the run_on_last_close flag. Process with all threads will
1382 become runnable when debugger closes all /proc fds. Returns
1383 non-zero for success, zero for failure. */
1386 proc_set_run_on_last_close (procinfo
*pi
)
1388 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1391 /* Reset the run_on_last_close flag. The process will NOT become
1392 runnable when debugger closes its file handles. Returns non-zero
1393 for success, zero for failure. */
1396 proc_unset_run_on_last_close (procinfo
*pi
)
1398 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1401 /* Reset inherit_on_fork flag. If the process forks a child while we
1402 are registered for events in the parent, then we will NOT recieve
1403 events from the child. Returns non-zero for success, zero for
1407 proc_unset_inherit_on_fork (procinfo
*pi
)
1409 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1413 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
1414 (signal etc.), the remaining LWPs will continue to run. Returns
1415 non-zero for success, zero for failure. */
1418 proc_set_async (procinfo
*pi
)
1420 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1423 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
1424 (signal etc.), then all other LWPs will stop as well. Returns
1425 non-zero for success, zero for failure. */
1428 proc_unset_async (procinfo
*pi
)
1430 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1432 #endif /* PR_ASYNC */
1434 /* Request the process/LWP to stop. Does not wait. Returns non-zero
1435 for success, zero for failure. */
1438 proc_stop_process (procinfo
*pi
)
1442 /* We might conceivably apply this operation to an LWP, and the
1443 LWP's ctl file descriptor might not be open. */
1445 if (pi
->ctl_fd
== 0 &&
1446 open_procinfo_files (pi
, FD_CTL
) == 0)
1451 procfs_ctl_t cmd
= PCSTOP
;
1453 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1454 #else /* ioctl method */
1455 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1456 /* Note: the call also reads the prstatus. */
1459 pi
->status_valid
= 1;
1460 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1463 proc_get_current_thread (pi
));
1471 /* Wait for the process or LWP to stop (block until it does). Returns
1472 non-zero for success, zero for failure. */
1475 proc_wait_for_stop (procinfo
*pi
)
1479 /* We should never have to apply this operation to any procinfo
1480 except the one for the main process. If that ever changes for
1481 any reason, then take out the following clause and replace it
1482 with one that makes sure the ctl_fd is open. */
1485 pi
= find_procinfo_or_die (pi
->pid
, 0);
1489 procfs_ctl_t cmd
= PCWSTOP
;
1491 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1492 /* We been runnin' and we stopped -- need to update status. */
1493 pi
->status_valid
= 0;
1495 #else /* ioctl method */
1496 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1497 /* Above call also refreshes the prstatus. */
1500 pi
->status_valid
= 1;
1501 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1504 proc_get_current_thread (pi
));
1511 /* Make the process or LWP runnable.
1513 Options (not all are implemented):
1515 - clear current fault
1516 - clear current signal
1517 - abort the current system call
1518 - stop as soon as finished with system call
1519 - (ioctl): set traced signal set
1520 - (ioctl): set held signal set
1521 - (ioctl): set traced fault set
1522 - (ioctl): set start pc (vaddr)
1524 Always clears the current fault. PI is the process or LWP to
1525 operate on. If STEP is true, set the process or LWP to trap after
1526 one instruction. If SIGNO is zero, clear the current signal if
1527 any; if non-zero, set the current signal to this one. Returns
1528 non-zero for success, zero for failure. */
1531 proc_run_process (procinfo
*pi
, int step
, int signo
)
1536 /* We will probably have to apply this operation to individual
1537 threads, so make sure the control file descriptor is open. */
1539 if (pi
->ctl_fd
== 0 &&
1540 open_procinfo_files (pi
, FD_CTL
) == 0)
1545 runflags
= PRCFAULT
; /* Always clear current fault. */
1550 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals. */
1551 proc_set_current_signal (pi
, signo
);
1555 procfs_ctl_t cmd
[2];
1559 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1561 #else /* ioctl method */
1565 memset (&prrun
, 0, sizeof (prrun
));
1566 prrun
.pr_flags
= runflags
;
1567 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1574 /* Register to trace signals in the process or LWP. Returns non-zero
1575 for success, zero for failure. */
1578 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1582 /* We should never have to apply this operation to any procinfo
1583 except the one for the main process. If that ever changes for
1584 any reason, then take out the following clause and replace it
1585 with one that makes sure the ctl_fd is open. */
1588 pi
= find_procinfo_or_die (pi
->pid
, 0);
1594 /* Use char array to avoid alignment issues. */
1595 char sigset
[sizeof (gdb_sigset_t
)];
1599 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1601 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1603 #else /* ioctl method */
1604 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1606 /* The above operation renders the procinfo's cached pstatus obsolete. */
1607 pi
->status_valid
= 0;
1610 warning (_("procfs: set_traced_signals failed"));
1614 /* Register to trace hardware faults in the process or LWP. Returns
1615 non-zero for success, zero for failure. */
1618 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1622 /* We should never have to apply this operation to any procinfo
1623 except the one for the main process. If that ever changes for
1624 any reason, then take out the following clause and replace it
1625 with one that makes sure the ctl_fd is open. */
1628 pi
= find_procinfo_or_die (pi
->pid
, 0);
1634 /* Use char array to avoid alignment issues. */
1635 char fltset
[sizeof (fltset_t
)];
1639 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1641 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1643 #else /* ioctl method */
1644 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1646 /* The above operation renders the procinfo's cached pstatus obsolete. */
1647 pi
->status_valid
= 0;
1652 /* Register to trace entry to system calls in the process or LWP.
1653 Returns non-zero for success, zero for failure. */
1656 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1660 /* We should never have to apply this operation to any procinfo
1661 except the one for the main process. If that ever changes for
1662 any reason, then take out the following clause and replace it
1663 with one that makes sure the ctl_fd is open. */
1666 pi
= find_procinfo_or_die (pi
->pid
, 0);
1670 struct gdb_proc_ctl_pcsentry
{
1672 /* Use char array to avoid alignment issues. */
1673 char sysset
[sizeof (sysset_t
)];
1675 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1677 + sysset_t_size (pi
);
1679 argp
= (struct gdb_proc_ctl_pcsentry
*) xmalloc (argp_size
);
1681 argp
->cmd
= PCSENTRY
;
1682 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1684 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1687 #else /* ioctl method */
1688 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1690 /* The above operation renders the procinfo's cached pstatus
1692 pi
->status_valid
= 0;
1697 /* Register to trace exit from system calls in the process or LWP.
1698 Returns non-zero for success, zero for failure. */
1701 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1705 /* We should never have to apply this operation to any procinfo
1706 except the one for the main process. If that ever changes for
1707 any reason, then take out the following clause and replace it
1708 with one that makes sure the ctl_fd is open. */
1711 pi
= find_procinfo_or_die (pi
->pid
, 0);
1715 struct gdb_proc_ctl_pcsexit
{
1717 /* Use char array to avoid alignment issues. */
1718 char sysset
[sizeof (sysset_t
)];
1720 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1722 + sysset_t_size (pi
);
1724 argp
= (struct gdb_proc_ctl_pcsexit
*) xmalloc (argp_size
);
1726 argp
->cmd
= PCSEXIT
;
1727 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1729 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1732 #else /* ioctl method */
1733 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1735 /* The above operation renders the procinfo's cached pstatus
1737 pi
->status_valid
= 0;
1742 /* Specify the set of blocked / held signals in the process or LWP.
1743 Returns non-zero for success, zero for failure. */
1746 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1750 /* We should never have to apply this operation to any procinfo
1751 except the one for the main process. If that ever changes for
1752 any reason, then take out the following clause and replace it
1753 with one that makes sure the ctl_fd is open. */
1756 pi
= find_procinfo_or_die (pi
->pid
, 0);
1762 /* Use char array to avoid alignment issues. */
1763 char hold
[sizeof (gdb_sigset_t
)];
1767 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1768 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1771 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1773 /* The above operation renders the procinfo's cached pstatus
1775 pi
->status_valid
= 0;
1780 /* Returns the set of signals that are held / blocked. Will also copy
1781 the sigset if SAVE is non-zero. */
1783 static gdb_sigset_t
*
1784 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
1786 gdb_sigset_t
*ret
= NULL
;
1788 /* We should never have to apply this operation to any procinfo
1789 except the one for the main process. If that ever changes for
1790 any reason, then take out the following clause and replace it
1791 with one that makes sure the ctl_fd is open. */
1794 pi
= find_procinfo_or_die (pi
->pid
, 0);
1797 if (!pi
->status_valid
)
1798 if (!proc_get_status (pi
))
1801 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1802 #else /* not NEW_PROC_API */
1804 static gdb_sigset_t sigheld
;
1806 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
1809 #endif /* NEW_PROC_API */
1811 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
1816 /* Returns the set of signals that are traced / debugged. Will also
1817 copy the sigset if SAVE is non-zero. */
1819 static gdb_sigset_t
*
1820 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
1822 gdb_sigset_t
*ret
= NULL
;
1824 /* We should never have to apply this operation to any procinfo
1825 except the one for the main process. If that ever changes for
1826 any reason, then take out the following clause and replace it
1827 with one that makes sure the ctl_fd is open. */
1830 pi
= find_procinfo_or_die (pi
->pid
, 0);
1833 if (!pi
->status_valid
)
1834 if (!proc_get_status (pi
))
1837 ret
= &pi
->prstatus
.pr_sigtrace
;
1840 static gdb_sigset_t sigtrace
;
1842 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
1847 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
1852 /* Returns the set of hardware faults that are traced /debugged. Will
1853 also copy the faultset if SAVE is non-zero. */
1856 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1858 fltset_t
*ret
= NULL
;
1860 /* We should never have to apply this operation to any procinfo
1861 except the one for the main process. If that ever changes for
1862 any reason, then take out the following clause and replace it
1863 with one that makes sure the ctl_fd is open. */
1866 pi
= find_procinfo_or_die (pi
->pid
, 0);
1869 if (!pi
->status_valid
)
1870 if (!proc_get_status (pi
))
1873 ret
= &pi
->prstatus
.pr_flttrace
;
1876 static fltset_t flttrace
;
1878 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
1883 memcpy (save
, ret
, sizeof (fltset_t
));
1888 /* Returns the set of syscalls that are traced /debugged on entry.
1889 Will also copy the syscall set if SAVE is non-zero. */
1892 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1894 sysset_t
*ret
= NULL
;
1896 /* We should never have to apply this operation to any procinfo
1897 except the one for the main process. If that ever changes for
1898 any reason, then take out the following clause and replace it
1899 with one that makes sure the ctl_fd is open. */
1902 pi
= find_procinfo_or_die (pi
->pid
, 0);
1905 if (!pi
->status_valid
)
1906 if (!proc_get_status (pi
))
1909 #ifndef DYNAMIC_SYSCALLS
1910 ret
= &pi
->prstatus
.pr_sysentry
;
1911 #else /* DYNAMIC_SYSCALLS */
1913 static sysset_t
*sysentry
;
1917 sysentry
= sysset_t_alloc (pi
);
1919 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
1921 if (pi
->prstatus
.pr_sysentry_offset
== 0)
1923 gdb_premptysysset (sysentry
);
1929 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
1931 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
1933 size
= sysset_t_size (pi
);
1934 gdb_premptysysset (sysentry
);
1935 rsize
= read (pi
->status_fd
, sysentry
, size
);
1940 #endif /* DYNAMIC_SYSCALLS */
1941 #else /* !NEW_PROC_API */
1943 static sysset_t sysentry
;
1945 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
1948 #endif /* NEW_PROC_API */
1950 memcpy (save
, ret
, sysset_t_size (pi
));
1955 /* Returns the set of syscalls that are traced /debugged on exit.
1956 Will also copy the syscall set if SAVE is non-zero. */
1959 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1961 sysset_t
* ret
= NULL
;
1963 /* We should never have to apply this operation to any procinfo
1964 except the one for the main process. If that ever changes for
1965 any reason, then take out the following clause and replace it
1966 with one that makes sure the ctl_fd is open. */
1969 pi
= find_procinfo_or_die (pi
->pid
, 0);
1972 if (!pi
->status_valid
)
1973 if (!proc_get_status (pi
))
1976 #ifndef DYNAMIC_SYSCALLS
1977 ret
= &pi
->prstatus
.pr_sysexit
;
1978 #else /* DYNAMIC_SYSCALLS */
1980 static sysset_t
*sysexit
;
1984 sysexit
= sysset_t_alloc (pi
);
1986 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
1988 if (pi
->prstatus
.pr_sysexit_offset
== 0)
1990 gdb_premptysysset (sysexit
);
1996 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
,
1998 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2000 size
= sysset_t_size (pi
);
2001 gdb_premptysysset (sysexit
);
2002 rsize
= read (pi
->status_fd
, sysexit
, size
);
2007 #endif /* DYNAMIC_SYSCALLS */
2010 static sysset_t sysexit
;
2012 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2017 memcpy (save
, ret
, sysset_t_size (pi
));
2022 /* The current fault (if any) is cleared; the associated signal will
2023 not be sent to the process or LWP when it resumes. Returns
2024 non-zero for success, zero for failure. */
2027 proc_clear_current_fault (procinfo
*pi
)
2031 /* We should never have to apply this operation to any procinfo
2032 except the one for the main process. If that ever changes for
2033 any reason, then take out the following clause and replace it
2034 with one that makes sure the ctl_fd is open. */
2037 pi
= find_procinfo_or_die (pi
->pid
, 0);
2041 procfs_ctl_t cmd
= PCCFAULT
;
2043 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2046 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2052 /* Set the "current signal" that will be delivered next to the
2053 process. NOTE: semantics are different from those of KILL. This
2054 signal will be delivered to the process or LWP immediately when it
2055 is resumed (even if the signal is held/blocked); it will NOT
2056 immediately cause another event of interest, and will NOT first
2057 trap back to the debugger. Returns non-zero for success, zero for
2061 proc_set_current_signal (procinfo
*pi
, int signo
)
2066 /* Use char array to avoid alignment issues. */
2067 char sinfo
[sizeof (gdb_siginfo_t
)];
2069 gdb_siginfo_t mysinfo
;
2071 struct target_waitstatus wait_status
;
2073 /* We should never have to apply this operation to any procinfo
2074 except the one for the main process. If that ever changes for
2075 any reason, then take out the following clause and replace it
2076 with one that makes sure the ctl_fd is open. */
2079 pi
= find_procinfo_or_die (pi
->pid
, 0);
2081 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2082 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2083 receives a PIOCSSIG with a signal identical to the current
2084 signal, it messes up the current signal. Work around the kernel
2087 signo
== proc_cursig (pi
))
2088 return 1; /* I assume this is a success? */
2091 /* The pointer is just a type alias. */
2092 get_last_target_status (&wait_ptid
, &wait_status
);
2093 if (ptid_equal (wait_ptid
, inferior_ptid
)
2094 && wait_status
.kind
== TARGET_WAITKIND_STOPPED
2095 && wait_status
.value
.sig
== gdb_signal_from_host (signo
)
2096 && proc_get_status (pi
)
2098 && pi
->prstatus
.pr_lwp
.pr_info
.si_signo
== signo
2100 && pi
->prstatus
.pr_info
.si_signo
== signo
2103 /* Use the siginfo associated with the signal being
2106 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_lwp
.pr_info
, sizeof (gdb_siginfo_t
));
2108 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_info
, sizeof (gdb_siginfo_t
));
2112 mysinfo
.si_signo
= signo
;
2113 mysinfo
.si_code
= 0;
2114 mysinfo
.si_pid
= getpid (); /* ?why? */
2115 mysinfo
.si_uid
= getuid (); /* ?why? */
2116 memcpy (arg
.sinfo
, &mysinfo
, sizeof (gdb_siginfo_t
));
2121 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2123 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2129 /* The current signal (if any) is cleared, and is not sent to the
2130 process or LWP when it resumes. Returns non-zero for success, zero
2134 proc_clear_current_signal (procinfo
*pi
)
2138 /* We should never have to apply this operation to any procinfo
2139 except the one for the main process. If that ever changes for
2140 any reason, then take out the following clause and replace it
2141 with one that makes sure the ctl_fd is open. */
2144 pi
= find_procinfo_or_die (pi
->pid
, 0);
2150 /* Use char array to avoid alignment issues. */
2151 char sinfo
[sizeof (gdb_siginfo_t
)];
2153 gdb_siginfo_t mysinfo
;
2156 /* The pointer is just a type alias. */
2157 mysinfo
.si_signo
= 0;
2158 mysinfo
.si_code
= 0;
2159 mysinfo
.si_errno
= 0;
2160 mysinfo
.si_pid
= getpid (); /* ?why? */
2161 mysinfo
.si_uid
= getuid (); /* ?why? */
2162 memcpy (arg
.sinfo
, &mysinfo
, sizeof (gdb_siginfo_t
));
2164 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2167 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2173 /* Return the general-purpose registers for the process or LWP
2174 corresponding to PI. Upon failure, return NULL. */
2176 static gdb_gregset_t
*
2177 proc_get_gregs (procinfo
*pi
)
2179 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2180 if (!proc_get_status (pi
))
2184 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2186 return &pi
->prstatus
.pr_reg
;
2190 /* Return the general-purpose registers for the process or LWP
2191 corresponding to PI. Upon failure, return NULL. */
2193 static gdb_fpregset_t
*
2194 proc_get_fpregs (procinfo
*pi
)
2197 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2198 if (!proc_get_status (pi
))
2201 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2203 #else /* not NEW_PROC_API */
2204 if (pi
->fpregs_valid
)
2205 return &pi
->fpregset
; /* Already got 'em. */
2208 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2217 tid_t pr_error_thread
;
2218 tfpregset_t thread_1
;
2221 thread_fpregs
.pr_count
= 1;
2222 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2225 && ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2227 pi
->fpregs_valid
= 1;
2228 return &pi
->fpregset
; /* Got 'em now! */
2230 else if (pi
->tid
!= 0
2231 && ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2233 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2234 sizeof (pi
->fpregset
));
2235 pi
->fpregs_valid
= 1;
2236 return &pi
->fpregset
; /* Got 'em now! */
2243 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2245 pi
->fpregs_valid
= 1;
2246 return &pi
->fpregset
; /* Got 'em now! */
2255 #endif /* NEW_PROC_API */
2258 /* Write the general-purpose registers back to the process or LWP
2259 corresponding to PI. Return non-zero for success, zero for
2263 proc_set_gregs (procinfo
*pi
)
2265 gdb_gregset_t
*gregs
;
2268 gregs
= proc_get_gregs (pi
);
2270 return 0; /* proc_get_regs has already warned. */
2272 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2281 /* Use char array to avoid alignment issues. */
2282 char gregs
[sizeof (gdb_gregset_t
)];
2286 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2287 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2289 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2293 /* Policy: writing the registers invalidates our cache. */
2294 pi
->gregs_valid
= 0;
2298 /* Write the floating-pointer registers back to the process or LWP
2299 corresponding to PI. Return non-zero for success, zero for
2303 proc_set_fpregs (procinfo
*pi
)
2305 gdb_fpregset_t
*fpregs
;
2308 fpregs
= proc_get_fpregs (pi
);
2310 return 0; /* proc_get_fpregs has already warned. */
2312 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2321 /* Use char array to avoid alignment issues. */
2322 char fpregs
[sizeof (gdb_fpregset_t
)];
2326 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2327 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2331 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2336 tid_t pr_error_thread
;
2337 tfpregset_t thread_1
;
2340 thread_fpregs
.pr_count
= 1;
2341 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2342 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2344 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2347 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2349 #endif /* NEW_PROC_API */
2352 /* Policy: writing the registers invalidates our cache. */
2353 pi
->fpregs_valid
= 0;
2357 /* Send a signal to the proc or lwp with the semantics of "kill()".
2358 Returns non-zero for success, zero for failure. */
2361 proc_kill (procinfo
*pi
, int signo
)
2365 /* We might conceivably apply this operation to an LWP, and the
2366 LWP's ctl file descriptor might not be open. */
2368 if (pi
->ctl_fd
== 0 &&
2369 open_procinfo_files (pi
, FD_CTL
) == 0)
2376 procfs_ctl_t cmd
[2];
2380 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2381 #else /* ioctl method */
2382 /* FIXME: do I need the Alpha OSF fixups present in
2383 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2384 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2391 /* Find the pid of the process that started this one. Returns the
2392 parent process pid, or zero. */
2395 proc_parent_pid (procinfo
*pi
)
2397 /* We should never have to apply this operation to any procinfo
2398 except the one for the main process. If that ever changes for
2399 any reason, then take out the following clause and replace it
2400 with one that makes sure the ctl_fd is open. */
2403 pi
= find_procinfo_or_die (pi
->pid
, 0);
2405 if (!pi
->status_valid
)
2406 if (!proc_get_status (pi
))
2409 return pi
->prstatus
.pr_ppid
;
2412 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2413 (a.k.a void pointer)! */
2415 #if (defined (PCWATCH) || defined (PIOCSWATCH)) \
2416 && !(defined (PIOCOPENLWP))
2418 procfs_address_to_host_pointer (CORE_ADDR addr
)
2420 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
2423 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (ptr_type
));
2424 gdbarch_address_to_pointer (target_gdbarch (), ptr_type
,
2425 (gdb_byte
*) &ptr
, addr
);
2431 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2433 #if !defined (PCWATCH) && !defined (PIOCSWATCH)
2434 /* If neither or these is defined, we can't support watchpoints.
2435 This just avoids possibly failing to compile the below on such
2439 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5. */
2440 #if defined (PIOCOPENLWP) /* Solaris 2.5: bail out. */
2445 char watch
[sizeof (prwatch_t
)];
2449 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2450 convert a target address into something that can be stored in a
2451 native data structure. */
2452 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2453 pwatch
.pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
2455 pwatch
.pr_vaddr
= (caddr_t
) procfs_address_to_host_pointer (addr
);
2457 pwatch
.pr_size
= len
;
2458 pwatch
.pr_wflags
= wflags
;
2459 #if defined(NEW_PROC_API) && defined (PCWATCH)
2461 memcpy (arg
.watch
, &pwatch
, sizeof (prwatch_t
));
2462 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2464 #if defined (PIOCSWATCH)
2465 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, &pwatch
) >= 0);
2467 return 0; /* Fail */
2474 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
2476 #include <sys/sysi86.h>
2478 /* The KEY is actually the value of the lower 16 bits of the GS
2479 register for the LWP that we're interested in. Returns the
2480 matching ssh struct (LDT entry). */
2483 proc_get_LDT_entry (procinfo
*pi
, int key
)
2485 static struct ssd
*ldt_entry
= NULL
;
2487 char pathname
[MAX_PROC_NAME_SIZE
];
2488 struct cleanup
*old_chain
= NULL
;
2491 /* Allocate space for one LDT entry.
2492 This alloc must persist, because we return a pointer to it. */
2493 if (ldt_entry
== NULL
)
2494 ldt_entry
= XNEW (struct ssd
);
2496 /* Open the file descriptor for the LDT table. */
2497 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2498 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2500 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2503 /* Make sure it gets closed again! */
2504 old_chain
= make_cleanup_close (fd
);
2506 /* Now 'read' thru the table, find a match and return it. */
2507 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2509 if (ldt_entry
->sel
== 0 &&
2510 ldt_entry
->bo
== 0 &&
2511 ldt_entry
->acc1
== 0 &&
2512 ldt_entry
->acc2
== 0)
2513 break; /* end of table */
2514 /* If key matches, return this entry. */
2515 if (ldt_entry
->sel
== key
)
2517 do_cleanups (old_chain
);
2521 /* Loop ended, match not found. */
2522 do_cleanups (old_chain
);
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
= XNEWVEC (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
)
2764 closedir ((DIR *) 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
= XNEWVEC (tid_t
, nthreads
);
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 /* Sets up the inferior to be debugged. Registers to trace signals,
2912 hardware faults, and syscalls. Note: does not set RLC flag: caller
2913 may want to customize that. Returns zero for success (note!
2914 unlike most functions in this module); on failure, returns the LINE
2915 NUMBER where it failed! */
2918 procfs_debug_inferior (procinfo
*pi
)
2920 fltset_t traced_faults
;
2921 gdb_sigset_t traced_signals
;
2922 sysset_t
*traced_syscall_entries
;
2923 sysset_t
*traced_syscall_exits
;
2926 /* Register to trace hardware faults in the child. */
2927 prfillset (&traced_faults
); /* trace all faults... */
2928 gdb_prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
2929 if (!proc_set_traced_faults (pi
, &traced_faults
))
2932 /* Initially, register to trace all signals in the child. */
2933 prfillset (&traced_signals
);
2934 if (!proc_set_traced_signals (pi
, &traced_signals
))
2938 /* Register to trace the 'exit' system call (on entry). */
2939 traced_syscall_entries
= sysset_t_alloc (pi
);
2940 gdb_premptysysset (traced_syscall_entries
);
2942 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
2945 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
);/* And _lwp_exit... */
2948 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
2950 #ifdef DYNAMIC_SYSCALLS
2952 int callnum
= find_syscall (pi
, "_exit");
2955 gdb_praddsysset (traced_syscall_entries
, callnum
);
2959 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
2960 xfree (traced_syscall_entries
);
2964 #ifdef PRFS_STOPEXEC /* defined on OSF */
2965 /* OSF method for tracing exec syscalls. Quoting:
2966 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
2967 exits from exec system calls because of the user level loader. */
2968 /* FIXME: make nice and maybe move into an access function. */
2972 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
2975 prfs_flags
|= PRFS_STOPEXEC
;
2977 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
2980 #else /* not PRFS_STOPEXEC */
2981 /* Everyone else's (except OSF) method for tracing exec syscalls. */
2983 Not all systems with /proc have all the exec* syscalls with the same
2984 names. On the SGI, for example, there is no SYS_exec, but there
2985 *is* a SYS_execv. So, we try to account for that. */
2987 traced_syscall_exits
= sysset_t_alloc (pi
);
2988 gdb_premptysysset (traced_syscall_exits
);
2990 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
2993 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
2996 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
2999 #ifdef SYS_lwpcreate
3000 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3001 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3004 #ifdef SYS_lwp_create /* FIXME: once only, please. */
3005 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3006 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3009 #ifdef DYNAMIC_SYSCALLS
3011 int callnum
= find_syscall (pi
, "execve");
3014 gdb_praddsysset (traced_syscall_exits
, callnum
);
3015 callnum
= find_syscall (pi
, "ra_execve");
3017 gdb_praddsysset (traced_syscall_exits
, callnum
);
3021 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3022 xfree (traced_syscall_exits
);
3026 #endif /* PRFS_STOPEXEC */
3031 procfs_attach (struct target_ops
*ops
, const char *args
, int from_tty
)
3036 pid
= parse_pid_to_attach (args
);
3038 if (pid
== getpid ())
3039 error (_("Attaching GDB to itself is not a good idea..."));
3043 exec_file
= get_exec_file (0);
3046 printf_filtered (_("Attaching to program `%s', %s\n"),
3047 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3049 printf_filtered (_("Attaching to %s\n"),
3050 target_pid_to_str (pid_to_ptid (pid
)));
3054 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3055 if (!target_is_pushed (ops
))
3060 procfs_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
3063 int pid
= ptid_get_pid (inferior_ptid
);
3070 const char *exec_file
;
3072 exec_file
= get_exec_file (0);
3073 if (exec_file
== NULL
)
3076 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file
,
3077 target_pid_to_str (pid_to_ptid (pid
)));
3078 gdb_flush (gdb_stdout
);
3083 inferior_ptid
= null_ptid
;
3084 detach_inferior (pid
);
3085 inf_child_maybe_unpush_target (ops
);
3089 do_attach (ptid_t ptid
)
3092 struct inferior
*inf
;
3096 if ((pi
= create_procinfo (ptid_get_pid (ptid
), 0)) == NULL
)
3097 perror (_("procfs: out of memory in 'attach'"));
3099 if (!open_procinfo_files (pi
, FD_CTL
))
3101 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3102 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3103 ptid_get_pid (ptid
));
3104 dead_procinfo (pi
, errmsg
, NOKILL
);
3107 /* Stop the process (if it isn't already stopped). */
3108 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3110 pi
->was_stopped
= 1;
3111 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3115 pi
->was_stopped
= 0;
3116 /* Set the process to run again when we close it. */
3117 if (!proc_set_run_on_last_close (pi
))
3118 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3120 /* Now stop the process. */
3121 if (!proc_stop_process (pi
))
3122 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3123 pi
->ignore_next_sigstop
= 1;
3125 /* Save some of the /proc state to be restored if we detach. */
3126 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3127 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3128 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3129 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3130 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3131 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3133 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3134 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3136 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3137 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3139 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3140 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3142 inf
= current_inferior ();
3143 inferior_appeared (inf
, pi
->pid
);
3144 /* Let GDB know that the inferior was attached. */
3145 inf
->attach_flag
= 1;
3147 /* Create a procinfo for the current lwp. */
3148 lwpid
= proc_get_current_thread (pi
);
3149 create_procinfo (pi
->pid
, lwpid
);
3151 /* Add it to gdb's thread list. */
3152 ptid
= ptid_build (pi
->pid
, lwpid
, 0);
3159 do_detach (int signo
)
3163 /* Find procinfo for the main process. */
3164 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
),
3165 0); /* FIXME: threads */
3167 if (!proc_set_current_signal (pi
, signo
))
3168 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3170 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3171 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3173 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3174 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3176 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3177 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3179 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3180 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3182 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3183 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3185 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3186 if (signo
|| !(pi
->was_stopped
) ||
3187 query (_("Was stopped when attached, make it runnable again? ")))
3189 /* Clear any pending signal. */
3190 if (!proc_clear_current_fault (pi
))
3191 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3193 if (signo
== 0 && !proc_clear_current_signal (pi
))
3194 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
3196 if (!proc_set_run_on_last_close (pi
))
3197 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3200 destroy_procinfo (pi
);
3203 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
3206 ??? Is the following note still relevant? We can't get individual
3207 registers with the PT_GETREGS ptrace(2) request either, yet we
3208 don't bother with caching at all in that case.
3210 NOTE: Since the /proc interface cannot give us individual
3211 registers, we pay no attention to REGNUM, and just fetch them all.
3212 This results in the possibility that we will do unnecessarily many
3213 fetches, since we may be called repeatedly for individual
3214 registers. So we cache the results, and mark the cache invalid
3215 when the process is resumed. */
3218 procfs_fetch_registers (struct target_ops
*ops
,
3219 struct regcache
*regcache
, int regnum
)
3221 gdb_gregset_t
*gregs
;
3223 ptid_t ptid
= regcache_get_ptid (regcache
);
3224 int pid
= ptid_get_pid (ptid
);
3225 int tid
= ptid_get_lwp (ptid
);
3226 struct gdbarch
*gdbarch
= regcache
->arch ();
3228 pi
= find_procinfo_or_die (pid
, tid
);
3231 error (_("procfs: fetch_registers failed to find procinfo for %s"),
3232 target_pid_to_str (ptid
));
3234 gregs
= proc_get_gregs (pi
);
3236 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3238 supply_gregset (regcache
, (const gdb_gregset_t
*) gregs
);
3240 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
3242 gdb_fpregset_t
*fpregs
;
3244 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
3245 || regnum
== gdbarch_pc_regnum (gdbarch
)
3246 || regnum
== gdbarch_sp_regnum (gdbarch
))
3247 return; /* Not a floating point register. */
3249 fpregs
= proc_get_fpregs (pi
);
3251 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3253 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregs
);
3257 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
3258 this for all registers.
3260 NOTE: Since the /proc interface will not read individual registers,
3261 we will cache these requests until the process is resumed, and only
3262 then write them back to the inferior process.
3264 FIXME: is that a really bad idea? Have to think about cases where
3265 writing one register might affect the value of others, etc. */
3268 procfs_store_registers (struct target_ops
*ops
,
3269 struct regcache
*regcache
, int regnum
)
3271 gdb_gregset_t
*gregs
;
3273 ptid_t ptid
= regcache_get_ptid (regcache
);
3274 int pid
= ptid_get_pid (ptid
);
3275 int tid
= ptid_get_lwp (ptid
);
3276 struct gdbarch
*gdbarch
= regcache
->arch ();
3278 pi
= find_procinfo_or_die (pid
, tid
);
3281 error (_("procfs: store_registers: failed to find procinfo for %s"),
3282 target_pid_to_str (ptid
));
3284 gregs
= proc_get_gregs (pi
);
3286 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3288 fill_gregset (regcache
, gregs
, regnum
);
3289 if (!proc_set_gregs (pi
))
3290 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3292 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
3294 gdb_fpregset_t
*fpregs
;
3296 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
3297 || regnum
== gdbarch_pc_regnum (gdbarch
)
3298 || regnum
== gdbarch_sp_regnum (gdbarch
))
3299 return; /* Not a floating point register. */
3301 fpregs
= proc_get_fpregs (pi
);
3303 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3305 fill_fpregset (regcache
, fpregs
, regnum
);
3306 if (!proc_set_fpregs (pi
))
3307 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3312 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3315 if (scall
== SYS_lwp_exit
)
3319 if (scall
== SYS_lwpexit
)
3326 syscall_is_exit (procinfo
*pi
, int scall
)
3329 if (scall
== SYS_exit
)
3332 #ifdef DYNAMIC_SYSCALLS
3333 if (find_syscall (pi
, "_exit") == scall
)
3340 syscall_is_exec (procinfo
*pi
, int scall
)
3343 if (scall
== SYS_exec
)
3347 if (scall
== SYS_execv
)
3351 if (scall
== SYS_execve
)
3354 #ifdef DYNAMIC_SYSCALLS
3355 if (find_syscall (pi
, "_execve"))
3357 if (find_syscall (pi
, "ra_execve"))
3364 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3366 #ifdef SYS_lwp_create
3367 if (scall
== SYS_lwp_create
)
3370 #ifdef SYS_lwpcreate
3371 if (scall
== SYS_lwpcreate
)
3378 /* Return the address of the __dbx_link() function in the file
3379 refernced by ABFD by scanning its symbol table. Return 0 if
3380 the symbol was not found. */
3383 dbx_link_addr (bfd
*abfd
)
3385 long storage_needed
;
3386 asymbol
**symbol_table
;
3387 long number_of_symbols
;
3390 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
3391 if (storage_needed
<= 0)
3394 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
3395 make_cleanup (xfree
, symbol_table
);
3397 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
3399 for (i
= 0; i
< number_of_symbols
; i
++)
3401 asymbol
*sym
= symbol_table
[i
];
3403 if ((sym
->flags
& BSF_GLOBAL
)
3404 && sym
->name
!= NULL
&& strcmp (sym
->name
, "__dbx_link") == 0)
3405 return (sym
->value
+ sym
->section
->vma
);
3408 /* Symbol not found, return NULL. */
3412 /* Search the symbol table of the file referenced by FD for a symbol
3413 named __dbx_link(). If found, then insert a breakpoint at this location,
3414 and return nonzero. Return zero otherwise. */
3417 insert_dbx_link_bpt_in_file (int fd
, CORE_ADDR ignored
)
3419 long storage_needed
;
3422 gdb_bfd_ref_ptr
abfd (gdb_bfd_fdopenr ("unamed", 0, fd
));
3425 warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
3429 if (!bfd_check_format (abfd
.get (), bfd_object
))
3431 /* Not the correct format, so we can not possibly find the dbx_link
3436 sym_addr
= dbx_link_addr (abfd
.get ());
3439 struct breakpoint
*dbx_link_bpt
;
3441 /* Insert the breakpoint. */
3443 = create_and_insert_solib_event_breakpoint (target_gdbarch (),
3445 if (dbx_link_bpt
== NULL
)
3447 warning (_("Failed to insert dbx_link breakpoint."));
3456 /* Calls the supplied callback function once for each mapped address
3457 space in the process. The callback function receives an open file
3458 descriptor for the file corresponding to that mapped address space
3459 (if there is one), and the base address of the mapped space. Quit
3460 when the callback function returns a nonzero value, or at teh end
3461 of the mappings. Returns the first non-zero return value of the
3462 callback function, or zero. */
3465 solib_mappings_callback (struct prmap
*map
, int (*func
) (int, CORE_ADDR
),
3468 procinfo
*pi
= data
;
3472 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
3474 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
3475 return -1; /* sanity */
3477 if (map
->pr_mapname
[0] == 0)
3479 fd
= -1; /* no map file */
3483 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
3484 /* Note: caller's responsibility to close this fd! */
3485 fd
= open_with_retry (name
, O_RDONLY
);
3486 /* Note: we don't test the above call for failure;
3487 we just pass the FD on as given. Sometimes there is
3488 no file, so the open may return failure, but that's
3492 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
->pr_vaddr
);
3493 /* Note: we don't test the above call for failure;
3494 we just pass the FD on as given. Sometimes there is
3495 no file, so the ioctl may return failure, but that's
3498 return (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
);
3501 /* If the given memory region MAP contains a symbol named __dbx_link,
3502 insert a breakpoint at this location and return nonzero. Return
3506 insert_dbx_link_bpt_in_region (struct prmap
*map
,
3507 find_memory_region_ftype child_func
,
3510 procinfo
*pi
= (procinfo
*) data
;
3512 /* We know the symbol we're looking for is in a text region, so
3513 only look for it if the region is a text one. */
3514 if (map
->pr_mflags
& MA_EXEC
)
3515 return solib_mappings_callback (map
, insert_dbx_link_bpt_in_file
, pi
);
3520 /* Search all memory regions for a symbol named __dbx_link. If found,
3521 insert a breakpoint at its location, and return nonzero. Return zero
3525 insert_dbx_link_breakpoint (procinfo
*pi
)
3527 return iterate_over_mappings (pi
, NULL
, pi
, insert_dbx_link_bpt_in_region
);
3531 /* Retrieve the next stop event from the child process. If child has
3532 not stopped yet, wait for it to stop. Translate /proc eventcodes
3533 (or possibly wait eventcodes) into gdb internal event codes.
3534 Returns the id of process (and possibly thread) that incurred the
3535 event. Event codes are returned through a pointer parameter. */
3538 procfs_wait (struct target_ops
*ops
,
3539 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
3541 /* First cut: loosely based on original version 2.1. */
3545 ptid_t retval
, temp_ptid
;
3546 int why
, what
, flags
;
3553 retval
= pid_to_ptid (-1);
3555 /* Find procinfo for main process. */
3556 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
3559 /* We must assume that the status is stale now... */
3560 pi
->status_valid
= 0;
3561 pi
->gregs_valid
= 0;
3562 pi
->fpregs_valid
= 0;
3564 #if 0 /* just try this out... */
3565 flags
= proc_flags (pi
);
3566 why
= proc_why (pi
);
3567 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3568 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3570 /* If child is not stopped, wait for it to stop. */
3571 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3572 !proc_wait_for_stop (pi
))
3574 /* wait_for_stop failed: has the child terminated? */
3575 if (errno
== ENOENT
)
3579 /* /proc file not found; presumably child has terminated. */
3580 wait_retval
= wait (&wstat
); /* "wait" for the child's exit. */
3583 if (wait_retval
!= ptid_get_pid (inferior_ptid
))
3584 error (_("procfs: couldn't stop "
3585 "process %d: wait returned %d."),
3586 ptid_get_pid (inferior_ptid
), wait_retval
);
3587 /* FIXME: might I not just use waitpid?
3588 Or try find_procinfo to see if I know about this child? */
3589 retval
= pid_to_ptid (wait_retval
);
3591 else if (errno
== EINTR
)
3595 /* Unknown error from wait_for_stop. */
3596 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3601 /* This long block is reached if either:
3602 a) the child was already stopped, or
3603 b) we successfully waited for the child with wait_for_stop.
3604 This block will analyze the /proc status, and translate it
3605 into a waitstatus for GDB.
3607 If we actually had to call wait because the /proc file
3608 is gone (child terminated), then we skip this block,
3609 because we already have a waitstatus. */
3611 flags
= proc_flags (pi
);
3612 why
= proc_why (pi
);
3613 what
= proc_what (pi
);
3615 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3618 /* If it's running async (for single_thread control),
3619 set it back to normal again. */
3620 if (flags
& PR_ASYNC
)
3621 if (!proc_unset_async (pi
))
3622 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3626 proc_prettyprint_why (why
, what
, 1);
3628 /* The 'pid' we will return to GDB is composed of
3629 the process ID plus the lwp ID. */
3630 retval
= ptid_build (pi
->pid
, proc_get_current_thread (pi
), 0);
3634 wstat
= (what
<< 8) | 0177;
3637 if (syscall_is_lwp_exit (pi
, what
))
3639 if (print_thread_events
)
3640 printf_unfiltered (_("[%s exited]\n"),
3641 target_pid_to_str (retval
));
3642 delete_thread (retval
);
3643 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3646 else if (syscall_is_exit (pi
, what
))
3648 struct inferior
*inf
;
3650 /* Handle SYS_exit call only. */
3651 /* Stopped at entry to SYS_exit.
3652 Make it runnable, resume it, then use
3653 the wait system call to get its exit code.
3654 Proc_run_process always clears the current
3656 Then return its exit status. */
3657 pi
->status_valid
= 0;
3659 /* FIXME: what we should do is return
3660 TARGET_WAITKIND_SPURIOUS. */
3661 if (!proc_run_process (pi
, 0, 0))
3662 proc_error (pi
, "target_wait, run_process", __LINE__
);
3664 inf
= find_inferior_pid (pi
->pid
);
3665 if (inf
->attach_flag
)
3667 /* Don't call wait: simulate waiting for exit,
3668 return a "success" exit code. Bogus: what if
3669 it returns something else? */
3671 retval
= inferior_ptid
; /* ? ? ? */
3675 int temp
= wait (&wstat
);
3677 /* FIXME: shouldn't I make sure I get the right
3678 event from the right process? If (for
3679 instance) I have killed an earlier inferior
3680 process but failed to clean up after it
3681 somehow, I could get its termination event
3684 /* If wait returns -1, that's what we return
3687 retval
= pid_to_ptid (temp
);
3692 printf_filtered (_("procfs: trapped on entry to "));
3693 proc_prettyprint_syscall (proc_what (pi
), 0);
3694 printf_filtered ("\n");
3697 long i
, nsysargs
, *sysargs
;
3699 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3700 (sysargs
= proc_sysargs (pi
)) != NULL
)
3702 printf_filtered (_("%ld syscall arguments:\n"),
3704 for (i
= 0; i
< nsysargs
; i
++)
3705 printf_filtered ("#%ld: 0x%08lx\n",
3713 /* How to exit gracefully, returning "unknown
3715 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3716 return inferior_ptid
;
3720 /* How to keep going without returning to wfi: */
3721 target_continue_no_signal (ptid
);
3727 if (syscall_is_exec (pi
, what
))
3729 /* Hopefully this is our own "fork-child" execing
3730 the real child. Hoax this event into a trap, and
3731 GDB will see the child about to execute its start
3733 wstat
= (SIGTRAP
<< 8) | 0177;
3736 else if (what
== SYS_syssgi
)
3738 /* see if we can break on dbx_link(). If yes, then
3739 we no longer need the SYS_syssgi notifications. */
3740 if (insert_dbx_link_breakpoint (pi
))
3741 proc_trace_syscalls_1 (pi
, SYS_syssgi
, PR_SYSEXIT
,
3744 /* This is an internal event and should be transparent
3745 to wfi, so resume the execution and wait again. See
3746 comment in procfs_init_inferior() for more details. */
3747 target_continue_no_signal (ptid
);
3751 else if (syscall_is_lwp_create (pi
, what
))
3753 /* This syscall is somewhat like fork/exec. We
3754 will get the event twice: once for the parent
3755 LWP, and once for the child. We should already
3756 know about the parent LWP, but the child will
3757 be new to us. So, whenever we get this event,
3758 if it represents a new thread, simply add the
3759 thread to the list. */
3761 /* If not in procinfo list, add it. */
3762 temp_tid
= proc_get_current_thread (pi
);
3763 if (!find_procinfo (pi
->pid
, temp_tid
))
3764 create_procinfo (pi
->pid
, temp_tid
);
3766 temp_ptid
= ptid_build (pi
->pid
, temp_tid
, 0);
3767 /* If not in GDB's thread list, add it. */
3768 if (!in_thread_list (temp_ptid
))
3769 add_thread (temp_ptid
);
3771 /* Return to WFI, but tell it to immediately resume. */
3772 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3773 return inferior_ptid
;
3775 else if (syscall_is_lwp_exit (pi
, what
))
3777 if (print_thread_events
)
3778 printf_unfiltered (_("[%s exited]\n"),
3779 target_pid_to_str (retval
));
3780 delete_thread (retval
);
3781 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3786 /* FIXME: Do we need to handle SYS_sproc,
3787 SYS_fork, or SYS_vfork here? The old procfs
3788 seemed to use this event to handle threads on
3789 older (non-LWP) systems, where I'm assuming
3790 that threads were actually separate processes.
3791 Irix, maybe? Anyway, low priority for now. */
3795 printf_filtered (_("procfs: trapped on exit from "));
3796 proc_prettyprint_syscall (proc_what (pi
), 0);
3797 printf_filtered ("\n");
3800 long i
, nsysargs
, *sysargs
;
3802 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3803 (sysargs
= proc_sysargs (pi
)) != NULL
)
3805 printf_filtered (_("%ld syscall arguments:\n"),
3807 for (i
= 0; i
< nsysargs
; i
++)
3808 printf_filtered ("#%ld: 0x%08lx\n",
3813 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3814 return inferior_ptid
;
3819 wstat
= (SIGSTOP
<< 8) | 0177;
3824 printf_filtered (_("Retry #%d:\n"), retry
);
3825 pi
->status_valid
= 0;
3830 /* If not in procinfo list, add it. */
3831 temp_tid
= proc_get_current_thread (pi
);
3832 if (!find_procinfo (pi
->pid
, temp_tid
))
3833 create_procinfo (pi
->pid
, temp_tid
);
3835 /* If not in GDB's thread list, add it. */
3836 temp_ptid
= ptid_build (pi
->pid
, temp_tid
, 0);
3837 if (!in_thread_list (temp_ptid
))
3838 add_thread (temp_ptid
);
3840 status
->kind
= TARGET_WAITKIND_STOPPED
;
3841 status
->value
.sig
= GDB_SIGNAL_0
;
3846 wstat
= (what
<< 8) | 0177;
3852 wstat
= (SIGTRAP
<< 8) | 0177;
3857 wstat
= (SIGTRAP
<< 8) | 0177;
3860 /* FIXME: use si_signo where possible. */
3862 #if (FLTILL != FLTPRIV) /* Avoid "duplicate case" error. */
3865 wstat
= (SIGILL
<< 8) | 0177;
3868 #if (FLTTRACE != FLTBPT) /* Avoid "duplicate case" error. */
3871 wstat
= (SIGTRAP
<< 8) | 0177;
3875 #if (FLTBOUNDS != FLTSTACK) /* Avoid "duplicate case" error. */
3878 wstat
= (SIGSEGV
<< 8) | 0177;
3882 #if (FLTFPE != FLTIOVF) /* Avoid "duplicate case" error. */
3885 wstat
= (SIGFPE
<< 8) | 0177;
3887 case FLTPAGE
: /* Recoverable page fault */
3888 default: /* FIXME: use si_signo if possible for
3890 retval
= pid_to_ptid (-1);
3891 printf_filtered ("procfs:%d -- ", __LINE__
);
3892 printf_filtered (_("child stopped for unknown reason:\n"));
3893 proc_prettyprint_why (why
, what
, 1);
3894 error (_("... giving up..."));
3897 break; /* case PR_FAULTED: */
3898 default: /* switch (why) unmatched */
3899 printf_filtered ("procfs:%d -- ", __LINE__
);
3900 printf_filtered (_("child stopped for unknown reason:\n"));
3901 proc_prettyprint_why (why
, what
, 1);
3902 error (_("... giving up..."));
3905 /* Got this far without error: If retval isn't in the
3906 threads database, add it. */
3907 if (ptid_get_pid (retval
) > 0 &&
3908 !ptid_equal (retval
, inferior_ptid
) &&
3909 !in_thread_list (retval
))
3911 /* We have a new thread. We need to add it both to
3912 GDB's list and to our own. If we don't create a
3913 procinfo, resume may be unhappy later. */
3914 add_thread (retval
);
3915 if (find_procinfo (ptid_get_pid (retval
),
3916 ptid_get_lwp (retval
)) == NULL
)
3917 create_procinfo (ptid_get_pid (retval
),
3918 ptid_get_lwp (retval
));
3921 else /* Flags do not indicate STOPPED. */
3923 /* surely this can't happen... */
3924 printf_filtered ("procfs:%d -- process not stopped.\n",
3926 proc_prettyprint_flags (flags
, 1);
3927 error (_("procfs: ...giving up..."));
3932 store_waitstatus (status
, wstat
);
3938 /* Perform a partial transfer to/from the specified object. For
3939 memory transfers, fall back to the old memory xfer functions. */
3941 static enum target_xfer_status
3942 procfs_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3943 const char *annex
, gdb_byte
*readbuf
,
3944 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
3945 ULONGEST
*xfered_len
)
3949 case TARGET_OBJECT_MEMORY
:
3950 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
3953 case TARGET_OBJECT_AUXV
:
3954 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
3955 offset
, len
, xfered_len
);
3959 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
,
3960 readbuf
, writebuf
, offset
, len
,
3965 /* Helper for procfs_xfer_partial that handles memory transfers.
3966 Arguments are like target_xfer_partial. */
3968 static enum target_xfer_status
3969 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
3970 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
3975 /* Find procinfo for main process. */
3976 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
3977 if (pi
->as_fd
== 0 &&
3978 open_procinfo_files (pi
, FD_AS
) == 0)
3980 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
3981 return TARGET_XFER_E_IO
;
3984 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
3985 return TARGET_XFER_E_IO
;
3987 if (writebuf
!= NULL
)
3989 PROCFS_NOTE ("write memory:\n");
3990 nbytes
= write (pi
->as_fd
, writebuf
, len
);
3994 PROCFS_NOTE ("read memory:\n");
3995 nbytes
= read (pi
->as_fd
, readbuf
, len
);
3998 return TARGET_XFER_E_IO
;
3999 *xfered_len
= nbytes
;
4000 return TARGET_XFER_OK
;
4003 /* Called by target_resume before making child runnable. Mark cached
4004 registers and status's invalid. If there are "dirty" caches that
4005 need to be written back to the child process, do that.
4007 File descriptors are also cached. As they are a limited resource,
4008 we cannot hold onto them indefinitely. However, as they are
4009 expensive to open, we don't want to throw them away
4010 indescriminately either. As a compromise, we will keep the file
4011 descriptors for the parent process, but discard any file
4012 descriptors we may have accumulated for the threads.
4014 As this function is called by iterate_over_threads, it always
4015 returns zero (so that iterate_over_threads will keep
4019 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4021 /* About to run the child; invalidate caches and do any other
4025 if (pi
->gregs_dirty
)
4026 if (parent
== NULL
||
4027 proc_get_current_thread (parent
) != pi
->tid
)
4028 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4029 proc_warn (pi
, "target_resume, set_gregs",
4031 if (gdbarch_fp0_regnum (target_gdbarch ()) >= 0)
4032 if (pi
->fpregs_dirty
)
4033 if (parent
== NULL
||
4034 proc_get_current_thread (parent
) != pi
->tid
)
4035 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4036 proc_warn (pi
, "target_resume, set_fpregs",
4042 /* The presence of a parent indicates that this is an LWP.
4043 Close any file descriptors that it might have open.
4044 We don't do this to the master (parent) procinfo. */
4046 close_procinfo_files (pi
);
4048 pi
->gregs_valid
= 0;
4049 pi
->fpregs_valid
= 0;
4051 pi
->gregs_dirty
= 0;
4052 pi
->fpregs_dirty
= 0;
4054 pi
->status_valid
= 0;
4055 pi
->threads_valid
= 0;
4061 /* A callback function for iterate_over_threads. Find the
4062 asynchronous signal thread, and make it runnable. See if that
4063 helps matters any. */
4066 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4069 if (proc_flags (pi
) & PR_ASLWP
)
4071 if (!proc_run_process (pi
, 0, -1))
4072 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4080 /* Make the child process runnable. Normally we will then call
4081 procfs_wait and wait for it to stop again (unless gdb is async).
4083 If STEP is true, then arrange for the child to stop again after
4084 executing a single instruction. If SIGNO is zero, then cancel any
4085 pending signal; if non-zero, then arrange for the indicated signal
4086 to be delivered to the child when it runs. If PID is -1, then
4087 allow any child thread to run; if non-zero, then allow only the
4088 indicated thread to run. (not implemented yet). */
4091 procfs_resume (struct target_ops
*ops
,
4092 ptid_t ptid
, int step
, enum gdb_signal signo
)
4094 procinfo
*pi
, *thread
;
4098 prrun.prflags |= PRSVADDR;
4099 prrun.pr_vaddr = $PC; set resume address
4100 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4101 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4102 prrun.prflags |= PRCFAULT; clear current fault.
4104 PRSTRACE and PRSFAULT can be done by other means
4105 (proc_trace_signals, proc_trace_faults)
4106 PRSVADDR is unnecessary.
4107 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4108 This basically leaves PRSTEP and PRCSIG.
4109 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4110 So basically PR_STEP is the sole argument that must be passed
4111 to proc_run_process (for use in the prrun struct by ioctl). */
4113 /* Find procinfo for main process. */
4114 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4116 /* First cut: ignore pid argument. */
4119 /* Convert signal to host numbering. */
4121 (signo
== GDB_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4124 native_signo
= gdb_signal_to_host (signo
);
4126 pi
->ignore_next_sigstop
= 0;
4128 /* Running the process voids all cached registers and status. */
4129 /* Void the threads' caches first. */
4130 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4131 /* Void the process procinfo's caches. */
4132 invalidate_cache (NULL
, pi
, NULL
);
4134 if (ptid_get_pid (ptid
) != -1)
4136 /* Resume a specific thread, presumably suppressing the
4138 thread
= find_procinfo (ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
4141 if (thread
->tid
!= 0)
4143 /* We're to resume a specific thread, and not the
4144 others. Set the child process's PR_ASYNC flag. */
4146 if (!proc_set_async (pi
))
4147 proc_error (pi
, "target_resume, set_async", __LINE__
);
4150 proc_iterate_over_threads (pi
,
4151 make_signal_thread_runnable
,
4154 pi
= thread
; /* Substitute the thread's procinfo
4160 if (!proc_run_process (pi
, step
, native_signo
))
4163 warning (_("resume: target already running. "
4164 "Pretend to resume, and hope for the best!"));
4166 proc_error (pi
, "target_resume", __LINE__
);
4170 /* Set up to trace signals in the child process. */
4173 procfs_pass_signals (struct target_ops
*self
,
4174 int numsigs
, unsigned char *pass_signals
)
4176 gdb_sigset_t signals
;
4177 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4180 prfillset (&signals
);
4182 for (signo
= 0; signo
< NSIG
; signo
++)
4184 int target_signo
= gdb_signal_from_host (signo
);
4185 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
4186 gdb_prdelset (&signals
, signo
);
4189 if (!proc_set_traced_signals (pi
, &signals
))
4190 proc_error (pi
, "pass_signals", __LINE__
);
4193 /* Print status information about the child process. */
4196 procfs_files_info (struct target_ops
*ignore
)
4198 struct inferior
*inf
= current_inferior ();
4200 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
4201 inf
->attach_flag
? "attached": "child",
4202 target_pid_to_str (inferior_ptid
));
4205 /* Stop the child process asynchronously, as when the gdb user types
4206 control-c or presses a "stop" button. Works by sending
4207 kill(SIGINT) to the child's process group. */
4210 procfs_interrupt (struct target_ops
*self
, ptid_t ptid
)
4212 kill (-inferior_process_group (), SIGINT
);
4215 /* Make it die. Wait for it to die. Clean up after it. Note: this
4216 should only be applied to the real process, not to an LWP, because
4217 of the check for parent-process. If we need this to work for an
4218 LWP, it needs some more logic. */
4221 unconditionally_kill_inferior (procinfo
*pi
)
4225 parent_pid
= proc_parent_pid (pi
);
4226 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4227 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4228 to kill the inferior, otherwise it might remain stopped with a
4230 We do not check the result of the PIOCSSIG, the inferior might have
4233 gdb_siginfo_t newsiginfo
;
4235 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4236 newsiginfo
.si_signo
= SIGKILL
;
4237 newsiginfo
.si_code
= 0;
4238 newsiginfo
.si_errno
= 0;
4239 newsiginfo
.si_pid
= getpid ();
4240 newsiginfo
.si_uid
= getuid ();
4241 /* FIXME: use proc_set_current_signal. */
4242 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4244 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4245 if (!proc_kill (pi
, SIGKILL
))
4246 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4247 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4248 destroy_procinfo (pi
);
4250 /* If pi is GDB's child, wait for it to die. */
4251 if (parent_pid
== getpid ())
4252 /* FIXME: should we use waitpid to make sure we get the right event?
4253 Should we check the returned event? */
4258 ret
= waitpid (pi
->pid
, &status
, 0);
4265 /* We're done debugging it, and we want it to go away. Then we want
4266 GDB to forget all about it. */
4269 procfs_kill_inferior (struct target_ops
*ops
)
4271 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4273 /* Find procinfo for main process. */
4274 procinfo
*pi
= find_procinfo (ptid_get_pid (inferior_ptid
), 0);
4277 unconditionally_kill_inferior (pi
);
4278 target_mourn_inferior (inferior_ptid
);
4282 /* Forget we ever debugged this thing! */
4285 procfs_mourn_inferior (struct target_ops
*ops
)
4289 if (!ptid_equal (inferior_ptid
, null_ptid
))
4291 /* Find procinfo for main process. */
4292 pi
= find_procinfo (ptid_get_pid (inferior_ptid
), 0);
4294 destroy_procinfo (pi
);
4297 generic_mourn_inferior ();
4299 inf_child_maybe_unpush_target (ops
);
4302 /* When GDB forks to create a runnable inferior process, this function
4303 is called on the parent side of the fork. It's job is to do
4304 whatever is necessary to make the child ready to be debugged, and
4305 then wait for the child to synchronize. */
4308 procfs_init_inferior (struct target_ops
*ops
, int pid
)
4311 gdb_sigset_t signals
;
4315 /* This routine called on the parent side (GDB side)
4316 after GDB forks the inferior. */
4317 if (!target_is_pushed (ops
))
4320 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4321 perror (_("procfs: out of memory in 'init_inferior'"));
4323 if (!open_procinfo_files (pi
, FD_CTL
))
4324 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4328 open_procinfo_files // done
4331 procfs_notice_signals
4338 /* If not stopped yet, wait for it to stop. */
4339 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4340 !(proc_wait_for_stop (pi
)))
4341 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4343 /* Save some of the /proc state to be restored if we detach. */
4344 /* FIXME: Why? In case another debugger was debugging it?
4345 We're it's parent, for Ghu's sake! */
4346 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4347 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4348 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4349 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4350 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4351 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4352 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4353 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4354 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4355 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4357 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4358 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4360 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4361 and possibly even turning ON kill-on-last-close at this point. But
4362 I can't make that change without careful testing which I don't have
4363 time to do right now... */
4364 /* Turn on run-on-last-close flag so that the child
4365 will die if GDB goes away for some reason. */
4366 if (!proc_set_run_on_last_close (pi
))
4367 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4369 /* We now have have access to the lwpid of the main thread/lwp. */
4370 lwpid
= proc_get_current_thread (pi
);
4372 /* Create a procinfo for the main lwp. */
4373 create_procinfo (pid
, lwpid
);
4375 /* We already have a main thread registered in the thread table at
4376 this point, but it didn't have any lwp info yet. Notify the core
4377 about it. This changes inferior_ptid as well. */
4378 thread_change_ptid (pid_to_ptid (pid
),
4379 ptid_build (pid
, lwpid
, 0));
4381 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
4384 /* On mips-irix, we need to stop the inferior early enough during
4385 the startup phase in order to be able to load the shared library
4386 symbols and insert the breakpoints that are located in these shared
4387 libraries. Stopping at the program entry point is not good enough
4388 because the -init code is executed before the execution reaches
4391 So what we need to do is to insert a breakpoint in the runtime
4392 loader (rld), more precisely in __dbx_link(). This procedure is
4393 called by rld once all shared libraries have been mapped, but before
4394 the -init code is executed. Unfortuantely, this is not straightforward,
4395 as rld is not part of the executable we are running, and thus we need
4396 the inferior to run until rld itself has been mapped in memory.
4398 For this, we trace all syssgi() syscall exit events. Each time
4399 we detect such an event, we iterate over each text memory maps,
4400 get its associated fd, and scan the symbol table for __dbx_link().
4401 When found, we know that rld has been mapped, and that we can insert
4402 the breakpoint at the symbol address. Once the dbx_link() breakpoint
4403 has been inserted, the syssgi() notifications are no longer necessary,
4404 so they should be canceled. */
4405 proc_trace_syscalls_1 (pi
, SYS_syssgi
, PR_SYSEXIT
, FLAG_SET
, 0);
4409 /* When GDB forks to create a new process, this function is called on
4410 the child side of the fork before GDB exec's the user program. Its
4411 job is to make the child minimally debuggable, so that the parent
4412 GDB process can connect to the child and take over. This function
4413 should do only the minimum to make that possible, and to
4414 synchronize with the parent process. The parent process should
4415 take care of the details. */
4418 procfs_set_exec_trap (void)
4420 /* This routine called on the child side (inferior side)
4421 after GDB forks the inferior. It must use only local variables,
4422 because it may be sharing data space with its parent. */
4427 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4428 perror_with_name (_("procfs: create_procinfo failed in child."));
4430 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4432 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4433 gdb_flush (gdb_stderr
);
4434 /* No need to call "dead_procinfo", because we're going to
4439 #ifdef PRFS_STOPEXEC /* defined on OSF */
4440 /* OSF method for tracing exec syscalls. Quoting:
4441 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4442 exits from exec system calls because of the user level loader. */
4443 /* FIXME: make nice and maybe move into an access function. */
4447 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4449 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4450 gdb_flush (gdb_stderr
);
4453 prfs_flags
|= PRFS_STOPEXEC
;
4455 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4457 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4458 gdb_flush (gdb_stderr
);
4462 #else /* not PRFS_STOPEXEC */
4463 /* Everyone else's (except OSF) method for tracing exec syscalls. */
4465 Not all systems with /proc have all the exec* syscalls with the same
4466 names. On the SGI, for example, there is no SYS_exec, but there
4467 *is* a SYS_execv. So, we try to account for that. */
4469 exitset
= sysset_t_alloc (pi
);
4470 gdb_premptysysset (exitset
);
4472 gdb_praddsysset (exitset
, SYS_exec
);
4475 gdb_praddsysset (exitset
, SYS_execve
);
4478 gdb_praddsysset (exitset
, SYS_execv
);
4480 #ifdef DYNAMIC_SYSCALLS
4482 int callnum
= find_syscall (pi
, "execve");
4485 gdb_praddsysset (exitset
, callnum
);
4487 callnum
= find_syscall (pi
, "ra_execve");
4489 gdb_praddsysset (exitset
, callnum
);
4491 #endif /* DYNAMIC_SYSCALLS */
4493 if (!proc_set_traced_sysexit (pi
, exitset
))
4495 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4496 gdb_flush (gdb_stderr
);
4499 #endif /* PRFS_STOPEXEC */
4501 /* FIXME: should this be done in the parent instead? */
4502 /* Turn off inherit on fork flag so that all grand-children
4503 of gdb start with tracing flags cleared. */
4504 if (!proc_unset_inherit_on_fork (pi
))
4505 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4507 /* Turn off run on last close flag, so that the child process
4508 cannot run away just because we close our handle on it.
4509 We want it to wait for the parent to attach. */
4510 if (!proc_unset_run_on_last_close (pi
))
4511 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4513 /* FIXME: No need to destroy the procinfo --
4514 we have our own address space, and we're about to do an exec! */
4515 /*destroy_procinfo (pi);*/
4518 /* This function is called BEFORE gdb forks the inferior process. Its
4519 only real responsibility is to set things up for the fork, and tell
4520 GDB which two functions to call after the fork (one for the parent,
4521 and one for the child).
4523 This function does a complicated search for a unix shell program,
4524 which it then uses to parse arguments and environment variables to
4525 be sent to the child. I wonder whether this code could not be
4526 abstracted out and shared with other unix targets such as
4530 procfs_create_inferior (struct target_ops
*ops
, const char *exec_file
,
4531 const std::string
&allargs
, char **env
, int from_tty
)
4533 char *shell_file
= getenv ("SHELL");
4537 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4540 /* We will be looking down the PATH to find shell_file. If we
4541 just do this the normal way (via execlp, which operates by
4542 attempting an exec for each element of the PATH until it
4543 finds one which succeeds), then there will be an exec for
4544 each failed attempt, each of which will cause a PR_SYSEXIT
4545 stop, and we won't know how to distinguish the PR_SYSEXIT's
4546 for these failed execs with the ones for successful execs
4547 (whether the exec has succeeded is stored at that time in the
4548 carry bit or some such architecture-specific and
4549 non-ABI-specified place).
4551 So I can't think of anything better than to search the PATH
4552 now. This has several disadvantages: (1) There is a race
4553 condition; if we find a file now and it is deleted before we
4554 exec it, we lose, even if the deletion leaves a valid file
4555 further down in the PATH, (2) there is no way to know exactly
4556 what an executable (in the sense of "capable of being
4557 exec'd") file is. Using access() loses because it may lose
4558 if the caller is the superuser; failing to use it loses if
4559 there are ACLs or some such. */
4563 /* FIXME-maybe: might want "set path" command so user can change what
4564 path is used from within GDB. */
4565 const char *path
= getenv ("PATH");
4567 struct stat statbuf
;
4570 path
= "/bin:/usr/bin";
4572 tryname
= (char *) alloca (strlen (path
) + strlen (shell_file
) + 2);
4573 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4575 p1
= strchr (p
, ':');
4580 strncpy (tryname
, p
, len
);
4581 tryname
[len
] = '\0';
4582 strcat (tryname
, "/");
4583 strcat (tryname
, shell_file
);
4584 if (access (tryname
, X_OK
) < 0)
4586 if (stat (tryname
, &statbuf
) < 0)
4588 if (!S_ISREG (statbuf
.st_mode
))
4589 /* We certainly need to reject directories. I'm not quite
4590 as sure about FIFOs, sockets, etc., but I kind of doubt
4591 that people want to exec() these things. */
4596 /* Not found. This must be an error rather than merely passing
4597 the file to execlp(), because execlp() would try all the
4598 exec()s, causing GDB to get confused. */
4599 error (_("procfs:%d -- Can't find shell %s in PATH"),
4600 __LINE__
, shell_file
);
4602 shell_file
= tryname
;
4605 pid
= fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4606 NULL
, NULL
, shell_file
, NULL
);
4608 /* We have something that executes now. We'll be running through
4609 the shell at this point (if startup-with-shell is true), but the
4610 pid shouldn't change. */
4611 add_thread_silent (pid_to_ptid (pid
));
4613 procfs_init_inferior (ops
, pid
);
4616 /* An observer for the "inferior_created" event. */
4619 procfs_inferior_created (struct target_ops
*ops
, int from_tty
)
4622 /* Make sure to cancel the syssgi() syscall-exit notifications.
4623 They should normally have been removed by now, but they may still
4624 be activated if the inferior doesn't use shared libraries, or if
4625 we didn't locate __dbx_link, or if we never stopped in __dbx_link.
4626 See procfs_init_inferior() for more details.
4628 Since these notifications are only ever enabled when we spawned
4629 the inferior ourselves, there is nothing to do when the inferior
4630 was created by attaching to an already running process, or when
4631 debugging a core file. */
4632 if (current_inferior ()->attach_flag
|| !target_can_run (¤t_target
))
4635 proc_trace_syscalls_1 (find_procinfo_or_die (ptid_get_pid (inferior_ptid
),
4636 0), SYS_syssgi
, PR_SYSEXIT
, FLAG_RESET
, 0);
4640 /* Callback for update_thread_list. Calls "add_thread". */
4643 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
4645 ptid_t gdb_threadid
= ptid_build (pi
->pid
, thread
->tid
, 0);
4647 if (!in_thread_list (gdb_threadid
) || is_exited (gdb_threadid
))
4648 add_thread (gdb_threadid
);
4653 /* Query all the threads that the target knows about, and give them
4654 back to GDB to add to its list. */
4657 procfs_update_thread_list (struct target_ops
*ops
)
4663 /* Find procinfo for main process. */
4664 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4665 proc_update_threads (pi
);
4666 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
4669 /* Return true if the thread is still 'alive'. This guy doesn't
4670 really seem to be doing his job. Got to investigate how to tell
4671 when a thread is really gone. */
4674 procfs_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
4679 proc
= ptid_get_pid (ptid
);
4680 thread
= ptid_get_lwp (ptid
);
4681 /* If I don't know it, it ain't alive! */
4682 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
4685 /* If I can't get its status, it ain't alive!
4686 What's more, I need to forget about it! */
4687 if (!proc_get_status (pi
))
4689 destroy_procinfo (pi
);
4692 /* I couldn't have got its status if it weren't alive, so it's
4697 /* Convert PTID to a string. Returns the string in a static
4701 procfs_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
4703 static char buf
[80];
4705 if (ptid_get_lwp (ptid
) == 0)
4706 sprintf (buf
, "process %d", ptid_get_pid (ptid
));
4708 sprintf (buf
, "LWP %ld", ptid_get_lwp (ptid
));
4713 /* Insert a watchpoint. */
4716 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
4723 pi
= find_procinfo_or_die (ptid_get_pid (ptid
) == -1 ?
4724 ptid_get_pid (inferior_ptid
) : ptid_get_pid (ptid
),
4727 /* Translate from GDB's flags to /proc's. */
4728 if (len
> 0) /* len == 0 means delete watchpoint. */
4730 switch (rwflag
) { /* FIXME: need an enum! */
4731 case hw_write
: /* default watchpoint (write) */
4732 pflags
= WRITE_WATCHFLAG
;
4734 case hw_read
: /* read watchpoint */
4735 pflags
= READ_WATCHFLAG
;
4737 case hw_access
: /* access watchpoint */
4738 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
4740 case hw_execute
: /* execution HW breakpoint */
4741 pflags
= EXEC_WATCHFLAG
;
4743 default: /* Something weird. Return error. */
4746 if (after
) /* Stop after r/w access is completed. */
4747 pflags
|= AFTER_WATCHFLAG
;
4750 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
4752 if (errno
== E2BIG
) /* Typical error for no resources. */
4753 return -1; /* fail */
4754 /* GDB may try to remove the same watchpoint twice.
4755 If a remove request returns no match, don't error. */
4756 if (errno
== ESRCH
&& len
== 0)
4757 return 0; /* ignore */
4758 proc_error (pi
, "set_watchpoint", __LINE__
);
4764 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
4765 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
4766 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
4769 Note: procfs_can_use_hw_breakpoint() is not yet used by all
4770 procfs.c targets due to the fact that some of them still define
4771 target_can_use_hardware_watchpoint. */
4774 procfs_can_use_hw_breakpoint (struct target_ops
*self
,
4776 int cnt
, int othertype
)
4778 /* Due to the way that proc_set_watchpoint() is implemented, host
4779 and target pointers must be of the same size. If they are not,
4780 we can't use hardware watchpoints. This limitation is due to the
4781 fact that proc_set_watchpoint() calls
4782 procfs_address_to_host_pointer(); a close inspection of
4783 procfs_address_to_host_pointer will reveal that an internal error
4784 will be generated when the host and target pointer sizes are
4786 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
4788 if (sizeof (void *) != TYPE_LENGTH (ptr_type
))
4791 /* Other tests here??? */
4796 /* Returns non-zero if process is stopped on a hardware watchpoint
4797 fault, else returns zero. */
4800 procfs_stopped_by_watchpoint (struct target_ops
*ops
)
4804 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4806 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
4808 if (proc_why (pi
) == PR_FAULTED
)
4811 if (proc_what (pi
) == FLTWATCH
)
4815 if (proc_what (pi
) == FLTKWATCH
)
4823 /* Returns 1 if the OS knows the position of the triggered watchpoint,
4824 and sets *ADDR to that address. Returns 0 if OS cannot report that
4825 address. This function is only called if
4826 procfs_stopped_by_watchpoint returned 1, thus no further checks are
4827 done. The function also assumes that ADDR is not NULL. */
4830 procfs_stopped_data_address (struct target_ops
*targ
, CORE_ADDR
*addr
)
4834 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4835 return proc_watchpoint_address (pi
, addr
);
4839 procfs_insert_watchpoint (struct target_ops
*self
,
4840 CORE_ADDR addr
, int len
,
4841 enum target_hw_bp_type type
,
4842 struct expression
*cond
)
4844 if (!target_have_steppable_watchpoint
4845 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
4847 /* When a hardware watchpoint fires off the PC will be left at
4848 the instruction following the one which caused the
4849 watchpoint. It will *NOT* be necessary for GDB to step over
4851 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 1);
4855 /* When a hardware watchpoint fires off the PC will be left at
4856 the instruction which caused the watchpoint. It will be
4857 necessary for GDB to step over the watchpoint. */
4858 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 0);
4863 procfs_remove_watchpoint (struct target_ops
*self
,
4864 CORE_ADDR addr
, int len
,
4865 enum target_hw_bp_type type
,
4866 struct expression
*cond
)
4868 return procfs_set_watchpoint (inferior_ptid
, addr
, 0, 0, 0);
4872 procfs_region_ok_for_hw_watchpoint (struct target_ops
*self
,
4873 CORE_ADDR addr
, int len
)
4875 /* The man page for proc(4) on Solaris 2.6 and up says that the
4876 system can support "thousands" of hardware watchpoints, but gives
4877 no method for finding out how many; It doesn't say anything about
4878 the allowed size for the watched area either. So we just tell
4884 procfs_use_watchpoints (struct target_ops
*t
)
4886 t
->to_stopped_by_watchpoint
= procfs_stopped_by_watchpoint
;
4887 t
->to_insert_watchpoint
= procfs_insert_watchpoint
;
4888 t
->to_remove_watchpoint
= procfs_remove_watchpoint
;
4889 t
->to_region_ok_for_hw_watchpoint
= procfs_region_ok_for_hw_watchpoint
;
4890 t
->to_can_use_hw_breakpoint
= procfs_can_use_hw_breakpoint
;
4891 t
->to_stopped_data_address
= procfs_stopped_data_address
;
4894 /* Memory Mappings Functions: */
4896 /* Call a callback function once for each mapping, passing it the
4897 mapping, an optional secondary callback function, and some optional
4898 opaque data. Quit and return the first non-zero value returned
4901 PI is the procinfo struct for the process to be mapped. FUNC is
4902 the callback function to be called by this iterator. DATA is the
4903 optional opaque data to be passed to the callback function.
4904 CHILD_FUNC is the optional secondary function pointer to be passed
4905 to the child function. Returns the first non-zero return value
4906 from the callback function, or zero. */
4909 iterate_over_mappings (procinfo
*pi
, find_memory_region_ftype child_func
,
4911 int (*func
) (struct prmap
*map
,
4912 find_memory_region_ftype child_func
,
4915 char pathname
[MAX_PROC_NAME_SIZE
];
4916 struct prmap
*prmaps
;
4917 struct prmap
*prmap
;
4921 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
4926 /* Get the number of mappings, allocate space,
4927 and read the mappings into prmaps. */
4930 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
4931 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
4932 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
4934 /* Make sure it gets closed again. */
4935 make_cleanup_close (map_fd
);
4937 /* Use stat to determine the file size, and compute
4938 the number of prmap_t objects it contains. */
4939 if (fstat (map_fd
, &sbuf
) != 0)
4940 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
4942 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
4943 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
4944 if (read (map_fd
, (char *) prmaps
, nmap
* sizeof (*prmaps
))
4945 != (nmap
* sizeof (*prmaps
)))
4946 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
4948 /* Use ioctl command PIOCNMAP to get number of mappings. */
4949 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmap
) != 0)
4950 proc_error (pi
, "iterate_over_mappings (PIOCNMAP)", __LINE__
);
4952 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
4953 if (ioctl (pi
->ctl_fd
, PIOCMAP
, prmaps
) != 0)
4954 proc_error (pi
, "iterate_over_mappings (PIOCMAP)", __LINE__
);
4957 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
4958 if ((funcstat
= (*func
) (prmap
, child_func
, data
)) != 0)
4960 do_cleanups (cleanups
);
4964 do_cleanups (cleanups
);
4968 /* Implements the to_find_memory_regions method. Calls an external
4969 function for each memory region.
4970 Returns the integer value returned by the callback. */
4973 find_memory_regions_callback (struct prmap
*map
,
4974 find_memory_region_ftype func
, void *data
)
4976 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
4978 (map
->pr_mflags
& MA_READ
) != 0,
4979 (map
->pr_mflags
& MA_WRITE
) != 0,
4980 (map
->pr_mflags
& MA_EXEC
) != 0,
4981 1, /* MODIFIED is unknown, pass it as true. */
4985 /* External interface. Calls a callback function once for each
4986 mapped memory region in the child process, passing as arguments:
4988 CORE_ADDR virtual_address,
4990 int read, TRUE if region is readable by the child
4991 int write, TRUE if region is writable by the child
4992 int execute TRUE if region is executable by the child.
4994 Stops iterating and returns the first non-zero value returned by
4998 proc_find_memory_regions (struct target_ops
*self
,
4999 find_memory_region_ftype func
, void *data
)
5001 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
5003 return iterate_over_mappings (pi
, func
, data
,
5004 find_memory_regions_callback
);
5007 /* Returns an ascii representation of a memory mapping's flags. */
5010 mappingflags (long flags
)
5012 static char asciiflags
[8];
5014 strcpy (asciiflags
, "-------");
5015 #if defined (MA_PHYS)
5016 if (flags
& MA_PHYS
)
5017 asciiflags
[0] = 'd';
5019 if (flags
& MA_STACK
)
5020 asciiflags
[1] = 's';
5021 if (flags
& MA_BREAK
)
5022 asciiflags
[2] = 'b';
5023 if (flags
& MA_SHARED
)
5024 asciiflags
[3] = 's';
5025 if (flags
& MA_READ
)
5026 asciiflags
[4] = 'r';
5027 if (flags
& MA_WRITE
)
5028 asciiflags
[5] = 'w';
5029 if (flags
& MA_EXEC
)
5030 asciiflags
[6] = 'x';
5031 return (asciiflags
);
5034 /* Callback function, does the actual work for 'info proc
5038 info_mappings_callback (struct prmap
*map
, find_memory_region_ftype ignore
,
5041 unsigned int pr_off
;
5043 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5044 pr_off
= (unsigned int) map
->pr_offset
;
5046 pr_off
= map
->pr_off
;
5049 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
5050 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
5051 (unsigned long) map
->pr_vaddr
,
5052 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5053 (unsigned long) map
->pr_size
,
5055 mappingflags (map
->pr_mflags
));
5057 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
5058 (unsigned long) map
->pr_vaddr
,
5059 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5060 (unsigned long) map
->pr_size
,
5062 mappingflags (map
->pr_mflags
));
5067 /* Implement the "info proc mappings" subcommand. */
5070 info_proc_mappings (procinfo
*pi
, int summary
)
5073 return; /* No output for summary mode. */
5075 printf_filtered (_("Mapped address spaces:\n\n"));
5076 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
5077 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
5084 printf_filtered (" %18s %18s %10s %10s %7s\n",
5091 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
5092 printf_filtered ("\n");
5095 /* Implement the "info proc" command. */
5098 procfs_info_proc (struct target_ops
*ops
, const char *args
,
5099 enum info_proc_what what
)
5101 struct cleanup
*old_chain
;
5102 procinfo
*process
= NULL
;
5103 procinfo
*thread
= NULL
;
5120 error (_("Not supported on this target."));
5123 old_chain
= make_cleanup (null_cleanup
, 0);
5124 gdb_argv
built_argv (args
);
5125 for (char *arg
: built_argv
)
5127 if (isdigit (arg
[0]))
5129 pid
= strtoul (arg
, &tmp
, 10);
5131 tid
= strtoul (++tmp
, NULL
, 10);
5133 else if (arg
[0] == '/')
5135 tid
= strtoul (arg
+ 1, NULL
, 10);
5139 pid
= ptid_get_pid (inferior_ptid
);
5141 error (_("No current process: you must name one."));
5144 /* Have pid, will travel.
5145 First see if it's a process we're already debugging. */
5146 process
= find_procinfo (pid
, 0);
5147 if (process
== NULL
)
5149 /* No. So open a procinfo for it, but
5150 remember to close it again when finished. */
5151 process
= create_procinfo (pid
, 0);
5152 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5153 if (!open_procinfo_files (process
, FD_CTL
))
5154 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5158 thread
= create_procinfo (pid
, tid
);
5162 printf_filtered (_("process %d flags:\n"), process
->pid
);
5163 proc_prettyprint_flags (proc_flags (process
), 1);
5164 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5165 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5166 if (proc_get_nthreads (process
) > 1)
5167 printf_filtered ("Process has %d threads.\n",
5168 proc_get_nthreads (process
));
5172 printf_filtered (_("thread %d flags:\n"), thread
->tid
);
5173 proc_prettyprint_flags (proc_flags (thread
), 1);
5174 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5175 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5180 info_proc_mappings (process
, 0);
5183 do_cleanups (old_chain
);
5186 /* Modify the status of the system call identified by SYSCALLNUM in
5187 the set of syscalls that are currently traced/debugged.
5189 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
5190 will be updated. Otherwise, the exit syscalls set will be updated.
5192 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
5193 will be disabled. */
5196 proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
, int entry_or_exit
,
5197 int mode
, int from_tty
)
5201 if (entry_or_exit
== PR_SYSENTRY
)
5202 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5204 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5207 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5209 if (mode
== FLAG_SET
)
5210 gdb_praddsysset (sysset
, syscallnum
);
5212 gdb_prdelsysset (sysset
, syscallnum
);
5214 if (entry_or_exit
== PR_SYSENTRY
)
5216 if (!proc_set_traced_sysentry (pi
, sysset
))
5217 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5221 if (!proc_set_traced_sysexit (pi
, sysset
))
5222 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5227 proc_trace_syscalls (const char *args
, int from_tty
, int entry_or_exit
, int mode
)
5231 if (ptid_get_pid (inferior_ptid
) <= 0)
5232 error (_("you must be debugging a process to use this command."));
5234 if (args
== NULL
|| args
[0] == 0)
5235 error_no_arg (_("system call to trace"));
5237 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
5238 if (isdigit (args
[0]))
5240 const int syscallnum
= atoi (args
);
5242 proc_trace_syscalls_1 (pi
, syscallnum
, entry_or_exit
, mode
, from_tty
);
5247 proc_trace_sysentry_cmd (const char *args
, int from_tty
)
5249 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5253 proc_trace_sysexit_cmd (const char *args
, int from_tty
)
5255 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5259 proc_untrace_sysentry_cmd (const char *args
, int from_tty
)
5261 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5265 proc_untrace_sysexit_cmd (const char *args
, int from_tty
)
5267 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5271 _initialize_procfs (void)
5273 observer_attach_inferior_created (procfs_inferior_created
);
5275 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5276 _("Give a trace of entries into the syscall."));
5277 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5278 _("Give a trace of exits from the syscall."));
5279 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5280 _("Cancel a trace of entries into the syscall."));
5281 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5282 _("Cancel a trace of exits from the syscall."));
5285 /* =================== END, GDB "MODULE" =================== */
5289 /* miscellaneous stubs: */
5291 /* The following satisfy a few random symbols mostly created by the
5292 solaris threads implementation, which I will chase down later. */
5294 /* Return a pid for which we guarantee we will be able to find a
5298 procfs_first_available (void)
5300 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);
5303 /* =================== GCORE .NOTE "MODULE" =================== */
5304 #if defined (PIOCOPENLWP) || defined (PCAGENT)
5305 /* gcore only implemented on solaris (so far) */
5308 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
5309 char *note_data
, int *note_size
,
5310 enum gdb_signal stop_signal
)
5312 struct regcache
*regcache
= get_thread_regcache (ptid
);
5313 gdb_gregset_t gregs
;
5314 gdb_fpregset_t fpregs
;
5315 unsigned long merged_pid
;
5317 merged_pid
= ptid_get_lwp (ptid
) << 16 | ptid_get_pid (ptid
);
5319 /* This part is the old method for fetching registers.
5320 It should be replaced by the newer one using regsets
5321 once it is implemented in this platform:
5322 gdbarch_iterate_over_regset_sections(). */
5324 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
5325 inferior_ptid
= ptid
;
5326 target_fetch_registers (regcache
, -1);
5328 fill_gregset (regcache
, &gregs
, -1);
5329 #if defined (NEW_PROC_API)
5330 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
5337 note_data
= (char *) elfcore_write_prstatus (obfd
,
5344 fill_fpregset (regcache
, &fpregs
, -1);
5345 note_data
= (char *) elfcore_write_prfpreg (obfd
,
5354 struct procfs_corefile_thread_data
{
5358 enum gdb_signal stop_signal
;
5362 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
5364 struct procfs_corefile_thread_data
*args
5365 = (struct procfs_corefile_thread_data
*) data
;
5369 ptid_t ptid
= ptid_build (pi
->pid
, thread
->tid
, 0);
5371 args
->note_data
= procfs_do_thread_registers (args
->obfd
, ptid
,
5380 find_signalled_thread (struct thread_info
*info
, void *data
)
5382 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
5383 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
5389 static enum gdb_signal
5390 find_stop_signal (void)
5392 struct thread_info
*info
=
5393 iterate_over_threads (find_signalled_thread
, NULL
);
5396 return info
->suspend
.stop_signal
;
5398 return GDB_SIGNAL_0
;
5402 procfs_make_note_section (struct target_ops
*self
, bfd
*obfd
, int *note_size
)
5404 struct cleanup
*old_chain
;
5405 gdb_gregset_t gregs
;
5406 gdb_fpregset_t fpregs
;
5407 char fname
[16] = {'\0'};
5408 char psargs
[80] = {'\0'};
5409 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
5410 char *note_data
= NULL
;
5412 struct procfs_corefile_thread_data thread_args
;
5415 enum gdb_signal stop_signal
;
5417 if (get_exec_file (0))
5419 strncpy (fname
, lbasename (get_exec_file (0)), sizeof (fname
));
5420 fname
[sizeof (fname
) - 1] = 0;
5421 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
5422 psargs
[sizeof (psargs
) - 1] = 0;
5424 inf_args
= get_inferior_args ();
5425 if (inf_args
&& *inf_args
&&
5426 strlen (inf_args
) < ((int) sizeof (psargs
) - (int) strlen (psargs
)))
5428 strncat (psargs
, " ",
5429 sizeof (psargs
) - strlen (psargs
));
5430 strncat (psargs
, inf_args
,
5431 sizeof (psargs
) - strlen (psargs
));
5435 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
5441 stop_signal
= find_stop_signal ();
5444 fill_gregset (get_current_regcache (), &gregs
, -1);
5445 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
5446 ptid_get_pid (inferior_ptid
),
5447 stop_signal
, &gregs
);
5450 thread_args
.obfd
= obfd
;
5451 thread_args
.note_data
= note_data
;
5452 thread_args
.note_size
= note_size
;
5453 thread_args
.stop_signal
= stop_signal
;
5454 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
,
5456 note_data
= thread_args
.note_data
;
5458 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
5462 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
5463 "CORE", NT_AUXV
, auxv
, auxv_len
);
5469 #else /* !Solaris */
5471 procfs_make_note_section (struct target_ops
*self
, bfd
*obfd
, int *note_size
)
5473 error (_("gcore not implemented for this host."));
5474 return NULL
; /* lint */
5476 #endif /* Solaris */
5477 /* =================== END GCORE .NOTE "MODULE" =================== */