2020-03-17 18:41:33 +08:00
|
|
|
//===-- source/Host/netbsd/HostNetBSD.cpp ---------------------------------===//
|
2015-10-13 13:04:13 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2015-10-13 13:04:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-05-26 18:19:37 +08:00
|
|
|
#include <cstdio>
|
2015-10-13 13:04:13 +08:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <execinfo.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2021-05-26 18:19:37 +08:00
|
|
|
#include <climits>
|
2015-10-13 13:04:13 +08:00
|
|
|
|
|
|
|
#include <kvm.h>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/ptrace.h>
|
|
|
|
|
2020-01-13 05:49:36 +08:00
|
|
|
#include "lldb/Host/FileSystem.h"
|
2015-10-13 13:04:13 +08:00
|
|
|
#include "lldb/Host/Host.h"
|
|
|
|
#include "lldb/Host/HostInfo.h"
|
2017-06-30 19:31:13 +08:00
|
|
|
#include "lldb/Utility/DataBufferHeap.h"
|
2017-03-04 09:30:05 +08:00
|
|
|
#include "lldb/Utility/DataExtractor.h"
|
2017-02-15 03:06:07 +08:00
|
|
|
#include "lldb/Utility/Endian.h"
|
2022-02-03 20:26:10 +08:00
|
|
|
#include "lldb/Utility/LLDBLog.h"
|
2017-03-04 04:56:28 +08:00
|
|
|
#include "lldb/Utility/Log.h"
|
2017-06-30 19:31:13 +08:00
|
|
|
#include "lldb/Utility/NameMatches.h"
|
Move ProcessInfo from Host to Utility.
There are set of classes in Target that describe the parameters of a
process - e.g. it's PID, name, user id, and similar. However, since it
is a bare description of a process and contains no actual functionality,
there's nothing specifically that makes this appropriate for being in
Target -- it could just as well be describing a process on the host, or
some hypothetical virtual process that doesn't even exist.
To cement this, I'm moving these classes to Utility. It's possible that
we can find a better place for it in the future, but as it is neither
Host specific nor Target specific, Utility seems like the most appropriate
place for the time being.
After this there is only 2 remaining references to Target from Host,
which I'll address in a followup.
Differential Revision: https://reviews.llvm.org/D58842
llvm-svn: 355342
2019-03-05 05:51:03 +08:00
|
|
|
#include "lldb/Utility/ProcessInfo.h"
|
2017-05-12 12:51:55 +08:00
|
|
|
#include "lldb/Utility/Status.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/StreamString.h"
|
2015-10-13 13:04:13 +08:00
|
|
|
|
2020-01-13 05:49:36 +08:00
|
|
|
#include "llvm/Object/ELF.h"
|
2015-10-13 13:04:13 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
extern char **environ;
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
Move ProcessInfo from Host to Utility.
There are set of classes in Target that describe the parameters of a
process - e.g. it's PID, name, user id, and similar. However, since it
is a bare description of a process and contains no actual functionality,
there's nothing specifically that makes this appropriate for being in
Target -- it could just as well be describing a process on the host, or
some hypothetical virtual process that doesn't even exist.
To cement this, I'm moving these classes to Utility. It's possible that
we can find a better place for it in the future, but as it is neither
Host specific nor Target specific, Utility seems like the most appropriate
place for the time being.
After this there is only 2 remaining references to Target from Host,
which I'll address in a followup.
Differential Revision: https://reviews.llvm.org/D58842
llvm-svn: 355342
2019-03-05 05:51:03 +08:00
|
|
|
namespace lldb_private {
|
|
|
|
class ProcessLaunchInfo;
|
|
|
|
}
|
|
|
|
|
Add Utility/Environment class for handling... environments
Summary:
There was some confusion in the code about how to represent process
environment. Most of the code (ab)used the Args class for this purpose,
but some of it used a more basic StringList class instead. In either
case, the fact that the underlying abstraction did not provide primitive
operations for the typical environment operations meant that even a
simple operation like checking for an environment variable value was
several lines of code.
This patch adds a separate Environment class, which is essentialy a
llvm::StringMap<std::string> in disguise. To standard StringMap
functionality, it adds a couple of new functions, which are specific to
the environment use case:
- (most important) envp conversion for passing into execve() and likes.
Instead of trying to maintain a constantly up-to-date envp view, it
provides a function which creates a envp view on demand, with the
expectation that this will be called as the very last thing before
handing the value to the system function.
- insert(StringRef KeyEqValue) - splits KeyEqValue into (key, value)
pair and inserts it into the environment map.
- compose(value_type KeyValue) - takes a map entry and converts in back
into "KEY=VALUE" representation.
With this interface most of the environment-manipulating code becomes
one-liners. The only tricky part was maintaining compatibility in
SBLaunchInfo, which expects that the environment entries are accessible
by index and that the returned const char* is backed by the launch info
object (random access into maps is hard and the map stores the entry in
a deconstructed form, so we cannot just return a .c_str() value). To
solve this, I have the SBLaunchInfo convert the environment into the
"envp" form, and use it to answer the environment queries. Extra code is
added to make sure the envp version is always in sync.
(This also improves the layering situation as Args was in the Interpreter module
whereas Environment is in Utility.)
Reviewers: zturner, davide, jingham, clayborg
Subscribers: emaste, lldb-commits, mgorny
Differential Revision: https://reviews.llvm.org/D41359
llvm-svn: 322174
2018-01-10 19:57:31 +08:00
|
|
|
Environment Host::GetEnvironment() { return Environment(environ); }
|
2015-10-13 13:04:13 +08:00
|
|
|
|
|
|
|
static bool GetNetBSDProcessArgs(const ProcessInstanceInfoMatch *match_info_ptr,
|
|
|
|
ProcessInstanceInfo &process_info) {
|
|
|
|
if (!process_info.ProcessIDIsValid())
|
|
|
|
return false;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-13 13:04:13 +08:00
|
|
|
int pid = process_info.GetProcessID();
|
|
|
|
|
|
|
|
int mib[4] = {CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_ARGV};
|
|
|
|
|
|
|
|
char arg_data[8192];
|
|
|
|
size_t arg_data_size = sizeof(arg_data);
|
|
|
|
if (::sysctl(mib, 4, arg_data, &arg_data_size, NULL, 0) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DataExtractor data(arg_data, arg_data_size, endian::InlHostByteOrder(),
|
2015-11-07 12:40:13 +08:00
|
|
|
sizeof(void *));
|
2015-10-13 13:04:13 +08:00
|
|
|
lldb::offset_t offset = 0;
|
|
|
|
const char *cstr;
|
|
|
|
|
2015-11-07 12:40:13 +08:00
|
|
|
cstr = data.GetCStr(&offset);
|
2015-10-13 13:04:13 +08:00
|
|
|
if (!cstr)
|
|
|
|
return false;
|
|
|
|
|
2018-11-05 00:53:16 +08:00
|
|
|
process_info.GetExecutableFile().SetFile(cstr,
|
2018-06-14 06:23:48 +08:00
|
|
|
FileSpec::Style::native);
|
2015-10-13 13:04:13 +08:00
|
|
|
|
|
|
|
if (!(match_info_ptr == NULL ||
|
|
|
|
NameMatches(process_info.GetExecutableFile().GetFilename().GetCString(),
|
|
|
|
match_info_ptr->GetNameMatchType(),
|
|
|
|
match_info_ptr->GetProcessInfo().GetName())))
|
|
|
|
return false;
|
|
|
|
|
2019-10-25 01:47:49 +08:00
|
|
|
process_info.SetArg0(cstr);
|
2015-10-13 13:04:13 +08:00
|
|
|
Args &proc_args = process_info.GetArguments();
|
|
|
|
while (1) {
|
|
|
|
const uint8_t *p = data.PeekData(offset, 1);
|
|
|
|
while ((p != NULL) && (*p == '\0') && offset < arg_data_size) {
|
|
|
|
++offset;
|
|
|
|
p = data.PeekData(offset, 1);
|
|
|
|
}
|
|
|
|
if (p == NULL || offset >= arg_data_size)
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
2015-10-13 13:04:13 +08:00
|
|
|
|
|
|
|
cstr = data.GetCStr(&offset);
|
|
|
|
if (!cstr)
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
|
2016-09-21 06:26:29 +08:00
|
|
|
proc_args.AppendArgument(llvm::StringRef(cstr));
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2015-10-13 13:04:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool GetNetBSDProcessCPUType(ProcessInstanceInfo &process_info) {
|
2022-01-31 22:57:48 +08:00
|
|
|
Log *log = GetLog(LLDBLog::Host);
|
2020-01-13 05:49:36 +08:00
|
|
|
|
2015-10-13 13:04:13 +08:00
|
|
|
if (process_info.ProcessIDIsValid()) {
|
2020-01-13 05:49:36 +08:00
|
|
|
auto buffer_sp = FileSystem::Instance().CreateDataBuffer(
|
|
|
|
process_info.GetExecutableFile(), 0x20, 0);
|
|
|
|
if (buffer_sp) {
|
2022-04-02 08:41:36 +08:00
|
|
|
uint8_t exe_class = llvm::object::getElfArchType(
|
|
|
|
{reinterpret_cast<char *>(buffer_sp->GetBytes()),
|
|
|
|
size_t(buffer_sp->GetByteSize())})
|
|
|
|
.first;
|
2020-01-13 05:49:36 +08:00
|
|
|
|
|
|
|
switch (exe_class) {
|
|
|
|
case llvm::ELF::ELFCLASS32:
|
|
|
|
process_info.GetArchitecture() =
|
|
|
|
HostInfo::GetArchitecture(HostInfo::eArchKind32);
|
|
|
|
return true;
|
|
|
|
case llvm::ELF::ELFCLASS64:
|
|
|
|
process_info.GetArchitecture() =
|
|
|
|
HostInfo::GetArchitecture(HostInfo::eArchKind64);
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
LLDB_LOG(log, "Unknown elf class ({0}) in file {1}", exe_class,
|
|
|
|
process_info.GetExecutableFile());
|
|
|
|
}
|
|
|
|
}
|
2015-10-13 13:04:13 +08:00
|
|
|
}
|
|
|
|
process_info.GetArchitecture().Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool GetNetBSDProcessUserAndGroup(ProcessInstanceInfo &process_info) {
|
|
|
|
::kvm_t *kdp;
|
|
|
|
char errbuf[_POSIX2_LINE_MAX]; /* XXX: error string unused */
|
|
|
|
|
|
|
|
struct ::kinfo_proc2 *proc_kinfo;
|
|
|
|
const int pid = process_info.GetProcessID();
|
|
|
|
int nproc;
|
|
|
|
|
|
|
|
if (!process_info.ProcessIDIsValid())
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((kdp = ::kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf)) == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((proc_kinfo = ::kvm_getproc2(kdp, KERN_PROC_PID, pid,
|
|
|
|
sizeof(struct ::kinfo_proc2), &nproc)) ==
|
|
|
|
NULL) {
|
|
|
|
::kvm_close(kdp);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nproc < 1) {
|
|
|
|
::kvm_close(kdp); /* XXX: we don't check for error here */
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
process_info.SetParentProcessID(proc_kinfo->p_ppid);
|
|
|
|
process_info.SetUserID(proc_kinfo->p_ruid);
|
|
|
|
process_info.SetGroupID(proc_kinfo->p_rgid);
|
|
|
|
process_info.SetEffectiveUserID(proc_kinfo->p_uid);
|
|
|
|
process_info.SetEffectiveGroupID(proc_kinfo->p_gid);
|
|
|
|
|
|
|
|
::kvm_close(kdp); /* XXX: we don't check for error here */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
error:
|
|
|
|
process_info.SetParentProcessID(LLDB_INVALID_PROCESS_ID);
|
|
|
|
process_info.SetUserID(UINT32_MAX);
|
|
|
|
process_info.SetGroupID(UINT32_MAX);
|
|
|
|
process_info.SetEffectiveUserID(UINT32_MAX);
|
|
|
|
process_info.SetEffectiveGroupID(UINT32_MAX);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-13 23:49:15 +08:00
|
|
|
uint32_t Host::FindProcessesImpl(const ProcessInstanceInfoMatch &match_info,
|
|
|
|
ProcessInstanceInfoList &process_infos) {
|
2015-10-13 13:04:13 +08:00
|
|
|
const ::pid_t our_pid = ::getpid();
|
|
|
|
const ::uid_t our_uid = ::getuid();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-13 13:04:13 +08:00
|
|
|
const bool all_users =
|
|
|
|
match_info.GetMatchAllUsers() ||
|
|
|
|
// Special case, if lldb is being run as root we can attach to anything
|
|
|
|
(our_uid == 0);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-13 13:04:13 +08:00
|
|
|
kvm_t *kdp;
|
|
|
|
char errbuf[_POSIX2_LINE_MAX]; /* XXX: error string unused */
|
|
|
|
if ((kdp = ::kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf)) == NULL)
|
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-13 13:04:13 +08:00
|
|
|
struct ::kinfo_proc2 *proc_kinfo;
|
|
|
|
int nproc;
|
|
|
|
if ((proc_kinfo = ::kvm_getproc2(kdp, KERN_PROC_ALL, 0,
|
|
|
|
sizeof(struct ::kinfo_proc2), &nproc)) ==
|
|
|
|
NULL) {
|
|
|
|
::kvm_close(kdp);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2020-10-30 19:00:45 +08:00
|
|
|
ProcessInstanceInfoMatch match_info_noname{match_info};
|
|
|
|
match_info_noname.SetNameMatchType(NameMatch::Ignore);
|
|
|
|
|
2015-10-13 13:04:13 +08:00
|
|
|
for (int i = 0; i < nproc; i++) {
|
|
|
|
if (proc_kinfo[i].p_pid < 1)
|
|
|
|
continue; /* not valid */
|
|
|
|
/* Make sure the user is acceptable */
|
|
|
|
if (!all_users && proc_kinfo[i].p_ruid != our_uid)
|
|
|
|
continue;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-13 13:04:13 +08:00
|
|
|
if (proc_kinfo[i].p_pid == our_pid || // Skip this process
|
|
|
|
proc_kinfo[i].p_pid == 0 || // Skip kernel (kernel pid is 0)
|
|
|
|
proc_kinfo[i].p_stat == LSZOMB || // Zombies are bad
|
|
|
|
proc_kinfo[i].p_flag & P_TRACED || // Being debugged?
|
|
|
|
proc_kinfo[i].p_flag & P_WEXIT) // Working on exiting
|
|
|
|
continue;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-13 13:04:13 +08:00
|
|
|
// Every thread is a process in NetBSD, but all the threads of a single
|
2018-05-01 00:49:04 +08:00
|
|
|
// 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.
|
2015-10-13 13:04:13 +08:00
|
|
|
if (proc_kinfo[i].p_nlwps > 1) {
|
|
|
|
bool already_registered = false;
|
2020-03-13 05:10:25 +08:00
|
|
|
for (size_t pi = 0; pi < process_infos.size(); pi++) {
|
2020-09-10 18:45:24 +08:00
|
|
|
if ((::pid_t)process_infos[pi].GetProcessID() == proc_kinfo[i].p_pid) {
|
2015-10-13 13:04:13 +08:00
|
|
|
already_registered = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2015-10-13 13:04:13 +08:00
|
|
|
if (already_registered)
|
|
|
|
continue;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2015-10-13 13:04:13 +08:00
|
|
|
ProcessInstanceInfo process_info;
|
|
|
|
process_info.SetProcessID(proc_kinfo[i].p_pid);
|
|
|
|
process_info.SetParentProcessID(proc_kinfo[i].p_ppid);
|
|
|
|
process_info.SetUserID(proc_kinfo[i].p_ruid);
|
|
|
|
process_info.SetGroupID(proc_kinfo[i].p_rgid);
|
|
|
|
process_info.SetEffectiveUserID(proc_kinfo[i].p_uid);
|
|
|
|
process_info.SetEffectiveGroupID(proc_kinfo[i].p_gid);
|
|
|
|
// Make sure our info matches before we go fetch the name and cpu type
|
2020-10-30 19:00:45 +08:00
|
|
|
if (match_info_noname.Matches(process_info) &&
|
2015-10-13 13:04:13 +08:00
|
|
|
GetNetBSDProcessArgs(&match_info, process_info)) {
|
|
|
|
GetNetBSDProcessCPUType(process_info);
|
|
|
|
if (match_info.Matches(process_info))
|
2020-03-13 05:10:25 +08:00
|
|
|
process_infos.push_back(process_info);
|
2015-10-13 13:04:13 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2015-10-13 13:04:13 +08:00
|
|
|
|
|
|
|
kvm_close(kdp); /* XXX: we don't check for error here */
|
|
|
|
|
2020-03-13 05:10:25 +08:00
|
|
|
return process_infos.size();
|
2015-10-13 13:04:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Host::GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &process_info) {
|
|
|
|
process_info.SetProcessID(pid);
|
|
|
|
|
|
|
|
if (GetNetBSDProcessArgs(NULL, process_info)) {
|
|
|
|
GetNetBSDProcessCPUType(process_info);
|
|
|
|
GetNetBSDProcessUserAndGroup(process_info);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
process_info.Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status Host::ShellExpandArguments(ProcessLaunchInfo &launch_info) {
|
|
|
|
return Status("unimplemented");
|
2015-10-13 13:04:13 +08:00
|
|
|
}
|