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
31 : HostNativeProcessBase(), m_owns_handle(true) {}
32
34 : HostNativeProcessBase(process), m_owns_handle(true) {}
35
37
39
42 if (m_process == nullptr)
43 error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32);
44
45 if (!::TerminateProcess(m_process, 0))
46 error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
47
48 return error;
49}
50
53}
54
56 if (m_process == nullptr)
57 return false;
58
59 DWORD code = 0;
60 if (!::GetExitCodeProcess(m_process, &code))
61 return false;
62
63 return (code == STILL_ACTIVE);
64}
65
68 HANDLE process_handle) {
69 DWORD exit_code;
70
71 ::WaitForSingleObject(process_handle, INFINITE);
72 ::GetExitCodeProcess(process_handle, &exit_code);
73 callback(::GetProcessId(process_handle), 0, exit_code);
74 ::CloseHandle(process_handle);
75 return {};
76}
77
78llvm::Expected<HostThread> HostProcessWindows::StartMonitoring(
79 const Host::MonitorChildProcessCallback &callback) {
80 HANDLE process_handle;
81
82 // Since the life of this HostProcessWindows instance and the life of the
83 // process may be different, duplicate the handle so that the monitor thread
84 // can have ownership over its own copy of the handle.
85 if (::DuplicateHandle(GetCurrentProcess(), m_process, GetCurrentProcess(),
86 &process_handle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
88 "ChildProcessMonitor", [callback, process_handle] {
89 return MonitorThread(callback, process_handle);
90 });
91 } else {
92 return llvm::errorCodeToError(llvm::mapWindowsError(GetLastError()));
93 }
94}
95
98 ::CloseHandle(m_process);
99 m_process = nullptr;
100}
static llvm::raw_ostream & error(Stream &strm)
static lldb::thread_result_t MonitorThread(const Host::MonitorChildProcessCallback &callback, HANDLE process_handle)
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:69
An error handling class.
Definition: Status.h:44
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