blob: 01ca311115556cfed86b9942bface0b1a386e8d3 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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 Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000013#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/StringRef.h"
15
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016// Project includes
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000017#include "CommandObjectExpression.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h"
19#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Value.h"
Jim Ingham6c68fb42010-09-30 00:54:27 +000021#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata4d93b8c2013-09-30 19:11:51 +000022#include "lldb/DataFormatters/ValueObjectPrinter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Expression/DWARFExpression.h"
Sean Callananf2bd5c32015-10-20 00:55:21 +000024#include "lldb/Expression/REPL.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000025#include "lldb/Expression/UserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Host/Host.h"
Zachary Turner3eb2b442017-03-22 23:33:16 +000027#include "lldb/Host/OptionParser.h"
Greg Clayton66111032010-06-23 01:19:29 +000028#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Interpreter/CommandReturnObject.h"
30#include "lldb/Symbol/ObjectFile.h"
31#include "lldb/Symbol/Variable.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000032#include "lldb/Target/Language.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Target/Process.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000034#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Target/Target.h"
Greg Clayton7260f622011-04-18 08:33:37 +000036#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
38using namespace lldb;
39using namespace lldb_private;
40
Kate Stoneb9c1b512016-09-06 20:57:50 +000041CommandObjectExpression::CommandOptions::CommandOptions() : OptionGroup() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000043CommandObjectExpression::CommandOptions::~CommandOptions() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044
Kate Stoneb9c1b512016-09-06 20:57:50 +000045static 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 Granata4d93b8c2013-09-30 19:11:51 +000051
Zachary Turner1f0f5b52016-09-22 20:22:55 +000052static OptionDefinition g_expression_options[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 // clang-format off
Kate Stoneac9c3a62016-08-26 23:28:47 +000054 {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."},
Sean Callanan0354a682016-10-11 16:57:21 +000065 {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 "
66 "context. Allows declaration of persistent, top-level entities without a $ prefix."},
Kate Stoneac9c3a62016-08-26 23:28:47 +000067 {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 Stoneb9c1b512016-09-06 20:57:50 +000069 // clang-format on
Greg Clayton1deb7962011-10-25 06:44:01 +000070};
71
Zachary Turner97206d52017-05-12 04:51:55 +000072Status CommandObjectExpression::CommandOptions::SetOptionValue(
Zachary Turner8cef4b02016-09-23 17:48:13 +000073 uint32_t option_idx, llvm::StringRef option_arg,
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 ExecutionContext *execution_context) {
Zachary Turner97206d52017-05-12 04:51:55 +000075 Status error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076
Zachary Turner1f0f5b52016-09-22 20:22:55 +000077 const int short_option = GetDefinitions()[option_idx].short_option;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 switch (short_option) {
80 case 'l':
81 language = Language::GetLanguageTypeFromString(option_arg);
82 if (language == eLanguageTypeUnknown)
83 error.SetErrorStringWithFormat(
Zachary Turner8cef4b02016-09-23 17:48:13 +000084 "unknown language type: '%s' for expression",
85 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 case 'a': {
89 bool success;
90 bool result;
Zachary Turner8cef4b02016-09-23 17:48:13 +000091 result = Args::StringToBoolean(option_arg, true, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 if (!success)
93 error.SetErrorStringWithFormat(
Zachary Turner8cef4b02016-09-23 17:48:13 +000094 "invalid all-threads value setting: \"%s\"",
95 option_arg.str().c_str());
Jim Ingham184e9812013-01-15 02:47:48 +000096 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 try_all_threads = result;
98 } break;
99
100 case 'i': {
101 bool success;
Zachary Turner8cef4b02016-09-23 17:48:13 +0000102 bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 if (success)
104 ignore_breakpoints = tmp_value;
105 else
106 error.SetErrorStringWithFormat(
Zachary Turner8cef4b02016-09-23 17:48:13 +0000107 "could not convert \"%s\" to a boolean value.",
108 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 break;
110 }
111
112 case 'j': {
113 bool success;
Zachary Turner8cef4b02016-09-23 17:48:13 +0000114 bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 if (success)
116 allow_jit = tmp_value;
117 else
118 error.SetErrorStringWithFormat(
Zachary Turner8cef4b02016-09-23 17:48:13 +0000119 "could not convert \"%s\" to a boolean value.",
120 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 break;
122 }
123
Zachary Turner8cef4b02016-09-23 17:48:13 +0000124 case 't':
125 if (option_arg.getAsInteger(0, timeout)) {
126 timeout = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 error.SetErrorStringWithFormat("invalid timeout setting \"%s\"",
Zachary Turner8cef4b02016-09-23 17:48:13 +0000128 option_arg.str().c_str());
129 }
130 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131
132 case 'u': {
133 bool success;
Zachary Turner8cef4b02016-09-23 17:48:13 +0000134 bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 if (success)
136 unwind_on_error = tmp_value;
137 else
138 error.SetErrorStringWithFormat(
Zachary Turner8cef4b02016-09-23 17:48:13 +0000139 "could not convert \"%s\" to a boolean value.",
140 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 break;
142 }
143
144 case 'v':
Enrico Granata543a26e2016-09-26 21:36:17 +0000145 if (option_arg.empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull;
147 break;
Jim Ingham184e9812013-01-15 02:47:48 +0000148 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 m_verbosity =
150 (LanguageRuntimeDescriptionDisplayVerbosity)Args::StringToOptionEnum(
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000151 option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 if (!error.Success())
153 error.SetErrorStringWithFormat(
Zachary Turner8cef4b02016-09-23 17:48:13 +0000154 "unrecognized value for description-verbosity '%s'",
155 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 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 Turner8cef4b02016-09-23 17:48:13 +0000170 bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 if (success)
172 auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo;
173 else
174 error.SetErrorStringWithFormat(
Zachary Turner8cef4b02016-09-23 17:48:13 +0000175 "could not convert \"%s\" to a boolean value.",
176 option_arg.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 break;
178 }
179
180 default:
181 error.SetErrorStringWithFormat("invalid short option character '%c'",
182 short_option);
183 break;
184 }
185
186 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187}
188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189void 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 Lattner30fdc8d2010-06-08 16:52:24 +0000210}
211
Zachary Turner1f0f5b52016-09-22 20:22:55 +0000212llvm::ArrayRef<OptionDefinition>
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213CommandObjectExpression::CommandOptions::GetDefinitions() {
Zachary Turner70602432016-09-22 21:06:13 +0000214 return llvm::makeArrayRef(g_expression_options);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215}
216
217CommandObjectExpression::CommandObjectExpression(
218 CommandInterpreter &interpreter)
Kate Stone7428a182016-07-14 22:03:10 +0000219 : CommandObjectRaw(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 interpreter, "expression", "Evaluate an expression on the current "
221 "thread. Displays any returned value "
222 "with LLDB's default formatting.",
Zachary Turnera4496982016-10-05 21:14:38 +0000223 "", eCommandProcessMustBePaused | eCommandTryTargetAPILock),
Kate Stone7428a182016-07-14 22:03:10 +0000224 IOHandlerDelegate(IOHandlerDelegate::Completion::Expression),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 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"(
Jim Inghamd0309912017-07-19 23:25:42 +0000231Single and multi-line expressions:
232
233)"
234 " The expression provided on the command line must be a complete expression \
235with no newlines. To evaluate a multi-line expression, \
236hit a return after an empty expression, and lldb will enter the multi-line expression editor. \
237Hit return on an empty line to end the multi-line expression."
238
239 R"(
240
Kate Stoneea671fb2015-07-14 05:48:36 +0000241Timeouts:
242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243)"
244 " If the expression can be evaluated statically (without running code) then it will be. \
Kate Stoneea671fb2015-07-14 05:48:36 +0000245Otherwise, by default the expression will run on the current thread with a short timeout: \
246currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \
247and resumed with all threads running. You can use the -a option to disable retrying on all \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248threads. You can use the -t option to set a shorter timeout."
249 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000250
251User defined variables:
252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253)"
254 " You can define your own variables for convenience or to be used in subsequent expressions. \
Kate Stoneea671fb2015-07-14 05:48:36 +0000255You define them the same way you would define variables in C. If the first character of \
256your user defined variable is a $, then the variable's value will be available in future \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257expressions, otherwise it will just be available in the current expression."
258 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000259
260Continuing evaluation after a breakpoint:
261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262)"
263 " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \
Kate Stoneea671fb2015-07-14 05:48:36 +0000264you are done with your investigation, you can either remove the expression execution frames \
265from the stack with \"thread return -x\" or if you are still interested in the expression result \
266you can issue the \"continue\" command and the expression evaluation will complete and the \
267expression result will be available using the \"thread.completed-expression\" key in the thread \
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268format."
Jim Inghamd0309912017-07-19 23:25:42 +0000269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 R"(
Kate Stoneea671fb2015-07-14 05:48:36 +0000271
272Examples:
273
274 expr my_struct->a = my_array[3]
275 expr -f bin -- (index * 8) + 5
276 expr unsigned int $foo = 5
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 expr char c[] = \"foo\"; c[0])");
Caroline Tice405fe672010-10-04 22:28:36 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 CommandArgumentEntry arg;
280 CommandArgumentData expression_arg;
Caroline Tice405fe672010-10-04 22:28:36 +0000281
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282 // Define the first (and only) variant of this arg.
283 expression_arg.arg_type = eArgTypeExpression;
284 expression_arg.arg_repetition = eArgRepeatPlain;
Caroline Tice405fe672010-10-04 22:28:36 +0000285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 // There is only one variant this argument could be; put it into the argument
287 // entry.
288 arg.push_back(expression_arg);
Caroline Tice405fe672010-10-04 22:28:36 +0000289
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 // Push the data for the first argument into the m_arguments vector.
291 m_arguments.push_back(arg);
292
293 // Add the "--format" and "--gdb-format"
294 m_option_group.Append(&m_format_options,
295 OptionGroupFormat::OPTION_GROUP_FORMAT |
296 OptionGroupFormat::OPTION_GROUP_GDB_FMT,
297 LLDB_OPT_SET_1);
298 m_option_group.Append(&m_command_options);
299 m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL,
300 LLDB_OPT_SET_1 | LLDB_OPT_SET_2);
301 m_option_group.Append(&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3);
302 m_option_group.Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303}
304
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000305CommandObjectExpression::~CommandObjectExpression() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307Options *CommandObjectExpression::GetOptions() { return &m_option_group; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308
Zachary Turner97206d52017-05-12 04:51:55 +0000309static lldb_private::Status
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310CanBeUsedForElementCountPrinting(ValueObject &valobj) {
311 CompilerType type(valobj.GetCompilerType());
312 CompilerType pointee;
313 if (!type.IsPointerType(&pointee))
Zachary Turner97206d52017-05-12 04:51:55 +0000314 return Status("as it does not refer to a pointer");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315 if (pointee.IsVoidType())
Zachary Turner97206d52017-05-12 04:51:55 +0000316 return Status("as it refers to a pointer to void");
317 return Status();
Enrico Granata520a4222016-04-25 00:52:47 +0000318}
319
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320bool CommandObjectExpression::EvaluateExpression(const char *expr,
321 Stream *output_stream,
322 Stream *error_stream,
323 CommandReturnObject *result) {
324 // Don't use m_exe_ctx as this might be called asynchronously
325 // after the command object DoExecute has finished when doing
326 // multi-line expression that use an input reader...
327 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Claytonba7b8e22013-01-26 23:54:29 +0000328
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329 Target *target = exe_ctx.GetTargetPtr();
Enrico Granatab576bba2013-01-09 20:12:53 +0000330
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 if (!target)
332 target = GetDummyTarget();
Dawn Perchik15663c52015-07-25 00:19:39 +0000333
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334 if (target) {
335 lldb::ValueObjectSP result_valobj_sp;
336 bool keep_in_memory = true;
337 StackFrame *frame = exe_ctx.GetFramePtr();
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +0000338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 EvaluateExpressionOptions options;
340 options.SetCoerceToId(m_varobj_options.use_objc);
341 options.SetUnwindOnError(m_command_options.unwind_on_error);
342 options.SetIgnoreBreakpoints(m_command_options.ignore_breakpoints);
343 options.SetKeepInMemory(keep_in_memory);
344 options.SetUseDynamic(m_varobj_options.use_dynamic);
345 options.SetTryAllThreads(m_command_options.try_all_threads);
346 options.SetDebug(m_command_options.debug);
347 options.SetLanguage(m_command_options.language);
348 options.SetExecutionPolicy(
349 m_command_options.allow_jit
350 ? EvaluateExpressionOptions::default_execution_policy
351 : lldb_private::eExecutionPolicyNever);
Sean Callanan4b388c92013-07-30 19:54:09 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 bool auto_apply_fixits;
354 if (m_command_options.auto_apply_fixits == eLazyBoolCalculate)
355 auto_apply_fixits = target->GetEnableAutoApplyFixIts();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000356 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 auto_apply_fixits =
358 m_command_options.auto_apply_fixits == eLazyBoolYes ? true : false;
359
360 options.SetAutoApplyFixIts(auto_apply_fixits);
361
362 if (m_command_options.top_level)
363 options.SetExecutionPolicy(eExecutionPolicyTopLevel);
364
365 // If there is any chance we are going to stop and want to see
366 // what went wrong with our expression, we should generate debug info
367 if (!m_command_options.ignore_breakpoints ||
368 !m_command_options.unwind_on_error)
369 options.SetGenerateDebugInfo(true);
370
371 if (m_command_options.timeout > 0)
Pavel Labath43d35412016-12-06 11:24:51 +0000372 options.SetTimeout(std::chrono::microseconds(m_command_options.timeout));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000373 else
Pavel Labath43d35412016-12-06 11:24:51 +0000374 options.SetTimeout(llvm::None);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375
376 ExpressionResults success = target->EvaluateExpression(
377 expr, frame, result_valobj_sp, options, &m_fixed_expression);
378
379 // We only tell you about the FixIt if we applied it. The compiler errors
380 // will suggest the FixIt if it parsed.
381 if (error_stream && !m_fixed_expression.empty() &&
382 target->GetEnableNotifyAboutFixIts()) {
383 if (success == eExpressionCompleted)
384 error_stream->Printf(
385 " Fix-it applied, fixed expression was: \n %s\n",
386 m_fixed_expression.c_str());
Sean Callananebf77072010-07-23 00:16:21 +0000387 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388
389 if (result_valobj_sp) {
390 Format format = m_format_options.GetFormat();
391
392 if (result_valobj_sp->GetError().Success()) {
393 if (format != eFormatVoid) {
394 if (format != eFormatDefault)
395 result_valobj_sp->SetFormat(format);
396
397 if (m_varobj_options.elem_count > 0) {
Zachary Turner97206d52017-05-12 04:51:55 +0000398 Status error(CanBeUsedForElementCountPrinting(*result_valobj_sp));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 if (error.Fail()) {
400 result->AppendErrorWithFormat(
401 "expression cannot be used with --element-count %s\n",
402 error.AsCString(""));
403 result->SetStatus(eReturnStatusFailed);
404 return false;
405 }
406 }
407
408 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(
409 m_command_options.m_verbosity, format));
410 options.SetVariableFormatDisplayLanguage(
411 result_valobj_sp->GetPreferredDisplayLanguage());
412
413 result_valobj_sp->Dump(*output_stream, options);
414
415 if (result)
416 result->SetStatus(eReturnStatusSuccessFinishResult);
417 }
418 } else {
419 if (result_valobj_sp->GetError().GetError() ==
420 UserExpression::kNoResult) {
421 if (format != eFormatVoid &&
422 m_interpreter.GetDebugger().GetNotifyVoid()) {
423 error_stream->PutCString("(void)\n");
424 }
425
426 if (result)
427 result->SetStatus(eReturnStatusSuccessFinishResult);
428 } else {
429 const char *error_cstr = result_valobj_sp->GetError().AsCString();
430 if (error_cstr && error_cstr[0]) {
431 const size_t error_cstr_len = strlen(error_cstr);
432 const bool ends_with_newline =
433 error_cstr[error_cstr_len - 1] == '\n';
434 if (strstr(error_cstr, "error:") != error_cstr)
435 error_stream->PutCString("error: ");
436 error_stream->Write(error_cstr, error_cstr_len);
437 if (!ends_with_newline)
438 error_stream->EOL();
439 } else {
440 error_stream->PutCString("error: unknown error\n");
441 }
442
443 if (result)
444 result->SetStatus(eReturnStatusFailed);
445 }
446 }
447 }
448 } else {
449 error_stream->Printf("error: invalid execution context for expression\n");
450 return false;
451 }
452
453 return true;
454}
455
456void CommandObjectExpression::IOHandlerInputComplete(IOHandler &io_handler,
457 std::string &line) {
458 io_handler.SetIsDone(true);
459 // StreamSP output_stream =
460 // io_handler.GetDebugger().GetAsyncOutputStream();
461 // StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream();
462 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
463 StreamFileSP error_sp(io_handler.GetErrorStreamFile());
464
465 EvaluateExpression(line.c_str(), output_sp.get(), error_sp.get());
466 if (output_sp)
467 output_sp->Flush();
468 if (error_sp)
469 error_sp->Flush();
470}
471
472bool CommandObjectExpression::IOHandlerIsInputComplete(IOHandler &io_handler,
473 StringList &lines) {
474 // An empty lines is used to indicate the end of input
475 const size_t num_lines = lines.GetSize();
476 if (num_lines > 0 && lines[num_lines - 1].empty()) {
477 // Remove the last empty line from "lines" so it doesn't appear
478 // in our resulting input and return true to indicate we are done
479 // getting lines
480 lines.PopBack();
Sean Callananebf77072010-07-23 00:16:21 +0000481 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482 }
483 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484}
485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486void CommandObjectExpression::GetMultilineExpression() {
487 m_expr_lines.clear();
488 m_expr_line_count = 0;
Greg Clayton44d93782014-01-27 23:43:24 +0000489
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 Debugger &debugger = GetCommandInterpreter().GetDebugger();
491 bool color_prompt = debugger.GetUseColor();
492 const bool multiple_lines = true; // Get multiple lines
493 IOHandlerSP io_handler_sp(
494 new IOHandlerEditline(debugger, IOHandler::Type::Expression,
495 "lldb-expr", // Name of input reader for history
Zachary Turner514d8cd2016-09-23 18:06:53 +0000496 llvm::StringRef(), // No prompt
497 llvm::StringRef(), // Continuation prompt
Kate Stoneb9c1b512016-09-06 20:57:50 +0000498 multiple_lines, color_prompt,
499 1, // Show line numbers starting at 1
500 *this));
501
502 StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile());
503 if (output_sp) {
504 output_sp->PutCString(
505 "Enter expressions, then terminate with an empty line to evaluate:\n");
506 output_sp->Flush();
507 }
508 debugger.PushIOHandler(io_handler_sp);
Greg Clayton44d93782014-01-27 23:43:24 +0000509}
510
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511bool CommandObjectExpression::DoExecute(const char *command,
512 CommandReturnObject &result) {
513 m_fixed_expression.clear();
514 auto exe_ctx = GetCommandInterpreter().GetExecutionContext();
515 m_option_group.NotifyOptionParsingStarting(&exe_ctx);
Greg Clayton44d93782014-01-27 23:43:24 +0000516
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 const char *expr = nullptr;
Greg Claytoncf28a8b2014-03-13 23:42:30 +0000518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519 if (command[0] == '\0') {
520 GetMultilineExpression();
521 return result.Succeeded();
522 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524 if (command[0] == '-') {
525 // We have some options and these options MUST end with --.
526 const char *end_options = nullptr;
527 const char *s = command;
528 while (s && s[0]) {
529 end_options = ::strstr(s, "--");
530 if (end_options) {
531 end_options += 2; // Get past the "--"
532 if (::isspace(end_options[0])) {
533 expr = end_options;
534 while (::isspace(*expr))
535 ++expr;
536 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538 }
539 s = end_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540 }
541
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542 if (end_options) {
543 Args args(llvm::StringRef(command, end_options - command));
544 if (!ParseOptions(args, result))
545 return false;
546
Zachary Turner97206d52017-05-12 04:51:55 +0000547 Status error(m_option_group.NotifyOptionParsingFinished(&exe_ctx));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548 if (error.Fail()) {
549 result.AppendError(error.AsCString());
550 result.SetStatus(eReturnStatusFailed);
551 return false;
552 }
553
554 if (m_repl_option.GetOptionValue().GetCurrentValue()) {
Jim Inghame5ee6f02016-03-29 22:00:08 +0000555 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 if (target) {
557 // Drop into REPL
558 m_expr_lines.clear();
559 m_expr_line_count = 0;
Johnny Chenfcd43b72010-08-13 00:42:30 +0000560
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561 Debugger &debugger = target->GetDebugger();
562
563 // Check if the LLDB command interpreter is sitting on top of a REPL
564 // that
565 // launched it...
566 if (debugger.CheckTopIOHandlerTypes(
567 IOHandler::Type::CommandInterpreter, IOHandler::Type::REPL)) {
568 // the LLDB command interpreter is sitting on top of a REPL that
569 // launched it,
570 // so just say the command interpreter is done and fall back to the
571 // existing REPL
572 m_interpreter.GetIOHandler(false)->SetIsDone(true);
573 } else {
574 // We are launching the REPL on top of the current LLDB command
575 // interpreter,
576 // so just push one
577 bool initialize = false;
Zachary Turner97206d52017-05-12 04:51:55 +0000578 Status repl_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 REPLSP repl_sp(target->GetREPL(
580 repl_error, m_command_options.language, nullptr, false));
581
582 if (!repl_sp) {
583 initialize = true;
584 repl_sp = target->GetREPL(repl_error, m_command_options.language,
585 nullptr, true);
586 if (!repl_error.Success()) {
587 result.SetError(repl_error);
588 return result.Succeeded();
589 }
590 }
591
592 if (repl_sp) {
593 if (initialize) {
594 repl_sp->SetCommandOptions(m_command_options);
595 repl_sp->SetFormatOptions(m_format_options);
596 repl_sp->SetValueObjectDisplayOptions(m_varobj_options);
597 }
598
599 IOHandlerSP io_handler_sp(repl_sp->GetIOHandler());
600
601 io_handler_sp->SetIsDone(false);
602
603 debugger.PushIOHandler(io_handler_sp);
604 } else {
605 repl_error.SetErrorStringWithFormat(
606 "Couldn't create a REPL for %s",
607 Language::GetNameForLanguageType(m_command_options.language));
608 result.SetError(repl_error);
609 return result.Succeeded();
610 }
611 }
612 }
613 }
614 // No expression following options
615 else if (expr == nullptr || expr[0] == '\0') {
616 GetMultilineExpression();
617 return result.Succeeded();
618 }
619 }
620 }
621
622 if (expr == nullptr)
623 expr = command;
624
625 if (EvaluateExpression(expr, &(result.GetOutputStream()),
626 &(result.GetErrorStream()), &result)) {
627 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
Jim Ingham981da8d2016-10-17 23:59:41 +0000628 if (!target)
629 target = GetDummyTarget();
630
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 if (!m_fixed_expression.empty() && target->GetEnableNotifyAboutFixIts()) {
632 CommandHistory &history = m_interpreter.GetCommandHistory();
633 // FIXME: Can we figure out what the user actually typed (e.g. some alias
634 // for expr???)
635 // If we can it would be nice to show that.
636 std::string fixed_command("expression ");
637 if (expr == command)
638 fixed_command.append(m_fixed_expression);
639 else {
640 // Add in any options that might have been in the original command:
641 fixed_command.append(command, expr - command);
642 fixed_command.append(m_fixed_expression);
643 }
644 history.AppendString(fixed_command);
645 }
646 return true;
647 }
648
649 result.SetStatus(eReturnStatusFailed);
650 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651}