-
Notifications
You must be signed in to change notification settings - Fork 0
/
forkExampleWithPtrace.cpp
35 lines (32 loc) · 1.12 KB
/
forkExampleWithPtrace.cpp
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
#include <cstdint>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/user.h>
using namespace std;
int main() {
pid_t pid = fork();
uint32_t temp = 0;
switch(pid) {
case -1:
cout << "Error occurred" << endl;
return -1;
case 0:
temp = 1;
ptrace(PTRACE_TRACEME, 0, nullptr, nullptr);
raise(SIGSTOP);
cout << "Child process" << endl;
break;
default:
temp = 2;
cout << "Parent process" << endl;
waitpid(pid, nullptr, 0);
ptrace(PTRACE_ATTACH, pid, nullptr, nullptr);
uint32_t child_temp = ptrace(PTRACE_PEEKDATA, pid, &temp, nullptr);
cout << "Child's temp value is: " << child_temp << endl;
ptrace(PTRACE_DETACH, pid, nullptr, nullptr);
}
return 0;
}