displaced-stepping.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #ifndef DISPLACED_STEPPING_H
  15. #define DISPLACED_STEPPING_H
  16. #include "gdbsupport/array-view.h"
  17. #include "gdbsupport/byte-vector.h"
  18. struct gdbarch;
  19. struct thread_info;
  20. /* True if we are debugging displaced stepping. */
  21. extern bool debug_displaced;
  22. /* Print a "displaced" debug statement. */
  23. #define displaced_debug_printf(fmt, ...) \
  24. debug_prefixed_printf_cond (debug_displaced, "displaced",fmt, ##__VA_ARGS__)
  25. enum displaced_step_prepare_status
  26. {
  27. /* A displaced stepping buffer was successfully allocated and prepared. */
  28. DISPLACED_STEP_PREPARE_STATUS_OK,
  29. /* This particular instruction can't be displaced stepped, GDB should fall
  30. back on in-line stepping. */
  31. DISPLACED_STEP_PREPARE_STATUS_CANT,
  32. /* Not enough resources are available at this time, try again later. */
  33. DISPLACED_STEP_PREPARE_STATUS_UNAVAILABLE,
  34. };
  35. enum displaced_step_finish_status
  36. {
  37. /* Either the instruction was stepped and fixed up, or the specified thread
  38. wasn't executing a displaced step (in which case there's nothing to
  39. finish). */
  40. DISPLACED_STEP_FINISH_STATUS_OK,
  41. /* The thread started a displaced step, but didn't complete it. */
  42. DISPLACED_STEP_FINISH_STATUS_NOT_EXECUTED,
  43. };
  44. /* Data returned by a gdbarch displaced_step_copy_insn method, to be passed to
  45. the matching displaced_step_fixup method. */
  46. struct displaced_step_copy_insn_closure
  47. {
  48. virtual ~displaced_step_copy_insn_closure () = 0;
  49. };
  50. using displaced_step_copy_insn_closure_up
  51. = std::unique_ptr<displaced_step_copy_insn_closure>;
  52. /* A simple displaced step closure that contains only a byte buffer. */
  53. struct buf_displaced_step_copy_insn_closure : displaced_step_copy_insn_closure
  54. {
  55. buf_displaced_step_copy_insn_closure (int buf_size)
  56. : buf (buf_size)
  57. {}
  58. /* The content of this buffer is up to the user of the class, but typically
  59. original instruction bytes, used during fixup to determine what needs to
  60. be fixed up. */
  61. gdb::byte_vector buf;
  62. };
  63. /* Per-inferior displaced stepping state. */
  64. struct displaced_step_inferior_state
  65. {
  66. displaced_step_inferior_state ()
  67. {
  68. reset ();
  69. }
  70. /* Put this object back in its original state. */
  71. void reset ()
  72. {
  73. failed_before = false;
  74. in_progress_count = 0;
  75. unavailable = false;
  76. }
  77. /* True if preparing a displaced step ever failed. If so, we won't
  78. try displaced stepping for this inferior again. */
  79. bool failed_before;
  80. /* Number of displaced steps in progress for this inferior. */
  81. unsigned int in_progress_count;
  82. /* If true, this tells GDB that it's not worth asking the gdbarch displaced
  83. stepping implementation to prepare a displaced step, because it would
  84. return UNAVAILABLE. This is set and reset by the gdbarch in the
  85. displaced_step_prepare and displaced_step_finish methods. */
  86. bool unavailable;
  87. };
  88. /* Per-thread displaced stepping state. */
  89. struct displaced_step_thread_state
  90. {
  91. /* Return true if this thread is currently executing a displaced step. */
  92. bool in_progress () const
  93. {
  94. return m_original_gdbarch != nullptr;
  95. }
  96. /* Return the gdbarch of the thread prior to the step. */
  97. gdbarch *get_original_gdbarch () const
  98. {
  99. return m_original_gdbarch;
  100. }
  101. /* Mark this thread as currently executing a displaced step.
  102. ORIGINAL_GDBARCH is the current gdbarch of the thread (before the step
  103. is executed). */
  104. void set (gdbarch *original_gdbarch)
  105. {
  106. m_original_gdbarch = original_gdbarch;
  107. }
  108. /* Mark this thread as no longer executing a displaced step. */
  109. void reset ()
  110. {
  111. m_original_gdbarch = nullptr;
  112. }
  113. private:
  114. gdbarch *m_original_gdbarch = nullptr;
  115. };
  116. /* Control access to multiple displaced stepping buffers at fixed addresses. */
  117. struct displaced_step_buffers
  118. {
  119. explicit displaced_step_buffers (gdb::array_view<CORE_ADDR> buffer_addrs)
  120. {
  121. gdb_assert (buffer_addrs.size () > 0);
  122. m_buffers.reserve (buffer_addrs.size ());
  123. for (CORE_ADDR buffer_addr : buffer_addrs)
  124. m_buffers.emplace_back (buffer_addr);
  125. }
  126. displaced_step_prepare_status prepare (thread_info *thread,
  127. CORE_ADDR &displaced_pc);
  128. displaced_step_finish_status finish (gdbarch *arch, thread_info *thread,
  129. gdb_signal sig);
  130. const displaced_step_copy_insn_closure *
  131. copy_insn_closure_by_addr (CORE_ADDR addr);
  132. void restore_in_ptid (ptid_t ptid);
  133. private:
  134. /* State of a single buffer. */
  135. struct displaced_step_buffer
  136. {
  137. explicit displaced_step_buffer (CORE_ADDR addr)
  138. : addr (addr)
  139. {}
  140. /* Address of the buffer. */
  141. const CORE_ADDR addr;
  142. /* The original PC of the instruction currently being stepped. */
  143. CORE_ADDR original_pc = 0;
  144. /* If set, the thread currently using the buffer. If unset, the buffer is not
  145. used. */
  146. thread_info *current_thread = nullptr;
  147. /* Saved copy of the bytes in the displaced buffer, to be restored once the
  148. buffer is no longer used. */
  149. gdb::byte_vector saved_copy;
  150. /* Closure obtained from gdbarch_displaced_step_copy_insn, to be passed to
  151. gdbarch_displaced_step_fixup_insn. */
  152. displaced_step_copy_insn_closure_up copy_insn_closure;
  153. };
  154. std::vector<displaced_step_buffer> m_buffers;
  155. };
  156. #endif /* DISPLACED_STEPPING_H */