Coverage Report

Created: 2023-09-21 18:56

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Utility/ProcessInfo.h
Line
Count
Source (jump to first uncovered line)
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"
14
#include "lldb/Utility/Environment.h"
15
#include "lldb/Utility/FileSpec.h"
16
#include "lldb/Utility/NameMatches.h"
17
#include "lldb/Utility/StructuredData.h"
18
#include <vector>
19
20
namespace lldb_private {
21
22
class UserIDResolver;
23
24
// ProcessInfo
25
//
26
// A base class for information for a process. This can be used to fill
27
// out information for a process prior to launching it, or it can be used for
28
// an instance of a process and can be filled in with the existing values for
29
// that process.
30
class ProcessInfo {
31
public:
32
  ProcessInfo();
33
34
  ProcessInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid);
35
36
  void Clear();
37
38
  const char *GetName() const;
39
40
  llvm::StringRef GetNameAsStringRef() const;
41
42
11.4k
  FileSpec &GetExecutableFile() { return m_executable; }
43
44
  void SetExecutableFile(const FileSpec &exe_file,
45
                         bool add_exe_file_as_first_arg);
46
47
27
  const FileSpec &GetExecutableFile() const { return m_executable; }
48
49
8.52k
  uint32_t GetUserID() const { return m_uid; }
50
51
81
  uint32_t GetGroupID() const { return m_gid; }
52
53
1.40k
  bool UserIDIsValid() const { return m_uid != UINT32_MAX; }
54
55
1.40k
  bool GroupIDIsValid() const { return m_gid != UINT32_MAX; }
56
57
2.95k
  void SetUserID(uint32_t uid) { m_uid = uid; }
58
59
826
  void SetGroupID(uint32_t gid) { m_gid = gid; }
60
61
23.4k
  ArchSpec &GetArchitecture() { return m_arch; }
62
63
6.73k
  const ArchSpec &GetArchitecture() const { return m_arch; }
64
65
2.16k
  void SetArchitecture(const ArchSpec &arch) { m_arch = arch; }
66
67
20.9k
  lldb::pid_t GetProcessID() const { return m_pid; }
68
69
7.23k
  void SetProcessID(lldb::pid_t pid) { m_pid = pid; }
70
71
9.33k
  bool ProcessIDIsValid() const { return m_pid != LLDB_INVALID_PROCESS_ID; }
72
73
  void Dump(Stream &s, Platform *platform) const;
74
75
16.6k
  Args &GetArguments() { return m_arguments; }
76
77
7.09k
  const Args &GetArguments() const { return m_arguments; }
78
79
  llvm::StringRef GetArg0() const;
80
81
  void SetArg0(llvm::StringRef arg);
82
83
  void SetArguments(const Args &args, bool first_arg_is_executable);
84
85
  void SetArguments(char const **argv, bool first_arg_is_executable);
86
87
20.7k
  Environment &GetEnvironment() { return m_environment; }
88
6.32k
  const Environment &GetEnvironment() const { return m_environment; }
89
90
  bool IsScriptedProcess() const;
91
92
204
  lldb::ScriptedMetadataSP GetScriptedMetadata() const {
93
204
    return m_scripted_metadata_sp;
94
204
  }
95
96
6
  void SetScriptedMetadata(lldb::ScriptedMetadataSP metadata_sp) {
97
6
    m_scripted_metadata_sp = metadata_sp;
98
6
  }
99
100
  // Get and set the actual listener that will be used for the process events
101
16
  lldb::ListenerSP GetListener() const { return m_listener_sp; }
102
103
24
  void SetListener(const lldb::ListenerSP &listener_sp) {
104
24
    m_listener_sp = listener_sp;
105
24
  }
106
107
10.6k
  lldb::ListenerSP GetHijackListener() const { return m_hijack_listener_sp; }
108
109
4.24k
  void SetHijackListener(const lldb::ListenerSP &listener_sp) {
110
4.24k
    m_hijack_listener_sp = listener_sp;
111
4.24k
  }
112
113
2.13k
  lldb::ListenerSP GetShadowListener() const { return m_shadow_listener_sp; }
114
115
1
  void SetShadowListener(const lldb::ListenerSP &listener_sp) {
116
1
    m_shadow_listener_sp = listener_sp;
117
1
  }
118
119
protected:
120
  FileSpec m_executable;
121
  std::string m_arg0; // argv[0] if supported. If empty, then use m_executable.
122
  // Not all process plug-ins support specifying an argv[0] that differs from
123
  // the resolved platform executable (which is in m_executable)
124
  Args m_arguments; // All program arguments except argv[0]
125
  Environment m_environment;
126
  uint32_t m_uid = UINT32_MAX;
127
  uint32_t m_gid = UINT32_MAX;
128
  ArchSpec m_arch;
129
  lldb::pid_t m_pid = LLDB_INVALID_PROCESS_ID;
130
  lldb::ScriptedMetadataSP m_scripted_metadata_sp = nullptr;
131
  lldb::ListenerSP m_listener_sp = nullptr;
132
  lldb::ListenerSP m_hijack_listener_sp = nullptr;
133
  lldb::ListenerSP m_shadow_listener_sp = nullptr;
134
};
135
136
// ProcessInstanceInfo
137
//
138
// Describes an existing process and any discoverable information that pertains
139
// to that process.
140
class ProcessInstanceInfo : public ProcessInfo {
141
public:
142
21.3k
  ProcessInstanceInfo() = default;
143
144
  ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid)
