DebugSession.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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(reinterpret_cast<u8*>(m_executable.data()), m_executable.size())
  33. {
  34. }
  35. DebugSession::~DebugSession()
  36. {
  37. if (!m_is_debugee_dead) {
  38. if (ptrace(PT_DETACH, m_debugee_pid, 0, 0) < 0) {
  39. perror("PT_DETACH");
  40. }
  41. }
  42. }
  43. OwnPtr<DebugSession> DebugSession::exec_and_attach(const String& command)
  44. {
  45. int pid = fork();
  46. if (!pid) {
  47. if (ptrace(PT_TRACE_ME, 0, 0, 0) < 0) {
  48. perror("PT_TRACE_ME");
  49. exit(1);
  50. }
  51. auto parts = command.split(' ');
  52. ASSERT(!parts.is_empty());
  53. const char** args = (const char**)calloc(parts.size() + 1, sizeof(const char*));
  54. for (size_t i = 0; i < parts.size(); i++) {
  55. args[i] = parts[i].characters();
  56. }
  57. int rc = execvp(args[0], const_cast<char**>(args));
  58. if (rc < 0) {
  59. perror("execvp");
  60. }
  61. ASSERT_NOT_REACHED();
  62. }
  63. if (waitpid(pid, nullptr, WSTOPPED) != pid) {
  64. perror("waitpid");
  65. return nullptr;
  66. }
  67. if (ptrace(PT_ATTACH, pid, 0, 0) < 0) {
  68. perror("PT_ATTACH");
  69. return nullptr;
  70. }
  71. if (waitpid(pid, nullptr, WSTOPPED) != pid) {
  72. perror("waitpid");
  73. return nullptr;
  74. }
  75. if (ptrace(PT_CONTINUE, pid, 0, 0) < 0) {
  76. perror("continue");
  77. return nullptr;
  78. }
  79. // We want to continue until the exit from the 'execve' sycsall.
  80. // This ensures that when we start debugging the process
  81. // it executes the target image, and not the forked image of the tracing process.
  82. // 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!)
  83. if (waitpid(pid, nullptr, WSTOPPED) != pid) {
  84. perror("wait_pid");
  85. return nullptr;
  86. }
  87. return make<DebugSession>(pid);
  88. }
  89. bool DebugSession::poke(u32* address, u32 data)
  90. {
  91. if (ptrace(PT_POKE, m_debugee_pid, (void*)address, data) < 0) {
  92. perror("PT_POKE");
  93. return false;
  94. }
  95. return true;
  96. }
  97. Optional<u32> DebugSession::peek(u32* address) const
  98. {
  99. Optional<u32> result;
  100. int rc = ptrace(PT_PEEK, m_debugee_pid, (void*)address, 0);
  101. if (errno == 0)
  102. result = static_cast<u32>(rc);
  103. return result;
  104. }
  105. bool DebugSession::insert_breakpoint(void* address)
  106. {
  107. // We insert a software breakpoint by
  108. // patching the first byte of the instruction at 'address'
  109. // with the breakpoint instruction (int3)
  110. if (m_breakpoints.contains(address))
  111. return false;
  112. auto original_bytes = peek(reinterpret_cast<u32*>(address));
  113. if (!original_bytes.has_value())
  114. return false;
  115. BreakPoint breakpoint { address, original_bytes.value(), BreakPointState::Disabled };
  116. m_breakpoints.set(address, breakpoint);
  117. enable_breakpoint(breakpoint);
  118. return true;
  119. }
  120. bool DebugSession::disable_breakpoint(const BreakPoint& breakpoint)
  121. {
  122. ASSERT(m_breakpoints.contains(breakpoint.address));
  123. if (!poke(reinterpret_cast<u32*>(reinterpret_cast<char*>(breakpoint.address)), breakpoint.original_first_word))
  124. return false;
  125. auto bp = m_breakpoints.get(breakpoint.address).value();
  126. bp.state = BreakPointState::Disabled;
  127. m_breakpoints.set(bp.address, bp);
  128. return true;
  129. }
  130. bool DebugSession::enable_breakpoint(const BreakPoint& breakpoint)
  131. {
  132. ASSERT(m_breakpoints.contains(breakpoint.address));
  133. if (!poke(reinterpret_cast<u32*>(breakpoint.address), (breakpoint.original_first_word & ~(uint32_t)0xff) | BREAKPOINT_INSTRUCTION))
  134. return false;
  135. auto bp = m_breakpoints.get(breakpoint.address).value();
  136. bp.state = BreakPointState::Enabled;
  137. m_breakpoints.set(bp.address, bp);
  138. return true;
  139. }
  140. PtraceRegisters DebugSession::get_registers() const
  141. {
  142. PtraceRegisters regs;
  143. if (ptrace(PT_GETREGS, m_debugee_pid, &regs, 0) < 0) {
  144. perror("PT_GETREGS");
  145. ASSERT_NOT_REACHED();
  146. }
  147. return regs;
  148. }
  149. void DebugSession::set_registers(const PtraceRegisters& regs)
  150. {
  151. if (ptrace(PT_SETREGS, m_debugee_pid, reinterpret_cast<void*>(&const_cast<PtraceRegisters&>(regs)), 0) < 0) {
  152. perror("PT_SETREGS");
  153. ASSERT_NOT_REACHED();
  154. }
  155. }
  156. void DebugSession::continue_debugee()
  157. {
  158. if (ptrace(PT_CONTINUE, m_debugee_pid, 0, 0) < 0) {
  159. perror("continue");
  160. ASSERT_NOT_REACHED();
  161. }
  162. }
  163. void* DebugSession::single_step()
  164. {
  165. auto regs = get_registers();
  166. constexpr u32 TRAP_FLAG = 0x100;
  167. regs.eflags |= TRAP_FLAG;
  168. set_registers(regs);
  169. continue_debugee();
  170. if (waitpid(m_debugee_pid, 0, WSTOPPED) != m_debugee_pid) {
  171. perror("waitpid");
  172. ASSERT_NOT_REACHED();
  173. }
  174. regs = get_registers();
  175. regs.eflags &= ~(TRAP_FLAG);
  176. set_registers(regs);
  177. return (void*)regs.eip;
  178. }