1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef OHOS_HIVIEWDFX_REGISTER_H
16 #define OHOS_HIVIEWDFX_REGISTER_H
17
18 #include <algorithm>
19 #include <limits>
20 #include <linux/perf_event.h>
21 #include <map>
22 #include <string>
23 #include <unistd.h>
24 #include "dfx_types.h"
25
26 namespace OHOS {
27 namespace HiviewDFX {
28 // these define copy from kernel uapi
29 enum perf_event_x86_regs {
30 PERF_REG_X86_AX,
31 PERF_REG_X86_BX,
32 PERF_REG_X86_CX,
33 PERF_REG_X86_DX,
34 PERF_REG_X86_SI,
35 PERF_REG_X86_DI,
36 PERF_REG_X86_BP,
37 PERF_REG_X86_SP,
38 PERF_REG_X86_IP,
39 PERF_REG_X86_FLAGS,
40 PERF_REG_X86_CS,
41 PERF_REG_X86_SS,
42 PERF_REG_X86_DS,
43 PERF_REG_X86_ES,
44 PERF_REG_X86_FS,
45 PERF_REG_X86_GS,
46 PERF_REG_X86_R8,
47 PERF_REG_X86_R9,
48 PERF_REG_X86_R10,
49 PERF_REG_X86_R11,
50 PERF_REG_X86_R12,
51 PERF_REG_X86_R13,
52 PERF_REG_X86_R14,
53 PERF_REG_X86_R15,
54 PERF_REG_X86_32_MAX = PERF_REG_X86_GS + 1,
55 PERF_REG_X86_64_MAX = PERF_REG_X86_R15 + 1,
56 };
57
58 enum perf_event_arm64_regs {
59 PERF_REG_ARM64_X0,
60 PERF_REG_ARM64_X1,
61 PERF_REG_ARM64_X2,
62 PERF_REG_ARM64_X3,
63 PERF_REG_ARM64_X4,
64 PERF_REG_ARM64_X5,
65 PERF_REG_ARM64_X6,
66 PERF_REG_ARM64_X7,
67 PERF_REG_ARM64_X8,
68 PERF_REG_ARM64_X9,
69 PERF_REG_ARM64_X10,
70 PERF_REG_ARM64_X11,
71 PERF_REG_ARM64_X12,
72 PERF_REG_ARM64_X13,
73 PERF_REG_ARM64_X14,
74 PERF_REG_ARM64_X15,
75 PERF_REG_ARM64_X16,
76 PERF_REG_ARM64_X17,
77 PERF_REG_ARM64_X18,
78 PERF_REG_ARM64_X19,
79 PERF_REG_ARM64_X20,
80 PERF_REG_ARM64_X21,
81 PERF_REG_ARM64_X22,
82 PERF_REG_ARM64_X23,
83 PERF_REG_ARM64_X24,
84 PERF_REG_ARM64_X25,
85 PERF_REG_ARM64_X26,
86 PERF_REG_ARM64_X27,
87 PERF_REG_ARM64_X28,
88 PERF_REG_ARM64_X29,
89 PERF_REG_ARM64_LR,
90 PERF_REG_ARM64_SP,
91 PERF_REG_ARM64_PC,
92 PERF_REG_ARM64_MAX,
93 };
94
95 enum perf_event_arm_regs {
96 PERF_REG_ARM_R0,
97 PERF_REG_ARM_R1,
98 PERF_REG_ARM_R2,
99 PERF_REG_ARM_R3,
100 PERF_REG_ARM_R4,
101 PERF_REG_ARM_R5,
102 PERF_REG_ARM_R6,
103 PERF_REG_ARM_R7,
104 PERF_REG_ARM_R8,
105 PERF_REG_ARM_R9,
106 PERF_REG_ARM_R10,
107 PERF_REG_ARM_FP = 11,
108 PERF_REG_ARM_IP = 12,
109 PERF_REG_ARM_SP = 13,
110 PERF_REG_ARM_LR = 14,
111 PERF_REG_ARM_PC = 15,
112 PERF_REG_ARM_MAX,
113 };
114
115 enum ArchType {
116 X86_32,
117 X86_64,
118 ARM,
119 ARM64,
120 UNSUPPORT,
121 };
122
123 // order is IP , SP for ut
124 static const std::map<size_t, const std::string> PERF_REG_NAME_MAP = {
125 #if defined(__x86_64__)
126 {PERF_REG_X86_IP, "PERF_REG_X86_IP"},
127 {PERF_REG_X86_SP, "PERF_REG_X86_SP"},
128 #elif defined(__arm__)
129 {PERF_REG_ARM_PC, "PERF_REG_ARM_PC"},
130 {PERF_REG_ARM_SP, "PERF_REG_ARM_SP"},
131 #elif defined(__aarch64__)
132 {PERF_REG_ARM64_PC, "PERF_REG_ARM64_PC"},
133 {PERF_REG_ARM64_SP, "PERF_REG_ARM64_SP"},
134 #endif
135 };
136
137 // context name
138 static const std::map<uint64_t, const std::string> PERF_CONTEXT_NAME = {
139 {PERF_CONTEXT_HV, "PERF_CONTEXT_HV"},
140 {PERF_CONTEXT_KERNEL, "PERF_CONTEXT_KERNEL"},
141 {PERF_CONTEXT_USER, "PERF_CONTEXT_USER"},
142 {PERF_CONTEXT_GUEST, "PERF_CONTEXT_GUEST"},
143 {PERF_CONTEXT_GUEST_KERNEL, "PERF_CONTEXT_GUEST_KERNEL"},
144 {PERF_CONTEXT_GUEST_USER, "PERF_CONTEXT_GUEST_USER"},
145 {PERF_CONTEXT_MAX, "PERF_CONTEXT_MAX"},
146 };
147
148 #if defined(__x86_64__)
149 constexpr ArchType buildArchType = ArchType::X86_64;
150 #elif defined(__aarch64__)
151 constexpr ArchType buildArchType = ArchType::ARM64;
152 #elif defined(__arm__)
153 constexpr ArchType buildArchType = ArchType::ARM;
154 #else
155 #error NOT SUPPORT ARCH
156 #endif
157
158 const std::string UpdatePerfContext(uint64_t addr, perf_callchain_context &perfCallchainContext);
159 const std::string GetArchName(ArchType arch);
160 uint64_t GetSupportedRegMask(ArchType arch);
161
162 // this is only for debug
163 const std::string RegisterGetName(size_t registerIndex);
164
165 size_t RegisterGetCount();
166
RegisterGetCount()167 inline size_t RegisterGetCount()
168 {
169 switch (buildArchType) {
170 case ArchType::X86_32:
171 return PERF_REG_X86_32_MAX;
172 case ArchType::X86_64:
173 return PERF_REG_X86_64_MAX;
174 case ArchType::ARM:
175 return PERF_REG_ARM_MAX;
176 case ArchType::ARM64:
177 return PERF_REG_ARM64_MAX;
178 default: {
179 return std::numeric_limits<size_t>::max();
180 }
181 }
182 }
183
184 bool RegisterGetValue(uint64_t &value, const u64 registers[], const size_t registerIndex,
185 const size_t registerNumber);
186
187 size_t RegisterGetSP(ArchType arch);
188 size_t RegisterGetIP(ArchType arch);
189
RegisterGetSPValue(uint64_t & value,ArchType arch,const u64 registers[],const size_t registerNumber)190 inline bool RegisterGetSPValue(uint64_t &value, ArchType arch, const u64 registers[],
191 const size_t registerNumber)
192 {
193 return RegisterGetValue(value, registers, RegisterGetSP(arch), registerNumber);
194 }
195
RegisterGetIPValue(uint64_t & value,ArchType arch,const u64 registers[],const size_t registerNumber)196 inline bool RegisterGetIPValue(uint64_t &value, ArchType arch, const u64 registers[],
197 const size_t registerNumber)
198 {
199 return RegisterGetValue(value, registers, RegisterGetIP(arch), registerNumber);
200 }
201
202 int LibunwindRegIdToPerfReg(int regnum);
203
204 ArchType GetDeviceArch();
205 ArchType SetDeviceArch(ArchType arch);
206 ArchType GetArchTypeFromUname(const std::string &machine);
207 ArchType GetArchTypeFromABI(bool abi32);
208 void UpdateRegForABI(ArchType arch, u64 registers[]);
209 } // namespace HiviewDFX
210 } // namespace OHOS
211 #endif // OHOS_HIVIEWDFX_REGISTER_H
212