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" 275275aaa0SVince Harron #include "lldb/Host/StringConvert.h" 286611103cSGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h" 2930fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 3030fdc8d8SChris Lattner #include "lldb/Symbol/ObjectFile.h" 3130fdc8d8SChris Lattner #include "lldb/Symbol/Variable.h" 32b9c1b51eSKate Stone #include "lldb/Target/Language.h" 3330fdc8d8SChris Lattner #include "lldb/Target/Process.h" 34b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h" 3530fdc8d8SChris Lattner #include "lldb/Target/Target.h" 367260f620SGreg Clayton #include "lldb/Target/Thread.h" 3730fdc8d8SChris Lattner 3830fdc8d8SChris Lattner using namespace lldb; 3930fdc8d8SChris Lattner using namespace lldb_private; 4030fdc8d8SChris Lattner 41b9c1b51eSKate Stone CommandObjectExpression::CommandOptions::CommandOptions() : OptionGroup() {} 4230fdc8d8SChris Lattner 43c8ecc2a9SEugene Zelenko CommandObjectExpression::CommandOptions::~CommandOptions() = default; 4430fdc8d8SChris Lattner 45b9c1b51eSKate Stone static OptionEnumValueElement g_description_verbosity_type[] = { 46b9c1b51eSKate Stone {eLanguageRuntimeDescriptionDisplayVerbosityCompact, "compact", 47b9c1b51eSKate Stone "Only show the description string"}, 48b9c1b51eSKate Stone {eLanguageRuntimeDescriptionDisplayVerbosityFull, "full", 49b9c1b51eSKate Stone "Show the full output, including persistent variable's name and type"}, 50b9c1b51eSKate Stone {0, nullptr, nullptr}}; 514d93b8cdSEnrico Granata 52b9c1b51eSKate Stone OptionDefinition CommandObjectExpression::CommandOptions::g_option_table[] = { 53ac9c3a62SKate Stone // clang-format off 54c8ecc2a9SEugene 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."}, 55c8ecc2a9SEugene 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"}, 56c8ecc2a9SEugene 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."}, 57ac9c3a62SKate 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. " 58ac9c3a62SKate Stone "Note, unlike gdb hitting a breakpoint is controlled by another option (-i)."}, 59ac9c3a62SKate 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 " 60ac9c3a62SKate Stone "and forcing breakpoints to not be ignored (-i0) and no unwinding to happen on error (-u0)."}, 61ac9c3a62SKate 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 " 62ac9c3a62SKate Stone "setting is used." }, 63279b2e88SJim 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." }, 64c8ecc2a9SEugene 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."}, 65ac9c3a62SKate Stone {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "top-level", 'p', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Interpret the expression as top-level definitions rather than code to be immediately " 66ac9c3a62SKate Stone "executed."}, 67ac9c3a62SKate 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 " 68ac9c3a62SKate Stone "the interpreter (defaults to true)."} 69ac9c3a62SKate Stone // clang-format on 701deb7962SGreg Clayton }; 711deb7962SGreg Clayton 72b9c1b51eSKate Stone uint32_t CommandObjectExpression::CommandOptions::GetNumDefinitions() { 7328606954SSaleem Abdulrasool return llvm::array_lengthof(g_option_table); 741deb7962SGreg Clayton } 751deb7962SGreg Clayton 76b9c1b51eSKate Stone Error CommandObjectExpression::CommandOptions::SetOptionValue( 77b9c1b51eSKate Stone uint32_t option_idx, const char *option_arg, 78b9c1b51eSKate Stone ExecutionContext *execution_context) { 7930fdc8d8SChris Lattner Error error; 8030fdc8d8SChris Lattner 81*ecbb0bb1SZachary Turner auto option_strref = llvm::StringRef::withNullAsEmpty(option_arg); 823bcdfc0eSGreg Clayton const int short_option = g_option_table[option_idx].short_option; 8330fdc8d8SChris Lattner 84b9c1b51eSKate Stone switch (short_option) { 8515663c53SDawn Perchik case 'l': 860e0984eeSJim Ingham language = Language::GetLanguageTypeFromString(option_arg); 8715663c53SDawn Perchik if (language == eLanguageTypeUnknown) 88b9c1b51eSKate Stone error.SetErrorStringWithFormat( 89b9c1b51eSKate Stone "unknown language type: '%s' for expression", option_arg); 9015663c53SDawn Perchik break; 9130fdc8d8SChris Lattner 92b9c1b51eSKate Stone case 'a': { 9335e1bda6SJim Ingham bool success; 9435e1bda6SJim Ingham bool result; 95*ecbb0bb1SZachary Turner result = Args::StringToBoolean(option_strref, true, &success); 9635e1bda6SJim Ingham if (!success) 97b9c1b51eSKate Stone error.SetErrorStringWithFormat( 98b9c1b51eSKate Stone "invalid all-threads value setting: \"%s\"", option_arg); 9935e1bda6SJim Ingham else 10035e1bda6SJim Ingham try_all_threads = result; 101b9c1b51eSKate Stone } break; 1026c68fb45SJim Ingham 103b9c1b51eSKate Stone case 'i': { 104184e9811SJim Ingham bool success; 105*ecbb0bb1SZachary Turner bool tmp_value = Args::StringToBoolean(option_strref, true, &success); 106184e9811SJim Ingham if (success) 107184e9811SJim Ingham ignore_breakpoints = tmp_value; 108184e9811SJim Ingham else 109b9c1b51eSKate Stone error.SetErrorStringWithFormat( 110b9c1b51eSKate Stone "could not convert \"%s\" to a boolean value.", option_arg); 111184e9811SJim Ingham break; 112184e9811SJim Ingham } 1133fe71581SMarianne Mailhot-Sarrasin 114b9c1b51eSKate Stone case 'j': { 1153fe71581SMarianne Mailhot-Sarrasin bool success; 116*ecbb0bb1SZachary Turner bool tmp_value = Args::StringToBoolean(option_strref, true, &success); 1173fe71581SMarianne Mailhot-Sarrasin if (success) 1183fe71581SMarianne Mailhot-Sarrasin allow_jit = tmp_value; 1193fe71581SMarianne Mailhot-Sarrasin else 120b9c1b51eSKate Stone error.SetErrorStringWithFormat( 121b9c1b51eSKate Stone "could not convert \"%s\" to a boolean value.", option_arg); 1223fe71581SMarianne Mailhot-Sarrasin break; 1233fe71581SMarianne Mailhot-Sarrasin } 1243fe71581SMarianne Mailhot-Sarrasin 125b9c1b51eSKate Stone case 't': { 12635e1bda6SJim Ingham bool success; 12735e1bda6SJim Ingham uint32_t result; 1285275aaa0SVince Harron result = StringConvert::ToUInt32(option_arg, 0, 0, &success); 12935e1bda6SJim Ingham if (success) 13035e1bda6SJim Ingham timeout = result; 13135e1bda6SJim Ingham else 132b9c1b51eSKate Stone error.SetErrorStringWithFormat("invalid timeout setting \"%s\"", 133b9c1b51eSKate Stone option_arg); 134b9c1b51eSKate Stone } break; 13535e1bda6SJim Ingham 136b9c1b51eSKate Stone case 'u': { 137399f1cafSJim Ingham bool success; 138*ecbb0bb1SZachary Turner bool tmp_value = Args::StringToBoolean(option_strref, true, &success); 139184e9811SJim Ingham if (success) 140184e9811SJim Ingham unwind_on_error = tmp_value; 141184e9811SJim Ingham else 142b9c1b51eSKate Stone error.SetErrorStringWithFormat( 143b9c1b51eSKate Stone "could not convert \"%s\" to a boolean value.", option_arg); 144399f1cafSJim Ingham break; 1453bfdaa2aSSean Callanan } 1464d93b8cdSEnrico Granata 1474d93b8cdSEnrico Granata case 'v': 148b9c1b51eSKate Stone if (!option_arg) { 1494d93b8cdSEnrico Granata m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull; 1504d93b8cdSEnrico Granata break; 1514d93b8cdSEnrico Granata } 152b9c1b51eSKate Stone m_verbosity = 153b9c1b51eSKate Stone (LanguageRuntimeDescriptionDisplayVerbosity)Args::StringToOptionEnum( 154b9c1b51eSKate Stone option_arg, g_option_table[option_idx].enum_values, 0, error); 1554d93b8cdSEnrico Granata if (!error.Success()) 156b9c1b51eSKate Stone error.SetErrorStringWithFormat( 157b9c1b51eSKate Stone "unrecognized value for description-verbosity '%s'", option_arg); 1584d93b8cdSEnrico Granata break; 1594d93b8cdSEnrico Granata 16062afb9f6SGreg Clayton case 'g': 16162afb9f6SGreg Clayton debug = true; 16262afb9f6SGreg Clayton unwind_on_error = false; 16362afb9f6SGreg Clayton ignore_breakpoints = false; 16462afb9f6SGreg Clayton break; 16562afb9f6SGreg Clayton 166863fab69SSean Callanan case 'p': 167863fab69SSean Callanan top_level = true; 168863fab69SSean Callanan break; 169863fab69SSean Callanan 170b9c1b51eSKate Stone case 'X': { 171a1e541bfSJim Ingham bool success; 172*ecbb0bb1SZachary Turner bool tmp_value = Args::StringToBoolean(option_strref, true, &success); 173a1e541bfSJim Ingham if (success) 174a1e541bfSJim Ingham auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo; 175a1e541bfSJim Ingham else 176b9c1b51eSKate Stone error.SetErrorStringWithFormat( 177b9c1b51eSKate Stone "could not convert \"%s\" to a boolean value.", option_arg); 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 213e0d378b3SGreg Clayton const OptionDefinition * 214b9c1b51eSKate Stone CommandObjectExpression::CommandOptions::GetDefinitions() { 21530fdc8d8SChris Lattner return g_option_table; 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.", 2247428a18cSKate Stone nullptr, 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"( 232ea671fbdSKate Stone Timeouts: 233ea671fbdSKate Stone 234b9c1b51eSKate Stone )" 235b9c1b51eSKate Stone " If the expression can be evaluated statically (without running code) then it will be. \ 236ea671fbdSKate Stone Otherwise, by default the expression will run on the current thread with a short timeout: \ 237ea671fbdSKate Stone currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \ 238ea671fbdSKate Stone and resumed with all threads running. You can use the -a option to disable retrying on all \ 239b9c1b51eSKate Stone threads. You can use the -t option to set a shorter timeout." 240b9c1b51eSKate Stone R"( 241ea671fbdSKate Stone 242ea671fbdSKate Stone User defined variables: 243ea671fbdSKate Stone 244b9c1b51eSKate Stone )" 245b9c1b51eSKate Stone " You can define your own variables for convenience or to be used in subsequent expressions. \ 246ea671fbdSKate Stone You define them the same way you would define variables in C. If the first character of \ 247ea671fbdSKate Stone your user defined variable is a $, then the variable's value will be available in future \ 248b9c1b51eSKate Stone expressions, otherwise it will just be available in the current expression." 249b9c1b51eSKate Stone R"( 250ea671fbdSKate Stone 251ea671fbdSKate Stone Continuing evaluation after a breakpoint: 252ea671fbdSKate Stone 253b9c1b51eSKate Stone )" 254b9c1b51eSKate Stone " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \ 255ea671fbdSKate Stone you are done with your investigation, you can either remove the expression execution frames \ 256ea671fbdSKate Stone from the stack with \"thread return -x\" or if you are still interested in the expression result \ 257ea671fbdSKate Stone you can issue the \"continue\" command and the expression evaluation will complete and the \ 258ea671fbdSKate Stone expression result will be available using the \"thread.completed-expression\" key in the thread \ 259b9c1b51eSKate Stone format." 260b9c1b51eSKate Stone R"( 261ea671fbdSKate Stone 262ea671fbdSKate Stone Examples: 263ea671fbdSKate Stone 264ea671fbdSKate Stone expr my_struct->a = my_array[3] 265ea671fbdSKate Stone expr -f bin -- (index * 8) + 5 266ea671fbdSKate Stone expr unsigned int $foo = 5 267b9c1b51eSKate Stone expr char c[] = \"foo\"; c[0])"); 268405fe67fSCaroline Tice 269405fe67fSCaroline Tice CommandArgumentEntry arg; 270405fe67fSCaroline Tice CommandArgumentData expression_arg; 271405fe67fSCaroline Tice 272405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 273405fe67fSCaroline Tice expression_arg.arg_type = eArgTypeExpression; 274405fe67fSCaroline Tice expression_arg.arg_repetition = eArgRepeatPlain; 275405fe67fSCaroline Tice 276b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the argument 277b9c1b51eSKate Stone // entry. 278405fe67fSCaroline Tice arg.push_back(expression_arg); 279405fe67fSCaroline Tice 280405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 281405fe67fSCaroline Tice m_arguments.push_back(arg); 2821deb7962SGreg Clayton 2835009f9d5SGreg Clayton // Add the "--format" and "--gdb-format" 284b9c1b51eSKate Stone m_option_group.Append(&m_format_options, 285b9c1b51eSKate Stone OptionGroupFormat::OPTION_GROUP_FORMAT | 286b9c1b51eSKate Stone OptionGroupFormat::OPTION_GROUP_GDB_FMT, 287b9c1b51eSKate Stone LLDB_OPT_SET_1); 2881deb7962SGreg Clayton m_option_group.Append(&m_command_options); 289b9c1b51eSKate Stone m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, 290b9c1b51eSKate Stone LLDB_OPT_SET_1 | LLDB_OPT_SET_2); 291f2bd5c3eSSean Callanan m_option_group.Append(&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3); 2921deb7962SGreg Clayton m_option_group.Finalize(); 29330fdc8d8SChris Lattner } 29430fdc8d8SChris Lattner 295c8ecc2a9SEugene Zelenko CommandObjectExpression::~CommandObjectExpression() = default; 29630fdc8d8SChris Lattner 297b9c1b51eSKate Stone Options *CommandObjectExpression::GetOptions() { return &m_option_group; } 29830fdc8d8SChris Lattner 299520a422bSEnrico Granata static lldb_private::Error 300b9c1b51eSKate Stone CanBeUsedForElementCountPrinting(ValueObject &valobj) { 301520a422bSEnrico Granata CompilerType type(valobj.GetCompilerType()); 302520a422bSEnrico Granata CompilerType pointee; 303520a422bSEnrico Granata if (!type.IsPointerType(&pointee)) 304520a422bSEnrico Granata return Error("as it does not refer to a pointer"); 305520a422bSEnrico Granata if (pointee.IsVoidType()) 306520a422bSEnrico Granata return Error("as it refers to a pointer to void"); 307520a422bSEnrico Granata return Error(); 308520a422bSEnrico Granata } 309520a422bSEnrico Granata 310b9c1b51eSKate Stone bool CommandObjectExpression::EvaluateExpression(const char *expr, 3116e8dc334SCaroline Tice Stream *output_stream, 3126e8dc334SCaroline Tice Stream *error_stream, 313b9c1b51eSKate Stone CommandReturnObject *result) { 314ba7b8e2cSGreg Clayton // Don't use m_exe_ctx as this might be called asynchronously 315ba7b8e2cSGreg Clayton // after the command object DoExecute has finished when doing 316ba7b8e2cSGreg Clayton // multi-line expression that use an input reader... 317ba7b8e2cSGreg Clayton ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 318ba7b8e2cSGreg Clayton 319ba7b8e2cSGreg Clayton Target *target = exe_ctx.GetTargetPtr(); 320c0a6e061SSean Callanan 321c0a6e061SSean Callanan if (!target) 322893c932aSJim Ingham target = GetDummyTarget(); 323c0a6e061SSean Callanan 324b9c1b51eSKate Stone if (target) { 3258b2fe6dcSGreg Clayton lldb::ValueObjectSP result_valobj_sp; 32692adcac9SSean Callanan bool keep_in_memory = true; 327009d110dSDawn Perchik StackFrame *frame = exe_ctx.GetFramePtr(); 32892adcac9SSean Callanan 32935e1bda6SJim Ingham EvaluateExpressionOptions options; 3306fbc48bcSJim Ingham options.SetCoerceToId(m_varobj_options.use_objc); 3316fbc48bcSJim Ingham options.SetUnwindOnError(m_command_options.unwind_on_error); 3326fbc48bcSJim Ingham options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints); 3336fbc48bcSJim Ingham options.SetKeepInMemory(keep_in_memory); 3346fbc48bcSJim Ingham options.SetUseDynamic(m_varobj_options.use_dynamic); 3356fbc48bcSJim Ingham options.SetTryAllThreads(m_command_options.try_all_threads); 3366fbc48bcSJim Ingham options.SetDebug(m_command_options.debug); 33715663c53SDawn Perchik options.SetLanguage(m_command_options.language); 338b9c1b51eSKate Stone options.SetExecutionPolicy( 339b9c1b51eSKate Stone m_command_options.allow_jit 340b9c1b51eSKate Stone ? EvaluateExpressionOptions::default_execution_policy 341b9c1b51eSKate Stone : lldb_private::eExecutionPolicyNever); 34215663c53SDawn Perchik 343a1e541bfSJim Ingham bool auto_apply_fixits; 344a1e541bfSJim Ingham if (m_command_options.auto_apply_fixits == eLazyBoolCalculate) 345a1e541bfSJim Ingham auto_apply_fixits = target->GetEnableAutoApplyFixIts(); 346a1e541bfSJim Ingham else 347b9c1b51eSKate Stone auto_apply_fixits = 348b9c1b51eSKate Stone m_command_options.auto_apply_fixits == eLazyBoolYes ? true : false; 349a1e541bfSJim Ingham 350a1e541bfSJim Ingham options.SetAutoApplyFixIts(auto_apply_fixits); 351a1e541bfSJim Ingham 352863fab69SSean Callanan if (m_command_options.top_level) 353863fab69SSean Callanan options.SetExecutionPolicy(eExecutionPolicyTopLevel); 354863fab69SSean Callanan 35523f8c95aSGreg Clayton // If there is any chance we are going to stop and want to see 35623f8c95aSGreg Clayton // what went wrong with our expression, we should generate debug info 35723f8c95aSGreg Clayton if (!m_command_options.ignore_breakpoints || 35823f8c95aSGreg Clayton !m_command_options.unwind_on_error) 35923f8c95aSGreg Clayton options.SetGenerateDebugInfo(true); 36023f8c95aSGreg Clayton 36162afb9f6SGreg Clayton if (m_command_options.timeout > 0) 36262afb9f6SGreg Clayton options.SetTimeoutUsec(m_command_options.timeout); 3636f78f386SJim Ingham else 3646f78f386SJim Ingham options.SetTimeoutUsec(0); 365d4439aa9SEnrico Granata 366b9c1b51eSKate Stone ExpressionResults success = target->EvaluateExpression( 367b9c1b51eSKate Stone expr, frame, result_valobj_sp, options, &m_fixed_expression); 368e5ee6f04SJim Ingham 369b9c1b51eSKate Stone // We only tell you about the FixIt if we applied it. The compiler errors 370b9c1b51eSKate Stone // will suggest the FixIt if it parsed. 371b9c1b51eSKate Stone if (error_stream && !m_fixed_expression.empty() && 372b9c1b51eSKate Stone target->GetEnableNotifyAboutFixIts()) { 373e5ee6f04SJim Ingham if (success == eExpressionCompleted) 374b9c1b51eSKate Stone error_stream->Printf( 375b9c1b51eSKate Stone " Fix-it applied, fixed expression was: \n %s\n", 376b9c1b51eSKate Stone m_fixed_expression.c_str()); 377e5ee6f04SJim Ingham } 3788b2fe6dcSGreg Clayton 379b9c1b51eSKate Stone if (result_valobj_sp) { 380bf154daeSSean Callanan Format format = m_format_options.GetFormat(); 381bf154daeSSean Callanan 382b9c1b51eSKate Stone if (result_valobj_sp->GetError().Success()) { 383b9c1b51eSKate Stone if (format != eFormatVoid) { 3841deb7962SGreg Clayton if (format != eFormatDefault) 3851deb7962SGreg Clayton result_valobj_sp->SetFormat(format); 38632c4085bSGreg Clayton 387b9c1b51eSKate Stone if (m_varobj_options.elem_count > 0) { 388520a422bSEnrico Granata Error error(CanBeUsedForElementCountPrinting(*result_valobj_sp)); 389b9c1b51eSKate Stone if (error.Fail()) { 390b9c1b51eSKate Stone result->AppendErrorWithFormat( 391b9c1b51eSKate Stone "expression cannot be used with --element-count %s\n", 392b9c1b51eSKate Stone error.AsCString("")); 393520a422bSEnrico Granata result->SetStatus(eReturnStatusFailed); 394520a422bSEnrico Granata return false; 395520a422bSEnrico Granata } 396520a422bSEnrico Granata } 397520a422bSEnrico Granata 398b9c1b51eSKate Stone DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions( 399b9c1b51eSKate Stone m_command_options.m_verbosity, format)); 400b9c1b51eSKate Stone options.SetVariableFormatDisplayLanguage( 401b9c1b51eSKate Stone result_valobj_sp->GetPreferredDisplayLanguage()); 402770eb05aSEnrico Granata 4034d93b8cdSEnrico Granata result_valobj_sp->Dump(*output_stream, options); 4044d93b8cdSEnrico Granata 405fcd43b71SJohnny Chen if (result) 406fcd43b71SJohnny Chen result->SetStatus(eReturnStatusSuccessFinishResult); 40730fdc8d8SChris Lattner } 408b9c1b51eSKate Stone } else { 409b9c1b51eSKate Stone if (result_valobj_sp->GetError().GetError() == 410b9c1b51eSKate Stone UserExpression::kNoResult) { 411b9c1b51eSKate Stone if (format != eFormatVoid && 412b9c1b51eSKate Stone m_interpreter.GetDebugger().GetNotifyVoid()) { 413bcf897faSSean Callanan error_stream->PutCString("(void)\n"); 414bcf897faSSean Callanan } 415bccce813SSean Callanan 416bccce813SSean Callanan if (result) 417bccce813SSean Callanan result->SetStatus(eReturnStatusSuccessFinishResult); 418b9c1b51eSKate Stone } else { 4195fd05903SGreg Clayton const char *error_cstr = result_valobj_sp->GetError().AsCString(); 420b9c1b51eSKate Stone if (error_cstr && error_cstr[0]) { 421c7bece56SGreg Clayton const size_t error_cstr_len = strlen(error_cstr); 422b9c1b51eSKate Stone const bool ends_with_newline = 423b9c1b51eSKate Stone error_cstr[error_cstr_len - 1] == '\n'; 4245fd05903SGreg Clayton if (strstr(error_cstr, "error:") != error_cstr) 4255fd05903SGreg Clayton error_stream->PutCString("error: "); 4265fd05903SGreg Clayton error_stream->Write(error_cstr, error_cstr_len); 4275fd05903SGreg Clayton if (!ends_with_newline) 4285fd05903SGreg Clayton error_stream->EOL(); 429b9c1b51eSKate Stone } else { 4305fd05903SGreg Clayton error_stream->PutCString("error: unknown error\n"); 4315fd05903SGreg Clayton } 4325fd05903SGreg Clayton 433fcd43b71SJohnny Chen if (result) 434b71f3844SGreg Clayton result->SetStatus(eReturnStatusFailed); 43530fdc8d8SChris Lattner } 4368b2fe6dcSGreg Clayton } 4378b2fe6dcSGreg Clayton } 438b9c1b51eSKate Stone } else { 4396e8dc334SCaroline Tice error_stream->Printf("error: invalid execution context for expression\n"); 4408b2fe6dcSGreg Clayton return false; 4418b2fe6dcSGreg Clayton } 44230fdc8d8SChris Lattner 44316ad5faeSSean Callanan return true; 44430fdc8d8SChris Lattner } 44530fdc8d8SChris Lattner 446b9c1b51eSKate Stone void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler, 447b9c1b51eSKate Stone std::string &line) { 44844d93782SGreg Clayton io_handler.SetIsDone(true); 449b9c1b51eSKate Stone // StreamSP output_stream = 450b9c1b51eSKate Stone // io_handler.GetDebugger().GetAsyncOutputStream(); 45144d93782SGreg Clayton // StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream(); 45244d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 45344d93782SGreg Clayton StreamFileSP error_sp(io_handler.GetErrorStreamFile()); 45444d93782SGreg Clayton 455b9c1b51eSKate Stone EvaluateExpression(line.c_str(), output_sp.get(), error_sp.get()); 45644d93782SGreg Clayton if (output_sp) 45744d93782SGreg Clayton output_sp->Flush(); 45844d93782SGreg Clayton if (error_sp) 45944d93782SGreg Clayton error_sp->Flush(); 46044d93782SGreg Clayton } 46144d93782SGreg Clayton 462b9c1b51eSKate Stone bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler, 463b9c1b51eSKate Stone StringList &lines) { 464f52c40c5SSean Callanan // An empty lines is used to indicate the end of input 465f52c40c5SSean Callanan const size_t num_lines = lines.GetSize(); 466b9c1b51eSKate Stone if (num_lines > 0 && lines[num_lines - 1].empty()) { 467f52c40c5SSean Callanan // Remove the last empty line from "lines" so it doesn't appear 468f52c40c5SSean Callanan // in our resulting input and return true to indicate we are done 469f52c40c5SSean Callanan // getting lines 47044d93782SGreg Clayton lines.PopBack(); 471f52c40c5SSean Callanan return true; 47244d93782SGreg Clayton } 473f52c40c5SSean Callanan return false; 47444d93782SGreg Clayton } 47544d93782SGreg Clayton 476b9c1b51eSKate Stone void CommandObjectExpression::GetMultilineExpression() { 47730fdc8d8SChris Lattner m_expr_lines.clear(); 47830fdc8d8SChris Lattner m_expr_line_count = 0; 47930fdc8d8SChris Lattner 48044d93782SGreg Clayton Debugger &debugger = GetCommandInterpreter().GetDebugger(); 481e30f11d9SKate Stone bool color_prompt = debugger.GetUseColor(); 48244d93782SGreg Clayton const bool multiple_lines = true; // Get multiple lines 483b9c1b51eSKate Stone IOHandlerSP io_handler_sp( 484b9c1b51eSKate Stone new IOHandlerEditline(debugger, IOHandler::Type::Expression, 48544d93782SGreg Clayton "lldb-expr", // Name of input reader for history 486c8ecc2a9SEugene Zelenko nullptr, // No prompt 487c8ecc2a9SEugene Zelenko nullptr, // Continuation prompt 488b9c1b51eSKate Stone multiple_lines, color_prompt, 489f6913cd7SGreg Clayton 1, // Show line numbers starting at 1 49044d93782SGreg Clayton *this)); 491b6892508SGreg Clayton 492b6892508SGreg Clayton StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile()); 493b9c1b51eSKate Stone if (output_sp) { 494b9c1b51eSKate Stone output_sp->PutCString( 495b9c1b51eSKate Stone "Enter expressions, then terminate with an empty line to evaluate:\n"); 496b6892508SGreg Clayton output_sp->Flush(); 497b6892508SGreg Clayton } 49844d93782SGreg Clayton debugger.PushIOHandler(io_handler_sp); 499cf28a8b7SGreg Clayton } 500cf28a8b7SGreg Clayton 501b9c1b51eSKate Stone bool CommandObjectExpression::DoExecute(const char *command, 502b9c1b51eSKate Stone CommandReturnObject &result) { 503e5ee6f04SJim Ingham m_fixed_expression.clear(); 504e1cfbc79STodd Fiala auto exe_ctx = GetCommandInterpreter().GetExecutionContext(); 505e1cfbc79STodd Fiala m_option_group.NotifyOptionParsingStarting(&exe_ctx); 506cf28a8b7SGreg Clayton 507c8ecc2a9SEugene Zelenko const char *expr = nullptr; 508cf28a8b7SGreg Clayton 509b9c1b51eSKate Stone if (command[0] == '\0') { 510cf28a8b7SGreg Clayton GetMultilineExpression(); 51130fdc8d8SChris Lattner return result.Succeeded(); 51230fdc8d8SChris Lattner } 51330fdc8d8SChris Lattner 514b9c1b51eSKate Stone if (command[0] == '-') { 51530fdc8d8SChris Lattner // We have some options and these options MUST end with --. 516c8ecc2a9SEugene Zelenko const char *end_options = nullptr; 51730fdc8d8SChris Lattner const char *s = command; 518b9c1b51eSKate Stone while (s && s[0]) { 51930fdc8d8SChris Lattner end_options = ::strstr(s, "--"); 520b9c1b51eSKate Stone if (end_options) { 52130fdc8d8SChris Lattner end_options += 2; // Get past the "--" 522b9c1b51eSKate Stone if (::isspace(end_options[0])) { 52330fdc8d8SChris Lattner expr = end_options; 52430fdc8d8SChris Lattner while (::isspace(*expr)) 52530fdc8d8SChris Lattner ++expr; 52630fdc8d8SChris Lattner break; 52730fdc8d8SChris Lattner } 52830fdc8d8SChris Lattner } 52930fdc8d8SChris Lattner s = end_options; 53030fdc8d8SChris Lattner } 53130fdc8d8SChris Lattner 532b9c1b51eSKate Stone if (end_options) { 53300b7f95bSPavel Labath Args args(llvm::StringRef(command, end_options - command)); 534a7015092SGreg Clayton if (!ParseOptions(args, result)) 53530fdc8d8SChris Lattner return false; 536f6b8b581SGreg Clayton 537e1cfbc79STodd Fiala Error error(m_option_group.NotifyOptionParsingFinished(&exe_ctx)); 538b9c1b51eSKate Stone if (error.Fail()) { 539f6b8b581SGreg Clayton result.AppendError(error.AsCString()); 540f6b8b581SGreg Clayton result.SetStatus(eReturnStatusFailed); 541f6b8b581SGreg Clayton return false; 542f6b8b581SGreg Clayton } 543cf28a8b7SGreg Clayton 544b9c1b51eSKate Stone if (m_repl_option.GetOptionValue().GetCurrentValue()) { 545f2bd5c3eSSean Callanan Target *target = m_interpreter.GetExecutionContext().GetTargetPtr(); 546b9c1b51eSKate Stone if (target) { 547f2bd5c3eSSean Callanan // Drop into REPL 548f2bd5c3eSSean Callanan m_expr_lines.clear(); 549f2bd5c3eSSean Callanan m_expr_line_count = 0; 550f2bd5c3eSSean Callanan 551f2bd5c3eSSean Callanan Debugger &debugger = target->GetDebugger(); 552f2bd5c3eSSean Callanan 553b9c1b51eSKate Stone // Check if the LLDB command interpreter is sitting on top of a REPL 554b9c1b51eSKate Stone // that 555f2bd5c3eSSean Callanan // launched it... 556b9c1b51eSKate Stone if (debugger.CheckTopIOHandlerTypes( 557b9c1b51eSKate Stone IOHandler::Type::CommandInterpreter, IOHandler::Type::REPL)) { 558b9c1b51eSKate Stone // the LLDB command interpreter is sitting on top of a REPL that 559b9c1b51eSKate Stone // launched it, 560b9c1b51eSKate Stone // so just say the command interpreter is done and fall back to the 561b9c1b51eSKate Stone // existing REPL 562f2bd5c3eSSean Callanan m_interpreter.GetIOHandler(false)->SetIsDone(true); 563b9c1b51eSKate Stone } else { 564b9c1b51eSKate Stone // We are launching the REPL on top of the current LLDB command 565b9c1b51eSKate Stone // interpreter, 566f2bd5c3eSSean Callanan // so just push one 567f2bd5c3eSSean Callanan bool initialize = false; 568f2bd5c3eSSean Callanan Error repl_error; 569b9c1b51eSKate Stone REPLSP repl_sp(target->GetREPL( 570b9c1b51eSKate Stone repl_error, m_command_options.language, nullptr, false)); 571f2bd5c3eSSean Callanan 572b9c1b51eSKate Stone if (!repl_sp) { 573f2bd5c3eSSean Callanan initialize = true; 574b9c1b51eSKate Stone repl_sp = target->GetREPL(repl_error, m_command_options.language, 575b9c1b51eSKate Stone nullptr, true); 576b9c1b51eSKate Stone if (!repl_error.Success()) { 577f2bd5c3eSSean Callanan result.SetError(repl_error); 578f2bd5c3eSSean Callanan return result.Succeeded(); 579f2bd5c3eSSean Callanan } 580f2bd5c3eSSean Callanan } 581f2bd5c3eSSean Callanan 582b9c1b51eSKate Stone if (repl_sp) { 583b9c1b51eSKate Stone if (initialize) { 584f2bd5c3eSSean Callanan repl_sp->SetCommandOptions(m_command_options); 585f2bd5c3eSSean Callanan repl_sp->SetFormatOptions(m_format_options); 586f2bd5c3eSSean Callanan repl_sp->SetValueObjectDisplayOptions(m_varobj_options); 587f2bd5c3eSSean Callanan } 588f2bd5c3eSSean Callanan 589f2bd5c3eSSean Callanan IOHandlerSP io_handler_sp(repl_sp->GetIOHandler()); 590f2bd5c3eSSean Callanan 591f2bd5c3eSSean Callanan io_handler_sp->SetIsDone(false); 592f2bd5c3eSSean Callanan 593f2bd5c3eSSean Callanan debugger.PushIOHandler(io_handler_sp); 594b9c1b51eSKate Stone } else { 595b9c1b51eSKate Stone repl_error.SetErrorStringWithFormat( 596b9c1b51eSKate Stone "Couldn't create a REPL for %s", 597b9c1b51eSKate Stone Language::GetNameForLanguageType(m_command_options.language)); 598f2bd5c3eSSean Callanan result.SetError(repl_error); 599f2bd5c3eSSean Callanan return result.Succeeded(); 600f2bd5c3eSSean Callanan } 601f2bd5c3eSSean Callanan } 602f2bd5c3eSSean Callanan } 603f2bd5c3eSSean Callanan } 604cf28a8b7SGreg Clayton // No expression following options 605b9c1b51eSKate Stone else if (expr == nullptr || expr[0] == '\0') { 606cf28a8b7SGreg Clayton GetMultilineExpression(); 607cf28a8b7SGreg Clayton return result.Succeeded(); 608cf28a8b7SGreg Clayton } 60930fdc8d8SChris Lattner } 61030fdc8d8SChris Lattner } 61130fdc8d8SChris Lattner 612c8ecc2a9SEugene Zelenko if (expr == nullptr) 61330fdc8d8SChris Lattner expr = command; 61430fdc8d8SChris Lattner 615b9c1b51eSKate Stone if (EvaluateExpression(expr, &(result.GetOutputStream()), 616b9c1b51eSKate Stone &(result.GetErrorStream()), &result)) { 617e5ee6f04SJim Ingham Target *target = m_interpreter.GetExecutionContext().GetTargetPtr(); 618b9c1b51eSKate Stone if (!m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) { 619e5ee6f04SJim Ingham CommandHistory &history = m_interpreter.GetCommandHistory(); 620b9c1b51eSKate Stone // FIXME: Can we figure out what the user actually typed (e.g. some alias 621b9c1b51eSKate Stone // for expr???) 622e5ee6f04SJim Ingham // If we can it would be nice to show that. 623e5ee6f04SJim Ingham std::string fixed_command("expression "); 624e5ee6f04SJim Ingham if (expr == command) 625e5ee6f04SJim Ingham fixed_command.append(m_fixed_expression); 626b9c1b51eSKate Stone else { 627e5ee6f04SJim Ingham // Add in any options that might have been in the original command: 628e5ee6f04SJim Ingham fixed_command.append(command, expr - command); 629e5ee6f04SJim Ingham fixed_command.append(m_fixed_expression); 630e5ee6f04SJim Ingham } 631e5ee6f04SJim Ingham history.AppendString(fixed_command); 632e5ee6f04SJim Ingham } 633fcd43b71SJohnny Chen return true; 634e5ee6f04SJim Ingham } 635fcd43b71SJohnny Chen 636fcd43b71SJohnny Chen result.SetStatus(eReturnStatusFailed); 637fcd43b71SJohnny Chen return false; 63830fdc8d8SChris Lattner } 639