|
| 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/Interpreter/CommandInterpreter.h" |
| 14 | +#include "lldb/Interpreter/CommandObject.h" |
| 15 | +#include "lldb/Interpreter/CommandReturnObject.h" |
| 16 | +#include "lldb/Interpreter/OptionGroupFormat.h" |
| 17 | +#include "lldb/Interpreter/OptionGroupValueObjectDisplay.h" |
| 18 | +#include "lldb/Target/StackFrame.h" |
| 19 | +#include "lldb/Utility/ConstString.h" |
| 20 | +#include "lldb/lldb-defines.h" |
| 21 | +#include "lldb/lldb-enumerations.h" |
| 22 | +#include "lldb/lldb-forward.h" |
| 23 | +#include "llvm/ADT/StringRef.h" |
| 24 | +#include "llvm/Support/FormatVariadic.h" |
| 25 | + |
| 26 | +using namespace llvm; |
| 27 | +using namespace lldb; |
| 28 | +using namespace lldb_private; |
| 29 | + |
| 30 | +CommandObjectDWIMPrint::CommandObjectDWIMPrint(CommandInterpreter &interpreter) |
| 31 | + : CommandObjectRaw(interpreter, "dwim-print", |
| 32 | + "Print a variable or expression.", |
| 33 | + "dwim-print [<variable-name> | <expression>]", |
| 34 | + eCommandProcessMustBePaused | eCommandTryTargetAPILock) { |
| 35 | + m_option_group.Append(&m_format_options, |
| 36 | + OptionGroupFormat::OPTION_GROUP_FORMAT | |
| 37 | + OptionGroupFormat::OPTION_GROUP_GDB_FMT, |
| 38 | + LLDB_OPT_SET_1); |
| 39 | + m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 40 | + m_option_group.Finalize(); |
| 41 | +} |
| 42 | + |
| 43 | +Options *CommandObjectDWIMPrint::GetOptions() { return &m_option_group; } |
| 44 | + |
| 45 | +bool CommandObjectDWIMPrint::DoExecute(StringRef command, |
| 46 | + CommandReturnObject &result) { |
| 47 | + m_option_group.NotifyOptionParsingStarting(&m_exe_ctx); |
| 48 | + OptionsWithRaw args{command}; |
| 49 | + StringRef expr = args.GetRawPart(); |
| 50 | + |
| 51 | + if (args.HasArgs()) { |
| 52 | + if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group, |
| 53 | + m_exe_ctx)) |
| 54 | + return false; |
| 55 | + } else if (command.empty()) { |
| 56 | + result.AppendErrorWithFormatv("'{0}' takes a variable or expression", |
| 57 | + m_cmd_name); |
| 58 | + return false; |
| 59 | + } |
| 60 | + auto verbosity = GetDebugger().GetDWIMPrintVerbosity(); |
| 61 | + |
| 62 | + DumpValueObjectOptions dump_options = m_varobj_options.GetAsDumpOptions( |
| 63 | + eLanguageRuntimeDescriptionDisplayVerbosityFull, |
| 64 | + m_format_options.GetFormat()); |
| 65 | + |
| 66 | + // First, try `expr` as the name of a frame variable. |
| 67 | + if (StackFrame *frame = m_exe_ctx.GetFramePtr()) { |
| 68 | + auto valobj_sp = frame->FindVariable(ConstString(expr)); |
| 69 | + if (valobj_sp && valobj_sp->GetError().Success()) { |
| 70 | + if (verbosity == eDWIMPrintVerbosityFull) { |
| 71 | + StringRef flags; |
| 72 | + if (args.HasArgs()) |
| 73 | + flags = args.GetArgString(); |
| 74 | + result.AppendMessageWithFormatv("note: ran `frame variable {0}{1}`", |
| 75 | + flags, expr); |
| 76 | + } |
| 77 | + valobj_sp->Dump(result.GetOutputStream(), dump_options); |
| 78 | + result.SetStatus(eReturnStatusSuccessFinishResult); |
| 79 | + return true; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Second, also lastly, try `expr` as a source expression to evaluate. |
| 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 | + auto *exe_scope = m_exe_ctx.GetBestExecutionContextScope(); |
| 90 | + ValueObjectSP valobj_sp; |
| 91 | + if (target.EvaluateExpression(expr, exe_scope, valobj_sp) == |
| 92 | + eExpressionCompleted) { |
| 93 | + if (verbosity != eDWIMPrintVerbosityNone) { |
| 94 | + StringRef flags; |
| 95 | + if (args.HasArgs()) |
| 96 | + flags = args.GetArgStringWithDelimiter(); |
| 97 | + result.AppendMessageWithFormatv("note: ran `expression {0}{1}`", flags, |
| 98 | + expr); |
| 99 | + } |
| 100 | + valobj_sp->Dump(result.GetOutputStream(), dump_options); |
| 101 | + result.SetStatus(eReturnStatusSuccessFinishResult); |
| 102 | + return true; |
| 103 | + } else { |
| 104 | + if (valobj_sp) |
| 105 | + result.SetError(valobj_sp->GetError()); |
| 106 | + else |
| 107 | + result.AppendErrorWithFormatv( |
| 108 | + "unknown error evaluating expression `{0}`", expr); |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments