1 // Copyright 2017 The Chromium Authors
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.h"
6
7 #include <memory>
8
9 #include "base/check.h"
10 #include "base/functional/bind.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/profiler/frame_pointer_unwinder.h"
13 #include "base/profiler/stack_copier_suspend.h"
14 #include "base/profiler/suspendable_thread_delegate_mac.h"
15 #include "base/profiler/unwinder.h"
16
17 namespace base {
18
19 namespace {
20
CreateUnwinders()21 std::vector<std::unique_ptr<Unwinder>> CreateUnwinders() {
22 std::vector<std::unique_ptr<Unwinder>> unwinders;
23 unwinders.push_back(std::make_unique<FramePointerUnwinder>());
24 return unwinders;
25 }
26
27 } // namespace
28
29 // static
Create(SamplingProfilerThreadToken thread_token,ModuleCache * module_cache,UnwindersFactory core_unwinders_factory,RepeatingClosure record_sample_callback,StackSamplerTestDelegate * test_delegate)30 std::unique_ptr<StackSampler> StackSampler::Create(
31 SamplingProfilerThreadToken thread_token,
32 ModuleCache* module_cache,
33 UnwindersFactory core_unwinders_factory,
34 RepeatingClosure record_sample_callback,
35 StackSamplerTestDelegate* test_delegate) {
36 DCHECK(!core_unwinders_factory);
37 return base::WrapUnique(new StackSampler(
38 std::make_unique<StackCopierSuspend>(
39 std::make_unique<SuspendableThreadDelegateMac>(thread_token)),
40 BindOnce(&CreateUnwinders), module_cache,
41 std::move(record_sample_callback), test_delegate));
42 }
43
44 // static
GetStackBufferSize()45 size_t StackSampler::GetStackBufferSize() {
46 size_t stack_size = PlatformThread::GetDefaultThreadStackSize();
47
48 // If getrlimit somehow fails, return the default macOS main thread stack size
49 // of 8 MB (DFLSSIZ in <i386/vmparam.h>) with extra wiggle room.
50 return stack_size > 0 ? stack_size : 12 * 1024 * 1024;
51 }
52
53 } // namespace base
54