]> Projects - linux-game-trainer.git/commitdiff Test
Attaching & Detaching from process
authorOrfeas <redacted>
Mon, 15 Apr 2024 16:38:05 +0000 (19:38 +0300)
committerOrfeas <redacted>
Mon, 15 Apr 2024 16:38:05 +0000 (19:38 +0300)
.gitignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
src/main.c

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..bc22e3a
--- /dev/null
@@ -0,0 +1,3 @@
+*~
+*.out
+*.o
\ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..844cc7b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+CC = gcc
+CFLAGS = -Wall -g
+OBJS = $(patsubst %.c,%.o,$(wildcard src/*.c))
+OUT = hack.out
+
+$(OUT): $(OBJS) 
+       $(CC) $(CFLAGS) -o $@ $(OBJS)
+
+%.o: %.c
+       $(CC) $(CFLAGS) -c $? -o $@
+
+clean:
+       rm -f src/*.o $(OUT)
index b54dbf1531e41962dd5dc0a5145ba99d8d267310..1d23902cf376237daa9ae505b73b99efa8ef8d73 100644 (file)
@@ -1,6 +1,38 @@
+#include <stdlib.h>
 #include <stdio.h>
+#include <sys/ptrace.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define ERROR(...) \
+  do { \
+    fprintf(stderr, __VA_ARGS__); \
+    exit(1); \
+  } while (0)
+
+#define LOG(...) \
+  fprintf(stderr, __VA_ARGS__)
 
 int main(int argc, char *argv[])
 {
-  
+  if (argc < 2) {
+    ERROR("Usage: %s <tracee_pid>\n", argv[0]);
+  }
+
+  int pid;
+  if ((pid = atoi(argv[1])) == 0) {
+    ERROR("Invalid pid '%s'\n", argv[1]);
+  }
+
+  ptrace(PTRACE_ATTACH, pid, NULL, NULL);
+  waitpid(pid, NULL, __WALL);
+  LOG("Attached to process %d\n", pid);
+
+  /* Do stuff ... */
+  sleep(5);
+
+  ptrace(PTRACE_DETACH, pid, NULL, NULL);
+  LOG("Detached from process %d\n", pid);
+
+  return 0;
 }