ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
lldb_controller.cpp
Go to the documentation of this file.
1
5
6/* Storage capacities in elements; text buffers include the terminating NUL. */
7#define TOOLING_QUERY_LLDB_CONTROLLER_PATH_BUFFER_SIZE 1024
8
9#include "lldb_controller.h"
10
11#include <lldb/API/SBBreakpoint.h>
12#include <lldb/API/SBBreakpointLocation.h>
13#include <lldb/API/SBCommandInterpreter.h>
14#include <lldb/API/SBDebugger.h>
15#include <lldb/API/SBError.h>
16#include <lldb/API/SBEvent.h>
17#include <lldb/API/SBFileSpec.h>
18#include <lldb/API/SBFrame.h>
19#include <lldb/API/SBListener.h>
20#include <lldb/API/SBProcess.h>
21#include <lldb/API/SBStream.h>
22#include <lldb/API/SBTarget.h>
23#include <lldb/API/SBThread.h>
24#include <lldb/API/SBType.h>
25#include <lldb/API/SBValue.h>
26#include <lldb/API/SBValueList.h>
27
28#include <chrono>
29#include <cstdio>
30
31namespace ascii_query {
32
33// =============================================================================
34// Constructor / Destructor
35// =============================================================================
36
38
42
43// =============================================================================
44// Initialization
45// =============================================================================
46
48 if (initialized_) {
49 return true;
50 }
51
52 // Initialize LLDB
53 lldb::SBDebugger::Initialize();
54
55 // Create debugger instance
56 debugger_ = lldb::SBDebugger::Create(false); // false = no source manager
57 if (!debugger_.IsValid()) {
58 setError("Failed to create LLDB debugger instance");
59 return false;
60 }
61
62 // Don't echo commands
63 debugger_.SetAsync(true);
64
65 // Create a listener for process events
66 listener_ = debugger_.GetListener();
67 if (!listener_.IsValid()) {
68 setError("Failed to create LLDB listener");
69 lldb::SBDebugger::Destroy(debugger_);
70 return false;
71 }
72
73 initialized_ = true;
74 clearError();
75 return true;
76}
77
79 if (!initialized_) {
80 return;
81 }
82
83 // Detach if still attached
84 if (isAttached()) {
85 detach();
86 }
87
88 // Delete target if valid
89 if (target_.IsValid()) {
90 debugger_.DeleteTarget(target_);
91 }
92
93 // Destroy debugger
94 if (debugger_.IsValid()) {
95 lldb::SBDebugger::Destroy(debugger_);
96 }
97
98 // Terminate LLDB
99 lldb::SBDebugger::Terminate();
100
101 initialized_ = false;
102}
103
104// =============================================================================
105// Process Attachment
106// =============================================================================
107
108bool LLDBController::attach(pid_t pid) {
109 if (!initialized_) {
110 setError("LLDB not initialized");
111 return false;
112 }
113
114 // Create an empty target - LLDB will fill it in when we attach
115 lldb::SBError error;
116 target_ = debugger_.CreateTarget("", "", "", false, error);
117 if (!target_.IsValid()) {
118 setError("Failed to create target: " + std::string(error.GetCString() ? error.GetCString() : "unknown error"));
119 return false;
120 }
121
122 // Attach to process
123 lldb::SBAttachInfo attach_info(pid);
124 attach_info.SetListener(listener_);
125
126 process_ = target_.Attach(attach_info, error);
127 if (!process_.IsValid() || error.Fail()) {
128 setError("Failed to attach to PID " + std::to_string(pid) + ": " + std::string(error.GetCString() ? error.GetCString() : "unknown error"));
129 debugger_.DeleteTarget(target_);
130 return false;
131 }
132
133 clearError();
134 return true;
135}
136
137bool LLDBController::attachByName(const std::string &process_name, bool wait_for) {
138 if (!initialized_) {
139 setError("LLDB not initialized");
140 return false;
141 }
142
143 // Create an empty target
144 lldb::SBError error;
145 target_ = debugger_.CreateTarget("", "", "", false, error);
146 if (!target_.IsValid()) {
147 setError("Failed to create target: " + std::string(error.GetCString() ? error.GetCString() : "unknown error"));
148 return false;
149 }
150
151 // Attach by name
152 lldb::SBAttachInfo attach_info;
153 attach_info.SetExecutable(process_name.c_str());
154 attach_info.SetWaitForLaunch(wait_for, false); // false = don't async
155 attach_info.SetListener(listener_);
156
157 process_ = target_.Attach(attach_info, error);
158 if (!process_.IsValid() || error.Fail()) {
159 setError("Failed to attach to process '" + process_name + "': " + std::string(error.GetCString() ? error.GetCString() : "unknown error"));
160 debugger_.DeleteTarget(target_);
161 return false;
162 }
163
164 clearError();
165 return true;
166}
167
169 if (!isAttached()) {
170 return;
171 }
172
173 lldb::SBError error = process_.Detach();
174 if (error.Fail()) {
175 setError("Detach failed: " + std::string(error.GetCString() ? error.GetCString() : "unknown error"));
176 } else {
177 clearError();
178 }
179}
180
182 if (!process_.IsValid()) {
183 return false;
184 }
185
186 lldb::StateType state = process_.GetState();
187 return state != lldb::eStateInvalid && state != lldb::eStateDetached && state != lldb::eStateExited;
188}
189
191 if (!process_.IsValid()) {
192 return 0;
193 }
194 return static_cast<pid_t>(process_.GetProcessID());
195}
196
197std::string LLDBController::targetName() const {
198 if (!target_.IsValid()) {
199 return "";
200 }
201
202 lldb::SBFileSpec exe = target_.GetExecutable();
203 if (!exe.IsValid()) {
204 return "";
205 }
206
207 const char *filename = exe.GetFilename();
208 return filename ? filename : "";
209}
210
211// =============================================================================
212// Process Control
213// =============================================================================
214
216 if (!isAttached()) {
217 setError("Not attached to a process");
218 return false;
219 }
220
221 // Sync state first to ensure we have the current process state
222 syncProcessState();
223
224 // Check if already stopped
225 lldb::StateType current = process_.GetState();
226 if (current == lldb::eStateStopped || current == lldb::eStateSuspended) {
227 clearError();
228 return true;
229 }
230
231 // Only try to stop if running
232 if (current != lldb::eStateRunning && current != lldb::eStateStepping) {
233 setError("Process is not running (state: " + std::to_string(static_cast<int>(current)) + ")");
234 return false;
235 }
236
237 lldb::SBError error = process_.Stop();
238 if (error.Fail()) {
239 setError("Failed to stop process: " + std::string(error.GetCString() ? error.GetCString() : "unknown error"));
240 return false;
241 }
242
243 // Wait for process to actually stop (async mode)
244 if (!waitForState(lldb::eStateStopped, 5)) {
245 setError("Timeout waiting for process to stop");
246 return false;
247 }
248
249 clearError();
250 return true;
251}
252
254 if (!isAttached()) {
255 setError("Not attached to a process");
256 return false;
257 }
258
259 // Sync state first
260 syncProcessState();
261
262 // Check if already running
263 lldb::StateType current = process_.GetState();
264 if (current == lldb::eStateRunning || current == lldb::eStateStepping) {
265 clearError();
266 return true;
267 }
268
269 lldb::SBError error = process_.Continue();
270 if (error.Fail()) {
271 setError("Failed to resume process: " + std::string(error.GetCString() ? error.GetCString() : "unknown error"));
272 return false;
273 }
274
275 // Wait briefly for process to start running (async mode)
276 // Note: We don't wait long because running is the target state
277 waitForState(lldb::eStateRunning, 2);
278
279 clearError();
280 return true;
281}
282
284 lldb::SBThread thread = getSelectedThreadInternal();
285 if (!thread.IsValid()) {
286 setError("No valid thread selected");
287 return false;
288 }
289
290 thread.StepInto();
291 clearError();
292 return true;
293}
294
296 lldb::SBThread thread = getSelectedThreadInternal();
297 if (!thread.IsValid()) {
298 setError("No valid thread selected");
299 return false;
300 }
301
302 thread.StepOver();
303 clearError();
304 return true;
305}
306
308 lldb::SBThread thread = getSelectedThreadInternal();
309 if (!thread.IsValid()) {
310 setError("No valid thread selected");
311 return false;
312 }
313
314 thread.StepOut();
315 clearError();
316 return true;
317}
318
320 if (!process_.IsValid()) {
322 }
323
324 lldb::StateType lldb_state = process_.GetState();
325 switch (lldb_state) {
326 case lldb::eStateInvalid:
328 case lldb::eStateRunning:
329 case lldb::eStateStepping:
331 case lldb::eStateStopped:
332 case lldb::eStateSuspended:
334 case lldb::eStateExited:
336 case lldb::eStateCrashed:
338 case lldb::eStateDetached:
340 default:
342 }
343}
344
345const std::string &LLDBController::lastError() const {
346 return last_error_;
347}
348
349// =============================================================================
350// Thread Information
351// =============================================================================
352
353std::vector<ThreadInfo> LLDBController::getThreads() const {
354 std::vector<ThreadInfo> result;
355
356 if (!process_.IsValid()) {
357 return result;
358 }
359
360 // Sync state to ensure we have fresh thread info
361 syncProcessState();
362
363 lldb::SBThread selected = process_.GetSelectedThread();
364 uint64_t selected_id = selected.IsValid() ? selected.GetThreadID() : 0;
365
366 uint32_t num_threads = process_.GetNumThreads();
367 result.reserve(num_threads);
368
369 for (uint32_t i = 0; i < num_threads; i++) {
370 lldb::SBThread thread = process_.GetThreadAtIndex(i);
371 if (thread.IsValid()) {
372 bool is_selected = (thread.GetThreadID() == selected_id);
373 result.push_back(threadToInfo(thread, is_selected));
374 }
375 }
376
377 return result;
378}
379
380std::optional<ThreadInfo> LLDBController::getSelectedThread() const {
381 lldb::SBThread thread = getSelectedThreadInternal();
382 if (!thread.IsValid()) {
383 return std::nullopt;
384 }
385 return threadToInfo(thread, true);
386}
387
389 if (!process_.IsValid()) {
390 setError("No valid process");
391 return false;
392 }
393
394 uint32_t num_threads = process_.GetNumThreads();
395 for (uint32_t i = 0; i < num_threads; i++) {
396 lldb::SBThread thread = process_.GetThreadAtIndex(i);
397 if (thread.IsValid() && thread.GetThreadID() == thread_id) {
398 process_.SetSelectedThread(thread);
399 clearError();
400 return true;
401 }
402 }
403
404 setError("Thread ID " + std::to_string(thread_id) + " not found");
405 return false;
406}
407
408// =============================================================================
409// Stack Frames
410// =============================================================================
411
412std::vector<FrameInfo> LLDBController::getFrames(uint32_t max_frames) const {
413 std::vector<FrameInfo> result;
414
415 lldb::SBThread thread = getSelectedThreadInternal();
416 if (!thread.IsValid()) {
417 return result;
418 }
419
420 uint32_t num_frames = thread.GetNumFrames();
421 if (max_frames > 0 && num_frames > max_frames) {
422 num_frames = max_frames;
423 }
424
425 result.reserve(num_frames);
426 for (uint32_t i = 0; i < num_frames; i++) {
427 lldb::SBFrame frame = thread.GetFrameAtIndex(i);
428 if (frame.IsValid()) {
429 result.push_back(frameToInfo(frame));
430 }
431 }
432
433 return result;
434}
435
436std::optional<FrameInfo> LLDBController::getFrame(uint32_t frame_index) const {
437 lldb::SBFrame frame = getFrameInternal(frame_index);
438 if (!frame.IsValid()) {
439 return std::nullopt;
440 }
441 return frameToInfo(frame);
442}
443
444// =============================================================================
445// Variable Reading
446// =============================================================================
447
448std::optional<VariableInfo> LLDBController::readVariable(const std::string &name, uint32_t frame_index, uint32_t expand_depth) const {
449 lldb::SBFrame frame = getFrameInternal(frame_index);
450 if (!frame.IsValid()) {
451 return std::nullopt;
452 }
453
454 // Try to find the variable
455 // First, check if it's a path expression (has dots or arrows)
456 lldb::SBValue value;
457
458 if (name.find('.') != std::string::npos || name.find("->") != std::string::npos || name.find('[') != std::string::npos) {
459 // Path expression - use GetValueForVariablePath
460 value = frame.GetValueForVariablePath(name.c_str());
461 } else {
462 // Simple name - try FindVariable first
463 value = frame.FindVariable(name.c_str());
464
465 // If not found, try as a register
466 if (!value.IsValid()) {
467 value = frame.FindRegister(name.c_str());
468 }
469 }
470
471 if (!value.IsValid()) {
472 return std::nullopt;
473 }
474
475 return valueToInfo(value, expand_depth);
476}
477
478std::vector<VariableInfo> LLDBController::listVariables(uint32_t frame_index, bool include_args, bool include_locals, bool include_statics) const {
479 std::vector<VariableInfo> result;
480
481 lldb::SBFrame frame = getFrameInternal(frame_index);
482 if (!frame.IsValid()) {
483 return result;
484 }
485
486 lldb::SBValueList vars = frame.GetVariables(include_args, include_locals, include_statics,
487 true); // true = in_scope_only
488
489 uint32_t num_vars = vars.GetSize();
490 result.reserve(num_vars);
491
492 for (uint32_t i = 0; i < num_vars; i++) {
493 lldb::SBValue value = vars.GetValueAtIndex(i);
494 if (value.IsValid()) {
495 result.push_back(valueToInfo(value, 0)); // Don't expand by default
496 }
497 }
498
499 return result;
500}
501
502// =============================================================================
503// Breakpoints
504// =============================================================================
505
506int32_t LLDBController::setBreakpoint(const std::string &file, uint32_t line, const std::string &condition) {
507 if (!target_.IsValid()) {
508 setError("No valid target");
509 return -1;
510 }
511
512 lldb::SBBreakpoint bp = target_.BreakpointCreateByLocation(file.c_str(), line);
513 if (!bp.IsValid()) {
514 setError("Failed to create breakpoint at " + file + ":" + std::to_string(line));
515 return -1;
516 }
517
518 if (!condition.empty()) {
519 bp.SetCondition(condition.c_str());
520 }
521
522 clearError();
523 return static_cast<int32_t>(bp.GetID());
524}
525
526bool LLDBController::removeBreakpoint(int32_t breakpoint_id) {
527 if (!target_.IsValid()) {
528 setError("No valid target");
529 return false;
530 }
531
532 bool result = target_.BreakpointDelete(static_cast<lldb::break_id_t>(breakpoint_id));
533 if (!result) {
534 setError("Failed to delete breakpoint " + std::to_string(breakpoint_id));
535 } else {
536 clearError();
537 }
538 return result;
539}
540
541std::vector<BreakpointInfo> LLDBController::getBreakpoints() const {
542 std::vector<BreakpointInfo> result;
543
544 if (!target_.IsValid()) {
545 return result;
546 }
547
548 uint32_t num_breakpoints = target_.GetNumBreakpoints();
549 result.reserve(num_breakpoints);
550
551 for (uint32_t i = 0; i < num_breakpoints; i++) {
552 lldb::SBBreakpoint bp = target_.GetBreakpointAtIndex(i);
553 if (bp.IsValid()) {
554 result.push_back(breakpointToInfo(bp));
555 }
556 }
557
558 return result;
559}
560
561std::optional<BreakpointInfo> LLDBController::getBreakpoint(int32_t breakpoint_id) const {
562 if (!target_.IsValid()) {
563 return std::nullopt;
564 }
565
566 lldb::SBBreakpoint bp = target_.FindBreakpointByID(static_cast<lldb::break_id_t>(breakpoint_id));
567 if (!bp.IsValid()) {
568 return std::nullopt;
569 }
570
571 return breakpointToInfo(bp);
572}
573
575 if (!process_.IsValid()) {
576 setError("No valid process");
577 return false;
578 }
579
580 // First, resume the process if it's stopped
581 if (state() == ProcessState::Stopped) {
582 if (!resume()) {
583 return false;
584 }
585 }
586
587 // Wait for process to stop
588 lldb::SBEvent event;
589 uint32_t timeout_sec = (timeout_ms + 999) / 1000; // Round up to seconds
590
591 while (true) {
592 bool got_event = listener_.WaitForEvent(timeout_sec, event);
593 if (!got_event) {
594 setError("Timeout waiting for breakpoint");
595 return false;
596 }
597
598 lldb::StateType new_state = lldb::SBProcess::GetStateFromEvent(event);
599 if (new_state == lldb::eStateStopped) {
600 clearError();
601 return true;
602 } else if (new_state == lldb::eStateExited || new_state == lldb::eStateCrashed || new_state == lldb::eStateDetached) {
603 setError("Process exited/crashed while waiting for breakpoint");
604 return false;
605 }
606 }
607}
608
609// =============================================================================
610// Expression Evaluation
611// =============================================================================
612
613std::optional<VariableInfo> LLDBController::evaluateExpression(const std::string &expression, uint32_t frame_index) const {
614 lldb::SBFrame frame = getFrameInternal(frame_index);
615 if (!frame.IsValid()) {
616 return std::nullopt;
617 }
618
619 lldb::SBExpressionOptions options;
620 options.SetIgnoreBreakpoints(true);
621 options.SetFetchDynamicValue(lldb::eDynamicDontRunTarget);
622
623 lldb::SBValue result = frame.EvaluateExpression(expression.c_str(), options);
624 if (!result.IsValid()) {
625 return std::nullopt;
626 }
627
628 lldb::SBError error = result.GetError();
629 if (error.Fail()) {
630 return std::nullopt;
631 }
632
633 return valueToInfo(result, 0);
634}
635
636// =============================================================================
637// Private Helpers
638// =============================================================================
639
640void LLDBController::setError(const std::string &msg) const {
641 last_error_ = msg;
642}
643
644void LLDBController::clearError() const {
645 last_error_.clear();
646}
647
648void LLDBController::syncProcessState() const {
649 if (!listener_.IsValid() || !process_.IsValid()) {
650 return;
651 }
652
653 // Drain all pending events to synchronize LLDB's internal state
654 lldb::SBEvent event;
655 while (listener_.PeekAtNextEvent(event)) {
656 listener_.GetNextEvent(event);
657 }
658}
659
660bool LLDBController::waitForState(lldb::StateType target_state, uint32_t timeout_sec) const {
661 if (!listener_.IsValid() || !process_.IsValid()) {
662 return false;
663 }
664
665 // Check if already in target state
666 lldb::StateType current = process_.GetState();
667 if (current == target_state) {
668 return true;
669 }
670
671 // Wait for state change event
672 lldb::SBEvent event;
673 auto start = std::chrono::steady_clock::now();
674
675 while (true) {
676 // Check timeout
677 auto elapsed = std::chrono::steady_clock::now() - start;
678 auto elapsed_sec = std::chrono::duration_cast<std::chrono::seconds>(elapsed).count();
679 if (elapsed_sec >= timeout_sec) {
680 return false;
681 }
682
683 // Wait for event with 1 second timeout
684 bool got_event = listener_.WaitForEvent(1, event);
685 if (got_event) {
686 lldb::StateType new_state = lldb::SBProcess::GetStateFromEvent(event);
687 if (new_state == target_state) {
688 return true;
689 }
690 // Check for terminal states
691 if (new_state == lldb::eStateExited || new_state == lldb::eStateCrashed || new_state == lldb::eStateDetached) {
692 return false;
693 }
694 }
695
696 // Re-check process state (might have been updated)
697 current = process_.GetState();
698 if (current == target_state) {
699 return true;
700 }
701 }
702}
703
704lldb::SBThread LLDBController::getSelectedThreadInternal() const {
705 if (!process_.IsValid()) {
706 return lldb::SBThread();
707 }
708 return process_.GetSelectedThread();
709}
710
711lldb::SBFrame LLDBController::getFrameInternal(uint32_t index) const {
712 lldb::SBThread thread = getSelectedThreadInternal();
713 if (!thread.IsValid()) {
714 return lldb::SBFrame();
715 }
716 return thread.GetFrameAtIndex(index);
717}
718
719ThreadInfo LLDBController::threadToInfo(lldb::SBThread &thread, bool is_selected) {
720 ThreadInfo info;
721 info.id = thread.GetThreadID();
722 info.index = thread.GetIndexID();
723 info.is_selected = is_selected;
724
725 const char *name = thread.GetName();
726 info.name = name ? name : "";
727
728 // Get stop reason
729 lldb::StopReason stop_reason = thread.GetStopReason();
730 switch (stop_reason) {
731 case lldb::eStopReasonNone:
732 info.stop_reason = "none";
733 break;
734 case lldb::eStopReasonBreakpoint:
735 info.stop_reason = "breakpoint";
736 break;
737 case lldb::eStopReasonWatchpoint:
738 info.stop_reason = "watchpoint";
739 break;
740 case lldb::eStopReasonSignal:
741 info.stop_reason = "signal";
742 break;
743 case lldb::eStopReasonException:
744 info.stop_reason = "exception";
745 break;
746 case lldb::eStopReasonPlanComplete:
747 info.stop_reason = "step_complete";
748 break;
749 default:
750 info.stop_reason = "other";
751 break;
752 }
753
754 // Get current frame info
755 lldb::SBFrame frame = thread.GetFrameAtIndex(0);
756 if (frame.IsValid()) {
757 const char *func_name = frame.GetFunctionName();
758 info.function = func_name ? func_name : "";
759
760 lldb::SBLineEntry line_entry = frame.GetLineEntry();
761 if (line_entry.IsValid()) {
762 lldb::SBFileSpec file_spec = line_entry.GetFileSpec();
763 if (file_spec.IsValid()) {
764 const char *filename = file_spec.GetFilename();
765 info.file = filename ? filename : "";
766 }
767 info.line = line_entry.GetLine();
768 }
769 }
770
771 return info;
772}
773
774FrameInfo LLDBController::frameToInfo(lldb::SBFrame &frame) {
775 FrameInfo info;
776 info.index = frame.GetFrameID();
777 info.pc = frame.GetPC();
778 info.fp = frame.GetFP();
779
780 const char *func_name = frame.GetFunctionName();
781 info.function = func_name ? func_name : "";
782
783 lldb::SBLineEntry line_entry = frame.GetLineEntry();
784 if (line_entry.IsValid()) {
785 lldb::SBFileSpec file_spec = line_entry.GetFileSpec();
786 if (file_spec.IsValid()) {
787 const char *filename = file_spec.GetFilename();
788 info.file = filename ? filename : "";
789 }
790 info.line = line_entry.GetLine();
791 }
792
793 return info;
794}
795
796VariableInfo LLDBController::valueToInfo(lldb::SBValue value, uint32_t expand_depth) const {
797 VariableInfo info;
798 info.is_valid = value.IsValid();
799
800 if (!info.is_valid) {
801 return info;
802 }
803
804 const char *name = value.GetName();
805 info.name = name ? name : "";
806
807 lldb::SBType type = value.GetType();
808 if (type.IsValid()) {
809 const char *type_name = type.GetName();
810 info.type = type_name ? type_name : "";
811 info.size = type.GetByteSize();
812 info.is_pointer = type.IsPointerType();
813 info.is_aggregate = type.IsAggregateType();
814 }
815
816 // Get value
817 const char *val_str = value.GetValue();
818 info.value = val_str ? val_str : "";
819
820 // Get summary (for strings, etc.)
821 const char *summary = value.GetSummary();
822 info.summary = summary ? summary : "";
823
824 // Get address
825 info.address = value.GetLoadAddress();
826
827 // Check for errors
828 lldb::SBError error = value.GetError();
829 if (error.Fail()) {
830 info.is_valid = false;
831 }
832
833 // Expand children if requested
834 if (expand_depth > 0 && info.is_aggregate) {
835 uint32_t num_children = value.GetNumChildren();
836 // Limit to reasonable number of children
837 if (num_children > 100) {
838 num_children = 100;
839 }
840
841 info.children.reserve(num_children);
842 for (uint32_t i = 0; i < num_children; i++) {
843 lldb::SBValue child = value.GetChildAtIndex(i);
844 if (child.IsValid()) {
845 info.children.push_back(valueToInfo(child, expand_depth - 1));
846 }
847 }
848 }
849
850 return info;
851}
852
853BreakpointInfo LLDBController::breakpointToInfo(lldb::SBBreakpoint bp) {
854 BreakpointInfo info;
855 info.id = static_cast<int32_t>(bp.GetID());
856 info.enabled = bp.IsEnabled();
857 info.hit_count = bp.GetHitCount();
858 info.line = 0; // Initialize to 0 for unresolved breakpoints
859 info.file = ""; // Initialize to empty for unresolved breakpoints
860
861 const char *condition = bp.GetCondition();
862 info.condition = condition ? condition : "";
863
864 // Get location info from first location
865 info.resolved = (bp.GetNumResolvedLocations() > 0);
866
867 if (bp.GetNumLocations() > 0) {
868 lldb::SBBreakpointLocation loc = bp.GetLocationAtIndex(0);
869 if (loc.IsValid()) {
870 lldb::SBAddress addr = loc.GetAddress();
871 if (addr.IsValid()) {
872 lldb::SBLineEntry line_entry = addr.GetLineEntry();
873 if (line_entry.IsValid()) {
874 lldb::SBFileSpec file_spec = line_entry.GetFileSpec();
875 if (file_spec.IsValid()) {
877 file_spec.GetPath(path, sizeof(path));
878 info.file = path;
879 }
880 info.line = line_entry.GetLine();
881 }
882 }
883 }
884 }
885
886 return info;
887}
888
889} // namespace ascii_query
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.
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.
bool stop()
Stop the target process.
bool isAttached() const
Check if attached to a process.
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
unsigned long long uint64_t
#define TOOLING_QUERY_LLDB_CONTROLLER_PATH_BUFFER_SIZE
LLDB process attachment and control wrapper.
ProcessState
This is the handoff point for ThreadInfo. Callers fill its members before passing it on; consumers re...
@ 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.).
static bool size_t index
Definition queries.h:8
Breakpoint information.
int32_t id
LLDB breakpoint ID.
Stack frame information.
uint32_t index
Frame index (0 = innermost).
Thread information.
uint64_t id
Thread ID.
Variable information (from LLDB SBValue).
bool is_valid
Whether value could be read.