1 /* Machine independent support for Solaris /proc (process file system) for GDB.
3 Copyright (C) 1999-2020 Free Software Foundation, Inc.
5 Written by Michael Snyder at Cygnus Solutions.
6 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "elf-bfd.h" /* for elfcore_write_* */
30 #include "gdbthread.h"
32 #include "inf-child.h"
33 #include "nat/fork-inferior.h"
36 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
38 #include <sys/procfs.h>
39 #include <sys/fault.h>
40 #include <sys/syscall.h>
41 #include "gdbsupport/gdb_wait.h"
48 #include "observable.h"
49 #include "gdbsupport/scoped_fd.h"
50 #include "gdbsupport/pathstuff.h"
52 /* This module provides the interface between GDB and the
53 /proc file system, which is used on many versions of Unix
54 as a means for debuggers to control other processes.
56 /proc works by imitating a file system: you open a simulated file
57 that represents the process you wish to interact with, and perform
58 operations on that "file" in order to examine or change the state
61 The most important thing to know about /proc and this module is
62 that there are two very different interfaces to /proc:
64 One that uses the ioctl system call, and another that uses read
65 and write system calls.
67 This module supports only the Solaris version of the read/write
70 #include <sys/types.h>
71 #include <dirent.h> /* opendir/readdir, for listing the LWP's */
73 #include <fcntl.h> /* for O_RDONLY */
74 #include <unistd.h> /* for "X_OK" */
75 #include <sys/stat.h> /* for struct stat */
77 /* Note: procfs-utils.h must be included after the above system header
78 files, because it redefines various system calls using macros.
79 This may be incompatible with the prototype declarations. */
81 #include "proc-utils.h"
83 /* Prototypes for supply_gregset etc. */
86 /* =================== TARGET_OPS "MODULE" =================== */
88 /* This module defines the GDB target vector and its methods. */
91 static enum target_xfer_status
procfs_xfer_memory (gdb_byte
*,
96 class procfs_target final
: public inf_child_target
99 void create_inferior (const char *, const std::string
&,
100 char **, int) override
;
102 void kill () override
;
104 void mourn_inferior () override
;
106 void attach (const char *, int) override
;
107 void detach (inferior
*inf
, int) override
;
109 void resume (ptid_t
, int, enum gdb_signal
) override
;
110 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
112 void fetch_registers (struct regcache
*, int) override
;
113 void store_registers (struct regcache
*, int) override
;
115 enum target_xfer_status
xfer_partial (enum target_object object
,
118 const gdb_byte
*writebuf
,
119 ULONGEST offset
, ULONGEST len
,
120 ULONGEST
*xfered_len
) override
;
122 void pass_signals (gdb::array_view
<const unsigned char>) override
;
124 void files_info () override
;
126 void update_thread_list () override
;
128 bool thread_alive (ptid_t ptid
) override
;
130 std::string
pid_to_str (ptid_t
) override
;
132 char *pid_to_exec_file (int pid
) override
;
134 thread_control_capabilities
get_thread_control_capabilities () override
135 { return tc_schedlock
; }
137 /* find_memory_regions support method for gcore */
138 int find_memory_regions (find_memory_region_ftype func
, void *data
)
141 char *make_corefile_notes (bfd
*, int *) override
;
143 bool info_proc (const char *, enum info_proc_what
) override
;
145 #if PR_MODEL_NATIVE == PR_MODEL_LP64
146 int auxv_parse (gdb_byte
**readptr
,
147 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
151 bool stopped_by_watchpoint () override
;
153 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
154 struct expression
*) override
;
156 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
157 struct expression
*) override
;
159 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
161 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
162 bool stopped_data_address (CORE_ADDR
*) override
;
164 void procfs_init_inferior (int pid
);
167 static procfs_target the_procfs_target
;
169 #if PR_MODEL_NATIVE == PR_MODEL_LP64
170 /* When GDB is built as 64-bit application on Solaris, the auxv data
171 is presented in 64-bit format. We need to provide a custom parser
174 procfs_target::auxv_parse (gdb_byte
**readptr
,
175 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
177 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
178 gdb_byte
*ptr
= *readptr
;
183 if (endptr
- ptr
< 8 * 2)
186 *typep
= extract_unsigned_integer (ptr
, 4, byte_order
);
188 /* The size of data is always 64-bit. If the application is 32-bit,
189 it will be zero extended, as expected. */
190 *valp
= extract_unsigned_integer (ptr
, 8, byte_order
);
198 /* =================== END, TARGET_OPS "MODULE" =================== */
200 /* =================== STRUCT PROCINFO "MODULE" =================== */
202 /* FIXME: this comment will soon be out of date W.R.T. threads. */
204 /* The procinfo struct is a wrapper to hold all the state information
205 concerning a /proc process. There should be exactly one procinfo
206 for each process, and since GDB currently can debug only one
207 process at a time, that means there should be only one procinfo.
208 All of the LWP's of a process can be accessed indirectly thru the
209 single process procinfo.
211 However, against the day when GDB may debug more than one process,
212 this data structure is kept in a list (which for now will hold no
213 more than one member), and many functions will have a pointer to a
214 procinfo as an argument.
216 There will be a separate procinfo structure for use by the (not yet
217 implemented) "info proc" command, so that we can print useful
218 information about any random process without interfering with the
219 inferior's procinfo information. */
221 /* format strings for /proc paths */
222 #define CTL_PROC_NAME_FMT "/proc/%d/ctl"
223 #define AS_PROC_NAME_FMT "/proc/%d/as"
224 #define MAP_PROC_NAME_FMT "/proc/%d/map"
225 #define STATUS_PROC_NAME_FMT "/proc/%d/status"
226 #define MAX_PROC_NAME_SIZE sizeof("/proc/999999/lwp/0123456789/lwpstatus")
228 typedef struct procinfo
{
229 struct procinfo
*next
;
230 int pid
; /* Process ID */
231 int tid
; /* Thread/LWP id */
235 int ignore_next_sigstop
;
237 int ctl_fd
; /* File descriptor for /proc control file */
238 int status_fd
; /* File descriptor for /proc status file */
239 int as_fd
; /* File descriptor for /proc as file */
241 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
243 fltset_t saved_fltset
; /* Saved traced hardware fault set */
244 sigset_t saved_sigset
; /* Saved traced signal set */
245 sigset_t saved_sighold
; /* Saved held signal set */
246 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
247 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
249 pstatus_t prstatus
; /* Current process status info */
251 struct procinfo
*thread_list
;
253 int status_valid
: 1;
255 int fpregs_valid
: 1;
256 int threads_valid
: 1;
259 static char errmsg
[128]; /* shared error msg buffer */
261 /* Function prototypes for procinfo module: */
263 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
264 static procinfo
*find_procinfo (int pid
, int tid
);
265 static procinfo
*create_procinfo (int pid
, int tid
);
266 static void destroy_procinfo (procinfo
*p
);
267 static void dead_procinfo (procinfo
*p
, const char *msg
, int killp
);
268 static int open_procinfo_files (procinfo
*p
, int which
);
269 static void close_procinfo_files (procinfo
*p
);
271 static int iterate_over_mappings
272 (procinfo
*pi
, find_memory_region_ftype child_func
, void *data
,
273 int (*func
) (struct prmap
*map
, find_memory_region_ftype child_func
,
276 /* The head of the procinfo list: */
277 static procinfo
*procinfo_list
;
279 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
283 find_procinfo (int pid
, int tid
)
287 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
294 /* Don't check threads_valid. If we're updating the
295 thread_list, we want to find whatever threads are already
296 here. This means that in general it is the caller's
297 responsibility to check threads_valid and update before
298 calling find_procinfo, if the caller wants to find a new
301 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
309 /* Calls find_procinfo, but errors on failure. */
312 find_procinfo_or_die (int pid
, int tid
)
314 procinfo
*pi
= find_procinfo (pid
, tid
);
319 error (_("procfs: couldn't find pid %d "
320 "(kernel thread %d) in procinfo list."),
323 error (_("procfs: couldn't find pid %d in procinfo list."), pid
);
328 /* Wrapper for `open'. The appropriate open call is attempted; if
329 unsuccessful, it will be retried as many times as needed for the
330 EAGAIN and EINTR conditions.
332 For other conditions, retry the open a limited number of times. In
333 addition, a short sleep is imposed prior to retrying the open. The
334 reason for this sleep is to give the kernel a chance to catch up
335 and create the file in question in the event that GDB "wins" the
336 race to open a file before the kernel has created it. */
339 open_with_retry (const char *pathname
, int flags
)
341 int retries_remaining
, status
;
343 retries_remaining
= 2;
347 status
= open (pathname
, flags
);
349 if (status
>= 0 || retries_remaining
== 0)
351 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
361 /* Open the file descriptor for the process or LWP. We only open the
362 control file descriptor; the others are opened lazily as needed.
363 Returns the file descriptor, or zero for failure. */
365 enum { FD_CTL
, FD_STATUS
, FD_AS
};
368 open_procinfo_files (procinfo
*pi
, int which
)
370 char tmp
[MAX_PROC_NAME_SIZE
];
373 /* This function is getting ALMOST long enough to break up into
374 several. Here is some rationale:
376 There are several file descriptors that may need to be open
377 for any given process or LWP. The ones we're interested in are:
378 - control (ctl) write-only change the state
379 - status (status) read-only query the state
380 - address space (as) read/write access memory
381 - map (map) read-only virtual addr map
382 Most of these are opened lazily as they are needed.
383 The pathnames for the 'files' for an LWP look slightly
384 different from those of a first-class process:
385 Pathnames for a process (<proc-id>):
387 /proc/<proc-id>/status
390 Pathnames for an LWP (lwp-id):
391 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
392 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
393 An LWP has no map or address space file descriptor, since
394 the memory map and address space are shared by all LWPs. */
396 /* In this case, there are several different file descriptors that
397 we might be asked to open. The control file descriptor will be
398 opened early, but the others will be opened lazily as they are
401 strcpy (tmp
, pi
->pathname
);
402 switch (which
) { /* Which file descriptor to open? */
405 strcat (tmp
, "/lwpctl");
407 strcat (tmp
, "/ctl");
408 fd
= open_with_retry (tmp
, O_WRONLY
);
415 return 0; /* There is no 'as' file descriptor for an lwp. */
417 fd
= open_with_retry (tmp
, O_RDWR
);
424 strcat (tmp
, "/lwpstatus");
426 strcat (tmp
, "/status");
427 fd
= open_with_retry (tmp
, O_RDONLY
);
433 return 0; /* unknown file descriptor */
436 return 1; /* success */
439 /* Allocate a data structure and link it into the procinfo list.
440 First tries to find a pre-existing one (FIXME: why?). Returns the
441 pointer to new procinfo struct. */
444 create_procinfo (int pid
, int tid
)
446 procinfo
*pi
, *parent
= NULL
;
448 pi
= find_procinfo (pid
, tid
);
450 return pi
; /* Already exists, nothing to do. */
452 /* Find parent before doing malloc, to save having to cleanup. */
454 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
456 doesn't exist yet? */
458 pi
= XNEW (procinfo
);
459 memset (pi
, 0, sizeof (procinfo
));
463 pi
->saved_entryset
= XNEW (sysset_t
);
464 pi
->saved_exitset
= XNEW (sysset_t
);
466 /* Chain into list. */
469 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), "/proc/%d", pid
);
470 pi
->next
= procinfo_list
;
475 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), "/proc/%d/lwp/%d",
477 pi
->next
= parent
->thread_list
;
478 parent
->thread_list
= pi
;
483 /* Close all file descriptors associated with the procinfo. */
486 close_procinfo_files (procinfo
*pi
)
492 if (pi
->status_fd
> 0)
493 close (pi
->status_fd
);
494 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
497 /* Destructor function. Close, unlink and deallocate the object. */
500 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
504 /* Step one: unlink the procinfo from its list. */
508 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
511 ptr
->next
= pi
->next
;
515 /* Step two: close any open file descriptors. */
516 close_procinfo_files (pi
);
518 /* Step three: free the memory. */
519 xfree (pi
->saved_entryset
);
520 xfree (pi
->saved_exitset
);
525 destroy_procinfo (procinfo
*pi
)
529 if (pi
->tid
!= 0) /* Destroy a thread procinfo. */
531 tmp
= find_procinfo (pi
->pid
, 0); /* Find the parent process. */
532 destroy_one_procinfo (&tmp
->thread_list
, pi
);
534 else /* Destroy a process procinfo and all its threads. */
536 /* First destroy the children, if any; */
537 while (pi
->thread_list
!= NULL
)
538 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
539 /* Then destroy the parent. Genocide!!! */
540 destroy_one_procinfo (&procinfo_list
, pi
);
544 /* A deleter that calls destroy_procinfo. */
545 struct procinfo_deleter
547 void operator() (procinfo
*pi
) const
549 destroy_procinfo (pi
);
553 typedef std::unique_ptr
<procinfo
, procinfo_deleter
> procinfo_up
;
555 enum { NOKILL
, KILL
};
557 /* To be called on a non_recoverable error for a procinfo. Prints
558 error messages, optionally sends a SIGKILL to the process, then
559 destroys the data structure. */
562 dead_procinfo (procinfo
*pi
, const char *msg
, int kill_p
)
567 print_sys_errmsg (pi
->pathname
, errno
);
570 xsnprintf (procfile
, sizeof (procfile
), "process %d", pi
->pid
);
571 print_sys_errmsg (procfile
, errno
);
574 kill (pi
->pid
, SIGKILL
);
576 destroy_procinfo (pi
);
580 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
582 /* =================== /proc "MODULE" =================== */
584 /* This "module" is the interface layer between the /proc system API
585 and the gdb target vector functions. This layer consists of access
586 functions that encapsulate each of the basic operations that we
587 need to use from the /proc API.
589 The main motivation for this layer is to hide the fact that there
590 were two very different implementations of the /proc API. */
592 static long proc_flags (procinfo
*pi
);
593 static int proc_why (procinfo
*pi
);
594 static int proc_what (procinfo
*pi
);
595 static int proc_set_current_signal (procinfo
*pi
, int signo
);
596 static int proc_get_current_thread (procinfo
*pi
);
597 static int proc_iterate_over_threads
599 int (*func
) (procinfo
*, procinfo
*, void *),
603 proc_warn (procinfo
*pi
, const char *func
, int line
)
605 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
606 func
, line
, pi
->pathname
);
607 print_sys_errmsg (errmsg
, errno
);
611 proc_error (procinfo
*pi
, const char *func
, int line
)
613 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
614 func
, line
, pi
->pathname
);
615 perror_with_name (errmsg
);
618 /* Updates the status struct in the procinfo. There is a 'valid'
619 flag, to let other functions know when this function needs to be
620 called (so the status is only read when it is needed). The status
621 file descriptor is also only opened when it is needed. Returns
622 non-zero for success, zero for failure. */
625 proc_get_status (procinfo
*pi
)
627 /* Status file descriptor is opened "lazily". */
628 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
630 pi
->status_valid
= 0;
634 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
635 pi
->status_valid
= 0; /* fail */
638 /* Sigh... I have to read a different data structure,
639 depending on whether this is a main process or an LWP. */
641 pi
->status_valid
= (read (pi
->status_fd
,
642 (char *) &pi
->prstatus
.pr_lwp
,
643 sizeof (lwpstatus_t
))
644 == sizeof (lwpstatus_t
));
647 pi
->status_valid
= (read (pi
->status_fd
,
648 (char *) &pi
->prstatus
,
650 == sizeof (pstatus_t
));
654 if (pi
->status_valid
)
656 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
659 proc_get_current_thread (pi
));
662 /* The status struct includes general regs, so mark them valid too. */
663 pi
->gregs_valid
= pi
->status_valid
;
664 /* In the read/write multiple-fd model, the status struct includes
665 the fp regs too, so mark them valid too. */
666 pi
->fpregs_valid
= pi
->status_valid
;
667 return pi
->status_valid
; /* True if success, false if failure. */
670 /* Returns the process flags (pr_flags field). */
673 proc_flags (procinfo
*pi
)
675 if (!pi
->status_valid
)
676 if (!proc_get_status (pi
))
677 return 0; /* FIXME: not a good failure value (but what is?) */
679 return pi
->prstatus
.pr_lwp
.pr_flags
;
682 /* Returns the pr_why field (why the process stopped). */
685 proc_why (procinfo
*pi
)
687 if (!pi
->status_valid
)
688 if (!proc_get_status (pi
))
689 return 0; /* FIXME: not a good failure value (but what is?) */
691 return pi
->prstatus
.pr_lwp
.pr_why
;
694 /* Returns the pr_what field (details of why the process stopped). */
697 proc_what (procinfo
*pi
)
699 if (!pi
->status_valid
)
700 if (!proc_get_status (pi
))
701 return 0; /* FIXME: not a good failure value (but what is?) */
703 return pi
->prstatus
.pr_lwp
.pr_what
;
706 /* This function is only called when PI is stopped by a watchpoint.
707 Assuming the OS supports it, write to *ADDR the data address which
708 triggered it and return 1. Return 0 if it is not possible to know
712 proc_watchpoint_address (procinfo
*pi
, CORE_ADDR
*addr
)
714 if (!pi
->status_valid
)
715 if (!proc_get_status (pi
))
718 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
719 builtin_type (target_gdbarch ())->builtin_data_ptr
,
720 (gdb_byte
*) &pi
->prstatus
.pr_lwp
.pr_info
.si_addr
);
724 /* Returns the pr_nsysarg field (number of args to the current
728 proc_nsysarg (procinfo
*pi
)
730 if (!pi
->status_valid
)
731 if (!proc_get_status (pi
))
734 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
737 /* Returns the pr_sysarg field (pointer to the arguments of current
741 proc_sysargs (procinfo
*pi
)
743 if (!pi
->status_valid
)
744 if (!proc_get_status (pi
))
747 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
750 /* Set or reset any of the following process flags:
751 PR_FORK -- forked child will inherit trace flags
752 PR_RLC -- traced process runs when last /proc file closed.
753 PR_KLC -- traced process is killed when last /proc file closed.
754 PR_ASYNC -- LWP's get to run/stop independently.
756 This function is done using read/write [PCSET/PCRESET/PCUNSET].
760 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
761 mode -- 1 for set, 0 for reset.
763 Returns non-zero for success, zero for failure. */
765 enum { FLAG_RESET
, FLAG_SET
};
768 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
770 long win
= 0; /* default to fail */
772 /* These operations affect the process as a whole, and applying them
773 to an individual LWP has the same meaning as applying them to the
774 main process. Therefore, if we're ever called with a pointer to
775 an LWP's procinfo, let's substitute the process's procinfo and
776 avoid opening the LWP's file descriptor unnecessarily. */
779 pi
= find_procinfo_or_die (pi
->pid
, 0);
783 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC). */
785 else /* Reset the flag. */
789 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
791 /* The above operation renders the procinfo's cached pstatus
793 pi
->status_valid
= 0;
796 warning (_("procfs: modify_flag failed to turn %s %s"),
797 flag
== PR_FORK
? "PR_FORK" :
798 flag
== PR_RLC
? "PR_RLC" :
799 flag
== PR_ASYNC
? "PR_ASYNC" :
800 flag
== PR_KLC
? "PR_KLC" :
802 mode
== FLAG_RESET
? "off" : "on");
807 /* Set the run_on_last_close flag. Process with all threads will
808 become runnable when debugger closes all /proc fds. Returns
809 non-zero for success, zero for failure. */
812 proc_set_run_on_last_close (procinfo
*pi
)
814 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
817 /* Reset the run_on_last_close flag. The process will NOT become
818 runnable when debugger closes its file handles. Returns non-zero
819 for success, zero for failure. */
822 proc_unset_run_on_last_close (procinfo
*pi
)
824 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
827 /* Reset inherit_on_fork flag. If the process forks a child while we
828 are registered for events in the parent, then we will NOT receive
829 events from the child. Returns non-zero for success, zero for
833 proc_unset_inherit_on_fork (procinfo
*pi
)
835 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
838 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
839 (signal etc.), the remaining LWPs will continue to run. Returns
840 non-zero for success, zero for failure. */
843 proc_set_async (procinfo
*pi
)
845 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
848 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
849 (signal etc.), then all other LWPs will stop as well. Returns
850 non-zero for success, zero for failure. */
853 proc_unset_async (procinfo
*pi
)
855 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
858 /* Request the process/LWP to stop. Does not wait. Returns non-zero
859 for success, zero for failure. */
862 proc_stop_process (procinfo
*pi
)
866 /* We might conceivably apply this operation to an LWP, and the
867 LWP's ctl file descriptor might not be open. */
869 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
873 procfs_ctl_t cmd
= PCSTOP
;
875 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
881 /* Wait for the process or LWP to stop (block until it does). Returns
882 non-zero for success, zero for failure. */
885 proc_wait_for_stop (procinfo
*pi
)
889 /* We should never have to apply this operation to any procinfo
890 except the one for the main process. If that ever changes for
891 any reason, then take out the following clause and replace it
892 with one that makes sure the ctl_fd is open. */
895 pi
= find_procinfo_or_die (pi
->pid
, 0);
897 procfs_ctl_t cmd
= PCWSTOP
;
901 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
903 clear_sigint_trap ();
905 /* We been runnin' and we stopped -- need to update status. */
906 pi
->status_valid
= 0;
911 /* Make the process or LWP runnable.
913 Options (not all are implemented):
915 - clear current fault
916 - clear current signal
917 - abort the current system call
918 - stop as soon as finished with system call
920 Always clears the current fault. PI is the process or LWP to
921 operate on. If STEP is true, set the process or LWP to trap after
922 one instruction. If SIGNO is zero, clear the current signal if
923 any; if non-zero, set the current signal to this one. Returns
924 non-zero for success, zero for failure. */
927 proc_run_process (procinfo
*pi
, int step
, int signo
)
932 /* We will probably have to apply this operation to individual
933 threads, so make sure the control file descriptor is open. */
935 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
938 runflags
= PRCFAULT
; /* Always clear current fault. */
943 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals. */
944 proc_set_current_signal (pi
, signo
);
950 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
955 /* Register to trace signals in the process or LWP. Returns non-zero
956 for success, zero for failure. */
959 proc_set_traced_signals (procinfo
*pi
, sigset_t
*sigset
)
963 /* We should never have to apply this operation to any procinfo
964 except the one for the main process. If that ever changes for
965 any reason, then take out the following clause and replace it
966 with one that makes sure the ctl_fd is open. */
969 pi
= find_procinfo_or_die (pi
->pid
, 0);
973 /* Use char array to avoid alignment issues. */
974 char sigset
[sizeof (sigset_t
)];
978 memcpy (&arg
.sigset
, sigset
, sizeof (sigset_t
));
980 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
982 /* The above operation renders the procinfo's cached pstatus obsolete. */
983 pi
->status_valid
= 0;
986 warning (_("procfs: set_traced_signals failed"));
990 /* Register to trace hardware faults in the process or LWP. Returns
991 non-zero for success, zero for failure. */
994 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
998 /* We should never have to apply this operation to any procinfo
999 except the one for the main process. If that ever changes for
1000 any reason, then take out the following clause and replace it
1001 with one that makes sure the ctl_fd is open. */
1004 pi
= find_procinfo_or_die (pi
->pid
, 0);
1008 /* Use char array to avoid alignment issues. */
1009 char fltset
[sizeof (fltset_t
)];
1013 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1015 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1017 /* The above operation renders the procinfo's cached pstatus obsolete. */
1018 pi
->status_valid
= 0;
1023 /* Register to trace entry to system calls in the process or LWP.
1024 Returns non-zero for success, zero for failure. */
1027 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1031 /* We should never have to apply this operation to any procinfo
1032 except the one for the main process. If that ever changes for
1033 any reason, then take out the following clause and replace it
1034 with one that makes sure the ctl_fd is open. */
1037 pi
= find_procinfo_or_die (pi
->pid
, 0);
1041 /* Use char array to avoid alignment issues. */
1042 char sysset
[sizeof (sysset_t
)];
1046 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1048 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1050 /* The above operation renders the procinfo's cached pstatus
1052 pi
->status_valid
= 0;
1057 /* Register to trace exit from system calls in the process or LWP.
1058 Returns non-zero for success, zero for failure. */
1061 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1065 /* We should never have to apply this operation to any procinfo
1066 except the one for the main process. If that ever changes for
1067 any reason, then take out the following clause and replace it
1068 with one that makes sure the ctl_fd is open. */
1071 pi
= find_procinfo_or_die (pi
->pid
, 0);
1073 struct gdb_proc_ctl_pcsexit
{
1075 /* Use char array to avoid alignment issues. */
1076 char sysset
[sizeof (sysset_t
)];
1080 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1082 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1084 /* The above operation renders the procinfo's cached pstatus
1086 pi
->status_valid
= 0;
1091 /* Specify the set of blocked / held signals in the process or LWP.
1092 Returns non-zero for success, zero for failure. */
1095 proc_set_held_signals (procinfo
*pi
, sigset_t
*sighold
)
1099 /* We should never have to apply this operation to any procinfo
1100 except the one for the main process. If that ever changes for
1101 any reason, then take out the following clause and replace it
1102 with one that makes sure the ctl_fd is open. */
1105 pi
= find_procinfo_or_die (pi
->pid
, 0);
1109 /* Use char array to avoid alignment issues. */
1110 char hold
[sizeof (sigset_t
)];
1114 memcpy (&arg
.hold
, sighold
, sizeof (sigset_t
));
1115 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1117 /* The above operation renders the procinfo's cached pstatus
1119 pi
->status_valid
= 0;
1124 /* Returns the set of signals that are held / blocked. Will also copy
1125 the sigset if SAVE is non-zero. */
1128 proc_get_held_signals (procinfo
*pi
, sigset_t
*save
)
1130 sigset_t
*ret
= NULL
;
1132 /* We should never have to apply this operation to any procinfo
1133 except the one for the main process. If that ever changes for
1134 any reason, then take out the following clause and replace it
1135 with one that makes sure the ctl_fd is open. */
1138 pi
= find_procinfo_or_die (pi
->pid
, 0);
1140 if (!pi
->status_valid
)
1141 if (!proc_get_status (pi
))
1144 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1146 memcpy (save
, ret
, sizeof (sigset_t
));
1151 /* Returns the set of signals that are traced / debugged. Will also
1152 copy the sigset if SAVE is non-zero. */
1155 proc_get_traced_signals (procinfo
*pi
, sigset_t
*save
)
1157 sigset_t
*ret
= NULL
;
1159 /* We should never have to apply this operation to any procinfo
1160 except the one for the main process. If that ever changes for
1161 any reason, then take out the following clause and replace it
1162 with one that makes sure the ctl_fd is open. */
1165 pi
= find_procinfo_or_die (pi
->pid
, 0);
1167 if (!pi
->status_valid
)
1168 if (!proc_get_status (pi
))
1171 ret
= &pi
->prstatus
.pr_sigtrace
;
1173 memcpy (save
, ret
, sizeof (sigset_t
));
1178 /* Returns the set of hardware faults that are traced /debugged. Will
1179 also copy the faultset if SAVE is non-zero. */
1182 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1184 fltset_t
*ret
= NULL
;
1186 /* We should never have to apply this operation to any procinfo
1187 except the one for the main process. If that ever changes for
1188 any reason, then take out the following clause and replace it
1189 with one that makes sure the ctl_fd is open. */
1192 pi
= find_procinfo_or_die (pi
->pid
, 0);
1194 if (!pi
->status_valid
)
1195 if (!proc_get_status (pi
))
1198 ret
= &pi
->prstatus
.pr_flttrace
;
1200 memcpy (save
, ret
, sizeof (fltset_t
));
1205 /* Returns the set of syscalls that are traced /debugged on entry.
1206 Will also copy the syscall set if SAVE is non-zero. */
1209 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1211 sysset_t
*ret
= NULL
;
1213 /* We should never have to apply this operation to any procinfo
1214 except the one for the main process. If that ever changes for
1215 any reason, then take out the following clause and replace it
1216 with one that makes sure the ctl_fd is open. */
1219 pi
= find_procinfo_or_die (pi
->pid
, 0);
1221 if (!pi
->status_valid
)
1222 if (!proc_get_status (pi
))
1225 ret
= &pi
->prstatus
.pr_sysentry
;
1227 memcpy (save
, ret
, sizeof (sysset_t
));
1232 /* Returns the set of syscalls that are traced /debugged on exit.
1233 Will also copy the syscall set if SAVE is non-zero. */
1236 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1238 sysset_t
*ret
= NULL
;
1240 /* We should never have to apply this operation to any procinfo
1241 except the one for the main process. If that ever changes for
1242 any reason, then take out the following clause and replace it
1243 with one that makes sure the ctl_fd is open. */
1246 pi
= find_procinfo_or_die (pi
->pid
, 0);
1248 if (!pi
->status_valid
)
1249 if (!proc_get_status (pi
))
1252 ret
= &pi
->prstatus
.pr_sysexit
;
1254 memcpy (save
, ret
, sizeof (sysset_t
));
1259 /* The current fault (if any) is cleared; the associated signal will
1260 not be sent to the process or LWP when it resumes. Returns
1261 non-zero for success, zero for failure. */
1264 proc_clear_current_fault (procinfo
*pi
)
1268 /* We should never have to apply this operation to any procinfo
1269 except the one for the main process. If that ever changes for
1270 any reason, then take out the following clause and replace it
1271 with one that makes sure the ctl_fd is open. */
1274 pi
= find_procinfo_or_die (pi
->pid
, 0);
1276 procfs_ctl_t cmd
= PCCFAULT
;
1278 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1283 /* Set the "current signal" that will be delivered next to the
1284 process. NOTE: semantics are different from those of KILL. This
1285 signal will be delivered to the process or LWP immediately when it
1286 is resumed (even if the signal is held/blocked); it will NOT
1287 immediately cause another event of interest, and will NOT first
1288 trap back to the debugger. Returns non-zero for success, zero for
1292 proc_set_current_signal (procinfo
*pi
, int signo
)
1297 /* Use char array to avoid alignment issues. */
1298 char sinfo
[sizeof (siginfo_t
)];
1301 process_stratum_target
*wait_target
;
1303 struct target_waitstatus wait_status
;
1305 /* We should never have to apply this operation to any procinfo
1306 except the one for the main process. If that ever changes for
1307 any reason, then take out the following clause and replace it
1308 with one that makes sure the ctl_fd is open. */
1311 pi
= find_procinfo_or_die (pi
->pid
, 0);
1313 /* The pointer is just a type alias. */
1314 get_last_target_status (&wait_target
, &wait_ptid
, &wait_status
);
1315 if (wait_target
== &the_procfs_target
1316 && wait_ptid
== inferior_ptid
1317 && wait_status
.kind
== TARGET_WAITKIND_STOPPED
1318 && wait_status
.value
.sig
== gdb_signal_from_host (signo
)
1319 && proc_get_status (pi
)
1320 && pi
->prstatus
.pr_lwp
.pr_info
.si_signo
== signo
1322 /* Use the siginfo associated with the signal being
1324 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_lwp
.pr_info
, sizeof (siginfo_t
));
1327 mysinfo
.si_signo
= signo
;
1328 mysinfo
.si_code
= 0;
1329 mysinfo
.si_pid
= getpid (); /* ?why? */
1330 mysinfo
.si_uid
= getuid (); /* ?why? */
1331 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1335 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1340 /* The current signal (if any) is cleared, and is not sent to the
1341 process or LWP when it resumes. Returns non-zero for success, zero
1345 proc_clear_current_signal (procinfo
*pi
)
1349 /* We should never have to apply this operation to any procinfo
1350 except the one for the main process. If that ever changes for
1351 any reason, then take out the following clause and replace it
1352 with one that makes sure the ctl_fd is open. */
1355 pi
= find_procinfo_or_die (pi
->pid
, 0);
1359 /* Use char array to avoid alignment issues. */
1360 char sinfo
[sizeof (siginfo_t
)];
1365 /* The pointer is just a type alias. */
1366 mysinfo
.si_signo
= 0;
1367 mysinfo
.si_code
= 0;
1368 mysinfo
.si_errno
= 0;
1369 mysinfo
.si_pid
= getpid (); /* ?why? */
1370 mysinfo
.si_uid
= getuid (); /* ?why? */
1371 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1373 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1378 /* Return the general-purpose registers for the process or LWP
1379 corresponding to PI. Upon failure, return NULL. */
1381 static gdb_gregset_t
*
1382 proc_get_gregs (procinfo
*pi
)
1384 if (!pi
->status_valid
|| !pi
->gregs_valid
)
1385 if (!proc_get_status (pi
))
1388 return &pi
->prstatus
.pr_lwp
.pr_reg
;
1391 /* Return the general-purpose registers for the process or LWP
1392 corresponding to PI. Upon failure, return NULL. */
1394 static gdb_fpregset_t
*
1395 proc_get_fpregs (procinfo
*pi
)
1397 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
1398 if (!proc_get_status (pi
))
1401 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
1404 /* Write the general-purpose registers back to the process or LWP
1405 corresponding to PI. Return non-zero for success, zero for
1409 proc_set_gregs (procinfo
*pi
)
1411 gdb_gregset_t
*gregs
;
1414 gregs
= proc_get_gregs (pi
);
1416 return 0; /* proc_get_regs has already warned. */
1418 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1424 /* Use char array to avoid alignment issues. */
1425 char gregs
[sizeof (gdb_gregset_t
)];
1429 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
1430 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1433 /* Policy: writing the registers invalidates our cache. */
1434 pi
->gregs_valid
= 0;
1438 /* Write the floating-pointer registers back to the process or LWP
1439 corresponding to PI. Return non-zero for success, zero for
1443 proc_set_fpregs (procinfo
*pi
)
1445 gdb_fpregset_t
*fpregs
;
1448 fpregs
= proc_get_fpregs (pi
);
1450 return 0; /* proc_get_fpregs has already warned. */
1452 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1458 /* Use char array to avoid alignment issues. */
1459 char fpregs
[sizeof (gdb_fpregset_t
)];
1463 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
1464 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1467 /* Policy: writing the registers invalidates our cache. */
1468 pi
->fpregs_valid
= 0;
1472 /* Send a signal to the proc or lwp with the semantics of "kill()".
1473 Returns non-zero for success, zero for failure. */
1476 proc_kill (procinfo
*pi
, int signo
)
1480 /* We might conceivably apply this operation to an LWP, and the
1481 LWP's ctl file descriptor might not be open. */
1483 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1487 procfs_ctl_t cmd
[2];
1491 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1497 /* Find the pid of the process that started this one. Returns the
1498 parent process pid, or zero. */
1501 proc_parent_pid (procinfo
*pi
)
1503 /* We should never have to apply this operation to any procinfo
1504 except the one for the main process. If that ever changes for
1505 any reason, then take out the following clause and replace it
1506 with one that makes sure the ctl_fd is open. */
1509 pi
= find_procinfo_or_die (pi
->pid
, 0);
1511 if (!pi
->status_valid
)
1512 if (!proc_get_status (pi
))
1515 return pi
->prstatus
.pr_ppid
;
1518 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
1519 (a.k.a void pointer)! */
1522 procfs_address_to_host_pointer (CORE_ADDR addr
)
1524 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
1527 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (ptr_type
));
1528 gdbarch_address_to_pointer (target_gdbarch (), ptr_type
,
1529 (gdb_byte
*) &ptr
, addr
);
1534 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
1538 char watch
[sizeof (prwatch_t
)];
1542 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
1543 convert a target address into something that can be stored in a
1544 native data structure. */
1545 pwatch
.pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
1546 pwatch
.pr_size
= len
;
1547 pwatch
.pr_wflags
= wflags
;
1549 memcpy (arg
.watch
, &pwatch
, sizeof (prwatch_t
));
1550 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
1553 /* =============== END, non-thread part of /proc "MODULE" =============== */
1555 /* =================== Thread "MODULE" =================== */
1557 /* Returns the number of threads for the process. */
1560 proc_get_nthreads (procinfo
*pi
)
1562 if (!pi
->status_valid
)
1563 if (!proc_get_status (pi
))
1566 /* Only works for the process procinfo, because the LWP procinfos do not
1567 get prstatus filled in. */
1568 if (pi
->tid
!= 0) /* Find the parent process procinfo. */
1569 pi
= find_procinfo_or_die (pi
->pid
, 0);
1570 return pi
->prstatus
.pr_nlwp
;
1573 /* Return the ID of the thread that had an event of interest.
1574 (ie. the one that hit a breakpoint or other traced event). All
1575 other things being equal, this should be the ID of a thread that is
1576 currently executing. */
1579 proc_get_current_thread (procinfo
*pi
)
1581 /* Note: this should be applied to the root procinfo for the
1582 process, not to the procinfo for an LWP. If applied to the
1583 procinfo for an LWP, it will simply return that LWP's ID. In
1584 that case, find the parent process procinfo. */
1587 pi
= find_procinfo_or_die (pi
->pid
, 0);
1589 if (!pi
->status_valid
)
1590 if (!proc_get_status (pi
))
1593 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
1596 /* Discover the IDs of all the threads within the process, and create
1597 a procinfo for each of them (chained to the parent). Returns
1598 non-zero for success, zero for failure. */
1601 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
1603 if (thread
&& parent
) /* sanity */
1605 thread
->status_valid
= 0;
1606 if (!proc_get_status (thread
))
1607 destroy_one_procinfo (&parent
->thread_list
, thread
);
1609 return 0; /* keep iterating */
1613 proc_update_threads (procinfo
*pi
)
1615 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
1616 struct dirent
*direntry
;
1621 /* We should never have to apply this operation to any procinfo
1622 except the one for the main process. If that ever changes for
1623 any reason, then take out the following clause and replace it
1624 with one that makes sure the ctl_fd is open. */
1627 pi
= find_procinfo_or_die (pi
->pid
, 0);
1629 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
1631 /* Note: this brute-force method was originally devised for Unixware
1632 (support removed since), and will also work on Solaris 2.6 and
1633 2.7. The original comment mentioned the existence of a much
1634 simpler and more elegant way to do this on Solaris, but didn't
1635 point out what that was. */
1637 strcpy (pathname
, pi
->pathname
);
1638 strcat (pathname
, "/lwp");
1639 dirp
.reset (opendir (pathname
));
1641 proc_error (pi
, "update_threads, opendir", __LINE__
);
1643 while ((direntry
= readdir (dirp
.get ())) != NULL
)
1644 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
1646 lwpid
= atoi (&direntry
->d_name
[0]);
1647 thread
= create_procinfo (pi
->pid
, lwpid
);
1649 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
1651 pi
->threads_valid
= 1;
1655 /* Given a pointer to a function, call that function once for each lwp
1656 in the procinfo list, until the function returns non-zero, in which
1657 event return the value returned by the function.
1659 Note: this function does NOT call update_threads. If you want to
1660 discover new threads first, you must call that function explicitly.
1661 This function just makes a quick pass over the currently-known
1664 PI is the parent process procinfo. FUNC is the per-thread
1665 function. PTR is an opaque parameter for function. Returns the
1666 first non-zero return value from the callee, or zero. */
1669 proc_iterate_over_threads (procinfo
*pi
,
1670 int (*func
) (procinfo
*, procinfo
*, void *),
1673 procinfo
*thread
, *next
;
1676 /* We should never have to apply this operation to any procinfo
1677 except the one for the main process. If that ever changes for
1678 any reason, then take out the following clause and replace it
1679 with one that makes sure the ctl_fd is open. */
1682 pi
= find_procinfo_or_die (pi
->pid
, 0);
1684 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
1686 next
= thread
->next
; /* In case thread is destroyed. */
1687 retval
= (*func
) (pi
, thread
, ptr
);
1695 /* =================== END, Thread "MODULE" =================== */
1697 /* =================== END, /proc "MODULE" =================== */
1699 /* =================== GDB "MODULE" =================== */
1701 /* Here are all of the gdb target vector functions and their
1704 static void do_attach (ptid_t ptid
);
1705 static void do_detach ();
1706 static void proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
,
1707 int entry_or_exit
, int mode
, int from_tty
);
1709 /* Sets up the inferior to be debugged. Registers to trace signals,
1710 hardware faults, and syscalls. Note: does not set RLC flag: caller
1711 may want to customize that. Returns zero for success (note!
1712 unlike most functions in this module); on failure, returns the LINE
1713 NUMBER where it failed! */
1716 procfs_debug_inferior (procinfo
*pi
)
1718 fltset_t traced_faults
;
1719 sigset_t traced_signals
;
1720 sysset_t
*traced_syscall_entries
;
1721 sysset_t
*traced_syscall_exits
;
1724 /* Register to trace hardware faults in the child. */
1725 prfillset (&traced_faults
); /* trace all faults... */
1726 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
1727 if (!proc_set_traced_faults (pi
, &traced_faults
))
1730 /* Initially, register to trace all signals in the child. */
1731 prfillset (&traced_signals
);
1732 if (!proc_set_traced_signals (pi
, &traced_signals
))
1736 /* Register to trace the 'exit' system call (on entry). */
1737 traced_syscall_entries
= XNEW (sysset_t
);
1738 premptyset (traced_syscall_entries
);
1739 praddset (traced_syscall_entries
, SYS_exit
);
1740 praddset (traced_syscall_entries
, SYS_lwp_exit
);
1742 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
1743 xfree (traced_syscall_entries
);
1747 /* Method for tracing exec syscalls. */
1748 traced_syscall_exits
= XNEW (sysset_t
);
1749 premptyset (traced_syscall_exits
);
1750 praddset (traced_syscall_exits
, SYS_execve
);
1751 praddset (traced_syscall_exits
, SYS_lwp_create
);
1752 praddset (traced_syscall_exits
, SYS_lwp_exit
);
1754 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
1755 xfree (traced_syscall_exits
);
1763 procfs_target::attach (const char *args
, int from_tty
)
1767 pid
= parse_pid_to_attach (args
);
1769 if (pid
== getpid ())
1770 error (_("Attaching GDB to itself is not a good idea..."));
1774 const char *exec_file
= get_exec_file (0);
1777 printf_filtered (_("Attaching to program `%s', %s\n"),
1778 exec_file
, target_pid_to_str (ptid_t (pid
)).c_str ());
1780 printf_filtered (_("Attaching to %s\n"),
1781 target_pid_to_str (ptid_t (pid
)).c_str ());
1785 do_attach (ptid_t (pid
));
1786 if (!target_is_pushed (this))
1791 procfs_target::detach (inferior
*inf
, int from_tty
)
1793 int pid
= inferior_ptid
.pid ();
1797 const char *exec_file
;
1799 exec_file
= get_exec_file (0);
1800 if (exec_file
== NULL
)
1803 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file
,
1804 target_pid_to_str (ptid_t (pid
)).c_str ());
1809 switch_to_no_thread ();
1810 detach_inferior (inf
);
1811 maybe_unpush_target ();
1815 do_attach (ptid_t ptid
)
1818 struct inferior
*inf
;
1822 pi
= create_procinfo (ptid
.pid (), 0);
1824 perror (_("procfs: out of memory in 'attach'"));
1826 if (!open_procinfo_files (pi
, FD_CTL
))
1828 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
1829 xsnprintf (errmsg
, sizeof (errmsg
),
1830 "do_attach: couldn't open /proc file for process %d",
1832 dead_procinfo (pi
, errmsg
, NOKILL
);
1835 /* Stop the process (if it isn't already stopped). */
1836 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
1838 pi
->was_stopped
= 1;
1839 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
1843 pi
->was_stopped
= 0;
1844 /* Set the process to run again when we close it. */
1845 if (!proc_set_run_on_last_close (pi
))
1846 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
1848 /* Now stop the process. */
1849 if (!proc_stop_process (pi
))
1850 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
1851 pi
->ignore_next_sigstop
= 1;
1853 /* Save some of the /proc state to be restored if we detach. */
1854 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
1855 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
1856 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
1857 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
1858 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
1859 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
1861 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
1862 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
1864 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
1865 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
1867 fail
= procfs_debug_inferior (pi
);
1869 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
1871 inf
= current_inferior ();
1872 inferior_appeared (inf
, pi
->pid
);
1873 /* Let GDB know that the inferior was attached. */
1874 inf
->attach_flag
= 1;
1876 /* Create a procinfo for the current lwp. */
1877 lwpid
= proc_get_current_thread (pi
);
1878 create_procinfo (pi
->pid
, lwpid
);
1880 /* Add it to gdb's thread list. */
1881 ptid
= ptid_t (pi
->pid
, lwpid
, 0);
1882 thread_info
*thr
= add_thread (&the_procfs_target
, ptid
);
1883 switch_to_thread (thr
);
1891 /* Find procinfo for the main process. */
1892 pi
= find_procinfo_or_die (inferior_ptid
.pid (),
1893 0); /* FIXME: threads */
1895 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
1896 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
1898 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
1899 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
1901 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
1902 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
1904 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
1905 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
1907 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
1908 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
1910 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
1911 if (!(pi
->was_stopped
)
1912 || query (_("Was stopped when attached, make it runnable again? ")))
1914 /* Clear any pending signal. */
1915 if (!proc_clear_current_fault (pi
))
1916 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
1918 if (!proc_clear_current_signal (pi
))
1919 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
1921 if (!proc_set_run_on_last_close (pi
))
1922 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
1925 destroy_procinfo (pi
);
1928 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
1931 NOTE: Since the /proc interface cannot give us individual
1932 registers, we pay no attention to REGNUM, and just fetch them all.
1933 This results in the possibility that we will do unnecessarily many
1934 fetches, since we may be called repeatedly for individual
1935 registers. So we cache the results, and mark the cache invalid
1936 when the process is resumed. */
1939 procfs_target::fetch_registers (struct regcache
*regcache
, int regnum
)
1941 gdb_gregset_t
*gregs
;
1943 ptid_t ptid
= regcache
->ptid ();
1944 int pid
= ptid
.pid ();
1945 int tid
= ptid
.lwp ();
1946 struct gdbarch
*gdbarch
= regcache
->arch ();
1948 pi
= find_procinfo_or_die (pid
, tid
);
1951 error (_("procfs: fetch_registers failed to find procinfo for %s"),
1952 target_pid_to_str (ptid
).c_str ());
1954 gregs
= proc_get_gregs (pi
);
1956 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
1958 supply_gregset (regcache
, (const gdb_gregset_t
*) gregs
);
1960 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
1962 gdb_fpregset_t
*fpregs
;
1964 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
1965 || regnum
== gdbarch_pc_regnum (gdbarch
)
1966 || regnum
== gdbarch_sp_regnum (gdbarch
))
1967 return; /* Not a floating point register. */
1969 fpregs
= proc_get_fpregs (pi
);
1971 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
1973 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregs
);
1977 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
1978 this for all registers.
1980 NOTE: Since the /proc interface will not read individual registers,
1981 we will cache these requests until the process is resumed, and only
1982 then write them back to the inferior process.
1984 FIXME: is that a really bad idea? Have to think about cases where
1985 writing one register might affect the value of others, etc. */
1988 procfs_target::store_registers (struct regcache
*regcache
, int regnum
)
1990 gdb_gregset_t
*gregs
;
1992 ptid_t ptid
= regcache
->ptid ();
1993 int pid
= ptid
.pid ();
1994 int tid
= ptid
.lwp ();
1995 struct gdbarch
*gdbarch
= regcache
->arch ();
1997 pi
= find_procinfo_or_die (pid
, tid
);
2000 error (_("procfs: store_registers: failed to find procinfo for %s"),
2001 target_pid_to_str (ptid
).c_str ());
2003 gregs
= proc_get_gregs (pi
);
2005 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
2007 fill_gregset (regcache
, gregs
, regnum
);
2008 if (!proc_set_gregs (pi
))
2009 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
2011 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
2013 gdb_fpregset_t
*fpregs
;
2015 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
2016 || regnum
== gdbarch_pc_regnum (gdbarch
)
2017 || regnum
== gdbarch_sp_regnum (gdbarch
))
2018 return; /* Not a floating point register. */
2020 fpregs
= proc_get_fpregs (pi
);
2022 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
2024 fill_fpregset (regcache
, fpregs
, regnum
);
2025 if (!proc_set_fpregs (pi
))
2026 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
2030 /* Retrieve the next stop event from the child process. If child has
2031 not stopped yet, wait for it to stop. Translate /proc eventcodes
2032 (or possibly wait eventcodes) into gdb internal event codes.
2033 Returns the id of process (and possibly thread) that incurred the
2034 event. Event codes are returned through a pointer parameter. */
2037 procfs_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
2040 /* First cut: loosely based on original version 2.1. */
2044 ptid_t retval
, temp_ptid
;
2045 int why
, what
, flags
;
2052 retval
= ptid_t (-1);
2054 /* Find procinfo for main process. */
2055 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2058 /* We must assume that the status is stale now... */
2059 pi
->status_valid
= 0;
2060 pi
->gregs_valid
= 0;
2061 pi
->fpregs_valid
= 0;
2063 #if 0 /* just try this out... */
2064 flags
= proc_flags (pi
);
2065 why
= proc_why (pi
);
2066 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
2067 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
2069 /* If child is not stopped, wait for it to stop. */
2070 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
2071 && !proc_wait_for_stop (pi
))
2073 /* wait_for_stop failed: has the child terminated? */
2074 if (errno
== ENOENT
)
2078 /* /proc file not found; presumably child has terminated. */
2079 wait_retval
= ::wait (&wstat
); /* "wait" for the child's exit. */
2082 if (wait_retval
!= inferior_ptid
.pid ())
2083 error (_("procfs: couldn't stop "
2084 "process %d: wait returned %d."),
2085 inferior_ptid
.pid (), wait_retval
);
2086 /* FIXME: might I not just use waitpid?
2087 Or try find_procinfo to see if I know about this child? */
2088 retval
= ptid_t (wait_retval
);
2090 else if (errno
== EINTR
)
2094 /* Unknown error from wait_for_stop. */
2095 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
2100 /* This long block is reached if either:
2101 a) the child was already stopped, or
2102 b) we successfully waited for the child with wait_for_stop.
2103 This block will analyze the /proc status, and translate it
2104 into a waitstatus for GDB.
2106 If we actually had to call wait because the /proc file
2107 is gone (child terminated), then we skip this block,
2108 because we already have a waitstatus. */
2110 flags
= proc_flags (pi
);
2111 why
= proc_why (pi
);
2112 what
= proc_what (pi
);
2114 if (flags
& (PR_STOPPED
| PR_ISTOP
))
2116 /* If it's running async (for single_thread control),
2117 set it back to normal again. */
2118 if (flags
& PR_ASYNC
)
2119 if (!proc_unset_async (pi
))
2120 proc_error (pi
, "target_wait, unset_async", __LINE__
);
2123 proc_prettyprint_why (why
, what
, 1);
2125 /* The 'pid' we will return to GDB is composed of
2126 the process ID plus the lwp ID. */
2127 retval
= ptid_t (pi
->pid
, proc_get_current_thread (pi
), 0);
2131 wstat
= (what
<< 8) | 0177;
2134 if (what
== SYS_lwp_exit
)
2136 if (print_thread_events
)
2137 printf_unfiltered (_("[%s exited]\n"),
2138 target_pid_to_str (retval
).c_str ());
2139 delete_thread (find_thread_ptid (this, retval
));
2140 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2143 else if (what
== SYS_exit
)
2145 struct inferior
*inf
;
2147 /* Handle SYS_exit call only. */
2148 /* Stopped at entry to SYS_exit.
2149 Make it runnable, resume it, then use
2150 the wait system call to get its exit code.
2151 Proc_run_process always clears the current
2153 Then return its exit status. */
2154 pi
->status_valid
= 0;
2156 /* FIXME: what we should do is return
2157 TARGET_WAITKIND_SPURIOUS. */
2158 if (!proc_run_process (pi
, 0, 0))
2159 proc_error (pi
, "target_wait, run_process", __LINE__
);
2161 inf
= find_inferior_pid (this, pi
->pid
);
2162 if (inf
->attach_flag
)
2164 /* Don't call wait: simulate waiting for exit,
2165 return a "success" exit code. Bogus: what if
2166 it returns something else? */
2168 retval
= inferior_ptid
; /* ? ? ? */
2172 int temp
= ::wait (&wstat
);
2174 /* FIXME: shouldn't I make sure I get the right
2175 event from the right process? If (for
2176 instance) I have killed an earlier inferior
2177 process but failed to clean up after it
2178 somehow, I could get its termination event
2181 /* If wait returns -1, that's what we return
2184 retval
= ptid_t (temp
);
2189 printf_filtered (_("procfs: trapped on entry to "));
2190 proc_prettyprint_syscall (proc_what (pi
), 0);
2191 printf_filtered ("\n");
2193 long i
, nsysargs
, *sysargs
;
2195 nsysargs
= proc_nsysarg (pi
);
2196 sysargs
= proc_sysargs (pi
);
2198 if (nsysargs
> 0 && sysargs
!= NULL
)
2200 printf_filtered (_("%ld syscall arguments:\n"),
2202 for (i
= 0; i
< nsysargs
; i
++)
2203 printf_filtered ("#%ld: 0x%08lx\n",
2209 /* How to exit gracefully, returning "unknown
2211 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2212 return inferior_ptid
;
2216 /* How to keep going without returning to wfi: */
2217 target_continue_no_signal (ptid
);
2223 if (what
== SYS_execve
)
2225 /* Hopefully this is our own "fork-child" execing
2226 the real child. Hoax this event into a trap, and
2227 GDB will see the child about to execute its start
2229 wstat
= (SIGTRAP
<< 8) | 0177;
2231 else if (what
== SYS_lwp_create
)
2233 /* This syscall is somewhat like fork/exec. We
2234 will get the event twice: once for the parent
2235 LWP, and once for the child. We should already
2236 know about the parent LWP, but the child will
2237 be new to us. So, whenever we get this event,
2238 if it represents a new thread, simply add the
2239 thread to the list. */
2241 /* If not in procinfo list, add it. */
2242 temp_tid
= proc_get_current_thread (pi
);
2243 if (!find_procinfo (pi
->pid
, temp_tid
))
2244 create_procinfo (pi
->pid
, temp_tid
);
2246 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2247 /* If not in GDB's thread list, add it. */
2248 if (!in_thread_list (this, temp_ptid
))
2249 add_thread (this, temp_ptid
);
2251 /* Return to WFI, but tell it to immediately resume. */
2252 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2253 return inferior_ptid
;
2255 else if (what
== SYS_lwp_exit
)
2257 if (print_thread_events
)
2258 printf_unfiltered (_("[%s exited]\n"),
2259 target_pid_to_str (retval
).c_str ());
2260 delete_thread (find_thread_ptid (this, retval
));
2261 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2266 printf_filtered (_("procfs: trapped on exit from "));
2267 proc_prettyprint_syscall (proc_what (pi
), 0);
2268 printf_filtered ("\n");
2270 long i
, nsysargs
, *sysargs
;
2272 nsysargs
= proc_nsysarg (pi
);
2273 sysargs
= proc_sysargs (pi
);
2275 if (nsysargs
> 0 && sysargs
!= NULL
)
2277 printf_filtered (_("%ld syscall arguments:\n"),
2279 for (i
= 0; i
< nsysargs
; i
++)
2280 printf_filtered ("#%ld: 0x%08lx\n",
2284 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2285 return inferior_ptid
;
2290 wstat
= (SIGSTOP
<< 8) | 0177;
2295 printf_filtered (_("Retry #%d:\n"), retry
);
2296 pi
->status_valid
= 0;
2301 /* If not in procinfo list, add it. */
2302 temp_tid
= proc_get_current_thread (pi
);
2303 if (!find_procinfo (pi
->pid
, temp_tid
))
2304 create_procinfo (pi
->pid
, temp_tid
);
2306 /* If not in GDB's thread list, add it. */
2307 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2308 if (!in_thread_list (this, temp_ptid
))
2309 add_thread (this, temp_ptid
);
2311 status
->kind
= TARGET_WAITKIND_STOPPED
;
2312 status
->value
.sig
= GDB_SIGNAL_0
;
2317 wstat
= (what
<< 8) | 0177;
2321 int signo
= pi
->prstatus
.pr_lwp
.pr_info
.si_signo
;
2323 wstat
= (signo
<< 8) | 0177;
2326 default: /* switch (why) unmatched */
2327 printf_filtered ("procfs:%d -- ", __LINE__
);
2328 printf_filtered (_("child stopped for unknown reason:\n"));
2329 proc_prettyprint_why (why
, what
, 1);
2330 error (_("... giving up..."));
2333 /* Got this far without error: If retval isn't in the
2334 threads database, add it. */
2335 if (retval
.pid () > 0
2336 && retval
!= inferior_ptid
2337 && !in_thread_list (this, retval
))
2339 /* We have a new thread. We need to add it both to
2340 GDB's list and to our own. If we don't create a
2341 procinfo, resume may be unhappy later. */
2342 add_thread (this, retval
);
2343 if (find_procinfo (retval
.pid (),
2344 retval
.lwp ()) == NULL
)
2345 create_procinfo (retval
.pid (),
2349 else /* Flags do not indicate STOPPED. */
2351 /* surely this can't happen... */
2352 printf_filtered ("procfs:%d -- process not stopped.\n",
2354 proc_prettyprint_flags (flags
, 1);
2355 error (_("procfs: ...giving up..."));
2360 store_waitstatus (status
, wstat
);
2366 /* Perform a partial transfer to/from the specified object. For
2367 memory transfers, fall back to the old memory xfer functions. */
2369 enum target_xfer_status
2370 procfs_target::xfer_partial (enum target_object object
,
2371 const char *annex
, gdb_byte
*readbuf
,
2372 const gdb_byte
*writebuf
, ULONGEST offset
,
2373 ULONGEST len
, ULONGEST
*xfered_len
)
2377 case TARGET_OBJECT_MEMORY
:
2378 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
2380 case TARGET_OBJECT_AUXV
:
2381 return memory_xfer_auxv (this, object
, annex
, readbuf
, writebuf
,
2382 offset
, len
, xfered_len
);
2385 return this->beneath ()->xfer_partial (object
, annex
,
2386 readbuf
, writebuf
, offset
, len
,
2391 /* Helper for procfs_xfer_partial that handles memory transfers.
2392 Arguments are like target_xfer_partial. */
2394 static enum target_xfer_status
2395 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
2396 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
2401 /* Find procinfo for main process. */
2402 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2403 if (pi
->as_fd
== 0 && open_procinfo_files (pi
, FD_AS
) == 0)
2405 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
2406 return TARGET_XFER_E_IO
;
2409 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
2410 return TARGET_XFER_E_IO
;
2412 if (writebuf
!= NULL
)
2414 PROCFS_NOTE ("write memory:\n");
2415 nbytes
= write (pi
->as_fd
, writebuf
, len
);
2419 PROCFS_NOTE ("read memory:\n");
2420 nbytes
= read (pi
->as_fd
, readbuf
, len
);
2423 return TARGET_XFER_E_IO
;
2424 *xfered_len
= nbytes
;
2425 return TARGET_XFER_OK
;
2428 /* Called by target_resume before making child runnable. Mark cached
2429 registers and status's invalid. If there are "dirty" caches that
2430 need to be written back to the child process, do that.
2432 File descriptors are also cached. As they are a limited resource,
2433 we cannot hold onto them indefinitely. However, as they are
2434 expensive to open, we don't want to throw them away
2435 indiscriminately either. As a compromise, we will keep the file
2436 descriptors for the parent process, but discard any file
2437 descriptors we may have accumulated for the threads.
2439 As this function is called by iterate_over_threads, it always
2440 returns zero (so that iterate_over_threads will keep
2444 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
2446 /* About to run the child; invalidate caches and do any other
2451 /* The presence of a parent indicates that this is an LWP.
2452 Close any file descriptors that it might have open.
2453 We don't do this to the master (parent) procinfo. */
2455 close_procinfo_files (pi
);
2457 pi
->gregs_valid
= 0;
2458 pi
->fpregs_valid
= 0;
2459 pi
->status_valid
= 0;
2460 pi
->threads_valid
= 0;
2465 /* Make the child process runnable. Normally we will then call
2466 procfs_wait and wait for it to stop again (unless gdb is async).
2468 If STEP is true, then arrange for the child to stop again after
2469 executing a single instruction. If SIGNO is zero, then cancel any
2470 pending signal; if non-zero, then arrange for the indicated signal
2471 to be delivered to the child when it runs. If PID is -1, then
2472 allow any child thread to run; if non-zero, then allow only the
2473 indicated thread to run. (not implemented yet). */
2476 procfs_target::resume (ptid_t ptid
, int step
, enum gdb_signal signo
)
2478 procinfo
*pi
, *thread
;
2481 /* FIXME: Check/reword. */
2483 /* prrun.prflags |= PRCFAULT; clear current fault.
2484 PRCFAULT may be replaced by a PCCFAULT call (proc_clear_current_fault)
2485 This basically leaves PRSTEP and PRCSIG.
2486 PRCSIG is like PCSSIG (proc_clear_current_signal).
2487 So basically PR_STEP is the sole argument that must be passed
2488 to proc_run_process. */
2490 /* Find procinfo for main process. */
2491 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2493 /* First cut: ignore pid argument. */
2496 /* Convert signal to host numbering. */
2497 if (signo
== 0 || (signo
== GDB_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
2500 native_signo
= gdb_signal_to_host (signo
);
2502 pi
->ignore_next_sigstop
= 0;
2504 /* Running the process voids all cached registers and status. */
2505 /* Void the threads' caches first. */
2506 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
2507 /* Void the process procinfo's caches. */
2508 invalidate_cache (NULL
, pi
, NULL
);
2510 if (ptid
.pid () != -1)
2512 /* Resume a specific thread, presumably suppressing the
2514 thread
= find_procinfo (ptid
.pid (), ptid
.lwp ());
2517 if (thread
->tid
!= 0)
2519 /* We're to resume a specific thread, and not the
2520 others. Set the child process's PR_ASYNC flag. */
2521 if (!proc_set_async (pi
))
2522 proc_error (pi
, "target_resume, set_async", __LINE__
);
2523 pi
= thread
; /* Substitute the thread's procinfo
2529 if (!proc_run_process (pi
, step
, native_signo
))
2532 warning (_("resume: target already running. "
2533 "Pretend to resume, and hope for the best!"));
2535 proc_error (pi
, "target_resume", __LINE__
);
2539 /* Set up to trace signals in the child process. */
2542 procfs_target::pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2545 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2548 prfillset (&signals
);
2550 for (signo
= 0; signo
< NSIG
; signo
++)
2552 int target_signo
= gdb_signal_from_host (signo
);
2553 if (target_signo
< pass_signals
.size () && pass_signals
[target_signo
])
2554 prdelset (&signals
, signo
);
2557 if (!proc_set_traced_signals (pi
, &signals
))
2558 proc_error (pi
, "pass_signals", __LINE__
);
2561 /* Print status information about the child process. */
2564 procfs_target::files_info ()
2566 struct inferior
*inf
= current_inferior ();
2568 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
2569 inf
->attach_flag
? "attached": "child",
2570 target_pid_to_str (inferior_ptid
).c_str ());
2573 /* Make it die. Wait for it to die. Clean up after it. Note: this
2574 should only be applied to the real process, not to an LWP, because
2575 of the check for parent-process. If we need this to work for an
2576 LWP, it needs some more logic. */
2579 unconditionally_kill_inferior (procinfo
*pi
)
2583 parent_pid
= proc_parent_pid (pi
);
2584 if (!proc_kill (pi
, SIGKILL
))
2585 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
2586 destroy_procinfo (pi
);
2588 /* If pi is GDB's child, wait for it to die. */
2589 if (parent_pid
== getpid ())
2590 /* FIXME: should we use waitpid to make sure we get the right event?
2591 Should we check the returned event? */
2596 ret
= waitpid (pi
->pid
, &status
, 0);
2603 /* We're done debugging it, and we want it to go away. Then we want
2604 GDB to forget all about it. */
2607 procfs_target::kill ()
2609 if (inferior_ptid
!= null_ptid
) /* ? */
2611 /* Find procinfo for main process. */
2612 procinfo
*pi
= find_procinfo (inferior_ptid
.pid (), 0);
2615 unconditionally_kill_inferior (pi
);
2616 target_mourn_inferior (inferior_ptid
);
2620 /* Forget we ever debugged this thing! */
2623 procfs_target::mourn_inferior ()
2627 if (inferior_ptid
!= null_ptid
)
2629 /* Find procinfo for main process. */
2630 pi
= find_procinfo (inferior_ptid
.pid (), 0);
2632 destroy_procinfo (pi
);
2635 generic_mourn_inferior ();
2637 maybe_unpush_target ();
2640 /* When GDB forks to create a runnable inferior process, this function
2641 is called on the parent side of the fork. It's job is to do
2642 whatever is necessary to make the child ready to be debugged, and
2643 then wait for the child to synchronize. */
2646 procfs_target::procfs_init_inferior (int pid
)
2652 pi
= create_procinfo (pid
, 0);
2654 perror (_("procfs: out of memory in 'init_inferior'"));
2656 if (!open_procinfo_files (pi
, FD_CTL
))
2657 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
2661 open_procinfo_files // done
2664 procfs_notice_signals
2669 /* If not stopped yet, wait for it to stop. */
2670 if (!(proc_flags (pi
) & PR_STOPPED
) && !(proc_wait_for_stop (pi
)))
2671 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
2673 /* Save some of the /proc state to be restored if we detach. */
2674 /* FIXME: Why? In case another debugger was debugging it?
2675 We're it's parent, for Ghu's sake! */
2676 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
2677 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
2678 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
2679 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
2680 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
2681 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
2682 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
2683 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
2684 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
2685 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
2687 fail
= procfs_debug_inferior (pi
);
2689 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
2691 /* FIXME: logically, we should really be turning OFF run-on-last-close,
2692 and possibly even turning ON kill-on-last-close at this point. But
2693 I can't make that change without careful testing which I don't have
2694 time to do right now... */
2695 /* Turn on run-on-last-close flag so that the child
2696 will die if GDB goes away for some reason. */
2697 if (!proc_set_run_on_last_close (pi
))
2698 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
2700 /* We now have have access to the lwpid of the main thread/lwp. */
2701 lwpid
= proc_get_current_thread (pi
);
2703 /* Create a procinfo for the main lwp. */
2704 create_procinfo (pid
, lwpid
);
2706 /* We already have a main thread registered in the thread table at
2707 this point, but it didn't have any lwp info yet. Notify the core
2708 about it. This changes inferior_ptid as well. */
2709 thread_change_ptid (this, ptid_t (pid
), ptid_t (pid
, lwpid
, 0));
2711 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
2714 /* When GDB forks to create a new process, this function is called on
2715 the child side of the fork before GDB exec's the user program. Its
2716 job is to make the child minimally debuggable, so that the parent
2717 GDB process can connect to the child and take over. This function
2718 should do only the minimum to make that possible, and to
2719 synchronize with the parent process. The parent process should
2720 take care of the details. */
2723 procfs_set_exec_trap (void)
2725 /* This routine called on the child side (inferior side)
2726 after GDB forks the inferior. It must use only local variables,
2727 because it may be sharing data space with its parent. */
2732 pi
= create_procinfo (getpid (), 0);
2734 perror_with_name (_("procfs: create_procinfo failed in child."));
2736 if (open_procinfo_files (pi
, FD_CTL
) == 0)
2738 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
2739 gdb_flush (gdb_stderr
);
2740 /* No need to call "dead_procinfo", because we're going to
2745 exitset
= XNEW (sysset_t
);
2746 premptyset (exitset
);
2747 praddset (exitset
, SYS_execve
);
2749 if (!proc_set_traced_sysexit (pi
, exitset
))
2751 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
2752 gdb_flush (gdb_stderr
);
2756 /* FIXME: should this be done in the parent instead? */
2757 /* Turn off inherit on fork flag so that all grand-children
2758 of gdb start with tracing flags cleared. */
2759 if (!proc_unset_inherit_on_fork (pi
))
2760 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
2762 /* Turn off run on last close flag, so that the child process
2763 cannot run away just because we close our handle on it.
2764 We want it to wait for the parent to attach. */
2765 if (!proc_unset_run_on_last_close (pi
))
2766 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
2768 /* FIXME: No need to destroy the procinfo --
2769 we have our own address space, and we're about to do an exec! */
2770 /*destroy_procinfo (pi);*/
2773 /* This function is called BEFORE gdb forks the inferior process. Its
2774 only real responsibility is to set things up for the fork, and tell
2775 GDB which two functions to call after the fork (one for the parent,
2776 and one for the child).
2778 This function does a complicated search for a unix shell program,
2779 which it then uses to parse arguments and environment variables to
2780 be sent to the child. I wonder whether this code could not be
2781 abstracted out and shared with other unix targets such as
2785 procfs_target::create_inferior (const char *exec_file
,
2786 const std::string
&allargs
,
2787 char **env
, int from_tty
)
2789 const char *shell_file
= get_shell ();
2793 if (strchr (shell_file
, '/') == NULL
)
2796 /* We will be looking down the PATH to find shell_file. If we
2797 just do this the normal way (via execlp, which operates by
2798 attempting an exec for each element of the PATH until it
2799 finds one which succeeds), then there will be an exec for
2800 each failed attempt, each of which will cause a PR_SYSEXIT
2801 stop, and we won't know how to distinguish the PR_SYSEXIT's
2802 for these failed execs with the ones for successful execs
2803 (whether the exec has succeeded is stored at that time in the
2804 carry bit or some such architecture-specific and
2805 non-ABI-specified place).
2807 So I can't think of anything better than to search the PATH
2808 now. This has several disadvantages: (1) There is a race
2809 condition; if we find a file now and it is deleted before we
2810 exec it, we lose, even if the deletion leaves a valid file
2811 further down in the PATH, (2) there is no way to know exactly
2812 what an executable (in the sense of "capable of being
2813 exec'd") file is. Using access() loses because it may lose
2814 if the caller is the superuser; failing to use it loses if
2815 there are ACLs or some such. */
2819 /* FIXME-maybe: might want "set path" command so user can change what
2820 path is used from within GDB. */
2821 const char *path
= getenv ("PATH");
2823 struct stat statbuf
;
2826 path
= "/bin:/usr/bin";
2828 tryname
= (char *) alloca (strlen (path
) + strlen (shell_file
) + 2);
2829 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
2831 p1
= strchr (p
, ':');
2836 strncpy (tryname
, p
, len
);
2837 tryname
[len
] = '\0';
2838 strcat (tryname
, "/");
2839 strcat (tryname
, shell_file
);
2840 if (access (tryname
, X_OK
) < 0)
2842 if (stat (tryname
, &statbuf
) < 0)
2844 if (!S_ISREG (statbuf
.st_mode
))
2845 /* We certainly need to reject directories. I'm not quite
2846 as sure about FIFOs, sockets, etc., but I kind of doubt
2847 that people want to exec() these things. */
2852 /* Not found. This must be an error rather than merely passing
2853 the file to execlp(), because execlp() would try all the
2854 exec()s, causing GDB to get confused. */
2855 error (_("procfs:%d -- Can't find shell %s in PATH"),
2856 __LINE__
, shell_file
);
2858 shell_file
= tryname
;
2861 if (!target_is_pushed (this))
2864 pid
= fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
2865 NULL
, NULL
, shell_file
, NULL
);
2867 /* We have something that executes now. We'll be running through
2868 the shell at this point (if startup-with-shell is true), but the
2869 pid shouldn't change. */
2870 thread_info
*thr
= add_thread_silent (this, ptid_t (pid
));
2871 switch_to_thread (thr
);
2873 procfs_init_inferior (pid
);
2876 /* An observer for the "inferior_created" event. */
2879 procfs_inferior_created (struct target_ops
*ops
, int from_tty
)
2883 /* Callback for update_thread_list. Calls "add_thread". */
2886 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
2888 ptid_t gdb_threadid
= ptid_t (pi
->pid
, thread
->tid
, 0);
2890 thread_info
*thr
= find_thread_ptid (&the_procfs_target
, gdb_threadid
);
2891 if (thr
== NULL
|| thr
->state
== THREAD_EXITED
)
2892 add_thread (&the_procfs_target
, gdb_threadid
);
2897 /* Query all the threads that the target knows about, and give them
2898 back to GDB to add to its list. */
2901 procfs_target::update_thread_list ()
2907 /* Find procinfo for main process. */
2908 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2909 proc_update_threads (pi
);
2910 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
2913 /* Return true if the thread is still 'alive'. This guy doesn't
2914 really seem to be doing his job. Got to investigate how to tell
2915 when a thread is really gone. */
2918 procfs_target::thread_alive (ptid_t ptid
)
2924 thread
= ptid
.lwp ();
2925 /* If I don't know it, it ain't alive! */
2926 pi
= find_procinfo (proc
, thread
);
2930 /* If I can't get its status, it ain't alive!
2931 What's more, I need to forget about it! */
2932 if (!proc_get_status (pi
))
2934 destroy_procinfo (pi
);
2937 /* I couldn't have got its status if it weren't alive, so it's
2942 /* Convert PTID to a string. */
2945 procfs_target::pid_to_str (ptid_t ptid
)
2947 if (ptid
.lwp () == 0)
2948 return string_printf ("process %d", ptid
.pid ());
2950 return string_printf ("LWP %ld", ptid
.lwp ());
2953 /* Accepts an integer PID; Returns a string representing a file that
2954 can be opened to get the symbols for the child process. */
2957 procfs_target::pid_to_exec_file (int pid
)
2959 static char buf
[PATH_MAX
];
2960 char name
[PATH_MAX
];
2962 /* Solaris 11 introduced /proc/<proc-id>/execname. */
2963 xsnprintf (name
, sizeof (name
), "/proc/%d/execname", pid
);
2964 scoped_fd
fd (gdb_open_cloexec (name
, O_RDONLY
, 0));
2965 if (fd
.get () < 0 || read (fd
.get (), buf
, PATH_MAX
- 1) < 0)
2967 /* If that fails, fall back to /proc/<proc-id>/path/a.out introduced in
2971 xsnprintf (name
, sizeof (name
), "/proc/%d/path/a.out", pid
);
2972 len
= readlink (name
, buf
, PATH_MAX
- 1);
2982 /* Insert a watchpoint. */
2985 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
2991 pi
= find_procinfo_or_die (ptid
.pid () == -1 ?
2992 inferior_ptid
.pid () : ptid
.pid (),
2995 /* Translate from GDB's flags to /proc's. */
2996 if (len
> 0) /* len == 0 means delete watchpoint. */
2998 switch (rwflag
) { /* FIXME: need an enum! */
2999 case hw_write
: /* default watchpoint (write) */
3002 case hw_read
: /* read watchpoint */
3005 case hw_access
: /* access watchpoint */
3006 pflags
= WA_READ
| WA_WRITE
;
3008 case hw_execute
: /* execution HW breakpoint */
3011 default: /* Something weird. Return error. */
3014 if (after
) /* Stop after r/w access is completed. */
3015 pflags
|= WA_TRAPAFTER
;
3018 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
3020 if (errno
== E2BIG
) /* Typical error for no resources. */
3021 return -1; /* fail */
3022 /* GDB may try to remove the same watchpoint twice.
3023 If a remove request returns no match, don't error. */
3024 if (errno
== ESRCH
&& len
== 0)
3025 return 0; /* ignore */
3026 proc_error (pi
, "set_watchpoint", __LINE__
);
3031 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
3032 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
3033 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
3037 procfs_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int othertype
)
3039 /* Due to the way that proc_set_watchpoint() is implemented, host
3040 and target pointers must be of the same size. If they are not,
3041 we can't use hardware watchpoints. This limitation is due to the
3042 fact that proc_set_watchpoint() calls
3043 procfs_address_to_host_pointer(); a close inspection of
3044 procfs_address_to_host_pointer will reveal that an internal error
3045 will be generated when the host and target pointer sizes are
3047 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
3049 if (sizeof (void *) != TYPE_LENGTH (ptr_type
))
3052 /* Other tests here??? */
3057 /* Returns non-zero if process is stopped on a hardware watchpoint
3058 fault, else returns zero. */
3061 procfs_target::stopped_by_watchpoint ()
3065 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3067 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3068 if (proc_why (pi
) == PR_FAULTED
)
3069 if (proc_what (pi
) == FLTWATCH
)
3074 /* Returns 1 if the OS knows the position of the triggered watchpoint,
3075 and sets *ADDR to that address. Returns 0 if OS cannot report that
3076 address. This function is only called if
3077 procfs_stopped_by_watchpoint returned 1, thus no further checks are
3078 done. The function also assumes that ADDR is not NULL. */
3081 procfs_target::stopped_data_address (CORE_ADDR
*addr
)
3085 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3086 return proc_watchpoint_address (pi
, addr
);
3090 procfs_target::insert_watchpoint (CORE_ADDR addr
, int len
,
3091 enum target_hw_bp_type type
,
3092 struct expression
*cond
)
3094 if (!target_have_steppable_watchpoint
3095 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
3096 /* When a hardware watchpoint fires off the PC will be left at
3097 the instruction following the one which caused the
3098 watchpoint. It will *NOT* be necessary for GDB to step over
3100 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 1);
3102 /* When a hardware watchpoint fires off the PC will be left at
3103 the instruction which caused the watchpoint. It will be
3104 necessary for GDB to step over the watchpoint. */
3105 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 0);
3109 procfs_target::remove_watchpoint (CORE_ADDR addr
, int len
,
3110 enum target_hw_bp_type type
,
3111 struct expression
*cond
)
3113 return procfs_set_watchpoint (inferior_ptid
, addr
, 0, 0, 0);
3117 procfs_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
3119 /* The man page for proc(4) on Solaris 2.6 and up says that the
3120 system can support "thousands" of hardware watchpoints, but gives
3121 no method for finding out how many; It doesn't say anything about
3122 the allowed size for the watched area either. So we just tell
3127 /* Memory Mappings Functions: */
3129 /* Call a callback function once for each mapping, passing it the
3130 mapping, an optional secondary callback function, and some optional
3131 opaque data. Quit and return the first non-zero value returned
3134 PI is the procinfo struct for the process to be mapped. FUNC is
3135 the callback function to be called by this iterator. DATA is the
3136 optional opaque data to be passed to the callback function.
3137 CHILD_FUNC is the optional secondary function pointer to be passed
3138 to the child function. Returns the first non-zero return value
3139 from the callback function, or zero. */
3142 iterate_over_mappings (procinfo
*pi
, find_memory_region_ftype child_func
,
3144 int (*func
) (struct prmap
*map
,
3145 find_memory_region_ftype child_func
,
3148 char pathname
[MAX_PROC_NAME_SIZE
];
3149 struct prmap
*prmaps
;
3150 struct prmap
*prmap
;
3155 /* Get the number of mappings, allocate space,
3156 and read the mappings into prmaps. */
3158 xsnprintf (pathname
, sizeof (pathname
), "/proc/%d/map", pi
->pid
);
3160 scoped_fd
map_fd (open (pathname
, O_RDONLY
));
3161 if (map_fd
.get () < 0)
3162 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
3164 /* Use stat to determine the file size, and compute
3165 the number of prmap_t objects it contains. */
3166 if (fstat (map_fd
.get (), &sbuf
) != 0)
3167 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
3169 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
3170 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
3171 if (read (map_fd
.get (), (char *) prmaps
, nmap
* sizeof (*prmaps
))
3172 != (nmap
* sizeof (*prmaps
)))
3173 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
3175 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
3177 funcstat
= (*func
) (prmap
, child_func
, data
);
3185 /* Implements the to_find_memory_regions method. Calls an external
3186 function for each memory region.
3187 Returns the integer value returned by the callback. */
3190 find_memory_regions_callback (struct prmap
*map
,
3191 find_memory_region_ftype func
, void *data
)
3193 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
3195 (map
->pr_mflags
& MA_READ
) != 0,
3196 (map
->pr_mflags
& MA_WRITE
) != 0,
3197 (map
->pr_mflags
& MA_EXEC
) != 0,
3198 1, /* MODIFIED is unknown, pass it as true. */
3202 /* External interface. Calls a callback function once for each
3203 mapped memory region in the child process, passing as arguments:
3205 CORE_ADDR virtual_address,
3207 int read, TRUE if region is readable by the child
3208 int write, TRUE if region is writable by the child
3209 int execute TRUE if region is executable by the child.
3211 Stops iterating and returns the first non-zero value returned by
3215 procfs_target::find_memory_regions (find_memory_region_ftype func
, void *data
)
3217 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3219 return iterate_over_mappings (pi
, func
, data
,
3220 find_memory_regions_callback
);
3223 /* Returns an ascii representation of a memory mapping's flags. */
3226 mappingflags (long flags
)
3228 static char asciiflags
[8];
3230 strcpy (asciiflags
, "-------");
3231 if (flags
& MA_STACK
)
3232 asciiflags
[1] = 's';
3233 if (flags
& MA_BREAK
)
3234 asciiflags
[2] = 'b';
3235 if (flags
& MA_SHARED
)
3236 asciiflags
[3] = 's';
3237 if (flags
& MA_READ
)
3238 asciiflags
[4] = 'r';
3239 if (flags
& MA_WRITE
)
3240 asciiflags
[5] = 'w';
3241 if (flags
& MA_EXEC
)
3242 asciiflags
[6] = 'x';
3243 return (asciiflags
);
3246 /* Callback function, does the actual work for 'info proc
3250 info_mappings_callback (struct prmap
*map
, find_memory_region_ftype ignore
,
3253 unsigned int pr_off
;
3255 pr_off
= (unsigned int) map
->pr_offset
;
3257 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
3258 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
3259 (unsigned long) map
->pr_vaddr
,
3260 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3261 (unsigned long) map
->pr_size
,
3263 mappingflags (map
->pr_mflags
));
3265 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
3266 (unsigned long) map
->pr_vaddr
,
3267 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3268 (unsigned long) map
->pr_size
,
3270 mappingflags (map
->pr_mflags
));
3275 /* Implement the "info proc mappings" subcommand. */
3278 info_proc_mappings (procinfo
*pi
, int summary
)
3281 return; /* No output for summary mode. */
3283 printf_filtered (_("Mapped address spaces:\n\n"));
3284 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
3285 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3292 printf_filtered (" %18s %18s %10s %10s %7s\n",
3299 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
3300 printf_filtered ("\n");
3303 /* Implement the "info proc" command. */
3306 procfs_target::info_proc (const char *args
, enum info_proc_what what
)
3308 procinfo
*process
= NULL
;
3309 procinfo
*thread
= NULL
;
3326 error (_("Not supported on this target."));
3329 gdb_argv
built_argv (args
);
3330 for (char *arg
: built_argv
)
3332 if (isdigit (arg
[0]))
3334 pid
= strtoul (arg
, &tmp
, 10);
3336 tid
= strtoul (++tmp
, NULL
, 10);
3338 else if (arg
[0] == '/')
3340 tid
= strtoul (arg
+ 1, NULL
, 10);
3344 procinfo_up temporary_procinfo
;
3346 pid
= inferior_ptid
.pid ();
3348 error (_("No current process: you must name one."));
3351 /* Have pid, will travel.
3352 First see if it's a process we're already debugging. */
3353 process
= find_procinfo (pid
, 0);
3354 if (process
== NULL
)
3356 /* No. So open a procinfo for it, but
3357 remember to close it again when finished. */
3358 process
= create_procinfo (pid
, 0);
3359 temporary_procinfo
.reset (process
);
3360 if (!open_procinfo_files (process
, FD_CTL
))
3361 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
3365 thread
= create_procinfo (pid
, tid
);
3369 printf_filtered (_("process %d flags:\n"), process
->pid
);
3370 proc_prettyprint_flags (proc_flags (process
), 1);
3371 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
3372 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
3373 if (proc_get_nthreads (process
) > 1)
3374 printf_filtered ("Process has %d threads.\n",
3375 proc_get_nthreads (process
));
3379 printf_filtered (_("thread %d flags:\n"), thread
->tid
);
3380 proc_prettyprint_flags (proc_flags (thread
), 1);
3381 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
3382 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
3386 info_proc_mappings (process
, 0);
3391 /* Modify the status of the system call identified by SYSCALLNUM in
3392 the set of syscalls that are currently traced/debugged.
3394 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
3395 will be updated. Otherwise, the exit syscalls set will be updated.
3397 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
3398 will be disabled. */
3401 proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
, int entry_or_exit
,
3402 int mode
, int from_tty
)
3406 if (entry_or_exit
== PR_SYSENTRY
)
3407 sysset
= proc_get_traced_sysentry (pi
, NULL
);
3409 sysset
= proc_get_traced_sysexit (pi
, NULL
);
3412 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
3414 if (mode
== FLAG_SET
)
3415 praddset (sysset
, syscallnum
);
3417 prdelset (sysset
, syscallnum
);
3419 if (entry_or_exit
== PR_SYSENTRY
)
3421 if (!proc_set_traced_sysentry (pi
, sysset
))
3422 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
3426 if (!proc_set_traced_sysexit (pi
, sysset
))
3427 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
3432 proc_trace_syscalls (const char *args
, int from_tty
, int entry_or_exit
, int mode
)
3436 if (inferior_ptid
.pid () <= 0)
3437 error (_("you must be debugging a process to use this command."));
3439 if (args
== NULL
|| args
[0] == 0)
3440 error_no_arg (_("system call to trace"));
3442 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3443 if (isdigit (args
[0]))
3445 const int syscallnum
= atoi (args
);
3447 proc_trace_syscalls_1 (pi
, syscallnum
, entry_or_exit
, mode
, from_tty
);
3452 proc_trace_sysentry_cmd (const char *args
, int from_tty
)
3454 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
3458 proc_trace_sysexit_cmd (const char *args
, int from_tty
)
3460 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
3464 proc_untrace_sysentry_cmd (const char *args
, int from_tty
)
3466 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
3470 proc_untrace_sysexit_cmd (const char *args
, int from_tty
)
3472 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
3475 void _initialize_procfs ();
3477 _initialize_procfs ()
3479 gdb::observers::inferior_created
.attach (procfs_inferior_created
);
3481 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
3482 _("Give a trace of entries into the syscall."));
3483 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
3484 _("Give a trace of exits from the syscall."));
3485 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
3486 _("Cancel a trace of entries into the syscall."));
3487 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
3488 _("Cancel a trace of exits from the syscall."));
3490 add_inf_child_target (&the_procfs_target
);
3493 /* =================== END, GDB "MODULE" =================== */
3497 /* miscellaneous stubs: */
3499 /* The following satisfy a few random symbols mostly created by the
3500 solaris threads implementation, which I will chase down later. */
3502 /* Return a pid for which we guarantee we will be able to find a
3506 procfs_first_available (void)
3508 return ptid_t (procinfo_list
? procinfo_list
->pid
: -1);
3511 /* =================== GCORE .NOTE "MODULE" =================== */
3514 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3515 char *note_data
, int *note_size
,
3516 enum gdb_signal stop_signal
)
3518 struct regcache
*regcache
= get_thread_regcache (&the_procfs_target
, ptid
);
3519 gdb_gregset_t gregs
;
3520 gdb_fpregset_t fpregs
;
3521 unsigned long merged_pid
;
3523 merged_pid
= ptid
.lwp () << 16 | ptid
.pid ();
3525 /* This part is the old method for fetching registers.
3526 It should be replaced by the newer one using regsets
3527 once it is implemented in this platform:
3528 gdbarch_iterate_over_regset_sections(). */
3530 target_fetch_registers (regcache
, -1);
3532 fill_gregset (regcache
, &gregs
, -1);
3533 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
3539 fill_fpregset (regcache
, &fpregs
, -1);
3540 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3549 struct procfs_corefile_thread_data
{
3553 enum gdb_signal stop_signal
;
3557 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
3559 struct procfs_corefile_thread_data
*args
3560 = (struct procfs_corefile_thread_data
*) data
;
3564 ptid_t ptid
= ptid_t (pi
->pid
, thread
->tid
, 0);
3566 args
->note_data
= procfs_do_thread_registers (args
->obfd
, ptid
,
3575 find_signalled_thread (struct thread_info
*info
, void *data
)
3577 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
3578 && info
->ptid
.pid () == inferior_ptid
.pid ())
3584 static enum gdb_signal
3585 find_stop_signal (void)
3587 struct thread_info
*info
=
3588 iterate_over_threads (find_signalled_thread
, NULL
);
3591 return info
->suspend
.stop_signal
;
3593 return GDB_SIGNAL_0
;
3597 procfs_target::make_corefile_notes (bfd
*obfd
, int *note_size
)
3599 gdb_gregset_t gregs
;
3600 char fname
[16] = {'\0'};
3601 char psargs
[80] = {'\0'};
3602 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3603 char *note_data
= NULL
;
3604 const char *inf_args
;
3605 struct procfs_corefile_thread_data thread_args
;
3606 enum gdb_signal stop_signal
;
3608 if (get_exec_file (0))
3610 strncpy (fname
, lbasename (get_exec_file (0)), sizeof (fname
));
3611 fname
[sizeof (fname
) - 1] = 0;
3612 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3613 psargs
[sizeof (psargs
) - 1] = 0;
3615 inf_args
= get_inferior_args ();
3616 if (inf_args
&& *inf_args
3617 && (strlen (inf_args
)
3618 < ((int) sizeof (psargs
) - (int) strlen (psargs
))))
3620 strncat (psargs
, " ",
3621 sizeof (psargs
) - strlen (psargs
));
3622 strncat (psargs
, inf_args
,
3623 sizeof (psargs
) - strlen (psargs
));
3627 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3633 stop_signal
= find_stop_signal ();
3635 fill_gregset (get_current_regcache (), &gregs
, -1);
3636 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
3637 inferior_ptid
.pid (),
3638 stop_signal
, &gregs
);
3640 thread_args
.obfd
= obfd
;
3641 thread_args
.note_data
= note_data
;
3642 thread_args
.note_size
= note_size
;
3643 thread_args
.stop_signal
= stop_signal
;
3644 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
,
3646 note_data
= thread_args
.note_data
;
3648 gdb::optional
<gdb::byte_vector
> auxv
=
3649 target_read_alloc (current_top_target (), TARGET_OBJECT_AUXV
, NULL
);
3650 if (auxv
&& !auxv
->empty ())
3651 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3652 "CORE", NT_AUXV
, auxv
->data (),
3657 /* =================== END GCORE .NOTE "MODULE" =================== */