1bdd1243dSDimitry Andric //===-- CommandObjectDWIMPrint.cpp ------------------------------*- C++ -*-===//
2bdd1243dSDimitry Andric //
3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bdd1243dSDimitry Andric //
7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8bdd1243dSDimitry Andric
9bdd1243dSDimitry Andric #include "CommandObjectDWIMPrint.h"
10bdd1243dSDimitry Andric
11bdd1243dSDimitry Andric #include "lldb/Core/ValueObject.h"
12fe013be4SDimitry Andric #include "lldb/DataFormatters/DumpValueObjectOptions.h"
13fe013be4SDimitry Andric #include "lldb/Expression/ExpressionVariable.h"
14fe013be4SDimitry Andric #include "lldb/Expression/UserExpression.h"
15bdd1243dSDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
16bdd1243dSDimitry Andric #include "lldb/Interpreter/CommandObject.h"
17bdd1243dSDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
18fe013be4SDimitry Andric #include "lldb/Interpreter/OptionGroupFormat.h"
19fe013be4SDimitry Andric #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
20bdd1243dSDimitry Andric #include "lldb/Target/StackFrame.h"
21bdd1243dSDimitry Andric #include "lldb/Utility/ConstString.h"
22fe013be4SDimitry Andric #include "lldb/lldb-defines.h"
23bdd1243dSDimitry Andric #include "lldb/lldb-enumerations.h"
24bdd1243dSDimitry Andric #include "lldb/lldb-forward.h"
25fe013be4SDimitry Andric #include "llvm/ADT/StringRef.h"
26fe013be4SDimitry Andric #include "llvm/Support/FormatVariadic.h"
27bdd1243dSDimitry Andric
28*c9157d92SDimitry Andric #include <regex>
29*c9157d92SDimitry Andric
30bdd1243dSDimitry Andric using namespace llvm;
31bdd1243dSDimitry Andric using namespace lldb;
32bdd1243dSDimitry Andric using namespace lldb_private;
33bdd1243dSDimitry Andric
CommandObjectDWIMPrint(CommandInterpreter & interpreter)34bdd1243dSDimitry Andric CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter)
35bdd1243dSDimitry Andric : CommandObjectRaw(interpreter, "dwim-print",
36bdd1243dSDimitry Andric "Print a variable or expression.",
37bdd1243dSDimitry Andric "dwim-print [<variable-name> | <expression>]",
38bdd1243dSDimitry Andric eCommandProcessMustBePaused | eCommandTryTargetAPILock) {
39fe013be4SDimitry Andric
40fe013be4SDimitry Andric CommandArgumentData var_name_arg(eArgTypeVarName, eArgRepeatPlain);
41fe013be4SDimitry Andric m_arguments.push_back({var_name_arg});
42fe013be4SDimitry Andric
43fe013be4SDimitry Andric m_option_group.Append(&m_format_options,
44fe013be4SDimitry Andric OptionGroupFormat::OPTION_GROUP_FORMAT |
45fe013be4SDimitry Andric OptionGroupFormat::OPTION_GROUP_GDB_FMT,
46fe013be4SDimitry Andric LLDB_OPT_SET_1);
47fe013be4SDimitry Andric StringRef exclude_expr_options[] = {"debug", "top-level"};
48fe013be4SDimitry Andric m_option_group.Append(&m_expr_options, exclude_expr_options);
49fe013be4SDimitry Andric m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
50fe013be4SDimitry Andric m_option_group.Finalize();
51bdd1243dSDimitry Andric }
52bdd1243dSDimitry Andric
GetOptions()53fe013be4SDimitry Andric Options *CommandObjectDWIMPrint::GetOptions() { return &m_option_group; }
54fe013be4SDimitry Andric
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)55fe013be4SDimitry Andric void CommandObjectDWIMPrint::HandleArgumentCompletion(
56fe013be4SDimitry Andric CompletionRequest &request, OptionElementVector &opt_element_vector) {
57fe013be4SDimitry Andric lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
58fe013be4SDimitry Andric GetCommandInterpreter(), lldb::eVariablePathCompletion, request, nullptr);
59fe013be4SDimitry Andric }
60fe013be4SDimitry Andric
DoExecute(StringRef command,CommandReturnObject & result)61*c9157d92SDimitry Andric void CommandObjectDWIMPrint::DoExecute(StringRef command,
62bdd1243dSDimitry Andric CommandReturnObject &result) {
63fe013be4SDimitry Andric m_option_group.NotifyOptionParsingStarting(&m_exe_ctx);
64fe013be4SDimitry Andric OptionsWithRaw args{command};
65fe013be4SDimitry Andric StringRef expr = args.GetRawPart();
66bdd1243dSDimitry Andric
67bdd1243dSDimitry Andric if (expr.empty()) {
68bdd1243dSDimitry Andric result.AppendErrorWithFormatv("'{0}' takes a variable or expression",
69bdd1243dSDimitry Andric m_cmd_name);
70*c9157d92SDimitry Andric return;
71bdd1243dSDimitry Andric }
72bdd1243dSDimitry Andric
73fe013be4SDimitry Andric if (args.HasArgs()) {
74fe013be4SDimitry Andric if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group,
75fe013be4SDimitry Andric m_exe_ctx))
76*c9157d92SDimitry Andric return;
77fe013be4SDimitry Andric }
78fe013be4SDimitry Andric
79fe013be4SDimitry Andric // If the user has not specified, default to disabling persistent results.
80fe013be4SDimitry Andric if (m_expr_options.suppress_persistent_result == eLazyBoolCalculate)
81fe013be4SDimitry Andric m_expr_options.suppress_persistent_result = eLazyBoolYes;
82fe013be4SDimitry Andric bool suppress_result = m_expr_options.ShouldSuppressResult(m_varobj_options);
83fe013be4SDimitry Andric
84bdd1243dSDimitry Andric auto verbosity = GetDebugger().GetDWIMPrintVerbosity();
85bdd1243dSDimitry Andric
86fe013be4SDimitry Andric Target *target_ptr = m_exe_ctx.GetTargetPtr();
87fe013be4SDimitry Andric // Fallback to the dummy target, which can allow for expression evaluation.
88fe013be4SDimitry Andric Target &target = target_ptr ? *target_ptr : GetDummyTarget();
89fe013be4SDimitry Andric
90fe013be4SDimitry Andric EvaluateExpressionOptions eval_options =
91fe013be4SDimitry Andric m_expr_options.GetEvaluateExpressionOptions(target, m_varobj_options);
92fe013be4SDimitry Andric // This command manually removes the result variable, make sure expression
93fe013be4SDimitry Andric // evaluation doesn't do it first.
94fe013be4SDimitry Andric eval_options.SetSuppressPersistentResult(false);
95fe013be4SDimitry Andric
96fe013be4SDimitry Andric DumpValueObjectOptions dump_options = m_varobj_options.GetAsDumpOptions(
97fe013be4SDimitry Andric m_expr_options.m_verbosity, m_format_options.GetFormat());
98fe013be4SDimitry Andric dump_options.SetHideRootName(suppress_result);
99fe013be4SDimitry Andric
100*c9157d92SDimitry Andric bool is_po = m_varobj_options.use_objc;
101*c9157d92SDimitry Andric
102fe013be4SDimitry Andric StackFrame *frame = m_exe_ctx.GetFramePtr();
103fe013be4SDimitry Andric
104*c9157d92SDimitry Andric // Either Swift was explicitly specified, or the frame is Swift.
105*c9157d92SDimitry Andric lldb::LanguageType language = m_expr_options.language;
106*c9157d92SDimitry Andric if (language == lldb::eLanguageTypeUnknown && frame)
107*c9157d92SDimitry Andric language = frame->GuessLanguage();
108*c9157d92SDimitry Andric
109*c9157d92SDimitry Andric // Add a hint if object description was requested, but no description
110*c9157d92SDimitry Andric // function was implemented.
111*c9157d92SDimitry Andric auto maybe_add_hint = [&](llvm::StringRef output) {
112*c9157d92SDimitry Andric // Identify the default output of object description for Swift and
113*c9157d92SDimitry Andric // Objective-C
114*c9157d92SDimitry Andric // "<Name: 0x...>. The regex is:
115*c9157d92SDimitry Andric // - Start with "<".
116*c9157d92SDimitry Andric // - Followed by 1 or more non-whitespace characters.
117*c9157d92SDimitry Andric // - Followed by ": 0x".
118*c9157d92SDimitry Andric // - Followed by 5 or more hex digits.
119*c9157d92SDimitry Andric // - Followed by ">".
120*c9157d92SDimitry Andric // - End with zero or more whitespace characters.
121*c9157d92SDimitry Andric const std::regex swift_class_regex("^<\\S+: 0x[[:xdigit:]]{5,}>\\s*$");
122*c9157d92SDimitry Andric
123*c9157d92SDimitry Andric if (GetDebugger().GetShowDontUsePoHint() && target_ptr &&
124*c9157d92SDimitry Andric (language == lldb::eLanguageTypeSwift ||
125*c9157d92SDimitry Andric language == lldb::eLanguageTypeObjC) &&
126*c9157d92SDimitry Andric std::regex_match(output.data(), swift_class_regex)) {
127*c9157d92SDimitry Andric
128*c9157d92SDimitry Andric static bool note_shown = false;
129*c9157d92SDimitry Andric if (note_shown)
130*c9157d92SDimitry Andric return;
131*c9157d92SDimitry Andric
132*c9157d92SDimitry Andric result.GetOutputStream()
133*c9157d92SDimitry Andric << "note: object description requested, but type doesn't implement "
134*c9157d92SDimitry Andric "a custom object description. Consider using \"p\" instead of "
135*c9157d92SDimitry Andric "\"po\" (this note will only be shown once per debug session).\n";
136*c9157d92SDimitry Andric note_shown = true;
137*c9157d92SDimitry Andric }
138*c9157d92SDimitry Andric };
139*c9157d92SDimitry Andric
140bdd1243dSDimitry Andric // First, try `expr` as the name of a frame variable.
141fe013be4SDimitry Andric if (frame) {
142bdd1243dSDimitry Andric auto valobj_sp = frame->FindVariable(ConstString(expr));
143bdd1243dSDimitry Andric if (valobj_sp && valobj_sp->GetError().Success()) {
144fe013be4SDimitry Andric if (!suppress_result) {
145fe013be4SDimitry Andric if (auto persisted_valobj = valobj_sp->Persist())
146fe013be4SDimitry Andric valobj_sp = persisted_valobj;
147fe013be4SDimitry Andric }
148fe013be4SDimitry Andric
149fe013be4SDimitry Andric if (verbosity == eDWIMPrintVerbosityFull) {
150fe013be4SDimitry Andric StringRef flags;
151fe013be4SDimitry Andric if (args.HasArgs())
152fe013be4SDimitry Andric flags = args.GetArgString();
153fe013be4SDimitry Andric result.AppendMessageWithFormatv("note: ran `frame variable {0}{1}`",
154fe013be4SDimitry Andric flags, expr);
155fe013be4SDimitry Andric }
156fe013be4SDimitry Andric
157*c9157d92SDimitry Andric if (is_po) {
158*c9157d92SDimitry Andric StreamString temp_result_stream;
159*c9157d92SDimitry Andric valobj_sp->Dump(temp_result_stream, dump_options);
160*c9157d92SDimitry Andric llvm::StringRef output = temp_result_stream.GetString();
161*c9157d92SDimitry Andric maybe_add_hint(output);
162*c9157d92SDimitry Andric result.GetOutputStream() << output;
163*c9157d92SDimitry Andric } else {
164fe013be4SDimitry Andric valobj_sp->Dump(result.GetOutputStream(), dump_options);
165*c9157d92SDimitry Andric }
166bdd1243dSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult);
167*c9157d92SDimitry Andric return;
168bdd1243dSDimitry Andric }
169bdd1243dSDimitry Andric }
170bdd1243dSDimitry Andric
171bdd1243dSDimitry Andric // Second, also lastly, try `expr` as a source expression to evaluate.
172bdd1243dSDimitry Andric {
173bdd1243dSDimitry Andric auto *exe_scope = m_exe_ctx.GetBestExecutionContextScope();
174bdd1243dSDimitry Andric ValueObjectSP valobj_sp;
175*c9157d92SDimitry Andric std::string fixed_expression;
176*c9157d92SDimitry Andric
177*c9157d92SDimitry Andric ExpressionResults expr_result = target.EvaluateExpression(
178*c9157d92SDimitry Andric expr, exe_scope, valobj_sp, eval_options, &fixed_expression);
179*c9157d92SDimitry Andric
180*c9157d92SDimitry Andric // Only mention Fix-Its if the expression evaluator applied them.
181*c9157d92SDimitry Andric // Compiler errors refer to the final expression after applying Fix-It(s).
182*c9157d92SDimitry Andric if (!fixed_expression.empty() && target.GetEnableNotifyAboutFixIts()) {
183*c9157d92SDimitry Andric Stream &error_stream = result.GetErrorStream();
184*c9157d92SDimitry Andric error_stream << " Evaluated this expression after applying Fix-It(s):\n";
185*c9157d92SDimitry Andric error_stream << " " << fixed_expression << "\n";
186*c9157d92SDimitry Andric }
187*c9157d92SDimitry Andric
188fe013be4SDimitry Andric if (expr_result == eExpressionCompleted) {
189fe013be4SDimitry Andric if (verbosity != eDWIMPrintVerbosityNone) {
190fe013be4SDimitry Andric StringRef flags;
191fe013be4SDimitry Andric if (args.HasArgs())
192fe013be4SDimitry Andric flags = args.GetArgStringWithDelimiter();
193fe013be4SDimitry Andric result.AppendMessageWithFormatv("note: ran `expression {0}{1}`", flags,
194fe013be4SDimitry Andric expr);
195fe013be4SDimitry Andric }
196fe013be4SDimitry Andric
197*c9157d92SDimitry Andric if (valobj_sp->GetError().GetError() != UserExpression::kNoResult) {
198*c9157d92SDimitry Andric if (is_po) {
199*c9157d92SDimitry Andric StreamString temp_result_stream;
200*c9157d92SDimitry Andric valobj_sp->Dump(temp_result_stream, dump_options);
201*c9157d92SDimitry Andric llvm::StringRef output = temp_result_stream.GetString();
202*c9157d92SDimitry Andric maybe_add_hint(output);
203*c9157d92SDimitry Andric result.GetOutputStream() << output;
204*c9157d92SDimitry Andric } else {
205fe013be4SDimitry Andric valobj_sp->Dump(result.GetOutputStream(), dump_options);
206*c9157d92SDimitry Andric }
207*c9157d92SDimitry Andric }
208fe013be4SDimitry Andric
209fe013be4SDimitry Andric if (suppress_result)
210fe013be4SDimitry Andric if (auto result_var_sp =
211fe013be4SDimitry Andric target.GetPersistentVariable(valobj_sp->GetName())) {
212fe013be4SDimitry Andric auto language = valobj_sp->GetPreferredDisplayLanguage();
213fe013be4SDimitry Andric if (auto *persistent_state =
214fe013be4SDimitry Andric target.GetPersistentExpressionStateForLanguage(language))
215fe013be4SDimitry Andric persistent_state->RemovePersistentVariable(result_var_sp);
216fe013be4SDimitry Andric }
217fe013be4SDimitry Andric
218bdd1243dSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult);
219bdd1243dSDimitry Andric } else {
220bdd1243dSDimitry Andric if (valobj_sp)
221bdd1243dSDimitry Andric result.SetError(valobj_sp->GetError());
222bdd1243dSDimitry Andric else
223bdd1243dSDimitry Andric result.AppendErrorWithFormatv(
224bdd1243dSDimitry Andric "unknown error evaluating expression `{0}`", expr);
225bdd1243dSDimitry Andric }
226bdd1243dSDimitry Andric }
227bdd1243dSDimitry Andric }
228