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"
13 #include "lldb/Utility/Stream.h"
16 #include "llvm/ADT/SmallString.h"
17 
18 #include <climits>
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 using namespace lldb_private::repro;
23 
24 ProcessInfo::ProcessInfo()
25  : m_executable(), m_arguments(), m_environment(), m_arch() {}
26 
27 ProcessInfo::ProcessInfo(const char *name, const ArchSpec &arch,
28  lldb::pid_t pid)
29  : m_executable(name), m_arguments(), m_environment(), m_uid(UINT32_MAX),
30  m_gid(UINT32_MAX), m_arch(arch), m_pid(pid) {}
31 
35  m_environment.clear();
36  m_uid = UINT32_MAX;
37  m_gid = UINT32_MAX;
38  m_arch.Clear();
40 }
41 
42 const char *ProcessInfo::GetName() const {
44 }
45 
46 llvm::StringRef ProcessInfo::GetNameAsStringRef() const {
48 }
49 
50 void ProcessInfo::Dump(Stream &s, Platform *platform) const {
51  s << "Executable: " << GetName() << "\n";
52  s << "Triple: ";
54  s << "\n";
55 
56  s << "Arguments:\n";
57  m_arguments.Dump(s);
58 
59  s.Format("Environment:\n{0}", m_environment);
60 }
61 
63  bool add_exe_file_as_first_arg) {
64  if (exe_file) {
65  m_executable = exe_file;
66  if (add_exe_file_as_first_arg) {
67  llvm::SmallString<128> filename;
68  exe_file.GetPath(filename);
69  if (!filename.empty())
71  }
72  } else {
74  }
75 }
76 
77 llvm::StringRef ProcessInfo::GetArg0() const { return m_arg0; }
78 
79 void ProcessInfo::SetArg0(llvm::StringRef arg) { m_arg0 = std::string(arg); }
80 
81 void ProcessInfo::SetArguments(char const **argv,
82  bool first_arg_is_executable) {
84 
85  // Is the first argument the executable?
86  if (first_arg_is_executable) {
87  const char *first_arg = m_arguments.GetArgumentAtIndex(0);
88  if (first_arg) {
89  // Yes the first argument is an executable, set it as the executable in
90  // the launch options. Don't resolve the file path as the path could be a
91  // remote platform path
92  m_executable.SetFile(first_arg, FileSpec::Style::native);
93  }
94  }
95 }
96 
97 void ProcessInfo::SetArguments(const Args &args, bool first_arg_is_executable) {
98  // Copy all arguments
99  m_arguments = args;
100 
101  // Is the first argument the executable?
102  if (first_arg_is_executable) {
103  const char *first_arg = m_arguments.GetArgumentAtIndex(0);
104  if (first_arg) {
105  // Yes the first argument is an executable, set it as the executable in
106  // the launch options. Don't resolve the file path as the path could be a
107  // remote platform path
108  m_executable.SetFile(first_arg, FileSpec::Style::native);
109  }
110  }
111 }
112 
115  s.Printf(" pid = %" PRIu64 "\n", m_pid);
116 
118  s.Printf(" parent = %" PRIu64 "\n", m_parent_pid);
119 
120  if (m_executable) {
121  s.Printf(" name = %s\n", m_executable.GetFilename().GetCString());
122  s.PutCString(" file = ");
124  s.EOL();
125  }
126  const uint32_t argc = m_arguments.GetArgumentCount();
127  if (argc > 0) {
128  for (uint32_t i = 0; i < argc; i++) {
129  const char *arg = m_arguments.GetArgumentAtIndex(i);
130  if (i < 10)
131  s.Printf(" arg[%u] = %s\n", i, arg);
132  else
133  s.Printf("arg[%u] = %s\n", i, arg);
134  }
135  }
136 
137  s.Format("{0}", m_environment);
138 
139  if (m_arch.IsValid()) {
140  s.Printf(" arch = ");
142  s.EOL();
143  }
144 
145  if (UserIDIsValid()) {
146  s.Format(" uid = {0,-5} ({1})\n", GetUserID(),
147  resolver.GetUserName(GetUserID()).getValueOr(""));
148  }
149  if (GroupIDIsValid()) {
150  s.Format(" gid = {0,-5} ({1})\n", GetGroupID(),
151  resolver.GetGroupName(GetGroupID()).getValueOr(""));
152  }
153  if (EffectiveUserIDIsValid()) {
154  s.Format(" euid = {0,-5} ({1})\n", GetEffectiveUserID(),
155  resolver.GetUserName(GetEffectiveUserID()).getValueOr(""));
156  }
157  if (EffectiveGroupIDIsValid()) {
158  s.Format(" egid = {0,-5} ({1})\n", GetEffectiveGroupID(),
159  resolver.GetGroupName(GetEffectiveGroupID()).getValueOr(""));
160  }
161 }
162 
164  bool verbose) {
165  const char *label;
166  if (show_args || verbose)
167  label = "ARGUMENTS";
168  else
169  label = "NAME";
170 
171  if (verbose) {
172  s.Printf("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE "
173  " %s\n",
174  label);
175  s.PutCString(
176  "====== ====== ========== ========== ========== ========== "
177  "============================== ============================\n");
178  } else {
179  s.Printf("PID PARENT USER TRIPLE %s\n",
180  label);
181  s.PutCString("====== ====== ========== ============================== "
182  "============================\n");
183  }
184 }
185 
187  bool show_args, bool verbose) const {
189  s.Printf("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid);
190 
191  StreamString arch_strm;
192  if (m_arch.IsValid())
193  m_arch.DumpTriple(arch_strm.AsRawOstream());
194 
195  auto print = [&](bool (ProcessInstanceInfo::*isValid)() const,
196  uint32_t (ProcessInstanceInfo::*getID)() const,
197  llvm::Optional<llvm::StringRef> (UserIDResolver::*getName)(
199  const char *format = "{0,-10} ";
200  if (!(this->*isValid)()) {
201  s.Format(format, "");
202  return;
203  }
204  uint32_t id = (this->*getID)();
205  if (auto name = (resolver.*getName)(id))
206  s.Format(format, *name);
207  else
208  s.Format(format, id);
209  };
210  if (verbose) {
221 
222  s.Printf("%-30s ", arch_strm.GetData());
223  } else {
227  s.Printf("%-30s ", arch_strm.GetData());
228  }
229 
230  if (verbose || show_args) {
231  s.PutCString(m_arg0);
232  const uint32_t argc = m_arguments.GetArgumentCount();
233  for (uint32_t i = 0; i < argc; i++) {
234  s.PutChar(' ');
236  }
237  } else {
238  s.PutCString(GetName());
239  }
240 
241  s.EOL();
242  }
243 }
244 
246  const ArchSpec &arch_spec) const {
247  return !m_match_info.GetArchitecture().IsValid() ||
249 }
250 
251 bool ProcessInstanceInfoMatch::NameMatches(const char *process_name) const {
253  return true;
254  const char *match_name = m_match_info.GetName();
255  if (!match_name)
256  return true;
257 
258  return lldb_private::NameMatches(process_name, m_name_match_type, match_name);
259 }
260 
262  const ProcessInstanceInfo &proc_info) const {
264  m_match_info.GetProcessID() != proc_info.GetProcessID())
265  return false;
266 
269  return false;
270  return true;
271 }
272 
274  const ProcessInstanceInfo &proc_info) const {
275  if (m_match_info.UserIDIsValid() &&
276  m_match_info.GetUserID() != proc_info.GetUserID())
277  return false;
278 
280  m_match_info.GetGroupID() != proc_info.GetGroupID())
281  return false;
282 
285  return false;
286 
289  return false;
290  return true;
291 }
293  const ProcessInstanceInfo &proc_info) const {
294  return ArchitectureMatches(proc_info.GetArchitecture()) &&
295  ProcessIDsMatch(proc_info) && UserIDsMatch(proc_info) &&
296  NameMatches(proc_info.GetName());
297 }
298 
301  return false;
302 
304  return false;
305 
307  return false;
308 
310  return false;
311 
313  return false;
314 
316  return false;
317 
319  return false;
320 
322  return false;
323 
324  if (m_match_all_users)
325  return false;
326 
327  return true;
328 }
329 
333  m_match_all_users = false;
334 }
335 
336 void llvm::yaml::MappingTraits<ProcessInstanceInfo>::mapping(
337  IO &io, ProcessInstanceInfo &Info) {
338  io.mapRequired("executable", Info.m_executable);
339  io.mapRequired("arg0", Info.m_arg0);
340  io.mapRequired("args", Info.m_arguments);
341  io.mapRequired("arch", Info.m_arch);
342  io.mapRequired("uid", Info.m_uid);
343  io.mapRequired("gid", Info.m_gid);
344  io.mapRequired("pid", Info.m_pid);
345  io.mapRequired("effective-uid", Info.m_euid);
346  io.mapRequired("effective-gid", Info.m_egid);
347  io.mapRequired("parent-pid", Info.m_parent_pid);
348 }
349 
350 
351 llvm::Optional<ProcessInstanceInfoList>
353  static std::unique_ptr<repro::MultiLoader<repro::ProcessInfoProvider>>
355  repro::Reproducer::Instance().GetLoader());
356 
357  if (!loader)
358  return {};
359 
360  llvm::Optional<std::string> nextfile = loader->GetNextFile();
361  if (!nextfile)
362  return {};
363 
364  auto error_or_file = llvm::MemoryBuffer::getFile(*nextfile);
365  if (std::error_code err = error_or_file.getError())
366  return {};
367 
369  llvm::yaml::Input yin((*error_or_file)->getBuffer());
370  yin >> infos;
371 
372  if (auto err = yin.error())
373  return {};
374 
375  return infos;
376 }
lldb_private::Stream::Format
void Format(const char *format, Args &&... args)
Definition: Stream.h:309
lldb_private::ProcessInstanceInfo
Definition: ProcessInfo.h:108
lldb_private::ProcessInstanceInfoMatch::m_name_match_type
NameMatch m_name_match_type
Definition: ProcessInfo.h:210
lldb_private::ProcessInfo::m_uid
uint32_t m_uid
Definition: ProcessInfo.h:98
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::ProcessInfo::ProcessIDIsValid
bool ProcessIDIsValid() const
Definition: ProcessInfo.h:71
lldb_private::ProcessInfo::SetExecutableFile
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:62
LLDB_INVALID_PROCESS_ID
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:93
lldb_private::ProcessInfo::m_executable
FileSpec m_executable
Definition: ProcessInfo.h:92
lldb_private::ProcessInstanceInfo::Clear
void Clear()
Definition: ProcessInfo.h:116
lldb_private::UserIDResolver::GetGroupName
llvm::Optional< llvm::StringRef > GetGroupName(id_t gid)
Definition: UserIDResolver.h:30
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:251
lldb_private::ProcessInfo::m_arch
ArchSpec m_arch
Definition: ProcessInfo.h:100
lldb_private::ProcessInfo::GetGroupID
uint32_t GetGroupID() const
Definition: ProcessInfo.h:51
lldb_private::ProcessInfo::GetUserID
uint32_t GetUserID() const
Definition: ProcessInfo.h:49
lldb_private::repro::GetReplayProcessInstanceInfoList
llvm::Optional< ProcessInstanceInfoList > GetReplayProcessInstanceInfoList()
Definition: ProcessInfo.cpp:352
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:245
lldb_private::repro::MultiLoader::Create
static std::unique_ptr< MultiLoader > Create(Loader *loader)
Definition: ReproducerProvider.h:358
lldb_private::ProcessInstanceInfo::GetEffectiveGroupID
uint32_t GetEffectiveGroupID() const
Definition: ProcessInfo.h:125
lldb_private::ProcessInstanceInfoMatch::Clear
void Clear()
Definition: ProcessInfo.cpp:330
lldb_private::Args::Clear
void Clear()
Clear the arguments.
Definition: Args.cpp:374
lldb_private::ProcessInfo::GetProcessID
lldb::pid_t GetProcessID() const
Definition: ProcessInfo.h:67
lldb_private::Stream
Definition: Stream.h:28
lldb_private::Args
Definition: Args.h:33
lldb_private::ProcessInstanceInfo::EffectiveUserIDIsValid
bool EffectiveUserIDIsValid() const
Definition: ProcessInfo.h:127
lldb_private::ProcessInstanceInfo::m_parent_pid
lldb::pid_t m_parent_pid
Definition: ProcessInfo.h:154
lldb_private::ProcessInfo::UserIDIsValid
bool UserIDIsValid() const
Definition: ProcessInfo.h:53
lldb_private::ProcessInstanceInfoMatch::Matches
bool Matches(const ProcessInstanceInfo &proc_info) const
Definition: ProcessInfo.cpp:292
lldb_private::ArchSpec::IsCompatibleMatch
bool IsCompatibleMatch(const ArchSpec &rhs) const
Compare an ArchSpec to another ArchSpec, requiring a compatible cpu type match between them.
Definition: ArchSpec.cpp:934
lldb_private::ProcessInstanceInfo::DumpTableHeader
static void DumpTableHeader(Stream &s, bool show_args, bool verbose)
Definition: ProcessInfo.cpp:163
lldb_private::ArchSpec::DumpTriple
void DumpTriple(llvm::raw_ostream &s) const
Definition: ArchSpec.cpp:1461
ReproducerProvider.h
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:322
lldb_private::FileSpec
Definition: FileSpec.h:56
ProcessInfo.h
lldb_private::UserIDResolver::GetUserName
llvm::Optional< llvm::StringRef > GetUserName(id_t uid)
Definition: UserIDResolver.h:27
lldb_private::ProcessInfo::m_arguments
Args m_arguments
Definition: ProcessInfo.h:96
lldb_private::ProcessInfo::Clear
void Clear()
Definition: ProcessInfo.cpp:32
lldb_private::ProcessInstanceInfo::GetEffectiveUserID
uint32_t GetEffectiveUserID() const
Definition: ProcessInfo.h:123
lldb_private::ConstString::GetStringRef
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:202
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:353
lldb_private::ProcessInfo::ProcessInfo
ProcessInfo()
Definition: ProcessInfo.cpp:24
lldb_private::ProcessInfo::GroupIDIsValid
bool GroupIDIsValid() const
Definition: ProcessInfo.h:55
lldb_private::ProcessInfo::SetArg0
void SetArg0(llvm::StringRef arg)
Definition: ProcessInfo.cpp:79
lldb_private::StreamString::GetData
const char * GetData() const
Definition: StreamString.h:43
StreamString.h
lldb_private::ProcessInstanceInfoMatch::MatchAllProcesses
bool MatchAllProcesses() const
Definition: ProcessInfo.cpp:299
lldb_private::ProcessInstanceInfo::ParentProcessIDIsValid
bool ParentProcessIDIsValid() const
Definition: ProcessInfo.h:139
lldb_private::ProcessInfo::GetName
const char * GetName() const
Definition: ProcessInfo.cpp:42
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::ProcessInfo::m_pid
lldb::pid_t m_pid
Definition: ProcessInfo.h:101
lldb_private::ProcessInfo::GetNameAsStringRef
llvm::StringRef GetNameAsStringRef() const
Definition: ProcessInfo.cpp:46
lldb_private::ProcessInstanceInfoMatch::m_match_all_users
bool m_match_all_users
Definition: ProcessInfo.h:211
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:39
lldb_private::ProcessInfo::m_environment
Environment m_environment
Definition: ProcessInfo.h:97
lldb_private::ArchSpec::IsValid
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:342
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:99
lldb_private::ProcessInstanceInfo::EffectiveGroupIDIsValid
bool EffectiveGroupIDIsValid() const
Definition: ProcessInfo.h:129
lldb_private::FileSpec::Clear
void Clear()
Clears the object state.
Definition: FileSpec.cpp:261
UserIDResolver.h
lldb_private::UserIDResolver
An abstract interface for things that know how to map numeric user/group IDs into names.
Definition: UserIDResolver.h:22
lldb_private::ProcessInstanceInfo::GetParentProcessID
lldb::pid_t GetParentProcessID() const
Definition: ProcessInfo.h:135
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:259
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:273
uint32_t
lldb_private::ProcessInstanceInfoMatch::m_match_info
ProcessInstanceInfo m_match_info
Definition: ProcessInfo.h:209
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:113
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:31
lldb_private::repro
Definition: SBReproducer.h:15
lldb_private::FileSpec::Dump
void Dump(llvm::raw_ostream &s) const
Dump this object to a Stream.
Definition: FileSpec.cpp:324
lldb_private::ConstString::GetCString
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
lldb_private::ProcessInfo::m_arg0
std::string m_arg0
Definition: ProcessInfo.h:93
lldb_private::Platform
Definition: Platform.h:72
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:186
lldb_private::ProcessInfo::GetArchitecture
ArchSpec & GetArchitecture()
Definition: ProcessInfo.h:61
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:77
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:174
lldb_private::ArchSpec::Clear
void Clear()
Clears the object state.
Definition: ArchSpec.cpp:527
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:348
lldb_private::repro::Reproducer::Instance
static Reproducer & Instance()
Definition: Reproducer.cpp:23
lldb_private::ProcessInfo::Dump
void Dump(Stream &s, Platform *platform) const
Definition: ProcessInfo.cpp:50
lldb_private::Args::GetArgumentCount
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.h:118
lldb_private::ProcessInstanceInfoList
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList
Definition: Host.h:30
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:261
lldb
Definition: SBAddress.h:15
lldb_private::FileSpec::GetFilename
ConstString & GetFilename()
Filename string get accessor.
Definition: FileSpec.cpp:341
lldb_private::ProcessInfo::SetArguments
void SetArguments(const Args &args, bool first_arg_is_executable)
Definition: ProcessInfo.cpp:97
lldb_private::NameMatches
bool NameMatches(llvm::StringRef name, NameMatch match_type, llvm::StringRef match)
Definition: NameMatches.cpp:15