Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ImGuiLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class LLDBDebugger;

class ImGuiLayer {
friend LLDBDebugger;
friend Window;
public:
ImGuiLayer(LLDBDebugger& debugger);
~ImGuiLayer();
Expand Down
19 changes: 12 additions & 7 deletions src/LLDBDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <filesystem>
#include <stdexcept>
#include "Logger.hpp"
#include "Util.hpp"
#ifndef _WIN32
#include <unistd.h>
#else
Expand All @@ -14,7 +15,7 @@
#include <cstdio>
#include <cstring>

LLDBDebugger::LLDBDebugger() {
LLDBDebugger::LLDBDebugger(): eventCallback(nullptr) {
lldb::SBDebugger::Initialize();
debugger = lldb::SBDebugger::Create();
debugger.SetAsync(true);
Expand All @@ -32,6 +33,10 @@ LLDBDebugger::~LLDBDebugger() {
lldbEventThread.join();
}

void LLDBDebugger::SetEventCallback(std::function<void(const Event&)> _eventCallback) {
eventCallback = _eventCallback;
}

void LLDBDebugger::LaunchTarget(std::optional<std::vector<std::string>> args) {
Logger::ScopedGroup g("LaunchTarget");
auto target = GetTarget();
Expand Down Expand Up @@ -212,7 +217,7 @@ void LLDBDebugger::SetActiveLine(BreakpointData data) {
Logger::ScopedGroup g("SetActiveLine");
Logger::Info("file: {}, line: {}", data.path.string(), data.line_number);
active_line = data;
imGuiLayer_ptr->SwitchToCodeFile(active_line->path);
eventCallback(Event{.data = Event::SwitchToFile{.filepath=active_line->path}});
}

bool LLDBDebugger::IsActiveFile(const std::string& path) {
Expand Down Expand Up @@ -290,7 +295,7 @@ LLDBDebugger::ExecResult LLDBDebugger::ExecCommand(const std::string& command, F
Logger::Err("File '{}' does not exist in target", bpfileline.file);
}
else {
imGuiLayer_ptr->FrontendLoadFile(*node);
eventCallback(Event{.data = Event::LoadFile{.node = node}});
if (AddBreakpoint(*node, bpfileline.line)) {
Logger::Info("Breakpoint in file '{}' line {}", bpfileline.file, bpfileline.line);
}
Expand Down Expand Up @@ -337,7 +342,7 @@ LLDBDebugger::ExecResult LLDBDebugger::ExecCommand(const std::string& command, F

auto path = std::filesystem::path(fs.GetDirectory()) / fs.GetFilename();
if (auto node = fh.GetElementByLocalPath(path)) {
imGuiLayer_ptr->FrontendLoadFile(*node);
eventCallback(Event{.data = Event::LoadFile{.node = node}});
if (AddBreakpoint(*node, lineno)) {
Logger::Info("Break on symbol {} in {}", bpsymbol.symbol, fs.GetFilename());
}
Expand Down Expand Up @@ -401,7 +406,7 @@ void LLDBDebugger::DumpToStd(TempRedirect &redirect, std::ostream &out, size_t&

while (fgets(buffer, buffer_size, redirect.file) != nullptr)
{
imGuiLayer_ptr->PushIOLine(std::string(buffer));
eventCallback(Event{.data = Event::IO{.data = std::string(buffer)}});
out << buffer;
}

Expand Down Expand Up @@ -483,7 +488,7 @@ void LLDBDebugger::LLDBEventThread() {
Logger::Info(" Location: {}:{}", file_spec.GetFilename(), line_entry.GetLine());
std::string fullpath = std::string(file_spec.GetDirectory()) + Util::PathSeparator + std::string(file_spec.GetFilename());
SetActiveLine({fullpath, (int)line_entry.GetLine()});
imGuiLayer_ptr->SwitchToCodeFile(file_spec.GetFilename());
eventCallback(Event{.data = Event::SwitchToFile{.filepath = file_spec.GetFilename()}});
}
}
}
Expand Down Expand Up @@ -537,6 +542,6 @@ void LLDBDebugger::LLDBEventThread() {
int exitCode = process.GetExitStatus();
const char* exitReason = process.GetExitDescription() ? process.GetExitDescription() : "none";
std::string fmt = fmt::format("Process exitted [code={}, reason={}]", exitCode, exitReason);
imGuiLayer_ptr->PushIOLine(fmt);
eventCallback(Event{.data = Event::IO{.data=fmt}});
Logger::Info("LLDB Event Thread Stopping");
}
22 changes: 19 additions & 3 deletions src/LLDBDebugger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@

struct FileContext;
#include <thread>
#include <variant>
#include <fmt/core.h>
#include "LLDBCommandParser.hpp"
#include "TempRedirect.hpp"

class ImGuiLayer;

class LLDBDebugger {
friend class Window;
public:
struct Event {
struct Continue {};
struct StepOver {};
struct StepInto {};
struct LoadFile {
FileHierarchy::TreeNode* node;
};
struct IO {
std::string data;
};
struct SwitchToFile {
std::filesystem::path filepath;
};
std::variant<Continue, StepOver, StepInto, LoadFile, IO, SwitchToFile> data;
};
public:
enum class ExecResultStatus {
Ok,
Expand Down Expand Up @@ -43,6 +58,7 @@ class LLDBDebugger {
public:
LLDBDebugger();
~LLDBDebugger();
void SetEventCallback(std::function<void(const Event&)> eventCallback);

void LaunchTarget(std::optional<std::vector<std::string>> args);
lldb::SBDebugger& GetDebugger();
Expand Down Expand Up @@ -92,7 +108,7 @@ class LLDBDebugger {
void DumpToStd(TempRedirect &redirect, std::ostream &out, size_t& offset);

protected:
ImGuiLayer* imGuiLayer_ptr = 0;
std::function<void(const Event&)> eventCallback;

private:
LLDB_CommandParser commandParser;
Expand Down
34 changes: 33 additions & 1 deletion src/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
#include <glad/gl.h>

Window::Window(const std::string& title, int width, int height):
debuggerCtx(),
imguiLayer(debuggerCtx)
{
debuggerCtx.imGuiLayer_ptr = &imguiLayer;
std::function<void(const LLDBDebugger::Event&)> listener = std::bind(&Window::DebuggerEventListener, this, std::placeholders::_1);
debuggerCtx.SetEventCallback(listener);

// Initialize glfw window
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
Expand Down Expand Up @@ -78,6 +81,35 @@ Window::Window(const std::string& title, int width, int height):
Logger::Info("Created window");
}

void Window::DebuggerEventListener(const LLDBDebugger::Event& event) {
std::visit([](auto&& arg) {
Logger::Info("Handling... {}", typeid(arg).name());
}, event.data);
if (auto e = std::get_if<LLDBDebugger::Event::LoadFile>(&event.data)) {
imguiLayer.FrontendLoadFile(*e->node);
}
else if (auto e = std::get_if<LLDBDebugger::Event::IO>(&event.data)) {
imguiLayer.PushIOLine(e->data);
}
else if (auto e = std::get_if<LLDBDebugger::Event::Continue>(&event.data)) {
debuggerCtx.Continue();
}
else if (auto e = std::get_if<LLDBDebugger::Event::StepOver>(&event.data)) {
debuggerCtx.StepOver();
}
else if (auto e = std::get_if<LLDBDebugger::Event::StepInto>(&event.data)) {
debuggerCtx.StepInto();
}
else if (auto e = std::get_if<LLDBDebugger::Event::SwitchToFile>(&event.data)) {
imguiLayer.SwitchToCodeFile(e->filepath);
}
else {
std::visit([](auto&& arg) {
Logger::Crit("Type not handled: {}", typeid(arg).name());
}, event.data);
}
}

Window::~Window() {
glfwDestroyWindow(m_Window);
Logger::Info("Destroyed window");
Expand Down
2 changes: 1 addition & 1 deletion src/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Window {
LLDBDebugger& GetDebuggerCtx();

private:

void DebuggerEventListener(const LLDBDebugger::Event& e);

private:
GLFWwindow *m_Window;
Expand Down