mem-break.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Simulate breakpoints by patching locations in the target system, for GDB.
  2. Copyright (C) 1990-2022 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. #include "gdbarch.h"
  21. /* Insert a breakpoint on targets that don't have any better
  22. breakpoint support. We read the contents of the target location
  23. and stash it, then overwrite it with a breakpoint instruction.
  24. BP_TGT->placed_address is the target location in the target
  25. machine. BP_TGT->shadow_contents is some memory allocated for
  26. saving the target contents. It is guaranteed by the caller to be
  27. long enough to save BREAKPOINT_LEN bytes (this is accomplished via
  28. BREAKPOINT_MAX). */
  29. int
  30. default_memory_insert_breakpoint (struct gdbarch *gdbarch,
  31. struct bp_target_info *bp_tgt)
  32. {
  33. CORE_ADDR addr = bp_tgt->placed_address;
  34. const unsigned char *bp;
  35. gdb_byte *readbuf;
  36. int bplen;
  37. int val;
  38. /* Determine appropriate breakpoint contents and size for this address. */
  39. bp = gdbarch_sw_breakpoint_from_kind (gdbarch, bp_tgt->kind, &bplen);
  40. /* Save the memory contents in the shadow_contents buffer and then
  41. write the breakpoint instruction. */
  42. readbuf = (gdb_byte *) alloca (bplen);
  43. val = target_read_memory (addr, readbuf, bplen);
  44. if (val == 0)
  45. {
  46. /* These must be set together, either before or after the shadow
  47. read, so that if we're "reinserting" a breakpoint that
  48. doesn't have a shadow yet, the breakpoint masking code inside
  49. target_read_memory doesn't mask out this breakpoint using an
  50. unfilled shadow buffer. The core may be trying to reinsert a
  51. permanent breakpoint, for targets that support breakpoint
  52. conditions/commands on the target side for some types of
  53. breakpoints, such as target remote. */
  54. bp_tgt->shadow_len = bplen;
  55. memcpy (bp_tgt->shadow_contents, readbuf, bplen);
  56. val = target_write_raw_memory (addr, bp, bplen);
  57. }
  58. return val;
  59. }
  60. int
  61. default_memory_remove_breakpoint (struct gdbarch *gdbarch,
  62. struct bp_target_info *bp_tgt)
  63. {
  64. int bplen;
  65. gdbarch_sw_breakpoint_from_kind (gdbarch, bp_tgt->kind, &bplen);
  66. return target_write_raw_memory (bp_tgt->placed_address, bp_tgt->shadow_contents,
  67. bplen);
  68. }
  69. int
  70. memory_insert_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
  71. struct bp_target_info *bp_tgt)
  72. {
  73. return gdbarch_memory_insert_breakpoint (gdbarch, bp_tgt);
  74. }
  75. int
  76. memory_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch,
  77. struct bp_target_info *bp_tgt,
  78. enum remove_bp_reason reason)
  79. {
  80. return gdbarch_memory_remove_breakpoint (gdbarch, bp_tgt);
  81. }
  82. int
  83. memory_validate_breakpoint (struct gdbarch *gdbarch,
  84. struct bp_target_info *bp_tgt)
  85. {
  86. CORE_ADDR addr = bp_tgt->placed_address;
  87. const gdb_byte *bp;
  88. int val;
  89. int bplen;
  90. gdb_byte cur_contents[BREAKPOINT_MAX];
  91. /* Determine appropriate breakpoint contents and size for this
  92. address. */
  93. bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
  94. if (bp == NULL)
  95. return 0;
  96. /* Make sure we see the memory breakpoints. */
  97. scoped_restore restore_memory
  98. = make_scoped_restore_show_memory_breakpoints (1);
  99. val = target_read_memory (addr, cur_contents, bplen);
  100. /* If our breakpoint is no longer at the address, this means that
  101. the program modified the code on us, so it is wrong to put back
  102. the old value. */
  103. return (val == 0 && memcmp (bp, cur_contents, bplen) == 0);
  104. }