blob: d76a5013a39ce5a4944986122bd17db4136eed1d [file] [log] [blame]
Alex Light543d8452018-07-13 16:25:58 +00001/*
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 Light543d8452018-07-13 16:25:58 +000021#include <sys/types.h>
David Srbeckyf1603942019-04-05 12:06:36 +010022#include <unistd.h>
David Srbeckya3e12402021-03-23 13:36:55 +000023#include <iomanip>
David Srbeckyf1603942019-04-05 12:06:36 +010024
Christopher Ferrisec232912019-05-28 16:18:18 -070025#include "unwindstack/Regs.h"
David Srbeckyf1603942019-04-05 12:06:36 +010026#include "unwindstack/RegsGetLocal.h"
Christopher Ferrisec232912019-05-28 16:18:18 -070027#include "unwindstack/Memory.h"
28#include "unwindstack/Unwinder.h"
Alex Light543d8452018-07-13 16:25:58 +000029
David Srbecky2f78a9c2020-01-17 15:53:42 +000030#include "base/bit_utils.h"
31#include "entrypoints/runtime_asm_entrypoints.h"
Alex Light543d8452018-07-13 16:25:58 +000032#include "thread-inl.h"
33
34#else
35
36// For UNUSED
37#include "base/macros.h"
38
39#endif
40
41namespace art {
42
Christopher Ferrisec232912019-05-28 16:18:18 -070043// We only really support libunwindstack on linux which is unfortunate but since this is only for
Alex Light543d8452018-07-13 16:25:58 +000044// gcstress this isn't a huge deal.
45#if defined(__linux__)
46
David Srbeckya3e12402021-03-23 13:36:55 +000047// Strict integrity check of the backtrace:
48// All methods must have a name, all the way to "main".
Roland Levillain0886d4e2021-06-14 17:59:42 +000049static constexpr bool kStrictUnwindChecks = false;
David Srbeckya3e12402021-03-23 13:36:55 +000050
David Srbeckyf1603942019-04-05 12:06:36 +010051struct UnwindHelper : public TLSData {
52 static constexpr const char* kTlsKey = "UnwindHelper::kTlsKey";
Alex Light543d8452018-07-13 16:25:58 +000053
David Srbeckyf1603942019-04-05 12:06:36 +010054 explicit UnwindHelper(size_t max_depth)
David Srbeckyf622d892021-03-12 08:30:29 +000055 : arch_(unwindstack::Regs::CurrentArch()),
David Srbecky6958df92021-05-07 17:40:34 +010056 memory_(unwindstack::Memory::CreateProcessMemoryThreadCached(getpid())),
David Srbeckyf622d892021-03-12 08:30:29 +000057 jit_(unwindstack::CreateJitDebug(arch_, memory_)),
58 dex_(unwindstack::CreateDexFiles(arch_, memory_)),
David Srbeckyf1603942019-04-05 12:06:36 +010059 unwinder_(max_depth, &maps_, memory_) {
60 CHECK(maps_.Parse());
David Srbeckyf622d892021-03-12 08:30:29 +000061 unwinder_.SetArch(arch_);
62 unwinder_.SetJitDebug(jit_.get());
63 unwinder_.SetDexFiles(dex_.get());
David Srbeckya3e12402021-03-23 13:36:55 +000064 unwinder_.SetResolveNames(kStrictUnwindChecks);
David Srbeckyf1603942019-04-05 12:06:36 +010065 unwindstack::Elf::SetCachingEnabled(true);
66 }
Alex Light543d8452018-07-13 16:25:58 +000067
David Srbecky2f78a9c2020-01-17 15:53:42 +000068 // Reparse process mmaps to detect newly loaded libraries.
David Srbecky8e270af2021-05-12 16:40:58 +010069 bool Reparse(bool* any_changed) { return maps_.Reparse(any_changed); }
David Srbecky2f78a9c2020-01-17 15:53:42 +000070
David Srbeckyf1603942019-04-05 12:06:36 +010071 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 Srbecky2f78a9c2020-01-17 15:53:42 +000083 unwindstack::LocalUpdatableMaps maps_;
David Srbeckyf622d892021-03-12 08:30:29 +000084 unwindstack::ArchEnum arch_;
David Srbeckyf1603942019-04-05 12:06:36 +010085 std::shared_ptr<unwindstack::Memory> memory_;
David Srbeckyf622d892021-03-12 08:30:29 +000086 std::unique_ptr<unwindstack::JitDebug> jit_;
87 std::unique_ptr<unwindstack::DexFiles> dex_;
David Srbeckyf1603942019-04-05 12:06:36 +010088 unwindstack::Unwinder unwinder_;
Alex Light543d8452018-07-13 16:25:58 +000089};
90
Alex Light543d8452018-07-13 16:25:58 +000091void BacktraceCollector::Collect() {
David Srbeckya3e12402021-03-23 13:36:55 +000092 unwindstack::Unwinder* unwinder = UnwindHelper::Get(Thread::Current(), max_depth_)->Unwinder();
93 if (!CollectImpl(unwinder)) {
David Srbecky8e270af2021-05-12 16:40:58 +010094 // 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 Srbeckya3e12402021-03-23 13:36:55 +000099 if (kStrictUnwindChecks) {
David Srbeckya3e12402021-03-23 13:36:55 +0000100 std::vector<unwindstack::FrameData>& frames = unwinder->frames();
David Srbecky8e270af2021-05-12 16:40:58 +0100101 LOG(ERROR) << "Failed to unwind stack (error " << unwinder->LastErrorCodeString() << "):";
Christopher Ferris3ba3a772021-11-10 17:24:11 -0800102 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 Srbeckya3e12402021-03-23 13:36:55 +0000112 }
David Srbecky8e270af2021-05-12 16:40:58 +0100113 LOG(ERROR) << " pc " << std::setw(8) << std::setfill('0') << std::hex <<
Christopher Ferris3ba3a772021-11-10 17:24:11 -0800114 frame.rel_pc << " " << frame.function_name.c_str();
David Srbeckya3e12402021-03-23 13:36:55 +0000115 }
116 LOG(FATAL);
117 }
David Srbecky2f78a9c2020-01-17 15:53:42 +0000118 }
119 }
120}
121
David Srbeckya3e12402021-03-23 13:36:55 +0000122bool BacktraceCollector::CollectImpl(unwindstack::Unwinder* unwinder) {
David Srbeckyf1603942019-04-05 12:06:36 +0100123 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal());
124 RegsGetLocal(regs.get());
125 unwinder->SetRegs(regs.get());
126 unwinder->Unwind();
David Srbecky2f78a9c2020-01-17 15:53:42 +0000127
David Srbeckyf1603942019-04-05 12:06:36 +0100128 num_frames_ = 0;
129 if (unwinder->NumFrames() > skip_count_) {
David Srbecky2f78a9c2020-01-17 15:53:42 +0000130 for (auto it = unwinder->frames().begin() + skip_count_; it != unwinder->frames().end(); ++it) {
131 CHECK_LT(num_frames_, max_depth_);
David Srbeckyf1603942019-04-05 12:06:36 +0100132 out_frames_[num_frames_++] = static_cast<uintptr_t>(it->pc);
David Srbecky2f78a9c2020-01-17 15:53:42 +0000133
134 // Expected early end: Instrumentation breaks unwinding (b/138296821).
David Srbecky8e270af2021-05-12 16:40:58 +0100135 // 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 Srbecky2f78a9c2020-01-17 15:53:42 +0000139 return true;
140 }
David Srbeckya3e12402021-03-23 13:36:55 +0000141
142 if (kStrictUnwindChecks) {
143 if (it->function_name.empty()) {
144 return false;
David Srbecky8e270af2021-05-12 16:40:58 +0100145 }
146 if (it->function_name == "main" ||
147 it->function_name == "start_thread" ||
148 it->function_name == "__start_thread") {
David Srbeckya3e12402021-03-23 13:36:55 +0000149 return true;
150 }
151 }
David Srbeckyf1603942019-04-05 12:06:36 +0100152 }
Alex Light543d8452018-07-13 16:25:58 +0000153 }
David Srbecky2f78a9c2020-01-17 15:53:42 +0000154
David Srbecky8e270af2021-05-12 16:40:58 +0100155 unwindstack::ErrorCode error = unwinder->LastErrorCode();
156 return error == unwindstack::ERROR_NONE || error == unwindstack::ERROR_MAX_FRAMES_EXCEEDED;
Alex Light543d8452018-07-13 16:25:58 +0000157}
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.
168void 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