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