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/DataFormatters/DumpValueObjectOptions.h" 13 #include "lldb/Expression/ExpressionVariable.h" 14 #include "lldb/Expression/UserExpression.h" 15 #include "lldb/Interpreter/CommandInterpreter.h" 16 #include "lldb/Interpreter/CommandObject.h" 17 #include "lldb/Interpreter/CommandReturnObject.h" 18 #include "lldb/Interpreter/OptionGroupFormat.h" 19 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h" 20 #include "lldb/Target/StackFrame.h" 21 #include "lldb/Utility/ConstString.h" 22 #include "lldb/lldb-defines.h" 23 #include "lldb/lldb-enumerations.h" 24 #include "lldb/lldb-forward.h" 25 #include "llvm/ADT/StringRef.h" 26 #include "llvm/Support/FormatVariadic.h" 27 28 using namespace llvm; 29 using namespace lldb; 30 using namespace lldb_private; 31 32 CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter) 33 : CommandObjectRaw(interpreter, "dwim-print", 34 "Print a variable or expression.", 35 "dwim-print [<variable-name> | <expression>]", 36 eCommandProcessMustBePaused | eCommandTryTargetAPILock) { 37 38 CommandArgumentData var_name_arg(eArgTypeVarName, eArgRepeatPlain); 39 m_arguments.push_back({var_name_arg}); 40 41 m_option_group.Append(&m_format_options, 42 OptionGroupFormat::OPTION_GROUP_FORMAT | 43 OptionGroupFormat::OPTION_GROUP_GDB_FMT, 44 LLDB_OPT_SET_1); 45 StringRef exclude_expr_options[] = {"debug", "top-level"}; 46 m_option_group.Append(&m_expr_options, exclude_expr_options); 47 m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 48 m_option_group.Finalize(); 49 } 50 51 Options *CommandObjectDWIMPrint::GetOptions() { return &m_option_group; } 52 53 void CommandObjectDWIMPrint::HandleArgumentCompletion( 54 CompletionRequest &request, OptionElementVector &opt_element_vector) { 55 CommandCompletions::InvokeCommonCompletionCallbacks( 56 GetCommandInterpreter(), CommandCompletions::eVariablePathCompletion, 57 request, nullptr); 58 } 59 60 bool CommandObjectDWIMPrint::DoExecute(StringRef command, 61 CommandReturnObject &result) { 62 m_option_group.NotifyOptionParsingStarting(&m_exe_ctx); 63 OptionsWithRaw args{command}; 64 StringRef expr = args.GetRawPart(); 65 66 if (expr.empty()) { 67 result.AppendErrorWithFormatv("'{0}' takes a variable or expression", 68 m_cmd_name); 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. 79 if (m_expr_options.suppress_persistent_result == eLazyBoolCalculate) 80 m_expr_options.suppress_persistent_result = eLazyBoolYes; 81 bool suppress_result = m_expr_options.ShouldSuppressResult(m_varobj_options); 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 = 90 m_expr_options.GetEvaluateExpressionOptions(target, m_varobj_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 95 DumpValueObjectOptions dump_options = m_varobj_options.GetAsDumpOptions( 96 m_expr_options.m_verbosity, m_format_options.GetFormat()); 97 dump_options.SetHideRootName(suppress_result); 98 99 StackFrame *frame = m_exe_ctx.GetFramePtr(); 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); 119 result.SetStatus(eReturnStatusSuccessFinishResult); 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 = 147 target.GetPersistentExpressionStateForLanguage(language)) 148 persistent_state->RemovePersistentVariable(result_var_sp); 149 } 150 151 result.SetStatus(eReturnStatusSuccessFinishResult); 152 return true; 153 } else { 154 if (valobj_sp) 155 result.SetError(valobj_sp->GetError()); 156 else 157 result.AppendErrorWithFormatv( 158 "unknown error evaluating expression `{0}`", expr); 159 return false; 160 } 161 } 162 } 163