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 | |
| 10 | #include "CommandObjectExpression.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Value.h" |
Jim Ingham | 6c68fb4 | 2010-09-30 00:54:27 +0000 | [diff] [blame] | 17 | #include "lldb/Core/ValueObjectVariable.h" |
Enrico Granata | 4d93b8c | 2013-09-30 19:11:51 +0000 | [diff] [blame] | 18 | #include "lldb/DataFormatters/ValueObjectPrinter.h" |
Sean Callanan | 30e3397 | 2015-09-03 00:48:23 +0000 | [diff] [blame] | 19 | #include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 20 | #include "lldb/Expression/UserExpression.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Expression/DWARFExpression.h" |
| 22 | #include "lldb/Host/Host.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 23 | #include "lldb/Host/StringConvert.h" |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 24 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 25 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | #include "lldb/Interpreter/CommandReturnObject.h" |
Jim Ingham | 0e0984e | 2015-09-02 01:06:46 +0000 | [diff] [blame] | 27 | #include "lldb/Target/Language.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | #include "lldb/Symbol/ObjectFile.h" |
| 29 | #include "lldb/Symbol/Variable.h" |
| 30 | #include "lldb/Target/Process.h" |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 31 | #include "lldb/Target/StackFrame.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | #include "lldb/Target/Target.h" |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 33 | #include "lldb/Target/Thread.h" |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/STLExtras.h" |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/StringRef.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace lldb; |
| 38 | using namespace lldb_private; |
| 39 | |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 40 | CommandObjectExpression::CommandOptions::CommandOptions () : |
| 41 | OptionGroup() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | |
| 46 | CommandObjectExpression::CommandOptions::~CommandOptions () |
| 47 | { |
| 48 | } |
| 49 | |
Enrico Granata | 4d93b8c | 2013-09-30 19:11:51 +0000 | [diff] [blame] | 50 | static OptionEnumValueElement g_description_verbosity_type[] = |
| 51 | { |
| 52 | { eLanguageRuntimeDescriptionDisplayVerbosityCompact, "compact", "Only show the description string"}, |
| 53 | { eLanguageRuntimeDescriptionDisplayVerbosityFull, "full", "Show the full output, including persistent variable's name and type"}, |
| 54 | { 0, NULL, NULL } |
| 55 | }; |
| 56 | |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 57 | OptionDefinition |
| 58 | CommandObjectExpression::CommandOptions::g_option_table[] = |
| 59 | { |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 60 | { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "all-threads", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Should we run all threads if the execution doesn't complete on one thread."}, |
| 61 | { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "ignore-breakpoints", 'i', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Ignore breakpoint hits while running expressions"}, |
| 62 | { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "timeout", 't', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeUnsignedInteger, "Timeout value (in microseconds) for running the expression."}, |
| 63 | { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "unwind-on-error", 'u', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Clean up program state if the expression causes a crash, or raises a signal. Note, unlike gdb hitting a breakpoint is controlled by another option (-i)."}, |
| 64 | { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "debug", 'g', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "When specified, debug the JIT code by setting a breakpoint on the first instruction and forcing breakpoints to not be ignored (-i0) and no unwinding to happen on error (-u0)."}, |
Dawn Perchik | 15663c5 | 2015-07-25 00:19:39 +0000 | [diff] [blame] | 65 | { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "language", 'l', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeLanguage, "Specifies the Language to use when parsing the expression. If not set the target.language setting is used." }, |
Zachary Turner | d37221d | 2014-07-09 16:31:49 +0000 | [diff] [blame] | 66 | { LLDB_OPT_SET_1, false, "description-verbosity", 'v', OptionParser::eOptionalArgument, NULL, g_description_verbosity_type, 0, eArgTypeDescriptionVerbosity, "How verbose should the output of this expression be, if the object description is asked for."}, |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | |
| 70 | uint32_t |
| 71 | CommandObjectExpression::CommandOptions::GetNumDefinitions () |
| 72 | { |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 73 | return llvm::array_lengthof(g_option_table); |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 76 | Error |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 77 | CommandObjectExpression::CommandOptions::SetOptionValue (CommandInterpreter &interpreter, |
| 78 | uint32_t option_idx, |
| 79 | const char *option_arg) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | { |
| 81 | Error error; |
| 82 | |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 83 | const int short_option = g_option_table[option_idx].short_option; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | |
| 85 | switch (short_option) |
| 86 | { |
Dawn Perchik | 15663c5 | 2015-07-25 00:19:39 +0000 | [diff] [blame] | 87 | case 'l': |
Jim Ingham | 0e0984e | 2015-09-02 01:06:46 +0000 | [diff] [blame] | 88 | language = Language::GetLanguageTypeFromString (option_arg); |
Dawn Perchik | 15663c5 | 2015-07-25 00:19:39 +0000 | [diff] [blame] | 89 | if (language == eLanguageTypeUnknown) |
| 90 | error.SetErrorStringWithFormat ("unknown language type: '%s' for expression", option_arg); |
| 91 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 93 | case 'a': |
| 94 | { |
| 95 | bool success; |
| 96 | bool result; |
| 97 | result = Args::StringToBoolean(option_arg, true, &success); |
| 98 | if (!success) |
| 99 | error.SetErrorStringWithFormat("invalid all-threads value setting: \"%s\"", option_arg); |
| 100 | else |
| 101 | try_all_threads = result; |
| 102 | } |
Jim Ingham | 6c68fb4 | 2010-09-30 00:54:27 +0000 | [diff] [blame] | 103 | break; |
Jim Ingham | 399f1ca | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 104 | |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 105 | case 'i': |
| 106 | { |
| 107 | bool success; |
| 108 | bool tmp_value = Args::StringToBoolean(option_arg, true, &success); |
| 109 | if (success) |
| 110 | ignore_breakpoints = tmp_value; |
| 111 | else |
| 112 | error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg); |
| 113 | break; |
| 114 | } |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 115 | case 't': |
| 116 | { |
| 117 | bool success; |
| 118 | uint32_t result; |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 119 | result = StringConvert::ToUInt32(option_arg, 0, 0, &success); |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 120 | if (success) |
| 121 | timeout = result; |
| 122 | else |
| 123 | error.SetErrorStringWithFormat ("invalid timeout setting \"%s\"", option_arg); |
| 124 | } |
| 125 | break; |
| 126 | |
Jim Ingham | 399f1ca | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 127 | case 'u': |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 128 | { |
| 129 | bool success; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 130 | bool tmp_value = Args::StringToBoolean(option_arg, true, &success); |
| 131 | if (success) |
| 132 | unwind_on_error = tmp_value; |
| 133 | else |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 134 | error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg); |
Sean Callanan | 3bfdaa2 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 135 | break; |
| 136 | } |
Enrico Granata | 4d93b8c | 2013-09-30 19:11:51 +0000 | [diff] [blame] | 137 | |
| 138 | case 'v': |
| 139 | if (!option_arg) |
| 140 | { |
| 141 | m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull; |
| 142 | break; |
| 143 | } |
| 144 | m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error); |
| 145 | if (!error.Success()) |
| 146 | error.SetErrorStringWithFormat ("unrecognized value for description-verbosity '%s'", option_arg); |
| 147 | break; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 148 | |
| 149 | case 'g': |
| 150 | debug = true; |
| 151 | unwind_on_error = false; |
| 152 | ignore_breakpoints = false; |
| 153 | break; |
| 154 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | default: |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 156 | error.SetErrorStringWithFormat("invalid short option character '%c'", short_option); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | |
| 160 | return error; |
| 161 | } |
| 162 | |
| 163 | void |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 164 | CommandObjectExpression::CommandOptions::OptionParsingStarting (CommandInterpreter &interpreter) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | { |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 166 | Process *process = interpreter.GetExecutionContext().GetProcessPtr(); |
| 167 | if (process != NULL) |
| 168 | { |
| 169 | ignore_breakpoints = process->GetIgnoreBreakpointsInExpressions(); |
| 170 | unwind_on_error = process->GetUnwindOnErrorInExpressions(); |
| 171 | } |
| 172 | else |
| 173 | { |
Greg Clayton | fc03f8f | 2014-03-25 18:47:07 +0000 | [diff] [blame] | 174 | ignore_breakpoints = true; |
Jim Ingham | 184e981 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 175 | unwind_on_error = true; |
| 176 | } |
| 177 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 178 | show_summary = true; |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 179 | try_all_threads = true; |
| 180 | timeout = 0; |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 181 | debug = false; |
Dawn Perchik | 15663c5 | 2015-07-25 00:19:39 +0000 | [diff] [blame] | 182 | language = eLanguageTypeUnknown; |
Enrico Granata | 4d93b8c | 2013-09-30 19:11:51 +0000 | [diff] [blame] | 183 | m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 186 | const OptionDefinition* |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | CommandObjectExpression::CommandOptions::GetDefinitions () |
| 188 | { |
| 189 | return g_option_table; |
| 190 | } |
| 191 | |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 192 | CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) : |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 193 | CommandObjectRaw (interpreter, |
| 194 | "expression", |
Dawn Perchik | 15663c5 | 2015-07-25 00:19:39 +0000 | [diff] [blame] | 195 | "Evaluate an expression in the current program context, using user defined variables and variables currently in scope.", |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 196 | NULL, |
Enrico Granata | e87764f | 2015-05-27 05:04:35 +0000 | [diff] [blame] | 197 | eCommandProcessMustBePaused | eCommandTryTargetAPILock), |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 198 | IOHandlerDelegate (IOHandlerDelegate::Completion::Expression), |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 199 | m_option_group (interpreter), |
| 200 | m_format_options (eFormatDefault), |
| 201 | m_command_options (), |
Johnny Chen | f7edb1c | 2010-09-30 18:30:25 +0000 | [diff] [blame] | 202 | m_expr_line_count (0), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 203 | m_expr_lines () |
| 204 | { |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 205 | SetHelpLong( |
| 206 | R"( |
| 207 | Timeouts: |
| 208 | |
| 209 | )" " If the expression can be evaluated statically (without running code) then it will be. \ |
| 210 | Otherwise, by default the expression will run on the current thread with a short timeout: \ |
| 211 | currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \ |
| 212 | and resumed with all threads running. You can use the -a option to disable retrying on all \ |
| 213 | threads. You can use the -t option to set a shorter timeout." R"( |
| 214 | |
| 215 | User defined variables: |
| 216 | |
| 217 | )" " You can define your own variables for convenience or to be used in subsequent expressions. \ |
| 218 | You define them the same way you would define variables in C. If the first character of \ |
| 219 | your user defined variable is a $, then the variable's value will be available in future \ |
| 220 | expressions, otherwise it will just be available in the current expression." R"( |
| 221 | |
| 222 | Continuing evaluation after a breakpoint: |
| 223 | |
| 224 | )" " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \ |
| 225 | you are done with your investigation, you can either remove the expression execution frames \ |
| 226 | from the stack with \"thread return -x\" or if you are still interested in the expression result \ |
| 227 | you can issue the \"continue\" command and the expression evaluation will complete and the \ |
| 228 | expression result will be available using the \"thread.completed-expression\" key in the thread \ |
| 229 | format." R"( |
| 230 | |
| 231 | Examples: |
| 232 | |
| 233 | expr my_struct->a = my_array[3] |
| 234 | expr -f bin -- (index * 8) + 5 |
| 235 | expr unsigned int $foo = 5 |
| 236 | expr char c[] = \"foo\"; c[0])" |
| 237 | ); |
Caroline Tice | 405fe67 | 2010-10-04 22:28:36 +0000 | [diff] [blame] | 238 | |
| 239 | CommandArgumentEntry arg; |
| 240 | CommandArgumentData expression_arg; |
| 241 | |
| 242 | // Define the first (and only) variant of this arg. |
| 243 | expression_arg.arg_type = eArgTypeExpression; |
| 244 | expression_arg.arg_repetition = eArgRepeatPlain; |
| 245 | |
| 246 | // There is only one variant this argument could be; put it into the argument entry. |
| 247 | arg.push_back (expression_arg); |
| 248 | |
| 249 | // Push the data for the first argument into the m_arguments vector. |
| 250 | m_arguments.push_back (arg); |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 251 | |
Greg Clayton | 5009f9d | 2011-10-27 17:55:14 +0000 | [diff] [blame] | 252 | // Add the "--format" and "--gdb-format" |
| 253 | m_option_group.Append (&m_format_options, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1); |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 254 | m_option_group.Append (&m_command_options); |
Enrico Granata | b576bba | 2013-01-09 20:12:53 +0000 | [diff] [blame] | 255 | m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1 | LLDB_OPT_SET_2); |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 256 | m_option_group.Finalize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | CommandObjectExpression::~CommandObjectExpression () |
| 260 | { |
| 261 | } |
| 262 | |
| 263 | Options * |
| 264 | CommandObjectExpression::GetOptions () |
| 265 | { |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 266 | return &m_option_group; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 269 | bool |
Greg Clayton | 1d3afba | 2010-10-05 00:00:42 +0000 | [diff] [blame] | 270 | CommandObjectExpression::EvaluateExpression |
| 271 | ( |
| 272 | const char *expr, |
Caroline Tice | 6e8dc33 | 2011-06-13 20:20:29 +0000 | [diff] [blame] | 273 | Stream *output_stream, |
| 274 | Stream *error_stream, |
Greg Clayton | 1d3afba | 2010-10-05 00:00:42 +0000 | [diff] [blame] | 275 | CommandReturnObject *result |
| 276 | ) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | { |
Greg Clayton | ba7b8e2 | 2013-01-26 23:54:29 +0000 | [diff] [blame] | 278 | // Don't use m_exe_ctx as this might be called asynchronously |
| 279 | // after the command object DoExecute has finished when doing |
| 280 | // multi-line expression that use an input reader... |
| 281 | ExecutionContext exe_ctx (m_interpreter.GetExecutionContext()); |
| 282 | |
| 283 | Target *target = exe_ctx.GetTargetPtr(); |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 284 | |
| 285 | if (!target) |
Jim Ingham | 893c932 | 2014-11-22 01:42:44 +0000 | [diff] [blame] | 286 | target = GetDummyTarget(); |
Sean Callanan | c0a6e06 | 2011-10-27 21:22:25 +0000 | [diff] [blame] | 287 | |
Greg Clayton | c14ee32 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 288 | if (target) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 289 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 290 | lldb::ValueObjectSP result_valobj_sp; |
Sean Callanan | 92adcac | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 291 | bool keep_in_memory = true; |
Dawn Perchik | 009d110 | 2015-09-04 01:02:30 +0000 | [diff] [blame] | 292 | StackFrame *frame = exe_ctx.GetFramePtr(); |
Enrico Granata | b576bba | 2013-01-09 20:12:53 +0000 | [diff] [blame] | 293 | |
Jim Ingham | 35e1bda | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 294 | EvaluateExpressionOptions options; |
Jim Ingham | 6fbc48b | 2013-11-07 00:11:47 +0000 | [diff] [blame] | 295 | options.SetCoerceToId(m_varobj_options.use_objc); |
| 296 | options.SetUnwindOnError(m_command_options.unwind_on_error); |
| 297 | options.SetIgnoreBreakpoints (m_command_options.ignore_breakpoints); |
| 298 | options.SetKeepInMemory(keep_in_memory); |
| 299 | options.SetUseDynamic(m_varobj_options.use_dynamic); |
| 300 | options.SetTryAllThreads(m_command_options.try_all_threads); |
| 301 | options.SetDebug(m_command_options.debug); |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 302 | |
Dawn Perchik | 009d110 | 2015-09-04 01:02:30 +0000 | [diff] [blame] | 303 | // If the language was not specified in the expression command, |
| 304 | // set it to the language in the target's properties if |
| 305 | // specified, else default to the langage for the frame. |
Dawn Perchik | 15663c5 | 2015-07-25 00:19:39 +0000 | [diff] [blame] | 306 | if (m_command_options.language != eLanguageTypeUnknown) |
| 307 | options.SetLanguage(m_command_options.language); |
Dawn Perchik | 009d110 | 2015-09-04 01:02:30 +0000 | [diff] [blame] | 308 | else if (target->GetLanguage() != eLanguageTypeUnknown) |
Dawn Perchik | 15663c5 | 2015-07-25 00:19:39 +0000 | [diff] [blame] | 309 | options.SetLanguage(target->GetLanguage()); |
Dawn Perchik | 009d110 | 2015-09-04 01:02:30 +0000 | [diff] [blame] | 310 | else if (frame) |
| 311 | options.SetLanguage(frame->GetLanguage()); |
Dawn Perchik | 15663c5 | 2015-07-25 00:19:39 +0000 | [diff] [blame] | 312 | |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 313 | // If there is any chance we are going to stop and want to see |
| 314 | // what went wrong with our expression, we should generate debug info |
| 315 | if (!m_command_options.ignore_breakpoints || |
| 316 | !m_command_options.unwind_on_error) |
| 317 | options.SetGenerateDebugInfo(true); |
| 318 | |
Greg Clayton | 62afb9f | 2013-11-04 19:35:17 +0000 | [diff] [blame] | 319 | if (m_command_options.timeout > 0) |
| 320 | options.SetTimeoutUsec(m_command_options.timeout); |
Jim Ingham | 6f78f38 | 2014-01-17 20:09:23 +0000 | [diff] [blame] | 321 | else |
| 322 | options.SetTimeoutUsec(0); |
Arnaud A. de Grandmaison | 62e5f4d | 2014-03-22 20:23:26 +0000 | [diff] [blame] | 323 | |
Dawn Perchik | 009d110 | 2015-09-04 01:02:30 +0000 | [diff] [blame] | 324 | target->EvaluateExpression(expr, frame, result_valobj_sp, options); |
Sean Callanan | 4b388c9 | 2013-07-30 19:54:09 +0000 | [diff] [blame] | 325 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 326 | if (result_valobj_sp) |
| 327 | { |
Sean Callanan | bf154da | 2012-08-08 17:35:10 +0000 | [diff] [blame] | 328 | Format format = m_format_options.GetFormat(); |
| 329 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 330 | if (result_valobj_sp->GetError().Success()) |
| 331 | { |
Sean Callanan | bf154da | 2012-08-08 17:35:10 +0000 | [diff] [blame] | 332 | if (format != eFormatVoid) |
| 333 | { |
| 334 | if (format != eFormatDefault) |
| 335 | result_valobj_sp->SetFormat (format); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 336 | |
Enrico Granata | 4d93b8c | 2013-09-30 19:11:51 +0000 | [diff] [blame] | 337 | DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(m_command_options.m_verbosity,format)); |
Enrico Granata | 73e8c4d | 2015-10-07 02:36:35 +0000 | [diff] [blame^] | 338 | options.SetVariableFormatDisplayLanguage(result_valobj_sp->GetPreferredDisplayLanguage()); |
Enrico Granata | b576bba | 2013-01-09 20:12:53 +0000 | [diff] [blame] | 339 | |
Enrico Granata | 4d93b8c | 2013-09-30 19:11:51 +0000 | [diff] [blame] | 340 | result_valobj_sp->Dump(*output_stream,options); |
| 341 | |
Sean Callanan | bf154da | 2012-08-08 17:35:10 +0000 | [diff] [blame] | 342 | if (result) |
| 343 | result->SetStatus (eReturnStatusSuccessFinishResult); |
| 344 | } |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 345 | } |
| 346 | else |
| 347 | { |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 348 | if (result_valobj_sp->GetError().GetError() == UserExpression::kNoResult) |
Greg Clayton | 5fd0590 | 2011-06-24 22:31:10 +0000 | [diff] [blame] | 349 | { |
Sean Callanan | bcf897f | 2012-08-09 18:18:47 +0000 | [diff] [blame] | 350 | if (format != eFormatVoid && m_interpreter.GetDebugger().GetNotifyVoid()) |
Sean Callanan | bf154da | 2012-08-08 17:35:10 +0000 | [diff] [blame] | 351 | { |
Sean Callanan | bcf897f | 2012-08-09 18:18:47 +0000 | [diff] [blame] | 352 | error_stream->PutCString("(void)\n"); |
Sean Callanan | bf154da | 2012-08-08 17:35:10 +0000 | [diff] [blame] | 353 | } |
Sean Callanan | bcf897f | 2012-08-09 18:18:47 +0000 | [diff] [blame] | 354 | |
| 355 | if (result) |
| 356 | result->SetStatus (eReturnStatusSuccessFinishResult); |
Greg Clayton | 5fd0590 | 2011-06-24 22:31:10 +0000 | [diff] [blame] | 357 | } |
| 358 | else |
| 359 | { |
Sean Callanan | bccce81 | 2011-08-23 21:20:51 +0000 | [diff] [blame] | 360 | const char *error_cstr = result_valobj_sp->GetError().AsCString(); |
| 361 | if (error_cstr && error_cstr[0]) |
| 362 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 363 | const size_t error_cstr_len = strlen (error_cstr); |
Sean Callanan | bccce81 | 2011-08-23 21:20:51 +0000 | [diff] [blame] | 364 | const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n'; |
| 365 | if (strstr(error_cstr, "error:") != error_cstr) |
| 366 | error_stream->PutCString ("error: "); |
| 367 | error_stream->Write(error_cstr, error_cstr_len); |
| 368 | if (!ends_with_newline) |
| 369 | error_stream->EOL(); |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | error_stream->PutCString ("error: unknown error\n"); |
| 374 | } |
| 375 | |
| 376 | if (result) |
| 377 | result->SetStatus (eReturnStatusFailed); |
Greg Clayton | 5fd0590 | 2011-06-24 22:31:10 +0000 | [diff] [blame] | 378 | } |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 381 | } |
| 382 | else |
| 383 | { |
Caroline Tice | 6e8dc33 | 2011-06-13 20:20:29 +0000 | [diff] [blame] | 384 | error_stream->Printf ("error: invalid execution context for expression\n"); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 385 | return false; |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 386 | } |
Sean Callanan | d1e5b43 | 2010-08-12 01:56:52 +0000 | [diff] [blame] | 387 | |
Sean Callanan | ebf7707 | 2010-07-23 00:16:21 +0000 | [diff] [blame] | 388 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 391 | void |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 392 | CommandObjectExpression::IOHandlerInputComplete (IOHandler &io_handler, std::string &line) |
| 393 | { |
| 394 | io_handler.SetIsDone(true); |
| 395 | // StreamSP output_stream = io_handler.GetDebugger().GetAsyncOutputStream(); |
| 396 | // StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream(); |
| 397 | StreamFileSP output_sp(io_handler.GetOutputStreamFile()); |
| 398 | StreamFileSP error_sp(io_handler.GetErrorStreamFile()); |
| 399 | |
| 400 | EvaluateExpression (line.c_str(), |
| 401 | output_sp.get(), |
| 402 | error_sp.get()); |
| 403 | if (output_sp) |
| 404 | output_sp->Flush(); |
| 405 | if (error_sp) |
| 406 | error_sp->Flush(); |
| 407 | } |
| 408 | |
| 409 | LineStatus |
| 410 | CommandObjectExpression::IOHandlerLinesUpdated (IOHandler &io_handler, |
| 411 | StringList &lines, |
| 412 | uint32_t line_idx, |
| 413 | Error &error) |
| 414 | { |
| 415 | if (line_idx == UINT32_MAX) |
| 416 | { |
| 417 | // Remove the last line from "lines" so it doesn't appear |
| 418 | // in our final expression |
| 419 | lines.PopBack(); |
| 420 | error.Clear(); |
| 421 | return LineStatus::Done; |
| 422 | } |
| 423 | else if (line_idx + 1 == lines.GetSize()) |
| 424 | { |
| 425 | // The last line was edited, if this line is empty, then we are done |
| 426 | // getting our multiple lines. |
| 427 | if (lines[line_idx].empty()) |
| 428 | return LineStatus::Done; |
| 429 | } |
| 430 | return LineStatus::Success; |
| 431 | } |
| 432 | |
Greg Clayton | cf28a8b | 2014-03-13 23:42:30 +0000 | [diff] [blame] | 433 | void |
| 434 | CommandObjectExpression::GetMultilineExpression () |
| 435 | { |
| 436 | m_expr_lines.clear(); |
| 437 | m_expr_line_count = 0; |
| 438 | |
| 439 | Debugger &debugger = GetCommandInterpreter().GetDebugger(); |
Kate Stone | e30f11d | 2014-11-17 19:06:59 +0000 | [diff] [blame] | 440 | bool color_prompt = debugger.GetUseColor(); |
Greg Clayton | cf28a8b | 2014-03-13 23:42:30 +0000 | [diff] [blame] | 441 | const bool multiple_lines = true; // Get multiple lines |
| 442 | IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger, |
Kate Stone | e30f11d | 2014-11-17 19:06:59 +0000 | [diff] [blame] | 443 | IOHandler::Type::Expression, |
Greg Clayton | cf28a8b | 2014-03-13 23:42:30 +0000 | [diff] [blame] | 444 | "lldb-expr", // Name of input reader for history |
| 445 | NULL, // No prompt |
Kate Stone | e30f11d | 2014-11-17 19:06:59 +0000 | [diff] [blame] | 446 | NULL, // Continuation prompt |
Greg Clayton | cf28a8b | 2014-03-13 23:42:30 +0000 | [diff] [blame] | 447 | multiple_lines, |
Kate Stone | e30f11d | 2014-11-17 19:06:59 +0000 | [diff] [blame] | 448 | color_prompt, |
Greg Clayton | cf28a8b | 2014-03-13 23:42:30 +0000 | [diff] [blame] | 449 | 1, // Show line numbers starting at 1 |
| 450 | *this)); |
| 451 | |
| 452 | StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile()); |
| 453 | if (output_sp) |
| 454 | { |
| 455 | output_sp->PutCString("Enter expressions, then terminate with an empty line to evaluate:\n"); |
| 456 | output_sp->Flush(); |
| 457 | } |
| 458 | debugger.PushIOHandler(io_handler_sp); |
| 459 | } |
| 460 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 461 | bool |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 462 | CommandObjectExpression::DoExecute |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 463 | ( |
| 464 | const char *command, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 465 | CommandReturnObject &result |
| 466 | ) |
| 467 | { |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 468 | m_option_group.NotifyOptionParsingStarting(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 469 | |
| 470 | const char * expr = NULL; |
| 471 | |
| 472 | if (command[0] == '\0') |
| 473 | { |
Greg Clayton | cf28a8b | 2014-03-13 23:42:30 +0000 | [diff] [blame] | 474 | GetMultilineExpression (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 475 | return result.Succeeded(); |
| 476 | } |
| 477 | |
| 478 | if (command[0] == '-') |
| 479 | { |
| 480 | // We have some options and these options MUST end with --. |
| 481 | const char *end_options = NULL; |
| 482 | const char *s = command; |
| 483 | while (s && s[0]) |
| 484 | { |
| 485 | end_options = ::strstr (s, "--"); |
| 486 | if (end_options) |
| 487 | { |
| 488 | end_options += 2; // Get past the "--" |
| 489 | if (::isspace (end_options[0])) |
| 490 | { |
| 491 | expr = end_options; |
| 492 | while (::isspace (*expr)) |
| 493 | ++expr; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | s = end_options; |
| 498 | } |
| 499 | |
| 500 | if (end_options) |
| 501 | { |
Pavel Labath | 00b7f95 | 2015-03-02 12:46:22 +0000 | [diff] [blame] | 502 | Args args (llvm::StringRef(command, end_options - command)); |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 503 | if (!ParseOptions (args, result)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 504 | return false; |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 505 | |
Greg Clayton | 1deb796 | 2011-10-25 06:44:01 +0000 | [diff] [blame] | 506 | Error error (m_option_group.NotifyOptionParsingFinished()); |
Greg Clayton | f6b8b58 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 507 | if (error.Fail()) |
| 508 | { |
| 509 | result.AppendError (error.AsCString()); |
| 510 | result.SetStatus (eReturnStatusFailed); |
| 511 | return false; |
| 512 | } |
Greg Clayton | cf28a8b | 2014-03-13 23:42:30 +0000 | [diff] [blame] | 513 | |
| 514 | // No expression following options |
Greg Clayton | 05da458c | 2014-03-13 23:48:40 +0000 | [diff] [blame] | 515 | if (expr == NULL || expr[0] == '\0') |
Greg Clayton | cf28a8b | 2014-03-13 23:42:30 +0000 | [diff] [blame] | 516 | { |
| 517 | GetMultilineExpression (); |
| 518 | return result.Succeeded(); |
| 519 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 523 | if (expr == NULL) |
| 524 | expr = command; |
Sean Callanan | 16ad5fa | 2010-06-24 00:16:27 +0000 | [diff] [blame] | 525 | |
Caroline Tice | 6e8dc33 | 2011-06-13 20:20:29 +0000 | [diff] [blame] | 526 | if (EvaluateExpression (expr, &(result.GetOutputStream()), &(result.GetErrorStream()), &result)) |
Johnny Chen | fcd43b7 | 2010-08-13 00:42:30 +0000 | [diff] [blame] | 527 | return true; |
| 528 | |
| 529 | result.SetStatus (eReturnStatusFailed); |
| 530 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 531 | } |
| 532 | |