180814287SRaphael Isemann //===-- CommandObjectExpression.cpp ---------------------------------------===// 230fdc8d8SChris Lattner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 630fdc8d8SChris Lattner // 730fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 830fdc8d8SChris Lattner 9c8ecc2a9SEugene Zelenko #include "llvm/ADT/StringRef.h" 10c8ecc2a9SEugene Zelenko 11c8ecc2a9SEugene Zelenko #include "CommandObjectExpression.h" 12b9c1b51eSKate Stone #include "lldb/Core/Debugger.h" 13f2bd5c3eSSean Callanan #include "lldb/Expression/REPL.h" 14b9c1b51eSKate Stone #include "lldb/Expression/UserExpression.h" 153eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h" 166611103cSGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h" 1730fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 1847cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h" 19b9c1b51eSKate Stone #include "lldb/Target/Language.h" 2030fdc8d8SChris Lattner #include "lldb/Target/Process.h" 21b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h" 2230fdc8d8SChris Lattner #include "lldb/Target/Target.h" 2330fdc8d8SChris Lattner 2430fdc8d8SChris Lattner using namespace lldb; 2530fdc8d8SChris Lattner using namespace lldb_private; 2630fdc8d8SChris Lattner 27b9c1b51eSKate Stone CommandObjectExpression::CommandOptions::CommandOptions() : OptionGroup() {} 2830fdc8d8SChris Lattner 29c8ecc2a9SEugene Zelenko CommandObjectExpression::CommandOptions::~CommandOptions() = default; 3030fdc8d8SChris Lattner 318fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_description_verbosity_type[] = { 32e063ecccSJonas Devlieghere { 33e063ecccSJonas Devlieghere eLanguageRuntimeDescriptionDisplayVerbosityCompact, 34e063ecccSJonas Devlieghere "compact", 35e063ecccSJonas Devlieghere "Only show the description string", 36e063ecccSJonas Devlieghere }, 37e063ecccSJonas Devlieghere { 38e063ecccSJonas Devlieghere eLanguageRuntimeDescriptionDisplayVerbosityFull, 39e063ecccSJonas Devlieghere "full", 40e063ecccSJonas Devlieghere "Show the full output, including persistent variable's name and type", 41e063ecccSJonas Devlieghere }, 42e063ecccSJonas Devlieghere }; 434d93b8cdSEnrico Granata 448fe53c49STatyana Krasnukha static constexpr OptionEnumValues DescriptionVerbosityTypes() { 458fe53c49STatyana Krasnukha return OptionEnumValues(g_description_verbosity_type); 468fe53c49STatyana Krasnukha } 478fe53c49STatyana Krasnukha 48ec67e734SRaphael Isemann #define LLDB_OPTIONS_expression 49ec67e734SRaphael Isemann #include "CommandOptions.inc" 501deb7962SGreg Clayton 5197206d57SZachary Turner Status CommandObjectExpression::CommandOptions::SetOptionValue( 528cef4b0bSZachary Turner uint32_t option_idx, llvm::StringRef option_arg, 53b9c1b51eSKate Stone ExecutionContext *execution_context) { 5497206d57SZachary Turner Status error; 5530fdc8d8SChris Lattner 561f0f5b5bSZachary Turner const int short_option = GetDefinitions()[option_idx].short_option; 5730fdc8d8SChris Lattner 58b9c1b51eSKate Stone switch (short_option) { 5915663c53SDawn Perchik case 'l': 600e0984eeSJim Ingham language = Language::GetLanguageTypeFromString(option_arg); 6115663c53SDawn Perchik if (language == eLanguageTypeUnknown) 62b9c1b51eSKate Stone error.SetErrorStringWithFormat( 638cef4b0bSZachary Turner "unknown language type: '%s' for expression", 648cef4b0bSZachary Turner option_arg.str().c_str()); 6515663c53SDawn Perchik break; 6630fdc8d8SChris Lattner 67b9c1b51eSKate Stone case 'a': { 6835e1bda6SJim Ingham bool success; 6935e1bda6SJim Ingham bool result; 7047cbf4a0SPavel Labath result = OptionArgParser::ToBoolean(option_arg, true, &success); 7135e1bda6SJim Ingham if (!success) 72b9c1b51eSKate Stone error.SetErrorStringWithFormat( 738cef4b0bSZachary Turner "invalid all-threads value setting: \"%s\"", 748cef4b0bSZachary Turner option_arg.str().c_str()); 7535e1bda6SJim Ingham else 7635e1bda6SJim Ingham try_all_threads = result; 77b9c1b51eSKate Stone } break; 786c68fb45SJim Ingham 79b9c1b51eSKate Stone case 'i': { 80184e9811SJim Ingham bool success; 8147cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 82184e9811SJim Ingham if (success) 83184e9811SJim Ingham ignore_breakpoints = tmp_value; 84184e9811SJim Ingham else 85b9c1b51eSKate Stone error.SetErrorStringWithFormat( 868cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 878cef4b0bSZachary Turner option_arg.str().c_str()); 88184e9811SJim Ingham break; 89184e9811SJim Ingham } 903fe71581SMarianne Mailhot-Sarrasin 91b9c1b51eSKate Stone case 'j': { 923fe71581SMarianne Mailhot-Sarrasin bool success; 9347cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 943fe71581SMarianne Mailhot-Sarrasin if (success) 953fe71581SMarianne Mailhot-Sarrasin allow_jit = tmp_value; 963fe71581SMarianne Mailhot-Sarrasin else 97b9c1b51eSKate Stone error.SetErrorStringWithFormat( 988cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 998cef4b0bSZachary Turner option_arg.str().c_str()); 1003fe71581SMarianne Mailhot-Sarrasin break; 1013fe71581SMarianne Mailhot-Sarrasin } 1023fe71581SMarianne Mailhot-Sarrasin 1038cef4b0bSZachary Turner case 't': 1048cef4b0bSZachary Turner if (option_arg.getAsInteger(0, timeout)) { 1058cef4b0bSZachary Turner timeout = 0; 106b9c1b51eSKate Stone error.SetErrorStringWithFormat("invalid timeout setting \"%s\"", 1078cef4b0bSZachary Turner option_arg.str().c_str()); 1088cef4b0bSZachary Turner } 1098cef4b0bSZachary Turner break; 11035e1bda6SJim Ingham 111b9c1b51eSKate Stone case 'u': { 112399f1cafSJim Ingham bool success; 11347cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 114184e9811SJim Ingham if (success) 115184e9811SJim Ingham unwind_on_error = tmp_value; 116184e9811SJim Ingham else 117b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1188cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 1198cef4b0bSZachary Turner option_arg.str().c_str()); 120399f1cafSJim Ingham break; 1213bfdaa2aSSean Callanan } 1224d93b8cdSEnrico Granata 1234d93b8cdSEnrico Granata case 'v': 124543a26e9SEnrico Granata if (option_arg.empty()) { 1254d93b8cdSEnrico Granata m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull; 1264d93b8cdSEnrico Granata break; 1274d93b8cdSEnrico Granata } 12847cbf4a0SPavel Labath m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) 12947cbf4a0SPavel Labath OptionArgParser::ToOptionEnum( 1301f0f5b5bSZachary Turner option_arg, GetDefinitions()[option_idx].enum_values, 0, error); 1314d93b8cdSEnrico Granata if (!error.Success()) 132b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1338cef4b0bSZachary Turner "unrecognized value for description-verbosity '%s'", 1348cef4b0bSZachary Turner option_arg.str().c_str()); 1354d93b8cdSEnrico Granata break; 1364d93b8cdSEnrico Granata 13762afb9f6SGreg Clayton case 'g': 13862afb9f6SGreg Clayton debug = true; 13962afb9f6SGreg Clayton unwind_on_error = false; 14062afb9f6SGreg Clayton ignore_breakpoints = false; 14162afb9f6SGreg Clayton break; 14262afb9f6SGreg Clayton 143863fab69SSean Callanan case 'p': 144863fab69SSean Callanan top_level = true; 145863fab69SSean Callanan break; 146863fab69SSean Callanan 147b9c1b51eSKate Stone case 'X': { 148a1e541bfSJim Ingham bool success; 14947cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 150a1e541bfSJim Ingham if (success) 151a1e541bfSJim Ingham auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo; 152a1e541bfSJim Ingham else 153b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1548cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 1558cef4b0bSZachary Turner option_arg.str().c_str()); 156a1e541bfSJim Ingham break; 157a1e541bfSJim Ingham } 158a1e541bfSJim Ingham 15930fdc8d8SChris Lattner default: 16036162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 16130fdc8d8SChris Lattner } 16230fdc8d8SChris Lattner 16330fdc8d8SChris Lattner return error; 16430fdc8d8SChris Lattner } 16530fdc8d8SChris Lattner 166b9c1b51eSKate Stone void CommandObjectExpression::CommandOptions::OptionParsingStarting( 167b9c1b51eSKate Stone ExecutionContext *execution_context) { 168e1cfbc79STodd Fiala auto process_sp = 169e1cfbc79STodd Fiala execution_context ? execution_context->GetProcessSP() : ProcessSP(); 170b9c1b51eSKate Stone if (process_sp) { 171e1cfbc79STodd Fiala ignore_breakpoints = process_sp->GetIgnoreBreakpointsInExpressions(); 172e1cfbc79STodd Fiala unwind_on_error = process_sp->GetUnwindOnErrorInExpressions(); 173b9c1b51eSKate Stone } else { 174fc03f8fcSGreg Clayton ignore_breakpoints = true; 175399f1cafSJim Ingham unwind_on_error = true; 176184e9811SJim Ingham } 177184e9811SJim Ingham 17830fdc8d8SChris Lattner show_summary = true; 17935e1bda6SJim Ingham try_all_threads = true; 18035e1bda6SJim Ingham timeout = 0; 18162afb9f6SGreg Clayton debug = false; 18215663c53SDawn Perchik language = eLanguageTypeUnknown; 1834d93b8cdSEnrico Granata m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact; 184a1e541bfSJim Ingham auto_apply_fixits = eLazyBoolCalculate; 185863fab69SSean Callanan top_level = false; 1863fe71581SMarianne Mailhot-Sarrasin allow_jit = true; 18730fdc8d8SChris Lattner } 18830fdc8d8SChris Lattner 1891f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> 190b9c1b51eSKate Stone CommandObjectExpression::CommandOptions::GetDefinitions() { 19170602439SZachary Turner return llvm::makeArrayRef(g_expression_options); 19230fdc8d8SChris Lattner } 19330fdc8d8SChris Lattner 194b9c1b51eSKate Stone CommandObjectExpression::CommandObjectExpression( 195b9c1b51eSKate Stone CommandInterpreter &interpreter) 196a925974bSAdrian Prantl : CommandObjectRaw(interpreter, "expression", 197a925974bSAdrian Prantl "Evaluate an expression on the current " 198b9c1b51eSKate Stone "thread. Displays any returned value " 199b9c1b51eSKate Stone "with LLDB's default formatting.", 200a925974bSAdrian Prantl "", 201a925974bSAdrian Prantl eCommandProcessMustBePaused | eCommandTryTargetAPILock), 20244d93782SGreg Clayton IOHandlerDelegate(IOHandlerDelegate::Completion::Expression), 203b9c1b51eSKate Stone m_option_group(), m_format_options(eFormatDefault), 204b9c1b51eSKate Stone m_repl_option(LLDB_OPT_SET_1, false, "repl", 'r', "Drop into REPL", false, 205b9c1b51eSKate Stone true), 206b9c1b51eSKate Stone m_command_options(), m_expr_line_count(0), m_expr_lines() { 20730fdc8d8SChris Lattner SetHelpLong( 208ea671fbdSKate Stone R"( 209d0309916SJim Ingham Single and multi-line expressions: 210d0309916SJim Ingham 211d0309916SJim Ingham )" 212d0309916SJim Ingham " The expression provided on the command line must be a complete expression \ 213d0309916SJim Ingham with no newlines. To evaluate a multi-line expression, \ 214d0309916SJim Ingham hit a return after an empty expression, and lldb will enter the multi-line expression editor. \ 215d0309916SJim Ingham Hit return on an empty line to end the multi-line expression." 216d0309916SJim Ingham 217d0309916SJim Ingham R"( 218d0309916SJim Ingham 219ea671fbdSKate Stone Timeouts: 220ea671fbdSKate Stone 221b9c1b51eSKate Stone )" 222b9c1b51eSKate Stone " If the expression can be evaluated statically (without running code) then it will be. \ 223ea671fbdSKate Stone Otherwise, by default the expression will run on the current thread with a short timeout: \ 224ea671fbdSKate Stone currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \ 225ea671fbdSKate Stone and resumed with all threads running. You can use the -a option to disable retrying on all \ 226b9c1b51eSKate Stone threads. You can use the -t option to set a shorter timeout." 227b9c1b51eSKate Stone R"( 228ea671fbdSKate Stone 229ea671fbdSKate Stone User defined variables: 230ea671fbdSKate Stone 231b9c1b51eSKate Stone )" 232b9c1b51eSKate Stone " You can define your own variables for convenience or to be used in subsequent expressions. \ 233ea671fbdSKate Stone You define them the same way you would define variables in C. If the first character of \ 234ea671fbdSKate Stone your user defined variable is a $, then the variable's value will be available in future \ 235b9c1b51eSKate Stone expressions, otherwise it will just be available in the current expression." 236b9c1b51eSKate Stone R"( 237ea671fbdSKate Stone 238ea671fbdSKate Stone Continuing evaluation after a breakpoint: 239ea671fbdSKate Stone 240b9c1b51eSKate Stone )" 241b9c1b51eSKate Stone " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \ 242ea671fbdSKate Stone you are done with your investigation, you can either remove the expression execution frames \ 243ea671fbdSKate Stone from the stack with \"thread return -x\" or if you are still interested in the expression result \ 244ea671fbdSKate Stone you can issue the \"continue\" command and the expression evaluation will complete and the \ 245ea671fbdSKate Stone expression result will be available using the \"thread.completed-expression\" key in the thread \ 246b9c1b51eSKate Stone format." 247d0309916SJim Ingham 248b9c1b51eSKate Stone R"( 249ea671fbdSKate Stone 250ea671fbdSKate Stone Examples: 251ea671fbdSKate Stone 252ea671fbdSKate Stone expr my_struct->a = my_array[3] 253ea671fbdSKate Stone expr -f bin -- (index * 8) + 5 254ea671fbdSKate Stone expr unsigned int $foo = 5 255b9c1b51eSKate Stone expr char c[] = \"foo\"; c[0])"); 256405fe67fSCaroline Tice 257405fe67fSCaroline Tice CommandArgumentEntry arg; 258405fe67fSCaroline Tice CommandArgumentData expression_arg; 259405fe67fSCaroline Tice 260405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 261405fe67fSCaroline Tice expression_arg.arg_type = eArgTypeExpression; 262405fe67fSCaroline Tice expression_arg.arg_repetition = eArgRepeatPlain; 263405fe67fSCaroline Tice 264b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the argument 265b9c1b51eSKate Stone // entry. 266405fe67fSCaroline Tice arg.push_back(expression_arg); 267405fe67fSCaroline Tice 268405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 269405fe67fSCaroline Tice m_arguments.push_back(arg); 2701deb7962SGreg Clayton 2715009f9d5SGreg Clayton // Add the "--format" and "--gdb-format" 272b9c1b51eSKate Stone m_option_group.Append(&m_format_options, 273b9c1b51eSKate Stone OptionGroupFormat::OPTION_GROUP_FORMAT | 274b9c1b51eSKate Stone OptionGroupFormat::OPTION_GROUP_GDB_FMT, 275b9c1b51eSKate Stone LLDB_OPT_SET_1); 2761deb7962SGreg Clayton m_option_group.Append(&m_command_options); 277b9c1b51eSKate Stone m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, 278b9c1b51eSKate Stone LLDB_OPT_SET_1 | LLDB_OPT_SET_2); 279f2bd5c3eSSean Callanan m_option_group.Append(&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3); 2801deb7962SGreg Clayton m_option_group.Finalize(); 28130fdc8d8SChris Lattner } 28230fdc8d8SChris Lattner 283c8ecc2a9SEugene Zelenko CommandObjectExpression::~CommandObjectExpression() = default; 28430fdc8d8SChris Lattner 285b9c1b51eSKate Stone Options *CommandObjectExpression::GetOptions() { return &m_option_group; } 28630fdc8d8SChris Lattner 287ae34ed2cSRaphael Isemann void CommandObjectExpression::HandleCompletion(CompletionRequest &request) { 28874829734SRaphael Isemann EvaluateExpressionOptions options; 28974829734SRaphael Isemann options.SetCoerceToId(m_varobj_options.use_objc); 29074829734SRaphael Isemann options.SetLanguage(m_command_options.language); 29174829734SRaphael Isemann options.SetExecutionPolicy(lldb_private::eExecutionPolicyNever); 29274829734SRaphael Isemann options.SetAutoApplyFixIts(false); 29374829734SRaphael Isemann options.SetGenerateDebugInfo(false); 29474829734SRaphael Isemann 29574829734SRaphael Isemann // We need a valid execution context with a frame pointer for this 29674829734SRaphael Isemann // completion, so if we don't have one we should try to make a valid 29774829734SRaphael Isemann // execution context. 29874829734SRaphael Isemann if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr) 29974829734SRaphael Isemann m_interpreter.UpdateExecutionContext(nullptr); 30074829734SRaphael Isemann 30174829734SRaphael Isemann // This didn't work, so let's get out before we start doing things that 30274829734SRaphael Isemann // expect a valid frame pointer. 30374829734SRaphael Isemann if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr) 304ae34ed2cSRaphael Isemann return; 30574829734SRaphael Isemann 30674829734SRaphael Isemann ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 30774829734SRaphael Isemann 30874829734SRaphael Isemann Target *target = exe_ctx.GetTargetPtr(); 30974829734SRaphael Isemann 31074829734SRaphael Isemann if (!target) 311cb2380c9SRaphael Isemann target = &GetDummyTarget(); 31274829734SRaphael Isemann 31374829734SRaphael Isemann unsigned cursor_pos = request.GetRawCursorPos(); 314243f52b5SRaphael Isemann // Get the full user input including the suffix. The suffix is necessary 315243f52b5SRaphael Isemann // as OptionsWithRaw will use it to detect if the cursor is cursor is in the 316243f52b5SRaphael Isemann // argument part of in the raw input part of the arguments. If we cut of 317243f52b5SRaphael Isemann // of the suffix then "expr -arg[cursor] --" would interpret the "-arg" as 318243f52b5SRaphael Isemann // the raw input (as the "--" is hidden in the suffix). 319243f52b5SRaphael Isemann llvm::StringRef code = request.GetRawLineWithUnusedSuffix(); 32074829734SRaphael Isemann 32174829734SRaphael Isemann const std::size_t original_code_size = code.size(); 32274829734SRaphael Isemann 32374829734SRaphael Isemann // Remove the first token which is 'expr' or some alias/abbreviation of that. 32474829734SRaphael Isemann code = llvm::getToken(code).second.ltrim(); 32574829734SRaphael Isemann OptionsWithRaw args(code); 32674829734SRaphael Isemann code = args.GetRawPart(); 32774829734SRaphael Isemann 32874829734SRaphael Isemann // The position where the expression starts in the command line. 32974829734SRaphael Isemann assert(original_code_size >= code.size()); 33074829734SRaphael Isemann std::size_t raw_start = original_code_size - code.size(); 33174829734SRaphael Isemann 33274829734SRaphael Isemann // Check if the cursor is actually in the expression string, and if not, we 33374829734SRaphael Isemann // exit. 33474829734SRaphael Isemann // FIXME: We should complete the options here. 33574829734SRaphael Isemann if (cursor_pos < raw_start) 336ae34ed2cSRaphael Isemann return; 33774829734SRaphael Isemann 33874829734SRaphael Isemann // Make the cursor_pos again relative to the start of the code string. 33974829734SRaphael Isemann assert(cursor_pos >= raw_start); 34074829734SRaphael Isemann cursor_pos -= raw_start; 34174829734SRaphael Isemann 34274829734SRaphael Isemann auto language = exe_ctx.GetFrameRef().GetLanguage(); 34374829734SRaphael Isemann 34474829734SRaphael Isemann Status error; 34574829734SRaphael Isemann lldb::UserExpressionSP expr(target->GetUserExpressionForLanguage( 34674829734SRaphael Isemann code, llvm::StringRef(), language, UserExpression::eResultTypeAny, 34740624a08SAleksandr Urakov options, nullptr, error)); 34874829734SRaphael Isemann if (error.Fail()) 349ae34ed2cSRaphael Isemann return; 35074829734SRaphael Isemann 351c11a780eSRaphael Isemann expr->Complete(exe_ctx, request, cursor_pos); 35274829734SRaphael Isemann } 35374829734SRaphael Isemann 35497206d57SZachary Turner static lldb_private::Status 355b9c1b51eSKate Stone CanBeUsedForElementCountPrinting(ValueObject &valobj) { 356520a422bSEnrico Granata CompilerType type(valobj.GetCompilerType()); 357520a422bSEnrico Granata CompilerType pointee; 358520a422bSEnrico Granata if (!type.IsPointerType(&pointee)) 35997206d57SZachary Turner return Status("as it does not refer to a pointer"); 360520a422bSEnrico Granata if (pointee.IsVoidType()) 36197206d57SZachary Turner return Status("as it refers to a pointer to void"); 36297206d57SZachary Turner return Status(); 363520a422bSEnrico Granata } 364520a422bSEnrico Granata 3658f7c911bSRaphael Isemann EvaluateExpressionOptions 3668f7c911bSRaphael Isemann CommandObjectExpression::GetEvalOptions(const Target &target) { 36735e1bda6SJim Ingham EvaluateExpressionOptions options; 3686fbc48bcSJim Ingham options.SetCoerceToId(m_varobj_options.use_objc); 3696fbc48bcSJim Ingham options.SetUnwindOnError(m_command_options.unwind_on_error); 3706fbc48bcSJim Ingham options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints); 3718f7c911bSRaphael Isemann options.SetKeepInMemory(true); 3726fbc48bcSJim Ingham options.SetUseDynamic(m_varobj_options.use_dynamic); 3736fbc48bcSJim Ingham options.SetTryAllThreads(m_command_options.try_all_threads); 3746fbc48bcSJim Ingham options.SetDebug(m_command_options.debug); 37515663c53SDawn Perchik options.SetLanguage(m_command_options.language); 376b9c1b51eSKate Stone options.SetExecutionPolicy( 377b9c1b51eSKate Stone m_command_options.allow_jit 378b9c1b51eSKate Stone ? EvaluateExpressionOptions::default_execution_policy 379b9c1b51eSKate Stone : lldb_private::eExecutionPolicyNever); 38015663c53SDawn Perchik 381a1e541bfSJim Ingham bool auto_apply_fixits; 382a1e541bfSJim Ingham if (m_command_options.auto_apply_fixits == eLazyBoolCalculate) 3838f7c911bSRaphael Isemann auto_apply_fixits = target.GetEnableAutoApplyFixIts(); 384a1e541bfSJim Ingham else 385a6682a41SJonas Devlieghere auto_apply_fixits = m_command_options.auto_apply_fixits == eLazyBoolYes; 386a1e541bfSJim Ingham 387a1e541bfSJim Ingham options.SetAutoApplyFixIts(auto_apply_fixits); 388203a8adbSRaphael Isemann options.SetRetriesWithFixIts(target.GetNumberOfRetriesWithFixits()); 389a1e541bfSJim Ingham 390863fab69SSean Callanan if (m_command_options.top_level) 391863fab69SSean Callanan options.SetExecutionPolicy(eExecutionPolicyTopLevel); 392863fab69SSean Callanan 39305097246SAdrian Prantl // If there is any chance we are going to stop and want to see what went 39405097246SAdrian Prantl // wrong with our expression, we should generate debug info 39523f8c95aSGreg Clayton if (!m_command_options.ignore_breakpoints || 39623f8c95aSGreg Clayton !m_command_options.unwind_on_error) 39723f8c95aSGreg Clayton options.SetGenerateDebugInfo(true); 39823f8c95aSGreg Clayton 39962afb9f6SGreg Clayton if (m_command_options.timeout > 0) 40043d35418SPavel Labath options.SetTimeout(std::chrono::microseconds(m_command_options.timeout)); 4016f78f386SJim Ingham else 40243d35418SPavel Labath options.SetTimeout(llvm::None); 4038f7c911bSRaphael Isemann return options; 4048f7c911bSRaphael Isemann } 405d4439aa9SEnrico Granata 4068f7c911bSRaphael Isemann bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr, 4077c6e52acSRaphael Isemann Stream &output_stream, 4087c6e52acSRaphael Isemann Stream &error_stream, 4097c6e52acSRaphael Isemann CommandReturnObject &result) { 4108f7c911bSRaphael Isemann // Don't use m_exe_ctx as this might be called asynchronously after the 4118f7c911bSRaphael Isemann // command object DoExecute has finished when doing multi-line expression 4128f7c911bSRaphael Isemann // that use an input reader... 4138f7c911bSRaphael Isemann ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 4148f7c911bSRaphael Isemann 4158f7c911bSRaphael Isemann Target *target = exe_ctx.GetTargetPtr(); 4168f7c911bSRaphael Isemann 4178f7c911bSRaphael Isemann if (!target) 4188f7c911bSRaphael Isemann target = &GetDummyTarget(); 4198f7c911bSRaphael Isemann 4208f7c911bSRaphael Isemann lldb::ValueObjectSP result_valobj_sp; 4218f7c911bSRaphael Isemann StackFrame *frame = exe_ctx.GetFramePtr(); 4228f7c911bSRaphael Isemann 4238f7c911bSRaphael Isemann const EvaluateExpressionOptions options = GetEvalOptions(*target); 424b9c1b51eSKate Stone ExpressionResults success = target->EvaluateExpression( 425b9c1b51eSKate Stone expr, frame, result_valobj_sp, options, &m_fixed_expression); 426e5ee6f04SJim Ingham 427b9c1b51eSKate Stone // We only tell you about the FixIt if we applied it. The compiler errors 428b9c1b51eSKate Stone // will suggest the FixIt if it parsed. 4297c6e52acSRaphael Isemann if (!m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) { 430e5ee6f04SJim Ingham if (success == eExpressionCompleted) 4317c6e52acSRaphael Isemann error_stream.Printf(" Fix-it applied, fixed expression was: \n %s\n", 432b9c1b51eSKate Stone m_fixed_expression.c_str()); 433e5ee6f04SJim Ingham } 4348b2fe6dcSGreg Clayton 435b9c1b51eSKate Stone if (result_valobj_sp) { 436bf154daeSSean Callanan Format format = m_format_options.GetFormat(); 437bf154daeSSean Callanan 438b9c1b51eSKate Stone if (result_valobj_sp->GetError().Success()) { 439b9c1b51eSKate Stone if (format != eFormatVoid) { 4401deb7962SGreg Clayton if (format != eFormatDefault) 4411deb7962SGreg Clayton result_valobj_sp->SetFormat(format); 44232c4085bSGreg Clayton 443b9c1b51eSKate Stone if (m_varobj_options.elem_count > 0) { 44497206d57SZachary Turner Status error(CanBeUsedForElementCountPrinting(*result_valobj_sp)); 445b9c1b51eSKate Stone if (error.Fail()) { 4467c6e52acSRaphael Isemann result.AppendErrorWithFormat( 447b9c1b51eSKate Stone "expression cannot be used with --element-count %s\n", 448b9c1b51eSKate Stone error.AsCString("")); 4497c6e52acSRaphael Isemann result.SetStatus(eReturnStatusFailed); 450520a422bSEnrico Granata return false; 451520a422bSEnrico Granata } 452520a422bSEnrico Granata } 453520a422bSEnrico Granata 454b9c1b51eSKate Stone DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions( 455b9c1b51eSKate Stone m_command_options.m_verbosity, format)); 456b9c1b51eSKate Stone options.SetVariableFormatDisplayLanguage( 457b9c1b51eSKate Stone result_valobj_sp->GetPreferredDisplayLanguage()); 458770eb05aSEnrico Granata 4597c6e52acSRaphael Isemann result_valobj_sp->Dump(output_stream, options); 4604d93b8cdSEnrico Granata 4617c6e52acSRaphael Isemann result.SetStatus(eReturnStatusSuccessFinishResult); 46230fdc8d8SChris Lattner } 463b9c1b51eSKate Stone } else { 464b9c1b51eSKate Stone if (result_valobj_sp->GetError().GetError() == 465a35912daSKrasimir Georgiev UserExpression::kNoResult) { 46657179860SJonas Devlieghere if (format != eFormatVoid && GetDebugger().GetNotifyVoid()) { 4677c6e52acSRaphael Isemann error_stream.PutCString("(void)\n"); 468bcf897faSSean Callanan } 469bccce813SSean Callanan 4707c6e52acSRaphael Isemann result.SetStatus(eReturnStatusSuccessFinishResult); 471b9c1b51eSKate Stone } else { 4725fd05903SGreg Clayton const char *error_cstr = result_valobj_sp->GetError().AsCString(); 473b9c1b51eSKate Stone if (error_cstr && error_cstr[0]) { 474c7bece56SGreg Clayton const size_t error_cstr_len = strlen(error_cstr); 475365b30a3SPavel Labath const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n'; 4765fd05903SGreg Clayton if (strstr(error_cstr, "error:") != error_cstr) 4777c6e52acSRaphael Isemann error_stream.PutCString("error: "); 4787c6e52acSRaphael Isemann error_stream.Write(error_cstr, error_cstr_len); 4795fd05903SGreg Clayton if (!ends_with_newline) 4807c6e52acSRaphael Isemann error_stream.EOL(); 481b9c1b51eSKate Stone } else { 4827c6e52acSRaphael Isemann error_stream.PutCString("error: unknown error\n"); 4835fd05903SGreg Clayton } 4845fd05903SGreg Clayton 4857c6e52acSRaphael Isemann result.SetStatus(eReturnStatusFailed); 48630fdc8d8SChris Lattner } 4878b2fe6dcSGreg Clayton } 4888b2fe6dcSGreg Clayton } 48930fdc8d8SChris Lattner 4906a4905aeSRaphael Isemann return (success != eExpressionSetupError && 4916a4905aeSRaphael Isemann success != eExpressionParseError); 49230fdc8d8SChris Lattner } 49330fdc8d8SChris Lattner 494b9c1b51eSKate Stone void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler, 495b9c1b51eSKate Stone std::string &line) { 49644d93782SGreg Clayton io_handler.SetIsDone(true); 497b9c1b51eSKate Stone // StreamSP output_stream = 498b9c1b51eSKate Stone // io_handler.GetDebugger().GetAsyncOutputStream(); 49944d93782SGreg Clayton // StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream(); 5007ca15ba7SLawrence D'Anna StreamFileSP output_sp = io_handler.GetOutputStreamFileSP(); 5017ca15ba7SLawrence D'Anna StreamFileSP error_sp = io_handler.GetErrorStreamFileSP(); 50244d93782SGreg Clayton 503*de019b88SJonas Devlieghere CommandReturnObject return_obj( 504*de019b88SJonas Devlieghere GetCommandInterpreter().GetDebugger().GetUseColor()); 5057c6e52acSRaphael Isemann EvaluateExpression(line.c_str(), *output_sp, *error_sp, return_obj); 50644d93782SGreg Clayton if (output_sp) 50744d93782SGreg Clayton output_sp->Flush(); 50844d93782SGreg Clayton if (error_sp) 50944d93782SGreg Clayton error_sp->Flush(); 51044d93782SGreg Clayton } 51144d93782SGreg Clayton 512b9c1b51eSKate Stone bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler, 513b9c1b51eSKate Stone StringList &lines) { 514f52c40c5SSean Callanan // An empty lines is used to indicate the end of input 515f52c40c5SSean Callanan const size_t num_lines = lines.GetSize(); 516b9c1b51eSKate Stone if (num_lines > 0 && lines[num_lines - 1].empty()) { 51705097246SAdrian Prantl // Remove the last empty line from "lines" so it doesn't appear in our 51805097246SAdrian Prantl // resulting input and return true to indicate we are done getting lines 51944d93782SGreg Clayton lines.PopBack(); 520f52c40c5SSean Callanan return true; 52144d93782SGreg Clayton } 522f52c40c5SSean Callanan return false; 52344d93782SGreg Clayton } 52444d93782SGreg Clayton 525b9c1b51eSKate Stone void CommandObjectExpression::GetMultilineExpression() { 52630fdc8d8SChris Lattner m_expr_lines.clear(); 52730fdc8d8SChris Lattner m_expr_line_count = 0; 52830fdc8d8SChris Lattner 52944d93782SGreg Clayton Debugger &debugger = GetCommandInterpreter().GetDebugger(); 530e30f11d9SKate Stone bool color_prompt = debugger.GetUseColor(); 53144d93782SGreg Clayton const bool multiple_lines = true; // Get multiple lines 532b9c1b51eSKate Stone IOHandlerSP io_handler_sp( 533b9c1b51eSKate Stone new IOHandlerEditline(debugger, IOHandler::Type::Expression, 53444d93782SGreg Clayton "lldb-expr", // Name of input reader for history 535514d8cd8SZachary Turner llvm::StringRef(), // No prompt 536514d8cd8SZachary Turner llvm::StringRef(), // Continuation prompt 537b9c1b51eSKate Stone multiple_lines, color_prompt, 538f6913cd7SGreg Clayton 1, // Show line numbers starting at 1 539d77c2e09SJonas Devlieghere *this, nullptr)); 540b6892508SGreg Clayton 5417ca15ba7SLawrence D'Anna StreamFileSP output_sp = io_handler_sp->GetOutputStreamFileSP(); 542b9c1b51eSKate Stone if (output_sp) { 543b9c1b51eSKate Stone output_sp->PutCString( 544b9c1b51eSKate Stone "Enter expressions, then terminate with an empty line to evaluate:\n"); 545b6892508SGreg Clayton output_sp->Flush(); 546b6892508SGreg Clayton } 5477ce2de2cSJonas Devlieghere debugger.RunIOHandlerAsync(io_handler_sp); 548cf28a8b7SGreg Clayton } 549cf28a8b7SGreg Clayton 550c5bfa3daSJonas Devlieghere static EvaluateExpressionOptions 551c5bfa3daSJonas Devlieghere GetExprOptions(ExecutionContext &ctx, 552c5bfa3daSJonas Devlieghere CommandObjectExpression::CommandOptions command_options) { 553c5bfa3daSJonas Devlieghere command_options.OptionParsingStarting(&ctx); 554c5bfa3daSJonas Devlieghere 555c5bfa3daSJonas Devlieghere // Default certain settings for REPL regardless of the global settings. 556c5bfa3daSJonas Devlieghere command_options.unwind_on_error = false; 557c5bfa3daSJonas Devlieghere command_options.ignore_breakpoints = false; 558c5bfa3daSJonas Devlieghere command_options.debug = false; 559c5bfa3daSJonas Devlieghere 560c5bfa3daSJonas Devlieghere EvaluateExpressionOptions expr_options; 561c5bfa3daSJonas Devlieghere expr_options.SetUnwindOnError(command_options.unwind_on_error); 562c5bfa3daSJonas Devlieghere expr_options.SetIgnoreBreakpoints(command_options.ignore_breakpoints); 563c5bfa3daSJonas Devlieghere expr_options.SetTryAllThreads(command_options.try_all_threads); 564c5bfa3daSJonas Devlieghere 565c5bfa3daSJonas Devlieghere if (command_options.timeout > 0) 566c5bfa3daSJonas Devlieghere expr_options.SetTimeout(std::chrono::microseconds(command_options.timeout)); 567c5bfa3daSJonas Devlieghere else 568c5bfa3daSJonas Devlieghere expr_options.SetTimeout(llvm::None); 569c5bfa3daSJonas Devlieghere 570c5bfa3daSJonas Devlieghere return expr_options; 571c5bfa3daSJonas Devlieghere } 572c5bfa3daSJonas Devlieghere 5734d51a902SRaphael Isemann bool CommandObjectExpression::DoExecute(llvm::StringRef command, 574b9c1b51eSKate Stone CommandReturnObject &result) { 575e5ee6f04SJim Ingham m_fixed_expression.clear(); 576e1cfbc79STodd Fiala auto exe_ctx = GetCommandInterpreter().GetExecutionContext(); 577e1cfbc79STodd Fiala m_option_group.NotifyOptionParsingStarting(&exe_ctx); 578cf28a8b7SGreg Clayton 5794d51a902SRaphael Isemann if (command.empty()) { 580cf28a8b7SGreg Clayton GetMultilineExpression(); 58130fdc8d8SChris Lattner return result.Succeeded(); 58230fdc8d8SChris Lattner } 58330fdc8d8SChris Lattner 5843a0e1270SRaphael Isemann OptionsWithRaw args(command); 5854d51a902SRaphael Isemann llvm::StringRef expr = args.GetRawPart(); 58630fdc8d8SChris Lattner 5873a0e1270SRaphael Isemann if (args.HasArgs()) { 5883a0e1270SRaphael Isemann if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group, exe_ctx)) 58930fdc8d8SChris Lattner return false; 590f6b8b581SGreg Clayton 591b9c1b51eSKate Stone if (m_repl_option.GetOptionValue().GetCurrentValue()) { 5925a1bac4dSRaphael Isemann Target &target = GetSelectedOrDummyTarget(); 593f2bd5c3eSSean Callanan // Drop into REPL 594f2bd5c3eSSean Callanan m_expr_lines.clear(); 595f2bd5c3eSSean Callanan m_expr_line_count = 0; 596f2bd5c3eSSean Callanan 5975a1bac4dSRaphael Isemann Debugger &debugger = target.GetDebugger(); 598f2bd5c3eSSean Callanan 599b9c1b51eSKate Stone // Check if the LLDB command interpreter is sitting on top of a REPL 60005097246SAdrian Prantl // that launched it... 6013a0e1270SRaphael Isemann if (debugger.CheckTopIOHandlerTypes(IOHandler::Type::CommandInterpreter, 6023a0e1270SRaphael Isemann IOHandler::Type::REPL)) { 603b9c1b51eSKate Stone // the LLDB command interpreter is sitting on top of a REPL that 60405097246SAdrian Prantl // launched it, so just say the command interpreter is done and 60505097246SAdrian Prantl // fall back to the existing REPL 606f2bd5c3eSSean Callanan m_interpreter.GetIOHandler(false)->SetIsDone(true); 607b9c1b51eSKate Stone } else { 608b9c1b51eSKate Stone // We are launching the REPL on top of the current LLDB command 60905097246SAdrian Prantl // interpreter, so just push one 610f2bd5c3eSSean Callanan bool initialize = false; 61197206d57SZachary Turner Status repl_error; 6125a1bac4dSRaphael Isemann REPLSP repl_sp(target.GetREPL(repl_error, m_command_options.language, 6133a0e1270SRaphael Isemann nullptr, false)); 614f2bd5c3eSSean Callanan 615b9c1b51eSKate Stone if (!repl_sp) { 616f2bd5c3eSSean Callanan initialize = true; 6175a1bac4dSRaphael Isemann repl_sp = target.GetREPL(repl_error, m_command_options.language, 618b9c1b51eSKate Stone nullptr, true); 619b9c1b51eSKate Stone if (!repl_error.Success()) { 620f2bd5c3eSSean Callanan result.SetError(repl_error); 621f2bd5c3eSSean Callanan return result.Succeeded(); 622f2bd5c3eSSean Callanan } 623f2bd5c3eSSean Callanan } 624f2bd5c3eSSean Callanan 625b9c1b51eSKate Stone if (repl_sp) { 626b9c1b51eSKate Stone if (initialize) { 627c5bfa3daSJonas Devlieghere repl_sp->SetEvaluateOptions( 628c5bfa3daSJonas Devlieghere GetExprOptions(exe_ctx, m_command_options)); 629f2bd5c3eSSean Callanan repl_sp->SetFormatOptions(m_format_options); 630f2bd5c3eSSean Callanan repl_sp->SetValueObjectDisplayOptions(m_varobj_options); 631f2bd5c3eSSean Callanan } 632f2bd5c3eSSean Callanan 633f2bd5c3eSSean Callanan IOHandlerSP io_handler_sp(repl_sp->GetIOHandler()); 634f2bd5c3eSSean Callanan io_handler_sp->SetIsDone(false); 6357ce2de2cSJonas Devlieghere debugger.RunIOHandlerAsync(io_handler_sp); 636b9c1b51eSKate Stone } else { 637b9c1b51eSKate Stone repl_error.SetErrorStringWithFormat( 638b9c1b51eSKate Stone "Couldn't create a REPL for %s", 639b9c1b51eSKate Stone Language::GetNameForLanguageType(m_command_options.language)); 640f2bd5c3eSSean Callanan result.SetError(repl_error); 641f2bd5c3eSSean Callanan return result.Succeeded(); 642f2bd5c3eSSean Callanan } 643f2bd5c3eSSean Callanan } 644f2bd5c3eSSean Callanan } 645cf28a8b7SGreg Clayton // No expression following options 6464d51a902SRaphael Isemann else if (expr.empty()) { 647cf28a8b7SGreg Clayton GetMultilineExpression(); 648cf28a8b7SGreg Clayton return result.Succeeded(); 649cf28a8b7SGreg Clayton } 65030fdc8d8SChris Lattner } 65130fdc8d8SChris Lattner 652cb2380c9SRaphael Isemann Target &target = GetSelectedOrDummyTarget(); 6537c6e52acSRaphael Isemann if (EvaluateExpression(expr, result.GetOutputStream(), 6547c6e52acSRaphael Isemann result.GetErrorStream(), result)) { 65524fff242SDavide Italiano 656cb2380c9SRaphael Isemann if (!m_fixed_expression.empty() && target.GetEnableNotifyAboutFixIts()) { 657e5ee6f04SJim Ingham CommandHistory &history = m_interpreter.GetCommandHistory(); 658b9c1b51eSKate Stone // FIXME: Can we figure out what the user actually typed (e.g. some alias 659b9c1b51eSKate Stone // for expr???) 660e5ee6f04SJim Ingham // If we can it would be nice to show that. 661e5ee6f04SJim Ingham std::string fixed_command("expression "); 6623a0e1270SRaphael Isemann if (args.HasArgs()) { 663e5ee6f04SJim Ingham // Add in any options that might have been in the original command: 664adcd0268SBenjamin Kramer fixed_command.append(std::string(args.GetArgStringWithDelimiter())); 665e5ee6f04SJim Ingham fixed_command.append(m_fixed_expression); 6663a0e1270SRaphael Isemann } else 6673a0e1270SRaphael Isemann fixed_command.append(m_fixed_expression); 668e5ee6f04SJim Ingham history.AppendString(fixed_command); 669e5ee6f04SJim Ingham } 67005097246SAdrian Prantl // Increment statistics to record this expression evaluation success. 671cb2380c9SRaphael Isemann target.IncrementStats(StatisticKind::ExpressionSuccessful); 672fcd43b71SJohnny Chen return true; 673e5ee6f04SJim Ingham } 674fcd43b71SJohnny Chen 67505097246SAdrian Prantl // Increment statistics to record this expression evaluation failure. 676cb2380c9SRaphael Isemann target.IncrementStats(StatisticKind::ExpressionFailure); 677fcd43b71SJohnny Chen result.SetStatus(eReturnStatusFailed); 678fcd43b71SJohnny Chen return false; 67930fdc8d8SChris Lattner } 680