#include #include #include #include #include #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 \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; }