287 changes: 287 additions & 0 deletions lldb/source/Host/netbsd/Host.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
//===-- source/Host/netbsd/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>
#include <dlfcn.h>
#include <execinfo.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/sysctl.h>
#include <sys/proc.h>

#include <limits.h>

#include <sys/ptrace.h>
#include <sys/exec.h>
#include <elf.h>
#include <kvm.h>

// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Core/Error.h"
#include "lldb/Host/Endian.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Core/Log.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Platform.h"

#include "lldb/Core/DataBufferHeap.h"
#include "lldb/Core/DataExtractor.h"
#include "lldb/Utility/CleanUp.h"
#include "lldb/Utility/NameMatches.h"

#include "llvm/Support/Host.h"

extern "C" {
extern char **environ;
}

using namespace lldb;
using namespace lldb_private;

size_t
Host::GetEnvironment (StringList &env)
{
char *v;
char **var = environ;
for (; var != NULL && *var != NULL; ++var)
{
v = ::strchr(*var, (int)'-');
if (v == NULL)
continue;
env.AppendString(v);
}
return env.GetSize();
}

static bool
GetNetBSDProcessArgs (const ProcessInstanceInfoMatch *match_info_ptr,
ProcessInstanceInfo &process_info)
{
if (!process_info.ProcessIDIsValid())
return false;

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, lldb::endian::InlHostByteOrder(), sizeof(void *));
lldb::offset_t offset = 0;
const char *cstr;

cstr = data.GetCStr (&offset);
if (!cstr)
return false;

process_info.GetExecutableFile().SetFile(cstr, false);

if (!(match_info_ptr == NULL ||
NameMatches (process_info.GetExecutableFile().GetFilename().GetCString(),
match_info_ptr->GetNameMatchType(),
match_info_ptr->GetProcessInfo().GetName())))
return false;

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)
break;

cstr = data.GetCStr(&offset);
if (!cstr)
break;

proc_args.AppendArgument(cstr);
}

return true;
}

static bool
GetNetBSDProcessCPUType (ProcessInstanceInfo &process_info)
{
if (process_info.ProcessIDIsValid())
{
process_info.GetArchitecture() = HostInfo::GetArchitecture(HostInfo::eArchKindDefault);
return true;
}
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;
}

uint32_t
Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
{
const ::pid_t our_pid = ::getpid();
const ::uid_t our_uid = ::getuid();

const bool all_users = match_info.GetMatchAllUsers() ||
// Special case, if lldb is being run as root we can attach to anything
(our_uid == 0);

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;

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;
}

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;

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;


// Every thread is a process in NetBSD, 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.
if (proc_kinfo[i].p_nlwps > 1) {
bool already_registered = false;
for (size_t pi = 0; pi < process_infos.GetSize(); pi++) {
if (process_infos.GetProcessIDAtIndex(pi) ==
proc_kinfo[i].p_pid) {
already_registered = true;
break;
}
}

if (already_registered)
continue;
}
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
if (match_info.Matches (process_info) &&
GetNetBSDProcessArgs (&match_info, process_info))
{
GetNetBSDProcessCPUType (process_info);
if (match_info.Matches (process_info))
process_infos.Append (process_info);
}
}

kvm_close(kdp); /* XXX: we don't check for error here */

return process_infos.GetSize();
}

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;
}

lldb::DataBufferSP
Host::GetAuxvData(lldb_private::Process *process)
{
return lldb::DataBufferSP();
}

Error
Host::ShellExpandArguments (ProcessLaunchInfo &launch_info)
{
return Error("unimplemented");
}
112 changes: 112 additions & 0 deletions lldb/source/Host/netbsd/HostInfoNetBSD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//===-- HostInfoNetBSD.cpp -------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/netbsd/HostInfoNetBSD.h"

#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <pthread.h>
#include <inttypes.h>


using namespace lldb_private;

