blob: c847bca768848257229628bff89866b6f4cb9ac9 [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>
haocheng.zyf190f152022-10-12 22:58:43 +080030#include <unwindstack/MachineRiscv64.h>
Yabin Cuicdc11a32018-03-20 15:29:03 -070031#include <unwindstack/Maps.h>
Yabin Cui6e75e1b2018-02-06 13:42:16 -080032#include <unwindstack/RegsArm.h>
33#include <unwindstack/RegsArm64.h>
34#include <unwindstack/RegsX86.h>
35#include <unwindstack/RegsX86_64.h>
haocheng.zyf190f152022-10-12 22:58:43 +080036#include <unwindstack/RegsRiscv64.h>
Yabin Cuia6a04202018-07-23 15:32:47 -070037#include <unwindstack/Unwinder.h>
Yabin Cui6e75e1b2018-02-06 13:42:16 -080038#include <unwindstack/UserArm.h>
39#include <unwindstack/UserArm64.h>
40#include <unwindstack/UserX86.h>
41#include <unwindstack/UserX86_64.h>
haocheng.zyf190f152022-10-12 22:58:43 +080042#include <unwindstack/UserRiscv64.h>
Yabin Cui3c8c2132015-08-13 20:30:20 -070043
Yabin Cui9ba4d942020-09-08 16:12:46 -070044#include "JITDebugReader.h"
Yabin Cui39e59792020-01-23 12:32:26 -080045#include "OfflineUnwinder_impl.h"
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020046#include "environment.h"
Yabin Cui6e75e1b2018-02-06 13:42:16 -080047#include "perf_regs.h"
Christopher Ferris11017682017-12-14 15:53:37 -080048#include "read_apk.h"
Yabin Cui3c8c2132015-08-13 20:30:20 -070049#include "thread_tree.h"
50
Yabin Cui03381452017-12-13 11:31:53 -080051namespace simpleperf {
52
Yabin Cuie0a19802021-03-16 17:24:11 -070053// 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.
55static_assert(map_flags::PROT_JIT_SYMFILE_MAP == unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP);
56
Liz Kammer60ef88b2021-07-21 09:29:38 -040057#define CHECK_ERROR_CODE(error_code_name) \
58 static_assert(UnwindStackErrorCode::error_code_name == \
59 (UnwindStackErrorCode)unwindstack::ErrorCode::error_code_name)
Yabin Cuie0a19802021-03-16 17:24:11 -070060
61CHECK_ERROR_CODE(ERROR_NONE);
62CHECK_ERROR_CODE(ERROR_MEMORY_INVALID);
63CHECK_ERROR_CODE(ERROR_UNWIND_INFO);
64CHECK_ERROR_CODE(ERROR_UNSUPPORTED);
65CHECK_ERROR_CODE(ERROR_INVALID_MAP);
66CHECK_ERROR_CODE(ERROR_MAX_FRAMES_EXCEEDED);
67CHECK_ERROR_CODE(ERROR_REPEATED_FRAME);
68CHECK_ERROR_CODE(ERROR_INVALID_ELF);
69CHECK_ERROR_CODE(ERROR_THREAD_DOES_NOT_EXIST);
70CHECK_ERROR_CODE(ERROR_THREAD_TIMEOUT);
71CHECK_ERROR_CODE(ERROR_SYSTEM_CALL);
Christopher Ferrise617e492022-04-19 20:06:25 -070072CHECK_ERROR_CODE(ERROR_BAD_ARCH);
73CHECK_ERROR_CODE(ERROR_MAPS_PARSE);
74CHECK_ERROR_CODE(ERROR_INVALID_PARAMETER);
Yabin Cuie0a19802021-03-16 17:24:11 -070075CHECK_ERROR_CODE(ERROR_MAX);
76
Yabin Cuia6a04202018-07-23 15:32:47 -070077// Max frames seen so far is 463, in http://b/110923759.
78static constexpr size_t MAX_UNWINDING_FRAMES = 512;
79
Yabin Cui91784fd2020-01-29 17:08:49 -080080unwindstack::Regs* OfflineUnwinderImpl::GetBacktraceRegs(const RegSet& regs) {
Yabin Cui6e75e1b2018-02-06 13:42:16 -080081 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));
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020085 static_assert(static_cast<int>(unwindstack::ARM_REG_R0) == static_cast<int>(PERF_REG_ARM_R0),
86 "");
Yabin Cui6e75e1b2018-02-06 13:42:16 -080087 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], &regs.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];
Yabin Cui91784fd2020-01-29 17:08:49 -0800105 auto regs =
106 static_cast<unwindstack::RegsArm64*>(unwindstack::RegsArm64::Read(&arm64_user_regs));
107 regs->SetPACMask(arm64_pac_mask_);
108 return regs;
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800109 }
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 }
haocheng.zyf190f152022-10-12 22:58:43 +0800146 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 }
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800183 default:
184 return nullptr;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700185 }
Yabin Cui3c8c2132015-08-13 20:30:20 -0700186}
187
Christopher Ferris260250e2021-01-15 15:59:06 -0800188static std::shared_ptr<unwindstack::MapInfo> CreateMapInfo(const MapEntry* entry) {
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700189 std::string name_holder;
190 const char* name = entry->dso->GetDebugFilePath().data();
Yabin Cuia6a04202018-07-23 15:32:47 -0700191 uint64_t pgoff = entry->pgoff;
Yabin Cuia1cfa932019-03-01 19:03:41 -0800192 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) {
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700198 name = elf->filepath().data();
Yabin Cuia1cfa932019-03-01 19:03:41 -0800199 pgoff += elf->entry_offset();
Yabin Cuia6a04202018-07-23 15:32:47 -0700200 }
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700201 } else if (entry->flags & map_flags::PROT_JIT_SYMFILE_MAP) {
202 // Remove location_in_file suffix, which isn't recognized by libunwindstack.
Yabin Cui9ba4d942020-09-08 16:12:46 -0700203 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);
Yabin Cuie32ed2b2020-07-23 15:30:14 -0700208 name = name_holder.data();
209 }
Yabin Cuia6a04202018-07-23 15:32:47 -0700210 }
Christopher Ferris260250e2021-01-15 15:59:06 -0800211 return unwindstack::MapInfo::Create(entry->start_addr, entry->get_end_addr(), pgoff,
212 PROT_READ | entry->flags, name);
Yabin Cuia6a04202018-07-23 15:32:47 -0700213}
214
215void 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();
Yabin Cui39e59792020-01-23 12:32:26 -0800222 bool has_removed_entry = false;
Yabin Cuia6a04202018-07-23 15:32:47 -0700223 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);
Florian Mayer50818b72019-02-27 19:35:01 +0000231 maps_.emplace_back(CreateMapInfo(entry));
Yabin Cuia6a04202018-07-23 15:32:47 -0700232 ++it;
233 } else {
234 // Remove an entry.
Yabin Cui39e59792020-01-23 12:32:26 -0800235 has_removed_entry = true;
Yabin Cuia6a04202018-07-23 15:32:47 -0700236 entries_[i] = nullptr;
Yabin Cuia6a04202018-07-23 15:32:47 -0700237 maps_[i++] = nullptr;
238 }
239 }
240 while (i < old_size) {
Yabin Cui39e59792020-01-23 12:32:26 -0800241 has_removed_entry = true;
Yabin Cuia6a04202018-07-23 15:32:47 -0700242 entries_[i] = nullptr;
Yabin Cuia6a04202018-07-23 15:32:47 -0700243 maps_[i++] = nullptr;
244 }
Yabin Cui39e59792020-01-23 12:32:26 -0800245
246 if (has_removed_entry) {
247 entries_.resize(std::remove(entries_.begin(), entries_.end(), nullptr) - entries_.begin());
Christopher Ferris260250e2021-01-15 15:59:06 -0800248 maps_.resize(std::remove(maps_.begin(), maps_.end(), std::shared_ptr<unwindstack::MapInfo>()) -
Yabin Cui39e59792020-01-23 12:32:26 -0800249 maps_.begin());
250 }
251
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +0200252 std::sort(entries_.begin(), entries_.end(),
253 [](const auto& e1, const auto& e2) { return e1->start_addr < e2->start_addr; });
Yabin Cui39e59792020-01-23 12:32:26 -0800254 // 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.
Yabin Cuia1cfa932019-03-01 19:03:41 -0800256 // See http://b/120981155.
Yabin Cui39e59792020-01-23 12:32:26 -0800257 Sort();
Yabin Cuia6a04202018-07-23 15:32:47 -0700258}
259
Yabin Cui91784fd2020-01-29 17:08:49 -0800260void 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);
Yabin Cui5ac7a252019-07-18 10:43:32 -0700275 }
Yabin Cui91784fd2020-01-29 17:08:49 -0800276#endif
277}
Christopher Ferris11017682017-12-14 15:53:37 -0800278
Yabin Cui91784fd2020-01-29 17:08:49 -0800279void 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}
Yabin Cui5ac7a252019-07-18 10:43:32 -0700285
286bool 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) {
Yabin Cui03381452017-12-13 11:31:53 -0800289 uint64_t start_time;
290 if (collect_stat_) {
291 start_time = GetSystemClock();
292 }
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700293 is_callchain_broken_for_incomplete_jit_debug_info_ = false;
294 ips->clear();
295 sps->clear();
Yabin Cui3c8c2132015-08-13 20:30:20 -0700296 std::vector<uint64_t> result;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700297 uint64_t sp_reg_value;
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800298 if (!regs.GetSpRegValue(&sp_reg_value)) {
Yabin Cui3c8c2132015-08-13 20:30:20 -0700299 LOG(ERROR) << "can't get sp reg value";
Yabin Cui81a9d332017-12-10 13:09:07 -0800300 return false;
301 }
Yabin Cui3c8c2132015-08-13 20:30:20 -0700302 uint64_t stack_addr = sp_reg_value;
303
Yabin Cuia6a04202018-07-23 15:32:47 -0700304 UnwindMaps& cached_map = cached_maps_[thread.pid];
305 cached_map.UpdateMaps(*thread.maps);
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800306 std::unique_ptr<unwindstack::Regs> unwind_regs(GetBacktraceRegs(regs));
Christopher Ferris15933b62018-02-22 19:06:42 -0800307 if (!unwind_regs) {
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800308 return false;
Christopher Ferris11017682017-12-14 15:53:37 -0800309 }
Christopher Ferris2b0a7782019-05-28 16:17:54 -0700310 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));
Yabin Cuia6a04202018-07-23 15:32:47 -0700314 unwinder.SetResolveNames(false);
315 unwinder.Unwind();
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700316 size_t last_jit_method_frame = UINT_MAX;
Yabin Cuia6a04202018-07-23 15:32:47 -0700317 for (auto& frame : unwinder.frames()) {
318 // Unwinding in arm architecture can return 0 pc address.
Yabin Cui18480102018-04-19 17:34:18 -0700319
Christopher Ferrisaccebda2021-11-10 14:27:45 -0800320 // If frame.map_info == nullptr, this frame doesn't hit any map, it could be:
Yabin Cuia6a04202018-07-23 15:32:47 -0700321 // 1. In an executable map not backed by a file. Note that RecordCommand::ShouldOmitRecord()
322 // may omit maps only exist memory.
Christopher Ferrisaccebda2021-11-10 14:27:45 -0800323 // 2. An incorrectly unwound frame. Likely caused by invalid stack data, as in
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700324 // SampleRecord::GetValidStackSize(). Or caused by incomplete JIT debug info.
Yabin Cuia6a04202018-07-23 15:32:47 -0700325 // We want to remove this frame and callchains following it in either case.
Christopher Ferrisaccebda2021-11-10 14:27:45 -0800326 if (frame.map_info == nullptr) {
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700327 is_callchain_broken_for_incomplete_jit_debug_info_ = true;
Yabin Cuia6a04202018-07-23 15:32:47 -0700328 break;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700329 }
Christopher Ferrisaccebda2021-11-10 14:27:45 -0800330 if (frame.map_info->flags() & unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP) {
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700331 last_jit_method_frame = ips->size();
332 }
Yabin Cuia6a04202018-07-23 15:32:47 -0700333 ips->push_back(frame.pc);
334 sps->push_back(frame.sp);
Yabin Cui3c8c2132015-08-13 20:30:20 -0700335 }
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700336 // 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 }
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800341
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 }
Yabin Cui03381452017-12-13 11:31:53 -0800347 if (ips->empty()) {
Yabin Cui43dabd52018-01-22 15:11:07 -0800348 ips->push_back(ip_reg_value);
349 sps->push_back(sp_reg_value);
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800350 } 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);
Yabin Cui03381452017-12-13 11:31:53 -0800353 }
354 if (collect_stat_) {
355 unwinding_result_.used_time = GetSystemClock() - start_time;
Yabin Cui0272f022021-02-24 15:11:57 -0800356 unwinding_result_.error_code = unwinder.LastErrorCode();
357 unwinding_result_.error_addr = unwinder.LastErrorAddress();
Yabin Cuia6a04202018-07-23 15:32:47 -0700358 unwinding_result_.stack_start = stack_addr;
359 unwinding_result_.stack_end = stack_addr + stack_size;
Yabin Cui03381452017-12-13 11:31:53 -0800360 }
361 return true;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700362}
Yabin Cui03381452017-12-13 11:31:53 -0800363
Yabin Cui5ac7a252019-07-18 10:43:32 -0700364std::unique_ptr<OfflineUnwinder> OfflineUnwinder::Create(bool collect_stat) {
365 return std::unique_ptr<OfflineUnwinder>(new OfflineUnwinderImpl(collect_stat));
366}
367
Yabin Cui03381452017-12-13 11:31:53 -0800368} // namespace simpleperf