Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 0 additions & 6 deletions lldb/include/lldb/Host/linux/Support.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ namespace lldb_private {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getProcFile(::pid_t pid, ::pid_t tid, const llvm::Twine &file);

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getProcFile(::pid_t pid, const llvm::Twine &file);

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getProcFile(const llvm::Twine &file);

} // namespace lldb_private

#endif // #ifndef LLDB_HOST_LINUX_SUPPORT_H
26 changes: 26 additions & 0 deletions lldb/include/lldb/Host/posix/Support.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Support.h -----------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_HOST_POSIX_SUPPORT_H
#define LLDB_HOST_POSIX_SUPPORT_H

#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include <memory>

namespace lldb_private {

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getProcFile(::pid_t pid, const llvm::Twine &file);

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getProcFile(const llvm::Twine &file);

} // namespace lldb_private

#endif // #ifndef LLDB_HOST_POSIX_SUPPORT_H
1 change: 1 addition & 0 deletions lldb/source/Host/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ else()
posix/MainLoopPosix.cpp
posix/PipePosix.cpp
posix/ProcessLauncherPosixFork.cpp
posix/Support.cpp
)

if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
Expand Down
124 changes: 123 additions & 1 deletion lldb/source/Host/aix/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,139 @@
//===----------------------------------------------------------------------===//

#include "lldb/Host/Host.h"
#include "lldb/Host/posix/Support.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/ProcessInfo.h"
#include "lldb/Utility/Status.h"
#include "llvm/BinaryFormat/XCOFF.h"
#include <sys/proc.h>
#include <sys/procfs.h>

using namespace llvm;
using namespace lldb;
using namespace lldb_private;

namespace {
enum class ProcessState {
Unknown,
Dead,
DiskSleep,
Idle,
Paging,
Parked,
Running,
Sleeping,
TracedOrStopped,
Zombie,
};
}

ProcessInstanceInfo::timespec convert(pr_timestruc64_t t) {
ProcessInstanceInfo::timespec ts;
ts.tv_sec = t.tv_sec;
ts.tv_usec = t.tv_nsec / 1000; // nanos to micros
return ts;
}

static bool GetStatusInfo(::pid_t pid, ProcessInstanceInfo &processInfo,
ProcessState &State) {
struct pstatus pstatusData;
auto BufferOrError = getProcFile(pid, "status");
if (!BufferOrError)
return false;

std::unique_ptr<llvm::MemoryBuffer> StatusBuffer = std::move(*BufferOrError);
// Ensure there's enough data for psinfoData
if (StatusBuffer->getBufferSize() < sizeof(pstatusData))
return false;

std::memcpy(&pstatusData, StatusBuffer->getBufferStart(),
sizeof(pstatusData));
switch (pstatusData.pr_stat) {
case SIDL:
State = ProcessState::Idle;
break;
case SACTIVE:
State = ProcessState::Running;
break;
case SSTOP:
State = ProcessState::TracedOrStopped;
break;
case SZOMB:
State = ProcessState::Zombie;
break;
default:
State = ProcessState::Unknown;
break;
}
processInfo.SetIsZombie(State == ProcessState::Zombie);
processInfo.SetUserTime(convert(pstatusData.pr_utime));
processInfo.SetSystemTime(convert(pstatusData.pr_stime));
processInfo.SetCumulativeUserTime(convert(pstatusData.pr_cutime));
processInfo.SetCumulativeSystemTime(convert(pstatusData.pr_cstime));
return true;
}

