DebugServer2
Loading...
Searching...
No Matches
ExtraWrappers.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include "DebugServer2/Base.h"
14#include "DebugServer2/Utils/CompilerSupport.h"
15
16#include <asm/unistd.h>
17#include <fcntl.h>
18#include <signal.h>
19#if defined(HAVE_SYS_PERSONALITY_H)
20#include <sys/personality.h>
21#endif
22#include <sys/ptrace.h>
23#include <sys/syscall.h>
24#include <sys/user.h>
25#include <unistd.h>
26
27#if !defined(DOXYGEN)
28
29#if defined(ARCH_ARM) && !defined(ARM_VFPREGS_SIZE)
30#define ARM_VFPREGS_SIZE (32 * 8 + 4)
31#endif // ARCH_ARM && !ARM_VFPREGS_SIZE
32
33#if defined(ARCH_X86) || defined(ARCH_X86_64)
34static constexpr size_t x87Padding = 6;
35
36// Required structs for PTrace GETREGSET with NT_X86_STATE
37// These structs are not made available by the system headers
38struct YMMHighVector {
39 uint8_t value[16];
40};
41
42struct xsave_hdr {
43 uint64_t xfeatures_mask;
44 uint64_t xcomp_bv;
45 uint64_t reserved[6];
46} DS2_ATTRIBUTE_PACKED;
47
48#if !defined(NT_X86_XSTATE)
49#define NT_X86_XSTATE 0x202
50#endif // !NT_X86_XSTATE
51
52struct fxsave_struct {
53 uint16_t fctw;
54 uint16_t fstw;
55 uint16_t ftag;
56 uint16_t fop;
57 uint32_t fioff;
58 uint32_t fiseg;
59 uint32_t fooff;
60 uint32_t foseg;
61 uint32_t mxcsr;
62 uint32_t mxcsrmask;
63 uint8_t st_space[128]; // There are 8 stmm registers, each one takes up
64 // 10 bytes of data and 6 bytes of padding for a total
65 // of 16 bytes per register (8 * 16 = 128).
66 uint8_t xmm_space[256]; // There are 16 xmm registers, each take up 16 bytes
67 // (16 * 16 = 256).
68 // NOTE: x86 only uses the first 8 registers, the
69 // second 128 bytes act as padding.
70 uint8_t padding1[48];
71 uint64_t xcr0; // xcr0 occurs at byte offset 464 into this structure
72 uint8_t padding[40];
73};
74
75struct xsave_struct {
76 fxsave_struct fpregs;
77 xsave_hdr header;
78 YMMHighVector ymmh[16]; // NOTE: x86 only uses the first 8 registers, the
79 // the second 128 bytes act as padding.
80} DS2_ATTRIBUTE_PACKED DS2_ATTRIBUTE_ALIGNED(64);
81
82#endif // ARCH_X86 || ARCH_X86_64
83
84#if !defined(HAVE_POSIX_OPENPT)
85// Older android sysroots don't have `posix_openpt` but they all use the
86// `/dev/ptmx` device.
87static inline int posix_openpt(int flags) { return ::open("/dev/ptmx", flags); }
88#endif // !HAVE_POSIX_OPENPT
89
90// Do not use PLATFORM_ANDROID here. We want to test if we're using the Android
91// toolchain, not if we're building for the Android target. In some cases, we
92// build Tizen binaries with the Android toolchain.
93#if !defined(HAVE_GETTID)
94// Android headers do have a wrapper for `gettid`, unlike glibc.
95static inline pid_t gettid() { return ::syscall(SYS_gettid); }
96#endif // !HAVE_GETTID
97
98#if !defined(SYS_tkill)
99#define SYS_tkill __NR_tkill
100#endif // !SYS_tkill
101
102#if !defined(HAVE_TGKILL)
103#if !defined(SYS_tgkill)
104#define SYS_tgkill __NR_tgkill
105#endif // !SYS_tgkill
106
107static inline int tgkill(pid_t pid, pid_t tid, int signo) {
108 return ::syscall(SYS_tgkill, pid, tid, signo);
109}
110#endif // !HAVE_TGKILL
111
112static inline int tkill(pid_t tid, int signo) {
113 return ::syscall(SYS_tkill, tid, signo);
114}
115
116#if !defined(HAVE_SYS_PERSONALITY_H)
117#if !defined(SYS_personality)
118#define SYS_personality __NR_personality
119#endif // !SYS_personality
120
121#if !defined(ADDR_NO_RANDOMIZE)
122#define ADDR_NO_RANDOMIZE 0x0040000
123#endif // !ADDR_NO_RANDOMIZE
124
125static inline int personality(unsigned long persona) {
126 return ::syscall(SYS_personality, persona);
127}
128#endif // !HAVE_SYS_PERSONALITY_H
129
130#if defined(ARCH_ARM)
131#if !defined(PTRACE_GETHBPREGS)
132#define PTRACE_GETHBPREGS 29
133#endif // !PTRACE_GETHBPREGS
134
135#if !defined(PTRACE_SETHBPREGS)
136#define PTRACE_SETHBPREGS 30
137#endif // !PTRACE_SETHBPREGS
138#endif // ARCH_ARM
139
140#if !defined(PTRACE_GETREGSET)
141#define PTRACE_GETREGSET 0x4204
142#endif // !PTRACE_GETREGSET
143
144#if !defined(PTRACE_SETREGSET)
145#define PTRACE_SETREGSET 0x4205
146#endif // !PTRACE_SETREGSET
147
148// As defined in <asm-generic/siginfo.h>, missing in glibc
149#if !defined(TRAP_BRKPT)
150#define TRAP_BRKPT 1
151#endif
152#if !defined(TRAP_TRACE)
153#define TRAP_TRACE 2
154#endif
155#if !defined(TRAP_BRANCH)
156#define TRAP_BRANCH 3
157#endif
158#if !defined(TRAP_HWBKPT)
159#define TRAP_HWBKPT 4
160#endif
161
162#endif // !DOXYGEN