1 /* Machine independent support for Solaris /proc (process file system) for GDB.
3 Copyright (C) 1999-2022 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"
45 #include "observable.h"
46 #include "gdbsupport/scoped_fd.h"
47 #include "gdbsupport/pathstuff.h"
49 /* This module provides the interface between GDB and the
50 /proc file system, which is used on many versions of Unix
51 as a means for debuggers to control other processes.
53 /proc works by imitating a file system: you open a simulated file
54 that represents the process you wish to interact with, and perform
55 operations on that "file" in order to examine or change the state
58 The most important thing to know about /proc and this module is
59 that there are two very different interfaces to /proc:
61 One that uses the ioctl system call, and another that uses read
62 and write system calls.
64 This module supports only the Solaris version of the read/write
67 #include <sys/types.h>
68 #include <dirent.h> /* opendir/readdir, for listing the LWP's */
70 #include <fcntl.h> /* for O_RDONLY */
71 #include <unistd.h> /* for "X_OK" */
72 #include <sys/stat.h> /* for struct stat */
74 /* Note: procfs-utils.h must be included after the above system header
75 files, because it redefines various system calls using macros.
76 This may be incompatible with the prototype declarations. */
78 #include "proc-utils.h"
80 /* Prototypes for supply_gregset etc. */
83 /* =================== TARGET_OPS "MODULE" =================== */
85 /* This module defines the GDB target vector and its methods. */
88 static enum target_xfer_status
procfs_xfer_memory (gdb_byte
*,
93 class procfs_target final
: public inf_child_target
96 void create_inferior (const char *, const std::string
&,
97 char **, int) override
;
99 void kill () override
;
101 void mourn_inferior () override
;
103 void attach (const char *, int) override
;
104 void detach (inferior
*inf
, int) override
;
106 void resume (ptid_t
, int, enum gdb_signal
) override
;
107 ptid_t
wait (ptid_t
, struct target_waitstatus
*, target_wait_flags
) override
;
109 void fetch_registers (struct regcache
*, int) override
;
110 void store_registers (struct regcache
*, int) override
;
112 enum target_xfer_status
xfer_partial (enum target_object object
,
115 const gdb_byte
*writebuf
,
116 ULONGEST offset
, ULONGEST len
,
117 ULONGEST
*xfered_len
) override
;
119 void pass_signals (gdb::array_view
<const unsigned char>) override
;
121 void files_info () override
;
123 void update_thread_list () override
;
125 bool thread_alive (ptid_t ptid
) override
;
127 std::string
pid_to_str (ptid_t
) override
;
129 char *pid_to_exec_file (int pid
) override
;
131 thread_control_capabilities
get_thread_control_capabilities () override
132 { return tc_schedlock
; }
134 /* find_memory_regions support method for gcore */
135 int find_memory_regions (find_memory_region_ftype func
, void *data
)
138 gdb::unique_xmalloc_ptr
<char> make_corefile_notes (bfd
*, int *) override
;
140 bool info_proc (const char *, enum info_proc_what
) override
;
142 #if PR_MODEL_NATIVE == PR_MODEL_LP64
143 int auxv_parse (gdb_byte
**readptr
,
144 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
148 bool stopped_by_watchpoint () override
;
150 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
151 struct expression
*) override
;
153 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
154 struct expression
*) override
;
156 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
158 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
159 bool stopped_data_address (CORE_ADDR
*) override
;
161 void procfs_init_inferior (int pid
);
164 static procfs_target the_procfs_target
;
166 #if PR_MODEL_NATIVE == PR_MODEL_LP64
167 /* When GDB is built as 64-bit application on Solaris, the auxv data
168 is presented in 64-bit format. We need to provide a custom parser
171 procfs_target::auxv_parse (gdb_byte
**readptr
,
172 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
174 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
175 gdb_byte
*ptr
= *readptr
;
180 if (endptr
- ptr
< 8 * 2)
183 *typep
= extract_unsigned_integer (ptr
, 4, byte_order
);
185 /* The size of data is always 64-bit. If the application is 32-bit,
186 it will be zero extended, as expected. */
187 *valp
= extract_unsigned_integer (ptr
, 8, byte_order
);
195 /* =================== END, TARGET_OPS "MODULE" =================== */
197 /* =================== STRUCT PROCINFO "MODULE" =================== */
199 /* FIXME: this comment will soon be out of date W.R.T. threads. */
201 /* The procinfo struct is a wrapper to hold all the state information
202 concerning a /proc process. There should be exactly one procinfo
203 for each process, and since GDB currently can debug only one
204 process at a time, that means there should be only one procinfo.
205 All of the LWP's of a process can be accessed indirectly thru the
206 single process procinfo.
208 However, against the day when GDB may debug more than one process,
209 this data structure is kept in a list (which for now will hold no
210 more than one member), and many functions will have a pointer to a
211 procinfo as an argument.
213 There will be a separate procinfo structure for use by the (not yet
214 implemented) "info proc" command, so that we can print useful
215 information about any random process without interfering with the
216 inferior's procinfo information. */
218 /* format strings for /proc paths */
219 #define CTL_PROC_NAME_FMT "/proc/%d/ctl"
220 #define AS_PROC_NAME_FMT "/proc/%d/as"
221 #define MAP_PROC_NAME_FMT "/proc/%d/map"
222 #define STATUS_PROC_NAME_FMT "/proc/%d/status"
223 #define MAX_PROC_NAME_SIZE sizeof("/proc/999999/lwp/0123456789/lwpstatus")
225 typedef struct procinfo
{
226 struct procinfo
*next
;
227 int pid
; /* Process ID */
228 int tid
; /* Thread/LWP id */
232 int ignore_next_sigstop
;
234 int ctl_fd
; /* File descriptor for /proc control file */
235 int status_fd
; /* File descriptor for /proc status file */
236 int as_fd
; /* File descriptor for /proc as file */
238 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
240 fltset_t saved_fltset
; /* Saved traced hardware fault set */
241 sigset_t saved_sigset
; /* Saved traced signal set */
242 sigset_t saved_sighold
; /* Saved held signal set */
243 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
244 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
246 pstatus_t prstatus
; /* Current process status info */
248 struct procinfo
*thread_list
;
250 int status_valid
: 1;
252 int fpregs_valid
: 1;
253 int threads_valid
: 1;
256 static char errmsg
[128]; /* shared error msg buffer */
258 /* Function prototypes for procinfo module: */
260 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
261 static procinfo
*find_procinfo (int pid
, int tid
);
262 static procinfo
*create_procinfo (int pid
, int tid
);
263 static void destroy_procinfo (procinfo
*p
);
264 static void dead_procinfo (procinfo
*p
, const char *msg
, int killp
);
265 static int open_procinfo_files (procinfo
*p
, int which
);
266 static void close_procinfo_files (procinfo
*p
);
268 static int iterate_over_mappings
269 (procinfo
*pi
, find_memory_region_ftype child_func
, void *data
,
270 int (*func
) (struct prmap
*map
, find_memory_region_ftype child_func
,
273 /* The head of the procinfo list: */
274 static procinfo
*procinfo_list
;
276 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
280 find_procinfo (int pid
, int tid
)
284 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
291 /* Don't check threads_valid. If we're updating the
292 thread_list, we want to find whatever threads are already
293 here. This means that in general it is the caller's
294 responsibility to check threads_valid and update before
295 calling find_procinfo, if the caller wants to find a new
298 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
306 /* Calls find_procinfo, but errors on failure. */
309 find_procinfo_or_die (int pid
, int tid
)
311 procinfo
*pi
= find_procinfo (pid
, tid
);
316 error (_("procfs: couldn't find pid %d "
317 "(kernel thread %d) in procinfo list."),
320 error (_("procfs: couldn't find pid %d in procinfo list."), pid
);
325 /* Wrapper for `open'. The appropriate open call is attempted; if
326 unsuccessful, it will be retried as many times as needed for the
327 EAGAIN and EINTR conditions.
329 For other conditions, retry the open a limited number of times. In
330 addition, a short sleep is imposed prior to retrying the open. The
331 reason for this sleep is to give the kernel a chance to catch up
332 and create the file in question in the event that GDB "wins" the
333 race to open a file before the kernel has created it. */
336 open_with_retry (const char *pathname
, int flags
)
338 int retries_remaining
, status
;
340 retries_remaining
= 2;
344 status
= open (pathname
, flags
);
346 if (status
>= 0 || retries_remaining
== 0)
348 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
358 /* Open the file descriptor for the process or LWP. We only open the
359 control file descriptor; the others are opened lazily as needed.
360 Returns the file descriptor, or zero for failure. */
362 enum { FD_CTL
, FD_STATUS
, FD_AS
};
365 open_procinfo_files (procinfo
*pi
, int which
)
367 char tmp
[MAX_PROC_NAME_SIZE
];
370 /* This function is getting ALMOST long enough to break up into
371 several. Here is some rationale:
373 There are several file descriptors that may need to be open
374 for any given process or LWP. The ones we're interested in are:
375 - control (ctl) write-only change the state
376 - status (status) read-only query the state
377 - address space (as) read/write access memory
378 - map (map) read-only virtual addr map
379 Most of these are opened lazily as they are needed.
380 The pathnames for the 'files' for an LWP look slightly
381 different from those of a first-class process:
382 Pathnames for a process (<proc-id>):
384 /proc/<proc-id>/status
387 Pathnames for an LWP (lwp-id):
388 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
389 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
390 An LWP has no map or address space file descriptor, since
391 the memory map and address space are shared by all LWPs. */
393 /* In this case, there are several different file descriptors that
394 we might be asked to open. The control file descriptor will be
395 opened early, but the others will be opened lazily as they are
398 strcpy (tmp
, pi
->pathname
);
399 switch (which
) { /* Which file descriptor to open? */
402 strcat (tmp
, "/lwpctl");
404 strcat (tmp
, "/ctl");
405 fd
= open_with_retry (tmp
, O_WRONLY
);
412 return 0; /* There is no 'as' file descriptor for an lwp. */
414 fd
= open_with_retry (tmp
, O_RDWR
);
421 strcat (tmp
, "/lwpstatus");
423 strcat (tmp
, "/status");
424 fd
= open_with_retry (tmp
, O_RDONLY
);
430 return 0; /* unknown file descriptor */
433 return 1; /* success */
436 /* Allocate a data structure and link it into the procinfo list.
437 First tries to find a pre-existing one (FIXME: why?). Returns the
438 pointer to new procinfo struct. */
441 create_procinfo (int pid
, int tid
)
443 procinfo
*pi
, *parent
= NULL
;
445 pi
= find_procinfo (pid
, tid
);
447 return pi
; /* Already exists, nothing to do. */
449 /* Find parent before doing malloc, to save having to cleanup. */
451 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
453 doesn't exist yet? */
455 pi
= XNEW (procinfo
);
456 memset (pi
, 0, sizeof (procinfo
));
460 pi
->saved_entryset
= XNEW (sysset_t
);
461 pi
->saved_exitset
= XNEW (sysset_t
);
463 /* Chain into list. */
466 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), "/proc/%d", pid
);
467 pi
->next
= procinfo_list
;
472 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), "/proc/%d/lwp/%d",
474 pi
->next
= parent
->thread_list
;
475 parent
->thread_list
= pi
;
480 /* Close all file descriptors associated with the procinfo. */
483 close_procinfo_files (procinfo
*pi
)
489 if (pi
->status_fd
> 0)
490 close (pi
->status_fd
);
491 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
494 /* Destructor function. Close, unlink and deallocate the object. */
497 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
501 /* Step one: unlink the procinfo from its list. */
505 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
508 ptr
->next
= pi
->next
;
512 /* Step two: close any open file descriptors. */
513 close_procinfo_files (pi
);
515 /* Step three: free the memory. */
516 xfree (pi
->saved_entryset
);
517 xfree (pi
->saved_exitset
);
522 destroy_procinfo (procinfo
*pi
)
526 if (pi
->tid
!= 0) /* Destroy a thread procinfo. */
528 tmp
= find_procinfo (pi
->pid
, 0); /* Find the parent process. */
529 destroy_one_procinfo (&tmp
->thread_list
, pi
);
531 else /* Destroy a process procinfo and all its threads. */
533 /* First destroy the children, if any; */
534 while (pi
->thread_list
!= NULL
)
535 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
536 /* Then destroy the parent. Genocide!!! */
537 destroy_one_procinfo (&procinfo_list
, pi
);
541 /* A deleter that calls destroy_procinfo. */
542 struct procinfo_deleter
544 void operator() (procinfo
*pi
) const
546 destroy_procinfo (pi
);
550 typedef std::unique_ptr
<procinfo
, procinfo_deleter
> procinfo_up
;
552 enum { NOKILL
, KILL
};
554 /* To be called on a non_recoverable error for a procinfo. Prints
555 error messages, optionally sends a SIGKILL to the process, then
556 destroys the data structure. */
559 dead_procinfo (procinfo
*pi
, const char *msg
, int kill_p
)
564 print_sys_errmsg (pi
->pathname
, errno
);
567 xsnprintf (procfile
, sizeof (procfile
), "process %d", pi
->pid
);
568 print_sys_errmsg (procfile
, errno
);
571 kill (pi
->pid
, SIGKILL
);
573 destroy_procinfo (pi
);
577 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
579 /* =================== /proc "MODULE" =================== */
581 /* This "module" is the interface layer between the /proc system API
582 and the gdb target vector functions. This layer consists of access
583 functions that encapsulate each of the basic operations that we
584 need to use from the /proc API.
586 The main motivation for this layer is to hide the fact that there
587 were two very different implementations of the /proc API. */
589 static long proc_flags (procinfo
*pi
);
590 static int proc_why (procinfo
*pi
);
591 static int proc_what (procinfo
*pi
);
592 static int proc_set_current_signal (procinfo
*pi
, int signo
);
593 static int proc_get_current_thread (procinfo
*pi
);
594 static int proc_iterate_over_threads
596 int (*func
) (procinfo
*, procinfo
*, void *),
600 proc_warn (procinfo
*pi
, const char *func
, int line
)
602 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
603 func
, line
, pi
->pathname
);
604 print_sys_errmsg (errmsg
, errno
);
608 proc_error (procinfo
*pi
, const char *func
, int line
)
610 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
611 func
, line
, pi
->pathname
);
612 perror_with_name (errmsg
);
615 /* Updates the status struct in the procinfo. There is a 'valid'
616 flag, to let other functions know when this function needs to be
617 called (so the status is only read when it is needed). The status
618 file descriptor is also only opened when it is needed. Returns
619 non-zero for success, zero for failure. */
622 proc_get_status (procinfo
*pi
)
624 /* Status file descriptor is opened "lazily". */
625 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
627 pi
->status_valid
= 0;
631 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
632 pi
->status_valid
= 0; /* fail */
635 /* Sigh... I have to read a different data structure,
636 depending on whether this is a main process or an LWP. */
638 pi
->status_valid
= (read (pi
->status_fd
,
639 (char *) &pi
->prstatus
.pr_lwp
,
640 sizeof (lwpstatus_t
))
641 == sizeof (lwpstatus_t
));
644 pi
->status_valid
= (read (pi
->status_fd
,
645 (char *) &pi
->prstatus
,
647 == sizeof (pstatus_t
));
651 if (pi
->status_valid
)
653 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
656 proc_get_current_thread (pi
));
659 /* The status struct includes general regs, so mark them valid too. */
660 pi
->gregs_valid
= pi
->status_valid
;
661 /* In the read/write multiple-fd model, the status struct includes
662 the fp regs too, so mark them valid too. */
663 pi
->fpregs_valid
= pi
->status_valid
;
664 return pi
->status_valid
; /* True if success, false if failure. */
667 /* Returns the process flags (pr_flags field). */
670 proc_flags (procinfo
*pi
)
672 if (!pi
->status_valid
)
673 if (!proc_get_status (pi
))
674 return 0; /* FIXME: not a good failure value (but what is?) */
676 return pi
->prstatus
.pr_lwp
.pr_flags
;
679 /* Returns the pr_why field (why the process stopped). */
682 proc_why (procinfo
*pi
)
684 if (!pi
->status_valid
)
685 if (!proc_get_status (pi
))
686 return 0; /* FIXME: not a good failure value (but what is?) */
688 return pi
->prstatus
.pr_lwp
.pr_why
;
691 /* Returns the pr_what field (details of why the process stopped). */
694 proc_what (procinfo
*pi
)
696 if (!pi
->status_valid
)
697 if (!proc_get_status (pi
))
698 return 0; /* FIXME: not a good failure value (but what is?) */
700 return pi
->prstatus
.pr_lwp
.pr_what
;
703 /* This function is only called when PI is stopped by a watchpoint.
704 Assuming the OS supports it, write to *ADDR the data address which
705 triggered it and return 1. Return 0 if it is not possible to know
709 proc_watchpoint_address (procinfo
*pi
, CORE_ADDR
*addr
)
711 if (!pi
->status_valid
)
712 if (!proc_get_status (pi
))
715 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
716 builtin_type (target_gdbarch ())->builtin_data_ptr
,
717 (gdb_byte
*) &pi
->prstatus
.pr_lwp
.pr_info
.si_addr
);
721 /* Returns the pr_nsysarg field (number of args to the current
725 proc_nsysarg (procinfo
*pi
)
727 if (!pi
->status_valid
)
728 if (!proc_get_status (pi
))
731 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
734 /* Returns the pr_sysarg field (pointer to the arguments of current
738 proc_sysargs (procinfo
*pi
)
740 if (!pi
->status_valid
)
741 if (!proc_get_status (pi
))
744 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
747 /* Set or reset any of the following process flags:
748 PR_FORK -- forked child will inherit trace flags
749 PR_RLC -- traced process runs when last /proc file closed.
750 PR_KLC -- traced process is killed when last /proc file closed.
751 PR_ASYNC -- LWP's get to run/stop independently.
753 This function is done using read/write [PCSET/PCRESET/PCUNSET].
757 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
758 mode -- 1 for set, 0 for reset.
760 Returns non-zero for success, zero for failure. */
762 enum { FLAG_RESET
, FLAG_SET
};
765 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
767 long win
= 0; /* default to fail */
769 /* These operations affect the process as a whole, and applying them
770 to an individual LWP has the same meaning as applying them to the
771 main process. Therefore, if we're ever called with a pointer to
772 an LWP's procinfo, let's substitute the process's procinfo and
773 avoid opening the LWP's file descriptor unnecessarily. */
776 pi
= find_procinfo_or_die (pi
->pid
, 0);
780 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC). */
782 else /* Reset the flag. */
786 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
788 /* The above operation renders the procinfo's cached pstatus
790 pi
->status_valid
= 0;
793 warning (_("procfs: modify_flag failed to turn %s %s"),
794 flag
== PR_FORK
? "PR_FORK" :
795 flag
== PR_RLC
? "PR_RLC" :
796 flag
== PR_ASYNC
? "PR_ASYNC" :
797 flag
== PR_KLC
? "PR_KLC" :
799 mode
== FLAG_RESET
? "off" : "on");
804 /* Set the run_on_last_close flag. Process with all threads will
805 become runnable when debugger closes all /proc fds. Returns
806 non-zero for success, zero for failure. */
809 proc_set_run_on_last_close (procinfo
*pi
)
811 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
814 /* Reset the run_on_last_close flag. The process will NOT become
815 runnable when debugger closes its file handles. Returns non-zero
816 for success, zero for failure. */
819 proc_unset_run_on_last_close (procinfo
*pi
)
821 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
824 /* Reset inherit_on_fork flag. If the process forks a child while we
825 are registered for events in the parent, then we will NOT receive
826 events from the child. Returns non-zero for success, zero for
830 proc_unset_inherit_on_fork (procinfo
*pi
)
832 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
835 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
836 (signal etc.), the remaining LWPs will continue to run. Returns
837 non-zero for success, zero for failure. */
840 proc_set_async (procinfo
*pi
)
842 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
845 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
846 (signal etc.), then all other LWPs will stop as well. Returns
847 non-zero for success, zero for failure. */
850 proc_unset_async (procinfo
*pi
)
852 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
855 /* Request the process/LWP to stop. Does not wait. Returns non-zero
856 for success, zero for failure. */
859 proc_stop_process (procinfo
*pi
)
863 /* We might conceivably apply this operation to an LWP, and the
864 LWP's ctl file descriptor might not be open. */
866 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
870 procfs_ctl_t cmd
= PCSTOP
;
872 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
878 /* Wait for the process or LWP to stop (block until it does). Returns
879 non-zero for success, zero for failure. */
882 proc_wait_for_stop (procinfo
*pi
)
886 /* We should never have to apply this operation to any procinfo
887 except the one for the main process. If that ever changes for
888 any reason, then take out the following clause and replace it
889 with one that makes sure the ctl_fd is open. */
892 pi
= find_procinfo_or_die (pi
->pid
, 0);
894 procfs_ctl_t cmd
= PCWSTOP
;
898 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
900 clear_sigint_trap ();
902 /* We been runnin' and we stopped -- need to update status. */
903 pi
->status_valid
= 0;
908 /* Make the process or LWP runnable.
910 Options (not all are implemented):
912 - clear current fault
913 - clear current signal
914 - abort the current system call
915 - stop as soon as finished with system call
917 Always clears the current fault. PI is the process or LWP to
918 operate on. If STEP is true, set the process or LWP to trap after
919 one instruction. If SIGNO is zero, clear the current signal if
920 any; if non-zero, set the current signal to this one. Returns
921 non-zero for success, zero for failure. */
924 proc_run_process (procinfo
*pi
, int step
, int signo
)
929 /* We will probably have to apply this operation to individual
930 threads, so make sure the control file descriptor is open. */
932 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
935 runflags
= PRCFAULT
; /* Always clear current fault. */
940 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals. */
941 proc_set_current_signal (pi
, signo
);
947 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
952 /* Register to trace signals in the process or LWP. Returns non-zero
953 for success, zero for failure. */
956 proc_set_traced_signals (procinfo
*pi
, sigset_t
*sigset
)
960 /* We should never have to apply this operation to any procinfo
961 except the one for the main process. If that ever changes for
962 any reason, then take out the following clause and replace it
963 with one that makes sure the ctl_fd is open. */
966 pi
= find_procinfo_or_die (pi
->pid
, 0);
970 /* Use char array to avoid alignment issues. */
971 char sigset
[sizeof (sigset_t
)];
975 memcpy (&arg
.sigset
, sigset
, sizeof (sigset_t
));
977 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
979 /* The above operation renders the procinfo's cached pstatus obsolete. */
980 pi
->status_valid
= 0;
983 warning (_("procfs: set_traced_signals failed"));
987 /* Register to trace hardware faults in the process or LWP. Returns
988 non-zero for success, zero for failure. */
991 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
995 /* We should never have to apply this operation to any procinfo
996 except the one for the main process. If that ever changes for
997 any reason, then take out the following clause and replace it
998 with one that makes sure the ctl_fd is open. */
1001 pi
= find_procinfo_or_die (pi
->pid
, 0);
1005 /* Use char array to avoid alignment issues. */
1006 char fltset
[sizeof (fltset_t
)];
1010 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1012 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1014 /* The above operation renders the procinfo's cached pstatus obsolete. */
1015 pi
->status_valid
= 0;
1020 /* Register to trace entry to system calls in the process or LWP.
1021 Returns non-zero for success, zero for failure. */
1024 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1028 /* We should never have to apply this operation to any procinfo
1029 except the one for the main process. If that ever changes for
1030 any reason, then take out the following clause and replace it
1031 with one that makes sure the ctl_fd is open. */
1034 pi
= find_procinfo_or_die (pi
->pid
, 0);
1038 /* Use char array to avoid alignment issues. */
1039 char sysset
[sizeof (sysset_t
)];
1043 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1045 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1047 /* The above operation renders the procinfo's cached pstatus
1049 pi
->status_valid
= 0;
1054 /* Register to trace exit from system calls in the process or LWP.
1055 Returns non-zero for success, zero for failure. */
1058 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1062 /* We should never have to apply this operation to any procinfo
1063 except the one for the main process. If that ever changes for
1064 any reason, then take out the following clause and replace it
1065 with one that makes sure the ctl_fd is open. */
1068 pi
= find_procinfo_or_die (pi
->pid
, 0);
1070 struct gdb_proc_ctl_pcsexit
{
1072 /* Use char array to avoid alignment issues. */
1073 char sysset
[sizeof (sysset_t
)];
1077 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1079 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1081 /* The above operation renders the procinfo's cached pstatus
1083 pi
->status_valid
= 0;
1088 /* Specify the set of blocked / held signals in the process or LWP.
1089 Returns non-zero for success, zero for failure. */
1092 proc_set_held_signals (procinfo
*pi
, sigset_t
*sighold
)
1096 /* We should never have to apply this operation to any procinfo
1097 except the one for the main process. If that ever changes for
1098 any reason, then take out the following clause and replace it
1099 with one that makes sure the ctl_fd is open. */
1102 pi
= find_procinfo_or_die (pi
->pid
, 0);
1106 /* Use char array to avoid alignment issues. */
1107 char hold
[sizeof (sigset_t
)];
1111 memcpy (&arg
.hold
, sighold
, sizeof (sigset_t
));
1112 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1114 /* The above operation renders the procinfo's cached pstatus
1116 pi
->status_valid
= 0;
1121 /* Returns the set of signals that are held / blocked. Will also copy
1122 the sigset if SAVE is non-zero. */
1125 proc_get_held_signals (procinfo
*pi
, sigset_t
*save
)
1127 sigset_t
*ret
= NULL
;
1129 /* We should never have to apply this operation to any procinfo
1130 except the one for the main process. If that ever changes for
1131 any reason, then take out the following clause and replace it
1132 with one that makes sure the ctl_fd is open. */
1135 pi
= find_procinfo_or_die (pi
->pid
, 0);
1137 if (!pi
->status_valid
)
1138 if (!proc_get_status (pi
))
1141 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1143 memcpy (save
, ret
, sizeof (sigset_t
));
1148 /* Returns the set of signals that are traced / debugged. Will also
1149 copy the sigset if SAVE is non-zero. */
1152 proc_get_traced_signals (procinfo
*pi
, sigset_t
*save
)
1154 sigset_t
*ret
= NULL
;
1156 /* We should never have to apply this operation to any procinfo
1157 except the one for the main process. If that ever changes for
1158 any reason, then take out the following clause and replace it
1159 with one that makes sure the ctl_fd is open. */
1162 pi
= find_procinfo_or_die (pi
->pid
, 0);
1164 if (!pi
->status_valid
)
1165 if (!proc_get_status (pi
))
1168 ret
= &pi
->prstatus
.pr_sigtrace
;
1170 memcpy (save
, ret
, sizeof (sigset_t
));
1175 /* Returns the set of hardware faults that are traced /debugged. Will
1176 also copy the faultset if SAVE is non-zero. */
1179 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1181 fltset_t
*ret
= NULL
;
1183 /* We should never have to apply this operation to any procinfo
1184 except the one for the main process. If that ever changes for
1185 any reason, then take out the following clause and replace it
1186 with one that makes sure the ctl_fd is open. */
1189 pi
= find_procinfo_or_die (pi
->pid
, 0);
1191 if (!pi
->status_valid
)
1192 if (!proc_get_status (pi
))
1195 ret
= &pi
->prstatus
.pr_flttrace
;
1197 memcpy (save
, ret
, sizeof (fltset_t
));
1202 /* Returns the set of syscalls that are traced /debugged on entry.
1203 Will also copy the syscall set if SAVE is non-zero. */
1206 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1208 sysset_t
*ret
= NULL
;
1210 /* We should never have to apply this operation to any procinfo
1211 except the one for the main process. If that ever changes for
1212 any reason, then take out the following clause and replace it
1213 with one that makes sure the ctl_fd is open. */
1216 pi
= find_procinfo_or_die (pi
->pid
, 0);
1218 if (!pi
->status_valid
)
1219 if (!proc_get_status (pi
))
1222 ret
= &pi
->prstatus
.pr_sysentry
;
1224 memcpy (save
, ret
, sizeof (sysset_t
));
1229 /* Returns the set of syscalls that are traced /debugged on exit.
1230 Will also copy the syscall set if SAVE is non-zero. */
1233 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1235 sysset_t
*ret
= NULL
;
1237 /* We should never have to apply this operation to any procinfo
1238 except the one for the main process. If that ever changes for
1239 any reason, then take out the following clause and replace it
1240 with one that makes sure the ctl_fd is open. */
1243 pi
= find_procinfo_or_die (pi
->pid
, 0);
1245 if (!pi
->status_valid
)
1246 if (!proc_get_status (pi
))
1249 ret
= &pi
->prstatus
.pr_sysexit
;
1251 memcpy (save
, ret
, sizeof (sysset_t
));
1256 /* The current fault (if any) is cleared; the associated signal will
1257 not be sent to the process or LWP when it resumes. Returns
1258 non-zero for success, zero for failure. */
1261 proc_clear_current_fault (procinfo
*pi
)
1265 /* We should never have to apply this operation to any procinfo
1266 except the one for the main process. If that ever changes for
1267 any reason, then take out the following clause and replace it
1268 with one that makes sure the ctl_fd is open. */
1271 pi
= find_procinfo_or_die (pi
->pid
, 0);
1273 procfs_ctl_t cmd
= PCCFAULT
;
1275 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1280 /* Set the "current signal" that will be delivered next to the
1281 process. NOTE: semantics are different from those of KILL. This
1282 signal will be delivered to the process or LWP immediately when it
1283 is resumed (even if the signal is held/blocked); it will NOT
1284 immediately cause another event of interest, and will NOT first
1285 trap back to the debugger. Returns non-zero for success, zero for
1289 proc_set_current_signal (procinfo
*pi
, int signo
)
1294 /* Use char array to avoid alignment issues. */
1295 char sinfo
[sizeof (siginfo_t
)];
1298 process_stratum_target
*wait_target
;
1300 struct target_waitstatus wait_status
;
1302 /* We should never have to apply this operation to any procinfo
1303 except the one for the main process. If that ever changes for
1304 any reason, then take out the following clause and replace it
1305 with one that makes sure the ctl_fd is open. */
1308 pi
= find_procinfo_or_die (pi
->pid
, 0);
1310 /* The pointer is just a type alias. */
1311 get_last_target_status (&wait_target
, &wait_ptid
, &wait_status
);
1312 if (wait_target
== &the_procfs_target
1313 && wait_ptid
== inferior_ptid
1314 && wait_status
.kind () == TARGET_WAITKIND_STOPPED
1315 && wait_status
.sig () == gdb_signal_from_host (signo
)
1316 && proc_get_status (pi
)
1317 && pi
->prstatus
.pr_lwp
.pr_info
.si_signo
== signo
1319 /* Use the siginfo associated with the signal being
1321 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_lwp
.pr_info
, sizeof (siginfo_t
));
1324 mysinfo
.si_signo
= signo
;
1325 mysinfo
.si_code
= 0;
1326 mysinfo
.si_pid
= getpid (); /* ?why? */
1327 mysinfo
.si_uid
= getuid (); /* ?why? */
1328 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1332 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1337 /* The current signal (if any) is cleared, and is not sent to the
1338 process or LWP when it resumes. Returns non-zero for success, zero
1342 proc_clear_current_signal (procinfo
*pi
)
1346 /* We should never have to apply this operation to any procinfo
1347 except the one for the main process. If that ever changes for
1348 any reason, then take out the following clause and replace it
1349 with one that makes sure the ctl_fd is open. */
1352 pi
= find_procinfo_or_die (pi
->pid
, 0);
1356 /* Use char array to avoid alignment issues. */
1357 char sinfo
[sizeof (siginfo_t
)];
1362 /* The pointer is just a type alias. */
1363 mysinfo
.si_signo
= 0;
1364 mysinfo
.si_code
= 0;
1365 mysinfo
.si_errno
= 0;
1366 mysinfo
.si_pid
= getpid (); /* ?why? */
1367 mysinfo
.si_uid
= getuid (); /* ?why? */
1368 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1370 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1375 /* Return the general-purpose registers for the process or LWP
1376 corresponding to PI. Upon failure, return NULL. */
1378 static gdb_gregset_t
*
1379 proc_get_gregs (procinfo
*pi
)
1381 if (!pi
->status_valid
|| !pi
->gregs_valid
)
1382 if (!proc_get_status (pi
))
1385 return &pi
->prstatus
.pr_lwp
.pr_reg
;
1388 /* Return the general-purpose registers for the process or LWP
1389 corresponding to PI. Upon failure, return NULL. */
1391 static gdb_fpregset_t
*
1392 proc_get_fpregs (procinfo
*pi
)
1394 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
1395 if (!proc_get_status (pi
))
1398 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
1401 /* Write the general-purpose registers back to the process or LWP
1402 corresponding to PI. Return non-zero for success, zero for
1406 proc_set_gregs (procinfo
*pi
)
1408 gdb_gregset_t
*gregs
;
1411 gregs
= proc_get_gregs (pi
);
1413 return 0; /* proc_get_regs has already warned. */
1415 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1421 /* Use char array to avoid alignment issues. */
1422 char gregs
[sizeof (gdb_gregset_t
)];
1426 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
1427 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1430 /* Policy: writing the registers invalidates our cache. */
1431 pi
->gregs_valid
= 0;
1435 /* Write the floating-pointer registers back to the process or LWP
1436 corresponding to PI. Return non-zero for success, zero for
1440 proc_set_fpregs (procinfo
*pi
)
1442 gdb_fpregset_t
*fpregs
;
1445 fpregs
= proc_get_fpregs (pi
);
1447 return 0; /* proc_get_fpregs has already warned. */
1449 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1455 /* Use char array to avoid alignment issues. */
1456 char fpregs
[sizeof (gdb_fpregset_t
)];
1460 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
1461 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1464 /* Policy: writing the registers invalidates our cache. */
1465 pi
->fpregs_valid
= 0;
1469 /* Send a signal to the proc or lwp with the semantics of "kill()".
1470 Returns non-zero for success, zero for failure. */
1473 proc_kill (procinfo
*pi
, int signo
)
1477 /* We might conceivably apply this operation to an LWP, and the
1478 LWP's ctl file descriptor might not be open. */
1480 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1484 procfs_ctl_t cmd
[2];
1488 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1494 /* Find the pid of the process that started this one. Returns the
1495 parent process pid, or zero. */
1498 proc_parent_pid (procinfo
*pi
)
1500 /* We should never have to apply this operation to any procinfo
1501 except the one for the main process. If that ever changes for
1502 any reason, then take out the following clause and replace it
1503 with one that makes sure the ctl_fd is open. */
1506 pi
= find_procinfo_or_die (pi
->pid
, 0);
1508 if (!pi
->status_valid
)
1509 if (!proc_get_status (pi
))
1512 return pi
->prstatus
.pr_ppid
;
1515 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
1516 (a.k.a void pointer)! */
1519 procfs_address_to_host_pointer (CORE_ADDR addr
)
1521 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
1524 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (ptr_type
));
1525 gdbarch_address_to_pointer (target_gdbarch (), ptr_type
,
1526 (gdb_byte
*) &ptr
, addr
);
1531 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
1535 char watch
[sizeof (prwatch_t
)];
1539 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
1540 convert a target address into something that can be stored in a
1541 native data structure. */
1542 pwatch
.pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
1543 pwatch
.pr_size
= len
;
1544 pwatch
.pr_wflags
= wflags
;
1546 memcpy (arg
.watch
, &pwatch
, sizeof (prwatch_t
));
1547 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
1550 /* =============== END, non-thread part of /proc "MODULE" =============== */
1552 /* =================== Thread "MODULE" =================== */
1554 /* Returns the number of threads for the process. */
1557 proc_get_nthreads (procinfo
*pi
)
1559 if (!pi
->status_valid
)
1560 if (!proc_get_status (pi
))
1563 /* Only works for the process procinfo, because the LWP procinfos do not
1564 get prstatus filled in. */
1565 if (pi
->tid
!= 0) /* Find the parent process procinfo. */
1566 pi
= find_procinfo_or_die (pi
->pid
, 0);
1567 return pi
->prstatus
.pr_nlwp
;
1570 /* Return the ID of the thread that had an event of interest.
1571 (ie. the one that hit a breakpoint or other traced event). All
1572 other things being equal, this should be the ID of a thread that is
1573 currently executing. */
1576 proc_get_current_thread (procinfo
*pi
)
1578 /* Note: this should be applied to the root procinfo for the
1579 process, not to the procinfo for an LWP. If applied to the
1580 procinfo for an LWP, it will simply return that LWP's ID. In
1581 that case, find the parent process procinfo. */
1584 pi
= find_procinfo_or_die (pi
->pid
, 0);
1586 if (!pi
->status_valid
)
1587 if (!proc_get_status (pi
))
1590 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
1593 /* Discover the IDs of all the threads within the process, and create
1594 a procinfo for each of them (chained to the parent). Returns
1595 non-zero for success, zero for failure. */
1598 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
1600 if (thread
&& parent
) /* sanity */
1602 thread
->status_valid
= 0;
1603 if (!proc_get_status (thread
))
1604 destroy_one_procinfo (&parent
->thread_list
, thread
);
1606 return 0; /* keep iterating */
1610 proc_update_threads (procinfo
*pi
)
1612 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
1613 struct dirent
*direntry
;
1618 /* We should never have to apply this operation to any procinfo
1619 except the one for the main process. If that ever changes for
1620 any reason, then take out the following clause and replace it
1621 with one that makes sure the ctl_fd is open. */
1624 pi
= find_procinfo_or_die (pi
->pid
, 0);
1626 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
1628 /* Note: this brute-force method was originally devised for Unixware
1629 (support removed since), and will also work on Solaris 2.6 and
1630 2.7. The original comment mentioned the existence of a much
1631 simpler and more elegant way to do this on Solaris, but didn't
1632 point out what that was. */
1634 strcpy (pathname
, pi
->pathname
);
1635 strcat (pathname
, "/lwp");
1636 dirp
.reset (opendir (pathname
));
1638 proc_error (pi
, "update_threads, opendir", __LINE__
);
1640 while ((direntry
= readdir (dirp
.get ())) != NULL
)
1641 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
1643 lwpid
= atoi (&direntry
->d_name
[0]);
1644 thread
= create_procinfo (pi
->pid
, lwpid
);
1646 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
1648 pi
->threads_valid
= 1;
1652 /* Given a pointer to a function, call that function once for each lwp
1653 in the procinfo list, until the function returns non-zero, in which
1654 event return the value returned by the function.
1656 Note: this function does NOT call update_threads. If you want to
1657 discover new threads first, you must call that function explicitly.
1658 This function just makes a quick pass over the currently-known
1661 PI is the parent process procinfo. FUNC is the per-thread
1662 function. PTR is an opaque parameter for function. Returns the
1663 first non-zero return value from the callee, or zero. */
1666 proc_iterate_over_threads (procinfo
*pi
,
1667 int (*func
) (procinfo
*, procinfo
*, void *),
1670 procinfo
*thread
, *next
;
1673 /* We should never have to apply this operation to any procinfo
1674 except the one for the main process. If that ever changes for
1675 any reason, then take out the following clause and replace it
1676 with one that makes sure the ctl_fd is open. */
1679 pi
= find_procinfo_or_die (pi
->pid
, 0);
1681 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
1683 next
= thread
->next
; /* In case thread is destroyed. */
1684 retval
= (*func
) (pi
, thread
, ptr
);
1692 /* =================== END, Thread "MODULE" =================== */
1694 /* =================== END, /proc "MODULE" =================== */
1696 /* =================== GDB "MODULE" =================== */
1698 /* Here are all of the gdb target vector functions and their
1701 static void do_attach (ptid_t ptid
);
1702 static void do_detach ();
1703 static void proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
,
1704 int entry_or_exit
, int mode
, int from_tty
);
1706 /* Sets up the inferior to be debugged. Registers to trace signals,
1707 hardware faults, and syscalls. Note: does not set RLC flag: caller
1708 may want to customize that. Returns zero for success (note!
1709 unlike most functions in this module); on failure, returns the LINE
1710 NUMBER where it failed! */
1713 procfs_debug_inferior (procinfo
*pi
)
1715 fltset_t traced_faults
;
1716 sigset_t traced_signals
;
1717 sysset_t
*traced_syscall_entries
;
1718 sysset_t
*traced_syscall_exits
;
1721 /* Register to trace hardware faults in the child. */
1722 prfillset (&traced_faults
); /* trace all faults... */
1723 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
1724 if (!proc_set_traced_faults (pi
, &traced_faults
))
1727 /* Initially, register to trace all signals in the child. */
1728 prfillset (&traced_signals
);
1729 if (!proc_set_traced_signals (pi
, &traced_signals
))
1733 /* Register to trace the 'exit' system call (on entry). */
1734 traced_syscall_entries
= XNEW (sysset_t
);
1735 premptyset (traced_syscall_entries
);
1736 praddset (traced_syscall_entries
, SYS_exit
);
1737 praddset (traced_syscall_entries
, SYS_lwp_exit
);
1739 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
1740 xfree (traced_syscall_entries
);
1744 /* Method for tracing exec syscalls. */
1745 traced_syscall_exits
= XNEW (sysset_t
);
1746 premptyset (traced_syscall_exits
);
1747 praddset (traced_syscall_exits
, SYS_execve
);
1748 praddset (traced_syscall_exits
, SYS_lwp_create
);
1749 praddset (traced_syscall_exits
, SYS_lwp_exit
);
1751 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
1752 xfree (traced_syscall_exits
);
1760 procfs_target::attach (const char *args
, int from_tty
)
1764 pid
= parse_pid_to_attach (args
);
1766 if (pid
== getpid ())
1767 error (_("Attaching GDB to itself is not a good idea..."));
1769 /* Push the target if needed, ensure it gets un-pushed it if attach fails. */
1770 inferior
*inf
= current_inferior ();
1771 target_unpush_up unpusher
;
1772 if (!inf
->target_is_pushed (this))
1774 inf
->push_target (this);
1775 unpusher
.reset (this);
1778 target_announce_attach (from_tty
, pid
);
1780 do_attach (ptid_t (pid
));
1782 /* Everything went fine, keep the target pushed. */
1783 unpusher
.release ();
1787 procfs_target::detach (inferior
*inf
, int from_tty
)
1789 target_announce_detach (from_tty
);
1793 switch_to_no_thread ();
1794 detach_inferior (inf
);
1795 maybe_unpush_target ();
1799 do_attach (ptid_t ptid
)
1802 struct inferior
*inf
;
1806 pi
= create_procinfo (ptid
.pid (), 0);
1808 perror (_("procfs: out of memory in 'attach'"));
1810 if (!open_procinfo_files (pi
, FD_CTL
))
1812 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
1813 xsnprintf (errmsg
, sizeof (errmsg
),
1814 "do_attach: couldn't open /proc file for process %d",
1816 dead_procinfo (pi
, errmsg
, NOKILL
);
1819 /* Stop the process (if it isn't already stopped). */
1820 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
1822 pi
->was_stopped
= 1;
1823 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
1827 pi
->was_stopped
= 0;
1828 /* Set the process to run again when we close it. */
1829 if (!proc_set_run_on_last_close (pi
))
1830 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
1832 /* Now stop the process. */
1833 if (!proc_stop_process (pi
))
1834 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
1835 pi
->ignore_next_sigstop
= 1;
1837 /* Save some of the /proc state to be restored if we detach. */
1838 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
1839 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
1840 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
1841 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
1842 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
1843 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
1845 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
1846 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
1848 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
1849 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
1851 fail
= procfs_debug_inferior (pi
);
1853 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
1855 inf
= current_inferior ();
1856 inferior_appeared (inf
, pi
->pid
);
1857 /* Let GDB know that the inferior was attached. */
1858 inf
->attach_flag
= 1;
1860 /* Create a procinfo for the current lwp. */
1861 lwpid
= proc_get_current_thread (pi
);
1862 create_procinfo (pi
->pid
, lwpid
);
1864 /* Add it to gdb's thread list. */
1865 ptid
= ptid_t (pi
->pid
, lwpid
, 0);
1866 thread_info
*thr
= add_thread (&the_procfs_target
, ptid
);
1867 switch_to_thread (thr
);
1875 /* Find procinfo for the main process. */
1876 pi
= find_procinfo_or_die (inferior_ptid
.pid (),
1877 0); /* FIXME: threads */
1879 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
1880 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
1882 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
1883 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
1885 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
1886 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
1888 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
1889 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
1891 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
1892 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
1894 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
1895 if (!(pi
->was_stopped
)
1896 || query (_("Was stopped when attached, make it runnable again? ")))
1898 /* Clear any pending signal. */
1899 if (!proc_clear_current_fault (pi
))
1900 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
1902 if (!proc_clear_current_signal (pi
))
1903 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
1905 if (!proc_set_run_on_last_close (pi
))
1906 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
1909 destroy_procinfo (pi
);
1912 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
1915 NOTE: Since the /proc interface cannot give us individual
1916 registers, we pay no attention to REGNUM, and just fetch them all.
1917 This results in the possibility that we will do unnecessarily many
1918 fetches, since we may be called repeatedly for individual
1919 registers. So we cache the results, and mark the cache invalid
1920 when the process is resumed. */
1923 procfs_target::fetch_registers (struct regcache
*regcache
, int regnum
)
1925 gdb_gregset_t
*gregs
;
1927 ptid_t ptid
= regcache
->ptid ();
1928 int pid
= ptid
.pid ();
1929 int tid
= ptid
.lwp ();
1930 struct gdbarch
*gdbarch
= regcache
->arch ();
1932 pi
= find_procinfo_or_die (pid
, tid
);
1935 error (_("procfs: fetch_registers failed to find procinfo for %s"),
1936 target_pid_to_str (ptid
).c_str ());
1938 gregs
= proc_get_gregs (pi
);
1940 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
1942 supply_gregset (regcache
, (const gdb_gregset_t
*) gregs
);
1944 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
1946 gdb_fpregset_t
*fpregs
;
1948 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
1949 || regnum
== gdbarch_pc_regnum (gdbarch
)
1950 || regnum
== gdbarch_sp_regnum (gdbarch
))
1951 return; /* Not a floating point register. */
1953 fpregs
= proc_get_fpregs (pi
);
1955 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
1957 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregs
);
1961 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
1962 this for all registers.
1964 NOTE: Since the /proc interface will not read individual registers,
1965 we will cache these requests until the process is resumed, and only
1966 then write them back to the inferior process.
1968 FIXME: is that a really bad idea? Have to think about cases where
1969 writing one register might affect the value of others, etc. */
1972 procfs_target::store_registers (struct regcache
*regcache
, int regnum
)
1974 gdb_gregset_t
*gregs
;
1976 ptid_t ptid
= regcache
->ptid ();
1977 int pid
= ptid
.pid ();
1978 int tid
= ptid
.lwp ();
1979 struct gdbarch
*gdbarch
= regcache
->arch ();
1981 pi
= find_procinfo_or_die (pid
, tid
);
1984 error (_("procfs: store_registers: failed to find procinfo for %s"),
1985 target_pid_to_str (ptid
).c_str ());
1987 gregs
= proc_get_gregs (pi
);
1989 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
1991 fill_gregset (regcache
, gregs
, regnum
);
1992 if (!proc_set_gregs (pi
))
1993 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
1995 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
1997 gdb_fpregset_t
*fpregs
;
1999 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
2000 || regnum
== gdbarch_pc_regnum (gdbarch
)
2001 || regnum
== gdbarch_sp_regnum (gdbarch
))
2002 return; /* Not a floating point register. */
2004 fpregs
= proc_get_fpregs (pi
);
2006 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
2008 fill_fpregset (regcache
, fpregs
, regnum
);
2009 if (!proc_set_fpregs (pi
))
2010 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
2014 /* Retrieve the next stop event from the child process. If child has
2015 not stopped yet, wait for it to stop. Translate /proc eventcodes
2016 (or possibly wait eventcodes) into gdb internal event codes.
2017 Returns the id of process (and possibly thread) that incurred the
2018 event. Event codes are returned through a pointer parameter. */
2021 procfs_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
2022 target_wait_flags options
)
2024 /* First cut: loosely based on original version 2.1. */
2028 ptid_t retval
, temp_ptid
;
2029 int why
, what
, flags
;
2036 retval
= ptid_t (-1);
2038 /* Find procinfo for main process. */
2040 /* procfs_target currently only supports one inferior. */
2041 inferior
*inf
= current_inferior ();
2043 pi
= find_procinfo_or_die (inf
->pid
, 0);
2046 /* We must assume that the status is stale now... */
2047 pi
->status_valid
= 0;
2048 pi
->gregs_valid
= 0;
2049 pi
->fpregs_valid
= 0;
2051 #if 0 /* just try this out... */
2052 flags
= proc_flags (pi
);
2053 why
= proc_why (pi
);
2054 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
2055 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
2057 /* If child is not stopped, wait for it to stop. */
2058 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
2059 && !proc_wait_for_stop (pi
))
2061 /* wait_for_stop failed: has the child terminated? */
2062 if (errno
== ENOENT
)
2066 /* /proc file not found; presumably child has terminated. */
2067 wait_retval
= ::wait (&wstat
); /* "wait" for the child's exit. */
2070 if (wait_retval
!= inf
->pid
)
2071 error (_("procfs: couldn't stop "
2072 "process %d: wait returned %d."),
2073 inf
->pid
, wait_retval
);
2074 /* FIXME: might I not just use waitpid?
2075 Or try find_procinfo to see if I know about this child? */
2076 retval
= ptid_t (wait_retval
);
2078 else if (errno
== EINTR
)
2082 /* Unknown error from wait_for_stop. */
2083 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
2088 /* This long block is reached if either:
2089 a) the child was already stopped, or
2090 b) we successfully waited for the child with wait_for_stop.
2091 This block will analyze the /proc status, and translate it
2092 into a waitstatus for GDB.
2094 If we actually had to call wait because the /proc file
2095 is gone (child terminated), then we skip this block,
2096 because we already have a waitstatus. */
2098 flags
= proc_flags (pi
);
2099 why
= proc_why (pi
);
2100 what
= proc_what (pi
);
2102 if (flags
& (PR_STOPPED
| PR_ISTOP
))
2104 /* If it's running async (for single_thread control),
2105 set it back to normal again. */
2106 if (flags
& PR_ASYNC
)
2107 if (!proc_unset_async (pi
))
2108 proc_error (pi
, "target_wait, unset_async", __LINE__
);
2111 proc_prettyprint_why (why
, what
, 1);
2113 /* The 'pid' we will return to GDB is composed of
2114 the process ID plus the lwp ID. */
2115 retval
= ptid_t (pi
->pid
, proc_get_current_thread (pi
), 0);
2119 wstat
= (what
<< 8) | 0177;
2122 if (what
== SYS_lwp_exit
)
2124 if (print_thread_events
)
2125 printf_unfiltered (_("[%s exited]\n"),
2126 target_pid_to_str (retval
).c_str ());
2127 delete_thread (find_thread_ptid (this, retval
));
2128 target_continue_no_signal (ptid
);
2131 else if (what
== SYS_exit
)
2133 /* Handle SYS_exit call only. */
2134 /* Stopped at entry to SYS_exit.
2135 Make it runnable, resume it, then use
2136 the wait system call to get its exit code.
2137 Proc_run_process always clears the current
2139 Then return its exit status. */
2140 pi
->status_valid
= 0;
2142 /* FIXME: what we should do is return
2143 TARGET_WAITKIND_SPURIOUS. */
2144 if (!proc_run_process (pi
, 0, 0))
2145 proc_error (pi
, "target_wait, run_process", __LINE__
);
2147 if (inf
->attach_flag
)
2149 /* Don't call wait: simulate waiting for exit,
2150 return a "success" exit code. Bogus: what if
2151 it returns something else? */
2153 retval
= ptid_t (inf
->pid
); /* ? ? ? */
2157 int temp
= ::wait (&wstat
);
2159 /* FIXME: shouldn't I make sure I get the right
2160 event from the right process? If (for
2161 instance) I have killed an earlier inferior
2162 process but failed to clean up after it
2163 somehow, I could get its termination event
2166 /* If wait returns -1, that's what we return
2169 retval
= ptid_t (temp
);
2174 printf_filtered (_("procfs: trapped on entry to "));
2175 proc_prettyprint_syscall (proc_what (pi
), 0);
2176 printf_filtered ("\n");
2178 long i
, nsysargs
, *sysargs
;
2180 nsysargs
= proc_nsysarg (pi
);
2181 sysargs
= proc_sysargs (pi
);
2183 if (nsysargs
> 0 && sysargs
!= NULL
)
2185 printf_filtered (_("%ld syscall arguments:\n"),
2187 for (i
= 0; i
< nsysargs
; i
++)
2188 printf_filtered ("#%ld: 0x%08lx\n",
2192 /* How to keep going without returning to wfi: */
2193 target_continue_no_signal (ptid
);
2198 if (what
== SYS_execve
)
2200 /* Hopefully this is our own "fork-child" execing
2201 the real child. Hoax this event into a trap, and
2202 GDB will see the child about to execute its start
2204 wstat
= (SIGTRAP
<< 8) | 0177;
2206 else if (what
== SYS_lwp_create
)
2208 /* This syscall is somewhat like fork/exec. We
2209 will get the event twice: once for the parent
2210 LWP, and once for the child. We should already
2211 know about the parent LWP, but the child will
2212 be new to us. So, whenever we get this event,
2213 if it represents a new thread, simply add the
2214 thread to the list. */
2216 /* If not in procinfo list, add it. */
2217 temp_tid
= proc_get_current_thread (pi
);
2218 if (!find_procinfo (pi
->pid
, temp_tid
))
2219 create_procinfo (pi
->pid
, temp_tid
);
2221 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2222 /* If not in GDB's thread list, add it. */
2223 if (!in_thread_list (this, temp_ptid
))
2224 add_thread (this, temp_ptid
);
2226 target_continue_no_signal (ptid
);
2229 else if (what
== SYS_lwp_exit
)
2231 if (print_thread_events
)
2232 printf_unfiltered (_("[%s exited]\n"),
2233 target_pid_to_str (retval
).c_str ());
2234 delete_thread (find_thread_ptid (this, retval
));
2235 status
->set_spurious ();
2240 printf_filtered (_("procfs: trapped on exit from "));
2241 proc_prettyprint_syscall (proc_what (pi
), 0);
2242 printf_filtered ("\n");
2244 long i
, nsysargs
, *sysargs
;
2246 nsysargs
= proc_nsysarg (pi
);
2247 sysargs
= proc_sysargs (pi
);
2249 if (nsysargs
> 0 && sysargs
!= NULL
)
2251 printf_filtered (_("%ld syscall arguments:\n"),
2253 for (i
= 0; i
< nsysargs
; i
++)
2254 printf_filtered ("#%ld: 0x%08lx\n",
2258 target_continue_no_signal (ptid
);
2264 wstat
= (SIGSTOP
<< 8) | 0177;
2269 printf_filtered (_("Retry #%d:\n"), retry
);
2270 pi
->status_valid
= 0;
2275 /* If not in procinfo list, add it. */
2276 temp_tid
= proc_get_current_thread (pi
);
2277 if (!find_procinfo (pi
->pid
, temp_tid
))
2278 create_procinfo (pi
->pid
, temp_tid
);
2280 /* If not in GDB's thread list, add it. */
2281 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2282 if (!in_thread_list (this, temp_ptid
))
2283 add_thread (this, temp_ptid
);
2285 status
->set_stopped (GDB_SIGNAL_0
);
2290 wstat
= (what
<< 8) | 0177;
2294 int signo
= pi
->prstatus
.pr_lwp
.pr_info
.si_signo
;
2296 wstat
= (signo
<< 8) | 0177;
2299 default: /* switch (why) unmatched */
2300 printf_filtered ("procfs:%d -- ", __LINE__
);
2301 printf_filtered (_("child stopped for unknown reason:\n"));
2302 proc_prettyprint_why (why
, what
, 1);
2303 error (_("... giving up..."));
2306 /* Got this far without error: If retval isn't in the
2307 threads database, add it. */
2308 if (retval
.pid () > 0
2309 && !in_thread_list (this, retval
))
2311 /* We have a new thread. We need to add it both to
2312 GDB's list and to our own. If we don't create a
2313 procinfo, resume may be unhappy later. */
2314 add_thread (this, retval
);
2315 if (find_procinfo (retval
.pid (),
2316 retval
.lwp ()) == NULL
)
2317 create_procinfo (retval
.pid (),
2321 else /* Flags do not indicate STOPPED. */
2323 /* surely this can't happen... */
2324 printf_filtered ("procfs:%d -- process not stopped.\n",
2326 proc_prettyprint_flags (flags
, 1);
2327 error (_("procfs: ...giving up..."));
2332 *status
= host_status_to_waitstatus (wstat
);
2338 /* Perform a partial transfer to/from the specified object. For
2339 memory transfers, fall back to the old memory xfer functions. */
2341 enum target_xfer_status
2342 procfs_target::xfer_partial (enum target_object object
,
2343 const char *annex
, gdb_byte
*readbuf
,
2344 const gdb_byte
*writebuf
, ULONGEST offset
,
2345 ULONGEST len
, ULONGEST
*xfered_len
)
2349 case TARGET_OBJECT_MEMORY
:
2350 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
2352 case TARGET_OBJECT_AUXV
:
2353 return memory_xfer_auxv (this, object
, annex
, readbuf
, writebuf
,
2354 offset
, len
, xfered_len
);
2357 return this->beneath ()->xfer_partial (object
, annex
,
2358 readbuf
, writebuf
, offset
, len
,
2363 /* Helper for procfs_xfer_partial that handles memory transfers.
2364 Arguments are like target_xfer_partial. */
2366 static enum target_xfer_status
2367 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
2368 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
2373 /* Find procinfo for main process. */
2374 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2375 if (pi
->as_fd
== 0 && open_procinfo_files (pi
, FD_AS
) == 0)
2377 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
2378 return TARGET_XFER_E_IO
;
2381 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
2382 return TARGET_XFER_E_IO
;
2384 if (writebuf
!= NULL
)
2386 PROCFS_NOTE ("write memory:\n");
2387 nbytes
= write (pi
->as_fd
, writebuf
, len
);
2391 PROCFS_NOTE ("read memory:\n");
2392 nbytes
= read (pi
->as_fd
, readbuf
, len
);
2395 return TARGET_XFER_E_IO
;
2396 *xfered_len
= nbytes
;
2397 return TARGET_XFER_OK
;
2400 /* Called by target_resume before making child runnable. Mark cached
2401 registers and status's invalid. If there are "dirty" caches that
2402 need to be written back to the child process, do that.
2404 File descriptors are also cached. As they are a limited resource,
2405 we cannot hold onto them indefinitely. However, as they are
2406 expensive to open, we don't want to throw them away
2407 indiscriminately either. As a compromise, we will keep the file
2408 descriptors for the parent process, but discard any file
2409 descriptors we may have accumulated for the threads.
2411 As this function is called by iterate_over_threads, it always
2412 returns zero (so that iterate_over_threads will keep
2416 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
2418 /* About to run the child; invalidate caches and do any other
2423 /* The presence of a parent indicates that this is an LWP.
2424 Close any file descriptors that it might have open.
2425 We don't do this to the master (parent) procinfo. */
2427 close_procinfo_files (pi
);
2429 pi
->gregs_valid
= 0;
2430 pi
->fpregs_valid
= 0;
2431 pi
->status_valid
= 0;
2432 pi
->threads_valid
= 0;
2437 /* Make the child process runnable. Normally we will then call
2438 procfs_wait and wait for it to stop again (unless gdb is async).
2440 If STEP is true, then arrange for the child to stop again after
2441 executing a single instruction. If SIGNO is zero, then cancel any
2442 pending signal; if non-zero, then arrange for the indicated signal
2443 to be delivered to the child when it runs. If PID is -1, then
2444 allow any child thread to run; if non-zero, then allow only the
2445 indicated thread to run. (not implemented yet). */
2448 procfs_target::resume (ptid_t ptid
, int step
, enum gdb_signal signo
)
2450 procinfo
*pi
, *thread
;
2453 /* FIXME: Check/reword. */
2455 /* prrun.prflags |= PRCFAULT; clear current fault.
2456 PRCFAULT may be replaced by a PCCFAULT call (proc_clear_current_fault)
2457 This basically leaves PRSTEP and PRCSIG.
2458 PRCSIG is like PCSSIG (proc_clear_current_signal).
2459 So basically PR_STEP is the sole argument that must be passed
2460 to proc_run_process. */
2462 /* Find procinfo for main process. */
2463 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2465 /* First cut: ignore pid argument. */
2468 /* Convert signal to host numbering. */
2469 if (signo
== 0 || (signo
== GDB_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
2472 native_signo
= gdb_signal_to_host (signo
);
2474 pi
->ignore_next_sigstop
= 0;
2476 /* Running the process voids all cached registers and status. */
2477 /* Void the threads' caches first. */
2478 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
2479 /* Void the process procinfo's caches. */
2480 invalidate_cache (NULL
, pi
, NULL
);
2482 if (ptid
.pid () != -1)
2484 /* Resume a specific thread, presumably suppressing the
2486 thread
= find_procinfo (ptid
.pid (), ptid
.lwp ());
2489 if (thread
->tid
!= 0)
2491 /* We're to resume a specific thread, and not the
2492 others. Set the child process's PR_ASYNC flag. */
2493 if (!proc_set_async (pi
))
2494 proc_error (pi
, "target_resume, set_async", __LINE__
);
2495 pi
= thread
; /* Substitute the thread's procinfo
2501 if (!proc_run_process (pi
, step
, native_signo
))
2504 warning (_("resume: target already running. "
2505 "Pretend to resume, and hope for the best!"));
2507 proc_error (pi
, "target_resume", __LINE__
);
2511 /* Set up to trace signals in the child process. */
2514 procfs_target::pass_signals (gdb::array_view
<const unsigned char> pass_signals
)
2517 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2520 prfillset (&signals
);
2522 for (signo
= 0; signo
< NSIG
; signo
++)
2524 int target_signo
= gdb_signal_from_host (signo
);
2525 if (target_signo
< pass_signals
.size () && pass_signals
[target_signo
])
2526 prdelset (&signals
, signo
);
2529 if (!proc_set_traced_signals (pi
, &signals
))
2530 proc_error (pi
, "pass_signals", __LINE__
);
2533 /* Print status information about the child process. */
2536 procfs_target::files_info ()
2538 struct inferior
*inf
= current_inferior ();
2540 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
2541 inf
->attach_flag
? "attached": "child",
2542 target_pid_to_str (inferior_ptid
).c_str ());
2545 /* Make it die. Wait for it to die. Clean up after it. Note: this
2546 should only be applied to the real process, not to an LWP, because
2547 of the check for parent-process. If we need this to work for an
2548 LWP, it needs some more logic. */
2551 unconditionally_kill_inferior (procinfo
*pi
)
2555 parent_pid
= proc_parent_pid (pi
);
2556 if (!proc_kill (pi
, SIGKILL
))
2557 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
2558 destroy_procinfo (pi
);
2560 /* If pi is GDB's child, wait for it to die. */
2561 if (parent_pid
== getpid ())
2562 /* FIXME: should we use waitpid to make sure we get the right event?
2563 Should we check the returned event? */
2568 ret
= waitpid (pi
->pid
, &status
, 0);
2575 /* We're done debugging it, and we want it to go away. Then we want
2576 GDB to forget all about it. */
2579 procfs_target::kill ()
2581 if (inferior_ptid
!= null_ptid
) /* ? */
2583 /* Find procinfo for main process. */
2584 procinfo
*pi
= find_procinfo (inferior_ptid
.pid (), 0);
2587 unconditionally_kill_inferior (pi
);
2588 target_mourn_inferior (inferior_ptid
);
2592 /* Forget we ever debugged this thing! */
2595 procfs_target::mourn_inferior ()
2599 if (inferior_ptid
!= null_ptid
)
2601 /* Find procinfo for main process. */
2602 pi
= find_procinfo (inferior_ptid
.pid (), 0);
2604 destroy_procinfo (pi
);
2607 generic_mourn_inferior ();
2609 maybe_unpush_target ();
2612 /* When GDB forks to create a runnable inferior process, this function
2613 is called on the parent side of the fork. It's job is to do
2614 whatever is necessary to make the child ready to be debugged, and
2615 then wait for the child to synchronize. */
2618 procfs_target::procfs_init_inferior (int pid
)
2624 pi
= create_procinfo (pid
, 0);
2626 perror (_("procfs: out of memory in 'init_inferior'"));
2628 if (!open_procinfo_files (pi
, FD_CTL
))
2629 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
2633 open_procinfo_files // done
2636 procfs_notice_signals
2641 /* If not stopped yet, wait for it to stop. */
2642 if (!(proc_flags (pi
) & PR_STOPPED
) && !(proc_wait_for_stop (pi
)))
2643 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
2645 /* Save some of the /proc state to be restored if we detach. */
2646 /* FIXME: Why? In case another debugger was debugging it?
2647 We're it's parent, for Ghu's sake! */
2648 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
2649 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
2650 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
2651 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
2652 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
2653 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
2654 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
2655 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
2656 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
2657 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
2659 fail
= procfs_debug_inferior (pi
);
2661 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
2663 /* FIXME: logically, we should really be turning OFF run-on-last-close,
2664 and possibly even turning ON kill-on-last-close at this point. But
2665 I can't make that change without careful testing which I don't have
2666 time to do right now... */
2667 /* Turn on run-on-last-close flag so that the child
2668 will die if GDB goes away for some reason. */
2669 if (!proc_set_run_on_last_close (pi
))
2670 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
2672 /* We now have have access to the lwpid of the main thread/lwp. */
2673 lwpid
= proc_get_current_thread (pi
);
2675 /* Create a procinfo for the main lwp. */
2676 create_procinfo (pid
, lwpid
);
2678 /* We already have a main thread registered in the thread table at
2679 this point, but it didn't have any lwp info yet. Notify the core
2680 about it. This changes inferior_ptid as well. */
2681 thread_change_ptid (this, ptid_t (pid
), ptid_t (pid
, lwpid
, 0));
2683 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
2686 /* When GDB forks to create a new process, this function is called on
2687 the child side of the fork before GDB exec's the user program. Its
2688 job is to make the child minimally debuggable, so that the parent
2689 GDB process can connect to the child and take over. This function
2690 should do only the minimum to make that possible, and to
2691 synchronize with the parent process. The parent process should
2692 take care of the details. */
2695 procfs_set_exec_trap (void)
2697 /* This routine called on the child side (inferior side)
2698 after GDB forks the inferior. It must use only local variables,
2699 because it may be sharing data space with its parent. */
2704 pi
= create_procinfo (getpid (), 0);
2706 perror_with_name (_("procfs: create_procinfo failed in child."));
2708 if (open_procinfo_files (pi
, FD_CTL
) == 0)
2710 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
2711 gdb_flush (gdb_stderr
);
2712 /* No need to call "dead_procinfo", because we're going to
2717 exitset
= XNEW (sysset_t
);
2718 premptyset (exitset
);
2719 praddset (exitset
, SYS_execve
);
2721 if (!proc_set_traced_sysexit (pi
, exitset
))
2723 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
2724 gdb_flush (gdb_stderr
);
2728 /* FIXME: should this be done in the parent instead? */
2729 /* Turn off inherit on fork flag so that all grand-children
2730 of gdb start with tracing flags cleared. */
2731 if (!proc_unset_inherit_on_fork (pi
))
2732 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
2734 /* Turn off run on last close flag, so that the child process
2735 cannot run away just because we close our handle on it.
2736 We want it to wait for the parent to attach. */
2737 if (!proc_unset_run_on_last_close (pi
))
2738 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
2740 /* FIXME: No need to destroy the procinfo --
2741 we have our own address space, and we're about to do an exec! */
2742 /*destroy_procinfo (pi);*/
2745 /* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2).
2746 This avoids a possible deadlock gdb and its vfork'ed child. */
2748 procfs_pre_trace (void)
2752 /* This function is called BEFORE gdb forks the inferior process. Its
2753 only real responsibility is to set things up for the fork, and tell
2754 GDB which two functions to call after the fork (one for the parent,
2755 and one for the child).
2757 This function does a complicated search for a unix shell program,
2758 which it then uses to parse arguments and environment variables to
2759 be sent to the child. I wonder whether this code could not be
2760 abstracted out and shared with other unix targets such as
2764 procfs_target::create_inferior (const char *exec_file
,
2765 const std::string
&allargs
,
2766 char **env
, int from_tty
)
2768 const char *shell_file
= get_shell ();
2772 if (strchr (shell_file
, '/') == NULL
)
2775 /* We will be looking down the PATH to find shell_file. If we
2776 just do this the normal way (via execlp, which operates by
2777 attempting an exec for each element of the PATH until it
2778 finds one which succeeds), then there will be an exec for
2779 each failed attempt, each of which will cause a PR_SYSEXIT
2780 stop, and we won't know how to distinguish the PR_SYSEXIT's
2781 for these failed execs with the ones for successful execs
2782 (whether the exec has succeeded is stored at that time in the
2783 carry bit or some such architecture-specific and
2784 non-ABI-specified place).
2786 So I can't think of anything better than to search the PATH
2787 now. This has several disadvantages: (1) There is a race
2788 condition; if we find a file now and it is deleted before we
2789 exec it, we lose, even if the deletion leaves a valid file
2790 further down in the PATH, (2) there is no way to know exactly
2791 what an executable (in the sense of "capable of being
2792 exec'd") file is. Using access() loses because it may lose
2793 if the caller is the superuser; failing to use it loses if
2794 there are ACLs or some such. */
2798 /* FIXME-maybe: might want "set path" command so user can change what
2799 path is used from within GDB. */
2800 const char *path
= getenv ("PATH");
2802 struct stat statbuf
;
2805 path
= "/bin:/usr/bin";
2807 tryname
= (char *) alloca (strlen (path
) + strlen (shell_file
) + 2);
2808 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
2810 p1
= strchr (p
, ':');
2815 strncpy (tryname
, p
, len
);
2816 tryname
[len
] = '\0';
2817 strcat (tryname
, "/");
2818 strcat (tryname
, shell_file
);
2819 if (access (tryname
, X_OK
) < 0)
2821 if (stat (tryname
, &statbuf
) < 0)
2823 if (!S_ISREG (statbuf
.st_mode
))
2824 /* We certainly need to reject directories. I'm not quite
2825 as sure about FIFOs, sockets, etc., but I kind of doubt
2826 that people want to exec() these things. */
2831 /* Not found. This must be an error rather than merely passing
2832 the file to execlp(), because execlp() would try all the
2833 exec()s, causing GDB to get confused. */
2834 error (_("procfs:%d -- Can't find shell %s in PATH"),
2835 __LINE__
, shell_file
);
2837 shell_file
= tryname
;
2840 inferior
*inf
= current_inferior ();
2841 if (!inf
->target_is_pushed (this))
2842 inf
->push_target (this);
2844 pid
= fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
2845 NULL
, procfs_pre_trace
, shell_file
, NULL
);
2847 /* We have something that executes now. We'll be running through
2848 the shell at this point (if startup-with-shell is true), but the
2849 pid shouldn't change. */
2850 thread_info
*thr
= add_thread_silent (this, ptid_t (pid
));
2851 switch_to_thread (thr
);
2853 procfs_init_inferior (pid
);
2856 /* Callback for update_thread_list. Calls "add_thread". */
2859 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
2861 ptid_t gdb_threadid
= ptid_t (pi
->pid
, thread
->tid
, 0);
2863 thread_info
*thr
= find_thread_ptid (&the_procfs_target
, gdb_threadid
);
2864 if (thr
== NULL
|| thr
->state
== THREAD_EXITED
)
2865 add_thread (&the_procfs_target
, gdb_threadid
);
2870 /* Query all the threads that the target knows about, and give them
2871 back to GDB to add to its list. */
2874 procfs_target::update_thread_list ()
2880 /* Find procinfo for main process. */
2881 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2882 proc_update_threads (pi
);
2883 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
2886 /* Return true if the thread is still 'alive'. This guy doesn't
2887 really seem to be doing his job. Got to investigate how to tell
2888 when a thread is really gone. */
2891 procfs_target::thread_alive (ptid_t ptid
)
2897 thread
= ptid
.lwp ();
2898 /* If I don't know it, it ain't alive! */
2899 pi
= find_procinfo (proc
, thread
);
2903 /* If I can't get its status, it ain't alive!
2904 What's more, I need to forget about it! */
2905 if (!proc_get_status (pi
))
2907 destroy_procinfo (pi
);
2910 /* I couldn't have got its status if it weren't alive, so it's
2915 /* Convert PTID to a string. */
2918 procfs_target::pid_to_str (ptid_t ptid
)
2920 if (ptid
.lwp () == 0)
2921 return string_printf ("process %d", ptid
.pid ());
2923 return string_printf ("LWP %ld", ptid
.lwp ());
2926 /* Accepts an integer PID; Returns a string representing a file that
2927 can be opened to get the symbols for the child process. */
2930 procfs_target::pid_to_exec_file (int pid
)
2932 static char buf
[PATH_MAX
];
2933 char name
[PATH_MAX
];
2935 /* Solaris 11 introduced /proc/<proc-id>/execname. */
2936 xsnprintf (name
, sizeof (name
), "/proc/%d/execname", pid
);
2937 scoped_fd
fd (gdb_open_cloexec (name
, O_RDONLY
, 0));
2938 if (fd
.get () < 0 || read (fd
.get (), buf
, PATH_MAX
- 1) < 0)
2940 /* If that fails, fall back to /proc/<proc-id>/path/a.out introduced in
2944 xsnprintf (name
, sizeof (name
), "/proc/%d/path/a.out", pid
);
2945 len
= readlink (name
, buf
, PATH_MAX
- 1);
2955 /* Insert a watchpoint. */
2958 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
2964 pi
= find_procinfo_or_die (ptid
.pid () == -1 ?
2965 inferior_ptid
.pid () : ptid
.pid (),
2968 /* Translate from GDB's flags to /proc's. */
2969 if (len
> 0) /* len == 0 means delete watchpoint. */
2971 switch (rwflag
) { /* FIXME: need an enum! */
2972 case hw_write
: /* default watchpoint (write) */
2975 case hw_read
: /* read watchpoint */
2978 case hw_access
: /* access watchpoint */
2979 pflags
= WA_READ
| WA_WRITE
;
2981 case hw_execute
: /* execution HW breakpoint */
2984 default: /* Something weird. Return error. */
2987 if (after
) /* Stop after r/w access is completed. */
2988 pflags
|= WA_TRAPAFTER
;
2991 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
2993 if (errno
== E2BIG
) /* Typical error for no resources. */
2994 return -1; /* fail */
2995 /* GDB may try to remove the same watchpoint twice.
2996 If a remove request returns no match, don't error. */
2997 if (errno
== ESRCH
&& len
== 0)
2998 return 0; /* ignore */
2999 proc_error (pi
, "set_watchpoint", __LINE__
);
3004 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
3005 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
3006 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
3010 procfs_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int othertype
)
3012 /* Due to the way that proc_set_watchpoint() is implemented, host
3013 and target pointers must be of the same size. If they are not,
3014 we can't use hardware watchpoints. This limitation is due to the
3015 fact that proc_set_watchpoint() calls
3016 procfs_address_to_host_pointer(); a close inspection of
3017 procfs_address_to_host_pointer will reveal that an internal error
3018 will be generated when the host and target pointer sizes are
3020 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
3022 if (sizeof (void *) != TYPE_LENGTH (ptr_type
))
3025 /* Other tests here??? */
3030 /* Returns non-zero if process is stopped on a hardware watchpoint
3031 fault, else returns zero. */
3034 procfs_target::stopped_by_watchpoint ()
3038 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3040 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3041 if (proc_why (pi
) == PR_FAULTED
)
3042 if (proc_what (pi
) == FLTWATCH
)
3047 /* Returns 1 if the OS knows the position of the triggered watchpoint,
3048 and sets *ADDR to that address. Returns 0 if OS cannot report that
3049 address. This function is only called if
3050 procfs_stopped_by_watchpoint returned 1, thus no further checks are
3051 done. The function also assumes that ADDR is not NULL. */
3054 procfs_target::stopped_data_address (CORE_ADDR
*addr
)
3058 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3059 return proc_watchpoint_address (pi
, addr
);
3063 procfs_target::insert_watchpoint (CORE_ADDR addr
, int len
,
3064 enum target_hw_bp_type type
,
3065 struct expression
*cond
)
3067 if (!target_have_steppable_watchpoint ()
3068 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
3069 /* When a hardware watchpoint fires off the PC will be left at
3070 the instruction following the one which caused the
3071 watchpoint. It will *NOT* be necessary for GDB to step over
3073 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 1);
3075 /* When a hardware watchpoint fires off the PC will be left at
3076 the instruction which caused the watchpoint. It will be
3077 necessary for GDB to step over the watchpoint. */
3078 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 0);
3082 procfs_target::remove_watchpoint (CORE_ADDR addr
, int len
,
3083 enum target_hw_bp_type type
,
3084 struct expression
*cond
)
3086 return procfs_set_watchpoint (inferior_ptid
, addr
, 0, 0, 0);
3090 procfs_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
3092 /* The man page for proc(4) on Solaris 2.6 and up says that the
3093 system can support "thousands" of hardware watchpoints, but gives
3094 no method for finding out how many; It doesn't say anything about
3095 the allowed size for the watched area either. So we just tell
3100 /* Memory Mappings Functions: */
3102 /* Call a callback function once for each mapping, passing it the
3103 mapping, an optional secondary callback function, and some optional
3104 opaque data. Quit and return the first non-zero value returned
3107 PI is the procinfo struct for the process to be mapped. FUNC is
3108 the callback function to be called by this iterator. DATA is the
3109 optional opaque data to be passed to the callback function.
3110 CHILD_FUNC is the optional secondary function pointer to be passed
3111 to the child function. Returns the first non-zero return value
3112 from the callback function, or zero. */
3115 iterate_over_mappings (procinfo
*pi
, find_memory_region_ftype child_func
,
3117 int (*func
) (struct prmap
*map
,
3118 find_memory_region_ftype child_func
,
3121 char pathname
[MAX_PROC_NAME_SIZE
];
3122 struct prmap
*prmaps
;
3123 struct prmap
*prmap
;
3128 /* Get the number of mappings, allocate space,
3129 and read the mappings into prmaps. */
3131 xsnprintf (pathname
, sizeof (pathname
), "/proc/%d/map", pi
->pid
);
3133 scoped_fd
map_fd (open (pathname
, O_RDONLY
));
3134 if (map_fd
.get () < 0)
3135 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
3137 /* Use stat to determine the file size, and compute
3138 the number of prmap_t objects it contains. */
3139 if (fstat (map_fd
.get (), &sbuf
) != 0)
3140 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
3142 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
3143 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
3144 if (read (map_fd
.get (), (char *) prmaps
, nmap
* sizeof (*prmaps
))
3145 != (nmap
* sizeof (*prmaps
)))
3146 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
3148 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
3150 funcstat
= (*func
) (prmap
, child_func
, data
);
3158 /* Implements the to_find_memory_regions method. Calls an external
3159 function for each memory region.
3160 Returns the integer value returned by the callback. */
3163 find_memory_regions_callback (struct prmap
*map
,
3164 find_memory_region_ftype func
, void *data
)
3166 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
3168 (map
->pr_mflags
& MA_READ
) != 0,
3169 (map
->pr_mflags
& MA_WRITE
) != 0,
3170 (map
->pr_mflags
& MA_EXEC
) != 0,
3171 1, /* MODIFIED is unknown, pass it as true. */
3175 /* External interface. Calls a callback function once for each
3176 mapped memory region in the child process, passing as arguments:
3178 CORE_ADDR virtual_address,
3180 int read, TRUE if region is readable by the child
3181 int write, TRUE if region is writable by the child
3182 int execute TRUE if region is executable by the child.
3184 Stops iterating and returns the first non-zero value returned by
3188 procfs_target::find_memory_regions (find_memory_region_ftype func
, void *data
)
3190 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3192 return iterate_over_mappings (pi
, func
, data
,
3193 find_memory_regions_callback
);
3196 /* Returns an ascii representation of a memory mapping's flags. */
3199 mappingflags (long flags
)
3201 static char asciiflags
[8];
3203 strcpy (asciiflags
, "-------");
3204 if (flags
& MA_STACK
)
3205 asciiflags
[1] = 's';
3206 if (flags
& MA_BREAK
)
3207 asciiflags
[2] = 'b';
3208 if (flags
& MA_SHARED
)
3209 asciiflags
[3] = 's';
3210 if (flags
& MA_READ
)
3211 asciiflags
[4] = 'r';
3212 if (flags
& MA_WRITE
)
3213 asciiflags
[5] = 'w';
3214 if (flags
& MA_EXEC
)
3215 asciiflags
[6] = 'x';
3216 return (asciiflags
);
3219 /* Callback function, does the actual work for 'info proc
3223 info_mappings_callback (struct prmap
*map
, find_memory_region_ftype ignore
,
3226 unsigned int pr_off
;
3228 pr_off
= (unsigned int) map
->pr_offset
;
3230 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
3231 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
3232 (unsigned long) map
->pr_vaddr
,
3233 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3234 (unsigned long) map
->pr_size
,
3236 mappingflags (map
->pr_mflags
));
3238 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
3239 (unsigned long) map
->pr_vaddr
,
3240 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3241 (unsigned long) map
->pr_size
,
3243 mappingflags (map
->pr_mflags
));
3248 /* Implement the "info proc mappings" subcommand. */
3251 info_proc_mappings (procinfo
*pi
, int summary
)
3254 return; /* No output for summary mode. */
3256 printf_filtered (_("Mapped address spaces:\n\n"));
3257 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
3258 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3265 printf_filtered (" %18s %18s %10s %10s %7s\n",
3272 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
3273 printf_filtered ("\n");
3276 /* Implement the "info proc" command. */
3279 procfs_target::info_proc (const char *args
, enum info_proc_what what
)
3281 procinfo
*process
= NULL
;
3282 procinfo
*thread
= NULL
;
3299 error (_("Not supported on this target."));
3302 gdb_argv
built_argv (args
);
3303 for (char *arg
: built_argv
)
3305 if (isdigit (arg
[0]))
3307 pid
= strtoul (arg
, &tmp
, 10);
3309 tid
= strtoul (++tmp
, NULL
, 10);
3311 else if (arg
[0] == '/')
3313 tid
= strtoul (arg
+ 1, NULL
, 10);
3317 procinfo_up temporary_procinfo
;
3319 pid
= inferior_ptid
.pid ();
3321 error (_("No current process: you must name one."));
3324 /* Have pid, will travel.
3325 First see if it's a process we're already debugging. */
3326 process
= find_procinfo (pid
, 0);
3327 if (process
== NULL
)
3329 /* No. So open a procinfo for it, but
3330 remember to close it again when finished. */
3331 process
= create_procinfo (pid
, 0);
3332 temporary_procinfo
.reset (process
);
3333 if (!open_procinfo_files (process
, FD_CTL
))
3334 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
3338 thread
= create_procinfo (pid
, tid
);
3342 printf_filtered (_("process %d flags:\n"), process
->pid
);
3343 proc_prettyprint_flags (proc_flags (process
), 1);
3344 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
3345 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
3346 if (proc_get_nthreads (process
) > 1)
3347 printf_filtered ("Process has %d threads.\n",
3348 proc_get_nthreads (process
));
3352 printf_filtered (_("thread %d flags:\n"), thread
->tid
);
3353 proc_prettyprint_flags (proc_flags (thread
), 1);
3354 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
3355 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
3359 info_proc_mappings (process
, 0);
3364 /* Modify the status of the system call identified by SYSCALLNUM in
3365 the set of syscalls that are currently traced/debugged.
3367 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
3368 will be updated. Otherwise, the exit syscalls set will be updated.
3370 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
3371 will be disabled. */
3374 proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
, int entry_or_exit
,
3375 int mode
, int from_tty
)
3379 if (entry_or_exit
== PR_SYSENTRY
)
3380 sysset
= proc_get_traced_sysentry (pi
, NULL
);
3382 sysset
= proc_get_traced_sysexit (pi
, NULL
);
3385 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
3387 if (mode
== FLAG_SET
)
3388 praddset (sysset
, syscallnum
);
3390 prdelset (sysset
, syscallnum
);
3392 if (entry_or_exit
== PR_SYSENTRY
)
3394 if (!proc_set_traced_sysentry (pi
, sysset
))
3395 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
3399 if (!proc_set_traced_sysexit (pi
, sysset
))
3400 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
3405 proc_trace_syscalls (const char *args
, int from_tty
, int entry_or_exit
, int mode
)
3409 if (inferior_ptid
.pid () <= 0)
3410 error (_("you must be debugging a process to use this command."));
3412 if (args
== NULL
|| args
[0] == 0)
3413 error_no_arg (_("system call to trace"));
3415 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3416 if (isdigit (args
[0]))
3418 const int syscallnum
= atoi (args
);
3420 proc_trace_syscalls_1 (pi
, syscallnum
, entry_or_exit
, mode
, from_tty
);
3425 proc_trace_sysentry_cmd (const char *args
, int from_tty
)
3427 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
3431 proc_trace_sysexit_cmd (const char *args
, int from_tty
)
3433 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
3437 proc_untrace_sysentry_cmd (const char *args
, int from_tty
)
3439 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
3443 proc_untrace_sysexit_cmd (const char *args
, int from_tty
)
3445 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
3448 void _initialize_procfs ();
3450 _initialize_procfs ()
3452 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
3453 _("Give a trace of entries into the syscall."));
3454 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
3455 _("Give a trace of exits from the syscall."));
3456 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
3457 _("Cancel a trace of entries into the syscall."));
3458 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
3459 _("Cancel a trace of exits from the syscall."));
3461 add_inf_child_target (&the_procfs_target
);
3464 /* =================== END, GDB "MODULE" =================== */
3468 /* miscellaneous stubs: */
3470 /* The following satisfy a few random symbols mostly created by the
3471 solaris threads implementation, which I will chase down later. */
3473 /* Return a pid for which we guarantee we will be able to find a
3477 procfs_first_available (void)
3479 return ptid_t (procinfo_list
? procinfo_list
->pid
: -1);
3482 /* =================== GCORE .NOTE "MODULE" =================== */
3485 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3486 gdb::unique_xmalloc_ptr
<char> ¬e_data
,
3487 int *note_size
, enum gdb_signal stop_signal
)
3489 struct regcache
*regcache
= get_thread_regcache (&the_procfs_target
, ptid
);
3490 gdb_gregset_t gregs
;
3491 gdb_fpregset_t fpregs
;
3492 unsigned long merged_pid
;
3494 merged_pid
= ptid
.lwp () << 16 | ptid
.pid ();
3496 /* This part is the old method for fetching registers.
3497 It should be replaced by the newer one using regsets
3498 once it is implemented in this platform:
3499 gdbarch_iterate_over_regset_sections(). */
3501 target_fetch_registers (regcache
, -1);
3503 fill_gregset (regcache
, &gregs
, -1);
3504 note_data
.reset (elfcore_write_lwpstatus (obfd
,
3505 note_data
.release (),
3510 fill_fpregset (regcache
, &fpregs
, -1);
3511 note_data
.reset (elfcore_write_prfpreg (obfd
,
3512 note_data
.release (),
3518 struct procfs_corefile_thread_data
3520 procfs_corefile_thread_data (bfd
*obfd
,
3521 gdb::unique_xmalloc_ptr
<char> ¬e_data
,
3522 int *note_size
, gdb_signal stop_signal
)
3523 : obfd (obfd
), note_data (note_data
), note_size (note_size
),
3524 stop_signal (stop_signal
)
3528 gdb::unique_xmalloc_ptr
<char> ¬e_data
;
3530 enum gdb_signal stop_signal
;
3534 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
3536 struct procfs_corefile_thread_data
*args
3537 = (struct procfs_corefile_thread_data
*) data
;
3541 ptid_t ptid
= ptid_t (pi
->pid
, thread
->tid
, 0);
3543 procfs_do_thread_registers (args
->obfd
, ptid
,
3552 find_signalled_thread (struct thread_info
*info
, void *data
)
3554 if (info
->stop_signal () != GDB_SIGNAL_0
3555 && info
->ptid
.pid () == inferior_ptid
.pid ())
3561 static enum gdb_signal
3562 find_stop_signal (void)
3564 struct thread_info
*info
=
3565 iterate_over_threads (find_signalled_thread
, NULL
);
3568 return info
->stop_signal ();
3570 return GDB_SIGNAL_0
;
3573 gdb::unique_xmalloc_ptr
<char>
3574 procfs_target::make_corefile_notes (bfd
*obfd
, int *note_size
)
3576 gdb_gregset_t gregs
;
3577 char fname
[16] = {'\0'};
3578 char psargs
[80] = {'\0'};
3579 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3580 gdb::unique_xmalloc_ptr
<char> note_data
;
3581 enum gdb_signal stop_signal
;
3583 if (get_exec_file (0))
3585 strncpy (fname
, lbasename (get_exec_file (0)), sizeof (fname
));
3586 fname
[sizeof (fname
) - 1] = 0;
3587 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3588 psargs
[sizeof (psargs
) - 1] = 0;
3590 const std::string
&inf_args
= current_inferior ()->args ();
3591 if (!inf_args
.empty () &&
3592 inf_args
.length () < ((int) sizeof (psargs
) - (int) strlen (psargs
)))
3594 strncat (psargs
, " ",
3595 sizeof (psargs
) - strlen (psargs
));
3596 strncat (psargs
, inf_args
.c_str (),
3597 sizeof (psargs
) - strlen (psargs
));
3601 note_data
.reset (elfcore_write_prpsinfo (obfd
,
3602 note_data
.release (),
3607 stop_signal
= find_stop_signal ();
3609 fill_gregset (get_current_regcache (), &gregs
, -1);
3610 note_data
.reset (elfcore_write_pstatus (obfd
, note_data
.release (), note_size
,
3611 inferior_ptid
.pid (),
3612 stop_signal
, &gregs
));
3614 procfs_corefile_thread_data
thread_args (obfd
, note_data
, note_size
,
3616 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
,
3619 gdb::optional
<gdb::byte_vector
> auxv
=
3620 target_read_alloc (current_inferior ()->top_target (),
3621 TARGET_OBJECT_AUXV
, NULL
);
3622 if (auxv
&& !auxv
->empty ())
3623 note_data
.reset (elfcore_write_note (obfd
, note_data
.release (), note_size
,
3624 "CORE", NT_AUXV
, auxv
->data (),
3629 /* =================== END GCORE .NOTE "MODULE" =================== */