Skip to content

[lldb][AIX] get host info for AIX (cont..) #138687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion lldb/source/Host/aix/Host.cpp
Original file line number Diff line number Diff line change
@@ -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>

@@ -133,7 +134,44 @@ 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) {
lldb::pid_t pid;
// Skip non-numeric name directories
if (!llvm::to_integer(direntry->d_name, pid))
continue;
// 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) {
14 changes: 14 additions & 0 deletions lldb/source/Host/aix/HostInfoAIX.cpp
Original file line number Diff line number Diff line change
@@ -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,17 @@ 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;
}
5 changes: 5 additions & 0 deletions lldb/unittests/Host/HostInfoTest.cpp
Original file line number Diff line number Diff line change
@@ -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 {
41 changes: 41 additions & 0 deletions lldb/unittests/Host/HostTest.cpp
Original file line number Diff line number Diff line change
@@ -9,15 +9,18 @@
#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"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <future>
#include <thread>

using namespace lldb_private;
using namespace llvm;
@@ -90,6 +93,44 @@ TEST(Host, LaunchProcessSetsArgv0) {
ASSERT_THAT(exit_status.get_future().get(), 0);
}

TEST(Host, FindProcesses) {
SubsystemRAII<FileSystem, HostInfo> subsystems;

if (test_arg != 0) {
// Give the parent time to retrieve information about self.
// It will kill self when it is done.
std::this_thread::sleep_for(std::chrono::seconds(10));
exit(0);
}

bool foundPID = false;
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));
for (const auto &process : processes) {
if (process.GetProcessID() == info.GetProcessID()) {
ASSERT_EQ(process.GetExecutableFile().GetFilename(),
info.GetExecutableFile().GetFilename());
foundPID = true;
}
}
ASSERT_TRUE(foundPID);
auto clean_up = llvm::make_scope_exit([&] {
Host::Kill(info.GetProcessID(), SIGKILL);
exit_status.get_future().get();
});
}

TEST(Host, LaunchProcessDuplicatesHandle) {
static constexpr llvm::StringLiteral test_msg("Hello subprocess!");

Loading
Oops, something went wrong.