Skip to content

Commit f5a0778

Browse files
committed
Added testcase for GetProgramFileSpec & FindProcesses
1 parent 69a7aad commit f5a0778

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

lldb/source/Host/aix/Host.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ static ProcessInstanceInfo::timespec convert(pr_timestruc64_t t) {
4242
return ts;
4343
}
4444

45-
static bool IsDirNumeric(const char *dname) {
46-
for (; *dname; dname++) {
47-
if (!isdigit(*dname))
48-
return false;
49-
}
50-
return true;
51-
}
52-
5345
static bool GetStatusInfo(::pid_t pid, ProcessInstanceInfo &processInfo,
5446
ProcessState &State) {
5547
struct pstatus pstatusData;
@@ -152,10 +144,10 @@ uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
152144
bool all_users = match_info.GetMatchAllUsers();
153145

154146
while ((direntry = readdir(dirproc)) != nullptr) {
155-
if (!IsDirNumeric(direntry->d_name))
147+
lldb::pid_t pid;
148+
// Skip non-numeric name directories
149+
if (!llvm::to_integer(direntry->d_name, pid))
156150
continue;
157-
158-
lldb::pid_t pid = atoi(direntry->d_name);
159151
// Skip this process.
160152
if (pid == our_pid)
161153
continue;
@@ -174,9 +166,8 @@ uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
174166
if (!all_users && (our_uid != 0) && (process_info.GetUserID() != our_uid))
175167
continue;
176168

177-
if (match_info.Matches(process_info)) {
169+
if (match_info.Matches(process_info))
178170
process_infos.push_back(process_info);
179-
}
180171
}
181172
closedir(dirproc);
182173
}

lldb/source/Host/aix/HostInfoAIX.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ FileSpec HostInfoAIX::GetProgramFileSpec() {
2929
llvm::StringRef exe_path(
3030
psinfoData.pr_psargs,
3131
strnlen(psinfoData.pr_psargs, sizeof(psinfoData.pr_psargs)));
32-
if (!exe_path.empty()) {
32+
if (!exe_path.empty())
3333
g_program_filespec.SetFile(exe_path, FileSpec::Style::native);
34-
}
3534
}
3635
return g_program_filespec;
3736
}

lldb/unittests/Host/HostInfoTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ TEST_F(HostInfoTest, GetHostname) {
5454
EXPECT_TRUE(HostInfo::GetHostname(s));
5555
}
5656

57+
TEST_F(HostInfoTest, GetProgramFileSpec) {
58+
// Test GetProgramFileSpec()
59+
FileSpec filespec = HostInfo::GetProgramFileSpec();
60+
EXPECT_FALSE(filespec.GetFilename().IsEmpty());
61+
}
62+
5763
#if defined(__APPLE__)
5864
TEST_F(HostInfoTest, GetXcodeSDK) {
5965
auto get_sdk = [](std::string sdk, bool error = false) -> llvm::StringRef {

lldb/unittests/Host/HostTest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,31 @@ TEST(Host, LaunchProcessSetsArgv0) {
9090
ASSERT_THAT(exit_status.get_future().get(), 0);
9191
}
9292

93+
TEST(Host, FindProcesses) {
94+
SubsystemRAII<FileSystem> subsystems;
95+
96+
if (test_arg != 0)
97+
exit(0);
98+
99+
ProcessLaunchInfo info;
100+
ProcessInstanceInfoList processes;
101+
ProcessInstanceInfoMatch match;
102+
ProcessInstanceInfo p_info;
103+
info.SetExecutableFile(FileSpec(TestMainArgv0),
104+
/*add_exe_file_as_first_arg=*/true);
105+
info.GetArguments().AppendArgument("--gtest_filter=Host.FindProcesses");
106+
info.GetArguments().AppendArgument("--test-arg=48");
107+
std::promise<int> exit_status;
108+
info.SetMonitorProcessCallback([&](lldb::pid_t pid, int signal, int status) {
109+
exit_status.set_value(status);
110+
});
111+
ASSERT_THAT_ERROR(Host::LaunchProcess(info).takeError(), Succeeded());
112+
ASSERT_TRUE(Host::GetProcessInfo(info.GetProcessID(), p_info));
113+
ASSERT_TRUE(Host::FindProcesses(match, processes));
114+
EXPECT_TRUE(match.ProcessIDsMatch(p_info));
115+
ASSERT_THAT(exit_status.get_future().get(), 0);
116+
}
117+
93118
#ifdef LLVM_ON_UNIX
94119
TEST(Host, LaunchProcessDuplicatesHandle) {
95120
static constexpr llvm::StringLiteral test_msg("Hello subprocess!");

0 commit comments

Comments
 (0)