1 /* Machine independent support for Solaris /proc (process file system) for GDB.
3 Copyright (C) 1999-2020 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"
36 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
38 #include <sys/procfs.h>
39 #include <sys/fault.h>
40 #include <sys/syscall.h>
41 #include "gdbsupport/gdb_wait.h"
48 #include "observable.h"
49 #include "gdbsupport/scoped_fd.h"
50 #include "gdbsupport/pathstuff.h"
52 /* This module provides the interface between GDB and the
53 /proc file system, which is used on many versions of Unix
54 as a means for debuggers to control other processes.
56 /proc works by imitating a file system: you open a simulated file
57 that represents the process you wish to interact with, and perform
58 operations on that "file" in order to examine or change the state
61 The most important thing to know about /proc and this module is
62 that there are two very different interfaces to /proc:
64 One that uses the ioctl system call, and another that uses read
65 and write system calls.
67 This module supports only the Solaris version of the read/write
70 #include <sys/types.h>
71 #include <dirent.h> /* opendir/readdir, for listing the LWP's */
73 #include <fcntl.h> /* for O_RDONLY */
74 #include <unistd.h> /* for "X_OK" */
75 #include <sys/stat.h> /* for struct stat */
77 /* Note: procfs-utils.h must be included after the above system header
78 files, because it redefines various system calls using macros.
79 This may be incompatible with the prototype declarations. */
81 #include "proc-utils.h"
83 /* Prototypes for supply_gregset etc. */
86 /* =================== TARGET_OPS "MODULE" =================== */
88 /* This module defines the GDB target vector and its methods. */
91 static enum target_xfer_status
procfs_xfer_memory (gdb_byte
*,
96 class procfs_target final
: public inf_child_target
99 void create_inferior (const char *, const std::string
&,
100 char **, int) override
;
102 void kill () override
;
104 void mourn_inferior () override
;
106 void attach (const char *, int) override
;
107 void detach (inferior
*inf
, int) override
;
109 void resume (ptid_t
, int, enum gdb_signal
) override
;
110 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
112 void fetch_registers (struct regcache
*, int) override
;
113 void store_registers (struct regcache
*, int) override
;
115 enum target_xfer_status
xfer_partial (enum target_object object
,
118 const gdb_byte
*writebuf
,
119 ULONGEST offset
, ULONGEST len
,
120 ULONGEST
*xfered_len
) override
;
122 void pass_signals (gdb::array_view
<const unsigned char>) override
;
124 void files_info () override
;
126 void update_thread_list () override
;
128 bool thread_alive (ptid_t ptid
) override
;
130 std::string
pid_to_str (ptid_t
) override
;
132 char *pid_to_exec_file (int pid
) override
;
134 thread_control_capabilities
get_thread_control_capabilities () override
135 { return tc_schedlock
; }
137 /* find_memory_regions support method for gcore */
138 int find_memory_regions (find_memory_region_ftype func
, void *data
)
141 char *make_corefile_notes (bfd
*, int *) override
;
143 bool info_proc (const char *, enum info_proc_what
) override
;
145 #if PR_MODEL_NATIVE == PR_MODEL_LP64
146 int auxv_parse (gdb_byte
**readptr
,
147 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
151 bool stopped_by_watchpoint () override
;
153 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
154 struct expression
*) override
;
156 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
157 struct expression
*) override
;
159 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
161 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
162 bool stopped_data_address (CORE_ADDR
*) override
;
164 void procfs_init_inferior (int pid
);
167 static procfs_target the_procfs_target
;
169 #if PR_MODEL_NATIVE == PR_MODEL_LP64
170 /* When GDB is built as 64-bit application on Solaris, the auxv data
171 is presented in 64-bit format. We need to provide a custom parser
174 procfs_target::auxv_parse (gdb_byte
**readptr
,
175 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
177 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
178 gdb_byte
*ptr
= *readptr
;
183 if (endptr
- ptr
< 8 * 2)
186 *typep
= extract_unsigned_integer (ptr
, 4, byte_order
);
188 /* The size of data is always 64-bit. If the application is 32-bit,
189 it will be zero extended, as expected. */
190 *valp
= extract_unsigned_integer (ptr
, 8, byte_order
);
198 /* =================== END, TARGET_OPS "MODULE" =================== */
200 /* World Unification:
202 Put any typedefs, defines etc. here that are required for the
203 unification of code that handles different versions of /proc. */
205 enum { READ_WATCHFLAG
= WA_READ
,
206 WRITE_WATCHFLAG
= WA_WRITE
,
207 EXEC_WATCHFLAG
= WA_EXEC
,
208 AFTER_WATCHFLAG
= WA_TRAPAFTER
212 /* =================== STRUCT PROCINFO "MODULE" =================== */
214 /* FIXME: this comment will soon be out of date W.R.T. threads. */
216 /* The procinfo struct is a wrapper to hold all the state information
217 concerning a /proc process. There should be exactly one procinfo
218 for each process, and since GDB currently can debug only one
219 process at a time, that means there should be only one procinfo.
220 All of the LWP's of a process can be accessed indirectly thru the
221 single process procinfo.
223 However, against the day when GDB may debug more than one process,
224 this data structure is kept in a list (which for now will hold no
225 more than one member), and many functions will have a pointer to a
226 procinfo as an argument.
228 There will be a separate procinfo structure for use by the (not yet
229 implemented) "info proc" command, so that we can print useful
230 information about any random process without interfering with the
231 inferior's procinfo information. */
233 /* format strings for /proc paths */
234 #define MAIN_PROC_NAME_FMT "/proc/%d"
235 #define CTL_PROC_NAME_FMT "/proc/%d/ctl"
236 #define AS_PROC_NAME_FMT "/proc/%d/as"
237 #define MAP_PROC_NAME_FMT "/proc/%d/map"
238 #define STATUS_PROC_NAME_FMT "/proc/%d/status"
239 #define MAX_PROC_NAME_SIZE sizeof("/proc/999999/lwp/0123456789/lwpstatus")
241 typedef struct procinfo
{
242 struct procinfo
*next
;
243 int pid
; /* Process ID */
244 int tid
; /* Thread/LWP id */
248 int ignore_next_sigstop
;
250 int ctl_fd
; /* File descriptor for /proc control file */
251 int status_fd
; /* File descriptor for /proc status file */
252 int as_fd
; /* File descriptor for /proc as file */
254 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
256 fltset_t saved_fltset
; /* Saved traced hardware fault set */
257 sigset_t saved_sigset
; /* Saved traced signal set */
258 sigset_t saved_sighold
; /* Saved held signal set */
259 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
260 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
262 pstatus_t prstatus
; /* Current process status info */
264 struct procinfo
*thread_list
;
266 int status_valid
: 1;
268 int fpregs_valid
: 1;
269 int threads_valid
: 1;
272 static char errmsg
[128]; /* shared error msg buffer */
274 /* Function prototypes for procinfo module: */
276 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
277 static procinfo
*find_procinfo (int pid
, int tid
);
278 static procinfo
*create_procinfo (int pid
, int tid
);
279 static void destroy_procinfo (procinfo
*p
);
280 static void dead_procinfo (procinfo
*p
, const char *msg
, int killp
);
281 static int open_procinfo_files (procinfo
*p
, int which
);
282 static void close_procinfo_files (procinfo
*p
);
284 static int iterate_over_mappings
285 (procinfo
*pi
, find_memory_region_ftype child_func
, void *data
,
286 int (*func
) (struct prmap
*map
, find_memory_region_ftype child_func
,
289 /* The head of the procinfo list: */
290 static procinfo
*procinfo_list
;
292 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
296 find_procinfo (int pid
, int tid
)
300 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
307 /* Don't check threads_valid. If we're updating the
308 thread_list, we want to find whatever threads are already
309 here. This means that in general it is the caller's
310 responsibility to check threads_valid and update before
311 calling find_procinfo, if the caller wants to find a new
314 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
322 /* Calls find_procinfo, but errors on failure. */
325 find_procinfo_or_die (int pid
, int tid
)
327 procinfo
*pi
= find_procinfo (pid
, tid
);
332 error (_("procfs: couldn't find pid %d "
333 "(kernel thread %d) in procinfo list."),
336 error (_("procfs: couldn't find pid %d in procinfo list."), pid
);
341 /* Wrapper for `open'. The appropriate open call is attempted; if
342 unsuccessful, it will be retried as many times as needed for the
343 EAGAIN and EINTR conditions.
345 For other conditions, retry the open a limited number of times. In
346 addition, a short sleep is imposed prior to retrying the open. The
347 reason for this sleep is to give the kernel a chance to catch up
348 and create the file in question in the event that GDB "wins" the
349 race to open a file before the kernel has created it. */
352 open_with_retry (const char *pathname
, int flags
)
354 int retries_remaining
, status
;
356 retries_remaining
= 2;
360 status
= open (pathname
, flags
);
362 if (status
>= 0 || retries_remaining
== 0)
364 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
374 /* Open the file descriptor for the process or LWP. We only open the
375 control file descriptor; the others are opened lazily as needed.
376 Returns the file descriptor, or zero for failure. */
378 enum { FD_CTL
, FD_STATUS
, FD_AS
};
381 open_procinfo_files (procinfo
*pi
, int which
)
383 char tmp
[MAX_PROC_NAME_SIZE
];
386 /* This function is getting ALMOST long enough to break up into
387 several. Here is some rationale:
389 There are several file descriptors that may need to be open
390 for any given process or LWP. The ones we're interested in are:
391 - control (ctl) write-only change the state
392 - status (status) read-only query the state
393 - address space (as) read/write access memory
394 - map (map) read-only virtual addr map
395 Most of these are opened lazily as they are needed.
396 The pathnames for the 'files' for an LWP look slightly
397 different from those of a first-class process:
398 Pathnames for a process (<proc-id>):
400 /proc/<proc-id>/status
403 Pathnames for an LWP (lwp-id):
404 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
405 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
406 An LWP has no map or address space file descriptor, since
407 the memory map and address space are shared by all LWPs. */
409 /* In this case, there are several different file descriptors that
410 we might be asked to open. The control file descriptor will be
411 opened early, but the others will be opened lazily as they are
414 strcpy (tmp
, pi
->pathname
);
415 switch (which
) { /* Which file descriptor to open? */
418 strcat (tmp
, "/lwpctl");
420 strcat (tmp
, "/ctl");
421 fd
= open_with_retry (tmp
, O_WRONLY
);
428 return 0; /* There is no 'as' file descriptor for an lwp. */
430 fd
= open_with_retry (tmp
, O_RDWR
);
437 strcat (tmp
, "/lwpstatus");
439 strcat (tmp
, "/status");
440 fd
= open_with_retry (tmp
, O_RDONLY
);
446 return 0; /* unknown file descriptor */
449 return 1; /* success */
452 /* Allocate a data structure and link it into the procinfo list.
453 First tries to find a pre-existing one (FIXME: why?). Returns the
454 pointer to new procinfo struct. */
457 create_procinfo (int pid
, int tid
)
459 procinfo
*pi
, *parent
= NULL
;
461 pi
= find_procinfo (pid
, tid
);
463 return pi
; /* Already exists, nothing to do. */
465 /* Find parent before doing malloc, to save having to cleanup. */
467 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
469 doesn't exist yet? */
471 pi
= XNEW (procinfo
);
472 memset (pi
, 0, sizeof (procinfo
));
476 pi
->saved_entryset
= XNEW (sysset_t
);
477 pi
->saved_exitset
= XNEW (sysset_t
);
479 /* Chain into list. */
482 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), MAIN_PROC_NAME_FMT
, pid
);
483 pi
->next
= procinfo_list
;
488 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), "/proc/%d/lwp/%d",
490 pi
->next
= parent
->thread_list
;
491 parent
->thread_list
= pi
;
496 /* Close all file descriptors associated with the procinfo. */
499 close_procinfo_files (procinfo
*pi
)
505 if (pi
->status_fd
> 0)
506 close (pi
->status_fd
);
507 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
510 /* Destructor function. Close, unlink and deallocate the object. */
513 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
517 /* Step one: unlink the procinfo from its list. */
521 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
524 ptr
->next
= pi
->next
;
528 /* Step two: close any open file descriptors. */
529 close_procinfo_files (pi
);
531 /* Step three: free the memory. */
532 xfree (pi
->saved_entryset
);
533 xfree (pi
->saved_exitset
);
538 destroy_procinfo (procinfo
*pi
)
542 if (pi
->tid
!= 0) /* Destroy a thread procinfo. */
544 tmp
= find_procinfo (pi
->pid
, 0); /* Find the parent process. */
545 destroy_one_procinfo (&tmp
->thread_list
, pi
);
547 else /* Destroy a process procinfo and all its threads. */
549 /* First destroy the children, if any; */
550 while (pi
->thread_list
!= NULL
)
551 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
552 /* Then destroy the parent. Genocide!!! */
553 destroy_one_procinfo (&procinfo_list
, pi
);
557 /* A deleter that calls destroy_procinfo. */
558 struct procinfo_deleter
560 void operator() (procinfo
*pi
) const
562 destroy_procinfo (pi
);
566 typedef std::unique_ptr
<procinfo
, procinfo_deleter
> procinfo_up
;
568 enum { NOKILL
, KILL
};
570 /* To be called on a non_recoverable error for a procinfo. Prints
571 error messages, optionally sends a SIGKILL to the process, then
572 destroys the data structure. */
575 dead_procinfo (procinfo
*pi
, const char *msg
, int kill_p
)
580 print_sys_errmsg (pi
->pathname
, errno
);
583 xsnprintf (procfile
, sizeof (procfile
), "process %d", pi
->pid
);
584 print_sys_errmsg (procfile
, errno
);
587 kill (pi
->pid
, SIGKILL
);
589 destroy_procinfo (pi
);
593 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
595 /* =================== /proc "MODULE" =================== */
597 /* This "module" is the interface layer between the /proc system API
598 and the gdb target vector functions. This layer consists of access
599 functions that encapsulate each of the basic operations that we
600 need to use from the /proc API.
602 The main motivation for this layer is to hide the fact that there
603 are two very different implementations of the /proc API. Rather
604 than have a bunch of #ifdefs all thru the gdb target vector
605 functions, we do our best to hide them all in here. */
607 static long proc_flags (procinfo
*pi
);
608 static int proc_why (procinfo
*pi
);
609 static int proc_what (procinfo
*pi
);
610 static int proc_set_current_signal (procinfo
*pi
, int signo
);
611 static int proc_get_current_thread (procinfo
*pi
);
612 static int proc_iterate_over_threads
614 int (*func
) (procinfo
*, procinfo
*, void *),
618 proc_warn (procinfo
*pi
, const char *func
, int line
)
620 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
621 func
, line
, pi
->pathname
);
622 print_sys_errmsg (errmsg
, errno
);
626 proc_error (procinfo
*pi
, const char *func
, int line
)
628 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
629 func
, line
, pi
->pathname
);
630 perror_with_name (errmsg
);
633 /* Updates the status struct in the procinfo. There is a 'valid'
634 flag, to let other functions know when this function needs to be
635 called (so the status is only read when it is needed). The status
636 file descriptor is also only opened when it is needed. Returns
637 non-zero for success, zero for failure. */
640 proc_get_status (procinfo
*pi
)
642 /* Status file descriptor is opened "lazily". */
643 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
645 pi
->status_valid
= 0;
649 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
650 pi
->status_valid
= 0; /* fail */
653 /* Sigh... I have to read a different data structure,
654 depending on whether this is a main process or an LWP. */
656 pi
->status_valid
= (read (pi
->status_fd
,
657 (char *) &pi
->prstatus
.pr_lwp
,
658 sizeof (lwpstatus_t
))
659 == sizeof (lwpstatus_t
));
662 pi
->status_valid
= (read (pi
->status_fd
,
663 (char *) &pi
->prstatus
,
665 == sizeof (pstatus_t
));
669 if (pi
->status_valid
)
671 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
674 proc_get_current_thread (pi
));
677 /* The status struct includes general regs, so mark them valid too. */
678 pi
->gregs_valid
= pi
->status_valid
;
679 /* In the read/write multiple-fd model, the status struct includes
680 the fp regs too, so mark them valid too. */
681 pi
->fpregs_valid
= pi
->status_valid
;
682 return pi
->status_valid
; /* True if success, false if failure. */
685 /* Returns the process flags (pr_flags field). */
688 proc_flags (procinfo
*pi
)
690 if (!pi
->status_valid
)
691 if (!proc_get_status (pi
))
692 return 0; /* FIXME: not a good failure value (but what is?) */
694 return pi
->prstatus
.pr_lwp
.pr_flags
;
697 /* Returns the pr_why field (why the process stopped). */
700 proc_why (procinfo
*pi
)
702 if (!pi
->status_valid
)
703 if (!proc_get_status (pi
))
704 return 0; /* FIXME: not a good failure value (but what is?) */
706 return pi
->prstatus
.pr_lwp
.pr_why
;
709 /* Returns the pr_what field (details of why the process stopped). */
712 proc_what (procinfo
*pi
)
714 if (!pi
->status_valid
)
715 if (!proc_get_status (pi
))
716 return 0; /* FIXME: not a good failure value (but what is?) */
718 return pi
->prstatus
.pr_lwp
.pr_what
;
721 /* This function is only called when PI is stopped by a watchpoint.
722 Assuming the OS supports it, write to *ADDR the data address which
723 triggered it and return 1. Return 0 if it is not possible to know
727 proc_watchpoint_address (procinfo
*pi
, CORE_ADDR
*addr
)
729 if (!pi
->status_valid
)
730 if (!proc_get_status (pi
))
733 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
734 builtin_type (target_gdbarch ())->builtin_data_ptr
,
735 (gdb_byte
*) &pi
->prstatus
.pr_lwp
.pr_info
.si_addr
);
739 /* Returns the pr_nsysarg field (number of args to the current
743 proc_nsysarg (procinfo
*pi
)
745 if (!pi
->status_valid
)
746 if (!proc_get_status (pi
))
749 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
752 /* Returns the pr_sysarg field (pointer to the arguments of current
756 proc_sysargs (procinfo
*pi
)
758 if (!pi
->status_valid
)
759 if (!proc_get_status (pi
))
762 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
765 /* Set or reset any of the following process flags:
766 PR_FORK -- forked child will inherit trace flags
767 PR_RLC -- traced process runs when last /proc file closed.
768 PR_KLC -- traced process is killed when last /proc file closed.
769 PR_ASYNC -- LWP's get to run/stop independently.
771 This function is done using read/write [PCSET/PCRESET/PCUNSET].
775 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
776 mode -- 1 for set, 0 for reset.
778 Returns non-zero for success, zero for failure. */
780 enum { FLAG_RESET
, FLAG_SET
};
783 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
785 long win
= 0; /* default to fail */
787 /* These operations affect the process as a whole, and applying them
788 to an individual LWP has the same meaning as applying them to the
789 main process. Therefore, if we're ever called with a pointer to
790 an LWP's procinfo, let's substitute the process's procinfo and
791 avoid opening the LWP's file descriptor unnecessarily. */
794 pi
= find_procinfo_or_die (pi
->pid
, 0);
798 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC). */
800 else /* Reset the flag. */
804 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
806 /* The above operation renders the procinfo's cached pstatus
808 pi
->status_valid
= 0;
811 warning (_("procfs: modify_flag failed to turn %s %s"),
812 flag
== PR_FORK
? "PR_FORK" :
813 flag
== PR_RLC
? "PR_RLC" :
814 flag
== PR_ASYNC
? "PR_ASYNC" :
815 flag
== PR_KLC
? "PR_KLC" :
817 mode
== FLAG_RESET
? "off" : "on");
822 /* Set the run_on_last_close flag. Process with all threads will
823 become runnable when debugger closes all /proc fds. Returns
824 non-zero for success, zero for failure. */
827 proc_set_run_on_last_close (procinfo
*pi
)
829 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
832 /* Reset the run_on_last_close flag. The process will NOT become
833 runnable when debugger closes its file handles. Returns non-zero
834 for success, zero for failure. */
837 proc_unset_run_on_last_close (procinfo
*pi
)
839 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
842 /* Reset inherit_on_fork flag. If the process forks a child while we
843 are registered for events in the parent, then we will NOT receive
844 events from the child. Returns non-zero for success, zero for
848 proc_unset_inherit_on_fork (procinfo
*pi
)
850 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
853 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
854 (signal etc.), the remaining LWPs will continue to run. Returns
855 non-zero for success, zero for failure. */
858 proc_set_async (procinfo
*pi
)
860 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
863 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
864 (signal etc.), then all other LWPs will stop as well. Returns
865 non-zero for success, zero for failure. */
868 proc_unset_async (procinfo
*pi
)
870 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
873 /* Request the process/LWP to stop. Does not wait. Returns non-zero
874 for success, zero for failure. */
877 proc_stop_process (procinfo
*pi
)
881 /* We might conceivably apply this operation to an LWP, and the
882 LWP's ctl file descriptor might not be open. */
884 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
888 procfs_ctl_t cmd
= PCSTOP
;
890 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
896 /* Wait for the process or LWP to stop (block until it does). Returns
897 non-zero for success, zero for failure. */
900 proc_wait_for_stop (procinfo
*pi
)
904 /* We should never have to apply this operation to any procinfo
905 except the one for the main process. If that ever changes for
906 any reason, then take out the following clause and replace it
907 with one that makes sure the ctl_fd is open. */
910 pi
= find_procinfo_or_die (pi
->pid
, 0);
912 procfs_ctl_t cmd
= PCWSTOP
;
916 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
918 clear_sigint_trap ();
920 /* We been runnin' and we stopped -- need to update status. */
921 pi
->status_valid
= 0;
926 /* Make the process or LWP runnable.
928 Options (not all are implemented):
930 - clear current fault
931 - clear current signal
932 - abort the current system call
933 - stop as soon as finished with system call
934 - (ioctl): set traced signal set
935 - (ioctl): set held signal set
936 - (ioctl): set traced fault set
937 - (ioctl): set start pc (vaddr)
939 Always clears the current fault. PI is the process or LWP to
940 operate on. If STEP is true, set the process or LWP to trap after
941 one instruction. If SIGNO is zero, clear the current signal if
942 any; if non-zero, set the current signal to this one. Returns
943 non-zero for success, zero for failure. */
946 proc_run_process (procinfo
*pi
, int step
, int signo
)
951 /* We will probably have to apply this operation to individual
952 threads, so make sure the control file descriptor is open. */
954 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
957 runflags
= PRCFAULT
; /* Always clear current fault. */
962 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals. */
963 proc_set_current_signal (pi
, signo
);
969 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
974 /* Register to trace signals in the process or LWP. Returns non-zero
975 for success, zero for failure. */
978 proc_set_traced_signals (procinfo
*pi
, sigset_t
*sigset
)
982 /* We should never have to apply this operation to any procinfo
983 except the one for the main process. If that ever changes for
984 any reason, then take out the following clause and replace it
985 with one that makes sure the ctl_fd is open. */
988 pi
= find_procinfo_or_die (pi
->pid
, 0);
992 /* Use char array to avoid alignment issues. */
993 char sigset
[sizeof (sigset_t
)];
997 memcpy (&arg
.sigset
, sigset
, sizeof (sigset_t
));
999 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1001 /* The above operation renders the procinfo's cached pstatus obsolete. */
1002 pi
->status_valid
= 0;
1005 warning (_("procfs: set_traced_signals failed"));
1009 /* Register to trace hardware faults in the process or LWP. Returns
1010 non-zero for success, zero for failure. */
1013 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1017 /* We should never have to apply this operation to any procinfo
1018 except the one for the main process. If that ever changes for
1019 any reason, then take out the following clause and replace it
1020 with one that makes sure the ctl_fd is open. */
1023 pi
= find_procinfo_or_die (pi
->pid
, 0);
1027 /* Use char array to avoid alignment issues. */
1028 char fltset
[sizeof (fltset_t
)];
1032 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1034 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1036 /* The above operation renders the procinfo's cached pstatus obsolete. */
1037 pi
->status_valid
= 0;
1042 /* Register to trace entry to system calls in the process or LWP.
1043 Returns non-zero for success, zero for failure. */
1046 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1050 /* We should never have to apply this operation to any procinfo
1051 except the one for the main process. If that ever changes for
1052 any reason, then take out the following clause and replace it
1053 with one that makes sure the ctl_fd is open. */
1056 pi
= find_procinfo_or_die (pi
->pid
, 0);
1060 /* Use char array to avoid alignment issues. */
1061 char sysset
[sizeof (sysset_t
)];
1065 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1067 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1069 /* The above operation renders the procinfo's cached pstatus
1071 pi
->status_valid
= 0;
1076 /* Register to trace exit from system calls in the process or LWP.
1077 Returns non-zero for success, zero for failure. */
1080 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1084 /* We should never have to apply this operation to any procinfo
1085 except the one for the main process. If that ever changes for
1086 any reason, then take out the following clause and replace it
1087 with one that makes sure the ctl_fd is open. */
1090 pi
= find_procinfo_or_die (pi
->pid
, 0);
1092 struct gdb_proc_ctl_pcsexit
{
1094 /* Use char array to avoid alignment issues. */
1095 char sysset
[sizeof (sysset_t
)];
1099 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1101 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1103 /* The above operation renders the procinfo's cached pstatus
1105 pi
->status_valid
= 0;
1110 /* Specify the set of blocked / held signals in the process or LWP.
1111 Returns non-zero for success, zero for failure. */
1114 proc_set_held_signals (procinfo
*pi
, sigset_t
*sighold
)
1118 /* We should never have to apply this operation to any procinfo
1119 except the one for the main process. If that ever changes for
1120 any reason, then take out the following clause and replace it
1121 with one that makes sure the ctl_fd is open. */
1124 pi
= find_procinfo_or_die (pi
->pid
, 0);
1128 /* Use char array to avoid alignment issues. */
1129 char hold
[sizeof (sigset_t
)];
1133 memcpy (&arg
.hold
, sighold
, sizeof (sigset_t
));
1134 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1136 /* The above operation renders the procinfo's cached pstatus
1138 pi
->status_valid
= 0;
1143 /* Returns the set of signals that are held / blocked. Will also copy
1144 the sigset if SAVE is non-zero. */
1147 proc_get_held_signals (procinfo
*pi
, sigset_t
*save
)
1149 sigset_t
*ret
= NULL
;
1151 /* We should never have to apply this operation to any procinfo
1152 except the one for the main process. If that ever changes for
1153 any reason, then take out the following clause and replace it
1154 with one that makes sure the ctl_fd is open. */
1157 pi
= find_procinfo_or_die (pi
->pid
, 0);
1159 if (!pi
->status_valid
)
1160 if (!proc_get_status (pi
))
1163 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1165 memcpy (save
, ret
, sizeof (sigset_t
));
1170 /* Returns the set of signals that are traced / debugged. Will also
1171 copy the sigset if SAVE is non-zero. */
1174 proc_get_traced_signals (procinfo
*pi
, sigset_t
*save
)
1176 sigset_t
*ret
= NULL
;
1178 /* We should never have to apply this operation to any procinfo
1179 except the one for the main process. If that ever changes for
1180 any reason, then take out the following clause and replace it
1181 with one that makes sure the ctl_fd is open. */
1184 pi
= find_procinfo_or_die (pi
->pid
, 0);
1186 if (!pi
->status_valid
)
1187 if (!proc_get_status (pi
))
1190 ret
= &pi
->prstatus
.pr_sigtrace
;
1192 memcpy (save
, ret
, sizeof (sigset_t
));
1197 /* Returns the set of hardware faults that are traced /debugged. Will
1198 also copy the faultset if SAVE is non-zero. */
1201 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1203 fltset_t
*ret
= NULL
;
1205 /* We should never have to apply this operation to any procinfo
1206 except the one for the main process. If that ever changes for
1207 any reason, then take out the following clause and replace it
1208 with one that makes sure the ctl_fd is open. */
1211 pi
= find_procinfo_or_die (pi
->pid
, 0);
1213 if (!pi
->status_valid
)
1214 if (!proc_get_status (pi
))
1217 ret
= &pi
->prstatus
.pr_flttrace
;
1219 memcpy (save
, ret
, sizeof (fltset_t
));
1224 /* Returns the set of syscalls that are traced /debugged on entry.
1225 Will also copy the syscall set if SAVE is non-zero. */
1228 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1230 sysset_t
*ret
= NULL
;
1232 /* We should never have to apply this operation to any procinfo
1233 except the one for the main process. If that ever changes for
1234 any reason, then take out the following clause and replace it
1235 with one that makes sure the ctl_fd is open. */
1238 pi
= find_procinfo_or_die (pi
->pid
, 0);
1240 if (!pi
->status_valid
)
1241 if (!proc_get_status (pi
))
1244 ret
= &pi
->prstatus
.pr_sysentry
;
1246 memcpy (save
, ret
, sizeof (sysset_t
));
1251 /* Returns the set of syscalls that are traced /debugged on exit.
1252 Will also copy the syscall set if SAVE is non-zero. */
1255 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1257 sysset_t
*ret
= NULL
;
1259 /* We should never have to apply this operation to any procinfo
1260 except the one for the main process. If that ever changes for
1261 any reason, then take out the following clause and replace it
1262 with one that makes sure the ctl_fd is open. */
1265 pi
= find_procinfo_or_die (pi
->pid
, 0);
1267 if (!pi
->status_valid
)
1268 if (!proc_get_status (pi
))
1271 ret
= &pi
->prstatus
.pr_sysexit
;
1273 memcpy (save
, ret
, sizeof (sysset_t
));
1278 /* The current fault (if any) is cleared; the associated signal will
1279 not be sent to the process or LWP when it resumes. Returns
1280 non-zero for success, zero for failure. */
1283 proc_clear_current_fault (procinfo
*pi
)
1287 /* We should never have to apply this operation to any procinfo
1288 except the one for the main process. If that ever changes for
1289 any reason, then take out the following clause and replace it
1290 with one that makes sure the ctl_fd is open. */
1293 pi
= find_procinfo_or_die (pi
->pid
, 0);
1295 procfs_ctl_t cmd
= PCCFAULT
;
1297 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1302 /* Set the "current signal" that will be delivered next to the
1303 process. NOTE: semantics are different from those of KILL. This
1304 signal will be delivered to the process or LWP immediately when it
1305 is resumed (even if the signal is held/blocked); it will NOT
1306 immediately cause another event of interest, and will NOT first
1307 trap back to the debugger. Returns non-zero for success, zero for
1311 proc_set_current_signal (procinfo
*pi
, int signo
)
1316 /* Use char array to avoid alignment issues. */
1317 char sinfo
[sizeof (siginfo_t
)];
1320 process_stratum_target
*wait_target
;
1322 struct target_waitstatus wait_status
;
1324 /* We should never have to apply this operation to any procinfo
1325 except the one for the main process. If that ever changes for
1326 any reason, then take out the following clause and replace it
1327 with one that makes sure the ctl_fd is open. */
1330 pi
= find_procinfo_or_die (pi
->pid
, 0);
1332 /* The pointer is just a type alias. */
1333 get_last_target_status (&wait_target
, &wait_ptid
, &wait_status
);
1334 if (wait_target
== &the_procfs_target
1335 && wait_ptid
== inferior_ptid
1336 && wait_status
.kind
== TARGET_WAITKIND_STOPPED
1337 && wait_status
.value
.sig
== gdb_signal_from_host (signo
)
1338 && proc_get_status (pi
)
1339 && pi
->prstatus
.pr_lwp
.pr_info
.si_signo
== signo
1341 /* Use the siginfo associated with the signal being
1343 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_lwp
.pr_info
, sizeof (siginfo_t
));
1346 mysinfo
.si_signo
= signo
;
1347 mysinfo
.si_code
= 0;
1348 mysinfo
.si_pid
= getpid (); /* ?why? */
1349 mysinfo
.si_uid
= getuid (); /* ?why? */
1350 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1354 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1359 /* The current signal (if any) is cleared, and is not sent to the
1360 process or LWP when it resumes. Returns non-zero for success, zero
1364 proc_clear_current_signal (procinfo
*pi
)
1368 /* We should never have to apply this operation to any procinfo
1369 except the one for the main process. If that ever changes for
1370 any reason, then take out the following clause and replace it
1371 with one that makes sure the ctl_fd is open. */
1374 pi
= find_procinfo_or_die (pi
->pid
, 0);
1378 /* Use char array to avoid alignment issues. */
1379 char sinfo
[sizeof (siginfo_t
)];
1384 /* The pointer is just a type alias. */
1385 mysinfo
.si_signo
= 0;
1386 mysinfo
.si_code
= 0;
1387 mysinfo
.si_errno
= 0;
1388 mysinfo
.si_pid
= getpid (); /* ?why? */
1389 mysinfo
.si_uid
= getuid (); /* ?why? */
1390 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1392 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1397 /* Return the general-purpose registers for the process or LWP
1398 corresponding to PI. Upon failure, return NULL. */
1400 static gdb_gregset_t
*
1401 proc_get_gregs (procinfo
*pi
)
1403 if (!pi
->status_valid
|| !pi
->gregs_valid
)
1404 if (!proc_get_status (pi
))
1407 return &pi
->prstatus
.pr_lwp
.pr_reg
;
1410 /* Return the general-purpose registers for the process or LWP
1411 corresponding to PI. Upon failure, return NULL. */
1413 static gdb_fpregset_t
*
1414 proc_get_fpregs (procinfo
*pi
)
1416 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
1417 if (!proc_get_status (pi
))
1420 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
1423 /* Write the general-purpose registers back to the process or LWP
1424 corresponding to PI. Return non-zero for success, zero for
1428 proc_set_gregs (procinfo
*pi
)
1430 gdb_gregset_t
*gregs
;
1433 gregs
= proc_get_gregs (pi
);
1435 return 0; /* proc_get_regs has already warned. */
1437 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1443 /* Use char array to avoid alignment issues. */
1444 char gregs
[sizeof (gdb_gregset_t
)];
1448 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
1449 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1452 /* Policy: writing the registers invalidates our cache. */
1453 pi
->gregs_valid
= 0;
1457 /* Write the floating-pointer registers back to the process or LWP
1458 corresponding to PI. Return non-zero for success, zero for
1462 proc_set_fpregs (procinfo
*pi
)
1464 gdb_fpregset_t
*fpregs
;
1467 fpregs
= proc_get_fpregs (pi
);
1469 return 0; /* proc_get_fpregs has already warned. */
1471 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1477 /* Use char array to avoid alignment issues. */
1478 char fpregs
[sizeof (gdb_fpregset_t
)];
1482 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
1483 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1486 /* Policy: writing the registers invalidates our cache. */
1487 pi
->fpregs_valid
= 0;
1491 /* Send a signal to the proc or lwp with the semantics of "kill()".
1492 Returns non-zero for success, zero for failure. */
1495 proc_kill (procinfo
*pi
, int signo
)
1499 /* We might conceivably apply this operation to an LWP, and the
1500 LWP's ctl file descriptor might not be open. */
1502 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1506 procfs_ctl_t cmd
[2];
1510 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1516 /* Find the pid of the process that started this one. Returns the
1517 parent process pid, or zero. */
1520 proc_parent_pid (procinfo
*pi
)
1522 /* We should never have to apply this operation to any procinfo
1523 except the one for the main process. If that ever changes for
1524 any reason, then take out the following clause and replace it
1525 with one that makes sure the ctl_fd is open. */
1528 pi
= find_procinfo_or_die (pi
->pid
, 0);
1530 if (!pi
->status_valid
)
1531 if (!proc_get_status (pi
))
1534 return pi
->prstatus
.pr_ppid
;
1537 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
1538 (a.k.a void pointer)! */
1541 procfs_address_to_host_pointer (CORE_ADDR addr
)
1543 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
1546 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (ptr_type
));
1547 gdbarch_address_to_pointer (target_gdbarch (), ptr_type
,
1548 (gdb_byte
*) &ptr
, addr
);
1553 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
1557 char watch
[sizeof (prwatch_t
)];
1561 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
1562 convert a target address into something that can be stored in a
1563 native data structure. */
1564 pwatch
.pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
1565 pwatch
.pr_size
= len
;
1566 pwatch
.pr_wflags
= wflags
;
1568 memcpy (arg
.watch
, &pwatch
, sizeof (prwatch_t
));
1569 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
1572 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
1574 #include <sys/sysi86.h>
1576 /* The KEY is actually the value of the lower 16 bits of the GS
1577 register for the LWP that we're interested in. Returns the
1578 matching ssh struct (LDT entry). */
1581 proc_get_LDT_entry (procinfo
*pi
, int key
) /* ARI: editCase function */
1583 static struct ssd
*ldt_entry
= NULL
;
1584 char pathname
[MAX_PROC_NAME_SIZE
];
1586 /* Allocate space for one LDT entry.
1587 This alloc must persist, because we return a pointer to it. */
1588 if (ldt_entry
== NULL
)
1589 ldt_entry
= XNEW (struct ssd
);
1591 /* Open the file descriptor for the LDT table. */
1592 xsnprintf (pathname
, sizeof (pathname
), "/proc/%d/ldt", pi
->pid
);
1593 scoped_fd
fd (open_with_retry (pathname
, O_RDONLY
));
1596 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
1600 /* Now 'read' thru the table, find a match and return it. */
1601 while (read (fd
.get (), ldt_entry
, sizeof (struct ssd
))
1602 == sizeof (struct ssd
))
1604 if (ldt_entry
->sel
== 0
1605 && ldt_entry
->bo
== 0
1606 && ldt_entry
->acc1
== 0
1607 && ldt_entry
->acc2
== 0)
1608 break; /* end of table */
1609 /* If key matches, return this entry. */
1610 if (ldt_entry
->sel
== key
)
1613 /* Loop ended, match not found. */
1617 /* Returns the pointer to the LDT entry of PTID. */
1620 procfs_find_LDT_entry (ptid_t ptid
) /* ARI: editCase function */
1622 gdb_gregset_t
*gregs
;
1626 /* Find procinfo for the lwp. */
1627 pi
= find_procinfo (ptid
.pid (), ptid
.lwp ());
1630 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
1631 ptid
.pid (), ptid
.lwp ());
1634 /* get its general registers. */
1635 gregs
= proc_get_gregs (pi
);
1638 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
1639 ptid
.pid (), ptid
.lwp ());
1642 /* Now extract the GS register's lower 16 bits. */
1643 key
= (*gregs
)[GS
] & 0xffff;
1645 /* Find the matching entry and return it. */
1646 return proc_get_LDT_entry (pi
, key
);
1651 /* =============== END, non-thread part of /proc "MODULE" =============== */
1653 /* =================== Thread "MODULE" =================== */
1655 /* NOTE: you'll see more ifdefs and duplication of functions here,
1656 since there is a different way to do threads on every OS. */
1658 /* Returns the number of threads for the process. */
1661 proc_get_nthreads (procinfo
*pi
)
1663 if (!pi
->status_valid
)
1664 if (!proc_get_status (pi
))
1667 /* Only works for the process procinfo, because the LWP procinfos do not
1668 get prstatus filled in. */
1669 if (pi
->tid
!= 0) /* Find the parent process procinfo. */
1670 pi
= find_procinfo_or_die (pi
->pid
, 0);
1671 return pi
->prstatus
.pr_nlwp
;
1676 Return the ID of the thread that had an event of interest.
1677 (ie. the one that hit a breakpoint or other traced event). All
1678 other things being equal, this should be the ID of a thread that is
1679 currently executing. */
1682 proc_get_current_thread (procinfo
*pi
)
1684 /* Note: this should be applied to the root procinfo for the
1685 process, not to the procinfo for an LWP. If applied to the
1686 procinfo for an LWP, it will simply return that LWP's ID. In
1687 that case, find the parent process procinfo. */
1690 pi
= find_procinfo_or_die (pi
->pid
, 0);
1692 if (!pi
->status_valid
)
1693 if (!proc_get_status (pi
))
1696 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
1699 /* Discover the IDs of all the threads within the process, and create
1700 a procinfo for each of them (chained to the parent). This
1701 unfortunately requires a different method on every OS. Returns
1702 non-zero for success, zero for failure. */
1705 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
1707 if (thread
&& parent
) /* sanity */
1709 thread
->status_valid
= 0;
1710 if (!proc_get_status (thread
))
1711 destroy_one_procinfo (&parent
->thread_list
, thread
);
1713 return 0; /* keep iterating */
1717 proc_update_threads (procinfo
*pi
)
1719 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
1720 struct dirent
*direntry
;
1725 /* We should never have to apply this operation to any procinfo
1726 except the one for the main process. If that ever changes for
1727 any reason, then take out the following clause and replace it
1728 with one that makes sure the ctl_fd is open. */
1731 pi
= find_procinfo_or_die (pi
->pid
, 0);
1733 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
1735 /* Note: this brute-force method was originally devised for Unixware
1736 (support removed since), and will also work on Solaris 2.6 and
1737 2.7. The original comment mentioned the existence of a much
1738 simpler and more elegant way to do this on Solaris, but didn't
1739 point out what that was. */
1741 strcpy (pathname
, pi
->pathname
);
1742 strcat (pathname
, "/lwp");
1743 dirp
.reset (opendir (pathname
));
1745 proc_error (pi
, "update_threads, opendir", __LINE__
);
1747 while ((direntry
= readdir (dirp
.get ())) != NULL
)
1748 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
1750 lwpid
= atoi (&direntry
->d_name
[0]);
1751 thread
= create_procinfo (pi
->pid
, lwpid
);
1753 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
1755 pi
->threads_valid
= 1;
1759 /* Given a pointer to a function, call that function once for each lwp
1760 in the procinfo list, until the function returns non-zero, in which
1761 event return the value returned by the function.
1763 Note: this function does NOT call update_threads. If you want to
1764 discover new threads first, you must call that function explicitly.
1765 This function just makes a quick pass over the currently-known
1768 PI is the parent process procinfo. FUNC is the per-thread
1769 function. PTR is an opaque parameter for function. Returns the
1770 first non-zero return value from the callee, or zero. */
1773 proc_iterate_over_threads (procinfo
*pi
,
1774 int (*func
) (procinfo
*, procinfo
*, void *),
1777 procinfo
*thread
, *next
;
1780 /* We should never have to apply this operation to any procinfo
1781 except the one for the main process. If that ever changes for
1782 any reason, then take out the following clause and replace it
1783 with one that makes sure the ctl_fd is open. */
1786 pi
= find_procinfo_or_die (pi
->pid
, 0);
1788 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
1790 next
= thread
->next
; /* In case thread is destroyed. */
1791 retval
= (*func
) (pi
, thread
, ptr
);
1799 /* =================== END, Thread "MODULE" =================== */
1801 /* =================== END, /proc "MODULE" =================== */
1803 /* =================== GDB "MODULE" =================== */
1805 /* Here are all of the gdb target vector functions and their
1808 static ptid_t
do_attach (ptid_t ptid
);
1809 static void do_detach ();
1810 static void proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
,
1811 int entry_or_exit
, int mode
, int from_tty
);
1813 /* Sets up the inferior to be debugged. Registers to trace signals,
1814 hardware faults, and syscalls. Note: does not set RLC flag: caller
1815 may want to customize that. Returns zero for success (note!
1816 unlike most functions in this module); on failure, returns the LINE
1817 NUMBER where it failed! */
1820 procfs_debug_inferior (procinfo
*pi
)
1822 fltset_t traced_faults
;
1823 sigset_t traced_signals
;
1824 sysset_t
*traced_syscall_entries
;
1825 sysset_t
*traced_syscall_exits
;
1828 /* Register to trace hardware faults in the child. */
1829 prfillset (&traced_faults
); /* trace all faults... */
1830 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
1831 if (!proc_set_traced_faults (pi
, &traced_faults
))
1834 /* Initially, register to trace all signals in the child. */
1835 prfillset (&traced_signals
);
1836 if (!proc_set_traced_signals (pi
, &traced_signals
))
1840 /* Register to trace the 'exit' system call (on entry). */
1841 traced_syscall_entries
= XNEW (sysset_t
);
1842 premptyset (traced_syscall_entries
);
1843 praddset (traced_syscall_entries
, SYS_exit
);
1844 praddset (traced_syscall_entries
, SYS_lwp_exit
);
1846 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
1847 xfree (traced_syscall_entries
);
1851 /* Method for tracing exec syscalls. */
1853 Not all systems with /proc have all the exec* syscalls with the same
1854 names. On the SGI, for example, there is no SYS_exec, but there
1855 *is* a SYS_execv. So, we try to account for that. */
1857 traced_syscall_exits
= XNEW (sysset_t
);
1858 premptyset (traced_syscall_exits
);
1860 praddset (traced_syscall_exits
, SYS_exec
);
1862 praddset (traced_syscall_exits
, SYS_execve
);
1863 praddset (traced_syscall_exits
, SYS_lwp_create
);
1864 praddset (traced_syscall_exits
, SYS_lwp_exit
);
1866 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
1867 xfree (traced_syscall_exits
);
1875 procfs_target::attach (const char *args
, int from_tty
)
1879 pid
= parse_pid_to_attach (args
);
1881 if (pid
== getpid ())
1882 error (_("Attaching GDB to itself is not a good idea..."));
1886 const char *exec_file
= get_exec_file (0);
1889 printf_filtered (_("Attaching to program `%s', %s\n"),
1890 exec_file
, target_pid_to_str (ptid_t (pid
)).c_str ());
1892 printf_filtered (_("Attaching to %s\n"),
1893 target_pid_to_str (ptid_t (pid
)).c_str ());
1897 inferior_ptid
= do_attach (ptid_t (pid
));
1898 if (!target_is_pushed (this))
1903 procfs_target::detach (inferior
*inf
, int from_tty
)
1905 int pid
= inferior_ptid
.pid ();
1909 const char *exec_file
;
1911 exec_file
= get_exec_file (0);
1912 if (exec_file
== NULL
)
1915 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file
,
1916 target_pid_to_str (ptid_t (pid
)).c_str ());
1921 inferior_ptid
= null_ptid
;
1922 detach_inferior (inf
);
1923 maybe_unpush_target ();
1927 do_attach (ptid_t ptid
)
1930 struct inferior
*inf
;
1934 pi
= create_procinfo (ptid
.pid (), 0);
1936 perror (_("procfs: out of memory in 'attach'"));
1938 if (!open_procinfo_files (pi
, FD_CTL
))
1940 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
1941 xsnprintf (errmsg
, sizeof (errmsg
),
1942 "do_attach: couldn't open /proc file for process %d",
1944 dead_procinfo (pi
, errmsg
, NOKILL
);
1947 /* Stop the process (if it isn't already stopped). */
1948 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
1950 pi
->was_stopped
= 1;
1951 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
1955 pi
->was_stopped
= 0;
1956 /* Set the process to run again when we close it. */
1957 if (!proc_set_run_on_last_close (pi
))
1958 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
1960 /* Now stop the process. */
1961 if (!proc_stop_process (pi
))
1962 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
1963 pi
->ignore_next_sigstop
= 1;
1965 /* Save some of the /proc state to be restored if we detach. */
1966 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
1967 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
1968 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
1969 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
1970 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
1971 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
1973 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
1974 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
1976 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
1977 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
1979 fail
= procfs_debug_inferior (pi
);
1981 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
1983 inf
= current_inferior ();
1984 inferior_appeared (inf
, pi
->pid
);
1985 /* Let GDB know that the inferior was attached. */
1986 inf
->attach_flag
= 1;
1988 /* Create a procinfo for the current lwp. */
1989 lwpid
= proc_get_current_thread (pi
);
1990 create_procinfo (pi
->pid
, lwpid
);
1992 /* Add it to gdb's thread list. */
1993 ptid
= ptid_t (pi
->pid
, lwpid
, 0);
1994 add_thread (&the_procfs_target
, ptid
);
2004 /* Find procinfo for the main process. */
2005 pi
= find_procinfo_or_die (inferior_ptid
.pid (),
2006 0); /* FIXME: threads */
2008 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
2009 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
2011 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
2012 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
2014 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
2015 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
2017 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
2018 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
2020 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
2021 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
2023 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
2024 if (!(pi
->was_stopped
)
2025 || query (_("Was stopped when attached, make it runnable again? ")))
2027 /* Clear any pending signal. */
2028 if (!proc_clear_current_fault (pi
))
2029 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
2031 if (!proc_clear_current_signal (pi
))
2032 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
2034 if (!proc_set_run_on_last_close (pi
))
2035 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
2038 destroy_procinfo (pi
);
2041 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
2044 ??? Is the following note still relevant? We can't get individual
2045 registers with the PT_GETREGS ptrace(2) request either, yet we
2046 don't bother with caching at all in that case.
2048 NOTE: Since the /proc interface cannot give us individual
2049 registers, we pay no attention to REGNUM, and just fetch them all.
2050 This results in the possibility that we will do unnecessarily many
2051 fetches, since we may be called repeatedly for individual
2052 registers. So we cache the results, and mark the cache invalid
2053 when the process is resumed. */
2056 procfs_target::fetch_registers (struct regcache
*regcache
, int regnum
)
2058 gdb_gregset_t
*gregs
;
2060 ptid_t ptid
= regcache
->ptid ();
2061 int pid
= ptid
.pid ();
2062 int tid
= ptid
.lwp ();
2063 struct gdbarch
*gdbarch
= regcache
->arch ();
2065 pi
= find_procinfo_or_die (pid
, tid
);
2068 error (_("procfs: fetch_registers failed to find procinfo for %s"),
2069 target_pid_to_str (ptid
).c_str ());
2071 gregs
= proc_get_gregs (pi
);
2073 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
2075 supply_gregset (regcache
, (const gdb_gregset_t
*) gregs
);
2077 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
2079 gdb_fpregset_t
*fpregs
;
2081 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
2082 || regnum
== gdbarch_pc_regnum (gdbarch
)
2083 || regnum
== gdbarch_sp_regnum (gdbarch
))
2084 return; /* Not a floating point register. */
2086 fpregs
= proc_get_fpregs (pi
);
2088 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
2090 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregs
);
2094 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
2095 this for all registers.
2097 NOTE: Since the /proc interface will not read individual registers,
2098 we will cache these requests until the process is resumed, and only
2099 then write them back to the inferior process.
2101 FIXME: is that a really bad idea? Have to think about cases where
2102 writing one register might affect the value of others, etc. */
2105 procfs_target::store_registers (struct regcache
*regcache
, int regnum
)
2107 gdb_gregset_t
*gregs
;
2109 ptid_t ptid
= regcache
->ptid ();
2110 int pid
= ptid
.pid ();
2111 int tid
= ptid
.lwp ();
2112 struct gdbarch
*gdbarch
= regcache
->arch ();
2114 pi
= find_procinfo_or_die (pid
, tid
);
2117 error (_("procfs: store_registers: failed to find procinfo for %s"),
2118 target_pid_to_str (ptid
).c_str ());
2120 gregs
= proc_get_gregs (pi
);
2122 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
2124 fill_gregset (regcache
, gregs
, regnum
);
2125 if (!proc_set_gregs (pi
))
2126 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
2128 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
2130 gdb_fpregset_t
*fpregs
;
2132 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
2133 || regnum
== gdbarch_pc_regnum (gdbarch
)
2134 || regnum
== gdbarch_sp_regnum (gdbarch
))
2135 return; /* Not a floating point register. */
2137 fpregs
= proc_get_fpregs (pi
);
2139 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
2141 fill_fpregset (regcache
, fpregs
, regnum
);
2142 if (!proc_set_fpregs (pi
))
2143 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
2148 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
2150 if (scall
== SYS_lwp_exit
)
2156 syscall_is_exit (procinfo
*pi
, int scall
)
2158 if (scall
== SYS_exit
)
2164 syscall_is_exec (procinfo
*pi
, int scall
)
2167 if (scall
== SYS_exec
)
2170 if (scall
== SYS_execve
)
2176 syscall_is_lwp_create (procinfo
*pi
, int scall
)
2178 if (scall
== SYS_lwp_create
)
2183 /* Retrieve the next stop event from the child process. If child has
2184 not stopped yet, wait for it to stop. Translate /proc eventcodes
2185 (or possibly wait eventcodes) into gdb internal event codes.
2186 Returns the id of process (and possibly thread) that incurred the
2187 event. Event codes are returned through a pointer parameter. */
2190 procfs_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
2193 /* First cut: loosely based on original version 2.1. */
2197 ptid_t retval
, temp_ptid
;
2198 int why
, what
, flags
;
2205 retval
= ptid_t (-1);
2207 /* Find procinfo for main process. */
2208 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2211 /* We must assume that the status is stale now... */
2212 pi
->status_valid
= 0;
2213 pi
->gregs_valid
= 0;
2214 pi
->fpregs_valid
= 0;
2216 #if 0 /* just try this out... */
2217 flags
= proc_flags (pi
);
2218 why
= proc_why (pi
);
2219 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
2220 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
2222 /* If child is not stopped, wait for it to stop. */
2223 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
2224 && !proc_wait_for_stop (pi
))
2226 /* wait_for_stop failed: has the child terminated? */
2227 if (errno
== ENOENT
)
2231 /* /proc file not found; presumably child has terminated. */
2232 wait_retval
= ::wait (&wstat
); /* "wait" for the child's exit. */
2235 if (wait_retval
!= inferior_ptid
.pid ())
2236 error (_("procfs: couldn't stop "
2237 "process %d: wait returned %d."),
2238 inferior_ptid
.pid (), wait_retval
);
2239 /* FIXME: might I not just use waitpid?
2240 Or try find_procinfo to see if I know about this child? */
2241 retval
= ptid_t (wait_retval
);
2243 else if (errno
== EINTR
)
2247 /* Unknown error from wait_for_stop. */
2248 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
2253 /* This long block is reached if either:
2254 a) the child was already stopped, or
2255 b) we successfully waited for the child with wait_for_stop.
2256 This block will analyze the /proc status, and translate it
2257 into a waitstatus for GDB.
2259 If we actually had to call wait because the /proc file
2260 is gone (child terminated), then we skip this block,
2261 because we already have a waitstatus. */
2263 flags
= proc_flags (pi
);
2264 why
= proc_why (pi
);
2265 what
= proc_what (pi
);
2267 if (flags
& (PR_STOPPED
| PR_ISTOP
))
2269 /* If it's running async (for single_thread control),
2270 set it back to normal again. */
2271 if (flags
& PR_ASYNC
)
2272 if (!proc_unset_async (pi
))
2273 proc_error (pi
, "target_wait, unset_async", __LINE__
);
2276 proc_prettyprint_why (why
, what
, 1);
2278 /* The 'pid' we will return to GDB is composed of
2279 the process ID plus the lwp ID. */
2280 retval
= ptid_t (pi
->pid
, proc_get_current_thread (pi
), 0);
2284 wstat
= (what
<< 8) | 0177;
2287 if (syscall_is_lwp_exit (pi
, what
))
2289 if (print_thread_events
)
2290 printf_unfiltered (_("[%s exited]\n"),
2291 target_pid_to_str (retval
).c_str ());
2292 delete_thread (find_thread_ptid (this, retval
));
2293 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2296 else if (syscall_is_exit (pi
, what
))
2298 struct inferior
*inf
;
2300 /* Handle SYS_exit call only. */
2301 /* Stopped at entry to SYS_exit.
2302 Make it runnable, resume it, then use
2303 the wait system call to get its exit code.
2304 Proc_run_process always clears the current
2306 Then return its exit status. */
2307 pi
->status_valid
= 0;
2309 /* FIXME: what we should do is return
2310 TARGET_WAITKIND_SPURIOUS. */
2311 if (!proc_run_process (pi
, 0, 0))
2312 proc_error (pi
, "target_wait, run_process", __LINE__
);
2314 inf
= find_inferior_pid (this, pi
->pid
);
2315 if (inf
->attach_flag
)
2317 /* Don't call wait: simulate waiting for exit,
2318 return a "success" exit code. Bogus: what if
2319 it returns something else? */
2321 retval
= inferior_ptid
; /* ? ? ? */
2325 int temp
= ::wait (&wstat
);
2327 /* FIXME: shouldn't I make sure I get the right
2328 event from the right process? If (for
2329 instance) I have killed an earlier inferior
2330 process but failed to clean up after it
2331 somehow, I could get its termination event
2334 /* If wait returns -1, that's what we return
2337 retval
= ptid_t (temp
);
2342 printf_filtered (_("procfs: trapped on entry to "));
2343 proc_prettyprint_syscall (proc_what (pi
), 0);
2344 printf_filtered ("\n");
2346 long i
, nsysargs
, *sysargs
;
2348 nsysargs
= proc_nsysarg (pi
);
2349 sysargs
= proc_sysargs (pi
);
2351 if (nsysargs
> 0 && sysargs
!= NULL
)
2353 printf_filtered (_("%ld syscall arguments:\n"),
2355 for (i
= 0; i
< nsysargs
; i
++)
2356 printf_filtered ("#%ld: 0x%08lx\n",
2362 /* How to exit gracefully, returning "unknown
2364 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2365 return inferior_ptid
;
2369 /* How to keep going without returning to wfi: */
2370 target_continue_no_signal (ptid
);
2376 if (syscall_is_exec (pi
, what
))
2378 /* Hopefully this is our own "fork-child" execing
2379 the real child. Hoax this event into a trap, and
2380 GDB will see the child about to execute its start
2382 wstat
= (SIGTRAP
<< 8) | 0177;
2384 else if (syscall_is_lwp_create (pi
, what
))
2386 /* This syscall is somewhat like fork/exec. We
2387 will get the event twice: once for the parent
2388 LWP, and once for the child. We should already
2389 know about the parent LWP, but the child will
2390 be new to us. So, whenever we get this event,
2391 if it represents a new thread, simply add the
2392 thread to the list. */
2394 /* If not in procinfo list, add it. */
2395 temp_tid
= proc_get_current_thread (pi
);
2396 if (!find_procinfo (pi
->pid
, temp_tid
))
2397 create_procinfo (pi
->pid
, temp_tid
);
2399 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2400 /* If not in GDB's thread list, add it. */
2401 if (!in_thread_list (this, temp_ptid
))
2402 add_thread (this, temp_ptid
);
2404 /* Return to WFI, but tell it to immediately resume. */
2405 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2406 return inferior_ptid
;
2408 else if (syscall_is_lwp_exit (pi
, what
))
2410 if (print_thread_events
)
2411 printf_unfiltered (_("[%s exited]\n"),
2412 target_pid_to_str (retval
).c_str ());
2413 delete_thread (find_thread_ptid (this, retval
));
2414 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2419 /* FIXME: Do we need to handle SYS_sproc,
2420 SYS_fork, or SYS_vfork here? The old procfs
2421 seemed to use this event to handle threads on
2422 older (non-LWP) systems, where I'm assuming
2423 that threads were actually separate processes.
2424 Irix, maybe? Anyway, low priority for now. */
2428 printf_filtered (_("procfs: trapped on exit from "));
2429 proc_prettyprint_syscall (proc_what (pi
), 0);
2430 printf_filtered ("\n");
2432 long i
, nsysargs
, *sysargs
;
2434 nsysargs
= proc_nsysarg (pi
);
2435 sysargs
= proc_sysargs (pi
);
2437 if (nsysargs
> 0 && sysargs
!= NULL
)
2439 printf_filtered (_("%ld syscall arguments:\n"),
2441 for (i
= 0; i
< nsysargs
; i
++)
2442 printf_filtered ("#%ld: 0x%08lx\n",
2446 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2447 return inferior_ptid
;
2452 wstat
= (SIGSTOP
<< 8) | 0177;
2457 printf_filtered (_("Retry #%d:\n"), retry
);
2458 pi
->status_valid
= 0;
2463 /* If not in procinfo list, add it. */
2464 temp_tid
= proc_get_current_thread (pi
);
2465 if (!find_procinfo (pi
->pid
, temp_tid
))
2466 create_procinfo (pi
->pid
, temp_tid
);
2468 /* If not in GDB's thread list, add it. */
2469 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2470 if (!in_thread_list (this, temp_ptid
))
2471 add_thread (this, temp_ptid
);
2473 status
->kind
= TARGET_WAITKIND_STOPPED
;
2474 status
->value
.sig
= GDB_SIGNAL_0
;
2479 wstat
= (what
<< 8) | 0177;
2483 int signo
= pi
->prstatus
.pr_lwp
.pr_info
.si_signo
;
2485 wstat
= (signo
<< 8) | 0177;
2488 default: /* switch (why) unmatched */
2489 printf_filtered ("procfs:%d -- ", __LINE__
);
2490 printf_filtered (_("child stopped for unknown reason:\n"));
2491 proc_prettyprint_why (why
, what
, 1);
2492 error (_("... giving up..."));
2495 /* Got this far without error: If retval isn't in the
2496 threads database, add it. */
2497 if (retval
.pid () > 0
2498 && retval
!= inferior_ptid
2499 && !in_thread_list (this, retval
))
2501 /* We have a new thread. We need to add it both to
2502 GDB's list and to our own. If we don't create a
2503 procinfo, resume may be unhappy later. */
2504 add_thread (this, retval
);
2505 if (find_procinfo (retval
.pid (),
2506 retval
.lwp ()) == NULL
)
2507 create_procinfo (retval
.pid (),
2511 else /* Flags do not indicate STOPPED. */
2513 /* surely this can't happen... */
2514 printf_filtered ("procfs:%d -- process not stopped.\n",
2516 proc_prettyprint_flags (flags
, 1);
2517 error (_("procfs: ...giving up..."));
2522 store_waitstatus (status
, wstat
);
2528 /* Perform a partial transfer to/from the specified object. For
2529 memory transfers, fall back to the old memory xfer functions. */
2531 enum target_xfer_status
2532 procfs_target::xfer_partial (enum target_object object
,
2533 const char *annex
, gdb_byte
*readbuf
,
2534 const gdb_byte
*writebuf
, ULONGEST offset
,
2535 ULONGEST len
, ULONGEST
*xfered_len
)
2539 case TARGET_OBJECT_MEMORY
:
2540 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
2542 case TARGET_OBJECT_AUXV
:
2543 return memory_xfer_auxv (this, object
, annex
, readbuf
, writebuf
,
2544 offset
, len
, xfered_len
);
2547 return this->beneath ()->xfer_partial (object
, annex
,
2548 readbuf
, writebuf
, offset
, len
,
2553 /* Helper for procfs_xfer_partial that handles memory transfers.
2554 Arguments are like target_xfer_partial. */
2556 static enum target_xfer_status
2557 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
2558 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
2563 /* Find procinfo for main process. */
2564 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2565 if (pi
->as_fd
== 0 && open_procinfo_files (pi
, FD_AS
) == 0)
2567 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
2568 return TARGET_XFER_E_IO
;
2571 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
2572 return TARGET_XFER_E_IO
;
2574 if (writebuf
!= NULL
)
2576 PROCFS_NOTE ("write memory:\n");
2577 nbytes
= write (pi
->as_fd
, writebuf
, len
);
2581 PROCFS_NOTE ("read memory:\n");
2582 nbytes
= read (pi
->as_fd
, readbuf
, len
);
2585 return TARGET_XFER_E_IO
;
2586 *xfered_len
= nbytes
;
2587 return TARGET_XFER_OK
;
2590 /* Called by target_resume before making child runnable. Mark cached
2591 registers and status's invalid. If there are "dirty" caches that
2592 need to be written back to the child process, do that.
2594 File descriptors are also cached. As they are a limited resource,
2595 we cannot hold onto them indefinitely. However, as they are
2596 expensive to open, we don't want to throw them away
2597 indiscriminately either. As a compromise, we will keep the file
2598 descriptors for the parent process, but discard any file
2599 descriptors we may have accumulated for the threads.
2601 As this function is called by iterate_over_threads, it always
2602 returns zero (so that iterate_over_threads will keep
2606 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
2608 /* About to run the child; invalidate caches and do any other
2612 if (pi
->gregs_dirty
)
2613 if (parent
== NULL
|| proc_get_current_thread (parent
) != pi
->tid
)
2614 if (!proc_set_gregs (pi
)) /* flush gregs cache */
2615 proc_warn (pi
, "target_resume, set_gregs",
2617 if (gdbarch_fp0_regnum (target_gdbarch ()) >= 0)
2618 if (pi
->fpregs_dirty
)
2619 if (parent
== NULL
|| proc_get_current_thread (parent
) != pi
->tid
)
2620 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
2621 proc_warn (pi
, "target_resume, set_fpregs",
2627 /* The presence of a parent indicates that this is an LWP.
2628 Close any file descriptors that it might have open.
2629 We don't do this to the master (parent) procinfo. */
2631 close_procinfo_files (pi
);
2633 pi
->gregs_valid
= 0;
2634 pi
->fpregs_valid
= 0;
2636 pi
->gregs_dirty
= 0;
2637 pi
->fpregs_dirty
= 0;
2639 pi
->status_valid
= 0;
2640 pi
->threads_valid
= 0;
2646 /* A callback function for iterate_over_threads. Find the
2647 asynchronous signal thread, and make it runnable. See if that
2648 helps matters any. */
2651 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
2654 if (proc_flags (pi
) & PR_ASLWP
)
2656 if (!proc_run_process (pi
, 0, -1))
2657 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
2665 /* Make the child process runnable. Normally we will then call
2666 procfs_wait and wait for it to stop again (unless gdb is async).
2668 If STEP is true, then arrange for the child to stop again after
2669 executing a single instruction. If SIGNO is zero, then cancel any
2670 pending signal; if non-zero, then arrange for the indicated signal
2671 to be delivered to the child when it runs. If PID is -1, then
2672 allow any child thread to run; if non-zero, then allow only the
2673 indicated thread to run. (not implemented yet). */
2676 procfs_target::resume (ptid_t ptid
, int step
, enum gdb_signal signo
)
2678 procinfo
*pi
, *thread
;
2682 prrun.prflags |= PRSVADDR;
2683 prrun.pr_vaddr = $PC; set resume address
2684 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
2685 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
2686 prrun.prflags |= PRCFAULT; clear current fault.
2688 PRSTRACE and PRSFAULT can be done by other means
2689 (proc_trace_signals, proc_trace_faults)
2690 PRSVADDR is unnecessary.
2691 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
2692 This basically leaves PRSTEP and PRCSIG.
2693 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
2694 So basically PR_STEP is the sole argument that must be passed
2695 to proc_run_process (for use in the prrun struct by ioctl). */
2697 /* Find procinfo for main process. */
2698 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2700 /* First cut: ignore pid argument. */
2703 /* Convert signal to host numbering. */
2704 if (signo
== 0 || (signo
== GDB_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
2707 native_signo
= gdb_signal_to_host (signo
);
2709 pi
->ignore_next_sigstop
= 0;
2711 /* Running the process voids all cached registers and status. */
2712 /* Void the threads' caches first. */
2713 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
2714 /* Void the process procinfo's caches. */
2715 invalidate_cache (NULL
, pi
, NULL
);
2717 if (ptid
.pid () != -1)
2719 /* Resume a specific thread, presumably suppressing the
2721 thread
= find_procinfo (ptid
.pid (), ptid
.lwp ());
2724 if (thread
->tid
!= 0)
2726 /* We're to resume a specific thread, and not the
2727 others. Set the child process's PR_ASYNC flag. */
2728 if (!proc_set_async (pi
))
2729 proc_error (pi
, "target_resume, set_async", __LINE__
);
2731 proc_iterate_over_threads (pi
,
2732 make_signal_thread_runnable
,
2735 pi
= thread
; /* Substitute the thread's procinfo
2741 if (!proc_run_process (pi
, step
, native_signo
))
2744 warning (_("resume: target already running. "
2745 "Pretend to resume, and hope for the best!"));
2747 proc_error (pi
, "target_resume", __LINE__
);
2751 /* Set up to trace signals in the child process. */
2754 procfs_target::pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2757 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2760 prfillset (&signals
);
2762 for (signo
= 0; signo
< NSIG
; signo
++)
2764 int target_signo
= gdb_signal_from_host (signo
);
2765 if (target_signo
< pass_signals
.size () && pass_signals
[target_signo
])
2766 prdelset (&signals
, signo
);
2769 if (!proc_set_traced_signals (pi
, &signals
))
2770 proc_error (pi
, "pass_signals", __LINE__
);
2773 /* Print status information about the child process. */
2776 procfs_target::files_info ()
2778 struct inferior
*inf
= current_inferior ();
2780 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
2781 inf
->attach_flag
? "attached": "child",
2782 target_pid_to_str (inferior_ptid
).c_str ());
2785 /* Make it die. Wait for it to die. Clean up after it. Note: this
2786 should only be applied to the real process, not to an LWP, because
2787 of the check for parent-process. If we need this to work for an
2788 LWP, it needs some more logic. */
2791 unconditionally_kill_inferior (procinfo
*pi
)
2795 parent_pid
= proc_parent_pid (pi
);
2796 if (!proc_kill (pi
, SIGKILL
))
2797 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
2798 destroy_procinfo (pi
);
2800 /* If pi is GDB's child, wait for it to die. */
2801 if (parent_pid
== getpid ())
2802 /* FIXME: should we use waitpid to make sure we get the right event?
2803 Should we check the returned event? */
2808 ret
= waitpid (pi
->pid
, &status
, 0);
2815 /* We're done debugging it, and we want it to go away. Then we want
2816 GDB to forget all about it. */
2819 procfs_target::kill ()
2821 if (inferior_ptid
!= null_ptid
) /* ? */
2823 /* Find procinfo for main process. */
2824 procinfo
*pi
= find_procinfo (inferior_ptid
.pid (), 0);
2827 unconditionally_kill_inferior (pi
);
2828 target_mourn_inferior (inferior_ptid
);
2832 /* Forget we ever debugged this thing! */
2835 procfs_target::mourn_inferior ()
2839 if (inferior_ptid
!= null_ptid
)
2841 /* Find procinfo for main process. */
2842 pi
= find_procinfo (inferior_ptid
.pid (), 0);
2844 destroy_procinfo (pi
);
2847 generic_mourn_inferior ();
2849 maybe_unpush_target ();
2852 /* When GDB forks to create a runnable inferior process, this function
2853 is called on the parent side of the fork. It's job is to do
2854 whatever is necessary to make the child ready to be debugged, and
2855 then wait for the child to synchronize. */
2858 procfs_target::procfs_init_inferior (int pid
)
2864 /* This routine called on the parent side (GDB side)
2865 after GDB forks the inferior. */
2866 if (!target_is_pushed (this))
2869 pi
= create_procinfo (pid
, 0);
2871 perror (_("procfs: out of memory in 'init_inferior'"));
2873 if (!open_procinfo_files (pi
, FD_CTL
))
2874 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
2878 open_procinfo_files // done
2881 procfs_notice_signals
2888 /* If not stopped yet, wait for it to stop. */
2889 if (!(proc_flags (pi
) & PR_STOPPED
) && !(proc_wait_for_stop (pi
)))
2890 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
2892 /* Save some of the /proc state to be restored if we detach. */
2893 /* FIXME: Why? In case another debugger was debugging it?
2894 We're it's parent, for Ghu's sake! */
2895 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
2896 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
2897 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
2898 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
2899 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
2900 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
2901 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
2902 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
2903 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
2904 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
2906 fail
= procfs_debug_inferior (pi
);
2908 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
2910 /* FIXME: logically, we should really be turning OFF run-on-last-close,
2911 and possibly even turning ON kill-on-last-close at this point. But
2912 I can't make that change without careful testing which I don't have
2913 time to do right now... */
2914 /* Turn on run-on-last-close flag so that the child
2915 will die if GDB goes away for some reason. */
2916 if (!proc_set_run_on_last_close (pi
))
2917 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
2919 /* We now have have access to the lwpid of the main thread/lwp. */
2920 lwpid
= proc_get_current_thread (pi
);
2922 /* Create a procinfo for the main lwp. */
2923 create_procinfo (pid
, lwpid
);
2925 /* We already have a main thread registered in the thread table at
2926 this point, but it didn't have any lwp info yet. Notify the core
2927 about it. This changes inferior_ptid as well. */
2928 thread_change_ptid (this, ptid_t (pid
), ptid_t (pid
, lwpid
, 0));
2930 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
2933 /* When GDB forks to create a new process, this function is called on
2934 the child side of the fork before GDB exec's the user program. Its
2935 job is to make the child minimally debuggable, so that the parent
2936 GDB process can connect to the child and take over. This function
2937 should do only the minimum to make that possible, and to
2938 synchronize with the parent process. The parent process should
2939 take care of the details. */
2942 procfs_set_exec_trap (void)
2944 /* This routine called on the child side (inferior side)
2945 after GDB forks the inferior. It must use only local variables,
2946 because it may be sharing data space with its parent. */
2951 pi
= create_procinfo (getpid (), 0);
2953 perror_with_name (_("procfs: create_procinfo failed in child."));
2955 if (open_procinfo_files (pi
, FD_CTL
) == 0)
2957 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
2958 gdb_flush (gdb_stderr
);
2959 /* No need to call "dead_procinfo", because we're going to
2964 /* Method for tracing exec syscalls. */
2966 Not all systems with /proc have all the exec* syscalls with the same
2967 names. On the SGI, for example, there is no SYS_exec, but there
2968 *is* a SYS_execv. So, we try to account for that. */
2970 exitset
= XNEW (sysset_t
);
2971 premptyset (exitset
);
2973 praddset (exitset
, SYS_exec
);
2975 praddset (exitset
, SYS_execve
);
2977 if (!proc_set_traced_sysexit (pi
, exitset
))
2979 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
2980 gdb_flush (gdb_stderr
);
2984 /* FIXME: should this be done in the parent instead? */
2985 /* Turn off inherit on fork flag so that all grand-children
2986 of gdb start with tracing flags cleared. */
2987 if (!proc_unset_inherit_on_fork (pi
))
2988 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
2990 /* Turn off run on last close flag, so that the child process
2991 cannot run away just because we close our handle on it.
2992 We want it to wait for the parent to attach. */
2993 if (!proc_unset_run_on_last_close (pi
))
2994 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
2996 /* FIXME: No need to destroy the procinfo --
2997 we have our own address space, and we're about to do an exec! */
2998 /*destroy_procinfo (pi);*/
3001 /* This function is called BEFORE gdb forks the inferior process. Its
3002 only real responsibility is to set things up for the fork, and tell
3003 GDB which two functions to call after the fork (one for the parent,
3004 and one for the child).
3006 This function does a complicated search for a unix shell program,
3007 which it then uses to parse arguments and environment variables to
3008 be sent to the child. I wonder whether this code could not be
3009 abstracted out and shared with other unix targets such as
3013 procfs_target::create_inferior (const char *exec_file
,
3014 const std::string
&allargs
,
3015 char **env
, int from_tty
)
3017 const char *shell_file
= get_shell ();
3021 if (strchr (shell_file
, '/') == NULL
)
3024 /* We will be looking down the PATH to find shell_file. If we
3025 just do this the normal way (via execlp, which operates by
3026 attempting an exec for each element of the PATH until it
3027 finds one which succeeds), then there will be an exec for
3028 each failed attempt, each of which will cause a PR_SYSEXIT
3029 stop, and we won't know how to distinguish the PR_SYSEXIT's
3030 for these failed execs with the ones for successful execs
3031 (whether the exec has succeeded is stored at that time in the
3032 carry bit or some such architecture-specific and
3033 non-ABI-specified place).
3035 So I can't think of anything better than to search the PATH
3036 now. This has several disadvantages: (1) There is a race
3037 condition; if we find a file now and it is deleted before we
3038 exec it, we lose, even if the deletion leaves a valid file
3039 further down in the PATH, (2) there is no way to know exactly
3040 what an executable (in the sense of "capable of being
3041 exec'd") file is. Using access() loses because it may lose
3042 if the caller is the superuser; failing to use it loses if
3043 there are ACLs or some such. */
3047 /* FIXME-maybe: might want "set path" command so user can change what
3048 path is used from within GDB. */
3049 const char *path
= getenv ("PATH");
3051 struct stat statbuf
;
3054 path
= "/bin:/usr/bin";
3056 tryname
= (char *) alloca (strlen (path
) + strlen (shell_file
) + 2);
3057 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
3059 p1
= strchr (p
, ':');
3064 strncpy (tryname
, p
, len
);
3065 tryname
[len
] = '\0';
3066 strcat (tryname
, "/");
3067 strcat (tryname
, shell_file
);
3068 if (access (tryname
, X_OK
) < 0)
3070 if (stat (tryname
, &statbuf
) < 0)
3072 if (!S_ISREG (statbuf
.st_mode
))
3073 /* We certainly need to reject directories. I'm not quite
3074 as sure about FIFOs, sockets, etc., but I kind of doubt
3075 that people want to exec() these things. */
3080 /* Not found. This must be an error rather than merely passing
3081 the file to execlp(), because execlp() would try all the
3082 exec()s, causing GDB to get confused. */
3083 error (_("procfs:%d -- Can't find shell %s in PATH"),
3084 __LINE__
, shell_file
);
3086 shell_file
= tryname
;
3089 pid
= fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
3090 NULL
, NULL
, shell_file
, NULL
);
3092 /* We have something that executes now. We'll be running through
3093 the shell at this point (if startup-with-shell is true), but the
3094 pid shouldn't change. */
3095 add_thread_silent (this, ptid_t (pid
));
3097 procfs_init_inferior (pid
);
3100 /* An observer for the "inferior_created" event. */
3103 procfs_inferior_created (struct target_ops
*ops
, int from_tty
)
3107 /* Callback for update_thread_list. Calls "add_thread". */
3110 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
3112 ptid_t gdb_threadid
= ptid_t (pi
->pid
, thread
->tid
, 0);
3114 thread_info
*thr
= find_thread_ptid (&the_procfs_target
, gdb_threadid
);
3115 if (thr
== NULL
|| thr
->state
== THREAD_EXITED
)
3116 add_thread (&the_procfs_target
, gdb_threadid
);
3121 /* Query all the threads that the target knows about, and give them
3122 back to GDB to add to its list. */
3125 procfs_target::update_thread_list ()
3131 /* Find procinfo for main process. */
3132 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3133 proc_update_threads (pi
);
3134 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
3137 /* Return true if the thread is still 'alive'. This guy doesn't
3138 really seem to be doing his job. Got to investigate how to tell
3139 when a thread is really gone. */
3142 procfs_target::thread_alive (ptid_t ptid
)
3148 thread
= ptid
.lwp ();
3149 /* If I don't know it, it ain't alive! */
3150 pi
= find_procinfo (proc
, thread
);
3154 /* If I can't get its status, it ain't alive!
3155 What's more, I need to forget about it! */
3156 if (!proc_get_status (pi
))
3158 destroy_procinfo (pi
);
3161 /* I couldn't have got its status if it weren't alive, so it's
3166 /* Convert PTID to a string. */
3169 procfs_target::pid_to_str (ptid_t ptid
)
3171 if (ptid
.lwp () == 0)
3172 return string_printf ("process %d", ptid
.pid ());
3174 return string_printf ("LWP %ld", ptid
.lwp ());
3177 /* Accepts an integer PID; Returns a string representing a file that
3178 can be opened to get the symbols for the child process. */
3181 procfs_target::pid_to_exec_file (int pid
)
3183 static char buf
[PATH_MAX
];
3184 char name
[PATH_MAX
];
3186 /* Solaris 11 introduced /proc/<proc-id>/execname. */
3187 xsnprintf (name
, sizeof (name
), "/proc/%d/execname", pid
);
3188 scoped_fd
fd (gdb_open_cloexec (name
, O_RDONLY
, 0));
3189 if (fd
.get () < 0 || read (fd
.get (), buf
, PATH_MAX
- 1) < 0)
3191 /* If that fails, fall back to /proc/<proc-id>/path/a.out introduced in
3195 xsnprintf (name
, sizeof (name
), "/proc/%d/path/a.out", pid
);
3196 len
= readlink (name
, buf
, PATH_MAX
- 1);
3206 /* Insert a watchpoint. */
3209 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
3215 pi
= find_procinfo_or_die (ptid
.pid () == -1 ?
3216 inferior_ptid
.pid () : ptid
.pid (),
3219 /* Translate from GDB's flags to /proc's. */
3220 if (len
> 0) /* len == 0 means delete watchpoint. */
3222 switch (rwflag
) { /* FIXME: need an enum! */
3223 case hw_write
: /* default watchpoint (write) */
3224 pflags
= WRITE_WATCHFLAG
;
3226 case hw_read
: /* read watchpoint */
3227 pflags
= READ_WATCHFLAG
;
3229 case hw_access
: /* access watchpoint */
3230 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
3232 case hw_execute
: /* execution HW breakpoint */
3233 pflags
= EXEC_WATCHFLAG
;
3235 default: /* Something weird. Return error. */
3238 if (after
) /* Stop after r/w access is completed. */
3239 pflags
|= AFTER_WATCHFLAG
;
3242 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
3244 if (errno
== E2BIG
) /* Typical error for no resources. */
3245 return -1; /* fail */
3246 /* GDB may try to remove the same watchpoint twice.
3247 If a remove request returns no match, don't error. */
3248 if (errno
== ESRCH
&& len
== 0)
3249 return 0; /* ignore */
3250 proc_error (pi
, "set_watchpoint", __LINE__
);
3255 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
3256 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
3257 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
3260 Note: procfs_can_use_hw_breakpoint() is not yet used by all
3261 procfs.c targets due to the fact that some of them still define
3262 target_can_use_hardware_watchpoint. */
3265 procfs_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int othertype
)
3267 /* Due to the way that proc_set_watchpoint() is implemented, host
3268 and target pointers must be of the same size. If they are not,
3269 we can't use hardware watchpoints. This limitation is due to the
3270 fact that proc_set_watchpoint() calls
3271 procfs_address_to_host_pointer(); a close inspection of
3272 procfs_address_to_host_pointer will reveal that an internal error
3273 will be generated when the host and target pointer sizes are
3275 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
3277 if (sizeof (void *) != TYPE_LENGTH (ptr_type
))
3280 /* Other tests here??? */
3285 /* Returns non-zero if process is stopped on a hardware watchpoint
3286 fault, else returns zero. */
3289 procfs_target::stopped_by_watchpoint ()
3293 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3295 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3296 if (proc_why (pi
) == PR_FAULTED
)
3297 if (proc_what (pi
) == FLTWATCH
)
3302 /* Returns 1 if the OS knows the position of the triggered watchpoint,
3303 and sets *ADDR to that address. Returns 0 if OS cannot report that
3304 address. This function is only called if
3305 procfs_stopped_by_watchpoint returned 1, thus no further checks are
3306 done. The function also assumes that ADDR is not NULL. */
3309 procfs_target::stopped_data_address (CORE_ADDR
*addr
)
3313 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3314 return proc_watchpoint_address (pi
, addr
);
3318 procfs_target::insert_watchpoint (CORE_ADDR addr
, int len
,
3319 enum target_hw_bp_type type
,
3320 struct expression
*cond
)
3322 if (!target_have_steppable_watchpoint
3323 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
3324 /* When a hardware watchpoint fires off the PC will be left at
3325 the instruction following the one which caused the
3326 watchpoint. It will *NOT* be necessary for GDB to step over
3328 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 1);
3330 /* When a hardware watchpoint fires off the PC will be left at
3331 the instruction which caused the watchpoint. It will be
3332 necessary for GDB to step over the watchpoint. */
3333 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 0);
3337 procfs_target::remove_watchpoint (CORE_ADDR addr
, int len
,
3338 enum target_hw_bp_type type
,
3339 struct expression
*cond
)
3341 return procfs_set_watchpoint (inferior_ptid
, addr
, 0, 0, 0);
3345 procfs_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
3347 /* The man page for proc(4) on Solaris 2.6 and up says that the
3348 system can support "thousands" of hardware watchpoints, but gives
3349 no method for finding out how many; It doesn't say anything about
3350 the allowed size for the watched area either. So we just tell
3355 /* Memory Mappings Functions: */
3357 /* Call a callback function once for each mapping, passing it the
3358 mapping, an optional secondary callback function, and some optional
3359 opaque data. Quit and return the first non-zero value returned
3362 PI is the procinfo struct for the process to be mapped. FUNC is
3363 the callback function to be called by this iterator. DATA is the
3364 optional opaque data to be passed to the callback function.
3365 CHILD_FUNC is the optional secondary function pointer to be passed
3366 to the child function. Returns the first non-zero return value
3367 from the callback function, or zero. */
3370 iterate_over_mappings (procinfo
*pi
, find_memory_region_ftype child_func
,
3372 int (*func
) (struct prmap
*map
,
3373 find_memory_region_ftype child_func
,
3376 char pathname
[MAX_PROC_NAME_SIZE
];
3377 struct prmap
*prmaps
;
3378 struct prmap
*prmap
;
3383 /* Get the number of mappings, allocate space,
3384 and read the mappings into prmaps. */
3386 xsnprintf (pathname
, sizeof (pathname
), "/proc/%d/map", pi
->pid
);
3388 scoped_fd
map_fd (open (pathname
, O_RDONLY
));
3389 if (map_fd
.get () < 0)
3390 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
3392 /* Use stat to determine the file size, and compute
3393 the number of prmap_t objects it contains. */
3394 if (fstat (map_fd
.get (), &sbuf
) != 0)
3395 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
3397 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
3398 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
3399 if (read (map_fd
.get (), (char *) prmaps
, nmap
* sizeof (*prmaps
))
3400 != (nmap
* sizeof (*prmaps
)))
3401 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
3403 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
3405 funcstat
= (*func
) (prmap
, child_func
, data
);
3413 /* Implements the to_find_memory_regions method. Calls an external
3414 function for each memory region.
3415 Returns the integer value returned by the callback. */
3418 find_memory_regions_callback (struct prmap
*map
,
3419 find_memory_region_ftype func
, void *data
)
3421 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
3423 (map
->pr_mflags
& MA_READ
) != 0,
3424 (map
->pr_mflags
& MA_WRITE
) != 0,
3425 (map
->pr_mflags
& MA_EXEC
) != 0,
3426 1, /* MODIFIED is unknown, pass it as true. */
3430 /* External interface. Calls a callback function once for each
3431 mapped memory region in the child process, passing as arguments:
3433 CORE_ADDR virtual_address,
3435 int read, TRUE if region is readable by the child
3436 int write, TRUE if region is writable by the child
3437 int execute TRUE if region is executable by the child.
3439 Stops iterating and returns the first non-zero value returned by
3443 procfs_target::find_memory_regions (find_memory_region_ftype func
, void *data
)
3445 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3447 return iterate_over_mappings (pi
, func
, data
,
3448 find_memory_regions_callback
);
3451 /* Returns an ascii representation of a memory mapping's flags. */
3454 mappingflags (long flags
)
3456 static char asciiflags
[8];
3458 strcpy (asciiflags
, "-------");
3459 if (flags
& MA_STACK
)
3460 asciiflags
[1] = 's';
3461 if (flags
& MA_BREAK
)
3462 asciiflags
[2] = 'b';
3463 if (flags
& MA_SHARED
)
3464 asciiflags
[3] = 's';
3465 if (flags
& MA_READ
)
3466 asciiflags
[4] = 'r';
3467 if (flags
& MA_WRITE
)
3468 asciiflags
[5] = 'w';
3469 if (flags
& MA_EXEC
)
3470 asciiflags
[6] = 'x';
3471 return (asciiflags
);
3474 /* Callback function, does the actual work for 'info proc
3478 info_mappings_callback (struct prmap
*map
, find_memory_region_ftype ignore
,
3481 unsigned int pr_off
;
3483 pr_off
= (unsigned int) map
->pr_offset
;
3485 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
3486 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
3487 (unsigned long) map
->pr_vaddr
,
3488 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3489 (unsigned long) map
->pr_size
,
3491 mappingflags (map
->pr_mflags
));
3493 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
3494 (unsigned long) map
->pr_vaddr
,
3495 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3496 (unsigned long) map
->pr_size
,
3498 mappingflags (map
->pr_mflags
));
3503 /* Implement the "info proc mappings" subcommand. */
3506 info_proc_mappings (procinfo
*pi
, int summary
)
3509 return; /* No output for summary mode. */
3511 printf_filtered (_("Mapped address spaces:\n\n"));
3512 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
3513 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3520 printf_filtered (" %18s %18s %10s %10s %7s\n",
3527 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
3528 printf_filtered ("\n");
3531 /* Implement the "info proc" command. */
3534 procfs_target::info_proc (const char *args
, enum info_proc_what what
)
3536 procinfo
*process
= NULL
;
3537 procinfo
*thread
= NULL
;
3554 error (_("Not supported on this target."));
3557 gdb_argv
built_argv (args
);
3558 for (char *arg
: built_argv
)
3560 if (isdigit (arg
[0]))
3562 pid
= strtoul (arg
, &tmp
, 10);
3564 tid
= strtoul (++tmp
, NULL
, 10);
3566 else if (arg
[0] == '/')
3568 tid
= strtoul (arg
+ 1, NULL
, 10);
3572 procinfo_up temporary_procinfo
;
3574 pid
= inferior_ptid
.pid ();
3576 error (_("No current process: you must name one."));
3579 /* Have pid, will travel.
3580 First see if it's a process we're already debugging. */
3581 process
= find_procinfo (pid
, 0);
3582 if (process
== NULL
)
3584 /* No. So open a procinfo for it, but
3585 remember to close it again when finished. */
3586 process
= create_procinfo (pid
, 0);
3587 temporary_procinfo
.reset (process
);
3588 if (!open_procinfo_files (process
, FD_CTL
))
3589 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
3593 thread
= create_procinfo (pid
, tid
);
3597 printf_filtered (_("process %d flags:\n"), process
->pid
);
3598 proc_prettyprint_flags (proc_flags (process
), 1);
3599 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
3600 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
3601 if (proc_get_nthreads (process
) > 1)
3602 printf_filtered ("Process has %d threads.\n",
3603 proc_get_nthreads (process
));
3607 printf_filtered (_("thread %d flags:\n"), thread
->tid
);
3608 proc_prettyprint_flags (proc_flags (thread
), 1);
3609 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
3610 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
3614 info_proc_mappings (process
, 0);
3619 /* Modify the status of the system call identified by SYSCALLNUM in
3620 the set of syscalls that are currently traced/debugged.
3622 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
3623 will be updated. Otherwise, the exit syscalls set will be updated.
3625 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
3626 will be disabled. */
3629 proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
, int entry_or_exit
,
3630 int mode
, int from_tty
)
3634 if (entry_or_exit
== PR_SYSENTRY
)
3635 sysset
= proc_get_traced_sysentry (pi
, NULL
);
3637 sysset
= proc_get_traced_sysexit (pi
, NULL
);
3640 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
3642 if (mode
== FLAG_SET
)
3643 praddset (sysset
, syscallnum
);
3645 prdelset (sysset
, syscallnum
);
3647 if (entry_or_exit
== PR_SYSENTRY
)
3649 if (!proc_set_traced_sysentry (pi
, sysset
))
3650 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
3654 if (!proc_set_traced_sysexit (pi
, sysset
))
3655 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
3660 proc_trace_syscalls (const char *args
, int from_tty
, int entry_or_exit
, int mode
)
3664 if (inferior_ptid
.pid () <= 0)
3665 error (_("you must be debugging a process to use this command."));
3667 if (args
== NULL
|| args
[0] == 0)
3668 error_no_arg (_("system call to trace"));
3670 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3671 if (isdigit (args
[0]))
3673 const int syscallnum
= atoi (args
);
3675 proc_trace_syscalls_1 (pi
, syscallnum
, entry_or_exit
, mode
, from_tty
);
3680 proc_trace_sysentry_cmd (const char *args
, int from_tty
)
3682 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
3686 proc_trace_sysexit_cmd (const char *args
, int from_tty
)
3688 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
3692 proc_untrace_sysentry_cmd (const char *args
, int from_tty
)
3694 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
3698 proc_untrace_sysexit_cmd (const char *args
, int from_tty
)
3700 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
3703 void _initialize_procfs ();
3705 _initialize_procfs ()
3707 gdb::observers::inferior_created
.attach (procfs_inferior_created
);
3709 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
3710 _("Give a trace of entries into the syscall."));
3711 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
3712 _("Give a trace of exits from the syscall."));
3713 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
3714 _("Cancel a trace of entries into the syscall."));
3715 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
3716 _("Cancel a trace of exits from the syscall."));
3718 add_inf_child_target (&the_procfs_target
);
3721 /* =================== END, GDB "MODULE" =================== */
3725 /* miscellaneous stubs: */
3727 /* The following satisfy a few random symbols mostly created by the
3728 solaris threads implementation, which I will chase down later. */
3730 /* Return a pid for which we guarantee we will be able to find a
3734 procfs_first_available (void)
3736 return ptid_t (procinfo_list
? procinfo_list
->pid
: -1);
3739 /* =================== GCORE .NOTE "MODULE" =================== */
3742 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3743 char *note_data
, int *note_size
,
3744 enum gdb_signal stop_signal
)
3746 struct regcache
*regcache
= get_thread_regcache (&the_procfs_target
, ptid
);
3747 gdb_gregset_t gregs
;
3748 gdb_fpregset_t fpregs
;
3749 unsigned long merged_pid
;
3751 merged_pid
= ptid
.lwp () << 16 | ptid
.pid ();
3753 /* This part is the old method for fetching registers.
3754 It should be replaced by the newer one using regsets
3755 once it is implemented in this platform:
3756 gdbarch_iterate_over_regset_sections(). */
3758 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
3759 inferior_ptid
= ptid
;
3760 target_fetch_registers (regcache
, -1);
3762 fill_gregset (regcache
, &gregs
, -1);
3763 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
3769 fill_fpregset (regcache
, &fpregs
, -1);
3770 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3779 struct procfs_corefile_thread_data
{
3783 enum gdb_signal stop_signal
;
3787 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
3789 struct procfs_corefile_thread_data
*args
3790 = (struct procfs_corefile_thread_data
*) data
;
3794 ptid_t ptid
= ptid_t (pi
->pid
, thread
->tid
, 0);
3796 args
->note_data
= procfs_do_thread_registers (args
->obfd
, ptid
,
3805 find_signalled_thread (struct thread_info
*info
, void *data
)
3807 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
3808 && info
->ptid
.pid () == inferior_ptid
.pid ())
3814 static enum gdb_signal
3815 find_stop_signal (void)
3817 struct thread_info
*info
=
3818 iterate_over_threads (find_signalled_thread
, NULL
);
3821 return info
->suspend
.stop_signal
;
3823 return GDB_SIGNAL_0
;
3827 procfs_target::make_corefile_notes (bfd
*obfd
, int *note_size
)
3829 gdb_gregset_t gregs
;
3830 char fname
[16] = {'\0'};
3831 char psargs
[80] = {'\0'};
3832 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3833 char *note_data
= NULL
;
3834 const char *inf_args
;
3835 struct procfs_corefile_thread_data thread_args
;
3836 enum gdb_signal stop_signal
;
3838 if (get_exec_file (0))
3840 strncpy (fname
, lbasename (get_exec_file (0)), sizeof (fname
));
3841 fname
[sizeof (fname
) - 1] = 0;
3842 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3843 psargs
[sizeof (psargs
) - 1] = 0;
3845 inf_args
= get_inferior_args ();
3846 if (inf_args
&& *inf_args
3847 && (strlen (inf_args
)
3848 < ((int) sizeof (psargs
) - (int) strlen (psargs
))))
3850 strncat (psargs
, " ",
3851 sizeof (psargs
) - strlen (psargs
));
3852 strncat (psargs
, inf_args
,
3853 sizeof (psargs
) - strlen (psargs
));
3857 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3863 stop_signal
= find_stop_signal ();
3865 fill_gregset (get_current_regcache (), &gregs
, -1);
3866 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
3867 inferior_ptid
.pid (),
3868 stop_signal
, &gregs
);
3870 thread_args
.obfd
= obfd
;
3871 thread_args
.note_data
= note_data
;
3872 thread_args
.note_size
= note_size
;
3873 thread_args
.stop_signal
= stop_signal
;
3874 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
,
3876 note_data
= thread_args
.note_data
;
3878 gdb::optional
<gdb::byte_vector
> auxv
=
3879 target_read_alloc (current_top_target (), TARGET_OBJECT_AUXV
, NULL
);
3880 if (auxv
&& !auxv
->empty ())
3881 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3882 "CORE", NT_AUXV
, auxv
->data (),
3887 /* =================== END GCORE .NOTE "MODULE" =================== */