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.
5 #include "base/profiler/stack_sampler.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/threading/platform_thread.h"
13 #include "build/build_config.h"
15 #if BUILDFLAG(IS_CHROMEOS) && defined(ARCH_CPU_X86_64)
16 #include "base/check.h"
17 #include "base/functional/bind.h"
18 #include "base/profiler/frame_pointer_unwinder.h"
19 #include "base/profiler/stack_copier_signal.h"
20 #include "base/profiler/thread_delegate_posix.h"
21 #include "base/profiler/unwinder.h"
28 #if BUILDFLAG(IS_CHROMEOS) && defined(ARCH_CPU_X86_64)
29 std::vector<std::unique_ptr<Unwinder>> CreateUnwinders() {
30 std::vector<std::unique_ptr<Unwinder>> unwinders;
31 unwinders.push_back(std::make_unique<FramePointerUnwinder>());
38 std::unique_ptr<StackSampler> StackSampler::Create(
39 SamplingProfilerThreadToken thread_token,
40 ModuleCache* module_cache,
41 UnwindersFactory core_unwinders_factory,
42 RepeatingClosure record_sample_callback,
43 StackSamplerTestDelegate* test_delegate) {
44 #if BUILDFLAG(IS_CHROMEOS) && defined(ARCH_CPU_X86_64)
45 DCHECK(!core_unwinders_factory);
46 return base::WrapUnique(
47 new StackSampler(std::make_unique<StackCopierSignal>(
48 ThreadDelegatePosix::Create(thread_token)),
49 BindOnce(&CreateUnwinders), module_cache,
50 std::move(record_sample_callback), test_delegate));
56 size_t StackSampler::GetStackBufferSize() {
57 size_t stack_size = PlatformThread::GetDefaultThreadStackSize();
60 if (stack_size == 0 && pthread_attr_init(&attr) == 0) {
61 if (pthread_attr_getstacksize(&attr, &stack_size) != 0)
63 pthread_attr_destroy(&attr);
66 // Maximum limits under NPTL implementation.
67 constexpr size_t kDefaultStackLimit = 4 * (1 << 20);
68 return stack_size > 0 ? stack_size : kDefaultStackLimit;