948 changes: 944 additions & 4 deletions lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp

Large diffs are not rendered by default.

90 changes: 90 additions & 0 deletions lldb/source/Plugins/Process/NetBSD/NativeProcessNetBSD.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,98 @@ class NativeProcessNetBSD : public NativeProcessProtocol {
lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
MainLoop &mainloop, NativeProcessProtocolSP &process_sp);

public:
// ---------------------------------------------------------------------
// NativeProcessProtocol Interface
// ---------------------------------------------------------------------
Error Resume(const ResumeActionList &resume_actions) override;

Error Halt() override;

Error Detach() override;

Error Signal(int signo) override;

Error Kill() override;

Error GetMemoryRegionInfo(lldb::addr_t load_addr,
MemoryRegionInfo &range_info) override;

Error ReadMemory(lldb::addr_t addr, void *buf, size_t size,
size_t &bytes_read) override;

Error ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
size_t &bytes_read) override;

Error WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
size_t &bytes_written) override;

Error AllocateMemory(size_t size, uint32_t permissions,
lldb::addr_t &addr) override;

Error DeallocateMemory(lldb::addr_t addr) override;

lldb::addr_t GetSharedLibraryInfoAddress() override;

size_t UpdateThreads() override;

bool GetArchitecture(ArchSpec &arch) const override;

Error SetBreakpoint(lldb::addr_t addr, uint32_t size, bool hardware) override;

Error GetLoadedModuleFileSpec(const char *module_path,
FileSpec &file_spec) override;

Error GetFileLoadAddress(const llvm::StringRef &file_name,
lldb::addr_t &load_addr) override;

llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
GetAuxvData() const override;

// ---------------------------------------------------------------------
// Interface used by NativeRegisterContext-derived classes.
// ---------------------------------------------------------------------
static Error PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
int data = 0, int *result = nullptr);

protected:
// ---------------------------------------------------------------------
// NativeProcessProtocol protected interface
// ---------------------------------------------------------------------

Error
GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint,
size_t &actual_opcode_size,
const uint8_t *&trap_opcode_bytes) override;

private:
MainLoop::SignalHandleUP m_sigchld_handle;
ArchSpec m_arch;
LazyBool m_supports_mem_region;
std::vector<std::pair<MemoryRegionInfo, FileSpec>> m_mem_region_cache;

// ---------------------------------------------------------------------
// Private Instance Methods
// ---------------------------------------------------------------------
NativeProcessNetBSD();

NativeThreadNetBSDSP AddThread(lldb::tid_t thread_id);

Error LaunchInferior(MainLoop &mainloop, ProcessLaunchInfo &launch_info);
void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Error &error);

void MonitorCallback(lldb::pid_t pid, int signal);
void MonitorExited(lldb::pid_t pid, int signal, int status);
void MonitorSIGSTOP(lldb::pid_t pid);
void MonitorSIGTRAP(lldb::pid_t pid);
void MonitorSignal(lldb::pid_t pid, int signal);

Error GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
Error FixupBreakpointPCAsNeeded(NativeThreadNetBSD &thread);
Error PopulateMemoryRegionCache();
void SigchldHandler();

::pid_t Attach(lldb::pid_t pid, Error &error);
};

} // namespace process_netbsd
Expand Down
47 changes: 47 additions & 0 deletions lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,58 @@

#include "NativeRegisterContextNetBSD.h"

#include "lldb/Host/common/NativeProcessProtocol.h"

using namespace lldb_private;
using namespace lldb_private::process_netbsd;

// clang-format off
#include <sys/types.h>
#include <sys/ptrace.h>
// clang-format on

NativeRegisterContextNetBSD::NativeRegisterContextNetBSD(
NativeThreadProtocol &native_thread, uint32_t concrete_frame_idx,
RegisterInfoInterface *reg_info_interface_p)
: NativeRegisterContextRegisterInfo(native_thread, concrete_frame_idx,
reg_info_interface_p) {}

Error NativeRegisterContextNetBSD::ReadGPR() {
void *buf = GetGPRBuffer();
if (!buf)
return Error("GPR buffer is NULL");

return DoReadGPR(buf);
}

Error NativeRegisterContextNetBSD::WriteGPR() {
void *buf = GetGPRBuffer();
if (!buf)
return Error("GPR buffer is NULL");

return DoWriteGPR(buf);
}

Error NativeRegisterContextNetBSD::DoReadGPR(void *buf) {
return NativeProcessNetBSD::PtraceWrapper(PT_GETREGS, GetProcessPid(), buf,
m_thread.GetID());
}

