130fdc8d8SChris Lattner //===-- CommandObjectExpression.cpp -----------------------------*- C++ -*-===// 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/STLExtras.h" 10c8ecc2a9SEugene Zelenko #include "llvm/ADT/StringRef.h" 11c8ecc2a9SEugene Zelenko 12c8ecc2a9SEugene Zelenko #include "CommandObjectExpression.h" 13b9c1b51eSKate Stone #include "lldb/Core/Debugger.h" 1430fdc8d8SChris Lattner #include "lldb/Core/Value.h" 156c68fb45SJim Ingham #include "lldb/Core/ValueObjectVariable.h" 164d93b8cdSEnrico Granata #include "lldb/DataFormatters/ValueObjectPrinter.h" 1730fdc8d8SChris Lattner #include "lldb/Expression/DWARFExpression.h" 18f2bd5c3eSSean Callanan #include "lldb/Expression/REPL.h" 19b9c1b51eSKate Stone #include "lldb/Expression/UserExpression.h" 2030fdc8d8SChris Lattner #include "lldb/Host/Host.h" 213eb2b44dSZachary Turner #include "lldb/Host/OptionParser.h" 226611103cSGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h" 2330fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h" 2447cbf4a0SPavel Labath #include "lldb/Interpreter/OptionArgParser.h" 2530fdc8d8SChris Lattner #include "lldb/Symbol/ObjectFile.h" 2630fdc8d8SChris Lattner #include "lldb/Symbol/Variable.h" 27b9c1b51eSKate Stone #include "lldb/Target/Language.h" 2830fdc8d8SChris Lattner #include "lldb/Target/Process.h" 29b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h" 3030fdc8d8SChris Lattner #include "lldb/Target/Target.h" 317260f620SGreg Clayton #include "lldb/Target/Thread.h" 3230fdc8d8SChris Lattner 3330fdc8d8SChris Lattner using namespace lldb; 3430fdc8d8SChris Lattner using namespace lldb_private; 3530fdc8d8SChris Lattner 36b9c1b51eSKate Stone CommandObjectExpression::CommandOptions::CommandOptions() : OptionGroup() {} 3730fdc8d8SChris Lattner 38c8ecc2a9SEugene Zelenko CommandObjectExpression::CommandOptions::~CommandOptions() = default; 3930fdc8d8SChris Lattner 408fe53c49STatyana Krasnukha static constexpr OptionEnumValueElement g_description_verbosity_type[] = { 41e063ecccSJonas Devlieghere { 42e063ecccSJonas Devlieghere eLanguageRuntimeDescriptionDisplayVerbosityCompact, 43e063ecccSJonas Devlieghere "compact", 44e063ecccSJonas Devlieghere "Only show the description string", 45e063ecccSJonas Devlieghere }, 46e063ecccSJonas Devlieghere { 47e063ecccSJonas Devlieghere eLanguageRuntimeDescriptionDisplayVerbosityFull, 48e063ecccSJonas Devlieghere "full", 49e063ecccSJonas Devlieghere "Show the full output, including persistent variable's name and type", 50e063ecccSJonas Devlieghere }, 51e063ecccSJonas Devlieghere }; 524d93b8cdSEnrico Granata 538fe53c49STatyana Krasnukha static constexpr OptionEnumValues DescriptionVerbosityTypes() { 548fe53c49STatyana Krasnukha return OptionEnumValues(g_description_verbosity_type); 558fe53c49STatyana Krasnukha } 568fe53c49STatyana Krasnukha 57ec67e734SRaphael Isemann #define LLDB_OPTIONS_expression 58ec67e734SRaphael Isemann #include "CommandOptions.inc" 591deb7962SGreg Clayton 6097206d57SZachary Turner Status CommandObjectExpression::CommandOptions::SetOptionValue( 618cef4b0bSZachary Turner uint32_t option_idx, llvm::StringRef option_arg, 62b9c1b51eSKate Stone ExecutionContext *execution_context) { 6397206d57SZachary Turner Status error; 6430fdc8d8SChris Lattner 651f0f5b5bSZachary Turner const int short_option = GetDefinitions()[option_idx].short_option; 6630fdc8d8SChris Lattner 67b9c1b51eSKate Stone switch (short_option) { 6815663c53SDawn Perchik case 'l': 690e0984eeSJim Ingham language = Language::GetLanguageTypeFromString(option_arg); 7015663c53SDawn Perchik if (language == eLanguageTypeUnknown) 71b9c1b51eSKate Stone error.SetErrorStringWithFormat( 728cef4b0bSZachary Turner "unknown language type: '%s' for expression", 738cef4b0bSZachary Turner option_arg.str().c_str()); 7415663c53SDawn Perchik break; 7530fdc8d8SChris Lattner 76b9c1b51eSKate Stone case 'a': { 7735e1bda6SJim Ingham bool success; 7835e1bda6SJim Ingham bool result; 7947cbf4a0SPavel Labath result = OptionArgParser::ToBoolean(option_arg, true, &success); 8035e1bda6SJim Ingham if (!success) 81b9c1b51eSKate Stone error.SetErrorStringWithFormat( 828cef4b0bSZachary Turner "invalid all-threads value setting: \"%s\"", 838cef4b0bSZachary Turner option_arg.str().c_str()); 8435e1bda6SJim Ingham else 8535e1bda6SJim Ingham try_all_threads = result; 86b9c1b51eSKate Stone } break; 876c68fb45SJim Ingham 88b9c1b51eSKate Stone case 'i': { 89184e9811SJim Ingham bool success; 9047cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 91184e9811SJim Ingham if (success) 92184e9811SJim Ingham ignore_breakpoints = tmp_value; 93184e9811SJim Ingham else 94b9c1b51eSKate Stone error.SetErrorStringWithFormat( 958cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 968cef4b0bSZachary Turner option_arg.str().c_str()); 97184e9811SJim Ingham break; 98184e9811SJim Ingham } 993fe71581SMarianne Mailhot-Sarrasin 100b9c1b51eSKate Stone case 'j': { 1013fe71581SMarianne Mailhot-Sarrasin bool success; 10247cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 1033fe71581SMarianne Mailhot-Sarrasin if (success) 1043fe71581SMarianne Mailhot-Sarrasin allow_jit = tmp_value; 1053fe71581SMarianne Mailhot-Sarrasin else 106b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1078cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 1088cef4b0bSZachary Turner option_arg.str().c_str()); 1093fe71581SMarianne Mailhot-Sarrasin break; 1103fe71581SMarianne Mailhot-Sarrasin } 1113fe71581SMarianne Mailhot-Sarrasin 1128cef4b0bSZachary Turner case 't': 1138cef4b0bSZachary Turner if (option_arg.getAsInteger(0, timeout)) { 1148cef4b0bSZachary Turner timeout = 0; 115b9c1b51eSKate Stone error.SetErrorStringWithFormat("invalid timeout setting \"%s\"", 1168cef4b0bSZachary Turner option_arg.str().c_str()); 1178cef4b0bSZachary Turner } 1188cef4b0bSZachary Turner break; 11935e1bda6SJim Ingham 120b9c1b51eSKate Stone case 'u': { 121399f1cafSJim Ingham bool success; 12247cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 123184e9811SJim Ingham if (success) 124184e9811SJim Ingham unwind_on_error = tmp_value; 125184e9811SJim Ingham else 126b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1278cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 1288cef4b0bSZachary Turner option_arg.str().c_str()); 129399f1cafSJim Ingham break; 1303bfdaa2aSSean Callanan } 1314d93b8cdSEnrico Granata 1324d93b8cdSEnrico Granata case 'v': 133543a26e9SEnrico Granata if (option_arg.empty()) { 1344d93b8cdSEnrico Granata m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull; 1354d93b8cdSEnrico Granata break; 1364d93b8cdSEnrico Granata } 13747cbf4a0SPavel Labath m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) 13847cbf4a0SPavel Labath OptionArgParser::ToOptionEnum( 1391f0f5b5bSZachary Turner option_arg, GetDefinitions()[option_idx].enum_values, 0, error); 1404d93b8cdSEnrico Granata if (!error.Success()) 141b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1428cef4b0bSZachary Turner "unrecognized value for description-verbosity '%s'", 1438cef4b0bSZachary Turner option_arg.str().c_str()); 1444d93b8cdSEnrico Granata break; 1454d93b8cdSEnrico Granata 14662afb9f6SGreg Clayton case 'g': 14762afb9f6SGreg Clayton debug = true; 14862afb9f6SGreg Clayton unwind_on_error = false; 14962afb9f6SGreg Clayton ignore_breakpoints = false; 15062afb9f6SGreg Clayton break; 15162afb9f6SGreg Clayton 152863fab69SSean Callanan case 'p': 153863fab69SSean Callanan top_level = true; 154863fab69SSean Callanan break; 155863fab69SSean Callanan 156b9c1b51eSKate Stone case 'X': { 157a1e541bfSJim Ingham bool success; 15847cbf4a0SPavel Labath bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); 159a1e541bfSJim Ingham if (success) 160a1e541bfSJim Ingham auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo; 161a1e541bfSJim Ingham else 162b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1638cef4b0bSZachary Turner "could not convert \"%s\" to a boolean value.", 1648cef4b0bSZachary Turner option_arg.str().c_str()); 165a1e541bfSJim Ingham break; 166a1e541bfSJim Ingham } 167a1e541bfSJim Ingham 16830fdc8d8SChris Lattner default: 169*36162014SRaphael Isemann llvm_unreachable("Unimplemented option"); 17030fdc8d8SChris Lattner } 17130fdc8d8SChris Lattner 17230fdc8d8SChris Lattner return error; 17330fdc8d8SChris Lattner } 17430fdc8d8SChris Lattner 175b9c1b51eSKate Stone void CommandObjectExpression::CommandOptions::OptionParsingStarting( 176b9c1b51eSKate Stone ExecutionContext *execution_context) { 177e1cfbc79STodd Fiala auto process_sp = 178e1cfbc79STodd Fiala execution_context ? execution_context->GetProcessSP() : ProcessSP(); 179b9c1b51eSKate Stone if (process_sp) { 180e1cfbc79STodd Fiala ignore_breakpoints = process_sp->GetIgnoreBreakpointsInExpressions(); 181e1cfbc79STodd Fiala unwind_on_error = process_sp->GetUnwindOnErrorInExpressions(); 182b9c1b51eSKate Stone } else { 183fc03f8fcSGreg Clayton ignore_breakpoints = true; 184399f1cafSJim Ingham unwind_on_error = true; 185184e9811SJim Ingham } 186184e9811SJim Ingham 18730fdc8d8SChris Lattner show_summary = true; 18835e1bda6SJim Ingham try_all_threads = true; 18935e1bda6SJim Ingham timeout = 0; 19062afb9f6SGreg Clayton debug = false; 19115663c53SDawn Perchik language = eLanguageTypeUnknown; 1924d93b8cdSEnrico Granata m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact; 193a1e541bfSJim Ingham auto_apply_fixits = eLazyBoolCalculate; 194863fab69SSean Callanan top_level = false; 1953fe71581SMarianne Mailhot-Sarrasin allow_jit = true; 19630fdc8d8SChris Lattner } 19730fdc8d8SChris Lattner 1981f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> 199b9c1b51eSKate Stone CommandObjectExpression::CommandOptions::GetDefinitions() { 20070602439SZachary Turner return llvm::makeArrayRef(g_expression_options); 20130fdc8d8SChris Lattner } 20230fdc8d8SChris Lattner 203b9c1b51eSKate Stone CommandObjectExpression::CommandObjectExpression( 204b9c1b51eSKate Stone CommandInterpreter &interpreter) 2057428a18cSKate Stone : CommandObjectRaw( 206b9c1b51eSKate Stone interpreter, "expression", "Evaluate an expression on the current " 207b9c1b51eSKate Stone "thread. Displays any returned value " 208b9c1b51eSKate Stone "with LLDB's default formatting.", 209a449698cSZachary Turner "", eCommandProcessMustBePaused | eCommandTryTargetAPILock), 21044d93782SGreg Clayton IOHandlerDelegate(IOHandlerDelegate::Completion::Expression), 211b9c1b51eSKate Stone m_option_group(), m_format_options(eFormatDefault), 212b9c1b51eSKate Stone m_repl_option(LLDB_OPT_SET_1, false, "repl", 'r', "Drop into REPL", false, 213b9c1b51eSKate Stone true), 214b9c1b51eSKate Stone m_command_options(), m_expr_line_count(0), m_expr_lines() { 21530fdc8d8SChris Lattner SetHelpLong( 216ea671fbdSKate Stone R"( 217d0309916SJim Ingham Single and multi-line expressions: 218d0309916SJim Ingham 219d0309916SJim Ingham )" 220d0309916SJim Ingham " The expression provided on the command line must be a complete expression \ 221d0309916SJim Ingham with no newlines. To evaluate a multi-line expression, \ 222d0309916SJim Ingham hit a return after an empty expression, and lldb will enter the multi-line expression editor. \ 223d0309916SJim Ingham Hit return on an empty line to end the multi-line expression." 224d0309916SJim Ingham 225d0309916SJim Ingham R"( 226d0309916SJim Ingham 227ea671fbdSKate Stone Timeouts: 228ea671fbdSKate Stone 229b9c1b51eSKate Stone )" 230b9c1b51eSKate Stone " If the expression can be evaluated statically (without running code) then it will be. \ 231ea671fbdSKate Stone Otherwise, by default the expression will run on the current thread with a short timeout: \ 232ea671fbdSKate Stone currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \ 233ea671fbdSKate Stone and resumed with all threads running. You can use the -a option to disable retrying on all \ 234b9c1b51eSKate Stone threads. You can use the -t option to set a shorter timeout." 235b9c1b51eSKate Stone R"( 236ea671fbdSKate Stone 237ea671fbdSKate Stone User defined variables: 238ea671fbdSKate Stone 239b9c1b51eSKate Stone )" 240b9c1b51eSKate Stone " You can define your own variables for convenience or to be used in subsequent expressions. \ 241ea671fbdSKate Stone You define them the same way you would define variables in C. If the first character of \ 242ea671fbdSKate Stone your user defined variable is a $, then the variable's value will be available in future \ 243b9c1b51eSKate Stone expressions, otherwise it will just be available in the current expression." 244b9c1b51eSKate Stone R"( 245ea671fbdSKate Stone 246ea671fbdSKate Stone Continuing evaluation after a breakpoint: 247ea671fbdSKate Stone 248b9c1b51eSKate Stone )" 249b9c1b51eSKate Stone " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \ 250ea671fbdSKate Stone you are done with your investigation, you can either remove the expression execution frames \ 251ea671fbdSKate Stone from the stack with \"thread return -x\" or if you are still interested in the expression result \ 252ea671fbdSKate Stone you can issue the \"continue\" command and the expression evaluation will complete and the \ 253ea671fbdSKate Stone expression result will be available using the \"thread.completed-expression\" key in the thread \ 254b9c1b51eSKate Stone format." 255d0309916SJim Ingham 256b9c1b51eSKate Stone R"( 257ea671fbdSKate Stone 258ea671fbdSKate Stone Examples: 259ea671fbdSKate Stone 260ea671fbdSKate Stone expr my_struct->a = my_array[3] 261ea671fbdSKate Stone expr -f bin -- (index * 8) + 5 262ea671fbdSKate Stone expr unsigned int $foo = 5 263b9c1b51eSKate Stone expr char c[] = \"foo\"; c[0])"); 264405fe67fSCaroline Tice 265405fe67fSCaroline Tice CommandArgumentEntry arg; 266405fe67fSCaroline Tice CommandArgumentData expression_arg; 267405fe67fSCaroline Tice 268405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 269405fe67fSCaroline Tice expression_arg.arg_type = eArgTypeExpression; 270405fe67fSCaroline Tice expression_arg.arg_repetition = eArgRepeatPlain; 271405fe67fSCaroline Tice 272b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the argument 273b9c1b51eSKate Stone // entry. 274405fe67fSCaroline Tice arg.push_back(expression_arg); 275405fe67fSCaroline Tice 276405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 277405fe67fSCaroline Tice m_arguments.push_back(arg); 2781deb7962SGreg Clayton 2795009f9d5SGreg Clayton // Add the "--format" and "--gdb-format" 280b9c1b51eSKate Stone m_option_group.Append(&m_format_options, 281b9c1b51eSKate Stone OptionGroupFormat::OPTION_GROUP_FORMAT | 282b9c1b51eSKate Stone OptionGroupFormat::OPTION_GROUP_GDB_FMT, 283b9c1b51eSKate Stone LLDB_OPT_SET_1); 2841deb7962SGreg Clayton m_option_group.Append(&m_command_options); 285b9c1b51eSKate Stone m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, 286b9c1b51eSKate Stone LLDB_OPT_SET_1 | LLDB_OPT_SET_2); 287f2bd5c3eSSean Callanan m_option_group.Append(&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3); 2881deb7962SGreg Clayton m_option_group.Finalize(); 28930fdc8d8SChris Lattner } 29030fdc8d8SChris Lattner 291c8ecc2a9SEugene Zelenko CommandObjectExpression::~CommandObjectExpression() = default; 29230fdc8d8SChris Lattner 293b9c1b51eSKate Stone Options *CommandObjectExpression::GetOptions() { return &m_option_group; } 29430fdc8d8SChris Lattner 295ae34ed2cSRaphael Isemann void CommandObjectExpression::HandleCompletion(CompletionRequest &request) { 29674829734SRaphael Isemann EvaluateExpressionOptions options; 29774829734SRaphael Isemann options.SetCoerceToId(m_varobj_options.use_objc); 29874829734SRaphael Isemann options.SetLanguage(m_command_options.language); 29974829734SRaphael Isemann options.SetExecutionPolicy(lldb_private::eExecutionPolicyNever); 30074829734SRaphael Isemann options.SetAutoApplyFixIts(false); 30174829734SRaphael Isemann options.SetGenerateDebugInfo(false); 30274829734SRaphael Isemann 30374829734SRaphael Isemann // We need a valid execution context with a frame pointer for this 30474829734SRaphael Isemann // completion, so if we don't have one we should try to make a valid 30574829734SRaphael Isemann // execution context. 30674829734SRaphael Isemann if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr) 30774829734SRaphael Isemann m_interpreter.UpdateExecutionContext(nullptr); 30874829734SRaphael Isemann 30974829734SRaphael Isemann // This didn't work, so let's get out before we start doing things that 31074829734SRaphael Isemann // expect a valid frame pointer. 31174829734SRaphael Isemann if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr) 312ae34ed2cSRaphael Isemann return; 31374829734SRaphael Isemann 31474829734SRaphael Isemann ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 31574829734SRaphael Isemann 31674829734SRaphael Isemann Target *target = exe_ctx.GetTargetPtr(); 31774829734SRaphael Isemann 31874829734SRaphael Isemann if (!target) 31974829734SRaphael Isemann target = GetDummyTarget(); 32074829734SRaphael Isemann 32174829734SRaphael Isemann if (!target) 322ae34ed2cSRaphael Isemann return; 32374829734SRaphael Isemann 32474829734SRaphael Isemann unsigned cursor_pos = request.GetRawCursorPos(); 32574829734SRaphael Isemann llvm::StringRef code = request.GetRawLine(); 32674829734SRaphael Isemann 32774829734SRaphael Isemann const std::size_t original_code_size = code.size(); 32874829734SRaphael Isemann 32974829734SRaphael Isemann // Remove the first token which is 'expr' or some alias/abbreviation of that. 33074829734SRaphael Isemann code = llvm::getToken(code).second.ltrim(); 33174829734SRaphael Isemann OptionsWithRaw args(code); 33274829734SRaphael Isemann code = args.GetRawPart(); 33374829734SRaphael Isemann 33474829734SRaphael Isemann // The position where the expression starts in the command line. 33574829734SRaphael Isemann assert(original_code_size >= code.size()); 33674829734SRaphael Isemann std::size_t raw_start = original_code_size - code.size(); 33774829734SRaphael Isemann 33874829734SRaphael Isemann // Check if the cursor is actually in the expression string, and if not, we 33974829734SRaphael Isemann // exit. 34074829734SRaphael Isemann // FIXME: We should complete the options here. 34174829734SRaphael Isemann if (cursor_pos < raw_start) 342ae34ed2cSRaphael Isemann return; 34374829734SRaphael Isemann 34474829734SRaphael Isemann // Make the cursor_pos again relative to the start of the code string. 34574829734SRaphael Isemann assert(cursor_pos >= raw_start); 34674829734SRaphael Isemann cursor_pos -= raw_start; 34774829734SRaphael Isemann 34874829734SRaphael Isemann auto language = exe_ctx.GetFrameRef().GetLanguage(); 34974829734SRaphael Isemann 35074829734SRaphael Isemann Status error; 35174829734SRaphael Isemann lldb::UserExpressionSP expr(target->GetUserExpressionForLanguage( 35274829734SRaphael Isemann code, llvm::StringRef(), language, UserExpression::eResultTypeAny, 35340624a08SAleksandr Urakov options, nullptr, error)); 35474829734SRaphael Isemann if (error.Fail()) 355ae34ed2cSRaphael Isemann return; 35674829734SRaphael Isemann 357c11a780eSRaphael Isemann expr->Complete(exe_ctx, request, cursor_pos); 35874829734SRaphael Isemann } 35974829734SRaphael Isemann 36097206d57SZachary Turner static lldb_private::Status 361b9c1b51eSKate Stone CanBeUsedForElementCountPrinting(ValueObject &valobj) { 362520a422bSEnrico Granata CompilerType type(valobj.GetCompilerType()); 363520a422bSEnrico Granata CompilerType pointee; 364520a422bSEnrico Granata if (!type.IsPointerType(&pointee)) 36597206d57SZachary Turner return Status("as it does not refer to a pointer"); 366520a422bSEnrico Granata if (pointee.IsVoidType()) 36797206d57SZachary Turner return Status("as it refers to a pointer to void"); 36897206d57SZachary Turner return Status(); 369520a422bSEnrico Granata } 370520a422bSEnrico Granata 3714d51a902SRaphael Isemann bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr, 3726e8dc334SCaroline Tice Stream *output_stream, 3736e8dc334SCaroline Tice Stream *error_stream, 374b9c1b51eSKate Stone CommandReturnObject *result) { 37505097246SAdrian Prantl // Don't use m_exe_ctx as this might be called asynchronously after the 37605097246SAdrian Prantl // command object DoExecute has finished when doing multi-line expression 37705097246SAdrian Prantl // that use an input reader... 378ba7b8e2cSGreg Clayton ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 379ba7b8e2cSGreg Clayton 380ba7b8e2cSGreg Clayton Target *target = exe_ctx.GetTargetPtr(); 381c0a6e061SSean Callanan 382c0a6e061SSean Callanan if (!target) 383893c932aSJim Ingham target = GetDummyTarget(); 384c0a6e061SSean Callanan 385b9c1b51eSKate Stone if (target) { 3868b2fe6dcSGreg Clayton lldb::ValueObjectSP result_valobj_sp; 38792adcac9SSean Callanan bool keep_in_memory = true; 388009d110dSDawn Perchik StackFrame *frame = exe_ctx.GetFramePtr(); 38992adcac9SSean Callanan 39035e1bda6SJim Ingham EvaluateExpressionOptions options; 3916fbc48bcSJim Ingham options.SetCoerceToId(m_varobj_options.use_objc); 3926fbc48bcSJim Ingham options.SetUnwindOnError(m_command_options.unwind_on_error); 3936fbc48bcSJim Ingham options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints); 3946fbc48bcSJim Ingham options.SetKeepInMemory(keep_in_memory); 3956fbc48bcSJim Ingham options.SetUseDynamic(m_varobj_options.use_dynamic); 3966fbc48bcSJim Ingham options.SetTryAllThreads(m_command_options.try_all_threads); 3976fbc48bcSJim Ingham options.SetDebug(m_command_options.debug); 39815663c53SDawn Perchik options.SetLanguage(m_command_options.language); 399b9c1b51eSKate Stone options.SetExecutionPolicy( 400b9c1b51eSKate Stone m_command_options.allow_jit 401b9c1b51eSKate Stone ? EvaluateExpressionOptions::default_execution_policy 402b9c1b51eSKate Stone : lldb_private::eExecutionPolicyNever); 40315663c53SDawn Perchik 404a1e541bfSJim Ingham bool auto_apply_fixits; 405a1e541bfSJim Ingham if (m_command_options.auto_apply_fixits == eLazyBoolCalculate) 406a1e541bfSJim Ingham auto_apply_fixits = target->GetEnableAutoApplyFixIts(); 407a1e541bfSJim Ingham else 408a6682a41SJonas Devlieghere auto_apply_fixits = m_command_options.auto_apply_fixits == eLazyBoolYes; 409a1e541bfSJim Ingham 410a1e541bfSJim Ingham options.SetAutoApplyFixIts(auto_apply_fixits); 411a1e541bfSJim Ingham 412863fab69SSean Callanan if (m_command_options.top_level) 413863fab69SSean Callanan options.SetExecutionPolicy(eExecutionPolicyTopLevel); 414863fab69SSean Callanan 41505097246SAdrian Prantl // If there is any chance we are going to stop and want to see what went 41605097246SAdrian Prantl // wrong with our expression, we should generate debug info 41723f8c95aSGreg Clayton if (!m_command_options.ignore_breakpoints || 41823f8c95aSGreg Clayton !m_command_options.unwind_on_error) 41923f8c95aSGreg Clayton options.SetGenerateDebugInfo(true); 42023f8c95aSGreg Clayton 42162afb9f6SGreg Clayton if (m_command_options.timeout > 0) 42243d35418SPavel Labath options.SetTimeout(std::chrono::microseconds(m_command_options.timeout)); 4236f78f386SJim Ingham else 42443d35418SPavel Labath options.SetTimeout(llvm::None); 425d4439aa9SEnrico Granata 426b9c1b51eSKate Stone ExpressionResults success = target->EvaluateExpression( 427b9c1b51eSKate Stone expr, frame, result_valobj_sp, options, &m_fixed_expression); 428e5ee6f04SJim Ingham 429b9c1b51eSKate Stone // We only tell you about the FixIt if we applied it. The compiler errors 430b9c1b51eSKate Stone // will suggest the FixIt if it parsed. 431b9c1b51eSKate Stone if (error_stream && !m_fixed_expression.empty() && 432b9c1b51eSKate Stone target->GetEnableNotifyAboutFixIts()) { 433e5ee6f04SJim Ingham if (success == eExpressionCompleted) 434b9c1b51eSKate Stone error_stream->Printf( 435b9c1b51eSKate Stone " Fix-it applied, fixed expression was: \n %s\n", 436b9c1b51eSKate Stone m_fixed_expression.c_str()); 437e5ee6f04SJim Ingham } 4388b2fe6dcSGreg Clayton 439b9c1b51eSKate Stone if (result_valobj_sp) { 440bf154daeSSean Callanan Format format = m_format_options.GetFormat(); 441bf154daeSSean Callanan 442b9c1b51eSKate Stone if (result_valobj_sp->GetError().Success()) { 443b9c1b51eSKate Stone if (format != eFormatVoid) { 4441deb7962SGreg Clayton if (format != eFormatDefault) 4451deb7962SGreg Clayton result_valobj_sp->SetFormat(format); 44632c4085bSGreg Clayton 447b9c1b51eSKate Stone if (m_varobj_options.elem_count > 0) { 44897206d57SZachary Turner Status error(CanBeUsedForElementCountPrinting(*result_valobj_sp)); 449b9c1b51eSKate Stone if (error.Fail()) { 450b9c1b51eSKate Stone result->AppendErrorWithFormat( 451b9c1b51eSKate Stone "expression cannot be used with --element-count %s\n", 452b9c1b51eSKate Stone error.AsCString("")); 453520a422bSEnrico Granata result->SetStatus(eReturnStatusFailed); 454520a422bSEnrico Granata return false; 455520a422bSEnrico Granata } 456520a422bSEnrico Granata } 457520a422bSEnrico Granata 458b9c1b51eSKate Stone DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions( 459b9c1b51eSKate Stone m_command_options.m_verbosity, format)); 460b9c1b51eSKate Stone options.SetVariableFormatDisplayLanguage( 461b9c1b51eSKate Stone result_valobj_sp->GetPreferredDisplayLanguage()); 462770eb05aSEnrico Granata 4634d93b8cdSEnrico Granata result_valobj_sp->Dump(*output_stream, options); 4644d93b8cdSEnrico Granata 465fcd43b71SJohnny Chen if (result) 466fcd43b71SJohnny Chen result->SetStatus(eReturnStatusSuccessFinishResult); 46730fdc8d8SChris Lattner } 468b9c1b51eSKate Stone } else { 469b9c1b51eSKate Stone if (result_valobj_sp->GetError().GetError() == 470a35912daSKrasimir Georgiev UserExpression::kNoResult) { 47157179860SJonas Devlieghere if (format != eFormatVoid && GetDebugger().GetNotifyVoid()) { 472bcf897faSSean Callanan error_stream->PutCString("(void)\n"); 473bcf897faSSean Callanan } 474bccce813SSean Callanan 475bccce813SSean Callanan if (result) 476bccce813SSean Callanan result->SetStatus(eReturnStatusSuccessFinishResult); 477b9c1b51eSKate Stone } else { 4785fd05903SGreg Clayton const char *error_cstr = result_valobj_sp->GetError().AsCString(); 479b9c1b51eSKate Stone if (error_cstr && error_cstr[0]) { 480c7bece56SGreg Clayton const size_t error_cstr_len = strlen(error_cstr); 481b9c1b51eSKate Stone const bool ends_with_newline = 482b9c1b51eSKate Stone error_cstr[error_cstr_len - 1] == '\n'; 4835fd05903SGreg Clayton if (strstr(error_cstr, "error:") != error_cstr) 4845fd05903SGreg Clayton error_stream->PutCString("error: "); 4855fd05903SGreg Clayton error_stream->Write(error_cstr, error_cstr_len); 4865fd05903SGreg Clayton if (!ends_with_newline) 4875fd05903SGreg Clayton error_stream->EOL(); 488b9c1b51eSKate Stone } else { 4895fd05903SGreg Clayton error_stream->PutCString("error: unknown error\n"); 4905fd05903SGreg Clayton } 4915fd05903SGreg Clayton 492fcd43b71SJohnny Chen if (result) 493b71f3844SGreg Clayton result->SetStatus(eReturnStatusFailed); 49430fdc8d8SChris Lattner } 4958b2fe6dcSGreg Clayton } 4968b2fe6dcSGreg Clayton } 497b9c1b51eSKate Stone } else { 4986e8dc334SCaroline Tice error_stream->Printf("error: invalid execution context for expression\n"); 4998b2fe6dcSGreg Clayton return false; 5008b2fe6dcSGreg Clayton } 50130fdc8d8SChris Lattner 50216ad5faeSSean Callanan return true; 50330fdc8d8SChris Lattner } 50430fdc8d8SChris Lattner 505b9c1b51eSKate Stone void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler, 506b9c1b51eSKate Stone std::string &line) { 50744d93782SGreg Clayton io_handler.SetIsDone(true); 508b9c1b51eSKate Stone // StreamSP output_stream = 509b9c1b51eSKate Stone // io_handler.GetDebugger().GetAsyncOutputStream(); 51044d93782SGreg Clayton // StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream(); 51144d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 51244d93782SGreg Clayton StreamFileSP error_sp(io_handler.GetErrorStreamFile()); 51344d93782SGreg Clayton 514b9c1b51eSKate Stone EvaluateExpression(line.c_str(), output_sp.get(), error_sp.get()); 51544d93782SGreg Clayton if (output_sp) 51644d93782SGreg Clayton output_sp->Flush(); 51744d93782SGreg Clayton if (error_sp) 51844d93782SGreg Clayton error_sp->Flush(); 51944d93782SGreg Clayton } 52044d93782SGreg Clayton 521b9c1b51eSKate Stone bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler, 522b9c1b51eSKate Stone StringList &lines) { 523f52c40c5SSean Callanan // An empty lines is used to indicate the end of input 524f52c40c5SSean Callanan const size_t num_lines = lines.GetSize(); 525b9c1b51eSKate Stone if (num_lines > 0 && lines[num_lines - 1].empty()) { 52605097246SAdrian Prantl // Remove the last empty line from "lines" so it doesn't appear in our 52705097246SAdrian Prantl // resulting input and return true to indicate we are done getting lines 52844d93782SGreg Clayton lines.PopBack(); 529f52c40c5SSean Callanan return true; 53044d93782SGreg Clayton } 531f52c40c5SSean Callanan return false; 53244d93782SGreg Clayton } 53344d93782SGreg Clayton 534b9c1b51eSKate Stone void CommandObjectExpression::GetMultilineExpression() { 53530fdc8d8SChris Lattner m_expr_lines.clear(); 53630fdc8d8SChris Lattner m_expr_line_count = 0; 53730fdc8d8SChris Lattner 53844d93782SGreg Clayton Debugger &debugger = GetCommandInterpreter().GetDebugger(); 539e30f11d9SKate Stone bool color_prompt = debugger.GetUseColor(); 54044d93782SGreg Clayton const bool multiple_lines = true; // Get multiple lines 541b9c1b51eSKate Stone IOHandlerSP io_handler_sp( 542b9c1b51eSKate Stone new IOHandlerEditline(debugger, IOHandler::Type::Expression, 54344d93782SGreg Clayton "lldb-expr", // Name of input reader for history 544514d8cd8SZachary Turner llvm::StringRef(), // No prompt 545514d8cd8SZachary Turner llvm::StringRef(), // Continuation prompt 546b9c1b51eSKate Stone multiple_lines, color_prompt, 547f6913cd7SGreg Clayton 1, // Show line numbers starting at 1 548d77c2e09SJonas Devlieghere *this, nullptr)); 549b6892508SGreg Clayton 550b6892508SGreg Clayton StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile()); 551b9c1b51eSKate Stone if (output_sp) { 552b9c1b51eSKate Stone output_sp->PutCString( 553b9c1b51eSKate Stone "Enter expressions, then terminate with an empty line to evaluate:\n"); 554b6892508SGreg Clayton output_sp->Flush(); 555b6892508SGreg Clayton } 55644d93782SGreg Clayton debugger.PushIOHandler(io_handler_sp); 557cf28a8b7SGreg Clayton } 558cf28a8b7SGreg Clayton 559c5bfa3daSJonas Devlieghere static EvaluateExpressionOptions 560c5bfa3daSJonas Devlieghere GetExprOptions(ExecutionContext &ctx, 561c5bfa3daSJonas Devlieghere CommandObjectExpression::CommandOptions command_options) { 562c5bfa3daSJonas Devlieghere command_options.OptionParsingStarting(&ctx); 563c5bfa3daSJonas Devlieghere 564c5bfa3daSJonas Devlieghere // Default certain settings for REPL regardless of the global settings. 565c5bfa3daSJonas Devlieghere command_options.unwind_on_error = false; 566c5bfa3daSJonas Devlieghere command_options.ignore_breakpoints = false; 567c5bfa3daSJonas Devlieghere command_options.debug = false; 568c5bfa3daSJonas Devlieghere 569c5bfa3daSJonas Devlieghere EvaluateExpressionOptions expr_options; 570c5bfa3daSJonas Devlieghere expr_options.SetUnwindOnError(command_options.unwind_on_error); 571c5bfa3daSJonas Devlieghere expr_options.SetIgnoreBreakpoints(command_options.ignore_breakpoints); 572c5bfa3daSJonas Devlieghere expr_options.SetTryAllThreads(command_options.try_all_threads); 573c5bfa3daSJonas Devlieghere 574c5bfa3daSJonas Devlieghere if (command_options.timeout > 0) 575c5bfa3daSJonas Devlieghere expr_options.SetTimeout(std::chrono::microseconds(command_options.timeout)); 576c5bfa3daSJonas Devlieghere else 577c5bfa3daSJonas Devlieghere expr_options.SetTimeout(llvm::None); 578c5bfa3daSJonas Devlieghere 579c5bfa3daSJonas Devlieghere return expr_options; 580c5bfa3daSJonas Devlieghere } 581c5bfa3daSJonas Devlieghere 5824d51a902SRaphael Isemann bool CommandObjectExpression::DoExecute(llvm::StringRef command, 583b9c1b51eSKate Stone CommandReturnObject &result) { 584e5ee6f04SJim Ingham m_fixed_expression.clear(); 585e1cfbc79STodd Fiala auto exe_ctx = GetCommandInterpreter().GetExecutionContext(); 586e1cfbc79STodd Fiala m_option_group.NotifyOptionParsingStarting(&exe_ctx); 587cf28a8b7SGreg Clayton 5884d51a902SRaphael Isemann if (command.empty()) { 589cf28a8b7SGreg Clayton GetMultilineExpression(); 59030fdc8d8SChris Lattner return result.Succeeded(); 59130fdc8d8SChris Lattner } 59230fdc8d8SChris Lattner 5933a0e1270SRaphael Isemann OptionsWithRaw args(command); 5944d51a902SRaphael Isemann llvm::StringRef expr = args.GetRawPart(); 59530fdc8d8SChris Lattner 5963a0e1270SRaphael Isemann if (args.HasArgs()) { 5973a0e1270SRaphael Isemann if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group, exe_ctx)) 59830fdc8d8SChris Lattner return false; 599f6b8b581SGreg Clayton 600b9c1b51eSKate Stone if (m_repl_option.GetOptionValue().GetCurrentValue()) { 601f2bd5c3eSSean Callanan Target *target = m_interpreter.GetExecutionContext().GetTargetPtr(); 602b9c1b51eSKate Stone if (target) { 603f2bd5c3eSSean Callanan // Drop into REPL 604f2bd5c3eSSean Callanan m_expr_lines.clear(); 605f2bd5c3eSSean Callanan m_expr_line_count = 0; 606f2bd5c3eSSean Callanan 607f2bd5c3eSSean Callanan Debugger &debugger = target->GetDebugger(); 608f2bd5c3eSSean Callanan 609b9c1b51eSKate Stone // Check if the LLDB command interpreter is sitting on top of a REPL 61005097246SAdrian Prantl // that launched it... 6113a0e1270SRaphael Isemann if (debugger.CheckTopIOHandlerTypes(IOHandler::Type::CommandInterpreter, 6123a0e1270SRaphael Isemann IOHandler::Type::REPL)) { 613b9c1b51eSKate Stone // the LLDB command interpreter is sitting on top of a REPL that 61405097246SAdrian Prantl // launched it, so just say the command interpreter is done and 61505097246SAdrian Prantl // fall back to the existing REPL 616f2bd5c3eSSean Callanan m_interpreter.GetIOHandler(false)->SetIsDone(true); 617b9c1b51eSKate Stone } else { 618b9c1b51eSKate Stone // We are launching the REPL on top of the current LLDB command 61905097246SAdrian Prantl // interpreter, so just push one 620f2bd5c3eSSean Callanan bool initialize = false; 62197206d57SZachary Turner Status repl_error; 6223a0e1270SRaphael Isemann REPLSP repl_sp(target->GetREPL(repl_error, m_command_options.language, 6233a0e1270SRaphael Isemann nullptr, false)); 624f2bd5c3eSSean Callanan 625b9c1b51eSKate Stone if (!repl_sp) { 626f2bd5c3eSSean Callanan initialize = true; 627b9c1b51eSKate Stone repl_sp = target->GetREPL(repl_error, m_command_options.language, 628b9c1b51eSKate Stone nullptr, true); 629b9c1b51eSKate Stone if (!repl_error.Success()) { 630f2bd5c3eSSean Callanan result.SetError(repl_error); 631f2bd5c3eSSean Callanan return result.Succeeded(); 632f2bd5c3eSSean Callanan } 633f2bd5c3eSSean Callanan } 634f2bd5c3eSSean Callanan 635b9c1b51eSKate Stone if (repl_sp) { 636b9c1b51eSKate Stone if (initialize) { 637c5bfa3daSJonas Devlieghere repl_sp->SetEvaluateOptions( 638c5bfa3daSJonas Devlieghere GetExprOptions(exe_ctx, m_command_options)); 639f2bd5c3eSSean Callanan repl_sp->SetFormatOptions(m_format_options); 640f2bd5c3eSSean Callanan repl_sp->SetValueObjectDisplayOptions(m_varobj_options); 641f2bd5c3eSSean Callanan } 642f2bd5c3eSSean Callanan 643f2bd5c3eSSean Callanan IOHandlerSP io_handler_sp(repl_sp->GetIOHandler()); 644f2bd5c3eSSean Callanan 645f2bd5c3eSSean Callanan io_handler_sp->SetIsDone(false); 646f2bd5c3eSSean Callanan 647f2bd5c3eSSean Callanan debugger.PushIOHandler(io_handler_sp); 648b9c1b51eSKate Stone } else { 649b9c1b51eSKate Stone repl_error.SetErrorStringWithFormat( 650b9c1b51eSKate Stone "Couldn't create a REPL for %s", 651b9c1b51eSKate Stone Language::GetNameForLanguageType(m_command_options.language)); 652f2bd5c3eSSean Callanan result.SetError(repl_error); 653f2bd5c3eSSean Callanan return result.Succeeded(); 654f2bd5c3eSSean Callanan } 655f2bd5c3eSSean Callanan } 656f2bd5c3eSSean Callanan } 657f2bd5c3eSSean Callanan } 658cf28a8b7SGreg Clayton // No expression following options 6594d51a902SRaphael Isemann else if (expr.empty()) { 660cf28a8b7SGreg Clayton GetMultilineExpression(); 661cf28a8b7SGreg Clayton return result.Succeeded(); 662cf28a8b7SGreg Clayton } 66330fdc8d8SChris Lattner } 66430fdc8d8SChris Lattner 6650df817aaSDavide Italiano Target *target = GetSelectedOrDummyTarget(); 66624fff242SDavide Italiano if (EvaluateExpression(expr, &(result.GetOutputStream()), 66724fff242SDavide Italiano &(result.GetErrorStream()), &result)) { 66824fff242SDavide Italiano 669b9c1b51eSKate Stone if (!m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) { 670e5ee6f04SJim Ingham CommandHistory &history = m_interpreter.GetCommandHistory(); 671b9c1b51eSKate Stone // FIXME: Can we figure out what the user actually typed (e.g. some alias 672b9c1b51eSKate Stone // for expr???) 673e5ee6f04SJim Ingham // If we can it would be nice to show that. 674e5ee6f04SJim Ingham std::string fixed_command("expression "); 6753a0e1270SRaphael Isemann if (args.HasArgs()) { 676e5ee6f04SJim Ingham // Add in any options that might have been in the original command: 6773a0e1270SRaphael Isemann fixed_command.append(args.GetArgStringWithDelimiter()); 678e5ee6f04SJim Ingham fixed_command.append(m_fixed_expression); 6793a0e1270SRaphael Isemann } else 6803a0e1270SRaphael Isemann fixed_command.append(m_fixed_expression); 681e5ee6f04SJim Ingham history.AppendString(fixed_command); 682e5ee6f04SJim Ingham } 68305097246SAdrian Prantl // Increment statistics to record this expression evaluation success. 68424fff242SDavide Italiano target->IncrementStats(StatisticKind::ExpressionSuccessful); 685fcd43b71SJohnny Chen return true; 686e5ee6f04SJim Ingham } 687fcd43b71SJohnny Chen 68805097246SAdrian Prantl // Increment statistics to record this expression evaluation failure. 68924fff242SDavide Italiano target->IncrementStats(StatisticKind::ExpressionFailure); 690fcd43b71SJohnny Chen result.SetStatus(eReturnStatusFailed); 691fcd43b71SJohnny Chen return false; 69230fdc8d8SChris Lattner } 693