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
;
165 static procfs_target the_procfs_target
;
167 #if PR_MODEL_NATIVE == PR_MODEL_LP64
168 /* When GDB is built as 64-bit application on Solaris, the auxv data
169 is presented in 64-bit format. We need to provide a custom parser
172 procfs_target::auxv_parse (gdb_byte
**readptr
,
173 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
175 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
176 gdb_byte
*ptr
= *readptr
;
181 if (endptr
- ptr
< 8 * 2)
184 *typep
= extract_unsigned_integer (ptr
, 4, byte_order
);
186 /* The size of data is always 64-bit. If the application is 32-bit,
187 it will be zero extended, as expected. */
188 *valp
= extract_unsigned_integer (ptr
, 8, byte_order
);
196 /* =================== END, TARGET_OPS "MODULE" =================== */
198 /* World Unification:
200 Put any typedefs, defines etc. here that are required for the
201 unification of code that handles different versions of /proc. */
203 enum { READ_WATCHFLAG
= WA_READ
,
204 WRITE_WATCHFLAG
= WA_WRITE
,
205 EXEC_WATCHFLAG
= WA_EXEC
,
206 AFTER_WATCHFLAG
= WA_TRAPAFTER
210 /* =================== STRUCT PROCINFO "MODULE" =================== */
212 /* FIXME: this comment will soon be out of date W.R.T. threads. */
214 /* The procinfo struct is a wrapper to hold all the state information
215 concerning a /proc process. There should be exactly one procinfo
216 for each process, and since GDB currently can debug only one
217 process at a time, that means there should be only one procinfo.
218 All of the LWP's of a process can be accessed indirectly thru the
219 single process procinfo.
221 However, against the day when GDB may debug more than one process,
222 this data structure is kept in a list (which for now will hold no
223 more than one member), and many functions will have a pointer to a
224 procinfo as an argument.
226 There will be a separate procinfo structure for use by the (not yet
227 implemented) "info proc" command, so that we can print useful
228 information about any random process without interfering with the
229 inferior's procinfo information. */
231 /* format strings for /proc paths */
232 #define MAIN_PROC_NAME_FMT "/proc/%d"
233 #define CTL_PROC_NAME_FMT "/proc/%d/ctl"
234 #define AS_PROC_NAME_FMT "/proc/%d/as"
235 #define MAP_PROC_NAME_FMT "/proc/%d/map"
236 #define STATUS_PROC_NAME_FMT "/proc/%d/status"
237 #define MAX_PROC_NAME_SIZE sizeof("/proc/999999/lwp/0123456789/lwpstatus")
239 typedef struct procinfo
{
240 struct procinfo
*next
;
241 int pid
; /* Process ID */
242 int tid
; /* Thread/LWP id */
246 int ignore_next_sigstop
;
248 int ctl_fd
; /* File descriptor for /proc control file */
249 int status_fd
; /* File descriptor for /proc status file */
250 int as_fd
; /* File descriptor for /proc as file */
252 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
254 fltset_t saved_fltset
; /* Saved traced hardware fault set */
255 sigset_t saved_sigset
; /* Saved traced signal set */
256 sigset_t saved_sighold
; /* Saved held signal set */
257 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
258 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
260 pstatus_t prstatus
; /* Current process status info */
262 struct procinfo
*thread_list
;
264 int status_valid
: 1;
266 int fpregs_valid
: 1;
267 int threads_valid
: 1;
270 static char errmsg
[128]; /* shared error msg buffer */
272 /* Function prototypes for procinfo module: */
274 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
275 static procinfo
*find_procinfo (int pid
, int tid
);
276 static procinfo
*create_procinfo (int pid
, int tid
);
277 static void destroy_procinfo (procinfo
*p
);
278 static void dead_procinfo (procinfo
*p
, const char *msg
, int killp
);
279 static int open_procinfo_files (procinfo
*p
, int which
);
280 static void close_procinfo_files (procinfo
*p
);
282 static int iterate_over_mappings
283 (procinfo
*pi
, find_memory_region_ftype child_func
, void *data
,
284 int (*func
) (struct prmap
*map
, find_memory_region_ftype child_func
,
287 /* The head of the procinfo list: */
288 static procinfo
*procinfo_list
;
290 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
294 find_procinfo (int pid
, int tid
)
298 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
305 /* Don't check threads_valid. If we're updating the
306 thread_list, we want to find whatever threads are already
307 here. This means that in general it is the caller's
308 responsibility to check threads_valid and update before
309 calling find_procinfo, if the caller wants to find a new
312 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
320 /* Calls find_procinfo, but errors on failure. */
323 find_procinfo_or_die (int pid
, int tid
)
325 procinfo
*pi
= find_procinfo (pid
, tid
);
330 error (_("procfs: couldn't find pid %d "
331 "(kernel thread %d) in procinfo list."),
334 error (_("procfs: couldn't find pid %d in procinfo list."), pid
);
339 /* Wrapper for `open'. The appropriate open call is attempted; if
340 unsuccessful, it will be retried as many times as needed for the
341 EAGAIN and EINTR conditions.
343 For other conditions, retry the open a limited number of times. In
344 addition, a short sleep is imposed prior to retrying the open. The
345 reason for this sleep is to give the kernel a chance to catch up
346 and create the file in question in the event that GDB "wins" the
347 race to open a file before the kernel has created it. */
350 open_with_retry (const char *pathname
, int flags
)
352 int retries_remaining
, status
;
354 retries_remaining
= 2;
358 status
= open (pathname
, flags
);
360 if (status
>= 0 || retries_remaining
== 0)
362 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
372 /* Open the file descriptor for the process or LWP. We only open the
373 control file descriptor; the others are opened lazily as needed.
374 Returns the file descriptor, or zero for failure. */
376 enum { FD_CTL
, FD_STATUS
, FD_AS
};
379 open_procinfo_files (procinfo
*pi
, int which
)
381 char tmp
[MAX_PROC_NAME_SIZE
];
384 /* This function is getting ALMOST long enough to break up into
385 several. Here is some rationale:
387 There are several file descriptors that may need to be open
388 for any given process or LWP. The ones we're interested in are:
389 - control (ctl) write-only change the state
390 - status (status) read-only query the state
391 - address space (as) read/write access memory
392 - map (map) read-only virtual addr map
393 Most of these are opened lazily as they are needed.
394 The pathnames for the 'files' for an LWP look slightly
395 different from those of a first-class process:
396 Pathnames for a process (<proc-id>):
398 /proc/<proc-id>/status
401 Pathnames for an LWP (lwp-id):
402 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
403 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
404 An LWP has no map or address space file descriptor, since
405 the memory map and address space are shared by all LWPs. */
407 /* In this case, there are several different file descriptors that
408 we might be asked to open. The control file descriptor will be
409 opened early, but the others will be opened lazily as they are
412 strcpy (tmp
, pi
->pathname
);
413 switch (which
) { /* Which file descriptor to open? */
416 strcat (tmp
, "/lwpctl");
418 strcat (tmp
, "/ctl");
419 fd
= open_with_retry (tmp
, O_WRONLY
);
426 return 0; /* There is no 'as' file descriptor for an lwp. */
428 fd
= open_with_retry (tmp
, O_RDWR
);
435 strcat (tmp
, "/lwpstatus");
437 strcat (tmp
, "/status");
438 fd
= open_with_retry (tmp
, O_RDONLY
);
444 return 0; /* unknown file descriptor */
447 return 1; /* success */
450 /* Allocate a data structure and link it into the procinfo list.
451 First tries to find a pre-existing one (FIXME: why?). Returns the
452 pointer to new procinfo struct. */
455 create_procinfo (int pid
, int tid
)
457 procinfo
*pi
, *parent
= NULL
;
459 pi
= find_procinfo (pid
, tid
);
461 return pi
; /* Already exists, nothing to do. */
463 /* Find parent before doing malloc, to save having to cleanup. */
465 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
467 doesn't exist yet? */
469 pi
= XNEW (procinfo
);
470 memset (pi
, 0, sizeof (procinfo
));
474 pi
->saved_entryset
= XNEW (sysset_t
);
475 pi
->saved_exitset
= XNEW (sysset_t
);
477 /* Chain into list. */
480 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), MAIN_PROC_NAME_FMT
, pid
);
481 pi
->next
= procinfo_list
;
486 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), "/proc/%d/lwp/%d",
488 pi
->next
= parent
->thread_list
;
489 parent
->thread_list
= pi
;
494 /* Close all file descriptors associated with the procinfo. */
497 close_procinfo_files (procinfo
*pi
)
503 if (pi
->status_fd
> 0)
504 close (pi
->status_fd
);
505 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
508 /* Destructor function. Close, unlink and deallocate the object. */
511 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
515 /* Step one: unlink the procinfo from its list. */
519 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
522 ptr
->next
= pi
->next
;
526 /* Step two: close any open file descriptors. */
527 close_procinfo_files (pi
);
529 /* Step three: free the memory. */
530 xfree (pi
->saved_entryset
);
531 xfree (pi
->saved_exitset
);
536 destroy_procinfo (procinfo
*pi
)
540 if (pi
->tid
!= 0) /* Destroy a thread procinfo. */
542 tmp
= find_procinfo (pi
->pid
, 0); /* Find the parent process. */
543 destroy_one_procinfo (&tmp
->thread_list
, pi
);
545 else /* Destroy a process procinfo and all its threads. */
547 /* First destroy the children, if any; */
548 while (pi
->thread_list
!= NULL
)
549 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
550 /* Then destroy the parent. Genocide!!! */
551 destroy_one_procinfo (&procinfo_list
, pi
);
555 /* A deleter that calls destroy_procinfo. */
556 struct procinfo_deleter
558 void operator() (procinfo
*pi
) const
560 destroy_procinfo (pi
);
564 typedef std::unique_ptr
<procinfo
, procinfo_deleter
> procinfo_up
;
566 enum { NOKILL
, KILL
};
568 /* To be called on a non_recoverable error for a procinfo. Prints
569 error messages, optionally sends a SIGKILL to the process, then
570 destroys the data structure. */
573 dead_procinfo (procinfo
*pi
, const char *msg
, int kill_p
)
578 print_sys_errmsg (pi
->pathname
, errno
);
581 xsnprintf (procfile
, sizeof (procfile
), "process %d", pi
->pid
);
582 print_sys_errmsg (procfile
, errno
);
585 kill (pi
->pid
, SIGKILL
);
587 destroy_procinfo (pi
);
591 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
593 /* =================== /proc "MODULE" =================== */
595 /* This "module" is the interface layer between the /proc system API
596 and the gdb target vector functions. This layer consists of access
597 functions that encapsulate each of the basic operations that we
598 need to use from the /proc API.
600 The main motivation for this layer is to hide the fact that there
601 are two very different implementations of the /proc API. Rather
602 than have a bunch of #ifdefs all thru the gdb target vector
603 functions, we do our best to hide them all in here. */
605 static long proc_flags (procinfo
*pi
);
606 static int proc_why (procinfo
*pi
);
607 static int proc_what (procinfo
*pi
);
608 static int proc_set_current_signal (procinfo
*pi
, int signo
);
609 static int proc_get_current_thread (procinfo
*pi
);
610 static int proc_iterate_over_threads
612 int (*func
) (procinfo
*, procinfo
*, void *),
616 proc_warn (procinfo
*pi
, const char *func
, int line
)
618 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
619 func
, line
, pi
->pathname
);
620 print_sys_errmsg (errmsg
, errno
);
624 proc_error (procinfo
*pi
, const char *func
, int line
)
626 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
627 func
, line
, pi
->pathname
);
628 perror_with_name (errmsg
);
631 /* Updates the status struct in the procinfo. There is a 'valid'
632 flag, to let other functions know when this function needs to be
633 called (so the status is only read when it is needed). The status
634 file descriptor is also only opened when it is needed. Returns
635 non-zero for success, zero for failure. */
638 proc_get_status (procinfo
*pi
)
640 /* Status file descriptor is opened "lazily". */
641 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
643 pi
->status_valid
= 0;
647 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
648 pi
->status_valid
= 0; /* fail */
651 /* Sigh... I have to read a different data structure,
652 depending on whether this is a main process or an LWP. */
654 pi
->status_valid
= (read (pi
->status_fd
,
655 (char *) &pi
->prstatus
.pr_lwp
,
656 sizeof (lwpstatus_t
))
657 == sizeof (lwpstatus_t
));
660 pi
->status_valid
= (read (pi
->status_fd
,
661 (char *) &pi
->prstatus
,
663 == sizeof (pstatus_t
));
667 if (pi
->status_valid
)
669 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
672 proc_get_current_thread (pi
));
675 /* The status struct includes general regs, so mark them valid too. */
676 pi
->gregs_valid
= pi
->status_valid
;
677 /* In the read/write multiple-fd model, the status struct includes
678 the fp regs too, so mark them valid too. */
679 pi
->fpregs_valid
= pi
->status_valid
;
680 return pi
->status_valid
; /* True if success, false if failure. */
683 /* Returns the process flags (pr_flags field). */
686 proc_flags (procinfo
*pi
)
688 if (!pi
->status_valid
)
689 if (!proc_get_status (pi
))
690 return 0; /* FIXME: not a good failure value (but what is?) */
692 return pi
->prstatus
.pr_lwp
.pr_flags
;
695 /* Returns the pr_why field (why the process stopped). */
698 proc_why (procinfo
*pi
)
700 if (!pi
->status_valid
)
701 if (!proc_get_status (pi
))
702 return 0; /* FIXME: not a good failure value (but what is?) */
704 return pi
->prstatus
.pr_lwp
.pr_why
;
707 /* Returns the pr_what field (details of why the process stopped). */
710 proc_what (procinfo
*pi
)
712 if (!pi
->status_valid
)
713 if (!proc_get_status (pi
))
714 return 0; /* FIXME: not a good failure value (but what is?) */
716 return pi
->prstatus
.pr_lwp
.pr_what
;
719 /* This function is only called when PI is stopped by a watchpoint.
720 Assuming the OS supports it, write to *ADDR the data address which
721 triggered it and return 1. Return 0 if it is not possible to know
725 proc_watchpoint_address (procinfo
*pi
, CORE_ADDR
*addr
)
727 if (!pi
->status_valid
)
728 if (!proc_get_status (pi
))
731 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
732 builtin_type (target_gdbarch ())->builtin_data_ptr
,
733 (gdb_byte
*) &pi
->prstatus
.pr_lwp
.pr_info
.si_addr
);
737 /* Returns the pr_nsysarg field (number of args to the current
741 proc_nsysarg (procinfo
*pi
)
743 if (!pi
->status_valid
)
744 if (!proc_get_status (pi
))
747 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
750 /* Returns the pr_sysarg field (pointer to the arguments of current
754 proc_sysargs (procinfo
*pi
)
756 if (!pi
->status_valid
)
757 if (!proc_get_status (pi
))
760 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
763 /* Set or reset any of the following process flags:
764 PR_FORK -- forked child will inherit trace flags
765 PR_RLC -- traced process runs when last /proc file closed.
766 PR_KLC -- traced process is killed when last /proc file closed.
767 PR_ASYNC -- LWP's get to run/stop independently.
769 This function is done using read/write [PCSET/PCRESET/PCUNSET].
773 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
774 mode -- 1 for set, 0 for reset.
776 Returns non-zero for success, zero for failure. */
778 enum { FLAG_RESET
, FLAG_SET
};
781 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
783 long win
= 0; /* default to fail */
785 /* These operations affect the process as a whole, and applying them
786 to an individual LWP has the same meaning as applying them to the
787 main process. Therefore, if we're ever called with a pointer to
788 an LWP's procinfo, let's substitute the process's procinfo and
789 avoid opening the LWP's file descriptor unnecessarily. */
792 pi
= find_procinfo_or_die (pi
->pid
, 0);
796 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC). */
798 else /* Reset the flag. */
802 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
804 /* The above operation renders the procinfo's cached pstatus
806 pi
->status_valid
= 0;
809 warning (_("procfs: modify_flag failed to turn %s %s"),
810 flag
== PR_FORK
? "PR_FORK" :
811 flag
== PR_RLC
? "PR_RLC" :
812 flag
== PR_ASYNC
? "PR_ASYNC" :
813 flag
== PR_KLC
? "PR_KLC" :
815 mode
== FLAG_RESET
? "off" : "on");
820 /* Set the run_on_last_close flag. Process with all threads will
821 become runnable when debugger closes all /proc fds. Returns
822 non-zero for success, zero for failure. */
825 proc_set_run_on_last_close (procinfo
*pi
)
827 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
830 /* Reset the run_on_last_close flag. The process will NOT become
831 runnable when debugger closes its file handles. Returns non-zero
832 for success, zero for failure. */
835 proc_unset_run_on_last_close (procinfo
*pi
)
837 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
840 /* Reset inherit_on_fork flag. If the process forks a child while we
841 are registered for events in the parent, then we will NOT receive
842 events from the child. Returns non-zero for success, zero for
846 proc_unset_inherit_on_fork (procinfo
*pi
)
848 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
851 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
852 (signal etc.), the remaining LWPs will continue to run. Returns
853 non-zero for success, zero for failure. */
856 proc_set_async (procinfo
*pi
)
858 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
861 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
862 (signal etc.), then all other LWPs will stop as well. Returns
863 non-zero for success, zero for failure. */
866 proc_unset_async (procinfo
*pi
)
868 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
871 /* Request the process/LWP to stop. Does not wait. Returns non-zero
872 for success, zero for failure. */
875 proc_stop_process (procinfo
*pi
)
879 /* We might conceivably apply this operation to an LWP, and the
880 LWP's ctl file descriptor might not be open. */
882 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
886 procfs_ctl_t cmd
= PCSTOP
;
888 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
894 /* Wait for the process or LWP to stop (block until it does). Returns
895 non-zero for success, zero for failure. */
898 proc_wait_for_stop (procinfo
*pi
)
902 /* We should never have to apply this operation to any procinfo
903 except the one for the main process. If that ever changes for
904 any reason, then take out the following clause and replace it
905 with one that makes sure the ctl_fd is open. */
908 pi
= find_procinfo_or_die (pi
->pid
, 0);
910 procfs_ctl_t cmd
= PCWSTOP
;
914 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
916 clear_sigint_trap ();
918 /* We been runnin' and we stopped -- need to update status. */
919 pi
->status_valid
= 0;
924 /* Make the process or LWP runnable.
926 Options (not all are implemented):
928 - clear current fault
929 - clear current signal
930 - abort the current system call
931 - stop as soon as finished with system call
932 - (ioctl): set traced signal set
933 - (ioctl): set held signal set
934 - (ioctl): set traced fault set
935 - (ioctl): set start pc (vaddr)
937 Always clears the current fault. PI is the process or LWP to
938 operate on. If STEP is true, set the process or LWP to trap after
939 one instruction. If SIGNO is zero, clear the current signal if
940 any; if non-zero, set the current signal to this one. Returns
941 non-zero for success, zero for failure. */
944 proc_run_process (procinfo
*pi
, int step
, int signo
)
949 /* We will probably have to apply this operation to individual
950 threads, so make sure the control file descriptor is open. */
952 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
955 runflags
= PRCFAULT
; /* Always clear current fault. */
960 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals. */
961 proc_set_current_signal (pi
, signo
);
967 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
972 /* Register to trace signals in the process or LWP. Returns non-zero
973 for success, zero for failure. */
976 proc_set_traced_signals (procinfo
*pi
, sigset_t
*sigset
)
980 /* We should never have to apply this operation to any procinfo
981 except the one for the main process. If that ever changes for
982 any reason, then take out the following clause and replace it
983 with one that makes sure the ctl_fd is open. */
986 pi
= find_procinfo_or_die (pi
->pid
, 0);
990 /* Use char array to avoid alignment issues. */
991 char sigset
[sizeof (sigset_t
)];
995 memcpy (&arg
.sigset
, sigset
, sizeof (sigset_t
));
997 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
999 /* The above operation renders the procinfo's cached pstatus obsolete. */
1000 pi
->status_valid
= 0;
1003 warning (_("procfs: set_traced_signals failed"));
1007 /* Register to trace hardware faults in the process or LWP. Returns
1008 non-zero for success, zero for failure. */
1011 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1015 /* We should never have to apply this operation to any procinfo
1016 except the one for the main process. If that ever changes for
1017 any reason, then take out the following clause and replace it
1018 with one that makes sure the ctl_fd is open. */
1021 pi
= find_procinfo_or_die (pi
->pid
, 0);
1025 /* Use char array to avoid alignment issues. */
1026 char fltset
[sizeof (fltset_t
)];
1030 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1032 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1034 /* The above operation renders the procinfo's cached pstatus obsolete. */
1035 pi
->status_valid
= 0;
1040 /* Register to trace entry to system calls in the process or LWP.
1041 Returns non-zero for success, zero for failure. */
1044 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1048 /* We should never have to apply this operation to any procinfo
1049 except the one for the main process. If that ever changes for
1050 any reason, then take out the following clause and replace it
1051 with one that makes sure the ctl_fd is open. */
1054 pi
= find_procinfo_or_die (pi
->pid
, 0);
1058 /* Use char array to avoid alignment issues. */
1059 char sysset
[sizeof (sysset_t
)];
1063 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1065 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1067 /* The above operation renders the procinfo's cached pstatus
1069 pi
->status_valid
= 0;
1074 /* Register to trace exit from system calls in the process or LWP.
1075 Returns non-zero for success, zero for failure. */
1078 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1082 /* We should never have to apply this operation to any procinfo
1083 except the one for the main process. If that ever changes for
1084 any reason, then take out the following clause and replace it
1085 with one that makes sure the ctl_fd is open. */
1088 pi
= find_procinfo_or_die (pi
->pid
, 0);
1090 struct gdb_proc_ctl_pcsexit
{
1092 /* Use char array to avoid alignment issues. */
1093 char sysset
[sizeof (sysset_t
)];
1097 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1099 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1101 /* The above operation renders the procinfo's cached pstatus
1103 pi
->status_valid
= 0;
1108 /* Specify the set of blocked / held signals in the process or LWP.
1109 Returns non-zero for success, zero for failure. */
1112 proc_set_held_signals (procinfo
*pi
, sigset_t
*sighold
)
1116 /* We should never have to apply this operation to any procinfo
1117 except the one for the main process. If that ever changes for
1118 any reason, then take out the following clause and replace it
1119 with one that makes sure the ctl_fd is open. */
1122 pi
= find_procinfo_or_die (pi
->pid
, 0);
1126 /* Use char array to avoid alignment issues. */
1127 char hold
[sizeof (sigset_t
)];
1131 memcpy (&arg
.hold
, sighold
, sizeof (sigset_t
));
1132 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1134 /* The above operation renders the procinfo's cached pstatus
1136 pi
->status_valid
= 0;
1141 /* Returns the set of signals that are held / blocked. Will also copy
1142 the sigset if SAVE is non-zero. */
1145 proc_get_held_signals (procinfo
*pi
, sigset_t
*save
)
1147 sigset_t
*ret
= NULL
;
1149 /* We should never have to apply this operation to any procinfo
1150 except the one for the main process. If that ever changes for
1151 any reason, then take out the following clause and replace it
1152 with one that makes sure the ctl_fd is open. */
1155 pi
= find_procinfo_or_die (pi
->pid
, 0);
1157 if (!pi
->status_valid
)
1158 if (!proc_get_status (pi
))
1161 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1163 memcpy (save
, ret
, sizeof (sigset_t
));
1168 /* Returns the set of signals that are traced / debugged. Will also
1169 copy the sigset if SAVE is non-zero. */
1172 proc_get_traced_signals (procinfo
*pi
, sigset_t
*save
)
1174 sigset_t
*ret
= NULL
;
1176 /* We should never have to apply this operation to any procinfo
1177 except the one for the main process. If that ever changes for
1178 any reason, then take out the following clause and replace it
1179 with one that makes sure the ctl_fd is open. */
1182 pi
= find_procinfo_or_die (pi
->pid
, 0);
1184 if (!pi
->status_valid
)
1185 if (!proc_get_status (pi
))
1188 ret
= &pi
->prstatus
.pr_sigtrace
;
1190 memcpy (save
, ret
, sizeof (sigset_t
));
1195 /* Returns the set of hardware faults that are traced /debugged. Will
1196 also copy the faultset if SAVE is non-zero. */
1199 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1201 fltset_t
*ret
= NULL
;
1203 /* We should never have to apply this operation to any procinfo
1204 except the one for the main process. If that ever changes for
1205 any reason, then take out the following clause and replace it
1206 with one that makes sure the ctl_fd is open. */
1209 pi
= find_procinfo_or_die (pi
->pid
, 0);
1211 if (!pi
->status_valid
)
1212 if (!proc_get_status (pi
))
1215 ret
= &pi
->prstatus
.pr_flttrace
;
1217 memcpy (save
, ret
, sizeof (fltset_t
));
1222 /* Returns the set of syscalls that are traced /debugged on entry.
1223 Will also copy the syscall set if SAVE is non-zero. */
1226 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1228 sysset_t
*ret
= NULL
;
1230 /* We should never have to apply this operation to any procinfo
1231 except the one for the main process. If that ever changes for
1232 any reason, then take out the following clause and replace it
1233 with one that makes sure the ctl_fd is open. */
1236 pi
= find_procinfo_or_die (pi
->pid
, 0);
1238 if (!pi
->status_valid
)
1239 if (!proc_get_status (pi
))
1242 ret
= &pi
->prstatus
.pr_sysentry
;
1244 memcpy (save
, ret
, sizeof (sysset_t
));
1249 /* Returns the set of syscalls that are traced /debugged on exit.
1250 Will also copy the syscall set if SAVE is non-zero. */
1253 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1255 sysset_t
*ret
= NULL
;
1257 /* We should never have to apply this operation to any procinfo
1258 except the one for the main process. If that ever changes for
1259 any reason, then take out the following clause and replace it
1260 with one that makes sure the ctl_fd is open. */
1263 pi
= find_procinfo_or_die (pi
->pid
, 0);
1265 if (!pi
->status_valid
)
1266 if (!proc_get_status (pi
))
1269 ret
= &pi
->prstatus
.pr_sysexit
;
1271 memcpy (save
, ret
, sizeof (sysset_t
));
1276 /* The current fault (if any) is cleared; the associated signal will
1277 not be sent to the process or LWP when it resumes. Returns
1278 non-zero for success, zero for failure. */
1281 proc_clear_current_fault (procinfo
*pi
)
1285 /* We should never have to apply this operation to any procinfo
1286 except the one for the main process. If that ever changes for
1287 any reason, then take out the following clause and replace it
1288 with one that makes sure the ctl_fd is open. */
1291 pi
= find_procinfo_or_die (pi
->pid
, 0);
1293 procfs_ctl_t cmd
= PCCFAULT
;
1295 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1300 /* Set the "current signal" that will be delivered next to the
1301 process. NOTE: semantics are different from those of KILL. This
1302 signal will be delivered to the process or LWP immediately when it
1303 is resumed (even if the signal is held/blocked); it will NOT
1304 immediately cause another event of interest, and will NOT first
1305 trap back to the debugger. Returns non-zero for success, zero for
1309 proc_set_current_signal (procinfo
*pi
, int signo
)
1314 /* Use char array to avoid alignment issues. */
1315 char sinfo
[sizeof (siginfo_t
)];
1319 struct target_waitstatus wait_status
;
1321 /* We should never have to apply this operation to any procinfo
1322 except the one for the main process. If that ever changes for
1323 any reason, then take out the following clause and replace it
1324 with one that makes sure the ctl_fd is open. */
1327 pi
= find_procinfo_or_die (pi
->pid
, 0);
1329 /* The pointer is just a type alias. */
1330 get_last_target_status (&wait_ptid
, &wait_status
);
1331 if (wait_ptid
== inferior_ptid
1332 && wait_status
.kind
== TARGET_WAITKIND_STOPPED
1333 && wait_status
.value
.sig
== gdb_signal_from_host (signo
)
1334 && proc_get_status (pi
)
1335 && pi
->prstatus
.pr_lwp
.pr_info
.si_signo
== signo
1337 /* Use the siginfo associated with the signal being
1339 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_lwp
.pr_info
, sizeof (siginfo_t
));
1342 mysinfo
.si_signo
= signo
;
1343 mysinfo
.si_code
= 0;
1344 mysinfo
.si_pid
= getpid (); /* ?why? */
1345 mysinfo
.si_uid
= getuid (); /* ?why? */
1346 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1350 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1355 /* The current signal (if any) is cleared, and is not sent to the
1356 process or LWP when it resumes. Returns non-zero for success, zero
1360 proc_clear_current_signal (procinfo
*pi
)
1364 /* We should never have to apply this operation to any procinfo
1365 except the one for the main process. If that ever changes for
1366 any reason, then take out the following clause and replace it
1367 with one that makes sure the ctl_fd is open. */
1370 pi
= find_procinfo_or_die (pi
->pid
, 0);
1374 /* Use char array to avoid alignment issues. */
1375 char sinfo
[sizeof (siginfo_t
)];
1380 /* The pointer is just a type alias. */
1381 mysinfo
.si_signo
= 0;
1382 mysinfo
.si_code
= 0;
1383 mysinfo
.si_errno
= 0;
1384 mysinfo
.si_pid
= getpid (); /* ?why? */
1385 mysinfo
.si_uid
= getuid (); /* ?why? */
1386 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1388 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1393 /* Return the general-purpose registers for the process or LWP
1394 corresponding to PI. Upon failure, return NULL. */
1396 static gdb_gregset_t
*
1397 proc_get_gregs (procinfo
*pi
)
1399 if (!pi
->status_valid
|| !pi
->gregs_valid
)
1400 if (!proc_get_status (pi
))
1403 return &pi
->prstatus
.pr_lwp
.pr_reg
;
1406 /* Return the general-purpose registers for the process or LWP
1407 corresponding to PI. Upon failure, return NULL. */
1409 static gdb_fpregset_t
*
1410 proc_get_fpregs (procinfo
*pi
)
1412 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
1413 if (!proc_get_status (pi
))
1416 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
1419 /* Write the general-purpose registers back to the process or LWP
1420 corresponding to PI. Return non-zero for success, zero for
1424 proc_set_gregs (procinfo
*pi
)
1426 gdb_gregset_t
*gregs
;
1429 gregs
= proc_get_gregs (pi
);
1431 return 0; /* proc_get_regs has already warned. */
1433 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1439 /* Use char array to avoid alignment issues. */
1440 char gregs
[sizeof (gdb_gregset_t
)];
1444 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
1445 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1448 /* Policy: writing the registers invalidates our cache. */
1449 pi
->gregs_valid
= 0;
1453 /* Write the floating-pointer registers back to the process or LWP
1454 corresponding to PI. Return non-zero for success, zero for
1458 proc_set_fpregs (procinfo
*pi
)
1460 gdb_fpregset_t
*fpregs
;
1463 fpregs
= proc_get_fpregs (pi
);
1465 return 0; /* proc_get_fpregs has already warned. */
1467 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1473 /* Use char array to avoid alignment issues. */
1474 char fpregs
[sizeof (gdb_fpregset_t
)];
1478 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
1479 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1482 /* Policy: writing the registers invalidates our cache. */
1483 pi
->fpregs_valid
= 0;
1487 /* Send a signal to the proc or lwp with the semantics of "kill()".
1488 Returns non-zero for success, zero for failure. */
1491 proc_kill (procinfo
*pi
, int signo
)
1495 /* We might conceivably apply this operation to an LWP, and the
1496 LWP's ctl file descriptor might not be open. */
1498 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1502 procfs_ctl_t cmd
[2];
1506 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1512 /* Find the pid of the process that started this one. Returns the
1513 parent process pid, or zero. */
1516 proc_parent_pid (procinfo
*pi
)
1518 /* We should never have to apply this operation to any procinfo
1519 except the one for the main process. If that ever changes for
1520 any reason, then take out the following clause and replace it
1521 with one that makes sure the ctl_fd is open. */
1524 pi
= find_procinfo_or_die (pi
->pid
, 0);
1526 if (!pi
->status_valid
)
1527 if (!proc_get_status (pi
))
1530 return pi
->prstatus
.pr_ppid
;
1533 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
1534 (a.k.a void pointer)! */
1537 procfs_address_to_host_pointer (CORE_ADDR addr
)
1539 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
1542 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (ptr_type
));
1543 gdbarch_address_to_pointer (target_gdbarch (), ptr_type
,
1544 (gdb_byte
*) &ptr
, addr
);
1549 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
1553 char watch
[sizeof (prwatch_t
)];
1557 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
1558 convert a target address into something that can be stored in a
1559 native data structure. */
1560 pwatch
.pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
1561 pwatch
.pr_size
= len
;
1562 pwatch
.pr_wflags
= wflags
;
1564 memcpy (arg
.watch
, &pwatch
, sizeof (prwatch_t
));
1565 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
1568 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
1570 #include <sys/sysi86.h>
1572 /* The KEY is actually the value of the lower 16 bits of the GS
1573 register for the LWP that we're interested in. Returns the
1574 matching ssh struct (LDT entry). */
1577 proc_get_LDT_entry (procinfo
*pi
, int key
) /* ARI: editCase function */
1579 static struct ssd
*ldt_entry
= NULL
;
1580 char pathname
[MAX_PROC_NAME_SIZE
];
1582 /* Allocate space for one LDT entry.
1583 This alloc must persist, because we return a pointer to it. */
1584 if (ldt_entry
== NULL
)
1585 ldt_entry
= XNEW (struct ssd
);
1587 /* Open the file descriptor for the LDT table. */
1588 xsnprintf (pathname
, sizeof (pathname
), "/proc/%d/ldt", pi
->pid
);
1589 scoped_fd
fd (open_with_retry (pathname
, O_RDONLY
));
1592 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
1596 /* Now 'read' thru the table, find a match and return it. */
1597 while (read (fd
.get (), ldt_entry
, sizeof (struct ssd
))
1598 == sizeof (struct ssd
))
1600 if (ldt_entry
->sel
== 0
1601 && ldt_entry
->bo
== 0
1602 && ldt_entry
->acc1
== 0
1603 && ldt_entry
->acc2
== 0)
1604 break; /* end of table */
1605 /* If key matches, return this entry. */
1606 if (ldt_entry
->sel
== key
)
1609 /* Loop ended, match not found. */
1613 /* Returns the pointer to the LDT entry of PTID. */
1616 procfs_find_LDT_entry (ptid_t ptid
) /* ARI: editCase function */
1618 gdb_gregset_t
*gregs
;
1622 /* Find procinfo for the lwp. */
1623 pi
= find_procinfo (ptid
.pid (), ptid
.lwp ());
1626 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
1627 ptid
.pid (), ptid
.lwp ());
1630 /* get its general registers. */
1631 gregs
= proc_get_gregs (pi
);
1634 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
1635 ptid
.pid (), ptid
.lwp ());
1638 /* Now extract the GS register's lower 16 bits. */
1639 key
= (*gregs
)[GS
] & 0xffff;
1641 /* Find the matching entry and return it. */
1642 return proc_get_LDT_entry (pi
, key
);
1647 /* =============== END, non-thread part of /proc "MODULE" =============== */
1649 /* =================== Thread "MODULE" =================== */
1651 /* NOTE: you'll see more ifdefs and duplication of functions here,
1652 since there is a different way to do threads on every OS. */
1654 /* Returns the number of threads for the process. */
1657 proc_get_nthreads (procinfo
*pi
)
1659 if (!pi
->status_valid
)
1660 if (!proc_get_status (pi
))
1663 /* Only works for the process procinfo, because the LWP procinfos do not
1664 get prstatus filled in. */
1665 if (pi
->tid
!= 0) /* Find the parent process procinfo. */
1666 pi
= find_procinfo_or_die (pi
->pid
, 0);
1667 return pi
->prstatus
.pr_nlwp
;
1672 Return the ID of the thread that had an event of interest.
1673 (ie. the one that hit a breakpoint or other traced event). All
1674 other things being equal, this should be the ID of a thread that is
1675 currently executing. */
1678 proc_get_current_thread (procinfo
*pi
)
1680 /* Note: this should be applied to the root procinfo for the
1681 process, not to the procinfo for an LWP. If applied to the
1682 procinfo for an LWP, it will simply return that LWP's ID. In
1683 that case, find the parent process procinfo. */
1686 pi
= find_procinfo_or_die (pi
->pid
, 0);
1688 if (!pi
->status_valid
)
1689 if (!proc_get_status (pi
))
1692 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
1695 /* Discover the IDs of all the threads within the process, and create
1696 a procinfo for each of them (chained to the parent). This
1697 unfortunately requires a different method on every OS. Returns
1698 non-zero for success, zero for failure. */
1701 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
1703 if (thread
&& parent
) /* sanity */
1705 thread
->status_valid
= 0;
1706 if (!proc_get_status (thread
))
1707 destroy_one_procinfo (&parent
->thread_list
, thread
);
1709 return 0; /* keep iterating */
1713 proc_update_threads (procinfo
*pi
)
1715 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
1716 struct dirent
*direntry
;
1721 /* We should never have to apply this operation to any procinfo
1722 except the one for the main process. If that ever changes for
1723 any reason, then take out the following clause and replace it
1724 with one that makes sure the ctl_fd is open. */
1727 pi
= find_procinfo_or_die (pi
->pid
, 0);
1729 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
1731 /* Note: this brute-force method was originally devised for Unixware
1732 (support removed since), and will also work on Solaris 2.6 and
1733 2.7. The original comment mentioned the existence of a much
1734 simpler and more elegant way to do this on Solaris, but didn't
1735 point out what that was. */
1737 strcpy (pathname
, pi
->pathname
);
1738 strcat (pathname
, "/lwp");
1739 dirp
.reset (opendir (pathname
));
1741 proc_error (pi
, "update_threads, opendir", __LINE__
);
1743 while ((direntry
= readdir (dirp
.get ())) != NULL
)
1744 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
1746 lwpid
= atoi (&direntry
->d_name
[0]);
1747 thread
= create_procinfo (pi
->pid
, lwpid
);
1749 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
1751 pi
->threads_valid
= 1;
1755 /* Given a pointer to a function, call that function once for each lwp
1756 in the procinfo list, until the function returns non-zero, in which
1757 event return the value returned by the function.
1759 Note: this function does NOT call update_threads. If you want to
1760 discover new threads first, you must call that function explicitly.
1761 This function just makes a quick pass over the currently-known
1764 PI is the parent process procinfo. FUNC is the per-thread
1765 function. PTR is an opaque parameter for function. Returns the
1766 first non-zero return value from the callee, or zero. */
1769 proc_iterate_over_threads (procinfo
*pi
,
1770 int (*func
) (procinfo
*, procinfo
*, void *),
1773 procinfo
*thread
, *next
;
1776 /* We should never have to apply this operation to any procinfo
1777 except the one for the main process. If that ever changes for
1778 any reason, then take out the following clause and replace it
1779 with one that makes sure the ctl_fd is open. */
1782 pi
= find_procinfo_or_die (pi
->pid
, 0);
1784 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
1786 next
= thread
->next
; /* In case thread is destroyed. */
1787 retval
= (*func
) (pi
, thread
, ptr
);
1795 /* =================== END, Thread "MODULE" =================== */
1797 /* =================== END, /proc "MODULE" =================== */
1799 /* =================== GDB "MODULE" =================== */
1801 /* Here are all of the gdb target vector functions and their
1804 static ptid_t
do_attach (ptid_t ptid
);
1805 static void do_detach ();
1806 static void proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
,
1807 int entry_or_exit
, int mode
, int from_tty
);
1809 /* Sets up the inferior to be debugged. Registers to trace signals,
1810 hardware faults, and syscalls. Note: does not set RLC flag: caller
1811 may want to customize that. Returns zero for success (note!
1812 unlike most functions in this module); on failure, returns the LINE
1813 NUMBER where it failed! */
1816 procfs_debug_inferior (procinfo
*pi
)
1818 fltset_t traced_faults
;
1819 sigset_t traced_signals
;
1820 sysset_t
*traced_syscall_entries
;
1821 sysset_t
*traced_syscall_exits
;
1824 /* Register to trace hardware faults in the child. */
1825 prfillset (&traced_faults
); /* trace all faults... */
1826 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
1827 if (!proc_set_traced_faults (pi
, &traced_faults
))
1830 /* Initially, register to trace all signals in the child. */
1831 prfillset (&traced_signals
);
1832 if (!proc_set_traced_signals (pi
, &traced_signals
))
1836 /* Register to trace the 'exit' system call (on entry). */
1837 traced_syscall_entries
= XNEW (sysset_t
);
1838 premptyset (traced_syscall_entries
);
1839 praddset (traced_syscall_entries
, SYS_exit
);
1840 praddset (traced_syscall_entries
, SYS_lwp_exit
);
1842 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
1843 xfree (traced_syscall_entries
);
1847 /* Method for tracing exec syscalls. */
1849 Not all systems with /proc have all the exec* syscalls with the same
1850 names. On the SGI, for example, there is no SYS_exec, but there
1851 *is* a SYS_execv. So, we try to account for that. */
1853 traced_syscall_exits
= XNEW (sysset_t
);
1854 premptyset (traced_syscall_exits
);
1856 praddset (traced_syscall_exits
, SYS_exec
);
1858 praddset (traced_syscall_exits
, SYS_execve
);
1859 praddset (traced_syscall_exits
, SYS_lwp_create
);
1860 praddset (traced_syscall_exits
, SYS_lwp_exit
);
1862 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
1863 xfree (traced_syscall_exits
);
1871 procfs_target::attach (const char *args
, int from_tty
)
1875 pid
= parse_pid_to_attach (args
);
1877 if (pid
== getpid ())
1878 error (_("Attaching GDB to itself is not a good idea..."));
1882 const char *exec_file
= get_exec_file (0);
1885 printf_filtered (_("Attaching to program `%s', %s\n"),
1886 exec_file
, target_pid_to_str (ptid_t (pid
)).c_str ());
1888 printf_filtered (_("Attaching to %s\n"),
1889 target_pid_to_str (ptid_t (pid
)).c_str ());
1893 inferior_ptid
= do_attach (ptid_t (pid
));
1894 if (!target_is_pushed (this))
1899 procfs_target::detach (inferior
*inf
, int from_tty
)
1901 int pid
= inferior_ptid
.pid ();
1905 const char *exec_file
;
1907 exec_file
= get_exec_file (0);
1908 if (exec_file
== NULL
)
1911 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file
,
1912 target_pid_to_str (ptid_t (pid
)).c_str ());
1917 inferior_ptid
= null_ptid
;
1918 detach_inferior (inf
);
1919 maybe_unpush_target ();
1923 do_attach (ptid_t ptid
)
1926 struct inferior
*inf
;
1930 pi
= create_procinfo (ptid
.pid (), 0);
1932 perror (_("procfs: out of memory in 'attach'"));
1934 if (!open_procinfo_files (pi
, FD_CTL
))
1936 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
1937 xsnprintf (errmsg
, sizeof (errmsg
),
1938 "do_attach: couldn't open /proc file for process %d",
1940 dead_procinfo (pi
, errmsg
, NOKILL
);
1943 /* Stop the process (if it isn't already stopped). */
1944 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
1946 pi
->was_stopped
= 1;
1947 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
1951 pi
->was_stopped
= 0;
1952 /* Set the process to run again when we close it. */
1953 if (!proc_set_run_on_last_close (pi
))
1954 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
1956 /* Now stop the process. */
1957 if (!proc_stop_process (pi
))
1958 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
1959 pi
->ignore_next_sigstop
= 1;
1961 /* Save some of the /proc state to be restored if we detach. */
1962 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
1963 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
1964 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
1965 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
1966 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
1967 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
1969 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
1970 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
1972 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
1973 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
1975 fail
= procfs_debug_inferior (pi
);
1977 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
1979 inf
= current_inferior ();
1980 inferior_appeared (inf
, pi
->pid
);
1981 /* Let GDB know that the inferior was attached. */
1982 inf
->attach_flag
= 1;
1984 /* Create a procinfo for the current lwp. */
1985 lwpid
= proc_get_current_thread (pi
);
1986 create_procinfo (pi
->pid
, lwpid
);
1988 /* Add it to gdb's thread list. */
1989 ptid
= ptid_t (pi
->pid
, lwpid
, 0);
2000 /* Find procinfo for the main process. */
2001 pi
= find_procinfo_or_die (inferior_ptid
.pid (),
2002 0); /* FIXME: threads */
2004 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
2005 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
2007 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
2008 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
2010 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
2011 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
2013 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
2014 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
2016 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
2017 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
2019 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
2020 if (!(pi
->was_stopped
)
2021 || query (_("Was stopped when attached, make it runnable again? ")))
2023 /* Clear any pending signal. */
2024 if (!proc_clear_current_fault (pi
))
2025 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
2027 if (!proc_clear_current_signal (pi
))
2028 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
2030 if (!proc_set_run_on_last_close (pi
))
2031 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
2034 destroy_procinfo (pi
);
2037 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
2040 ??? Is the following note still relevant? We can't get individual
2041 registers with the PT_GETREGS ptrace(2) request either, yet we
2042 don't bother with caching at all in that case.
2044 NOTE: Since the /proc interface cannot give us individual
2045 registers, we pay no attention to REGNUM, and just fetch them all.
2046 This results in the possibility that we will do unnecessarily many
2047 fetches, since we may be called repeatedly for individual
2048 registers. So we cache the results, and mark the cache invalid
2049 when the process is resumed. */
2052 procfs_target::fetch_registers (struct regcache
*regcache
, int regnum
)
2054 gdb_gregset_t
*gregs
;
2056 ptid_t ptid
= regcache
->ptid ();
2057 int pid
= ptid
.pid ();
2058 int tid
= ptid
.lwp ();
2059 struct gdbarch
*gdbarch
= regcache
->arch ();
2061 pi
= find_procinfo_or_die (pid
, tid
);
2064 error (_("procfs: fetch_registers failed to find procinfo for %s"),
2065 target_pid_to_str (ptid
).c_str ());
2067 gregs
= proc_get_gregs (pi
);
2069 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
2071 supply_gregset (regcache
, (const gdb_gregset_t
*) gregs
);
2073 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
2075 gdb_fpregset_t
*fpregs
;
2077 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
2078 || regnum
== gdbarch_pc_regnum (gdbarch
)
2079 || regnum
== gdbarch_sp_regnum (gdbarch
))
2080 return; /* Not a floating point register. */
2082 fpregs
= proc_get_fpregs (pi
);
2084 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
2086 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregs
);
2090 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
2091 this for all registers.
2093 NOTE: Since the /proc interface will not read individual registers,
2094 we will cache these requests until the process is resumed, and only
2095 then write them back to the inferior process.
2097 FIXME: is that a really bad idea? Have to think about cases where
2098 writing one register might affect the value of others, etc. */
2101 procfs_target::store_registers (struct regcache
*regcache
, int regnum
)
2103 gdb_gregset_t
*gregs
;
2105 ptid_t ptid
= regcache
->ptid ();
2106 int pid
= ptid
.pid ();
2107 int tid
= ptid
.lwp ();
2108 struct gdbarch
*gdbarch
= regcache
->arch ();
2110 pi
= find_procinfo_or_die (pid
, tid
);
2113 error (_("procfs: store_registers: failed to find procinfo for %s"),
2114 target_pid_to_str (ptid
).c_str ());
2116 gregs
= proc_get_gregs (pi
);
2118 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
2120 fill_gregset (regcache
, gregs
, regnum
);
2121 if (!proc_set_gregs (pi
))
2122 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
2124 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
2126 gdb_fpregset_t
*fpregs
;
2128 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
2129 || regnum
== gdbarch_pc_regnum (gdbarch
)
2130 || regnum
== gdbarch_sp_regnum (gdbarch
))
2131 return; /* Not a floating point register. */
2133 fpregs
= proc_get_fpregs (pi
);
2135 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
2137 fill_fpregset (regcache
, fpregs
, regnum
);
2138 if (!proc_set_fpregs (pi
))
2139 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
2144 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
2146 if (scall
== SYS_lwp_exit
)
2152 syscall_is_exit (procinfo
*pi
, int scall
)
2154 if (scall
== SYS_exit
)
2160 syscall_is_exec (procinfo
*pi
, int scall
)
2163 if (scall
== SYS_exec
)
2166 if (scall
== SYS_execve
)
2172 syscall_is_lwp_create (procinfo
*pi
, int scall
)
2174 if (scall
== SYS_lwp_create
)
2179 /* Retrieve the next stop event from the child process. If child has
2180 not stopped yet, wait for it to stop. Translate /proc eventcodes
2181 (or possibly wait eventcodes) into gdb internal event codes.
2182 Returns the id of process (and possibly thread) that incurred the
2183 event. Event codes are returned through a pointer parameter. */
2186 procfs_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
2189 /* First cut: loosely based on original version 2.1. */
2193 ptid_t retval
, temp_ptid
;
2194 int why
, what
, flags
;
2201 retval
= ptid_t (-1);
2203 /* Find procinfo for main process. */
2204 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2207 /* We must assume that the status is stale now... */
2208 pi
->status_valid
= 0;
2209 pi
->gregs_valid
= 0;
2210 pi
->fpregs_valid
= 0;
2212 #if 0 /* just try this out... */
2213 flags
= proc_flags (pi
);
2214 why
= proc_why (pi
);
2215 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
2216 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
2218 /* If child is not stopped, wait for it to stop. */
2219 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
2220 && !proc_wait_for_stop (pi
))
2222 /* wait_for_stop failed: has the child terminated? */
2223 if (errno
== ENOENT
)
2227 /* /proc file not found; presumably child has terminated. */
2228 wait_retval
= ::wait (&wstat
); /* "wait" for the child's exit. */
2231 if (wait_retval
!= inferior_ptid
.pid ())
2232 error (_("procfs: couldn't stop "
2233 "process %d: wait returned %d."),
2234 inferior_ptid
.pid (), wait_retval
);
2235 /* FIXME: might I not just use waitpid?
2236 Or try find_procinfo to see if I know about this child? */
2237 retval
= ptid_t (wait_retval
);
2239 else if (errno
== EINTR
)
2243 /* Unknown error from wait_for_stop. */
2244 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
2249 /* This long block is reached if either:
2250 a) the child was already stopped, or
2251 b) we successfully waited for the child with wait_for_stop.
2252 This block will analyze the /proc status, and translate it
2253 into a waitstatus for GDB.
2255 If we actually had to call wait because the /proc file
2256 is gone (child terminated), then we skip this block,
2257 because we already have a waitstatus. */
2259 flags
= proc_flags (pi
);
2260 why
= proc_why (pi
);
2261 what
= proc_what (pi
);
2263 if (flags
& (PR_STOPPED
| PR_ISTOP
))
2265 /* If it's running async (for single_thread control),
2266 set it back to normal again. */
2267 if (flags
& PR_ASYNC
)
2268 if (!proc_unset_async (pi
))
2269 proc_error (pi
, "target_wait, unset_async", __LINE__
);
2272 proc_prettyprint_why (why
, what
, 1);
2274 /* The 'pid' we will return to GDB is composed of
2275 the process ID plus the lwp ID. */
2276 retval
= ptid_t (pi
->pid
, proc_get_current_thread (pi
), 0);
2280 wstat
= (what
<< 8) | 0177;
2283 if (syscall_is_lwp_exit (pi
, what
))
2285 if (print_thread_events
)
2286 printf_unfiltered (_("[%s exited]\n"),
2287 target_pid_to_str (retval
).c_str ());
2288 delete_thread (find_thread_ptid (retval
));
2289 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2292 else if (syscall_is_exit (pi
, what
))
2294 struct inferior
*inf
;
2296 /* Handle SYS_exit call only. */
2297 /* Stopped at entry to SYS_exit.
2298 Make it runnable, resume it, then use
2299 the wait system call to get its exit code.
2300 Proc_run_process always clears the current
2302 Then return its exit status. */
2303 pi
->status_valid
= 0;
2305 /* FIXME: what we should do is return
2306 TARGET_WAITKIND_SPURIOUS. */
2307 if (!proc_run_process (pi
, 0, 0))
2308 proc_error (pi
, "target_wait, run_process", __LINE__
);
2310 inf
= find_inferior_pid (pi
->pid
);
2311 if (inf
->attach_flag
)
2313 /* Don't call wait: simulate waiting for exit,
2314 return a "success" exit code. Bogus: what if
2315 it returns something else? */
2317 retval
= inferior_ptid
; /* ? ? ? */
2321 int temp
= ::wait (&wstat
);
2323 /* FIXME: shouldn't I make sure I get the right
2324 event from the right process? If (for
2325 instance) I have killed an earlier inferior
2326 process but failed to clean up after it
2327 somehow, I could get its termination event
2330 /* If wait returns -1, that's what we return
2333 retval
= ptid_t (temp
);
2338 printf_filtered (_("procfs: trapped on entry to "));
2339 proc_prettyprint_syscall (proc_what (pi
), 0);
2340 printf_filtered ("\n");
2342 long i
, nsysargs
, *sysargs
;
2344 nsysargs
= proc_nsysarg (pi
);
2345 sysargs
= proc_sysargs (pi
);
2347 if (nsysargs
> 0 && sysargs
!= NULL
)
2349 printf_filtered (_("%ld syscall arguments:\n"),
2351 for (i
= 0; i
< nsysargs
; i
++)
2352 printf_filtered ("#%ld: 0x%08lx\n",
2358 /* How to exit gracefully, returning "unknown
2360 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2361 return inferior_ptid
;
2365 /* How to keep going without returning to wfi: */
2366 target_continue_no_signal (ptid
);
2372 if (syscall_is_exec (pi
, what
))
2374 /* Hopefully this is our own "fork-child" execing
2375 the real child. Hoax this event into a trap, and
2376 GDB will see the child about to execute its start
2378 wstat
= (SIGTRAP
<< 8) | 0177;
2380 else if (syscall_is_lwp_create (pi
, what
))
2382 /* This syscall is somewhat like fork/exec. We
2383 will get the event twice: once for the parent
2384 LWP, and once for the child. We should already
2385 know about the parent LWP, but the child will
2386 be new to us. So, whenever we get this event,
2387 if it represents a new thread, simply add the
2388 thread to the list. */
2390 /* If not in procinfo list, add it. */
2391 temp_tid
= proc_get_current_thread (pi
);
2392 if (!find_procinfo (pi
->pid
, temp_tid
))
2393 create_procinfo (pi
->pid
, temp_tid
);
2395 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2396 /* If not in GDB's thread list, add it. */
2397 if (!in_thread_list (temp_ptid
))
2398 add_thread (temp_ptid
);
2400 /* Return to WFI, but tell it to immediately resume. */
2401 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2402 return inferior_ptid
;
2404 else if (syscall_is_lwp_exit (pi
, what
))
2406 if (print_thread_events
)
2407 printf_unfiltered (_("[%s exited]\n"),
2408 target_pid_to_str (retval
).c_str ());
2409 delete_thread (find_thread_ptid (retval
));
2410 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2415 /* FIXME: Do we need to handle SYS_sproc,
2416 SYS_fork, or SYS_vfork here? The old procfs
2417 seemed to use this event to handle threads on
2418 older (non-LWP) systems, where I'm assuming
2419 that threads were actually separate processes.
2420 Irix, maybe? Anyway, low priority for now. */
2424 printf_filtered (_("procfs: trapped on exit from "));
2425 proc_prettyprint_syscall (proc_what (pi
), 0);
2426 printf_filtered ("\n");
2428 long i
, nsysargs
, *sysargs
;
2430 nsysargs
= proc_nsysarg (pi
);
2431 sysargs
= proc_sysargs (pi
);
2433 if (nsysargs
> 0 && sysargs
!= NULL
)
2435 printf_filtered (_("%ld syscall arguments:\n"),
2437 for (i
= 0; i
< nsysargs
; i
++)
2438 printf_filtered ("#%ld: 0x%08lx\n",
2442 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2443 return inferior_ptid
;
2448 wstat
= (SIGSTOP
<< 8) | 0177;
2453 printf_filtered (_("Retry #%d:\n"), retry
);
2454 pi
->status_valid
= 0;
2459 /* If not in procinfo list, add it. */
2460 temp_tid
= proc_get_current_thread (pi
);
2461 if (!find_procinfo (pi
->pid
, temp_tid
))
2462 create_procinfo (pi
->pid
, temp_tid
);
2464 /* If not in GDB's thread list, add it. */
2465 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2466 if (!in_thread_list (temp_ptid
))
2467 add_thread (temp_ptid
);
2469 status
->kind
= TARGET_WAITKIND_STOPPED
;
2470 status
->value
.sig
= GDB_SIGNAL_0
;
2475 wstat
= (what
<< 8) | 0177;
2479 int signo
= pi
->prstatus
.pr_lwp
.pr_info
.si_signo
;
2481 wstat
= (signo
<< 8) | 0177;
2484 default: /* switch (why) unmatched */
2485 printf_filtered ("procfs:%d -- ", __LINE__
);
2486 printf_filtered (_("child stopped for unknown reason:\n"));
2487 proc_prettyprint_why (why
, what
, 1);
2488 error (_("... giving up..."));
2491 /* Got this far without error: If retval isn't in the
2492 threads database, add it. */
2493 if (retval
.pid () > 0
2494 && retval
!= inferior_ptid
2495 && !in_thread_list (retval
))
2497 /* We have a new thread. We need to add it both to
2498 GDB's list and to our own. If we don't create a
2499 procinfo, resume may be unhappy later. */
2500 add_thread (retval
);
2501 if (find_procinfo (retval
.pid (),
2502 retval
.lwp ()) == NULL
)
2503 create_procinfo (retval
.pid (),
2507 else /* Flags do not indicate STOPPED. */
2509 /* surely this can't happen... */
2510 printf_filtered ("procfs:%d -- process not stopped.\n",
2512 proc_prettyprint_flags (flags
, 1);
2513 error (_("procfs: ...giving up..."));
2518 store_waitstatus (status
, wstat
);
2524 /* Perform a partial transfer to/from the specified object. For
2525 memory transfers, fall back to the old memory xfer functions. */
2527 enum target_xfer_status
2528 procfs_target::xfer_partial (enum target_object object
,
2529 const char *annex
, gdb_byte
*readbuf
,
2530 const gdb_byte
*writebuf
, ULONGEST offset
,
2531 ULONGEST len
, ULONGEST
*xfered_len
)
2535 case TARGET_OBJECT_MEMORY
:
2536 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
2538 case TARGET_OBJECT_AUXV
:
2539 return memory_xfer_auxv (this, object
, annex
, readbuf
, writebuf
,
2540 offset
, len
, xfered_len
);
2543 return this->beneath ()->xfer_partial (object
, annex
,
2544 readbuf
, writebuf
, offset
, len
,
2549 /* Helper for procfs_xfer_partial that handles memory transfers.
2550 Arguments are like target_xfer_partial. */
2552 static enum target_xfer_status
2553 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
2554 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
2559 /* Find procinfo for main process. */
2560 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2561 if (pi
->as_fd
== 0 && open_procinfo_files (pi
, FD_AS
) == 0)
2563 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
2564 return TARGET_XFER_E_IO
;
2567 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
2568 return TARGET_XFER_E_IO
;
2570 if (writebuf
!= NULL
)
2572 PROCFS_NOTE ("write memory:\n");
2573 nbytes
= write (pi
->as_fd
, writebuf
, len
);
2577 PROCFS_NOTE ("read memory:\n");
2578 nbytes
= read (pi
->as_fd
, readbuf
, len
);
2581 return TARGET_XFER_E_IO
;
2582 *xfered_len
= nbytes
;
2583 return TARGET_XFER_OK
;
2586 /* Called by target_resume before making child runnable. Mark cached
2587 registers and status's invalid. If there are "dirty" caches that
2588 need to be written back to the child process, do that.
2590 File descriptors are also cached. As they are a limited resource,
2591 we cannot hold onto them indefinitely. However, as they are
2592 expensive to open, we don't want to throw them away
2593 indiscriminately either. As a compromise, we will keep the file
2594 descriptors for the parent process, but discard any file
2595 descriptors we may have accumulated for the threads.
2597 As this function is called by iterate_over_threads, it always
2598 returns zero (so that iterate_over_threads will keep
2602 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
2604 /* About to run the child; invalidate caches and do any other
2608 if (pi
->gregs_dirty
)
2609 if (parent
== NULL
|| proc_get_current_thread (parent
) != pi
->tid
)
2610 if (!proc_set_gregs (pi
)) /* flush gregs cache */
2611 proc_warn (pi
, "target_resume, set_gregs",
2613 if (gdbarch_fp0_regnum (target_gdbarch ()) >= 0)
2614 if (pi
->fpregs_dirty
)
2615 if (parent
== NULL
|| proc_get_current_thread (parent
) != pi
->tid
)
2616 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
2617 proc_warn (pi
, "target_resume, set_fpregs",
2623 /* The presence of a parent indicates that this is an LWP.
2624 Close any file descriptors that it might have open.
2625 We don't do this to the master (parent) procinfo. */
2627 close_procinfo_files (pi
);
2629 pi
->gregs_valid
= 0;
2630 pi
->fpregs_valid
= 0;
2632 pi
->gregs_dirty
= 0;
2633 pi
->fpregs_dirty
= 0;
2635 pi
->status_valid
= 0;
2636 pi
->threads_valid
= 0;
2642 /* A callback function for iterate_over_threads. Find the
2643 asynchronous signal thread, and make it runnable. See if that
2644 helps matters any. */
2647 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
2650 if (proc_flags (pi
) & PR_ASLWP
)
2652 if (!proc_run_process (pi
, 0, -1))
2653 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
2661 /* Make the child process runnable. Normally we will then call
2662 procfs_wait and wait for it to stop again (unless gdb is async).
2664 If STEP is true, then arrange for the child to stop again after
2665 executing a single instruction. If SIGNO is zero, then cancel any
2666 pending signal; if non-zero, then arrange for the indicated signal
2667 to be delivered to the child when it runs. If PID is -1, then
2668 allow any child thread to run; if non-zero, then allow only the
2669 indicated thread to run. (not implemented yet). */
2672 procfs_target::resume (ptid_t ptid
, int step
, enum gdb_signal signo
)
2674 procinfo
*pi
, *thread
;
2678 prrun.prflags |= PRSVADDR;
2679 prrun.pr_vaddr = $PC; set resume address
2680 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
2681 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
2682 prrun.prflags |= PRCFAULT; clear current fault.
2684 PRSTRACE and PRSFAULT can be done by other means
2685 (proc_trace_signals, proc_trace_faults)
2686 PRSVADDR is unnecessary.
2687 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
2688 This basically leaves PRSTEP and PRCSIG.
2689 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
2690 So basically PR_STEP is the sole argument that must be passed
2691 to proc_run_process (for use in the prrun struct by ioctl). */
2693 /* Find procinfo for main process. */
2694 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2696 /* First cut: ignore pid argument. */
2699 /* Convert signal to host numbering. */
2700 if (signo
== 0 || (signo
== GDB_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
2703 native_signo
= gdb_signal_to_host (signo
);
2705 pi
->ignore_next_sigstop
= 0;
2707 /* Running the process voids all cached registers and status. */
2708 /* Void the threads' caches first. */
2709 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
2710 /* Void the process procinfo's caches. */
2711 invalidate_cache (NULL
, pi
, NULL
);
2713 if (ptid
.pid () != -1)
2715 /* Resume a specific thread, presumably suppressing the
2717 thread
= find_procinfo (ptid
.pid (), ptid
.lwp ());
2720 if (thread
->tid
!= 0)
2722 /* We're to resume a specific thread, and not the
2723 others. Set the child process's PR_ASYNC flag. */
2724 if (!proc_set_async (pi
))
2725 proc_error (pi
, "target_resume, set_async", __LINE__
);
2727 proc_iterate_over_threads (pi
,
2728 make_signal_thread_runnable
,
2731 pi
= thread
; /* Substitute the thread's procinfo
2737 if (!proc_run_process (pi
, step
, native_signo
))
2740 warning (_("resume: target already running. "
2741 "Pretend to resume, and hope for the best!"));
2743 proc_error (pi
, "target_resume", __LINE__
);
2747 /* Set up to trace signals in the child process. */
2750 procfs_target::pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2753 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2756 prfillset (&signals
);
2758 for (signo
= 0; signo
< NSIG
; signo
++)
2760 int target_signo
= gdb_signal_from_host (signo
);
2761 if (target_signo
< pass_signals
.size () && pass_signals
[target_signo
])
2762 prdelset (&signals
, signo
);
2765 if (!proc_set_traced_signals (pi
, &signals
))
2766 proc_error (pi
, "pass_signals", __LINE__
);
2769 /* Print status information about the child process. */
2772 procfs_target::files_info ()
2774 struct inferior
*inf
= current_inferior ();
2776 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
2777 inf
->attach_flag
? "attached": "child",
2778 target_pid_to_str (inferior_ptid
).c_str ());
2781 /* Make it die. Wait for it to die. Clean up after it. Note: this
2782 should only be applied to the real process, not to an LWP, because
2783 of the check for parent-process. If we need this to work for an
2784 LWP, it needs some more logic. */
2787 unconditionally_kill_inferior (procinfo
*pi
)
2791 parent_pid
= proc_parent_pid (pi
);
2792 if (!proc_kill (pi
, SIGKILL
))
2793 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
2794 destroy_procinfo (pi
);
2796 /* If pi is GDB's child, wait for it to die. */
2797 if (parent_pid
== getpid ())
2798 /* FIXME: should we use waitpid to make sure we get the right event?
2799 Should we check the returned event? */
2804 ret
= waitpid (pi
->pid
, &status
, 0);
2811 /* We're done debugging it, and we want it to go away. Then we want
2812 GDB to forget all about it. */
2815 procfs_target::kill ()
2817 if (inferior_ptid
!= null_ptid
) /* ? */
2819 /* Find procinfo for main process. */
2820 procinfo
*pi
= find_procinfo (inferior_ptid
.pid (), 0);
2823 unconditionally_kill_inferior (pi
);
2824 target_mourn_inferior (inferior_ptid
);
2828 /* Forget we ever debugged this thing! */
2831 procfs_target::mourn_inferior ()
2835 if (inferior_ptid
!= null_ptid
)
2837 /* Find procinfo for main process. */
2838 pi
= find_procinfo (inferior_ptid
.pid (), 0);
2840 destroy_procinfo (pi
);
2843 generic_mourn_inferior ();
2845 maybe_unpush_target ();
2848 /* When GDB forks to create a runnable inferior process, this function
2849 is called on the parent side of the fork. It's job is to do
2850 whatever is necessary to make the child ready to be debugged, and
2851 then wait for the child to synchronize. */
2854 procfs_init_inferior (struct target_ops
*ops
, int pid
)
2860 /* This routine called on the parent side (GDB side)
2861 after GDB forks the inferior. */
2862 if (!target_is_pushed (ops
))
2865 pi
= create_procinfo (pid
, 0);
2867 perror (_("procfs: out of memory in 'init_inferior'"));
2869 if (!open_procinfo_files (pi
, FD_CTL
))
2870 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
2874 open_procinfo_files // done
2877 procfs_notice_signals
2884 /* If not stopped yet, wait for it to stop. */
2885 if (!(proc_flags (pi
) & PR_STOPPED
) && !(proc_wait_for_stop (pi
)))
2886 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
2888 /* Save some of the /proc state to be restored if we detach. */
2889 /* FIXME: Why? In case another debugger was debugging it?
2890 We're it's parent, for Ghu's sake! */
2891 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
2892 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
2893 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
2894 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
2895 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
2896 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
2897 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
2898 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
2899 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
2900 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
2902 fail
= procfs_debug_inferior (pi
);
2904 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
2906 /* FIXME: logically, we should really be turning OFF run-on-last-close,
2907 and possibly even turning ON kill-on-last-close at this point. But
2908 I can't make that change without careful testing which I don't have
2909 time to do right now... */
2910 /* Turn on run-on-last-close flag so that the child
2911 will die if GDB goes away for some reason. */
2912 if (!proc_set_run_on_last_close (pi
))
2913 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
2915 /* We now have have access to the lwpid of the main thread/lwp. */
2916 lwpid
= proc_get_current_thread (pi
);
2918 /* Create a procinfo for the main lwp. */
2919 create_procinfo (pid
, lwpid
);
2921 /* We already have a main thread registered in the thread table at
2922 this point, but it didn't have any lwp info yet. Notify the core
2923 about it. This changes inferior_ptid as well. */
2924 thread_change_ptid (ptid_t (pid
),
2925 ptid_t (pid
, lwpid
, 0));
2927 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
2930 /* When GDB forks to create a new process, this function is called on
2931 the child side of the fork before GDB exec's the user program. Its
2932 job is to make the child minimally debuggable, so that the parent
2933 GDB process can connect to the child and take over. This function
2934 should do only the minimum to make that possible, and to
2935 synchronize with the parent process. The parent process should
2936 take care of the details. */
2939 procfs_set_exec_trap (void)
2941 /* This routine called on the child side (inferior side)
2942 after GDB forks the inferior. It must use only local variables,
2943 because it may be sharing data space with its parent. */
2948 pi
= create_procinfo (getpid (), 0);
2950 perror_with_name (_("procfs: create_procinfo failed in child."));
2952 if (open_procinfo_files (pi
, FD_CTL
) == 0)
2954 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
2955 gdb_flush (gdb_stderr
);
2956 /* No need to call "dead_procinfo", because we're going to
2961 /* Method for tracing exec syscalls. */
2963 Not all systems with /proc have all the exec* syscalls with the same
2964 names. On the SGI, for example, there is no SYS_exec, but there
2965 *is* a SYS_execv. So, we try to account for that. */
2967 exitset
= XNEW (sysset_t
);
2968 premptyset (exitset
);
2970 praddset (exitset
, SYS_exec
);
2972 praddset (exitset
, SYS_execve
);
2974 if (!proc_set_traced_sysexit (pi
, exitset
))
2976 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
2977 gdb_flush (gdb_stderr
);
2981 /* FIXME: should this be done in the parent instead? */
2982 /* Turn off inherit on fork flag so that all grand-children
2983 of gdb start with tracing flags cleared. */
2984 if (!proc_unset_inherit_on_fork (pi
))
2985 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
2987 /* Turn off run on last close flag, so that the child process
2988 cannot run away just because we close our handle on it.
2989 We want it to wait for the parent to attach. */
2990 if (!proc_unset_run_on_last_close (pi
))
2991 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
2993 /* FIXME: No need to destroy the procinfo --
2994 we have our own address space, and we're about to do an exec! */
2995 /*destroy_procinfo (pi);*/
2998 /* This function is called BEFORE gdb forks the inferior process. Its
2999 only real responsibility is to set things up for the fork, and tell
3000 GDB which two functions to call after the fork (one for the parent,
3001 and one for the child).
3003 This function does a complicated search for a unix shell program,
3004 which it then uses to parse arguments and environment variables to
3005 be sent to the child. I wonder whether this code could not be
3006 abstracted out and shared with other unix targets such as
3010 procfs_target::create_inferior (const char *exec_file
,
3011 const std::string
&allargs
,
3012 char **env
, int from_tty
)
3014 const char *shell_file
= get_shell ();
3018 if (strchr (shell_file
, '/') == NULL
)
3021 /* We will be looking down the PATH to find shell_file. If we
3022 just do this the normal way (via execlp, which operates by
3023 attempting an exec for each element of the PATH until it
3024 finds one which succeeds), then there will be an exec for
3025 each failed attempt, each of which will cause a PR_SYSEXIT
3026 stop, and we won't know how to distinguish the PR_SYSEXIT's
3027 for these failed execs with the ones for successful execs
3028 (whether the exec has succeeded is stored at that time in the
3029 carry bit or some such architecture-specific and
3030 non-ABI-specified place).
3032 So I can't think of anything better than to search the PATH
3033 now. This has several disadvantages: (1) There is a race
3034 condition; if we find a file now and it is deleted before we
3035 exec it, we lose, even if the deletion leaves a valid file
3036 further down in the PATH, (2) there is no way to know exactly
3037 what an executable (in the sense of "capable of being
3038 exec'd") file is. Using access() loses because it may lose
3039 if the caller is the superuser; failing to use it loses if
3040 there are ACLs or some such. */
3044 /* FIXME-maybe: might want "set path" command so user can change what
3045 path is used from within GDB. */
3046 const char *path
= getenv ("PATH");
3048 struct stat statbuf
;
3051 path
= "/bin:/usr/bin";
3053 tryname
= (char *) alloca (strlen (path
) + strlen (shell_file
) + 2);
3054 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
3056 p1
= strchr (p
, ':');
3061 strncpy (tryname
, p
, len
);
3062 tryname
[len
] = '\0';
3063 strcat (tryname
, "/");
3064 strcat (tryname
, shell_file
);
3065 if (access (tryname
, X_OK
) < 0)
3067 if (stat (tryname
, &statbuf
) < 0)
3069 if (!S_ISREG (statbuf
.st_mode
))
3070 /* We certainly need to reject directories. I'm not quite
3071 as sure about FIFOs, sockets, etc., but I kind of doubt
3072 that people want to exec() these things. */
3077 /* Not found. This must be an error rather than merely passing
3078 the file to execlp(), because execlp() would try all the
3079 exec()s, causing GDB to get confused. */
3080 error (_("procfs:%d -- Can't find shell %s in PATH"),
3081 __LINE__
, shell_file
);
3083 shell_file
= tryname
;
3086 pid
= fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
3087 NULL
, NULL
, shell_file
, NULL
);
3089 /* We have something that executes now. We'll be running through
3090 the shell at this point (if startup-with-shell is true), but the
3091 pid shouldn't change. */
3092 add_thread_silent (ptid_t (pid
));
3094 procfs_init_inferior (this, pid
);
3097 /* An observer for the "inferior_created" event. */
3100 procfs_inferior_created (struct target_ops
*ops
, int from_tty
)
3104 /* Callback for update_thread_list. Calls "add_thread". */
3107 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
3109 ptid_t gdb_threadid
= ptid_t (pi
->pid
, thread
->tid
, 0);
3111 thread_info
*thr
= find_thread_ptid (gdb_threadid
);
3112 if (thr
== NULL
|| thr
->state
== THREAD_EXITED
)
3113 add_thread (gdb_threadid
);
3118 /* Query all the threads that the target knows about, and give them
3119 back to GDB to add to its list. */
3122 procfs_target::update_thread_list ()
3128 /* Find procinfo for main process. */
3129 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3130 proc_update_threads (pi
);
3131 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
3134 /* Return true if the thread is still 'alive'. This guy doesn't
3135 really seem to be doing his job. Got to investigate how to tell
3136 when a thread is really gone. */
3139 procfs_target::thread_alive (ptid_t ptid
)
3145 thread
= ptid
.lwp ();
3146 /* If I don't know it, it ain't alive! */
3147 pi
= find_procinfo (proc
, thread
);
3151 /* If I can't get its status, it ain't alive!
3152 What's more, I need to forget about it! */
3153 if (!proc_get_status (pi
))
3155 destroy_procinfo (pi
);
3158 /* I couldn't have got its status if it weren't alive, so it's
3163 /* Convert PTID to a string. */
3166 procfs_target::pid_to_str (ptid_t ptid
)
3168 if (ptid
.lwp () == 0)
3169 return string_printf ("process %d", ptid
.pid ());
3171 return string_printf ("LWP %ld", ptid
.lwp ());
3174 /* Accepts an integer PID; Returns a string representing a file that
3175 can be opened to get the symbols for the child process. */
3178 procfs_target::pid_to_exec_file (int pid
)
3180 static char buf
[PATH_MAX
];
3181 char name
[PATH_MAX
];
3183 /* Solaris 11 introduced /proc/<proc-id>/execname. */
3184 xsnprintf (name
, sizeof (name
), "/proc/%d/execname", pid
);
3185 scoped_fd
fd (gdb_open_cloexec (name
, O_RDONLY
, 0));
3186 if (fd
.get () < 0 || read (fd
.get (), buf
, PATH_MAX
- 1) < 0)
3188 /* If that fails, fall back to /proc/<proc-id>/path/a.out introduced in
3192 xsnprintf (name
, sizeof (name
), "/proc/%d/path/a.out", pid
);
3193 len
= readlink (name
, buf
, PATH_MAX
- 1);
3203 /* Insert a watchpoint. */
3206 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
3212 pi
= find_procinfo_or_die (ptid
.pid () == -1 ?
3213 inferior_ptid
.pid () : ptid
.pid (),
3216 /* Translate from GDB's flags to /proc's. */
3217 if (len
> 0) /* len == 0 means delete watchpoint. */
3219 switch (rwflag
) { /* FIXME: need an enum! */
3220 case hw_write
: /* default watchpoint (write) */
3221 pflags
= WRITE_WATCHFLAG
;
3223 case hw_read
: /* read watchpoint */
3224 pflags
= READ_WATCHFLAG
;
3226 case hw_access
: /* access watchpoint */
3227 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
3229 case hw_execute
: /* execution HW breakpoint */
3230 pflags
= EXEC_WATCHFLAG
;
3232 default: /* Something weird. Return error. */
3235 if (after
) /* Stop after r/w access is completed. */
3236 pflags
|= AFTER_WATCHFLAG
;
3239 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
3241 if (errno
== E2BIG
) /* Typical error for no resources. */
3242 return -1; /* fail */
3243 /* GDB may try to remove the same watchpoint twice.
3244 If a remove request returns no match, don't error. */
3245 if (errno
== ESRCH
&& len
== 0)
3246 return 0; /* ignore */
3247 proc_error (pi
, "set_watchpoint", __LINE__
);
3252 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
3253 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
3254 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
3257 Note: procfs_can_use_hw_breakpoint() is not yet used by all
3258 procfs.c targets due to the fact that some of them still define
3259 target_can_use_hardware_watchpoint. */
3262 procfs_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int othertype
)
3264 /* Due to the way that proc_set_watchpoint() is implemented, host
3265 and target pointers must be of the same size. If they are not,
3266 we can't use hardware watchpoints. This limitation is due to the
3267 fact that proc_set_watchpoint() calls
3268 procfs_address_to_host_pointer(); a close inspection of
3269 procfs_address_to_host_pointer will reveal that an internal error
3270 will be generated when the host and target pointer sizes are
3272 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
3274 if (sizeof (void *) != TYPE_LENGTH (ptr_type
))
3277 /* Other tests here??? */
3282 /* Returns non-zero if process is stopped on a hardware watchpoint
3283 fault, else returns zero. */
3286 procfs_target::stopped_by_watchpoint ()
3290 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3292 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3293 if (proc_why (pi
) == PR_FAULTED
)
3294 if (proc_what (pi
) == FLTWATCH
)
3299 /* Returns 1 if the OS knows the position of the triggered watchpoint,
3300 and sets *ADDR to that address. Returns 0 if OS cannot report that
3301 address. This function is only called if
3302 procfs_stopped_by_watchpoint returned 1, thus no further checks are
3303 done. The function also assumes that ADDR is not NULL. */
3306 procfs_target::stopped_data_address (CORE_ADDR
*addr
)
3310 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3311 return proc_watchpoint_address (pi
, addr
);
3315 procfs_target::insert_watchpoint (CORE_ADDR addr
, int len
,
3316 enum target_hw_bp_type type
,
3317 struct expression
*cond
)
3319 if (!target_have_steppable_watchpoint
3320 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
3321 /* When a hardware watchpoint fires off the PC will be left at
3322 the instruction following the one which caused the
3323 watchpoint. It will *NOT* be necessary for GDB to step over
3325 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 1);
3327 /* When a hardware watchpoint fires off the PC will be left at
3328 the instruction which caused the watchpoint. It will be
3329 necessary for GDB to step over the watchpoint. */
3330 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 0);
3334 procfs_target::remove_watchpoint (CORE_ADDR addr
, int len
,
3335 enum target_hw_bp_type type
,
3336 struct expression
*cond
)
3338 return procfs_set_watchpoint (inferior_ptid
, addr
, 0, 0, 0);
3342 procfs_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
3344 /* The man page for proc(4) on Solaris 2.6 and up says that the
3345 system can support "thousands" of hardware watchpoints, but gives
3346 no method for finding out how many; It doesn't say anything about
3347 the allowed size for the watched area either. So we just tell
3352 /* Memory Mappings Functions: */
3354 /* Call a callback function once for each mapping, passing it the
3355 mapping, an optional secondary callback function, and some optional
3356 opaque data. Quit and return the first non-zero value returned
3359 PI is the procinfo struct for the process to be mapped. FUNC is
3360 the callback function to be called by this iterator. DATA is the
3361 optional opaque data to be passed to the callback function.
3362 CHILD_FUNC is the optional secondary function pointer to be passed
3363 to the child function. Returns the first non-zero return value
3364 from the callback function, or zero. */
3367 iterate_over_mappings (procinfo
*pi
, find_memory_region_ftype child_func
,
3369 int (*func
) (struct prmap
*map
,
3370 find_memory_region_ftype child_func
,
3373 char pathname
[MAX_PROC_NAME_SIZE
];
3374 struct prmap
*prmaps
;
3375 struct prmap
*prmap
;
3380 /* Get the number of mappings, allocate space,
3381 and read the mappings into prmaps. */
3383 xsnprintf (pathname
, sizeof (pathname
), "/proc/%d/map", pi
->pid
);
3385 scoped_fd
map_fd (open (pathname
, O_RDONLY
));
3386 if (map_fd
.get () < 0)
3387 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
3389 /* Use stat to determine the file size, and compute
3390 the number of prmap_t objects it contains. */
3391 if (fstat (map_fd
.get (), &sbuf
) != 0)
3392 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
3394 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
3395 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
3396 if (read (map_fd
.get (), (char *) prmaps
, nmap
* sizeof (*prmaps
))
3397 != (nmap
* sizeof (*prmaps
)))
3398 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
3400 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
3402 funcstat
= (*func
) (prmap
, child_func
, data
);
3410 /* Implements the to_find_memory_regions method. Calls an external
3411 function for each memory region.
3412 Returns the integer value returned by the callback. */
3415 find_memory_regions_callback (struct prmap
*map
,
3416 find_memory_region_ftype func
, void *data
)
3418 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
3420 (map
->pr_mflags
& MA_READ
) != 0,
3421 (map
->pr_mflags
& MA_WRITE
) != 0,
3422 (map
->pr_mflags
& MA_EXEC
) != 0,
3423 1, /* MODIFIED is unknown, pass it as true. */
3427 /* External interface. Calls a callback function once for each
3428 mapped memory region in the child process, passing as arguments:
3430 CORE_ADDR virtual_address,
3432 int read, TRUE if region is readable by the child
3433 int write, TRUE if region is writable by the child
3434 int execute TRUE if region is executable by the child.
3436 Stops iterating and returns the first non-zero value returned by
3440 procfs_target::find_memory_regions (find_memory_region_ftype func
, void *data
)
3442 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3444 return iterate_over_mappings (pi
, func
, data
,
3445 find_memory_regions_callback
);
3448 /* Returns an ascii representation of a memory mapping's flags. */
3451 mappingflags (long flags
)
3453 static char asciiflags
[8];
3455 strcpy (asciiflags
, "-------");
3456 if (flags
& MA_STACK
)
3457 asciiflags
[1] = 's';
3458 if (flags
& MA_BREAK
)
3459 asciiflags
[2] = 'b';
3460 if (flags
& MA_SHARED
)
3461 asciiflags
[3] = 's';
3462 if (flags
& MA_READ
)
3463 asciiflags
[4] = 'r';
3464 if (flags
& MA_WRITE
)
3465 asciiflags
[5] = 'w';
3466 if (flags
& MA_EXEC
)
3467 asciiflags
[6] = 'x';
3468 return (asciiflags
);
3471 /* Callback function, does the actual work for 'info proc
3475 info_mappings_callback (struct prmap
*map
, find_memory_region_ftype ignore
,
3478 unsigned int pr_off
;
3480 pr_off
= (unsigned int) map
->pr_offset
;
3482 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
3483 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
3484 (unsigned long) map
->pr_vaddr
,
3485 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3486 (unsigned long) map
->pr_size
,
3488 mappingflags (map
->pr_mflags
));
3490 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
3491 (unsigned long) map
->pr_vaddr
,
3492 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3493 (unsigned long) map
->pr_size
,
3495 mappingflags (map
->pr_mflags
));
3500 /* Implement the "info proc mappings" subcommand. */
3503 info_proc_mappings (procinfo
*pi
, int summary
)
3506 return; /* No output for summary mode. */
3508 printf_filtered (_("Mapped address spaces:\n\n"));
3509 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
3510 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3517 printf_filtered (" %18s %18s %10s %10s %7s\n",
3524 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
3525 printf_filtered ("\n");
3528 /* Implement the "info proc" command. */
3531 procfs_target::info_proc (const char *args
, enum info_proc_what what
)
3533 procinfo
*process
= NULL
;
3534 procinfo
*thread
= NULL
;
3551 error (_("Not supported on this target."));
3554 gdb_argv
built_argv (args
);
3555 for (char *arg
: built_argv
)
3557 if (isdigit (arg
[0]))
3559 pid
= strtoul (arg
, &tmp
, 10);
3561 tid
= strtoul (++tmp
, NULL
, 10);
3563 else if (arg
[0] == '/')
3565 tid
= strtoul (arg
+ 1, NULL
, 10);
3569 procinfo_up temporary_procinfo
;
3571 pid
= inferior_ptid
.pid ();
3573 error (_("No current process: you must name one."));
3576 /* Have pid, will travel.
3577 First see if it's a process we're already debugging. */
3578 process
= find_procinfo (pid
, 0);
3579 if (process
== NULL
)
3581 /* No. So open a procinfo for it, but
3582 remember to close it again when finished. */
3583 process
= create_procinfo (pid
, 0);
3584 temporary_procinfo
.reset (process
);
3585 if (!open_procinfo_files (process
, FD_CTL
))
3586 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
3590 thread
= create_procinfo (pid
, tid
);
3594 printf_filtered (_("process %d flags:\n"), process
->pid
);
3595 proc_prettyprint_flags (proc_flags (process
), 1);
3596 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
3597 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
3598 if (proc_get_nthreads (process
) > 1)
3599 printf_filtered ("Process has %d threads.\n",
3600 proc_get_nthreads (process
));
3604 printf_filtered (_("thread %d flags:\n"), thread
->tid
);
3605 proc_prettyprint_flags (proc_flags (thread
), 1);
3606 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
3607 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
3611 info_proc_mappings (process
, 0);
3616 /* Modify the status of the system call identified by SYSCALLNUM in
3617 the set of syscalls that are currently traced/debugged.
3619 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
3620 will be updated. Otherwise, the exit syscalls set will be updated.
3622 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
3623 will be disabled. */
3626 proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
, int entry_or_exit
,
3627 int mode
, int from_tty
)
3631 if (entry_or_exit
== PR_SYSENTRY
)
3632 sysset
= proc_get_traced_sysentry (pi
, NULL
);
3634 sysset
= proc_get_traced_sysexit (pi
, NULL
);
3637 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
3639 if (mode
== FLAG_SET
)
3640 praddset (sysset
, syscallnum
);
3642 prdelset (sysset
, syscallnum
);
3644 if (entry_or_exit
== PR_SYSENTRY
)
3646 if (!proc_set_traced_sysentry (pi
, sysset
))
3647 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
3651 if (!proc_set_traced_sysexit (pi
, sysset
))
3652 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
3657 proc_trace_syscalls (const char *args
, int from_tty
, int entry_or_exit
, int mode
)
3661 if (inferior_ptid
.pid () <= 0)
3662 error (_("you must be debugging a process to use this command."));
3664 if (args
== NULL
|| args
[0] == 0)
3665 error_no_arg (_("system call to trace"));
3667 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3668 if (isdigit (args
[0]))
3670 const int syscallnum
= atoi (args
);
3672 proc_trace_syscalls_1 (pi
, syscallnum
, entry_or_exit
, mode
, from_tty
);
3677 proc_trace_sysentry_cmd (const char *args
, int from_tty
)
3679 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
3683 proc_trace_sysexit_cmd (const char *args
, int from_tty
)
3685 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
3689 proc_untrace_sysentry_cmd (const char *args
, int from_tty
)
3691 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
3695 proc_untrace_sysexit_cmd (const char *args
, int from_tty
)
3697 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
3701 _initialize_procfs (void)
3703 gdb::observers::inferior_created
.attach (procfs_inferior_created
);
3705 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
3706 _("Give a trace of entries into the syscall."));
3707 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
3708 _("Give a trace of exits from the syscall."));
3709 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
3710 _("Cancel a trace of entries into the syscall."));
3711 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
3712 _("Cancel a trace of exits from the syscall."));
3714 add_inf_child_target (&the_procfs_target
);
3717 /* =================== END, GDB "MODULE" =================== */
3721 /* miscellaneous stubs: */
3723 /* The following satisfy a few random symbols mostly created by the
3724 solaris threads implementation, which I will chase down later. */
3726 /* Return a pid for which we guarantee we will be able to find a
3730 procfs_first_available (void)
3732 return ptid_t (procinfo_list
? procinfo_list
->pid
: -1);
3735 /* =================== GCORE .NOTE "MODULE" =================== */
3738 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3739 char *note_data
, int *note_size
,
3740 enum gdb_signal stop_signal
)
3742 struct regcache
*regcache
= get_thread_regcache (ptid
);
3743 gdb_gregset_t gregs
;
3744 gdb_fpregset_t fpregs
;
3745 unsigned long merged_pid
;
3747 merged_pid
= ptid
.lwp () << 16 | ptid
.pid ();
3749 /* This part is the old method for fetching registers.
3750 It should be replaced by the newer one using regsets
3751 once it is implemented in this platform:
3752 gdbarch_iterate_over_regset_sections(). */
3754 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
3755 inferior_ptid
= ptid
;
3756 target_fetch_registers (regcache
, -1);
3758 fill_gregset (regcache
, &gregs
, -1);
3759 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
3765 fill_fpregset (regcache
, &fpregs
, -1);
3766 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3775 struct procfs_corefile_thread_data
{
3779 enum gdb_signal stop_signal
;
3783 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
3785 struct procfs_corefile_thread_data
*args
3786 = (struct procfs_corefile_thread_data
*) data
;
3790 ptid_t ptid
= ptid_t (pi
->pid
, thread
->tid
, 0);
3792 args
->note_data
= procfs_do_thread_registers (args
->obfd
, ptid
,
3801 find_signalled_thread (struct thread_info
*info
, void *data
)
3803 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
3804 && info
->ptid
.pid () == inferior_ptid
.pid ())
3810 static enum gdb_signal
3811 find_stop_signal (void)
3813 struct thread_info
*info
=
3814 iterate_over_threads (find_signalled_thread
, NULL
);
3817 return info
->suspend
.stop_signal
;
3819 return GDB_SIGNAL_0
;
3823 procfs_target::make_corefile_notes (bfd
*obfd
, int *note_size
)
3825 gdb_gregset_t gregs
;
3826 char fname
[16] = {'\0'};
3827 char psargs
[80] = {'\0'};
3828 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3829 char *note_data
= NULL
;
3830 const char *inf_args
;
3831 struct procfs_corefile_thread_data thread_args
;
3832 enum gdb_signal stop_signal
;
3834 if (get_exec_file (0))
3836 strncpy (fname
, lbasename (get_exec_file (0)), sizeof (fname
));
3837 fname
[sizeof (fname
) - 1] = 0;
3838 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3839 psargs
[sizeof (psargs
) - 1] = 0;
3841 inf_args
= get_inferior_args ();
3842 if (inf_args
&& *inf_args
3843 && (strlen (inf_args
)
3844 < ((int) sizeof (psargs
) - (int) strlen (psargs
))))
3846 strncat (psargs
, " ",
3847 sizeof (psargs
) - strlen (psargs
));
3848 strncat (psargs
, inf_args
,
3849 sizeof (psargs
) - strlen (psargs
));
3853 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3859 stop_signal
= find_stop_signal ();
3861 fill_gregset (get_current_regcache (), &gregs
, -1);
3862 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
3863 inferior_ptid
.pid (),
3864 stop_signal
, &gregs
);
3866 thread_args
.obfd
= obfd
;
3867 thread_args
.note_data
= note_data
;
3868 thread_args
.note_size
= note_size
;
3869 thread_args
.stop_signal
= stop_signal
;
3870 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
,
3872 note_data
= thread_args
.note_data
;
3874 gdb::optional
<gdb::byte_vector
> auxv
=
3875 target_read_alloc (current_top_target (), TARGET_OBJECT_AUXV
, NULL
);
3876 if (auxv
&& !auxv
->empty ())
3877 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3878 "CORE", NT_AUXV
, auxv
->data (),
3883 /* =================== END GCORE .NOTE "MODULE" =================== */