Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8ed8c54
[lldb] Add DWARFExpressionEntry and GetExpressionEntryAtAddress() to …
UltimateForce21 Jun 11, 2025
1db5002
Update lldb/include/lldb/Expression/DWARFExpressionList.h
UltimateForce21 Jun 19, 2025
a26010b
Update lldb/include/lldb/Expression/DWARFExpressionList.h
UltimateForce21 Jun 19, 2025
72237b7
Update lldb/source/Expression/DWARFExpressionList.cpp
UltimateForce21 Jun 19, 2025
94e4951
Update DWARFExpressionList.h
UltimateForce21 Jun 24, 2025
e8142da
Update DWARFExpressionList.cpp
UltimateForce21 Jun 24, 2025
7e8741e
Update DWARFExpressionList.h
UltimateForce21 Jun 28, 2025
c4cd77f
Update DWARFExpressionList.cpp
UltimateForce21 Jun 28, 2025
62c02a9
Change GetExpressionEntryAtAddress to return std::optional instead of…
UltimateForce21 Jun 29, 2025
d015971
Update DWARFExpressionList.cpp
UltimateForce21 Jul 2, 2025
60898ea
Add underflow/overflow checks to GetExpressionEntryAtAddressi
UltimateForce21 Jul 3, 2025
3462165
Make file_range optional in DWARFExpressionEntry for always-valid expr
UltimateForce21 Jul 8, 2025
2ed8443
Annotate Instruction::Dump() with DWARF variable locations
UltimateForce21 Jul 3, 2025
8c6b22d
Added Initial Basic API test for rich variable annotation in disassem…
UltimateForce21 Jul 5, 2025
842a9e5
Improved DWARF variable annotation printing and alignment
UltimateForce21 Jul 6, 2025
2fa6d24
Filter out partial DWARF decoding errors from disassembly annotations
UltimateForce21 Jul 6, 2025
6bbc8aa
Ignore annotations with only decoding errors
UltimateForce21 Jul 6, 2025
cbbc924
Add tests for disassembly variable annotations and decoding edge cases
UltimateForce21 Jul 6, 2025
b887db2
Rebase disassembler annotations branch onto updated DWARFExpressionEn…
UltimateForce21 Jul 8, 2025
912ba6d
Add `PrintRegisterOnly` flag in `struct DIDumpOptions` and created ne…
UltimateForce21 Jul 9, 2025
09c4d04
Add high-level comment explaining rich disassembly annotation logic i…
UltimateForce21 Jul 20, 2025
6e17f77
Add comment clarifying annotation column length check in Instruction:…
UltimateForce21 Jul 20, 2025
31431c0
Refactor variable annotation logic in `Instruction::Dump` using `anno…
UltimateForce21 Jul 20, 2025
9c5cb8f
Use range-based for loop for variable list iteration in Instruction::…
UltimateForce21 Jul 20, 2025
ca8510c
Consolidated DumpLocation and DumpLocationWithOptions using default D…
UltimateForce21 Jul 20, 2025
ffefe5f
Use `llvm::join` to simplify annotation output formatting
UltimateForce21 Jul 20, 2025
fae745a
Merge branch 'main' into add-disassembler-annotations
UltimateForce21 Aug 4, 2025
dcddf16
Fix formatting to match LLVM style
UltimateForce21 Aug 4, 2025
7bac074
More formatting fixes
UltimateForce21 Aug 4, 2025
79c0a9e
Fix formatting for code and tests
UltimateForce21 Aug 5, 2025
3d19b02
Added `--rich` option for disassembler annotations and updated SBFram…
UltimateForce21 Aug 8, 2025
6ca4bb6
Formatting changes.
UltimateForce21 Aug 8, 2025
4bf584e
Merge branch 'main' into add-disassembler-annotations
UltimateForce21 Aug 8, 2025
b1f13e7
Redo Workflow tests
UltimateForce21 Aug 9, 2025
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
27 changes: 14 additions & 13 deletions lldb/include/lldb/Core/Disassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ class Instruction {
const SymbolContext *sym_ctx,
const SymbolContext *prev_sym_ctx,
const FormatEntity::Entry *disassembly_addr_format,
size_t max_address_text_size);
size_t max_address_text_size,
bool enable_rich_annotations = false);

virtual bool DoesBranch() = 0;

Expand All @@ -169,7 +170,7 @@ class Instruction {

virtual bool IsAuthenticated() = 0;

bool CanSetBreakpoint ();
bool CanSetBreakpoint();

virtual size_t Decode(const Disassembler &disassembler,
const DataExtractor &data,
Expand Down Expand Up @@ -282,7 +283,7 @@ std::function<bool(const Instruction::Operand &)> FetchImmOp(int64_t &imm);

std::function<bool(const Instruction::Operand &)>
MatchOpType(Instruction::Operand::Type type);
}
} // namespace OperandMatchers

class InstructionList {
public:
Expand Down Expand Up @@ -316,20 +317,19 @@ class InstructionList {
/// @param[in] ignore_calls
/// It true, then fine the first branch instruction that isn't
/// a function call (a branch that calls and returns to the next
/// instruction). If false, find the instruction index of any
/// instruction). If false, find the instruction index of any
/// branch in the list.
///
///
/// @param[out] found_calls
/// If non-null, this will be set to true if any calls were found in
/// If non-null, this will be set to true if any calls were found in
/// extending the range.
///
///
/// @return
/// The instruction index of the first branch that is at or past
/// \a start. Returns UINT32_MAX if no matching branches are
/// \a start. Returns UINT32_MAX if no matching branches are
/// found.
//------------------------------------------------------------------
uint32_t GetIndexOfNextBranchInstruction(uint32_t start,
bool ignore_calls,
uint32_t GetIndexOfNextBranchInstruction(uint32_t start, bool ignore_calls,
bool *found_calls) const;

uint32_t GetIndexOfInstructionAtLoadAddress(lldb::addr_t load_addr,
Expand Down Expand Up @@ -445,10 +445,11 @@ class Disassembler : public std::enable_shared_from_this<Disassembler>,
const ExecutionContext &exe_ctx, const Address &start,
Limit limit, bool mixed_source_and_assembly,
uint32_t num_mixed_context_lines, uint32_t options,
Stream &strm);
Stream &strm, bool enable_rich_annotations = false);

static bool Disassemble(Debugger &debugger, const ArchSpec &arch,
StackFrame &frame, Stream &strm);
StackFrame &frame, Stream &strm,
bool enable_rich_annotations = false);

// Constructors and Destructors
Disassembler(const ArchSpec &arch, const char *flavor);
Expand All @@ -458,7 +459,7 @@ class Disassembler : public std::enable_shared_from_this<Disassembler>,
const ExecutionContext &exe_ctx,
bool mixed_source_and_assembly,
uint32_t num_mixed_context_lines, uint32_t options,
Stream &strm);
Stream &strm, bool enable_rich_annotations = false);

size_t ParseInstructions(Target &target, Address address, Limit limit,
Stream *error_strm_ptr,
Expand Down
3 changes: 2 additions & 1 deletion lldb/include/lldb/Expression/DWARFExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ class DWARFExpression {
return data.GetByteSize() > 0;
}

void DumpLocation(Stream *s, lldb::DescriptionLevel level, ABI *abi) const;
void DumpLocation(Stream *s, lldb::DescriptionLevel level, ABI *abi,
llvm::DIDumpOptions options = {}) const;

bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op) const;

Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Target/StackFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class StackFrame : public ExecutionContextScope,
///
/// \return
/// C string with the assembly instructions for this function.
const char *Disassemble();
const char *Disassemble(bool enable_rich_annotations = false);

/// Print a description of this frame using the provided frame format.
///
Expand Down
8 changes: 7 additions & 1 deletion lldb/source/Commands/CommandObjectDisassemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ Status CommandObjectDisassemble::CommandOptions::SetOptionValue(
}
} break;

case 'R': // --rich
enable_rich_annotations = true;
break;

case '\x01':
force = true;
break;
Expand All @@ -180,6 +184,7 @@ void CommandObjectDisassemble::CommandOptions::OptionParsingStarting(
end_addr = LLDB_INVALID_ADDRESS;
symbol_containing_addr = LLDB_INVALID_ADDRESS;
raw = false;
enable_rich_annotations = false;
plugin_name.clear();

Target *target =
Expand Down Expand Up @@ -550,7 +555,8 @@ void CommandObjectDisassemble::DoExecute(Args &command,
cpu_string, features_string, m_exe_ctx, cur_range.GetBaseAddress(),
limit, m_options.show_mixed,
m_options.show_mixed ? m_options.num_lines_context : 0, options,
result.GetOutputStream())) {
result.GetOutputStream(),
/*enable_rich_annotations=*/m_options.enable_rich_annotations)) {
result.SetStatus(eReturnStatusSuccessFinishResult);
} else {
if (m_options.symbol_containing_addr != LLDB_INVALID_ADDRESS) {
Expand Down
1 change: 1 addition & 0 deletions lldb/source/Commands/CommandObjectDisassemble.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class CommandObjectDisassemble : public CommandObjectParsed {
// in SetOptionValue if anything the selects a location is set.
lldb::addr_t symbol_containing_addr = 0;
bool force = false;
bool enable_rich_annotations = false;
};

CommandObjectDisassemble(CommandInterpreter &interpreter);
Expand Down
2 changes: 2 additions & 0 deletions lldb/source/Commands/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ let Command = "disassemble" in {
Desc<"Disassemble function containing this address.">;
def disassemble_options_force : Option<"force", "\\x01">, Groups<[2,3,4,5,7]>,
Desc<"Force disassembly of large functions.">;
def disassemble_options_rich : Option<"rich", "R">,
Desc<"Enable rich disassembly annotations for this invocation.">;
}

let Command = "diagnostics dump" in {
Expand Down
143 changes: 121 additions & 22 deletions lldb/source/Core/Disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Target.h"
Expand Down Expand Up @@ -165,14 +168,12 @@ Disassembler::DisassembleBytes(const ArchSpec &arch, const char *plugin_name,
return disasm_sp;
}

bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
const char *plugin_name, const char *flavor,
const char *cpu, const char *features,
const ExecutionContext &exe_ctx,
const Address &address, Limit limit,
bool mixed_source_and_assembly,
uint32_t num_mixed_context_lines,
uint32_t options, Stream &strm) {
bool Disassembler::Disassemble(
Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
const char *flavor, const char *cpu, const char *features,
const ExecutionContext &exe_ctx, const Address &address, Limit limit,
bool mixed_source_and_assembly, uint32_t num_mixed_context_lines,
uint32_t options, Stream &strm, bool enable_rich_annotations) {
if (!exe_ctx.GetTargetPtr())
return false;

Expand All @@ -187,9 +188,10 @@ bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
if (bytes_disassembled == 0)
return false;

disasm_sp->PrintInstructions(debugger, arch, exe_ctx,
mixed_source_and_assembly,
num_mixed_context_lines, options, strm);
disasm_sp->PrintInstructions(
debugger, arch, exe_ctx, mixed_source_and_assembly,
num_mixed_context_lines, options, strm,
/*enable_rich_annotations=*/enable_rich_annotations);
return true;
}

Expand Down Expand Up @@ -284,7 +286,8 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch,
const ExecutionContext &exe_ctx,
bool mixed_source_and_assembly,
uint32_t num_mixed_context_lines,
uint32_t options, Stream &strm) {
uint32_t options, Stream &strm,
bool enable_rich_annotations) {
// We got some things disassembled...
size_t num_instructions_found = GetInstructionList().GetSize();

Expand Down Expand Up @@ -542,7 +545,7 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch,
(options & eOptionShowControlFlowKind) != 0;
inst->Dump(&strm, max_opcode_byte_size, true, show_bytes,
show_control_flow_kind, &exe_ctx, &sc, &prev_sc, nullptr,
address_text_size);
address_text_size, enable_rich_annotations);
strm.EOL();
} else {
break;
Expand All @@ -551,7 +554,8 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch,
}

bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
StackFrame &frame, Stream &strm) {
StackFrame &frame, Stream &strm,
bool enable_rich_annotations) {
constexpr const char *plugin_name = nullptr;
constexpr const char *flavor = nullptr;
constexpr const char *cpu = nullptr;
Expand Down Expand Up @@ -638,7 +642,8 @@ void Instruction::Dump(lldb_private::Stream *s, uint32_t max_opcode_byte_size,
const SymbolContext *sym_ctx,
const SymbolContext *prev_sym_ctx,
const FormatEntity::Entry *disassembly_addr_format,
size_t max_address_text_size) {
size_t max_address_text_size,
bool enable_rich_annotations) {
size_t opcode_column_width = 7;
const size_t operand_column_width = 25;

Expand Down Expand Up @@ -704,6 +709,104 @@ void Instruction::Dump(lldb_private::Stream *s, uint32_t max_opcode_byte_size,
ss.FillLastLineToColumn(opcode_pos + opcode_column_width, ' ');
ss.PutCString(mnemonics);

// Add rich variable location annotations to the disassembly output.
//
// For each instruction, this block attempts to resolve in-scope variables
// and determine if the current PC falls within their
// DWARF location entry. If so, it prints a simplified annotation using the
// variable name and its resolved location (e.g., "var = reg; " ).
//
// Annotations are only included if the variable has a valid DWARF location
// entry, and the location string is non-empty after filtering. Decoding
// errors and DWARF opcodes are intentionally omitted to keep the output
// concise and user-friendly.
//
// The goal is to give users helpful live variable hints alongside the
// disassembled instruction stream, similar to how debug information
// enhances source-level debugging.

const size_t annotation_column = 150;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JDevlieghere Question for a future PR: Should we consider threading through and checking the actual width of the Terminal to see if the annotations would fit?


auto annotate_variables = [&]() {
StackFrame *frame = exe_ctx->GetFramePtr();
TargetSP target_sp = exe_ctx->GetTargetSP();
if (!frame || !target_sp)
return;

addr_t current_pc = m_address.GetLoadAddress(target_sp.get());
addr_t original_pc =
frame->GetFrameCodeAddress().GetLoadAddress(target_sp.get());

if (!frame->ChangePC(current_pc))
return;

VariableListSP var_list_sp = frame->GetInScopeVariableList(true);
if (!var_list_sp)
return;

SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction);
addr_t func_load_addr = LLDB_INVALID_ADDRESS;
if (sc.function)
func_load_addr =
sc.function->GetAddress().GetLoadAddress(target_sp.get());

// Only annotate if the current disassembly line is short enough
// to keep annotations aligned past the desired annotation_column.
if (ss.GetSizeOfLastLine() >= annotation_column)
return;

std::vector<std::string> annotations;

for (const VariableSP &var_sp : *var_list_sp) {
if (!var_sp)
continue;

const char *name = var_sp->GetName().AsCString();
auto &expr_list = var_sp->LocationExpressionList();
if (!expr_list.IsValid())
continue;

// Handle std::optional<DWARFExpressionEntry>.
if (auto entryOrErr = expr_list.GetExpressionEntryAtAddress(
func_load_addr, current_pc)) {
auto entry = *entryOrErr;
// Check if entry has a file_range, and filter on address if so.
if (!entry.file_range || entry.file_range->ContainsFileAddress(
(current_pc - func_load_addr) +
expr_list.GetFuncFileAddress())) {

StreamString loc_str;
ABI *abi = exe_ctx->GetProcessPtr()->GetABI().get();
llvm::DIDumpOptions opts;
opts.ShowAddresses = false;
opts.PrintRegisterOnly =
true; // <-- important: suppress DW_OP_... annotations, etc.

entry.expr->DumpLocation(&loc_str, eDescriptionLevelBrief, abi, opts);

// Only include if not empty.
llvm::StringRef loc_clean =
llvm::StringRef(loc_str.GetString()).trim();
if (!loc_clean.empty()) {
annotations.push_back(llvm::formatv("{0} = {1}", name, loc_clean));
}
}
}
}

if (!annotations.empty()) {
ss.FillLastLineToColumn(annotation_column, ' ');
ss.PutCString(" ; ");
ss.PutCString(llvm::join(annotations, ", "));
}

frame->ChangePC(original_pc);
};

if (enable_rich_annotations && exe_ctx && exe_ctx->GetFramePtr()) {
annotate_variables();
}

if (!m_comment.empty()) {
ss.FillLastLineToColumn(
opcode_pos + opcode_column_width + operand_column_width, ' ');
Expand All @@ -724,9 +827,7 @@ bool Instruction::DumpEmulation(const ArchSpec &arch) {
return false;
}

bool Instruction::CanSetBreakpoint () {
return !HasDelaySlot();
}
bool Instruction::CanSetBreakpoint() { return !HasDelaySlot(); }

bool Instruction::HasDelaySlot() {
// Default is false.
Expand Down Expand Up @@ -1073,10 +1174,8 @@ void InstructionList::Append(lldb::InstructionSP &inst_sp) {
m_instructions.push_back(inst_sp);
}

uint32_t
InstructionList::GetIndexOfNextBranchInstruction(uint32_t start,
bool ignore_calls,
bool *found_calls) const {
uint32_t InstructionList::GetIndexOfNextBranchInstruction(
uint32_t start, bool ignore_calls, bool *found_calls) const {
size_t num_instructions = m_instructions.size();

uint32_t next_branch = UINT32_MAX;
Expand Down
8 changes: 4 additions & 4 deletions lldb/source/Expression/DWARFExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ void DWARFExpression::UpdateValue(uint64_t const_value,
}

void DWARFExpression::DumpLocation(Stream *s, lldb::DescriptionLevel level,
ABI *abi) const {
ABI *abi,
llvm::DIDumpOptions options) const {
auto *MCRegInfo = abi ? &abi->GetMCRegisterInfo() : nullptr;
auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum,
bool IsEH) -> llvm::StringRef {
Expand All @@ -79,10 +80,9 @@ void DWARFExpression::DumpLocation(Stream *s, lldb::DescriptionLevel level,
return llvm::StringRef(RegName);
return {};
};
llvm::DIDumpOptions DumpOpts;
DumpOpts.GetNameForDWARFReg = GetRegName;
options.GetNameForDWARFReg = GetRegName;
llvm::DWARFExpression E(m_data.GetAsLLVM(), m_data.GetAddressByteSize());
llvm::printDwarfExpression(&E, s->AsRawOstream(), DumpOpts, nullptr);
llvm::printDwarfExpression(&E, s->AsRawOstream(), options, nullptr);
}

RegisterKind DWARFExpression::GetRegisterKind() const { return m_reg_kind; }
Expand Down
Loading