Skip to content

Commit 31431c0

Browse files
Refactor variable annotation logic in Instruction::Dump using annotate_variable lambda function
1 parent 6e17f77 commit 31431c0

File tree

1 file changed

+70
-61
lines changed

1 file changed

+70
-61
lines changed

lldb/source/Core/Disassembler.cpp

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -723,75 +723,83 @@ void Instruction::Dump(lldb_private::Stream *s, uint32_t max_opcode_byte_size,
723723

724724
const size_t annotation_column = 150;
725725

726-
if (exe_ctx && exe_ctx->GetFramePtr()) {
726+
auto annotate_variables = [&]() {
727727
StackFrame *frame = exe_ctx->GetFramePtr();
728728
TargetSP target_sp = exe_ctx->GetTargetSP();
729-
if (frame && target_sp) {
730-
addr_t current_pc = m_address.GetLoadAddress(target_sp.get());
731-
addr_t original_pc = frame->GetFrameCodeAddress().GetLoadAddress(target_sp.get());
732-
if (frame->ChangePC(current_pc)) {
733-
VariableListSP var_list_sp = frame->GetInScopeVariableList(true);
734-
SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction);
735-
addr_t func_load_addr = LLDB_INVALID_ADDRESS;
736-
if (sc.function)
737-
func_load_addr = sc.function->GetAddress().GetLoadAddress(target_sp.get());
738-
739-
// Only annotate if the current disassembly line is short enough
740-
// to keep annotations aligned past the desired annotation_column.
741-
if(ss.GetSizeOfLastLine() < annotation_column) {
742-
743-
std::vector<std::string> annotations;
744-
745-
if (var_list_sp) {
746-
for (size_t i = 0; i < var_list_sp->GetSize(); ++i) {
747-
VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
748-
if (!var_sp)
749-
continue;
750-
751-
const char *name = var_sp->GetName().AsCString();
752-
auto &expr_list = var_sp->LocationExpressionList();
753-
if (!expr_list.IsValid())
754-
continue;
755-
// Handle std::optional<DWARFExpressionEntry>.
756-
if (auto entryOrErr = expr_list.GetExpressionEntryAtAddress(func_load_addr, current_pc)) {
757-
auto entry = *entryOrErr;
758-
759-
// Check if entry has a file_range, and filter on address if so.
760-
if (!entry.file_range || entry.file_range->ContainsFileAddress(
761-
(current_pc - func_load_addr) + expr_list.GetFuncFileAddress())) {
762-
763-
StreamString loc_str;
764-
ABI *abi = exe_ctx->GetProcessPtr()->GetABI().get();
765-
llvm::DIDumpOptions opts;
766-
opts.ShowAddresses = false;
767-
opts.PrintRegisterOnly = true; // <-- important: suppress DW_OP_... annotations, etc.
768-
769-
entry.expr->DumpLocationWithOptions(&loc_str, eDescriptionLevelBrief, abi, opts);
770-
771-
// Only include if not empty
772-
llvm::StringRef loc_clean = llvm::StringRef(loc_str.GetString()).trim();
773-
if (!loc_clean.empty()) {
774-
annotations.push_back(llvm::formatv("{0} = {1}", name, loc_clean));
775-
}
776-
}
777-
}
778-
}
729+
if (!frame || !target_sp)
730+
return;
779731

780-
if (!annotations.empty()) {
781-
ss.FillLastLineToColumn(annotation_column, ' ');
782-
ss.PutCString(" ; ");
783-
for (size_t i = 0; i < annotations.size(); ++i) {
784-
if (i > 0)
785-
ss.PutCString(", ");
786-
ss.PutCString(annotations[i]);
787-
}
788-
}
732+
addr_t current_pc = m_address.GetLoadAddress(target_sp.get());
733+
addr_t original_pc = frame->GetFrameCodeAddress().GetLoadAddress(target_sp.get());
734+
735+
if (!frame->ChangePC(current_pc))
736+
return;
737+
738+
VariableListSP var_list_sp = frame->GetInScopeVariableList(true);
739+
if (!var_list_sp)
740+
return;
741+
742+
SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction);
743+
addr_t func_load_addr = LLDB_INVALID_ADDRESS;
744+
if (sc.function)
745+
func_load_addr = sc.function->GetAddress().GetLoadAddress(target_sp.get());
746+
747+
// Only annotate if the current disassembly line is short enough
748+
// to keep annotations aligned past the desired annotation_column.
749+
if (ss.GetSizeOfLastLine() >= annotation_column)
750+
return;
751+
752+
std::vector<std::string> annotations;
753+
754+
for (size_t i = 0; i < var_list_sp->GetSize(); ++i) {
755+
VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
756+
if (!var_sp)
757+
continue;
758+
759+
const char *name = var_sp->GetName().AsCString();
760+
auto &expr_list = var_sp->LocationExpressionList();
761+
if (!expr_list.IsValid())
762+
continue;
763+
764+
// Handle std::optional<DWARFExpressionEntry>.
765+
if (auto entryOrErr = expr_list.GetExpressionEntryAtAddress(func_load_addr, current_pc)) {
766+
auto entry = *entryOrErr;
767+
// Check if entry has a file_range, and filter on address if so.
768+
if (!entry.file_range || entry.file_range->ContainsFileAddress(
769+
(current_pc - func_load_addr) + expr_list.GetFuncFileAddress())) {
770+
771+
StreamString loc_str;
772+
ABI *abi = exe_ctx->GetProcessPtr()->GetABI().get();
773+
llvm::DIDumpOptions opts;
774+
opts.ShowAddresses = false;
775+
opts.PrintRegisterOnly = true; // <-- important: suppress DW_OP_... annotations, etc.
776+
777+
entry.expr->DumpLocationWithOptions(&loc_str, eDescriptionLevelBrief, abi, opts);
778+
779+
// Only include if not empty.
780+
llvm::StringRef loc_clean = llvm::StringRef(loc_str.GetString()).trim();
781+
if (!loc_clean.empty()) {
782+
annotations.push_back(llvm::formatv("{0} = {1}", name, loc_clean));
789783
}
790784
}
785+
}
786+
}
791787

792-
frame->ChangePC(original_pc);
788+
if (!annotations.empty()) {
789+
ss.FillLastLineToColumn(annotation_column, ' ');
790+
ss.PutCString(" ; ");
791+
for (size_t i = 0; i < annotations.size(); ++i) {
792+
if (i > 0)
793+
ss.PutCString(", ");
794+
ss.PutCString(annotations[i]);
793795
}
794796
}
797+
798+
frame->ChangePC(original_pc);
799+
};
800+
801+
if (exe_ctx && exe_ctx->GetFramePtr()) {
802+
annotate_variables();
795803
}
796804

797805
if (!m_comment.empty()) {
@@ -803,6 +811,7 @@ void Instruction::Dump(lldb_private::Stream *s, uint32_t max_opcode_byte_size,
803811
s->PutCString(ss.GetString());
804812
}
805813

814+
806815
bool Instruction::DumpEmulation(const ArchSpec &arch) {
807816
std::unique_ptr<EmulateInstruction> insn_emulator_up(
808817
EmulateInstruction::FindPlugin(arch, eInstructionTypeAny, nullptr));

0 commit comments

Comments
 (0)