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
22#include "lldb/lldb-defines.h"
24#include "lldb/lldb-forward.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/Support/FormatVariadic.h"
27
28using namespace llvm;
29using namespace lldb;
30using namespace lldb_private;
31
33 : CommandObjectRaw(interpreter, "dwim-print",
34 "Print a variable or expression.",
35 "dwim-print [<variable-name> | <expression>]",
36 eCommandProcessMustBePaused | eCommandTryTargetAPILock) {
37
39 m_arguments.push_back({var_name_arg});
40
45 StringRef exclude_expr_options[] = {"debug", "top-level"};
46 m_option_group.Append(&m_expr_options, exclude_expr_options);
49}
50
52
54 CompletionRequest &request, OptionElementVector &opt_element_vector) {
57 request, nullptr);
58}
59
60bool CommandObjectDWIMPrint::DoExecute(StringRef command,
61 CommandReturnObject &result) {
63 OptionsWithRaw args{command};
64 StringRef expr = args.GetRawPart();
65
66 if (expr.empty()) {
67 result.AppendErrorWithFormatv("'{0}' takes a variable or expression",
69 return false;
70 }
71
72 if (args.HasArgs()) {
73 if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group,
74 m_exe_ctx))
75 return false;
76 }
77
78 // If the user has not specified, default to disabling persistent results.
82
83 auto verbosity = GetDebugger().GetDWIMPrintVerbosity();
84
85 Target *target_ptr = m_exe_ctx.GetTargetPtr();
86 // Fallback to the dummy target, which can allow for expression evaluation.
87 Target &target = target_ptr ? *target_ptr : GetDummyTarget();
88
89 EvaluateExpressionOptions eval_options =
91 // This command manually removes the result variable, make sure expression
92 // evaluation doesn't do it first.
93 eval_options.SetSuppressPersistentResult(false);
94
97 dump_options.SetHideRootName(suppress_result);
98
100
101 // First, try `expr` as the name of a frame variable.
102 if (frame) {
103 auto valobj_sp = frame->FindVariable(ConstString(expr));
104 if (valobj_sp && valobj_sp->GetError().Success()) {
105 if (!suppress_result) {
106 if (auto persisted_valobj = valobj_sp->Persist())
107 valobj_sp = persisted_valobj;
108 }
109
110 if (verbosity == eDWIMPrintVerbosityFull) {
111 StringRef flags;
112 if (args.HasArgs())
113 flags = args.GetArgString();
114 result.AppendMessageWithFormatv("note: ran `frame variable {0}{1}`",
115 flags, expr);
116 }
117
118 valobj_sp->Dump(result.GetOutputStream(), dump_options);
120 return true;
121 }
122 }
123
124 // Second, also lastly, try `expr` as a source expression to evaluate.
125 {
126 auto *exe_scope = m_exe_ctx.GetBestExecutionContextScope();
127 ValueObjectSP valobj_sp;
128 ExpressionResults expr_result =
129 target.EvaluateExpression(expr, exe_scope, valobj_sp, eval_options);
130 if (expr_result == eExpressionCompleted) {
131 if (verbosity != eDWIMPrintVerbosityNone) {
132 StringRef flags;
133 if (args.HasArgs())
134 flags = args.GetArgStringWithDelimiter();
135 result.AppendMessageWithFormatv("note: ran `expression {0}{1}`", flags,
136 expr);
137 }
138
139 if (valobj_sp->GetError().GetError() != UserExpression::kNoResult)
140 valobj_sp->Dump(result.GetOutputStream(), dump_options);
141
142 if (suppress_result)
143 if (auto result_var_sp =
144 target.GetPersistentVariable(valobj_sp->GetName())) {
145 auto language = valobj_sp->GetPreferredDisplayLanguage();
146 if (auto *persistent_state =
148 persistent_state->RemovePersistentVariable(result_var_sp);
149 }
150
152 return true;
153 } else {
154 if (valobj_sp)
155 result.SetError(valobj_sp->GetError());
156 else
158 "unknown error evaluating expression `{0}`", expr);
159 return false;
160 }
161 }
162}
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override
CommandObjectExpression::CommandOptions m_expr_options
CommandObjectDWIMPrint(CommandInterpreter &interpreter)
OptionGroupValueObjectDisplay m_varobj_options
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The input array contains a parsed version of the line.
bool ShouldSuppressResult(const OptionGroupValueObjectDisplay &display_opts) const
LanguageRuntimeDescriptionDisplayVerbosity m_verbosity
EvaluateExpressionOptions GetEvaluateExpressionOptions(const Target &target, const OptionGroupValueObjectDisplay &display_opts)
Return the appropriate expression options used for evaluating the expression in the given target.
bool ParseOptionsAndNotify(Args &args, CommandReturnObject &result, OptionGroupOptions &group_options, ExecutionContext &exe_ctx)
ExecutionContext m_exe_ctx
std::vector< CommandArgumentEntry > m_arguments
CommandInterpreter & GetCommandInterpreter()
void AppendErrorWithFormatv(const char *format, Args &&... args)
void SetStatus(lldb::ReturnStatus status)
void void AppendMessageWithFormatv(const char *format, Args &&... args)
void SetError(const Status &error, const char *fallback_error_cstr=nullptr)
"lldb/Utility/ArgCompletionRequest.h"
A uniqued constant string class.
Definition: ConstString.h:40
lldb::DWIMPrintVerbosity GetDWIMPrintVerbosity() const
Definition: Debugger.cpp:552
DumpValueObjectOptions & SetHideRootName(bool hide_root_name)
ExecutionContextScope * GetBestExecutionContextScope() const
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Target * GetTargetPtr() const
Returns a pointer to the target object.
static const uint32_t OPTION_GROUP_GDB_FMT
static const uint32_t OPTION_GROUP_FORMAT
void Append(OptionGroup *group)
Append options from a OptionGroup class.
Definition: Options.cpp:755
DumpValueObjectOptions GetAsDumpOptions(LanguageRuntimeDescriptionDisplayVerbosity lang_descr_verbosity=eLanguageRuntimeDescriptionDisplayVerbosityFull, lldb::Format format=lldb::eFormatDefault, lldb::TypeSummaryImplSP summary_sp=lldb::TypeSummaryImplSP())
A pair of an option list with a 'raw' string as a suffix.
Definition: Args.h:315
const std::string & GetRawPart() const
Returns the raw suffix part of the parsed string.
Definition: Args.h:364
A command line option parsing protocol class.
Definition: Options.h:58
void NotifyOptionParsingStarting(ExecutionContext *execution_context)
Definition: Options.cpp:33
This base class provides an interface to stack frames.
Definition: StackFrame.h:41
lldb::ValueObjectSP FindVariable(ConstString name)
Attempt to reconstruct the ValueObject for a variable with a given name from within the current Stack...
PersistentExpressionState * GetPersistentExpressionStateForLanguage(lldb::LanguageType language)
Definition: Target.cpp:2406
lldb::ExpressionVariableSP GetPersistentVariable(ConstString name)
Definition: Target.cpp:2626
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:2553
static const Status::ValueType kNoResult
ValueObject::GetError() returns this if there is no result from the expression.
#define LLDB_OPT_SET_1
Definition: lldb-defines.h:102
#define LLDB_OPT_SET_ALL
Definition: lldb-defines.h:101
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
std::vector< OptionArgElement > OptionElementVector
Definition: Options.h:43
Definition: SBAddress.h:15
@ eDWIMPrintVerbosityFull
Always print a message indicating how dwim-print is evaluating its expression.
@ eDWIMPrintVerbosityNone
Run dwim-print with no verbosity.
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
@ eReturnStatusSuccessFinishResult
Definition: Debugger.h:52
Used to build individual command argument lists.
Definition: CommandObject.h:93