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 31097206d57SZachary Turner static lldb_private::Status 311b9c1b51eSKate Stone CanBeUsedForElementCountPrinting(ValueObject &valobj) { 312520a422bSEnrico Granata CompilerType type(valobj.GetCompilerType()); 313520a422bSEnrico Granata CompilerType pointee; 314520a422bSEnrico Granata if (!type.IsPointerType(&pointee)) 31597206d57SZachary Turner return Status("as it does not refer to a pointer"); 316520a422bSEnrico Granata if (pointee.IsVoidType()) 31797206d57SZachary Turner return Status("as it refers to a pointer to void"); 31897206d57SZachary Turner return Status(); 319520a422bSEnrico Granata } 320520a422bSEnrico Granata 321b9c1b51eSKate Stone bool CommandObjectExpression::EvaluateExpression(const char *expr, 3226e8dc334SCaroline Tice Stream *output_stream, 3236e8dc334SCaroline Tice Stream *error_stream, 324b9c1b51eSKate Stone CommandReturnObject *result) { 325ba7b8e2cSGreg Clayton // Don't use m_exe_ctx as this might be called asynchronously 326ba7b8e2cSGreg Clayton // after the command object DoExecute has finished when doing 327ba7b8e2cSGreg Clayton // multi-line expression that use an input reader... 328ba7b8e2cSGreg Clayton ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 329ba7b8e2cSGreg Clayton 330ba7b8e2cSGreg Clayton Target *target = exe_ctx.GetTargetPtr(); 331c0a6e061SSean Callanan 332c0a6e061SSean Callanan if (!target) 333893c932aSJim Ingham target = GetDummyTarget(); 334c0a6e061SSean Callanan 335b9c1b51eSKate Stone if (target) { 3368b2fe6dcSGreg Clayton lldb::ValueObjectSP result_valobj_sp; 33792adcac9SSean Callanan bool keep_in_memory = true; 338009d110dSDawn Perchik StackFrame *frame = exe_ctx.GetFramePtr(); 33992adcac9SSean Callanan 34035e1bda6SJim Ingham EvaluateExpressionOptions options; 3416fbc48bcSJim Ingham options.SetCoerceToId(m_varobj_options.use_objc); 3426fbc48bcSJim Ingham options.SetUnwindOnError(m_command_options.unwind_on_error); 3436fbc48bcSJim Ingham options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints); 3446fbc48bcSJim Ingham options.SetKeepInMemory(keep_in_memory); 3456fbc48bcSJim Ingham options.SetUseDynamic(m_varobj_options.use_dynamic); 3466fbc48bcSJim Ingham options.SetTryAllThreads(m_command_options.try_all_threads); 3476fbc48bcSJim Ingham options.SetDebug(m_command_options.debug); 34815663c53SDawn Perchik options.SetLanguage(m_command_options.language); 349b9c1b51eSKate Stone options.SetExecutionPolicy( 350b9c1b51eSKate Stone m_command_options.allow_jit 351b9c1b51eSKate Stone ? EvaluateExpressionOptions::default_execution_policy 352b9c1b51eSKate Stone : lldb_private::eExecutionPolicyNever); 35315663c53SDawn Perchik 354a1e541bfSJim Ingham bool auto_apply_fixits; 355a1e541bfSJim Ingham if (m_command_options.auto_apply_fixits == eLazyBoolCalculate) 356a1e541bfSJim Ingham auto_apply_fixits = target->GetEnableAutoApplyFixIts(); 357a1e541bfSJim Ingham else 358b9c1b51eSKate Stone auto_apply_fixits = 359b9c1b51eSKate Stone m_command_options.auto_apply_fixits == eLazyBoolYes ? true : false; 360a1e541bfSJim Ingham 361a1e541bfSJim Ingham options.SetAutoApplyFixIts(auto_apply_fixits); 362a1e541bfSJim Ingham 363863fab69SSean Callanan if (m_command_options.top_level) 364863fab69SSean Callanan options.SetExecutionPolicy(eExecutionPolicyTopLevel); 365863fab69SSean Callanan 36623f8c95aSGreg Clayton // If there is any chance we are going to stop and want to see 36723f8c95aSGreg Clayton // what went wrong with our expression, we should generate debug info 36823f8c95aSGreg Clayton if (!m_command_options.ignore_breakpoints || 36923f8c95aSGreg Clayton !m_command_options.unwind_on_error) 37023f8c95aSGreg Clayton options.SetGenerateDebugInfo(true); 37123f8c95aSGreg Clayton 37262afb9f6SGreg Clayton if (m_command_options.timeout > 0) 37343d35418SPavel Labath options.SetTimeout(std::chrono::microseconds(m_command_options.timeout)); 3746f78f386SJim Ingham else 37543d35418SPavel Labath options.SetTimeout(llvm::None); 376d4439aa9SEnrico Granata 377b9c1b51eSKate Stone ExpressionResults success = target->EvaluateExpression( 378b9c1b51eSKate Stone expr, frame, result_valobj_sp, options, &m_fixed_expression); 379e5ee6f04SJim Ingham 380b9c1b51eSKate Stone // We only tell you about the FixIt if we applied it. The compiler errors 381b9c1b51eSKate Stone // will suggest the FixIt if it parsed. 382b9c1b51eSKate Stone if (error_stream && !m_fixed_expression.empty() && 383b9c1b51eSKate Stone target->GetEnableNotifyAboutFixIts()) { 384e5ee6f04SJim Ingham if (success == eExpressionCompleted) 385b9c1b51eSKate Stone error_stream->Printf( 386b9c1b51eSKate Stone " Fix-it applied, fixed expression was: \n %s\n", 387b9c1b51eSKate Stone m_fixed_expression.c_str()); 388e5ee6f04SJim Ingham } 3898b2fe6dcSGreg Clayton 390b9c1b51eSKate Stone if (result_valobj_sp) { 391bf154daeSSean Callanan Format format = m_format_options.GetFormat(); 392bf154daeSSean Callanan 393b9c1b51eSKate Stone if (result_valobj_sp->GetError().Success()) { 394b9c1b51eSKate Stone if (format != eFormatVoid) { 3951deb7962SGreg Clayton if (format != eFormatDefault) 3961deb7962SGreg Clayton result_valobj_sp->SetFormat(format); 39732c4085bSGreg Clayton 398b9c1b51eSKate Stone if (m_varobj_options.elem_count > 0) { 39997206d57SZachary Turner Status error(CanBeUsedForElementCountPrinting(*result_valobj_sp)); 400b9c1b51eSKate Stone if (error.Fail()) { 401b9c1b51eSKate Stone result->AppendErrorWithFormat( 402b9c1b51eSKate Stone "expression cannot be used with --element-count %s\n", 403b9c1b51eSKate Stone error.AsCString("")); 404520a422bSEnrico Granata result->SetStatus(eReturnStatusFailed); 405520a422bSEnrico Granata return false; 406520a422bSEnrico Granata } 407520a422bSEnrico Granata } 408520a422bSEnrico Granata 409b9c1b51eSKate Stone DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions( 410b9c1b51eSKate Stone m_command_options.m_verbosity, format)); 411b9c1b51eSKate Stone options.SetVariableFormatDisplayLanguage( 412b9c1b51eSKate Stone result_valobj_sp->GetPreferredDisplayLanguage()); 413770eb05aSEnrico Granata 4144d93b8cdSEnrico Granata result_valobj_sp->Dump(*output_stream, options); 4154d93b8cdSEnrico Granata 416fcd43b71SJohnny Chen if (result) 417fcd43b71SJohnny Chen result->SetStatus(eReturnStatusSuccessFinishResult); 41830fdc8d8SChris Lattner } 419b9c1b51eSKate Stone } else { 420b9c1b51eSKate Stone if (result_valobj_sp->GetError().GetError() == 421b9c1b51eSKate Stone UserExpression::kNoResult) { 422b9c1b51eSKate Stone if (format != eFormatVoid && 423b9c1b51eSKate Stone m_interpreter.GetDebugger().GetNotifyVoid()) { 424bcf897faSSean Callanan error_stream->PutCString("(void)\n"); 425bcf897faSSean Callanan } 426bccce813SSean Callanan 427bccce813SSean Callanan if (result) 428bccce813SSean Callanan result->SetStatus(eReturnStatusSuccessFinishResult); 429b9c1b51eSKate Stone } else { 4305fd05903SGreg Clayton const char *error_cstr = result_valobj_sp->GetError().AsCString(); 431b9c1b51eSKate Stone if (error_cstr && error_cstr[0]) { 432c7bece56SGreg Clayton const size_t error_cstr_len = strlen(error_cstr); 433b9c1b51eSKate Stone const bool ends_with_newline = 434b9c1b51eSKate Stone error_cstr[error_cstr_len - 1] == '\n'; 4355fd05903SGreg Clayton if (strstr(error_cstr, "error:") != error_cstr) 4365fd05903SGreg Clayton error_stream->PutCString("error: "); 4375fd05903SGreg Clayton error_stream->Write(error_cstr, error_cstr_len); 4385fd05903SGreg Clayton if (!ends_with_newline) 4395fd05903SGreg Clayton error_stream->EOL(); 440b9c1b51eSKate Stone } else { 4415fd05903SGreg Clayton error_stream->PutCString("error: unknown error\n"); 4425fd05903SGreg Clayton } 4435fd05903SGreg Clayton 444fcd43b71SJohnny Chen if (result) 445b71f3844SGreg Clayton result->SetStatus(eReturnStatusFailed); 44630fdc8d8SChris Lattner } 4478b2fe6dcSGreg Clayton } 4488b2fe6dcSGreg Clayton } 449b9c1b51eSKate Stone } else { 4506e8dc334SCaroline Tice error_stream->Printf("error: invalid execution context for expression\n"); 4518b2fe6dcSGreg Clayton return false; 4528b2fe6dcSGreg Clayton } 45330fdc8d8SChris Lattner 45416ad5faeSSean Callanan return true; 45530fdc8d8SChris Lattner } 45630fdc8d8SChris Lattner 457b9c1b51eSKate Stone void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler, 458b9c1b51eSKate Stone std::string &line) { 45944d93782SGreg Clayton io_handler.SetIsDone(true); 460b9c1b51eSKate Stone // StreamSP output_stream = 461b9c1b51eSKate Stone // io_handler.GetDebugger().GetAsyncOutputStream(); 46244d93782SGreg Clayton // StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream(); 46344d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 46444d93782SGreg Clayton StreamFileSP error_sp(io_handler.GetErrorStreamFile()); 46544d93782SGreg Clayton 466b9c1b51eSKate Stone EvaluateExpression(line.c_str(), output_sp.get(), error_sp.get()); 46744d93782SGreg Clayton if (output_sp) 46844d93782SGreg Clayton output_sp->Flush(); 46944d93782SGreg Clayton if (error_sp) 47044d93782SGreg Clayton error_sp->Flush(); 47144d93782SGreg Clayton } 47244d93782SGreg Clayton 473b9c1b51eSKate Stone bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler, 474b9c1b51eSKate Stone StringList &lines) { 475f52c40c5SSean Callanan // An empty lines is used to indicate the end of input 476f52c40c5SSean Callanan const size_t num_lines = lines.GetSize(); 477b9c1b51eSKate Stone if (num_lines > 0 && lines[num_lines - 1].empty()) { 478f52c40c5SSean Callanan // Remove the last empty line from "lines" so it doesn't appear 479f52c40c5SSean Callanan // in our resulting input and return true to indicate we are done 480f52c40c5SSean Callanan // getting lines 48144d93782SGreg Clayton lines.PopBack(); 482f52c40c5SSean Callanan return true; 48344d93782SGreg Clayton } 484f52c40c5SSean Callanan return false; 48544d93782SGreg Clayton } 48644d93782SGreg Clayton 487b9c1b51eSKate Stone void CommandObjectExpression::GetMultilineExpression() { 48830fdc8d8SChris Lattner m_expr_lines.clear(); 48930fdc8d8SChris Lattner m_expr_line_count = 0; 49030fdc8d8SChris Lattner 49144d93782SGreg Clayton Debugger &debugger = GetCommandInterpreter().GetDebugger(); 492e30f11d9SKate Stone bool color_prompt = debugger.GetUseColor(); 49344d93782SGreg Clayton const bool multiple_lines = true; // Get multiple lines 494b9c1b51eSKate Stone IOHandlerSP io_handler_sp( 495b9c1b51eSKate Stone new IOHandlerEditline(debugger, IOHandler::Type::Expression, 49644d93782SGreg Clayton "lldb-expr", // Name of input reader for history 497514d8cd8SZachary Turner llvm::StringRef(), // No prompt 498514d8cd8SZachary Turner llvm::StringRef(), // Continuation prompt 499b9c1b51eSKate Stone multiple_lines, color_prompt, 500f6913cd7SGreg Clayton 1, // Show line numbers starting at 1 50144d93782SGreg Clayton *this)); 502b6892508SGreg Clayton 503b6892508SGreg Clayton StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile()); 504b9c1b51eSKate Stone if (output_sp) { 505b9c1b51eSKate Stone output_sp->PutCString( 506b9c1b51eSKate Stone "Enter expressions, then terminate with an empty line to evaluate:\n"); 507b6892508SGreg Clayton output_sp->Flush(); 508b6892508SGreg Clayton } 50944d93782SGreg Clayton debugger.PushIOHandler(io_handler_sp); 510cf28a8b7SGreg Clayton } 511cf28a8b7SGreg Clayton 512b9c1b51eSKate Stone bool CommandObjectExpression::DoExecute(const char *command, 513b9c1b51eSKate Stone CommandReturnObject &result) { 514e5ee6f04SJim Ingham m_fixed_expression.clear(); 515e1cfbc79STodd Fiala auto exe_ctx = GetCommandInterpreter().GetExecutionContext(); 516e1cfbc79STodd Fiala m_option_group.NotifyOptionParsingStarting(&exe_ctx); 517cf28a8b7SGreg Clayton 518c8ecc2a9SEugene Zelenko const char *expr = nullptr; 519cf28a8b7SGreg Clayton 520b9c1b51eSKate Stone if (command[0] == '\0') { 521cf28a8b7SGreg Clayton GetMultilineExpression(); 52230fdc8d8SChris Lattner return result.Succeeded(); 52330fdc8d8SChris Lattner } 52430fdc8d8SChris Lattner 525b9c1b51eSKate Stone if (command[0] == '-') { 52630fdc8d8SChris Lattner // We have some options and these options MUST end with --. 527c8ecc2a9SEugene Zelenko const char *end_options = nullptr; 52830fdc8d8SChris Lattner const char *s = command; 529b9c1b51eSKate Stone while (s && s[0]) { 53030fdc8d8SChris Lattner end_options = ::strstr(s, "--"); 531b9c1b51eSKate Stone if (end_options) { 53230fdc8d8SChris Lattner end_options += 2; // Get past the "--" 533b9c1b51eSKate Stone if (::isspace(end_options[0])) { 53430fdc8d8SChris Lattner expr = end_options; 53530fdc8d8SChris Lattner while (::isspace(*expr)) 53630fdc8d8SChris Lattner ++expr; 53730fdc8d8SChris Lattner break; 53830fdc8d8SChris Lattner } 53930fdc8d8SChris Lattner } 54030fdc8d8SChris Lattner s = end_options; 54130fdc8d8SChris Lattner } 54230fdc8d8SChris Lattner 543b9c1b51eSKate Stone if (end_options) { 54400b7f95bSPavel Labath Args args(llvm::StringRef(command, end_options - command)); 545a7015092SGreg Clayton if (!ParseOptions(args, result)) 54630fdc8d8SChris Lattner return false; 547f6b8b581SGreg Clayton 54897206d57SZachary Turner Status error(m_option_group.NotifyOptionParsingFinished(&exe_ctx)); 549b9c1b51eSKate Stone if (error.Fail()) { 550f6b8b581SGreg Clayton result.AppendError(error.AsCString()); 551f6b8b581SGreg Clayton result.SetStatus(eReturnStatusFailed); 552f6b8b581SGreg Clayton return false; 553f6b8b581SGreg Clayton } 554cf28a8b7SGreg Clayton 555b9c1b51eSKate Stone if (m_repl_option.GetOptionValue().GetCurrentValue()) { 556f2bd5c3eSSean Callanan Target *target = m_interpreter.GetExecutionContext().GetTargetPtr(); 557b9c1b51eSKate Stone if (target) { 558f2bd5c3eSSean Callanan // Drop into REPL 559f2bd5c3eSSean Callanan m_expr_lines.clear(); 560f2bd5c3eSSean Callanan m_expr_line_count = 0; 561f2bd5c3eSSean Callanan 562f2bd5c3eSSean Callanan Debugger &debugger = target->GetDebugger(); 563f2bd5c3eSSean Callanan 564b9c1b51eSKate Stone // Check if the LLDB command interpreter is sitting on top of a REPL 565b9c1b51eSKate Stone // that 566f2bd5c3eSSean Callanan // launched it... 567b9c1b51eSKate Stone if (debugger.CheckTopIOHandlerTypes( 568b9c1b51eSKate Stone IOHandler::Type::CommandInterpreter, IOHandler::Type::REPL)) { 569b9c1b51eSKate Stone // the LLDB command interpreter is sitting on top of a REPL that 570b9c1b51eSKate Stone // launched it, 571b9c1b51eSKate Stone // so just say the command interpreter is done and fall back to the 572b9c1b51eSKate Stone // existing REPL 573f2bd5c3eSSean Callanan m_interpreter.GetIOHandler(false)->SetIsDone(true); 574b9c1b51eSKate Stone } else { 575b9c1b51eSKate Stone // We are launching the REPL on top of the current LLDB command 576b9c1b51eSKate Stone // interpreter, 577f2bd5c3eSSean Callanan // so just push one 578f2bd5c3eSSean Callanan bool initialize = false; 57997206d57SZachary Turner Status repl_error; 580b9c1b51eSKate Stone REPLSP repl_sp(target->GetREPL( 581b9c1b51eSKate Stone repl_error, m_command_options.language, nullptr, false)); 582f2bd5c3eSSean Callanan 583b9c1b51eSKate Stone if (!repl_sp) { 584f2bd5c3eSSean Callanan initialize = true; 585b9c1b51eSKate Stone repl_sp = target->GetREPL(repl_error, m_command_options.language, 586b9c1b51eSKate Stone nullptr, true); 587b9c1b51eSKate Stone if (!repl_error.Success()) { 588f2bd5c3eSSean Callanan result.SetError(repl_error); 589f2bd5c3eSSean Callanan return result.Succeeded(); 590f2bd5c3eSSean Callanan } 591f2bd5c3eSSean Callanan } 592f2bd5c3eSSean Callanan 593b9c1b51eSKate Stone if (repl_sp) { 594b9c1b51eSKate Stone if (initialize) { 595f2bd5c3eSSean Callanan repl_sp->SetCommandOptions(m_command_options); 596f2bd5c3eSSean Callanan repl_sp->SetFormatOptions(m_format_options); 597f2bd5c3eSSean Callanan repl_sp->SetValueObjectDisplayOptions(m_varobj_options); 598f2bd5c3eSSean Callanan } 599f2bd5c3eSSean Callanan 600f2bd5c3eSSean Callanan IOHandlerSP io_handler_sp(repl_sp->GetIOHandler()); 601f2bd5c3eSSean Callanan 602f2bd5c3eSSean Callanan io_handler_sp->SetIsDone(false); 603f2bd5c3eSSean Callanan 604f2bd5c3eSSean Callanan debugger.PushIOHandler(io_handler_sp); 605b9c1b51eSKate Stone } else { 606b9c1b51eSKate Stone repl_error.SetErrorStringWithFormat( 607b9c1b51eSKate Stone "Couldn't create a REPL for %s", 608b9c1b51eSKate Stone Language::GetNameForLanguageType(m_command_options.language)); 609f2bd5c3eSSean Callanan result.SetError(repl_error); 610f2bd5c3eSSean Callanan return result.Succeeded(); 611f2bd5c3eSSean Callanan } 612f2bd5c3eSSean Callanan } 613f2bd5c3eSSean Callanan } 614f2bd5c3eSSean Callanan } 615cf28a8b7SGreg Clayton // No expression following options 616b9c1b51eSKate Stone else if (expr == nullptr || expr[0] == '\0') { 617cf28a8b7SGreg Clayton GetMultilineExpression(); 618cf28a8b7SGreg Clayton return result.Succeeded(); 619cf28a8b7SGreg Clayton } 62030fdc8d8SChris Lattner } 62130fdc8d8SChris Lattner } 62230fdc8d8SChris Lattner 623c8ecc2a9SEugene Zelenko if (expr == nullptr) 62430fdc8d8SChris Lattner expr = command; 62530fdc8d8SChris Lattner 626*0df817aaSDavide Italiano Target *target = GetSelectedOrDummyTarget(); 62724fff242SDavide Italiano if (EvaluateExpression(expr, &(result.GetOutputStream()), 62824fff242SDavide Italiano &(result.GetErrorStream()), &result)) { 62924fff242SDavide Italiano 630b9c1b51eSKate Stone if (!m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) { 631e5ee6f04SJim Ingham CommandHistory &history = m_interpreter.GetCommandHistory(); 632b9c1b51eSKate Stone // FIXME: Can we figure out what the user actually typed (e.g. some alias 633b9c1b51eSKate Stone // for expr???) 634e5ee6f04SJim Ingham // If we can it would be nice to show that. 635e5ee6f04SJim Ingham std::string fixed_command("expression "); 636e5ee6f04SJim Ingham if (expr == command) 637e5ee6f04SJim Ingham fixed_command.append(m_fixed_expression); 638b9c1b51eSKate Stone else { 639e5ee6f04SJim Ingham // Add in any options that might have been in the original command: 640e5ee6f04SJim Ingham fixed_command.append(command, expr - command); 641e5ee6f04SJim Ingham fixed_command.append(m_fixed_expression); 642e5ee6f04SJim Ingham } 643e5ee6f04SJim Ingham history.AppendString(fixed_command); 644e5ee6f04SJim Ingham } 64524fff242SDavide Italiano // Increment statistics to record this expression evaluation 64624fff242SDavide Italiano // success. 64724fff242SDavide Italiano target->IncrementStats(StatisticKind::ExpressionSuccessful); 648fcd43b71SJohnny Chen return true; 649e5ee6f04SJim Ingham } 650fcd43b71SJohnny Chen 65124fff242SDavide Italiano // Increment statistics to record this expression evaluation 65224fff242SDavide Italiano // failure. 65324fff242SDavide Italiano target->IncrementStats(StatisticKind::ExpressionFailure); 654fcd43b71SJohnny Chen result.SetStatus(eReturnStatusFailed); 655fcd43b71SJohnny Chen return false; 65630fdc8d8SChris Lattner } 657