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_uid(UINT32_MAX),
26  m_gid(UINT32_MAX), m_arch(), m_pid(LLDB_INVALID_PROCESS_ID) {}
27 
28 ProcessInfo::ProcessInfo(const char *name, const ArchSpec &arch,
29  lldb::pid_t pid)
30  : m_executable(name), m_arguments(), m_environment(), m_uid(UINT32_MAX),
31  m_gid(UINT32_MAX), m_arch(arch), m_pid(pid) {}
32 
36  m_environment.clear();
37  m_uid = UINT32_MAX;
38  m_gid = UINT32_MAX;
39  m_arch.Clear();
41 }
42 
43 const char *ProcessInfo::GetName() const {
45 }
46 
47 llvm::StringRef ProcessInfo::GetNameAsStringRef() const {
49 }
50 
51 void ProcessInfo::Dump(Stream &s, Platform *platform) const {
52  s << "Executable: " << GetName() << "\n";
53  s << "Triple: ";
55  s << "\n";
56 
57  s << "Arguments:\n";
58  m_arguments.Dump(s);
59 
60  s.Format("Environment:\n{0}", m_environment);
61 }
62 
64  bool add_exe_file_as_first_arg) {
65  if (exe_file) {
66  m_executable = exe_file;
67  if (add_exe_file_as_first_arg) {
68  llvm::SmallString<128> filename;
69  exe_file.GetPath(filename);
70  if (!filename.empty())
72  }
73  } else {
75  }
76 }
77 
78 llvm::StringRef ProcessInfo::GetArg0() const { return m_arg0; }
79 
80 void ProcessInfo::SetArg0(llvm::StringRef arg) { m_arg0 = std::string(arg); }
81 
82 void ProcessInfo::SetArguments(char const **argv,
83  bool first_arg_is_executable) {
85 
86  // Is the first argument the executable?
87  if (first_arg_is_executable) {
88  const char *first_arg = m_arguments.GetArgumentAtIndex(0);
89  if (first_arg) {
90  // Yes the first argument is an executable, set it as the executable in
91  // the launch options. Don't resolve the file path as the path could be a
92  // remote platform path
93  m_executable.SetFile(first_arg, FileSpec::Style::native);
94  }
95  }
96 }
97 
98 void ProcessInfo::SetArguments(const Args &args, bool first_arg_is_executable) {
99  // Copy all arguments
100  m_arguments = args;
101 
102  // Is the first argument the executable?
103  if (first_arg_is_executable) {
104  const char *first_arg = m_arguments.GetArgumentAtIndex(0);
105  if (first_arg) {
106  // Yes the first argument is an executable, set it as the executable in
107  // the launch options. Don't resolve the file path as the path could be a
108  // remote platform path
109  m_executable.SetFile(first_arg, FileSpec::Style::native);
110  }
111  }
112 }
113 
116  s.Printf(" pid = %" PRIu64 "\n", m_pid);
117 
119  s.Printf(" parent = %" PRIu64 "\n", m_parent_pid);
120 
121  if (m_executable) {
122  s.Printf(" name = %s\n", m_executable.GetFilename().GetCString());
123  s.PutCString(" file = ");
125  s.EOL();
126  }
127  const uint32_t argc = m_arguments.GetArgumentCount();
128  if (argc > 0) {
129  for (uint32_t i = 0; i < argc; i++) {
130  const char *arg = m_arguments.GetArgumentAtIndex(i);
131  if (i < 10)
132  s.Printf(" arg[%u] = %s\n", i, arg);
133  else
134  s.Printf("arg[%u] = %s\n", i, arg);
135  }
136  }
137 
138  s.Format("{0}", m_environment);
139 
140  if (m_arch.IsValid()) {
141  s.Printf(" arch = ");
143  s.EOL();
144  }
145 
146  if (UserIDIsValid()) {
147  s.Format(" uid = {0,-5} ({1})\n", GetUserID(),
148  resolver.GetUserName(GetUserID()).getValueOr(""));
149  }
150  if (GroupIDIsValid()) {
151  s.Format(" gid = {0,-5} ({1})\n", GetGroupID(),
152  resolver.GetGroupName(GetGroupID()).getValueOr(""));
153  }
154  if (EffectiveUserIDIsValid()) {
155  s.Format(" euid = {0,-5} ({1})\n", GetEffectiveUserID(),
156  resolver.GetUserName(GetEffectiveUserID()).getValueOr(""));
157  }
158  if (EffectiveGroupIDIsValid()) {
159  s.Format(" egid = {0,-5} ({1})\n", GetEffectiveGroupID(),
160  resolver.GetGroupName(GetEffectiveGroupID()).getValueOr(""));
161  }
162 }
163 
165  bool verbose) {
166  const char *label;
167  if (show_args || verbose)
168  label = "ARGUMENTS";
169  else
170  label = "NAME";
171 
172  if (verbose) {
173  s.Printf("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE "
174  " %s\n",
175  label);
176  s.PutCString(
177  "====== ====== ========== ========== ========== ========== "
178  "============================== ============================\n");
179  } else {
180  s.Printf("PID PARENT USER TRIPLE %s\n",
181  label);
182  s.PutCString("====== ====== ========== ============================== "
183  "============================\n");
184  }
185 }
186 
188  bool show_args, bool verbose) const {
190  s.Printf("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid);
191 
192  StreamString arch_strm;
193  if (m_arch.IsValid())
194  m_arch.DumpTriple(arch_strm.AsRawOstream());
195 
196  auto print = [&](bool (ProcessInstanceInfo::*isValid)() const,
197  uint32_t (ProcessInstanceInfo::*getID)() const,
198  llvm::Optional<llvm::StringRef> (UserIDResolver::*getName)(
200  const char *format = "{0,-10} ";
201  if (!(this->*isValid)()) {
202  s.Format(format, "");
203  return;
204  }
205  uint32_t id = (this->*getID)();
206  if (auto name = (resolver.*getName)(id))
207  s.Format(format, *name);
208  else
209  s.Format(format, id);
210  };
211  if (verbose) {
222 
223  s.Printf("%-30s ", arch_strm.GetData());
224  } else {
228  s.Printf("%-30s ", arch_strm.GetData());
229  }
230 
231  if (verbose || show_args) {
232  s.PutCString(m_arg0);
233  const uint32_t argc = m_arguments.GetArgumentCount();
234  for (uint32_t i = 0; i < argc; i++) {
235  s.PutChar(' ');
237  }
238  } else {
239  s.PutCString(GetName());
240  }
241 
242  s.EOL();
243  }
244 }
245 
247  const ArchSpec &arch_spec) const {
248  return !m_match_info.GetArchitecture().IsValid() ||
250 }
251 
252 bool ProcessInstanceInfoMatch::NameMatches(const char *process_name) const {
254  return true;
255  const char *match_name = m_match_info.GetName();
256  if (!match_name)
257  return true;
258 
259  return lldb_private::NameMatches(process_name, m_name_match_type, match_name);
260 }
261 
263  const ProcessInstanceInfo &proc_info) const {
265  m_match_info.GetProcessID() != proc_info.GetProcessID())
266  return false;
267 
270  return false;
271  return true;
272 }
273 
275  const ProcessInstanceInfo &proc_info) const {
276  if (m_match_info.UserIDIsValid() &&
277  m_match_info.GetUserID() != proc_info.GetUserID())
278  return false;
279 
281  m_match_info.GetGroupID() != proc_info.GetGroupID())
282  return false;
283 
286  return false;
287 
290  return false;
291  return true;
292 }
294  const ProcessInstanceInfo &proc_info) const {
295  return ArchitectureMatches(proc_info.GetArchitecture()) &&
296  ProcessIDsMatch(proc_info) && UserIDsMatch(proc_info) &&
297  NameMatches(proc_info.GetName());
298 }
299 
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 
323  return false;
324 
325  if (m_match_all_users)
326  return false;
327 
328  return true;
329 }
330 
334  m_match_all_users = false;
335 }
336 
337 void llvm::yaml::MappingTraits<ProcessInstanceInfo>::mapping(
338  IO &io, ProcessInstanceInfo &Info) {
339  io.mapRequired("executable", Info.m_executable);
340  io.mapRequired("arg0", Info.m_arg0);
341  io.mapRequired("args", Info.m_arguments);
342  io.mapRequired("arch", Info.m_arch);
343  io.mapRequired("uid", Info.m_uid);
344  io.mapRequired("gid", Info.m_gid);
345  io.mapRequired("pid", Info.m_pid);
346  io.mapRequired("effective-uid", Info.m_euid);
347  io.mapRequired("effective-gid", Info.m_egid);
348  io.mapRequired("parent-pid", Info.m_parent_pid);
349 }
350 
351 
352 llvm::Optional<ProcessInstanceInfoList>
354  static std::unique_ptr<repro::MultiLoader<repro::ProcessInfoProvider>>
356  repro::Reproducer::Instance().GetLoader());
357 
358  if (!loader)
359  return {};
360 
361  llvm::Optional<std::string> nextfile = loader->GetNextFile();
362  if (!nextfile)
363  return {};
364 
365  auto error_or_file = llvm::MemoryBuffer::getFile(*nextfile);
366  if (std::error_code err = error_or_file.getError())
367  return {};
368 
370  llvm::yaml::Input yin((*error_or_file)->getBuffer());
371  yin >> infos;
372 
373  if (auto err = yin.error())
374  return {};
375 
376  return infos;
377 }
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:214
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:63
LLDB_INVALID_PROCESS_ID
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:92
lldb_private::ProcessInfo::m_executable
FileSpec m_executable
Definition: ProcessInfo.h:92
lldb_private::ProcessInstanceInfo::Clear
void Clear()
Definition: ProcessInfo.h:118
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:252
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:353
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:246
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:127
lldb_private::ProcessInstanceInfoMatch::Clear
void Clear()
Definition: ProcessInfo.cpp:331
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:129
lldb_private::ProcessInstanceInfo::m_parent_pid
lldb::pid_t m_parent_pid
Definition: ProcessInfo.h:156
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:293
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:164
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:33
lldb_private::ProcessInstanceInfo::GetEffectiveUserID
uint32_t GetEffectiveUserID() const
Definition: ProcessInfo.h:125
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:80
lldb_private::StreamString::GetData
const char * GetData() const
Definition: StreamString.h:43
StreamString.h
lldb_private::ProcessInstanceInfoMatch::MatchAllProcesses
bool MatchAllProcesses() const
Definition: ProcessInfo.cpp:300
lldb_private::ProcessInstanceInfo::ParentProcessIDIsValid
bool ParentProcessIDIsValid() const
Definition: ProcessInfo.h:141
lldb_private::ProcessInfo::GetName
const char * GetName() const
Definition: ProcessInfo.cpp:43
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:47
lldb_private::ProcessInstanceInfoMatch::m_match_all_users
bool m_match_all_users
Definition: ProcessInfo.h:215
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
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:131
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:137
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:274
uint32_t
lldb_private::ProcessInstanceInfoMatch::m_match_info
ProcessInstanceInfo m_match_info
Definition: ProcessInfo.h:213
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:114
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:187
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:78
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:51
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:262
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:98
lldb_private::NameMatches
bool NameMatches(llvm::StringRef name, NameMatch match_type, llvm::StringRef match)
Definition: NameMatches.cpp:15