blob: b6b7d91fb72b99c42ed9820140a1ad14dabafa5f [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 Cuia6a04202018-07-23 15:32:47 -070019#include <sys/mman.h>
20
Elliott Hughes66dd09e2015-12-04 14:00:57 -080021#include <android-base/logging.h>
Yabin Cui6e75e1b2018-02-06 13:42:16 -080022#include <unwindstack/MachineArm.h>
23#include <unwindstack/MachineArm64.h>
24#include <unwindstack/MachineX86.h>
25#include <unwindstack/MachineX86_64.h>
Yabin Cuicdc11a32018-03-20 15:29:03 -070026#include <unwindstack/Maps.h>
Yabin Cui6e75e1b2018-02-06 13:42:16 -080027#include <unwindstack/Regs.h>
28#include <unwindstack/RegsArm.h>
29#include <unwindstack/RegsArm64.h>
30#include <unwindstack/RegsX86.h>
31#include <unwindstack/RegsX86_64.h>
Yabin Cuia6a04202018-07-23 15:32:47 -070032#include <unwindstack/Unwinder.h>
Yabin Cui6e75e1b2018-02-06 13:42:16 -080033#include <unwindstack/UserArm.h>
34#include <unwindstack/UserArm64.h>
35#include <unwindstack/UserX86.h>
36#include <unwindstack/UserX86_64.h>
Yabin Cui3c8c2132015-08-13 20:30:20 -070037
Yabin Cui03381452017-12-13 11:31:53 -080038#include "environment.h"
Yabin Cui6e75e1b2018-02-06 13:42:16 -080039#include "perf_regs.h"
Christopher Ferris11017682017-12-14 15:53:37 -080040#include "read_apk.h"
Yabin Cui3c8c2132015-08-13 20:30:20 -070041#include "thread_tree.h"
42
Yabin Cuicdc11a32018-03-20 15:29:03 -070043static_assert(simpleperf::map_flags::PROT_JIT_SYMFILE_MAP ==
44 unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP, "");
45
Yabin Cui03381452017-12-13 11:31:53 -080046namespace simpleperf {
47
Yabin Cuia6a04202018-07-23 15:32:47 -070048// Max frames seen so far is 463, in http://b/110923759.
49static constexpr size_t MAX_UNWINDING_FRAMES = 512;
50
Yabin Cui6e75e1b2018-02-06 13:42:16 -080051static unwindstack::Regs* GetBacktraceRegs(const RegSet& regs) {
52 switch (regs.arch) {
53 case ARCH_ARM: {
54 unwindstack::arm_user_regs arm_user_regs;
55 memset(&arm_user_regs, 0, sizeof(arm_user_regs));
56 static_assert(
57 static_cast<int>(unwindstack::ARM_REG_R0) == static_cast<int>(PERF_REG_ARM_R0), "");
58 static_assert(
59 static_cast<int>(unwindstack::ARM_REG_LAST) == static_cast<int>(PERF_REG_ARM_MAX), "");
60 for (size_t i = unwindstack::ARM_REG_R0; i < unwindstack::ARM_REG_LAST; ++i) {
61 arm_user_regs.regs[i] = static_cast<uint32_t>(regs.data[i]);
62 }
63 return unwindstack::RegsArm::Read(&arm_user_regs);
64 }
65 case ARCH_ARM64: {
66 unwindstack::arm64_user_regs arm64_user_regs;
67 memset(&arm64_user_regs, 0, sizeof(arm64_user_regs));
68 static_assert(
69 static_cast<int>(unwindstack::ARM64_REG_R0) == static_cast<int>(PERF_REG_ARM64_X0), "");
70 static_assert(
71 static_cast<int>(unwindstack::ARM64_REG_R30) == static_cast<int>(PERF_REG_ARM64_LR), "");
72 memcpy(&arm64_user_regs.regs[unwindstack::ARM64_REG_R0], &regs.data[PERF_REG_ARM64_X0],
73 sizeof(uint64_t) * (PERF_REG_ARM64_LR - PERF_REG_ARM64_X0 + 1));
74 arm64_user_regs.sp = regs.data[PERF_REG_ARM64_SP];
75 arm64_user_regs.pc = regs.data[PERF_REG_ARM64_PC];
76 return unwindstack::RegsArm64::Read(&arm64_user_regs);
77 }
78 case ARCH_X86_32: {
79 unwindstack::x86_user_regs x86_user_regs;
80 memset(&x86_user_regs, 0, sizeof(x86_user_regs));
81 x86_user_regs.eax = static_cast<uint32_t>(regs.data[PERF_REG_X86_AX]);
82 x86_user_regs.ebx = static_cast<uint32_t>(regs.data[PERF_REG_X86_BX]);
83 x86_user_regs.ecx = static_cast<uint32_t>(regs.data[PERF_REG_X86_CX]);
84 x86_user_regs.edx = static_cast<uint32_t>(regs.data[PERF_REG_X86_DX]);
85 x86_user_regs.ebp = static_cast<uint32_t>(regs.data[PERF_REG_X86_BP]);
86 x86_user_regs.edi = static_cast<uint32_t>(regs.data[PERF_REG_X86_DI]);
87 x86_user_regs.esi = static_cast<uint32_t>(regs.data[PERF_REG_X86_SI]);
88 x86_user_regs.esp = static_cast<uint32_t>(regs.data[PERF_REG_X86_SP]);
89 x86_user_regs.eip = static_cast<uint32_t>(regs.data[PERF_REG_X86_IP]);
90 return unwindstack::RegsX86::Read(&x86_user_regs);
91 }
92 case ARCH_X86_64: {
93 unwindstack::x86_64_user_regs x86_64_user_regs;
94 memset(&x86_64_user_regs, 0, sizeof(x86_64_user_regs));
95 x86_64_user_regs.rax = regs.data[PERF_REG_X86_AX];
96 x86_64_user_regs.rbx = regs.data[PERF_REG_X86_BX];
97 x86_64_user_regs.rcx = regs.data[PERF_REG_X86_CX];
98 x86_64_user_regs.rdx = regs.data[PERF_REG_X86_DX];
99 x86_64_user_regs.r8 = regs.data[PERF_REG_X86_R8];
100 x86_64_user_regs.r9 = regs.data[PERF_REG_X86_R9];
101 x86_64_user_regs.r10 = regs.data[PERF_REG_X86_R10];
102 x86_64_user_regs.r11 = regs.data[PERF_REG_X86_R11];
103 x86_64_user_regs.r12 = regs.data[PERF_REG_X86_R12];
104 x86_64_user_regs.r13 = regs.data[PERF_REG_X86_R13];
105 x86_64_user_regs.r14 = regs.data[PERF_REG_X86_R14];
106 x86_64_user_regs.r15 = regs.data[PERF_REG_X86_R15];
107 x86_64_user_regs.rdi = regs.data[PERF_REG_X86_DI];
108 x86_64_user_regs.rsi = regs.data[PERF_REG_X86_SI];
109 x86_64_user_regs.rbp = regs.data[PERF_REG_X86_BP];
110 x86_64_user_regs.rsp = regs.data[PERF_REG_X86_SP];
111 x86_64_user_regs.rip = regs.data[PERF_REG_X86_IP];
112 return unwindstack::RegsX86_64::Read(&x86_64_user_regs);
113 }
114 default:
115 return nullptr;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700116 }
Yabin Cui3c8c2132015-08-13 20:30:20 -0700117}
118
Yabin Cuia6a04202018-07-23 15:32:47 -0700119static unwindstack::MapInfo* CreateMapInfo(const MapEntry* entry) {
120 const char* name = entry->dso->GetDebugFilePath().c_str();
121 uint64_t pgoff = entry->pgoff;
122 if (entry->pgoff == 0) {
123 auto tuple = SplitUrlInApk(entry->dso->GetDebugFilePath());
124 if (std::get<0>(tuple)) {
125 // The unwinder does not understand the ! format, so change back to
126 // the previous format (apk, offset).
127 EmbeddedElf* elf = ApkInspector::FindElfInApkByName(std::get<1>(tuple), std::get<2>(tuple));
128 if (elf != nullptr) {
129 name = elf->filepath().c_str();
130 pgoff = elf->entry_offset();
131 }
132 }
133 }
Christopher Ferris2fed0f12018-10-03 14:14:36 -0700134 return new unwindstack::MapInfo(nullptr, entry->start_addr, entry->get_end_addr(), pgoff,
Yabin Cuia6a04202018-07-23 15:32:47 -0700135 PROT_READ | PROT_EXEC | entry->flags, name);
136}
137
138void UnwindMaps::UpdateMaps(const MapSet& map_set) {
139 if (version_ == map_set.version) {
140 return;
141 }
142 version_ = map_set.version;
143 size_t i = 0;
144 size_t old_size = entries_.size();
145 for (auto it = map_set.maps.begin(); it != map_set.maps.end();) {
146 const MapEntry* entry = it->second;
147 if (i < old_size && entry == entries_[i]) {
148 i++;
149 ++it;
150 } else if (i == old_size || entry->start_addr <= entries_[i]->start_addr) {
151 // Add an entry.
152 entries_.push_back(entry);
Florian Mayer50818b72019-02-27 19:35:01 +0000153 maps_.emplace_back(CreateMapInfo(entry));
Yabin Cuia6a04202018-07-23 15:32:47 -0700154 ++it;
155 } else {
156 // Remove an entry.
157 entries_[i] = nullptr;
Yabin Cuia6a04202018-07-23 15:32:47 -0700158 maps_[i++] = nullptr;
159 }
160 }
161 while (i < old_size) {
162 entries_[i] = nullptr;
Yabin Cuia6a04202018-07-23 15:32:47 -0700163 maps_[i++] = nullptr;
164 }
Florian Mayer50818b72019-02-27 19:35:01 +0000165 std::sort(entries_.begin(), entries_.end(), [](const auto& e1, const auto& e2) {
Yabin Cuia6a04202018-07-23 15:32:47 -0700166 if (e1 == nullptr || e2 == nullptr) {
167 return e1 != nullptr;
168 }
169 return e1->start_addr < e2->start_addr;
170 });
171 std::sort(maps_.begin(), maps_.end(),
Florian Mayer50818b72019-02-27 19:35:01 +0000172 [](const auto& m1, const auto& m2) {
Yabin Cuia6a04202018-07-23 15:32:47 -0700173 if (m1 == nullptr || m2 == nullptr) {
174 return m1 != nullptr;
175 }
176 return m1->start < m2->start;
177 });
178 entries_.resize(map_set.maps.size());
179 maps_.resize(map_set.maps.size());
180}
181
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800182OfflineUnwinder::OfflineUnwinder(bool collect_stat) : collect_stat_(collect_stat) {
Yabin Cuia6a04202018-07-23 15:32:47 -0700183 unwindstack::Elf::SetCachingEnabled(true);
Christopher Ferris11017682017-12-14 15:53:37 -0800184}
185
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800186bool OfflineUnwinder::UnwindCallChain(const ThreadEntry& thread, const RegSet& regs,
Yabin Cui03381452017-12-13 11:31:53 -0800187 const char* stack, size_t stack_size,
188 std::vector<uint64_t>* ips, std::vector<uint64_t>* sps) {
189 uint64_t start_time;
190 if (collect_stat_) {
191 start_time = GetSystemClock();
192 }
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700193 is_callchain_broken_for_incomplete_jit_debug_info_ = false;
194 ips->clear();
195 sps->clear();
Yabin Cui3c8c2132015-08-13 20:30:20 -0700196 std::vector<uint64_t> result;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700197 uint64_t sp_reg_value;
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800198 if (!regs.GetSpRegValue(&sp_reg_value)) {
Yabin Cui3c8c2132015-08-13 20:30:20 -0700199 LOG(ERROR) << "can't get sp reg value";
Yabin Cui81a9d332017-12-10 13:09:07 -0800200 return false;
201 }
Yabin Cui3c8c2132015-08-13 20:30:20 -0700202 uint64_t stack_addr = sp_reg_value;
203
Yabin Cuia6a04202018-07-23 15:32:47 -0700204 UnwindMaps& cached_map = cached_maps_[thread.pid];
205 cached_map.UpdateMaps(*thread.maps);
206 std::shared_ptr<unwindstack::MemoryOfflineBuffer> stack_memory(
207 new unwindstack::MemoryOfflineBuffer(reinterpret_cast<const uint8_t*>(stack),
208 stack_addr, stack_addr + stack_size));
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800209 std::unique_ptr<unwindstack::Regs> unwind_regs(GetBacktraceRegs(regs));
Christopher Ferris15933b62018-02-22 19:06:42 -0800210 if (!unwind_regs) {
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800211 return false;
Christopher Ferris11017682017-12-14 15:53:37 -0800212 }
Yabin Cuia6a04202018-07-23 15:32:47 -0700213 unwindstack::Unwinder unwinder(MAX_UNWINDING_FRAMES, &cached_map, unwind_regs.get(),
214 stack_memory);
215 unwinder.SetResolveNames(false);
216 unwinder.Unwind();
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700217 size_t last_jit_method_frame = UINT_MAX;
Yabin Cuia6a04202018-07-23 15:32:47 -0700218 for (auto& frame : unwinder.frames()) {
219 // Unwinding in arm architecture can return 0 pc address.
Yabin Cui18480102018-04-19 17:34:18 -0700220
Yabin Cuia6a04202018-07-23 15:32:47 -0700221 // If frame.map.start == 0, this frame doesn't hit any map, it could be:
222 // 1. In an executable map not backed by a file. Note that RecordCommand::ShouldOmitRecord()
223 // may omit maps only exist memory.
224 // 2. An incorrectly unwound frame. Like caused by invalid stack data, as in
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700225 // SampleRecord::GetValidStackSize(). Or caused by incomplete JIT debug info.
Yabin Cuia6a04202018-07-23 15:32:47 -0700226 // We want to remove this frame and callchains following it in either case.
227 if (frame.pc == 0 || frame.map_start == 0) {
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700228 is_callchain_broken_for_incomplete_jit_debug_info_ = true;
Yabin Cuia6a04202018-07-23 15:32:47 -0700229 break;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700230 }
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700231 if (frame.map_flags & unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP) {
232 last_jit_method_frame = ips->size();
233 }
Yabin Cuia6a04202018-07-23 15:32:47 -0700234 ips->push_back(frame.pc);
235 sps->push_back(frame.sp);
Yabin Cui3c8c2132015-08-13 20:30:20 -0700236 }
Yabin Cui5d5c01a2018-08-27 10:30:20 -0700237 // If the unwound frames stop near to a JITed method, it may be caused by incomplete JIT debug
238 // info.
239 if (last_jit_method_frame != UINT_MAX && last_jit_method_frame + 3 > ips->size()) {
240 is_callchain_broken_for_incomplete_jit_debug_info_ = true;
241 }
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800242
243 uint64_t ip_reg_value;
244 if (!regs.GetIpRegValue(&ip_reg_value)) {
245 LOG(ERROR) << "can't get ip reg value";
246 return false;
247 }
Yabin Cui03381452017-12-13 11:31:53 -0800248 if (ips->empty()) {
Yabin Cui43dabd52018-01-22 15:11:07 -0800249 ips->push_back(ip_reg_value);
250 sps->push_back(sp_reg_value);
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800251 } else {
252 // Check if the unwinder returns ip reg value as the first ip address in callstack.
253 CHECK_EQ((*ips)[0], ip_reg_value);
Yabin Cui03381452017-12-13 11:31:53 -0800254 }
255 if (collect_stat_) {
256 unwinding_result_.used_time = GetSystemClock() - start_time;
Yabin Cuia6a04202018-07-23 15:32:47 -0700257 switch (unwinder.LastErrorCode()) {
258 case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
Yabin Cui03381452017-12-13 11:31:53 -0800259 unwinding_result_.stop_reason = UnwindingResult::EXCEED_MAX_FRAMES_LIMIT;
260 break;
Yabin Cuia6a04202018-07-23 15:32:47 -0700261 case unwindstack::ERROR_MEMORY_INVALID: {
262 uint64_t addr = unwinder.LastErrorAddress();
Yabin Cui03381452017-12-13 11:31:53 -0800263 // Because we don't have precise stack range here, just guess an addr is in stack
264 // if sp - 128K <= addr <= sp.
Yabin Cuia6a04202018-07-23 15:32:47 -0700265 if (addr <= stack_addr && addr >= stack_addr - 128 * 1024) {
Yabin Cui03381452017-12-13 11:31:53 -0800266 unwinding_result_.stop_reason = UnwindingResult::ACCESS_STACK_FAILED;
267 } else {
268 unwinding_result_.stop_reason = UnwindingResult::ACCESS_MEM_FAILED;
269 }
Yabin Cuia6a04202018-07-23 15:32:47 -0700270 unwinding_result_.stop_info.addr = addr;
Yabin Cui03381452017-12-13 11:31:53 -0800271 break;
Yabin Cuia6a04202018-07-23 15:32:47 -0700272 }
273 case unwindstack::ERROR_INVALID_MAP:
Yabin Cui750b3732017-12-18 17:46:36 -0800274 unwinding_result_.stop_reason = UnwindingResult::MAP_MISSING;
275 break;
Yabin Cui03381452017-12-13 11:31:53 -0800276 default:
277 unwinding_result_.stop_reason = UnwindingResult::UNKNOWN_REASON;
278 break;
279 }
Yabin Cuia6a04202018-07-23 15:32:47 -0700280 unwinding_result_.stack_start = stack_addr;
281 unwinding_result_.stack_end = stack_addr + stack_size;
Yabin Cui03381452017-12-13 11:31:53 -0800282 }
283 return true;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700284}
Yabin Cui03381452017-12-13 11:31:53 -0800285
286} // namespace simpleperf