• 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 #include "debug_logger.h"
21 #if HAVE_LIBUNWIND
22 #include "libunwind.h"
23 #endif
24 namespace OHOS {
25 namespace Developtools {
26 namespace NativeDaemon {
27 // these copy from kerne uapi perf_regs.h
GetSupportedRegMask(ArchType arch)28 uint64_t GetSupportedRegMask(ArchType arch)
29 {
30     uint64_t result = 0;
31     switch (arch) {
32         case ArchType::X86_32:
33             result = ((1ULL << PERF_REG_X86_32_MAX) - 1);
34             break;
35         case ArchType::X86_64:
36             result = ((1ULL << PERF_REG_X86_64_MAX) - 1);
37             result &= ~((1ULL << PERF_REG_X86_DS) | (1ULL << PERF_REG_X86_ES) |
38                         (1ULL << PERF_REG_X86_FS) | (1ULL << PERF_REG_X86_GS));
39             break;
40         case ArchType::ARM:
41             result = ((1ULL << PERF_REG_ARM_MAX) - 1);
42             break;
43         case ArchType::ARM64:
44             result = ((1ULL << PERF_REG_ARM64_MAX) - 1);
45             break;
46         default:
47             result = std::numeric_limits<uint64_t>::max();
48             HLOGE("unsupport arch %d", arch);
49             break;
50     }
51     return result;
52 }
53 
54 #if HAVE_LIBUNWIND
55 const std::map<int, int> LibUnwindRegMap = {
56 #if defined(target_cpu_x64)
57     {UNW_X86_64_RAX, PERF_REG_X86_AX},
58     {UNW_X86_64_RDX, PERF_REG_X86_DX},
59     {UNW_X86_64_RCX, PERF_REG_X86_CX},
60     {UNW_X86_64_RBX, PERF_REG_X86_BX},
61     {UNW_X86_64_RSI, PERF_REG_X86_SI},
62     {UNW_X86_64_RDI, PERF_REG_X86_DI},
63     {UNW_X86_64_RBP, PERF_REG_X86_BP},
64     {UNW_X86_64_RSP, PERF_REG_X86_SP},
65     {UNW_X86_64_R8, PERF_REG_X86_R8},
66     {UNW_X86_64_R9, PERF_REG_X86_R9},
67     {UNW_X86_64_R10, PERF_REG_X86_R10},
68     {UNW_X86_64_R11, PERF_REG_X86_R11},
69     {UNW_X86_64_R12, PERF_REG_X86_R12},
70     {UNW_X86_64_R13, PERF_REG_X86_R13},
71     {UNW_X86_64_R14, PERF_REG_X86_R14},
72     {UNW_X86_64_R15, PERF_REG_X86_R15},
73     {UNW_X86_64_RIP, PERF_REG_X86_IP},
74 #elif defined(target_cpu_arm64)
75     {UNW_AARCH64_X0, PERF_REG_ARM64_X0},
76     {UNW_AARCH64_X1, PERF_REG_ARM64_X1},
77     {UNW_AARCH64_X2, PERF_REG_ARM64_X2},
78     {UNW_AARCH64_X3, PERF_REG_ARM64_X3},
79     {UNW_AARCH64_X4, PERF_REG_ARM64_X4},
80     {UNW_AARCH64_X5, PERF_REG_ARM64_X5},
81     {UNW_AARCH64_X6, PERF_REG_ARM64_X6},
82     {UNW_AARCH64_X7, PERF_REG_ARM64_X7},
83     {UNW_AARCH64_X8, PERF_REG_ARM64_X8},
84     {UNW_AARCH64_X9, PERF_REG_ARM64_X9},
85     {UNW_AARCH64_X10, PERF_REG_ARM64_X10},
86     {UNW_AARCH64_X11, PERF_REG_ARM64_X11},
87     {UNW_AARCH64_X12, PERF_REG_ARM64_X12},
88     {UNW_AARCH64_X13, PERF_REG_ARM64_X13},
89     {UNW_AARCH64_X14, PERF_REG_ARM64_X14},
90     {UNW_AARCH64_X15, PERF_REG_ARM64_X15},
91     {UNW_AARCH64_X16, PERF_REG_ARM64_X16},
92     {UNW_AARCH64_X17, PERF_REG_ARM64_X17},
93     {UNW_AARCH64_X18, PERF_REG_ARM64_X18},
94     {UNW_AARCH64_X19, PERF_REG_ARM64_X19},
95     {UNW_AARCH64_X20, PERF_REG_ARM64_X20},
96     {UNW_AARCH64_X21, PERF_REG_ARM64_X21},
97     {UNW_AARCH64_X22, PERF_REG_ARM64_X22},
98     {UNW_AARCH64_X23, PERF_REG_ARM64_X23},
99     {UNW_AARCH64_X24, PERF_REG_ARM64_X24},
100     {UNW_AARCH64_X25, PERF_REG_ARM64_X25},
101     {UNW_AARCH64_X26, PERF_REG_ARM64_X26},
102     {UNW_AARCH64_X27, PERF_REG_ARM64_X27},
103     {UNW_AARCH64_X28, PERF_REG_ARM64_X28},
104     {UNW_AARCH64_X29, PERF_REG_ARM64_X29},
105     {UNW_AARCH64_X30, PERF_REG_ARM64_LR},
106     {UNW_AARCH64_SP, PERF_REG_ARM64_SP},
107     {UNW_AARCH64_PC, PERF_REG_ARM64_PC},
108 #elif defined(target_cpu_arm)
109     {UNW_ARM_R13, PERF_REG_ARM_SP},
110     {UNW_ARM_R15, PERF_REG_ARM_PC},
111 #else
112 #error not support
113 #endif
114 };
115 
LibunwindRegIdToPrefReg(int libUnwindReg)116 int LibunwindRegIdToPrefReg(int libUnwindReg)
117 {
118     if (LibUnwindRegMap.count(libUnwindReg)) {
119         return LibUnwindRegMap.at(libUnwindReg);
120     } else {
121         HLOGE("unwind: invalid reg id %d", libUnwindReg);
122         return -EINVAL;
123     }
124 }
125 #endif
126 
UpdatePerfContext(uint64_t addr,perf_callchain_context & perfCallchainContext)127 const std::string UpdatePerfContext(uint64_t addr, perf_callchain_context &perfCallchainContext)
128 {
129     if (PERF_CONTEXT_NAME.count(addr) != 0) {
130         perfCallchainContext = static_cast<perf_callchain_context>(addr);
131         return StringPrintf("%s: %" PRIx64 "", PERF_CONTEXT_NAME.at(addr).c_str(), addr);
132     } else {
133         perfCallchainContext = PERF_CONTEXT_MAX;
134         return StringPrintf("unknow context: %" PRIx64 "", addr);
135     }
136 }
137 
138 
RegisterGetName(size_t registerIndex)139 const std::string RegisterGetName(size_t registerIndex)
140 {
141     std::string name;
142     name.append("PerfReg[");
143     name.append(std::to_string(registerIndex));
144     if (PERF_REG_NAME_MAP.count(registerIndex) > 0) {
145         name.append(":");
146         name.append(PERF_REG_NAME_MAP.at(registerIndex));
147     }
148     name.append("]");
149     return name;
150 }
151 } // namespace NativeDaemon
152 } // namespace Developtools
153 } // namespace OHOS