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/FileSpec.h"
11 #include "lldb/Host/windows/windows.h"
12 #include "lldb/Host/windows/HostProcessWindows.h"
13 
14 #include "llvm/ADT/STLExtras.h"
15 
16 #include <Psapi.h>
17 
18 using namespace lldb_private;
19 
20 HostProcessWindows::HostProcessWindows()
21     : HostNativeProcessBase()
22 {
23 }
24 
25 HostProcessWindows::HostProcessWindows(lldb::process_t process)
26     : HostNativeProcessBase(process)
27 {
28 }
29 
30 HostProcessWindows::~HostProcessWindows()
31 {
32     Close();
33 }
34 
35 Error HostProcessWindows::Terminate()
36 {
37     Error error;
38     if (m_process == nullptr)
39         error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32);
40 
41     if (!::TerminateProcess(m_process, 0))
42         error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
43 
44     return error;
45 }
46 
47 Error HostProcessWindows::GetMainModule(FileSpec &file_spec) const
48 {
49     Error error;
50     if (m_process == nullptr)
51         error.SetError(ERROR_INVALID_HANDLE, lldb::eErrorTypeWin32);
52 
53     char path[MAX_PATH] = { 0 };
54     if (::GetProcessImageFileName(m_process, path, llvm::array_lengthof(path)))
55         file_spec.SetFile(path, false);
56     else
57         error.SetError(::GetLastError(), lldb::eErrorTypeWin32);
58 
59     return error;
60 }
61 
62 lldb::pid_t HostProcessWindows::GetProcessId() const
63 {
64     return ::GetProcessId(m_process);
65 }
66 
67 bool HostProcessWindows::IsRunning() const
68 {
69     if (m_process == nullptr)
70         return false;
71 
72     DWORD code = 0;
73     if (!::GetExitCodeProcess(m_process, &code))
74         return false;
75 
76     return (code == STILL_ACTIVE);
77 }
78 
79 void HostProcessWindows::Close()
80 {
81     if (m_process != nullptr)
82         ::CloseHandle(m_process);
83     m_process = nullptr;
84 }
85