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
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include "unwindstack/Regs.h"
25 #include "unwindstack/RegsGetLocal.h"
26 #include "unwindstack/Memory.h"
27 #include "unwindstack/Unwinder.h"
28
29 #include "base/bit_utils.h"
30 #include "entrypoints/runtime_asm_entrypoints.h"
31 #include "thread-inl.h"
32
33 #else
34
35 // For UNUSED
36 #include "base/macros.h"
37
38 #endif
39
40 namespace art {
41
42 // We only really support libunwindstack on linux which is unfortunate but since this is only for
43 // gcstress this isn't a huge deal.
44 #if defined(__linux__)
45
46 struct UnwindHelper : public TLSData {
47 static constexpr const char* kTlsKey = "UnwindHelper::kTlsKey";
48
UnwindHelperart::UnwindHelper49 explicit UnwindHelper(size_t max_depth)
50 : memory_(unwindstack::Memory::CreateProcessMemory(getpid())),
51 jit_(memory_),
52 dex_(memory_),
53 unwinder_(max_depth, &maps_, memory_) {
54 CHECK(maps_.Parse());
55 unwinder_.SetJitDebug(&jit_, unwindstack::Regs::CurrentArch());
56 unwinder_.SetDexFiles(&dex_, unwindstack::Regs::CurrentArch());
57 unwinder_.SetResolveNames(false);
58 unwindstack::Elf::SetCachingEnabled(true);
59 }
60
61 // Reparse process mmaps to detect newly loaded libraries.
Reparseart::UnwindHelper62 bool Reparse() { return maps_.Reparse(); }
63
Getart::UnwindHelper64 static UnwindHelper* Get(Thread* self, size_t max_depth) {
65 UnwindHelper* tls = reinterpret_cast<UnwindHelper*>(self->GetCustomTLS(kTlsKey));
66 if (tls == nullptr) {
67 tls = new UnwindHelper(max_depth);
68 self->SetCustomTLS(kTlsKey, tls);
69 }
70 return tls;
71 }
72
Unwinderart::UnwindHelper73 unwindstack::Unwinder* Unwinder() { return &unwinder_; }
74
75 private:
76 unwindstack::LocalUpdatableMaps maps_;
77 std::shared_ptr<unwindstack::Memory> memory_;
78 unwindstack::JitDebug jit_;
79 unwindstack::DexFiles dex_;
80 unwindstack::Unwinder unwinder_;
81 };
82
Collect()83 void BacktraceCollector::Collect() {
84 if (!CollectImpl()) {
85 // Reparse process mmaps to detect newly loaded libraries and retry.
86 UnwindHelper::Get(Thread::Current(), max_depth_)->Reparse();
87 if (!CollectImpl()) {
88 // Failed to unwind stack. Ignore for now.
89 }
90 }
91 }
92
CollectImpl()93 bool BacktraceCollector::CollectImpl() {
94 unwindstack::Unwinder* unwinder = UnwindHelper::Get(Thread::Current(), max_depth_)->Unwinder();
95 std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal());
96 RegsGetLocal(regs.get());
97 unwinder->SetRegs(regs.get());
98 unwinder->Unwind();
99
100 num_frames_ = 0;
101 if (unwinder->NumFrames() > skip_count_) {
102 for (auto it = unwinder->frames().begin() + skip_count_; it != unwinder->frames().end(); ++it) {
103 CHECK_LT(num_frames_, max_depth_);
104 out_frames_[num_frames_++] = static_cast<uintptr_t>(it->pc);
105
106 // Expected early end: Instrumentation breaks unwinding (b/138296821).
107 size_t align = GetInstructionSetAlignment(kRuntimeISA);
108 if (RoundUp(it->pc, align) == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
109 return true;
110 }
111 }
112 }
113
114 if (unwinder->LastErrorCode() == unwindstack::ERROR_INVALID_MAP) {
115 return false;
116 }
117
118 return true;
119 }
120
121 #else
122
123 #pragma clang diagnostic push
124 #pragma clang diagnostic warning "-W#warnings"
125 #warning "Backtrace collector is not implemented. GCStress cannot be used."
126 #pragma clang diagnostic pop
127
128 // We only have an implementation for linux. On other plaforms just return nothing. This is not
129 // really correct but we only use this for hashing and gcstress so it's not too big a deal.
130 void BacktraceCollector::Collect() {
131 UNUSED(skip_count_);
132 UNUSED(out_frames_);
133 UNUSED(max_depth_);
134 num_frames_ = 0;
135 }
136
137 #endif
138
139 } // namespace art
140