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*);
namespace base {
using Frame = StackSamplingProfiler::Frame;
using Module = StackSamplingProfiler::Module;
using SamplingProfileBuilder = StackSamplingProfiler::SamplingProfileBuilder;
namespace {
// 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) {
// StackSamplingProfiler::Frame::kUnknownModuleIndex if no Module can be
// determined for |module|.
size_t GetModuleIndex(const uintptr_t instruction_pointer,
std::vector<StackSamplingProfiler::Module>* modules,
std::vector<Module>* modules,
std::vector<ModuleIndex>* profile_module_index) {
// Check if |instruction_pointer| is in the address range of a module we've
// already seen.
......@@ -130,11 +134,10 @@ size_t GetModuleIndex(const uintptr_t instruction_pointer,
Dl_info inf;
if (!dladdr(reinterpret_cast<const void*>(instruction_pointer), &inf))
return StackSamplingProfiler::Frame::kUnknownModuleIndex;
return Frame::kUnknownModuleIndex;
StackSamplingProfiler::Module module(
reinterpret_cast<uintptr_t>(inf.dli_fbase), GetUniqueId(inf.dli_fbase),
FilePath(inf.dli_fname));
Module module(reinterpret_cast<uintptr_t>(inf.dli_fbase),
GetUniqueId(inf.dli_fbase), FilePath(inf.dli_fname));
modules->push_back(module);
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) {
// lambda for each frame. Returns false if an error occurred, otherwise returns
// true.
template <typename StackFrameCallback, typename ContinueUnwindPredicate>
bool WalkStackFromContext(
unw_context_t* unwind_context,
size_t* frame_count,
std::vector<StackSamplingProfiler::Module>* current_modules,
std::vector<ModuleIndex>* profile_module_index,
const StackFrameCallback& callback,
const ContinueUnwindPredicate& continue_unwind) {
bool WalkStackFromContext(unw_context_t* unwind_context,
size_t* frame_count,
std::vector<Module>* current_modules,
std::vector<ModuleIndex>* profile_module_index,
const StackFrameCallback& callback,
const ContinueUnwindPredicate& continue_unwind) {
unw_cursor_t unwind_cursor;
unw_init_local(&unwind_cursor, unwind_context);
......@@ -328,7 +330,7 @@ bool WalkStackFromContext(
// frame.
size_t module_index =
GetModuleIndex(rip, current_modules, profile_module_index);
if (module_index == StackSamplingProfiler::Frame::kUnknownModuleIndex)
if (module_index == Frame::kUnknownModuleIndex)
return false;
callback(static_cast<uintptr_t>(rip), module_index);
......@@ -388,7 +390,7 @@ void GetSigtrampRange(uintptr_t* start, uintptr_t* end) {
// lambda for each frame.
template <typename StackFrameCallback, typename ContinueUnwindPredicate>
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,
const StackFrameCallback& callback,
const ContinueUnwindPredicate& continue_unwind) {
......@@ -470,11 +472,10 @@ class NativeStackSamplerMac : public NativeStackSampler {
~NativeStackSamplerMac() override;
// StackSamplingProfiler::NativeStackSampler:
void ProfileRecordingStarting(
std::vector<StackSamplingProfiler::Module>* modules) override;
std::vector<StackSamplingProfiler::Frame> RecordStackFrames(
void ProfileRecordingStarting(std::vector<Module>* modules) override;
std::vector<Frame> RecordStackFrames(
StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder) override;
SamplingProfileBuilder* profile_builder) override;
void ProfileRecordingStopped() override;
private:
......@@ -482,9 +483,9 @@ class NativeStackSamplerMac : public NativeStackSampler {
// thread.
// Returns a vector of frames which record the information of the stack frames
// and associated modules.
std::vector<StackSamplingProfiler::Frame> SuspendThreadAndRecordStack(
std::vector<Frame> SuspendThreadAndRecordStack(
StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder);
SamplingProfileBuilder* profile_builder);
// Weak reference: Mach port for thread being profiled.
mach_port_t thread_port_;
......@@ -496,7 +497,7 @@ class NativeStackSamplerMac : public NativeStackSampler {
// Weak. Points to the modules associated with the profile being recorded
// 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
// current_modules_.
......@@ -528,15 +529,14 @@ NativeStackSamplerMac::NativeStackSamplerMac(
NativeStackSamplerMac::~NativeStackSamplerMac() {}
void NativeStackSamplerMac::ProfileRecordingStarting(
std::vector<StackSamplingProfiler::Module>* modules) {
std::vector<Module>* modules) {
current_modules_ = modules;
profile_module_index_.clear();
}
std::vector<StackSamplingProfiler::Frame>
NativeStackSamplerMac::RecordStackFrames(
std::vector<Frame> NativeStackSamplerMac::RecordStackFrames(
StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder) {
SamplingProfileBuilder* profile_builder) {
DCHECK(current_modules_);
return SuspendThreadAndRecordStack(stack_buffer, profile_builder);
......@@ -546,13 +546,12 @@ void NativeStackSamplerMac::ProfileRecordingStopped() {
current_modules_ = nullptr;
}
std::vector<StackSamplingProfiler::Frame>
NativeStackSamplerMac::SuspendThreadAndRecordStack(
std::vector<Frame> NativeStackSamplerMac::SuspendThreadAndRecordStack(
StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder) {
SamplingProfileBuilder* profile_builder) {
x86_thread_state64_t thread_state;
std::vector<StackSamplingProfiler::Frame> frames;
std::vector<Frame> frames;
// Copy the stack.
......
......@@ -31,6 +31,10 @@
namespace base {
using Frame = StackSamplingProfiler::Frame;
using Module = StackSamplingProfiler::Module;
using SamplingProfileBuilder = StackSamplingProfiler::SamplingProfileBuilder;
// Stack recording functions --------------------------------------------------
namespace {
......@@ -334,7 +338,7 @@ void SuspendThreadAndRecordStack(
void* stack_copy_buffer,
size_t stack_copy_buffer_size,
std::vector<RecordedFrame>* stack,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder,
SamplingProfileBuilder* profile_builder,
NativeStackSamplerTestDelegate* test_delegate) {
DCHECK(stack->empty());
......@@ -392,31 +396,27 @@ class NativeStackSamplerWin : public NativeStackSampler {
~NativeStackSamplerWin() override;
// StackSamplingProfiler::NativeStackSampler:
void ProfileRecordingStarting(
std::vector<StackSamplingProfiler::Module>* modules) override;
std::vector<StackSamplingProfiler::Frame> RecordStackFrames(
void ProfileRecordingStarting(std::vector<Module>* modules) override;
std::vector<Frame> RecordStackFrames(
StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder) override;
SamplingProfileBuilder* profile_builder) override;
void ProfileRecordingStopped() override;
private:
// Attempts to query the module filename, base address, and id for
// |module_handle|, and store them in |module|. Returns true if it succeeded.
static bool GetModuleForHandle(HMODULE module_handle,
StackSamplingProfiler::Module* module);
static bool GetModuleForHandle(HMODULE module_handle, Module* module);
// Gets the index for the Module corresponding to |module_handle| in
// |modules|, adding it if it's not already present. Returns
// StackSamplingProfiler::Frame::kUnknownModuleIndex if no Module can be
// determined for |module|.
size_t GetModuleIndex(HMODULE module_handle,
std::vector<StackSamplingProfiler::Module>* modules);
size_t GetModuleIndex(HMODULE module_handle, std::vector<Module>* modules);
// Creates a set of frames with the information represented by |stack| and
// |modules|.
std::vector<StackSamplingProfiler::Frame> CreateFrames(
const std::vector<RecordedFrame>& stack,
std::vector<StackSamplingProfiler::Module>* modules);
std::vector<Frame> CreateFrames(const std::vector<RecordedFrame>& stack,
std::vector<Module>* modules);
win::ScopedHandle thread_handle_;
......@@ -427,7 +427,7 @@ class NativeStackSamplerWin : public NativeStackSampler {
// Weak. Points to the modules associated with the profile being recorded
// 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
// current_modules_.
......@@ -448,15 +448,14 @@ NativeStackSamplerWin::~NativeStackSamplerWin() {
}
void NativeStackSamplerWin::ProfileRecordingStarting(
std::vector<StackSamplingProfiler::Module>* modules) {
std::vector<Module>* modules) {
current_modules_ = modules;
profile_module_index_.clear();
}
std::vector<StackSamplingProfiler::Frame>
NativeStackSamplerWin::RecordStackFrames(
std::vector<Frame> NativeStackSamplerWin::RecordStackFrames(
StackBuffer* stack_buffer,
StackSamplingProfiler::SamplingProfileBuilder* profile_builder) {
SamplingProfileBuilder* profile_builder) {
DCHECK(stack_buffer);
DCHECK(current_modules_);
......@@ -473,9 +472,8 @@ void NativeStackSamplerWin::ProfileRecordingStopped() {
}
// static
bool NativeStackSamplerWin::GetModuleForHandle(
HMODULE module_handle,
StackSamplingProfiler::Module* module) {
bool NativeStackSamplerWin::GetModuleForHandle(HMODULE module_handle,
Module* module) {
wchar_t module_name[MAX_PATH];
DWORD result_length =
::GetModuleFileName(module_handle, module_name, size(module_name));
......@@ -488,17 +486,16 @@ bool NativeStackSamplerWin::GetModuleForHandle(
return !module->id.empty();
}
size_t NativeStackSamplerWin::GetModuleIndex(
HMODULE module_handle,
std::vector<StackSamplingProfiler::Module>* modules) {
size_t NativeStackSamplerWin::GetModuleIndex(HMODULE module_handle,
std::vector<Module>* modules) {
if (!module_handle)
return StackSamplingProfiler::Frame::kUnknownModuleIndex;
return Frame::kUnknownModuleIndex;
auto loc = profile_module_index_.find(module_handle);
if (loc == profile_module_index_.end()) {
StackSamplingProfiler::Module module;
Module module;
if (!GetModuleForHandle(module_handle, &module))
return StackSamplingProfiler::Frame::kUnknownModuleIndex;
return Frame::kUnknownModuleIndex;
modules->push_back(module);
loc = profile_module_index_.insert(std::make_pair(
module_handle, modules->size() - 1)).first;
......@@ -507,10 +504,10 @@ size_t NativeStackSamplerWin::GetModuleIndex(
return loc->second;
}
std::vector<StackSamplingProfiler::Frame> NativeStackSamplerWin::CreateFrames(
std::vector<Frame> NativeStackSamplerWin::CreateFrames(
const std::vector<RecordedFrame>& stack,
std::vector<StackSamplingProfiler::Module>* modules) {
std::vector<StackSamplingProfiler::Frame> frames;
std::vector<Module>* modules) {
std::vector<Frame> frames;
frames.reserve(stack.size());
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