1 //===-- ProcessInfo.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/Target/ProcessInfo.h"
11 
12 // C Includes
13 // C++ Includes
14 #include <climits>
15 
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/Host/PosixApi.h"
19 #include "lldb/Utility/Stream.h"
20 
21 #include "llvm/ADT/SmallString.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 ProcessInfo::ProcessInfo()
27     : m_executable(), m_arguments(), m_environment(), m_uid(UINT32_MAX),
28       m_gid(UINT32_MAX), m_arch(), m_pid(LLDB_INVALID_PROCESS_ID) {}
29 
30 ProcessInfo::ProcessInfo(const char *name, const ArchSpec &arch,
31                          lldb::pid_t pid)
32     : m_executable(name, false), m_arguments(), m_environment(),
33       m_uid(UINT32_MAX), m_gid(UINT32_MAX), m_arch(arch), m_pid(pid) {}
34 
35 void ProcessInfo::Clear() {
36   m_executable.Clear();
37   m_arguments.Clear();
38   m_environment.clear();
39   m_uid = UINT32_MAX;
40   m_gid = UINT32_MAX;
41   m_arch.Clear();
42   m_pid = LLDB_INVALID_PROCESS_ID;
43 }
44 
45 const char *ProcessInfo::GetName() const {
46   return m_executable.GetFilename().GetCString();
47 }
48 
49 size_t ProcessInfo::GetNameLength() const {
50   return m_executable.GetFilename().GetLength();
51 }
52 
53 void ProcessInfo::Dump(Stream &s, Platform *platform) const {
54   s << "Executable: " << GetName() << "\n";
55   s << "Triple: ";
56   m_arch.DumpTriple(s);
57   s << "\n";
58 
59   s << "Arguments:\n";
60   m_arguments.Dump(s);
61 
62   s.Format("Environment:\n{0}", m_environment);
63 }
64 
65 void ProcessInfo::SetExecutableFile(const FileSpec &exe_file,
66                                     bool add_exe_file_as_first_arg) {
67   if (exe_file) {
68     m_executable = exe_file;
69     if (add_exe_file_as_first_arg) {
70       llvm::SmallString<PATH_MAX> filename;
71       exe_file.GetPath(filename);
72       if (!filename.empty())
73         m_arguments.InsertArgumentAtIndex(0, filename);
74     }
75   } else {
76     m_executable.Clear();
77   }
78 }
79 
80 llvm::StringRef ProcessInfo::GetArg0() const {
81   return m_arg0;
82 }
83 
84 void ProcessInfo::SetArg0(llvm::StringRef arg) {
85   m_arg0 = arg;
86 }
87 
88 void ProcessInfo::SetArguments(char const **argv,
89                                bool first_arg_is_executable) {
90   m_arguments.SetArguments(argv);
91 
92   // Is the first argument the executable?
93   if (first_arg_is_executable) {
94     const char *first_arg = m_arguments.GetArgumentAtIndex(0);
95     if (first_arg) {
96       // Yes the first argument is an executable, set it as the executable in
97       // the launch options. Don't resolve the file path as the path could be a
98       // remote platform path
99       const bool resolve = false;
100       m_executable.SetFile(first_arg, resolve, FileSpec::Style::native);
101     }
102   }
103 }
104 
105 void ProcessInfo::SetArguments(const Args &args, bool first_arg_is_executable) {
106   // Copy all arguments
107   m_arguments = args;
108 
109   // Is the first argument the executable?
110   if (first_arg_is_executable) {
111     const char *first_arg = m_arguments.GetArgumentAtIndex(0);
112     if (first_arg) {
113       // Yes the first argument is an executable, set it as the executable in
114       // the launch options. Don't resolve the file path as the path could be a
115       // remote platform path
116       const bool resolve = false;
117       m_executable.SetFile(first_arg, resolve, FileSpec::Style::native);
118     }
119   }
120 }
121