strace.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Types.h>
  8. #include <LibC/sys/arch/i386/regs.h>
  9. #include <LibCore/ArgsParser.h>
  10. #include <LibCore/File.h>
  11. #include <signal.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/ptrace.h>
  16. #include <sys/wait.h>
  17. #include <syscall.h>
  18. #include <unistd.h>
  19. static int g_pid = -1;
  20. static void handle_sigint(int)
  21. {
  22. if (g_pid == -1)
  23. return;
  24. if (ptrace(PT_DETACH, g_pid, 0, 0) == -1) {
  25. perror("detach");
  26. }
  27. }
  28. int main(int argc, char** argv)
  29. {
  30. if (pledge("stdio wpath cpath proc exec ptrace sigaction", nullptr) < 0) {
  31. perror("pledge");
  32. return 1;
  33. }
  34. Vector<const char*> child_argv;
  35. const char* output_filename = nullptr;
  36. auto trace_file = Core::File::standard_error();
  37. Core::ArgsParser parser;
  38. parser.set_general_help(
  39. "Trace all syscalls and their result.");
  40. parser.add_option(g_pid, "Trace the given PID", "pid", 'p', "pid");
  41. parser.add_option(output_filename, "Filename to write output to", "output", 'o', "output");
  42. parser.add_positional_argument(child_argv, "Arguments to exec", "argument", Core::ArgsParser::Required::No);
  43. parser.parse(argc, argv);
  44. if (output_filename != nullptr) {
  45. auto open_result = Core::File::open(output_filename, Core::IODevice::OpenMode::WriteOnly);
  46. if (open_result.is_error()) {
  47. outln(stderr, "Failed to open output file: {}", open_result.error());
  48. return 1;
  49. }
  50. trace_file = open_result.value();
  51. }
  52. if (pledge("stdio proc exec ptrace sigaction", nullptr) < 0) {
  53. perror("pledge");
  54. return 1;
  55. }
  56. int status;
  57. if (g_pid == -1) {
  58. if (child_argv.is_empty()) {
  59. outln(stderr, "strace: Expected either a pid or some arguments\n");
  60. return 1;
  61. }
  62. child_argv.append(nullptr);
  63. int pid = fork();
  64. if (pid < 0) {
  65. perror("fork");
  66. return 1;
  67. }
  68. if (!pid) {
  69. if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1) {
  70. perror("traceme");
  71. return 1;
  72. }
  73. int rc = execvp(child_argv.first(), const_cast<char**>(child_argv.data()));
  74. if (rc < 0) {
  75. perror("execvp");
  76. exit(1);
  77. }
  78. VERIFY_NOT_REACHED();
  79. }
  80. g_pid = pid;
  81. if (waitpid(pid, &status, WSTOPPED | WEXITED) != pid || !WIFSTOPPED(status)) {
  82. perror("waitpid");
  83. return 1;
  84. }
  85. }
  86. struct sigaction sa;
  87. memset(&sa, 0, sizeof(struct sigaction));
  88. sa.sa_handler = handle_sigint;
  89. sigaction(SIGINT, &sa, nullptr);
  90. if (ptrace(PT_ATTACH, g_pid, 0, 0) == -1) {
  91. perror("attach");
  92. return 1;
  93. }
  94. if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
  95. perror("waitpid");
  96. return 1;
  97. }
  98. for (;;) {
  99. if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
  100. perror("syscall");
  101. return 1;
  102. }
  103. if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
  104. perror("wait_pid");
  105. return 1;
  106. }
  107. PtraceRegisters regs = {};
  108. if (ptrace(PT_GETREGS, g_pid, &regs, 0) == -1) {
  109. perror("getregs");
  110. return 1;
  111. }
  112. u32 syscall_index = regs.eax;
  113. u32 arg1 = regs.edx;
  114. u32 arg2 = regs.ecx;
  115. u32 arg3 = regs.ebx;
  116. if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
  117. perror("syscall");
  118. return 1;
  119. }
  120. if (waitpid(g_pid, &status, WSTOPPED | WEXITED) != g_pid || !WIFSTOPPED(status)) {
  121. perror("wait_pid");
  122. return 1;
  123. }
  124. if (ptrace(PT_GETREGS, g_pid, &regs, 0) == -1) {
  125. perror("getregs");
  126. return 1;
  127. }
  128. u32 res = regs.eax;
  129. auto string = String::formatted("{}({:#08x}, {:#08x}, {:#08x})\t={}\n",
  130. Syscall::to_string((Syscall::Function)syscall_index),
  131. arg1,
  132. arg2,
  133. arg3,
  134. res);
  135. if (!trace_file->write(string)) {
  136. warnln("write: {}", trace_file->error_string());
  137. return 1;
  138. }
  139. }
  140. return 0;
  141. }