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, | |
zturner
2015/03/18 23:04:37
Did you consider using RtlCaptureStackBacktrace?
danduong
2015/03/18 23:20:48
As far as I know, RtlCaptureStackBackTrace cannot
| |
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; | |
zturner
2015/03/18 23:04:37
Is there any desire to support x86? RtlVirtualUnw
danduong
2015/03/18 23:20:47
We'll focus on x64 for now. StackWalk64, I've hear
| |
93 #endif | |
94 } | |
95 | |
96 // Fills in |modules| corresponding to the pointers to code in |addresses|. | |
97 void FindModulesForAddresses(const void* const addresses[], HMODULE modules[], | |
98 int stack_depth, | |
99 bool last_frame_is_unknown_function) { | |
100 const int module_frames = last_frame_is_unknown_function ? stack_depth - 1 : | |
101 stack_depth; | |
102 for (int i = 0; i < module_frames; ++i) { | |
103 HMODULE module = NULL; | |
104 if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | | |
105 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, | |
zturner
2015/03/18 23:04:37
Why not increment the module's refcount? You stor
Mike Wittman
2015/03/19 00:15:44
Incrementing the refcount led to deadlock previous
| |
106 reinterpret_cast<LPCTSTR>(addresses[i]), | |
107 &module)) { | |
108 // HMODULE is the base address of the module. | |
109 DCHECK_LT(reinterpret_cast<const void*>(module), addresses[i]); | |
110 modules[i] = module; | |
111 } | |
112 } | |
113 for (int i = module_frames; i < stack_depth; ++i) | |
zturner
2015/03/18 23:04:37
Alternatively, you could memset() the entire array
Mike Wittman
2015/03/19 00:15:44
Used a zero initializer.
| |
114 modules[stack_depth - 1] = NULL; | |
zturner
2015/03/18 23:04:37
What about addresses? You're going to end up with
Mike Wittman
2015/03/19 00:15:44
Done.
| |
115 } | |
116 | |
117 // Suspends the thread with |thread_handle|, records the stack into | |
118 // |instruction_pointers| and the corresponding modules into |modules|, then | |
119 // resumes the thread. Returns the size of the stack. | |
120 int SuspendThreadAndRecordStack(HANDLE thread_handle, int max_stack_size, | |
121 const void* instruction_pointers[], | |
122 HMODULE modules[]) { | |
123 if (::SuspendThread(thread_handle) == -1) { | |
zturner
2015/03/18 23:04:37
Just to be certain, this will never be equal to th
Mike Wittman
2015/03/19 00:15:43
Yes, this is only ever called on the profiler thre
| |
124 LOG(ERROR) << "SuspendThread failed: " << GetLastError(); | |
125 return 0; | |
126 } | |
127 | |
128 CONTEXT thread_context = {0}; | |
129 thread_context.ContextFlags = CONTEXT_ALL; | |
zturner
2015/03/18 23:04:37
Do you actually need CONTEXT_ALL? CONTEXT_FULL is
Mike Wittman
2015/03/19 00:15:44
CONTEXT_FULL appears to be sufficient.
| |
130 if (!::GetThreadContext(thread_handle, &thread_context)) { | |
131 LOG(ERROR) << "GetThreadContext failed: " << GetLastError(); | |
132 } | |
133 | |
134 bool last_frame_is_unknown_function = false; | |
135 int stack_depth = RecordStack(&thread_context, max_stack_size, | |
136 instruction_pointers, | |
137 &last_frame_is_unknown_function); | |
138 | |
139 if (::ResumeThread(thread_handle) == -1) | |
140 LOG(ERROR) << "ResumeThread failed: " << GetLastError(); | |
141 | |
142 FindModulesForAddresses(instruction_pointers, modules, stack_depth, | |
143 last_frame_is_unknown_function); | |
144 | |
145 return stack_depth; | |
146 } | |
147 | |
148 } // namespace | |
149 | |
150 scoped_ptr<StackSamplingProfiler::NativeStackSampler> | |
151 StackSamplingProfiler::NativeStackSampler::Create(PlatformThreadId thread_id) { | |
152 #if _WIN64 | |
153 // Get the thread's handle. | |
154 HANDLE thread_handle = ::OpenThread(THREAD_ALL_ACCESS, FALSE, thread_id); | |
zturner
2015/03/18 23:04:37
THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREA
Mike Wittman
2015/03/19 00:15:44
Done.
| |
155 DCHECK(thread_handle) << "OpenThread failed"; | |
156 | |
157 return scoped_ptr<NativeStackSampler>(new NativeStackSamplerWin( | |
158 win::ScopedHandle(thread_handle))); | |
159 #else | |
160 return scoped_ptr<NativeStackSampler>(); | |
161 #endif | |
162 } | |
163 | |
164 NativeStackSamplerWin::NativeStackSamplerWin(win::ScopedHandle thread_handle) | |
165 : thread_handle_(thread_handle.Take()) { | |
166 #ifdef _WIN64 | |
167 if (RtlVirtualUnwind == nullptr && RtlLookupFunctionEntry == nullptr) { | |
168 const HMODULE nt_dll_handle = ::GetModuleHandle(L"ntdll.dll"); | |
zturner
2015/03/18 23:04:37
You should probably handle the case where nt_dll_h
Mike Wittman
2015/03/19 00:15:44
Done.
| |
169 reinterpret_cast<void*&>(RtlVirtualUnwind) = | |
170 ::GetProcAddress(nt_dll_handle, "RtlVirtualUnwind"); | |
171 reinterpret_cast<void*&>(RtlLookupFunctionEntry) = | |
172 ::GetProcAddress(nt_dll_handle, "RtlLookupFunctionEntry"); | |
173 } | |
174 #endif | |
175 } | |
176 | |
177 NativeStackSamplerWin::~NativeStackSamplerWin() { | |
178 } | |
179 | |
180 void NativeStackSamplerWin::ProfileRecordingStarting( | |
181 StackSamplingProfiler::Profile* profile) { | |
182 current_profile_ = profile; | |
183 profile_module_index_.clear(); | |
184 } | |
185 | |
186 void NativeStackSamplerWin::RecordStackSample( | |
187 StackSamplingProfiler::Sample* sample) { | |
188 DCHECK(current_profile_); | |
189 | |
190 const int max_stack_size = 64; | |
191 const void* instruction_pointers[max_stack_size]; | |
192 HMODULE modules[max_stack_size]; | |
zturner
2015/03/18 23:04:37
If you initialize these two arrays with "= {0}" th
Mike Wittman
2015/03/19 00:15:44
Done.
| |
193 | |
194 int stack_depth = SuspendThreadAndRecordStack( | |
195 thread_handle_.Get(), max_stack_size, instruction_pointers, modules); | |
196 CopyToSample(instruction_pointers, modules, stack_depth, sample, | |
197 ¤t_profile_->modules); | |
198 } | |
199 | |
200 void NativeStackSamplerWin::ProfileRecordingStopped() { | |
201 current_profile_ = nullptr; | |
202 } | |
203 | |
204 // static | |
205 bool NativeStackSamplerWin::GetModuleInfo( | |
206 HMODULE module, | |
207 StackSamplingProfiler::Module* module_info) { | |
208 wchar_t module_name[MAX_PATH]; | |
209 DWORD result_length = | |
210 GetModuleFileName(module, module_name, arraysize(module_name)); | |
211 if (result_length == 0) | |
212 return false; | |
213 | |
214 module_info->filename = base::FilePath(module_name); | |
215 | |
216 module_info->base_address = reinterpret_cast<const void*>(module); | |
217 | |
218 GUID guid; | |
219 DWORD age; | |
220 win::PEImage(module).GetDebugId(&guid, &age); | |
zturner
2015/03/18 23:04:37
Is this present for release / optimized builds?
danduong
2015/03/18 23:20:47
Should be. Minidumps include the debugIds of the m
| |
221 module_info->id.insert(module_info->id.end(), | |
222 reinterpret_cast<char*>(&guid), | |
223 reinterpret_cast<char*>(&guid + 1)); | |
224 module_info->id.insert(module_info->id.end(), | |
225 reinterpret_cast<char*>(&age), | |
226 reinterpret_cast<char*>(&age + 1)); | |
227 | |
228 return true; | |
229 } | |
230 | |
231 void NativeStackSamplerWin::CopyToSample( | |
232 const void* const instruction_pointers[], | |
233 const HMODULE modules[], | |
234 int stack_depth, | |
235 StackSamplingProfiler::Sample* sample, | |
236 std::vector<StackSamplingProfiler::Module>* module_infos) { | |
237 sample->clear(); | |
238 sample->reserve(stack_depth); | |
239 | |
240 for (int i = 0; i < stack_depth; ++i) { | |
241 sample->push_back(StackSamplingProfiler::Frame()); | |
242 StackSamplingProfiler::Frame& frame = sample->back(); | |
243 | |
244 frame.instruction_pointer = instruction_pointers[i]; | |
245 | |
246 // Record an invalid module index if we don't have a valid module. | |
247 if (!modules[i]) { | |
248 frame.module_index = -1; | |
249 continue; | |
250 } | |
251 | |
252 auto loc = profile_module_index_.find(modules[i]); | |
253 if (loc == profile_module_index_.end()) { | |
254 StackSamplingProfiler::Module module_info; | |
255 // Record an invalid module index if we have a module but can't find | |
256 // information on it. | |
257 if (!GetModuleInfo(modules[i], &module_info)) { | |
258 frame.module_index = -1; | |
259 continue; | |
260 } | |
261 module_infos->push_back(module_info); | |
262 loc = profile_module_index_.insert(std::make_pair( | |
263 modules[i], static_cast<int>(module_infos->size() - 1))).first; | |
264 } | |
265 | |
266 frame.module_index = loc->second; | |
267 } | |
268 } | |
269 | |
270 } // namespace base | |
OLD | NEW |