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
16 // for libunwind.h empty struct has size 0 in c, size 1 in c++
17 #define UNW_EMPTY_STRUCT uint8_t unused;
18 #include "register.h"
19 #if !is_mingw
20 #include <sys/utsname.h>
21 #endif
22 #ifdef HAVE_LIBUNWIND
23 #include <libunwind.h>
24 #endif
25 #include "callstack_log.h"
26 #include "string_util.h"
27
28 namespace OHOS {
29 namespace HiviewDFX {
30 static ArchType deviceArchType = ArchType::UNSUPPORT;
31
32 // these copy from kerne uapi perf_regs.h
GetSupportedRegMask(ArchType arch)33 uint64_t GetSupportedRegMask(ArchType arch)
34 {
35 uint64_t result = 0;
36 switch (arch) {
37 case ArchType::X86_32:
38 result = ((1ULL << PERF_REG_X86_32_MAX) - 1);
39 break;
40 case ArchType::X86_64:
41 result = ((1ULL << PERF_REG_X86_64_MAX) - 1);
42 result &= ~((1ULL << PERF_REG_X86_DS) | (1ULL << PERF_REG_X86_ES) |
43 (1ULL << PERF_REG_X86_FS) | (1ULL << PERF_REG_X86_GS));
44 break;
45 case ArchType::ARM:
46 result = ((1ULL << PERF_REG_ARM_MAX) - 1);
47 break;
48 case ArchType::ARM64:
49 result = ((1ULL << PERF_REG_ARM64_MAX) - 1);
50 break;
51 default:
52 result = std::numeric_limits<uint64_t>::max();
53 DFXLOG_ERROR("unsupport arch %d", arch);
54 break;
55 }
56 return result;
57 }
58
59 #ifdef HAVE_LIBUNWIND
60 const std::map<int, int> LibUnwindRegMap = {
61 #if defined(__x86_64__)
62 {UNW_X86_64_RAX, PERF_REG_X86_AX}, {UNW_X86_64_RDX, PERF_REG_X86_DX},
63 {UNW_X86_64_RCX, PERF_REG_X86_CX}, {UNW_X86_64_RBX, PERF_REG_X86_BX},
64 {UNW_X86_64_RSI, PERF_REG_X86_SI}, {UNW_X86_64_RDI, PERF_REG_X86_DI},
65 {UNW_X86_64_RBP, PERF_REG_X86_BP}, {UNW_X86_64_RSP, PERF_REG_X86_SP},
66 {UNW_X86_64_R8, PERF_REG_X86_R8}, {UNW_X86_64_R9, PERF_REG_X86_R9},
67 {UNW_X86_64_R10, PERF_REG_X86_R10}, {UNW_X86_64_R11, PERF_REG_X86_R11},
68 {UNW_X86_64_R12, PERF_REG_X86_R12}, {UNW_X86_64_R13, PERF_REG_X86_R13},
69 {UNW_X86_64_R14, PERF_REG_X86_R14}, {UNW_X86_64_R15, PERF_REG_X86_R15},
70 {UNW_X86_64_RIP, PERF_REG_X86_IP},
71 #elif defined(__aarch64__)
72 {UNW_AARCH64_X0, PERF_REG_ARM64_X0}, {UNW_AARCH64_X1, PERF_REG_ARM64_X1},
73 {UNW_AARCH64_X2, PERF_REG_ARM64_X2}, {UNW_AARCH64_X3, PERF_REG_ARM64_X3},
74 {UNW_AARCH64_X4, PERF_REG_ARM64_X4}, {UNW_AARCH64_X5, PERF_REG_ARM64_X5},
75 {UNW_AARCH64_X6, PERF_REG_ARM64_X6}, {UNW_AARCH64_X7, PERF_REG_ARM64_X7},
76 {UNW_AARCH64_X8, PERF_REG_ARM64_X8}, {UNW_AARCH64_X9, PERF_REG_ARM64_X9},
77 {UNW_AARCH64_X10, PERF_REG_ARM64_X10}, {UNW_AARCH64_X11, PERF_REG_ARM64_X11},
78 {UNW_AARCH64_X12, PERF_REG_ARM64_X12}, {UNW_AARCH64_X13, PERF_REG_ARM64_X13},
79 {UNW_AARCH64_X14, PERF_REG_ARM64_X14}, {UNW_AARCH64_X15, PERF_REG_ARM64_X15},
80 {UNW_AARCH64_X16, PERF_REG_ARM64_X16}, {UNW_AARCH64_X17, PERF_REG_ARM64_X17},
81 {UNW_AARCH64_X18, PERF_REG_ARM64_X18}, {UNW_AARCH64_X19, PERF_REG_ARM64_X19},
82 {UNW_AARCH64_X20, PERF_REG_ARM64_X20}, {UNW_AARCH64_X21, PERF_REG_ARM64_X21},
83 {UNW_AARCH64_X22, PERF_REG_ARM64_X22}, {UNW_AARCH64_X23, PERF_REG_ARM64_X23},
84 {UNW_AARCH64_X24, PERF_REG_ARM64_X24}, {UNW_AARCH64_X25, PERF_REG_ARM64_X25},
85 {UNW_AARCH64_X26, PERF_REG_ARM64_X26}, {UNW_AARCH64_X27, PERF_REG_ARM64_X27},
86 {UNW_AARCH64_X28, PERF_REG_ARM64_X28}, {UNW_AARCH64_X29, PERF_REG_ARM64_X29},
87 {UNW_AARCH64_X30, PERF_REG_ARM64_LR}, {UNW_AARCH64_SP, PERF_REG_ARM64_SP},
88 {UNW_AARCH64_PC, PERF_REG_ARM64_PC},
89 #elif defined(__arm__)
90 {UNW_ARM_R0, PERF_REG_ARM_R0}, {UNW_ARM_R1, PERF_REG_ARM_R1}, {UNW_ARM_R2, PERF_REG_ARM_R2},
91 {UNW_ARM_R3, PERF_REG_ARM_R3}, {UNW_ARM_R4, PERF_REG_ARM_R4}, {UNW_ARM_R5, PERF_REG_ARM_R5},
92 {UNW_ARM_R6, PERF_REG_ARM_R6}, {UNW_ARM_R7, PERF_REG_ARM_R7}, {UNW_ARM_R8, PERF_REG_ARM_R8},
93 {UNW_ARM_R9, PERF_REG_ARM_R9}, {UNW_ARM_R10, PERF_REG_ARM_R10}, {UNW_ARM_R11, PERF_REG_ARM_FP},
94 {UNW_ARM_R12, PERF_REG_ARM_IP}, {UNW_ARM_R13, PERF_REG_ARM_SP}, {UNW_ARM_R14, PERF_REG_ARM_LR},
95 {UNW_ARM_R15, PERF_REG_ARM_PC},
96 #else
97 #error not support
98 #endif
99 };
100
LibunwindRegIdToPerfReg(int libUnwindReg)101 int LibunwindRegIdToPerfReg(int libUnwindReg)
102 {
103 if (LibUnwindRegMap.count(libUnwindReg)) {
104 return LibUnwindRegMap.at(libUnwindReg);
105 } else {
106 DFXLOG_ERROR("unwind: invalid reg id %d", libUnwindReg);
107 return -EINVAL;
108 }
109 }
110 #endif
111
UpdatePerfContext(uint64_t addr,perf_callchain_context & perfCallchainContext)112 const std::string UpdatePerfContext(uint64_t addr, perf_callchain_context &perfCallchainContext)
113 {
114 if (PERF_CONTEXT_NAME.count(addr) != 0) {
115 perfCallchainContext = static_cast<perf_callchain_context>(addr);
116 return StringPrintf("%s: %" PRIx64 "", PERF_CONTEXT_NAME.at(addr).c_str(), addr);
117 } else {
118 perfCallchainContext = PERF_CONTEXT_MAX;
119 return StringPrintf("unknow context: %" PRIx64 "", addr);
120 }
121 }
122
GetArchName(ArchType arch)123 const std::string GetArchName(ArchType arch)
124 {
125 switch (arch) {
126 case ArchType::X86_32:
127 return "X86_32";
128 case ArchType::X86_64:
129 return "X86_64";
130 case ArchType::ARM:
131 return "ARM";
132 case ArchType::ARM64:
133 return "ARM64";
134 default:
135 return "Unsupport";
136 }
137 }
138
RegisterGetIP(ArchType arch)139 size_t RegisterGetIP(ArchType arch)
140 {
141 switch (arch) {
142 case ArchType::X86_32:
143 case ArchType::X86_64:
144 return PERF_REG_X86_IP;
145 case ArchType::ARM:
146 return PERF_REG_ARM_PC;
147 case ArchType::ARM64:
148 return PERF_REG_ARM64_PC;
149 default:
150 return std::numeric_limits<size_t>::max();
151 }
152 }
153
RegisterGetSP(ArchType arch)154 size_t RegisterGetSP(ArchType arch)
155 {
156 switch (arch) {
157 case ArchType::X86_32:
158 case ArchType::X86_64:
159 return PERF_REG_X86_SP;
160 case ArchType::ARM:
161 return PERF_REG_ARM_SP;
162 case ArchType::ARM64:
163 return PERF_REG_ARM64_SP;
164 default:
165 return std::numeric_limits<size_t>::max();
166 }
167 }
168
RegisterGetName(size_t registerIndex)169 const std::string RegisterGetName(size_t registerIndex)
170 {
171 std::string name;
172 name.append("PerfReg[");
173 name.append(std::to_string(registerIndex));
174 if (PERF_REG_NAME_MAP.count(registerIndex) > 0) {
175 name.append(":");
176 name.append(PERF_REG_NAME_MAP.at(registerIndex));
177 }
178 name.append("]");
179 return name;
180 }
181
RegisterGetValue(uint64_t & value,const u64 registers[],const size_t registerIndex,const size_t registerNumber)182 bool RegisterGetValue(uint64_t &value, const u64 registers[], const size_t registerIndex,
183 const size_t registerNumber)
184 {
185 if (registerIndex >= registerNumber) {
186 DFXLOG_ERROR("registerIndex is %zu, max is %zu", registerIndex, registerNumber);
187 return false;
188 }
189 value = registers[registerIndex];
190 return true;
191 }
192
GetArchTypeFromUname(const std::string & machine)193 ArchType GetArchTypeFromUname(const std::string &machine)
194 {
195 if (StartsWith(machine, "arm")) {
196 if (machine == "armv8l") {
197 // 32 bit elf run in 64 bit cpu
198 return ArchType::ARM64;
199 }
200 return ArchType::ARM;
201 } else if (machine == "aarch64") {
202 return ArchType::ARM64;
203 } else if (machine == "x86_64") {
204 return ArchType::X86_64;
205 } else if (machine == "x86" || machine == "i686") {
206 return ArchType::X86_32;
207 } else {
208 DFXLOG_ERROR("unsupport machine %s", machine.c_str());
209 return ArchType::UNSUPPORT;
210 }
211 }
212
GetArchTypeFromABI(bool abi32)213 ArchType GetArchTypeFromABI(bool abi32)
214 {
215 if (deviceArchType == ArchType::UNSUPPORT) {
216 deviceArchType = GetDeviceArch();
217 }
218 if (abi32) {
219 if (deviceArchType == ArchType::ARM64) {
220 return ArchType::ARM;
221 } else if (deviceArchType == ArchType::X86_64) {
222 return ArchType::X86_32;
223 }
224 }
225 return deviceArchType;
226 }
227
SetDeviceArch(ArchType arch)228 ArchType SetDeviceArch(ArchType arch)
229 {
230 DFXLOG_DEBUG("deviceArchType change to %s", GetArchName(arch).c_str());
231 deviceArchType = arch;
232 return deviceArchType;
233 }
234
GetDeviceArch()235 ArchType GetDeviceArch()
236 {
237 #if is_mingw
238 return deviceArchType;
239 #else
240 if (deviceArchType != ArchType::UNSUPPORT) {
241 return deviceArchType;
242 } else {
243 utsname systemName;
244 if ((uname(&systemName)) != 0) {
245 // fallback
246 deviceArchType = buildArchType;
247 } else {
248 deviceArchType = GetArchTypeFromUname(systemName.machine);
249 DFXLOG_DEBUG("machine arch is %s : %s", systemName.machine, GetArchName(deviceArchType).c_str());
250 if (deviceArchType == ArchType::UNSUPPORT) {
251 deviceArchType = buildArchType;
252 }
253 }
254 }
255 return deviceArchType;
256 #endif
257 }
258
UpdateRegForABI(ArchType arch,u64 * regs)259 void UpdateRegForABI(ArchType arch, u64 *regs)
260 {
261 if (deviceArchType == ArchType::ARM64 and arch == ArchType::ARM) {
262 // arm in arm64
263 regs[PERF_REG_ARM_PC] = regs[PERF_REG_ARM64_PC];
264 }
265 }
266 } // namespace HiviewDFX
267 } // namespace OHOS
268