Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:57

0001 //===-- ProcessInfo.h -------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_UTILITY_PROCESSINFO_H
0010 #define LLDB_UTILITY_PROCESSINFO_H
0011 
0012 #include "lldb/Utility/ArchSpec.h"
0013 #include "lldb/Utility/Args.h"
0014 #include "lldb/Utility/Environment.h"
0015 #include "lldb/Utility/FileSpec.h"
0016 #include "lldb/Utility/NameMatches.h"
0017 #include "lldb/Utility/StructuredData.h"
0018 #include <optional>
0019 #include <vector>
0020 
0021 namespace lldb_private {
0022 
0023 class UserIDResolver;
0024 
0025 // ProcessInfo
0026 //
0027 // A base class for information for a process. This can be used to fill
0028 // out information for a process prior to launching it, or it can be used for
0029 // an instance of a process and can be filled in with the existing values for
0030 // that process.
0031 class ProcessInfo {
0032 public:
0033   ProcessInfo();
0034 
0035   ProcessInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid);
0036 
0037   void Clear();
0038 
0039   const char *GetName() const;
0040 
0041   llvm::StringRef GetNameAsStringRef() const;
0042 
0043   FileSpec &GetExecutableFile() { return m_executable; }
0044 
0045   void SetExecutableFile(const FileSpec &exe_file,
0046                          bool add_exe_file_as_first_arg);
0047 
0048   const FileSpec &GetExecutableFile() const { return m_executable; }
0049 
0050   uint32_t GetUserID() const { return m_uid; }
0051 
0052   uint32_t GetGroupID() const { return m_gid; }
0053 
0054   bool UserIDIsValid() const { return m_uid != UINT32_MAX; }
0055 
0056   bool GroupIDIsValid() const { return m_gid != UINT32_MAX; }
0057 
0058   void SetUserID(uint32_t uid) { m_uid = uid; }
0059 
0060   void SetGroupID(uint32_t gid) { m_gid = gid; }
0061 
0062   ArchSpec &GetArchitecture() { return m_arch; }
0063 
0064   const ArchSpec &GetArchitecture() const { return m_arch; }
0065 
0066   void SetArchitecture(const ArchSpec &arch) { m_arch = arch; }
0067 
0068   lldb::pid_t GetProcessID() const { return m_pid; }
0069 
0070   void SetProcessID(lldb::pid_t pid) { m_pid = pid; }
0071 
0072   bool ProcessIDIsValid() const { return m_pid != LLDB_INVALID_PROCESS_ID; }
0073 
0074   void Dump(Stream &s, Platform *platform) const;
0075 
0076   Args &GetArguments() { return m_arguments; }
0077 
0078   const Args &GetArguments() const { return m_arguments; }
0079 
0080   llvm::StringRef GetArg0() const;
0081 
0082   void SetArg0(llvm::StringRef arg);
0083 
0084   void SetArguments(const Args &args, bool first_arg_is_executable);
0085 
0086   void SetArguments(char const **argv, bool first_arg_is_executable);
0087 
0088   Environment &GetEnvironment() { return m_environment; }
0089   const Environment &GetEnvironment() const { return m_environment; }
0090 
0091   bool IsScriptedProcess() const;
0092 
0093   lldb::ScriptedMetadataSP GetScriptedMetadata() const {
0094     return m_scripted_metadata_sp;
0095   }
0096 
0097   void SetScriptedMetadata(lldb::ScriptedMetadataSP metadata_sp) {
0098     m_scripted_metadata_sp = metadata_sp;
0099   }
0100 
0101   // Get and set the actual listener that will be used for the process events
0102   lldb::ListenerSP GetListener() const { return m_listener_sp; }
0103 
0104   void SetListener(const lldb::ListenerSP &listener_sp) {
0105     m_listener_sp = listener_sp;
0106   }
0107 
0108   lldb::ListenerSP GetHijackListener() const { return m_hijack_listener_sp; }
0109 
0110   void SetHijackListener(const lldb::ListenerSP &listener_sp) {
0111     m_hijack_listener_sp = listener_sp;
0112   }
0113 
0114   lldb::ListenerSP GetShadowListener() const { return m_shadow_listener_sp; }
0115 
0116   void SetShadowListener(const lldb::ListenerSP &listener_sp) {
0117     m_shadow_listener_sp = listener_sp;
0118   }
0119 
0120 protected:
0121   FileSpec m_executable;
0122   std::string m_arg0; // argv[0] if supported. If empty, then use m_executable.
0123   // Not all process plug-ins support specifying an argv[0] that differs from
0124   // the resolved platform executable (which is in m_executable)
0125   Args m_arguments; // All program arguments except argv[0]
0126   Environment m_environment;
0127   uint32_t m_uid = UINT32_MAX;
0128   uint32_t m_gid = UINT32_MAX;
0129   ArchSpec m_arch;
0130   lldb::pid_t m_pid = LLDB_INVALID_PROCESS_ID;
0131   lldb::ScriptedMetadataSP m_scripted_metadata_sp = nullptr;
0132   lldb::ListenerSP m_listener_sp = nullptr;
0133   lldb::ListenerSP m_hijack_listener_sp = nullptr;
0134   lldb::ListenerSP m_shadow_listener_sp = nullptr;
0135 };
0136 
0137 // ProcessInstanceInfo
0138 //
0139 // Describes an existing process and any discoverable information that pertains
0140 // to that process.
0141 class ProcessInstanceInfo : public ProcessInfo {
0142 public:
0143   struct timespec {
0144     time_t tv_sec = 0;
0145     long int tv_usec = 0;
0146   };
0147 
0148   ProcessInstanceInfo() = default;
0149 
0150   ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid)
0151       : ProcessInfo(name, arch, pid) {}
0152 
0153   void Clear() {
0154     ProcessInfo::Clear();
0155     m_euid = UINT32_MAX;
0156     m_egid = UINT32_MAX;
0157     m_parent_pid = LLDB_INVALID_PROCESS_ID;
0158   }
0159 
0160   uint32_t GetEffectiveUserID() const { return m_euid; }
0161 
0162   uint32_t GetEffectiveGroupID() const { return m_egid; }
0163 
0164   bool EffectiveUserIDIsValid() const { return m_euid != UINT32_MAX; }
0165 
0166   bool EffectiveGroupIDIsValid() const { return m_egid != UINT32_MAX; }
0167 
0168   void SetEffectiveUserID(uint32_t uid) { m_euid = uid; }
0169 
0170   void SetEffectiveGroupID(uint32_t gid) { m_egid = gid; }
0171 
0172   lldb::pid_t GetParentProcessID() const { return m_parent_pid; }
0173 
0174   void SetParentProcessID(lldb::pid_t pid) { m_parent_pid = pid; }
0175 
0176   bool ParentProcessIDIsValid() const {
0177     return m_parent_pid != LLDB_INVALID_PROCESS_ID;
0178   }
0179 
0180   lldb::pid_t GetProcessGroupID() const { return m_process_group_id; }
0181 
0182   void SetProcessGroupID(lldb::pid_t pgrp) { m_process_group_id = pgrp; }
0183 
0184   bool ProcessGroupIDIsValid() const {
0185     return m_process_group_id != LLDB_INVALID_PROCESS_ID;
0186   }
0187 
0188   lldb::pid_t GetProcessSessionID() const { return m_process_session_id; }
0189 
0190   void SetProcessSessionID(lldb::pid_t session) {
0191     m_process_session_id = session;
0192   }
0193 
0194   bool ProcessSessionIDIsValid() const {
0195     return m_process_session_id != LLDB_INVALID_PROCESS_ID;
0196   }
0197 
0198   struct timespec GetUserTime() const { return m_user_time; }
0199 
0200   void SetUserTime(struct timespec utime) { m_user_time = utime; }
0201 
0202   bool UserTimeIsValid() const {
0203     return m_user_time.tv_sec > 0 || m_user_time.tv_usec > 0;
0204   }
0205 
0206   struct timespec GetSystemTime() const { return m_system_time; }
0207 
0208   void SetSystemTime(struct timespec stime) { m_system_time = stime; }
0209 
0210   bool SystemTimeIsValid() const {
0211     return m_system_time.tv_sec > 0 || m_system_time.tv_usec > 0;
0212   }
0213 
0214   struct timespec GetCumulativeUserTime() const {
0215     return m_cumulative_user_time;
0216   }
0217 
0218   void SetCumulativeUserTime(struct timespec cutime) {
0219     m_cumulative_user_time = cutime;
0220   }
0221 
0222   bool CumulativeUserTimeIsValid() const {
0223     return m_cumulative_user_time.tv_sec > 0 ||
0224            m_cumulative_user_time.tv_usec > 0;
0225   }
0226 
0227   struct timespec GetCumulativeSystemTime() const {
0228     return m_cumulative_system_time;
0229   }
0230 
0231   void SetCumulativeSystemTime(struct timespec cstime) {
0232     m_cumulative_system_time = cstime;
0233   }
0234 
0235   bool CumulativeSystemTimeIsValid() const {
0236     return m_cumulative_system_time.tv_sec > 0 ||
0237            m_cumulative_system_time.tv_usec > 0;
0238   }
0239 
0240   std::optional<int8_t> GetPriorityValue() const { return m_priority_value; }
0241 
0242   void SetPriorityValue(int8_t priority_value) {
0243     m_priority_value = priority_value;
0244   }
0245 
0246   void SetIsZombie(bool is_zombie) { m_zombie = is_zombie; }
0247 
0248   std::optional<bool> IsZombie() const { return m_zombie; }
0249 
0250   void Dump(Stream &s, UserIDResolver &resolver) const;
0251 
0252   static void DumpTableHeader(Stream &s, bool show_args, bool verbose);
0253 
0254   void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args,
0255                       bool verbose) const;
0256 
0257 protected:
0258   uint32_t m_euid = UINT32_MAX;
0259   uint32_t m_egid = UINT32_MAX;
0260   lldb::pid_t m_parent_pid = LLDB_INVALID_PROCESS_ID;
0261   lldb::pid_t m_process_group_id = LLDB_INVALID_PROCESS_ID;
0262   lldb::pid_t m_process_session_id = LLDB_INVALID_PROCESS_ID;
0263   struct timespec m_user_time;
0264   struct timespec m_system_time;
0265   struct timespec m_cumulative_user_time;
0266   struct timespec m_cumulative_system_time;
0267   std::optional<int8_t> m_priority_value = std::nullopt;
0268   std::optional<bool> m_zombie = std::nullopt;
0269 };
0270 
0271 typedef std::vector<ProcessInstanceInfo> ProcessInstanceInfoList;
0272 
0273 class ProcessInfoList {
0274 public:
0275   ProcessInfoList(const ProcessInstanceInfoList &list) : m_list(list) {}
0276 
0277   uint32_t GetSize() const { return m_list.size(); }
0278 
0279   bool GetProcessInfoAtIndex(uint32_t idx, ProcessInstanceInfo &info) {
0280     if (idx < m_list.size()) {
0281       info = m_list[idx];
0282       return true;
0283     }
0284     return false;
0285   }
0286 
0287   void Clear() { return m_list.clear(); }
0288 
0289 private:
0290   ProcessInstanceInfoList m_list;
0291 };
0292 
0293 // ProcessInstanceInfoMatch
0294 //
0295 // A class to help matching one ProcessInstanceInfo to another.
0296 
0297 class ProcessInstanceInfoMatch {
0298 public:
0299   ProcessInstanceInfoMatch() = default;
0300 
0301   ProcessInstanceInfoMatch(const char *process_name,
0302                            NameMatch process_name_match_type)
0303       : m_name_match_type(process_name_match_type), m_match_all_users(false) {
0304     m_match_info.GetExecutableFile().SetFile(process_name,
0305                                              FileSpec::Style::native);
0306   }
0307 
0308   ProcessInstanceInfo &GetProcessInfo() { return m_match_info; }
0309 
0310   const ProcessInstanceInfo &GetProcessInfo() const { return m_match_info; }
0311 
0312   bool GetMatchAllUsers() const { return m_match_all_users; }
0313 
0314   void SetMatchAllUsers(bool b) { m_match_all_users = b; }
0315 
0316   NameMatch GetNameMatchType() const { return m_name_match_type; }
0317 
0318   void SetNameMatchType(NameMatch name_match_type) {
0319     m_name_match_type = name_match_type;
0320   }
0321 
0322   /// Return true iff the architecture in this object matches arch_spec.
0323   bool ArchitectureMatches(const ArchSpec &arch_spec) const;
0324 
0325   /// Return true iff the process name in this object matches process_name.
0326   bool NameMatches(const char *process_name) const;
0327 
0328   /// Return true iff the process ID and parent process IDs in this object match
0329   /// the ones in proc_info.
0330   bool ProcessIDsMatch(const ProcessInstanceInfo &proc_info) const;
0331 
0332   /// Return true iff the (both effective and real) user and group IDs in this
0333   /// object match the ones in proc_info.
0334   bool UserIDsMatch(const ProcessInstanceInfo &proc_info) const;
0335 
0336   bool Matches(const ProcessInstanceInfo &proc_info) const;
0337 
0338   bool MatchAllProcesses() const;
0339   void Clear();
0340 
0341 protected:
0342   ProcessInstanceInfo m_match_info;
0343   NameMatch m_name_match_type = NameMatch::Ignore;
0344   bool m_match_all_users = false;
0345 };
0346 
0347 } // namespace lldb_private
0348 
0349 #endif // LLDB_UTILITY_PROCESSINFO_H