blob: 59db45f61ea69f248ca001faad19b29f84b549d8 [file] [log] [blame]
Yabin Cui76769e52015-07-13 12:23:54 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "perf_regs.h"
18
Yabin Cui6e75e1b2018-02-06 13:42:16 -080019#include <string.h>
20
Elliott Hughes66dd09e2015-12-04 14:00:57 -080021#include <android-base/logging.h>
22#include <android-base/stringprintf.h>
23#include <android-base/strings.h>
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020024#include <unordered_map>
Yabin Cui76769e52015-07-13 12:23:54 -070025
Yabin Cui257d5e62016-03-30 16:21:47 -070026#include "perf_event.h"
27
Yabin Cuifaa7b922021-01-11 17:35:57 -080028namespace simpleperf {
29
Yabin Cui417df292016-11-16 12:26:13 -080030ArchType ScopedCurrentArch::current_arch = ARCH_UNSUPPORTED;
Yabin Cui76769e52015-07-13 12:23:54 -070031
Yabin Cui48460892016-03-18 12:30:31 -070032ArchType GetArchType(const std::string& arch) {
Than McIntosh46546e92015-12-29 13:56:10 -050033 if (arch == "x86" || arch == "i686") {
Yabin Cui48460892016-03-18 12:30:31 -070034 return ARCH_X86_32;
Yabin Cui76769e52015-07-13 12:23:54 -070035 } else if (arch == "x86_64") {
Yabin Cui48460892016-03-18 12:30:31 -070036 return ARCH_X86_64;
Yabin Cui76769e52015-07-13 12:23:54 -070037 } else if (arch == "aarch64") {
Yabin Cui48460892016-03-18 12:30:31 -070038 return ARCH_ARM64;
Yabin Cui76769e52015-07-13 12:23:54 -070039 } else if (android::base::StartsWith(arch, "arm")) {
Yabin Cui417df292016-11-16 12:26:13 -080040 // If arch is "armv8l", it is likely that we are using a 32-bit simpleperf
41 // binary on a aarch64 device. In this case, the profiling environment is
42 // ARCH_ARM64, because the kernel is aarch64.
43 if (arch[3] == 'v') {
44 int version = atoi(&arch[4]);
45 if (version >= 8) {
46 return ARCH_ARM64;
47 }
48 }
Yabin Cui48460892016-03-18 12:30:31 -070049 return ARCH_ARM;
Yabin Cui76769e52015-07-13 12:23:54 -070050 }
Yabin Cui48460892016-03-18 12:30:31 -070051 LOG(ERROR) << "unsupported arch: " << arch;
52 return ARCH_UNSUPPORTED;
Yabin Cui76769e52015-07-13 12:23:54 -070053}
54
Yabin Cui257d5e62016-03-30 16:21:47 -070055ArchType GetArchForAbi(ArchType machine_arch, int abi) {
56 if (abi == PERF_SAMPLE_REGS_ABI_32) {
57 if (machine_arch == ARCH_X86_64) {
58 return ARCH_X86_32;
59 }
60 if (machine_arch == ARCH_ARM64) {
61 return ARCH_ARM;
62 }
Yabin Cuif227b6d2021-01-06 14:01:53 -080063 } else if (abi == PERF_SAMPLE_REGS_ABI_64) {
64 if (machine_arch == ARCH_X86_32) {
65 return ARCH_X86_64;
66 }
67 if (machine_arch == ARCH_ARM) {
68 return ARCH_ARM64;
69 }
Yabin Cui257d5e62016-03-30 16:21:47 -070070 }
71 return machine_arch;
72}
73
74std::string GetArchString(ArchType arch) {
75 switch (arch) {
76 case ARCH_X86_32:
77 return "x86";
78 case ARCH_X86_64:
79 return "x86_64";
80 case ARCH_ARM64:
81 return "arm64";
82 case ARCH_ARM:
83 return "arm";
84 default:
85 break;
86 }
87 return "unknown";
88}
89
Yabin Cui48460892016-03-18 12:30:31 -070090uint64_t GetSupportedRegMask(ArchType arch) {
91 switch (arch) {
Yabin Cui76769e52015-07-13 12:23:54 -070092 case ARCH_X86_32:
Yabin Cui30ae2db2017-06-22 13:02:29 -070093 return ((1ULL << PERF_REG_X86_32_MAX) - 1) & ~(1ULL << PERF_REG_X86_DS) &
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020094 ~(1ULL << PERF_REG_X86_ES) & ~(1ULL << PERF_REG_X86_FS) & ~(1ULL << PERF_REG_X86_GS);
Yabin Cui76769e52015-07-13 12:23:54 -070095 case ARCH_X86_64:
96 return (((1ULL << PERF_REG_X86_64_MAX) - 1) & ~(1ULL << PERF_REG_X86_DS) &
97 ~(1ULL << PERF_REG_X86_ES) & ~(1ULL << PERF_REG_X86_FS) & ~(1ULL << PERF_REG_X86_GS));
98 case ARCH_ARM:
99 return ((1ULL << PERF_REG_ARM_MAX) - 1);
100 case ARCH_ARM64:
101 return ((1ULL << PERF_REG_ARM64_MAX) - 1);
Yabin Cuica7b9e72015-07-13 19:44:24 -0700102 default:
103 return 0;
Yabin Cui76769e52015-07-13 12:23:54 -0700104 }
105 return 0;
106}
107
108static std::unordered_map<size_t, std::string> x86_reg_map = {
109 {PERF_REG_X86_AX, "ax"}, {PERF_REG_X86_BX, "bx"}, {PERF_REG_X86_CX, "cx"},
110 {PERF_REG_X86_DX, "dx"}, {PERF_REG_X86_SI, "si"}, {PERF_REG_X86_DI, "di"},
111 {PERF_REG_X86_BP, "bp"}, {PERF_REG_X86_SP, "sp"}, {PERF_REG_X86_IP, "ip"},
112 {PERF_REG_X86_FLAGS, "flags"}, {PERF_REG_X86_CS, "cs"}, {PERF_REG_X86_SS, "ss"},
113 {PERF_REG_X86_DS, "ds"}, {PERF_REG_X86_ES, "es"}, {PERF_REG_X86_FS, "fs"},
114 {PERF_REG_X86_GS, "gs"},
115};
116
117static std::unordered_map<size_t, std::string> arm_reg_map = {
118 {PERF_REG_ARM_FP, "fp"}, {PERF_REG_ARM_IP, "ip"}, {PERF_REG_ARM_SP, "sp"},
119 {PERF_REG_ARM_LR, "lr"}, {PERF_REG_ARM_PC, "pc"},
120};
121
122static std::unordered_map<size_t, std::string> arm64_reg_map = {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200123 {PERF_REG_ARM64_LR, "lr"},
124 {PERF_REG_ARM64_SP, "sp"},
125 {PERF_REG_ARM64_PC, "pc"},
Yabin Cui76769e52015-07-13 12:23:54 -0700126};
127
Yabin Cui48460892016-03-18 12:30:31 -0700128std::string GetRegName(size_t regno, ArchType arch) {
Yabin Cuiffaa9122016-01-15 15:25:48 -0800129 // Cast regno to int type to avoid -Werror=type-limits.
130 int reg = static_cast<int>(regno);
Yabin Cui48460892016-03-18 12:30:31 -0700131 switch (arch) {
Yabin Cui76769e52015-07-13 12:23:54 -0700132 case ARCH_X86_64: {
133 if (reg >= PERF_REG_X86_R8 && reg <= PERF_REG_X86_R15) {
Yabin Cuiffaa9122016-01-15 15:25:48 -0800134 return android::base::StringPrintf("r%d", reg - PERF_REG_X86_R8 + 8);
Yabin Cui76769e52015-07-13 12:23:54 -0700135 }
George Burgess IVda078442018-08-13 22:41:12 -0700136 FALLTHROUGH_INTENDED;
137 }
Yabin Cui76769e52015-07-13 12:23:54 -0700138 case ARCH_X86_32: {
139 auto it = x86_reg_map.find(reg);
140 CHECK(it != x86_reg_map.end()) << "unknown reg " << reg;
141 return it->second;
142 }
143 case ARCH_ARM: {
144 if (reg >= PERF_REG_ARM_R0 && reg <= PERF_REG_ARM_R10) {
Yabin Cuiffaa9122016-01-15 15:25:48 -0800145 return android::base::StringPrintf("r%d", reg - PERF_REG_ARM_R0);
Yabin Cui76769e52015-07-13 12:23:54 -0700146 }
Yabin Cui078e0c42021-07-13 14:18:06 -0700147 if (auto it = arm_reg_map.find(reg); it != arm_reg_map.end()) {
148 return it->second;
149 }
150 FALLTHROUGH_INTENDED;
Yabin Cui76769e52015-07-13 12:23:54 -0700151 }
152 case ARCH_ARM64: {
153 if (reg >= PERF_REG_ARM64_X0 && reg <= PERF_REG_ARM64_X29) {
Yabin Cuiffaa9122016-01-15 15:25:48 -0800154 return android::base::StringPrintf("r%d", reg - PERF_REG_ARM64_X0);
Yabin Cui76769e52015-07-13 12:23:54 -0700155 }
156 auto it = arm64_reg_map.find(reg);
157 CHECK(it != arm64_reg_map.end()) << "unknown reg " << reg;
158 return it->second;
159 }
Yabin Cui3c8c2132015-08-13 20:30:20 -0700160 default:
Yabin Cuica7b9e72015-07-13 19:44:24 -0700161 return "unknown";
Yabin Cui76769e52015-07-13 12:23:54 -0700162 }
Yabin Cui76769e52015-07-13 12:23:54 -0700163}
Yabin Cui3c8c2132015-08-13 20:30:20 -0700164
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200165RegSet::RegSet(int abi, uint64_t valid_mask, const uint64_t* valid_regs) : valid_mask(valid_mask) {
Yabin Cuif227b6d2021-01-06 14:01:53 -0800166 arch = GetArchForAbi(ScopedCurrentArch::GetCurrentArch(), abi);
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800167 memset(data, 0, sizeof(data));
Yabin Cui3c8c2132015-08-13 20:30:20 -0700168 for (int i = 0, j = 0; i < 64; ++i) {
169 if ((valid_mask >> i) & 1) {
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800170 data[i] = valid_regs[j++];
Yabin Cui3c8c2132015-08-13 20:30:20 -0700171 }
172 }
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800173 if (ScopedCurrentArch::GetCurrentArch() == ARCH_ARM64 && abi == PERF_SAMPLE_REGS_ABI_32) {
174 // The kernel dumps arm64 regs, but we need arm regs. So map arm64 regs into arm regs.
175 data[PERF_REG_ARM_PC] = data[PERF_REG_ARM64_PC];
Yabin Cui417df292016-11-16 12:26:13 -0800176 }
Yabin Cui3c8c2132015-08-13 20:30:20 -0700177}
178
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800179bool RegSet::GetRegValue(size_t regno, uint64_t* value) const {
Yabin Cui3c8c2132015-08-13 20:30:20 -0700180 CHECK_LT(regno, 64U);
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800181 if ((valid_mask >> regno) & 1) {
182 *value = data[regno];
Yabin Cui3c8c2132015-08-13 20:30:20 -0700183 return true;
184 }
185 return false;
186}
187
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800188bool RegSet::GetSpRegValue(uint64_t* value) const {
Yabin Cui3c8c2132015-08-13 20:30:20 -0700189 size_t regno;
Yabin Cui48460892016-03-18 12:30:31 -0700190 switch (arch) {
191 case ARCH_X86_32:
192 regno = PERF_REG_X86_SP;
193 break;
194 case ARCH_X86_64:
195 regno = PERF_REG_X86_SP;
196 break;
197 case ARCH_ARM:
198 regno = PERF_REG_ARM_SP;
199 break;
200 case ARCH_ARM64:
201 regno = PERF_REG_ARM64_SP;
202 break;
203 default:
204 return false;
205 }
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800206 return GetRegValue(regno, value);
Yabin Cui3c8c2132015-08-13 20:30:20 -0700207}
Yabin Cui81a9d332017-12-10 13:09:07 -0800208
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800209bool RegSet::GetIpRegValue(uint64_t* value) const {
Yabin Cui81a9d332017-12-10 13:09:07 -0800210 size_t regno;
211 switch (arch) {
212 case ARCH_X86_64:
213 case ARCH_X86_32:
214 regno = PERF_REG_X86_IP;
215 break;
216 case ARCH_ARM:
217 regno = PERF_REG_ARM_PC;
218 break;
219 case ARCH_ARM64:
220 regno = PERF_REG_ARM64_PC;
221 break;
222 default:
223 return false;
224 }
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800225 return GetRegValue(regno, value);
Yabin Cui81a9d332017-12-10 13:09:07 -0800226}
Yabin Cuifaa7b922021-01-11 17:35:57 -0800227
228} // namespace simpleperf