| 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() << "):"; |
| David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 102 | for (auto it = frames.begin(); it != frames.end(); it++) { |
| 103 | if (it == frames.begin() || std::prev(it)->map_name != it->map_name) { |
| David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 104 | LOG(ERROR) << " in " << it->map_name.c_str(); |
| David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 105 | } |
| David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 106 | LOG(ERROR) << " pc " << std::setw(8) << std::setfill('0') << std::hex << |
| 107 | it->rel_pc << " " << it->function_name.c_str(); |
| David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 108 | } |
| 109 | LOG(FATAL); |
| 110 | } |
| David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 115 | bool BacktraceCollector::CollectImpl(unwindstack::Unwinder* unwinder) { |
| David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 116 | std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal()); |
| 117 | RegsGetLocal(regs.get()); |
| 118 | unwinder->SetRegs(regs.get()); |
| 119 | unwinder->Unwind(); |
| David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 120 | |
| David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 121 | num_frames_ = 0; |
| 122 | if (unwinder->NumFrames() > skip_count_) { |
| David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 123 | for (auto it = unwinder->frames().begin() + skip_count_; it != unwinder->frames().end(); ++it) { |
| 124 | CHECK_LT(num_frames_, max_depth_); |
| David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 125 | out_frames_[num_frames_++] = static_cast<uintptr_t>(it->pc); |
| David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 126 | |
| 127 | // Expected early end: Instrumentation breaks unwinding (b/138296821). |
| David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 128 | // Inexact compare because the unwinder does not give us exact return address, |
| 129 | // but rather it tries to guess the address of the preceding call instruction. |
| 130 | size_t exit_pc = reinterpret_cast<size_t>(GetQuickInstrumentationExitPc()); |
| 131 | if (exit_pc - 4 <= it->pc && it->pc <= exit_pc) { |
| David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 132 | return true; |
| 133 | } |
| David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 134 | |
| 135 | if (kStrictUnwindChecks) { |
| 136 | if (it->function_name.empty()) { |
| 137 | return false; |
| David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 138 | } |
| 139 | if (it->function_name == "main" || |
| 140 | it->function_name == "start_thread" || |
| 141 | it->function_name == "__start_thread") { |
| David Srbecky | a3e1240 | 2021-03-23 13:36:55 +0000 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | } |
| David Srbecky | f160394 | 2019-04-05 12:06:36 +0100 | [diff] [blame] | 145 | } |
| Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 146 | } |
| David Srbecky | 2f78a9c | 2020-01-17 15:53:42 +0000 | [diff] [blame] | 147 | |
| David Srbecky | 8e270af | 2021-05-12 16:40:58 +0100 | [diff] [blame] | 148 | unwindstack::ErrorCode error = unwinder->LastErrorCode(); |
| 149 | return error == unwindstack::ERROR_NONE || error == unwindstack::ERROR_MAX_FRAMES_EXCEEDED; |
| Alex Light | 543d845 | 2018-07-13 16:25:58 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | #else |
| 153 | |
| 154 | #pragma clang diagnostic push |
| 155 | #pragma clang diagnostic warning "-W#warnings" |
| 156 | #warning "Backtrace collector is not implemented. GCStress cannot be used." |
| 157 | #pragma clang diagnostic pop |
| 158 | |
| 159 | // We only have an implementation for linux. On other plaforms just return nothing. This is not |
| 160 | // really correct but we only use this for hashing and gcstress so it's not too big a deal. |
| 161 | void BacktraceCollector::Collect() { |
| 162 | UNUSED(skip_count_); |
| 163 | UNUSED(out_frames_); |
| 164 | UNUSED(max_depth_); |
| 165 | num_frames_ = 0; |
| 166 | } |
| 167 | |
| 168 | #endif |
| 169 | |
| 170 | } // namespace art |