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 <unordered_map>
22 
23 #include <android-base/logging.h>
24 #include <unwindstack/MachineArm.h>
25 #include <unwindstack/MachineArm64.h>
26 #include <unwindstack/MachineX86.h>
27 #include <unwindstack/MachineX86_64.h>
28 #include <unwindstack/Maps.h>
29 #include <unwindstack/Regs.h>
30 #include <unwindstack/RegsArm.h>
31 #include <unwindstack/RegsArm64.h>
32 #include <unwindstack/RegsX86.h>
33 #include <unwindstack/RegsX86_64.h>
34 #include <unwindstack/Unwinder.h>
35 #include <unwindstack/UserArm.h>
36 #include <unwindstack/UserArm64.h>
37 #include <unwindstack/UserX86.h>
38 #include <unwindstack/UserX86_64.h>
39 
40 #include "environment.h"
41 #include "perf_regs.h"
42 #include "read_apk.h"
43 #include "thread_tree.h"
44 
45 static_assert(simpleperf::map_flags::PROT_JIT_SYMFILE_MAP ==
46               unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP, "");
47 
48 namespace simpleperf {
49 
50 // Max frames seen so far is 463, in http://b/110923759.
51 static constexpr size_t MAX_UNWINDING_FRAMES = 512;
52 
GetBacktraceRegs(const RegSet & regs)53 static unwindstack::Regs* GetBacktraceRegs(const RegSet& regs) {
54   switch (regs.arch) {
55     case ARCH_ARM: {
56       unwindstack::arm_user_regs arm_user_regs;
57       memset(&arm_user_regs, 0, sizeof(arm_user_regs));
58       static_assert(
59           static_cast<int>(unwindstack::ARM_REG_R0) == static_cast<int>(PERF_REG_ARM_R0), "");
60       static_assert(
61           static_cast<int>(unwindstack::ARM_REG_LAST) == static_cast<int>(PERF_REG_ARM_MAX), "");
62       for (size_t i = unwindstack::ARM_REG_R0; i < unwindstack::ARM_REG_LAST; ++i) {
63         arm_user_regs.regs[i] = static_cast<uint32_t>(regs.data[i]);
64       }
65       return unwindstack::RegsArm::Read(&arm_user_regs);
66     }
67     case ARCH_ARM64: {
68       unwindstack::arm64_user_regs arm64_user_regs;
69       memset(&arm64_user_regs, 0, sizeof(arm64_user_regs));
70       static_assert(
71           static_cast<int>(unwindstack::ARM64_REG_R0) == static_cast<int>(PERF_REG_ARM64_X0), "");
72       static_assert(
73           static_cast<int>(unwindstack::ARM64_REG_R30) == static_cast<int>(PERF_REG_ARM64_LR), "");
74       memcpy(&arm64_user_regs.regs[unwindstack::ARM64_REG_R0], &regs.data[PERF_REG_ARM64_X0],
75              sizeof(uint64_t) * (PERF_REG_ARM64_LR - PERF_REG_ARM64_X0 + 1));
76       arm64_user_regs.sp = regs.data[PERF_REG_ARM64_SP];
77       arm64_user_regs.pc = regs.data[PERF_REG_ARM64_PC];
78       return unwindstack::RegsArm64::Read(&arm64_user_regs);
79     }
80     case ARCH_X86_32: {
81       unwindstack::x86_user_regs x86_user_regs;
82       memset(&x86_user_regs, 0, sizeof(x86_user_regs));
83       x86_user_regs.eax = static_cast<uint32_t>(regs.data[PERF_REG_X86_AX]);
84       x86_user_regs.ebx = static_cast<uint32_t>(regs.data[PERF_REG_X86_BX]);
85       x86_user_regs.ecx = static_cast<uint32_t>(regs.data[PERF_REG_X86_CX]);
86       x86_user_regs.edx = static_cast<uint32_t>(regs.data[PERF_REG_X86_DX]);
87       x86_user_regs.ebp = static_cast<uint32_t>(regs.data[PERF_REG_X86_BP]);
88       x86_user_regs.edi = static_cast<uint32_t>(regs.data[PERF_REG_X86_DI]);
89       x86_user_regs.esi = static_cast<uint32_t>(regs.data[PERF_REG_X86_SI]);
90       x86_user_regs.esp = static_cast<uint32_t>(regs.data[PERF_REG_X86_SP]);
91       x86_user_regs.eip = static_cast<uint32_t>(regs.data[PERF_REG_X86_IP]);
92       return unwindstack::RegsX86::Read(&x86_user_regs);
93     }
94     case ARCH_X86_64: {
95       unwindstack::x86_64_user_regs x86_64_user_regs;
96       memset(&x86_64_user_regs, 0, sizeof(x86_64_user_regs));
97       x86_64_user_regs.rax = regs.data[PERF_REG_X86_AX];
98       x86_64_user_regs.rbx = regs.data[PERF_REG_X86_BX];
99       x86_64_user_regs.rcx = regs.data[PERF_REG_X86_CX];
100       x86_64_user_regs.rdx = regs.data[PERF_REG_X86_DX];
101       x86_64_user_regs.r8 = regs.data[PERF_REG_X86_R8];
102       x86_64_user_regs.r9 = regs.data[PERF_REG_X86_R9];
103       x86_64_user_regs.r10 = regs.data[PERF_REG_X86_R10];
104       x86_64_user_regs.r11 = regs.data[PERF_REG_X86_R11];
105       x86_64_user_regs.r12 = regs.data[PERF_REG_X86_R12];
106       x86_64_user_regs.r13 = regs.data[PERF_REG_X86_R13];
107       x86_64_user_regs.r14 = regs.data[PERF_REG_X86_R14];
108       x86_64_user_regs.r15 = regs.data[PERF_REG_X86_R15];
109       x86_64_user_regs.rdi = regs.data[PERF_REG_X86_DI];
110       x86_64_user_regs.rsi = regs.data[PERF_REG_X86_SI];
111       x86_64_user_regs.rbp = regs.data[PERF_REG_X86_BP];
112       x86_64_user_regs.rsp = regs.data[PERF_REG_X86_SP];
113       x86_64_user_regs.rip = regs.data[PERF_REG_X86_IP];
114       return unwindstack::RegsX86_64::Read(&x86_64_user_regs);
115     }
116     default:
117       return nullptr;
118   }
119 }
120 
CreateMapInfo(const MapEntry * entry)121 static unwindstack::MapInfo* CreateMapInfo(const MapEntry* entry) {
122   const char* name = entry->dso->GetDebugFilePath().c_str();
123   uint64_t pgoff = entry->pgoff;
124   auto tuple = SplitUrlInApk(entry->dso->GetDebugFilePath());
125   if (std::get<0>(tuple)) {
126     // The unwinder does not understand the ! format, so change back to
127     // the previous format (apk, offset).
128     EmbeddedElf* elf = ApkInspector::FindElfInApkByName(std::get<1>(tuple), std::get<2>(tuple));
129     if (elf != nullptr) {
130       name = elf->filepath().c_str();
131       pgoff += elf->entry_offset();
132     }
133   }
134   return new unwindstack::MapInfo(nullptr, entry->start_addr, entry->get_end_addr(), pgoff,
135                                   PROT_READ | entry->flags, name);
136 }
137 
138 class UnwindMaps : public unwindstack::Maps {
139  public:
140   void UpdateMaps(const MapSet& map_set);
141 
142  private:
143   uint64_t version_ = 0u;
144   std::vector<const MapEntry*> entries_;
145 };
146 
UpdateMaps(const MapSet & map_set)147 void UnwindMaps::UpdateMaps(const MapSet& map_set) {
148   if (version_ == map_set.version) {
149     return;
150   }
151   version_ = map_set.version;
152   size_t i = 0;
153   size_t old_size = entries_.size();
154   for (auto it = map_set.maps.begin(); it != map_set.maps.end();) {
155     const MapEntry* entry = it->second;
156     if (i < old_size && entry == entries_[i]) {
157       i++;
158       ++it;
159     } else if (i == old_size || entry->start_addr <= entries_[i]->start_addr) {
160       // Add an entry.
161       entries_.push_back(entry);
162       maps_.emplace_back(CreateMapInfo(entry));
163       ++it;
164     } else {
165       // Remove an entry.
166       entries_[i] = nullptr;
167       maps_[i++] = nullptr;
168     }
169   }
170   while (i < old_size) {
171     entries_[i] = nullptr;
172     maps_[i++] = nullptr;
173   }
174   std::sort(entries_.begin(), entries_.end(), [](const auto& e1, const auto& e2) {
175     if (e1 == nullptr || e2 == nullptr) {
176       return e1 != nullptr;
177     }
178     return e1->start_addr < e2->start_addr;
179   });
180   std::sort(maps_.begin(), maps_.end(),
181             [](const auto& m1, const auto& m2) {
182     if (m1 == nullptr || m2 == nullptr) {
183       return m1 != nullptr;
184     }
185     return m1->start < m2->start;
186   });
187   entries_.resize(map_set.maps.size());
188   maps_.resize(map_set.maps.size());
189   // prev_map is needed by libunwindstack to find the start of an embedded lib in an apk.
190   // See http://b/120981155.
191   for (size_t i = 1; i < maps_.size(); ++i) {
192     maps_[i]->prev_map = maps_[i-1].get();
193   }
194 }
195 
196 class OfflineUnwinderImpl : public OfflineUnwinder {
197  public:
OfflineUnwinderImpl(bool collect_stat)198   OfflineUnwinderImpl(bool collect_stat) : collect_stat_(collect_stat) {
199     unwindstack::Elf::SetCachingEnabled(true);
200   }
201 
202   bool UnwindCallChain(const ThreadEntry& thread, const RegSet& regs, const char* stack,
203                        size_t stack_size, std::vector<uint64_t>* ips,
204                        std::vector<uint64_t>* sps) override;
205 
206  private:
207   bool collect_stat_;
208   std::unordered_map<pid_t, UnwindMaps> cached_maps_;
209 };
210 
UnwindCallChain(const ThreadEntry & thread,const RegSet & regs,const char * stack,size_t stack_size,std::vector<uint64_t> * ips,std::vector<uint64_t> * sps)211 bool OfflineUnwinderImpl::UnwindCallChain(const ThreadEntry& thread, const RegSet& regs,
212                                           const char* stack, size_t stack_size,
213                                           std::vector<uint64_t>* ips, std::vector<uint64_t>* sps) {
214   uint64_t start_time;
215   if (collect_stat_) {
216     start_time = GetSystemClock();
217   }
218   is_callchain_broken_for_incomplete_jit_debug_info_ = false;
219   ips->clear();
220   sps->clear();
221   std::vector<uint64_t> result;
222   uint64_t sp_reg_value;
223   if (!regs.GetSpRegValue(&sp_reg_value)) {
224     LOG(ERROR) << "can't get sp reg value";
225     return false;
226   }
227   uint64_t stack_addr = sp_reg_value;
228 
229   UnwindMaps& cached_map = cached_maps_[thread.pid];
230   cached_map.UpdateMaps(*thread.maps);
231   std::unique_ptr<unwindstack::Regs> unwind_regs(GetBacktraceRegs(regs));
232   if (!unwind_regs) {
233     return false;
234   }
235   unwindstack::Unwinder unwinder(
236       MAX_UNWINDING_FRAMES, &cached_map, unwind_regs.get(),
237       unwindstack::Memory::CreateOfflineMemory(reinterpret_cast<const uint8_t*>(stack), stack_addr,
238                                                stack_addr + stack_size));
239   unwinder.SetResolveNames(false);
240   unwinder.Unwind();
241   size_t last_jit_method_frame = UINT_MAX;
242   for (auto& frame : unwinder.frames()) {
243     // Unwinding in arm architecture can return 0 pc address.
244 
245     // If frame.map.start == 0, this frame doesn't hit any map, it could be:
246     // 1. In an executable map not backed by a file. Note that RecordCommand::ShouldOmitRecord()
247     //    may omit maps only exist memory.
248     // 2. An incorrectly unwound frame. Like caused by invalid stack data, as in
249     //    SampleRecord::GetValidStackSize(). Or caused by incomplete JIT debug info.
250     // We want to remove this frame and callchains following it in either case.
251     if (frame.pc == 0 || frame.map_start == 0) {
252       is_callchain_broken_for_incomplete_jit_debug_info_ = true;
253       break;
254     }
255     if (frame.map_flags & unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP) {
256       last_jit_method_frame = ips->size();
257     }
258     ips->push_back(frame.pc);
259     sps->push_back(frame.sp);
260   }
261   // If the unwound frames stop near to a JITed method, it may be caused by incomplete JIT debug
262   // info.
263   if (last_jit_method_frame != UINT_MAX && last_jit_method_frame + 3 > ips->size()) {
264     is_callchain_broken_for_incomplete_jit_debug_info_ = true;
265   }
266 
267   uint64_t ip_reg_value;
268   if (!regs.GetIpRegValue(&ip_reg_value)) {
269     LOG(ERROR) << "can't get ip reg value";
270     return false;
271   }
272   if (ips->empty()) {
273     ips->push_back(ip_reg_value);
274     sps->push_back(sp_reg_value);
275   } else {
276     // Check if the unwinder returns ip reg value as the first ip address in callstack.
277     CHECK_EQ((*ips)[0], ip_reg_value);
278   }
279   if (collect_stat_) {
280     unwinding_result_.used_time = GetSystemClock() - start_time;
281     switch (unwinder.LastErrorCode()) {
282       case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
283         unwinding_result_.stop_reason = UnwindingResult::EXCEED_MAX_FRAMES_LIMIT;
284         break;
285       case unwindstack::ERROR_MEMORY_INVALID: {
286         uint64_t addr = unwinder.LastErrorAddress();
287         // Because we don't have precise stack range here, just guess an addr is in stack
288         // if sp - 128K <= addr <= sp.
289         if (addr <= stack_addr && addr >= stack_addr - 128 * 1024) {
290           unwinding_result_.stop_reason = UnwindingResult::ACCESS_STACK_FAILED;
291         } else {
292           unwinding_result_.stop_reason = UnwindingResult::ACCESS_MEM_FAILED;
293         }
294         unwinding_result_.stop_info.addr = addr;
295         break;
296       }
297       case unwindstack::ERROR_INVALID_MAP:
298         unwinding_result_.stop_reason = UnwindingResult::MAP_MISSING;
299         break;
300       default:
301         unwinding_result_.stop_reason = UnwindingResult::UNKNOWN_REASON;
302         break;
303     }
304     unwinding_result_.stack_start = stack_addr;
305     unwinding_result_.stack_end = stack_addr + stack_size;
306   }
307   return true;
308 }
309 
Create(bool collect_stat)310 std::unique_ptr<OfflineUnwinder> OfflineUnwinder::Create(bool collect_stat) {
311   return std::unique_ptr<OfflineUnwinder>(new OfflineUnwinderImpl(collect_stat));
312 }
313 
314 }  // namespace simpleperf
315