aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
authorOrfeas <38209077+0xfea5@users.noreply.github.com>2024-04-15 19:38:05 +0300
committerOrfeas <38209077+0xfea5@users.noreply.github.com>2024-04-15 19:38:05 +0300
commit0be46c952901aaafe76e1fb5a8faecb38323d549 (patch)
tree9ec9e7a2c99bd5cd81e17f1578a7be725558f88e /src/main.c
parentInitial commit (diff)
downloadlinux-game-trainer-0be46c952901aaafe76e1fb5a8faecb38323d549.tar.gz
linux-game-trainer-0be46c952901aaafe76e1fb5a8faecb38323d549.zip
Attaching & Detaching from process
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/main.c b/src/main.c
index b54dbf1..1d23902 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,6 +1,38 @@
1#include <stdlib.h>
1#include <stdio.h> 2#include <stdio.h>
3#include <sys/ptrace.h>
4#include <sys/wait.h>
5#include <unistd.h>
6
7#define ERROR(...) \
8 do { \
9 fprintf(stderr, __VA_ARGS__); \
10 exit(1); \
11 } while (0)
12
13#define LOG(...) \
14 fprintf(stderr, __VA_ARGS__)
2 15
3int main(int argc, char *argv[]) 16int main(int argc, char *argv[])
4{ 17{
5 18 if (argc < 2) {
19 ERROR("Usage: %s <tracee_pid>\n", argv[0]);
20 }
21
22 int pid;
23 if ((pid = atoi(argv[1])) == 0) {
24 ERROR("Invalid pid '%s'\n", argv[1]);
25 }
26
27 ptrace(PTRACE_ATTACH, pid, NULL, NULL);
28 waitpid(pid, NULL, __WALL);
29 LOG("Attached to process %d\n", pid);
30
31 /* Do stuff ... */
32 sleep(5);
33
34 ptrace(PTRACE_DETACH, pid, NULL, NULL);
35 LOG("Detached from process %d\n", pid);
36
37 return 0;
6} 38}