Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: base/profiler/stack_sampling_profiler_win.cc

Issue 1030923002: StackSamplingProfiler clean up (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: address initial comments Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/profiler/stack_sampling_profiler.h" 5 #include "base/profiler/stack_sampling_profiler.h"
6 6
7 #include <dbghelp.h> 7 #include <windows.h>
8
8 #include <map> 9 #include <map>
9 #include <utility> 10 #include <utility>
10 #include <windows.h>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "base/win/pe_image.h" 14 #include "base/win/pe_image.h"
15 #include "base/win/scoped_handle.h" 15 #include "base/win/scoped_handle.h"
16 16
17 namespace base { 17 namespace base {
18 18
19 namespace { 19 namespace {
20 20
21 class NativeStackSamplerWin : public StackSamplingProfiler::NativeStackSampler { 21 // Walks the stack represented by |context| from the current frame downwards,
22 public:
23 explicit NativeStackSamplerWin(win::ScopedHandle thread_handle);
24 ~NativeStackSamplerWin() override;
25
26 // StackSamplingProfiler::NativeStackSampler:
27 void ProfileRecordingStarting(
28 StackSamplingProfiler::Profile* profile) override;
29 void RecordStackSample(StackSamplingProfiler::Sample* sample) override;
30 void ProfileRecordingStopped() override;
31
32 private:
33 static bool GetModuleInfo(HMODULE module,
34 StackSamplingProfiler::Module* module_info);
35
36 void CopyToSample(const void* const instruction_pointers[],
37 const HMODULE modules[],
38 int stack_depth,
39 StackSamplingProfiler::Sample* sample,
40 std::vector<StackSamplingProfiler::Module>* module_infos);
41
42 win::ScopedHandle thread_handle_;
43 // Weak. Points to the profile being recorded between
44 // ProfileRecordingStarting() and ProfileRecordingStopped().
45 StackSamplingProfiler::Profile* current_profile_;
46 // Maps a module to the module's index within current_profile_->modules.
47 std::map<HMODULE, int> profile_module_index_;
48
49 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerWin);
50 };
51
52 // Walk the stack represented by |context| from the current frame downwards,
53 // recording the instruction pointers for each frame in |instruction_pointers|. 22 // recording the instruction pointers for each frame in |instruction_pointers|.
54 int RecordStack(CONTEXT* context, 23 int RecordStack(CONTEXT* context,
55 int max_stack_size, 24 int max_stack_size,
56 const void* instruction_pointers[], 25 const void* instruction_pointers[],
57 bool* last_frame_is_unknown_function) { 26 bool* last_frame_is_unknown_function) {
58 #ifdef _WIN64 27 #ifdef _WIN64
59 *last_frame_is_unknown_function = false; 28 *last_frame_is_unknown_function = false;
60 29
61 IMAGEHLP_SYMBOL64 sym; 30 int i = 0;
62 sym.SizeOfStruct = sizeof(sym); 31 for (; (i < max_stack_size) && context->Rip; ++i) {
63 sym.MaxNameLength = 0;
64
65 for (int i = 0; i < max_stack_size; ++i) {
66 // Try to look up unwind metadata for the current function. 32 // Try to look up unwind metadata for the current function.
67 ULONG64 image_base; 33 ULONG64 image_base;
68 PRUNTIME_FUNCTION runtime_function = 34 PRUNTIME_FUNCTION runtime_function =
69 RtlLookupFunctionEntry(context->Rip, &image_base, nullptr); 35 RtlLookupFunctionEntry(context->Rip, &image_base, nullptr);
70 36
71 instruction_pointers[i] = reinterpret_cast<void*>(context->Rip); 37 instruction_pointers[i] = reinterpret_cast<void*>(context->Rip);
72 38
73 if (runtime_function) { 39 if (runtime_function) {
74 KNONVOLATILE_CONTEXT_POINTERS nvcontext = {0}; 40 KNONVOLATILE_CONTEXT_POINTERS nvcontext = {0};
75 void* handler_data; 41 void* handler_data;
76 ULONG64 establisher_frame; 42 ULONG64 establisher_frame;
77 RtlVirtualUnwind(0, image_base, context->Rip, runtime_function, context, 43 RtlVirtualUnwind(0, image_base, context->Rip, runtime_function, context,
78 &handler_data, &establisher_frame, &nvcontext); 44 &handler_data, &establisher_frame, &nvcontext);
79 } else { 45 } else {
80 // If we don't have a RUNTIME_FUNCTION, then we've encountered 46 // If we don't have a RUNTIME_FUNCTION, then we've encountered a leaf
81 // a leaf function. Adjust the stack appropriately. 47 // function. Adjust the stack appropriately prior to the next function
48 // lookup.
82 context->Rip = *reinterpret_cast<PDWORD64>(context->Rsp); 49 context->Rip = *reinterpret_cast<PDWORD64>(context->Rsp);
83 context->Rsp += 8; 50 context->Rsp += 8;
84 *last_frame_is_unknown_function = true; 51 *last_frame_is_unknown_function = true;
85 } 52 }
86
87 if (!context->Rip)
88 return i;
89 } 53 }
90 return max_stack_size; 54 return i;
91 #else 55 #else
92 return 0; 56 return 0;
93 #endif 57 #endif
94 } 58 }
95 59
96 // Fills in |modules| corresponding to the pointers to code in |addresses|. The 60 // Fills in |modules| corresponding to the pointers to code in |addresses|. The
97 // modules are returned with reference counts incremented should be freed with 61 // modules are returned with reference counts incremented and should be freed
98 // FreeModules. 62 // with FreeModules.
99 void FindModulesForAddresses(const void* const addresses[], HMODULE modules[], 63 void FindModulesForAddresses(const void* const addresses[], HMODULE modules[],
100 int stack_depth, 64 int stack_depth,
101 bool last_frame_is_unknown_function) { 65 bool last_frame_is_unknown_function) {
102 const int module_frames = last_frame_is_unknown_function ? stack_depth - 1 : 66 const int module_frames =
103 stack_depth; 67 last_frame_is_unknown_function ? stack_depth - 1 : stack_depth;
104 for (int i = 0; i < module_frames; ++i) { 68 for (int i = 0; i < module_frames; ++i) {
105 HMODULE module = NULL; 69 HMODULE module = NULL;
106 if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, 70 if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
107 reinterpret_cast<LPCTSTR>(addresses[i]), 71 reinterpret_cast<LPCTSTR>(addresses[i]),
108 &module)) { 72 &module)) {
109 // HMODULE is the base address of the module. 73 // HMODULE actually represents the base address of the module, so we can
110 DCHECK_LT(reinterpret_cast<const void*>(module), addresses[i]); 74 // use it directly as an address.
75 DCHECK_LE(reinterpret_cast<const void*>(module), addresses[i]);
111 modules[i] = module; 76 modules[i] = module;
112 } 77 }
113 } 78 }
114 } 79 }
115 80
116 // Free the modules returned by FindModulesForAddresses. 81 // Frees the modules returned by FindModulesForAddresses.
117 void FreeModules(int stack_depth, HMODULE modules[]) { 82 void FreeModules(int stack_depth, HMODULE modules[]) {
118 for (int i = 0; i < stack_depth; ++i) { 83 for (int i = 0; i < stack_depth; ++i) {
119 if (modules[i]) 84 if (modules[i])
120 ::FreeLibrary(modules[i]); 85 ::FreeLibrary(modules[i]);
121 } 86 }
122 } 87 }
123 88
124 // Disables priority boost on a thread for the lifetime of the object. 89 // Disables priority boost on a thread for the lifetime of the object.
125 class ScopedDisablePriorityBoost { 90 class ScopedDisablePriorityBoost {
126 public: 91 public:
127 ScopedDisablePriorityBoost(HANDLE thread_handle); 92 ScopedDisablePriorityBoost(HANDLE thread_handle);
128 ~ScopedDisablePriorityBoost(); 93 ~ScopedDisablePriorityBoost();
129 94
130 private: 95 private:
131 HANDLE thread_handle_; 96 HANDLE thread_handle_;
132 BOOL got_previous_boost_state_; 97 BOOL got_previous_boost_state_;
133 BOOL boost_state_was_disabled_; 98 BOOL boost_state_was_disabled_;
134 99
135 DISALLOW_COPY_AND_ASSIGN(ScopedDisablePriorityBoost); 100 DISALLOW_COPY_AND_ASSIGN(ScopedDisablePriorityBoost);
136 }; 101 };
137 102
138 ScopedDisablePriorityBoost::ScopedDisablePriorityBoost(HANDLE thread_handle) 103 ScopedDisablePriorityBoost::ScopedDisablePriorityBoost(HANDLE thread_handle)
139 : thread_handle_(thread_handle), 104 : thread_handle_(thread_handle),
140 got_previous_boost_state_(false), 105 got_previous_boost_state_(false),
141 boost_state_was_disabled_(false) { 106 boost_state_was_disabled_(false) {
142 got_previous_boost_state_ = 107 got_previous_boost_state_ =
143 ::GetThreadPriorityBoost(thread_handle_, &boost_state_was_disabled_); 108 ::GetThreadPriorityBoost(thread_handle_, &boost_state_was_disabled_);
144 if (got_previous_boost_state_ && !boost_state_was_disabled_) { 109 if (got_previous_boost_state_) {
145 // Confusingly, TRUE disables priority boost ... 110 // Confusingly, TRUE disables priority boost.
146 ::SetThreadPriorityBoost(thread_handle_, TRUE); 111 ::SetThreadPriorityBoost(thread_handle_, TRUE);
147 } 112 }
148 } 113 }
149 114
150 ScopedDisablePriorityBoost::~ScopedDisablePriorityBoost() { 115 ScopedDisablePriorityBoost::~ScopedDisablePriorityBoost() {
151 if (got_previous_boost_state_ && !boost_state_was_disabled_) { 116 if (got_previous_boost_state_)
152 // ... and FALSE enables priority boost. 117 ::SetThreadPriorityBoost(thread_handle_, boost_state_was_disabled_);
153 ::SetThreadPriorityBoost(thread_handle_, FALSE);
154 }
155 } 118 }
156 119
157 // Suspends the thread with |thread_handle|, records the stack into 120 // Suspends the thread with |thread_handle|, records the stack into
158 // |instruction_pointers|, then resumes the thread. Returns the size of the 121 // |instruction_pointers|, then resumes the thread. Returns the size of the
159 // stack. 122 // stack.
123 //
124 // IMPORTANT NOTE: No heap allocations may occur between SuspendThread and
125 // ResumeThread. Otherwise this code can deadlock on heap locks acquired by the
126 // target thread before it was suspended. This is why we pass instruction
127 // pointers as a bare array rather than a vector.
160 int SuspendThreadAndRecordStack(HANDLE thread_handle, int max_stack_size, 128 int SuspendThreadAndRecordStack(HANDLE thread_handle, int max_stack_size,
161 const void* instruction_pointers[], 129 const void* instruction_pointers[],
162 bool* last_frame_is_unknown_function) { 130 bool* last_frame_is_unknown_function) {
163 #if defined(_WIN64)
164 if (RtlVirtualUnwind == nullptr || RtlLookupFunctionEntry == nullptr)
165 return 0;
166 #endif
167
168 if (::SuspendThread(thread_handle) == -1) { 131 if (::SuspendThread(thread_handle) == -1) {
169 LOG(ERROR) << "SuspendThread failed: " << GetLastError(); 132 NOTREACHED() << "SuspendThread failed: " << GetLastError();
Peter Kasting 2015/03/27 23:44:47 A side effect of changing LOG -> NOTREACHED, coupl
Mike Wittman 2015/03/30 21:01:13 Here also, it's not worth crashing people and comm
170 return 0; 133 return 0;
171 } 134 }
172 135
136 int stack_depth = 0;
173 CONTEXT thread_context = {0}; 137 CONTEXT thread_context = {0};
174 thread_context.ContextFlags = CONTEXT_FULL; 138 thread_context.ContextFlags = CONTEXT_FULL;
175 if (!::GetThreadContext(thread_handle, &thread_context)) { 139 if (::GetThreadContext(thread_handle, &thread_context)) {
176 LOG(ERROR) << "GetThreadContext failed: " << GetLastError(); 140 stack_depth = RecordStack(&thread_context, max_stack_size,
141 instruction_pointers,
142 last_frame_is_unknown_function);
143 } else {
144 NOTREACHED() << "GetThreadContext failed: " << GetLastError();
177 } 145 }
178 146
179 int stack_depth = RecordStack(&thread_context, max_stack_size, 147 // Disable the priority boost that the thread would otherwise receive on
180 instruction_pointers, 148 // resume. We do this to avoid artificially altering the dynamics of the
181 last_frame_is_unknown_function); 149 // executing application any more than we already are by suspending and
182 150 // resuming the thread.
183 { 151 ScopedDisablePriorityBoost disable_priority_boost(thread_handle);
184 ScopedDisablePriorityBoost disable_priority_boost(thread_handle); 152 if (::ResumeThread(thread_handle) == -1)
185 if (::ResumeThread(thread_handle) == -1) 153 NOTREACHED() << "ResumeThread failed: " << GetLastError();
186 LOG(ERROR) << "ResumeThread failed: " << GetLastError();
187 }
188 154
189 return stack_depth; 155 return stack_depth;
190 } 156 }
191 157
192 } // namespace 158 class NativeStackSamplerWin : public StackSamplingProfiler::NativeStackSampler {
159 public:
160 explicit NativeStackSamplerWin(win::ScopedHandle thread_handle);
161 ~NativeStackSamplerWin() override;
193 162
194 scoped_ptr<StackSamplingProfiler::NativeStackSampler> 163 // StackSamplingProfiler::NativeStackSampler:
195 StackSamplingProfiler::NativeStackSampler::Create(PlatformThreadId thread_id) { 164 void ProfileRecordingStarting(
196 #if _WIN64 165 StackSamplingProfiler::Profile* profile) override;
197 // Get the thread's handle. 166 void RecordStackSample(StackSamplingProfiler::Sample* sample) override;
198 HANDLE thread_handle = ::OpenThread( 167 void ProfileRecordingStopped() override;
199 THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION,
200 FALSE,
201 thread_id);
202 DCHECK(thread_handle) << "OpenThread failed";
203 168
204 return scoped_ptr<NativeStackSampler>(new NativeStackSamplerWin( 169 private:
205 win::ScopedHandle(thread_handle))); 170 // Attempts to query the module filename, base address, and id and store them
206 #else 171 // in |module_info|. Returns true if it succeeded.
207 return scoped_ptr<NativeStackSampler>(); 172 static bool GetModuleInfo(HMODULE module,
208 #endif 173 StackSamplingProfiler::Module* module_info);
209 } 174
175 // Gets the module index for |module| in |modules|, adding it if it's not
176 // already present. Returns StackSamplingProfiler::Frame::kUnknownModuleIndex
177 // if no Module can be determined for |module|
178 size_t GetModuleIndex(HMODULE module,
179 std::vector<StackSamplingProfiler::Module>* modules);
180
181 // Copies the stack information represented by |instruction_pointers| into
182 // |sample| and |module_infos|.
183 void CopyToSample(const void* const instruction_pointers[],
184 const HMODULE modules[],
185 int stack_depth,
186 StackSamplingProfiler::Sample* sample,
187 std::vector<StackSamplingProfiler::Module>* module_infos);
188
189 win::ScopedHandle thread_handle_;
190 // Weak. Points to the profile being recorded between
191 // ProfileRecordingStarting() and ProfileRecordingStopped().
192 StackSamplingProfiler::Profile* current_profile_;
193 // Maps a module to the module's index within current_profile_->modules.
194 std::map<HMODULE, size_t> profile_module_index_;
195
196 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerWin);
197 };
210 198
211 NativeStackSamplerWin::NativeStackSamplerWin(win::ScopedHandle thread_handle) 199 NativeStackSamplerWin::NativeStackSamplerWin(win::ScopedHandle thread_handle)
212 : thread_handle_(thread_handle.Take()) { 200 : thread_handle_(thread_handle.Take()) {
213 #ifdef _WIN64
214 if (RtlVirtualUnwind == nullptr && RtlLookupFunctionEntry == nullptr) {
215 const HMODULE nt_dll_handle = ::GetModuleHandle(L"ntdll.dll");
216 // This should always be non-null, but handle just in case.
217 if (nt_dll_handle) {
218 reinterpret_cast<void*&>(RtlVirtualUnwind) =
219 ::GetProcAddress(nt_dll_handle, "RtlVirtualUnwind");
220 reinterpret_cast<void*&>(RtlLookupFunctionEntry) =
221 ::GetProcAddress(nt_dll_handle, "RtlLookupFunctionEntry");
222 }
223 }
224 #endif
225 } 201 }
226 202
227 NativeStackSamplerWin::~NativeStackSamplerWin() { 203 NativeStackSamplerWin::~NativeStackSamplerWin() {
228 } 204 }
229 205
230 void NativeStackSamplerWin::ProfileRecordingStarting( 206 void NativeStackSamplerWin::ProfileRecordingStarting(
231 StackSamplingProfiler::Profile* profile) { 207 StackSamplingProfiler::Profile* profile) {
232 current_profile_ = profile; 208 current_profile_ = profile;
233 profile_module_index_.clear(); 209 profile_module_index_.clear();
234 } 210 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 if (result_length == 0) 242 if (result_length == 0)
267 return false; 243 return false;
268 244
269 module_info->filename = base::FilePath(module_name); 245 module_info->filename = base::FilePath(module_name);
270 246
271 module_info->base_address = reinterpret_cast<const void*>(module); 247 module_info->base_address = reinterpret_cast<const void*>(module);
272 248
273 GUID guid; 249 GUID guid;
274 DWORD age; 250 DWORD age;
275 win::PEImage(module).GetDebugId(&guid, &age); 251 win::PEImage(module).GetDebugId(&guid, &age);
276 module_info->id.insert(module_info->id.end(), 252 module_info->id.assign(reinterpret_cast<char*>(&guid), sizeof(guid));
277 reinterpret_cast<char*>(&guid), 253 module_info->id.append(reinterpret_cast<char*>(&age), sizeof(age));
278 reinterpret_cast<char*>(&guid + 1));
279 module_info->id.insert(module_info->id.end(),
280 reinterpret_cast<char*>(&age),
281 reinterpret_cast<char*>(&age + 1));
282 254
283 return true; 255 return true;
284 } 256 }
285 257
258 size_t NativeStackSamplerWin::GetModuleIndex(
259 HMODULE module,
260 std::vector<StackSamplingProfiler::Module>* modules) {
261 if (!module)
262 return StackSamplingProfiler::Frame::kUnknownModuleIndex;
263
264 auto loc = profile_module_index_.find(module);
265 if (loc == profile_module_index_.end()) {
266 StackSamplingProfiler::Module module_info;
267 if (!GetModuleInfo(module, &module_info))
268 return StackSamplingProfiler::Frame::kUnknownModuleIndex;
269 modules->push_back(module_info);
270 loc = profile_module_index_.insert(std::make_pair(
271 module, modules->size() - 1)).first;
272 }
273
274 return loc->second;
275 }
276
286 void NativeStackSamplerWin::CopyToSample( 277 void NativeStackSamplerWin::CopyToSample(
287 const void* const instruction_pointers[], 278 const void* const instruction_pointers[],
288 const HMODULE modules[], 279 const HMODULE modules[],
289 int stack_depth, 280 int stack_depth,
290 StackSamplingProfiler::Sample* sample, 281 StackSamplingProfiler::Sample* sample,
291 std::vector<StackSamplingProfiler::Module>* module_infos) { 282 std::vector<StackSamplingProfiler::Module>* module_infos) {
292 sample->clear(); 283 sample->clear();
293 sample->reserve(stack_depth); 284 sample->reserve(stack_depth);
294 285
295 for (int i = 0; i < stack_depth; ++i) { 286 for (int i = 0; i < stack_depth; ++i) {
296 sample->push_back(StackSamplingProfiler::Frame()); 287 sample->push_back(StackSamplingProfiler::Frame(
297 StackSamplingProfiler::Frame& frame = sample->back(); 288 instruction_pointers[i], GetModuleIndex(modules[i], module_infos)));
298
299 frame.instruction_pointer = instruction_pointers[i];
300
301 // Record an invalid module index if we don't have a valid module.
302 if (!modules[i]) {
303 frame.module_index = -1;
304 continue;
305 }
306
307 auto loc = profile_module_index_.find(modules[i]);
308 if (loc == profile_module_index_.end()) {
309 StackSamplingProfiler::Module module_info;
310 // Record an invalid module index if we have a module but can't find
311 // information on it.
312 if (!GetModuleInfo(modules[i], &module_info)) {
313 frame.module_index = -1;
314 continue;
315 }
316 module_infos->push_back(module_info);
317 loc = profile_module_index_.insert(std::make_pair(
318 modules[i], static_cast<int>(module_infos->size() - 1))).first;
319 }
320
321 frame.module_index = loc->second;
322 } 289 }
323 } 290 }
324 291
292 } // namespace
293
294 scoped_ptr<StackSamplingProfiler::NativeStackSampler>
295 StackSamplingProfiler::NativeStackSampler::Create(PlatformThreadId thread_id) {
296 #if _WIN64
297 // Get the thread's handle.
298 HANDLE thread_handle = ::OpenThread(
299 THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION,
300 FALSE,
301 thread_id);
302
303 if (thread_handle) {
304 return scoped_ptr<NativeStackSampler>(new NativeStackSamplerWin(
305 win::ScopedHandle(thread_handle)));
306 }
307 #endif
308 return scoped_ptr<NativeStackSampler>();
309 }
310
325 } // namespace base 311 } // namespace base
312
OLDNEW
« base/profiler/stack_sampling_profiler.cc ('K') | « base/profiler/stack_sampling_profiler_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698