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