main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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 <AK/Assertions.h>
  27. #include <AK/ByteBuffer.h>
  28. #include <LibC/sys/arch/i386/regs.h>
  29. #include <LibCore/File.h>
  30. #include <LibELF/ELFImage.h>
  31. #include <signal.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sys/ptrace.h>
  36. #include <sys/wait.h>
  37. #include <unistd.h>
  38. static int usage()
  39. {
  40. printf("usage: sdb [command...]\n");
  41. return 1;
  42. }
  43. static int g_pid = -1;
  44. static void handle_sigint(int)
  45. {
  46. if (g_pid == -1)
  47. return;
  48. if (ptrace(PT_DETACH, g_pid, 0, 0) == -1) {
  49. perror("detach");
  50. }
  51. }
  52. void run_child_and_attach(char** argv)
  53. {
  54. int pid = fork();
  55. if (!pid) {
  56. if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1) {
  57. perror("traceme");
  58. return exit(1);
  59. }
  60. int rc = execvp(argv[1], &argv[1]);
  61. if (rc < 0) {
  62. perror("execvp");
  63. exit(1);
  64. }
  65. ASSERT_NOT_REACHED();
  66. }
  67. g_pid = pid;
  68. if (waitpid(pid, nullptr, WSTOPPED) != pid) {
  69. perror("waitpid");
  70. exit(1);
  71. }
  72. if (ptrace(PT_ATTACH, g_pid, 0, 0) == -1) {
  73. perror("attach");
  74. exit(1);
  75. }
  76. if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
  77. perror("waitpid");
  78. exit(1);
  79. }
  80. // we want to continue until the exit from the 'execve' sycsall
  81. // we do this to ensure that when we start debugging the process,
  82. // it executes the target image, and not the forked image of the debugger
  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 (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
  85. perror("syscall");
  86. exit(1);
  87. }
  88. if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
  89. perror("wait_pid");
  90. exit(1);
  91. }
  92. }
  93. VirtualAddress get_entry_point(int pid)
  94. {
  95. auto path = String::format("/proc/%d/exe", pid);
  96. dbg() << "path: " << path;
  97. auto file = Core::File::construct(path);
  98. if (!file->open(Core::File::ReadOnly)) {
  99. fprintf(stderr, "Failed to open Debugged executable");
  100. exit(1);
  101. }
  102. auto data = file->read_all();
  103. dbg() << "data size:" << data.size();
  104. ELFImage elf(data.data(), data.size());
  105. return elf.entry();
  106. }
  107. int main(int argc, char** argv)
  108. {
  109. // TODO: pledge & unveil
  110. // TOOD: check that we didn't somehow hurt performance. boot seems slower? (or it's just laptop battey)
  111. if (argc == 1)
  112. return usage();
  113. struct sigaction sa;
  114. memset(&sa, 0, sizeof(struct sigaction));
  115. sa.sa_handler = handle_sigint;
  116. sigaction(SIGINT, &sa, nullptr);
  117. run_child_and_attach(argv);
  118. dbg() << "pid:" << g_pid;
  119. auto entry_point = get_entry_point(g_pid);
  120. dbg() << "entry point:" << entry_point;
  121. if (ptrace(PT_CONTINUE, g_pid, 0, 0) == -1) {
  122. perror("continue");
  123. }
  124. // wait for breakpoint
  125. if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
  126. perror("waitpid");
  127. return 1;
  128. }
  129. printf("hit breakpoint\n");
  130. PtraceRegisters regs;
  131. if (ptrace(PT_GETREGS, g_pid, &regs, 0) == -1) {
  132. perror("getregs");
  133. return 1;
  134. }
  135. printf("eip:0x%x\n", regs.eip);
  136. uint32_t data = ptrace(PT_PEEK, g_pid, (void*)regs.eip, 0);
  137. printf("peeked data: 0x%x\n", data);
  138. if (ptrace(PT_POKE, g_pid, (void*)regs.eip, data) < 0) {
  139. perror("poke");
  140. return 1;
  141. }
  142. if (ptrace(PT_CONTINUE, g_pid, 0, 0) == -1) {
  143. perror("continue");
  144. }
  145. if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
  146. perror("waitpid");
  147. return 1;
  148. }
  149. }