From 6a3dbf44bb8056f3f4554282f092132332ac7f4a Mon Sep 17 00:00:00 2001 From: hachem Date: Thu, 10 Oct 2024 14:17:34 +0200 Subject: feat: debugger interface --- src/debugger/debugger.h | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/debugger/debugger.h (limited to 'src/debugger/debugger.h') diff --git a/src/debugger/debugger.h b/src/debugger/debugger.h new file mode 100644 index 0000000..23bb173 --- /dev/null +++ b/src/debugger/debugger.h @@ -0,0 +1,93 @@ +#pragma once + +#include "core/result.h" +#include "debugger/types.h" + +#include +#include +#include +#include +#include +#include + +namespace Hsdbg +{ + class Debugger + { + public: + Debugger(); + ~Debugger(); + + Debugger(const Debugger&) = delete; + Debugger(Debugger&&) = delete; + auto operator=(const Debugger&) -> Debugger& = delete; + auto operator=(Debugger&&) -> Debugger& = delete; + + auto load_target(const std::filesystem::path& executable) -> Result; + auto unload_target() -> void; + auto launch(const LaunchSpec& spec) -> Result; + auto attach(uint64_t process_id) -> Result; + auto detach() -> Result; + auto terminate() -> Result; + + auto resume() -> Result; + auto pause() -> Result; + auto step_over(StepMode mode = StepMode::Line) -> Result; + auto step_into(StepMode mode = StepMode::Line) -> Result; + auto step_out() -> Result; + auto run_to(const std::filesystem::path& file, uint32_t line) -> Result; + + auto add_breakpoint(const std::filesystem::path& file, uint32_t line) -> uint32_t; + auto add_function_breakpoint(std::string_view function) -> uint32_t; + auto remove_breakpoint(uint32_t id) -> bool; + auto set_breakpoint_enabled(uint32_t id, bool enabled) -> bool; + auto clear_breakpoints() -> void; + auto find_breakpoint(uint32_t id) -> Breakpoint*; + auto breakpoints() const -> std::span { return m_breakpoints; } + + auto threads() const -> std::span { return m_threads; } + auto call_stack() const -> std::span { return m_call_stack; } + auto locals() const -> std::span { return m_locals; } + auto registers() const -> std::span { return m_registers; } + auto evaluate(std::string_view expression) -> Result; + auto read_memory(uint64_t address, size_t size) -> Result>; + auto console_output() const -> std::span { return m_console_output; } + + auto select_thread(uint64_t thread_id) -> bool; + auto select_frame(uint32_t frame_index) -> bool; + auto selected_thread() const -> uint64_t { return m_selected_thread; } + auto selected_frame() const -> uint32_t { return m_selected_frame; } + + auto update() -> void; + + auto state() const -> TargetState { return m_state; } + auto stop_reason() const -> StopReason { return m_stop_reason; } + auto target_path() const -> const std::filesystem::path& { return m_target_path; } + auto process_id() const -> uint64_t { return m_process_id; } + + auto has_target() const -> bool { return m_state != TargetState::NoTarget; } + auto is_running() const -> bool { return m_state == TargetState::Running; } + auto is_stopped() const -> bool { return m_state == TargetState::Stopped; } + + private: + auto set_state(TargetState state) -> void; + + TargetState m_state = TargetState::NoTarget; + StopReason m_stop_reason = StopReason::None; + + std::filesystem::path m_target_path; + uint64_t m_process_id = 0; + + std::vector m_breakpoints; + uint32_t m_next_breakpoint_id = 1; + + std::vector m_threads; + std::vector m_call_stack; + std::vector m_locals; + std::vector m_registers; + std::vector m_console_output; + + uint64_t m_selected_thread = 0; + uint32_t m_selected_frame = 0; + }; +} -- cgit v1.3