diff --git a/lldb/include/lldb/Host/Host.h b/lldb/include/lldb/Host/Host.h index deea80a7227e..ab4b051ac945 100644 --- a/lldb/include/lldb/Host/Host.h +++ b/lldb/include/lldb/Host/Host.h @@ -26,6 +26,9 @@ namespace lldb_private { class FileAction; class ProcessLaunchInfo; +class ProcessInstanceInfo; +class ProcessInstanceInfoList; +class ProcessInstanceInfoMatch; //---------------------------------------------------------------------- // Exit Type for inferior processes diff --git a/lldb/include/lldb/Host/ProcessInfo.h b/lldb/include/lldb/Host/ProcessInfo.h deleted file mode 100644 index 818e29ef357e..000000000000 --- a/lldb/include/lldb/Host/ProcessInfo.h +++ /dev/null @@ -1,101 +0,0 @@ -//===-- ProcessInfo.h -------------------------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef liblldb_ProcessInfo_h_ -#define liblldb_ProcessInfo_h_ - -// LLDB headers -#include "lldb/Utility/ArchSpec.h" -#include "lldb/Utility/Args.h" -#include "lldb/Utility/Environment.h" -#include "lldb/Utility/FileSpec.h" - -namespace lldb_private { -//---------------------------------------------------------------------- -// ProcessInfo -// -// A base class for information for a process. This can be used to fill -// out information for a process prior to launching it, or it can be used for -// an instance of a process and can be filled in with the existing values for -// that process. -//---------------------------------------------------------------------- -class ProcessInfo { -public: - ProcessInfo(); - - ProcessInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid); - - void Clear(); - - const char *GetName() const; - - size_t GetNameLength() const; - - FileSpec &GetExecutableFile() { return m_executable; } - - void SetExecutableFile(const FileSpec &exe_file, - bool add_exe_file_as_first_arg); - - const FileSpec &GetExecutableFile() const { return m_executable; } - - uint32_t GetUserID() const { return m_uid; } - - uint32_t GetGroupID() const { return m_gid; } - - bool UserIDIsValid() const { return m_uid != UINT32_MAX; } - - bool GroupIDIsValid() const { return m_gid != UINT32_MAX; } - - void SetUserID(uint32_t uid) { m_uid = uid; } - - void SetGroupID(uint32_t gid) { m_gid = gid; } - - ArchSpec &GetArchitecture() { return m_arch; } - - const ArchSpec &GetArchitecture() const { return m_arch; } - - void SetArchitecture(const ArchSpec &arch) { m_arch = arch; } - - lldb::pid_t GetProcessID() const { return m_pid; } - - void SetProcessID(lldb::pid_t pid) { m_pid = pid; } - - bool ProcessIDIsValid() const { return m_pid != LLDB_INVALID_PROCESS_ID; } - - void Dump(Stream &s, Platform *platform) const; - - Args &GetArguments() { return m_arguments; } - - const Args &GetArguments() const { return m_arguments; } - - llvm::StringRef GetArg0() const; - - void SetArg0(llvm::StringRef arg); - - void SetArguments(const Args &args, bool first_arg_is_executable); - - void SetArguments(char const **argv, bool first_arg_is_executable); - - Environment &GetEnvironment() { return m_environment; } - const Environment &GetEnvironment() const { return m_environment; } - -protected: - FileSpec m_executable; - std::string m_arg0; // argv[0] if supported. If empty, then use m_executable. - // Not all process plug-ins support specifying an argv[0] that differs from - // the resolved platform executable (which is in m_executable) - Args m_arguments; // All program arguments except argv[0] - Environment m_environment; - uint32_t m_uid; - uint32_t m_gid; - ArchSpec m_arch; - lldb::pid_t m_pid; -}; -} - -#endif // #ifndef liblldb_ProcessInfo_h_ diff --git a/lldb/include/lldb/Host/ProcessLaunchInfo.h b/lldb/include/lldb/Host/ProcessLaunchInfo.h index 6c1419a792ca..94d0108f7475 100644 --- a/lldb/include/lldb/Host/ProcessLaunchInfo.h +++ b/lldb/include/lldb/Host/ProcessLaunchInfo.h @@ -17,9 +17,9 @@ #include "lldb/Host/FileAction.h" #include "lldb/Host/Host.h" -#include "lldb/Host/ProcessInfo.h" #include "lldb/Host/PseudoTerminal.h" #include "lldb/Utility/FileSpec.h" +#include "lldb/Utility/ProcessInfo.h" namespace lldb_private { diff --git a/lldb/include/lldb/Host/posix/HostInfoPosix.h b/lldb/include/lldb/Host/posix/HostInfoPosix.h index c6f012bca72a..45e86e22b10d 100644 --- a/lldb/include/lldb/Host/posix/HostInfoPosix.h +++ b/lldb/include/lldb/Host/posix/HostInfoPosix.h @@ -22,7 +22,6 @@ class HostInfoPosix : public HostInfoBase { public: static size_t GetPageSize(); static bool GetHostname(std::string &s); - static UserIDResolver &GetUserIDResolver(); static uint32_t GetUserID(); static uint32_t GetGroupID(); diff --git a/lldb/include/lldb/Target/Platform.h b/lldb/include/lldb/Target/Platform.h index f581ac3f9c7b..248bf1396cef 100644 --- a/lldb/include/lldb/Target/Platform.h +++ b/lldb/include/lldb/Target/Platform.h @@ -30,6 +30,10 @@ namespace lldb_private { +class ProcessInstanceInfo; +class ProcessInstanceInfoList; +class ProcessInstanceInfoMatch; + class ModuleCache; enum MmapFlags { eMmapFlagsPrivate = 1, eMmapFlagsAnon = 2 }; diff --git a/lldb/include/lldb/Target/Process.h b/lldb/include/lldb/Target/Process.h index 54ee084f4806..e86a8a1ad172 100644 --- a/lldb/include/lldb/Target/Process.h +++ b/lldb/include/lldb/Target/Process.h @@ -28,7 +28,6 @@ #include "lldb/Core/ThreadSafeValue.h" #include "lldb/Core/UserSettingsController.h" #include "lldb/Host/HostThread.h" -#include "lldb/Host/ProcessInfo.h" #include "lldb/Host/ProcessLaunchInfo.h" #include "lldb/Host/ProcessRunLock.h" #include "lldb/Interpreter/Options.h" @@ -43,6 +42,7 @@ #include "lldb/Utility/Event.h" #include "lldb/Utility/Listener.h" #include "lldb/Utility/NameMatches.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StructuredData.h" #include "lldb/Utility/TraceOptions.h" @@ -108,62 +108,6 @@ protected: typedef std::shared_ptr ProcessPropertiesSP; -//---------------------------------------------------------------------- -// ProcessInstanceInfo -// -// Describes an existing process and any discoverable information that pertains -// to that process. -//---------------------------------------------------------------------- -class ProcessInstanceInfo : public ProcessInfo { -public: - ProcessInstanceInfo() - : ProcessInfo(), m_euid(UINT32_MAX), m_egid(UINT32_MAX), - m_parent_pid(LLDB_INVALID_PROCESS_ID) {} - - ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid) - : ProcessInfo(name, arch, pid), m_euid(UINT32_MAX), m_egid(UINT32_MAX), - m_parent_pid(LLDB_INVALID_PROCESS_ID) {} - - void Clear() { - ProcessInfo::Clear(); - m_euid = UINT32_MAX; - m_egid = UINT32_MAX; - m_parent_pid = LLDB_INVALID_PROCESS_ID; - } - - uint32_t GetEffectiveUserID() const { return m_euid; } - - uint32_t GetEffectiveGroupID() const { return m_egid; } - - bool EffectiveUserIDIsValid() const { return m_euid != UINT32_MAX; } - - bool EffectiveGroupIDIsValid() const { return m_egid != UINT32_MAX; } - - void SetEffectiveUserID(uint32_t uid) { m_euid = uid; } - - void SetEffectiveGroupID(uint32_t gid) { m_egid = gid; } - - lldb::pid_t GetParentProcessID() const { return m_parent_pid; } - - void SetParentProcessID(lldb::pid_t pid) { m_parent_pid = pid; } - - bool ParentProcessIDIsValid() const { - return m_parent_pid != LLDB_INVALID_PROCESS_ID; - } - - void Dump(Stream &s, UserIDResolver &resolver) const; - - static void DumpTableHeader(Stream &s, bool show_args, bool verbose); - - void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args, - bool verbose) const; - -protected: - uint32_t m_euid; - uint32_t m_egid; - lldb::pid_t m_parent_pid; -}; - //---------------------------------------------------------------------- // ProcessAttachInfo // @@ -298,94 +242,6 @@ public: lldb_private::LazyBool disable_aslr; }; -//---------------------------------------------------------------------- -// ProcessInstanceInfoMatch -// -// A class to help matching one ProcessInstanceInfo to another. -//---------------------------------------------------------------------- - -class ProcessInstanceInfoMatch { -public: - ProcessInstanceInfoMatch() - : m_match_info(), m_name_match_type(NameMatch::Ignore), - m_match_all_users(false) {} - - ProcessInstanceInfoMatch(const char *process_name, - NameMatch process_name_match_type) - : m_match_info(), m_name_match_type(process_name_match_type), - m_match_all_users(false) { - m_match_info.GetExecutableFile().SetFile(process_name, - FileSpec::Style::native); - } - - ProcessInstanceInfo &GetProcessInfo() { return m_match_info; } - - const ProcessInstanceInfo &GetProcessInfo() const { return m_match_info; } - - bool GetMatchAllUsers() const { return m_match_all_users; } - - void SetMatchAllUsers(bool b) { m_match_all_users = b; } - - NameMatch GetNameMatchType() const { return m_name_match_type; } - - void SetNameMatchType(NameMatch name_match_type) { - m_name_match_type = name_match_type; - } - - bool NameMatches(const char *process_name) const; - - bool Matches(const ProcessInstanceInfo &proc_info) const; - - bool MatchAllProcesses() const; - void Clear(); - -protected: - ProcessInstanceInfo m_match_info; - NameMatch m_name_match_type; - bool m_match_all_users; -}; - -class ProcessInstanceInfoList { -public: - ProcessInstanceInfoList() = default; - - void Clear() { m_infos.clear(); } - - size_t GetSize() { return m_infos.size(); } - - void Append(const ProcessInstanceInfo &info) { m_infos.push_back(info); } - - const char *GetProcessNameAtIndex(size_t idx) { - return ((idx < m_infos.size()) ? m_infos[idx].GetName() : nullptr); - } - - size_t GetProcessNameLengthAtIndex(size_t idx) { - return ((idx < m_infos.size()) ? m_infos[idx].GetNameLength() : 0); - } - - lldb::pid_t GetProcessIDAtIndex(size_t idx) { - return ((idx < m_infos.size()) ? m_infos[idx].GetProcessID() : 0); - } - - bool GetInfoAtIndex(size_t idx, ProcessInstanceInfo &info) { - if (idx < m_infos.size()) { - info = m_infos[idx]; - return true; - } - return false; - } - - // You must ensure "idx" is valid before calling this function - const ProcessInstanceInfo &GetProcessInfoAtIndex(size_t idx) const { - assert(idx < m_infos.size()); - return m_infos[idx]; - } - -protected: - typedef std::vector collection; - collection m_infos; -}; - // This class tracks the Modification state of the process. Things that can // currently modify the program are running the program (which will up the // StopID) and writing memory (which will up the MemoryID.) @@ -2519,7 +2375,7 @@ public: /// //------------------------------------------------------------------ void RestoreProcessEvents(); - + bool StateChangedIsHijackedForSynchronousResume(); bool StateChangedIsExternallyHijacked(); diff --git a/lldb/include/lldb/Utility/ProcessInfo.h b/lldb/include/lldb/Utility/ProcessInfo.h new file mode 100644 index 000000000000..41161ef4c761 --- /dev/null +++ b/lldb/include/lldb/Utility/ProcessInfo.h @@ -0,0 +1,251 @@ +//===-- ProcessInfo.h -------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLDB_UTILITY_PROCESSINFO_H +#define LLDB_UTILITY_PROCESSINFO_H + +// LLDB headers +#include "lldb/Utility/ArchSpec.h" +#include "lldb/Utility/Args.h" +#include "lldb/Utility/Environment.h" +#include "lldb/Utility/FileSpec.h" +#include "lldb/Utility/NameMatches.h" + +#include + +namespace lldb_private { + +class UserIDResolver; + +//---------------------------------------------------------------------- +// ProcessInfo +// +// A base class for information for a process. This can be used to fill +// out information for a process prior to launching it, or it can be used for +// an instance of a process and can be filled in with the existing values for +// that process. +//---------------------------------------------------------------------- +class ProcessInfo { +public: + ProcessInfo(); + + ProcessInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid); + + void Clear(); + + const char *GetName() const; + + size_t GetNameLength() const; + + FileSpec &GetExecutableFile() { return m_executable; } + + void SetExecutableFile(const FileSpec &exe_file, + bool add_exe_file_as_first_arg); + + const FileSpec &GetExecutableFile() const { return m_executable; } + + uint32_t GetUserID() const { return m_uid; } + + uint32_t GetGroupID() const { return m_gid; } + + bool UserIDIsValid() const { return m_uid != UINT32_MAX; } + + bool GroupIDIsValid() const { return m_gid != UINT32_MAX; } + + void SetUserID(uint32_t uid) { m_uid = uid; } + + void SetGroupID(uint32_t gid) { m_gid = gid; } + + ArchSpec &GetArchitecture() { return m_arch; } + + const ArchSpec &GetArchitecture() const { return m_arch; } + + void SetArchitecture(const ArchSpec &arch) { m_arch = arch; } + + lldb::pid_t GetProcessID() const { return m_pid; } + + void SetProcessID(lldb::pid_t pid) { m_pid = pid; } + + bool ProcessIDIsValid() const { return m_pid != LLDB_INVALID_PROCESS_ID; } + + void Dump(Stream &s, Platform *platform) const; + + Args &GetArguments() { return m_arguments; } + + const Args &GetArguments() const { return m_arguments; } + + llvm::StringRef GetArg0() const; + + void SetArg0(llvm::StringRef arg); + + void SetArguments(const Args &args, bool first_arg_is_executable); + + void SetArguments(char const **argv, bool first_arg_is_executable); + + Environment &GetEnvironment() { return m_environment; } + const Environment &GetEnvironment() const { return m_environment; } + +protected: + FileSpec m_executable; + std::string m_arg0; // argv[0] if supported. If empty, then use m_executable. + // Not all process plug-ins support specifying an argv[0] that differs from + // the resolved platform executable (which is in m_executable) + Args m_arguments; // All program arguments except argv[0] + Environment m_environment; + uint32_t m_uid; + uint32_t m_gid; + ArchSpec m_arch; + lldb::pid_t m_pid; +}; + +//---------------------------------------------------------------------- +// ProcessInstanceInfo +// +// Describes an existing process and any discoverable information that pertains +// to that process. +//---------------------------------------------------------------------- +class ProcessInstanceInfo : public ProcessInfo { +public: + ProcessInstanceInfo() + : ProcessInfo(), m_euid(UINT32_MAX), m_egid(UINT32_MAX), + m_parent_pid(LLDB_INVALID_PROCESS_ID) {} + + ProcessInstanceInfo(const char *name, const ArchSpec &arch, lldb::pid_t pid) + : ProcessInfo(name, arch, pid), m_euid(UINT32_MAX), m_egid(UINT32_MAX), + m_parent_pid(LLDB_INVALID_PROCESS_ID) {} + + void Clear() { + ProcessInfo::Clear(); + m_euid = UINT32_MAX; + m_egid = UINT32_MAX; + m_parent_pid = LLDB_INVALID_PROCESS_ID; + } + + uint32_t GetEffectiveUserID() const { return m_euid; } + + uint32_t GetEffectiveGroupID() const { return m_egid; } + + bool EffectiveUserIDIsValid() const { return m_euid != UINT32_MAX; } + + bool EffectiveGroupIDIsValid() const { return m_egid != UINT32_MAX; } + + void SetEffectiveUserID(uint32_t uid) { m_euid = uid; } + + void SetEffectiveGroupID(uint32_t gid) { m_egid = gid; } + + lldb::pid_t GetParentProcessID() const { return m_parent_pid; } + + void SetParentProcessID(lldb::pid_t pid) { m_parent_pid = pid; } + + bool ParentProcessIDIsValid() const { + return m_parent_pid != LLDB_INVALID_PROCESS_ID; + } + + void Dump(Stream &s, UserIDResolver &resolver) const; + + static void DumpTableHeader(Stream &s, bool show_args, bool verbose); + + void DumpAsTableRow(Stream &s, UserIDResolver &resolver, bool show_args, + bool verbose) const; + +protected: + uint32_t m_euid; + uint32_t m_egid; + lldb::pid_t m_parent_pid; +}; + +class ProcessInstanceInfoList { +public: + ProcessInstanceInfoList() = default; + + void Clear() { m_infos.clear(); } + + size_t GetSize() { return m_infos.size(); } + + void Append(const ProcessInstanceInfo &info) { m_infos.push_back(info); } + + const char *GetProcessNameAtIndex(size_t idx) { + return ((idx < m_infos.size()) ? m_infos[idx].GetName() : nullptr); + } + + size_t GetProcessNameLengthAtIndex(size_t idx) { + return ((idx < m_infos.size()) ? m_infos[idx].GetNameLength() : 0); + } + + lldb::pid_t GetProcessIDAtIndex(size_t idx) { + return ((idx < m_infos.size()) ? m_infos[idx].GetProcessID() : 0); + } + + bool GetInfoAtIndex(size_t idx, ProcessInstanceInfo &info) { + if (idx < m_infos.size()) { + info = m_infos[idx]; + return true; + } + return false; + } + + // You must ensure "idx" is valid before calling this function + const ProcessInstanceInfo &GetProcessInfoAtIndex(size_t idx) const { + assert(idx < m_infos.size()); + return m_infos[idx]; + } + +protected: + std::vector m_infos; +}; + +//---------------------------------------------------------------------- +// ProcessInstanceInfoMatch +// +// A class to help matching one ProcessInstanceInfo to another. +//---------------------------------------------------------------------- + +class ProcessInstanceInfoMatch { +public: + ProcessInstanceInfoMatch() + : m_match_info(), m_name_match_type(NameMatch::Ignore), + m_match_all_users(false) {} + + ProcessInstanceInfoMatch(const char *process_name, + NameMatch process_name_match_type) + : m_match_info(), m_name_match_type(process_name_match_type), + m_match_all_users(false) { + m_match_info.GetExecutableFile().SetFile(process_name, + FileSpec::Style::native); + } + + ProcessInstanceInfo &GetProcessInfo() { return m_match_info; } + + const ProcessInstanceInfo &GetProcessInfo() const { return m_match_info; } + + bool GetMatchAllUsers() const { return m_match_all_users; } + + void SetMatchAllUsers(bool b) { m_match_all_users = b; } + + NameMatch GetNameMatchType() const { return m_name_match_type; } + + void SetNameMatchType(NameMatch name_match_type) { + m_name_match_type = name_match_type; + } + + bool NameMatches(const char *process_name) const; + + bool Matches(const ProcessInstanceInfo &proc_info) const; + + bool MatchAllProcesses() const; + void Clear(); + +protected: + ProcessInstanceInfo m_match_info; + NameMatch m_name_match_type; + bool m_match_all_users; +}; + +} // namespace lldb_private + +#endif // #ifndef LLDB_UTILITY_PROCESSINFO_H diff --git a/lldb/include/lldb/module.modulemap b/lldb/include/lldb/module.modulemap index e21bd7078196..66d2c3b3205e 100644 --- a/lldb/include/lldb/module.modulemap +++ b/lldb/include/lldb/module.modulemap @@ -39,7 +39,6 @@ module lldb_Host { module PipeBase { header "Host/PipeBase.h" export * } module Pipe { header "Host/Pipe.h" export * } module PosixApi { header "Host/PosixApi.h" export * } - module ProcessInfo { header "Host/ProcessInfo.h" export * } module ProcessLauncher { header "Host/ProcessLauncher.h" export * } module ProcessLaunchInfo { header "Host/ProcessLaunchInfo.h" export * } module ProcessRunLock { header "Host/ProcessRunLock.h" export * } diff --git a/lldb/source/API/SBProcess.cpp b/lldb/source/API/SBProcess.cpp index 8bd5691302c8..4ec235aecc87 100644 --- a/lldb/source/API/SBProcess.cpp +++ b/lldb/source/API/SBProcess.cpp @@ -25,6 +25,7 @@ #include "lldb/Target/Thread.h" #include "lldb/Utility/Args.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/State.h" #include "lldb/Utility/Stream.h" diff --git a/lldb/source/API/SBProcessInfo.cpp b/lldb/source/API/SBProcessInfo.cpp index 80eaebe41274..83f5eb1d680a 100644 --- a/lldb/source/API/SBProcessInfo.cpp +++ b/lldb/source/API/SBProcessInfo.cpp @@ -9,7 +9,7 @@ #include "lldb/API/SBProcessInfo.h" #include "lldb/API/SBFileSpec.h" -#include "lldb/Target/Process.h" +#include "lldb/Utility/ProcessInfo.h" using namespace lldb; using namespace lldb_private; diff --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp index b863b3d3b052..3b53d2dcd5d8 100644 --- a/lldb/source/API/SBTarget.cpp +++ b/lldb/source/API/SBTarget.cpp @@ -61,6 +61,7 @@ #include "lldb/Utility/Args.h" #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/RegularExpression.h" #include "Commands/CommandObjectBreakpoint.h" diff --git a/lldb/source/Host/CMakeLists.txt b/lldb/source/Host/CMakeLists.txt index 13e0a3573f32..93e4fd516bae 100644 --- a/lldb/source/Host/CMakeLists.txt +++ b/lldb/source/Host/CMakeLists.txt @@ -37,7 +37,6 @@ add_host_subdirectory(common common/NativeWatchpointList.cpp common/OptionParser.cpp common/PipeBase.cpp - common/ProcessInfo.cpp common/ProcessLaunchInfo.cpp common/ProcessRunLock.cpp common/PseudoTerminal.cpp diff --git a/lldb/source/Host/common/ProcessInfo.cpp b/lldb/source/Host/common/ProcessInfo.cpp deleted file mode 100644 index 76f521bebb51..000000000000 --- a/lldb/source/Host/common/ProcessInfo.cpp +++ /dev/null @@ -1,113 +0,0 @@ -//===-- ProcessInfo.cpp -----------------------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "lldb/Host/ProcessInfo.h" - -#include - -#include "lldb/Host/PosixApi.h" -#include "lldb/Utility/Stream.h" - -#include "llvm/ADT/SmallString.h" - -using namespace lldb; -using namespace lldb_private; - -ProcessInfo::ProcessInfo() - : m_executable(), m_arguments(), m_environment(), m_uid(UINT32_MAX), - m_gid(UINT32_MAX), m_arch(), m_pid(LLDB_INVALID_PROCESS_ID) {} - -ProcessInfo::ProcessInfo(const char *name, const ArchSpec &arch, - lldb::pid_t pid) - : m_executable(name), m_arguments(), m_environment(), m_uid(UINT32_MAX), - m_gid(UINT32_MAX), m_arch(arch), m_pid(pid) {} - -void ProcessInfo::Clear() { - m_executable.Clear(); - m_arguments.Clear(); - m_environment.clear(); - m_uid = UINT32_MAX; - m_gid = UINT32_MAX; - m_arch.Clear(); - m_pid = LLDB_INVALID_PROCESS_ID; -} - -const char *ProcessInfo::GetName() const { - return m_executable.GetFilename().GetCString(); -} - -size_t ProcessInfo::GetNameLength() const { - return m_executable.GetFilename().GetLength(); -} - -void ProcessInfo::Dump(Stream &s, Platform *platform) const { - s << "Executable: " << GetName() << "\n"; - s << "Triple: "; - m_arch.DumpTriple(s); - s << "\n"; - - s << "Arguments:\n"; - m_arguments.Dump(s); - - s.Format("Environment:\n{0}", m_environment); -} - -void ProcessInfo::SetExecutableFile(const FileSpec &exe_file, - bool add_exe_file_as_first_arg) { - if (exe_file) { - m_executable = exe_file; - if (add_exe_file_as_first_arg) { - llvm::SmallString<128> filename; - exe_file.GetPath(filename); - if (!filename.empty()) - m_arguments.InsertArgumentAtIndex(0, filename); - } - } else { - m_executable.Clear(); - } -} - -llvm::StringRef ProcessInfo::GetArg0() const { - return m_arg0; -} - -void ProcessInfo::SetArg0(llvm::StringRef arg) { - m_arg0 = arg; -} - -void ProcessInfo::SetArguments(char const **argv, - bool first_arg_is_executable) { - m_arguments.SetArguments(argv); - - // Is the first argument the executable? - if (first_arg_is_executable) { - const char *first_arg = m_arguments.GetArgumentAtIndex(0); - if (first_arg) { - // Yes the first argument is an executable, set it as the executable in - // the launch options. Don't resolve the file path as the path could be a - // remote platform path - m_executable.SetFile(first_arg, FileSpec::Style::native); - } - } -} - -void ProcessInfo::SetArguments(const Args &args, bool first_arg_is_executable) { - // Copy all arguments - m_arguments = args; - - // Is the first argument the executable? - if (first_arg_is_executable) { - const char *first_arg = m_arguments.GetArgumentAtIndex(0); - if (first_arg) { - // Yes the first argument is an executable, set it as the executable in - // the launch options. Don't resolve the file path as the path could be a - // remote platform path - m_executable.SetFile(first_arg, FileSpec::Style::native); - } - } -} diff --git a/lldb/source/Host/freebsd/Host.cpp b/lldb/source/Host/freebsd/Host.cpp index 8b3c6ad186b5..99d728d63bc0 100644 --- a/lldb/source/Host/freebsd/Host.cpp +++ b/lldb/source/Host/freebsd/Host.cpp @@ -23,12 +23,12 @@ #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" -#include "lldb/Target/Process.h" #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Endian.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/NameMatches.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StreamString.h" @@ -38,6 +38,10 @@ extern "C" { extern char **environ; } +namespace lldb_private { +class ProcessLaunchInfo; +} + using namespace lldb; using namespace lldb_private; diff --git a/lldb/source/Host/linux/Host.cpp b/lldb/source/Host/linux/Host.cpp index e67558df0adb..f94abd03a6f0 100644 --- a/lldb/source/Host/linux/Host.cpp +++ b/lldb/source/Host/linux/Host.cpp @@ -19,7 +19,6 @@ #include "llvm/Object/ELF.h" #include "llvm/Support/ScopedPrinter.h" -#include "lldb/Target/Process.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/Status.h" @@ -44,6 +43,10 @@ enum class ProcessState { }; } +namespace lldb_private { +class ProcessLaunchInfo; +} + static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo, ProcessState &State, ::pid_t &TracerPid) { auto BufferOrError = getProcFile(Pid, "status"); diff --git a/lldb/source/Host/macosx/objcxx/Host.mm b/lldb/source/Host/macosx/objcxx/Host.mm index 94a0a5b25b92..8b58b28802c4 100644 --- a/lldb/source/Host/macosx/objcxx/Host.mm +++ b/lldb/source/Host/macosx/objcxx/Host.mm @@ -58,7 +58,6 @@ #include "lldb/Host/HostInfo.h" #include "lldb/Host/ProcessLaunchInfo.h" #include "lldb/Host/ThreadLauncher.h" -#include "lldb/Target/Process.h" #include "lldb/Utility/ArchSpec.h" #include "lldb/Utility/CleanUp.h" #include "lldb/Utility/DataBufferHeap.h" @@ -67,6 +66,7 @@ #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/NameMatches.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/StreamString.h" #include "lldb/Utility/StructuredData.h" #include "lldb/lldb-defines.h" diff --git a/lldb/source/Host/netbsd/Host.cpp b/lldb/source/Host/netbsd/Host.cpp index b24ec75ad27a..08fec099bf49 100644 --- a/lldb/source/Host/netbsd/Host.cpp +++ b/lldb/source/Host/netbsd/Host.cpp @@ -22,12 +22,12 @@ #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" -#include "lldb/Target/Process.h" #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Endian.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/NameMatches.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StreamString.h" @@ -40,6 +40,10 @@ extern char **environ; using namespace lldb; using namespace lldb_private; +namespace lldb_private { +class ProcessLaunchInfo; +} + Environment Host::GetEnvironment() { return Environment(environ); } static bool GetNetBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr, diff --git a/lldb/source/Host/openbsd/Host.cpp b/lldb/source/Host/openbsd/Host.cpp index d91a01b68a51..ba6cf057ca17 100644 --- a/lldb/source/Host/openbsd/Host.cpp +++ b/lldb/source/Host/openbsd/Host.cpp @@ -19,12 +19,12 @@ #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" -#include "lldb/Target/Process.h" #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Endian.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/NameMatches.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StreamString.h" @@ -37,6 +37,10 @@ extern char **environ; using namespace lldb; using namespace lldb_private; +namespace lldb_private { +class ProcessLaunchInfo; +} + Environment Host::GetEnvironment() { Environment env; char *v; diff --git a/lldb/source/Host/posix/HostInfoPosix.cpp b/lldb/source/Host/posix/HostInfoPosix.cpp index 864d6c9a252b..94bc7ebe542f 100644 --- a/lldb/source/Host/posix/HostInfoPosix.cpp +++ b/lldb/source/Host/posix/HostInfoPosix.cpp @@ -105,7 +105,7 @@ llvm::Optional PosixUserIDResolver::DoGetGroupName(id_t gid) { static llvm::ManagedStatic g_user_id_resolver; -UserIDResolver &HostInfoPosix::GetUserIDResolver() { +UserIDResolver &HostInfoBase::GetUserIDResolver() { return *g_user_id_resolver; } diff --git a/lldb/source/Host/windows/Host.cpp b/lldb/source/Host/windows/Host.cpp index 32ea39ae1cbb..2b73beb0c789 100644 --- a/lldb/source/Host/windows/Host.cpp +++ b/lldb/source/Host/windows/Host.cpp @@ -13,11 +13,13 @@ #include "lldb/Host/FileSystem.h" #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" -#include "lldb/Target/Process.h" +#include "lldb/Host/ProcessLaunchInfo.h" #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" +#include "lldb/Utility/StreamString.h" #include "lldb/Utility/StructuredData.h" #include "llvm/Support/ConvertUTF.h" diff --git a/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp b/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp index 24a42d7baf02..6583a809efcc 100644 --- a/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp +++ b/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp @@ -20,11 +20,11 @@ #include "lldb/Symbol/ObjectFile.h" #include "lldb/Target/MemoryRegionInfo.h" #include "lldb/Target/Platform.h" -#include "lldb/Target/Process.h" #include "lldb/Target/Target.h" #include "lldb/Target/Thread.h" #include "lldb/Target/ThreadPlanRunToAddress.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/ProcessInfo.h" #include diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp index 6935ac6053c1..81dc1edf79cd 100644 --- a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp +++ b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp @@ -15,11 +15,11 @@ #include "lldb/Core/PluginManager.h" #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" -#include "lldb/Target/Process.h" #include "lldb/Target/Target.h" #include "lldb/Utility/ArchSpec.h" #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StreamString.h" @@ -28,6 +28,10 @@ using namespace lldb; using namespace lldb_private; +namespace lldb_private { +class Process; +} + //------------------------------------------------------------------ // Static Variables //------------------------------------------------------------------ diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp index fb2f1316383e..51a88e6cb4dd 100644 --- a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp +++ b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp @@ -16,16 +16,20 @@ #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" #include "lldb/Target/Process.h" -#include "lldb/Target/Target.h" #include "lldb/Utility/ArchSpec.h" #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StreamString.h" using namespace lldb; using namespace lldb_private; +namespace lldb_private { +class Process; +} + //------------------------------------------------------------------ // Static Variables //------------------------------------------------------------------ diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp index e3cf3f7d36f6..692b45178306 100644 --- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp +++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp @@ -31,6 +31,7 @@ #include "lldb/Target/Process.h" #include "lldb/Target/Target.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/Timer.h" #include "llvm/ADT/STLExtras.h" diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp index 1ba9419b07ac..6acaa5af6570 100644 --- a/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp +++ b/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp @@ -16,11 +16,11 @@ #include "lldb/Core/PluginManager.h" #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" -#include "lldb/Target/Process.h" #include "lldb/Target/Target.h" #include "lldb/Utility/ArchSpec.h" #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StreamString.h" @@ -29,6 +29,10 @@ using namespace lldb; using namespace lldb_private; +namespace lldb_private { +class Process; +} + //------------------------------------------------------------------ // Static Variables //------------------------------------------------------------------ diff --git a/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp b/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp index 4ced723b0b2d..c73e29ef9afc 100644 --- a/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp +++ b/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp @@ -24,6 +24,7 @@ #include "lldb/Target/Target.h" #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/Log.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StreamString.h" #include "lldb/Utility/UriParser.h" diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp index 2723b6985175..d74aaf1e4e70 100644 --- a/lldb/source/Target/Process.cpp +++ b/lldb/source/Target/Process.cpp @@ -64,6 +64,7 @@ #include "lldb/Utility/Event.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/NameMatches.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/SelectHelper.h" #include "lldb/Utility/State.h" @@ -278,123 +279,6 @@ bool ProcessProperties::GetStopOnExec() const { nullptr, idx, g_properties[idx].default_uint_value != 0); } -void ProcessInstanceInfo::Dump(Stream &s, UserIDResolver &resolver) const { - if (m_pid != LLDB_INVALID_PROCESS_ID) - s.Printf(" pid = %" PRIu64 "\n", m_pid); - - if (m_parent_pid != LLDB_INVALID_PROCESS_ID) - s.Printf(" parent = %" PRIu64 "\n", m_parent_pid); - - if (m_executable) { - s.Printf(" name = %s\n", m_executable.GetFilename().GetCString()); - s.PutCString(" file = "); - m_executable.Dump(&s); - s.EOL(); - } - const uint32_t argc = m_arguments.GetArgumentCount(); - if (argc > 0) { - for (uint32_t i = 0; i < argc; i++) { - const char *arg = m_arguments.GetArgumentAtIndex(i); - if (i < 10) - s.Printf(" arg[%u] = %s\n", i, arg); - else - s.Printf("arg[%u] = %s\n", i, arg); - } - } - - s.Format("{0}", m_environment); - - if (m_arch.IsValid()) { - s.Printf(" arch = "); - m_arch.DumpTriple(s); - s.EOL(); - } - - if (UserIDIsValid()) { - s.Format(" uid = {0,-5} ({1})\n", GetUserID(), - resolver.GetUserName(GetUserID()).getValueOr("")); - } - if (GroupIDIsValid()) { - s.Format(" gid = {0,-5} ({1})\n", GetGroupID(), - resolver.GetGroupName(GetGroupID()).getValueOr("")); - } - if (EffectiveUserIDIsValid()) { - s.Format(" euid = {0,-5} ({1})\n", GetEffectiveUserID(), - resolver.GetUserName(GetEffectiveUserID()).getValueOr("")); - } - if (EffectiveGroupIDIsValid()) { - s.Format(" egid = {0,-5} ({1})\n", GetEffectiveGroupID(), - resolver.GetGroupName(GetEffectiveGroupID()).getValueOr("")); - } -} - -void ProcessInstanceInfo::DumpTableHeader(Stream &s, bool show_args, - bool verbose) { - const char *label; - if (show_args || verbose) - label = "ARGUMENTS"; - else - label = "NAME"; - - if (verbose) { - s.Printf("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE " - " %s\n", - label); - s.PutCString("====== ====== ========== ========== ========== ========== " - "======================== ============================\n"); - } else { - s.Printf("PID PARENT USER TRIPLE %s\n", label); - s.PutCString("====== ====== ========== ======================== " - "============================\n"); - } -} - -void ProcessInstanceInfo::DumpAsTableRow(Stream &s, UserIDResolver &resolver, - bool show_args, bool verbose) const { - if (m_pid != LLDB_INVALID_PROCESS_ID) { - s.Printf("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid); - - StreamString arch_strm; - if (m_arch.IsValid()) - m_arch.DumpTriple(arch_strm); - - auto print = [&](UserIDResolver::id_t id, - llvm::Optional (UserIDResolver::*get)( - UserIDResolver::id_t id)) { - if (auto name = (resolver.*get)(id)) - s.Format("{0,-10} ", *name); - else - s.Format("{0,-10} ", id); - }; - if (verbose) { - print(m_uid, &UserIDResolver::GetUserName); - print(m_gid, &UserIDResolver::GetGroupName); - print(m_euid, &UserIDResolver::GetUserName); - print(m_egid, &UserIDResolver::GetGroupName); - - s.Printf("%-24s ", arch_strm.GetData()); - } else { - print(m_euid, &UserIDResolver::GetUserName); - s.Printf(" %-24s ", arch_strm.GetData()); - } - - if (verbose || show_args) { - const uint32_t argc = m_arguments.GetArgumentCount(); - if (argc > 0) { - for (uint32_t i = 0; i < argc; i++) { - if (i > 0) - s.PutChar(' '); - s.PutCString(m_arguments.GetArgumentAtIndex(i)); - } - } - } else { - s.PutCString(GetName()); - } - - s.EOL(); - } -} - Status ProcessLaunchCommandOptions::SetOptionValue( uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) { @@ -564,89 +448,6 @@ llvm::ArrayRef ProcessLaunchCommandOptions::GetDefinitions() { return llvm::makeArrayRef(g_process_launch_options); } -bool ProcessInstanceInfoMatch::NameMatches(const char *process_name) const { - if (m_name_match_type == NameMatch::Ignore || process_name == nullptr) - return true; - const char *match_name = m_match_info.GetName(); - if (!match_name) - return true; - - return lldb_private::NameMatches(process_name, m_name_match_type, match_name); -} - -bool ProcessInstanceInfoMatch::Matches( - const ProcessInstanceInfo &proc_info) const { - if (!NameMatches(proc_info.GetName())) - return false; - - if (m_match_info.ProcessIDIsValid() && - m_match_info.GetProcessID() != proc_info.GetProcessID()) - return false; - - if (m_match_info.ParentProcessIDIsValid() && - m_match_info.GetParentProcessID() != proc_info.GetParentProcessID()) - return false; - - if (m_match_info.UserIDIsValid() && - m_match_info.GetUserID() != proc_info.GetUserID()) - return false; - - if (m_match_info.GroupIDIsValid() && - m_match_info.GetGroupID() != proc_info.GetGroupID()) - return false; - - if (m_match_info.EffectiveUserIDIsValid() && - m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID()) - return false; - - if (m_match_info.EffectiveGroupIDIsValid() && - m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID()) - return false; - - if (m_match_info.GetArchitecture().IsValid() && - !m_match_info.GetArchitecture().IsCompatibleMatch( - proc_info.GetArchitecture())) - return false; - return true; -} - -bool ProcessInstanceInfoMatch::MatchAllProcesses() const { - if (m_name_match_type != NameMatch::Ignore) - return false; - - if (m_match_info.ProcessIDIsValid()) - return false; - - if (m_match_info.ParentProcessIDIsValid()) - return false; - - if (m_match_info.UserIDIsValid()) - return false; - - if (m_match_info.GroupIDIsValid()) - return false; - - if (m_match_info.EffectiveUserIDIsValid()) - return false; - - if (m_match_info.EffectiveGroupIDIsValid()) - return false; - - if (m_match_info.GetArchitecture().IsValid()) - return false; - - if (m_match_all_users) - return false; - - return true; -} - -void ProcessInstanceInfoMatch::Clear() { - m_match_info.Clear(); - m_name_match_type = NameMatch::Ignore; - m_match_all_users = false; -} - ProcessSP Process::FindPlugin(lldb::TargetSP target_sp, llvm::StringRef plugin_name, ListenerSP listener_sp, @@ -4267,11 +4068,11 @@ void Process::ProcessEventData::DoOnRemoval(Event *event_ptr) { process_sp->GetTarget().RunStopHooks(); if (process_sp->GetPrivateState() == eStateRunning) SetRestarted(true); - } } } } } +} void Process::ProcessEventData::Dump(Stream *s) const { ProcessSP process_sp(m_process_wp.lock()); diff --git a/lldb/source/Utility/CMakeLists.txt b/lldb/source/Utility/CMakeLists.txt index 34b6fb27e644..254fb41b76d6 100644 --- a/lldb/source/Utility/CMakeLists.txt +++ b/lldb/source/Utility/CMakeLists.txt @@ -64,6 +64,7 @@ add_lldb_library(lldbUtility Log.cpp Logging.cpp NameMatches.cpp + ProcessInfo.cpp RegisterValue.cpp RegularExpression.cpp Reproducer.cpp diff --git a/lldb/source/Utility/ProcessInfo.cpp b/lldb/source/Utility/ProcessInfo.cpp new file mode 100644 index 000000000000..ad3c47e8597b --- /dev/null +++ b/lldb/source/Utility/ProcessInfo.cpp @@ -0,0 +1,310 @@ +//===-- ProcessInstance.cpp -------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "lldb/Utility/ProcessInfo.h" + +#include "lldb/Utility/ArchSpec.h" +#include "lldb/Utility/Stream.h" +#include "lldb/Utility/StreamString.h" +#include "lldb/Utility/UserIDResolver.h" +#include "llvm/ADT/SmallString.h" + +#include + +using namespace lldb; +using namespace lldb_private; + +ProcessInfo::ProcessInfo() + : m_executable(), m_arguments(), m_environment(), m_uid(UINT32_MAX), + m_gid(UINT32_MAX), m_arch(), m_pid(LLDB_INVALID_PROCESS_ID) {} + +ProcessInfo::ProcessInfo(const char *name, const ArchSpec &arch, + lldb::pid_t pid) + : m_executable(name), m_arguments(), m_environment(), m_uid(UINT32_MAX), + m_gid(UINT32_MAX), m_arch(arch), m_pid(pid) {} + +void ProcessInfo::Clear() { + m_executable.Clear(); + m_arguments.Clear(); + m_environment.clear(); + m_uid = UINT32_MAX; + m_gid = UINT32_MAX; + m_arch.Clear(); + m_pid = LLDB_INVALID_PROCESS_ID; +} + +const char *ProcessInfo::GetName() const { + return m_executable.GetFilename().GetCString(); +} + +size_t ProcessInfo::GetNameLength() const { + return m_executable.GetFilename().GetLength(); +} + +void ProcessInfo::Dump(Stream &s, Platform *platform) const { + s << "Executable: " << GetName() << "\n"; + s << "Triple: "; + m_arch.DumpTriple(s); + s << "\n"; + + s << "Arguments:\n"; + m_arguments.Dump(s); + + s.Format("Environment:\n{0}", m_environment); +} + +void ProcessInfo::SetExecutableFile(const FileSpec &exe_file, + bool add_exe_file_as_first_arg) { + if (exe_file) { + m_executable = exe_file; + if (add_exe_file_as_first_arg) { + llvm::SmallString<128> filename; + exe_file.GetPath(filename); + if (!filename.empty()) + m_arguments.InsertArgumentAtIndex(0, filename); + } + } else { + m_executable.Clear(); + } +} + +llvm::StringRef ProcessInfo::GetArg0() const { return m_arg0; } + +void ProcessInfo::SetArg0(llvm::StringRef arg) { m_arg0 = arg; } + +void ProcessInfo::SetArguments(char const **argv, + bool first_arg_is_executable) { + m_arguments.SetArguments(argv); + + // Is the first argument the executable? + if (first_arg_is_executable) { + const char *first_arg = m_arguments.GetArgumentAtIndex(0); + if (first_arg) { + // Yes the first argument is an executable, set it as the executable in + // the launch options. Don't resolve the file path as the path could be a + // remote platform path + m_executable.SetFile(first_arg, FileSpec::Style::native); + } + } +} + +void ProcessInfo::SetArguments(const Args &args, bool first_arg_is_executable) { + // Copy all arguments + m_arguments = args; + + // Is the first argument the executable? + if (first_arg_is_executable) { + const char *first_arg = m_arguments.GetArgumentAtIndex(0); + if (first_arg) { + // Yes the first argument is an executable, set it as the executable in + // the launch options. Don't resolve the file path as the path could be a + // remote platform path + m_executable.SetFile(first_arg, FileSpec::Style::native); + } + } +} + +void ProcessInstanceInfo::Dump(Stream &s, UserIDResolver &resolver) const { + if (m_pid != LLDB_INVALID_PROCESS_ID) + s.Printf(" pid = %" PRIu64 "\n", m_pid); + + if (m_parent_pid != LLDB_INVALID_PROCESS_ID) + s.Printf(" parent = %" PRIu64 "\n", m_parent_pid); + + if (m_executable) { + s.Printf(" name = %s\n", m_executable.GetFilename().GetCString()); + s.PutCString(" file = "); + m_executable.Dump(&s); + s.EOL(); + } + const uint32_t argc = m_arguments.GetArgumentCount(); + if (argc > 0) { + for (uint32_t i = 0; i < argc; i++) { + const char *arg = m_arguments.GetArgumentAtIndex(i); + if (i < 10) + s.Printf(" arg[%u] = %s\n", i, arg); + else + s.Printf("arg[%u] = %s\n", i, arg); + } + } + + s.Format("{0}", m_environment); + + if (m_arch.IsValid()) { + s.Printf(" arch = "); + m_arch.DumpTriple(s); + s.EOL(); + } + + if (UserIDIsValid()) { + s.Format(" uid = {0,-5} ({1})\n", GetUserID(), + resolver.GetUserName(GetUserID()).getValueOr("")); + } + if (GroupIDIsValid()) { + s.Format(" gid = {0,-5} ({1})\n", GetGroupID(), + resolver.GetGroupName(GetGroupID()).getValueOr("")); + } + if (EffectiveUserIDIsValid()) { + s.Format(" euid = {0,-5} ({1})\n", GetEffectiveUserID(), + resolver.GetUserName(GetEffectiveUserID()).getValueOr("")); + } + if (EffectiveGroupIDIsValid()) { + s.Format(" egid = {0,-5} ({1})\n", GetEffectiveGroupID(), + resolver.GetGroupName(GetEffectiveGroupID()).getValueOr("")); + } +} + +void ProcessInstanceInfo::DumpTableHeader(Stream &s, bool show_args, + bool verbose) { + const char *label; + if (show_args || verbose) + label = "ARGUMENTS"; + else + label = "NAME"; + + if (verbose) { + s.Printf("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE " + " %s\n", + label); + s.PutCString("====== ====== ========== ========== ========== ========== " + "======================== ============================\n"); + } else { + s.Printf("PID PARENT USER TRIPLE %s\n", label); + s.PutCString("====== ====== ========== ======================== " + "============================\n"); + } +} + +void ProcessInstanceInfo::DumpAsTableRow(Stream &s, UserIDResolver &resolver, + bool show_args, bool verbose) const { + if (m_pid != LLDB_INVALID_PROCESS_ID) { + s.Printf("%-6" PRIu64 " %-6" PRIu64 " ", m_pid, m_parent_pid); + + StreamString arch_strm; + if (m_arch.IsValid()) + m_arch.DumpTriple(arch_strm); + + auto print = [&](UserIDResolver::id_t id, + llvm::Optional (UserIDResolver::*get)( + UserIDResolver::id_t id)) { + if (auto name = (resolver.*get)(id)) + s.Format("{0,-10} ", *name); + else + s.Format("{0,-10} ", id); + }; + if (verbose) { + print(m_uid, &UserIDResolver::GetUserName); + print(m_gid, &UserIDResolver::GetGroupName); + print(m_euid, &UserIDResolver::GetUserName); + print(m_egid, &UserIDResolver::GetGroupName); + + s.Printf("%-24s ", arch_strm.GetData()); + } else { + print(m_euid, &UserIDResolver::GetUserName); + s.Printf(" %-24s ", arch_strm.GetData()); + } + + if (verbose || show_args) { + const uint32_t argc = m_arguments.GetArgumentCount(); + if (argc > 0) { + for (uint32_t i = 0; i < argc; i++) { + if (i > 0) + s.PutChar(' '); + s.PutCString(m_arguments.GetArgumentAtIndex(i)); + } + } + } else { + s.PutCString(GetName()); + } + + s.EOL(); + } +} + +bool ProcessInstanceInfoMatch::NameMatches(const char *process_name) const { + if (m_name_match_type == NameMatch::Ignore || process_name == nullptr) + return true; + const char *match_name = m_match_info.GetName(); + if (!match_name) + return true; + + return lldb_private::NameMatches(process_name, m_name_match_type, match_name); +} + +bool ProcessInstanceInfoMatch::Matches( + const ProcessInstanceInfo &proc_info) const { + if (!NameMatches(proc_info.GetName())) + return false; + + if (m_match_info.ProcessIDIsValid() && + m_match_info.GetProcessID() != proc_info.GetProcessID()) + return false; + + if (m_match_info.ParentProcessIDIsValid() && + m_match_info.GetParentProcessID() != proc_info.GetParentProcessID()) + return false; + + if (m_match_info.UserIDIsValid() && + m_match_info.GetUserID() != proc_info.GetUserID()) + return false; + + if (m_match_info.GroupIDIsValid() && + m_match_info.GetGroupID() != proc_info.GetGroupID()) + return false; + + if (m_match_info.EffectiveUserIDIsValid() && + m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID()) + return false; + + if (m_match_info.EffectiveGroupIDIsValid() && + m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID()) + return false; + + if (m_match_info.GetArchitecture().IsValid() && + !m_match_info.GetArchitecture().IsCompatibleMatch( + proc_info.GetArchitecture())) + return false; + return true; +} + +bool ProcessInstanceInfoMatch::MatchAllProcesses() const { + if (m_name_match_type != NameMatch::Ignore) + return false; + + if (m_match_info.ProcessIDIsValid()) + return false; + + if (m_match_info.ParentProcessIDIsValid()) + return false; + + if (m_match_info.UserIDIsValid()) + return false; + + if (m_match_info.GroupIDIsValid()) + return false; + + if (m_match_info.EffectiveUserIDIsValid()) + return false; + + if (m_match_info.EffectiveGroupIDIsValid()) + return false; + + if (m_match_info.GetArchitecture().IsValid()) + return false; + + if (m_match_all_users) + return false; + + return true; +} + +void ProcessInstanceInfoMatch::Clear() { + m_match_info.Clear(); + m_name_match_type = NameMatch::Ignore; + m_match_all_users = false; +} diff --git a/lldb/unittests/Host/CMakeLists.txt b/lldb/unittests/Host/CMakeLists.txt index 54274b895e06..e216e6d65cc0 100644 --- a/lldb/unittests/Host/CMakeLists.txt +++ b/lldb/unittests/Host/CMakeLists.txt @@ -5,7 +5,6 @@ set (FILES HostTest.cpp MainLoopTest.cpp NativeProcessProtocolTest.cpp - ProcessInfoTest.cpp ProcessLaunchInfoTest.cpp SocketAddressTest.cpp SocketTest.cpp diff --git a/lldb/unittests/Utility/CMakeLists.txt b/lldb/unittests/Utility/CMakeLists.txt index 093dc726d87b..37bcb9ff9d0a 100644 --- a/lldb/unittests/Utility/CMakeLists.txt +++ b/lldb/unittests/Utility/CMakeLists.txt @@ -18,6 +18,7 @@ add_lldb_unittest(UtilityTests LogTest.cpp NameMatchesTest.cpp PredicateTest.cpp + ProcessInfoTest.cpp RegisterValueTest.cpp ReproducerTest.cpp ReproducerInstrumentationTest.cpp diff --git a/lldb/unittests/Host/ProcessInfoTest.cpp b/lldb/unittests/Utility/ProcessInfoTest.cpp similarity index 94% rename from lldb/unittests/Host/ProcessInfoTest.cpp rename to lldb/unittests/Utility/ProcessInfoTest.cpp index 1dbf57bed7e6..3a8e00104759 100644 --- a/lldb/unittests/Host/ProcessInfoTest.cpp +++ b/lldb/unittests/Utility/ProcessInfoTest.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "lldb/Host/ProcessInfo.h" +#include "lldb/Utility/ProcessInfo.h" #include "gtest/gtest.h" using namespace lldb_private;