blob: bbc488b41c275fe9ce7fc607b76754dd3660ea10 [file] [log] [blame]
Yabin Cui3c8c2132015-08-13 20:30:20 -07001/*
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
Yabin Cui03381452017-12-13 11:31:53 -080017#include "OfflineUnwinder.h"
Yabin Cui3c8c2132015-08-13 20:30:20 -070018
Yabin Cui91784fd2020-01-29 17:08:49 -080019#include <inttypes.h>
Yabin Cuia6a04202018-07-23 15:32:47 -070020#include <sys/mman.h>
21
Yabin Cui5ac7a252019-07-18 10:43:32 -070022#include <unordered_map>
23
Elliott Hughes66dd09e2015-12-04 14:00:57 -080024#include <android-base/logging.h>
Yabin Cui91784fd2020-01-29 17:08:49 -080025#include <android-base/parseint.h>
Yabin Cui6e75e1b2018-02-06 13:42:16 -080026#include <unwindstack/MachineArm.h>
27#include <unwindstack/MachineArm64.h>
28#include <unwindstack/MachineX86.h>
29#include <unwindstack/MachineX86_64.h>
Yabin Cuicdc11a32018-03-20 15:29:03 -070030#include <unwindstack/Maps.h>
Yabin Cui6e75e1b2018-02-06 13:42:16 -080031#include <unwindstack/RegsArm.h>
32#include <unwindstack/RegsArm64.h>
33#include <unwindstack/RegsX86.h>
34#include <unwindstack/RegsX86_64.h>
Yabin Cuia6a04202018-07-23 15:32:47 -070035#include <unwindstack/Unwinder.h>
Yabin Cui6e75e1b2018-02-06 13:42:16 -080036#include <unwindstack/UserArm.h>
37#include <unwindstack/UserArm64.h>
38#include <unwindstack/UserX86.h>
39#include <unwindstack/UserX86_64.h>
Yabin Cui3c8c2132015-08-13 20:30:20 -070040
Yabin Cui9ba4d942020-09-08 16:12:46 -070041#include "JITDebugReader.h"
Yabin Cui39e59792020-01-23 12:32:26 -080042#include "OfflineUnwinder_impl.h"
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020043#include "environment.h"
Yabin Cui6e75e1b2018-02-06 13:42:16 -080044#include "perf_regs.h"
Christopher Ferris11017682017-12-14 15:53:37 -080045#include "read_apk.h"
Yabin Cui3c8c2132015-08-13 20:30:20 -070046#include "thread_tree.h"
47
Yabin Cui03381452017-12-13 11:31:53 -080048namespace simpleperf {
49
Yabin Cuie0a19802021-03-16 17:24:11 -070050// unwindstack only builds on linux. So simpleperf redefines flags in unwindstack, to use them on
51// darwin/windows. Use static_assert to make sure they are on the same page.
52static_assert(map_flags::PROT_JIT_SYMFILE_MAP == unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP);
53
Liz Kammer60ef88b2021-07-21 09:29:38 -040054#define CHECK_ERROR_CODE(error_code_name) \
55 static_assert(UnwindStackErrorCode::error_code_name == \
56 (UnwindStackErrorCode)unwindstack::ErrorCode::error_code_name)
Yabin Cuie0a19802021-03-16 17:24:11 -070057
58CHECK_ERROR_CODE(ERROR_NONE);
59CHECK_ERROR_CODE(ERROR_MEMORY_INVALID);
60CHECK_ERROR_CODE(ERROR_UNWIND_INFO);
61CHECK_ERROR_CODE(ERROR_UNSUPPORTED);
62CHECK_ERROR_CODE(ERROR_INVALID_MAP);
63CHECK_ERROR_CODE(ERROR_MAX_FRAMES_EXCEEDED);
64CHECK_ERROR_CODE(ERROR_REPEATED_FRAME);
65CHECK_ERROR_CODE(ERROR_INVALID_ELF);
66CHECK_ERROR_CODE(ERROR_THREAD_DOES_NOT_EXIST);
67CHECK_ERROR_CODE(ERROR_THREAD_TIMEOUT);
68CHECK_ERROR_CODE(ERROR_SYSTEM_CALL);
Christopher Ferrise617e492022-04-19 20:06:25 -070069CHECK_ERROR_CODE(ERROR_BAD_ARCH);
70CHECK_ERROR_CODE(ERROR_MAPS_PARSE);
71CHECK_ERROR_CODE(ERROR_INVALID_PARAMETER);
Yabin Cuie0a19802021-03-16 17:24:11 -070072CHECK_ERROR_CODE(ERROR_MAX);
73
Yabin Cuia6a04202018-07-23 15:32:47 -070074// Max frames seen so far is 463, in http://b/110923759.
75static constexpr size_t MAX_UNWINDING_FRAMES = 512;
76
Yabin Cui91784fd2020-01-29 17:08:49 -080077unwindstack::Regs* OfflineUnwinderImpl::GetBacktraceRegs(const RegSet& regs) {
Yabin Cui6e75e1b2018-02-06 13:42:16 -080078 switch (regs.arch) {
79 case ARCH_ARM: {
80 unwindstack::arm_user_regs arm_user_regs;
81 memset(&arm_user_regs, 0, sizeof(arm_user_regs));
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020082 static_assert(static_cast<int>(unwindstack::ARM_REG_R0) == static_cast<int>(PERF_REG_ARM_R0),
83 "");
Yabin Cui6e75e1b2018-02-06 13:42:16 -080084 static_assert(
85 static_cast<int>(unwindstack::ARM_REG_LAST) == static_cast<int>(PERF_REG_ARM_MAX), "");
86 for (size_t i = unwindstack::ARM_REG_R0; i < unwindstack::ARM_REG_LAST; ++i) {
87 arm_user_regs.regs[i] = static_cast<uint32_t>(regs.data[i]);
88 }
89 return unwindstack::RegsArm::Read(&arm_user_regs);
90 }
91 case ARCH_ARM64: {
92 unwindstack::arm64_user_regs arm64_user_regs;
93 memset(&arm64_user_regs, 0, sizeof(arm64_user_regs));
94 static_assert(
95 static_cast<int>(unwindstack::ARM64_REG_R0) == static_cast<int>(PERF_REG_ARM64_X0), "");
96 static_assert(
97 static_cast<int>(unwindstack::ARM64_REG_R30) == static_cast<int>(PERF_REG_ARM64_LR), "");
98 memcpy(&arm64_user_regs.regs[unwindstack::ARM64_REG_R0], &regs.data[PERF_REG_ARM64_X0],
99 sizeof(uint64_t) * (PERF_REG_ARM64_LR - PERF_REG_ARM64_X0 + 1));
100 arm64_user_regs.sp = regs.data[PERF_REG_ARM64_SP];
101 arm64_user_regs.pc = regs.data[PERF_REG_ARM64_PC];
Yabin Cui91784fd2020-01-29 17:08:49 -0800102 auto regs =
103 static_cast<unwindstack::RegsArm64*>(unwindstack::RegsArm64::Read(&arm64_user_regs));
104 regs->SetPACMask(arm64_pac_mask_);
105 return regs;
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800106 }
107 case ARCH_X86_32: {
108 unwindstack::x86_user_regs x86_user_regs;
109 memset(&x86_user_regs, 0, sizeof(x86_user_regs));
110 x86_user_regs.eax = static_cast<uint32_t>(regs.data[PERF_REG_X86_AX]);
111 x86_user_regs.ebx = static_cast<uint32_t>(regs.data[PERF_REG_X86_BX]);
112 x86_user_regs.ecx = static_cast<uint32_t>(regs.data[PERF_REG_X86_CX]);
113 x86_user_regs.edx = static_cast<uint32_t>(regs.data[PERF_REG_X86_DX]);
114 x86_user_regs.ebp = static_cast<uint32_t>(regs.data[PERF_REG_X86_BP]);
115 x86_user_regs.edi = static_cast<uint32_t>(regs.data[PERF_REG_X86_DI]);
116 x86_user_regs.esi = static_cast<uint32_t>(regs.data[PERF_REG_X86_SI]);
117 x86_user_regs.esp = static_cast<uint32_t>(regs.data[PERF_REG_X86_SP]);
118 x86_user_regs.eip = static_cast<uint32_t>(regs.data[PERF_REG_X86_IP]);
119 return unwindstack::RegsX86::Read(&x86_user_regs);
120 }
121 case ARCH_X86_64: {
122 unwindstack::x86_64_user_regs x86_64_user_regs;
123 memset(&x86_64_user_regs, 0, sizeof(x86_64_user_regs));
124 x86_64_user_regs.rax = regs.data[PERF_REG_X86_AX];
125 x86_64_user_regs.rbx = regs.data[PERF_REG_X86_BX];
126 x86_64_user_regs.rcx = regs.data[PERF_REG_X86_CX];
127 x86_64_user_regs.rdx = regs.data[PERF_REG_X86_DX];
128 x86_64_user_regs.r8 = regs.data[PERF_REG_X86_R8];
129 x86_64_user_regs.r9 = regs.data[PERF_REG_X86_R9];
130 x86_64_user_regs.r10 = regs.data[PERF_REG_X86_R10];
131 x86_64_user_regs.r11 = regs.data[PERF_REG_X86_R11];
132 x86_64_user_regs.r12 = regs.data[PERF_REG_X86_R12];
133 x86_64_user_regs.r13 = regs.data[PERF_REG_X86_R13];
134 x86_64_user_regs.r14 = regs.data[PERF_REG_X86_R14];
135 x86_64_user_regs.r15 = regs.data[PERF_REG_X86_R15];
136 x86_64_user_regs.rdi = regs.data[PERF_REG_X86_DI];
137 x86_64_user_regs.rsi = regs.data[PERF_REG_X86_SI];
138 x86_64_user_regs.rbp = regs.data[PERF_REG_X86_BP];
139 x86_64_user_regs.rsp = regs.data[PERF_REG_X86_SP];
140 x86_64_user_regs.rip = regs.data[PERF_REG_X86_IP];
141 return unwindstack::RegsX86_64::Read(&x86_64_user_regs);
142 }
143 default:
144 return nullptr;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700145 }
Yabin Cui3c8c2132015-08-13 20:30:20 -0700146}
147
Christopher Ferris260250e2021-01-15 15:59:06 -0800148static std::shared_ptr<unwindstack::MapInfo> CreateMapInfo(const MapEntry* entry) {
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700149 std::string name_holder;
150 const char* name = entry->dso->GetDebugFilePath().data();
Yabin Cuia6a04202018-07-23 15:32:47 -0700151 uint64_t pgoff = entry->pgoff;
Yabin Cuia1cfa932019-03-01 19:03:41 -0800152 auto tuple = SplitUrlInApk(entry->dso->GetDebugFilePath());
153 if (std::get<0>(tuple)) {
154 // The unwinder does not understand the ! format, so change back to
155 // the previous format (apk, offset).
156 EmbeddedElf* elf = ApkInspector::FindElfInApkByName(std::get<1>(tuple), std::get<2>(tuple));
157 if (elf != nullptr) {
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700158 name = elf->filepath().data();
Yabin Cuia1cfa932019-03-01 19:03:41 -0800159 pgoff += elf->entry_offset();
Yabin Cuia6a04202018-07-23 15:32:47 -0700160 }
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700161 } else if (entry->flags & map_flags::PROT_JIT_SYMFILE_MAP) {
162 // Remove location_in_file suffix, which isn't recognized by libunwindstack.
Yabin Cui9ba4d942020-09-08 16:12:46 -0700163 const std::string& path = entry->dso->GetDebugFilePath();
164 if (JITDebugReader::IsPathInJITSymFile(path)) {
165 size_t colon_pos = path.rfind(':');
166 CHECK_NE(colon_pos, std::string::npos);
167 name_holder = path.substr(0, colon_pos);
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700168 name = name_holder.data();
169 }
Yabin Cuia6a04202018-07-23 15:32:47 -0700170 }
Christopher Ferris260250e2021-01-15 15:59:06 -0800171 return unwindstack::MapInfo::Create(entry->start_addr, entry->get_end_addr(), pgoff,
172 PROT_READ | entry->flags, name);
Yabin Cuia6a04202018-07-23 15:32:47 -0700173}
174
175void UnwindMaps::UpdateMaps(const MapSet& map_set) {
176 if (version_ == map_set.version) {
177 return;
178 }
179 version_ = map_set.version;
180 size_t i = 0;
181 size_t old_size = entries_.size();
Yabin Cui39e59792020-01-23 12:32:26 -0800182 bool has_removed_entry = false;
Yabin Cuia6a04202018-07-23 15:32:47 -0700183 for (auto it = map_set.maps.begin(); it != map_set.maps.end();) {
184 const MapEntry* entry = it->second;
185 if (i < old_size && entry == entries_[i]) {
186 i++;
187 ++it;
188 } else if (i == old_size || entry->start_addr <= entries_[i]->start_addr) {
189 // Add an entry.
190 entries_.push_back(entry);
Florian Mayer50818b72019-02-27 19:35:01 +0000191 maps_.emplace_back(CreateMapInfo(entry));
Yabin Cuia6a04202018-07-23 15:32:47 -0700192 ++it;
193 } else {
194 // Remove an entry.
Yabin Cui39e59792020-01-23 12:32:26 -0800195 has_removed_entry = true;
Yabin Cuia6a04202018-07-23 15:32:47 -0700196 entries_[i] = nullptr;
Yabin Cuia6a04202018-07-23 15:32:47 -0700197 maps_[i++] = nullptr;
198 }
199 }
200 while (i < old_size) {
Yabin Cui39e59792020-01-23 12:32:26 -0800201 has_removed_entry = true;
Yabin Cuia6a04202018-07-23 15:32:47 -0700202 entries_[i] = nullptr;
Yabin Cuia6a04202018-07-23 15:32:47 -0700203 maps_[i++] = nullptr;
204 }
Yabin Cui39e59792020-01-23 12:32:26 -0800205
206 if (has_removed_entry) {
207 entries_.resize(std::remove(entries_.begin(), entries_.end(), nullptr) - entries_.begin());
Christopher Ferris260250e2021-01-15 15:59:06 -0800208 maps_.resize(std::remove(maps_.begin(), maps_.end(), std::shared_ptr<unwindstack::MapInfo>()) -
Yabin Cui39e59792020-01-23 12:32:26 -0800209 maps_.begin());
210 }
211
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200212 std::sort(entries_.begin(), entries_.end(),
213 [](const auto& e1, const auto& e2) { return e1->start_addr < e2->start_addr; });
Yabin Cui39e59792020-01-23 12:32:26 -0800214 // Use Sort() to sort maps_ and create prev_real_map links.
215 // prev_real_map is needed by libunwindstack to find the start of an embedded lib in an apk.
Yabin Cuia1cfa932019-03-01 19:03:41 -0800216 // See http://b/120981155.
Yabin Cui39e59792020-01-23 12:32:26 -0800217 Sort();
Yabin Cuia6a04202018-07-23 15:32:47 -0700218}
219
Yabin Cui91784fd2020-01-29 17:08:49 -0800220void OfflineUnwinder::CollectMetaInfo(std::unordered_map<std::string, std::string>* info_map
221 __attribute__((unused))) {
222#if defined(__aarch64__)
223 // Find pac_mask for ARMv8.3-A Pointer Authentication by below steps:
224 // 1. Create a 64 bit value with every bit set, but clear bit 55. Because linux user space uses
225 // TTBR0.
226 // 2. Use XPACLRI to clear auth code bits.
227 // 3. Flip every bit to get pac_mask, excluding bit 55.
228 // We can also use ptrace(PTRACE_GETREGSET, pid, NT_ARM_PAC_MASK). But it needs a tracee.
229 register uint64_t x30 __asm("x30") = ~(1ULL << 55);
230 // This is XPACLRI on ARMv8.3-A, and nop on prev ARMv8.3-A.
231 asm("hint 0x7" : "+r"(x30));
232 uint64_t pac_mask = ~x30 & ~(1ULL << 55);
233 if (pac_mask != 0) {
234 (*info_map)[META_KEY_ARM64_PAC_MASK] = android::base::StringPrintf("0x%" PRIx64, pac_mask);
Yabin Cui5ac7a252019-07-18 10:43:32 -0700235 }
Yabin Cui91784fd2020-01-29 17:08:49 -0800236#endif
237}
Christopher Ferris11017682017-12-14 15:53:37 -0800238
Yabin Cui91784fd2020-01-29 17:08:49 -0800239void OfflineUnwinderImpl::LoadMetaInfo(
240 const std::unordered_map<std::string, std::string>& info_map) {
241 if (auto it = info_map.find(META_KEY_ARM64_PAC_MASK); it != info_map.end()) {
242 CHECK(android::base::ParseUint(it->second, &arm64_pac_mask_));
243 }
244}
Yabin Cui5ac7a252019-07-18 10:43:32 -0700245
246bool OfflineUnwinderImpl::UnwindCallChain(const ThreadEntry& thread, const RegSet& regs,
247 const char* stack, size_t stack_size,
248 std::vector<uint64_t>* ips, std::vector<uint64_t>* sps) {
Yabin Cui03381452017-12-13 11:31:53 -0800249 uint64_t start_time;
250 if (collect_stat_) {
251 start_time = GetSystemClock();
252 }
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700253 is_callchain_broken_for_incomplete_jit_debug_info_ = false;
254 ips->clear();
255 sps->clear();
Yabin Cui3c8c2132015-08-13 20:30:20 -0700256 std::vector<uint64_t> result;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700257 uint64_t sp_reg_value;
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800258 if (!regs.GetSpRegValue(&sp_reg_value)) {
Yabin Cui3c8c2132015-08-13 20:30:20 -0700259 LOG(ERROR) << "can't get sp reg value";
Yabin Cui81a9d332017-12-10 13:09:07 -0800260 return false;
261 }
Yabin Cui3c8c2132015-08-13 20:30:20 -0700262 uint64_t stack_addr = sp_reg_value;
263
Yabin Cuia6a04202018-07-23 15:32:47 -0700264 UnwindMaps& cached_map = cached_maps_[thread.pid];
265 cached_map.UpdateMaps(*thread.maps);
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800266 std::unique_ptr<unwindstack::Regs> unwind_regs(GetBacktraceRegs(regs));
Christopher Ferris15933b62018-02-22 19:06:42 -0800267 if (!unwind_regs) {
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800268 return false;
Christopher Ferris11017682017-12-14 15:53:37 -0800269 }
Christopher Ferris2b0a7782019-05-28 16:17:54 -0700270 unwindstack::Unwinder unwinder(
271 MAX_UNWINDING_FRAMES, &cached_map, unwind_regs.get(),
272 unwindstack::Memory::CreateOfflineMemory(reinterpret_cast<const uint8_t*>(stack), stack_addr,
273 stack_addr + stack_size));
Yabin Cuia6a04202018-07-23 15:32:47 -0700274 unwinder.SetResolveNames(false);
275 unwinder.Unwind();
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700276 size_t last_jit_method_frame = UINT_MAX;
Yabin Cuia6a04202018-07-23 15:32:47 -0700277 for (auto& frame : unwinder.frames()) {
278 // Unwinding in arm architecture can return 0 pc address.
Yabin Cui18480102018-04-19 17:34:18 -0700279
Christopher Ferrisaccebda2021-11-10 14:27:45 -0800280 // If frame.map_info == nullptr, this frame doesn't hit any map, it could be:
Yabin Cuia6a04202018-07-23 15:32:47 -0700281 // 1. In an executable map not backed by a file. Note that RecordCommand::ShouldOmitRecord()
282 // may omit maps only exist memory.
Christopher Ferrisaccebda2021-11-10 14:27:45 -0800283 // 2. An incorrectly unwound frame. Likely caused by invalid stack data, as in
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700284 // SampleRecord::GetValidStackSize(). Or caused by incomplete JIT debug info.
Yabin Cuia6a04202018-07-23 15:32:47 -0700285 // We want to remove this frame and callchains following it in either case.
Christopher Ferrisaccebda2021-11-10 14:27:45 -0800286 if (frame.map_info == nullptr) {
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700287 is_callchain_broken_for_incomplete_jit_debug_info_ = true;
Yabin Cuia6a04202018-07-23 15:32:47 -0700288 break;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700289 }
Christopher Ferrisaccebda2021-11-10 14:27:45 -0800290 if (frame.map_info->flags() & unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP) {
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700291 last_jit_method_frame = ips->size();
292 }
Yabin Cuia6a04202018-07-23 15:32:47 -0700293 ips->push_back(frame.pc);
294 sps->push_back(frame.sp);
Yabin Cui3c8c2132015-08-13 20:30:20 -0700295 }
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700296 // If the unwound frames stop near to a JITed method, it may be caused by incomplete JIT debug
297 // info.
298 if (last_jit_method_frame != UINT_MAX && last_jit_method_frame + 3 > ips->size()) {
299 is_callchain_broken_for_incomplete_jit_debug_info_ = true;
300 }
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800301
302 uint64_t ip_reg_value;
303 if (!regs.GetIpRegValue(&ip_reg_value)) {
304 LOG(ERROR) << "can't get ip reg value";
305 return false;
306 }
Yabin Cui03381452017-12-13 11:31:53 -0800307 if (ips->empty()) {
Yabin Cui43dabd52018-01-22 15:11:07 -0800308 ips->push_back(ip_reg_value);
309 sps->push_back(sp_reg_value);
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800310 } else {
311 // Check if the unwinder returns ip reg value as the first ip address in callstack.
312 CHECK_EQ((*ips)[0], ip_reg_value);
Yabin Cui03381452017-12-13 11:31:53 -0800313 }
314 if (collect_stat_) {
315 unwinding_result_.used_time = GetSystemClock() - start_time;
Yabin Cui0272f022021-02-24 15:11:57 -0800316 unwinding_result_.error_code = unwinder.LastErrorCode();
317 unwinding_result_.error_addr = unwinder.LastErrorAddress();
Yabin Cuia6a04202018-07-23 15:32:47 -0700318 unwinding_result_.stack_start = stack_addr;
319 unwinding_result_.stack_end = stack_addr + stack_size;
Yabin Cui03381452017-12-13 11:31:53 -0800320 }
321 return true;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700322}
Yabin Cui03381452017-12-13 11:31:53 -0800323
Yabin Cui5ac7a252019-07-18 10:43:32 -0700324std::unique_ptr<OfflineUnwinder> OfflineUnwinder::Create(bool collect_stat) {
325 return std::unique_ptr<OfflineUnwinder>(new OfflineUnwinderImpl(collect_stat));
326}
327
Yabin Cui03381452017-12-13 11:31:53 -0800328} // namespace simpleperf