-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathptrace.c
77 lines (64 loc) · 1.44 KB
/
ptrace.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "ptrace.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
void alarm_handler(int signum) {}
int ptrace_attach_and_wait(unsigned int pid)
{
const long status = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
if (status < 0)
{
return -1;
}
const void* prev_handler = signal(SIGALRM, alarm_handler);
if (prev_handler != 0)
{
ptrace_detach(pid);
return -1;
}
static const unsigned int ATTACH_TIMEOUT = 10;
alarm(ATTACH_TIMEOUT);
static int* NO_STATUS = NULL;
const unsigned int attached_pid = waitpid(pid, NO_STATUS, WSTOPPED);
alarm(0);
if (attached_pid != pid)
{
ptrace_detach(pid);
return -1;
}
return 0;
}
int ptrace_detach(unsigned int pid)
{
const long status = ptrace(PTRACE_DETACH, pid, NULL, NULL);
if (status < 0)
{
return -1;
}
return 0;
}
int ptrace_peek(unsigned int pid, unsigned long addr, unsigned long* out)
{
errno = 0;
const long data = ptrace(PTRACE_PEEKDATA, pid, addr, NULL);
if (errno != 0 && data < 0)
{
return -1;
}
*out = data;
return 0;
}
int ptrace_poke(unsigned int pid, unsigned long addr, unsigned long data)
{
const long status = ptrace(PTRACE_POKEDATA, pid, addr, data);
if (status < 0)
{
return -1;
}
return 0;
}