LLDB mainline
ProcessInfo.h
Go to the documentation of this file.
1//===-- ProcessInfo.h -------------------------------------------*- C++ -*-===//
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
9#ifndef LLDB_UTILITY_PROCESSINFO_H
10#define LLDB_UTILITY_PROCESSINFO_H
11
13#include "lldb/Utility/Args.h"
18#include <optional>
19#include <vector>
20
21namespace lldb_private {
22
23class UserIDResolver;
24
25// ProcessInfo
26//
27// A base class for information for a process. This can be used to fill
28// out information for a process prior to launching it, or it can be used for
29// an instance of a process and can be filled in with the existing values for
30// that process.
32public:
34
35 ProcessInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid);
36
37 void Clear();
38
39 const char *GetName() const;
40
41 llvm::StringRef GetNameAsStringRef() const;
42
44
45 void SetExecutableFile(const FileSpec &exe_file,
46 bool add_exe_file_as_first_arg);
47
48 const FileSpec &GetExecutableFile() const { return m_executable; }
49
50 uint32_t GetUserID() const { return m_uid; }
51
52 uint32_t GetGroupID() const { return m_gid; }
53
54 bool UserIDIsValid() const { return m_uid != UINT32_MAX; }
55
56 bool GroupIDIsValid() const { return m_gid != UINT32_MAX; }
57
58 void SetUserID(uint32_t uid) { m_uid = uid; }
59
60 void SetGroupID(uint32_t gid) { m_gid = gid; }
61
63
64 const ArchSpec &GetArchitecture() const { return m_arch; }
65
66 void SetArchitecture(const ArchSpec &arch) { m_arch = arch; }
67
68 lldb::pid_t GetProcessID() const { return m_pid; }
69
70 void SetProcessID(lldb::pid_t pid) { m_pid = pid; }
71
73
74 void Dump(Stream &s, Platform *platform) const;
75
77
78 const Args &GetArguments() const { return m_arguments; }
79
80 llvm::StringRef GetArg0() const;
81
82 void SetArg0(llvm::StringRef arg);
83
84 void SetArguments(const Args &args, bool first_arg_is_executable);
85
86 void SetArguments(char const **argv, bool first_arg_is_executable);
87
89 const Environment &GetEnvironment() const { return m_environment; }
90
91 bool IsScriptedProcess() const;
92
95 }
96
98 m_scripted_metadata_sp = metadata_sp;
99 }
100
101 // Get and set the actual listener that will be used for the process events
103
104 void SetListener(const lldb::ListenerSP &listener_sp) {
105 m_listener_sp = listener_sp;
106 }
107
109
110 void SetHijackListener(const lldb::ListenerSP &listener_sp) {
111 m_hijack_listener_sp = listener_sp;
112 }
113
115
116 void SetShadowListener(const lldb::ListenerSP &listener_sp) {
117 m_shadow_listener_sp = listener_sp;
118 }
119
120protected:
122 std::string m_arg0; // argv[0] if supported. If empty, then use m_executable.
123 // Not all process plug-ins support specifying an argv[0] that differs from
124 // the resolved platform executable (which is in m_executable)
125 Args m_arguments; // All program arguments except argv[0]
127 uint32_t m_uid = UINT32_MAX;
128 uint32_t m_gid = UINT32_MAX;
135};
136
137// ProcessInstanceInfo
138//
139// Describes an existing process and any discoverable information that pertains
140// to that process.
142public:
143 struct timespec {
144 time_t tv_sec = 0;
145 long int tv_usec = 0;
146 };
147
149
150 ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid)
151 : ProcessInfo(name, arch, pid) {}
152
153 void Clear() {
158 }
159
160 uint32_t GetEffectiveUserID() const { return m_euid; }
161
162 uint32_t GetEffectiveGroupID() const { return m_egid; }
163
164 bool EffectiveUserIDIsValid() const { return m_euid != UINT32_MAX; }
165
166 bool EffectiveGroupIDIsValid() const { return m_egid != UINT32_MAX; }
167
168 void SetEffectiveUserID(uint32_t uid) { m_euid = uid; }
169
170 void SetEffectiveGroupID(uint32_t gid) { m_egid = gid; }
171
173
175
178 }
179
181
183
186 }
187
189
191 m_process_session_id = session;
192 }
193
196 }
197
198 struct timespec GetUserTime() const { return m_user_time; }
199
200 void SetUserTime(struct timespec utime) { m_user_time = utime; }
201
202 bool UserTimeIsValid() const {
203 return m_user_time.tv_sec > 0 || m_user_time.tv_usec > 0;
204 }
205
206 struct timespec GetSystemTime() const { return m_system_time; }
207
208 void SetSystemTime(struct timespec stime) { m_system_time = stime; }
209
210 bool SystemTimeIsValid() const {
211 return m_system_time.tv_sec > 0 || m_system_time.tv_usec > 0;
212 }
213
216 }
217
218 void SetCumulativeUserTime(struct timespec cutime) {
219 m_cumulative_user_time = cutime;
220 }
221
223 return m_cumulative_user_time.tv_sec > 0 ||
225 }
226
229 }
230
231 void SetCumulativeSystemTime(struct timespec cstime) {
233 }
234
236 return m_cumulative_system_time.tv_sec > 0 ||
238 }
239
240 std::optional<int8_t> GetPriorityValue() const { return m_priority_value; }
241
242 void SetPriorityValue(int8_t priority_value) {
243 m_priority_value = priority_value;
244 }
245
246 void SetIsZombie(bool is_zombie) { m_zombie = is_zombie; }
247
248 std::optional<bool> IsZombie() const { return m_zombie; }
249
250 void Dump(Stream &s, UserIDResolver &resolver) const;
251
252 static void DumpTableHeader(Stream &s, bool show_args, bool verbose);
253
254 void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args,
255 bool verbose) const;
256
257protected:
258 uint32_t m_euid = UINT32_MAX;
259 uint32_t m_egid = UINT32_MAX;
267 std::optional<int8_t> m_priority_value = std::nullopt;
268 std::optional<bool> m_zombie = std::nullopt;
269};
270
271typedef std::vector<ProcessInstanceInfo> ProcessInstanceInfoList;
272
274public:
276
277 uint32_t GetSize() const { return m_list.size(); }
278
279 bool GetProcessInfoAtIndex(uint32_t idx, ProcessInstanceInfo &info) {
280 if (idx < m_list.size()) {
281 info = m_list[idx];
282 return true;
283 }
284 return false;
285 }
286
287 void Clear() { return m_list.clear(); }
288
289private:
291};
292
293// ProcessInstanceInfoMatch
294//
295// A class to help matching one ProcessInstanceInfo to another.
296
298public:
300
301 ProcessInstanceInfoMatch(const char *process_name,
302 NameMatch process_name_match_type)
303 : m_name_match_type(process_name_match_type), m_match_all_users(false) {
305 FileSpec::Style::native);
306 }
307
309
311
312 bool GetMatchAllUsers() const { return m_match_all_users; }
313
315
317
318 void SetNameMatchType(NameMatch name_match_type) {
319 m_name_match_type = name_match_type;
320 }
321
322 /// Return true iff the architecture in this object matches arch_spec.
323 bool ArchitectureMatches(const ArchSpec &arch_spec) const;
324
325 /// Return true iff the process name in this object matches process_name.
326 bool NameMatches(const char *process_name) const;
327
328 /// Return true iff the process ID and parent process IDs in this object match
329 /// the ones in proc_info.
330 bool ProcessIDsMatch(const ProcessInstanceInfo &proc_info) const;
331
332 /// Return true iff the (both effective and real) user and group IDs in this
333 /// object match the ones in proc_info.
334 bool UserIDsMatch(const ProcessInstanceInfo &proc_info) const;
335
336 bool Matches(const ProcessInstanceInfo &proc_info) const;
337
338 bool MatchAllProcesses() const;
339 void Clear();
340
341protected:
344 bool m_match_all_users = false;
345};
346
347} // namespace lldb_private
348
349#endif // LLDB_UTILITY_PROCESSINFO_H
An architecture specification class.
Definition: ArchSpec.h:31
A command line argument class.
Definition: Args.h:33
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
A plug-in interface definition class for debug platform that includes many platform abilities such as...
Definition: Platform.h:76
ProcessInstanceInfoList m_list
Definition: ProcessInfo.h:290
ProcessInfoList(const ProcessInstanceInfoList &list)
Definition: ProcessInfo.h:275
bool GetProcessInfoAtIndex(uint32_t idx, ProcessInstanceInfo &info)
Definition: ProcessInfo.h:279
const FileSpec & GetExecutableFile() const
Definition: ProcessInfo.h:48
void SetGroupID(uint32_t gid)
Definition: ProcessInfo.h:60
void SetHijackListener(const lldb::ListenerSP &listener_sp)
Definition: ProcessInfo.h:110
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:65
lldb::ScriptedMetadataSP GetScriptedMetadata() const
Definition: ProcessInfo.h:93
lldb::ListenerSP GetHijackListener() const
Definition: ProcessInfo.h:108
void SetArchitecture(const ArchSpec &arch)
Definition: ProcessInfo.h:66
lldb::ScriptedMetadataSP m_scripted_metadata_sp
Definition: ProcessInfo.h:131
bool ProcessIDIsValid() const
Definition: ProcessInfo.h:72
void SetShadowListener(const lldb::ListenerSP &listener_sp)
Definition: ProcessInfo.h:116
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 SetScriptedMetadata(lldb::ScriptedMetadataSP metadata_sp)
Definition: ProcessInfo.h:97
lldb::ListenerSP m_hijack_listener_sp
Definition: ProcessInfo.h:133
void SetArguments(const Args &args, bool first_arg_is_executable)
void SetProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:70
lldb::ListenerSP m_listener_sp
Definition: ProcessInfo.h:132
FileSpec & GetExecutableFile()
Definition: ProcessInfo.h:43
void SetListener(const lldb::ListenerSP &listener_sp)
Definition: ProcessInfo.h:104
lldb::ListenerSP GetListener() const
Definition: ProcessInfo.h:102
const Environment & GetEnvironment() const
Definition: ProcessInfo.h:89
lldb::ListenerSP GetShadowListener() const
Definition: ProcessInfo.h:114
bool UserIDIsValid() const
Definition: ProcessInfo.h:54
const ArchSpec & GetArchitecture() const
Definition: ProcessInfo.h:64
uint32_t GetUserID() const
Definition: ProcessInfo.h:50
const Args & GetArguments() const
Definition: ProcessInfo.h:78
Environment & GetEnvironment()
Definition: ProcessInfo.h:88
llvm::StringRef GetNameAsStringRef() const
Definition: ProcessInfo.cpp:49
uint32_t GetGroupID() const
Definition: ProcessInfo.h:52
bool IsScriptedProcess() const
void SetUserID(uint32_t uid)
Definition: ProcessInfo.h:58
bool GroupIDIsValid() const
Definition: ProcessInfo.h:56
ArchSpec & GetArchitecture()
Definition: ProcessInfo.h:62
lldb::ListenerSP m_shadow_listener_sp
Definition: ProcessInfo.h:134
bool NameMatches(const char *process_name) const
Return true iff the process name in this object matches process_name.
ProcessInstanceInfoMatch(const char *process_name, NameMatch process_name_match_type)
Definition: ProcessInfo.h:301
const ProcessInstanceInfo & GetProcessInfo() const
Definition: ProcessInfo.h:310
bool Matches(const ProcessInstanceInfo &proc_info) const
void SetNameMatchType(NameMatch name_match_type)
Definition: ProcessInfo.h:318
ProcessInstanceInfo & GetProcessInfo()
Definition: ProcessInfo.h:308
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.
struct timespec m_cumulative_user_time
Definition: ProcessInfo.h:265
uint32_t GetEffectiveUserID() const
Definition: ProcessInfo.h:160
lldb::pid_t GetProcessSessionID() const
Definition: ProcessInfo.h:188
ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid)
Definition: ProcessInfo.h:150
void SetUserTime(struct timespec utime)
Definition: ProcessInfo.h:200
struct timespec m_cumulative_system_time
Definition: ProcessInfo.h:266
void SetEffectiveGroupID(uint32_t gid)
Definition: ProcessInfo.h:170
static void DumpTableHeader(Stream &s, bool show_args, bool verbose)
struct timespec GetSystemTime() const
Definition: ProcessInfo.h:206
lldb::pid_t GetParentProcessID() const
Definition: ProcessInfo.h:172
struct timespec GetUserTime() const
Definition: ProcessInfo.h:198
std::optional< int8_t > GetPriorityValue() const
Definition: ProcessInfo.h:240
struct timespec GetCumulativeSystemTime() const
Definition: ProcessInfo.h:227
void SetIsZombie(bool is_zombie)
Definition: ProcessInfo.h:246
void SetCumulativeUserTime(struct timespec cutime)
Definition: ProcessInfo.h:218
void SetPriorityValue(int8_t priority_value)
Definition: ProcessInfo.h:242
void SetProcessGroupID(lldb::pid_t pgrp)
Definition: ProcessInfo.h:182
void SetProcessSessionID(lldb::pid_t session)
Definition: ProcessInfo.h:190
void SetCumulativeSystemTime(struct timespec cstime)
Definition: ProcessInfo.h:231
uint32_t GetEffectiveGroupID() const
Definition: ProcessInfo.h:162
std::optional< bool > IsZombie() const
Definition: ProcessInfo.h:248
struct timespec GetCumulativeUserTime() const
Definition: ProcessInfo.h:214
void SetSystemTime(struct timespec stime)
Definition: ProcessInfo.h:208
void Dump(Stream &s, UserIDResolver &resolver) const
lldb::pid_t GetProcessGroupID() const
Definition: ProcessInfo.h:180
std::optional< int8_t > m_priority_value
Definition: ProcessInfo.h:267
void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args, bool verbose) const
void SetParentProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:174
std::optional< bool > m_zombie
Definition: ProcessInfo.h:268
void SetEffectiveUserID(uint32_t uid)
Definition: ProcessInfo.h:168
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
An abstract interface for things that know how to map numeric user/group IDs into names.
#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.
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList
Definition: Host.h:32
std::shared_ptr< lldb_private::ScriptedMetadata > ScriptedMetadataSP
Definition: lldb-forward.h:405
uint64_t pid_t
Definition: lldb-types.h:83
std::shared_ptr< lldb_private::Listener > ListenerSP
Definition: lldb-forward.h:366