diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/debugger/debugger.cpp | 240 | ||||
| -rw-r--r-- | src/debugger/debugger.h | 7 | ||||
| -rw-r--r-- | src/debugger/types.h | 1 | ||||
| -rw-r--r-- | src/ui/source_view.cpp | 11 | ||||
| -rw-r--r-- | src/ui/ui.cpp | 24 | ||||
| -rw-r--r-- | src/ui/ui.h | 1 |
6 files changed, 277 insertions, 7 deletions
diff --git a/src/debugger/debugger.cpp b/src/debugger/debugger.cpp index c552988..9b50096 100644 --- a/src/debugger/debugger.cpp +++ b/src/debugger/debugger.cpp @@ -1,14 +1,110 @@ #include "debugger/debugger.h" +#include "core/assert.h" #include "core/log.h" +#include <lldb/API/LLDB.h> + #include <algorithm> #include <source_location> +#include <cstdlib> + namespace Hsdbg { namespace { +#ifndef HSDBG_LLVM_PREFIX + #define HSDBG_LLVM_PREFIX "" +#endif + + auto set_environment(const char* name, const std::string& value) -> void + { +#ifdef HSDBG_WINDOWS + _putenv_s(name, value.c_str()); +#else + setenv(name, value.c_str(), 1); +#endif + } + + auto debug_server_candidates() -> std::vector<std::filesystem::path> + { + const std::filesystem::path llvm_prefix(HSDBG_LLVM_PREFIX); + +#if defined(HSDBG_MACOS) + return { + "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/" + "Resources/debugserver", + "/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/" + "Resources/debugserver", + llvm_prefix / "bin" / "debugserver", + }; +#elif defined(HSDBG_LINUX) + return { llvm_prefix / "bin" / "lldb-server" }; +#else + return {}; +#endif + } + + auto adopt_debug_server() -> void + { + const std::vector<std::filesystem::path> candidates = debug_server_candidates(); + + if (candidates.empty()) + return; + + if (std::getenv("LLDB_DEBUGSERVER_PATH") != nullptr) + return; + + for (const std::filesystem::path& candidate : candidates) + { + std::error_code error; + + if (!std::filesystem::exists(candidate, error)) + continue; + + set_environment("LLDB_DEBUGSERVER_PATH", candidate.string()); + Log::debug("debugger: using debug server '{}'", candidate.string()); + + return; + } + + Log::warn("debugger: no debug server found, launching a target will fail"); + } + + auto read_back(lldb::SBTarget& target, lldb::SBBreakpoint& source, Breakpoint& breakpoint) -> void + { + breakpoint.enabled = source.IsEnabled(); + breakpoint.hit_count = source.GetHitCount(); + + breakpoint.resolved = source.GetNumLocations() > 0; + + if (source.GetNumLocations() == 0) + { + breakpoint.address = 0; + return; + } + + lldb::SBBreakpointLocation location = source.GetLocationAtIndex(0); + lldb::SBAddress address = location.GetAddress(); + + const lldb::addr_t load_address = address.GetLoadAddress(target); + breakpoint.address = load_address != LLDB_INVALID_ADDRESS ? load_address + : address.GetFileAddress(); + + lldb::SBSymbolContext context = address.GetSymbolContext(lldb::eSymbolContextEverything); + + if (const char* name = context.GetFunction().GetName(); name != nullptr) + breakpoint.function = name; + else if (const char* symbol = context.GetSymbol().GetName(); symbol != nullptr) + breakpoint.function = symbol; + + lldb::SBLineEntry line_entry = context.GetLineEntry(); + + if (breakpoint.line != 0 && line_entry.IsValid()) + breakpoint.line = line_entry.GetLine(); + } + auto not_implemented(std::source_location location = std::source_location::current()) -> std::unexpected<std::string> { @@ -17,14 +113,37 @@ namespace Hsdbg } } + struct Debugger::Session + { + lldb::SBDebugger debugger; + lldb::SBTarget target; + }; + Debugger::Debugger() + : m_session(std::make_unique<Session>()) { - Log::info("debugger: ready"); + adopt_debug_server(); + + lldb::SBDebugger::Initialize(); + + m_session->debugger = lldb::SBDebugger::Create(); + m_session->debugger.SetAsync(true); + + HSDBG_ASSERT(m_session->debugger.IsValid(), "failed to create the lldb debugger"); + + Log::info("debugger: {}", lldb::SBDebugger::GetVersionString()); } Debugger::~Debugger() { unload_target(); + + if (m_session->debugger.IsValid()) + lldb::SBDebugger::Destroy(m_session->debugger); + + m_session.reset(); + + lldb::SBDebugger::Terminate(); } auto Debugger::load_target(const std::filesystem::path& executable) -> Result<void> @@ -41,11 +160,41 @@ namespace Hsdbg if (error) m_target_path = executable; + lldb::SBError create_error; + lldb::SBTarget target = m_session->debugger.CreateTarget(m_target_path.string().c_str(), + nullptr, + nullptr, + true, + create_error); + + if (!target.IsValid()) + { + const char* reason = create_error.GetCString(); + + m_target_path.clear(); + + return fail("could not load '{}': {}", + executable.string(), + reason != nullptr ? reason : "unknown error"); + } + + if (m_session->target.IsValid()) + m_session->debugger.DeleteTarget(m_session->target); + + m_session->target = target; + m_process_id = 0; m_stop_reason = StopReason::None; set_state(TargetState::Loaded); - Log::info("debugger: loaded target '{}'", m_target_path.string()); + const char* triple = m_session->target.GetTriple(); + + Log::info("debugger: loaded target '{}' ({})", + m_target_path.string(), + triple != nullptr ? triple : "unknown triple"); + + for (Breakpoint& breakpoint : m_breakpoints) + resolve_breakpoint(breakpoint); return {}; } @@ -67,11 +216,19 @@ namespace Hsdbg for (Breakpoint& breakpoint : m_breakpoints) { + breakpoint.backend_id = 0; breakpoint.resolved = false; breakpoint.hit_count = 0; breakpoint.address = 0; } + if (m_session->target.IsValid()) + { + m_session->target.DeleteAllBreakpoints(); + m_session->debugger.DeleteTarget(m_session->target); + m_session->target = lldb::SBTarget(); + } + set_state(TargetState::NoTarget); } @@ -147,6 +304,7 @@ namespace Hsdbg breakpoint.line = line; m_breakpoints.push_back(std::move(breakpoint)); + resolve_breakpoint(m_breakpoints.back()); Log::info("debugger: breakpoint {} at {}:{}", m_breakpoints.back().id, @@ -171,6 +329,7 @@ namespace Hsdbg breakpoint.function = function; m_breakpoints.push_back(std::move(breakpoint)); + resolve_breakpoint(m_breakpoints.back()); Log::info("debugger: breakpoint {} at {}()", m_breakpoints.back().id, function); @@ -183,6 +342,9 @@ namespace Hsdbg if (entry == m_breakpoints.end()) return false; + if (entry->backend_id != 0 && m_session->target.IsValid()) + m_session->target.BreakpointDelete(entry->backend_id); + m_breakpoints.erase(entry); Log::info("debugger: removed breakpoint {}", id); @@ -198,11 +360,22 @@ namespace Hsdbg breakpoint->enabled = enabled; + if (breakpoint->backend_id != 0 && m_session->target.IsValid()) + { + lldb::SBBreakpoint source = m_session->target.FindBreakpointByID(breakpoint->backend_id); + + if (source.IsValid()) + source.SetEnabled(enabled); + } + return true; } auto Debugger::clear_breakpoints() -> void { + if (m_session->target.IsValid()) + m_session->target.DeleteAllBreakpoints(); + m_breakpoints.clear(); } @@ -250,6 +423,69 @@ namespace Hsdbg auto Debugger::update() -> void { + sync_breakpoints(); + } + + auto Debugger::resolve_breakpoint(Breakpoint& breakpoint) -> void + { + if (!m_session->target.IsValid()) + return; + + lldb::SBBreakpoint created; + + if (breakpoint.line == 0) + { + created = m_session->target.BreakpointCreateByName( + breakpoint.function.c_str(), m_session->target.GetExecutable().GetFilename()); + } + else + { + created = m_session->target.BreakpointCreateByLocation(breakpoint.file.string().c_str(), + breakpoint.line); + + if (created.GetNumLocations() == 0 && breakpoint.file.has_parent_path()) + { + m_session->target.BreakpointDelete(created.GetID()); + + created = m_session->target.BreakpointCreateByLocation( + breakpoint.file.filename().string().c_str(), breakpoint.line); + } + } + + if (!created.IsValid()) + { + Log::warn("debugger: lldb refused breakpoint {}", breakpoint.id); + return; + } + + created.SetEnabled(breakpoint.enabled); + + breakpoint.backend_id = created.GetID(); + + read_back(m_session->target, created, breakpoint); + + Log::debug("debugger: breakpoint {} {} ({} locations)", + breakpoint.id, + breakpoint.resolved ? "resolved" : "pending", + created.GetNumLocations()); + } + + auto Debugger::sync_breakpoints() -> void + { + if (!m_session->target.IsValid()) + return; + + for (Breakpoint& breakpoint : m_breakpoints) + { + if (breakpoint.backend_id == 0) + continue; + + lldb::SBBreakpoint source = m_session->target.FindBreakpointByID(breakpoint.backend_id); + if (!source.IsValid()) + continue; + + read_back(m_session->target, source, breakpoint); + } } auto Debugger::set_state(TargetState state) -> void diff --git a/src/debugger/debugger.h b/src/debugger/debugger.h index 23bb173..0dbeda6 100644 --- a/src/debugger/debugger.h +++ b/src/debugger/debugger.h @@ -5,6 +5,7 @@ #include <cstdint> #include <filesystem> +#include <memory> #include <span> #include <string> #include <string_view> @@ -70,7 +71,13 @@ namespace Hsdbg auto is_stopped() const -> bool { return m_state == TargetState::Stopped; } private: + struct Session; + auto set_state(TargetState state) -> void; + auto resolve_breakpoint(Breakpoint& breakpoint) -> void; + auto sync_breakpoints() -> void; + + std::unique_ptr<Session> m_session; TargetState m_state = TargetState::NoTarget; StopReason m_stop_reason = StopReason::None; diff --git a/src/debugger/types.h b/src/debugger/types.h index 2f9a698..193a1cc 100644 --- a/src/debugger/types.h +++ b/src/debugger/types.h @@ -78,6 +78,7 @@ namespace Hsdbg struct Breakpoint { uint32_t id = 0; + int32_t backend_id = 0; std::filesystem::path file; uint32_t line = 0; std::string function; diff --git a/src/ui/source_view.cpp b/src/ui/source_view.cpp index e8e7d1e..652acc0 100644 --- a/src/ui/source_view.cpp +++ b/src/ui/source_view.cpp @@ -190,10 +190,13 @@ namespace Hsdbg if (breakpoint != nullptr) { - draw_list->AddCircleFilled(marker_center, - BREAKPOINT_RADIUS, - breakpoint->enabled ? BREAKPOINT_COLOR - : BREAKPOINT_DISABLED_COLOR); + const ImU32 color = breakpoint->enabled ? BREAKPOINT_COLOR + : BREAKPOINT_DISABLED_COLOR; + + if (breakpoint->resolved) + draw_list->AddCircleFilled(marker_center, BREAKPOINT_RADIUS, color); + else + draw_list->AddCircle(marker_center, BREAKPOINT_RADIUS, color, 0, 1.5f); } else if (gutter_hovered) { diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index f7cbcae..56348ff 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -169,6 +169,8 @@ namespace Hsdbg { build_default_layout(dockspace_id); m_layout_built = true; + + m_focus_breakpoints = true; } draw_status_bar(debugger); @@ -186,6 +188,12 @@ namespace Hsdbg if (m_visible.demo) ImGui::ShowDemoWindow(&m_visible.demo); + + if (m_focus_breakpoints) + { + ImGui::SetWindowFocus(PANEL_BREAKPOINTS); + m_focus_breakpoints = false; + } } auto Ui::build_default_layout(uint32_t dockspace_id) -> void @@ -460,11 +468,12 @@ namespace Hsdbg ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_ScrollY; - if (ImGui::BeginTable("##breakpoints", 5, flags)) + if (ImGui::BeginTable("##breakpoints", 6, flags)) { ImGui::TableSetupColumn("on", ImGuiTableColumnFlags_WidthFixed, 26.0f); ImGui::TableSetupColumn("id", ImGuiTableColumnFlags_WidthFixed, 30.0f); ImGui::TableSetupColumn("location"); + ImGui::TableSetupColumn("address"); ImGui::TableSetupColumn("hits", ImGuiTableColumnFlags_WidthFixed, 40.0f); ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 26.0f); ImGui::TableSetupScrollFreeze(0, 1); @@ -501,6 +510,19 @@ namespace Hsdbg ImGui::SetTooltip("%s", breakpoint.file.string().c_str()); ImGui::TableNextColumn(); + if (breakpoint.resolved) + { + ImGui::Text("0x%llx", static_cast<unsigned long long>(breakpoint.address)); + + if (ImGui::IsItemHovered() && !breakpoint.function.empty()) + ImGui::SetTooltip("%s", breakpoint.function.c_str()); + } + else + { + ImGui::TextDisabled("pending"); + } + + ImGui::TableNextColumn(); ImGui::Text("%u", breakpoint.hit_count); ImGui::TableNextColumn(); diff --git a/src/ui/ui.h b/src/ui/ui.h index 7c0de5d..fd86497 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -74,5 +74,6 @@ namespace Hsdbg bool m_layout_built = false; bool m_console_scroll_pending = false; bool m_load_target_pending = false; + bool m_focus_breakpoints = false; }; } |
