• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <inttypes.h>
20 #include <sys/mman.h>
21 
22 #include <unordered_map>
23 
24 #include <android-base/logging.h>
25 #include <android-base/parseint.h>
26 #include <unwindstack/MachineArm.h>
27 #include <unwindstack/MachineArm64.h>
28 #include <unwindstack/MachineX86.h>
29 #include <unwindstack/MachineX86_64.h>
30 #include <unwindstack/Maps.h>
31 #include <unwindstack/RegsArm.h>
32 #include <unwindstack/RegsArm64.h>
33 #include <unwindstack/RegsX86.h>
34 #include <unwindstack/RegsX86_64.h>
35 #include <unwindstack/Unwinder.h>
36 #include <unwindstack/UserArm.h>
37 #include <unwindstack/UserArm64.h>
38 #include <unwindstack/UserX86.h>
39 #include <unwindstack/UserX86_64.h>
40 
41 #include "JITDebugReader.h"
42 #include "OfflineUnwinder_impl.h"
43 #include "environment.h"
44 #include "perf_regs.h"
45 #include "read_apk.h"
46 #include "thread_tree.h"
47 
48 namespace simpleperf {
49 
50 // unwindstack only builds on linux. So simpleperf redefines flags in unwindstack, to use them on
51 // darwin/windows. Use static_assert to make sure they are on the same page.
52 static_assert(map_flags::PROT_JIT_SYMFILE_MAP == unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP);
53 
54 #define CHECK_ERROR_CODE(error_code_name)                \
55   static_assert(UnwindStackErrorCode::error_code_name == \
56                 (UnwindStackErrorCode)unwindstack::ErrorCode::error_code_name)
57 
58 CHECK_ERROR_CODE(ERROR_NONE);
59 CHECK_ERROR_CODE(ERROR_MEMORY_INVALID);
60 CHECK_ERROR_CODE(ERROR_UNWIND_INFO);
61 CHECK_ERROR_CODE(ERROR_UNSUPPORTED);
62 CHECK_ERROR_CODE(ERROR_INVALID_MAP);
63 CHECK_ERROR_CODE(ERROR_MAX_FRAMES_EXCEEDED);
64 CHECK_ERROR_CODE(ERROR_REPEATED_FRAME);
65 CHECK_ERROR_CODE(ERROR_INVALID_ELF);
66 CHECK_ERROR_CODE(ERROR_THREAD_DOES_NOT_EXIST);
67 CHECK_ERROR_CODE(ERROR_THREAD_TIMEOUT);
68 CHECK_ERROR_CODE(ERROR_SYSTEM_CALL);
69 CHECK_ERROR_CODE(ERROR_BAD_ARCH);
70 CHECK_ERROR_CODE(ERROR_MAPS_PARSE);
71 CHECK_ERROR_CODE(ERROR_INVALID_PARAMETER);
72 CHECK_ERROR_CODE(ERROR_MAX);
73 
74 // Max frames seen so far is 463, in http://b/110923759.
75 static constexpr size_t MAX_UNWINDING_FRAMES = 512;
76 
GetBacktraceRegs(const RegSet & regs)77 unwindstack::Regs* OfflineUnwinderImpl::GetBacktraceRegs(const RegSet& regs) {
78   switch (regs.arch) {
79     case ARCH_ARM: {
80       unwindstack::arm_user_regs arm_user_regs;
81       memset(&arm_user_regs, 0, sizeof(arm_user_regs));
82       static_assert(static_cast<int>(unwindstack::ARM_REG_R0) == static_cast<int>(PERF_REG_ARM_R0),
83                     "");
84       static_assert(
85           static_cast<int>(unwindstack::ARM_REG_LAST) == static_cast<int>(PERF_REG_ARM_MAX), "");
86       for (size_t i = unwindstack::ARM_REG_R0; i < unwindstack::ARM_REG_LAST; ++i) {
87         arm_user_regs.regs[i] = static_cast<uint32_t>(regs.data[i]);
88       }
89       return unwindstack::RegsArm::Read(&arm_user_regs);
90     }
91     case ARCH_ARM64: {
92       unwindstack::arm64_user_regs arm64_user_regs;
93       memset(&arm64_user_regs, 0, sizeof(arm64_user_regs));
94       static_assert(
95           static_cast<int>(unwindstack::ARM64_REG_R0) == static_cast<int>(PERF_REG_ARM64_X0), "");
96       static_assert(
97           static_cast<int>(unwindstack::ARM64_REG_R30) == static_cast<int>(PERF_REG_ARM64_LR), "");
98       memcpy(&arm64_user_regs.regs[unwindstack::ARM64_REG_R0], &regs.data[PERF_REG_ARM64_X0],
99              sizeof(uint64_t) * (PERF_REG_ARM64_LR - PERF_REG_ARM64_X0 + 1));
100       arm64_user_regs.sp = regs.data[PERF_REG_ARM64_SP];
101       arm64_user_regs.pc = regs.data[PERF_REG_ARM64_PC];
102       auto regs =
103           static_cast<unwindstack::RegsArm64*>(unwindstack::RegsArm64::Read(&arm64_user_regs));
104       regs->SetPACMask(arm64_pac_mask_);
105       return regs;
106     }
107     case ARCH_X86_32: {
108       unwindstack::x86_user_regs x86_user_regs;
109       memset(&x86_user_regs, 0, sizeof(x86_user_regs));
110       x86_user_regs.eax = static_cast<uint32_t>(regs.data[PERF_REG_X86_AX]);
111       x86_user_regs.ebx = static_cast<uint32_t>(regs.data[PERF_REG_X86_BX]);
112       x86_user_regs.ecx = static_cast<uint32_t>(regs.data[PERF_REG_X86_CX]);
113       x86_user_regs.edx = static_cast<uint32_t>(regs.data[PERF_REG_X86_DX]);
114       x86_user_regs.ebp = static_cast<uint32_t>(regs.data[PERF_REG_X86_BP]);
115       x86_user_regs.edi = static_cast<uint32_t>(regs.data[PERF_REG_X86_DI]);
116       x86_user_regs.esi = static_cast<uint32_t>(regs.data[PERF_REG_X86_SI]);
117       x86_user_regs.esp = static_cast<uint32_t>(regs.data[PERF_REG_X86_SP]);
118       x86_user_regs.eip = static_cast<uint32_t>(regs.data[PERF_REG_X86_IP]);
119       return unwindstack::RegsX86::Read(&x86_user_regs);
120     }
121     case ARCH_X86_64: {
122       unwindstack::x86_64_user_regs x86_64_user_regs;
123       memset(&x86_64_user_regs, 0, sizeof(x86_64_user_regs));
124       x86_64_user_regs.rax = regs.data[PERF_REG_X86_AX];
125       x86_64_user_regs.rbx = regs.data[PERF_REG_X86_BX];
126       x86_64_user_regs.rcx = regs.data[PERF_REG_X86_CX];
127       x86_64_user_regs.rdx = regs.data[PERF_REG_X86_DX];
128       x86_64_user_regs.r8 = regs.data[PERF_REG_X86_R8];
129       x86_64_user_regs.r9 = regs.data[PERF_REG_X86_R9];
130       x86_64_user_regs.r10 = regs.data[PERF_REG_X86_R10];
131       x86_64_user_regs.r11 = regs.data[PERF_REG_X86_R11];
132       x86_64_user_regs.r12 = regs.data[PERF_REG_X86_R12];
133       x86_64_user_regs.r13 = regs.data[PERF_REG_X86_R13];
134       x86_64_user_regs.r14 = regs.data[PERF_REG_X86_R14];
135       x86_64_user_regs.r15 = regs.data[PERF_REG_X86_R15];
136       x86_64_user_regs.rdi = regs.data[PERF_REG_X86_DI];
137       x86_64_user_regs.rsi = regs.data[PERF_REG_X86_SI];
138       x86_64_user_regs.rbp = regs.data[PERF_REG_X86_BP];
139       x86_64_user_regs.rsp = regs.data[PERF_REG_X86_SP];
140       x86_64_user_regs.rip = regs.data[PERF_REG_X86_IP];
141       return unwindstack::RegsX86_64::Read(&x86_64_user_regs);
142     }
143     default:
144       return nullptr;
145   }
146 }
147 
CreateMapInfo(const MapEntry * entry)148 static std::shared_ptr<unwindstack::MapInfo> CreateMapInfo(const MapEntry* entry) {
149   std::string name_holder;
150   const char* name = entry->dso->GetDebugFilePath().data();
151   uint64_t pgoff = entry->pgoff;
152   auto tuple = SplitUrlInApk(entry->dso->GetDebugFilePath());
153   if (std::get<0>(tuple)) {
154     // The unwinder does not understand the ! format, so change back to
155     // the previous format (apk, offset).
156     EmbeddedElf* elf = ApkInspector::FindElfInApkByName(std::get<1>(tuple), std::get<2>(tuple));
157     if (elf != nullptr) {
158       name = elf->filepath().data();
159       pgoff += elf->entry_offset();
160     }
161   } else if (entry->flags & map_flags::PROT_JIT_SYMFILE_MAP) {
162     // Remove location_in_file suffix, which isn't recognized by libunwindstack.
163     const std::string& path = entry->dso->GetDebugFilePath();
164     if (JITDebugReader::IsPathInJITSymFile(path)) {
165       size_t colon_pos = path.rfind(':');
166       CHECK_NE(colon_pos, std::string::npos);
167       name_holder = path.substr(0, colon_pos);
168       name = name_holder.data();
169     }
170   }
171   return unwindstack::MapInfo::Create(entry->start_addr, entry->get_end_addr(), pgoff,
172                                       PROT_READ | entry->flags, name);
173 }
174 
UpdateMaps(const MapSet & map_set)175 void UnwindMaps::UpdateMaps(const MapSet& map_set) {
176   if (version_ == map_set.version) {
177     return;
178   }
179   version_ = map_set.version;
180   size_t i = 0;
181   size_t old_size = entries_.size();
182   bool has_removed_entry = false;
183   for (auto it = map_set.maps.begin(); it != map_set.maps.end();) {
184     const MapEntry* entry = it->second;
185     if (i < old_size && entry == entries_[i]) {
186       i++;
187       ++it;
188     } else if (i == old_size || entry->start_addr <= entries_[i]->start_addr) {
189       // Add an entry.
190       entries_.push_back(entry);
191       maps_.emplace_back(CreateMapInfo(entry));
192       ++it;
193     } else {
194       // Remove an entry.
195       has_removed_entry = true;
196       entries_[i] = nullptr;
197       maps_[i++] = nullptr;
198     }
199   }
200   while (i < old_size) {
201     has_removed_entry = true;
202     entries_[i] = nullptr;
203     maps_[i++] = nullptr;
204   }
205 
206   if (has_removed_entry) {
207     entries_.resize(std::remove(entries_.begin(), entries_.end(), nullptr) - entries_.begin());
208     maps_.resize(std::remove(maps_.begin(), maps_.end(), std::shared_ptr<unwindstack::MapInfo>()) -
209                  maps_.begin());
210   }
211 
212   std::sort(entries_.begin(), entries_.end(),
213             [](const auto& e1, const auto& e2) { return e1->start_addr < e2->start_addr; });
214   // Use Sort() to sort maps_ and create prev_real_map links.
215   // prev_real_map is needed by libunwindstack to find the start of an embedded lib in an apk.
216   // See http://b/120981155.
217   Sort();
218 }
219 
CollectMetaInfo(std::unordered_map<std::string,std::string> * info_map)220 void OfflineUnwinder::CollectMetaInfo(std::unordered_map<std::string, std::string>* info_map
221                                       __attribute__((unused))) {
222 #if defined(__aarch64__)
223   // Find pac_mask for ARMv8.3-A Pointer Authentication by below steps:
224   // 1. Create a 64 bit value with every bit set, but clear bit 55. Because linux user space uses
225   //    TTBR0.
226   // 2. Use XPACLRI to clear auth code bits.
227   // 3. Flip every bit to get pac_mask, excluding bit 55.
228   // We can also use ptrace(PTRACE_GETREGSET, pid, NT_ARM_PAC_MASK). But it needs a tracee.
229   register uint64_t x30 __asm("x30") = ~(1ULL << 55);
230   // This is XPACLRI on ARMv8.3-A, and nop on prev ARMv8.3-A.
231   asm("hint 0x7" : "+r"(x30));
232   uint64_t pac_mask = ~x30 & ~(1ULL << 55);
233   if (pac_mask != 0) {
234     (*info_map)[META_KEY_ARM64_PAC_MASK] = android::base::StringPrintf("0x%" PRIx64, pac_mask);
235   }
236 #endif
237 }
238 
LoadMetaInfo(const std::unordered_map<std::string,std::string> & info_map)239 void OfflineUnwinderImpl::LoadMetaInfo(
240     const std::unordered_map<std::string, std::string>& info_map) {
241   if (auto it = info_map.find(META_KEY_ARM64_PAC_MASK); it != info_map.end()) {
242     CHECK(android::base::ParseUint(it->second, &arm64_pac_mask_));
243   }
244 }
245 
UnwindCallChain(const ThreadEntry & thread,const RegSet & regs,const char * stack,size_t stack_size,std::vector<uint64_t> * ips,std::vector<uint64_t> * sps)246 bool OfflineUnwinderImpl::UnwindCallChain(const ThreadEntry& thread, const RegSet& regs,
247                                           const char* stack, size_t stack_size,
248                                           std::vector<uint64_t>* ips, std::vector<uint64_t>* sps) {
249   uint64_t start_time;
250   if (collect_stat_) {
251     start_time = GetSystemClock();
252   }
253   is_callchain_broken_for_incomplete_jit_debug_info_ = false;
254   ips->clear();
255   sps->clear();
256   std::vector<uint64_t> result;
257   uint64_t sp_reg_value;
258   if (!regs.GetSpRegValue(&sp_reg_value)) {
259     LOG(ERROR) << "can't get sp reg value";
260     return false;
261   }
262   uint64_t stack_addr = sp_reg_value;
263 
264   UnwindMaps& cached_map = cached_maps_[thread.pid];
265   cached_map.UpdateMaps(*thread.maps);
266   std::unique_ptr<unwindstack::Regs> unwind_regs(GetBacktraceRegs(regs));
267   if (!unwind_regs) {
268     return false;
269   }
270   unwindstack::Unwinder unwinder(
271       MAX_UNWINDING_FRAMES, &cached_map, unwind_regs.get(),
272       unwindstack::Memory::CreateOfflineMemory(reinterpret_cast<const uint8_t*>(stack), stack_addr,
273                                                stack_addr + stack_size));
274   unwinder.SetResolveNames(false);
275   unwinder.Unwind();
276   size_t last_jit_method_frame = UINT_MAX;
277   for (auto& frame : unwinder.frames()) {
278     // Unwinding in arm architecture can return 0 pc address.
279 
280     // If frame.map_info == nullptr, this frame doesn't hit any map, it could be:
281     // 1. In an executable map not backed by a file. Note that RecordCommand::ShouldOmitRecord()
282     //    may omit maps only exist memory.
283     // 2. An incorrectly unwound frame. Likely caused by invalid stack data, as in
284     //    SampleRecord::GetValidStackSize(). Or caused by incomplete JIT debug info.
285     // We want to remove this frame and callchains following it in either case.
286     if (frame.map_info == nullptr) {
287       is_callchain_broken_for_incomplete_jit_debug_info_ = true;
288       break;
289     }
290     if (frame.map_info->flags() & unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP) {
291       last_jit_method_frame = ips->size();
292     }
293     ips->push_back(frame.pc);
294     sps->push_back(frame.sp);
295   }
296   // If the unwound frames stop near to a JITed method, it may be caused by incomplete JIT debug
297   // info.
298   if (last_jit_method_frame != UINT_MAX && last_jit_method_frame + 3 > ips->size()) {
299     is_callchain_broken_for_incomplete_jit_debug_info_ = true;
300   }
301 
302   uint64_t ip_reg_value;
303   if (!regs.GetIpRegValue(&ip_reg_value)) {
304     LOG(ERROR) << "can't get ip reg value";
305     return false;
306   }
307   if (ips->empty()) {
308     ips->push_back(ip_reg_value);
309     sps->push_back(sp_reg_value);
310   } else {
311     // Check if the unwinder returns ip reg value as the first ip address in callstack.
312     CHECK_EQ((*ips)[0], ip_reg_value);
313   }
314   if (collect_stat_) {
315     unwinding_result_.used_time = GetSystemClock() - start_time;
316     unwinding_result_.error_code = unwinder.LastErrorCode();
317     unwinding_result_.error_addr = unwinder.LastErrorAddress();
318     unwinding_result_.stack_start = stack_addr;
319     unwinding_result_.stack_end = stack_addr + stack_size;
320   }
321   return true;
322 }
323 
Create(bool collect_stat)324 std::unique_ptr<OfflineUnwinder> OfflineUnwinder::Create(bool collect_stat) {
325   return std::unique_ptr<OfflineUnwinder>(new OfflineUnwinderImpl(collect_stat));
326 }
327 
328 }  // namespace simpleperf
329