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" 17*7ced9fffSJonas Devlieghere #include "lldb/Interpreter/CommandOptionArgumentTable.h" 1830fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 1947cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h" 20b9c1b51eSKate Stone #include "lldb/Target/Language.h" 2130fdc8d8SChris Lattner #include "lldb/Target/Process.h" 22b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h" 2330fdc8d8SChris Lattner #include "lldb/Target/Target.h" 2430fdc8d8SChris Lattner 2530fdc8d8SChris Lattner using namespace lldb; 2630fdc8d8SChris Lattner using namespace lldb_private; 2730fdc8d8SChris Lattner 2824f9a2f5SShafik Yaghmour CommandObjectExpression::CommandOptions::CommandOptions() = default; 2930fdc8d8SChris Lattner 30c8ecc2a9SEugene Zelenko CommandObjectExpression::CommandOptions::~CommandOptions() = default; 3130fdc8d8SChris Lattner 32ec67e734SRaphael Isemann #define LLDB_OPTIONS_expression 33ec67e734SRaphael Isemann #include "CommandOptions.inc" 341deb7962SGreg Clayton 3597206d57SZachary Turner Status CommandObjectExpression::CommandOptions::SetOptionValue( 368cef4b0bSZachary Turner uint32_t option_idx, llvm::StringRef option_arg, 37b9c1b51eSKate Stone ExecutionContext *execution_context) { 3897206d57SZachary Turner Status error; 3930fdc8d8SChris Lattner 401f0f5b5bSZachary Turner const int short_option = GetDefinitions()[option_idx].short_option; 4130fdc8d8SChris Lattner 42b9c1b51eSKate Stone switch (short_option) { 4315663c53SDawn Perchik case 'l': 440e0984eeSJim Ingham language = Language::GetLanguageTypeFromString(option_arg); 4515663c53SDawn Perchik if (language == eLanguageTypeUnknown) 46b9c1b51eSKate Stone error.SetErrorStringWithFormat( 478cef4b0bSZachary Turner "unknown language type: '%s' for expression", 488cef4b0bSZachary Turner option_arg.str().c_str()); 4915663c53SDawn Perchik break; 5030fdc8d8SChris Lattner 51b9c1b51eSKate Stone case 'a': { 5235e1bda6SJim Ingham bool success; 5335e1bda6SJim Ingham bool result; 5447cbf4a0SPavel Labath result = OptionArgParser::ToBoolean(option_arg, true, &success); 5535e1bda6SJim Ingham if (!success) 56b9c1b51eSKate Stone error.SetErrorStringWithFormat( 578cef4b0bSZachary Turner "invalid all-threads value setting: \"%s\"", 588cef4b0bSZachary Turner option_arg.str().c_str()); 5935e1bda6SJim Ingham else 6035e1bda6SJim Ingham try_all_threads = result; 61b9c1b51eSKate Stone } break; 626c68fb45SJim Ingham 63b9c1b51eSKate Stone case 'i': { 64184e9811SJim Ingham bool success; 6547cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 66184e9811SJim Ingham if (success) 67184e9811SJim Ingham ignore_breakpoints = tmp_value; 68184e9811SJim Ingham else 69b9c1b51eSKate Stone error.SetErrorStringWithFormat( 708cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 718cef4b0bSZachary Turner option_arg.str().c_str()); 72184e9811SJim Ingham break; 73184e9811SJim Ingham } 743fe71581SMarianne Mailhot-Sarrasin 75b9c1b51eSKate Stone case 'j': { 763fe71581SMarianne Mailhot-Sarrasin bool success; 7747cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 783fe71581SMarianne Mailhot-Sarrasin if (success) 793fe71581SMarianne Mailhot-Sarrasin allow_jit = tmp_value; 803fe71581SMarianne Mailhot-Sarrasin else 81b9c1b51eSKate Stone error.SetErrorStringWithFormat( 828cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 838cef4b0bSZachary Turner option_arg.str().c_str()); 843fe71581SMarianne Mailhot-Sarrasin break; 853fe71581SMarianne Mailhot-Sarrasin } 863fe71581SMarianne Mailhot-Sarrasin 878cef4b0bSZachary Turner case 't': 888cef4b0bSZachary Turner if (option_arg.getAsInteger(0, timeout)) { 898cef4b0bSZachary Turner timeout = 0; 90b9c1b51eSKate Stone error.SetErrorStringWithFormat("invalid timeout setting \"%s\"", 918cef4b0bSZachary Turner option_arg.str().c_str()); 928cef4b0bSZachary Turner } 938cef4b0bSZachary Turner break; 9435e1bda6SJim Ingham 95b9c1b51eSKate Stone case 'u': { 96399f1cafSJim Ingham bool success; 9747cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 98184e9811SJim Ingham if (success) 99184e9811SJim Ingham unwind_on_error = tmp_value; 100184e9811SJim Ingham else 101b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1028cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 1038cef4b0bSZachary Turner option_arg.str().c_str()); 104399f1cafSJim Ingham break; 1053bfdaa2aSSean Callanan } 1064d93b8cdSEnrico Granata 1074d93b8cdSEnrico Granata case 'v': 108543a26e9SEnrico Granata if (option_arg.empty()) { 1094d93b8cdSEnrico Granata m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull; 1104d93b8cdSEnrico Granata break; 1114d93b8cdSEnrico Granata } 11247cbf4a0SPavel Labath m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) 11347cbf4a0SPavel Labath OptionArgParser::ToOptionEnum( 1141f0f5b5bSZachary Turner option_arg, GetDefinitions()[option_idx].enum_values, 0, error); 1154d93b8cdSEnrico Granata if (!error.Success()) 116b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1178cef4b0bSZachary Turner "unrecognized value for description-verbosity '%s'", 1188cef4b0bSZachary Turner option_arg.str().c_str()); 1194d93b8cdSEnrico Granata break; 1204d93b8cdSEnrico Granata 12162afb9f6SGreg Clayton case 'g': 12262afb9f6SGreg Clayton debug = true; 12362afb9f6SGreg Clayton unwind_on_error = false; 12462afb9f6SGreg Clayton ignore_breakpoints = false; 12562afb9f6SGreg Clayton break; 12662afb9f6SGreg Clayton 127863fab69SSean Callanan case 'p': 128863fab69SSean Callanan top_level = true; 129863fab69SSean Callanan break; 130863fab69SSean Callanan 131b9c1b51eSKate Stone case 'X': { 132a1e541bfSJim Ingham bool success; 13347cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 134a1e541bfSJim Ingham if (success) 135a1e541bfSJim Ingham auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo; 136a1e541bfSJim Ingham else 137b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1388cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 1398cef4b0bSZachary Turner option_arg.str().c_str()); 140a1e541bfSJim Ingham break; 141a1e541bfSJim Ingham } 142a1e541bfSJim Ingham 14330fdc8d8SChris Lattner default: 14436162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 14530fdc8d8SChris Lattner } 14630fdc8d8SChris Lattner 14730fdc8d8SChris Lattner return error; 14830fdc8d8SChris Lattner } 14930fdc8d8SChris Lattner 150b9c1b51eSKate Stone void CommandObjectExpression::CommandOptions::OptionParsingStarting( 151b9c1b51eSKate Stone ExecutionContext *execution_context) { 152e1cfbc79STodd Fiala auto process_sp = 153e1cfbc79STodd Fiala execution_context ? execution_context->GetProcessSP() : ProcessSP(); 154b9c1b51eSKate Stone if (process_sp) { 155e1cfbc79STodd Fiala ignore_breakpoints = process_sp->GetIgnoreBreakpointsInExpressions(); 156e1cfbc79STodd Fiala unwind_on_error = process_sp->GetUnwindOnErrorInExpressions(); 157b9c1b51eSKate Stone } else { 158fc03f8fcSGreg Clayton ignore_breakpoints = true; 159399f1cafSJim Ingham unwind_on_error = true; 160184e9811SJim Ingham } 161184e9811SJim Ingham 16230fdc8d8SChris Lattner show_summary = true; 16335e1bda6SJim Ingham try_all_threads = true; 16435e1bda6SJim Ingham timeout = 0; 16562afb9f6SGreg Clayton debug = false; 16615663c53SDawn Perchik language = eLanguageTypeUnknown; 1674d93b8cdSEnrico Granata m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact; 168a1e541bfSJim Ingham auto_apply_fixits = eLazyBoolCalculate; 169863fab69SSean Callanan top_level = false; 1703fe71581SMarianne Mailhot-Sarrasin allow_jit = true; 17130fdc8d8SChris Lattner } 17230fdc8d8SChris Lattner 1731f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> 174b9c1b51eSKate Stone CommandObjectExpression::CommandOptions::GetDefinitions() { 17570602439SZachary Turner return llvm::makeArrayRef(g_expression_options); 17630fdc8d8SChris Lattner } 17730fdc8d8SChris Lattner 178b9c1b51eSKate Stone CommandObjectExpression::CommandObjectExpression( 179b9c1b51eSKate Stone CommandInterpreter &interpreter) 180a925974bSAdrian Prantl : CommandObjectRaw(interpreter, "expression", 181a925974bSAdrian Prantl "Evaluate an expression on the current " 182b9c1b51eSKate Stone "thread. Displays any returned value " 183b9c1b51eSKate Stone "with LLDB's default formatting.", 184a925974bSAdrian Prantl "", 185a925974bSAdrian Prantl eCommandProcessMustBePaused | eCommandTryTargetAPILock), 18644d93782SGreg Clayton IOHandlerDelegate(IOHandlerDelegate::Completion::Expression), 187abb0ed44SKazu Hirata m_format_options(eFormatDefault), 188b9c1b51eSKate Stone m_repl_option(LLDB_OPT_SET_1, false, "repl", 'r', "Drop into REPL", false, 189b9c1b51eSKate Stone true), 190abb0ed44SKazu Hirata m_command_options(), m_expr_line_count(0) { 19130fdc8d8SChris Lattner SetHelpLong( 192ea671fbdSKate Stone R"( 193d0309916SJim Ingham Single and multi-line expressions: 194d0309916SJim Ingham 195d0309916SJim Ingham )" 196d0309916SJim Ingham " The expression provided on the command line must be a complete expression \ 197d0309916SJim Ingham with no newlines. To evaluate a multi-line expression, \ 198d0309916SJim Ingham hit a return after an empty expression, and lldb will enter the multi-line expression editor. \ 199d0309916SJim Ingham Hit return on an empty line to end the multi-line expression." 200d0309916SJim Ingham 201d0309916SJim Ingham R"( 202d0309916SJim Ingham 203ea671fbdSKate Stone Timeouts: 204ea671fbdSKate Stone 205b9c1b51eSKate Stone )" 206b9c1b51eSKate Stone " If the expression can be evaluated statically (without running code) then it will be. \ 207ea671fbdSKate Stone Otherwise, by default the expression will run on the current thread with a short timeout: \ 208ea671fbdSKate Stone currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \ 209ea671fbdSKate Stone and resumed with all threads running. You can use the -a option to disable retrying on all \ 210b9c1b51eSKate Stone threads. You can use the -t option to set a shorter timeout." 211b9c1b51eSKate Stone R"( 212ea671fbdSKate Stone 213ea671fbdSKate Stone User defined variables: 214ea671fbdSKate Stone 215b9c1b51eSKate Stone )" 216b9c1b51eSKate Stone " You can define your own variables for convenience or to be used in subsequent expressions. \ 217ea671fbdSKate Stone You define them the same way you would define variables in C. If the first character of \ 218ea671fbdSKate Stone your user defined variable is a $, then the variable's value will be available in future \ 219b9c1b51eSKate Stone expressions, otherwise it will just be available in the current expression." 220b9c1b51eSKate Stone R"( 221ea671fbdSKate Stone 222ea671fbdSKate Stone Continuing evaluation after a breakpoint: 223ea671fbdSKate Stone 224b9c1b51eSKate Stone )" 225b9c1b51eSKate Stone " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \ 226ea671fbdSKate Stone you are done with your investigation, you can either remove the expression execution frames \ 227ea671fbdSKate Stone from the stack with \"thread return -x\" or if you are still interested in the expression result \ 228ea671fbdSKate Stone you can issue the \"continue\" command and the expression evaluation will complete and the \ 229ea671fbdSKate Stone expression result will be available using the \"thread.completed-expression\" key in the thread \ 230b9c1b51eSKate Stone format." 231d0309916SJim Ingham 232b9c1b51eSKate Stone R"( 233ea671fbdSKate Stone 234ea671fbdSKate Stone Examples: 235ea671fbdSKate Stone 236ea671fbdSKate Stone expr my_struct->a = my_array[3] 237ea671fbdSKate Stone expr -f bin -- (index * 8) + 5 238ea671fbdSKate Stone expr unsigned int $foo = 5 239b9c1b51eSKate Stone expr char c[] = \"foo\"; c[0])"); 240405fe67fSCaroline Tice 241405fe67fSCaroline Tice CommandArgumentEntry arg; 242405fe67fSCaroline Tice CommandArgumentData expression_arg; 243405fe67fSCaroline Tice 244405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 245405fe67fSCaroline Tice expression_arg.arg_type = eArgTypeExpression; 246405fe67fSCaroline Tice expression_arg.arg_repetition = eArgRepeatPlain; 247405fe67fSCaroline Tice 248b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the argument 249b9c1b51eSKate Stone // entry. 250405fe67fSCaroline Tice arg.push_back(expression_arg); 251405fe67fSCaroline Tice 252405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 253405fe67fSCaroline Tice m_arguments.push_back(arg); 2541deb7962SGreg Clayton 2555009f9d5SGreg Clayton // Add the "--format" and "--gdb-format" 256b9c1b51eSKate Stone m_option_group.Append(&m_format_options, 257b9c1b51eSKate Stone OptionGroupFormat::OPTION_GROUP_FORMAT | 258b9c1b51eSKate Stone OptionGroupFormat::OPTION_GROUP_GDB_FMT, 259b9c1b51eSKate Stone LLDB_OPT_SET_1); 2601deb7962SGreg Clayton m_option_group.Append(&m_command_options); 261b9c1b51eSKate Stone m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, 262b9c1b51eSKate Stone LLDB_OPT_SET_1 | LLDB_OPT_SET_2); 263f2bd5c3eSSean Callanan m_option_group.Append(&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3); 2641deb7962SGreg Clayton m_option_group.Finalize(); 26530fdc8d8SChris Lattner } 26630fdc8d8SChris Lattner 267c8ecc2a9SEugene Zelenko CommandObjectExpression::~CommandObjectExpression() = default; 26830fdc8d8SChris Lattner 269b9c1b51eSKate Stone Options *CommandObjectExpression::GetOptions() { return &m_option_group; } 27030fdc8d8SChris Lattner 271ae34ed2cSRaphael Isemann void CommandObjectExpression::HandleCompletion(CompletionRequest &request) { 27274829734SRaphael Isemann EvaluateExpressionOptions options; 27374829734SRaphael Isemann options.SetCoerceToId(m_varobj_options.use_objc); 27474829734SRaphael Isemann options.SetLanguage(m_command_options.language); 27574829734SRaphael Isemann options.SetExecutionPolicy(lldb_private::eExecutionPolicyNever); 27674829734SRaphael Isemann options.SetAutoApplyFixIts(false); 27774829734SRaphael Isemann options.SetGenerateDebugInfo(false); 27874829734SRaphael Isemann 27936de94cfSTatyana Krasnukha ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 28074829734SRaphael Isemann 28136de94cfSTatyana Krasnukha // Get out before we start doing things that expect a valid frame pointer. 28236de94cfSTatyana Krasnukha if (exe_ctx.GetFramePtr() == nullptr) 283ae34ed2cSRaphael Isemann return; 28474829734SRaphael Isemann 285b2fa3b92SJonas Devlieghere Target *exe_target = exe_ctx.GetTargetPtr(); 286b2fa3b92SJonas Devlieghere Target &target = exe_target ? *exe_target : GetDummyTarget(); 28774829734SRaphael Isemann 28874829734SRaphael Isemann unsigned cursor_pos = request.GetRawCursorPos(); 289243f52b5SRaphael Isemann // Get the full user input including the suffix. The suffix is necessary 290243f52b5SRaphael Isemann // as OptionsWithRaw will use it to detect if the cursor is cursor is in the 291243f52b5SRaphael Isemann // argument part of in the raw input part of the arguments. If we cut of 292243f52b5SRaphael Isemann // of the suffix then "expr -arg[cursor] --" would interpret the "-arg" as 293243f52b5SRaphael Isemann // the raw input (as the "--" is hidden in the suffix). 294243f52b5SRaphael Isemann llvm::StringRef code = request.GetRawLineWithUnusedSuffix(); 29574829734SRaphael Isemann 29674829734SRaphael Isemann const std::size_t original_code_size = code.size(); 29774829734SRaphael Isemann 29874829734SRaphael Isemann // Remove the first token which is 'expr' or some alias/abbreviation of that. 29974829734SRaphael Isemann code = llvm::getToken(code).second.ltrim(); 30074829734SRaphael Isemann OptionsWithRaw args(code); 30174829734SRaphael Isemann code = args.GetRawPart(); 30274829734SRaphael Isemann 30374829734SRaphael Isemann // The position where the expression starts in the command line. 30474829734SRaphael Isemann assert(original_code_size >= code.size()); 30574829734SRaphael Isemann std::size_t raw_start = original_code_size - code.size(); 30674829734SRaphael Isemann 30774829734SRaphael Isemann // Check if the cursor is actually in the expression string, and if not, we 30874829734SRaphael Isemann // exit. 30974829734SRaphael Isemann // FIXME: We should complete the options here. 31074829734SRaphael Isemann if (cursor_pos < raw_start) 311ae34ed2cSRaphael Isemann return; 31274829734SRaphael Isemann 31374829734SRaphael Isemann // Make the cursor_pos again relative to the start of the code string. 31474829734SRaphael Isemann assert(cursor_pos >= raw_start); 31574829734SRaphael Isemann cursor_pos -= raw_start; 31674829734SRaphael Isemann 31774829734SRaphael Isemann auto language = exe_ctx.GetFrameRef().GetLanguage(); 31874829734SRaphael Isemann 31974829734SRaphael Isemann Status error; 320b2fa3b92SJonas Devlieghere lldb::UserExpressionSP expr(target.GetUserExpressionForLanguage( 32174829734SRaphael Isemann code, llvm::StringRef(), language, UserExpression::eResultTypeAny, 32240624a08SAleksandr Urakov options, nullptr, error)); 32374829734SRaphael Isemann if (error.Fail()) 324ae34ed2cSRaphael Isemann return; 32574829734SRaphael Isemann 326c11a780eSRaphael Isemann expr->Complete(exe_ctx, request, cursor_pos); 32774829734SRaphael Isemann } 32874829734SRaphael Isemann 32997206d57SZachary Turner static lldb_private::Status 330b9c1b51eSKate Stone CanBeUsedForElementCountPrinting(ValueObject &valobj) { 331520a422bSEnrico Granata CompilerType type(valobj.GetCompilerType()); 332520a422bSEnrico Granata CompilerType pointee; 333520a422bSEnrico Granata if (!type.IsPointerType(&pointee)) 33497206d57SZachary Turner return Status("as it does not refer to a pointer"); 335520a422bSEnrico Granata if (pointee.IsVoidType()) 33697206d57SZachary Turner return Status("as it refers to a pointer to void"); 33797206d57SZachary Turner return Status(); 338520a422bSEnrico Granata } 339520a422bSEnrico Granata 3408f7c911bSRaphael Isemann EvaluateExpressionOptions 3418f7c911bSRaphael Isemann CommandObjectExpression::GetEvalOptions(const Target &target) { 34235e1bda6SJim Ingham EvaluateExpressionOptions options; 3436fbc48bcSJim Ingham options.SetCoerceToId(m_varobj_options.use_objc); 3446fbc48bcSJim Ingham options.SetUnwindOnError(m_command_options.unwind_on_error); 3456fbc48bcSJim Ingham options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints); 3468f7c911bSRaphael Isemann options.SetKeepInMemory(true); 3476fbc48bcSJim Ingham options.SetUseDynamic(m_varobj_options.use_dynamic); 3486fbc48bcSJim Ingham options.SetTryAllThreads(m_command_options.try_all_threads); 3496fbc48bcSJim Ingham options.SetDebug(m_command_options.debug); 35015663c53SDawn Perchik options.SetLanguage(m_command_options.language); 351b9c1b51eSKate Stone options.SetExecutionPolicy( 352b9c1b51eSKate Stone m_command_options.allow_jit 353b9c1b51eSKate Stone ? EvaluateExpressionOptions::default_execution_policy 354b9c1b51eSKate Stone : lldb_private::eExecutionPolicyNever); 35515663c53SDawn Perchik 356a1e541bfSJim Ingham bool auto_apply_fixits; 357a1e541bfSJim Ingham if (m_command_options.auto_apply_fixits == eLazyBoolCalculate) 3588f7c911bSRaphael Isemann auto_apply_fixits = target.GetEnableAutoApplyFixIts(); 359a1e541bfSJim Ingham else 360a6682a41SJonas Devlieghere auto_apply_fixits = m_command_options.auto_apply_fixits == eLazyBoolYes; 361a1e541bfSJim Ingham 362a1e541bfSJim Ingham options.SetAutoApplyFixIts(auto_apply_fixits); 363203a8adbSRaphael Isemann options.SetRetriesWithFixIts(target.GetNumberOfRetriesWithFixits()); 364a1e541bfSJim Ingham 365863fab69SSean Callanan if (m_command_options.top_level) 366863fab69SSean Callanan options.SetExecutionPolicy(eExecutionPolicyTopLevel); 367863fab69SSean Callanan 36805097246SAdrian Prantl // If there is any chance we are going to stop and want to see what went 36905097246SAdrian Prantl // wrong with our expression, we should generate debug info 37023f8c95aSGreg Clayton if (!m_command_options.ignore_breakpoints || 37123f8c95aSGreg Clayton !m_command_options.unwind_on_error) 37223f8c95aSGreg Clayton options.SetGenerateDebugInfo(true); 37323f8c95aSGreg Clayton 37462afb9f6SGreg Clayton if (m_command_options.timeout > 0) 37543d35418SPavel Labath options.SetTimeout(std::chrono::microseconds(m_command_options.timeout)); 3766f78f386SJim Ingham else 37743d35418SPavel Labath options.SetTimeout(llvm::None); 3788f7c911bSRaphael Isemann return options; 3798f7c911bSRaphael Isemann } 380d4439aa9SEnrico Granata 3818f7c911bSRaphael Isemann bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr, 3827c6e52acSRaphael Isemann Stream &output_stream, 3837c6e52acSRaphael Isemann Stream &error_stream, 3847c6e52acSRaphael Isemann CommandReturnObject &result) { 3858f7c911bSRaphael Isemann // Don't use m_exe_ctx as this might be called asynchronously after the 3868f7c911bSRaphael Isemann // command object DoExecute has finished when doing multi-line expression 3878f7c911bSRaphael Isemann // that use an input reader... 3888f7c911bSRaphael Isemann ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 389b2fa3b92SJonas Devlieghere Target *exe_target = exe_ctx.GetTargetPtr(); 390b2fa3b92SJonas Devlieghere Target &target = exe_target ? *exe_target : GetDummyTarget(); 3918f7c911bSRaphael Isemann 3928f7c911bSRaphael Isemann lldb::ValueObjectSP result_valobj_sp; 3938f7c911bSRaphael Isemann StackFrame *frame = exe_ctx.GetFramePtr(); 3948f7c911bSRaphael Isemann 395d616a6bdSRaphael Isemann if (m_command_options.top_level && !m_command_options.allow_jit) { 396d616a6bdSRaphael Isemann result.AppendErrorWithFormat( 397d616a6bdSRaphael Isemann "Can't disable JIT compilation for top-level expressions.\n"); 398d616a6bdSRaphael Isemann return false; 399d616a6bdSRaphael Isemann } 400d616a6bdSRaphael Isemann 401b2fa3b92SJonas Devlieghere const EvaluateExpressionOptions options = GetEvalOptions(target); 402b2fa3b92SJonas Devlieghere ExpressionResults success = target.EvaluateExpression( 403b9c1b51eSKate Stone expr, frame, result_valobj_sp, options, &m_fixed_expression); 404e5ee6f04SJim Ingham 405b9c1b51eSKate Stone // We only tell you about the FixIt if we applied it. The compiler errors 406b9c1b51eSKate Stone // will suggest the FixIt if it parsed. 407b2fa3b92SJonas Devlieghere if (!m_fixed_expression.empty() && target.GetEnableNotifyAboutFixIts()) { 4087c6e52acSRaphael Isemann error_stream.Printf(" Fix-it applied, fixed expression was: \n %s\n", 409b9c1b51eSKate Stone m_fixed_expression.c_str()); 410e5ee6f04SJim Ingham } 4118b2fe6dcSGreg Clayton 412b9c1b51eSKate Stone if (result_valobj_sp) { 413bf154daeSSean Callanan Format format = m_format_options.GetFormat(); 414bf154daeSSean Callanan 415b9c1b51eSKate Stone if (result_valobj_sp->GetError().Success()) { 416b9c1b51eSKate Stone if (format != eFormatVoid) { 4171deb7962SGreg Clayton if (format != eFormatDefault) 4181deb7962SGreg Clayton result_valobj_sp->SetFormat(format); 41932c4085bSGreg Clayton 420b9c1b51eSKate Stone if (m_varobj_options.elem_count > 0) { 42197206d57SZachary Turner Status error(CanBeUsedForElementCountPrinting(*result_valobj_sp)); 422b9c1b51eSKate Stone if (error.Fail()) { 4237c6e52acSRaphael Isemann result.AppendErrorWithFormat( 424b9c1b51eSKate Stone "expression cannot be used with --element-count %s\n", 425b9c1b51eSKate Stone error.AsCString("")); 426520a422bSEnrico Granata return false; 427520a422bSEnrico Granata } 428520a422bSEnrico Granata } 429520a422bSEnrico Granata 430b9c1b51eSKate Stone DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions( 431b9c1b51eSKate Stone m_command_options.m_verbosity, format)); 432b9c1b51eSKate Stone options.SetVariableFormatDisplayLanguage( 433b9c1b51eSKate Stone result_valobj_sp->GetPreferredDisplayLanguage()); 434770eb05aSEnrico Granata 4357c6e52acSRaphael Isemann result_valobj_sp->Dump(output_stream, options); 4364d93b8cdSEnrico Granata 4377c6e52acSRaphael Isemann result.SetStatus(eReturnStatusSuccessFinishResult); 43830fdc8d8SChris Lattner } 439b9c1b51eSKate Stone } else { 440b9c1b51eSKate Stone if (result_valobj_sp->GetError().GetError() == 441a35912daSKrasimir Georgiev UserExpression::kNoResult) { 44257179860SJonas Devlieghere if (format != eFormatVoid && GetDebugger().GetNotifyVoid()) { 4437c6e52acSRaphael Isemann error_stream.PutCString("(void)\n"); 444bcf897faSSean Callanan } 445bccce813SSean Callanan 4467c6e52acSRaphael Isemann result.SetStatus(eReturnStatusSuccessFinishResult); 447b9c1b51eSKate Stone } else { 4485fd05903SGreg Clayton const char *error_cstr = result_valobj_sp->GetError().AsCString(); 449b9c1b51eSKate Stone if (error_cstr && error_cstr[0]) { 450c7bece56SGreg Clayton const size_t error_cstr_len = strlen(error_cstr); 451365b30a3SPavel Labath const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n'; 4525fd05903SGreg Clayton if (strstr(error_cstr, "error:") != error_cstr) 4537c6e52acSRaphael Isemann error_stream.PutCString("error: "); 4547c6e52acSRaphael Isemann error_stream.Write(error_cstr, error_cstr_len); 4555fd05903SGreg Clayton if (!ends_with_newline) 4567c6e52acSRaphael Isemann error_stream.EOL(); 457b9c1b51eSKate Stone } else { 4587c6e52acSRaphael Isemann error_stream.PutCString("error: unknown error\n"); 4595fd05903SGreg Clayton } 4605fd05903SGreg Clayton 4617c6e52acSRaphael Isemann result.SetStatus(eReturnStatusFailed); 46230fdc8d8SChris Lattner } 4638b2fe6dcSGreg Clayton } 4648b2fe6dcSGreg Clayton } 46530fdc8d8SChris Lattner 4666a4905aeSRaphael Isemann return (success != eExpressionSetupError && 4676a4905aeSRaphael Isemann success != eExpressionParseError); 46830fdc8d8SChris Lattner } 46930fdc8d8SChris Lattner 470b9c1b51eSKate Stone void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler, 471b9c1b51eSKate Stone std::string &line) { 47244d93782SGreg Clayton io_handler.SetIsDone(true); 4737ca15ba7SLawrence D'Anna StreamFileSP output_sp = io_handler.GetOutputStreamFileSP(); 4747ca15ba7SLawrence D'Anna StreamFileSP error_sp = io_handler.GetErrorStreamFileSP(); 47544d93782SGreg Clayton 476de019b88SJonas Devlieghere CommandReturnObject return_obj( 477de019b88SJonas Devlieghere GetCommandInterpreter().GetDebugger().GetUseColor()); 4787c6e52acSRaphael Isemann EvaluateExpression(line.c_str(), *output_sp, *error_sp, return_obj); 47944d93782SGreg Clayton if (output_sp) 48044d93782SGreg Clayton output_sp->Flush(); 48144d93782SGreg Clayton if (error_sp) 48244d93782SGreg Clayton error_sp->Flush(); 48344d93782SGreg Clayton } 48444d93782SGreg Clayton 485b9c1b51eSKate Stone bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler, 486b9c1b51eSKate Stone StringList &lines) { 487f52c40c5SSean Callanan // An empty lines is used to indicate the end of input 488f52c40c5SSean Callanan const size_t num_lines = lines.GetSize(); 489b9c1b51eSKate Stone if (num_lines > 0 && lines[num_lines - 1].empty()) { 49005097246SAdrian Prantl // Remove the last empty line from "lines" so it doesn't appear in our 49105097246SAdrian Prantl // resulting input and return true to indicate we are done getting lines 49244d93782SGreg Clayton lines.PopBack(); 493f52c40c5SSean Callanan return true; 49444d93782SGreg Clayton } 495f52c40c5SSean Callanan return false; 49644d93782SGreg Clayton } 49744d93782SGreg Clayton 498b9c1b51eSKate Stone void CommandObjectExpression::GetMultilineExpression() { 49930fdc8d8SChris Lattner m_expr_lines.clear(); 50030fdc8d8SChris Lattner m_expr_line_count = 0; 50130fdc8d8SChris Lattner 50244d93782SGreg Clayton Debugger &debugger = GetCommandInterpreter().GetDebugger(); 503e30f11d9SKate Stone bool color_prompt = debugger.GetUseColor(); 50444d93782SGreg Clayton const bool multiple_lines = true; // Get multiple lines 505b9c1b51eSKate Stone IOHandlerSP io_handler_sp( 506b9c1b51eSKate Stone new IOHandlerEditline(debugger, IOHandler::Type::Expression, 50744d93782SGreg Clayton "lldb-expr", // Name of input reader for history 508514d8cd8SZachary Turner llvm::StringRef(), // No prompt 509514d8cd8SZachary Turner llvm::StringRef(), // Continuation prompt 510b9c1b51eSKate Stone multiple_lines, color_prompt, 511f6913cd7SGreg Clayton 1, // Show line numbers starting at 1 512d77c2e09SJonas Devlieghere *this, nullptr)); 513b6892508SGreg Clayton 5147ca15ba7SLawrence D'Anna StreamFileSP output_sp = io_handler_sp->GetOutputStreamFileSP(); 515b9c1b51eSKate Stone if (output_sp) { 516b9c1b51eSKate Stone output_sp->PutCString( 517b9c1b51eSKate Stone "Enter expressions, then terminate with an empty line to evaluate:\n"); 518b6892508SGreg Clayton output_sp->Flush(); 519b6892508SGreg Clayton } 5207ce2de2cSJonas Devlieghere debugger.RunIOHandlerAsync(io_handler_sp); 521cf28a8b7SGreg Clayton } 522cf28a8b7SGreg Clayton 523c5bfa3daSJonas Devlieghere static EvaluateExpressionOptions 524c5bfa3daSJonas Devlieghere GetExprOptions(ExecutionContext &ctx, 525c5bfa3daSJonas Devlieghere CommandObjectExpression::CommandOptions command_options) { 526c5bfa3daSJonas Devlieghere command_options.OptionParsingStarting(&ctx); 527c5bfa3daSJonas Devlieghere 528c5bfa3daSJonas Devlieghere // Default certain settings for REPL regardless of the global settings. 529c5bfa3daSJonas Devlieghere command_options.unwind_on_error = false; 530c5bfa3daSJonas Devlieghere command_options.ignore_breakpoints = false; 531c5bfa3daSJonas Devlieghere command_options.debug = false; 532c5bfa3daSJonas Devlieghere 533c5bfa3daSJonas Devlieghere EvaluateExpressionOptions expr_options; 534c5bfa3daSJonas Devlieghere expr_options.SetUnwindOnError(command_options.unwind_on_error); 535c5bfa3daSJonas Devlieghere expr_options.SetIgnoreBreakpoints(command_options.ignore_breakpoints); 536c5bfa3daSJonas Devlieghere expr_options.SetTryAllThreads(command_options.try_all_threads); 537c5bfa3daSJonas Devlieghere 538c5bfa3daSJonas Devlieghere if (command_options.timeout > 0) 539c5bfa3daSJonas Devlieghere expr_options.SetTimeout(std::chrono::microseconds(command_options.timeout)); 540c5bfa3daSJonas Devlieghere else 541c5bfa3daSJonas Devlieghere expr_options.SetTimeout(llvm::None); 542c5bfa3daSJonas Devlieghere 543c5bfa3daSJonas Devlieghere return expr_options; 544c5bfa3daSJonas Devlieghere } 545c5bfa3daSJonas Devlieghere 5464d51a902SRaphael Isemann bool CommandObjectExpression::DoExecute(llvm::StringRef command, 547b9c1b51eSKate Stone CommandReturnObject &result) { 548e5ee6f04SJim Ingham m_fixed_expression.clear(); 549e1cfbc79STodd Fiala auto exe_ctx = GetCommandInterpreter().GetExecutionContext(); 550e1cfbc79STodd Fiala m_option_group.NotifyOptionParsingStarting(&exe_ctx); 551cf28a8b7SGreg Clayton 5524d51a902SRaphael Isemann if (command.empty()) { 553cf28a8b7SGreg Clayton GetMultilineExpression(); 55430fdc8d8SChris Lattner return result.Succeeded(); 55530fdc8d8SChris Lattner } 55630fdc8d8SChris Lattner 5573a0e1270SRaphael Isemann OptionsWithRaw args(command); 5584d51a902SRaphael Isemann llvm::StringRef expr = args.GetRawPart(); 55930fdc8d8SChris Lattner 5603a0e1270SRaphael Isemann if (args.HasArgs()) { 5613a0e1270SRaphael Isemann if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group, exe_ctx)) 56230fdc8d8SChris Lattner return false; 563f6b8b581SGreg Clayton 564b9c1b51eSKate Stone if (m_repl_option.GetOptionValue().GetCurrentValue()) { 5655a1bac4dSRaphael Isemann Target &target = GetSelectedOrDummyTarget(); 566f2bd5c3eSSean Callanan // Drop into REPL 567f2bd5c3eSSean Callanan m_expr_lines.clear(); 568f2bd5c3eSSean Callanan m_expr_line_count = 0; 569f2bd5c3eSSean Callanan 5705a1bac4dSRaphael Isemann Debugger &debugger = target.GetDebugger(); 571f2bd5c3eSSean Callanan 572b9c1b51eSKate Stone // Check if the LLDB command interpreter is sitting on top of a REPL 57305097246SAdrian Prantl // that launched it... 5743a0e1270SRaphael Isemann if (debugger.CheckTopIOHandlerTypes(IOHandler::Type::CommandInterpreter, 5753a0e1270SRaphael Isemann IOHandler::Type::REPL)) { 576b9c1b51eSKate Stone // the LLDB command interpreter is sitting on top of a REPL that 57705097246SAdrian Prantl // launched it, so just say the command interpreter is done and 57805097246SAdrian Prantl // fall back to the existing REPL 579f2bd5c3eSSean Callanan m_interpreter.GetIOHandler(false)->SetIsDone(true); 580b9c1b51eSKate Stone } else { 581b9c1b51eSKate Stone // We are launching the REPL on top of the current LLDB command 58205097246SAdrian Prantl // interpreter, so just push one 583f2bd5c3eSSean Callanan bool initialize = false; 58497206d57SZachary Turner Status repl_error; 5855a1bac4dSRaphael Isemann REPLSP repl_sp(target.GetREPL(repl_error, m_command_options.language, 5863a0e1270SRaphael Isemann nullptr, false)); 587f2bd5c3eSSean Callanan 588b9c1b51eSKate Stone if (!repl_sp) { 589f2bd5c3eSSean Callanan initialize = true; 5905a1bac4dSRaphael Isemann repl_sp = target.GetREPL(repl_error, m_command_options.language, 591b9c1b51eSKate Stone nullptr, true); 592b9c1b51eSKate Stone if (!repl_error.Success()) { 593f2bd5c3eSSean Callanan result.SetError(repl_error); 594f2bd5c3eSSean Callanan return result.Succeeded(); 595f2bd5c3eSSean Callanan } 596f2bd5c3eSSean Callanan } 597f2bd5c3eSSean Callanan 598b9c1b51eSKate Stone if (repl_sp) { 599b9c1b51eSKate Stone if (initialize) { 600c5bfa3daSJonas Devlieghere repl_sp->SetEvaluateOptions( 601c5bfa3daSJonas Devlieghere GetExprOptions(exe_ctx, m_command_options)); 602f2bd5c3eSSean Callanan repl_sp->SetFormatOptions(m_format_options); 603f2bd5c3eSSean Callanan repl_sp->SetValueObjectDisplayOptions(m_varobj_options); 604f2bd5c3eSSean Callanan } 605f2bd5c3eSSean Callanan 606f2bd5c3eSSean Callanan IOHandlerSP io_handler_sp(repl_sp->GetIOHandler()); 607f2bd5c3eSSean Callanan io_handler_sp->SetIsDone(false); 6087ce2de2cSJonas Devlieghere debugger.RunIOHandlerAsync(io_handler_sp); 609b9c1b51eSKate Stone } else { 610b9c1b51eSKate Stone repl_error.SetErrorStringWithFormat( 611b9c1b51eSKate Stone "Couldn't create a REPL for %s", 612b9c1b51eSKate Stone Language::GetNameForLanguageType(m_command_options.language)); 613f2bd5c3eSSean Callanan result.SetError(repl_error); 614f2bd5c3eSSean Callanan return result.Succeeded(); 615f2bd5c3eSSean Callanan } 616f2bd5c3eSSean Callanan } 617f2bd5c3eSSean Callanan } 618cf28a8b7SGreg Clayton // No expression following options 6194d51a902SRaphael Isemann else if (expr.empty()) { 620cf28a8b7SGreg Clayton GetMultilineExpression(); 621cf28a8b7SGreg Clayton return result.Succeeded(); 622cf28a8b7SGreg Clayton } 62330fdc8d8SChris Lattner } 62430fdc8d8SChris Lattner 625cb2380c9SRaphael Isemann Target &target = GetSelectedOrDummyTarget(); 6267c6e52acSRaphael Isemann if (EvaluateExpression(expr, result.GetOutputStream(), 6277c6e52acSRaphael Isemann result.GetErrorStream(), result)) { 62824fff242SDavide Italiano 629cb2380c9SRaphael Isemann if (!m_fixed_expression.empty() && target.GetEnableNotifyAboutFixIts()) { 630e5ee6f04SJim Ingham CommandHistory &history = m_interpreter.GetCommandHistory(); 631b9c1b51eSKate Stone // FIXME: Can we figure out what the user actually typed (e.g. some alias 632b9c1b51eSKate Stone // for expr???) 633e5ee6f04SJim Ingham // If we can it would be nice to show that. 634e5ee6f04SJim Ingham std::string fixed_command("expression "); 6353a0e1270SRaphael Isemann if (args.HasArgs()) { 636e5ee6f04SJim Ingham // Add in any options that might have been in the original command: 637adcd0268SBenjamin Kramer fixed_command.append(std::string(args.GetArgStringWithDelimiter())); 638e5ee6f04SJim Ingham fixed_command.append(m_fixed_expression); 6393a0e1270SRaphael Isemann } else 6403a0e1270SRaphael Isemann fixed_command.append(m_fixed_expression); 641e5ee6f04SJim Ingham history.AppendString(fixed_command); 642e5ee6f04SJim Ingham } 643fcd43b71SJohnny Chen return true; 644e5ee6f04SJim Ingham } 645fcd43b71SJohnny Chen result.SetStatus(eReturnStatusFailed); 646fcd43b71SJohnny Chen return false; 64730fdc8d8SChris Lattner } 648