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 
10 #include "lldb/Host/HostThread.h"
13 #include "lldb/Utility/FileSpec.h"
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 
21 using namespace lldb_private;
22 
23 namespace {
24 struct 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 
41  Status error;
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 
78 llvm::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 }
lldb_private::HostProcessWindows::Terminate
Status Terminate() override
Definition: HostProcessWindows.cpp:40
lldb_private::HostProcessWindows::GetProcessId
lldb::pid_t GetProcessId() const override
Definition: HostProcessWindows.cpp:51
lldb_private::ThreadLauncher::LaunchThread
static llvm::Expected< HostThread > LaunchThread(llvm::StringRef name, std::function< lldb::thread_result_t()> thread_function, size_t min_stack_byte_size=0)
Definition: ThreadLauncher.cpp:25
lldb_private::HostProcessWindows::IsRunning
bool IsRunning() const override
Definition: HostProcessWindows.cpp:55
lldb_private::HostProcessWindows::SetOwnsHandle
void SetOwnsHandle(bool owns)
Definition: HostProcessWindows.cpp:38
ThreadLauncher.h
lldb::eErrorTypeWin32
@ eErrorTypeWin32
Standard Win32 error codes.
Definition: lldb-enumerations.h:312
MonitorThread
static lldb::thread_result_t MonitorThread(const Host::MonitorChildProcessCallback &callback, HANDLE process_handle)
Definition: HostProcessWindows.cpp:67
lldb_private::Host::MonitorChildProcessCallback
std::function< void(lldb::pid_t pid, int signal, int status)> MonitorChildProcessCallback
Definition: Host.h:69
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
HostThread.h
lldb_private::HostProcessWindows::HostProcessWindows
HostProcessWindows()
Definition: HostProcessWindows.cpp:30
lldb_private::HostProcessWindows::Close
void Close()
Definition: HostProcessWindows.cpp:96
lldb_private::HostProcessWindows::~HostProcessWindows
~HostProcessWindows()
Definition: HostProcessWindows.cpp:36
LLDB_INVALID_PROCESS
#define LLDB_INVALID_PROCESS
Definition: lldb-types.h:78
lldb_private::Status
Definition: Status.h:44
lldb_private::HostProcessWindows::m_owns_handle
bool m_owns_handle
Definition: HostProcessWindows.h:38
lldb::thread_result_t
void * thread_result_t
Definition: lldb-types.h:62
lldb::pid_t
uint64_t pid_t
Definition: lldb-types.h:85
lldb::process_t
uint64_t process_t
Definition: lldb-types.h:57
windows.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
FileSpec.h
lldb_private::HostProcessWindows::StartMonitoring
virtual llvm::Expected< HostThread > StartMonitoring(const Host::MonitorChildProcessCallback &callback) override
Definition: HostProcessWindows.cpp:78
lldb_private::HostNativeProcessBase
Definition: HostNativeProcessBase.h:21
lldb_private::HostNativeProcessBase::m_process
lldb::process_t m_process
Definition: HostNativeProcessBase.h:43
HostProcessWindows.h