uint32_t
HostInfoNetBSD::GetMaxThreadNameLength()
{
return PTHREAD_MAX_NAMELEN_NP;
}

bool
HostInfoNetBSD::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
{
struct utsname un;

::memset(&un, 0, sizeof(un));
if (::uname(&un) < 0)
return false;

/* Accept versions like 7.99.21 and 6.1_STABLE */
int status = ::sscanf(un.release, "%" PRIu32 ".%" PRIu32 ".%" PRIu32, &major, &minor, &update);
switch(status) {
case 0:
return false;
case 1:
minor = 0;
/* FALLTHROUGH */
case 2:
update = 0;
/* FALLTHROUGH */
case 3:
default:
return true;
}
}

bool
HostInfoNetBSD::GetOSBuildString(std::string &s)
{
int mib[2] = {CTL_KERN, KERN_OSREV};
char osrev_str[12];
int osrev = 0;
size_t osrev_len = sizeof(osrev);

if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0)
{
::snprintf(osrev_str, sizeof(osrev_str), "%-10.10d", osrev);
s.assign(osrev_str);
return true;
}

s.clear();
return false;
}

bool
HostInfoNetBSD::GetOSKernelDescription(std::string &s)
{
struct utsname un;

::memset(&un, 0, sizeof(un));
s.clear();

if (::uname(&un) < 0)
return false;

s.assign(un.version);

return true;
}

FileSpec
HostInfoNetBSD::GetProgramFileSpec()
{
static FileSpec g_program_filespec;

if (!g_program_filespec)
{
ssize_t len;
static char buf[PATH_MAX];
char name[PATH_MAX];

::snprintf(name, PATH_MAX, "/proc/%d/exe", ::getpid());
len = ::readlink(name, buf, PATH_MAX - 1);
if (len != -1)
{
buf[len] = '\0';
g_program_filespec.SetFile(buf, false);
}
}
return g_program_filespec;
}
50 changes: 50 additions & 0 deletions lldb/source/Host/netbsd/HostThreadNetBSD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//===-- HostThreadNetBSD.cpp -----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// lldb Includes
#include "lldb/Host/netbsd/HostThreadNetBSD.h"
#include "lldb/Host/Host.h"

// C includes
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sysctl.h>
#include <sys/user.h>

// C++ includes
#include <string>

using namespace lldb_private;

HostThreadNetBSD::HostThreadNetBSD()
{
}

HostThreadNetBSD::HostThreadNetBSD(lldb::thread_t thread)
: HostThreadPosix(thread)
{
}

void
HostThreadNetBSD::SetName(lldb::thread_t thread, llvm::StringRef &name)
{
::pthread_setname_np(thread, "%s", const_cast<char*>(name.data()));
}

void
HostThreadNetBSD::GetName(lldb::thread_t thread, llvm::SmallVectorImpl<char> &name)
{
char buf[PTHREAD_MAX_NAMELEN_NP];
::pthread_getname_np(thread, buf, PTHREAD_MAX_NAMELEN_NP);

name.clear();
name.append(buf, buf + strlen(buf));
}
30 changes: 30 additions & 0 deletions lldb/source/Host/netbsd/ThisThread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===-- ThisThread.cpp ------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/HostNativeThread.h"
#include "lldb/Host/ThisThread.h"

#include "llvm/ADT/SmallVector.h"

#include <pthread.h>
#include <string.h>

using namespace lldb_private;

void
ThisThread::SetName(llvm::StringRef name)
{
HostNativeThread::SetName(::pthread_self(), name);
}

void
ThisThread::GetName(llvm::SmallVectorImpl<char> &name)
{
HostNativeThread::GetName(::pthread_self(), name);
}
696 changes: 696 additions & 0 deletions lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp

Large diffs are not rendered by default.

182 changes: 182 additions & 0 deletions lldb/source/Plugins/Platform/NetBSD/PlatformNetBSD.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
//===-- PlatformNetBSD.h ---------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_PlatformNetBSD_h_
#define liblldb_PlatformNetBSD_h_

// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Target/Platform.h"

namespace lldb_private {
namespace platform_netbsd {

class PlatformNetBSD : public Platform
{
public:

//------------------------------------------------------------
// Class functions
//------------------------------------------------------------
static lldb::PlatformSP
CreateInstance(bool force, const ArchSpec *arch);

static void
Initialize ();

static void
Terminate ();

static ConstString
GetPluginNameStatic (bool is_host);

static const char *
GetDescriptionStatic (bool is_host);

//------------------------------------------------------------
// Class Methods
//------------------------------------------------------------
PlatformNetBSD (bool is_host);

virtual
~PlatformNetBSD() override = default;

//------------------------------------------------------------
// lldb_private::PluginInterface functions
//------------------------------------------------------------
ConstString
GetPluginName() override
{
return GetPluginNameStatic (IsHost());
}

uint32_t
GetPluginVersion() override
{
return 1;
}

const char *
GetDescription () override
{
return GetDescriptionStatic(IsHost());
}

//------------------------------------------------------------
// lldb_private::Platform functions
//------------------------------------------------------------
bool
GetModuleSpec(const FileSpec& module_file_spec,
const ArchSpec& arch,
ModuleSpec &module_spec) override;

Error
RunShellCommand(const char *command,
const FileSpec &working_dir,
int *status_ptr,
int *signo_ptr,
std::string *command_output,
uint32_t timeout_sec) override;

Error
ResolveExecutable(const ModuleSpec &module_spec,
lldb::ModuleSP &module_sp,
const FileSpecList *module_search_paths_ptr) override;

size_t
GetSoftwareBreakpointTrapOpcode(Target &target,
BreakpointSite *bp_site) override;

bool
GetRemoteOSVersion () override;

bool
GetRemoteOSBuildString (std::string &s) override;

bool
GetRemoteOSKernelDescription (std::string &s) override;

// Remote Platform subclasses need to override this function
ArchSpec
GetRemoteSystemArchitecture() override;

bool
IsConnected () const override;

Error
ConnectRemote(Args& args) override;

Error
DisconnectRemote() override;

const char *
GetHostname () override;

const char *
GetUserName (uint32_t uid) override;

const char *
GetGroupName (uint32_t gid) override;

bool
GetProcessInfo(lldb::pid_t pid,
ProcessInstanceInfo &proc_info) override;

uint32_t
FindProcesses(const ProcessInstanceInfoMatch &match_info,
ProcessInstanceInfoList &process_infos) override;

Error
LaunchProcess(ProcessLaunchInfo &launch_info) override;

lldb::ProcessSP
Attach(ProcessAttachInfo &attach_info,
Debugger &debugger,
Target *target,
Error &error) override;

// NetBSD processes can not be launched by spawning and attaching.
bool
CanDebugProcess () override { return false; }

// Only on PlatformMacOSX:
Error
GetFileWithUUID(const FileSpec &platform_file,
const UUID* uuid, FileSpec &local_file) override;

Error
GetSharedModule(const ModuleSpec &module_spec,
Process* process,
lldb::ModuleSP &module_sp,
const FileSpecList *module_search_paths_ptr,
lldb::ModuleSP *old_module_sp_ptr,
bool *did_create_ptr) override;

bool
GetSupportedArchitectureAtIndex(uint32_t idx, ArchSpec &arch) override;

void
GetStatus(Stream &strm) override;

void
CalculateTrapHandlerSymbolNames () override;

protected:
lldb::PlatformSP m_remote_platform_sp; // Allow multiple ways to connect to a remote netbsd OS

private:
DISALLOW_COPY_AND_ASSIGN (PlatformNetBSD);
};

} // namespace platform_netbsd
} // namespace lldb_private

#endif // liblldb_PlatformNetBSD_h_