-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
52 lines (47 loc) · 1.62 KB
/
Copy pathmain.cpp
File metadata and controls
52 lines (47 loc) · 1.62 KB
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
#include <iostream>
#include <unistd.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
int main(int argc, char* argv[]) {
if (argc == 1) {
std::cout << "pass a program to debug!" << std::endl;
return 1;
}
std::cout << argv[1] << std::endl;
// A process can initiate a trace by calling fork(2) and having the
// resulting child do a PTRACE_TRACEME, followed (typically) by an
// execve(2).
pid_t pid = fork();
int ret;
int status;
if (pid == -1) {
std::cout << "fork failed" << std::endl;
return 1;
} else if (pid == 0) {
// Child process
// ret = ptrace(PT_TRACE_ME, 0, 0, 0);
// std::cout << "ptrace: " << ret << std::endl;
ret = execve(argv[1], NULL, NULL);
// execve will not return if successful
std::cout << "execve returned, oh no: " << ret << std::endl;
} else {
do {
ret = waitpid(pid, &status, 0);
if (ret == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("exited, status=%d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("killed by signal %d\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("stopped by signal %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
std::cout << "waitpid: " << ret << " " << status << std::endl;
}
return 0;
}