150276Speter/* Machine independent support for SVR4 /proc (process file system) for GDB. 250276Speter 397049Speter Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, 4178866Srafan Inc. 5178866Srafan 662449Speter Written by Michael Snyder at Cygnus Solutions. 7184989Srafan Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others. 862449Speter 950276SpeterThis file is part of GDB. 1050276Speter 1150276SpeterThis program is free software; you can redistribute it and/or modify 1250276Speterit under the terms of the GNU General Public License as published by 1350276Speterthe Free Software Foundation; either version 2 of the License, or 1450276Speter(at your option) any later version. 1550276Speter 1650276SpeterThis program is distributed in the hope that it will be useful, 1750276Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of 1850276SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1950276SpeterGNU General Public License for more details. 2050276Speter 21166124SrafanYou should have received a copy of the GNU General Public License 22166124Srafanalong with this program; if not, write to the Free Software Foundation, 2350276SpeterInc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 2450276Speter 2550276Speter#include "defs.h" 2650276Speter#include "inferior.h" 2750276Speter#include "target.h" 2850276Speter#include "gdbcore.h" 29166124Srafan#include "elf-bfd.h" /* for elfcore_write_* */ 3097049Speter#include "gdbcmd.h" 3197049Speter#include "gdbthread.h" 3297049Speter 3350276Speter#if defined (NEW_PROC_API) 3450276Speter#define _STRUCTURED_PROC 1 /* Should be done by configure script. */ 3550276Speter#endif 3650276Speter 3750276Speter#include <sys/procfs.h> 3850276Speter#ifdef HAVE_SYS_FAULT_H 3976726Speter#include <sys/fault.h> 4050276Speter#endif 4162449Speter#ifdef HAVE_SYS_SYSCALL_H 4262449Speter#include <sys/syscall.h> 4362449Speter#endif 4462449Speter#include <sys/errno.h> 4562449Speter#include "gdb_wait.h" 4676726Speter#include <signal.h> 4762449Speter#include <ctype.h> 4862449Speter#include "gdb_string.h" 4976726Speter#include "gdb_assert.h" 5076726Speter#include "inflow.h" 5176726Speter#include "auxv.h" 5262449Speter 5376726Speter/* 5476726Speter * PROCFS.C 5576726Speter * 5676726Speter * This module provides the interface between GDB and the 5776726Speter * /proc file system, which is used on many versions of Unix 5876726Speter * as a means for debuggers to control other processes. 59178866Srafan * Examples of the systems that use this interface are: 60178866Srafan * Irix 6176726Speter * Solaris 6276726Speter * OSF 6376726Speter * Unixware 6476726Speter * AIX5 6562449Speter * 6662449Speter * /proc works by imitating a file system: you open a simulated file 6762449Speter * that represents the process you wish to interact with, and 6862449Speter * perform operations on that "file" in order to examine or change 6962449Speter * the state of the other process. 7076726Speter * 7176726Speter * The most important thing to know about /proc and this module 72166124Srafan * is that there are two very different interfaces to /proc: 7376726Speter * One that uses the ioctl system call, and 74166124Srafan * another that uses read and write system calls. 7562449Speter * This module has to support both /proc interfaces. This means 76166124Srafan * that there are two different ways of doing every basic operation. 7762449Speter * 7862449Speter * In order to keep most of the code simple and clean, I have 7962449Speter * defined an interface "layer" which hides all these system calls. 8062449Speter * An ifdef (NEW_PROC_API) determines which interface we are using, 8162449Speter * and most or all occurrances of this ifdef should be confined to 8276726Speter * this interface layer. 8362449Speter */ 8462449Speter 8562449Speter 8662449Speter/* Determine which /proc API we are using: 8762449Speter The ioctl API defines PIOCSTATUS, while 8862449Speter the read/write (multiple fd) API never does. */ 8962449Speter 9062449Speter#ifdef NEW_PROC_API 9162449Speter#include <sys/types.h> 9262449Speter#include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */ 9362449Speter#endif 94166124Srafan 9562449Speter#include <fcntl.h> /* for O_RDONLY */ 96166124Srafan#include <unistd.h> /* for "X_OK" */ 97166124Srafan#include "gdb_stat.h" /* for struct stat */ 98166124Srafan 99166124Srafan/* Note: procfs-utils.h must be included after the above system header 10062449Speter files, because it redefines various system calls using macros. 10176726Speter This may be incompatible with the prototype declarations. */ 10276726Speter 10376726Speter#include "proc-utils.h" 104166124Srafan 105166124Srafan/* Prototypes for supply_gregset etc. */ 106166124Srafan#include "gregset.h" 107166124Srafan 108166124Srafan/* =================== TARGET_OPS "MODULE" =================== */ 109166124Srafan 110166124Srafan/* 111166124Srafan * This module defines the GDB target vector and its methods. 112166124Srafan */ 113166124Srafan 114166124Srafanstatic void procfs_open (char *, int); 115166124Srafanstatic void procfs_attach (char *, int); 116166124Srafanstatic void procfs_detach (char *, int); 117166124Srafanstatic void procfs_resume (ptid_t, int, enum target_signal); 11897049Speterstatic int procfs_can_run (void); 119166124Srafanstatic void procfs_stop (void); 12097049Speterstatic void procfs_files_info (struct target_ops *); 12197049Speterstatic void procfs_fetch_registers (int); 12297049Speterstatic void procfs_store_registers (int); 12376726Speterstatic void procfs_notice_signals (ptid_t); 12497049Speterstatic void procfs_prepare_to_store (void); 12576726Speterstatic void procfs_kill_inferior (void); 12676726Speterstatic void procfs_mourn_inferior (void); 12776726Speterstatic void procfs_create_inferior (char *, char *, char **); 12876726Speterstatic ptid_t procfs_wait (ptid_t, struct target_waitstatus *); 129166124Srafanstatic int procfs_xfer_memory (CORE_ADDR, char *, int, int, 13076726Speter struct mem_attrib *attrib, 13150276Speter struct target_ops *); 13297049Speterstatic LONGEST procfs_xfer_partial (struct target_ops *ops, 13350276Speter enum target_object object, 13450276Speter const char *annex, 13550276Speter void *readbuf, const void *writebuf, 13650276Speter ULONGEST offset, LONGEST len); 13750276Speter 13850276Speterstatic int procfs_thread_alive (ptid_t); 13976726Speter 14050276Spetervoid procfs_find_new_threads (void); 14150276Speterchar *procfs_pid_to_str (ptid_t); 14250276Speter 14350276Speterstatic int proc_find_memory_regions (int (*) (CORE_ADDR, 14450276Speter unsigned long, 14562449Speter int, int, int, 14697049Speter void *), 14762449Speter void *); 14862449Speter 14962449Speterstatic char * procfs_make_note_section (bfd *, int *); 15062449Speter 15162449Speterstatic int procfs_can_use_hw_breakpoint (int, int, int); 15262449Speter 15397049Speterstruct target_ops procfs_ops; /* the target vector */ 15497049Speter 15597049Speterstatic void 15697049Speterinit_procfs_ops (void) 15797049Speter{ 15897049Speter procfs_ops.to_shortname = "procfs"; 15997049Speter procfs_ops.to_longname = "Unix /proc child process"; 160166124Srafan procfs_ops.to_doc = 16197049Speter "Unix /proc child process (started by the \"run\" command)."; 16297049Speter procfs_ops.to_open = procfs_open; 16397049Speter procfs_ops.to_can_run = procfs_can_run; 164174993Srafan procfs_ops.to_create_inferior = procfs_create_inferior; 16597049Speter procfs_ops.to_kill = procfs_kill_inferior; 16662449Speter procfs_ops.to_mourn_inferior = procfs_mourn_inferior; 16776726Speter procfs_ops.to_attach = procfs_attach; 16876726Speter procfs_ops.to_detach = procfs_detach; 16997049Speter procfs_ops.to_wait = procfs_wait; 17097049Speter procfs_ops.to_resume = procfs_resume; 17197049Speter procfs_ops.to_prepare_to_store = procfs_prepare_to_store; 17276726Speter procfs_ops.to_fetch_registers = procfs_fetch_registers; 17376726Speter procfs_ops.to_store_registers = procfs_store_registers; 17476726Speter procfs_ops.to_xfer_partial = procfs_xfer_partial; 17576726Speter procfs_ops.to_xfer_memory = procfs_xfer_memory; 17676726Speter procfs_ops.to_insert_breakpoint = memory_insert_breakpoint; 17776726Speter procfs_ops.to_remove_breakpoint = memory_remove_breakpoint; 17876726Speter procfs_ops.to_notice_signals = procfs_notice_signals; 17976726Speter procfs_ops.to_files_info = procfs_files_info; 18076726Speter procfs_ops.to_stop = procfs_stop; 18176726Speter 18276726Speter procfs_ops.to_terminal_init = terminal_init_inferior; 183184989Srafan procfs_ops.to_terminal_inferior = terminal_inferior; 18476726Speter procfs_ops.to_terminal_ours_for_output = terminal_ours_for_output; 18576726Speter procfs_ops.to_terminal_ours = terminal_ours; 18662449Speter procfs_ops.to_terminal_save_ours = terminal_save_ours; 187166124Srafan procfs_ops.to_terminal_info = child_terminal_info; 188166124Srafan 189166124Srafan procfs_ops.to_find_new_threads = procfs_find_new_threads; 190166124Srafan procfs_ops.to_thread_alive = procfs_thread_alive; 191166124Srafan procfs_ops.to_pid_to_str = procfs_pid_to_str; 192166124Srafan 193166124Srafan procfs_ops.to_has_all_memory = 1; 194166124Srafan procfs_ops.to_has_memory = 1; 195166124Srafan procfs_ops.to_has_execution = 1; 196166124Srafan procfs_ops.to_has_stack = 1; 197166124Srafan procfs_ops.to_has_registers = 1; 198166124Srafan procfs_ops.to_stratum = process_stratum; 19962449Speter procfs_ops.to_has_thread_control = tc_schedlock; 20062449Speter procfs_ops.to_find_memory_regions = proc_find_memory_regions; 20162449Speter procfs_ops.to_make_corefile_notes = procfs_make_note_section; 20262449Speter procfs_ops.to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint; 203166124Srafan procfs_ops.to_magic = OPS_MAGIC; 20497049Speter} 205166124Srafan 206166124Srafan/* =================== END, TARGET_OPS "MODULE" =================== */ 207166124Srafan 208166124Srafan/* 209166124Srafan * World Unification: 210166124Srafan * 211166124Srafan * Put any typedefs, defines etc. here that are required for 212166124Srafan * the unification of code that handles different versions of /proc. 213166124Srafan */ 214166124Srafan 215166124Srafan#ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */ 216166124Srafan#ifdef WA_READ 217166124Srafan enum { READ_WATCHFLAG = WA_READ, 218166124Srafan WRITE_WATCHFLAG = WA_WRITE, 219166124Srafan EXEC_WATCHFLAG = WA_EXEC, 22050276Speter AFTER_WATCHFLAG = WA_TRAPAFTER 221166124Srafan }; 222184989Srafan#endif 22350276Speter#else /* Irix method for watchpoints */ 224166124Srafan enum { READ_WATCHFLAG = MA_READ, 225184989Srafan WRITE_WATCHFLAG = MA_WRITE, 226184989Srafan EXEC_WATCHFLAG = MA_EXEC, 227166124Srafan AFTER_WATCHFLAG = 0 /* trapafter not implemented */ 228166124Srafan }; 229166124Srafan#endif 230166124Srafan 231166124Srafan/* gdb_sigset_t */ 232166124Srafan#ifdef HAVE_PR_SIGSET_T 233166124Srafantypedef pr_sigset_t gdb_sigset_t; 234166124Srafan#else 235166124Srafantypedef sigset_t gdb_sigset_t; 236166124Srafan#endif 237166124Srafan 238166124Srafan/* sigaction */ 239166124Srafan#ifdef HAVE_PR_SIGACTION64_T 240166124Srafantypedef pr_sigaction64_t gdb_sigaction_t; 241166124Srafan#else 242166124Srafantypedef struct sigaction gdb_sigaction_t; 243166124Srafan#endif 244166124Srafan 245166124Srafan/* siginfo */ 246166124Srafan#ifdef HAVE_PR_SIGINFO64_T 247166124Srafantypedef pr_siginfo64_t gdb_siginfo_t; 248166124Srafan#else 249166124Srafantypedef struct siginfo gdb_siginfo_t; 250166124Srafan#endif 251166124Srafan 252166124Srafan/* gdb_premptysysset */ 253166124Srafan#ifdef premptysysset 254166124Srafan#define gdb_premptysysset premptysysset 255166124Srafan#else 256166124Srafan#define gdb_premptysysset premptyset 257166124Srafan#endif 258166124Srafan 259166124Srafan/* praddsysset */ 260166124Srafan#ifdef praddsysset 261166124Srafan#define gdb_praddsysset praddsysset 262166124Srafan#else 263166124Srafan#define gdb_praddsysset praddset 264166124Srafan#endif 265166124Srafan 266166124Srafan/* prdelsysset */ 26750276Speter#ifdef prdelsysset 26850276Speter#define gdb_prdelsysset prdelsysset 26950276Speter#else 27050276Speter#define gdb_prdelsysset prdelset 271166124Srafan#endif 272166124Srafan 27362449Speter/* prissyssetmember */ 27462449Speter#ifdef prissyssetmember 27562449Speter#define gdb_pr_issyssetmember prissyssetmember 27662449Speter#else 27762449Speter#define gdb_pr_issyssetmember prismember 278166124Srafan#endif 27950276Speter 28050276Speter/* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't 281166124Srafan as intuitively descriptive as it could be, so we'll define 28250276Speter DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of 28376726Speter this writing, this feature is only found on AIX5 systems and 284166124Srafan basically means that the set of syscalls is not fixed. I.e, 28550276Speter there's no nice table that one can #include to get all of the 28650276Speter syscall numbers. Instead, they're stored in /proc/PID/sysent 287166124Srafan for each process. We are at least guaranteed that they won't 28897049Speter change over the lifetime of the process. But each process could 28997049Speter (in theory) have different syscall numbers. 290166124Srafan*/ 29162449Speter#ifdef HAVE_PRSYSENT_T 29262449Speter#define DYNAMIC_SYSCALLS 293166124Srafan#endif 294166124Srafan 295166124Srafan 296166124Srafan 297166124Srafan/* =================== STRUCT PROCINFO "MODULE" =================== */ 298184989Srafan 299166124Srafan /* FIXME: this comment will soon be out of date W.R.T. threads. */ 30050276Speter 30150276Speter/* The procinfo struct is a wrapper to hold all the state information 302166124Srafan concerning a /proc process. There should be exactly one procinfo 303166124Srafan for each process, and since GDB currently can debug only one 304166124Srafan process at a time, that means there should be only one procinfo. 305166124Srafan All of the LWP's of a process can be accessed indirectly thru the 30676726Speter single process procinfo. 30750276Speter 308166124Srafan However, against the day when GDB may debug more than one process, 30962449Speter this data structure is kept in a list (which for now will hold no 31050276Speter more than one member), and many functions will have a pointer to a 31150276Speter procinfo as an argument. 31250276Speter 31350276Speter There will be a separate procinfo structure for use by the (not yet 31450276Speter implemented) "info proc" command, so that we can print useful 31550276Speter information about any random process without interfering with the 316166124Srafan inferior's procinfo information. */ 31762449Speter 31850276Speter#ifdef NEW_PROC_API 319166124Srafan/* format strings for /proc paths */ 320166124Srafan# ifndef CTL_PROC_NAME_FMT 321166124Srafan# define MAIN_PROC_NAME_FMT "/proc/%d" 322166124Srafan# define CTL_PROC_NAME_FMT "/proc/%d/ctl" 323166124Srafan# define AS_PROC_NAME_FMT "/proc/%d/as" 324166124Srafan# define MAP_PROC_NAME_FMT "/proc/%d/map" 325166124Srafan# define STATUS_PROC_NAME_FMT "/proc/%d/status" 326166124Srafan# define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus") 32750276Speter# endif 32850276Speter/* the name of the proc status struct depends on the implementation */ 329166124Srafantypedef pstatus_t gdb_prstatus_t; 33050276Spetertypedef lwpstatus_t gdb_lwpstatus_t; 33150276Speter#else /* ! NEW_PROC_API */ 332166124Srafan/* format strings for /proc paths */ 333178866Srafan# ifndef CTL_PROC_NAME_FMT 33450276Speter# define MAIN_PROC_NAME_FMT "/proc/%05d" 335166124Srafan# define CTL_PROC_NAME_FMT "/proc/%05d" 33650276Speter# define AS_PROC_NAME_FMT "/proc/%05d" 33750276Speter# define MAP_PROC_NAME_FMT "/proc/%05d" 33850276Speter# define STATUS_PROC_NAME_FMT "/proc/%05d" 33950276Speter# define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp") 34050276Speter# endif 341166124Srafan/* the name of the proc status struct depends on the implementation */ 34250276Spetertypedef prstatus_t gdb_prstatus_t; 34350276Spetertypedef prstatus_t gdb_lwpstatus_t; 34450276Speter#endif /* NEW_PROC_API */ 34550276Speter 34650276Spetertypedef struct procinfo { 34750276Speter struct procinfo *next; 34850276Speter int pid; /* Process ID */ 34950276Speter int tid; /* Thread/LWP id */ 350166124Srafan 35150276Speter /* process state */ 35250276Speter int was_stopped; 353166124Srafan int ignore_next_sigstop; 35450276Speter 35597049Speter /* The following four fd fields may be identical, or may contain 35650276Speter several different fd's, depending on the version of /proc 35750276Speter (old ioctl or new read/write). */ 35850276Speter 35950276Speter int ctl_fd; /* File descriptor for /proc control file */ 36050276Speter /* 36150276Speter * The next three file descriptors are actually only needed in the 36250276Speter * read/write, multiple-file-descriptor implemenation (NEW_PROC_API). 36350276Speter * However, to avoid a bunch of #ifdefs in the code, we will use 36450276Speter * them uniformly by (in the case of the ioctl single-file-descriptor 365166124Srafan * implementation) filling them with copies of the control fd. 36650276Speter */ 36750276Speter int status_fd; /* File descriptor for /proc status file */ 368166124Srafan int as_fd; /* File descriptor for /proc as file */ 36962449Speter 37062449Speter char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */ 37162449Speter 37262449Speter fltset_t saved_fltset; /* Saved traced hardware fault set */ 37362449Speter gdb_sigset_t saved_sigset; /* Saved traced signal set */ 37462449Speter gdb_sigset_t saved_sighold; /* Saved held signal set */ 37562449Speter sysset_t *saved_exitset; /* Saved traced system call exit set */ 37662449Speter sysset_t *saved_entryset; /* Saved traced system call entry set */ 37762449Speter 378184989Srafan gdb_prstatus_t prstatus; /* Current process status info */ 379166124Srafan 38062449Speter#ifndef NEW_PROC_API 38162449Speter gdb_fpregset_t fpregset; /* Current floating point registers */ 382184989Srafan#endif 38362449Speter 384184989Srafan#ifdef DYNAMIC_SYSCALLS 385166124Srafan int num_syscalls; /* Total number of syscalls */ 38662449Speter char **syscall_names; /* Syscall number to name map */ 387184989Srafan#endif 388184989Srafan 38962449Speter struct procinfo *thread_list; 390184989Srafan 391184989Srafan int status_valid : 1; 39262449Speter int gregs_valid : 1; 393184989Srafan int fpregs_valid : 1; 394184989Srafan int threads_valid: 1; 395166124Srafan} procinfo; 396166124Srafan 397166124Srafanstatic char errmsg[128]; /* shared error msg buffer */ 39850276Speter 39950276Speter/* Function prototypes for procinfo module: */ 400166124Srafan 40150276Speterstatic procinfo *find_procinfo_or_die (int pid, int tid); 40250276Speterstatic procinfo *find_procinfo (int pid, int tid); 403166124Srafanstatic procinfo *create_procinfo (int pid, int tid); 40450276Speterstatic void destroy_procinfo (procinfo * p); 40550276Speterstatic void do_destroy_procinfo_cleanup (void *); 406166124Srafanstatic void dead_procinfo (procinfo * p, char *msg, int killp); 40750276Speterstatic int open_procinfo_files (procinfo * p, int which); 40850276Speterstatic void close_procinfo_files (procinfo * p); 409166124Srafanstatic int sysset_t_size (procinfo *p); 41050276Speterstatic sysset_t *sysset_t_alloc (procinfo * pi); 41150276Speter#ifdef DYNAMIC_SYSCALLS 412166124Srafanstatic void load_syscalls (procinfo *pi); 41350276Speterstatic void free_syscalls (procinfo *pi); 41497049Speterstatic int find_syscall (procinfo *pi, char *name); 41562449Speter#endif /* DYNAMIC_SYSCALLS */ 41662449Speter 41762449Speter/* The head of the procinfo list: */ 41862449Speterstatic procinfo * procinfo_list; 41962449Speter 42062449Speter/* 42162449Speter * Function: find_procinfo 42250276Speter * 42350276Speter * Search the procinfo list. 42450276Speter * 42550276Speter * Returns: pointer to procinfo, or NULL if not found. 42650276Speter */ 42750276Speter 42850276Speterstatic procinfo * 42950276Speterfind_procinfo (int pid, int tid) 43050276Speter{ 43150276Speter procinfo *pi; 43250276Speter 43350276Speter for (pi = procinfo_list; pi; pi = pi->next) 43450276Speter if (pi->pid == pid) 43550276Speter break; 436166124Srafan 437166124Srafan if (pi) 438166124Srafan if (tid) 439166124Srafan { 44050276Speter /* Don't check threads_valid. If we're updating the 441166124Srafan thread_list, we want to find whatever threads are already 44297049Speter here. This means that in general it is the caller's 44397049Speter responsibility to check threads_valid and update before 444166124Srafan calling find_procinfo, if the caller wants to find a new 445166124Srafan thread. */ 446166124Srafan 447166124Srafan for (pi = pi->thread_list; pi; pi = pi->next) 448166124Srafan if (pi->tid == tid) 449166124Srafan break; 450166124Srafan } 45150276Speter 45250276Speter return pi; 453166124Srafan} 45450276Speter 45550276Speter/* 456166124Srafan * Function: find_procinfo_or_die 45750276Speter * 45850276Speter * Calls find_procinfo, but errors on failure. 459166124Srafan */ 46050276Speter 46150276Speterstatic procinfo * 462166124Srafanfind_procinfo_or_die (int pid, int tid) 46350276Speter{ 464184989Srafan procinfo *pi = find_procinfo (pid, tid); 465184989Srafan 46662449Speter if (pi == NULL) 46762449Speter { 46862449Speter if (tid) 46962449Speter error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.", 47062449Speter pid, tid); 47150276Speter else 47262449Speter error ("procfs: couldn't find pid %d in procinfo list.", pid); 47362449Speter } 47462449Speter return pi; 47550276Speter} 47662449Speter 47750276Speter/* open_with_retry() is a wrapper for open(). The appropriate 478166124Srafan open() call is attempted; if unsuccessful, it will be retried as 47950276Speter many times as needed for the EAGAIN and EINTR conditions. 48050276Speter 481166124Srafan For other conditions, open_with_retry() will retry the open() a 48250276Speter limited number of times. In addition, a short sleep is imposed 48350276Speter prior to retrying the open(). The reason for this sleep is to give 48450276Speter the kernel a chance to catch up and create the file in question in 485166124Srafan the event that GDB "wins" the race to open a file before the kernel 48650276Speter has created it. */ 48750276Speter 488166124Srafanstatic int 48950276Speteropen_with_retry (const char *pathname, int flags) 49050276Speter{ 491166124Srafan int retries_remaining, status; 49250276Speter 49350276Speter retries_remaining = 2; 494166124Srafan 49576726Speter while (1) 496166124Srafan { 497166124Srafan status = open (pathname, flags); 49897049Speter 49950276Speter if (status >= 0 || retries_remaining == 0) 500166124Srafan break; 50176726Speter else if (errno != EINTR && errno != EAGAIN) 50276726Speter { 50376726Speter retries_remaining--; 50476726Speter sleep (1); 50576726Speter } 50676726Speter } 50776726Speter 508166124Srafan return status; 50950276Speter} 51050276Speter 51197049Speter/* 51262449Speter * Function: open_procinfo_files 51350276Speter * 51450276Speter * Open the file descriptor for the process or LWP. 51550276Speter * ifdef NEW_PROC_API, we only open the control file descriptor; 51650276Speter * the others are opened lazily as needed. 51750276Speter * else (if not NEW_PROC_API), there is only one real 51850276Speter * file descriptor, but we keep multiple copies of it so that 51950276Speter * the code that uses them does not have to be #ifdef'd. 52050276Speter * 52150276Speter * Return: file descriptor, or zero for failure. 52250276Speter */ 523166124Srafan 524166124Srafanenum { FD_CTL, FD_STATUS, FD_AS }; 525166124Srafan 526166124Srafanstatic int 527166124Srafanopen_procinfo_files (procinfo *pi, int which) 528166124Srafan{ 52950276Speter#ifdef NEW_PROC_API 53050276Speter char tmp[MAX_PROC_NAME_SIZE]; 53150276Speter#endif 53250276Speter int fd; 53350276Speter 534166124Srafan /* 535178866Srafan * This function is getting ALMOST long enough to break up into several. 53697049Speter * Here is some rationale: 53776726Speter * 53850276Speter * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware): 53950276Speter * There are several file descriptors that may need to be open 54050276Speter * for any given process or LWP. The ones we're intereted in are: 54150276Speter * - control (ctl) write-only change the state 54250276Speter * - status (status) read-only query the state 54350276Speter * - address space (as) read/write access memory 54450276Speter * - map (map) read-only virtual addr map 54576726Speter * Most of these are opened lazily as they are needed. 54650276Speter * The pathnames for the 'files' for an LWP look slightly 54750276Speter * different from those of a first-class process: 548166124Srafan * Pathnames for a process (<proc-id>): 54950276Speter * /proc/<proc-id>/ctl 55050276Speter * /proc/<proc-id>/status 551166124Srafan * /proc/<proc-id>/as 55250276Speter * /proc/<proc-id>/map 55350276Speter * Pathnames for an LWP (lwp-id): 554166124Srafan * /proc/<proc-id>/lwp/<lwp-id>/lwpctl 55562449Speter * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus 55650276Speter * An LWP has no map or address space file descriptor, since 557166124Srafan * the memory map and address space are shared by all LWPs. 55850276Speter * 55950276Speter * Everyone else (Solaris 2.5, Irix, OSF) 560166124Srafan * There is only one file descriptor for each process or LWP. 56150276Speter * For convenience, we copy the same file descriptor into all 56250276Speter * three fields of the procinfo struct (ctl_fd, status_fd, and 563166124Srafan * as_fd, see NEW_PROC_API above) so that code that uses them 56450276Speter * doesn't need any #ifdef's. 56550276Speter * Pathname for all: 566166124Srafan * /proc/<proc-id> 56750276Speter * 56850276Speter * Solaris 2.5 LWP's: 569166124Srafan * Each LWP has an independent file descriptor, but these 57050276Speter * are not obtained via the 'open' system call like the rest: 57176726Speter * instead, they're obtained thru an ioctl call (PIOCOPENLWP) 57250276Speter * to the file descriptor of the parent process. 57350276Speter * 57450276Speter * OSF threads: 57562449Speter * These do not even have their own independent file descriptor. 57697049Speter * All operations are carried out on the file descriptor of the 57797049Speter * parent process. Therefore we just call open again for each 578184989Srafan * thread, getting a new handle for the same 'file'. 579184989Srafan */ 58076726Speter 58176726Speter#ifdef NEW_PROC_API 58276726Speter /* 58376726Speter * In this case, there are several different file descriptors that 58476726Speter * we might be asked to open. The control file descriptor will be 58576726Speter * opened early, but the others will be opened lazily as they are 58697049Speter * needed. 58776726Speter */ 58876726Speter 58997049Speter strcpy (tmp, pi->pathname); 59097049Speter switch (which) { /* which file descriptor to open? */ 59197049Speter case FD_CTL: 59297049Speter if (pi->tid) 59362449Speter strcat (tmp, "/lwpctl"); 59462449Speter else 59550276Speter strcat (tmp, "/ctl"); 59650276Speter fd = open_with_retry (tmp, O_WRONLY); 59750276Speter if (fd <= 0) 59850276Speter return 0; /* fail */ 59950276Speter pi->ctl_fd = fd; 60050276Speter break; 60150276Speter case FD_AS: 60250276Speter if (pi->tid) 60350276Speter return 0; /* there is no 'as' file descriptor for an lwp */ 60450276Speter strcat (tmp, "/as"); 60550276Speter fd = open_with_retry (tmp, O_RDWR); 60650276Speter if (fd <= 0) 60750276Speter return 0; /* fail */ 60850276Speter pi->as_fd = fd; 60950276Speter break; 61050276Speter case FD_STATUS: 61150276Speter if (pi->tid) 61250276Speter strcat (tmp, "/lwpstatus"); 61350276Speter else 61450276Speter strcat (tmp, "/status"); 61550276Speter fd = open_with_retry (tmp, O_RDONLY); 61650276Speter if (fd <= 0) 61750276Speter return 0; /* fail */ 61850276Speter pi->status_fd = fd; 61950276Speter break; 62050276Speter default: 62150276Speter return 0; /* unknown file descriptor */ 62250276Speter } 62350276Speter#else /* not NEW_PROC_API */ 62450276Speter /* 625166124Srafan * In this case, there is only one file descriptor for each procinfo 626166124Srafan * (ie. each process or LWP). In fact, only the file descriptor for 62797049Speter * the process can actually be opened by an 'open' system call. 62850276Speter * The ones for the LWPs have to be obtained thru an IOCTL call 629166124Srafan * on the process's file descriptor. 630166124Srafan * 631166124Srafan * For convenience, we copy each procinfo's single file descriptor 632166124Srafan * into all of the fields occupied by the several file descriptors 633166124Srafan * of the NEW_PROC_API implementation. That way, the code that uses 634166124Srafan * them can be written without ifdefs. 635166124Srafan */ 636166124Srafan 637166124Srafan 638166124Srafan#ifdef PIOCTSTATUS /* OSF */ 639166124Srafan /* Only one FD; just open it. */ 640166124Srafan if ((fd = open_with_retry (pi->pathname, O_RDWR)) == 0) 641166124Srafan return 0; 642166124Srafan#else /* Sol 2.5, Irix, other? */ 643166124Srafan if (pi->tid == 0) /* Master procinfo for the process */ 644166124Srafan { 645166124Srafan fd = open_with_retry (pi->pathname, O_RDWR); 646166124Srafan if (fd <= 0) 647166124Srafan return 0; /* fail */ 648166124Srafan } 649166124Srafan else /* LWP thread procinfo */ 65050276Speter { 651166124Srafan#ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */ 65276726Speter procinfo *process; 65376726Speter int lwpid = pi->tid; 65476726Speter 655166124Srafan /* Find the procinfo for the entire process. */ 65650276Speter if ((process = find_procinfo (pi->pid, 0)) == NULL) 65797049Speter return 0; /* fail */ 65862449Speter 65950276Speter /* Now obtain the file descriptor for the LWP. */ 66050276Speter if ((fd = ioctl (process->ctl_fd, PIOCOPENLWP, &lwpid)) <= 0) 66150276Speter return 0; /* fail */ 66250276Speter#else /* Irix, other? */ 66350276Speter return 0; /* Don't know how to open threads */ 66450276Speter#endif /* Sol 2.5 PIOCOPENLWP */ 66550276Speter } 66650276Speter#endif /* OSF PIOCTSTATUS */ 66750276Speter pi->ctl_fd = pi->as_fd = pi->status_fd = fd; 66850276Speter#endif /* NEW_PROC_API */ 66950276Speter 67050276Speter return 1; /* success */ 67150276Speter} 67250276Speter 67350276Speter/* 67450276Speter * Function: create_procinfo 67550276Speter * 67650276Speter * Allocate a data structure and link it into the procinfo list. 67750276Speter * (First tries to find a pre-existing one (FIXME: why?) 67850276Speter * 67950276Speter * Return: pointer to new procinfo struct. 68050276Speter */ 68150276Speter 68250276Speterstatic procinfo * 683166124Srafancreate_procinfo (int pid, int tid) 684166124Srafan{ 68550276Speter procinfo *pi, *parent; 686166124Srafan 68750276Speter if ((pi = find_procinfo (pid, tid))) 68850276Speter return pi; /* Already exists, nothing to do. */ 689166124Srafan 69050276Speter /* find parent before doing malloc, to save having to cleanup */ 69150276Speter if (tid != 0) 692166124Srafan parent = find_procinfo_or_die (pid, 0); /* FIXME: should I 69397049Speter create it if it 69462449Speter doesn't exist yet? */ 695166124Srafan 69650276Speter pi = (procinfo *) xmalloc (sizeof (procinfo)); 69750276Speter memset (pi, 0, sizeof (procinfo)); 698166124Srafan pi->pid = pid; 69950276Speter pi->tid = tid; 70050276Speter 701166124Srafan#ifdef DYNAMIC_SYSCALLS 70297049Speter load_syscalls (pi); 70350276Speter#endif 70450276Speter 70550276Speter pi->saved_entryset = sysset_t_alloc (pi); 70650276Speter pi->saved_exitset = sysset_t_alloc (pi); 70750276Speter 708166124Srafan /* Chain into list. */ 70950276Speter if (tid == 0) 71050276Speter { 711166124Srafan sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid); 71250276Speter pi->next = procinfo_list; 71350276Speter procinfo_list = pi; 714184989Srafan } 71550276Speter else 71650276Speter { 71750276Speter#ifdef NEW_PROC_API 71850276Speter sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid); 71950276Speter#else 720184989Srafan sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid); 72150276Speter#endif 72250276Speter pi->next = parent->thread_list; 723184989Srafan parent->thread_list = pi; 72450276Speter } 72550276Speter return pi; 726184989Srafan} 72750276Speter 72850276Speter/* 729184989Srafan * Function: close_procinfo_files 73050276Speter * 73197049Speter * Close all file descriptors associated with the procinfo 732166124Srafan */ 73350276Speter 73450276Speterstatic void 73550276Speterclose_procinfo_files (procinfo *pi) 73697049Speter{ 73797049Speter if (pi->ctl_fd > 0) 738166124Srafan close (pi->ctl_fd); 73950276Speter#ifdef NEW_PROC_API 74062449Speter if (pi->as_fd > 0) 741166124Srafan close (pi->as_fd); 74276726Speter if (pi->status_fd > 0) 74376726Speter close (pi->status_fd); 744166124Srafan#endif 74562449Speter pi->ctl_fd = pi->as_fd = pi->status_fd = 0; 74662449Speter} 747166124Srafan 748166124Srafan/* 749166124Srafan * Function: destroy_procinfo 750166124Srafan * 75176726Speter * Destructor function. Close, unlink and deallocate the object. 75276726Speter */ 753184989Srafan 754184989Srafanstatic void 755184989Srafandestroy_one_procinfo (procinfo **list, procinfo *pi) 756184989Srafan{ 757166124Srafan procinfo *ptr; 758184989Srafan 759184989Srafan /* Step one: unlink the procinfo from its list */ 760184989Srafan if (pi == *list) 761166124Srafan *list = pi->next; 76297049Speter else 76362449Speter for (ptr = *list; ptr; ptr = ptr->next) 764166124Srafan if (ptr->next == pi) 76550276Speter { 76650276Speter ptr->next = pi->next; 767166124Srafan break; 76862449Speter } 76962449Speter 770166124Srafan /* Step two: close any open file descriptors */ 77150276Speter close_procinfo_files (pi); 772166124Srafan 773166124Srafan /* Step three: free the memory. */ 774166124Srafan#ifdef DYNAMIC_SYSCALLS 775166124Srafan free_syscalls (pi); 776166124Srafan#endif 777166124Srafan xfree (pi->saved_entryset); 778166124Srafan xfree (pi->saved_exitset); 779166124Srafan xfree (pi); 780166124Srafan} 78150276Speter 78250276Speterstatic void 783166124Srafandestroy_procinfo (procinfo *pi) 784174993Srafan{ 78550276Speter procinfo *tmp; 786166124Srafan 787166124Srafan if (pi->tid != 0) /* destroy a thread procinfo */ 788184989Srafan { 789184989Srafan tmp = find_procinfo (pi->pid, 0); /* find the parent process */ 790166124Srafan destroy_one_procinfo (&tmp->thread_list, pi); 79176726Speter } 79276726Speter else /* destroy a process procinfo and all its threads */ 793166124Srafan { 794174993Srafan /* First destroy the children, if any; */ 795184989Srafan while (pi->thread_list != NULL) 796178866Srafan destroy_one_procinfo (&pi->thread_list, pi->thread_list); 797174993Srafan /* Then destroy the parent. Genocide!!! */ 798174993Srafan destroy_one_procinfo (&procinfo_list, pi); 799174993Srafan } 800174993Srafan} 801174993Srafan 802178866Srafanstatic void 803184989Srafando_destroy_procinfo_cleanup (void *pi) 804184989Srafan{ 805174993Srafan destroy_procinfo (pi); 806166124Srafan} 807166124Srafan 808166124Srafanenum { NOKILL, KILL }; 80962449Speter 81062449Speter/* 81162449Speter * Function: dead_procinfo 81262449Speter * 813166124Srafan * To be called on a non_recoverable error for a procinfo. 814166124Srafan * Prints error messages, optionally sends a SIGKILL to the process, 81562449Speter * then destroys the data structure. 81662449Speter */ 817166124Srafan 818166124Srafanstatic void 819166124Srafandead_procinfo (procinfo *pi, char *msg, int kill_p) 820166124Srafan{ 82150276Speter char procfile[80]; 82250276Speter 823166124Srafan if (pi->pathname) 82450276Speter { 82550276Speter print_sys_errmsg (pi->pathname, errno); 826166124Srafan } 82750276Speter else 828166124Srafan { 82950276Speter sprintf (procfile, "process %d", pi->pid); 830166124Srafan print_sys_errmsg (procfile, errno); 831166124Srafan } 832166124Srafan if (kill_p == KILL) 833166124Srafan kill (pi->pid, SIGKILL); 834166124Srafan 83562449Speter destroy_procinfo (pi); 83662449Speter error (msg); 837166124Srafan} 83876726Speter 839178866Srafan/* 840184989Srafan * Function: sysset_t_size 841184989Srafan * 842178866Srafan * Returns the (complete) size of a sysset_t struct. Normally, this 843178866Srafan * is just sizeof (syset_t), but in the case of Monterey/64, the actual 844178866Srafan * size of sysset_t isn't known until runtime. 845178866Srafan */ 846178866Srafan 847166124Srafanstatic int 848166124Srafansysset_t_size (procinfo * pi) 849166124Srafan{ 850166124Srafan#ifndef DYNAMIC_SYSCALLS 851166124Srafan return sizeof (sysset_t); 852166124Srafan#else 853166124Srafan return sizeof (sysset_t) - sizeof (uint64_t) 854166124Srafan + sizeof (uint64_t) * ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1)) 855166124Srafan / (8 * sizeof (uint64_t))); 856166124Srafan#endif 857166124Srafan} 858184989Srafan 859166124Srafan/* Function: sysset_t_alloc 86076726Speter 86197049Speter Allocate and (partially) initialize a sysset_t struct. */ 862166124Srafan 863166124Srafanstatic sysset_t * 864166124Srafansysset_t_alloc (procinfo * pi) 865166124Srafan{ 86676726Speter sysset_t *ret; 86776726Speter int size = sysset_t_size (pi); 868166124Srafan ret = xmalloc (size); 86976726Speter#ifdef DYNAMIC_SYSCALLS 87097049Speter ret->pr_size = (pi->num_syscalls + (8 * sizeof (uint64_t) - 1)) 87197049Speter / (8 * sizeof (uint64_t)); 87297049Speter#endif 87397049Speter return ret; 87497049Speter} 87597049Speter 87697049Speter#ifdef DYNAMIC_SYSCALLS 87797049Speter 87897049Speter/* Function: load_syscalls 87997049Speter 88097049Speter Extract syscall numbers and names from /proc/<pid>/sysent. Initialize 88197049Speter pi->num_syscalls with the number of syscalls and pi->syscall_names 88297049Speter with the names. (Certain numbers may be skipped in which case the 88397049Speter names for these numbers will be left as NULL.) */ 88476726Speter 885166124Srafan#define MAX_SYSCALL_NAME_LENGTH 256 886184989Srafan#define MAX_SYSCALLS 65536 887184989Srafan 888184989Srafanstatic void 889184989Srafanload_syscalls (procinfo *pi) 890166124Srafan{ 89176726Speter char pathname[MAX_PROC_NAME_SIZE]; 892166124Srafan int sysent_fd; 893166124Srafan prsysent_t header; 894166124Srafan prsyscall_t *syscalls; 895166124Srafan int i, size, maxcall; 896166124Srafan 897166124Srafan pi->num_syscalls = 0; 898166124Srafan pi->syscall_names = 0; 899166124Srafan 900166124Srafan /* Open the file descriptor for the sysent file */ 901166124Srafan sprintf (pathname, "/proc/%d/sysent", pi->pid); 902166124Srafan sysent_fd = open_with_retry (pathname, O_RDONLY); 903166124Srafan if (sysent_fd < 0) 904166124Srafan { 905166124Srafan error ("load_syscalls: Can't open /proc/%d/sysent", pi->pid); 906166124Srafan } 907166124Srafan 908166124Srafan size = sizeof header - sizeof (prsyscall_t); 909184989Srafan if (read (sysent_fd, &header, size) != size) 910184989Srafan { 911184989Srafan error ("load_syscalls: Error reading /proc/%d/sysent", pi->pid); 912184989Srafan } 913166124Srafan 914166124Srafan if (header.pr_nsyscalls == 0) 915166124Srafan { 916184989Srafan error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi->pid); 917184989Srafan } 91876726Speter 91997049Speter size = header.pr_nsyscalls * sizeof (prsyscall_t); 920166124Srafan syscalls = xmalloc (size); 92197049Speter 92297049Speter if (read (sysent_fd, syscalls, size) != size) 923166124Srafan { 92476726Speter xfree (syscalls); 92597049Speter error ("load_syscalls: Error reading /proc/%d/sysent", pi->pid); 92697049Speter } 92797049Speter 92897049Speter /* Find maximum syscall number. This may not be the same as 92997049Speter pr_nsyscalls since that value refers to the number of entries 93097049Speter in the table. (Also, the docs indicate that some system 93197049Speter call numbers may be skipped.) */ 93297049Speter 933184989Srafan maxcall = syscalls[0].pr_number; 93497049Speter 93597049Speter for (i = 1; i < header.pr_nsyscalls; i++) 93676726Speter if (syscalls[i].pr_number > maxcall 937166124Srafan && syscalls[i].pr_nameoff > 0 93876726Speter && syscalls[i].pr_number < MAX_SYSCALLS) 93976726Speter maxcall = syscalls[i].pr_number; 94076726Speter 94176726Speter pi->num_syscalls = maxcall+1; 94276726Speter pi->syscall_names = xmalloc (pi->num_syscalls * sizeof (char *)); 94376726Speter 94476726Speter for (i = 0; i < pi->num_syscalls; i++) 945166124Srafan pi->syscall_names[i] = NULL; 94676726Speter 94776726Speter /* Read the syscall names in */ 948166124Srafan for (i = 0; i < header.pr_nsyscalls; i++) 94976726Speter { 95076726Speter char namebuf[MAX_SYSCALL_NAME_LENGTH]; 951166124Srafan int nread; 952166124Srafan int callnum; 953184989Srafan 954166124Srafan if (syscalls[i].pr_number >= MAX_SYSCALLS 95576726Speter || syscalls[i].pr_number < 0 95676726Speter || syscalls[i].pr_nameoff <= 0 957166124Srafan || (lseek (sysent_fd, (off_t) syscalls[i].pr_nameoff, SEEK_SET) 95876726Speter != (off_t) syscalls[i].pr_nameoff)) 95976726Speter continue; 960166124Srafan 961166124Srafan nread = read (sysent_fd, namebuf, sizeof namebuf); 962166124Srafan if (nread <= 0) 963166124Srafan continue; 96476726Speter 96576726Speter callnum = syscalls[i].pr_number; 966166124Srafan 967178866Srafan if (pi->syscall_names[callnum] != NULL) 968184989Srafan { 969174993Srafan /* FIXME: Generate warning */ 97097049Speter continue; 97176726Speter } 97276726Speter 97376726Speter namebuf[nread-1] = '\0'; 97497049Speter size = strlen (namebuf) + 1; 97597049Speter pi->syscall_names[callnum] = xmalloc (size); 97697049Speter strncpy (pi->syscall_names[callnum], namebuf, size-1); 97776726Speter pi->syscall_names[callnum][size-1] = '\0'; 97897049Speter } 97976726Speter 98076726Speter close (sysent_fd); 981184989Srafan xfree (syscalls); 98297049Speter} 98376726Speter 98476726Speter/* Function: free_syscalls 98597049Speter 98697049Speter Free the space allocated for the syscall names from the procinfo 987166124Srafan structure. */ 98897049Speter 98997049Speterstatic void 99097049Speterfree_syscalls (procinfo *pi) 99197049Speter{ 992166124Srafan if (pi->syscall_names) 99350276Speter { 99476726Speter int i; 99597049Speter 99697049Speter for (i = 0; i < pi->num_syscalls; i++) 99797049Speter if (pi->syscall_names[i] != NULL) 99897049Speter xfree (pi->syscall_names[i]); 99997049Speter 100097049Speter xfree (pi->syscall_names); 100197049Speter pi->syscall_names = 0; 100297049Speter } 100397049Speter} 100497049Speter 100597049Speter/* Function: find_syscall 100697049Speter 100797049Speter Given a name, look up (and return) the corresponding syscall number. 100897049Speter If no match is found, return -1. */ 1009166124Srafan 101097049Speterstatic int 101197049Speterfind_syscall (procinfo *pi, char *name) 101297049Speter{ 101397049Speter int i; 101497049Speter for (i = 0; i < pi->num_syscalls; i++) 1015166124Srafan { 1016166124Srafan if (pi->syscall_names[i] && strcmp (name, pi->syscall_names[i]) == 0) 1017166124Srafan return i; 101850276Speter } 1019166124Srafan return -1; 1020184989Srafan} 1021184989Srafan#endif 1022184989Srafan 1023184989Srafan/* =================== END, STRUCT PROCINFO "MODULE" =================== */ 1024166124Srafan 1025166124Srafan/* =================== /proc "MODULE" =================== */ 1026166124Srafan 1027166124Srafan/* 1028166124Srafan * This "module" is the interface layer between the /proc system API 102976726Speter * and the gdb target vector functions. This layer consists of 103097049Speter * access functions that encapsulate each of the basic operations 103197049Speter * that we need to use from the /proc API. 103297049Speter * 103397049Speter * The main motivation for this layer is to hide the fact that 103450276Speter * there are two very different implementations of the /proc API. 1035166124Srafan * Rather than have a bunch of #ifdefs all thru the gdb target vector 103697049Speter * functions, we do our best to hide them all in here. 1037184989Srafan */ 1038184989Srafan 1039184989Srafanint proc_get_status (procinfo * pi); 104050276Speterlong proc_flags (procinfo * pi); 1041184989Srafanint proc_why (procinfo * pi); 104250276Speterint proc_what (procinfo * pi); 1043166124Srafanint proc_set_run_on_last_close (procinfo * pi); 1044166124Srafanint proc_unset_run_on_last_close (procinfo * pi); 1045166124Srafanint proc_set_inherit_on_fork (procinfo * pi); 1046166124Srafanint proc_unset_inherit_on_fork (procinfo * pi); 1047166124Srafanint proc_set_async (procinfo * pi); 1048166124Srafanint proc_unset_async (procinfo * pi); 1049166124Srafanint proc_stop_process (procinfo * pi); 1050166124Srafanint proc_trace_signal (procinfo * pi, int signo); 1051166124Srafanint proc_ignore_signal (procinfo * pi, int signo); 1052166124Srafanint proc_clear_current_fault (procinfo * pi); 1053166124Srafanint proc_set_current_signal (procinfo * pi, int signo); 1054166124Srafanint proc_clear_current_signal (procinfo * pi); 1055166124Srafanint proc_set_gregs (procinfo * pi); 1056166124Srafanint proc_set_fpregs (procinfo * pi); 1057166124Srafanint proc_wait_for_stop (procinfo * pi); 1058166124Srafanint proc_run_process (procinfo * pi, int step, int signo); 1059166124Srafanint proc_kill (procinfo * pi, int signo); 1060166124Srafanint proc_parent_pid (procinfo * pi); 1061166124Srafanint proc_get_nthreads (procinfo * pi); 1062166124Srafanint proc_get_current_thread (procinfo * pi); 1063166124Srafanint proc_set_held_signals (procinfo * pi, gdb_sigset_t * sighold); 106497049Speterint proc_set_traced_sysexit (procinfo * pi, sysset_t * sysset); 106562449Speterint proc_set_traced_sysentry (procinfo * pi, sysset_t * sysset); 106662449Speterint proc_set_traced_faults (procinfo * pi, fltset_t * fltset); 106762449Speterint proc_set_traced_signals (procinfo * pi, gdb_sigset_t * sigset); 106850276Speter 106962449Speterint proc_update_threads (procinfo * pi); 107050276Speterint proc_iterate_over_threads (procinfo * pi, 1071166124Srafan int (*func) (procinfo *, procinfo *, void *), 1072166124Srafan void *ptr); 1073184989Srafan 107497049Spetergdb_gregset_t *proc_get_gregs (procinfo * pi); 107597049Spetergdb_fpregset_t *proc_get_fpregs (procinfo * pi); 107697049Spetersysset_t *proc_get_traced_sysexit (procinfo * pi, sysset_t * save); 107797049Spetersysset_t *proc_get_traced_sysentry (procinfo * pi, sysset_t * save); 107897049Speterfltset_t *proc_get_traced_faults (procinfo * pi, fltset_t * save); 107997049Spetergdb_sigset_t *proc_get_traced_signals (procinfo * pi, gdb_sigset_t * save); 1080166124Srafangdb_sigset_t *proc_get_held_signals (procinfo * pi, gdb_sigset_t * save); 108197049Spetergdb_sigset_t *proc_get_pending_signals (procinfo * pi, gdb_sigset_t * save); 108250276Spetergdb_sigaction_t *proc_get_signal_actions (procinfo * pi, gdb_sigaction_t *save); 108350276Speter 108450276Spetervoid proc_warn (procinfo * pi, char *func, int line); 108550276Spetervoid proc_error (procinfo * pi, char *func, int line); 1086166124Srafan 1087166124Srafanvoid 1088166124Srafanproc_warn (procinfo *pi, char *func, int line) 108950276Speter{ 1090166124Srafan sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname); 109162449Speter print_sys_errmsg (errmsg, errno); 1092166124Srafan} 109362449Speter 109450276Spetervoid 109550276Speterproc_error (procinfo *pi, char *func, int line) 109650276Speter{ 109750276Speter sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname); 1098166124Srafan perror_with_name (errmsg); 109950276Speter} 110062449Speter 1101184989Srafan/* 1102184989Srafan * Function: proc_get_status 110350276Speter * 1104184989Srafan * Updates the status struct in the procinfo. 110550276Speter * There is a 'valid' flag, to let other functions know when 110650276Speter * this function needs to be called (so the status is only 1107166124Srafan * read when it is needed). The status file descriptor is 110850276Speter * also only opened when it is needed. 110950276Speter * 1110166124Srafan * Return: non-zero for success, zero for failure. 111150276Speter */ 111250276Speter 111350276Speterint 111450276Speterproc_get_status (procinfo *pi) 111550276Speter{ 111650276Speter /* Status file descriptor is opened "lazily" */ 1117166124Srafan if (pi->status_fd == 0 && 111850276Speter open_procinfo_files (pi, FD_STATUS) == 0) 111950276Speter { 112050276Speter pi->status_valid = 0; 1121166124Srafan return 0; 1122166124Srafan } 1123166124Srafan 1124166124Srafan#ifdef NEW_PROC_API 1125166124Srafan if (lseek (pi->status_fd, 0, SEEK_SET) < 0) 1126166124Srafan pi->status_valid = 0; /* fail */ 1127166124Srafan else 1128166124Srafan { 1129166124Srafan /* Sigh... I have to read a different data structure, 1130166124Srafan depending on whether this is a main process or an LWP. */ 113150276Speter if (pi->tid) 113250276Speter pi->status_valid = (read (pi->status_fd, 113350276Speter (char *) &pi->prstatus.pr_lwp, 113450276Speter sizeof (lwpstatus_t)) 1135166124Srafan == sizeof (lwpstatus_t)); 113650276Speter else 1137166124Srafan { 113850276Speter pi->status_valid = (read (pi->status_fd, 1139184989Srafan (char *) &pi->prstatus, 1140184989Srafan sizeof (gdb_prstatus_t)) 114197049Speter == sizeof (gdb_prstatus_t)); 114250276Speter#if 0 /*def UNIXWARE*/ 1143166124Srafan if (pi->status_valid && 114450276Speter (pi->prstatus.pr_lwp.pr_flags & PR_ISTOP) && 114550276Speter pi->prstatus.pr_lwp.pr_why == PR_REQUESTED) 1146166124Srafan /* Unixware peculiarity -- read the damn thing again! */ 114750276Speter pi->status_valid = (read (pi->status_fd, 114850276Speter (char *) &pi->prstatus, 1149166124Srafan sizeof (gdb_prstatus_t)) 115076726Speter == sizeof (gdb_prstatus_t)); 115150276Speter#endif /* UNIXWARE */ 1152166124Srafan } 115397049Speter } 115476726Speter#else /* ioctl method */ 1155166124Srafan#ifdef PIOCTSTATUS /* osf */ 115650276Speter if (pi->tid == 0) /* main process */ 115750276Speter { 1158166124Srafan /* Just read the danged status. Now isn't that simple? */ 115962449Speter pi->status_valid = 116062449Speter (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0); 1161166124Srafan } 116250276Speter else 116350276Speter { 1164166124Srafan int win; 116550276Speter struct { 116650276Speter long pr_count; 116750276Speter tid_t pr_error_thread; 116850276Speter struct prstatus status; 116950276Speter } thread_status; 117050276Speter 117150276Speter thread_status.pr_count = 1; 1172166124Srafan thread_status.status.pr_tid = pi->tid; 117397049Speter win = (ioctl (pi->status_fd, PIOCTSTATUS, &thread_status) >= 0); 1174184989Srafan if (win) 1175184989Srafan { 1176184989Srafan memcpy (&pi->prstatus, &thread_status.status, 117750276Speter sizeof (pi->prstatus)); 117850276Speter pi->status_valid = 1; 117950276Speter } 118050276Speter } 1181166124Srafan#else 118250276Speter /* Just read the danged status. Now isn't that simple? */ 118350276Speter pi->status_valid = (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0); 118450276Speter#endif 1185166124Srafan#endif 1186166124Srafan 1187166124Srafan if (pi->status_valid) 1188166124Srafan { 1189166124Srafan PROC_PRETTYFPRINT_STATUS (proc_flags (pi), 119097049Speter proc_why (pi), 119197049Speter proc_what (pi), 119297049Speter proc_get_current_thread (pi)); 1193166124Srafan } 119450276Speter 119550276Speter /* The status struct includes general regs, so mark them valid too */ 1196166124Srafan pi->gregs_valid = pi->status_valid; 119776726Speter#ifdef NEW_PROC_API 119850276Speter /* In the read/write multiple-fd model, 1199166124Srafan the status struct includes the fp regs too, so mark them valid too */ 120062449Speter pi->fpregs_valid = pi->status_valid; 120150276Speter#endif 120250276Speter return pi->status_valid; /* True if success, false if failure. */ 120350276Speter} 120450276Speter 120550276Speter/* 1206184989Srafan * Function: proc_flags 120750276Speter * 120850276Speter * returns the process flags (pr_flags field). 1209166124Srafan */ 121050276Speter 121150276Speterlong 1212166124Srafanproc_flags (procinfo *pi) 121350276Speter{ 121450276Speter if (!pi->status_valid) 1215166124Srafan if (!proc_get_status (pi)) 1216178866Srafan return 0; /* FIXME: not a good failure value (but what is?) */ 1217178866Srafan 1218178866Srafan#ifdef NEW_PROC_API 121962449Speter# ifdef UNIXWARE 122062449Speter /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in 1221166124Srafan pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t. 122262449Speter The two sets of flags don't overlap. */ 122362449Speter return pi->prstatus.pr_flags | pi->prstatus.pr_lwp.pr_flags; 1224166124Srafan# else 1225166124Srafan return pi->prstatus.pr_lwp.pr_flags; 1226166124Srafan# endif 1227166124Srafan#else 1228174993Srafan return pi->prstatus.pr_flags; 1229174993Srafan#endif 1230174993Srafan} 1231174993Srafan 1232174993Srafan/* 1233174993Srafan * Function: proc_why 1234174993Srafan * 1235174993Srafan * returns the pr_why field (why the process stopped). 1236174993Srafan */ 123762449Speter 123862449Speterint 1239166124Srafanproc_why (procinfo *pi) 124062449Speter{ 124162449Speter if (!pi->status_valid) 1242166124Srafan if (!proc_get_status (pi)) 124362449Speter return 0; /* FIXME: not a good failure value (but what is?) */ 1244166124Srafan 1245166124Srafan#ifdef NEW_PROC_API 1246166124Srafan return pi->prstatus.pr_lwp.pr_why; 1247166124Srafan#else 1248166124Srafan return pi->prstatus.pr_why; 1249166124Srafan#endif 125062449Speter} 125197049Speter 125297049Speter/* 1253184989Srafan * Function: proc_what 1254184989Srafan * 125562449Speter * returns the pr_what field (details of why the process stopped). 125697049Speter */ 1257166124Srafan 125862449Speterint 125962449Speterproc_what (procinfo *pi) 1260166124Srafan{ 1261166124Srafan if (!pi->status_valid) 1262166124Srafan if (!proc_get_status (pi)) 1263166124Srafan return 0; /* FIXME: not a good failure value (but what is?) */ 1264166124Srafan 126562449Speter#ifdef NEW_PROC_API 1266166124Srafan return pi->prstatus.pr_lwp.pr_what; 126776726Speter#else 126876726Speter return pi->prstatus.pr_what; 1269166124Srafan#endif 127062449Speter} 127162449Speter 1272166124Srafan#ifndef PIOCSSPCACT /* The following is not supported on OSF. */ 127362449Speter/* 127462449Speter * Function: proc_nsysarg 1275166124Srafan * 127676726Speter * returns the pr_nsysarg field (number of args to the current syscall). 127776726Speter */ 127876726Speter 127976726Speterint 128076726Speterproc_nsysarg (procinfo *pi) 128176726Speter{ 128276726Speter if (!pi->status_valid) 128376726Speter if (!proc_get_status (pi)) 128476726Speter return 0; 128576726Speter 1286166124Srafan#ifdef NEW_PROC_API 128776726Speter return pi->prstatus.pr_lwp.pr_nsysarg; 128876726Speter#else 1289166124Srafan return pi->prstatus.pr_nsysarg; 129076726Speter#endif 129176726Speter} 1292166124Srafan 129376726Speter/* 129476726Speter * Function: proc_sysargs 1295166124Srafan * 129676726Speter * returns the pr_sysarg field (pointer to the arguments of current syscall). 129776726Speter */ 1298166124Srafan 129976726Speterlong * 130076726Speterproc_sysargs (procinfo *pi) 1301166124Srafan{ 130276726Speter if (!pi->status_valid) 130376726Speter if (!proc_get_status (pi)) 1304166124Srafan return NULL; 1305166124Srafan 1306184989Srafan#ifdef NEW_PROC_API 1307166124Srafan return (long *) &pi->prstatus.pr_lwp.pr_sysarg; 1308166124Srafan#else 1309166124Srafan return (long *) &pi->prstatus.pr_sysarg; 1310166124Srafan#endif 1311166124Srafan} 1312184989Srafan 1313166124Srafan/* 1314166124Srafan * Function: proc_syscall 1315166124Srafan * 1316166124Srafan * returns the pr_syscall field (id of current syscall if we are in one). 1317166124Srafan */ 1318166124Srafan 1319166124Srafanint 1320166124Srafanproc_syscall (procinfo *pi) 1321166124Srafan{ 1322166124Srafan if (!pi->status_valid) 1323166124Srafan if (!proc_get_status (pi)) 1324166124Srafan return 0; 1325166124Srafan 1326166124Srafan#ifdef NEW_PROC_API 132750276Speter return pi->prstatus.pr_lwp.pr_syscall; 132850276Speter#else 132950276Speter return pi->prstatus.pr_syscall; 133050276Speter#endif 133150276Speter} 133297049Speter#endif /* PIOCSSPCACT */ 133362449Speter 133450276Speter/* 133550276Speter * Function: proc_cursig: 133650276Speter * 133750276Speter * returns the pr_cursig field (current signal). 133850276Speter */ 133950276Speter 134050276Speterlong 134150276Speterproc_cursig (struct procinfo *pi) 134250276Speter{ 134350276Speter if (!pi->status_valid) 134450276Speter if (!proc_get_status (pi)) 134550276Speter return 0; /* FIXME: not a good failure value (but what is?) */ 134650276Speter 134750276Speter#ifdef NEW_PROC_API 134850276Speter return pi->prstatus.pr_lwp.pr_cursig; 134950276Speter#else 135050276Speter return pi->prstatus.pr_cursig; 135150276Speter#endif 135250276Speter} 135350276Speter 135450276Speter/* 135550276Speter * Function: proc_modify_flag 135650276Speter * 135750276Speter * === I appologize for the messiness of this function. 1358166124Srafan * === This is an area where the different versions of 135950276Speter * === /proc are more inconsistent than usual. MVS 136050276Speter * 136150276Speter * Set or reset any of the following process flags: 136250276Speter * PR_FORK -- forked child will inherit trace flags 136350276Speter * PR_RLC -- traced process runs when last /proc file closed. 136450276Speter * PR_KLC -- traced process is killed when last /proc file closed. 136550276Speter * PR_ASYNC -- LWP's get to run/stop independently. 136650276Speter * 136750276Speter * There are three methods for doing this function: 136850276Speter * 1) Newest: read/write [PCSET/PCRESET/PCUNSET] 136950276Speter * [Sol6, Sol7, UW] 137050276Speter * 2) Middle: PIOCSET/PIOCRESET 137162449Speter * [Irix, Sol5] 137262449Speter * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC 137362449Speter * [OSF, Sol5] 137462449Speter * 137550276Speter * Note: Irix does not define PR_ASYNC. 137650276Speter * Note: OSF does not define PR_KLC. 137750276Speter * Note: OSF is the only one that can ONLY use the oldest method. 137850276Speter * 137950276Speter * Arguments: 138050276Speter * pi -- the procinfo 138150276Speter * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC 138250276Speter * mode -- 1 for set, 0 for reset. 138350276Speter * 138450276Speter * Returns non-zero for success, zero for failure. 138550276Speter */ 138650276Speter 138750276Speterenum { FLAG_RESET, FLAG_SET }; 138850276Speter 138950276Speterstatic int 139050276Speterproc_modify_flag (procinfo *pi, long flag, long mode) 139150276Speter{ 139250276Speter long win = 0; /* default to fail */ 139350276Speter 139450276Speter /* 139550276Speter * These operations affect the process as a whole, and applying 139650276Speter * them to an individual LWP has the same meaning as applying them 139750276Speter * to the main process. Therefore, if we're ever called with a 139850276Speter * pointer to an LWP's procinfo, let's substitute the process's 139950276Speter * procinfo and avoid opening the LWP's file descriptor 140050276Speter * unnecessarily. 140150276Speter */ 140250276Speter 140350276Speter if (pi->pid != 0) 140450276Speter pi = find_procinfo_or_die (pi->pid, 0); 140550276Speter 140650276Speter#ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */ 140750276Speter /* First normalize the PCUNSET/PCRESET command opcode 140850276Speter (which for no obvious reason has a different definition 140950276Speter from one operating system to the next...) */ 141050276Speter#ifdef PCUNSET 141150276Speter#define GDBRESET PCUNSET 141250276Speter#else 141350276Speter#ifdef PCRESET 141450276Speter#define GDBRESET PCRESET 141550276Speter#endif 141650276Speter#endif 141750276Speter { 141850276Speter procfs_ctl_t arg[2]; 141976726Speter 142076726Speter if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC) */ 142176726Speter arg[0] = PCSET; 142276726Speter else /* Reset the flag */ 142376726Speter arg[0] = GDBRESET; 142476726Speter 142576726Speter arg[1] = flag; 142676726Speter win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); 142776726Speter } 142876726Speter#else 142976726Speter#ifdef PIOCSET /* Irix/Sol5 method */ 143076726Speter if (mode == FLAG_SET) /* Set the flag (hopefully RLC, FORK, or ASYNC) */ 143176726Speter { 143276726Speter win = (ioctl (pi->ctl_fd, PIOCSET, &flag) >= 0); 143376726Speter } 143476726Speter else /* Reset the flag */ 143576726Speter { 143676726Speter win = (ioctl (pi->ctl_fd, PIOCRESET, &flag) >= 0); 143750276Speter } 143850276Speter 143950276Speter#else 144050276Speter#ifdef PIOCSRLC /* Oldest method: OSF */ 144150276Speter switch (flag) { 144250276Speter case PR_RLC: 144350276Speter if (mode == FLAG_SET) /* Set run-on-last-close */ 144450276Speter { 144550276Speter win = (ioctl (pi->ctl_fd, PIOCSRLC, NULL) >= 0); 144650276Speter } 1447166124Srafan else /* Clear run-on-last-close */ 1448166124Srafan { 144950276Speter win = (ioctl (pi->ctl_fd, PIOCRRLC, NULL) >= 0); 145050276Speter } 145150276Speter break; 1452166124Srafan case PR_FORK: 145350276Speter if (mode == FLAG_SET) /* Set inherit-on-fork */ 145450276Speter { 145550276Speter win = (ioctl (pi->ctl_fd, PIOCSFORK, NULL) >= 0); 145650276Speter } 145750276Speter else /* Clear inherit-on-fork */ 145850276Speter { 145950276Speter win = (ioctl (pi->ctl_fd, PIOCRFORK, NULL) >= 0); 146050276Speter } 1461166124Srafan break; 146250276Speter default: 146350276Speter win = 0; /* fail -- unknown flag (can't do PR_ASYNC) */ 146450276Speter break; 146550276Speter } 146650276Speter#endif 1467166124Srafan#endif 146850276Speter#endif 146950276Speter#undef GDBRESET 1470166124Srafan /* The above operation renders the procinfo's cached pstatus obsolete. */ 147150276Speter pi->status_valid = 0; 147250276Speter 1473166124Srafan if (!win) 147450276Speter warning ("procfs: modify_flag failed to turn %s %s", 147550276Speter flag == PR_FORK ? "PR_FORK" : 1476166124Srafan flag == PR_RLC ? "PR_RLC" : 147750276Speter#ifdef PR_ASYNC 147850276Speter flag == PR_ASYNC ? "PR_ASYNC" : 147950276Speter#endif 148062449Speter#ifdef PR_KLC 148162449Speter flag == PR_KLC ? "PR_KLC" : 148250276Speter#endif 148376726Speter "<unknown flag>", 148476726Speter mode == FLAG_RESET ? "off" : "on"); 148576726Speter 148662449Speter return win; 1487184989Srafan} 1488166124Srafan 1489184989Srafan/* 149062449Speter * Function: proc_set_run_on_last_close 149162449Speter * 149262449Speter * Set the run_on_last_close flag. 149362449Speter * Process with all threads will become runnable 149462449Speter * when debugger closes all /proc fds. 149562449Speter * 149676726Speter * Returns non-zero for success, zero for failure. 149762449Speter */ 149862449Speter 149962449Speterint 150062449Speterproc_set_run_on_last_close (procinfo *pi) 150162449Speter{ 150262449Speter return proc_modify_flag (pi, PR_RLC, FLAG_SET); 150362449Speter} 150462449Speter 150562449Speter/* 150662449Speter * Function: proc_unset_run_on_last_close 150762449Speter * 150862449Speter * Reset the run_on_last_close flag. 150962449Speter * Process will NOT become runnable 151062449Speter * when debugger closes its file handles. 151162449Speter * 151262449Speter * Returns non-zero for success, zero for failure. 151362449Speter */ 151462449Speter 151562449Speterint 151662449Speterproc_unset_run_on_last_close (procinfo *pi) 151762449Speter{ 151862449Speter return proc_modify_flag (pi, PR_RLC, FLAG_RESET); 151950276Speter} 152062449Speter 152162449Speter#ifdef PR_KLC 152262449Speter/* 152376726Speter * Function: proc_set_kill_on_last_close 152462449Speter * 152562449Speter * Set the kill_on_last_close flag. 152662449Speter * Process with all threads will be killed when debugger 1527 * closes all /proc fds (or debugger exits or dies). 1528 * 1529 * Returns non-zero for success, zero for failure. 1530 */ 1531 1532int 1533proc_set_kill_on_last_close (procinfo *pi) 1534{ 1535 return proc_modify_flag (pi, PR_KLC, FLAG_SET); 1536} 1537 1538/* 1539 * Function: proc_unset_kill_on_last_close 1540 * 1541 * Reset the kill_on_last_close flag. 1542 * Process will NOT be killed when debugger 1543 * closes its file handles (or exits or dies). 1544 * 1545 * Returns non-zero for success, zero for failure. 1546 */ 1547 1548int 1549proc_unset_kill_on_last_close (procinfo *pi) 1550{ 1551 return proc_modify_flag (pi, PR_KLC, FLAG_RESET); 1552} 1553#endif /* PR_KLC */ 1554 1555/* 1556 * Function: proc_set_inherit_on_fork 1557 * 1558 * Set inherit_on_fork flag. 1559 * If the process forks a child while we are registered for events 1560 * in the parent, then we will also recieve events from the child. 1561 * 1562 * Returns non-zero for success, zero for failure. 1563 */ 1564 1565int 1566proc_set_inherit_on_fork (procinfo *pi) 1567{ 1568 return proc_modify_flag (pi, PR_FORK, FLAG_SET); 1569} 1570 1571/* 1572 * Function: proc_unset_inherit_on_fork 1573 * 1574 * Reset inherit_on_fork flag. 1575 * If the process forks a child while we are registered for events 1576 * in the parent, then we will NOT recieve events from the child. 1577 * 1578 * Returns non-zero for success, zero for failure. 1579 */ 1580 1581int 1582proc_unset_inherit_on_fork (procinfo *pi) 1583{ 1584 return proc_modify_flag (pi, PR_FORK, FLAG_RESET); 1585} 1586 1587#ifdef PR_ASYNC 1588/* 1589 * Function: proc_set_async 1590 * 1591 * Set PR_ASYNC flag. 1592 * If one LWP stops because of a debug event (signal etc.), 1593 * the remaining LWPs will continue to run. 1594 * 1595 * Returns non-zero for success, zero for failure. 1596 */ 1597 1598int 1599proc_set_async (procinfo *pi) 1600{ 1601 return proc_modify_flag (pi, PR_ASYNC, FLAG_SET); 1602} 1603 1604/* 1605 * Function: proc_unset_async 1606 * 1607 * Reset PR_ASYNC flag. 1608 * If one LWP stops because of a debug event (signal etc.), 1609 * then all other LWPs will stop as well. 1610 * 1611 * Returns non-zero for success, zero for failure. 1612 */ 1613 1614int 1615proc_unset_async (procinfo *pi) 1616{ 1617 return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET); 1618} 1619#endif /* PR_ASYNC */ 1620 1621/* 1622 * Function: proc_stop_process 1623 * 1624 * Request the process/LWP to stop. Does not wait. 1625 * Returns non-zero for success, zero for failure. 1626 */ 1627 1628int 1629proc_stop_process (procinfo *pi) 1630{ 1631 int win; 1632 1633 /* 1634 * We might conceivably apply this operation to an LWP, and 1635 * the LWP's ctl file descriptor might not be open. 1636 */ 1637 1638 if (pi->ctl_fd == 0 && 1639 open_procinfo_files (pi, FD_CTL) == 0) 1640 return 0; 1641 else 1642 { 1643#ifdef NEW_PROC_API 1644 procfs_ctl_t cmd = PCSTOP; 1645 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); 1646#else /* ioctl method */ 1647 win = (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) >= 0); 1648 /* Note: the call also reads the prstatus. */ 1649 if (win) 1650 { 1651 pi->status_valid = 1; 1652 PROC_PRETTYFPRINT_STATUS (proc_flags (pi), 1653 proc_why (pi), 1654 proc_what (pi), 1655 proc_get_current_thread (pi)); 1656 } 1657#endif 1658 } 1659 1660 return win; 1661} 1662 1663/* 1664 * Function: proc_wait_for_stop 1665 * 1666 * Wait for the process or LWP to stop (block until it does). 1667 * Returns non-zero for success, zero for failure. 1668 */ 1669 1670int 1671proc_wait_for_stop (procinfo *pi) 1672{ 1673 int win; 1674 1675 /* 1676 * We should never have to apply this operation to any procinfo 1677 * except the one for the main process. If that ever changes 1678 * for any reason, then take out the following clause and 1679 * replace it with one that makes sure the ctl_fd is open. 1680 */ 1681 1682 if (pi->tid != 0) 1683 pi = find_procinfo_or_die (pi->pid, 0); 1684 1685#ifdef NEW_PROC_API 1686 { 1687 procfs_ctl_t cmd = PCWSTOP; 1688 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); 1689 /* We been runnin' and we stopped -- need to update status. */ 1690 pi->status_valid = 0; 1691 } 1692#else /* ioctl method */ 1693 win = (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) >= 0); 1694 /* Above call also refreshes the prstatus. */ 1695 if (win) 1696 { 1697 pi->status_valid = 1; 1698 PROC_PRETTYFPRINT_STATUS (proc_flags (pi), 1699 proc_why (pi), 1700 proc_what (pi), 1701 proc_get_current_thread (pi)); 1702 } 1703#endif 1704 1705 return win; 1706} 1707 1708/* 1709 * Function: proc_run_process 1710 * 1711 * Make the process or LWP runnable. 1712 * Options (not all are implemented): 1713 * - single-step 1714 * - clear current fault 1715 * - clear current signal 1716 * - abort the current system call 1717 * - stop as soon as finished with system call 1718 * - (ioctl): set traced signal set 1719 * - (ioctl): set held signal set 1720 * - (ioctl): set traced fault set 1721 * - (ioctl): set start pc (vaddr) 1722 * Always clear the current fault. 1723 * Clear the current signal if 'signo' is zero. 1724 * 1725 * Arguments: 1726 * pi the process or LWP to operate on. 1727 * step if true, set the process or LWP to trap after one instr. 1728 * signo if zero, clear the current signal if any. 1729 * if non-zero, set the current signal to this one. 1730 * 1731 * Returns non-zero for success, zero for failure. 1732 */ 1733 1734int 1735proc_run_process (procinfo *pi, int step, int signo) 1736{ 1737 int win; 1738 int runflags; 1739 1740 /* 1741 * We will probably have to apply this operation to individual threads, 1742 * so make sure the control file descriptor is open. 1743 */ 1744 1745 if (pi->ctl_fd == 0 && 1746 open_procinfo_files (pi, FD_CTL) == 0) 1747 { 1748 return 0; 1749 } 1750 1751 runflags = PRCFAULT; /* always clear current fault */ 1752 if (step) 1753 runflags |= PRSTEP; 1754 if (signo == 0) 1755 runflags |= PRCSIG; 1756 else if (signo != -1) /* -1 means do nothing W.R.T. signals */ 1757 proc_set_current_signal (pi, signo); 1758 1759#ifdef NEW_PROC_API 1760 { 1761 procfs_ctl_t cmd[2]; 1762 1763 cmd[0] = PCRUN; 1764 cmd[1] = runflags; 1765 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); 1766 } 1767#else /* ioctl method */ 1768 { 1769 prrun_t prrun; 1770 1771 memset (&prrun, 0, sizeof (prrun)); 1772 prrun.pr_flags = runflags; 1773 win = (ioctl (pi->ctl_fd, PIOCRUN, &prrun) >= 0); 1774 } 1775#endif 1776 1777 return win; 1778} 1779 1780/* 1781 * Function: proc_set_traced_signals 1782 * 1783 * Register to trace signals in the process or LWP. 1784 * Returns non-zero for success, zero for failure. 1785 */ 1786 1787int 1788proc_set_traced_signals (procinfo *pi, gdb_sigset_t *sigset) 1789{ 1790 int win; 1791 1792 /* 1793 * We should never have to apply this operation to any procinfo 1794 * except the one for the main process. If that ever changes 1795 * for any reason, then take out the following clause and 1796 * replace it with one that makes sure the ctl_fd is open. 1797 */ 1798 1799 if (pi->tid != 0) 1800 pi = find_procinfo_or_die (pi->pid, 0); 1801 1802#ifdef NEW_PROC_API 1803 { 1804 struct { 1805 procfs_ctl_t cmd; 1806 /* Use char array to avoid alignment issues. */ 1807 char sigset[sizeof (gdb_sigset_t)]; 1808 } arg; 1809 1810 arg.cmd = PCSTRACE; 1811 memcpy (&arg.sigset, sigset, sizeof (gdb_sigset_t)); 1812 1813 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg)); 1814 } 1815#else /* ioctl method */ 1816 win = (ioctl (pi->ctl_fd, PIOCSTRACE, sigset) >= 0); 1817#endif 1818 /* The above operation renders the procinfo's cached pstatus obsolete. */ 1819 pi->status_valid = 0; 1820 1821 if (!win) 1822 warning ("procfs: set_traced_signals failed"); 1823 return win; 1824} 1825 1826/* 1827 * Function: proc_set_traced_faults 1828 * 1829 * Register to trace hardware faults in the process or LWP. 1830 * Returns non-zero for success, zero for failure. 1831 */ 1832 1833int 1834proc_set_traced_faults (procinfo *pi, fltset_t *fltset) 1835{ 1836 int win; 1837 1838 /* 1839 * We should never have to apply this operation to any procinfo 1840 * except the one for the main process. If that ever changes 1841 * for any reason, then take out the following clause and 1842 * replace it with one that makes sure the ctl_fd is open. 1843 */ 1844 1845 if (pi->tid != 0) 1846 pi = find_procinfo_or_die (pi->pid, 0); 1847 1848#ifdef NEW_PROC_API 1849 { 1850 struct { 1851 procfs_ctl_t cmd; 1852 /* Use char array to avoid alignment issues. */ 1853 char fltset[sizeof (fltset_t)]; 1854 } arg; 1855 1856 arg.cmd = PCSFAULT; 1857 memcpy (&arg.fltset, fltset, sizeof (fltset_t)); 1858 1859 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg)); 1860 } 1861#else /* ioctl method */ 1862 win = (ioctl (pi->ctl_fd, PIOCSFAULT, fltset) >= 0); 1863#endif 1864 /* The above operation renders the procinfo's cached pstatus obsolete. */ 1865 pi->status_valid = 0; 1866 1867 return win; 1868} 1869 1870/* 1871 * Function: proc_set_traced_sysentry 1872 * 1873 * Register to trace entry to system calls in the process or LWP. 1874 * Returns non-zero for success, zero for failure. 1875 */ 1876 1877int 1878proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset) 1879{ 1880 int win; 1881 1882 /* 1883 * We should never have to apply this operation to any procinfo 1884 * except the one for the main process. If that ever changes 1885 * for any reason, then take out the following clause and 1886 * replace it with one that makes sure the ctl_fd is open. 1887 */ 1888 1889 if (pi->tid != 0) 1890 pi = find_procinfo_or_die (pi->pid, 0); 1891 1892#ifdef NEW_PROC_API 1893 { 1894 struct gdb_proc_ctl_pcsentry { 1895 procfs_ctl_t cmd; 1896 /* Use char array to avoid alignment issues. */ 1897 char sysset[sizeof (sysset_t)]; 1898 } *argp; 1899 int argp_size = sizeof (struct gdb_proc_ctl_pcsentry) 1900 - sizeof (sysset_t) 1901 + sysset_t_size (pi); 1902 1903 argp = xmalloc (argp_size); 1904 1905 argp->cmd = PCSENTRY; 1906 memcpy (&argp->sysset, sysset, sysset_t_size (pi)); 1907 1908 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size); 1909 xfree (argp); 1910 } 1911#else /* ioctl method */ 1912 win = (ioctl (pi->ctl_fd, PIOCSENTRY, sysset) >= 0); 1913#endif 1914 /* The above operation renders the procinfo's cached pstatus obsolete. */ 1915 pi->status_valid = 0; 1916 1917 return win; 1918} 1919 1920/* 1921 * Function: proc_set_traced_sysexit 1922 * 1923 * Register to trace exit from system calls in the process or LWP. 1924 * Returns non-zero for success, zero for failure. 1925 */ 1926 1927int 1928proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset) 1929{ 1930 int win; 1931 1932 /* 1933 * We should never have to apply this operation to any procinfo 1934 * except the one for the main process. If that ever changes 1935 * for any reason, then take out the following clause and 1936 * replace it with one that makes sure the ctl_fd is open. 1937 */ 1938 1939 if (pi->tid != 0) 1940 pi = find_procinfo_or_die (pi->pid, 0); 1941 1942#ifdef NEW_PROC_API 1943 { 1944 struct gdb_proc_ctl_pcsexit { 1945 procfs_ctl_t cmd; 1946 /* Use char array to avoid alignment issues. */ 1947 char sysset[sizeof (sysset_t)]; 1948 } *argp; 1949 int argp_size = sizeof (struct gdb_proc_ctl_pcsexit) 1950 - sizeof (sysset_t) 1951 + sysset_t_size (pi); 1952 1953 argp = xmalloc (argp_size); 1954 1955 argp->cmd = PCSEXIT; 1956 memcpy (&argp->sysset, sysset, sysset_t_size (pi)); 1957 1958 win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size); 1959 xfree (argp); 1960 } 1961#else /* ioctl method */ 1962 win = (ioctl (pi->ctl_fd, PIOCSEXIT, sysset) >= 0); 1963#endif 1964 /* The above operation renders the procinfo's cached pstatus obsolete. */ 1965 pi->status_valid = 0; 1966 1967 return win; 1968} 1969 1970/* 1971 * Function: proc_set_held_signals 1972 * 1973 * Specify the set of blocked / held signals in the process or LWP. 1974 * Returns non-zero for success, zero for failure. 1975 */ 1976 1977int 1978proc_set_held_signals (procinfo *pi, gdb_sigset_t *sighold) 1979{ 1980 int win; 1981 1982 /* 1983 * We should never have to apply this operation to any procinfo 1984 * except the one for the main process. If that ever changes 1985 * for any reason, then take out the following clause and 1986 * replace it with one that makes sure the ctl_fd is open. 1987 */ 1988 1989 if (pi->tid != 0) 1990 pi = find_procinfo_or_die (pi->pid, 0); 1991 1992#ifdef NEW_PROC_API 1993 { 1994 struct { 1995 procfs_ctl_t cmd; 1996 /* Use char array to avoid alignment issues. */ 1997 char hold[sizeof (gdb_sigset_t)]; 1998 } arg; 1999 2000 arg.cmd = PCSHOLD; 2001 memcpy (&arg.hold, sighold, sizeof (gdb_sigset_t)); 2002 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); 2003 } 2004#else 2005 win = (ioctl (pi->ctl_fd, PIOCSHOLD, sighold) >= 0); 2006#endif 2007 /* The above operation renders the procinfo's cached pstatus obsolete. */ 2008 pi->status_valid = 0; 2009 2010 return win; 2011} 2012 2013/* 2014 * Function: proc_get_pending_signals 2015 * 2016 * returns the set of signals that are pending in the process or LWP. 2017 * Will also copy the sigset if 'save' is non-zero. 2018 */ 2019 2020gdb_sigset_t * 2021proc_get_pending_signals (procinfo *pi, gdb_sigset_t *save) 2022{ 2023 gdb_sigset_t *ret = NULL; 2024 2025 /* 2026 * We should never have to apply this operation to any procinfo 2027 * except the one for the main process. If that ever changes 2028 * for any reason, then take out the following clause and 2029 * replace it with one that makes sure the ctl_fd is open. 2030 */ 2031 2032 if (pi->tid != 0) 2033 pi = find_procinfo_or_die (pi->pid, 0); 2034 2035 if (!pi->status_valid) 2036 if (!proc_get_status (pi)) 2037 return NULL; 2038 2039#ifdef NEW_PROC_API 2040 ret = &pi->prstatus.pr_lwp.pr_lwppend; 2041#else 2042 ret = &pi->prstatus.pr_sigpend; 2043#endif 2044 if (save && ret) 2045 memcpy (save, ret, sizeof (gdb_sigset_t)); 2046 2047 return ret; 2048} 2049 2050/* 2051 * Function: proc_get_signal_actions 2052 * 2053 * returns the set of signal actions. 2054 * Will also copy the sigactionset if 'save' is non-zero. 2055 */ 2056 2057gdb_sigaction_t * 2058proc_get_signal_actions (procinfo *pi, gdb_sigaction_t *save) 2059{ 2060 gdb_sigaction_t *ret = NULL; 2061 2062 /* 2063 * We should never have to apply this operation to any procinfo 2064 * except the one for the main process. If that ever changes 2065 * for any reason, then take out the following clause and 2066 * replace it with one that makes sure the ctl_fd is open. 2067 */ 2068 2069 if (pi->tid != 0) 2070 pi = find_procinfo_or_die (pi->pid, 0); 2071 2072 if (!pi->status_valid) 2073 if (!proc_get_status (pi)) 2074 return NULL; 2075 2076#ifdef NEW_PROC_API 2077 ret = &pi->prstatus.pr_lwp.pr_action; 2078#else 2079 ret = &pi->prstatus.pr_action; 2080#endif 2081 if (save && ret) 2082 memcpy (save, ret, sizeof (gdb_sigaction_t)); 2083 2084 return ret; 2085} 2086 2087/* 2088 * Function: proc_get_held_signals 2089 * 2090 * returns the set of signals that are held / blocked. 2091 * Will also copy the sigset if 'save' is non-zero. 2092 */ 2093 2094gdb_sigset_t * 2095proc_get_held_signals (procinfo *pi, gdb_sigset_t *save) 2096{ 2097 gdb_sigset_t *ret = NULL; 2098 2099 /* 2100 * We should never have to apply this operation to any procinfo 2101 * except the one for the main process. If that ever changes 2102 * for any reason, then take out the following clause and 2103 * replace it with one that makes sure the ctl_fd is open. 2104 */ 2105 2106 if (pi->tid != 0) 2107 pi = find_procinfo_or_die (pi->pid, 0); 2108 2109#ifdef NEW_PROC_API 2110 if (!pi->status_valid) 2111 if (!proc_get_status (pi)) 2112 return NULL; 2113 2114#ifdef UNIXWARE 2115 ret = &pi->prstatus.pr_lwp.pr_context.uc_sigmask; 2116#else 2117 ret = &pi->prstatus.pr_lwp.pr_lwphold; 2118#endif /* UNIXWARE */ 2119#else /* not NEW_PROC_API */ 2120 { 2121 static gdb_sigset_t sigheld; 2122 2123 if (ioctl (pi->ctl_fd, PIOCGHOLD, &sigheld) >= 0) 2124 ret = &sigheld; 2125 } 2126#endif /* NEW_PROC_API */ 2127 if (save && ret) 2128 memcpy (save, ret, sizeof (gdb_sigset_t)); 2129 2130 return ret; 2131} 2132 2133/* 2134 * Function: proc_get_traced_signals 2135 * 2136 * returns the set of signals that are traced / debugged. 2137 * Will also copy the sigset if 'save' is non-zero. 2138 */ 2139 2140gdb_sigset_t * 2141proc_get_traced_signals (procinfo *pi, gdb_sigset_t *save) 2142{ 2143 gdb_sigset_t *ret = NULL; 2144 2145 /* 2146 * We should never have to apply this operation to any procinfo 2147 * except the one for the main process. If that ever changes 2148 * for any reason, then take out the following clause and 2149 * replace it with one that makes sure the ctl_fd is open. 2150 */ 2151 2152 if (pi->tid != 0) 2153 pi = find_procinfo_or_die (pi->pid, 0); 2154 2155#ifdef NEW_PROC_API 2156 if (!pi->status_valid) 2157 if (!proc_get_status (pi)) 2158 return NULL; 2159 2160 ret = &pi->prstatus.pr_sigtrace; 2161#else 2162 { 2163 static gdb_sigset_t sigtrace; 2164 2165 if (ioctl (pi->ctl_fd, PIOCGTRACE, &sigtrace) >= 0) 2166 ret = &sigtrace; 2167 } 2168#endif 2169 if (save && ret) 2170 memcpy (save, ret, sizeof (gdb_sigset_t)); 2171 2172 return ret; 2173} 2174 2175/* 2176 * Function: proc_trace_signal 2177 * 2178 * Add 'signo' to the set of signals that are traced. 2179 * Returns non-zero for success, zero for failure. 2180 */ 2181 2182int 2183proc_trace_signal (procinfo *pi, int signo) 2184{ 2185 gdb_sigset_t temp; 2186 2187 /* 2188 * We should never have to apply this operation to any procinfo 2189 * except the one for the main process. If that ever changes 2190 * for any reason, then take out the following clause and 2191 * replace it with one that makes sure the ctl_fd is open. 2192 */ 2193 2194 if (pi->tid != 0) 2195 pi = find_procinfo_or_die (pi->pid, 0); 2196 2197 if (pi) 2198 { 2199 if (proc_get_traced_signals (pi, &temp)) 2200 { 2201 praddset (&temp, signo); 2202 return proc_set_traced_signals (pi, &temp); 2203 } 2204 } 2205 2206 return 0; /* failure */ 2207} 2208 2209/* 2210 * Function: proc_ignore_signal 2211 * 2212 * Remove 'signo' from the set of signals that are traced. 2213 * Returns non-zero for success, zero for failure. 2214 */ 2215 2216int 2217proc_ignore_signal (procinfo *pi, int signo) 2218{ 2219 gdb_sigset_t temp; 2220 2221 /* 2222 * We should never have to apply this operation to any procinfo 2223 * except the one for the main process. If that ever changes 2224 * for any reason, then take out the following clause and 2225 * replace it with one that makes sure the ctl_fd is open. 2226 */ 2227 2228 if (pi->tid != 0) 2229 pi = find_procinfo_or_die (pi->pid, 0); 2230 2231 if (pi) 2232 { 2233 if (proc_get_traced_signals (pi, &temp)) 2234 { 2235 prdelset (&temp, signo); 2236 return proc_set_traced_signals (pi, &temp); 2237 } 2238 } 2239 2240 return 0; /* failure */ 2241} 2242 2243/* 2244 * Function: proc_get_traced_faults 2245 * 2246 * returns the set of hardware faults that are traced /debugged. 2247 * Will also copy the faultset if 'save' is non-zero. 2248 */ 2249 2250fltset_t * 2251proc_get_traced_faults (procinfo *pi, fltset_t *save) 2252{ 2253 fltset_t *ret = NULL; 2254 2255 /* 2256 * We should never have to apply this operation to any procinfo 2257 * except the one for the main process. If that ever changes 2258 * for any reason, then take out the following clause and 2259 * replace it with one that makes sure the ctl_fd is open. 2260 */ 2261 2262 if (pi->tid != 0) 2263 pi = find_procinfo_or_die (pi->pid, 0); 2264 2265#ifdef NEW_PROC_API 2266 if (!pi->status_valid) 2267 if (!proc_get_status (pi)) 2268 return NULL; 2269 2270 ret = &pi->prstatus.pr_flttrace; 2271#else 2272 { 2273 static fltset_t flttrace; 2274 2275 if (ioctl (pi->ctl_fd, PIOCGFAULT, &flttrace) >= 0) 2276 ret = &flttrace; 2277 } 2278#endif 2279 if (save && ret) 2280 memcpy (save, ret, sizeof (fltset_t)); 2281 2282 return ret; 2283} 2284 2285/* 2286 * Function: proc_get_traced_sysentry 2287 * 2288 * returns the set of syscalls that are traced /debugged on entry. 2289 * Will also copy the syscall set if 'save' is non-zero. 2290 */ 2291 2292sysset_t * 2293proc_get_traced_sysentry (procinfo *pi, sysset_t *save) 2294{ 2295 sysset_t *ret = NULL; 2296 2297 /* 2298 * We should never have to apply this operation to any procinfo 2299 * except the one for the main process. If that ever changes 2300 * for any reason, then take out the following clause and 2301 * replace it with one that makes sure the ctl_fd is open. 2302 */ 2303 2304 if (pi->tid != 0) 2305 pi = find_procinfo_or_die (pi->pid, 0); 2306 2307#ifdef NEW_PROC_API 2308 if (!pi->status_valid) 2309 if (!proc_get_status (pi)) 2310 return NULL; 2311 2312#ifndef DYNAMIC_SYSCALLS 2313 ret = &pi->prstatus.pr_sysentry; 2314#else /* DYNAMIC_SYSCALLS */ 2315 { 2316 static sysset_t *sysentry; 2317 size_t size; 2318 2319 if (!sysentry) 2320 sysentry = sysset_t_alloc (pi); 2321 ret = sysentry; 2322 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0) 2323 return NULL; 2324 if (pi->prstatus.pr_sysentry_offset == 0) 2325 { 2326 gdb_premptysysset (sysentry); 2327 } 2328 else 2329 { 2330 int rsize; 2331 2332 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysentry_offset, 2333 SEEK_SET) 2334 != (off_t) pi->prstatus.pr_sysentry_offset) 2335 return NULL; 2336 size = sysset_t_size (pi); 2337 gdb_premptysysset (sysentry); 2338 rsize = read (pi->status_fd, sysentry, size); 2339 if (rsize < 0) 2340 return NULL; 2341 } 2342 } 2343#endif /* DYNAMIC_SYSCALLS */ 2344#else /* !NEW_PROC_API */ 2345 { 2346 static sysset_t sysentry; 2347 2348 if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysentry) >= 0) 2349 ret = &sysentry; 2350 } 2351#endif /* NEW_PROC_API */ 2352 if (save && ret) 2353 memcpy (save, ret, sysset_t_size (pi)); 2354 2355 return ret; 2356} 2357 2358/* 2359 * Function: proc_get_traced_sysexit 2360 * 2361 * returns the set of syscalls that are traced /debugged on exit. 2362 * Will also copy the syscall set if 'save' is non-zero. 2363 */ 2364 2365sysset_t * 2366proc_get_traced_sysexit (procinfo *pi, sysset_t *save) 2367{ 2368 sysset_t * ret = NULL; 2369 2370 /* 2371 * We should never have to apply this operation to any procinfo 2372 * except the one for the main process. If that ever changes 2373 * for any reason, then take out the following clause and 2374 * replace it with one that makes sure the ctl_fd is open. 2375 */ 2376 2377 if (pi->tid != 0) 2378 pi = find_procinfo_or_die (pi->pid, 0); 2379 2380#ifdef NEW_PROC_API 2381 if (!pi->status_valid) 2382 if (!proc_get_status (pi)) 2383 return NULL; 2384 2385#ifndef DYNAMIC_SYSCALLS 2386 ret = &pi->prstatus.pr_sysexit; 2387#else /* DYNAMIC_SYSCALLS */ 2388 { 2389 static sysset_t *sysexit; 2390 size_t size; 2391 2392 if (!sysexit) 2393 sysexit = sysset_t_alloc (pi); 2394 ret = sysexit; 2395 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0) 2396 return NULL; 2397 if (pi->prstatus.pr_sysexit_offset == 0) 2398 { 2399 gdb_premptysysset (sysexit); 2400 } 2401 else 2402 { 2403 int rsize; 2404 2405 if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysexit_offset, SEEK_SET) 2406 != (off_t) pi->prstatus.pr_sysexit_offset) 2407 return NULL; 2408 size = sysset_t_size (pi); 2409 gdb_premptysysset (sysexit); 2410 rsize = read (pi->status_fd, sysexit, size); 2411 if (rsize < 0) 2412 return NULL; 2413 } 2414 } 2415#endif /* DYNAMIC_SYSCALLS */ 2416#else 2417 { 2418 static sysset_t sysexit; 2419 2420 if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysexit) >= 0) 2421 ret = &sysexit; 2422 } 2423#endif 2424 if (save && ret) 2425 memcpy (save, ret, sysset_t_size (pi)); 2426 2427 return ret; 2428} 2429 2430/* 2431 * Function: proc_clear_current_fault 2432 * 2433 * The current fault (if any) is cleared; the associated signal 2434 * will not be sent to the process or LWP when it resumes. 2435 * Returns non-zero for success, zero for failure. 2436 */ 2437 2438int 2439proc_clear_current_fault (procinfo *pi) 2440{ 2441 int win; 2442 2443 /* 2444 * We should never have to apply this operation to any procinfo 2445 * except the one for the main process. If that ever changes 2446 * for any reason, then take out the following clause and 2447 * replace it with one that makes sure the ctl_fd is open. 2448 */ 2449 2450 if (pi->tid != 0) 2451 pi = find_procinfo_or_die (pi->pid, 0); 2452 2453#ifdef NEW_PROC_API 2454 { 2455 procfs_ctl_t cmd = PCCFAULT; 2456 win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd)); 2457 } 2458#else 2459 win = (ioctl (pi->ctl_fd, PIOCCFAULT, 0) >= 0); 2460#endif 2461 2462 return win; 2463} 2464 2465/* 2466 * Function: proc_set_current_signal 2467 * 2468 * Set the "current signal" that will be delivered next to the process. 2469 * NOTE: semantics are different from those of KILL. 2470 * This signal will be delivered to the process or LWP 2471 * immediately when it is resumed (even if the signal is held/blocked); 2472 * it will NOT immediately cause another event of interest, and will NOT 2473 * first trap back to the debugger. 2474 * 2475 * Returns non-zero for success, zero for failure. 2476 */ 2477 2478int 2479proc_set_current_signal (procinfo *pi, int signo) 2480{ 2481 int win; 2482 struct { 2483 procfs_ctl_t cmd; 2484 /* Use char array to avoid alignment issues. */ 2485 char sinfo[sizeof (gdb_siginfo_t)]; 2486 } arg; 2487 gdb_siginfo_t *mysinfo; 2488 2489 /* 2490 * We should never have to apply this operation to any procinfo 2491 * except the one for the main process. If that ever changes 2492 * for any reason, then take out the following clause and 2493 * replace it with one that makes sure the ctl_fd is open. 2494 */ 2495 2496 if (pi->tid != 0) 2497 pi = find_procinfo_or_die (pi->pid, 0); 2498 2499#ifdef PROCFS_DONT_PIOCSSIG_CURSIG 2500 /* With Alpha OSF/1 procfs, the kernel gets really confused if it 2501 * receives a PIOCSSIG with a signal identical to the current signal, 2502 * it messes up the current signal. Work around the kernel bug. 2503 */ 2504 if (signo > 0 && 2505 signo == proc_cursig (pi)) 2506 return 1; /* I assume this is a success? */ 2507#endif 2508 2509 /* The pointer is just a type alias. */ 2510 mysinfo = (gdb_siginfo_t *) &arg.sinfo; 2511 mysinfo->si_signo = signo; 2512 mysinfo->si_code = 0; 2513 mysinfo->si_pid = getpid (); /* ?why? */ 2514 mysinfo->si_uid = getuid (); /* ?why? */ 2515 2516#ifdef NEW_PROC_API 2517 arg.cmd = PCSSIG; 2518 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); 2519#else 2520 win = (ioctl (pi->ctl_fd, PIOCSSIG, (void *) &arg.sinfo) >= 0); 2521#endif 2522 2523 return win; 2524} 2525 2526/* 2527 * Function: proc_clear_current_signal 2528 * 2529 * The current signal (if any) is cleared, and 2530 * is not sent to the process or LWP when it resumes. 2531 * Returns non-zero for success, zero for failure. 2532 */ 2533 2534int 2535proc_clear_current_signal (procinfo *pi) 2536{ 2537 int win; 2538 2539 /* 2540 * We should never have to apply this operation to any procinfo 2541 * except the one for the main process. If that ever changes 2542 * for any reason, then take out the following clause and 2543 * replace it with one that makes sure the ctl_fd is open. 2544 */ 2545 2546 if (pi->tid != 0) 2547 pi = find_procinfo_or_die (pi->pid, 0); 2548 2549#ifdef NEW_PROC_API 2550 { 2551 struct { 2552 procfs_ctl_t cmd; 2553 /* Use char array to avoid alignment issues. */ 2554 char sinfo[sizeof (gdb_siginfo_t)]; 2555 } arg; 2556 gdb_siginfo_t *mysinfo; 2557 2558 arg.cmd = PCSSIG; 2559 /* The pointer is just a type alias. */ 2560 mysinfo = (gdb_siginfo_t *) &arg.sinfo; 2561 mysinfo->si_signo = 0; 2562 mysinfo->si_code = 0; 2563 mysinfo->si_errno = 0; 2564 mysinfo->si_pid = getpid (); /* ?why? */ 2565 mysinfo->si_uid = getuid (); /* ?why? */ 2566 2567 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); 2568 } 2569#else 2570 win = (ioctl (pi->ctl_fd, PIOCSSIG, 0) >= 0); 2571#endif 2572 2573 return win; 2574} 2575 2576/* 2577 * Function: proc_get_gregs 2578 * 2579 * Get the general registers for the process or LWP. 2580 * Returns non-zero for success, zero for failure. 2581 */ 2582 2583gdb_gregset_t * 2584proc_get_gregs (procinfo *pi) 2585{ 2586 if (!pi->status_valid || !pi->gregs_valid) 2587 if (!proc_get_status (pi)) 2588 return NULL; 2589 2590 /* 2591 * OK, sorry about the ifdef's. 2592 * There's three cases instead of two, because 2593 * in this instance Unixware and Solaris/RW differ. 2594 */ 2595 2596#ifdef NEW_PROC_API 2597#ifdef UNIXWARE /* ugh, a true architecture dependency */ 2598 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs; 2599#else /* not Unixware */ 2600 return &pi->prstatus.pr_lwp.pr_reg; 2601#endif /* Unixware */ 2602#else /* not NEW_PROC_API */ 2603 return &pi->prstatus.pr_reg; 2604#endif /* NEW_PROC_API */ 2605} 2606 2607/* 2608 * Function: proc_get_fpregs 2609 * 2610 * Get the floating point registers for the process or LWP. 2611 * Returns non-zero for success, zero for failure. 2612 */ 2613 2614gdb_fpregset_t * 2615proc_get_fpregs (procinfo *pi) 2616{ 2617#ifdef NEW_PROC_API 2618 if (!pi->status_valid || !pi->fpregs_valid) 2619 if (!proc_get_status (pi)) 2620 return NULL; 2621 2622#ifdef UNIXWARE /* a true architecture dependency */ 2623 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs; 2624#else 2625 return &pi->prstatus.pr_lwp.pr_fpreg; 2626#endif /* Unixware */ 2627 2628#else /* not NEW_PROC_API */ 2629 if (pi->fpregs_valid) 2630 return &pi->fpregset; /* already got 'em */ 2631 else 2632 { 2633 if (pi->ctl_fd == 0 && 2634 open_procinfo_files (pi, FD_CTL) == 0) 2635 { 2636 return NULL; 2637 } 2638 else 2639 { 2640#ifdef PIOCTGFPREG 2641 struct { 2642 long pr_count; 2643 tid_t pr_error_thread; 2644 tfpregset_t thread_1; 2645 } thread_fpregs; 2646 2647 thread_fpregs.pr_count = 1; 2648 thread_fpregs.thread_1.tid = pi->tid; 2649 2650 if (pi->tid == 0 && 2651 ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0) 2652 { 2653 pi->fpregs_valid = 1; 2654 return &pi->fpregset; /* got 'em now! */ 2655 } 2656 else if (pi->tid != 0 && 2657 ioctl (pi->ctl_fd, PIOCTGFPREG, &thread_fpregs) >= 0) 2658 { 2659 memcpy (&pi->fpregset, &thread_fpregs.thread_1.pr_fpregs, 2660 sizeof (pi->fpregset)); 2661 pi->fpregs_valid = 1; 2662 return &pi->fpregset; /* got 'em now! */ 2663 } 2664 else 2665 { 2666 return NULL; 2667 } 2668#else 2669 if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0) 2670 { 2671 pi->fpregs_valid = 1; 2672 return &pi->fpregset; /* got 'em now! */ 2673 } 2674 else 2675 { 2676 return NULL; 2677 } 2678#endif 2679 } 2680 } 2681#endif 2682} 2683 2684/* 2685 * Function: proc_set_gregs 2686 * 2687 * Write the general registers back to the process or LWP. 2688 * Returns non-zero for success, zero for failure. 2689 */ 2690 2691int 2692proc_set_gregs (procinfo *pi) 2693{ 2694 gdb_gregset_t *gregs; 2695 int win; 2696 2697 if ((gregs = proc_get_gregs (pi)) == NULL) 2698 return 0; /* get_regs has already warned */ 2699 2700 if (pi->ctl_fd == 0 && 2701 open_procinfo_files (pi, FD_CTL) == 0) 2702 { 2703 return 0; 2704 } 2705 else 2706 { 2707#ifdef NEW_PROC_API 2708 struct { 2709 procfs_ctl_t cmd; 2710 /* Use char array to avoid alignment issues. */ 2711 char gregs[sizeof (gdb_gregset_t)]; 2712 } arg; 2713 2714 arg.cmd = PCSREG; 2715 memcpy (&arg.gregs, gregs, sizeof (arg.gregs)); 2716 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); 2717#else 2718 win = (ioctl (pi->ctl_fd, PIOCSREG, gregs) >= 0); 2719#endif 2720 } 2721 2722 /* Policy: writing the regs invalidates our cache. */ 2723 pi->gregs_valid = 0; 2724 return win; 2725} 2726 2727/* 2728 * Function: proc_set_fpregs 2729 * 2730 * Modify the floating point register set of the process or LWP. 2731 * Returns non-zero for success, zero for failure. 2732 */ 2733 2734int 2735proc_set_fpregs (procinfo *pi) 2736{ 2737 gdb_fpregset_t *fpregs; 2738 int win; 2739 2740 if ((fpregs = proc_get_fpregs (pi)) == NULL) 2741 return 0; /* get_fpregs has already warned */ 2742 2743 if (pi->ctl_fd == 0 && 2744 open_procinfo_files (pi, FD_CTL) == 0) 2745 { 2746 return 0; 2747 } 2748 else 2749 { 2750#ifdef NEW_PROC_API 2751 struct { 2752 procfs_ctl_t cmd; 2753 /* Use char array to avoid alignment issues. */ 2754 char fpregs[sizeof (gdb_fpregset_t)]; 2755 } arg; 2756 2757 arg.cmd = PCSFPREG; 2758 memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs)); 2759 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); 2760#else 2761#ifdef PIOCTSFPREG 2762 if (pi->tid == 0) 2763 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0); 2764 else 2765 { 2766 struct { 2767 long pr_count; 2768 tid_t pr_error_thread; 2769 tfpregset_t thread_1; 2770 } thread_fpregs; 2771 2772 thread_fpregs.pr_count = 1; 2773 thread_fpregs.thread_1.tid = pi->tid; 2774 memcpy (&thread_fpregs.thread_1.pr_fpregs, fpregs, 2775 sizeof (*fpregs)); 2776 win = (ioctl (pi->ctl_fd, PIOCTSFPREG, &thread_fpregs) >= 0); 2777 } 2778#else 2779 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0); 2780#endif /* osf PIOCTSFPREG */ 2781#endif /* NEW_PROC_API */ 2782 } 2783 2784 /* Policy: writing the regs invalidates our cache. */ 2785 pi->fpregs_valid = 0; 2786 return win; 2787} 2788 2789/* 2790 * Function: proc_kill 2791 * 2792 * Send a signal to the proc or lwp with the semantics of "kill()". 2793 * Returns non-zero for success, zero for failure. 2794 */ 2795 2796int 2797proc_kill (procinfo *pi, int signo) 2798{ 2799 int win; 2800 2801 /* 2802 * We might conceivably apply this operation to an LWP, and 2803 * the LWP's ctl file descriptor might not be open. 2804 */ 2805 2806 if (pi->ctl_fd == 0 && 2807 open_procinfo_files (pi, FD_CTL) == 0) 2808 { 2809 return 0; 2810 } 2811 else 2812 { 2813#ifdef NEW_PROC_API 2814 procfs_ctl_t cmd[2]; 2815 2816 cmd[0] = PCKILL; 2817 cmd[1] = signo; 2818 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); 2819#else /* ioctl method */ 2820 /* FIXME: do I need the Alpha OSF fixups present in 2821 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */ 2822 win = (ioctl (pi->ctl_fd, PIOCKILL, &signo) >= 0); 2823#endif 2824 } 2825 2826 return win; 2827} 2828 2829/* 2830 * Function: proc_parent_pid 2831 * 2832 * Find the pid of the process that started this one. 2833 * Returns the parent process pid, or zero. 2834 */ 2835 2836int 2837proc_parent_pid (procinfo *pi) 2838{ 2839 /* 2840 * We should never have to apply this operation to any procinfo 2841 * except the one for the main process. If that ever changes 2842 * for any reason, then take out the following clause and 2843 * replace it with one that makes sure the ctl_fd is open. 2844 */ 2845 2846 if (pi->tid != 0) 2847 pi = find_procinfo_or_die (pi->pid, 0); 2848 2849 if (!pi->status_valid) 2850 if (!proc_get_status (pi)) 2851 return 0; 2852 2853 return pi->prstatus.pr_ppid; 2854} 2855 2856 2857/* Convert a target address (a.k.a. CORE_ADDR) into a host address 2858 (a.k.a void pointer)! */ 2859 2860static void * 2861procfs_address_to_host_pointer (CORE_ADDR addr) 2862{ 2863 void *ptr; 2864 2865 gdb_assert (sizeof (ptr) == TYPE_LENGTH (builtin_type_void_data_ptr)); 2866 ADDRESS_TO_POINTER (builtin_type_void_data_ptr, &ptr, addr); 2867 return ptr; 2868} 2869 2870/* 2871 * Function: proc_set_watchpoint 2872 * 2873 */ 2874 2875int 2876proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags) 2877{ 2878#if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS) 2879 return 0; 2880#else 2881/* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */ 2882#if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */ 2883 return 0; 2884#else 2885 struct { 2886 procfs_ctl_t cmd; 2887 char watch[sizeof (prwatch_t)]; 2888 } arg; 2889 prwatch_t *pwatch; 2890 2891 pwatch = (prwatch_t *) &arg.watch; 2892 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to 2893 convert a target address into something that can be stored in a 2894 native data structure. */ 2895#ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */ 2896 pwatch->pr_vaddr = (uintptr_t) procfs_address_to_host_pointer (addr); 2897#else 2898 pwatch->pr_vaddr = (caddr_t) procfs_address_to_host_pointer (addr); 2899#endif 2900 pwatch->pr_size = len; 2901 pwatch->pr_wflags = wflags; 2902#if defined(NEW_PROC_API) && defined (PCWATCH) 2903 arg.cmd = PCWATCH; 2904 return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg)); 2905#else 2906#if defined (PIOCSWATCH) 2907 return (ioctl (pi->ctl_fd, PIOCSWATCH, pwatch) >= 0); 2908#else 2909 return 0; /* Fail */ 2910#endif 2911#endif 2912#endif 2913#endif 2914} 2915 2916#ifdef TM_I386SOL2_H /* Is it hokey to use this? */ 2917 2918#include <sys/sysi86.h> 2919 2920/* 2921 * Function: proc_get_LDT_entry 2922 * 2923 * Inputs: 2924 * procinfo *pi; 2925 * int key; 2926 * 2927 * The 'key' is actually the value of the lower 16 bits of 2928 * the GS register for the LWP that we're interested in. 2929 * 2930 * Return: matching ssh struct (LDT entry). 2931 */ 2932 2933struct ssd * 2934proc_get_LDT_entry (procinfo *pi, int key) 2935{ 2936 static struct ssd *ldt_entry = NULL; 2937#ifdef NEW_PROC_API 2938 char pathname[MAX_PROC_NAME_SIZE]; 2939 struct cleanup *old_chain = NULL; 2940 int fd; 2941 2942 /* Allocate space for one LDT entry. 2943 This alloc must persist, because we return a pointer to it. */ 2944 if (ldt_entry == NULL) 2945 ldt_entry = (struct ssd *) xmalloc (sizeof (struct ssd)); 2946 2947 /* Open the file descriptor for the LDT table. */ 2948 sprintf (pathname, "/proc/%d/ldt", pi->pid); 2949 if ((fd = open_with_retry (pathname, O_RDONLY)) < 0) 2950 { 2951 proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__); 2952 return NULL; 2953 } 2954 /* Make sure it gets closed again! */ 2955 old_chain = make_cleanup_close (fd); 2956 2957 /* Now 'read' thru the table, find a match and return it. */ 2958 while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd)) 2959 { 2960 if (ldt_entry->sel == 0 && 2961 ldt_entry->bo == 0 && 2962 ldt_entry->acc1 == 0 && 2963 ldt_entry->acc2 == 0) 2964 break; /* end of table */ 2965 /* If key matches, return this entry. */ 2966 if (ldt_entry->sel == key) 2967 return ldt_entry; 2968 } 2969 /* Loop ended, match not found. */ 2970 return NULL; 2971#else 2972 int nldt, i; 2973 static int nalloc = 0; 2974 2975 /* Get the number of LDT entries. */ 2976 if (ioctl (pi->ctl_fd, PIOCNLDT, &nldt) < 0) 2977 { 2978 proc_warn (pi, "proc_get_LDT_entry (PIOCNLDT)", __LINE__); 2979 return NULL; 2980 } 2981 2982 /* Allocate space for the number of LDT entries. */ 2983 /* This alloc has to persist, 'cause we return a pointer to it. */ 2984 if (nldt > nalloc) 2985 { 2986 ldt_entry = (struct ssd *) 2987 xrealloc (ldt_entry, (nldt + 1) * sizeof (struct ssd)); 2988 nalloc = nldt; 2989 } 2990 2991 /* Read the whole table in one gulp. */ 2992 if (ioctl (pi->ctl_fd, PIOCLDT, ldt_entry) < 0) 2993 { 2994 proc_warn (pi, "proc_get_LDT_entry (PIOCLDT)", __LINE__); 2995 return NULL; 2996 } 2997 2998 /* Search the table and return the (first) entry matching 'key'. */ 2999 for (i = 0; i < nldt; i++) 3000 if (ldt_entry[i].sel == key) 3001 return &ldt_entry[i]; 3002 3003 /* Loop ended, match not found. */ 3004 return NULL; 3005#endif 3006} 3007 3008#endif /* TM_I386SOL2_H */ 3009 3010/* =============== END, non-thread part of /proc "MODULE" =============== */ 3011 3012/* =================== Thread "MODULE" =================== */ 3013 3014/* NOTE: you'll see more ifdefs and duplication of functions here, 3015 since there is a different way to do threads on every OS. */ 3016 3017/* 3018 * Function: proc_get_nthreads 3019 * 3020 * Return the number of threads for the process 3021 */ 3022 3023#if defined (PIOCNTHR) && defined (PIOCTLIST) 3024/* 3025 * OSF version 3026 */ 3027int 3028proc_get_nthreads (procinfo *pi) 3029{ 3030 int nthreads = 0; 3031 3032 if (ioctl (pi->ctl_fd, PIOCNTHR, &nthreads) < 0) 3033 proc_warn (pi, "procfs: PIOCNTHR failed", __LINE__); 3034 3035 return nthreads; 3036} 3037 3038#else 3039#if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */ 3040/* 3041 * Solaris and Unixware version 3042 */ 3043int 3044proc_get_nthreads (procinfo *pi) 3045{ 3046 if (!pi->status_valid) 3047 if (!proc_get_status (pi)) 3048 return 0; 3049 3050 /* 3051 * NEW_PROC_API: only works for the process procinfo, 3052 * because the LWP procinfos do not get prstatus filled in. 3053 */ 3054#ifdef NEW_PROC_API 3055 if (pi->tid != 0) /* find the parent process procinfo */ 3056 pi = find_procinfo_or_die (pi->pid, 0); 3057#endif 3058 return pi->prstatus.pr_nlwp; 3059} 3060 3061#else 3062/* 3063 * Default version 3064 */ 3065int 3066proc_get_nthreads (procinfo *pi) 3067{ 3068 return 0; 3069} 3070#endif 3071#endif 3072 3073/* 3074 * Function: proc_get_current_thread (LWP version) 3075 * 3076 * Return the ID of the thread that had an event of interest. 3077 * (ie. the one that hit a breakpoint or other traced event). 3078 * All other things being equal, this should be the ID of a 3079 * thread that is currently executing. 3080 */ 3081 3082#if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */ 3083/* 3084 * Solaris and Unixware version 3085 */ 3086int 3087proc_get_current_thread (procinfo *pi) 3088{ 3089 /* 3090 * Note: this should be applied to the root procinfo for the process, 3091 * not to the procinfo for an LWP. If applied to the procinfo for 3092 * an LWP, it will simply return that LWP's ID. In that case, 3093 * find the parent process procinfo. 3094 */ 3095 3096 if (pi->tid != 0) 3097 pi = find_procinfo_or_die (pi->pid, 0); 3098 3099 if (!pi->status_valid) 3100 if (!proc_get_status (pi)) 3101 return 0; 3102 3103#ifdef NEW_PROC_API 3104 return pi->prstatus.pr_lwp.pr_lwpid; 3105#else 3106 return pi->prstatus.pr_who; 3107#endif 3108} 3109 3110#else 3111#if defined (PIOCNTHR) && defined (PIOCTLIST) 3112/* 3113 * OSF version 3114 */ 3115int 3116proc_get_current_thread (procinfo *pi) 3117{ 3118#if 0 /* FIXME: not ready for prime time? */ 3119 return pi->prstatus.pr_tid; 3120#else 3121 return 0; 3122#endif 3123} 3124 3125#else 3126/* 3127 * Default version 3128 */ 3129int 3130proc_get_current_thread (procinfo *pi) 3131{ 3132 return 0; 3133} 3134 3135#endif 3136#endif 3137 3138/* 3139 * Function: proc_update_threads 3140 * 3141 * Discover the IDs of all the threads within the process, and 3142 * create a procinfo for each of them (chained to the parent). 3143 * 3144 * This unfortunately requires a different method on every OS. 3145 * 3146 * Return: non-zero for success, zero for failure. 3147 */ 3148 3149int 3150proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore) 3151{ 3152 if (thread && parent) /* sanity */ 3153 { 3154 thread->status_valid = 0; 3155 if (!proc_get_status (thread)) 3156 destroy_one_procinfo (&parent->thread_list, thread); 3157 } 3158 return 0; /* keep iterating */ 3159} 3160 3161#if defined (PIOCLSTATUS) 3162/* 3163 * Solaris 2.5 (ioctl) version 3164 */ 3165int 3166proc_update_threads (procinfo *pi) 3167{ 3168 gdb_prstatus_t *prstatus; 3169 struct cleanup *old_chain = NULL; 3170 procinfo *thread; 3171 int nlwp, i; 3172 3173 /* 3174 * We should never have to apply this operation to any procinfo 3175 * except the one for the main process. If that ever changes 3176 * for any reason, then take out the following clause and 3177 * replace it with one that makes sure the ctl_fd is open. 3178 */ 3179 3180 if (pi->tid != 0) 3181 pi = find_procinfo_or_die (pi->pid, 0); 3182 3183 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL); 3184 3185 if ((nlwp = proc_get_nthreads (pi)) <= 1) 3186 return 1; /* Process is not multi-threaded; nothing to do. */ 3187 3188 prstatus = xmalloc (sizeof (gdb_prstatus_t) * (nlwp + 1)); 3189 3190 old_chain = make_cleanup (xfree, prstatus); 3191 if (ioctl (pi->ctl_fd, PIOCLSTATUS, prstatus) < 0) 3192 proc_error (pi, "update_threads (PIOCLSTATUS)", __LINE__); 3193 3194 /* Skip element zero, which represents the process as a whole. */ 3195 for (i = 1; i < nlwp + 1; i++) 3196 { 3197 if ((thread = create_procinfo (pi->pid, prstatus[i].pr_who)) == NULL) 3198 proc_error (pi, "update_threads, create_procinfo", __LINE__); 3199 3200 memcpy (&thread->prstatus, &prstatus[i], sizeof (*prstatus)); 3201 thread->status_valid = 1; 3202 } 3203 pi->threads_valid = 1; 3204 do_cleanups (old_chain); 3205 return 1; 3206} 3207#else 3208#ifdef NEW_PROC_API 3209/* 3210 * Unixware and Solaris 6 (and later) version 3211 */ 3212static void 3213do_closedir_cleanup (void *dir) 3214{ 3215 closedir (dir); 3216} 3217 3218int 3219proc_update_threads (procinfo *pi) 3220{ 3221 char pathname[MAX_PROC_NAME_SIZE + 16]; 3222 struct dirent *direntry; 3223 struct cleanup *old_chain = NULL; 3224 procinfo *thread; 3225 DIR *dirp; 3226 int lwpid; 3227 3228 /* 3229 * We should never have to apply this operation to any procinfo 3230 * except the one for the main process. If that ever changes 3231 * for any reason, then take out the following clause and 3232 * replace it with one that makes sure the ctl_fd is open. 3233 */ 3234 3235 if (pi->tid != 0) 3236 pi = find_procinfo_or_die (pi->pid, 0); 3237 3238 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL); 3239 3240 /* 3241 * Unixware 3242 * 3243 * Note: this brute-force method is the only way I know of 3244 * to accomplish this task on Unixware. This method will 3245 * also work on Solaris 2.6 and 2.7. There is a much simpler 3246 * and more elegant way to do this on Solaris, but the margins 3247 * of this manuscript are too small to write it here... ;-) 3248 */ 3249 3250 strcpy (pathname, pi->pathname); 3251 strcat (pathname, "/lwp"); 3252 if ((dirp = opendir (pathname)) == NULL) 3253 proc_error (pi, "update_threads, opendir", __LINE__); 3254 3255 old_chain = make_cleanup (do_closedir_cleanup, dirp); 3256 while ((direntry = readdir (dirp)) != NULL) 3257 if (direntry->d_name[0] != '.') /* skip '.' and '..' */ 3258 { 3259 lwpid = atoi (&direntry->d_name[0]); 3260 if ((thread = create_procinfo (pi->pid, lwpid)) == NULL) 3261 proc_error (pi, "update_threads, create_procinfo", __LINE__); 3262 } 3263 pi->threads_valid = 1; 3264 do_cleanups (old_chain); 3265 return 1; 3266} 3267#else 3268#ifdef PIOCTLIST 3269/* 3270 * OSF version 3271 */ 3272int 3273proc_update_threads (procinfo *pi) 3274{ 3275 int nthreads, i; 3276 tid_t *threads; 3277 3278 /* 3279 * We should never have to apply this operation to any procinfo 3280 * except the one for the main process. If that ever changes 3281 * for any reason, then take out the following clause and 3282 * replace it with one that makes sure the ctl_fd is open. 3283 */ 3284 3285 if (pi->tid != 0) 3286 pi = find_procinfo_or_die (pi->pid, 0); 3287 3288 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL); 3289 3290 nthreads = proc_get_nthreads (pi); 3291 if (nthreads < 2) 3292 return 0; /* nothing to do for 1 or fewer threads */ 3293 3294 threads = xmalloc (nthreads * sizeof (tid_t)); 3295 3296 if (ioctl (pi->ctl_fd, PIOCTLIST, threads) < 0) 3297 proc_error (pi, "procfs: update_threads (PIOCTLIST)", __LINE__); 3298 3299 for (i = 0; i < nthreads; i++) 3300 { 3301 if (!find_procinfo (pi->pid, threads[i])) 3302 if (!create_procinfo (pi->pid, threads[i])) 3303 proc_error (pi, "update_threads, create_procinfo", __LINE__); 3304 } 3305 pi->threads_valid = 1; 3306 return 1; 3307} 3308#else 3309/* 3310 * Default version 3311 */ 3312int 3313proc_update_threads (procinfo *pi) 3314{ 3315 return 0; 3316} 3317#endif /* OSF PIOCTLIST */ 3318#endif /* NEW_PROC_API */ 3319#endif /* SOL 2.5 PIOCLSTATUS */ 3320 3321/* 3322 * Function: proc_iterate_over_threads 3323 * 3324 * Description: 3325 * Given a pointer to a function, call that function once 3326 * for each lwp in the procinfo list, until the function 3327 * returns non-zero, in which event return the value 3328 * returned by the function. 3329 * 3330 * Note: this function does NOT call update_threads. 3331 * If you want to discover new threads first, you must 3332 * call that function explicitly. This function just makes 3333 * a quick pass over the currently-known procinfos. 3334 * 3335 * Arguments: 3336 * pi - parent process procinfo 3337 * func - per-thread function 3338 * ptr - opaque parameter for function. 3339 * 3340 * Return: 3341 * First non-zero return value from the callee, or zero. 3342 */ 3343 3344int 3345proc_iterate_over_threads (procinfo *pi, 3346 int (*func) (procinfo *, procinfo *, void *), 3347 void *ptr) 3348{ 3349 procinfo *thread, *next; 3350 int retval = 0; 3351 3352 /* 3353 * We should never have to apply this operation to any procinfo 3354 * except the one for the main process. If that ever changes 3355 * for any reason, then take out the following clause and 3356 * replace it with one that makes sure the ctl_fd is open. 3357 */ 3358 3359 if (pi->tid != 0) 3360 pi = find_procinfo_or_die (pi->pid, 0); 3361 3362 for (thread = pi->thread_list; thread != NULL; thread = next) 3363 { 3364 next = thread->next; /* in case thread is destroyed */ 3365 if ((retval = (*func) (pi, thread, ptr)) != 0) 3366 break; 3367 } 3368 3369 return retval; 3370} 3371 3372/* =================== END, Thread "MODULE" =================== */ 3373 3374/* =================== END, /proc "MODULE" =================== */ 3375 3376/* =================== GDB "MODULE" =================== */ 3377 3378/* 3379 * Here are all of the gdb target vector functions and their friends. 3380 */ 3381 3382static ptid_t do_attach (ptid_t ptid); 3383static void do_detach (int signo); 3384static int register_gdb_signals (procinfo *, gdb_sigset_t *); 3385 3386/* 3387 * Function: procfs_debug_inferior 3388 * 3389 * Sets up the inferior to be debugged. 3390 * Registers to trace signals, hardware faults, and syscalls. 3391 * Note: does not set RLC flag: caller may want to customize that. 3392 * 3393 * Returns: zero for success (note! unlike most functions in this module) 3394 * On failure, returns the LINE NUMBER where it failed! 3395 */ 3396 3397static int 3398procfs_debug_inferior (procinfo *pi) 3399{ 3400 fltset_t traced_faults; 3401 gdb_sigset_t traced_signals; 3402 sysset_t *traced_syscall_entries; 3403 sysset_t *traced_syscall_exits; 3404 int status; 3405 3406#ifdef PROCFS_DONT_TRACE_FAULTS 3407 /* On some systems (OSF), we don't trace hardware faults. 3408 Apparently it's enough that we catch them as signals. 3409 Wonder why we don't just do that in general? */ 3410 premptyset (&traced_faults); /* don't trace faults. */ 3411#else 3412 /* Register to trace hardware faults in the child. */ 3413 prfillset (&traced_faults); /* trace all faults... */ 3414 prdelset (&traced_faults, FLTPAGE); /* except page fault. */ 3415#endif 3416 if (!proc_set_traced_faults (pi, &traced_faults)) 3417 return __LINE__; 3418 3419 /* Register to trace selected signals in the child. */ 3420 premptyset (&traced_signals); 3421 if (!register_gdb_signals (pi, &traced_signals)) 3422 return __LINE__; 3423 3424 3425 /* Register to trace the 'exit' system call (on entry). */ 3426 traced_syscall_entries = sysset_t_alloc (pi); 3427 gdb_premptysysset (traced_syscall_entries); 3428#ifdef SYS_exit 3429 gdb_praddsysset (traced_syscall_entries, SYS_exit); 3430#endif 3431#ifdef SYS_lwpexit 3432 gdb_praddsysset (traced_syscall_entries, SYS_lwpexit); /* And _lwp_exit... */ 3433#endif 3434#ifdef SYS_lwp_exit 3435 gdb_praddsysset (traced_syscall_entries, SYS_lwp_exit); 3436#endif 3437#ifdef DYNAMIC_SYSCALLS 3438 { 3439 int callnum = find_syscall (pi, "_exit"); 3440 if (callnum >= 0) 3441 gdb_praddsysset (traced_syscall_entries, callnum); 3442 } 3443#endif 3444 3445 status = proc_set_traced_sysentry (pi, traced_syscall_entries); 3446 xfree (traced_syscall_entries); 3447 if (!status) 3448 return __LINE__; 3449 3450#ifdef PRFS_STOPEXEC /* defined on OSF */ 3451 /* OSF method for tracing exec syscalls. Quoting: 3452 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace 3453 exits from exec system calls because of the user level loader. */ 3454 /* FIXME: make nice and maybe move into an access function. */ 3455 { 3456 int prfs_flags; 3457 3458 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0) 3459 return __LINE__; 3460 3461 prfs_flags |= PRFS_STOPEXEC; 3462 3463 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0) 3464 return __LINE__; 3465 } 3466#else /* not PRFS_STOPEXEC */ 3467 /* Everyone else's (except OSF) method for tracing exec syscalls */ 3468 /* GW: Rationale... 3469 Not all systems with /proc have all the exec* syscalls with the same 3470 names. On the SGI, for example, there is no SYS_exec, but there 3471 *is* a SYS_execv. So, we try to account for that. */ 3472 3473 traced_syscall_exits = sysset_t_alloc (pi); 3474 gdb_premptysysset (traced_syscall_exits); 3475#ifdef SYS_exec 3476 gdb_praddsysset (traced_syscall_exits, SYS_exec); 3477#endif 3478#ifdef SYS_execve 3479 gdb_praddsysset (traced_syscall_exits, SYS_execve); 3480#endif 3481#ifdef SYS_execv 3482 gdb_praddsysset (traced_syscall_exits, SYS_execv); 3483#endif 3484 3485#ifdef SYS_lwpcreate 3486 gdb_praddsysset (traced_syscall_exits, SYS_lwpcreate); 3487 gdb_praddsysset (traced_syscall_exits, SYS_lwpexit); 3488#endif 3489 3490#ifdef SYS_lwp_create /* FIXME: once only, please */ 3491 gdb_praddsysset (traced_syscall_exits, SYS_lwp_create); 3492 gdb_praddsysset (traced_syscall_exits, SYS_lwp_exit); 3493#endif 3494 3495#ifdef DYNAMIC_SYSCALLS 3496 { 3497 int callnum = find_syscall (pi, "execve"); 3498 if (callnum >= 0) 3499 gdb_praddsysset (traced_syscall_exits, callnum); 3500 callnum = find_syscall (pi, "ra_execve"); 3501 if (callnum >= 0) 3502 gdb_praddsysset (traced_syscall_exits, callnum); 3503 } 3504#endif 3505 3506 status = proc_set_traced_sysexit (pi, traced_syscall_exits); 3507 xfree (traced_syscall_exits); 3508 if (!status) 3509 return __LINE__; 3510 3511#endif /* PRFS_STOPEXEC */ 3512 return 0; 3513} 3514 3515static void 3516procfs_attach (char *args, int from_tty) 3517{ 3518 char *exec_file; 3519 int pid; 3520 3521 if (!args) 3522 error_no_arg ("process-id to attach"); 3523 3524 pid = atoi (args); 3525 if (pid == getpid ()) 3526 error ("Attaching GDB to itself is not a good idea..."); 3527 3528 if (from_tty) 3529 { 3530 exec_file = get_exec_file (0); 3531 3532 if (exec_file) 3533 printf_filtered ("Attaching to program `%s', %s\n", 3534 exec_file, target_pid_to_str (pid_to_ptid (pid))); 3535 else 3536 printf_filtered ("Attaching to %s\n", 3537 target_pid_to_str (pid_to_ptid (pid))); 3538 3539 fflush (stdout); 3540 } 3541 inferior_ptid = do_attach (pid_to_ptid (pid)); 3542 push_target (&procfs_ops); 3543} 3544 3545static void 3546procfs_detach (char *args, int from_tty) 3547{ 3548 char *exec_file; 3549 int signo = 0; 3550 3551 if (from_tty) 3552 { 3553 exec_file = get_exec_file (0); 3554 if (exec_file == 0) 3555 exec_file = ""; 3556 printf_filtered ("Detaching from program: %s %s\n", 3557 exec_file, target_pid_to_str (inferior_ptid)); 3558 fflush (stdout); 3559 } 3560 if (args) 3561 signo = atoi (args); 3562 3563 do_detach (signo); 3564 inferior_ptid = null_ptid; 3565 unpush_target (&procfs_ops); /* Pop out of handling an inferior */ 3566} 3567 3568static ptid_t 3569do_attach (ptid_t ptid) 3570{ 3571 procinfo *pi; 3572 int fail; 3573 3574 if ((pi = create_procinfo (PIDGET (ptid), 0)) == NULL) 3575 perror ("procfs: out of memory in 'attach'"); 3576 3577 if (!open_procinfo_files (pi, FD_CTL)) 3578 { 3579 fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__); 3580 sprintf (errmsg, "do_attach: couldn't open /proc file for process %d", 3581 PIDGET (ptid)); 3582 dead_procinfo (pi, errmsg, NOKILL); 3583 } 3584 3585 /* Stop the process (if it isn't already stopped). */ 3586 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) 3587 { 3588 pi->was_stopped = 1; 3589 proc_prettyprint_why (proc_why (pi), proc_what (pi), 1); 3590 } 3591 else 3592 { 3593 pi->was_stopped = 0; 3594 /* Set the process to run again when we close it. */ 3595 if (!proc_set_run_on_last_close (pi)) 3596 dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL); 3597 3598 /* Now stop the process. */ 3599 if (!proc_stop_process (pi)) 3600 dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL); 3601 pi->ignore_next_sigstop = 1; 3602 } 3603 /* Save some of the /proc state to be restored if we detach. */ 3604 if (!proc_get_traced_faults (pi, &pi->saved_fltset)) 3605 dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL); 3606 if (!proc_get_traced_signals (pi, &pi->saved_sigset)) 3607 dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL); 3608 if (!proc_get_traced_sysentry (pi, pi->saved_entryset)) 3609 dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.", 3610 NOKILL); 3611 if (!proc_get_traced_sysexit (pi, pi->saved_exitset)) 3612 dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.", 3613 NOKILL); 3614 if (!proc_get_held_signals (pi, &pi->saved_sighold)) 3615 dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL); 3616 3617 if ((fail = procfs_debug_inferior (pi)) != 0) 3618 dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL); 3619 3620 /* Let GDB know that the inferior was attached. */ 3621 attach_flag = 1; 3622 return MERGEPID (pi->pid, proc_get_current_thread (pi)); 3623} 3624 3625static void 3626do_detach (int signo) 3627{ 3628 procinfo *pi; 3629 3630 /* Find procinfo for the main process */ 3631 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); /* FIXME: threads */ 3632 if (signo) 3633 if (!proc_set_current_signal (pi, signo)) 3634 proc_warn (pi, "do_detach, set_current_signal", __LINE__); 3635 3636 if (!proc_set_traced_signals (pi, &pi->saved_sigset)) 3637 proc_warn (pi, "do_detach, set_traced_signal", __LINE__); 3638 3639 if (!proc_set_traced_faults (pi, &pi->saved_fltset)) 3640 proc_warn (pi, "do_detach, set_traced_faults", __LINE__); 3641 3642 if (!proc_set_traced_sysentry (pi, pi->saved_entryset)) 3643 proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__); 3644 3645 if (!proc_set_traced_sysexit (pi, pi->saved_exitset)) 3646 proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__); 3647 3648 if (!proc_set_held_signals (pi, &pi->saved_sighold)) 3649 proc_warn (pi, "do_detach, set_held_signals", __LINE__); 3650 3651 if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))) 3652 if (signo || !(pi->was_stopped) || 3653 query ("Was stopped when attached, make it runnable again? ")) 3654 { 3655 /* Clear any pending signal. */ 3656 if (!proc_clear_current_fault (pi)) 3657 proc_warn (pi, "do_detach, clear_current_fault", __LINE__); 3658 3659 if (signo == 0 && !proc_clear_current_signal (pi)) 3660 proc_warn (pi, "do_detach, clear_current_signal", __LINE__); 3661 3662 if (!proc_set_run_on_last_close (pi)) 3663 proc_warn (pi, "do_detach, set_rlc", __LINE__); 3664 } 3665 3666 attach_flag = 0; 3667 destroy_procinfo (pi); 3668} 3669 3670/* 3671 * fetch_registers 3672 * 3673 * Since the /proc interface cannot give us individual registers, 3674 * we pay no attention to the (regno) argument, and just fetch them all. 3675 * This results in the possibility that we will do unnecessarily many 3676 * fetches, since we may be called repeatedly for individual registers. 3677 * So we cache the results, and mark the cache invalid when the process 3678 * is resumed. 3679 */ 3680 3681static void 3682procfs_fetch_registers (int regno) 3683{ 3684 gdb_fpregset_t *fpregs; 3685 gdb_gregset_t *gregs; 3686 procinfo *pi; 3687 int pid; 3688 int tid; 3689 3690 pid = PIDGET (inferior_ptid); 3691 tid = TIDGET (inferior_ptid); 3692 3693 /* First look up procinfo for the main process. */ 3694 pi = find_procinfo_or_die (pid, 0); 3695 3696 /* If the event thread is not the same as GDB's requested thread 3697 (ie. inferior_ptid), then look up procinfo for the requested 3698 thread. */ 3699 if ((tid != 0) && 3700 (tid != proc_get_current_thread (pi))) 3701 pi = find_procinfo_or_die (pid, tid); 3702 3703 if (pi == NULL) 3704 error ("procfs: fetch_registers failed to find procinfo for %s", 3705 target_pid_to_str (inferior_ptid)); 3706 3707 if ((gregs = proc_get_gregs (pi)) == NULL) 3708 proc_error (pi, "fetch_registers, get_gregs", __LINE__); 3709 3710 supply_gregset (gregs); 3711 3712 if (FP0_REGNUM >= 0) /* need floating point? */ 3713 { 3714 if ((regno >= 0 && regno < FP0_REGNUM) 3715 || regno == PC_REGNUM 3716 || regno == DEPRECATED_FP_REGNUM 3717 || regno == SP_REGNUM) 3718 return; /* not a floating point register */ 3719 3720 if ((fpregs = proc_get_fpregs (pi)) == NULL) 3721 proc_error (pi, "fetch_registers, get_fpregs", __LINE__); 3722 3723 supply_fpregset (fpregs); 3724 } 3725} 3726 3727/* Get ready to modify the registers array. On machines which store 3728 individual registers, this doesn't need to do anything. On 3729 machines which store all the registers in one fell swoop, such as 3730 /proc, this makes sure that registers contains all the registers 3731 from the program being debugged. */ 3732 3733static void 3734procfs_prepare_to_store (void) 3735{ 3736#ifdef CHILD_PREPARE_TO_STORE 3737 CHILD_PREPARE_TO_STORE (); 3738#endif 3739} 3740 3741/* 3742 * store_registers 3743 * 3744 * Since the /proc interface will not read individual registers, 3745 * we will cache these requests until the process is resumed, and 3746 * only then write them back to the inferior process. 3747 * 3748 * FIXME: is that a really bad idea? Have to think about cases 3749 * where writing one register might affect the value of others, etc. 3750 */ 3751 3752static void 3753procfs_store_registers (int regno) 3754{ 3755 gdb_fpregset_t *fpregs; 3756 gdb_gregset_t *gregs; 3757 procinfo *pi; 3758 int pid; 3759 int tid; 3760 3761 pid = PIDGET (inferior_ptid); 3762 tid = TIDGET (inferior_ptid); 3763 3764 /* First find procinfo for main process */ 3765 pi = find_procinfo_or_die (pid, 0); 3766 3767 /* If current lwp for process is not the same as requested thread 3768 (ie. inferior_ptid), then find procinfo for the requested thread. */ 3769 3770 if ((tid != 0) && 3771 (tid != proc_get_current_thread (pi))) 3772 pi = find_procinfo_or_die (pid, tid); 3773 3774 if (pi == NULL) 3775 error ("procfs: store_registers: failed to find procinfo for %s", 3776 target_pid_to_str (inferior_ptid)); 3777 3778 if ((gregs = proc_get_gregs (pi)) == NULL) 3779 proc_error (pi, "store_registers, get_gregs", __LINE__); 3780 3781 fill_gregset (gregs, regno); 3782 if (!proc_set_gregs (pi)) 3783 proc_error (pi, "store_registers, set_gregs", __LINE__); 3784 3785 if (FP0_REGNUM >= 0) /* need floating point? */ 3786 { 3787 if ((regno >= 0 && regno < FP0_REGNUM) 3788 || regno == PC_REGNUM 3789 || regno == DEPRECATED_FP_REGNUM 3790 || regno == SP_REGNUM) 3791 return; /* not a floating point register */ 3792 3793 if ((fpregs = proc_get_fpregs (pi)) == NULL) 3794 proc_error (pi, "store_registers, get_fpregs", __LINE__); 3795 3796 fill_fpregset (fpregs, regno); 3797 if (!proc_set_fpregs (pi)) 3798 proc_error (pi, "store_registers, set_fpregs", __LINE__); 3799 } 3800} 3801 3802static int 3803syscall_is_lwp_exit (procinfo *pi, int scall) 3804{ 3805 3806#ifdef SYS_lwp_exit 3807 if (scall == SYS_lwp_exit) 3808 return 1; 3809#endif 3810#ifdef SYS_lwpexit 3811 if (scall == SYS_lwpexit) 3812 return 1; 3813#endif 3814 return 0; 3815} 3816 3817static int 3818syscall_is_exit (procinfo *pi, int scall) 3819{ 3820#ifdef SYS_exit 3821 if (scall == SYS_exit) 3822 return 1; 3823#endif 3824#ifdef DYNAMIC_SYSCALLS 3825 if (find_syscall (pi, "_exit") == scall) 3826 return 1; 3827#endif 3828 return 0; 3829} 3830 3831static int 3832syscall_is_exec (procinfo *pi, int scall) 3833{ 3834#ifdef SYS_exec 3835 if (scall == SYS_exec) 3836 return 1; 3837#endif 3838#ifdef SYS_execv 3839 if (scall == SYS_execv) 3840 return 1; 3841#endif 3842#ifdef SYS_execve 3843 if (scall == SYS_execve) 3844 return 1; 3845#endif 3846#ifdef DYNAMIC_SYSCALLS 3847 if (find_syscall (pi, "_execve")) 3848 return 1; 3849 if (find_syscall (pi, "ra_execve")) 3850 return 1; 3851#endif 3852 return 0; 3853} 3854 3855static int 3856syscall_is_lwp_create (procinfo *pi, int scall) 3857{ 3858#ifdef SYS_lwp_create 3859 if (scall == SYS_lwp_create) 3860 return 1; 3861#endif 3862#ifdef SYS_lwpcreate 3863 if (scall == SYS_lwpcreate) 3864 return 1; 3865#endif 3866 return 0; 3867} 3868 3869/* 3870 * Function: target_wait 3871 * 3872 * Retrieve the next stop event from the child process. 3873 * If child has not stopped yet, wait for it to stop. 3874 * Translate /proc eventcodes (or possibly wait eventcodes) 3875 * into gdb internal event codes. 3876 * 3877 * Return: id of process (and possibly thread) that incurred the event. 3878 * event codes are returned thru a pointer parameter. 3879 */ 3880 3881static ptid_t 3882procfs_wait (ptid_t ptid, struct target_waitstatus *status) 3883{ 3884 /* First cut: loosely based on original version 2.1 */ 3885 procinfo *pi; 3886 int wstat; 3887 int temp_tid; 3888 ptid_t retval, temp_ptid; 3889 int why, what, flags; 3890 int retry = 0; 3891 3892wait_again: 3893 3894 retry++; 3895 wstat = 0; 3896 retval = pid_to_ptid (-1); 3897 3898 /* Find procinfo for main process */ 3899 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); 3900 if (pi) 3901 { 3902 /* We must assume that the status is stale now... */ 3903 pi->status_valid = 0; 3904 pi->gregs_valid = 0; 3905 pi->fpregs_valid = 0; 3906 3907#if 0 /* just try this out... */ 3908 flags = proc_flags (pi); 3909 why = proc_why (pi); 3910 if ((flags & PR_STOPPED) && (why == PR_REQUESTED)) 3911 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */ 3912#endif 3913 /* If child is not stopped, wait for it to stop. */ 3914 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) && 3915 !proc_wait_for_stop (pi)) 3916 { 3917 /* wait_for_stop failed: has the child terminated? */ 3918 if (errno == ENOENT) 3919 { 3920 int wait_retval; 3921 3922 /* /proc file not found; presumably child has terminated. */ 3923 wait_retval = wait (&wstat); /* "wait" for the child's exit */ 3924 3925 if (wait_retval != PIDGET (inferior_ptid)) /* wrong child? */ 3926 error ("procfs: couldn't stop process %d: wait returned %d\n", 3927 PIDGET (inferior_ptid), wait_retval); 3928 /* FIXME: might I not just use waitpid? 3929 Or try find_procinfo to see if I know about this child? */ 3930 retval = pid_to_ptid (wait_retval); 3931 } 3932 else if (errno == EINTR) 3933 goto wait_again; 3934 else 3935 { 3936 /* Unknown error from wait_for_stop. */ 3937 proc_error (pi, "target_wait (wait_for_stop)", __LINE__); 3938 } 3939 } 3940 else 3941 { 3942 /* This long block is reached if either: 3943 a) the child was already stopped, or 3944 b) we successfully waited for the child with wait_for_stop. 3945 This block will analyze the /proc status, and translate it 3946 into a waitstatus for GDB. 3947 3948 If we actually had to call wait because the /proc file 3949 is gone (child terminated), then we skip this block, 3950 because we already have a waitstatus. */ 3951 3952 flags = proc_flags (pi); 3953 why = proc_why (pi); 3954 what = proc_what (pi); 3955 3956 if (flags & (PR_STOPPED | PR_ISTOP)) 3957 { 3958#ifdef PR_ASYNC 3959 /* If it's running async (for single_thread control), 3960 set it back to normal again. */ 3961 if (flags & PR_ASYNC) 3962 if (!proc_unset_async (pi)) 3963 proc_error (pi, "target_wait, unset_async", __LINE__); 3964#endif 3965 3966 if (info_verbose) 3967 proc_prettyprint_why (why, what, 1); 3968 3969 /* The 'pid' we will return to GDB is composed of 3970 the process ID plus the lwp ID. */ 3971 retval = MERGEPID (pi->pid, proc_get_current_thread (pi)); 3972 3973 switch (why) { 3974 case PR_SIGNALLED: 3975 wstat = (what << 8) | 0177; 3976 break; 3977 case PR_SYSENTRY: 3978 if (syscall_is_lwp_exit (pi, what)) 3979 { 3980 printf_filtered ("[%s exited]\n", 3981 target_pid_to_str (retval)); 3982 delete_thread (retval); 3983 status->kind = TARGET_WAITKIND_SPURIOUS; 3984 return retval; 3985 } 3986 else if (syscall_is_exit (pi, what)) 3987 { 3988 /* Handle SYS_exit call only */ 3989 /* Stopped at entry to SYS_exit. 3990 Make it runnable, resume it, then use 3991 the wait system call to get its exit code. 3992 Proc_run_process always clears the current 3993 fault and signal. 3994 Then return its exit status. */ 3995 pi->status_valid = 0; 3996 wstat = 0; 3997 /* FIXME: what we should do is return 3998 TARGET_WAITKIND_SPURIOUS. */ 3999 if (!proc_run_process (pi, 0, 0)) 4000 proc_error (pi, "target_wait, run_process", __LINE__); 4001 if (attach_flag) 4002 { 4003 /* Don't call wait: simulate waiting for exit, 4004 return a "success" exit code. Bogus: what if 4005 it returns something else? */ 4006 wstat = 0; 4007 retval = inferior_ptid; /* ? ? ? */ 4008 } 4009 else 4010 { 4011 int temp = wait (&wstat); 4012 4013 /* FIXME: shouldn't I make sure I get the right 4014 event from the right process? If (for 4015 instance) I have killed an earlier inferior 4016 process but failed to clean up after it 4017 somehow, I could get its termination event 4018 here. */ 4019 4020 /* If wait returns -1, that's what we return to GDB. */ 4021 if (temp < 0) 4022 retval = pid_to_ptid (temp); 4023 } 4024 } 4025 else 4026 { 4027 printf_filtered ("procfs: trapped on entry to "); 4028 proc_prettyprint_syscall (proc_what (pi), 0); 4029 printf_filtered ("\n"); 4030#ifndef PIOCSSPCACT 4031 { 4032 long i, nsysargs, *sysargs; 4033 4034 if ((nsysargs = proc_nsysarg (pi)) > 0 && 4035 (sysargs = proc_sysargs (pi)) != NULL) 4036 { 4037 printf_filtered ("%ld syscall arguments:\n", nsysargs); 4038 for (i = 0; i < nsysargs; i++) 4039 printf_filtered ("#%ld: 0x%08lx\n", 4040 i, sysargs[i]); 4041 } 4042 4043 } 4044#endif 4045 if (status) 4046 { 4047 /* How to exit gracefully, returning "unknown event" */ 4048 status->kind = TARGET_WAITKIND_SPURIOUS; 4049 return inferior_ptid; 4050 } 4051 else 4052 { 4053 /* How to keep going without returning to wfi: */ 4054 target_resume (ptid, 0, TARGET_SIGNAL_0); 4055 goto wait_again; 4056 } 4057 } 4058 break; 4059 case PR_SYSEXIT: 4060 if (syscall_is_exec (pi, what)) 4061 { 4062 /* Hopefully this is our own "fork-child" execing 4063 the real child. Hoax this event into a trap, and 4064 GDB will see the child about to execute its start 4065 address. */ 4066 wstat = (SIGTRAP << 8) | 0177; 4067 } 4068 else if (syscall_is_lwp_create (pi, what)) 4069 { 4070 /* 4071 * This syscall is somewhat like fork/exec. 4072 * We will get the event twice: once for the parent LWP, 4073 * and once for the child. We should already know about 4074 * the parent LWP, but the child will be new to us. So, 4075 * whenever we get this event, if it represents a new 4076 * thread, simply add the thread to the list. 4077 */ 4078 4079 /* If not in procinfo list, add it. */ 4080 temp_tid = proc_get_current_thread (pi); 4081 if (!find_procinfo (pi->pid, temp_tid)) 4082 create_procinfo (pi->pid, temp_tid); 4083 4084 temp_ptid = MERGEPID (pi->pid, temp_tid); 4085 /* If not in GDB's thread list, add it. */ 4086 if (!in_thread_list (temp_ptid)) 4087 { 4088 printf_filtered ("[New %s]\n", 4089 target_pid_to_str (temp_ptid)); 4090 add_thread (temp_ptid); 4091 } 4092 /* Return to WFI, but tell it to immediately resume. */ 4093 status->kind = TARGET_WAITKIND_SPURIOUS; 4094 return inferior_ptid; 4095 } 4096 else if (syscall_is_lwp_exit (pi, what)) 4097 { 4098 printf_filtered ("[%s exited]\n", 4099 target_pid_to_str (retval)); 4100 delete_thread (retval); 4101 status->kind = TARGET_WAITKIND_SPURIOUS; 4102 return retval; 4103 } 4104 else if (0) 4105 { 4106 /* FIXME: Do we need to handle SYS_sproc, 4107 SYS_fork, or SYS_vfork here? The old procfs 4108 seemed to use this event to handle threads on 4109 older (non-LWP) systems, where I'm assuming 4110 that threads were actually separate processes. 4111 Irix, maybe? Anyway, low priority for now. */ 4112 } 4113 else 4114 { 4115 printf_filtered ("procfs: trapped on exit from "); 4116 proc_prettyprint_syscall (proc_what (pi), 0); 4117 printf_filtered ("\n"); 4118#ifndef PIOCSSPCACT 4119 { 4120 long i, nsysargs, *sysargs; 4121 4122 if ((nsysargs = proc_nsysarg (pi)) > 0 && 4123 (sysargs = proc_sysargs (pi)) != NULL) 4124 { 4125 printf_filtered ("%ld syscall arguments:\n", nsysargs); 4126 for (i = 0; i < nsysargs; i++) 4127 printf_filtered ("#%ld: 0x%08lx\n", 4128 i, sysargs[i]); 4129 } 4130 } 4131#endif 4132 status->kind = TARGET_WAITKIND_SPURIOUS; 4133 return inferior_ptid; 4134 } 4135 break; 4136 case PR_REQUESTED: 4137#if 0 /* FIXME */ 4138 wstat = (SIGSTOP << 8) | 0177; 4139 break; 4140#else 4141 if (retry < 5) 4142 { 4143 printf_filtered ("Retry #%d:\n", retry); 4144 pi->status_valid = 0; 4145 goto wait_again; 4146 } 4147 else 4148 { 4149 /* If not in procinfo list, add it. */ 4150 temp_tid = proc_get_current_thread (pi); 4151 if (!find_procinfo (pi->pid, temp_tid)) 4152 create_procinfo (pi->pid, temp_tid); 4153 4154 /* If not in GDB's thread list, add it. */ 4155 temp_ptid = MERGEPID (pi->pid, temp_tid); 4156 if (!in_thread_list (temp_ptid)) 4157 { 4158 printf_filtered ("[New %s]\n", 4159 target_pid_to_str (temp_ptid)); 4160 add_thread (temp_ptid); 4161 } 4162 4163 status->kind = TARGET_WAITKIND_STOPPED; 4164 status->value.sig = 0; 4165 return retval; 4166 } 4167#endif 4168 case PR_JOBCONTROL: 4169 wstat = (what << 8) | 0177; 4170 break; 4171 case PR_FAULTED: 4172 switch (what) { 4173#ifdef FLTWATCH 4174 case FLTWATCH: 4175 wstat = (SIGTRAP << 8) | 0177; 4176 break; 4177#endif 4178#ifdef FLTKWATCH 4179 case FLTKWATCH: 4180 wstat = (SIGTRAP << 8) | 0177; 4181 break; 4182#endif 4183 /* FIXME: use si_signo where possible. */ 4184 case FLTPRIV: 4185#if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */ 4186 case FLTILL: 4187#endif 4188 wstat = (SIGILL << 8) | 0177; 4189 break; 4190 case FLTBPT: 4191#if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */ 4192 case FLTTRACE: 4193#endif 4194 wstat = (SIGTRAP << 8) | 0177; 4195 break; 4196 case FLTSTACK: 4197 case FLTACCESS: 4198#if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */ 4199 case FLTBOUNDS: 4200#endif 4201 wstat = (SIGSEGV << 8) | 0177; 4202 break; 4203 case FLTIOVF: 4204 case FLTIZDIV: 4205#if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */ 4206 case FLTFPE: 4207#endif 4208 wstat = (SIGFPE << 8) | 0177; 4209 break; 4210 case FLTPAGE: /* Recoverable page fault */ 4211 default: /* FIXME: use si_signo if possible for fault */ 4212 retval = pid_to_ptid (-1); 4213 printf_filtered ("procfs:%d -- ", __LINE__); 4214 printf_filtered ("child stopped for unknown reason:\n"); 4215 proc_prettyprint_why (why, what, 1); 4216 error ("... giving up..."); 4217 break; 4218 } 4219 break; /* case PR_FAULTED: */ 4220 default: /* switch (why) unmatched */ 4221 printf_filtered ("procfs:%d -- ", __LINE__); 4222 printf_filtered ("child stopped for unknown reason:\n"); 4223 proc_prettyprint_why (why, what, 1); 4224 error ("... giving up..."); 4225 break; 4226 } 4227 /* 4228 * Got this far without error: 4229 * If retval isn't in the threads database, add it. 4230 */ 4231 if (PIDGET (retval) > 0 && 4232 !ptid_equal (retval, inferior_ptid) && 4233 !in_thread_list (retval)) 4234 { 4235 /* 4236 * We have a new thread. 4237 * We need to add it both to GDB's list and to our own. 4238 * If we don't create a procinfo, resume may be unhappy 4239 * later. 4240 */ 4241 printf_filtered ("[New %s]\n", target_pid_to_str (retval)); 4242 add_thread (retval); 4243 if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL) 4244 create_procinfo (PIDGET (retval), TIDGET (retval)); 4245 4246 /* In addition, it's possible that this is the first 4247 * new thread we've seen, in which case we may not 4248 * have created entries for inferior_ptid yet. 4249 */ 4250 if (TIDGET (inferior_ptid) != 0) 4251 { 4252 if (!in_thread_list (inferior_ptid)) 4253 add_thread (inferior_ptid); 4254 if (find_procinfo (PIDGET (inferior_ptid), 4255 TIDGET (inferior_ptid)) == NULL) 4256 create_procinfo (PIDGET (inferior_ptid), 4257 TIDGET (inferior_ptid)); 4258 } 4259 } 4260 } 4261 else /* flags do not indicate STOPPED */ 4262 { 4263 /* surely this can't happen... */ 4264 printf_filtered ("procfs:%d -- process not stopped.\n", 4265 __LINE__); 4266 proc_prettyprint_flags (flags, 1); 4267 error ("procfs: ...giving up..."); 4268 } 4269 } 4270 4271 if (status) 4272 store_waitstatus (status, wstat); 4273 } 4274 4275 return retval; 4276} 4277 4278/* Perform a partial transfer to/from the specified object. For 4279 memory transfers, fall back to the old memory xfer functions. */ 4280 4281static LONGEST 4282procfs_xfer_partial (struct target_ops *ops, enum target_object object, 4283 const char *annex, void *readbuf, 4284 const void *writebuf, ULONGEST offset, LONGEST len) 4285{ 4286 switch (object) 4287 { 4288 case TARGET_OBJECT_MEMORY: 4289 if (readbuf) 4290 return (*ops->to_xfer_memory) (offset, readbuf, len, 0/*write*/, 4291 NULL, ops); 4292 if (writebuf) 4293 return (*ops->to_xfer_memory) (offset, readbuf, len, 1/*write*/, 4294 NULL, ops); 4295 return -1; 4296 4297#ifdef NEW_PROC_API 4298 case TARGET_OBJECT_AUXV: 4299 return procfs_xfer_auxv (ops, object, annex, readbuf, writebuf, 4300 offset, len); 4301#endif 4302 4303 default: 4304 if (ops->beneath != NULL) 4305 return ops->beneath->to_xfer_partial (ops->beneath, object, annex, 4306 readbuf, writebuf, offset, len); 4307 return -1; 4308 } 4309} 4310 4311 4312/* Transfer LEN bytes between GDB address MYADDR and target address 4313 MEMADDR. If DOWRITE is non-zero, transfer them to the target, 4314 otherwise transfer them from the target. TARGET is unused. 4315 4316 The return value is 0 if an error occurred or no bytes were 4317 transferred. Otherwise, it will be a positive value which 4318 indicates the number of bytes transferred between gdb and the 4319 target. (Note that the interface also makes provisions for 4320 negative values, but this capability isn't implemented here.) */ 4321 4322static int 4323procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite, 4324 struct mem_attrib *attrib, struct target_ops *target) 4325{ 4326 procinfo *pi; 4327 int nbytes = 0; 4328 4329 /* Find procinfo for main process */ 4330 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); 4331 if (pi->as_fd == 0 && 4332 open_procinfo_files (pi, FD_AS) == 0) 4333 { 4334 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__); 4335 return 0; 4336 } 4337 4338 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr) 4339 { 4340 if (dowrite) 4341 { 4342#ifdef NEW_PROC_API 4343 PROCFS_NOTE ("write memory: "); 4344#else 4345 PROCFS_NOTE ("write memory: \n"); 4346#endif 4347 nbytes = write (pi->as_fd, myaddr, len); 4348 } 4349 else 4350 { 4351 PROCFS_NOTE ("read memory: \n"); 4352 nbytes = read (pi->as_fd, myaddr, len); 4353 } 4354 if (nbytes < 0) 4355 { 4356 nbytes = 0; 4357 } 4358 } 4359 return nbytes; 4360} 4361 4362/* 4363 * Function: invalidate_cache 4364 * 4365 * Called by target_resume before making child runnable. 4366 * Mark cached registers and status's invalid. 4367 * If there are "dirty" caches that need to be written back 4368 * to the child process, do that. 4369 * 4370 * File descriptors are also cached. 4371 * As they are a limited resource, we cannot hold onto them indefinitely. 4372 * However, as they are expensive to open, we don't want to throw them 4373 * away indescriminately either. As a compromise, we will keep the 4374 * file descriptors for the parent process, but discard any file 4375 * descriptors we may have accumulated for the threads. 4376 * 4377 * Return value: 4378 * As this function is called by iterate_over_threads, it always 4379 * returns zero (so that iterate_over_threads will keep iterating). 4380 */ 4381 4382 4383static int 4384invalidate_cache (procinfo *parent, procinfo *pi, void *ptr) 4385{ 4386 /* 4387 * About to run the child; invalidate caches and do any other cleanup. 4388 */ 4389 4390#if 0 4391 if (pi->gregs_dirty) 4392 if (parent == NULL || 4393 proc_get_current_thread (parent) != pi->tid) 4394 if (!proc_set_gregs (pi)) /* flush gregs cache */ 4395 proc_warn (pi, "target_resume, set_gregs", 4396 __LINE__); 4397 if (FP0_REGNUM >= 0) 4398 if (pi->fpregs_dirty) 4399 if (parent == NULL || 4400 proc_get_current_thread (parent) != pi->tid) 4401 if (!proc_set_fpregs (pi)) /* flush fpregs cache */ 4402 proc_warn (pi, "target_resume, set_fpregs", 4403 __LINE__); 4404#endif 4405 4406 if (parent != NULL) 4407 { 4408 /* The presence of a parent indicates that this is an LWP. 4409 Close any file descriptors that it might have open. 4410 We don't do this to the master (parent) procinfo. */ 4411 4412 close_procinfo_files (pi); 4413 } 4414 pi->gregs_valid = 0; 4415 pi->fpregs_valid = 0; 4416#if 0 4417 pi->gregs_dirty = 0; 4418 pi->fpregs_dirty = 0; 4419#endif 4420 pi->status_valid = 0; 4421 pi->threads_valid = 0; 4422 4423 return 0; 4424} 4425 4426#if 0 4427/* 4428 * Function: make_signal_thread_runnable 4429 * 4430 * A callback function for iterate_over_threads. 4431 * Find the asynchronous signal thread, and make it runnable. 4432 * See if that helps matters any. 4433 */ 4434 4435static int 4436make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr) 4437{ 4438#ifdef PR_ASLWP 4439 if (proc_flags (pi) & PR_ASLWP) 4440 { 4441 if (!proc_run_process (pi, 0, -1)) 4442 proc_error (pi, "make_signal_thread_runnable", __LINE__); 4443 return 1; 4444 } 4445#endif 4446 return 0; 4447} 4448#endif 4449 4450/* 4451 * Function: target_resume 4452 * 4453 * Make the child process runnable. Normally we will then call 4454 * procfs_wait and wait for it to stop again (unles gdb is async). 4455 * 4456 * Arguments: 4457 * step: if true, then arrange for the child to stop again 4458 * after executing a single instruction. 4459 * signo: if zero, then cancel any pending signal. 4460 * If non-zero, then arrange for the indicated signal 4461 * to be delivered to the child when it runs. 4462 * pid: if -1, then allow any child thread to run. 4463 * if non-zero, then allow only the indicated thread to run. 4464 ******* (not implemented yet) 4465 */ 4466 4467static void 4468procfs_resume (ptid_t ptid, int step, enum target_signal signo) 4469{ 4470 procinfo *pi, *thread; 4471 int native_signo; 4472 4473 /* 2.1: 4474 prrun.prflags |= PRSVADDR; 4475 prrun.pr_vaddr = $PC; set resume address 4476 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all) 4477 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE) 4478 prrun.prflags |= PRCFAULT; clear current fault. 4479 4480 PRSTRACE and PRSFAULT can be done by other means 4481 (proc_trace_signals, proc_trace_faults) 4482 PRSVADDR is unnecessary. 4483 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault) 4484 This basically leaves PRSTEP and PRCSIG. 4485 PRCSIG is like PIOCSSIG (proc_clear_current_signal). 4486 So basically PR_STEP is the sole argument that must be passed 4487 to proc_run_process (for use in the prrun struct by ioctl). */ 4488 4489 /* Find procinfo for main process */ 4490 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); 4491 4492 /* First cut: ignore pid argument */ 4493 errno = 0; 4494 4495 /* Convert signal to host numbering. */ 4496 if (signo == 0 || 4497 (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop)) 4498 native_signo = 0; 4499 else 4500 native_signo = target_signal_to_host (signo); 4501 4502 pi->ignore_next_sigstop = 0; 4503 4504 /* Running the process voids all cached registers and status. */ 4505 /* Void the threads' caches first */ 4506 proc_iterate_over_threads (pi, invalidate_cache, NULL); 4507 /* Void the process procinfo's caches. */ 4508 invalidate_cache (NULL, pi, NULL); 4509 4510 if (PIDGET (ptid) != -1) 4511 { 4512 /* Resume a specific thread, presumably suppressing the others. */ 4513 thread = find_procinfo (PIDGET (ptid), TIDGET (ptid)); 4514 if (thread != NULL) 4515 { 4516 if (thread->tid != 0) 4517 { 4518 /* We're to resume a specific thread, and not the others. 4519 * Set the child process's PR_ASYNC flag. 4520 */ 4521#ifdef PR_ASYNC 4522 if (!proc_set_async (pi)) 4523 proc_error (pi, "target_resume, set_async", __LINE__); 4524#endif 4525#if 0 4526 proc_iterate_over_threads (pi, 4527 make_signal_thread_runnable, 4528 NULL); 4529#endif 4530 pi = thread; /* substitute the thread's procinfo for run */ 4531 } 4532 } 4533 } 4534 4535 if (!proc_run_process (pi, step, native_signo)) 4536 { 4537 if (errno == EBUSY) 4538 warning ("resume: target already running. Pretend to resume, and hope for the best!\n"); 4539 else 4540 proc_error (pi, "target_resume", __LINE__); 4541 } 4542} 4543 4544/* 4545 * Function: register_gdb_signals 4546 * 4547 * Traverse the list of signals that GDB knows about 4548 * (see "handle" command), and arrange for the target 4549 * to be stopped or not, according to these settings. 4550 * 4551 * Returns non-zero for success, zero for failure. 4552 */ 4553 4554static int 4555register_gdb_signals (procinfo *pi, gdb_sigset_t *signals) 4556{ 4557 int signo; 4558 4559 for (signo = 0; signo < NSIG; signo ++) 4560 if (signal_stop_state (target_signal_from_host (signo)) == 0 && 4561 signal_print_state (target_signal_from_host (signo)) == 0 && 4562 signal_pass_state (target_signal_from_host (signo)) == 1) 4563 prdelset (signals, signo); 4564 else 4565 praddset (signals, signo); 4566 4567 return proc_set_traced_signals (pi, signals); 4568} 4569 4570/* 4571 * Function: target_notice_signals 4572 * 4573 * Set up to trace signals in the child process. 4574 */ 4575 4576static void 4577procfs_notice_signals (ptid_t ptid) 4578{ 4579 gdb_sigset_t signals; 4580 procinfo *pi = find_procinfo_or_die (PIDGET (ptid), 0); 4581 4582 if (proc_get_traced_signals (pi, &signals) && 4583 register_gdb_signals (pi, &signals)) 4584 return; 4585 else 4586 proc_error (pi, "notice_signals", __LINE__); 4587} 4588 4589/* 4590 * Function: target_files_info 4591 * 4592 * Print status information about the child process. 4593 */ 4594 4595static void 4596procfs_files_info (struct target_ops *ignore) 4597{ 4598 printf_filtered ("\tUsing the running image of %s %s via /proc.\n", 4599 attach_flag? "attached": "child", 4600 target_pid_to_str (inferior_ptid)); 4601} 4602 4603/* 4604 * Function: target_open 4605 * 4606 * A dummy: you don't open procfs. 4607 */ 4608 4609static void 4610procfs_open (char *args, int from_tty) 4611{ 4612 error ("Use the \"run\" command to start a Unix child process."); 4613} 4614 4615/* 4616 * Function: target_can_run 4617 * 4618 * This tells GDB that this target vector can be invoked 4619 * for "run" or "attach". 4620 */ 4621 4622int procfs_suppress_run = 0; /* Non-zero if procfs should pretend not to 4623 be a runnable target. Used by targets 4624 that can sit atop procfs, such as solaris 4625 thread support. */ 4626 4627 4628static int 4629procfs_can_run (void) 4630{ 4631 /* This variable is controlled by modules that sit atop procfs that 4632 may layer their own process structure atop that provided here. 4633 sol-thread.c does this because of the Solaris two-level thread 4634 model. */ 4635 4636 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */ 4637 4638 return !procfs_suppress_run; 4639} 4640 4641/* 4642 * Function: target_stop 4643 * 4644 * Stop the child process asynchronously, as when the 4645 * gdb user types control-c or presses a "stop" button. 4646 * 4647 * Works by sending kill(SIGINT) to the child's process group. 4648 */ 4649 4650static void 4651procfs_stop (void) 4652{ 4653 kill (-inferior_process_group, SIGINT); 4654} 4655 4656/* 4657 * Function: unconditionally_kill_inferior 4658 * 4659 * Make it die. Wait for it to die. Clean up after it. 4660 * Note: this should only be applied to the real process, 4661 * not to an LWP, because of the check for parent-process. 4662 * If we need this to work for an LWP, it needs some more logic. 4663 */ 4664 4665static void 4666unconditionally_kill_inferior (procinfo *pi) 4667{ 4668 int parent_pid; 4669 4670 parent_pid = proc_parent_pid (pi); 4671#ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL 4672 /* FIXME: use access functions */ 4673 /* Alpha OSF/1-3.x procfs needs a clear of the current signal 4674 before the PIOCKILL, otherwise it might generate a corrupted core 4675 file for the inferior. */ 4676 if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0) 4677 { 4678 printf_filtered ("unconditionally_kill: SSIG failed!\n"); 4679 } 4680#endif 4681#ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL 4682 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal 4683 to kill the inferior, otherwise it might remain stopped with a 4684 pending SIGKILL. 4685 We do not check the result of the PIOCSSIG, the inferior might have 4686 died already. */ 4687 { 4688 gdb_siginfo_t newsiginfo; 4689 4690 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo)); 4691 newsiginfo.si_signo = SIGKILL; 4692 newsiginfo.si_code = 0; 4693 newsiginfo.si_errno = 0; 4694 newsiginfo.si_pid = getpid (); 4695 newsiginfo.si_uid = getuid (); 4696 /* FIXME: use proc_set_current_signal */ 4697 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo); 4698 } 4699#else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */ 4700 if (!proc_kill (pi, SIGKILL)) 4701 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__); 4702#endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */ 4703 destroy_procinfo (pi); 4704 4705 /* If pi is GDB's child, wait for it to die. */ 4706 if (parent_pid == getpid ()) 4707 /* FIXME: should we use waitpid to make sure we get the right event? 4708 Should we check the returned event? */ 4709 { 4710#if 0 4711 int status, ret; 4712 4713 ret = waitpid (pi->pid, &status, 0); 4714#else 4715 wait (NULL); 4716#endif 4717 } 4718} 4719 4720/* 4721 * Function: target_kill_inferior 4722 * 4723 * We're done debugging it, and we want it to go away. 4724 * Then we want GDB to forget all about it. 4725 */ 4726 4727static void 4728procfs_kill_inferior (void) 4729{ 4730 if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */ 4731 { 4732 /* Find procinfo for main process */ 4733 procinfo *pi = find_procinfo (PIDGET (inferior_ptid), 0); 4734 4735 if (pi) 4736 unconditionally_kill_inferior (pi); 4737 target_mourn_inferior (); 4738 } 4739} 4740 4741/* 4742 * Function: target_mourn_inferior 4743 * 4744 * Forget we ever debugged this thing! 4745 */ 4746 4747static void 4748procfs_mourn_inferior (void) 4749{ 4750 procinfo *pi; 4751 4752 if (!ptid_equal (inferior_ptid, null_ptid)) 4753 { 4754 /* Find procinfo for main process */ 4755 pi = find_procinfo (PIDGET (inferior_ptid), 0); 4756 if (pi) 4757 destroy_procinfo (pi); 4758 } 4759 unpush_target (&procfs_ops); 4760 generic_mourn_inferior (); 4761} 4762 4763/* 4764 * Function: init_inferior 4765 * 4766 * When GDB forks to create a runnable inferior process, 4767 * this function is called on the parent side of the fork. 4768 * It's job is to do whatever is necessary to make the child 4769 * ready to be debugged, and then wait for the child to synchronize. 4770 */ 4771 4772static void 4773procfs_init_inferior (int pid) 4774{ 4775 procinfo *pi; 4776 gdb_sigset_t signals; 4777 int fail; 4778 4779 /* This routine called on the parent side (GDB side) 4780 after GDB forks the inferior. */ 4781 4782 push_target (&procfs_ops); 4783 4784 if ((pi = create_procinfo (pid, 0)) == NULL) 4785 perror ("procfs: out of memory in 'init_inferior'"); 4786 4787 if (!open_procinfo_files (pi, FD_CTL)) 4788 proc_error (pi, "init_inferior, open_proc_files", __LINE__); 4789 4790 /* 4791 xmalloc // done 4792 open_procinfo_files // done 4793 link list // done 4794 prfillset (trace) 4795 procfs_notice_signals 4796 prfillset (fault) 4797 prdelset (FLTPAGE) 4798 PIOCWSTOP 4799 PIOCSFAULT 4800 */ 4801 4802 /* If not stopped yet, wait for it to stop. */ 4803 if (!(proc_flags (pi) & PR_STOPPED) && 4804 !(proc_wait_for_stop (pi))) 4805 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL); 4806 4807 /* Save some of the /proc state to be restored if we detach. */ 4808 /* FIXME: Why? In case another debugger was debugging it? 4809 We're it's parent, for Ghu's sake! */ 4810 if (!proc_get_traced_signals (pi, &pi->saved_sigset)) 4811 proc_error (pi, "init_inferior, get_traced_signals", __LINE__); 4812 if (!proc_get_held_signals (pi, &pi->saved_sighold)) 4813 proc_error (pi, "init_inferior, get_held_signals", __LINE__); 4814 if (!proc_get_traced_faults (pi, &pi->saved_fltset)) 4815 proc_error (pi, "init_inferior, get_traced_faults", __LINE__); 4816 if (!proc_get_traced_sysentry (pi, pi->saved_entryset)) 4817 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__); 4818 if (!proc_get_traced_sysexit (pi, pi->saved_exitset)) 4819 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__); 4820 4821 /* Register to trace selected signals in the child. */ 4822 prfillset (&signals); 4823 if (!register_gdb_signals (pi, &signals)) 4824 proc_error (pi, "init_inferior, register_signals", __LINE__); 4825 4826 if ((fail = procfs_debug_inferior (pi)) != 0) 4827 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail); 4828 4829 /* FIXME: logically, we should really be turning OFF run-on-last-close, 4830 and possibly even turning ON kill-on-last-close at this point. But 4831 I can't make that change without careful testing which I don't have 4832 time to do right now... */ 4833 /* Turn on run-on-last-close flag so that the child 4834 will die if GDB goes away for some reason. */ 4835 if (!proc_set_run_on_last_close (pi)) 4836 proc_error (pi, "init_inferior, set_RLC", __LINE__); 4837 4838 /* The 'process ID' we return to GDB is composed of 4839 the actual process ID plus the lwp ID. */ 4840 inferior_ptid = MERGEPID (pi->pid, proc_get_current_thread (pi)); 4841 4842 /* Typically two, one trap to exec the shell, one to exec the 4843 program being debugged. Defined by "inferior.h". */ 4844 startup_inferior (START_INFERIOR_TRAPS_EXPECTED); 4845} 4846 4847/* 4848 * Function: set_exec_trap 4849 * 4850 * When GDB forks to create a new process, this function is called 4851 * on the child side of the fork before GDB exec's the user program. 4852 * Its job is to make the child minimally debuggable, so that the 4853 * parent GDB process can connect to the child and take over. 4854 * This function should do only the minimum to make that possible, 4855 * and to synchronize with the parent process. The parent process 4856 * should take care of the details. 4857 */ 4858 4859static void 4860procfs_set_exec_trap (void) 4861{ 4862 /* This routine called on the child side (inferior side) 4863 after GDB forks the inferior. It must use only local variables, 4864 because it may be sharing data space with its parent. */ 4865 4866 procinfo *pi; 4867 sysset_t *exitset; 4868 4869 if ((pi = create_procinfo (getpid (), 0)) == NULL) 4870 perror_with_name ("procfs: create_procinfo failed in child."); 4871 4872 if (open_procinfo_files (pi, FD_CTL) == 0) 4873 { 4874 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__); 4875 gdb_flush (gdb_stderr); 4876 /* no need to call "dead_procinfo", because we're going to exit. */ 4877 _exit (127); 4878 } 4879 4880#ifdef PRFS_STOPEXEC /* defined on OSF */ 4881 /* OSF method for tracing exec syscalls. Quoting: 4882 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace 4883 exits from exec system calls because of the user level loader. */ 4884 /* FIXME: make nice and maybe move into an access function. */ 4885 { 4886 int prfs_flags; 4887 4888 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0) 4889 { 4890 proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__); 4891 gdb_flush (gdb_stderr); 4892 _exit (127); 4893 } 4894 prfs_flags |= PRFS_STOPEXEC; 4895 4896 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0) 4897 { 4898 proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__); 4899 gdb_flush (gdb_stderr); 4900 _exit (127); 4901 } 4902 } 4903#else /* not PRFS_STOPEXEC */ 4904 /* Everyone else's (except OSF) method for tracing exec syscalls */ 4905 /* GW: Rationale... 4906 Not all systems with /proc have all the exec* syscalls with the same 4907 names. On the SGI, for example, there is no SYS_exec, but there 4908 *is* a SYS_execv. So, we try to account for that. */ 4909 4910 exitset = sysset_t_alloc (pi); 4911 gdb_premptysysset (exitset); 4912#ifdef SYS_exec 4913 gdb_praddsysset (exitset, SYS_exec); 4914#endif 4915#ifdef SYS_execve 4916 gdb_praddsysset (exitset, SYS_execve); 4917#endif 4918#ifdef SYS_execv 4919 gdb_praddsysset (exitset, SYS_execv); 4920#endif 4921#ifdef DYNAMIC_SYSCALLS 4922 { 4923 int callnum = find_syscall (pi, "execve"); 4924 4925 if (callnum >= 0) 4926 gdb_praddsysset (exitset, callnum); 4927 4928 callnum = find_syscall (pi, "ra_execve"); 4929 if (callnum >= 0) 4930 gdb_praddsysset (exitset, callnum); 4931 } 4932#endif /* DYNAMIC_SYSCALLS */ 4933 4934 if (!proc_set_traced_sysexit (pi, exitset)) 4935 { 4936 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__); 4937 gdb_flush (gdb_stderr); 4938 _exit (127); 4939 } 4940#endif /* PRFS_STOPEXEC */ 4941 4942 /* FIXME: should this be done in the parent instead? */ 4943 /* Turn off inherit on fork flag so that all grand-children 4944 of gdb start with tracing flags cleared. */ 4945 if (!proc_unset_inherit_on_fork (pi)) 4946 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__); 4947 4948 /* Turn off run on last close flag, so that the child process 4949 cannot run away just because we close our handle on it. 4950 We want it to wait for the parent to attach. */ 4951 if (!proc_unset_run_on_last_close (pi)) 4952 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__); 4953 4954 /* FIXME: No need to destroy the procinfo -- 4955 we have our own address space, and we're about to do an exec! */ 4956 /*destroy_procinfo (pi);*/ 4957} 4958 4959/* 4960 * Function: create_inferior 4961 * 4962 * This function is called BEFORE gdb forks the inferior process. 4963 * Its only real responsibility is to set things up for the fork, 4964 * and tell GDB which two functions to call after the fork (one 4965 * for the parent, and one for the child). 4966 * 4967 * This function does a complicated search for a unix shell program, 4968 * which it then uses to parse arguments and environment variables 4969 * to be sent to the child. I wonder whether this code could not 4970 * be abstracted out and shared with other unix targets such as 4971 * infptrace? 4972 */ 4973 4974static void 4975procfs_create_inferior (char *exec_file, char *allargs, char **env) 4976{ 4977 char *shell_file = getenv ("SHELL"); 4978 char *tryname; 4979 if (shell_file != NULL && strchr (shell_file, '/') == NULL) 4980 { 4981 4982 /* We will be looking down the PATH to find shell_file. If we 4983 just do this the normal way (via execlp, which operates by 4984 attempting an exec for each element of the PATH until it 4985 finds one which succeeds), then there will be an exec for 4986 each failed attempt, each of which will cause a PR_SYSEXIT 4987 stop, and we won't know how to distinguish the PR_SYSEXIT's 4988 for these failed execs with the ones for successful execs 4989 (whether the exec has succeeded is stored at that time in the 4990 carry bit or some such architecture-specific and 4991 non-ABI-specified place). 4992 4993 So I can't think of anything better than to search the PATH 4994 now. This has several disadvantages: (1) There is a race 4995 condition; if we find a file now and it is deleted before we 4996 exec it, we lose, even if the deletion leaves a valid file 4997 further down in the PATH, (2) there is no way to know exactly 4998 what an executable (in the sense of "capable of being 4999 exec'd") file is. Using access() loses because it may lose 5000 if the caller is the superuser; failing to use it loses if 5001 there are ACLs or some such. */ 5002 5003 char *p; 5004 char *p1; 5005 /* FIXME-maybe: might want "set path" command so user can change what 5006 path is used from within GDB. */ 5007 char *path = getenv ("PATH"); 5008 int len; 5009 struct stat statbuf; 5010 5011 if (path == NULL) 5012 path = "/bin:/usr/bin"; 5013 5014 tryname = alloca (strlen (path) + strlen (shell_file) + 2); 5015 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL) 5016 { 5017 p1 = strchr (p, ':'); 5018 if (p1 != NULL) 5019 len = p1 - p; 5020 else 5021 len = strlen (p); 5022 strncpy (tryname, p, len); 5023 tryname[len] = '\0'; 5024 strcat (tryname, "/"); 5025 strcat (tryname, shell_file); 5026 if (access (tryname, X_OK) < 0) 5027 continue; 5028 if (stat (tryname, &statbuf) < 0) 5029 continue; 5030 if (!S_ISREG (statbuf.st_mode)) 5031 /* We certainly need to reject directories. I'm not quite 5032 as sure about FIFOs, sockets, etc., but I kind of doubt 5033 that people want to exec() these things. */ 5034 continue; 5035 break; 5036 } 5037 if (p == NULL) 5038 /* Not found. This must be an error rather than merely passing 5039 the file to execlp(), because execlp() would try all the 5040 exec()s, causing GDB to get confused. */ 5041 error ("procfs:%d -- Can't find shell %s in PATH", 5042 __LINE__, shell_file); 5043 5044 shell_file = tryname; 5045 } 5046 5047 fork_inferior (exec_file, allargs, env, procfs_set_exec_trap, 5048 procfs_init_inferior, NULL, shell_file); 5049 5050 /* We are at the first instruction we care about. */ 5051 /* Pedal to the metal... */ 5052 5053 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0); 5054} 5055 5056/* 5057 * Function: notice_thread 5058 * 5059 * Callback for find_new_threads. 5060 * Calls "add_thread". 5061 */ 5062 5063static int 5064procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr) 5065{ 5066 ptid_t gdb_threadid = MERGEPID (pi->pid, thread->tid); 5067 5068 if (!in_thread_list (gdb_threadid)) 5069 add_thread (gdb_threadid); 5070 5071 return 0; 5072} 5073 5074/* 5075 * Function: target_find_new_threads 5076 * 5077 * Query all the threads that the target knows about, 5078 * and give them back to GDB to add to its list. 5079 */ 5080 5081void 5082procfs_find_new_threads (void) 5083{ 5084 procinfo *pi; 5085 5086 /* Find procinfo for main process */ 5087 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); 5088 proc_update_threads (pi); 5089 proc_iterate_over_threads (pi, procfs_notice_thread, NULL); 5090} 5091 5092/* 5093 * Function: target_thread_alive 5094 * 5095 * Return true if the thread is still 'alive'. 5096 * 5097 * This guy doesn't really seem to be doing his job. 5098 * Got to investigate how to tell when a thread is really gone. 5099 */ 5100 5101static int 5102procfs_thread_alive (ptid_t ptid) 5103{ 5104 int proc, thread; 5105 procinfo *pi; 5106 5107 proc = PIDGET (ptid); 5108 thread = TIDGET (ptid); 5109 /* If I don't know it, it ain't alive! */ 5110 if ((pi = find_procinfo (proc, thread)) == NULL) 5111 return 0; 5112 5113 /* If I can't get its status, it ain't alive! 5114 What's more, I need to forget about it! */ 5115 if (!proc_get_status (pi)) 5116 { 5117 destroy_procinfo (pi); 5118 return 0; 5119 } 5120 /* I couldn't have got its status if it weren't alive, so it's alive. */ 5121 return 1; 5122} 5123 5124/* 5125 * Function: target_pid_to_str 5126 * 5127 * Return a string to be used to identify the thread in 5128 * the "info threads" display. 5129 */ 5130 5131char * 5132procfs_pid_to_str (ptid_t ptid) 5133{ 5134 static char buf[80]; 5135 int proc, thread; 5136 procinfo *pi; 5137 5138 proc = PIDGET (ptid); 5139 thread = TIDGET (ptid); 5140 pi = find_procinfo (proc, thread); 5141 5142 if (thread == 0) 5143 sprintf (buf, "Process %d", proc); 5144 else 5145 sprintf (buf, "LWP %d", thread); 5146 return &buf[0]; 5147} 5148 5149/* 5150 * Function: procfs_set_watchpoint 5151 * Insert a watchpoint 5152 */ 5153 5154int 5155procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag, 5156 int after) 5157{ 5158#ifndef UNIXWARE 5159#ifndef AIX5 5160 int pflags = 0; 5161 procinfo *pi; 5162 5163 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ? 5164 PIDGET (inferior_ptid) : PIDGET (ptid), 0); 5165 5166 /* Translate from GDB's flags to /proc's */ 5167 if (len > 0) /* len == 0 means delete watchpoint */ 5168 { 5169 switch (rwflag) { /* FIXME: need an enum! */ 5170 case hw_write: /* default watchpoint (write) */ 5171 pflags = WRITE_WATCHFLAG; 5172 break; 5173 case hw_read: /* read watchpoint */ 5174 pflags = READ_WATCHFLAG; 5175 break; 5176 case hw_access: /* access watchpoint */ 5177 pflags = READ_WATCHFLAG | WRITE_WATCHFLAG; 5178 break; 5179 case hw_execute: /* execution HW breakpoint */ 5180 pflags = EXEC_WATCHFLAG; 5181 break; 5182 default: /* Something weird. Return error. */ 5183 return -1; 5184 } 5185 if (after) /* Stop after r/w access is completed. */ 5186 pflags |= AFTER_WATCHFLAG; 5187 } 5188 5189 if (!proc_set_watchpoint (pi, addr, len, pflags)) 5190 { 5191 if (errno == E2BIG) /* Typical error for no resources */ 5192 return -1; /* fail */ 5193 /* GDB may try to remove the same watchpoint twice. 5194 If a remove request returns no match, don't error. */ 5195 if (errno == ESRCH && len == 0) 5196 return 0; /* ignore */ 5197 proc_error (pi, "set_watchpoint", __LINE__); 5198 } 5199#endif /* AIX5 */ 5200#endif /* UNIXWARE */ 5201 return 0; 5202} 5203 5204/* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE 5205 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint, 5206 or bp_hardware_watchpoint. CNT is the number of watchpoints used so 5207 far. 5208 5209 Note: procfs_can_use_hw_breakpoint() is not yet used by all 5210 procfs.c targets due to the fact that some of them still define 5211 TARGET_CAN_USE_HARDWARE_WATCHPOINT. */ 5212 5213static int 5214procfs_can_use_hw_breakpoint (int type, int cnt, int othertype) 5215{ 5216#ifndef TARGET_HAS_HARDWARE_WATCHPOINTS 5217 return 0; 5218#else 5219 /* Due to the way that proc_set_watchpoint() is implemented, host 5220 and target pointers must be of the same size. If they are not, 5221 we can't use hardware watchpoints. This limitation is due to the 5222 fact that proc_set_watchpoint() calls 5223 procfs_address_to_host_pointer(); a close inspection of 5224 procfs_address_to_host_pointer will reveal that an internal error 5225 will be generated when the host and target pointer sizes are 5226 different. */ 5227 if (sizeof (void *) != TYPE_LENGTH (builtin_type_void_data_ptr)) 5228 return 0; 5229 5230 /* Other tests here??? */ 5231 5232 return 1; 5233#endif 5234} 5235 5236/* 5237 * Function: stopped_by_watchpoint 5238 * 5239 * Returns non-zero if process is stopped on a hardware watchpoint fault, 5240 * else returns zero. 5241 */ 5242 5243int 5244procfs_stopped_by_watchpoint (ptid_t ptid) 5245{ 5246 procinfo *pi; 5247 5248 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ? 5249 PIDGET (inferior_ptid) : PIDGET (ptid), 0); 5250 5251 if (!pi) /* If no process, then not stopped by watchpoint! */ 5252 return 0; 5253 5254 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) 5255 { 5256 if (proc_why (pi) == PR_FAULTED) 5257 { 5258#ifdef FLTWATCH 5259 if (proc_what (pi) == FLTWATCH) 5260 return 1; 5261#endif 5262#ifdef FLTKWATCH 5263 if (proc_what (pi) == FLTKWATCH) 5264 return 1; 5265#endif 5266 } 5267 } 5268 return 0; 5269} 5270 5271#ifdef TM_I386SOL2_H 5272/* 5273 * Function: procfs_find_LDT_entry 5274 * 5275 * Input: 5276 * ptid_t ptid; // The GDB-style pid-plus-LWP. 5277 * 5278 * Return: 5279 * pointer to the corresponding LDT entry. 5280 */ 5281 5282struct ssd * 5283procfs_find_LDT_entry (ptid_t ptid) 5284{ 5285 gdb_gregset_t *gregs; 5286 int key; 5287 procinfo *pi; 5288 5289 /* Find procinfo for the lwp. */ 5290 if ((pi = find_procinfo (PIDGET (ptid), TIDGET (ptid))) == NULL) 5291 { 5292 warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.", 5293 PIDGET (ptid), TIDGET (ptid)); 5294 return NULL; 5295 } 5296 /* get its general registers. */ 5297 if ((gregs = proc_get_gregs (pi)) == NULL) 5298 { 5299 warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.", 5300 PIDGET (ptid), TIDGET (ptid)); 5301 return NULL; 5302 } 5303 /* Now extract the GS register's lower 16 bits. */ 5304 key = (*gregs)[GS] & 0xffff; 5305 5306 /* Find the matching entry and return it. */ 5307 return proc_get_LDT_entry (pi, key); 5308} 5309#endif /* TM_I386SOL2_H */ 5310 5311/* 5312 * Memory Mappings Functions: 5313 */ 5314 5315/* 5316 * Function: iterate_over_mappings 5317 * 5318 * Call a callback function once for each mapping, passing it the mapping, 5319 * an optional secondary callback function, and some optional opaque data. 5320 * Quit and return the first non-zero value returned from the callback. 5321 * 5322 * Arguments: 5323 * pi -- procinfo struct for the process to be mapped. 5324 * func -- callback function to be called by this iterator. 5325 * data -- optional opaque data to be passed to the callback function. 5326 * child_func -- optional secondary function pointer to be passed 5327 * to the child function. 5328 * 5329 * Return: First non-zero return value from the callback function, 5330 * or zero. 5331 */ 5332 5333static int 5334iterate_over_mappings (procinfo *pi, int (*child_func) (), void *data, 5335 int (*func) (struct prmap *map, 5336 int (*child_func) (), 5337 void *data)) 5338{ 5339 char pathname[MAX_PROC_NAME_SIZE]; 5340 struct prmap *prmaps; 5341 struct prmap *prmap; 5342 int funcstat; 5343 int map_fd; 5344 int nmap; 5345#ifdef NEW_PROC_API 5346 struct stat sbuf; 5347#endif 5348 5349 /* Get the number of mappings, allocate space, 5350 and read the mappings into prmaps. */ 5351#ifdef NEW_PROC_API 5352 /* Open map fd. */ 5353 sprintf (pathname, "/proc/%d/map", pi->pid); 5354 if ((map_fd = open (pathname, O_RDONLY)) < 0) 5355 proc_error (pi, "iterate_over_mappings (open)", __LINE__); 5356 5357 /* Make sure it gets closed again. */ 5358 make_cleanup_close (map_fd); 5359 5360 /* Use stat to determine the file size, and compute 5361 the number of prmap_t objects it contains. */ 5362 if (fstat (map_fd, &sbuf) != 0) 5363 proc_error (pi, "iterate_over_mappings (fstat)", __LINE__); 5364 5365 nmap = sbuf.st_size / sizeof (prmap_t); 5366 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps)); 5367 if (read (map_fd, (char *) prmaps, nmap * sizeof (*prmaps)) 5368 != (nmap * sizeof (*prmaps))) 5369 proc_error (pi, "iterate_over_mappings (read)", __LINE__); 5370#else 5371 /* Use ioctl command PIOCNMAP to get number of mappings. */ 5372 if (ioctl (pi->ctl_fd, PIOCNMAP, &nmap) != 0) 5373 proc_error (pi, "iterate_over_mappings (PIOCNMAP)", __LINE__); 5374 5375 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps)); 5376 if (ioctl (pi->ctl_fd, PIOCMAP, prmaps) != 0) 5377 proc_error (pi, "iterate_over_mappings (PIOCMAP)", __LINE__); 5378#endif 5379 5380 for (prmap = prmaps; nmap > 0; prmap++, nmap--) 5381 if ((funcstat = (*func) (prmap, child_func, data)) != 0) 5382 return funcstat; 5383 5384 return 0; 5385} 5386 5387/* 5388 * Function: solib_mappings_callback 5389 * 5390 * Calls the supplied callback function once for each mapped address 5391 * space in the process. The callback function receives an open 5392 * file descriptor for the file corresponding to that mapped 5393 * address space (if there is one), and the base address of the 5394 * mapped space. Quit when the callback function returns a 5395 * nonzero value, or at teh end of the mappings. 5396 * 5397 * Returns: the first non-zero return value of the callback function, 5398 * or zero. 5399 */ 5400 5401int solib_mappings_callback (struct prmap *map, 5402 int (*func) (int, CORE_ADDR), 5403 void *data) 5404{ 5405 procinfo *pi = data; 5406 int fd; 5407 5408#ifdef NEW_PROC_API 5409 char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)]; 5410 5411 if (map->pr_vaddr == 0 && map->pr_size == 0) 5412 return -1; /* sanity */ 5413 5414 if (map->pr_mapname[0] == 0) 5415 { 5416 fd = -1; /* no map file */ 5417 } 5418 else 5419 { 5420 sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname); 5421 /* Note: caller's responsibility to close this fd! */ 5422 fd = open_with_retry (name, O_RDONLY); 5423 /* Note: we don't test the above call for failure; 5424 we just pass the FD on as given. Sometimes there is 5425 no file, so the open may return failure, but that's 5426 not a problem. */ 5427 } 5428#else 5429 fd = ioctl (pi->ctl_fd, PIOCOPENM, &map->pr_vaddr); 5430 /* Note: we don't test the above call for failure; 5431 we just pass the FD on as given. Sometimes there is 5432 no file, so the ioctl may return failure, but that's 5433 not a problem. */ 5434#endif 5435 return (*func) (fd, (CORE_ADDR) map->pr_vaddr); 5436} 5437 5438/* 5439 * Function: proc_iterate_over_mappings 5440 * 5441 * Uses the unified "iterate_over_mappings" function 5442 * to implement the exported interface to solib-svr4.c. 5443 * 5444 * Given a pointer to a function, call that function once for every 5445 * mapped address space in the process. The callback function 5446 * receives an open file descriptor for the file corresponding to 5447 * that mapped address space (if there is one), and the base address 5448 * of the mapped space. Quit when the callback function returns a 5449 * nonzero value, or at teh end of the mappings. 5450 * 5451 * Returns: the first non-zero return value of the callback function, 5452 * or zero. 5453 */ 5454 5455int 5456proc_iterate_over_mappings (int (*func) (int, CORE_ADDR)) 5457{ 5458 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); 5459 5460 return iterate_over_mappings (pi, func, pi, solib_mappings_callback); 5461} 5462 5463/* 5464 * Function: find_memory_regions_callback 5465 * 5466 * Implements the to_find_memory_regions method. 5467 * Calls an external function for each memory region. 5468 * External function will have the signiture: 5469 * 5470 * int callback (CORE_ADDR vaddr, 5471 * unsigned long size, 5472 * int read, int write, int execute, 5473 * void *data); 5474 * 5475 * Returns the integer value returned by the callback. 5476 */ 5477 5478static int 5479find_memory_regions_callback (struct prmap *map, 5480 int (*func) (CORE_ADDR, 5481 unsigned long, 5482 int, int, int, 5483 void *), 5484 void *data) 5485{ 5486 return (*func) ((CORE_ADDR) map->pr_vaddr, 5487 map->pr_size, 5488 (map->pr_mflags & MA_READ) != 0, 5489 (map->pr_mflags & MA_WRITE) != 0, 5490 (map->pr_mflags & MA_EXEC) != 0, 5491 data); 5492} 5493 5494/* 5495 * Function: proc_find_memory_regions 5496 * 5497 * External interface. Calls a callback function once for each 5498 * mapped memory region in the child process, passing as arguments 5499 * CORE_ADDR virtual_address, 5500 * unsigned long size, 5501 * int read, TRUE if region is readable by the child 5502 * int write, TRUE if region is writable by the child 5503 * int execute TRUE if region is executable by the child. 5504 * 5505 * Stops iterating and returns the first non-zero value 5506 * returned by the callback. 5507 */ 5508 5509static int 5510proc_find_memory_regions (int (*func) (CORE_ADDR, 5511 unsigned long, 5512 int, int, int, 5513 void *), 5514 void *data) 5515{ 5516 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); 5517 5518 return iterate_over_mappings (pi, func, data, 5519 find_memory_regions_callback); 5520} 5521 5522/* 5523 * Function: mappingflags 5524 * 5525 * Returns an ascii representation of a memory mapping's flags. 5526 */ 5527 5528static char * 5529mappingflags (long flags) 5530{ 5531 static char asciiflags[8]; 5532 5533 strcpy (asciiflags, "-------"); 5534#if defined (MA_PHYS) 5535 if (flags & MA_PHYS) 5536 asciiflags[0] = 'd'; 5537#endif 5538 if (flags & MA_STACK) 5539 asciiflags[1] = 's'; 5540 if (flags & MA_BREAK) 5541 asciiflags[2] = 'b'; 5542 if (flags & MA_SHARED) 5543 asciiflags[3] = 's'; 5544 if (flags & MA_READ) 5545 asciiflags[4] = 'r'; 5546 if (flags & MA_WRITE) 5547 asciiflags[5] = 'w'; 5548 if (flags & MA_EXEC) 5549 asciiflags[6] = 'x'; 5550 return (asciiflags); 5551} 5552 5553/* 5554 * Function: info_mappings_callback 5555 * 5556 * Callback function, does the actual work for 'info proc mappings'. 5557 */ 5558 5559static int 5560info_mappings_callback (struct prmap *map, int (*ignore) (), void *unused) 5561{ 5562 char *data_fmt_string; 5563 5564 if (TARGET_ADDR_BIT == 32) 5565 data_fmt_string = "\t%#10lx %#10lx %#10x %#10x %7s\n"; 5566 else 5567 data_fmt_string = " %#18lx %#18lx %#10x %#10x %7s\n"; 5568 5569 printf_filtered (data_fmt_string, 5570 (unsigned long) map->pr_vaddr, 5571 (unsigned long) map->pr_vaddr + map->pr_size - 1, 5572 map->pr_size, 5573#ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */ 5574 (unsigned int) map->pr_offset, 5575#else 5576 map->pr_off, 5577#endif 5578 mappingflags (map->pr_mflags)); 5579 5580 return 0; 5581} 5582 5583/* 5584 * Function: info_proc_mappings 5585 * 5586 * Implement the "info proc mappings" subcommand. 5587 */ 5588 5589static void 5590info_proc_mappings (procinfo *pi, int summary) 5591{ 5592 char *header_fmt_string; 5593 5594 if (TARGET_PTR_BIT == 32) 5595 header_fmt_string = "\t%10s %10s %10s %10s %7s\n"; 5596 else 5597 header_fmt_string = " %18s %18s %10s %10s %7s\n"; 5598 5599 if (summary) 5600 return; /* No output for summary mode. */ 5601 5602 printf_filtered ("Mapped address spaces:\n\n"); 5603 printf_filtered (header_fmt_string, 5604 "Start Addr", 5605 " End Addr", 5606 " Size", 5607 " Offset", 5608 "Flags"); 5609 5610 iterate_over_mappings (pi, NULL, NULL, info_mappings_callback); 5611 printf_filtered ("\n"); 5612} 5613 5614/* 5615 * Function: info_proc_cmd 5616 * 5617 * Implement the "info proc" command. 5618 */ 5619 5620static void 5621info_proc_cmd (char *args, int from_tty) 5622{ 5623 struct cleanup *old_chain; 5624 procinfo *process = NULL; 5625 procinfo *thread = NULL; 5626 char **argv = NULL; 5627 char *tmp = NULL; 5628 int pid = 0; 5629 int tid = 0; 5630 int mappings = 0; 5631 5632 old_chain = make_cleanup (null_cleanup, 0); 5633 if (args) 5634 { 5635 if ((argv = buildargv (args)) == NULL) 5636 nomem (0); 5637 else 5638 make_cleanup_freeargv (argv); 5639 } 5640 while (argv != NULL && *argv != NULL) 5641 { 5642 if (isdigit (argv[0][0])) 5643 { 5644 pid = strtoul (argv[0], &tmp, 10); 5645 if (*tmp == '/') 5646 tid = strtoul (++tmp, NULL, 10); 5647 } 5648 else if (argv[0][0] == '/') 5649 { 5650 tid = strtoul (argv[0] + 1, NULL, 10); 5651 } 5652 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0) 5653 { 5654 mappings = 1; 5655 } 5656 else 5657 { 5658 /* [...] */ 5659 } 5660 argv++; 5661 } 5662 if (pid == 0) 5663 pid = PIDGET (inferior_ptid); 5664 if (pid == 0) 5665 error ("No current process: you must name one."); 5666 else 5667 { 5668 /* Have pid, will travel. 5669 First see if it's a process we're already debugging. */ 5670 process = find_procinfo (pid, 0); 5671 if (process == NULL) 5672 { 5673 /* No. So open a procinfo for it, but 5674 remember to close it again when finished. */ 5675 process = create_procinfo (pid, 0); 5676 make_cleanup (do_destroy_procinfo_cleanup, process); 5677 if (!open_procinfo_files (process, FD_CTL)) 5678 proc_error (process, "info proc, open_procinfo_files", __LINE__); 5679 } 5680 } 5681 if (tid != 0) 5682 thread = create_procinfo (pid, tid); 5683 5684 if (process) 5685 { 5686 printf_filtered ("process %d flags:\n", process->pid); 5687 proc_prettyprint_flags (proc_flags (process), 1); 5688 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP)) 5689 proc_prettyprint_why (proc_why (process), proc_what (process), 1); 5690 if (proc_get_nthreads (process) > 1) 5691 printf_filtered ("Process has %d threads.\n", 5692 proc_get_nthreads (process)); 5693 } 5694 if (thread) 5695 { 5696 printf_filtered ("thread %d flags:\n", thread->tid); 5697 proc_prettyprint_flags (proc_flags (thread), 1); 5698 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP)) 5699 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1); 5700 } 5701 5702 if (mappings) 5703 { 5704 info_proc_mappings (process, 0); 5705 } 5706 5707 do_cleanups (old_chain); 5708} 5709 5710static void 5711proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode) 5712{ 5713 procinfo *pi; 5714 sysset_t *sysset; 5715 int syscallnum = 0; 5716 5717 if (PIDGET (inferior_ptid) <= 0) 5718 error ("you must be debugging a process to use this command."); 5719 5720 if (args == NULL || args[0] == 0) 5721 error_no_arg ("system call to trace"); 5722 5723 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); 5724 if (isdigit (args[0])) 5725 { 5726 syscallnum = atoi (args); 5727 if (entry_or_exit == PR_SYSENTRY) 5728 sysset = proc_get_traced_sysentry (pi, NULL); 5729 else 5730 sysset = proc_get_traced_sysexit (pi, NULL); 5731 5732 if (sysset == NULL) 5733 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__); 5734 5735 if (mode == FLAG_SET) 5736 gdb_praddsysset (sysset, syscallnum); 5737 else 5738 gdb_prdelsysset (sysset, syscallnum); 5739 5740 if (entry_or_exit == PR_SYSENTRY) 5741 { 5742 if (!proc_set_traced_sysentry (pi, sysset)) 5743 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__); 5744 } 5745 else 5746 { 5747 if (!proc_set_traced_sysexit (pi, sysset)) 5748 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__); 5749 } 5750 } 5751} 5752 5753static void 5754proc_trace_sysentry_cmd (char *args, int from_tty) 5755{ 5756 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET); 5757} 5758 5759static void 5760proc_trace_sysexit_cmd (char *args, int from_tty) 5761{ 5762 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET); 5763} 5764 5765static void 5766proc_untrace_sysentry_cmd (char *args, int from_tty) 5767{ 5768 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET); 5769} 5770 5771static void 5772proc_untrace_sysexit_cmd (char *args, int from_tty) 5773{ 5774 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET); 5775} 5776 5777 5778void 5779_initialize_procfs (void) 5780{ 5781 init_procfs_ops (); 5782 add_target (&procfs_ops); 5783 add_info ("proc", info_proc_cmd, 5784 "Show /proc process information about any running process.\n\ 5785Specify process id, or use the program being debugged by default.\n\ 5786Specify keyword 'mappings' for detailed info on memory mappings."); 5787 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd, 5788 "Give a trace of entries into the syscall."); 5789 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd, 5790 "Give a trace of exits from the syscall."); 5791 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd, 5792 "Cancel a trace of entries into the syscall."); 5793 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd, 5794 "Cancel a trace of exits from the syscall."); 5795} 5796 5797/* =================== END, GDB "MODULE" =================== */ 5798 5799 5800 5801/* miscellaneous stubs: */ 5802/* The following satisfy a few random symbols mostly created by */ 5803/* the solaris threads implementation, which I will chase down */ 5804/* later. */ 5805 5806/* 5807 * Return a pid for which we guarantee 5808 * we will be able to find a 'live' procinfo. 5809 */ 5810 5811ptid_t 5812procfs_first_available (void) 5813{ 5814 return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1); 5815} 5816 5817/* =================== GCORE .NOTE "MODULE" =================== */ 5818#if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT) 5819/* gcore only implemented on solaris and unixware (so far) */ 5820 5821static char * 5822procfs_do_thread_registers (bfd *obfd, ptid_t ptid, 5823 char *note_data, int *note_size) 5824{ 5825 gdb_gregset_t gregs; 5826 gdb_fpregset_t fpregs; 5827 unsigned long merged_pid; 5828 5829 merged_pid = TIDGET (ptid) << 16 | PIDGET (ptid); 5830 5831 fill_gregset (&gregs, -1); 5832#if defined (UNIXWARE) 5833 note_data = (char *) elfcore_write_lwpstatus (obfd, 5834 note_data, 5835 note_size, 5836 merged_pid, 5837 stop_signal, 5838 &gregs); 5839#else 5840 note_data = (char *) elfcore_write_prstatus (obfd, 5841 note_data, 5842 note_size, 5843 merged_pid, 5844 stop_signal, 5845 &gregs); 5846#endif 5847 fill_fpregset (&fpregs, -1); 5848 note_data = (char *) elfcore_write_prfpreg (obfd, 5849 note_data, 5850 note_size, 5851 &fpregs, 5852 sizeof (fpregs)); 5853 return note_data; 5854} 5855 5856struct procfs_corefile_thread_data { 5857 bfd *obfd; 5858 char *note_data; 5859 int *note_size; 5860}; 5861 5862static int 5863procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data) 5864{ 5865 struct procfs_corefile_thread_data *args = data; 5866 5867 if (pi != NULL && thread->tid != 0) 5868 { 5869 ptid_t saved_ptid = inferior_ptid; 5870 inferior_ptid = MERGEPID (pi->pid, thread->tid); 5871 args->note_data = procfs_do_thread_registers (args->obfd, inferior_ptid, 5872 args->note_data, 5873 args->note_size); 5874 inferior_ptid = saved_ptid; 5875 } 5876 return 0; 5877} 5878 5879static char * 5880procfs_make_note_section (bfd *obfd, int *note_size) 5881{ 5882 struct cleanup *old_chain; 5883 gdb_gregset_t gregs; 5884 gdb_fpregset_t fpregs; 5885 char fname[16] = {'\0'}; 5886 char psargs[80] = {'\0'}; 5887 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); 5888 char *note_data = NULL; 5889 char *inf_args; 5890 struct procfs_corefile_thread_data thread_args; 5891 char *auxv; 5892 int auxv_len; 5893 5894 if (get_exec_file (0)) 5895 { 5896 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname)); 5897 strncpy (psargs, get_exec_file (0), 5898 sizeof (psargs)); 5899 5900 inf_args = get_inferior_args (); 5901 if (inf_args && *inf_args && 5902 strlen (inf_args) < ((int) sizeof (psargs) - (int) strlen (psargs))) 5903 { 5904 strncat (psargs, " ", 5905 sizeof (psargs) - strlen (psargs)); 5906 strncat (psargs, inf_args, 5907 sizeof (psargs) - strlen (psargs)); 5908 } 5909 } 5910 5911 note_data = (char *) elfcore_write_prpsinfo (obfd, 5912 note_data, 5913 note_size, 5914 fname, 5915 psargs); 5916 5917#ifdef UNIXWARE 5918 fill_gregset (&gregs, -1); 5919 note_data = elfcore_write_pstatus (obfd, note_data, note_size, 5920 PIDGET (inferior_ptid), 5921 stop_signal, &gregs); 5922#endif 5923 5924 thread_args.obfd = obfd; 5925 thread_args.note_data = note_data; 5926 thread_args.note_size = note_size; 5927 proc_iterate_over_threads (pi, procfs_corefile_thread_callback, &thread_args); 5928 5929 if (thread_args.note_data == note_data) 5930 { 5931 /* iterate_over_threads didn't come up with any threads; 5932 just use inferior_ptid. */ 5933 note_data = procfs_do_thread_registers (obfd, inferior_ptid, 5934 note_data, note_size); 5935 } 5936 else 5937 { 5938 note_data = thread_args.note_data; 5939 } 5940 5941 auxv_len = target_auxv_read (¤t_target, &auxv); 5942 if (auxv_len > 0) 5943 { 5944 note_data = elfcore_write_note (obfd, note_data, note_size, 5945 "CORE", NT_AUXV, auxv, auxv_len); 5946 xfree (auxv); 5947 } 5948 5949 make_cleanup (xfree, note_data); 5950 return note_data; 5951} 5952#else /* !(Solaris or Unixware) */ 5953static char * 5954procfs_make_note_section (bfd *obfd, int *note_size) 5955{ 5956 error ("gcore not implemented for this host."); 5957 return NULL; /* lint */ 5958} 5959#endif /* Solaris or Unixware */ 5960/* =================== END GCORE .NOTE "MODULE" =================== */ 5961