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 llvm::StringRef GetName() const;
40
42
43 void SetExecutableFile(const FileSpec &exe_file,
44 bool add_exe_file_as_first_arg);
45
46 const FileSpec &GetExecutableFile() const { return m_executable; }
47
48 uint32_t GetUserID() const { return m_uid; }
49
50 uint32_t GetGroupID() const { return m_gid; }
51
52 bool UserIDIsValid() const { return m_uid != UINT32_MAX; }
53
54 bool GroupIDIsValid() const { return m_gid != UINT32_MAX; }
55
56 void SetUserID(uint32_t uid) { m_uid = uid; }
57
58 void SetGroupID(uint32_t gid) { m_gid = gid; }
59
61
62 const ArchSpec &GetArchitecture() const { return m_arch; }
63
64 void SetArchitecture(const ArchSpec &arch) { m_arch = arch; }
65
66 lldb::pid_t GetProcessID() const { return m_pid; }
67
68 void SetProcessID(lldb::pid_t pid) { m_pid = pid; }
69
71
72 void Dump(Stream &s, Platform *platform) const;
73
75
76 const Args &GetArguments() const { return m_arguments; }
77
78 llvm::StringRef GetArg0() const;
79
80 void SetArg0(llvm::StringRef arg);
81
82 void SetArguments(const Args &args, bool first_arg_is_executable);
83
84 void SetArguments(char const **argv, bool first_arg_is_executable);
85
87 const Environment &GetEnvironment() const { return m_environment; }
88
89 bool IsScriptedProcess() const;
90
94
96 m_scripted_metadata_sp = metadata_sp;
97 }
98
99 // Get and set the actual listener that will be used for the process events
101
102 void SetListener(const lldb::ListenerSP &listener_sp) {
103 m_listener_sp = listener_sp;
104 }
105
107
108 void SetHijackListener(const lldb::ListenerSP &listener_sp) {
109 m_hijack_listener_sp = listener_sp;
110 }
111
113
114 void SetShadowListener(const lldb::ListenerSP &listener_sp) {
115 m_shadow_listener_sp = listener_sp;
116 }
117
118protected:
120 std::string m_arg0; // argv[0] if supported. If empty, then use m_executable.
121 // Not all process plug-ins support specifying an argv[0] that differs from
122 // the resolved platform executable (which is in m_executable)
123 Args m_arguments; // All program arguments except argv[0]
125 uint32_t m_uid = UINT32_MAX;
126 uint32_t m_gid = UINT32_MAX;
133};
134
135// ProcessInstanceInfo
136//
137// Describes an existing process and any discoverable information that pertains
138// to that process.
140public:
141 struct timespec {
142 time_t tv_sec = 0;
143 long int tv_usec = 0;
144 };
145
147
148 ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid)
149 : ProcessInfo(name, arch, pid) {}
150
157
158 uint32_t GetEffectiveUserID() const { return m_euid; }
159
160 uint32_t GetEffectiveGroupID() const { return m_egid; }
161
162 bool EffectiveUserIDIsValid() const { return m_euid != UINT32_MAX; }
163
164 bool EffectiveGroupIDIsValid() const { return m_egid != UINT32_MAX; }
165
166 void SetEffectiveUserID(uint32_t uid) { m_euid = uid; }
167
168 void SetEffectiveGroupID(uint32_t gid) { m_egid = gid; }
169
171
173
177
179
181
185
187
189 m_process_session_id = session;
190 }
191
195
196 struct timespec GetUserTime() const { return m_user_time; }
197
198 void SetUserTime(struct timespec utime) { m_user_time = utime; }
199
200 bool UserTimeIsValid() const {
201 return m_user_time.tv_sec > 0 || m_user_time.tv_usec > 0;
202 }
203
204 struct timespec GetSystemTime() const { return m_system_time; }
205
206 void SetSystemTime(struct timespec stime) { m_system_time = stime; }
207
208 bool SystemTimeIsValid() const {
209 return m_system_time.tv_sec > 0 || m_system_time.tv_usec > 0;
210 }
211
214 }
215
216 void SetCumulativeUserTime(struct timespec cutime) {
217 m_cumulative_user_time = cutime;
218 }
219
221 return m_cumulative_user_time.tv_sec > 0 ||
222 m_cumulative_user_time.tv_usec > 0;
223 }
224
228
229 void SetCumulativeSystemTime(struct timespec cstime) {
231 }
232
234 return m_cumulative_system_time.tv_sec > 0 ||
235 m_cumulative_system_time.tv_usec > 0;
236 }
237
238 std::optional<int8_t> GetPriorityValue() const { return m_priority_value; }
239
240 void SetPriorityValue(int8_t priority_value) {
241 m_priority_value = priority_value;
242 }
243
244 void SetIsZombie(bool is_zombie) { m_zombie = is_zombie; }
245
246 std::optional<bool> IsZombie() const { return m_zombie; }
247
248 // proc/../status specifies CoreDumping as the field
249 // so we match the case here.
250 void SetIsCoreDumping(bool is_coredumping) { m_coredumping = is_coredumping; }
251 std::optional<bool> IsCoreDumping() const { return m_coredumping; }
252
253 void Dump(Stream &s, UserIDResolver &resolver) const;
254
255 static void DumpTableHeader(Stream &s, bool show_args, bool verbose);
256
257 void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args,
258 bool verbose) const;
259
260protected:
261 uint32_t m_euid = UINT32_MAX;
262 uint32_t m_egid = UINT32_MAX;
270 std::optional<int8_t> m_priority_value = std::nullopt;
271 std::optional<bool> m_zombie = std::nullopt;
272 std::optional<bool> m_coredumping = std::nullopt;
273};
274
275typedef std::vector<ProcessInstanceInfo> ProcessInstanceInfoList;
276
278public:
280
281 uint32_t GetSize() const { return m_list.size(); }
282
283 bool GetProcessInfoAtIndex(uint32_t idx, ProcessInstanceInfo &info) {
284 if (idx < m_list.size()) {
285 info = m_list[idx];
286 return true;
287 }
288 return false;
289 }
290
291 void Clear() { return m_list.clear(); }
292
293private:
295};
296
297// ProcessInstanceInfoMatch
298//
299// A class to help matching one ProcessInstanceInfo to another.
300
302public:
304
305 ProcessInstanceInfoMatch(const char *process_name,
306 NameMatch process_name_match_type)
307 : m_name_match_type(process_name_match_type), m_match_all_users(false) {
308 m_match_info.GetExecutableFile().SetFile(process_name,
309 FileSpec::Style::native);
310 }
311
313
315
316 bool GetMatchAllUsers() const { return m_match_all_users; }
317
319
321
322 void SetNameMatchType(NameMatch name_match_type) {
323 m_name_match_type = name_match_type;
324 }
325
326 /// Return true iff the architecture in this object matches arch_spec.
327 bool ArchitectureMatches(const ArchSpec &arch_spec) const;
328
329 /// Return true iff the process name in this object matches process_name.
330 bool NameMatches(llvm::StringRef process_name) const;
331
332 /// Return true iff the process ID and parent process IDs in this object match
333 /// the ones in proc_info.
334 bool ProcessIDsMatch(const ProcessInstanceInfo &proc_info) const;
335
336 /// Return true iff the (both effective and real) user and group IDs in this
337 /// object match the ones in proc_info.
338 bool UserIDsMatch(const ProcessInstanceInfo &proc_info) const;
339
340 bool Matches(const ProcessInstanceInfo &proc_info) const;
341
342 bool MatchAllProcesses() const;
343 void Clear();
344
345protected:
348 bool m_match_all_users = false;
349};
350
351} // namespace lldb_private
352
353#endif // LLDB_UTILITY_PROCESSINFO_H
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
A plug-in interface definition class for debug platform that includes many platform abilities such as...
Definition Platform.h:79
ProcessInstanceInfoList m_list
ProcessInfoList(const ProcessInstanceInfoList &list)
bool GetProcessInfoAtIndex(uint32_t idx, ProcessInstanceInfo &info)
const FileSpec & GetExecutableFile() const
Definition ProcessInfo.h:46
void SetGroupID(uint32_t gid)
Definition ProcessInfo.h:58
void SetHijackListener(const lldb::ListenerSP &listener_sp)
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
lldb::ScriptedMetadataSP GetScriptedMetadata() const
Definition ProcessInfo.h:91
lldb::ListenerSP GetHijackListener() const
void SetArchitecture(const ArchSpec &arch)
Definition ProcessInfo.h:64
lldb::ScriptedMetadataSP m_scripted_metadata_sp
bool ProcessIDIsValid() const
Definition ProcessInfo.h:70
void SetShadowListener(const lldb::ListenerSP &listener_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
void SetScriptedMetadata(lldb::ScriptedMetadataSP metadata_sp)
Definition ProcessInfo.h:95
lldb::ListenerSP m_hijack_listener_sp
void SetArguments(const Args &args, bool first_arg_is_executable)
void SetProcessID(lldb::pid_t pid)
Definition ProcessInfo.h:68
lldb::ListenerSP m_listener_sp
FileSpec & GetExecutableFile()
Definition ProcessInfo.h:41
void SetListener(const lldb::ListenerSP &listener_sp)
lldb::ListenerSP GetListener() const
const Environment & GetEnvironment() const
Definition ProcessInfo.h:87
llvm::StringRef GetName() const
lldb::ListenerSP GetShadowListener() const
const ArchSpec & GetArchitecture() const
Definition ProcessInfo.h:62
uint32_t GetUserID() const
Definition ProcessInfo.h:48
const Args & GetArguments() const
Definition ProcessInfo.h:76
Environment & GetEnvironment()
Definition ProcessInfo.h:86
uint32_t GetGroupID() const
Definition ProcessInfo.h:50
void SetUserID(uint32_t uid)
Definition ProcessInfo.h:56
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.
ProcessInstanceInfoMatch(const char *process_name, NameMatch process_name_match_type)
const ProcessInstanceInfo & GetProcessInfo() const
bool Matches(const ProcessInstanceInfo &proc_info) const
void SetNameMatchType(NameMatch name_match_type)
ProcessInstanceInfo & GetProcessInfo()
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.
lldb::pid_t GetProcessSessionID() const
ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid)
void SetUserTime(struct timespec utime)
struct timespec m_cumulative_system_time
void SetEffectiveGroupID(uint32_t gid)
void SetIsCoreDumping(bool is_coredumping)
std::optional< bool > IsCoreDumping() const
static void DumpTableHeader(Stream &s, bool show_args, bool verbose)
struct timespec GetSystemTime() const
lldb::pid_t GetParentProcessID() const
struct timespec GetUserTime() const
std::optional< int8_t > GetPriorityValue() const
struct timespec GetCumulativeSystemTime() const
void SetIsZombie(bool is_zombie)
void SetCumulativeUserTime(struct timespec cutime)
void SetPriorityValue(int8_t priority_value)
void SetProcessGroupID(lldb::pid_t pgrp)
void SetProcessSessionID(lldb::pid_t session)
void SetCumulativeSystemTime(struct timespec cstime)
std::optional< bool > m_coredumping
std::optional< bool > IsZombie() const
struct timespec GetCumulativeUserTime() const
void SetSystemTime(struct timespec stime)
void Dump(Stream &s, UserIDResolver &resolver) const
lldb::pid_t GetProcessGroupID() const
std::optional< int8_t > m_priority_value
void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args, bool verbose) const
void SetParentProcessID(lldb::pid_t pid)
std::optional< bool > m_zombie
void SetEffectiveUserID(uint32_t uid)
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
#define LLDB_INVALID_PROCESS_ID
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
uint64_t pid_t
Definition lldb-types.h:83
std::shared_ptr< lldb_private::Listener > ListenerSP