https://github.com/HemangGadhavi updated https://github.com/llvm/llvm-project/pull/138687
>From a47e4642e6ebcbe6b5466ff118968bac83ccf1e9 Mon Sep 17 00:00:00 2001 From: HemangGadhavi <hemang.gadh...@ibm.com> Date: Tue, 6 May 2025 07:59:45 -0500 Subject: [PATCH 1/2] [lldb][AIX] get host info for AIX (cont..) --- lldb/source/Host/aix/Host.cpp | 49 +++++++++++++++++++++++++++- lldb/source/Host/aix/HostInfoAIX.cpp | 15 +++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/lldb/source/Host/aix/Host.cpp b/lldb/source/Host/aix/Host.cpp index a812e061ccae2..ead8202cbbdef 100644 --- a/lldb/source/Host/aix/Host.cpp +++ b/lldb/source/Host/aix/Host.cpp @@ -13,6 +13,7 @@ #include "lldb/Utility/ProcessInfo.h" #include "lldb/Utility/Status.h" #include "llvm/BinaryFormat/XCOFF.h" +#include <dirent.h> #include <sys/proc.h> #include <sys/procfs.h> @@ -41,6 +42,14 @@ static ProcessInstanceInfo::timespec convert(pr_timestruc64_t t) { return ts; } +static bool IsDirNumeric(const char *dname) { + for (; *dname; dname++) { + if (!isdigit(*dname)) + return false; + } + return true; +} + static bool GetStatusInfo(::pid_t pid, ProcessInstanceInfo &processInfo, ProcessState &State) { struct pstatus pstatusData; @@ -133,7 +142,45 @@ static bool GetProcessAndStatInfo(::pid_t pid, uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos) { - return 0; + static const char procdir[] = "/proc/"; + + DIR *dirproc = opendir(procdir); + if (dirproc) { + struct dirent *direntry = nullptr; + const uid_t our_uid = getuid(); + const lldb::pid_t our_pid = getpid(); + bool all_users = match_info.GetMatchAllUsers(); + + while ((direntry = readdir(dirproc)) != nullptr) { + if (!IsDirNumeric(direntry->d_name)) + continue; + + lldb::pid_t pid = atoi(direntry->d_name); + // Skip this process. + if (pid == our_pid) + continue; + + ProcessState State; + ProcessInstanceInfo process_info; + if (!GetProcessAndStatInfo(pid, process_info, State)) + continue; + + if (State == ProcessState::Zombie || + State == ProcessState::TracedOrStopped) + continue; + + // Check for user match if we're not matching all users and not running + // as root. + if (!all_users && (our_uid != 0) && (process_info.GetUserID() != our_uid)) + continue; + + if (match_info.Matches(process_info)) { + process_infos.push_back(process_info); + } + } + closedir(dirproc); + } + return process_infos.size(); } bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) { diff --git a/lldb/source/Host/aix/HostInfoAIX.cpp b/lldb/source/Host/aix/HostInfoAIX.cpp index 61b47462dd647..d720f5c52d3b1 100644 --- a/lldb/source/Host/aix/HostInfoAIX.cpp +++ b/lldb/source/Host/aix/HostInfoAIX.cpp @@ -7,6 +7,8 @@ //===----------------------------------------------------------------------===// #include "lldb/Host/aix/HostInfoAIX.h" +#include "lldb/Host/posix/Support.h" +#include <sys/procfs.h> using namespace lldb_private; @@ -18,5 +20,18 @@ void HostInfoAIX::Terminate() { HostInfoBase::Terminate(); } FileSpec HostInfoAIX::GetProgramFileSpec() { static FileSpec g_program_filespec; + struct psinfo psinfoData; + auto BufferOrError = getProcFile(getpid(), "psinfo"); + if (BufferOrError) { + std::unique_ptr<llvm::MemoryBuffer> PsinfoBuffer = + std::move(*BufferOrError); + memcpy(&psinfoData, PsinfoBuffer->getBufferStart(), sizeof(psinfoData)); + llvm::StringRef exe_path( + psinfoData.pr_psargs, + strnlen(psinfoData.pr_psargs, sizeof(psinfoData.pr_psargs))); + if (!exe_path.empty()) { + g_program_filespec.SetFile(exe_path, FileSpec::Style::native); + } + } return g_program_filespec; } >From adf7708a50e781c31e2bccc1eff484499977f064 Mon Sep 17 00:00:00 2001 From: HemangGadhavi <hemang.gadh...@ibm.com> Date: Wed, 21 May 2025 05:22:07 -0400 Subject: [PATCH 2/2] Added testcase for GetProgramFileSpec & FindProcesses --- lldb/source/Host/aix/Host.cpp | 17 ++++------------- lldb/source/Host/aix/HostInfoAIX.cpp | 3 +-- lldb/unittests/Host/HostInfoTest.cpp | 5 +++++ lldb/unittests/Host/HostTest.cpp | 24 ++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/lldb/source/Host/aix/Host.cpp b/lldb/source/Host/aix/Host.cpp index ead8202cbbdef..b5572b93d93a9 100644 --- a/lldb/source/Host/aix/Host.cpp +++ b/lldb/source/Host/aix/Host.cpp @@ -42,14 +42,6 @@ static ProcessInstanceInfo::timespec convert(pr_timestruc64_t t) { return ts; } -static bool IsDirNumeric(const char *dname) { - for (; *dname; dname++) { - if (!isdigit(*dname)) - return false; - } - return true; -} - static bool GetStatusInfo(::pid_t pid, ProcessInstanceInfo &processInfo, ProcessState &State) { struct pstatus pstatusData; @@ -152,10 +144,10 @@ uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info, bool all_users = match_info.GetMatchAllUsers(); while ((direntry = readdir(dirproc)) != nullptr) { - if (!IsDirNumeric(direntry->d_name)) + lldb::pid_t pid; + // Skip non-numeric name directories + if (!llvm::to_integer(direntry->d_name, pid)) continue; - - lldb::pid_t pid = atoi(direntry->d_name); // Skip this process. if (pid == our_pid) continue; @@ -174,9 +166,8 @@ uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info, if (!all_users && (our_uid != 0) && (process_info.GetUserID() != our_uid)) continue; - if (match_info.Matches(process_info)) { + if (match_info.Matches(process_info)) process_infos.push_back(process_info); - } } closedir(dirproc); } diff --git a/lldb/source/Host/aix/HostInfoAIX.cpp b/lldb/source/Host/aix/HostInfoAIX.cpp index d720f5c52d3b1..aab3bf62a635f 100644 --- a/lldb/source/Host/aix/HostInfoAIX.cpp +++ b/lldb/source/Host/aix/HostInfoAIX.cpp @@ -29,9 +29,8 @@ FileSpec HostInfoAIX::GetProgramFileSpec() { llvm::StringRef exe_path( psinfoData.pr_psargs, strnlen(psinfoData.pr_psargs, sizeof(psinfoData.pr_psargs))); - if (!exe_path.empty()) { + if (!exe_path.empty()) g_program_filespec.SetFile(exe_path, FileSpec::Style::native); - } } return g_program_filespec; } diff --git a/lldb/unittests/Host/HostInfoTest.cpp b/lldb/unittests/Host/HostInfoTest.cpp index 14941ee5a94b2..0b3bae5c56f2f 100644 --- a/lldb/unittests/Host/HostInfoTest.cpp +++ b/lldb/unittests/Host/HostInfoTest.cpp @@ -54,6 +54,11 @@ TEST_F(HostInfoTest, GetHostname) { EXPECT_TRUE(HostInfo::GetHostname(s)); } +TEST_F(HostInfoTest, GetProgramFileSpec) { + FileSpec filespec = HostInfo::GetProgramFileSpec(); + EXPECT_TRUE(FileSystem::Instance().Exists(filespec)); +} + #if defined(__APPLE__) TEST_F(HostInfoTest, GetXcodeSDK) { auto get_sdk = [](std::string sdk, bool error = false) -> llvm::StringRef { diff --git a/lldb/unittests/Host/HostTest.cpp b/lldb/unittests/Host/HostTest.cpp index 222de62ab6697..92dd70c180dae 100644 --- a/lldb/unittests/Host/HostTest.cpp +++ b/lldb/unittests/Host/HostTest.cpp @@ -9,6 +9,7 @@ #include "lldb/Host/Host.h" #include "TestingSupport/SubsystemRAII.h" #include "lldb/Host/FileSystem.h" +#include "lldb/Host/HostInfo.h" #include "lldb/Host/Pipe.h" #include "lldb/Host/ProcessLaunchInfo.h" #include "lldb/Utility/ProcessInfo.h" @@ -90,6 +91,29 @@ TEST(Host, LaunchProcessSetsArgv0) { ASSERT_THAT(exit_status.get_future().get(), 0); } +TEST(Host, FindProcesses) { + SubsystemRAII<FileSystem, HostInfo> subsystems; + + if (test_arg != 0) + exit(0); + + ProcessLaunchInfo info; + ProcessInstanceInfoList processes; + ProcessInstanceInfoMatch match(TestMainArgv0, NameMatch::Equals); + info.SetExecutableFile(FileSpec(TestMainArgv0), + /*add_exe_file_as_first_arg=*/true); + info.GetArguments().AppendArgument("--gtest_filter=Host.FindProcesses"); + info.GetArguments().AppendArgument("--test-arg=48"); + std::promise<int> exit_status; + info.SetMonitorProcessCallback([&](lldb::pid_t pid, int signal, int status) { + exit_status.set_value(status); + }); + ASSERT_THAT_ERROR(Host::LaunchProcess(info).takeError(), Succeeded()); + ASSERT_TRUE(Host::FindProcesses(match, processes)); + ASSERT_EQ(processes[0].GetArg0(), TestMainArgv0); + ASSERT_THAT(exit_status.get_future().get(), 0); +} + #ifdef LLVM_ON_UNIX TEST(Host, LaunchProcessDuplicatesHandle) { static constexpr llvm::StringLiteral test_msg("Hello subprocess!"); _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits