gdb/x86-linux-nat.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Native-dependent code for GNU/Linux x86 (i386 and x86-64).

  2.    Copyright (C) 1999-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 "inferior.h"
  16. #include "elf/common.h"
  17. #include "gdb_proc_service.h"
  18. #include <sys/ptrace.h>
  19. #include <sys/user.h>
  20. #include <sys/procfs.h>
  21. #include <sys/uio.h>

  22. #include "x86-nat.h"
  23. #include "linux-nat.h"
  24. #ifndef __x86_64__
  25. #include "i386-linux-nat.h"
  26. #endif
  27. #include "x86-linux-nat.h"
  28. #include "i386-linux-tdep.h"
  29. #ifdef __x86_64__
  30. #include "amd64-linux-tdep.h"
  31. #endif
  32. #include "x86-xstate.h"
  33. #include "nat/linux-btrace.h"

  34. /* Per-thread arch-specific data we want to keep.  */

  35. struct arch_lwp_info
  36. {
  37.   /* Non-zero if our copy differs from what's recorded in the thread.  */
  38.   int debug_registers_changed;
  39. };

  40. /* Does the current host support PTRACE_GETREGSET?  */
  41. int have_ptrace_getregset = -1;


  42. /* Support for debug registers.  */

  43. /* Get debug register REGNUM value from only the one LWP of PTID.  */

  44. static unsigned long
  45. x86_linux_dr_get (ptid_t ptid, int regnum)
  46. {
  47.   int tid;
  48.   unsigned long value;

  49.   gdb_assert (ptid_lwp_p (ptid));
  50.   tid = ptid_get_lwp (ptid);

  51.   errno = 0;
  52.   value = ptrace (PTRACE_PEEKUSER, tid,
  53.                   offsetof (struct user, u_debugreg[regnum]), 0);
  54.   if (errno != 0)
  55.     perror_with_name (_("Couldn't read debug register"));

  56.   return value;
  57. }

  58. /* Set debug register REGNUM to VALUE in only the one LWP of PTID.  */

  59. static void
  60. x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
  61. {
  62.   int tid;

  63.   gdb_assert (ptid_lwp_p (ptid));
  64.   tid = ptid_get_lwp (ptid);

  65.   errno = 0;
  66.   ptrace (PTRACE_POKEUSER, tid,
  67.           offsetof (struct user, u_debugreg[regnum]), value);
  68.   if (errno != 0)
  69.     perror_with_name (_("Couldn't write debug register"));
  70. }

  71. /* Return the inferior's debug register REGNUM.  */

  72. static CORE_ADDR
  73. x86_linux_dr_get_addr (int regnum)
  74. {
  75.   /* DR6 and DR7 are retrieved with some other way.  */
  76.   gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);

  77.   return x86_linux_dr_get (inferior_ptid, regnum);
  78. }

  79. /* Return the inferior's DR7 debug control register.  */

  80. static unsigned long
  81. x86_linux_dr_get_control (void)
  82. {
  83.   return x86_linux_dr_get (inferior_ptid, DR_CONTROL);
  84. }

  85. /* Get DR_STATUS from only the one LWP of INFERIOR_PTID.  */

  86. static unsigned long
  87. x86_linux_dr_get_status (void)
  88. {
  89.   return x86_linux_dr_get (inferior_ptid, DR_STATUS);
  90. }

  91. /* Callback for iterate_over_lwps.  Update the debug registers of
  92.    LWP.  */

  93. static int
  94. update_debug_registers_callback (struct lwp_info *lwp, void *arg)
  95. {
  96.   if (lwp->arch_private == NULL)
  97.     lwp->arch_private = XCNEW (struct arch_lwp_info);

  98.   /* The actual update is done later just before resuming the lwp, we
  99.      just mark that the registers need updating.  */
  100.   lwp->arch_private->debug_registers_changed = 1;

  101.   /* If the lwp isn't stopped, force it to momentarily pause, so we
  102.      can update its debug registers.  */
  103.   if (!lwp->stopped)
  104.     linux_stop_lwp (lwp);

  105.   /* Continue the iteration.  */
  106.   return 0;
  107. }

  108. /* Set DR_CONTROL to CONTROL in all LWPs of the current inferior.  */

  109. static void
  110. x86_linux_dr_set_control (unsigned long control)
  111. {
  112.   ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));

  113.   iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
  114. }

  115. /* Set address REGNUM (zero based) to ADDR in all LWPs of the current
  116.    inferior.  */

  117. static void
  118. x86_linux_dr_set_addr (int regnum, CORE_ADDR addr)
  119. {
  120.   ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));

  121.   gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);

  122.   iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
  123. }

  124. /* Called when resuming a thread.
  125.    If the debug regs have changed, update the thread's copies.  */

  126. static void
  127. x86_linux_prepare_to_resume (struct lwp_info *lwp)
  128. {
  129.   int clear_status = 0;

  130.   /* NULL means this is the main thread still going through the shell,
  131.      or, no watchpoint has been set yet.  In that case, there's
  132.      nothing to do.  */
  133.   if (lwp->arch_private == NULL)
  134.     return;

  135.   if (lwp->arch_private->debug_registers_changed)
  136.     {
  137.       struct x86_debug_reg_state *state
  138.         = x86_debug_reg_state (ptid_get_pid (lwp->ptid));
  139.       int i;

  140.       /* On Linux kernel before 2.6.33 commit
  141.          72f674d203cd230426437cdcf7dd6f681dad8b0d
  142.          if you enable a breakpoint by the DR_CONTROL bits you need to have
  143.          already written the corresponding DR_FIRSTADDR...DR_LASTADDR registers.

  144.          Ensure DR_CONTROL gets written as the very last register here.  */

  145.       /* Clear DR_CONTROL first.  In some cases, setting DR0-3 to a
  146.          value that doesn't match what is enabled in DR_CONTROL
  147.          results in EINVAL.  */
  148.       x86_linux_dr_set (lwp->ptid, DR_CONTROL, 0);

  149.       ALL_DEBUG_ADDRESS_REGISTERS (i)
  150.         if (state->dr_ref_count[i] > 0)
  151.           {
  152.             x86_linux_dr_set (lwp->ptid, i, state->dr_mirror[i]);

  153.             /* If we're setting a watchpoint, any change the inferior
  154.                had done itself to the debug registers needs to be
  155.                discarded, otherwise, x86_stopped_data_address can get
  156.                confused.  */
  157.             clear_status = 1;
  158.           }

  159.       /* If DR_CONTROL is supposed to be zero, we've already set it
  160.          above.  */
  161.       if (state->dr_control_mirror != 0)
  162.         x86_linux_dr_set (lwp->ptid, DR_CONTROL, state->dr_control_mirror);

  163.       lwp->arch_private->debug_registers_changed = 0;
  164.     }

  165.   if (clear_status || lwp->stop_reason == LWP_STOPPED_BY_WATCHPOINT)
  166.     x86_linux_dr_set (lwp->ptid, DR_STATUS, 0);
  167. }

  168. static void
  169. x86_linux_new_thread (struct lwp_info *lp)
  170. {
  171.   struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);

  172.   info->debug_registers_changed = 1;

  173.   lp->arch_private = info;
  174. }


  175. /* linux_nat_new_fork hook.   */

  176. static void
  177. x86_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
  178. {
  179.   pid_t parent_pid;
  180.   struct x86_debug_reg_state *parent_state;
  181.   struct x86_debug_reg_state *child_state;

  182.   /* NULL means no watchpoint has ever been set in the parent.  In
  183.      that case, there's nothing to do.  */
  184.   if (parent->arch_private == NULL)
  185.     return;

  186.   /* Linux kernel before 2.6.33 commit
  187.      72f674d203cd230426437cdcf7dd6f681dad8b0d
  188.      will inherit hardware debug registers from parent
  189.      on fork/vfork/clone.  Newer Linux kernels create such tasks with
  190.      zeroed debug registers.

  191.      GDB core assumes the child inherits the watchpoints/hw
  192.      breakpoints of the parent, and will remove them all from the
  193.      forked off process.  Copy the debug registers mirrors into the
  194.      new process so that all breakpoints and watchpoints can be
  195.      removed together.  The debug registers mirror will become zeroed
  196.      in the end before detaching the forked off process, thus making
  197.      this compatible with older Linux kernels too.  */

  198.   parent_pid = ptid_get_pid (parent->ptid);
  199.   parent_state = x86_debug_reg_state (parent_pid);
  200.   child_state = x86_debug_reg_state (child_pid);
  201.   *child_state = *parent_state;
  202. }


  203. static void (*super_post_startup_inferior) (struct target_ops *self,
  204.                                             ptid_t ptid);

  205. static void
  206. x86_linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
  207. {
  208.   x86_cleanup_dregs ();
  209.   super_post_startup_inferior (self, ptid);
  210. }

  211. #ifdef __x86_64__
  212. /* Value of CS segment register:
  213.      64bit process: 0x33
  214.      32bit process: 0x23  */
  215. #define AMD64_LINUX_USER64_CS 0x33

  216. /* Value of DS segment register:
  217.      LP64 process: 0x0
  218.      X32 process: 0x2b  */
  219. #define AMD64_LINUX_X32_DS 0x2b
  220. #endif

  221. /* Get Linux/x86 target description from running target.  */

  222. static const struct target_desc *
  223. x86_linux_read_description (struct target_ops *ops)
  224. {
  225.   int tid;
  226.   int is_64bit = 0;
  227. #ifdef __x86_64__
  228.   int is_x32;
  229. #endif
  230.   static uint64_t xcr0;
  231.   uint64_t xcr0_features_bits;

  232.   /* GNU/Linux LWP ID's are process ID's.  */
  233.   tid = ptid_get_lwp (inferior_ptid);
  234.   if (tid == 0)
  235.     tid = ptid_get_pid (inferior_ptid); /* Not a threaded program.  */

  236. #ifdef __x86_64__
  237.   {
  238.     unsigned long cs;
  239.     unsigned long ds;

  240.     /* Get CS register.  */
  241.     errno = 0;
  242.     cs = ptrace (PTRACE_PEEKUSER, tid,
  243.                  offsetof (struct user_regs_struct, cs), 0);
  244.     if (errno != 0)
  245.       perror_with_name (_("Couldn't get CS register"));

  246.     is_64bit = cs == AMD64_LINUX_USER64_CS;

  247.     /* Get DS register.  */
  248.     errno = 0;
  249.     ds = ptrace (PTRACE_PEEKUSER, tid,
  250.                  offsetof (struct user_regs_struct, ds), 0);
  251.     if (errno != 0)
  252.       perror_with_name (_("Couldn't get DS register"));

  253.     is_x32 = ds == AMD64_LINUX_X32_DS;

  254.     if (sizeof (void *) == 4 && is_64bit && !is_x32)
  255.       error (_("Can't debug 64-bit process with 32-bit GDB"));
  256.   }
  257. #elif HAVE_PTRACE_GETFPXREGS
  258.   if (have_ptrace_getfpxregs == -1)
  259.     {
  260.       elf_fpxregset_t fpxregs;

  261.       if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
  262.         {
  263.           have_ptrace_getfpxregs = 0;
  264.           have_ptrace_getregset = 0;
  265.           return tdesc_i386_mmx_linux;
  266.         }
  267.     }
  268. #endif

  269.   if (have_ptrace_getregset == -1)
  270.     {
  271.       uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))];
  272.       struct iovec iov;

  273.       iov.iov_base = xstateregs;
  274.       iov.iov_len = sizeof (xstateregs);

  275.       /* Check if PTRACE_GETREGSET works.  */
  276.       if (ptrace (PTRACE_GETREGSET, tid,
  277.                   (unsigned int) NT_X86_XSTATE, &iov) < 0)
  278.         have_ptrace_getregset = 0;
  279.       else
  280.         {
  281.           have_ptrace_getregset = 1;

  282.           /* Get XCR0 from XSAVE extended state.  */
  283.           xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
  284.                              / sizeof (uint64_t))];
  285.         }
  286.     }

  287.   /* Check the native XCR0 only if PTRACE_GETREGSET is available.  If
  288.      PTRACE_GETREGSET is not available then set xcr0_features_bits to
  289.      zero so that the "no-features" descriptions are returned by the
  290.      switches below.  */
  291.   if (have_ptrace_getregset)
  292.     xcr0_features_bits = xcr0 & X86_XSTATE_ALL_MASK;
  293.   else
  294.     xcr0_features_bits = 0;

  295.   if (is_64bit)
  296.     {
  297. #ifdef __x86_64__
  298.       switch (xcr0_features_bits)
  299.         {
  300.         case X86_XSTATE_MPX_AVX512_MASK:
  301.         case X86_XSTATE_AVX512_MASK:
  302.           if (is_x32)
  303.             return tdesc_x32_avx512_linux;
  304.           else
  305.             return tdesc_amd64_avx512_linux;
  306.         case X86_XSTATE_MPX_MASK:
  307.           if (is_x32)
  308.             return tdesc_x32_avx_linux; /* No MPX on x32 using AVX.  */
  309.           else
  310.             return tdesc_amd64_mpx_linux;
  311.         case X86_XSTATE_AVX_MASK:
  312.           if (is_x32)
  313.             return tdesc_x32_avx_linux;
  314.           else
  315.             return tdesc_amd64_avx_linux;
  316.         default:
  317.           if (is_x32)
  318.             return tdesc_x32_linux;
  319.           else
  320.             return tdesc_amd64_linux;
  321.         }
  322. #endif
  323.     }
  324.   else
  325.     {
  326.       switch (xcr0_features_bits)
  327.         {
  328.         case X86_XSTATE_MPX_AVX512_MASK:
  329.         case X86_XSTATE_AVX512_MASK:
  330.           return tdesc_i386_avx512_linux;
  331.         case X86_XSTATE_MPX_MASK:
  332.           return tdesc_i386_mpx_linux;
  333.         case X86_XSTATE_AVX_MASK:
  334.           return tdesc_i386_avx_linux;
  335.         default:
  336.           return tdesc_i386_linux;
  337.         }
  338.     }

  339.   gdb_assert_not_reached ("failed to return tdesc");
  340. }


  341. /* Enable branch tracing.  */

  342. static struct btrace_target_info *
  343. x86_linux_enable_btrace (struct target_ops *self, ptid_t ptid)
  344. {
  345.   struct btrace_target_info *tinfo;
  346.   struct gdbarch *gdbarch;

  347.   errno = 0;
  348.   tinfo = linux_enable_btrace (ptid);

  349.   if (tinfo == NULL)
  350.     error (_("Could not enable branch tracing for %s: %s."),
  351.            target_pid_to_str (ptid), safe_strerror (errno));

  352.   /* Fill in the size of a pointer in bits.  */
  353.   gdbarch = target_thread_architecture (ptid);
  354.   tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);

  355.   return tinfo;
  356. }

  357. /* Disable branch tracing.  */

  358. static void
  359. x86_linux_disable_btrace (struct target_ops *self,
  360.                           struct btrace_target_info *tinfo)
  361. {
  362.   enum btrace_error errcode = linux_disable_btrace (tinfo);

  363.   if (errcode != BTRACE_ERR_NONE)
  364.     error (_("Could not disable branch tracing."));
  365. }

  366. /* Teardown branch tracing.  */

  367. static void
  368. x86_linux_teardown_btrace (struct target_ops *self,
  369.                            struct btrace_target_info *tinfo)
  370. {
  371.   /* Ignore errors.  */
  372.   linux_disable_btrace (tinfo);
  373. }

  374. static enum btrace_error
  375. x86_linux_read_btrace (struct target_ops *self,
  376.                        VEC (btrace_block_s) **data,
  377.                        struct btrace_target_info *btinfo,
  378.                        enum btrace_read_type type)
  379. {
  380.   return linux_read_btrace (data, btinfo, type);
  381. }


  382. /* Helper for ps_get_thread_area.  Sets BASE_ADDR to a pointer to
  383.    the thread local storage (or its descriptor) and returns PS_OK
  384.    on success.  Returns PS_ERR on failure.  */

  385. ps_err_e
  386. x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
  387. {
  388.   /* NOTE: cagney/2003-08-26: The definition of this buffer is found
  389.      in the kernel header <asm-i386/ldt.h>.  It, after padding, is 4 x
  390.      4 byte integers in size: `entry_number', `base_addr', `limit',
  391.      and a bunch of status bits.

  392.      The values returned by this ptrace call should be part of the
  393.      regcache buffer, and ps_get_thread_area should channel its
  394.      request through the regcache.  That way remote targets could
  395.      provide the value using the remote protocol and not this direct
  396.      call.

  397.      Is this function needed?  I'm guessing that the `base' is the
  398.      address of a descriptor that libthread_db uses to find the
  399.      thread local address base that GDB needs.  Perhaps that
  400.      descriptor is defined by the ABI.  Anyway, given that
  401.      libthread_db calls this function without prompting (gdb
  402.      requesting tls base) I guess it needs info in there anyway.  */
  403.   unsigned int desc[4];

  404.   /* This code assumes that "int" is 32 bits and that
  405.      GET_THREAD_AREA returns no more than 4 int values.  */
  406.   gdb_assert (sizeof (int) == 4);

  407. #ifndef PTRACE_GET_THREAD_AREA
  408. #define PTRACE_GET_THREAD_AREA 25
  409. #endif

  410.   if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
  411.     return PS_ERR;

  412.   *base_addr = desc[1];
  413.   return PS_OK;
  414. }


  415. /* Create an x86 GNU/Linux target.  */

  416. struct target_ops *
  417. x86_linux_create_target (void)
  418. {
  419.   /* Fill in the generic GNU/Linux methods.  */
  420.   struct target_ops *t = linux_target ();

  421.   /* Initialize the debug register function vectors.  */
  422.   x86_use_watchpoints (t);
  423.   x86_dr_low.set_control = x86_linux_dr_set_control;
  424.   x86_dr_low.set_addr = x86_linux_dr_set_addr;
  425.   x86_dr_low.get_addr = x86_linux_dr_get_addr;
  426.   x86_dr_low.get_status = x86_linux_dr_get_status;
  427.   x86_dr_low.get_control = x86_linux_dr_get_control;
  428.   x86_set_debug_register_length (sizeof (void *));

  429.   /* Override the GNU/Linux inferior startup hook.  */
  430.   super_post_startup_inferior = t->to_post_startup_inferior;
  431.   t->to_post_startup_inferior = x86_linux_child_post_startup_inferior;

  432.   /* Add the description reader.  */
  433.   t->to_read_description = x86_linux_read_description;

  434.   /* Add btrace methods.  */
  435.   t->to_supports_btrace = linux_supports_btrace;
  436.   t->to_enable_btrace = x86_linux_enable_btrace;
  437.   t->to_disable_btrace = x86_linux_disable_btrace;
  438.   t->to_teardown_btrace = x86_linux_teardown_btrace;
  439.   t->to_read_btrace = x86_linux_read_btrace;

  440.   return t;
  441. }

  442. /* Add an x86 GNU/Linux target.  */

  443. void
  444. x86_linux_add_target (struct target_ops *t)
  445. {
  446.   linux_nat_add_target (t);
  447.   linux_nat_set_new_thread (t, x86_linux_new_thread);
  448.   linux_nat_set_new_fork (t, x86_linux_new_fork);
  449.   linux_nat_set_forget_process (t, x86_forget_process);
  450.   linux_nat_set_prepare_to_resume (t, x86_linux_prepare_to_resume);
  451. }