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

Side by Side Diff: snapshot/win/process_reader_win.cc

Issue 1133203002: win: Retrieve thread context for x64 (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: . Created 5 years, 7 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
« no previous file with comments | « snapshot/win/process_reader_win.h ('k') | snapshot/win/thread_snapshot_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Crashpad Authors. All rights reserved. 1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 static NtOpenThreadFunction nt_open_thread = 57 static NtOpenThreadFunction nt_open_thread =
58 reinterpret_cast<NtOpenThreadFunction>( 58 reinterpret_cast<NtOpenThreadFunction>(
59 GetProcAddress(LoadLibrary(L"ntdll.dll"), "NtOpenThread")); 59 GetProcAddress(LoadLibrary(L"ntdll.dll"), "NtOpenThread"));
60 DCHECK(nt_open_thread); 60 DCHECK(nt_open_thread);
61 return nt_open_thread(thread_handle, 61 return nt_open_thread(thread_handle,
62 desired_access, 62 desired_access,
63 object_attributes, 63 object_attributes,
64 static_cast<const void*>(client_id)); 64 static_cast<const void*>(client_id));
65 } 65 }
66 66
67 NTSTATUS NtQueryInformationThread(HANDLE thread_handle,
68 THREADINFOCLASS thread_information_class,
69 PVOID thread_information,
70 ULONG thread_information_length,
71 PULONG return_length) {
72 static decltype(::NtQueryInformationThread)* nt_query_information_thread =
73 reinterpret_cast<decltype(::NtQueryInformationThread)*>(GetProcAddress(
74 LoadLibrary(L"ntdll.dll"), "NtQueryInformationThread"));
75 DCHECK(nt_query_information_thread);
76 return nt_query_information_thread(thread_handle,
77 thread_information_class,
78 thread_information,
79 thread_information_length,
80 return_length);
81 }
82
83 // Copied from ntstatus.h because um/winnt.h conflicts with general inclusion of 67 // Copied from ntstatus.h because um/winnt.h conflicts with general inclusion of
84 // ntstatus.h. 68 // ntstatus.h.
85 #define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC0000023L) 69 #define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC0000023L)
86 #define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) 70 #define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
87 71
88 // Gets a pointer to the process information structure after a given one, or 72 // Gets a pointer to the process information structure after a given one, or
89 // null when iteration is complete, assuming they've been retrieved in a block 73 // null when iteration is complete, assuming they've been retrieved in a block
90 // via NtQuerySystemInformation(). 74 // via NtQuerySystemInformation().
91 template <class Traits> 75 template <class Traits>
92 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* NextProcess( 76 process_types::SYSTEM_PROCESS_INFORMATION<Traits>* NextProcess(
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 do { 130 do {
147 if (process->UniqueProcessId == process_id) 131 if (process->UniqueProcessId == process_id)
148 return process; 132 return process;
149 } while (process = NextProcess(process)); 133 } while (process = NextProcess(process));
150 134
151 LOG(ERROR) << "process " << process_id << " not found"; 135 LOG(ERROR) << "process " << process_id << " not found";
152 return nullptr; 136 return nullptr;
153 } 137 }
154 138
155 template <class Traits> 139 template <class Traits>
156 uint32_t GetThreadSuspendCount( 140 HANDLE OpenThread(const process_types::SYSTEM_EXTENDED_THREAD_INFORMATION<
157 const process_types::SYSTEM_EXTENDED_THREAD_INFORMATION<Traits>& 141 Traits>& thread_info) {
158 thread_info) { 142 HANDLE handle;
159 // Wait reason values are from KWAIT_REASON in wdm.h. We don't need all of 143 ACCESS_MASK query_access = THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME;
160 // them, so just declare the one we need.
161 const ULONG kWaitReasonSuspended = 5;
162
163 // Kernel mode enumerations for thread state come from
164 // http://www.nirsoft.net/kernel_struct/vista/KTHREAD_STATE.html and
165 // https://msdn.microsoft.com/en-us/library/system.diagnostics.threadstate(v=v s.110).aspx
166 const ULONG kThreadStateWaiting = 5;
167 const ULONG kThreadStateGateWait = 8;
168
169 bool suspended = (thread_info.ThreadState == kThreadStateWaiting ||
170 thread_info.ThreadState == kThreadStateGateWait) &&
171 thread_info.WaitReason == kWaitReasonSuspended;
172 if (!suspended)
173 return 0;
174
175 HANDLE thread_handle;
176 ACCESS_MASK query_access = THREAD_QUERY_LIMITED_INFORMATION;
177 OBJECT_ATTRIBUTES object_attributes; 144 OBJECT_ATTRIBUTES object_attributes;
178 InitializeObjectAttributes(&object_attributes, nullptr, 0, nullptr, nullptr); 145 InitializeObjectAttributes(&object_attributes, nullptr, 0, nullptr, nullptr);
179 NTSTATUS status = crashpad::NtOpenThread( 146 NTSTATUS status = crashpad::NtOpenThread(
180 &thread_handle, query_access, &object_attributes, &thread_info.ClientId); 147 &handle, query_access, &object_attributes, &thread_info.ClientId);
181 if (!NT_SUCCESS(status)) { 148 if (!NT_SUCCESS(status)) {
182 LOG(WARNING) << "couldn't open thread to retrieve suspend count"; 149 LOG(ERROR) << "NtOpenThread failed";
183 // Fall back to something semi-reasonable. We know we're suspended at this 150 return nullptr;
184 // point, so just return 1. 151 }
185 return 1; 152 return handle;
153 }
154
155 // It's necessary to suspend the thread to grab CONTEXT. SuspendThread has a
156 // side-effect of returning the SuspendCount of the thread on success, so we
157 // fill out these two pieces of semi-unrelated data in the same function.
158 template <class Traits>
159 void FillThreadContextAndSuspendCount(
160 const process_types::SYSTEM_EXTENDED_THREAD_INFORMATION<Traits>&
161 thread_info,
162 ProcessReaderWin::Thread* thread) {
163
164 // Don't suspend the thread if it's this thread. This is really only for test
165 // binaries, as we won't be walking ourselves, in general.
166 bool is_current_thread = thread_info.ClientId.UniqueThread ==
167 reinterpret_cast<process_types::TEB<Traits>*>(
168 NtCurrentTeb())->ClientId.UniqueThread;
169
170 ScopedKernelHANDLE thread_handle(OpenThread(thread_info));
171
172 if (is_current_thread) {
173 thread->suspend_count = 0;
174 } else {
175 DWORD previous_suspend_count = SuspendThread(thread_handle.get());
176 if (previous_suspend_count == -1) {
177 PLOG(ERROR) << "SuspendThread failed";
178 return;
179 }
180
181 thread->suspend_count = previous_suspend_count;
186 } 182 }
187 183
188 // Take ownership of this handle so we close on exit. NtClose and CloseHandle 184 // TODO(scottmg): Handle cross-bitness here.
189 // are identical.
190 ScopedKernelHANDLE handle(thread_handle);
191 185
192 // From ntddk.h. winternl.h defines THREADINFOCLASS, but only one value. 186 memset(&thread->context, 0, sizeof(thread->context));
193 const int kThreadSuspendCount = 35; 187 thread->context.ContextFlags = CONTEXT_ALL;
194 ULONG suspend_count; 188 if (!GetThreadContext(thread_handle.get(), &thread->context)) {
195 status = crashpad::NtQueryInformationThread( 189 PLOG(ERROR) << "GetThreadContext failed";
196 handle.get(), 190 return;
cpu_(ooo_6.6-7.5) 2015/05/13 00:58:29 "If you call GetThreadContext for the current thre
scottmg 2015/05/13 17:16:37 Thanks. I switch to RtlCaptureContext when it's th
197 static_cast<THREADINFOCLASS>(kThreadSuspendCount),
198 &suspend_count,
199 sizeof(suspend_count),
200 nullptr);
201 if (!NT_SUCCESS(status)) {
202 LOG(WARNING) << "NtQueryInformationThread failed" << std::hex << status;
203 return 1;
204 } 191 }
205 192
206 return suspend_count; 193 if (!is_current_thread) {
194 if (!ResumeThread(thread_handle.get())) {
195 PLOG(ERROR) << "ResumeThread failed";
196 }
197 }
207 } 198 }
208 199
209 } // namespace 200 } // namespace
210 201
211 ProcessReaderWin::Thread::Thread() 202 ProcessReaderWin::Thread::Thread()
212 : id(0), 203 : context(),
204 id(0),
213 teb(0), 205 teb(0),
214 stack_region_address(0), 206 stack_region_address(0),
215 stack_region_size(0), 207 stack_region_size(0),
216 suspend_count(0), 208 suspend_count(0),
217 priority_class(0), 209 priority_class(0),
218 priority(0) { 210 priority(0) {
219 } 211 }
220 212
221 ProcessReaderWin::ProcessReaderWin() 213 ProcessReaderWin::ProcessReaderWin()
222 : process_(INVALID_HANDLE_VALUE), 214 : process_(INVALID_HANDLE_VALUE),
(...skipping 20 matching lines...) Expand all
243 bool ProcessReaderWin::ReadMemory(WinVMAddress at, 235 bool ProcessReaderWin::ReadMemory(WinVMAddress at,
244 WinVMSize num_bytes, 236 WinVMSize num_bytes,
245 void* into) { 237 void* into) {
246 SIZE_T bytes_read; 238 SIZE_T bytes_read;
247 if (!ReadProcessMemory(process_, 239 if (!ReadProcessMemory(process_,
248 reinterpret_cast<void*>(at), 240 reinterpret_cast<void*>(at),
249 into, 241 into,
250 base::checked_cast<SIZE_T>(num_bytes), 242 base::checked_cast<SIZE_T>(num_bytes),
251 &bytes_read) || 243 &bytes_read) ||
252 num_bytes != bytes_read) { 244 num_bytes != bytes_read) {
253 PLOG(ERROR) << "ReadMemory at " << at << " of " << num_bytes << " failed"; 245 PLOG(ERROR) << "ReadMemory at 0x" << std::hex << at << std::dec << " of "
246 << num_bytes << " bytes failed";
254 return false; 247 return false;
255 } 248 }
256 return true; 249 return true;
257 } 250 }
258 251
259 bool ProcessReaderWin::StartTime(timeval* start_time) const { 252 bool ProcessReaderWin::StartTime(timeval* start_time) const {
260 FILETIME creation, exit, kernel, user; 253 FILETIME creation, exit, kernel, user;
261 if (!GetProcessTimes(process_, &creation, &exit, &kernel, &user)) { 254 if (!GetProcessTimes(process_, &creation, &exit, &kernel, &user)) {
262 PLOG(ERROR) << "GetProcessTimes"; 255 PLOG(ERROR) << "GetProcessTimes";
263 return false; 256 return false;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 process_types::SYSTEM_PROCESS_INFORMATION<SizeTraits>* process_information = 290 process_types::SYSTEM_PROCESS_INFORMATION<SizeTraits>* process_information =
298 GetProcessInformation<SizeTraits>(process_, &buffer); 291 GetProcessInformation<SizeTraits>(process_, &buffer);
299 if (!process_information) 292 if (!process_information)
300 return threads_; 293 return threads_;
301 294
302 for (unsigned long i = 0; i < process_information->NumberOfThreads; ++i) { 295 for (unsigned long i = 0; i < process_information->NumberOfThreads; ++i) {
303 const process_types::SYSTEM_EXTENDED_THREAD_INFORMATION<SizeTraits>& 296 const process_types::SYSTEM_EXTENDED_THREAD_INFORMATION<SizeTraits>&
304 thread_info = process_information->Threads[i]; 297 thread_info = process_information->Threads[i];
305 Thread thread; 298 Thread thread;
306 thread.id = thread_info.ClientId.UniqueThread; 299 thread.id = thread_info.ClientId.UniqueThread;
307 thread.suspend_count = GetThreadSuspendCount(thread_info); 300
301 FillThreadContextAndSuspendCount(thread_info, &thread);
308 302
309 // TODO(scottmg): I believe we could reverse engineer the PriorityClass from 303 // TODO(scottmg): I believe we could reverse engineer the PriorityClass from
310 // the Priority, BasePriority, and 304 // the Priority, BasePriority, and
311 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100 . 305 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100 .
312 // MinidumpThreadWriter doesn't handle it yet in any case, so investigate 306 // MinidumpThreadWriter doesn't handle it yet in any case, so investigate
313 // both of those at the same time if it's useful. 307 // both of those at the same time if it's useful.
314 thread.priority_class = NORMAL_PRIORITY_CLASS; 308 thread.priority_class = NORMAL_PRIORITY_CLASS;
315 309
316 thread.priority = thread_info.Priority; 310 thread.priority = thread_info.Priority;
317 thread.teb = thread_info.TebBase; 311 thread.teb = thread_info.TebBase;
318 312
319 // While there are semi-documented fields in the thread structure called 313 // While there are semi-documented fields in the thread structure called
320 // StackBase and StackLimit, they don't appear to be correct in practice (or 314 // StackBase and StackLimit, they don't appear to be correct in practice (or
321 // at least, I don't know how to interpret them). Instead, read the TIB 315 // at least, I don't know how to interpret them). Instead, read the TIB
322 // (Thread Information Block) which is the first element of the TEB, and use 316 // (Thread Information Block) which is the first element of the TEB, and use
323 // its stack fields. 317 // its stack fields.
324 process_types::NT_TIB<SizeTraits> tib; 318 process_types::NT_TIB<SizeTraits> tib;
325 if (ReadMemory(thread_info.TebBase, sizeof(tib), &tib)) { 319 if (ReadMemory(thread_info.TebBase, sizeof(tib), &tib)) {
326 thread.stack_region_address = tib.StackBase;
327 // Note, "backwards" because of direction of stack growth. 320 // Note, "backwards" because of direction of stack growth.
321 thread.stack_region_address = tib.StackLimit;
328 thread.stack_region_size = tib.StackBase - tib.StackLimit; 322 thread.stack_region_size = tib.StackBase - tib.StackLimit;
329 } 323 }
330 threads_.push_back(thread); 324 threads_.push_back(thread);
331 } 325 }
332 326
333 return threads_; 327 return threads_;
334 } 328 }
335 329
336 const std::vector<ProcessInfo::Module>& ProcessReaderWin::Modules() { 330 const std::vector<ProcessInfo::Module>& ProcessReaderWin::Modules() {
337 INITIALIZATION_STATE_DCHECK_VALID(initialized_); 331 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
338 332
339 if (!process_info_.Modules(&modules_)) { 333 if (!process_info_.Modules(&modules_)) {
340 LOG(ERROR) << "couldn't retrieve modules"; 334 LOG(ERROR) << "couldn't retrieve modules";
341 } 335 }
342 336
343 return modules_; 337 return modules_;
344 } 338 }
345 339
346 } // namespace crashpad 340 } // namespace crashpad
OLDNEW
« no previous file with comments | « snapshot/win/process_reader_win.h ('k') | snapshot/win/thread_snapshot_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698