1 /* Machine independent support for Solaris /proc (process file system) for GDB.
3 Copyright (C) 1999-2018 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"
34 #include "filestuff.h"
36 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
38 #include <sys/procfs.h>
39 #include <sys/fault.h>
40 #include <sys/syscall.h>
48 #include "observable.h"
49 #include "common/scoped_fd.h"
51 /* This module provides the interface between GDB and the
52 /proc file system, which is used on many versions of Unix
53 as a means for debuggers to control other processes.
55 /proc works by imitating a file system: you open a simulated file
56 that represents the process you wish to interact with, and perform
57 operations on that "file" in order to examine or change the state
60 The most important thing to know about /proc and this module is
61 that there are two very different interfaces to /proc:
63 One that uses the ioctl system call, and another that uses read
64 and write system calls.
66 This module supports only the Solaris version of the read/write
69 #include <sys/types.h>
70 #include <dirent.h> /* opendir/readdir, for listing the LWP's */
72 #include <fcntl.h> /* for O_RDONLY */
73 #include <unistd.h> /* for "X_OK" */
74 #include <sys/stat.h> /* for struct stat */
76 /* Note: procfs-utils.h must be included after the above system header
77 files, because it redefines various system calls using macros.
78 This may be incompatible with the prototype declarations. */
80 #include "proc-utils.h"
82 /* Prototypes for supply_gregset etc. */
85 /* =================== TARGET_OPS "MODULE" =================== */
87 /* This module defines the GDB target vector and its methods. */
90 static enum target_xfer_status
procfs_xfer_memory (gdb_byte
*,
95 class procfs_target final
: public inf_child_target
98 void create_inferior (const char *, const std::string
&,
99 char **, int) override
;
101 void kill () override
;
103 void mourn_inferior () override
;
105 void attach (const char *, int) override
;
106 void detach (inferior
*inf
, int) override
;
108 void resume (ptid_t
, int, enum gdb_signal
) override
;
109 ptid_t
wait (ptid_t
, struct target_waitstatus
*, int) override
;
111 void fetch_registers (struct regcache
*, int) override
;
112 void store_registers (struct regcache
*, int) override
;
114 enum target_xfer_status
xfer_partial (enum target_object object
,
117 const gdb_byte
*writebuf
,
118 ULONGEST offset
, ULONGEST len
,
119 ULONGEST
*xfered_len
) override
;
121 void pass_signals (int, unsigned char *) override
;
123 void files_info () override
;
125 void update_thread_list () override
;
127 bool thread_alive (ptid_t ptid
) override
;
129 const char *pid_to_str (ptid_t
) override
;
131 char *pid_to_exec_file (int pid
) override
;
133 thread_control_capabilities
get_thread_control_capabilities () override
134 { return tc_schedlock
; }
136 /* find_memory_regions support method for gcore */
137 int find_memory_regions (find_memory_region_ftype func
, void *data
)
140 char *make_corefile_notes (bfd
*, int *) override
;
142 bool info_proc (const char *, enum info_proc_what
) override
;
144 #if PR_MODEL_NATIVE == PR_MODEL_LP64
145 int auxv_parse (gdb_byte
**readptr
,
146 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
150 bool stopped_by_watchpoint () override
;
152 int insert_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
153 struct expression
*) override
;
155 int remove_watchpoint (CORE_ADDR
, int, enum target_hw_bp_type
,
156 struct expression
*) override
;
158 int region_ok_for_hw_watchpoint (CORE_ADDR
, int) override
;
160 int can_use_hw_breakpoint (enum bptype
, int, int) override
;
161 bool stopped_data_address (CORE_ADDR
*) override
;
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 /* World Unification:
199 Put any typedefs, defines etc. here that are required for the
200 unification of code that handles different versions of /proc. */
202 enum { READ_WATCHFLAG
= WA_READ
,
203 WRITE_WATCHFLAG
= WA_WRITE
,
204 EXEC_WATCHFLAG
= WA_EXEC
,
205 AFTER_WATCHFLAG
= WA_TRAPAFTER
209 /* =================== STRUCT PROCINFO "MODULE" =================== */
211 /* FIXME: this comment will soon be out of date W.R.T. threads. */
213 /* The procinfo struct is a wrapper to hold all the state information
214 concerning a /proc process. There should be exactly one procinfo
215 for each process, and since GDB currently can debug only one
216 process at a time, that means there should be only one procinfo.
217 All of the LWP's of a process can be accessed indirectly thru the
218 single process procinfo.
220 However, against the day when GDB may debug more than one process,
221 this data structure is kept in a list (which for now will hold no
222 more than one member), and many functions will have a pointer to a
223 procinfo as an argument.
225 There will be a separate procinfo structure for use by the (not yet
226 implemented) "info proc" command, so that we can print useful
227 information about any random process without interfering with the
228 inferior's procinfo information. */
230 /* format strings for /proc paths */
231 #define MAIN_PROC_NAME_FMT "/proc/%d"
232 #define CTL_PROC_NAME_FMT "/proc/%d/ctl"
233 #define AS_PROC_NAME_FMT "/proc/%d/as"
234 #define MAP_PROC_NAME_FMT "/proc/%d/map"
235 #define STATUS_PROC_NAME_FMT "/proc/%d/status"
236 #define MAX_PROC_NAME_SIZE sizeof("/proc/999999/lwp/0123456789/lwpstatus")
238 typedef struct procinfo
{
239 struct procinfo
*next
;
240 int pid
; /* Process ID */
241 int tid
; /* Thread/LWP id */
245 int ignore_next_sigstop
;
247 int ctl_fd
; /* File descriptor for /proc control file */
248 int status_fd
; /* File descriptor for /proc status file */
249 int as_fd
; /* File descriptor for /proc as file */
251 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
253 fltset_t saved_fltset
; /* Saved traced hardware fault set */
254 sigset_t saved_sigset
; /* Saved traced signal set */
255 sigset_t saved_sighold
; /* Saved held signal set */
256 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
257 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
259 pstatus_t prstatus
; /* Current process status info */
261 struct procinfo
*thread_list
;
263 int status_valid
: 1;
265 int fpregs_valid
: 1;
266 int threads_valid
: 1;
269 static char errmsg
[128]; /* shared error msg buffer */
271 /* Function prototypes for procinfo module: */
273 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
274 static procinfo
*find_procinfo (int pid
, int tid
);
275 static procinfo
*create_procinfo (int pid
, int tid
);
276 static void destroy_procinfo (procinfo
*p
);
277 static void dead_procinfo (procinfo
*p
, const char *msg
, int killp
);
278 static int open_procinfo_files (procinfo
*p
, int which
);
279 static void close_procinfo_files (procinfo
*p
);
281 static int iterate_over_mappings
282 (procinfo
*pi
, find_memory_region_ftype child_func
, void *data
,
283 int (*func
) (struct prmap
*map
, find_memory_region_ftype child_func
,
286 /* The head of the procinfo list: */
287 static procinfo
*procinfo_list
;
289 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
293 find_procinfo (int pid
, int tid
)
297 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
304 /* Don't check threads_valid. If we're updating the
305 thread_list, we want to find whatever threads are already
306 here. This means that in general it is the caller's
307 responsibility to check threads_valid and update before
308 calling find_procinfo, if the caller wants to find a new
311 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
319 /* Calls find_procinfo, but errors on failure. */
322 find_procinfo_or_die (int pid
, int tid
)
324 procinfo
*pi
= find_procinfo (pid
, tid
);
329 error (_("procfs: couldn't find pid %d "
330 "(kernel thread %d) in procinfo list."),
333 error (_("procfs: couldn't find pid %d in procinfo list."), pid
);
338 /* Wrapper for `open'. The appropriate open call is attempted; if
339 unsuccessful, it will be retried as many times as needed for the
340 EAGAIN and EINTR conditions.
342 For other conditions, retry the open a limited number of times. In
343 addition, a short sleep is imposed prior to retrying the open. The
344 reason for this sleep is to give the kernel a chance to catch up
345 and create the file in question in the event that GDB "wins" the
346 race to open a file before the kernel has created it. */
349 open_with_retry (const char *pathname
, int flags
)
351 int retries_remaining
, status
;
353 retries_remaining
= 2;
357 status
= open (pathname
, flags
);
359 if (status
>= 0 || retries_remaining
== 0)
361 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
371 /* Open the file descriptor for the process or LWP. We only open the
372 control file descriptor; the others are opened lazily as needed.
373 Returns the file descriptor, or zero for failure. */
375 enum { FD_CTL
, FD_STATUS
, FD_AS
};
378 open_procinfo_files (procinfo
*pi
, int which
)
380 char tmp
[MAX_PROC_NAME_SIZE
];
383 /* This function is getting ALMOST long enough to break up into
384 several. Here is some rationale:
386 There are several file descriptors that may need to be open
387 for any given process or LWP. The ones we're intereted in are:
388 - control (ctl) write-only change the state
389 - status (status) read-only query the state
390 - address space (as) read/write access memory
391 - map (map) read-only virtual addr map
392 Most of these are opened lazily as they are needed.
393 The pathnames for the 'files' for an LWP look slightly
394 different from those of a first-class process:
395 Pathnames for a process (<proc-id>):
397 /proc/<proc-id>/status
400 Pathnames for an LWP (lwp-id):
401 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
402 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
403 An LWP has no map or address space file descriptor, since
404 the memory map and address space are shared by all LWPs. */
406 /* In this case, there are several different file descriptors that
407 we might be asked to open. The control file descriptor will be
408 opened early, but the others will be opened lazily as they are
411 strcpy (tmp
, pi
->pathname
);
412 switch (which
) { /* Which file descriptor to open? */
415 strcat (tmp
, "/lwpctl");
417 strcat (tmp
, "/ctl");
418 fd
= open_with_retry (tmp
, O_WRONLY
);
425 return 0; /* There is no 'as' file descriptor for an lwp. */
427 fd
= open_with_retry (tmp
, O_RDWR
);
434 strcat (tmp
, "/lwpstatus");
436 strcat (tmp
, "/status");
437 fd
= open_with_retry (tmp
, O_RDONLY
);
443 return 0; /* unknown file descriptor */
446 return 1; /* success */
449 /* Allocate a data structure and link it into the procinfo list.
450 First tries to find a pre-existing one (FIXME: why?). Returns the
451 pointer to new procinfo struct. */
454 create_procinfo (int pid
, int tid
)
456 procinfo
*pi
, *parent
= NULL
;
458 pi
= find_procinfo (pid
, tid
);
460 return pi
; /* Already exists, nothing to do. */
462 /* Find parent before doing malloc, to save having to cleanup. */
464 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
466 doesn't exist yet? */
468 pi
= XNEW (procinfo
);
469 memset (pi
, 0, sizeof (procinfo
));
473 pi
->saved_entryset
= XNEW (sysset_t
);
474 pi
->saved_exitset
= XNEW (sysset_t
);
476 /* Chain into list. */
479 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), MAIN_PROC_NAME_FMT
, pid
);
480 pi
->next
= procinfo_list
;
485 xsnprintf (pi
->pathname
, sizeof (pi
->pathname
), "/proc/%d/lwp/%d",
487 pi
->next
= parent
->thread_list
;
488 parent
->thread_list
= pi
;
493 /* Close all file descriptors associated with the procinfo. */
496 close_procinfo_files (procinfo
*pi
)
502 if (pi
->status_fd
> 0)
503 close (pi
->status_fd
);
504 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
507 /* Destructor function. Close, unlink and deallocate the object. */
510 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
514 /* Step one: unlink the procinfo from its list. */
518 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
521 ptr
->next
= pi
->next
;
525 /* Step two: close any open file descriptors. */
526 close_procinfo_files (pi
);
528 /* Step three: free the memory. */
529 xfree (pi
->saved_entryset
);
530 xfree (pi
->saved_exitset
);
535 destroy_procinfo (procinfo
*pi
)
539 if (pi
->tid
!= 0) /* Destroy a thread procinfo. */
541 tmp
= find_procinfo (pi
->pid
, 0); /* Find the parent process. */
542 destroy_one_procinfo (&tmp
->thread_list
, pi
);
544 else /* Destroy a process procinfo and all its threads. */
546 /* First destroy the children, if any; */
547 while (pi
->thread_list
!= NULL
)
548 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
549 /* Then destroy the parent. Genocide!!! */
550 destroy_one_procinfo (&procinfo_list
, pi
);
554 /* A deleter that calls destroy_procinfo. */
555 struct procinfo_deleter
557 void operator() (procinfo
*pi
) const
559 destroy_procinfo (pi
);
563 typedef std::unique_ptr
<procinfo
, procinfo_deleter
> procinfo_up
;
565 enum { NOKILL
, KILL
};
567 /* To be called on a non_recoverable error for a procinfo. Prints
568 error messages, optionally sends a SIGKILL to the process, then
569 destroys the data structure. */
572 dead_procinfo (procinfo
*pi
, const char *msg
, int kill_p
)
577 print_sys_errmsg (pi
->pathname
, errno
);
580 xsnprintf (procfile
, sizeof (procfile
), "process %d", pi
->pid
);
581 print_sys_errmsg (procfile
, errno
);
584 kill (pi
->pid
, SIGKILL
);
586 destroy_procinfo (pi
);
590 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
592 /* =================== /proc "MODULE" =================== */
594 /* This "module" is the interface layer between the /proc system API
595 and the gdb target vector functions. This layer consists of access
596 functions that encapsulate each of the basic operations that we
597 need to use from the /proc API.
599 The main motivation for this layer is to hide the fact that there
600 are two very different implementations of the /proc API. Rather
601 than have a bunch of #ifdefs all thru the gdb target vector
602 functions, we do our best to hide them all in here. */
604 static long proc_flags (procinfo
*pi
);
605 static int proc_why (procinfo
*pi
);
606 static int proc_what (procinfo
*pi
);
607 static int proc_set_current_signal (procinfo
*pi
, int signo
);
608 static int proc_get_current_thread (procinfo
*pi
);
609 static int proc_iterate_over_threads
611 int (*func
) (procinfo
*, procinfo
*, void *),
615 proc_warn (procinfo
*pi
, const char *func
, int line
)
617 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
618 func
, line
, pi
->pathname
);
619 print_sys_errmsg (errmsg
, errno
);
623 proc_error (procinfo
*pi
, const char *func
, int line
)
625 xsnprintf (errmsg
, sizeof (errmsg
), "procfs: %s line %d, %s",
626 func
, line
, pi
->pathname
);
627 perror_with_name (errmsg
);
630 /* Updates the status struct in the procinfo. There is a 'valid'
631 flag, to let other functions know when this function needs to be
632 called (so the status is only read when it is needed). The status
633 file descriptor is also only opened when it is needed. Returns
634 non-zero for success, zero for failure. */
637 proc_get_status (procinfo
*pi
)
639 /* Status file descriptor is opened "lazily". */
640 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
642 pi
->status_valid
= 0;
646 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
647 pi
->status_valid
= 0; /* fail */
650 /* Sigh... I have to read a different data structure,
651 depending on whether this is a main process or an LWP. */
653 pi
->status_valid
= (read (pi
->status_fd
,
654 (char *) &pi
->prstatus
.pr_lwp
,
655 sizeof (lwpstatus_t
))
656 == sizeof (lwpstatus_t
));
659 pi
->status_valid
= (read (pi
->status_fd
,
660 (char *) &pi
->prstatus
,
662 == sizeof (pstatus_t
));
666 if (pi
->status_valid
)
668 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
671 proc_get_current_thread (pi
));
674 /* The status struct includes general regs, so mark them valid too. */
675 pi
->gregs_valid
= pi
->status_valid
;
676 /* In the read/write multiple-fd model, the status struct includes
677 the fp regs too, so mark them valid too. */
678 pi
->fpregs_valid
= pi
->status_valid
;
679 return pi
->status_valid
; /* True if success, false if failure. */
682 /* Returns the process flags (pr_flags field). */
685 proc_flags (procinfo
*pi
)
687 if (!pi
->status_valid
)
688 if (!proc_get_status (pi
))
689 return 0; /* FIXME: not a good failure value (but what is?) */
691 return pi
->prstatus
.pr_lwp
.pr_flags
;
694 /* Returns the pr_why field (why the process stopped). */
697 proc_why (procinfo
*pi
)
699 if (!pi
->status_valid
)
700 if (!proc_get_status (pi
))
701 return 0; /* FIXME: not a good failure value (but what is?) */
703 return pi
->prstatus
.pr_lwp
.pr_why
;
706 /* Returns the pr_what field (details of why the process stopped). */
709 proc_what (procinfo
*pi
)
711 if (!pi
->status_valid
)
712 if (!proc_get_status (pi
))
713 return 0; /* FIXME: not a good failure value (but what is?) */
715 return pi
->prstatus
.pr_lwp
.pr_what
;
718 /* This function is only called when PI is stopped by a watchpoint.
719 Assuming the OS supports it, write to *ADDR the data address which
720 triggered it and return 1. Return 0 if it is not possible to know
724 proc_watchpoint_address (procinfo
*pi
, CORE_ADDR
*addr
)
726 if (!pi
->status_valid
)
727 if (!proc_get_status (pi
))
730 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
731 builtin_type (target_gdbarch ())->builtin_data_ptr
,
732 (gdb_byte
*) &pi
->prstatus
.pr_lwp
.pr_info
.si_addr
);
736 /* Returns the pr_nsysarg field (number of args to the current
740 proc_nsysarg (procinfo
*pi
)
742 if (!pi
->status_valid
)
743 if (!proc_get_status (pi
))
746 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
749 /* Returns the pr_sysarg field (pointer to the arguments of current
753 proc_sysargs (procinfo
*pi
)
755 if (!pi
->status_valid
)
756 if (!proc_get_status (pi
))
759 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
762 /* Set or reset any of the following process flags:
763 PR_FORK -- forked child will inherit trace flags
764 PR_RLC -- traced process runs when last /proc file closed.
765 PR_KLC -- traced process is killed when last /proc file closed.
766 PR_ASYNC -- LWP's get to run/stop independently.
768 This function is done using read/write [PCSET/PCRESET/PCUNSET].
772 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
773 mode -- 1 for set, 0 for reset.
775 Returns non-zero for success, zero for failure. */
777 enum { FLAG_RESET
, FLAG_SET
};
780 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
782 long win
= 0; /* default to fail */
784 /* These operations affect the process as a whole, and applying them
785 to an individual LWP has the same meaning as applying them to the
786 main process. Therefore, if we're ever called with a pointer to
787 an LWP's procinfo, let's substitute the process's procinfo and
788 avoid opening the LWP's file descriptor unnecessarily. */
791 pi
= find_procinfo_or_die (pi
->pid
, 0);
795 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC). */
797 else /* Reset the flag. */
801 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
803 /* The above operation renders the procinfo's cached pstatus
805 pi
->status_valid
= 0;
808 warning (_("procfs: modify_flag failed to turn %s %s"),
809 flag
== PR_FORK
? "PR_FORK" :
810 flag
== PR_RLC
? "PR_RLC" :
811 flag
== PR_ASYNC
? "PR_ASYNC" :
812 flag
== PR_KLC
? "PR_KLC" :
814 mode
== FLAG_RESET
? "off" : "on");
819 /* Set the run_on_last_close flag. Process with all threads will
820 become runnable when debugger closes all /proc fds. Returns
821 non-zero for success, zero for failure. */
824 proc_set_run_on_last_close (procinfo
*pi
)
826 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
829 /* Reset the run_on_last_close flag. The process will NOT become
830 runnable when debugger closes its file handles. Returns non-zero
831 for success, zero for failure. */
834 proc_unset_run_on_last_close (procinfo
*pi
)
836 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
839 /* Reset inherit_on_fork flag. If the process forks a child while we
840 are registered for events in the parent, then we will NOT recieve
841 events from the child. Returns non-zero for success, zero for
845 proc_unset_inherit_on_fork (procinfo
*pi
)
847 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
850 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
851 (signal etc.), the remaining LWPs will continue to run. Returns
852 non-zero for success, zero for failure. */
855 proc_set_async (procinfo
*pi
)
857 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
860 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
861 (signal etc.), then all other LWPs will stop as well. Returns
862 non-zero for success, zero for failure. */
865 proc_unset_async (procinfo
*pi
)
867 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
870 /* Request the process/LWP to stop. Does not wait. Returns non-zero
871 for success, zero for failure. */
874 proc_stop_process (procinfo
*pi
)
878 /* We might conceivably apply this operation to an LWP, and the
879 LWP's ctl file descriptor might not be open. */
881 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
885 procfs_ctl_t cmd
= PCSTOP
;
887 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
893 /* Wait for the process or LWP to stop (block until it does). Returns
894 non-zero for success, zero for failure. */
897 proc_wait_for_stop (procinfo
*pi
)
901 /* We should never have to apply this operation to any procinfo
902 except the one for the main process. If that ever changes for
903 any reason, then take out the following clause and replace it
904 with one that makes sure the ctl_fd is open. */
907 pi
= find_procinfo_or_die (pi
->pid
, 0);
909 procfs_ctl_t cmd
= PCWSTOP
;
911 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
912 /* We been runnin' and we stopped -- need to update status. */
913 pi
->status_valid
= 0;
918 /* Make the process or LWP runnable.
920 Options (not all are implemented):
922 - clear current fault
923 - clear current signal
924 - abort the current system call
925 - stop as soon as finished with system call
926 - (ioctl): set traced signal set
927 - (ioctl): set held signal set
928 - (ioctl): set traced fault set
929 - (ioctl): set start pc (vaddr)
931 Always clears the current fault. PI is the process or LWP to
932 operate on. If STEP is true, set the process or LWP to trap after
933 one instruction. If SIGNO is zero, clear the current signal if
934 any; if non-zero, set the current signal to this one. Returns
935 non-zero for success, zero for failure. */
938 proc_run_process (procinfo
*pi
, int step
, int signo
)
943 /* We will probably have to apply this operation to individual
944 threads, so make sure the control file descriptor is open. */
946 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
949 runflags
= PRCFAULT
; /* Always clear current fault. */
954 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals. */
955 proc_set_current_signal (pi
, signo
);
961 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
966 /* Register to trace signals in the process or LWP. Returns non-zero
967 for success, zero for failure. */
970 proc_set_traced_signals (procinfo
*pi
, sigset_t
*sigset
)
974 /* We should never have to apply this operation to any procinfo
975 except the one for the main process. If that ever changes for
976 any reason, then take out the following clause and replace it
977 with one that makes sure the ctl_fd is open. */
980 pi
= find_procinfo_or_die (pi
->pid
, 0);
984 /* Use char array to avoid alignment issues. */
985 char sigset
[sizeof (sigset_t
)];
989 memcpy (&arg
.sigset
, sigset
, sizeof (sigset_t
));
991 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
993 /* The above operation renders the procinfo's cached pstatus obsolete. */
994 pi
->status_valid
= 0;
997 warning (_("procfs: set_traced_signals failed"));
1001 /* Register to trace hardware faults in the process or LWP. Returns
1002 non-zero for success, zero for failure. */
1005 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1009 /* We should never have to apply this operation to any procinfo
1010 except the one for the main process. If that ever changes for
1011 any reason, then take out the following clause and replace it
1012 with one that makes sure the ctl_fd is open. */
1015 pi
= find_procinfo_or_die (pi
->pid
, 0);
1019 /* Use char array to avoid alignment issues. */
1020 char fltset
[sizeof (fltset_t
)];
1024 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1026 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1028 /* The above operation renders the procinfo's cached pstatus obsolete. */
1029 pi
->status_valid
= 0;
1034 /* Register to trace entry to system calls in the process or LWP.
1035 Returns non-zero for success, zero for failure. */
1038 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1042 /* We should never have to apply this operation to any procinfo
1043 except the one for the main process. If that ever changes for
1044 any reason, then take out the following clause and replace it
1045 with one that makes sure the ctl_fd is open. */
1048 pi
= find_procinfo_or_die (pi
->pid
, 0);
1052 /* Use char array to avoid alignment issues. */
1053 char sysset
[sizeof (sysset_t
)];
1057 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1059 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1061 /* The above operation renders the procinfo's cached pstatus
1063 pi
->status_valid
= 0;
1068 /* Register to trace exit from system calls in the process or LWP.
1069 Returns non-zero for success, zero for failure. */
1072 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1076 /* We should never have to apply this operation to any procinfo
1077 except the one for the main process. If that ever changes for
1078 any reason, then take out the following clause and replace it
1079 with one that makes sure the ctl_fd is open. */
1082 pi
= find_procinfo_or_die (pi
->pid
, 0);
1084 struct gdb_proc_ctl_pcsexit
{
1086 /* Use char array to avoid alignment issues. */
1087 char sysset
[sizeof (sysset_t
)];
1091 memcpy (&arg
.sysset
, sysset
, sizeof (sysset_t
));
1093 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1095 /* The above operation renders the procinfo's cached pstatus
1097 pi
->status_valid
= 0;
1102 /* Specify the set of blocked / held signals in the process or LWP.
1103 Returns non-zero for success, zero for failure. */
1106 proc_set_held_signals (procinfo
*pi
, sigset_t
*sighold
)
1110 /* We should never have to apply this operation to any procinfo
1111 except the one for the main process. If that ever changes for
1112 any reason, then take out the following clause and replace it
1113 with one that makes sure the ctl_fd is open. */
1116 pi
= find_procinfo_or_die (pi
->pid
, 0);
1120 /* Use char array to avoid alignment issues. */
1121 char hold
[sizeof (sigset_t
)];
1125 memcpy (&arg
.hold
, sighold
, sizeof (sigset_t
));
1126 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1128 /* The above operation renders the procinfo's cached pstatus
1130 pi
->status_valid
= 0;
1135 /* Returns the set of signals that are held / blocked. Will also copy
1136 the sigset if SAVE is non-zero. */
1139 proc_get_held_signals (procinfo
*pi
, sigset_t
*save
)
1141 sigset_t
*ret
= NULL
;
1143 /* We should never have to apply this operation to any procinfo
1144 except the one for the main process. If that ever changes for
1145 any reason, then take out the following clause and replace it
1146 with one that makes sure the ctl_fd is open. */
1149 pi
= find_procinfo_or_die (pi
->pid
, 0);
1151 if (!pi
->status_valid
)
1152 if (!proc_get_status (pi
))
1155 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1157 memcpy (save
, ret
, sizeof (sigset_t
));
1162 /* Returns the set of signals that are traced / debugged. Will also
1163 copy the sigset if SAVE is non-zero. */
1166 proc_get_traced_signals (procinfo
*pi
, sigset_t
*save
)
1168 sigset_t
*ret
= NULL
;
1170 /* We should never have to apply this operation to any procinfo
1171 except the one for the main process. If that ever changes for
1172 any reason, then take out the following clause and replace it
1173 with one that makes sure the ctl_fd is open. */
1176 pi
= find_procinfo_or_die (pi
->pid
, 0);
1178 if (!pi
->status_valid
)
1179 if (!proc_get_status (pi
))
1182 ret
= &pi
->prstatus
.pr_sigtrace
;
1184 memcpy (save
, ret
, sizeof (sigset_t
));
1189 /* Returns the set of hardware faults that are traced /debugged. Will
1190 also copy the faultset if SAVE is non-zero. */
1193 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1195 fltset_t
*ret
= NULL
;
1197 /* We should never have to apply this operation to any procinfo
1198 except the one for the main process. If that ever changes for
1199 any reason, then take out the following clause and replace it
1200 with one that makes sure the ctl_fd is open. */
1203 pi
= find_procinfo_or_die (pi
->pid
, 0);
1205 if (!pi
->status_valid
)
1206 if (!proc_get_status (pi
))
1209 ret
= &pi
->prstatus
.pr_flttrace
;
1211 memcpy (save
, ret
, sizeof (fltset_t
));
1216 /* Returns the set of syscalls that are traced /debugged on entry.
1217 Will also copy the syscall set if SAVE is non-zero. */
1220 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1222 sysset_t
*ret
= NULL
;
1224 /* We should never have to apply this operation to any procinfo
1225 except the one for the main process. If that ever changes for
1226 any reason, then take out the following clause and replace it
1227 with one that makes sure the ctl_fd is open. */
1230 pi
= find_procinfo_or_die (pi
->pid
, 0);
1232 if (!pi
->status_valid
)
1233 if (!proc_get_status (pi
))
1236 ret
= &pi
->prstatus
.pr_sysentry
;
1238 memcpy (save
, ret
, sizeof (sysset_t
));
1243 /* Returns the set of syscalls that are traced /debugged on exit.
1244 Will also copy the syscall set if SAVE is non-zero. */
1247 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1249 sysset_t
*ret
= NULL
;
1251 /* We should never have to apply this operation to any procinfo
1252 except the one for the main process. If that ever changes for
1253 any reason, then take out the following clause and replace it
1254 with one that makes sure the ctl_fd is open. */
1257 pi
= find_procinfo_or_die (pi
->pid
, 0);
1259 if (!pi
->status_valid
)
1260 if (!proc_get_status (pi
))
1263 ret
= &pi
->prstatus
.pr_sysexit
;
1265 memcpy (save
, ret
, sizeof (sysset_t
));
1270 /* The current fault (if any) is cleared; the associated signal will
1271 not be sent to the process or LWP when it resumes. Returns
1272 non-zero for success, zero for failure. */
1275 proc_clear_current_fault (procinfo
*pi
)
1279 /* We should never have to apply this operation to any procinfo
1280 except the one for the main process. If that ever changes for
1281 any reason, then take out the following clause and replace it
1282 with one that makes sure the ctl_fd is open. */
1285 pi
= find_procinfo_or_die (pi
->pid
, 0);
1287 procfs_ctl_t cmd
= PCCFAULT
;
1289 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1294 /* Set the "current signal" that will be delivered next to the
1295 process. NOTE: semantics are different from those of KILL. This
1296 signal will be delivered to the process or LWP immediately when it
1297 is resumed (even if the signal is held/blocked); it will NOT
1298 immediately cause another event of interest, and will NOT first
1299 trap back to the debugger. Returns non-zero for success, zero for
1303 proc_set_current_signal (procinfo
*pi
, int signo
)
1308 /* Use char array to avoid alignment issues. */
1309 char sinfo
[sizeof (siginfo_t
)];
1313 struct target_waitstatus wait_status
;
1315 /* We should never have to apply this operation to any procinfo
1316 except the one for the main process. If that ever changes for
1317 any reason, then take out the following clause and replace it
1318 with one that makes sure the ctl_fd is open. */
1321 pi
= find_procinfo_or_die (pi
->pid
, 0);
1323 /* The pointer is just a type alias. */
1324 get_last_target_status (&wait_ptid
, &wait_status
);
1325 if (wait_ptid
== inferior_ptid
1326 && wait_status
.kind
== TARGET_WAITKIND_STOPPED
1327 && wait_status
.value
.sig
== gdb_signal_from_host (signo
)
1328 && proc_get_status (pi
)
1329 && pi
->prstatus
.pr_lwp
.pr_info
.si_signo
== signo
1331 /* Use the siginfo associated with the signal being
1333 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_lwp
.pr_info
, sizeof (siginfo_t
));
1336 mysinfo
.si_signo
= signo
;
1337 mysinfo
.si_code
= 0;
1338 mysinfo
.si_pid
= getpid (); /* ?why? */
1339 mysinfo
.si_uid
= getuid (); /* ?why? */
1340 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1344 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1349 /* The current signal (if any) is cleared, and is not sent to the
1350 process or LWP when it resumes. Returns non-zero for success, zero
1354 proc_clear_current_signal (procinfo
*pi
)
1358 /* We should never have to apply this operation to any procinfo
1359 except the one for the main process. If that ever changes for
1360 any reason, then take out the following clause and replace it
1361 with one that makes sure the ctl_fd is open. */
1364 pi
= find_procinfo_or_die (pi
->pid
, 0);
1368 /* Use char array to avoid alignment issues. */
1369 char sinfo
[sizeof (siginfo_t
)];
1374 /* The pointer is just a type alias. */
1375 mysinfo
.si_signo
= 0;
1376 mysinfo
.si_code
= 0;
1377 mysinfo
.si_errno
= 0;
1378 mysinfo
.si_pid
= getpid (); /* ?why? */
1379 mysinfo
.si_uid
= getuid (); /* ?why? */
1380 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1382 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1387 /* Return the general-purpose registers for the process or LWP
1388 corresponding to PI. Upon failure, return NULL. */
1390 static gdb_gregset_t
*
1391 proc_get_gregs (procinfo
*pi
)
1393 if (!pi
->status_valid
|| !pi
->gregs_valid
)
1394 if (!proc_get_status (pi
))
1397 return &pi
->prstatus
.pr_lwp
.pr_reg
;
1400 /* Return the general-purpose registers for the process or LWP
1401 corresponding to PI. Upon failure, return NULL. */
1403 static gdb_fpregset_t
*
1404 proc_get_fpregs (procinfo
*pi
)
1406 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
1407 if (!proc_get_status (pi
))
1410 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
1413 /* Write the general-purpose registers back to the process or LWP
1414 corresponding to PI. Return non-zero for success, zero for
1418 proc_set_gregs (procinfo
*pi
)
1420 gdb_gregset_t
*gregs
;
1423 gregs
= proc_get_gregs (pi
);
1425 return 0; /* proc_get_regs has already warned. */
1427 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1433 /* Use char array to avoid alignment issues. */
1434 char gregs
[sizeof (gdb_gregset_t
)];
1438 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
1439 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1442 /* Policy: writing the registers invalidates our cache. */
1443 pi
->gregs_valid
= 0;
1447 /* Write the floating-pointer registers back to the process or LWP
1448 corresponding to PI. Return non-zero for success, zero for
1452 proc_set_fpregs (procinfo
*pi
)
1454 gdb_fpregset_t
*fpregs
;
1457 fpregs
= proc_get_fpregs (pi
);
1459 return 0; /* proc_get_fpregs has already warned. */
1461 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1467 /* Use char array to avoid alignment issues. */
1468 char fpregs
[sizeof (gdb_fpregset_t
)];
1472 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
1473 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1476 /* Policy: writing the registers invalidates our cache. */
1477 pi
->fpregs_valid
= 0;
1481 /* Send a signal to the proc or lwp with the semantics of "kill()".
1482 Returns non-zero for success, zero for failure. */
1485 proc_kill (procinfo
*pi
, int signo
)
1489 /* We might conceivably apply this operation to an LWP, and the
1490 LWP's ctl file descriptor might not be open. */
1492 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1496 procfs_ctl_t cmd
[2];
1500 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1506 /* Find the pid of the process that started this one. Returns the
1507 parent process pid, or zero. */
1510 proc_parent_pid (procinfo
*pi
)
1512 /* We should never have to apply this operation to any procinfo
1513 except the one for the main process. If that ever changes for
1514 any reason, then take out the following clause and replace it
1515 with one that makes sure the ctl_fd is open. */
1518 pi
= find_procinfo_or_die (pi
->pid
, 0);
1520 if (!pi
->status_valid
)
1521 if (!proc_get_status (pi
))
1524 return pi
->prstatus
.pr_ppid
;
1527 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
1528 (a.k.a void pointer)! */
1531 procfs_address_to_host_pointer (CORE_ADDR addr
)
1533 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
1536 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (ptr_type
));
1537 gdbarch_address_to_pointer (target_gdbarch (), ptr_type
,
1538 (gdb_byte
*) &ptr
, addr
);
1543 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
1547 char watch
[sizeof (prwatch_t
)];
1551 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
1552 convert a target address into something that can be stored in a
1553 native data structure. */
1554 pwatch
.pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
1555 pwatch
.pr_size
= len
;
1556 pwatch
.pr_wflags
= wflags
;
1558 memcpy (arg
.watch
, &pwatch
, sizeof (prwatch_t
));
1559 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
1562 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
1564 #include <sys/sysi86.h>
1566 /* The KEY is actually the value of the lower 16 bits of the GS
1567 register for the LWP that we're interested in. Returns the
1568 matching ssh struct (LDT entry). */
1571 proc_get_LDT_entry (procinfo
*pi
, int key
) /* ARI: editCase function */
1573 static struct ssd
*ldt_entry
= NULL
;
1574 char pathname
[MAX_PROC_NAME_SIZE
];
1576 /* Allocate space for one LDT entry.
1577 This alloc must persist, because we return a pointer to it. */
1578 if (ldt_entry
== NULL
)
1579 ldt_entry
= XNEW (struct ssd
);
1581 /* Open the file descriptor for the LDT table. */
1582 xsnprintf (pathname
, sizeof (pathname
), "/proc/%d/ldt", pi
->pid
);
1583 scoped_fd
fd (open_with_retry (pathname
, O_RDONLY
));
1586 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
1590 /* Now 'read' thru the table, find a match and return it. */
1591 while (read (fd
.get (), ldt_entry
, sizeof (struct ssd
))
1592 == sizeof (struct ssd
))
1594 if (ldt_entry
->sel
== 0
1595 && ldt_entry
->bo
== 0
1596 && ldt_entry
->acc1
== 0
1597 && ldt_entry
->acc2
== 0)
1598 break; /* end of table */
1599 /* If key matches, return this entry. */
1600 if (ldt_entry
->sel
== key
)
1603 /* Loop ended, match not found. */
1607 /* Returns the pointer to the LDT entry of PTID. */
1610 procfs_find_LDT_entry (ptid_t ptid
) /* ARI: editCase function */
1612 gdb_gregset_t
*gregs
;
1616 /* Find procinfo for the lwp. */
1617 pi
= find_procinfo (ptid
.pid (), ptid
.lwp ());
1620 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
1621 ptid
.pid (), ptid
.lwp ());
1624 /* get its general registers. */
1625 gregs
= proc_get_gregs (pi
);
1628 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
1629 ptid
.pid (), ptid
.lwp ());
1632 /* Now extract the GS register's lower 16 bits. */
1633 key
= (*gregs
)[GS
] & 0xffff;
1635 /* Find the matching entry and return it. */
1636 return proc_get_LDT_entry (pi
, key
);
1641 /* =============== END, non-thread part of /proc "MODULE" =============== */
1643 /* =================== Thread "MODULE" =================== */
1645 /* NOTE: you'll see more ifdefs and duplication of functions here,
1646 since there is a different way to do threads on every OS. */
1648 /* Returns the number of threads for the process. */
1651 proc_get_nthreads (procinfo
*pi
)
1653 if (!pi
->status_valid
)
1654 if (!proc_get_status (pi
))
1657 /* Only works for the process procinfo, because the LWP procinfos do not
1658 get prstatus filled in. */
1659 if (pi
->tid
!= 0) /* Find the parent process procinfo. */
1660 pi
= find_procinfo_or_die (pi
->pid
, 0);
1661 return pi
->prstatus
.pr_nlwp
;
1666 Return the ID of the thread that had an event of interest.
1667 (ie. the one that hit a breakpoint or other traced event). All
1668 other things being equal, this should be the ID of a thread that is
1669 currently executing. */
1672 proc_get_current_thread (procinfo
*pi
)
1674 /* Note: this should be applied to the root procinfo for the
1675 process, not to the procinfo for an LWP. If applied to the
1676 procinfo for an LWP, it will simply return that LWP's ID. In
1677 that case, find the parent process procinfo. */
1680 pi
= find_procinfo_or_die (pi
->pid
, 0);
1682 if (!pi
->status_valid
)
1683 if (!proc_get_status (pi
))
1686 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
1689 /* Discover the IDs of all the threads within the process, and create
1690 a procinfo for each of them (chained to the parent). This
1691 unfortunately requires a different method on every OS. Returns
1692 non-zero for success, zero for failure. */
1695 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
1697 if (thread
&& parent
) /* sanity */
1699 thread
->status_valid
= 0;
1700 if (!proc_get_status (thread
))
1701 destroy_one_procinfo (&parent
->thread_list
, thread
);
1703 return 0; /* keep iterating */
1707 proc_update_threads (procinfo
*pi
)
1709 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
1710 struct dirent
*direntry
;
1715 /* We should never have to apply this operation to any procinfo
1716 except the one for the main process. If that ever changes for
1717 any reason, then take out the following clause and replace it
1718 with one that makes sure the ctl_fd is open. */
1721 pi
= find_procinfo_or_die (pi
->pid
, 0);
1723 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
1725 /* Note: this brute-force method was originally devised for Unixware
1726 (support removed since), and will also work on Solaris 2.6 and
1727 2.7. The original comment mentioned the existence of a much
1728 simpler and more elegant way to do this on Solaris, but didn't
1729 point out what that was. */
1731 strcpy (pathname
, pi
->pathname
);
1732 strcat (pathname
, "/lwp");
1733 dirp
.reset (opendir (pathname
));
1735 proc_error (pi
, "update_threads, opendir", __LINE__
);
1737 while ((direntry
= readdir (dirp
.get ())) != NULL
)
1738 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
1740 lwpid
= atoi (&direntry
->d_name
[0]);
1741 thread
= create_procinfo (pi
->pid
, lwpid
);
1743 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
1745 pi
->threads_valid
= 1;
1749 /* Given a pointer to a function, call that function once for each lwp
1750 in the procinfo list, until the function returns non-zero, in which
1751 event return the value returned by the function.
1753 Note: this function does NOT call update_threads. If you want to
1754 discover new threads first, you must call that function explicitly.
1755 This function just makes a quick pass over the currently-known
1758 PI is the parent process procinfo. FUNC is the per-thread
1759 function. PTR is an opaque parameter for function. Returns the
1760 first non-zero return value from the callee, or zero. */
1763 proc_iterate_over_threads (procinfo
*pi
,
1764 int (*func
) (procinfo
*, procinfo
*, void *),
1767 procinfo
*thread
, *next
;
1770 /* We should never have to apply this operation to any procinfo
1771 except the one for the main process. If that ever changes for
1772 any reason, then take out the following clause and replace it
1773 with one that makes sure the ctl_fd is open. */
1776 pi
= find_procinfo_or_die (pi
->pid
, 0);
1778 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
1780 next
= thread
->next
; /* In case thread is destroyed. */
1781 retval
= (*func
) (pi
, thread
, ptr
);
1789 /* =================== END, Thread "MODULE" =================== */
1791 /* =================== END, /proc "MODULE" =================== */
1793 /* =================== GDB "MODULE" =================== */
1795 /* Here are all of the gdb target vector functions and their
1798 static ptid_t
do_attach (ptid_t ptid
);
1799 static void do_detach ();
1800 static void proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
,
1801 int entry_or_exit
, int mode
, int from_tty
);
1803 /* Sets up the inferior to be debugged. Registers to trace signals,
1804 hardware faults, and syscalls. Note: does not set RLC flag: caller
1805 may want to customize that. Returns zero for success (note!
1806 unlike most functions in this module); on failure, returns the LINE
1807 NUMBER where it failed! */
1810 procfs_debug_inferior (procinfo
*pi
)
1812 fltset_t traced_faults
;
1813 sigset_t traced_signals
;
1814 sysset_t
*traced_syscall_entries
;
1815 sysset_t
*traced_syscall_exits
;
1818 /* Register to trace hardware faults in the child. */
1819 prfillset (&traced_faults
); /* trace all faults... */
1820 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
1821 if (!proc_set_traced_faults (pi
, &traced_faults
))
1824 /* Initially, register to trace all signals in the child. */
1825 prfillset (&traced_signals
);
1826 if (!proc_set_traced_signals (pi
, &traced_signals
))
1830 /* Register to trace the 'exit' system call (on entry). */
1831 traced_syscall_entries
= XNEW (sysset_t
);
1832 premptyset (traced_syscall_entries
);
1833 praddset (traced_syscall_entries
, SYS_exit
);
1834 praddset (traced_syscall_entries
, SYS_lwp_exit
);
1836 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
1837 xfree (traced_syscall_entries
);
1841 /* Method for tracing exec syscalls. */
1843 Not all systems with /proc have all the exec* syscalls with the same
1844 names. On the SGI, for example, there is no SYS_exec, but there
1845 *is* a SYS_execv. So, we try to account for that. */
1847 traced_syscall_exits
= XNEW (sysset_t
);
1848 premptyset (traced_syscall_exits
);
1850 praddset (traced_syscall_exits
, SYS_exec
);
1852 praddset (traced_syscall_exits
, SYS_execve
);
1853 praddset (traced_syscall_exits
, SYS_lwp_create
);
1854 praddset (traced_syscall_exits
, SYS_lwp_exit
);
1856 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
1857 xfree (traced_syscall_exits
);
1865 procfs_target::attach (const char *args
, int from_tty
)
1870 pid
= parse_pid_to_attach (args
);
1872 if (pid
== getpid ())
1873 error (_("Attaching GDB to itself is not a good idea..."));
1877 exec_file
= get_exec_file (0);
1880 printf_filtered (_("Attaching to program `%s', %s\n"),
1881 exec_file
, target_pid_to_str (ptid_t (pid
)));
1883 printf_filtered (_("Attaching to %s\n"),
1884 target_pid_to_str (ptid_t (pid
)));
1888 inferior_ptid
= do_attach (ptid_t (pid
));
1889 if (!target_is_pushed (this))
1894 procfs_target::detach (inferior
*inf
, int from_tty
)
1896 int pid
= inferior_ptid
.pid ();
1900 const char *exec_file
;
1902 exec_file
= get_exec_file (0);
1903 if (exec_file
== NULL
)
1906 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file
,
1907 target_pid_to_str (ptid_t (pid
)));
1908 gdb_flush (gdb_stdout
);
1913 inferior_ptid
= null_ptid
;
1914 detach_inferior (inf
);
1915 maybe_unpush_target ();
1919 do_attach (ptid_t ptid
)
1922 struct inferior
*inf
;
1926 pi
= create_procinfo (ptid
.pid (), 0);
1928 perror (_("procfs: out of memory in 'attach'"));
1930 if (!open_procinfo_files (pi
, FD_CTL
))
1932 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
1933 xsnprintf (errmsg
, sizeof (errmsg
),
1934 "do_attach: couldn't open /proc file for process %d",
1936 dead_procinfo (pi
, errmsg
, NOKILL
);
1939 /* Stop the process (if it isn't already stopped). */
1940 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
1942 pi
->was_stopped
= 1;
1943 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
1947 pi
->was_stopped
= 0;
1948 /* Set the process to run again when we close it. */
1949 if (!proc_set_run_on_last_close (pi
))
1950 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
1952 /* Now stop the process. */
1953 if (!proc_stop_process (pi
))
1954 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
1955 pi
->ignore_next_sigstop
= 1;
1957 /* Save some of the /proc state to be restored if we detach. */
1958 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
1959 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
1960 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
1961 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
1962 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
1963 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
1965 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
1966 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
1968 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
1969 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
1971 fail
= procfs_debug_inferior (pi
);
1973 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
1975 inf
= current_inferior ();
1976 inferior_appeared (inf
, pi
->pid
);
1977 /* Let GDB know that the inferior was attached. */
1978 inf
->attach_flag
= 1;
1980 /* Create a procinfo for the current lwp. */
1981 lwpid
= proc_get_current_thread (pi
);
1982 create_procinfo (pi
->pid
, lwpid
);
1984 /* Add it to gdb's thread list. */
1985 ptid
= ptid_t (pi
->pid
, lwpid
, 0);
1996 /* Find procinfo for the main process. */
1997 pi
= find_procinfo_or_die (inferior_ptid
.pid (),
1998 0); /* FIXME: threads */
2000 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
2001 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
2003 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
2004 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
2006 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
2007 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
2009 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
2010 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
2012 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
2013 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
2015 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
2016 if (!(pi
->was_stopped
)
2017 || query (_("Was stopped when attached, make it runnable again? ")))
2019 /* Clear any pending signal. */
2020 if (!proc_clear_current_fault (pi
))
2021 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
2023 if (!proc_clear_current_signal (pi
))
2024 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
2026 if (!proc_set_run_on_last_close (pi
))
2027 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
2030 destroy_procinfo (pi
);
2033 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
2036 ??? Is the following note still relevant? We can't get individual
2037 registers with the PT_GETREGS ptrace(2) request either, yet we
2038 don't bother with caching at all in that case.
2040 NOTE: Since the /proc interface cannot give us individual
2041 registers, we pay no attention to REGNUM, and just fetch them all.
2042 This results in the possibility that we will do unnecessarily many
2043 fetches, since we may be called repeatedly for individual
2044 registers. So we cache the results, and mark the cache invalid
2045 when the process is resumed. */
2048 procfs_target::fetch_registers (struct regcache
*regcache
, int regnum
)
2050 gdb_gregset_t
*gregs
;
2052 ptid_t ptid
= regcache
->ptid ();
2053 int pid
= ptid
.pid ();
2054 int tid
= ptid
.lwp ();
2055 struct gdbarch
*gdbarch
= regcache
->arch ();
2057 pi
= find_procinfo_or_die (pid
, tid
);
2060 error (_("procfs: fetch_registers failed to find procinfo for %s"),
2061 target_pid_to_str (ptid
));
2063 gregs
= proc_get_gregs (pi
);
2065 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
2067 supply_gregset (regcache
, (const gdb_gregset_t
*) gregs
);
2069 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
2071 gdb_fpregset_t
*fpregs
;
2073 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
2074 || regnum
== gdbarch_pc_regnum (gdbarch
)
2075 || regnum
== gdbarch_sp_regnum (gdbarch
))
2076 return; /* Not a floating point register. */
2078 fpregs
= proc_get_fpregs (pi
);
2080 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
2082 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregs
);
2086 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
2087 this for all registers.
2089 NOTE: Since the /proc interface will not read individual registers,
2090 we will cache these requests until the process is resumed, and only
2091 then write them back to the inferior process.
2093 FIXME: is that a really bad idea? Have to think about cases where
2094 writing one register might affect the value of others, etc. */
2097 procfs_target::store_registers (struct regcache
*regcache
, int regnum
)
2099 gdb_gregset_t
*gregs
;
2101 ptid_t ptid
= regcache
->ptid ();
2102 int pid
= ptid
.pid ();
2103 int tid
= ptid
.lwp ();
2104 struct gdbarch
*gdbarch
= regcache
->arch ();
2106 pi
= find_procinfo_or_die (pid
, tid
);
2109 error (_("procfs: store_registers: failed to find procinfo for %s"),
2110 target_pid_to_str (ptid
));
2112 gregs
= proc_get_gregs (pi
);
2114 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
2116 fill_gregset (regcache
, gregs
, regnum
);
2117 if (!proc_set_gregs (pi
))
2118 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
2120 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
2122 gdb_fpregset_t
*fpregs
;
2124 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
2125 || regnum
== gdbarch_pc_regnum (gdbarch
)
2126 || regnum
== gdbarch_sp_regnum (gdbarch
))
2127 return; /* Not a floating point register. */
2129 fpregs
= proc_get_fpregs (pi
);
2131 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
2133 fill_fpregset (regcache
, fpregs
, regnum
);
2134 if (!proc_set_fpregs (pi
))
2135 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
2140 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
2142 if (scall
== SYS_lwp_exit
)
2148 syscall_is_exit (procinfo
*pi
, int scall
)
2150 if (scall
== SYS_exit
)
2156 syscall_is_exec (procinfo
*pi
, int scall
)
2159 if (scall
== SYS_exec
)
2162 if (scall
== SYS_execve
)
2168 syscall_is_lwp_create (procinfo
*pi
, int scall
)
2170 if (scall
== SYS_lwp_create
)
2175 /* Retrieve the next stop event from the child process. If child has
2176 not stopped yet, wait for it to stop. Translate /proc eventcodes
2177 (or possibly wait eventcodes) into gdb internal event codes.
2178 Returns the id of process (and possibly thread) that incurred the
2179 event. Event codes are returned through a pointer parameter. */
2182 procfs_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
2185 /* First cut: loosely based on original version 2.1. */
2189 ptid_t retval
, temp_ptid
;
2190 int why
, what
, flags
;
2197 retval
= ptid_t (-1);
2199 /* Find procinfo for main process. */
2200 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2203 /* We must assume that the status is stale now... */
2204 pi
->status_valid
= 0;
2205 pi
->gregs_valid
= 0;
2206 pi
->fpregs_valid
= 0;
2208 #if 0 /* just try this out... */
2209 flags
= proc_flags (pi
);
2210 why
= proc_why (pi
);
2211 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
2212 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
2214 /* If child is not stopped, wait for it to stop. */
2215 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
2216 && !proc_wait_for_stop (pi
))
2218 /* wait_for_stop failed: has the child terminated? */
2219 if (errno
== ENOENT
)
2223 /* /proc file not found; presumably child has terminated. */
2224 wait_retval
= ::wait (&wstat
); /* "wait" for the child's exit. */
2227 if (wait_retval
!= inferior_ptid
.pid ())
2228 error (_("procfs: couldn't stop "
2229 "process %d: wait returned %d."),
2230 inferior_ptid
.pid (), wait_retval
);
2231 /* FIXME: might I not just use waitpid?
2232 Or try find_procinfo to see if I know about this child? */
2233 retval
= ptid_t (wait_retval
);
2235 else if (errno
== EINTR
)
2239 /* Unknown error from wait_for_stop. */
2240 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
2245 /* This long block is reached if either:
2246 a) the child was already stopped, or
2247 b) we successfully waited for the child with wait_for_stop.
2248 This block will analyze the /proc status, and translate it
2249 into a waitstatus for GDB.
2251 If we actually had to call wait because the /proc file
2252 is gone (child terminated), then we skip this block,
2253 because we already have a waitstatus. */
2255 flags
= proc_flags (pi
);
2256 why
= proc_why (pi
);
2257 what
= proc_what (pi
);
2259 if (flags
& (PR_STOPPED
| PR_ISTOP
))
2261 /* If it's running async (for single_thread control),
2262 set it back to normal again. */
2263 if (flags
& PR_ASYNC
)
2264 if (!proc_unset_async (pi
))
2265 proc_error (pi
, "target_wait, unset_async", __LINE__
);
2268 proc_prettyprint_why (why
, what
, 1);
2270 /* The 'pid' we will return to GDB is composed of
2271 the process ID plus the lwp ID. */
2272 retval
= ptid_t (pi
->pid
, proc_get_current_thread (pi
), 0);
2276 wstat
= (what
<< 8) | 0177;
2279 if (syscall_is_lwp_exit (pi
, what
))
2281 if (print_thread_events
)
2282 printf_unfiltered (_("[%s exited]\n"),
2283 target_pid_to_str (retval
));
2284 delete_thread (find_thread_ptid (retval
));
2285 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2288 else if (syscall_is_exit (pi
, what
))
2290 struct inferior
*inf
;
2292 /* Handle SYS_exit call only. */
2293 /* Stopped at entry to SYS_exit.
2294 Make it runnable, resume it, then use
2295 the wait system call to get its exit code.
2296 Proc_run_process always clears the current
2298 Then return its exit status. */
2299 pi
->status_valid
= 0;
2301 /* FIXME: what we should do is return
2302 TARGET_WAITKIND_SPURIOUS. */
2303 if (!proc_run_process (pi
, 0, 0))
2304 proc_error (pi
, "target_wait, run_process", __LINE__
);
2306 inf
= find_inferior_pid (pi
->pid
);
2307 if (inf
->attach_flag
)
2309 /* Don't call wait: simulate waiting for exit,
2310 return a "success" exit code. Bogus: what if
2311 it returns something else? */
2313 retval
= inferior_ptid
; /* ? ? ? */
2317 int temp
= ::wait (&wstat
);
2319 /* FIXME: shouldn't I make sure I get the right
2320 event from the right process? If (for
2321 instance) I have killed an earlier inferior
2322 process but failed to clean up after it
2323 somehow, I could get its termination event
2326 /* If wait returns -1, that's what we return
2329 retval
= ptid_t (temp
);
2334 printf_filtered (_("procfs: trapped on entry to "));
2335 proc_prettyprint_syscall (proc_what (pi
), 0);
2336 printf_filtered ("\n");
2338 long i
, nsysargs
, *sysargs
;
2340 nsysargs
= proc_nsysarg (pi
);
2341 sysargs
= proc_sysargs (pi
);
2343 if (nsysargs
> 0 && sysargs
!= NULL
)
2345 printf_filtered (_("%ld syscall arguments:\n"),
2347 for (i
= 0; i
< nsysargs
; i
++)
2348 printf_filtered ("#%ld: 0x%08lx\n",
2354 /* How to exit gracefully, returning "unknown
2356 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2357 return inferior_ptid
;
2361 /* How to keep going without returning to wfi: */
2362 target_continue_no_signal (ptid
);
2368 if (syscall_is_exec (pi
, what
))
2370 /* Hopefully this is our own "fork-child" execing
2371 the real child. Hoax this event into a trap, and
2372 GDB will see the child about to execute its start
2374 wstat
= (SIGTRAP
<< 8) | 0177;
2376 else if (syscall_is_lwp_create (pi
, what
))
2378 /* This syscall is somewhat like fork/exec. We
2379 will get the event twice: once for the parent
2380 LWP, and once for the child. We should already
2381 know about the parent LWP, but the child will
2382 be new to us. So, whenever we get this event,
2383 if it represents a new thread, simply add the
2384 thread to the list. */
2386 /* If not in procinfo list, add it. */
2387 temp_tid
= proc_get_current_thread (pi
);
2388 if (!find_procinfo (pi
->pid
, temp_tid
))
2389 create_procinfo (pi
->pid
, temp_tid
);
2391 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2392 /* If not in GDB's thread list, add it. */
2393 if (!in_thread_list (temp_ptid
))
2394 add_thread (temp_ptid
);
2396 /* Return to WFI, but tell it to immediately resume. */
2397 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2398 return inferior_ptid
;
2400 else if (syscall_is_lwp_exit (pi
, what
))
2402 if (print_thread_events
)
2403 printf_unfiltered (_("[%s exited]\n"),
2404 target_pid_to_str (retval
));
2405 delete_thread (find_thread_ptid (retval
));
2406 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2411 /* FIXME: Do we need to handle SYS_sproc,
2412 SYS_fork, or SYS_vfork here? The old procfs
2413 seemed to use this event to handle threads on
2414 older (non-LWP) systems, where I'm assuming
2415 that threads were actually separate processes.
2416 Irix, maybe? Anyway, low priority for now. */
2420 printf_filtered (_("procfs: trapped on exit from "));
2421 proc_prettyprint_syscall (proc_what (pi
), 0);
2422 printf_filtered ("\n");
2424 long i
, nsysargs
, *sysargs
;
2426 nsysargs
= proc_nsysarg (pi
);
2427 sysargs
= proc_sysargs (pi
);
2429 if (nsysargs
> 0 && sysargs
!= NULL
)
2431 printf_filtered (_("%ld syscall arguments:\n"),
2433 for (i
= 0; i
< nsysargs
; i
++)
2434 printf_filtered ("#%ld: 0x%08lx\n",
2438 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2439 return inferior_ptid
;
2444 wstat
= (SIGSTOP
<< 8) | 0177;
2449 printf_filtered (_("Retry #%d:\n"), retry
);
2450 pi
->status_valid
= 0;
2455 /* If not in procinfo list, add it. */
2456 temp_tid
= proc_get_current_thread (pi
);
2457 if (!find_procinfo (pi
->pid
, temp_tid
))
2458 create_procinfo (pi
->pid
, temp_tid
);
2460 /* If not in GDB's thread list, add it. */
2461 temp_ptid
= ptid_t (pi
->pid
, temp_tid
, 0);
2462 if (!in_thread_list (temp_ptid
))
2463 add_thread (temp_ptid
);
2465 status
->kind
= TARGET_WAITKIND_STOPPED
;
2466 status
->value
.sig
= GDB_SIGNAL_0
;
2471 wstat
= (what
<< 8) | 0177;
2476 wstat
= (SIGTRAP
<< 8) | 0177;
2478 /* FIXME: use si_signo where possible. */
2481 wstat
= (SIGILL
<< 8) | 0177;
2485 wstat
= (SIGTRAP
<< 8) | 0177;
2490 wstat
= (SIGSEGV
<< 8) | 0177;
2495 wstat
= (SIGFPE
<< 8) | 0177;
2497 case FLTPAGE
: /* Recoverable page fault */
2498 default: /* FIXME: use si_signo if possible for
2500 retval
= ptid_t (-1);
2501 printf_filtered ("procfs:%d -- ", __LINE__
);
2502 printf_filtered (_("child stopped for unknown reason:\n"));
2503 proc_prettyprint_why (why
, what
, 1);
2504 error (_("... giving up..."));
2507 break; /* case PR_FAULTED: */
2508 default: /* switch (why) unmatched */
2509 printf_filtered ("procfs:%d -- ", __LINE__
);
2510 printf_filtered (_("child stopped for unknown reason:\n"));
2511 proc_prettyprint_why (why
, what
, 1);
2512 error (_("... giving up..."));
2515 /* Got this far without error: If retval isn't in the
2516 threads database, add it. */
2517 if (retval
.pid () > 0
2518 && retval
!= inferior_ptid
2519 && !in_thread_list (retval
))
2521 /* We have a new thread. We need to add it both to
2522 GDB's list and to our own. If we don't create a
2523 procinfo, resume may be unhappy later. */
2524 add_thread (retval
);
2525 if (find_procinfo (retval
.pid (),
2526 retval
.lwp ()) == NULL
)
2527 create_procinfo (retval
.pid (),
2531 else /* Flags do not indicate STOPPED. */
2533 /* surely this can't happen... */
2534 printf_filtered ("procfs:%d -- process not stopped.\n",
2536 proc_prettyprint_flags (flags
, 1);
2537 error (_("procfs: ...giving up..."));
2542 store_waitstatus (status
, wstat
);
2548 /* Perform a partial transfer to/from the specified object. For
2549 memory transfers, fall back to the old memory xfer functions. */
2551 enum target_xfer_status
2552 procfs_target::xfer_partial (enum target_object object
,
2553 const char *annex
, gdb_byte
*readbuf
,
2554 const gdb_byte
*writebuf
, ULONGEST offset
,
2555 ULONGEST len
, ULONGEST
*xfered_len
)
2559 case TARGET_OBJECT_MEMORY
:
2560 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
2562 case TARGET_OBJECT_AUXV
:
2563 return memory_xfer_auxv (this, object
, annex
, readbuf
, writebuf
,
2564 offset
, len
, xfered_len
);
2567 return this->beneath ()->xfer_partial (object
, annex
,
2568 readbuf
, writebuf
, offset
, len
,
2573 /* Helper for procfs_xfer_partial that handles memory transfers.
2574 Arguments are like target_xfer_partial. */
2576 static enum target_xfer_status
2577 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
2578 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
2583 /* Find procinfo for main process. */
2584 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2585 if (pi
->as_fd
== 0 && open_procinfo_files (pi
, FD_AS
) == 0)
2587 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
2588 return TARGET_XFER_E_IO
;
2591 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
2592 return TARGET_XFER_E_IO
;
2594 if (writebuf
!= NULL
)
2596 PROCFS_NOTE ("write memory:\n");
2597 nbytes
= write (pi
->as_fd
, writebuf
, len
);
2601 PROCFS_NOTE ("read memory:\n");
2602 nbytes
= read (pi
->as_fd
, readbuf
, len
);
2605 return TARGET_XFER_E_IO
;
2606 *xfered_len
= nbytes
;
2607 return TARGET_XFER_OK
;
2610 /* Called by target_resume before making child runnable. Mark cached
2611 registers and status's invalid. If there are "dirty" caches that
2612 need to be written back to the child process, do that.
2614 File descriptors are also cached. As they are a limited resource,
2615 we cannot hold onto them indefinitely. However, as they are
2616 expensive to open, we don't want to throw them away
2617 indescriminately either. As a compromise, we will keep the file
2618 descriptors for the parent process, but discard any file
2619 descriptors we may have accumulated for the threads.
2621 As this function is called by iterate_over_threads, it always
2622 returns zero (so that iterate_over_threads will keep
2626 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
2628 /* About to run the child; invalidate caches and do any other
2632 if (pi
->gregs_dirty
)
2633 if (parent
== NULL
|| proc_get_current_thread (parent
) != pi
->tid
)
2634 if (!proc_set_gregs (pi
)) /* flush gregs cache */
2635 proc_warn (pi
, "target_resume, set_gregs",
2637 if (gdbarch_fp0_regnum (target_gdbarch ()) >= 0)
2638 if (pi
->fpregs_dirty
)
2639 if (parent
== NULL
|| proc_get_current_thread (parent
) != pi
->tid
)
2640 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
2641 proc_warn (pi
, "target_resume, set_fpregs",
2647 /* The presence of a parent indicates that this is an LWP.
2648 Close any file descriptors that it might have open.
2649 We don't do this to the master (parent) procinfo. */
2651 close_procinfo_files (pi
);
2653 pi
->gregs_valid
= 0;
2654 pi
->fpregs_valid
= 0;
2656 pi
->gregs_dirty
= 0;
2657 pi
->fpregs_dirty
= 0;
2659 pi
->status_valid
= 0;
2660 pi
->threads_valid
= 0;
2666 /* A callback function for iterate_over_threads. Find the
2667 asynchronous signal thread, and make it runnable. See if that
2668 helps matters any. */
2671 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
2674 if (proc_flags (pi
) & PR_ASLWP
)
2676 if (!proc_run_process (pi
, 0, -1))
2677 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
2685 /* Make the child process runnable. Normally we will then call
2686 procfs_wait and wait for it to stop again (unless gdb is async).
2688 If STEP is true, then arrange for the child to stop again after
2689 executing a single instruction. If SIGNO is zero, then cancel any
2690 pending signal; if non-zero, then arrange for the indicated signal
2691 to be delivered to the child when it runs. If PID is -1, then
2692 allow any child thread to run; if non-zero, then allow only the
2693 indicated thread to run. (not implemented yet). */
2696 procfs_target::resume (ptid_t ptid
, int step
, enum gdb_signal signo
)
2698 procinfo
*pi
, *thread
;
2702 prrun.prflags |= PRSVADDR;
2703 prrun.pr_vaddr = $PC; set resume address
2704 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
2705 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
2706 prrun.prflags |= PRCFAULT; clear current fault.
2708 PRSTRACE and PRSFAULT can be done by other means
2709 (proc_trace_signals, proc_trace_faults)
2710 PRSVADDR is unnecessary.
2711 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
2712 This basically leaves PRSTEP and PRCSIG.
2713 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
2714 So basically PR_STEP is the sole argument that must be passed
2715 to proc_run_process (for use in the prrun struct by ioctl). */
2717 /* Find procinfo for main process. */
2718 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2720 /* First cut: ignore pid argument. */
2723 /* Convert signal to host numbering. */
2724 if (signo
== 0 || (signo
== GDB_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
2727 native_signo
= gdb_signal_to_host (signo
);
2729 pi
->ignore_next_sigstop
= 0;
2731 /* Running the process voids all cached registers and status. */
2732 /* Void the threads' caches first. */
2733 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
2734 /* Void the process procinfo's caches. */
2735 invalidate_cache (NULL
, pi
, NULL
);
2737 if (ptid
.pid () != -1)
2739 /* Resume a specific thread, presumably suppressing the
2741 thread
= find_procinfo (ptid
.pid (), ptid
.lwp ());
2744 if (thread
->tid
!= 0)
2746 /* We're to resume a specific thread, and not the
2747 others. Set the child process's PR_ASYNC flag. */
2748 if (!proc_set_async (pi
))
2749 proc_error (pi
, "target_resume, set_async", __LINE__
);
2751 proc_iterate_over_threads (pi
,
2752 make_signal_thread_runnable
,
2755 pi
= thread
; /* Substitute the thread's procinfo
2761 if (!proc_run_process (pi
, step
, native_signo
))
2764 warning (_("resume: target already running. "
2765 "Pretend to resume, and hope for the best!"));
2767 proc_error (pi
, "target_resume", __LINE__
);
2771 /* Set up to trace signals in the child process. */
2774 procfs_target::pass_signals (int numsigs
, unsigned char *pass_signals
)
2777 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
2780 prfillset (&signals
);
2782 for (signo
= 0; signo
< NSIG
; signo
++)
2784 int target_signo
= gdb_signal_from_host (signo
);
2785 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
2786 prdelset (&signals
, signo
);
2789 if (!proc_set_traced_signals (pi
, &signals
))
2790 proc_error (pi
, "pass_signals", __LINE__
);
2793 /* Print status information about the child process. */
2796 procfs_target::files_info ()
2798 struct inferior
*inf
= current_inferior ();
2800 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
2801 inf
->attach_flag
? "attached": "child",
2802 target_pid_to_str (inferior_ptid
));
2805 /* Make it die. Wait for it to die. Clean up after it. Note: this
2806 should only be applied to the real process, not to an LWP, because
2807 of the check for parent-process. If we need this to work for an
2808 LWP, it needs some more logic. */
2811 unconditionally_kill_inferior (procinfo
*pi
)
2815 parent_pid
= proc_parent_pid (pi
);
2816 if (!proc_kill (pi
, SIGKILL
))
2817 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
2818 destroy_procinfo (pi
);
2820 /* If pi is GDB's child, wait for it to die. */
2821 if (parent_pid
== getpid ())
2822 /* FIXME: should we use waitpid to make sure we get the right event?
2823 Should we check the returned event? */
2828 ret
= waitpid (pi
->pid
, &status
, 0);
2835 /* We're done debugging it, and we want it to go away. Then we want
2836 GDB to forget all about it. */
2839 procfs_target::kill ()
2841 if (inferior_ptid
!= null_ptid
) /* ? */
2843 /* Find procinfo for main process. */
2844 procinfo
*pi
= find_procinfo (inferior_ptid
.pid (), 0);
2847 unconditionally_kill_inferior (pi
);
2848 target_mourn_inferior (inferior_ptid
);
2852 /* Forget we ever debugged this thing! */
2855 procfs_target::mourn_inferior ()
2859 if (inferior_ptid
!= null_ptid
)
2861 /* Find procinfo for main process. */
2862 pi
= find_procinfo (inferior_ptid
.pid (), 0);
2864 destroy_procinfo (pi
);
2867 generic_mourn_inferior ();
2869 maybe_unpush_target ();
2872 /* When GDB forks to create a runnable inferior process, this function
2873 is called on the parent side of the fork. It's job is to do
2874 whatever is necessary to make the child ready to be debugged, and
2875 then wait for the child to synchronize. */
2878 procfs_init_inferior (struct target_ops
*ops
, int pid
)
2884 /* This routine called on the parent side (GDB side)
2885 after GDB forks the inferior. */
2886 if (!target_is_pushed (ops
))
2889 pi
= create_procinfo (pid
, 0);
2891 perror (_("procfs: out of memory in 'init_inferior'"));
2893 if (!open_procinfo_files (pi
, FD_CTL
))
2894 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
2898 open_procinfo_files // done
2901 procfs_notice_signals
2908 /* If not stopped yet, wait for it to stop. */
2909 if (!(proc_flags (pi
) & PR_STOPPED
) && !(proc_wait_for_stop (pi
)))
2910 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
2912 /* Save some of the /proc state to be restored if we detach. */
2913 /* FIXME: Why? In case another debugger was debugging it?
2914 We're it's parent, for Ghu's sake! */
2915 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
2916 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
2917 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
2918 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
2919 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
2920 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
2921 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
2922 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
2923 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
2924 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
2926 fail
= procfs_debug_inferior (pi
);
2928 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
2930 /* FIXME: logically, we should really be turning OFF run-on-last-close,
2931 and possibly even turning ON kill-on-last-close at this point. But
2932 I can't make that change without careful testing which I don't have
2933 time to do right now... */
2934 /* Turn on run-on-last-close flag so that the child
2935 will die if GDB goes away for some reason. */
2936 if (!proc_set_run_on_last_close (pi
))
2937 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
2939 /* We now have have access to the lwpid of the main thread/lwp. */
2940 lwpid
= proc_get_current_thread (pi
);
2942 /* Create a procinfo for the main lwp. */
2943 create_procinfo (pid
, lwpid
);
2945 /* We already have a main thread registered in the thread table at
2946 this point, but it didn't have any lwp info yet. Notify the core
2947 about it. This changes inferior_ptid as well. */
2948 thread_change_ptid (ptid_t (pid
),
2949 ptid_t (pid
, lwpid
, 0));
2951 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
2954 /* When GDB forks to create a new process, this function is called on
2955 the child side of the fork before GDB exec's the user program. Its
2956 job is to make the child minimally debuggable, so that the parent
2957 GDB process can connect to the child and take over. This function
2958 should do only the minimum to make that possible, and to
2959 synchronize with the parent process. The parent process should
2960 take care of the details. */
2963 procfs_set_exec_trap (void)
2965 /* This routine called on the child side (inferior side)
2966 after GDB forks the inferior. It must use only local variables,
2967 because it may be sharing data space with its parent. */
2972 pi
= create_procinfo (getpid (), 0);
2974 perror_with_name (_("procfs: create_procinfo failed in child."));
2976 if (open_procinfo_files (pi
, FD_CTL
) == 0)
2978 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
2979 gdb_flush (gdb_stderr
);
2980 /* No need to call "dead_procinfo", because we're going to
2985 /* Method for tracing exec syscalls. */
2987 Not all systems with /proc have all the exec* syscalls with the same
2988 names. On the SGI, for example, there is no SYS_exec, but there
2989 *is* a SYS_execv. So, we try to account for that. */
2991 exitset
= XNEW (sysset_t
);
2992 premptyset (exitset
);
2994 praddset (exitset
, SYS_exec
);
2996 praddset (exitset
, SYS_execve
);
2998 if (!proc_set_traced_sysexit (pi
, exitset
))
3000 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
3001 gdb_flush (gdb_stderr
);
3005 /* FIXME: should this be done in the parent instead? */
3006 /* Turn off inherit on fork flag so that all grand-children
3007 of gdb start with tracing flags cleared. */
3008 if (!proc_unset_inherit_on_fork (pi
))
3009 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
3011 /* Turn off run on last close flag, so that the child process
3012 cannot run away just because we close our handle on it.
3013 We want it to wait for the parent to attach. */
3014 if (!proc_unset_run_on_last_close (pi
))
3015 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
3017 /* FIXME: No need to destroy the procinfo --
3018 we have our own address space, and we're about to do an exec! */
3019 /*destroy_procinfo (pi);*/
3022 /* This function is called BEFORE gdb forks the inferior process. Its
3023 only real responsibility is to set things up for the fork, and tell
3024 GDB which two functions to call after the fork (one for the parent,
3025 and one for the child).
3027 This function does a complicated search for a unix shell program,
3028 which it then uses to parse arguments and environment variables to
3029 be sent to the child. I wonder whether this code could not be
3030 abstracted out and shared with other unix targets such as
3034 procfs_target::create_inferior (const char *exec_file
,
3035 const std::string
&allargs
,
3036 char **env
, int from_tty
)
3038 char *shell_file
= getenv ("SHELL");
3042 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
3045 /* We will be looking down the PATH to find shell_file. If we
3046 just do this the normal way (via execlp, which operates by
3047 attempting an exec for each element of the PATH until it
3048 finds one which succeeds), then there will be an exec for
3049 each failed attempt, each of which will cause a PR_SYSEXIT
3050 stop, and we won't know how to distinguish the PR_SYSEXIT's
3051 for these failed execs with the ones for successful execs
3052 (whether the exec has succeeded is stored at that time in the
3053 carry bit or some such architecture-specific and
3054 non-ABI-specified place).
3056 So I can't think of anything better than to search the PATH
3057 now. This has several disadvantages: (1) There is a race
3058 condition; if we find a file now and it is deleted before we
3059 exec it, we lose, even if the deletion leaves a valid file
3060 further down in the PATH, (2) there is no way to know exactly
3061 what an executable (in the sense of "capable of being
3062 exec'd") file is. Using access() loses because it may lose
3063 if the caller is the superuser; failing to use it loses if
3064 there are ACLs or some such. */
3068 /* FIXME-maybe: might want "set path" command so user can change what
3069 path is used from within GDB. */
3070 const char *path
= getenv ("PATH");
3072 struct stat statbuf
;
3075 path
= "/bin:/usr/bin";
3077 tryname
= (char *) alloca (strlen (path
) + strlen (shell_file
) + 2);
3078 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
3080 p1
= strchr (p
, ':');
3085 strncpy (tryname
, p
, len
);
3086 tryname
[len
] = '\0';
3087 strcat (tryname
, "/");
3088 strcat (tryname
, shell_file
);
3089 if (access (tryname
, X_OK
) < 0)
3091 if (stat (tryname
, &statbuf
) < 0)
3093 if (!S_ISREG (statbuf
.st_mode
))
3094 /* We certainly need to reject directories. I'm not quite
3095 as sure about FIFOs, sockets, etc., but I kind of doubt
3096 that people want to exec() these things. */
3101 /* Not found. This must be an error rather than merely passing
3102 the file to execlp(), because execlp() would try all the
3103 exec()s, causing GDB to get confused. */
3104 error (_("procfs:%d -- Can't find shell %s in PATH"),
3105 __LINE__
, shell_file
);
3107 shell_file
= tryname
;
3110 pid
= fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
3111 NULL
, NULL
, shell_file
, NULL
);
3113 /* We have something that executes now. We'll be running through
3114 the shell at this point (if startup-with-shell is true), but the
3115 pid shouldn't change. */
3116 add_thread_silent (ptid_t (pid
));
3118 procfs_init_inferior (this, pid
);
3121 /* An observer for the "inferior_created" event. */
3124 procfs_inferior_created (struct target_ops
*ops
, int from_tty
)
3128 /* Callback for update_thread_list. Calls "add_thread". */
3131 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
3133 ptid_t gdb_threadid
= ptid_t (pi
->pid
, thread
->tid
, 0);
3135 if (!in_thread_list (gdb_threadid
) || is_exited (gdb_threadid
))
3136 add_thread (gdb_threadid
);
3141 /* Query all the threads that the target knows about, and give them
3142 back to GDB to add to its list. */
3145 procfs_target::update_thread_list ()
3151 /* Find procinfo for main process. */
3152 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3153 proc_update_threads (pi
);
3154 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
3157 /* Return true if the thread is still 'alive'. This guy doesn't
3158 really seem to be doing his job. Got to investigate how to tell
3159 when a thread is really gone. */
3162 procfs_target::thread_alive (ptid_t ptid
)
3168 thread
= ptid
.lwp ();
3169 /* If I don't know it, it ain't alive! */
3170 pi
= find_procinfo (proc
, thread
);
3174 /* If I can't get its status, it ain't alive!
3175 What's more, I need to forget about it! */
3176 if (!proc_get_status (pi
))
3178 destroy_procinfo (pi
);
3181 /* I couldn't have got its status if it weren't alive, so it's
3186 /* Convert PTID to a string. Returns the string in a static
3190 procfs_target::pid_to_str (ptid_t ptid
)
3192 static char buf
[80];
3194 if (ptid
.lwp () == 0)
3195 xsnprintf (buf
, sizeof (buf
), "process %d", ptid
.pid ());
3197 xsnprintf (buf
, sizeof (buf
), "LWP %ld", ptid
.lwp ());
3202 /* Accepts an integer PID; Returns a string representing a file that
3203 can be opened to get the symbols for the child process. */
3206 procfs_target::pid_to_exec_file (int pid
)
3208 static char buf
[PATH_MAX
];
3209 char name
[PATH_MAX
];
3211 /* Solaris 11 introduced /proc/<proc-id>/execname. */
3212 xsnprintf (name
, sizeof (name
), "/proc/%d/execname", pid
);
3213 scoped_fd
fd (gdb_open_cloexec (name
, O_RDONLY
, 0));
3214 if (fd
.get () < 0 || read (fd
.get (), buf
, PATH_MAX
- 1) < 0)
3216 /* If that fails, fall back to /proc/<proc-id>/path/a.out introduced in
3220 xsnprintf (name
, sizeof (name
), "/proc/%d/path/a.out", pid
);
3221 len
= readlink (name
, buf
, PATH_MAX
- 1);
3231 /* Insert a watchpoint. */
3234 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
3240 pi
= find_procinfo_or_die (ptid
.pid () == -1 ?
3241 inferior_ptid
.pid () : ptid
.pid (),
3244 /* Translate from GDB's flags to /proc's. */
3245 if (len
> 0) /* len == 0 means delete watchpoint. */
3247 switch (rwflag
) { /* FIXME: need an enum! */
3248 case hw_write
: /* default watchpoint (write) */
3249 pflags
= WRITE_WATCHFLAG
;
3251 case hw_read
: /* read watchpoint */
3252 pflags
= READ_WATCHFLAG
;
3254 case hw_access
: /* access watchpoint */
3255 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
3257 case hw_execute
: /* execution HW breakpoint */
3258 pflags
= EXEC_WATCHFLAG
;
3260 default: /* Something weird. Return error. */
3263 if (after
) /* Stop after r/w access is completed. */
3264 pflags
|= AFTER_WATCHFLAG
;
3267 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
3269 if (errno
== E2BIG
) /* Typical error for no resources. */
3270 return -1; /* fail */
3271 /* GDB may try to remove the same watchpoint twice.
3272 If a remove request returns no match, don't error. */
3273 if (errno
== ESRCH
&& len
== 0)
3274 return 0; /* ignore */
3275 proc_error (pi
, "set_watchpoint", __LINE__
);
3280 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
3281 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
3282 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
3285 Note: procfs_can_use_hw_breakpoint() is not yet used by all
3286 procfs.c targets due to the fact that some of them still define
3287 target_can_use_hardware_watchpoint. */
3290 procfs_target::can_use_hw_breakpoint (enum bptype type
, int cnt
, int othertype
)
3292 /* Due to the way that proc_set_watchpoint() is implemented, host
3293 and target pointers must be of the same size. If they are not,
3294 we can't use hardware watchpoints. This limitation is due to the
3295 fact that proc_set_watchpoint() calls
3296 procfs_address_to_host_pointer(); a close inspection of
3297 procfs_address_to_host_pointer will reveal that an internal error
3298 will be generated when the host and target pointer sizes are
3300 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
3302 if (sizeof (void *) != TYPE_LENGTH (ptr_type
))
3305 /* Other tests here??? */
3310 /* Returns non-zero if process is stopped on a hardware watchpoint
3311 fault, else returns zero. */
3314 procfs_target::stopped_by_watchpoint ()
3318 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3320 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3321 if (proc_why (pi
) == PR_FAULTED
)
3322 if (proc_what (pi
) == FLTWATCH
)
3327 /* Returns 1 if the OS knows the position of the triggered watchpoint,
3328 and sets *ADDR to that address. Returns 0 if OS cannot report that
3329 address. This function is only called if
3330 procfs_stopped_by_watchpoint returned 1, thus no further checks are
3331 done. The function also assumes that ADDR is not NULL. */
3334 procfs_target::stopped_data_address (CORE_ADDR
*addr
)
3338 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3339 return proc_watchpoint_address (pi
, addr
);
3343 procfs_target::insert_watchpoint (CORE_ADDR addr
, int len
,
3344 enum target_hw_bp_type type
,
3345 struct expression
*cond
)
3347 if (!target_have_steppable_watchpoint
3348 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
3349 /* When a hardware watchpoint fires off the PC will be left at
3350 the instruction following the one which caused the
3351 watchpoint. It will *NOT* be necessary for GDB to step over
3353 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 1);
3355 /* When a hardware watchpoint fires off the PC will be left at
3356 the instruction which caused the watchpoint. It will be
3357 necessary for GDB to step over the watchpoint. */
3358 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 0);
3362 procfs_target::remove_watchpoint (CORE_ADDR addr
, int len
,
3363 enum target_hw_bp_type type
,
3364 struct expression
*cond
)
3366 return procfs_set_watchpoint (inferior_ptid
, addr
, 0, 0, 0);
3370 procfs_target::region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
3372 /* The man page for proc(4) on Solaris 2.6 and up says that the
3373 system can support "thousands" of hardware watchpoints, but gives
3374 no method for finding out how many; It doesn't say anything about
3375 the allowed size for the watched area either. So we just tell
3380 /* Memory Mappings Functions: */
3382 /* Call a callback function once for each mapping, passing it the
3383 mapping, an optional secondary callback function, and some optional
3384 opaque data. Quit and return the first non-zero value returned
3387 PI is the procinfo struct for the process to be mapped. FUNC is
3388 the callback function to be called by this iterator. DATA is the
3389 optional opaque data to be passed to the callback function.
3390 CHILD_FUNC is the optional secondary function pointer to be passed
3391 to the child function. Returns the first non-zero return value
3392 from the callback function, or zero. */
3395 iterate_over_mappings (procinfo
*pi
, find_memory_region_ftype child_func
,
3397 int (*func
) (struct prmap
*map
,
3398 find_memory_region_ftype child_func
,
3401 char pathname
[MAX_PROC_NAME_SIZE
];
3402 struct prmap
*prmaps
;
3403 struct prmap
*prmap
;
3408 /* Get the number of mappings, allocate space,
3409 and read the mappings into prmaps. */
3411 xsnprintf (pathname
, sizeof (pathname
), "/proc/%d/map", pi
->pid
);
3413 scoped_fd
map_fd (open (pathname
, O_RDONLY
));
3414 if (map_fd
.get () < 0)
3415 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
3417 /* Use stat to determine the file size, and compute
3418 the number of prmap_t objects it contains. */
3419 if (fstat (map_fd
.get (), &sbuf
) != 0)
3420 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
3422 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
3423 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
3424 if (read (map_fd
.get (), (char *) prmaps
, nmap
* sizeof (*prmaps
))
3425 != (nmap
* sizeof (*prmaps
)))
3426 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
3428 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
3430 funcstat
= (*func
) (prmap
, child_func
, data
);
3438 /* Implements the to_find_memory_regions method. Calls an external
3439 function for each memory region.
3440 Returns the integer value returned by the callback. */
3443 find_memory_regions_callback (struct prmap
*map
,
3444 find_memory_region_ftype func
, void *data
)
3446 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
3448 (map
->pr_mflags
& MA_READ
) != 0,
3449 (map
->pr_mflags
& MA_WRITE
) != 0,
3450 (map
->pr_mflags
& MA_EXEC
) != 0,
3451 1, /* MODIFIED is unknown, pass it as true. */
3455 /* External interface. Calls a callback function once for each
3456 mapped memory region in the child process, passing as arguments:
3458 CORE_ADDR virtual_address,
3460 int read, TRUE if region is readable by the child
3461 int write, TRUE if region is writable by the child
3462 int execute TRUE if region is executable by the child.
3464 Stops iterating and returns the first non-zero value returned by
3468 procfs_target::find_memory_regions (find_memory_region_ftype func
, void *data
)
3470 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3472 return iterate_over_mappings (pi
, func
, data
,
3473 find_memory_regions_callback
);
3476 /* Returns an ascii representation of a memory mapping's flags. */
3479 mappingflags (long flags
)
3481 static char asciiflags
[8];
3483 strcpy (asciiflags
, "-------");
3484 if (flags
& MA_STACK
)
3485 asciiflags
[1] = 's';
3486 if (flags
& MA_BREAK
)
3487 asciiflags
[2] = 'b';
3488 if (flags
& MA_SHARED
)
3489 asciiflags
[3] = 's';
3490 if (flags
& MA_READ
)
3491 asciiflags
[4] = 'r';
3492 if (flags
& MA_WRITE
)
3493 asciiflags
[5] = 'w';
3494 if (flags
& MA_EXEC
)
3495 asciiflags
[6] = 'x';
3496 return (asciiflags
);
3499 /* Callback function, does the actual work for 'info proc
3503 info_mappings_callback (struct prmap
*map
, find_memory_region_ftype ignore
,
3506 unsigned int pr_off
;
3508 pr_off
= (unsigned int) map
->pr_offset
;
3510 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
3511 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
3512 (unsigned long) map
->pr_vaddr
,
3513 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3514 (unsigned long) map
->pr_size
,
3516 mappingflags (map
->pr_mflags
));
3518 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
3519 (unsigned long) map
->pr_vaddr
,
3520 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3521 (unsigned long) map
->pr_size
,
3523 mappingflags (map
->pr_mflags
));
3528 /* Implement the "info proc mappings" subcommand. */
3531 info_proc_mappings (procinfo
*pi
, int summary
)
3534 return; /* No output for summary mode. */
3536 printf_filtered (_("Mapped address spaces:\n\n"));
3537 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
3538 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3545 printf_filtered (" %18s %18s %10s %10s %7s\n",
3552 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
3553 printf_filtered ("\n");
3556 /* Implement the "info proc" command. */
3559 procfs_target::info_proc (const char *args
, enum info_proc_what what
)
3561 procinfo
*process
= NULL
;
3562 procinfo
*thread
= NULL
;
3579 error (_("Not supported on this target."));
3582 gdb_argv
built_argv (args
);
3583 for (char *arg
: built_argv
)
3585 if (isdigit (arg
[0]))
3587 pid
= strtoul (arg
, &tmp
, 10);
3589 tid
= strtoul (++tmp
, NULL
, 10);
3591 else if (arg
[0] == '/')
3593 tid
= strtoul (arg
+ 1, NULL
, 10);
3597 procinfo_up temporary_procinfo
;
3599 pid
= inferior_ptid
.pid ();
3601 error (_("No current process: you must name one."));
3604 /* Have pid, will travel.
3605 First see if it's a process we're already debugging. */
3606 process
= find_procinfo (pid
, 0);
3607 if (process
== NULL
)
3609 /* No. So open a procinfo for it, but
3610 remember to close it again when finished. */
3611 process
= create_procinfo (pid
, 0);
3612 temporary_procinfo
.reset (process
);
3613 if (!open_procinfo_files (process
, FD_CTL
))
3614 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
3618 thread
= create_procinfo (pid
, tid
);
3622 printf_filtered (_("process %d flags:\n"), process
->pid
);
3623 proc_prettyprint_flags (proc_flags (process
), 1);
3624 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
3625 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
3626 if (proc_get_nthreads (process
) > 1)
3627 printf_filtered ("Process has %d threads.\n",
3628 proc_get_nthreads (process
));
3632 printf_filtered (_("thread %d flags:\n"), thread
->tid
);
3633 proc_prettyprint_flags (proc_flags (thread
), 1);
3634 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
3635 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
3639 info_proc_mappings (process
, 0);
3644 /* Modify the status of the system call identified by SYSCALLNUM in
3645 the set of syscalls that are currently traced/debugged.
3647 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
3648 will be updated. Otherwise, the exit syscalls set will be updated.
3650 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
3651 will be disabled. */
3654 proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
, int entry_or_exit
,
3655 int mode
, int from_tty
)
3659 if (entry_or_exit
== PR_SYSENTRY
)
3660 sysset
= proc_get_traced_sysentry (pi
, NULL
);
3662 sysset
= proc_get_traced_sysexit (pi
, NULL
);
3665 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
3667 if (mode
== FLAG_SET
)
3668 praddset (sysset
, syscallnum
);
3670 prdelset (sysset
, syscallnum
);
3672 if (entry_or_exit
== PR_SYSENTRY
)
3674 if (!proc_set_traced_sysentry (pi
, sysset
))
3675 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
3679 if (!proc_set_traced_sysexit (pi
, sysset
))
3680 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
3685 proc_trace_syscalls (const char *args
, int from_tty
, int entry_or_exit
, int mode
)
3689 if (inferior_ptid
.pid () <= 0)
3690 error (_("you must be debugging a process to use this command."));
3692 if (args
== NULL
|| args
[0] == 0)
3693 error_no_arg (_("system call to trace"));
3695 pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3696 if (isdigit (args
[0]))
3698 const int syscallnum
= atoi (args
);
3700 proc_trace_syscalls_1 (pi
, syscallnum
, entry_or_exit
, mode
, from_tty
);
3705 proc_trace_sysentry_cmd (const char *args
, int from_tty
)
3707 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
3711 proc_trace_sysexit_cmd (const char *args
, int from_tty
)
3713 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
3717 proc_untrace_sysentry_cmd (const char *args
, int from_tty
)
3719 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
3723 proc_untrace_sysexit_cmd (const char *args
, int from_tty
)
3725 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
3729 _initialize_procfs (void)
3731 gdb::observers::inferior_created
.attach (procfs_inferior_created
);
3733 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
3734 _("Give a trace of entries into the syscall."));
3735 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
3736 _("Give a trace of exits from the syscall."));
3737 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
3738 _("Cancel a trace of entries into the syscall."));
3739 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
3740 _("Cancel a trace of exits from the syscall."));
3742 add_inf_child_target (&the_procfs_target
);
3745 /* =================== END, GDB "MODULE" =================== */
3749 /* miscellaneous stubs: */
3751 /* The following satisfy a few random symbols mostly created by the
3752 solaris threads implementation, which I will chase down later. */
3754 /* Return a pid for which we guarantee we will be able to find a
3758 procfs_first_available (void)
3760 return ptid_t (procinfo_list
? procinfo_list
->pid
: -1);
3763 /* =================== GCORE .NOTE "MODULE" =================== */
3766 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3767 char *note_data
, int *note_size
,
3768 enum gdb_signal stop_signal
)
3770 struct regcache
*regcache
= get_thread_regcache (ptid
);
3771 gdb_gregset_t gregs
;
3772 gdb_fpregset_t fpregs
;
3773 unsigned long merged_pid
;
3775 merged_pid
= ptid
.lwp () << 16 | ptid
.pid ();
3777 /* This part is the old method for fetching registers.
3778 It should be replaced by the newer one using regsets
3779 once it is implemented in this platform:
3780 gdbarch_iterate_over_regset_sections(). */
3782 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
3783 inferior_ptid
= ptid
;
3784 target_fetch_registers (regcache
, -1);
3786 fill_gregset (regcache
, &gregs
, -1);
3787 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
3793 fill_fpregset (regcache
, &fpregs
, -1);
3794 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3803 struct procfs_corefile_thread_data
{
3807 enum gdb_signal stop_signal
;
3811 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
3813 struct procfs_corefile_thread_data
*args
3814 = (struct procfs_corefile_thread_data
*) data
;
3818 ptid_t ptid
= ptid_t (pi
->pid
, thread
->tid
, 0);
3820 args
->note_data
= procfs_do_thread_registers (args
->obfd
, ptid
,
3829 find_signalled_thread (struct thread_info
*info
, void *data
)
3831 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
3832 && info
->ptid
.pid () == inferior_ptid
.pid ())
3838 static enum gdb_signal
3839 find_stop_signal (void)
3841 struct thread_info
*info
=
3842 iterate_over_threads (find_signalled_thread
, NULL
);
3845 return info
->suspend
.stop_signal
;
3847 return GDB_SIGNAL_0
;
3851 procfs_target::make_corefile_notes (bfd
*obfd
, int *note_size
)
3853 gdb_gregset_t gregs
;
3854 char fname
[16] = {'\0'};
3855 char psargs
[80] = {'\0'};
3856 procinfo
*pi
= find_procinfo_or_die (inferior_ptid
.pid (), 0);
3857 char *note_data
= NULL
;
3858 const char *inf_args
;
3859 struct procfs_corefile_thread_data thread_args
;
3860 enum gdb_signal stop_signal
;
3862 if (get_exec_file (0))
3864 strncpy (fname
, lbasename (get_exec_file (0)), sizeof (fname
));
3865 fname
[sizeof (fname
) - 1] = 0;
3866 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3867 psargs
[sizeof (psargs
) - 1] = 0;
3869 inf_args
= get_inferior_args ();
3870 if (inf_args
&& *inf_args
3871 && (strlen (inf_args
)
3872 < ((int) sizeof (psargs
) - (int) strlen (psargs
))))
3874 strncat (psargs
, " ",
3875 sizeof (psargs
) - strlen (psargs
));
3876 strncat (psargs
, inf_args
,
3877 sizeof (psargs
) - strlen (psargs
));
3881 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3887 stop_signal
= find_stop_signal ();
3889 fill_gregset (get_current_regcache (), &gregs
, -1);
3890 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
3891 inferior_ptid
.pid (),
3892 stop_signal
, &gregs
);
3894 thread_args
.obfd
= obfd
;
3895 thread_args
.note_data
= note_data
;
3896 thread_args
.note_size
= note_size
;
3897 thread_args
.stop_signal
= stop_signal
;
3898 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
,
3900 note_data
= thread_args
.note_data
;
3902 gdb::optional
<gdb::byte_vector
> auxv
=
3903 target_read_alloc (current_top_target (), TARGET_OBJECT_AUXV
, NULL
);
3904 if (auxv
&& !auxv
->empty ())
3905 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3906 "CORE", NT_AUXV
, auxv
->data (),
3911 /* =================== END GCORE .NOTE "MODULE" =================== */