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 HostThread HostProcessWindows::StartMonitoring( 84 const Host::MonitorChildProcessCallback &callback, bool monitor_signals) { 85 HostThread monitor_thread; 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 HostThread result; 93 if (::DuplicateHandle(GetCurrentProcess(), m_process, GetCurrentProcess(), 94 &info->process_handle, 0, FALSE, DUPLICATE_SAME_ACCESS)) 95 result = ThreadLauncher::LaunchThread("ChildProcessMonitor", 96 HostProcessWindows::MonitorThread, 97 info, nullptr); 98 return result; 99 } 100 101 lldb::thread_result_t HostProcessWindows::MonitorThread(void *thread_arg) { 102 DWORD exit_code; 103 104 MonitorInfo *info = static_cast<MonitorInfo *>(thread_arg); 105 if (info) { 106 ::WaitForSingleObject(info->process_handle, INFINITE); 107 ::GetExitCodeProcess(info->process_handle, &exit_code); 108 info->callback(::GetProcessId(info->process_handle), true, 0, exit_code); 109 ::CloseHandle(info->process_handle); 110 delete (info); 111 } 112 return 0; 113 } 114 115 void HostProcessWindows::Close() { 116 if (m_owns_handle && m_process != LLDB_INVALID_PROCESS) 117 ::CloseHandle(m_process); 118 m_process = nullptr; 119 } 120