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 
52  Status error;
53  if (m_process == nullptr)
54  error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32);
55 
56  std::vector<wchar_t> wpath(PATH_MAX);
57  if (::GetProcessImageFileNameW(m_process, wpath.data(), wpath.size())) {
58  std::string path;
59  if (llvm::convertWideToUTF8(wpath.data(), path))
60  file_spec.SetFile(path, FileSpec::Style::native);
61  else
62  error.SetErrorString("Error converting path to UTF-8");
63  } else
64  error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
65 
66  return error;
67 }
68 
71 }
72 
74  if (m_process == nullptr)
75  return false;
76 
77  DWORD code = 0;
78  if (!::GetExitCodeProcess(m_process, &code))
79  return false;
80 
81  return (code == STILL_ACTIVE);
82 }
83 
84 llvm::Expected<HostThread> HostProcessWindows::StartMonitoring(
85  const Host::MonitorChildProcessCallback &callback, bool monitor_signals) {
86  MonitorInfo *info = new MonitorInfo;
87  info->callback = callback;
88 
89  // Since the life of this HostProcessWindows instance and the life of the
90  // process may be different, duplicate the handle so that the monitor thread
91  // can have ownership over its own copy of the handle.
92  if (::DuplicateHandle(GetCurrentProcess(), m_process, GetCurrentProcess(),
93  &info->process_handle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
94  return ThreadLauncher::LaunchThread("ChildProcessMonitor",
96  info);
97  } else {
98  return llvm::errorCodeToError(llvm::mapWindowsError(GetLastError()));
99  }
100 }
101 
103  DWORD exit_code;
104 
105  MonitorInfo *info = static_cast<MonitorInfo *>(thread_arg);
106  if (info) {
107  ::WaitForSingleObject(info->process_handle, INFINITE);
108  ::GetExitCodeProcess(info->process_handle, &exit_code);
109  info->callback(::GetProcessId(info->process_handle), true, 0, exit_code);
110  ::CloseHandle(info->process_handle);
111  delete (info);
112  }
113  return {};
114 }
115 
118  ::CloseHandle(m_process);
119  m_process = nullptr;
120 }
lldb_private::HostProcessWindows::Terminate
Status Terminate() override
Definition: HostProcessWindows.cpp:40
lldb_private::HostProcessWindows::GetProcessId
lldb::pid_t GetProcessId() const override
Definition: HostProcessWindows.cpp:69
lldb_private::Host::MonitorChildProcessCallback
std::function< bool(lldb::pid_t pid, bool exited, int signal, int status)> MonitorChildProcessCallback
Definition: Host.h:68
lldb_private::HostProcessWindows::MonitorThread
static lldb::thread_result_t MonitorThread(void *thread_arg)
Definition: HostProcessWindows.cpp:102
lldb_private::HostProcessWindows::IsRunning
bool IsRunning() const override
Definition: HostProcessWindows.cpp:73
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
lldb_private::FileSpec
Definition: FileSpec.h:56
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:116
lldb_private::HostProcessWindows::StartMonitoring
virtual llvm::Expected< HostThread > StartMonitoring(const Host::MonitorChildProcessCallback &callback, bool monitor_signals) override
Definition: HostProcessWindows.cpp:84
lldb_private::HostProcessWindows::~HostProcessWindows
~HostProcessWindows()
Definition: HostProcessWindows.cpp:36
LLDB_INVALID_PROCESS
#define LLDB_INVALID_PROCESS
Definition: lldb-types.h:78
lldb_private::HostProcessWindows::GetMainModule
Status GetMainModule(FileSpec &file_spec) const override
Definition: HostProcessWindows.cpp:51
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb_private::Status
Definition: Status.h:44
lldb_private::HostProcessWindows::m_owns_handle
bool m_owns_handle
Definition: HostProcessWindows.h:42
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::FileSpec::SetFile
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:174
PATH_MAX
#define PATH_MAX
Definition: windows/PosixApi.h:25
lldb_private::ThreadLauncher::LaunchThread
static llvm::Expected< HostThread > LaunchThread(llvm::StringRef name, lldb::thread_func_t thread_function, lldb::thread_arg_t thread_arg, size_t min_stack_byte_size=0)
Definition: ThreadLauncher.cpp:24
lldb_private::HostNativeProcessBase
Definition: HostNativeProcessBase.h:21
lldb_private::HostNativeProcessBase::m_process
lldb::process_t m_process
Definition: HostNativeProcessBase.h:45
HostProcessWindows.h