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