| 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/profiler/stack_sampler_impl.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "base/compiler_specific.h" |
| 10 | #include "base/logging.h" |
| 11 | #include "base/profiler/profile_builder.h" |
| 12 | #include "base/profiler/sample_metadata.h" |
| 13 | #include "base/profiler/stack_buffer.h" |
| 14 | #include "base/profiler/stack_copier.h" |
| 15 | #include "base/profiler/suspendable_thread_delegate.h" |
| 16 | #include "base/profiler/unwinder.h" |
| 17 | #include "build/build_config.h" |
| 18 | |
| 19 | // IMPORTANT NOTE: Some functions within this implementation are invoked while |
| 20 | // the target thread is suspended so it must not do any allocation from the |
| 21 | // heap, including indirectly via use of DCHECK/CHECK or other logging |
| 22 | // statements. Otherwise this code can deadlock on heap locks acquired by the |
| 23 | // target thread before it was suspended. These functions are commented with "NO |
| 24 | // HEAP ALLOCATIONS". |
| 25 | |
| 26 | namespace base { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | // Notifies the unwinders about the stack capture, and records metadata, while |
| 31 | // the thread is suspended. |
| 32 | class StackCopierDelegate : public StackCopier::Delegate { |
| 33 | public: |
| 34 | StackCopierDelegate(ModuleCache* module_cache, |
| 35 | Unwinder* native_unwinder, |
| 36 | Unwinder* aux_unwinder, |
| 37 | ProfileBuilder* profile_builder) |
| 38 | : module_cache_(module_cache), |
| 39 | native_unwinder_(native_unwinder), |
| 40 | aux_unwinder_(aux_unwinder), |
| 41 | profile_builder_(profile_builder), |
| 42 | metadata_provider_( |
| 43 | GetSampleMetadataRecorder()->CreateMetadataProvider()) {} |
| 44 | |
| 45 | StackCopierDelegate(const StackCopierDelegate&) = delete; |
| 46 | StackCopierDelegate& operator=(const StackCopierDelegate&) = delete; |
| 47 | |
| 48 | // StackCopier::Delegate: |
| 49 | // IMPORTANT NOTE: to avoid deadlock this function must not invoke any |
| 50 | // non-reentrant code that is also invoked by the target thread. In |
| 51 | // particular, it may not perform any heap allocation or deallocation, |
| 52 | // including indirectly via use of DCHECK/CHECK or other logging statements. |
| 53 | void OnStackCopy() override { |
| 54 | native_unwinder_->OnStackCapture(); |
| 55 | if (aux_unwinder_) |
| 56 | aux_unwinder_->OnStackCapture(); |
| 57 | |
| 58 | #if !defined(OS_POSIX) || defined(OS_MACOSX) |
| 59 | profile_builder_->RecordMetadata(metadata_provider_.get()); |
| 60 | #else |
| 61 | // TODO(https://crbug.com/1056283): Support metadata recording on POSIX |
| 62 | // platforms. |
| 63 | ALLOW_UNUSED_LOCAL(profile_builder_); |
| 64 | #endif |
| 65 | } |
| 66 | |
| 67 | void OnThreadResume() override { |
| 68 | // Reset this as soon as possible because it may hold a lock on the |
| 69 | // metadata. |
| 70 | metadata_provider_.reset(); |
| 71 | |
| 72 | native_unwinder_->UpdateModules(module_cache_); |
| 73 | if (aux_unwinder_) |
| 74 | aux_unwinder_->UpdateModules(module_cache_); |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | ModuleCache* const module_cache_; |
| 79 | Unwinder* const native_unwinder_; |
| 80 | Unwinder* const aux_unwinder_; |
| 81 | ProfileBuilder* const profile_builder_; |
| 82 | std::unique_ptr<ProfileBuilder::MetadataProvider> metadata_provider_; |
| 83 | }; |
| 84 | |
| 85 | } // namespace |
| 86 | |
| 87 | StackSamplerImpl::StackSamplerImpl(std::unique_ptr<StackCopier> stack_copier, |
| 88 | std::unique_ptr<Unwinder> native_unwinder, |
| 89 | ModuleCache* module_cache, |
| 90 | StackSamplerTestDelegate* test_delegate) |
| 91 | : stack_copier_(std::move(stack_copier)), |
| 92 | native_unwinder_(std::move(native_unwinder)), |
| 93 | module_cache_(module_cache), |
| 94 | test_delegate_(test_delegate) {} |
| 95 | |
| 96 | StackSamplerImpl::~StackSamplerImpl() = default; |
| 97 | |
| 98 | void StackSamplerImpl::AddAuxUnwinder(std::unique_ptr<Unwinder> unwinder) { |
| 99 | aux_unwinder_ = std::move(unwinder); |
| 100 | aux_unwinder_->AddInitialModules(module_cache_); |
| 101 | } |
| 102 | |
| 103 | void StackSamplerImpl::RecordStackFrames(StackBuffer* stack_buffer, |
| 104 | ProfileBuilder* profile_builder) { |
| 105 | DCHECK(stack_buffer); |
| 106 | |
| 107 | RegisterContext thread_context; |
| 108 | uintptr_t stack_top; |
| 109 | TimeTicks timestamp; |
| 110 | StackCopierDelegate delegate(module_cache_, native_unwinder_.get(), |
| 111 | aux_unwinder_.get(), profile_builder); |
| 112 | bool success = stack_copier_->CopyStack(stack_buffer, &stack_top, ×tamp, |
| 113 | &thread_context, &delegate); |
| 114 | if (!success) |
| 115 | return; |
| 116 | |
| 117 | if (test_delegate_) |
| 118 | test_delegate_->OnPreStackWalk(); |
| 119 | |
| 120 | profile_builder->OnSampleCompleted( |
| 121 | WalkStack(module_cache_, &thread_context, stack_top, |
| 122 | native_unwinder_.get(), aux_unwinder_.get()), |
| 123 | timestamp); |
| 124 | } |
| 125 | |
| 126 | // static |
| 127 | std::vector<Frame> StackSamplerImpl::WalkStackForTesting( |
| 128 | ModuleCache* module_cache, |
| 129 | RegisterContext* thread_context, |
| 130 | uintptr_t stack_top, |
| 131 | Unwinder* native_unwinder, |
| 132 | Unwinder* aux_unwinder) { |
| 133 | return WalkStack(module_cache, thread_context, stack_top, native_unwinder, |
| 134 | aux_unwinder); |
| 135 | } |
| 136 | |
| 137 | // static |
| 138 | std::vector<Frame> StackSamplerImpl::WalkStack(ModuleCache* module_cache, |
| 139 | RegisterContext* thread_context, |
| 140 | uintptr_t stack_top, |
| 141 | Unwinder* native_unwinder, |
| 142 | Unwinder* aux_unwinder) { |
| 143 | std::vector<Frame> stack; |
| 144 | // Reserve enough memory for most stacks, to avoid repeated |
| 145 | // allocations. Approximately 99.9% of recorded stacks are 128 frames or |
| 146 | // fewer. |
| 147 | stack.reserve(128); |
| 148 | |
| 149 | // Record the first frame from the context values. |
| 150 | stack.emplace_back(RegisterContextInstructionPointer(thread_context), |
| 151 | module_cache->GetModuleForAddress( |
| 152 | RegisterContextInstructionPointer(thread_context))); |
| 153 | |
| 154 | size_t prior_stack_size; |
| 155 | UnwindResult result; |
| 156 | do { |
| 157 | // Choose an authoritative unwinder for the current module. Use the aux |
| 158 | // unwinder if it thinks it can unwind from the current frame, otherwise use |
| 159 | // the native unwinder. |
| 160 | Unwinder* unwinder = |
| 161 | aux_unwinder && aux_unwinder->CanUnwindFrom(stack.back()) |
| 162 | ? aux_unwinder |
| 163 | : native_unwinder; |
| 164 | |
| 165 | prior_stack_size = stack.size(); |
| 166 | result = |
| 167 | unwinder->TryUnwind(thread_context, stack_top, module_cache, &stack); |
| 168 | |
| 169 | // The native unwinder should be the only one that returns COMPLETED |
| 170 | // since the stack starts in native code. |
| 171 | DCHECK(result != UnwindResult::COMPLETED || unwinder == native_unwinder); |
| 172 | } while (result != UnwindResult::ABORTED && |
| 173 | result != UnwindResult::COMPLETED && |
| 174 | // Give up if the authoritative unwinder for the module was unable to |
| 175 | // unwind. |
| 176 | stack.size() > prior_stack_size); |
| 177 | |
| 178 | return stack; |
| 179 | } |
| 180 | |
| 181 | } // namespace base |
| 182 | |