Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "backtrace_helper.h" |
| 18 | |
| 19 | #if defined(__linux__) |
| 20 | |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 21 | #include <sys/types.h> |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 22 | #include <unistd.h> |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 23 | #include <iomanip> |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 24 | |
Christopher Ferris | ec23291 | 2019-05-28 16:18:18 -0700 | [diff] [blame] | 25 | #include "unwindstack/Regs.h" |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 26 | #include "unwindstack/RegsGetLocal.h" |
Christopher Ferris | ec23291 | 2019-05-28 16:18:18 -0700 | [diff] [blame] | 27 | #include "unwindstack/Memory.h" |
| 28 | #include "unwindstack/Unwinder.h" |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 29 | |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 30 | #include "base/bit_utils.h" |
| 31 | #include "entrypoints/runtime_asm_entrypoints.h" |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 32 | #include "thread-inl.h" |
| 33 | |
| 34 | #else |
| 35 | |
| 36 | // For UNUSED |
| 37 | #include "base/macros.h" |
| 38 | |
| 39 | #endif |
| 40 | |
| 41 | namespace art { |
| 42 | |
Christopher Ferris | ec23291 | 2019-05-28 16:18:18 -0700 | [diff] [blame] | 43 | // We only really support libunwindstack on linux which is unfortunate but since this is only for |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 44 | // gcstress this isn't a huge deal. |
| 45 | #if defined(__linux__) |
| 46 | |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 47 | // Strict integrity check of the backtrace: |
| 48 | // All methods must have a name, all the way to "main". |
Roland Levillain | 0886d4e | 2021-06-14 17:59:42 +0000 | [diff] [blame] | 49 | static constexpr bool kStrictUnwindChecks = false; |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 50 | |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 51 | struct UnwindHelper : public TLSData { |
| 52 | static constexpr const char* kTlsKey = "UnwindHelper::kTlsKey"; |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 53 | |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 54 | explicit UnwindHelper(size_t max_depth) |
David Srbecky | f622d89 | 2021-03-12 08:30:29 +0000 | [diff] [blame] | 55 | : arch_(unwindstack::Regs::CurrentArch()), |
David Srbecky | 6958df9 | 2021-05-07 17:40:34 +0100 | [diff] [blame] | 56 | memory_(unwindstack::Memory::CreateProcessMemoryThreadCached(getpid())), |
David Srbecky | f622d89 | 2021-03-12 08:30:29 +0000 | [diff] [blame] | 57 | jit_(unwindstack::CreateJitDebug(arch_, memory_)), |
| 58 | dex_(unwindstack::CreateDexFiles(arch_, memory_)), |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 59 | unwinder_(max_depth, &maps_, memory_) { |
| 60 | CHECK(maps_.Parse()); |
David Srbecky | f622d89 | 2021-03-12 08:30:29 +0000 | [diff] [blame] | 61 | unwinder_.SetArch(arch_); |
| 62 | unwinder_.SetJitDebug(jit_.get()); |
| 63 | unwinder_.SetDexFiles(dex_.get()); |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 64 | unwinder_.SetResolveNames(kStrictUnwindChecks); |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 65 | unwindstack::Elf::SetCachingEnabled(true); |
| 66 | } |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 67 | |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 68 | // Reparse process mmaps to detect newly loaded libraries. |
David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 69 | bool Reparse(bool* any_changed) { return maps_.Reparse(any_changed); } |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 70 | |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 71 | static UnwindHelper* Get(Thread* self, size_t max_depth) { |
| 72 | UnwindHelper* tls = reinterpret_cast<UnwindHelper*>(self->GetCustomTLS(kTlsKey)); |
| 73 | if (tls == nullptr) { |
| 74 | tls = new UnwindHelper(max_depth); |
| 75 | self->SetCustomTLS(kTlsKey, tls); |
| 76 | } |
| 77 | return tls; |
| 78 | } |
| 79 | |
| 80 | unwindstack::Unwinder* Unwinder() { return &unwinder_; } |
| 81 | |
| 82 | private: |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 83 | unwindstack::LocalUpdatableMaps maps_; |
David Srbecky | f622d89 | 2021-03-12 08:30:29 +0000 | [diff] [blame] | 84 | unwindstack::ArchEnum arch_; |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 85 | std::shared_ptr<unwindstack::Memory> memory_; |
David Srbecky | f622d89 | 2021-03-12 08:30:29 +0000 | [diff] [blame] | 86 | std::unique_ptr<unwindstack::JitDebug> jit_; |
| 87 | std::unique_ptr<unwindstack::DexFiles> dex_; |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 88 | unwindstack::Unwinder unwinder_; |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 91 | void BacktraceCollector::Collect() { |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 92 | unwindstack::Unwinder* unwinder = UnwindHelper::Get(Thread::Current(), max_depth_)->Unwinder(); |
| 93 | if (!CollectImpl(unwinder)) { |
David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 94 | // Reparse process mmaps to detect newly loaded libraries and retry, |
| 95 | // but only if any maps changed (we don't want to hide racy failures). |
| 96 | bool any_changed; |
| 97 | UnwindHelper::Get(Thread::Current(), max_depth_)->Reparse(&any_changed); |
| 98 | if (!any_changed || !CollectImpl(unwinder)) { |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 99 | if (kStrictUnwindChecks) { |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 100 | std::vector<unwindstack::FrameData>& frames = unwinder->frames(); |
David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 101 | LOG(ERROR) << "Failed to unwind stack (error " << unwinder->LastErrorCodeString() << "):"; |
Christopher Ferris | 3ba3a77 | 2021-11-10 17:24:11 -0800 | [diff] [blame] | 102 | std::string prev_name; |
| 103 | for (auto& frame : frames) { |
| 104 | if (frame.map_info != nullptr) { |
| 105 | std::string full_name = frame.map_info->GetFullName(); |
| 106 | if (prev_name != full_name) { |
| 107 | LOG(ERROR) << " in " << full_name; |
| 108 | } |
| 109 | prev_name = full_name; |
| 110 | } else { |
| 111 | prev_name = ""; |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 112 | } |
David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 113 | LOG(ERROR) << " pc " << std::setw(8) << std::setfill('0') << std::hex << |
Christopher Ferris | 3ba3a77 | 2021-11-10 17:24:11 -0800 | [diff] [blame] | 114 | frame.rel_pc << " " << frame.function_name.c_str(); |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 115 | } |
| 116 | LOG(FATAL); |
| 117 | } |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 122 | bool BacktraceCollector::CollectImpl(unwindstack::Unwinder* unwinder) { |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 123 | std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal()); |
| 124 | RegsGetLocal(regs.get()); |
| 125 | unwinder->SetRegs(regs.get()); |
| 126 | unwinder->Unwind(); |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 127 | |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 128 | num_frames_ = 0; |
| 129 | if (unwinder->NumFrames() > skip_count_) { |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 130 | for (auto it = unwinder->frames().begin() + skip_count_; it != unwinder->frames().end(); ++it) { |
| 131 | CHECK_LT(num_frames_, max_depth_); |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 132 | out_frames_[num_frames_++] = static_cast<uintptr_t>(it->pc); |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 133 | |
| 134 | // Expected early end: Instrumentation breaks unwinding (b/138296821). |
David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 135 | // Inexact compare because the unwinder does not give us exact return address, |
| 136 | // but rather it tries to guess the address of the preceding call instruction. |
| 137 | size_t exit_pc = reinterpret_cast<size_t>(GetQuickInstrumentationExitPc()); |
| 138 | if (exit_pc - 4 <= it->pc && it->pc <= exit_pc) { |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 139 | return true; |
| 140 | } |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 141 | |
| 142 | if (kStrictUnwindChecks) { |
| 143 | if (it->function_name.empty()) { |
| 144 | return false; |
David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 145 | } |
| 146 | if (it->function_name == "main" || |
| 147 | it->function_name == "start_thread" || |
| 148 | it->function_name == "__start_thread") { |
David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | } |
David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 152 | } |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 153 | } |
David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 154 | |
David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 155 | unwindstack::ErrorCode error = unwinder->LastErrorCode(); |
| 156 | return error == unwindstack::ERROR_NONE || error == unwindstack::ERROR_MAX_FRAMES_EXCEEDED; |
Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | #else |
| 160 | |
| 161 | #pragma clang diagnostic push |
| 162 | #pragma clang diagnostic warning "-W#warnings" |
| 163 | #warning "Backtrace collector is not implemented. GCStress cannot be used." |
| 164 | #pragma clang diagnostic pop |
| 165 | |
| 166 | // We only have an implementation for linux. On other plaforms just return nothing. This is not |
| 167 | // really correct but we only use this for hashing and gcstress so it's not too big a deal. |
| 168 | void BacktraceCollector::Collect() { |
| 169 | UNUSED(skip_count_); |
| 170 | UNUSED(out_frames_); |
| 171 | UNUSED(max_depth_); |
| 172 | num_frames_ = 0; |
| 173 | } |
| 174 | |
| 175 | #endif |
| 176 | |
| 177 | } // namespace art |