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

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
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 ScopedKernelHANDLE thread_handle(OpenThread(thread_info));
164
165 DWORD previous_suspend_count = SuspendThread(thread_handle.get());
166 if (previous_suspend_count == -1) {
167 PLOG(ERROR) << "SuspendThread failed";
168 return;
186 } 169 }
187 170
188 // Take ownership of this handle so we close on exit. NtClose and CloseHandle 171 thread->suspend_count = previous_suspend_count;
189 // are identical.
190 ScopedKernelHANDLE handle(thread_handle);
191 172
192 // From ntddk.h. winternl.h defines THREADINFOCLASS, but only one value. 173 // TODO(scottmg): Handle cross-bitness here.
193 const int kThreadSuspendCount = 35; 174
194 ULONG suspend_count; 175 CONTEXT context;
cpu_(ooo_6.6-7.5) 2015/05/12 01:06:49 = {0} ? or you imagine that GTC in 176 fills every
scottmg 2015/05/12 19:37:05 This was all wrong in this ps, better now.
195 status = crashpad::NtQueryInformationThread( 176 if (!GetThreadContext(thread_handle.get(), &context)) {
196 handle.get(), 177 PLOG(ERROR) << "GetThreadContext failed";
197 static_cast<THREADINFOCLASS>(kThreadSuspendCount), 178 return;
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 } 179 }
205 180
206 return suspend_count; 181 if (!ResumeThread(thread_handle.get())) {
182 PLOG(ERROR) << "ResumeThread failed";
183 }
207 } 184 }
208 185
209 } // namespace 186 } // namespace
210 187
211 ProcessReaderWin::Thread::Thread() 188 ProcessReaderWin::Thread::Thread()
212 : id(0), 189 : context(),
190 id(0),
213 teb(0), 191 teb(0),
214 stack_region_address(0), 192 stack_region_address(0),
215 stack_region_size(0), 193 stack_region_size(0),
216 suspend_count(0), 194 suspend_count(0),
217 priority_class(0), 195 priority_class(0),
218 priority(0) { 196 priority(0) {
219 } 197 }
220 198
221 ProcessReaderWin::ProcessReaderWin() 199 ProcessReaderWin::ProcessReaderWin()
222 : process_(INVALID_HANDLE_VALUE), 200 : process_(INVALID_HANDLE_VALUE),
(...skipping 20 matching lines...) Expand all
243 bool ProcessReaderWin::ReadMemory(WinVMAddress at, 221 bool ProcessReaderWin::ReadMemory(WinVMAddress at,
244 WinVMSize num_bytes, 222 WinVMSize num_bytes,
245 void* into) { 223 void* into) {
246 SIZE_T bytes_read; 224 SIZE_T bytes_read;
247 if (!ReadProcessMemory(process_, 225 if (!ReadProcessMemory(process_,
248 reinterpret_cast<void*>(at), 226 reinterpret_cast<void*>(at),
249 into, 227 into,
250 base::checked_cast<SIZE_T>(num_bytes), 228 base::checked_cast<SIZE_T>(num_bytes),
251 &bytes_read) || 229 &bytes_read) ||
252 num_bytes != bytes_read) { 230 num_bytes != bytes_read) {
253 PLOG(ERROR) << "ReadMemory at " << at << " of " << num_bytes << " failed"; 231 PLOG(ERROR) << "ReadMemory at 0x" << std::hex << at << std::dec << " of "
232 << num_bytes << " bytes failed";
254 return false; 233 return false;
255 } 234 }
256 return true; 235 return true;
257 } 236 }
258 237
259 bool ProcessReaderWin::StartTime(timeval* start_time) const { 238 bool ProcessReaderWin::StartTime(timeval* start_time) const {
260 FILETIME creation, exit, kernel, user; 239 FILETIME creation, exit, kernel, user;
261 if (!GetProcessTimes(process_, &creation, &exit, &kernel, &user)) { 240 if (!GetProcessTimes(process_, &creation, &exit, &kernel, &user)) {
262 PLOG(ERROR) << "GetProcessTimes"; 241 PLOG(ERROR) << "GetProcessTimes";
263 return false; 242 return false;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 process_types::SYSTEM_PROCESS_INFORMATION<SizeTraits>* process_information = 276 process_types::SYSTEM_PROCESS_INFORMATION<SizeTraits>* process_information =
298 GetProcessInformation<SizeTraits>(process_, &buffer); 277 GetProcessInformation<SizeTraits>(process_, &buffer);
299 if (!process_information) 278 if (!process_information)
300 return threads_; 279 return threads_;
301 280
302 for (unsigned long i = 0; i < process_information->NumberOfThreads; ++i) { 281 for (unsigned long i = 0; i < process_information->NumberOfThreads; ++i) {
303 const process_types::SYSTEM_EXTENDED_THREAD_INFORMATION<SizeTraits>& 282 const process_types::SYSTEM_EXTENDED_THREAD_INFORMATION<SizeTraits>&
304 thread_info = process_information->Threads[i]; 283 thread_info = process_information->Threads[i];
305 Thread thread; 284 Thread thread;
306 thread.id = thread_info.ClientId.UniqueThread; 285 thread.id = thread_info.ClientId.UniqueThread;
307 thread.suspend_count = GetThreadSuspendCount(thread_info); 286
287 FillThreadContextAndSuspendCount(thread_info, &thread);
308 288
309 // TODO(scottmg): I believe we could reverse engineer the PriorityClass from 289 // TODO(scottmg): I believe we could reverse engineer the PriorityClass from
310 // the Priority, BasePriority, and 290 // the Priority, BasePriority, and
311 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100 . 291 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100 .
312 // MinidumpThreadWriter doesn't handle it yet in any case, so investigate 292 // MinidumpThreadWriter doesn't handle it yet in any case, so investigate
313 // both of those at the same time if it's useful. 293 // both of those at the same time if it's useful.
314 thread.priority_class = NORMAL_PRIORITY_CLASS; 294 thread.priority_class = NORMAL_PRIORITY_CLASS;
315 295
316 thread.priority = thread_info.Priority; 296 thread.priority = thread_info.Priority;
317 thread.teb = thread_info.TebBase; 297 thread.teb = thread_info.TebBase;
318 298
319 // While there are semi-documented fields in the thread structure called 299 // 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 300 // 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 301 // 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 302 // (Thread Information Block) which is the first element of the TEB, and use
323 // its stack fields. 303 // its stack fields.
cpu_(ooo_6.6-7.5) 2015/05/12 01:06:50 TIB is reliably afaik.
324 process_types::NT_TIB<SizeTraits> tib; 304 process_types::NT_TIB<SizeTraits> tib;
325 if (ReadMemory(thread_info.TebBase, sizeof(tib), &tib)) { 305 if (ReadMemory(thread_info.TebBase, sizeof(tib), &tib)) {
326 thread.stack_region_address = tib.StackBase;
327 // Note, "backwards" because of direction of stack growth. 306 // Note, "backwards" because of direction of stack growth.
307 thread.stack_region_address = tib.StackLimit;
328 thread.stack_region_size = tib.StackBase - tib.StackLimit; 308 thread.stack_region_size = tib.StackBase - tib.StackLimit;
329 } 309 }
330 threads_.push_back(thread); 310 threads_.push_back(thread);
331 } 311 }
332 312
333 return threads_; 313 return threads_;
334 } 314 }
335 315
336 const std::vector<ProcessInfo::Module>& ProcessReaderWin::Modules() { 316 const std::vector<ProcessInfo::Module>& ProcessReaderWin::Modules() {
337 INITIALIZATION_STATE_DCHECK_VALID(initialized_); 317 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
338 318
339 if (!process_info_.Modules(&modules_)) { 319 if (!process_info_.Modules(&modules_)) {
340 LOG(ERROR) << "couldn't retrieve modules"; 320 LOG(ERROR) << "couldn't retrieve modules";
341 } 321 }
342 322
343 return modules_; 323 return modules_;
344 } 324 }
345 325
346 } // namespace crashpad 326 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698