ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
lldb_controller.h
Go to the documentation of this file.
1
14#pragma once
15
16#include <lldb/API/LLDB.h>
17
18#include <cstdint>
19#include <functional>
20#include <memory>
21#include <optional>
22#include <string>
23#include <vector>
24
25// Windows doesn't define pid_t, use LLDB's definition
26#ifdef _WIN32
27using pid_t = lldb::pid_t;
28#endif
29
30namespace ascii_query {
31
35enum class ProcessState {
36 Invalid,
37 Running,
38 Stopped,
39 Exited,
40 Crashed,
41 Detached,
42};
43
47struct ThreadInfo {
50 std::string name;
51 std::string stop_reason;
52 std::string function;
53 std::string file;
56};
57
69
74 std::string name;
75 std::string type;
76 std::string value;
77 std::string summary;
79 size_t size;
80 bool is_valid;
83
84 std::vector<VariableInfo> children;
85};
86
91 int32_t id;
92 std::string file;
94 std::string condition;
96 bool enabled;
97 bool resolved;
98};
99
107public:
110
111 // Non-copyable, non-movable (owns LLDB resources)
116
117 // =========================================================================
118 // Initialization
119 // =========================================================================
120
125 bool initialize();
126
130 void shutdown();
131
132 // =========================================================================
133 // Process Attachment
134 // =========================================================================
135
143 bool attach(pid_t pid);
144
151 bool attachByName(const std::string &process_name, bool wait_for = false);
152
158 void detach();
159
164 [[nodiscard]] bool isAttached() const;
165
170 [[nodiscard]] pid_t targetPid() const;
171
176 [[nodiscard]] std::string targetName() const;
177
178 // =========================================================================
179 // Process Control
180 // =========================================================================
181
186 bool stop();
187
192 bool resume();
193
198 bool stepInto();
199
204 bool stepOver();
205
210 bool stepOut();
211
216 [[nodiscard]] ProcessState state() const;
217
222 [[nodiscard]] const std::string &lastError() const;
223
224 // =========================================================================
225 // Thread Information
226 // =========================================================================
227
232 [[nodiscard]] std::vector<ThreadInfo> getThreads() const;
233
238 [[nodiscard]] std::optional<ThreadInfo> getSelectedThread() const;
239
246
247 // =========================================================================
248 // Stack Frames
249 // =========================================================================
250
256 [[nodiscard]] std::vector<FrameInfo> getFrames(uint32_t max_frames = 0) const;
257
263 [[nodiscard]] std::optional<FrameInfo> getFrame(uint32_t frame_index) const;
264
265 // =========================================================================
266 // Variable Reading
267 // =========================================================================
268
276 [[nodiscard]] std::optional<VariableInfo> readVariable(const std::string &name, uint32_t frame_index = 0,
277 uint32_t expand_depth = 0) const;
278
287 [[nodiscard]] std::vector<VariableInfo> listVariables(uint32_t frame_index = 0, bool include_args = true,
288 bool include_locals = true, bool include_statics = false) const;
289
290 // =========================================================================
291 // Breakpoints
292 // =========================================================================
293
301 int32_t setBreakpoint(const std::string &file, uint32_t line, const std::string &condition = "");
302
308 bool removeBreakpoint(int32_t breakpoint_id);
309
314 [[nodiscard]] std::vector<BreakpointInfo> getBreakpoints() const;
315
321 [[nodiscard]] std::optional<BreakpointInfo> getBreakpoint(int32_t breakpoint_id) const;
322
328 bool waitForBreakpoint(uint32_t timeout_ms = 0);
329
330 // =========================================================================
331 // Expression Evaluation
332 // =========================================================================
333
340 [[nodiscard]] std::optional<VariableInfo> evaluateExpression(const std::string &expression,
341 uint32_t frame_index = 0) const;
342
343private:
344 // LLDB objects (mutable because LLDB's SB API methods aren't const-correct)
345 mutable lldb::SBDebugger debugger_;
346 mutable lldb::SBTarget target_;
347 mutable lldb::SBProcess process_;
348 mutable lldb::SBListener listener_;
349
350 // State
351 bool initialized_ = false;
352 mutable std::string last_error_;
353
354 // Helper methods
355 void setError(const std::string &msg) const;
356 void clearError() const;
357
358 // Drain pending LLDB events to synchronize process state
359 void syncProcessState() const;
360
361 // Wait for process to reach a specific state (with timeout in seconds)
362 bool waitForState(lldb::StateType target_state, uint32_t timeout_sec = 5) const;
363
364 [[nodiscard]] lldb::SBThread getSelectedThreadInternal() const;
365 [[nodiscard]] lldb::SBFrame getFrameInternal(uint32_t index) const;
366
367 // Note: LLDB SB API methods are not const in LLVM 21+, so we use non-const references
368 [[nodiscard]] static ThreadInfo threadToInfo(lldb::SBThread &thread, bool is_selected);
369 [[nodiscard]] static FrameInfo frameToInfo(lldb::SBFrame &frame);
370 [[nodiscard]] VariableInfo valueToInfo(lldb::SBValue value, uint32_t expand_depth) const;
371 [[nodiscard]] static BreakpointInfo breakpointToInfo(lldb::SBBreakpoint bp);
372};
373
374} // namespace ascii_query
int thread_id
LLDB process controller.
int32_t setBreakpoint(const std::string &file, uint32_t line, const std::string &condition="")
Set a breakpoint at file:line.
std::optional< VariableInfo > evaluateExpression(const std::string &expression, uint32_t frame_index=0) const
Evaluate an expression in the current context.
bool attachByName(const std::string &process_name, bool wait_for=false)
Attach to a process by name.
bool removeBreakpoint(int32_t breakpoint_id)
Remove a breakpoint.
bool stepInto()
Single step the current thread (step into)
pid_t targetPid() const
Get the PID of the attached process.
LLDBController & operator=(LLDBController &&)=delete
std::string targetName() const
Get the name of the attached process.
bool selectThread(uint64_t thread_id)
Select a thread by ID.
std::optional< VariableInfo > readVariable(const std::string &name, uint32_t frame_index=0, uint32_t expand_depth=0) const
Read a variable from the current frame.
const std::string & lastError() const
Get the last error message.
std::vector< VariableInfo > listVariables(uint32_t frame_index=0, bool include_args=true, bool include_locals=true, bool include_statics=false) const
List all variables in scope at a frame.
bool initialize()
Initialize LLDB. Must be called before any other methods.
void shutdown()
Shutdown LLDB and release resources.
LLDBController(LLDBController &&)=delete
bool stop()
Stop the target process.
bool isAttached() const
Check if attached to a process.
LLDBController & operator=(const LLDBController &)=delete
LLDBController(const LLDBController &)=delete
std::vector< ThreadInfo > getThreads() const
Get list of all threads.
std::optional< FrameInfo > getFrame(uint32_t frame_index) const
Get a specific frame by index.
std::vector< BreakpointInfo > getBreakpoints() const
Get all breakpoints.
bool resume()
Resume the target process.
std::vector< FrameInfo > getFrames(uint32_t max_frames=0) const
Get stack frames for the selected thread.
bool stepOver()
Step over the current line.
std::optional< BreakpointInfo > getBreakpoint(int32_t breakpoint_id) const
Get information about a specific breakpoint.
bool stepOut()
Step out of the current function.
void detach()
Detach from the current process.
bool attach(pid_t pid)
Attach to a process by PID.
ProcessState state() const
Get the current process state.
std::optional< ThreadInfo > getSelectedThread() const
Get the currently selected thread.
bool waitForBreakpoint(uint32_t timeout_ms=0)
Wait for any breakpoint to be hit.
unsigned int uint32_t
Definition common.h:58
unsigned long long uint64_t
Definition common.h:59
ProcessState
Process state enumeration.
@ Exited
Process has exited.
@ Crashed
Process crashed.
@ Detached
Detached from process.
@ Invalid
No valid process attached.
@ Running
Process is running normally.
@ Stopped
Process is stopped (breakpoint, signal, etc.)
Breakpoint information.
int32_t id
LLDB breakpoint ID.
std::string condition
Condition expression (may be empty)
bool enabled
Is breakpoint enabled?
uint32_t line
Line number.
bool resolved
Is breakpoint resolved (has valid location)?
uint32_t hit_count
Number of times hit.
std::string file
Source file.
Stack frame information.
uint32_t line
Line number.
std::string function
Function name.
uint64_t pc
Program counter.
uint64_t fp
Frame pointer.
uint32_t index
Frame index (0 = innermost)
std::string file
Source file.
Thread information.
std::string name
Thread name (may be empty)
uint32_t line
Current line number.
uint64_t id
Thread ID.
bool is_selected
Is this the selected thread?
std::string stop_reason
Why thread is stopped (if stopped)
std::string function
Current function name.
uint32_t index
Index in thread list.
std::string file
Current source file.
Variable information (from LLDB SBValue)
std::vector< VariableInfo > children
Struct members / array elements.
std::string value
Value as string.
bool is_aggregate
Whether this is struct/class/array.
size_t size
Size in bytes.
std::string type
Type name.
std::string name
Variable name.
bool is_pointer
Whether this is a pointer type.
std::string summary
LLDB summary (for complex types)
bool is_valid
Whether value could be read.
uint64_t address
Memory address.