1 //===-- CommandObjectDWIMPrint.cpp ------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CommandObjectDWIMPrint.h"
10 
11 #include "lldb/Core/ValueObject.h"
12 #include "lldb/Interpreter/CommandInterpreter.h"
13 #include "lldb/Interpreter/CommandObject.h"
14 #include "lldb/Interpreter/CommandReturnObject.h"
15 #include "lldb/Target/StackFrame.h"
16 #include "lldb/Utility/ConstString.h"
17 #include "lldb/lldb-enumerations.h"
18 #include "lldb/lldb-forward.h"
19 
20 using namespace llvm;
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter)
25     : CommandObjectRaw(interpreter, "dwim-print",
26                        "Print a variable or expression.",
27                        "dwim-print [<variable-name> | <expression>]",
28                        eCommandProcessMustBePaused | eCommandTryTargetAPILock) {
29 }
30 
31 bool CommandObjectDWIMPrint::DoExecute(StringRef expr,
32                                        CommandReturnObject &result) {
33   // Ignore leading and trailing whitespace.
34   expr = expr.trim();
35 
36   if (expr.empty()) {
37     result.AppendErrorWithFormatv("'{0}' takes a variable or expression",
38                                   m_cmd_name);
39     return false;
40   }
41 
42   auto verbosity = GetDebugger().GetDWIMPrintVerbosity();
43 
44   // First, try `expr` as the name of a frame variable.
45   if (StackFrame *frame = m_exe_ctx.GetFramePtr()) {
46     auto valobj_sp = frame->FindVariable(ConstString(expr));
47     if (valobj_sp && valobj_sp->GetError().Success()) {
48       if (verbosity == eDWIMPrintVerbosityFull)
49         result.AppendMessageWithFormatv("note: ran `frame variable {0}`", expr);
50       valobj_sp->Dump(result.GetOutputStream());
51       result.SetStatus(eReturnStatusSuccessFinishResult);
52       return true;
53     }
54   }
55 
56   // Second, also lastly, try `expr` as a source expression to evaluate.
57   {
58     Target *target_ptr = m_exe_ctx.GetTargetPtr();
59     // Fallback to the dummy target, which can allow for expression evaluation.
60     Target &target = target_ptr ? *target_ptr : GetDummyTarget();
61 
62     auto *exe_scope = m_exe_ctx.GetBestExecutionContextScope();
63     ValueObjectSP valobj_sp;
64     if (target.EvaluateExpression(expr, exe_scope, valobj_sp) ==
65         eExpressionCompleted) {
66       if (verbosity != eDWIMPrintVerbosityNone)
67         result.AppendMessageWithFormatv("note: ran `expression -- {0}`", expr);
68       valobj_sp->Dump(result.GetOutputStream());
69       result.SetStatus(eReturnStatusSuccessFinishResult);
70       return true;
71     } else {
72       if (valobj_sp)
73         result.SetError(valobj_sp->GetError());
74       else
75         result.AppendErrorWithFormatv(
76             "unknown error evaluating expression `{0}`", expr);
77       return false;
78     }
79   }
80 }
81