Skip to content

Commit 78453d1

Browse files
committed
Moved the host support and unittest functions to posix
1 parent f3edd7c commit 78453d1

File tree

16 files changed

+138
-109
lines changed

16 files changed

+138
-109
lines changed

lldb/include/lldb/Host/linux/Support.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ namespace lldb_private {
1818
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
1919
getProcFile(::pid_t pid, ::pid_t tid, const llvm::Twine &file);
2020

21-
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
22-
getProcFile(::pid_t pid, const llvm::Twine &file);
23-
24-
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
25-
getProcFile(const llvm::Twine &file);
26-
2721
} // namespace lldb_private
2822

2923
#endif // #ifndef LLDB_HOST_LINUX_SUPPORT_H
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Support.h -----------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_HOST_POSIX_SUPPORT_H
10+
#define LLDB_HOST_POSIX_SUPPORT_H
11+
12+
#include "llvm/Support/ErrorOr.h"
13+
#include "llvm/Support/MemoryBuffer.h"
14+
#include <memory>
15+
16+
namespace lldb_private {
17+
18+
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
19+
getProcFile(::pid_t pid, const llvm::Twine &file);
20+
21+
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
22+
getProcFile(const llvm::Twine &file);
23+
24+
} // namespace lldb_private
25+
26+
#endif // #ifndef LLDB_HOST_POSIX_SUPPORT_H

lldb/source/Host/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ else()
8787
posix/MainLoopPosix.cpp
8888
posix/PipePosix.cpp
8989
posix/ProcessLauncherPosixFork.cpp
90+
posix/Support.cpp
9091
)
9192

9293
if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
@@ -141,7 +142,6 @@ else()
141142
add_host_subdirectory(aix
142143
aix/Host.cpp
143144
aix/HostInfoAIX.cpp
144-
linux/Support.cpp
145145
)
146146
endif()
147147
endif()

lldb/source/Host/aix/Host.cpp

Lines changed: 63 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "lldb/Host/Host.h"
10-
#include "lldb/Host/linux/Support.h"
10+
#include "lldb/Host/posix/Support.h"
1111
#include "lldb/Utility/LLDBLog.h"
1212
#include "lldb/Utility/Log.h"
1313
#include "lldb/Utility/ProcessInfo.h"
1414
#include "lldb/Utility/Status.h"
1515
#include "llvm/BinaryFormat/XCOFF.h"
1616
#include <sys/procfs.h>
17+
#include <sys/proc.h>
1718

1819
using namespace llvm;
1920
using namespace lldb;
@@ -34,67 +35,53 @@ enum class ProcessState {
3435
};
3536
}
3637