145
      : ProcessInfo(name, arch, pid), m_euid(UINT32_MAX), m_egid(UINT32_MAX),
146
        m_parent_pid(LLDB_INVALID_PROCESS_ID) {}
147
148
12.3k
  void Clear() {
149
12.3k
    ProcessInfo::Clear();
150
12.3k
    m_euid = UINT32_MAX;
151
12.3k
    m_egid = UINT32_MAX;
152
12.3k
    m_parent_pid = LLDB_INVALID_PROCESS_ID;
153
12.3k
  }
154
155
165
  uint32_t GetEffectiveUserID() const { return m_euid; }
156
157
81
  uint32_t GetEffectiveGroupID() const { return m_egid; }
158
159
1.48k
  bool EffectiveUserIDIsValid() const { return m_euid != UINT32_MAX; }
160
161
1.40k
  bool EffectiveGroupIDIsValid() const { return m_egid != UINT32_MAX; }
162
163
826
  void SetEffectiveUserID(uint32_t uid) { m_euid = uid; }
164
165
826
  void SetEffectiveGroupID(uint32_t gid) { m_egid = gid; }
166
167
1
  lldb::pid_t GetParentProcessID() const { return m_parent_pid; }
168
169
824
  void SetParentProcessID(lldb::pid_t pid) { m_parent_pid = pid; }
170
171
1.32k
  bool ParentProcessIDIsValid() const {
172
1.32k
    return m_parent_pid != LLDB_INVALID_PROCESS_ID;
173
1.32k
  }
174
175
  void Dump(Stream &s, UserIDResolver &resolver) const;
176
177
  static void DumpTableHeader(Stream &s, bool show_args, bool verbose);
178
179
  void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args,
180
                      bool verbose) const;
181
182
protected:
183
  uint32_t m_euid = UINT32_MAX;
184
  uint32_t m_egid = UINT32_MAX;
185
  lldb::pid_t m_parent_pid = LLDB_INVALID_PROCESS_ID;
186
};
187
188
typedef std::vector<ProcessInstanceInfo> ProcessInstanceInfoList;
189
190
// ProcessInstanceInfoMatch
191
//
192
// A class to help matching one ProcessInstanceInfo to another.
193
194
class ProcessInstanceInfoMatch {
195
public:
196
6.10k
  ProcessInstanceInfoMatch() = default;
197
198
  ProcessInstanceInfoMatch(const char *process_name,
199
                           NameMatch process_name_match_type)
200
0
      : m_name_match_type(process_name_match_type), m_match_all_users(false) {
201
0
    m_match_info.GetExecutableFile().SetFile(process_name,
202
0
                                             FileSpec::Style::native);
203
0
  }
204
205
20
  ProcessInstanceInfo &GetProcessInfo() { return m_match_info; }
206
207
798
  const ProcessInstanceInfo &GetProcessInfo() const { return m_match_info; }
208
209
13
  bool GetMatchAllUsers() const { return m_match_all_users; }
210
211
3
  void SetMatchAllUsers(bool b) { m_match_all_users = b; }
212
213
774
  NameMatch GetNameMatchType() const { return m_name_match_type; }
214
215
4
  void SetNameMatchType(NameMatch name_match_type) {
216
4
    m_name_match_type = name_match_type;
217
4
  }
218
219
  /// Return true iff the architecture in this object matches arch_spec.
220
  bool ArchitectureMatches(const ArchSpec &arch_spec) const;
221
222
  /// Return true iff the process name in this object matches process_name.
223
  bool NameMatches(const char *process_name) const;
224
225
  /// Return true iff the process ID and parent process IDs in this object match
226
  /// the ones in proc_info.
227
  bool ProcessIDsMatch(const ProcessInstanceInfo &proc_info) const;
228
229
  /// Return true iff the (both effective and real) user and group IDs in this
230
  /// object match the ones in proc_info.
231
  bool UserIDsMatch(const ProcessInstanceInfo &proc_info) const;
232
233
  bool Matches(const ProcessInstanceInfo &proc_info) const;
234
235
  bool MatchAllProcesses() const;
236
  void Clear();
237
238
protected:
239
  ProcessInstanceInfo m_match_info;
240
  NameMatch m_name_match_type = NameMatch::Ignore;
241
  bool m_match_all_users = false;
242
};
243
244
} // namespace lldb_private
245
246
#endif // LLDB_UTILITY_PROCESSINFO_H