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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -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>

Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions lldb/source/Host/aix/HostInfoAIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//===----------------------------------------------------------------------===//

#include "lldb/Host/aix/HostInfoAIX.h"
#include "lldb/Host/posix/Support.h"
#include <sys/procfs.h>

using namespace lldb_private;

Expand All @@ -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
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions lldb/unittests/Host/HostTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,6 +19,7 @@
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <future>
#include <thread>

using namespace lldb_private;
using namespace llvm;
Expand Down Expand Up @@ -90,6 +92,30 @@ 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());
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
ASSERT_TRUE(Host::FindProcesses(match, processes));
ASSERT_EQ(processes[0].GetArg0(), TestMainArgv0);
ASSERT_THAT(exit_status.get_future().get(), 0);
}

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

Expand Down
Loading