OSDN Git Service

7efa01b4f138f670fcf6278312cc1178cc9606ca
[android-x86/system-extras.git] / simpleperf / perf_regs.cpp
1 /*
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
19 #include <unordered_map>
20 #include <base/logging.h>
21 #include <base/stringprintf.h>
22 #include <base/strings.h>
23
24 static ArchType current_arch = GetBuildArch();
25
26 ArchType GetCurrentArch() {
27   return current_arch;
28 }
29
30 bool SetCurrentArch(const std::string& arch) {
31   if (arch == "x86") {
32     current_arch = ARCH_X86_32;
33   } else if (arch == "x86_64") {
34     current_arch = ARCH_X86_64;
35   } else if (arch == "aarch64") {
36     current_arch = ARCH_ARM64;
37   } else if (android::base::StartsWith(arch, "arm")) {
38     current_arch = ARCH_ARM;
39   } else {
40     LOG(ERROR) << "unsupported arch: " << arch;
41     return false;
42   }
43   return true;
44 }
45
46 uint64_t GetSupportedRegMask() {
47   switch (GetCurrentArch()) {
48     case ARCH_X86_32:
49       return ((1ULL << PERF_REG_X86_32_MAX) - 1);
50     case ARCH_X86_64:
51       return (((1ULL << PERF_REG_X86_64_MAX) - 1) & ~(1ULL << PERF_REG_X86_DS) &
52               ~(1ULL << PERF_REG_X86_ES) & ~(1ULL << PERF_REG_X86_FS) & ~(1ULL << PERF_REG_X86_GS));
53     case ARCH_ARM:
54       return ((1ULL << PERF_REG_ARM_MAX) - 1);
55     case ARCH_ARM64:
56       return ((1ULL << PERF_REG_ARM64_MAX) - 1);
57     default:
58       return 0;
59   }
60   return 0;
61 }
62
63 static std::unordered_map<size_t, std::string> x86_reg_map = {
64     {PERF_REG_X86_AX, "ax"},       {PERF_REG_X86_BX, "bx"}, {PERF_REG_X86_CX, "cx"},
65     {PERF_REG_X86_DX, "dx"},       {PERF_REG_X86_SI, "si"}, {PERF_REG_X86_DI, "di"},
66     {PERF_REG_X86_BP, "bp"},       {PERF_REG_X86_SP, "sp"}, {PERF_REG_X86_IP, "ip"},
67     {PERF_REG_X86_FLAGS, "flags"}, {PERF_REG_X86_CS, "cs"}, {PERF_REG_X86_SS, "ss"},
68     {PERF_REG_X86_DS, "ds"},       {PERF_REG_X86_ES, "es"}, {PERF_REG_X86_FS, "fs"},
69     {PERF_REG_X86_GS, "gs"},
70 };
71
72 static std::unordered_map<size_t, std::string> arm_reg_map = {
73     {PERF_REG_ARM_FP, "fp"}, {PERF_REG_ARM_IP, "ip"}, {PERF_REG_ARM_SP, "sp"},
74     {PERF_REG_ARM_LR, "lr"}, {PERF_REG_ARM_PC, "pc"},
75 };
76
77 static std::unordered_map<size_t, std::string> arm64_reg_map = {
78     {PERF_REG_ARM64_LR, "lr"}, {PERF_REG_ARM64_SP, "sp"}, {PERF_REG_ARM64_PC, "pc"},
79 };
80
81 std::string GetRegName(size_t reg) {
82   switch (GetCurrentArch()) {
83     case ARCH_X86_64: {
84       if (reg >= PERF_REG_X86_R8 && reg <= PERF_REG_X86_R15) {
85         return android::base::StringPrintf("r%zu", reg - PERF_REG_X86_R8 + 8);
86       }
87     }  // go through
88     case ARCH_X86_32: {
89       auto it = x86_reg_map.find(reg);
90       CHECK(it != x86_reg_map.end()) << "unknown reg " << reg;
91       return it->second;
92     }
93     case ARCH_ARM: {
94       if (reg >= PERF_REG_ARM_R0 && reg <= PERF_REG_ARM_R10) {
95         return android::base::StringPrintf("r%zu", reg - PERF_REG_ARM_R0);
96       }
97       auto it = arm_reg_map.find(reg);
98       CHECK(it != arm_reg_map.end()) << "unknown reg " << reg;
99       return it->second;
100     }
101     case ARCH_ARM64: {
102       if (reg >= PERF_REG_ARM64_X0 && reg <= PERF_REG_ARM64_X29) {
103         return android::base::StringPrintf("r%zu", reg - PERF_REG_ARM64_X0);
104       }
105       auto it = arm64_reg_map.find(reg);
106       CHECK(it != arm64_reg_map.end()) << "unknown reg " << reg;
107       return it->second;
108     }
109     default:
110       return "unknown";
111   }
112 }
113
114 RegSet CreateRegSet(uint64_t valid_mask, const std::vector<uint64_t>& valid_regs) {
115   RegSet regs;
116   regs.valid_mask = valid_mask;
117   for (int i = 0, j = 0; i < 64; ++i) {
118     if ((valid_mask >> i) & 1) {
119       regs.data[i] = valid_regs[j++];
120     }
121   }
122   return regs;
123 }
124
125 bool GetRegValue(const RegSet& regs, size_t regno, uint64_t* value) {
126   CHECK_LT(regno, 64U);
127   if ((regs.valid_mask >> regno) & 1) {
128     *value = regs.data[regno];
129     return true;
130   }
131   return false;
132 }
133
134 bool GetSpRegValue(const RegSet& regs, uint64_t* value) {
135   size_t regno;
136 #if defined(__i386__)
137   regno = PERF_REG_X86_SP;
138 #elif defined(__x86_64__)
139   regno = PERF_REG_X86_SP;
140 #elif defined(__aarch64__)
141   regno = PERF_REG_ARM64_SP;
142 #elif defined(__arm__)
143   regno = PERF_REG_ARM_SP;
144 #else
145   return false;
146 #endif
147   return GetRegValue(regs, regno, value);
148 }