LLDB mainline
HostProcessWindows.cpp
Go to the documentation of this file.
1//===-- HostProcessWindows.cpp --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
14
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/Support/ConvertUTF.h"
17#include "llvm/Support/WindowsError.h"
18
19#include <psapi.h>
20
21using namespace lldb_private;
22
23namespace {
24struct MonitorInfo {
26 HANDLE process_handle;
27};
28}
29
32
35
37
39
41 if (m_process == nullptr)
42 return Status(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32);
43
44 if (!::TerminateProcess(m_process, 0))
45 return Status(::GetLastError(), lldb::eErrorTypeWin32);
46
47 return Status();
48}
49
53
55 if (m_process == nullptr)
56 return false;
57
58 DWORD code = 0;
59 if (!::GetExitCodeProcess(m_process, &code))
60 return false;
61
62 return (code == STILL_ACTIVE);
63}
64
67 HANDLE process_handle) {
68 DWORD exit_code;
69
70 ::WaitForSingleObject(process_handle, INFINITE);
71 ::GetExitCodeProcess(process_handle, &exit_code);
72 callback(::GetProcessId(process_handle), 0, exit_code);
73 ::CloseHandle(process_handle);
74 return {};
75}
76
77llvm::Expected<HostThread> HostProcessWindows::StartMonitoring(
78 const Host::MonitorChildProcessCallback &callback) {
79 HANDLE process_handle;
80
81 // Since the life of this HostProcessWindows instance and the life of the
82 // process may be different, duplicate the handle so that the monitor thread
83 // can have ownership over its own copy of the handle.
84 if (::DuplicateHandle(GetCurrentProcess(), m_process, GetCurrentProcess(),
85 &process_handle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
87 "ChildProcessMonitor", [callback, process_handle] {
88 return MonitorThread(callback, process_handle);
89 });
90 } else {
91 return llvm::errorCodeToError(llvm::mapWindowsError(GetLastError()));
92 }
93}
94
97 ::CloseHandle(m_process);
98 m_process = nullptr;
99}
static lldb::thread_result_t MonitorThread(const Host::MonitorChildProcessCallback &callback, HANDLE process_handle)
void * HANDLE
HostNativeProcessBase(const HostNativeProcessBase &)=delete
lldb::pid_t GetProcessId() const override
virtual llvm::Expected< HostThread > StartMonitoring(const Host::MonitorChildProcessCallback &callback) override
std::function< void(lldb::pid_t pid, int signal, int status)> MonitorChildProcessCallback
Definition Host.h:88
An error handling class.
Definition Status.h:118
static llvm::Expected< HostThread > LaunchThread(llvm::StringRef name, std::function< lldb::thread_result_t()> thread_function, size_t min_stack_byte_size=0)
#define LLDB_INVALID_PROCESS
Definition lldb-types.h:68
A class that represents a running process on the host machine.
void * thread_result_t
Definition lldb-types.h:62
@ eErrorTypeWin32
Standard Win32 error codes.
uint64_t pid_t
Definition lldb-types.h:83
uint64_t process_t
Definition lldb-types.h:57