10b57cec5SDimitry Andric //===-- CommandObjectExpression.cpp -----------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
100b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "CommandObjectExpression.h"
130b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
140b57cec5SDimitry Andric #include "lldb/Core/Value.h"
150b57cec5SDimitry Andric #include "lldb/Core/ValueObjectVariable.h"
160b57cec5SDimitry Andric #include "lldb/DataFormatters/ValueObjectPrinter.h"
170b57cec5SDimitry Andric #include "lldb/Expression/DWARFExpression.h"
180b57cec5SDimitry Andric #include "lldb/Expression/REPL.h"
190b57cec5SDimitry Andric #include "lldb/Expression/UserExpression.h"
200b57cec5SDimitry Andric #include "lldb/Host/Host.h"
210b57cec5SDimitry Andric #include "lldb/Host/OptionParser.h"
220b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
230b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
240b57cec5SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
250b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
260b57cec5SDimitry Andric #include "lldb/Symbol/Variable.h"
270b57cec5SDimitry Andric #include "lldb/Target/Language.h"
280b57cec5SDimitry Andric #include "lldb/Target/Process.h"
290b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
300b57cec5SDimitry Andric #include "lldb/Target/Target.h"
310b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric using namespace lldb;
340b57cec5SDimitry Andric using namespace lldb_private;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric CommandObjectExpression::CommandOptions::CommandOptions() : OptionGroup() {}
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric CommandObjectExpression::CommandOptions::~CommandOptions() = default;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric static constexpr OptionEnumValueElement g_description_verbosity_type[] = {
410b57cec5SDimitry Andric     {eLanguageRuntimeDescriptionDisplayVerbosityCompact, "compact",
420b57cec5SDimitry Andric      "Only show the description string"},
430b57cec5SDimitry Andric     {eLanguageRuntimeDescriptionDisplayVerbosityFull, "full",
440b57cec5SDimitry Andric      "Show the full output, including persistent variable's name and type"} };
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric static constexpr OptionEnumValues DescriptionVerbosityTypes() {
470b57cec5SDimitry Andric   return OptionEnumValues(g_description_verbosity_type);
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric static constexpr OptionDefinition g_expression_options[] = {
510b57cec5SDimitry Andric     // clang-format off
520b57cec5SDimitry Andric   {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "all-threads",           'a', OptionParser::eRequiredArgument, nullptr, {},                          0, eArgTypeBoolean,              "Should we run all threads if the execution doesn't complete on one thread."},
530b57cec5SDimitry Andric   {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "ignore-breakpoints",    'i', OptionParser::eRequiredArgument, nullptr, {},                          0, eArgTypeBoolean,              "Ignore breakpoint hits while running expressions"},
540b57cec5SDimitry Andric   {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "timeout",               't', OptionParser::eRequiredArgument, nullptr, {},                          0, eArgTypeUnsignedInteger,      "Timeout value (in microseconds) for running the expression."},
550b57cec5SDimitry Andric   {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "unwind-on-error",       'u', OptionParser::eRequiredArgument, nullptr, {},                          0, eArgTypeBoolean,              "Clean up program state if the expression causes a crash, or raises a signal.  "
560b57cec5SDimitry Andric                                                                                                                                                                                   "Note, unlike gdb hitting a breakpoint is controlled by another option (-i)."},
570b57cec5SDimitry Andric   {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "debug",                 'g', OptionParser::eNoArgument,       nullptr, {},                          0, eArgTypeNone,                 "When specified, debug the JIT code by setting a breakpoint on the first instruction "
580b57cec5SDimitry Andric                                                                                                                                                                                   "and forcing breakpoints to not be ignored (-i0) and no unwinding to happen on error (-u0)."},
590b57cec5SDimitry Andric   {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "language",              'l', OptionParser::eRequiredArgument, nullptr, {},                          0, eArgTypeLanguage,             "Specifies the Language to use when parsing the expression.  If not set the target.language "
600b57cec5SDimitry Andric                                                                                                                                                                                   "setting is used." },
610b57cec5SDimitry Andric   {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "apply-fixits",          'X', OptionParser::eRequiredArgument, nullptr, {},                          0, eArgTypeLanguage,             "If true, simple fix-it hints will be automatically applied to the expression." },
620b57cec5SDimitry Andric   {LLDB_OPT_SET_1,                  false, "description-verbosity", 'v', OptionParser::eOptionalArgument, nullptr, DescriptionVerbosityTypes(), 0, eArgTypeDescriptionVerbosity, "How verbose should the output of this expression be, if the object description is asked for."},
630b57cec5SDimitry Andric   {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "top-level",             'p', OptionParser::eNoArgument,       nullptr, {},                          0, eArgTypeNone,                 "Interpret the expression as a complete translation unit, without injecting it into the local "
640b57cec5SDimitry Andric                                                                                                                                                                                   "context.  Allows declaration of persistent, top-level entities without a $ prefix."},
650b57cec5SDimitry Andric   {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "allow-jit",             'j', OptionParser::eRequiredArgument, nullptr, {},                          0, eArgTypeBoolean,              "Controls whether the expression can fall back to being JITted if it's not supported by "
660b57cec5SDimitry Andric                                                                                                                                                                                   "the interpreter (defaults to true)."}
670b57cec5SDimitry Andric     // clang-format on
680b57cec5SDimitry Andric };
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric Status CommandObjectExpression::CommandOptions::SetOptionValue(
710b57cec5SDimitry Andric     uint32_t option_idx, llvm::StringRef option_arg,
720b57cec5SDimitry Andric     ExecutionContext *execution_context) {
730b57cec5SDimitry Andric   Status error;
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   const int short_option = GetDefinitions()[option_idx].short_option;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   switch (short_option) {
780b57cec5SDimitry Andric   case 'l':
790b57cec5SDimitry Andric     language = Language::GetLanguageTypeFromString(option_arg);
800b57cec5SDimitry Andric     if (language == eLanguageTypeUnknown)
810b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
820b57cec5SDimitry Andric           "unknown language type: '%s' for expression",
830b57cec5SDimitry Andric           option_arg.str().c_str());
840b57cec5SDimitry Andric     break;
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   case 'a': {
870b57cec5SDimitry Andric     bool success;
880b57cec5SDimitry Andric     bool result;
890b57cec5SDimitry Andric     result = OptionArgParser::ToBoolean(option_arg, true, &success);
900b57cec5SDimitry Andric     if (!success)
910b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
920b57cec5SDimitry Andric           "invalid all-threads value setting: \"%s\"",
930b57cec5SDimitry Andric           option_arg.str().c_str());
940b57cec5SDimitry Andric     else
950b57cec5SDimitry Andric       try_all_threads = result;
960b57cec5SDimitry Andric   } break;
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   case 'i': {
990b57cec5SDimitry Andric     bool success;
1000b57cec5SDimitry Andric     bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success);
1010b57cec5SDimitry Andric     if (success)
1020b57cec5SDimitry Andric       ignore_breakpoints = tmp_value;
1030b57cec5SDimitry Andric     else
1040b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
1050b57cec5SDimitry Andric           "could not convert \"%s\" to a boolean value.",
1060b57cec5SDimitry Andric           option_arg.str().c_str());
1070b57cec5SDimitry Andric     break;
1080b57cec5SDimitry Andric   }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   case 'j': {
1110b57cec5SDimitry Andric     bool success;
1120b57cec5SDimitry Andric     bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success);
1130b57cec5SDimitry Andric     if (success)
1140b57cec5SDimitry Andric       allow_jit = tmp_value;
1150b57cec5SDimitry Andric     else
1160b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
1170b57cec5SDimitry Andric           "could not convert \"%s\" to a boolean value.",
1180b57cec5SDimitry Andric           option_arg.str().c_str());
1190b57cec5SDimitry Andric     break;
1200b57cec5SDimitry Andric   }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   case 't':
1230b57cec5SDimitry Andric     if (option_arg.getAsInteger(0, timeout)) {
1240b57cec5SDimitry Andric       timeout = 0;
1250b57cec5SDimitry Andric       error.SetErrorStringWithFormat("invalid timeout setting \"%s\"",
1260b57cec5SDimitry Andric                                      option_arg.str().c_str());
1270b57cec5SDimitry Andric     }
1280b57cec5SDimitry Andric     break;
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   case 'u': {
1310b57cec5SDimitry Andric     bool success;
1320b57cec5SDimitry Andric     bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success);
1330b57cec5SDimitry Andric     if (success)
1340b57cec5SDimitry Andric       unwind_on_error = tmp_value;
1350b57cec5SDimitry Andric     else
1360b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
1370b57cec5SDimitry Andric           "could not convert \"%s\" to a boolean value.",
1380b57cec5SDimitry Andric           option_arg.str().c_str());
1390b57cec5SDimitry Andric     break;
1400b57cec5SDimitry Andric   }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   case 'v':
1430b57cec5SDimitry Andric     if (option_arg.empty()) {
1440b57cec5SDimitry Andric       m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull;
1450b57cec5SDimitry Andric       break;
1460b57cec5SDimitry Andric     }
1470b57cec5SDimitry Andric     m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity)
1480b57cec5SDimitry Andric         OptionArgParser::ToOptionEnum(
1490b57cec5SDimitry Andric             option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
1500b57cec5SDimitry Andric     if (!error.Success())
1510b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
1520b57cec5SDimitry Andric           "unrecognized value for description-verbosity '%s'",
1530b57cec5SDimitry Andric           option_arg.str().c_str());
1540b57cec5SDimitry Andric     break;
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric   case 'g':
1570b57cec5SDimitry Andric     debug = true;
1580b57cec5SDimitry Andric     unwind_on_error = false;
1590b57cec5SDimitry Andric     ignore_breakpoints = false;
1600b57cec5SDimitry Andric     break;
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   case 'p':
1630b57cec5SDimitry Andric     top_level = true;
1640b57cec5SDimitry Andric     break;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   case 'X': {
1670b57cec5SDimitry Andric     bool success;
1680b57cec5SDimitry Andric     bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success);
1690b57cec5SDimitry Andric     if (success)
1700b57cec5SDimitry Andric       auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo;
1710b57cec5SDimitry Andric     else
1720b57cec5SDimitry Andric       error.SetErrorStringWithFormat(
1730b57cec5SDimitry Andric           "could not convert \"%s\" to a boolean value.",
1740b57cec5SDimitry Andric           option_arg.str().c_str());
1750b57cec5SDimitry Andric     break;
1760b57cec5SDimitry Andric   }
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric   default:
1790b57cec5SDimitry Andric     error.SetErrorStringWithFormat("invalid short option character '%c'",
1800b57cec5SDimitry Andric                                    short_option);
1810b57cec5SDimitry Andric     break;
1820b57cec5SDimitry Andric   }
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   return error;
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric void CommandObjectExpression::CommandOptions::OptionParsingStarting(
1880b57cec5SDimitry Andric     ExecutionContext *execution_context) {
1890b57cec5SDimitry Andric   auto process_sp =
1900b57cec5SDimitry Andric       execution_context ? execution_context->GetProcessSP() : ProcessSP();
1910b57cec5SDimitry Andric   if (process_sp) {
1920b57cec5SDimitry Andric     ignore_breakpoints = process_sp->GetIgnoreBreakpointsInExpressions();
1930b57cec5SDimitry Andric     unwind_on_error = process_sp->GetUnwindOnErrorInExpressions();
1940b57cec5SDimitry Andric   } else {
1950b57cec5SDimitry Andric     ignore_breakpoints = true;
1960b57cec5SDimitry Andric     unwind_on_error = true;
1970b57cec5SDimitry Andric   }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   show_summary = true;
2000b57cec5SDimitry Andric   try_all_threads = true;
2010b57cec5SDimitry Andric   timeout = 0;
2020b57cec5SDimitry Andric   debug = false;
2030b57cec5SDimitry Andric   language = eLanguageTypeUnknown;
2040b57cec5SDimitry Andric   m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact;
2050b57cec5SDimitry Andric   auto_apply_fixits = eLazyBoolCalculate;
2060b57cec5SDimitry Andric   top_level = false;
2070b57cec5SDimitry Andric   allow_jit = true;
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric llvm::ArrayRef<OptionDefinition>
2110b57cec5SDimitry Andric CommandObjectExpression::CommandOptions::GetDefinitions() {
2120b57cec5SDimitry Andric   return llvm::makeArrayRef(g_expression_options);
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric CommandObjectExpression::CommandObjectExpression(
2160b57cec5SDimitry Andric     CommandInterpreter &interpreter)
2170b57cec5SDimitry Andric     : CommandObjectRaw(
2180b57cec5SDimitry Andric           interpreter, "expression", "Evaluate an expression on the current "
2190b57cec5SDimitry Andric                                      "thread.  Displays any returned value "
2200b57cec5SDimitry Andric                                      "with LLDB's default formatting.",
2210b57cec5SDimitry Andric           "", eCommandProcessMustBePaused | eCommandTryTargetAPILock),
2220b57cec5SDimitry Andric       IOHandlerDelegate(IOHandlerDelegate::Completion::Expression),
2230b57cec5SDimitry Andric       m_option_group(), m_format_options(eFormatDefault),
2240b57cec5SDimitry Andric       m_repl_option(LLDB_OPT_SET_1, false, "repl", 'r', "Drop into REPL", false,
2250b57cec5SDimitry Andric                     true),
2260b57cec5SDimitry Andric       m_command_options(), m_expr_line_count(0), m_expr_lines() {
2270b57cec5SDimitry Andric   SetHelpLong(
2280b57cec5SDimitry Andric       R"(
2290b57cec5SDimitry Andric Single and multi-line expressions:
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric )"
2320b57cec5SDimitry Andric       "    The expression provided on the command line must be a complete expression \
2330b57cec5SDimitry Andric with no newlines.  To evaluate a multi-line expression, \
2340b57cec5SDimitry Andric hit a return after an empty expression, and lldb will enter the multi-line expression editor. \
2350b57cec5SDimitry Andric Hit return on an empty line to end the multi-line expression."
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric       R"(
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric Timeouts:
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric )"
2420b57cec5SDimitry Andric       "    If the expression can be evaluated statically (without running code) then it will be.  \
2430b57cec5SDimitry Andric Otherwise, by default the expression will run on the current thread with a short timeout: \
2440b57cec5SDimitry Andric currently .25 seconds.  If it doesn't return in that time, the evaluation will be interrupted \
2450b57cec5SDimitry Andric and resumed with all threads running.  You can use the -a option to disable retrying on all \
2460b57cec5SDimitry Andric threads.  You can use the -t option to set a shorter timeout."
2470b57cec5SDimitry Andric       R"(
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric User defined variables:
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric )"
2520b57cec5SDimitry Andric       "    You can define your own variables for convenience or to be used in subsequent expressions.  \
2530b57cec5SDimitry Andric You define them the same way you would define variables in C.  If the first character of \
2540b57cec5SDimitry Andric your user defined variable is a $, then the variable's value will be available in future \
2550b57cec5SDimitry Andric expressions, otherwise it will just be available in the current expression."
2560b57cec5SDimitry Andric       R"(
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric Continuing evaluation after a breakpoint:
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric )"
2610b57cec5SDimitry Andric       "    If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \
2620b57cec5SDimitry Andric you are done with your investigation, you can either remove the expression execution frames \
2630b57cec5SDimitry Andric from the stack with \"thread return -x\" or if you are still interested in the expression result \
2640b57cec5SDimitry Andric you can issue the \"continue\" command and the expression evaluation will complete and the \
2650b57cec5SDimitry Andric expression result will be available using the \"thread.completed-expression\" key in the thread \
2660b57cec5SDimitry Andric format."
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric       R"(
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric Examples:
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric     expr my_struct->a = my_array[3]
2730b57cec5SDimitry Andric     expr -f bin -- (index * 8) + 5
2740b57cec5SDimitry Andric     expr unsigned int $foo = 5
2750b57cec5SDimitry Andric     expr char c[] = \"foo\"; c[0])");
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   CommandArgumentEntry arg;
2780b57cec5SDimitry Andric   CommandArgumentData expression_arg;
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric   // Define the first (and only) variant of this arg.
2810b57cec5SDimitry Andric   expression_arg.arg_type = eArgTypeExpression;
2820b57cec5SDimitry Andric   expression_arg.arg_repetition = eArgRepeatPlain;
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   // There is only one variant this argument could be; put it into the argument
2850b57cec5SDimitry Andric   // entry.
2860b57cec5SDimitry Andric   arg.push_back(expression_arg);
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   // Push the data for the first argument into the m_arguments vector.
2890b57cec5SDimitry Andric   m_arguments.push_back(arg);
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric   // Add the "--format" and "--gdb-format"
2920b57cec5SDimitry Andric   m_option_group.Append(&m_format_options,
2930b57cec5SDimitry Andric                         OptionGroupFormat::OPTION_GROUP_FORMAT |
2940b57cec5SDimitry Andric                             OptionGroupFormat::OPTION_GROUP_GDB_FMT,
2950b57cec5SDimitry Andric                         LLDB_OPT_SET_1);
2960b57cec5SDimitry Andric   m_option_group.Append(&m_command_options);
2970b57cec5SDimitry Andric   m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL,
2980b57cec5SDimitry Andric                         LLDB_OPT_SET_1 | LLDB_OPT_SET_2);
2990b57cec5SDimitry Andric   m_option_group.Append(&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3);
3000b57cec5SDimitry Andric   m_option_group.Finalize();
3010b57cec5SDimitry Andric }
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric CommandObjectExpression::~CommandObjectExpression() = default;
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric Options *CommandObjectExpression::GetOptions() { return &m_option_group; }
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric int CommandObjectExpression::HandleCompletion(CompletionRequest &request) {
3080b57cec5SDimitry Andric   EvaluateExpressionOptions options;
3090b57cec5SDimitry Andric   options.SetCoerceToId(m_varobj_options.use_objc);
3100b57cec5SDimitry Andric   options.SetLanguage(m_command_options.language);
3110b57cec5SDimitry Andric   options.SetExecutionPolicy(lldb_private::eExecutionPolicyNever);
3120b57cec5SDimitry Andric   options.SetAutoApplyFixIts(false);
3130b57cec5SDimitry Andric   options.SetGenerateDebugInfo(false);
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric   // We need a valid execution context with a frame pointer for this
3160b57cec5SDimitry Andric   // completion, so if we don't have one we should try to make a valid
3170b57cec5SDimitry Andric   // execution context.
3180b57cec5SDimitry Andric   if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr)
3190b57cec5SDimitry Andric     m_interpreter.UpdateExecutionContext(nullptr);
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric   // This didn't work, so let's get out before we start doing things that
3220b57cec5SDimitry Andric   // expect a valid frame pointer.
3230b57cec5SDimitry Andric   if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr)
3240b57cec5SDimitry Andric     return 0;
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric   Target *target = exe_ctx.GetTargetPtr();
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   if (!target)
3310b57cec5SDimitry Andric     target = GetDummyTarget();
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   if (!target)
3340b57cec5SDimitry Andric     return 0;
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric   unsigned cursor_pos = request.GetRawCursorPos();
3370b57cec5SDimitry Andric   llvm::StringRef code = request.GetRawLine();
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric   const std::size_t original_code_size = code.size();
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric   // Remove the first token which is 'expr' or some alias/abbreviation of that.
3420b57cec5SDimitry Andric   code = llvm::getToken(code).second.ltrim();
3430b57cec5SDimitry Andric   OptionsWithRaw args(code);
3440b57cec5SDimitry Andric   code = args.GetRawPart();
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric   // The position where the expression starts in the command line.
3470b57cec5SDimitry Andric   assert(original_code_size >= code.size());
3480b57cec5SDimitry Andric   std::size_t raw_start = original_code_size - code.size();
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric   // Check if the cursor is actually in the expression string, and if not, we
3510b57cec5SDimitry Andric   // exit.
3520b57cec5SDimitry Andric   // FIXME: We should complete the options here.
3530b57cec5SDimitry Andric   if (cursor_pos < raw_start)
3540b57cec5SDimitry Andric     return 0;
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   // Make the cursor_pos again relative to the start of the code string.
3570b57cec5SDimitry Andric   assert(cursor_pos >= raw_start);
3580b57cec5SDimitry Andric   cursor_pos -= raw_start;
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric   auto language = exe_ctx.GetFrameRef().GetLanguage();
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric   Status error;
3630b57cec5SDimitry Andric   lldb::UserExpressionSP expr(target->GetUserExpressionForLanguage(
3640b57cec5SDimitry Andric       code, llvm::StringRef(), language, UserExpression::eResultTypeAny,
3650b57cec5SDimitry Andric       options, nullptr, error));
3660b57cec5SDimitry Andric   if (error.Fail())
3670b57cec5SDimitry Andric     return 0;
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   expr->Complete(exe_ctx, request, cursor_pos);
3700b57cec5SDimitry Andric   return request.GetNumberOfMatches();
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric static lldb_private::Status
3740b57cec5SDimitry Andric CanBeUsedForElementCountPrinting(ValueObject &valobj) {
3750b57cec5SDimitry Andric   CompilerType type(valobj.GetCompilerType());
3760b57cec5SDimitry Andric   CompilerType pointee;
3770b57cec5SDimitry Andric   if (!type.IsPointerType(&pointee))
3780b57cec5SDimitry Andric     return Status("as it does not refer to a pointer");
3790b57cec5SDimitry Andric   if (pointee.IsVoidType())
3800b57cec5SDimitry Andric     return Status("as it refers to a pointer to void");
3810b57cec5SDimitry Andric   return Status();
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
3850b57cec5SDimitry Andric                                                  Stream *output_stream,
3860b57cec5SDimitry Andric                                                  Stream *error_stream,
3870b57cec5SDimitry Andric                                                  CommandReturnObject *result) {
3880b57cec5SDimitry Andric   // Don't use m_exe_ctx as this might be called asynchronously after the
3890b57cec5SDimitry Andric   // command object DoExecute has finished when doing multi-line expression
3900b57cec5SDimitry Andric   // that use an input reader...
3910b57cec5SDimitry Andric   ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   Target *target = exe_ctx.GetTargetPtr();
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric   if (!target)
3960b57cec5SDimitry Andric     target = GetDummyTarget();
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric   if (target) {
3990b57cec5SDimitry Andric     lldb::ValueObjectSP result_valobj_sp;
4000b57cec5SDimitry Andric     bool keep_in_memory = true;
4010b57cec5SDimitry Andric     StackFrame *frame = exe_ctx.GetFramePtr();
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric     EvaluateExpressionOptions options;
4040b57cec5SDimitry Andric     options.SetCoerceToId(m_varobj_options.use_objc);
4050b57cec5SDimitry Andric     options.SetUnwindOnError(m_command_options.unwind_on_error);
4060b57cec5SDimitry Andric     options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints);
4070b57cec5SDimitry Andric     options.SetKeepInMemory(keep_in_memory);
4080b57cec5SDimitry Andric     options.SetUseDynamic(m_varobj_options.use_dynamic);
4090b57cec5SDimitry Andric     options.SetTryAllThreads(m_command_options.try_all_threads);
4100b57cec5SDimitry Andric     options.SetDebug(m_command_options.debug);
4110b57cec5SDimitry Andric     options.SetLanguage(m_command_options.language);
4120b57cec5SDimitry Andric     options.SetExecutionPolicy(
4130b57cec5SDimitry Andric         m_command_options.allow_jit
4140b57cec5SDimitry Andric             ? EvaluateExpressionOptions::default_execution_policy
4150b57cec5SDimitry Andric             : lldb_private::eExecutionPolicyNever);
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric     bool auto_apply_fixits;
4180b57cec5SDimitry Andric     if (m_command_options.auto_apply_fixits == eLazyBoolCalculate)
4190b57cec5SDimitry Andric       auto_apply_fixits = target->GetEnableAutoApplyFixIts();
4200b57cec5SDimitry Andric     else
4210b57cec5SDimitry Andric       auto_apply_fixits = m_command_options.auto_apply_fixits == eLazyBoolYes;
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric     options.SetAutoApplyFixIts(auto_apply_fixits);
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric     if (m_command_options.top_level)
4260b57cec5SDimitry Andric       options.SetExecutionPolicy(eExecutionPolicyTopLevel);
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric     // If there is any chance we are going to stop and want to see what went
4290b57cec5SDimitry Andric     // wrong with our expression, we should generate debug info
4300b57cec5SDimitry Andric     if (!m_command_options.ignore_breakpoints ||
4310b57cec5SDimitry Andric         !m_command_options.unwind_on_error)
4320b57cec5SDimitry Andric       options.SetGenerateDebugInfo(true);
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric     if (m_command_options.timeout > 0)
4350b57cec5SDimitry Andric       options.SetTimeout(std::chrono::microseconds(m_command_options.timeout));
4360b57cec5SDimitry Andric     else
4370b57cec5SDimitry Andric       options.SetTimeout(llvm::None);
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric     ExpressionResults success = target->EvaluateExpression(
4400b57cec5SDimitry Andric         expr, frame, result_valobj_sp, options, &m_fixed_expression);
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric     // We only tell you about the FixIt if we applied it.  The compiler errors
4430b57cec5SDimitry Andric     // will suggest the FixIt if it parsed.
4440b57cec5SDimitry Andric     if (error_stream && !m_fixed_expression.empty() &&
4450b57cec5SDimitry Andric         target->GetEnableNotifyAboutFixIts()) {
4460b57cec5SDimitry Andric       if (success == eExpressionCompleted)
4470b57cec5SDimitry Andric         error_stream->Printf(
4480b57cec5SDimitry Andric             "  Fix-it applied, fixed expression was: \n    %s\n",
4490b57cec5SDimitry Andric             m_fixed_expression.c_str());
4500b57cec5SDimitry Andric     }
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric     if (result_valobj_sp) {
4530b57cec5SDimitry Andric       Format format = m_format_options.GetFormat();
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric       if (result_valobj_sp->GetError().Success()) {
4560b57cec5SDimitry Andric         if (format != eFormatVoid) {
4570b57cec5SDimitry Andric           if (format != eFormatDefault)
4580b57cec5SDimitry Andric             result_valobj_sp->SetFormat(format);
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric           if (m_varobj_options.elem_count > 0) {
4610b57cec5SDimitry Andric             Status error(CanBeUsedForElementCountPrinting(*result_valobj_sp));
4620b57cec5SDimitry Andric             if (error.Fail()) {
4630b57cec5SDimitry Andric               result->AppendErrorWithFormat(
4640b57cec5SDimitry Andric                   "expression cannot be used with --element-count %s\n",
4650b57cec5SDimitry Andric                   error.AsCString(""));
4660b57cec5SDimitry Andric               result->SetStatus(eReturnStatusFailed);
4670b57cec5SDimitry Andric               return false;
4680b57cec5SDimitry Andric             }
4690b57cec5SDimitry Andric           }
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric           DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(
4720b57cec5SDimitry Andric               m_command_options.m_verbosity, format));
4730b57cec5SDimitry Andric           options.SetVariableFormatDisplayLanguage(
4740b57cec5SDimitry Andric               result_valobj_sp->GetPreferredDisplayLanguage());
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric           result_valobj_sp->Dump(*output_stream, options);
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric           if (result)
4790b57cec5SDimitry Andric             result->SetStatus(eReturnStatusSuccessFinishResult);
4800b57cec5SDimitry Andric         }
4810b57cec5SDimitry Andric       } else {
4820b57cec5SDimitry Andric         if (result_valobj_sp->GetError().GetError() ==
4830b57cec5SDimitry Andric             UserExpression::kNoResult) {
4840b57cec5SDimitry Andric           if (format != eFormatVoid && GetDebugger().GetNotifyVoid()) {
4850b57cec5SDimitry Andric             error_stream->PutCString("(void)\n");
4860b57cec5SDimitry Andric           }
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric           if (result)
4890b57cec5SDimitry Andric             result->SetStatus(eReturnStatusSuccessFinishResult);
4900b57cec5SDimitry Andric         } else {
4910b57cec5SDimitry Andric           const char *error_cstr = result_valobj_sp->GetError().AsCString();
4920b57cec5SDimitry Andric           if (error_cstr && error_cstr[0]) {
4930b57cec5SDimitry Andric             const size_t error_cstr_len = strlen(error_cstr);
4940b57cec5SDimitry Andric             const bool ends_with_newline =
4950b57cec5SDimitry Andric                 error_cstr[error_cstr_len - 1] == '\n';
4960b57cec5SDimitry Andric             if (strstr(error_cstr, "error:") != error_cstr)
4970b57cec5SDimitry Andric               error_stream->PutCString("error: ");
4980b57cec5SDimitry Andric             error_stream->Write(error_cstr, error_cstr_len);
4990b57cec5SDimitry Andric             if (!ends_with_newline)
5000b57cec5SDimitry Andric               error_stream->EOL();
5010b57cec5SDimitry Andric           } else {
5020b57cec5SDimitry Andric             error_stream->PutCString("error: unknown error\n");
5030b57cec5SDimitry Andric           }
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric           if (result)
5060b57cec5SDimitry Andric             result->SetStatus(eReturnStatusFailed);
5070b57cec5SDimitry Andric         }
5080b57cec5SDimitry Andric       }
5090b57cec5SDimitry Andric     }
5100b57cec5SDimitry Andric   } else {
5110b57cec5SDimitry Andric     error_stream->Printf("error: invalid execution context for expression\n");
5120b57cec5SDimitry Andric     return false;
5130b57cec5SDimitry Andric   }
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric   return true;
5160b57cec5SDimitry Andric }
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler,
5190b57cec5SDimitry Andric                                                      std::string &line) {
5200b57cec5SDimitry Andric   io_handler.SetIsDone(true);
5210b57cec5SDimitry Andric   //    StreamSP output_stream =
5220b57cec5SDimitry Andric   //    io_handler.GetDebugger().GetAsyncOutputStream();
5230b57cec5SDimitry Andric   //    StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream();
5240b57cec5SDimitry Andric   StreamFileSP output_sp(io_handler.GetOutputStreamFile());
5250b57cec5SDimitry Andric   StreamFileSP error_sp(io_handler.GetErrorStreamFile());
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric   EvaluateExpression(line.c_str(), output_sp.get(), error_sp.get());
5280b57cec5SDimitry Andric   if (output_sp)
5290b57cec5SDimitry Andric     output_sp->Flush();
5300b57cec5SDimitry Andric   if (error_sp)
5310b57cec5SDimitry Andric     error_sp->Flush();
5320b57cec5SDimitry Andric }
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler,
5350b57cec5SDimitry Andric                                                        StringList &lines) {
5360b57cec5SDimitry Andric   // An empty lines is used to indicate the end of input
5370b57cec5SDimitry Andric   const size_t num_lines = lines.GetSize();
5380b57cec5SDimitry Andric   if (num_lines > 0 && lines[num_lines - 1].empty()) {
5390b57cec5SDimitry Andric     // Remove the last empty line from "lines" so it doesn't appear in our
5400b57cec5SDimitry Andric     // resulting input and return true to indicate we are done getting lines
5410b57cec5SDimitry Andric     lines.PopBack();
5420b57cec5SDimitry Andric     return true;
5430b57cec5SDimitry Andric   }
5440b57cec5SDimitry Andric   return false;
5450b57cec5SDimitry Andric }
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric void CommandObjectExpression::GetMultilineExpression() {
5480b57cec5SDimitry Andric   m_expr_lines.clear();
5490b57cec5SDimitry Andric   m_expr_line_count = 0;
5500b57cec5SDimitry Andric 
5510b57cec5SDimitry Andric   Debugger &debugger = GetCommandInterpreter().GetDebugger();
5520b57cec5SDimitry Andric   bool color_prompt = debugger.GetUseColor();
5530b57cec5SDimitry Andric   const bool multiple_lines = true; // Get multiple lines
5540b57cec5SDimitry Andric   IOHandlerSP io_handler_sp(
5550b57cec5SDimitry Andric       new IOHandlerEditline(debugger, IOHandler::Type::Expression,
5560b57cec5SDimitry Andric                             "lldb-expr", // Name of input reader for history
5570b57cec5SDimitry Andric                             llvm::StringRef(), // No prompt
5580b57cec5SDimitry Andric                             llvm::StringRef(), // Continuation prompt
5590b57cec5SDimitry Andric                             multiple_lines, color_prompt,
5600b57cec5SDimitry Andric                             1, // Show line numbers starting at 1
5610b57cec5SDimitry Andric                             *this, nullptr));
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric   StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile());
5640b57cec5SDimitry Andric   if (output_sp) {
5650b57cec5SDimitry Andric     output_sp->PutCString(
5660b57cec5SDimitry Andric         "Enter expressions, then terminate with an empty line to evaluate:\n");
5670b57cec5SDimitry Andric     output_sp->Flush();
5680b57cec5SDimitry Andric   }
5690b57cec5SDimitry Andric   debugger.PushIOHandler(io_handler_sp);
5700b57cec5SDimitry Andric }
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric static EvaluateExpressionOptions
5730b57cec5SDimitry Andric GetExprOptions(ExecutionContext &ctx,
5740b57cec5SDimitry Andric                CommandObjectExpression::CommandOptions command_options) {
5750b57cec5SDimitry Andric   command_options.OptionParsingStarting(&ctx);
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric   // Default certain settings for REPL regardless of the global settings.
5780b57cec5SDimitry Andric   command_options.unwind_on_error = false;
5790b57cec5SDimitry Andric   command_options.ignore_breakpoints = false;
5800b57cec5SDimitry Andric   command_options.debug = false;
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric   EvaluateExpressionOptions expr_options;
5830b57cec5SDimitry Andric   expr_options.SetUnwindOnError(command_options.unwind_on_error);
5840b57cec5SDimitry Andric   expr_options.SetIgnoreBreakpoints(command_options.ignore_breakpoints);
5850b57cec5SDimitry Andric   expr_options.SetTryAllThreads(command_options.try_all_threads);
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric   if (command_options.timeout > 0)
5880b57cec5SDimitry Andric     expr_options.SetTimeout(std::chrono::microseconds(command_options.timeout));
5890b57cec5SDimitry Andric   else
5900b57cec5SDimitry Andric     expr_options.SetTimeout(llvm::None);
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric   return expr_options;
5930b57cec5SDimitry Andric }
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric bool CommandObjectExpression::DoExecute(llvm::StringRef command,
5960b57cec5SDimitry Andric                                         CommandReturnObject &result) {
5970b57cec5SDimitry Andric   m_fixed_expression.clear();
5980b57cec5SDimitry Andric   auto exe_ctx = GetCommandInterpreter().GetExecutionContext();
5990b57cec5SDimitry Andric   m_option_group.NotifyOptionParsingStarting(&exe_ctx);
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric   if (command.empty()) {
6020b57cec5SDimitry Andric     GetMultilineExpression();
6030b57cec5SDimitry Andric     return result.Succeeded();
6040b57cec5SDimitry Andric   }
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric   OptionsWithRaw args(command);
6070b57cec5SDimitry Andric   llvm::StringRef expr = args.GetRawPart();
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric   if (args.HasArgs()) {
6100b57cec5SDimitry Andric     if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group, exe_ctx))
6110b57cec5SDimitry Andric       return false;
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric     if (m_repl_option.GetOptionValue().GetCurrentValue()) {
6140b57cec5SDimitry Andric       Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
6150b57cec5SDimitry Andric       if (target) {
6160b57cec5SDimitry Andric         // Drop into REPL
6170b57cec5SDimitry Andric         m_expr_lines.clear();
6180b57cec5SDimitry Andric         m_expr_line_count = 0;
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric         Debugger &debugger = target->GetDebugger();
6210b57cec5SDimitry Andric 
6220b57cec5SDimitry Andric         // Check if the LLDB command interpreter is sitting on top of a REPL
6230b57cec5SDimitry Andric         // that launched it...
6240b57cec5SDimitry Andric         if (debugger.CheckTopIOHandlerTypes(IOHandler::Type::CommandInterpreter,
6250b57cec5SDimitry Andric                                             IOHandler::Type::REPL)) {
6260b57cec5SDimitry Andric           // the LLDB command interpreter is sitting on top of a REPL that
6270b57cec5SDimitry Andric           // launched it, so just say the command interpreter is done and
6280b57cec5SDimitry Andric           // fall back to the existing REPL
6290b57cec5SDimitry Andric           m_interpreter.GetIOHandler(false)->SetIsDone(true);
6300b57cec5SDimitry Andric         } else {
6310b57cec5SDimitry Andric           // We are launching the REPL on top of the current LLDB command
6320b57cec5SDimitry Andric           // interpreter, so just push one
6330b57cec5SDimitry Andric           bool initialize = false;
6340b57cec5SDimitry Andric           Status repl_error;
6350b57cec5SDimitry Andric           REPLSP repl_sp(target->GetREPL(repl_error, m_command_options.language,
6360b57cec5SDimitry Andric                                          nullptr, false));
6370b57cec5SDimitry Andric 
6380b57cec5SDimitry Andric           if (!repl_sp) {
6390b57cec5SDimitry Andric             initialize = true;
6400b57cec5SDimitry Andric             repl_sp = target->GetREPL(repl_error, m_command_options.language,
6410b57cec5SDimitry Andric                                       nullptr, true);
6420b57cec5SDimitry Andric             if (!repl_error.Success()) {
6430b57cec5SDimitry Andric               result.SetError(repl_error);
6440b57cec5SDimitry Andric               return result.Succeeded();
6450b57cec5SDimitry Andric             }
6460b57cec5SDimitry Andric           }
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric           if (repl_sp) {
6490b57cec5SDimitry Andric             if (initialize) {
6500b57cec5SDimitry Andric               repl_sp->SetEvaluateOptions(
6510b57cec5SDimitry Andric                   GetExprOptions(exe_ctx, m_command_options));
6520b57cec5SDimitry Andric               repl_sp->SetFormatOptions(m_format_options);
6530b57cec5SDimitry Andric               repl_sp->SetValueObjectDisplayOptions(m_varobj_options);
6540b57cec5SDimitry Andric             }
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric             IOHandlerSP io_handler_sp(repl_sp->GetIOHandler());
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric             io_handler_sp->SetIsDone(false);
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric             debugger.PushIOHandler(io_handler_sp);
6610b57cec5SDimitry Andric           } else {
6620b57cec5SDimitry Andric             repl_error.SetErrorStringWithFormat(
6630b57cec5SDimitry Andric                 "Couldn't create a REPL for %s",
6640b57cec5SDimitry Andric                 Language::GetNameForLanguageType(m_command_options.language));
6650b57cec5SDimitry Andric             result.SetError(repl_error);
6660b57cec5SDimitry Andric             return result.Succeeded();
6670b57cec5SDimitry Andric           }
6680b57cec5SDimitry Andric         }
6690b57cec5SDimitry Andric       }
6700b57cec5SDimitry Andric     }
6710b57cec5SDimitry Andric     // No expression following options
6720b57cec5SDimitry Andric     else if (expr.empty()) {
6730b57cec5SDimitry Andric       GetMultilineExpression();
6740b57cec5SDimitry Andric       return result.Succeeded();
6750b57cec5SDimitry Andric     }
6760b57cec5SDimitry Andric   }
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric   Target *target = GetSelectedOrDummyTarget();
6790b57cec5SDimitry Andric   if (EvaluateExpression(expr, &(result.GetOutputStream()),
6800b57cec5SDimitry Andric                          &(result.GetErrorStream()), &result)) {
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric     if (!m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) {
6830b57cec5SDimitry Andric       CommandHistory &history = m_interpreter.GetCommandHistory();
6840b57cec5SDimitry Andric       // FIXME: Can we figure out what the user actually typed (e.g. some alias
6850b57cec5SDimitry Andric       // for expr???)
6860b57cec5SDimitry Andric       // If we can it would be nice to show that.
6870b57cec5SDimitry Andric       std::string fixed_command("expression ");
6880b57cec5SDimitry Andric       if (args.HasArgs()) {
6890b57cec5SDimitry Andric         // Add in any options that might have been in the original command:
6900b57cec5SDimitry Andric         fixed_command.append(args.GetArgStringWithDelimiter());
6910b57cec5SDimitry Andric         fixed_command.append(m_fixed_expression);
6920b57cec5SDimitry Andric       } else
6930b57cec5SDimitry Andric         fixed_command.append(m_fixed_expression);
6940b57cec5SDimitry Andric       history.AppendString(fixed_command);
6950b57cec5SDimitry Andric     }
6960b57cec5SDimitry Andric     // Increment statistics to record this expression evaluation success.
6970b57cec5SDimitry Andric     target->IncrementStats(StatisticKind::ExpressionSuccessful);
6980b57cec5SDimitry Andric     return true;
6990b57cec5SDimitry Andric   }
7000b57cec5SDimitry Andric 
7010b57cec5SDimitry Andric   // Increment statistics to record this expression evaluation failure.
7020b57cec5SDimitry Andric   target->IncrementStats(StatisticKind::ExpressionFailure);
7030b57cec5SDimitry Andric   result.SetStatus(eReturnStatusFailed);
7040b57cec5SDimitry Andric   return false;
7050b57cec5SDimitry Andric }
706