static bool GetExePathAndIds(::pid_t pid, ProcessInstanceInfo &process_info) {
struct psinfo psinfoData;
auto BufferOrError = getProcFile(pid, "psinfo");
if (!BufferOrError)
return false;

std::unique_ptr<llvm::MemoryBuffer> PsinfoBuffer = std::move(*BufferOrError);
// Ensure there's enough data for psinfoData
if (PsinfoBuffer->getBufferSize() < sizeof(psinfoData))
return false;

std::memcpy(&psinfoData, PsinfoBuffer->getBufferStart(), sizeof(psinfoData));
llvm::StringRef PathRef(
psinfoData.pr_psargs,
strnlen(psinfoData.pr_psargs, sizeof(psinfoData.pr_psargs)));
if (PathRef.empty())
return false;

process_info.GetExecutableFile().SetFile(PathRef, FileSpec::Style::native);
ArchSpec arch_spec = ArchSpec();
arch_spec.SetArchitecture(eArchTypeXCOFF, XCOFF::TCPU_PPC64,
LLDB_INVALID_CPUTYPE, llvm::Triple::AIX);
process_info.SetArchitecture(arch_spec);
process_info.SetParentProcessID(psinfoData.pr_ppid);
process_info.SetGroupID(psinfoData.pr_gid);
process_info.SetEffectiveGroupID(psinfoData.pr_egid);
process_info.SetUserID(psinfoData.pr_uid);
process_info.SetEffectiveUserID(psinfoData.pr_euid);
process_info.SetProcessGroupID(psinfoData.pr_pgid);
process_info.SetProcessSessionID(psinfoData.pr_sid);
return true;
}

static bool GetProcessAndStatInfo(::pid_t pid,
ProcessInstanceInfo &process_info,
ProcessState &State) {
process_info.Clear();
process_info.SetProcessID(pid);

if (pid == LLDB_INVALID_PROCESS_ID)
return false;
// Get Executable path/Arch and Get User and Group IDs.
if (!GetExePathAndIds(pid, process_info))
return false;
// Get process status and timing info.
if (!GetStatusInfo(pid, process_info, State))
return false;

return true;
}

uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
ProcessInstanceInfoList &process_infos) {
return 0;
}

bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) {
return false;
ProcessState State;
return GetProcessAndStatInfo(pid, process_info, State);
}

Status Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Host/linux/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Host/linux/Host.h"
#include "lldb/Host/linux/Support.h"
#include "lldb/Host/posix/Support.h"
#include "lldb/Utility/DataExtractor.h"

using namespace lldb;
Expand Down
20 changes: 0 additions & 20 deletions lldb/source/Host/linux/Support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,3 @@ lldb_private::getProcFile(::pid_t pid, ::pid_t tid, const llvm::Twine &file) {
LLDB_LOG(log, "Failed to open {0}: {1}", File, Ret.getError().message());
return Ret;
}

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
lldb_private::getProcFile(::pid_t pid, const llvm::Twine &file) {
Log *log = GetLog(LLDBLog::Host);
std::string File = ("/proc/" + llvm::Twine(pid) + "/" + file).str();
auto Ret = llvm::MemoryBuffer::getFileAsStream(File);
if (!Ret)
LLDB_LOG(log, "Failed to open {0}: {1}", File, Ret.getError().message());
return Ret;
}

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
lldb_private::getProcFile(const llvm::Twine &file) {
Log *log = GetLog(LLDBLog::Host);
std::string File = ("/proc/" + file).str();
auto Ret = llvm::MemoryBuffer::getFileAsStream(File);
if (!Ret)
LLDB_LOG(log, "Failed to open {0}: {1}", File, Ret.getError().message());
return Ret;
}
32 changes: 32 additions & 0 deletions lldb/source/Host/posix/Support.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===-- Support.cpp -------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/posix/Support.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "llvm/Support/MemoryBuffer.h"

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
lldb_private::getProcFile(::pid_t pid, const llvm::Twine &file) {
Log *log = GetLog(LLDBLog::Host);
std::string File = ("/proc/" + llvm::Twine(pid) + "/" + file).str();
auto Ret = llvm::MemoryBuffer::getFileAsStream(File);
if (!Ret)
LLDB_LOG(log, "Failed to open {0}: {1}", File, Ret.getError().message());
return Ret;
}

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
lldb_private::getProcFile(const llvm::Twine &file) {
Log *log = GetLog(LLDBLog::Host);
std::string File = ("/proc/" + file).str();
auto Ret = llvm::MemoryBuffer::getFileAsStream(File);
if (!Ret)
LLDB_LOG(log, "Failed to open {0}: {1}", File, Ret.getError().message());
return Ret;
}
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Process/AIX/NativeProcessAIX.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "Plugins/Process/Utility/NativeProcessSoftwareSingleStep.h"
#include "lldb/Host/Debug.h"
#include "lldb/Host/common/NativeProcessProtocol.h"
#include "lldb/Host/linux/Support.h"
#include "lldb/Host/posix/Support.h"
#include "lldb/Target/MemoryRegionInfo.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/FileSpec.h"
Expand Down
1 change: 0 additions & 1 deletion lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "Perf.h"
#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
#include "Procfs.h"
#include "lldb/Host/linux/Support.h"
#include "lldb/Utility/StreamString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
Expand Down
1 change: 1 addition & 0 deletions lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "lldb/Host/Debug.h"
#include "lldb/Host/HostThread.h"
#include "lldb/Host/linux/Support.h"
#include "lldb/Host/posix/Support.h"
#include "lldb/Target/MemoryRegionInfo.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/FileSpec.h"
Expand Down
1 change: 0 additions & 1 deletion lldb/source/Plugins/Process/Linux/Perf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "Perf.h"

