Skip to content

[lldb] Fallback to expression eval when Dump of variable fails in dwim-print #151374

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

Conversation

kastiglione
Copy link
Contributor

@kastiglione kastiglione commented Jul 30, 2025

Previously, when dwim-print finds a frame variables, it returns immediately after calling Dump, even if Dump returns an error. This is most likely to happen when evaluating an object description, ie po.

This changes dwim-print to continue on to expression evaluation when Dumping a variable returns an error . This is to allow for more complete diagnostics when po fails.

@llvmbot
Copy link
Member

llvmbot commented Jul 30, 2025

@llvm/pr-subscribers-lldb

Author: Dave Lee (kastiglione)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/151374.diff

1 Files Affected:

  • (modified) lldb/source/Commands/CommandObjectDWIMPrint.cpp (+20-18)
diff --git a/lldb/source/Commands/CommandObjectDWIMPrint.cpp b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
index a2c004d0ee97f..5b788d7d0c5a2 100644
--- a/lldb/source/Commands/CommandObjectDWIMPrint.cpp
+++ b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
@@ -23,6 +23,7 @@
 #include "lldb/lldb-enumerations.h"
 #include "lldb/lldb-forward.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Error.h"
 
 #include <regex>
 
@@ -132,27 +133,22 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
   };
 
   // Dump `valobj` according to whether `po` was requested or not.
-  auto dump_val_object = [&](ValueObject &valobj) {
+  auto dump_val_object = [&](ValueObject &valobj) -> Error {
     if (is_po) {
       StreamString temp_result_stream;
-      if (llvm::Error error = valobj.Dump(temp_result_stream, dump_options)) {
-        result.AppendError(toString(std::move(error)));
-        return;
-      }
+      if (Error err = valobj.Dump(temp_result_stream, dump_options))
+        return err;
       llvm::StringRef output = temp_result_stream.GetString();
       maybe_add_hint(output);
       result.GetOutputStream() << output;
     } else {
-      llvm::Error error =
-        valobj.Dump(result.GetOutputStream(), dump_options);
-      if (error) {
-        result.AppendError(toString(std::move(error)));
-        return;
-      }
+      if (Error err = valobj.Dump(result.GetOutputStream(), dump_options))
+        return err;
     }
     m_interpreter.PrintWarningsIfNecessary(result.GetOutputStream(),
                                            m_cmd_name);
     result.SetStatus(eReturnStatusSuccessFinishResult);
+    return Error::success();
   };
 
   // First, try `expr` as a _limited_ frame variable expression path: only the
@@ -186,8 +182,11 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
                                      expr);
       }
 
-      dump_val_object(*valobj_sp);
-      return;
+      bool failed = errorToBool(dump_val_object(*valobj_sp));
+      if (!failed)
+        return;
+
+      // Dump failed, continue on to expression evaluation.
     }
   }
 
@@ -196,8 +195,9 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
     if (auto *state = target.GetPersistentExpressionStateForLanguage(language))
       if (auto var_sp = state->GetVariable(expr))
         if (auto valobj_sp = var_sp->GetValueObject()) {
-          dump_val_object(*valobj_sp);
-          return;
+          bool failed = errorToBool(dump_val_object(*valobj_sp));
+          if (!failed)
+            return;
         }
 
   // Third, and lastly, try `expr` as a source expression to evaluate.
@@ -248,10 +248,12 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
       result.AppendNoteWithFormatv("ran `expression {0}{1}`", flags, expr);
     }
 
-    if (valobj_sp->GetError().GetError() != UserExpression::kNoResult)
-      dump_val_object(*valobj_sp);
-    else
+    if (valobj_sp->GetError().GetError() != UserExpression::kNoResult) {
+      if (Error err = dump_val_object(*valobj_sp))
+        result.SetError(std::move(err));
+    } else {
       result.SetStatus(eReturnStatusSuccessFinishNoResult);
+    }
 
     if (suppress_result)
       if (auto result_var_sp =

return;

// Dump failed, continue on to expression evaluation.
LLDB_LOG_ERROR(GetLog(LLDBLog::Expressions), std::move(err),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about the order of the arguments to this macro. The error comes first, then the format string, then the other parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup – error, format, params

@kastiglione kastiglione merged commit f23c10f into llvm:main Jul 31, 2025
9 checks passed
@kastiglione kastiglione deleted the lldb-Fallback-to-expression-eval-when-Dump-of-variable-fails-in-dwim-print branch July 31, 2025 17:28
kastiglione added a commit to swiftlang/llvm-project that referenced this pull request Jul 31, 2025
…m-print (llvm#151374)

Previously, when dwim-print finds a frame variables, it returns immediately after
calling `Dump`, even if `Dump` returns an error. This is most likely to happen when
evaluating an object description, ie `po`.

This changes dwim-print to continue on to expression evaluation when `Dump`ing a
variable returns an error . This is to allow for diagnostics that match `expression`.

(cherry picked from commit f23c10f)
krishna2803 pushed a commit to krishna2803/llvm-project that referenced this pull request Aug 12, 2025
…m-print (llvm#151374)

Previously, when dwim-print finds a frame variables, it returns immediately after
calling `Dump`, even if `Dump` returns an error. This is most likely to happen when
evaluating an object description, ie `po`.

This changes dwim-print to continue on to expression evaluation when `Dump`ing a
variable returns an error . This is to allow for diagnostics that match `expression`.
kastiglione added a commit that referenced this pull request Aug 15, 2025
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants