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 <pthread.h>
8
9 #include <memory>
10
11 #include "base/memory/ptr_util.h"
12 #include "base/threading/platform_thread.h"
13 #include "build/build_config.h"
14
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"
22 #endif
23
24 namespace base {
25
26 namespace {
27
28 #if BUILDFLAG(IS_CHROMEOS) && defined(ARCH_CPU_X86_64)
CreateUnwinders()29 std::vector<std::unique_ptr<Unwinder>> CreateUnwinders() {
30 std::vector<std::unique_ptr<Unwinder>> unwinders;
31 unwinders.push_back(std::make_unique<FramePointerUnwinder>());
32 return unwinders;
33 }
34 #endif
35
36 } // namespace
37
Create(SamplingProfilerThreadToken thread_token,ModuleCache * module_cache,UnwindersFactory core_unwinders_factory,RepeatingClosure record_sample_callback,StackSamplerTestDelegate * test_delegate)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));
51 #else
52 return nullptr;
53 #endif
54 }
55
GetStackBufferSize()56 size_t StackSampler::GetStackBufferSize() {
57 size_t stack_size = PlatformThread::GetDefaultThreadStackSize();
58
59 pthread_attr_t attr;
60 if (stack_size == 0 && pthread_attr_init(&attr) == 0) {
61 if (pthread_attr_getstacksize(&attr, &stack_size) != 0)
62 stack_size = 0;
63 pthread_attr_destroy(&attr);
64 }
65
66 // Maximum limits under NPTL implementation.
67 constexpr size_t kDefaultStackLimit = 4 * (1 << 20);
68 return stack_size > 0 ? stack_size : kDefaultStackLimit;
69 }
70
71 } // namespace base
72