1 //===-- HostProcessWindows.cpp ----------------------------------*- C++ -*-===// 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 18 #include <psapi.h> 19 20 using namespace lldb_private; 21 22 namespace { 23 struct MonitorInfo { 24 Host::MonitorChildProcessCallback callback; 25 HANDLE process_handle; 26 }; 27 } 28 29 HostProcessWindows::HostProcessWindows() 30 : HostNativeProcessBase(), m_owns_handle(true) {} 31 32 HostProcessWindows::HostProcessWindows(lldb::process_t process) 33 : HostNativeProcessBase(process), m_owns_handle(true) {} 34 35 HostProcessWindows::~HostProcessWindows() { Close(); } 36 37 void HostProcessWindows::SetOwnsHandle(bool owns) { m_owns_handle = owns; } 38 39 Status HostProcessWindows::Terminate() { 40 Status error; 41 if (m_process == nullptr) 42 error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32); 43 44 if (!::TerminateProcess(m_process, 0)) 45 error.SetError(::GetLastError(), lldb::eErrorTypeWin32); 46 47 return error; 48 } 49 50 Status HostProcessWindows::GetMainModule(FileSpec &file_spec) const { 51 Status error; 52 if (m_process == nullptr) 53 error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32); 54 55 std::vector<wchar_t> wpath(PATH_MAX); 56 if (::GetProcessImageFileNameW(m_process, wpath.data(), wpath.size())) { 57 std::string path; 58 if (llvm::convertWideToUTF8(wpath.data(), path)) 59 file_spec.SetFile(path, FileSpec::Style::native); 60 else 61 error.SetErrorString("Error converting path to UTF-8"); 62 } else 63 error.SetError(::GetLastError(), lldb::eErrorTypeWin32); 64 65 return error; 66 } 67 68 lldb::pid_t HostProcessWindows::GetProcessId() const { 69 return (m_process == LLDB_INVALID_PROCESS) ? -1 : ::GetProcessId(m_process); 70 } 71 72 bool HostProcessWindows::IsRunning() const { 73 if (m_process == nullptr) 74 return false; 75 76 DWORD code = 0; 77 if (!::GetExitCodeProcess(m_process, &code)) 78 return false; 79 80 return (code == STILL_ACTIVE); 81 } 82 83 llvm::Expected<HostThread> HostProcessWindows::StartMonitoring( 84 const Host::MonitorChildProcessCallback &callback, bool monitor_signals) { 85 MonitorInfo *info = new MonitorInfo; 86 info->callback = callback; 87 88 // Since the life of this HostProcessWindows instance and the life of the 89 // process may be different, duplicate the handle so that the monitor thread 90 // can have ownership over its own copy of the handle. 91 if (::DuplicateHandle(GetCurrentProcess(), m_process, GetCurrentProcess(), 92 &info->process_handle, 0, FALSE, DUPLICATE_SAME_ACCESS)) { 93 return ThreadLauncher::LaunchThread("ChildProcessMonitor", 94 HostProcessWindows::MonitorThread, 95 info); 96 } else { 97 DWORD err = GetLastError(); 98 return llvm::errorCodeToError(std::error_code(err, std::system_category())); 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