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