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 <sys/mman.h>
20
21 #include <android-base/logging.h>
22 #include <unwindstack/MachineArm.h>
23 #include <unwindstack/MachineArm64.h>
24 #include <unwindstack/MachineX86.h>
25 #include <unwindstack/MachineX86_64.h>
26 #include <unwindstack/Maps.h>
27 #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>
32 #include <unwindstack/Unwinder.h>
33 #include <unwindstack/UserArm.h>
34 #include <unwindstack/UserArm64.h>
35 #include <unwindstack/UserX86.h>
36 #include <unwindstack/UserX86_64.h>
37
38 #include "environment.h"
39 #include "perf_regs.h"
40 #include "read_apk.h"
41 #include "thread_tree.h"
42
43 static_assert(simpleperf::map_flags::PROT_JIT_SYMFILE_MAP ==
44 unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP, "");
45
46 namespace simpleperf {
47
48 // Max frames seen so far is 463, in http://b/110923759.
49 static constexpr size_t MAX_UNWINDING_FRAMES = 512;
50
GetBacktraceRegs(const RegSet & regs)51 static 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], ®s.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;
116 }
117 }
118
CreateMapInfo(const MapEntry * entry)119 static 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 }
134 return new unwindstack::MapInfo(nullptr, entry->start_addr, entry->get_end_addr(), pgoff,
135 PROT_READ | PROT_EXEC | entry->flags, name);
136 }
137
UpdateMaps(const MapSet & map_set)138 void 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);
153 maps_.emplace_back(CreateMapInfo(entry));
154 ++it;
155 } else {
156 // Remove an entry.
157 entries_[i] = nullptr;
158 maps_[i++] = nullptr;
159 }
160 }
161 while (i < old_size) {
162 entries_[i] = nullptr;
163 maps_[i++] = nullptr;
164 }
165 std::sort(entries_.begin(), entries_.end(), [](const auto& e1, const auto& e2) {
166 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(),
172 [](const auto& m1, const auto& m2) {
173 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
OfflineUnwinder(bool collect_stat)182 OfflineUnwinder::OfflineUnwinder(bool collect_stat) : collect_stat_(collect_stat) {
183 unwindstack::Elf::SetCachingEnabled(true);
184 }
185
UnwindCallChain(const ThreadEntry & thread,const RegSet & regs,const char * stack,size_t stack_size,std::vector<uint64_t> * ips,std::vector<uint64_t> * sps)186 bool OfflineUnwinder::UnwindCallChain(const ThreadEntry& thread, const RegSet& regs,
187 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 }
193 is_callchain_broken_for_incomplete_jit_debug_info_ = false;
194 ips->clear();
195 sps->clear();
196 std::vector<uint64_t> result;
197 uint64_t sp_reg_value;
198 if (!regs.GetSpRegValue(&sp_reg_value)) {
199 LOG(ERROR) << "can't get sp reg value";
200 return false;
201 }
202 uint64_t stack_addr = sp_reg_value;
203
204 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));
209 std::unique_ptr<unwindstack::Regs> unwind_regs(GetBacktraceRegs(regs));
210 if (!unwind_regs) {
211 return false;
212 }
213 unwindstack::Unwinder unwinder(MAX_UNWINDING_FRAMES, &cached_map, unwind_regs.get(),
214 stack_memory);
215 unwinder.SetResolveNames(false);
216 unwinder.Unwind();
217 size_t last_jit_method_frame = UINT_MAX;
218 for (auto& frame : unwinder.frames()) {
219 // Unwinding in arm architecture can return 0 pc address.
220
221 // 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
225 // SampleRecord::GetValidStackSize(). Or caused by incomplete JIT debug info.
226 // We want to remove this frame and callchains following it in either case.
227 if (frame.pc == 0 || frame.map_start == 0) {
228 is_callchain_broken_for_incomplete_jit_debug_info_ = true;
229 break;
230 }
231 if (frame.map_flags & unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP) {
232 last_jit_method_frame = ips->size();
233 }
234 ips->push_back(frame.pc);
235 sps->push_back(frame.sp);
236 }
237 // 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 }
242
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 }
248 if (ips->empty()) {
249 ips->push_back(ip_reg_value);
250 sps->push_back(sp_reg_value);
251 } 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);
254 }
255 if (collect_stat_) {
256 unwinding_result_.used_time = GetSystemClock() - start_time;
257 switch (unwinder.LastErrorCode()) {
258 case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
259 unwinding_result_.stop_reason = UnwindingResult::EXCEED_MAX_FRAMES_LIMIT;
260 break;
261 case unwindstack::ERROR_MEMORY_INVALID: {
262 uint64_t addr = unwinder.LastErrorAddress();
263 // Because we don't have precise stack range here, just guess an addr is in stack
264 // if sp - 128K <= addr <= sp.
265 if (addr <= stack_addr && addr >= stack_addr - 128 * 1024) {
266 unwinding_result_.stop_reason = UnwindingResult::ACCESS_STACK_FAILED;
267 } else {
268 unwinding_result_.stop_reason = UnwindingResult::ACCESS_MEM_FAILED;
269 }
270 unwinding_result_.stop_info.addr = addr;
271 break;
272 }
273 case unwindstack::ERROR_INVALID_MAP:
274 unwinding_result_.stop_reason = UnwindingResult::MAP_MISSING;
275 break;
276 default:
277 unwinding_result_.stop_reason = UnwindingResult::UNKNOWN_REASON;
278 break;
279 }
280 unwinding_result_.stack_start = stack_addr;
281 unwinding_result_.stack_end = stack_addr + stack_size;
282 }
283 return true;
284 }
285
286 } // namespace simpleperf
287