OLD | NEW |
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 |module_handles| corresponding to the pointers to code in |
97 // modules are returned with reference counts incremented should be freed with | 60 // |addresses|. The module handles are returned with reference counts |
98 // FreeModules. | 61 // incremented and should be freed with FreeModuleHandles. See note in |
99 void FindModulesForAddresses(const void* const addresses[], HMODULE modules[], | 62 // SuspendThreadAndRecordStack for why |addresses| and |module_handles| are |
100 int stack_depth, | 63 // arrays. |
| 64 void FindModuleHandlesForAddresses(const void* const addresses[], |
| 65 HMODULE module_handles[], int stack_depth, |
101 bool last_frame_is_unknown_function) { | 66 bool last_frame_is_unknown_function) { |
102 const int module_frames = last_frame_is_unknown_function ? stack_depth - 1 : | 67 const int module_frames = |
103 stack_depth; | 68 last_frame_is_unknown_function ? stack_depth - 1 : stack_depth; |
104 for (int i = 0; i < module_frames; ++i) { | 69 for (int i = 0; i < module_frames; ++i) { |
105 HMODULE module = NULL; | 70 HMODULE module_handle = NULL; |
106 if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, | 71 if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, |
107 reinterpret_cast<LPCTSTR>(addresses[i]), | 72 reinterpret_cast<LPCTSTR>(addresses[i]), |
108 &module)) { | 73 &module_handle)) { |
109 // HMODULE is the base address of the module. | 74 // HMODULE actually represents the base address of the module, so we can |
110 DCHECK_LT(reinterpret_cast<const void*>(module), addresses[i]); | 75 // use it directly as an address. |
111 modules[i] = module; | 76 DCHECK_LE(reinterpret_cast<const void*>(module_handle), addresses[i]); |
| 77 module_handles[i] = module_handle; |
112 } | 78 } |
113 } | 79 } |
114 } | 80 } |
115 | 81 |
116 // Free the modules returned by FindModulesForAddresses. | 82 // Frees the modules handles returned by FindModuleHandlesForAddresses. See note |
117 void FreeModules(int stack_depth, HMODULE modules[]) { | 83 // in SuspendThreadAndRecordStack for why |module_handles| is an array. |
| 84 void FreeModuleHandles(int stack_depth, HMODULE module_handles[]) { |
118 for (int i = 0; i < stack_depth; ++i) { | 85 for (int i = 0; i < stack_depth; ++i) { |
119 if (modules[i]) | 86 if (module_handles[i]) |
120 ::FreeLibrary(modules[i]); | 87 ::FreeLibrary(module_handles[i]); |
121 } | 88 } |
122 } | 89 } |
123 | 90 |
124 // Disables priority boost on a thread for the lifetime of the object. | 91 // Disables priority boost on a thread for the lifetime of the object. |
125 class ScopedDisablePriorityBoost { | 92 class ScopedDisablePriorityBoost { |
126 public: | 93 public: |
127 ScopedDisablePriorityBoost(HANDLE thread_handle); | 94 ScopedDisablePriorityBoost(HANDLE thread_handle); |
128 ~ScopedDisablePriorityBoost(); | 95 ~ScopedDisablePriorityBoost(); |
129 | 96 |
130 private: | 97 private: |
131 HANDLE thread_handle_; | 98 HANDLE thread_handle_; |
132 BOOL got_previous_boost_state_; | 99 BOOL got_previous_boost_state_; |
133 BOOL boost_state_was_disabled_; | 100 BOOL boost_state_was_disabled_; |
134 | 101 |
135 DISALLOW_COPY_AND_ASSIGN(ScopedDisablePriorityBoost); | 102 DISALLOW_COPY_AND_ASSIGN(ScopedDisablePriorityBoost); |
136 }; | 103 }; |
137 | 104 |
138 ScopedDisablePriorityBoost::ScopedDisablePriorityBoost(HANDLE thread_handle) | 105 ScopedDisablePriorityBoost::ScopedDisablePriorityBoost(HANDLE thread_handle) |
139 : thread_handle_(thread_handle), | 106 : thread_handle_(thread_handle), |
140 got_previous_boost_state_(false), | 107 got_previous_boost_state_(false), |
141 boost_state_was_disabled_(false) { | 108 boost_state_was_disabled_(false) { |
142 got_previous_boost_state_ = | 109 got_previous_boost_state_ = |
143 ::GetThreadPriorityBoost(thread_handle_, &boost_state_was_disabled_); | 110 ::GetThreadPriorityBoost(thread_handle_, &boost_state_was_disabled_); |
144 if (got_previous_boost_state_ && !boost_state_was_disabled_) { | 111 if (got_previous_boost_state_) { |
145 // Confusingly, TRUE disables priority boost ... | 112 // Confusingly, TRUE disables priority boost. |
146 ::SetThreadPriorityBoost(thread_handle_, TRUE); | 113 ::SetThreadPriorityBoost(thread_handle_, TRUE); |
147 } | 114 } |
148 } | 115 } |
149 | 116 |
150 ScopedDisablePriorityBoost::~ScopedDisablePriorityBoost() { | 117 ScopedDisablePriorityBoost::~ScopedDisablePriorityBoost() { |
151 if (got_previous_boost_state_ && !boost_state_was_disabled_) { | 118 if (got_previous_boost_state_) |
152 // ... and FALSE enables priority boost. | 119 ::SetThreadPriorityBoost(thread_handle_, boost_state_was_disabled_); |
153 ::SetThreadPriorityBoost(thread_handle_, FALSE); | |
154 } | |
155 } | 120 } |
156 | 121 |
157 // Suspends the thread with |thread_handle|, records the stack into | 122 // Suspends the thread with |thread_handle|, records the stack into |
158 // |instruction_pointers|, then resumes the thread. Returns the size of the | 123 // |instruction_pointers|, then resumes the thread. Returns the size of the |
159 // stack. | 124 // stack. |
| 125 // |
| 126 // IMPORTANT NOTE: No heap allocations may occur between SuspendThread and |
| 127 // ResumeThread. Otherwise this code can deadlock on heap locks acquired by the |
| 128 // target thread before it was suspended. This is why we pass instruction |
| 129 // pointers and module handles as preallocated arrays rather than vectors, since |
| 130 // vectors make it too easy to subtly allocate memory. |
160 int SuspendThreadAndRecordStack(HANDLE thread_handle, int max_stack_size, | 131 int SuspendThreadAndRecordStack(HANDLE thread_handle, int max_stack_size, |
161 const void* instruction_pointers[], | 132 const void* instruction_pointers[], |
162 bool* last_frame_is_unknown_function) { | 133 bool* last_frame_is_unknown_function) { |
163 #if defined(_WIN64) | 134 if (::SuspendThread(thread_handle) == -1) |
164 if (RtlVirtualUnwind == nullptr || RtlLookupFunctionEntry == nullptr) | |
165 return 0; | 135 return 0; |
166 #endif | 136 |
167 | 137 int stack_depth = 0; |
168 if (::SuspendThread(thread_handle) == -1) { | |
169 LOG(ERROR) << "SuspendThread failed: " << GetLastError(); | |
170 return 0; | |
171 } | |
172 | |
173 CONTEXT thread_context = {0}; | 138 CONTEXT thread_context = {0}; |
174 thread_context.ContextFlags = CONTEXT_FULL; | 139 thread_context.ContextFlags = CONTEXT_FULL; |
175 if (!::GetThreadContext(thread_handle, &thread_context)) { | 140 if (::GetThreadContext(thread_handle, &thread_context)) { |
176 LOG(ERROR) << "GetThreadContext failed: " << GetLastError(); | 141 stack_depth = RecordStack(&thread_context, max_stack_size, |
177 } | 142 instruction_pointers, |
178 | 143 last_frame_is_unknown_function); |
179 int stack_depth = RecordStack(&thread_context, max_stack_size, | 144 } |
180 instruction_pointers, | 145 |
181 last_frame_is_unknown_function); | 146 // Disable the priority boost that the thread would otherwise receive on |
182 | 147 // resume. We do this to avoid artificially altering the dynamics of the |
183 { | 148 // executing application any more than we already are by suspending and |
184 ScopedDisablePriorityBoost disable_priority_boost(thread_handle); | 149 // resuming the thread. |
185 if (::ResumeThread(thread_handle) == -1) | 150 ScopedDisablePriorityBoost disable_priority_boost(thread_handle); |
186 LOG(ERROR) << "ResumeThread failed: " << GetLastError(); | 151 bool resume_thread_succeeded = ::ResumeThread(thread_handle) != -1; |
187 } | 152 CHECK(resume_thread_succeeded) << "ResumeThread failed: " << GetLastError(); |
188 | 153 |
189 return stack_depth; | 154 return stack_depth; |
190 } | 155 } |
191 | 156 |
| 157 class NativeStackSamplerWin : public NativeStackSampler { |
| 158 public: |
| 159 explicit NativeStackSamplerWin(win::ScopedHandle thread_handle); |
| 160 ~NativeStackSamplerWin() override; |
| 161 |
| 162 // StackSamplingProfiler::NativeStackSampler: |
| 163 void ProfileRecordingStarting( |
| 164 std::vector<StackSamplingProfiler::Module>* modules) override; |
| 165 void RecordStackSample(StackSamplingProfiler::Sample* sample) override; |
| 166 void ProfileRecordingStopped() override; |
| 167 |
| 168 private: |
| 169 // Attempts to query the module filename, base address, and id for |
| 170 // |module_handle|, and store them in |module|. Returns true if it succeeded. |
| 171 static bool GetModuleForHandle(HMODULE module_handle, |
| 172 StackSamplingProfiler::Module* module); |
| 173 |
| 174 // Gets the index for the Module corresponding to |module_handle| in |
| 175 // |modules|, adding it if it's not already present. Returns |
| 176 // StackSamplingProfiler::Frame::kUnknownModuleIndex if no Module can be |
| 177 // determined for |module|. |
| 178 size_t GetModuleIndex(HMODULE module_handle, |
| 179 std::vector<StackSamplingProfiler::Module>* modules); |
| 180 |
| 181 // Copies the stack information represented by |instruction_pointers| into |
| 182 // |sample| and |modules|. |
| 183 void CopyToSample(const void* const instruction_pointers[], |
| 184 const HMODULE module_handles[], |
| 185 int stack_depth, |
| 186 StackSamplingProfiler::Sample* sample, |
| 187 std::vector<StackSamplingProfiler::Module>* modules); |
| 188 |
| 189 win::ScopedHandle thread_handle_; |
| 190 // Weak. Points to the modules associated with the profile being recorded |
| 191 // between ProfileRecordingStarting() and ProfileRecordingStopped(). |
| 192 std::vector<StackSamplingProfiler::Module>* current_modules_; |
| 193 // Maps a module handle to the corresponding Module's index within |
| 194 // current_modules_. |
| 195 std::map<HMODULE, size_t> profile_module_index_; |
| 196 |
| 197 DISALLOW_COPY_AND_ASSIGN(NativeStackSamplerWin); |
| 198 }; |
| 199 |
| 200 NativeStackSamplerWin::NativeStackSamplerWin(win::ScopedHandle thread_handle) |
| 201 : thread_handle_(thread_handle.Take()) { |
| 202 } |
| 203 |
| 204 NativeStackSamplerWin::~NativeStackSamplerWin() { |
| 205 } |
| 206 |
| 207 void NativeStackSamplerWin::ProfileRecordingStarting( |
| 208 std::vector<StackSamplingProfiler::Module>* modules) { |
| 209 current_modules_ = modules; |
| 210 profile_module_index_.clear(); |
| 211 } |
| 212 |
| 213 void NativeStackSamplerWin::RecordStackSample( |
| 214 StackSamplingProfiler::Sample* sample) { |
| 215 DCHECK(current_modules_); |
| 216 |
| 217 const int max_stack_size = 64; |
| 218 const void* instruction_pointers[max_stack_size] = {0}; |
| 219 HMODULE module_handles[max_stack_size] = {0}; |
| 220 |
| 221 bool last_frame_is_unknown_function = false; |
| 222 int stack_depth = SuspendThreadAndRecordStack( |
| 223 thread_handle_.Get(), max_stack_size, instruction_pointers, |
| 224 &last_frame_is_unknown_function); |
| 225 FindModuleHandlesForAddresses(instruction_pointers, module_handles, |
| 226 stack_depth, last_frame_is_unknown_function); |
| 227 CopyToSample(instruction_pointers, module_handles, stack_depth, sample, |
| 228 current_modules_); |
| 229 FreeModuleHandles(stack_depth, module_handles); |
| 230 } |
| 231 |
| 232 void NativeStackSamplerWin::ProfileRecordingStopped() { |
| 233 current_modules_ = nullptr; |
| 234 } |
| 235 |
| 236 // static |
| 237 bool NativeStackSamplerWin::GetModuleForHandle( |
| 238 HMODULE module_handle, |
| 239 StackSamplingProfiler::Module* module) { |
| 240 wchar_t module_name[MAX_PATH]; |
| 241 DWORD result_length = |
| 242 GetModuleFileName(module_handle, module_name, arraysize(module_name)); |
| 243 if (result_length == 0) |
| 244 return false; |
| 245 |
| 246 module->filename = base::FilePath(module_name); |
| 247 |
| 248 module->base_address = reinterpret_cast<const void*>(module_handle); |
| 249 |
| 250 GUID guid; |
| 251 DWORD age; |
| 252 win::PEImage(module_handle).GetDebugId(&guid, &age); |
| 253 module->id.assign(reinterpret_cast<char*>(&guid), sizeof(guid)); |
| 254 module->id.append(reinterpret_cast<char*>(&age), sizeof(age)); |
| 255 |
| 256 return true; |
| 257 } |
| 258 |
| 259 size_t NativeStackSamplerWin::GetModuleIndex( |
| 260 HMODULE module_handle, |
| 261 std::vector<StackSamplingProfiler::Module>* modules) { |
| 262 if (!module_handle) |
| 263 return StackSamplingProfiler::Frame::kUnknownModuleIndex; |
| 264 |
| 265 auto loc = profile_module_index_.find(module_handle); |
| 266 if (loc == profile_module_index_.end()) { |
| 267 StackSamplingProfiler::Module module; |
| 268 if (!GetModuleForHandle(module_handle, &module)) |
| 269 return StackSamplingProfiler::Frame::kUnknownModuleIndex; |
| 270 modules->push_back(module); |
| 271 loc = profile_module_index_.insert(std::make_pair( |
| 272 module_handle, modules->size() - 1)).first; |
| 273 } |
| 274 |
| 275 return loc->second; |
| 276 } |
| 277 |
| 278 void NativeStackSamplerWin::CopyToSample( |
| 279 const void* const instruction_pointers[], |
| 280 const HMODULE module_handles[], |
| 281 int stack_depth, |
| 282 StackSamplingProfiler::Sample* sample, |
| 283 std::vector<StackSamplingProfiler::Module>* module) { |
| 284 sample->clear(); |
| 285 sample->reserve(stack_depth); |
| 286 |
| 287 for (int i = 0; i < stack_depth; ++i) { |
| 288 sample->push_back(StackSamplingProfiler::Frame( |
| 289 instruction_pointers[i], |
| 290 GetModuleIndex(module_handles[i], module))); |
| 291 } |
| 292 } |
| 293 |
192 } // namespace | 294 } // namespace |
193 | 295 |
194 scoped_ptr<StackSamplingProfiler::NativeStackSampler> | 296 scoped_ptr<NativeStackSampler> NativeStackSampler::Create( |
195 StackSamplingProfiler::NativeStackSampler::Create(PlatformThreadId thread_id) { | 297 PlatformThreadId thread_id) { |
196 #if _WIN64 | 298 #if _WIN64 |
197 // Get the thread's handle. | 299 // Get the thread's handle. |
198 HANDLE thread_handle = ::OpenThread( | 300 HANDLE thread_handle = ::OpenThread( |
199 THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION, | 301 THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION, |
200 FALSE, | 302 FALSE, |
201 thread_id); | 303 thread_id); |
202 DCHECK(thread_handle) << "OpenThread failed"; | 304 |
203 | 305 if (thread_handle) { |
204 return scoped_ptr<NativeStackSampler>(new NativeStackSamplerWin( | 306 return scoped_ptr<NativeStackSampler>(new NativeStackSamplerWin( |
205 win::ScopedHandle(thread_handle))); | 307 win::ScopedHandle(thread_handle))); |
206 #else | 308 } |
| 309 #endif |
207 return scoped_ptr<NativeStackSampler>(); | 310 return scoped_ptr<NativeStackSampler>(); |
208 #endif | |
209 } | |
210 | |
211 NativeStackSamplerWin::NativeStackSamplerWin(win::ScopedHandle thread_handle) | |
212 : 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 } | |
226 | |
227 NativeStackSamplerWin::~NativeStackSamplerWin() { | |
228 } | |
229 | |
230 void NativeStackSamplerWin::ProfileRecordingStarting( | |
231 StackSamplingProfiler::Profile* profile) { | |
232 current_profile_ = profile; | |
233 profile_module_index_.clear(); | |
234 } | |
235 | |
236 void NativeStackSamplerWin::RecordStackSample( | |
237 StackSamplingProfiler::Sample* sample) { | |
238 DCHECK(current_profile_); | |
239 | |
240 const int max_stack_size = 64; | |
241 const void* instruction_pointers[max_stack_size] = {0}; | |
242 HMODULE modules[max_stack_size] = {0}; | |
243 | |
244 bool last_frame_is_unknown_function = false; | |
245 int stack_depth = SuspendThreadAndRecordStack( | |
246 thread_handle_.Get(), max_stack_size, instruction_pointers, | |
247 &last_frame_is_unknown_function); | |
248 FindModulesForAddresses(instruction_pointers, modules, stack_depth, | |
249 last_frame_is_unknown_function); | |
250 CopyToSample(instruction_pointers, modules, stack_depth, sample, | |
251 ¤t_profile_->modules); | |
252 FreeModules(stack_depth, modules); | |
253 } | |
254 | |
255 void NativeStackSamplerWin::ProfileRecordingStopped() { | |
256 current_profile_ = nullptr; | |
257 } | |
258 | |
259 // static | |
260 bool NativeStackSamplerWin::GetModuleInfo( | |
261 HMODULE module, | |
262 StackSamplingProfiler::Module* module_info) { | |
263 wchar_t module_name[MAX_PATH]; | |
264 DWORD result_length = | |
265 GetModuleFileName(module, module_name, arraysize(module_name)); | |
266 if (result_length == 0) | |
267 return false; | |
268 | |
269 module_info->filename = base::FilePath(module_name); | |
270 | |
271 module_info->base_address = reinterpret_cast<const void*>(module); | |
272 | |
273 GUID guid; | |
274 DWORD age; | |
275 win::PEImage(module).GetDebugId(&guid, &age); | |
276 module_info->id.insert(module_info->id.end(), | |
277 reinterpret_cast<char*>(&guid), | |
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 | |
283 return true; | |
284 } | |
285 | |
286 void NativeStackSamplerWin::CopyToSample( | |
287 const void* const instruction_pointers[], | |
288 const HMODULE modules[], | |
289 int stack_depth, | |
290 StackSamplingProfiler::Sample* sample, | |
291 std::vector<StackSamplingProfiler::Module>* module_infos) { | |
292 sample->clear(); | |
293 sample->reserve(stack_depth); | |
294 | |
295 for (int i = 0; i < stack_depth; ++i) { | |
296 sample->push_back(StackSamplingProfiler::Frame()); | |
297 StackSamplingProfiler::Frame& frame = sample->back(); | |
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 } | |
323 } | 311 } |
324 | 312 |
325 } // namespace base | 313 } // namespace base |
OLD | NEW |