Error NativeRegisterContextNetBSD::DoWriteGPR(void *buf) {
return NativeProcessNetBSD::PtraceWrapper(PT_SETREGS, GetProcessPid(), buf,
m_thread.GetID());
}

NativeProcessNetBSD &NativeRegisterContextNetBSD::GetProcess() {
auto process_sp =
std::static_pointer_cast<NativeProcessNetBSD>(m_thread.GetProcess());
assert(process_sp);
return *process_sp;
}

::pid_t NativeRegisterContextNetBSD::GetProcessPid() {
NativeProcessNetBSD &process = GetProcess();
lldb::pid_t pid = process.GetID();

return pid;
}
13 changes: 13 additions & 0 deletions lldb/source/Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ class NativeRegisterContextNetBSD : public NativeRegisterContextRegisterInfo {
CreateHostNativeRegisterContextNetBSD(const ArchSpec &target_arch,
NativeThreadProtocol &native_thread,
uint32_t concrete_frame_idx);

protected:
virtual Error ReadGPR();
virtual Error WriteGPR();
virtual void *GetGPRBuffer() { return nullptr; }
virtual size_t GetGPRSize() {
return GetRegisterInfoInterface().GetGPRSize();
}
virtual Error DoReadGPR(void *buf);
virtual Error DoWriteGPR(void *buf);

virtual NativeProcessNetBSD &GetProcess();
virtual ::pid_t GetProcessPid();
};

} // namespace process_netbsd
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===-- NativeRegisterContextNetBSD_x86_64.h --------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#if defined(__x86_64__)

#ifndef lldb_NativeRegisterContextNetBSD_x86_64_h
#define lldb_NativeRegisterContextNetBSD_x86_64_h

// clang-format off
#include <sys/param.h>
#include <sys/types.h>
#include <machine/reg.h>
// clang-format on

#include "Plugins/Process/NetBSD/NativeRegisterContextNetBSD.h"
#include "Plugins/Process/Utility/RegisterContext_x86.h"
#include "Plugins/Process/Utility/lldb-x86-register-enums.h"

namespace lldb_private {
namespace process_netbsd {

class NativeProcessNetBSD;

class NativeRegisterContextNetBSD_x86_64 : public NativeRegisterContextNetBSD {
public:
NativeRegisterContextNetBSD_x86_64(const ArchSpec &target_arch,
NativeThreadProtocol &native_thread,
uint32_t concrete_frame_idx);
uint32_t GetRegisterSetCount() const override;

const RegisterSet *GetRegisterSet(uint32_t set_index) const override;

Error ReadRegister(const RegisterInfo *reg_info,
RegisterValue &reg_value) override;

Error WriteRegister(const RegisterInfo *reg_info,
const RegisterValue &reg_value) override;

Error ReadAllRegisterValues(lldb::DataBufferSP &data_sp) override;

Error WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override;

protected:
void *GetGPRBuffer() override { return &m_gpr_x86_64; }

private:
// Private member types.
enum { GPRegSet };

// Private member variables.
struct reg m_gpr_x86_64;

int GetSetForNativeRegNum(int reg_num) const;

int ReadRegisterSet(uint32_t set);
int WriteRegisterSet(uint32_t set);
};

} // namespace process_netbsd
} // namespace lldb_private

#endif // #ifndef lldb_NativeRegisterContextNetBSD_x86_64_h

#endif // defined(__x86_64__)
123 changes: 122 additions & 1 deletion lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,131 @@

#include "NativeProcessNetBSD.h"

#include "Plugins/Process/POSIX/CrashReason.h"
#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
#include "lldb/Core/RegisterValue.h"
#include "lldb/Core/State.h"

using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::process_netbsd;

NativeThreadNetBSD::NativeThreadNetBSD(NativeProcessNetBSD *process,
lldb::tid_t tid)
: NativeThreadProtocol(process, tid) {}
: NativeThreadProtocol(process, tid), m_state(StateType::eStateInvalid),
m_stop_info(), m_reg_context_sp(), m_stop_description() {}

void NativeThreadNetBSD::SetStoppedBySignal(uint32_t signo,
const siginfo_t *info) {
Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));
LLDB_LOG(log, "tid = {0} in called with signal {1}", GetID(), signo);

SetStopped();

m_stop_info.reason = StopReason::eStopReasonSignal;
m_stop_info.details.signal.signo = signo;

