DebugSession.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "DebugSession.h"
  27. #include <AK/Optional.h>
  28. #include <stdlib.h>
  29. DebugSession::DebugSession(int pid)
  30. : m_debugee_pid(pid)
  31. , m_executable(String::format("/proc/%d/exe", pid))
  32. , m_elf(ELF::Loader::create(reinterpret_cast<u8*>(m_executable.data()), m_executable.size()))
  33. , m_debug_info(m_elf)
  34. {
  35. }
  36. DebugSession::~DebugSession()
  37. {
  38. if (!m_is_debugee_dead) {
  39. if (ptrace(PT_DETACH, m_debugee_pid, 0, 0) < 0) {
  40. perror("PT_DETACH");
  41. }
  42. }
  43. }
  44. OwnPtr<DebugSession> DebugSession::exec_and_attach(const String& command)
  45. {
  46. int pid = fork();
  47. if (!pid) {
  48. if (ptrace(PT_TRACE_ME, 0, 0, 0) < 0) {
  49. perror("PT_TRACE_ME");
  50. exit(1);
  51. }
  52. auto parts = command.split(' ');
  53. ASSERT(!parts.is_empty());
  54. const char** args = (const char**)calloc(parts.size() + 1, sizeof(const char*));
  55. for (size_t i = 0; i < parts.size(); i++) {
  56. args[i] = parts[i].characters();
  57. }
  58. int rc = execvp(args[0], const_cast<char**>(args));
  59. if (rc < 0) {
  60. perror("execvp");
  61. }
  62. ASSERT_NOT_REACHED();
  63. }
  64. if (waitpid(pid, nullptr, WSTOPPED) != pid) {
  65. perror("waitpid");
  66. return nullptr;
  67. }
  68. if (ptrace(PT_ATTACH, pid, 0, 0) < 0) {
  69. perror("PT_ATTACH");
  70. return nullptr;
  71. }
  72. if (waitpid(pid, nullptr, WSTOPPED) != pid) {
  73. perror("waitpid");
  74. return nullptr;
  75. }
  76. if (ptrace(PT_CONTINUE, pid, 0, 0) < 0) {
  77. perror("continue");
  78. return nullptr;
  79. }
  80. // We want to continue until the exit from the 'execve' sycsall.
  81. // This ensures that when we start debugging the process
  82. // it executes the target image, and not the forked image of the tracing process.
  83. // NOTE: we only need to do this when we are debugging a new process (i.e not attaching to a process that's already running!)
  84. if (waitpid(pid, nullptr, WSTOPPED) != pid) {
  85. perror("wait_pid");
  86. return nullptr;
  87. }
  88. return make<DebugSession>(pid);
  89. }
  90. bool DebugSession::poke(u32* address, u32 data)
  91. {
  92. if (ptrace(PT_POKE, m_debugee_pid, (void*)address, data) < 0) {
  93. perror("PT_POKE");
  94. return false;
  95. }
  96. return true;
  97. }
  98. Optional<u32> DebugSession::peek(u32* address) const
  99. {
  100. Optional<u32> result;
  101. int rc = ptrace(PT_PEEK, m_debugee_pid, (void*)address, 0);
  102. if (errno == 0)
  103. result = static_cast<u32>(rc);
  104. return result;
  105. }
  106. bool DebugSession::insert_breakpoint(void* address)
  107. {
  108. // We insert a software breakpoint by
  109. // patching the first byte of the instruction at 'address'
  110. // with the breakpoint instruction (int3)
  111. if (m_breakpoints.contains(address))
  112. return false;
  113. auto original_bytes = peek(reinterpret_cast<u32*>(address));
  114. if (!original_bytes.has_value())
  115. return false;
  116. ASSERT((original_bytes.value() & 0xff) != BREAKPOINT_INSTRUCTION);
  117. BreakPoint breakpoint { address, original_bytes.value(), BreakPointState::Disabled };
  118. m_breakpoints.set(address, breakpoint);
  119. enable_breakpoint(breakpoint.address);
  120. return true;
  121. }
  122. bool DebugSession::disable_breakpoint(void* address)
  123. {
  124. auto breakpoint = m_breakpoints.get(address);
  125. ASSERT(breakpoint.has_value());
  126. if (!poke(reinterpret_cast<u32*>(reinterpret_cast<char*>(breakpoint.value().address)), breakpoint.value().original_first_word))
  127. return false;
  128. auto bp = m_breakpoints.get(breakpoint.value().address).value();
  129. bp.state = BreakPointState::Disabled;
  130. m_breakpoints.set(bp.address, bp);
  131. return true;
  132. }
  133. bool DebugSession::enable_breakpoint(void* address)
  134. {
  135. auto breakpoint = m_breakpoints.get(address);
  136. ASSERT(breakpoint.has_value());
  137. if (!poke(reinterpret_cast<u32*>(breakpoint.value().address), (breakpoint.value().original_first_word & ~(uint32_t)0xff) | BREAKPOINT_INSTRUCTION))
  138. return false;
  139. auto bp = m_breakpoints.get(breakpoint.value().address).value();
  140. bp.state = BreakPointState::Enabled;
  141. m_breakpoints.set(bp.address, bp);
  142. return true;
  143. }
  144. bool DebugSession::remove_breakpoint(void* address)
  145. {
  146. if (!disable_breakpoint(address))
  147. return false;
  148. m_breakpoints.remove(address);
  149. return true;
  150. }
  151. bool DebugSession::breakpoint_exists(void* address) const
  152. {
  153. return m_breakpoints.contains(address);
  154. }
  155. PtraceRegisters DebugSession::get_registers() const
  156. {
  157. PtraceRegisters regs;
  158. if (ptrace(PT_GETREGS, m_debugee_pid, &regs, 0) < 0) {
  159. perror("PT_GETREGS");
  160. ASSERT_NOT_REACHED();
  161. }
  162. return regs;
  163. }
  164. void DebugSession::set_registers(const PtraceRegisters& regs)
  165. {
  166. if (ptrace(PT_SETREGS, m_debugee_pid, reinterpret_cast<void*>(&const_cast<PtraceRegisters&>(regs)), 0) < 0) {
  167. perror("PT_SETREGS");
  168. ASSERT_NOT_REACHED();
  169. }
  170. }
  171. void DebugSession::continue_debugee(ContinueType type)
  172. {
  173. int command = (type == ContinueType::FreeRun) ? PT_CONTINUE : PT_SYSCALL;
  174. if (ptrace(command, m_debugee_pid, 0, 0) < 0) {
  175. perror("continue");
  176. ASSERT_NOT_REACHED();
  177. }
  178. }
  179. int DebugSession::continue_debugee_and_wait(ContinueType type)
  180. {
  181. continue_debugee(type);
  182. int wstatus = 0;
  183. if (waitpid(m_debugee_pid, &wstatus, WSTOPPED | WEXITED) != m_debugee_pid) {
  184. perror("waitpid");
  185. ASSERT_NOT_REACHED();
  186. }
  187. return wstatus;
  188. }
  189. void* DebugSession::single_step()
  190. {
  191. // Single stepping works by setting the x86 TRAP flag bit in the eflags register.
  192. // This flag causes the cpu to enter single-stepping mode, which causes
  193. // Interupt 1 (debug interrupt) to be emitted after every instruction.
  194. // To single step the program, we set the TRAP flag and continue the debugee.
  195. // After the debugee has stopped, we clear the TRAP flag.
  196. auto regs = get_registers();
  197. constexpr u32 TRAP_FLAG = 0x100;
  198. regs.eflags |= TRAP_FLAG;
  199. set_registers(regs);
  200. continue_debugee();
  201. if (waitpid(m_debugee_pid, 0, WSTOPPED) != m_debugee_pid) {
  202. perror("waitpid");
  203. ASSERT_NOT_REACHED();
  204. }
  205. regs = get_registers();
  206. regs.eflags &= ~(TRAP_FLAG);
  207. set_registers(regs);
  208. return (void*)regs.eip;
  209. }