2011-08-03 04:52:42 +08:00
|
|
|
//===-- source/Host/freebsd/Host.cpp ------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// C Includes
|
|
|
|
#include <stdio.h>
|
2012-01-06 03:17:38 +08:00
|
|
|
#include <dlfcn.h>
|
2011-08-03 04:52:42 +08:00
|
|
|
#include <execinfo.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
#include <sys/sysctl.h>
|
2013-08-27 07:57:52 +08:00
|
|
|
#include <sys/proc.h>
|
2011-08-03 04:52:42 +08:00
|
|
|
|
2012-01-06 03:17:38 +08:00
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <machine/elf.h>
|
|
|
|
|
2011-08-03 04:52:42 +08:00
|
|
|
// C++ Includes
|
|
|
|
// Other libraries and framework includes
|
|
|
|
// Project includes
|
|
|
|
#include "lldb/Core/Error.h"
|
|
|
|
#include "lldb/Host/Endian.h"
|
|
|
|
#include "lldb/Host/Host.h"
|
2014-08-21 00:42:51 +08:00
|
|
|
#include "lldb/Host/HostInfo.h"
|
2013-08-27 07:57:52 +08:00
|
|
|
#include "lldb/Core/Module.h"
|
2011-08-03 04:52:42 +08:00
|
|
|
#include "lldb/Core/DataExtractor.h"
|
|
|
|
#include "lldb/Core/StreamFile.h"
|
|
|
|
#include "lldb/Core/StreamString.h"
|
2013-08-28 00:03:22 +08:00
|
|
|
#include "lldb/Core/Log.h"
|
2011-08-03 04:52:42 +08:00
|
|
|
#include "lldb/Target/Process.h"
|
2013-08-27 07:57:52 +08:00
|
|
|
#include "lldb/Target/Platform.h"
|
2011-08-03 04:52:42 +08:00
|
|
|
|
2012-01-06 03:17:38 +08:00
|
|
|
#include "lldb/Core/DataBufferHeap.h"
|
|
|
|
#include "lldb/Core/DataExtractor.h"
|
2013-08-27 07:57:52 +08:00
|
|
|
#include "lldb/Utility/CleanUp.h"
|
2015-03-19 10:47:36 +08:00
|
|
|
#include "lldb/Utility/NameMatches.h"
|
2013-08-27 07:57:52 +08:00
|
|
|
|
2014-08-30 01:35:57 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-01-06 03:17:38 +08:00
|
|
|
|
2011-08-03 04:52:42 +08:00
|
|
|
extern "C" {
|
2012-01-06 03:17:38 +08:00
|
|
|
extern char **environ;
|
2011-08-03 04:52:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
size_t
|
|
|
|
Host::GetEnvironment (StringList &env)
|
|
|
|
{
|
|
|
|
char *v;
|
|
|
|
char **var = environ;
|
2013-08-27 07:57:52 +08:00
|
|
|
for (; var != NULL && *var != NULL; ++var)
|
|
|
|
{
|
2011-08-03 04:52:42 +08:00
|
|
|
v = strchr(*var, (int)'-');
|
|
|
|
if (v == NULL)
|
|
|
|
continue;
|
|
|
|
env.AppendString(v);
|
|
|
|
}
|
|
|
|
return env.GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetFreeBSDProcessArgs (const ProcessInstanceInfoMatch *match_info_ptr,
|
|
|
|
ProcessInstanceInfo &process_info)
|
|
|
|
{
|
2013-08-27 07:57:52 +08:00
|
|
|
if (process_info.ProcessIDIsValid())
|
|
|
|
{
|
2012-09-12 02:11:16 +08:00
|
|
|
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ARGS, (int)process_info.GetProcessID() };
|
2011-08-03 04:52:42 +08:00
|
|
|
|
|
|
|
char arg_data[8192];
|
|
|
|
size_t arg_data_size = sizeof(arg_data);
|
2012-01-06 03:17:38 +08:00
|
|
|
if (::sysctl (mib, 4, arg_data, &arg_data_size , NULL, 0) == 0)
|
2011-08-03 04:52:42 +08:00
|
|
|
{
|
2015-11-07 12:40:13 +08:00
|
|
|
DataExtractor data (arg_data, arg_data_size, endian::InlHostByteOrder(), sizeof(void *));
|
2013-05-02 04:38:19 +08:00
|
|
|
lldb::offset_t offset = 0;
|
2011-08-03 04:52:42 +08:00
|
|
|
const char *cstr;
|
2012-01-06 03:17:38 +08:00
|
|
|
|
2011-08-03 04:52:42 +08:00
|
|
|
cstr = data.GetCStr (&offset);
|
|
|
|
if (cstr)
|
|
|
|
{
|
|
|
|
process_info.GetExecutableFile().SetFile(cstr, false);
|
|
|
|
|
2013-06-24 23:09:18 +08:00
|
|
|
if (!(match_info_ptr == NULL ||
|
2011-08-03 04:52:42 +08:00
|
|
|
NameMatches (process_info.GetExecutableFile().GetFilename().GetCString(),
|
|
|
|
match_info_ptr->GetNameMatchType(),
|
2012-01-06 03:17:38 +08:00
|
|
|
match_info_ptr->GetProcessInfo().GetName())))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Args &proc_args = process_info.GetArguments();
|
|
|
|
while (1)
|
2011-08-03 04:52:42 +08:00
|
|
|
{
|
2012-01-06 03:17:38 +08:00
|
|
|
const uint8_t *p = data.PeekData(offset, 1);
|
|
|
|
while ((p != NULL) && (*p == '\0') && offset < arg_data_size)
|
2011-08-03 04:52:42 +08:00
|
|
|
{
|
|
|
|
++offset;
|
2012-01-06 03:17:38 +08:00
|
|
|
p = data.PeekData(offset, 1);
|
2011-08-03 04:52:42 +08:00
|
|
|
}
|
2012-01-06 03:17:38 +08:00
|
|
|
if (p == NULL || offset >= arg_data_size)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
cstr = data.GetCStr(&offset);
|
|
|
|
if (cstr)
|
|
|
|
proc_args.AppendArgument(cstr);
|
|
|
|
else
|
|
|
|
return true;
|
2011-08-03 04:52:42 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-24 23:09:18 +08:00
|
|
|
}
|
2011-08-03 04:52:42 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetFreeBSDProcessCPUType (ProcessInstanceInfo &process_info)
|
|
|
|
{
|
2013-08-27 07:57:52 +08:00
|
|
|
if (process_info.ProcessIDIsValid())
|
|
|
|
{
|
2014-08-21 00:42:51 +08:00
|
|
|
process_info.GetArchitecture() = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
|
2012-01-06 03:17:38 +08:00
|
|
|
return true;
|
2011-08-03 04:52:42 +08:00
|
|
|
}
|
|
|
|
process_info.GetArchitecture().Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
GetFreeBSDProcessUserAndGroup(ProcessInstanceInfo &process_info)
|
|
|
|
{
|
|
|
|
struct kinfo_proc proc_kinfo;
|
|
|
|
size_t proc_kinfo_size;
|
|
|
|
|
2013-06-24 23:09:18 +08:00
|
|
|
if (process_info.ProcessIDIsValid())
|
2011-08-03 04:52:42 +08:00
|
|
|
{
|
|
|
|
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID,
|
2012-09-12 02:11:16 +08:00
|
|
|
(int)process_info.GetProcessID() };
|
2011-08-03 04:52:42 +08:00
|
|
|
proc_kinfo_size = sizeof(struct kinfo_proc);
|
|
|
|
|
|
|
|
if (::sysctl (mib, 4, &proc_kinfo, &proc_kinfo_size, NULL, 0) == 0)
|
|
|
|
{
|
|
|
|
if (proc_kinfo_size > 0)
|
|
|
|
{
|
|
|
|
process_info.SetParentProcessID (proc_kinfo.ki_ppid);
|
|
|
|
process_info.SetUserID (proc_kinfo.ki_ruid);
|
|
|
|
process_info.SetGroupID (proc_kinfo.ki_rgid);
|
|
|
|
process_info.SetEffectiveUserID (proc_kinfo.ki_uid);
|
|
|
|
if (proc_kinfo.ki_ngroups > 0)
|
|
|
|
process_info.SetEffectiveGroupID (proc_kinfo.ki_groups[0]);
|
|
|
|
else
|
2012-01-06 03:17:38 +08:00
|
|
|
process_info.SetEffectiveGroupID (UINT32_MAX);
|
2011-08-03 04:52:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
process_info.SetParentProcessID (LLDB_INVALID_PROCESS_ID);
|
|
|
|
process_info.SetUserID (UINT32_MAX);
|
|
|
|
process_info.SetGroupID (UINT32_MAX);
|
|
|
|
process_info.SetEffectiveUserID (UINT32_MAX);
|
2012-01-06 03:17:38 +08:00
|
|
|
process_info.SetEffectiveGroupID (UINT32_MAX);
|
2011-08-03 04:52:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
uint32_t
|
|
|
|
Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
|
|
|
|
{
|
|
|
|
std::vector<struct kinfo_proc> kinfos;
|
|
|
|
|
|
|
|
int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL };
|
|
|
|
|
|
|
|
size_t pid_data_size = 0;
|
|
|
|
if (::sysctl (mib, 3, NULL, &pid_data_size, NULL, 0) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Add a few extra in case a few more show up
|
|
|
|
const size_t estimated_pid_count = (pid_data_size / sizeof(struct kinfo_proc)) + 10;
|
|
|
|
|
|
|
|
kinfos.resize (estimated_pid_count);
|
|
|
|
pid_data_size = kinfos.size() * sizeof(struct kinfo_proc);
|
|
|
|
|
|
|
|
if (::sysctl (mib, 3, &kinfos[0], &pid_data_size, NULL, 0) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const size_t actual_pid_count = (pid_data_size / sizeof(struct kinfo_proc));
|
|
|
|
|
|
|
|
bool all_users = match_info.GetMatchAllUsers();
|
2014-02-28 21:46:51 +08:00
|
|
|
const ::pid_t our_pid = getpid();
|
2013-08-27 07:57:52 +08:00
|
|
|
const uid_t our_uid = getuid();
|
2014-02-28 21:46:51 +08:00
|
|
|
for (size_t i = 0; i < actual_pid_count; i++)
|
2013-08-27 07:57:52 +08:00
|
|
|
{
|
|
|
|
const struct kinfo_proc &kinfo = kinfos[i];
|
|
|
|
const bool kinfo_user_matches = (all_users ||
|
|
|
|
(kinfo.ki_ruid == our_uid) ||
|
|
|
|
// Special case, if lldb is being run as root we can attach to anything.
|
|
|
|
(our_uid == 0)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (kinfo_user_matches == false || // Make sure the user is acceptable
|
|
|
|
kinfo.ki_pid == our_pid || // Skip this process
|
|
|
|
kinfo.ki_pid == 0 || // Skip kernel (kernel pid is zero)
|
|
|
|
kinfo.ki_stat == SZOMB || // Zombies are bad, they like brains...
|
|
|
|
kinfo.ki_flag & P_TRACED || // Being debugged?
|
|
|
|
kinfo.ki_flag & P_WEXIT) // Working on exiting
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Every thread is a process in FreeBSD, but all the threads of a single process
|
|
|
|
// have the same pid. Do not store the process info in the result list if a process
|
|
|
|
// with given identifier is already registered there.
|
|
|
|
bool already_registered = false;
|
|
|
|
for (uint32_t pi = 0;
|
|
|
|
!already_registered &&
|
|
|
|
(const int)kinfo.ki_numthreads > 1 &&
|
|
|
|
pi < (const uint32_t)process_infos.GetSize(); pi++)
|
|
|
|
already_registered = (process_infos.GetProcessIDAtIndex(pi) == (uint32_t)kinfo.ki_pid);
|
|
|
|
|
|
|
|
if (already_registered)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ProcessInstanceInfo process_info;
|
|
|
|
process_info.SetProcessID (kinfo.ki_pid);
|
|
|
|
process_info.SetParentProcessID (kinfo.ki_ppid);
|
|
|
|
process_info.SetUserID (kinfo.ki_ruid);
|
|
|
|
process_info.SetGroupID (kinfo.ki_rgid);
|
|
|
|
process_info.SetEffectiveUserID (kinfo.ki_svuid);
|
|
|
|
process_info.SetEffectiveGroupID (kinfo.ki_svgid);
|
|
|
|
|
|
|
|
// Make sure our info matches before we go fetch the name and cpu type
|
|
|
|
if (match_info.Matches (process_info) &&
|
|
|
|
GetFreeBSDProcessArgs (&match_info, process_info))
|
|
|
|
{
|
|
|
|
GetFreeBSDProcessCPUType (process_info);
|
|
|
|
if (match_info.Matches (process_info))
|
|
|
|
process_infos.Append (process_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return process_infos.GetSize();
|
|
|
|
}
|
|
|
|
|
2011-08-03 04:52:42 +08:00
|
|
|
bool
|
|
|
|
Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
|
|
|
|
{
|
|
|
|
process_info.SetProcessID(pid);
|
2013-08-27 07:57:52 +08:00
|
|
|
|
|
|
|
if (GetFreeBSDProcessArgs(NULL, process_info))
|
|
|
|
{
|
2011-08-03 04:52:42 +08:00
|
|
|
// should use libprocstat instead of going right into sysctl?
|
|
|
|
GetFreeBSDProcessCPUType(process_info);
|
|
|
|
GetFreeBSDProcessUserAndGroup(process_info);
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-27 07:57:52 +08:00
|
|
|
|
2011-08-03 04:52:42 +08:00
|
|
|
process_info.Clear();
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-06 03:17:38 +08:00
|
|
|
|
|
|
|
lldb::DataBufferSP
|
|
|
|
Host::GetAuxvData(lldb_private::Process *process)
|
|
|
|
{
|
2014-11-02 05:27:08 +08:00
|
|
|
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_AUXV, 0 };
|
|
|
|
size_t auxv_size = AT_COUNT * sizeof(Elf_Auxinfo);
|
2012-01-06 03:17:38 +08:00
|
|
|
DataBufferSP buf_sp;
|
2014-10-31 10:34:28 +08:00
|
|
|
|
2014-11-02 05:27:08 +08:00
|
|
|
std::unique_ptr<DataBufferHeap> buf_ap(new DataBufferHeap(auxv_size, 0));
|
|
|
|
|
|
|
|
mib[3] = process->GetID();
|
|
|
|
if (::sysctl(mib, 4, buf_ap->GetBytes(), &auxv_size, NULL, 0) == 0) {
|
2012-01-06 03:17:38 +08:00
|
|
|
buf_sp.reset(buf_ap.release());
|
|
|
|
} else {
|
2014-11-02 05:27:08 +08:00
|
|
|
perror("sysctl failed on auxv");
|
2012-01-06 03:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf_sp;
|
|
|
|
}
|
2014-08-30 01:35:57 +08:00
|
|
|
|
2015-02-21 05:48:38 +08:00
|
|
|
Error
|
2015-02-21 06:20:30 +08:00
|
|
|
Host::ShellExpandArguments (ProcessLaunchInfo &launch_info)
|
2015-02-21 05:48:38 +08:00
|
|
|
{
|
|
|
|
return Error("unimplemented");
|
|
|
|
}
|
|
|
|
|