• 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 <android-base/logging.h>
20 #include <backtrace/Backtrace.h>
21 #include <backtrace/BacktraceMap.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/Regs.h>
27 #include <unwindstack/RegsArm.h>
28 #include <unwindstack/RegsArm64.h>
29 #include <unwindstack/RegsX86.h>
30 #include <unwindstack/RegsX86_64.h>
31 #include <unwindstack/UserArm.h>
32 #include <unwindstack/UserArm64.h>
33 #include <unwindstack/UserX86.h>
34 #include <unwindstack/UserX86_64.h>
35 
36 #include "environment.h"
37 #include "perf_regs.h"
38 #include "read_apk.h"
39 #include "thread_tree.h"
40 
41 namespace simpleperf {
42 
GetBacktraceRegs(const RegSet & regs)43 static unwindstack::Regs* GetBacktraceRegs(const RegSet& regs) {
44   switch (regs.arch) {
45     case ARCH_ARM: {
46       unwindstack::arm_user_regs arm_user_regs;
47       memset(&arm_user_regs, 0, sizeof(arm_user_regs));
48       static_assert(
49           static_cast<int>(unwindstack::ARM_REG_R0) == static_cast<int>(PERF_REG_ARM_R0), "");
50       static_assert(
51           static_cast<int>(unwindstack::ARM_REG_LAST) == static_cast<int>(PERF_REG_ARM_MAX), "");
52       for (size_t i = unwindstack::ARM_REG_R0; i < unwindstack::ARM_REG_LAST; ++i) {
53         arm_user_regs.regs[i] = static_cast<uint32_t>(regs.data[i]);
54       }
55       return unwindstack::RegsArm::Read(&arm_user_regs);
56     }
57     case ARCH_ARM64: {
58       unwindstack::arm64_user_regs arm64_user_regs;
59       memset(&arm64_user_regs, 0, sizeof(arm64_user_regs));
60       static_assert(
61           static_cast<int>(unwindstack::ARM64_REG_R0) == static_cast<int>(PERF_REG_ARM64_X0), "");
62       static_assert(
63           static_cast<int>(unwindstack::ARM64_REG_R30) == static_cast<int>(PERF_REG_ARM64_LR), "");
64       memcpy(&arm64_user_regs.regs[unwindstack::ARM64_REG_R0], &regs.data[PERF_REG_ARM64_X0],
65              sizeof(uint64_t) * (PERF_REG_ARM64_LR - PERF_REG_ARM64_X0 + 1));
66       arm64_user_regs.sp = regs.data[PERF_REG_ARM64_SP];
67       arm64_user_regs.pc = regs.data[PERF_REG_ARM64_PC];
68       return unwindstack::RegsArm64::Read(&arm64_user_regs);
69     }
70     case ARCH_X86_32: {
71       unwindstack::x86_user_regs x86_user_regs;
72       memset(&x86_user_regs, 0, sizeof(x86_user_regs));
73       x86_user_regs.eax = static_cast<uint32_t>(regs.data[PERF_REG_X86_AX]);
74       x86_user_regs.ebx = static_cast<uint32_t>(regs.data[PERF_REG_X86_BX]);
75       x86_user_regs.ecx = static_cast<uint32_t>(regs.data[PERF_REG_X86_CX]);
76       x86_user_regs.edx = static_cast<uint32_t>(regs.data[PERF_REG_X86_DX]);
77       x86_user_regs.ebp = static_cast<uint32_t>(regs.data[PERF_REG_X86_BP]);
78       x86_user_regs.edi = static_cast<uint32_t>(regs.data[PERF_REG_X86_DI]);
79       x86_user_regs.esi = static_cast<uint32_t>(regs.data[PERF_REG_X86_SI]);
80       x86_user_regs.esp = static_cast<uint32_t>(regs.data[PERF_REG_X86_SP]);
81       x86_user_regs.eip = static_cast<uint32_t>(regs.data[PERF_REG_X86_IP]);
82       return unwindstack::RegsX86::Read(&x86_user_regs);
83     }
84     case ARCH_X86_64: {
85       unwindstack::x86_64_user_regs x86_64_user_regs;
86       memset(&x86_64_user_regs, 0, sizeof(x86_64_user_regs));
87       x86_64_user_regs.rax = regs.data[PERF_REG_X86_AX];
88       x86_64_user_regs.rbx = regs.data[PERF_REG_X86_BX];
89       x86_64_user_regs.rcx = regs.data[PERF_REG_X86_CX];
90       x86_64_user_regs.rdx = regs.data[PERF_REG_X86_DX];
91       x86_64_user_regs.r8 = regs.data[PERF_REG_X86_R8];
92       x86_64_user_regs.r9 = regs.data[PERF_REG_X86_R9];
93       x86_64_user_regs.r10 = regs.data[PERF_REG_X86_R10];
94       x86_64_user_regs.r11 = regs.data[PERF_REG_X86_R11];
95       x86_64_user_regs.r12 = regs.data[PERF_REG_X86_R12];
96       x86_64_user_regs.r13 = regs.data[PERF_REG_X86_R13];
97       x86_64_user_regs.r14 = regs.data[PERF_REG_X86_R14];
98       x86_64_user_regs.r15 = regs.data[PERF_REG_X86_R15];
99       x86_64_user_regs.rdi = regs.data[PERF_REG_X86_DI];
100       x86_64_user_regs.rsi = regs.data[PERF_REG_X86_SI];
101       x86_64_user_regs.rbp = regs.data[PERF_REG_X86_BP];
102       x86_64_user_regs.rsp = regs.data[PERF_REG_X86_SP];
103       x86_64_user_regs.rip = regs.data[PERF_REG_X86_IP];
104       return unwindstack::RegsX86_64::Read(&x86_64_user_regs);
105     }
106     default:
107       return nullptr;
108   }
109 }
110 
OfflineUnwinder(bool collect_stat)111 OfflineUnwinder::OfflineUnwinder(bool collect_stat) : collect_stat_(collect_stat) {
112   Backtrace::SetGlobalElfCache(true);
113 }
114 
UnwindCallChain(const ThreadEntry & thread,const RegSet & regs,const char * stack,size_t stack_size,std::vector<uint64_t> * ips,std::vector<uint64_t> * sps)115 bool OfflineUnwinder::UnwindCallChain(const ThreadEntry& thread, const RegSet& regs,
116                                       const char* stack, size_t stack_size,
117                                       std::vector<uint64_t>* ips, std::vector<uint64_t>* sps) {
118   uint64_t start_time;
119   if (collect_stat_) {
120     start_time = GetSystemClock();
121   }
122   std::vector<uint64_t> result;
123   uint64_t sp_reg_value;
124   if (!regs.GetSpRegValue(&sp_reg_value)) {
125     LOG(ERROR) << "can't get sp reg value";
126     return false;
127   }
128   uint64_t stack_addr = sp_reg_value;
129 
130   // Create map only if the maps have changed since the last unwind.
131   auto map_it = cached_maps_.find(thread.pid);
132   CachedMap& cached_map = (map_it == cached_maps_.end() ? cached_maps_[thread.pid]
133                                                         : map_it->second);
134   if (cached_map.version < thread.maps->version) {
135     std::vector<backtrace_map_t> bt_maps(thread.maps->maps.size());
136     size_t map_index = 0;
137     for (auto& map : thread.maps->maps) {
138       backtrace_map_t& bt_map = bt_maps[map_index++];
139       bt_map.start = map->start_addr;
140       bt_map.end = map->start_addr + map->len;
141       bt_map.offset = map->pgoff;
142       bt_map.name = map->dso->GetDebugFilePath();
143       if (bt_map.offset == 0) {
144         size_t apk_pos = bt_map.name.find_last_of('!');
145         if (apk_pos != std::string::npos) {
146           // The unwinder does not understand the ! format, so change back to
147           // the previous format (apk, offset).
148           std::string shared_lib(bt_map.name.substr(apk_pos + 2));
149           bt_map.name = bt_map.name.substr(0, apk_pos);
150           uint64_t offset;
151           uint32_t length;
152           if (ApkInspector::FindOffsetInApkByName(bt_map.name, shared_lib, &offset, &length)) {
153             bt_map.offset = offset;
154           }
155         }
156       }
157       bt_map.flags = PROT_READ | PROT_EXEC;
158     }
159     cached_map.map.reset(BacktraceMap::CreateOffline(thread.pid, bt_maps));
160     if (!cached_map.map) {
161       return false;
162     }
163     // Disable the resolving of names, this data is not used.
164     cached_map.map->SetResolveNames(false);
165     cached_map.version = thread.maps->version;
166   }
167 
168   backtrace_stackinfo_t stack_info;
169   stack_info.start = stack_addr;
170   stack_info.end = stack_addr + stack_size;
171   stack_info.data = reinterpret_cast<const uint8_t*>(stack);
172 
173   std::unique_ptr<unwindstack::Regs> unwind_regs(GetBacktraceRegs(regs));
174   if (!unwind_regs) {
175     return false;
176   }
177   std::vector<backtrace_frame_data_t> frames;
178   BacktraceUnwindError error;
179   if (Backtrace::UnwindOffline(unwind_regs.get(), cached_map.map.get(), stack_info, &frames, &error)) {
180     for (auto& frame : frames) {
181       // Unwinding in arm architecture can return 0 pc address.
182       if (frame.pc == 0) {
183         break;
184       }
185       ips->push_back(frame.pc);
186       sps->push_back(frame.sp);
187     }
188   }
189 
190   uint64_t ip_reg_value;
191   if (!regs.GetIpRegValue(&ip_reg_value)) {
192     LOG(ERROR) << "can't get ip reg value";
193     return false;
194   }
195   if (ips->empty()) {
196     ips->push_back(ip_reg_value);
197     sps->push_back(sp_reg_value);
198   } else {
199     // Check if the unwinder returns ip reg value as the first ip address in callstack.
200     CHECK_EQ((*ips)[0], ip_reg_value);
201   }
202   if (collect_stat_) {
203     unwinding_result_.used_time = GetSystemClock() - start_time;
204     switch (error.error_code) {
205       case BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT:
206         unwinding_result_.stop_reason = UnwindingResult::EXCEED_MAX_FRAMES_LIMIT;
207         break;
208       case BACKTRACE_UNWIND_ERROR_ACCESS_REG_FAILED:
209         unwinding_result_.stop_reason = UnwindingResult::ACCESS_REG_FAILED;
210         unwinding_result_.stop_info.regno = error.error_info.regno;
211         break;
212       case BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED:
213         // Because we don't have precise stack range here, just guess an addr is in stack
214         // if sp - 128K <= addr <= sp.
215         if (error.error_info.addr <= stack_addr &&
216             error.error_info.addr >= stack_addr - 128 * 1024) {
217           unwinding_result_.stop_reason = UnwindingResult::ACCESS_STACK_FAILED;
218         } else {
219           unwinding_result_.stop_reason = UnwindingResult::ACCESS_MEM_FAILED;
220         }
221         unwinding_result_.stop_info.addr = error.error_info.addr;
222         break;
223       case BACKTRACE_UNWIND_ERROR_FIND_PROC_INFO_FAILED:
224         unwinding_result_.stop_reason = UnwindingResult::FIND_PROC_INFO_FAILED;
225         break;
226       case BACKTRACE_UNWIND_ERROR_EXECUTE_DWARF_INSTRUCTION_FAILED:
227         unwinding_result_.stop_reason = UnwindingResult::EXECUTE_DWARF_INSTRUCTION_FAILED;
228         break;
229       case BACKTRACE_UNWIND_ERROR_MAP_MISSING:
230         unwinding_result_.stop_reason = UnwindingResult::MAP_MISSING;
231         break;
232       default:
233         unwinding_result_.stop_reason = UnwindingResult::UNKNOWN_REASON;
234         break;
235     }
236     unwinding_result_.stack_start = stack_info.start;
237     unwinding_result_.stack_end = stack_info.end;
238   }
239   return true;
240 }
241 
242 }  // namespace simpleperf
243