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) && \
16 (defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM64))
17 #include "base/check.h"
18 #include "base/functional/bind.h"
19 #include "base/profiler/frame_pointer_unwinder.h"
20 #include "base/profiler/stack_copier_signal.h"
21 #include "base/profiler/thread_delegate_posix.h"
22 #include "base/profiler/unwinder.h"
23 #endif
24
25 namespace base {
26
27 namespace {
28
29 #if BUILDFLAG(IS_CHROMEOS) && \
30 (defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM64))
CreateUnwinders()31 std::vector<std::unique_ptr<Unwinder>> CreateUnwinders() {
32 std::vector<std::unique_ptr<Unwinder>> unwinders;
33 unwinders.push_back(std::make_unique<FramePointerUnwinder>());
34 return unwinders;
35 }
36 #endif
37
38 } // namespace
39
Create(SamplingProfilerThreadToken thread_token,ModuleCache * module_cache,UnwindersFactory core_unwinders_factory,RepeatingClosure record_sample_callback,StackSamplerTestDelegate * test_delegate)40 std::unique_ptr<StackSampler> StackSampler::Create(
41 SamplingProfilerThreadToken thread_token,
42 ModuleCache* module_cache,
43 UnwindersFactory core_unwinders_factory,
44 RepeatingClosure record_sample_callback,
45 StackSamplerTestDelegate* test_delegate) {
46 #if BUILDFLAG(IS_CHROMEOS) && \
47 (defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_ARM64))
48 DCHECK(!core_unwinders_factory);
49 return base::WrapUnique(
50 new StackSampler(std::make_unique<StackCopierSignal>(
51 ThreadDelegatePosix::Create(thread_token)),
52 BindOnce(&CreateUnwinders), module_cache,
53 std::move(record_sample_callback), test_delegate));
54 #else
55 return nullptr;
56 #endif
57 }
58
GetStackBufferSize()59 size_t StackSampler::GetStackBufferSize() {
60 size_t stack_size = PlatformThread::GetDefaultThreadStackSize();
61
62 pthread_attr_t attr;
63 if (stack_size == 0 && pthread_attr_init(&attr) == 0) {
64 if (pthread_attr_getstacksize(&attr, &stack_size) != 0)
65 stack_size = 0;
66 pthread_attr_destroy(&attr);
67 }
68
69 // Maximum limits under NPTL implementation.
70 constexpr size_t kDefaultStackLimit = 4 * (1 << 20);
71 return stack_size > 0 ? stack_size : kDefaultStackLimit;
72 }
73
74 } // namespace base
75