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