LLDB  mainline
ProcessInfo.cpp
Go to the documentation of this file.
1 //===-- ProcessInstance.cpp -------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
11 #include "lldb/Utility/ArchSpec.h"
12 #include "lldb/Utility/Stream.h"
15 #include "llvm/ADT/SmallString.h"
16 
17 #include <climits>
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
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 
26 ProcessInfo::ProcessInfo(const char *name, const ArchSpec &arch,
27  lldb::pid_t pid)
29  m_gid(UINT32_MAX), m_arch(arch), m_pid(pid) {}
30 
34  m_environment.clear();
35  m_uid = UINT32_MAX;
36  m_gid = UINT32_MAX;
37  m_arch.Clear();
39 }
40 
41 const char *ProcessInfo::GetName() const {
43 }
44 
47 }
48 
49 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 
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())
70  }
71  } else {
73  }
74 }
75 
76 llvm::StringRef ProcessInfo::GetArg0() const { return m_arg0; }
77 
78 void ProcessInfo::SetArg0(llvm::StringRef arg) { m_arg0 = arg; }
79 
80 void ProcessInfo::SetArguments(char const **argv,
81  bool first_arg_is_executable) {
83 
84  // Is the first argument the executable?
85  if (first_arg_is_executable) {
86  const char *first_arg = m_arguments.GetArgumentAtIndex(0);
87  if (first_arg) {
88  // Yes the first argument is an executable, set it as the executable in
89  // the launch options. Don't resolve the file path as the path could be a
90  // remote platform path
91  m_executable.SetFile(first_arg, FileSpec::Style::native);
92  }
93  }
94 }
95 
96 void ProcessInfo::SetArguments(const Args &args, bool first_arg_is_executable) {
97  // Copy all arguments
98  m_arguments = args;
99 
100  // Is the first argument the executable?
101  if (first_arg_is_executable) {
102  const char *first_arg = m_arguments.GetArgumentAtIndex(0);
103  if (first_arg) {
104  // Yes the first argument is an executable, set it as the executable in
105  // the launch options. Don't resolve the file path as the path could be a
106  // remote platform path
107  m_executable.SetFile(first_arg, FileSpec::Style::native);
108  }
109  }
110 }
111 
114  s.Printf(" pid = %" PRIu64 "\n", m_pid);
115 
116  if (m_parent_pid != LLDB_INVALID_PROCESS_ID)
117  s.Printf(" parent = %" PRIu64 "\n", m_parent_pid);
118 
119  if (m_executable) {
120  s.Printf(" name = %s\n", m_executable.GetFilename().GetCString());
121  s.PutCString(" file = ");
122  m_executable.Dump(&s);
123  s.EOL();
124  }
125  const uint32_t argc = m_arguments.GetArgumentCount();
126  if (argc > 0) {
127  for (uint32_t i = 0; i < argc; i++) {
128  const char *arg = m_arguments.GetArgumentAtIndex(i);
129  if (i < 10)
130  s.Printf(" arg[%u] = %s\n", i, arg);
131  else
132  s.Printf("arg[%u] = %s\n", i, arg);
133  }
134  }
135 
136  s.Format("{0}", m_environment);
137 
138  if (m_arch.IsValid()) {
139  s.Printf(" arch = ");
140  m_arch.DumpTriple(s);
141  s.EOL();
142  }
143 
144  if (UserIDIsValid()) {
145  s.Format(" uid = {0,-5} ({1})\n", GetUserID(),
146  resolver.GetUserName(GetUserID()).getValueOr(""));
147  }
148  if (GroupIDIsValid()) {
149  s.Format(" gid = {0,-5} ({1})\n", GetGroupID(),
150  resolver.GetGroupName(GetGroupID()).getValueOr(""));
151  }
152  if (EffectiveUserIDIsValid()) {
153  s.Format(" euid = {0,-5} ({1})\n", GetEffectiveUserID(),
154  resolver.GetUserName(GetEffectiveUserID()).getValueOr(""));
155  }
156  if (EffectiveGroupIDIsValid()) {
157  s.Format(" egid = {0,-5} ({1})\n", GetEffectiveGroupID(),
158  resolver.GetGroupName(GetEffectiveGroupID()).getValueOr(""));
159  }
160 }
161 
163  bool verbose) {
164  const char *label;
165  if (show_args || verbose)
166  label = "ARGUMENTS";
167  else
168  label = "NAME";
169 
170  if (verbose) {
171  s.Printf("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE "
172  " %s\n",
173  label);
174  s.PutCString("====== ====== ========== ========== ========== ========== "
175  "======================== ============================\n");
176  } else {
177  s.Printf("PID PARENT USER TRIPLE %s\n", label);
178  s.PutCString("====== ====== ========== ======================== "
179  "============================\n");
180  }
181 }
182 
184  bool show_args, bool verbose) const {
186  s.Printf("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid);
187 
188  StreamString arch_strm;
189  if (m_arch.IsValid())
190  m_arch.DumpTriple(arch_strm);
191 
192  auto print = [&](UserIDResolver::id_t id,
193  llvm::Optional<llvm::StringRef> (UserIDResolver::*get)(
194  UserIDResolver::id_t id)) {
195  if (auto name = (resolver.*get)(id))
196  s.Format("{0,-10} ", *name);
197  else
198  s.Format("{0,-10} ", id);
199  };
200  if (verbose) {
203  print(m_euid, &UserIDResolver::GetUserName);
204  print(m_egid, &UserIDResolver::GetGroupName);
205 
206  s.Printf("%-24s ", arch_strm.GetData());
207  } else {
208  print(m_euid, &UserIDResolver::GetUserName);
209  s.Printf(" %-24s ", arch_strm.GetData());
210  }
211 
212  if (verbose || show_args) {
213  const uint32_t argc = m_arguments.GetArgumentCount();
214  if (argc > 0) {
215  for (uint32_t i = 0; i < argc; i++) {
216  if (i > 0)
217  s.PutChar(' ');
219  }
220  }
221  } else {
222  s.PutCString(GetName());
223  }
224 
225  s.EOL();
226  }
227 }
228 
229 bool ProcessInstanceInfoMatch::NameMatches(const char *process_name) const {
230  if (m_name_match_type == NameMatch::Ignore || process_name == nullptr)
231  return true;
232  const char *match_name = m_match_info.GetName();
233  if (!match_name)
234  return true;
235 
236  return lldb_private::NameMatches(process_name, m_name_match_type, match_name);
237 }
238 
240  const ProcessInstanceInfo &proc_info) const {
241  if (!NameMatches(proc_info.GetName()))
242  return false;
243 
244  if (m_match_info.ProcessIDIsValid() &&
245  m_match_info.GetProcessID() != proc_info.GetProcessID())
246  return false;
247 
248  if (m_match_info.ParentProcessIDIsValid() &&
249  m_match_info.GetParentProcessID() != proc_info.GetParentProcessID())
250  return false;
251 
252  if (m_match_info.UserIDIsValid() &&
253  m_match_info.GetUserID() != proc_info.GetUserID())
254  return false;
255 
256  if (m_match_info.GroupIDIsValid() &&
257  m_match_info.GetGroupID() != proc_info.GetGroupID())
258  return false;
259 
260  if (m_match_info.EffectiveUserIDIsValid() &&
261  m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID())
262  return false;
263 
264  if (m_match_info.EffectiveGroupIDIsValid() &&
265  m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID())
266  return false;
267 
268  if (m_match_info.GetArchitecture().IsValid() &&
269  !m_match_info.GetArchitecture().IsCompatibleMatch(
270  proc_info.GetArchitecture()))
271  return false;
272  return true;
273 }
274 
276  if (m_name_match_type != NameMatch::Ignore)
277  return false;
278 
279  if (m_match_info.ProcessIDIsValid())
280  return false;
281 
282  if (m_match_info.ParentProcessIDIsValid())
283  return false;
284 
285  if (m_match_info.UserIDIsValid())
286  return false;
287 
288  if (m_match_info.GroupIDIsValid())
289  return false;
290 
291  if (m_match_info.EffectiveUserIDIsValid())
292  return false;
293 
294  if (m_match_info.EffectiveGroupIDIsValid())
295  return false;
296 
297  if (m_match_info.GetArchitecture().IsValid())
298  return false;
299 
300  if (m_match_all_users)
301  return false;
302 
303  return true;
304 }
305 
307  m_match_info.Clear();
308  m_name_match_type = NameMatch::Ignore;
309  m_match_all_users = false;
310 }
ConstString & GetFilename()
Filename string get accessor.
Definition: FileSpec.cpp:369
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
llvm::StringRef GetArg0() const
Definition: ProcessInfo.cpp:76
bool NameMatches(llvm::StringRef name, NameMatch match_type, llvm::StringRef match)
Definition: NameMatches.cpp:15
A command line argument class.
Definition: Args.h:32
uint32_t GetEffectiveGroupID() const
Definition: ProcessInfo.h:127
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
size_t GetNameLength() const
Definition: ProcessInfo.cpp:45
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:61
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
uint32_t GetUserID() const
Definition: ProcessInfo.h:50
void Dump(Stream &s, const char *label_name="argv") const
Dump all entries to the stream s using label label_name.
Definition: Args.cpp:194
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:92
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.cpp:254
A file utility class.
Definition: FileSpec.h:55
An architecture specification class.
Definition: ArchSpec.h:32
llvm::Optional< llvm::StringRef > GetUserName(id_t uid)
static void DumpTableHeader(Stream &s, bool show_args, bool verbose)
void Clear()
Clears the object state.
Definition: FileSpec.cpp:285
void Dump(Stream *s) const
Dump this object to a Stream.
Definition: FileSpec.cpp:350
void SetArguments(size_t argc, const char **argv)
Sets the argument vector value, optionally copying all arguments into an internal buffer...
Definition: Args.cpp:363
const char * GetData() const
Definition: StreamString.h:43
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:329
bool Matches(const ProcessInstanceInfo &proc_info) const
void SetArguments(const Args &args, bool first_arg_is_executable)
Definition: ProcessInfo.cpp:96
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx. ...
Definition: Args.cpp:256
void Dump(Stream &s, UserIDResolver &resolver) const
const char * GetName() const
Definition: ProcessInfo.cpp:41
void InsertArgumentAtIndex(size_t idx, llvm::StringRef arg_str, char quote_char='\0')
Insert the argument value at index idx to arg_cstr.
Definition: Args.cpp:325
void DumpTriple(Stream &s) const
Definition: ArchSpec.cpp:1452
void Clear()
Clears the object state.
Definition: ArchSpec.cpp:580
uint32_t GetEffectiveUserID() const
Definition: ProcessInfo.h:125
#define UINT32_MAX
Definition: lldb-defines.h:31
void Format(const char *format, Args &&... args)
Definition: Stream.h:422
void SetArg0(llvm::StringRef arg)
Definition: ProcessInfo.cpp:78
size_t GetLength() const
Get the length in bytes of string value.
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:127
uint32_t GetGroupID() const
Definition: ProcessInfo.h:52
A plug-in interface definition class for debug platform that includes many platform abilities such as...
Definition: Platform.h:67
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
ArchSpec & GetArchitecture()
Definition: ProcessInfo.h:62
llvm::Optional< llvm::StringRef > GetGroupName(id_t gid)
void Dump(Stream &s, Platform *platform) const
Definition: ProcessInfo.cpp:49
size_t PutChar(char ch)
Definition: Stream.cpp:103
lldb::pid_t GetParentProcessID() const
Definition: ProcessInfo.h:137
An abstract interface for things that know how to map numeric user/group IDs into names...
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:247
Definition: SBAddress.h:15
uint64_t pid_t
Definition: lldb-types.h:85
void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args, bool verbose) const
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:198
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:376
bool UserIDIsValid() const
Definition: ProcessInfo.h:54
lldb::pid_t GetProcessID() const
Definition: ProcessInfo.h:68
bool NameMatches(const char *process_name) const
bool GroupIDIsValid() const
Definition: ProcessInfo.h:56