https://github.com/HemangGadhavi updated https://github.com/llvm/llvm-project/pull/134354
>From e7b3d8d95477f96b4c1b1a2bbec5cce49f4c15cd Mon Sep 17 00:00:00 2001 From: HemangGadhavi <hemang.gadh...@ibm.com> Date: Fri, 4 Apr 2025 00:59:17 -0500 Subject: [PATCH 1/3] [lldb][AIX] get host info for AIX --- lldb/source/Host/CMakeLists.txt | 1 + lldb/source/Host/aix/Host.cpp | 156 +++++++++++++++++++++++++++++++- 2 files changed, 156 insertions(+), 1 deletion(-) diff --git a/lldb/source/Host/CMakeLists.txt b/lldb/source/Host/CMakeLists.txt index a2ae6f1430c38..a02b1c104396e 100644 --- a/lldb/source/Host/CMakeLists.txt +++ b/lldb/source/Host/CMakeLists.txt @@ -141,6 +141,7 @@ else() add_host_subdirectory(aix aix/Host.cpp aix/HostInfoAIX.cpp + linux/Support.cpp ) endif() endif() diff --git a/lldb/source/Host/aix/Host.cpp b/lldb/source/Host/aix/Host.cpp index 751c4fbcc9368..6ba3e05348df1 100644 --- a/lldb/source/Host/aix/Host.cpp +++ b/lldb/source/Host/aix/Host.cpp @@ -6,18 +6,172 @@ // //===----------------------------------------------------------------------===// +#include <fcntl.h> +#include <sstream> +#include <sys/procfs.h> + #include "lldb/Host/Host.h" +#include "lldb/Host/linux/Support.h" +#include "lldb/Utility/LLDBLog.h" +#include "lldb/Utility/Log.h" +#include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" +#include "llvm/BinaryFormat/XCOFF.h" +using namespace llvm; +using namespace lldb; using namespace lldb_private; +namespace { +enum class ProcessState { + Unknown, + Dead, + DiskSleep, + Idle, + Paging, + Parked, + Running, + Sleeping, + TracedOrStopped, + Zombie, +}; +} + +static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo, + ProcessState &State, ::pid_t &TracerPid, + ::pid_t &Tgid) { + Log *log = GetLog(LLDBLog::Host); + + auto BufferOrError = getProcFile(Pid, "status"); + if (!BufferOrError) + return false; + + llvm::StringRef Rest = BufferOrError.get()->getBuffer(); + while (!Rest.empty()) { + llvm::StringRef Line; + std::tie(Line, Rest) = Rest.split('\n'); + + if (Line.consume_front("Gid:")) { + // Real, effective, saved set, and file system GIDs. Read the first two. + Line = Line.ltrim(); + uint32_t RGid, EGid; + Line.consumeInteger(10, RGid); + Line = Line.ltrim(); + Line.consumeInteger(10, EGid); + + ProcessInfo.SetGroupID(RGid); + ProcessInfo.SetEffectiveGroupID(EGid); + } else if (Line.consume_front("Uid:")) { + // Real, effective, saved set, and file system UIDs. Read the first two. + Line = Line.ltrim(); + uint32_t RUid, EUid; + Line.consumeInteger(10, RUid); + Line = Line.ltrim(); + Line.consumeInteger(10, EUid); + + ProcessInfo.SetUserID(RUid); + ProcessInfo.SetEffectiveUserID(EUid); + } else if (Line.consume_front("PPid:")) { + ::pid_t PPid; + Line.ltrim().consumeInteger(10, PPid); + ProcessInfo.SetParentProcessID(PPid); + } else if (Line.consume_front("State:")) { + State = llvm::StringSwitch<ProcessState>(Line.ltrim().take_front(1)) + .Case("D", ProcessState::DiskSleep) + .Case("I", ProcessState::Idle) + .Case("R", ProcessState::Running) + .Case("S", ProcessState::Sleeping) + .CaseLower("T", ProcessState::TracedOrStopped) + .Case("W", ProcessState::Paging) + .Case("P", ProcessState::Parked) + .Case("X", ProcessState::Dead) + .Case("Z", ProcessState::Zombie) + .Default(ProcessState::Unknown); + if (State == ProcessState::Unknown) { + LLDB_LOG(log, "Unknown process state {0}", Line); + } + } else if (Line.consume_front("TracerPid:")) { + Line = Line.ltrim(); + Line.consumeInteger(10, TracerPid); + } else if (Line.consume_front("Tgid:")) { + Line = Line.ltrim(); + Line.consumeInteger(10, Tgid); + } + } + return true; +} + +static void GetExePathAndArch(::pid_t pid, ProcessInstanceInfo &process_info) { + Log *log = GetLog(LLDBLog::Process); + std::string ExePath(PATH_MAX, '\0'); + struct psinfo psinfoData; + + // We can't use getProcFile here because proc/[pid]/exe is a symbolic link. + llvm::SmallString<64> ProcExe; + (llvm::Twine("/proc/") + llvm::Twine(pid) + "/cwd").toVector(ProcExe); + + ssize_t len = readlink(ProcExe.c_str(), &ExePath[0], PATH_MAX); + if (len > 0) { + ExePath.resize(len); + + struct stat statData; + + std::ostringstream oss; + + oss << "/proc/" << std::dec << pid << "/psinfo"; + assert(stat(oss.str().c_str(), &statData) == 0); + + const int fd = open(oss.str().c_str(), O_RDONLY); + assert(fd >= 0); + + ssize_t readNum = read(fd, &psinfoData, sizeof(psinfoData)); + assert(readNum >= 0); + + close(fd); + } else { + LLDB_LOG(log, "failed to read link exe link for {0}: {1}", pid, + Status(errno, eErrorTypePOSIX)); + ExePath.resize(0); + } + + llvm::StringRef PathRef(&(psinfoData.pr_psargs[0])); + + if (!PathRef.empty()) { + process_info.GetExecutableFile().SetFile(PathRef, FileSpec::Style::native); + ArchSpec arch_spec = ArchSpec(); + arch_spec.SetArchitecture(eArchTypeXCOFF, XCOFF::TCPU_PPC64, + LLDB_INVALID_CPUTYPE, llvm::Triple::AIX); + process_info.SetArchitecture(arch_spec); + } +} + +static bool GetProcessAndStatInfo(::pid_t pid, + ProcessInstanceInfo &process_info, + ProcessState &State, ::pid_t &tracerpid) { + ::pid_t tgid; + tracerpid = 0; + process_info.Clear(); + + process_info.SetProcessID(pid); + + GetExePathAndArch(pid, process_info); + + // Get User and Group IDs and get tracer pid. + if (!GetStatusInfo(pid, process_info, State, tracerpid, tgid)) + return false; + + return true; +} + uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos) { return 0; } bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) { - return false; + ::pid_t tracerpid; + ProcessState State; + return GetProcessAndStatInfo(pid, process_info, State, tracerpid); } Status Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) { >From 3ea41b00f9702cd20c1b762432ede2dcc3befbb8 Mon Sep 17 00:00:00 2001 From: HemangGadhavi <hemang.gadh...@ibm.com> Date: Thu, 10 Apr 2025 06:16:36 -0500 Subject: [PATCH 2/3] Addressed review comments --- lldb/source/Host/aix/Host.cpp | 56 ++++++++++------------------------- 1 file changed, 15 insertions(+), 41 deletions(-) diff --git a/lldb/source/Host/aix/Host.cpp b/lldb/source/Host/aix/Host.cpp index 6ba3e05348df1..6f2b2ac169cfc 100644 --- a/lldb/source/Host/aix/Host.cpp +++ b/lldb/source/Host/aix/Host.cpp @@ -9,7 +9,6 @@ #include <fcntl.h> #include <sstream> #include <sys/procfs.h> - #include "lldb/Host/Host.h" #include "lldb/Host/linux/Support.h" #include "lldb/Utility/LLDBLog.h" @@ -41,7 +40,6 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo, ProcessState &State, ::pid_t &TracerPid, ::pid_t &Tgid) { Log *log = GetLog(LLDBLog::Host); - auto BufferOrError = getProcFile(Pid, "status"); if (!BufferOrError) return false; @@ -50,7 +48,6 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo, while (!Rest.empty()) { llvm::StringRef Line; std::tie(Line, Rest) = Rest.split('\n'); - if (Line.consume_front("Gid:")) { // Real, effective, saved set, and file system GIDs. Read the first two. Line = Line.ltrim(); @@ -58,7 +55,6 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo, Line.consumeInteger(10, RGid); Line = Line.ltrim(); Line.consumeInteger(10, EGid); - ProcessInfo.SetGroupID(RGid); ProcessInfo.SetEffectiveGroupID(EGid); } else if (Line.consume_front("Uid:")) { @@ -68,7 +64,6 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo, Line.consumeInteger(10, RUid); Line = Line.ltrim(); Line.consumeInteger(10, EUid); - ProcessInfo.SetUserID(RUid); ProcessInfo.SetEffectiveUserID(EUid); } else if (Line.consume_front("PPid:")) { @@ -101,48 +96,28 @@ static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo, return true; } -static void GetExePathAndArch(::pid_t pid, ProcessInstanceInfo &process_info) { - Log *log = GetLog(LLDBLog::Process); - std::string ExePath(PATH_MAX, '\0'); +static bool GetExePathAndArch(::pid_t pid, ProcessInstanceInfo &process_info) { struct psinfo psinfoData; - - // We can't use getProcFile here because proc/[pid]/exe is a symbolic link. - llvm::SmallString<64> ProcExe; - (llvm::Twine("/proc/") + llvm::Twine(pid) + "/cwd").toVector(ProcExe); - - ssize_t len = readlink(ProcExe.c_str(), &ExePath[0], PATH_MAX); - if (len > 0) { - ExePath.resize(len); - - struct stat statData; - - std::ostringstream oss; - - oss << "/proc/" << std::dec << pid << "/psinfo"; - assert(stat(oss.str().c_str(), &statData) == 0); - - const int fd = open(oss.str().c_str(), O_RDONLY); - assert(fd >= 0); - - ssize_t readNum = read(fd, &psinfoData, sizeof(psinfoData)); - assert(readNum >= 0); - - close(fd); - } else { - LLDB_LOG(log, "failed to read link exe link for {0}: {1}", pid, - Status(errno, eErrorTypePOSIX)); - ExePath.resize(0); - } - + auto BufferOrError = getProcFile(pid, "psinfo"); + if (!BufferOrError) + return false; + + std::unique_ptr<llvm::MemoryBuffer> PsinfoBuffer = std::move(*BufferOrError); + // Ensure there's enough data for psinfoData + if(PsinfoBuffer->getBufferSize() < sizeof(psinfoData)) + return false; + + std::memcpy(&psinfoData, PsinfoBuffer->getBufferStart(), sizeof(psinfoData)); llvm::StringRef PathRef(&(psinfoData.pr_psargs[0])); - if (!PathRef.empty()) { process_info.GetExecutableFile().SetFile(PathRef, FileSpec::Style::native); ArchSpec arch_spec = ArchSpec(); arch_spec.SetArchitecture(eArchTypeXCOFF, XCOFF::TCPU_PPC64, LLDB_INVALID_CPUTYPE, llvm::Triple::AIX); process_info.SetArchitecture(arch_spec); + return true; } + return false; } static bool GetProcessAndStatInfo(::pid_t pid, @@ -151,11 +126,10 @@ static bool GetProcessAndStatInfo(::pid_t pid, ::pid_t tgid; tracerpid = 0; process_info.Clear(); - process_info.SetProcessID(pid); - GetExePathAndArch(pid, process_info); - + if(!GetExePathAndArch(pid, process_info)) + return false; // Get User and Group IDs and get tracer pid. if (!GetStatusInfo(pid, process_info, State, tracerpid, tgid)) return false; >From f3edd7ccf2f8a38caad9de8fba0cca734b79acfb Mon Sep 17 00:00:00 2001 From: HemangGadhavi <hemang.gadh...@ibm.com> Date: Thu, 10 Apr 2025 07:12:46 -0500 Subject: [PATCH 3/3] changes as per the clang-format --- lldb/source/Host/aix/Host.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lldb/source/Host/aix/Host.cpp b/lldb/source/Host/aix/Host.cpp index 6f2b2ac169cfc..d1880ff7d1432 100644 --- a/lldb/source/Host/aix/Host.cpp +++ b/lldb/source/Host/aix/Host.cpp @@ -6,9 +6,6 @@ // //===----------------------------------------------------------------------===// -#include <fcntl.h> -#include <sstream> -#include <sys/procfs.h> #include "lldb/Host/Host.h" #include "lldb/Host/linux/Support.h" #include "lldb/Utility/LLDBLog.h" @@ -16,6 +13,7 @@ #include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" #include "llvm/BinaryFormat/XCOFF.h" +#include <sys/procfs.h> using namespace llvm; using namespace lldb; @@ -101,12 +99,12 @@ static bool GetExePathAndArch(::pid_t pid, ProcessInstanceInfo &process_info) { auto BufferOrError = getProcFile(pid, "psinfo"); if (!BufferOrError) return false; - + std::unique_ptr<llvm::MemoryBuffer> PsinfoBuffer = std::move(*BufferOrError); // Ensure there's enough data for psinfoData - if(PsinfoBuffer->getBufferSize() < sizeof(psinfoData)) + if (PsinfoBuffer->getBufferSize() < sizeof(psinfoData)) return false; - + std::memcpy(&psinfoData, PsinfoBuffer->getBufferStart(), sizeof(psinfoData)); llvm::StringRef PathRef(&(psinfoData.pr_psargs[0])); if (!PathRef.empty()) { @@ -128,7 +126,7 @@ static bool GetProcessAndStatInfo(::pid_t pid, process_info.Clear(); process_info.SetProcessID(pid); - if(!GetExePathAndArch(pid, process_info)) + if (!GetExePathAndArch(pid, process_info)) return false; // Get User and Group IDs and get tracer pid. if (!GetStatusInfo(pid, process_info, State, tracerpid, tgid)) _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits