[Lldb-commits] [lldb] cebb87e - [lldb] Enable use of dummy target from dwim-print

Dave Lee via lldb-commits lldb-commits at lists.llvm.org
Thu Dec 1 13:21:34 PST 2022


Author: Dave Lee
Date: 2022-12-01T13:21:24-08:00
New Revision: cebb87e7dce891b9a70ccb28e7da8c1fc71f2439

URL: https://github.com/llvm/llvm-project/commit/cebb87e7dce891b9a70ccb28e7da8c1fc71f2439
DIFF: https://github.com/llvm/llvm-project/commit/cebb87e7dce891b9a70ccb28e7da8c1fc71f2439.diff

LOG: [lldb] Enable use of dummy target from dwim-print

Allow `dwim-print` to evaluate expressions using the dummy target if no real
target exists.

This adds some parity to `expression`. With this, both of the following work:

```
lldb -o 'expr 1+2'
lldb -o 'dwim-print 1+2'
```

Differential Revision: https://reviews.llvm.org/D138960

Added: 
    

Modified: 
    lldb/source/Commands/CommandObjectDWIMPrint.cpp
    lldb/test/API/commands/dwim-print/TestDWIMPrint.py

Removed: 
    


################################################################################
diff  --git a/lldb/source/Commands/CommandObjectDWIMPrint.cpp b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
index 232e877f53d76..e15e723de5880 100644
--- a/lldb/source/Commands/CommandObjectDWIMPrint.cpp
+++ b/lldb/source/Commands/CommandObjectDWIMPrint.cpp
@@ -22,12 +22,11 @@ using namespace lldb;
 using namespace lldb_private;
 
 CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter)
-    : CommandObjectRaw(
-          interpreter, "dwim-print", "Print a variable or expression.",
-          "dwim-print [<variable-name> | <expression>]",
-          eCommandProcessMustBePaused | eCommandTryTargetAPILock |
-              eCommandRequiresFrame | eCommandProcessMustBeLaunched |
-              eCommandRequiresProcess) {}
+    : CommandObjectRaw(interpreter, "dwim-print",
+                       "Print a variable or expression.",
+                       "dwim-print [<variable-name> | <expression>]",
+                       eCommandProcessMustBePaused | eCommandTryTargetAPILock) {
+}
 
 bool CommandObjectDWIMPrint::DoExecute(StringRef expr,
                                        CommandReturnObject &result) {
@@ -40,14 +39,10 @@ bool CommandObjectDWIMPrint::DoExecute(StringRef expr,
     return false;
   }
 
-  // eCommandRequiresFrame guarantees a frame.
-  StackFrame *frame = m_exe_ctx.GetFramePtr();
-  assert(frame);
-
   auto verbosity = GetDebugger().GetDWIMPrintVerbosity();
 
-  // First, try `expr` as the name of a variable.
-  {
+  // First, try `expr` as the name of a frame variable.
+  if (StackFrame *frame = m_exe_ctx.GetFramePtr()) {
     auto valobj_sp = frame->FindVariable(ConstString(expr));
     if (valobj_sp && valobj_sp->GetError().Success()) {
       if (verbosity == eDWIMPrintVerbosityFull)
@@ -60,12 +55,13 @@ bool CommandObjectDWIMPrint::DoExecute(StringRef expr,
 
   // Second, also lastly, try `expr` as a source expression to evaluate.
   {
-    // eCommandRequiresProcess guarantees a target.
-    Target *target = m_exe_ctx.GetTargetPtr();
-    assert(target);
+    Target *target_ptr = m_exe_ctx.GetTargetPtr();
+    // Fallback to the dummy target, which can allow for expression evaluation.
+    Target &target = target_ptr ? *target_ptr : GetDummyTarget();
 
+    auto *exe_scope = m_exe_ctx.GetBestExecutionContextScope();
     ValueObjectSP valobj_sp;
-    if (target->EvaluateExpression(expr, frame, valobj_sp) ==
+    if (target.EvaluateExpression(expr, exe_scope, valobj_sp) ==
         eExpressionCompleted) {
       if (verbosity != eDWIMPrintVerbosityNone)
         result.AppendMessageWithFormatv("note: ran `expression -- {0}`", expr);

diff  --git a/lldb/test/API/commands/dwim-print/TestDWIMPrint.py b/lldb/test/API/commands/dwim-print/TestDWIMPrint.py
index 7bc1448441cc9..6b3266deaad86 100644
--- a/lldb/test/API/commands/dwim-print/TestDWIMPrint.py
+++ b/lldb/test/API/commands/dwim-print/TestDWIMPrint.py
@@ -10,11 +10,6 @@
 
 
 class TestCase(TestBase):
-    def setUp(self):
-        TestBase.setUp(self)
-        self.build()
-        lldbutil.run_to_name_breakpoint(self, "main")
-
     def _run_cmd(self, cmd: str) -> str:
         """Run the given lldb command and return its output."""
         result = lldb.SBCommandReturnObject()
@@ -51,18 +46,28 @@ def _expect_cmd(self, expr: str, base_cmd: str) -> None:
 
     def test_variables(self):
         """Test dwim-print with variables."""
+        self.build()
+        lldbutil.run_to_name_breakpoint(self, "main")
         vars = ("argc", "argv")
         for var in vars:
             self._expect_cmd(var, "frame variable")
 
     def test_variable_paths(self):
         """Test dwim-print with variable path expressions."""
+        self.build()
+        lldbutil.run_to_name_breakpoint(self, "main")
         exprs = ("&argc", "*argv", "argv[0]")
         for expr in exprs:
             self._expect_cmd(expr, "expression --")
 
     def test_expressions(self):
         """Test dwim-print with expressions."""
+        self.build()
+        lldbutil.run_to_name_breakpoint(self, "main")
         exprs = ("argc + 1", "(void)argc", "(int)abs(argc)")
         for expr in exprs:
             self._expect_cmd(expr, "expression --")
+
+    def test_dummy_target_expressions(self):
+        """Test dwim-print's ability to evaluate expressions without a target."""
+        self._expect_cmd("1 + 2", "expression --")


        


More information about the lldb-commits mailing list