mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:07:35 +00:00
strace: Change implementation to use ptrace()
This commit is contained in:
parent
6b74d38aab
commit
e5ebdb9bca
6 changed files with 177 additions and 38 deletions
|
@ -46,6 +46,7 @@ LIBC_OBJS = \
|
||||||
sys/socket.o \
|
sys/socket.o \
|
||||||
sys/wait.o \
|
sys/wait.o \
|
||||||
sys/uio.o \
|
sys/uio.o \
|
||||||
|
sys/ptrace.o \
|
||||||
poll.o \
|
poll.o \
|
||||||
locale.o \
|
locale.o \
|
||||||
arpa/inet.o \
|
arpa/inet.o \
|
||||||
|
|
44
Libraries/LibC/sys/ptrace.cpp
Normal file
44
Libraries/LibC/sys/ptrace.cpp
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Kernel/Syscall.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/ptrace.h>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
int ptrace(int request, pid_t pid, void* addr, int data)
|
||||||
|
{
|
||||||
|
Syscall::SC_ptrace_params params {
|
||||||
|
request,
|
||||||
|
pid,
|
||||||
|
reinterpret_cast<u8*>(addr),
|
||||||
|
data
|
||||||
|
};
|
||||||
|
int rc = syscall(SC_ptrace, ¶ms);
|
||||||
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||||
|
}
|
||||||
|
}
|
42
Libraries/LibC/sys/ptrace.h
Normal file
42
Libraries/LibC/sys/ptrace.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
__BEGIN_DECLS
|
||||||
|
|
||||||
|
#define PT_TRACE_ME 1
|
||||||
|
#define PT_ATTACH 2
|
||||||
|
#define PT_CONTINUE 3
|
||||||
|
#define PT_SYSCALL 4
|
||||||
|
#define PT_GETREGS 5
|
||||||
|
#define PT_DETACH 6
|
||||||
|
|
||||||
|
int ptrace(int request, pid_t pid, void* addr, int data);
|
||||||
|
|
||||||
|
__END_DECLS
|
|
@ -45,12 +45,6 @@
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
int systrace(pid_t pid)
|
|
||||||
{
|
|
||||||
int rc = syscall(SC_systrace, pid);
|
|
||||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int chown(const char* pathname, uid_t uid, gid_t gid)
|
int chown(const char* pathname, uid_t uid, gid_t gid)
|
||||||
{
|
{
|
||||||
if (!pathname) {
|
if (!pathname) {
|
||||||
|
|
|
@ -58,7 +58,6 @@ int get_process_name(char* buffer, int buffer_size);
|
||||||
void dump_backtrace();
|
void dump_backtrace();
|
||||||
int fsync(int fd);
|
int fsync(int fd);
|
||||||
void sysbeep();
|
void sysbeep();
|
||||||
int systrace(pid_t);
|
|
||||||
int gettid();
|
int gettid();
|
||||||
int donate(int tid);
|
int donate(int tid);
|
||||||
int set_process_icon(int icon_id);
|
int set_process_icon(int icon_id);
|
||||||
|
|
|
@ -25,18 +25,34 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
|
#include <AK/LogStream.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <Kernel/Syscall.h>
|
#include <Kernel/Syscall.h>
|
||||||
|
#include <LibC/sys/arch/i386/regs.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/ptrace.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static int usage()
|
static int usage()
|
||||||
{
|
{
|
||||||
printf("usage: strace [-p PID] [command...]\n");
|
printf("usage: strace [-p pid] [command...]\n");
|
||||||
return 0;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int g_pid = -1;
|
||||||
|
|
||||||
|
static void handle_sigint(int)
|
||||||
|
{
|
||||||
|
if (g_pid == -1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (ptrace(PT_DETACH, g_pid, 0, 0) == -1) {
|
||||||
|
perror("detach");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
|
@ -44,18 +60,17 @@ int main(int argc, char** argv)
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
return usage();
|
return usage();
|
||||||
|
|
||||||
pid_t pid = -1;
|
|
||||||
bool pid_is_child = false;
|
|
||||||
|
|
||||||
if (!strcmp(argv[1], "-p")) {
|
if (!strcmp(argv[1], "-p")) {
|
||||||
if (argc != 3)
|
if (argc != 3)
|
||||||
return usage();
|
return usage();
|
||||||
pid = atoi(argv[2]);
|
g_pid = atoi(argv[2]);
|
||||||
} else {
|
} else {
|
||||||
pid_is_child = true;
|
int pid = fork();
|
||||||
pid = fork();
|
|
||||||
if (!pid) {
|
if (!pid) {
|
||||||
kill(getpid(), SIGSTOP);
|
if (ptrace(PT_TRACE_ME, 0, 0, 0) == -1) {
|
||||||
|
perror("traceme");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
int rc = execvp(argv[1], &argv[1]);
|
int rc = execvp(argv[1], &argv[1]);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
perror("execvp");
|
perror("execvp");
|
||||||
|
@ -63,40 +78,84 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
if (waitpid(pid, nullptr, WSTOPPED) != pid) {
|
||||||
|
perror("waitpid");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
g_pid = pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fd = systrace(pid);
|
struct sigaction sa;
|
||||||
if (fd < 0) {
|
memset(&sa, 0, sizeof(struct sigaction));
|
||||||
perror("systrace");
|
sa.sa_handler = handle_sigint;
|
||||||
|
sigaction(SIGINT, &sa, nullptr);
|
||||||
|
|
||||||
|
if (ptrace(PT_ATTACH, g_pid, 0, 0) == -1) {
|
||||||
|
perror("attach");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pid_is_child) {
|
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
|
||||||
int rc = kill(pid, SIGCONT);
|
perror("waitpid");
|
||||||
if (rc < 0) {
|
return 1;
|
||||||
perror("kill(pid, SIGCONT)");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
u32 call[5];
|
if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
|
||||||
int nread = read(fd, &call, sizeof(call));
|
if (errno == ESRCH)
|
||||||
if (nread == 0)
|
return 0;
|
||||||
break;
|
perror("syscall");
|
||||||
if (nread < 0) {
|
return 1;
|
||||||
perror("read");
|
}
|
||||||
|
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
|
||||||
|
perror("wait_pid");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
ASSERT(nread == sizeof(call));
|
|
||||||
fprintf(stderr, "%s(%#x, %#x, %#x) = %#x\n", Syscall::to_string((Syscall::Function)call[0]), call[1], call[2], call[3], call[4]);
|
|
||||||
}
|
|
||||||
|
|
||||||
int rc = close(fd);
|
PtraceRegisters regs = {};
|
||||||
if (rc < 0) {
|
if (ptrace(PT_GETREGS, g_pid, ®s, 0) == -1) {
|
||||||
perror("close");
|
perror("getregs");
|
||||||
return 1;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 syscall_index = regs.eax;
|
||||||
|
u32 arg1 = regs.edx;
|
||||||
|
u32 arg2 = regs.ecx;
|
||||||
|
u32 arg3 = regs.ebx;
|
||||||
|
|
||||||
|
// skip syscall exit
|
||||||
|
if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
|
||||||
|
if (errno == ESRCH)
|
||||||
|
return 0;
|
||||||
|
perror("syscall");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
|
||||||
|
perror("wait_pid");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ptrace(PT_GETREGS, g_pid, ®s, 0) == -1) {
|
||||||
|
if (errno == ESRCH && syscall_index == SC_exit) {
|
||||||
|
regs.eax = 0;
|
||||||
|
} else {
|
||||||
|
perror("getregs");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 res = regs.eax;
|
||||||
|
|
||||||
|
fprintf(stderr, "%s(0x%x, 0x%x, 0x%x)\t=%d\n",
|
||||||
|
Syscall::to_string(
|
||||||
|
(Syscall::Function)syscall_index),
|
||||||
|
arg1,
|
||||||
|
arg2,
|
||||||
|
arg3,
|
||||||
|
res);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue