Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObjectExpression.cpp -----------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
| 15 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | // Project includes |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 17 | #include "CommandObjectExpression.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | #include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h" |
| 19 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Value.h" |
Jim Ingham | 6c68fb4 | 2010-09-30 00:54:27 +0000 | [diff] [blame] | 21 | #include "lldb/Core/ValueObjectVariable.h" |
Enrico Granata | 4d93b8c | 2013-09-30 19:11:51 +0000 | [diff] [blame] | 22 | #include "lldb/DataFormatters/ValueObjectPrinter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Expression/DWARFExpression.h" |
Sean Callanan | f2bd5c3 | 2015-10-20 00:55:21 +0000 | [diff] [blame] | 24 | #include "lldb/Expression/REPL.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | #include "lldb/Expression/UserExpression.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | #include "lldb/Host/Host.h" |
Zachary Turner | 3eb2b44 | 2017-03-22 23:33:16 +0000 | [diff] [blame] | 27 | #include "lldb/Host/OptionParser.h" |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 28 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Interpreter/CommandReturnObject.h" |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 30 | #include "lldb/Interpreter/OptionArgParser.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | #include "lldb/Symbol/ObjectFile.h" |
| 32 | #include "lldb/Symbol/Variable.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | #include "lldb/Target/Language.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | #include "lldb/Target/Process.h" |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 35 | #include "lldb/Target/StackFrame.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | #include "lldb/Target/Target.h" |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 37 | #include "lldb/Target/Thread.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace lldb; |
| 40 | using namespace lldb_private; |
| 41 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | CommandObjectExpression::CommandOptions::CommandOptions() : OptionGroup() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 44 | CommandObjectExpression::CommandOptions::~CommandOptions() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | static OptionEnumValueElement g_description_verbosity_type[] = { |
| 47 | {eLanguageRuntimeDescriptionDisplayVerbosityCompact, "compact", |
| 48 | "Only show the description string"}, |
| 49 | {eLanguageRuntimeDescriptionDisplayVerbosityFull, "full", |
| 50 | "Show the full output, including persistent variable's name and type"}, |
| 51 | {0, nullptr, nullptr}}; |
Enrico Granata | 4d93b8c | 2013-09-30 19:11:51 +0000 | [diff] [blame] | 52 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 53 | static OptionDefinition g_expression_options[] = { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | // clang-format off |
Kate Stone | ac9c3a6 | 2016-08-26 23:28:47 +0000 | [diff] [blame] | 55 | {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "all-threads", 'a', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Should we run all threads if the execution doesn't complete on one thread."}, |
| 56 | {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "ignore-breakpoints", 'i', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Ignore breakpoint hits while running expressions"}, |
| 57 | {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "timeout", 't', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Timeout value (in microseconds) for running the expression."}, |
| 58 | {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "unwind-on-error", 'u', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Clean up program state if the expression causes a crash, or raises a signal. " |
| 59 | "Note, unlike gdb hitting a breakpoint is controlled by another option (-i)."}, |
| 60 | {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "debug", 'g', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "When specified, debug the JIT code by setting a breakpoint on the first instruction " |
| 61 | "and forcing breakpoints to not be ignored (-i0) and no unwinding to happen on error (-u0)."}, |
| 62 | {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "language", 'l', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLanguage, "Specifies the Language to use when parsing the expression. If not set the target.language " |
| 63 | "setting is used." }, |
| 64 | {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "apply-fixits", 'X', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLanguage, "If true, simple fix-it hints will be automatically applied to the expression." }, |
| 65 | {LLDB_OPT_SET_1, false, "description-verbosity", 'v', OptionParser::eOptionalArgument, nullptr, g_description_verbosity_type, 0, eArgTypeDescriptionVerbosity, "How verbose should the output of this expression be, if the object description is asked for."}, |
Sean Callanan | 0354a68 | 2016-10-11 16:57:21 +0000 | [diff] [blame] | 66 | {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "top-level", 'p', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Interpret the expression as a complete translation unit, without injecting it into the local " |
| 67 | "context. Allows declaration of persistent, top-level entities without a $ prefix."}, |
Kate Stone | ac9c3a6 | 2016-08-26 23:28:47 +0000 | [diff] [blame] | 68 | {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "allow-jit", 'j', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Controls whether the expression can fall back to being JITted if it's not supported by " |
| 69 | "the interpreter (defaults to true)."} |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | // clang-format on |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 73 | Status CommandObjectExpression::CommandOptions::SetOptionValue( |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 74 | uint32_t option_idx, llvm::StringRef option_arg, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | ExecutionContext *execution_context) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 76 | Status error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 78 | const int short_option = GetDefinitions()[option_idx].short_option; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | switch (short_option) { |
| 81 | case 'l': |
| 82 | language = Language::GetLanguageTypeFromString(option_arg); |
| 83 | if (language == eLanguageTypeUnknown) |
| 84 | error.SetErrorStringWithFormat( |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 85 | "unknown language type: '%s' for expression", |
| 86 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | case 'a': { |
| 90 | bool success; |
| 91 | bool result; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 92 | result = OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | if (!success) |
| 94 | error.SetErrorStringWithFormat( |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 95 | "invalid all-threads value setting: \"%s\"", |
| 96 | option_arg.str().c_str()); |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 97 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | try_all_threads = result; |
| 99 | } break; |
| 100 | |
| 101 | case 'i': { |
| 102 | bool success; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 103 | bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | if (success) |
| 105 | ignore_breakpoints = tmp_value; |
| 106 | else |
| 107 | error.SetErrorStringWithFormat( |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 108 | "could not convert \"%s\" to a boolean value.", |
| 109 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | break; |
| 111 | } |
| 112 | |
| 113 | case 'j': { |
| 114 | bool success; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 115 | bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | if (success) |
| 117 | allow_jit = tmp_value; |
| 118 | else |
| 119 | error.SetErrorStringWithFormat( |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 120 | "could not convert \"%s\" to a boolean value.", |
| 121 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 125 | case 't': |
| 126 | if (option_arg.getAsInteger(0, timeout)) { |
| 127 | timeout = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | error.SetErrorStringWithFormat("invalid timeout setting \"%s\"", |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 129 | option_arg.str().c_str()); |
| 130 | } |
| 131 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | |
| 133 | case 'u': { |
| 134 | bool success; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 135 | bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | if (success) |
| 137 | unwind_on_error = tmp_value; |
| 138 | else |
| 139 | error.SetErrorStringWithFormat( |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 140 | "could not convert \"%s\" to a boolean value.", |
| 141 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | break; |
| 143 | } |
| 144 | |
| 145 | case 'v': |
Enrico Granata | 543a26e | 2016-09-26 21:36:17 +0000 | [diff] [blame] | 146 | if (option_arg.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull; |
| 148 | break; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 149 | } |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 150 | m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) |
| 151 | OptionArgParser::ToOptionEnum( |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 152 | option_arg, GetDefinitions()[option_idx].enum_values, 0, error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | if (!error.Success()) |
| 154 | error.SetErrorStringWithFormat( |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 155 | "unrecognized value for description-verbosity '%s'", |
| 156 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | break; |
| 158 | |
| 159 | case 'g': |
| 160 | debug = true; |
| 161 | unwind_on_error = false; |
| 162 | ignore_breakpoints = false; |
| 163 | break; |
| 164 | |
| 165 | case 'p': |
| 166 | top_level = true; |
| 167 | break; |
| 168 | |
| 169 | case 'X': { |
| 170 | bool success; |
Pavel Labath | 47cbf4a | 2018-04-10 09:03:59 +0000 | [diff] [blame] | 171 | bool tmp_value = OptionArgParser::ToBoolean(option_arg, true, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | if (success) |
| 173 | auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo; |
| 174 | else |
| 175 | error.SetErrorStringWithFormat( |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 176 | "could not convert \"%s\" to a boolean value.", |
| 177 | option_arg.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | |
| 181 | default: |
| 182 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 183 | short_option); |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 190 | void CommandObjectExpression::CommandOptions::OptionParsingStarting( |
| 191 | ExecutionContext *execution_context) { |
| 192 | auto process_sp = |
| 193 | execution_context ? execution_context->GetProcessSP() : ProcessSP(); |
| 194 | if (process_sp) { |
| 195 | ignore_breakpoints = process_sp->GetIgnoreBreakpointsInExpressions(); |
| 196 | unwind_on_error = process_sp->GetUnwindOnErrorInExpressions(); |
| 197 | } else { |
| 198 | ignore_breakpoints = true; |
| 199 | unwind_on_error = true; |
| 200 | } |
| 201 | |
| 202 | show_summary = true; |
| 203 | try_all_threads = true; |
| 204 | timeout = 0; |
| 205 | debug = false; |
| 206 | language = eLanguageTypeUnknown; |
| 207 | m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact; |
| 208 | auto_apply_fixits = eLazyBoolCalculate; |
| 209 | top_level = false; |
| 210 | allow_jit = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 213 | llvm::ArrayRef<OptionDefinition> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | CommandObjectExpression::CommandOptions::GetDefinitions() { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 215 | return llvm::makeArrayRef(g_expression_options); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | CommandObjectExpression::CommandObjectExpression( |
| 219 | CommandInterpreter &interpreter) |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 220 | : CommandObjectRaw( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | interpreter, "expression", "Evaluate an expression on the current " |
| 222 | "thread. Displays any returned value " |
| 223 | "with LLDB's default formatting.", |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame] | 224 | "", eCommandProcessMustBePaused | eCommandTryTargetAPILock), |
Kate Stone | 7428a18 | 2016-07-14 22:03:10 +0000 | [diff] [blame] | 225 | IOHandlerDelegate(IOHandlerDelegate::Completion::Expression), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 226 | m_option_group(), m_format_options(eFormatDefault), |
| 227 | m_repl_option(LLDB_OPT_SET_1, false, "repl", 'r', "Drop into REPL", false, |
| 228 | true), |
| 229 | m_command_options(), m_expr_line_count(0), m_expr_lines() { |
| 230 | SetHelpLong( |
| 231 | R"( |
Jim Ingham | d030991 | 2017-07-19 23:25:42 +0000 | [diff] [blame] | 232 | Single and multi-line expressions: |
| 233 | |
| 234 | )" |
| 235 | " The expression provided on the command line must be a complete expression \ |
| 236 | with no newlines. To evaluate a multi-line expression, \ |
| 237 | hit a return after an empty expression, and lldb will enter the multi-line expression editor. \ |
| 238 | Hit return on an empty line to end the multi-line expression." |
| 239 | |
| 240 | R"( |
| 241 | |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 242 | Timeouts: |
| 243 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 244 | )" |
| 245 | " If the expression can be evaluated statically (without running code) then it will be. \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 246 | Otherwise, by default the expression will run on the current thread with a short timeout: \ |
| 247 | currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \ |
| 248 | and resumed with all threads running. You can use the -a option to disable retrying on all \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | threads. You can use the -t option to set a shorter timeout." |
| 250 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 251 | |
| 252 | User defined variables: |
| 253 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 254 | )" |
| 255 | " You can define your own variables for convenience or to be used in subsequent expressions. \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 256 | You define them the same way you would define variables in C. If the first character of \ |
| 257 | your user defined variable is a $, then the variable's value will be available in future \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | expressions, otherwise it will just be available in the current expression." |
| 259 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 260 | |
| 261 | Continuing evaluation after a breakpoint: |
| 262 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 263 | )" |
| 264 | " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 265 | you are done with your investigation, you can either remove the expression execution frames \ |
| 266 | from the stack with \"thread return -x\" or if you are still interested in the expression result \ |
| 267 | you can issue the \"continue\" command and the expression evaluation will complete and the \ |
| 268 | expression result will be available using the \"thread.completed-expression\" key in the thread \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 269 | format." |
Jim Ingham | d030991 | 2017-07-19 23:25:42 +0000 | [diff] [blame] | 270 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 272 | |
| 273 | Examples: |
| 274 | |
| 275 | expr my_struct->a = my_array[3] |
| 276 | expr -f bin -- (index * 8) + 5 |
| 277 | expr unsigned int $foo = 5 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | expr char c[] = \"foo\"; c[0])"); |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 279 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 280 | CommandArgumentEntry arg; |
| 281 | CommandArgumentData expression_arg; |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 283 | // Define the first (and only) variant of this arg. |
| 284 | expression_arg.arg_type = eArgTypeExpression; |
| 285 | expression_arg.arg_repetition = eArgRepeatPlain; |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 286 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 287 | // There is only one variant this argument could be; put it into the argument |
| 288 | // entry. |
| 289 | arg.push_back(expression_arg); |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | // Push the data for the first argument into the m_arguments vector. |
| 292 | m_arguments.push_back(arg); |
| 293 | |
| 294 | // Add the "--format" and "--gdb-format" |
| 295 | m_option_group.Append(&m_format_options, |
| 296 | OptionGroupFormat::OPTION_GROUP_FORMAT | |
| 297 | OptionGroupFormat::OPTION_GROUP_GDB_FMT, |
| 298 | LLDB_OPT_SET_1); |
| 299 | m_option_group.Append(&m_command_options); |
| 300 | m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, |
| 301 | LLDB_OPT_SET_1 | LLDB_OPT_SET_2); |
| 302 | m_option_group.Append(&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3); |
| 303 | m_option_group.Finalize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Eugene Zelenko | c8ecc2a | 2016-02-19 19:33:46 +0000 | [diff] [blame] | 306 | CommandObjectExpression::~CommandObjectExpression() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 308 | Options *CommandObjectExpression::GetOptions() { return &m_option_group; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 309 | |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 310 | int CommandObjectExpression::HandleCompletion(CompletionRequest &request) { |
| 311 | EvaluateExpressionOptions options; |
| 312 | options.SetCoerceToId(m_varobj_options.use_objc); |
| 313 | options.SetLanguage(m_command_options.language); |
| 314 | options.SetExecutionPolicy(lldb_private::eExecutionPolicyNever); |
| 315 | options.SetAutoApplyFixIts(false); |
| 316 | options.SetGenerateDebugInfo(false); |
| 317 | |
| 318 | // We need a valid execution context with a frame pointer for this |
| 319 | // completion, so if we don't have one we should try to make a valid |
| 320 | // execution context. |
| 321 | if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr) |
| 322 | m_interpreter.UpdateExecutionContext(nullptr); |
| 323 | |
| 324 | // This didn't work, so let's get out before we start doing things that |
| 325 | // expect a valid frame pointer. |
| 326 | if (m_interpreter.GetExecutionContext().GetFramePtr() == nullptr) |
| 327 | return 0; |
| 328 | |
| 329 | ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); |
| 330 | |
| 331 | Target *target = exe_ctx.GetTargetPtr(); |
| 332 | |
| 333 | if (!target) |
| 334 | target = GetDummyTarget(); |
| 335 | |
| 336 | if (!target) |
| 337 | return 0; |
| 338 | |
| 339 | unsigned cursor_pos = request.GetRawCursorPos(); |
| 340 | llvm::StringRef code = request.GetRawLine(); |
| 341 | |
| 342 | const std::size_t original_code_size = code.size(); |
| 343 | |
| 344 | // Remove the first token which is 'expr' or some alias/abbreviation of that. |
| 345 | code = llvm::getToken(code).second.ltrim(); |
| 346 | OptionsWithRaw args(code); |
| 347 | code = args.GetRawPart(); |
| 348 | |
| 349 | // The position where the expression starts in the command line. |
| 350 | assert(original_code_size >= code.size()); |
| 351 | std::size_t raw_start = original_code_size - code.size(); |
| 352 | |
| 353 | // Check if the cursor is actually in the expression string, and if not, we |
| 354 | // exit. |
| 355 | // FIXME: We should complete the options here. |
| 356 | if (cursor_pos < raw_start) |
| 357 | return 0; |
| 358 | |
| 359 | // Make the cursor_pos again relative to the start of the code string. |
| 360 | assert(cursor_pos >= raw_start); |
| 361 | cursor_pos -= raw_start; |
| 362 | |
| 363 | auto language = exe_ctx.GetFrameRef().GetLanguage(); |
| 364 | |
| 365 | Status error; |
| 366 | lldb::UserExpressionSP expr(target->GetUserExpressionForLanguage( |
| 367 | code, llvm::StringRef(), language, UserExpression::eResultTypeAny, |
| 368 | options, error)); |
| 369 | if (error.Fail()) |
| 370 | return 0; |
| 371 | |
Raphael Isemann | c11a780 | 2018-08-30 21:26:32 +0000 | [diff] [blame] | 372 | expr->Complete(exe_ctx, request, cursor_pos); |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 373 | return request.GetNumberOfMatches(); |
| 374 | } |
| 375 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 376 | static lldb_private::Status |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | CanBeUsedForElementCountPrinting(ValueObject &valobj) { |
| 378 | CompilerType type(valobj.GetCompilerType()); |
| 379 | CompilerType pointee; |
| 380 | if (!type.IsPointerType(&pointee)) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 381 | return Status("as it does not refer to a pointer"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 382 | if (pointee.IsVoidType()) |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 383 | return Status("as it refers to a pointer to void"); |
| 384 | return Status(); |
Enrico Granata | 520a422 | 2016-04-25 00:52:47 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Raphael Isemann | 4d51a90 | 2018-07-12 22:28:52 +0000 | [diff] [blame] | 387 | bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 388 | Stream *output_stream, |
| 389 | Stream *error_stream, |
| 390 | CommandReturnObject *result) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 391 | // Don't use m_exe_ctx as this might be called asynchronously after the |
| 392 | // command object DoExecute has finished when doing multi-line expression |
| 393 | // that use an input reader... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 394 | ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); |
Greg Clayton | ba7b8e2 | 2013-01-26 23:54:29 +0000 | [diff] [blame] | 395 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 396 | Target *target = exe_ctx.GetTargetPtr(); |
Enrico Granata | b576bba | 2013-01-09 20:12:53 +0000 | [diff] [blame] | 397 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 398 | if (!target) |
| 399 | target = GetDummyTarget(); |
Dawn Perchik | 15663c5 | 2015-07-25 00:19:39 +0000 | [diff] [blame] | 400 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 401 | if (target) { |
| 402 | lldb::ValueObjectSP result_valobj_sp; |
| 403 | bool keep_in_memory = true; |
| 404 | StackFrame *frame = exe_ctx.GetFramePtr(); |
Arnaud A. de Grandmaison | 62e5f4d | 2014-03-22 20:23:26 +0000 | [diff] [blame] | 405 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 406 | EvaluateExpressionOptions options; |
| 407 | options.SetCoerceToId(m_varobj_options.use_objc); |
| 408 | options.SetUnwindOnError(m_command_options.unwind_on_error); |
| 409 | options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints); |
| 410 | options.SetKeepInMemory(keep_in_memory); |
| 411 | options.SetUseDynamic(m_varobj_options.use_dynamic); |
| 412 | options.SetTryAllThreads(m_command_options.try_all_threads); |
| 413 | options.SetDebug(m_command_options.debug); |
| 414 | options.SetLanguage(m_command_options.language); |
| 415 | options.SetExecutionPolicy( |
| 416 | m_command_options.allow_jit |
| 417 | ? EvaluateExpressionOptions::default_execution_policy |
| 418 | : lldb_private::eExecutionPolicyNever); |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 419 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 420 | bool auto_apply_fixits; |
| 421 | if (m_command_options.auto_apply_fixits == eLazyBoolCalculate) |
| 422 | auto_apply_fixits = target->GetEnableAutoApplyFixIts(); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 423 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 424 | auto_apply_fixits = |
| 425 | m_command_options.auto_apply_fixits == eLazyBoolYes ? true : false; |
| 426 | |
| 427 | options.SetAutoApplyFixIts(auto_apply_fixits); |
| 428 | |
| 429 | if (m_command_options.top_level) |
| 430 | options.SetExecutionPolicy(eExecutionPolicyTopLevel); |
| 431 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 432 | // If there is any chance we are going to stop and want to see what went |
| 433 | // wrong with our expression, we should generate debug info |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 434 | if (!m_command_options.ignore_breakpoints || |
| 435 | !m_command_options.unwind_on_error) |
| 436 | options.SetGenerateDebugInfo(true); |
| 437 | |
| 438 | if (m_command_options.timeout > 0) |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 439 | options.SetTimeout(std::chrono::microseconds(m_command_options.timeout)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 440 | else |
Pavel Labath | 43d3541 | 2016-12-06 11:24:51 +0000 | [diff] [blame] | 441 | options.SetTimeout(llvm::None); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 442 | |
| 443 | ExpressionResults success = target->EvaluateExpression( |
| 444 | expr, frame, result_valobj_sp, options, &m_fixed_expression); |
| 445 | |
| 446 | // We only tell you about the FixIt if we applied it. The compiler errors |
| 447 | // will suggest the FixIt if it parsed. |
| 448 | if (error_stream && !m_fixed_expression.empty() && |
| 449 | target->GetEnableNotifyAboutFixIts()) { |
| 450 | if (success == eExpressionCompleted) |
| 451 | error_stream->Printf( |
| 452 | " Fix-it applied, fixed expression was: \n %s\n", |
| 453 | m_fixed_expression.c_str()); |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 454 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 455 | |
| 456 | if (result_valobj_sp) { |
| 457 | Format format = m_format_options.GetFormat(); |
| 458 | |
| 459 | if (result_valobj_sp->GetError().Success()) { |
| 460 | if (format != eFormatVoid) { |
| 461 | if (format != eFormatDefault) |
| 462 | result_valobj_sp->SetFormat(format); |
| 463 | |
| 464 | if (m_varobj_options.elem_count > 0) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 465 | Status error(CanBeUsedForElementCountPrinting(*result_valobj_sp)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 466 | if (error.Fail()) { |
| 467 | result->AppendErrorWithFormat( |
| 468 | "expression cannot be used with --element-count %s\n", |
| 469 | error.AsCString("")); |
| 470 | result->SetStatus(eReturnStatusFailed); |
| 471 | return false; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions( |
| 476 | m_command_options.m_verbosity, format)); |
| 477 | options.SetVariableFormatDisplayLanguage( |
| 478 | result_valobj_sp->GetPreferredDisplayLanguage()); |
| 479 | |
| 480 | result_valobj_sp->Dump(*output_stream, options); |
| 481 | |
| 482 | if (result) |
| 483 | result->SetStatus(eReturnStatusSuccessFinishResult); |
| 484 | } |
| 485 | } else { |
| 486 | if (result_valobj_sp->GetError().GetError() == |
| 487 | UserExpression::kNoResult) { |
| 488 | if (format != eFormatVoid && |
| 489 | m_interpreter.GetDebugger().GetNotifyVoid()) { |
| 490 | error_stream->PutCString("(void)\n"); |
| 491 | } |
| 492 | |
| 493 | if (result) |
| 494 | result->SetStatus(eReturnStatusSuccessFinishResult); |
| 495 | } else { |
| 496 | const char *error_cstr = result_valobj_sp->GetError().AsCString(); |
| 497 | if (error_cstr && error_cstr[0]) { |
| 498 | const size_t error_cstr_len = strlen(error_cstr); |
| 499 | const bool ends_with_newline = |
| 500 | error_cstr[error_cstr_len - 1] == '\n'; |
| 501 | if (strstr(error_cstr, "error:") != error_cstr) |
| 502 | error_stream->PutCString("error: "); |
| 503 | error_stream->Write(error_cstr, error_cstr_len); |
| 504 | if (!ends_with_newline) |
| 505 | error_stream->EOL(); |
| 506 | } else { |
| 507 | error_stream->PutCString("error: unknown error\n"); |
| 508 | } |
| 509 | |
| 510 | if (result) |
| 511 | result->SetStatus(eReturnStatusFailed); |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | } else { |
| 516 | error_stream->Printf("error: invalid execution context for expression\n"); |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | return true; |
| 521 | } |
| 522 | |
| 523 | void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler, |
| 524 | std::string &line) { |
| 525 | io_handler.SetIsDone(true); |
| 526 | // StreamSP output_stream = |
| 527 | // io_handler.GetDebugger().GetAsyncOutputStream(); |
| 528 | // StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream(); |
| 529 | StreamFileSP output_sp(io_handler.GetOutputStreamFile()); |
| 530 | StreamFileSP error_sp(io_handler.GetErrorStreamFile()); |
| 531 | |
| 532 | EvaluateExpression(line.c_str(), output_sp.get(), error_sp.get()); |
| 533 | if (output_sp) |
| 534 | output_sp->Flush(); |
| 535 | if (error_sp) |
| 536 | error_sp->Flush(); |
| 537 | } |
| 538 | |
| 539 | bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler, |
| 540 | StringList &lines) { |
| 541 | // An empty lines is used to indicate the end of input |
| 542 | const size_t num_lines = lines.GetSize(); |
| 543 | if (num_lines > 0 && lines[num_lines - 1].empty()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 544 | // Remove the last empty line from "lines" so it doesn't appear in our |
| 545 | // resulting input and return true to indicate we are done getting lines |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 546 | lines.PopBack(); |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 547 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 548 | } |
| 549 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 552 | void CommandObjectExpression::GetMultilineExpression() { |
| 553 | m_expr_lines.clear(); |
| 554 | m_expr_line_count = 0; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 555 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 556 | Debugger &debugger = GetCommandInterpreter().GetDebugger(); |
| 557 | bool color_prompt = debugger.GetUseColor(); |
| 558 | const bool multiple_lines = true; // Get multiple lines |
| 559 | IOHandlerSP io_handler_sp( |
| 560 | new IOHandlerEditline(debugger, IOHandler::Type::Expression, |
| 561 | "lldb-expr", // Name of input reader for history |
Zachary Turner | 514d8cd | 2016-09-23 18:06:53 +0000 | [diff] [blame] | 562 | llvm::StringRef(), // No prompt |
| 563 | llvm::StringRef(), // Continuation prompt |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 564 | multiple_lines, color_prompt, |
| 565 | 1, // Show line numbers starting at 1 |
| 566 | *this)); |
| 567 | |
| 568 | StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile()); |
| 569 | if (output_sp) { |
| 570 | output_sp->PutCString( |
| 571 | "Enter expressions, then terminate with an empty line to evaluate:\n"); |
| 572 | output_sp->Flush(); |
| 573 | } |
| 574 | debugger.PushIOHandler(io_handler_sp); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Raphael Isemann | 4d51a90 | 2018-07-12 22:28:52 +0000 | [diff] [blame] | 577 | bool CommandObjectExpression::DoExecute(llvm::StringRef command, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 578 | CommandReturnObject &result) { |
| 579 | m_fixed_expression.clear(); |
| 580 | auto exe_ctx = GetCommandInterpreter().GetExecutionContext(); |
| 581 | m_option_group.NotifyOptionParsingStarting(&exe_ctx); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 582 | |
Raphael Isemann | 4d51a90 | 2018-07-12 22:28:52 +0000 | [diff] [blame] | 583 | if (command.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 584 | GetMultilineExpression(); |
| 585 | return result.Succeeded(); |
| 586 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 587 | |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 588 | OptionsWithRaw args(command); |
Raphael Isemann | 4d51a90 | 2018-07-12 22:28:52 +0000 | [diff] [blame] | 589 | llvm::StringRef expr = args.GetRawPart(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 590 | |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 591 | if (args.HasArgs()) { |
| 592 | if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group, exe_ctx)) |
| 593 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 594 | |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 595 | if (m_repl_option.GetOptionValue().GetCurrentValue()) { |
| 596 | Target *target = m_interpreter.GetExecutionContext().GetTargetPtr(); |
| 597 | if (target) { |
| 598 | // Drop into REPL |
| 599 | m_expr_lines.clear(); |
| 600 | m_expr_line_count = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 601 | |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 602 | Debugger &debugger = target->GetDebugger(); |
Johnny Chen | fcd43b7 | 2010-08-13 00:42:30 +0000 | [diff] [blame] | 603 | |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 604 | // Check if the LLDB command interpreter is sitting on top of a REPL |
| 605 | // that launched it... |
| 606 | if (debugger.CheckTopIOHandlerTypes(IOHandler::Type::CommandInterpreter, |
| 607 | IOHandler::Type::REPL)) { |
| 608 | // the LLDB command interpreter is sitting on top of a REPL that |
| 609 | // launched it, so just say the command interpreter is done and |
| 610 | // fall back to the existing REPL |
| 611 | m_interpreter.GetIOHandler(false)->SetIsDone(true); |
| 612 | } else { |
| 613 | // We are launching the REPL on top of the current LLDB command |
| 614 | // interpreter, so just push one |
| 615 | bool initialize = false; |
| 616 | Status repl_error; |
| 617 | REPLSP repl_sp(target->GetREPL(repl_error, m_command_options.language, |
| 618 | nullptr, false)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 619 | |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 620 | if (!repl_sp) { |
| 621 | initialize = true; |
| 622 | repl_sp = target->GetREPL(repl_error, m_command_options.language, |
| 623 | nullptr, true); |
| 624 | if (!repl_error.Success()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 625 | result.SetError(repl_error); |
| 626 | return result.Succeeded(); |
| 627 | } |
| 628 | } |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 629 | |
| 630 | if (repl_sp) { |
| 631 | if (initialize) { |
| 632 | repl_sp->SetCommandOptions(m_command_options); |
| 633 | repl_sp->SetFormatOptions(m_format_options); |
| 634 | repl_sp->SetValueObjectDisplayOptions(m_varobj_options); |
| 635 | } |
| 636 | |
| 637 | IOHandlerSP io_handler_sp(repl_sp->GetIOHandler()); |
| 638 | |
| 639 | io_handler_sp->SetIsDone(false); |
| 640 | |
| 641 | debugger.PushIOHandler(io_handler_sp); |
| 642 | } else { |
| 643 | repl_error.SetErrorStringWithFormat( |
| 644 | "Couldn't create a REPL for %s", |
| 645 | Language::GetNameForLanguageType(m_command_options.language)); |
| 646 | result.SetError(repl_error); |
| 647 | return result.Succeeded(); |
| 648 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 649 | } |
| 650 | } |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 651 | } |
| 652 | // No expression following options |
Raphael Isemann | 4d51a90 | 2018-07-12 22:28:52 +0000 | [diff] [blame] | 653 | else if (expr.empty()) { |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 654 | GetMultilineExpression(); |
| 655 | return result.Succeeded(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
Davide Italiano | 0df817a | 2018-04-13 18:37:14 +0000 | [diff] [blame] | 659 | Target *target = GetSelectedOrDummyTarget(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 660 | if (EvaluateExpression(expr, &(result.GetOutputStream()), |
| 661 | &(result.GetErrorStream()), &result)) { |
Jim Ingham | 981da8d | 2016-10-17 23:59:41 +0000 | [diff] [blame] | 662 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 663 | if (!m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) { |
| 664 | CommandHistory &history = m_interpreter.GetCommandHistory(); |
| 665 | // FIXME: Can we figure out what the user actually typed (e.g. some alias |
| 666 | // for expr???) |
| 667 | // If we can it would be nice to show that. |
| 668 | std::string fixed_command("expression "); |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 669 | if (args.HasArgs()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 670 | // Add in any options that might have been in the original command: |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 671 | fixed_command.append(args.GetArgStringWithDelimiter()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 672 | fixed_command.append(m_fixed_expression); |
Raphael Isemann | 3a0e127 | 2018-07-10 20:17:38 +0000 | [diff] [blame] | 673 | } else |
| 674 | fixed_command.append(m_fixed_expression); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 675 | history.AppendString(fixed_command); |
| 676 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 677 | // Increment statistics to record this expression evaluation success. |
Davide Italiano | 24fff24 | 2018-04-13 18:02:39 +0000 | [diff] [blame] | 678 | target->IncrementStats(StatisticKind::ExpressionSuccessful); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 679 | return true; |
| 680 | } |
| 681 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 682 | // Increment statistics to record this expression evaluation failure. |
Davide Italiano | 24fff24 | 2018-04-13 18:02:39 +0000 | [diff] [blame] | 683 | target->IncrementStats(StatisticKind::ExpressionFailure); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 684 | result.SetStatus(eReturnStatusFailed); |
| 685 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 686 | } |