regs and we can step into!

This commit is contained in:
T0b1
2023-06-02 02:06:49 +02:00
parent d4bf6731a3
commit ac3718b12b
17 changed files with 1434 additions and 344 deletions

View File

@@ -1,24 +1,555 @@
#include "lldb_backend.h"
#include <lldb/API/SBListener.h>
#include <lldb/API/SBEvent.h>
#include <lldb/API/SBStream.h>
#include <lldb/API/SBThread.h>
#include <lldb/API/SBFrame.h>
#include <lldb/API/SBValueList.h>
#include <lldb/API/SBValue.h>
#include <filesystem>
#include <array>
#include <cassert>
#include <cstring>
using namespace dbgui::backend;
LLDBBackend::~LLDBBackend() {
namespace
{}
LLDBBackend::~LLDBBackend() {}
LLDBBackend::LLDBBackend(std::string filename)
{
_filename = filename;
lldb::SBDebugger::Initialize();
_instance = lldb::SBDebugger::Create(false);
_target = _instance.CreateTarget(filename.c_str());
}
LLDBBackend::LLDBBackend(std::string filename) {
_filename = filename;
_instance = lldb::SBDebugger::Create(false);
_target = _instance.CreateTarget(filename.c_str());
void LLDBBackend::start()
{
const char *argv[2] = {_filename.c_str(), nullptr};
const auto cwd = std::filesystem::current_path();
auto error = lldb::SBError();
auto listener = lldb::SBListener();
_process = _target.Launch(listener, argv, nullptr, nullptr, nullptr, nullptr,
cwd.c_str(), lldb::LaunchFlags::eLaunchFlagNone,
true, error);
_msg_thread = std::thread{[this]() {
auto ptr = this->shared_from_this();
static_cast<LLDBBackend *>(ptr.get())->run_msg_loop();
}};
}
void LLDBBackend::start() {
const char* argv[2] = {_filename.c_str(), nullptr};
const auto cwd = std::filesystem::current_path();
void LLDBBackend::run_msg_loop()
{
std::thread event_thread{[this]() { this->wait_for_debug_events(); }};
auto error = lldb::SBError();
auto listener = lldb::SBListener();
_process = _target.Launch(listener, argv, nullptr, nullptr, nullptr, nullptr, cwd.c_str(), lldb::LaunchFlags::eLaunchFlagNone, true, error);
while (true)
{
std::this_thread::sleep_for(std::chrono::milliseconds{1000});
}
}
void LLDBBackend::wait_for_debug_events()
{
using namespace lldb;
auto listener = _instance.GetListener();
while (true)
{
auto event = lldb::SBEvent();
if (listener.WaitForEvent(0xFFFFFFFF, event))
{
std::lock_guard g{_data_lock};
printf("Got Event:\n");
auto stream = lldb::SBStream{};
if (event.GetDescription(stream))
{
printf(" Desc: %.*s\n", static_cast<int>(stream.GetSize()),
stream.GetData());
} else
{
printf(" Failed to get stream\n");
}
if (SBProcess::EventIsProcessEvent(event))
{
// Check if state changed
auto state = SBProcess::GetStateFromEvent(event);
// TODO: get state from process if we can't get it from the event?
if (state != StateType::eStateInvalid)
{
this->handle_state_change(state);
}
} else
{
printf(" Unknown event source!\n");
}
}
}
}
void LLDBBackend::handle_state_change(lldb::StateType state)
{
using namespace lldb;
if (_first_run)
{
_first_run = false;
// TODO: do initialization
// TODO: we should only do this when the process was stopped, no?
auto proc_info = BackToFront::InitialProcessInfo{};
auto reg_infos = std::vector<BackToFront::RegsChanged>{};
this->prepare_proc_info(proc_info, reg_infos);
this->dump_threads();
this->send_proc_info(std::move(proc_info));
for (size_t i = 0; i < reg_infos.size(); ++i)
{
this->send_reg_change(std::move(reg_infos[i]));
}
reg_infos.clear();
if (state == StateType::eStateStopped)
{
this->send_state_change(TargetState::paused,
StateChangeReason::initial_entry);
return;
}
}
switch (state)
{
case eStateStopped:
this->check_reg_changes();
this->send_state_change(TargetState::paused, StateChangeReason::unknown);
break;
case eStateRunning:
case eStateStepping:
this->send_state_change(TargetState::running, StateChangeReason::unknown);
return;
default: printf("Unknown StateType %u encountered\n", state); exit(1);
}
}
std::array<const char *, 24> x86_64_gpr = {
"rax", "rbx", "rcx", "rdx", "rdi", "rsi", "rbp", "rsp",
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
"rip", "rflags", "cs", "fs", "gs", "ss", "ds", "es"};
std::array<const char *, 14> x86_64_fp_special = {
"fctrl", "fstat", "ftag", "fop", "fiseg", "fioff", "fip",
"foseg", "fooff", "foseg", "fooff", "fdp", "mxcsr", "mxcsrmask"};
std::array<const char *, 8> x86_64_fp = {"st0", "st1", "st2", "st3",
"st4", "st5", "st6", "st7"};
std::array<const char *, 16> x86_64_xmm = {
"xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
"xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15"};
std::array<const char *, 16> x86_64_ymm = {
"ymm0", "ymm1", "ymm2", "ymm3", "ymm4", "ymm5", "ymm6", "ymm7",
"ymm8", "ymm9", "ymm10", "ymm11", "ymm12", "ymm13", "ymm14", "ymm15"};
std::array<const char *, 16> x86_64_zmm = {
"zmm0", "zmm1", "zmm2", "zmm3", "zmm4", "zmm5", "zmm6", "zmm7",
"zmm8", "zmm9", "zmm10", "zmm11", "zmm12", "zmm13", "zmm14", "zmm15"};
void LLDBBackend::prepare_proc_info(
BackToFront::InitialProcessInfo &info,
std::vector<BackToFront::RegsChanged> &reg_infos)
{
using namespace lldb;
info.pid = _process->GetProcessID();
const auto target_triple =
std::string_view{_process->GetProcessInfo().GetTriple()};
printf("Target Triple: %s\n", target_triple.data());
if (target_triple.starts_with("x86_64-"))
{
info.arch = Arch::x86_64;
} /* else if (target_triple.starts_with("x86-")) {
// TODO: is this the right triple?
info.arch = Arch::x86;
}*/
else
{
printf("Arch not supported!\n");
exit(1);
}
auto frame = _process->GetThreadAtIndex(0).GetFrameAtIndex(0);
const auto regs = frame.GetRegisters();
/*const auto len = regs.GetSize();
for (size_t i = 0; i < len; ++i) {
auto reg_or_set = regs.GetValueAtIndex(i);
if (reg_or_set.GetValueType() == eValueTypeRegister) {
printf("Got register %s (%s) with value: %lX\n", reg_or_set.GetName(), reg_or_set.GetTypeName(), reg_or_set.GetValueAsUnsigned(0));
info.reg_names.emplace_back(reg_or_set.GetName());
continue;
}
printf("Got register set %s\n", reg_or_set.GetName());
const auto is_gp_set = std::string_view{reg_or_set.GetName()} == "General Purpose Registers";
for (uint32_t child_idx = 0; child_idx < reg_or_set.GetNumChildren(); ++child_idx) {
auto reg = reg_or_set.GetChildAtIndex(child_idx);
printf("Got register %s (%s) with value: %lX\n", reg.GetName(), reg.GetTypeName(), reg.GetValueAsUnsigned(0));
if (is_gp_set && info.arch == Arch::x86_64 && reg.GetByteSize() < 8) {
// skip non-full regs here
printf(" -> Skipped\n");
continue;
}
info.reg_names.emplace_back(reg.GetName());
}
}*/
if (info.arch == Arch::x86_64)
{
{
auto reg_set = RegSet{};
auto info_set = BackToFront::InitialProcessInfo::RegSet{};
auto reg_info = BackToFront::RegsChanged{};
reg_info.set_idx = 0;
info_set.name = "GPR";
reg_set.set_name = "General Purpose Registers";
auto lldb_set = regs.GetFirstValueByName("General Purpose Registers");
for (size_t i = 0; i < x86_64_gpr.size(); ++i)
{
auto val = lldb_set.GetChildMemberWithName(x86_64_gpr[i]);
if (!val.IsValid())
{
printf("GPR %s not found!\n", x86_64_gpr[i]);
exit(1);
}
info_set.reg_names.push_back(x86_64_gpr[i]);
reg_set.names.push_back(x86_64_gpr[i]);
reg_set.values.push_back({});
auto data = val.GetData();
auto len = data.GetByteSize();
reg_set.values.back().resize(data.GetByteSize());
auto error = SBError{};
data.ReadRawData(error, 0, reg_set.values.back().data(), len);
reg_info.changes.push_back(
std::make_pair((uint16_t)i, reg_set.values.back()));
}
_reg_sets.push_back(reg_set);
info.reg_sets.push_back(info_set);
reg_infos.push_back(reg_info);
}
}
}
void LLDBBackend::dump_threads()
{
using namespace lldb;
const auto thread_count = _process->GetNumThreads();
for (size_t i = 0; i < thread_count; ++i)
{
auto thread = _process->GetThreadAtIndex(i);
auto stop_reason = thread.GetStopReason();
switch (stop_reason)
{
case eStopReasonSignal:
{
auto signal_num = thread.GetStopReasonDataAtIndex(0);
printf("Thread %lu: TID: %lu, Stop Reason: Signal: %lu\n", i,
thread.GetThreadID(), signal_num);
break;
}
default:
printf("Thread %lu: TID: %lu, Stop Reason: %lu\n", i,
thread.GetThreadID(), stop_reason);
}
}
auto sel = _process->GetSelectedThread();
if (sel.IsValid())
{
printf("Selected Thread: %lu\n", sel.GetThreadID());
} else
{
printf("Selected thread not valid\n");
}
}
bool LLDBBackend::step_into()
{
std::lock_guard g{_data_lock};
if (!_process)
{
return false;
}
auto thread = _process->GetSelectedThread();
if (!thread.IsValid())
{
return false;
}
// TODO: figure out what the runmodes mean
thread.StepInto();
return false;
}
bool LLDBBackend::step_over()
{
std::lock_guard g{_data_lock};
if (!_process)
{
return false;
}
auto thread = _process->GetSelectedThread();
if (!thread.IsValid())
{
return false;
}
thread.StepOver();
return false;
}
bool LLDBBackend::step_out()
{
std::lock_guard g{_data_lock};
if (!_process)
{
return false;
}
auto thread = _process->GetSelectedThread();
if (!thread.IsValid())
{
return false;
}
thread.StepOut();
return false;
}
void LLDBBackend::check_reg_changes()
{
auto thread = _process->GetSelectedThread();
if (!thread.IsValid())
{
// TODO: try to find another thread to select
return;
}
// TODO: figure out if 0 is always the lowest one?
auto frame = thread.GetFrameAtIndex(0);
const auto regs = frame.GetRegisters();
uint8_t tmp_buf[64];
for (size_t set_idx = 0; set_idx < _reg_sets.size(); ++set_idx)
{
auto &set = _reg_sets[set_idx];
auto lldb_set = regs.GetFirstValueByName(set.set_name.c_str());
if (!lldb_set.IsValid())
{
printf("Failed to get set %s for frame\n", set.set_name.c_str());
continue;
}
auto change_info = BackToFront::RegsChanged{.set_idx = set_idx};
assert(set.names.size() == set.values.size());
for (size_t i = 0; i < set.names.size(); ++i)
{
auto val = lldb_set.GetChildMemberWithName(set.names[i]);
if (!val.IsValid())
{
printf("Failed to get %s for frame\n", set.names[i]);
continue;
}
auto data = val.GetData();
const auto len = data.GetByteSize();
if (len != set.values[i].size())
{
set.values[i].resize(len);
// TODO: error handling
auto error = lldb::SBError{};
data.ReadRawData(error, 0, set.values[i].data(), len);
change_info.changes.emplace_back(std::make_pair(i, set.values[i]));
continue;
}
if (len > sizeof(tmp_buf))
{
printf("Register too large\n");
exit(1);
}
// TODO: error handling
auto error = lldb::SBError{};
data.ReadRawData(error, 0, tmp_buf, len);
if (std::memcmp(tmp_buf, set.values[i].data(), len))
{
std::copy(tmp_buf, tmp_buf + len, set.values[i].begin());
change_info.changes.emplace_back(std::make_pair(i, set.values[i]));
}
}
if (!change_info.changes.empty())
{
this->send_reg_change(std::move(change_info));
}
}
}
/*
Reg output for x64
Got register set General Purpose Registers
Got register rax (unsigned long) with value: 0
Got register rbx (unsigned long) with value: 0
Got register rcx (unsigned long) with value: 0
Got register rdx (unsigned long) with value: 0
Got register rdi (unsigned long) with value: 0
Got register rsi (unsigned long) with value: 0
Got register rbp (unsigned long) with value: 0
Got register rsp (unsigned long) with value: 7FFF30F924E0
Got register r8 (unsigned long) with value: 0
Got register r9 (unsigned long) with value: 0
Got register r10 (unsigned long) with value: 0
Got register r11 (unsigned long) with value: 0
Got register r12 (unsigned long) with value: 0
Got register r13 (unsigned long) with value: 0
Got register r14 (unsigned long) with value: 0
Got register r15 (unsigned long) with value: 0
Got register rip (unsigned long) with value: 7FD5ABB9B3B0
Got register rflags (unsigned long) with value: 200
Got register cs (unsigned long) with value: 33
Got register fs (unsigned long) with value: 0
Got register gs (unsigned long) with value: 0
Got register ss (unsigned long) with value: 2B
Got register ds (unsigned long) with value: 0
Got register es (unsigned long) with value: 0
Got register eax (unsigned int) with value: 0
Got register ebx (unsigned int) with value: 0
Got register ecx (unsigned int) with value: 0
Got register edx (unsigned int) with value: 0
Got register edi (unsigned int) with value: 0
Got register esi (unsigned int) with value: 0
Got register ebp (unsigned int) with value: 0
Got register esp (unsigned int) with value: 30F924E0
Got register r8d (unsigned int) with value: 0
Got register r9d (unsigned int) with value: 0
Got register r10d (unsigned int) with value: 0
Got register r11d (unsigned int) with value: 0
Got register r12d (unsigned int) with value: 0
Got register r13d (unsigned int) with value: 0
Got register r14d (unsigned int) with value: 0
Got register r15d (unsigned int) with value: 0
Got register ax (unsigned short) with value: 0
Got register bx (unsigned short) with value: 0
Got register cx (unsigned short) with value: 0
Got register dx (unsigned short) with value: 0
Got register di (unsigned short) with value: 0
Got register si (unsigned short) with value: 0
Got register bp (unsigned short) with value: 0
Got register sp (unsigned short) with value: 24E0
Got register r8w (unsigned short) with value: 0
Got register r9w (unsigned short) with value: 0
Got register r10w (unsigned short) with value: 0
Got register r11w (unsigned short) with value: 0
Got register r12w (unsigned short) with value: 0
Got register r13w (unsigned short) with value: 0
Got register r14w (unsigned short) with value: 0
Got register r15w (unsigned short) with value: 0
Got register ah (unsigned char) with value: 0
Got register bh (unsigned char) with value: 0
Got register ch (unsigned char) with value: 0
Got register dh (unsigned char) with value: 0
Got register al (unsigned char) with value: 0
Got register bl (unsigned char) with value: 0
Got register cl (unsigned char) with value: 0
Got register dl (unsigned char) with value: 0
Got register dil (unsigned char) with value: 0
Got register sil (unsigned char) with value: 0
Got register bpl (unsigned char) with value: 0
Got register spl (unsigned char) with value: E0
Got register r8l (unsigned char) with value: 0
Got register r9l (unsigned char) with value: 0
Got register r10l (unsigned char) with value: 0
Got register r11l (unsigned char) with value: 0
Got register r12l (unsigned char) with value: 0
Got register r13l (unsigned char) with value: 0
Got register r14l (unsigned char) with value: 0
Got register r15l (unsigned char) with value: 0
Got register set Floating Point Registers
Got register fctrl (unsigned short) with value: 37F
Got register fstat (unsigned short) with value: 0
Got register ftag (unsigned short) with value: FFFF
Got register fop (unsigned short) with value: 0
Got register fiseg (unsigned int) with value: 0
Got register fioff (unsigned int) with value: 0
Got register fip (unsigned long) with value: 0
Got register foseg (unsigned int) with value: 0
Got register fooff (unsigned int) with value: 0
Got register fdp (unsigned long) with value: 0
Got register mxcsr (unsigned int) with value: 1F80
Got register mxcsrmask (unsigned int) with value: 2FFFF
Got register st0 (unsigned char __attribute__((ext_vector_type(10)))) with value: 0
Got register st1 (unsigned char __attribute__((ext_vector_type(10)))) with value: 0
Got register st2 (unsigned char __attribute__((ext_vector_type(10)))) with value: 0
Got register st3 (unsigned char __attribute__((ext_vector_type(10)))) with value: 0
Got register st4 (unsigned char __attribute__((ext_vector_type(10)))) with value: 0
Got register st5 (unsigned char __attribute__((ext_vector_type(10)))) with value: 0
Got register st6 (unsigned char __attribute__((ext_vector_type(10)))) with value: 0
Got register st7 (unsigned char __attribute__((ext_vector_type(10)))) with value: 0
Got register mm0 (unsigned long) with value: 0
Got register mm1 (unsigned long) with value: 0
Got register mm2 (unsigned long) with value: 0
Got register mm3 (unsigned long) with value: 0
Got register mm4 (unsigned long) with value: 0
Got register mm5 (unsigned long) with value: 0
Got register mm6 (unsigned long) with value: 0
Got register mm7 (unsigned long) with value: 0
Got register xmm0 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm1 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm2 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm3 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm4 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm5 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm6 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm7 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm8 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm9 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm10 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm11 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm12 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm13 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm14 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register xmm15 (unsigned char __attribute__((ext_vector_type(16)))) with value: 0
Got register set Advanced Vector Extensions
Got register ymm0 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm1 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm2 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm3 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm4 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm5 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm6 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm7 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm8 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm9 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm10 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm11 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm12 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm13 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm14 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
Got register ymm15 (unsigned char __attribute__((ext_vector_type(32)))) with value: 0
*/
namespace
{
using namespace dbgui;
}