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

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 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 <windows.h>
6 6
7 #include <dbghelp.h>
8 #include <map> 7 #include <map>
9 #include <utility> 8 #include <utility>
10 #include <windows.h>
11 9
12 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/profiler/native_stack_sampler.h"
13 #include "base/time/time.h" 12 #include "base/time/time.h"
14 #include "base/win/pe_image.h" 13 #include "base/win/pe_image.h"
15 #include "base/win/scoped_handle.h" 14 #include "base/win/scoped_handle.h"
16 15
17 namespace base { 16 namespace base {
18 17
19 namespace { 18 namespace {
20 19
21 class NativeStackSamplerWin : public StackSamplingProfiler::NativeStackSampler { 20 // 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|. 21 // recording the instruction pointers for each frame in |instruction_pointers|.
54 int RecordStack(CONTEXT* context, 22 int RecordStack(CONTEXT* context,
55 int max_stack_size, 23 int max_stack_size,
56 const void* instruction_pointers[], 24 const void* instruction_pointers[],
57 bool* last_frame_is_unknown_function) { 25 bool* last_frame_is_unknown_function) {
58 #ifdef _WIN64 26 #ifdef _WIN64
59 *last_frame_is_unknown_function = false; 27 *last_frame_is_unknown_function = false;
60 28
61 IMAGEHLP_SYMBOL64 sym; 29 int i = 0;
62 sym.SizeOfStruct = sizeof(sym); 30 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. 31 // Try to look up unwind metadata for the current function.
67 ULONG64 image_base; 32 ULONG64 image_base;
68 PRUNTIME_FUNCTION runtime_function = 33 PRUNTIME_FUNCTION runtime_function =
69 RtlLookupFunctionEntry(context->Rip, &image_base, nullptr); 34 RtlLookupFunctionEntry(context->Rip, &image_base, nullptr);
70 35
71 instruction_pointers[i] = reinterpret_cast<void*>(context->Rip); 36 instruction_pointers[i] = reinterpret_cast<const void*>(context->Rip);
72 37
73 if (runtime_function) { 38 if (runtime_function) {
74 KNONVOLATILE_CONTEXT_POINTERS nvcontext = {0}; 39 KNONVOLATILE_CONTEXT_POINTERS nvcontext = {0};
75 void* handler_data; 40 void* handler_data;
76 ULONG64 establisher_frame; 41 ULONG64 establisher_frame;
77 RtlVirtualUnwind(0, image_base, context->Rip, runtime_function, context, 42 RtlVirtualUnwind(0, image_base, context->Rip, runtime_function, context,
78 &handler_data, &establisher_frame, &nvcontext); 43 &handler_data, &establisher_frame, &nvcontext);
79 } else { 44 } else {
80 // If we don't have a RUNTIME_FUNCTION, then we've encountered 45 // If we don't have a RUNTIME_FUNCTION, then we've encountered a leaf
81 // a leaf function. Adjust the stack appropriately. 46 // function. Adjust the stack appropriately prior to the next function
47 // lookup.
82 context->Rip = *reinterpret_cast<PDWORD64>(context->Rsp); 48 context->Rip = *reinterpret_cast<PDWORD64>(context->Rsp);
83 context->Rsp += 8; 49 context->Rsp += 8;
84 *last_frame_is_unknown_function = true; 50 *last_frame_is_unknown_function = true;
85 } 51 }
86
87 if (!context->Rip)
88 return i;
89 } 52 }
90 return max_stack_size; 53 return i;
91 #else 54 #else
92 return 0; 55 return 0;
93 #endif 56 #endif
94 } 57 }
95 58
96 // Fills in |modules| corresponding to the pointers to code in |addresses|. The 59 // Fills in |modules| corresponding to the pointers to code in |addresses|. The
97 // modules are returned with reference counts incremented should be freed with 60 // modules are returned with reference counts incremented and should be freed
98 // FreeModules. 61 // with FreeModules. See note in SuspendThreadAndRecordStack for why |addresses|
62 // and modules are |arrays|.
Peter Kasting 2015/03/30 23:07:35 Nit: I think you meant to put the || around "modul
Mike Wittman 2015/03/31 01:06:37 Oops, yes.
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. See note in
82 // SuspendThreadAndRecordStack for why |modules| is an array.
117 void FreeModules(int stack_depth, HMODULE modules[]) { 83 void FreeModules(int stack_depth, HMODULE modules[]) {
118 for (int i = 0; i < stack_depth; ++i) { 84 for (int i = 0; i < stack_depth; ++i) {
119 if (modules[i]) 85 if (modules[i])
120 ::FreeLibrary(modules[i]); 86 ::FreeLibrary(modules[i]);
121 } 87 }
122 } 88 }
123 89
124 // Disables priority boost on a thread for the lifetime of the object. 90 // Disables priority boost on a thread for the lifetime of the object.
125 class ScopedDisablePriorityBoost { 91 class ScopedDisablePriorityBoost {
126 public: 92 public:
127 ScopedDisablePriorityBoost(HANDLE thread_handle); 93 ScopedDisablePriorityBoost(HANDLE thread_handle);
128 ~ScopedDisablePriorityBoost(); 94 ~ScopedDisablePriorityBoost();
129 95
130 private: 96 private:
131 HANDLE thread_handle_; 97 HANDLE thread_handle_;
132 BOOL got_previous_boost_state_; 98 BOOL got_previous_boost_state_;
133 BOOL boost_state_was_disabled_; 99 BOOL boost_state_was_disabled_;
134 100
135 DISALLOW_COPY_AND_ASSIGN(ScopedDisablePriorityBoost); 101 DISALLOW_COPY_AND_ASSIGN(ScopedDisablePriorityBoost);
136 }; 102 };
137 103
138 ScopedDisablePriorityBoost::ScopedDisablePriorityBoost(HANDLE thread_handle) 104 ScopedDisablePriorityBoost::ScopedDisablePriorityBoost(HANDLE thread_handle)
139 : thread_handle_(thread_handle), 105 : thread_handle_(thread_handle),
140 got_previous_boost_state_(false), 106 got_previous_boost_state_(false),
141 boost_state_was_disabled_(false) { 107 boost_state_was_disabled_(false) {
142 got_previous_boost_state_ = 108 got_previous_boost_state_ =
143 ::GetThreadPriorityBoost(thread_handle_, &boost_state_was_disabled_); 109 ::GetThreadPriorityBoost(thread_handle_, &boost_state_was_disabled_);
144 if (got_previous_boost_state_ && !boost_state_was_disabled_) { 110 if (got_previous_boost_state_) {
145 // Confusingly, TRUE disables priority boost ... 111 // Confusingly, TRUE disables priority boost.
146 ::SetThreadPriorityBoost(thread_handle_, TRUE); 112 ::SetThreadPriorityBoost(thread_handle_, TRUE);
147 } 113 }
148 } 114 }
149 115
150 ScopedDisablePriorityBoost::~ScopedDisablePriorityBoost() { 116 ScopedDisablePriorityBoost::~ScopedDisablePriorityBoost() {
151 if (got_previous_boost_state_ && !boost_state_was_disabled_) { 117 if (got_previous_boost_state_)
152 // ... and FALSE enables priority boost. 118 ::SetThreadPriorityBoost(thread_handle_, boost_state_was_disabled_);
153 ::SetThreadPriorityBoost(thread_handle_, FALSE);
154 }
155 } 119 }
156 120
157 // Suspends the thread with |thread_handle|, records the stack into 121 // Suspends the thread with |thread_handle|, records the stack into
158 // |instruction_pointers|, then resumes the thread. Returns the size of the 122 // |instruction_pointers|, then resumes the thread. Returns the size of the
159 // stack. 123 // stack.
124 //
125 // IMPORTANT NOTE: No heap allocations may occur between SuspendThread and
126 // ResumeThread. Otherwise this code can deadlock on heap locks acquired by the
127 // target thread before it was suspended. This is why we pass instruction
128 // pointers and modules as bare arrays rather than vectors.
Peter Kasting 2015/03/30 23:07:35 Nit: bare -> preallocated? Maybe add ", since vec
Mike Wittman 2015/03/31 01:06:37 Being explicit sounds OK to me.
160 int SuspendThreadAndRecordStack(HANDLE thread_handle, int max_stack_size, 129 int SuspendThreadAndRecordStack(HANDLE thread_handle, int max_stack_size,
161 const void* instruction_pointers[], 130 const void* instruction_pointers[],
162 bool* last_frame_is_unknown_function) { 131 bool* last_frame_is_unknown_function) {
163 #if defined(_WIN64) 132 if (::SuspendThread(thread_handle) == -1)
164 if (RtlVirtualUnwind == nullptr || RtlLookupFunctionEntry == nullptr)
165 return 0; 133 return 0;
166 #endif
167 134
168 if (::SuspendThread(thread_handle) == -1) { 135 int stack_depth = 0;
169 LOG(ERROR) << "SuspendThread failed: " << GetLastError(); 136 CONTEXT thread_context = {0};
170 return 0; 137 thread_context.ContextFlags = CONTEXT_FULL;
138 if (::GetThreadContext(thread_handle, &thread_context)) {
139 stack_depth = RecordStack(&thread_context, max_stack_size,
140 instruction_pointers,
141 last_frame_is_unknown_function);
171 } 142 }
172 143
173 CONTEXT thread_context = {0}; 144 // Disable the priority boost that the thread would otherwise receive on
174 thread_context.ContextFlags = CONTEXT_FULL; 145 // resume. We do this to avoid artificially altering the dynamics of the
175 if (!::GetThreadContext(thread_handle, &thread_context)) { 146 // executing application any more than we already are by suspending and
176 LOG(ERROR) << "GetThreadContext failed: " << GetLastError(); 147 // resuming the thread.
177 } 148 ScopedDisablePriorityBoost disable_priority_boost(thread_handle);
178 149 bool resume_thread_succeeded = ::ResumeThread(thread_handle) != -1;
179 int stack_depth = RecordStack(&thread_context, max_stack_size, 150 CHECK(resume_thread_succeeded) << "ResumeThread failed: " << GetLastError();
180 instruction_pointers,
181 last_frame_is_unknown_function);
182
183 {
184 ScopedDisablePriorityBoost disable_priority_boost(thread_handle);
185 if (::ResumeThread(thread_handle) == -1)
186 LOG(ERROR) << "ResumeThread failed: " << GetLastError();
187 }
188 151
189 return stack_depth; 152 return stack_depth;
190 } 153 }
191 154
192 } // namespace 155 class NativeStackSamplerWin : public StackSamplingProfiler::NativeStackSampler {
156 public:
157 explicit NativeStackSamplerWin(win::ScopedHandle thread_handle);
158 ~NativeStackSamplerWin() override;
193 159
194 scoped_ptr<StackSamplingProfiler::NativeStackSampler> 160 // StackSamplingProfiler::NativeStackSampler:
195 StackSamplingProfiler::NativeStackSampler::Create(PlatformThreadId thread_id) { 161 void ProfileRecordingStarting(
196 #if _WIN64 162 StackSamplingProfiler::Profile* profile) override;
197 // Get the thread's handle. 163 void RecordStackSample(StackSamplingProfiler::Sample* sample) override;
198 HANDLE thread_handle = ::OpenThread( 164 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 165
204 return scoped_ptr<NativeStackSampler>(new NativeStackSamplerWin( 166 private:
205 win::ScopedHandle(thread_handle))); 167 // Attempts to query the module filename, base address, and id and store them
206 #else 168 // in |module_info|. Returns true if it succeeded.
207 return scoped_ptr<NativeStackSampler>(); 169 static bool GetModuleInfo(HMODULE module,
208 #endif 170 StackSamplingProfiler::Module* module_info);
209 } 171
172 // Gets the module index for |module| in |modules|, adding it if it's not
173 // already present. Returns StackSamplingProfiler::Frame::kUnknownModuleIndex
174 // if no Module can be determined for |module|
Peter Kasting 2015/03/30 23:07:35 Nit: trailing period
Mike Wittman 2015/03/31 01:06:37 Done.
175 size_t GetModuleIndex(HMODULE module,
176 std::vector<StackSamplingProfiler::Module>* modules);
177
178 // Copies the stack information represented by |instruction_pointers| into
179 // |sample| and |module_infos|.
180 void CopyToSample(const void* const instruction_pointers[],
181 const HMODULE modules[],
182 int stack_depth,
183 StackSamplingProfiler::Sample* sample,
184 std::vector<StackSamplingProfiler::Module>* module_infos);
185
186 win::ScopedHandle thread_handle_;
187 // Weak. Points to the profile being recorded between
188 // ProfileRecordingStarting() and ProfileRecordingStopped().
189 StackSamplingProfiler::Profile* current_profile_;
190 // Maps a module to the module's index within current_profile_->modules.
191 std::map<HMODULE, size_t> profile_module_index_;
192
193 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerWin);
194 };
210 195
211 NativeStackSamplerWin::NativeStackSamplerWin(win::ScopedHandle thread_handle) 196 NativeStackSamplerWin::NativeStackSamplerWin(win::ScopedHandle thread_handle)
212 : thread_handle_(thread_handle.Take()) { 197 : 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 } 198 }
226 199
227 NativeStackSamplerWin::~NativeStackSamplerWin() { 200 NativeStackSamplerWin::~NativeStackSamplerWin() {
228 } 201 }
229 202
230 void NativeStackSamplerWin::ProfileRecordingStarting( 203 void NativeStackSamplerWin::ProfileRecordingStarting(
231 StackSamplingProfiler::Profile* profile) { 204 StackSamplingProfiler::Profile* profile) {
232 current_profile_ = profile; 205 current_profile_ = profile;
233 profile_module_index_.clear(); 206 profile_module_index_.clear();
234 } 207 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 if (result_length == 0) 239 if (result_length == 0)
267 return false; 240 return false;
268 241
269 module_info->filename = base::FilePath(module_name); 242 module_info->filename = base::FilePath(module_name);
270 243
271 module_info->base_address = reinterpret_cast<const void*>(module); 244 module_info->base_address = reinterpret_cast<const void*>(module);
272 245
273 GUID guid; 246 GUID guid;
274 DWORD age; 247 DWORD age;
275 win::PEImage(module).GetDebugId(&guid, &age); 248 win::PEImage(module).GetDebugId(&guid, &age);
276 module_info->id.insert(module_info->id.end(), 249 module_info->id.assign(reinterpret_cast<char*>(&guid), sizeof(guid));
277 reinterpret_cast<char*>(&guid), 250 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 251
283 return true; 252 return true;
284 } 253 }
285 254
255 size_t NativeStackSamplerWin::GetModuleIndex(
256 HMODULE module,
257 std::vector<StackSamplingProfiler::Module>* modules) {
258 if (!module)
259 return StackSamplingProfiler::Frame::kUnknownModuleIndex;
260
261 auto loc = profile_module_index_.find(module);
262 if (loc == profile_module_index_.end()) {
263 StackSamplingProfiler::Module module_info;
264 if (!GetModuleInfo(module, &module_info))
265 return StackSamplingProfiler::Frame::kUnknownModuleIndex;
266 modules->push_back(module_info);
267 loc = profile_module_index_.insert(std::make_pair(
268 module, modules->size() - 1)).first;
269 }
270
271 return loc->second;
272 }
273
286 void NativeStackSamplerWin::CopyToSample( 274 void NativeStackSamplerWin::CopyToSample(
287 const void* const instruction_pointers[], 275 const void* const instruction_pointers[],
288 const HMODULE modules[], 276 const HMODULE modules[],
289 int stack_depth, 277 int stack_depth,
290 StackSamplingProfiler::Sample* sample, 278 StackSamplingProfiler::Sample* sample,
291 std::vector<StackSamplingProfiler::Module>* module_infos) { 279 std::vector<StackSamplingProfiler::Module>* module_infos) {
292 sample->clear(); 280 sample->clear();
293 sample->reserve(stack_depth); 281 sample->reserve(stack_depth);
294 282
295 for (int i = 0; i < stack_depth; ++i) { 283 for (int i = 0; i < stack_depth; ++i) {
296 sample->push_back(StackSamplingProfiler::Frame()); 284 sample->push_back(StackSamplingProfiler::Frame(
297 StackSamplingProfiler::Frame& frame = sample->back(); 285 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 } 286 }
323 } 287 }
324 288
289 } // namespace
290
291 scoped_ptr<StackSamplingProfiler::NativeStackSampler>
292 StackSamplingProfiler::NativeStackSampler::Create(PlatformThreadId thread_id) {
293 #if _WIN64
294 // Get the thread's handle.
295 HANDLE thread_handle = ::OpenThread(
296 THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION,
297 FALSE,
298 thread_id);
299
300 if (thread_handle) {
301 return scoped_ptr<NativeStackSampler>(new NativeStackSamplerWin(
302 win::ScopedHandle(thread_handle)));
303 }
304 #endif
305 return scoped_ptr<NativeStackSampler>();
306 }
307
325 } // namespace base 308 } // namespace base
309
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698