diff options
Diffstat (limited to 'src')
| -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 | } |
