DebugSession.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. if (!poke(reinterpret_cast<u32*>(address), (original_bytes.value() & ~(uint32_t)0xff) | BREAKPOINT_INSTRUCTION))
  116. return false;
  117. m_breakpoints.set(address, { address, original_bytes.value() });
  118. return true;
  119. }
  120. void DebugSession::remove_breakpoint(const BreakPoint& breakpoint)
  121. {
  122. ASSERT(m_breakpoints.contains(breakpoint.address));
  123. poke(reinterpret_cast<u32*>(reinterpret_cast<char*>(breakpoint.address)), breakpoint.original_first_word);
  124. m_breakpoints.remove(breakpoint.address);
  125. }
  126. PtraceRegisters DebugSession::get_registers() const
  127. {
  128. PtraceRegisters regs;
  129. if (ptrace(PT_GETREGS, m_debugee_pid, &regs, 0) < 0) {
  130. perror("PT_GETREGS");
  131. ASSERT_NOT_REACHED();
  132. }
  133. return regs;
  134. }
  135. void DebugSession::set_registers(const PtraceRegisters& regs)
  136. {
  137. if (ptrace(PT_SETREGS, m_debugee_pid, reinterpret_cast<void*>(&const_cast<PtraceRegisters&>(regs)), 0) < 0) {
  138. perror("PT_SETREGS");
  139. ASSERT_NOT_REACHED();
  140. }
  141. }
  142. Optional<DebugSession::BreakPoint> DebugSession::get_matching_breakpoint(const PtraceRegisters& regs) const
  143. {
  144. return m_breakpoints.get(reinterpret_cast<void*>(regs.eip - 1));
  145. }
  146. void DebugSession::continue_debugee()
  147. {
  148. if (ptrace(PT_CONTINUE, m_debugee_pid, 0, 0) < 0) {
  149. perror("continue");
  150. ASSERT_NOT_REACHED();
  151. }
  152. }