Commit afb3cbf6 authored by Xi Cheng's avatar Xi Cheng Committed by Commit Bot

Simplify namespace code in NativeStackSampler

Bug: 851163
Change-Id: I651da22403ba5597667549970b29d7d47a62f73b
Reviewed-on: https://chromium-review.googlesource.com/1105491Reviewed-by: default avatarMike Wittman <wittman@chromium.org>
Commit-Queue: Xi Cheng <chengx@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568484}
parent d929779d
...@@ -34,6 +34,10 @@ void _sigtramp(int, int, struct sigset*); ...@@ -34,6 +34,10 @@ void _sigtramp(int, int, struct sigset*);
namespace base { namespace base {
using Frame = StackSamplingProfiler::Frame;
using Module = StackSamplingProfiler::Module;
using SamplingProfileBuilder = StackSamplingProfiler::SamplingProfileBuilder;
namespace { namespace {
// Maps a module's address range (half-open) in memory to an index in a separate // Maps a module's address range (half-open) in memory to an index in a separate
...@@ -115,7 +119,7 @@ size_t GetModuleTextSize(const void* module_addr) { ...@@ -115,7 +119,7 @@ size_t GetModuleTextSize(const void* module_addr) {
// StackSamplingProfiler::Frame::kUnknownModuleIndex if no Module can be // StackSamplingProfiler::Frame::kUnknownModuleIndex if no Module can be
// determined for |module|. // determined for |module|.
size_t GetModuleIndex(const uintptr_t instruction_pointer, size_t GetModuleIndex(const uintptr_t instruction_pointer,
std::vector<StackSamplingProfiler::Module>* modules, std::vector<Module>* modules,
std::vector<ModuleIndex>* profile_module_index) { std::vector<ModuleIndex>* profile_module_index) {
// Check if |instruction_pointer| is in the address range of a module we've // Check if |instruction_pointer| is in the address range of a module we've
// already seen. // already seen.
...@@ -130,11 +134,10 @@ size_t GetModuleIndex(const uintptr_t instruction_pointer, ...@@ -130,11 +134,10 @@ size_t GetModuleIndex(const uintptr_t instruction_pointer,
Dl_info inf; Dl_info inf;
if (!dladdr(reinterpret_cast<const void*>(instruction_pointer), &inf)) if (!dladdr(reinterpret_cast<const void*>(instruction_pointer), &inf))
return StackSamplingProfiler::Frame::kUnknownModuleIndex; return Frame::kUnknownModuleIndex;
StackSamplingProfiler::Module module( Module module(reinterpret_cast<uintptr_t>(inf.dli_fbase),
reinterpret_cast<uintptr_t>(inf.dli_fbase), GetUniqueId(inf.dli_fbase), GetUniqueId(inf.dli_fbase), FilePath(inf.dli_fname));
FilePath(inf.dli_fname));
modules->push_back(module); modules->push_back(module);
auto base_module_address = reinterpret_cast<uintptr_t>(inf.dli_fbase); auto base_module_address = reinterpret_cast<uintptr_t>(inf.dli_fbase);
...@@ -298,13 +301,12 @@ bool HasValidRbp(unw_cursor_t* unwind_cursor, uintptr_t stack_top) { ...@@ -298,13 +301,12 @@ bool HasValidRbp(unw_cursor_t* unwind_cursor, uintptr_t stack_top) {
// lambda for each frame. Returns false if an error occurred, otherwise returns // lambda for each frame. Returns false if an error occurred, otherwise returns
// true. // true.
template <typename StackFrameCallback, typename ContinueUnwindPredicate> template <typename StackFrameCallback, typename ContinueUnwindPredicate>
bool WalkStackFromContext( bool WalkStackFromContext(unw_context_t* unwind_context,
unw_context_t* unwind_context, size_t* frame_count,
size_t* frame_count, std::vector<Module>* current_modules,
std::vector<StackSamplingProfiler::Module>* current_modules, std::vector<ModuleIndex>* profile_module_index,
std::vector<ModuleIndex>* profile_module_index, const StackFrameCallback& callback,
const StackFrameCallback& callback, const ContinueUnwindPredicate& continue_unwind) {
const ContinueUnwindPredicate& continue_unwind) {
unw_cursor_t unwind_cursor; unw_cursor_t unwind_cursor;
unw_init_local(&unwind_cursor, unwind_context); unw_init_local(&unwind_cursor, unwind_context);
...@@ -328,7 +330,7 @@ bool WalkStackFromContext( ...@@ -328,7 +330,7 @@ bool WalkStackFromContext(
// frame. // frame.
size_t module_index = size_t module_index =
GetModuleIndex(rip, current_modules, profile_module_index); GetModuleIndex(rip, current_modules, profile_module_index);
if (module_index == StackSamplingProfiler::Frame::kUnknownModuleIndex) if (module_index == Frame::kUnknownModuleIndex)
return false; return false;
callback(static_cast<uintptr_t>(rip), module_index); callback(static_cast<uintptr_t>(rip), module_index);
...@@ -388,7 +390,7 @@ void GetSigtrampRange(uintptr_t* start, uintptr_t* end) { ...@@ -388,7 +390,7 @@ void GetSigtrampRange(uintptr_t* start, uintptr_t* end) {
// lambda for each frame. // lambda for each frame.
template <typename StackFrameCallback, typename ContinueUnwindPredicate> template <typename StackFrameCallback, typename ContinueUnwindPredicate>
void WalkStack(const x86_thread_state64_t& thread_state, void WalkStack(const x86_thread_state64_t& thread_state,
std::vector<StackSamplingProfiler::Module>* current_modules, std::vector<Module>* current_modules,
std::vector<ModuleIndex>* profile_module_index, std::vector<ModuleIndex>* profile_module_index,
const StackFrameCallback& callback, const StackFrameCallback& callback,
const ContinueUnwindPredicate& continue_unwind) { const ContinueUnwindPredicate& continue_unwind) {
...@@ -470,11 +472,10 @@ class NativeStackSamplerMac : public NativeStackSampler { ...@@ -470,11 +472,10 @@ class NativeStackSamplerMac : public NativeStackSampler {
~NativeStackSamplerMac() override; ~NativeStackSamplerMac() override;
// StackSamplingProfiler::NativeStackSampler: // StackSamplingProfiler::NativeStackSampler:
void ProfileRecordingStarting( void ProfileRecordingStarting(std::vector<Module>* modules) override;
std::vector<StackSamplingProfiler::Module>* modules) override; std::vector<Frame> RecordStackFrames(
std::vector<StackSamplingProfiler::Frame> RecordStackFrames(
StackBuffer* stack_buffer, StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder) override; SamplingProfileBuilder* profile_builder) override;
void ProfileRecordingStopped() override; void ProfileRecordingStopped() override;
private: private:
...@@ -482,9 +483,9 @@ class NativeStackSamplerMac : public NativeStackSampler { ...@@ -482,9 +483,9 @@ class NativeStackSamplerMac : public NativeStackSampler {
// thread. // thread.
// Returns a vector of frames which record the information of the stack frames // Returns a vector of frames which record the information of the stack frames
// and associated modules. // and associated modules.
std::vector<StackSamplingProfiler::Frame> SuspendThreadAndRecordStack( std::vector<Frame> SuspendThreadAndRecordStack(
StackBuffer* stack_buffer, StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder); SamplingProfileBuilder* profile_builder);
// Weak reference: Mach port for thread being profiled. // Weak reference: Mach port for thread being profiled.
mach_port_t thread_port_; mach_port_t thread_port_;
...@@ -496,7 +497,7 @@ class NativeStackSamplerMac : public NativeStackSampler { ...@@ -496,7 +497,7 @@ class NativeStackSamplerMac : public NativeStackSampler {
// Weak. Points to the modules associated with the profile being recorded // Weak. Points to the modules associated with the profile being recorded
// between ProfileRecordingStarting() and ProfileRecordingStopped(). // between ProfileRecordingStarting() and ProfileRecordingStopped().
std::vector<StackSamplingProfiler::Module>* current_modules_ = nullptr; std::vector<Module>* current_modules_ = nullptr;
// Maps a module's address range to the corresponding Module's index within // Maps a module's address range to the corresponding Module's index within
// current_modules_. // current_modules_.
...@@ -528,15 +529,14 @@ NativeStackSamplerMac::NativeStackSamplerMac( ...@@ -528,15 +529,14 @@ NativeStackSamplerMac::NativeStackSamplerMac(
NativeStackSamplerMac::~NativeStackSamplerMac() {} NativeStackSamplerMac::~NativeStackSamplerMac() {}
void NativeStackSamplerMac::ProfileRecordingStarting( void NativeStackSamplerMac::ProfileRecordingStarting(
std::vector<StackSamplingProfiler::Module>* modules) { std::vector<Module>* modules) {
current_modules_ = modules; current_modules_ = modules;
profile_module_index_.clear(); profile_module_index_.clear();
} }
std::vector<StackSamplingProfiler::Frame> std::vector<Frame> NativeStackSamplerMac::RecordStackFrames(
NativeStackSamplerMac::RecordStackFrames(
StackBuffer* stack_buffer, StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder) { SamplingProfileBuilder* profile_builder) {
DCHECK(current_modules_); DCHECK(current_modules_);
return SuspendThreadAndRecordStack(stack_buffer, profile_builder); return SuspendThreadAndRecordStack(stack_buffer, profile_builder);
...@@ -546,13 +546,12 @@ void NativeStackSamplerMac::ProfileRecordingStopped() { ...@@ -546,13 +546,12 @@ void NativeStackSamplerMac::ProfileRecordingStopped() {
current_modules_ = nullptr; current_modules_ = nullptr;
} }
std::vector<StackSamplingProfiler::Frame> std::vector<Frame> NativeStackSamplerMac::SuspendThreadAndRecordStack(
NativeStackSamplerMac::SuspendThreadAndRecordStack(
StackBuffer* stack_buffer, StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder) { SamplingProfileBuilder* profile_builder) {
x86_thread_state64_t thread_state; x86_thread_state64_t thread_state;
std::vector<StackSamplingProfiler::Frame> frames; std::vector<Frame> frames;
// Copy the stack. // Copy the stack.
......
...@@ -31,6 +31,10 @@ ...@@ -31,6 +31,10 @@
namespace base { namespace base {
using Frame = StackSamplingProfiler::Frame;
using Module = StackSamplingProfiler::Module;
using SamplingProfileBuilder = StackSamplingProfiler::SamplingProfileBuilder;
// Stack recording functions -------------------------------------------------- // Stack recording functions --------------------------------------------------
namespace { namespace {
...@@ -334,7 +338,7 @@ void SuspendThreadAndRecordStack( ...@@ -334,7 +338,7 @@ void SuspendThreadAndRecordStack(
void* stack_copy_buffer, void* stack_copy_buffer,
size_t stack_copy_buffer_size, size_t stack_copy_buffer_size,
std::vector<RecordedFrame>* stack, std::vector<RecordedFrame>* stack,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder, SamplingProfileBuilder* profile_builder,
NativeStackSamplerTestDelegate* test_delegate) { NativeStackSamplerTestDelegate* test_delegate) {
DCHECK(stack->empty()); DCHECK(stack->empty());
...@@ -392,31 +396,27 @@ class NativeStackSamplerWin : public NativeStackSampler { ...@@ -392,31 +396,27 @@ class NativeStackSamplerWin : public NativeStackSampler {
~NativeStackSamplerWin() override; ~NativeStackSamplerWin() override;
// StackSamplingProfiler::NativeStackSampler: // StackSamplingProfiler::NativeStackSampler:
void ProfileRecordingStarting( void ProfileRecordingStarting(std::vector<Module>* modules) override;
std::vector<StackSamplingProfiler::Module>* modules) override; std::vector<Frame> RecordStackFrames(
std::vector<StackSamplingProfiler::Frame> RecordStackFrames(
StackBuffer* stack_buffer, StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder) override; SamplingProfileBuilder* profile_builder) override;
void ProfileRecordingStopped() override; void ProfileRecordingStopped() override;
private: private:
// Attempts to query the module filename, base address, and id for // Attempts to query the module filename, base address, and id for
// |module_handle|, and store them in |module|. Returns true if it succeeded. // |module_handle|, and store them in |module|. Returns true if it succeeded.
static bool GetModuleForHandle(HMODULE module_handle, static bool GetModuleForHandle(HMODULE module_handle, Module* module);
StackSamplingProfiler::Module* module);
// Gets the index for the Module corresponding to |module_handle| in // Gets the index for the Module corresponding to |module_handle| in
// |modules|, adding it if it's not already present. Returns // |modules|, adding it if it's not already present. Returns
// StackSamplingProfiler::Frame::kUnknownModuleIndex if no Module can be // StackSamplingProfiler::Frame::kUnknownModuleIndex if no Module can be
// determined for |module|. // determined for |module|.
size_t GetModuleIndex(HMODULE module_handle, size_t GetModuleIndex(HMODULE module_handle, std::vector<Module>* modules);
std::vector<StackSamplingProfiler::Module>* modules);
// Creates a set of frames with the information represented by |stack| and // Creates a set of frames with the information represented by |stack| and
// |modules|. // |modules|.
std::vector<StackSamplingProfiler::Frame> CreateFrames( std::vector<Frame> CreateFrames(const std::vector<RecordedFrame>& stack,
const std::vector<RecordedFrame>& stack, std::vector<Module>* modules);
std::vector<StackSamplingProfiler::Module>* modules);
win::ScopedHandle thread_handle_; win::ScopedHandle thread_handle_;
...@@ -427,7 +427,7 @@ class NativeStackSamplerWin : public NativeStackSampler { ...@@ -427,7 +427,7 @@ class NativeStackSamplerWin : public NativeStackSampler {
// Weak. Points to the modules associated with the profile being recorded // Weak. Points to the modules associated with the profile being recorded
// between ProfileRecordingStarting() and ProfileRecordingStopped(). // between ProfileRecordingStarting() and ProfileRecordingStopped().
std::vector<StackSamplingProfiler::Module>* current_modules_; std::vector<Module>* current_modules_;
// Maps a module handle to the corresponding Module's index within // Maps a module handle to the corresponding Module's index within
// current_modules_. // current_modules_.
...@@ -448,15 +448,14 @@ NativeStackSamplerWin::~NativeStackSamplerWin() { ...@@ -448,15 +448,14 @@ NativeStackSamplerWin::~NativeStackSamplerWin() {
} }
void NativeStackSamplerWin::ProfileRecordingStarting( void NativeStackSamplerWin::ProfileRecordingStarting(
std::vector<StackSamplingProfiler::Module>* modules) { std::vector<Module>* modules) {
current_modules_ = modules; current_modules_ = modules;
profile_module_index_.clear(); profile_module_index_.clear();
} }
std::vector<StackSamplingProfiler::Frame> std::vector<Frame> NativeStackSamplerWin::RecordStackFrames(
NativeStackSamplerWin::RecordStackFrames(
StackBuffer* stack_buffer, StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder) { SamplingProfileBuilder* profile_builder) {
DCHECK(stack_buffer); DCHECK(stack_buffer);
DCHECK(current_modules_); DCHECK(current_modules_);
...@@ -473,9 +472,8 @@ void NativeStackSamplerWin::ProfileRecordingStopped() { ...@@ -473,9 +472,8 @@ void NativeStackSamplerWin::ProfileRecordingStopped() {
} }
// static // static
bool NativeStackSamplerWin::GetModuleForHandle( bool NativeStackSamplerWin::GetModuleForHandle(HMODULE module_handle,
HMODULE module_handle, Module* module) {
StackSamplingProfiler::Module* module) {
wchar_t module_name[MAX_PATH]; wchar_t module_name[MAX_PATH];
DWORD result_length = DWORD result_length =
::GetModuleFileName(module_handle, module_name, size(module_name)); ::GetModuleFileName(module_handle, module_name, size(module_name));
...@@ -488,17 +486,16 @@ bool NativeStackSamplerWin::GetModuleForHandle( ...@@ -488,17 +486,16 @@ bool NativeStackSamplerWin::GetModuleForHandle(
return !module->id.empty(); return !module->id.empty();
} }
size_t NativeStackSamplerWin::GetModuleIndex( size_t NativeStackSamplerWin::GetModuleIndex(HMODULE module_handle,
HMODULE module_handle, std::vector<Module>* modules) {
std::vector<StackSamplingProfiler::Module>* modules) {
if (!module_handle) if (!module_handle)
return StackSamplingProfiler::Frame::kUnknownModuleIndex; return Frame::kUnknownModuleIndex;
auto loc = profile_module_index_.find(module_handle); auto loc = profile_module_index_.find(module_handle);
if (loc == profile_module_index_.end()) { if (loc == profile_module_index_.end()) {
StackSamplingProfiler::Module module; Module module;
if (!GetModuleForHandle(module_handle, &module)) if (!GetModuleForHandle(module_handle, &module))
return StackSamplingProfiler::Frame::kUnknownModuleIndex; return Frame::kUnknownModuleIndex;
modules->push_back(module); modules->push_back(module);
loc = profile_module_index_.insert(std::make_pair( loc = profile_module_index_.insert(std::make_pair(
module_handle, modules->size() - 1)).first; module_handle, modules->size() - 1)).first;
...@@ -507,10 +504,10 @@ size_t NativeStackSamplerWin::GetModuleIndex( ...@@ -507,10 +504,10 @@ size_t NativeStackSamplerWin::GetModuleIndex(
return loc->second; return loc->second;
} }
std::vector<StackSamplingProfiler::Frame> NativeStackSamplerWin::CreateFrames( std::vector<Frame> NativeStackSamplerWin::CreateFrames(
const std::vector<RecordedFrame>& stack, const std::vector<RecordedFrame>& stack,
std::vector<StackSamplingProfiler::Module>* modules) { std::vector<Module>* modules) {
std::vector<StackSamplingProfiler::Frame> frames; std::vector<Frame> frames;
frames.reserve(stack.size()); frames.reserve(stack.size());
for (const auto& frame : stack) { for (const auto& frame : stack) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment