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
12#include "lldb/Utility/Stream.h"
15#include "llvm/ADT/SmallString.h"
16
17#include <climits>
18#include <optional>
19
20using namespace lldb;
21using namespace lldb_private;
22
24 : m_executable(), m_arguments(), m_environment(), m_arch() {}
25
26ProcessInfo::ProcessInfo(const char *name, const ArchSpec &arch,
27 lldb::pid_t pid)
28 : m_executable(name), m_arguments(), m_environment(), m_arch(arch),
29 m_pid(pid) {}
30
34 m_environment.clear();
37 m_arch.Clear();
39}
40
41const char *ProcessInfo::GetName() const {
43}
44
45llvm::StringRef ProcessInfo::GetNameAsStringRef() const {
47}
48
49void ProcessInfo::Dump(Stream &s, Platform *platform) const {
50 s << "Executable: " << GetName() << "\n";
51 s << "Triple: ";
53 s << "\n";
54
55 s << "Arguments:\n";
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
76llvm::StringRef ProcessInfo::GetArg0() const { return m_arg0; }
77
78void ProcessInfo::SetArg0(llvm::StringRef arg) { m_arg0 = std::string(arg); }
79
80void 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
96void 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
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 = ");
123 s.EOL();
124 }
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 = ");
141 s.EOL();
142 }
143
144 if (UserIDIsValid()) {
145 s.Format(" uid = {0,-5} ({1})\n", GetUserID(),
146 resolver.GetUserName(GetUserID()).value_or(""));
147 }
148 if (GroupIDIsValid()) {
149 s.Format(" gid = {0,-5} ({1})\n", GetGroupID(),
150 resolver.GetGroupName(GetGroupID()).value_or(""));
151 }
153 s.Format(" euid = {0,-5} ({1})\n", GetEffectiveUserID(),
154 resolver.GetUserName(GetEffectiveUserID()).value_or(""));
155 }
157 s.Format(" egid = {0,-5} ({1})\n", GetEffectiveGroupID(),
158 resolver.GetGroupName(GetEffectiveGroupID()).value_or(""));
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 "====== ====== ========== ========== ========== ========== "
176 "============================== ============================\n");
177 } else {
178 s.Printf("PID PARENT USER TRIPLE %s\n",
179 label);
180 s.PutCString("====== ====== ========== ============================== "
181 "============================\n");
182 }
183}
184
186 bool show_args, bool verbose) const {
188 s.Printf("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid);
189
190 StreamString arch_strm;
191 if (m_arch.IsValid())
192 m_arch.DumpTriple(arch_strm.AsRawOstream());
193
194 auto print = [&](bool (ProcessInstanceInfo::*isValid)() const,
195 uint32_t (ProcessInstanceInfo::*getID)() const,
196 std::optional<llvm::StringRef> (UserIDResolver::*getName)(
198 const char *format = "{0,-10} ";
199 if (!(this->*isValid)()) {
200 s.Format(format, "");
201 return;
202 }
203 uint32_t id = (this->*getID)();
204 if (auto name = (resolver.*getName)(id))
205 s.Format(format, *name);
206 else
207 s.Format(format, id);
208 };
209 if (verbose) {
220
221 s.Printf("%-30s ", arch_strm.GetData());
222 } else {
226 s.Printf("%-30s ", arch_strm.GetData());
227 }
228
229 if (verbose || show_args) {
232 for (uint32_t i = 0; i < argc; i++) {
233 s.PutChar(' ');
235 }
236 } else {
237 s.PutCString(GetName());
238 }
239
240 s.EOL();
241 }
242}
243
245 const ArchSpec &arch_spec) const {
248}
249
250bool ProcessInstanceInfoMatch::NameMatches(const char *process_name) const {
252 return true;
253 const char *match_name = m_match_info.GetName();
254 if (!match_name)
255 return true;
256
257 return lldb_private::NameMatches(process_name, m_name_match_type, match_name);
258}
259
261 const ProcessInstanceInfo &proc_info) const {
263 m_match_info.GetProcessID() != proc_info.GetProcessID())
264 return false;
265
268 return false;
269 return true;
270}
271
273 const ProcessInstanceInfo &proc_info) const {
275 m_match_info.GetUserID() != proc_info.GetUserID())
276 return false;
277
279 m_match_info.GetGroupID() != proc_info.GetGroupID())
280 return false;
281
284 return false;
285
288 return false;
289 return true;
290}
292 const ProcessInstanceInfo &proc_info) const {
293 return ArchitectureMatches(proc_info.GetArchitecture()) &&
294 ProcessIDsMatch(proc_info) && UserIDsMatch(proc_info) &&
295 NameMatches(proc_info.GetName());
296}
297
300 return false;
301
303 return false;
304
306 return false;
307
309 return false;
310
312 return false;
313
315 return false;
316
318 return false;
319
321 return false;
322
324 return false;
325
326 return true;
327}
328
332 m_match_all_users = false;
333}
An architecture specification class.
Definition: ArchSpec.h:32
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:361
void Clear()
Clears the object state.
Definition: ArchSpec.cpp:536
void DumpTriple(llvm::raw_ostream &s) const
Definition: ArchSpec.cpp:1470
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
Definition: ArchSpec.h:515
A command line argument class.
Definition: Args.h:33
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
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
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.h:116
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
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
void Clear()
Clear the arguments.
Definition: Args.cpp:379
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:201
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:215
A file utility class.
Definition: FileSpec.h:56
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:173
const ConstString & GetFilename() const
Filename string const get accessor.
Definition: FileSpec.h:240
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:366
void Clear()
Clears the object state.
Definition: FileSpec.cpp:258
void Dump(llvm::raw_ostream &s) const
Dump this object to a Stream.
Definition: FileSpec.cpp:324
A plug-in interface definition class for debug platform that includes many platform abilities such as...
Definition: Platform.h:72
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:61
bool ProcessIDIsValid() const
Definition: ProcessInfo.h:70
void SetArg0(llvm::StringRef arg)
Definition: ProcessInfo.cpp:78
const char * GetName() const
Definition: ProcessInfo.cpp:41
void Dump(Stream &s, Platform *platform) const
Definition: ProcessInfo.cpp:49
lldb::pid_t GetProcessID() const
Definition: ProcessInfo.h:66
llvm::StringRef GetArg0() const
Definition: ProcessInfo.cpp:76
void SetArguments(const Args &args, bool first_arg_is_executable)
Definition: ProcessInfo.cpp:96
bool UserIDIsValid() const
Definition: ProcessInfo.h:52
uint32_t GetUserID() const
Definition: ProcessInfo.h:48
llvm::StringRef GetNameAsStringRef() const
Definition: ProcessInfo.cpp:45
uint32_t GetGroupID() const
Definition: ProcessInfo.h:50
bool GroupIDIsValid() const
Definition: ProcessInfo.h:54
ArchSpec & GetArchitecture()
Definition: ProcessInfo.h:60
bool NameMatches(const char *process_name) const
Return true iff the process name in this object matches process_name.
bool Matches(const ProcessInstanceInfo &proc_info) const
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.
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...
bool ArchitectureMatches(const ArchSpec &arch_spec) const
Return true iff the architecture in this object matches arch_spec.
uint32_t GetEffectiveUserID() const
Definition: ProcessInfo.h:121
static void DumpTableHeader(Stream &s, bool show_args, bool verbose)
lldb::pid_t GetParentProcessID() const
Definition: ProcessInfo.h:133
uint32_t GetEffectiveGroupID() const
Definition: ProcessInfo.h:123
void Dump(Stream &s, UserIDResolver &resolver) const
void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args, bool verbose) const
const char * GetData() const
Definition: StreamString.h:43
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void Format(const char *format, Args &&... args)
Definition: Stream.h:309
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:357
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
size_t PutChar(char ch)
Definition: Stream.cpp:104
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
An abstract interface for things that know how to map numeric user/group IDs into names.
std::optional< llvm::StringRef > GetGroupName(id_t gid)
std::optional< llvm::StringRef > GetUserName(id_t uid)
#define UINT32_MAX
Definition: lldb-defines.h:19
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:81
A class that represents a running process on the host machine.
bool NameMatches(llvm::StringRef name, NameMatch match_type, llvm::StringRef match)
Definition: NameMatches.cpp:15
Definition: SBAddress.h:15
uint64_t pid_t
Definition: lldb-types.h:85