1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
3 Copyright (C) 1999-2017 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 "filestuff.h"
35 #if defined (NEW_PROC_API)
36 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
39 #include <sys/procfs.h>
40 #ifdef HAVE_SYS_FAULT_H
41 #include <sys/fault.h>
43 #ifdef HAVE_SYS_SYSCALL_H
44 #include <sys/syscall.h>
55 /* This module provides the interface between GDB and the
56 /proc file system, which is used on many versions of Unix
57 as a means for debuggers to control other processes.
59 Examples of the systems that use this interface are:
66 /proc works by imitating a file system: you open a simulated file
67 that represents the process you wish to interact with, and perform
68 operations on that "file" in order to examine or change the state
71 The most important thing to know about /proc and this module is
72 that there are two very different interfaces to /proc:
74 One that uses the ioctl system call, and another that uses read
75 and write system calls.
77 This module has to support both /proc interfaces. This means that
78 there are two different ways of doing every basic operation.
80 In order to keep most of the code simple and clean, I have defined
81 an interface "layer" which hides all these system calls. An ifdef
82 (NEW_PROC_API) determines which interface we are using, and most or
83 all occurrances of this ifdef should be confined to this interface
86 /* Determine which /proc API we are using: The ioctl API defines
87 PIOCSTATUS, while the read/write (multiple fd) API never does. */
90 #include <sys/types.h>
91 #include <dirent.h> /* opendir/readdir, for listing the LWP's */
94 #include <fcntl.h> /* for O_RDONLY */
95 #include <unistd.h> /* for "X_OK" */
96 #include <sys/stat.h> /* for struct stat */
98 /* Note: procfs-utils.h must be included after the above system header
99 files, because it redefines various system calls using macros.
100 This may be incompatible with the prototype declarations. */
102 #include "proc-utils.h"
104 /* Prototypes for supply_gregset etc. */
107 /* =================== TARGET_OPS "MODULE" =================== */
109 /* This module defines the GDB target vector and its methods. */
111 static void procfs_attach (struct target_ops
*, const char *, int);
112 static void procfs_detach (struct target_ops
*, const char *, int);
113 static void procfs_resume (struct target_ops
*,
114 ptid_t
, int, enum gdb_signal
);
115 static void procfs_interrupt (struct target_ops
*self
, ptid_t
);
116 static void procfs_files_info (struct target_ops
*);
117 static void procfs_fetch_registers (struct target_ops
*,
118 struct regcache
*, int);
119 static void procfs_store_registers (struct target_ops
*,
120 struct regcache
*, int);
121 static void procfs_pass_signals (struct target_ops
*self
,
122 int, unsigned char *);
123 static void procfs_kill_inferior (struct target_ops
*ops
);
124 static void procfs_mourn_inferior (struct target_ops
*ops
);
125 static void procfs_create_inferior (struct target_ops
*, const char *,
126 const std::string
&, char **, int);
127 static ptid_t
procfs_wait (struct target_ops
*,
128 ptid_t
, struct target_waitstatus
*, int);
129 static enum target_xfer_status
procfs_xfer_memory (gdb_byte
*,
133 static target_xfer_partial_ftype procfs_xfer_partial
;
135 static int procfs_thread_alive (struct target_ops
*ops
, ptid_t
);
137 static void procfs_update_thread_list (struct target_ops
*ops
);
138 static const char *procfs_pid_to_str (struct target_ops
*, ptid_t
);
140 static int proc_find_memory_regions (struct target_ops
*self
,
141 find_memory_region_ftype
, void *);
143 static char * procfs_make_note_section (struct target_ops
*self
,
146 static int procfs_can_use_hw_breakpoint (struct target_ops
*self
,
147 enum bptype
, int, int);
149 static void procfs_info_proc (struct target_ops
*, const char *,
150 enum info_proc_what
);
152 #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
153 /* When GDB is built as 64-bit application on Solaris, the auxv data
154 is presented in 64-bit format. We need to provide a custom parser
157 procfs_auxv_parse (struct target_ops
*ops
, gdb_byte
**readptr
,
158 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
160 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
161 gdb_byte
*ptr
= *readptr
;
166 if (endptr
- ptr
< 8 * 2)
169 *typep
= extract_unsigned_integer (ptr
, 4, byte_order
);
171 /* The size of data is always 64-bit. If the application is 32-bit,
172 it will be zero extended, as expected. */
173 *valp
= extract_unsigned_integer (ptr
, 8, byte_order
);
184 struct target_ops
*t
= inf_child_target ();
186 t
->to_create_inferior
= procfs_create_inferior
;
187 t
->to_kill
= procfs_kill_inferior
;
188 t
->to_mourn_inferior
= procfs_mourn_inferior
;
189 t
->to_attach
= procfs_attach
;
190 t
->to_detach
= procfs_detach
;
191 t
->to_wait
= procfs_wait
;
192 t
->to_resume
= procfs_resume
;
193 t
->to_fetch_registers
= procfs_fetch_registers
;
194 t
->to_store_registers
= procfs_store_registers
;
195 t
->to_xfer_partial
= procfs_xfer_partial
;
196 t
->to_pass_signals
= procfs_pass_signals
;
197 t
->to_files_info
= procfs_files_info
;
198 t
->to_interrupt
= procfs_interrupt
;
200 t
->to_update_thread_list
= procfs_update_thread_list
;
201 t
->to_thread_alive
= procfs_thread_alive
;
202 t
->to_pid_to_str
= procfs_pid_to_str
;
204 t
->to_has_thread_control
= tc_schedlock
;
205 t
->to_find_memory_regions
= proc_find_memory_regions
;
206 t
->to_make_corefile_notes
= procfs_make_note_section
;
207 t
->to_info_proc
= procfs_info_proc
;
209 #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
210 t
->to_auxv_parse
= procfs_auxv_parse
;
213 t
->to_magic
= OPS_MAGIC
;
218 /* =================== END, TARGET_OPS "MODULE" =================== */
220 /* World Unification:
222 Put any typedefs, defines etc. here that are required for the
223 unification of code that handles different versions of /proc. */
225 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
227 enum { READ_WATCHFLAG
= WA_READ
,
228 WRITE_WATCHFLAG
= WA_WRITE
,
229 EXEC_WATCHFLAG
= WA_EXEC
,
230 AFTER_WATCHFLAG
= WA_TRAPAFTER
233 #else /* Irix method for watchpoints */
234 enum { READ_WATCHFLAG
= MA_READ
,
235 WRITE_WATCHFLAG
= MA_WRITE
,
236 EXEC_WATCHFLAG
= MA_EXEC
,
237 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
242 #ifdef HAVE_PR_SIGSET_T
243 typedef pr_sigset_t gdb_sigset_t
;
245 typedef sigset_t gdb_sigset_t
;
249 #ifdef HAVE_PR_SIGACTION64_T
250 typedef pr_sigaction64_t gdb_sigaction_t
;
252 typedef struct sigaction gdb_sigaction_t
;
256 #ifdef HAVE_PR_SIGINFO64_T
257 typedef pr_siginfo64_t gdb_siginfo_t
;
259 typedef siginfo_t gdb_siginfo_t
;
262 /* On mips-irix, praddset and prdelset are defined in such a way that
263 they return a value, which causes GCC to emit a -Wunused error
264 because the returned value is not used. Prevent this warning
265 by casting the return value to void. On sparc-solaris, this issue
266 does not exist because the definition of these macros already include
267 that cast to void. */
268 #define gdb_praddset(sp, flag) ((void) praddset (sp, flag))
269 #define gdb_prdelset(sp, flag) ((void) prdelset (sp, flag))
271 /* gdb_premptysysset */
273 #define gdb_premptysysset premptysysset
275 #define gdb_premptysysset premptyset
280 #define gdb_praddsysset praddsysset
282 #define gdb_praddsysset gdb_praddset
287 #define gdb_prdelsysset prdelsysset
289 #define gdb_prdelsysset gdb_prdelset
292 /* prissyssetmember */
293 #ifdef prissyssetmember
294 #define gdb_pr_issyssetmember prissyssetmember
296 #define gdb_pr_issyssetmember prismember
299 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
300 as intuitively descriptive as it could be, so we'll define
301 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
302 this writing, this feature is only found on AIX5 systems and
303 basically means that the set of syscalls is not fixed. I.e,
304 there's no nice table that one can #include to get all of the
305 syscall numbers. Instead, they're stored in /proc/PID/sysent
306 for each process. We are at least guaranteed that they won't
307 change over the lifetime of the process. But each process could
308 (in theory) have different syscall numbers. */
309 #ifdef HAVE_PRSYSENT_T
310 #define DYNAMIC_SYSCALLS
315 /* =================== STRUCT PROCINFO "MODULE" =================== */
317 /* FIXME: this comment will soon be out of date W.R.T. threads. */
319 /* The procinfo struct is a wrapper to hold all the state information
320 concerning a /proc process. There should be exactly one procinfo
321 for each process, and since GDB currently can debug only one
322 process at a time, that means there should be only one procinfo.
323 All of the LWP's of a process can be accessed indirectly thru the
324 single process procinfo.
326 However, against the day when GDB may debug more than one process,
327 this data structure is kept in a list (which for now will hold no
328 more than one member), and many functions will have a pointer to a
329 procinfo as an argument.
331 There will be a separate procinfo structure for use by the (not yet
332 implemented) "info proc" command, so that we can print useful
333 information about any random process without interfering with the
334 inferior's procinfo information. */
337 /* format strings for /proc paths */
338 # ifndef CTL_PROC_NAME_FMT
339 # define MAIN_PROC_NAME_FMT "/proc/%d"
340 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
341 # define AS_PROC_NAME_FMT "/proc/%d/as"
342 # define MAP_PROC_NAME_FMT "/proc/%d/map"
343 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
344 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
346 /* the name of the proc status struct depends on the implementation */
347 typedef pstatus_t gdb_prstatus_t
;
348 typedef lwpstatus_t gdb_lwpstatus_t
;
349 #else /* ! NEW_PROC_API */
350 /* format strings for /proc paths */
351 # ifndef CTL_PROC_NAME_FMT
352 # define MAIN_PROC_NAME_FMT "/proc/%05d"
353 # define CTL_PROC_NAME_FMT "/proc/%05d"
354 # define AS_PROC_NAME_FMT "/proc/%05d"
355 # define MAP_PROC_NAME_FMT "/proc/%05d"
356 # define STATUS_PROC_NAME_FMT "/proc/%05d"
357 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
359 /* The name of the proc status struct depends on the implementation. */
360 typedef prstatus_t gdb_prstatus_t
;
361 typedef prstatus_t gdb_lwpstatus_t
;
362 #endif /* NEW_PROC_API */
364 typedef struct procinfo
{
365 struct procinfo
*next
;
366 int pid
; /* Process ID */
367 int tid
; /* Thread/LWP id */
371 int ignore_next_sigstop
;
373 /* The following four fd fields may be identical, or may contain
374 several different fd's, depending on the version of /proc
375 (old ioctl or new read/write). */
377 int ctl_fd
; /* File descriptor for /proc control file */
379 /* The next three file descriptors are actually only needed in the
380 read/write, multiple-file-descriptor implemenation
381 (NEW_PROC_API). However, to avoid a bunch of #ifdefs in the
382 code, we will use them uniformly by (in the case of the ioctl
383 single-file-descriptor implementation) filling them with copies
384 of the control fd. */
385 int status_fd
; /* File descriptor for /proc status file */
386 int as_fd
; /* File descriptor for /proc as file */
388 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
390 fltset_t saved_fltset
; /* Saved traced hardware fault set */
391 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
392 gdb_sigset_t saved_sighold
; /* Saved held signal set */
393 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
394 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
396 gdb_prstatus_t prstatus
; /* Current process status info */
399 gdb_fpregset_t fpregset
; /* Current floating point registers */
402 #ifdef DYNAMIC_SYSCALLS
403 int num_syscalls
; /* Total number of syscalls */
404 char **syscall_names
; /* Syscall number to name map */
407 struct procinfo
*thread_list
;
409 int status_valid
: 1;
411 int fpregs_valid
: 1;
412 int threads_valid
: 1;
415 static char errmsg
[128]; /* shared error msg buffer */
417 /* Function prototypes for procinfo module: */
419 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
420 static procinfo
*find_procinfo (int pid
, int tid
);
421 static procinfo
*create_procinfo (int pid
, int tid
);
422 static void destroy_procinfo (procinfo
* p
);
423 static void do_destroy_procinfo_cleanup (void *);
424 static void dead_procinfo (procinfo
* p
, const char *msg
, int killp
);
425 static int open_procinfo_files (procinfo
* p
, int which
);
426 static void close_procinfo_files (procinfo
* p
);
427 static int sysset_t_size (procinfo
*p
);
428 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
429 #ifdef DYNAMIC_SYSCALLS
430 static void load_syscalls (procinfo
*pi
);
431 static void free_syscalls (procinfo
*pi
);
432 static int find_syscall (procinfo
*pi
, const char *name
);
433 #endif /* DYNAMIC_SYSCALLS */
435 static int iterate_over_mappings
436 (procinfo
*pi
, find_memory_region_ftype child_func
, void *data
,
437 int (*func
) (struct prmap
*map
, find_memory_region_ftype child_func
,
440 /* The head of the procinfo list: */
441 static procinfo
* procinfo_list
;
443 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
447 find_procinfo (int pid
, int tid
)
451 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
458 /* Don't check threads_valid. If we're updating the
459 thread_list, we want to find whatever threads are already
460 here. This means that in general it is the caller's
461 responsibility to check threads_valid and update before
462 calling find_procinfo, if the caller wants to find a new
465 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
473 /* Calls find_procinfo, but errors on failure. */
476 find_procinfo_or_die (int pid
, int tid
)
478 procinfo
*pi
= find_procinfo (pid
, tid
);
483 error (_("procfs: couldn't find pid %d "
484 "(kernel thread %d) in procinfo list."),
487 error (_("procfs: couldn't find pid %d in procinfo list."), pid
);
492 /* Wrapper for `open'. The appropriate open call is attempted; if
493 unsuccessful, it will be retried as many times as needed for the
494 EAGAIN and EINTR conditions.
496 For other conditions, retry the open a limited number of times. In
497 addition, a short sleep is imposed prior to retrying the open. The
498 reason for this sleep is to give the kernel a chance to catch up
499 and create the file in question in the event that GDB "wins" the
500 race to open a file before the kernel has created it. */
503 open_with_retry (const char *pathname
, int flags
)
505 int retries_remaining
, status
;
507 retries_remaining
= 2;
511 status
= open (pathname
, flags
);
513 if (status
>= 0 || retries_remaining
== 0)
515 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
525 /* Open the file descriptor for the process or LWP. If NEW_PROC_API
526 is defined, we only open the control file descriptor; the others
527 are opened lazily as needed. Otherwise (if not NEW_PROC_API),
528 there is only one real file descriptor, but we keep multiple copies
529 of it so that the code that uses them does not have to be #ifdef'd.
530 Returns the file descriptor, or zero for failure. */
532 enum { FD_CTL
, FD_STATUS
, FD_AS
};
535 open_procinfo_files (procinfo
*pi
, int which
)
538 char tmp
[MAX_PROC_NAME_SIZE
];
542 /* This function is getting ALMOST long enough to break up into
543 several. Here is some rationale:
545 NEW_PROC_API (Solaris 2.6, Solaris 2.7):
546 There are several file descriptors that may need to be open
547 for any given process or LWP. The ones we're intereted in are:
548 - control (ctl) write-only change the state
549 - status (status) read-only query the state
550 - address space (as) read/write access memory
551 - map (map) read-only virtual addr map
552 Most of these are opened lazily as they are needed.
553 The pathnames for the 'files' for an LWP look slightly
554 different from those of a first-class process:
555 Pathnames for a process (<proc-id>):
557 /proc/<proc-id>/status
560 Pathnames for an LWP (lwp-id):
561 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
562 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
563 An LWP has no map or address space file descriptor, since
564 the memory map and address space are shared by all LWPs.
566 Everyone else (Solaris 2.5, Irix, OSF)
567 There is only one file descriptor for each process or LWP.
568 For convenience, we copy the same file descriptor into all
569 three fields of the procinfo struct (ctl_fd, status_fd, and
570 as_fd, see NEW_PROC_API above) so that code that uses them
571 doesn't need any #ifdef's.
576 Each LWP has an independent file descriptor, but these
577 are not obtained via the 'open' system call like the rest:
578 instead, they're obtained thru an ioctl call (PIOCOPENLWP)
579 to the file descriptor of the parent process.
582 These do not even have their own independent file descriptor.
583 All operations are carried out on the file descriptor of the
584 parent process. Therefore we just call open again for each
585 thread, getting a new handle for the same 'file'. */
588 /* In this case, there are several different file descriptors that
589 we might be asked to open. The control file descriptor will be
590 opened early, but the others will be opened lazily as they are
593 strcpy (tmp
, pi
->pathname
);
594 switch (which
) { /* Which file descriptor to open? */
597 strcat (tmp
, "/lwpctl");
599 strcat (tmp
, "/ctl");
600 fd
= open_with_retry (tmp
, O_WRONLY
);
607 return 0; /* There is no 'as' file descriptor for an lwp. */
609 fd
= open_with_retry (tmp
, O_RDWR
);
616 strcat (tmp
, "/lwpstatus");
618 strcat (tmp
, "/status");
619 fd
= open_with_retry (tmp
, O_RDONLY
);
625 return 0; /* unknown file descriptor */
627 #else /* not NEW_PROC_API */
628 /* In this case, there is only one file descriptor for each procinfo
629 (ie. each process or LWP). In fact, only the file descriptor for
630 the process can actually be opened by an 'open' system call. The
631 ones for the LWPs have to be obtained thru an IOCTL call on the
632 process's file descriptor.
634 For convenience, we copy each procinfo's single file descriptor
635 into all of the fields occupied by the several file descriptors
636 of the NEW_PROC_API implementation. That way, the code that uses
637 them can be written without ifdefs. */
640 #ifdef PIOCTSTATUS /* OSF */
641 /* Only one FD; just open it. */
642 if ((fd
= open_with_retry (pi
->pathname
, O_RDWR
)) < 0)
644 #else /* Sol 2.5, Irix, other? */
645 if (pi
->tid
== 0) /* Master procinfo for the process */
647 fd
= open_with_retry (pi
->pathname
, O_RDWR
);
651 else /* LWP thread procinfo */
653 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
657 /* Find the procinfo for the entire process. */
658 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
661 /* Now obtain the file descriptor for the LWP. */
662 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) < 0)
664 #else /* Irix, other? */
665 return 0; /* Don't know how to open threads. */
666 #endif /* Sol 2.5 PIOCOPENLWP */
668 #endif /* OSF PIOCTSTATUS */
669 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
670 #endif /* NEW_PROC_API */
672 return 1; /* success */
675 /* Allocate a data structure and link it into the procinfo list.
676 First tries to find a pre-existing one (FIXME: why?). Returns the
677 pointer to new procinfo struct. */
680 create_procinfo (int pid
, int tid
)
682 procinfo
*pi
, *parent
= NULL
;
684 if ((pi
= find_procinfo (pid
, tid
)))
685 return pi
; /* Already exists, nothing to do. */
687 /* Find parent before doing malloc, to save having to cleanup. */
689 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
691 doesn't exist yet? */
693 pi
= XNEW (procinfo
);
694 memset (pi
, 0, sizeof (procinfo
));
698 #ifdef DYNAMIC_SYSCALLS
702 pi
->saved_entryset
= sysset_t_alloc (pi
);
703 pi
->saved_exitset
= sysset_t_alloc (pi
);
705 /* Chain into list. */
708 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
709 pi
->next
= procinfo_list
;
715 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
717 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
719 pi
->next
= parent
->thread_list
;
720 parent
->thread_list
= pi
;
725 /* Close all file descriptors associated with the procinfo. */
728 close_procinfo_files (procinfo
*pi
)
735 if (pi
->status_fd
> 0)
736 close (pi
->status_fd
);
738 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
741 /* Destructor function. Close, unlink and deallocate the object. */
744 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
748 /* Step one: unlink the procinfo from its list. */
752 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
755 ptr
->next
= pi
->next
;
759 /* Step two: close any open file descriptors. */
760 close_procinfo_files (pi
);
762 /* Step three: free the memory. */
763 #ifdef DYNAMIC_SYSCALLS
766 xfree (pi
->saved_entryset
);
767 xfree (pi
->saved_exitset
);
772 destroy_procinfo (procinfo
*pi
)
776 if (pi
->tid
!= 0) /* Destroy a thread procinfo. */
778 tmp
= find_procinfo (pi
->pid
, 0); /* Find the parent process. */
779 destroy_one_procinfo (&tmp
->thread_list
, pi
);
781 else /* Destroy a process procinfo and all its threads. */
783 /* First destroy the children, if any; */
784 while (pi
->thread_list
!= NULL
)
785 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
786 /* Then destroy the parent. Genocide!!! */
787 destroy_one_procinfo (&procinfo_list
, pi
);
792 do_destroy_procinfo_cleanup (void *pi
)
794 destroy_procinfo ((procinfo
*) pi
);
797 enum { NOKILL
, KILL
};
799 /* To be called on a non_recoverable error for a procinfo. Prints
800 error messages, optionally sends a SIGKILL to the process, then
801 destroys the data structure. */
804 dead_procinfo (procinfo
*pi
, const char *msg
, int kill_p
)
810 print_sys_errmsg (pi
->pathname
, errno
);
814 sprintf (procfile
, "process %d", pi
->pid
);
815 print_sys_errmsg (procfile
, errno
);
818 kill (pi
->pid
, SIGKILL
);
820 destroy_procinfo (pi
);
824 /* Returns the (complete) size of a sysset_t struct. Normally, this
825 is just sizeof (sysset_t), but in the case of Monterey/64, the
826 actual size of sysset_t isn't known until runtime. */
829 sysset_t_size (procinfo
* pi
)
831 #ifndef DYNAMIC_SYSCALLS
832 return sizeof (sysset_t
);
834 return sizeof (sysset_t
) - sizeof (uint64_t)
835 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
836 / (8 * sizeof (uint64_t)));
840 /* Allocate and (partially) initialize a sysset_t struct. */
843 sysset_t_alloc (procinfo
* pi
)
846 int size
= sysset_t_size (pi
);
848 ret
= (sysset_t
*) xmalloc (size
);
849 #ifdef DYNAMIC_SYSCALLS
850 ret
->pr_size
= ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
851 / (8 * sizeof (uint64_t)));
856 #ifdef DYNAMIC_SYSCALLS
858 /* Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
859 pi->num_syscalls with the number of syscalls and pi->syscall_names
860 with the names. (Certain numbers may be skipped in which case the
861 names for these numbers will be left as NULL.) */
863 #define MAX_SYSCALL_NAME_LENGTH 256
864 #define MAX_SYSCALLS 65536
867 load_syscalls (procinfo
*pi
)
869 char pathname
[MAX_PROC_NAME_SIZE
];
872 prsyscall_t
*syscalls
;
873 int i
, size
, maxcall
;
874 struct cleanup
*cleanups
;
876 pi
->num_syscalls
= 0;
877 pi
->syscall_names
= 0;
879 /* Open the file descriptor for the sysent file. */
880 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
881 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
884 error (_("load_syscalls: Can't open /proc/%d/sysent"), pi
->pid
);
886 cleanups
= make_cleanup_close (sysent_fd
);
888 size
= sizeof header
- sizeof (prsyscall_t
);
889 if (read (sysent_fd
, &header
, size
) != size
)
891 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi
->pid
);
894 if (header
.pr_nsyscalls
== 0)
896 error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"),
900 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
901 syscalls
= xmalloc (size
);
902 make_cleanup (free_current_contents
, &syscalls
);
904 if (read (sysent_fd
, syscalls
, size
) != size
)
905 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi
->pid
);
907 /* Find maximum syscall number. This may not be the same as
908 pr_nsyscalls since that value refers to the number of entries
909 in the table. (Also, the docs indicate that some system
910 call numbers may be skipped.) */
912 maxcall
= syscalls
[0].pr_number
;
914 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
915 if (syscalls
[i
].pr_number
> maxcall
916 && syscalls
[i
].pr_nameoff
> 0
917 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
918 maxcall
= syscalls
[i
].pr_number
;
920 pi
->num_syscalls
= maxcall
+1;
921 pi
->syscall_names
= XNEWVEC (char *, pi
->num_syscalls
);
923 for (i
= 0; i
< pi
->num_syscalls
; i
++)
924 pi
->syscall_names
[i
] = NULL
;
926 /* Read the syscall names in. */
927 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
929 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
933 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
934 || syscalls
[i
].pr_number
< 0
935 || syscalls
[i
].pr_nameoff
<= 0
936 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
937 != (off_t
) syscalls
[i
].pr_nameoff
))
940 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
944 callnum
= syscalls
[i
].pr_number
;
946 if (pi
->syscall_names
[callnum
] != NULL
)
948 /* FIXME: Generate warning. */
952 namebuf
[nread
-1] = '\0';
953 size
= strlen (namebuf
) + 1;
954 pi
->syscall_names
[callnum
] = xmalloc (size
);
955 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
956 pi
->syscall_names
[callnum
][size
-1] = '\0';
959 do_cleanups (cleanups
);
962 /* Free the space allocated for the syscall names from the procinfo
966 free_syscalls (procinfo
*pi
)
968 if (pi
->syscall_names
)
972 for (i
= 0; i
< pi
->num_syscalls
; i
++)
973 if (pi
->syscall_names
[i
] != NULL
)
974 xfree (pi
->syscall_names
[i
]);
976 xfree (pi
->syscall_names
);
977 pi
->syscall_names
= 0;
981 /* Given a name, look up (and return) the corresponding syscall number.
982 If no match is found, return -1. */
985 find_syscall (procinfo
*pi
, const char *name
)
989 for (i
= 0; i
< pi
->num_syscalls
; i
++)
991 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
998 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1000 /* =================== /proc "MODULE" =================== */
1002 /* This "module" is the interface layer between the /proc system API
1003 and the gdb target vector functions. This layer consists of access
1004 functions that encapsulate each of the basic operations that we
1005 need to use from the /proc API.
1007 The main motivation for this layer is to hide the fact that there
1008 are two very different implementations of the /proc API. Rather
1009 than have a bunch of #ifdefs all thru the gdb target vector
1010 functions, we do our best to hide them all in here. */
1012 static long proc_flags (procinfo
* pi
);
1013 static int proc_why (procinfo
* pi
);
1014 static int proc_what (procinfo
* pi
);
1015 static int proc_set_current_signal (procinfo
* pi
, int signo
);
1016 static int proc_get_current_thread (procinfo
* pi
);
1017 static int proc_iterate_over_threads
1019 int (*func
) (procinfo
*, procinfo
*, void *),
1023 proc_warn (procinfo
*pi
, const char *func
, int line
)
1025 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1026 print_sys_errmsg (errmsg
, errno
);
1030 proc_error (procinfo
*pi
, const char *func
, int line
)
1032 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1033 perror_with_name (errmsg
);
1036 /* Updates the status struct in the procinfo. There is a 'valid'
1037 flag, to let other functions know when this function needs to be
1038 called (so the status is only read when it is needed). The status
1039 file descriptor is also only opened when it is needed. Returns
1040 non-zero for success, zero for failure. */
1043 proc_get_status (procinfo
*pi
)
1045 /* Status file descriptor is opened "lazily". */
1046 if (pi
->status_fd
== 0 &&
1047 open_procinfo_files (pi
, FD_STATUS
) == 0)
1049 pi
->status_valid
= 0;
1054 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1055 pi
->status_valid
= 0; /* fail */
1058 /* Sigh... I have to read a different data structure,
1059 depending on whether this is a main process or an LWP. */
1061 pi
->status_valid
= (read (pi
->status_fd
,
1062 (char *) &pi
->prstatus
.pr_lwp
,
1063 sizeof (lwpstatus_t
))
1064 == sizeof (lwpstatus_t
));
1067 pi
->status_valid
= (read (pi
->status_fd
,
1068 (char *) &pi
->prstatus
,
1069 sizeof (gdb_prstatus_t
))
1070 == sizeof (gdb_prstatus_t
));
1073 #else /* ioctl method */
1074 #ifdef PIOCTSTATUS /* osf */
1075 if (pi
->tid
== 0) /* main process */
1077 /* Just read the danged status. Now isn't that simple? */
1079 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1086 tid_t pr_error_thread
;
1087 struct prstatus status
;
1090 thread_status
.pr_count
= 1;
1091 thread_status
.status
.pr_tid
= pi
->tid
;
1092 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1095 memcpy (&pi
->prstatus
, &thread_status
.status
,
1096 sizeof (pi
->prstatus
));
1097 pi
->status_valid
= 1;
1101 /* Just read the danged status. Now isn't that simple? */
1102 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1106 if (pi
->status_valid
)
1108 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1111 proc_get_current_thread (pi
));
1114 /* The status struct includes general regs, so mark them valid too. */
1115 pi
->gregs_valid
= pi
->status_valid
;
1117 /* In the read/write multiple-fd model, the status struct includes
1118 the fp regs too, so mark them valid too. */
1119 pi
->fpregs_valid
= pi
->status_valid
;
1121 return pi
->status_valid
; /* True if success, false if failure. */
1124 /* Returns the process flags (pr_flags field). */
1127 proc_flags (procinfo
*pi
)
1129 if (!pi
->status_valid
)
1130 if (!proc_get_status (pi
))
1131 return 0; /* FIXME: not a good failure value (but what is?) */
1134 return pi
->prstatus
.pr_lwp
.pr_flags
;
1136 return pi
->prstatus
.pr_flags
;
1140 /* Returns the pr_why field (why the process stopped). */
1143 proc_why (procinfo
*pi
)
1145 if (!pi
->status_valid
)
1146 if (!proc_get_status (pi
))
1147 return 0; /* FIXME: not a good failure value (but what is?) */
1150 return pi
->prstatus
.pr_lwp
.pr_why
;
1152 return pi
->prstatus
.pr_why
;
1156 /* Returns the pr_what field (details of why the process stopped). */
1159 proc_what (procinfo
*pi
)
1161 if (!pi
->status_valid
)
1162 if (!proc_get_status (pi
))
1163 return 0; /* FIXME: not a good failure value (but what is?) */
1166 return pi
->prstatus
.pr_lwp
.pr_what
;
1168 return pi
->prstatus
.pr_what
;
1172 /* This function is only called when PI is stopped by a watchpoint.
1173 Assuming the OS supports it, write to *ADDR the data address which
1174 triggered it and return 1. Return 0 if it is not possible to know
1178 proc_watchpoint_address (procinfo
*pi
, CORE_ADDR
*addr
)
1180 if (!pi
->status_valid
)
1181 if (!proc_get_status (pi
))
1185 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
1186 builtin_type (target_gdbarch ())->builtin_data_ptr
,
1187 (gdb_byte
*) &pi
->prstatus
.pr_lwp
.pr_info
.si_addr
);
1189 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
1190 builtin_type (target_gdbarch ())->builtin_data_ptr
,
1191 (gdb_byte
*) &pi
->prstatus
.pr_info
.si_addr
);
1196 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1198 /* Returns the pr_nsysarg field (number of args to the current
1202 proc_nsysarg (procinfo
*pi
)
1204 if (!pi
->status_valid
)
1205 if (!proc_get_status (pi
))
1209 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1211 return pi
->prstatus
.pr_nsysarg
;
1215 /* Returns the pr_sysarg field (pointer to the arguments of current
1219 proc_sysargs (procinfo
*pi
)
1221 if (!pi
->status_valid
)
1222 if (!proc_get_status (pi
))
1226 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1228 return (long *) &pi
->prstatus
.pr_sysarg
;
1231 #endif /* PIOCSSPCACT */
1233 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
1234 /* Returns the pr_cursig field (current signal). */
1237 proc_cursig (struct procinfo
*pi
)
1239 if (!pi
->status_valid
)
1240 if (!proc_get_status (pi
))
1241 return 0; /* FIXME: not a good failure value (but what is?) */
1244 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1246 return pi
->prstatus
.pr_cursig
;
1249 #endif /* PROCFS_DONT_PIOCSSIG_CURSIG */
1251 /* === I appologize for the messiness of this function.
1252 === This is an area where the different versions of
1253 === /proc are more inconsistent than usual.
1255 Set or reset any of the following process flags:
1256 PR_FORK -- forked child will inherit trace flags
1257 PR_RLC -- traced process runs when last /proc file closed.
1258 PR_KLC -- traced process is killed when last /proc file closed.
1259 PR_ASYNC -- LWP's get to run/stop independently.
1261 There are three methods for doing this function:
1262 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1264 2) Middle: PIOCSET/PIOCRESET
1266 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1269 Note: Irix does not define PR_ASYNC.
1270 Note: OSF does not define PR_KLC.
1271 Note: OSF is the only one that can ONLY use the oldest method.
1275 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1276 mode -- 1 for set, 0 for reset.
1278 Returns non-zero for success, zero for failure. */
1280 enum { FLAG_RESET
, FLAG_SET
};
1283 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1285 long win
= 0; /* default to fail */
1287 /* These operations affect the process as a whole, and applying them
1288 to an individual LWP has the same meaning as applying them to the
1289 main process. Therefore, if we're ever called with a pointer to
1290 an LWP's procinfo, let's substitute the process's procinfo and
1291 avoid opening the LWP's file descriptor unnecessarily. */
1294 pi
= find_procinfo_or_die (pi
->pid
, 0);
1296 #ifdef NEW_PROC_API /* Newest method: Newer Solarii. */
1297 /* First normalize the PCUNSET/PCRESET command opcode
1298 (which for no obvious reason has a different definition
1299 from one operating system to the next...) */
1301 #define GDBRESET PCUNSET
1304 #define GDBRESET PCRESET
1308 procfs_ctl_t arg
[2];
1310 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC). */
1312 else /* Reset the flag. */
1316 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1319 #ifdef PIOCSET /* Irix/Sol5 method */
1320 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC). */
1322 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1324 else /* Reset the flag. */
1326 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1330 #ifdef PIOCSRLC /* Oldest method: OSF */
1333 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1335 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1337 else /* Clear run-on-last-close */
1339 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1343 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1345 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1347 else /* Clear inherit-on-fork */
1349 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1353 win
= 0; /* Fail -- unknown flag (can't do PR_ASYNC). */
1360 /* The above operation renders the procinfo's cached pstatus
1362 pi
->status_valid
= 0;
1365 warning (_("procfs: modify_flag failed to turn %s %s"),
1366 flag
== PR_FORK
? "PR_FORK" :
1367 flag
== PR_RLC
? "PR_RLC" :
1369 flag
== PR_ASYNC
? "PR_ASYNC" :
1372 flag
== PR_KLC
? "PR_KLC" :
1375 mode
== FLAG_RESET
? "off" : "on");
1380 /* Set the run_on_last_close flag. Process with all threads will
1381 become runnable when debugger closes all /proc fds. Returns
1382 non-zero for success, zero for failure. */
1385 proc_set_run_on_last_close (procinfo
*pi
)
1387 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1390 /* Reset the run_on_last_close flag. The process will NOT become
1391 runnable when debugger closes its file handles. Returns non-zero
1392 for success, zero for failure. */
1395 proc_unset_run_on_last_close (procinfo
*pi
)
1397 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1400 /* Reset inherit_on_fork flag. If the process forks a child while we
1401 are registered for events in the parent, then we will NOT recieve
1402 events from the child. Returns non-zero for success, zero for
1406 proc_unset_inherit_on_fork (procinfo
*pi
)
1408 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1412 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
1413 (signal etc.), the remaining LWPs will continue to run. Returns
1414 non-zero for success, zero for failure. */
1417 proc_set_async (procinfo
*pi
)
1419 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1422 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
1423 (signal etc.), then all other LWPs will stop as well. Returns
1424 non-zero for success, zero for failure. */
1427 proc_unset_async (procinfo
*pi
)
1429 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1431 #endif /* PR_ASYNC */
1433 /* Request the process/LWP to stop. Does not wait. Returns non-zero
1434 for success, zero for failure. */
1437 proc_stop_process (procinfo
*pi
)
1441 /* We might conceivably apply this operation to an LWP, and the
1442 LWP's ctl file descriptor might not be open. */
1444 if (pi
->ctl_fd
== 0 &&
1445 open_procinfo_files (pi
, FD_CTL
) == 0)
1450 procfs_ctl_t cmd
= PCSTOP
;
1452 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1453 #else /* ioctl method */
1454 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1455 /* Note: the call also reads the prstatus. */
1458 pi
->status_valid
= 1;
1459 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1462 proc_get_current_thread (pi
));
1470 /* Wait for the process or LWP to stop (block until it does). Returns
1471 non-zero for success, zero for failure. */
1474 proc_wait_for_stop (procinfo
*pi
)
1478 /* We should never have to apply this operation to any procinfo
1479 except the one for the main process. If that ever changes for
1480 any reason, then take out the following clause and replace it
1481 with one that makes sure the ctl_fd is open. */
1484 pi
= find_procinfo_or_die (pi
->pid
, 0);
1488 procfs_ctl_t cmd
= PCWSTOP
;
1490 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1491 /* We been runnin' and we stopped -- need to update status. */
1492 pi
->status_valid
= 0;
1494 #else /* ioctl method */
1495 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1496 /* Above call also refreshes the prstatus. */
1499 pi
->status_valid
= 1;
1500 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1503 proc_get_current_thread (pi
));
1510 /* Make the process or LWP runnable.
1512 Options (not all are implemented):
1514 - clear current fault
1515 - clear current signal
1516 - abort the current system call
1517 - stop as soon as finished with system call
1518 - (ioctl): set traced signal set
1519 - (ioctl): set held signal set
1520 - (ioctl): set traced fault set
1521 - (ioctl): set start pc (vaddr)
1523 Always clears the current fault. PI is the process or LWP to
1524 operate on. If STEP is true, set the process or LWP to trap after
1525 one instruction. If SIGNO is zero, clear the current signal if
1526 any; if non-zero, set the current signal to this one. Returns
1527 non-zero for success, zero for failure. */
1530 proc_run_process (procinfo
*pi
, int step
, int signo
)
1535 /* We will probably have to apply this operation to individual
1536 threads, so make sure the control file descriptor is open. */
1538 if (pi
->ctl_fd
== 0 &&
1539 open_procinfo_files (pi
, FD_CTL
) == 0)
1544 runflags
= PRCFAULT
; /* Always clear current fault. */
1549 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals. */
1550 proc_set_current_signal (pi
, signo
);
1554 procfs_ctl_t cmd
[2];
1558 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1560 #else /* ioctl method */
1564 memset (&prrun
, 0, sizeof (prrun
));
1565 prrun
.pr_flags
= runflags
;
1566 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1573 /* Register to trace signals in the process or LWP. Returns non-zero
1574 for success, zero for failure. */
1577 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1581 /* We should never have to apply this operation to any procinfo
1582 except the one for the main process. If that ever changes for
1583 any reason, then take out the following clause and replace it
1584 with one that makes sure the ctl_fd is open. */
1587 pi
= find_procinfo_or_die (pi
->pid
, 0);
1593 /* Use char array to avoid alignment issues. */
1594 char sigset
[sizeof (gdb_sigset_t
)];
1598 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1600 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1602 #else /* ioctl method */
1603 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1605 /* The above operation renders the procinfo's cached pstatus obsolete. */
1606 pi
->status_valid
= 0;
1609 warning (_("procfs: set_traced_signals failed"));
1613 /* Register to trace hardware faults in the process or LWP. Returns
1614 non-zero for success, zero for failure. */
1617 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1621 /* We should never have to apply this operation to any procinfo
1622 except the one for the main process. If that ever changes for
1623 any reason, then take out the following clause and replace it
1624 with one that makes sure the ctl_fd is open. */
1627 pi
= find_procinfo_or_die (pi
->pid
, 0);
1633 /* Use char array to avoid alignment issues. */
1634 char fltset
[sizeof (fltset_t
)];
1638 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1640 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1642 #else /* ioctl method */
1643 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1645 /* The above operation renders the procinfo's cached pstatus obsolete. */
1646 pi
->status_valid
= 0;
1651 /* Register to trace entry to system calls in the process or LWP.
1652 Returns non-zero for success, zero for failure. */
1655 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1659 /* We should never have to apply this operation to any procinfo
1660 except the one for the main process. If that ever changes for
1661 any reason, then take out the following clause and replace it
1662 with one that makes sure the ctl_fd is open. */
1665 pi
= find_procinfo_or_die (pi
->pid
, 0);
1669 struct gdb_proc_ctl_pcsentry
{
1671 /* Use char array to avoid alignment issues. */
1672 char sysset
[sizeof (sysset_t
)];
1674 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1676 + sysset_t_size (pi
);
1678 argp
= (struct gdb_proc_ctl_pcsentry
*) xmalloc (argp_size
);
1680 argp
->cmd
= PCSENTRY
;
1681 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1683 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1686 #else /* ioctl method */
1687 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1689 /* The above operation renders the procinfo's cached pstatus
1691 pi
->status_valid
= 0;
1696 /* Register to trace exit from system calls in the process or LWP.
1697 Returns non-zero for success, zero for failure. */
1700 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1704 /* We should never have to apply this operation to any procinfo
1705 except the one for the main process. If that ever changes for
1706 any reason, then take out the following clause and replace it
1707 with one that makes sure the ctl_fd is open. */
1710 pi
= find_procinfo_or_die (pi
->pid
, 0);
1714 struct gdb_proc_ctl_pcsexit
{
1716 /* Use char array to avoid alignment issues. */
1717 char sysset
[sizeof (sysset_t
)];
1719 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1721 + sysset_t_size (pi
);
1723 argp
= (struct gdb_proc_ctl_pcsexit
*) xmalloc (argp_size
);
1725 argp
->cmd
= PCSEXIT
;
1726 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1728 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1731 #else /* ioctl method */
1732 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1734 /* The above operation renders the procinfo's cached pstatus
1736 pi
->status_valid
= 0;
1741 /* Specify the set of blocked / held signals in the process or LWP.
1742 Returns non-zero for success, zero for failure. */
1745 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1749 /* We should never have to apply this operation to any procinfo
1750 except the one for the main process. If that ever changes for
1751 any reason, then take out the following clause and replace it
1752 with one that makes sure the ctl_fd is open. */
1755 pi
= find_procinfo_or_die (pi
->pid
, 0);
1761 /* Use char array to avoid alignment issues. */
1762 char hold
[sizeof (gdb_sigset_t
)];
1766 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1767 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1770 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1772 /* The above operation renders the procinfo's cached pstatus
1774 pi
->status_valid
= 0;
1779 /* Returns the set of signals that are held / blocked. Will also copy
1780 the sigset if SAVE is non-zero. */
1782 static gdb_sigset_t
*
1783 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
1785 gdb_sigset_t
*ret
= NULL
;
1787 /* We should never have to apply this operation to any procinfo
1788 except the one for the main process. If that ever changes for
1789 any reason, then take out the following clause and replace it
1790 with one that makes sure the ctl_fd is open. */
1793 pi
= find_procinfo_or_die (pi
->pid
, 0);
1796 if (!pi
->status_valid
)
1797 if (!proc_get_status (pi
))
1800 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1801 #else /* not NEW_PROC_API */
1803 static gdb_sigset_t sigheld
;
1805 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
1808 #endif /* NEW_PROC_API */
1810 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
1815 /* Returns the set of signals that are traced / debugged. Will also
1816 copy the sigset if SAVE is non-zero. */
1818 static gdb_sigset_t
*
1819 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
1821 gdb_sigset_t
*ret
= NULL
;
1823 /* We should never have to apply this operation to any procinfo
1824 except the one for the main process. If that ever changes for
1825 any reason, then take out the following clause and replace it
1826 with one that makes sure the ctl_fd is open. */
1829 pi
= find_procinfo_or_die (pi
->pid
, 0);
1832 if (!pi
->status_valid
)
1833 if (!proc_get_status (pi
))
1836 ret
= &pi
->prstatus
.pr_sigtrace
;
1839 static gdb_sigset_t sigtrace
;
1841 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
1846 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
1851 /* Returns the set of hardware faults that are traced /debugged. Will
1852 also copy the faultset if SAVE is non-zero. */
1855 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1857 fltset_t
*ret
= NULL
;
1859 /* We should never have to apply this operation to any procinfo
1860 except the one for the main process. If that ever changes for
1861 any reason, then take out the following clause and replace it
1862 with one that makes sure the ctl_fd is open. */
1865 pi
= find_procinfo_or_die (pi
->pid
, 0);
1868 if (!pi
->status_valid
)
1869 if (!proc_get_status (pi
))
1872 ret
= &pi
->prstatus
.pr_flttrace
;
1875 static fltset_t flttrace
;
1877 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
1882 memcpy (save
, ret
, sizeof (fltset_t
));
1887 /* Returns the set of syscalls that are traced /debugged on entry.
1888 Will also copy the syscall set if SAVE is non-zero. */
1891 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1893 sysset_t
*ret
= NULL
;
1895 /* We should never have to apply this operation to any procinfo
1896 except the one for the main process. If that ever changes for
1897 any reason, then take out the following clause and replace it
1898 with one that makes sure the ctl_fd is open. */
1901 pi
= find_procinfo_or_die (pi
->pid
, 0);
1904 if (!pi
->status_valid
)
1905 if (!proc_get_status (pi
))
1908 #ifndef DYNAMIC_SYSCALLS
1909 ret
= &pi
->prstatus
.pr_sysentry
;
1910 #else /* DYNAMIC_SYSCALLS */
1912 static sysset_t
*sysentry
;
1916 sysentry
= sysset_t_alloc (pi
);
1918 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
1920 if (pi
->prstatus
.pr_sysentry_offset
== 0)
1922 gdb_premptysysset (sysentry
);
1928 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
1930 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
1932 size
= sysset_t_size (pi
);
1933 gdb_premptysysset (sysentry
);
1934 rsize
= read (pi
->status_fd
, sysentry
, size
);
1939 #endif /* DYNAMIC_SYSCALLS */
1940 #else /* !NEW_PROC_API */
1942 static sysset_t sysentry
;
1944 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
1947 #endif /* NEW_PROC_API */
1949 memcpy (save
, ret
, sysset_t_size (pi
));
1954 /* Returns the set of syscalls that are traced /debugged on exit.
1955 Will also copy the syscall set if SAVE is non-zero. */
1958 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1960 sysset_t
* ret
= NULL
;
1962 /* We should never have to apply this operation to any procinfo
1963 except the one for the main process. If that ever changes for
1964 any reason, then take out the following clause and replace it
1965 with one that makes sure the ctl_fd is open. */
1968 pi
= find_procinfo_or_die (pi
->pid
, 0);
1971 if (!pi
->status_valid
)
1972 if (!proc_get_status (pi
))
1975 #ifndef DYNAMIC_SYSCALLS
1976 ret
= &pi
->prstatus
.pr_sysexit
;
1977 #else /* DYNAMIC_SYSCALLS */
1979 static sysset_t
*sysexit
;
1983 sysexit
= sysset_t_alloc (pi
);
1985 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
1987 if (pi
->prstatus
.pr_sysexit_offset
== 0)
1989 gdb_premptysysset (sysexit
);
1995 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
,
1997 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
1999 size
= sysset_t_size (pi
);
2000 gdb_premptysysset (sysexit
);
2001 rsize
= read (pi
->status_fd
, sysexit
, size
);
2006 #endif /* DYNAMIC_SYSCALLS */
2009 static sysset_t sysexit
;
2011 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2016 memcpy (save
, ret
, sysset_t_size (pi
));
2021 /* The current fault (if any) is cleared; the associated signal will
2022 not be sent to the process or LWP when it resumes. Returns
2023 non-zero for success, zero for failure. */
2026 proc_clear_current_fault (procinfo
*pi
)
2030 /* We should never have to apply this operation to any procinfo
2031 except the one for the main process. If that ever changes for
2032 any reason, then take out the following clause and replace it
2033 with one that makes sure the ctl_fd is open. */
2036 pi
= find_procinfo_or_die (pi
->pid
, 0);
2040 procfs_ctl_t cmd
= PCCFAULT
;
2042 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2045 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2051 /* Set the "current signal" that will be delivered next to the
2052 process. NOTE: semantics are different from those of KILL. This
2053 signal will be delivered to the process or LWP immediately when it
2054 is resumed (even if the signal is held/blocked); it will NOT
2055 immediately cause another event of interest, and will NOT first
2056 trap back to the debugger. Returns non-zero for success, zero for
2060 proc_set_current_signal (procinfo
*pi
, int signo
)
2065 /* Use char array to avoid alignment issues. */
2066 char sinfo
[sizeof (gdb_siginfo_t
)];
2068 gdb_siginfo_t mysinfo
;
2070 struct target_waitstatus wait_status
;
2072 /* We should never have to apply this operation to any procinfo
2073 except the one for the main process. If that ever changes for
2074 any reason, then take out the following clause and replace it
2075 with one that makes sure the ctl_fd is open. */
2078 pi
= find_procinfo_or_die (pi
->pid
, 0);
2080 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2081 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2082 receives a PIOCSSIG with a signal identical to the current
2083 signal, it messes up the current signal. Work around the kernel
2086 signo
== proc_cursig (pi
))
2087 return 1; /* I assume this is a success? */
2090 /* The pointer is just a type alias. */
2091 get_last_target_status (&wait_ptid
, &wait_status
);
2092 if (ptid_equal (wait_ptid
, inferior_ptid
)
2093 && wait_status
.kind
== TARGET_WAITKIND_STOPPED
2094 && wait_status
.value
.sig
== gdb_signal_from_host (signo
)
2095 && proc_get_status (pi
)
2097 && pi
->prstatus
.pr_lwp
.pr_info
.si_signo
== signo
2099 && pi
->prstatus
.pr_info
.si_signo
== signo
2102 /* Use the siginfo associated with the signal being
2105 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_lwp
.pr_info
, sizeof (gdb_siginfo_t
));
2107 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_info
, sizeof (gdb_siginfo_t
));
2111 mysinfo
.si_signo
= signo
;
2112 mysinfo
.si_code
= 0;
2113 mysinfo
.si_pid
= getpid (); /* ?why? */
2114 mysinfo
.si_uid
= getuid (); /* ?why? */
2115 memcpy (arg
.sinfo
, &mysinfo
, sizeof (gdb_siginfo_t
));
2120 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2122 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2128 /* The current signal (if any) is cleared, and is not sent to the
2129 process or LWP when it resumes. Returns non-zero for success, zero
2133 proc_clear_current_signal (procinfo
*pi
)
2137 /* We should never have to apply this operation to any procinfo
2138 except the one for the main process. If that ever changes for
2139 any reason, then take out the following clause and replace it
2140 with one that makes sure the ctl_fd is open. */
2143 pi
= find_procinfo_or_die (pi
->pid
, 0);
2149 /* Use char array to avoid alignment issues. */
2150 char sinfo
[sizeof (gdb_siginfo_t
)];
2152 gdb_siginfo_t mysinfo
;
2155 /* The pointer is just a type alias. */
2156 mysinfo
.si_signo
= 0;
2157 mysinfo
.si_code
= 0;
2158 mysinfo
.si_errno
= 0;
2159 mysinfo
.si_pid
= getpid (); /* ?why? */
2160 mysinfo
.si_uid
= getuid (); /* ?why? */
2161 memcpy (arg
.sinfo
, &mysinfo
, sizeof (gdb_siginfo_t
));
2163 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2166 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2172 /* Return the general-purpose registers for the process or LWP
2173 corresponding to PI. Upon failure, return NULL. */
2175 static gdb_gregset_t
*
2176 proc_get_gregs (procinfo
*pi
)
2178 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2179 if (!proc_get_status (pi
))
2183 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2185 return &pi
->prstatus
.pr_reg
;
2189 /* Return the general-purpose registers for the process or LWP
2190 corresponding to PI. Upon failure, return NULL. */
2192 static gdb_fpregset_t
*
2193 proc_get_fpregs (procinfo
*pi
)
2196 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2197 if (!proc_get_status (pi
))
2200 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2202 #else /* not NEW_PROC_API */
2203 if (pi
->fpregs_valid
)
2204 return &pi
->fpregset
; /* Already got 'em. */
2207 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2216 tid_t pr_error_thread
;
2217 tfpregset_t thread_1
;
2220 thread_fpregs
.pr_count
= 1;
2221 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2224 && ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2226 pi
->fpregs_valid
= 1;
2227 return &pi
->fpregset
; /* Got 'em now! */
2229 else if (pi
->tid
!= 0
2230 && ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2232 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2233 sizeof (pi
->fpregset
));
2234 pi
->fpregs_valid
= 1;
2235 return &pi
->fpregset
; /* Got 'em now! */
2242 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2244 pi
->fpregs_valid
= 1;
2245 return &pi
->fpregset
; /* Got 'em now! */
2254 #endif /* NEW_PROC_API */
2257 /* Write the general-purpose registers back to the process or LWP
2258 corresponding to PI. Return non-zero for success, zero for
2262 proc_set_gregs (procinfo
*pi
)
2264 gdb_gregset_t
*gregs
;
2267 gregs
= proc_get_gregs (pi
);
2269 return 0; /* proc_get_regs has already warned. */
2271 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2280 /* Use char array to avoid alignment issues. */
2281 char gregs
[sizeof (gdb_gregset_t
)];
2285 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2286 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2288 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2292 /* Policy: writing the registers invalidates our cache. */
2293 pi
->gregs_valid
= 0;
2297 /* Write the floating-pointer registers back to the process or LWP
2298 corresponding to PI. Return non-zero for success, zero for
2302 proc_set_fpregs (procinfo
*pi
)
2304 gdb_fpregset_t
*fpregs
;
2307 fpregs
= proc_get_fpregs (pi
);
2309 return 0; /* proc_get_fpregs has already warned. */
2311 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2320 /* Use char array to avoid alignment issues. */
2321 char fpregs
[sizeof (gdb_fpregset_t
)];
2325 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2326 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2330 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2335 tid_t pr_error_thread
;
2336 tfpregset_t thread_1
;
2339 thread_fpregs
.pr_count
= 1;
2340 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2341 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2343 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2346 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2348 #endif /* NEW_PROC_API */
2351 /* Policy: writing the registers invalidates our cache. */
2352 pi
->fpregs_valid
= 0;
2356 /* Send a signal to the proc or lwp with the semantics of "kill()".
2357 Returns non-zero for success, zero for failure. */
2360 proc_kill (procinfo
*pi
, int signo
)
2364 /* We might conceivably apply this operation to an LWP, and the
2365 LWP's ctl file descriptor might not be open. */
2367 if (pi
->ctl_fd
== 0 &&
2368 open_procinfo_files (pi
, FD_CTL
) == 0)
2375 procfs_ctl_t cmd
[2];
2379 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2380 #else /* ioctl method */
2381 /* FIXME: do I need the Alpha OSF fixups present in
2382 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2383 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2390 /* Find the pid of the process that started this one. Returns the
2391 parent process pid, or zero. */
2394 proc_parent_pid (procinfo
*pi
)
2396 /* We should never have to apply this operation to any procinfo
2397 except the one for the main process. If that ever changes for
2398 any reason, then take out the following clause and replace it
2399 with one that makes sure the ctl_fd is open. */
2402 pi
= find_procinfo_or_die (pi
->pid
, 0);
2404 if (!pi
->status_valid
)
2405 if (!proc_get_status (pi
))
2408 return pi
->prstatus
.pr_ppid
;
2411 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2412 (a.k.a void pointer)! */
2414 #if (defined (PCWATCH) || defined (PIOCSWATCH)) \
2415 && !(defined (PIOCOPENLWP))
2417 procfs_address_to_host_pointer (CORE_ADDR addr
)
2419 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
2422 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (ptr_type
));
2423 gdbarch_address_to_pointer (target_gdbarch (), ptr_type
,
2424 (gdb_byte
*) &ptr
, addr
);
2430 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2432 #if !defined (PCWATCH) && !defined (PIOCSWATCH)
2433 /* If neither or these is defined, we can't support watchpoints.
2434 This just avoids possibly failing to compile the below on such
2438 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5. */
2439 #if defined (PIOCOPENLWP) /* Solaris 2.5: bail out. */
2444 char watch
[sizeof (prwatch_t
)];
2448 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2449 convert a target address into something that can be stored in a
2450 native data structure. */
2451 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2452 pwatch
.pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
2454 pwatch
.pr_vaddr
= (caddr_t
) procfs_address_to_host_pointer (addr
);
2456 pwatch
.pr_size
= len
;
2457 pwatch
.pr_wflags
= wflags
;
2458 #if defined(NEW_PROC_API) && defined (PCWATCH)
2460 memcpy (arg
.watch
, &pwatch
, sizeof (prwatch_t
));
2461 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2463 #if defined (PIOCSWATCH)
2464 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, &pwatch
) >= 0);
2466 return 0; /* Fail */
2473 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
2475 #include <sys/sysi86.h>
2477 /* The KEY is actually the value of the lower 16 bits of the GS
2478 register for the LWP that we're interested in. Returns the
2479 matching ssh struct (LDT entry). */
2482 proc_get_LDT_entry (procinfo
*pi
, int key
)
2484 static struct ssd
*ldt_entry
= NULL
;
2486 char pathname
[MAX_PROC_NAME_SIZE
];
2487 struct cleanup
*old_chain
= NULL
;
2490 /* Allocate space for one LDT entry.
2491 This alloc must persist, because we return a pointer to it. */
2492 if (ldt_entry
== NULL
)
2493 ldt_entry
= XNEW (struct ssd
);
2495 /* Open the file descriptor for the LDT table. */
2496 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2497 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2499 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2502 /* Make sure it gets closed again! */
2503 old_chain
= make_cleanup_close (fd
);
2505 /* Now 'read' thru the table, find a match and return it. */
2506 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2508 if (ldt_entry
->sel
== 0 &&
2509 ldt_entry
->bo
== 0 &&
2510 ldt_entry
->acc1
== 0 &&
2511 ldt_entry
->acc2
== 0)
2512 break; /* end of table */
2513 /* If key matches, return this entry. */
2514 if (ldt_entry
->sel
== key
)
2516 do_cleanups (old_chain
);
2520 /* Loop ended, match not found. */
2521 do_cleanups (old_chain
);
2525 static int nalloc
= 0;
2527 /* Get the number of LDT entries. */
2528 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2530 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2534 /* Allocate space for the number of LDT entries. */
2535 /* This alloc has to persist, 'cause we return a pointer to it. */
2538 ldt_entry
= (struct ssd
*)
2539 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2543 /* Read the whole table in one gulp. */
2544 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2546 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2550 /* Search the table and return the (first) entry matching 'key'. */
2551 for (i
= 0; i
< nldt
; i
++)
2552 if (ldt_entry
[i
].sel
== key
)
2553 return &ldt_entry
[i
];
2555 /* Loop ended, match not found. */
2560 /* Returns the pointer to the LDT entry of PTID. */
2563 procfs_find_LDT_entry (ptid_t ptid
)
2565 gdb_gregset_t
*gregs
;
2569 /* Find procinfo for the lwp. */
2570 if ((pi
= find_procinfo (ptid_get_pid (ptid
), ptid_get_lwp (ptid
))) == NULL
)
2572 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
2573 ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
2576 /* get its general registers. */
2577 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2579 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
2580 ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
2583 /* Now extract the GS register's lower 16 bits. */
2584 key
= (*gregs
)[GS
] & 0xffff;
2586 /* Find the matching entry and return it. */
2587 return proc_get_LDT_entry (pi
, key
);
2592 /* =============== END, non-thread part of /proc "MODULE" =============== */
2594 /* =================== Thread "MODULE" =================== */
2596 /* NOTE: you'll see more ifdefs and duplication of functions here,
2597 since there is a different way to do threads on every OS. */
2599 /* Returns the number of threads for the process. */
2601 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2604 proc_get_nthreads (procinfo
*pi
)
2608 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
2609 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
2615 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2616 /* Solaris version */
2618 proc_get_nthreads (procinfo
*pi
)
2620 if (!pi
->status_valid
)
2621 if (!proc_get_status (pi
))
2624 /* NEW_PROC_API: only works for the process procinfo, because the
2625 LWP procinfos do not get prstatus filled in. */
2627 if (pi
->tid
!= 0) /* Find the parent process procinfo. */
2628 pi
= find_procinfo_or_die (pi
->pid
, 0);
2630 return pi
->prstatus
.pr_nlwp
;
2634 /* Default version */
2636 proc_get_nthreads (procinfo
*pi
)
2645 Return the ID of the thread that had an event of interest.
2646 (ie. the one that hit a breakpoint or other traced event). All
2647 other things being equal, this should be the ID of a thread that is
2648 currently executing. */
2650 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2651 /* Solaris version */
2653 proc_get_current_thread (procinfo
*pi
)
2655 /* Note: this should be applied to the root procinfo for the
2656 process, not to the procinfo for an LWP. If applied to the
2657 procinfo for an LWP, it will simply return that LWP's ID. In
2658 that case, find the parent process procinfo. */
2661 pi
= find_procinfo_or_die (pi
->pid
, 0);
2663 if (!pi
->status_valid
)
2664 if (!proc_get_status (pi
))
2668 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
2670 return pi
->prstatus
.pr_who
;
2675 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2678 proc_get_current_thread (procinfo
*pi
)
2680 #if 0 /* FIXME: not ready for prime time? */
2681 return pi
->prstatus
.pr_tid
;
2688 /* Default version */
2690 proc_get_current_thread (procinfo
*pi
)
2698 /* Discover the IDs of all the threads within the process, and create
2699 a procinfo for each of them (chained to the parent). This
2700 unfortunately requires a different method on every OS. Returns
2701 non-zero for success, zero for failure. */
2704 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
2706 if (thread
&& parent
) /* sanity */
2708 thread
->status_valid
= 0;
2709 if (!proc_get_status (thread
))
2710 destroy_one_procinfo (&parent
->thread_list
, thread
);
2712 return 0; /* keep iterating */
2715 #if defined (PIOCLSTATUS)
2716 /* Solaris 2.5 (ioctl) version */
2718 proc_update_threads (procinfo
*pi
)
2720 gdb_prstatus_t
*prstatus
;
2721 struct cleanup
*old_chain
= NULL
;
2725 /* We should never have to apply this operation to any procinfo
2726 except the one for the main process. If that ever changes for
2727 any reason, then take out the following clause and replace it
2728 with one that makes sure the ctl_fd is open. */
2731 pi
= find_procinfo_or_die (pi
->pid
, 0);
2733 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2735 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
2736 return 1; /* Process is not multi-threaded; nothing to do. */
2738 prstatus
= XNEWVEC (gdb_prstatus_t
, nlwp
+ 1);
2740 old_chain
= make_cleanup (xfree
, prstatus
);
2741 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
2742 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
2744 /* Skip element zero, which represents the process as a whole. */
2745 for (i
= 1; i
< nlwp
+ 1; i
++)
2747 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
2748 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2750 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
2751 thread
->status_valid
= 1;
2753 pi
->threads_valid
= 1;
2754 do_cleanups (old_chain
);
2759 /* Solaris 6 (and later) version. */
2761 do_closedir_cleanup (void *dir
)
2763 closedir ((DIR *) dir
);
2767 proc_update_threads (procinfo
*pi
)
2769 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
2770 struct dirent
*direntry
;
2771 struct cleanup
*old_chain
= NULL
;
2776 /* We should never have to apply this operation to any procinfo
2777 except the one for the main process. If that ever changes for
2778 any reason, then take out the following clause and replace it
2779 with one that makes sure the ctl_fd is open. */
2782 pi
= find_procinfo_or_die (pi
->pid
, 0);
2784 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2786 /* Note: this brute-force method was originally devised for Unixware
2787 (support removed since), and will also work on Solaris 2.6 and
2788 2.7. The original comment mentioned the existence of a much
2789 simpler and more elegant way to do this on Solaris, but didn't
2790 point out what that was. */
2792 strcpy (pathname
, pi
->pathname
);
2793 strcat (pathname
, "/lwp");
2794 if ((dirp
= opendir (pathname
)) == NULL
)
2795 proc_error (pi
, "update_threads, opendir", __LINE__
);
2797 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
2798 while ((direntry
= readdir (dirp
)) != NULL
)
2799 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
2801 lwpid
= atoi (&direntry
->d_name
[0]);
2802 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
2803 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2805 pi
->threads_valid
= 1;
2806 do_cleanups (old_chain
);
2813 proc_update_threads (procinfo
*pi
)
2818 /* We should never have to apply this operation to any procinfo
2819 except the one for the main process. If that ever changes for
2820 any reason, then take out the following clause and replace it
2821 with one that makes sure the ctl_fd is open. */
2824 pi
= find_procinfo_or_die (pi
->pid
, 0);
2826 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2828 nthreads
= proc_get_nthreads (pi
);
2830 return 0; /* Nothing to do for 1 or fewer threads. */
2832 threads
= XNEWVEC (tid_t
, nthreads
);
2834 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
2835 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
2837 for (i
= 0; i
< nthreads
; i
++)
2839 if (!find_procinfo (pi
->pid
, threads
[i
]))
2840 if (!create_procinfo (pi
->pid
, threads
[i
]))
2841 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2843 pi
->threads_valid
= 1;
2847 /* Default version */
2849 proc_update_threads (procinfo
*pi
)
2853 #endif /* OSF PIOCTLIST */
2854 #endif /* NEW_PROC_API */
2855 #endif /* SOL 2.5 PIOCLSTATUS */
2857 /* Given a pointer to a function, call that function once for each lwp
2858 in the procinfo list, until the function returns non-zero, in which
2859 event return the value returned by the function.
2861 Note: this function does NOT call update_threads. If you want to
2862 discover new threads first, you must call that function explicitly.
2863 This function just makes a quick pass over the currently-known
2866 PI is the parent process procinfo. FUNC is the per-thread
2867 function. PTR is an opaque parameter for function. Returns the
2868 first non-zero return value from the callee, or zero. */
2871 proc_iterate_over_threads (procinfo
*pi
,
2872 int (*func
) (procinfo
*, procinfo
*, void *),
2875 procinfo
*thread
, *next
;
2878 /* We should never have to apply this operation to any procinfo
2879 except the one for the main process. If that ever changes for
2880 any reason, then take out the following clause and replace it
2881 with one that makes sure the ctl_fd is open. */
2884 pi
= find_procinfo_or_die (pi
->pid
, 0);
2886 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
2888 next
= thread
->next
; /* In case thread is destroyed. */
2889 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
2896 /* =================== END, Thread "MODULE" =================== */
2898 /* =================== END, /proc "MODULE" =================== */
2900 /* =================== GDB "MODULE" =================== */
2902 /* Here are all of the gdb target vector functions and their
2905 static ptid_t
do_attach (ptid_t ptid
);
2906 static void do_detach (int signo
);
2907 static void proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
,
2908 int entry_or_exit
, int mode
, int from_tty
);
2910 /* Sets up the inferior to be debugged. Registers to trace signals,
2911 hardware faults, and syscalls. Note: does not set RLC flag: caller
2912 may want to customize that. Returns zero for success (note!
2913 unlike most functions in this module); on failure, returns the LINE
2914 NUMBER where it failed! */
2917 procfs_debug_inferior (procinfo
*pi
)
2919 fltset_t traced_faults
;
2920 gdb_sigset_t traced_signals
;
2921 sysset_t
*traced_syscall_entries
;
2922 sysset_t
*traced_syscall_exits
;
2925 /* Register to trace hardware faults in the child. */
2926 prfillset (&traced_faults
); /* trace all faults... */
2927 gdb_prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
2928 if (!proc_set_traced_faults (pi
, &traced_faults
))
2931 /* Initially, register to trace all signals in the child. */
2932 prfillset (&traced_signals
);
2933 if (!proc_set_traced_signals (pi
, &traced_signals
))
2937 /* Register to trace the 'exit' system call (on entry). */
2938 traced_syscall_entries
= sysset_t_alloc (pi
);
2939 gdb_premptysysset (traced_syscall_entries
);
2941 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
2944 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
);/* And _lwp_exit... */
2947 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
2949 #ifdef DYNAMIC_SYSCALLS
2951 int callnum
= find_syscall (pi
, "_exit");
2954 gdb_praddsysset (traced_syscall_entries
, callnum
);
2958 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
2959 xfree (traced_syscall_entries
);
2963 #ifdef PRFS_STOPEXEC /* defined on OSF */
2964 /* OSF method for tracing exec syscalls. Quoting:
2965 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
2966 exits from exec system calls because of the user level loader. */
2967 /* FIXME: make nice and maybe move into an access function. */
2971 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
2974 prfs_flags
|= PRFS_STOPEXEC
;
2976 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
2979 #else /* not PRFS_STOPEXEC */
2980 /* Everyone else's (except OSF) method for tracing exec syscalls. */
2982 Not all systems with /proc have all the exec* syscalls with the same
2983 names. On the SGI, for example, there is no SYS_exec, but there
2984 *is* a SYS_execv. So, we try to account for that. */
2986 traced_syscall_exits
= sysset_t_alloc (pi
);
2987 gdb_premptysysset (traced_syscall_exits
);
2989 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
2992 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
2995 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
2998 #ifdef SYS_lwpcreate
2999 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3000 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3003 #ifdef SYS_lwp_create /* FIXME: once only, please. */
3004 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3005 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3008 #ifdef DYNAMIC_SYSCALLS
3010 int callnum
= find_syscall (pi
, "execve");
3013 gdb_praddsysset (traced_syscall_exits
, callnum
);
3014 callnum
= find_syscall (pi
, "ra_execve");
3016 gdb_praddsysset (traced_syscall_exits
, callnum
);
3020 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3021 xfree (traced_syscall_exits
);
3025 #endif /* PRFS_STOPEXEC */
3030 procfs_attach (struct target_ops
*ops
, const char *args
, int from_tty
)
3035 pid
= parse_pid_to_attach (args
);
3037 if (pid
== getpid ())
3038 error (_("Attaching GDB to itself is not a good idea..."));
3042 exec_file
= get_exec_file (0);
3045 printf_filtered (_("Attaching to program `%s', %s\n"),
3046 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3048 printf_filtered (_("Attaching to %s\n"),
3049 target_pid_to_str (pid_to_ptid (pid
)));
3053 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3054 if (!target_is_pushed (ops
))
3059 procfs_detach (struct target_ops
*ops
, const char *args
, int from_tty
)
3062 int pid
= ptid_get_pid (inferior_ptid
);
3069 const char *exec_file
;
3071 exec_file
= get_exec_file (0);
3072 if (exec_file
== NULL
)
3075 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file
,
3076 target_pid_to_str (pid_to_ptid (pid
)));
3077 gdb_flush (gdb_stdout
);
3082 inferior_ptid
= null_ptid
;
3083 detach_inferior (pid
);
3084 inf_child_maybe_unpush_target (ops
);
3088 do_attach (ptid_t ptid
)
3091 struct inferior
*inf
;
3095 if ((pi
= create_procinfo (ptid_get_pid (ptid
), 0)) == NULL
)
3096 perror (_("procfs: out of memory in 'attach'"));
3098 if (!open_procinfo_files (pi
, FD_CTL
))
3100 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3101 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3102 ptid_get_pid (ptid
));
3103 dead_procinfo (pi
, errmsg
, NOKILL
);
3106 /* Stop the process (if it isn't already stopped). */
3107 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3109 pi
->was_stopped
= 1;
3110 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3114 pi
->was_stopped
= 0;
3115 /* Set the process to run again when we close it. */
3116 if (!proc_set_run_on_last_close (pi
))
3117 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3119 /* Now stop the process. */
3120 if (!proc_stop_process (pi
))
3121 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3122 pi
->ignore_next_sigstop
= 1;
3124 /* Save some of the /proc state to be restored if we detach. */
3125 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3126 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3127 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3128 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3129 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3130 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3132 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3133 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3135 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3136 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3138 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3139 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3141 inf
= current_inferior ();
3142 inferior_appeared (inf
, pi
->pid
);
3143 /* Let GDB know that the inferior was attached. */
3144 inf
->attach_flag
= 1;
3146 /* Create a procinfo for the current lwp. */
3147 lwpid
= proc_get_current_thread (pi
);
3148 create_procinfo (pi
->pid
, lwpid
);
3150 /* Add it to gdb's thread list. */
3151 ptid
= ptid_build (pi
->pid
, lwpid
, 0);
3158 do_detach (int signo
)
3162 /* Find procinfo for the main process. */
3163 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
),
3164 0); /* FIXME: threads */
3166 if (!proc_set_current_signal (pi
, signo
))
3167 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3169 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3170 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3172 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3173 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3175 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3176 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3178 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3179 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3181 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3182 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3184 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3185 if (signo
|| !(pi
->was_stopped
) ||
3186 query (_("Was stopped when attached, make it runnable again? ")))
3188 /* Clear any pending signal. */
3189 if (!proc_clear_current_fault (pi
))
3190 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3192 if (signo
== 0 && !proc_clear_current_signal (pi
))
3193 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
3195 if (!proc_set_run_on_last_close (pi
))
3196 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3199 destroy_procinfo (pi
);
3202 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
3205 ??? Is the following note still relevant? We can't get individual
3206 registers with the PT_GETREGS ptrace(2) request either, yet we
3207 don't bother with caching at all in that case.
3209 NOTE: Since the /proc interface cannot give us individual
3210 registers, we pay no attention to REGNUM, and just fetch them all.
3211 This results in the possibility that we will do unnecessarily many
3212 fetches, since we may be called repeatedly for individual
3213 registers. So we cache the results, and mark the cache invalid
3214 when the process is resumed. */
3217 procfs_fetch_registers (struct target_ops
*ops
,
3218 struct regcache
*regcache
, int regnum
)
3220 gdb_gregset_t
*gregs
;
3222 ptid_t ptid
= regcache_get_ptid (regcache
);
3223 int pid
= ptid_get_pid (ptid
);
3224 int tid
= ptid_get_lwp (ptid
);
3225 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3227 pi
= find_procinfo_or_die (pid
, tid
);
3230 error (_("procfs: fetch_registers failed to find procinfo for %s"),
3231 target_pid_to_str (ptid
));
3233 gregs
= proc_get_gregs (pi
);
3235 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3237 supply_gregset (regcache
, (const gdb_gregset_t
*) gregs
);
3239 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
3241 gdb_fpregset_t
*fpregs
;
3243 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
3244 || regnum
== gdbarch_pc_regnum (gdbarch
)
3245 || regnum
== gdbarch_sp_regnum (gdbarch
))
3246 return; /* Not a floating point register. */
3248 fpregs
= proc_get_fpregs (pi
);
3250 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3252 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregs
);
3256 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
3257 this for all registers.
3259 NOTE: Since the /proc interface will not read individual registers,
3260 we will cache these requests until the process is resumed, and only
3261 then write them back to the inferior process.
3263 FIXME: is that a really bad idea? Have to think about cases where
3264 writing one register might affect the value of others, etc. */
3267 procfs_store_registers (struct target_ops
*ops
,
3268 struct regcache
*regcache
, int regnum
)
3270 gdb_gregset_t
*gregs
;
3272 ptid_t ptid
= regcache_get_ptid (regcache
);
3273 int pid
= ptid_get_pid (ptid
);
3274 int tid
= ptid_get_lwp (ptid
);
3275 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3277 pi
= find_procinfo_or_die (pid
, tid
);
3280 error (_("procfs: store_registers: failed to find procinfo for %s"),
3281 target_pid_to_str (ptid
));
3283 gregs
= proc_get_gregs (pi
);
3285 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3287 fill_gregset (regcache
, gregs
, regnum
);
3288 if (!proc_set_gregs (pi
))
3289 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3291 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
3293 gdb_fpregset_t
*fpregs
;
3295 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
3296 || regnum
== gdbarch_pc_regnum (gdbarch
)
3297 || regnum
== gdbarch_sp_regnum (gdbarch
))
3298 return; /* Not a floating point register. */
3300 fpregs
= proc_get_fpregs (pi
);
3302 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3304 fill_fpregset (regcache
, fpregs
, regnum
);
3305 if (!proc_set_fpregs (pi
))
3306 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3311 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3314 if (scall
== SYS_lwp_exit
)
3318 if (scall
== SYS_lwpexit
)
3325 syscall_is_exit (procinfo
*pi
, int scall
)
3328 if (scall
== SYS_exit
)
3331 #ifdef DYNAMIC_SYSCALLS
3332 if (find_syscall (pi
, "_exit") == scall
)
3339 syscall_is_exec (procinfo
*pi
, int scall
)
3342 if (scall
== SYS_exec
)
3346 if (scall
== SYS_execv
)
3350 if (scall
== SYS_execve
)
3353 #ifdef DYNAMIC_SYSCALLS
3354 if (find_syscall (pi
, "_execve"))
3356 if (find_syscall (pi
, "ra_execve"))
3363 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3365 #ifdef SYS_lwp_create
3366 if (scall
== SYS_lwp_create
)
3369 #ifdef SYS_lwpcreate
3370 if (scall
== SYS_lwpcreate
)
3377 /* Return the address of the __dbx_link() function in the file
3378 refernced by ABFD by scanning its symbol table. Return 0 if
3379 the symbol was not found. */
3382 dbx_link_addr (bfd
*abfd
)
3384 long storage_needed
;
3385 asymbol
**symbol_table
;
3386 long number_of_symbols
;
3389 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
3390 if (storage_needed
<= 0)
3393 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
3394 make_cleanup (xfree
, symbol_table
);
3396 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
3398 for (i
= 0; i
< number_of_symbols
; i
++)
3400 asymbol
*sym
= symbol_table
[i
];
3402 if ((sym
->flags
& BSF_GLOBAL
)
3403 && sym
->name
!= NULL
&& strcmp (sym
->name
, "__dbx_link") == 0)
3404 return (sym
->value
+ sym
->section
->vma
);
3407 /* Symbol not found, return NULL. */
3411 /* Search the symbol table of the file referenced by FD for a symbol
3412 named __dbx_link(). If found, then insert a breakpoint at this location,
3413 and return nonzero. Return zero otherwise. */
3416 insert_dbx_link_bpt_in_file (int fd
, CORE_ADDR ignored
)
3418 long storage_needed
;
3421 gdb_bfd_ref_ptr
abfd (gdb_bfd_fdopenr ("unamed", 0, fd
));
3424 warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
3428 if (!bfd_check_format (abfd
.get (), bfd_object
))
3430 /* Not the correct format, so we can not possibly find the dbx_link
3435 sym_addr
= dbx_link_addr (abfd
.get ());
3438 struct breakpoint
*dbx_link_bpt
;
3440 /* Insert the breakpoint. */
3442 = create_and_insert_solib_event_breakpoint (target_gdbarch (),
3444 if (dbx_link_bpt
== NULL
)
3446 warning (_("Failed to insert dbx_link breakpoint."));
3455 /* Calls the supplied callback function once for each mapped address
3456 space in the process. The callback function receives an open file
3457 descriptor for the file corresponding to that mapped address space
3458 (if there is one), and the base address of the mapped space. Quit
3459 when the callback function returns a nonzero value, or at teh end
3460 of the mappings. Returns the first non-zero return value of the
3461 callback function, or zero. */
3464 solib_mappings_callback (struct prmap
*map
, int (*func
) (int, CORE_ADDR
),
3467 procinfo
*pi
= data
;
3471 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
3473 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
3474 return -1; /* sanity */
3476 if (map
->pr_mapname
[0] == 0)
3478 fd
= -1; /* no map file */
3482 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
3483 /* Note: caller's responsibility to close this fd! */
3484 fd
= open_with_retry (name
, O_RDONLY
);
3485 /* Note: we don't test the above call for failure;
3486 we just pass the FD on as given. Sometimes there is
3487 no file, so the open may return failure, but that's
3491 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
->pr_vaddr
);
3492 /* Note: we don't test the above call for failure;
3493 we just pass the FD on as given. Sometimes there is
3494 no file, so the ioctl may return failure, but that's
3497 return (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
);
3500 /* If the given memory region MAP contains a symbol named __dbx_link,
3501 insert a breakpoint at this location and return nonzero. Return
3505 insert_dbx_link_bpt_in_region (struct prmap
*map
,
3506 find_memory_region_ftype child_func
,
3509 procinfo
*pi
= (procinfo
*) data
;
3511 /* We know the symbol we're looking for is in a text region, so
3512 only look for it if the region is a text one. */
3513 if (map
->pr_mflags
& MA_EXEC
)
3514 return solib_mappings_callback (map
, insert_dbx_link_bpt_in_file
, pi
);
3519 /* Search all memory regions for a symbol named __dbx_link. If found,
3520 insert a breakpoint at its location, and return nonzero. Return zero
3524 insert_dbx_link_breakpoint (procinfo
*pi
)
3526 return iterate_over_mappings (pi
, NULL
, pi
, insert_dbx_link_bpt_in_region
);
3530 /* Retrieve the next stop event from the child process. If child has
3531 not stopped yet, wait for it to stop. Translate /proc eventcodes
3532 (or possibly wait eventcodes) into gdb internal event codes.
3533 Returns the id of process (and possibly thread) that incurred the
3534 event. Event codes are returned through a pointer parameter. */
3537 procfs_wait (struct target_ops
*ops
,
3538 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
3540 /* First cut: loosely based on original version 2.1. */
3544 ptid_t retval
, temp_ptid
;
3545 int why
, what
, flags
;
3552 retval
= pid_to_ptid (-1);
3554 /* Find procinfo for main process. */
3555 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
3558 /* We must assume that the status is stale now... */
3559 pi
->status_valid
= 0;
3560 pi
->gregs_valid
= 0;
3561 pi
->fpregs_valid
= 0;
3563 #if 0 /* just try this out... */
3564 flags
= proc_flags (pi
);
3565 why
= proc_why (pi
);
3566 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3567 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3569 /* If child is not stopped, wait for it to stop. */
3570 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3571 !proc_wait_for_stop (pi
))
3573 /* wait_for_stop failed: has the child terminated? */
3574 if (errno
== ENOENT
)
3578 /* /proc file not found; presumably child has terminated. */
3579 wait_retval
= wait (&wstat
); /* "wait" for the child's exit. */
3582 if (wait_retval
!= ptid_get_pid (inferior_ptid
))
3583 error (_("procfs: couldn't stop "
3584 "process %d: wait returned %d."),
3585 ptid_get_pid (inferior_ptid
), wait_retval
);
3586 /* FIXME: might I not just use waitpid?
3587 Or try find_procinfo to see if I know about this child? */
3588 retval
= pid_to_ptid (wait_retval
);
3590 else if (errno
== EINTR
)
3594 /* Unknown error from wait_for_stop. */
3595 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3600 /* This long block is reached if either:
3601 a) the child was already stopped, or
3602 b) we successfully waited for the child with wait_for_stop.
3603 This block will analyze the /proc status, and translate it
3604 into a waitstatus for GDB.
3606 If we actually had to call wait because the /proc file
3607 is gone (child terminated), then we skip this block,
3608 because we already have a waitstatus. */
3610 flags
= proc_flags (pi
);
3611 why
= proc_why (pi
);
3612 what
= proc_what (pi
);
3614 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3617 /* If it's running async (for single_thread control),
3618 set it back to normal again. */
3619 if (flags
& PR_ASYNC
)
3620 if (!proc_unset_async (pi
))
3621 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3625 proc_prettyprint_why (why
, what
, 1);
3627 /* The 'pid' we will return to GDB is composed of
3628 the process ID plus the lwp ID. */
3629 retval
= ptid_build (pi
->pid
, proc_get_current_thread (pi
), 0);
3633 wstat
= (what
<< 8) | 0177;
3636 if (syscall_is_lwp_exit (pi
, what
))
3638 if (print_thread_events
)
3639 printf_unfiltered (_("[%s exited]\n"),
3640 target_pid_to_str (retval
));
3641 delete_thread (retval
);
3642 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3645 else if (syscall_is_exit (pi
, what
))
3647 struct inferior
*inf
;
3649 /* Handle SYS_exit call only. */
3650 /* Stopped at entry to SYS_exit.
3651 Make it runnable, resume it, then use
3652 the wait system call to get its exit code.
3653 Proc_run_process always clears the current
3655 Then return its exit status. */
3656 pi
->status_valid
= 0;
3658 /* FIXME: what we should do is return
3659 TARGET_WAITKIND_SPURIOUS. */
3660 if (!proc_run_process (pi
, 0, 0))
3661 proc_error (pi
, "target_wait, run_process", __LINE__
);
3663 inf
= find_inferior_pid (pi
->pid
);
3664 if (inf
->attach_flag
)
3666 /* Don't call wait: simulate waiting for exit,
3667 return a "success" exit code. Bogus: what if
3668 it returns something else? */
3670 retval
= inferior_ptid
; /* ? ? ? */
3674 int temp
= wait (&wstat
);
3676 /* FIXME: shouldn't I make sure I get the right
3677 event from the right process? If (for
3678 instance) I have killed an earlier inferior
3679 process but failed to clean up after it
3680 somehow, I could get its termination event
3683 /* If wait returns -1, that's what we return
3686 retval
= pid_to_ptid (temp
);
3691 printf_filtered (_("procfs: trapped on entry to "));
3692 proc_prettyprint_syscall (proc_what (pi
), 0);
3693 printf_filtered ("\n");
3696 long i
, nsysargs
, *sysargs
;
3698 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3699 (sysargs
= proc_sysargs (pi
)) != NULL
)
3701 printf_filtered (_("%ld syscall arguments:\n"),
3703 for (i
= 0; i
< nsysargs
; i
++)
3704 printf_filtered ("#%ld: 0x%08lx\n",
3712 /* How to exit gracefully, returning "unknown
3714 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3715 return inferior_ptid
;
3719 /* How to keep going without returning to wfi: */
3720 target_continue_no_signal (ptid
);
3726 if (syscall_is_exec (pi
, what
))
3728 /* Hopefully this is our own "fork-child" execing
3729 the real child. Hoax this event into a trap, and
3730 GDB will see the child about to execute its start
3732 wstat
= (SIGTRAP
<< 8) | 0177;
3735 else if (what
== SYS_syssgi
)
3737 /* see if we can break on dbx_link(). If yes, then
3738 we no longer need the SYS_syssgi notifications. */
3739 if (insert_dbx_link_breakpoint (pi
))
3740 proc_trace_syscalls_1 (pi
, SYS_syssgi
, PR_SYSEXIT
,
3743 /* This is an internal event and should be transparent
3744 to wfi, so resume the execution and wait again. See
3745 comment in procfs_init_inferior() for more details. */
3746 target_continue_no_signal (ptid
);
3750 else if (syscall_is_lwp_create (pi
, what
))
3752 /* This syscall is somewhat like fork/exec. We
3753 will get the event twice: once for the parent
3754 LWP, and once for the child. We should already
3755 know about the parent LWP, but the child will
3756 be new to us. So, whenever we get this event,
3757 if it represents a new thread, simply add the
3758 thread to the list. */
3760 /* If not in procinfo list, add it. */
3761 temp_tid
= proc_get_current_thread (pi
);
3762 if (!find_procinfo (pi
->pid
, temp_tid
))
3763 create_procinfo (pi
->pid
, temp_tid
);
3765 temp_ptid
= ptid_build (pi
->pid
, temp_tid
, 0);
3766 /* If not in GDB's thread list, add it. */
3767 if (!in_thread_list (temp_ptid
))
3768 add_thread (temp_ptid
);
3770 /* Return to WFI, but tell it to immediately resume. */
3771 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3772 return inferior_ptid
;
3774 else if (syscall_is_lwp_exit (pi
, what
))
3776 if (print_thread_events
)
3777 printf_unfiltered (_("[%s exited]\n"),
3778 target_pid_to_str (retval
));
3779 delete_thread (retval
);
3780 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3785 /* FIXME: Do we need to handle SYS_sproc,
3786 SYS_fork, or SYS_vfork here? The old procfs
3787 seemed to use this event to handle threads on
3788 older (non-LWP) systems, where I'm assuming
3789 that threads were actually separate processes.
3790 Irix, maybe? Anyway, low priority for now. */
3794 printf_filtered (_("procfs: trapped on exit from "));
3795 proc_prettyprint_syscall (proc_what (pi
), 0);
3796 printf_filtered ("\n");
3799 long i
, nsysargs
, *sysargs
;
3801 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3802 (sysargs
= proc_sysargs (pi
)) != NULL
)
3804 printf_filtered (_("%ld syscall arguments:\n"),
3806 for (i
= 0; i
< nsysargs
; i
++)
3807 printf_filtered ("#%ld: 0x%08lx\n",
3812 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3813 return inferior_ptid
;
3818 wstat
= (SIGSTOP
<< 8) | 0177;
3823 printf_filtered (_("Retry #%d:\n"), retry
);
3824 pi
->status_valid
= 0;
3829 /* If not in procinfo list, add it. */
3830 temp_tid
= proc_get_current_thread (pi
);
3831 if (!find_procinfo (pi
->pid
, temp_tid
))
3832 create_procinfo (pi
->pid
, temp_tid
);
3834 /* If not in GDB's thread list, add it. */
3835 temp_ptid
= ptid_build (pi
->pid
, temp_tid
, 0);
3836 if (!in_thread_list (temp_ptid
))
3837 add_thread (temp_ptid
);
3839 status
->kind
= TARGET_WAITKIND_STOPPED
;
3840 status
->value
.sig
= GDB_SIGNAL_0
;
3845 wstat
= (what
<< 8) | 0177;
3851 wstat
= (SIGTRAP
<< 8) | 0177;
3856 wstat
= (SIGTRAP
<< 8) | 0177;
3859 /* FIXME: use si_signo where possible. */
3861 #if (FLTILL != FLTPRIV) /* Avoid "duplicate case" error. */
3864 wstat
= (SIGILL
<< 8) | 0177;
3867 #if (FLTTRACE != FLTBPT) /* Avoid "duplicate case" error. */
3870 wstat
= (SIGTRAP
<< 8) | 0177;
3874 #if (FLTBOUNDS != FLTSTACK) /* Avoid "duplicate case" error. */
3877 wstat
= (SIGSEGV
<< 8) | 0177;
3881 #if (FLTFPE != FLTIOVF) /* Avoid "duplicate case" error. */
3884 wstat
= (SIGFPE
<< 8) | 0177;
3886 case FLTPAGE
: /* Recoverable page fault */
3887 default: /* FIXME: use si_signo if possible for
3889 retval
= pid_to_ptid (-1);
3890 printf_filtered ("procfs:%d -- ", __LINE__
);
3891 printf_filtered (_("child stopped for unknown reason:\n"));
3892 proc_prettyprint_why (why
, what
, 1);
3893 error (_("... giving up..."));
3896 break; /* case PR_FAULTED: */
3897 default: /* switch (why) unmatched */
3898 printf_filtered ("procfs:%d -- ", __LINE__
);
3899 printf_filtered (_("child stopped for unknown reason:\n"));
3900 proc_prettyprint_why (why
, what
, 1);
3901 error (_("... giving up..."));
3904 /* Got this far without error: If retval isn't in the
3905 threads database, add it. */
3906 if (ptid_get_pid (retval
) > 0 &&
3907 !ptid_equal (retval
, inferior_ptid
) &&
3908 !in_thread_list (retval
))
3910 /* We have a new thread. We need to add it both to
3911 GDB's list and to our own. If we don't create a
3912 procinfo, resume may be unhappy later. */
3913 add_thread (retval
);
3914 if (find_procinfo (ptid_get_pid (retval
),
3915 ptid_get_lwp (retval
)) == NULL
)
3916 create_procinfo (ptid_get_pid (retval
),
3917 ptid_get_lwp (retval
));
3920 else /* Flags do not indicate STOPPED. */
3922 /* surely this can't happen... */
3923 printf_filtered ("procfs:%d -- process not stopped.\n",
3925 proc_prettyprint_flags (flags
, 1);
3926 error (_("procfs: ...giving up..."));
3931 store_waitstatus (status
, wstat
);
3937 /* Perform a partial transfer to/from the specified object. For
3938 memory transfers, fall back to the old memory xfer functions. */
3940 static enum target_xfer_status
3941 procfs_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3942 const char *annex
, gdb_byte
*readbuf
,
3943 const gdb_byte
*writebuf
, ULONGEST offset
, ULONGEST len
,
3944 ULONGEST
*xfered_len
)
3948 case TARGET_OBJECT_MEMORY
:
3949 return procfs_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
3952 case TARGET_OBJECT_AUXV
:
3953 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
3954 offset
, len
, xfered_len
);
3958 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
,
3959 readbuf
, writebuf
, offset
, len
,
3964 /* Helper for procfs_xfer_partial that handles memory transfers.
3965 Arguments are like target_xfer_partial. */
3967 static enum target_xfer_status
3968 procfs_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
3969 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
3974 /* Find procinfo for main process. */
3975 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
3976 if (pi
->as_fd
== 0 &&
3977 open_procinfo_files (pi
, FD_AS
) == 0)
3979 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
3980 return TARGET_XFER_E_IO
;
3983 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) != (off_t
) memaddr
)
3984 return TARGET_XFER_E_IO
;
3986 if (writebuf
!= NULL
)
3988 PROCFS_NOTE ("write memory:\n");
3989 nbytes
= write (pi
->as_fd
, writebuf
, len
);
3993 PROCFS_NOTE ("read memory:\n");
3994 nbytes
= read (pi
->as_fd
, readbuf
, len
);
3997 return TARGET_XFER_E_IO
;
3998 *xfered_len
= nbytes
;
3999 return TARGET_XFER_OK
;
4002 /* Called by target_resume before making child runnable. Mark cached
4003 registers and status's invalid. If there are "dirty" caches that
4004 need to be written back to the child process, do that.
4006 File descriptors are also cached. As they are a limited resource,
4007 we cannot hold onto them indefinitely. However, as they are
4008 expensive to open, we don't want to throw them away
4009 indescriminately either. As a compromise, we will keep the file
4010 descriptors for the parent process, but discard any file
4011 descriptors we may have accumulated for the threads.
4013 As this function is called by iterate_over_threads, it always
4014 returns zero (so that iterate_over_threads will keep
4018 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4020 /* About to run the child; invalidate caches and do any other
4024 if (pi
->gregs_dirty
)
4025 if (parent
== NULL
||
4026 proc_get_current_thread (parent
) != pi
->tid
)
4027 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4028 proc_warn (pi
, "target_resume, set_gregs",
4030 if (gdbarch_fp0_regnum (target_gdbarch ()) >= 0)
4031 if (pi
->fpregs_dirty
)
4032 if (parent
== NULL
||
4033 proc_get_current_thread (parent
) != pi
->tid
)
4034 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4035 proc_warn (pi
, "target_resume, set_fpregs",
4041 /* The presence of a parent indicates that this is an LWP.
4042 Close any file descriptors that it might have open.
4043 We don't do this to the master (parent) procinfo. */
4045 close_procinfo_files (pi
);
4047 pi
->gregs_valid
= 0;
4048 pi
->fpregs_valid
= 0;
4050 pi
->gregs_dirty
= 0;
4051 pi
->fpregs_dirty
= 0;
4053 pi
->status_valid
= 0;
4054 pi
->threads_valid
= 0;
4060 /* A callback function for iterate_over_threads. Find the
4061 asynchronous signal thread, and make it runnable. See if that
4062 helps matters any. */
4065 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4068 if (proc_flags (pi
) & PR_ASLWP
)
4070 if (!proc_run_process (pi
, 0, -1))
4071 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4079 /* Make the child process runnable. Normally we will then call
4080 procfs_wait and wait for it to stop again (unless gdb is async).
4082 If STEP is true, then arrange for the child to stop again after
4083 executing a single instruction. If SIGNO is zero, then cancel any
4084 pending signal; if non-zero, then arrange for the indicated signal
4085 to be delivered to the child when it runs. If PID is -1, then
4086 allow any child thread to run; if non-zero, then allow only the
4087 indicated thread to run. (not implemented yet). */
4090 procfs_resume (struct target_ops
*ops
,
4091 ptid_t ptid
, int step
, enum gdb_signal signo
)
4093 procinfo
*pi
, *thread
;
4097 prrun.prflags |= PRSVADDR;
4098 prrun.pr_vaddr = $PC; set resume address
4099 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4100 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4101 prrun.prflags |= PRCFAULT; clear current fault.
4103 PRSTRACE and PRSFAULT can be done by other means
4104 (proc_trace_signals, proc_trace_faults)
4105 PRSVADDR is unnecessary.
4106 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4107 This basically leaves PRSTEP and PRCSIG.
4108 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4109 So basically PR_STEP is the sole argument that must be passed
4110 to proc_run_process (for use in the prrun struct by ioctl). */
4112 /* Find procinfo for main process. */
4113 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4115 /* First cut: ignore pid argument. */
4118 /* Convert signal to host numbering. */
4120 (signo
== GDB_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4123 native_signo
= gdb_signal_to_host (signo
);
4125 pi
->ignore_next_sigstop
= 0;
4127 /* Running the process voids all cached registers and status. */
4128 /* Void the threads' caches first. */
4129 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4130 /* Void the process procinfo's caches. */
4131 invalidate_cache (NULL
, pi
, NULL
);
4133 if (ptid_get_pid (ptid
) != -1)
4135 /* Resume a specific thread, presumably suppressing the
4137 thread
= find_procinfo (ptid_get_pid (ptid
), ptid_get_lwp (ptid
));
4140 if (thread
->tid
!= 0)
4142 /* We're to resume a specific thread, and not the
4143 others. Set the child process's PR_ASYNC flag. */
4145 if (!proc_set_async (pi
))
4146 proc_error (pi
, "target_resume, set_async", __LINE__
);
4149 proc_iterate_over_threads (pi
,
4150 make_signal_thread_runnable
,
4153 pi
= thread
; /* Substitute the thread's procinfo
4159 if (!proc_run_process (pi
, step
, native_signo
))
4162 warning (_("resume: target already running. "
4163 "Pretend to resume, and hope for the best!"));
4165 proc_error (pi
, "target_resume", __LINE__
);
4169 /* Set up to trace signals in the child process. */
4172 procfs_pass_signals (struct target_ops
*self
,
4173 int numsigs
, unsigned char *pass_signals
)
4175 gdb_sigset_t signals
;
4176 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4179 prfillset (&signals
);
4181 for (signo
= 0; signo
< NSIG
; signo
++)
4183 int target_signo
= gdb_signal_from_host (signo
);
4184 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
4185 gdb_prdelset (&signals
, signo
);
4188 if (!proc_set_traced_signals (pi
, &signals
))
4189 proc_error (pi
, "pass_signals", __LINE__
);
4192 /* Print status information about the child process. */
4195 procfs_files_info (struct target_ops
*ignore
)
4197 struct inferior
*inf
= current_inferior ();
4199 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
4200 inf
->attach_flag
? "attached": "child",
4201 target_pid_to_str (inferior_ptid
));
4204 /* Stop the child process asynchronously, as when the gdb user types
4205 control-c or presses a "stop" button. Works by sending
4206 kill(SIGINT) to the child's process group. */
4209 procfs_interrupt (struct target_ops
*self
, ptid_t ptid
)
4211 kill (-inferior_process_group (), SIGINT
);
4214 /* Make it die. Wait for it to die. Clean up after it. Note: this
4215 should only be applied to the real process, not to an LWP, because
4216 of the check for parent-process. If we need this to work for an
4217 LWP, it needs some more logic. */
4220 unconditionally_kill_inferior (procinfo
*pi
)
4224 parent_pid
= proc_parent_pid (pi
);
4225 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4226 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4227 to kill the inferior, otherwise it might remain stopped with a
4229 We do not check the result of the PIOCSSIG, the inferior might have
4232 gdb_siginfo_t newsiginfo
;
4234 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4235 newsiginfo
.si_signo
= SIGKILL
;
4236 newsiginfo
.si_code
= 0;
4237 newsiginfo
.si_errno
= 0;
4238 newsiginfo
.si_pid
= getpid ();
4239 newsiginfo
.si_uid
= getuid ();
4240 /* FIXME: use proc_set_current_signal. */
4241 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4243 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4244 if (!proc_kill (pi
, SIGKILL
))
4245 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4246 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4247 destroy_procinfo (pi
);
4249 /* If pi is GDB's child, wait for it to die. */
4250 if (parent_pid
== getpid ())
4251 /* FIXME: should we use waitpid to make sure we get the right event?
4252 Should we check the returned event? */
4257 ret
= waitpid (pi
->pid
, &status
, 0);
4264 /* We're done debugging it, and we want it to go away. Then we want
4265 GDB to forget all about it. */
4268 procfs_kill_inferior (struct target_ops
*ops
)
4270 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4272 /* Find procinfo for main process. */
4273 procinfo
*pi
= find_procinfo (ptid_get_pid (inferior_ptid
), 0);
4276 unconditionally_kill_inferior (pi
);
4277 target_mourn_inferior (inferior_ptid
);
4281 /* Forget we ever debugged this thing! */
4284 procfs_mourn_inferior (struct target_ops
*ops
)
4288 if (!ptid_equal (inferior_ptid
, null_ptid
))
4290 /* Find procinfo for main process. */
4291 pi
= find_procinfo (ptid_get_pid (inferior_ptid
), 0);
4293 destroy_procinfo (pi
);
4296 generic_mourn_inferior ();
4298 inf_child_maybe_unpush_target (ops
);
4301 /* When GDB forks to create a runnable inferior process, this function
4302 is called on the parent side of the fork. It's job is to do
4303 whatever is necessary to make the child ready to be debugged, and
4304 then wait for the child to synchronize. */
4307 procfs_init_inferior (struct target_ops
*ops
, int pid
)
4310 gdb_sigset_t signals
;
4314 /* This routine called on the parent side (GDB side)
4315 after GDB forks the inferior. */
4316 if (!target_is_pushed (ops
))
4319 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4320 perror (_("procfs: out of memory in 'init_inferior'"));
4322 if (!open_procinfo_files (pi
, FD_CTL
))
4323 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4327 open_procinfo_files // done
4330 procfs_notice_signals
4337 /* If not stopped yet, wait for it to stop. */
4338 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4339 !(proc_wait_for_stop (pi
)))
4340 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4342 /* Save some of the /proc state to be restored if we detach. */
4343 /* FIXME: Why? In case another debugger was debugging it?
4344 We're it's parent, for Ghu's sake! */
4345 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4346 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4347 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4348 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4349 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4350 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4351 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4352 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4353 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4354 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4356 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4357 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4359 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4360 and possibly even turning ON kill-on-last-close at this point. But
4361 I can't make that change without careful testing which I don't have
4362 time to do right now... */
4363 /* Turn on run-on-last-close flag so that the child
4364 will die if GDB goes away for some reason. */
4365 if (!proc_set_run_on_last_close (pi
))
4366 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4368 /* We now have have access to the lwpid of the main thread/lwp. */
4369 lwpid
= proc_get_current_thread (pi
);
4371 /* Create a procinfo for the main lwp. */
4372 create_procinfo (pid
, lwpid
);
4374 /* We already have a main thread registered in the thread table at
4375 this point, but it didn't have any lwp info yet. Notify the core
4376 about it. This changes inferior_ptid as well. */
4377 thread_change_ptid (pid_to_ptid (pid
),
4378 ptid_build (pid
, lwpid
, 0));
4380 gdb_startup_inferior (pid
, START_INFERIOR_TRAPS_EXPECTED
);
4383 /* On mips-irix, we need to stop the inferior early enough during
4384 the startup phase in order to be able to load the shared library
4385 symbols and insert the breakpoints that are located in these shared
4386 libraries. Stopping at the program entry point is not good enough
4387 because the -init code is executed before the execution reaches
4390 So what we need to do is to insert a breakpoint in the runtime
4391 loader (rld), more precisely in __dbx_link(). This procedure is
4392 called by rld once all shared libraries have been mapped, but before
4393 the -init code is executed. Unfortuantely, this is not straightforward,
4394 as rld is not part of the executable we are running, and thus we need
4395 the inferior to run until rld itself has been mapped in memory.
4397 For this, we trace all syssgi() syscall exit events. Each time
4398 we detect such an event, we iterate over each text memory maps,
4399 get its associated fd, and scan the symbol table for __dbx_link().
4400 When found, we know that rld has been mapped, and that we can insert
4401 the breakpoint at the symbol address. Once the dbx_link() breakpoint
4402 has been inserted, the syssgi() notifications are no longer necessary,
4403 so they should be canceled. */
4404 proc_trace_syscalls_1 (pi
, SYS_syssgi
, PR_SYSEXIT
, FLAG_SET
, 0);
4408 /* When GDB forks to create a new process, this function is called on
4409 the child side of the fork before GDB exec's the user program. Its
4410 job is to make the child minimally debuggable, so that the parent
4411 GDB process can connect to the child and take over. This function
4412 should do only the minimum to make that possible, and to
4413 synchronize with the parent process. The parent process should
4414 take care of the details. */
4417 procfs_set_exec_trap (void)
4419 /* This routine called on the child side (inferior side)
4420 after GDB forks the inferior. It must use only local variables,
4421 because it may be sharing data space with its parent. */
4426 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4427 perror_with_name (_("procfs: create_procinfo failed in child."));
4429 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4431 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4432 gdb_flush (gdb_stderr
);
4433 /* No need to call "dead_procinfo", because we're going to
4438 #ifdef PRFS_STOPEXEC /* defined on OSF */
4439 /* OSF method for tracing exec syscalls. Quoting:
4440 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4441 exits from exec system calls because of the user level loader. */
4442 /* FIXME: make nice and maybe move into an access function. */
4446 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4448 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4449 gdb_flush (gdb_stderr
);
4452 prfs_flags
|= PRFS_STOPEXEC
;
4454 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4456 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4457 gdb_flush (gdb_stderr
);
4461 #else /* not PRFS_STOPEXEC */
4462 /* Everyone else's (except OSF) method for tracing exec syscalls. */
4464 Not all systems with /proc have all the exec* syscalls with the same
4465 names. On the SGI, for example, there is no SYS_exec, but there
4466 *is* a SYS_execv. So, we try to account for that. */
4468 exitset
= sysset_t_alloc (pi
);
4469 gdb_premptysysset (exitset
);
4471 gdb_praddsysset (exitset
, SYS_exec
);
4474 gdb_praddsysset (exitset
, SYS_execve
);
4477 gdb_praddsysset (exitset
, SYS_execv
);
4479 #ifdef DYNAMIC_SYSCALLS
4481 int callnum
= find_syscall (pi
, "execve");
4484 gdb_praddsysset (exitset
, callnum
);
4486 callnum
= find_syscall (pi
, "ra_execve");
4488 gdb_praddsysset (exitset
, callnum
);
4490 #endif /* DYNAMIC_SYSCALLS */
4492 if (!proc_set_traced_sysexit (pi
, exitset
))
4494 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4495 gdb_flush (gdb_stderr
);
4498 #endif /* PRFS_STOPEXEC */
4500 /* FIXME: should this be done in the parent instead? */
4501 /* Turn off inherit on fork flag so that all grand-children
4502 of gdb start with tracing flags cleared. */
4503 if (!proc_unset_inherit_on_fork (pi
))
4504 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4506 /* Turn off run on last close flag, so that the child process
4507 cannot run away just because we close our handle on it.
4508 We want it to wait for the parent to attach. */
4509 if (!proc_unset_run_on_last_close (pi
))
4510 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4512 /* FIXME: No need to destroy the procinfo --
4513 we have our own address space, and we're about to do an exec! */
4514 /*destroy_procinfo (pi);*/
4517 /* This function is called BEFORE gdb forks the inferior process. Its
4518 only real responsibility is to set things up for the fork, and tell
4519 GDB which two functions to call after the fork (one for the parent,
4520 and one for the child).
4522 This function does a complicated search for a unix shell program,
4523 which it then uses to parse arguments and environment variables to
4524 be sent to the child. I wonder whether this code could not be
4525 abstracted out and shared with other unix targets such as
4529 procfs_create_inferior (struct target_ops
*ops
, const char *exec_file
,
4530 const std::string
&allargs
, char **env
, int from_tty
)
4532 char *shell_file
= getenv ("SHELL");
4536 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4539 /* We will be looking down the PATH to find shell_file. If we
4540 just do this the normal way (via execlp, which operates by
4541 attempting an exec for each element of the PATH until it
4542 finds one which succeeds), then there will be an exec for
4543 each failed attempt, each of which will cause a PR_SYSEXIT
4544 stop, and we won't know how to distinguish the PR_SYSEXIT's
4545 for these failed execs with the ones for successful execs
4546 (whether the exec has succeeded is stored at that time in the
4547 carry bit or some such architecture-specific and
4548 non-ABI-specified place).
4550 So I can't think of anything better than to search the PATH
4551 now. This has several disadvantages: (1) There is a race
4552 condition; if we find a file now and it is deleted before we
4553 exec it, we lose, even if the deletion leaves a valid file
4554 further down in the PATH, (2) there is no way to know exactly
4555 what an executable (in the sense of "capable of being
4556 exec'd") file is. Using access() loses because it may lose
4557 if the caller is the superuser; failing to use it loses if
4558 there are ACLs or some such. */
4562 /* FIXME-maybe: might want "set path" command so user can change what
4563 path is used from within GDB. */
4564 const char *path
= getenv ("PATH");
4566 struct stat statbuf
;
4569 path
= "/bin:/usr/bin";
4571 tryname
= (char *) alloca (strlen (path
) + strlen (shell_file
) + 2);
4572 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4574 p1
= strchr (p
, ':');
4579 strncpy (tryname
, p
, len
);
4580 tryname
[len
] = '\0';
4581 strcat (tryname
, "/");
4582 strcat (tryname
, shell_file
);
4583 if (access (tryname
, X_OK
) < 0)
4585 if (stat (tryname
, &statbuf
) < 0)
4587 if (!S_ISREG (statbuf
.st_mode
))
4588 /* We certainly need to reject directories. I'm not quite
4589 as sure about FIFOs, sockets, etc., but I kind of doubt
4590 that people want to exec() these things. */
4595 /* Not found. This must be an error rather than merely passing
4596 the file to execlp(), because execlp() would try all the
4597 exec()s, causing GDB to get confused. */
4598 error (_("procfs:%d -- Can't find shell %s in PATH"),
4599 __LINE__
, shell_file
);
4601 shell_file
= tryname
;
4604 pid
= fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4605 NULL
, NULL
, shell_file
, NULL
);
4607 /* We have something that executes now. We'll be running through
4608 the shell at this point (if startup-with-shell is true), but the
4609 pid shouldn't change. */
4610 add_thread_silent (pid_to_ptid (pid
));
4612 procfs_init_inferior (ops
, pid
);
4615 /* An observer for the "inferior_created" event. */
4618 procfs_inferior_created (struct target_ops
*ops
, int from_tty
)
4621 /* Make sure to cancel the syssgi() syscall-exit notifications.
4622 They should normally have been removed by now, but they may still
4623 be activated if the inferior doesn't use shared libraries, or if
4624 we didn't locate __dbx_link, or if we never stopped in __dbx_link.
4625 See procfs_init_inferior() for more details.
4627 Since these notifications are only ever enabled when we spawned
4628 the inferior ourselves, there is nothing to do when the inferior
4629 was created by attaching to an already running process, or when
4630 debugging a core file. */
4631 if (current_inferior ()->attach_flag
|| !target_can_run (¤t_target
))
4634 proc_trace_syscalls_1 (find_procinfo_or_die (ptid_get_pid (inferior_ptid
),
4635 0), SYS_syssgi
, PR_SYSEXIT
, FLAG_RESET
, 0);
4639 /* Callback for update_thread_list. Calls "add_thread". */
4642 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
4644 ptid_t gdb_threadid
= ptid_build (pi
->pid
, thread
->tid
, 0);
4646 if (!in_thread_list (gdb_threadid
) || is_exited (gdb_threadid
))
4647 add_thread (gdb_threadid
);
4652 /* Query all the threads that the target knows about, and give them
4653 back to GDB to add to its list. */
4656 procfs_update_thread_list (struct target_ops
*ops
)
4662 /* Find procinfo for main process. */
4663 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4664 proc_update_threads (pi
);
4665 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
4668 /* Return true if the thread is still 'alive'. This guy doesn't
4669 really seem to be doing his job. Got to investigate how to tell
4670 when a thread is really gone. */
4673 procfs_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
4678 proc
= ptid_get_pid (ptid
);
4679 thread
= ptid_get_lwp (ptid
);
4680 /* If I don't know it, it ain't alive! */
4681 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
4684 /* If I can't get its status, it ain't alive!
4685 What's more, I need to forget about it! */
4686 if (!proc_get_status (pi
))
4688 destroy_procinfo (pi
);
4691 /* I couldn't have got its status if it weren't alive, so it's
4696 /* Convert PTID to a string. Returns the string in a static
4700 procfs_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
4702 static char buf
[80];
4704 if (ptid_get_lwp (ptid
) == 0)
4705 sprintf (buf
, "process %d", ptid_get_pid (ptid
));
4707 sprintf (buf
, "LWP %ld", ptid_get_lwp (ptid
));
4712 /* Insert a watchpoint. */
4715 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
4722 pi
= find_procinfo_or_die (ptid_get_pid (ptid
) == -1 ?
4723 ptid_get_pid (inferior_ptid
) : ptid_get_pid (ptid
),
4726 /* Translate from GDB's flags to /proc's. */
4727 if (len
> 0) /* len == 0 means delete watchpoint. */
4729 switch (rwflag
) { /* FIXME: need an enum! */
4730 case hw_write
: /* default watchpoint (write) */
4731 pflags
= WRITE_WATCHFLAG
;
4733 case hw_read
: /* read watchpoint */
4734 pflags
= READ_WATCHFLAG
;
4736 case hw_access
: /* access watchpoint */
4737 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
4739 case hw_execute
: /* execution HW breakpoint */
4740 pflags
= EXEC_WATCHFLAG
;
4742 default: /* Something weird. Return error. */
4745 if (after
) /* Stop after r/w access is completed. */
4746 pflags
|= AFTER_WATCHFLAG
;
4749 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
4751 if (errno
== E2BIG
) /* Typical error for no resources. */
4752 return -1; /* fail */
4753 /* GDB may try to remove the same watchpoint twice.
4754 If a remove request returns no match, don't error. */
4755 if (errno
== ESRCH
&& len
== 0)
4756 return 0; /* ignore */
4757 proc_error (pi
, "set_watchpoint", __LINE__
);
4763 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
4764 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
4765 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
4768 Note: procfs_can_use_hw_breakpoint() is not yet used by all
4769 procfs.c targets due to the fact that some of them still define
4770 target_can_use_hardware_watchpoint. */
4773 procfs_can_use_hw_breakpoint (struct target_ops
*self
,
4775 int cnt
, int othertype
)
4777 /* Due to the way that proc_set_watchpoint() is implemented, host
4778 and target pointers must be of the same size. If they are not,
4779 we can't use hardware watchpoints. This limitation is due to the
4780 fact that proc_set_watchpoint() calls
4781 procfs_address_to_host_pointer(); a close inspection of
4782 procfs_address_to_host_pointer will reveal that an internal error
4783 will be generated when the host and target pointer sizes are
4785 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
4787 if (sizeof (void *) != TYPE_LENGTH (ptr_type
))
4790 /* Other tests here??? */
4795 /* Returns non-zero if process is stopped on a hardware watchpoint
4796 fault, else returns zero. */
4799 procfs_stopped_by_watchpoint (struct target_ops
*ops
)
4803 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4805 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
4807 if (proc_why (pi
) == PR_FAULTED
)
4810 if (proc_what (pi
) == FLTWATCH
)
4814 if (proc_what (pi
) == FLTKWATCH
)
4822 /* Returns 1 if the OS knows the position of the triggered watchpoint,
4823 and sets *ADDR to that address. Returns 0 if OS cannot report that
4824 address. This function is only called if
4825 procfs_stopped_by_watchpoint returned 1, thus no further checks are
4826 done. The function also assumes that ADDR is not NULL. */
4829 procfs_stopped_data_address (struct target_ops
*targ
, CORE_ADDR
*addr
)
4833 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
4834 return proc_watchpoint_address (pi
, addr
);
4838 procfs_insert_watchpoint (struct target_ops
*self
,
4839 CORE_ADDR addr
, int len
,
4840 enum target_hw_bp_type type
,
4841 struct expression
*cond
)
4843 if (!target_have_steppable_watchpoint
4844 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
4846 /* When a hardware watchpoint fires off the PC will be left at
4847 the instruction following the one which caused the
4848 watchpoint. It will *NOT* be necessary for GDB to step over
4850 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 1);
4854 /* When a hardware watchpoint fires off the PC will be left at
4855 the instruction which caused the watchpoint. It will be
4856 necessary for GDB to step over the watchpoint. */
4857 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 0);
4862 procfs_remove_watchpoint (struct target_ops
*self
,
4863 CORE_ADDR addr
, int len
,
4864 enum target_hw_bp_type type
,
4865 struct expression
*cond
)
4867 return procfs_set_watchpoint (inferior_ptid
, addr
, 0, 0, 0);
4871 procfs_region_ok_for_hw_watchpoint (struct target_ops
*self
,
4872 CORE_ADDR addr
, int len
)
4874 /* The man page for proc(4) on Solaris 2.6 and up says that the
4875 system can support "thousands" of hardware watchpoints, but gives
4876 no method for finding out how many; It doesn't say anything about
4877 the allowed size for the watched area either. So we just tell
4883 procfs_use_watchpoints (struct target_ops
*t
)
4885 t
->to_stopped_by_watchpoint
= procfs_stopped_by_watchpoint
;
4886 t
->to_insert_watchpoint
= procfs_insert_watchpoint
;
4887 t
->to_remove_watchpoint
= procfs_remove_watchpoint
;
4888 t
->to_region_ok_for_hw_watchpoint
= procfs_region_ok_for_hw_watchpoint
;
4889 t
->to_can_use_hw_breakpoint
= procfs_can_use_hw_breakpoint
;
4890 t
->to_stopped_data_address
= procfs_stopped_data_address
;
4893 /* Memory Mappings Functions: */
4895 /* Call a callback function once for each mapping, passing it the
4896 mapping, an optional secondary callback function, and some optional
4897 opaque data. Quit and return the first non-zero value returned
4900 PI is the procinfo struct for the process to be mapped. FUNC is
4901 the callback function to be called by this iterator. DATA is the
4902 optional opaque data to be passed to the callback function.
4903 CHILD_FUNC is the optional secondary function pointer to be passed
4904 to the child function. Returns the first non-zero return value
4905 from the callback function, or zero. */
4908 iterate_over_mappings (procinfo
*pi
, find_memory_region_ftype child_func
,
4910 int (*func
) (struct prmap
*map
,
4911 find_memory_region_ftype child_func
,
4914 char pathname
[MAX_PROC_NAME_SIZE
];
4915 struct prmap
*prmaps
;
4916 struct prmap
*prmap
;
4920 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
4925 /* Get the number of mappings, allocate space,
4926 and read the mappings into prmaps. */
4929 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
4930 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
4931 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
4933 /* Make sure it gets closed again. */
4934 make_cleanup_close (map_fd
);
4936 /* Use stat to determine the file size, and compute
4937 the number of prmap_t objects it contains. */
4938 if (fstat (map_fd
, &sbuf
) != 0)
4939 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
4941 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
4942 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
4943 if (read (map_fd
, (char *) prmaps
, nmap
* sizeof (*prmaps
))
4944 != (nmap
* sizeof (*prmaps
)))
4945 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
4947 /* Use ioctl command PIOCNMAP to get number of mappings. */
4948 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmap
) != 0)
4949 proc_error (pi
, "iterate_over_mappings (PIOCNMAP)", __LINE__
);
4951 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
4952 if (ioctl (pi
->ctl_fd
, PIOCMAP
, prmaps
) != 0)
4953 proc_error (pi
, "iterate_over_mappings (PIOCMAP)", __LINE__
);
4956 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
4957 if ((funcstat
= (*func
) (prmap
, child_func
, data
)) != 0)
4959 do_cleanups (cleanups
);
4963 do_cleanups (cleanups
);
4967 /* Implements the to_find_memory_regions method. Calls an external
4968 function for each memory region.
4969 Returns the integer value returned by the callback. */
4972 find_memory_regions_callback (struct prmap
*map
,
4973 find_memory_region_ftype func
, void *data
)
4975 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
4977 (map
->pr_mflags
& MA_READ
) != 0,
4978 (map
->pr_mflags
& MA_WRITE
) != 0,
4979 (map
->pr_mflags
& MA_EXEC
) != 0,
4980 1, /* MODIFIED is unknown, pass it as true. */
4984 /* External interface. Calls a callback function once for each
4985 mapped memory region in the child process, passing as arguments:
4987 CORE_ADDR virtual_address,
4989 int read, TRUE if region is readable by the child
4990 int write, TRUE if region is writable by the child
4991 int execute TRUE if region is executable by the child.
4993 Stops iterating and returns the first non-zero value returned by
4997 proc_find_memory_regions (struct target_ops
*self
,
4998 find_memory_region_ftype func
, void *data
)
5000 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
5002 return iterate_over_mappings (pi
, func
, data
,
5003 find_memory_regions_callback
);
5006 /* Returns an ascii representation of a memory mapping's flags. */
5009 mappingflags (long flags
)
5011 static char asciiflags
[8];
5013 strcpy (asciiflags
, "-------");
5014 #if defined (MA_PHYS)
5015 if (flags
& MA_PHYS
)
5016 asciiflags
[0] = 'd';
5018 if (flags
& MA_STACK
)
5019 asciiflags
[1] = 's';
5020 if (flags
& MA_BREAK
)
5021 asciiflags
[2] = 'b';
5022 if (flags
& MA_SHARED
)
5023 asciiflags
[3] = 's';
5024 if (flags
& MA_READ
)
5025 asciiflags
[4] = 'r';
5026 if (flags
& MA_WRITE
)
5027 asciiflags
[5] = 'w';
5028 if (flags
& MA_EXEC
)
5029 asciiflags
[6] = 'x';
5030 return (asciiflags
);
5033 /* Callback function, does the actual work for 'info proc
5037 info_mappings_callback (struct prmap
*map
, find_memory_region_ftype ignore
,
5040 unsigned int pr_off
;
5042 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5043 pr_off
= (unsigned int) map
->pr_offset
;
5045 pr_off
= map
->pr_off
;
5048 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
5049 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
5050 (unsigned long) map
->pr_vaddr
,
5051 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5052 (unsigned long) map
->pr_size
,
5054 mappingflags (map
->pr_mflags
));
5056 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
5057 (unsigned long) map
->pr_vaddr
,
5058 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5059 (unsigned long) map
->pr_size
,
5061 mappingflags (map
->pr_mflags
));
5066 /* Implement the "info proc mappings" subcommand. */
5069 info_proc_mappings (procinfo
*pi
, int summary
)
5072 return; /* No output for summary mode. */
5074 printf_filtered (_("Mapped address spaces:\n\n"));
5075 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
5076 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
5083 printf_filtered (" %18s %18s %10s %10s %7s\n",
5090 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
5091 printf_filtered ("\n");
5094 /* Implement the "info proc" command. */
5097 procfs_info_proc (struct target_ops
*ops
, const char *args
,
5098 enum info_proc_what what
)
5100 struct cleanup
*old_chain
;
5101 procinfo
*process
= NULL
;
5102 procinfo
*thread
= NULL
;
5119 error (_("Not supported on this target."));
5122 old_chain
= make_cleanup (null_cleanup
, 0);
5123 gdb_argv
built_argv (args
);
5124 for (char *arg
: argv
)
5126 if (isdigit (arg
[0]))
5128 pid
= strtoul (arg
, &tmp
, 10);
5130 tid
= strtoul (++tmp
, NULL
, 10);
5132 else if (arg
[0] == '/')
5134 tid
= strtoul (arg
+ 1, NULL
, 10);
5138 pid
= ptid_get_pid (inferior_ptid
);
5140 error (_("No current process: you must name one."));
5143 /* Have pid, will travel.
5144 First see if it's a process we're already debugging. */
5145 process
= find_procinfo (pid
, 0);
5146 if (process
== NULL
)
5148 /* No. So open a procinfo for it, but
5149 remember to close it again when finished. */
5150 process
= create_procinfo (pid
, 0);
5151 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5152 if (!open_procinfo_files (process
, FD_CTL
))
5153 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5157 thread
= create_procinfo (pid
, tid
);
5161 printf_filtered (_("process %d flags:\n"), process
->pid
);
5162 proc_prettyprint_flags (proc_flags (process
), 1);
5163 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5164 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5165 if (proc_get_nthreads (process
) > 1)
5166 printf_filtered ("Process has %d threads.\n",
5167 proc_get_nthreads (process
));
5171 printf_filtered (_("thread %d flags:\n"), thread
->tid
);
5172 proc_prettyprint_flags (proc_flags (thread
), 1);
5173 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5174 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5179 info_proc_mappings (process
, 0);
5182 do_cleanups (old_chain
);
5185 /* Modify the status of the system call identified by SYSCALLNUM in
5186 the set of syscalls that are currently traced/debugged.
5188 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
5189 will be updated. Otherwise, the exit syscalls set will be updated.
5191 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
5192 will be disabled. */
5195 proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
, int entry_or_exit
,
5196 int mode
, int from_tty
)
5200 if (entry_or_exit
== PR_SYSENTRY
)
5201 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5203 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5206 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5208 if (mode
== FLAG_SET
)
5209 gdb_praddsysset (sysset
, syscallnum
);
5211 gdb_prdelsysset (sysset
, syscallnum
);
5213 if (entry_or_exit
== PR_SYSENTRY
)
5215 if (!proc_set_traced_sysentry (pi
, sysset
))
5216 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5220 if (!proc_set_traced_sysexit (pi
, sysset
))
5221 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5226 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5230 if (ptid_get_pid (inferior_ptid
) <= 0)
5231 error (_("you must be debugging a process to use this command."));
5233 if (args
== NULL
|| args
[0] == 0)
5234 error_no_arg (_("system call to trace"));
5236 pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
5237 if (isdigit (args
[0]))
5239 const int syscallnum
= atoi (args
);
5241 proc_trace_syscalls_1 (pi
, syscallnum
, entry_or_exit
, mode
, from_tty
);
5246 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5248 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5252 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5254 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5258 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5260 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5264 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5266 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5270 _initialize_procfs (void)
5272 observer_attach_inferior_created (procfs_inferior_created
);
5274 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5275 _("Give a trace of entries into the syscall."));
5276 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5277 _("Give a trace of exits from the syscall."));
5278 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5279 _("Cancel a trace of entries into the syscall."));
5280 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5281 _("Cancel a trace of exits from the syscall."));
5284 /* =================== END, GDB "MODULE" =================== */
5288 /* miscellaneous stubs: */
5290 /* The following satisfy a few random symbols mostly created by the
5291 solaris threads implementation, which I will chase down later. */
5293 /* Return a pid for which we guarantee we will be able to find a
5297 procfs_first_available (void)
5299 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);
5302 /* =================== GCORE .NOTE "MODULE" =================== */
5303 #if defined (PIOCOPENLWP) || defined (PCAGENT)
5304 /* gcore only implemented on solaris (so far) */
5307 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
5308 char *note_data
, int *note_size
,
5309 enum gdb_signal stop_signal
)
5311 struct regcache
*regcache
= get_thread_regcache (ptid
);
5312 gdb_gregset_t gregs
;
5313 gdb_fpregset_t fpregs
;
5314 unsigned long merged_pid
;
5316 merged_pid
= ptid_get_lwp (ptid
) << 16 | ptid_get_pid (ptid
);
5318 /* This part is the old method for fetching registers.
5319 It should be replaced by the newer one using regsets
5320 once it is implemented in this platform:
5321 gdbarch_iterate_over_regset_sections(). */
5323 scoped_restore save_inferior_ptid
= make_scoped_restore (&inferior_ptid
);
5324 inferior_ptid
= ptid
;
5325 target_fetch_registers (regcache
, -1);
5327 fill_gregset (regcache
, &gregs
, -1);
5328 #if defined (NEW_PROC_API)
5329 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
5336 note_data
= (char *) elfcore_write_prstatus (obfd
,
5343 fill_fpregset (regcache
, &fpregs
, -1);
5344 note_data
= (char *) elfcore_write_prfpreg (obfd
,
5353 struct procfs_corefile_thread_data
{
5357 enum gdb_signal stop_signal
;
5361 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
5363 struct procfs_corefile_thread_data
*args
5364 = (struct procfs_corefile_thread_data
*) data
;
5368 ptid_t ptid
= ptid_build (pi
->pid
, thread
->tid
, 0);
5370 args
->note_data
= procfs_do_thread_registers (args
->obfd
, ptid
,
5379 find_signalled_thread (struct thread_info
*info
, void *data
)
5381 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
5382 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
5388 static enum gdb_signal
5389 find_stop_signal (void)
5391 struct thread_info
*info
=
5392 iterate_over_threads (find_signalled_thread
, NULL
);
5395 return info
->suspend
.stop_signal
;
5397 return GDB_SIGNAL_0
;
5401 procfs_make_note_section (struct target_ops
*self
, bfd
*obfd
, int *note_size
)
5403 struct cleanup
*old_chain
;
5404 gdb_gregset_t gregs
;
5405 gdb_fpregset_t fpregs
;
5406 char fname
[16] = {'\0'};
5407 char psargs
[80] = {'\0'};
5408 procinfo
*pi
= find_procinfo_or_die (ptid_get_pid (inferior_ptid
), 0);
5409 char *note_data
= NULL
;
5411 struct procfs_corefile_thread_data thread_args
;
5414 enum gdb_signal stop_signal
;
5416 if (get_exec_file (0))
5418 strncpy (fname
, lbasename (get_exec_file (0)), sizeof (fname
));
5419 fname
[sizeof (fname
) - 1] = 0;
5420 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
5421 psargs
[sizeof (psargs
) - 1] = 0;
5423 inf_args
= get_inferior_args ();
5424 if (inf_args
&& *inf_args
&&
5425 strlen (inf_args
) < ((int) sizeof (psargs
) - (int) strlen (psargs
)))
5427 strncat (psargs
, " ",
5428 sizeof (psargs
) - strlen (psargs
));
5429 strncat (psargs
, inf_args
,
5430 sizeof (psargs
) - strlen (psargs
));
5434 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
5440 stop_signal
= find_stop_signal ();
5443 fill_gregset (get_current_regcache (), &gregs
, -1);
5444 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
5445 ptid_get_pid (inferior_ptid
),
5446 stop_signal
, &gregs
);
5449 thread_args
.obfd
= obfd
;
5450 thread_args
.note_data
= note_data
;
5451 thread_args
.note_size
= note_size
;
5452 thread_args
.stop_signal
= stop_signal
;
5453 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
,
5455 note_data
= thread_args
.note_data
;
5457 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
5461 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
5462 "CORE", NT_AUXV
, auxv
, auxv_len
);
5468 #else /* !Solaris */
5470 procfs_make_note_section (struct target_ops
*self
, bfd
*obfd
, int *note_size
)
5472 error (_("gcore not implemented for this host."));
5473 return NULL
; /* lint */
5475 #endif /* Solaris */
5476 /* =================== END GCORE .NOTE "MODULE" =================== */