Index: lldb/trunk/include/lldb/Host/Host.h =================================================================== --- lldb/trunk/include/lldb/Host/Host.h +++ lldb/trunk/include/lldb/Host/Host.h @@ -26,6 +26,9 @@ class FileAction; class ProcessLaunchInfo; +class ProcessInstanceInfo; +class ProcessInstanceInfoList; +class ProcessInstanceInfoMatch; //---------------------------------------------------------------------- // Exit Type for inferior processes Index: lldb/trunk/include/lldb/Host/ProcessInfo.h =================================================================== --- lldb/trunk/include/lldb/Host/ProcessInfo.h +++ lldb/trunk/include/lldb/Host/ProcessInfo.h @@ -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_ Index: lldb/trunk/include/lldb/Host/ProcessLaunchInfo.h =================================================================== --- lldb/trunk/include/lldb/Host/ProcessLaunchInfo.h +++ lldb/trunk/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 { Index: lldb/trunk/include/lldb/Host/posix/HostInfoPosix.h =================================================================== --- lldb/trunk/include/lldb/Host/posix/HostInfoPosix.h +++ lldb/trunk/include/lldb/Host/posix/HostInfoPosix.h @@ -22,7 +22,6 @@ public: static size_t GetPageSize(); static bool GetHostname(std::string &s); - static UserIDResolver &GetUserIDResolver(); static uint32_t GetUserID(); static uint32_t GetGroupID(); Index: lldb/trunk/include/lldb/Target/Platform.h =================================================================== --- lldb/trunk/include/lldb/Target/Platform.h +++ lldb/trunk/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 }; Index: lldb/trunk/include/lldb/Target/Process.h =================================================================== --- lldb/trunk/include/lldb/Target/Process.h +++ lldb/trunk/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" @@ -109,62 +109,6 @@ 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 // // Describes any information that is required to attach to a process. @@ -298,94 +242,6 @@ 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 @@ /// //------------------------------------------------------------------ void RestoreProcessEvents(); - + bool StateChangedIsHijackedForSynchronousResume(); bool StateChangedIsExternallyHijacked(); Index: lldb/trunk/include/lldb/Utility/ProcessInfo.h =================================================================== --- lldb/trunk/include/lldb/Utility/ProcessInfo.h +++ lldb/trunk/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 Index: lldb/trunk/include/lldb/module.modulemap =================================================================== --- lldb/trunk/include/lldb/module.modulemap +++ lldb/trunk/include/lldb/module.modulemap @@ -39,7 +39,6 @@ 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 * } Index: lldb/trunk/source/API/SBProcess.cpp =================================================================== --- lldb/trunk/source/API/SBProcess.cpp +++ lldb/trunk/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" Index: lldb/trunk/source/API/SBProcessInfo.cpp =================================================================== --- lldb/trunk/source/API/SBProcessInfo.cpp +++ lldb/trunk/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; Index: lldb/trunk/source/API/SBTarget.cpp =================================================================== --- lldb/trunk/source/API/SBTarget.cpp +++ lldb/trunk/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" Index: lldb/trunk/source/Host/CMakeLists.txt =================================================================== --- lldb/trunk/source/Host/CMakeLists.txt +++ lldb/trunk/source/Host/CMakeLists.txt @@ -37,7 +37,6 @@ common/NativeWatchpointList.cpp common/OptionParser.cpp common/PipeBase.cpp - common/ProcessInfo.cpp common/ProcessLaunchInfo.cpp common/ProcessRunLock.cpp common/PseudoTerminal.cpp Index: lldb/trunk/source/Host/common/ProcessInfo.cpp =================================================================== --- lldb/trunk/source/Host/common/ProcessInfo.cpp +++ lldb/trunk/source/Host/common/ProcessInfo.cpp @@ -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); - } - } -} Index: lldb/trunk/source/Host/freebsd/Host.cpp =================================================================== --- lldb/trunk/source/Host/freebsd/Host.cpp +++ lldb/trunk/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 char **environ; } +namespace lldb_private { +class ProcessLaunchInfo; +} + using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Host/linux/Host.cpp =================================================================== --- lldb/trunk/source/Host/linux/Host.cpp +++ lldb/trunk/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 @@ }; } +namespace lldb_private { +class ProcessLaunchInfo; +} + static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo, ProcessState &State, ::pid_t &TracerPid) { auto BufferOrError = getProcFile(Pid, "status"); Index: lldb/trunk/source/Host/macosx/objcxx/Host.mm =================================================================== --- lldb/trunk/source/Host/macosx/objcxx/Host.mm +++ lldb/trunk/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" Index: lldb/trunk/source/Host/netbsd/Host.cpp =================================================================== --- lldb/trunk/source/Host/netbsd/Host.cpp +++ lldb/trunk/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 @@ 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, Index: lldb/trunk/source/Host/openbsd/Host.cpp =================================================================== --- lldb/trunk/source/Host/openbsd/Host.cpp +++ lldb/trunk/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 @@ using namespace lldb; using namespace lldb_private; +namespace lldb_private { +class ProcessLaunchInfo; +} + Environment Host::GetEnvironment() { Environment env; char *v; Index: lldb/trunk/source/Host/posix/HostInfoPosix.cpp =================================================================== --- lldb/trunk/source/Host/posix/HostInfoPosix.cpp +++ lldb/trunk/source/Host/posix/HostInfoPosix.cpp @@ -105,7 +105,7 @@ static llvm::ManagedStatic g_user_id_resolver; -UserIDResolver &HostInfoPosix::GetUserIDResolver() { +UserIDResolver &HostInfoBase::GetUserIDResolver() { return *g_user_id_resolver; } Index: lldb/trunk/source/Host/windows/Host.cpp =================================================================== --- lldb/trunk/source/Host/windows/Host.cpp +++ lldb/trunk/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" Index: lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp =================================================================== --- lldb/trunk/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp +++ lldb/trunk/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 Index: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp =================================================================== --- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp +++ lldb/trunk/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 //------------------------------------------------------------------ Index: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp =================================================================== --- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp +++ lldb/trunk/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 //------------------------------------------------------------------ Index: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp =================================================================== --- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp +++ lldb/trunk/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" Index: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp =================================================================== --- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp +++ lldb/trunk/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 //------------------------------------------------------------------ Index: lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp =================================================================== --- lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp +++ lldb/trunk/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" Index: lldb/trunk/source/Target/Process.cpp =================================================================== --- lldb/trunk/source/Target/Process.cpp +++ lldb/trunk/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 @@ 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 @@ 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 @@ 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()); Index: lldb/trunk/source/Utility/CMakeLists.txt =================================================================== --- lldb/trunk/source/Utility/CMakeLists.txt +++ lldb/trunk/source/Utility/CMakeLists.txt @@ -64,6 +64,7 @@ Log.cpp Logging.cpp NameMatches.cpp + ProcessInfo.cpp RegisterValue.cpp RegularExpression.cpp Reproducer.cpp Index: lldb/trunk/source/Utility/ProcessInfo.cpp =================================================================== --- lldb/trunk/source/Utility/ProcessInfo.cpp +++ lldb/trunk/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; +} Index: lldb/trunk/unittests/Host/CMakeLists.txt =================================================================== --- lldb/trunk/unittests/Host/CMakeLists.txt +++ lldb/trunk/unittests/Host/CMakeLists.txt @@ -5,7 +5,6 @@ HostTest.cpp MainLoopTest.cpp NativeProcessProtocolTest.cpp - ProcessInfoTest.cpp ProcessLaunchInfoTest.cpp SocketAddressTest.cpp SocketTest.cpp Index: lldb/trunk/unittests/Host/ProcessInfoTest.cpp =================================================================== --- lldb/trunk/unittests/Host/ProcessInfoTest.cpp +++ lldb/trunk/unittests/Host/ProcessInfoTest.cpp @@ -1,19 +0,0 @@ -//===-- ProcessInfoTest.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 "gtest/gtest.h" - -using namespace lldb_private; - -TEST(ProcessInfoTest, Constructor) { - ProcessInfo Info("foo", ArchSpec("x86_64-pc-linux"), 47); - EXPECT_STREQ("foo", Info.GetName()); - EXPECT_EQ(ArchSpec("x86_64-pc-linux"), Info.GetArchitecture()); - EXPECT_EQ(47u, Info.GetProcessID()); -} Index: lldb/trunk/unittests/Utility/CMakeLists.txt =================================================================== --- lldb/trunk/unittests/Utility/CMakeLists.txt +++ lldb/trunk/unittests/Utility/CMakeLists.txt @@ -18,6 +18,7 @@ LogTest.cpp NameMatchesTest.cpp PredicateTest.cpp + ProcessInfoTest.cpp RegisterValueTest.cpp ReproducerTest.cpp ReproducerInstrumentationTest.cpp Index: lldb/trunk/unittests/Utility/ProcessInfoTest.cpp =================================================================== --- lldb/trunk/unittests/Utility/ProcessInfoTest.cpp +++ lldb/trunk/unittests/Utility/ProcessInfoTest.cpp @@ -0,0 +1,19 @@ +//===-- ProcessInfoTest.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 "gtest/gtest.h" + +using namespace lldb_private; + +TEST(ProcessInfoTest, Constructor) { + ProcessInfo Info("foo", ArchSpec("x86_64-pc-linux"), 47); + EXPECT_STREQ("foo", Info.GetName()); + EXPECT_EQ(ArchSpec("x86_64-pc-linux"), Info.GetArchitecture()); + EXPECT_EQ(47u, Info.GetProcessID()); +}