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. */
89 static void procfs_attach (struct target_ops
*, const char *, int);
90 static void procfs_detach (struct target_ops
*, inferior
*, int);
91 static void procfs_resume (struct target_ops
*,
92 ptid_t
, int, enum gdb_signal
);
93 static void procfs_files_info (struct target_ops
*);
94 static void procfs_fetch_registers (struct target_ops
*,
95 struct regcache
*, int);
96 static void procfs_store_registers (struct target_ops
*,
97 struct regcache
*, int);
98 static void procfs_pass_signals (struct target_ops
*self
,
99 int, unsigned char *);
100 static void procfs_kill_inferior (struct target_ops
*ops
);
101 static void procfs_mourn_inferior (struct target_ops
*ops
);
102 static void procfs_create_inferior (struct target_ops
*, const char *,
103 const std::string
&, char **, int);
104 static ptid_t
procfs_wait (struct target_ops
*,
105 ptid_t
, struct target_waitstatus
*, int);
106 static enum target_xfer_status
procfs_xfer_memory (gdb_byte
*,
110 static target_xfer_partial_ftype procfs_xfer_partial
;
112 static int procfs_thread_alive (struct target_ops
*ops
, ptid_t
);
114 static void procfs_update_thread_list (struct target_ops
*ops
);
115 static const char *procfs_pid_to_str (struct target_ops
*, ptid_t
);
117 static int proc_find_memory_regions (struct target_ops
*self
,
118 find_memory_region_ftype
, void *);
120 static char *procfs_make_note_section (struct target_ops
*self
,
123 static int procfs_can_use_hw_breakpoint (struct target_ops
*self
,
124 enum bptype
, int, int);
126 static void procfs_info_proc (struct target_ops
*, const char *,
127 enum info_proc_what
);
129 static int procfs_stopped_by_watchpoint (struct target_ops
*);
131 static int procfs_insert_watchpoint (struct target_ops
*,
133 enum target_hw_bp_type
,
134 struct expression
*);
136 static int procfs_remove_watchpoint (struct target_ops
*,
138 enum target_hw_bp_type
,
139 struct expression
*);
141 static int procfs_region_ok_for_hw_watchpoint (struct target_ops
*,
143 static int procfs_stopped_data_address (struct target_ops
*, CORE_ADDR
*);
145 #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
146 /* When GDB is built as 64-bit application on Solaris, the auxv data
147 is presented in 64-bit format. We need to provide a custom parser
150 procfs_auxv_parse (struct target_ops
*ops
, gdb_byte
**readptr
,
151 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
153 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
154 gdb_byte
*ptr
= *readptr
;
159 if (endptr
- ptr
< 8 * 2)
162 *typep
= extract_unsigned_integer (ptr
, 4, byte_order
);
164 /* The size of data is always 64-bit. If the application is 32-bit,
165 it will be zero extended, as expected. */
166 *valp
= extract_unsigned_integer (ptr
, 8, byte_order
);
177 struct target_ops
*t
= inf_child_target ();
179 t
->to_create_inferior
= procfs_create_inferior
;
180 t
->to_kill
= procfs_kill_inferior
;
181 t
->to_mourn_inferior
= procfs_mourn_inferior
;
182 t
->to_attach
= procfs_attach
;
183 t
->to_detach
= procfs_detach
;
184 t
->to_wait
= procfs_wait
;
185 t
->to_resume
= procfs_resume
;
186 t
->to_fetch_registers
= procfs_fetch_registers
;
187 t
->to_store_registers
= procfs_store_registers
;
188 t
->to_xfer_partial
= procfs_xfer_partial
;
189 t
->to_pass_signals
= procfs_pass_signals
;
190 t
->to_files_info
= procfs_files_info
;
192 t
->to_update_thread_list
= procfs_update_thread_list
;
193 t
->to_thread_alive
= procfs_thread_alive
;
194 t
->to_pid_to_str
= procfs_pid_to_str
;
196 t
->to_has_thread_control
= tc_schedlock
;
197 t
->to_find_memory_regions
= proc_find_memory_regions
;
198 t
->to_make_corefile_notes
= procfs_make_note_section
;
199 t
->to_info_proc
= procfs_info_proc
;
201 #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
202 t
->to_auxv_parse
= procfs_auxv_parse
;
205 t
->to_stopped_by_watchpoint
= procfs_stopped_by_watchpoint
;
206 t
->to_insert_watchpoint
= procfs_insert_watchpoint
;
207 t
->to_remove_watchpoint
= procfs_remove_watchpoint
;
208 t
->to_region_ok_for_hw_watchpoint
= procfs_region_ok_for_hw_watchpoint
;
209 t
->to_can_use_hw_breakpoint
= procfs_can_use_hw_breakpoint
;
210 t
->to_stopped_data_address
= procfs_stopped_data_address
;
212 t
->to_magic
= OPS_MAGIC
;
217 /* =================== END, TARGET_OPS "MODULE" =================== */
219 /* World Unification:
221 Put any typedefs, defines etc. here that are required for the
222 unification of code that handles different versions of /proc. */
224 enum { READ_WATCHFLAG
= WA_READ
,
225 WRITE_WATCHFLAG
= WA_WRITE
,
226 EXEC_WATCHFLAG
= WA_EXEC
,
227 AFTER_WATCHFLAG
= WA_TRAPAFTER
231 /* =================== STRUCT PROCINFO "MODULE" =================== */
233 /* FIXME: this comment will soon be out of date W.R.T. threads. */
235 /* The procinfo struct is a wrapper to hold all the state information
236 concerning a /proc process. There should be exactly one procinfo
237 for each process, and since GDB currently can debug only one
238 process at a time, that means there should be only one procinfo.
239 All of the LWP's of a process can be accessed indirectly thru the
240 single process procinfo.
242 However, against the day when GDB may debug more than one process,
243 this data structure is kept in a list (which for now will hold no
244 more than one member), and many functions will have a pointer to a
245 procinfo as an argument.
247 There will be a separate procinfo structure for use by the (not yet
248 implemented) "info proc" command, so that we can print useful
249 information about any random process without interfering with the
250 inferior's procinfo information. */
252 /* format strings for /proc paths */
253 #define MAIN_PROC_NAME_FMT "/proc/%d"
254 #define CTL_PROC_NAME_FMT "/proc/%d/ctl"
255 #define AS_PROC_NAME_FMT "/proc/%d/as"
256 #define MAP_PROC_NAME_FMT "/proc/%d/map"
257 #define STATUS_PROC_NAME_FMT "/proc/%d/status"
258 #define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
260 typedef struct procinfo
{
261 struct procinfo
*next
;
262 int pid
; /* Process ID */
263 int tid
; /* Thread/LWP id */
267 int ignore_next_sigstop
;
269 int ctl_fd
; /* File descriptor for /proc control file */
270 int status_fd
; /* File descriptor for /proc status file */
271 int as_fd
; /* File descriptor for /proc as file */
273 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
275 fltset_t saved_fltset
; /* Saved traced hardware fault set */
276 sigset_t saved_sigset
; /* Saved traced signal set */
277 sigset_t saved_sighold
; /* Saved held signal set */
278 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
279 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
281 pstatus_t prstatus
; /* Current process status info */
283 struct procinfo
*thread_list
;
285 int status_valid
: 1;
287 int fpregs_valid
: 1;
288 int threads_valid
: 1;
291 static char errmsg
[128]; /* shared error msg buffer */
293 /* Function prototypes for procinfo module: */
295 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
296 static procinfo
*find_procinfo (int pid
, int tid
);
297 static procinfo
*create_procinfo (int pid
, int tid
);
298 static void destroy_procinfo (procinfo
*p
);
299 static void do_destroy_procinfo_cleanup (void *);
300 static void dead_procinfo (procinfo
*p
, const char *msg
, int killp
);
301 static int open_procinfo_files (procinfo
*p
, int which
);
302 static void close_procinfo_files (procinfo
*p
);
303 static sysset_t
*sysset_t_alloc (procinfo
*pi
);
305 static int iterate_over_mappings
306 (procinfo
*pi
, find_memory_region_ftype child_func
, void *data
,
307 int (*func
) (struct prmap
*map
, find_memory_region_ftype child_func
,
310 /* The head of the procinfo list: */
311 static procinfo
*procinfo_list
;
313 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
317 find_procinfo (int pid
, int tid
)
321 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
328 /* Don't check threads_valid. If we're updating the
329 thread_list, we want to find whatever threads are already
330 here. This means that in general it is the caller's
331 responsibility to check threads_valid and update before
332 calling find_procinfo, if the caller wants to find a new
335 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
343 /* Calls find_procinfo, but errors on failure. */
346 find_procinfo_or_die (int pid
, int tid
)
348 procinfo
*pi
= find_procinfo (pid
, tid
);
353 error (_("procfs: couldn't find pid %d "
354 "(kernel thread %d) in procinfo list."),
357 error (_("procfs: couldn't find pid %d in procinfo list."), pid
);
362 /* Wrapper for `open'. The appropriate open call is attempted; if
363 unsuccessful, it will be retried as many times as needed for the
364 EAGAIN and EINTR conditions.
366 For other conditions, retry the open a limited number of times. In
367 addition, a short sleep is imposed prior to retrying the open. The
368 reason for this sleep is to give the kernel a chance to catch up
369 and create the file in question in the event that GDB "wins" the
370 race to open a file before the kernel has created it. */
373 open_with_retry (const char *pathname
, int flags
)
375 int retries_remaining
, status
;
377 retries_remaining
= 2;
381 status
= open (pathname
, flags
);
383 if (status
>= 0 || retries_remaining
== 0)
385 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
395 /* Open the file descriptor for the process or LWP. We only open the
396 control file descriptor; the others are opened lazily as needed.
397 Returns the file descriptor, or zero for failure. */
399 enum { FD_CTL
, FD_STATUS
, FD_AS
};
402 open_procinfo_files (procinfo
*pi
, int which
)
404 char tmp
[MAX_PROC_NAME_SIZE
];
407 /* This function is getting ALMOST long enough to break up into
408 several. Here is some rationale:
410 There are several file descriptors that may need to be open
411 for any given process or LWP. The ones we're intereted in are:
412 - control (ctl) write-only change the state
413 - status (status) read-only query the state
414 - address space (as) read/write access memory
415 - map (map) read-only virtual addr map
416 Most of these are opened lazily as they are needed.
417 The pathnames for the 'files' for an LWP look slightly
418 different from those of a first-class process:
419 Pathnames for a process (<proc-id>):
421 /proc/<proc-id>/status
424 Pathnames for an LWP (lwp-id):
425 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
426 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
427 An LWP has no map or address space file descriptor, since
428 the memory map and address space are shared by all LWPs. */
430 /* In this case, there are several different file descriptors that
431 we might be asked to open. The control file descriptor will be
432 opened early, but the others will be opened lazily as they are
435 strcpy (tmp
, pi
->pathname
);
436 switch (which
) { /* Which file descriptor to open? */
439 strcat (tmp
, "/lwpctl");
441 strcat (tmp
, "/ctl");
442 fd
= open_with_retry (tmp
, O_WRONLY
);
449 return 0; /* There is no 'as' file descriptor for an lwp. */
451 fd
= open_with_retry (tmp
, O_RDWR
);
458 strcat (tmp
, "/lwpstatus");
460 strcat (tmp
, "/status");
461 fd
= open_with_retry (tmp
, O_RDONLY
);
467 return 0; /* unknown file descriptor */
470 return 1; /* success */
473 /* Allocate a data structure and link it into the procinfo list.
474 First tries to find a pre-existing one (FIXME: why?). Returns the
475 pointer to new procinfo struct. */
478 create_procinfo (int pid
, int tid
)
480 procinfo
*pi
, *parent
= NULL
;
482 if ((pi
= find_procinfo (pid
, tid
)))
483 return pi
; /* Already exists, nothing to do. */
485 /* Find parent before doing malloc, to save having to cleanup. */
487 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
489 doesn't exist yet? */
491 pi
= XNEW (procinfo
);
492 memset (pi
, 0, sizeof (procinfo
));
496 pi
->saved_entryset
= sysset_t_alloc (pi
);
497 pi
->saved_exitset
= sysset_t_alloc (pi
);
499 /* Chain into list. */
502 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
503 pi
->next
= procinfo_list
;
508 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
509 pi
->next
= parent
->thread_list
;
510 parent
->thread_list
= pi
;
515 /* Close all file descriptors associated with the procinfo. */
518 close_procinfo_files (procinfo
*pi
)
524 if (pi
->status_fd
> 0)
525 close (pi
->status_fd
);
526 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
529 /* Destructor function. Close, unlink and deallocate the object. */
532 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
536 /* Step one: unlink the procinfo from its list. */
540 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
543 ptr
->next
= pi
->next
;
547 /* Step two: close any open file descriptors. */
548 close_procinfo_files (pi
);
550 /* Step three: free the memory. */
551 xfree (pi
->saved_entryset
);
552 xfree (pi
->saved_exitset
);
557 destroy_procinfo (procinfo
*pi
)
561 if (pi
->tid
!= 0) /* Destroy a thread procinfo. */
563 tmp
= find_procinfo (pi
->pid
, 0); /* Find the parent process. */
564 destroy_one_procinfo (&tmp
->thread_list
, pi
);
566 else /* Destroy a process procinfo and all its threads. */
568 /* First destroy the children, if any; */
569 while (pi
->thread_list
!= NULL
)
570 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
571 /* Then destroy the parent. Genocide!!! */
572 destroy_one_procinfo (&procinfo_list
, pi
);
577 do_destroy_procinfo_cleanup (void *pi
)
579 destroy_procinfo ((procinfo
*) pi
);
582 enum { NOKILL
, KILL
};
584 /* To be called on a non_recoverable error for a procinfo. Prints
585 error messages, optionally sends a SIGKILL to the process, then
586 destroys the data structure. */
589 dead_procinfo (procinfo
*pi
, const char *msg
, int kill_p
)
595 print_sys_errmsg (pi
->pathname
, errno
);
599 sprintf (procfile
, "process %d", pi
->pid
);
600 print_sys_errmsg (procfile
, errno
);
603 kill (pi
->pid
, SIGKILL
);
605 destroy_procinfo (pi
);
609 /* Allocate and (partially) initialize a sysset_t struct. */
612 sysset_t_alloc (procinfo
*pi
)
614 return (sysset_t
*) xmalloc (sizeof (sysset_t
));
617 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
619 /* =================== /proc "MODULE" =================== */
621 /* This "module" is the interface layer between the /proc system API
622 and the gdb target vector functions. This layer consists of access
623 functions that encapsulate each of the basic operations that we
624 need to use from the /proc API.
626 The main motivation for this layer is to hide the fact that there
627 are two very different implementations of the /proc API. Rather
628 than have a bunch of #ifdefs all thru the gdb target vector
629 functions, we do our best to hide them all in here. */
631 static long proc_flags (procinfo
*pi
);
632 static int proc_why (procinfo
*pi
);
633 static int proc_what (procinfo
*pi
);
634 static int proc_set_current_signal (procinfo
*pi
, int signo
);
635 static int proc_get_current_thread (procinfo
*pi
);
636 static int proc_iterate_over_threads
638 int (*func
) (procinfo
*, procinfo
*, void *),
642 proc_warn (procinfo
*pi
, const char *func
, int line
)
644 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
645 print_sys_errmsg (errmsg
, errno
);
649 proc_error (procinfo
*pi
, const char *func
, int line
)
651 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
652 perror_with_name (errmsg
);
655 /* Updates the status struct in the procinfo. There is a 'valid'
656 flag, to let other functions know when this function needs to be
657 called (so the status is only read when it is needed). The status
658 file descriptor is also only opened when it is needed. Returns
659 non-zero for success, zero for failure. */
662 proc_get_status (procinfo
*pi
)
664 /* Status file descriptor is opened "lazily". */
665 if (pi
->status_fd
== 0 &&
666 open_procinfo_files (pi
, FD_STATUS
) == 0)
668 pi
->status_valid
= 0;
672 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
673 pi
->status_valid
= 0; /* fail */
676 /* Sigh... I have to read a different data structure,
677 depending on whether this is a main process or an LWP. */
679 pi
->status_valid
= (read (pi
->status_fd
,
680 (char *) &pi
->prstatus
.pr_lwp
,
681 sizeof (lwpstatus_t
))
682 == sizeof (lwpstatus_t
));
685 pi
->status_valid
= (read (pi
->status_fd
,
686 (char *) &pi
->prstatus
,
688 == sizeof (pstatus_t
));
692 if (pi
->status_valid
)
694 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
697 proc_get_current_thread (pi
));
700 /* The status struct includes general regs, so mark them valid too. */
701 pi
->gregs_valid
= pi
->status_valid
;
702 /* In the read/write multiple-fd model, the status struct includes
703 the fp regs too, so mark them valid too. */
704 pi
->fpregs_valid
= pi
->status_valid
;
705 return pi
->status_valid
; /* True if success, false if failure. */
708 /* Returns the process flags (pr_flags field). */
711 proc_flags (procinfo
*pi
)
713 if (!pi
->status_valid
)
714 if (!proc_get_status (pi
))
715 return 0; /* FIXME: not a good failure value (but what is?) */
717 return pi
->prstatus
.pr_lwp
.pr_flags
;
720 /* Returns the pr_why field (why the process stopped). */
723 proc_why (procinfo
*pi
)
725 if (!pi
->status_valid
)
726 if (!proc_get_status (pi
))
727 return 0; /* FIXME: not a good failure value (but what is?) */
729 return pi
->prstatus
.pr_lwp
.pr_why
;
732 /* Returns the pr_what field (details of why the process stopped). */
735 proc_what (procinfo
*pi
)
737 if (!pi
->status_valid
)
738 if (!proc_get_status (pi
))
739 return 0; /* FIXME: not a good failure value (but what is?) */
741 return pi
->prstatus
.pr_lwp
.pr_what
;
744 /* This function is only called when PI is stopped by a watchpoint.
745 Assuming the OS supports it, write to *ADDR the data address which
746 triggered it and return 1. Return 0 if it is not possible to know
750 proc_watchpoint_address (procinfo
*pi
, CORE_ADDR
*addr
)
752 if (!pi
->status_valid
)
753 if (!proc_get_status (pi
))
756 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
757 builtin_type (target_gdbarch ())->builtin_data_ptr
,
758 (gdb_byte
*) &pi
->prstatus
.pr_lwp
.pr_info
.si_addr
);
762 /* Returns the pr_nsysarg field (number of args to the current
766 proc_nsysarg (procinfo
*pi
)
768 if (!pi
->status_valid
)
769 if (!proc_get_status (pi
))
772 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
775 /* Returns the pr_sysarg field (pointer to the arguments of current
779 proc_sysargs (procinfo
*pi
)
781 if (!pi
->status_valid
)
782 if (!proc_get_status (pi
))
785 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
788 /* Set or reset any of the following process flags:
789 PR_FORK -- forked child will inherit trace flags
790 PR_RLC -- traced process runs when last /proc file closed.
791 PR_KLC -- traced process is killed when last /proc file closed.
792 PR_ASYNC -- LWP's get to run/stop independently.
794 This function is done using read/write [PCSET/PCRESET/PCUNSET].
798 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
799 mode -- 1 for set, 0 for reset.
801 Returns non-zero for success, zero for failure. */
803 enum { FLAG_RESET
, FLAG_SET
};
806 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
808 long win
= 0; /* default to fail */
810 /* These operations affect the process as a whole, and applying them
811 to an individual LWP has the same meaning as applying them to the
812 main process. Therefore, if we're ever called with a pointer to
813 an LWP's procinfo, let's substitute the process's procinfo and
814 avoid opening the LWP's file descriptor unnecessarily. */
817 pi
= find_procinfo_or_die (pi
->pid
, 0);
821 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC). */
823 else /* Reset the flag. */
827 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
829 /* The above operation renders the procinfo's cached pstatus
831 pi
->status_valid
= 0;
834 warning (_("procfs: modify_flag failed to turn %s %s"),
835 flag
== PR_FORK
? "PR_FORK" :
836 flag
== PR_RLC
? "PR_RLC" :
837 flag
== PR_ASYNC
? "PR_ASYNC" :
838 flag
== PR_KLC
? "PR_KLC" :
840 mode
== FLAG_RESET
? "off" : "on");
845 /* Set the run_on_last_close flag. Process with all threads will
846 become runnable when debugger closes all /proc fds. Returns
847 non-zero for success, zero for failure. */
850 proc_set_run_on_last_close (procinfo
*pi
)
852 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
855 /* Reset the run_on_last_close flag. The process will NOT become
856 runnable when debugger closes its file handles. Returns non-zero
857 for success, zero for failure. */
860 proc_unset_run_on_last_close (procinfo
*pi
)
862 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
865 /* Reset inherit_on_fork flag. If the process forks a child while we
866 are registered for events in the parent, then we will NOT recieve
867 events from the child. Returns non-zero for success, zero for
871 proc_unset_inherit_on_fork (procinfo
*pi
)
873 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
876 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
877 (signal etc.), the remaining LWPs will continue to run. Returns
878 non-zero for success, zero for failure. */
881 proc_set_async (procinfo
*pi
)
883 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
886 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
887 (signal etc.), then all other LWPs will stop as well. Returns
888 non-zero for success, zero for failure. */
891 proc_unset_async (procinfo
*pi
)
893 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
896 /* Request the process/LWP to stop. Does not wait. Returns non-zero
897 for success, zero for failure. */
900 proc_stop_process (procinfo
*pi
)
904 /* We might conceivably apply this operation to an LWP, and the
905 LWP's ctl file descriptor might not be open. */
907 if (pi
->ctl_fd
== 0 &&
908 open_procinfo_files (pi
, FD_CTL
) == 0)
912 procfs_ctl_t cmd
= PCSTOP
;
914 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
920 /* Wait for the process or LWP to stop (block until it does). Returns
921 non-zero for success, zero for failure. */
924 proc_wait_for_stop (procinfo
*pi
)
928 /* We should never have to apply this operation to any procinfo
929 except the one for the main process. If that ever changes for
930 any reason, then take out the following clause and replace it
931 with one that makes sure the ctl_fd is open. */
934 pi
= find_procinfo_or_die (pi
->pid
, 0);
936 procfs_ctl_t cmd
= PCWSTOP
;
938 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
939 /* We been runnin' and we stopped -- need to update status. */
940 pi
->status_valid
= 0;
945 /* Make the process or LWP runnable.
947 Options (not all are implemented):
949 - clear current fault
950 - clear current signal
951 - abort the current system call
952 - stop as soon as finished with system call
953 - (ioctl): set traced signal set
954 - (ioctl): set held signal set
955 - (ioctl): set traced fault set
956 - (ioctl): set start pc (vaddr)
958 Always clears the current fault. PI is the process or LWP to
959 operate on. If STEP is true, set the process or LWP to trap after
960 one instruction. If SIGNO is zero, clear the current signal if
961 any; if non-zero, set the current signal to this one. Returns
962 non-zero for success, zero for failure. */
965 proc_run_process (procinfo
*pi
, int step
, int signo
)
970 /* We will probably have to apply this operation to individual
971 threads, so make sure the control file descriptor is open. */
973 if (pi
->ctl_fd
== 0 &&
974 open_procinfo_files (pi
, FD_CTL
) == 0)
979 runflags
= PRCFAULT
; /* Always clear current fault. */
984 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals. */
985 proc_set_current_signal (pi
, signo
);
991 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
996 /* Register to trace signals in the process or LWP. Returns non-zero
997 for success, zero for failure. */
1000 proc_set_traced_signals (procinfo
*pi
, sigset_t
*sigset
)
1004 /* We should never have to apply this operation to any procinfo
1005 except the one for the main process. If that ever changes for
1006 any reason, then take out the following clause and replace it
1007 with one that makes sure the ctl_fd is open. */
1010 pi
= find_procinfo_or_die (pi
->pid
, 0);
1014 /* Use char array to avoid alignment issues. */
1015 char sigset
[sizeof (sigset_t
)];
1019 memcpy (&arg
.sigset
, sigset
, sizeof (sigset_t
));
1021 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1023 /* The above operation renders the procinfo's cached pstatus obsolete. */
1024 pi
->status_valid
= 0;
1027 warning (_("procfs: set_traced_signals failed"));
1031 /* Register to trace hardware faults in the process or LWP. Returns
1032 non-zero for success, zero for failure. */
1035 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1039 /* We should never have to apply this operation to any procinfo
1040 except the one for the main process. If that ever changes for
1041 any reason, then take out the following clause and replace it
1042 with one that makes sure the ctl_fd is open. */
1045 pi
= find_procinfo_or_die (pi
->pid
, 0);
1049 /* Use char array to avoid alignment issues. */
1050 char fltset
[sizeof (fltset_t
)];
1054 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1056 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1058 /* The above operation renders the procinfo's cached pstatus obsolete. */
1059 pi
->status_valid
= 0;
1064 /* Register to trace entry to system calls in the process or LWP.
1065 Returns non-zero for success, zero for failure. */
1068 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1072 /* We should never have to apply this operation to any procinfo
1073 except the one for the main process. If that ever changes for
1074 any reason, then take out the following clause and replace it
1075 with one that makes sure the ctl_fd is open. */
1078 pi
= find_procinfo_or_die (pi
->pid
, 0);
1080 struct gdb_proc_ctl_pcsentry
{
1082 /* Use char array to avoid alignment issues. */
1083 char sysset
[sizeof (sysset_t
)];
1085 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
);
1087 argp
= (struct gdb_proc_ctl_pcsentry
*) xmalloc (argp_size
);
1089 argp
->cmd
= PCSENTRY
;
1090 memcpy (&argp
->sysset
, sysset
, sizeof (sysset_t
));
1092 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1095 /* The above operation renders the procinfo's cached pstatus
1097 pi
->status_valid
= 0;
1102 /* Register to trace exit from system calls in the process or LWP.
1103 Returns non-zero for success, zero for failure. */
1106 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
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);
1118 struct gdb_proc_ctl_pcsexit
{
1120 /* Use char array to avoid alignment issues. */
1121 char sysset
[sizeof (sysset_t
)];
1123 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
);
1125 argp
= (struct gdb_proc_ctl_pcsexit
*) xmalloc (argp_size
);
1127 argp
->cmd
= PCSEXIT
;
1128 memcpy (&argp
->sysset
, sysset
, sizeof (sysset_t
));
1130 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1133 /* The above operation renders the procinfo's cached pstatus
1135 pi
->status_valid
= 0;
1140 /* Specify the set of blocked / held signals in the process or LWP.
1141 Returns non-zero for success, zero for failure. */
1144 proc_set_held_signals (procinfo
*pi
, sigset_t
*sighold
)
1148 /* We should never have to apply this operation to any procinfo
1149 except the one for the main process. If that ever changes for
1150 any reason, then take out the following clause and replace it
1151 with one that makes sure the ctl_fd is open. */
1154 pi
= find_procinfo_or_die (pi
->pid
, 0);
1158 /* Use char array to avoid alignment issues. */
1159 char hold
[sizeof (sigset_t
)];
1163 memcpy (&arg
.hold
, sighold
, sizeof (sigset_t
));
1164 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1166 /* The above operation renders the procinfo's cached pstatus
1168 pi
->status_valid
= 0;
1173 /* Returns the set of signals that are held / blocked. Will also copy
1174 the sigset if SAVE is non-zero. */
1177 proc_get_held_signals (procinfo
*pi
, sigset_t
*save
)
1179 sigset_t
*ret
= NULL
;
1181 /* We should never have to apply this operation to any procinfo
1182 except the one for the main process. If that ever changes for
1183 any reason, then take out the following clause and replace it
1184 with one that makes sure the ctl_fd is open. */
1187 pi
= find_procinfo_or_die (pi
->pid
, 0);
1189 if (!pi
->status_valid
)
1190 if (!proc_get_status (pi
))
1193 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1195 memcpy (save
, ret
, sizeof (sigset_t
));
1200 /* Returns the set of signals that are traced / debugged. Will also
1201 copy the sigset if SAVE is non-zero. */
1204 proc_get_traced_signals (procinfo
*pi
, sigset_t
*save
)
1206 sigset_t
*ret
= NULL
;
1208 /* We should never have to apply this operation to any procinfo
1209 except the one for the main process. If that ever changes for
1210 any reason, then take out the following clause and replace it
1211 with one that makes sure the ctl_fd is open. */
1214 pi
= find_procinfo_or_die (pi
->pid
, 0);
1216 if (!pi
->status_valid
)
1217 if (!proc_get_status (pi
))
1220 ret
= &pi
->prstatus
.pr_sigtrace
;
1222 memcpy (save
, ret
, sizeof (sigset_t
));
1227 /* Returns the set of hardware faults that are traced /debugged. Will
1228 also copy the faultset if SAVE is non-zero. */
1231 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1233 fltset_t
*ret
= NULL
;
1235 /* We should never have to apply this operation to any procinfo
1236 except the one for the main process. If that ever changes for
1237 any reason, then take out the following clause and replace it
1238 with one that makes sure the ctl_fd is open. */
1241 pi
= find_procinfo_or_die (pi
->pid
, 0);
1243 if (!pi
->status_valid
)
1244 if (!proc_get_status (pi
))
1247 ret
= &pi
->prstatus
.pr_flttrace
;
1249 memcpy (save
, ret
, sizeof (fltset_t
));
1254 /* Returns the set of syscalls that are traced /debugged on entry.
1255 Will also copy the syscall set if SAVE is non-zero. */
1258 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1260 sysset_t
*ret
= NULL
;
1262 /* We should never have to apply this operation to any procinfo
1263 except the one for the main process. If that ever changes for
1264 any reason, then take out the following clause and replace it
1265 with one that makes sure the ctl_fd is open. */
1268 pi
= find_procinfo_or_die (pi
->pid
, 0);
1270 if (!pi
->status_valid
)
1271 if (!proc_get_status (pi
))
1274 ret
= &pi
->prstatus
.pr_sysentry
;
1276 memcpy (save
, ret
, sizeof (sysset_t
));
1281 /* Returns the set of syscalls that are traced /debugged on exit.
1282 Will also copy the syscall set if SAVE is non-zero. */
1285 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1287 sysset_t
*ret
= NULL
;
1289 /* We should never have to apply this operation to any procinfo
1290 except the one for the main process. If that ever changes for
1291 any reason, then take out the following clause and replace it
1292 with one that makes sure the ctl_fd is open. */
1295 pi
= find_procinfo_or_die (pi
->pid
, 0);
1297 if (!pi
->status_valid
)
1298 if (!proc_get_status (pi
))
1301 ret
= &pi
->prstatus
.pr_sysexit
;
1303 memcpy (save
, ret
, sizeof (sysset_t
));
1308 /* The current fault (if any) is cleared; the associated signal will
1309 not be sent to the process or LWP when it resumes. Returns
1310 non-zero for success, zero for failure. */
1313 proc_clear_current_fault (procinfo
*pi
)
1317 /* We should never have to apply this operation to any procinfo
1318 except the one for the main process. If that ever changes for
1319 any reason, then take out the following clause and replace it
1320 with one that makes sure the ctl_fd is open. */
1323 pi
= find_procinfo_or_die (pi
->pid
, 0);
1325 procfs_ctl_t cmd
= PCCFAULT
;
1327 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1332 /* Set the "current signal" that will be delivered next to the
1333 process. NOTE: semantics are different from those of KILL. This
1334 signal will be delivered to the process or LWP immediately when it
1335 is resumed (even if the signal is held/blocked); it will NOT
1336 immediately cause another event of interest, and will NOT first
1337 trap back to the debugger. Returns non-zero for success, zero for
1341 proc_set_current_signal (procinfo
*pi
, int signo
)
1346 /* Use char array to avoid alignment issues. */
1347 char sinfo
[sizeof (siginfo_t
)];
1351 struct target_waitstatus wait_status
;
1353 /* We should never have to apply this operation to any procinfo
1354 except the one for the main process. If that ever changes for
1355 any reason, then take out the following clause and replace it
1356 with one that makes sure the ctl_fd is open. */
1359 pi
= find_procinfo_or_die (pi
->pid
, 0);
1361 /* The pointer is just a type alias. */
1362 get_last_target_status (&wait_ptid
, &wait_status
);
1363 if (ptid_equal (wait_ptid
, inferior_ptid
)
1364 && wait_status
.kind
== TARGET_WAITKIND_STOPPED
1365 && wait_status
.value
.sig
== gdb_signal_from_host (signo
)
1366 && proc_get_status (pi
)
1367 && pi
->prstatus
.pr_lwp
.pr_info
.si_signo
== signo
1369 /* Use the siginfo associated with the signal being
1371 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_lwp
.pr_info
, sizeof (siginfo_t
));
1374 mysinfo
.si_signo
= signo
;
1375 mysinfo
.si_code
= 0;
1376 mysinfo
.si_pid
= getpid (); /* ?why? */
1377 mysinfo
.si_uid
= getuid (); /* ?why? */
1378 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1382 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1387 /* The current signal (if any) is cleared, and is not sent to the
1388 process or LWP when it resumes. Returns non-zero for success, zero
1392 proc_clear_current_signal (procinfo
*pi
)
1396 /* We should never have to apply this operation to any procinfo
1397 except the one for the main process. If that ever changes for
1398 any reason, then take out the following clause and replace it
1399 with one that makes sure the ctl_fd is open. */
1402 pi
= find_procinfo_or_die (pi
->pid
, 0);
1406 /* Use char array to avoid alignment issues. */
1407 char sinfo
[sizeof (siginfo_t
)];
1412 /* The pointer is just a type alias. */
1413 mysinfo
.si_signo
= 0;
1414 mysinfo
.si_code
= 0;
1415 mysinfo
.si_errno
= 0;
1416 mysinfo
.si_pid
= getpid (); /* ?why? */
1417 mysinfo
.si_uid
= getuid (); /* ?why? */
1418 memcpy (arg
.sinfo
, &mysinfo
, sizeof (siginfo_t
));
1420 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1425 /* Return the general-purpose registers for the process or LWP
1426 corresponding to PI. Upon failure, return NULL. */
1428 static gdb_gregset_t
*
1429 proc_get_gregs (procinfo
*pi
)
1431 if (!pi
->status_valid
|| !pi
->gregs_valid
)
1432 if (!proc_get_status (pi
))
1435 return &pi
->prstatus
.pr_lwp
.pr_reg
;
1438 /* Return the general-purpose registers for the process or LWP
1439 corresponding to PI. Upon failure, return NULL. */
1441 static gdb_fpregset_t
*
1442 proc_get_fpregs (procinfo
*pi
)
1444 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
1445 if (!proc_get_status (pi
))
1448 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
1451 /* Write the general-purpose registers back to the process or LWP
1452 corresponding to PI. Return non-zero for success, zero for
1456 proc_set_gregs (procinfo
*pi
)
1458 gdb_gregset_t
*gregs
;
1461 gregs
= proc_get_gregs (pi
);
1463 return 0; /* proc_get_regs has already warned. */
1465 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1473 /* Use char array to avoid alignment issues. */
1474 char gregs
[sizeof (gdb_gregset_t
)];
1478 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
1479 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1482 /* Policy: writing the registers invalidates our cache. */
1483 pi
->gregs_valid
= 0;
1487 /* Write the floating-pointer registers back to the process or LWP
1488 corresponding to PI. Return non-zero for success, zero for
1492 proc_set_fpregs (procinfo
*pi
)
1494 gdb_fpregset_t
*fpregs
;
1497 fpregs
= proc_get_fpregs (pi
);
1499 return 0; /* proc_get_fpregs has already warned. */
1501 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
1509 /* Use char array to avoid alignment issues. */
1510 char fpregs
[sizeof (gdb_fpregset_t
)];
1514 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
1515 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1518 /* Policy: writing the registers invalidates our cache. */
1519 pi
->fpregs_valid
= 0;
1523 /* Send a signal to the proc or lwp with the semantics of "kill()".
1524 Returns non-zero for success, zero for failure. */
1527 proc_kill (procinfo
*pi
, int signo
)
1531 /* We might conceivably apply this operation to an LWP, and the
1532 LWP's ctl file descriptor might not be open. */
1534 if (pi
->ctl_fd
== 0 &&
1535 open_procinfo_files (pi
, FD_CTL
) == 0)
1541 procfs_ctl_t cmd
[2];
1545 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1551 /* Find the pid of the process that started this one. Returns the
1552 parent process pid, or zero. */
1555 proc_parent_pid (procinfo
*pi
)
1557 /* We should never have to apply this operation to any procinfo
1558 except the one for the main process. If that ever changes for
1559 any reason, then take out the following clause and replace it
1560 with one that makes sure the ctl_fd is open. */
1563 pi
= find_procinfo_or_die (pi
->pid
, 0);
1565 if (!pi
->status_valid
)
1566 if (!proc_get_status (pi
))
1569 return pi
->prstatus
.pr_ppid
;
1572 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
1573 (a.k.a void pointer)! */
1576 procfs_address_to_host_pointer (CORE_ADDR addr
)
1578 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
1581 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (ptr_type
));
1582 gdbarch_address_to_pointer (target_gdbarch (), ptr_type
,
1583 (gdb_byte
*) &ptr
, addr
);
1588 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
1592 char watch
[sizeof (prwatch_t
)];
1596 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
1597 convert a target address into something that can be stored in a
1598 native data structure. */
1599 pwatch
.pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
1600 pwatch
.pr_size
= len
;
1601 pwatch
.pr_wflags
= wflags
;
1603 memcpy (arg
.watch
, &pwatch
, sizeof (prwatch_t
));
1604 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
1607 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
1609 #include <sys/sysi86.h>
1611 /* The KEY is actually the value of the lower 16 bits of the GS
1612 register for the LWP that we're interested in. Returns the
1613 matching ssh struct (LDT entry). */
1616 proc_get_LDT_entry (procinfo
*pi
, int key
)
1618 static struct ssd
*ldt_entry
= NULL
;
1619 char pathname
[MAX_PROC_NAME_SIZE
];
1621 /* Allocate space for one LDT entry.
1622 This alloc must persist, because we return a pointer to it. */
1623 if (ldt_entry
== NULL
)
1624 ldt_entry
= XNEW (struct ssd
);
1626 /* Open the file descriptor for the LDT table. */
1627 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
1628 scoped_fd
fd (open_with_retry (pathname
, O_RDONLY
));
1631 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
1635 /* Now 'read' thru the table, find a match and return it. */
1636 while (read (fd
.get (), ldt_entry
, sizeof (struct ssd
))
1637 == sizeof (struct ssd
))
1639 if (ldt_entry
->sel
== 0 &&
1640 ldt_entry
->bo
== 0 &&
1641 ldt_entry
->acc1
== 0 &&
1642 ldt_entry
->acc2
== 0)
1643 break; /* end of table */
1644 /* If key matches, return this entry. */
1645 if (ldt_entry
->sel
== key
)
1648 /* Loop ended, match not found. */
1652 /* Returns the pointer to the LDT entry of PTID. */
1655 procfs_find_LDT_entry (ptid_t ptid
)
1657 gdb_gregset_t
*gregs
;
1661 /* Find procinfo for the lwp. */
1662 if ((pi
= find_procinfo (ptid_get_pid (ptid
), ptid_get_lwp (ptid
))) == NULL
)
1664 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
1665 ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
1668 /* get its general registers. */
1669 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
1671 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
1672 ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
1675 /* Now extract the GS register's lower 16 bits. */
1676 key
= (*gregs
)[GS
] & 0xffff;
1678 /* Find the matching entry and return it. */
1679 return proc_get_LDT_entry (pi
, key
);
1684 /* =============== END, non-thread part of /proc "MODULE" =============== */
1686 /* =================== Thread "MODULE" =================== */
1688 /* NOTE: you'll see more ifdefs and duplication of functions here,
1689 since there is a different way to do threads on every OS. */
1691 /* Returns the number of threads for the process. */
1694 proc_get_nthreads (procinfo
*pi
)
1696 if (!pi
->status_valid
)
1697 if (!proc_get_status (pi
))
1700 /* Only works for the process procinfo, because the LWP procinfos do not
1701 get prstatus filled in. */
1702 if (pi
->tid
!= 0) /* Find the parent process procinfo. */
1703 pi
= find_procinfo_or_die (pi
->pid
, 0);
1704 return pi
->prstatus
.pr_nlwp
;
1709 Return the ID of the thread that had an event of interest.
1710 (ie. the one that hit a breakpoint or other traced event). All
1711 other things being equal, this should be the ID of a thread that is
1712 currently executing. */
1715 proc_get_current_thread (procinfo
*pi
)
1717 /* Note: this should be applied to the root procinfo for the
1718 process, not to the procinfo for an LWP. If applied to the
1719 procinfo for an LWP, it will simply return that LWP's ID. In
1720 that case, find the parent process procinfo. */
1723 pi
= find_procinfo_or_die (pi
->pid
, 0);
1725 if (!pi
->status_valid
)
1726 if (!proc_get_status (pi
))
1729 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
1732 /* Discover the IDs of all the threads within the process, and create
1733 a procinfo for each of them (chained to the parent). This
1734 unfortunately requires a different method on every OS. Returns
1735 non-zero for success, zero for failure. */
1738 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
1740 if (thread
&& parent
) /* sanity */
1742 thread
->status_valid
= 0;
1743 if (!proc_get_status (thread
))
1744 destroy_one_procinfo (&parent
->thread_list
, thread
);
1746 return 0; /* keep iterating */
1750 do_closedir_cleanup (void *dir
)
1752 closedir ((DIR *) dir
);
1756 proc_update_threads (procinfo
*pi
)
1758 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
1759 struct dirent
*direntry
;
1760 struct cleanup
*old_chain
= NULL
;
1765 /* We should never have to apply this operation to any procinfo
1766 except the one for the main process. If that ever changes for
1767 any reason, then take out the following clause and replace it
1768 with one that makes sure the ctl_fd is open. */
1771 pi
= find_procinfo_or_die (pi
->pid
, 0);
1773 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
1775 /* Note: this brute-force method was originally devised for Unixware
1776 (support removed since), and will also work on Solaris 2.6 and
1777 2.7. The original comment mentioned the existence of a much
1778 simpler and more elegant way to do this on Solaris, but didn't
1779 point out what that was. */
1781 strcpy (pathname
, pi
->pathname
);
1782 strcat (pathname
, "/lwp");
1783 if ((dirp
= opendir (pathname
)) == NULL
)
1784 proc_error (pi
, "update_threads, opendir", __LINE__
);
1786 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
1787 while ((direntry
= readdir (dirp
)) != NULL
)
1788 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
1790 lwpid
= atoi (&direntry
->d_name
[0]);
1791 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
1792 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
1794 pi
->threads_valid
= 1;
1795 do_cleanups (old_chain
);
1799 /* Given a pointer to a function, call that function once for each lwp
1800 in the procinfo list, until the function returns non-zero, in which
1801 event return the value returned by the function.
1803 Note: this function does NOT call update_threads. If you want to
1804 discover new threads first, you must call that function explicitly.
1805 This function just makes a quick pass over the currently-known
1808 PI is the parent process procinfo. FUNC is the per-thread
1809 function. PTR is an opaque parameter for function. Returns the
1810 first non-zero return value from the callee, or zero. */
1813 proc_iterate_over_threads (procinfo
*pi
,
1814 int (*func
) (procinfo
*, procinfo
*, void *),
1817 procinfo
*thread
, *next
;
1820 /* We should never have to apply this operation to any procinfo
1821 except the one for the main process. If that ever changes for
1822 any reason, then take out the following clause and replace it
1823 with one that makes sure the ctl_fd is open. */
1826 pi
= find_procinfo_or_die (pi
->pid
, 0);
1828 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
1830 next
= thread
->next
; /* In case thread is destroyed. */
1831 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
1838 /* =================== END, Thread "MODULE" =================== */
1840 /* =================== END, /proc "MODULE" =================== */
1842 /* =================== GDB "MODULE" =================== */
1844 /* Here are all of the gdb target vector functions and their
1847 static ptid_t
do_attach (ptid_t ptid
);
1848 static void do_detach ();
1849 static void proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
,
1850 int entry_or_exit
, int mode
, int from_tty
);
1852 /* Sets up the inferior to be debugged. Registers to trace signals,
1853 hardware faults, and syscalls. Note: does not set RLC flag: caller
1854 may want to customize that. Returns zero for success (note!
1855 unlike most functions in this module); on failure, returns the LINE
1856 NUMBER where it failed! */
1859 procfs_debug_inferior (procinfo
*pi
)
1861 fltset_t traced_faults
;
1862 sigset_t traced_signals
;
1863 sysset_t
*traced_syscall_entries
;
1864 sysset_t
*traced_syscall_exits
;
1867 /* Register to trace hardware faults in the child. */
1868 prfillset (&traced_faults
); /* trace all faults... */
1869 prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
1870 if (!proc_set_traced_faults (pi
, &traced_faults
))
1873 /* Initially, register to trace all signals in the child. */
1874 prfillset (&traced_signals
);
1875 if (!proc_set_traced_signals (pi
, &traced_signals
))
1879 /* Register to trace the 'exit' system call (on entry). */
1880 traced_syscall_entries
= sysset_t_alloc (pi
);
1881 premptyset (traced_syscall_entries
);
1882 praddset (traced_syscall_entries
, SYS_exit
);
1883 praddset (traced_syscall_entries
, SYS_lwp_exit
);
1885 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
1886 xfree (traced_syscall_entries
);
1890 /* Method for tracing exec syscalls. */
1892 Not all systems with /proc have all the exec* syscalls with the same
1893 names. On the SGI, for example, there is no SYS_exec, but there
1894 *is* a SYS_execv. So, we try to account for that. */
1896 traced_syscall_exits
= sysset_t_alloc (pi
);
1897 premptyset (traced_syscall_exits
);
1899 praddset (traced_syscall_exits
, SYS_exec
);
1901 praddset (traced_syscall_exits
, SYS_execve
);
1902 praddset (traced_syscall_exits
, SYS_lwp_create
);
1903 praddset (traced_syscall_exits
, SYS_lwp_exit
);
1905 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
1906 xfree (traced_syscall_exits
);
1914 procfs_attach (struct target_ops
*ops
, const char *args
, int from_tty
)
1919 pid
= parse_pid_to_attach (args
);
1921 if (pid
== getpid ())
1922 error (_("Attaching GDB to itself is not a good idea..."));
1926 exec_file
= get_exec_file (0);
1929 printf_filtered (_("Attaching to program `%s', %s\n"),
1930 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
1932 printf_filtered (_("Attaching to %s\n"),
1933 target_pid_to_str (pid_to_ptid (pid
)));
1937 inferior_ptid
= do_attach (pid_to_ptid (pid
));
1938 if (!target_is_pushed (ops
))
1943 procfs_detach (struct target_ops
*ops
, inferior
*inf
, int from_tty
)
1945 int pid
= ptid_get_pid (inferior_ptid
);
1949 const char *exec_file
;
1951 exec_file
= get_exec_file (0);
1952 if (exec_file
== NULL
)
1955 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file
,
1956 target_pid_to_str (pid_to_ptid (pid
)));
1957 gdb_flush (gdb_stdout
);
1962 inferior_ptid
= null_ptid
;
1963 detach_inferior (pid
);
1964 inf_child_maybe_unpush_target (ops
);
1968 do_attach (ptid_t ptid
)
1971 struct inferior
*inf
;
1975 if ((pi
= create_procinfo (ptid_get_pid (ptid
), 0)) == NULL
)
1976 perror (_("procfs: out of memory in 'attach'"));
1978 if (!open_procinfo_files (pi
, FD_CTL
))
1980 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
1981 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
1982 ptid_get_pid (ptid
));
1983 dead_procinfo (pi
, errmsg
, NOKILL
);
1986 /* Stop the process (if it isn't already stopped). */
1987 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
1989 pi
->was_stopped
= 1;
1990 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
1994 pi
->was_stopped
= 0;
1995 /* Set the process to run again when we close it. */
1996 if (!proc_set_run_on_last_close (pi
))
1997 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
1999 /* Now stop the process. */
2000 if (!proc_stop_process (pi
))
2001 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
2002 pi
->ignore_next_sigstop
= 1;
2004 /* Save some of the /proc state to be restored if we detach. */
2005 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
2006 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
2007 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
2008 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
2009 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
2010 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
2012 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
2013 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
2015 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
2016 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
2018 if ((fail
= procfs_debug_inferior (pi
)) != 0)
2019 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
2021 inf
= current_inferior ();
2022 inferior_appeared (inf
, pi
->pid
);
2023 /* Let GDB know that the inferior was attached. */
2024 inf
->attach_flag
= 1;
2026 /* Create a procinfo for the current lwp. */
2027 lwpid
= proc_get_current_thread (pi
);
2028 create_procinfo (pi
->pid
, lwpid
);
2030 /* Add it to gdb's thread list. */
2031 ptid
= ptid_build (pi
->pid
, lwpid
, 0);
2042 /* Find procinfo for the main process. */
2043 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
),
2044 0); /* FIXME: threads */
2046 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
2047 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
2049 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
2050 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
2052 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
2053 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
2055 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
2056 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
2058 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
2059 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
2061 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
2062 if (!(pi
->was_stopped
)
2063 || query (_("Was stopped when attached, make it runnable again? ")))
2065 /* Clear any pending signal. */
2066 if (!proc_clear_current_fault (pi
))
2067 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
2069 if (!proc_clear_current_signal (pi
))
2070 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
2072 if (!proc_set_run_on_last_close (pi
))
2073 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
2076 destroy_procinfo (pi
);
2079 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
2082 ??? Is the following note still relevant? We can't get individual
2083 registers with the PT_GETREGS ptrace(2) request either, yet we
2084 don't bother with caching at all in that case.
2086 NOTE: Since the /proc interface cannot give us individual
2087 registers, we pay no attention to REGNUM, and just fetch them all.
2088 This results in the possibility that we will do unnecessarily many
2089 fetches, since we may be called repeatedly for individual
2090 registers. So we cache the results, and mark the cache invalid
2091 when the process is resumed. */
2094 procfs_fetch_registers (struct target_ops
*ops
,
2095 struct regcache
*regcache
, int regnum
)
2097 gdb_gregset_t
*gregs
;
2099 ptid_t ptid
= regcache_get_ptid (regcache
);
2100 int pid
= ptid_get_pid (ptid
);
2101 int tid
= ptid_get_lwp (ptid
);
2102 struct gdbarch
*gdbarch
= regcache
->arch ();
2104 pi
= find_procinfo_or_die (pid
, tid
);
2107 error (_("procfs: fetch_registers failed to find procinfo for %s"),
2108 target_pid_to_str (ptid
));
2110 gregs
= proc_get_gregs (pi
);
2112 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
2114 supply_gregset (regcache
, (const gdb_gregset_t
*) gregs
);
2116 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
2118 gdb_fpregset_t
*fpregs
;
2120 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
2121 || regnum
== gdbarch_pc_regnum (gdbarch
)
2122 || regnum
== gdbarch_sp_regnum (gdbarch
))
2123 return; /* Not a floating point register. */
2125 fpregs
= proc_get_fpregs (pi
);
2127 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
2129 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregs
);
2133 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
2134 this for all registers.
2136 NOTE: Since the /proc interface will not read individual registers,
2137 we will cache these requests until the process is resumed, and only
2138 then write them back to the inferior process.
2140 FIXME: is that a really bad idea? Have to think about cases where
2141 writing one register might affect the value of others, etc. */
2144 procfs_store_registers (struct target_ops
*ops
,
2145 struct regcache
*regcache
, int regnum
)
2147 gdb_gregset_t
*gregs
;
2149 ptid_t ptid
= regcache_get_ptid (regcache
);
2150 int pid
= ptid_get_pid (ptid
);
2151 int tid
= ptid_get_lwp (ptid
);
2152 struct gdbarch
*gdbarch
= regcache
->arch ();
2154 pi
= find_procinfo_or_die (pid
, tid
);
2157 error (_("procfs: store_registers: failed to find procinfo for %s"),
2158 target_pid_to_str (ptid
));
2160 gregs
= proc_get_gregs (pi
);
2162 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
2164 fill_gregset (regcache
, gregs
, regnum
);
2165 if (!proc_set_gregs (pi
))
2166 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
2168 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
2170 gdb_fpregset_t
*fpregs
;
2172 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
2173 || regnum
== gdbarch_pc_regnum (gdbarch
)
2174 || regnum
== gdbarch_sp_regnum (gdbarch
))
2175 return; /* Not a floating point register. */
2177 fpregs
= proc_get_fpregs (pi
);
2179 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
2181 fill_fpregset (regcache
, fpregs
, regnum
);
2182 if (!proc_set_fpregs (pi
))
2183 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
2188 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
2190 if (scall
== SYS_lwp_exit
)
2196 syscall_is_exit (procinfo
*pi
, int scall
)
2198 if (scall
== SYS_exit
)
2204 syscall_is_exec (procinfo
*pi
, int scall
)
2207 if (scall
== SYS_exec
)
2210 if (scall
== SYS_execve
)
2216 syscall_is_lwp_create (procinfo
*pi
, int scall
)
2218 if (scall
== SYS_lwp_create
)
2223 /* Retrieve the next stop event from the child process. If child has
2224 not stopped yet, wait for it to stop. Translate /proc eventcodes
2225 (or possibly wait eventcodes) into gdb internal event codes.
2226 Returns the id of process (and possibly thread) that incurred the
2227 event. Event codes are returned through a pointer parameter. */
2230 procfs_wait (struct target_ops
*ops
,
2231 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
2233 /* First cut: loosely based on original version 2.1. */
2237 ptid_t retval
, temp_ptid
;
2238 int why
, what
, flags
;
2245 retval
= pid_to_ptid (-1);
2247 /* Find procinfo for main process. */
2248 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
2251 /* We must assume that the status is stale now... */
2252 pi
->status_valid
= 0;
2253 pi
->gregs_valid
= 0;
2254 pi
->fpregs_valid
= 0;
2256 #if 0 /* just try this out... */
2257 flags
= proc_flags (pi
);
2258 why
= proc_why (pi
);
2259 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
2260 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
2262 /* If child is not stopped, wait for it to stop. */
2263 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
2264 !proc_wait_for_stop (pi
))
2266 /* wait_for_stop failed: has the child terminated? */
2267 if (errno
== ENOENT
)
2271 /* /proc file not found; presumably child has terminated. */
2272 wait_retval
= wait (&wstat
); /* "wait" for the child's exit. */
2275 if (wait_retval
!= ptid_get_pid (inferior_ptid
))
2276 error (_("procfs: couldn't stop "
2277 "process %d: wait returned %d."),
2278 ptid_get_pid (inferior_ptid
), wait_retval
);
2279 /* FIXME: might I not just use waitpid?
2280 Or try find_procinfo to see if I know about this child? */
2281 retval
= pid_to_ptid (wait_retval
);
2283 else if (errno
== EINTR
)
2287 /* Unknown error from wait_for_stop. */
2288 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
2293 /* This long block is reached if either:
2294 a) the child was already stopped, or
2295 b) we successfully waited for the child with wait_for_stop.
2296 This block will analyze the /proc status, and translate it
2297 into a waitstatus for GDB.
2299 If we actually had to call wait because the /proc file
2300 is gone (child terminated), then we skip this block,
2301 because we already have a waitstatus. */
2303 flags
= proc_flags (pi
);
2304 why
= proc_why (pi
);
2305 what
= proc_what (pi
);
2307 if (flags
& (PR_STOPPED
| PR_ISTOP
))
2309 /* If it's running async (for single_thread control),
2310 set it back to normal again. */
2311 if (flags
& PR_ASYNC
)
2312 if (!proc_unset_async (pi
))
2313 proc_error (pi
, "target_wait, unset_async", __LINE__
);
2316 proc_prettyprint_why (why
, what
, 1);
2318 /* The 'pid' we will return to GDB is composed of
2319 the process ID plus the lwp ID. */
2320 retval
= ptid_build (pi
->pid
, proc_get_current_thread (pi
), 0);
2324 wstat
= (what
<< 8) | 0177;
2327 if (syscall_is_lwp_exit (pi
, what
))
2329 if (print_thread_events
)
2330 printf_unfiltered (_("[%s exited]\n"),
2331 target_pid_to_str (retval
));
2332 delete_thread (retval
);
2333 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2336 else if (syscall_is_exit (pi
, what
))
2338 struct inferior
*inf
;
2340 /* Handle SYS_exit call only. */
2341 /* Stopped at entry to SYS_exit.
2342 Make it runnable, resume it, then use
2343 the wait system call to get its exit code.
2344 Proc_run_process always clears the current
2346 Then return its exit status. */
2347 pi
->status_valid
= 0;
2349 /* FIXME: what we should do is return
2350 TARGET_WAITKIND_SPURIOUS. */
2351 if (!proc_run_process (pi
, 0, 0))
2352 proc_error (pi
, "target_wait, run_process", __LINE__
);
2354 inf
= find_inferior_pid (pi
->pid
);
2355 if (inf
->attach_flag
)
2357 /* Don't call wait: simulate waiting for exit,
2358 return a "success" exit code. Bogus: what if
2359 it returns something else? */
2361 retval
= inferior_ptid
; /* ? ? ? */
2365 int temp
= wait (&wstat
);
2367 /* FIXME: shouldn't I make sure I get the right
2368 event from the right process? If (for
2369 instance) I have killed an earlier inferior
2370 process but failed to clean up after it
2371 somehow, I could get its termination event
2374 /* If wait returns -1, that's what we return
2377 retval
= pid_to_ptid (temp
);
2382 printf_filtered (_("procfs: trapped on entry to "));
2383 proc_prettyprint_syscall (proc_what (pi
), 0);
2384 printf_filtered ("\n");
2386 long i
, nsysargs
, *sysargs
;
2388 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
2389 (sysargs
= proc_sysargs (pi
)) != NULL
)
2391 printf_filtered (_("%ld syscall arguments:\n"),
2393 for (i
= 0; i
< nsysargs
; i
++)
2394 printf_filtered ("#%ld: 0x%08lx\n",
2400 /* How to exit gracefully, returning "unknown
2402 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2403 return inferior_ptid
;
2407 /* How to keep going without returning to wfi: */
2408 target_continue_no_signal (ptid
);
2414 if (syscall_is_exec (pi
, what
))
2416 /* Hopefully this is our own "fork-child" execing
2417 the real child. Hoax this event into a trap, and
2418 GDB will see the child about to execute its start
2420 wstat
= (SIGTRAP
<< 8) | 0177;
2422 else if (syscall_is_lwp_create (pi
, what
))
2424 /* This syscall is somewhat like fork/exec. We
2425 will get the event twice: once for the parent
2426 LWP, and once for the child. We should already
2427 know about the parent LWP, but the child will
2428 be new to us. So, whenever we get this event,
2429 if it represents a new thread, simply add the
2430 thread to the list. */
2432 /* If not in procinfo list, add it. */
2433 temp_tid
= proc_get_current_thread (pi
);
2434 if (!find_procinfo (pi
->pid
, temp_tid
))
2435 create_procinfo (pi
->pid
, temp_tid
);
2437 temp_ptid
= ptid_build (pi
->pid
, temp_tid
, 0);
2438 /* If not in GDB's thread list, add it. */
2439 if (!in_thread_list (temp_ptid
))
2440 add_thread (temp_ptid
);
2442 /* Return to WFI, but tell it to immediately resume. */
2443 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2444 return inferior_ptid
;
2446 else if (syscall_is_lwp_exit (pi
, what
))
2448 if (print_thread_events
)
2449 printf_unfiltered (_("[%s exited]\n"),
2450 target_pid_to_str (retval
));
2451 delete_thread (retval
);
2452 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2457 /* FIXME: Do we need to handle SYS_sproc,
2458 SYS_fork, or SYS_vfork here? The old procfs
2459 seemed to use this event to handle threads on
2460 older (non-LWP) systems, where I'm assuming
2461 that threads were actually separate processes.
2462 Irix, maybe? Anyway, low priority for now. */
2466 printf_filtered (_("procfs: trapped on exit from "));
2467 proc_prettyprint_syscall (proc_what (pi
), 0);
2468 printf_filtered ("\n");
2470 long i
, nsysargs
, *sysargs
;
2472 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
2473 (sysargs
= proc_sysargs (pi
)) != NULL
)
2475 printf_filtered (_("%ld syscall arguments:\n"),
2477 for (i
= 0; i
< nsysargs
; i
++)
2478 printf_filtered ("#%ld: 0x%08lx\n",
2482 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
2483 return inferior_ptid
;
2488 wstat
= (SIGSTOP
<< 8) | 0177;
2493 printf_filtered (_("Retry #%d:\n"), retry
);
2494 pi
->status_valid
= 0;
2499 /* If not in procinfo list, add it. */
2500 temp_tid
= proc_get_current_thread (pi
);
2501 if (!find_procinfo (pi
->pid
, temp_tid
))
2502 create_procinfo (pi
->pid
, temp_tid
);
2504 /* If not in GDB's thread list, add it. */
2505 temp_ptid
= ptid_build (pi
->pid
, temp_tid
, 0);
2506 if (!in_thread_list (temp_ptid
))
2507 add_thread (temp_ptid
);
2509 status
->kind
= TARGET_WAITKIND_STOPPED
;
2510 status
->value
.sig
= GDB_SIGNAL_0
;
2515 wstat
= (what
<< 8) | 0177;
2520 wstat
= (SIGTRAP
<< 8) | 0177;
2522 /* FIXME: use si_signo where possible. */
2525 wstat
= (SIGILL
<< 8) | 0177;
2529 wstat
= (SIGTRAP
<< 8) | 0177;
2534 wstat
= (SIGSEGV
<< 8) | 0177;
2539 wstat
= (SIGFPE
<< 8) | 0177;
2541 case FLTPAGE
: /* Recoverable page fault */
2542 default: /* FIXME: use si_signo if possible for
2544 retval
= pid_to_ptid (-1);
2545 printf_filtered ("procfs:%d -- ", __LINE__
);
2546 printf_filtered (_("child stopped for unknown reason:\n"));
2547 proc_prettyprint_why (why
, what
, 1);
2548 error (_("... giving up..."));
2551 break; /* case PR_FAULTED: */
2552 default: /* switch (why) unmatched */
2553 printf_filtered ("procfs:%d -- ", __LINE__
);
2554 printf_filtered (_("child stopped for unknown reason:\n"));
2555 proc_prettyprint_why (why
, what
, 1);
2556 error (_("... giving up..."));
2559 /* Got this far without error: If retval isn't in the
2560 threads database, add it. */
2561 if (ptid_get_pid (retval
) > 0 &&
2562 !ptid_equal (retval
, inferior_ptid
) &&
2563 !in_thread_list (retval
))
2565 /* We have a new thread. We need to add it both to
2566 GDB's list and to our own. If we don't create a
2567 procinfo, resume may be unhappy later. */
2568 add_thread (retval
);
2569 if (find_procinfo (ptid_get_pid (retval
),
2570 ptid_get_lwp (retval
)) == NULL
)
2571 create_procinfo (ptid_get_pid (retval
),
2572 ptid_get_lwp (retval
));
2575 else /* Flags do not indicate STOPPED. */
2577 /* surely this can't happen... */
2578 printf_filtered ("procfs:%d -- process not stopped.\n",
2580 proc_prettyprint_flags (flags
, 1);
2581 error (_("procfs: ...giving up..."));
2586 store_waitstatus (status
, wstat
);
2592 /* Perform a partial transfer to/from the specified object. For
2593 memory transfers, fall back to the old memory xfer functions. */
2595 static enum target_xfer_status
2596 procfs_xfer_partial (struct target_ops
*ops
, enum target_object object
,
2597 const char *annex
, gdb_byte
*readbuf
,
2598 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
2599 ULONGEST
*xfered_len
)
2603 case TARGET_OBJECT_MEMORY
:
2604 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
2606 case TARGET_OBJECT_AUXV
:
2607 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
2608 offset
, len
, xfered_len
);
2611 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
,
2612 readbuf
, writebuf
, offset
, len
,
2617 /* Helper for procfs_xfer_partial that handles memory transfers.
2618 Arguments are like target_xfer_partial. */
2620 static enum target_xfer_status
2621 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
2622 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
2627 /* Find procinfo for main process. */
2628 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
2629 if (pi
->as_fd
== 0 &&
2630 open_procinfo_files (pi
, FD_AS
) == 0)
2632 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
2633 return TARGET_XFER_E_IO
;
2636 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
2637 return TARGET_XFER_E_IO
;
2639 if (writebuf
!= NULL
)
2641 PROCFS_NOTE ("write memory:\n");
2642 nbytes
= write (pi
->as_fd
, writebuf
, len
);
2646 PROCFS_NOTE ("read memory:\n");
2647 nbytes
= read (pi
->as_fd
, readbuf
, len
);
2650 return TARGET_XFER_E_IO
;
2651 *xfered_len
= nbytes
;
2652 return TARGET_XFER_OK
;
2655 /* Called by target_resume before making child runnable. Mark cached
2656 registers and status's invalid. If there are "dirty" caches that
2657 need to be written back to the child process, do that.
2659 File descriptors are also cached. As they are a limited resource,
2660 we cannot hold onto them indefinitely. However, as they are
2661 expensive to open, we don't want to throw them away
2662 indescriminately either. As a compromise, we will keep the file
2663 descriptors for the parent process, but discard any file
2664 descriptors we may have accumulated for the threads.
2666 As this function is called by iterate_over_threads, it always
2667 returns zero (so that iterate_over_threads will keep
2671 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
2673 /* About to run the child; invalidate caches and do any other
2677 if (pi
->gregs_dirty
)
2678 if (parent
== NULL
||
2679 proc_get_current_thread (parent
) != pi
->tid
)
2680 if (!proc_set_gregs (pi
)) /* flush gregs cache */
2681 proc_warn (pi
, "target_resume, set_gregs",
2683 if (gdbarch_fp0_regnum (target_gdbarch ()) >= 0)
2684 if (pi
->fpregs_dirty
)
2685 if (parent
== NULL
||
2686 proc_get_current_thread (parent
) != pi
->tid
)
2687 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
2688 proc_warn (pi
, "target_resume, set_fpregs",
2694 /* The presence of a parent indicates that this is an LWP.
2695 Close any file descriptors that it might have open.
2696 We don't do this to the master (parent) procinfo. */
2698 close_procinfo_files (pi
);
2700 pi
->gregs_valid
= 0;
2701 pi
->fpregs_valid
= 0;
2703 pi
->gregs_dirty
= 0;
2704 pi
->fpregs_dirty
= 0;
2706 pi
->status_valid
= 0;
2707 pi
->threads_valid
= 0;
2713 /* A callback function for iterate_over_threads. Find the
2714 asynchronous signal thread, and make it runnable. See if that
2715 helps matters any. */
2718 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
2721 if (proc_flags (pi
) & PR_ASLWP
)
2723 if (!proc_run_process (pi
, 0, -1))
2724 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
2732 /* Make the child process runnable. Normally we will then call
2733 procfs_wait and wait for it to stop again (unless gdb is async).
2735 If STEP is true, then arrange for the child to stop again after
2736 executing a single instruction. If SIGNO is zero, then cancel any
2737 pending signal; if non-zero, then arrange for the indicated signal
2738 to be delivered to the child when it runs. If PID is -1, then
2739 allow any child thread to run; if non-zero, then allow only the
2740 indicated thread to run. (not implemented yet). */
2743 procfs_resume (struct target_ops
*ops
,
2744 ptid_t ptid
, int step
, enum gdb_signal signo
)
2746 procinfo
*pi
, *thread
;
2750 prrun.prflags |= PRSVADDR;
2751 prrun.pr_vaddr = $PC; set resume address
2752 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
2753 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
2754 prrun.prflags |= PRCFAULT; clear current fault.
2756 PRSTRACE and PRSFAULT can be done by other means
2757 (proc_trace_signals, proc_trace_faults)
2758 PRSVADDR is unnecessary.
2759 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
2760 This basically leaves PRSTEP and PRCSIG.
2761 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
2762 So basically PR_STEP is the sole argument that must be passed
2763 to proc_run_process (for use in the prrun struct by ioctl). */
2765 /* Find procinfo for main process. */
2766 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
2768 /* First cut: ignore pid argument. */
2771 /* Convert signal to host numbering. */
2773 (signo
== GDB_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
2776 native_signo
= gdb_signal_to_host (signo
);
2778 pi
->ignore_next_sigstop
= 0;
2780 /* Running the process voids all cached registers and status. */
2781 /* Void the threads' caches first. */
2782 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
2783 /* Void the process procinfo's caches. */
2784 invalidate_cache (NULL
, pi
, NULL
);
2786 if (ptid_get_pid (ptid
) != -1)
2788 /* Resume a specific thread, presumably suppressing the
2790 thread
= find_procinfo (ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
2793 if (thread
->tid
!= 0)
2795 /* We're to resume a specific thread, and not the
2796 others. Set the child process's PR_ASYNC flag. */
2797 if (!proc_set_async (pi
))
2798 proc_error (pi
, "target_resume, set_async", __LINE__
);
2800 proc_iterate_over_threads (pi
,
2801 make_signal_thread_runnable
,
2804 pi
= thread
; /* Substitute the thread's procinfo
2810 if (!proc_run_process (pi
, step
, native_signo
))
2813 warning (_("resume: target already running. "
2814 "Pretend to resume, and hope for the best!"));
2816 proc_error (pi
, "target_resume", __LINE__
);
2820 /* Set up to trace signals in the child process. */
2823 procfs_pass_signals (struct target_ops
*self
,
2824 int numsigs
, unsigned char *pass_signals
)
2827 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
2830 prfillset (&signals
);
2832 for (signo
= 0; signo
< NSIG
; signo
++)
2834 int target_signo
= gdb_signal_from_host (signo
);
2835 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
2836 prdelset (&signals
, signo
);
2839 if (!proc_set_traced_signals (pi
, &signals
))
2840 proc_error (pi
, "pass_signals", __LINE__
);
2843 /* Print status information about the child process. */
2846 procfs_files_info (struct target_ops
*ignore
)
2848 struct inferior
*inf
= current_inferior ();
2850 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
2851 inf
->attach_flag
? "attached": "child",
2852 target_pid_to_str (inferior_ptid
));
2855 /* Make it die. Wait for it to die. Clean up after it. Note: this
2856 should only be applied to the real process, not to an LWP, because
2857 of the check for parent-process. If we need this to work for an
2858 LWP, it needs some more logic. */
2861 unconditionally_kill_inferior (procinfo
*pi
)
2865 parent_pid
= proc_parent_pid (pi
);
2866 if (!proc_kill (pi
, SIGKILL
))
2867 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
2868 destroy_procinfo (pi
);
2870 /* If pi is GDB's child, wait for it to die. */
2871 if (parent_pid
== getpid ())
2872 /* FIXME: should we use waitpid to make sure we get the right event?
2873 Should we check the returned event? */
2878 ret
= waitpid (pi
->pid
, &status
, 0);
2885 /* We're done debugging it, and we want it to go away. Then we want
2886 GDB to forget all about it. */
2889 procfs_kill_inferior (struct target_ops
*ops
)
2891 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
2893 /* Find procinfo for main process. */
2894 procinfo
*pi
= find_procinfo (ptid_get_pid (inferior_ptid
), 0);
2897 unconditionally_kill_inferior (pi
);
2898 target_mourn_inferior (inferior_ptid
);
2902 /* Forget we ever debugged this thing! */
2905 procfs_mourn_inferior (struct target_ops
*ops
)
2909 if (!ptid_equal (inferior_ptid
, null_ptid
))
2911 /* Find procinfo for main process. */
2912 pi
= find_procinfo (ptid_get_pid (inferior_ptid
), 0);
2914 destroy_procinfo (pi
);
2917 generic_mourn_inferior ();
2919 inf_child_maybe_unpush_target (ops
);
2922 /* When GDB forks to create a runnable inferior process, this function
2923 is called on the parent side of the fork. It's job is to do
2924 whatever is necessary to make the child ready to be debugged, and
2925 then wait for the child to synchronize. */
2928 procfs_init_inferior (struct target_ops
*ops
, int pid
)
2935 /* This routine called on the parent side (GDB side)
2936 after GDB forks the inferior. */
2937 if (!target_is_pushed (ops
))
2940 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
2941 perror (_("procfs: out of memory in 'init_inferior'"));
2943 if (!open_procinfo_files (pi
, FD_CTL
))
2944 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
2948 open_procinfo_files // done
2951 procfs_notice_signals
2958 /* If not stopped yet, wait for it to stop. */
2959 if (!(proc_flags (pi
) & PR_STOPPED
) &&
2960 !(proc_wait_for_stop (pi
)))
2961 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
2963 /* Save some of the /proc state to be restored if we detach. */
2964 /* FIXME: Why? In case another debugger was debugging it?
2965 We're it's parent, for Ghu's sake! */
2966 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
2967 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
2968 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
2969 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
2970 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
2971 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
2972 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
2973 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
2974 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
2975 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
2977 if ((fail
= procfs_debug_inferior (pi
)) != 0)
2978 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
2980 /* FIXME: logically, we should really be turning OFF run-on-last-close,
2981 and possibly even turning ON kill-on-last-close at this point. But
2982 I can't make that change without careful testing which I don't have
2983 time to do right now... */
2984 /* Turn on run-on-last-close flag so that the child
2985 will die if GDB goes away for some reason. */
2986 if (!proc_set_run_on_last_close (pi
))
2987 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
2989 /* We now have have access to the lwpid of the main thread/lwp. */
2990 lwpid
= proc_get_current_thread (pi
);
2992 /* Create a procinfo for the main lwp. */
2993 create_procinfo (pid
, lwpid
);
2995 /* We already have a main thread registered in the thread table at
2996 this point, but it didn't have any lwp info yet. Notify the core
2997 about it. This changes inferior_ptid as well. */
2998 thread_change_ptid (pid_to_ptid (pid
),
2999 ptid_build (pid
, lwpid
, 0));
3001 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
3004 /* When GDB forks to create a new process, this function is called on
3005 the child side of the fork before GDB exec's the user program. Its
3006 job is to make the child minimally debuggable, so that the parent
3007 GDB process can connect to the child and take over. This function
3008 should do only the minimum to make that possible, and to
3009 synchronize with the parent process. The parent process should
3010 take care of the details. */
3013 procfs_set_exec_trap (void)
3015 /* This routine called on the child side (inferior side)
3016 after GDB forks the inferior. It must use only local variables,
3017 because it may be sharing data space with its parent. */
3022 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
3023 perror_with_name (_("procfs: create_procinfo failed in child."));
3025 if (open_procinfo_files (pi
, FD_CTL
) == 0)
3027 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
3028 gdb_flush (gdb_stderr
);
3029 /* No need to call "dead_procinfo", because we're going to
3034 /* Method for tracing exec syscalls. */
3036 Not all systems with /proc have all the exec* syscalls with the same
3037 names. On the SGI, for example, there is no SYS_exec, but there
3038 *is* a SYS_execv. So, we try to account for that. */
3040 exitset
= sysset_t_alloc (pi
);
3041 premptyset (exitset
);
3043 praddset (exitset
, SYS_exec
);
3045 praddset (exitset
, SYS_execve
);
3047 if (!proc_set_traced_sysexit (pi
, exitset
))
3049 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
3050 gdb_flush (gdb_stderr
);
3054 /* FIXME: should this be done in the parent instead? */
3055 /* Turn off inherit on fork flag so that all grand-children
3056 of gdb start with tracing flags cleared. */
3057 if (!proc_unset_inherit_on_fork (pi
))
3058 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
3060 /* Turn off run on last close flag, so that the child process
3061 cannot run away just because we close our handle on it.
3062 We want it to wait for the parent to attach. */
3063 if (!proc_unset_run_on_last_close (pi
))
3064 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
3066 /* FIXME: No need to destroy the procinfo --
3067 we have our own address space, and we're about to do an exec! */
3068 /*destroy_procinfo (pi);*/
3071 /* This function is called BEFORE gdb forks the inferior process. Its
3072 only real responsibility is to set things up for the fork, and tell
3073 GDB which two functions to call after the fork (one for the parent,
3074 and one for the child).
3076 This function does a complicated search for a unix shell program,
3077 which it then uses to parse arguments and environment variables to
3078 be sent to the child. I wonder whether this code could not be
3079 abstracted out and shared with other unix targets such as
3083 procfs_create_inferior (struct target_ops
*ops
, const char *exec_file
,
3084 const std::string
&allargs
, char **env
, int from_tty
)
3086 char *shell_file
= getenv ("SHELL");
3090 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
3093 /* We will be looking down the PATH to find shell_file. If we
3094 just do this the normal way (via execlp, which operates by
3095 attempting an exec for each element of the PATH until it
3096 finds one which succeeds), then there will be an exec for
3097 each failed attempt, each of which will cause a PR_SYSEXIT
3098 stop, and we won't know how to distinguish the PR_SYSEXIT's
3099 for these failed execs with the ones for successful execs
3100 (whether the exec has succeeded is stored at that time in the
3101 carry bit or some such architecture-specific and
3102 non-ABI-specified place).
3104 So I can't think of anything better than to search the PATH
3105 now. This has several disadvantages: (1) There is a race
3106 condition; if we find a file now and it is deleted before we
3107 exec it, we lose, even if the deletion leaves a valid file
3108 further down in the PATH, (2) there is no way to know exactly
3109 what an executable (in the sense of "capable of being
3110 exec'd") file is. Using access() loses because it may lose
3111 if the caller is the superuser; failing to use it loses if
3112 there are ACLs or some such. */
3116 /* FIXME-maybe: might want "set path" command so user can change what
3117 path is used from within GDB. */
3118 const char *path
= getenv ("PATH");
3120 struct stat statbuf
;
3123 path
= "/bin:/usr/bin";
3125 tryname
= (char *) alloca (strlen (path
) + strlen (shell_file
) + 2);
3126 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
3128 p1
= strchr (p
, ':');
3133 strncpy (tryname
, p
, len
);
3134 tryname
[len
] = '\0';
3135 strcat (tryname
, "/");
3136 strcat (tryname
, shell_file
);
3137 if (access (tryname
, X_OK
) < 0)
3139 if (stat (tryname
, &statbuf
) < 0)
3141 if (!S_ISREG (statbuf
.st_mode
))
3142 /* We certainly need to reject directories. I'm not quite
3143 as sure about FIFOs, sockets, etc., but I kind of doubt
3144 that people want to exec() these things. */
3149 /* Not found. This must be an error rather than merely passing
3150 the file to execlp(), because execlp() would try all the
3151 exec()s, causing GDB to get confused. */
3152 error (_("procfs:%d -- Can't find shell %s in PATH"),
3153 __LINE__
, shell_file
);
3155 shell_file
= tryname
;
3158 pid
= fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
3159 NULL
, NULL
, shell_file
, NULL
);
3161 /* We have something that executes now. We'll be running through
3162 the shell at this point (if startup-with-shell is true), but the
3163 pid shouldn't change. */
3164 add_thread_silent (pid_to_ptid (pid
));
3166 procfs_init_inferior (ops
, pid
);
3169 /* An observer for the "inferior_created" event. */
3172 procfs_inferior_created (struct target_ops
*ops
, int from_tty
)
3176 /* Callback for update_thread_list. Calls "add_thread". */
3179 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
3181 ptid_t gdb_threadid
= ptid_build (pi
->pid
, thread
->tid
, 0);
3183 if (!in_thread_list (gdb_threadid
) || is_exited (gdb_threadid
))
3184 add_thread (gdb_threadid
);
3189 /* Query all the threads that the target knows about, and give them
3190 back to GDB to add to its list. */
3193 procfs_update_thread_list (struct target_ops
*ops
)
3199 /* Find procinfo for main process. */
3200 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
3201 proc_update_threads (pi
);
3202 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
3205 /* Return true if the thread is still 'alive'. This guy doesn't
3206 really seem to be doing his job. Got to investigate how to tell
3207 when a thread is really gone. */
3210 procfs_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
3215 proc
= ptid_get_pid (ptid
);
3216 thread
= ptid_get_lwp (ptid
);
3217 /* If I don't know it, it ain't alive! */
3218 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
3221 /* If I can't get its status, it ain't alive!
3222 What's more, I need to forget about it! */
3223 if (!proc_get_status (pi
))
3225 destroy_procinfo (pi
);
3228 /* I couldn't have got its status if it weren't alive, so it's
3233 /* Convert PTID to a string. Returns the string in a static
3237 procfs_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
3239 static char buf
[80];
3241 if (ptid_get_lwp (ptid
) == 0)
3242 sprintf (buf
, "process %d", ptid_get_pid (ptid
));
3244 sprintf (buf
, "LWP %ld", ptid_get_lwp (ptid
));
3249 /* Insert a watchpoint. */
3252 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
3258 pi
= find_procinfo_or_die (ptid_get_pid (ptid
) == -1 ?
3259 ptid_get_pid (inferior_ptid
) : ptid_get_pid (ptid
),
3262 /* Translate from GDB's flags to /proc's. */
3263 if (len
> 0) /* len == 0 means delete watchpoint. */
3265 switch (rwflag
) { /* FIXME: need an enum! */
3266 case hw_write
: /* default watchpoint (write) */
3267 pflags
= WRITE_WATCHFLAG
;
3269 case hw_read
: /* read watchpoint */
3270 pflags
= READ_WATCHFLAG
;
3272 case hw_access
: /* access watchpoint */
3273 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
3275 case hw_execute
: /* execution HW breakpoint */
3276 pflags
= EXEC_WATCHFLAG
;
3278 default: /* Something weird. Return error. */
3281 if (after
) /* Stop after r/w access is completed. */
3282 pflags
|= AFTER_WATCHFLAG
;
3285 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
3287 if (errno
== E2BIG
) /* Typical error for no resources. */
3288 return -1; /* fail */
3289 /* GDB may try to remove the same watchpoint twice.
3290 If a remove request returns no match, don't error. */
3291 if (errno
== ESRCH
&& len
== 0)
3292 return 0; /* ignore */
3293 proc_error (pi
, "set_watchpoint", __LINE__
);
3298 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
3299 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
3300 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
3303 Note: procfs_can_use_hw_breakpoint() is not yet used by all
3304 procfs.c targets due to the fact that some of them still define
3305 target_can_use_hardware_watchpoint. */
3308 procfs_can_use_hw_breakpoint (struct target_ops
*self
,
3310 int cnt
, int othertype
)
3312 /* Due to the way that proc_set_watchpoint() is implemented, host
3313 and target pointers must be of the same size. If they are not,
3314 we can't use hardware watchpoints. This limitation is due to the
3315 fact that proc_set_watchpoint() calls
3316 procfs_address_to_host_pointer(); a close inspection of
3317 procfs_address_to_host_pointer will reveal that an internal error
3318 will be generated when the host and target pointer sizes are
3320 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
3322 if (sizeof (void *) != TYPE_LENGTH (ptr_type
))
3325 /* Other tests here??? */
3330 /* Returns non-zero if process is stopped on a hardware watchpoint
3331 fault, else returns zero. */
3334 procfs_stopped_by_watchpoint (struct target_ops
*ops
)
3338 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
3340 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3342 if (proc_why (pi
) == PR_FAULTED
)
3344 if (proc_what (pi
) == FLTWATCH
)
3351 /* Returns 1 if the OS knows the position of the triggered watchpoint,
3352 and sets *ADDR to that address. Returns 0 if OS cannot report that
3353 address. This function is only called if
3354 procfs_stopped_by_watchpoint returned 1, thus no further checks are
3355 done. The function also assumes that ADDR is not NULL. */
3358 procfs_stopped_data_address (struct target_ops
*targ
, CORE_ADDR
*addr
)
3362 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
3363 return proc_watchpoint_address (pi
, addr
);
3367 procfs_insert_watchpoint (struct target_ops
*self
,
3368 CORE_ADDR addr
, int len
,
3369 enum target_hw_bp_type type
,
3370 struct expression
*cond
)
3372 if (!target_have_steppable_watchpoint
3373 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
3375 /* When a hardware watchpoint fires off the PC will be left at
3376 the instruction following the one which caused the
3377 watchpoint. It will *NOT* be necessary for GDB to step over
3379 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 1);
3383 /* When a hardware watchpoint fires off the PC will be left at
3384 the instruction which caused the watchpoint. It will be
3385 necessary for GDB to step over the watchpoint. */
3386 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 0);
3391 procfs_remove_watchpoint (struct target_ops
*self
,
3392 CORE_ADDR addr
, int len
,
3393 enum target_hw_bp_type type
,
3394 struct expression
*cond
)
3396 return procfs_set_watchpoint (inferior_ptid
, addr
, 0, 0, 0);
3400 procfs_region_ok_for_hw_watchpoint (struct target_ops
*self
,
3401 CORE_ADDR addr
, int len
)
3403 /* The man page for proc(4) on Solaris 2.6 and up says that the
3404 system can support "thousands" of hardware watchpoints, but gives
3405 no method for finding out how many; It doesn't say anything about
3406 the allowed size for the watched area either. So we just tell
3411 /* Memory Mappings Functions: */
3413 /* Call a callback function once for each mapping, passing it the
3414 mapping, an optional secondary callback function, and some optional
3415 opaque data. Quit and return the first non-zero value returned
3418 PI is the procinfo struct for the process to be mapped. FUNC is
3419 the callback function to be called by this iterator. DATA is the
3420 optional opaque data to be passed to the callback function.
3421 CHILD_FUNC is the optional secondary function pointer to be passed
3422 to the child function. Returns the first non-zero return value
3423 from the callback function, or zero. */
3426 iterate_over_mappings (procinfo
*pi
, find_memory_region_ftype child_func
,
3428 int (*func
) (struct prmap
*map
,
3429 find_memory_region_ftype child_func
,
3432 char pathname
[MAX_PROC_NAME_SIZE
];
3433 struct prmap
*prmaps
;
3434 struct prmap
*prmap
;
3439 /* Get the number of mappings, allocate space,
3440 and read the mappings into prmaps. */
3442 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
3444 scoped_fd
map_fd (open (pathname
, O_RDONLY
));
3445 if (map_fd
.get () < 0)
3446 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
3448 /* Use stat to determine the file size, and compute
3449 the number of prmap_t objects it contains. */
3450 if (fstat (map_fd
.get (), &sbuf
) != 0)
3451 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
3453 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
3454 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
3455 if (read (map_fd
.get (), (char *) prmaps
, nmap
* sizeof (*prmaps
))
3456 != (nmap
* sizeof (*prmaps
)))
3457 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
3459 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
3460 if ((funcstat
= (*func
) (prmap
, child_func
, data
)) != 0)
3466 /* Implements the to_find_memory_regions method. Calls an external
3467 function for each memory region.
3468 Returns the integer value returned by the callback. */
3471 find_memory_regions_callback (struct prmap
*map
,
3472 find_memory_region_ftype func
, void *data
)
3474 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
3476 (map
->pr_mflags
& MA_READ
) != 0,
3477 (map
->pr_mflags
& MA_WRITE
) != 0,
3478 (map
->pr_mflags
& MA_EXEC
) != 0,
3479 1, /* MODIFIED is unknown, pass it as true. */
3483 /* External interface. Calls a callback function once for each
3484 mapped memory region in the child process, passing as arguments:
3486 CORE_ADDR virtual_address,
3488 int read, TRUE if region is readable by the child
3489 int write, TRUE if region is writable by the child
3490 int execute TRUE if region is executable by the child.
3492 Stops iterating and returns the first non-zero value returned by
3496 proc_find_memory_regions (struct target_ops
*self
,
3497 find_memory_region_ftype func
, void *data
)
3499 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
3501 return iterate_over_mappings (pi
, func
, data
,
3502 find_memory_regions_callback
);
3505 /* Returns an ascii representation of a memory mapping's flags. */
3508 mappingflags (long flags
)
3510 static char asciiflags
[8];
3512 strcpy (asciiflags
, "-------");
3513 if (flags
& MA_STACK
)
3514 asciiflags
[1] = 's';
3515 if (flags
& MA_BREAK
)
3516 asciiflags
[2] = 'b';
3517 if (flags
& MA_SHARED
)
3518 asciiflags
[3] = 's';
3519 if (flags
& MA_READ
)
3520 asciiflags
[4] = 'r';
3521 if (flags
& MA_WRITE
)
3522 asciiflags
[5] = 'w';
3523 if (flags
& MA_EXEC
)
3524 asciiflags
[6] = 'x';
3525 return (asciiflags
);
3528 /* Callback function, does the actual work for 'info proc
3532 info_mappings_callback (struct prmap
*map
, find_memory_region_ftype ignore
,
3535 unsigned int pr_off
;
3537 pr_off
= (unsigned int) map
->pr_offset
;
3539 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
3540 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
3541 (unsigned long) map
->pr_vaddr
,
3542 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3543 (unsigned long) map
->pr_size
,
3545 mappingflags (map
->pr_mflags
));
3547 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
3548 (unsigned long) map
->pr_vaddr
,
3549 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
3550 (unsigned long) map
->pr_size
,
3552 mappingflags (map
->pr_mflags
));
3557 /* Implement the "info proc mappings" subcommand. */
3560 info_proc_mappings (procinfo
*pi
, int summary
)
3563 return; /* No output for summary mode. */
3565 printf_filtered (_("Mapped address spaces:\n\n"));
3566 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
3567 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
3574 printf_filtered (" %18s %18s %10s %10s %7s\n",
3581 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
3582 printf_filtered ("\n");
3585 /* Implement the "info proc" command. */
3588 procfs_info_proc (struct target_ops
*ops
, const char *args
,
3589 enum info_proc_what what
)
3591 struct cleanup
*old_chain
;
3592 procinfo
*process
= NULL
;
3593 procinfo
*thread
= NULL
;
3610 error (_("Not supported on this target."));
3613 old_chain
= make_cleanup (null_cleanup
, 0);
3614 gdb_argv
built_argv (args
);
3615 for (char *arg
: built_argv
)
3617 if (isdigit (arg
[0]))
3619 pid
= strtoul (arg
, &tmp
, 10);
3621 tid
= strtoul (++tmp
, NULL
, 10);
3623 else if (arg
[0] == '/')
3625 tid
= strtoul (arg
+ 1, NULL
, 10);
3629 pid
= ptid_get_pid (inferior_ptid
);
3631 error (_("No current process: you must name one."));
3634 /* Have pid, will travel.
3635 First see if it's a process we're already debugging. */
3636 process
= find_procinfo (pid
, 0);
3637 if (process
== NULL
)
3639 /* No. So open a procinfo for it, but
3640 remember to close it again when finished. */
3641 process
= create_procinfo (pid
, 0);
3642 make_cleanup (do_destroy_procinfo_cleanup
, process
);
3643 if (!open_procinfo_files (process
, FD_CTL
))
3644 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
3648 thread
= create_procinfo (pid
, tid
);
3652 printf_filtered (_("process %d flags:\n"), process
->pid
);
3653 proc_prettyprint_flags (proc_flags (process
), 1);
3654 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
3655 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
3656 if (proc_get_nthreads (process
) > 1)
3657 printf_filtered ("Process has %d threads.\n",
3658 proc_get_nthreads (process
));
3662 printf_filtered (_("thread %d flags:\n"), thread
->tid
);
3663 proc_prettyprint_flags (proc_flags (thread
), 1);
3664 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
3665 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
3670 info_proc_mappings (process
, 0);
3673 do_cleanups (old_chain
);
3676 /* Modify the status of the system call identified by SYSCALLNUM in
3677 the set of syscalls that are currently traced/debugged.
3679 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
3680 will be updated. Otherwise, the exit syscalls set will be updated.
3682 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
3683 will be disabled. */
3686 proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
, int entry_or_exit
,
3687 int mode
, int from_tty
)
3691 if (entry_or_exit
== PR_SYSENTRY
)
3692 sysset
= proc_get_traced_sysentry (pi
, NULL
);
3694 sysset
= proc_get_traced_sysexit (pi
, NULL
);
3697 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
3699 if (mode
== FLAG_SET
)
3700 praddset (sysset
, syscallnum
);
3702 prdelset (sysset
, syscallnum
);
3704 if (entry_or_exit
== PR_SYSENTRY
)
3706 if (!proc_set_traced_sysentry (pi
, sysset
))
3707 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
3711 if (!proc_set_traced_sysexit (pi
, sysset
))
3712 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
3717 proc_trace_syscalls (const char *args
, int from_tty
, int entry_or_exit
, int mode
)
3721 if (ptid_get_pid (inferior_ptid
) <= 0)
3722 error (_("you must be debugging a process to use this command."));
3724 if (args
== NULL
|| args
[0] == 0)
3725 error_no_arg (_("system call to trace"));
3727 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
3728 if (isdigit (args
[0]))
3730 const int syscallnum
= atoi (args
);
3732 proc_trace_syscalls_1 (pi
, syscallnum
, entry_or_exit
, mode
, from_tty
);
3737 proc_trace_sysentry_cmd (const char *args
, int from_tty
)
3739 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
3743 proc_trace_sysexit_cmd (const char *args
, int from_tty
)
3745 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
3749 proc_untrace_sysentry_cmd (const char *args
, int from_tty
)
3751 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
3755 proc_untrace_sysexit_cmd (const char *args
, int from_tty
)
3757 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
3761 _initialize_procfs (void)
3763 gdb::observers::inferior_created
.attach (procfs_inferior_created
);
3765 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
3766 _("Give a trace of entries into the syscall."));
3767 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
3768 _("Give a trace of exits from the syscall."));
3769 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
3770 _("Cancel a trace of entries into the syscall."));
3771 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
3772 _("Cancel a trace of exits from the syscall."));
3775 /* =================== END, GDB "MODULE" =================== */
3779 /* miscellaneous stubs: */
3781 /* The following satisfy a few random symbols mostly created by the
3782 solaris threads implementation, which I will chase down later. */
3784 /* Return a pid for which we guarantee we will be able to find a
3788 procfs_first_available (void)
3790 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);
3793 /* =================== GCORE .NOTE "MODULE" =================== */
3796 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
3797 char *note_data
, int *note_size
,
3798 enum gdb_signal stop_signal
)
3800 struct regcache
*regcache
= get_thread_regcache (ptid
);
3801 gdb_gregset_t gregs
;
3802 gdb_fpregset_t fpregs
;
3803 unsigned long merged_pid
;
3805 merged_pid
= ptid_get_lwp (ptid
) << 16 | ptid_get_pid (ptid
);
3807 /* This part is the old method for fetching registers.
3808 It should be replaced by the newer one using regsets
3809 once it is implemented in this platform:
3810 gdbarch_iterate_over_regset_sections(). */
3812 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
3813 inferior_ptid
= ptid
;
3814 target_fetch_registers (regcache
, -1);
3816 fill_gregset (regcache
, &gregs
, -1);
3817 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
3823 fill_fpregset (regcache
, &fpregs
, -1);
3824 note_data
= (char *) elfcore_write_prfpreg (obfd
,
3833 struct procfs_corefile_thread_data
{
3837 enum gdb_signal stop_signal
;
3841 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
3843 struct procfs_corefile_thread_data
*args
3844 = (struct procfs_corefile_thread_data
*) data
;
3848 ptid_t ptid
= ptid_build (pi
->pid
, thread
->tid
, 0);
3850 args
->note_data
= procfs_do_thread_registers (args
->obfd
, ptid
,
3859 find_signalled_thread (struct thread_info
*info
, void *data
)
3861 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
3862 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
3868 static enum gdb_signal
3869 find_stop_signal (void)
3871 struct thread_info
*info
=
3872 iterate_over_threads (find_signalled_thread
, NULL
);
3875 return info
->suspend
.stop_signal
;
3877 return GDB_SIGNAL_0
;
3881 procfs_make_note_section (struct target_ops
*self
, bfd
*obfd
, int *note_size
)
3883 struct cleanup
*old_chain
;
3884 gdb_gregset_t gregs
;
3885 gdb_fpregset_t fpregs
;
3886 char fname
[16] = {'\0'};
3887 char psargs
[80] = {'\0'};
3888 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
3889 char *note_data
= NULL
;
3891 struct procfs_corefile_thread_data thread_args
;
3894 enum gdb_signal stop_signal
;
3896 if (get_exec_file (0))
3898 strncpy (fname
, lbasename (get_exec_file (0)), sizeof (fname
));
3899 fname
[sizeof (fname
) - 1] = 0;
3900 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
3901 psargs
[sizeof (psargs
) - 1] = 0;
3903 inf_args
= get_inferior_args ();
3904 if (inf_args
&& *inf_args
&&
3905 strlen (inf_args
) < ((int) sizeof (psargs
) - (int) strlen (psargs
)))
3907 strncat (psargs
, " ",
3908 sizeof (psargs
) - strlen (psargs
));
3909 strncat (psargs
, inf_args
,
3910 sizeof (psargs
) - strlen (psargs
));
3914 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
3920 stop_signal
= find_stop_signal ();
3922 fill_gregset (get_current_regcache (), &gregs
, -1);
3923 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
3924 ptid_get_pid (inferior_ptid
),
3925 stop_signal
, &gregs
);
3927 thread_args
.obfd
= obfd
;
3928 thread_args
.note_data
= note_data
;
3929 thread_args
.note_size
= note_size
;
3930 thread_args
.stop_signal
= stop_signal
;
3931 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
,
3933 note_data
= thread_args
.note_data
;
3935 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
3939 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
3940 "CORE", NT_AUXV
, auxv
, auxv_len
);
3946 /* =================== END GCORE .NOTE "MODULE" =================== */