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 // LLDB headers
13 #include "lldb/Utility/ArchSpec.h"
14 #include "lldb/Utility/Args.h"
16 #include "lldb/Utility/FileSpec.h"
18 
19 #include <vector>
20 
21 namespace lldb_private {
22 
23 class 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.
31 class ProcessInfo {
32 public:
33  ProcessInfo();
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  size_t GetNameLength() 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 
72  bool ProcessIDIsValid() const { return m_pid != LLDB_INVALID_PROCESS_ID; }
73 
74  void Dump(Stream &s, Platform *platform) const;
75 
76  Args &GetArguments() { return m_arguments; }
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 protected:
93  std::string m_arg0; // argv[0] if supported. If empty, then use m_executable.
94  // Not all process plug-ins support specifying an argv[0] that differs from
95  // the resolved platform executable (which is in m_executable)
96  Args m_arguments; // All program arguments except argv[0]
102 };
103 
104 // ProcessInstanceInfo
105 //
106 // Describes an existing process and any discoverable information that pertains
107 // to that process.
109 public:
111  : ProcessInfo(), m_euid(UINT32_MAX), m_egid(UINT32_MAX),
112  m_parent_pid(LLDB_INVALID_PROCESS_ID) {}
113 
114  ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid)
115  : ProcessInfo(name, arch, pid), m_euid(UINT32_MAX), m_egid(UINT32_MAX),
116  m_parent_pid(LLDB_INVALID_PROCESS_ID) {}
117 
118  void Clear() {
120  m_euid = UINT32_MAX;
121  m_egid = UINT32_MAX;
122  m_parent_pid = LLDB_INVALID_PROCESS_ID;
123  }
124 
125  uint32_t GetEffectiveUserID() const { return m_euid; }
126 
127  uint32_t GetEffectiveGroupID() const { return m_egid; }
128 
129  bool EffectiveUserIDIsValid() const { return m_euid != UINT32_MAX; }
130 
131  bool EffectiveGroupIDIsValid() const { return m_egid != UINT32_MAX; }
132 
133  void SetEffectiveUserID(uint32_t uid) { m_euid = uid; }
134 
135  void SetEffectiveGroupID(uint32_t gid) { m_egid = gid; }
136 
137  lldb::pid_t GetParentProcessID() const { return m_parent_pid; }
138 
139  void SetParentProcessID(lldb::pid_t pid) { m_parent_pid = pid; }
140 
141  bool ParentProcessIDIsValid() const {
142  return m_parent_pid != LLDB_INVALID_PROCESS_ID;
143  }
144 
145  void Dump(Stream &s, UserIDResolver &resolver) const;
146 
147  static void DumpTableHeader(Stream &s, bool show_args, bool verbose);
148 
149  void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args,
150  bool verbose) const;
151 
152 protected:
156 };
157 
159 public:
160  ProcessInstanceInfoList() = default;
161 
162  void Clear() { m_infos.clear(); }
163 
164  size_t GetSize() { return m_infos.size(); }
165 
166  void Append(const ProcessInstanceInfo &info) { m_infos.push_back(info); }
167 
168  const char *GetProcessNameAtIndex(size_t idx) {
169  return ((idx < m_infos.size()) ? m_infos[idx].GetName() : nullptr);
170  }
171 
172  size_t GetProcessNameLengthAtIndex(size_t idx) {
173  return ((idx < m_infos.size()) ? m_infos[idx].GetNameLength() : 0);
174  }
175 
177  return ((idx < m_infos.size()) ? m_infos[idx].GetProcessID() : 0);
178  }
179 
180  bool GetInfoAtIndex(size_t idx, ProcessInstanceInfo &info) {
181  if (idx < m_infos.size()) {
182  info = m_infos[idx];
183  return true;
184  }
185  return false;
186  }
187 
188  // You must ensure "idx" is valid before calling this function
189  const ProcessInstanceInfo &GetProcessInfoAtIndex(size_t idx) const {
190  assert(idx < m_infos.size());
191  return m_infos[idx];
192  }
193 
194 protected:
195  std::vector<ProcessInstanceInfo> m_infos;
196 };
197 
198 // ProcessInstanceInfoMatch
199 //
200 // A class to help matching one ProcessInstanceInfo to another.
201 
203 public:
205  : m_match_info(), m_name_match_type(NameMatch::Ignore),
206  m_match_all_users(false) {}
207 
208  ProcessInstanceInfoMatch(const char *process_name,
209  NameMatch process_name_match_type)
210  : m_match_info(), m_name_match_type(process_name_match_type),
211  m_match_all_users(false) {
212  m_match_info.GetExecutableFile().SetFile(process_name,
213  FileSpec::Style::native);
214  }
215 
216  ProcessInstanceInfo &GetProcessInfo() { return m_match_info; }
217 
218  const ProcessInstanceInfo &GetProcessInfo() const { return m_match_info; }
219 
220  bool GetMatchAllUsers() const { return m_match_all_users; }
221 
222  void SetMatchAllUsers(bool b) { m_match_all_users = b; }
223 
224  NameMatch GetNameMatchType() const { return m_name_match_type; }
225 
226  void SetNameMatchType(NameMatch name_match_type) {
227  m_name_match_type = name_match_type;
228  }
229 
230  bool NameMatches(const char *process_name) const;
231 
232  bool Matches(const ProcessInstanceInfo &proc_info) const;
233 
234  bool MatchAllProcesses() const;
235  void Clear();
236 
237 protected:
241 };
242 
243 } // namespace lldb_private
244 
245 #endif // #ifndef LLDB_UTILITY_PROCESSINFO_H
llvm::StringRef GetArg0() const
Definition: ProcessInfo.cpp:76
bool NameMatches(llvm::StringRef name, NameMatch match_type, llvm::StringRef match)
Definition: NameMatches.cpp:15
A command line argument class.
Definition: Args.h:32
uint32_t GetEffectiveGroupID() const
Definition: ProcessInfo.h:127
bool ProcessIDIsValid() const
Definition: ProcessInfo.h:72
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
void SetGroupID(uint32_t gid)
Definition: ProcessInfo.h:60
size_t GetNameLength() const
Definition: ProcessInfo.cpp:45
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
Definition: ProcessInfo.cpp:61
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
uint32_t GetUserID() const
Definition: ProcessInfo.h:50
void SetArchitecture(const ArchSpec &arch)
Definition: ProcessInfo.h:66
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:92
const ProcessInstanceInfo & GetProcessInfo() const
Definition: ProcessInfo.h:218
void Append(const ProcessInstanceInfo &info)
Definition: ProcessInfo.h:166
ProcessInstanceInfo & GetProcessInfo()
Definition: ProcessInfo.h:216
size_t GetProcessNameLengthAtIndex(size_t idx)
Definition: ProcessInfo.h:172
void SetProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:70
A file utility class.
Definition: FileSpec.h:55
An architecture specification class.
Definition: ArchSpec.h:32
void SetNameMatchType(NameMatch name_match_type)
Definition: ProcessInfo.h:226
void SetArguments(const Args &args, bool first_arg_is_executable)
Definition: ProcessInfo.cpp:96
const char * GetName() const
Definition: ProcessInfo.cpp:41
uint32_t GetEffectiveUserID() const
Definition: ProcessInfo.h:125
#define UINT32_MAX
Definition: lldb-defines.h:31
void SetArg0(llvm::StringRef arg)
Definition: ProcessInfo.cpp:78
std::vector< ProcessInstanceInfo > m_infos
Definition: ProcessInfo.h:195
void SetEffectiveUserID(uint32_t uid)
Definition: ProcessInfo.h:133
void SetEffectiveGroupID(uint32_t gid)
Definition: ProcessInfo.h:135
const ProcessInstanceInfo & GetProcessInfoAtIndex(size_t idx) const
Definition: ProcessInfo.h:189
void SetParentProcessID(lldb::pid_t pid)
Definition: ProcessInfo.h:139
Environment & GetEnvironment()
Definition: ProcessInfo.h:88
ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid)
Definition: ProcessInfo.h:114
uint32_t GetGroupID() const
Definition: ProcessInfo.h:52
FileSpec & GetExecutableFile()
Definition: ProcessInfo.h:43
A plug-in interface definition class for debug platform that includes many platform abilities such as...
Definition: Platform.h:67
ArchSpec & GetArchitecture()
Definition: ProcessInfo.h:62
ProcessInstanceInfoMatch(const char *process_name, NameMatch process_name_match_type)
Definition: ProcessInfo.h:208
const char * GetProcessNameAtIndex(size_t idx)
Definition: ProcessInfo.h:168
const ArchSpec & GetArchitecture() const
Definition: ProcessInfo.h:64
void Dump(Stream &s, Platform *platform) const
Definition: ProcessInfo.cpp:49
lldb::pid_t GetParentProcessID() const
Definition: ProcessInfo.h:137
An abstract interface for things that know how to map numeric user/group IDs into names...
const FileSpec & GetExecutableFile() const
Definition: ProcessInfo.h:48
uint64_t pid_t
Definition: lldb-types.h:85
const Environment & GetEnvironment() const
Definition: ProcessInfo.h:89
void SetUserID(uint32_t uid)
Definition: ProcessInfo.h:58
bool GetInfoAtIndex(size_t idx, ProcessInstanceInfo &info)
Definition: ProcessInfo.h:180
bool UserIDIsValid() const
Definition: ProcessInfo.h:54
const Args & GetArguments() const
Definition: ProcessInfo.h:78
lldb::pid_t GetProcessID() const
Definition: ProcessInfo.h:68
lldb::pid_t GetProcessIDAtIndex(size_t idx)
Definition: ProcessInfo.h:176
bool GroupIDIsValid() const
Definition: ProcessInfo.h:56