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