Skip to content

Commit adf7708

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

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ TEST_F(HostInfoTest, GetHostname) {
5454
EXPECT_TRUE(HostInfo::GetHostname(s));
5555
}
5656

57+
TEST_F(HostInfoTest, GetProgramFileSpec) {
58+
FileSpec filespec = HostInfo::GetProgramFileSpec();
59+
EXPECT_TRUE(FileSystem::Instance().Exists(filespec));
60+
}
61+
5762
#if defined(__APPLE__)
5863
TEST_F(HostInfoTest, GetXcodeSDK) {
5964
auto get_sdk = [](std::string sdk, bool error = false) -> llvm::StringRef {

lldb/unittests/Host/HostTest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "lldb/Host/Host.h"
1010
#include "TestingSupport/SubsystemRAII.h"
1111
#include "lldb/Host/FileSystem.h"
12+
#include "lldb/Host/HostInfo.h"
1213
#include "lldb/Host/Pipe.h"
1314
#include "lldb/Host/ProcessLaunchInfo.h"
1415
#include "lldb/Utility/ProcessInfo.h"
@@ -90,6 +91,29 @@ TEST(Host, LaunchProcessSetsArgv0) {
9091
ASSERT_THAT(exit_status.get_future().get(), 0);
9192
}
9293

94+
TEST(Host, FindProcesses) {
95+
SubsystemRAII<FileSystem, HostInfo> subsystems;
96+
97+
if (test_arg != 0)
98+
exit(0);
99+
100+
ProcessLaunchInfo info;
101+
ProcessInstanceInfoList processes;
102+
ProcessInstanceInfoMatch match(TestMainArgv0, NameMatch::Equals);
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::FindProcesses(match, processes));
113+
ASSERT_EQ(processes[0].GetArg0(), TestMainArgv0);
114+
ASSERT_THAT(exit_status.get_future().get(), 0);
115+
}
116+
93117
#ifdef LLVM_ON_UNIX
94118
TEST(Host, LaunchProcessDuplicatesHandle) {
95119
static constexpr llvm::StringLiteral test_msg("Hello subprocess!");

0 commit comments

Comments
 (0)