Skip to content
Closed
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
62 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
c7f1b30
Ported annotations from Instruction::Dump to Disassembler::PrintInstr…
UltimateForce21 Aug 7, 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
7b526fc
Updated from add-disassembler-annotations to include enable_annotatio…
UltimateForce21 Aug 9, 2025
10fddc4
Added basic stateful variable location annotations to disassembly output
UltimateForce21 Aug 9, 2025
b784868
Formatting changes.
UltimateForce21 Aug 10, 2025
cb0cd3a
Moved rich annotations flag into Disassembler options
UltimateForce21 Aug 11, 2025
fbd4e65
Switched to llvm::SmallDenseMap for live_vars in PrintInstructions
UltimateForce21 Aug 11, 2025
77fa1ed
Fixed code style to match LLVM convention
UltimateForce21 Aug 12, 2025
7069b6a
Formatting changes.
UltimateForce21 Aug 12, 2025
212a401
Switched rich annotations CLI flag from -R (--rich) to -v (--variable)
UltimateForce21 Aug 15, 2025
d234d3e
Update lldb/source/Core/Disassembler.cpp
UltimateForce21 Aug 16, 2025
e282782
Make variable annotations work offline
UltimateForce21 Aug 18, 2025
0b71f82
Added portable disassembler variable tests across register cases
UltimateForce21 Aug 20, 2025
9b3f552
Add stable assembler-based tests for rich disassembler annotations
UltimateForce21 Aug 22, 2025
75e6b44
Formatting updates
UltimateForce21 Aug 22, 2025
e5e168f
Formatting change
UltimateForce21 Aug 22, 2025
cc77627
Moved the building of assembly source code files to object files to M…
UltimateForce21 Aug 25, 2025
51e27b6
Merge branch 'main' into live-state-disassmbler-annotations
UltimateForce21 Aug 25, 2025
c41b79c
Show constants in rich disassembler annotations when using PrintRegis…
UltimateForce21 Aug 25, 2025
11d85ea
Update lldb/source/Core/Disassembler.cpp
UltimateForce21 Aug 27, 2025
c7d489b
Changed all naming `rich` annotations for variables to `variable`.
UltimateForce21 Aug 27, 2025
a82957c
Removed executable that was added by accident for test cases.
UltimateForce21 Aug 27, 2025
2cda69c
Changed name of tester for TestRichDisassembler to TestVariableAnnota…
UltimateForce21 Aug 27, 2025
6b24fff
Update lldb/source/Core/Disassembler.cpp
UltimateForce21 Aug 27, 2025
cc0aee9
Update lldb/source/Commands/CommandObjectDisassemble.cpp
UltimateForce21 Aug 27, 2025
e1d0f91
Switched live_vars from llvm::SmallDenseMap to llvm::DenseMap.
UltimateForce21 Aug 27, 2025
c0babd5
Merge branch 'main' into live-state-disassmbler-annotations
UltimateForce21 Aug 27, 2025
ad70817
Skip TestVariableAnnotationsDisassembler.py on non-x86 architectures
UltimateForce21 Aug 28, 2025
48481f7
Merge branch 'main' into live-state-disassmbler-annotations
UltimateForce21 Aug 28, 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
18 changes: 9 additions & 9 deletions lldb/include/lldb/Core/Disassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,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 +282,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 +316,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 @@ -399,6 +398,7 @@ class Disassembler : public std::enable_shared_from_this<Disassembler>,
eOptionMarkPCAddress =
(1u << 3), // Mark the disassembly line the contains the PC
eOptionShowControlFlowKind = (1u << 4),
eOptionVariableAnnotations = (1u << 5),
};