37-
static bool GetStatusInfo(::pid_t Pid, ProcessInstanceInfo &ProcessInfo,
38-
ProcessState &State, ::pid_t &TracerPid,
39-
::pid_t &Tgid) {
40-
Log *log = GetLog(LLDBLog::Host);
41-
auto BufferOrError = getProcFile(Pid, "status");
38+
ProcessInstanceInfo::timespec convert(pr_timestruc64_t t) {
39+
ProcessInstanceInfo::timespec ts;
40+
ts.tv_sec = t.tv_sec;
41+
ts.tv_usec = t.tv_nsec / 1000 ; //nanos to micros
42+
return ts;
43+
}
44+
45+
static bool GetStatusInfo(::pid_t pid, ProcessInstanceInfo &processInfo,
46+
ProcessState &State) {
47+
struct pstatus pstatusData;
48+
auto BufferOrError = getProcFile(pid, "status");
4249
if (!BufferOrError)
4350
return false;
4451

45-
llvm::StringRef Rest = BufferOrError.get()->getBuffer();
46-
while (!Rest.empty()) {
47-
llvm::StringRef Line;
48-
std::tie(Line, Rest) = Rest.split('\n');
49-
if (Line.consume_front("Gid:")) {
50-
// Real, effective, saved set, and file system GIDs. Read the first two.
51-
Line = Line.ltrim();
52-
uint32_t RGid, EGid;
53-
Line.consumeInteger(10, RGid);
54-
Line = Line.ltrim();
55-
Line.consumeInteger(10, EGid);
56-
ProcessInfo.SetGroupID(RGid);
57-
ProcessInfo.SetEffectiveGroupID(EGid);
58-
} else if (Line.consume_front("Uid:")) {
59-
// Real, effective, saved set, and file system UIDs. Read the first two.
60-
Line = Line.ltrim();
61-
uint32_t RUid, EUid;
62-
Line.consumeInteger(10, RUid);
63-
Line = Line.ltrim();
64-
Line.consumeInteger(10, EUid);
65-
ProcessInfo.SetUserID(RUid);
66-
ProcessInfo.SetEffectiveUserID(EUid);
67-
} else if (Line.consume_front("PPid:")) {
68-
::pid_t PPid;
69-
Line.ltrim().consumeInteger(10, PPid);
70-
ProcessInfo.SetParentProcessID(PPid);
71-
} else if (Line.consume_front("State:")) {
72-
State = llvm::StringSwitch<ProcessState>(Line.ltrim().take_front(1))
73-
.Case("D", ProcessState::DiskSleep)
74-
.Case("I", ProcessState::Idle)
75-
.Case("R", ProcessState::Running)
76-
.Case("S", ProcessState::Sleeping)
77-
.CaseLower("T", ProcessState::TracedOrStopped)
78-
.Case("W", ProcessState::Paging)
79-
.Case("P", ProcessState::Parked)
80-
.Case("X", ProcessState::Dead)
81-
.Case("Z", ProcessState::Zombie)
82-
.Default(ProcessState::Unknown);
83-
if (State == ProcessState::Unknown) {
84-
LLDB_LOG(log, "Unknown process state {0}", Line);
85-
}
86-
} else if (Line.consume_front("TracerPid:")) {
87-
Line = Line.ltrim();
88-
Line.consumeInteger(10, TracerPid);
89-
} else if (Line.consume_front("Tgid:")) {
90-
Line = Line.ltrim();
91-
Line.consumeInteger(10, Tgid);
92-
}
52+
std::unique_ptr<llvm::MemoryBuffer> StatusBuffer = std::move(*BufferOrError);
53+
// Ensure there's enough data for psinfoData
54+
if (StatusBuffer->getBufferSize() < sizeof(pstatusData))
55+
return false;
56+
57+
std::memcpy(&pstatusData, StatusBuffer->getBufferStart(),
58+
sizeof(pstatusData));
59+
switch(pstatusData.pr_stat) {
60+
case SIDL:
61+
State = ProcessState::Idle;
62+
break;
63+
case SACTIVE:
64+
State = ProcessState::Running;
65+
break;
66+
case SSTOP:
67+
State = ProcessState::TracedOrStopped;
68+
break;
69+
case SZOMB:
70+
State = ProcessState::Zombie;
71+
break;
72+
default:
73+
State = ProcessState::Unknown;
74+
break;
9375
}
76+
processInfo.SetIsZombie(State == ProcessState::Zombie);
77+
processInfo.SetUserTime(convert(pstatusData.pr_utime));
78+
processInfo.SetSystemTime(convert(pstatusData.pr_stime));
79+
processInfo.SetCumulativeUserTime(convert(pstatusData.pr_cutime));
80+
processInfo.SetCumulativeSystemTime(convert(pstatusData.pr_cstime));
9481
return true;
9582
}
9683

97-
static bool GetExePathAndArch(::pid_t pid, ProcessInstanceInfo &process_info) {
84+
static bool GetExePathAndIds(::pid_t pid, ProcessInstanceInfo &process_info) {
9885
struct psinfo psinfoData;
9986
auto BufferOrError = getProcFile(pid, "psinfo");
10087
if (!BufferOrError)
@@ -107,29 +94,35 @@ static bool GetExePathAndArch(::pid_t pid, ProcessInstanceInfo &process_info) {
10794

10895
std::memcpy(&psinfoData, PsinfoBuffer->getBufferStart(), sizeof(psinfoData));
10996
llvm::StringRef PathRef(&(psinfoData.pr_psargs[0]));
110-
if (!PathRef.empty()) {
111-
process_info.GetExecutableFile().SetFile(PathRef, FileSpec::Style::native);
112-
ArchSpec arch_spec = ArchSpec();
113-
arch_spec.SetArchitecture(eArchTypeXCOFF, XCOFF::TCPU_PPC64,
97+
if (PathRef.empty())
98+
return false;
99+
100+
process_info.GetExecutableFile().SetFile(PathRef, FileSpec::Style::native);
101+
ArchSpec arch_spec = ArchSpec();
102+
arch_spec.SetArchitecture(eArchTypeXCOFF, XCOFF::TCPU_PPC64,
114103
LLDB_INVALID_CPUTYPE, llvm::Triple::AIX);
115-
process_info.SetArchitecture(arch_spec);
116-
return true;
117-
}
118-
return false;
104+
process_info.SetArchitecture(arch_spec);
105+
process_info.SetParentProcessID(psinfoData.pr_ppid);
106+
process_info.SetGroupID(psinfoData.pr_gid);
107+
process_info.SetEffectiveGroupID(psinfoData.pr_egid);
108+
process_info.SetUserID(psinfoData.pr_uid);
109+
process_info.SetEffectiveUserID(psinfoData.pr_euid);
110+
process_info.SetProcessGroupID(psinfoData.pr_pgid);
111+
process_info.SetProcessSessionID(psinfoData.pr_sid);
112+
return true;
119113
}
120114

121115
static bool GetProcessAndStatInfo(::pid_t pid,
122116
ProcessInstanceInfo &process_info,
123-
ProcessState &State, ::pid_t &tracerpid) {
124-
::pid_t tgid;
125-
tracerpid = 0;
117+
ProcessState &State) {
126118
process_info.Clear();
127119
process_info.SetProcessID(pid);
128120

129-
if (!GetExePathAndArch(pid, process_info))
121+
// Get Executable path/Arch and Get User and Group IDs.
122+
if (!GetExePathAndIds(pid, process_info))
130123
return false;
131-
// Get User and Group IDs and get tracer pid.
132-
if (!GetStatusInfo(pid, process_info, State, tracerpid, tgid))
124+
//Get process status and timing info.
125+
if (!GetStatusInfo(pid, process_info, State))
133126
return false;
134127

135128
return true;
@@ -141,9 +134,8 @@ uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
141134
}
142135

143136
bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) {
144-
::pid_t tracerpid;
145137
ProcessState State;
146-
return GetProcessAndStatInfo(pid, process_info, State, tracerpid);
138+
return GetProcessAndStatInfo(pid, process_info, State);
147139
}
148140

149141
Status Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) {

lldb/source/Host/linux/Host.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "lldb/Host/Host.h"
3131
#include "lldb/Host/HostInfo.h"
3232
#include "lldb/Host/linux/Host.h"
33-
#include "lldb/Host/linux/Support.h"
33+
#include "lldb/Host/posix/Support.h"
3434
#include "lldb/Utility/DataExtractor.h"
3535

3636
using namespace lldb;

lldb/source/Host/linux/Support.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,3 @@ lldb_private::getProcFile(::pid_t pid, ::pid_t tid, const llvm::Twine &file) {
2222
LLDB_LOG(log, "Failed to open {0}: {1}", File, Ret.getError().message());
2323
return Ret;
2424
}
25-
26-
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
27-
lldb_private::getProcFile(::pid_t pid, const llvm::Twine &file) {
28-
Log *log = GetLog(LLDBLog::Host);
29-
std::string File = ("/proc/" + llvm::Twine(pid) + "/" + file).str();
30-
auto Ret = llvm::MemoryBuffer::getFileAsStream(File);
31-
if (!Ret)
32-
LLDB_LOG(log, "Failed to open {0}: {1}", File, Ret.getError().message());
33-
return Ret;
34-
}
35-
36-
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
37-
lldb_private::getProcFile(const llvm::Twine &file) {
38-
Log *log = GetLog(LLDBLog::Host);
39-
std::string File = ("/proc/" + file).str();
40-
auto Ret = llvm::MemoryBuffer::getFileAsStream(File);
41-
if (!Ret)
42-
LLDB_LOG(log, "Failed to open {0}: {1}", File, Ret.getError().message());
43-
return Ret;
44-
}

lldb/source/Host/posix/Support.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===-- Support.cpp -------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "lldb/Host/posix/Support.h"
10+
#include "lldb/Utility/LLDBLog.h"
11+
#include "lldb/Utility/Log.h"
12+
#include "llvm/Support/MemoryBuffer.h"
13+
14+
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
15+
lldb_private::getProcFile(::pid_t pid, const llvm::Twine &file) {
16+
Log *log = GetLog(LLDBLog::Host);
17+
std::string File = ("/proc/" + llvm::Twine(pid) + "/" + file).str();
18+
auto Ret = llvm::MemoryBuffer::getFileAsStream(File);
19+
if (!Ret)
20+
LLDB_LOG(log, "Failed to open {0}: {1}", File, Ret.getError().message());
21+
return Ret;
22+
}
23+
24+
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
25+
lldb_private::getProcFile(const llvm::Twine &file) {
26+
Log *log = GetLog(LLDBLog::Host);
27+
std::string File = ("/proc/" + file).str();
28+
auto Ret = llvm::MemoryBuffer::getFileAsStream(File);
29+
if (!Ret)
30+
LLDB_LOG(log, "Failed to open {0}: {1}", File, Ret.getError().message());
31+
return Ret;
32+
}

lldb/source/Plugins/Process/AIX/NativeProcessAIX.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "Plugins/Process/Utility/NativeProcessSoftwareSingleStep.h"
1313
#include "lldb/Host/Debug.h"
1414
#include "lldb/Host/common/NativeProcessProtocol.h"
15-
#include "lldb/Host/linux/Support.h"
15+
#include "lldb/Host/posix/Support.h"
1616
#include "lldb/Target/MemoryRegionInfo.h"
1717
#include "lldb/Utility/ArchSpec.h"
1818
#include "lldb/Utility/FileSpec.h"

lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "Perf.h"
1111
#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
1212
#include "Procfs.h"
13-
#include "lldb/Host/linux/Support.h"
1413
#include "lldb/Utility/StreamString.h"
1514
#include "llvm/ADT/StringRef.h"
1615
#include "llvm/Support/Error.h"

lldb/source/Plugins/Process/Linux/NativeProcessLinux.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "lldb/Host/Debug.h"
1616
#include "lldb/Host/HostThread.h"
17+
#include "lldb/Host/posix/Support.h"
1718
#include "lldb/Host/linux/Support.h"
1819
#include "lldb/Target/MemoryRegionInfo.h"
1920
#include "lldb/Utility/ArchSpec.h"

lldb/source/Plugins/Process/Linux/Perf.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "Perf.h"
1010

1111
#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
12-
#include "lldb/Host/linux/Support.h"
1312
#include "llvm/Support/FormatVariadic.h"
1413
#include "llvm/Support/MathExtras.h"
1514
#include "llvm/Support/MemoryBuffer.h"

lldb/source/Plugins/Process/Linux/Procfs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Procfs.h"
10-
#include "lldb/Host/linux/Support.h"
10+
#include "lldb/Host/posix/Support.h"
1111
#include "llvm/ADT/StringExtras.h"
1212
#include "llvm/Support/Error.h"
1313
#include "llvm/Support/MemoryBuffer.h"

lldb/unittests/Host/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ set (FILES
1515
XMLTest.cpp
1616
)
1717

18-
if (CMAKE_SYSTEM_NAME MATCHES "Linux|Android")
18+
if (CMAKE_SYSTEM_NAME MATCHES "Linux|Android|AIX")
1919
list(APPEND FILES
20-
linux/HostTest.cpp
21-
linux/SupportTest.cpp
20+
posix/HostTest.cpp
21+
posix/SupportTest.cpp
2222
)
2323
endif()
2424

lldb/unittests/Host/linux/HostTest.cpp renamed to lldb/unittests/Host/posix/HostTest.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ TEST_F(HostTest, GetProcessInfo) {
4040
triple.getEnvironment() == llvm::Triple::EnvironmentType::Android));
4141

4242
ProcessInstanceInfo Info;
43+
#ifndef _AIX
4344
ASSERT_FALSE(Host::GetProcessInfo(0, Info));
44-
45+
#endif /* ifndef _AIX */
4546
ASSERT_TRUE(Host::GetProcessInfo(getpid(), Info));
4647

4748
ASSERT_TRUE(Info.ProcessIDIsValid());
@@ -90,6 +91,7 @@ TEST_F(HostTest, GetProcessInfo) {
9091
ASSERT_TRUE(user_time.tv_sec <= next_user_time.tv_sec ||
9192
user_time.tv_usec <= next_user_time.tv_usec);
9293

94+
#ifndef _AIX
9395
struct rlimit rlim;
9496
EXPECT_EQ(getrlimit(RLIMIT_NICE, &rlim), 0);
9597
// getpriority can return -1 so we zero errno first
@@ -108,4 +110,5 @@ TEST_F(HostTest, GetProcessInfo) {
108110
}
109111
ASSERT_TRUE(Info.IsZombie().has_value());
110112
ASSERT_FALSE(Info.IsZombie().value());
113+
#endif /* ifndef _AIX */
111114
}

0 commit comments

Comments
 (0)