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/posix/HostProcessPosix.h"
11 #include "lldb/Host/FileSystem.h"
12 
13 #include "llvm/ADT/STLExtras.h"
14 
15 #include <limits.h>
16 
17 using namespace lldb_private;
18 
19 namespace
20 {
21     const int kInvalidPosixProcess = 0;
22 }
23 
24 HostProcessPosix::HostProcessPosix()
25     : HostNativeProcessBase(kInvalidPosixProcess)
26 {
27 }
28 
29 HostProcessPosix::HostProcessPosix(lldb::process_t process)
30     : HostNativeProcessBase(process)
31 {
32 }
33 
34 HostProcessPosix::~HostProcessPosix()
35 {
36 }
37 
38 Error HostProcessPosix::Signal(int signo) const
39 {
40     if (m_process == kInvalidPosixProcess)
41     {
42         Error error;
43         error.SetErrorString("HostProcessPosix refers to an invalid process");
44         return error;
45     }
46 
47     return HostProcessPosix::Signal(m_process, signo);
48 }
49 
50 Error HostProcessPosix::Signal(lldb::process_t process, int signo)
51 {
52     Error error;
53 
54     if (-1 == ::kill(process, signo))
55         error.SetErrorToErrno();
56 
57     return error;
58 }
59 
60 Error HostProcessPosix::Terminate()
61 {
62     return Signal(SIGKILL);
63 }
64 
65 Error HostProcessPosix::GetMainModule(FileSpec &file_spec) const
66 {
67     Error error;
68 
69     // Use special code here because proc/[pid]/exe is a symbolic link.
70     char link_path[PATH_MAX];
71     char exe_path[PATH_MAX] = "";
72     if (snprintf (link_path, PATH_MAX, "/proc/%" PRIu64 "/exe", m_process) <= 0)
73     {
74         error.SetErrorString("Unable to build /proc/<pid>/exe string");
75         return error;
76     }
77 
78     error = FileSystem::Readlink(link_path, exe_path, llvm::array_lengthof(exe_path));
79     if (!error.Success())
80         return error;
81 
82     const ssize_t len = strlen(exe_path);
83     // If the binary has been deleted, the link name has " (deleted)" appended.
84     // Remove if there.
85     static const ssize_t deleted_len = strlen(" (deleted)");
86     if (len > deleted_len &&
87         !strcmp(exe_path + len - deleted_len, " (deleted)"))
88     {
89         exe_path[len - deleted_len] = 0;
90     }
91 
92     file_spec.SetFile(exe_path, false);
93     return error;
94 }
95 
96 lldb::pid_t HostProcessPosix::GetProcessId() const
97 {
98     return m_process;
99 }
100 
101 bool HostProcessPosix::IsRunning() const
102 {
103     if (m_process == kInvalidPosixProcess)
104         return false;
105 
106     // Send this process the null signal.  If it succeeds the process is running.
107     Error error = Signal(0);
108     return error.Success();
109 }
110