1 /* Machine independent support for SVR4 /proc (process file system) for GDB.
3 Copyright (C) 1999-2013 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/>. */
27 #include "elf-bfd.h" /* for elfcore_write_* */
29 #include "gdbthread.h"
31 #include "inf-child.h"
33 #if defined (NEW_PROC_API)
34 #define _STRUCTURED_PROC 1 /* Should be done by configure script. */
37 #include <sys/procfs.h>
38 #ifdef HAVE_SYS_FAULT_H
39 #include <sys/fault.h>
41 #ifdef HAVE_SYS_SYSCALL_H
42 #include <sys/syscall.h>
44 #include <sys/errno.h>
49 #include "gdb_string.h"
50 #include "gdb_assert.h"
56 /* This module provides the interface between GDB and the
57 /proc file system, which is used on many versions of Unix
58 as a means for debuggers to control other processes.
60 Examples of the systems that use this interface are:
67 /proc works by imitating a file system: you open a simulated file
68 that represents the process you wish to interact with, and perform
69 operations on that "file" in order to examine or change the state
72 The most important thing to know about /proc and this module is
73 that there are two very different interfaces to /proc:
75 One that uses the ioctl system call, and another that uses read
76 and write system calls.
78 This module has to support both /proc interfaces. This means that
79 there are two different ways of doing every basic operation.
81 In order to keep most of the code simple and clean, I have defined
82 an interface "layer" which hides all these system calls. An ifdef
83 (NEW_PROC_API) determines which interface we are using, and most or
84 all occurrances of this ifdef should be confined to this interface
87 /* Determine which /proc API we are using: The ioctl API defines
88 PIOCSTATUS, while the read/write (multiple fd) API never does. */
91 #include <sys/types.h>
92 #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
95 #include <fcntl.h> /* for O_RDONLY */
96 #include <unistd.h> /* for "X_OK" */
97 #include "gdb_stat.h" /* for struct stat */
99 /* Note: procfs-utils.h must be included after the above system header
100 files, because it redefines various system calls using macros.
101 This may be incompatible with the prototype declarations. */
103 #include "proc-utils.h"
105 /* Prototypes for supply_gregset etc. */
108 /* =================== TARGET_OPS "MODULE" =================== */
110 /* This module defines the GDB target vector and its methods. */
112 static void procfs_attach (struct target_ops
*, char *, int);
113 static void procfs_detach (struct target_ops
*, char *, int);
114 static void procfs_resume (struct target_ops
*,
115 ptid_t
, int, enum gdb_signal
);
116 static void procfs_stop (ptid_t
);
117 static void procfs_files_info (struct target_ops
*);
118 static void procfs_fetch_registers (struct target_ops
*,
119 struct regcache
*, int);
120 static void procfs_store_registers (struct target_ops
*,
121 struct regcache
*, int);
122 static void procfs_pass_signals (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
*, char *,
126 char *, char **, int);
127 static ptid_t
procfs_wait (struct target_ops
*,
128 ptid_t
, struct target_waitstatus
*, int);
129 static int procfs_xfer_memory (CORE_ADDR
, gdb_byte
*, int, int,
130 struct mem_attrib
*attrib
,
131 struct target_ops
*);
132 static LONGEST
procfs_xfer_partial (struct target_ops
*ops
,
133 enum target_object object
,
136 const gdb_byte
*writebuf
,
137 ULONGEST offset
, LONGEST len
);
139 static int procfs_thread_alive (struct target_ops
*ops
, ptid_t
);
141 static void procfs_find_new_threads (struct target_ops
*ops
);
142 static char *procfs_pid_to_str (struct target_ops
*, ptid_t
);
144 static int proc_find_memory_regions (find_memory_region_ftype
, void *);
146 static char * procfs_make_note_section (bfd
*, int *);
148 static int procfs_can_use_hw_breakpoint (int, int, int);
150 static void procfs_info_proc (struct target_ops
*, char *,
151 enum info_proc_what
);
153 #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
154 /* When GDB is built as 64-bit application on Solaris, the auxv data
155 is presented in 64-bit format. We need to provide a custom parser
158 procfs_auxv_parse (struct target_ops
*ops
, gdb_byte
**readptr
,
159 gdb_byte
*endptr
, CORE_ADDR
*typep
, CORE_ADDR
*valp
)
161 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
162 gdb_byte
*ptr
= *readptr
;
167 if (endptr
- ptr
< 8 * 2)
170 *typep
= extract_unsigned_integer (ptr
, 4, byte_order
);
172 /* The size of data is always 64-bit. If the application is 32-bit,
173 it will be zero extended, as expected. */
174 *valp
= extract_unsigned_integer (ptr
, 8, byte_order
);
185 struct target_ops
*t
= inf_child_target ();
187 t
->to_shortname
= "procfs";
188 t
->to_longname
= "Unix /proc child process";
190 "Unix /proc child process (started by the \"run\" command).";
191 t
->to_create_inferior
= procfs_create_inferior
;
192 t
->to_kill
= procfs_kill_inferior
;
193 t
->to_mourn_inferior
= procfs_mourn_inferior
;
194 t
->to_attach
= procfs_attach
;
195 t
->to_detach
= procfs_detach
;
196 t
->to_wait
= procfs_wait
;
197 t
->to_resume
= procfs_resume
;
198 t
->to_fetch_registers
= procfs_fetch_registers
;
199 t
->to_store_registers
= procfs_store_registers
;
200 t
->to_xfer_partial
= procfs_xfer_partial
;
201 t
->deprecated_xfer_memory
= procfs_xfer_memory
;
202 t
->to_pass_signals
= procfs_pass_signals
;
203 t
->to_files_info
= procfs_files_info
;
204 t
->to_stop
= procfs_stop
;
206 t
->to_find_new_threads
= procfs_find_new_threads
;
207 t
->to_thread_alive
= procfs_thread_alive
;
208 t
->to_pid_to_str
= procfs_pid_to_str
;
210 t
->to_has_thread_control
= tc_schedlock
;
211 t
->to_find_memory_regions
= proc_find_memory_regions
;
212 t
->to_make_corefile_notes
= procfs_make_note_section
;
213 t
->to_info_proc
= procfs_info_proc
;
215 #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64)
216 t
->to_auxv_parse
= procfs_auxv_parse
;
219 t
->to_magic
= OPS_MAGIC
;
224 /* =================== END, TARGET_OPS "MODULE" =================== */
226 /* World Unification:
228 Put any typedefs, defines etc. here that are required for the
229 unification of code that handles different versions of /proc. */
231 #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
233 enum { READ_WATCHFLAG
= WA_READ
,
234 WRITE_WATCHFLAG
= WA_WRITE
,
235 EXEC_WATCHFLAG
= WA_EXEC
,
236 AFTER_WATCHFLAG
= WA_TRAPAFTER
239 #else /* Irix method for watchpoints */
240 enum { READ_WATCHFLAG
= MA_READ
,
241 WRITE_WATCHFLAG
= MA_WRITE
,
242 EXEC_WATCHFLAG
= MA_EXEC
,
243 AFTER_WATCHFLAG
= 0 /* trapafter not implemented */
248 #ifdef HAVE_PR_SIGSET_T
249 typedef pr_sigset_t gdb_sigset_t
;
251 typedef sigset_t gdb_sigset_t
;
255 #ifdef HAVE_PR_SIGACTION64_T
256 typedef pr_sigaction64_t gdb_sigaction_t
;
258 typedef struct sigaction gdb_sigaction_t
;
262 #ifdef HAVE_PR_SIGINFO64_T
263 typedef pr_siginfo64_t gdb_siginfo_t
;
265 typedef siginfo_t gdb_siginfo_t
;
268 /* On mips-irix, praddset and prdelset are defined in such a way that
269 they return a value, which causes GCC to emit a -Wunused error
270 because the returned value is not used. Prevent this warning
271 by casting the return value to void. On sparc-solaris, this issue
272 does not exist because the definition of these macros already include
273 that cast to void. */
274 #define gdb_praddset(sp, flag) ((void) praddset (sp, flag))
275 #define gdb_prdelset(sp, flag) ((void) prdelset (sp, flag))
277 /* gdb_premptysysset */
279 #define gdb_premptysysset premptysysset
281 #define gdb_premptysysset premptyset
286 #define gdb_praddsysset praddsysset
288 #define gdb_praddsysset gdb_praddset
293 #define gdb_prdelsysset prdelsysset
295 #define gdb_prdelsysset gdb_prdelset
298 /* prissyssetmember */
299 #ifdef prissyssetmember
300 #define gdb_pr_issyssetmember prissyssetmember
302 #define gdb_pr_issyssetmember prismember
305 /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
306 as intuitively descriptive as it could be, so we'll define
307 DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of
308 this writing, this feature is only found on AIX5 systems and
309 basically means that the set of syscalls is not fixed. I.e,
310 there's no nice table that one can #include to get all of the
311 syscall numbers. Instead, they're stored in /proc/PID/sysent
312 for each process. We are at least guaranteed that they won't
313 change over the lifetime of the process. But each process could
314 (in theory) have different syscall numbers. */
315 #ifdef HAVE_PRSYSENT_T
316 #define DYNAMIC_SYSCALLS
321 /* =================== STRUCT PROCINFO "MODULE" =================== */
323 /* FIXME: this comment will soon be out of date W.R.T. threads. */
325 /* The procinfo struct is a wrapper to hold all the state information
326 concerning a /proc process. There should be exactly one procinfo
327 for each process, and since GDB currently can debug only one
328 process at a time, that means there should be only one procinfo.
329 All of the LWP's of a process can be accessed indirectly thru the
330 single process procinfo.
332 However, against the day when GDB may debug more than one process,
333 this data structure is kept in a list (which for now will hold no
334 more than one member), and many functions will have a pointer to a
335 procinfo as an argument.
337 There will be a separate procinfo structure for use by the (not yet
338 implemented) "info proc" command, so that we can print useful
339 information about any random process without interfering with the
340 inferior's procinfo information. */
343 /* format strings for /proc paths */
344 # ifndef CTL_PROC_NAME_FMT
345 # define MAIN_PROC_NAME_FMT "/proc/%d"
346 # define CTL_PROC_NAME_FMT "/proc/%d/ctl"
347 # define AS_PROC_NAME_FMT "/proc/%d/as"
348 # define MAP_PROC_NAME_FMT "/proc/%d/map"
349 # define STATUS_PROC_NAME_FMT "/proc/%d/status"
350 # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
352 /* the name of the proc status struct depends on the implementation */
353 typedef pstatus_t gdb_prstatus_t
;
354 typedef lwpstatus_t gdb_lwpstatus_t
;
355 #else /* ! NEW_PROC_API */
356 /* format strings for /proc paths */
357 # ifndef CTL_PROC_NAME_FMT
358 # define MAIN_PROC_NAME_FMT "/proc/%05d"
359 # define CTL_PROC_NAME_FMT "/proc/%05d"
360 # define AS_PROC_NAME_FMT "/proc/%05d"
361 # define MAP_PROC_NAME_FMT "/proc/%05d"
362 # define STATUS_PROC_NAME_FMT "/proc/%05d"
363 # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
365 /* The name of the proc status struct depends on the implementation. */
366 typedef prstatus_t gdb_prstatus_t
;
367 typedef prstatus_t gdb_lwpstatus_t
;
368 #endif /* NEW_PROC_API */
370 typedef struct procinfo
{
371 struct procinfo
*next
;
372 int pid
; /* Process ID */
373 int tid
; /* Thread/LWP id */
377 int ignore_next_sigstop
;
379 /* The following four fd fields may be identical, or may contain
380 several different fd's, depending on the version of /proc
381 (old ioctl or new read/write). */
383 int ctl_fd
; /* File descriptor for /proc control file */
385 /* The next three file descriptors are actually only needed in the
386 read/write, multiple-file-descriptor implemenation
387 (NEW_PROC_API). However, to avoid a bunch of #ifdefs in the
388 code, we will use them uniformly by (in the case of the ioctl
389 single-file-descriptor implementation) filling them with copies
390 of the control fd. */
391 int status_fd
; /* File descriptor for /proc status file */
392 int as_fd
; /* File descriptor for /proc as file */
394 char pathname
[MAX_PROC_NAME_SIZE
]; /* Pathname to /proc entry */
396 fltset_t saved_fltset
; /* Saved traced hardware fault set */
397 gdb_sigset_t saved_sigset
; /* Saved traced signal set */
398 gdb_sigset_t saved_sighold
; /* Saved held signal set */
399 sysset_t
*saved_exitset
; /* Saved traced system call exit set */
400 sysset_t
*saved_entryset
; /* Saved traced system call entry set */
402 gdb_prstatus_t prstatus
; /* Current process status info */
405 gdb_fpregset_t fpregset
; /* Current floating point registers */
408 #ifdef DYNAMIC_SYSCALLS
409 int num_syscalls
; /* Total number of syscalls */
410 char **syscall_names
; /* Syscall number to name map */
413 struct procinfo
*thread_list
;
415 int status_valid
: 1;
417 int fpregs_valid
: 1;
418 int threads_valid
: 1;
421 static char errmsg
[128]; /* shared error msg buffer */
423 /* Function prototypes for procinfo module: */
425 static procinfo
*find_procinfo_or_die (int pid
, int tid
);
426 static procinfo
*find_procinfo (int pid
, int tid
);
427 static procinfo
*create_procinfo (int pid
, int tid
);
428 static void destroy_procinfo (procinfo
* p
);
429 static void do_destroy_procinfo_cleanup (void *);
430 static void dead_procinfo (procinfo
* p
, char *msg
, int killp
);
431 static int open_procinfo_files (procinfo
* p
, int which
);
432 static void close_procinfo_files (procinfo
* p
);
433 static int sysset_t_size (procinfo
*p
);
434 static sysset_t
*sysset_t_alloc (procinfo
* pi
);
435 #ifdef DYNAMIC_SYSCALLS
436 static void load_syscalls (procinfo
*pi
);
437 static void free_syscalls (procinfo
*pi
);
438 static int find_syscall (procinfo
*pi
, char *name
);
439 #endif /* DYNAMIC_SYSCALLS */
441 static int iterate_over_mappings
442 (procinfo
*pi
, find_memory_region_ftype child_func
, void *data
,
443 int (*func
) (struct prmap
*map
, find_memory_region_ftype child_func
,
446 /* The head of the procinfo list: */
447 static procinfo
* procinfo_list
;
449 /* Search the procinfo list. Return a pointer to procinfo, or NULL if
453 find_procinfo (int pid
, int tid
)
457 for (pi
= procinfo_list
; pi
; pi
= pi
->next
)
464 /* Don't check threads_valid. If we're updating the
465 thread_list, we want to find whatever threads are already
466 here. This means that in general it is the caller's
467 responsibility to check threads_valid and update before
468 calling find_procinfo, if the caller wants to find a new
471 for (pi
= pi
->thread_list
; pi
; pi
= pi
->next
)
479 /* Calls find_procinfo, but errors on failure. */
482 find_procinfo_or_die (int pid
, int tid
)
484 procinfo
*pi
= find_procinfo (pid
, tid
);
489 error (_("procfs: couldn't find pid %d "
490 "(kernel thread %d) in procinfo list."),
493 error (_("procfs: couldn't find pid %d in procinfo list."), pid
);
498 /* Wrapper for `open'. The appropriate open call is attempted; if
499 unsuccessful, it will be retried as many times as needed for the
500 EAGAIN and EINTR conditions.
502 For other conditions, retry the open a limited number of times. In
503 addition, a short sleep is imposed prior to retrying the open. The
504 reason for this sleep is to give the kernel a chance to catch up
505 and create the file in question in the event that GDB "wins" the
506 race to open a file before the kernel has created it. */
509 open_with_retry (const char *pathname
, int flags
)
511 int retries_remaining
, status
;
513 retries_remaining
= 2;
517 status
= open (pathname
, flags
);
519 if (status
>= 0 || retries_remaining
== 0)
521 else if (errno
!= EINTR
&& errno
!= EAGAIN
)
531 /* Open the file descriptor for the process or LWP. If NEW_PROC_API
532 is defined, we only open the control file descriptor; the others
533 are opened lazily as needed. Otherwise (if not NEW_PROC_API),
534 there is only one real file descriptor, but we keep multiple copies
535 of it so that the code that uses them does not have to be #ifdef'd.
536 Returns the file descriptor, or zero for failure. */
538 enum { FD_CTL
, FD_STATUS
, FD_AS
};
541 open_procinfo_files (procinfo
*pi
, int which
)
544 char tmp
[MAX_PROC_NAME_SIZE
];
548 /* This function is getting ALMOST long enough to break up into
549 several. Here is some rationale:
551 NEW_PROC_API (Solaris 2.6, Solaris 2.7):
552 There are several file descriptors that may need to be open
553 for any given process or LWP. The ones we're intereted in are:
554 - control (ctl) write-only change the state
555 - status (status) read-only query the state
556 - address space (as) read/write access memory
557 - map (map) read-only virtual addr map
558 Most of these are opened lazily as they are needed.
559 The pathnames for the 'files' for an LWP look slightly
560 different from those of a first-class process:
561 Pathnames for a process (<proc-id>):
563 /proc/<proc-id>/status
566 Pathnames for an LWP (lwp-id):
567 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
568 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
569 An LWP has no map or address space file descriptor, since
570 the memory map and address space are shared by all LWPs.
572 Everyone else (Solaris 2.5, Irix, OSF)
573 There is only one file descriptor for each process or LWP.
574 For convenience, we copy the same file descriptor into all
575 three fields of the procinfo struct (ctl_fd, status_fd, and
576 as_fd, see NEW_PROC_API above) so that code that uses them
577 doesn't need any #ifdef's.
582 Each LWP has an independent file descriptor, but these
583 are not obtained via the 'open' system call like the rest:
584 instead, they're obtained thru an ioctl call (PIOCOPENLWP)
585 to the file descriptor of the parent process.
588 These do not even have their own independent file descriptor.
589 All operations are carried out on the file descriptor of the
590 parent process. Therefore we just call open again for each
591 thread, getting a new handle for the same 'file'. */
594 /* In this case, there are several different file descriptors that
595 we might be asked to open. The control file descriptor will be
596 opened early, but the others will be opened lazily as they are
599 strcpy (tmp
, pi
->pathname
);
600 switch (which
) { /* Which file descriptor to open? */
603 strcat (tmp
, "/lwpctl");
605 strcat (tmp
, "/ctl");
606 fd
= open_with_retry (tmp
, O_WRONLY
);
613 return 0; /* There is no 'as' file descriptor for an lwp. */
615 fd
= open_with_retry (tmp
, O_RDWR
);
622 strcat (tmp
, "/lwpstatus");
624 strcat (tmp
, "/status");
625 fd
= open_with_retry (tmp
, O_RDONLY
);
631 return 0; /* unknown file descriptor */
633 #else /* not NEW_PROC_API */
634 /* In this case, there is only one file descriptor for each procinfo
635 (ie. each process or LWP). In fact, only the file descriptor for
636 the process can actually be opened by an 'open' system call. The
637 ones for the LWPs have to be obtained thru an IOCTL call on the
638 process's file descriptor.
640 For convenience, we copy each procinfo's single file descriptor
641 into all of the fields occupied by the several file descriptors
642 of the NEW_PROC_API implementation. That way, the code that uses
643 them can be written without ifdefs. */
646 #ifdef PIOCTSTATUS /* OSF */
647 /* Only one FD; just open it. */
648 if ((fd
= open_with_retry (pi
->pathname
, O_RDWR
)) < 0)
650 #else /* Sol 2.5, Irix, other? */
651 if (pi
->tid
== 0) /* Master procinfo for the process */
653 fd
= open_with_retry (pi
->pathname
, O_RDWR
);
657 else /* LWP thread procinfo */
659 #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
663 /* Find the procinfo for the entire process. */
664 if ((process
= find_procinfo (pi
->pid
, 0)) == NULL
)
667 /* Now obtain the file descriptor for the LWP. */
668 if ((fd
= ioctl (process
->ctl_fd
, PIOCOPENLWP
, &lwpid
)) < 0)
670 #else /* Irix, other? */
671 return 0; /* Don't know how to open threads. */
672 #endif /* Sol 2.5 PIOCOPENLWP */
674 #endif /* OSF PIOCTSTATUS */
675 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= fd
;
676 #endif /* NEW_PROC_API */
678 return 1; /* success */
681 /* Allocate a data structure and link it into the procinfo list.
682 First tries to find a pre-existing one (FIXME: why?). Returns the
683 pointer to new procinfo struct. */
686 create_procinfo (int pid
, int tid
)
688 procinfo
*pi
, *parent
= NULL
;
690 if ((pi
= find_procinfo (pid
, tid
)))
691 return pi
; /* Already exists, nothing to do. */
693 /* Find parent before doing malloc, to save having to cleanup. */
695 parent
= find_procinfo_or_die (pid
, 0); /* FIXME: should I
697 doesn't exist yet? */
699 pi
= (procinfo
*) xmalloc (sizeof (procinfo
));
700 memset (pi
, 0, sizeof (procinfo
));
704 #ifdef DYNAMIC_SYSCALLS
708 pi
->saved_entryset
= sysset_t_alloc (pi
);
709 pi
->saved_exitset
= sysset_t_alloc (pi
);
711 /* Chain into list. */
714 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
715 pi
->next
= procinfo_list
;
721 sprintf (pi
->pathname
, "/proc/%05d/lwp/%d", pid
, tid
);
723 sprintf (pi
->pathname
, MAIN_PROC_NAME_FMT
, pid
);
725 pi
->next
= parent
->thread_list
;
726 parent
->thread_list
= pi
;
731 /* Close all file descriptors associated with the procinfo. */
734 close_procinfo_files (procinfo
*pi
)
741 if (pi
->status_fd
> 0)
742 close (pi
->status_fd
);
744 pi
->ctl_fd
= pi
->as_fd
= pi
->status_fd
= 0;
747 /* Destructor function. Close, unlink and deallocate the object. */
750 destroy_one_procinfo (procinfo
**list
, procinfo
*pi
)
754 /* Step one: unlink the procinfo from its list. */
758 for (ptr
= *list
; ptr
; ptr
= ptr
->next
)
761 ptr
->next
= pi
->next
;
765 /* Step two: close any open file descriptors. */
766 close_procinfo_files (pi
);
768 /* Step three: free the memory. */
769 #ifdef DYNAMIC_SYSCALLS
772 xfree (pi
->saved_entryset
);
773 xfree (pi
->saved_exitset
);
778 destroy_procinfo (procinfo
*pi
)
782 if (pi
->tid
!= 0) /* Destroy a thread procinfo. */
784 tmp
= find_procinfo (pi
->pid
, 0); /* Find the parent process. */
785 destroy_one_procinfo (&tmp
->thread_list
, pi
);
787 else /* Destroy a process procinfo and all its threads. */
789 /* First destroy the children, if any; */
790 while (pi
->thread_list
!= NULL
)
791 destroy_one_procinfo (&pi
->thread_list
, pi
->thread_list
);
792 /* Then destroy the parent. Genocide!!! */
793 destroy_one_procinfo (&procinfo_list
, pi
);
798 do_destroy_procinfo_cleanup (void *pi
)
800 destroy_procinfo (pi
);
803 enum { NOKILL
, KILL
};
805 /* To be called on a non_recoverable error for a procinfo. Prints
806 error messages, optionally sends a SIGKILL to the process, then
807 destroys the data structure. */
810 dead_procinfo (procinfo
*pi
, char *msg
, int kill_p
)
816 print_sys_errmsg (pi
->pathname
, errno
);
820 sprintf (procfile
, "process %d", pi
->pid
);
821 print_sys_errmsg (procfile
, errno
);
824 kill (pi
->pid
, SIGKILL
);
826 destroy_procinfo (pi
);
830 /* Returns the (complete) size of a sysset_t struct. Normally, this
831 is just sizeof (sysset_t), but in the case of Monterey/64, the
832 actual size of sysset_t isn't known until runtime. */
835 sysset_t_size (procinfo
* pi
)
837 #ifndef DYNAMIC_SYSCALLS
838 return sizeof (sysset_t
);
840 return sizeof (sysset_t
) - sizeof (uint64_t)
841 + sizeof (uint64_t) * ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
842 / (8 * sizeof (uint64_t)));
846 /* Allocate and (partially) initialize a sysset_t struct. */
849 sysset_t_alloc (procinfo
* pi
)
852 int size
= sysset_t_size (pi
);
854 ret
= xmalloc (size
);
855 #ifdef DYNAMIC_SYSCALLS
856 ret
->pr_size
= ((pi
->num_syscalls
+ (8 * sizeof (uint64_t) - 1))
857 / (8 * sizeof (uint64_t)));
862 #ifdef DYNAMIC_SYSCALLS
864 /* Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
865 pi->num_syscalls with the number of syscalls and pi->syscall_names
866 with the names. (Certain numbers may be skipped in which case the
867 names for these numbers will be left as NULL.) */
869 #define MAX_SYSCALL_NAME_LENGTH 256
870 #define MAX_SYSCALLS 65536
873 load_syscalls (procinfo
*pi
)
875 char pathname
[MAX_PROC_NAME_SIZE
];
878 prsyscall_t
*syscalls
;
879 int i
, size
, maxcall
;
880 struct cleanup
*cleanups
;
882 pi
->num_syscalls
= 0;
883 pi
->syscall_names
= 0;
885 /* Open the file descriptor for the sysent file. */
886 sprintf (pathname
, "/proc/%d/sysent", pi
->pid
);
887 sysent_fd
= open_with_retry (pathname
, O_RDONLY
);
890 error (_("load_syscalls: Can't open /proc/%d/sysent"), pi
->pid
);
892 cleanups
= make_cleanup_close (sysent_fd
);
894 size
= sizeof header
- sizeof (prsyscall_t
);
895 if (read (sysent_fd
, &header
, size
) != size
)
897 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi
->pid
);
900 if (header
.pr_nsyscalls
== 0)
902 error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"),
906 size
= header
.pr_nsyscalls
* sizeof (prsyscall_t
);
907 syscalls
= xmalloc (size
);
908 make_cleanup (free_current_contents
, &syscalls
);
910 if (read (sysent_fd
, syscalls
, size
) != size
)
911 error (_("load_syscalls: Error reading /proc/%d/sysent"), pi
->pid
);
913 /* Find maximum syscall number. This may not be the same as
914 pr_nsyscalls since that value refers to the number of entries
915 in the table. (Also, the docs indicate that some system
916 call numbers may be skipped.) */
918 maxcall
= syscalls
[0].pr_number
;
920 for (i
= 1; i
< header
.pr_nsyscalls
; i
++)
921 if (syscalls
[i
].pr_number
> maxcall
922 && syscalls
[i
].pr_nameoff
> 0
923 && syscalls
[i
].pr_number
< MAX_SYSCALLS
)
924 maxcall
= syscalls
[i
].pr_number
;
926 pi
->num_syscalls
= maxcall
+1;
927 pi
->syscall_names
= xmalloc (pi
->num_syscalls
* sizeof (char *));
929 for (i
= 0; i
< pi
->num_syscalls
; i
++)
930 pi
->syscall_names
[i
] = NULL
;
932 /* Read the syscall names in. */
933 for (i
= 0; i
< header
.pr_nsyscalls
; i
++)
935 char namebuf
[MAX_SYSCALL_NAME_LENGTH
];
939 if (syscalls
[i
].pr_number
>= MAX_SYSCALLS
940 || syscalls
[i
].pr_number
< 0
941 || syscalls
[i
].pr_nameoff
<= 0
942 || (lseek (sysent_fd
, (off_t
) syscalls
[i
].pr_nameoff
, SEEK_SET
)
943 != (off_t
) syscalls
[i
].pr_nameoff
))
946 nread
= read (sysent_fd
, namebuf
, sizeof namebuf
);
950 callnum
= syscalls
[i
].pr_number
;
952 if (pi
->syscall_names
[callnum
] != NULL
)
954 /* FIXME: Generate warning. */
958 namebuf
[nread
-1] = '\0';
959 size
= strlen (namebuf
) + 1;
960 pi
->syscall_names
[callnum
] = xmalloc (size
);
961 strncpy (pi
->syscall_names
[callnum
], namebuf
, size
-1);
962 pi
->syscall_names
[callnum
][size
-1] = '\0';
965 do_cleanups (cleanups
);
968 /* Free the space allocated for the syscall names from the procinfo
972 free_syscalls (procinfo
*pi
)
974 if (pi
->syscall_names
)
978 for (i
= 0; i
< pi
->num_syscalls
; i
++)
979 if (pi
->syscall_names
[i
] != NULL
)
980 xfree (pi
->syscall_names
[i
]);
982 xfree (pi
->syscall_names
);
983 pi
->syscall_names
= 0;
987 /* Given a name, look up (and return) the corresponding syscall number.
988 If no match is found, return -1. */
991 find_syscall (procinfo
*pi
, char *name
)
995 for (i
= 0; i
< pi
->num_syscalls
; i
++)
997 if (pi
->syscall_names
[i
] && strcmp (name
, pi
->syscall_names
[i
]) == 0)
1004 /* =================== END, STRUCT PROCINFO "MODULE" =================== */
1006 /* =================== /proc "MODULE" =================== */
1008 /* This "module" is the interface layer between the /proc system API
1009 and the gdb target vector functions. This layer consists of access
1010 functions that encapsulate each of the basic operations that we
1011 need to use from the /proc API.
1013 The main motivation for this layer is to hide the fact that there
1014 are two very different implementations of the /proc API. Rather
1015 than have a bunch of #ifdefs all thru the gdb target vector
1016 functions, we do our best to hide them all in here. */
1018 static long proc_flags (procinfo
* pi
);
1019 static int proc_why (procinfo
* pi
);
1020 static int proc_what (procinfo
* pi
);
1021 static int proc_set_current_signal (procinfo
* pi
, int signo
);
1022 static int proc_get_current_thread (procinfo
* pi
);
1023 static int proc_iterate_over_threads
1025 int (*func
) (procinfo
*, procinfo
*, void *),
1029 proc_warn (procinfo
*pi
, char *func
, int line
)
1031 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1032 print_sys_errmsg (errmsg
, errno
);
1036 proc_error (procinfo
*pi
, char *func
, int line
)
1038 sprintf (errmsg
, "procfs: %s line %d, %s", func
, line
, pi
->pathname
);
1039 perror_with_name (errmsg
);
1042 /* Updates the status struct in the procinfo. There is a 'valid'
1043 flag, to let other functions know when this function needs to be
1044 called (so the status is only read when it is needed). The status
1045 file descriptor is also only opened when it is needed. Returns
1046 non-zero for success, zero for failure. */
1049 proc_get_status (procinfo
*pi
)
1051 /* Status file descriptor is opened "lazily". */
1052 if (pi
->status_fd
== 0 &&
1053 open_procinfo_files (pi
, FD_STATUS
) == 0)
1055 pi
->status_valid
= 0;
1060 if (lseek (pi
->status_fd
, 0, SEEK_SET
) < 0)
1061 pi
->status_valid
= 0; /* fail */
1064 /* Sigh... I have to read a different data structure,
1065 depending on whether this is a main process or an LWP. */
1067 pi
->status_valid
= (read (pi
->status_fd
,
1068 (char *) &pi
->prstatus
.pr_lwp
,
1069 sizeof (lwpstatus_t
))
1070 == sizeof (lwpstatus_t
));
1073 pi
->status_valid
= (read (pi
->status_fd
,
1074 (char *) &pi
->prstatus
,
1075 sizeof (gdb_prstatus_t
))
1076 == sizeof (gdb_prstatus_t
));
1079 #else /* ioctl method */
1080 #ifdef PIOCTSTATUS /* osf */
1081 if (pi
->tid
== 0) /* main process */
1083 /* Just read the danged status. Now isn't that simple? */
1085 (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1092 tid_t pr_error_thread
;
1093 struct prstatus status
;
1096 thread_status
.pr_count
= 1;
1097 thread_status
.status
.pr_tid
= pi
->tid
;
1098 win
= (ioctl (pi
->status_fd
, PIOCTSTATUS
, &thread_status
) >= 0);
1101 memcpy (&pi
->prstatus
, &thread_status
.status
,
1102 sizeof (pi
->prstatus
));
1103 pi
->status_valid
= 1;
1107 /* Just read the danged status. Now isn't that simple? */
1108 pi
->status_valid
= (ioctl (pi
->status_fd
, PIOCSTATUS
, &pi
->prstatus
) >= 0);
1112 if (pi
->status_valid
)
1114 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1117 proc_get_current_thread (pi
));
1120 /* The status struct includes general regs, so mark them valid too. */
1121 pi
->gregs_valid
= pi
->status_valid
;
1123 /* In the read/write multiple-fd model, the status struct includes
1124 the fp regs too, so mark them valid too. */
1125 pi
->fpregs_valid
= pi
->status_valid
;
1127 return pi
->status_valid
; /* True if success, false if failure. */
1130 /* Returns the process flags (pr_flags field). */
1133 proc_flags (procinfo
*pi
)
1135 if (!pi
->status_valid
)
1136 if (!proc_get_status (pi
))
1137 return 0; /* FIXME: not a good failure value (but what is?) */
1140 return pi
->prstatus
.pr_lwp
.pr_flags
;
1142 return pi
->prstatus
.pr_flags
;
1146 /* Returns the pr_why field (why the process stopped). */
1149 proc_why (procinfo
*pi
)
1151 if (!pi
->status_valid
)
1152 if (!proc_get_status (pi
))
1153 return 0; /* FIXME: not a good failure value (but what is?) */
1156 return pi
->prstatus
.pr_lwp
.pr_why
;
1158 return pi
->prstatus
.pr_why
;
1162 /* Returns the pr_what field (details of why the process stopped). */
1165 proc_what (procinfo
*pi
)
1167 if (!pi
->status_valid
)
1168 if (!proc_get_status (pi
))
1169 return 0; /* FIXME: not a good failure value (but what is?) */
1172 return pi
->prstatus
.pr_lwp
.pr_what
;
1174 return pi
->prstatus
.pr_what
;
1178 /* This function is only called when PI is stopped by a watchpoint.
1179 Assuming the OS supports it, write to *ADDR the data address which
1180 triggered it and return 1. Return 0 if it is not possible to know
1184 proc_watchpoint_address (procinfo
*pi
, CORE_ADDR
*addr
)
1186 if (!pi
->status_valid
)
1187 if (!proc_get_status (pi
))
1191 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
1192 builtin_type (target_gdbarch ())->builtin_data_ptr
,
1193 (gdb_byte
*) &pi
->prstatus
.pr_lwp
.pr_info
.si_addr
);
1195 *addr
= (CORE_ADDR
) gdbarch_pointer_to_address (target_gdbarch (),
1196 builtin_type (target_gdbarch ())->builtin_data_ptr
,
1197 (gdb_byte
*) &pi
->prstatus
.pr_info
.si_addr
);
1202 #ifndef PIOCSSPCACT /* The following is not supported on OSF. */
1204 /* Returns the pr_nsysarg field (number of args to the current
1208 proc_nsysarg (procinfo
*pi
)
1210 if (!pi
->status_valid
)
1211 if (!proc_get_status (pi
))
1215 return pi
->prstatus
.pr_lwp
.pr_nsysarg
;
1217 return pi
->prstatus
.pr_nsysarg
;
1221 /* Returns the pr_sysarg field (pointer to the arguments of current
1225 proc_sysargs (procinfo
*pi
)
1227 if (!pi
->status_valid
)
1228 if (!proc_get_status (pi
))
1232 return (long *) &pi
->prstatus
.pr_lwp
.pr_sysarg
;
1234 return (long *) &pi
->prstatus
.pr_sysarg
;
1237 #endif /* PIOCSSPCACT */
1239 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
1240 /* Returns the pr_cursig field (current signal). */
1243 proc_cursig (struct procinfo
*pi
)
1245 if (!pi
->status_valid
)
1246 if (!proc_get_status (pi
))
1247 return 0; /* FIXME: not a good failure value (but what is?) */
1250 return pi
->prstatus
.pr_lwp
.pr_cursig
;
1252 return pi
->prstatus
.pr_cursig
;
1255 #endif /* PROCFS_DONT_PIOCSSIG_CURSIG */
1257 /* === I appologize for the messiness of this function.
1258 === This is an area where the different versions of
1259 === /proc are more inconsistent than usual.
1261 Set or reset any of the following process flags:
1262 PR_FORK -- forked child will inherit trace flags
1263 PR_RLC -- traced process runs when last /proc file closed.
1264 PR_KLC -- traced process is killed when last /proc file closed.
1265 PR_ASYNC -- LWP's get to run/stop independently.
1267 There are three methods for doing this function:
1268 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1270 2) Middle: PIOCSET/PIOCRESET
1272 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1275 Note: Irix does not define PR_ASYNC.
1276 Note: OSF does not define PR_KLC.
1277 Note: OSF is the only one that can ONLY use the oldest method.
1281 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1282 mode -- 1 for set, 0 for reset.
1284 Returns non-zero for success, zero for failure. */
1286 enum { FLAG_RESET
, FLAG_SET
};
1289 proc_modify_flag (procinfo
*pi
, long flag
, long mode
)
1291 long win
= 0; /* default to fail */
1293 /* These operations affect the process as a whole, and applying them
1294 to an individual LWP has the same meaning as applying them to the
1295 main process. Therefore, if we're ever called with a pointer to
1296 an LWP's procinfo, let's substitute the process's procinfo and
1297 avoid opening the LWP's file descriptor unnecessarily. */
1300 pi
= find_procinfo_or_die (pi
->pid
, 0);
1302 #ifdef NEW_PROC_API /* Newest method: Newer Solarii. */
1303 /* First normalize the PCUNSET/PCRESET command opcode
1304 (which for no obvious reason has a different definition
1305 from one operating system to the next...) */
1307 #define GDBRESET PCUNSET
1310 #define GDBRESET PCRESET
1314 procfs_ctl_t arg
[2];
1316 if (mode
== FLAG_SET
) /* Set the flag (RLC, FORK, or ASYNC). */
1318 else /* Reset the flag. */
1322 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1325 #ifdef PIOCSET /* Irix/Sol5 method */
1326 if (mode
== FLAG_SET
) /* Set the flag (hopefully RLC, FORK, or ASYNC). */
1328 win
= (ioctl (pi
->ctl_fd
, PIOCSET
, &flag
) >= 0);
1330 else /* Reset the flag. */
1332 win
= (ioctl (pi
->ctl_fd
, PIOCRESET
, &flag
) >= 0);
1336 #ifdef PIOCSRLC /* Oldest method: OSF */
1339 if (mode
== FLAG_SET
) /* Set run-on-last-close */
1341 win
= (ioctl (pi
->ctl_fd
, PIOCSRLC
, NULL
) >= 0);
1343 else /* Clear run-on-last-close */
1345 win
= (ioctl (pi
->ctl_fd
, PIOCRRLC
, NULL
) >= 0);
1349 if (mode
== FLAG_SET
) /* Set inherit-on-fork */
1351 win
= (ioctl (pi
->ctl_fd
, PIOCSFORK
, NULL
) >= 0);
1353 else /* Clear inherit-on-fork */
1355 win
= (ioctl (pi
->ctl_fd
, PIOCRFORK
, NULL
) >= 0);
1359 win
= 0; /* Fail -- unknown flag (can't do PR_ASYNC). */
1366 /* The above operation renders the procinfo's cached pstatus
1368 pi
->status_valid
= 0;
1371 warning (_("procfs: modify_flag failed to turn %s %s"),
1372 flag
== PR_FORK
? "PR_FORK" :
1373 flag
== PR_RLC
? "PR_RLC" :
1375 flag
== PR_ASYNC
? "PR_ASYNC" :
1378 flag
== PR_KLC
? "PR_KLC" :
1381 mode
== FLAG_RESET
? "off" : "on");
1386 /* Set the run_on_last_close flag. Process with all threads will
1387 become runnable when debugger closes all /proc fds. Returns
1388 non-zero for success, zero for failure. */
1391 proc_set_run_on_last_close (procinfo
*pi
)
1393 return proc_modify_flag (pi
, PR_RLC
, FLAG_SET
);
1396 /* Reset the run_on_last_close flag. The process will NOT become
1397 runnable when debugger closes its file handles. Returns non-zero
1398 for success, zero for failure. */
1401 proc_unset_run_on_last_close (procinfo
*pi
)
1403 return proc_modify_flag (pi
, PR_RLC
, FLAG_RESET
);
1406 /* Reset inherit_on_fork flag. If the process forks a child while we
1407 are registered for events in the parent, then we will NOT recieve
1408 events from the child. Returns non-zero for success, zero for
1412 proc_unset_inherit_on_fork (procinfo
*pi
)
1414 return proc_modify_flag (pi
, PR_FORK
, FLAG_RESET
);
1418 /* Set PR_ASYNC flag. If one LWP stops because of a debug event
1419 (signal etc.), the remaining LWPs will continue to run. Returns
1420 non-zero for success, zero for failure. */
1423 proc_set_async (procinfo
*pi
)
1425 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_SET
);
1428 /* Reset PR_ASYNC flag. If one LWP stops because of a debug event
1429 (signal etc.), then all other LWPs will stop as well. Returns
1430 non-zero for success, zero for failure. */
1433 proc_unset_async (procinfo
*pi
)
1435 return proc_modify_flag (pi
, PR_ASYNC
, FLAG_RESET
);
1437 #endif /* PR_ASYNC */
1439 /* Request the process/LWP to stop. Does not wait. Returns non-zero
1440 for success, zero for failure. */
1443 proc_stop_process (procinfo
*pi
)
1447 /* We might conceivably apply this operation to an LWP, and the
1448 LWP's ctl file descriptor might not be open. */
1450 if (pi
->ctl_fd
== 0 &&
1451 open_procinfo_files (pi
, FD_CTL
) == 0)
1456 procfs_ctl_t cmd
= PCSTOP
;
1458 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1459 #else /* ioctl method */
1460 win
= (ioctl (pi
->ctl_fd
, PIOCSTOP
, &pi
->prstatus
) >= 0);
1461 /* Note: the call also reads the prstatus. */
1464 pi
->status_valid
= 1;
1465 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1468 proc_get_current_thread (pi
));
1476 /* Wait for the process or LWP to stop (block until it does). Returns
1477 non-zero for success, zero for failure. */
1480 proc_wait_for_stop (procinfo
*pi
)
1484 /* We should never have to apply this operation to any procinfo
1485 except the one for the main process. If that ever changes for
1486 any reason, then take out the following clause and replace it
1487 with one that makes sure the ctl_fd is open. */
1490 pi
= find_procinfo_or_die (pi
->pid
, 0);
1494 procfs_ctl_t cmd
= PCWSTOP
;
1496 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1497 /* We been runnin' and we stopped -- need to update status. */
1498 pi
->status_valid
= 0;
1500 #else /* ioctl method */
1501 win
= (ioctl (pi
->ctl_fd
, PIOCWSTOP
, &pi
->prstatus
) >= 0);
1502 /* Above call also refreshes the prstatus. */
1505 pi
->status_valid
= 1;
1506 PROC_PRETTYFPRINT_STATUS (proc_flags (pi
),
1509 proc_get_current_thread (pi
));
1516 /* Make the process or LWP runnable.
1518 Options (not all are implemented):
1520 - clear current fault
1521 - clear current signal
1522 - abort the current system call
1523 - stop as soon as finished with system call
1524 - (ioctl): set traced signal set
1525 - (ioctl): set held signal set
1526 - (ioctl): set traced fault set
1527 - (ioctl): set start pc (vaddr)
1529 Always clears the current fault. PI is the process or LWP to
1530 operate on. If STEP is true, set the process or LWP to trap after
1531 one instruction. If SIGNO is zero, clear the current signal if
1532 any; if non-zero, set the current signal to this one. Returns
1533 non-zero for success, zero for failure. */
1536 proc_run_process (procinfo
*pi
, int step
, int signo
)
1541 /* We will probably have to apply this operation to individual
1542 threads, so make sure the control file descriptor is open. */
1544 if (pi
->ctl_fd
== 0 &&
1545 open_procinfo_files (pi
, FD_CTL
) == 0)
1550 runflags
= PRCFAULT
; /* Always clear current fault. */
1555 else if (signo
!= -1) /* -1 means do nothing W.R.T. signals. */
1556 proc_set_current_signal (pi
, signo
);
1560 procfs_ctl_t cmd
[2];
1564 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
1566 #else /* ioctl method */
1570 memset (&prrun
, 0, sizeof (prrun
));
1571 prrun
.pr_flags
= runflags
;
1572 win
= (ioctl (pi
->ctl_fd
, PIOCRUN
, &prrun
) >= 0);
1579 /* Register to trace signals in the process or LWP. Returns non-zero
1580 for success, zero for failure. */
1583 proc_set_traced_signals (procinfo
*pi
, gdb_sigset_t
*sigset
)
1587 /* We should never have to apply this operation to any procinfo
1588 except the one for the main process. If that ever changes for
1589 any reason, then take out the following clause and replace it
1590 with one that makes sure the ctl_fd is open. */
1593 pi
= find_procinfo_or_die (pi
->pid
, 0);
1599 /* Use char array to avoid alignment issues. */
1600 char sigset
[sizeof (gdb_sigset_t
)];
1604 memcpy (&arg
.sigset
, sigset
, sizeof (gdb_sigset_t
));
1606 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1608 #else /* ioctl method */
1609 win
= (ioctl (pi
->ctl_fd
, PIOCSTRACE
, sigset
) >= 0);
1611 /* The above operation renders the procinfo's cached pstatus obsolete. */
1612 pi
->status_valid
= 0;
1615 warning (_("procfs: set_traced_signals failed"));
1619 /* Register to trace hardware faults in the process or LWP. Returns
1620 non-zero for success, zero for failure. */
1623 proc_set_traced_faults (procinfo
*pi
, fltset_t
*fltset
)
1627 /* We should never have to apply this operation to any procinfo
1628 except the one for the main process. If that ever changes for
1629 any reason, then take out the following clause and replace it
1630 with one that makes sure the ctl_fd is open. */
1633 pi
= find_procinfo_or_die (pi
->pid
, 0);
1639 /* Use char array to avoid alignment issues. */
1640 char fltset
[sizeof (fltset_t
)];
1644 memcpy (&arg
.fltset
, fltset
, sizeof (fltset_t
));
1646 win
= (write (pi
->ctl_fd
, (char *) &arg
, sizeof (arg
)) == sizeof (arg
));
1648 #else /* ioctl method */
1649 win
= (ioctl (pi
->ctl_fd
, PIOCSFAULT
, fltset
) >= 0);
1651 /* The above operation renders the procinfo's cached pstatus obsolete. */
1652 pi
->status_valid
= 0;
1657 /* Register to trace entry to system calls in the process or LWP.
1658 Returns non-zero for success, zero for failure. */
1661 proc_set_traced_sysentry (procinfo
*pi
, sysset_t
*sysset
)
1665 /* We should never have to apply this operation to any procinfo
1666 except the one for the main process. If that ever changes for
1667 any reason, then take out the following clause and replace it
1668 with one that makes sure the ctl_fd is open. */
1671 pi
= find_procinfo_or_die (pi
->pid
, 0);
1675 struct gdb_proc_ctl_pcsentry
{
1677 /* Use char array to avoid alignment issues. */
1678 char sysset
[sizeof (sysset_t
)];
1680 int argp_size
= sizeof (struct gdb_proc_ctl_pcsentry
)
1682 + sysset_t_size (pi
);
1684 argp
= xmalloc (argp_size
);
1686 argp
->cmd
= PCSENTRY
;
1687 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1689 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1692 #else /* ioctl method */
1693 win
= (ioctl (pi
->ctl_fd
, PIOCSENTRY
, sysset
) >= 0);
1695 /* The above operation renders the procinfo's cached pstatus
1697 pi
->status_valid
= 0;
1702 /* Register to trace exit from system calls in the process or LWP.
1703 Returns non-zero for success, zero for failure. */
1706 proc_set_traced_sysexit (procinfo
*pi
, sysset_t
*sysset
)
1710 /* We should never have to apply this operation to any procinfo
1711 except the one for the main process. If that ever changes for
1712 any reason, then take out the following clause and replace it
1713 with one that makes sure the ctl_fd is open. */
1716 pi
= find_procinfo_or_die (pi
->pid
, 0);
1720 struct gdb_proc_ctl_pcsexit
{
1722 /* Use char array to avoid alignment issues. */
1723 char sysset
[sizeof (sysset_t
)];
1725 int argp_size
= sizeof (struct gdb_proc_ctl_pcsexit
)
1727 + sysset_t_size (pi
);
1729 argp
= xmalloc (argp_size
);
1731 argp
->cmd
= PCSEXIT
;
1732 memcpy (&argp
->sysset
, sysset
, sysset_t_size (pi
));
1734 win
= (write (pi
->ctl_fd
, (char *) argp
, argp_size
) == argp_size
);
1737 #else /* ioctl method */
1738 win
= (ioctl (pi
->ctl_fd
, PIOCSEXIT
, sysset
) >= 0);
1740 /* The above operation renders the procinfo's cached pstatus
1742 pi
->status_valid
= 0;
1747 /* Specify the set of blocked / held signals in the process or LWP.
1748 Returns non-zero for success, zero for failure. */
1751 proc_set_held_signals (procinfo
*pi
, gdb_sigset_t
*sighold
)
1755 /* We should never have to apply this operation to any procinfo
1756 except the one for the main process. If that ever changes for
1757 any reason, then take out the following clause and replace it
1758 with one that makes sure the ctl_fd is open. */
1761 pi
= find_procinfo_or_die (pi
->pid
, 0);
1767 /* Use char array to avoid alignment issues. */
1768 char hold
[sizeof (gdb_sigset_t
)];
1772 memcpy (&arg
.hold
, sighold
, sizeof (gdb_sigset_t
));
1773 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
1776 win
= (ioctl (pi
->ctl_fd
, PIOCSHOLD
, sighold
) >= 0);
1778 /* The above operation renders the procinfo's cached pstatus
1780 pi
->status_valid
= 0;
1785 /* Returns the set of signals that are held / blocked. Will also copy
1786 the sigset if SAVE is non-zero. */
1788 static gdb_sigset_t
*
1789 proc_get_held_signals (procinfo
*pi
, gdb_sigset_t
*save
)
1791 gdb_sigset_t
*ret
= NULL
;
1793 /* We should never have to apply this operation to any procinfo
1794 except the one for the main process. If that ever changes for
1795 any reason, then take out the following clause and replace it
1796 with one that makes sure the ctl_fd is open. */
1799 pi
= find_procinfo_or_die (pi
->pid
, 0);
1802 if (!pi
->status_valid
)
1803 if (!proc_get_status (pi
))
1806 ret
= &pi
->prstatus
.pr_lwp
.pr_lwphold
;
1807 #else /* not NEW_PROC_API */
1809 static gdb_sigset_t sigheld
;
1811 if (ioctl (pi
->ctl_fd
, PIOCGHOLD
, &sigheld
) >= 0)
1814 #endif /* NEW_PROC_API */
1816 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
1821 /* Returns the set of signals that are traced / debugged. Will also
1822 copy the sigset if SAVE is non-zero. */
1824 static gdb_sigset_t
*
1825 proc_get_traced_signals (procinfo
*pi
, gdb_sigset_t
*save
)
1827 gdb_sigset_t
*ret
= NULL
;
1829 /* We should never have to apply this operation to any procinfo
1830 except the one for the main process. If that ever changes for
1831 any reason, then take out the following clause and replace it
1832 with one that makes sure the ctl_fd is open. */
1835 pi
= find_procinfo_or_die (pi
->pid
, 0);
1838 if (!pi
->status_valid
)
1839 if (!proc_get_status (pi
))
1842 ret
= &pi
->prstatus
.pr_sigtrace
;
1845 static gdb_sigset_t sigtrace
;
1847 if (ioctl (pi
->ctl_fd
, PIOCGTRACE
, &sigtrace
) >= 0)
1852 memcpy (save
, ret
, sizeof (gdb_sigset_t
));
1857 /* Returns the set of hardware faults that are traced /debugged. Will
1858 also copy the faultset if SAVE is non-zero. */
1861 proc_get_traced_faults (procinfo
*pi
, fltset_t
*save
)
1863 fltset_t
*ret
= NULL
;
1865 /* We should never have to apply this operation to any procinfo
1866 except the one for the main process. If that ever changes for
1867 any reason, then take out the following clause and replace it
1868 with one that makes sure the ctl_fd is open. */
1871 pi
= find_procinfo_or_die (pi
->pid
, 0);
1874 if (!pi
->status_valid
)
1875 if (!proc_get_status (pi
))
1878 ret
= &pi
->prstatus
.pr_flttrace
;
1881 static fltset_t flttrace
;
1883 if (ioctl (pi
->ctl_fd
, PIOCGFAULT
, &flttrace
) >= 0)
1888 memcpy (save
, ret
, sizeof (fltset_t
));
1893 /* Returns the set of syscalls that are traced /debugged on entry.
1894 Will also copy the syscall set if SAVE is non-zero. */
1897 proc_get_traced_sysentry (procinfo
*pi
, sysset_t
*save
)
1899 sysset_t
*ret
= NULL
;
1901 /* We should never have to apply this operation to any procinfo
1902 except the one for the main process. If that ever changes for
1903 any reason, then take out the following clause and replace it
1904 with one that makes sure the ctl_fd is open. */
1907 pi
= find_procinfo_or_die (pi
->pid
, 0);
1910 if (!pi
->status_valid
)
1911 if (!proc_get_status (pi
))
1914 #ifndef DYNAMIC_SYSCALLS
1915 ret
= &pi
->prstatus
.pr_sysentry
;
1916 #else /* DYNAMIC_SYSCALLS */
1918 static sysset_t
*sysentry
;
1922 sysentry
= sysset_t_alloc (pi
);
1924 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
1926 if (pi
->prstatus
.pr_sysentry_offset
== 0)
1928 gdb_premptysysset (sysentry
);
1934 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysentry_offset
,
1936 != (off_t
) pi
->prstatus
.pr_sysentry_offset
)
1938 size
= sysset_t_size (pi
);
1939 gdb_premptysysset (sysentry
);
1940 rsize
= read (pi
->status_fd
, sysentry
, size
);
1945 #endif /* DYNAMIC_SYSCALLS */
1946 #else /* !NEW_PROC_API */
1948 static sysset_t sysentry
;
1950 if (ioctl (pi
->ctl_fd
, PIOCGENTRY
, &sysentry
) >= 0)
1953 #endif /* NEW_PROC_API */
1955 memcpy (save
, ret
, sysset_t_size (pi
));
1960 /* Returns the set of syscalls that are traced /debugged on exit.
1961 Will also copy the syscall set if SAVE is non-zero. */
1964 proc_get_traced_sysexit (procinfo
*pi
, sysset_t
*save
)
1966 sysset_t
* ret
= NULL
;
1968 /* We should never have to apply this operation to any procinfo
1969 except the one for the main process. If that ever changes for
1970 any reason, then take out the following clause and replace it
1971 with one that makes sure the ctl_fd is open. */
1974 pi
= find_procinfo_or_die (pi
->pid
, 0);
1977 if (!pi
->status_valid
)
1978 if (!proc_get_status (pi
))
1981 #ifndef DYNAMIC_SYSCALLS
1982 ret
= &pi
->prstatus
.pr_sysexit
;
1983 #else /* DYNAMIC_SYSCALLS */
1985 static sysset_t
*sysexit
;
1989 sysexit
= sysset_t_alloc (pi
);
1991 if (pi
->status_fd
== 0 && open_procinfo_files (pi
, FD_STATUS
) == 0)
1993 if (pi
->prstatus
.pr_sysexit_offset
== 0)
1995 gdb_premptysysset (sysexit
);
2001 if (lseek (pi
->status_fd
, (off_t
) pi
->prstatus
.pr_sysexit_offset
,
2003 != (off_t
) pi
->prstatus
.pr_sysexit_offset
)
2005 size
= sysset_t_size (pi
);
2006 gdb_premptysysset (sysexit
);
2007 rsize
= read (pi
->status_fd
, sysexit
, size
);
2012 #endif /* DYNAMIC_SYSCALLS */
2015 static sysset_t sysexit
;
2017 if (ioctl (pi
->ctl_fd
, PIOCGEXIT
, &sysexit
) >= 0)
2022 memcpy (save
, ret
, sysset_t_size (pi
));
2027 /* The current fault (if any) is cleared; the associated signal will
2028 not be sent to the process or LWP when it resumes. Returns
2029 non-zero for success, zero for failure. */
2032 proc_clear_current_fault (procinfo
*pi
)
2036 /* We should never have to apply this operation to any procinfo
2037 except the one for the main process. If that ever changes for
2038 any reason, then take out the following clause and replace it
2039 with one that makes sure the ctl_fd is open. */
2042 pi
= find_procinfo_or_die (pi
->pid
, 0);
2046 procfs_ctl_t cmd
= PCCFAULT
;
2048 win
= (write (pi
->ctl_fd
, (void *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2051 win
= (ioctl (pi
->ctl_fd
, PIOCCFAULT
, 0) >= 0);
2057 /* Set the "current signal" that will be delivered next to the
2058 process. NOTE: semantics are different from those of KILL. This
2059 signal will be delivered to the process or LWP immediately when it
2060 is resumed (even if the signal is held/blocked); it will NOT
2061 immediately cause another event of interest, and will NOT first
2062 trap back to the debugger. Returns non-zero for success, zero for
2066 proc_set_current_signal (procinfo
*pi
, int signo
)
2071 /* Use char array to avoid alignment issues. */
2072 char sinfo
[sizeof (gdb_siginfo_t
)];
2074 gdb_siginfo_t mysinfo
;
2076 struct target_waitstatus wait_status
;
2078 /* We should never have to apply this operation to any procinfo
2079 except the one for the main process. If that ever changes for
2080 any reason, then take out the following clause and replace it
2081 with one that makes sure the ctl_fd is open. */
2084 pi
= find_procinfo_or_die (pi
->pid
, 0);
2086 #ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2087 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2088 receives a PIOCSSIG with a signal identical to the current
2089 signal, it messes up the current signal. Work around the kernel
2092 signo
== proc_cursig (pi
))
2093 return 1; /* I assume this is a success? */
2096 /* The pointer is just a type alias. */
2097 get_last_target_status (&wait_ptid
, &wait_status
);
2098 if (ptid_equal (wait_ptid
, inferior_ptid
)
2099 && wait_status
.kind
== TARGET_WAITKIND_STOPPED
2100 && wait_status
.value
.sig
== gdb_signal_from_host (signo
)
2101 && proc_get_status (pi
)
2103 && pi
->prstatus
.pr_lwp
.pr_info
.si_signo
== signo
2105 && pi
->prstatus
.pr_info
.si_signo
== signo
2108 /* Use the siginfo associated with the signal being
2111 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_lwp
.pr_info
, sizeof (gdb_siginfo_t
));
2113 memcpy (arg
.sinfo
, &pi
->prstatus
.pr_info
, sizeof (gdb_siginfo_t
));
2117 mysinfo
.si_signo
= signo
;
2118 mysinfo
.si_code
= 0;
2119 mysinfo
.si_pid
= getpid (); /* ?why? */
2120 mysinfo
.si_uid
= getuid (); /* ?why? */
2121 memcpy (arg
.sinfo
, &mysinfo
, sizeof (gdb_siginfo_t
));
2126 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2128 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, (void *) &arg
.sinfo
) >= 0);
2134 /* The current signal (if any) is cleared, and is not sent to the
2135 process or LWP when it resumes. Returns non-zero for success, zero
2139 proc_clear_current_signal (procinfo
*pi
)
2143 /* We should never have to apply this operation to any procinfo
2144 except the one for the main process. If that ever changes for
2145 any reason, then take out the following clause and replace it
2146 with one that makes sure the ctl_fd is open. */
2149 pi
= find_procinfo_or_die (pi
->pid
, 0);
2155 /* Use char array to avoid alignment issues. */
2156 char sinfo
[sizeof (gdb_siginfo_t
)];
2158 gdb_siginfo_t mysinfo
;
2161 /* The pointer is just a type alias. */
2162 mysinfo
.si_signo
= 0;
2163 mysinfo
.si_code
= 0;
2164 mysinfo
.si_errno
= 0;
2165 mysinfo
.si_pid
= getpid (); /* ?why? */
2166 mysinfo
.si_uid
= getuid (); /* ?why? */
2167 memcpy (arg
.sinfo
, &mysinfo
, sizeof (gdb_siginfo_t
));
2169 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2172 win
= (ioctl (pi
->ctl_fd
, PIOCSSIG
, 0) >= 0);
2178 /* Return the general-purpose registers for the process or LWP
2179 corresponding to PI. Upon failure, return NULL. */
2181 static gdb_gregset_t
*
2182 proc_get_gregs (procinfo
*pi
)
2184 if (!pi
->status_valid
|| !pi
->gregs_valid
)
2185 if (!proc_get_status (pi
))
2189 return &pi
->prstatus
.pr_lwp
.pr_reg
;
2191 return &pi
->prstatus
.pr_reg
;
2195 /* Return the general-purpose registers for the process or LWP
2196 corresponding to PI. Upon failure, return NULL. */
2198 static gdb_fpregset_t
*
2199 proc_get_fpregs (procinfo
*pi
)
2202 if (!pi
->status_valid
|| !pi
->fpregs_valid
)
2203 if (!proc_get_status (pi
))
2206 return &pi
->prstatus
.pr_lwp
.pr_fpreg
;
2208 #else /* not NEW_PROC_API */
2209 if (pi
->fpregs_valid
)
2210 return &pi
->fpregset
; /* Already got 'em. */
2213 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2222 tid_t pr_error_thread
;
2223 tfpregset_t thread_1
;
2226 thread_fpregs
.pr_count
= 1;
2227 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2230 && ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2232 pi
->fpregs_valid
= 1;
2233 return &pi
->fpregset
; /* Got 'em now! */
2235 else if (pi
->tid
!= 0
2236 && ioctl (pi
->ctl_fd
, PIOCTGFPREG
, &thread_fpregs
) >= 0)
2238 memcpy (&pi
->fpregset
, &thread_fpregs
.thread_1
.pr_fpregs
,
2239 sizeof (pi
->fpregset
));
2240 pi
->fpregs_valid
= 1;
2241 return &pi
->fpregset
; /* Got 'em now! */
2248 if (ioctl (pi
->ctl_fd
, PIOCGFPREG
, &pi
->fpregset
) >= 0)
2250 pi
->fpregs_valid
= 1;
2251 return &pi
->fpregset
; /* Got 'em now! */
2260 #endif /* NEW_PROC_API */
2263 /* Write the general-purpose registers back to the process or LWP
2264 corresponding to PI. Return non-zero for success, zero for
2268 proc_set_gregs (procinfo
*pi
)
2270 gdb_gregset_t
*gregs
;
2273 gregs
= proc_get_gregs (pi
);
2275 return 0; /* proc_get_regs has already warned. */
2277 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2286 /* Use char array to avoid alignment issues. */
2287 char gregs
[sizeof (gdb_gregset_t
)];
2291 memcpy (&arg
.gregs
, gregs
, sizeof (arg
.gregs
));
2292 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2294 win
= (ioctl (pi
->ctl_fd
, PIOCSREG
, gregs
) >= 0);
2298 /* Policy: writing the registers invalidates our cache. */
2299 pi
->gregs_valid
= 0;
2303 /* Write the floating-pointer registers back to the process or LWP
2304 corresponding to PI. Return non-zero for success, zero for
2308 proc_set_fpregs (procinfo
*pi
)
2310 gdb_fpregset_t
*fpregs
;
2313 fpregs
= proc_get_fpregs (pi
);
2315 return 0; /* proc_get_fpregs has already warned. */
2317 if (pi
->ctl_fd
== 0 && open_procinfo_files (pi
, FD_CTL
) == 0)
2326 /* Use char array to avoid alignment issues. */
2327 char fpregs
[sizeof (gdb_fpregset_t
)];
2331 memcpy (&arg
.fpregs
, fpregs
, sizeof (arg
.fpregs
));
2332 win
= (write (pi
->ctl_fd
, (void *) &arg
, sizeof (arg
)) == sizeof (arg
));
2336 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2341 tid_t pr_error_thread
;
2342 tfpregset_t thread_1
;
2345 thread_fpregs
.pr_count
= 1;
2346 thread_fpregs
.thread_1
.tid
= pi
->tid
;
2347 memcpy (&thread_fpregs
.thread_1
.pr_fpregs
, fpregs
,
2349 win
= (ioctl (pi
->ctl_fd
, PIOCTSFPREG
, &thread_fpregs
) >= 0);
2352 win
= (ioctl (pi
->ctl_fd
, PIOCSFPREG
, fpregs
) >= 0);
2354 #endif /* NEW_PROC_API */
2357 /* Policy: writing the registers invalidates our cache. */
2358 pi
->fpregs_valid
= 0;
2362 /* Send a signal to the proc or lwp with the semantics of "kill()".
2363 Returns non-zero for success, zero for failure. */
2366 proc_kill (procinfo
*pi
, int signo
)
2370 /* We might conceivably apply this operation to an LWP, and the
2371 LWP's ctl file descriptor might not be open. */
2373 if (pi
->ctl_fd
== 0 &&
2374 open_procinfo_files (pi
, FD_CTL
) == 0)
2381 procfs_ctl_t cmd
[2];
2385 win
= (write (pi
->ctl_fd
, (char *) &cmd
, sizeof (cmd
)) == sizeof (cmd
));
2386 #else /* ioctl method */
2387 /* FIXME: do I need the Alpha OSF fixups present in
2388 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2389 win
= (ioctl (pi
->ctl_fd
, PIOCKILL
, &signo
) >= 0);
2396 /* Find the pid of the process that started this one. Returns the
2397 parent process pid, or zero. */
2400 proc_parent_pid (procinfo
*pi
)
2402 /* We should never have to apply this operation to any procinfo
2403 except the one for the main process. If that ever changes for
2404 any reason, then take out the following clause and replace it
2405 with one that makes sure the ctl_fd is open. */
2408 pi
= find_procinfo_or_die (pi
->pid
, 0);
2410 if (!pi
->status_valid
)
2411 if (!proc_get_status (pi
))
2414 return pi
->prstatus
.pr_ppid
;
2417 /* Convert a target address (a.k.a. CORE_ADDR) into a host address
2418 (a.k.a void pointer)! */
2420 #if (defined (PCWATCH) || defined (PIOCSWATCH)) \
2421 && !(defined (PIOCOPENLWP))
2423 procfs_address_to_host_pointer (CORE_ADDR addr
)
2425 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
2428 gdb_assert (sizeof (ptr
) == TYPE_LENGTH (ptr_type
));
2429 gdbarch_address_to_pointer (target_gdbarch (), ptr_type
,
2430 (gdb_byte
*) &ptr
, addr
);
2436 proc_set_watchpoint (procinfo
*pi
, CORE_ADDR addr
, int len
, int wflags
)
2438 #if !defined (PCWATCH) && !defined (PIOCSWATCH)
2439 /* If neither or these is defined, we can't support watchpoints.
2440 This just avoids possibly failing to compile the below on such
2444 /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5. */
2445 #if defined (PIOCOPENLWP) /* Solaris 2.5: bail out. */
2450 char watch
[sizeof (prwatch_t
)];
2454 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
2455 convert a target address into something that can be stored in a
2456 native data structure. */
2457 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
2458 pwatch
.pr_vaddr
= (uintptr_t) procfs_address_to_host_pointer (addr
);
2460 pwatch
.pr_vaddr
= (caddr_t
) procfs_address_to_host_pointer (addr
);
2462 pwatch
.pr_size
= len
;
2463 pwatch
.pr_wflags
= wflags
;
2464 #if defined(NEW_PROC_API) && defined (PCWATCH)
2466 memcpy (arg
.watch
, &pwatch
, sizeof (prwatch_t
));
2467 return (write (pi
->ctl_fd
, &arg
, sizeof (arg
)) == sizeof (arg
));
2469 #if defined (PIOCSWATCH)
2470 return (ioctl (pi
->ctl_fd
, PIOCSWATCH
, &pwatch
) >= 0);
2472 return 0; /* Fail */
2479 #if (defined(__i386__) || defined(__x86_64__)) && defined (sun)
2481 #include <sys/sysi86.h>
2483 /* The KEY is actually the value of the lower 16 bits of the GS
2484 register for the LWP that we're interested in. Returns the
2485 matching ssh struct (LDT entry). */
2488 proc_get_LDT_entry (procinfo
*pi
, int key
)
2490 static struct ssd
*ldt_entry
= NULL
;
2492 char pathname
[MAX_PROC_NAME_SIZE
];
2493 struct cleanup
*old_chain
= NULL
;
2496 /* Allocate space for one LDT entry.
2497 This alloc must persist, because we return a pointer to it. */
2498 if (ldt_entry
== NULL
)
2499 ldt_entry
= (struct ssd
*) xmalloc (sizeof (struct ssd
));
2501 /* Open the file descriptor for the LDT table. */
2502 sprintf (pathname
, "/proc/%d/ldt", pi
->pid
);
2503 if ((fd
= open_with_retry (pathname
, O_RDONLY
)) < 0)
2505 proc_warn (pi
, "proc_get_LDT_entry (open)", __LINE__
);
2508 /* Make sure it gets closed again! */
2509 old_chain
= make_cleanup_close (fd
);
2511 /* Now 'read' thru the table, find a match and return it. */
2512 while (read (fd
, ldt_entry
, sizeof (struct ssd
)) == sizeof (struct ssd
))
2514 if (ldt_entry
->sel
== 0 &&
2515 ldt_entry
->bo
== 0 &&
2516 ldt_entry
->acc1
== 0 &&
2517 ldt_entry
->acc2
== 0)
2518 break; /* end of table */
2519 /* If key matches, return this entry. */
2520 if (ldt_entry
->sel
== key
)
2523 /* Loop ended, match not found. */
2527 static int nalloc
= 0;
2529 /* Get the number of LDT entries. */
2530 if (ioctl (pi
->ctl_fd
, PIOCNLDT
, &nldt
) < 0)
2532 proc_warn (pi
, "proc_get_LDT_entry (PIOCNLDT)", __LINE__
);
2536 /* Allocate space for the number of LDT entries. */
2537 /* This alloc has to persist, 'cause we return a pointer to it. */
2540 ldt_entry
= (struct ssd
*)
2541 xrealloc (ldt_entry
, (nldt
+ 1) * sizeof (struct ssd
));
2545 /* Read the whole table in one gulp. */
2546 if (ioctl (pi
->ctl_fd
, PIOCLDT
, ldt_entry
) < 0)
2548 proc_warn (pi
, "proc_get_LDT_entry (PIOCLDT)", __LINE__
);
2552 /* Search the table and return the (first) entry matching 'key'. */
2553 for (i
= 0; i
< nldt
; i
++)
2554 if (ldt_entry
[i
].sel
== key
)
2555 return &ldt_entry
[i
];
2557 /* Loop ended, match not found. */
2562 /* Returns the pointer to the LDT entry of PTID. */
2565 procfs_find_LDT_entry (ptid_t ptid
)
2567 gdb_gregset_t
*gregs
;
2571 /* Find procinfo for the lwp. */
2572 if ((pi
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
))) == NULL
)
2574 warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."),
2575 PIDGET (ptid
), TIDGET (ptid
));
2578 /* get its general registers. */
2579 if ((gregs
= proc_get_gregs (pi
)) == NULL
)
2581 warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."),
2582 PIDGET (ptid
), TIDGET (ptid
));
2585 /* Now extract the GS register's lower 16 bits. */
2586 key
= (*gregs
)[GS
] & 0xffff;
2588 /* Find the matching entry and return it. */
2589 return proc_get_LDT_entry (pi
, key
);
2594 /* =============== END, non-thread part of /proc "MODULE" =============== */
2596 /* =================== Thread "MODULE" =================== */
2598 /* NOTE: you'll see more ifdefs and duplication of functions here,
2599 since there is a different way to do threads on every OS. */
2601 /* Returns the number of threads for the process. */
2603 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2606 proc_get_nthreads (procinfo
*pi
)
2610 if (ioctl (pi
->ctl_fd
, PIOCNTHR
, &nthreads
) < 0)
2611 proc_warn (pi
, "procfs: PIOCNTHR failed", __LINE__
);
2617 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2618 /* Solaris version */
2620 proc_get_nthreads (procinfo
*pi
)
2622 if (!pi
->status_valid
)
2623 if (!proc_get_status (pi
))
2626 /* NEW_PROC_API: only works for the process procinfo, because the
2627 LWP procinfos do not get prstatus filled in. */
2629 if (pi
->tid
!= 0) /* Find the parent process procinfo. */
2630 pi
= find_procinfo_or_die (pi
->pid
, 0);
2632 return pi
->prstatus
.pr_nlwp
;
2636 /* Default version */
2638 proc_get_nthreads (procinfo
*pi
)
2647 Return the ID of the thread that had an event of interest.
2648 (ie. the one that hit a breakpoint or other traced event). All
2649 other things being equal, this should be the ID of a thread that is
2650 currently executing. */
2652 #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2653 /* Solaris version */
2655 proc_get_current_thread (procinfo
*pi
)
2657 /* Note: this should be applied to the root procinfo for the
2658 process, not to the procinfo for an LWP. If applied to the
2659 procinfo for an LWP, it will simply return that LWP's ID. In
2660 that case, find the parent process procinfo. */
2663 pi
= find_procinfo_or_die (pi
->pid
, 0);
2665 if (!pi
->status_valid
)
2666 if (!proc_get_status (pi
))
2670 return pi
->prstatus
.pr_lwp
.pr_lwpid
;
2672 return pi
->prstatus
.pr_who
;
2677 #if defined (PIOCNTHR) && defined (PIOCTLIST)
2680 proc_get_current_thread (procinfo
*pi
)
2682 #if 0 /* FIXME: not ready for prime time? */
2683 return pi
->prstatus
.pr_tid
;
2690 /* Default version */
2692 proc_get_current_thread (procinfo
*pi
)
2700 /* Discover the IDs of all the threads within the process, and create
2701 a procinfo for each of them (chained to the parent). This
2702 unfortunately requires a different method on every OS. Returns
2703 non-zero for success, zero for failure. */
2706 proc_delete_dead_threads (procinfo
*parent
, procinfo
*thread
, void *ignore
)
2708 if (thread
&& parent
) /* sanity */
2710 thread
->status_valid
= 0;
2711 if (!proc_get_status (thread
))
2712 destroy_one_procinfo (&parent
->thread_list
, thread
);
2714 return 0; /* keep iterating */
2717 #if defined (PIOCLSTATUS)
2718 /* Solaris 2.5 (ioctl) version */
2720 proc_update_threads (procinfo
*pi
)
2722 gdb_prstatus_t
*prstatus
;
2723 struct cleanup
*old_chain
= NULL
;
2727 /* We should never have to apply this operation to any procinfo
2728 except the one for the main process. If that ever changes for
2729 any reason, then take out the following clause and replace it
2730 with one that makes sure the ctl_fd is open. */
2733 pi
= find_procinfo_or_die (pi
->pid
, 0);
2735 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2737 if ((nlwp
= proc_get_nthreads (pi
)) <= 1)
2738 return 1; /* Process is not multi-threaded; nothing to do. */
2740 prstatus
= xmalloc (sizeof (gdb_prstatus_t
) * (nlwp
+ 1));
2742 old_chain
= make_cleanup (xfree
, prstatus
);
2743 if (ioctl (pi
->ctl_fd
, PIOCLSTATUS
, prstatus
) < 0)
2744 proc_error (pi
, "update_threads (PIOCLSTATUS)", __LINE__
);
2746 /* Skip element zero, which represents the process as a whole. */
2747 for (i
= 1; i
< nlwp
+ 1; i
++)
2749 if ((thread
= create_procinfo (pi
->pid
, prstatus
[i
].pr_who
)) == NULL
)
2750 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2752 memcpy (&thread
->prstatus
, &prstatus
[i
], sizeof (*prstatus
));
2753 thread
->status_valid
= 1;
2755 pi
->threads_valid
= 1;
2756 do_cleanups (old_chain
);
2761 /* Solaris 6 (and later) version. */
2763 do_closedir_cleanup (void *dir
)
2769 proc_update_threads (procinfo
*pi
)
2771 char pathname
[MAX_PROC_NAME_SIZE
+ 16];
2772 struct dirent
*direntry
;
2773 struct cleanup
*old_chain
= NULL
;
2778 /* We should never have to apply this operation to any procinfo
2779 except the one for the main process. If that ever changes for
2780 any reason, then take out the following clause and replace it
2781 with one that makes sure the ctl_fd is open. */
2784 pi
= find_procinfo_or_die (pi
->pid
, 0);
2786 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2788 /* Note: this brute-force method was originally devised for Unixware
2789 (support removed since), and will also work on Solaris 2.6 and
2790 2.7. The original comment mentioned the existence of a much
2791 simpler and more elegant way to do this on Solaris, but didn't
2792 point out what that was. */
2794 strcpy (pathname
, pi
->pathname
);
2795 strcat (pathname
, "/lwp");
2796 if ((dirp
= opendir (pathname
)) == NULL
)
2797 proc_error (pi
, "update_threads, opendir", __LINE__
);
2799 old_chain
= make_cleanup (do_closedir_cleanup
, dirp
);
2800 while ((direntry
= readdir (dirp
)) != NULL
)
2801 if (direntry
->d_name
[0] != '.') /* skip '.' and '..' */
2803 lwpid
= atoi (&direntry
->d_name
[0]);
2804 if ((thread
= create_procinfo (pi
->pid
, lwpid
)) == NULL
)
2805 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2807 pi
->threads_valid
= 1;
2808 do_cleanups (old_chain
);
2815 proc_update_threads (procinfo
*pi
)
2820 /* We should never have to apply this operation to any procinfo
2821 except the one for the main process. If that ever changes for
2822 any reason, then take out the following clause and replace it
2823 with one that makes sure the ctl_fd is open. */
2826 pi
= find_procinfo_or_die (pi
->pid
, 0);
2828 proc_iterate_over_threads (pi
, proc_delete_dead_threads
, NULL
);
2830 nthreads
= proc_get_nthreads (pi
);
2832 return 0; /* Nothing to do for 1 or fewer threads. */
2834 threads
= xmalloc (nthreads
* sizeof (tid_t
));
2836 if (ioctl (pi
->ctl_fd
, PIOCTLIST
, threads
) < 0)
2837 proc_error (pi
, "procfs: update_threads (PIOCTLIST)", __LINE__
);
2839 for (i
= 0; i
< nthreads
; i
++)
2841 if (!find_procinfo (pi
->pid
, threads
[i
]))
2842 if (!create_procinfo (pi
->pid
, threads
[i
]))
2843 proc_error (pi
, "update_threads, create_procinfo", __LINE__
);
2845 pi
->threads_valid
= 1;
2849 /* Default version */
2851 proc_update_threads (procinfo
*pi
)
2855 #endif /* OSF PIOCTLIST */
2856 #endif /* NEW_PROC_API */
2857 #endif /* SOL 2.5 PIOCLSTATUS */
2859 /* Given a pointer to a function, call that function once for each lwp
2860 in the procinfo list, until the function returns non-zero, in which
2861 event return the value returned by the function.
2863 Note: this function does NOT call update_threads. If you want to
2864 discover new threads first, you must call that function explicitly.
2865 This function just makes a quick pass over the currently-known
2868 PI is the parent process procinfo. FUNC is the per-thread
2869 function. PTR is an opaque parameter for function. Returns the
2870 first non-zero return value from the callee, or zero. */
2873 proc_iterate_over_threads (procinfo
*pi
,
2874 int (*func
) (procinfo
*, procinfo
*, void *),
2877 procinfo
*thread
, *next
;
2880 /* We should never have to apply this operation to any procinfo
2881 except the one for the main process. If that ever changes for
2882 any reason, then take out the following clause and replace it
2883 with one that makes sure the ctl_fd is open. */
2886 pi
= find_procinfo_or_die (pi
->pid
, 0);
2888 for (thread
= pi
->thread_list
; thread
!= NULL
; thread
= next
)
2890 next
= thread
->next
; /* In case thread is destroyed. */
2891 if ((retval
= (*func
) (pi
, thread
, ptr
)) != 0)
2898 /* =================== END, Thread "MODULE" =================== */
2900 /* =================== END, /proc "MODULE" =================== */
2902 /* =================== GDB "MODULE" =================== */
2904 /* Here are all of the gdb target vector functions and their
2907 static ptid_t
do_attach (ptid_t ptid
);
2908 static void do_detach (int signo
);
2909 static void proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
,
2910 int entry_or_exit
, int mode
, int from_tty
);
2912 /* On mips-irix, we need to insert a breakpoint at __dbx_link during
2913 the startup phase. The following two variables are used to record
2914 the address of the breakpoint, and the code that was replaced by
2916 static int dbx_link_bpt_addr
= 0;
2917 static void *dbx_link_bpt
;
2919 /* Sets up the inferior to be debugged. Registers to trace signals,
2920 hardware faults, and syscalls. Note: does not set RLC flag: caller
2921 may want to customize that. Returns zero for success (note!
2922 unlike most functions in this module); on failure, returns the LINE
2923 NUMBER where it failed! */
2926 procfs_debug_inferior (procinfo
*pi
)
2928 fltset_t traced_faults
;
2929 gdb_sigset_t traced_signals
;
2930 sysset_t
*traced_syscall_entries
;
2931 sysset_t
*traced_syscall_exits
;
2934 #ifdef PROCFS_DONT_TRACE_FAULTS
2935 /* On some systems (OSF), we don't trace hardware faults.
2936 Apparently it's enough that we catch them as signals.
2937 Wonder why we don't just do that in general? */
2938 premptyset (&traced_faults
); /* don't trace faults. */
2940 /* Register to trace hardware faults in the child. */
2941 prfillset (&traced_faults
); /* trace all faults... */
2942 gdb_prdelset (&traced_faults
, FLTPAGE
); /* except page fault. */
2944 if (!proc_set_traced_faults (pi
, &traced_faults
))
2947 /* Initially, register to trace all signals in the child. */
2948 prfillset (&traced_signals
);
2949 if (!proc_set_traced_signals (pi
, &traced_signals
))
2953 /* Register to trace the 'exit' system call (on entry). */
2954 traced_syscall_entries
= sysset_t_alloc (pi
);
2955 gdb_premptysysset (traced_syscall_entries
);
2957 gdb_praddsysset (traced_syscall_entries
, SYS_exit
);
2960 gdb_praddsysset (traced_syscall_entries
, SYS_lwpexit
);/* And _lwp_exit... */
2963 gdb_praddsysset (traced_syscall_entries
, SYS_lwp_exit
);
2965 #ifdef DYNAMIC_SYSCALLS
2967 int callnum
= find_syscall (pi
, "_exit");
2970 gdb_praddsysset (traced_syscall_entries
, callnum
);
2974 status
= proc_set_traced_sysentry (pi
, traced_syscall_entries
);
2975 xfree (traced_syscall_entries
);
2979 #ifdef PRFS_STOPEXEC /* defined on OSF */
2980 /* OSF method for tracing exec syscalls. Quoting:
2981 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
2982 exits from exec system calls because of the user level loader. */
2983 /* FIXME: make nice and maybe move into an access function. */
2987 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
2990 prfs_flags
|= PRFS_STOPEXEC
;
2992 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
2995 #else /* not PRFS_STOPEXEC */
2996 /* Everyone else's (except OSF) method for tracing exec syscalls. */
2998 Not all systems with /proc have all the exec* syscalls with the same
2999 names. On the SGI, for example, there is no SYS_exec, but there
3000 *is* a SYS_execv. So, we try to account for that. */
3002 traced_syscall_exits
= sysset_t_alloc (pi
);
3003 gdb_premptysysset (traced_syscall_exits
);
3005 gdb_praddsysset (traced_syscall_exits
, SYS_exec
);
3008 gdb_praddsysset (traced_syscall_exits
, SYS_execve
);
3011 gdb_praddsysset (traced_syscall_exits
, SYS_execv
);
3014 #ifdef SYS_lwpcreate
3015 gdb_praddsysset (traced_syscall_exits
, SYS_lwpcreate
);
3016 gdb_praddsysset (traced_syscall_exits
, SYS_lwpexit
);
3019 #ifdef SYS_lwp_create /* FIXME: once only, please. */
3020 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_create
);
3021 gdb_praddsysset (traced_syscall_exits
, SYS_lwp_exit
);
3024 #ifdef DYNAMIC_SYSCALLS
3026 int callnum
= find_syscall (pi
, "execve");
3029 gdb_praddsysset (traced_syscall_exits
, callnum
);
3030 callnum
= find_syscall (pi
, "ra_execve");
3032 gdb_praddsysset (traced_syscall_exits
, callnum
);
3036 status
= proc_set_traced_sysexit (pi
, traced_syscall_exits
);
3037 xfree (traced_syscall_exits
);
3041 #endif /* PRFS_STOPEXEC */
3046 procfs_attach (struct target_ops
*ops
, char *args
, int from_tty
)
3051 pid
= parse_pid_to_attach (args
);
3053 if (pid
== getpid ())
3054 error (_("Attaching GDB to itself is not a good idea..."));
3058 exec_file
= get_exec_file (0);
3061 printf_filtered (_("Attaching to program `%s', %s\n"),
3062 exec_file
, target_pid_to_str (pid_to_ptid (pid
)));
3064 printf_filtered (_("Attaching to %s\n"),
3065 target_pid_to_str (pid_to_ptid (pid
)));
3069 inferior_ptid
= do_attach (pid_to_ptid (pid
));
3074 procfs_detach (struct target_ops
*ops
, char *args
, int from_tty
)
3077 int pid
= PIDGET (inferior_ptid
);
3086 exec_file
= get_exec_file (0);
3087 if (exec_file
== NULL
)
3090 printf_filtered (_("Detaching from program: %s, %s\n"), exec_file
,
3091 target_pid_to_str (pid_to_ptid (pid
)));
3092 gdb_flush (gdb_stdout
);
3097 inferior_ptid
= null_ptid
;
3098 detach_inferior (pid
);
3099 unpush_target (ops
);
3103 do_attach (ptid_t ptid
)
3106 struct inferior
*inf
;
3110 if ((pi
= create_procinfo (PIDGET (ptid
), 0)) == NULL
)
3111 perror (_("procfs: out of memory in 'attach'"));
3113 if (!open_procinfo_files (pi
, FD_CTL
))
3115 fprintf_filtered (gdb_stderr
, "procfs:%d -- ", __LINE__
);
3116 sprintf (errmsg
, "do_attach: couldn't open /proc file for process %d",
3118 dead_procinfo (pi
, errmsg
, NOKILL
);
3121 /* Stop the process (if it isn't already stopped). */
3122 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
3124 pi
->was_stopped
= 1;
3125 proc_prettyprint_why (proc_why (pi
), proc_what (pi
), 1);
3129 pi
->was_stopped
= 0;
3130 /* Set the process to run again when we close it. */
3131 if (!proc_set_run_on_last_close (pi
))
3132 dead_procinfo (pi
, "do_attach: couldn't set RLC.", NOKILL
);
3134 /* Now stop the process. */
3135 if (!proc_stop_process (pi
))
3136 dead_procinfo (pi
, "do_attach: couldn't stop the process.", NOKILL
);
3137 pi
->ignore_next_sigstop
= 1;
3139 /* Save some of the /proc state to be restored if we detach. */
3140 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
3141 dead_procinfo (pi
, "do_attach: couldn't save traced faults.", NOKILL
);
3142 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
3143 dead_procinfo (pi
, "do_attach: couldn't save traced signals.", NOKILL
);
3144 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
3145 dead_procinfo (pi
, "do_attach: couldn't save traced syscall entries.",
3147 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
3148 dead_procinfo (pi
, "do_attach: couldn't save traced syscall exits.",
3150 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
3151 dead_procinfo (pi
, "do_attach: couldn't save held signals.", NOKILL
);
3153 if ((fail
= procfs_debug_inferior (pi
)) != 0)
3154 dead_procinfo (pi
, "do_attach: failed in procfs_debug_inferior", NOKILL
);
3156 inf
= current_inferior ();
3157 inferior_appeared (inf
, pi
->pid
);
3158 /* Let GDB know that the inferior was attached. */
3159 inf
->attach_flag
= 1;
3161 /* Create a procinfo for the current lwp. */
3162 lwpid
= proc_get_current_thread (pi
);
3163 create_procinfo (pi
->pid
, lwpid
);
3165 /* Add it to gdb's thread list. */
3166 ptid
= MERGEPID (pi
->pid
, lwpid
);
3173 do_detach (int signo
)
3177 /* Find procinfo for the main process. */
3178 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0); /* FIXME: threads */
3180 if (!proc_set_current_signal (pi
, signo
))
3181 proc_warn (pi
, "do_detach, set_current_signal", __LINE__
);
3183 if (!proc_set_traced_signals (pi
, &pi
->saved_sigset
))
3184 proc_warn (pi
, "do_detach, set_traced_signal", __LINE__
);
3186 if (!proc_set_traced_faults (pi
, &pi
->saved_fltset
))
3187 proc_warn (pi
, "do_detach, set_traced_faults", __LINE__
);
3189 if (!proc_set_traced_sysentry (pi
, pi
->saved_entryset
))
3190 proc_warn (pi
, "do_detach, set_traced_sysentry", __LINE__
);
3192 if (!proc_set_traced_sysexit (pi
, pi
->saved_exitset
))
3193 proc_warn (pi
, "do_detach, set_traced_sysexit", __LINE__
);
3195 if (!proc_set_held_signals (pi
, &pi
->saved_sighold
))
3196 proc_warn (pi
, "do_detach, set_held_signals", __LINE__
);
3198 if (signo
|| (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)))
3199 if (signo
|| !(pi
->was_stopped
) ||
3200 query (_("Was stopped when attached, make it runnable again? ")))
3202 /* Clear any pending signal. */
3203 if (!proc_clear_current_fault (pi
))
3204 proc_warn (pi
, "do_detach, clear_current_fault", __LINE__
);
3206 if (signo
== 0 && !proc_clear_current_signal (pi
))
3207 proc_warn (pi
, "do_detach, clear_current_signal", __LINE__
);
3209 if (!proc_set_run_on_last_close (pi
))
3210 proc_warn (pi
, "do_detach, set_rlc", __LINE__
);
3213 destroy_procinfo (pi
);
3216 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
3219 ??? Is the following note still relevant? We can't get individual
3220 registers with the PT_GETREGS ptrace(2) request either, yet we
3221 don't bother with caching at all in that case.
3223 NOTE: Since the /proc interface cannot give us individual
3224 registers, we pay no attention to REGNUM, and just fetch them all.
3225 This results in the possibility that we will do unnecessarily many
3226 fetches, since we may be called repeatedly for individual
3227 registers. So we cache the results, and mark the cache invalid
3228 when the process is resumed. */
3231 procfs_fetch_registers (struct target_ops
*ops
,
3232 struct regcache
*regcache
, int regnum
)
3234 gdb_gregset_t
*gregs
;
3236 int pid
= PIDGET (inferior_ptid
);
3237 int tid
= TIDGET (inferior_ptid
);
3238 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3240 pi
= find_procinfo_or_die (pid
, tid
);
3243 error (_("procfs: fetch_registers failed to find procinfo for %s"),
3244 target_pid_to_str (inferior_ptid
));
3246 gregs
= proc_get_gregs (pi
);
3248 proc_error (pi
, "fetch_registers, get_gregs", __LINE__
);
3250 supply_gregset (regcache
, (const gdb_gregset_t
*) gregs
);
3252 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
3254 gdb_fpregset_t
*fpregs
;
3256 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
3257 || regnum
== gdbarch_pc_regnum (gdbarch
)
3258 || regnum
== gdbarch_sp_regnum (gdbarch
))
3259 return; /* Not a floating point register. */
3261 fpregs
= proc_get_fpregs (pi
);
3263 proc_error (pi
, "fetch_registers, get_fpregs", __LINE__
);
3265 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregs
);
3269 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
3270 this for all registers.
3272 NOTE: Since the /proc interface will not read individual registers,
3273 we will cache these requests until the process is resumed, and only
3274 then write them back to the inferior process.
3276 FIXME: is that a really bad idea? Have to think about cases where
3277 writing one register might affect the value of others, etc. */
3280 procfs_store_registers (struct target_ops
*ops
,
3281 struct regcache
*regcache
, int regnum
)
3283 gdb_gregset_t
*gregs
;
3285 int pid
= PIDGET (inferior_ptid
);
3286 int tid
= TIDGET (inferior_ptid
);
3287 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
3289 pi
= find_procinfo_or_die (pid
, tid
);
3292 error (_("procfs: store_registers: failed to find procinfo for %s"),
3293 target_pid_to_str (inferior_ptid
));
3295 gregs
= proc_get_gregs (pi
);
3297 proc_error (pi
, "store_registers, get_gregs", __LINE__
);
3299 fill_gregset (regcache
, gregs
, regnum
);
3300 if (!proc_set_gregs (pi
))
3301 proc_error (pi
, "store_registers, set_gregs", __LINE__
);
3303 if (gdbarch_fp0_regnum (gdbarch
) >= 0) /* Do we have an FPU? */
3305 gdb_fpregset_t
*fpregs
;
3307 if ((regnum
>= 0 && regnum
< gdbarch_fp0_regnum (gdbarch
))
3308 || regnum
== gdbarch_pc_regnum (gdbarch
)
3309 || regnum
== gdbarch_sp_regnum (gdbarch
))
3310 return; /* Not a floating point register. */
3312 fpregs
= proc_get_fpregs (pi
);
3314 proc_error (pi
, "store_registers, get_fpregs", __LINE__
);
3316 fill_fpregset (regcache
, fpregs
, regnum
);
3317 if (!proc_set_fpregs (pi
))
3318 proc_error (pi
, "store_registers, set_fpregs", __LINE__
);
3323 syscall_is_lwp_exit (procinfo
*pi
, int scall
)
3326 if (scall
== SYS_lwp_exit
)
3330 if (scall
== SYS_lwpexit
)
3337 syscall_is_exit (procinfo
*pi
, int scall
)
3340 if (scall
== SYS_exit
)
3343 #ifdef DYNAMIC_SYSCALLS
3344 if (find_syscall (pi
, "_exit") == scall
)
3351 syscall_is_exec (procinfo
*pi
, int scall
)
3354 if (scall
== SYS_exec
)
3358 if (scall
== SYS_execv
)
3362 if (scall
== SYS_execve
)
3365 #ifdef DYNAMIC_SYSCALLS
3366 if (find_syscall (pi
, "_execve"))
3368 if (find_syscall (pi
, "ra_execve"))
3375 syscall_is_lwp_create (procinfo
*pi
, int scall
)
3377 #ifdef SYS_lwp_create
3378 if (scall
== SYS_lwp_create
)
3381 #ifdef SYS_lwpcreate
3382 if (scall
== SYS_lwpcreate
)
3388 /* Remove the breakpoint that we inserted in __dbx_link().
3389 Does nothing if the breakpoint hasn't been inserted or has already
3393 remove_dbx_link_breakpoint (void)
3395 if (dbx_link_bpt_addr
== 0)
3398 if (deprecated_remove_raw_breakpoint (target_gdbarch (), dbx_link_bpt
) != 0)
3399 warning (_("Unable to remove __dbx_link breakpoint."));
3401 dbx_link_bpt_addr
= 0;
3402 dbx_link_bpt
= NULL
;
3406 /* Return the address of the __dbx_link() function in the file
3407 refernced by ABFD by scanning its symbol table. Return 0 if
3408 the symbol was not found. */
3411 dbx_link_addr (bfd
*abfd
)
3413 long storage_needed
;
3414 asymbol
**symbol_table
;
3415 long number_of_symbols
;
3418 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
3419 if (storage_needed
<= 0)
3422 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
3423 make_cleanup (xfree
, symbol_table
);
3425 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
3427 for (i
= 0; i
< number_of_symbols
; i
++)
3429 asymbol
*sym
= symbol_table
[i
];
3431 if ((sym
->flags
& BSF_GLOBAL
)
3432 && sym
->name
!= NULL
&& strcmp (sym
->name
, "__dbx_link") == 0)
3433 return (sym
->value
+ sym
->section
->vma
);
3436 /* Symbol not found, return NULL. */
3440 /* Search the symbol table of the file referenced by FD for a symbol
3441 named __dbx_link(). If found, then insert a breakpoint at this location,
3442 and return nonzero. Return zero otherwise. */
3445 insert_dbx_link_bpt_in_file (int fd
, CORE_ADDR ignored
)
3448 long storage_needed
;
3451 abfd
= gdb_bfd_fdopenr ("unamed", 0, fd
);
3454 warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
3458 if (!bfd_check_format (abfd
, bfd_object
))
3460 /* Not the correct format, so we can not possibly find the dbx_link
3462 gdb_bfd_unref (abfd
);
3466 sym_addr
= dbx_link_addr (abfd
);
3469 /* Insert the breakpoint. */
3470 dbx_link_bpt_addr
= sym_addr
;
3471 dbx_link_bpt
= deprecated_insert_raw_breakpoint (target_gdbarch (), NULL
,
3473 if (dbx_link_bpt
== NULL
)
3475 warning (_("Failed to insert dbx_link breakpoint."));
3476 gdb_bfd_unref (abfd
);
3479 gdb_bfd_unref (abfd
);
3483 gdb_bfd_unref (abfd
);
3487 /* Calls the supplied callback function once for each mapped address
3488 space in the process. The callback function receives an open file
3489 descriptor for the file corresponding to that mapped address space
3490 (if there is one), and the base address of the mapped space. Quit
3491 when the callback function returns a nonzero value, or at teh end
3492 of the mappings. Returns the first non-zero return value of the
3493 callback function, or zero. */
3496 solib_mappings_callback (struct prmap
*map
, int (*func
) (int, CORE_ADDR
),
3499 procinfo
*pi
= data
;
3503 char name
[MAX_PROC_NAME_SIZE
+ sizeof (map
->pr_mapname
)];
3505 if (map
->pr_vaddr
== 0 && map
->pr_size
== 0)
3506 return -1; /* sanity */
3508 if (map
->pr_mapname
[0] == 0)
3510 fd
= -1; /* no map file */
3514 sprintf (name
, "/proc/%d/object/%s", pi
->pid
, map
->pr_mapname
);
3515 /* Note: caller's responsibility to close this fd! */
3516 fd
= open_with_retry (name
, O_RDONLY
);
3517 /* Note: we don't test the above call for failure;
3518 we just pass the FD on as given. Sometimes there is
3519 no file, so the open may return failure, but that's
3523 fd
= ioctl (pi
->ctl_fd
, PIOCOPENM
, &map
->pr_vaddr
);
3524 /* Note: we don't test the above call for failure;
3525 we just pass the FD on as given. Sometimes there is
3526 no file, so the ioctl may return failure, but that's
3529 return (*func
) (fd
, (CORE_ADDR
) map
->pr_vaddr
);
3532 /* If the given memory region MAP contains a symbol named __dbx_link,
3533 insert a breakpoint at this location and return nonzero. Return
3537 insert_dbx_link_bpt_in_region (struct prmap
*map
,
3538 find_memory_region_ftype child_func
,
3541 procinfo
*pi
= (procinfo
*) data
;
3543 /* We know the symbol we're looking for is in a text region, so
3544 only look for it if the region is a text one. */
3545 if (map
->pr_mflags
& MA_EXEC
)
3546 return solib_mappings_callback (map
, insert_dbx_link_bpt_in_file
, pi
);
3551 /* Search all memory regions for a symbol named __dbx_link. If found,
3552 insert a breakpoint at its location, and return nonzero. Return zero
3556 insert_dbx_link_breakpoint (procinfo
*pi
)
3558 return iterate_over_mappings (pi
, NULL
, pi
, insert_dbx_link_bpt_in_region
);
3562 /* Retrieve the next stop event from the child process. If child has
3563 not stopped yet, wait for it to stop. Translate /proc eventcodes
3564 (or possibly wait eventcodes) into gdb internal event codes.
3565 Returns the id of process (and possibly thread) that incurred the
3566 event. Event codes are returned through a pointer parameter. */
3569 procfs_wait (struct target_ops
*ops
,
3570 ptid_t ptid
, struct target_waitstatus
*status
, int options
)
3572 /* First cut: loosely based on original version 2.1. */
3576 ptid_t retval
, temp_ptid
;
3577 int why
, what
, flags
;
3584 retval
= pid_to_ptid (-1);
3586 /* Find procinfo for main process. */
3587 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
3590 /* We must assume that the status is stale now... */
3591 pi
->status_valid
= 0;
3592 pi
->gregs_valid
= 0;
3593 pi
->fpregs_valid
= 0;
3595 #if 0 /* just try this out... */
3596 flags
= proc_flags (pi
);
3597 why
= proc_why (pi
);
3598 if ((flags
& PR_STOPPED
) && (why
== PR_REQUESTED
))
3599 pi
->status_valid
= 0; /* re-read again, IMMEDIATELY... */
3601 /* If child is not stopped, wait for it to stop. */
3602 if (!(proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
)) &&
3603 !proc_wait_for_stop (pi
))
3605 /* wait_for_stop failed: has the child terminated? */
3606 if (errno
== ENOENT
)
3610 /* /proc file not found; presumably child has terminated. */
3611 wait_retval
= wait (&wstat
); /* "wait" for the child's exit. */
3613 if (wait_retval
!= PIDGET (inferior_ptid
)) /* wrong child? */
3614 error (_("procfs: couldn't stop "
3615 "process %d: wait returned %d."),
3616 PIDGET (inferior_ptid
), wait_retval
);
3617 /* FIXME: might I not just use waitpid?
3618 Or try find_procinfo to see if I know about this child? */
3619 retval
= pid_to_ptid (wait_retval
);
3621 else if (errno
== EINTR
)
3625 /* Unknown error from wait_for_stop. */
3626 proc_error (pi
, "target_wait (wait_for_stop)", __LINE__
);
3631 /* This long block is reached if either:
3632 a) the child was already stopped, or
3633 b) we successfully waited for the child with wait_for_stop.
3634 This block will analyze the /proc status, and translate it
3635 into a waitstatus for GDB.
3637 If we actually had to call wait because the /proc file
3638 is gone (child terminated), then we skip this block,
3639 because we already have a waitstatus. */
3641 flags
= proc_flags (pi
);
3642 why
= proc_why (pi
);
3643 what
= proc_what (pi
);
3645 if (flags
& (PR_STOPPED
| PR_ISTOP
))
3648 /* If it's running async (for single_thread control),
3649 set it back to normal again. */
3650 if (flags
& PR_ASYNC
)
3651 if (!proc_unset_async (pi
))
3652 proc_error (pi
, "target_wait, unset_async", __LINE__
);
3656 proc_prettyprint_why (why
, what
, 1);
3658 /* The 'pid' we will return to GDB is composed of
3659 the process ID plus the lwp ID. */
3660 retval
= MERGEPID (pi
->pid
, proc_get_current_thread (pi
));
3664 wstat
= (what
<< 8) | 0177;
3667 if (syscall_is_lwp_exit (pi
, what
))
3669 if (print_thread_events
)
3670 printf_unfiltered (_("[%s exited]\n"),
3671 target_pid_to_str (retval
));
3672 delete_thread (retval
);
3673 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3676 else if (syscall_is_exit (pi
, what
))
3678 struct inferior
*inf
;
3680 /* Handle SYS_exit call only. */
3681 /* Stopped at entry to SYS_exit.
3682 Make it runnable, resume it, then use
3683 the wait system call to get its exit code.
3684 Proc_run_process always clears the current
3686 Then return its exit status. */
3687 pi
->status_valid
= 0;
3689 /* FIXME: what we should do is return
3690 TARGET_WAITKIND_SPURIOUS. */
3691 if (!proc_run_process (pi
, 0, 0))
3692 proc_error (pi
, "target_wait, run_process", __LINE__
);
3694 inf
= find_inferior_pid (pi
->pid
);
3695 if (inf
->attach_flag
)
3697 /* Don't call wait: simulate waiting for exit,
3698 return a "success" exit code. Bogus: what if
3699 it returns something else? */
3701 retval
= inferior_ptid
; /* ? ? ? */
3705 int temp
= wait (&wstat
);
3707 /* FIXME: shouldn't I make sure I get the right
3708 event from the right process? If (for
3709 instance) I have killed an earlier inferior
3710 process but failed to clean up after it
3711 somehow, I could get its termination event
3714 /* If wait returns -1, that's what we return
3717 retval
= pid_to_ptid (temp
);
3722 printf_filtered (_("procfs: trapped on entry to "));
3723 proc_prettyprint_syscall (proc_what (pi
), 0);
3724 printf_filtered ("\n");
3727 long i
, nsysargs
, *sysargs
;
3729 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3730 (sysargs
= proc_sysargs (pi
)) != NULL
)
3732 printf_filtered (_("%ld syscall arguments:\n"),
3734 for (i
= 0; i
< nsysargs
; i
++)
3735 printf_filtered ("#%ld: 0x%08lx\n",
3743 /* How to exit gracefully, returning "unknown
3745 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3746 return inferior_ptid
;
3750 /* How to keep going without returning to wfi: */
3751 target_resume (ptid
, 0, GDB_SIGNAL_0
);
3757 if (syscall_is_exec (pi
, what
))
3759 /* Hopefully this is our own "fork-child" execing
3760 the real child. Hoax this event into a trap, and
3761 GDB will see the child about to execute its start
3763 wstat
= (SIGTRAP
<< 8) | 0177;
3766 else if (what
== SYS_syssgi
)
3768 /* see if we can break on dbx_link(). If yes, then
3769 we no longer need the SYS_syssgi notifications. */
3770 if (insert_dbx_link_breakpoint (pi
))
3771 proc_trace_syscalls_1 (pi
, SYS_syssgi
, PR_SYSEXIT
,
3774 /* This is an internal event and should be transparent
3775 to wfi, so resume the execution and wait again. See
3776 comment in procfs_init_inferior() for more details. */
3777 target_resume (ptid
, 0, GDB_SIGNAL_0
);
3781 else if (syscall_is_lwp_create (pi
, what
))
3783 /* This syscall is somewhat like fork/exec. We
3784 will get the event twice: once for the parent
3785 LWP, and once for the child. We should already
3786 know about the parent LWP, but the child will
3787 be new to us. So, whenever we get this event,
3788 if it represents a new thread, simply add the
3789 thread to the list. */
3791 /* If not in procinfo list, add it. */
3792 temp_tid
= proc_get_current_thread (pi
);
3793 if (!find_procinfo (pi
->pid
, temp_tid
))
3794 create_procinfo (pi
->pid
, temp_tid
);
3796 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
3797 /* If not in GDB's thread list, add it. */
3798 if (!in_thread_list (temp_ptid
))
3799 add_thread (temp_ptid
);
3801 /* Return to WFI, but tell it to immediately resume. */
3802 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3803 return inferior_ptid
;
3805 else if (syscall_is_lwp_exit (pi
, what
))
3807 if (print_thread_events
)
3808 printf_unfiltered (_("[%s exited]\n"),
3809 target_pid_to_str (retval
));
3810 delete_thread (retval
);
3811 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3816 /* FIXME: Do we need to handle SYS_sproc,
3817 SYS_fork, or SYS_vfork here? The old procfs
3818 seemed to use this event to handle threads on
3819 older (non-LWP) systems, where I'm assuming
3820 that threads were actually separate processes.
3821 Irix, maybe? Anyway, low priority for now. */
3825 printf_filtered (_("procfs: trapped on exit from "));
3826 proc_prettyprint_syscall (proc_what (pi
), 0);
3827 printf_filtered ("\n");
3830 long i
, nsysargs
, *sysargs
;
3832 if ((nsysargs
= proc_nsysarg (pi
)) > 0 &&
3833 (sysargs
= proc_sysargs (pi
)) != NULL
)
3835 printf_filtered (_("%ld syscall arguments:\n"),
3837 for (i
= 0; i
< nsysargs
; i
++)
3838 printf_filtered ("#%ld: 0x%08lx\n",
3843 status
->kind
= TARGET_WAITKIND_SPURIOUS
;
3844 return inferior_ptid
;
3849 wstat
= (SIGSTOP
<< 8) | 0177;
3854 printf_filtered (_("Retry #%d:\n"), retry
);
3855 pi
->status_valid
= 0;
3860 /* If not in procinfo list, add it. */
3861 temp_tid
= proc_get_current_thread (pi
);
3862 if (!find_procinfo (pi
->pid
, temp_tid
))
3863 create_procinfo (pi
->pid
, temp_tid
);
3865 /* If not in GDB's thread list, add it. */
3866 temp_ptid
= MERGEPID (pi
->pid
, temp_tid
);
3867 if (!in_thread_list (temp_ptid
))
3868 add_thread (temp_ptid
);
3870 status
->kind
= TARGET_WAITKIND_STOPPED
;
3871 status
->value
.sig
= 0;
3876 wstat
= (what
<< 8) | 0177;
3882 wstat
= (SIGTRAP
<< 8) | 0177;
3887 wstat
= (SIGTRAP
<< 8) | 0177;
3890 /* FIXME: use si_signo where possible. */
3892 #if (FLTILL != FLTPRIV) /* Avoid "duplicate case" error. */
3895 wstat
= (SIGILL
<< 8) | 0177;
3898 #if (FLTTRACE != FLTBPT) /* Avoid "duplicate case" error. */
3901 /* If we hit our __dbx_link() internal breakpoint,
3902 then remove it. See comments in procfs_init_inferior()
3903 for more details. */
3904 if (dbx_link_bpt_addr
!= 0
3905 && dbx_link_bpt_addr
3906 == regcache_read_pc (get_current_regcache ()))
3907 remove_dbx_link_breakpoint ();
3909 wstat
= (SIGTRAP
<< 8) | 0177;
3913 #if (FLTBOUNDS != FLTSTACK) /* Avoid "duplicate case" error. */
3916 wstat
= (SIGSEGV
<< 8) | 0177;
3920 #if (FLTFPE != FLTIOVF) /* Avoid "duplicate case" error. */
3923 wstat
= (SIGFPE
<< 8) | 0177;
3925 case FLTPAGE
: /* Recoverable page fault */
3926 default: /* FIXME: use si_signo if possible for
3928 retval
= pid_to_ptid (-1);
3929 printf_filtered ("procfs:%d -- ", __LINE__
);
3930 printf_filtered (_("child stopped for unknown reason:\n"));
3931 proc_prettyprint_why (why
, what
, 1);
3932 error (_("... giving up..."));
3935 break; /* case PR_FAULTED: */
3936 default: /* switch (why) unmatched */
3937 printf_filtered ("procfs:%d -- ", __LINE__
);
3938 printf_filtered (_("child stopped for unknown reason:\n"));
3939 proc_prettyprint_why (why
, what
, 1);
3940 error (_("... giving up..."));
3943 /* Got this far without error: If retval isn't in the
3944 threads database, add it. */
3945 if (PIDGET (retval
) > 0 &&
3946 !ptid_equal (retval
, inferior_ptid
) &&
3947 !in_thread_list (retval
))
3949 /* We have a new thread. We need to add it both to
3950 GDB's list and to our own. If we don't create a
3951 procinfo, resume may be unhappy later. */
3952 add_thread (retval
);
3953 if (find_procinfo (PIDGET (retval
), TIDGET (retval
)) == NULL
)
3954 create_procinfo (PIDGET (retval
), TIDGET (retval
));
3957 else /* Flags do not indicate STOPPED. */
3959 /* surely this can't happen... */
3960 printf_filtered ("procfs:%d -- process not stopped.\n",
3962 proc_prettyprint_flags (flags
, 1);
3963 error (_("procfs: ...giving up..."));
3968 store_waitstatus (status
, wstat
);
3974 /* Perform a partial transfer to/from the specified object. For
3975 memory transfers, fall back to the old memory xfer functions. */
3978 procfs_xfer_partial (struct target_ops
*ops
, enum target_object object
,
3979 const char *annex
, gdb_byte
*readbuf
,
3980 const gdb_byte
*writebuf
, ULONGEST offset
, LONGEST len
)
3984 case TARGET_OBJECT_MEMORY
:
3986 return (*ops
->deprecated_xfer_memory
) (offset
, readbuf
,
3987 len
, 0/*read*/, NULL
, ops
);
3989 return (*ops
->deprecated_xfer_memory
) (offset
, (gdb_byte
*) writebuf
,
3990 len
, 1/*write*/, NULL
, ops
);
3994 case TARGET_OBJECT_AUXV
:
3995 return memory_xfer_auxv (ops
, object
, annex
, readbuf
, writebuf
,
4000 if (ops
->beneath
!= NULL
)
4001 return ops
->beneath
->to_xfer_partial (ops
->beneath
, object
, annex
,
4002 readbuf
, writebuf
, offset
, len
);
4008 /* Transfer LEN bytes between GDB address MYADDR and target address
4009 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4010 otherwise transfer them from the target. TARGET is unused.
4012 The return value is 0 if an error occurred or no bytes were
4013 transferred. Otherwise, it will be a positive value which
4014 indicates the number of bytes transferred between gdb and the
4015 target. (Note that the interface also makes provisions for
4016 negative values, but this capability isn't implemented here.) */
4019 procfs_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
, int dowrite
,
4020 struct mem_attrib
*attrib
, struct target_ops
*target
)
4025 /* Find procinfo for main process. */
4026 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4027 if (pi
->as_fd
== 0 &&
4028 open_procinfo_files (pi
, FD_AS
) == 0)
4030 proc_warn (pi
, "xfer_memory, open_proc_files", __LINE__
);
4034 if (lseek (pi
->as_fd
, (off_t
) memaddr
, SEEK_SET
) == (off_t
) memaddr
)
4039 PROCFS_NOTE ("write memory:\n");
4041 PROCFS_NOTE ("write memory:\n");
4043 nbytes
= write (pi
->as_fd
, myaddr
, len
);
4047 PROCFS_NOTE ("read memory:\n");
4048 nbytes
= read (pi
->as_fd
, myaddr
, len
);
4058 /* Called by target_resume before making child runnable. Mark cached
4059 registers and status's invalid. If there are "dirty" caches that
4060 need to be written back to the child process, do that.
4062 File descriptors are also cached. As they are a limited resource,
4063 we cannot hold onto them indefinitely. However, as they are
4064 expensive to open, we don't want to throw them away
4065 indescriminately either. As a compromise, we will keep the file
4066 descriptors for the parent process, but discard any file
4067 descriptors we may have accumulated for the threads.
4069 As this function is called by iterate_over_threads, it always
4070 returns zero (so that iterate_over_threads will keep
4074 invalidate_cache (procinfo
*parent
, procinfo
*pi
, void *ptr
)
4076 /* About to run the child; invalidate caches and do any other
4080 if (pi
->gregs_dirty
)
4081 if (parent
== NULL
||
4082 proc_get_current_thread (parent
) != pi
->tid
)
4083 if (!proc_set_gregs (pi
)) /* flush gregs cache */
4084 proc_warn (pi
, "target_resume, set_gregs",
4086 if (gdbarch_fp0_regnum (target_gdbarch ()) >= 0)
4087 if (pi
->fpregs_dirty
)
4088 if (parent
== NULL
||
4089 proc_get_current_thread (parent
) != pi
->tid
)
4090 if (!proc_set_fpregs (pi
)) /* flush fpregs cache */
4091 proc_warn (pi
, "target_resume, set_fpregs",
4097 /* The presence of a parent indicates that this is an LWP.
4098 Close any file descriptors that it might have open.
4099 We don't do this to the master (parent) procinfo. */
4101 close_procinfo_files (pi
);
4103 pi
->gregs_valid
= 0;
4104 pi
->fpregs_valid
= 0;
4106 pi
->gregs_dirty
= 0;
4107 pi
->fpregs_dirty
= 0;
4109 pi
->status_valid
= 0;
4110 pi
->threads_valid
= 0;
4116 /* A callback function for iterate_over_threads. Find the
4117 asynchronous signal thread, and make it runnable. See if that
4118 helps matters any. */
4121 make_signal_thread_runnable (procinfo
*process
, procinfo
*pi
, void *ptr
)
4124 if (proc_flags (pi
) & PR_ASLWP
)
4126 if (!proc_run_process (pi
, 0, -1))
4127 proc_error (pi
, "make_signal_thread_runnable", __LINE__
);
4135 /* Make the child process runnable. Normally we will then call
4136 procfs_wait and wait for it to stop again (unless gdb is async).
4138 If STEP is true, then arrange for the child to stop again after
4139 executing a single instruction. If SIGNO is zero, then cancel any
4140 pending signal; if non-zero, then arrange for the indicated signal
4141 to be delivered to the child when it runs. If PID is -1, then
4142 allow any child thread to run; if non-zero, then allow only the
4143 indicated thread to run. (not implemented yet). */
4146 procfs_resume (struct target_ops
*ops
,
4147 ptid_t ptid
, int step
, enum gdb_signal signo
)
4149 procinfo
*pi
, *thread
;
4153 prrun.prflags |= PRSVADDR;
4154 prrun.pr_vaddr = $PC; set resume address
4155 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4156 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4157 prrun.prflags |= PRCFAULT; clear current fault.
4159 PRSTRACE and PRSFAULT can be done by other means
4160 (proc_trace_signals, proc_trace_faults)
4161 PRSVADDR is unnecessary.
4162 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4163 This basically leaves PRSTEP and PRCSIG.
4164 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4165 So basically PR_STEP is the sole argument that must be passed
4166 to proc_run_process (for use in the prrun struct by ioctl). */
4168 /* Find procinfo for main process. */
4169 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4171 /* First cut: ignore pid argument. */
4174 /* Convert signal to host numbering. */
4176 (signo
== GDB_SIGNAL_STOP
&& pi
->ignore_next_sigstop
))
4179 native_signo
= gdb_signal_to_host (signo
);
4181 pi
->ignore_next_sigstop
= 0;
4183 /* Running the process voids all cached registers and status. */
4184 /* Void the threads' caches first. */
4185 proc_iterate_over_threads (pi
, invalidate_cache
, NULL
);
4186 /* Void the process procinfo's caches. */
4187 invalidate_cache (NULL
, pi
, NULL
);
4189 if (PIDGET (ptid
) != -1)
4191 /* Resume a specific thread, presumably suppressing the
4193 thread
= find_procinfo (PIDGET (ptid
), TIDGET (ptid
));
4196 if (thread
->tid
!= 0)
4198 /* We're to resume a specific thread, and not the
4199 others. Set the child process's PR_ASYNC flag. */
4201 if (!proc_set_async (pi
))
4202 proc_error (pi
, "target_resume, set_async", __LINE__
);
4205 proc_iterate_over_threads (pi
,
4206 make_signal_thread_runnable
,
4209 pi
= thread
; /* Substitute the thread's procinfo
4215 if (!proc_run_process (pi
, step
, native_signo
))
4218 warning (_("resume: target already running. "
4219 "Pretend to resume, and hope for the best!"));
4221 proc_error (pi
, "target_resume", __LINE__
);
4225 /* Set up to trace signals in the child process. */
4228 procfs_pass_signals (int numsigs
, unsigned char *pass_signals
)
4230 gdb_sigset_t signals
;
4231 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4234 prfillset (&signals
);
4236 for (signo
= 0; signo
< NSIG
; signo
++)
4238 int target_signo
= gdb_signal_from_host (signo
);
4239 if (target_signo
< numsigs
&& pass_signals
[target_signo
])
4240 gdb_prdelset (&signals
, signo
);
4243 if (!proc_set_traced_signals (pi
, &signals
))
4244 proc_error (pi
, "pass_signals", __LINE__
);
4247 /* Print status information about the child process. */
4250 procfs_files_info (struct target_ops
*ignore
)
4252 struct inferior
*inf
= current_inferior ();
4254 printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"),
4255 inf
->attach_flag
? "attached": "child",
4256 target_pid_to_str (inferior_ptid
));
4259 /* Stop the child process asynchronously, as when the gdb user types
4260 control-c or presses a "stop" button. Works by sending
4261 kill(SIGINT) to the child's process group. */
4264 procfs_stop (ptid_t ptid
)
4266 kill (-inferior_process_group (), SIGINT
);
4269 /* Make it die. Wait for it to die. Clean up after it. Note: this
4270 should only be applied to the real process, not to an LWP, because
4271 of the check for parent-process. If we need this to work for an
4272 LWP, it needs some more logic. */
4275 unconditionally_kill_inferior (procinfo
*pi
)
4279 parent_pid
= proc_parent_pid (pi
);
4280 #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4281 /* FIXME: use access functions. */
4282 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4283 before the PIOCKILL, otherwise it might generate a corrupted core
4284 file for the inferior. */
4285 if (ioctl (pi
->ctl_fd
, PIOCSSIG
, NULL
) < 0)
4287 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4290 #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4291 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4292 to kill the inferior, otherwise it might remain stopped with a
4294 We do not check the result of the PIOCSSIG, the inferior might have
4297 gdb_siginfo_t newsiginfo
;
4299 memset ((char *) &newsiginfo
, 0, sizeof (newsiginfo
));
4300 newsiginfo
.si_signo
= SIGKILL
;
4301 newsiginfo
.si_code
= 0;
4302 newsiginfo
.si_errno
= 0;
4303 newsiginfo
.si_pid
= getpid ();
4304 newsiginfo
.si_uid
= getuid ();
4305 /* FIXME: use proc_set_current_signal. */
4306 ioctl (pi
->ctl_fd
, PIOCSSIG
, &newsiginfo
);
4308 #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4309 if (!proc_kill (pi
, SIGKILL
))
4310 proc_error (pi
, "unconditionally_kill, proc_kill", __LINE__
);
4311 #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4312 destroy_procinfo (pi
);
4314 /* If pi is GDB's child, wait for it to die. */
4315 if (parent_pid
== getpid ())
4316 /* FIXME: should we use waitpid to make sure we get the right event?
4317 Should we check the returned event? */
4322 ret
= waitpid (pi
->pid
, &status
, 0);
4329 /* We're done debugging it, and we want it to go away. Then we want
4330 GDB to forget all about it. */
4333 procfs_kill_inferior (struct target_ops
*ops
)
4335 if (!ptid_equal (inferior_ptid
, null_ptid
)) /* ? */
4337 /* Find procinfo for main process. */
4338 procinfo
*pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4341 unconditionally_kill_inferior (pi
);
4342 target_mourn_inferior ();
4346 /* Forget we ever debugged this thing! */
4349 procfs_mourn_inferior (struct target_ops
*ops
)
4353 if (!ptid_equal (inferior_ptid
, null_ptid
))
4355 /* Find procinfo for main process. */
4356 pi
= find_procinfo (PIDGET (inferior_ptid
), 0);
4358 destroy_procinfo (pi
);
4360 unpush_target (ops
);
4362 if (dbx_link_bpt
!= NULL
)
4364 deprecated_remove_raw_breakpoint (target_gdbarch (), dbx_link_bpt
);
4365 dbx_link_bpt_addr
= 0;
4366 dbx_link_bpt
= NULL
;
4369 generic_mourn_inferior ();
4372 /* When GDB forks to create a runnable inferior process, this function
4373 is called on the parent side of the fork. It's job is to do
4374 whatever is necessary to make the child ready to be debugged, and
4375 then wait for the child to synchronize. */
4378 procfs_init_inferior (struct target_ops
*ops
, int pid
)
4381 gdb_sigset_t signals
;
4385 /* This routine called on the parent side (GDB side)
4386 after GDB forks the inferior. */
4389 if ((pi
= create_procinfo (pid
, 0)) == NULL
)
4390 perror (_("procfs: out of memory in 'init_inferior'"));
4392 if (!open_procinfo_files (pi
, FD_CTL
))
4393 proc_error (pi
, "init_inferior, open_proc_files", __LINE__
);
4397 open_procinfo_files // done
4400 procfs_notice_signals
4407 /* If not stopped yet, wait for it to stop. */
4408 if (!(proc_flags (pi
) & PR_STOPPED
) &&
4409 !(proc_wait_for_stop (pi
)))
4410 dead_procinfo (pi
, "init_inferior: wait_for_stop failed", KILL
);
4412 /* Save some of the /proc state to be restored if we detach. */
4413 /* FIXME: Why? In case another debugger was debugging it?
4414 We're it's parent, for Ghu's sake! */
4415 if (!proc_get_traced_signals (pi
, &pi
->saved_sigset
))
4416 proc_error (pi
, "init_inferior, get_traced_signals", __LINE__
);
4417 if (!proc_get_held_signals (pi
, &pi
->saved_sighold
))
4418 proc_error (pi
, "init_inferior, get_held_signals", __LINE__
);
4419 if (!proc_get_traced_faults (pi
, &pi
->saved_fltset
))
4420 proc_error (pi
, "init_inferior, get_traced_faults", __LINE__
);
4421 if (!proc_get_traced_sysentry (pi
, pi
->saved_entryset
))
4422 proc_error (pi
, "init_inferior, get_traced_sysentry", __LINE__
);
4423 if (!proc_get_traced_sysexit (pi
, pi
->saved_exitset
))
4424 proc_error (pi
, "init_inferior, get_traced_sysexit", __LINE__
);
4426 if ((fail
= procfs_debug_inferior (pi
)) != 0)
4427 proc_error (pi
, "init_inferior (procfs_debug_inferior)", fail
);
4429 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4430 and possibly even turning ON kill-on-last-close at this point. But
4431 I can't make that change without careful testing which I don't have
4432 time to do right now... */
4433 /* Turn on run-on-last-close flag so that the child
4434 will die if GDB goes away for some reason. */
4435 if (!proc_set_run_on_last_close (pi
))
4436 proc_error (pi
, "init_inferior, set_RLC", __LINE__
);
4438 /* We now have have access to the lwpid of the main thread/lwp. */
4439 lwpid
= proc_get_current_thread (pi
);
4441 /* Create a procinfo for the main lwp. */
4442 create_procinfo (pid
, lwpid
);
4444 /* We already have a main thread registered in the thread table at
4445 this point, but it didn't have any lwp info yet. Notify the core
4446 about it. This changes inferior_ptid as well. */
4447 thread_change_ptid (pid_to_ptid (pid
),
4448 MERGEPID (pid
, lwpid
));
4450 /* Typically two, one trap to exec the shell, one to exec the
4451 program being debugged. Defined by "inferior.h". */
4452 startup_inferior (START_INFERIOR_TRAPS_EXPECTED
);
4455 /* On mips-irix, we need to stop the inferior early enough during
4456 the startup phase in order to be able to load the shared library
4457 symbols and insert the breakpoints that are located in these shared
4458 libraries. Stopping at the program entry point is not good enough
4459 because the -init code is executed before the execution reaches
4462 So what we need to do is to insert a breakpoint in the runtime
4463 loader (rld), more precisely in __dbx_link(). This procedure is
4464 called by rld once all shared libraries have been mapped, but before
4465 the -init code is executed. Unfortuantely, this is not straightforward,
4466 as rld is not part of the executable we are running, and thus we need
4467 the inferior to run until rld itself has been mapped in memory.
4469 For this, we trace all syssgi() syscall exit events. Each time
4470 we detect such an event, we iterate over each text memory maps,
4471 get its associated fd, and scan the symbol table for __dbx_link().
4472 When found, we know that rld has been mapped, and that we can insert
4473 the breakpoint at the symbol address. Once the dbx_link() breakpoint
4474 has been inserted, the syssgi() notifications are no longer necessary,
4475 so they should be canceled. */
4476 proc_trace_syscalls_1 (pi
, SYS_syssgi
, PR_SYSEXIT
, FLAG_SET
, 0);
4480 /* When GDB forks to create a new process, this function is called on
4481 the child side of the fork before GDB exec's the user program. Its
4482 job is to make the child minimally debuggable, so that the parent
4483 GDB process can connect to the child and take over. This function
4484 should do only the minimum to make that possible, and to
4485 synchronize with the parent process. The parent process should
4486 take care of the details. */
4489 procfs_set_exec_trap (void)
4491 /* This routine called on the child side (inferior side)
4492 after GDB forks the inferior. It must use only local variables,
4493 because it may be sharing data space with its parent. */
4498 if ((pi
= create_procinfo (getpid (), 0)) == NULL
)
4499 perror_with_name (_("procfs: create_procinfo failed in child."));
4501 if (open_procinfo_files (pi
, FD_CTL
) == 0)
4503 proc_warn (pi
, "set_exec_trap, open_proc_files", __LINE__
);
4504 gdb_flush (gdb_stderr
);
4505 /* No need to call "dead_procinfo", because we're going to
4510 #ifdef PRFS_STOPEXEC /* defined on OSF */
4511 /* OSF method for tracing exec syscalls. Quoting:
4512 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4513 exits from exec system calls because of the user level loader. */
4514 /* FIXME: make nice and maybe move into an access function. */
4518 if (ioctl (pi
->ctl_fd
, PIOCGSPCACT
, &prfs_flags
) < 0)
4520 proc_warn (pi
, "set_exec_trap (PIOCGSPCACT)", __LINE__
);
4521 gdb_flush (gdb_stderr
);
4524 prfs_flags
|= PRFS_STOPEXEC
;
4526 if (ioctl (pi
->ctl_fd
, PIOCSSPCACT
, &prfs_flags
) < 0)
4528 proc_warn (pi
, "set_exec_trap (PIOCSSPCACT)", __LINE__
);
4529 gdb_flush (gdb_stderr
);
4533 #else /* not PRFS_STOPEXEC */
4534 /* Everyone else's (except OSF) method for tracing exec syscalls. */
4536 Not all systems with /proc have all the exec* syscalls with the same
4537 names. On the SGI, for example, there is no SYS_exec, but there
4538 *is* a SYS_execv. So, we try to account for that. */
4540 exitset
= sysset_t_alloc (pi
);
4541 gdb_premptysysset (exitset
);
4543 gdb_praddsysset (exitset
, SYS_exec
);
4546 gdb_praddsysset (exitset
, SYS_execve
);
4549 gdb_praddsysset (exitset
, SYS_execv
);
4551 #ifdef DYNAMIC_SYSCALLS
4553 int callnum
= find_syscall (pi
, "execve");
4556 gdb_praddsysset (exitset
, callnum
);
4558 callnum
= find_syscall (pi
, "ra_execve");
4560 gdb_praddsysset (exitset
, callnum
);
4562 #endif /* DYNAMIC_SYSCALLS */
4564 if (!proc_set_traced_sysexit (pi
, exitset
))
4566 proc_warn (pi
, "set_exec_trap, set_traced_sysexit", __LINE__
);
4567 gdb_flush (gdb_stderr
);
4570 #endif /* PRFS_STOPEXEC */
4572 /* FIXME: should this be done in the parent instead? */
4573 /* Turn off inherit on fork flag so that all grand-children
4574 of gdb start with tracing flags cleared. */
4575 if (!proc_unset_inherit_on_fork (pi
))
4576 proc_warn (pi
, "set_exec_trap, unset_inherit", __LINE__
);
4578 /* Turn off run on last close flag, so that the child process
4579 cannot run away just because we close our handle on it.
4580 We want it to wait for the parent to attach. */
4581 if (!proc_unset_run_on_last_close (pi
))
4582 proc_warn (pi
, "set_exec_trap, unset_RLC", __LINE__
);
4584 /* FIXME: No need to destroy the procinfo --
4585 we have our own address space, and we're about to do an exec! */
4586 /*destroy_procinfo (pi);*/
4589 /* This function is called BEFORE gdb forks the inferior process. Its
4590 only real responsibility is to set things up for the fork, and tell
4591 GDB which two functions to call after the fork (one for the parent,
4592 and one for the child).
4594 This function does a complicated search for a unix shell program,
4595 which it then uses to parse arguments and environment variables to
4596 be sent to the child. I wonder whether this code could not be
4597 abstracted out and shared with other unix targets such as
4601 procfs_create_inferior (struct target_ops
*ops
, char *exec_file
,
4602 char *allargs
, char **env
, int from_tty
)
4604 char *shell_file
= getenv ("SHELL");
4608 if (shell_file
!= NULL
&& strchr (shell_file
, '/') == NULL
)
4611 /* We will be looking down the PATH to find shell_file. If we
4612 just do this the normal way (via execlp, which operates by
4613 attempting an exec for each element of the PATH until it
4614 finds one which succeeds), then there will be an exec for
4615 each failed attempt, each of which will cause a PR_SYSEXIT
4616 stop, and we won't know how to distinguish the PR_SYSEXIT's
4617 for these failed execs with the ones for successful execs
4618 (whether the exec has succeeded is stored at that time in the
4619 carry bit or some such architecture-specific and
4620 non-ABI-specified place).
4622 So I can't think of anything better than to search the PATH
4623 now. This has several disadvantages: (1) There is a race
4624 condition; if we find a file now and it is deleted before we
4625 exec it, we lose, even if the deletion leaves a valid file
4626 further down in the PATH, (2) there is no way to know exactly
4627 what an executable (in the sense of "capable of being
4628 exec'd") file is. Using access() loses because it may lose
4629 if the caller is the superuser; failing to use it loses if
4630 there are ACLs or some such. */
4634 /* FIXME-maybe: might want "set path" command so user can change what
4635 path is used from within GDB. */
4636 char *path
= getenv ("PATH");
4638 struct stat statbuf
;
4641 path
= "/bin:/usr/bin";
4643 tryname
= alloca (strlen (path
) + strlen (shell_file
) + 2);
4644 for (p
= path
; p
!= NULL
; p
= p1
? p1
+ 1: NULL
)
4646 p1
= strchr (p
, ':');
4651 strncpy (tryname
, p
, len
);
4652 tryname
[len
] = '\0';
4653 strcat (tryname
, "/");
4654 strcat (tryname
, shell_file
);
4655 if (access (tryname
, X_OK
) < 0)
4657 if (stat (tryname
, &statbuf
) < 0)
4659 if (!S_ISREG (statbuf
.st_mode
))
4660 /* We certainly need to reject directories. I'm not quite
4661 as sure about FIFOs, sockets, etc., but I kind of doubt
4662 that people want to exec() these things. */
4667 /* Not found. This must be an error rather than merely passing
4668 the file to execlp(), because execlp() would try all the
4669 exec()s, causing GDB to get confused. */
4670 error (_("procfs:%d -- Can't find shell %s in PATH"),
4671 __LINE__
, shell_file
);
4673 shell_file
= tryname
;
4676 pid
= fork_inferior (exec_file
, allargs
, env
, procfs_set_exec_trap
,
4677 NULL
, NULL
, shell_file
, NULL
);
4679 procfs_init_inferior (ops
, pid
);
4682 /* An observer for the "inferior_created" event. */
4685 procfs_inferior_created (struct target_ops
*ops
, int from_tty
)
4688 /* Make sure to cancel the syssgi() syscall-exit notifications.
4689 They should normally have been removed by now, but they may still
4690 be activated if the inferior doesn't use shared libraries, or if
4691 we didn't locate __dbx_link, or if we never stopped in __dbx_link.
4692 See procfs_init_inferior() for more details.
4694 Since these notifications are only ever enabled when we spawned
4695 the inferior ourselves, there is nothing to do when the inferior
4696 was created by attaching to an already running process, or when
4697 debugging a core file. */
4698 if (current_inferior ()->attach_flag
|| !target_can_run (¤t_target
))
4701 proc_trace_syscalls_1 (find_procinfo_or_die (PIDGET (inferior_ptid
), 0),
4702 SYS_syssgi
, PR_SYSEXIT
, FLAG_RESET
, 0);
4706 /* Callback for find_new_threads. Calls "add_thread". */
4709 procfs_notice_thread (procinfo
*pi
, procinfo
*thread
, void *ptr
)
4711 ptid_t gdb_threadid
= MERGEPID (pi
->pid
, thread
->tid
);
4713 if (!in_thread_list (gdb_threadid
) || is_exited (gdb_threadid
))
4714 add_thread (gdb_threadid
);
4719 /* Query all the threads that the target knows about, and give them
4720 back to GDB to add to its list. */
4723 procfs_find_new_threads (struct target_ops
*ops
)
4727 /* Find procinfo for main process. */
4728 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4729 proc_update_threads (pi
);
4730 proc_iterate_over_threads (pi
, procfs_notice_thread
, NULL
);
4733 /* Return true if the thread is still 'alive'. This guy doesn't
4734 really seem to be doing his job. Got to investigate how to tell
4735 when a thread is really gone. */
4738 procfs_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
4743 proc
= PIDGET (ptid
);
4744 thread
= TIDGET (ptid
);
4745 /* If I don't know it, it ain't alive! */
4746 if ((pi
= find_procinfo (proc
, thread
)) == NULL
)
4749 /* If I can't get its status, it ain't alive!
4750 What's more, I need to forget about it! */
4751 if (!proc_get_status (pi
))
4753 destroy_procinfo (pi
);
4756 /* I couldn't have got its status if it weren't alive, so it's
4761 /* Convert PTID to a string. Returns the string in a static
4765 procfs_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
4767 static char buf
[80];
4769 if (TIDGET (ptid
) == 0)
4770 sprintf (buf
, "process %d", PIDGET (ptid
));
4772 sprintf (buf
, "LWP %ld", TIDGET (ptid
));
4777 /* Insert a watchpoint. */
4780 procfs_set_watchpoint (ptid_t ptid
, CORE_ADDR addr
, int len
, int rwflag
,
4787 pi
= find_procinfo_or_die (PIDGET (ptid
) == -1 ?
4788 PIDGET (inferior_ptid
) : PIDGET (ptid
), 0);
4790 /* Translate from GDB's flags to /proc's. */
4791 if (len
> 0) /* len == 0 means delete watchpoint. */
4793 switch (rwflag
) { /* FIXME: need an enum! */
4794 case hw_write
: /* default watchpoint (write) */
4795 pflags
= WRITE_WATCHFLAG
;
4797 case hw_read
: /* read watchpoint */
4798 pflags
= READ_WATCHFLAG
;
4800 case hw_access
: /* access watchpoint */
4801 pflags
= READ_WATCHFLAG
| WRITE_WATCHFLAG
;
4803 case hw_execute
: /* execution HW breakpoint */
4804 pflags
= EXEC_WATCHFLAG
;
4806 default: /* Something weird. Return error. */
4809 if (after
) /* Stop after r/w access is completed. */
4810 pflags
|= AFTER_WATCHFLAG
;
4813 if (!proc_set_watchpoint (pi
, addr
, len
, pflags
))
4815 if (errno
== E2BIG
) /* Typical error for no resources. */
4816 return -1; /* fail */
4817 /* GDB may try to remove the same watchpoint twice.
4818 If a remove request returns no match, don't error. */
4819 if (errno
== ESRCH
&& len
== 0)
4820 return 0; /* ignore */
4821 proc_error (pi
, "set_watchpoint", __LINE__
);
4827 /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
4828 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
4829 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
4832 Note: procfs_can_use_hw_breakpoint() is not yet used by all
4833 procfs.c targets due to the fact that some of them still define
4834 target_can_use_hardware_watchpoint. */
4837 procfs_can_use_hw_breakpoint (int type
, int cnt
, int othertype
)
4839 /* Due to the way that proc_set_watchpoint() is implemented, host
4840 and target pointers must be of the same size. If they are not,
4841 we can't use hardware watchpoints. This limitation is due to the
4842 fact that proc_set_watchpoint() calls
4843 procfs_address_to_host_pointer(); a close inspection of
4844 procfs_address_to_host_pointer will reveal that an internal error
4845 will be generated when the host and target pointer sizes are
4847 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
4849 if (sizeof (void *) != TYPE_LENGTH (ptr_type
))
4852 /* Other tests here??? */
4857 /* Returns non-zero if process is stopped on a hardware watchpoint
4858 fault, else returns zero. */
4861 procfs_stopped_by_watchpoint (void)
4865 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4867 if (proc_flags (pi
) & (PR_STOPPED
| PR_ISTOP
))
4869 if (proc_why (pi
) == PR_FAULTED
)
4872 if (proc_what (pi
) == FLTWATCH
)
4876 if (proc_what (pi
) == FLTKWATCH
)
4884 /* Returns 1 if the OS knows the position of the triggered watchpoint,
4885 and sets *ADDR to that address. Returns 0 if OS cannot report that
4886 address. This function is only called if
4887 procfs_stopped_by_watchpoint returned 1, thus no further checks are
4888 done. The function also assumes that ADDR is not NULL. */
4891 procfs_stopped_data_address (struct target_ops
*targ
, CORE_ADDR
*addr
)
4895 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
4896 return proc_watchpoint_address (pi
, addr
);
4900 procfs_insert_watchpoint (CORE_ADDR addr
, int len
, int type
,
4901 struct expression
*cond
)
4903 if (!target_have_steppable_watchpoint
4904 && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch ()))
4906 /* When a hardware watchpoint fires off the PC will be left at
4907 the instruction following the one which caused the
4908 watchpoint. It will *NOT* be necessary for GDB to step over
4910 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 1);
4914 /* When a hardware watchpoint fires off the PC will be left at
4915 the instruction which caused the watchpoint. It will be
4916 necessary for GDB to step over the watchpoint. */
4917 return procfs_set_watchpoint (inferior_ptid
, addr
, len
, type
, 0);
4922 procfs_remove_watchpoint (CORE_ADDR addr
, int len
, int type
,
4923 struct expression
*cond
)
4925 return procfs_set_watchpoint (inferior_ptid
, addr
, 0, 0, 0);
4929 procfs_region_ok_for_hw_watchpoint (CORE_ADDR addr
, int len
)
4931 /* The man page for proc(4) on Solaris 2.6 and up says that the
4932 system can support "thousands" of hardware watchpoints, but gives
4933 no method for finding out how many; It doesn't say anything about
4934 the allowed size for the watched area either. So we just tell
4940 procfs_use_watchpoints (struct target_ops
*t
)
4942 t
->to_stopped_by_watchpoint
= procfs_stopped_by_watchpoint
;
4943 t
->to_insert_watchpoint
= procfs_insert_watchpoint
;
4944 t
->to_remove_watchpoint
= procfs_remove_watchpoint
;
4945 t
->to_region_ok_for_hw_watchpoint
= procfs_region_ok_for_hw_watchpoint
;
4946 t
->to_can_use_hw_breakpoint
= procfs_can_use_hw_breakpoint
;
4947 t
->to_stopped_data_address
= procfs_stopped_data_address
;
4950 /* Memory Mappings Functions: */
4952 /* Call a callback function once for each mapping, passing it the
4953 mapping, an optional secondary callback function, and some optional
4954 opaque data. Quit and return the first non-zero value returned
4957 PI is the procinfo struct for the process to be mapped. FUNC is
4958 the callback function to be called by this iterator. DATA is the
4959 optional opaque data to be passed to the callback function.
4960 CHILD_FUNC is the optional secondary function pointer to be passed
4961 to the child function. Returns the first non-zero return value
4962 from the callback function, or zero. */
4965 iterate_over_mappings (procinfo
*pi
, find_memory_region_ftype child_func
,
4967 int (*func
) (struct prmap
*map
,
4968 find_memory_region_ftype child_func
,
4971 char pathname
[MAX_PROC_NAME_SIZE
];
4972 struct prmap
*prmaps
;
4973 struct prmap
*prmap
;
4977 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
4982 /* Get the number of mappings, allocate space,
4983 and read the mappings into prmaps. */
4986 sprintf (pathname
, "/proc/%d/map", pi
->pid
);
4987 if ((map_fd
= open (pathname
, O_RDONLY
)) < 0)
4988 proc_error (pi
, "iterate_over_mappings (open)", __LINE__
);
4990 /* Make sure it gets closed again. */
4991 make_cleanup_close (map_fd
);
4993 /* Use stat to determine the file size, and compute
4994 the number of prmap_t objects it contains. */
4995 if (fstat (map_fd
, &sbuf
) != 0)
4996 proc_error (pi
, "iterate_over_mappings (fstat)", __LINE__
);
4998 nmap
= sbuf
.st_size
/ sizeof (prmap_t
);
4999 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5000 if (read (map_fd
, (char *) prmaps
, nmap
* sizeof (*prmaps
))
5001 != (nmap
* sizeof (*prmaps
)))
5002 proc_error (pi
, "iterate_over_mappings (read)", __LINE__
);
5004 /* Use ioctl command PIOCNMAP to get number of mappings. */
5005 if (ioctl (pi
->ctl_fd
, PIOCNMAP
, &nmap
) != 0)
5006 proc_error (pi
, "iterate_over_mappings (PIOCNMAP)", __LINE__
);
5008 prmaps
= (struct prmap
*) alloca ((nmap
+ 1) * sizeof (*prmaps
));
5009 if (ioctl (pi
->ctl_fd
, PIOCMAP
, prmaps
) != 0)
5010 proc_error (pi
, "iterate_over_mappings (PIOCMAP)", __LINE__
);
5013 for (prmap
= prmaps
; nmap
> 0; prmap
++, nmap
--)
5014 if ((funcstat
= (*func
) (prmap
, child_func
, data
)) != 0)
5016 do_cleanups (cleanups
);
5020 do_cleanups (cleanups
);
5024 /* Implements the to_find_memory_regions method. Calls an external
5025 function for each memory region.
5026 Returns the integer value returned by the callback. */
5029 find_memory_regions_callback (struct prmap
*map
,
5030 find_memory_region_ftype func
, void *data
)
5032 return (*func
) ((CORE_ADDR
) map
->pr_vaddr
,
5034 (map
->pr_mflags
& MA_READ
) != 0,
5035 (map
->pr_mflags
& MA_WRITE
) != 0,
5036 (map
->pr_mflags
& MA_EXEC
) != 0,
5037 1, /* MODIFIED is unknown, pass it as true. */
5041 /* External interface. Calls a callback function once for each
5042 mapped memory region in the child process, passing as arguments:
5044 CORE_ADDR virtual_address,
5046 int read, TRUE if region is readable by the child
5047 int write, TRUE if region is writable by the child
5048 int execute TRUE if region is executable by the child.
5050 Stops iterating and returns the first non-zero value returned by
5054 proc_find_memory_regions (find_memory_region_ftype func
, void *data
)
5056 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5058 return iterate_over_mappings (pi
, func
, data
,
5059 find_memory_regions_callback
);
5062 /* Returns an ascii representation of a memory mapping's flags. */
5065 mappingflags (long flags
)
5067 static char asciiflags
[8];
5069 strcpy (asciiflags
, "-------");
5070 #if defined (MA_PHYS)
5071 if (flags
& MA_PHYS
)
5072 asciiflags
[0] = 'd';
5074 if (flags
& MA_STACK
)
5075 asciiflags
[1] = 's';
5076 if (flags
& MA_BREAK
)
5077 asciiflags
[2] = 'b';
5078 if (flags
& MA_SHARED
)
5079 asciiflags
[3] = 's';
5080 if (flags
& MA_READ
)
5081 asciiflags
[4] = 'r';
5082 if (flags
& MA_WRITE
)
5083 asciiflags
[5] = 'w';
5084 if (flags
& MA_EXEC
)
5085 asciiflags
[6] = 'x';
5086 return (asciiflags
);
5089 /* Callback function, does the actual work for 'info proc
5093 info_mappings_callback (struct prmap
*map
, find_memory_region_ftype ignore
,
5096 unsigned int pr_off
;
5098 #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5099 pr_off
= (unsigned int) map
->pr_offset
;
5101 pr_off
= map
->pr_off
;
5104 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
5105 printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
5106 (unsigned long) map
->pr_vaddr
,
5107 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5108 (unsigned long) map
->pr_size
,
5110 mappingflags (map
->pr_mflags
));
5112 printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n",
5113 (unsigned long) map
->pr_vaddr
,
5114 (unsigned long) map
->pr_vaddr
+ map
->pr_size
- 1,
5115 (unsigned long) map
->pr_size
,
5117 mappingflags (map
->pr_mflags
));
5122 /* Implement the "info proc mappings" subcommand. */
5125 info_proc_mappings (procinfo
*pi
, int summary
)
5128 return; /* No output for summary mode. */
5130 printf_filtered (_("Mapped address spaces:\n\n"));
5131 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
5132 printf_filtered ("\t%10s %10s %10s %10s %7s\n",
5139 printf_filtered (" %18s %18s %10s %10s %7s\n",
5146 iterate_over_mappings (pi
, NULL
, NULL
, info_mappings_callback
);
5147 printf_filtered ("\n");
5150 /* Implement the "info proc" command. */
5153 procfs_info_proc (struct target_ops
*ops
, char *args
,
5154 enum info_proc_what what
)
5156 struct cleanup
*old_chain
;
5157 procinfo
*process
= NULL
;
5158 procinfo
*thread
= NULL
;
5176 error (_("Not supported on this target."));
5179 old_chain
= make_cleanup (null_cleanup
, 0);
5182 argv
= gdb_buildargv (args
);
5183 make_cleanup_freeargv (argv
);
5185 while (argv
!= NULL
&& *argv
!= NULL
)
5187 if (isdigit (argv
[0][0]))
5189 pid
= strtoul (argv
[0], &tmp
, 10);
5191 tid
= strtoul (++tmp
, NULL
, 10);
5193 else if (argv
[0][0] == '/')
5195 tid
= strtoul (argv
[0] + 1, NULL
, 10);
5200 pid
= PIDGET (inferior_ptid
);
5202 error (_("No current process: you must name one."));
5205 /* Have pid, will travel.
5206 First see if it's a process we're already debugging. */
5207 process
= find_procinfo (pid
, 0);
5208 if (process
== NULL
)
5210 /* No. So open a procinfo for it, but
5211 remember to close it again when finished. */
5212 process
= create_procinfo (pid
, 0);
5213 make_cleanup (do_destroy_procinfo_cleanup
, process
);
5214 if (!open_procinfo_files (process
, FD_CTL
))
5215 proc_error (process
, "info proc, open_procinfo_files", __LINE__
);
5219 thread
= create_procinfo (pid
, tid
);
5223 printf_filtered (_("process %d flags:\n"), process
->pid
);
5224 proc_prettyprint_flags (proc_flags (process
), 1);
5225 if (proc_flags (process
) & (PR_STOPPED
| PR_ISTOP
))
5226 proc_prettyprint_why (proc_why (process
), proc_what (process
), 1);
5227 if (proc_get_nthreads (process
) > 1)
5228 printf_filtered ("Process has %d threads.\n",
5229 proc_get_nthreads (process
));
5233 printf_filtered (_("thread %d flags:\n"), thread
->tid
);
5234 proc_prettyprint_flags (proc_flags (thread
), 1);
5235 if (proc_flags (thread
) & (PR_STOPPED
| PR_ISTOP
))
5236 proc_prettyprint_why (proc_why (thread
), proc_what (thread
), 1);
5241 info_proc_mappings (process
, 0);
5244 do_cleanups (old_chain
);
5247 /* Modify the status of the system call identified by SYSCALLNUM in
5248 the set of syscalls that are currently traced/debugged.
5250 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
5251 will be updated. Otherwise, the exit syscalls set will be updated.
5253 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
5254 will be disabled. */
5257 proc_trace_syscalls_1 (procinfo
*pi
, int syscallnum
, int entry_or_exit
,
5258 int mode
, int from_tty
)
5262 if (entry_or_exit
== PR_SYSENTRY
)
5263 sysset
= proc_get_traced_sysentry (pi
, NULL
);
5265 sysset
= proc_get_traced_sysexit (pi
, NULL
);
5268 proc_error (pi
, "proc-trace, get_traced_sysset", __LINE__
);
5270 if (mode
== FLAG_SET
)
5271 gdb_praddsysset (sysset
, syscallnum
);
5273 gdb_prdelsysset (sysset
, syscallnum
);
5275 if (entry_or_exit
== PR_SYSENTRY
)
5277 if (!proc_set_traced_sysentry (pi
, sysset
))
5278 proc_error (pi
, "proc-trace, set_traced_sysentry", __LINE__
);
5282 if (!proc_set_traced_sysexit (pi
, sysset
))
5283 proc_error (pi
, "proc-trace, set_traced_sysexit", __LINE__
);
5288 proc_trace_syscalls (char *args
, int from_tty
, int entry_or_exit
, int mode
)
5292 if (PIDGET (inferior_ptid
) <= 0)
5293 error (_("you must be debugging a process to use this command."));
5295 if (args
== NULL
|| args
[0] == 0)
5296 error_no_arg (_("system call to trace"));
5298 pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5299 if (isdigit (args
[0]))
5301 const int syscallnum
= atoi (args
);
5303 proc_trace_syscalls_1 (pi
, syscallnum
, entry_or_exit
, mode
, from_tty
);
5308 proc_trace_sysentry_cmd (char *args
, int from_tty
)
5310 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_SET
);
5314 proc_trace_sysexit_cmd (char *args
, int from_tty
)
5316 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_SET
);
5320 proc_untrace_sysentry_cmd (char *args
, int from_tty
)
5322 proc_trace_syscalls (args
, from_tty
, PR_SYSENTRY
, FLAG_RESET
);
5326 proc_untrace_sysexit_cmd (char *args
, int from_tty
)
5328 proc_trace_syscalls (args
, from_tty
, PR_SYSEXIT
, FLAG_RESET
);
5332 /* Provide a prototype to silence -Wmissing-prototypes. */
5333 extern void _initialize_procfs (void);
5336 _initialize_procfs (void)
5338 observer_attach_inferior_created (procfs_inferior_created
);
5340 add_com ("proc-trace-entry", no_class
, proc_trace_sysentry_cmd
,
5341 _("Give a trace of entries into the syscall."));
5342 add_com ("proc-trace-exit", no_class
, proc_trace_sysexit_cmd
,
5343 _("Give a trace of exits from the syscall."));
5344 add_com ("proc-untrace-entry", no_class
, proc_untrace_sysentry_cmd
,
5345 _("Cancel a trace of entries into the syscall."));
5346 add_com ("proc-untrace-exit", no_class
, proc_untrace_sysexit_cmd
,
5347 _("Cancel a trace of exits from the syscall."));
5350 /* =================== END, GDB "MODULE" =================== */
5354 /* miscellaneous stubs: */
5356 /* The following satisfy a few random symbols mostly created by the
5357 solaris threads implementation, which I will chase down later. */
5359 /* Return a pid for which we guarantee we will be able to find a
5363 procfs_first_available (void)
5365 return pid_to_ptid (procinfo_list
? procinfo_list
->pid
: -1);
5368 /* =================== GCORE .NOTE "MODULE" =================== */
5369 #if defined (PIOCOPENLWP) || defined (PCAGENT)
5370 /* gcore only implemented on solaris (so far) */
5373 procfs_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
5374 char *note_data
, int *note_size
,
5375 enum gdb_signal stop_signal
)
5377 struct regcache
*regcache
= get_thread_regcache (ptid
);
5378 gdb_gregset_t gregs
;
5379 gdb_fpregset_t fpregs
;
5380 unsigned long merged_pid
;
5381 struct cleanup
*old_chain
;
5383 merged_pid
= TIDGET (ptid
) << 16 | PIDGET (ptid
);
5385 /* This part is the old method for fetching registers.
5386 It should be replaced by the newer one using regsets
5387 once it is implemented in this platform:
5388 gdbarch_regset_from_core_section() and regset->collect_regset(). */
5390 old_chain
= save_inferior_ptid ();
5391 inferior_ptid
= ptid
;
5392 target_fetch_registers (regcache
, -1);
5394 fill_gregset (regcache
, &gregs
, -1);
5395 #if defined (NEW_PROC_API)
5396 note_data
= (char *) elfcore_write_lwpstatus (obfd
,
5403 note_data
= (char *) elfcore_write_prstatus (obfd
,
5410 fill_fpregset (regcache
, &fpregs
, -1);
5411 note_data
= (char *) elfcore_write_prfpreg (obfd
,
5417 do_cleanups (old_chain
);
5422 struct procfs_corefile_thread_data
{
5426 enum gdb_signal stop_signal
;
5430 procfs_corefile_thread_callback (procinfo
*pi
, procinfo
*thread
, void *data
)
5432 struct procfs_corefile_thread_data
*args
= data
;
5436 ptid_t ptid
= MERGEPID (pi
->pid
, thread
->tid
);
5438 args
->note_data
= procfs_do_thread_registers (args
->obfd
, ptid
,
5447 find_signalled_thread (struct thread_info
*info
, void *data
)
5449 if (info
->suspend
.stop_signal
!= GDB_SIGNAL_0
5450 && ptid_get_pid (info
->ptid
) == ptid_get_pid (inferior_ptid
))
5456 static enum gdb_signal
5457 find_stop_signal (void)
5459 struct thread_info
*info
=
5460 iterate_over_threads (find_signalled_thread
, NULL
);
5463 return info
->suspend
.stop_signal
;
5465 return GDB_SIGNAL_0
;
5469 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5471 struct cleanup
*old_chain
;
5472 gdb_gregset_t gregs
;
5473 gdb_fpregset_t fpregs
;
5474 char fname
[16] = {'\0'};
5475 char psargs
[80] = {'\0'};
5476 procinfo
*pi
= find_procinfo_or_die (PIDGET (inferior_ptid
), 0);
5477 char *note_data
= NULL
;
5479 struct procfs_corefile_thread_data thread_args
;
5482 enum gdb_signal stop_signal
;
5484 if (get_exec_file (0))
5486 strncpy (fname
, lbasename (get_exec_file (0)), sizeof (fname
));
5487 fname
[sizeof (fname
) - 1] = 0;
5488 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
5489 psargs
[sizeof (psargs
) - 1] = 0;
5491 inf_args
= get_inferior_args ();
5492 if (inf_args
&& *inf_args
&&
5493 strlen (inf_args
) < ((int) sizeof (psargs
) - (int) strlen (psargs
)))
5495 strncat (psargs
, " ",
5496 sizeof (psargs
) - strlen (psargs
));
5497 strncat (psargs
, inf_args
,
5498 sizeof (psargs
) - strlen (psargs
));
5502 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
5508 stop_signal
= find_stop_signal ();
5511 fill_gregset (get_current_regcache (), &gregs
, -1);
5512 note_data
= elfcore_write_pstatus (obfd
, note_data
, note_size
,
5513 PIDGET (inferior_ptid
),
5514 stop_signal
, &gregs
);
5517 thread_args
.obfd
= obfd
;
5518 thread_args
.note_data
= note_data
;
5519 thread_args
.note_size
= note_size
;
5520 thread_args
.stop_signal
= stop_signal
;
5521 proc_iterate_over_threads (pi
, procfs_corefile_thread_callback
,
5524 /* There should be always at least one thread. */
5525 gdb_assert (thread_args
.note_data
!= note_data
);
5526 note_data
= thread_args
.note_data
;
5528 auxv_len
= target_read_alloc (¤t_target
, TARGET_OBJECT_AUXV
,
5532 note_data
= elfcore_write_note (obfd
, note_data
, note_size
,
5533 "CORE", NT_AUXV
, auxv
, auxv_len
);
5537 make_cleanup (xfree
, note_data
);
5540 #else /* !Solaris */
5542 procfs_make_note_section (bfd
*obfd
, int *note_size
)
5544 error (_("gcore not implemented for this host."));
5545 return NULL
; /* lint */
5547 #endif /* Solaris */
5548 /* =================== END GCORE .NOTE "MODULE" =================== */