gdb/nat/linux-ptrace.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Linux-specific ptrace manipulation routines.
  2.    Copyright (C) 2012-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 "common-defs.h"
  15. #include "linux-ptrace.h"
  16. #include "linux-procfs.h"
  17. #include "linux-waitpid.h"
  18. #include "buffer.h"
  19. #include "gdb_wait.h"

  20. #include <stdint.h>

  21. /* Stores the currently supported ptrace options.  A value of
  22.    -1 means we did not check for features yet.  A value of 0 means
  23.    there are no supported features.  */
  24. static int current_ptrace_options = -1;

  25. /* Additional flags to test.  */

  26. static int additional_flags;

  27. /* Find all possible reasons we could fail to attach PID and append
  28.    these as strings to the already initialized BUFFER.  '\0'
  29.    termination of BUFFER must be done by the caller.  */

  30. void
  31. linux_ptrace_attach_fail_reason (pid_t pid, struct buffer *buffer)
  32. {
  33.   pid_t tracerpid;

  34.   tracerpid = linux_proc_get_tracerpid_nowarn (pid);
  35.   if (tracerpid > 0)
  36.     buffer_xml_printf (buffer, _("process %d is already traced "
  37.                                  "by process %d"),
  38.                        (int) pid, (int) tracerpid);

  39.   if (linux_proc_pid_is_zombie_nowarn (pid))
  40.     buffer_xml_printf (buffer, _("process %d is a zombie "
  41.                                  "- the process has already terminated"),
  42.                        (int) pid);
  43. }

  44. /* See linux-ptrace.h.  */

  45. char *
  46. linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err)
  47. {
  48.   static char *reason_string;
  49.   struct buffer buffer;
  50.   char *warnings;
  51.   long lwpid = ptid_get_lwp (ptid);

  52.   xfree (reason_string);

  53.   buffer_init (&buffer);
  54.   linux_ptrace_attach_fail_reason (lwpid, &buffer);
  55.   buffer_grow_str0 (&buffer, "");
  56.   warnings = buffer_finish (&buffer);
  57.   if (warnings[0] != '\0')
  58.     reason_string = xstrprintf ("%s (%d), %s",
  59.                                 strerror (err), err, warnings);
  60.   else
  61.     reason_string = xstrprintf ("%s (%d)",
  62.                                 strerror (err), err);
  63.   xfree (warnings);
  64.   return reason_string;
  65. }

  66. #if defined __i386__ || defined __x86_64__

  67. /* Address of the 'ret' instruction in asm code block below.  */
  68. extern void (linux_ptrace_test_ret_to_nx_instr) (void);

  69. #include <sys/reg.h>
  70. #include <sys/mman.h>
  71. #include <signal.h>

  72. #endif /* defined __i386__ || defined __x86_64__ */

  73. /* Test broken off-trunk Linux kernel patchset for NX support on i386.  It was
  74.    removed in Fedora kernel 88fa1f0332d188795ed73d7ac2b1564e11a0b4cd.

  75.    Test also x86_64 arch for PaX support.  */

  76. static void
  77. linux_ptrace_test_ret_to_nx (void)
  78. {
  79. #if defined __i386__ || defined __x86_64__
  80.   pid_t child, got_pid;
  81.   gdb_byte *return_address, *pc;
  82.   long l;
  83.   int status, kill_status;

  84.   return_address = mmap (NULL, 2, PROT_READ | PROT_WRITE,
  85.                          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  86.   if (return_address == MAP_FAILED)
  87.     {
  88.       warning (_("linux_ptrace_test_ret_to_nx: Cannot mmap: %s"),
  89.                strerror (errno));
  90.       return;
  91.     }

  92.   /* Put there 'int3'.  */
  93.   *return_address = 0xcc;

  94.   child = fork ();
  95.   switch (child)
  96.     {
  97.     case -1:
  98.       warning (_("linux_ptrace_test_ret_to_nx: Cannot fork: %s"),
  99.                strerror (errno));
  100.       return;

  101.     case 0:
  102.       l = ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) NULL,
  103.                   (PTRACE_TYPE_ARG4) NULL);
  104.       if (l != 0)
  105.         warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME: %s"),
  106.                  strerror (errno));
  107.       else
  108.         {
  109. #if defined __i386__
  110.           asm volatile ("pushl %0;"
  111.                         ".globl linux_ptrace_test_ret_to_nx_instr;"
  112.                         "linux_ptrace_test_ret_to_nx_instr:"
  113.                         "ret"
  114.                         : : "r" (return_address) : "%esp", "memory");
  115. #elif defined __x86_64__
  116.           asm volatile ("pushq %0;"
  117.                         ".globl linux_ptrace_test_ret_to_nx_instr;"
  118.                         "linux_ptrace_test_ret_to_nx_instr:"
  119.                         "ret"
  120.                         : : "r" ((uint64_t) (uintptr_t) return_address)
  121.                         : "%rsp", "memory");
  122. #else
  123. # error "!__i386__ && !__x86_64__"
  124. #endif
  125.           gdb_assert_not_reached ("asm block did not terminate");
  126.         }

  127.       _exit (1);
  128.     }

  129.   errno = 0;
  130.   got_pid = waitpid (child, &status, 0);
  131.   if (got_pid != child)
  132.     {
  133.       warning (_("linux_ptrace_test_ret_to_nx: waitpid returned %ld: %s"),
  134.                (long) got_pid, strerror (errno));
  135.       return;
  136.     }

  137.   if (WIFSIGNALED (status))
  138.     {
  139.       if (WTERMSIG (status) != SIGKILL)
  140.         warning (_("linux_ptrace_test_ret_to_nx: WTERMSIG %d is not SIGKILL!"),
  141.                  (int) WTERMSIG (status));
  142.       else
  143.         warning (_("Cannot call inferior functions, Linux kernel PaX "
  144.                    "protection forbids return to non-executable pages!"));
  145.       return;
  146.     }

  147.   if (!WIFSTOPPED (status))
  148.     {
  149.       warning (_("linux_ptrace_test_ret_to_nx: status %d is not WIFSTOPPED!"),
  150.                status);
  151.       return;
  152.     }

  153.   /* We may get SIGSEGV due to missing PROT_EXEC of the return_address.  */
  154.   if (WSTOPSIG (status) != SIGTRAP && WSTOPSIG (status) != SIGSEGV)
  155.     {
  156.       warning (_("linux_ptrace_test_ret_to_nx: "
  157.                  "WSTOPSIG %d is neither SIGTRAP nor SIGSEGV!"),
  158.                (int) WSTOPSIG (status));
  159.       return;
  160.     }

  161.   errno = 0;
  162. #if defined __i386__
  163.   l = ptrace (PTRACE_PEEKUSER, child, (PTRACE_TYPE_ARG3) (uintptr_t) (EIP * 4),
  164.               (PTRACE_TYPE_ARG4) NULL);
  165. #elif defined __x86_64__
  166.   l = ptrace (PTRACE_PEEKUSER, child, (PTRACE_TYPE_ARG3) (uintptr_t) (RIP * 8),
  167.               (PTRACE_TYPE_ARG4) NULL);
  168. #else
  169. # error "!__i386__ && !__x86_64__"
  170. #endif
  171.   if (errno != 0)
  172.     {
  173.       warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_PEEKUSER: %s"),
  174.                strerror (errno));
  175.       return;
  176.     }
  177.   pc = (void *) (uintptr_t) l;

  178.   kill (child, SIGKILL);
  179.   ptrace (PTRACE_KILL, child, (PTRACE_TYPE_ARG3) NULL,
  180.           (PTRACE_TYPE_ARG4) NULL);

  181.   errno = 0;
  182.   got_pid = waitpid (child, &kill_status, 0);
  183.   if (got_pid != child)
  184.     {
  185.       warning (_("linux_ptrace_test_ret_to_nx: "
  186.                  "PTRACE_KILL waitpid returned %ld: %s"),
  187.                (long) got_pid, strerror (errno));
  188.       return;
  189.     }
  190.   if (!WIFSIGNALED (kill_status))
  191.     {
  192.       warning (_("linux_ptrace_test_ret_to_nx: "
  193.                  "PTRACE_KILL status %d is not WIFSIGNALED!"),
  194.                status);
  195.       return;
  196.     }

  197.   /* + 1 is there as x86* stops after the 'int3' instruction.  */
  198.   if (WSTOPSIG (status) == SIGTRAP && pc == return_address + 1)
  199.     {
  200.       /* PASS */
  201.       return;
  202.     }

  203.   /* We may get SIGSEGV due to missing PROT_EXEC of the RETURN_ADDRESS page.  */
  204.   if (WSTOPSIG (status) == SIGSEGV && pc == return_address)
  205.     {
  206.       /* PASS */
  207.       return;
  208.     }

  209.   if ((void (*) (void)) pc != &linux_ptrace_test_ret_to_nx_instr)
  210.     warning (_("linux_ptrace_test_ret_to_nx: PC %p is neither near return "
  211.                "address %p nor is the return instruction %p!"),
  212.              pc, return_address, &linux_ptrace_test_ret_to_nx_instr);
  213.   else
  214.     warning (_("Cannot call inferior functions on this system - "
  215.                "Linux kernel with broken i386 NX (non-executable pages) "
  216.                "support detected!"));
  217. #endif /* defined __i386__ || defined __x86_64__ */
  218. }

  219. /* Helper function to fork a process and make the child process call
  220.    the function FUNCTION, passing CHILD_STACK as parameter.

  221.    For MMU-less targets, clone is used instead of fork, and
  222.    CHILD_STACK is used as stack space for the cloned child.  If NULL,
  223.    stack space is allocated via malloc (and subsequently passed to
  224.    FUNCTION).  For MMU targets, CHILD_STACK is ignored.  */

  225. static int
  226. linux_fork_to_function (gdb_byte *child_stack, void (*function) (gdb_byte *))
  227. {
  228.   int child_pid;

  229.   /* Sanity check the function pointer.  */
  230.   gdb_assert (function != NULL);

  231. #if defined(__UCLIBC__) && defined(HAS_NOMMU)
  232. #define STACK_SIZE 4096

  233.     if (child_stack == NULL)
  234.       child_stack = xmalloc (STACK_SIZE * 4);

  235.     /* Use CLONE_VM instead of fork, to support uClinux (no MMU).  */
  236. #ifdef __ia64__
  237.       child_pid = __clone2 (function, child_stack, STACK_SIZE,
  238.                             CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2);
  239. #else /* !__ia64__ */
  240.       child_pid = clone (function, child_stack + STACK_SIZE,
  241.                          CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2);
  242. #endif /* !__ia64__ */
  243. #else /* !defined(__UCLIBC) && defined(HAS_NOMMU) */
  244.   child_pid = fork ();

  245.   if (child_pid == 0)
  246.     function (NULL);
  247. #endif /* defined(__UCLIBC) && defined(HAS_NOMMU) */

  248.   if (child_pid == -1)
  249.     perror_with_name (("fork"));

  250.   return child_pid;
  251. }

  252. /* A helper function for linux_check_ptrace_features, called after
  253.    the child forks a grandchild.  */

  254. static void
  255. linux_grandchild_function (gdb_byte *child_stack)
  256. {
  257.   /* Free any allocated stack.  */
  258.   xfree (child_stack);

  259.   /* This code is only reacheable by the grandchild (child's child)
  260.      process.  */
  261.   _exit (0);
  262. }

  263. /* A helper function for linux_check_ptrace_features, called after
  264.    the parent process forks a child.  The child allows itself to
  265.    be traced by its parent.  */

  266. static void
  267. linux_child_function (gdb_byte *child_stack)
  268. {
  269.   ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
  270.   kill (getpid (), SIGSTOP);

  271.   /* Fork a grandchild.  */
  272.   linux_fork_to_function (child_stack, linux_grandchild_function);

  273.   /* This code is only reacheable by the child (grandchild's parent)
  274.      process.  */
  275.   _exit (0);
  276. }

  277. static void linux_test_for_tracesysgood (int child_pid);
  278. static void linux_test_for_tracefork (int child_pid);
  279. static void linux_test_for_exitkill (int child_pid);

  280. /* Determine ptrace features available on this target.  */

  281. static void
  282. linux_check_ptrace_features (void)
  283. {
  284.   int child_pid, ret, status;

  285.   /* Initialize the options.  */
  286.   current_ptrace_options = 0;

  287.   /* Fork a child so we can do some testing.  The child will call
  288.      linux_child_function and will get traced.  The child will
  289.      eventually fork a grandchild so we can test fork event
  290.      reporting.  */
  291.   child_pid = linux_fork_to_function (NULL, linux_child_function);

  292.   ret = my_waitpid (child_pid, &status, 0);
  293.   if (ret == -1)
  294.     perror_with_name (("waitpid"));
  295.   else if (ret != child_pid)
  296.     error (_("linux_check_ptrace_features: waitpid: unexpected result %d."),
  297.            ret);
  298.   if (! WIFSTOPPED (status))
  299.     error (_("linux_check_ptrace_features: waitpid: unexpected status %d."),
  300.            status);

  301.   linux_test_for_tracesysgood (child_pid);

  302.   linux_test_for_tracefork (child_pid);

  303.   linux_test_for_exitkill (child_pid);

  304.   /* Clean things up and kill any pending children.  */
  305.   do
  306.     {
  307.       ret = ptrace (PTRACE_KILL, child_pid, (PTRACE_TYPE_ARG3) 0,
  308.                     (PTRACE_TYPE_ARG4) 0);
  309.       if (ret != 0)
  310.         warning (_("linux_check_ptrace_features: failed to kill child"));
  311.       my_waitpid (child_pid, &status, 0);
  312.     }
  313.   while (WIFSTOPPED (status));
  314. }

  315. /* Determine if PTRACE_O_TRACESYSGOOD can be used to catch
  316.    syscalls.  */

  317. static void
  318. linux_test_for_tracesysgood (int child_pid)
  319. {
  320.   int ret;

  321.   if ((additional_flags & PTRACE_O_TRACESYSGOOD) == 0)
  322.     return;

  323.   ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
  324.                 (PTRACE_TYPE_ARG4) PTRACE_O_TRACESYSGOOD);

  325.   if (ret == 0)
  326.     current_ptrace_options |= PTRACE_O_TRACESYSGOOD;
  327. }

  328. /* Determine if PTRACE_O_TRACEFORK can be used to follow fork
  329.    events.  */

  330. static void
  331. linux_test_for_tracefork (int child_pid)
  332. {
  333.   int ret, status;
  334.   long second_pid;

  335.   /* First, set the PTRACE_O_TRACEFORK option.  If this fails, we
  336.      know for sure that it is not supported.  */
  337.   ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
  338.                 (PTRACE_TYPE_ARG4) PTRACE_O_TRACEFORK);

  339.   if (ret != 0)
  340.     return;

  341.   if ((additional_flags & PTRACE_O_TRACEVFORKDONE) != 0)
  342.     {
  343.       /* Check if the target supports PTRACE_O_TRACEVFORKDONE.  */
  344.       ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
  345.                     (PTRACE_TYPE_ARG4) (PTRACE_O_TRACEFORK
  346.                                         | PTRACE_O_TRACEVFORKDONE));
  347.       if (ret == 0)
  348.         current_ptrace_options |= PTRACE_O_TRACEVFORKDONE;
  349.     }

  350.   /* Setting PTRACE_O_TRACEFORK did not cause an error, however we
  351.      don't know for sure that the feature is available; old
  352.      versions of PTRACE_SETOPTIONS ignored unknown options.
  353.      Therefore, we attach to the child process, use PTRACE_SETOPTIONS
  354.      to enable fork tracing, and let it fork.  If the process exits,
  355.      we assume that we can't use PTRACE_O_TRACEFORK; if we get the
  356.      fork notification, and we can extract the new child's PID, then
  357.      we assume that we can.

  358.      We do not explicitly check for vfork tracing here.  It is
  359.      assumed that vfork tracing is available whenever fork tracing
  360.      is available.  */
  361.   ret = ptrace (PTRACE_CONT, child_pid, (PTRACE_TYPE_ARG3) 0,
  362.                 (PTRACE_TYPE_ARG4) 0);
  363.   if (ret != 0)
  364.     warning (_("linux_test_for_tracefork: failed to resume child"));

  365.   ret = my_waitpid (child_pid, &status, 0);

  366.   /* Check if we received a fork event notification.  */
  367.   if (ret == child_pid && WIFSTOPPED (status)
  368.       && linux_ptrace_get_extended_event (status) == PTRACE_EVENT_FORK)
  369.     {
  370.       /* We did receive a fork event notification.  Make sure its PID
  371.          is reported.  */
  372.       second_pid = 0;
  373.       ret = ptrace (PTRACE_GETEVENTMSG, child_pid, (PTRACE_TYPE_ARG3) 0,
  374.                     (PTRACE_TYPE_ARG4) &second_pid);
  375.       if (ret == 0 && second_pid != 0)
  376.         {
  377.           int second_status;

  378.           /* We got the PID from the grandchild, which means fork
  379.              tracing is supported.  */
  380.           current_ptrace_options |= PTRACE_O_TRACECLONE;
  381.           current_ptrace_options |= (additional_flags & (PTRACE_O_TRACEFORK
  382.                                                          | PTRACE_O_TRACEVFORK
  383.                                                          | PTRACE_O_TRACEEXEC));

  384.           /* Do some cleanup and kill the grandchild.  */
  385.           my_waitpid (second_pid, &second_status, 0);
  386.           ret = ptrace (PTRACE_KILL, second_pid, (PTRACE_TYPE_ARG3) 0,
  387.                         (PTRACE_TYPE_ARG4) 0);
  388.           if (ret != 0)
  389.             warning (_("linux_test_for_tracefork: "
  390.                        "failed to kill second child"));
  391.           my_waitpid (second_pid, &status, 0);
  392.         }
  393.     }
  394.   else
  395.     warning (_("linux_test_for_tracefork: unexpected result from waitpid "
  396.              "(%d, status 0x%x)"), ret, status);
  397. }

  398. /* Determine if PTRACE_O_EXITKILL can be used.  */

  399. static void
  400. linux_test_for_exitkill (int child_pid)
  401. {
  402.   int ret;

  403.   ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
  404.                 (PTRACE_TYPE_ARG4) PTRACE_O_EXITKILL);

  405.   if (ret == 0)
  406.     current_ptrace_options |= PTRACE_O_EXITKILL;
  407. }

  408. /* Enable reporting of all currently supported ptrace events.
  409.    ATTACHED should be nonzero if we have attached to the inferior.  */

  410. void
  411. linux_enable_event_reporting (pid_t pid, int attached)
  412. {
  413.   int ptrace_options;

  414.   /* Check if we have initialized the ptrace features for this
  415.      target.  If not, do it now.  */
  416.   if (current_ptrace_options == -1)
  417.     linux_check_ptrace_features ();

  418.   ptrace_options = current_ptrace_options;
  419.   if (attached)
  420.     {
  421.       /* When attached to our inferior, we do not want the inferior
  422.          to die with us if we terminate unexpectedly.  */
  423.       ptrace_options &= ~PTRACE_O_EXITKILL;
  424.     }

  425.   /* Set the options.  */
  426.   ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0,
  427.           (PTRACE_TYPE_ARG4) (uintptr_t) ptrace_options);
  428. }

  429. /* Disable reporting of all currently supported ptrace events.  */

  430. void
  431. linux_disable_event_reporting (pid_t pid)
  432. {
  433.   /* Set the options.  */
  434.   ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0, 0);
  435. }

  436. /* Returns non-zero if PTRACE_OPTIONS is contained within
  437.    CURRENT_PTRACE_OPTIONS, therefore supported.  Returns 0
  438.    otherwise.  */

  439. static int
  440. ptrace_supports_feature (int ptrace_options)
  441. {
  442.   if (current_ptrace_options == -1)
  443.     linux_check_ptrace_features ();

  444.   return ((current_ptrace_options & ptrace_options) == ptrace_options);
  445. }

  446. /* Returns non-zero if PTRACE_EVENT_FORK is supported by ptrace,
  447.    0 otherwise.  Note that if PTRACE_EVENT_FORK is supported so is
  448.    PTRACE_EVENT_CLONE, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
  449.    since they were all added to the kernel at the same time.  */

  450. int
  451. linux_supports_tracefork (void)
  452. {
  453.   return ptrace_supports_feature (PTRACE_O_TRACEFORK);
  454. }

  455. /* Returns non-zero if PTRACE_EVENT_CLONE is supported by ptrace,
  456.    0 otherwise.  Note that if PTRACE_EVENT_CLONE is supported so is
  457.    PTRACE_EVENT_FORK, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
  458.    since they were all added to the kernel at the same time.  */

  459. int
  460. linux_supports_traceclone (void)
  461. {
  462.   return ptrace_supports_feature (PTRACE_O_TRACECLONE);
  463. }

  464. /* Returns non-zero if PTRACE_O_TRACEVFORKDONE is supported by
  465.    ptrace, 0 otherwise.  */

  466. int
  467. linux_supports_tracevforkdone (void)
  468. {
  469.   return ptrace_supports_feature (PTRACE_O_TRACEVFORKDONE);
  470. }

  471. /* Returns non-zero if PTRACE_O_TRACESYSGOOD is supported by ptrace,
  472.    0 otherwise.  */

  473. int
  474. linux_supports_tracesysgood (void)
  475. {
  476.   return ptrace_supports_feature (PTRACE_O_TRACESYSGOOD);
  477. }

  478. /* Display possible problems on this system.  Display them only once per GDB
  479.    execution.  */

  480. void
  481. linux_ptrace_init_warnings (void)
  482. {
  483.   static int warned = 0;

  484.   if (warned)
  485.     return;
  486.   warned = 1;

  487.   linux_ptrace_test_ret_to_nx ();
  488. }

  489. /* Set additional ptrace flags to use.  Some such flags may be checked
  490.    by the implementation above.  This function must be called before
  491.    any other function in this file; otherwise the flags may not take
  492.    effect appropriately.  */

  493. void
  494. linux_ptrace_set_additional_flags (int flags)
  495. {
  496.   additional_flags = flags;
  497. }

  498. /* Extract extended ptrace event from wait status.  */

  499. int
  500. linux_ptrace_get_extended_event (int wstat)
  501. {
  502.   return (wstat >> 16);
  503. }

  504. /* Determine whether wait status denotes an extended event.  */

  505. int
  506. linux_is_extended_waitstatus (int wstat)
  507. {
  508.   return (linux_ptrace_get_extended_event (wstat) != 0);
  509. }