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 lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks( 56 GetCommandInterpreter(), lldb::eVariablePathCompletion, 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 if (valobj_sp->GetError().GetError() != UserExpression::kNoResult) 139 valobj_sp->Dump(result.GetOutputStream(), dump_options); 140 141 if (suppress_result) 142 if (auto result_var_sp = 143 target.GetPersistentVariable(valobj_sp->GetName())) { 144 auto language = valobj_sp->GetPreferredDisplayLanguage(); 145 if (auto *persistent_state = 146 target.GetPersistentExpressionStateForLanguage(language)) 147 persistent_state->RemovePersistentVariable(result_var_sp); 148 } 149 150 result.SetStatus(eReturnStatusSuccessFinishResult); 151 return true; 152 } else { 153 if (valobj_sp) 154 result.SetError(valobj_sp->GetError()); 155 else 156 result.AppendErrorWithFormatv( 157 "unknown error evaluating expression `{0}`", expr); 158 return false; 159 } 160 } 161 } 162