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