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