File: ptrace.cpp

package info (click to toggle)
llvm-toolchain-16 1%3A16.0.6-15~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,634,792 kB
  • sloc: cpp: 6,179,261; ansic: 1,216,205; asm: 741,319; python: 196,614; objc: 75,325; f90: 49,640; lisp: 32,396; pascal: 12,286; sh: 9,394; perl: 7,442; ml: 5,494; awk: 3,523; makefile: 2,723; javascript: 1,206; xml: 886; fortran: 581; cs: 573
file content (155 lines) | stat: -rw-r--r-- 4,267 bytes parent folder | download | duplicates (15)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// 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
#if defined(__aarch64__) || defined(__loongarch__)
// 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 (__loongarch__)
    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.csr_era)
      printf("%lx\n", regs.csr_era);

    struct user_fp_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.fcsr)
      printf("%x\n", fpregs.fcsr);
#endif // (__loongarch__)

#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;
}