-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Stateful variable-location annotations in Disassembler::PrintInstructions() (follow-up to #147460) #152887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
UltimateForce21
wants to merge
43
commits into
llvm:main
Choose a base branch
from
UltimateForce21:live-state-disassmbler-annotations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+312
−94
Open
Stateful variable-location annotations in Disassembler::PrintInstructions() (follow-up to #147460) #152887
Changes from 42 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
8ed8c54
[lldb] Add DWARFExpressionEntry and GetExpressionEntryAtAddress() to …
UltimateForce21 1db5002
Update lldb/include/lldb/Expression/DWARFExpressionList.h
UltimateForce21 a26010b
Update lldb/include/lldb/Expression/DWARFExpressionList.h
UltimateForce21 72237b7
Update lldb/source/Expression/DWARFExpressionList.cpp
UltimateForce21 94e4951
Update DWARFExpressionList.h
UltimateForce21 e8142da
Update DWARFExpressionList.cpp
UltimateForce21 7e8741e
Update DWARFExpressionList.h
UltimateForce21 c4cd77f
Update DWARFExpressionList.cpp
UltimateForce21 62c02a9
Change GetExpressionEntryAtAddress to return std::optional instead of…
UltimateForce21 d015971
Update DWARFExpressionList.cpp
UltimateForce21 60898ea
Add underflow/overflow checks to GetExpressionEntryAtAddressi
UltimateForce21 3462165
Make file_range optional in DWARFExpressionEntry for always-valid expr
UltimateForce21 2ed8443
Annotate Instruction::Dump() with DWARF variable locations
UltimateForce21 8c6b22d
Added Initial Basic API test for rich variable annotation in disassem…
UltimateForce21 842a9e5
Improved DWARF variable annotation printing and alignment
UltimateForce21 2fa6d24
Filter out partial DWARF decoding errors from disassembly annotations
UltimateForce21 6bbc8aa
Ignore annotations with only decoding errors
UltimateForce21 cbbc924
Add tests for disassembly variable annotations and decoding edge cases
UltimateForce21 b887db2
Rebase disassembler annotations branch onto updated DWARFExpressionEn…
UltimateForce21 912ba6d
Add `PrintRegisterOnly` flag in `struct DIDumpOptions` and created ne…
UltimateForce21 09c4d04
Add high-level comment explaining rich disassembly annotation logic i…
UltimateForce21 6e17f77
Add comment clarifying annotation column length check in Instruction:…
UltimateForce21 31431c0
Refactor variable annotation logic in `Instruction::Dump` using `anno…
UltimateForce21 9c5cb8f
Use range-based for loop for variable list iteration in Instruction::…
UltimateForce21 ca8510c
Consolidated DumpLocation and DumpLocationWithOptions using default D…
UltimateForce21 ffefe5f
Use `llvm::join` to simplify annotation output formatting
UltimateForce21 fae745a
Merge branch 'main' into add-disassembler-annotations
UltimateForce21 dcddf16
Fix formatting to match LLVM style
UltimateForce21 7bac074
More formatting fixes
UltimateForce21 79c0a9e
Fix formatting for code and tests
UltimateForce21 c7f1b30
Ported annotations from Instruction::Dump to Disassembler::PrintInstr…
UltimateForce21 3d19b02
Added `--rich` option for disassembler annotations and updated SBFram…
UltimateForce21 6ca4bb6
Formatting changes.
UltimateForce21 4bf584e
Merge branch 'main' into add-disassembler-annotations
UltimateForce21 b1f13e7
Redo Workflow tests
UltimateForce21 7b526fc
Updated from add-disassembler-annotations to include enable_annotatio…
UltimateForce21 10fddc4
Added basic stateful variable location annotations to disassembly output
UltimateForce21 b784868
Formatting changes.
UltimateForce21 cb0cd3a
Moved rich annotations flag into Disassembler options
UltimateForce21 fbd4e65
Switched to llvm::SmallDenseMap for live_vars in PrintInstructions
UltimateForce21 77fa1ed
Fixed code style to match LLVM convention
UltimateForce21 7069b6a
Formatting changes.
UltimateForce21 212a401
Switched rich annotations CLI flag from -R (--rich) to -v (--variable)
UltimateForce21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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">, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Desc<"Enable rich disassembly annotations for this invocation.">; | ||||||
} | ||||||
|
||||||
let Command = "diagnostics dump" in { | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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" | ||||||
|
@@ -41,6 +44,7 @@ | |||||
#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/Support/Compiler.h" | ||||||
#include "llvm/TargetParser/Triple.h" | ||||||
|
||||||
|
@@ -376,6 +380,147 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch, | |||||
} | ||||||
} | ||||||
|
||||||
// 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. | ||||||
|
||||||
struct VarState { | ||||||
std::string name; //< Display name. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
std::string last_loc; //< Last printed location (empty means <undef>). | ||||||
bool seen_this_inst = false; | ||||||
}; | ||||||
|
||||||
// Track live variables across instructions (keyed by stable LLDB user_id_t. 8 | ||||||
// is a good small-buffer guess. | ||||||
llvm::SmallDenseMap<lldb::user_id_t, VarState, 8> live_vars; | ||||||
|
||||||
// Stateful annotator: updates live_vars and returns only what should be | ||||||
// printed for THIS instruction. | ||||||
auto annotate_variables = [&](Instruction &inst) -> std::vector<std::string> { | ||||||
std::vector<std::string> events; | ||||||
|
||||||
StackFrame *frame = exe_ctx.GetFramePtr(); | ||||||
TargetSP target_sp = exe_ctx.GetTargetSP(); | ||||||
ProcessSP process_sp = exe_ctx.GetProcessSP(); | ||||||
if (!frame || !target_sp || !process_sp) | ||||||
return events; | ||||||
|
||||||
// Reset "seen" flags for this instruction. | ||||||
for (auto &kv : live_vars) | ||||||
kv.second.seen_this_inst = false; | ||||||
|
||||||
addr_t current_pc = inst.GetAddress().GetLoadAddress(target_sp.get()); | ||||||
addr_t original_pc = | ||||||
frame->GetFrameCodeAddress().GetLoadAddress(target_sp.get()); | ||||||
|
||||||
// We temporarily move the frame PC so variable locations resolve at this | ||||||
// instruction. | ||||||
if (!frame->ChangePC(current_pc)) | ||||||
return events; | ||||||
|
||||||
VariableListSP var_list_sp = frame->GetInScopeVariableList(true); | ||||||
if (!var_list_sp) { | ||||||
// No variables in scope: 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); | ||||||
} | ||||||
frame->ChangePC(original_pc); | ||||||
return events; | ||||||
} | ||||||
|
||||||
SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction); | ||||||
addr_t func_load_addr = | ||||||
sc.function ? sc.function->GetAddress().GetLoadAddress(target_sp.get()) | ||||||
: LLDB_INVALID_ADDRESS; | ||||||
|
||||||
// Walk all in-scope variables and try to resolve a location. | ||||||
for (const VariableSP &var_sp : *var_list_sp) { | ||||||
if (!var_sp) | ||||||
continue; | ||||||
|
||||||
// The var_id is a lldb::user_id_t – stable key. | ||||||
const auto var_id = var_sp->GetID(); | ||||||
const char *name_cstr = var_sp->GetName().AsCString(); | ||||||
llvm::StringRef name = name_cstr ? name_cstr : "<anon>"; | ||||||
|
||||||
auto &expr_list = var_sp->LocationExpressionList(); | ||||||
if (!expr_list.IsValid()) | ||||||
continue; | ||||||
|
||||||
auto entry_or_err = | ||||||
expr_list.GetExpressionEntryAtAddress(func_load_addr, current_pc); | ||||||
if (!entry_or_err) | ||||||
continue; | ||||||
|
||||||
auto entry = *entry_or_err; | ||||||
|
||||||
// Check range if present. | ||||||
if (entry.file_range && | ||||||
!entry.file_range->ContainsFileAddress( | ||||||
(current_pc - func_load_addr) + expr_list.GetFuncFileAddress())) | ||||||
continue; | ||||||
|
||||||
// Render a compact location string. | ||||||
ABI *abi = process_sp->GetABI().get(); | ||||||
llvm::DIDumpOptions opts; | ||||||
opts.ShowAddresses = false; | ||||||
opts.PrintRegisterOnly = true; | ||||||
|
||||||
StreamString loc_str; | ||||||
entry.expr->DumpLocation(&loc_str, eDescriptionLevelBrief, abi, opts); | ||||||
llvm::StringRef loc_clean = llvm::StringRef(loc_str.GetString()).trim(); | ||||||
if (loc_clean.empty()) | ||||||
continue; | ||||||
|
||||||
auto insert_res = | ||||||
live_vars.insert({var_id, VarState{std::string(name), loc_clean.str(), | ||||||
/*seen_this_inst*/ true}}); | ||||||
if (insert_res.second) { | ||||||
// Newly inserted → print. | ||||||
events.push_back(llvm::formatv("{0} = {1}", name, loc_clean).str()); | ||||||
} else { | ||||||
// Already present. | ||||||
VarState &vs = insert_res.first->second; | ||||||
vs.seen_this_inst = true; | ||||||
if (vs.last_loc != loc_clean) { | ||||||
vs.last_loc = loc_clean.str(); | ||||||
events.push_back( | ||||||
llvm::formatv("{0} = {1}", vs.name, loc_clean).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); | ||||||
} | ||||||
} | ||||||
|
||||||
// Restore PC. | ||||||
frame->ChangePC(original_pc); | ||||||
return events; | ||||||
}; | ||||||
|
||||||
previous_symbol = nullptr; | ||||||
SourceLine previous_line; | ||||||
for (size_t i = 0; i < num_instructions_found; ++i) { | ||||||
|
@@ -540,10 +685,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 & eOptionRichAnnotations) { | ||||||
std::vector<std::string> annotations = annotate_variables(*inst); | ||||||
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; | ||||||
} | ||||||
|
@@ -724,9 +885,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. | ||||||
|
@@ -1073,10 +1232,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; | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eOptionVariableAnnotations