15ffd83dbSDimitry Andric //===-- CommandObjectExpression.cpp ---------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 100b57cec5SDimitry Andric 110b57cec5SDimitry Andric #include "CommandObjectExpression.h" 120b57cec5SDimitry Andric #include "lldb/Core/Debugger.h" 1306c3fb27SDimitry Andric #include "lldb/Expression/ExpressionVariable.h" 140b57cec5SDimitry Andric #include "lldb/Expression/REPL.h" 150b57cec5SDimitry Andric #include "lldb/Expression/UserExpression.h" 160b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h" 170b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h" 18fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h" 190b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h" 200b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h" 210b57cec5SDimitry Andric #include "lldb/Target/Language.h" 220b57cec5SDimitry Andric #include "lldb/Target/Process.h" 230b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h" 240b57cec5SDimitry Andric #include "lldb/Target/Target.h" 2506c3fb27SDimitry Andric #include "lldb/lldb-enumerations.h" 2606c3fb27SDimitry Andric #include "lldb/lldb-private-enumerations.h" 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric using namespace lldb; 290b57cec5SDimitry Andric using namespace lldb_private; 300b57cec5SDimitry Andric 3181ad6265SDimitry Andric CommandObjectExpression::CommandOptions::CommandOptions() = default; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric CommandObjectExpression::CommandOptions::~CommandOptions() = default; 340b57cec5SDimitry Andric 359dba64beSDimitry Andric #define LLDB_OPTIONS_expression 369dba64beSDimitry Andric #include "CommandOptions.inc" 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric Status CommandObjectExpression::CommandOptions::SetOptionValue( 390b57cec5SDimitry Andric uint32_t option_idx, llvm::StringRef option_arg, 400b57cec5SDimitry Andric ExecutionContext *execution_context) { 410b57cec5SDimitry Andric Status error; 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric const int short_option = GetDefinitions()[option_idx].short_option; 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric switch (short_option) { 460b57cec5SDimitry Andric case 'l': 470b57cec5SDimitry Andric language = Language::GetLanguageTypeFromString(option_arg); 48bdd1243dSDimitry Andric if (language == eLanguageTypeUnknown) { 49bdd1243dSDimitry Andric StreamString sstr; 50bdd1243dSDimitry Andric sstr.Printf("unknown language type: '%s' for expression. " 51bdd1243dSDimitry Andric "List of supported languages:\n", 520b57cec5SDimitry Andric option_arg.str().c_str()); 53bdd1243dSDimitry Andric 54bdd1243dSDimitry Andric Language::PrintSupportedLanguagesForExpressions(sstr, " ", "\n"); 55bdd1243dSDimitry Andric error.SetErrorString(sstr.GetString()); 56bdd1243dSDimitry Andric } 570b57cec5SDimitry Andric break; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric case 'a': { 600b57cec5SDimitry Andric bool success; 610b57cec5SDimitry Andric bool result; 620b57cec5SDimitry Andric result = OptionArgParser::ToBoolean(option_arg, true, &success); 630b57cec5SDimitry Andric if (!success) 640b57cec5SDimitry Andric error.SetErrorStringWithFormat( 650b57cec5SDimitry Andric "invalid all-threads value setting: \"%s\"", 660b57cec5SDimitry Andric option_arg.str().c_str()); 670b57cec5SDimitry Andric else 680b57cec5SDimitry Andric try_all_threads = result; 690b57cec5SDimitry Andric } break; 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric case 'i': { 720b57cec5SDimitry Andric bool success; 730b57cec5SDimitry Andric bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 740b57cec5SDimitry Andric if (success) 750b57cec5SDimitry Andric ignore_breakpoints = tmp_value; 760b57cec5SDimitry Andric else 770b57cec5SDimitry Andric error.SetErrorStringWithFormat( 780b57cec5SDimitry Andric "could not convert \"%s\" to a boolean value.", 790b57cec5SDimitry Andric option_arg.str().c_str()); 800b57cec5SDimitry Andric break; 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric case 'j': { 840b57cec5SDimitry Andric bool success; 850b57cec5SDimitry Andric bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 860b57cec5SDimitry Andric if (success) 870b57cec5SDimitry Andric allow_jit = tmp_value; 880b57cec5SDimitry Andric else 890b57cec5SDimitry Andric error.SetErrorStringWithFormat( 900b57cec5SDimitry Andric "could not convert \"%s\" to a boolean value.", 910b57cec5SDimitry Andric option_arg.str().c_str()); 920b57cec5SDimitry Andric break; 930b57cec5SDimitry Andric } 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric case 't': 960b57cec5SDimitry Andric if (option_arg.getAsInteger(0, timeout)) { 970b57cec5SDimitry Andric timeout = 0; 980b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid timeout setting \"%s\"", 990b57cec5SDimitry Andric option_arg.str().c_str()); 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric break; 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric case 'u': { 1040b57cec5SDimitry Andric bool success; 1050b57cec5SDimitry Andric bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 1060b57cec5SDimitry Andric if (success) 1070b57cec5SDimitry Andric unwind_on_error = tmp_value; 1080b57cec5SDimitry Andric else 1090b57cec5SDimitry Andric error.SetErrorStringWithFormat( 1100b57cec5SDimitry Andric "could not convert \"%s\" to a boolean value.", 1110b57cec5SDimitry Andric option_arg.str().c_str()); 1120b57cec5SDimitry Andric break; 1130b57cec5SDimitry Andric } 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric case 'v': 1160b57cec5SDimitry Andric if (option_arg.empty()) { 1170b57cec5SDimitry Andric m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull; 1180b57cec5SDimitry Andric break; 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) 1210b57cec5SDimitry Andric OptionArgParser::ToOptionEnum( 1220b57cec5SDimitry Andric option_arg, GetDefinitions()[option_idx].enum_values, 0, error); 1230b57cec5SDimitry Andric if (!error.Success()) 1240b57cec5SDimitry Andric error.SetErrorStringWithFormat( 1250b57cec5SDimitry Andric "unrecognized value for description-verbosity '%s'", 1260b57cec5SDimitry Andric option_arg.str().c_str()); 1270b57cec5SDimitry Andric break; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric case 'g': 1300b57cec5SDimitry Andric debug = true; 1310b57cec5SDimitry Andric unwind_on_error = false; 1320b57cec5SDimitry Andric ignore_breakpoints = false; 1330b57cec5SDimitry Andric break; 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric case 'p': 1360b57cec5SDimitry Andric top_level = true; 1370b57cec5SDimitry Andric break; 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric case 'X': { 1400b57cec5SDimitry Andric bool success; 1410b57cec5SDimitry Andric bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 1420b57cec5SDimitry Andric if (success) 1430b57cec5SDimitry Andric auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo; 1440b57cec5SDimitry Andric else 1450b57cec5SDimitry Andric error.SetErrorStringWithFormat( 1460b57cec5SDimitry Andric "could not convert \"%s\" to a boolean value.", 1470b57cec5SDimitry Andric option_arg.str().c_str()); 1480b57cec5SDimitry Andric break; 1490b57cec5SDimitry Andric } 1500b57cec5SDimitry Andric 15106c3fb27SDimitry Andric case '\x01': { 15206c3fb27SDimitry Andric bool success; 15306c3fb27SDimitry Andric bool persist_result = 15406c3fb27SDimitry Andric OptionArgParser::ToBoolean(option_arg, true, &success); 15506c3fb27SDimitry Andric if (success) 15606c3fb27SDimitry Andric suppress_persistent_result = !persist_result ? eLazyBoolYes : eLazyBoolNo; 15706c3fb27SDimitry Andric else 15806c3fb27SDimitry Andric error.SetErrorStringWithFormat( 15906c3fb27SDimitry Andric "could not convert \"%s\" to a boolean value.", 16006c3fb27SDimitry Andric option_arg.str().c_str()); 16106c3fb27SDimitry Andric break; 16206c3fb27SDimitry Andric } 16306c3fb27SDimitry Andric 1640b57cec5SDimitry Andric default: 1659dba64beSDimitry Andric llvm_unreachable("Unimplemented option"); 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric return error; 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric void CommandObjectExpression::CommandOptions::OptionParsingStarting( 1720b57cec5SDimitry Andric ExecutionContext *execution_context) { 1730b57cec5SDimitry Andric auto process_sp = 1740b57cec5SDimitry Andric execution_context ? execution_context->GetProcessSP() : ProcessSP(); 1750b57cec5SDimitry Andric if (process_sp) { 1760b57cec5SDimitry Andric ignore_breakpoints = process_sp->GetIgnoreBreakpointsInExpressions(); 1770b57cec5SDimitry Andric unwind_on_error = process_sp->GetUnwindOnErrorInExpressions(); 1780b57cec5SDimitry Andric } else { 1790b57cec5SDimitry Andric ignore_breakpoints = true; 1800b57cec5SDimitry Andric unwind_on_error = true; 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric show_summary = true; 1840b57cec5SDimitry Andric try_all_threads = true; 1850b57cec5SDimitry Andric timeout = 0; 1860b57cec5SDimitry Andric debug = false; 1870b57cec5SDimitry Andric language = eLanguageTypeUnknown; 1880b57cec5SDimitry Andric m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact; 1890b57cec5SDimitry Andric auto_apply_fixits = eLazyBoolCalculate; 1900b57cec5SDimitry Andric top_level = false; 1910b57cec5SDimitry Andric allow_jit = true; 19206c3fb27SDimitry Andric suppress_persistent_result = eLazyBoolCalculate; 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition> 1960b57cec5SDimitry Andric CommandObjectExpression::CommandOptions::GetDefinitions() { 197bdd1243dSDimitry Andric return llvm::ArrayRef(g_expression_options); 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 20006c3fb27SDimitry Andric EvaluateExpressionOptions 20106c3fb27SDimitry Andric CommandObjectExpression::CommandOptions::GetEvaluateExpressionOptions( 20206c3fb27SDimitry Andric const Target &target, const OptionGroupValueObjectDisplay &display_opts) { 20306c3fb27SDimitry Andric EvaluateExpressionOptions options; 20406c3fb27SDimitry Andric options.SetCoerceToId(display_opts.use_objc); 20506c3fb27SDimitry Andric options.SetUnwindOnError(unwind_on_error); 20606c3fb27SDimitry Andric options.SetIgnoreBreakpoints(ignore_breakpoints); 20706c3fb27SDimitry Andric options.SetKeepInMemory(true); 20806c3fb27SDimitry Andric options.SetUseDynamic(display_opts.use_dynamic); 20906c3fb27SDimitry Andric options.SetTryAllThreads(try_all_threads); 21006c3fb27SDimitry Andric options.SetDebug(debug); 21106c3fb27SDimitry Andric options.SetLanguage(language); 21206c3fb27SDimitry Andric options.SetExecutionPolicy( 21306c3fb27SDimitry Andric allow_jit ? EvaluateExpressionOptions::default_execution_policy 21406c3fb27SDimitry Andric : lldb_private::eExecutionPolicyNever); 21506c3fb27SDimitry Andric 21606c3fb27SDimitry Andric bool auto_apply_fixits; 21706c3fb27SDimitry Andric if (this->auto_apply_fixits == eLazyBoolCalculate) 21806c3fb27SDimitry Andric auto_apply_fixits = target.GetEnableAutoApplyFixIts(); 21906c3fb27SDimitry Andric else 22006c3fb27SDimitry Andric auto_apply_fixits = this->auto_apply_fixits == eLazyBoolYes; 22106c3fb27SDimitry Andric 22206c3fb27SDimitry Andric options.SetAutoApplyFixIts(auto_apply_fixits); 22306c3fb27SDimitry Andric options.SetRetriesWithFixIts(target.GetNumberOfRetriesWithFixits()); 22406c3fb27SDimitry Andric 22506c3fb27SDimitry Andric if (top_level) 22606c3fb27SDimitry Andric options.SetExecutionPolicy(eExecutionPolicyTopLevel); 22706c3fb27SDimitry Andric 22806c3fb27SDimitry Andric // If there is any chance we are going to stop and want to see what went 22906c3fb27SDimitry Andric // wrong with our expression, we should generate debug info 23006c3fb27SDimitry Andric if (!ignore_breakpoints || !unwind_on_error) 23106c3fb27SDimitry Andric options.SetGenerateDebugInfo(true); 23206c3fb27SDimitry Andric 23306c3fb27SDimitry Andric if (timeout > 0) 23406c3fb27SDimitry Andric options.SetTimeout(std::chrono::microseconds(timeout)); 23506c3fb27SDimitry Andric else 23606c3fb27SDimitry Andric options.SetTimeout(std::nullopt); 23706c3fb27SDimitry Andric return options; 23806c3fb27SDimitry Andric } 23906c3fb27SDimitry Andric 24006c3fb27SDimitry Andric bool CommandObjectExpression::CommandOptions::ShouldSuppressResult( 24106c3fb27SDimitry Andric const OptionGroupValueObjectDisplay &display_opts) const { 24206c3fb27SDimitry Andric // Explicitly disabling persistent results takes precedence over the 24306c3fb27SDimitry Andric // m_verbosity/use_objc logic. 24406c3fb27SDimitry Andric if (suppress_persistent_result != eLazyBoolCalculate) 24506c3fb27SDimitry Andric return suppress_persistent_result == eLazyBoolYes; 24606c3fb27SDimitry Andric 24706c3fb27SDimitry Andric return display_opts.use_objc && 24806c3fb27SDimitry Andric m_verbosity == eLanguageRuntimeDescriptionDisplayVerbosityCompact; 24906c3fb27SDimitry Andric } 25006c3fb27SDimitry Andric 2510b57cec5SDimitry Andric CommandObjectExpression::CommandObjectExpression( 2520b57cec5SDimitry Andric CommandInterpreter &interpreter) 253480093f4SDimitry Andric : CommandObjectRaw(interpreter, "expression", 254480093f4SDimitry Andric "Evaluate an expression on the current " 2550b57cec5SDimitry Andric "thread. Displays any returned value " 2560b57cec5SDimitry Andric "with LLDB's default formatting.", 257480093f4SDimitry Andric "", 258480093f4SDimitry Andric eCommandProcessMustBePaused | eCommandTryTargetAPILock), 2590b57cec5SDimitry Andric IOHandlerDelegate(IOHandlerDelegate::Completion::Expression), 26004eeddc0SDimitry Andric m_format_options(eFormatDefault), 2610b57cec5SDimitry Andric m_repl_option(LLDB_OPT_SET_1, false, "repl", 'r', "Drop into REPL", false, 2620b57cec5SDimitry Andric true), 263972a253aSDimitry Andric m_expr_line_count(0) { 2640b57cec5SDimitry Andric SetHelpLong( 2650b57cec5SDimitry Andric R"( 2660b57cec5SDimitry Andric Single and multi-line expressions: 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric )" 2690b57cec5SDimitry Andric " The expression provided on the command line must be a complete expression \ 2700b57cec5SDimitry Andric with no newlines. To evaluate a multi-line expression, \ 2710b57cec5SDimitry Andric hit a return after an empty expression, and lldb will enter the multi-line expression editor. \ 2720b57cec5SDimitry Andric Hit return on an empty line to end the multi-line expression." 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric R"( 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric Timeouts: 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric )" 2790b57cec5SDimitry Andric " If the expression can be evaluated statically (without running code) then it will be. \ 2800b57cec5SDimitry Andric Otherwise, by default the expression will run on the current thread with a short timeout: \ 2810b57cec5SDimitry Andric currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \ 2820b57cec5SDimitry Andric and resumed with all threads running. You can use the -a option to disable retrying on all \ 2830b57cec5SDimitry Andric threads. You can use the -t option to set a shorter timeout." 2840b57cec5SDimitry Andric R"( 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric User defined variables: 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric )" 2890b57cec5SDimitry Andric " You can define your own variables for convenience or to be used in subsequent expressions. \ 2900b57cec5SDimitry Andric You define them the same way you would define variables in C. If the first character of \ 2910b57cec5SDimitry Andric your user defined variable is a $, then the variable's value will be available in future \ 2920b57cec5SDimitry Andric expressions, otherwise it will just be available in the current expression." 2930b57cec5SDimitry Andric R"( 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric Continuing evaluation after a breakpoint: 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric )" 2980b57cec5SDimitry Andric " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \ 2990b57cec5SDimitry Andric you are done with your investigation, you can either remove the expression execution frames \ 3000b57cec5SDimitry Andric from the stack with \"thread return -x\" or if you are still interested in the expression result \ 3010b57cec5SDimitry Andric you can issue the \"continue\" command and the expression evaluation will complete and the \ 3020b57cec5SDimitry Andric expression result will be available using the \"thread.completed-expression\" key in the thread \ 3030b57cec5SDimitry Andric format." 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric R"( 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric Examples: 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric expr my_struct->a = my_array[3] 3100b57cec5SDimitry Andric expr -f bin -- (index * 8) + 5 3110b57cec5SDimitry Andric expr unsigned int $foo = 5 3120b57cec5SDimitry Andric expr char c[] = \"foo\"; c[0])"); 3130b57cec5SDimitry Andric 314*0fca6ea1SDimitry Andric AddSimpleArgumentList(eArgTypeExpression); 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric // Add the "--format" and "--gdb-format" 3170b57cec5SDimitry Andric m_option_group.Append(&m_format_options, 3180b57cec5SDimitry Andric OptionGroupFormat::OPTION_GROUP_FORMAT | 3190b57cec5SDimitry Andric OptionGroupFormat::OPTION_GROUP_GDB_FMT, 3200b57cec5SDimitry Andric LLDB_OPT_SET_1); 3210b57cec5SDimitry Andric m_option_group.Append(&m_command_options); 3220b57cec5SDimitry Andric m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, 3230b57cec5SDimitry Andric LLDB_OPT_SET_1 | LLDB_OPT_SET_2); 3240b57cec5SDimitry Andric m_option_group.Append(&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3); 3250b57cec5SDimitry Andric m_option_group.Finalize(); 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric CommandObjectExpression::~CommandObjectExpression() = default; 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric Options *CommandObjectExpression::GetOptions() { return &m_option_group; } 3310b57cec5SDimitry Andric 3329dba64beSDimitry Andric void CommandObjectExpression::HandleCompletion(CompletionRequest &request) { 3330b57cec5SDimitry Andric EvaluateExpressionOptions options; 3340b57cec5SDimitry Andric options.SetCoerceToId(m_varobj_options.use_objc); 3350b57cec5SDimitry Andric options.SetLanguage(m_command_options.language); 3360b57cec5SDimitry Andric options.SetExecutionPolicy(lldb_private::eExecutionPolicyNever); 3370b57cec5SDimitry Andric options.SetAutoApplyFixIts(false); 3380b57cec5SDimitry Andric options.SetGenerateDebugInfo(false); 3390b57cec5SDimitry Andric 340fe6060f1SDimitry Andric ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 3410b57cec5SDimitry Andric 342fe6060f1SDimitry Andric // Get out before we start doing things that expect a valid frame pointer. 343fe6060f1SDimitry Andric if (exe_ctx.GetFramePtr() == nullptr) 3449dba64beSDimitry Andric return; 3450b57cec5SDimitry Andric 346e8d8bef9SDimitry Andric Target *exe_target = exe_ctx.GetTargetPtr(); 347e8d8bef9SDimitry Andric Target &target = exe_target ? *exe_target : GetDummyTarget(); 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric unsigned cursor_pos = request.GetRawCursorPos(); 3505ffd83dbSDimitry Andric // Get the full user input including the suffix. The suffix is necessary 3515ffd83dbSDimitry Andric // as OptionsWithRaw will use it to detect if the cursor is cursor is in the 3525ffd83dbSDimitry Andric // argument part of in the raw input part of the arguments. If we cut of 3535ffd83dbSDimitry Andric // of the suffix then "expr -arg[cursor] --" would interpret the "-arg" as 3545ffd83dbSDimitry Andric // the raw input (as the "--" is hidden in the suffix). 3555ffd83dbSDimitry Andric llvm::StringRef code = request.GetRawLineWithUnusedSuffix(); 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric const std::size_t original_code_size = code.size(); 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric // Remove the first token which is 'expr' or some alias/abbreviation of that. 3600b57cec5SDimitry Andric code = llvm::getToken(code).second.ltrim(); 3610b57cec5SDimitry Andric OptionsWithRaw args(code); 3620b57cec5SDimitry Andric code = args.GetRawPart(); 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric // The position where the expression starts in the command line. 3650b57cec5SDimitry Andric assert(original_code_size >= code.size()); 3660b57cec5SDimitry Andric std::size_t raw_start = original_code_size - code.size(); 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric // Check if the cursor is actually in the expression string, and if not, we 3690b57cec5SDimitry Andric // exit. 3700b57cec5SDimitry Andric // FIXME: We should complete the options here. 3710b57cec5SDimitry Andric if (cursor_pos < raw_start) 3729dba64beSDimitry Andric return; 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric // Make the cursor_pos again relative to the start of the code string. 3750b57cec5SDimitry Andric assert(cursor_pos >= raw_start); 3760b57cec5SDimitry Andric cursor_pos -= raw_start; 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric auto language = exe_ctx.GetFrameRef().GetLanguage(); 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric Status error; 381e8d8bef9SDimitry Andric lldb::UserExpressionSP expr(target.GetUserExpressionForLanguage( 3820b57cec5SDimitry Andric code, llvm::StringRef(), language, UserExpression::eResultTypeAny, 3830b57cec5SDimitry Andric options, nullptr, error)); 3840b57cec5SDimitry Andric if (error.Fail()) 3859dba64beSDimitry Andric return; 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric expr->Complete(exe_ctx, request, cursor_pos); 3880b57cec5SDimitry Andric } 3890b57cec5SDimitry Andric 3900b57cec5SDimitry Andric static lldb_private::Status 3910b57cec5SDimitry Andric CanBeUsedForElementCountPrinting(ValueObject &valobj) { 3920b57cec5SDimitry Andric CompilerType type(valobj.GetCompilerType()); 3930b57cec5SDimitry Andric CompilerType pointee; 3940b57cec5SDimitry Andric if (!type.IsPointerType(&pointee)) 3950b57cec5SDimitry Andric return Status("as it does not refer to a pointer"); 3960b57cec5SDimitry Andric if (pointee.IsVoidType()) 3970b57cec5SDimitry Andric return Status("as it refers to a pointer to void"); 3980b57cec5SDimitry Andric return Status(); 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric 4015ffd83dbSDimitry Andric bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr, 4025ffd83dbSDimitry Andric Stream &output_stream, 4035ffd83dbSDimitry Andric Stream &error_stream, 4045ffd83dbSDimitry Andric CommandReturnObject &result) { 4055ffd83dbSDimitry Andric // Don't use m_exe_ctx as this might be called asynchronously after the 4065ffd83dbSDimitry Andric // command object DoExecute has finished when doing multi-line expression 4075ffd83dbSDimitry Andric // that use an input reader... 4085ffd83dbSDimitry Andric ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 409e8d8bef9SDimitry Andric Target *exe_target = exe_ctx.GetTargetPtr(); 410e8d8bef9SDimitry Andric Target &target = exe_target ? *exe_target : GetDummyTarget(); 4115ffd83dbSDimitry Andric 4125ffd83dbSDimitry Andric lldb::ValueObjectSP result_valobj_sp; 4135ffd83dbSDimitry Andric StackFrame *frame = exe_ctx.GetFramePtr(); 4145ffd83dbSDimitry Andric 415fe6060f1SDimitry Andric if (m_command_options.top_level && !m_command_options.allow_jit) { 416fe6060f1SDimitry Andric result.AppendErrorWithFormat( 417fe6060f1SDimitry Andric "Can't disable JIT compilation for top-level expressions.\n"); 418fe6060f1SDimitry Andric return false; 419fe6060f1SDimitry Andric } 420fe6060f1SDimitry Andric 42106c3fb27SDimitry Andric EvaluateExpressionOptions eval_options = 42206c3fb27SDimitry Andric m_command_options.GetEvaluateExpressionOptions(target, m_varobj_options); 42306c3fb27SDimitry Andric // This command manually removes the result variable, make sure expression 42406c3fb27SDimitry Andric // evaluation doesn't do it first. 42506c3fb27SDimitry Andric eval_options.SetSuppressPersistentResult(false); 42606c3fb27SDimitry Andric 427e8d8bef9SDimitry Andric ExpressionResults success = target.EvaluateExpression( 42806c3fb27SDimitry Andric expr, frame, result_valobj_sp, eval_options, &m_fixed_expression); 4290b57cec5SDimitry Andric 4305f757f3fSDimitry Andric // Only mention Fix-Its if the expression evaluator applied them. 4315f757f3fSDimitry Andric // Compiler errors refer to the final expression after applying Fix-It(s). 432e8d8bef9SDimitry Andric if (!m_fixed_expression.empty() && target.GetEnableNotifyAboutFixIts()) { 4335f757f3fSDimitry Andric error_stream << " Evaluated this expression after applying Fix-It(s):\n"; 4345f757f3fSDimitry Andric error_stream << " " << m_fixed_expression << "\n"; 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric if (result_valobj_sp) { 4380b57cec5SDimitry Andric Format format = m_format_options.GetFormat(); 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric if (result_valobj_sp->GetError().Success()) { 4410b57cec5SDimitry Andric if (format != eFormatVoid) { 4420b57cec5SDimitry Andric if (format != eFormatDefault) 4430b57cec5SDimitry Andric result_valobj_sp->SetFormat(format); 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric if (m_varobj_options.elem_count > 0) { 4460b57cec5SDimitry Andric Status error(CanBeUsedForElementCountPrinting(*result_valobj_sp)); 4470b57cec5SDimitry Andric if (error.Fail()) { 4485ffd83dbSDimitry Andric result.AppendErrorWithFormat( 4490b57cec5SDimitry Andric "expression cannot be used with --element-count %s\n", 4500b57cec5SDimitry Andric error.AsCString("")); 4510b57cec5SDimitry Andric return false; 4520b57cec5SDimitry Andric } 4530b57cec5SDimitry Andric } 4540b57cec5SDimitry Andric 45506c3fb27SDimitry Andric bool suppress_result = 45606c3fb27SDimitry Andric m_command_options.ShouldSuppressResult(m_varobj_options); 45706c3fb27SDimitry Andric 4580b57cec5SDimitry Andric DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions( 4590b57cec5SDimitry Andric m_command_options.m_verbosity, format)); 46006c3fb27SDimitry Andric options.SetHideRootName(suppress_result); 4610b57cec5SDimitry Andric options.SetVariableFormatDisplayLanguage( 4620b57cec5SDimitry Andric result_valobj_sp->GetPreferredDisplayLanguage()); 4630b57cec5SDimitry Andric 464*0fca6ea1SDimitry Andric if (llvm::Error error = 465*0fca6ea1SDimitry Andric result_valobj_sp->Dump(output_stream, options)) { 466*0fca6ea1SDimitry Andric result.AppendError(toString(std::move(error))); 467*0fca6ea1SDimitry Andric return false; 468*0fca6ea1SDimitry Andric } 4690b57cec5SDimitry Andric 47006c3fb27SDimitry Andric if (suppress_result) 47106c3fb27SDimitry Andric if (auto result_var_sp = 47206c3fb27SDimitry Andric target.GetPersistentVariable(result_valobj_sp->GetName())) { 47306c3fb27SDimitry Andric auto language = result_valobj_sp->GetPreferredDisplayLanguage(); 47406c3fb27SDimitry Andric if (auto *persistent_state = 47506c3fb27SDimitry Andric target.GetPersistentExpressionStateForLanguage(language)) 47606c3fb27SDimitry Andric persistent_state->RemovePersistentVariable(result_var_sp); 47706c3fb27SDimitry Andric } 4785ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric } else { 4810b57cec5SDimitry Andric if (result_valobj_sp->GetError().GetError() == 4820b57cec5SDimitry Andric UserExpression::kNoResult) { 4830b57cec5SDimitry Andric if (format != eFormatVoid && GetDebugger().GetNotifyVoid()) { 4845ffd83dbSDimitry Andric error_stream.PutCString("(void)\n"); 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric 4875ffd83dbSDimitry Andric result.SetStatus(eReturnStatusSuccessFinishResult); 4880b57cec5SDimitry Andric } else { 4890b57cec5SDimitry Andric const char *error_cstr = result_valobj_sp->GetError().AsCString(); 4900b57cec5SDimitry Andric if (error_cstr && error_cstr[0]) { 4910b57cec5SDimitry Andric const size_t error_cstr_len = strlen(error_cstr); 4929dba64beSDimitry Andric const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n'; 4930b57cec5SDimitry Andric if (strstr(error_cstr, "error:") != error_cstr) 4945ffd83dbSDimitry Andric error_stream.PutCString("error: "); 4955ffd83dbSDimitry Andric error_stream.Write(error_cstr, error_cstr_len); 4960b57cec5SDimitry Andric if (!ends_with_newline) 4975ffd83dbSDimitry Andric error_stream.EOL(); 4980b57cec5SDimitry Andric } else { 4995ffd83dbSDimitry Andric error_stream.PutCString("error: unknown error\n"); 5000b57cec5SDimitry Andric } 5010b57cec5SDimitry Andric 5025ffd83dbSDimitry Andric result.SetStatus(eReturnStatusFailed); 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric } 505bdd1243dSDimitry Andric } else { 506bdd1243dSDimitry Andric error_stream.Printf("error: unknown error\n"); 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric 5095ffd83dbSDimitry Andric return (success != eExpressionSetupError && 5105ffd83dbSDimitry Andric success != eExpressionParseError); 5110b57cec5SDimitry Andric } 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler, 5140b57cec5SDimitry Andric std::string &line) { 5150b57cec5SDimitry Andric io_handler.SetIsDone(true); 5169dba64beSDimitry Andric StreamFileSP output_sp = io_handler.GetOutputStreamFileSP(); 5179dba64beSDimitry Andric StreamFileSP error_sp = io_handler.GetErrorStreamFileSP(); 5180b57cec5SDimitry Andric 5195ffd83dbSDimitry Andric CommandReturnObject return_obj( 5205ffd83dbSDimitry Andric GetCommandInterpreter().GetDebugger().GetUseColor()); 5215ffd83dbSDimitry Andric EvaluateExpression(line.c_str(), *output_sp, *error_sp, return_obj); 5220b57cec5SDimitry Andric if (output_sp) 5230b57cec5SDimitry Andric output_sp->Flush(); 5240b57cec5SDimitry Andric if (error_sp) 5250b57cec5SDimitry Andric error_sp->Flush(); 5260b57cec5SDimitry Andric } 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler, 5290b57cec5SDimitry Andric StringList &lines) { 5300b57cec5SDimitry Andric // An empty lines is used to indicate the end of input 5310b57cec5SDimitry Andric const size_t num_lines = lines.GetSize(); 5320b57cec5SDimitry Andric if (num_lines > 0 && lines[num_lines - 1].empty()) { 5330b57cec5SDimitry Andric // Remove the last empty line from "lines" so it doesn't appear in our 5340b57cec5SDimitry Andric // resulting input and return true to indicate we are done getting lines 5350b57cec5SDimitry Andric lines.PopBack(); 5360b57cec5SDimitry Andric return true; 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric return false; 5390b57cec5SDimitry Andric } 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric void CommandObjectExpression::GetMultilineExpression() { 5420b57cec5SDimitry Andric m_expr_lines.clear(); 5430b57cec5SDimitry Andric m_expr_line_count = 0; 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric Debugger &debugger = GetCommandInterpreter().GetDebugger(); 5460b57cec5SDimitry Andric bool color_prompt = debugger.GetUseColor(); 5470b57cec5SDimitry Andric const bool multiple_lines = true; // Get multiple lines 5480b57cec5SDimitry Andric IOHandlerSP io_handler_sp( 5490b57cec5SDimitry Andric new IOHandlerEditline(debugger, IOHandler::Type::Expression, 5500b57cec5SDimitry Andric "lldb-expr", // Name of input reader for history 5510b57cec5SDimitry Andric llvm::StringRef(), // No prompt 5520b57cec5SDimitry Andric llvm::StringRef(), // Continuation prompt 5530b57cec5SDimitry Andric multiple_lines, color_prompt, 5540b57cec5SDimitry Andric 1, // Show line numbers starting at 1 555bdd1243dSDimitry Andric *this)); 5560b57cec5SDimitry Andric 5579dba64beSDimitry Andric StreamFileSP output_sp = io_handler_sp->GetOutputStreamFileSP(); 5580b57cec5SDimitry Andric if (output_sp) { 5590b57cec5SDimitry Andric output_sp->PutCString( 5600b57cec5SDimitry Andric "Enter expressions, then terminate with an empty line to evaluate:\n"); 5610b57cec5SDimitry Andric output_sp->Flush(); 5620b57cec5SDimitry Andric } 5635ffd83dbSDimitry Andric debugger.RunIOHandlerAsync(io_handler_sp); 5640b57cec5SDimitry Andric } 5650b57cec5SDimitry Andric 5660b57cec5SDimitry Andric static EvaluateExpressionOptions 5670b57cec5SDimitry Andric GetExprOptions(ExecutionContext &ctx, 5680b57cec5SDimitry Andric CommandObjectExpression::CommandOptions command_options) { 5690b57cec5SDimitry Andric command_options.OptionParsingStarting(&ctx); 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric // Default certain settings for REPL regardless of the global settings. 5720b57cec5SDimitry Andric command_options.unwind_on_error = false; 5730b57cec5SDimitry Andric command_options.ignore_breakpoints = false; 5740b57cec5SDimitry Andric command_options.debug = false; 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric EvaluateExpressionOptions expr_options; 5770b57cec5SDimitry Andric expr_options.SetUnwindOnError(command_options.unwind_on_error); 5780b57cec5SDimitry Andric expr_options.SetIgnoreBreakpoints(command_options.ignore_breakpoints); 5790b57cec5SDimitry Andric expr_options.SetTryAllThreads(command_options.try_all_threads); 5800b57cec5SDimitry Andric 5810b57cec5SDimitry Andric if (command_options.timeout > 0) 5820b57cec5SDimitry Andric expr_options.SetTimeout(std::chrono::microseconds(command_options.timeout)); 5830b57cec5SDimitry Andric else 584bdd1243dSDimitry Andric expr_options.SetTimeout(std::nullopt); 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andric return expr_options; 5870b57cec5SDimitry Andric } 5880b57cec5SDimitry Andric 5895f757f3fSDimitry Andric void CommandObjectExpression::DoExecute(llvm::StringRef command, 5900b57cec5SDimitry Andric CommandReturnObject &result) { 5910b57cec5SDimitry Andric m_fixed_expression.clear(); 5920b57cec5SDimitry Andric auto exe_ctx = GetCommandInterpreter().GetExecutionContext(); 5930b57cec5SDimitry Andric m_option_group.NotifyOptionParsingStarting(&exe_ctx); 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric if (command.empty()) { 5960b57cec5SDimitry Andric GetMultilineExpression(); 5975f757f3fSDimitry Andric return; 5980b57cec5SDimitry Andric } 5990b57cec5SDimitry Andric 6000b57cec5SDimitry Andric OptionsWithRaw args(command); 6010b57cec5SDimitry Andric llvm::StringRef expr = args.GetRawPart(); 6020b57cec5SDimitry Andric 6030b57cec5SDimitry Andric if (args.HasArgs()) { 6040b57cec5SDimitry Andric if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group, exe_ctx)) 6055f757f3fSDimitry Andric return; 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric if (m_repl_option.GetOptionValue().GetCurrentValue()) { 608480093f4SDimitry Andric Target &target = GetSelectedOrDummyTarget(); 6090b57cec5SDimitry Andric // Drop into REPL 6100b57cec5SDimitry Andric m_expr_lines.clear(); 6110b57cec5SDimitry Andric m_expr_line_count = 0; 6120b57cec5SDimitry Andric 613480093f4SDimitry Andric Debugger &debugger = target.GetDebugger(); 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andric // Check if the LLDB command interpreter is sitting on top of a REPL 6160b57cec5SDimitry Andric // that launched it... 6170b57cec5SDimitry Andric if (debugger.CheckTopIOHandlerTypes(IOHandler::Type::CommandInterpreter, 6180b57cec5SDimitry Andric IOHandler::Type::REPL)) { 6190b57cec5SDimitry Andric // the LLDB command interpreter is sitting on top of a REPL that 6200b57cec5SDimitry Andric // launched it, so just say the command interpreter is done and 6210b57cec5SDimitry Andric // fall back to the existing REPL 6220b57cec5SDimitry Andric m_interpreter.GetIOHandler(false)->SetIsDone(true); 6230b57cec5SDimitry Andric } else { 6240b57cec5SDimitry Andric // We are launching the REPL on top of the current LLDB command 6250b57cec5SDimitry Andric // interpreter, so just push one 6260b57cec5SDimitry Andric bool initialize = false; 6270b57cec5SDimitry Andric Status repl_error; 628480093f4SDimitry Andric REPLSP repl_sp(target.GetREPL(repl_error, m_command_options.language, 6290b57cec5SDimitry Andric nullptr, false)); 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric if (!repl_sp) { 6320b57cec5SDimitry Andric initialize = true; 633480093f4SDimitry Andric repl_sp = target.GetREPL(repl_error, m_command_options.language, 6340b57cec5SDimitry Andric nullptr, true); 6350b57cec5SDimitry Andric if (!repl_error.Success()) { 6360b57cec5SDimitry Andric result.SetError(repl_error); 6375f757f3fSDimitry Andric return; 6380b57cec5SDimitry Andric } 6390b57cec5SDimitry Andric } 6400b57cec5SDimitry Andric 6410b57cec5SDimitry Andric if (repl_sp) { 6420b57cec5SDimitry Andric if (initialize) { 6430b57cec5SDimitry Andric repl_sp->SetEvaluateOptions( 6440b57cec5SDimitry Andric GetExprOptions(exe_ctx, m_command_options)); 6450b57cec5SDimitry Andric repl_sp->SetFormatOptions(m_format_options); 6460b57cec5SDimitry Andric repl_sp->SetValueObjectDisplayOptions(m_varobj_options); 6470b57cec5SDimitry Andric } 6480b57cec5SDimitry Andric 6490b57cec5SDimitry Andric IOHandlerSP io_handler_sp(repl_sp->GetIOHandler()); 6500b57cec5SDimitry Andric io_handler_sp->SetIsDone(false); 6515ffd83dbSDimitry Andric debugger.RunIOHandlerAsync(io_handler_sp); 6520b57cec5SDimitry Andric } else { 6530b57cec5SDimitry Andric repl_error.SetErrorStringWithFormat( 6540b57cec5SDimitry Andric "Couldn't create a REPL for %s", 6550b57cec5SDimitry Andric Language::GetNameForLanguageType(m_command_options.language)); 6560b57cec5SDimitry Andric result.SetError(repl_error); 6575f757f3fSDimitry Andric return; 6580b57cec5SDimitry Andric } 6590b57cec5SDimitry Andric } 6600b57cec5SDimitry Andric } 6610b57cec5SDimitry Andric // No expression following options 6620b57cec5SDimitry Andric else if (expr.empty()) { 6630b57cec5SDimitry Andric GetMultilineExpression(); 6645f757f3fSDimitry Andric return; 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric } 6670b57cec5SDimitry Andric 6689dba64beSDimitry Andric Target &target = GetSelectedOrDummyTarget(); 6695ffd83dbSDimitry Andric if (EvaluateExpression(expr, result.GetOutputStream(), 6705ffd83dbSDimitry Andric result.GetErrorStream(), result)) { 6710b57cec5SDimitry Andric 6729dba64beSDimitry Andric if (!m_fixed_expression.empty() && target.GetEnableNotifyAboutFixIts()) { 6730b57cec5SDimitry Andric CommandHistory &history = m_interpreter.GetCommandHistory(); 6740b57cec5SDimitry Andric // FIXME: Can we figure out what the user actually typed (e.g. some alias 6750b57cec5SDimitry Andric // for expr???) 6760b57cec5SDimitry Andric // If we can it would be nice to show that. 6770b57cec5SDimitry Andric std::string fixed_command("expression "); 6780b57cec5SDimitry Andric if (args.HasArgs()) { 6790b57cec5SDimitry Andric // Add in any options that might have been in the original command: 6805ffd83dbSDimitry Andric fixed_command.append(std::string(args.GetArgStringWithDelimiter())); 6810b57cec5SDimitry Andric fixed_command.append(m_fixed_expression); 6820b57cec5SDimitry Andric } else 6830b57cec5SDimitry Andric fixed_command.append(m_fixed_expression); 6840b57cec5SDimitry Andric history.AppendString(fixed_command); 6850b57cec5SDimitry Andric } 6865f757f3fSDimitry Andric return; 6870b57cec5SDimitry Andric } 6880b57cec5SDimitry Andric result.SetStatus(eReturnStatusFailed); 6890b57cec5SDimitry Andric } 690