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
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),
33
35 m_executable.Clear();
36 m_arguments.Clear();
37 m_environment.clear();
40 m_arch.Clear();
43}
44
45llvm::StringRef ProcessInfo::GetName() const {
46 return m_executable.GetFilename().GetStringRef();
47}
48
49void ProcessInfo::Dump(Stream &s, Platform *platform) const {
50 s << "Executable: " << GetName() << "\n";
51 s << "Triple: ";
52 m_arch.DumpTriple(s.AsRawOstream());
53 s << "\n";
54
55 s << "Arguments:\n";
56 m_arguments.Dump(s);
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())
69 m_arguments.InsertArgumentAtIndex(0, filename);
70 }
71 } else {
72 m_executable.Clear();
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) {
82 m_arguments.SetArguments(argv);
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
115
118 s.Printf(" pid = %" PRIu64 "\n", m_pid);
119
121 s.Printf(" parent = %" PRIu64 "\n", GetParentProcessID());
122
123 if (m_executable) {
124 s.Printf(" name = %s\n", m_executable.GetFilename().GetCString());
125 s.PutCString(" file = ");
126 m_executable.Dump(s.AsRawOstream());
127 s.EOL();
128 }
129 const uint32_t argc = m_arguments.GetArgumentCount();
130 if (argc > 0) {
131 for (uint32_t i = 0; i < argc; i++) {
132 const char *arg = m_arguments.GetArgumentAtIndex(i);
133 if (i < 10)
134 s.Printf(" arg[%u] = %s\n", i, arg);
135 else
136 s.Printf("arg[%u] = %s\n", i, arg);
137 }
138 }
139
140 s.Format("{0}", m_environment);
141
142 if (m_arch.IsValid()) {
143 s.Printf(" arch = ");
144 m_arch.DumpTriple(s.AsRawOstream());
145 s.EOL();
146 }
147
148 if (UserIDIsValid()) {
149 s.Format(" uid = {0,-5} ({1})\n", GetUserID(),
150 resolver.GetUserName(GetUserID()).value_or(""));
151 }
152 if (GroupIDIsValid()) {
153 s.Format(" gid = {0,-5} ({1})\n", GetGroupID(),
154 resolver.GetGroupName(GetGroupID()).value_or(""));
155 }
157 s.Format(" euid = {0,-5} ({1})\n", GetEffectiveUserID(),
158 resolver.GetUserName(GetEffectiveUserID()).value_or(""));
159 }
161 s.Format(" egid = {0,-5} ({1})\n", GetEffectiveGroupID(),
162 resolver.GetGroupName(GetEffectiveGroupID()).value_or(""));
163 }
164}
165
167 bool verbose) {
168 const char *label;
169 if (show_args || verbose)
170 label = "ARGUMENTS";
171 else
172 label = "NAME";
173
174 if (verbose) {
175 s.Printf("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE "
176 " %s\n",
177 label);
178 s.PutCString(
179 "====== ====== ========== ========== ========== ========== "
180 "============================== ============================\n");
181 } else {
182 s.Printf("PID PARENT USER TRIPLE %s\n",
183 label);
184 s.PutCString("====== ====== ========== ============================== "
185 "============================\n");
186 }
187}
188
190 bool show_args, bool verbose) const {
192 s.Printf("%-6" PRIu64 " %-6" PRIu64 " ", m_pid,
194
195 StreamString arch_strm;
196 if (m_arch.IsValid())
197 m_arch.DumpTriple(arch_strm.AsRawOstream());
198
199 auto print = [&](bool (ProcessInstanceInfo::*isValid)() const,
200 uint32_t (ProcessInstanceInfo::*getID)() const,
201 std::optional<llvm::StringRef> (UserIDResolver::*getName)(
203 const char *format = "{0,-10} ";
204 if (!(this->*isValid)()) {
205 s.Format(format, "");
206 return;
207 }
208 uint32_t id = (this->*getID)();
209 if (auto name = (resolver.*getName)(id))
210 s.Format(format, *name);
211 else
212 s.Format(format, id);
213 };
214 if (verbose) {
225
226 s.Printf("%-30s ", arch_strm.GetData());
227 } else {
231 s.Printf("%-30s ", arch_strm.GetData());
232 }
233
234 if (verbose || show_args) {
236 const uint32_t argc = m_arguments.GetArgumentCount();
237 for (uint32_t i = 0; i < argc; i++) {
238 s.PutChar(' ');
239 s.PutCString(m_arguments.GetArgumentAtIndex(i));
240 }
241 } else {
242 s.PutCString(GetName());
243 }
244
245 s.EOL();
246 }
247}
248
250 const ArchSpec &arch_spec) const {
251 return !m_match_info.GetArchitecture().IsValid() ||
252 m_match_info.GetArchitecture().IsCompatibleMatch(arch_spec);
253}
254
255bool ProcessInstanceInfoMatch::NameMatches(llvm::StringRef process_name) const {
257 return true;
258 llvm::StringRef match_name = m_match_info.GetName();
259 if (match_name.empty())
260 return true;
261
262 return lldb_private::NameMatches(process_name, m_name_match_type, match_name);
263}
264
266 const ProcessInstanceInfo &proc_info) const {
267 if (m_match_info.ProcessIDIsValid() &&
268 m_match_info.GetProcessID() != proc_info.GetProcessID())
269 return false;
270
271 if (m_match_info.ParentProcessIDIsValid() &&
272 m_match_info.GetParentProcessID() != proc_info.GetParentProcessID())
273 return false;
274 return true;
275}
276
278 const ProcessInstanceInfo &proc_info) const {
279 if (m_match_info.UserIDIsValid() &&
280 m_match_info.GetUserID() != proc_info.GetUserID())
281 return false;
282
283 if (m_match_info.GroupIDIsValid() &&
284 m_match_info.GetGroupID() != proc_info.GetGroupID())
285 return false;
286
287 if (m_match_info.EffectiveUserIDIsValid() &&
288 m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID())
289 return false;
290
291 if (m_match_info.EffectiveGroupIDIsValid() &&
292 m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID())
293 return false;
294 return true;
295}
297 const ProcessInstanceInfo &proc_info) const {
298 return ArchitectureMatches(proc_info.GetArchitecture()) &&
299 ProcessIDsMatch(proc_info) && UserIDsMatch(proc_info) &&
300 NameMatches(proc_info.GetName());
301}
302
305 return false;
306
307 if (m_match_info.ProcessIDIsValid())
308 return false;
309
310 if (m_match_info.ParentProcessIDIsValid())
311 return false;
312
313 if (m_match_info.UserIDIsValid())
314 return false;
315
316 if (m_match_info.GroupIDIsValid())
317 return false;
318
319 if (m_match_info.EffectiveUserIDIsValid())
320 return false;
321
322 if (m_match_info.EffectiveGroupIDIsValid())
323 return false;
324
325 if (m_match_info.GetArchitecture().IsValid())
326 return false;
327
329 return false;
330
331 return true;
332}
333
An architecture specification class.
Definition ArchSpec.h:32
A command line argument class.
Definition Args.h:33
A file utility class.
Definition FileSpec.h:57
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
A plug-in interface definition class for debug platform that includes many platform abilities such as...
Definition Platform.h:79
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
lldb::ScriptedMetadataSP m_scripted_metadata_sp
void SetArg0(llvm::StringRef arg)
void Dump(Stream &s, Platform *platform) const
lldb::pid_t GetProcessID() const
Definition ProcessInfo.h:66
llvm::StringRef GetArg0() const
lldb::ListenerSP m_hijack_listener_sp
void SetArguments(const Args &args, bool first_arg_is_executable)
lldb::ListenerSP m_listener_sp
llvm::StringRef GetName() const
uint32_t GetUserID() const
Definition ProcessInfo.h:48
uint32_t GetGroupID() const
Definition ProcessInfo.h:50
bool GroupIDIsValid() const
Definition ProcessInfo.h:54
ArchSpec & GetArchitecture()
Definition ProcessInfo.h:60
lldb::ListenerSP m_shadow_listener_sp
bool NameMatches(llvm::StringRef 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.
static void DumpTableHeader(Stream &s, bool show_args, bool verbose)
lldb::pid_t GetParentProcessID() const
void Dump(Stream &s, UserIDResolver &resolver) const
void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args, bool verbose) const
const char * GetData() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:370
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:405
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:63
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
#define LLDB_INVALID_PROCESS_ID
A class that represents a running process on the host machine.
bool NameMatches(llvm::StringRef name, NameMatch match_type, llvm::StringRef match)
uint64_t pid_t
Definition lldb-types.h:83