Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | // RUN: %clangxx -O0 %s -o %t && %run %t |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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> |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 11 | #if __mips64 |
| 12 | #include <asm/ptrace.h> |
| 13 | #include <sys/procfs.h> |
| 14 | #endif |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 24 | int res; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 25 | |
| 26 | #if __x86_64__ |
| 27 | user_regs_struct regs; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 28 | res = ptrace(PTRACE_GETREGS, pid, NULL, ®s); |
| 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); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 38 | #endif // __x86_64__ |
| 39 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 40 | #if (__powerpc64__ || __mips64) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 41 | struct pt_regs regs; |
| 42 | res = ptrace((enum __ptrace_request)PTRACE_GETREGS, pid, NULL, ®s); |
| 43 | assert(!res); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 44 | #if (__powerpc64__) |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 45 | if (regs.nip) |
| 46 | printf("%lx\n", regs.nip); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 47 | #else |
| 48 | if (regs.cp0_epc) |
| 49 | printf("%lx\n", regs.cp0_epc); |
| 50 | #endif |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 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]); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 56 | #endif // (__powerpc64__ || __mips64) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | } |