13#include <sys/ptrace.h>
14#include <sys/sysctl.h>
33#include "llvm/Object/ELF.h"
34#include "llvm/TargetParser/Host.h"
51 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, pid};
54 size_t arg_data_size =
sizeof(arg_data);
55 if (::sysctl(mib, 4, arg_data, &arg_data_size, NULL, 0) != 0)
68 char pathname[MAXPATHLEN];
69 size_t pathname_len =
sizeof(pathname);
70 mib[2] = KERN_PROC_PATHNAME;
71 if (::sysctl(mib, 4, pathname, &pathname_len, NULL, 0) == 0)
76 if (!(match_info_ptr == NULL ||
85 const uint8_t *p = data.
PeekData(offset, 1);
86 while ((p != NULL) && (*p ==
'\0') && offset < arg_data_size) {
90 if (p == NULL || offset >= arg_data_size)
102 process_info.
Clear();
106 llvm::object::getElfArchType(
107 {
reinterpret_cast<const char *
>(buffer_sp->GetBytes()),
108 size_t(buffer_sp->GetByteSize())})
112 case llvm::ELF::ELFCLASS32:
114 HostInfo::GetArchitecture(HostInfo::eArchKind32));
116 case llvm::ELF::ELFCLASS64:
118 HostInfo::GetArchitecture(HostInfo::eArchKind64));
120 case llvm::ELF::ELFCLASSNONE:
122 HostInfo::GetArchitecture(HostInfo::eArchKindDefault));
130 struct kinfo_proc proc_kinfo;
131 size_t proc_kinfo_size;
133 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
138 proc_kinfo_size =
sizeof(
struct kinfo_proc);
140 if (::sysctl(mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) != 0)
143 if (proc_kinfo_size == 0)
147 process_info.
SetUserID(proc_kinfo.ki_ruid);
150 if (proc_kinfo.ki_ngroups > 0)
167 const ::pid_t our_pid = ::getpid();
168 const ::uid_t our_uid = ::getuid();
169 std::vector<struct kinfo_proc> kinfos;
173 int mib[3] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
175 size_t pid_data_size = 0;
176 if (::sysctl(mib, 3, NULL, &pid_data_size, NULL, 0) != 0)
180 const size_t estimated_pid_count =
181 (pid_data_size /
sizeof(
struct kinfo_proc)) + 10;
183 kinfos.resize(estimated_pid_count);
184 pid_data_size = kinfos.size() *
sizeof(
struct kinfo_proc);
186 if (::sysctl(mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0)
189 const size_t actual_pid_count = (pid_data_size /
sizeof(
struct kinfo_proc));
191 ProcessInstanceInfoMatch match_info_noname{match_info};
194 for (
size_t i = 0; i < actual_pid_count; i++) {
195 const struct kinfo_proc &kinfo = kinfos[i];
198 if (!all_users && kinfo.ki_ruid != our_uid)
201 if (kinfo.ki_pid == our_pid ||
203 kinfo.ki_stat == SZOMB ||
204 kinfo.ki_flag & P_TRACED ||
205 kinfo.ki_flag & P_WEXIT)
211 bool already_registered =
false;
212 for (uint32_t pi = 0;
213 !already_registered && (
const int)kinfo.ki_numthreads > 1 &&
214 pi < (
const uint32_t)process_infos.size();
217 (process_infos[pi].GetProcessID() == (uint32_t)kinfo.ki_pid);
219 if (already_registered)
222 ProcessInstanceInfo process_info;
231 if (match_info_noname.
Matches(process_info) &&
233 if (match_info.
Matches(process_info))
234 process_infos.push_back(process_info);
238 return process_infos.size();
250 process_info.
Clear();
static llvm::raw_ostream & error(Stream &strm)
A command line argument class.
void AppendArgument(llvm::StringRef arg_str, char quote_char='\0')
Appends a new argument to the end of the list argument list.
const char * GetCString() const
Get the string value as a C string.
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
const ConstString & GetFilename() const
Filename string const get accessor.
static FileSystem & Instance()
std::shared_ptr< DataBuffer > CreateDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
Create memory buffer from 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)
bool ProcessIDIsValid() const
void SetArg0(llvm::StringRef arg)
const char * GetName() const
lldb::pid_t GetProcessID() const
void SetProcessID(lldb::pid_t pid)
FileSpec & GetExecutableFile()
void SetUserID(uint32_t uid)
NameMatch GetNameMatchType() const
bool Matches(const ProcessInstanceInfo &proc_info) const
bool GetMatchAllUsers() const
void SetNameMatchType(NameMatch name_match_type)
ProcessInstanceInfo & GetProcessInfo()
void SetEffectiveGroupID(uint32_t gid)
void SetParentProcessID(lldb::pid_t pid)
void SetEffectiveUserID(uint32_t uid)
static Status FromErrorString(const char *str)
static bool GetFreeBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr, ProcessInstanceInfo &process_info)
static bool GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info)
#define LLDB_INVALID_PROCESS_ID
lldb::ByteOrder InlHostByteOrder()
A class that represents a running process on the host machine.
bool NameMatches(llvm::StringRef name, NameMatch match_type, llvm::StringRef match)
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList