Home | History | Annotate | Line # | Download | only in gdb
inf-ptrace.c revision 1.6
      1  1.1  christos /* Low-level child interface to ptrace.
      2  1.1  christos 
      3  1.6  christos    Copyright (C) 1988-2016 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos #include "defs.h"
     21  1.1  christos #include "command.h"
     22  1.1  christos #include "inferior.h"
     23  1.1  christos #include "inflow.h"
     24  1.1  christos #include "terminal.h"
     25  1.1  christos #include "gdbcore.h"
     26  1.1  christos #include "regcache.h"
     27  1.6  christos #include "nat/gdb_ptrace.h"
     28  1.1  christos #include "gdb_wait.h"
     29  1.1  christos #include <signal.h>
     30  1.1  christos 
     31  1.1  christos #include "inf-ptrace.h"
     32  1.1  christos #include "inf-child.h"
     33  1.1  christos #include "gdbthread.h"
     34  1.1  christos 
     35  1.1  christos 
     36  1.1  christos 
     38  1.1  christos #ifdef PT_GET_PROCESS_STATE
     39  1.3  christos 
     40  1.3  christos /* Target hook for follow_fork.  On entry and at return inferior_ptid is
     41  1.3  christos    the ptid of the followed inferior.  */
     42  1.1  christos 
     43  1.1  christos static int
     44  1.1  christos inf_ptrace_follow_fork (struct target_ops *ops, int follow_child,
     45  1.1  christos 			int detach_fork)
     46  1.3  christos {
     47  1.1  christos   if (!follow_child)
     48  1.3  christos     {
     49  1.3  christos       struct thread_info *tp = inferior_thread ();
     50  1.1  christos       pid_t child_pid = ptid_get_pid (tp->pending_follow.value.related_pid);
     51  1.1  christos 
     52  1.1  christos       /* Breakpoints have already been detached from the child by
     53  1.1  christos 	 infrun.c.  */
     54  1.3  christos 
     55  1.1  christos       if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
     56  1.1  christos 	perror_with_name (("ptrace"));
     57  1.1  christos     }
     58  1.1  christos 
     59  1.1  christos   return 0;
     60  1.1  christos }
     61  1.5  christos 
     62  1.5  christos static int
     63  1.5  christos inf_ptrace_insert_fork_catchpoint (struct target_ops *self, int pid)
     64  1.5  christos {
     65  1.5  christos   return 0;
     66  1.5  christos }
     67  1.5  christos 
     68  1.5  christos static int
     69  1.5  christos inf_ptrace_remove_fork_catchpoint (struct target_ops *self, int pid)
     70  1.5  christos {
     71  1.5  christos   return 0;
     72  1.5  christos }
     73  1.1  christos 
     74  1.1  christos #endif /* PT_GET_PROCESS_STATE */
     75  1.1  christos 
     76  1.1  christos 
     78  1.1  christos /* Prepare to be traced.  */
     79  1.1  christos 
     80  1.1  christos static void
     81  1.1  christos inf_ptrace_me (void)
     82  1.1  christos {
     83  1.1  christos   /* "Trace me, Dr. Memory!"  */
     84  1.1  christos   ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
     85  1.1  christos }
     86  1.1  christos 
     87  1.1  christos /* Start a new inferior Unix child process.  EXEC_FILE is the file to
     88  1.1  christos    run, ALLARGS is a string containing the arguments to the program.
     89  1.1  christos    ENV is the environment vector to pass.  If FROM_TTY is non-zero, be
     90  1.1  christos    chatty about it.  */
     91  1.1  christos 
     92  1.1  christos static void
     93  1.1  christos inf_ptrace_create_inferior (struct target_ops *ops,
     94  1.1  christos 			    char *exec_file, char *allargs, char **env,
     95  1.1  christos 			    int from_tty)
     96  1.1  christos {
     97  1.1  christos   int pid;
     98  1.1  christos 
     99  1.1  christos   /* Do not change either targets above or the same target if already present.
    100  1.1  christos      The reason is the target stack is shared across multiple inferiors.  */
    101  1.1  christos   int ops_already_pushed = target_is_pushed (ops);
    102  1.1  christos   struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
    103  1.1  christos 
    104  1.1  christos   if (! ops_already_pushed)
    105  1.1  christos     {
    106  1.1  christos       /* Clear possible core file with its process_stratum.  */
    107  1.1  christos       push_target (ops);
    108  1.1  christos       make_cleanup_unpush_target (ops);
    109  1.1  christos     }
    110  1.1  christos 
    111  1.1  christos   pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
    112  1.1  christos 		       NULL, NULL, NULL);
    113  1.1  christos 
    114  1.1  christos   discard_cleanups (back_to);
    115  1.1  christos 
    116  1.1  christos   startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
    117  1.1  christos 
    118  1.1  christos   /* On some targets, there must be some explicit actions taken after
    119  1.1  christos      the inferior has been started up.  */
    120  1.1  christos   target_post_startup_inferior (pid_to_ptid (pid));
    121  1.1  christos }
    122  1.1  christos 
    123  1.1  christos #ifdef PT_GET_PROCESS_STATE
    124  1.3  christos 
    125  1.1  christos static void
    126  1.1  christos inf_ptrace_post_startup_inferior (struct target_ops *self, ptid_t pid)
    127  1.1  christos {
    128  1.1  christos   ptrace_event_t pe;
    129  1.1  christos 
    130  1.1  christos   /* Set the initial event mask.  */
    131  1.1  christos   memset (&pe, 0, sizeof pe);
    132  1.1  christos   pe.pe_set_event |= PTRACE_FORK;
    133  1.1  christos   if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid),
    134  1.1  christos 	      (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
    135  1.1  christos     perror_with_name (("ptrace"));
    136  1.1  christos }
    137  1.1  christos 
    138  1.1  christos #endif
    139  1.1  christos 
    140  1.1  christos /* Clean up a rotting corpse of an inferior after it died.  */
    141  1.1  christos 
    142  1.1  christos static void
    143  1.1  christos inf_ptrace_mourn_inferior (struct target_ops *ops)
    144  1.1  christos {
    145  1.1  christos   int status;
    146  1.1  christos 
    147  1.1  christos   /* Wait just one more time to collect the inferior's exit status.
    148  1.1  christos      Do not check whether this succeeds though, since we may be
    149  1.1  christos      dealing with a process that we attached to.  Such a process will
    150  1.1  christos      only report its exit status to its original parent.  */
    151  1.3  christos   waitpid (ptid_get_pid (inferior_ptid), &status, 0);
    152  1.1  christos 
    153  1.1  christos   inf_child_mourn_inferior (ops);
    154  1.1  christos }
    155  1.1  christos 
    156  1.1  christos /* Attach to the process specified by ARGS.  If FROM_TTY is non-zero,
    157  1.1  christos    be chatty about it.  */
    158  1.3  christos 
    159  1.1  christos static void
    160  1.1  christos inf_ptrace_attach (struct target_ops *ops, const char *args, int from_tty)
    161  1.1  christos {
    162  1.1  christos   char *exec_file;
    163  1.1  christos   pid_t pid;
    164  1.1  christos   struct inferior *inf;
    165  1.1  christos 
    166  1.1  christos   /* Do not change either targets above or the same target if already present.
    167  1.1  christos      The reason is the target stack is shared across multiple inferiors.  */
    168  1.1  christos   int ops_already_pushed = target_is_pushed (ops);
    169  1.1  christos   struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
    170  1.1  christos 
    171  1.1  christos   pid = parse_pid_to_attach (args);
    172  1.1  christos 
    173  1.1  christos   if (pid == getpid ())		/* Trying to masturbate?  */
    174  1.1  christos     error (_("I refuse to debug myself!"));
    175  1.1  christos 
    176  1.1  christos   if (! ops_already_pushed)
    177  1.1  christos     {
    178  1.1  christos       /* target_pid_to_str already uses the target.  Also clear possible core
    179  1.1  christos 	 file with its process_stratum.  */
    180  1.1  christos       push_target (ops);
    181  1.1  christos       make_cleanup_unpush_target (ops);
    182  1.1  christos     }
    183  1.1  christos 
    184  1.1  christos   if (from_tty)
    185  1.1  christos     {
    186  1.1  christos       exec_file = get_exec_file (0);
    187  1.1  christos 
    188  1.1  christos       if (exec_file)
    189  1.1  christos 	printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
    190  1.1  christos 			   target_pid_to_str (pid_to_ptid (pid)));
    191  1.1  christos       else
    192  1.1  christos 	printf_unfiltered (_("Attaching to %s\n"),
    193  1.1  christos 			   target_pid_to_str (pid_to_ptid (pid)));
    194  1.1  christos 
    195  1.1  christos       gdb_flush (gdb_stdout);
    196  1.1  christos     }
    197  1.1  christos 
    198  1.1  christos #ifdef PT_ATTACH
    199  1.1  christos   errno = 0;
    200  1.1  christos   ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
    201  1.1  christos   if (errno != 0)
    202  1.1  christos     perror_with_name (("ptrace"));
    203  1.1  christos #else
    204  1.1  christos   error (_("This system does not support attaching to a process"));
    205  1.1  christos #endif
    206  1.1  christos 
    207  1.1  christos   inf = current_inferior ();
    208  1.1  christos   inferior_appeared (inf, pid);
    209  1.1  christos   inf->attach_flag = 1;
    210  1.1  christos   inferior_ptid = pid_to_ptid (pid);
    211  1.1  christos 
    212  1.1  christos   /* Always add a main thread.  If some target extends the ptrace
    213  1.1  christos      target, it should decorate the ptid later with more info.  */
    214  1.1  christos   add_thread_silent (inferior_ptid);
    215  1.1  christos 
    216  1.1  christos   discard_cleanups (back_to);
    217  1.1  christos }
    218  1.1  christos 
    219  1.1  christos #ifdef PT_GET_PROCESS_STATE
    220  1.3  christos 
    221  1.1  christos static void
    222  1.1  christos inf_ptrace_post_attach (struct target_ops *self, int pid)
    223  1.1  christos {
    224  1.1  christos   ptrace_event_t pe;
    225  1.1  christos 
    226  1.1  christos   /* Set the initial event mask.  */
    227  1.1  christos   memset (&pe, 0, sizeof pe);
    228  1.1  christos   pe.pe_set_event |= PTRACE_FORK;
    229  1.1  christos   if (ptrace (PT_SET_EVENT_MASK, pid,
    230  1.1  christos 	      (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
    231  1.1  christos     perror_with_name (("ptrace"));
    232  1.1  christos }
    233  1.1  christos 
    234  1.1  christos #endif
    235  1.1  christos 
    236  1.1  christos /* Detach from the inferior, optionally passing it the signal
    237  1.1  christos    specified by ARGS.  If FROM_TTY is non-zero, be chatty about it.  */
    238  1.1  christos 
    239  1.1  christos static void
    240  1.1  christos inf_ptrace_detach (struct target_ops *ops, const char *args, int from_tty)
    241  1.1  christos {
    242  1.1  christos   pid_t pid = ptid_get_pid (inferior_ptid);
    243  1.6  christos   int sig = 0;
    244  1.1  christos 
    245  1.1  christos   target_announce_detach (from_tty);
    246  1.1  christos   if (args)
    247  1.1  christos     sig = atoi (args);
    248  1.1  christos 
    249  1.1  christos #ifdef PT_DETACH
    250  1.1  christos   /* We'd better not have left any breakpoints in the program or it'll
    251  1.1  christos      die when it hits one.  Also note that this may only work if we
    252  1.1  christos      previously attached to the inferior.  It *might* work if we
    253  1.1  christos      started the process ourselves.  */
    254  1.1  christos   errno = 0;
    255  1.1  christos   ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
    256  1.1  christos   if (errno != 0)
    257  1.1  christos     perror_with_name (("ptrace"));
    258  1.1  christos #else
    259  1.1  christos   error (_("This system does not support detaching from a process"));
    260  1.6  christos #endif
    261  1.6  christos 
    262  1.6  christos   inf_ptrace_detach_success (ops);
    263  1.6  christos }
    264  1.6  christos 
    265  1.6  christos /* See inf-ptrace.h.  */
    266  1.6  christos 
    267  1.6  christos void
    268  1.6  christos inf_ptrace_detach_success (struct target_ops *ops)
    269  1.6  christos {
    270  1.1  christos   pid_t pid = ptid_get_pid (inferior_ptid);
    271  1.1  christos 
    272  1.1  christos   inferior_ptid = null_ptid;
    273  1.3  christos   detach_inferior (pid);
    274  1.1  christos 
    275  1.1  christos   inf_child_maybe_unpush_target (ops);
    276  1.1  christos }
    277  1.1  christos 
    278  1.1  christos /* Kill the inferior.  */
    279  1.1  christos 
    280  1.1  christos static void
    281  1.1  christos inf_ptrace_kill (struct target_ops *ops)
    282  1.1  christos {
    283  1.1  christos   pid_t pid = ptid_get_pid (inferior_ptid);
    284  1.1  christos   int status;
    285  1.1  christos 
    286  1.1  christos   if (pid == 0)
    287  1.1  christos     return;
    288  1.1  christos 
    289  1.1  christos   ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
    290  1.1  christos   waitpid (pid, &status, 0);
    291  1.1  christos 
    292  1.1  christos   target_mourn_inferior ();
    293  1.6  christos }
    294  1.1  christos 
    295  1.1  christos /* Interrupt the inferior.  */
    296  1.6  christos 
    297  1.1  christos static void
    298  1.1  christos inf_ptrace_interrupt (struct target_ops *self, ptid_t ptid)
    299  1.1  christos {
    300  1.1  christos   /* Send a SIGINT to the process group.  This acts just like the user
    301  1.1  christos      typed a ^C on the controlling terminal.  Note that using a
    302  1.1  christos      negative process number in kill() is a System V-ism.  The proper
    303  1.1  christos      BSD interface is killpg().  However, all modern BSDs support the
    304  1.1  christos      System V interface too.  */
    305  1.1  christos   kill (-inferior_process_group (), SIGINT);
    306  1.5  christos }
    307  1.5  christos 
    308  1.5  christos /* Return which PID to pass to ptrace in order to observe/control the
    309  1.6  christos    tracee identified by PTID.  */
    310  1.5  christos 
    311  1.5  christos pid_t
    312  1.5  christos get_ptrace_pid (ptid_t ptid)
    313  1.5  christos {
    314  1.5  christos   pid_t pid;
    315  1.5  christos 
    316  1.5  christos #ifndef __NetBSD__
    317  1.5  christos   /* If we have an LWPID to work with, use it.  Otherwise, we're
    318  1.5  christos      dealing with a non-threaded program/target.  */
    319  1.5  christos   pid = ptid_get_lwp (ptid);
    320  1.5  christos   if (pid == 0)
    321  1.5  christos #endif
    322  1.5  christos     pid = ptid_get_pid (ptid);
    323  1.5  christos   return pid;
    324  1.1  christos }
    325  1.1  christos 
    326  1.1  christos /* Resume execution of thread PTID, or all threads if PTID is -1.  If
    327  1.1  christos    STEP is nonzero, single-step it.  If SIGNAL is nonzero, give it
    328  1.1  christos    that signal.  */
    329  1.1  christos 
    330  1.1  christos static void
    331  1.1  christos inf_ptrace_resume (struct target_ops *ops,
    332  1.5  christos 		   ptid_t ptid, int step, enum gdb_signal signal)
    333  1.1  christos {
    334  1.1  christos   pid_t pid;
    335  1.5  christos   int request, sig;
    336  1.1  christos 
    337  1.1  christos   if (ptid_equal (minus_one_ptid, ptid))
    338  1.1  christos     /* Resume all threads.  Traditionally ptrace() only supports
    339  1.5  christos        single-threaded processes, so simply resume the inferior.  */
    340  1.5  christos     pid = ptid_get_pid (inferior_ptid);
    341  1.1  christos   else
    342  1.1  christos     pid = get_ptrace_pid (ptid);
    343  1.1  christos 
    344  1.1  christos   if (catch_syscall_enabled () > 0)
    345  1.1  christos     request = PT_SYSCALL;
    346  1.1  christos   else
    347  1.1  christos     request = PT_CONTINUE;
    348  1.1  christos 
    349  1.1  christos   if (step)
    350  1.1  christos     {
    351  1.1  christos       /* If this system does not support PT_STEP, a higher level
    352  1.1  christos          function will have called single_step() to transmute the step
    353  1.1  christos          request into a continue request (by setting breakpoints on
    354  1.1  christos          all possible successor instructions), so we don't have to
    355  1.1  christos          worry about that here.  */
    356  1.1  christos       request = PT_STEP;
    357  1.1  christos #ifdef __NetBSD__
    358  1.1  christos       /*
    359  1.1  christos        * On NetBSD the data field of PT_STEP contains the thread
    360  1.1  christos        * to be stepped; all other threads are continued if this value is > 0
    361  1.1  christos        */
    362  1.1  christos       sig = ptid_get_lwp(ptid);
    363  1.1  christos #else
    364  1.1  christos       sig = 0;
    365  1.1  christos #endif
    366  1.1  christos     } else
    367  1.1  christos       sig = gdb_signal_to_host (signal);
    368  1.1  christos 
    369  1.1  christos   /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
    370  1.1  christos      where it was.  If GDB wanted it to start some other way, we have
    371  1.1  christos      already written a new program counter value to the child.  */
    372  1.1  christos   errno = 0;
    373  1.1  christos   ptrace (request, pid, (PTRACE_TYPE_ARG3)1, sig);
    374  1.1  christos   if (errno != 0)
    375  1.1  christos     perror_with_name (("ptrace"));
    376  1.1  christos }
    377  1.1  christos 
    378  1.1  christos /* Wait for the child specified by PTID to do something.  Return the
    379  1.1  christos    process ID of the child, or MINUS_ONE_PTID in case of error; store
    380  1.1  christos    the status in *OURSTATUS.  */
    381  1.1  christos 
    382  1.1  christos static ptid_t
    383  1.1  christos inf_ptrace_wait (struct target_ops *ops,
    384  1.1  christos 		 ptid_t ptid, struct target_waitstatus *ourstatus, int options)
    385  1.1  christos {
    386  1.1  christos   pid_t pid;
    387  1.1  christos   int status, save_errno;
    388  1.1  christos 
    389  1.1  christos   do
    390  1.1  christos     {
    391  1.1  christos       set_sigint_trap ();
    392  1.1  christos 
    393  1.1  christos       do
    394  1.1  christos 	{
    395  1.1  christos 	  pid = waitpid (ptid_get_pid (ptid), &status, 0);
    396  1.1  christos 	  save_errno = errno;
    397  1.1  christos 	}
    398  1.1  christos       while (pid == -1 && errno == EINTR);
    399  1.1  christos 
    400  1.1  christos       clear_sigint_trap ();
    401  1.1  christos 
    402  1.1  christos       if (pid == -1)
    403  1.1  christos 	{
    404  1.1  christos 	  fprintf_unfiltered (gdb_stderr,
    405  1.1  christos 			      _("Child process unexpectedly missing: %s.\n"),
    406  1.1  christos 			      safe_strerror (save_errno));
    407  1.1  christos 
    408  1.1  christos 	  /* Claim it exited with unknown signal.  */
    409  1.1  christos 	  ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
    410  1.1  christos 	  ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
    411  1.1  christos 	  return inferior_ptid;
    412  1.1  christos 	}
    413  1.1  christos 
    414  1.1  christos       /* Ignore terminated detached child processes.  */
    415  1.1  christos       if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
    416  1.1  christos 	pid = -1;
    417  1.1  christos     }
    418  1.1  christos   while (pid == -1);
    419  1.1  christos 
    420  1.1  christos #ifdef PT_GET_PROCESS_STATE
    421  1.1  christos   if (WIFSTOPPED (status))
    422  1.1  christos     {
    423  1.1  christos       ptrace_state_t pe;
    424  1.1  christos       pid_t fpid;
    425  1.1  christos 
    426  1.1  christos       if (ptrace (PT_GET_PROCESS_STATE, pid,
    427  1.1  christos 		  (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
    428  1.1  christos 	perror_with_name (("ptrace"));
    429  1.1  christos 
    430  1.1  christos       switch (pe.pe_report_event)
    431  1.1  christos 	{
    432  1.1  christos 	case PTRACE_FORK:
    433  1.1  christos 	  ourstatus->kind = TARGET_WAITKIND_FORKED;
    434  1.1  christos 	  ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
    435  1.1  christos 
    436  1.1  christos 	  /* Make sure the other end of the fork is stopped too.  */
    437  1.1  christos 	  fpid = waitpid (pe.pe_other_pid, &status, 0);
    438  1.1  christos 	  if (fpid == -1)
    439  1.1  christos 	    perror_with_name (("waitpid"));
    440  1.1  christos 
    441  1.1  christos 	  if (ptrace (PT_GET_PROCESS_STATE, fpid,
    442  1.1  christos 		      (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
    443  1.1  christos 	    perror_with_name (("ptrace"));
    444  1.1  christos 
    445  1.1  christos 	  gdb_assert (pe.pe_report_event == PTRACE_FORK);
    446  1.1  christos 	  gdb_assert (pe.pe_other_pid == pid);
    447  1.1  christos 	  if (fpid == ptid_get_pid (inferior_ptid))
    448  1.1  christos 	    {
    449  1.1  christos 	      ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
    450  1.1  christos 	      return pid_to_ptid (fpid);
    451  1.1  christos 	    }
    452  1.1  christos 
    453  1.1  christos 	  return pid_to_ptid (pid);
    454  1.1  christos 	}
    455  1.1  christos     }
    456  1.1  christos #endif
    457  1.1  christos 
    458  1.1  christos   store_waitstatus (ourstatus, status);
    459  1.1  christos   return pid_to_ptid (pid);
    460  1.3  christos }
    461  1.1  christos 
    462  1.3  christos /* Implement the to_xfer_partial target_ops method.  */
    463  1.1  christos 
    464  1.1  christos static enum target_xfer_status
    465  1.1  christos inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
    466  1.3  christos 			 const char *annex, gdb_byte *readbuf,
    467  1.1  christos 			 const gdb_byte *writebuf,
    468  1.1  christos 			 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
    469  1.1  christos {
    470  1.1  christos   pid_t pid = ptid_get_pid (inferior_ptid);
    471  1.1  christos 
    472  1.1  christos   switch (object)
    473  1.1  christos     {
    474  1.1  christos     case TARGET_OBJECT_MEMORY:
    475  1.1  christos #ifdef PT_IO
    476  1.1  christos       /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
    477  1.1  christos 	 request that promises to be much more efficient in reading
    478  1.1  christos 	 and writing data in the traced process's address space.  */
    479  1.1  christos       {
    480  1.1  christos 	struct ptrace_io_desc piod;
    481  1.1  christos 
    482  1.1  christos 	/* NOTE: We assume that there are no distinct address spaces
    483  1.1  christos 	   for instruction and data.  However, on OpenBSD 3.9 and
    484  1.1  christos 	   later, PIOD_WRITE_D doesn't allow changing memory that's
    485  1.1  christos 	   mapped read-only.  Since most code segments will be
    486  1.1  christos 	   read-only, using PIOD_WRITE_D will prevent us from
    487  1.1  christos 	   inserting breakpoints, so we use PIOD_WRITE_I instead.  */
    488  1.1  christos 	piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
    489  1.1  christos 	piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
    490  1.1  christos 	piod.piod_offs = (void *) (long) offset;
    491  1.1  christos 	piod.piod_len = len;
    492  1.1  christos 
    493  1.3  christos 	errno = 0;
    494  1.3  christos 	if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
    495  1.3  christos 	  {
    496  1.3  christos 	    /* Return the actual number of bytes read or written.  */
    497  1.3  christos 	    *xfered_len = piod.piod_len;
    498  1.1  christos 	    return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
    499  1.1  christos 	  }
    500  1.1  christos 	/* If the PT_IO request is somehow not supported, fallback on
    501  1.6  christos 	   using PT_WRITE_D/PT_READ_D.  Otherwise we will return zero
    502  1.6  christos 	   to indicate failure.  */
    503  1.6  christos 	if (errno == EACCES)
    504  1.6  christos 	  {
    505  1.6  christos 	    fprintf_unfiltered (gdb_stderr, "Cannot %s process at %p (%s). "
    506  1.6  christos 				"Is PaX MPROTECT active? See security(7), "
    507  1.6  christos 				"sysctl(7), paxctl(8)\n", writebuf ? "write to" :
    508  1.6  christos 				"read from", piod.piod_offs,
    509  1.6  christos 				strerror(errno));
    510  1.1  christos 	    return TARGET_XFER_E_IO;	/* Some other error perhaps? */
    511  1.3  christos 	  }
    512  1.1  christos 	if (errno != EINVAL)
    513  1.1  christos 	  return TARGET_XFER_EOF;
    514  1.1  christos       }
    515  1.1  christos #endif
    516  1.1  christos       {
    517  1.1  christos 	union
    518  1.1  christos 	{
    519  1.1  christos 	  PTRACE_TYPE_RET word;
    520  1.1  christos 	  gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
    521  1.3  christos 	} buffer;
    522  1.1  christos 	ULONGEST rounded_offset;
    523  1.1  christos 	ULONGEST partial_len;
    524  1.1  christos 
    525  1.1  christos 	/* Round the start offset down to the next long word
    526  1.1  christos 	   boundary.  */
    527  1.1  christos 	rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);
    528  1.1  christos 
    529  1.1  christos 	/* Since ptrace will transfer a single word starting at that
    530  1.1  christos 	   rounded_offset the partial_len needs to be adjusted down to
    531  1.1  christos 	   that (remember this function only does a single transfer).
    532  1.1  christos 	   Should the required length be even less, adjust it down
    533  1.1  christos 	   again.  */
    534  1.1  christos 	partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
    535  1.1  christos 	if (partial_len > len)
    536  1.1  christos 	  partial_len = len;
    537  1.1  christos 
    538  1.1  christos 	if (writebuf)
    539  1.1  christos 	  {
    540  1.1  christos 	    /* If OFFSET:PARTIAL_LEN is smaller than
    541  1.1  christos 	       ROUNDED_OFFSET:WORDSIZE then a read/modify write will
    542  1.1  christos 	       be needed.  Read in the entire word.  */
    543  1.1  christos 	    if (rounded_offset < offset
    544  1.1  christos 		|| (offset + partial_len
    545  1.1  christos 		    < rounded_offset + sizeof (PTRACE_TYPE_RET)))
    546  1.1  christos 	      /* Need part of initial word -- fetch it.  */
    547  1.1  christos 	      buffer.word = ptrace (PT_READ_I, pid,
    548  1.1  christos 				    (PTRACE_TYPE_ARG3)(uintptr_t)
    549  1.1  christos 				    rounded_offset, 0);
    550  1.1  christos 
    551  1.1  christos 	    /* Copy data to be written over corresponding part of
    552  1.1  christos 	       buffer.  */
    553  1.1  christos 	    memcpy (buffer.byte + (offset - rounded_offset),
    554  1.1  christos 		    writebuf, partial_len);
    555  1.1  christos 
    556  1.1  christos 	    errno = 0;
    557  1.1  christos 	    ptrace (PT_WRITE_D, pid,
    558  1.1  christos 		    (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
    559  1.1  christos 		    buffer.word);
    560  1.1  christos 	    if (errno)
    561  1.1  christos 	      {
    562  1.1  christos 		/* Using the appropriate one (I or D) is necessary for
    563  1.1  christos 		   Gould NP1, at least.  */
    564  1.1  christos 		errno = 0;
    565  1.1  christos 		ptrace (PT_WRITE_I, pid,
    566  1.1  christos 			(PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
    567  1.3  christos 			buffer.word);
    568  1.1  christos 		if (errno)
    569  1.1  christos 		  return TARGET_XFER_EOF;
    570  1.1  christos 	      }
    571  1.1  christos 	  }
    572  1.1  christos 
    573  1.1  christos 	if (readbuf)
    574  1.1  christos 	  {
    575  1.1  christos 	    errno = 0;
    576  1.1  christos 	    buffer.word = ptrace (PT_READ_I, pid,
    577  1.1  christos 				  (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
    578  1.3  christos 				  0);
    579  1.1  christos 	    if (errno)
    580  1.1  christos 	      return TARGET_XFER_EOF;
    581  1.1  christos 	    /* Copy appropriate bytes out of the buffer.  */
    582  1.1  christos 	    memcpy (readbuf, buffer.byte + (offset - rounded_offset),
    583  1.1  christos 		    partial_len);
    584  1.3  christos 	  }
    585  1.3  christos 
    586  1.1  christos 	*xfered_len = partial_len;
    587  1.1  christos 	return TARGET_XFER_OK;
    588  1.1  christos       }
    589  1.3  christos 
    590  1.1  christos     case TARGET_OBJECT_UNWIND_TABLE:
    591  1.1  christos       return TARGET_XFER_E_IO;
    592  1.1  christos 
    593  1.1  christos     case TARGET_OBJECT_AUXV:
    594  1.1  christos #if defined (PT_IO) && defined (PIOD_READ_AUXV)
    595  1.1  christos       /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
    596  1.1  christos 	 request that allows us to read the auxilliary vector.  Other
    597  1.1  christos 	 BSD's may follow if they feel the need to support PIE.  */
    598  1.1  christos       {
    599  1.1  christos 	struct ptrace_io_desc piod;
    600  1.3  christos 
    601  1.1  christos 	if (writebuf)
    602  1.1  christos 	  return TARGET_XFER_E_IO;
    603  1.1  christos 	piod.piod_op = PIOD_READ_AUXV;
    604  1.1  christos 	piod.piod_addr = readbuf;
    605  1.1  christos 	piod.piod_offs = (void *) (long) offset;
    606  1.1  christos 	piod.piod_len = len;
    607  1.1  christos 
    608  1.3  christos 	errno = 0;
    609  1.3  christos 	if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
    610  1.3  christos 	  {
    611  1.3  christos 	    /* Return the actual number of bytes read or written.  */
    612  1.3  christos 	    *xfered_len = piod.piod_len;
    613  1.1  christos 	    return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
    614  1.1  christos 	  }
    615  1.3  christos       }
    616  1.1  christos #endif
    617  1.1  christos       return TARGET_XFER_E_IO;
    618  1.3  christos 
    619  1.1  christos     case TARGET_OBJECT_WCOOKIE:
    620  1.1  christos       return TARGET_XFER_E_IO;
    621  1.3  christos 
    622  1.1  christos     default:
    623  1.1  christos       return TARGET_XFER_E_IO;
    624  1.1  christos     }
    625  1.1  christos }
    626  1.1  christos 
    627  1.1  christos /* Return non-zero if the thread specified by PTID is alive.  */
    628  1.1  christos 
    629  1.1  christos static int
    630  1.1  christos inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
    631  1.1  christos {
    632  1.1  christos   /* ??? Is kill the right way to do this?  */
    633  1.1  christos   return (kill (ptid_get_pid (ptid), 0) != -1);
    634  1.1  christos }
    635  1.1  christos 
    636  1.1  christos /* Print status information about what we're accessing.  */
    637  1.1  christos 
    638  1.1  christos static void
    639  1.1  christos inf_ptrace_files_info (struct target_ops *ignore)
    640  1.1  christos {
    641  1.1  christos   struct inferior *inf = current_inferior ();
    642  1.1  christos 
    643  1.1  christos   printf_filtered (_("\tUsing the running image of %s %s.\n"),
    644  1.1  christos 		   inf->attach_flag ? "attached" : "child",
    645  1.1  christos 		   target_pid_to_str (inferior_ptid));
    646  1.1  christos }
    647  1.1  christos 
    648  1.1  christos static char *
    649  1.1  christos inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
    650  1.1  christos {
    651  1.1  christos   return normal_pid_to_str (ptid);
    652  1.1  christos }
    653  1.1  christos 
    654  1.1  christos #if defined (PT_IO) && defined (PIOD_READ_AUXV)
    655  1.1  christos 
    656  1.1  christos /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
    657  1.1  christos    Return 0 if *READPTR is already at the end of the buffer.
    658  1.1  christos    Return -1 if there is insufficient buffer for a whole entry.
    659  1.1  christos    Return 1 if an entry was read into *TYPEP and *VALP.  */
    660  1.1  christos 
    661  1.1  christos static int
    662  1.1  christos inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
    663  1.1  christos 		       gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
    664  1.1  christos {
    665  1.1  christos   struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
    666  1.1  christos   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
    667  1.1  christos   const int sizeof_auxv_type = TYPE_LENGTH (int_type);
    668  1.1  christos   const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
    669  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
    670  1.1  christos   gdb_byte *ptr = *readptr;
    671  1.1  christos 
    672  1.1  christos   if (endptr == ptr)
    673  1.1  christos     return 0;
    674  1.1  christos 
    675  1.1  christos   if (endptr - ptr < 2 * sizeof_auxv_val)
    676  1.1  christos     return -1;
    677  1.1  christos 
    678  1.1  christos   *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
    679  1.1  christos   ptr += sizeof_auxv_val;	/* Alignment.  */
    680  1.1  christos   *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
    681  1.1  christos   ptr += sizeof_auxv_val;
    682  1.1  christos 
    683  1.1  christos   *readptr = ptr;
    684  1.1  christos   return 1;
    685  1.1  christos }
    686  1.1  christos 
    687  1.1  christos #endif
    688  1.1  christos 
    689  1.1  christos /* Create a prototype ptrace target.  The client can override it with
    690  1.1  christos    local methods.  */
    691  1.1  christos 
    692  1.1  christos struct target_ops *
    693  1.1  christos inf_ptrace_target (void)
    694  1.1  christos {
    695  1.1  christos   struct target_ops *t = inf_child_target ();
    696  1.1  christos 
    697  1.1  christos   t->to_attach = inf_ptrace_attach;
    698  1.1  christos   t->to_detach = inf_ptrace_detach;
    699  1.1  christos   t->to_resume = inf_ptrace_resume;
    700  1.1  christos   t->to_wait = inf_ptrace_wait;
    701  1.1  christos   t->to_files_info = inf_ptrace_files_info;
    702  1.1  christos   t->to_kill = inf_ptrace_kill;
    703  1.1  christos   t->to_create_inferior = inf_ptrace_create_inferior;
    704  1.5  christos #ifdef PT_GET_PROCESS_STATE
    705  1.5  christos   t->to_follow_fork = inf_ptrace_follow_fork;
    706  1.1  christos   t->to_insert_fork_catchpoint = inf_ptrace_insert_fork_catchpoint;
    707  1.1  christos   t->to_remove_fork_catchpoint = inf_ptrace_remove_fork_catchpoint;
    708  1.1  christos   t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
    709  1.1  christos   t->to_post_attach = inf_ptrace_post_attach;
    710  1.1  christos #endif
    711  1.1  christos   t->to_mourn_inferior = inf_ptrace_mourn_inferior;
    712  1.6  christos   t->to_thread_alive = inf_ptrace_thread_alive;
    713  1.1  christos   t->to_pid_to_str = inf_ptrace_pid_to_str;
    714  1.1  christos   t->to_interrupt = inf_ptrace_interrupt;
    715  1.1  christos   t->to_xfer_partial = inf_ptrace_xfer_partial;
    716  1.1  christos #if defined (PT_IO) && defined (PIOD_READ_AUXV)
    717  1.1  christos   t->to_auxv_parse = inf_ptrace_auxv_parse;
    718  1.1  christos #endif
    719  1.1  christos 
    720  1.1  christos   return t;
    721  1.1  christos }
    722  1.1  christos 
    723  1.1  christos 
    725  1.1  christos /* Pointer to a function that returns the offset within the user area
    726  1.1  christos    where a particular register is stored.  */
    727  1.1  christos static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
    728  1.1  christos 
    729  1.1  christos /* Fetch register REGNUM from the inferior.  */
    730  1.1  christos 
    731  1.1  christos static void
    732  1.1  christos inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
    733  1.1  christos {
    734  1.1  christos   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    735  1.1  christos   CORE_ADDR addr;
    736  1.1  christos   size_t size;
    737  1.1  christos   PTRACE_TYPE_RET *buf;
    738  1.1  christos   int pid, i;
    739  1.1  christos 
    740  1.1  christos   /* This isn't really an address, but ptrace thinks of it as one.  */
    741  1.1  christos   addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
    742  1.1  christos   if (addr == (CORE_ADDR)-1
    743  1.1  christos       || gdbarch_cannot_fetch_register (gdbarch, regnum))
    744  1.1  christos     {
    745  1.1  christos       regcache_raw_supply (regcache, regnum, NULL);
    746  1.1  christos       return;
    747  1.1  christos     }
    748  1.1  christos 
    749  1.1  christos   /* Cater for systems like GNU/Linux, that implement threads as
    750  1.1  christos      separate processes.  */
    751  1.1  christos   pid = ptid_get_lwp (inferior_ptid);
    752  1.1  christos   if (pid == 0)
    753  1.1  christos     pid = ptid_get_pid (inferior_ptid);
    754  1.6  christos 
    755  1.1  christos   size = register_size (gdbarch, regnum);
    756  1.1  christos   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
    757  1.1  christos   buf = (PTRACE_TYPE_RET *) alloca (size);
    758  1.1  christos 
    759  1.1  christos   /* Read the register contents from the inferior a chunk at a time.  */
    760  1.1  christos   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
    761  1.1  christos     {
    762  1.1  christos       errno = 0;
    763  1.1  christos       buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
    764  1.1  christos       if (errno != 0)
    765  1.1  christos 	error (_("Couldn't read register %s (#%d): %s."),
    766  1.1  christos 	       gdbarch_register_name (gdbarch, regnum),
    767  1.1  christos 	       regnum, safe_strerror (errno));
    768  1.1  christos 
    769  1.1  christos       addr += sizeof (PTRACE_TYPE_RET);
    770  1.1  christos     }
    771  1.1  christos   regcache_raw_supply (regcache, regnum, buf);
    772  1.1  christos }
    773  1.1  christos 
    774  1.1  christos /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
    775  1.1  christos    for all registers.  */
    776  1.1  christos 
    777  1.1  christos static void
    778  1.1  christos inf_ptrace_fetch_registers (struct target_ops *ops,
    779  1.1  christos 			    struct regcache *regcache, int regnum)
    780  1.1  christos {
    781  1.1  christos   if (regnum == -1)
    782  1.1  christos     for (regnum = 0;
    783  1.1  christos 	 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
    784  1.1  christos 	 regnum++)
    785  1.1  christos       inf_ptrace_fetch_register (regcache, regnum);
    786  1.1  christos   else
    787  1.1  christos     inf_ptrace_fetch_register (regcache, regnum);
    788  1.1  christos }
    789  1.1  christos 
    790  1.1  christos /* Store register REGNUM into the inferior.  */
    791  1.1  christos 
    792  1.1  christos static void
    793  1.1  christos inf_ptrace_store_register (const struct regcache *regcache, int regnum)
    794  1.1  christos {
    795  1.1  christos   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    796  1.1  christos   CORE_ADDR addr;
    797  1.1  christos   size_t size;
    798  1.1  christos   PTRACE_TYPE_RET *buf;
    799  1.1  christos   int pid, i;
    800  1.1  christos 
    801  1.1  christos   /* This isn't really an address, but ptrace thinks of it as one.  */
    802  1.1  christos   addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
    803  1.1  christos   if (addr == (CORE_ADDR)-1
    804  1.1  christos       || gdbarch_cannot_store_register (gdbarch, regnum))
    805  1.1  christos     return;
    806  1.1  christos 
    807  1.1  christos   /* Cater for systems like GNU/Linux, that implement threads as
    808  1.1  christos      separate processes.  */
    809  1.1  christos   pid = ptid_get_lwp (inferior_ptid);
    810  1.1  christos   if (pid == 0)
    811  1.1  christos     pid = ptid_get_pid (inferior_ptid);
    812  1.6  christos 
    813  1.1  christos   size = register_size (gdbarch, regnum);
    814  1.1  christos   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
    815  1.1  christos   buf = (PTRACE_TYPE_RET *) alloca (size);
    816  1.1  christos 
    817  1.1  christos   /* Write the register contents into the inferior a chunk at a time.  */
    818  1.1  christos   regcache_raw_collect (regcache, regnum, buf);
    819  1.1  christos   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
    820  1.1  christos     {
    821  1.1  christos       errno = 0;
    822  1.1  christos       ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
    823  1.1  christos       if (errno != 0)
    824  1.1  christos 	error (_("Couldn't write register %s (#%d): %s."),
    825  1.1  christos 	       gdbarch_register_name (gdbarch, regnum),
    826  1.1  christos 	       regnum, safe_strerror (errno));
    827  1.1  christos 
    828  1.1  christos       addr += sizeof (PTRACE_TYPE_RET);
    829  1.1  christos     }
    830  1.1  christos }
    831  1.1  christos 
    832  1.1  christos /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
    833  1.1  christos    this for all registers.  */
    834  1.1  christos 
    835  1.1  christos static void
    836  1.1  christos inf_ptrace_store_registers (struct target_ops *ops,
    837  1.1  christos 			    struct regcache *regcache, int regnum)
    838  1.1  christos {
    839  1.1  christos   if (regnum == -1)
    840  1.1  christos     for (regnum = 0;
    841  1.1  christos 	 regnum < gdbarch_num_regs (get_regcache_arch (regcache));
    842  1.1  christos 	 regnum++)
    843  1.1  christos       inf_ptrace_store_register (regcache, regnum);
    844  1.1  christos   else
    845  1.1  christos     inf_ptrace_store_register (regcache, regnum);
    846  1.1  christos }
    847  1.1  christos 
    848  1.1  christos /* Create a "traditional" ptrace target.  REGISTER_U_OFFSET should be
    849  1.1  christos    a function returning the offset within the user area where a
    850  1.1  christos    particular register is stored.  */
    851  1.1  christos 
    852  1.1  christos struct target_ops *
    853  1.1  christos inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
    854  1.1  christos 					(struct gdbarch *, int, int))
    855  1.1  christos {
    856  1.1  christos   struct target_ops *t = inf_ptrace_target();
    857  1.1  christos 
    858  1.1  christos   gdb_assert (register_u_offset);
    859  1.1  christos   inf_ptrace_register_u_offset = register_u_offset;
    860  1.1  christos   t->to_fetch_registers = inf_ptrace_fetch_registers;
    861  1.1  christos   t->to_store_registers = inf_ptrace_store_registers;
    862                
    863                  return t;
    864                }
    865