Home | History | Annotate | Download | only in Linux
      1 // RUN: %clangxx -O0 %s -o %t && %run %t
      2 
      3 #include <assert.h>
      4 #include <signal.h>
      5 #include <stdio.h>
      6 #include <sys/ptrace.h>
      7 #include <sys/types.h>
      8 #include <sys/user.h>
      9 #include <sys/wait.h>
     10 #include <unistd.h>
     11 #if __mips64
     12  #include <asm/ptrace.h>
     13  #include <sys/procfs.h>
     14 #endif
     15 
     16 int main(void) {
     17   pid_t pid;
     18   pid = fork();
     19   if (pid == 0) { // child
     20     ptrace(PTRACE_TRACEME, 0, NULL, NULL);
     21     execl("/bin/true", "true", NULL);
     22   } else {
     23     wait(NULL);
     24     int res;
     25 
     26 #if __x86_64__
     27     user_regs_struct regs;
     28     res = ptrace(PTRACE_GETREGS, pid, NULL, &regs);
     29     assert(!res);
     30     if (regs.rip)
     31       printf("%zx\n", regs.rip);
     32 
     33     user_fpregs_struct fpregs;
     34     res = ptrace(PTRACE_GETFPREGS, pid, NULL, &fpregs);
     35     assert(!res);
     36     if (fpregs.mxcsr)
     37       printf("%x\n", fpregs.mxcsr);
     38 #endif // __x86_64__
     39 
     40 #if (__powerpc64__ || __mips64)
     41     struct pt_regs regs;
     42     res = ptrace((enum __ptrace_request)PTRACE_GETREGS, pid, NULL, &regs);
     43     assert(!res);
     44 #if (__powerpc64__)
     45     if (regs.nip)
     46       printf("%lx\n", regs.nip);
     47 #else
     48     if (regs.cp0_epc)
     49     printf("%lx\n", regs.cp0_epc);
     50 #endif
     51     elf_fpregset_t fpregs;
     52     res = ptrace((enum __ptrace_request)PTRACE_GETFPREGS, pid, NULL, &fpregs);
     53     assert(!res);
     54     if ((elf_greg_t)fpregs[32]) // fpscr
     55       printf("%lx\n", (elf_greg_t)fpregs[32]);
     56 #endif // (__powerpc64__ || __mips64)
     57 
     58     siginfo_t siginfo;
     59     res = ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo);
     60     assert(!res);
     61     assert(siginfo.si_pid == pid);
     62 
     63     ptrace(PTRACE_CONT, pid, NULL, NULL);
     64 
     65     wait(NULL);
     66   }
     67   return 0;
     68 }
     69