2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "perf_regs.h"
19 #include <unordered_map>
20 #include <android-base/logging.h>
21 #include <android-base/stringprintf.h>
22 #include <android-base/strings.h>
24 #include "perf_event.h"
26 ArchType ScopedCurrentArch::current_arch = GetBuildArch();
28 ArchType GetArchType(const std::string& arch) {
29 if (arch == "x86" || arch == "i686") {
31 } else if (arch == "x86_64") {
33 } else if (arch == "aarch64") {
35 } else if (android::base::StartsWith(arch, "arm")) {
38 LOG(ERROR) << "unsupported arch: " << arch;
39 return ARCH_UNSUPPORTED;
42 ArchType GetArchForAbi(ArchType machine_arch, int abi) {
43 if (abi == PERF_SAMPLE_REGS_ABI_32) {
44 if (machine_arch == ARCH_X86_64) {
47 if (machine_arch == ARCH_ARM64) {
54 std::string GetArchString(ArchType arch) {
70 // If strict_check, must have arch1 == arch2.
71 // Otherwise, allow X86_32 with X86_64, ARM with ARM64.
72 bool IsArchTheSame(ArchType arch1, ArchType arch2, bool strict_check) {
74 return arch1 == arch2;
79 return arch2 == ARCH_X86_32 || arch2 == ARCH_X86_64;
82 return arch2 == ARCH_ARM64 || arch2 == ARCH_ARM;
86 return arch1 == arch2;
89 uint64_t GetSupportedRegMask(ArchType arch) {
92 return ((1ULL << PERF_REG_X86_32_MAX) - 1);
94 return (((1ULL << PERF_REG_X86_64_MAX) - 1) & ~(1ULL << PERF_REG_X86_DS) &
95 ~(1ULL << PERF_REG_X86_ES) & ~(1ULL << PERF_REG_X86_FS) & ~(1ULL << PERF_REG_X86_GS));
97 return ((1ULL << PERF_REG_ARM_MAX) - 1);
99 return ((1ULL << PERF_REG_ARM64_MAX) - 1);
106 static std::unordered_map<size_t, std::string> x86_reg_map = {
107 {PERF_REG_X86_AX, "ax"}, {PERF_REG_X86_BX, "bx"}, {PERF_REG_X86_CX, "cx"},
108 {PERF_REG_X86_DX, "dx"}, {PERF_REG_X86_SI, "si"}, {PERF_REG_X86_DI, "di"},
109 {PERF_REG_X86_BP, "bp"}, {PERF_REG_X86_SP, "sp"}, {PERF_REG_X86_IP, "ip"},
110 {PERF_REG_X86_FLAGS, "flags"}, {PERF_REG_X86_CS, "cs"}, {PERF_REG_X86_SS, "ss"},
111 {PERF_REG_X86_DS, "ds"}, {PERF_REG_X86_ES, "es"}, {PERF_REG_X86_FS, "fs"},
112 {PERF_REG_X86_GS, "gs"},
115 static std::unordered_map<size_t, std::string> arm_reg_map = {
116 {PERF_REG_ARM_FP, "fp"}, {PERF_REG_ARM_IP, "ip"}, {PERF_REG_ARM_SP, "sp"},
117 {PERF_REG_ARM_LR, "lr"}, {PERF_REG_ARM_PC, "pc"},
120 static std::unordered_map<size_t, std::string> arm64_reg_map = {
121 {PERF_REG_ARM64_LR, "lr"}, {PERF_REG_ARM64_SP, "sp"}, {PERF_REG_ARM64_PC, "pc"},
124 std::string GetRegName(size_t regno, ArchType arch) {
125 // Cast regno to int type to avoid -Werror=type-limits.
126 int reg = static_cast<int>(regno);
129 if (reg >= PERF_REG_X86_R8 && reg <= PERF_REG_X86_R15) {
130 return android::base::StringPrintf("r%d", reg - PERF_REG_X86_R8 + 8);
134 auto it = x86_reg_map.find(reg);
135 CHECK(it != x86_reg_map.end()) << "unknown reg " << reg;
139 if (reg >= PERF_REG_ARM_R0 && reg <= PERF_REG_ARM_R10) {
140 return android::base::StringPrintf("r%d", reg - PERF_REG_ARM_R0);
142 auto it = arm_reg_map.find(reg);
143 CHECK(it != arm_reg_map.end()) << "unknown reg " << reg;
147 if (reg >= PERF_REG_ARM64_X0 && reg <= PERF_REG_ARM64_X29) {
148 return android::base::StringPrintf("r%d", reg - PERF_REG_ARM64_X0);
150 auto it = arm64_reg_map.find(reg);
151 CHECK(it != arm64_reg_map.end()) << "unknown reg " << reg;
159 RegSet CreateRegSet(uint64_t valid_mask, const std::vector<uint64_t>& valid_regs) {
161 regs.valid_mask = valid_mask;
162 for (int i = 0, j = 0; i < 64; ++i) {
163 if ((valid_mask >> i) & 1) {
164 regs.data[i] = valid_regs[j++];
170 bool GetRegValue(const RegSet& regs, size_t regno, uint64_t* value) {
171 CHECK_LT(regno, 64U);
172 if ((regs.valid_mask >> regno) & 1) {
173 *value = regs.data[regno];
179 bool GetSpRegValue(const RegSet& regs, ArchType arch, uint64_t* value) {
183 regno = PERF_REG_X86_SP;
186 regno = PERF_REG_X86_SP;
189 regno = PERF_REG_ARM_SP;
192 regno = PERF_REG_ARM64_SP;
197 return GetRegValue(regs, regno, value);