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/Interpreter/CommandInterpreter.h" 15 #include "lldb/Interpreter/CommandObject.h" 16 #include "lldb/Interpreter/CommandReturnObject.h" 17 #include "lldb/Interpreter/OptionGroupFormat.h" 18 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h" 19 #include "lldb/Target/StackFrame.h" 20 #include "lldb/Utility/ConstString.h" 21 #include "lldb/lldb-defines.h" 22 #include "lldb/lldb-enumerations.h" 23 #include "lldb/lldb-forward.h" 24 #include "llvm/ADT/StringRef.h" 25 #include "llvm/Support/FormatVariadic.h" 26 27 using namespace llvm; 28 using namespace lldb; 29 using namespace lldb_private; 30 31 CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter) 32 : CommandObjectRaw(interpreter, "dwim-print", 33 "Print a variable or expression.", 34 "dwim-print [<variable-name> | <expression>]", 35 eCommandProcessMustBePaused | eCommandTryTargetAPILock) { 36 37 CommandArgumentData var_name_arg(eArgTypeVarName, eArgRepeatPlain); 38 m_arguments.push_back({var_name_arg}); 39 40 m_option_group.Append(&m_format_options, 41 OptionGroupFormat::OPTION_GROUP_FORMAT | 42 OptionGroupFormat::OPTION_GROUP_GDB_FMT, 43 LLDB_OPT_SET_1); 44 StringRef exclude_expr_options[] = {"debug", "top-level"}; 45 m_option_group.Append(&m_expr_options, exclude_expr_options); 46 m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 47 m_option_group.Finalize(); 48 } 49 50 Options *CommandObjectDWIMPrint::GetOptions() { return &m_option_group; } 51 52 void CommandObjectDWIMPrint::HandleArgumentCompletion( 53 CompletionRequest &request, OptionElementVector &opt_element_vector) { 54 CommandCompletions::InvokeCommonCompletionCallbacks( 55 GetCommandInterpreter(), CommandCompletions::eVariablePathCompletion, 56 request, nullptr); 57 } 58 59 bool CommandObjectDWIMPrint::DoExecute(StringRef command, 60 CommandReturnObject &result) { 61 m_option_group.NotifyOptionParsingStarting(&m_exe_ctx); 62 OptionsWithRaw args{command}; 63 StringRef expr = args.GetRawPart(); 64 65 if (expr.empty()) { 66 result.AppendErrorWithFormatv("'{0}' takes a variable or expression", 67 m_cmd_name); 68 return false; 69 } 70 71 if (args.HasArgs()) { 72 if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group, 73 m_exe_ctx)) 74 return false; 75 } 76 77 // If the user has not specified, default to disabling persistent results. 78 if (m_expr_options.suppress_persistent_result == eLazyBoolCalculate) 79 m_expr_options.suppress_persistent_result = eLazyBoolYes; 80 bool suppress_result = m_expr_options.ShouldSuppressResult(m_varobj_options); 81 82 auto verbosity = GetDebugger().GetDWIMPrintVerbosity(); 83 84 Target *target_ptr = m_exe_ctx.GetTargetPtr(); 85 // Fallback to the dummy target, which can allow for expression evaluation. 86 Target &target = target_ptr ? *target_ptr : GetDummyTarget(); 87 88 EvaluateExpressionOptions eval_options = 89 m_expr_options.GetEvaluateExpressionOptions(target, m_varobj_options); 90 // This command manually removes the result variable, make sure expression 91 // evaluation doesn't do it first. 92 eval_options.SetSuppressPersistentResult(false); 93 94 DumpValueObjectOptions dump_options = m_varobj_options.GetAsDumpOptions( 95 m_expr_options.m_verbosity, m_format_options.GetFormat()); 96 dump_options.SetHideRootName(suppress_result); 97 98 StackFrame *frame = m_exe_ctx.GetFramePtr(); 99 100 // First, try `expr` as the name of a frame variable. 101 if (frame) { 102 auto valobj_sp = frame->FindVariable(ConstString(expr)); 103 if (valobj_sp && valobj_sp->GetError().Success()) { 104 if (!suppress_result) { 105 if (auto persisted_valobj = valobj_sp->Persist()) 106 valobj_sp = persisted_valobj; 107 } 108 109 if (verbosity == eDWIMPrintVerbosityFull) { 110 StringRef flags; 111 if (args.HasArgs()) 112 flags = args.GetArgString(); 113 result.AppendMessageWithFormatv("note: ran `frame variable {0}{1}`", 114 flags, expr); 115 } 116 117 valobj_sp->Dump(result.GetOutputStream(), dump_options); 118 result.SetStatus(eReturnStatusSuccessFinishResult); 119 return true; 120 } 121 } 122 123 // Second, also lastly, try `expr` as a source expression to evaluate. 124 { 125 auto *exe_scope = m_exe_ctx.GetBestExecutionContextScope(); 126 ValueObjectSP valobj_sp; 127 ExpressionResults expr_result = 128 target.EvaluateExpression(expr, exe_scope, valobj_sp, eval_options); 129 if (expr_result == eExpressionCompleted) { 130 if (verbosity != eDWIMPrintVerbosityNone) { 131 StringRef flags; 132 if (args.HasArgs()) 133 flags = args.GetArgStringWithDelimiter(); 134 result.AppendMessageWithFormatv("note: ran `expression {0}{1}`", flags, 135 expr); 136 } 137 138 valobj_sp->Dump(result.GetOutputStream(), dump_options); 139 140 if (suppress_result) 141 if (auto result_var_sp = 142 target.GetPersistentVariable(valobj_sp->GetName())) { 143 auto language = valobj_sp->GetPreferredDisplayLanguage(); 144 if (auto *persistent_state = 145 target.GetPersistentExpressionStateForLanguage(language)) 146 persistent_state->RemovePersistentVariable(result_var_sp); 147 } 148 149 result.SetStatus(eReturnStatusSuccessFinishResult); 150 return true; 151 } else { 152 if (valobj_sp) 153 result.SetError(valobj_sp->GetError()); 154 else 155 result.AppendErrorWithFormatv( 156 "unknown error evaluating expression `{0}`", expr); 157 return false; 158 } 159 } 160 } 161