gdb/mem-break.c - gdb

Functions defined

Source code

  1. /* Simulate breakpoints by patching locations in the target system, for GDB.

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

  3.    Contributed by Cygnus Support.  Written by John Gilmore.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "symtab.h"
  17. #include "breakpoint.h"
  18. #include "inferior.h"
  19. #include "target.h"
  20. /* Insert a breakpoint on targets that don't have any better
  21.    breakpoint support.  We read the contents of the target location
  22.    and stash it, then overwrite it with a breakpoint instruction.
  23.    BP_TGT->placed_address is the target location in the target
  24.    machine.  BP_TGT->shadow_contents is some memory allocated for
  25.    saving the target contents.  It is guaranteed by the caller to be
  26.    long enough to save BREAKPOINT_LEN bytes (this is accomplished via
  27.    BREAKPOINT_MAX).  */

  28. int
  29. default_memory_insert_breakpoint (struct gdbarch *gdbarch,
  30.                                   struct bp_target_info *bp_tgt)
  31. {
  32.   CORE_ADDR addr = bp_tgt->reqstd_address;
  33.   const unsigned char *bp;
  34.   gdb_byte *readbuf;
  35.   int bplen;
  36.   int val;

  37.   /* Determine appropriate breakpoint contents and size for this address.  */
  38.   bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
  39.   if (bp == NULL)
  40.     error (_("Software breakpoints not implemented for this target."));

  41.   bp_tgt->placed_address = addr;
  42.   bp_tgt->placed_size = bplen;

  43.   /* Save the memory contents in the shadow_contents buffer and then
  44.      write the breakpoint instruction.  */
  45.   bp_tgt->shadow_len = bplen;
  46.   readbuf = alloca (bplen);
  47.   val = target_read_memory (addr, readbuf, bplen);
  48.   if (val == 0)
  49.     {
  50.       memcpy (bp_tgt->shadow_contents, readbuf, bplen);
  51.       val = target_write_raw_memory (addr, bp, bplen);
  52.     }

  53.   return val;
  54. }


  55. int
  56. default_memory_remove_breakpoint (struct gdbarch *gdbarch,
  57.                                   struct bp_target_info *bp_tgt)
  58. {
  59.   return target_write_raw_memory (bp_tgt->placed_address, bp_tgt->shadow_contents,
  60.                                   bp_tgt->placed_size);
  61. }


  62. int
  63. memory_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
  64.                           struct bp_target_info *bp_tgt)
  65. {
  66.   return gdbarch_memory_insert_breakpoint (gdbarch, bp_tgt);
  67. }

  68. int
  69. memory_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
  70.                           struct bp_target_info *bp_tgt)
  71. {
  72.   return gdbarch_memory_remove_breakpoint (gdbarch, bp_tgt);
  73. }

  74. int
  75. memory_validate_breakpoint (struct gdbarch *gdbarch,
  76.                             struct bp_target_info *bp_tgt)
  77. {
  78.   CORE_ADDR addr = bp_tgt->placed_address;
  79.   const gdb_byte *bp;
  80.   int val;
  81.   int bplen;
  82.   gdb_byte cur_contents[BREAKPOINT_MAX];
  83.   struct cleanup *cleanup;
  84.   int ret;

  85.   /* Determine appropriate breakpoint contents and size for this
  86.      address.  */
  87.   bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);

  88.   if (bp == NULL || bp_tgt->placed_size != bplen)
  89.     return 0;

  90.   /* Make sure we see the memory breakpoints.  */
  91.   cleanup = make_show_memory_breakpoints_cleanup (1);
  92.   val = target_read_memory (addr, cur_contents, bplen);

  93.   /* If our breakpoint is no longer at the address, this means that
  94.      the program modified the code on us, so it is wrong to put back
  95.      the old value.  */
  96.   ret = (val == 0 && memcmp (bp, cur_contents, bplen) == 0);

  97.   do_cleanups (cleanup);
  98.   return ret;
  99. }