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 9 #include "lldb/Host/windows/HostProcessWindows.h" 10 #include "lldb/Host/HostThread.h" 11 #include "lldb/Host/ThreadLauncher.h" 12 #include "lldb/Host/windows/windows.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 { 25 Host::MonitorChildProcessCallback callback; 26 HANDLE process_handle; 27 }; 28 } 29 30 HostProcessWindows::HostProcessWindows() 31 : HostNativeProcessBase(), m_owns_handle(true) {} 32 33 HostProcessWindows::HostProcessWindows(lldb::process_t process) 34 : HostNativeProcessBase(process), m_owns_handle(true) {} 35 36 HostProcessWindows::~HostProcessWindows() { Close(); } 37 38 void HostProcessWindows::SetOwnsHandle(bool owns) { m_owns_handle = owns; } 39 40 Status HostProcessWindows::Terminate() { 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 51 Status HostProcessWindows::GetMainModule(FileSpec &file_spec) const { 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 69 lldb::pid_t HostProcessWindows::GetProcessId() const { 70 return (m_process == LLDB_INVALID_PROCESS) ? -1 : ::GetProcessId(m_process); 71 } 72 73 bool HostProcessWindows::IsRunning() const { 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", 95 HostProcessWindows::MonitorThread, 96 info); 97 } else { 98 return llvm::errorCodeToError(llvm::mapWindowsError(GetLastError())); 99 } 100 } 101 102 lldb::thread_result_t HostProcessWindows::MonitorThread(void *thread_arg) { 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 116 void HostProcessWindows::Close() { 117 if (m_owns_handle && m_process != LLDB_INVALID_PROCESS) 118 ::CloseHandle(m_process); 119 m_process = nullptr; 120 } 121