Chromium Code Reviews| 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); | |
|
Alexei Svitkine (slow)
2015/03/17 13:10:00
explicit
Mike Wittman
2015/03/17 19:07:38
Done.
| |
| 24 ~NativeStackSamplerWin() override; | |
| 25 | |
| 26 void ProfileRecordingStarting( | |
|
Alexei Svitkine (slow)
2015/03/17 13:10:00
Add a comment:
// StackSamplingProfiler::NativeSt
Mike Wittman
2015/03/17 19:07:38
Done.
| |
| 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_; | |
|
Alexei Svitkine (slow)
2015/03/17 13:10:00
Please add comments. What's the ownership of this?
Mike Wittman
2015/03/17 19:07:38
Done.
| |
| 42 std::map<HMODULE, int> profile_module_index_; | |
|
Alexei Svitkine (slow)
2015/03/17 13:10:00
Please add comments, what's this mapping to?
Mike Wittman
2015/03/17 19:07:38
Done.
| |
| 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 // HMODULE is the base address of the module. | |
| 104 DCHECK_LT(reinterpret_cast<const void*>(module), addresses[i]); | |
| 105 modules[i] = module; | |
| 106 } | |
| 107 } | |
| 108 for (int i = module_frames; i < stack_depth; ++i) | |
| 109 modules[stack_depth - 1] = NULL; | |
| 110 } | |
| 111 | |
| 112 // Suspends the thread with |thread_handle|, records the stack into | |
| 113 // |instruction_pointers| and the corresponding modules into |modules|, then | |
| 114 // resumes the thread. Returns the size of the stack. | |
| 115 int SuspendThreadAndRecordStack(HANDLE thread_handle, int max_stack_size, | |
| 116 const void* instruction_pointers[], | |
| 117 HMODULE modules[]) { | |
| 118 if (::SuspendThread(thread_handle) == -1) { | |
| 119 LOG(ERROR) << "SuspendThread failed: " << GetLastError(); | |
| 120 return 0; | |
| 121 } | |
| 122 | |
| 123 CONTEXT thread_context = {0}; | |
| 124 thread_context.ContextFlags = CONTEXT_ALL; | |
| 125 if (!::GetThreadContext(thread_handle, &thread_context)) { | |
| 126 LOG(ERROR) << "GetThreadContext failed: " << GetLastError(); | |
| 127 } | |
| 128 | |
| 129 bool last_frame_is_unknown_function = false; | |
| 130 int stack_depth = RecordStack(&thread_context, max_stack_size, | |
| 131 instruction_pointers, | |
| 132 &last_frame_is_unknown_function); | |
| 133 | |
| 134 if (::ResumeThread(thread_handle) == -1) | |
| 135 LOG(ERROR) << "ResumeThread failed: " << GetLastError(); | |
| 136 | |
| 137 FindModulesForAddresses(instruction_pointers, modules, stack_depth, | |
| 138 last_frame_is_unknown_function); | |
| 139 | |
| 140 return stack_depth; | |
| 141 } | |
| 142 | |
| 143 } | |
|
Alexei Svitkine (slow)
2015/03/17 13:10:00
Nit: // namespace base
Mike Wittman
2015/03/17 19:07:38
Done. (This is end of the anonymous namespace.)
| |
| 144 | |
| 145 scoped_ptr<StackSamplingProfiler::NativeStackSampler> | |
| 146 StackSamplingProfiler::NativeStackSampler::Create( | |
| 147 PlatformThreadId thread_id) { | |
| 148 #if _WIN64 | |
| 149 // Get the thread's handle. | |
| 150 HANDLE thread_handle = ::OpenThread(THREAD_ALL_ACCESS, FALSE, thread_id); | |
| 151 DCHECK(thread_handle) << "OpenThread failed"; | |
| 152 | |
| 153 return scoped_ptr<NativeStackSampler>(new NativeStackSamplerWin( | |
| 154 win::ScopedHandle(thread_handle))); | |
| 155 #else | |
| 156 return scoped_ptr<NativeStackSampler>(); | |
| 157 #endif | |
| 158 } | |
| 159 | |
| 160 NativeStackSamplerWin::NativeStackSamplerWin(win::ScopedHandle thread_handle) | |
| 161 : thread_handle_(thread_handle.Take()) { | |
| 162 #ifdef _WIN64 | |
| 163 if (RtlVirtualUnwind == nullptr && RtlLookupFunctionEntry == nullptr) { | |
| 164 const HMODULE nt_dll_handle = ::GetModuleHandle(L"ntdll.dll"); | |
| 165 reinterpret_cast<void*&>(RtlVirtualUnwind) = | |
| 166 ::GetProcAddress(nt_dll_handle, "RtlVirtualUnwind"); | |
| 167 reinterpret_cast<void*&>(RtlLookupFunctionEntry) = | |
| 168 ::GetProcAddress(nt_dll_handle, "RtlLookupFunctionEntry"); | |
| 169 } | |
| 170 #endif | |
| 171 } | |
| 172 | |
| 173 NativeStackSamplerWin::~NativeStackSamplerWin() { | |
| 174 } | |
| 175 | |
| 176 void NativeStackSamplerWin::ProfileRecordingStarting( | |
| 177 StackSamplingProfiler::Profile* profile) { | |
| 178 current_profile_ = profile; | |
| 179 profile_module_index_.clear(); | |
| 180 } | |
| 181 | |
| 182 void NativeStackSamplerWin::RecordStackSample( | |
| 183 StackSamplingProfiler::Sample* sample) { | |
| 184 DCHECK(current_profile_); | |
| 185 | |
| 186 const int max_stack_size = 64; | |
| 187 const void* instruction_pointers[max_stack_size]; | |
| 188 HMODULE modules[max_stack_size]; | |
| 189 | |
| 190 int stack_depth = SuspendThreadAndRecordStack( | |
| 191 thread_handle_.Get(), max_stack_size, instruction_pointers, modules); | |
| 192 CopyToSample(instruction_pointers, modules, stack_depth, sample, | |
| 193 ¤t_profile_->modules); | |
| 194 } | |
| 195 | |
| 196 void NativeStackSamplerWin::ProfileRecordingStopped() { | |
| 197 current_profile_ = nullptr; | |
| 198 } | |
| 199 | |
| 200 // static | |
| 201 bool NativeStackSamplerWin::GetModuleInfo( | |
| 202 HMODULE module, StackSamplingProfiler::Module* module_info) { | |
| 203 wchar_t module_name[MAX_PATH]; | |
| 204 DWORD result_length = | |
| 205 GetModuleFileName(module, module_name, arraysize(module_name)); | |
| 206 if (result_length == 0) | |
| 207 return false; | |
| 208 | |
| 209 module_info->filename = base::FilePath(module_name); | |
| 210 | |
| 211 module_info->base_address = reinterpret_cast<const void*>(module); | |
| 212 | |
| 213 GUID guid; | |
| 214 DWORD age; | |
| 215 win::PEImage(module).GetDebugId(&guid, &age); | |
| 216 module_info->id.insert(module_info->id.end(), | |
| 217 reinterpret_cast<char*>(&guid), | |
| 218 reinterpret_cast<char*>(&guid + 1)); | |
| 219 module_info->id.insert(module_info->id.end(), | |
| 220 reinterpret_cast<char*>(&age), | |
| 221 reinterpret_cast<char*>(&age + 1)); | |
| 222 | |
| 223 return true; | |
| 224 } | |
| 225 | |
| 226 void NativeStackSamplerWin::CopyToSample( | |
| 227 const void* const instruction_pointers[], const HMODULE modules[], | |
|
Alexei Svitkine (slow)
2015/03/17 13:10:00
Nit: 1 param per line, when first param is on a se
Mike Wittman
2015/03/17 19:07:38
Done.
| |
| 228 int stack_depth, StackSamplingProfiler::Sample* sample, | |
| 229 std::vector<StackSamplingProfiler::Module>* module_infos) { | |
| 230 sample->clear(); | |
| 231 sample->reserve(stack_depth); | |
| 232 | |
| 233 for (int i = 0; i < stack_depth; ++i) { | |
| 234 sample->push_back(StackSamplingProfiler::Frame()); | |
| 235 StackSamplingProfiler::Frame& frame = sample->back(); | |
|
Alexei Svitkine (slow)
2015/03/17 13:10:00
Non-const refs are discouraged. Use a pointer. Or,
Mike Wittman
2015/03/17 19:07:38
They are for function parameters, but aliases are
| |
| 236 | |
| 237 if (!modules[i]) | |
|
Alexei Svitkine (slow)
2015/03/17 13:10:00
Is it correct to have an entry in |sample| in this
Mike Wittman
2015/03/17 19:07:38
Yes. In this case, we saw a frame on the stack but
Mike Wittman
2015/03/18 19:17:22
On second thought, storing the instruction pointer
| |
| 238 continue; | |
| 239 | |
| 240 auto loc = profile_module_index_.find(modules[i]); | |
| 241 if (loc == profile_module_index_.end()) { | |
| 242 StackSamplingProfiler::Module module_info; | |
| 243 if (!GetModuleInfo(modules[i], &module_info)) | |
| 244 continue; | |
| 245 module_infos->push_back(module_info); | |
| 246 loc = profile_module_index_.insert(std::make_pair( | |
| 247 modules[i], static_cast<int>(module_infos->size() - 1))).first; | |
| 248 } | |
| 249 | |
| 250 frame.module_index = loc->second; | |
| 251 frame.instruction_pointer = instruction_pointers[i]; | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 } // namespace base | |
| OLD | NEW |