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 
12 #include "lldb/Utility/ArchSpec.h"
13 #include "lldb/Utility/Args.h"
15 #include "lldb/Utility/FileSpec.h"
17 #include <vector>
18 
19 namespace lldb_private {
20 
21 class UserIDResolver;
22 
23 // ProcessInfo
24 //
25 // A base class for information for a process. This can be used to fill
26 // out information for a process prior to launching it, or it can be used for
27 // an instance of a process and can be filled in with the existing values for
28 // that process.
29 class ProcessInfo {
30 public:
31  ProcessInfo();
32 
33  ProcessInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid);
34 
35  void Clear();
36 
37  const char *GetName() const;
38 
39  llvm::StringRef GetNameAsStringRef() 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 
70  bool ProcessIDIsValid() const { return m_pid != LLDB_INVALID_PROCESS_ID; }
71 
72  void Dump(Stream &s, Platform *platform) const;
73 
74  Args &GetArguments() { return m_arguments; }
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 protected:
91  std::string m_arg0; // argv[0] if supported. If empty, then use m_executable.
92  // Not all process plug-ins support specifying an argv[0] that differs from
93  // the resolved platform executable (which is in m_executable)
94  Args m_arguments; // All program arguments except argv[0]
100 };
101 
102 // ProcessInstanceInfo
103 //
104 // Describes an existing process and any discoverable information that pertains
105 // to that process.
107 public:
108  ProcessInstanceInfo() = default;
109 
110  ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid)
111  : ProcessInfo(name, arch, pid), m_euid(UINT32_MAX), m_egid(UINT32_MAX),
113 
114  void Clear() {
116  m_euid = UINT32_MAX;
117  m_egid = UINT32_MAX;
119  }
120 
121  uint32_t GetEffectiveUserID() const { return m_euid; }
122 
123  uint32_t GetEffectiveGroupID() const { return m_egid; }
124 
125  bool EffectiveUserIDIsValid() const { return m_euid != UINT32_MAX; }
126 
127  bool EffectiveGroupIDIsValid() const { return m_egid != UINT32_MAX; }
128 
129  void SetEffectiveUserID(uint32_t uid) { m_euid = uid; }
130 
131  void SetEffectiveGroupID(uint32_t gid) { m_egid = gid; }
132 
134 
136 
137  bool ParentProcessIDIsValid() const {
139  }
140 
141  void Dump(Stream &s, UserIDResolver &resolver) const;
142 
143  static void DumpTableHeader(Stream &s, bool show_args, bool verbose);
144 
145  void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args,
146  bool verbose) const;
147 
148 protected:
152 };
153 
154 typedef std::vector<ProcessInstanceInfo> ProcessInstanceInfoList;
155 
156 // ProcessInstanceInfoMatch
157 //
158 // A class to help matching one ProcessInstanceInfo to another.
159 
161 public:
162  ProcessInstanceInfoMatch() = default;
163 
164  ProcessInstanceInfoMatch(const char *process_name,
165  NameMatch process_name_match_type)
166  : m_name_match_type(process_name_match_type), m_match_all_users(false) {
167  m_match_info.GetExecutableFile().SetFile(process_name,
168  FileSpec::Style::native);
169  }
170 
172 
174 
175  bool GetMatchAllUsers() const { return m_match_all_users; }
176 
177  void SetMatchAllUsers(bool b) { m_match_all_users = b; }
178 
180 
181  void SetNameMatchType(NameMatch name_match_type) {
182  m_name_match_type = name_match_type;
183  }
184 
185  /// Return true iff the architecture in this object matches arch_spec.
186  bool ArchitectureMatches(const ArchSpec &arch_spec) const;
187 
188  /// Return true iff the process name in this object matches process_name.
189  bool NameMatches(const char *process_name) const;
190 
191  /// Return true iff the process ID and parent process IDs in this object match
192  /// the ones in proc_info.
193  bool ProcessIDsMatch(const ProcessInstanceInfo &proc_info) const;
194 
195  /// Return true iff the (both effective and real) user and group IDs in this
196  /// object match the ones in proc_info.
197  bool UserIDsMatch(const ProcessInstanceInfo &proc_info) const;
198 
199  bool Matches(const ProcessInstanceInfo &proc_info) const;
200 
201  bool MatchAllProcesses() const;
202  void Clear();
203 
204 protected:
207  bool m_match_all_users = false;
208 };
209 
210 } // namespace lldb_private
211 
212 #endif // LLDB_UTILITY_PROCESSINFO_H
lldb_private::ProcessInfo::GetArguments
Args & GetArguments()
Definition: ProcessInfo.h:74
lldb_private::ProcessInstanceInfo
Definition: ProcessInfo.h:106
lldb_private::ProcessInstanceInfoMatch::SetNameMatchType
void SetNameMatchType(NameMatch name_match_type)
Definition: ProcessInfo.h:181
lldb_private::ProcessInstanceInfoMatch::m_name_match_type
NameMatch m_name_match_type
Definition: ProcessInfo.h:206
lldb_private::ProcessInfo::m_uid
uint32_t m_uid
Definition: ProcessInfo.h:96
lldb_private::ProcessInfo::GetEnvironment
const Environment & GetEnvironment() const
Definition: ProcessInfo.h:87
lldb_private::ArchSpec
Definition: ArchSpec.h:32
lldb_private::ProcessInfo::ProcessIDIsValid
bool ProcessIDIsValid() const
Definition: ProcessInfo.h:70
lldb_private::ProcessInfo::SetExecutableFile
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:60
lldb_private::ProcessInstanceInfoMatch::GetMatchAllUsers
bool GetMatchAllUsers() const
Definition: ProcessInfo.h:175
lldb_private::ProcessInfo::SetArchitecture
void SetArchitecture(const ArchSpec &arch)
Definition: ProcessInfo.h:64
LLDB_INVALID_PROCESS_ID
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:81
lldb_private::ProcessInfo::m_executable
FileSpec m_executable
Definition: ProcessInfo.h:90
lldb_private::ProcessInstanceInfo::Clear
void Clear()
Definition: ProcessInfo.h:114
lldb_private::ProcessInstanceInfoMatch::NameMatches
bool NameMatches(const char *process_name) const
Return true iff the process name in this object matches process_name.
Definition: ProcessInfo.cpp:249
lldb_private::ProcessInstanceInfoMatch::GetProcessInfo
const ProcessInstanceInfo & GetProcessInfo() const
Definition: ProcessInfo.h:173
lldb_private::ProcessInfo::m_arch
ArchSpec m_arch
Definition: ProcessInfo.h:98
lldb_private::ProcessInfo::GetGroupID
uint32_t GetGroupID() const
Definition: ProcessInfo.h:50
lldb_private::ProcessInstanceInfoMatch::SetMatchAllUsers
void SetMatchAllUsers(bool b)
Definition: ProcessInfo.h:177
lldb_private::ProcessInfo::GetUserID
uint32_t GetUserID() const
Definition: ProcessInfo.h:48
lldb_private::ProcessInstanceInfoMatch::ArchitectureMatches
bool ArchitectureMatches(const ArchSpec &arch_spec) const
Return true iff the architecture in this object matches arch_spec.
Definition: ProcessInfo.cpp:243
lldb_private::ProcessInstanceInfo::GetEffectiveGroupID
uint32_t GetEffectiveGroupID() const
Definition: ProcessInfo.h:123
lldb_private::ProcessInstanceInfoMatch::Clear
void Clear()
Definition: ProcessInfo.cpp:328
lldb_private::ProcessInfo::SetUserID
void SetUserID(uint32_t uid)
Definition: ProcessInfo.h:56
lldb_private::ProcessInstanceInfoMatch
Definition: ProcessInfo.h:160
lldb_private::ProcessInstanceInfo::m_euid
uint32_t m_euid
Definition: ProcessInfo.h:149
lldb_private::ProcessInstanceInfo::m_egid
uint32_t m_egid
Definition: ProcessInfo.h:150
lldb_private::ProcessInfo::GetProcessID
lldb::pid_t GetProcessID() const
Definition: ProcessInfo.h:66
lldb_private::ProcessInfo::SetProcessID
void SetProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:68
lldb_private::ProcessInstanceInfo::SetEffectiveUserID
void SetEffectiveUserID(uint32_t uid)
Definition: ProcessInfo.h:129
lldb_private::Stream
Definition: Stream.h:28
lldb_private::Args
Definition: Args.h:33
lldb_private::ProcessInstanceInfo::EffectiveUserIDIsValid
bool EffectiveUserIDIsValid() const
Definition: ProcessInfo.h:125
lldb_private::ProcessInstanceInfo::m_parent_pid
lldb::pid_t m_parent_pid
Definition: ProcessInfo.h:151
lldb_private::ProcessInfo::GetExecutableFile
const FileSpec & GetExecutableFile() const
Definition: ProcessInfo.h:46
lldb_private::ProcessInfo::UserIDIsValid
bool UserIDIsValid() const
Definition: ProcessInfo.h:52
lldb_private::ProcessInstanceInfoMatch::Matches
bool Matches(const ProcessInstanceInfo &proc_info) const
Definition: ProcessInfo.cpp:290
lldb_private::NameMatch
NameMatch
Definition: NameMatches.h:15
lldb_private::ProcessInstanceInfo::DumpTableHeader
static void DumpTableHeader(Stream &s, bool show_args, bool verbose)
Definition: ProcessInfo.cpp:161
lldb_private::FileSpec
Definition: FileSpec.h:55
lldb_private::ProcessInfo::m_arguments
Args m_arguments
Definition: ProcessInfo.h:94
lldb_private::ProcessInfo::Clear
void Clear()
Definition: ProcessInfo.cpp:30
lldb_private::ProcessInstanceInfo::GetEffectiveUserID
uint32_t GetEffectiveUserID() const
Definition: ProcessInfo.h:121
Args.h
lldb_private::ProcessInfo
Definition: ProcessInfo.h:29
lldb_private::ProcessInstanceInfo::SetEffectiveGroupID
void SetEffectiveGroupID(uint32_t gid)
Definition: ProcessInfo.h:131
lldb_private::ProcessInfo::ProcessInfo
ProcessInfo()
Definition: ProcessInfo.cpp:22
lldb_private::ProcessInfo::GroupIDIsValid
bool GroupIDIsValid() const
Definition: ProcessInfo.h:54
NameMatches.h
lldb_private::ProcessInfo::SetArg0
void SetArg0(llvm::StringRef arg)
Definition: ProcessInfo.cpp:77
lldb_private::ProcessInstanceInfoMatch::GetProcessInfo
ProcessInstanceInfo & GetProcessInfo()
Definition: ProcessInfo.h:171
lldb_private::ProcessInstanceInfoMatch::MatchAllProcesses
bool MatchAllProcesses() const
Definition: ProcessInfo.cpp:297
lldb_private::ProcessInstanceInfo::ParentProcessIDIsValid
bool ParentProcessIDIsValid() const
Definition: ProcessInfo.h:137
lldb_private::ProcessInfo::GetName
const char * GetName() const
Definition: ProcessInfo.cpp:40
lldb_private::ProcessInfo::m_pid
lldb::pid_t m_pid
Definition: ProcessInfo.h:99
lldb_private::ProcessInfo::GetNameAsStringRef
llvm::StringRef GetNameAsStringRef() const
Definition: ProcessInfo.cpp:44
lldb_private::ProcessInstanceInfoMatch::m_match_all_users
bool m_match_all_users
Definition: ProcessInfo.h:207
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::ProcessInfo::m_environment
Environment m_environment
Definition: ProcessInfo.h:95
lldb_private::ProcessInfo::m_gid
uint32_t m_gid
Definition: ProcessInfo.h:97
lldb_private::ProcessInstanceInfo::EffectiveGroupIDIsValid
bool EffectiveGroupIDIsValid() const
Definition: ProcessInfo.h:127
lldb_private::ProcessInstanceInfoMatch::GetNameMatchType
NameMatch GetNameMatchType() const
Definition: ProcessInfo.h:179
lldb_private::UserIDResolver
An abstract interface for things that know how to map numeric user/group IDs into names.
Definition: UserIDResolver.h:23
lldb_private::ProcessInstanceInfo::GetParentProcessID
lldb::pid_t GetParentProcessID() const
Definition: ProcessInfo.h:133
lldb_private::ProcessInstanceInfoMatch::UserIDsMatch
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...
Definition: ProcessInfo.cpp:271
uint32_t
lldb_private::ProcessInfo::GetEnvironment
Environment & GetEnvironment()
Definition: ProcessInfo.h:86
lldb_private::ProcessInstanceInfoMatch::m_match_info
ProcessInstanceInfo m_match_info
Definition: ProcessInfo.h:205
lldb::pid_t
uint64_t pid_t
Definition: lldb-types.h:85
lldb_private::NameMatch::Ignore
@ Ignore
lldb_private::ProcessInstanceInfo::Dump
void Dump(Stream &s, UserIDResolver &resolver) const
Definition: ProcessInfo.cpp:111
ArchSpec.h
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:19
lldb_private::Environment
Definition: Environment.h:18
lldb_private::ProcessInstanceInfoMatch::ProcessInstanceInfoMatch
ProcessInstanceInfoMatch(const char *process_name, NameMatch process_name_match_type)
Definition: ProcessInfo.h:164
lldb_private::ProcessInfo::m_arg0
std::string m_arg0
Definition: ProcessInfo.h:91
lldb_private::Platform
Definition: Platform.h:71
lldb_private::ProcessInstanceInfo::DumpAsTableRow
void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args, bool verbose) const
Definition: ProcessInfo.cpp:184
Environment.h
lldb_private::ProcessInstanceInfoMatch::ProcessInstanceInfoMatch
ProcessInstanceInfoMatch()=default
lldb_private::ProcessInfo::GetArchitecture
ArchSpec & GetArchitecture()
Definition: ProcessInfo.h:60
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ProcessInfo::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: ProcessInfo.h:62
lldb_private::ProcessInfo::GetExecutableFile
FileSpec & GetExecutableFile()
Definition: ProcessInfo.h:41
FileSpec.h
lldb_private::ProcessInstanceInfo::ProcessInstanceInfo
ProcessInstanceInfo()=default
lldb_private::ProcessInstanceInfo::ProcessInstanceInfo
ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid)
Definition: ProcessInfo.h:110
lldb_private::ProcessInfo::GetArg0
llvm::StringRef GetArg0() const
Definition: ProcessInfo.cpp:75
lldb_private::FileSpec::SetFile
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:172
lldb_private::ProcessInfo::GetArguments
const Args & GetArguments() const
Definition: ProcessInfo.h:76
lldb_private::ProcessInfo::Dump
void Dump(Stream &s, Platform *platform) const
Definition: ProcessInfo.cpp:48
lldb_private::ProcessInstanceInfoList
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList
Definition: Host.h:31
lldb_private::ProcessInstanceInfoMatch::ProcessIDsMatch
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.
Definition: ProcessInfo.cpp:259
lldb_private::ProcessInstanceInfo::SetParentProcessID
void SetParentProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:135
lldb_private::ProcessInfo::SetArguments
void SetArguments(const Args &args, bool first_arg_is_executable)
Definition: ProcessInfo.cpp:95
lldb_private::ProcessInfo::SetGroupID
void SetGroupID(uint32_t gid)
Definition: ProcessInfo.h:58