Serenity Operating System
at hosted 161 lines 4.5 kB view raw
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 27#include <AK/Assertions.h> 28#include <AK/LogStream.h> 29#include <AK/Types.h> 30#include <Kernel/Syscall.h> 31#include <LibC/sys/arch/i386/regs.h> 32#include <signal.h> 33#include <stdio.h> 34#include <stdlib.h> 35#include <string.h> 36#include <sys/ptrace.h> 37#include <sys/wait.h> 38#include <unistd.h> 39 40static int usage() 41{ 42 printf("usage: strace [-p pid] [command...]\n"); 43 return 1; 44} 45 46static int g_pid = -1; 47 48static void handle_sigint(int) 49{ 50 if (g_pid == -1) 51 return; 52 53 if (ptrace(PT_DETACH, g_pid, 0, 0) == -1) { 54 perror("detach"); 55 } 56} 57 58int main(int argc, char** argv) 59{ 60 if (argc == 1) 61 return usage(); 62 63 if (!strcmp(argv[1], "-p")) { 64 if (argc != 3) 65 return usage(); 66 g_pid = atoi(argv[2]); 67 } else { 68 int pid = fork(); 69 if (!pid) { 70 if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1) { 71 perror("traceme"); 72 return 1; 73 } 74 int rc = execvp(argv[1], &argv[1]); 75 if (rc < 0) { 76 perror("execvp"); 77 exit(1); 78 } 79 ASSERT_NOT_REACHED(); 80 } 81 if (waitpid(pid, nullptr, WSTOPPED) != pid) { 82 perror("waitpid"); 83 return 1; 84 } 85 g_pid = pid; 86 } 87 88 struct sigaction sa; 89 memset(&sa, 0, sizeof(struct sigaction)); 90 sa.sa_handler = handle_sigint; 91 sigaction(SIGINT, &sa, nullptr); 92 93 if (ptrace(PT_ATTACH, g_pid, 0, 0) == -1) { 94 perror("attach"); 95 return 1; 96 } 97 98 if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) { 99 perror("waitpid"); 100 return 1; 101 } 102 103 for (;;) { 104 if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) { 105 if (errno == ESRCH) 106 return 0; 107 perror("syscall"); 108 return 1; 109 } 110 if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) { 111 perror("wait_pid"); 112 return 1; 113 } 114 115 PtraceRegisters regs = {}; 116 if (ptrace(PT_GETREGS, g_pid, &regs, 0) == -1) { 117 perror("getregs"); 118 return 1; 119 } 120 121 u32 syscall_index = regs.eax; 122 u32 arg1 = regs.edx; 123 u32 arg2 = regs.ecx; 124 u32 arg3 = regs.ebx; 125 126 // skip syscall exit 127 if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) { 128 if (errno == ESRCH) 129 return 0; 130 perror("syscall"); 131 return 1; 132 } 133 if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) { 134 perror("wait_pid"); 135 return 1; 136 } 137 138 if (ptrace(PT_GETREGS, g_pid, &regs, 0) == -1) { 139 if (errno == ESRCH && syscall_index == SC_exit) { 140 regs.eax = 0; 141 } else { 142 perror("getregs"); 143 return 1; 144 } 145 } 146 147 u32 res = regs.eax; 148 149 fprintf(stderr, "%s(0x%x, 0x%x, 0x%x)\t=%d\n", 150 Syscall::to_string( 151 (Syscall::Function)syscall_index), 152 arg1, 153 arg2, 154 arg3, 155 res); 156 } 157 158 return 0; 159 160 return 0; 161}