130fdc8d8SChris Lattner //===-- CommandObjectExpression.cpp -----------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 330fdc8d8SChris Lattner // The LLVM Compiler Infrastructure 430fdc8d8SChris Lattner // 530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source 630fdc8d8SChris Lattner // License. See LICENSE.TXT for details. 730fdc8d8SChris Lattner // 830fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 930fdc8d8SChris Lattner 1030fdc8d8SChris Lattner // C Includes 1130fdc8d8SChris Lattner // C++ Includes 1230fdc8d8SChris Lattner // Other libraries and framework includes 13c8ecc2a9SEugene Zelenko #include "llvm/ADT/STLExtras.h" 14c8ecc2a9SEugene Zelenko #include "llvm/ADT/StringRef.h" 15c8ecc2a9SEugene Zelenko 1630fdc8d8SChris Lattner // Project includes 17c8ecc2a9SEugene Zelenko #include "CommandObjectExpression.h" 18b9c1b51eSKate Stone #include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h" 19b9c1b51eSKate Stone #include "lldb/Core/Debugger.h" 2030fdc8d8SChris Lattner #include "lldb/Core/Value.h" 216c68fb45SJim Ingham #include "lldb/Core/ValueObjectVariable.h" 224d93b8cdSEnrico Granata #include "lldb/DataFormatters/ValueObjectPrinter.h" 2330fdc8d8SChris Lattner #include "lldb/Expression/DWARFExpression.h" 24f2bd5c3eSSean Callanan #include "lldb/Expression/REPL.h" 25b9c1b51eSKate Stone #include "lldb/Expression/UserExpression.h" 2630fdc8d8SChris Lattner #include "lldb/Host/Host.h" 273eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h" 286611103cSGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h" 2930fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 3047cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h" 3130fdc8d8SChris Lattner #include "lldb/Symbol/ObjectFile.h" 3230fdc8d8SChris Lattner #include "lldb/Symbol/Variable.h" 33b9c1b51eSKate Stone #include "lldb/Target/Language.h" 3430fdc8d8SChris Lattner #include "lldb/Target/Process.h" 35b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h" 3630fdc8d8SChris Lattner #include "lldb/Target/Target.h" 377260f620SGreg Clayton #include "lldb/Target/Thread.h" 3830fdc8d8SChris Lattner 3930fdc8d8SChris Lattner using namespace lldb; 4030fdc8d8SChris Lattner using namespace lldb_private; 4130fdc8d8SChris Lattner 42b9c1b51eSKate Stone CommandObjectExpression::CommandOptions::CommandOptions() : OptionGroup() {} 4330fdc8d8SChris Lattner 44c8ecc2a9SEugene Zelenko CommandObjectExpression::CommandOptions::~CommandOptions() = default; 4530fdc8d8SChris Lattner 46b9c1b51eSKate Stone static OptionEnumValueElement g_description_verbosity_type[] = { 47b9c1b51eSKate Stone {eLanguageRuntimeDescriptionDisplayVerbosityCompact, "compact", 48b9c1b51eSKate Stone "Only show the description string"}, 49b9c1b51eSKate Stone {eLanguageRuntimeDescriptionDisplayVerbosityFull, "full", 50b9c1b51eSKate Stone "Show the full output, including persistent variable's name and type"}, 51b9c1b51eSKate Stone {0, nullptr, nullptr}}; 524d93b8cdSEnrico Granata 531f0f5b5bSZachary Turner static OptionDefinition g_expression_options[] = { 54ac9c3a62SKate Stone // clang-format off 55c8ecc2a9SEugene Zelenko {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "all-threads", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Should we run all threads if the execution doesn't complete on one thread."}, 56c8ecc2a9SEugene Zelenko {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "ignore-breakpoints", 'i', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Ignore breakpoint hits while running expressions"}, 57c8ecc2a9SEugene Zelenko {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "timeout", 't', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Timeout value (in microseconds) for running the expression."}, 58ac9c3a62SKate Stone {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "unwind-on-error", 'u', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Clean up program state if the expression causes a crash, or raises a signal. " 59ac9c3a62SKate Stone "Note, unlike gdb hitting a breakpoint is controlled by another option (-i)."}, 60ac9c3a62SKate Stone {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "debug", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "When specified, debug the JIT code by setting a breakpoint on the first instruction " 61ac9c3a62SKate Stone "and forcing breakpoints to not be ignored (-i0) and no unwinding to happen on error (-u0)."}, 62ac9c3a62SKate Stone {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "language", 'l', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLanguage, "Specifies the Language to use when parsing the expression. If not set the target.language " 63ac9c3a62SKate Stone "setting is used." }, 64279b2e88SJim Ingham {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "apply-fixits", 'X', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLanguage, "If true, simple fix-it hints will be automatically applied to the expression." }, 65c8ecc2a9SEugene Zelenko {LLDB_OPT_SET_1, false, "description-verbosity", 'v', OptionParser::eOptionalArgument, nullptr, g_description_verbosity_type, 0, eArgTypeDescriptionVerbosity, "How verbose should the output of this expression be, if the object description is asked for."}, 660354a688SSean Callanan {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "top-level", 'p', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Interpret the expression as a complete translation unit, without injecting it into the local " 670354a688SSean Callanan "context. Allows declaration of persistent, top-level entities without a $ prefix."}, 68ac9c3a62SKate Stone {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "allow-jit", 'j', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Controls whether the expression can fall back to being JITted if it's not supported by " 69ac9c3a62SKate Stone "the interpreter (defaults to true)."} 70ac9c3a62SKate Stone // clang-format on 711deb7962SGreg Clayton }; 721deb7962SGreg Clayton 7397206d57SZachary Turner Status CommandObjectExpression::CommandOptions::SetOptionValue( 748cef4b0bSZachary Turner uint32_t option_idx, llvm::StringRef option_arg, 75b9c1b51eSKate Stone ExecutionContext *execution_context) { 7697206d57SZachary Turner Status error; 7730fdc8d8SChris Lattner 781f0f5b5bSZachary Turner const int short_option = GetDefinitions()[option_idx].short_option; 7930fdc8d8SChris Lattner 80b9c1b51eSKate Stone switch (short_option) { 8115663c53SDawn Perchik case 'l': 820e0984eeSJim Ingham language = Language::GetLanguageTypeFromString(option_arg); 8315663c53SDawn Perchik if (language == eLanguageTypeUnknown) 84b9c1b51eSKate Stone error.SetErrorStringWithFormat( 858cef4b0bSZachary Turner "unknown language type: '%s' for expression", 868cef4b0bSZachary Turner option_arg.str().c_str()); 8715663c53SDawn Perchik break; 8830fdc8d8SChris Lattner 89b9c1b51eSKate Stone case 'a': { 9035e1bda6SJim Ingham bool success; 9135e1bda6SJim Ingham bool result; 9247cbf4a0SPavel Labath result = OptionArgParser::ToBoolean(option_arg, true, &success); 9335e1bda6SJim Ingham if (!success) 94b9c1b51eSKate Stone error.SetErrorStringWithFormat( 958cef4b0bSZachary Turner "invalid all-threads value setting: \"%s\"", 968cef4b0bSZachary Turner option_arg.str().c_str()); 9735e1bda6SJim Ingham else 9835e1bda6SJim Ingham try_all_threads = result; 99b9c1b51eSKate Stone } break; 1006c68fb45SJim Ingham 101b9c1b51eSKate Stone case 'i': { 102184e9811SJim Ingham bool success; 10347cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 104184e9811SJim Ingham if (success) 105184e9811SJim Ingham ignore_breakpoints = tmp_value; 106184e9811SJim Ingham else 107b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1088cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 1098cef4b0bSZachary Turner option_arg.str().c_str()); 110184e9811SJim Ingham break; 111184e9811SJim Ingham } 1123fe71581SMarianne Mailhot-Sarrasin 113b9c1b51eSKate Stone case 'j': { 1143fe71581SMarianne Mailhot-Sarrasin bool success; 11547cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 1163fe71581SMarianne Mailhot-Sarrasin if (success) 1173fe71581SMarianne Mailhot-Sarrasin allow_jit = tmp_value; 1183fe71581SMarianne Mailhot-Sarrasin else 119b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1208cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 1218cef4b0bSZachary Turner option_arg.str().c_str()); 1223fe71581SMarianne Mailhot-Sarrasin break; 1233fe71581SMarianne Mailhot-Sarrasin } 1243fe71581SMarianne Mailhot-Sarrasin 1258cef4b0bSZachary Turner case 't': 1268cef4b0bSZachary Turner if (option_arg.getAsInteger(0, timeout)) { 1278cef4b0bSZachary Turner timeout = 0; 128b9c1b51eSKate Stone error.SetErrorStringWithFormat("invalid timeout setting \"%s\"", 1298cef4b0bSZachary Turner option_arg.str().c_str()); 1308cef4b0bSZachary Turner } 1318cef4b0bSZachary Turner break; 13235e1bda6SJim Ingham 133b9c1b51eSKate Stone case 'u': { 134399f1cafSJim Ingham bool success; 13547cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 136184e9811SJim Ingham if (success) 137184e9811SJim Ingham unwind_on_error = tmp_value; 138184e9811SJim Ingham else 139b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1408cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 1418cef4b0bSZachary Turner option_arg.str().c_str()); 142399f1cafSJim Ingham break; 1433bfdaa2aSSean Callanan } 1444d93b8cdSEnrico Granata 1454d93b8cdSEnrico Granata case 'v': 146543a26e9SEnrico Granata if (option_arg.empty()) { 1474d93b8cdSEnrico Granata m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull; 1484d93b8cdSEnrico Granata break; 1494d93b8cdSEnrico Granata } 15047cbf4a0SPavel Labath m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) 15147cbf4a0SPavel Labath OptionArgParser::ToOptionEnum( 1521f0f5b5bSZachary Turner option_arg, GetDefinitions()[option_idx].enum_values, 0, error); 1534d93b8cdSEnrico Granata if (!error.Success()) 154b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1558cef4b0bSZachary Turner "unrecognized value for description-verbosity '%s'", 1568cef4b0bSZachary Turner option_arg.str().c_str()); 1574d93b8cdSEnrico Granata break; 1584d93b8cdSEnrico Granata 15962afb9f6SGreg Clayton case 'g': 16062afb9f6SGreg Clayton debug = true; 16162afb9f6SGreg Clayton unwind_on_error = false; 16262afb9f6SGreg Clayton ignore_breakpoints = false; 16362afb9f6SGreg Clayton break; 16462afb9f6SGreg Clayton 165863fab69SSean Callanan case 'p': 166863fab69SSean Callanan top_level = true; 167863fab69SSean Callanan break; 168863fab69SSean Callanan 169b9c1b51eSKate Stone case 'X': { 170a1e541bfSJim Ingham bool success; 17147cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 172a1e541bfSJim Ingham if (success) 173a1e541bfSJim Ingham auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo; 174a1e541bfSJim Ingham else 175b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1768cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 1778cef4b0bSZachary Turner option_arg.str().c_str()); 178a1e541bfSJim Ingham break; 179a1e541bfSJim Ingham } 180a1e541bfSJim Ingham 18130fdc8d8SChris Lattner default: 182b9c1b51eSKate Stone error.SetErrorStringWithFormat("invalid short option character '%c'", 183b9c1b51eSKate Stone short_option); 18430fdc8d8SChris Lattner break; 18530fdc8d8SChris Lattner } 18630fdc8d8SChris Lattner 18730fdc8d8SChris Lattner return error; 18830fdc8d8SChris Lattner } 18930fdc8d8SChris Lattner 190b9c1b51eSKate Stone void CommandObjectExpression::CommandOptions::OptionParsingStarting( 191b9c1b51eSKate Stone ExecutionContext *execution_context) { 192e1cfbc79STodd Fiala auto process_sp = 193e1cfbc79STodd Fiala execution_context ? execution_context->GetProcessSP() : ProcessSP(); 194b9c1b51eSKate Stone if (process_sp) { 195e1cfbc79STodd Fiala ignore_breakpoints = process_sp->GetIgnoreBreakpointsInExpressions(); 196e1cfbc79STodd Fiala unwind_on_error = process_sp->GetUnwindOnErrorInExpressions(); 197b9c1b51eSKate Stone } else { 198fc03f8fcSGreg Clayton ignore_breakpoints = true; 199399f1cafSJim Ingham unwind_on_error = true; 200184e9811SJim Ingham } 201184e9811SJim Ingham 20230fdc8d8SChris Lattner show_summary = true; 20335e1bda6SJim Ingham try_all_threads = true; 20435e1bda6SJim Ingham timeout = 0; 20562afb9f6SGreg Clayton debug = false; 20615663c53SDawn Perchik language = eLanguageTypeUnknown; 2074d93b8cdSEnrico Granata m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact; 208a1e541bfSJim Ingham auto_apply_fixits = eLazyBoolCalculate; 209863fab69SSean Callanan top_level = false; 2103fe71581SMarianne Mailhot-Sarrasin allow_jit = true; 21130fdc8d8SChris Lattner } 21230fdc8d8SChris Lattner 2131f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> 214b9c1b51eSKate Stone CommandObjectExpression::CommandOptions::GetDefinitions() { 21570602439SZachary Turner return llvm::makeArrayRef(g_expression_options); 21630fdc8d8SChris Lattner } 21730fdc8d8SChris Lattner 218b9c1b51eSKate Stone CommandObjectExpression::CommandObjectExpression( 219b9c1b51eSKate Stone CommandInterpreter &interpreter) 2207428a18cSKate Stone : CommandObjectRaw( 221b9c1b51eSKate Stone interpreter, "expression", "Evaluate an expression on the current " 222b9c1b51eSKate Stone "thread. Displays any returned value " 223b9c1b51eSKate Stone "with LLDB's default formatting.", 224a449698cSZachary Turner "", eCommandProcessMustBePaused | eCommandTryTargetAPILock), 22544d93782SGreg Clayton IOHandlerDelegate(IOHandlerDelegate::Completion::Expression), 226b9c1b51eSKate Stone m_option_group(), m_format_options(eFormatDefault), 227b9c1b51eSKate Stone m_repl_option(LLDB_OPT_SET_1, false, "repl", 'r', "Drop into REPL", false, 228b9c1b51eSKate Stone true), 229b9c1b51eSKate Stone m_command_options(), m_expr_line_count(0), m_expr_lines() { 23030fdc8d8SChris Lattner SetHelpLong( 231ea671fbdSKate Stone R"( 232d0309916SJim Ingham Single and multi-line expressions: 233d0309916SJim Ingham 234d0309916SJim Ingham )" 235d0309916SJim Ingham " The expression provided on the command line must be a complete expression \ 236d0309916SJim Ingham with no newlines. To evaluate a multi-line expression, \ 237d0309916SJim Ingham hit a return after an empty expression, and lldb will enter the multi-line expression editor. \ 238d0309916SJim Ingham Hit return on an empty line to end the multi-line expression." 239d0309916SJim Ingham 240d0309916SJim Ingham R"( 241d0309916SJim Ingham 242ea671fbdSKate Stone Timeouts: 243ea671fbdSKate Stone 244b9c1b51eSKate Stone )" 245b9c1b51eSKate Stone " If the expression can be evaluated statically (without running code) then it will be. \ 246ea671fbdSKate Stone Otherwise, by default the expression will run on the current thread with a short timeout: \ 247ea671fbdSKate Stone currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \ 248ea671fbdSKate Stone and resumed with all threads running. You can use the -a option to disable retrying on all \ 249b9c1b51eSKate Stone threads. You can use the -t option to set a shorter timeout." 250b9c1b51eSKate Stone R"( 251ea671fbdSKate Stone 252ea671fbdSKate Stone User defined variables: 253ea671fbdSKate Stone 254b9c1b51eSKate Stone )" 255b9c1b51eSKate Stone " You can define your own variables for convenience or to be used in subsequent expressions. \ 256ea671fbdSKate Stone You define them the same way you would define variables in C. If the first character of \ 257ea671fbdSKate Stone your user defined variable is a $, then the variable's value will be available in future \ 258b9c1b51eSKate Stone expressions, otherwise it will just be available in the current expression." 259b9c1b51eSKate Stone R"( 260ea671fbdSKate Stone 261ea671fbdSKate Stone Continuing evaluation after a breakpoint: 262ea671fbdSKate Stone 263b9c1b51eSKate Stone )" 264b9c1b51eSKate Stone " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \ 265ea671fbdSKate Stone you are done with your investigation, you can either remove the expression execution frames \ 266ea671fbdSKate Stone from the stack with \"thread return -x\" or if you are still interested in the expression result \ 267ea671fbdSKate Stone you can issue the \"continue\" command and the expression evaluation will complete and the \ 268ea671fbdSKate Stone expression result will be available using the \"thread.completed-expression\" key in the thread \ 269b9c1b51eSKate Stone format." 270d0309916SJim Ingham 271b9c1b51eSKate Stone R"( 272ea671fbdSKate Stone 273ea671fbdSKate Stone Examples: 274ea671fbdSKate Stone 275ea671fbdSKate Stone expr my_struct->a = my_array[3] 276ea671fbdSKate Stone expr -f bin -- (index * 8) + 5 277ea671fbdSKate Stone expr unsigned int $foo = 5 278b9c1b51eSKate Stone expr char c[] = \"foo\"; c[0])"); 279405fe67fSCaroline Tice 280405fe67fSCaroline Tice CommandArgumentEntry arg; 281405fe67fSCaroline Tice CommandArgumentData expression_arg; 282405fe67fSCaroline Tice 283405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 284405fe67fSCaroline Tice expression_arg.arg_type = eArgTypeExpression; 285405fe67fSCaroline Tice expression_arg.arg_repetition = eArgRepeatPlain; 286405fe67fSCaroline Tice 287b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the argument 288b9c1b51eSKate Stone // entry. 289405fe67fSCaroline Tice arg.push_back(expression_arg); 290405fe67fSCaroline Tice 291405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 292405fe67fSCaroline Tice m_arguments.push_back(arg); 2931deb7962SGreg Clayton 2945009f9d5SGreg Clayton // Add the "--format" and "--gdb-format" 295b9c1b51eSKate Stone m_option_group.Append(&m_format_options, 296b9c1b51eSKate Stone OptionGroupFormat::OPTION_GROUP_FORMAT | 297b9c1b51eSKate Stone OptionGroupFormat::OPTION_GROUP_GDB_FMT, 298b9c1b51eSKate Stone LLDB_OPT_SET_1); 2991deb7962SGreg Clayton m_option_group.Append(&m_command_options); 300b9c1b51eSKate Stone m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, 301b9c1b51eSKate Stone LLDB_OPT_SET_1 | LLDB_OPT_SET_2); 302f2bd5c3eSSean Callanan m_option_group.Append(&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3); 3031deb7962SGreg Clayton m_option_group.Finalize(); 30430fdc8d8SChris Lattner } 30530fdc8d8SChris Lattner 306c8ecc2a9SEugene Zelenko CommandObjectExpression::~CommandObjectExpression() = default; 30730fdc8d8SChris Lattner 308b9c1b51eSKate Stone Options *CommandObjectExpression::GetOptions() { return &m_option_group; } 30930fdc8d8SChris Lattner 31074829734SRaphael Isemann int CommandObjectExpression::HandleCompletion(CompletionRequest &request) { 31174829734SRaphael Isemann EvaluateExpressionOptions options; 31274829734SRaphael Isemann options.SetCoerceToId(m_varobj_options.use_objc); 31374829734SRaphael Isemann options.SetLanguage(m_command_options.language); 31474829734SRaphael Isemann options.SetExecutionPolicy(lldb_private::eExecutionPolicyNever); 31574829734SRaphael Isemann options.SetAutoApplyFixIts(false); 31674829734SRaphael Isemann options.SetGenerateDebugInfo(false); 31774829734SRaphael Isemann 31874829734SRaphael Isemann // We need a valid execution context with a frame pointer for this 31974829734SRaphael Isemann // completion, so if we don't have one we should try to make a valid 32074829734SRaphael Isemann // execution context. 32174829734SRaphael Isemann if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr) 32274829734SRaphael Isemann m_interpreter.UpdateExecutionContext(nullptr); 32374829734SRaphael Isemann 32474829734SRaphael Isemann // This didn't work, so let's get out before we start doing things that 32574829734SRaphael Isemann // expect a valid frame pointer. 32674829734SRaphael Isemann if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr) 32774829734SRaphael Isemann return 0; 32874829734SRaphael Isemann 32974829734SRaphael Isemann ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 33074829734SRaphael Isemann 33174829734SRaphael Isemann Target *target = exe_ctx.GetTargetPtr(); 33274829734SRaphael Isemann 33374829734SRaphael Isemann if (!target) 33474829734SRaphael Isemann target = GetDummyTarget(); 33574829734SRaphael Isemann 33674829734SRaphael Isemann if (!target) 33774829734SRaphael Isemann return 0; 33874829734SRaphael Isemann 33974829734SRaphael Isemann unsigned cursor_pos = request.GetRawCursorPos(); 34074829734SRaphael Isemann llvm::StringRef code = request.GetRawLine(); 34174829734SRaphael Isemann 34274829734SRaphael Isemann const std::size_t original_code_size = code.size(); 34374829734SRaphael Isemann 34474829734SRaphael Isemann // Remove the first token which is 'expr' or some alias/abbreviation of that. 34574829734SRaphael Isemann code = llvm::getToken(code).second.ltrim(); 34674829734SRaphael Isemann OptionsWithRaw args(code); 34774829734SRaphael Isemann code = args.GetRawPart(); 34874829734SRaphael Isemann 34974829734SRaphael Isemann // The position where the expression starts in the command line. 35074829734SRaphael Isemann assert(original_code_size >= code.size()); 35174829734SRaphael Isemann std::size_t raw_start = original_code_size - code.size(); 35274829734SRaphael Isemann 35374829734SRaphael Isemann // Check if the cursor is actually in the expression string, and if not, we 35474829734SRaphael Isemann // exit. 35574829734SRaphael Isemann // FIXME: We should complete the options here. 35674829734SRaphael Isemann if (cursor_pos < raw_start) 35774829734SRaphael Isemann return 0; 35874829734SRaphael Isemann 35974829734SRaphael Isemann // Make the cursor_pos again relative to the start of the code string. 36074829734SRaphael Isemann assert(cursor_pos >= raw_start); 36174829734SRaphael Isemann cursor_pos -= raw_start; 36274829734SRaphael Isemann 36374829734SRaphael Isemann auto language = exe_ctx.GetFrameRef().GetLanguage(); 36474829734SRaphael Isemann 36574829734SRaphael Isemann Status error; 36674829734SRaphael Isemann lldb::UserExpressionSP expr(target->GetUserExpressionForLanguage( 36774829734SRaphael Isemann code, llvm::StringRef(), language, UserExpression::eResultTypeAny, 36874829734SRaphael Isemann options, error)); 36974829734SRaphael Isemann if (error.Fail()) 37074829734SRaphael Isemann return 0; 37174829734SRaphael Isemann 372*c11a780eSRaphael Isemann expr->Complete(exe_ctx, request, cursor_pos); 37374829734SRaphael Isemann return request.GetNumberOfMatches(); 37474829734SRaphael Isemann } 37574829734SRaphael Isemann 37697206d57SZachary Turner static lldb_private::Status 377b9c1b51eSKate Stone CanBeUsedForElementCountPrinting(ValueObject &valobj) { 378520a422bSEnrico Granata CompilerType type(valobj.GetCompilerType()); 379520a422bSEnrico Granata CompilerType pointee; 380520a422bSEnrico Granata if (!type.IsPointerType(&pointee)) 38197206d57SZachary Turner return Status("as it does not refer to a pointer"); 382520a422bSEnrico Granata if (pointee.IsVoidType()) 38397206d57SZachary Turner return Status("as it refers to a pointer to void"); 38497206d57SZachary Turner return Status(); 385520a422bSEnrico Granata } 386520a422bSEnrico Granata 3874d51a902SRaphael Isemann bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr, 3886e8dc334SCaroline Tice Stream *output_stream, 3896e8dc334SCaroline Tice Stream *error_stream, 390b9c1b51eSKate Stone CommandReturnObject *result) { 39105097246SAdrian Prantl // Don't use m_exe_ctx as this might be called asynchronously after the 39205097246SAdrian Prantl // command object DoExecute has finished when doing multi-line expression 39305097246SAdrian Prantl // that use an input reader... 394ba7b8e2cSGreg Clayton ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 395ba7b8e2cSGreg Clayton 396ba7b8e2cSGreg Clayton Target *target = exe_ctx.GetTargetPtr(); 397c0a6e061SSean Callanan 398c0a6e061SSean Callanan if (!target) 399893c932aSJim Ingham target = GetDummyTarget(); 400c0a6e061SSean Callanan 401b9c1b51eSKate Stone if (target) { 4028b2fe6dcSGreg Clayton lldb::ValueObjectSP result_valobj_sp; 40392adcac9SSean Callanan bool keep_in_memory = true; 404009d110dSDawn Perchik StackFrame *frame = exe_ctx.GetFramePtr(); 40592adcac9SSean Callanan 40635e1bda6SJim Ingham EvaluateExpressionOptions options; 4076fbc48bcSJim Ingham options.SetCoerceToId(m_varobj_options.use_objc); 4086fbc48bcSJim Ingham options.SetUnwindOnError(m_command_options.unwind_on_error); 4096fbc48bcSJim Ingham options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints); 4106fbc48bcSJim Ingham options.SetKeepInMemory(keep_in_memory); 4116fbc48bcSJim Ingham options.SetUseDynamic(m_varobj_options.use_dynamic); 4126fbc48bcSJim Ingham options.SetTryAllThreads(m_command_options.try_all_threads); 4136fbc48bcSJim Ingham options.SetDebug(m_command_options.debug); 41415663c53SDawn Perchik options.SetLanguage(m_command_options.language); 415b9c1b51eSKate Stone options.SetExecutionPolicy( 416b9c1b51eSKate Stone m_command_options.allow_jit 417b9c1b51eSKate Stone ? EvaluateExpressionOptions::default_execution_policy 418b9c1b51eSKate Stone : lldb_private::eExecutionPolicyNever); 41915663c53SDawn Perchik 420a1e541bfSJim Ingham bool auto_apply_fixits; 421a1e541bfSJim Ingham if (m_command_options.auto_apply_fixits == eLazyBoolCalculate) 422a1e541bfSJim Ingham auto_apply_fixits = target->GetEnableAutoApplyFixIts(); 423a1e541bfSJim Ingham else 424b9c1b51eSKate Stone auto_apply_fixits = 425b9c1b51eSKate Stone m_command_options.auto_apply_fixits == eLazyBoolYes ? true : false; 426a1e541bfSJim Ingham 427a1e541bfSJim Ingham options.SetAutoApplyFixIts(auto_apply_fixits); 428a1e541bfSJim Ingham 429863fab69SSean Callanan if (m_command_options.top_level) 430863fab69SSean Callanan options.SetExecutionPolicy(eExecutionPolicyTopLevel); 431863fab69SSean Callanan 43205097246SAdrian Prantl // If there is any chance we are going to stop and want to see what went 43305097246SAdrian Prantl // wrong with our expression, we should generate debug info 43423f8c95aSGreg Clayton if (!m_command_options.ignore_breakpoints || 43523f8c95aSGreg Clayton !m_command_options.unwind_on_error) 43623f8c95aSGreg Clayton options.SetGenerateDebugInfo(true); 43723f8c95aSGreg Clayton 43862afb9f6SGreg Clayton if (m_command_options.timeout > 0) 43943d35418SPavel Labath options.SetTimeout(std::chrono::microseconds(m_command_options.timeout)); 4406f78f386SJim Ingham else 44143d35418SPavel Labath options.SetTimeout(llvm::None); 442d4439aa9SEnrico Granata 443b9c1b51eSKate Stone ExpressionResults success = target->EvaluateExpression( 444b9c1b51eSKate Stone expr, frame, result_valobj_sp, options, &m_fixed_expression); 445e5ee6f04SJim Ingham 446b9c1b51eSKate Stone // We only tell you about the FixIt if we applied it. The compiler errors 447b9c1b51eSKate Stone // will suggest the FixIt if it parsed. 448b9c1b51eSKate Stone if (error_stream && !m_fixed_expression.empty() && 449b9c1b51eSKate Stone target->GetEnableNotifyAboutFixIts()) { 450e5ee6f04SJim Ingham if (success == eExpressionCompleted) 451b9c1b51eSKate Stone error_stream->Printf( 452b9c1b51eSKate Stone " Fix-it applied, fixed expression was: \n %s\n", 453b9c1b51eSKate Stone m_fixed_expression.c_str()); 454e5ee6f04SJim Ingham } 4558b2fe6dcSGreg Clayton 456b9c1b51eSKate Stone if (result_valobj_sp) { 457bf154daeSSean Callanan Format format = m_format_options.GetFormat(); 458bf154daeSSean Callanan 459b9c1b51eSKate Stone if (result_valobj_sp->GetError().Success()) { 460b9c1b51eSKate Stone if (format != eFormatVoid) { 4611deb7962SGreg Clayton if (format != eFormatDefault) 4621deb7962SGreg Clayton result_valobj_sp->SetFormat(format); 46332c4085bSGreg Clayton 464b9c1b51eSKate Stone if (m_varobj_options.elem_count > 0) { 46597206d57SZachary Turner Status error(CanBeUsedForElementCountPrinting(*result_valobj_sp)); 466b9c1b51eSKate Stone if (error.Fail()) { 467b9c1b51eSKate Stone result->AppendErrorWithFormat( 468b9c1b51eSKate Stone "expression cannot be used with --element-count %s\n", 469b9c1b51eSKate Stone error.AsCString("")); 470520a422bSEnrico Granata result->SetStatus(eReturnStatusFailed); 471520a422bSEnrico Granata return false; 472520a422bSEnrico Granata } 473520a422bSEnrico Granata } 474520a422bSEnrico Granata 475b9c1b51eSKate Stone DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions( 476b9c1b51eSKate Stone m_command_options.m_verbosity, format)); 477b9c1b51eSKate Stone options.SetVariableFormatDisplayLanguage( 478b9c1b51eSKate Stone result_valobj_sp->GetPreferredDisplayLanguage()); 479770eb05aSEnrico Granata 4804d93b8cdSEnrico Granata result_valobj_sp->Dump(*output_stream, options); 4814d93b8cdSEnrico Granata 482fcd43b71SJohnny Chen if (result) 483fcd43b71SJohnny Chen result->SetStatus(eReturnStatusSuccessFinishResult); 48430fdc8d8SChris Lattner } 485b9c1b51eSKate Stone } else { 486b9c1b51eSKate Stone if (result_valobj_sp->GetError().GetError() == 487b9c1b51eSKate Stone UserExpression::kNoResult) { 488b9c1b51eSKate Stone if (format != eFormatVoid && 489b9c1b51eSKate Stone m_interpreter.GetDebugger().GetNotifyVoid()) { 490bcf897faSSean Callanan error_stream->PutCString("(void)\n"); 491bcf897faSSean Callanan } 492bccce813SSean Callanan 493bccce813SSean Callanan if (result) 494bccce813SSean Callanan result->SetStatus(eReturnStatusSuccessFinishResult); 495b9c1b51eSKate Stone } else { 4965fd05903SGreg Clayton const char *error_cstr = result_valobj_sp->GetError().AsCString(); 497b9c1b51eSKate Stone if (error_cstr && error_cstr[0]) { 498c7bece56SGreg Clayton const size_t error_cstr_len = strlen(error_cstr); 499b9c1b51eSKate Stone const bool ends_with_newline = 500b9c1b51eSKate Stone error_cstr[error_cstr_len - 1] == '\n'; 5015fd05903SGreg Clayton if (strstr(error_cstr, "error:") != error_cstr) 5025fd05903SGreg Clayton error_stream->PutCString("error: "); 5035fd05903SGreg Clayton error_stream->Write(error_cstr, error_cstr_len); 5045fd05903SGreg Clayton if (!ends_with_newline) 5055fd05903SGreg Clayton error_stream->EOL(); 506b9c1b51eSKate Stone } else { 5075fd05903SGreg Clayton error_stream->PutCString("error: unknown error\n"); 5085fd05903SGreg Clayton } 5095fd05903SGreg Clayton 510fcd43b71SJohnny Chen if (result) 511b71f3844SGreg Clayton result->SetStatus(eReturnStatusFailed); 51230fdc8d8SChris Lattner } 5138b2fe6dcSGreg Clayton } 5148b2fe6dcSGreg Clayton } 515b9c1b51eSKate Stone } else { 5166e8dc334SCaroline Tice error_stream->Printf("error: invalid execution context for expression\n"); 5178b2fe6dcSGreg Clayton return false; 5188b2fe6dcSGreg Clayton } 51930fdc8d8SChris Lattner 52016ad5faeSSean Callanan return true; 52130fdc8d8SChris Lattner } 52230fdc8d8SChris Lattner 523b9c1b51eSKate Stone void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler, 524b9c1b51eSKate Stone std::string &line) { 52544d93782SGreg Clayton io_handler.SetIsDone(true); 526b9c1b51eSKate Stone // StreamSP output_stream = 527b9c1b51eSKate Stone // io_handler.GetDebugger().GetAsyncOutputStream(); 52844d93782SGreg Clayton // StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream(); 52944d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 53044d93782SGreg Clayton StreamFileSP error_sp(io_handler.GetErrorStreamFile()); 53144d93782SGreg Clayton 532b9c1b51eSKate Stone EvaluateExpression(line.c_str(), output_sp.get(), error_sp.get()); 53344d93782SGreg Clayton if (output_sp) 53444d93782SGreg Clayton output_sp->Flush(); 53544d93782SGreg Clayton if (error_sp) 53644d93782SGreg Clayton error_sp->Flush(); 53744d93782SGreg Clayton } 53844d93782SGreg Clayton 539b9c1b51eSKate Stone bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler, 540b9c1b51eSKate Stone StringList &lines) { 541f52c40c5SSean Callanan // An empty lines is used to indicate the end of input 542f52c40c5SSean Callanan const size_t num_lines = lines.GetSize(); 543b9c1b51eSKate Stone if (num_lines > 0 && lines[num_lines - 1].empty()) { 54405097246SAdrian Prantl // Remove the last empty line from "lines" so it doesn't appear in our 54505097246SAdrian Prantl // resulting input and return true to indicate we are done getting lines 54644d93782SGreg Clayton lines.PopBack(); 547f52c40c5SSean Callanan return true; 54844d93782SGreg Clayton } 549f52c40c5SSean Callanan return false; 55044d93782SGreg Clayton } 55144d93782SGreg Clayton 552b9c1b51eSKate Stone void CommandObjectExpression::GetMultilineExpression() { 55330fdc8d8SChris Lattner m_expr_lines.clear(); 55430fdc8d8SChris Lattner m_expr_line_count = 0; 55530fdc8d8SChris Lattner 55644d93782SGreg Clayton Debugger &debugger = GetCommandInterpreter().GetDebugger(); 557e30f11d9SKate Stone bool color_prompt = debugger.GetUseColor(); 55844d93782SGreg Clayton const bool multiple_lines = true; // Get multiple lines 559b9c1b51eSKate Stone IOHandlerSP io_handler_sp( 560b9c1b51eSKate Stone new IOHandlerEditline(debugger, IOHandler::Type::Expression, 56144d93782SGreg Clayton "lldb-expr", // Name of input reader for history 562514d8cd8SZachary Turner llvm::StringRef(), // No prompt 563514d8cd8SZachary Turner llvm::StringRef(), // Continuation prompt 564b9c1b51eSKate Stone multiple_lines, color_prompt, 565f6913cd7SGreg Clayton 1, // Show line numbers starting at 1 56644d93782SGreg Clayton *this)); 567b6892508SGreg Clayton 568b6892508SGreg Clayton StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile()); 569b9c1b51eSKate Stone if (output_sp) { 570b9c1b51eSKate Stone output_sp->PutCString( 571b9c1b51eSKate Stone "Enter expressions, then terminate with an empty line to evaluate:\n"); 572b6892508SGreg Clayton output_sp->Flush(); 573b6892508SGreg Clayton } 57444d93782SGreg Clayton debugger.PushIOHandler(io_handler_sp); 575cf28a8b7SGreg Clayton } 576cf28a8b7SGreg Clayton 5774d51a902SRaphael Isemann bool CommandObjectExpression::DoExecute(llvm::StringRef command, 578b9c1b51eSKate Stone CommandReturnObject &result) { 579e5ee6f04SJim Ingham m_fixed_expression.clear(); 580e1cfbc79STodd Fiala auto exe_ctx = GetCommandInterpreter().GetExecutionContext(); 581e1cfbc79STodd Fiala m_option_group.NotifyOptionParsingStarting(&exe_ctx); 582cf28a8b7SGreg Clayton 5834d51a902SRaphael Isemann if (command.empty()) { 584cf28a8b7SGreg Clayton GetMultilineExpression(); 58530fdc8d8SChris Lattner return result.Succeeded(); 58630fdc8d8SChris Lattner } 58730fdc8d8SChris Lattner 5883a0e1270SRaphael Isemann OptionsWithRaw args(command); 5894d51a902SRaphael Isemann llvm::StringRef expr = args.GetRawPart(); 59030fdc8d8SChris Lattner 5913a0e1270SRaphael Isemann if (args.HasArgs()) { 5923a0e1270SRaphael Isemann if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group, exe_ctx)) 59330fdc8d8SChris Lattner return false; 594f6b8b581SGreg Clayton 595b9c1b51eSKate Stone if (m_repl_option.GetOptionValue().GetCurrentValue()) { 596f2bd5c3eSSean Callanan Target *target = m_interpreter.GetExecutionContext().GetTargetPtr(); 597b9c1b51eSKate Stone if (target) { 598f2bd5c3eSSean Callanan // Drop into REPL 599f2bd5c3eSSean Callanan m_expr_lines.clear(); 600f2bd5c3eSSean Callanan m_expr_line_count = 0; 601f2bd5c3eSSean Callanan 602f2bd5c3eSSean Callanan Debugger &debugger = target->GetDebugger(); 603f2bd5c3eSSean Callanan 604b9c1b51eSKate Stone // Check if the LLDB command interpreter is sitting on top of a REPL 60505097246SAdrian Prantl // that launched it... 6063a0e1270SRaphael Isemann if (debugger.CheckTopIOHandlerTypes(IOHandler::Type::CommandInterpreter, 6073a0e1270SRaphael Isemann IOHandler::Type::REPL)) { 608b9c1b51eSKate Stone // the LLDB command interpreter is sitting on top of a REPL that 60905097246SAdrian Prantl // launched it, so just say the command interpreter is done and 61005097246SAdrian Prantl // fall back to the existing REPL 611f2bd5c3eSSean Callanan m_interpreter.GetIOHandler(false)->SetIsDone(true); 612b9c1b51eSKate Stone } else { 613b9c1b51eSKate Stone // We are launching the REPL on top of the current LLDB command 61405097246SAdrian Prantl // interpreter, so just push one 615f2bd5c3eSSean Callanan bool initialize = false; 61697206d57SZachary Turner Status repl_error; 6173a0e1270SRaphael Isemann REPLSP repl_sp(target->GetREPL(repl_error, m_command_options.language, 6183a0e1270SRaphael Isemann nullptr, false)); 619f2bd5c3eSSean Callanan 620b9c1b51eSKate Stone if (!repl_sp) { 621f2bd5c3eSSean Callanan initialize = true; 622b9c1b51eSKate Stone repl_sp = target->GetREPL(repl_error, m_command_options.language, 623b9c1b51eSKate Stone nullptr, true); 624b9c1b51eSKate Stone if (!repl_error.Success()) { 625f2bd5c3eSSean Callanan result.SetError(repl_error); 626f2bd5c3eSSean Callanan return result.Succeeded(); 627f2bd5c3eSSean Callanan } 628f2bd5c3eSSean Callanan } 629f2bd5c3eSSean Callanan 630b9c1b51eSKate Stone if (repl_sp) { 631b9c1b51eSKate Stone if (initialize) { 632f2bd5c3eSSean Callanan repl_sp->SetCommandOptions(m_command_options); 633f2bd5c3eSSean Callanan repl_sp->SetFormatOptions(m_format_options); 634f2bd5c3eSSean Callanan repl_sp->SetValueObjectDisplayOptions(m_varobj_options); 635f2bd5c3eSSean Callanan } 636f2bd5c3eSSean Callanan 637f2bd5c3eSSean Callanan IOHandlerSP io_handler_sp(repl_sp->GetIOHandler()); 638f2bd5c3eSSean Callanan 639f2bd5c3eSSean Callanan io_handler_sp->SetIsDone(false); 640f2bd5c3eSSean Callanan 641f2bd5c3eSSean Callanan debugger.PushIOHandler(io_handler_sp); 642b9c1b51eSKate Stone } else { 643b9c1b51eSKate Stone repl_error.SetErrorStringWithFormat( 644b9c1b51eSKate Stone "Couldn't create a REPL for %s", 645b9c1b51eSKate Stone Language::GetNameForLanguageType(m_command_options.language)); 646f2bd5c3eSSean Callanan result.SetError(repl_error); 647f2bd5c3eSSean Callanan return result.Succeeded(); 648f2bd5c3eSSean Callanan } 649f2bd5c3eSSean Callanan } 650f2bd5c3eSSean Callanan } 651f2bd5c3eSSean Callanan } 652cf28a8b7SGreg Clayton // No expression following options 6534d51a902SRaphael Isemann else if (expr.empty()) { 654cf28a8b7SGreg Clayton GetMultilineExpression(); 655cf28a8b7SGreg Clayton return result.Succeeded(); 656cf28a8b7SGreg Clayton } 65730fdc8d8SChris Lattner } 65830fdc8d8SChris Lattner 6590df817aaSDavide Italiano Target *target = GetSelectedOrDummyTarget(); 66024fff242SDavide Italiano if (EvaluateExpression(expr, &(result.GetOutputStream()), 66124fff242SDavide Italiano &(result.GetErrorStream()), &result)) { 66224fff242SDavide Italiano 663b9c1b51eSKate Stone if (!m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) { 664e5ee6f04SJim Ingham CommandHistory &history = m_interpreter.GetCommandHistory(); 665b9c1b51eSKate Stone // FIXME: Can we figure out what the user actually typed (e.g. some alias 666b9c1b51eSKate Stone // for expr???) 667e5ee6f04SJim Ingham // If we can it would be nice to show that. 668e5ee6f04SJim Ingham std::string fixed_command("expression "); 6693a0e1270SRaphael Isemann if (args.HasArgs()) { 670e5ee6f04SJim Ingham // Add in any options that might have been in the original command: 6713a0e1270SRaphael Isemann fixed_command.append(args.GetArgStringWithDelimiter()); 672e5ee6f04SJim Ingham fixed_command.append(m_fixed_expression); 6733a0e1270SRaphael Isemann } else 6743a0e1270SRaphael Isemann fixed_command.append(m_fixed_expression); 675e5ee6f04SJim Ingham history.AppendString(fixed_command); 676e5ee6f04SJim Ingham } 67705097246SAdrian Prantl // Increment statistics to record this expression evaluation success. 67824fff242SDavide Italiano target->IncrementStats(StatisticKind::ExpressionSuccessful); 679fcd43b71SJohnny Chen return true; 680e5ee6f04SJim Ingham } 681fcd43b71SJohnny Chen 68205097246SAdrian Prantl // Increment statistics to record this expression evaluation failure. 68324fff242SDavide Italiano target->IncrementStats(StatisticKind::ExpressionFailure); 684fcd43b71SJohnny Chen result.SetStatus(eReturnStatusFailed); 685fcd43b71SJohnny Chen return false; 68630fdc8d8SChris Lattner } 687