aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 1d23902cf376237daa9ae505b73b99efa8ef8d73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;
}