1 // RUN: %clangxx -O0 %s -o %t && %run %t
3 // UNSUPPORTED: android
10 #include <sys/ptrace.h>
11 #include <sys/types.h>
16 #if __mips64 || __arm__
17 #include <asm/ptrace.h>
18 #include <sys/procfs.h>
20 #if defined(__aarch64__) || defined(__loongarch__)
21 // GLIBC 2.20+ sys/user does not include asm/ptrace.h
22 #include <asm/ptrace.h>
28 if (pid
== 0) { // child
29 ptrace(PTRACE_TRACEME
, 0, NULL
, NULL
);
30 execl("/bin/true", "true", NULL
);
36 user_regs_struct regs
;
37 res
= ptrace(PTRACE_GETREGS
, pid
, NULL
, ®s
);
40 printf("%zx\n", regs
.rip
);
42 user_fpregs_struct fpregs
;
43 res
= ptrace(PTRACE_GETFPREGS
, pid
, NULL
, &fpregs
);
46 printf("%x\n", fpregs
.mxcsr
);
49 #if (__powerpc64__ || __mips64 || __arm__)
50 // Check that nothing writes out-of-bounds.
51 struct pt_regs regs_buf
[4];
52 memset(®s_buf
, 0xcd, sizeof(regs_buf
));
53 struct pt_regs
®s
= regs_buf
[1];
54 res
= ptrace((enum __ptrace_request
)PTRACE_GETREGS
, pid
, NULL
, ®s
);
56 assert(memcmp(®s_buf
[0], ®s_buf
[3], sizeof(regs_buf
[3])) == 0);
57 assert(memcmp(®s_buf
[2], ®s_buf
[3], sizeof(regs_buf
[3])) == 0);
60 printf("%lx\n", regs
.nip
);
63 printf("%lx\n", regs
.cp0_epc
);
66 printf("%lx\n", regs
.ARM_pc
);
68 #if (__powerpc64 || __mips64)
69 elf_fpregset_t fpregs
;
70 res
= ptrace((enum __ptrace_request
)PTRACE_GETFPREGS
, pid
, NULL
, &fpregs
);
72 if ((elf_greg_t
)fpregs
[32]) // fpscr
73 printf("%lx\n", (elf_greg_t
)fpregs
[32]);
75 char regbuf
[ARM_VFPREGS_SIZE
];
76 res
= ptrace((enum __ptrace_request
)PTRACE_GETVFPREGS
, pid
, 0, regbuf
);
78 unsigned fpscr
= *(unsigned*)(regbuf
+ (32 * 8));
79 printf ("%x\n", fpscr
);
81 #endif // (__powerpc64__ || __mips64 || __arm__)
84 struct iovec regset_io
;
86 struct user_pt_regs regs
;
87 regset_io
.iov_base
= ®s
;
88 regset_io
.iov_len
= sizeof(regs
);
89 res
= ptrace(PTRACE_GETREGSET
, pid
, (void*)NT_PRSTATUS
, (void*)®set_io
);
92 printf("%llx\n", regs
.pc
);
94 struct user_fpsimd_state fpregs
;
95 regset_io
.iov_base
= &fpregs
;
96 regset_io
.iov_len
= sizeof(fpregs
);
97 res
= ptrace(PTRACE_GETREGSET
, pid
, (void*)NT_FPREGSET
, (void*)®set_io
);
100 printf("%x\n", fpregs
.fpsr
);
101 #endif // (__aarch64__)
104 struct iovec regset_io
;
106 struct user_pt_regs regs
;
107 regset_io
.iov_base
= ®s
;
108 regset_io
.iov_len
= sizeof(regs
);
110 ptrace(PTRACE_GETREGSET
, pid
, (void *)NT_PRSTATUS
, (void *)®set_io
);
113 printf("%lx\n", regs
.csr_era
);
115 struct user_fp_state fpregs
;
116 regset_io
.iov_base
= &fpregs
;
117 regset_io
.iov_len
= sizeof(fpregs
);
119 ptrace(PTRACE_GETREGSET
, pid
, (void *)NT_FPREGSET
, (void *)®set_io
);
122 printf("%x\n", fpregs
.fcsr
);
123 #endif // (__loongarch__)
126 struct iovec regset_io
;
128 struct _user_regs_struct regs
;
129 regset_io
.iov_base
= ®s
;
130 regset_io
.iov_len
= sizeof(regs
);
131 res
= ptrace(PTRACE_GETREGSET
, pid
, (void*)NT_PRSTATUS
, (void*)®set_io
);
134 printf("%lx\n", regs
.psw
.addr
);
136 struct _user_fpregs_struct fpregs
;
137 regset_io
.iov_base
= &fpregs
;
138 regset_io
.iov_len
= sizeof(fpregs
);
139 res
= ptrace(PTRACE_GETREGSET
, pid
, (void*)NT_FPREGSET
, (void*)®set_io
);
142 printf("%x\n", fpregs
.fpc
);
146 res
= ptrace(PTRACE_GETSIGINFO
, pid
, NULL
, &siginfo
);
148 assert(siginfo
.si_pid
== pid
);
150 ptrace(PTRACE_CONT
, pid
, NULL
, NULL
);