Chromium Code Reviews| Index: base/profiler/cpu_profiler_win.cc |
| diff --git a/base/profiler/cpu_profiler_win.cc b/base/profiler/cpu_profiler_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aa1947ad004af740c13ec8825c8a71a80c8c3280 |
| --- /dev/null |
| +++ b/base/profiler/cpu_profiler_win.cc |
| @@ -0,0 +1,178 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/profiler/cpu_profiler.h" |
| + |
| +#include <Tlhelp32.h> |
| +#include <dbghelp.h> |
| +#include <stddef.h> |
| +#include <windows.h> |
| + |
| +#include "base/debug/stack_trace.h" |
| +#include "base/threading/platform_thread.h" |
| +#include "base/threading/thread_id_name_manager.h" |
| +#include "base/time/time.h" |
| + |
| +namespace base { |
| + |
| +namespace { |
| + |
| +const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
| + |
| +int StackTrace64(CONTEXT* context, int stack_depth, |
| + StackTraceEntry* stack_trace) { |
| +#ifdef _WIN64 |
| + IMAGEHLP_SYMBOL64 sym; |
| + sym.SizeOfStruct = sizeof(sym); |
| + sym.MaxNameLength = 0; |
| + |
| + for (int i = 0; i < stack_depth; ++i, ++stack_trace) { |
| + // Try to look up unwind metadata for the current function. |
| + ULONG64 image_base; |
| + PRUNTIME_FUNCTION runtime_function = |
| + RtlLookupFunctionEntry(context->Rip, &image_base, nullptr); |
| + |
| + stack_trace->rsp = context->Rsp; |
| + stack_trace->rip = context->Rip; |
| + stack_trace->module = nullptr; |
| + |
| + if (runtime_function) { |
| + HMODULE module = NULL; |
| + if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, |
|
cpu_(ooo_6.6-7.5)
2015/02/05 01:03:26
I think or-ing both should work GET_MODULE_HANDLE_
Mike Wittman
2015/02/05 02:08:48
It seems that specifying GET_MODULE_HANDLE_EX_UNCH
|
| + reinterpret_cast<LPCTSTR>(context->Rip), &module)) { |
| + stack_trace->module = module; |
| + FreeLibrary(module); |
| + } |
| + |
| + KNONVOLATILE_CONTEXT_POINTERS nvcontext = {0}; |
| + PVOID handler_data; |
|
cpu_(ooo_6.6-7.5)
2015/02/05 01:03:27
pvoid --> void*
Mike Wittman
2015/02/05 02:08:48
Done.
|
| + ULONG64 establisher_frame; |
| + RtlVirtualUnwind(0, image_base, context->Rip, runtime_function, context, |
| + &handler_data, &establisher_frame, &nvcontext); |
| + } else { |
| + // If we don't have a RUNTIME_FUNCTION, then we've encountered |
| + // a leaf function. Adjust the stack appropriately. |
| + context->Rip = *reinterpret_cast<PDWORD64>(context->Rsp); |
| + context->Rsp += 8; |
| + } |
| + |
| + if (!context->Rip) |
| + return i; |
| + } |
| + return stack_depth; |
| +#else |
| + return 0; |
| +#endif |
| +} |
| + |
| +int SampleThread(HANDLE thread_handle, int max_stack_size, |
| + StackTraceEntry* stack) { |
| + if (!thread_handle) |
|
cpu_(ooo_6.6-7.5)
2015/02/05 01:03:26
can we not fire the timer when you don't have a th
Mike Wittman
2015/02/05 02:08:47
We should always have a thread handle on x64 since
|
| + return 0; |
| + |
| + if (::SuspendThread(thread_handle) == kSuspendFailed) { |
| + LOG(ERROR) << "SuspendThread failed: " << GetLastError(); |
| + return 0; |
| + } |
| + |
| + CONTEXT thread_context = {0}; |
| + thread_context.ContextFlags = CONTEXT_ALL; |
| + if (!::GetThreadContext(thread_handle, &thread_context)) { |
| + LOG(ERROR) << "GetThreadContext failed: " << GetLastError(); |
| + } |
| + |
| + int stack_depth = StackTrace64(&thread_context, max_stack_size, stack); |
| + |
| + if (::ResumeThread(thread_handle) == kSuspendFailed) { |
| + LOG(ERROR) << "ResumeThread failed: " << GetLastError(); |
| + } |
| + return stack_depth; |
| +} |
| + |
| +void GetNames(StackTraceEntry* stack_trace, int stack_depth) { |
| + HANDLE process = GetCurrentProcess(); |
| + DWORD64 displacement = 0; |
| + DWORD line_displacement = 0; |
| + char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(wchar_t)]; |
| + PSYMBOL_INFO symbol_info = reinterpret_cast<PSYMBOL_INFO>(buffer); |
|
cpu_(ooo_6.6-7.5)
2015/02/05 01:03:27
psymbol_info -->symbol_info*
Mike Wittman
2015/02/05 02:08:48
Done.
|
| + symbol_info->SizeOfStruct = sizeof(SYMBOL_INFO); |
| + symbol_info->MaxNameLen = MAX_SYM_NAME; |
| + std::string file_name; |
| + |
| + IMAGEHLP_LINE64 line; |
| + line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); |
| + |
| + for (int i = 0; i < stack_depth; ++i, ++stack_trace) { |
| + DWORD64 address = stack_trace->rip; |
| + if (!SymFromAddr(process, address, &displacement, symbol_info)) { |
| + wcscpy(reinterpret_cast<wchar_t*>(symbol_info->Name), L"failed"); |
|
cpu_(ooo_6.6-7.5)
2015/02/05 01:03:26
use the safe versions wcscpy_s
Mike Wittman
2015/02/05 02:08:48
Done.
|
| + } |
| + if (!SymGetLineFromAddr64(process, address, &line_displacement, &line)) { |
| + line.LineNumber = 0; |
| + file_name.clear(); |
| + } else { |
| + file_name.assign(line.FileName); |
| + } |
| + } |
| +} |
| + |
| +} |
| + |
| +CpuProfiler::CpuProfiler() |
| + : main_thread_(0), |
| + main_thread_stack_depth_(0) { |
| +#ifdef _WIN64 |
| + // Record the main thread's handle. |
| + BOOL result = ::DuplicateHandle(::GetCurrentProcess(), ::GetCurrentThread(), |
| + ::GetCurrentProcess(), &main_thread_, 0, |
| + FALSE, DUPLICATE_SAME_ACCESS); |
| + DCHECK(result) << "DuplicateHandle failed"; |
| + |
| + // Initialization |
| + if (RtlVirtualUnwind == NULL && RtlLookupFunctionEntry == NULL) { |
| + const HMODULE nt_dll_handle = ::GetModuleHandle(L"ntdll.dll"); |
| + reinterpret_cast<void*&>(RtlVirtualUnwind) = |
| + ::GetProcAddress(nt_dll_handle, "RtlVirtualUnwind"); |
| + reinterpret_cast<void*&>(RtlLookupFunctionEntry) = |
| + ::GetProcAddress(nt_dll_handle, "RtlLookupFunctionEntry"); |
| + } |
| +#endif |
| +} |
| + |
| +CpuProfiler::~CpuProfiler() { |
| + CloseHandle(main_thread_); |
| +} |
| + |
| +// static |
| +bool CpuProfiler::IsPlatformSupported() { |
| +#ifdef _WIN64 |
| + return true; |
| +#else |
| + return false; |
| +#endif |
| +} |
| + |
| +void CpuProfiler::OnTimer() { |
| + main_thread_stack_depth_ = SampleThread( |
| + main_thread_, arraysize(main_thread_stack_), main_thread_stack_); |
| + ProcessStack(main_thread_stack_, main_thread_stack_depth_); |
| + |
| + modules_.clear(); |
| + main_thread_stack_depth_ = 0; |
| +} |
| + |
| +void CpuProfiler::ProcessStack(StackTraceEntry* stack, int stack_depth) { |
| + for (int i = 0; i < stack_depth; ++i, ++stack) { |
| + HMODULE module = stack->module; |
| + if (!module || (modules_.find(module) != modules_.end())) |
| + continue; |
| + |
| + wchar_t module_name[MAX_PATH]; |
| + if (GetModuleFileName(module, module_name, arraysize(module_name))) { |
| + modules_.insert(std::pair<HMODULE, base::string16>(module, module_name)); |
| + } |
| + } |
| +} |
| + |
| +} // namespace base |