strace.cpp 3.9 KB

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