LLDB  mainline
CommandObjectDWIMPrint.cpp
Go to the documentation of this file.
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 
10 
11 #include "lldb/Core/ValueObject.h"
15 #include "lldb/Target/StackFrame.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 
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());
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) ==
66  if (verbosity != eDWIMPrintVerbosityNone)
67  result.AppendMessageWithFormatv("note: ran `expression -- {0}`", expr);
68  valobj_sp->Dump(result.GetOutputStream());
70  return true;
71  } else {
72  if (valobj_sp)
73  result.SetError(valobj_sp->GetError());
74  else
76  "unknown error evaluating expression `{0}`", expr);
77  return false;
78  }
79  }
80 }
lldb_private::CommandObjectDWIMPrint::DoExecute
bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override
Definition: CommandObjectDWIMPrint.cpp:31
llvm
Definition: Debugger.h:51
lldb_private::CommandObject::GetDummyTarget
Target & GetDummyTarget()
Definition: CommandObject.cpp:680
lldb_private::CommandReturnObject::SetError
void SetError(const Status &error, const char *fallback_error_cstr=nullptr)
Definition: CommandReturnObject.cpp:107
lldb_private::CommandObject::m_exe_ctx
ExecutionContext m_exe_ctx
Definition: CommandObject.h:371
lldb_private::ExecutionContext::GetFramePtr
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Definition: ExecutionContext.h:408
StackFrame.h
CommandReturnObject.h
lldb_private::Target
Definition: Target.h:469
lldb_private::CommandReturnObject::AppendErrorWithFormatv
void AppendErrorWithFormatv(const char *format, Args &&... args)
Definition: CommandReturnObject.h:130
lldb_private::CommandReturnObject::SetStatus
void SetStatus(lldb::ReturnStatus status)
Definition: CommandReturnObject.cpp:127
lldb::eExpressionCompleted
@ eExpressionCompleted
Definition: lldb-enumerations.h:272
lldb_private::CommandReturnObject::GetOutputStream
Stream & GetOutputStream()
Definition: CommandReturnObject.h:46
lldb_private::ConstString
Definition: ConstString.h:39
lldb::eDWIMPrintVerbosityFull
@ eDWIMPrintVerbosityFull
Always print a message indicating how dwim-print is evaluating its expression.
Definition: lldb-enumerations.h:1220
lldb-enumerations.h
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:215
lldb_private::CommandObjectRaw
Definition: CommandObject.h:408
ValueObject.h
lldb_private::Debugger::GetDWIMPrintVerbosity
lldb::DWIMPrintVerbosity GetDWIMPrintVerbosity() const
Definition: Debugger.cpp:534
lldb_private::CommandReturnObject
Definition: CommandReturnObject.h:26
lldb_private::ExecutionContext::GetBestExecutionContextScope
ExecutionContextScope * GetBestExecutionContextScope() const
Definition: ExecutionContext.cpp:214
lldb_private::CommandObject::m_cmd_name
std::string m_cmd_name
Definition: CommandObject.h:373
CommandObject.h
lldb::eReturnStatusSuccessFinishResult
@ eReturnStatusSuccessFinishResult
Definition: lldb-enumerations.h:262
lldb_private::Target::EvaluateExpression
lldb::ExpressionResults EvaluateExpression(llvm::StringRef expression, ExecutionContextScope *exe_scope, lldb::ValueObjectSP &result_valobj_sp, const EvaluateExpressionOptions &options=EvaluateExpressionOptions(), std::string *fixed_expression=nullptr, ValueObject *ctx_obj=nullptr)
Definition: Target.cpp:2544
lldb-forward.h
CommandObjectDWIMPrint.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
CommandInterpreter.h
ConstString.h
lldb_private::StackFrame
Definition: StackFrame.h:40
lldb::eDWIMPrintVerbosityNone
@ eDWIMPrintVerbosityNone
Run dwim-print with no verbosity.
Definition: lldb-enumerations.h:1215
lldb
Definition: SBAddress.h:15
lldb_private::CommandReturnObject::AppendMessageWithFormatv
void void AppendMessageWithFormatv(const char *format, Args &&... args)
Definition: CommandReturnObject.h:120
lldb_private::ExecutionContext::GetTargetPtr
Target * GetTargetPtr() const
Returns a pointer to the target object.
Definition: ExecutionContext.cpp:198
lldb_private::CommandObject::GetDebugger
Debugger & GetDebugger()
Definition: CommandObject.cpp:53