-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.c
60 lines (52 loc) · 1.1 KB
/
logic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
** EPITECH PROJECT, 2020
** PSU_strace_2019
** File description:
** automated desc ftw
*/
#include "strace.h"
#include "syscalls.h"
void loop(bool full, pid_t p)
{
for (; !wait_syscall(p); ) {
print_syscall(full, p);
if (wait_syscall(p))
break;
print_ret(full, p);
}
}
struct user_regs_struct get_registers(pid_t p)
{
static struct user_regs_struct ret;
errno = 0;
ptrace(PTRACE_GETREGS, p, 0, &ret);
if (errno) {
perror("strace");
exit(84);
}
return ret;
}
bool wait_syscall(pid_t p)
{
int s = 0;
while (true) {
ptrace(PTRACE_SYSCALL, p, 0, 0);
waitpid(p, &s, 0);
if (WIFSTOPPED(s) && (WSTOPSIG(s) & 0x80))
return 0;
if (WIFEXITED(s)) {
fprintf(stderr, "--- %s ---\n", ALLSIGS[(s)]);
return 1;
}
print_signal(s);
}
}
int trace(bool full, pid_t p)
{
int s = 0;
waitpid(p, &s, 0);
ptrace(PTRACE_SETOPTIONS, p, 0, PTRACE_O_TRACESYSGOOD
| PTRACE_O_EXITKILL | PTRACE_O_TRACEEXIT);
loop(full, p);
return 0;
}