OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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_sampling_profiler.h" |
| 6 |
| 7 #include <dbghelp.h> |
| 8 #include <map> |
| 9 #include <utility> |
| 10 #include <windows.h> |
| 11 |
| 12 #include "base/logging.h" |
| 13 #include "base/time/time.h" |
| 14 #include "base/win/pe_image.h" |
| 15 #include "base/win/scoped_handle.h" |
| 16 |
| 17 namespace base { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class NativeStackSamplerWin : public StackSamplingProfiler::NativeStackSampler { |
| 22 public: |
| 23 NativeStackSamplerWin(win::ScopedHandle thread_handle); |
| 24 ~NativeStackSamplerWin() override; |
| 25 |
| 26 void ProfileRecordingStarting( |
| 27 StackSamplingProfiler::Profile* profile) override; |
| 28 void RecordStackSample(StackSamplingProfiler::Sample* sample) override; |
| 29 void ProfileRecordingStopped() override; |
| 30 |
| 31 private: |
| 32 static bool GetModuleInfo(HMODULE module, |
| 33 StackSamplingProfiler::Module* module_info); |
| 34 |
| 35 void CopyToSample(const void* const instruction_pointers[], |
| 36 const HMODULE modules[], int stack_depth, |
| 37 StackSamplingProfiler::Sample* sample, |
| 38 std::vector<StackSamplingProfiler::Module>* module_infos); |
| 39 |
| 40 win::ScopedHandle thread_handle_; |
| 41 StackSamplingProfiler::Profile* current_profile_; |
| 42 std::map<HMODULE, int> profile_module_index_; |
| 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerWin); |
| 45 }; |
| 46 |
| 47 |
| 48 // Walk the stack represented by |context| from the current frame downwards, |
| 49 // recording the instruction pointers for each frame in |instruction_pointers|. |
| 50 int RecordStack(CONTEXT* context, int max_stack_size, |
| 51 const void* instruction_pointers[], |
| 52 bool* last_frame_is_unknown_function) { |
| 53 #ifdef _WIN64 |
| 54 *last_frame_is_unknown_function = false; |
| 55 |
| 56 IMAGEHLP_SYMBOL64 sym; |
| 57 sym.SizeOfStruct = sizeof(sym); |
| 58 sym.MaxNameLength = 0; |
| 59 |
| 60 for (int i = 0; i < max_stack_size; ++i) { |
| 61 // Try to look up unwind metadata for the current function. |
| 62 ULONG64 image_base; |
| 63 PRUNTIME_FUNCTION runtime_function = |
| 64 RtlLookupFunctionEntry(context->Rip, &image_base, nullptr); |
| 65 |
| 66 instruction_pointers[i] = reinterpret_cast<void*>(context->Rip); |
| 67 |
| 68 if (runtime_function) { |
| 69 KNONVOLATILE_CONTEXT_POINTERS nvcontext = {0}; |
| 70 void* handler_data; |
| 71 ULONG64 establisher_frame; |
| 72 RtlVirtualUnwind(0, image_base, context->Rip, runtime_function, context, |
| 73 &handler_data, &establisher_frame, &nvcontext); |
| 74 } else { |
| 75 // If we don't have a RUNTIME_FUNCTION, then we've encountered |
| 76 // a leaf function. Adjust the stack appropriately. |
| 77 context->Rip = *reinterpret_cast<PDWORD64>(context->Rsp); |
| 78 context->Rsp += 8; |
| 79 *last_frame_is_unknown_function = true; |
| 80 } |
| 81 |
| 82 if (!context->Rip) |
| 83 return i; |
| 84 } |
| 85 return max_stack_size; |
| 86 #else |
| 87 return 0; |
| 88 #endif |
| 89 } |
| 90 |
| 91 // Fills in |modules| corresponding to the pointers to code in |addresses|. |
| 92 void FindModulesForAddresses(const void* const addresses[], HMODULE modules[], |
| 93 int stack_depth, |
| 94 bool last_frame_is_unknown_function) { |
| 95 const int module_frames = last_frame_is_unknown_function ? stack_depth - 1 : |
| 96 stack_depth; |
| 97 for (int i = 0; i < module_frames; ++i) { |
| 98 HMODULE module = NULL; |
| 99 if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 100 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 101 reinterpret_cast<LPCTSTR>(addresses[i]), |
| 102 &module)) { |
| 103 modules[i] = module; |
| 104 } |
| 105 } |
| 106 for (int i = module_frames; i < stack_depth; ++i) |
| 107 modules[stack_depth - 1] = NULL; |
| 108 } |
| 109 |
| 110 // Suspends the thread with |thread_handle|, records the stack into |
| 111 // |instruction_pointers| and the corresponding modules into |modules|, then |
| 112 // resumes the thread. Returns the size of the stack. |
| 113 int SuspendThreadAndRecordStack(HANDLE thread_handle, int max_stack_size, |
| 114 const void* instruction_pointers[], |
| 115 HMODULE modules[]) { |
| 116 if (::SuspendThread(thread_handle) == -1) { |
| 117 LOG(ERROR) << "SuspendThread failed: " << GetLastError(); |
| 118 return 0; |
| 119 } |
| 120 |
| 121 CONTEXT thread_context = {0}; |
| 122 thread_context.ContextFlags = CONTEXT_ALL; |
| 123 if (!::GetThreadContext(thread_handle, &thread_context)) { |
| 124 LOG(ERROR) << "GetThreadContext failed: " << GetLastError(); |
| 125 } |
| 126 |
| 127 bool last_frame_is_unknown_function = false; |
| 128 int stack_depth = RecordStack(&thread_context, max_stack_size, |
| 129 instruction_pointers, |
| 130 &last_frame_is_unknown_function); |
| 131 |
| 132 if (::ResumeThread(thread_handle) == -1) |
| 133 LOG(ERROR) << "ResumeThread failed: " << GetLastError(); |
| 134 |
| 135 FindModulesForAddresses(instruction_pointers, modules, stack_depth, |
| 136 last_frame_is_unknown_function); |
| 137 |
| 138 return stack_depth; |
| 139 } |
| 140 |
| 141 } |
| 142 |
| 143 scoped_ptr<StackSamplingProfiler::NativeStackSampler> |
| 144 StackSamplingProfiler::NativeStackSampler::Create( |
| 145 PlatformThreadId thread_id) { |
| 146 #if _WIN64 |
| 147 // Get the thread's handle. |
| 148 HANDLE thread_handle = ::OpenThread(THREAD_ALL_ACCESS, FALSE, thread_id); |
| 149 DCHECK(thread_handle) << "OpenThread failed"; |
| 150 |
| 151 return scoped_ptr<NativeStackSampler>(new NativeStackSamplerWin( |
| 152 win::ScopedHandle(thread_handle))); |
| 153 #else |
| 154 return scoped_ptr<NativeStackSampler>(); |
| 155 #endif |
| 156 } |
| 157 |
| 158 NativeStackSamplerWin::NativeStackSamplerWin(win::ScopedHandle thread_handle) |
| 159 : thread_handle_(thread_handle.Take()) { |
| 160 #ifdef _WIN64 |
| 161 if (RtlVirtualUnwind == nullptr && RtlLookupFunctionEntry == nullptr) { |
| 162 const HMODULE nt_dll_handle = ::GetModuleHandle(L"ntdll.dll"); |
| 163 reinterpret_cast<void*&>(RtlVirtualUnwind) = |
| 164 ::GetProcAddress(nt_dll_handle, "RtlVirtualUnwind"); |
| 165 reinterpret_cast<void*&>(RtlLookupFunctionEntry) = |
| 166 ::GetProcAddress(nt_dll_handle, "RtlLookupFunctionEntry"); |
| 167 } |
| 168 #endif |
| 169 } |
| 170 |
| 171 NativeStackSamplerWin::~NativeStackSamplerWin() { |
| 172 } |
| 173 |
| 174 void NativeStackSamplerWin::ProfileRecordingStarting( |
| 175 StackSamplingProfiler::Profile* profile) { |
| 176 current_profile_ = profile; |
| 177 profile_module_index_.clear(); |
| 178 } |
| 179 |
| 180 void NativeStackSamplerWin::RecordStackSample( |
| 181 StackSamplingProfiler::Sample* sample) { |
| 182 DCHECK(current_profile_); |
| 183 |
| 184 const int max_stack_size = 64; |
| 185 const void* instruction_pointers[max_stack_size]; |
| 186 HMODULE modules[max_stack_size]; |
| 187 |
| 188 int stack_depth = SuspendThreadAndRecordStack( |
| 189 thread_handle_.Get(), max_stack_size, instruction_pointers, modules); |
| 190 CopyToSample(instruction_pointers, modules, stack_depth, sample, |
| 191 ¤t_profile_->modules); |
| 192 } |
| 193 |
| 194 void NativeStackSamplerWin::ProfileRecordingStopped() { |
| 195 current_profile_ = nullptr; |
| 196 } |
| 197 |
| 198 // static |
| 199 bool NativeStackSamplerWin::GetModuleInfo( |
| 200 HMODULE module, StackSamplingProfiler::Module* module_info) { |
| 201 wchar_t module_name[MAX_PATH]; |
| 202 DWORD result_length = |
| 203 GetModuleFileName(module, module_name, arraysize(module_name)); |
| 204 if (result_length == 0) |
| 205 return false; |
| 206 |
| 207 module_info->filename = base::FilePath(module_name); |
| 208 |
| 209 GUID guid; |
| 210 DWORD age; |
| 211 win::PEImage(module).GetDebugId(&guid, &age); |
| 212 module_info->id.insert(module_info->id.end(), |
| 213 reinterpret_cast<char*>(&guid), |
| 214 reinterpret_cast<char*>(&guid + 1)); |
| 215 module_info->id.insert(module_info->id.end(), |
| 216 reinterpret_cast<char*>(&age), |
| 217 reinterpret_cast<char*>(&age + 1)); |
| 218 |
| 219 return true; |
| 220 } |
| 221 |
| 222 void NativeStackSamplerWin::CopyToSample( |
| 223 const void* const instruction_pointers[], const HMODULE modules[], |
| 224 int stack_depth, StackSamplingProfiler::Sample* sample, |
| 225 std::vector<StackSamplingProfiler::Module>* module_infos) { |
| 226 sample->clear(); |
| 227 sample->reserve(stack_depth); |
| 228 |
| 229 for (int i = 0; i < stack_depth; ++i) { |
| 230 sample->push_back(StackSamplingProfiler::Frame()); |
| 231 StackSamplingProfiler::Frame& frame = sample->back(); |
| 232 |
| 233 if (!modules[i]) |
| 234 continue; |
| 235 |
| 236 auto loc = profile_module_index_.find(modules[i]); |
| 237 if (loc == profile_module_index_.end()) { |
| 238 StackSamplingProfiler::Module module_info; |
| 239 if (!GetModuleInfo(modules[i], &module_info)) |
| 240 continue; |
| 241 module_infos->push_back(module_info); |
| 242 loc = profile_module_index_.insert(std::make_pair( |
| 243 modules[i], static_cast<int>(module_infos->size()))).first; |
| 244 } |
| 245 |
| 246 frame.module_index = loc->second; |
| 247 |
| 248 int64 module_offset = |
| 249 reinterpret_cast<const char*>(instruction_pointers[i]) - |
| 250 reinterpret_cast<const char*>(modules[i]); |
| 251 DCHECK_GE(module_offset, 0); |
| 252 frame.ip_offset = static_cast<uint64>(module_offset); |
| 253 } |
| 254 } |
| 255 |
| 256 } // namespace base |
OLD | NEW |