File: ptrace.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (133 lines) | stat: -rw-r--r-- 3,620 bytes parent folder | download | duplicates (4)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// RUN: %clangxx -O0 %s -o %t && %run %t

// UNSUPPORTED: android

#include <assert.h>
#include <elf.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <unistd.h>
#if __mips64 || __arm__
 #include <asm/ptrace.h>
 #include <sys/procfs.h>
#endif
#ifdef __aarch64__
// GLIBC 2.20+ sys/user does not include asm/ptrace.h
 #include <asm/ptrace.h>
#endif

int main(void) {
  pid_t pid;
  pid = fork();
  if (pid == 0) { // child
    ptrace(PTRACE_TRACEME, 0, NULL, NULL);
    execl("/bin/true", "true", NULL);
  } else {
    wait(NULL);
    int res;

#if __x86_64__
    user_regs_struct regs;
    res = ptrace(PTRACE_GETREGS, pid, NULL, &regs);
    assert(!res);
    if (regs.rip)
      printf("%zx\n", regs.rip);

    user_fpregs_struct fpregs;
    res = ptrace(PTRACE_GETFPREGS, pid, NULL, &fpregs);
    assert(!res);
    if (fpregs.mxcsr)
      printf("%x\n", fpregs.mxcsr);
#endif // __x86_64__

#if (__powerpc64__ || __mips64 || __arm__)
    // Check that nothing writes out-of-bounds.
    struct pt_regs regs_buf[4];
    memset(&regs_buf, 0xcd, sizeof(regs_buf));
    struct pt_regs &regs = regs_buf[1];
    res = ptrace((enum __ptrace_request)PTRACE_GETREGS, pid, NULL, &regs);
    assert(!res);
    assert(memcmp(&regs_buf[0], &regs_buf[3], sizeof(regs_buf[3])) == 0);
    assert(memcmp(&regs_buf[2], &regs_buf[3], sizeof(regs_buf[3])) == 0);
#if (__powerpc64__)
    if (regs.nip)
      printf("%lx\n", regs.nip);
#elif (__mips64)
    if (regs.cp0_epc)
    printf("%lx\n", regs.cp0_epc);
#elif (__arm__)
    if (regs.ARM_pc)
    printf("%lx\n", regs.ARM_pc);
#endif
#if (__powerpc64 || __mips64)
    elf_fpregset_t fpregs;
    res = ptrace((enum __ptrace_request)PTRACE_GETFPREGS, pid, NULL, &fpregs);
    assert(!res);
    if ((elf_greg_t)fpregs[32]) // fpscr
      printf("%lx\n", (elf_greg_t)fpregs[32]);
#elif (__arm__)
    char regbuf[ARM_VFPREGS_SIZE];
    res = ptrace((enum __ptrace_request)PTRACE_GETVFPREGS, pid, 0, regbuf);
    assert(!res);
    unsigned fpscr = *(unsigned*)(regbuf + (32 * 8));
    printf ("%x\n", fpscr);
#endif
#endif // (__powerpc64__ || __mips64 || __arm__)

#if (__aarch64__)
    struct iovec regset_io;

    struct user_pt_regs regs;
    regset_io.iov_base = &regs;
    regset_io.iov_len = sizeof(regs);
    res = ptrace(PTRACE_GETREGSET, pid, (void*)NT_PRSTATUS, (void*)&regset_io);
    assert(!res);
    if (regs.pc)
      printf("%llx\n", regs.pc);

    struct user_fpsimd_state fpregs;
    regset_io.iov_base = &fpregs;
    regset_io.iov_len = sizeof(fpregs);
    res = ptrace(PTRACE_GETREGSET, pid, (void*)NT_FPREGSET, (void*)&regset_io);
    assert(!res);
    if (fpregs.fpsr)
      printf("%x\n", fpregs.fpsr);
#endif // (__aarch64__)

#if (__s390__)
    struct iovec regset_io;

    struct _user_regs_struct regs;
    regset_io.iov_base = &regs;
    regset_io.iov_len = sizeof(regs);
    res = ptrace(PTRACE_GETREGSET, pid, (void*)NT_PRSTATUS, (void*)&regset_io);
    assert(!res);
    if (regs.psw.addr)
      printf("%lx\n", regs.psw.addr);

    struct _user_fpregs_struct fpregs;
    regset_io.iov_base = &fpregs;
    regset_io.iov_len = sizeof(fpregs);
    res = ptrace(PTRACE_GETREGSET, pid, (void*)NT_FPREGSET, (void*)&regset_io);
    assert(!res);
    if (fpregs.fpc)
      printf("%x\n", fpregs.fpc);
#endif // (__s390__)

    siginfo_t siginfo;
    res = ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo);
    assert(!res);
    assert(siginfo.si_pid == pid);

    ptrace(PTRACE_CONT, pid, NULL, NULL);

    wait(NULL);
  }
  return 0;
}