/* $Id$ */ /* Copyright (c) 2009-2015 Pierre Pronchery */ /* This file is part of DeforaOS Devel strace */ /* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include #include #include "platform.h" #include "strace.h" #include "../config.h" #ifndef PROGNAME # define PROGNAME PACKAGE #endif /* strace */ /* private */ /* prototypes */ static int _strace_error(char const * message, int ret); static int _strace_handle(pid_t pid, int res); /* public */ /* functions */ /* strace */ static int _strace_child(char * argv[]); static int _strace_parent(pid_t pid); int strace(char * argv[]) { pid_t pid; #ifdef DEBUG fprintf(stderr, "DEBUG: %s(\"%s\")\n", __func__, argv[0]); #endif if((pid = fork()) == -1) return _strace_error("fork", 1); if(pid == 0) return _strace_child(argv); return _strace_parent(pid); } static int _strace_child(char * argv[]) { errno = 0; if(ptrace(PT_TRACE_ME, -1, NULL, (ptrace_data_t)0) == -1 && errno != 0) { _strace_error("ptrace", 1); _exit(125); } else { execvp(argv[0], argv); _strace_error(argv[0], 1); _exit(127); } return 0; } static int _strace_parent(pid_t pid) { int status; for(;;) { if(waitpid(pid, &status, 0) == -1) return -_strace_error("waitpid", 1); if(_strace_handle(pid, status) != 0) return 0; } } /* private */ /* functions */ /* strace_error */ static int _strace_error(char const * message, int ret) { fputs(PROGNAME ": ", stderr); perror(message); return ret; } /* strace_handle */ static void _handle_trap_before(pid_t pid); static void _handle_trap_after(pid_t pid); static int _strace_handle(pid_t pid, int status) { if(!WIFSTOPPED(status)) return -1; switch(WSTOPSIG(status)) { case SIGTRAP: /* examine the system call */ _handle_trap_before(pid); /* execute the system call */ ptrace(PT_SYSCALL, pid, (caddr_t)1, (ptrace_data_t)0); /* examine the return value and data */ _handle_trap_after(pid); /* wait until the next syscall */ ptrace(PT_SYSCALL, pid, (caddr_t)1, (ptrace_data_t)0); break; default: ptrace(PT_CONTINUE, pid, (caddr_t)1, WSTOPSIG(status)); break; } return 0; } static void _handle_trap_before(pid_t pid) { struct user context; char const * syscall; platform_get_registers(pid, &context); #ifdef DEBUG platform_print_registers(pid, &context); #endif if((syscall = platform_get_syscall(pid, &context)) == NULL) syscall = "syscall"; fprintf(stderr, "%s()", syscall); } static void _handle_trap_after(pid_t pid) { struct user context; long res; platform_get_registers(pid, &context); #ifdef DEBUG platform_print_registers(pid, &context); #endif res = platform_get_result(pid, &context); fprintf(stderr, " => %ld\n", res); }