gdb/x86-nat.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

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

  2.    Copyright (C) 2001-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 "x86-nat.h"
  16. #include "gdbcmd.h"
  17. #include "inferior.h"

  18. /* Support for hardware watchpoints and breakpoints using the x86
  19.    debug registers.

  20.    This provides several functions for inserting and removing
  21.    hardware-assisted breakpoints and watchpoints, testing if one or
  22.    more of the watchpoints triggered and at what address, checking
  23.    whether a given region can be watched, etc.

  24.    The functions below implement debug registers sharing by reference
  25.    counts, and allow to watch regions up to 16 bytes long.  */

  26. /* Low-level function vector.  */
  27. struct x86_dr_low_type x86_dr_low;

  28. /* Per-process data.  We don't bind this to a per-inferior registry
  29.    because of targets like x86 GNU/Linux that need to keep track of
  30.    processes that aren't bound to any inferior (e.g., fork children,
  31.    checkpoints).  */

  32. struct x86_process_info
  33. {
  34.   /* Linked list.  */
  35.   struct x86_process_info *next;

  36.   /* The process identifier.  */
  37.   pid_t pid;

  38.   /* Copy of x86 hardware debug registers.  */
  39.   struct x86_debug_reg_state state;
  40. };

  41. static struct x86_process_info *x86_process_list = NULL;

  42. /* Find process data for process PID.  */

  43. static struct x86_process_info *
  44. x86_find_process_pid (pid_t pid)
  45. {
  46.   struct x86_process_info *proc;

  47.   for (proc = x86_process_list; proc; proc = proc->next)
  48.     if (proc->pid == pid)
  49.       return proc;

  50.   return NULL;
  51. }

  52. /* Add process data for process PID.  Returns newly allocated info
  53.    object.  */

  54. static struct x86_process_info *
  55. x86_add_process (pid_t pid)
  56. {
  57.   struct x86_process_info *proc;

  58.   proc = xcalloc (1, sizeof (*proc));
  59.   proc->pid = pid;

  60.   proc->next = x86_process_list;
  61.   x86_process_list = proc;

  62.   return proc;
  63. }

  64. /* Get data specific info for process PID, creating it if necessary.
  65.    Never returns NULL.  */

  66. static struct x86_process_info *
  67. x86_process_info_get (pid_t pid)
  68. {
  69.   struct x86_process_info *proc;

  70.   proc = x86_find_process_pid (pid);
  71.   if (proc == NULL)
  72.     proc = x86_add_process (pid);

  73.   return proc;
  74. }

  75. /* Get debug registers state for process PID.  */

  76. struct x86_debug_reg_state *
  77. x86_debug_reg_state (pid_t pid)
  78. {
  79.   return &x86_process_info_get (pid)->state;
  80. }

  81. /* See declaration in i386-nat.h.  */

  82. void
  83. x86_forget_process (pid_t pid)
  84. {
  85.   struct x86_process_info *proc, **proc_link;

  86.   proc = x86_process_list;
  87.   proc_link = &x86_process_list;

  88.   while (proc != NULL)
  89.     {
  90.       if (proc->pid == pid)
  91.         {
  92.           *proc_link = proc->next;

  93.           xfree (proc);
  94.           return;
  95.         }

  96.       proc_link = &proc->next;
  97.       proc = *proc_link;
  98.     }
  99. }

  100. /* Clear the reference counts and forget everything we knew about the
  101.    debug registers.  */

  102. void
  103. x86_cleanup_dregs (void)
  104. {
  105.   /* Starting from scratch has the same effect.  */
  106.   x86_forget_process (ptid_get_pid (inferior_ptid));
  107. }

  108. /* Insert a watchpoint to watch a memory region which starts at
  109.    address ADDR and whose length is LEN bytes.  Watch memory accesses
  110.    of the type TYPE.  Return 0 on success, -1 on failure.  */

  111. static int
  112. x86_insert_watchpoint (struct target_ops *self,
  113.                        CORE_ADDR addr, int len, int type,
  114.                        struct expression *cond)
  115. {
  116.   struct x86_debug_reg_state *state
  117.     = x86_debug_reg_state (ptid_get_pid (inferior_ptid));

  118.   return x86_dr_insert_watchpoint (state, type, addr, len);
  119. }

  120. /* Remove a watchpoint that watched the memory region which starts at
  121.    address ADDR, whose length is LEN bytes, and for accesses of the
  122.    type TYPE.  Return 0 on success, -1 on failure.  */
  123. static int
  124. x86_remove_watchpoint (struct target_ops *self,
  125.                        CORE_ADDR addr, int len, int type,
  126.                        struct expression *cond)
  127. {
  128.   struct x86_debug_reg_state *state
  129.     = x86_debug_reg_state (ptid_get_pid (inferior_ptid));

  130.   return x86_dr_remove_watchpoint (state, type, addr, len);
  131. }

  132. /* Return non-zero if we can watch a memory region that starts at
  133.    address ADDR and whose length is LEN bytes.  */

  134. static int
  135. x86_region_ok_for_watchpoint (struct target_ops *self,
  136.                               CORE_ADDR addr, int len)
  137. {
  138.   struct x86_debug_reg_state *state
  139.     = x86_debug_reg_state (ptid_get_pid (inferior_ptid));

  140.   return x86_dr_region_ok_for_watchpoint (state, addr, len);
  141. }

  142. /* If the inferior has some break/watchpoint that triggered, set the
  143.    address associated with that break/watchpoint and return non-zero.
  144.    Otherwise, return zero.  */

  145. static int
  146. x86_stopped_data_address (struct target_ops *ops, CORE_ADDR *addr_p)
  147. {
  148.   struct x86_debug_reg_state *state
  149.     = x86_debug_reg_state (ptid_get_pid (inferior_ptid));

  150.   return x86_dr_stopped_data_address (state, addr_p);
  151. }

  152. /* Return non-zero if the inferior has some watchpoint that triggered.
  153.    Otherwise return zero.  */

  154. static int
  155. x86_stopped_by_watchpoint (struct target_ops *ops)
  156. {
  157.   struct x86_debug_reg_state *state
  158.     = x86_debug_reg_state (ptid_get_pid (inferior_ptid));

  159.   return x86_dr_stopped_by_watchpoint (state);
  160. }

  161. /* Insert a hardware-assisted breakpoint at BP_TGT->reqstd_address.
  162.    Return 0 on success, EBUSY on failure.  */

  163. static int
  164. x86_insert_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
  165.                           struct bp_target_info *bp_tgt)
  166. {
  167.   struct x86_debug_reg_state *state
  168.     = x86_debug_reg_state (ptid_get_pid (inferior_ptid));

  169.   bp_tgt->placed_address = bp_tgt->reqstd_address;
  170.   return x86_dr_insert_watchpoint (state, hw_execute,
  171.                                    bp_tgt->placed_address, 1) ? EBUSY : 0;
  172. }

  173. /* Remove a hardware-assisted breakpoint at BP_TGT->placed_address.
  174.    Return 0 on success, -1 on failure.  */

  175. static int
  176. x86_remove_hw_breakpoint (struct target_ops *self, struct gdbarch *gdbarch,
  177.                           struct bp_target_info *bp_tgt)
  178. {
  179.   struct x86_debug_reg_state *state
  180.     = x86_debug_reg_state (ptid_get_pid (inferior_ptid));

  181.   return x86_dr_remove_watchpoint (state, hw_execute,
  182.                                    bp_tgt->placed_address, 1);
  183. }

  184. /* Returns the number of hardware watchpoints of type TYPE that we can
  185.    set.  Value is positive if we can set CNT watchpoints, zero if
  186.    setting watchpoints of type TYPE is not supported, and negative if
  187.    CNT is more than the maximum number of watchpoints of type TYPE
  188.    that we can support.  TYPE is one of bp_hardware_watchpoint,
  189.    bp_read_watchpoint, bp_write_watchpoint, or bp_hardware_breakpoint.
  190.    CNT is the number of such watchpoints used so far (including this
  191.    one).  OTHERTYPE is non-zero if other types of watchpoints are
  192.    currently enabled.

  193.    We always return 1 here because we don't have enough information
  194.    about possible overlap of addresses that they want to watch.  As an
  195.    extreme example, consider the case where all the watchpoints watch
  196.    the same address and the same region length: then we can handle a
  197.    virtually unlimited number of watchpoints, due to debug register
  198.    sharing implemented via reference counts in i386-nat.c.  */

  199. static int
  200. x86_can_use_hw_breakpoint (struct target_ops *self,
  201.                            int type, int cnt, int othertype)
  202. {
  203.   return 1;
  204. }

  205. static void
  206. add_show_debug_regs_command (void)
  207. {
  208.   /* A maintenance command to enable printing the internal DRi mirror
  209.      variables.  */
  210.   add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
  211.                            &show_debug_regs, _("\
  212. Set whether to show variables that mirror the x86 debug registers."), _("\
  213. Show whether to show variables that mirror the x86 debug registers."), _("\
  214. Use \"on\" to enable, \"off\" to disable.\n\
  215. If enabled, the debug registers values are shown when GDB inserts\n\
  216. or removes a hardware breakpoint or watchpoint, and when the inferior\n\
  217. triggers a breakpoint or watchpoint."),
  218.                            NULL,
  219.                            NULL,
  220.                            &maintenance_set_cmdlist,
  221.                            &maintenance_show_cmdlist);
  222. }

  223. /* There are only two global functions left.  */

  224. void
  225. x86_use_watchpoints (struct target_ops *t)
  226. {
  227.   /* After a watchpoint trap, the PC points to the instruction after the
  228.      one that caused the trap.  Therefore we don't need to step over it.
  229.      But we do need to reset the status register to avoid another trap.  */
  230.   t->to_have_continuable_watchpoint = 1;

  231.   t->to_can_use_hw_breakpoint = x86_can_use_hw_breakpoint;
  232.   t->to_region_ok_for_hw_watchpoint = x86_region_ok_for_watchpoint;
  233.   t->to_stopped_by_watchpoint = x86_stopped_by_watchpoint;
  234.   t->to_stopped_data_address = x86_stopped_data_address;
  235.   t->to_insert_watchpoint = x86_insert_watchpoint;
  236.   t->to_remove_watchpoint = x86_remove_watchpoint;
  237.   t->to_insert_hw_breakpoint = x86_insert_hw_breakpoint;
  238.   t->to_remove_hw_breakpoint = x86_remove_hw_breakpoint;
  239. }

  240. void
  241. x86_set_debug_register_length (int len)
  242. {
  243.   /* This function should be called only once for each native target.  */
  244.   gdb_assert (x86_dr_low.debug_register_length == 0);
  245.   gdb_assert (len == 4 || len == 8);
  246.   x86_dr_low.debug_register_length = len;
  247.   add_show_debug_regs_command ();
  248. }