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" 1830fdc8d8SChris Lattner #include "lldb/Core/Value.h" 196c68fb45SJim Ingham #include "lldb/Core/ValueObjectVariable.h" 204d93b8cdSEnrico Granata #include "lldb/DataFormatters/ValueObjectPrinter.h" 2130e33974SSean Callanan #include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h" 22151c032cSJim Ingham #include "lldb/Expression/UserExpression.h" 2330fdc8d8SChris Lattner #include "lldb/Expression/DWARFExpression.h" 24f2bd5c3eSSean Callanan #include "lldb/Expression/REPL.h" 2530fdc8d8SChris Lattner #include "lldb/Host/Host.h" 265275aaa0SVince Harron #include "lldb/Host/StringConvert.h" 27ebf7707eSSean Callanan #include "lldb/Core/Debugger.h" 286611103cSGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h" 2930fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 300e0984eeSJim Ingham #include "lldb/Target/Language.h" 3130fdc8d8SChris Lattner #include "lldb/Symbol/ObjectFile.h" 3230fdc8d8SChris Lattner #include "lldb/Symbol/Variable.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 411deb7962SGreg Clayton CommandObjectExpression::CommandOptions::CommandOptions () : 421deb7962SGreg Clayton OptionGroup() 4330fdc8d8SChris Lattner { 4430fdc8d8SChris Lattner } 4530fdc8d8SChris Lattner 46c8ecc2a9SEugene Zelenko CommandObjectExpression::CommandOptions::~CommandOptions() = default; 4730fdc8d8SChris Lattner 484d93b8cdSEnrico Granata static OptionEnumValueElement g_description_verbosity_type[] = 494d93b8cdSEnrico Granata { 504d93b8cdSEnrico Granata { eLanguageRuntimeDescriptionDisplayVerbosityCompact, "compact", "Only show the description string"}, 514d93b8cdSEnrico Granata { eLanguageRuntimeDescriptionDisplayVerbosityFull, "full", "Show the full output, including persistent variable's name and type"}, 52c8ecc2a9SEugene Zelenko { 0, nullptr, nullptr } 534d93b8cdSEnrico Granata }; 544d93b8cdSEnrico Granata 551deb7962SGreg Clayton OptionDefinition 561deb7962SGreg Clayton CommandObjectExpression::CommandOptions::g_option_table[] = 571deb7962SGreg Clayton { 58c8ecc2a9SEugene 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."}, 59c8ecc2a9SEugene 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"}, 60c8ecc2a9SEugene 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."}, 61c8ecc2a9SEugene Zelenko { 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. Note, unlike gdb hitting a breakpoint is controlled by another option (-i)."}, 62c8ecc2a9SEugene Zelenko { 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 and forcing breakpoints to not be ignored (-i0) and no unwinding to happen on error (-u0)."}, 63c8ecc2a9SEugene Zelenko { 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 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."}, 663fe71581SMarianne Mailhot-Sarrasin { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "top-level", 'p', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Interpret the expression as top-level definitions rather than code to be immediately executed."}, 673fe71581SMarianne Mailhot-Sarrasin { 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 the interpreter (defaults to true)."} 681deb7962SGreg Clayton }; 691deb7962SGreg Clayton 701deb7962SGreg Clayton uint32_t 711deb7962SGreg Clayton CommandObjectExpression::CommandOptions::GetNumDefinitions () 721deb7962SGreg Clayton { 7328606954SSaleem Abdulrasool return llvm::array_lengthof(g_option_table); 741deb7962SGreg Clayton } 751deb7962SGreg Clayton 7630fdc8d8SChris Lattner Error 771deb7962SGreg Clayton CommandObjectExpression::CommandOptions::SetOptionValue (CommandInterpreter &interpreter, 781deb7962SGreg Clayton uint32_t option_idx, 791deb7962SGreg Clayton const char *option_arg) 8030fdc8d8SChris Lattner { 8130fdc8d8SChris Lattner Error error; 8230fdc8d8SChris Lattner 833bcdfc0eSGreg Clayton const int short_option = g_option_table[option_idx].short_option; 8430fdc8d8SChris Lattner 8530fdc8d8SChris Lattner switch (short_option) 8630fdc8d8SChris Lattner { 8715663c53SDawn Perchik case 'l': 880e0984eeSJim Ingham language = Language::GetLanguageTypeFromString (option_arg); 8915663c53SDawn Perchik if (language == eLanguageTypeUnknown) 9015663c53SDawn Perchik error.SetErrorStringWithFormat ("unknown language type: '%s' for expression", option_arg); 9115663c53SDawn Perchik break; 9230fdc8d8SChris Lattner 9335e1bda6SJim Ingham case 'a': 9435e1bda6SJim Ingham { 9535e1bda6SJim Ingham bool success; 9635e1bda6SJim Ingham bool result; 9735e1bda6SJim Ingham result = Args::StringToBoolean(option_arg, true, &success); 9835e1bda6SJim Ingham if (!success) 9935e1bda6SJim Ingham error.SetErrorStringWithFormat("invalid all-threads value setting: \"%s\"", option_arg); 10035e1bda6SJim Ingham else 10135e1bda6SJim Ingham try_all_threads = result; 10235e1bda6SJim Ingham } 1036c68fb45SJim Ingham break; 1046c68fb45SJim Ingham 105184e9811SJim Ingham case 'i': 106184e9811SJim Ingham { 107184e9811SJim Ingham bool success; 108184e9811SJim Ingham bool tmp_value = Args::StringToBoolean(option_arg, true, &success); 109184e9811SJim Ingham if (success) 110184e9811SJim Ingham ignore_breakpoints = tmp_value; 111184e9811SJim Ingham else 112184e9811SJim Ingham error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg); 113184e9811SJim Ingham break; 114184e9811SJim Ingham } 1153fe71581SMarianne Mailhot-Sarrasin 1163fe71581SMarianne Mailhot-Sarrasin case 'j': 1173fe71581SMarianne Mailhot-Sarrasin { 1183fe71581SMarianne Mailhot-Sarrasin bool success; 1193fe71581SMarianne Mailhot-Sarrasin bool tmp_value = Args::StringToBoolean(option_arg, true, &success); 1203fe71581SMarianne Mailhot-Sarrasin if (success) 1213fe71581SMarianne Mailhot-Sarrasin allow_jit = tmp_value; 1223fe71581SMarianne Mailhot-Sarrasin else 1233fe71581SMarianne Mailhot-Sarrasin error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg); 1243fe71581SMarianne Mailhot-Sarrasin break; 1253fe71581SMarianne Mailhot-Sarrasin } 1263fe71581SMarianne Mailhot-Sarrasin 12735e1bda6SJim Ingham case 't': 12835e1bda6SJim Ingham { 12935e1bda6SJim Ingham bool success; 13035e1bda6SJim Ingham uint32_t result; 1315275aaa0SVince Harron result = StringConvert::ToUInt32(option_arg, 0, 0, &success); 13235e1bda6SJim Ingham if (success) 13335e1bda6SJim Ingham timeout = result; 13435e1bda6SJim Ingham else 13535e1bda6SJim Ingham error.SetErrorStringWithFormat ("invalid timeout setting \"%s\"", option_arg); 13635e1bda6SJim Ingham } 13735e1bda6SJim Ingham break; 13835e1bda6SJim Ingham 139399f1cafSJim Ingham case 'u': 1403bfdaa2aSSean Callanan { 141399f1cafSJim Ingham bool success; 142184e9811SJim Ingham bool tmp_value = Args::StringToBoolean(option_arg, true, &success); 143184e9811SJim Ingham if (success) 144184e9811SJim Ingham unwind_on_error = tmp_value; 145184e9811SJim Ingham else 14686edbf41SGreg Clayton error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg); 147399f1cafSJim Ingham break; 1483bfdaa2aSSean Callanan } 1494d93b8cdSEnrico Granata 1504d93b8cdSEnrico Granata case 'v': 1514d93b8cdSEnrico Granata if (!option_arg) 1524d93b8cdSEnrico Granata { 1534d93b8cdSEnrico Granata m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull; 1544d93b8cdSEnrico Granata break; 1554d93b8cdSEnrico Granata } 1564d93b8cdSEnrico Granata m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error); 1574d93b8cdSEnrico Granata if (!error.Success()) 1584d93b8cdSEnrico Granata error.SetErrorStringWithFormat ("unrecognized value for description-verbosity '%s'", option_arg); 1594d93b8cdSEnrico Granata break; 1604d93b8cdSEnrico Granata 16162afb9f6SGreg Clayton case 'g': 16262afb9f6SGreg Clayton debug = true; 16362afb9f6SGreg Clayton unwind_on_error = false; 16462afb9f6SGreg Clayton ignore_breakpoints = false; 16562afb9f6SGreg Clayton break; 16662afb9f6SGreg Clayton 167863fab69SSean Callanan case 'p': 168863fab69SSean Callanan top_level = true; 169863fab69SSean Callanan break; 170863fab69SSean Callanan 171a1e541bfSJim Ingham case 'X': 172a1e541bfSJim Ingham { 173a1e541bfSJim Ingham bool success; 174a1e541bfSJim Ingham bool tmp_value = Args::StringToBoolean(option_arg, true, &success); 175a1e541bfSJim Ingham if (success) 176a1e541bfSJim Ingham auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo; 177a1e541bfSJim Ingham else 178a1e541bfSJim Ingham error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg); 179a1e541bfSJim Ingham break; 180a1e541bfSJim Ingham } 181a1e541bfSJim Ingham 18230fdc8d8SChris Lattner default: 18386edbf41SGreg Clayton error.SetErrorStringWithFormat("invalid short option character '%c'", short_option); 18430fdc8d8SChris Lattner break; 18530fdc8d8SChris Lattner } 18630fdc8d8SChris Lattner 18730fdc8d8SChris Lattner return error; 18830fdc8d8SChris Lattner } 18930fdc8d8SChris Lattner 19030fdc8d8SChris Lattner void 1911deb7962SGreg Clayton CommandObjectExpression::CommandOptions::OptionParsingStarting (CommandInterpreter &interpreter) 19230fdc8d8SChris Lattner { 193184e9811SJim Ingham Process *process = interpreter.GetExecutionContext().GetProcessPtr(); 194c8ecc2a9SEugene Zelenko if (process != nullptr) 195184e9811SJim Ingham { 196184e9811SJim Ingham ignore_breakpoints = process->GetIgnoreBreakpointsInExpressions(); 197184e9811SJim Ingham unwind_on_error = process->GetUnwindOnErrorInExpressions(); 198184e9811SJim Ingham } 199184e9811SJim Ingham else 200184e9811SJim Ingham { 201fc03f8fcSGreg Clayton ignore_breakpoints = true; 202399f1cafSJim Ingham unwind_on_error = true; 203184e9811SJim Ingham } 204184e9811SJim Ingham 20530fdc8d8SChris Lattner show_summary = true; 20635e1bda6SJim Ingham try_all_threads = true; 20735e1bda6SJim Ingham timeout = 0; 20862afb9f6SGreg Clayton debug = false; 20915663c53SDawn Perchik language = eLanguageTypeUnknown; 2104d93b8cdSEnrico Granata m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact; 211a1e541bfSJim Ingham auto_apply_fixits = eLazyBoolCalculate; 212863fab69SSean Callanan top_level = false; 2133fe71581SMarianne Mailhot-Sarrasin allow_jit = true; 21430fdc8d8SChris Lattner } 21530fdc8d8SChris Lattner 216e0d378b3SGreg Clayton const OptionDefinition* 21730fdc8d8SChris Lattner CommandObjectExpression::CommandOptions::GetDefinitions () 21830fdc8d8SChris Lattner { 21930fdc8d8SChris Lattner return g_option_table; 22030fdc8d8SChris Lattner } 22130fdc8d8SChris Lattner 222*7428a18cSKate Stone CommandObjectExpression::CommandObjectExpression(CommandInterpreter &interpreter) 223*7428a18cSKate Stone : CommandObjectRaw( 224*7428a18cSKate Stone interpreter, "expression", 225*7428a18cSKate Stone "Evaluate an expression on the current thread. Displays any returned value with LLDB's default formatting.", 226*7428a18cSKate Stone nullptr, eCommandProcessMustBePaused | eCommandTryTargetAPILock), 22744d93782SGreg Clayton IOHandlerDelegate(IOHandlerDelegate::Completion::Expression), 2281deb7962SGreg Clayton m_option_group(interpreter), 2291deb7962SGreg Clayton m_format_options(eFormatDefault), 230f2bd5c3eSSean Callanan m_repl_option(LLDB_OPT_SET_1, false, "repl", 'r', "Drop into REPL", false, true), 2311deb7962SGreg Clayton m_command_options(), 23230fdc8d8SChris Lattner m_expr_line_count(0), 23330fdc8d8SChris Lattner m_expr_lines() 23430fdc8d8SChris Lattner { 23530fdc8d8SChris Lattner SetHelpLong( 236ea671fbdSKate Stone R"( 237ea671fbdSKate Stone Timeouts: 238ea671fbdSKate Stone 239ea671fbdSKate Stone )" " If the expression can be evaluated statically (without running code) then it will be. \ 240ea671fbdSKate Stone Otherwise, by default the expression will run on the current thread with a short timeout: \ 241ea671fbdSKate Stone currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \ 242ea671fbdSKate Stone and resumed with all threads running. You can use the -a option to disable retrying on all \ 243ea671fbdSKate Stone threads. You can use the -t option to set a shorter timeout." R"( 244ea671fbdSKate Stone 245ea671fbdSKate Stone User defined variables: 246ea671fbdSKate Stone 247ea671fbdSKate Stone )" " You can define your own variables for convenience or to be used in subsequent expressions. \ 248ea671fbdSKate Stone You define them the same way you would define variables in C. If the first character of \ 249ea671fbdSKate Stone your user defined variable is a $, then the variable's value will be available in future \ 250ea671fbdSKate Stone expressions, otherwise it will just be available in the current expression." R"( 251ea671fbdSKate Stone 252ea671fbdSKate Stone Continuing evaluation after a breakpoint: 253ea671fbdSKate Stone 254ea671fbdSKate 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 \ 259ea671fbdSKate Stone format." R"( 260ea671fbdSKate Stone 261ea671fbdSKate Stone Examples: 262ea671fbdSKate Stone 263ea671fbdSKate Stone expr my_struct->a = my_array[3] 264ea671fbdSKate Stone expr -f bin -- (index * 8) + 5 265ea671fbdSKate Stone expr unsigned int $foo = 5 266ea671fbdSKate Stone expr char c[] = \"foo\"; c[0])" 267ea671fbdSKate Stone ); 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 276405fe67fSCaroline Tice // There is only one variant this argument could be; put it into the argument entry. 277405fe67fSCaroline Tice arg.push_back (expression_arg); 278405fe67fSCaroline Tice 279405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 280405fe67fSCaroline Tice m_arguments.push_back (arg); 2811deb7962SGreg Clayton 2825009f9d5SGreg Clayton // Add the "--format" and "--gdb-format" 2835009f9d5SGreg Clayton m_option_group.Append (&m_format_options, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1); 2841deb7962SGreg Clayton m_option_group.Append (&m_command_options); 285b576bba2SEnrico Granata m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1 | LLDB_OPT_SET_2); 286f2bd5c3eSSean Callanan m_option_group.Append (&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3); 2871deb7962SGreg Clayton m_option_group.Finalize(); 28830fdc8d8SChris Lattner } 28930fdc8d8SChris Lattner 290c8ecc2a9SEugene Zelenko CommandObjectExpression::~CommandObjectExpression() = default; 29130fdc8d8SChris Lattner 29230fdc8d8SChris Lattner Options * 29330fdc8d8SChris Lattner CommandObjectExpression::GetOptions () 29430fdc8d8SChris Lattner { 2951deb7962SGreg Clayton return &m_option_group; 29630fdc8d8SChris Lattner } 29730fdc8d8SChris Lattner 298520a422bSEnrico Granata static lldb_private::Error 299520a422bSEnrico Granata CanBeUsedForElementCountPrinting (ValueObject& valobj) 300520a422bSEnrico Granata { 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 31030fdc8d8SChris Lattner bool 311c8ecc2a9SEugene Zelenko CommandObjectExpression::EvaluateExpression(const char *expr, 3126e8dc334SCaroline Tice Stream *output_stream, 3136e8dc334SCaroline Tice Stream *error_stream, 314c8ecc2a9SEugene Zelenko CommandReturnObject *result) 31530fdc8d8SChris Lattner { 316ba7b8e2cSGreg Clayton // Don't use m_exe_ctx as this might be called asynchronously 317ba7b8e2cSGreg Clayton // after the command object DoExecute has finished when doing 318ba7b8e2cSGreg Clayton // multi-line expression that use an input reader... 319ba7b8e2cSGreg Clayton ExecutionContext exe_ctx (m_interpreter.GetExecutionContext()); 320ba7b8e2cSGreg Clayton 321ba7b8e2cSGreg Clayton Target *target = exe_ctx.GetTargetPtr(); 322c0a6e061SSean Callanan 323c0a6e061SSean Callanan if (!target) 324893c932aSJim Ingham target = GetDummyTarget(); 325c0a6e061SSean Callanan 326c14ee32dSGreg Clayton if (target) 3276961e878SSean Callanan { 3288b2fe6dcSGreg Clayton lldb::ValueObjectSP result_valobj_sp; 32992adcac9SSean Callanan bool keep_in_memory = true; 330009d110dSDawn Perchik StackFrame *frame = exe_ctx.GetFramePtr(); 33192adcac9SSean Callanan 33235e1bda6SJim Ingham EvaluateExpressionOptions options; 3336fbc48bcSJim Ingham options.SetCoerceToId(m_varobj_options.use_objc); 3346fbc48bcSJim Ingham options.SetUnwindOnError(m_command_options.unwind_on_error); 3356fbc48bcSJim Ingham options.SetIgnoreBreakpoints (m_command_options.ignore_breakpoints); 3366fbc48bcSJim Ingham options.SetKeepInMemory(keep_in_memory); 3376fbc48bcSJim Ingham options.SetUseDynamic(m_varobj_options.use_dynamic); 3386fbc48bcSJim Ingham options.SetTryAllThreads(m_command_options.try_all_threads); 3396fbc48bcSJim Ingham options.SetDebug(m_command_options.debug); 34015663c53SDawn Perchik options.SetLanguage(m_command_options.language); 3413fe71581SMarianne Mailhot-Sarrasin options.SetExecutionPolicy(m_command_options.allow_jit ? 3423fe71581SMarianne Mailhot-Sarrasin EvaluateExpressionOptions::default_execution_policy : 3433fe71581SMarianne Mailhot-Sarrasin lldb_private::eExecutionPolicyNever); 34415663c53SDawn Perchik 345a1e541bfSJim Ingham bool auto_apply_fixits; 346a1e541bfSJim Ingham if (m_command_options.auto_apply_fixits == eLazyBoolCalculate) 347a1e541bfSJim Ingham auto_apply_fixits = target->GetEnableAutoApplyFixIts(); 348a1e541bfSJim Ingham else 349a1e541bfSJim Ingham auto_apply_fixits = m_command_options.auto_apply_fixits == eLazyBoolYes ? true : false; 350a1e541bfSJim Ingham 351a1e541bfSJim Ingham options.SetAutoApplyFixIts(auto_apply_fixits); 352a1e541bfSJim Ingham 353863fab69SSean Callanan if (m_command_options.top_level) 354863fab69SSean Callanan options.SetExecutionPolicy(eExecutionPolicyTopLevel); 355863fab69SSean Callanan 35623f8c95aSGreg Clayton // If there is any chance we are going to stop and want to see 35723f8c95aSGreg Clayton // what went wrong with our expression, we should generate debug info 35823f8c95aSGreg Clayton if (!m_command_options.ignore_breakpoints || 35923f8c95aSGreg Clayton !m_command_options.unwind_on_error) 36023f8c95aSGreg Clayton options.SetGenerateDebugInfo(true); 36123f8c95aSGreg Clayton 36262afb9f6SGreg Clayton if (m_command_options.timeout > 0) 36362afb9f6SGreg Clayton options.SetTimeoutUsec(m_command_options.timeout); 3646f78f386SJim Ingham else 3656f78f386SJim Ingham options.SetTimeoutUsec(0); 366d4439aa9SEnrico Granata 367e5ee6f04SJim Ingham ExpressionResults success = target->EvaluateExpression(expr, frame, result_valobj_sp, options, &m_fixed_expression); 368e5ee6f04SJim Ingham 369e5ee6f04SJim Ingham // We only tell you about the FixIt if we applied it. The compiler errors will suggest the FixIt if it parsed. 370e5ee6f04SJim Ingham if (error_stream && !m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) 371e5ee6f04SJim Ingham { 372e5ee6f04SJim Ingham if (success == eExpressionCompleted) 373279b2e88SJim Ingham error_stream->Printf (" Fix-it applied, fixed expression was: \n %s\n", m_fixed_expression.c_str()); 374e5ee6f04SJim Ingham } 3758b2fe6dcSGreg Clayton 3768b2fe6dcSGreg Clayton if (result_valobj_sp) 3778b2fe6dcSGreg Clayton { 378bf154daeSSean Callanan Format format = m_format_options.GetFormat(); 379bf154daeSSean Callanan 380b71f3844SGreg Clayton if (result_valobj_sp->GetError().Success()) 38130fdc8d8SChris Lattner { 382bf154daeSSean Callanan if (format != eFormatVoid) 383bf154daeSSean Callanan { 3841deb7962SGreg Clayton if (format != eFormatDefault) 3851deb7962SGreg Clayton result_valobj_sp->SetFormat (format); 38632c4085bSGreg Clayton 387520a422bSEnrico Granata if (m_varobj_options.elem_count > 0) 388520a422bSEnrico Granata { 389520a422bSEnrico Granata Error error(CanBeUsedForElementCountPrinting(*result_valobj_sp)); 390520a422bSEnrico Granata if (error.Fail()) 391520a422bSEnrico Granata { 392520a422bSEnrico Granata result->AppendErrorWithFormat("expression cannot be used with --element-count %s\n", error.AsCString("")); 393520a422bSEnrico Granata result->SetStatus(eReturnStatusFailed); 394520a422bSEnrico Granata return false; 395520a422bSEnrico Granata } 396520a422bSEnrico Granata } 397520a422bSEnrico Granata 3984d93b8cdSEnrico Granata DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(m_command_options.m_verbosity,format)); 39973e8c4d0SEnrico Granata options.SetVariableFormatDisplayLanguage(result_valobj_sp->GetPreferredDisplayLanguage()); 400770eb05aSEnrico Granata 4014d93b8cdSEnrico Granata result_valobj_sp->Dump(*output_stream,options); 4024d93b8cdSEnrico Granata 403fcd43b71SJohnny Chen if (result) 404fcd43b71SJohnny Chen result->SetStatus (eReturnStatusSuccessFinishResult); 40530fdc8d8SChris Lattner } 406bf154daeSSean Callanan } 40730fdc8d8SChris Lattner else 40830fdc8d8SChris Lattner { 409151c032cSJim Ingham if (result_valobj_sp->GetError().GetError() == UserExpression::kNoResult) 410bccce813SSean Callanan { 411bcf897faSSean Callanan if (format != eFormatVoid && m_interpreter.GetDebugger().GetNotifyVoid()) 412bf154daeSSean Callanan { 413bcf897faSSean Callanan error_stream->PutCString("(void)\n"); 414bcf897faSSean Callanan } 415bccce813SSean Callanan 416bccce813SSean Callanan if (result) 417bccce813SSean Callanan result->SetStatus (eReturnStatusSuccessFinishResult); 418bccce813SSean Callanan } 419bccce813SSean Callanan else 420bccce813SSean Callanan { 4215fd05903SGreg Clayton const char *error_cstr = result_valobj_sp->GetError().AsCString(); 4225fd05903SGreg Clayton if (error_cstr && error_cstr[0]) 4235fd05903SGreg Clayton { 424c7bece56SGreg Clayton const size_t error_cstr_len = strlen (error_cstr); 4255fd05903SGreg Clayton const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n'; 4265fd05903SGreg Clayton if (strstr(error_cstr, "error:") != error_cstr) 4275fd05903SGreg Clayton error_stream->PutCString ("error: "); 4285fd05903SGreg Clayton error_stream->Write(error_cstr, error_cstr_len); 4295fd05903SGreg Clayton if (!ends_with_newline) 4305fd05903SGreg Clayton error_stream->EOL(); 4315fd05903SGreg Clayton } 4325fd05903SGreg Clayton else 4335fd05903SGreg Clayton { 4345fd05903SGreg Clayton error_stream->PutCString ("error: unknown error\n"); 4355fd05903SGreg Clayton } 4365fd05903SGreg Clayton 437fcd43b71SJohnny Chen if (result) 438b71f3844SGreg Clayton result->SetStatus (eReturnStatusFailed); 43930fdc8d8SChris Lattner } 4408b2fe6dcSGreg Clayton } 4418b2fe6dcSGreg Clayton } 442bccce813SSean Callanan } 4438b2fe6dcSGreg Clayton else 4448b2fe6dcSGreg Clayton { 4456e8dc334SCaroline Tice error_stream->Printf ("error: invalid execution context for expression\n"); 4468b2fe6dcSGreg Clayton return false; 4478b2fe6dcSGreg Clayton } 44830fdc8d8SChris Lattner 44916ad5faeSSean Callanan return true; 45030fdc8d8SChris Lattner } 45130fdc8d8SChris Lattner 45244d93782SGreg Clayton void 45344d93782SGreg Clayton CommandObjectExpression::IOHandlerInputComplete (IOHandler &io_handler, std::string &line) 45444d93782SGreg Clayton { 45544d93782SGreg Clayton io_handler.SetIsDone(true); 45644d93782SGreg Clayton // StreamSP output_stream = io_handler.GetDebugger().GetAsyncOutputStream(); 45744d93782SGreg Clayton // StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream(); 45844d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 45944d93782SGreg Clayton StreamFileSP error_sp(io_handler.GetErrorStreamFile()); 46044d93782SGreg Clayton 46144d93782SGreg Clayton EvaluateExpression (line.c_str(), 46244d93782SGreg Clayton output_sp.get(), 46344d93782SGreg Clayton error_sp.get()); 46444d93782SGreg Clayton if (output_sp) 46544d93782SGreg Clayton output_sp->Flush(); 46644d93782SGreg Clayton if (error_sp) 46744d93782SGreg Clayton error_sp->Flush(); 46844d93782SGreg Clayton } 46944d93782SGreg Clayton 470f52c40c5SSean Callanan bool 471f52c40c5SSean Callanan CommandObjectExpression::IOHandlerIsInputComplete (IOHandler &io_handler, 472f52c40c5SSean Callanan StringList &lines) 47344d93782SGreg Clayton { 474f52c40c5SSean Callanan // An empty lines is used to indicate the end of input 475f52c40c5SSean Callanan const size_t num_lines = lines.GetSize(); 476f52c40c5SSean Callanan if (num_lines > 0 && lines[num_lines - 1].empty()) 47744d93782SGreg Clayton { 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 487cf28a8b7SGreg Clayton void 488cf28a8b7SGreg Clayton CommandObjectExpression::GetMultilineExpression () 48930fdc8d8SChris Lattner { 49030fdc8d8SChris Lattner m_expr_lines.clear(); 49130fdc8d8SChris Lattner m_expr_line_count = 0; 49230fdc8d8SChris Lattner 49344d93782SGreg Clayton Debugger &debugger = GetCommandInterpreter().GetDebugger(); 494e30f11d9SKate Stone bool color_prompt = debugger.GetUseColor(); 49544d93782SGreg Clayton const bool multiple_lines = true; // Get multiple lines 49644d93782SGreg Clayton IOHandlerSP io_handler_sp(new IOHandlerEditline(debugger, 497e30f11d9SKate Stone IOHandler::Type::Expression, 49844d93782SGreg Clayton "lldb-expr", // Name of input reader for history 499c8ecc2a9SEugene Zelenko nullptr, // No prompt 500c8ecc2a9SEugene Zelenko nullptr, // Continuation prompt 50144d93782SGreg Clayton multiple_lines, 502e30f11d9SKate Stone color_prompt, 503f6913cd7SGreg Clayton 1, // Show line numbers starting at 1 50444d93782SGreg Clayton *this)); 505b6892508SGreg Clayton 506b6892508SGreg Clayton StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile()); 507b6892508SGreg Clayton if (output_sp) 508b6892508SGreg Clayton { 509b6892508SGreg Clayton output_sp->PutCString("Enter expressions, then terminate with an empty line to evaluate:\n"); 510b6892508SGreg Clayton output_sp->Flush(); 511b6892508SGreg Clayton } 51244d93782SGreg Clayton debugger.PushIOHandler(io_handler_sp); 513cf28a8b7SGreg Clayton } 514cf28a8b7SGreg Clayton 515cf28a8b7SGreg Clayton bool 516c8ecc2a9SEugene Zelenko CommandObjectExpression::DoExecute(const char *command, 517c8ecc2a9SEugene Zelenko CommandReturnObject &result) 518cf28a8b7SGreg Clayton { 519e5ee6f04SJim Ingham m_fixed_expression.clear(); 520cf28a8b7SGreg Clayton m_option_group.NotifyOptionParsingStarting(); 521cf28a8b7SGreg Clayton 522c8ecc2a9SEugene Zelenko const char * expr = nullptr; 523cf28a8b7SGreg Clayton 524cf28a8b7SGreg Clayton if (command[0] == '\0') 525cf28a8b7SGreg Clayton { 526cf28a8b7SGreg Clayton GetMultilineExpression (); 52730fdc8d8SChris Lattner return result.Succeeded(); 52830fdc8d8SChris Lattner } 52930fdc8d8SChris Lattner 53030fdc8d8SChris Lattner if (command[0] == '-') 53130fdc8d8SChris Lattner { 53230fdc8d8SChris Lattner // We have some options and these options MUST end with --. 533c8ecc2a9SEugene Zelenko const char *end_options = nullptr; 53430fdc8d8SChris Lattner const char *s = command; 53530fdc8d8SChris Lattner while (s && s[0]) 53630fdc8d8SChris Lattner { 53730fdc8d8SChris Lattner end_options = ::strstr (s, "--"); 53830fdc8d8SChris Lattner if (end_options) 53930fdc8d8SChris Lattner { 54030fdc8d8SChris Lattner end_options += 2; // Get past the "--" 54130fdc8d8SChris Lattner if (::isspace (end_options[0])) 54230fdc8d8SChris Lattner { 54330fdc8d8SChris Lattner expr = end_options; 54430fdc8d8SChris Lattner while (::isspace (*expr)) 54530fdc8d8SChris Lattner ++expr; 54630fdc8d8SChris Lattner break; 54730fdc8d8SChris Lattner } 54830fdc8d8SChris Lattner } 54930fdc8d8SChris Lattner s = end_options; 55030fdc8d8SChris Lattner } 55130fdc8d8SChris Lattner 55230fdc8d8SChris Lattner if (end_options) 55330fdc8d8SChris Lattner { 55400b7f95bSPavel Labath Args args (llvm::StringRef(command, end_options - command)); 555a7015092SGreg Clayton if (!ParseOptions (args, result)) 55630fdc8d8SChris Lattner return false; 557f6b8b581SGreg Clayton 5581deb7962SGreg Clayton Error error (m_option_group.NotifyOptionParsingFinished()); 559f6b8b581SGreg Clayton if (error.Fail()) 560f6b8b581SGreg Clayton { 561f6b8b581SGreg Clayton result.AppendError (error.AsCString()); 562f6b8b581SGreg Clayton result.SetStatus (eReturnStatusFailed); 563f6b8b581SGreg Clayton return false; 564f6b8b581SGreg Clayton } 565cf28a8b7SGreg Clayton 566f2bd5c3eSSean Callanan if (m_repl_option.GetOptionValue().GetCurrentValue()) 567f2bd5c3eSSean Callanan { 568f2bd5c3eSSean Callanan Target *target = m_interpreter.GetExecutionContext().GetTargetPtr(); 569f2bd5c3eSSean Callanan if (target) 570f2bd5c3eSSean Callanan { 571f2bd5c3eSSean Callanan // Drop into REPL 572f2bd5c3eSSean Callanan m_expr_lines.clear(); 573f2bd5c3eSSean Callanan m_expr_line_count = 0; 574f2bd5c3eSSean Callanan 575f2bd5c3eSSean Callanan Debugger &debugger = target->GetDebugger(); 576f2bd5c3eSSean Callanan 577f2bd5c3eSSean Callanan // Check if the LLDB command interpreter is sitting on top of a REPL that 578f2bd5c3eSSean Callanan // launched it... 579f2bd5c3eSSean Callanan if (debugger.CheckTopIOHandlerTypes(IOHandler::Type::CommandInterpreter, IOHandler::Type::REPL)) 580f2bd5c3eSSean Callanan { 581f2bd5c3eSSean Callanan // the LLDB command interpreter is sitting on top of a REPL that launched it, 582f2bd5c3eSSean Callanan // so just say the command interpreter is done and fall back to the existing REPL 583f2bd5c3eSSean Callanan m_interpreter.GetIOHandler(false)->SetIsDone(true); 584f2bd5c3eSSean Callanan } 585f2bd5c3eSSean Callanan else 586f2bd5c3eSSean Callanan { 587f2bd5c3eSSean Callanan // We are launching the REPL on top of the current LLDB command interpreter, 588f2bd5c3eSSean Callanan // so just push one 589f2bd5c3eSSean Callanan bool initialize = false; 590f2bd5c3eSSean Callanan Error repl_error; 591f2bd5c3eSSean Callanan REPLSP repl_sp (target->GetREPL(repl_error, m_command_options.language, nullptr, false)); 592f2bd5c3eSSean Callanan 593f2bd5c3eSSean Callanan if (!repl_sp) 594f2bd5c3eSSean Callanan { 595f2bd5c3eSSean Callanan initialize = true; 596f2bd5c3eSSean Callanan repl_sp = target->GetREPL(repl_error, m_command_options.language, nullptr, true); 597f2bd5c3eSSean Callanan if (!repl_error.Success()) 598f2bd5c3eSSean Callanan { 599f2bd5c3eSSean Callanan result.SetError(repl_error); 600f2bd5c3eSSean Callanan return result.Succeeded(); 601f2bd5c3eSSean Callanan } 602f2bd5c3eSSean Callanan } 603f2bd5c3eSSean Callanan 604f2bd5c3eSSean Callanan if (repl_sp) 605f2bd5c3eSSean Callanan { 606f2bd5c3eSSean Callanan if (initialize) 607f2bd5c3eSSean Callanan { 608f2bd5c3eSSean Callanan repl_sp->SetCommandOptions(m_command_options); 609f2bd5c3eSSean Callanan repl_sp->SetFormatOptions(m_format_options); 610f2bd5c3eSSean Callanan repl_sp->SetValueObjectDisplayOptions(m_varobj_options); 611f2bd5c3eSSean Callanan } 612f2bd5c3eSSean Callanan 613f2bd5c3eSSean Callanan IOHandlerSP io_handler_sp (repl_sp->GetIOHandler()); 614f2bd5c3eSSean Callanan 615f2bd5c3eSSean Callanan io_handler_sp->SetIsDone(false); 616f2bd5c3eSSean Callanan 617f2bd5c3eSSean Callanan debugger.PushIOHandler(io_handler_sp); 618f2bd5c3eSSean Callanan } 619f2bd5c3eSSean Callanan else 620f2bd5c3eSSean Callanan { 621f2bd5c3eSSean Callanan repl_error.SetErrorStringWithFormat("Couldn't create a REPL for %s", Language::GetNameForLanguageType(m_command_options.language)); 622f2bd5c3eSSean Callanan result.SetError(repl_error); 623f2bd5c3eSSean Callanan return result.Succeeded(); 624f2bd5c3eSSean Callanan } 625f2bd5c3eSSean Callanan } 626f2bd5c3eSSean Callanan } 627f2bd5c3eSSean Callanan } 628cf28a8b7SGreg Clayton // No expression following options 629c8ecc2a9SEugene Zelenko else if (expr == nullptr || expr[0] == '\0') 630cf28a8b7SGreg Clayton { 631cf28a8b7SGreg Clayton GetMultilineExpression (); 632cf28a8b7SGreg Clayton return result.Succeeded(); 633cf28a8b7SGreg Clayton } 63430fdc8d8SChris Lattner } 63530fdc8d8SChris Lattner } 63630fdc8d8SChris Lattner 637c8ecc2a9SEugene Zelenko if (expr == nullptr) 63830fdc8d8SChris Lattner expr = command; 63930fdc8d8SChris Lattner 6406e8dc334SCaroline Tice if (EvaluateExpression (expr, &(result.GetOutputStream()), &(result.GetErrorStream()), &result)) 641e5ee6f04SJim Ingham { 642e5ee6f04SJim Ingham Target *target = m_interpreter.GetExecutionContext().GetTargetPtr(); 643e5ee6f04SJim Ingham if (!m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) 644e5ee6f04SJim Ingham { 645e5ee6f04SJim Ingham CommandHistory &history = m_interpreter.GetCommandHistory(); 646e5ee6f04SJim Ingham // FIXME: Can we figure out what the user actually typed (e.g. some alias for expr???) 647e5ee6f04SJim Ingham // If we can it would be nice to show that. 648e5ee6f04SJim Ingham std::string fixed_command("expression "); 649e5ee6f04SJim Ingham if (expr == command) 650e5ee6f04SJim Ingham fixed_command.append(m_fixed_expression); 651e5ee6f04SJim Ingham else 652e5ee6f04SJim Ingham { 653e5ee6f04SJim Ingham // Add in any options that might have been in the original command: 654e5ee6f04SJim Ingham fixed_command.append(command, expr - command); 655e5ee6f04SJim Ingham fixed_command.append(m_fixed_expression); 656e5ee6f04SJim Ingham } 657e5ee6f04SJim Ingham history.AppendString(fixed_command); 658e5ee6f04SJim Ingham } 659fcd43b71SJohnny Chen return true; 660e5ee6f04SJim Ingham } 661fcd43b71SJohnny Chen 662fcd43b71SJohnny Chen result.SetStatus (eReturnStatusFailed); 663fcd43b71SJohnny Chen return false; 66430fdc8d8SChris Lattner } 665