Skip to content

Commit a47e464

Browse files
committed
[lldb][AIX] get host info for AIX (cont..)
1 parent 8dc89e3 commit a47e464

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

lldb/source/Host/aix/Host.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "lldb/Utility/ProcessInfo.h"
1414
#include "lldb/Utility/Status.h"
1515
#include "llvm/BinaryFormat/XCOFF.h"
16+
#include <dirent.h>
1617
#include <sys/proc.h>
1718
#include <sys/procfs.h>
1819

@@ -41,6 +42,14 @@ static ProcessInstanceInfo::timespec convert(pr_timestruc64_t t) {
4142
return ts;
4243
}
4344

45+
static bool IsDirNumeric(const char *dname) {
46+
for (; *dname; dname++) {
47+
if (!isdigit(*dname))
48+
return false;
49+
}
50+
return true;
51+
}
52+
4453
static bool GetStatusInfo(::pid_t pid, ProcessInstanceInfo &processInfo,
4554
ProcessState &State) {
4655
struct pstatus pstatusData;
@@ -133,7 +142,45 @@ static bool GetProcessAndStatInfo(::pid_t pid,
133142

134143
uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
135144
ProcessInstanceInfoList &process_infos) {
136-
return 0;
145+
static const char procdir[] = "/proc/";
146+
147+
DIR *dirproc = opendir(procdir);
148+
if (dirproc) {
149+
struct dirent *direntry = nullptr;
150+
const uid_t our_uid = getuid();
151+
const lldb::pid_t our_pid = getpid();
152+
bool all_users = match_info.GetMatchAllUsers();
153+
154+
while ((direntry = readdir(dirproc)) != nullptr) {
155+
if (!IsDirNumeric(direntry->d_name))
156+
continue;
157+
158+
lldb::pid_t pid = atoi(direntry->d_name);
159+
// Skip this process.
160+
if (pid == our_pid)
161+
continue;
162+
163+
ProcessState State;
164+
ProcessInstanceInfo process_info;
165+
if (!GetProcessAndStatInfo(pid, process_info, State))
166+
continue;
167+
168+
if (State == ProcessState::Zombie ||
169+
State == ProcessState::TracedOrStopped)
170+
continue;
171+
172+
// Check for user match if we're not matching all users and not running
173+
// as root.
174+
if (!all_users && (our_uid != 0) && (process_info.GetUserID() != our_uid))
175+
continue;
176+
177+
if (match_info.Matches(process_info)) {
178+
process_infos.push_back(process_info);
179+
}
180+
}
181+
closedir(dirproc);
182+
}
183+
return process_infos.size();
137184
}
138185

139186
bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) {

lldb/source/Host/aix/HostInfoAIX.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/Host/aix/HostInfoAIX.h"
10+
#include "lldb/Host/posix/Support.h"
11+
#include <sys/procfs.h>
1012

1113
using namespace lldb_private;
1214

@@ -18,5 +20,18 @@ void HostInfoAIX::Terminate() { HostInfoBase::Terminate(); }
1820

1921
FileSpec HostInfoAIX::GetProgramFileSpec() {
2022
static FileSpec g_program_filespec;
23+
struct psinfo psinfoData;
24+
auto BufferOrError = getProcFile(getpid(), "psinfo");
25+
if (BufferOrError) {
26+
std::unique_ptr<llvm::MemoryBuffer> PsinfoBuffer =
27+
std::move(*BufferOrError);
28+
memcpy(&psinfoData, PsinfoBuffer->getBufferStart(), sizeof(psinfoData));
29+
llvm::StringRef exe_path(
30+
psinfoData.pr_psargs,
31+
strnlen(psinfoData.pr_psargs, sizeof(psinfoData.pr_psargs)));
32+
if (!exe_path.empty()) {
33+
g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
34+
}
35+
}
2136
return g_program_filespec;
2237
}

0 commit comments

Comments
 (0)