2015-03-02 13:06:34 -08:00
|
|
|
// Copyright 2015 The Crashpad Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "snapshot/win/process_reader_win.h"
|
|
|
|
|
2015-05-11 13:29:52 -07:00
|
|
|
#include <winternl.h>
|
|
|
|
|
|
|
|
#include "base/memory/scoped_ptr.h"
|
2015-05-01 13:48:23 -07:00
|
|
|
#include "base/numerics/safe_conversions.h"
|
2015-09-30 14:10:08 -04:00
|
|
|
#include "util/win/capture_context.h"
|
2015-08-18 12:25:19 -07:00
|
|
|
#include "util/win/nt_internals.h"
|
2015-09-16 12:42:20 -07:00
|
|
|
#include "util/win/ntstatus_logging.h"
|
2015-05-11 13:29:52 -07:00
|
|
|
#include "util/win/process_structs.h"
|
|
|
|
#include "util/win/scoped_handle.h"
|
2015-05-06 11:13:44 -07:00
|
|
|
#include "util/win/time.h"
|
2015-05-01 13:48:23 -07:00
|
|
|
|
2015-03-02 13:06:34 -08:00
|
|
|
namespace crashpad {
|
|
|
|
|
2015-05-11 13:29:52 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Gets a pointer to the process information structure after a given one, or
|
|
|
|
// null when iteration is complete, assuming they've been retrieved in a block
|
|
|
|
// via NtQuerySystemInformation().
|
|
|
|
template <class Traits>
|
|
|
|
process_types::SYSTEM_PROCESS_INFORMATION<Traits>* NextProcess(
|
|
|
|
process_types::SYSTEM_PROCESS_INFORMATION<Traits>* process) {
|
|
|
|
ULONG offset = process->NextEntryOffset;
|
|
|
|
if (offset == 0)
|
|
|
|
return nullptr;
|
|
|
|
return reinterpret_cast<process_types::SYSTEM_PROCESS_INFORMATION<Traits>*>(
|
|
|
|
reinterpret_cast<uint8_t*>(process) + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Retrieves the SYSTEM_PROCESS_INFORMATION for a given process.
|
|
|
|
//!
|
|
|
|
//! The returned pointer points into the memory block stored by \a buffer.
|
|
|
|
//! Ownership of \a buffer is transferred to the caller.
|
|
|
|
//!
|
|
|
|
//! \return Pointer to the process' data, or nullptr if it was not found or on
|
|
|
|
//! error. On error, a message will be logged.
|
|
|
|
template <class Traits>
|
|
|
|
process_types::SYSTEM_PROCESS_INFORMATION<Traits>* GetProcessInformation(
|
|
|
|
HANDLE process_handle,
|
|
|
|
scoped_ptr<uint8_t[]>* buffer) {
|
|
|
|
ULONG buffer_size = 16384;
|
|
|
|
buffer->reset(new uint8_t[buffer_size]);
|
|
|
|
NTSTATUS status;
|
|
|
|
// This must be in retry loop, as we're racing with process creation on the
|
|
|
|
// system to find a buffer large enough to hold all process information.
|
|
|
|
for (int tries = 0; tries < 20; ++tries) {
|
|
|
|
status = crashpad::NtQuerySystemInformation(
|
2015-09-16 12:42:20 -07:00
|
|
|
SystemProcessInformation,
|
2015-05-11 13:29:52 -07:00
|
|
|
reinterpret_cast<void*>(buffer->get()),
|
|
|
|
buffer_size,
|
|
|
|
&buffer_size);
|
|
|
|
if (status == STATUS_BUFFER_TOO_SMALL ||
|
|
|
|
status == STATUS_INFO_LENGTH_MISMATCH) {
|
|
|
|
// Add a little extra to try to avoid an additional loop iteration. We're
|
|
|
|
// racing with system-wide process creation between here and the next call
|
|
|
|
// to NtQuerySystemInformation().
|
|
|
|
buffer_size += 4096;
|
|
|
|
buffer->reset(new uint8_t[buffer_size]);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(status)) {
|
2015-09-16 12:42:20 -07:00
|
|
|
NTSTATUS_LOG(ERROR, status) << "NtQuerySystemInformation";
|
2015-05-11 13:29:52 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
process_types::SYSTEM_PROCESS_INFORMATION<Traits>* process =
|
|
|
|
reinterpret_cast<process_types::SYSTEM_PROCESS_INFORMATION<Traits>*>(
|
|
|
|
buffer->get());
|
|
|
|
DWORD process_id = GetProcessId(process_handle);
|
|
|
|
do {
|
|
|
|
if (process->UniqueProcessId == process_id)
|
|
|
|
return process;
|
|
|
|
} while (process = NextProcess(process));
|
|
|
|
|
|
|
|
LOG(ERROR) << "process " << process_id << " not found";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Traits>
|
2015-09-16 12:42:20 -07:00
|
|
|
HANDLE OpenThread(
|
|
|
|
const process_types::SYSTEM_THREAD_INFORMATION<Traits>& thread_info) {
|
2015-05-14 17:37:02 -07:00
|
|
|
HANDLE handle;
|
2015-09-16 12:42:20 -07:00
|
|
|
ACCESS_MASK query_access =
|
|
|
|
THREAD_GET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_QUERY_INFORMATION;
|
2015-05-11 13:29:52 -07:00
|
|
|
OBJECT_ATTRIBUTES object_attributes;
|
|
|
|
InitializeObjectAttributes(&object_attributes, nullptr, 0, nullptr, nullptr);
|
|
|
|
NTSTATUS status = crashpad::NtOpenThread(
|
2015-05-14 17:37:02 -07:00
|
|
|
&handle, query_access, &object_attributes, &thread_info.ClientId);
|
2015-05-11 13:29:52 -07:00
|
|
|
if (!NT_SUCCESS(status)) {
|
2015-09-16 12:42:20 -07:00
|
|
|
NTSTATUS_LOG(ERROR, status) << "NtOpenThread";
|
2015-05-14 17:37:02 -07:00
|
|
|
return nullptr;
|
2015-05-11 13:29:52 -07:00
|
|
|
}
|
2015-05-14 17:37:02 -07:00
|
|
|
return handle;
|
|
|
|
}
|
2015-05-11 13:29:52 -07:00
|
|
|
|
2015-05-14 17:37:02 -07:00
|
|
|
// It's necessary to suspend the thread to grab CONTEXT. SuspendThread has a
|
|
|
|
// side-effect of returning the SuspendCount of the thread on success, so we
|
|
|
|
// fill out these two pieces of semi-unrelated data in the same function.
|
|
|
|
template <class Traits>
|
2015-09-16 12:42:20 -07:00
|
|
|
bool FillThreadContextAndSuspendCount(HANDLE thread_handle,
|
|
|
|
ProcessReaderWin::Thread* thread,
|
2015-09-18 16:06:05 -07:00
|
|
|
ProcessSuspensionState suspension_state,
|
|
|
|
bool is_64_reading_32) {
|
2015-05-14 17:37:02 -07:00
|
|
|
// Don't suspend the thread if it's this thread. This is really only for test
|
|
|
|
// binaries, as we won't be walking ourselves, in general.
|
2015-09-16 12:42:20 -07:00
|
|
|
bool is_current_thread = thread->id ==
|
2015-05-14 17:37:02 -07:00
|
|
|
reinterpret_cast<process_types::TEB<Traits>*>(
|
|
|
|
NtCurrentTeb())->ClientId.UniqueThread;
|
|
|
|
|
|
|
|
if (is_current_thread) {
|
2015-09-09 12:29:29 -07:00
|
|
|
DCHECK(suspension_state == ProcessSuspensionState::kRunning);
|
2015-05-14 17:37:02 -07:00
|
|
|
thread->suspend_count = 0;
|
2015-09-18 16:06:05 -07:00
|
|
|
DCHECK(!is_64_reading_32);
|
2015-09-30 14:10:08 -04:00
|
|
|
CaptureContext(&thread->context.native);
|
2015-05-14 17:37:02 -07:00
|
|
|
} else {
|
2015-09-16 12:42:20 -07:00
|
|
|
DWORD previous_suspend_count = SuspendThread(thread_handle);
|
2015-05-14 17:37:02 -07:00
|
|
|
if (previous_suspend_count == -1) {
|
2015-09-18 16:06:05 -07:00
|
|
|
PLOG(ERROR) << "SuspendThread";
|
2015-09-16 12:42:20 -07:00
|
|
|
return false;
|
2015-05-14 17:37:02 -07:00
|
|
|
}
|
2015-09-09 12:29:29 -07:00
|
|
|
DCHECK(previous_suspend_count > 0 ||
|
|
|
|
suspension_state == ProcessSuspensionState::kRunning);
|
|
|
|
thread->suspend_count =
|
|
|
|
previous_suspend_count -
|
|
|
|
(suspension_state == ProcessSuspensionState::kSuspended ? 1 : 0);
|
2015-05-14 17:37:02 -07:00
|
|
|
|
|
|
|
memset(&thread->context, 0, sizeof(thread->context));
|
2015-09-18 16:06:05 -07:00
|
|
|
#if defined(ARCH_CPU_32_BITS)
|
|
|
|
const bool is_native = true;
|
|
|
|
#elif defined(ARCH_CPU_64_BITS)
|
|
|
|
const bool is_native = !is_64_reading_32;
|
|
|
|
if (is_64_reading_32) {
|
|
|
|
thread->context.wow64.ContextFlags = CONTEXT_ALL;
|
|
|
|
if (!Wow64GetThreadContext(thread_handle, &thread->context.wow64)) {
|
|
|
|
PLOG(ERROR) << "Wow64GetThreadContext";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (is_native) {
|
|
|
|
thread->context.native.ContextFlags = CONTEXT_ALL;
|
|
|
|
if (!GetThreadContext(thread_handle, &thread->context.native)) {
|
|
|
|
PLOG(ERROR) << "GetThreadContext";
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-14 17:37:02 -07:00
|
|
|
}
|
2015-05-11 13:29:52 -07:00
|
|
|
|
2015-09-16 12:42:20 -07:00
|
|
|
if (!ResumeThread(thread_handle)) {
|
2015-09-18 16:06:05 -07:00
|
|
|
PLOG(ERROR) << "ResumeThread";
|
2015-09-16 12:42:20 -07:00
|
|
|
return false;
|
2015-05-14 17:37:02 -07:00
|
|
|
}
|
|
|
|
}
|
2015-09-16 12:42:20 -07:00
|
|
|
|
|
|
|
return true;
|
2015-05-11 13:29:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ProcessReaderWin::Thread::Thread()
|
2015-05-14 17:37:02 -07:00
|
|
|
: context(),
|
|
|
|
id(0),
|
2015-10-01 14:04:49 -07:00
|
|
|
teb_address(0),
|
|
|
|
teb_size(0),
|
2015-05-11 13:29:52 -07:00
|
|
|
stack_region_address(0),
|
|
|
|
stack_region_size(0),
|
|
|
|
suspend_count(0),
|
|
|
|
priority_class(0),
|
|
|
|
priority(0) {
|
|
|
|
}
|
|
|
|
|
2015-05-01 13:48:23 -07:00
|
|
|
ProcessReaderWin::ProcessReaderWin()
|
|
|
|
: process_(INVALID_HANDLE_VALUE),
|
|
|
|
process_info_(),
|
2015-05-11 13:29:52 -07:00
|
|
|
threads_(),
|
2015-05-01 13:48:23 -07:00
|
|
|
modules_(),
|
2015-09-09 12:29:29 -07:00
|
|
|
suspension_state_(),
|
2015-05-11 13:29:52 -07:00
|
|
|
initialized_threads_(false),
|
2015-05-01 13:48:23 -07:00
|
|
|
initialized_() {
|
2015-03-02 13:06:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ProcessReaderWin::~ProcessReaderWin() {
|
|
|
|
}
|
|
|
|
|
2015-09-09 12:29:29 -07:00
|
|
|
bool ProcessReaderWin::Initialize(HANDLE process,
|
|
|
|
ProcessSuspensionState suspension_state) {
|
2015-03-02 13:06:34 -08:00
|
|
|
INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
|
|
|
|
|
2015-05-01 13:48:23 -07:00
|
|
|
process_ = process;
|
2015-09-09 12:29:29 -07:00
|
|
|
suspension_state_ = suspension_state;
|
2015-03-06 16:05:34 -08:00
|
|
|
process_info_.Initialize(process);
|
|
|
|
|
2015-03-02 13:06:34 -08:00
|
|
|
INITIALIZATION_STATE_SET_VALID(initialized_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-01 13:48:23 -07:00
|
|
|
bool ProcessReaderWin::ReadMemory(WinVMAddress at,
|
|
|
|
WinVMSize num_bytes,
|
2015-09-16 12:42:20 -07:00
|
|
|
void* into) const {
|
2015-05-01 13:48:23 -07:00
|
|
|
SIZE_T bytes_read;
|
|
|
|
if (!ReadProcessMemory(process_,
|
|
|
|
reinterpret_cast<void*>(at),
|
|
|
|
into,
|
|
|
|
base::checked_cast<SIZE_T>(num_bytes),
|
|
|
|
&bytes_read) ||
|
|
|
|
num_bytes != bytes_read) {
|
2015-05-14 17:37:02 -07:00
|
|
|
PLOG(ERROR) << "ReadMemory at 0x" << std::hex << at << std::dec << " of "
|
|
|
|
<< num_bytes << " bytes failed";
|
2015-05-01 13:48:23 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-06 11:13:44 -07:00
|
|
|
bool ProcessReaderWin::StartTime(timeval* start_time) const {
|
|
|
|
FILETIME creation, exit, kernel, user;
|
|
|
|
if (!GetProcessTimes(process_, &creation, &exit, &kernel, &user)) {
|
|
|
|
PLOG(ERROR) << "GetProcessTimes";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*start_time = FiletimeToTimevalEpoch(creation);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessReaderWin::CPUTimes(timeval* user_time,
|
|
|
|
timeval* system_time) const {
|
|
|
|
FILETIME creation, exit, kernel, user;
|
|
|
|
if (!GetProcessTimes(process_, &creation, &exit, &kernel, &user)) {
|
|
|
|
PLOG(ERROR) << "GetProcessTimes";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*user_time = FiletimeToTimevalInterval(user);
|
|
|
|
*system_time = FiletimeToTimevalInterval(kernel);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-11 13:29:52 -07:00
|
|
|
const std::vector<ProcessReaderWin::Thread>& ProcessReaderWin::Threads() {
|
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
|
|
|
if (initialized_threads_)
|
|
|
|
return threads_;
|
|
|
|
|
|
|
|
initialized_threads_ = true;
|
|
|
|
|
2015-09-18 16:06:05 -07:00
|
|
|
#if defined(ARCH_CPU_64_BITS)
|
|
|
|
ReadThreadData<process_types::internal::Traits64>(process_info_.IsWow64());
|
|
|
|
#else
|
|
|
|
ReadThreadData<process_types::internal::Traits32>(false);
|
|
|
|
#endif
|
2015-09-16 12:42:20 -07:00
|
|
|
|
|
|
|
return threads_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<ProcessInfo::Module>& ProcessReaderWin::Modules() {
|
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
|
|
|
if (!process_info_.Modules(&modules_)) {
|
|
|
|
LOG(ERROR) << "couldn't retrieve modules";
|
|
|
|
}
|
|
|
|
|
|
|
|
return modules_;
|
|
|
|
}
|
|
|
|
|
2015-09-25 10:31:02 -07:00
|
|
|
const ProcessInfo& ProcessReaderWin::GetProcessInfo() const {
|
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
return process_info_;
|
|
|
|
}
|
|
|
|
|
2015-09-16 12:42:20 -07:00
|
|
|
template <class Traits>
|
2015-09-18 16:06:05 -07:00
|
|
|
void ProcessReaderWin::ReadThreadData(bool is_64_reading_32) {
|
2015-05-11 13:29:52 -07:00
|
|
|
DCHECK(threads_.empty());
|
|
|
|
|
|
|
|
scoped_ptr<uint8_t[]> buffer;
|
2015-09-16 12:42:20 -07:00
|
|
|
process_types::SYSTEM_PROCESS_INFORMATION<Traits>* process_information =
|
|
|
|
GetProcessInformation<Traits>(process_, &buffer);
|
2015-05-11 13:29:52 -07:00
|
|
|
if (!process_information)
|
2015-09-16 12:42:20 -07:00
|
|
|
return;
|
2015-05-11 13:29:52 -07:00
|
|
|
|
|
|
|
for (unsigned long i = 0; i < process_information->NumberOfThreads; ++i) {
|
2015-09-16 12:42:20 -07:00
|
|
|
const process_types::SYSTEM_THREAD_INFORMATION<Traits>& thread_info =
|
|
|
|
process_information->Threads[i];
|
|
|
|
ProcessReaderWin::Thread thread;
|
2015-05-11 13:29:52 -07:00
|
|
|
thread.id = thread_info.ClientId.UniqueThread;
|
2015-05-14 17:37:02 -07:00
|
|
|
|
2015-09-16 12:42:20 -07:00
|
|
|
ScopedKernelHANDLE thread_handle(OpenThread(thread_info));
|
|
|
|
if (!thread_handle.is_valid())
|
|
|
|
continue;
|
|
|
|
|
2015-09-18 16:06:05 -07:00
|
|
|
if (!FillThreadContextAndSuspendCount<Traits>(thread_handle.get(),
|
|
|
|
&thread,
|
|
|
|
suspension_state_,
|
|
|
|
is_64_reading_32)) {
|
2015-09-16 12:42:20 -07:00
|
|
|
continue;
|
|
|
|
}
|
2015-05-11 13:29:52 -07:00
|
|
|
|
|
|
|
// TODO(scottmg): I believe we could reverse engineer the PriorityClass from
|
|
|
|
// the Priority, BasePriority, and
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms685100 .
|
|
|
|
// MinidumpThreadWriter doesn't handle it yet in any case, so investigate
|
|
|
|
// both of those at the same time if it's useful.
|
|
|
|
thread.priority_class = NORMAL_PRIORITY_CLASS;
|
|
|
|
|
|
|
|
thread.priority = thread_info.Priority;
|
2015-09-16 12:42:20 -07:00
|
|
|
|
|
|
|
process_types::THREAD_BASIC_INFORMATION<Traits> thread_basic_info;
|
|
|
|
NTSTATUS status = crashpad::NtQueryInformationThread(
|
|
|
|
thread_handle.get(),
|
|
|
|
static_cast<THREADINFOCLASS>(ThreadBasicInformation),
|
|
|
|
&thread_basic_info,
|
|
|
|
sizeof(thread_basic_info),
|
|
|
|
nullptr);
|
|
|
|
if (!NT_SUCCESS(status)) {
|
|
|
|
NTSTATUS_LOG(ERROR, status) << "NtQueryInformationThread";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the TIB (Thread Information Block) which is the first element of the
|
|
|
|
// TEB, for its stack fields.
|
|
|
|
process_types::NT_TIB<Traits> tib;
|
2015-10-01 14:04:49 -07:00
|
|
|
thread.teb_address = thread_basic_info.TebBaseAddress;
|
|
|
|
thread.teb_size = sizeof(process_types::TEB<Traits>);
|
|
|
|
if (ReadMemory(thread.teb_address, sizeof(tib), &tib)) {
|
2015-09-18 16:06:05 -07:00
|
|
|
WinVMAddress base = 0;
|
|
|
|
WinVMAddress limit = 0;
|
|
|
|
// If we're reading a WOW64 process, then the TIB we just retrieved is the
|
|
|
|
// x64 one. The first word of the x64 TIB points at the x86 TIB. See
|
|
|
|
// https://msdn.microsoft.com/en-us/library/dn424783.aspx
|
|
|
|
if (is_64_reading_32) {
|
|
|
|
process_types::NT_TIB<process_types::internal::Traits32> tib32;
|
2015-10-01 14:04:49 -07:00
|
|
|
thread.teb_address = tib.Wow64Teb;
|
|
|
|
thread.teb_size =
|
|
|
|
sizeof(process_types::TEB<process_types::internal::Traits32>);
|
|
|
|
if (ReadMemory(thread.teb_address, sizeof(tib32), &tib32)) {
|
2015-09-18 16:06:05 -07:00
|
|
|
base = tib32.StackBase;
|
|
|
|
limit = tib32.StackLimit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
base = tib.StackBase;
|
|
|
|
limit = tib.StackLimit;
|
|
|
|
}
|
|
|
|
|
2015-05-11 13:29:52 -07:00
|
|
|
// Note, "backwards" because of direction of stack growth.
|
2015-09-18 16:06:05 -07:00
|
|
|
thread.stack_region_address = limit;
|
|
|
|
if (limit > base) {
|
|
|
|
LOG(ERROR) << "invalid stack range: " << base << " - " << limit;
|
2015-09-16 12:42:20 -07:00
|
|
|
thread.stack_region_size = 0;
|
|
|
|
} else {
|
2015-09-18 16:06:05 -07:00
|
|
|
thread.stack_region_size = base - limit;
|
2015-09-16 12:42:20 -07:00
|
|
|
}
|
2015-05-11 13:29:52 -07:00
|
|
|
}
|
|
|
|
threads_.push_back(thread);
|
|
|
|
}
|
2015-05-01 13:48:23 -07:00
|
|
|
}
|
|
|
|
|
2015-03-02 13:06:34 -08:00
|
|
|
} // namespace crashpad
|