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