1 // Copyright 2015 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 "base/check.h"
8 #include "base/functional/bind.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/profiler/native_unwinder_win.h"
11 #include "base/profiler/stack_copier_suspend.h"
12 #include "base/profiler/suspendable_thread_delegate_win.h"
13 #include "build/build_config.h"
14
15 namespace base {
16
17 // static
Create(SamplingProfilerThreadToken thread_token,ModuleCache * module_cache,UnwindersFactory core_unwinders_factory,RepeatingClosure record_sample_callback,StackSamplerTestDelegate * test_delegate)18 std::unique_ptr<StackSampler> StackSampler::Create(
19 SamplingProfilerThreadToken thread_token,
20 ModuleCache* module_cache,
21 UnwindersFactory core_unwinders_factory,
22 RepeatingClosure record_sample_callback,
23 StackSamplerTestDelegate* test_delegate) {
24 DCHECK(!core_unwinders_factory);
25 #if defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM64)
26 const auto create_unwinders = []() {
27 std::vector<std::unique_ptr<Unwinder>> unwinders;
28 unwinders.push_back(std::make_unique<NativeUnwinderWin>());
29 return unwinders;
30 };
31 return base::WrapUnique(new StackSampler(
32 std::make_unique<StackCopierSuspend>(
33 std::make_unique<SuspendableThreadDelegateWin>(thread_token)),
34 BindOnce(create_unwinders), module_cache,
35 std::move(record_sample_callback), test_delegate));
36 #else
37 return nullptr;
38 #endif
39 }
40
41 // static
GetStackBufferSize()42 size_t StackSampler::GetStackBufferSize() {
43 // The default Win32 reserved stack size is 1 MB and Chrome Windows threads
44 // currently always use the default, but this allows for expansion if it
45 // occurs. The size beyond the actual stack size consists of unallocated
46 // virtual memory pages so carries little cost (just a bit of wasted address
47 // space).
48 return 2 << 20; // 2 MiB
49 }
50
51 } // namespace base
52