gdb/inf-ptrace.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Low-level child interface to ptrace.

  2.    Copyright (C) 1988-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "command.h"
  16. #include "inferior.h"
  17. #include "inflow.h"
  18. #include "terminal.h"
  19. #include "gdbcore.h"
  20. #include "regcache.h"
  21. #include "gdb_ptrace.h"
  22. #include "gdb_wait.h"
  23. #include <signal.h>

  24. #include "inf-ptrace.h"
  25. #include "inf-child.h"
  26. #include "gdbthread.h"



  27. #ifdef PT_GET_PROCESS_STATE

  28. /* Target hook for follow_fork.  On entry and at return inferior_ptid is
  29.    the ptid of the followed inferior.  */

  30. static int
  31. inf_ptrace_follow_fork (struct target_ops *ops, int follow_child,
  32.                         int detach_fork)
  33. {
  34.   if (!follow_child)
  35.     {
  36.       pid_t child_pid = inferior_thread->pending_follow.value.related_pid;

  37.       /* Breakpoints have already been detached from the child by
  38.          infrun.c.  */

  39.       if (ptrace (PT_DETACH, child_pid, (PTRACE_TYPE_ARG3)1, 0) == -1)
  40.         perror_with_name (("ptrace"));
  41.     }

  42.   return 0;
  43. }

  44. #endif /* PT_GET_PROCESS_STATE */


  45. /* Prepare to be traced.  */

  46. static void
  47. inf_ptrace_me (void)
  48. {
  49.   /* "Trace me, Dr. Memory!"  */
  50.   ptrace (PT_TRACE_ME, 0, (PTRACE_TYPE_ARG3)0, 0);
  51. }

  52. /* Start a new inferior Unix child process.  EXEC_FILE is the file to
  53.    run, ALLARGS is a string containing the arguments to the program.
  54.    ENV is the environment vector to pass.  If FROM_TTY is non-zero, be
  55.    chatty about it.  */

  56. static void
  57. inf_ptrace_create_inferior (struct target_ops *ops,
  58.                             char *exec_file, char *allargs, char **env,
  59.                             int from_tty)
  60. {
  61.   int pid;

  62.   /* Do not change either targets above or the same target if already present.
  63.      The reason is the target stack is shared across multiple inferiors.  */
  64.   int ops_already_pushed = target_is_pushed (ops);
  65.   struct cleanup *back_to = make_cleanup (null_cleanup, NULL);

  66.   if (! ops_already_pushed)
  67.     {
  68.       /* Clear possible core file with its process_stratum.  */
  69.       push_target (ops);
  70.       make_cleanup_unpush_target (ops);
  71.     }

  72.   pid = fork_inferior (exec_file, allargs, env, inf_ptrace_me, NULL,
  73.                        NULL, NULL, NULL);

  74.   discard_cleanups (back_to);

  75.   startup_inferior (START_INFERIOR_TRAPS_EXPECTED);

  76.   /* On some targets, there must be some explicit actions taken after
  77.      the inferior has been started up.  */
  78.   target_post_startup_inferior (pid_to_ptid (pid));
  79. }

  80. #ifdef PT_GET_PROCESS_STATE

  81. static void
  82. inf_ptrace_post_startup_inferior (struct target_ops *self, ptid_t pid)
  83. {
  84.   ptrace_event_t pe;

  85.   /* Set the initial event mask.  */
  86.   memset (&pe, 0, sizeof pe);
  87.   pe.pe_set_event |= PTRACE_FORK;
  88.   if (ptrace (PT_SET_EVENT_MASK, ptid_get_pid (pid),
  89.               (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
  90.     perror_with_name (("ptrace"));
  91. }

  92. #endif

  93. /* Clean up a rotting corpse of an inferior after it died.  */

  94. static void
  95. inf_ptrace_mourn_inferior (struct target_ops *ops)
  96. {
  97.   int status;

  98.   /* Wait just one more time to collect the inferior's exit status.
  99.      Do not check whether this succeeds though, since we may be
  100.      dealing with a process that we attached to.  Such a process will
  101.      only report its exit status to its original parent.  */
  102.   waitpid (ptid_get_pid (inferior_ptid), &status, 0);

  103.   inf_child_mourn_inferior (ops);
  104. }

  105. /* Attach to the process specified by ARGS.  If FROM_TTY is non-zero,
  106.    be chatty about it.  */

  107. static void
  108. inf_ptrace_attach (struct target_ops *ops, const char *args, int from_tty)
  109. {
  110.   char *exec_file;
  111.   pid_t pid;
  112.   struct inferior *inf;

  113.   /* Do not change either targets above or the same target if already present.
  114.      The reason is the target stack is shared across multiple inferiors.  */
  115.   int ops_already_pushed = target_is_pushed (ops);
  116.   struct cleanup *back_to = make_cleanup (null_cleanup, NULL);

  117.   pid = parse_pid_to_attach (args);

  118.   if (pid == getpid ())                /* Trying to masturbate?  */
  119.     error (_("I refuse to debug myself!"));

  120.   if (! ops_already_pushed)
  121.     {
  122.       /* target_pid_to_str already uses the target.  Also clear possible core
  123.          file with its process_stratum.  */
  124.       push_target (ops);
  125.       make_cleanup_unpush_target (ops);
  126.     }

  127.   if (from_tty)
  128.     {
  129.       exec_file = get_exec_file (0);

  130.       if (exec_file)
  131.         printf_unfiltered (_("Attaching to program: %s, %s\n"), exec_file,
  132.                            target_pid_to_str (pid_to_ptid (pid)));
  133.       else
  134.         printf_unfiltered (_("Attaching to %s\n"),
  135.                            target_pid_to_str (pid_to_ptid (pid)));

  136.       gdb_flush (gdb_stdout);
  137.     }

  138. #ifdef PT_ATTACH
  139.   errno = 0;
  140.   ptrace (PT_ATTACH, pid, (PTRACE_TYPE_ARG3)0, 0);
  141.   if (errno != 0)
  142.     perror_with_name (("ptrace"));
  143. #else
  144.   error (_("This system does not support attaching to a process"));
  145. #endif

  146.   inf = current_inferior ();
  147.   inferior_appeared (inf, pid);
  148.   inf->attach_flag = 1;
  149.   inferior_ptid = pid_to_ptid (pid);

  150.   /* Always add a main thread.  If some target extends the ptrace
  151.      target, it should decorate the ptid later with more info.  */
  152.   add_thread_silent (inferior_ptid);

  153.   discard_cleanups (back_to);
  154. }

  155. #ifdef PT_GET_PROCESS_STATE

  156. static void
  157. inf_ptrace_post_attach (struct target_ops *self, int pid)
  158. {
  159.   ptrace_event_t pe;

  160.   /* Set the initial event mask.  */
  161.   memset (&pe, 0, sizeof pe);
  162.   pe.pe_set_event |= PTRACE_FORK;
  163.   if (ptrace (PT_SET_EVENT_MASK, pid,
  164.               (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
  165.     perror_with_name (("ptrace"));
  166. }

  167. #endif

  168. /* Detach from the inferior, optionally passing it the signal
  169.    specified by ARGS.  If FROM_TTY is non-zero, be chatty about it.  */

  170. static void
  171. inf_ptrace_detach (struct target_ops *ops, const char *args, int from_tty)
  172. {
  173.   pid_t pid = ptid_get_pid (inferior_ptid);
  174.   int sig = 0;

  175.   if (from_tty)
  176.     {
  177.       char *exec_file = get_exec_file (0);
  178.       if (exec_file == 0)
  179.         exec_file = "";
  180.       printf_unfiltered (_("Detaching from program: %s, %s\n"), exec_file,
  181.                          target_pid_to_str (pid_to_ptid (pid)));
  182.       gdb_flush (gdb_stdout);
  183.     }
  184.   if (args)
  185.     sig = atoi (args);

  186. #ifdef PT_DETACH
  187.   /* We'd better not have left any breakpoints in the program or it'll
  188.      die when it hits one.  Also note that this may only work if we
  189.      previously attached to the inferior.  It *might* work if we
  190.      started the process ourselves.  */
  191.   errno = 0;
  192.   ptrace (PT_DETACH, pid, (PTRACE_TYPE_ARG3)1, sig);
  193.   if (errno != 0)
  194.     perror_with_name (("ptrace"));
  195. #else
  196.   error (_("This system does not support detaching from a process"));
  197. #endif

  198.   inferior_ptid = null_ptid;
  199.   detach_inferior (pid);

  200.   inf_child_maybe_unpush_target (ops);
  201. }

  202. /* Kill the inferior.  */

  203. static void
  204. inf_ptrace_kill (struct target_ops *ops)
  205. {
  206.   pid_t pid = ptid_get_pid (inferior_ptid);
  207.   int status;

  208.   if (pid == 0)
  209.     return;

  210.   ptrace (PT_KILL, pid, (PTRACE_TYPE_ARG3)0, 0);
  211.   waitpid (pid, &status, 0);

  212.   target_mourn_inferior ();
  213. }

  214. /* Stop the inferior.  */

  215. static void
  216. inf_ptrace_stop (struct target_ops *self, ptid_t ptid)
  217. {
  218.   /* Send a SIGINT to the process group.  This acts just like the user
  219.      typed a ^C on the controlling terminal.  Note that using a
  220.      negative process number in kill() is a System V-ism.  The proper
  221.      BSD interface is killpg().  However, all modern BSDs support the
  222.      System V interface too.  */
  223.   kill (-inferior_process_group (), SIGINT);
  224. }

  225. /* Resume execution of thread PTID, or all threads if PTID is -1.  If
  226.    STEP is nonzero, single-step it.  If SIGNAL is nonzero, give it
  227.    that signal.  */

  228. static void
  229. inf_ptrace_resume (struct target_ops *ops,
  230.                    ptid_t ptid, int step, enum gdb_signal signal)
  231. {
  232.   pid_t pid = ptid_get_pid (ptid);
  233.   int request;

  234.   if (pid == -1)
  235.     /* Resume all threads.  Traditionally ptrace() only supports
  236.        single-threaded processes, so simply resume the inferior.  */
  237.     pid = ptid_get_pid (inferior_ptid);

  238.   if (catch_syscall_enabled () > 0)
  239.     request = PT_SYSCALL;
  240.   else
  241.     request = PT_CONTINUE;

  242.   if (step)
  243.     {
  244.       /* If this system does not support PT_STEP, a higher level
  245.          function will have called single_step() to transmute the step
  246.          request into a continue request (by setting breakpoints on
  247.          all possible successor instructions), so we don't have to
  248.          worry about that here.  */
  249.       request = PT_STEP;
  250.     }

  251.   /* An address of (PTRACE_TYPE_ARG3)1 tells ptrace to continue from
  252.      where it was.  If GDB wanted it to start some other way, we have
  253.      already written a new program counter value to the child.  */
  254.   errno = 0;
  255.   ptrace (request, pid, (PTRACE_TYPE_ARG3)1, gdb_signal_to_host (signal));
  256.   if (errno != 0)
  257.     perror_with_name (("ptrace"));
  258. }

  259. /* Wait for the child specified by PTID to do something.  Return the
  260.    process ID of the child, or MINUS_ONE_PTID in case of error; store
  261.    the status in *OURSTATUS.  */

  262. static ptid_t
  263. inf_ptrace_wait (struct target_ops *ops,
  264.                  ptid_t ptid, struct target_waitstatus *ourstatus, int options)
  265. {
  266.   pid_t pid;
  267.   int status, save_errno;

  268.   do
  269.     {
  270.       set_sigint_trap ();

  271.       do
  272.         {
  273.           pid = waitpid (ptid_get_pid (ptid), &status, 0);
  274.           save_errno = errno;
  275.         }
  276.       while (pid == -1 && errno == EINTR);

  277.       clear_sigint_trap ();

  278.       if (pid == -1)
  279.         {
  280.           fprintf_unfiltered (gdb_stderr,
  281.                               _("Child process unexpectedly missing: %s.\n"),
  282.                               safe_strerror (save_errno));

  283.           /* Claim it exited with unknown signal.  */
  284.           ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
  285.           ourstatus->value.sig = GDB_SIGNAL_UNKNOWN;
  286.           return inferior_ptid;
  287.         }

  288.       /* Ignore terminated detached child processes.  */
  289.       if (!WIFSTOPPED (status) && pid != ptid_get_pid (inferior_ptid))
  290.         pid = -1;
  291.     }
  292.   while (pid == -1);

  293. #ifdef PT_GET_PROCESS_STATE
  294.   if (WIFSTOPPED (status))
  295.     {
  296.       ptrace_state_t pe;
  297.       pid_t fpid;

  298.       if (ptrace (PT_GET_PROCESS_STATE, pid,
  299.                   (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
  300.         perror_with_name (("ptrace"));

  301.       switch (pe.pe_report_event)
  302.         {
  303.         case PTRACE_FORK:
  304.           ourstatus->kind = TARGET_WAITKIND_FORKED;
  305.           ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);

  306.           /* Make sure the other end of the fork is stopped too.  */
  307.           fpid = waitpid (pe.pe_other_pid, &status, 0);
  308.           if (fpid == -1)
  309.             perror_with_name (("waitpid"));

  310.           if (ptrace (PT_GET_PROCESS_STATE, fpid,
  311.                       (PTRACE_TYPE_ARG3)&pe, sizeof pe) == -1)
  312.             perror_with_name (("ptrace"));

  313.           gdb_assert (pe.pe_report_event == PTRACE_FORK);
  314.           gdb_assert (pe.pe_other_pid == pid);
  315.           if (fpid == ptid_get_pid (inferior_ptid))
  316.             {
  317.               ourstatus->value.related_pid = pid_to_ptid (pe.pe_other_pid);
  318.               return pid_to_ptid (fpid);
  319.             }

  320.           return pid_to_ptid (pid);
  321.         }
  322.     }
  323. #endif

  324.   store_waitstatus (ourstatus, status);
  325.   return pid_to_ptid (pid);
  326. }

  327. /* Implement the to_xfer_partial target_ops method.  */

  328. static enum target_xfer_status
  329. inf_ptrace_xfer_partial (struct target_ops *ops, enum target_object object,
  330.                          const char *annex, gdb_byte *readbuf,
  331.                          const gdb_byte *writebuf,
  332.                          ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
  333. {
  334.   pid_t pid = ptid_get_pid (inferior_ptid);

  335.   switch (object)
  336.     {
  337.     case TARGET_OBJECT_MEMORY:
  338. #ifdef PT_IO
  339.       /* OpenBSD 3.1, NetBSD 1.6 and FreeBSD 5.0 have a new PT_IO
  340.          request that promises to be much more efficient in reading
  341.          and writing data in the traced process's address space.  */
  342.       {
  343.         struct ptrace_io_desc piod;

  344.         /* NOTE: We assume that there are no distinct address spaces
  345.            for instruction and data.  However, on OpenBSD 3.9 and
  346.            later, PIOD_WRITE_D doesn't allow changing memory that's
  347.            mapped read-only.  Since most code segments will be
  348.            read-only, using PIOD_WRITE_D will prevent us from
  349.            inserting breakpoints, so we use PIOD_WRITE_I instead.  */
  350.         piod.piod_op = writebuf ? PIOD_WRITE_I : PIOD_READ_D;
  351.         piod.piod_addr = writebuf ? (void *) writebuf : readbuf;
  352.         piod.piod_offs = (void *) (long) offset;
  353.         piod.piod_len = len;

  354.         errno = 0;
  355.         if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
  356.           {
  357.             /* Return the actual number of bytes read or written.  */
  358.             *xfered_len = piod.piod_len;
  359.             return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
  360.           }
  361.         /* If the PT_IO request is somehow not supported, fallback on
  362.            using PT_WRITE_D/PT_READ_D.  Otherwise we will return zero
  363.            to indicate failure.  */
  364.         if (errno != EINVAL)
  365.           return TARGET_XFER_EOF;
  366.       }
  367. #endif
  368.       {
  369.         union
  370.         {
  371.           PTRACE_TYPE_RET word;
  372.           gdb_byte byte[sizeof (PTRACE_TYPE_RET)];
  373.         } buffer;
  374.         ULONGEST rounded_offset;
  375.         ULONGEST partial_len;

  376.         /* Round the start offset down to the next long word
  377.            boundary.  */
  378.         rounded_offset = offset & -(ULONGEST) sizeof (PTRACE_TYPE_RET);

  379.         /* Since ptrace will transfer a single word starting at that
  380.            rounded_offset the partial_len needs to be adjusted down to
  381.            that (remember this function only does a single transfer).
  382.            Should the required length be even less, adjust it down
  383.            again.  */
  384.         partial_len = (rounded_offset + sizeof (PTRACE_TYPE_RET)) - offset;
  385.         if (partial_len > len)
  386.           partial_len = len;

  387.         if (writebuf)
  388.           {
  389.             /* If OFFSET:PARTIAL_LEN is smaller than
  390.                ROUNDED_OFFSET:WORDSIZE then a read/modify write will
  391.                be needed.  Read in the entire word.  */
  392.             if (rounded_offset < offset
  393.                 || (offset + partial_len
  394.                     < rounded_offset + sizeof (PTRACE_TYPE_RET)))
  395.               /* Need part of initial word -- fetch it.  */
  396.               buffer.word = ptrace (PT_READ_I, pid,
  397.                                     (PTRACE_TYPE_ARG3)(uintptr_t)
  398.                                     rounded_offset, 0);

  399.             /* Copy data to be written over corresponding part of
  400.                buffer.  */
  401.             memcpy (buffer.byte + (offset - rounded_offset),
  402.                     writebuf, partial_len);

  403.             errno = 0;
  404.             ptrace (PT_WRITE_D, pid,
  405.                     (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
  406.                     buffer.word);
  407.             if (errno)
  408.               {
  409.                 /* Using the appropriate one (I or D) is necessary for
  410.                    Gould NP1, at least.  */
  411.                 errno = 0;
  412.                 ptrace (PT_WRITE_I, pid,
  413.                         (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
  414.                         buffer.word);
  415.                 if (errno)
  416.                   return TARGET_XFER_EOF;
  417.               }
  418.           }

  419.         if (readbuf)
  420.           {
  421.             errno = 0;
  422.             buffer.word = ptrace (PT_READ_I, pid,
  423.                                   (PTRACE_TYPE_ARG3)(uintptr_t)rounded_offset,
  424.                                   0);
  425.             if (errno)
  426.               return TARGET_XFER_EOF;
  427.             /* Copy appropriate bytes out of the buffer.  */
  428.             memcpy (readbuf, buffer.byte + (offset - rounded_offset),
  429.                     partial_len);
  430.           }

  431.         *xfered_len = partial_len;
  432.         return TARGET_XFER_OK;
  433.       }

  434.     case TARGET_OBJECT_UNWIND_TABLE:
  435.       return TARGET_XFER_E_IO;

  436.     case TARGET_OBJECT_AUXV:
  437. #if defined (PT_IO) && defined (PIOD_READ_AUXV)
  438.       /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
  439.          request that allows us to read the auxilliary vector.  Other
  440.          BSD's may follow if they feel the need to support PIE.  */
  441.       {
  442.         struct ptrace_io_desc piod;

  443.         if (writebuf)
  444.           return TARGET_XFER_E_IO;
  445.         piod.piod_op = PIOD_READ_AUXV;
  446.         piod.piod_addr = readbuf;
  447.         piod.piod_offs = (void *) (long) offset;
  448.         piod.piod_len = len;

  449.         errno = 0;
  450.         if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
  451.           {
  452.             /* Return the actual number of bytes read or written.  */
  453.             *xfered_len = piod.piod_len;
  454.             return (piod.piod_len == 0) ? TARGET_XFER_EOF : TARGET_XFER_OK;
  455.           }
  456.       }
  457. #endif
  458.       return TARGET_XFER_E_IO;

  459.     case TARGET_OBJECT_WCOOKIE:
  460.       return TARGET_XFER_E_IO;

  461.     default:
  462.       return TARGET_XFER_E_IO;
  463.     }
  464. }

  465. /* Return non-zero if the thread specified by PTID is alive.  */

  466. static int
  467. inf_ptrace_thread_alive (struct target_ops *ops, ptid_t ptid)
  468. {
  469.   /* ??? Is kill the right way to do this?  */
  470.   return (kill (ptid_get_pid (ptid), 0) != -1);
  471. }

  472. /* Print status information about what we're accessing.  */

  473. static void
  474. inf_ptrace_files_info (struct target_ops *ignore)
  475. {
  476.   struct inferior *inf = current_inferior ();

  477.   printf_filtered (_("\tUsing the running image of %s %s.\n"),
  478.                    inf->attach_flag ? "attached" : "child",
  479.                    target_pid_to_str (inferior_ptid));
  480. }

  481. static char *
  482. inf_ptrace_pid_to_str (struct target_ops *ops, ptid_t ptid)
  483. {
  484.   return normal_pid_to_str (ptid);
  485. }

  486. #if defined (PT_IO) && defined (PIOD_READ_AUXV)

  487. /* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
  488.    Return 0 if *READPTR is already at the end of the buffer.
  489.    Return -1 if there is insufficient buffer for a whole entry.
  490.    Return 1 if an entry was read into *TYPEP and *VALP.  */

  491. static int
  492. inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
  493.                        gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
  494. {
  495.   struct type *int_type = builtin_type (target_gdbarch ())->builtin_int;
  496.   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  497.   const int sizeof_auxv_type = TYPE_LENGTH (int_type);
  498.   const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
  499.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  500.   gdb_byte *ptr = *readptr;

  501.   if (endptr == ptr)
  502.     return 0;

  503.   if (endptr - ptr < 2 * sizeof_auxv_val)
  504.     return -1;

  505.   *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
  506.   ptr += sizeof_auxv_val;        /* Alignment.  */
  507.   *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
  508.   ptr += sizeof_auxv_val;

  509.   *readptr = ptr;
  510.   return 1;
  511. }

  512. #endif

  513. /* Create a prototype ptrace target.  The client can override it with
  514.    local methods.  */

  515. struct target_ops *
  516. inf_ptrace_target (void)
  517. {
  518.   struct target_ops *t = inf_child_target ();

  519.   t->to_attach = inf_ptrace_attach;
  520.   t->to_detach = inf_ptrace_detach;
  521.   t->to_resume = inf_ptrace_resume;
  522.   t->to_wait = inf_ptrace_wait;
  523.   t->to_files_info = inf_ptrace_files_info;
  524.   t->to_kill = inf_ptrace_kill;
  525.   t->to_create_inferior = inf_ptrace_create_inferior;
  526. #ifdef PT_GET_PROCESS_STATE
  527.   t->to_follow_fork = inf_ptrace_follow_fork;
  528.   t->to_post_startup_inferior = inf_ptrace_post_startup_inferior;
  529.   t->to_post_attach = inf_ptrace_post_attach;
  530. #endif
  531.   t->to_mourn_inferior = inf_ptrace_mourn_inferior;
  532.   t->to_thread_alive = inf_ptrace_thread_alive;
  533.   t->to_pid_to_str = inf_ptrace_pid_to_str;
  534.   t->to_stop = inf_ptrace_stop;
  535.   t->to_xfer_partial = inf_ptrace_xfer_partial;
  536. #if defined (PT_IO) && defined (PIOD_READ_AUXV)
  537.   t->to_auxv_parse = inf_ptrace_auxv_parse;
  538. #endif

  539.   return t;
  540. }


  541. /* Pointer to a function that returns the offset within the user area
  542.    where a particular register is stored.  */
  543. static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);

  544. /* Fetch register REGNUM from the inferior.  */

  545. static void
  546. inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
  547. {
  548.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  549.   CORE_ADDR addr;
  550.   size_t size;
  551.   PTRACE_TYPE_RET *buf;
  552.   int pid, i;

  553.   /* This isn't really an address, but ptrace thinks of it as one.  */
  554.   addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
  555.   if (addr == (CORE_ADDR)-1
  556.       || gdbarch_cannot_fetch_register (gdbarch, regnum))
  557.     {
  558.       regcache_raw_supply (regcache, regnum, NULL);
  559.       return;
  560.     }

  561.   /* Cater for systems like GNU/Linux, that implement threads as
  562.      separate processes.  */
  563.   pid = ptid_get_lwp (inferior_ptid);
  564.   if (pid == 0)
  565.     pid = ptid_get_pid (inferior_ptid);

  566.   size = register_size (gdbarch, regnum);
  567.   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
  568.   buf = alloca (size);

  569.   /* Read the register contents from the inferior a chunk at a time.  */
  570.   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
  571.     {
  572.       errno = 0;
  573.       buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, 0);
  574.       if (errno != 0)
  575.         error (_("Couldn't read register %s (#%d): %s."),
  576.                gdbarch_register_name (gdbarch, regnum),
  577.                regnum, safe_strerror (errno));

  578.       addr += sizeof (PTRACE_TYPE_RET);
  579.     }
  580.   regcache_raw_supply (regcache, regnum, buf);
  581. }

  582. /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
  583.    for all registers.  */

  584. static void
  585. inf_ptrace_fetch_registers (struct target_ops *ops,
  586.                             struct regcache *regcache, int regnum)
  587. {
  588.   if (regnum == -1)
  589.     for (regnum = 0;
  590.          regnum < gdbarch_num_regs (get_regcache_arch (regcache));
  591.          regnum++)
  592.       inf_ptrace_fetch_register (regcache, regnum);
  593.   else
  594.     inf_ptrace_fetch_register (regcache, regnum);
  595. }

  596. /* Store register REGNUM into the inferior.  */

  597. static void
  598. inf_ptrace_store_register (const struct regcache *regcache, int regnum)
  599. {
  600.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  601.   CORE_ADDR addr;
  602.   size_t size;
  603.   PTRACE_TYPE_RET *buf;
  604.   int pid, i;

  605.   /* This isn't really an address, but ptrace thinks of it as one.  */
  606.   addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
  607.   if (addr == (CORE_ADDR)-1
  608.       || gdbarch_cannot_store_register (gdbarch, regnum))
  609.     return;

  610.   /* Cater for systems like GNU/Linux, that implement threads as
  611.      separate processes.  */
  612.   pid = ptid_get_lwp (inferior_ptid);
  613.   if (pid == 0)
  614.     pid = ptid_get_pid (inferior_ptid);

  615.   size = register_size (gdbarch, regnum);
  616.   gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
  617.   buf = alloca (size);

  618.   /* Write the register contents into the inferior a chunk at a time.  */
  619.   regcache_raw_collect (regcache, regnum, buf);
  620.   for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
  621.     {
  622.       errno = 0;
  623.       ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
  624.       if (errno != 0)
  625.         error (_("Couldn't write register %s (#%d): %s."),
  626.                gdbarch_register_name (gdbarch, regnum),
  627.                regnum, safe_strerror (errno));

  628.       addr += sizeof (PTRACE_TYPE_RET);
  629.     }
  630. }

  631. /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
  632.    this for all registers.  */

  633. static void
  634. inf_ptrace_store_registers (struct target_ops *ops,
  635.                             struct regcache *regcache, int regnum)
  636. {
  637.   if (regnum == -1)
  638.     for (regnum = 0;
  639.          regnum < gdbarch_num_regs (get_regcache_arch (regcache));
  640.          regnum++)
  641.       inf_ptrace_store_register (regcache, regnum);
  642.   else
  643.     inf_ptrace_store_register (regcache, regnum);
  644. }

  645. /* Create a "traditional" ptrace target.  REGISTER_U_OFFSET should be
  646.    a function returning the offset within the user area where a
  647.    particular register is stored.  */

  648. struct target_ops *
  649. inf_ptrace_trad_target (CORE_ADDR (*register_u_offset)
  650.                                         (struct gdbarch *, int, int))
  651. {
  652.   struct target_ops *t = inf_ptrace_target();

  653.   gdb_assert (register_u_offset);
  654.   inf_ptrace_register_u_offset = register_u_offset;
  655.   t->to_fetch_registers = inf_ptrace_fetch_registers;
  656.   t->to_store_registers = inf_ptrace_store_registers;

  657.   return t;
  658. }