1 //===-- HostProcessWindows.cpp ----------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Host/windows/windows.h" 11 12 #include <Psapi.h> 13 14 #include "lldb/Host/windows/HostProcessWindows.h" 15 16 #include "llvm/ADT/STLExtras.h" 17 18 using namespace lldb_private; 19 20 HostProcessWindows::HostProcessWindows() 21 : m_process(NULL) 22 , m_pid(0) 23 { 24 } 25 26 HostProcessWindows::~HostProcessWindows() 27 { 28 Close(); 29 } 30 31 Error HostProcessWindows::Create(lldb::pid_t pid) 32 { 33 Error error; 34 if (pid == m_pid) 35 return error; 36 Close(); 37 38 m_process = ::OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid); 39 if (m_process == NULL) 40 { 41 error.SetError(::GetLastError(), lldb::eErrorTypeWin32); 42 return error; 43 } 44 m_pid = pid; 45 return error; 46 } 47 48 Error HostProcessWindows::Create(lldb::process_t process) 49 { 50 Error error; 51 if (process == m_process) 52 return error; 53 Close(); 54 55 m_pid = ::GetProcessId(process); 56 if (m_pid == 0) 57 error.SetError(::GetLastError(), lldb::eErrorTypeWin32); 58 m_process = process; 59 return error; 60 } 61 62 Error HostProcessWindows::Terminate() 63 { 64 Error error; 65 if (m_process == NULL) 66 error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32); 67 68 if (!::TerminateProcess(m_process, 0)) 69 error.SetError(::GetLastError(), lldb::eErrorTypeWin32); 70 71 return error; 72 } 73 74 Error HostProcessWindows::GetMainModule(FileSpec &file_spec) const 75 { 76 Error error; 77 if (m_process == NULL) 78 error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32); 79 80 char path[MAX_PATH] = { 0 }; 81 if (::GetProcessImageFileName(m_process, path, llvm::array_lengthof(path))) 82 file_spec.SetFile(path, false); 83 else 84 error.SetError(::GetLastError(), lldb::eErrorTypeWin32); 85 86 return error; 87 } 88 89 lldb::pid_t HostProcessWindows::GetProcessId() const 90 { 91 return m_pid; 92 } 93 94 bool HostProcessWindows::IsRunning() const 95 { 96 if (m_process == NULL) 97 return false; 98 99 DWORD code = 0; 100 if (!::GetExitCodeProcess(m_process, &code)) 101 return false; 102 103 return (code == STILL_ACTIVE); 104 } 105 106 void HostProcessWindows::Close() 107 { 108 if (m_process != NULL) 109 ::CloseHandle(m_process); 110 m_process = nullptr; 111 m_pid = 0; 112 } 113