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
13#include "lldb/Utility/Stream.h"
16#include "llvm/ADT/SmallString.h"
17
18#include <climits>
19#include <optional>
20
21using namespace lldb;
22using namespace lldb_private;
23
25 : m_executable(), m_arguments(), m_environment(), m_arch(), m_listener_sp(),
26 m_hijack_listener_sp(), m_shadow_listener_sp() {}
27
28ProcessInfo::ProcessInfo(const char *name, const ArchSpec &arch,
29 lldb::pid_t pid)
30 : m_executable(name), m_arguments(), m_environment(), m_arch(arch),
31 m_pid(pid), m_listener_sp(), m_hijack_listener_sp(),
32 m_shadow_listener_sp() {}
33
37 m_environment.clear();
40 m_arch.Clear();
43}
44
45const char *ProcessInfo::GetName() const {
47}
48
49llvm::StringRef ProcessInfo::GetNameAsStringRef() const {
51}
52
53void ProcessInfo::Dump(Stream &s, Platform *platform) const {
54 s << "Executable: " << GetName() << "\n";
55 s << "Triple: ";
57 s << "\n";
58
59 s << "Arguments:\n";
61
62 s.Format("Environment:\n{0}", m_environment);
63}
64
66 bool add_exe_file_as_first_arg) {
67 if (exe_file) {
68 m_executable = exe_file;
69 if (add_exe_file_as_first_arg) {
70 llvm::SmallString<128> filename;
71 exe_file.GetPath(filename);
72 if (!filename.empty())
74 }
75 } else {
77 }
78}
79
80llvm::StringRef ProcessInfo::GetArg0() const { return m_arg0; }
81
82void ProcessInfo::SetArg0(llvm::StringRef arg) { m_arg0 = std::string(arg); }
83
84void ProcessInfo::SetArguments(char const **argv,
85 bool first_arg_is_executable) {
87
88 // Is the first argument the executable?
89 if (first_arg_is_executable) {
90 const char *first_arg = m_arguments.GetArgumentAtIndex(0);
91 if (first_arg) {
92 // Yes the first argument is an executable, set it as the executable in
93 // the launch options. Don't resolve the file path as the path could be a
94 // remote platform path
95 m_executable.SetFile(first_arg, FileSpec::Style::native);
96 }
97 }
98}
99
100void ProcessInfo::SetArguments(const Args &args, bool first_arg_is_executable) {
101 // Copy all arguments
102 m_arguments = args;
103
104 // Is the first argument the executable?
105 if (first_arg_is_executable) {
106 const char *first_arg = m_arguments.GetArgumentAtIndex(0);
107 if (first_arg) {
108 // Yes the first argument is an executable, set it as the executable in
109 // the launch options. Don't resolve the file path as the path could be a
110 // remote platform path
111 m_executable.SetFile(first_arg, FileSpec::Style::native);
112 }
113 }
114}
115
118}
119
122 s.Printf(" pid = %" PRIu64 "\n", m_pid);
123
125 s.Printf(" parent = %" PRIu64 "\n", GetParentProcessID());
126
127 if (m_executable) {
128 s.Printf(" name = %s\n", m_executable.GetFilename().GetCString());
129 s.PutCString(" file = ");
131 s.EOL();
132 }
133 const uint32_t argc = m_arguments.GetArgumentCount();
134 if (argc > 0) {
135 for (uint32_t i = 0; i < argc; i++) {
136 const char *arg = m_arguments.GetArgumentAtIndex(i);
137 if (i < 10)
138 s.Printf(" arg[%u] = %s\n", i, arg);
139 else
140 s.Printf("arg[%u] = %s\n", i, arg);
141 }
142 }
143
144 s.Format("{0}", m_environment);
145
146 if (m_arch.IsValid()) {
147 s.Printf(" arch = ");
149 s.EOL();
150 }
151
152 if (UserIDIsValid()) {
153 s.Format(" uid = {0,-5} ({1})\n", GetUserID(),
154 resolver.GetUserName(GetUserID()).value_or(""));
155 }
156 if (GroupIDIsValid()) {
157 s.Format(" gid = {0,-5} ({1})\n", GetGroupID(),
158 resolver.GetGroupName(GetGroupID()).value_or(""));
159 }
161 s.Format(" euid = {0,-5} ({1})\n", GetEffectiveUserID(),
162 resolver.GetUserName(GetEffectiveUserID()).value_or(""));
163 }
165 s.Format(" egid = {0,-5} ({1})\n", GetEffectiveGroupID(),
166 resolver.GetGroupName(GetEffectiveGroupID()).value_or(""));
167 }
168}
169
171 bool verbose) {
172 const char *label;
173 if (show_args || verbose)
174 label = "ARGUMENTS";
175 else
176 label = "NAME";
177
178 if (verbose) {
179 s.Printf("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE "
180 " %s\n",
181 label);
182 s.PutCString(
183 "====== ====== ========== ========== ========== ========== "
184 "============================== ============================\n");
185 } else {
186 s.Printf("PID PARENT USER TRIPLE %s\n",
187 label);
188 s.PutCString("====== ====== ========== ============================== "
189 "============================\n");
190 }
191}
192
194 bool show_args, bool verbose) const {
196 s.Printf("%-6" PRIu64 " %-6" PRIu64 " ", m_pid,
198
199 StreamString arch_strm;
200 if (m_arch.IsValid())
201 m_arch.DumpTriple(arch_strm.AsRawOstream());
202
203 auto print = [&](bool (ProcessInstanceInfo::*isValid)() const,
204 uint32_t (ProcessInstanceInfo::*getID)() const,
205 std::optional<llvm::StringRef> (UserIDResolver::*getName)(
207 const char *format = "{0,-10} ";
208 if (!(this->*isValid)()) {
209 s.Format(format, "");
210 return;
211 }
212 uint32_t id = (this->*getID)();
213 if (auto name = (resolver.*getName)(id))
214 s.Format(format, *name);
215 else
216 s.Format(format, id);
217 };
218 if (verbose) {
229
230 s.Printf("%-30s ", arch_strm.GetData());
231 } else {
235 s.Printf("%-30s ", arch_strm.GetData());
236 }
237
238 if (verbose || show_args) {
240 const uint32_t argc = m_arguments.GetArgumentCount();
241 for (uint32_t i = 0; i < argc; i++) {
242 s.PutChar(' ');
244 }
245 } else {
246 s.PutCString(GetName());
247 }
248
249 s.EOL();
250 }
251}
252
254 const ArchSpec &arch_spec) const {
257}
258
259bool ProcessInstanceInfoMatch::NameMatches(const char *process_name) const {
261 return true;
262 const char *match_name = m_match_info.GetName();
263 if (!match_name)
264 return true;
265
266 return lldb_private::NameMatches(process_name, m_name_match_type, match_name);
267}
268
270 const ProcessInstanceInfo &proc_info) const {
272 m_match_info.GetProcessID() != proc_info.GetProcessID())
273 return false;
274
277 return false;
278 return true;
279}
280
282 const ProcessInstanceInfo &proc_info) const {
284 m_match_info.GetUserID() != proc_info.GetUserID())
285 return false;
286
288 m_match_info.GetGroupID() != proc_info.GetGroupID())
289 return false;
290
293 return false;
294
297 return false;
298 return true;
299}
301 const ProcessInstanceInfo &proc_info) const {
302 return ArchitectureMatches(proc_info.GetArchitecture()) &&
303 ProcessIDsMatch(proc_info) && UserIDsMatch(proc_info) &&
304 NameMatches(proc_info.GetName());
305}
306
309 return false;
310
312 return false;
313
315 return false;
316
318 return false;
319
321 return false;
322
324 return false;
325
327 return false;
328
330 return false;
331
333 return false;
334
335 return true;
336}
337
341 m_match_all_users = false;
342}
An architecture specification class.
Definition: ArchSpec.h:31
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:348
void Clear()
Clears the object state.
Definition: ArchSpec.cpp:542
void DumpTriple(llvm::raw_ostream &s) const
Definition: ArchSpec.cpp:1451
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
Definition: ArchSpec.h:502
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:198
void SetArguments(size_t argc, const char **argv)
Sets the argument vector value, optionally copying all arguments into an internal buffer.
Definition: Args.cpp:357
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:263
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:326
void Clear()
Clear the arguments.
Definition: Args.cpp:378
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:197
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
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:174
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:367
void Clear()
Clears the object state.
Definition: FileSpec.cpp:259
void Dump(llvm::raw_ostream &s) const
Dump this object to a Stream.
Definition: FileSpec.cpp:325
A plug-in interface definition class for debug platform that includes many platform abilities such as...
Definition: Platform.h:76
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:65
lldb::ScriptedMetadataSP m_scripted_metadata_sp
Definition: ProcessInfo.h:131
bool ProcessIDIsValid() const
Definition: ProcessInfo.h:72
void SetArg0(llvm::StringRef arg)
Definition: ProcessInfo.cpp:82
const char * GetName() const
Definition: ProcessInfo.cpp:45
void Dump(Stream &s, Platform *platform) const
Definition: ProcessInfo.cpp:53
lldb::pid_t GetProcessID() const
Definition: ProcessInfo.h:68
llvm::StringRef GetArg0() const
Definition: ProcessInfo.cpp:80
void SetArguments(const Args &args, bool first_arg_is_executable)
bool UserIDIsValid() const
Definition: ProcessInfo.h:54
uint32_t GetUserID() const
Definition: ProcessInfo.h:50
llvm::StringRef GetNameAsStringRef() const
Definition: ProcessInfo.cpp:49
uint32_t GetGroupID() const
Definition: ProcessInfo.h:52
bool IsScriptedProcess() const
bool GroupIDIsValid() const
Definition: ProcessInfo.h:56
ArchSpec & GetArchitecture()
Definition: ProcessInfo.h:62
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:160
static void DumpTableHeader(Stream &s, bool show_args, bool verbose)
lldb::pid_t GetParentProcessID() const
Definition: ProcessInfo.h:172
uint32_t GetEffectiveGroupID() const
Definition: ProcessInfo.h:162
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:45
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:353
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:401
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
size_t PutChar(char ch)
Definition: Stream.cpp:131
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:155
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:89
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:83