[llvm-branch-commits] [lldb] [lldb] Add extended variable support to Get*VariableList. (PR #181501)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Feb 14 11:02:36 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Aman LaChapelle (bzcheeseman)
<details>
<summary>Changes</summary>
Stacked PRs:
* __->__#<!-- -->181501
* #<!-- -->181500
--- --- ---
### [lldb] Add extended variable support to Get*VariableList.
This patch adds a new flag to the lldb_private::StackFrame API to get variable lists: `include_extended_vars`. This allows ScriptedFrame (and other future synthetic frames) to construct 'fake' variables and return them in the VariableList, so that commands like `fr v` and `SBFrame::GetVariables` can show them to the user as requested.
This patch includes all changes necessary to call the API the new way - I tried to use my best judgement on when to include extended variables or not and leave comments explaining the decision.
As a consequence of producing extended variables, this patch means that ScriptedFrame can produce Variable objects with ValueType that contains a ValueTypeExtendedMask in a high bit. This necessarily complicates some of the switch/case handling in places where we would expect to find such variables, and this patch makes best effort to address all such cases as well. From experience, they tend to show up whenever we're dealing with checking if a Variable is in a specified scope, which means we basically have to check the high bit against some user input saying "yes/no extended variables".
---
Patch is 28.79 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/181501.diff
13 Files Affected:
- (modified) lldb/include/lldb/Target/BorrowedStackFrame.h (+2)
- (modified) lldb/include/lldb/Target/StackFrame.h (+16-2)
- (modified) lldb/source/API/SBFrame.cpp (+40-10)
- (modified) lldb/source/Commands/CommandObjectFrame.cpp (+30-11)
- (modified) lldb/source/Core/IOHandlerCursesGUI.cpp (+1-1)
- (modified) lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp (+8-3)
- (modified) lldb/source/Plugins/Process/scripted/ScriptedFrame.cpp (+42-8)
- (modified) lldb/source/Plugins/Process/scripted/ScriptedFrame.h (+10-5)
- (modified) lldb/source/Symbol/Variable.cpp (+6-4)
- (modified) lldb/source/Target/BorrowedStackFrame.cpp (+6-3)
- (modified) lldb/source/Target/StackFrame.cpp (+15-2)
- (modified) lldb/test/API/functionalities/scripted_frame_provider/TestScriptedFrameProvider.py (+27-7)
- (modified) lldb/test/API/functionalities/scripted_frame_provider/test_frame_providers.py (+2)
``````````diff
diff --git a/lldb/include/lldb/Target/BorrowedStackFrame.h b/lldb/include/lldb/Target/BorrowedStackFrame.h
index 72e7777961da7..f0dcb89559085 100644
--- a/lldb/include/lldb/Target/BorrowedStackFrame.h
+++ b/lldb/include/lldb/Target/BorrowedStackFrame.h
@@ -78,10 +78,12 @@ class BorrowedStackFrame : public StackFrame {
lldb::RegisterContextSP GetRegisterContext() override;
VariableList *GetVariableList(bool get_file_globals,
+ bool include_extended_vars,
Status *error_ptr) override;
lldb::VariableListSP
GetInScopeVariableList(bool get_file_globals,
+ bool include_extended_vars = true,
bool must_have_valid_location = false) override;
lldb::ValueObjectSP GetValueForVariableExpressionPath(
diff --git a/lldb/include/lldb/Target/StackFrame.h b/lldb/include/lldb/Target/StackFrame.h
index 46922448d6e59..83f863bafb1cb 100644
--- a/lldb/include/lldb/Target/StackFrame.h
+++ b/lldb/include/lldb/Target/StackFrame.h
@@ -262,6 +262,12 @@ class StackFrame : public ExecutionContextScope,
/// that are visible to the entire compilation unit (e.g. file
/// static in C, globals that are homed in this CU).
///
+ /// \param[in] include_extended_vars
+ /// Whether to also include extended variables from other
+ /// sources. For example, synthetic frames can produce
+ /// variables that aren't strictly 'variables', but can still
+ /// be displayed with their values.
+ ///
/// \param [out] error_ptr
/// If there is an error in the debug information that prevents variables
/// from being fetched. \see SymbolFile::GetFrameVariableError() for full
@@ -269,7 +275,7 @@ class StackFrame : public ExecutionContextScope,
///
/// \return
/// A pointer to a list of variables.
- virtual VariableList *GetVariableList(bool get_file_globals,
+ virtual VariableList *GetVariableList(bool get_file_globals, bool include_extended_vars,
Status *error_ptr);
/// Retrieve the list of variables that are in scope at this StackFrame's
@@ -284,13 +290,21 @@ class StackFrame : public ExecutionContextScope,
/// that are visible to the entire compilation unit (e.g. file
/// static in C, globals that are homed in this CU).
///
+ /// \param[in] include_extended_vars
+ /// Whether to also include extended variables from other
+ /// sources. For example, synthetic frames can produce
+ /// variables that aren't strictly 'variables', but can still
+ /// be displayed with their values. Defaults to `true` because
+ /// we are assuming that if a user's context has extended variables,
+ /// they want them shown.
+ ///
/// \param[in] must_have_valid_location
/// Whether to filter variables whose location is not available at this
/// StackFrame's pc.
/// \return
/// A pointer to a list of variables.
virtual lldb::VariableListSP
- GetInScopeVariableList(bool get_file_globals,
+ GetInScopeVariableList(bool get_file_globals, bool include_extended_vars = true,
bool must_have_valid_location = false);
/// Create a ValueObject for a variable name / pathname, possibly including
diff --git a/lldb/source/API/SBFrame.cpp b/lldb/source/API/SBFrame.cpp
index 31947a054aaaf..84c9211073ec8 100644
--- a/lldb/source/API/SBFrame.cpp
+++ b/lldb/source/API/SBFrame.cpp
@@ -12,6 +12,7 @@
#include "lldb/API/SBFrame.h"
+#include "lldb/lldb-enumerations.h"
#include "lldb/lldb-types.h"
#include "Utils.h"
@@ -498,7 +499,11 @@ SBValue SBFrame::FindValue(const char *name, ValueType value_type,
VariableList variable_list;
- switch (value_type) {
+ bool include_extended_vars = value_type & ValueTypeExtendedMask;
+ // Switch on the value_type without the mask, but keep it in the value type so
+ // we can use it later when we look for variables in the list.
+ auto base_value_type = value_type & ~ValueTypeExtendedMask;
+ switch (base_value_type) {
case eValueTypeVariableGlobal: // global variable
case eValueTypeVariableStatic: // static variable
case eValueTypeVariableArgument: // function argument variables
@@ -514,14 +519,17 @@ SBValue SBFrame::FindValue(const char *name, ValueType value_type,
sc.block->AppendVariables(
can_create, get_parent_variables, stop_if_block_is_inlined_function,
[frame](Variable *v) { return v->IsInScope(frame); }, &variable_list);
- if (value_type == eValueTypeVariableGlobal ||
- value_type == eValueTypeVariableStatic) {
+ // Fetch variables from the frame if we need to get globals/statics/extended
+ // variables.
+ if (base_value_type == eValueTypeVariableGlobal ||
+ base_value_type == eValueTypeVariableStatic || include_extended_vars) {
const bool get_file_globals = true;
- VariableList *frame_vars =
- frame->GetVariableList(get_file_globals, nullptr);
+ VariableList *frame_vars = frame->GetVariableList(
+ get_file_globals, include_extended_vars, nullptr);
if (frame_vars)
frame_vars->AppendVariablesIfUnique(variable_list);
}
+
ConstString const_name(name);
VariableSP variable_sp(variable_list.FindVariable(const_name, value_type));
if (variable_sp) {
@@ -686,8 +694,22 @@ lldb::SBValueList SBFrame::GetVariables(bool arguments, bool locals,
/// Returns true if the variable is in any of the requested scopes.
static bool IsInRequestedScope(bool statics, bool arguments, bool locals,
- Variable &var) {
- switch (var.GetScope()) {
+ bool extended, Variable &var) {
+ auto value_type = var.GetScope();
+ // Check if the variable is 'extended' first.
+ bool is_extended = value_type & ValueTypeExtendedMask;
+ if (is_extended) {
+ // If the variable is extended but we don't want those, then it's
+ // automatically out of scope.
+ if (!extended)
+ return false;
+
+ // Clear the ValueTypeExtendedMask bit so the rest of the switch works
+ // correctly.
+ value_type = ValueType(value_type ^ ValueTypeExtendedMask);
+ }
+
+ switch (value_type) {
case eValueTypeVariableGlobal:
case eValueTypeVariableStatic:
case eValueTypeVariableThreadLocal:
@@ -702,7 +724,13 @@ static bool IsInRequestedScope(bool statics, bool arguments, bool locals,
default:
break;
}
- return false;
+
+ // The default for all other value types is !is_extended. At this point, if we
+ // didn't want extended variables we'd have exited by now anyway, so we must
+ // want them. Aside from the modifiers above that should apply equally to
+ // extended and normal variables, any other extended variable we should
+ // default to showing.
+ return !is_extended;
}
enum WasInterrupted { Yes, No };
@@ -718,13 +746,15 @@ static std::pair<WasInterrupted, Status> FetchVariablesUnlessInterrupted(
const bool statics = options.GetIncludeStatics();
const bool arguments = options.GetIncludeArguments();
const bool locals = options.GetIncludeLocals();
+ const bool extended = options.GetIncludeExtended();
const bool in_scope_only = options.GetInScopeOnly();
const bool include_runtime_support_values =
options.GetIncludeRuntimeSupportValues();
const lldb::DynamicValueType use_dynamic = options.GetUseDynamic();
Status var_error;
- VariableList *variable_list = frame.GetVariableList(true, &var_error);
+ // Fetch all variables available and filter them later.
+ VariableList *variable_list = frame.GetVariableList(true, true, &var_error);
std::set<VariableSP> variable_set;
@@ -734,7 +764,7 @@ static std::pair<WasInterrupted, Status> FetchVariablesUnlessInterrupted(
size_t num_produced = 0;
for (const VariableSP &variable_sp : *variable_list) {
if (!variable_sp ||
- !IsInRequestedScope(statics, arguments, locals, *variable_sp))
+ !IsInRequestedScope(statics, arguments, locals, extended, *variable_sp))
continue;
if (INTERRUPT_REQUESTED(
diff --git a/lldb/source/Commands/CommandObjectFrame.cpp b/lldb/source/Commands/CommandObjectFrame.cpp
index 9133359fbf537..fc6fabe14fc17 100644
--- a/lldb/source/Commands/CommandObjectFrame.cpp
+++ b/lldb/source/Commands/CommandObjectFrame.cpp
@@ -30,6 +30,7 @@
#include "lldb/Target/Thread.h"
#include "lldb/Utility/Args.h"
#include "lldb/ValueObject/ValueObject.h"
+#include "lldb/lldb-enumerations.h"
#include <memory>
#include <optional>
@@ -337,9 +338,9 @@ class CommandObjectFrameSelect : public CommandObjectParsed {
// The request went past the stack, so handle that case:
const uint32_t num_frames = thread->GetStackFrameCount();
if (static_cast<int32_t>(num_frames - frame_idx) >
- *m_options.relative_frame_offset)
- frame_idx += *m_options.relative_frame_offset;
- else {
+ *m_options.relative_frame_offset) {
+ frame_idx += *m_options.relative_frame_offset;
+ } else {
if (frame_idx == num_frames - 1) {
// If we are already at the top of the stack, just warn and don't
// reset the frame.
@@ -439,17 +440,23 @@ may even involve JITing and running code in the target program.)");
if (!var_sp)
return llvm::StringRef();
- switch (var_sp->GetScope()) {
+ auto vt = var_sp->GetScope();
+ bool is_extended = vt & ValueTypeExtendedMask;
+ // Clear the bit so the rest works correctly.
+ if (is_extended)
+ vt = ValueType(vt ^ ValueTypeExtendedMask);
+
+ switch (vt) {
case eValueTypeVariableGlobal:
- return "GLOBAL: ";
+ return is_extended ? "(ext) GLOBAL: " : "GLOBAL: ";
case eValueTypeVariableStatic:
- return "STATIC: ";
+ return is_extended ? "(ext) STATIC: " : "STATIC: ";
case eValueTypeVariableArgument:
- return "ARG: ";
+ return is_extended ? "(ext) ARG: " : "ARG: ";
case eValueTypeVariableLocal:
- return "LOCAL: ";
+ return is_extended ? "(ext) LOCAL: " : "LOCAL: ";
case eValueTypeVariableThreadLocal:
- return "THREAD: ";
+ return is_extended ? "(ext) THREAD: " : "THREAD: ";
default:
break;
}
@@ -459,6 +466,14 @@ may even involve JITing and running code in the target program.)");
/// Returns true if `scope` matches any of the options in `m_option_variable`.
bool ScopeRequested(lldb::ValueType scope) {
+ // If it's an extended variable, check if we want to show those first.
+ bool is_extended = scope & ValueTypeExtendedMask;
+ if (is_extended) {
+ if (!m_option_variable.show_extended)
+ return false;
+
+ scope = ValueType(scope ^ ValueTypeExtendedMask);
+ }
switch (scope) {
case eValueTypeVariableGlobal:
case eValueTypeVariableStatic:
@@ -474,7 +489,10 @@ may even involve JITing and running code in the target program.)");
case eValueTypeVariableThreadLocal:
case eValueTypeVTable:
case eValueTypeVTableEntry:
- return false;
+ // The default for all other value types is !is_extended. Aside from the
+ // modifiers above that should apply equally to extended and normal
+ // variables, any other extended variable we should default to showing.
+ return !is_extended;
}
llvm_unreachable("Unexpected scope value");
}
@@ -521,7 +539,8 @@ may even involve JITing and running code in the target program.)");
Status error;
VariableList *variable_list =
- frame->GetVariableList(m_option_variable.show_globals, &error);
+ frame->GetVariableList(m_option_variable.show_globals,
+ m_option_variable.show_extended, &error);
if (error.Fail() && (!variable_list || variable_list->GetSize() == 0)) {
result.AppendError(error.AsCString());
diff --git a/lldb/source/Core/IOHandlerCursesGUI.cpp b/lldb/source/Core/IOHandlerCursesGUI.cpp
index 47ba35877651a..867b097ab7b12 100644
--- a/lldb/source/Core/IOHandlerCursesGUI.cpp
+++ b/lldb/source/Core/IOHandlerCursesGUI.cpp
@@ -5934,7 +5934,7 @@ class FrameVariablesWindowDelegate : public ValueObjectListDelegate {
if (m_frame_block != frame_block) {
m_frame_block = frame_block;
- VariableList *locals = frame->GetVariableList(true, nullptr);
+ VariableList *locals = frame->GetVariableList(true, true, nullptr);
if (locals) {
const DynamicValueType use_dynamic = eDynamicDontRunTarget;
for (const VariableSP &local_sp : *locals) {
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
index 664bcba0017a7..345cebb685e04 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
@@ -868,8 +868,10 @@ void ClangExpressionDeclMap::LookUpLldbClass(NameSearchContext &context) {
// creates decls for function templates by attaching them to the TU instead
// of a class context. So we can actually have template methods scoped
// outside of a class. Once we fix that, we can remove this code-path.
-
- VariableList *vars = frame->GetVariableList(false, nullptr);
+ // Additionally, we exclude extended variables from here. Clang-based
+ // languages are unlikely candidates for extended variables anyway, and
+ // especially in this case, we're looking for something specific to C++.
+ VariableList *vars = frame->GetVariableList(false, false, nullptr);
lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
@@ -955,7 +957,10 @@ void ClangExpressionDeclMap::LookUpLldbObjCClass(NameSearchContext &context) {
// In that case, just look up the "self" variable in the current scope
// and use its type.
- VariableList *vars = frame->GetVariableList(false, nullptr);
+ // We exclude extended variables from here. Like above, it's highly unlikely
+ // we care about synthetic variables here, and indeed this code is looking for
+ // an obj-C specific construct.
+ VariableList *vars = frame->GetVariableList(false, false, nullptr);
lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
diff --git a/lldb/source/Plugins/Process/scripted/ScriptedFrame.cpp b/lldb/source/Plugins/Process/scripted/ScriptedFrame.cpp
index 7462c467eb7da..a1e44a9dd0ed3 100644
--- a/lldb/source/Plugins/Process/scripted/ScriptedFrame.cpp
+++ b/lldb/source/Plugins/Process/scripted/ScriptedFrame.cpp
@@ -14,6 +14,7 @@
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleList.h"
+#include "lldb/Expression/DWARFExpressionList.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Interpreter/Interfaces/ScriptedFrameInterface.h"
#include "lldb/Interpreter/Interfaces/ScriptedThreadInterface.h"
@@ -32,6 +33,11 @@
#include "lldb/Utility/StructuredData.h"
#include "lldb/ValueObject/ValueObject.h"
#include "lldb/ValueObject/ValueObjectList.h"
+#include "lldb/lldb-enumerations.h"
+#include "lldb/lldb-forward.h"
+#include "llvm/Support/ErrorHandling.h"
+
+#include <memory>
using namespace lldb;
using namespace lldb_private;
@@ -271,19 +277,22 @@ lldb::RegisterContextSP ScriptedFrame::GetRegisterContext() {
}
VariableList *ScriptedFrame::GetVariableList(bool get_file_globals,
+ bool include_extended_vars,
Status *error_ptr) {
- PopulateVariableListFromInterface();
+ PopulateVariableListFromInterface(include_extended_vars);
return m_variable_list_sp.get();
}
lldb::VariableListSP
ScriptedFrame::GetInScopeVariableList(bool get_file_globals,
+ bool include_extended_vars,
bool must_have_valid_location) {
- PopulateVariableListFromInterface();
+ PopulateVariableListFromInterface(include_extended_vars);
return m_variable_list_sp;
}
-void ScriptedFrame::PopulateVariableListFromInterface() {
+void ScriptedFrame::PopulateVariableListFromInterface(
+ bool include_extended_vars) {
// Fetch values from the interface.
ValueObjectListSP value_list_sp = GetInterface()->GetVariables();
if (!value_list_sp)
@@ -297,12 +306,28 @@ void ScriptedFrame::PopulateVariableListFromInterface() {
continue;
VariableSP var = v->GetVariable();
- // TODO: We could in theory ask the scripted frame to *produce* a
- // variable for this value object.
- if (!var)
- continue;
+ if (!var && include_extended_vars) {
+ // Construct the value type as an extended verison of what the value type
+ // is. That'll allow the user to tell the scope and the 'extended-ness' of
+ // the variable.
+ lldb::ValueType vt =
+ lldb::ValueType(v->GetValueType() & ValueTypeExtendedMask);
+
+ // Just make up a variable - the frame variable dumper just passes it
+ // back in to GetValueObjectForFrameVariable, so we really just need to
+ // make sure the name and type are correct.
+ var = std::make_shared<lldb_private::Variable>(
+ (lldb::user_id_t)v->GetID() + i, v->GetName().GetCString(),
+ v->GetName().GetCString(), nullptr, vt,
+ /*owner_scope=*/nullptr,
+ /*scope_range=*/Variable::RangeList{},
+ /*decl=*/nullptr, DWARFExpressionList{}, /*external=*/false,
+ /*artificial=*/true, /*location_is_constant_data=*/false);
+ }
- m_variable_list_sp->AddVariable(var);
+ // Only append the variable if we have one (had already, or just created).
+ if (var)
+ m_variable_list_sp->AddVariable(var);
}
}
@@ -316,6 +341,15 @@ lldb::ValueObjectSP ScriptedFrame::GetValueObjectForFrameVariable(
return values->FindValueObjectByValueName(variable_sp->GetName().AsCString());
}
+lldb::ValueObjectSP ScriptedFrame::FindVariable(ConstString name) {
+ // Fetch values from the interface.
+ ValueObjectListSP values = m_scripted_frame_interface_sp->GetVariables();
+ if (!values)
+ return {};
+
+ return values->FindValueObjectByValueName(name.AsCString());
+}
+
lldb::ValueObjectSP ScriptedFrame::GetValueForVariableExpressionPath(
llvm::StringRef var_expr, lldb::DynamicValueType use_dynamic,
uint32_t options, lldb::VariableSP &var_sp, Status &error) {
diff --git a/lldb/source/Plugins/Process/scripted/ScriptedFrame.h b/lldb/source/Plugins/Process/scripted/ScriptedFrame.h
index fe154792c745b..43dbf36f683e1 100644
--- a/lldb/source/Plugins/Process/scripted/ScriptedFrame.h
+++ b/lldb/source/Plugins/Process/scripted/ScriptedFrame.h
@@ -64,16 +64,19 @@ class ScriptedFrame : public lldb_private::StackFrame {
lldb::RegisterContextSP GetRegisterContext() override;
VariableList *GetVariableList(bool get_file_globals,
+ bool include_extended_vars,
lldb_private::Status *error_ptr) override;
lldb::VariableListSP
- GetInScopeVariableList(bool get_file_globals,
+ GetInScopeVariableList(bool get_file_globals, bool include_extended_vars,
bool must_have_valid_location = false) override;
lldb::ValueObjectSP
GetValueObjectForFrameVariable(const lldb::VariableSP &variable_sp,
lldb::DynamicValueType use_dynamic) override;
+ lldb::ValueObjectSP FindVariable(ConstString name) override;
+
lldb::ValueObjectSP GetValueForVariableExpressionPath(
llvm::StringRef var_expr, lldb::DynamicValueType use_dynamic,
uint32_t options, lldb::VariableSP &var_sp, Status &error) override;
@@ -90,10 +93,12 @@ class ScriptedFrame : public lldb_private::StackFrame {
CreateRegisterContext(ScriptedFrameInterface &interface, Thread &thread,
lldb::user_id_t frame_id);
- // Populate m_variable_list_sp from the scripted frame interface. Right now
- // this doesn't take any options because the implementation can't really do
- // anything with those options anyway, so there's no point.
- void PopulateVariableListFromInterface();
+ // Populate m_variable_list_sp from the scripted frame interface. The boolean
+ // controls if we should try to fabricate Variable objects for each of the
+ // ValueObjects that we ...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/181501
More information about the llvm-branch-commits
mailing list