#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
#include "lldb/Host/linux/Support.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Process/Linux/Procfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

#include "Procfs.h"
#include "lldb/Host/linux/Support.h"
#include "lldb/Host/posix/Support.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
Expand Down
6 changes: 3 additions & 3 deletions lldb/unittests/Host/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ set (FILES
XMLTest.cpp
)

if (CMAKE_SYSTEM_NAME MATCHES "Linux|Android")
if (UNIX)
list(APPEND FILES
linux/HostTest.cpp
linux/SupportTest.cpp
posix/HostTest.cpp
posix/SupportTest.cpp
)
endif()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ TEST_F(HostTest, GetProcessInfo) {
triple.getEnvironment() == llvm::Triple::EnvironmentType::Android));

ProcessInstanceInfo Info;
ASSERT_FALSE(Host::GetProcessInfo(0, Info));

ASSERT_FALSE(Host::GetProcessInfo(LLDB_INVALID_PROCESS_ID, Info));

ASSERT_TRUE(Host::GetProcessInfo(getpid(), Info));

Expand Down Expand Up @@ -90,6 +91,7 @@ TEST_F(HostTest, GetProcessInfo) {
ASSERT_TRUE(user_time.tv_sec <= next_user_time.tv_sec ||
user_time.tv_usec <= next_user_time.tv_usec);

#ifndef _AIX
struct rlimit rlim;
EXPECT_EQ(getrlimit(RLIMIT_NICE, &rlim), 0);
// getpriority can return -1 so we zero errno first
Expand All @@ -108,4 +110,5 @@ TEST_F(HostTest, GetProcessInfo) {
}
ASSERT_TRUE(Info.IsZombie().has_value());
ASSERT_FALSE(Info.IsZombie().value());
#endif /* ifndef _AIX */
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/linux/Support.h"
#include "lldb/Host/posix/Support.h"
#include "llvm/Support/Threading.h"
#include "gtest/gtest.h"

using namespace lldb_private;

#ifndef __APPLE__
TEST(Support, getProcFile_Pid) {
auto BufferOrError = getProcFile(getpid(), "maps");
auto BufferOrError = getProcFile(getpid(), "status");
ASSERT_TRUE(BufferOrError);
ASSERT_TRUE(*BufferOrError);
}

#ifdef LLVM_ENABLE_THREADING
TEST(Support, getProcFile_Tid) {
auto BufferOrError = getProcFile(getpid(), llvm::get_threadid(), "comm");
ASSERT_TRUE(BufferOrError);
ASSERT_TRUE(*BufferOrError);
}
#endif /*ifdef LLVM_ENABLE_THREADING */
#endif // #ifndef __APPLE__
9 changes: 9 additions & 0 deletions lldb/unittests/Process/Linux/ProcfsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Procfs.h"

#include "lldb/Host/linux/Support.h"
#include "lldb/Host/posix/Support.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"
Expand Down Expand Up @@ -118,3 +119,11 @@ TEST(Perf, RealPtraceScope) {
ASSERT_LE(*ptrace_scope, 3)
<< "Sensible values of ptrace_scope are between 0 and 3";
}

#ifdef LLVM_ENABLE_THREADING
TEST(Support, getProcFile_Tid) {
auto BufferOrError = getProcFile(getpid(), llvm::get_threadid(), "comm");
ASSERT_TRUE(BufferOrError);
ASSERT_TRUE(*BufferOrError);
}
#endif /*ifdef LLVM_ENABLE_THREADING */
Loading