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 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|. | |
54 int RecordStack(CONTEXT* context, | |
55 int max_stack_size, | |
56 const void* instruction_pointers[], | |
57 bool* last_frame_is_unknown_function) { | |
58 #ifdef _WIN64 | |
59 *last_frame_is_unknown_function = false; | |
60 | |
61 IMAGEHLP_SYMBOL64 sym; | |
62 sym.SizeOfStruct = sizeof(sym); | |
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. | |
67 ULONG64 image_base; | |
68 PRUNTIME_FUNCTION runtime_function = | |
69 RtlLookupFunctionEntry(context->Rip, &image_base, nullptr); | |
70 | |
71 instruction_pointers[i] = reinterpret_cast<void*>(context->Rip); | |
72 | |
73 if (runtime_function) { | |
74 KNONVOLATILE_CONTEXT_POINTERS nvcontext = {0}; | |
75 void* handler_data; | |
76 ULONG64 establisher_frame; | |
77 RtlVirtualUnwind(0, image_base, context->Rip, runtime_function, context, | |
78 &handler_data, &establisher_frame, &nvcontext); | |
79 } else { | |
80 // If we don't have a RUNTIME_FUNCTION, then we've encountered | |
81 // a leaf function. Adjust the stack appropriately. | |
82 context->Rip = *reinterpret_cast<PDWORD64>(context->Rsp); | |
83 context->Rsp += 8; | |
84 *last_frame_is_unknown_function = true; | |
85 } | |
86 | |
87 if (!context->Rip) | |
88 return i; | |
89 } | |
90 return max_stack_size; | |
91 #else | |
92 return 0; | |
93 #endif | |
94 } | |
95 | |
96 // Fills in |modules| corresponding to the pointers to code in |addresses|. The | |
97 // modules are returned with reference counts incremented should be freed with | |
98 // FreeModules. | |
99 void FindModulesForAddresses(const void* const addresses[], HMODULE modules[], | |
100 int stack_depth, | |
101 bool last_frame_is_unknown_function) { | |
102 const int module_frames = last_frame_is_unknown_function ? stack_depth - 1 : | |
103 stack_depth; | |
104 for (int i = 0; i < module_frames; ++i) { | |
105 HMODULE module = NULL; | |
106 if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, | |
107 reinterpret_cast<LPCTSTR>(addresses[i]), | |
108 &module)) { | |
109 // HMODULE is the base address of the module. | |
110 DCHECK_LT(reinterpret_cast<const void*>(module), addresses[i]); | |
111 modules[i] = module; | |
112 } | |
113 } | |
114 } | |
115 | |
116 // Free the modules returned by FindModulesForAddresses. | |
117 void FreeModules(int stack_depth, HMODULE modules[]) { | |
118 for (int i = 0; i < stack_depth; ++i) { | |
119 if (modules[i]) | |
120 ::FreeLibrary(modules[i]); | |
121 } | |
122 } | |
123 | |
124 // Suspends the thread with |thread_handle|, records the stack into | |
125 // |instruction_pointers|, then resumes the thread. Returns the size of the | |
126 // stack. | |
127 int SuspendThreadAndRecordStack(HANDLE thread_handle, int max_stack_size, | |
128 const void* instruction_pointers[], | |
129 bool* last_frame_is_unknown_function) { | |
130 if (RtlVirtualUnwind == nullptr || RtlLookupFunctionEntry == nullptr) | |
131 return 0; | |
132 | |
133 if (::SuspendThread(thread_handle) == -1) { | |
134 LOG(ERROR) << "SuspendThread failed: " << GetLastError(); | |
135 return 0; | |
136 } | |
137 | |
138 CONTEXT thread_context = {0}; | |
139 thread_context.ContextFlags = CONTEXT_FULL; | |
140 if (!::GetThreadContext(thread_handle, &thread_context)) { | |
141 LOG(ERROR) << "GetThreadContext failed: " << GetLastError(); | |
142 } | |
143 | |
144 int stack_depth = RecordStack(&thread_context, max_stack_size, | |
145 instruction_pointers, | |
146 last_frame_is_unknown_function); | |
147 | |
148 if (::ResumeThread(thread_handle) == -1) | |
zturner
2015/03/19 02:54:12
One minor comment here. This will cause the threa
Mike Wittman
2015/03/19 20:27:17
Thanks for the heads up. I don't think we want pri
| |
149 LOG(ERROR) << "ResumeThread failed: " << GetLastError(); | |
150 | |
151 return stack_depth; | |
152 } | |
153 | |
154 } // namespace | |
155 | |
156 scoped_ptr<StackSamplingProfiler::NativeStackSampler> | |
157 StackSamplingProfiler::NativeStackSampler::Create(PlatformThreadId thread_id) { | |
158 #if _WIN64 | |
159 // Get the thread's handle. | |
160 HANDLE thread_handle = ::OpenThread( | |
161 THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION, | |
162 FALSE, | |
163 thread_id); | |
164 DCHECK(thread_handle) << "OpenThread failed"; | |
165 | |
166 return scoped_ptr<NativeStackSampler>(new NativeStackSamplerWin( | |
167 win::ScopedHandle(thread_handle))); | |
168 #else | |
169 return scoped_ptr<NativeStackSampler>(); | |
170 #endif | |
171 } | |
172 | |
173 NativeStackSamplerWin::NativeStackSamplerWin(win::ScopedHandle thread_handle) | |
174 : thread_handle_(thread_handle.Take()) { | |
175 #ifdef _WIN64 | |
176 if (RtlVirtualUnwind == nullptr && RtlLookupFunctionEntry == nullptr) { | |
177 const HMODULE nt_dll_handle = ::GetModuleHandle(L"ntdll.dll"); | |
178 // This should always be non-null, but handle just in case. | |
179 if (nt_dll_handle) { | |
180 reinterpret_cast<void*&>(RtlVirtualUnwind) = | |
181 ::GetProcAddress(nt_dll_handle, "RtlVirtualUnwind"); | |
182 reinterpret_cast<void*&>(RtlLookupFunctionEntry) = | |
183 ::GetProcAddress(nt_dll_handle, "RtlLookupFunctionEntry"); | |
184 } | |
185 } | |
186 #endif | |
187 } | |
188 | |
189 NativeStackSamplerWin::~NativeStackSamplerWin() { | |
190 } | |
191 | |
192 void NativeStackSamplerWin::ProfileRecordingStarting( | |
193 StackSamplingProfiler::Profile* profile) { | |
194 current_profile_ = profile; | |
195 profile_module_index_.clear(); | |
196 } | |
197 | |
198 void NativeStackSamplerWin::RecordStackSample( | |
199 StackSamplingProfiler::Sample* sample) { | |
200 DCHECK(current_profile_); | |
201 | |
202 const int max_stack_size = 64; | |
203 const void* instruction_pointers[max_stack_size] = {0}; | |
204 HMODULE modules[max_stack_size] = {0}; | |
205 | |
206 bool last_frame_is_unknown_function = false; | |
207 int stack_depth = SuspendThreadAndRecordStack( | |
208 thread_handle_.Get(), max_stack_size, instruction_pointers, | |
209 &last_frame_is_unknown_function); | |
210 FindModulesForAddresses(instruction_pointers, modules, stack_depth, | |
211 last_frame_is_unknown_function); | |
212 CopyToSample(instruction_pointers, modules, stack_depth, sample, | |
213 ¤t_profile_->modules); | |
214 FreeModules(stack_depth, modules); | |
215 } | |
216 | |
217 void NativeStackSamplerWin::ProfileRecordingStopped() { | |
218 current_profile_ = nullptr; | |
219 } | |
220 | |
221 // static | |
222 bool NativeStackSamplerWin::GetModuleInfo( | |
223 HMODULE module, | |
224 StackSamplingProfiler::Module* module_info) { | |
225 wchar_t module_name[MAX_PATH]; | |
226 DWORD result_length = | |
227 GetModuleFileName(module, module_name, arraysize(module_name)); | |
228 if (result_length == 0) | |
229 return false; | |
230 | |
231 module_info->filename = base::FilePath(module_name); | |
232 | |
233 module_info->base_address = reinterpret_cast<const void*>(module); | |
234 | |
235 GUID guid; | |
236 DWORD age; | |
237 win::PEImage(module).GetDebugId(&guid, &age); | |
238 module_info->id.insert(module_info->id.end(), | |
239 reinterpret_cast<char*>(&guid), | |
240 reinterpret_cast<char*>(&guid + 1)); | |
241 module_info->id.insert(module_info->id.end(), | |
242 reinterpret_cast<char*>(&age), | |
243 reinterpret_cast<char*>(&age + 1)); | |
244 | |
245 return true; | |
246 } | |
247 | |
248 void NativeStackSamplerWin::CopyToSample( | |
249 const void* const instruction_pointers[], | |
250 const HMODULE modules[], | |
251 int stack_depth, | |
252 StackSamplingProfiler::Sample* sample, | |
253 std::vector<StackSamplingProfiler::Module>* module_infos) { | |
254 sample->clear(); | |
255 sample->reserve(stack_depth); | |
256 | |
257 for (int i = 0; i < stack_depth; ++i) { | |
258 sample->push_back(StackSamplingProfiler::Frame()); | |
259 StackSamplingProfiler::Frame& frame = sample->back(); | |
260 | |
261 frame.instruction_pointer = instruction_pointers[i]; | |
262 | |
263 // Record an invalid module index if we don't have a valid module. | |
264 if (!modules[i]) { | |
265 frame.module_index = -1; | |
266 continue; | |
267 } | |
268 | |
269 auto loc = profile_module_index_.find(modules[i]); | |
270 if (loc == profile_module_index_.end()) { | |
271 StackSamplingProfiler::Module module_info; | |
272 // Record an invalid module index if we have a module but can't find | |
273 // information on it. | |
274 if (!GetModuleInfo(modules[i], &module_info)) { | |
275 frame.module_index = -1; | |
276 continue; | |
277 } | |
278 module_infos->push_back(module_info); | |
279 loc = profile_module_index_.insert(std::make_pair( | |
280 modules[i], static_cast<int>(module_infos->size() - 1))).first; | |
281 } | |
282 | |
283 frame.module_index = loc->second; | |
284 } | |
285 } | |
286 | |
287 } // namespace base | |
OLD | NEW |