-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[lldb] [disassembler] chore: enhance VariableAnnotator to return structured data #165163
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
n2h9
wants to merge
18
commits into
llvm:main
Choose a base branch
from
n2h9:rich-disassembler-structured-variable-annotations-00
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.
+379
−45
Open
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
aba4890
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 199a909
Update lldb/include/lldb/Core/Disassembler.h
n2h9 ea1bf46
Update lldb/include/lldb/Core/Disassembler.h
n2h9 0331516
Update lldb/test/API/functionalities/disassembler-variables/TestVaria…
n2h9 2449b6c
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 839ac8e
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 5278024
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 7055cc0
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 13afa00
Merge branch 'main' into rich-disassembler-structured-variable-annota…
n2h9 4cee0d7
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 c955af3
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 4d8706d
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 573a6a1
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 c688f70
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 31e91cf
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 f805dbb
Update lldb/include/lldb/Core/Disassembler.h
n2h9 f03a448
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 f10922f
[lldb] [disassembler] chore: enhance VariableAnnotator to return stru…
n2h9 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 |
|---|---|---|
|
|
@@ -299,17 +299,45 @@ bool Disassembler::ElideMixedSourceAndDisassemblyLine( | |
| // The goal is to give users helpful live variable hints alongside the | ||
| // disassembled instruction stream, similar to how debug information | ||
| // enhances source-level debugging. | ||
| std::vector<std::string> | ||
| VariableAnnotator::annotate(Instruction &inst, Target &target, | ||
| const lldb::ModuleSP &module_sp) { | ||
| std::vector<std::string> VariableAnnotator::Annotate(Instruction &inst, | ||
| Target &target, | ||
| lldb::ModuleSP module_sp) { | ||
| auto structured_annotations = AnnotateStructured(inst, target, module_sp); | ||
|
|
||
| std::vector<std::string> events; | ||
| events.reserve(structured_annotations.size()); | ||
|
|
||
| for (const auto &annotation : structured_annotations) { | ||
| std::string display_string; | ||
| display_string = | ||
| llvm::formatv( | ||
| "{0} = {1}", annotation.variable_name, | ||
| annotation.location_description == VariableAnnotator::kUndefLocation | ||
| ? llvm::formatv("<{0}>", VariableAnnotator::kUndefLocation) | ||
| .str() | ||
| : annotation.location_description) | ||
| .str(); | ||
|
Comment on lines
+312
to
+319
Member
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. This seems like it could benefit from writing this as a stream: |
||
| events.push_back(display_string); | ||
| } | ||
|
|
||
| return events; | ||
| } | ||
|
|
||
| std::vector<VariableAnnotation> | ||
| VariableAnnotator::AnnotateStructured(Instruction &inst, Target &target, | ||
| lldb::ModuleSP module_sp) { | ||
| std::vector<VariableAnnotation> annotations; | ||
|
|
||
| // If we lost module context, everything becomes <undef>. | ||
| // If we lost module context, mark all live variables as undefined. | ||
| if (!module_sp) { | ||
| for (const auto &KV : Live_) | ||
| events.emplace_back(llvm::formatv("{0} = <undef>", KV.second.name).str()); | ||
| for (const auto &KV : Live_) { | ||
| auto annotation_entity = KV.second; | ||
| annotation_entity.is_live = false; | ||
| annotation_entity.location_description = kUndefLocation; | ||
| annotations.push_back(annotation_entity); | ||
| } | ||
| Live_.clear(); | ||
| return events; | ||
| return annotations; | ||
| } | ||
|
|
||
| // Resolve function/block at this *file* address. | ||
|
|
@@ -319,10 +347,14 @@ VariableAnnotator::annotate(Instruction &inst, Target &target, | |
| if (!module_sp->ResolveSymbolContextForAddress(iaddr, mask, sc) || | ||
| !sc.function) { | ||
| // No function context: everything dies here. | ||
| for (const auto &KV : Live_) | ||
| events.emplace_back(llvm::formatv("{0} = <undef>", KV.second.name).str()); | ||
| for (const auto &KV : Live_) { | ||
| auto annotation_entity = KV.second; | ||
| annotation_entity.is_live = false; | ||
| annotation_entity.location_description = kUndefLocation; | ||
| annotations.push_back(annotation_entity); | ||
| } | ||
| Live_.clear(); | ||
| return events; | ||
| return annotations; | ||
| } | ||
|
|
||
| // Collect in-scope variables for this instruction into Current. | ||
|
|
@@ -349,7 +381,7 @@ VariableAnnotator::annotate(Instruction &inst, Target &target, | |
| // Prefer "register-only" output when we have an ABI. | ||
| opts.PrintRegisterOnly = static_cast<bool>(abi_sp); | ||
|
|
||
| llvm::DenseMap<lldb::user_id_t, VarState> Current; | ||
| llvm::DenseMap<lldb::user_id_t, VariableAnnotation> Current; | ||
|
|
||
| for (size_t i = 0, e = var_list.GetSize(); i != e; ++i) { | ||
| lldb::VariableSP v = var_list.GetVariableAtIndex(i); | ||
|
|
@@ -376,8 +408,26 @@ VariableAnnotator::annotate(Instruction &inst, Target &target, | |
| if (loc.empty()) | ||
| continue; | ||
|
|
||
| std::optional<std::string> decl_file; | ||
| std::optional<uint32_t> decl_line; | ||
| std::optional<std::string> type_name; | ||
|
|
||
| const Declaration &decl = v->GetDeclaration(); | ||
| if (decl.GetFile()) { | ||
| decl_file = decl.GetFile().GetFilename().AsCString(); | ||
| if (decl.GetLine() > 0) | ||
| decl_line = decl.GetLine(); | ||
| } | ||
|
|
||
| if (Type *type = v->GetType()) | ||
| if (const char *type_str = type->GetName().AsCString()) | ||
| type_name = type_str; | ||
|
|
||
| Current.try_emplace(v->GetID(), | ||
| VarState{std::string(name), std::string(loc)}); | ||
| VariableAnnotation{std::string(name), std::string(loc), | ||
| true, entry.expr->GetRegisterKind(), | ||
| entry.file_range, decl_file, | ||
| decl_line, type_name}); | ||
| } | ||
|
|
||
| // Diff Live_ → Current. | ||
|
|
@@ -387,24 +437,31 @@ VariableAnnotator::annotate(Instruction &inst, Target &target, | |
| auto it = Live_.find(KV.first); | ||
| if (it == Live_.end()) { | ||
| // Newly live. | ||
| events.emplace_back( | ||
| llvm::formatv("{0} = {1}", KV.second.name, KV.second.last_loc).str()); | ||
| } else if (it->second.last_loc != KV.second.last_loc) { | ||
| auto annotation_entity = KV.second; | ||
| annotation_entity.is_live = true; | ||
| annotations.push_back(annotation_entity); | ||
| } else if (it->second.location_description != | ||
| KV.second.location_description) { | ||
| // Location changed. | ||
| events.emplace_back( | ||
| llvm::formatv("{0} = {1}", KV.second.name, KV.second.last_loc).str()); | ||
| auto annotation_entity = KV.second; | ||
| annotation_entity.is_live = true; | ||
| annotations.push_back(annotation_entity); | ||
| } | ||
| } | ||
|
|
||
| // 2) Ends: anything that was live but is not in Current becomes <undef>. | ||
| for (const auto &KV : Live_) { | ||
| if (!Current.count(KV.first)) | ||
| events.emplace_back(llvm::formatv("{0} = <undef>", KV.second.name).str()); | ||
| } | ||
| // 2) Ends: anything that was live but is not in Current becomes | ||
| // <kUndefLocation>. | ||
| for (const auto &KV : Live_) | ||
| if (!Current.count(KV.first)) { | ||
| auto annotation_entity = KV.second; | ||
| annotation_entity.is_live = false; | ||
| annotation_entity.location_description = kUndefLocation; | ||
| annotations.push_back(annotation_entity); | ||
| } | ||
|
|
||
| // Commit new state. | ||
| Live_ = std::move(Current); | ||
| return events; | ||
| return annotations; | ||
| } | ||
|
|
||
| void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch, | ||
|
|
@@ -676,7 +733,7 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch, | |
| address_text_size); | ||
|
|
||
| if ((options & eOptionVariableAnnotations) && target_sp) { | ||
| auto annotations = annot.annotate(*inst, *target_sp, module_sp); | ||
| auto annotations = annot.Annotate(*inst, *target_sp, module_sp); | ||
| if (!annotations.empty()) { | ||
| const size_t annotation_column = 100; | ||
| inst_line.FillLastLineToColumn(annotation_column, ' '); | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.