1 /* Machine independent support for Solaris /proc (process file system) for GDB.
3 Copyright (C) 1999-2021 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 #include <sys/procfs.h>
37 #include <sys/fault.h>
38 #include <sys/syscall.h>
39 #include "gdbsupport/gdb_wait.h"
46 #include "observable.h"
47 #include "gdbsupport/scoped_fd.h"
48 #include "gdbsupport/pathstuff.h"
50 /* This module provides the interface between GDB and the
51 /proc file system, which is used on many versions of Unix
52 as a means for debuggers to control other processes.
54 /proc works by imitating a file system: you open a simulated file
55 that represents the process you wish to interact with, and perform
56 operations on that "file" in order to examine or change the state
59 The most important thing to know about /proc and this module is
60 that there are two very different interfaces to /proc:
62 One that uses the ioctl system call, and another that uses read
63 and write system calls.
65 This module supports only the Solaris version of the read/write
68 #include <sys/types.h>
69 #include <dirent.h> /* opendir/readdir, for listing the LWP's */
71 #include <fcntl.h> /* for O_RDONLY */
72 #include <unistd.h> /* for "X_OK" */
73 #include <sys/stat.h> /* for struct stat */
75 /* Note: procfs-utils.h must be included after the above system header
76 files, because it redefines various system calls using macros.
77 This may be incompatible with the prototype declarations. */
79 #include "proc-utils.h"
81 /* Prototypes for supply_gregset etc. */
84 /* =================== TARGET_OPS "MODULE" =================== */
86 /* This module defines the GDB target vector and its methods. */
89 static enum target_xfer_status
procfs_xfer_memory (gdb_byte
*,
94 class procfs_target final
: public inf_child_target
97 void create_inferior (const char *, const std::string
&,
98 char **, int) override
;
100 void kill () override
;
102 void mourn_inferior () override
;
104 void attach (const char *, int) override
;
105 void detach (inferior
*inf
, int) override
;
107 void resume (ptid_t
, int, enum gdb_signal
) override
;
108 ptid_t
wait (ptid_t
, struct target_waitstatus
*, target_wait_flags
) override
;
110 void fetch_registers (struct regcache
*, int) override
;
111 void store_registers (struct regcache
*, int) override
;
113 enum target_xfer_status
xfer_partial (enum target_object object
,
116 const gdb_byte
*writebuf
,
117 ULONGEST offset
, ULONGEST len
,
118 ULONGEST
*xfered_len
) override
;
120 void pass_signals (gdb::array_view
<const unsigned char>) override
;
122 void files_info () override
;
124 void update_thread_list () override
;
126 bool thread_alive (ptid_t ptid
) override
;
128 std::string
pid_to_str (ptid_t
) override
;
130 char *pid_to_exec_file (int pid
) override
;
132 thread_control_capabilities
get_thread_control_capabilities () override
133 { return tc_schedlock
; }
135 /* find_memory_regions support method for gcore */
136 int find_memory_regions (find_memory_region_ftype func
, void *data
)
139 gdb::unique_xmalloc_ptr
<char> make_corefile_notes (bfd
*, int *) override
;
141 bool info_proc (const char *, enum info_proc_what
) override
;
143 #if PR_MODEL_NATIVE == PR_MODEL_LP64
144 int auxv_parse (gdb_byte
**readptr
,
145 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
149 bool stopped_by_watchpoint () override
;
151 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
152 struct expression
*) override
;
154 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
155 struct expression
*) override
;
157 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
159 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
160 bool stopped_data_address (CORE_ADDR
*) override
;
162 void procfs_init_inferior (int pid
);
165 static procfs_target the_procfs_target
;
167 #if PR_MODEL_NATIVE == PR_MODEL_LP64
168 /* When GDB is built as 64-bit application on Solaris, the auxv data
169 is presented in 64-bit format. We need to provide a custom parser
172 procfs_target::auxv_parse (gdb_byte
**readptr
,
173 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
175 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
176 gdb_byte
*ptr
= *readptr
;
181 if (endptr
- ptr
< 8 * 2)
184 *typep
= extract_unsigned_integer (ptr
, 4, byte_order
);
186 /* The size of data is always 64-bit. If the application is 32-bit,
187 it will be zero extended, as expected. */
188 *valp
= extract_unsigned_integer (ptr
, 8, byte_order
);
196 /* =================== END, TARGET_OPS "MODULE" =================== */
198 /* =================== STRUCT PROCINFO "MODULE" =================== */
200 /* FIXME: this comment will soon be out of date W.R.T. threads. */
202 /* The procinfo struct is a wrapper to hold all the state information
203 concerning a /proc process. There should be exactly one procinfo
204 for each process, and since GDB currently can debug only one
205 process at a time, that means there should be only one procinfo.
206 All of the LWP's of a process can be accessed indirectly thru the
207 single process procinfo.
209 However, against the day when GDB may debug more than one process,
210 this data structure is kept in a list (which for now will hold no
211 more than one member), and many functions will have a pointer to a
212 procinfo as an argument.
214 There will be a separate procinfo structure for use by the (not yet
215 implemented) "info proc" command, so that we can print useful
216 information about any random process without interfering with the
217 inferior's procinfo information. */
219 /* format strings for /proc paths */
220 #define CTL_PROC_NAME_FMT "/proc/%d/ctl"
221 #define AS_PROC_NAME_FMT "/proc/%d/as"
222 #define MAP_PROC_NAME_FMT "/proc/%d/map"
223 #define STATUS_PROC_NAME_FMT "/proc/%d/status"
224 #define MAX_PROC_NAME_SIZE sizeof("/proc/999999/lwp/0123456789/lwpstatus")
226 typedef struct procinfo
{
227 struct procinfo
*next
;
228 int pid
; /* Process ID */
229 int tid
; /* Thread/LWP id */
233 int ignore_next_sigstop
;
235 int ctl_fd
; /* File descriptor for /proc control file */
236 int status_fd
; /* File descriptor for /proc status file */
237 int as_fd
; /* File descriptor for /proc as file */
239 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
241 fltset_t saved_fltset
; /* Saved traced hardware fault set */
242 sigset_t saved_sigset
; /* Saved traced signal set */
243 sigset_t saved_sighold
; /* Saved held signal set */
244 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
245 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
247 pstatus_t prstatus
; /* Current process status info */
249 struct procinfo
*thread_list
;
251 int status_valid
: 1;
253 int fpregs_valid
: 1;
254 int threads_valid
: 1;
257 static char errmsg
[128]; /* shared error msg buffer */
259 /* Function prototypes for procinfo module: */
261 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
262 static procinfo
*find_procinfo (int pid
, int tid
);
263 static procinfo
*create_procinfo (int pid
, int tid
);
264 static void destroy_procinfo (procinfo
*p
);
265 static void dead_procinfo (procinfo
*p
, const char *msg
, int killp
);
266 static int open_procinfo_files (procinfo
*p
, int which
);
267 static void close_procinfo_files (procinfo
*p
);
269 static int iterate_over_mappings
270 (procinfo
*pi
, find_memory_region_ftype child_func
, void *data
,
271 int (*func
) (struct prmap
*map
, find_memory_region_ftype child_func
,
274 /* The head of the procinfo list: */
275 static procinfo
*procinfo_list
;
277 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
281 find_procinfo (int pid
, int tid
)
285 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
292 /* Don't check threads_valid. If we're updating the
293 thread_list, we want to find whatever threads are already
294 here. This means that in general it is the caller's
295 responsibility to check threads_valid and update before
296 calling find_procinfo, if the caller wants to find a new
299 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
307 /* Calls find_procinfo, but errors on failure. */
310 find_procinfo_or_die (int pid
, int tid
)
312 procinfo
*pi
= find_procinfo (pid
, tid
);
317 error (_("procfs: couldn't find pid %d "
318 "(kernel thread %d) in procinfo list."),
321 error (_("procfs: couldn't find pid %d in procinfo list."), pid
);
326 /* Wrapper for `open'. The appropriate open call is attempted; if
327 unsuccessful, it will be retried as many times as needed for the
328 EAGAIN and EINTR conditions.
330 For other conditions, retry the open a limited number of times. In
331 addition, a short sleep is imposed prior to retrying the open. The
332 reason for this sleep is to give the kernel a chance to catch up
333 and create the file in question in the event that GDB "wins" the
334 race to open a file before the kernel has created it. */
337 open_with_retry (const char *pathname
, int flags
)
339 int retries_remaining
, status
;
341 retries_remaining
= 2;
345 status
= open (pathname
, flags
);
347 if (status
>= 0 || retries_remaining
== 0)
349 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
359 /* Open the file descriptor for the process or LWP. We only open the
360 control file descriptor; the others are opened lazily as needed.
361 Returns the file descriptor, or zero for failure. */
363 enum { FD_CTL
, FD_STATUS
, FD_AS
};
366 open_procinfo_files (procinfo
*pi
, int which
)
368 char tmp
[MAX_PROC_NAME_SIZE
];
371 /* This function is getting ALMOST long enough to break up into
372 several. Here is some rationale:
374 There are several file descriptors that may need to be open
375 for any given process or LWP. The ones we're interested in are:
376 - control (ctl) write-only change the state
377 - status (status) read-only query the state
378 - address space (as) read/write access memory
379 - map (map) read-only virtual addr map
380 Most of these are opened lazily as they are needed.
381 The pathnames for the 'files' for an LWP look slightly
382 different from those of a first-class process:
383 Pathnames for a process (<proc-id>):
385 /proc/<proc-id>/status
388 Pathnames for an LWP (lwp-id):
389 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
390 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
391 An LWP has no map or address space file descriptor, since
392 the memory map and address space are shared by all LWPs. */
394 /* In this case, there are several different file descriptors that
395 we might be asked to open. The control file descriptor will be
396 opened early, but the others will be opened lazily as they are
399 strcpy (tmp
, pi
->pathname
);
400 switch (which
) { /* Which file descriptor to open? */
403 strcat (tmp
, "/lwpctl");
405 strcat (tmp
, "/ctl");
406 fd
= open_with_retry (tmp
, O_WRONLY
);
413 return 0; /* There is no 'as' file descriptor for an lwp. */
415 fd
= open_with_retry (tmp
, O_RDWR
);
422 strcat (tmp
, "/lwpstatus");
424 strcat (tmp
, "/status");
425 fd
= open_with_retry (tmp
, O_RDONLY
);
431 return 0; /* unknown file descriptor */
434 return 1; /* success */
437 /* Allocate a data structure and link it into the procinfo list.
438 First tries to find a pre-existing one (FIXME: why?). Returns the
439 pointer to new procinfo struct. */
442 create_procinfo (int pid
, int tid
)
444 procinfo
*pi
, *parent
= NULL
;
446 pi
= find_procinfo (pid
, tid
);
448 return pi
; /* Already exists, nothing to do. */
450 /* Find parent before doing malloc, to save having to cleanup. */
452 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
454 doesn't exist yet? */
456 pi
= XNEW (procinfo
);
457 memset (pi
, 0, sizeof (procinfo
));
461 pi
->saved_entryset
= XNEW (sysset_t
);
462 pi
->saved_exitset
= XNEW (sysset_t
);
464 /* Chain into list. */
467 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), "/proc/%d", pid
);
468 pi
->next
= procinfo_list
;
473 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), "/proc/%d/lwp/%d",
475 pi
->next
= parent
->thread_list
;
476 parent
->thread_list
= pi
;
481 /* Close all file descriptors associated with the procinfo. */
484 close_procinfo_files (procinfo
*pi
)
490 if (pi
->status_fd
> 0)
491 close (pi
->status_fd
);
492 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
495 /* Destructor function. Close, unlink and deallocate the object. */
498 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
502 /* Step one: unlink the procinfo from its list. */
506 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
509 ptr
->next
= pi
->next
;
513 /* Step two: close any open file descriptors. */
514 close_procinfo_files (pi
);
516 /* Step three: free the memory. */
517 xfree (pi
->saved_entryset
);
518 xfree (pi
->saved_exitset
);
523 destroy_procinfo (procinfo
*pi
)
527 if (pi
->tid
!= 0) /* Destroy a thread procinfo. */
529 tmp
= find_procinfo (pi
->pid
, 0); /* Find the parent process. */
530 destroy_one_procinfo (&tmp
->thread_list
, pi
);
532 else /* Destroy a process procinfo and all its threads. */
534 /* First destroy the children, if any; */
535 while (pi
->thread_list
!= NULL
)
536 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
537 /* Then destroy the parent. Genocide!!! */
538 destroy_one_procinfo (&procinfo_list
, pi
);
542 /* A deleter that calls destroy_procinfo. */
543 struct procinfo_deleter
545 void operator() (procinfo
*pi
) const
547 destroy_procinfo (pi
);
551 typedef std::unique_ptr
<procinfo
, procinfo_deleter
> procinfo_up
;
553 enum { NOKILL
, KILL
};
555 /* To be called on a non_recoverable error for a procinfo. Prints
556 error messages, optionally sends a SIGKILL to the process, then
557 destroys the data structure. */
560 dead_procinfo (procinfo
*pi
, const char *msg
, int kill_p
)
565 print_sys_errmsg (pi
->pathname
, errno
);
568 xsnprintf (procfile
, sizeof (procfile
), "process %d", pi
->pid
);
569 print_sys_errmsg (procfile
, errno
);
572 kill (pi
->pid
, SIGKILL
);
574 destroy_procinfo (pi
);
578 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
580 /* =================== /proc "MODULE" =================== */
582 /* This "module" is the interface layer between the /proc system API
583 and the gdb target vector functions. This layer consists of access
584 functions that encapsulate each of the basic operations that we
585 need to use from the /proc API.
587 The main motivation for this layer is to hide the fact that there
588 were two very different implementations of the /proc API. */
590 static long proc_flags (procinfo
*pi
);
591 static int proc_why (procinfo
*pi
);
592 static int proc_what (procinfo
*pi
);
593 static int proc_set_current_signal (procinfo
*pi
, int signo
);
594 static int proc_get_current_thread (procinfo
*pi
);
595 static int proc_iterate_over_threads
597 int (*func
) (procinfo
*, procinfo
*, void *),
601 proc_warn (procinfo
*pi
, const char *func
, int line
)
603 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
604 func
, line
, pi
->pathname
);
605 print_sys_errmsg (errmsg
, errno
);
609 proc_error (procinfo
*pi
, const char *func
, int line
)
611 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
612 func
, line
, pi
->pathname
);
613 perror_with_name (errmsg
);
616 /* Updates the status struct in the procinfo. There is a 'valid'
617 flag, to let other functions know when this function needs to be
618 called (so the status is only read when it is needed). The status
619 file descriptor is also only opened when it is needed. Returns
620 non-zero for success, zero for failure. */
623 proc_get_status (procinfo
*pi
)
625 /* Status file descriptor is opened "lazily". */
626 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
628 pi
->status_valid
= 0;
632 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
633 pi
->status_valid
= 0; /* fail */
636 /* Sigh... I have to read a different data structure,
637 depending on whether this is a main process or an LWP. */
639 pi
->status_valid
= (read (pi
->status_fd
,
640 (char *) &pi
->prstatus
.pr_lwp
,
641 sizeof (lwpstatus_t
))
642 == sizeof (lwpstatus_t
));
645 pi
->status_valid
= (read (pi
->status_fd
,
646 (char *) &pi
->prstatus
,
648 == sizeof (pstatus_t
));
652 if (pi
->status_valid
)
654 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
657 proc_get_current_thread (pi
));
660 /* The status struct includes general regs, so mark them valid too. */
661 pi
->gregs_valid
= pi
->status_valid
;
662 /* In the read/write multiple-fd model, the status struct includes
663 the fp regs too, so mark them valid too. */
664 pi
->fpregs_valid
= pi
->status_valid
;
665 return pi
->status_valid
; /* True if success, false if failure. */
668 /* Returns the process flags (pr_flags field). */
671 proc_flags (procinfo
*pi
)
673 if (!pi
->status_valid
)
674 if (!proc_get_status (pi
))
675 return 0; /* FIXME: not a good failure value (but what is?) */
677 return pi
->prstatus
.pr_lwp
.pr_flags
;
680 /* Returns the pr_why field (why the process stopped). */
683 proc_why (procinfo
*pi
)
685 if (!pi
->status_valid
)
686 if (!proc_get_status (pi
))
687 return 0; /* FIXME: not a good failure value (but what is?) */
689 return pi
->prstatus
.pr_lwp
.pr_why
;
692 /* Returns the pr_what field (details of why the process stopped). */
695 proc_what (procinfo
*pi
)
697 if (!pi
->status_valid
)
698 if (!proc_get_status (pi
))
699 return 0; /* FIXME: not a good failure value (but what is?) */
701 return pi
->prstatus
.pr_lwp
.pr_what
;
704 /* This function is only called when PI is stopped by a watchpoint.
705 Assuming the OS supports it, write to *ADDR the data address which
706 triggered it and return 1. Return 0 if it is not possible to know
710 proc_watchpoint_address (procinfo
*pi
, CORE_ADDR
*addr
)
712 if (!pi
->status_valid
)
713 if (!proc_get_status (pi
))
716 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
717 builtin_type (target_gdbarch ())->builtin_data_ptr
,
718 (gdb_byte
*) &pi
->prstatus
.pr_lwp
.pr_info
.si_addr
);
722 /* Returns the pr_nsysarg field (number of args to the current
726 proc_nsysarg (procinfo
*pi
)
728 if (!pi
->status_valid
)
729 if (!proc_get_status (pi
))
732 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
735 /* Returns the pr_sysarg field (pointer to the arguments of current
739 proc_sysargs (procinfo
*pi
)
741 if (!pi
->status_valid
)
742 if (!proc_get_status (pi
))
745 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
748 /* Set or reset any of the following process flags:
749 PR_FORK -- forked child will inherit trace flags
750 PR_RLC -- traced process runs when last /proc file closed.
751 PR_KLC -- traced process is killed when last /proc file closed.
752 PR_ASYNC -- LWP's get to run/stop independently.
754 This function is done using read/write [PCSET/PCRESET/PCUNSET].
758 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
759 mode -- 1 for set, 0 for reset.
761 Returns non-zero for success, zero for failure. */
763 enum { FLAG_RESET
, FLAG_SET
};
766 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
768 long win
= 0; /* default to fail */
770 /* These operations affect the process as a whole, and applying them
771 to an individual LWP has the same meaning as applying them to the
772 main process. Therefore, if we're ever called with a pointer to
773 an LWP's procinfo, let's substitute the process's procinfo and
774 avoid opening the LWP's file descriptor unnecessarily. */
777 pi
= find_procinfo_or_die (pi
->pid
, 0);
781 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC). */
783 else /* Reset the flag. */
787 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
789 /* The above operation renders the procinfo's cached pstatus
791 pi
->status_valid
= 0;
794 warning (_("procfs: modify_flag failed to turn %s %s"),
795 flag
== PR_FORK
? "PR_FORK" :
796 flag
== PR_RLC
? "PR_RLC" :
797 flag
== PR_ASYNC
? "PR_ASYNC" :
798 flag
== PR_KLC
? "PR_KLC" :
800 mode
== FLAG_RESET
? "off" : "on");
805 /* Set the run_on_last_close flag. Process with all threads will
806 become runnable when debugger closes all /proc fds. Returns
807 non-zero for success, zero for failure. */
810 proc_set_run_on_last_close (procinfo
*pi
)
812 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
815 /* Reset the run_on_last_close flag. The process will NOT become
816 runnable when debugger closes its file handles. Returns non-zero
817 for success, zero for failure. */
820 proc_unset_run_on_last_close (procinfo
*pi
)
822 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
825 /* Reset inherit_on_fork flag. If the process forks a child while we
826 are registered for events in the parent, then we will NOT receive
827 events from the child. Returns non-zero for success, zero for
831 proc_unset_inherit_on_fork (procinfo
*pi
)
833 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
836 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
837 (signal etc.), the remaining LWPs will continue to run. Returns
838 non-zero for success, zero for failure. */
841 proc_set_async (procinfo
*pi
)
843 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
846 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
847 (signal etc.), then all other LWPs will stop as well. Returns
848 non-zero for success, zero for failure. */
851 proc_unset_async (procinfo
*pi
)
853 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
856 /* Request the process/LWP to stop. Does not wait. Returns non-zero
857 for success, zero for failure. */
860 proc_stop_process (procinfo
*pi
)
864 /* We might conceivably apply this operation to an LWP, and the
865 LWP's ctl file descriptor might not be open. */
867 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
871 procfs_ctl_t cmd
= PCSTOP
;
873 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
879 /* Wait for the process or LWP to stop (block until it does). Returns
880 non-zero for success, zero for failure. */
883 proc_wait_for_stop (procinfo
*pi
)
887 /* We should never have to apply this operation to any procinfo
888 except the one for the main process. If that ever changes for
889 any reason, then take out the following clause and replace it
890 with one that makes sure the ctl_fd is open. */
893 pi
= find_procinfo_or_die (pi
->pid
, 0);
895 procfs_ctl_t cmd
= PCWSTOP
;
899 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
901 clear_sigint_trap ();
903 /* We been runnin' and we stopped -- need to update status. */
904 pi
->status_valid
= 0;
909 /* Make the process or LWP runnable.
911 Options (not all are implemented):
913 - clear current fault
914 - clear current signal
915 - abort the current system call
916 - stop as soon as finished with system call
918 Always clears the current fault. PI is the process or LWP to
919 operate on. If STEP is true, set the process or LWP to trap after
920 one instruction. If SIGNO is zero, clear the current signal if
921 any; if non-zero, set the current signal to this one. Returns
922 non-zero for success, zero for failure. */
925 proc_run_process (procinfo
*pi
, int step
, int signo
)
930 /* We will probably have to apply this operation to individual
931 threads, so make sure the control file descriptor is open. */
933 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
936 runflags
= PRCFAULT
; /* Always clear current fault. */
941 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals. */
942 proc_set_current_signal (pi
, signo
);
948 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
953 /* Register to trace signals in the process or LWP. Returns non-zero
954 for success, zero for failure. */
957 proc_set_traced_signals (procinfo
*pi
, sigset_t
*sigset
)
961 /* We should never have to apply this operation to any procinfo
962 except the one for the main process. If that ever changes for
963 any reason, then take out the following clause and replace it
964 with one that makes sure the ctl_fd is open. */
967 pi
= find_procinfo_or_die (pi
->pid
, 0);
971 /* Use char array to avoid alignment issues. */
972 char sigset
[sizeof (sigset_t
)];
976 memcpy (&arg
.sigset
, sigset
, sizeof (sigset_t
));
978 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
980 /* The above operation renders the procinfo's cached pstatus obsolete. */
981 pi
->status_valid
= 0;
984 warning (_("procfs: set_traced_signals failed"));
988 /* Register to trace hardware faults in the process or LWP. Returns
989 non-zero for success, zero for failure. */
992 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
996 /* We should never have to apply this operation to any procinfo
997 except the one for the main process. If that ever changes for
998 any reason, then take out the following clause and replace it
999 with one that makes sure the ctl_fd is open. */
1002 pi
= find_procinfo_or_die (pi
->pid
, 0);
1006 /* Use char array to avoid alignment issues. */
1007 char fltset
[sizeof (fltset_t
)];
1011 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1013 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1015 /* The above operation renders the procinfo's cached pstatus obsolete. */
1016 pi
->status_valid
= 0;
1021 /* Register to trace entry to system calls in the process or LWP.
1022 Returns non-zero for success, zero for failure. */
1025 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1029 /* We should never have to apply this operation to any procinfo
1030 except the one for the main process. If that ever changes for
1031 any reason, then take out the following clause and replace it
1032 with one that makes sure the ctl_fd is open. */
1035 pi
= find_procinfo_or_die (pi
->pid
, 0);
1039 /* Use char array to avoid alignment issues. */
1040 char sysset
[sizeof (sysset_t
)];
1044 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1046 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1048 /* The above operation renders the procinfo's cached pstatus
1050 pi
->status_valid
= 0;
1055 /* Register to trace exit from system calls in the process or LWP.
1056 Returns non-zero for success, zero for failure. */
1059 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1063 /* We should never have to apply this operation to any procinfo
1064 except the one for the main process. If that ever changes for
1065 any reason, then take out the following clause and replace it
1066 with one that makes sure the ctl_fd is open. */
1069 pi
= find_procinfo_or_die (pi
->pid
, 0);
1071 struct gdb_proc_ctl_pcsexit
{
1073 /* Use char array to avoid alignment issues. */
1074 char sysset
[sizeof (sysset_t
)];
1078 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1080 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1082 /* The above operation renders the procinfo's cached pstatus
1084 pi
->status_valid
= 0;
1089 /* Specify the set of blocked / held signals in the process or LWP.
1090 Returns non-zero for success, zero for failure. */
1093 proc_set_held_signals (procinfo
*pi
, sigset_t
*sighold
)
1097 /* We should never have to apply this operation to any procinfo
1098 except the one for the main process. If that ever changes for
1099 any reason, then take out the following clause and replace it
1100 with one that makes sure the ctl_fd is open. */
1103 pi
= find_procinfo_or_die (pi
->pid
, 0);
1107 /* Use char array to avoid alignment issues. */
1108 char hold
[sizeof (sigset_t
)];
1112 memcpy (&arg
.hold
, sighold
, sizeof (sigset_t
));
1113 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1115 /* The above operation renders the procinfo's cached pstatus
1117 pi
->status_valid
= 0;
1122 /* Returns the set of signals that are held / blocked. Will also copy
1123 the sigset if SAVE is non-zero. */
1126 proc_get_held_signals (procinfo
*pi
, sigset_t
*save
)
1128 sigset_t
*ret
= NULL
;
1130 /* We should never have to apply this operation to any procinfo
1131 except the one for the main process. If that ever changes for
1132 any reason, then take out the following clause and replace it
1133 with one that makes sure the ctl_fd is open. */
1136 pi
= find_procinfo_or_die (pi
->pid
, 0);
1138 if (!pi
->status_valid
)
1139 if (!proc_get_status (pi
))
1142 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1144 memcpy (save
, ret
, sizeof (sigset_t
));
1149 /* Returns the set of signals that are traced / debugged. Will also
1150 copy the sigset if SAVE is non-zero. */
1153 proc_get_traced_signals (procinfo
*pi
, sigset_t
*save
)
1155 sigset_t
*ret
= NULL
;
1157 /* We should never have to apply this operation to any procinfo
1158 except the one for the main process. If that ever changes for
1159 any reason, then take out the following clause and replace it
1160 with one that makes sure the ctl_fd is open. */
1163 pi
= find_procinfo_or_die (pi
->pid
, 0);
1165 if (!pi
->status_valid
)
1166 if (!proc_get_status (pi
))
1169 ret
= &pi
->prstatus
.pr_sigtrace
;
1171 memcpy (save
, ret
, sizeof (sigset_t
));
1176 /* Returns the set of hardware faults that are traced /debugged. Will
1177 also copy the faultset if SAVE is non-zero. */
1180 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1182 fltset_t
*ret
= NULL
;
1184 /* We should never have to apply this operation to any procinfo
1185 except the one for the main process. If that ever changes for
1186 any reason, then take out the following clause and replace it
1187 with one that makes sure the ctl_fd is open. */
1190 pi
= find_procinfo_or_die (pi
->pid
, 0);
1192 if (!pi
->status_valid
)
1193 if (!proc_get_status (pi
))
1196 ret
= &pi
->prstatus
.pr_flttrace
;
1198 memcpy (save
, ret
, sizeof (fltset_t
));
1203 /* Returns the set of syscalls that are traced /debugged on entry.
1204 Will also copy the syscall set if SAVE is non-zero. */
1207 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1209 sysset_t
*ret
= NULL
;
1211 /* We should never have to apply this operation to any procinfo
1212 except the one for the main process. If that ever changes for
1213 any reason, then take out the following clause and replace it
1214 with one that makes sure the ctl_fd is open. */
1217 pi
= find_procinfo_or_die (pi
->pid
, 0);
1219 if (!pi
->status_valid
)
1220 if (!proc_get_status (pi
))
1223 ret
= &pi
->prstatus
.pr_sysentry
;
1225 memcpy (save
, ret
, sizeof (sysset_t
));
1230 /* Returns the set of syscalls that are traced /debugged on exit.
1231 Will also copy the syscall set if SAVE is non-zero. */
1234 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1236 sysset_t
*ret
= NULL
;
1238 /* We should never have to apply this operation to any procinfo
1239 except the one for the main process. If that ever changes for
1240 any reason, then take out the following clause and replace it
1241 with one that makes sure the ctl_fd is open. */
1244 pi
= find_procinfo_or_die (pi
->pid
, 0);
1246 if (!pi
->status_valid
)
1247 if (!proc_get_status (pi
))
1250 ret
= &pi
->prstatus
.pr_sysexit
;
1252 memcpy (save
, ret
, sizeof (sysset_t
));
1257 /* The current fault (if any) is cleared; the associated signal will
1258 not be sent to the process or LWP when it resumes. Returns
1259 non-zero for success, zero for failure. */
1262 proc_clear_current_fault (procinfo
*pi
)
1266 /* We should never have to apply this operation to any procinfo
1267 except the one for the main process. If that ever changes for
1268 any reason, then take out the following clause and replace it
1269 with one that makes sure the ctl_fd is open. */
1272 pi
= find_procinfo_or_die (pi
->pid
, 0);
1274 procfs_ctl_t cmd
= PCCFAULT
;
1276 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1281 /* Set the "current signal" that will be delivered next to the
1282 process. NOTE: semantics are different from those of KILL. This
1283 signal will be delivered to the process or LWP immediately when it
1284 is resumed (even if the signal is held/blocked); it will NOT
1285 immediately cause another event of interest, and will NOT first
1286 trap back to the debugger. Returns non-zero for success, zero for
1290 proc_set_current_signal (procinfo
*pi
, int signo
)
1295 /* Use char array to avoid alignment issues. */
1296 char sinfo
[sizeof (siginfo_t
)];
1299 process_stratum_target
*wait_target
;
1301 struct target_waitstatus wait_status
;
1303 /* We should never have to apply this operation to any procinfo
1304 except the one for the main process. If that ever changes for
1305 any reason, then take out the following clause and replace it
1306 with one that makes sure the ctl_fd is open. */
1309 pi
= find_procinfo_or_die (pi
->pid
, 0);
1311 /* The pointer is just a type alias. */
1312 get_last_target_status (&wait_target
, &wait_ptid
, &wait_status
);
1313 if (wait_target
== &the_procfs_target
1314 && wait_ptid
== inferior_ptid
1315 && wait_status
.kind
== TARGET_WAITKIND_STOPPED
1316 && wait_status
.value
.sig
== gdb_signal_from_host (signo
)
1317 && proc_get_status (pi
)
1318 && pi
->prstatus
.pr_lwp
.pr_info
.si_signo
== signo
1320 /* Use the siginfo associated with the signal being
1322 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_lwp
.pr_info
, sizeof (siginfo_t
));
1325 mysinfo
.si_signo
= signo
;
1326 mysinfo
.si_code
= 0;
1327 mysinfo
.si_pid
= getpid (); /* ?why? */
1328 mysinfo
.si_uid
= getuid (); /* ?why? */
1329 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1333 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1338 /* The current signal (if any) is cleared, and is not sent to the
1339 process or LWP when it resumes. Returns non-zero for success, zero
1343 proc_clear_current_signal (procinfo
*pi
)
1347 /* We should never have to apply this operation to any procinfo
1348 except the one for the main process. If that ever changes for
1349 any reason, then take out the following clause and replace it
1350 with one that makes sure the ctl_fd is open. */
1353 pi
= find_procinfo_or_die (pi
->pid
, 0);
1357 /* Use char array to avoid alignment issues. */
1358 char sinfo
[sizeof (siginfo_t
)];
1363 /* The pointer is just a type alias. */
1364 mysinfo
.si_signo
= 0;
1365 mysinfo
.si_code
= 0;
1366 mysinfo
.si_errno
= 0;
1367 mysinfo
.si_pid
= getpid (); /* ?why? */
1368 mysinfo
.si_uid
= getuid (); /* ?why? */
1369 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1371 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1376 /* Return the general-purpose registers for the process or LWP
1377 corresponding to PI. Upon failure, return NULL. */
1379 static gdb_gregset_t
*
1380 proc_get_gregs (procinfo
*pi
)
1382 if (!pi
->status_valid
|| !pi
->gregs_valid
)
1383 if (!proc_get_status (pi
))
1386 return &pi
->prstatus
.pr_lwp
.pr_reg
;
1389 /* Return the general-purpose registers for the process or LWP
1390 corresponding to PI. Upon failure, return NULL. */
1392 static gdb_fpregset_t
*
1393 proc_get_fpregs (procinfo
*pi
)
1395 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
1396 if (!proc_get_status (pi
))
1399 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
1402 /* Write the general-purpose registers back to the process or LWP
1403 corresponding to PI. Return non-zero for success, zero for
1407 proc_set_gregs (procinfo
*pi
)
1409 gdb_gregset_t
*gregs
;
1412 gregs
= proc_get_gregs (pi
);
1414 return 0; /* proc_get_regs has already warned. */
1416 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1422 /* Use char array to avoid alignment issues. */
1423 char gregs
[sizeof (gdb_gregset_t
)];
1427 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
1428 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1431 /* Policy: writing the registers invalidates our cache. */
1432 pi
->gregs_valid
= 0;
1436 /* Write the floating-pointer registers back to the process or LWP
1437 corresponding to PI. Return non-zero for success, zero for
1441 proc_set_fpregs (procinfo
*pi
)
1443 gdb_fpregset_t
*fpregs
;
1446 fpregs
= proc_get_fpregs (pi
);
1448 return 0; /* proc_get_fpregs has already warned. */
1450 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1456 /* Use char array to avoid alignment issues. */
1457 char fpregs
[sizeof (gdb_fpregset_t
)];
1461 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
1462 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1465 /* Policy: writing the registers invalidates our cache. */
1466 pi
->fpregs_valid
= 0;
1470 /* Send a signal to the proc or lwp with the semantics of "kill()".
1471 Returns non-zero for success, zero for failure. */
1474 proc_kill (procinfo
*pi
, int signo
)
1478 /* We might conceivably apply this operation to an LWP, and the
1479 LWP's ctl file descriptor might not be open. */
1481 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1485 procfs_ctl_t cmd
[2];
1489 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1495 /* Find the pid of the process that started this one. Returns the
1496 parent process pid, or zero. */
1499 proc_parent_pid (procinfo
*pi
)
1501 /* We should never have to apply this operation to any procinfo
1502 except the one for the main process. If that ever changes for
1503 any reason, then take out the following clause and replace it
1504 with one that makes sure the ctl_fd is open. */
1507 pi
= find_procinfo_or_die (pi
->pid
, 0);
1509 if (!pi
->status_valid
)
1510 if (!proc_get_status (pi
))
1513 return pi
->prstatus
.pr_ppid
;
1516 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
1517 (a.k.a void pointer)! */
1520 procfs_address_to_host_pointer (CORE_ADDR addr
)
1522 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
1525 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (ptr_type
));
1526 gdbarch_address_to_pointer (target_gdbarch (), ptr_type
,
1527 (gdb_byte
*) &ptr
, addr
);
1532 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
1536 char watch
[sizeof (prwatch_t
)];
1540 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
1541 convert a target address into something that can be stored in a
1542 native data structure. */
1543 pwatch
.pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
1544 pwatch
.pr_size
= len
;
1545 pwatch
.pr_wflags
= wflags
;
1547 memcpy (arg
.watch
, &pwatch
, sizeof (prwatch_t
));
1548 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
1551 /* =============== END, non-thread part of /proc "MODULE" =============== */
1553 /* =================== Thread "MODULE" =================== */
1555 /* Returns the number of threads for the process. */
1558 proc_get_nthreads (procinfo
*pi
)
1560 if (!pi
->status_valid
)
1561 if (!proc_get_status (pi
))
1564 /* Only works for the process procinfo, because the LWP procinfos do not
1565 get prstatus filled in. */
1566 if (pi
->tid
!= 0) /* Find the parent process procinfo. */
1567 pi
= find_procinfo_or_die (pi
->pid
, 0);
1568 return pi
->prstatus
.pr_nlwp
;
1571 /* Return the ID of the thread that had an event of interest.
1572 (ie. the one that hit a breakpoint or other traced event). All
1573 other things being equal, this should be the ID of a thread that is
1574 currently executing. */
1577 proc_get_current_thread (procinfo
*pi
)
1579 /* Note: this should be applied to the root procinfo for the
1580 process, not to the procinfo for an LWP. If applied to the
1581 procinfo for an LWP, it will simply return that LWP's ID. In
1582 that case, find the parent process procinfo. */
1585 pi
= find_procinfo_or_die (pi
->pid
, 0);
1587 if (!pi
->status_valid
)
1588 if (!proc_get_status (pi
))
1591 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
1594 /* Discover the IDs of all the threads within the process, and create
1595 a procinfo for each of them (chained to the parent). Returns
1596 non-zero for success, zero for failure. */
1599 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
1601 if (thread
&& parent
) /* sanity */
1603 thread
->status_valid
= 0;
1604 if (!proc_get_status (thread
))
1605 destroy_one_procinfo (&parent
->thread_list
, thread
);
1607 return 0; /* keep iterating */
1611 proc_update_threads (procinfo
*pi
)
1613 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
1614 struct dirent
*direntry
;
1619 /* We should never have to apply this operation to any procinfo
1620 except the one for the main process. If that ever changes for
1621 any reason, then take out the following clause and replace it
1622 with one that makes sure the ctl_fd is open. */
1625 pi
= find_procinfo_or_die (pi
->pid
, 0);
1627 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
1629 /* Note: this brute-force method was originally devised for Unixware
1630 (support removed since), and will also work on Solaris 2.6 and
1631 2.7. The original comment mentioned the existence of a much
1632 simpler and more elegant way to do this on Solaris, but didn't
1633 point out what that was. */
1635 strcpy (pathname
, pi
->pathname
);
1636 strcat (pathname
, "/lwp");
1637 dirp
.reset (opendir (pathname
));
1639 proc_error (pi
, "update_threads, opendir", __LINE__
);
1641 while ((direntry
= readdir (dirp
.get ())) != NULL
)
1642 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
1644 lwpid
= atoi (&direntry
->d_name
[0]);
1645 thread
= create_procinfo (pi
->pid
, lwpid
);
1647 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
1649 pi
->threads_valid
= 1;
1653 /* Given a pointer to a function, call that function once for each lwp
1654 in the procinfo list, until the function returns non-zero, in which
1655 event return the value returned by the function.
1657 Note: this function does NOT call update_threads. If you want to
1658 discover new threads first, you must call that function explicitly.
1659 This function just makes a quick pass over the currently-known
1662 PI is the parent process procinfo. FUNC is the per-thread
1663 function. PTR is an opaque parameter for function. Returns the
1664 first non-zero return value from the callee, or zero. */
1667 proc_iterate_over_threads (procinfo
*pi
,
1668 int (*func
) (procinfo
*, procinfo
*, void *),
1671 procinfo
*thread
, *next
;
1674 /* We should never have to apply this operation to any procinfo
1675 except the one for the main process. If that ever changes for
1676 any reason, then take out the following clause and replace it
1677 with one that makes sure the ctl_fd is open. */
1680 pi
= find_procinfo_or_die (pi
->pid
, 0);
1682 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
1684 next
= thread
->next
; /* In case thread is destroyed. */
1685 retval
= (*func
) (pi
, thread
, ptr
);
1693 /* =================== END, Thread "MODULE" =================== */
1695 /* =================== END, /proc "MODULE" =================== */
1697 /* =================== GDB "MODULE" =================== */
1699 /* Here are all of the gdb target vector functions and their
1702 static void do_attach (ptid_t ptid
);
1703 static void do_detach ();
1704 static void proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
,
1705 int entry_or_exit
, int mode
, int from_tty
);
1707 /* Sets up the inferior to be debugged. Registers to trace signals,
1708 hardware faults, and syscalls. Note: does not set RLC flag: caller
1709 may want to customize that. Returns zero for success (note!
1710 unlike most functions in this module); on failure, returns the LINE
1711 NUMBER where it failed! */
1714 procfs_debug_inferior (procinfo
*pi
)
1716 fltset_t traced_faults
;
1717 sigset_t traced_signals
;
1718 sysset_t
*traced_syscall_entries
;
1719 sysset_t
*traced_syscall_exits
;
1722 /* Register to trace hardware faults in the child. */
1723 prfillset (&traced_faults
); /* trace all faults... */
1724 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
1725 if (!proc_set_traced_faults (pi
, &traced_faults
))
1728 /* Initially, register to trace all signals in the child. */
1729 prfillset (&traced_signals
);
1730 if (!proc_set_traced_signals (pi
, &traced_signals
))
1734 /* Register to trace the 'exit' system call (on entry). */
1735 traced_syscall_entries
= XNEW (sysset_t
);
1736 premptyset (traced_syscall_entries
);
1737 praddset (traced_syscall_entries
, SYS_exit
);
1738 praddset (traced_syscall_entries
, SYS_lwp_exit
);
1740 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
1741 xfree (traced_syscall_entries
);
1745 /* Method for tracing exec syscalls. */
1746 traced_syscall_exits
= XNEW (sysset_t
);
1747 premptyset (traced_syscall_exits
);
1748 praddset (traced_syscall_exits
, SYS_execve
);
1749 praddset (traced_syscall_exits
, SYS_lwp_create
);
1750 praddset (traced_syscall_exits
, SYS_lwp_exit
);
1752 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
1753 xfree (traced_syscall_exits
);
1761 procfs_target::attach (const char *args
, int from_tty
)
1765 pid
= parse_pid_to_attach (args
);
1767 if (pid
== getpid ())
1768 error (_("Attaching GDB to itself is not a good idea..."));
1770 /* Push the target if needed, ensure it gets un-pushed it if attach fails. */
1771 target_unpush_up unpusher
;
1772 if (!target_is_pushed (this))
1775 unpusher
.reset (this);
1780 const char *exec_file
= get_exec_file (0);
1783 printf_filtered (_("Attaching to program `%s', %s\n"),
1784 exec_file
, target_pid_to_str (ptid_t (pid
)).c_str ());
1786 printf_filtered (_("Attaching to %s\n"),
1787 target_pid_to_str (ptid_t (pid
)).c_str ());
1792 do_attach (ptid_t (pid
));
1794 /* Everything went fine, keep the target pushed. */
1795 unpusher
.release ();
1799 procfs_target::detach (inferior
*inf
, int from_tty
)
1801 int pid
= inferior_ptid
.pid ();
1805 const char *exec_file
;
1807 exec_file
= get_exec_file (0);
1808 if (exec_file
== NULL
)
1811 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file
,
1812 target_pid_to_str (ptid_t (pid
)).c_str ());
1817 switch_to_no_thread ();
1818 detach_inferior (inf
);
1819 maybe_unpush_target ();
1823 do_attach (ptid_t ptid
)
1826 struct inferior
*inf
;
1830 pi
= create_procinfo (ptid
.pid (), 0);
1832 perror (_("procfs: out of memory in 'attach'"));
1834 if (!open_procinfo_files (pi
, FD_CTL
))
1836 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
1837 xsnprintf (errmsg
, sizeof (errmsg
),
1838 "do_attach: couldn't open /proc file for process %d",
1840 dead_procinfo (pi
, errmsg
, NOKILL
);
1843 /* Stop the process (if it isn't already stopped). */
1844 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
1846 pi
->was_stopped
= 1;
1847 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
1851 pi
->was_stopped
= 0;
1852 /* Set the process to run again when we close it. */
1853 if (!proc_set_run_on_last_close (pi
))
1854 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
1856 /* Now stop the process. */
1857 if (!proc_stop_process (pi
))
1858 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
1859 pi
->ignore_next_sigstop
= 1;
1861 /* Save some of the /proc state to be restored if we detach. */
1862 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
1863 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
1864 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
1865 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
1866 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
1867 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
1869 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
1870 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
1872 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
1873 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
1875 fail
= procfs_debug_inferior (pi
);
1877 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
1879 inf
= current_inferior ();
1880 inferior_appeared (inf
, pi
->pid
);
1881 /* Let GDB know that the inferior was attached. */
1882 inf
->attach_flag
= 1;
1884 /* Create a procinfo for the current lwp. */
1885 lwpid
= proc_get_current_thread (pi
);
1886 create_procinfo (pi
->pid
, lwpid
);
1888 /* Add it to gdb's thread list. */
1889 ptid
= ptid_t (pi
->pid
, lwpid
, 0);
1890 thread_info
*thr
= add_thread (&the_procfs_target
, ptid
);
1891 switch_to_thread (thr
);
1899 /* Find procinfo for the main process. */
1900 pi
= find_procinfo_or_die (inferior_ptid
.pid (),
1901 0); /* FIXME: threads */
1903 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
1904 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
1906 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
1907 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
1909 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
1910 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
1912 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
1913 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
1915 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
1916 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
1918 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
1919 if (!(pi
->was_stopped
)
1920 || query (_("Was stopped when attached, make it runnable again? ")))
1922 /* Clear any pending signal. */
1923 if (!proc_clear_current_fault (pi
))
1924 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
1926 if (!proc_clear_current_signal (pi
))
1927 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
1929 if (!proc_set_run_on_last_close (pi
))
1930 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
1933 destroy_procinfo (pi
);
1936 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
1939 NOTE: Since the /proc interface cannot give us individual
1940 registers, we pay no attention to REGNUM, and just fetch them all.
1941 This results in the possibility that we will do unnecessarily many
1942 fetches, since we may be called repeatedly for individual
1943 registers. So we cache the results, and mark the cache invalid
1944 when the process is resumed. */
1947 procfs_target::fetch_registers (struct regcache
*regcache
, int regnum
)
1949 gdb_gregset_t
*gregs
;
1951 ptid_t ptid
= regcache
->ptid ();
1952 int pid
= ptid
.pid ();
1953 int tid
= ptid
.lwp ();
1954 struct gdbarch
*gdbarch
= regcache
->arch ();
1956 pi
= find_procinfo_or_die (pid
, tid
);
1959 error (_("procfs: fetch_registers failed to find procinfo for %s"),
1960 target_pid_to_str (ptid
).c_str ());
1962 gregs
= proc_get_gregs (pi
);
1964 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
1966 supply_gregset (regcache
, (const gdb_gregset_t
*) gregs
);
1968 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
1970 gdb_fpregset_t
*fpregs
;
1972 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
1973 || regnum
== gdbarch_pc_regnum (gdbarch
)
1974 || regnum
== gdbarch_sp_regnum (gdbarch
))
1975 return; /* Not a floating point register. */
1977 fpregs
= proc_get_fpregs (pi
);
1979 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
1981 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregs
);
1985 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
1986 this for all registers.
1988 NOTE: Since the /proc interface will not read individual registers,
1989 we will cache these requests until the process is resumed, and only
1990 then write them back to the inferior process.
1992 FIXME: is that a really bad idea? Have to think about cases where
1993 writing one register might affect the value of others, etc. */
1996 procfs_target::store_registers (struct regcache
*regcache
, int regnum
)
1998 gdb_gregset_t
*gregs
;
2000 ptid_t ptid
= regcache
->ptid ();
2001 int pid
= ptid
.pid ();
2002 int tid
= ptid
.lwp ();
2003 struct gdbarch
*gdbarch
= regcache
->arch ();
2005 pi
= find_procinfo_or_die (pid
, tid
);
2008 error (_("procfs: store_registers: failed to find procinfo for %s"),
2009 target_pid_to_str (ptid
).c_str ());
2011 gregs
= proc_get_gregs (pi
);
2013 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
2015 fill_gregset (regcache
, gregs
, regnum
);
2016 if (!proc_set_gregs (pi
))
2017 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
2019 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
2021 gdb_fpregset_t
*fpregs
;
2023 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
2024 || regnum
== gdbarch_pc_regnum (gdbarch
)
2025 || regnum
== gdbarch_sp_regnum (gdbarch
))
2026 return; /* Not a floating point register. */
2028 fpregs
= proc_get_fpregs (pi
);
2030 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
2032 fill_fpregset (regcache
, fpregs
, regnum
);
2033 if (!proc_set_fpregs (pi
))
2034 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
2038 /* Retrieve the next stop event from the child process. If child has
2039 not stopped yet, wait for it to stop. Translate /proc eventcodes
2040 (or possibly wait eventcodes) into gdb internal event codes.
2041 Returns the id of process (and possibly thread) that incurred the
2042 event. Event codes are returned through a pointer parameter. */
2045 procfs_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
2046 target_wait_flags options
)
2048 /* First cut: loosely based on original version 2.1. */
2052 ptid_t retval
, temp_ptid
;
2053 int why
, what
, flags
;
2060 retval
= ptid_t (-1);
2062 /* Find procinfo for main process. */
2064 /* procfs_target currently only supports one inferior. */
2065 inferior
*inf
= current_inferior ();
2067 pi
= find_procinfo_or_die (inf
->pid
, 0);
2070 /* We must assume that the status is stale now... */
2071 pi
->status_valid
= 0;
2072 pi
->gregs_valid
= 0;
2073 pi
->fpregs_valid
= 0;
2075 #if 0 /* just try this out... */
2076 flags
= proc_flags (pi
);
2077 why
= proc_why (pi
);
2078 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
2079 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
2081 /* If child is not stopped, wait for it to stop. */
2082 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
2083 && !proc_wait_for_stop (pi
))
2085 /* wait_for_stop failed: has the child terminated? */
2086 if (errno
== ENOENT
)
2090 /* /proc file not found; presumably child has terminated. */
2091 wait_retval
= ::wait (&wstat
); /* "wait" for the child's exit. */
2094 if (wait_retval
!= inf
->pid
)
2095 error (_("procfs: couldn't stop "
2096 "process %d: wait returned %d."),
2097 inf
->pid
, wait_retval
);
2098 /* FIXME: might I not just use waitpid?
2099 Or try find_procinfo to see if I know about this child? */
2100 retval
= ptid_t (wait_retval
);
2102 else if (errno
== EINTR
)
2106 /* Unknown error from wait_for_stop. */
2107 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
2112 /* This long block is reached if either:
2113 a) the child was already stopped, or
2114 b) we successfully waited for the child with wait_for_stop.
2115 This block will analyze the /proc status, and translate it
2116 into a waitstatus for GDB.
2118 If we actually had to call wait because the /proc file
2119 is gone (child terminated), then we skip this block,
2120 because we already have a waitstatus. */
2122 flags
= proc_flags (pi
);
2123 why
= proc_why (pi
);
2124 what
= proc_what (pi
);
2126 if (flags
& (PR_STOPPED
| PR_ISTOP
))
2128 /* If it's running async (for single_thread control),
2129 set it back to normal again. */
2130 if (flags
& PR_ASYNC
)
2131 if (!proc_unset_async (pi
))
2132 proc_error (pi
, "target_wait, unset_async", __LINE__
);
2135 proc_prettyprint_why (why
, what
, 1);
2137 /* The 'pid' we will return to GDB is composed of
2138 the process ID plus the lwp ID. */
2139 retval
= ptid_t (pi
->pid
, proc_get_current_thread (pi
), 0);
2143 wstat
= (what
<< 8) | 0177;
2146 if (what
== SYS_lwp_exit
)
2148 if (print_thread_events
)
2149 printf_unfiltered (_("[%s exited]\n"),
2150 target_pid_to_str (retval
).c_str ());
2151 delete_thread (find_thread_ptid (this, retval
));
2152 target_continue_no_signal (ptid
);
2155 else if (what
== SYS_exit
)
2157 /* Handle SYS_exit call only. */
2158 /* Stopped at entry to SYS_exit.
2159 Make it runnable, resume it, then use
2160 the wait system call to get its exit code.
2161 Proc_run_process always clears the current
2163 Then return its exit status. */
2164 pi
->status_valid
= 0;
2166 /* FIXME: what we should do is return
2167 TARGET_WAITKIND_SPURIOUS. */
2168 if (!proc_run_process (pi
, 0, 0))
2169 proc_error (pi
, "target_wait, run_process", __LINE__
);
2171 if (inf
->attach_flag
)
2173 /* Don't call wait: simulate waiting for exit,
2174 return a "success" exit code. Bogus: what if
2175 it returns something else? */
2177 retval
= ptid_t (inf
->pid
); /* ? ? ? */
2181 int temp
= ::wait (&wstat
);
2183 /* FIXME: shouldn't I make sure I get the right
2184 event from the right process? If (for
2185 instance) I have killed an earlier inferior
2186 process but failed to clean up after it
2187 somehow, I could get its termination event
2190 /* If wait returns -1, that's what we return
2193 retval
= ptid_t (temp
);
2198 printf_filtered (_("procfs: trapped on entry to "));
2199 proc_prettyprint_syscall (proc_what (pi
), 0);
2200 printf_filtered ("\n");
2202 long i
, nsysargs
, *sysargs
;
2204 nsysargs
= proc_nsysarg (pi
);
2205 sysargs
= proc_sysargs (pi
);
2207 if (nsysargs
> 0 && sysargs
!= NULL
)
2209 printf_filtered (_("%ld syscall arguments:\n"),
2211 for (i
= 0; i
< nsysargs
; i
++)
2212 printf_filtered ("#%ld: 0x%08lx\n",
2216 /* How to keep going without returning to wfi: */
2217 target_continue_no_signal (ptid
);
2222 if (what
== SYS_execve
)
2224 /* Hopefully this is our own "fork-child" execing
2225 the real child. Hoax this event into a trap, and
2226 GDB will see the child about to execute its start
2228 wstat
= (SIGTRAP
<< 8) | 0177;
2230 else if (what
== SYS_lwp_create
)
2232 /* This syscall is somewhat like fork/exec. We
2233 will get the event twice: once for the parent
2234 LWP, and once for the child. We should already
2235 know about the parent LWP, but the child will
2236 be new to us. So, whenever we get this event,
2237 if it represents a new thread, simply add the
2238 thread to the list. */
2240 /* If not in procinfo list, add it. */
2241 temp_tid
= proc_get_current_thread (pi
);
2242 if (!find_procinfo (pi
->pid
, temp_tid
))
2243 create_procinfo (pi
->pid
, temp_tid
);
2245 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2246 /* If not in GDB's thread list, add it. */
2247 if (!in_thread_list (this, temp_ptid
))
2248 add_thread (this, temp_ptid
);
2250 target_continue_no_signal (ptid
);
2253 else if (what
== SYS_lwp_exit
)
2255 if (print_thread_events
)
2256 printf_unfiltered (_("[%s exited]\n"),
2257 target_pid_to_str (retval
).c_str ());
2258 delete_thread (find_thread_ptid (this, retval
));
2259 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2264 printf_filtered (_("procfs: trapped on exit from "));
2265 proc_prettyprint_syscall (proc_what (pi
), 0);
2266 printf_filtered ("\n");
2268 long i
, nsysargs
, *sysargs
;
2270 nsysargs
= proc_nsysarg (pi
);
2271 sysargs
= proc_sysargs (pi
);
2273 if (nsysargs
> 0 && sysargs
!= NULL
)
2275 printf_filtered (_("%ld syscall arguments:\n"),
2277 for (i
= 0; i
< nsysargs
; i
++)
2278 printf_filtered ("#%ld: 0x%08lx\n",
2282 target_continue_no_signal (ptid
);
2288 wstat
= (SIGSTOP
<< 8) | 0177;
2293 printf_filtered (_("Retry #%d:\n"), retry
);
2294 pi
->status_valid
= 0;
2299 /* If not in procinfo list, add it. */
2300 temp_tid
= proc_get_current_thread (pi
);
2301 if (!find_procinfo (pi
->pid
, temp_tid
))
2302 create_procinfo (pi
->pid
, temp_tid
);
2304 /* If not in GDB's thread list, add it. */
2305 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2306 if (!in_thread_list (this, temp_ptid
))
2307 add_thread (this, temp_ptid
);
2309 status
->kind
= TARGET_WAITKIND_STOPPED
;
2310 status
->value
.sig
= GDB_SIGNAL_0
;
2315 wstat
= (what
<< 8) | 0177;
2319 int signo
= pi
->prstatus
.pr_lwp
.pr_info
.si_signo
;
2321 wstat
= (signo
<< 8) | 0177;
2324 default: /* switch (why) unmatched */
2325 printf_filtered ("procfs:%d -- ", __LINE__
);
2326 printf_filtered (_("child stopped for unknown reason:\n"));
2327 proc_prettyprint_why (why
, what
, 1);
2328 error (_("... giving up..."));
2331 /* Got this far without error: If retval isn't in the
2332 threads database, add it. */
2333 if (retval
.pid () > 0
2334 && !in_thread_list (this, retval
))
2336 /* We have a new thread. We need to add it both to
2337 GDB's list and to our own. If we don't create a
2338 procinfo, resume may be unhappy later. */
2339 add_thread (this, retval
);
2340 if (find_procinfo (retval
.pid (),
2341 retval
.lwp ()) == NULL
)
2342 create_procinfo (retval
.pid (),
2346 else /* Flags do not indicate STOPPED. */
2348 /* surely this can't happen... */
2349 printf_filtered ("procfs:%d -- process not stopped.\n",
2351 proc_prettyprint_flags (flags
, 1);
2352 error (_("procfs: ...giving up..."));
2357 store_waitstatus (status
, wstat
);
2363 /* Perform a partial transfer to/from the specified object. For
2364 memory transfers, fall back to the old memory xfer functions. */
2366 enum target_xfer_status
2367 procfs_target::xfer_partial (enum target_object object
,
2368 const char *annex
, gdb_byte
*readbuf
,
2369 const gdb_byte
*writebuf
, ULONGEST offset
,
2370 ULONGEST len
, ULONGEST
*xfered_len
)
2374 case TARGET_OBJECT_MEMORY
:
2375 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
2377 case TARGET_OBJECT_AUXV
:
2378 return memory_xfer_auxv (this, object
, annex
, readbuf
, writebuf
,
2379 offset
, len
, xfered_len
);
2382 return this->beneath ()->xfer_partial (object
, annex
,
2383 readbuf
, writebuf
, offset
, len
,
2388 /* Helper for procfs_xfer_partial that handles memory transfers.
2389 Arguments are like target_xfer_partial. */
2391 static enum target_xfer_status
2392 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
2393 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
2398 /* Find procinfo for main process. */
2399 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2400 if (pi
->as_fd
== 0 && open_procinfo_files (pi
, FD_AS
) == 0)
2402 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
2403 return TARGET_XFER_E_IO
;
2406 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
2407 return TARGET_XFER_E_IO
;
2409 if (writebuf
!= NULL
)
2411 PROCFS_NOTE ("write memory:\n");
2412 nbytes
= write (pi
->as_fd
, writebuf
, len
);
2416 PROCFS_NOTE ("read memory:\n");
2417 nbytes
= read (pi
->as_fd
, readbuf
, len
);
2420 return TARGET_XFER_E_IO
;
2421 *xfered_len
= nbytes
;
2422 return TARGET_XFER_OK
;
2425 /* Called by target_resume before making child runnable. Mark cached
2426 registers and status's invalid. If there are "dirty" caches that
2427 need to be written back to the child process, do that.
2429 File descriptors are also cached. As they are a limited resource,
2430 we cannot hold onto them indefinitely. However, as they are
2431 expensive to open, we don't want to throw them away
2432 indiscriminately either. As a compromise, we will keep the file
2433 descriptors for the parent process, but discard any file
2434 descriptors we may have accumulated for the threads.
2436 As this function is called by iterate_over_threads, it always
2437 returns zero (so that iterate_over_threads will keep
2441 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
2443 /* About to run the child; invalidate caches and do any other
2448 /* The presence of a parent indicates that this is an LWP.
2449 Close any file descriptors that it might have open.
2450 We don't do this to the master (parent) procinfo. */
2452 close_procinfo_files (pi
);
2454 pi
->gregs_valid
= 0;
2455 pi
->fpregs_valid
= 0;
2456 pi
->status_valid
= 0;
2457 pi
->threads_valid
= 0;
2462 /* Make the child process runnable. Normally we will then call
2463 procfs_wait and wait for it to stop again (unless gdb is async).
2465 If STEP is true, then arrange for the child to stop again after
2466 executing a single instruction. If SIGNO is zero, then cancel any
2467 pending signal; if non-zero, then arrange for the indicated signal
2468 to be delivered to the child when it runs. If PID is -1, then
2469 allow any child thread to run; if non-zero, then allow only the
2470 indicated thread to run. (not implemented yet). */
2473 procfs_target::resume (ptid_t ptid
, int step
, enum gdb_signal signo
)
2475 procinfo
*pi
, *thread
;
2478 /* FIXME: Check/reword. */
2480 /* prrun.prflags |= PRCFAULT; clear current fault.
2481 PRCFAULT may be replaced by a PCCFAULT call (proc_clear_current_fault)
2482 This basically leaves PRSTEP and PRCSIG.
2483 PRCSIG is like PCSSIG (proc_clear_current_signal).
2484 So basically PR_STEP is the sole argument that must be passed
2485 to proc_run_process. */
2487 /* Find procinfo for main process. */
2488 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2490 /* First cut: ignore pid argument. */
2493 /* Convert signal to host numbering. */
2494 if (signo
== 0 || (signo
== GDB_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
2497 native_signo
= gdb_signal_to_host (signo
);
2499 pi
->ignore_next_sigstop
= 0;
2501 /* Running the process voids all cached registers and status. */
2502 /* Void the threads' caches first. */
2503 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
2504 /* Void the process procinfo's caches. */
2505 invalidate_cache (NULL
, pi
, NULL
);
2507 if (ptid
.pid () != -1)
2509 /* Resume a specific thread, presumably suppressing the
2511 thread
= find_procinfo (ptid
.pid (), ptid
.lwp ());
2514 if (thread
->tid
!= 0)
2516 /* We're to resume a specific thread, and not the
2517 others. Set the child process's PR_ASYNC flag. */
2518 if (!proc_set_async (pi
))
2519 proc_error (pi
, "target_resume, set_async", __LINE__
);
2520 pi
= thread
; /* Substitute the thread's procinfo
2526 if (!proc_run_process (pi
, step
, native_signo
))
2529 warning (_("resume: target already running. "
2530 "Pretend to resume, and hope for the best!"));
2532 proc_error (pi
, "target_resume", __LINE__
);
2536 /* Set up to trace signals in the child process. */
2539 procfs_target::pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2542 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2545 prfillset (&signals
);
2547 for (signo
= 0; signo
< NSIG
; signo
++)
2549 int target_signo
= gdb_signal_from_host (signo
);
2550 if (target_signo
< pass_signals
.size () && pass_signals
[target_signo
])
2551 prdelset (&signals
, signo
);
2554 if (!proc_set_traced_signals (pi
, &signals
))
2555 proc_error (pi
, "pass_signals", __LINE__
);
2558 /* Print status information about the child process. */
2561 procfs_target::files_info ()
2563 struct inferior
*inf
= current_inferior ();
2565 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
2566 inf
->attach_flag
? "attached": "child",
2567 target_pid_to_str (inferior_ptid
).c_str ());
2570 /* Make it die. Wait for it to die. Clean up after it. Note: this
2571 should only be applied to the real process, not to an LWP, because
2572 of the check for parent-process. If we need this to work for an
2573 LWP, it needs some more logic. */
2576 unconditionally_kill_inferior (procinfo
*pi
)
2580 parent_pid
= proc_parent_pid (pi
);
2581 if (!proc_kill (pi
, SIGKILL
))
2582 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
2583 destroy_procinfo (pi
);
2585 /* If pi is GDB's child, wait for it to die. */
2586 if (parent_pid
== getpid ())
2587 /* FIXME: should we use waitpid to make sure we get the right event?
2588 Should we check the returned event? */
2593 ret
= waitpid (pi
->pid
, &status
, 0);
2600 /* We're done debugging it, and we want it to go away. Then we want
2601 GDB to forget all about it. */
2604 procfs_target::kill ()
2606 if (inferior_ptid
!= null_ptid
) /* ? */
2608 /* Find procinfo for main process. */
2609 procinfo
*pi
= find_procinfo (inferior_ptid
.pid (), 0);
2612 unconditionally_kill_inferior (pi
);
2613 target_mourn_inferior (inferior_ptid
);
2617 /* Forget we ever debugged this thing! */
2620 procfs_target::mourn_inferior ()
2624 if (inferior_ptid
!= null_ptid
)
2626 /* Find procinfo for main process. */
2627 pi
= find_procinfo (inferior_ptid
.pid (), 0);
2629 destroy_procinfo (pi
);
2632 generic_mourn_inferior ();
2634 maybe_unpush_target ();
2637 /* When GDB forks to create a runnable inferior process, this function
2638 is called on the parent side of the fork. It's job is to do
2639 whatever is necessary to make the child ready to be debugged, and
2640 then wait for the child to synchronize. */
2643 procfs_target::procfs_init_inferior (int pid
)
2649 pi
= create_procinfo (pid
, 0);
2651 perror (_("procfs: out of memory in 'init_inferior'"));
2653 if (!open_procinfo_files (pi
, FD_CTL
))
2654 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
2658 open_procinfo_files // done
2661 procfs_notice_signals
2666 /* If not stopped yet, wait for it to stop. */
2667 if (!(proc_flags (pi
) & PR_STOPPED
) && !(proc_wait_for_stop (pi
)))
2668 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
2670 /* Save some of the /proc state to be restored if we detach. */
2671 /* FIXME: Why? In case another debugger was debugging it?
2672 We're it's parent, for Ghu's sake! */
2673 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
2674 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
2675 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
2676 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
2677 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
2678 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
2679 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
2680 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
2681 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
2682 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
2684 fail
= procfs_debug_inferior (pi
);
2686 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
2688 /* FIXME: logically, we should really be turning OFF run-on-last-close,
2689 and possibly even turning ON kill-on-last-close at this point. But
2690 I can't make that change without careful testing which I don't have
2691 time to do right now... */
2692 /* Turn on run-on-last-close flag so that the child
2693 will die if GDB goes away for some reason. */
2694 if (!proc_set_run_on_last_close (pi
))
2695 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
2697 /* We now have have access to the lwpid of the main thread/lwp. */
2698 lwpid
= proc_get_current_thread (pi
);
2700 /* Create a procinfo for the main lwp. */
2701 create_procinfo (pid
, lwpid
);
2703 /* We already have a main thread registered in the thread table at
2704 this point, but it didn't have any lwp info yet. Notify the core
2705 about it. This changes inferior_ptid as well. */
2706 thread_change_ptid (this, ptid_t (pid
), ptid_t (pid
, lwpid
, 0));
2708 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
2711 /* When GDB forks to create a new process, this function is called on
2712 the child side of the fork before GDB exec's the user program. Its
2713 job is to make the child minimally debuggable, so that the parent
2714 GDB process can connect to the child and take over. This function
2715 should do only the minimum to make that possible, and to
2716 synchronize with the parent process. The parent process should
2717 take care of the details. */
2720 procfs_set_exec_trap (void)
2722 /* This routine called on the child side (inferior side)
2723 after GDB forks the inferior. It must use only local variables,
2724 because it may be sharing data space with its parent. */
2729 pi
= create_procinfo (getpid (), 0);
2731 perror_with_name (_("procfs: create_procinfo failed in child."));
2733 if (open_procinfo_files (pi
, FD_CTL
) == 0)
2735 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
2736 gdb_flush (gdb_stderr
);
2737 /* No need to call "dead_procinfo", because we're going to
2742 exitset
= XNEW (sysset_t
);
2743 premptyset (exitset
);
2744 praddset (exitset
, SYS_execve
);
2746 if (!proc_set_traced_sysexit (pi
, exitset
))
2748 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
2749 gdb_flush (gdb_stderr
);
2753 /* FIXME: should this be done in the parent instead? */
2754 /* Turn off inherit on fork flag so that all grand-children
2755 of gdb start with tracing flags cleared. */
2756 if (!proc_unset_inherit_on_fork (pi
))
2757 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
2759 /* Turn off run on last close flag, so that the child process
2760 cannot run away just because we close our handle on it.
2761 We want it to wait for the parent to attach. */
2762 if (!proc_unset_run_on_last_close (pi
))
2763 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
2765 /* FIXME: No need to destroy the procinfo --
2766 we have our own address space, and we're about to do an exec! */
2767 /*destroy_procinfo (pi);*/
2770 /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2).
2771 This avoids a possible deadlock gdb and its vfork'ed child. */
2773 procfs_pre_trace (void)
2777 /* This function is called BEFORE gdb forks the inferior process. Its
2778 only real responsibility is to set things up for the fork, and tell
2779 GDB which two functions to call after the fork (one for the parent,
2780 and one for the child).
2782 This function does a complicated search for a unix shell program,
2783 which it then uses to parse arguments and environment variables to
2784 be sent to the child. I wonder whether this code could not be
2785 abstracted out and shared with other unix targets such as
2789 procfs_target::create_inferior (const char *exec_file
,
2790 const std::string
&allargs
,
2791 char **env
, int from_tty
)
2793 const char *shell_file
= get_shell ();
2797 if (strchr (shell_file
, '/') == NULL
)
2800 /* We will be looking down the PATH to find shell_file. If we
2801 just do this the normal way (via execlp, which operates by
2802 attempting an exec for each element of the PATH until it
2803 finds one which succeeds), then there will be an exec for
2804 each failed attempt, each of which will cause a PR_SYSEXIT
2805 stop, and we won't know how to distinguish the PR_SYSEXIT's
2806 for these failed execs with the ones for successful execs
2807 (whether the exec has succeeded is stored at that time in the
2808 carry bit or some such architecture-specific and
2809 non-ABI-specified place).
2811 So I can't think of anything better than to search the PATH
2812 now. This has several disadvantages: (1) There is a race
2813 condition; if we find a file now and it is deleted before we
2814 exec it, we lose, even if the deletion leaves a valid file
2815 further down in the PATH, (2) there is no way to know exactly
2816 what an executable (in the sense of "capable of being
2817 exec'd") file is. Using access() loses because it may lose
2818 if the caller is the superuser; failing to use it loses if
2819 there are ACLs or some such. */
2823 /* FIXME-maybe: might want "set path" command so user can change what
2824 path is used from within GDB. */
2825 const char *path
= getenv ("PATH");
2827 struct stat statbuf
;
2830 path
= "/bin:/usr/bin";
2832 tryname
= (char *) alloca (strlen (path
) + strlen (shell_file
) + 2);
2833 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
2835 p1
= strchr (p
, ':');
2840 strncpy (tryname
, p
, len
);
2841 tryname
[len
] = '\0';
2842 strcat (tryname
, "/");
2843 strcat (tryname
, shell_file
);
2844 if (access (tryname
, X_OK
) < 0)
2846 if (stat (tryname
, &statbuf
) < 0)
2848 if (!S_ISREG (statbuf
.st_mode
))
2849 /* We certainly need to reject directories. I'm not quite
2850 as sure about FIFOs, sockets, etc., but I kind of doubt
2851 that people want to exec() these things. */
2856 /* Not found. This must be an error rather than merely passing
2857 the file to execlp(), because execlp() would try all the
2858 exec()s, causing GDB to get confused. */
2859 error (_("procfs:%d -- Can't find shell %s in PATH"),
2860 __LINE__
, shell_file
);
2862 shell_file
= tryname
;
2865 if (!target_is_pushed (this))
2868 pid
= fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
2869 NULL
, procfs_pre_trace
, shell_file
, NULL
);
2871 /* We have something that executes now. We'll be running through
2872 the shell at this point (if startup-with-shell is true), but the
2873 pid shouldn't change. */
2874 thread_info
*thr
= add_thread_silent (this, ptid_t (pid
));
2875 switch_to_thread (thr
);
2877 procfs_init_inferior (pid
);
2880 /* Callback for update_thread_list. Calls "add_thread". */
2883 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
2885 ptid_t gdb_threadid
= ptid_t (pi
->pid
, thread
->tid
, 0);
2887 thread_info
*thr
= find_thread_ptid (&the_procfs_target
, gdb_threadid
);
2888 if (thr
== NULL
|| thr
->state
== THREAD_EXITED
)
2889 add_thread (&the_procfs_target
, gdb_threadid
);
2894 /* Query all the threads that the target knows about, and give them
2895 back to GDB to add to its list. */
2898 procfs_target::update_thread_list ()
2904 /* Find procinfo for main process. */
2905 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2906 proc_update_threads (pi
);
2907 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
2910 /* Return true if the thread is still 'alive'. This guy doesn't
2911 really seem to be doing his job. Got to investigate how to tell
2912 when a thread is really gone. */
2915 procfs_target::thread_alive (ptid_t ptid
)
2921 thread
= ptid
.lwp ();
2922 /* If I don't know it, it ain't alive! */
2923 pi
= find_procinfo (proc
, thread
);
2927 /* If I can't get its status, it ain't alive!
2928 What's more, I need to forget about it! */
2929 if (!proc_get_status (pi
))
2931 destroy_procinfo (pi
);
2934 /* I couldn't have got its status if it weren't alive, so it's
2939 /* Convert PTID to a string. */
2942 procfs_target::pid_to_str (ptid_t ptid
)
2944 if (ptid
.lwp () == 0)
2945 return string_printf ("process %d", ptid
.pid ());
2947 return string_printf ("LWP %ld", ptid
.lwp ());
2950 /* Accepts an integer PID; Returns a string representing a file that
2951 can be opened to get the symbols for the child process. */
2954 procfs_target::pid_to_exec_file (int pid
)
2956 static char buf
[PATH_MAX
];
2957 char name
[PATH_MAX
];
2959 /* Solaris 11 introduced /proc/<proc-id>/execname. */
2960 xsnprintf (name
, sizeof (name
), "/proc/%d/execname", pid
);
2961 scoped_fd
fd (gdb_open_cloexec (name
, O_RDONLY
, 0));
2962 if (fd
.get () < 0 || read (fd
.get (), buf
, PATH_MAX
- 1) < 0)
2964 /* If that fails, fall back to /proc/<proc-id>/path/a.out introduced in
2968 xsnprintf (name
, sizeof (name
), "/proc/%d/path/a.out", pid
);
2969 len
= readlink (name
, buf
, PATH_MAX
- 1);
2979 /* Insert a watchpoint. */
2982 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
2988 pi
= find_procinfo_or_die (ptid
.pid () == -1 ?
2989 inferior_ptid
.pid () : ptid
.pid (),
2992 /* Translate from GDB's flags to /proc's. */
2993 if (len
> 0) /* len == 0 means delete watchpoint. */
2995 switch (rwflag
) { /* FIXME: need an enum! */
2996 case hw_write
: /* default watchpoint (write) */
2999 case hw_read
: /* read watchpoint */
3002 case hw_access
: /* access watchpoint */
3003 pflags
= WA_READ
| WA_WRITE
;
3005 case hw_execute
: /* execution HW breakpoint */
3008 default: /* Something weird. Return error. */
3011 if (after
) /* Stop after r/w access is completed. */
3012 pflags
|= WA_TRAPAFTER
;
3015 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
3017 if (errno
== E2BIG
) /* Typical error for no resources. */
3018 return -1; /* fail */
3019 /* GDB may try to remove the same watchpoint twice.
3020 If a remove request returns no match, don't error. */
3021 if (errno
== ESRCH
&& len
== 0)
3022 return 0; /* ignore */
3023 proc_error (pi
, "set_watchpoint", __LINE__
);
3028 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
3029 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
3030 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
3034 procfs_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int othertype
)
3036 /* Due to the way that proc_set_watchpoint() is implemented, host
3037 and target pointers must be of the same size. If they are not,
3038 we can't use hardware watchpoints. This limitation is due to the
3039 fact that proc_set_watchpoint() calls
3040 procfs_address_to_host_pointer(); a close inspection of
3041 procfs_address_to_host_pointer will reveal that an internal error
3042 will be generated when the host and target pointer sizes are
3044 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
3046 if (sizeof (void *) != TYPE_LENGTH (ptr_type
))
3049 /* Other tests here??? */
3054 /* Returns non-zero if process is stopped on a hardware watchpoint
3055 fault, else returns zero. */
3058 procfs_target::stopped_by_watchpoint ()
3062 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3064 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3065 if (proc_why (pi
) == PR_FAULTED
)
3066 if (proc_what (pi
) == FLTWATCH
)
3071 /* Returns 1 if the OS knows the position of the triggered watchpoint,
3072 and sets *ADDR to that address. Returns 0 if OS cannot report that
3073 address. This function is only called if
3074 procfs_stopped_by_watchpoint returned 1, thus no further checks are
3075 done. The function also assumes that ADDR is not NULL. */
3078 procfs_target::stopped_data_address (CORE_ADDR
*addr
)
3082 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3083 return proc_watchpoint_address (pi
, addr
);
3087 procfs_target::insert_watchpoint (CORE_ADDR addr
, int len
,
3088 enum target_hw_bp_type type
,
3089 struct expression
*cond
)
3091 if (!target_have_steppable_watchpoint ()
3092 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
3093 /* When a hardware watchpoint fires off the PC will be left at
3094 the instruction following the one which caused the
3095 watchpoint. It will *NOT* be necessary for GDB to step over
3097 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 1);
3099 /* When a hardware watchpoint fires off the PC will be left at
3100 the instruction which caused the watchpoint. It will be
3101 necessary for GDB to step over the watchpoint. */
3102 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 0);
3106 procfs_target::remove_watchpoint (CORE_ADDR addr
, int len
,
3107 enum target_hw_bp_type type
,
3108 struct expression
*cond
)
3110 return procfs_set_watchpoint (inferior_ptid
, addr
, 0, 0, 0);
3114 procfs_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
3116 /* The man page for proc(4) on Solaris 2.6 and up says that the
3117 system can support "thousands" of hardware watchpoints, but gives
3118 no method for finding out how many; It doesn't say anything about
3119 the allowed size for the watched area either. So we just tell
3124 /* Memory Mappings Functions: */
3126 /* Call a callback function once for each mapping, passing it the
3127 mapping, an optional secondary callback function, and some optional
3128 opaque data. Quit and return the first non-zero value returned
3131 PI is the procinfo struct for the process to be mapped. FUNC is
3132 the callback function to be called by this iterator. DATA is the
3133 optional opaque data to be passed to the callback function.
3134 CHILD_FUNC is the optional secondary function pointer to be passed
3135 to the child function. Returns the first non-zero return value
3136 from the callback function, or zero. */
3139 iterate_over_mappings (procinfo
*pi
, find_memory_region_ftype child_func
,
3141 int (*func
) (struct prmap
*map
,
3142 find_memory_region_ftype child_func
,
3145 char pathname
[MAX_PROC_NAME_SIZE
];
3146 struct prmap
*prmaps
;
3147 struct prmap
*prmap
;
3152 /* Get the number of mappings, allocate space,
3153 and read the mappings into prmaps. */
3155 xsnprintf (pathname
, sizeof (pathname
), "/proc/%d/map", pi
->pid
);
3157 scoped_fd
map_fd (open (pathname
, O_RDONLY
));
3158 if (map_fd
.get () < 0)
3159 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
3161 /* Use stat to determine the file size, and compute
3162 the number of prmap_t objects it contains. */
3163 if (fstat (map_fd
.get (), &sbuf
) != 0)
3164 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
3166 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
3167 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
3168 if (read (map_fd
.get (), (char *) prmaps
, nmap
* sizeof (*prmaps
))
3169 != (nmap
* sizeof (*prmaps
)))
3170 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
3172 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
3174 funcstat
= (*func
) (prmap
, child_func
, data
);
3182 /* Implements the to_find_memory_regions method. Calls an external
3183 function for each memory region.
3184 Returns the integer value returned by the callback. */
3187 find_memory_regions_callback (struct prmap
*map
,
3188 find_memory_region_ftype func
, void *data
)
3190 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
3192 (map
->pr_mflags
& MA_READ
) != 0,
3193 (map
->pr_mflags
& MA_WRITE
) != 0,
3194 (map
->pr_mflags
& MA_EXEC
) != 0,
3195 1, /* MODIFIED is unknown, pass it as true. */
3199 /* External interface. Calls a callback function once for each
3200 mapped memory region in the child process, passing as arguments:
3202 CORE_ADDR virtual_address,
3204 int read, TRUE if region is readable by the child
3205 int write, TRUE if region is writable by the child
3206 int execute TRUE if region is executable by the child.
3208 Stops iterating and returns the first non-zero value returned by
3212 procfs_target::find_memory_regions (find_memory_region_ftype func
, void *data
)
3214 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3216 return iterate_over_mappings (pi
, func
, data
,
3217 find_memory_regions_callback
);
3220 /* Returns an ascii representation of a memory mapping's flags. */
3223 mappingflags (long flags
)
3225 static char asciiflags
[8];
3227 strcpy (asciiflags
, "-------");
3228 if (flags
& MA_STACK
)
3229 asciiflags
[1] = 's';
3230 if (flags
& MA_BREAK
)
3231 asciiflags
[2] = 'b';
3232 if (flags
& MA_SHARED
)
3233 asciiflags
[3] = 's';
3234 if (flags
& MA_READ
)
3235 asciiflags
[4] = 'r';
3236 if (flags
& MA_WRITE
)
3237 asciiflags
[5] = 'w';
3238 if (flags
& MA_EXEC
)
3239 asciiflags
[6] = 'x';
3240 return (asciiflags
);
3243 /* Callback function, does the actual work for 'info proc
3247 info_mappings_callback (struct prmap
*map
, find_memory_region_ftype ignore
,
3250 unsigned int pr_off
;
3252 pr_off
= (unsigned int) map
->pr_offset
;
3254 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
3255 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
3256 (unsigned long) map
->pr_vaddr
,
3257 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3258 (unsigned long) map
->pr_size
,
3260 mappingflags (map
->pr_mflags
));
3262 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
3263 (unsigned long) map
->pr_vaddr
,
3264 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3265 (unsigned long) map
->pr_size
,
3267 mappingflags (map
->pr_mflags
));
3272 /* Implement the "info proc mappings" subcommand. */
3275 info_proc_mappings (procinfo
*pi
, int summary
)
3278 return; /* No output for summary mode. */
3280 printf_filtered (_("Mapped address spaces:\n\n"));
3281 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
3282 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3289 printf_filtered (" %18s %18s %10s %10s %7s\n",
3296 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
3297 printf_filtered ("\n");
3300 /* Implement the "info proc" command. */
3303 procfs_target::info_proc (const char *args
, enum info_proc_what what
)
3305 procinfo
*process
= NULL
;
3306 procinfo
*thread
= NULL
;
3323 error (_("Not supported on this target."));
3326 gdb_argv
built_argv (args
);
3327 for (char *arg
: built_argv
)
3329 if (isdigit (arg
[0]))
3331 pid
= strtoul (arg
, &tmp
, 10);
3333 tid
= strtoul (++tmp
, NULL
, 10);
3335 else if (arg
[0] == '/')
3337 tid
= strtoul (arg
+ 1, NULL
, 10);
3341 procinfo_up temporary_procinfo
;
3343 pid
= inferior_ptid
.pid ();
3345 error (_("No current process: you must name one."));
3348 /* Have pid, will travel.
3349 First see if it's a process we're already debugging. */
3350 process
= find_procinfo (pid
, 0);
3351 if (process
== NULL
)
3353 /* No. So open a procinfo for it, but
3354 remember to close it again when finished. */
3355 process
= create_procinfo (pid
, 0);
3356 temporary_procinfo
.reset (process
);
3357 if (!open_procinfo_files (process
, FD_CTL
))
3358 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
3362 thread
= create_procinfo (pid
, tid
);
3366 printf_filtered (_("process %d flags:\n"), process
->pid
);
3367 proc_prettyprint_flags (proc_flags (process
), 1);
3368 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
3369 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
3370 if (proc_get_nthreads (process
) > 1)
3371 printf_filtered ("Process has %d threads.\n",
3372 proc_get_nthreads (process
));
3376 printf_filtered (_("thread %d flags:\n"), thread
->tid
);
3377 proc_prettyprint_flags (proc_flags (thread
), 1);
3378 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
3379 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
3383 info_proc_mappings (process
, 0);
3388 /* Modify the status of the system call identified by SYSCALLNUM in
3389 the set of syscalls that are currently traced/debugged.
3391 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
3392 will be updated. Otherwise, the exit syscalls set will be updated.
3394 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
3395 will be disabled. */
3398 proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
, int entry_or_exit
,
3399 int mode
, int from_tty
)
3403 if (entry_or_exit
== PR_SYSENTRY
)
3404 sysset
= proc_get_traced_sysentry (pi
, NULL
);
3406 sysset
= proc_get_traced_sysexit (pi
, NULL
);
3409 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
3411 if (mode
== FLAG_SET
)
3412 praddset (sysset
, syscallnum
);
3414 prdelset (sysset
, syscallnum
);
3416 if (entry_or_exit
== PR_SYSENTRY
)
3418 if (!proc_set_traced_sysentry (pi
, sysset
))
3419 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
3423 if (!proc_set_traced_sysexit (pi
, sysset
))
3424 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
3429 proc_trace_syscalls (const char *args
, int from_tty
, int entry_or_exit
, int mode
)
3433 if (inferior_ptid
.pid () <= 0)
3434 error (_("you must be debugging a process to use this command."));
3436 if (args
== NULL
|| args
[0] == 0)
3437 error_no_arg (_("system call to trace"));
3439 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3440 if (isdigit (args
[0]))
3442 const int syscallnum
= atoi (args
);
3444 proc_trace_syscalls_1 (pi
, syscallnum
, entry_or_exit
, mode
, from_tty
);
3449 proc_trace_sysentry_cmd (const char *args
, int from_tty
)
3451 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
3455 proc_trace_sysexit_cmd (const char *args
, int from_tty
)
3457 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
3461 proc_untrace_sysentry_cmd (const char *args
, int from_tty
)
3463 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
3467 proc_untrace_sysexit_cmd (const char *args
, int from_tty
)
3469 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
3472 void _initialize_procfs ();
3474 _initialize_procfs ()
3476 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
3477 _("Give a trace of entries into the syscall."));
3478 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
3479 _("Give a trace of exits from the syscall."));
3480 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
3481 _("Cancel a trace of entries into the syscall."));
3482 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
3483 _("Cancel a trace of exits from the syscall."));
3485 add_inf_child_target (&the_procfs_target
);
3488 /* =================== END, GDB "MODULE" =================== */
3492 /* miscellaneous stubs: */
3494 /* The following satisfy a few random symbols mostly created by the
3495 solaris threads implementation, which I will chase down later. */
3497 /* Return a pid for which we guarantee we will be able to find a
3501 procfs_first_available (void)
3503 return ptid_t (procinfo_list
? procinfo_list
->pid
: -1);
3506 /* =================== GCORE .NOTE "MODULE" =================== */
3509 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3510 gdb::unique_xmalloc_ptr
<char> ¬e_data
,
3511 int *note_size
, enum gdb_signal stop_signal
)
3513 struct regcache
*regcache
= get_thread_regcache (&the_procfs_target
, ptid
);
3514 gdb_gregset_t gregs
;
3515 gdb_fpregset_t fpregs
;
3516 unsigned long merged_pid
;
3518 merged_pid
= ptid
.lwp () << 16 | ptid
.pid ();
3520 /* This part is the old method for fetching registers.
3521 It should be replaced by the newer one using regsets
3522 once it is implemented in this platform:
3523 gdbarch_iterate_over_regset_sections(). */
3525 target_fetch_registers (regcache
, -1);
3527 fill_gregset (regcache
, &gregs
, -1);
3528 note_data
.reset (elfcore_write_lwpstatus (obfd
,
3529 note_data
.release (),
3534 fill_fpregset (regcache
, &fpregs
, -1);
3535 note_data
.reset (elfcore_write_prfpreg (obfd
,
3536 note_data
.release (),
3542 struct procfs_corefile_thread_data
3544 procfs_corefile_thread_data (bfd
*obfd
,
3545 gdb::unique_xmalloc_ptr
<char> ¬e_data
,
3546 int *note_size
, gdb_signal stop_signal
)
3547 : obfd (obfd
), note_data (note_data
), note_size (note_size
),
3548 stop_signal (stop_signal
)
3552 gdb::unique_xmalloc_ptr
<char> ¬e_data
;
3554 enum gdb_signal stop_signal
;
3558 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
3560 struct procfs_corefile_thread_data
*args
3561 = (struct procfs_corefile_thread_data
*) data
;
3565 ptid_t ptid
= ptid_t (pi
->pid
, thread
->tid
, 0);
3567 procfs_do_thread_registers (args
->obfd
, ptid
,
3576 find_signalled_thread (struct thread_info
*info
, void *data
)
3578 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
3579 && info
->ptid
.pid () == inferior_ptid
.pid ())
3585 static enum gdb_signal
3586 find_stop_signal (void)
3588 struct thread_info
*info
=
3589 iterate_over_threads (find_signalled_thread
, NULL
);
3592 return info
->suspend
.stop_signal
;
3594 return GDB_SIGNAL_0
;
3597 gdb::unique_xmalloc_ptr
<char>
3598 procfs_target::make_corefile_notes (bfd
*obfd
, int *note_size
)
3600 gdb_gregset_t gregs
;
3601 char fname
[16] = {'\0'};
3602 char psargs
[80] = {'\0'};
3603 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3604 gdb::unique_xmalloc_ptr
<char> note_data
;
3605 const char *inf_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
.reset (elfcore_write_prpsinfo (obfd
,
3628 note_data
.release (),
3633 stop_signal
= find_stop_signal ();
3635 fill_gregset (get_current_regcache (), &gregs
, -1);
3636 note_data
.reset (elfcore_write_pstatus (obfd
, note_data
.release (), note_size
,
3637 inferior_ptid
.pid (),
3638 stop_signal
, &gregs
));
3640 procfs_corefile_thread_data
thread_args (obfd
, note_data
, note_size
,
3642 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
,
3645 gdb::optional
<gdb::byte_vector
> auxv
=
3646 target_read_alloc (current_top_target (), TARGET_OBJECT_AUXV
, NULL
);
3647 if (auxv
&& !auxv
->empty ())
3648 note_data
.reset (elfcore_write_note (obfd
, note_data
.release (), note_size
,
3649 "CORE", NT_AUXV
, auxv
->data (),
3654 /* =================== END GCORE .NOTE "MODULE" =================== */