LLDB  mainline
ProcessInfo.cpp
Go to the documentation of this file.
1 //===-- ProcessInfo.cpp ---------------------------------------------------===//
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_arch() {}
24 
25 ProcessInfo::ProcessInfo(const char *name, const ArchSpec &arch,
26  lldb::pid_t pid)
27  : m_executable(name), m_arguments(), m_environment(), m_arch(arch),
28  m_pid(pid) {}
29 
33  m_environment.clear();
34  m_uid = UINT32_MAX;
35  m_gid = UINT32_MAX;
36  m_arch.Clear();
38 }
39 
40 const char *ProcessInfo::GetName() const {
42 }
43 
44 llvm::StringRef ProcessInfo::GetNameAsStringRef() const {
46 }
47 
48 void ProcessInfo::Dump(Stream &s, Platform *platform) const {
49  s << "Executable: " << GetName() << "\n";
50  s << "Triple: ";
52  s << "\n";
53 
54  s << "Arguments:\n";
55  m_arguments.Dump(s);
56 
57  s.Format("Environment:\n{0}", m_environment);
58 }
59 
61  bool add_exe_file_as_first_arg) {
62  if (exe_file) {
63  m_executable = exe_file;
64  if (add_exe_file_as_first_arg) {
65  llvm::SmallString<128> filename;
66  exe_file.GetPath(filename);
67  if (!filename.empty())
69  }
70  } else {
72  }
73 }
74 
75 llvm::StringRef ProcessInfo::GetArg0() const { return m_arg0; }
76 
77 void ProcessInfo::SetArg0(llvm::StringRef arg) { m_arg0 = std::string(arg); }
78 
79 void ProcessInfo::SetArguments(char const **argv,
80  bool first_arg_is_executable) {
82 
83  // Is the first argument the executable?
84  if (first_arg_is_executable) {
85  const char *first_arg = m_arguments.GetArgumentAtIndex(0);
86  if (first_arg) {
87  // Yes the first argument is an executable, set it as the executable in
88  // the launch options. Don't resolve the file path as the path could be a
89  // remote platform path
90  m_executable.SetFile(first_arg, FileSpec::Style::native);
91  }
92  }
93 }
94 
95 void ProcessInfo::SetArguments(const Args &args, bool first_arg_is_executable) {
96  // Copy all arguments
97  m_arguments = args;
98 
99  // Is the first argument the executable?
100  if (first_arg_is_executable) {
101  const char *first_arg = m_arguments.GetArgumentAtIndex(0);
102  if (first_arg) {
103  // Yes the first argument is an executable, set it as the executable in
104  // the launch options. Don't resolve the file path as the path could be a
105  // remote platform path
106  m_executable.SetFile(first_arg, FileSpec::Style::native);
107  }
108  }
109 }
110 
113  s.Printf(" pid = %" PRIu64 "\n", m_pid);
114 
116  s.Printf(" parent = %" PRIu64 "\n", m_parent_pid);
117 
118  if (m_executable) {
119  s.Printf(" name = %s\n", m_executable.GetFilename().GetCString());
120  s.PutCString(" file = ");
122  s.EOL();
123  }
124  const uint32_t argc = m_arguments.GetArgumentCount();
125  if (argc > 0) {
126  for (uint32_t i = 0; i < argc; i++) {
127  const char *arg = m_arguments.GetArgumentAtIndex(i);
128  if (i < 10)
129  s.Printf(" arg[%u] = %s\n", i, arg);
130  else
131  s.Printf("arg[%u] = %s\n", i, arg);
132  }
133  }
134 
135  s.Format("{0}", m_environment);
136 
137  if (m_arch.IsValid()) {
138  s.Printf(" arch = ");
140  s.EOL();
141  }
142 
143  if (UserIDIsValid()) {
144  s.Format(" uid = {0,-5} ({1})\n", GetUserID(),
145  resolver.GetUserName(GetUserID()).value_or(""));
146  }
147  if (GroupIDIsValid()) {
148  s.Format(" gid = {0,-5} ({1})\n", GetGroupID(),
149  resolver.GetGroupName(GetGroupID()).value_or(""));
150  }
151  if (EffectiveUserIDIsValid()) {
152  s.Format(" euid = {0,-5} ({1})\n", GetEffectiveUserID(),
153  resolver.GetUserName(GetEffectiveUserID()).value_or(""));
154  }
155  if (EffectiveGroupIDIsValid()) {
156  s.Format(" egid = {0,-5} ({1})\n", GetEffectiveGroupID(),
157  resolver.GetGroupName(GetEffectiveGroupID()).value_or(""));
158  }
159 }
160 
162  bool verbose) {
163  const char *label;
164  if (show_args || verbose)
165  label = "ARGUMENTS";
166  else
167  label = "NAME";
168 
169  if (verbose) {
170  s.Printf("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE "
171  " %s\n",
172  label);
173  s.PutCString(
174  "====== ====== ========== ========== ========== ========== "
175  "============================== ============================\n");
176  } else {
177  s.Printf("PID PARENT USER TRIPLE %s\n",
178  label);
179  s.PutCString("====== ====== ========== ============================== "
180  "============================\n");
181  }
182 }
183 
185  bool show_args, bool verbose) const {
187  s.Printf("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid);
188 
189  StreamString arch_strm;
190  if (m_arch.IsValid())
191  m_arch.DumpTriple(arch_strm.AsRawOstream());
192 
193  auto print = [&](bool (ProcessInstanceInfo::*isValid)() const,
194  uint32_t (ProcessInstanceInfo::*getID)() const,
195  llvm::Optional<llvm::StringRef> (UserIDResolver::*getName)(
197  const char *format = "{0,-10} ";
198  if (!(this->*isValid)()) {
199  s.Format(format, "");
200  return;
201  }
202  uint32_t id = (this->*getID)();
203  if (auto name = (resolver.*getName)(id))
204  s.Format(format, *name);
205  else
206  s.Format(format, id);
207  };
208  if (verbose) {
219 
220  s.Printf("%-30s ", arch_strm.GetData());
221  } else {
225  s.Printf("%-30s ", arch_strm.GetData());
226  }
227 
228  if (verbose || show_args) {
229  s.PutCString(m_arg0);
230  const uint32_t argc = m_arguments.GetArgumentCount();
231  for (uint32_t i = 0; i < argc; i++) {
232  s.PutChar(' ');
234  }
235  } else {
236  s.PutCString(GetName());
237  }
238 
239  s.EOL();
240  }
241 }
242 
244  const ArchSpec &arch_spec) const {
245  return !m_match_info.GetArchitecture().IsValid() ||
247 }
248 
249 bool ProcessInstanceInfoMatch::NameMatches(const char *process_name) const {
251  return true;
252  const char *match_name = m_match_info.GetName();
253  if (!match_name)
254  return true;
255 
256  return lldb_private::NameMatches(process_name, m_name_match_type, match_name);
257 }
258 
260  const ProcessInstanceInfo &proc_info) const {
262  m_match_info.GetProcessID() != proc_info.GetProcessID())
263  return false;
264 
267  return false;
268  return true;
269 }
270 
272  const ProcessInstanceInfo &proc_info) const {
273  if (m_match_info.UserIDIsValid() &&
274  m_match_info.GetUserID() != proc_info.GetUserID())
275  return false;
276 
278  m_match_info.GetGroupID() != proc_info.GetGroupID())
279  return false;
280 
283  return false;
284 
287  return false;
288  return true;
289 }
291  const ProcessInstanceInfo &proc_info) const {
292  return ArchitectureMatches(proc_info.GetArchitecture()) &&
293  ProcessIDsMatch(proc_info) && UserIDsMatch(proc_info) &&
294  NameMatches(proc_info.GetName());
295 }
296 
299  return false;
300 
302  return false;
303 
305  return false;
306 
308  return false;
309 
311  return false;
312 
314  return false;
315 
317  return false;
318 
320  return false;
321 
322  if (m_match_all_users)
323  return false;
324 
325  return true;
326 }
327 
331  m_match_all_users = false;
332 }
lldb_private::Stream::Format
void Format(const char *format, Args &&... args)
Definition: Stream.h:309
lldb_private::ProcessInstanceInfo
Definition: ProcessInfo.h:106
lldb_private::ProcessInstanceInfoMatch::m_name_match_type
NameMatch m_name_match_type
Definition: ProcessInfo.h:206
lldb_private::ProcessInfo::m_uid
uint32_t m_uid
Definition: ProcessInfo.h:96
lldb_private::ArchSpec
Definition: ArchSpec.h:32
lldb_private::ProcessInfo::ProcessIDIsValid
bool ProcessIDIsValid() const
Definition: ProcessInfo.h:70
lldb_private::ProcessInfo::SetExecutableFile
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:60
LLDB_INVALID_PROCESS_ID
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:81
lldb_private::ProcessInfo::m_executable
FileSpec m_executable
Definition: ProcessInfo.h:90
lldb_private::ProcessInstanceInfo::Clear
void Clear()
Definition: ProcessInfo.h:114
lldb_private::UserIDResolver::GetGroupName
llvm::Optional< llvm::StringRef > GetGroupName(id_t gid)
Definition: UserIDResolver.h:31
lldb_private::ProcessInstanceInfoMatch::NameMatches
bool NameMatches(const char *process_name) const
Return true iff the process name in this object matches process_name.
Definition: ProcessInfo.cpp:249
lldb_private::ProcessInfo::m_arch
ArchSpec m_arch
Definition: ProcessInfo.h:98
lldb_private::ProcessInfo::GetGroupID
uint32_t GetGroupID() const
Definition: ProcessInfo.h:50
lldb_private::ProcessInfo::GetUserID
uint32_t GetUserID() const
Definition: ProcessInfo.h:48
lldb_private::ProcessInstanceInfoMatch::ArchitectureMatches
bool ArchitectureMatches(const ArchSpec &arch_spec) const
Return true iff the architecture in this object matches arch_spec.
Definition: ProcessInfo.cpp:243
lldb_private::ProcessInstanceInfo::GetEffectiveGroupID
uint32_t GetEffectiveGroupID() const
Definition: ProcessInfo.h:123
lldb_private::ProcessInstanceInfoMatch::Clear
void Clear()
Definition: ProcessInfo.cpp:328
lldb_private::Args::Clear
void Clear()
Clear the arguments.
Definition: Args.cpp:379
lldb_private::ProcessInfo::GetProcessID
lldb::pid_t GetProcessID() const
Definition: ProcessInfo.h:66
lldb_private::FileSpec::GetFilename
const ConstString & GetFilename() const
Filename string const get accessor.
Definition: FileSpec.h:239
lldb_private::Stream
Definition: Stream.h:28
lldb_private::Args
Definition: Args.h:33
lldb_private::ArchSpec::IsCompatibleMatch
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
Definition: ArchSpec.h:506
lldb_private::ProcessInstanceInfo::EffectiveUserIDIsValid
bool EffectiveUserIDIsValid() const
Definition: ProcessInfo.h:125
lldb_private::ProcessInstanceInfo::m_parent_pid
lldb::pid_t m_parent_pid
Definition: ProcessInfo.h:151
lldb_private::ProcessInfo::UserIDIsValid
bool UserIDIsValid() const
Definition: ProcessInfo.h:52
lldb_private::ProcessInstanceInfoMatch::Matches
bool Matches(const ProcessInstanceInfo &proc_info) const
Definition: ProcessInfo.cpp:290
lldb_private::ProcessInstanceInfo::DumpTableHeader
static void DumpTableHeader(Stream &s, bool show_args, bool verbose)
Definition: ProcessInfo.cpp:161
lldb_private::ArchSpec::DumpTriple
void DumpTriple(llvm::raw_ostream &s) const
Definition: ArchSpec.cpp:1458
lldb_private::Args::InsertArgumentAtIndex
void InsertArgumentAtIndex(size_t idx, llvm::StringRef arg_str, char quote_char='\0')
Insert the argument value at index idx to arg_str.
Definition: Args.cpp:327
lldb_private::FileSpec
Definition: FileSpec.h:55
ProcessInfo.h
lldb_private::UserIDResolver::GetUserName
llvm::Optional< llvm::StringRef > GetUserName(id_t uid)
Definition: UserIDResolver.h:28
lldb_private::ProcessInfo::m_arguments
Args m_arguments
Definition: ProcessInfo.h:94
lldb_private::ProcessInfo::Clear
void Clear()
Definition: ProcessInfo.cpp:30
bool
lldb_private::ProcessInstanceInfo::GetEffectiveUserID
uint32_t GetEffectiveUserID() const
Definition: ProcessInfo.h:121
lldb_private::ConstString::GetStringRef
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:201
lldb_private::Args::SetArguments
void SetArguments(size_t argc, const char **argv)
Sets the argument vector value, optionally copying all arguments into an internal buffer.
Definition: Args.cpp:358
lldb_private::ProcessInfo::ProcessInfo
ProcessInfo()
Definition: ProcessInfo.cpp:22
lldb_private::ProcessInfo::GroupIDIsValid
bool GroupIDIsValid() const
Definition: ProcessInfo.h:54
lldb_private::ProcessInfo::SetArg0
void SetArg0(llvm::StringRef arg)
Definition: ProcessInfo.cpp:77
lldb_private::StreamString::GetData
const char * GetData() const
Definition: StreamString.h:43
StreamString.h
lldb_private::ProcessInstanceInfoMatch::MatchAllProcesses
bool MatchAllProcesses() const
Definition: ProcessInfo.cpp:297
lldb_private::ProcessInstanceInfo::ParentProcessIDIsValid
bool ParentProcessIDIsValid() const
Definition: ProcessInfo.h:137
lldb_private::ProcessInfo::GetName
const char * GetName() const
Definition: ProcessInfo.cpp:40
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::ProcessInfo::m_pid
lldb::pid_t m_pid
Definition: ProcessInfo.h:99
lldb_private::ProcessInfo::GetNameAsStringRef
llvm::StringRef GetNameAsStringRef() const
Definition: ProcessInfo.cpp:44
lldb_private::ProcessInstanceInfoMatch::m_match_all_users
bool m_match_all_users
Definition: ProcessInfo.h:207
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::ProcessInfo::m_environment
Environment m_environment
Definition: ProcessInfo.h:95
lldb_private::ArchSpec::IsValid
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:352
lldb_private::Stream::PutChar
size_t PutChar(char ch)
Definition: Stream.cpp:104
lldb_private::ProcessInfo::m_gid
uint32_t m_gid
Definition: ProcessInfo.h:97
lldb_private::ProcessInstanceInfo::EffectiveGroupIDIsValid
bool EffectiveGroupIDIsValid() const
Definition: ProcessInfo.h:127
lldb_private::FileSpec::Clear
void Clear()
Clears the object state.
Definition: FileSpec.cpp:257
UserIDResolver.h
lldb_private::UserIDResolver
An abstract interface for things that know how to map numeric user/group IDs into names.
Definition: UserIDResolver.h:23
lldb_private::ProcessInstanceInfo::GetParentProcessID
lldb::pid_t GetParentProcessID() const
Definition: ProcessInfo.h:133
lldb_private::Args::GetArgumentAtIndex
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Definition: Args.cpp:264
lldb_private::ProcessInstanceInfoMatch::UserIDsMatch
bool UserIDsMatch(const ProcessInstanceInfo &proc_info) const
Return true iff the (both effective and real) user and group IDs in this object match the ones in pro...
Definition: ProcessInfo.cpp:271
uint32_t
lldb_private::ProcessInstanceInfoMatch::m_match_info
ProcessInstanceInfo m_match_info
Definition: ProcessInfo.h:205
id
void * id
Definition: PlatformiOSSimulatorCoreSimulatorSupport.h:20
lldb::pid_t
uint64_t pid_t
Definition: lldb-types.h:85
lldb_private::Stream::EOL
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
lldb_private::NameMatch::Ignore
@ Ignore
lldb_private::ProcessInstanceInfo::Dump
void Dump(Stream &s, UserIDResolver &resolver) const
Definition: ProcessInfo.cpp:111
ArchSpec.h
lldb_private::Stream::AsRawOstream
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:19
lldb_private::FileSpec::Dump
void Dump(llvm::raw_ostream &s) const
Dump this object to a Stream.
Definition: FileSpec.cpp:322
lldb_private::ConstString::GetCString
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:215
lldb_private::ProcessInfo::m_arg0
std::string m_arg0
Definition: ProcessInfo.h:91
lldb_private::Platform
Definition: Platform.h:71
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::ProcessInstanceInfo::DumpAsTableRow
void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args, bool verbose) const
Definition: ProcessInfo.cpp:184
lldb_private::ProcessInfo::GetArchitecture
ArchSpec & GetArchitecture()
Definition: ProcessInfo.h:60
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ProcessInfo::GetArg0
llvm::StringRef GetArg0() const
Definition: ProcessInfo.cpp:75
Stream.h
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb_private::FileSpec::SetFile
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:172
lldb_private::ArchSpec::Clear
void Clear()
Clears the object state.
Definition: ArchSpec.cpp:524
lldb_private::Args::Dump
void Dump(Stream &s, const char *label_name="argv") const
Dump all entries to the stream s using label label_name.
Definition: Args.cpp:199
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:364
lldb_private::ProcessInfo::Dump
void Dump(Stream &s, Platform *platform) const
Definition: ProcessInfo.cpp:48
lldb_private::Args::GetArgumentCount
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.h:116
lldb_private::ProcessInstanceInfoMatch::ProcessIDsMatch
bool ProcessIDsMatch(const ProcessInstanceInfo &proc_info) const
Return true iff the process ID and parent process IDs in this object match the ones in proc_info.
Definition: ProcessInfo.cpp:259
lldb
Definition: SBAddress.h:15
lldb_private::ProcessInfo::SetArguments
void SetArguments(const Args &args, bool first_arg_is_executable)
Definition: ProcessInfo.cpp:95
lldb_private::NameMatches
bool NameMatches(llvm::StringRef name, NameMatch match_type, llvm::StringRef match)
Definition: NameMatches.cpp:15