m_stop_description.clear();
if (info) {
switch (signo) {
case SIGSEGV:
case SIGBUS:
case SIGFPE:
case SIGILL:
const auto reason = GetCrashReason(*info);
m_stop_description = GetCrashReasonString(reason, *info);
break;
}
}
}

void NativeThreadNetBSD::SetStoppedByBreakpoint() {
SetStopped();
m_stop_info.reason = StopReason::eStopReasonBreakpoint;
m_stop_info.details.signal.signo = SIGTRAP;
}

void NativeThreadNetBSD::SetStopped() {
const StateType new_state = StateType::eStateStopped;
m_state = new_state;
m_stop_description.clear();
}

void NativeThreadNetBSD::SetRunning() {
m_state = StateType::eStateRunning;
m_stop_info.reason = StopReason::eStopReasonNone;
}

std::string NativeThreadNetBSD::GetName() { return std::string(""); }

lldb::StateType NativeThreadNetBSD::GetState() { return m_state; }

bool NativeThreadNetBSD::GetStopReason(ThreadStopInfo &stop_info,
std::string &description) {
Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD));

description.clear();

switch (m_state) {
case eStateStopped:
case eStateCrashed:
case eStateExited:
case eStateSuspended:
case eStateUnloaded:
stop_info = m_stop_info;
description = m_stop_description;

return true;

case eStateInvalid:
case eStateConnected:
case eStateAttaching:
case eStateLaunching:
case eStateRunning:
case eStateStepping:
case eStateDetached:
LLDB_LOG(log, "tid = {0} in state {1} cannot answer stop reason", GetID(),
StateAsCString(m_state));
return false;
}
llvm_unreachable("unhandled StateType!");
}

NativeRegisterContextSP NativeThreadNetBSD::GetRegisterContext() {
// Return the register context if we already created it.
if (m_reg_context_sp)
return m_reg_context_sp;

NativeProcessProtocolSP m_process_sp = m_process_wp.lock();
if (!m_process_sp)
return NativeRegisterContextSP();

ArchSpec target_arch;
if (!m_process_sp->GetArchitecture(target_arch))
return NativeRegisterContextSP();

const uint32_t concrete_frame_idx = 0;
m_reg_context_sp.reset(
NativeRegisterContextNetBSD::CreateHostNativeRegisterContextNetBSD(
target_arch, *this, concrete_frame_idx));

return m_reg_context_sp;
}

Error NativeThreadNetBSD::SetWatchpoint(lldb::addr_t addr, size_t size,
uint32_t watch_flags, bool hardware) {
return Error("Unimplemented");
}

Error NativeThreadNetBSD::RemoveWatchpoint(lldb::addr_t addr) {
return Error("Unimplemented");
}

Error NativeThreadNetBSD::SetHardwareBreakpoint(lldb::addr_t addr,
size_t size) {
return Error("Unimplemented");
}

Error NativeThreadNetBSD::RemoveHardwareBreakpoint(lldb::addr_t addr) {
return Error("Unimplemented");
}
39 changes: 39 additions & 0 deletions lldb/source/Plugins/Process/NetBSD/NativeThreadNetBSD.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,45 @@ class NativeThreadNetBSD : public NativeThreadProtocol {

public:
NativeThreadNetBSD(NativeProcessNetBSD *process, lldb::tid_t tid);

// ---------------------------------------------------------------------
// NativeThreadProtocol Interface
// ---------------------------------------------------------------------
std::string GetName() override;

lldb::StateType GetState() override;

bool GetStopReason(ThreadStopInfo &stop_info,
std::string &description) override;

NativeRegisterContextSP GetRegisterContext() override;

Error SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags,
bool hardware) override;

Error RemoveWatchpoint(lldb::addr_t addr) override;

Error SetHardwareBreakpoint(lldb::addr_t addr, size_t size) override;

Error RemoveHardwareBreakpoint(lldb::addr_t addr) override;

private:
// ---------------------------------------------------------------------
// Interface for friend classes
// ---------------------------------------------------------------------

void SetStoppedBySignal(uint32_t signo, const siginfo_t *info = nullptr);
void SetStoppedByBreakpoint();
void SetStopped();
void SetRunning();

// ---------------------------------------------------------------------
// Member Variables
// ---------------------------------------------------------------------
lldb::StateType m_state;
ThreadStopInfo m_stop_info;
NativeRegisterContextSP m_reg_context_sp;
std::string m_stop_description;
};

typedef std::shared_ptr<NativeThreadNetBSD> NativeThreadNetBSDSP;
Expand Down