[Lldb-commits] [lldb] [lldb] Add extended variable support to Get*VariableList. (PR #181501)

Aman LaChapelle via lldb-commits lldb-commits at lists.llvm.org
Sat Feb 14 12:08:25 PST 2026


https://github.com/bzcheeseman updated https://github.com/llvm/llvm-project/pull/181501

>From 8b4485f4613c9ea1495b24d1d4a3db1f5676500b Mon Sep 17 00:00:00 2001
From: bzcheeseman <aman.lachapelle at gmail.com>
Date: Sat, 14 Feb 2026 10:55:50 -0800
Subject: [PATCH 1/2] [lldb] Scaffolding for extended variable support.

This patch handles most of the scaffolding for extended variable support that isn't directly tied to functional changes. This patch will be used by one following patch that actually modifies the lldb_private::StackFrame API to allow us to fetch extended variables.

There were a couple important/interesting decisions made in this patch that should be noted:
- Any value type may be 'extended', which is why it's a mask applied over the top of another value type.
- When printing frame variables with `fr v`, default to showing extended variables.

This new value type mask makes some of the ValueType handling more interesting, but since nothing generates objects with this mask until the next patch, we can land the concept in this patch in some amount of isolation.

stack-info: PR: https://github.com/llvm/llvm-project/pull/181500, branch: users/bzcheeseman/stack/8
---
 lldb/include/lldb/API/SBVariablesOptions.h    |  4 ++++
 .../lldb/Interpreter/OptionGroupVariable.h    |  1 +
 lldb/include/lldb/lldb-enumerations.h         |  5 +++++
 lldb/source/API/SBVariablesOptions.cpp        | 21 +++++++++++++++++--
 .../Interpreter/OptionGroupVariable.cpp       | 12 +++++++++--
 5 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/lldb/include/lldb/API/SBVariablesOptions.h b/lldb/include/lldb/API/SBVariablesOptions.h
index 53ab4b7e14f2f..4868c618a1c10 100644
--- a/lldb/include/lldb/API/SBVariablesOptions.h
+++ b/lldb/include/lldb/API/SBVariablesOptions.h
@@ -46,6 +46,10 @@ class LLDB_API SBVariablesOptions {
 
   void SetIncludeStatics(bool);
 
+  bool GetIncludeExtended() const;
+
+  void SetIncludeExtended(bool);
+
   bool GetInScopeOnly() const;
 
   void SetInScopeOnly(bool);
diff --git a/lldb/include/lldb/Interpreter/OptionGroupVariable.h b/lldb/include/lldb/Interpreter/OptionGroupVariable.h
index c9f1283d4de20..4bea1eada6f6b 100644
--- a/lldb/include/lldb/Interpreter/OptionGroupVariable.h
+++ b/lldb/include/lldb/Interpreter/OptionGroupVariable.h
@@ -35,6 +35,7 @@ class OptionGroupVariable : public OptionGroup {
                                  // true)
       show_locals : 1,  // Frame option only (include_frame_options == true)
       show_globals : 1, // Frame option only (include_frame_options == true)
+      show_extended : 1, // Frame option only (include_frame_options == true)
       use_regex : 1, show_scope : 1, show_decl : 1;
   OptionValueString summary;        // the name of a named summary
   OptionValueString summary_string; // a summary string
diff --git a/lldb/include/lldb/lldb-enumerations.h b/lldb/include/lldb/lldb-enumerations.h
index 4cbbabbf879ad..e42526309bca5 100644
--- a/lldb/include/lldb/lldb-enumerations.h
+++ b/lldb/include/lldb/lldb-enumerations.h
@@ -340,6 +340,11 @@ enum ValueType {
   eValueTypeVTableEntry = 10, ///< function pointer in virtual function table
 };
 
+/// A mask that we can use to check if the value type is 'extended' or not.
+// NOTE: This limits the number of value types to 31, but that's 3x more than
+// what we currently have now.
+static constexpr unsigned ValueTypeExtendedMask = 0x20;
+
 /// Token size/granularities for Input Readers.
 
 enum InputReaderGranularity {
diff --git a/lldb/source/API/SBVariablesOptions.cpp b/lldb/source/API/SBVariablesOptions.cpp
index 989d159139cca..402af0039495e 100644
--- a/lldb/source/API/SBVariablesOptions.cpp
+++ b/lldb/source/API/SBVariablesOptions.cpp
@@ -20,8 +20,8 @@ class VariablesOptionsImpl {
 public:
   VariablesOptionsImpl()
       : m_include_arguments(false), m_include_locals(false),
-        m_include_statics(false), m_in_scope_only(false),
-        m_include_runtime_support_values(false) {}
+        m_include_statics(false), m_include_extended(false),
+        m_in_scope_only(false), m_include_runtime_support_values(false) {}
 
   VariablesOptionsImpl(const VariablesOptionsImpl &) = default;
 
@@ -51,6 +51,10 @@ class VariablesOptionsImpl {
 
   void SetIncludeStatics(bool b) { m_include_statics = b; }
 
+  bool GetIncludeExtended() const { return m_include_extended; }
+
+  void SetIncludeExtended(bool b) { m_include_extended = b; }
+
   bool GetInScopeOnly() const { return m_in_scope_only; }
 
   void SetInScopeOnly(bool b) { m_in_scope_only = b; }
@@ -71,6 +75,7 @@ class VariablesOptionsImpl {
   bool m_include_arguments : 1;
   bool m_include_locals : 1;
   bool m_include_statics : 1;
+  bool m_include_extended : 1;
   bool m_in_scope_only : 1;
   bool m_include_runtime_support_values : 1;
   LazyBool m_include_recognized_arguments =
@@ -157,6 +162,18 @@ void SBVariablesOptions::SetIncludeStatics(bool statics) {
   m_opaque_up->SetIncludeStatics(statics);
 }
 
+bool SBVariablesOptions::GetIncludeExtended() const {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetIncludeExtended();
+}
+
+void SBVariablesOptions::SetIncludeExtended(bool extended) {
+  LLDB_INSTRUMENT_VA(this, extended);
+
+  m_opaque_up->SetIncludeExtended(extended);
+}
+
 bool SBVariablesOptions::GetInScopeOnly() const {
   LLDB_INSTRUMENT_VA(this);
 
diff --git a/lldb/source/Interpreter/OptionGroupVariable.cpp b/lldb/source/Interpreter/OptionGroupVariable.cpp
index 9bffe1dba85e7..e97a4013c9f9a 100644
--- a/lldb/source/Interpreter/OptionGroupVariable.cpp
+++ b/lldb/source/Interpreter/OptionGroupVariable.cpp
@@ -32,6 +32,9 @@ static constexpr OptionDefinition g_variable_options[] = {
     {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "show-globals", 'g',
      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
      "Show the current frame source file global and static variables."},
+     {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-extended", 'e',
+     OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
+     "Omit extended variables."},
     {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "show-declaration", 'c',
      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
      "Show variable declaration information (source file and line where the "
@@ -77,8 +80,9 @@ static Status ValidateSummaryString(const char *str, void *) {
 OptionGroupVariable::OptionGroupVariable(bool show_frame_options)
     : include_frame_options(show_frame_options), show_args(false),
       show_recognized_args(false), show_locals(false), show_globals(false),
-      use_regex(false), show_scope(false), show_decl(false),
-      summary(ValidateNamedSummary), summary_string(ValidateSummaryString) {}
+      show_extended(true), use_regex(false), show_scope(false),
+      show_decl(false), summary(ValidateNamedSummary),
+      summary_string(ValidateSummaryString) {}
 
 Status
 OptionGroupVariable::SetOptionValue(uint32_t option_idx,
@@ -101,6 +105,9 @@ OptionGroupVariable::SetOptionValue(uint32_t option_idx,
   case 'g':
     show_globals = true;
     break;
+  case 'e':
+    show_extended = false;
+    break;
   case 'c':
     show_decl = true;
     break;
@@ -129,6 +136,7 @@ void OptionGroupVariable::OptionParsingStarting(
   show_recognized_args = true; // Frame option only
   show_locals = true;   // Frame option only
   show_globals = false; // Frame option only
+  show_extended = true; // Frame option only
   show_decl = false;
   use_regex = false;
   show_scope = false;

>From 03471de061a67eb4e0c2e06cb43c6cf38ba4820b Mon Sep 17 00:00:00 2001
From: bzcheeseman <aman.lachapelle at gmail.com>
Date: Sat, 14 Feb 2026 11:00:57 -0800
Subject: [PATCH 2/2] [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".

stack-info: PR: https://github.com/llvm/llvm-project/pull/181501, branch: users/bzcheeseman/stack/9
---
 lldb/include/lldb/Target/BorrowedStackFrame.h |  2 +
 lldb/include/lldb/Target/StackFrame.h         | 18 ++++++-
 lldb/source/API/SBFrame.cpp                   | 50 +++++++++++++++----
 lldb/source/Commands/CommandObjectFrame.cpp   | 41 +++++++++++----
 lldb/source/Core/IOHandlerCursesGUI.cpp       |  2 +-
 .../Clang/ClangExpressionDeclMap.cpp          | 11 ++--
 .../Process/scripted/ScriptedFrame.cpp        | 50 ++++++++++++++++---
 .../Plugins/Process/scripted/ScriptedFrame.h  | 15 ++++--
 lldb/source/Symbol/Variable.cpp               | 10 ++--
 lldb/source/Target/BorrowedStackFrame.cpp     |  9 ++--
 lldb/source/Target/StackFrame.cpp             | 17 ++++++-
 .../TestScriptedFrameProvider.py              | 34 ++++++++++---
 .../test_frame_providers.py                   |  2 +
 13 files changed, 205 insertions(+), 56 deletions(-)

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 have. This defaults to 'true' because this is a
+  // scripted frame, so kind of the whole point is to provide extended variables
+  // to the user.
+  void PopulateVariableListFromInterface(bool include_extended_vars = true);
 
   ScriptedFrame(const ScriptedFrame &) = delete;
   const ScriptedFrame &operator=(const ScriptedFrame &) = delete;
diff --git a/lldb/source/Symbol/Variable.cpp b/lldb/source/Symbol/Variable.cpp
index 45c9c0a843837..74cdc03feb614 100644
--- a/lldb/source/Symbol/Variable.cpp
+++ b/lldb/source/Symbol/Variable.cpp
@@ -590,9 +590,10 @@ static void PrivateAutoComplete(
     } else {
       if (frame) {
         const bool get_file_globals = true;
+        const bool include_extended_vars = true;
 
-        VariableList *variable_list = frame->GetVariableList(get_file_globals,
-                                                             nullptr);
+        VariableList *variable_list = frame->GetVariableList(
+            get_file_globals, include_extended_vars, nullptr);
 
         if (variable_list) {
           for (const VariableSP &var_sp : *variable_list)
@@ -686,9 +687,10 @@ static void PrivateAutoComplete(
         } else if (frame) {
           // We haven't found our variable yet
           const bool get_file_globals = true;
+          const bool include_extended_vars = true;
 
-          VariableList *variable_list =
-              frame->GetVariableList(get_file_globals, nullptr);
+          VariableList *variable_list = frame->GetVariableList(
+              get_file_globals, include_extended_vars, nullptr);
 
           if (!variable_list)
             break;
diff --git a/lldb/source/Target/BorrowedStackFrame.cpp b/lldb/source/Target/BorrowedStackFrame.cpp
index 5afadf21fde03..875f7d9764bc0 100644
--- a/lldb/source/Target/BorrowedStackFrame.cpp
+++ b/lldb/source/Target/BorrowedStackFrame.cpp
@@ -86,15 +86,18 @@ RegisterContextSP BorrowedStackFrame::GetRegisterContext() {
 }
 
 VariableList *BorrowedStackFrame::GetVariableList(bool get_file_globals,
+                                                  bool include_extended_vars,
                                                   Status *error_ptr) {
-  return m_borrowed_frame_sp->GetVariableList(get_file_globals, error_ptr);
+  return m_borrowed_frame_sp->GetVariableList(get_file_globals,
+                                              include_extended_vars, error_ptr);
 }
 
 VariableListSP
 BorrowedStackFrame::GetInScopeVariableList(bool get_file_globals,
+                                           bool include_extended_vars,
                                            bool must_have_valid_location) {
-  return m_borrowed_frame_sp->GetInScopeVariableList(get_file_globals,
-                                                     must_have_valid_location);
+  return m_borrowed_frame_sp->GetInScopeVariableList(
+      get_file_globals, include_extended_vars, must_have_valid_location);
 }
 
 ValueObjectSP BorrowedStackFrame::GetValueForVariableExpressionPath(
diff --git a/lldb/source/Target/StackFrame.cpp b/lldb/source/Target/StackFrame.cpp
index 340607e14abed..754516eaf9060 100644
--- a/lldb/source/Target/StackFrame.cpp
+++ b/lldb/source/Target/StackFrame.cpp
@@ -439,7 +439,11 @@ StackFrame::GetSymbolContext(SymbolContextItem resolve_scope) {
 }
 
 VariableList *StackFrame::GetVariableList(bool get_file_globals,
+                                          bool include_extended_vars,
                                           Status *error_ptr) {
+  // We don't have 'extended variables' in the base stack frame.
+  (void)include_extended_vars;
+
   std::lock_guard<std::recursive_mutex> guard(m_mutex);
   if (m_flags.IsClear(RESOLVED_VARIABLES)) {
     m_flags.Set(RESOLVED_VARIABLES);
@@ -490,7 +494,11 @@ VariableList *StackFrame::GetVariableList(bool get_file_globals,
 
 VariableListSP
 StackFrame::GetInScopeVariableList(bool get_file_globals,
+                                   bool include_extended_vars,
                                    bool must_have_valid_location) {
+  // We don't have 'extended variables' in the base stack frame.
+  (void)include_extended_vars;
+
   std::lock_guard<std::recursive_mutex> guard(m_mutex);
   // We can't fetch variable information for a history stack frame.
   if (IsHistorical())
@@ -1235,7 +1243,7 @@ StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
     if (IsHistorical()) {
       return valobj_sp;
     }
-    VariableList *var_list = GetVariableList(true, nullptr);
+    VariableList *var_list = GetVariableList(true, true, nullptr);
     if (var_list) {
       // Make sure the variable is a frame variable
       const uint32_t var_idx =
@@ -1856,7 +1864,12 @@ lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
   }
 
   const bool get_file_globals = false;
-  VariableList *variables = GetVariableList(get_file_globals, nullptr);
+  // Keep this as 'false' here because if we're inspecting a register, it's
+  // HIGHLY unlikely that we have an extended variable. Indeed, since we're not
+  // in a synthetic frame, it's probably actually impossible here.
+  const bool include_extended_vars = false;
+  VariableList *variables =
+      GetVariableList(get_file_globals, include_extended_vars, nullptr);
 
   if (!variables) {
     return ValueObjectSP();
diff --git a/lldb/test/API/functionalities/scripted_frame_provider/TestScriptedFrameProvider.py b/lldb/test/API/functionalities/scripted_frame_provider/TestScriptedFrameProvider.py
index 7dd74013b90f8..eea0143355753 100644
--- a/lldb/test/API/functionalities/scripted_frame_provider/TestScriptedFrameProvider.py
+++ b/lldb/test/API/functionalities/scripted_frame_provider/TestScriptedFrameProvider.py
@@ -769,17 +769,37 @@ def test_get_values(self):
         # Check that we can get variables from this frame.
         frame0 = thread.GetFrameAtIndex(0)
         self.assertIsNotNone(frame0)
-        # Get every variable visible at this point
-        variables = frame0.GetVariables(True, True, True, False)
-        self.assertTrue(variables.IsValid() and variables.GetSize() == 1)
+
+        # Ensure that we can get extended variables with `SetIncludeExtended`.
+        options = lldb.SBVariablesOptions()
+        options.SetIncludeExtended(True)
+        variables = frame0.GetVariables(options)
+        self.assertTrue(variables.IsValid())
+        self.assertTrue(variables.GetValueAtIndex(0).name == "_handler_one")
+
+        # Check the `frame variable` command(s) handle extended variables the
+        # way we expect by printing them.
+        self.expect("frame var", substrs=["variable_in_main", "_handler_one"])
+
+        # Then, try and run it without extended variables and ensure we don't
+        # get any, but we still get the others.
+        interp = self.dbg.GetCommandInterpreter()
+        command_result = lldb.SBCommandReturnObject()
+        result = interp.HandleCommand("frame var -e", command_result)
+        self.assertEqual(
+            result, lldb.eReturnStatusSuccessFinishResult, "frame var -e didn't succeed"
+        )
+        output = command_result.GetOutput()
+        self.assertIn("variable_in_main", output, "Didn't find a regular variable")
+        self.assertNotIn("_handler_one", output, "Found an extended variable")
 
         # Check that we can get values from paths. `_handler_one` is a special
         # value we provide through only our expression handler in the frame
-        # implementation.
+        # implementation. We can't evaluate expressions on the special value
+        # just because the test implementation doesn't handle it, and we
+        # delegate all expression handling to the implementation.
         one = frame0.GetValueForVariablePath("_handler_one")
         self.assertEqual(one.unsigned, 1)
-        var = frame0.GetValueForVariablePath("variable_in_main")
-        # The names won't necessarily match, but the values should (the frame renames the SBValue)
-        self.assertEqual(var.unsigned, variables.GetValueAtIndex(0).unsigned)
+        # Ensure I can still access and do arithmetic on regular variables.
         varp1 = frame0.GetValueForVariablePath("variable_in_main + 1")
         self.assertEqual(varp1.unsigned, 124)
diff --git a/lldb/test/API/functionalities/scripted_frame_provider/test_frame_providers.py b/lldb/test/API/functionalities/scripted_frame_provider/test_frame_providers.py
index 3a30e4fa96d6e..efdbe4cc4bf59 100644
--- a/lldb/test/API/functionalities/scripted_frame_provider/test_frame_providers.py
+++ b/lldb/test/API/functionalities/scripted_frame_provider/test_frame_providers.py
@@ -500,6 +500,8 @@ def get_variables(self):
         """"""
         out = lldb.SBValueList()
         out.Append(self.variable)
+        # Produce a fake value to be displayed.
+        out.Append(self.variable.CreateValueFromExpression("_handler_one", "(uint32_t)1"))
         return out
 
     def get_value_for_variable_expression(self, expr, options, error: lldb.SBError):



More information about the lldb-commits mailing list