16#include "llvm/BinaryFormat/XCOFF.h"
17#include "llvm/Object/XCOFFObjectFile.h"
18#include "llvm/Support/MemoryBuffer.h"
21#include <sys/procfs.h>
27enum class ProcessState {
49 ProcessState &State) {
50 struct pstatus pstatusData;
55 std::unique_ptr<llvm::MemoryBuffer> StatusBuffer = std::move(*BufferOrError);
57 if (StatusBuffer->getBufferSize() <
sizeof(pstatusData))
60 std::memcpy(&pstatusData, StatusBuffer->getBufferStart(),
62 switch (pstatusData.pr_stat) {
64 State = ProcessState::Idle;
67 State = ProcessState::Running;
70 State = ProcessState::TracedOrStopped;
73 State = ProcessState::Zombie;
76 State = ProcessState::Unknown;
79 processInfo.
SetIsZombie(State == ProcessState::Zombie);
90 auto file_buffer = llvm::MemoryBuffer::getFile(exe_path);
94 llvm::Expected<std::unique_ptr<llvm::object::ObjectFile>> obj_or_err =
95 llvm::object::ObjectFile::createObjectFile(
96 (*file_buffer)->getMemBufferRef());
99 LLDB_LOG(log,
"failed to create ObjectFile from buffer");
103 llvm::object::ObjectFile *obj = obj_or_err->get();
105 const llvm::object::XCOFFObjectFile *xcoff_obj =
106 llvm::dyn_cast<llvm::object::XCOFFObjectFile>(obj);
108 LLDB_LOG(log,
"Not an valid XCOFF object file: {0}", exe_path);
112 if (xcoff_obj->is64Bit())
113 return HostInfo::GetArchitecture(HostInfo::eArchKind64);
115 return HostInfo::GetArchitecture(HostInfo::eArchKind32);
119 struct psinfo psinfoData;
124 std::unique_ptr<llvm::MemoryBuffer> PsinfoBuffer = std::move(*BufferOrError);
126 if (PsinfoBuffer->getBufferSize() <
sizeof(psinfoData))
129 std::memcpy(&psinfoData, PsinfoBuffer->getBufferStart(),
sizeof(psinfoData));
130 llvm::StringRef PathRef(
131 psinfoData.pr_psargs,
132 strnlen(psinfoData.pr_psargs,
sizeof(psinfoData.pr_psargs)));
144 process_info.
SetUserID(psinfoData.pr_uid);
153 ProcessState &State) {
154 process_info.
Clear();
171 static const char procdir[] =
"/proc/";
173 DIR *dirproc = opendir(procdir);
175 struct dirent *direntry =
nullptr;
176 const uid_t our_uid = getuid();
180 while ((direntry = readdir(dirproc)) !=
nullptr) {
183 if (!llvm::to_integer(direntry->d_name, pid))
194 if (
State == ProcessState::Zombie ||
195 State == ProcessState::TracedOrStopped)
200 if (!all_users && (our_uid != 0) && (process_info.
GetUserID() != our_uid))
203 if (match_info.
Matches(process_info))
204 process_infos.push_back(process_info);
208 return process_infos.size();
217 return Status(
"unimplemented");
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
static ProcessInstanceInfo::timespec convert(pr_timestruc64_t t)
static bool GetExePathAndIds(::pid_t pid, ProcessInstanceInfo &process_info)
static bool GetStatusInfo(::pid_t pid, ProcessInstanceInfo &processInfo, ProcessState &State)
static ArchSpec GetXCOFFProcessCPUType(llvm::StringRef exe_path)
static bool GetProcessAndStatInfo(::pid_t pid, ProcessInstanceInfo &process_info, ProcessState &State)
An architecture specification class.
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
static Status ShellExpandArguments(ProcessLaunchInfo &launch_info)
Perform expansion of the command-line for this launch info This can potentially involve wildcard expa...
static uint32_t FindProcessesImpl(const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &proc_infos)
static bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &proc_info)
void SetGroupID(uint32_t gid)
void SetArchitecture(const ArchSpec &arch)
void SetProcessID(lldb::pid_t pid)
FileSpec & GetExecutableFile()
uint32_t GetUserID() const
void SetUserID(uint32_t uid)
bool Matches(const ProcessInstanceInfo &proc_info) const
bool GetMatchAllUsers() const
void SetUserTime(struct timespec utime)
void SetEffectiveGroupID(uint32_t gid)
void SetIsZombie(bool is_zombie)
void SetCumulativeUserTime(struct timespec cutime)
void SetProcessGroupID(lldb::pid_t pgrp)
void SetProcessSessionID(lldb::pid_t session)
void SetCumulativeSystemTime(struct timespec cstime)
void SetSystemTime(struct timespec stime)
void SetParentProcessID(lldb::pid_t pid)
void SetEffectiveUserID(uint32_t uid)
#define LLDB_INVALID_PROCESS_ID
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getProcFile(::pid_t pid, ::pid_t tid, const llvm::Twine &file)
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList