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 "OfflineUnwinder.h"
18
19 #include <inttypes.h>
20 #include <sys/mman.h>
21
22 #include <unordered_map>
23
24 #include <android-base/logging.h>
25 #include <android-base/parseint.h>
26 #include <unwindstack/MachineArm.h>
27 #include <unwindstack/MachineArm64.h>
28 #include <unwindstack/MachineX86.h>
29 #include <unwindstack/MachineX86_64.h>
30 #include <unwindstack/MachineRiscv64.h>
31 #include <unwindstack/Maps.h>
32 #include <unwindstack/RegsArm.h>
33 #include <unwindstack/RegsArm64.h>
34 #include <unwindstack/RegsX86.h>
35 #include <unwindstack/RegsX86_64.h>
36 #include <unwindstack/RegsRiscv64.h>
37 #include <unwindstack/Unwinder.h>
38 #include <unwindstack/UserArm.h>
39 #include <unwindstack/UserArm64.h>
40 #include <unwindstack/UserX86.h>
41 #include <unwindstack/UserX86_64.h>
42 #include <unwindstack/UserRiscv64.h>
43
44 #include "JITDebugReader.h"
45 #include "OfflineUnwinder_impl.h"
46 #include "environment.h"
47 #include "perf_regs.h"
48 #include "read_apk.h"
49 #include "thread_tree.h"
50
51 namespace simpleperf {
52
53 // unwindstack only builds on linux. So simpleperf redefines flags in unwindstack, to use them on
54 // darwin/windows. Use static_assert to make sure they are on the same page.
55 static_assert(map_flags::PROT_JIT_SYMFILE_MAP == unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP);
56
57 #define CHECK_ERROR_CODE(error_code_name) \
58 static_assert(UnwindStackErrorCode::error_code_name == \
59 (UnwindStackErrorCode)unwindstack::ErrorCode::error_code_name)
60
61 CHECK_ERROR_CODE(ERROR_NONE);
62 CHECK_ERROR_CODE(ERROR_MEMORY_INVALID);
63 CHECK_ERROR_CODE(ERROR_UNWIND_INFO);
64 CHECK_ERROR_CODE(ERROR_UNSUPPORTED);
65 CHECK_ERROR_CODE(ERROR_INVALID_MAP);
66 CHECK_ERROR_CODE(ERROR_MAX_FRAMES_EXCEEDED);
67 CHECK_ERROR_CODE(ERROR_REPEATED_FRAME);
68 CHECK_ERROR_CODE(ERROR_INVALID_ELF);
69 CHECK_ERROR_CODE(ERROR_THREAD_DOES_NOT_EXIST);
70 CHECK_ERROR_CODE(ERROR_THREAD_TIMEOUT);
71 CHECK_ERROR_CODE(ERROR_SYSTEM_CALL);
72 CHECK_ERROR_CODE(ERROR_BAD_ARCH);
73 CHECK_ERROR_CODE(ERROR_MAPS_PARSE);
74 CHECK_ERROR_CODE(ERROR_INVALID_PARAMETER);
75 CHECK_ERROR_CODE(ERROR_MAX);
76
77 // Max frames seen so far is 463, in http://b/110923759.
78 static constexpr size_t MAX_UNWINDING_FRAMES = 512;
79
GetBacktraceRegs(const RegSet & regs)80 unwindstack::Regs* OfflineUnwinderImpl::GetBacktraceRegs(const RegSet& regs) {
81 switch (regs.arch) {
82 case ARCH_ARM: {
83 unwindstack::arm_user_regs arm_user_regs;
84 memset(&arm_user_regs, 0, sizeof(arm_user_regs));
85 static_assert(static_cast<int>(unwindstack::ARM_REG_R0) == static_cast<int>(PERF_REG_ARM_R0),
86 "");
87 static_assert(
88 static_cast<int>(unwindstack::ARM_REG_LAST) == static_cast<int>(PERF_REG_ARM_MAX), "");
89 for (size_t i = unwindstack::ARM_REG_R0; i < unwindstack::ARM_REG_LAST; ++i) {
90 arm_user_regs.regs[i] = static_cast<uint32_t>(regs.data[i]);
91 }
92 return unwindstack::RegsArm::Read(&arm_user_regs);
93 }
94 case ARCH_ARM64: {
95 unwindstack::arm64_user_regs arm64_user_regs;
96 memset(&arm64_user_regs, 0, sizeof(arm64_user_regs));
97 static_assert(
98 static_cast<int>(unwindstack::ARM64_REG_R0) == static_cast<int>(PERF_REG_ARM64_X0), "");
99 static_assert(
100 static_cast<int>(unwindstack::ARM64_REG_R30) == static_cast<int>(PERF_REG_ARM64_LR), "");
101 memcpy(&arm64_user_regs.regs[unwindstack::ARM64_REG_R0], ®s.data[PERF_REG_ARM64_X0],
102 sizeof(uint64_t) * (PERF_REG_ARM64_LR - PERF_REG_ARM64_X0 + 1));
103 arm64_user_regs.sp = regs.data[PERF_REG_ARM64_SP];
104 arm64_user_regs.pc = regs.data[PERF_REG_ARM64_PC];
105 auto regs =
106 static_cast<unwindstack::RegsArm64*>(unwindstack::RegsArm64::Read(&arm64_user_regs));
107 regs->SetPACMask(arm64_pac_mask_);
108 return regs;
109 }
110 case ARCH_X86_32: {
111 unwindstack::x86_user_regs x86_user_regs;
112 memset(&x86_user_regs, 0, sizeof(x86_user_regs));
113 x86_user_regs.eax = static_cast<uint32_t>(regs.data[PERF_REG_X86_AX]);
114 x86_user_regs.ebx = static_cast<uint32_t>(regs.data[PERF_REG_X86_BX]);
115 x86_user_regs.ecx = static_cast<uint32_t>(regs.data[PERF_REG_X86_CX]);
116 x86_user_regs.edx = static_cast<uint32_t>(regs.data[PERF_REG_X86_DX]);
117 x86_user_regs.ebp = static_cast<uint32_t>(regs.data[PERF_REG_X86_BP]);
118 x86_user_regs.edi = static_cast<uint32_t>(regs.data[PERF_REG_X86_DI]);
119 x86_user_regs.esi = static_cast<uint32_t>(regs.data[PERF_REG_X86_SI]);
120 x86_user_regs.esp = static_cast<uint32_t>(regs.data[PERF_REG_X86_SP]);
121 x86_user_regs.eip = static_cast<uint32_t>(regs.data[PERF_REG_X86_IP]);
122 return unwindstack::RegsX86::Read(&x86_user_regs);
123 }
124 case ARCH_X86_64: {
125 unwindstack::x86_64_user_regs x86_64_user_regs;
126 memset(&x86_64_user_regs, 0, sizeof(x86_64_user_regs));
127 x86_64_user_regs.rax = regs.data[PERF_REG_X86_AX];
128 x86_64_user_regs.rbx = regs.data[PERF_REG_X86_BX];
129 x86_64_user_regs.rcx = regs.data[PERF_REG_X86_CX];
130 x86_64_user_regs.rdx = regs.data[PERF_REG_X86_DX];
131 x86_64_user_regs.r8 = regs.data[PERF_REG_X86_R8];
132 x86_64_user_regs.r9 = regs.data[PERF_REG_X86_R9];
133 x86_64_user_regs.r10 = regs.data[PERF_REG_X86_R10];
134 x86_64_user_regs.r11 = regs.data[PERF_REG_X86_R11];
135 x86_64_user_regs.r12 = regs.data[PERF_REG_X86_R12];
136 x86_64_user_regs.r13 = regs.data[PERF_REG_X86_R13];
137 x86_64_user_regs.r14 = regs.data[PERF_REG_X86_R14];
138 x86_64_user_regs.r15 = regs.data[PERF_REG_X86_R15];
139 x86_64_user_regs.rdi = regs.data[PERF_REG_X86_DI];
140 x86_64_user_regs.rsi = regs.data[PERF_REG_X86_SI];
141 x86_64_user_regs.rbp = regs.data[PERF_REG_X86_BP];
142 x86_64_user_regs.rsp = regs.data[PERF_REG_X86_SP];
143 x86_64_user_regs.rip = regs.data[PERF_REG_X86_IP];
144 return unwindstack::RegsX86_64::Read(&x86_64_user_regs);
145 }
146 case ARCH_RISCV64: {
147 unwindstack::riscv64_user_regs riscv64_user_regs;
148 memset(&riscv64_user_regs, 0, sizeof(riscv64_user_regs));
149 riscv64_user_regs.regs[PERF_REG_RISCV_PC] = regs.data[PERF_REG_RISCV_PC];
150 riscv64_user_regs.regs[PERF_REG_RISCV_RA] = regs.data[PERF_REG_RISCV_RA];
151 riscv64_user_regs.regs[PERF_REG_RISCV_SP] = regs.data[PERF_REG_RISCV_SP];
152 riscv64_user_regs.regs[PERF_REG_RISCV_GP] = regs.data[PERF_REG_RISCV_GP];
153 riscv64_user_regs.regs[PERF_REG_RISCV_TP] = regs.data[PERF_REG_RISCV_TP];
154 riscv64_user_regs.regs[PERF_REG_RISCV_T0] = regs.data[PERF_REG_RISCV_T0];
155 riscv64_user_regs.regs[PERF_REG_RISCV_T1] = regs.data[PERF_REG_RISCV_T1];
156 riscv64_user_regs.regs[PERF_REG_RISCV_T2] = regs.data[PERF_REG_RISCV_T2];
157 riscv64_user_regs.regs[PERF_REG_RISCV_S0] = regs.data[PERF_REG_RISCV_S0];
158 riscv64_user_regs.regs[PERF_REG_RISCV_S1] = regs.data[PERF_REG_RISCV_S1];
159 riscv64_user_regs.regs[PERF_REG_RISCV_A0] = regs.data[PERF_REG_RISCV_A0];
160 riscv64_user_regs.regs[PERF_REG_RISCV_A1] = regs.data[PERF_REG_RISCV_A1];
161 riscv64_user_regs.regs[PERF_REG_RISCV_A2] = regs.data[PERF_REG_RISCV_A2];
162 riscv64_user_regs.regs[PERF_REG_RISCV_A3] = regs.data[PERF_REG_RISCV_A3];
163 riscv64_user_regs.regs[PERF_REG_RISCV_A4] = regs.data[PERF_REG_RISCV_A4];
164 riscv64_user_regs.regs[PERF_REG_RISCV_A5] = regs.data[PERF_REG_RISCV_A5];
165 riscv64_user_regs.regs[PERF_REG_RISCV_A6] = regs.data[PERF_REG_RISCV_A6];
166 riscv64_user_regs.regs[PERF_REG_RISCV_A7] = regs.data[PERF_REG_RISCV_A7];
167 riscv64_user_regs.regs[PERF_REG_RISCV_S2] = regs.data[PERF_REG_RISCV_S2];
168 riscv64_user_regs.regs[PERF_REG_RISCV_S3] = regs.data[PERF_REG_RISCV_S3];
169 riscv64_user_regs.regs[PERF_REG_RISCV_S4] = regs.data[PERF_REG_RISCV_S4];
170 riscv64_user_regs.regs[PERF_REG_RISCV_S5] = regs.data[PERF_REG_RISCV_S5];
171 riscv64_user_regs.regs[PERF_REG_RISCV_S6] = regs.data[PERF_REG_RISCV_S6];
172 riscv64_user_regs.regs[PERF_REG_RISCV_S7] = regs.data[PERF_REG_RISCV_S7];
173 riscv64_user_regs.regs[PERF_REG_RISCV_S8] = regs.data[PERF_REG_RISCV_S8];
174 riscv64_user_regs.regs[PERF_REG_RISCV_S9] = regs.data[PERF_REG_RISCV_S9];
175 riscv64_user_regs.regs[PERF_REG_RISCV_S10] = regs.data[PERF_REG_RISCV_S10];
176 riscv64_user_regs.regs[PERF_REG_RISCV_S11] = regs.data[PERF_REG_RISCV_S11];
177 riscv64_user_regs.regs[PERF_REG_RISCV_T3] = regs.data[PERF_REG_RISCV_T3];
178 riscv64_user_regs.regs[PERF_REG_RISCV_T4] = regs.data[PERF_REG_RISCV_T4];
179 riscv64_user_regs.regs[PERF_REG_RISCV_T5] = regs.data[PERF_REG_RISCV_T5];
180 riscv64_user_regs.regs[PERF_REG_RISCV_T6] = regs.data[PERF_REG_RISCV_T6];
181 return unwindstack::RegsRiscv64::Read(&riscv64_user_regs);
182 }
183 default:
184 return nullptr;
185 }
186 }
187
CreateMapInfo(const MapEntry * entry)188 static std::shared_ptr<unwindstack::MapInfo> CreateMapInfo(const MapEntry* entry) {
189 std::string name_holder;
190 const char* name = entry->dso->GetDebugFilePath().data();
191 uint64_t pgoff = entry->pgoff;
192 auto tuple = SplitUrlInApk(entry->dso->GetDebugFilePath());
193 if (std::get<0>(tuple)) {
194 // The unwinder does not understand the ! format, so change back to
195 // the previous format (apk, offset).
196 EmbeddedElf* elf = ApkInspector::FindElfInApkByName(std::get<1>(tuple), std::get<2>(tuple));
197 if (elf != nullptr) {
198 name = elf->filepath().data();
199 pgoff += elf->entry_offset();
200 }
201 } else if (entry->flags & map_flags::PROT_JIT_SYMFILE_MAP) {
202 // Remove location_in_file suffix, which isn't recognized by libunwindstack.
203 const std::string& path = entry->dso->GetDebugFilePath();
204 if (JITDebugReader::IsPathInJITSymFile(path)) {
205 size_t colon_pos = path.rfind(':');
206 CHECK_NE(colon_pos, std::string::npos);
207 name_holder = path.substr(0, colon_pos);
208 name = name_holder.data();
209 }
210 }
211 return unwindstack::MapInfo::Create(entry->start_addr, entry->get_end_addr(), pgoff,
212 PROT_READ | entry->flags, name);
213 }
214
UpdateMaps(const MapSet & map_set)215 void UnwindMaps::UpdateMaps(const MapSet& map_set) {
216 if (version_ == map_set.version) {
217 return;
218 }
219 version_ = map_set.version;
220 size_t i = 0;
221 size_t old_size = entries_.size();
222 bool has_removed_entry = false;
223 for (auto it = map_set.maps.begin(); it != map_set.maps.end();) {
224 const MapEntry* entry = it->second;
225 if (i < old_size && entry == entries_[i]) {
226 i++;
227 ++it;
228 } else if (i == old_size || entry->start_addr <= entries_[i]->start_addr) {
229 // Add an entry.
230 entries_.push_back(entry);
231 maps_.emplace_back(CreateMapInfo(entry));
232 ++it;
233 } else {
234 // Remove an entry.
235 has_removed_entry = true;
236 entries_[i] = nullptr;
237 maps_[i++] = nullptr;
238 }
239 }
240 while (i < old_size) {
241 has_removed_entry = true;
242 entries_[i] = nullptr;
243 maps_[i++] = nullptr;
244 }
245
246 if (has_removed_entry) {
247 entries_.resize(std::remove(entries_.begin(), entries_.end(), nullptr) - entries_.begin());
248 maps_.resize(std::remove(maps_.begin(), maps_.end(), std::shared_ptr<unwindstack::MapInfo>()) -
249 maps_.begin());
250 }
251
252 std::sort(entries_.begin(), entries_.end(),
253 [](const auto& e1, const auto& e2) { return e1->start_addr < e2->start_addr; });
254 // Use Sort() to sort maps_ and create prev_real_map links.
255 // prev_real_map is needed by libunwindstack to find the start of an embedded lib in an apk.
256 // See http://b/120981155.
257 Sort();
258 }
259
CollectMetaInfo(std::unordered_map<std::string,std::string> * info_map)260 void OfflineUnwinder::CollectMetaInfo(std::unordered_map<std::string, std::string>* info_map
261 __attribute__((unused))) {
262 #if defined(__aarch64__)
263 // Find pac_mask for ARMv8.3-A Pointer Authentication by below steps:
264 // 1. Create a 64 bit value with every bit set, but clear bit 55. Because linux user space uses
265 // TTBR0.
266 // 2. Use XPACLRI to clear auth code bits.
267 // 3. Flip every bit to get pac_mask, excluding bit 55.
268 // We can also use ptrace(PTRACE_GETREGSET, pid, NT_ARM_PAC_MASK). But it needs a tracee.
269 register uint64_t x30 __asm("x30") = ~(1ULL << 55);
270 // This is XPACLRI on ARMv8.3-A, and nop on prev ARMv8.3-A.
271 asm("hint 0x7" : "+r"(x30));
272 uint64_t pac_mask = ~x30 & ~(1ULL << 55);
273 if (pac_mask != 0) {
274 (*info_map)[META_KEY_ARM64_PAC_MASK] = android::base::StringPrintf("0x%" PRIx64, pac_mask);
275 }
276 #endif
277 }
278
LoadMetaInfo(const std::unordered_map<std::string,std::string> & info_map)279 void OfflineUnwinderImpl::LoadMetaInfo(
280 const std::unordered_map<std::string, std::string>& info_map) {
281 if (auto it = info_map.find(META_KEY_ARM64_PAC_MASK); it != info_map.end()) {
282 CHECK(android::base::ParseUint(it->second, &arm64_pac_mask_));
283 }
284 }
285
UnwindCallChain(const ThreadEntry & thread,const RegSet & regs,const char * stack,size_t stack_size,std::vector<uint64_t> * ips,std::vector<uint64_t> * sps)286 bool OfflineUnwinderImpl::UnwindCallChain(const ThreadEntry& thread, const RegSet& regs,
287 const char* stack, size_t stack_size,
288 std::vector<uint64_t>* ips, std::vector<uint64_t>* sps) {
289 uint64_t start_time;
290 if (collect_stat_) {
291 start_time = GetSystemClock();
292 }
293 is_callchain_broken_for_incomplete_jit_debug_info_ = false;
294 ips->clear();
295 sps->clear();
296 std::vector<uint64_t> result;
297 uint64_t sp_reg_value;
298 if (!regs.GetSpRegValue(&sp_reg_value)) {
299 LOG(ERROR) << "can't get sp reg value";
300 return false;
301 }
302 uint64_t stack_addr = sp_reg_value;
303
304 UnwindMaps& cached_map = cached_maps_[thread.pid];
305 cached_map.UpdateMaps(*thread.maps);
306 std::unique_ptr<unwindstack::Regs> unwind_regs(GetBacktraceRegs(regs));
307 if (!unwind_regs) {
308 return false;
309 }
310 unwindstack::Unwinder unwinder(
311 MAX_UNWINDING_FRAMES, &cached_map, unwind_regs.get(),
312 unwindstack::Memory::CreateOfflineMemory(reinterpret_cast<const uint8_t*>(stack), stack_addr,
313 stack_addr + stack_size));
314 unwinder.SetResolveNames(false);
315 unwinder.Unwind();
316 size_t last_jit_method_frame = UINT_MAX;
317 for (auto& frame : unwinder.frames()) {
318 // Unwinding in arm architecture can return 0 pc address.
319
320 // If frame.map_info == nullptr, this frame doesn't hit any map, it could be:
321 // 1. In an executable map not backed by a file. Note that RecordCommand::ShouldOmitRecord()
322 // may omit maps only exist memory.
323 // 2. An incorrectly unwound frame. Likely caused by invalid stack data, as in
324 // SampleRecord::GetValidStackSize(). Or caused by incomplete JIT debug info.
325 // We want to remove this frame and callchains following it in either case.
326 if (frame.map_info == nullptr) {
327 is_callchain_broken_for_incomplete_jit_debug_info_ = true;
328 break;
329 }
330 if (frame.map_info->flags() & unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP) {
331 last_jit_method_frame = ips->size();
332 }
333 ips->push_back(frame.pc);
334 sps->push_back(frame.sp);
335 }
336 // If the unwound frames stop near to a JITed method, it may be caused by incomplete JIT debug
337 // info.
338 if (last_jit_method_frame != UINT_MAX && last_jit_method_frame + 3 > ips->size()) {
339 is_callchain_broken_for_incomplete_jit_debug_info_ = true;
340 }
341
342 uint64_t ip_reg_value;
343 if (!regs.GetIpRegValue(&ip_reg_value)) {
344 LOG(ERROR) << "can't get ip reg value";
345 return false;
346 }
347 if (ips->empty()) {
348 ips->push_back(ip_reg_value);
349 sps->push_back(sp_reg_value);
350 } else {
351 // Check if the unwinder returns ip reg value as the first ip address in callstack.
352 CHECK_EQ((*ips)[0], ip_reg_value);
353 }
354 if (collect_stat_) {
355 unwinding_result_.used_time = GetSystemClock() - start_time;
356 unwinding_result_.error_code = unwinder.LastErrorCode();
357 unwinding_result_.error_addr = unwinder.LastErrorAddress();
358 unwinding_result_.stack_start = stack_addr;
359 unwinding_result_.stack_end = stack_addr + stack_size;
360 }
361 return true;
362 }
363
Create(bool collect_stat)364 std::unique_ptr<OfflineUnwinder> OfflineUnwinder::Create(bool collect_stat) {
365 return std::unique_ptr<OfflineUnwinder>(new OfflineUnwinderImpl(collect_stat));
366 }
367
368 } // namespace simpleperf
369