diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-04-15 19:38:05 +0300 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-04-15 19:38:05 +0300 |
| commit | 0be46c952901aaafe76e1fb5a8faecb38323d549 (patch) | |
| tree | 9ec9e7a2c99bd5cd81e17f1578a7be725558f88e /src/main.c | |
| parent | Initial commit (diff) | |
| download | linux-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.c | 34 |
1 files changed, 33 insertions, 1 deletions
| @@ -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 | ||
| 3 | int main(int argc, char *argv[]) | 16 | int 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 | } |