enum HexImmediateStyle {
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
8 changes: 8 additions & 0 deletions 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 'v':
enable_variable_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_variable_annotations = false;
plugin_name.clear();

Target *target =
Expand Down Expand Up @@ -529,6 +534,9 @@ void CommandObjectDisassemble::DoExecute(Args &command,
if (m_options.raw)
options |= Disassembler::eOptionRawOuput;

if (m_options.enable_variable_annotations)
options |= Disassembler::eOptionVariableAnnotations;

llvm::Expected<std::vector<AddressRange>> ranges =
GetRangesForSelectedMode(result);
if (!ranges) {
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_variable_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 @@ -366,6 +366,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_variable : Option<"variable", "v">,
Desc<"Enable variable disassembly annotations for this invocation.">;
}

let Command = "diagnostics dump" in {
Expand Down
175 changes: 167 additions & 8 deletions lldb/source/Core/Disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
#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/ABI.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 All @@ -41,6 +45,8 @@
#include "lldb/lldb-private-enumerations.h"
#include "lldb/lldb-private-interfaces.h"
#include "lldb/lldb-private-types.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
#include "llvm/TargetParser/Triple.h"

Expand Down Expand Up @@ -376,6 +382,147 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch,
}
}

// Add 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.

struct VarState {
std::string name; ///< Display name.
std::string last_loc; ///< Last printed location (empty means <undef>).
bool seen_this_inst = false;
};

// Track live variables across instructions.
llvm::DenseMap<lldb::user_id_t, VarState> live_vars;

// Stateful annotator: updates live_vars and returns only what should be
// printed for THIS instruction.
auto annotate_static = [&](Instruction &inst, Target &target,
ModuleSP module_sp) -> std::vector<std::string> {
std::vector<std::string> events;

// Reset per-instruction seen flags.
for (auto &kv : live_vars)
kv.second.seen_this_inst = false;

const Address &iaddr = inst.GetAddress();
if (!module_sp) {
// Everything previously live becomes <undef>.
for (auto I = live_vars.begin(), E = live_vars.end(); I != E;) {
auto Cur = I++;
events.push_back(
llvm::formatv("{0} = <undef>", Cur->second.name).str());
live_vars.erase(Cur);
}
return events;
}

// Resolve innermost block at this *file* address.
SymbolContext sc;
const lldb::SymbolContextItem mask =
eSymbolContextFunction | eSymbolContextBlock;
if (!module_sp->ResolveSymbolContextForAddress(iaddr, mask, sc) ||
!sc.function) {
// No function context: everything dies here.
for (auto I = live_vars.begin(), E = live_vars.end(); I != E;) {
auto Cur = I++;
events.push_back(
llvm::formatv("{0} = <undef>", Cur->second.name).str());
live_vars.erase(Cur);
}
return events;
}

Block *B = sc.block; ///< Innermost block containing iaddr.
VariableList var_list;
if (B) {
auto filter = [](Variable *v) -> bool { return v && !v->IsArtificial(); };

B->AppendVariables(/*can_create*/ true,
/*get_parent_variables*/ true,
/*stop_if_block_is_inlined_function*/ false,
/*filter*/ filter,
/*variable_list*/ &var_list);
}

const lldb::addr_t pc_file = iaddr.GetFileAddress();
const lldb::addr_t func_file = sc.function->GetAddress().GetFileAddress();

// ABI from Target (pretty reg names if plugin exists). Safe to be null.
lldb::ProcessSP no_process;
lldb::ABISP abi_sp = ABI::FindPlugin(no_process, target.GetArchitecture());
ABI *abi = abi_sp.get();

llvm::DIDumpOptions opts;
opts.ShowAddresses = false;
if (abi)
opts.PrintRegisterOnly = true;

for (size_t i = 0, e = var_list.GetSize(); i != e; ++i) {
lldb::VariableSP v = var_list.GetVariableAtIndex(i);
if (!v || v->IsArtificial())
continue;

const char *nm = v->GetName().AsCString();
llvm::StringRef name = nm ? nm : "<anon>";

lldb_private::DWARFExpressionList &exprs = v->LocationExpressionList();
if (!exprs.IsValid())
continue;

auto entry_or_err = exprs.GetExpressionEntryAtAddress(func_file, pc_file);
if (!entry_or_err)
continue;

auto entry = *entry_or_err;

StreamString loc_ss;
entry.expr->DumpLocation(&loc_ss, eDescriptionLevelBrief, abi, opts);
llvm::StringRef loc = llvm::StringRef(loc_ss.GetString()).trim();
if (loc.empty())
continue;

auto ins = live_vars.insert(
{v->GetID(), VarState{name.str(), loc.str(), /*seen*/ true}});
if (ins.second) {
// Newly live.
events.push_back(llvm::formatv("{0} = {1}", name, loc).str());
} else {
VarState &vs = ins.first->second;
vs.seen_this_inst = true;
if (vs.last_loc != loc) {
vs.last_loc = loc.str();
events.push_back(llvm::formatv("{0} = {1}", vs.name, loc).str());
}
}
}

// Anything previously live that we didn't see a location for at this inst
// is now <undef>.
for (auto I = live_vars.begin(), E = live_vars.end(); I != E;) {
auto Cur = I++;
if (!Cur->second.seen_this_inst) {
events.push_back(
llvm::formatv("{0} = <undef>", Cur->second.name).str());
live_vars.erase(Cur);
}
}

return events;
};

previous_symbol = nullptr;
SourceLine previous_line;
for (size_t i = 0; i < num_instructions_found; ++i) {
Expand Down Expand Up @@ -540,10 +687,26 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch,
const bool show_bytes = (options & eOptionShowBytes) != 0;
const bool show_control_flow_kind =
(options & eOptionShowControlFlowKind) != 0;
inst->Dump(&strm, max_opcode_byte_size, true, show_bytes,

StreamString inst_line;

inst->Dump(&inst_line, max_opcode_byte_size, true, show_bytes,
show_control_flow_kind, &exe_ctx, &sc, &prev_sc, nullptr,
address_text_size);

if ((options & eOptionVariableAnnotations) && target_sp) {
auto annotations = annotate_static(*inst, *target_sp, module_sp);
if (!annotations.empty()) {
const size_t annotation_column = 100;
inst_line.FillLastLineToColumn(annotation_column, ' ');
inst_line.PutCString("; ");
inst_line.PutCString(llvm::join(annotations, ", "));
}
}

strm.PutCString(inst_line.GetString());
strm.EOL();

} else {
break;
}
Expand Down Expand Up @@ -724,9 +887,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 +1234,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