displaced-stepping.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* Displaced stepping related things.
  2. Copyright (C) 2020-2022 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 "displaced-stepping.h"
  16. #include "cli/cli-cmds.h"
  17. #include "command.h"
  18. #include "gdbarch.h"
  19. #include "gdbcore.h"
  20. #include "gdbthread.h"
  21. #include "inferior.h"
  22. #include "regcache.h"
  23. #include "target/target.h"
  24. /* Default destructor for displaced_step_copy_insn_closure. */
  25. displaced_step_copy_insn_closure::~displaced_step_copy_insn_closure ()
  26. = default;
  27. bool debug_displaced = false;
  28. static void
  29. show_debug_displaced (struct ui_file *file, int from_tty,
  30. struct cmd_list_element *c, const char *value)
  31. {
  32. gdb_printf (file, _("Displace stepping debugging is %s.\n"), value);
  33. }
  34. displaced_step_prepare_status
  35. displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc)
  36. {
  37. gdb_assert (!thread->displaced_step_state.in_progress ());
  38. /* Sanity check: the thread should not be using a buffer at this point. */
  39. for (displaced_step_buffer &buf : m_buffers)
  40. gdb_assert (buf.current_thread != thread);
  41. regcache *regcache = get_thread_regcache (thread);
  42. const address_space *aspace = regcache->aspace ();
  43. gdbarch *arch = regcache->arch ();
  44. ULONGEST len = gdbarch_max_insn_length (arch);
  45. /* Search for an unused buffer. */
  46. displaced_step_buffer *buffer = nullptr;
  47. displaced_step_prepare_status fail_status
  48. = DISPLACED_STEP_PREPARE_STATUS_CANT;
  49. for (displaced_step_buffer &candidate : m_buffers)
  50. {
  51. bool bp_in_range = breakpoint_in_range_p (aspace, candidate.addr, len);
  52. bool is_free = candidate.current_thread == nullptr;
  53. if (!bp_in_range)
  54. {
  55. if (is_free)
  56. {
  57. buffer = &candidate;
  58. break;
  59. }
  60. else
  61. {
  62. /* This buffer would be suitable, but it's used right now. */
  63. fail_status = DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE;
  64. }
  65. }
  66. else
  67. {
  68. /* There's a breakpoint set in the scratch pad location range
  69. (which is usually around the entry point). We'd either
  70. install it before resuming, which would overwrite/corrupt the
  71. scratch pad, or if it was already inserted, this displaced
  72. step would overwrite it. The latter is OK in the sense that
  73. we already assume that no thread is going to execute the code
  74. in the scratch pad range (after initial startup) anyway, but
  75. the former is unacceptable. Simply punt and fallback to
  76. stepping over this breakpoint in-line. */
  77. displaced_debug_printf ("breakpoint set in displaced stepping "
  78. "buffer at %s, can't use.",
  79. paddress (arch, candidate.addr));
  80. }
  81. }
  82. if (buffer == nullptr)
  83. return fail_status;
  84. displaced_debug_printf ("selected buffer at %s",
  85. paddress (arch, buffer->addr));
  86. /* Save the original PC of the thread. */
  87. buffer->original_pc = regcache_read_pc (regcache);
  88. /* Return displaced step buffer address to caller. */
  89. displaced_pc = buffer->addr;
  90. /* Save the original contents of the displaced stepping buffer. */
  91. buffer->saved_copy.resize (len);
  92. int status = target_read_memory (buffer->addr,
  93. buffer->saved_copy.data (), len);
  94. if (status != 0)
  95. throw_error (MEMORY_ERROR,
  96. _("Error accessing memory address %s (%s) for "
  97. "displaced-stepping scratch space."),
  98. paddress (arch, buffer->addr), safe_strerror (status));
  99. displaced_debug_printf ("saved %s: %s",
  100. paddress (arch, buffer->addr),
  101. displaced_step_dump_bytes
  102. (buffer->saved_copy.data (), len).c_str ());
  103. /* Save this in a local variable first, so it's released if code below
  104. throws. */
  105. displaced_step_copy_insn_closure_up copy_insn_closure
  106. = gdbarch_displaced_step_copy_insn (arch, buffer->original_pc,
  107. buffer->addr, regcache);
  108. if (copy_insn_closure == nullptr)
  109. {
  110. /* The architecture doesn't know how or want to displaced step
  111. this instruction or instruction sequence. Fallback to
  112. stepping over the breakpoint in-line. */
  113. return DISPLACED_STEP_PREPARE_STATUS_CANT;
  114. }
  115. /* Resume execution at the copy. */
  116. regcache_write_pc (regcache, buffer->addr);
  117. /* This marks the buffer as being in use. */
  118. buffer->current_thread = thread;
  119. /* Save this, now that we know everything went fine. */
  120. buffer->copy_insn_closure = std::move (copy_insn_closure);
  121. /* Tell infrun not to try preparing a displaced step again for this inferior if
  122. all buffers are taken. */
  123. thread->inf->displaced_step_state.unavailable = true;
  124. for (const displaced_step_buffer &buf : m_buffers)
  125. {
  126. if (buf.current_thread == nullptr)
  127. {
  128. thread->inf->displaced_step_state.unavailable = false;
  129. break;
  130. }
  131. }
  132. return DISPLACED_STEP_PREPARE_STATUS_OK;
  133. }
  134. static void
  135. write_memory_ptid (ptid_t ptid, CORE_ADDR memaddr,
  136. const gdb_byte *myaddr, int len)
  137. {
  138. scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
  139. inferior_ptid = ptid;
  140. write_memory (memaddr, myaddr, len);
  141. }
  142. static bool
  143. displaced_step_instruction_executed_successfully (gdbarch *arch,
  144. gdb_signal signal)
  145. {
  146. if (signal != GDB_SIGNAL_TRAP)
  147. return false;
  148. if (target_stopped_by_watchpoint ())
  149. {
  150. if (gdbarch_have_nonsteppable_watchpoint (arch)
  151. || target_have_steppable_watchpoint ())
  152. return false;
  153. }
  154. return true;
  155. }
  156. displaced_step_finish_status
  157. displaced_step_buffers::finish (gdbarch *arch, thread_info *thread,
  158. gdb_signal sig)
  159. {
  160. gdb_assert (thread->displaced_step_state.in_progress ());
  161. /* Find the buffer this thread was using. */
  162. displaced_step_buffer *buffer = nullptr;
  163. for (displaced_step_buffer &candidate : m_buffers)
  164. {
  165. if (candidate.current_thread == thread)
  166. {
  167. buffer = &candidate;
  168. break;
  169. }
  170. }
  171. gdb_assert (buffer != nullptr);
  172. /* Move this to a local variable so it's released in case something goes
  173. wrong. */
  174. displaced_step_copy_insn_closure_up copy_insn_closure
  175. = std::move (buffer->copy_insn_closure);
  176. gdb_assert (copy_insn_closure != nullptr);
  177. /* Reset BUFFER->CURRENT_THREAD immediately to mark the buffer as available,
  178. in case something goes wrong below. */
  179. buffer->current_thread = nullptr;
  180. /* Now that a buffer gets freed, tell infrun it can ask us to prepare a displaced
  181. step again for this inferior. Do that here in case something goes wrong
  182. below. */
  183. thread->inf->displaced_step_state.unavailable = false;
  184. ULONGEST len = gdbarch_max_insn_length (arch);
  185. /* Restore memory of the buffer. */
  186. write_memory_ptid (thread->ptid, buffer->addr,
  187. buffer->saved_copy.data (), len);
  188. displaced_debug_printf ("restored %s %s",
  189. thread->ptid.to_string ().c_str (),
  190. paddress (arch, buffer->addr));
  191. regcache *rc = get_thread_regcache (thread);
  192. bool instruction_executed_successfully
  193. = displaced_step_instruction_executed_successfully (arch, sig);
  194. if (instruction_executed_successfully)
  195. {
  196. gdbarch_displaced_step_fixup (arch, copy_insn_closure.get (),
  197. buffer->original_pc,
  198. buffer->addr, rc);
  199. return DISPLACED_STEP_FINISH_STATUS_OK;
  200. }
  201. else
  202. {
  203. /* Since the instruction didn't complete, all we can do is relocate the
  204. PC. */
  205. CORE_ADDR pc = regcache_read_pc (rc);
  206. pc = buffer->original_pc + (pc - buffer->addr);
  207. regcache_write_pc (rc, pc);
  208. return DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED;
  209. }
  210. }
  211. const displaced_step_copy_insn_closure *
  212. displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr)
  213. {
  214. for (const displaced_step_buffer &buffer : m_buffers)
  215. {
  216. if (addr == buffer.addr)
  217. return buffer.copy_insn_closure.get ();
  218. }
  219. return nullptr;
  220. }
  221. void
  222. displaced_step_buffers::restore_in_ptid (ptid_t ptid)
  223. {
  224. for (const displaced_step_buffer &buffer : m_buffers)
  225. {
  226. if (buffer.current_thread == nullptr)
  227. continue;
  228. regcache *regcache = get_thread_regcache (buffer.current_thread);
  229. gdbarch *arch = regcache->arch ();
  230. ULONGEST len = gdbarch_max_insn_length (arch);
  231. write_memory_ptid (ptid, buffer.addr, buffer.saved_copy.data (), len);
  232. displaced_debug_printf ("restored in ptid %s %s",
  233. ptid.to_string ().c_str (),
  234. paddress (arch, buffer.addr));
  235. }
  236. }
  237. void _initialize_displaced_stepping ();
  238. void
  239. _initialize_displaced_stepping ()
  240. {
  241. add_setshow_boolean_cmd ("displaced", class_maintenance,
  242. &debug_displaced, _("\
  243. Set displaced stepping debugging."), _("\
  244. Show displaced stepping debugging."), _("\
  245. When non-zero, displaced stepping specific debugging is enabled."),
  246. NULL,
  247. show_debug_displaced,
  248. &setdebuglist, &showdebuglist);
  249. }