blob: 3f550883b299da1fced8f145ef7dd6aae33c6bb4 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Value.h"
Jim Ingham6c68fb42010-09-30 00:54:27 +000019#include "lldb/Core/ValueObjectVariable.h"
Enrico Granata4d93b8c2013-09-30 19:11:51 +000020#include "lldb/DataFormatters/ValueObjectPrinter.h"
Sean Callanan30e33972015-09-03 00:48:23 +000021#include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h"
Jim Ingham151c0322015-09-15 21:13:50 +000022#include "lldb/Expression/UserExpression.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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Host/Host.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000026#include "lldb/Host/StringConvert.h"
Sean Callananebf77072010-07-23 00:16:21 +000027#include "lldb/Core/Debugger.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"
Jim Ingham0e0984e2015-09-02 01:06:46 +000030#include "lldb/Target/Language.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Symbol/ObjectFile.h"
32#include "lldb/Symbol/Variable.h"
33#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
Greg Clayton1deb7962011-10-25 06:44:01 +000041CommandObjectExpression::CommandOptions::CommandOptions () :
42 OptionGroup()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043{
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044}
45
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000046CommandObjectExpression::CommandOptions::~CommandOptions() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
Enrico Granata4d93b8c2013-09-30 19:11:51 +000048static OptionEnumValueElement g_description_verbosity_type[] =
49{
50 { eLanguageRuntimeDescriptionDisplayVerbosityCompact, "compact", "Only show the description string"},
51 { eLanguageRuntimeDescriptionDisplayVerbosityFull, "full", "Show the full output, including persistent variable's name and type"},
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000052 { 0, nullptr, nullptr }
Enrico Granata4d93b8c2013-09-30 19:11:51 +000053};
54
Greg Clayton1deb7962011-10-25 06:44:01 +000055OptionDefinition
56CommandObjectExpression::CommandOptions::g_option_table[] =
57{
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000058 { 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."},
59 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "ignore-breakpoints", 'i', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Ignore breakpoint hits while running expressions"},
60 { 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."},
61 { 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. Note, unlike gdb hitting a breakpoint is controlled by another option (-i)."},
62 { 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 and forcing breakpoints to not be ignored (-i0) and no unwinding to happen on error (-u0)."},
63 { 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 setting is used." },
Jim Inghama1e541b2016-03-25 01:57:14 +000064 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "apply-fixits", 'X', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeLanguage, "If true, simple FixIt hints will be automatically applied to the expression." },
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +000065 { 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 Callanan863fab62016-03-28 21:20:05 +000066 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "top-level", 'p', OptionParser::eNoArgument , NULL, NULL, 0, eArgTypeNone, "Interpret the expression as top-level definitions rather than code to be immediately executed."}
Greg Clayton1deb7962011-10-25 06:44:01 +000067};
68
Greg Clayton1deb7962011-10-25 06:44:01 +000069uint32_t
70CommandObjectExpression::CommandOptions::GetNumDefinitions ()
71{
Saleem Abdulrasool28606952014-06-27 05:17:41 +000072 return llvm::array_lengthof(g_option_table);
Greg Clayton1deb7962011-10-25 06:44:01 +000073}
74
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075Error
Greg Clayton1deb7962011-10-25 06:44:01 +000076CommandObjectExpression::CommandOptions::SetOptionValue (CommandInterpreter &interpreter,
77 uint32_t option_idx,
78 const char *option_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079{
80 Error error;
81
Greg Clayton3bcdfc02012-12-04 00:32:51 +000082 const int short_option = g_option_table[option_idx].short_option;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083
84 switch (short_option)
85 {
Dawn Perchik15663c52015-07-25 00:19:39 +000086 case 'l':
Jim Ingham0e0984e2015-09-02 01:06:46 +000087 language = Language::GetLanguageTypeFromString (option_arg);
Dawn Perchik15663c52015-07-25 00:19:39 +000088 if (language == eLanguageTypeUnknown)
89 error.SetErrorStringWithFormat ("unknown language type: '%s' for expression", option_arg);
90 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091
Jim Ingham35e1bda2012-10-16 21:41:58 +000092 case 'a':
93 {
94 bool success;
95 bool result;
96 result = Args::StringToBoolean(option_arg, true, &success);
97 if (!success)
98 error.SetErrorStringWithFormat("invalid all-threads value setting: \"%s\"", option_arg);
99 else
100 try_all_threads = result;
101 }
Jim Ingham6c68fb42010-09-30 00:54:27 +0000102 break;
Jim Ingham399f1ca2010-11-05 19:25:48 +0000103
Jim Ingham184e9812013-01-15 02:47:48 +0000104 case 'i':
105 {
106 bool success;
107 bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
108 if (success)
109 ignore_breakpoints = tmp_value;
110 else
111 error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg);
112 break;
113 }
Jim Ingham35e1bda2012-10-16 21:41:58 +0000114 case 't':
115 {
116 bool success;
117 uint32_t result;
Vince Harron5275aaa2015-01-15 20:08:35 +0000118 result = StringConvert::ToUInt32(option_arg, 0, 0, &success);
Jim Ingham35e1bda2012-10-16 21:41:58 +0000119 if (success)
120 timeout = result;
121 else
122 error.SetErrorStringWithFormat ("invalid timeout setting \"%s\"", option_arg);
123 }
124 break;
125
Jim Ingham399f1ca2010-11-05 19:25:48 +0000126 case 'u':
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000127 {
128 bool success;
Jim Ingham184e9812013-01-15 02:47:48 +0000129 bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
130 if (success)
131 unwind_on_error = tmp_value;
132 else
Greg Clayton86edbf42011-10-26 00:56:27 +0000133 error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000134 break;
135 }
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000136
137 case 'v':
138 if (!option_arg)
139 {
140 m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull;
141 break;
142 }
143 m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error);
144 if (!error.Success())
145 error.SetErrorStringWithFormat ("unrecognized value for description-verbosity '%s'", option_arg);
146 break;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000147
148 case 'g':
149 debug = true;
150 unwind_on_error = false;
151 ignore_breakpoints = false;
152 break;
Sean Callanan863fab62016-03-28 21:20:05 +0000153
154 case 'p':
155 top_level = true;
156 break;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000157
Jim Inghama1e541b2016-03-25 01:57:14 +0000158 case 'X':
159 {
160 bool success;
161 bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
162 if (success)
163 auto_apply_fixits = tmp_value ? eLazyBoolYes : eLazyBoolNo;
164 else
165 error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg);
166 break;
167 }
168
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000170 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 break;
172 }
173
174 return error;
175}
176
177void
Greg Clayton1deb7962011-10-25 06:44:01 +0000178CommandObjectExpression::CommandOptions::OptionParsingStarting (CommandInterpreter &interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179{
Jim Ingham184e9812013-01-15 02:47:48 +0000180 Process *process = interpreter.GetExecutionContext().GetProcessPtr();
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000181 if (process != nullptr)
Jim Ingham184e9812013-01-15 02:47:48 +0000182 {
183 ignore_breakpoints = process->GetIgnoreBreakpointsInExpressions();
184 unwind_on_error = process->GetUnwindOnErrorInExpressions();
185 }
186 else
187 {
Greg Claytonfc03f8f2014-03-25 18:47:07 +0000188 ignore_breakpoints = true;
Jim Ingham184e9812013-01-15 02:47:48 +0000189 unwind_on_error = true;
190 }
191
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 show_summary = true;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000193 try_all_threads = true;
194 timeout = 0;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000195 debug = false;
Dawn Perchik15663c52015-07-25 00:19:39 +0000196 language = eLanguageTypeUnknown;
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000197 m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact;
Jim Inghama1e541b2016-03-25 01:57:14 +0000198 auto_apply_fixits = eLazyBoolCalculate;
Sean Callanan863fab62016-03-28 21:20:05 +0000199 top_level = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200}
201
Greg Claytone0d378b2011-03-24 21:19:54 +0000202const OptionDefinition*
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203CommandObjectExpression::CommandOptions::GetDefinitions ()
204{
205 return g_option_table;
206}
207
Greg Claytona7015092010-09-18 01:14:36 +0000208CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000209 CommandObjectRaw(interpreter,
210 "expression",
211 "Evaluate an expression in the current program context, using user defined variables and variables currently in scope.",
212 nullptr,
213 eCommandProcessMustBePaused | eCommandTryTargetAPILock),
Greg Clayton44d93782014-01-27 23:43:24 +0000214 IOHandlerDelegate (IOHandlerDelegate::Completion::Expression),
Greg Clayton1deb7962011-10-25 06:44:01 +0000215 m_option_group (interpreter),
216 m_format_options (eFormatDefault),
Sean Callananf2bd5c32015-10-20 00:55:21 +0000217 m_repl_option (LLDB_OPT_SET_1, false, "repl", 'r', "Drop into REPL", false, true),
Greg Clayton1deb7962011-10-25 06:44:01 +0000218 m_command_options (),
Johnny Chenf7edb1c2010-09-30 18:30:25 +0000219 m_expr_line_count (0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220 m_expr_lines ()
221{
Kate Stoneea671fb2015-07-14 05:48:36 +0000222 SetHelpLong(
223R"(
224Timeouts:
225
226)" " If the expression can be evaluated statically (without running code) then it will be. \
227Otherwise, by default the expression will run on the current thread with a short timeout: \
228currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted \
229and resumed with all threads running. You can use the -a option to disable retrying on all \
230threads. You can use the -t option to set a shorter timeout." R"(
231
232User defined variables:
233
234)" " You can define your own variables for convenience or to be used in subsequent expressions. \
235You define them the same way you would define variables in C. If the first character of \
236your user defined variable is a $, then the variable's value will be available in future \
237expressions, otherwise it will just be available in the current expression." R"(
238
239Continuing evaluation after a breakpoint:
240
241)" " If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once \
242you are done with your investigation, you can either remove the expression execution frames \
243from the stack with \"thread return -x\" or if you are still interested in the expression result \
244you can issue the \"continue\" command and the expression evaluation will complete and the \
245expression result will be available using the \"thread.completed-expression\" key in the thread \
246format." R"(
247
248Examples:
249
250 expr my_struct->a = my_array[3]
251 expr -f bin -- (index * 8) + 5
252 expr unsigned int $foo = 5
253 expr char c[] = \"foo\"; c[0])"
254 );
Caroline Tice405fe672010-10-04 22:28:36 +0000255
256 CommandArgumentEntry arg;
257 CommandArgumentData expression_arg;
258
259 // Define the first (and only) variant of this arg.
260 expression_arg.arg_type = eArgTypeExpression;
261 expression_arg.arg_repetition = eArgRepeatPlain;
262
263 // There is only one variant this argument could be; put it into the argument entry.
264 arg.push_back (expression_arg);
265
266 // Push the data for the first argument into the m_arguments vector.
267 m_arguments.push_back (arg);
Greg Clayton1deb7962011-10-25 06:44:01 +0000268
Greg Clayton5009f9d2011-10-27 17:55:14 +0000269 // Add the "--format" and "--gdb-format"
270 m_option_group.Append (&m_format_options, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1);
Greg Clayton1deb7962011-10-25 06:44:01 +0000271 m_option_group.Append (&m_command_options);
Enrico Granatab576bba2013-01-09 20:12:53 +0000272 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1 | LLDB_OPT_SET_2);
Sean Callananf2bd5c32015-10-20 00:55:21 +0000273 m_option_group.Append (&m_repl_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_3);
Greg Clayton1deb7962011-10-25 06:44:01 +0000274 m_option_group.Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275}
276
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000277CommandObjectExpression::~CommandObjectExpression() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278
279Options *
280CommandObjectExpression::GetOptions ()
281{
Greg Clayton1deb7962011-10-25 06:44:01 +0000282 return &m_option_group;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283}
284
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285bool
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000286CommandObjectExpression::EvaluateExpression(const char *expr,
287 Stream *output_stream,
288 Stream *error_stream,
289 CommandReturnObject *result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290{
Greg Claytonba7b8e22013-01-26 23:54:29 +0000291 // Don't use m_exe_ctx as this might be called asynchronously
292 // after the command object DoExecute has finished when doing
293 // multi-line expression that use an input reader...
294 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
295
296 Target *target = exe_ctx.GetTargetPtr();
Sean Callananc0a6e062011-10-27 21:22:25 +0000297
298 if (!target)
Jim Ingham893c9322014-11-22 01:42:44 +0000299 target = GetDummyTarget();
Sean Callananc0a6e062011-10-27 21:22:25 +0000300
Greg Claytonc14ee322011-09-22 04:58:26 +0000301 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000303 lldb::ValueObjectSP result_valobj_sp;
Sean Callanan92adcac2011-01-13 08:53:35 +0000304 bool keep_in_memory = true;
Dawn Perchik009d1102015-09-04 01:02:30 +0000305 StackFrame *frame = exe_ctx.GetFramePtr();
Enrico Granatab576bba2013-01-09 20:12:53 +0000306
Jim Ingham35e1bda2012-10-16 21:41:58 +0000307 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +0000308 options.SetCoerceToId(m_varobj_options.use_objc);
309 options.SetUnwindOnError(m_command_options.unwind_on_error);
310 options.SetIgnoreBreakpoints (m_command_options.ignore_breakpoints);
311 options.SetKeepInMemory(keep_in_memory);
312 options.SetUseDynamic(m_varobj_options.use_dynamic);
313 options.SetTryAllThreads(m_command_options.try_all_threads);
314 options.SetDebug(m_command_options.debug);
Dawn Perchik1bbaede2015-10-07 22:01:12 +0000315 options.SetLanguage(m_command_options.language);
Jim Inghama1e541b2016-03-25 01:57:14 +0000316
317 bool auto_apply_fixits;
318 if (m_command_options.auto_apply_fixits == eLazyBoolCalculate)
319 auto_apply_fixits = target->GetEnableAutoApplyFixIts();
320 else
321 auto_apply_fixits = m_command_options.auto_apply_fixits == eLazyBoolYes ? true : false;
322
323 options.SetAutoApplyFixIts(auto_apply_fixits);
Sean Callanan863fab62016-03-28 21:20:05 +0000324
325 if (m_command_options.top_level)
326 options.SetExecutionPolicy(eExecutionPolicyTopLevel);
Dawn Perchik15663c52015-07-25 00:19:39 +0000327
Greg Clayton23f8c952014-03-24 23:10:19 +0000328 // If there is any chance we are going to stop and want to see
329 // what went wrong with our expression, we should generate debug info
330 if (!m_command_options.ignore_breakpoints ||
331 !m_command_options.unwind_on_error)
332 options.SetGenerateDebugInfo(true);
333
Greg Clayton62afb9f2013-11-04 19:35:17 +0000334 if (m_command_options.timeout > 0)
335 options.SetTimeoutUsec(m_command_options.timeout);
Jim Ingham6f78f382014-01-17 20:09:23 +0000336 else
337 options.SetTimeoutUsec(0);
Arnaud A. de Grandmaison62e5f4d2014-03-22 20:23:26 +0000338
Dawn Perchik009d1102015-09-04 01:02:30 +0000339 target->EvaluateExpression(expr, frame, result_valobj_sp, options);
Sean Callanan4b388c92013-07-30 19:54:09 +0000340
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000341 if (result_valobj_sp)
342 {
Sean Callananbf154da2012-08-08 17:35:10 +0000343 Format format = m_format_options.GetFormat();
344
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000345 if (result_valobj_sp->GetError().Success())
346 {
Sean Callananbf154da2012-08-08 17:35:10 +0000347 if (format != eFormatVoid)
348 {
349 if (format != eFormatDefault)
350 result_valobj_sp->SetFormat (format);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000351
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000352 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(m_command_options.m_verbosity,format));
Enrico Granata73e8c4d2015-10-07 02:36:35 +0000353 options.SetVariableFormatDisplayLanguage(result_valobj_sp->GetPreferredDisplayLanguage());
Enrico Granatab576bba2013-01-09 20:12:53 +0000354
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000355 result_valobj_sp->Dump(*output_stream,options);
356
Sean Callananbf154da2012-08-08 17:35:10 +0000357 if (result)
358 result->SetStatus (eReturnStatusSuccessFinishResult);
359 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000360 }
361 else
362 {
Jim Ingham151c0322015-09-15 21:13:50 +0000363 if (result_valobj_sp->GetError().GetError() == UserExpression::kNoResult)
Greg Clayton5fd05902011-06-24 22:31:10 +0000364 {
Sean Callananbcf897f2012-08-09 18:18:47 +0000365 if (format != eFormatVoid && m_interpreter.GetDebugger().GetNotifyVoid())
Sean Callananbf154da2012-08-08 17:35:10 +0000366 {
Sean Callananbcf897f2012-08-09 18:18:47 +0000367 error_stream->PutCString("(void)\n");
Sean Callananbf154da2012-08-08 17:35:10 +0000368 }
Sean Callananbcf897f2012-08-09 18:18:47 +0000369
370 if (result)
371 result->SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton5fd05902011-06-24 22:31:10 +0000372 }
373 else
374 {
Sean Callananbccce812011-08-23 21:20:51 +0000375 const char *error_cstr = result_valobj_sp->GetError().AsCString();
376 if (error_cstr && error_cstr[0])
377 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000378 const size_t error_cstr_len = strlen (error_cstr);
Sean Callananbccce812011-08-23 21:20:51 +0000379 const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n';
380 if (strstr(error_cstr, "error:") != error_cstr)
381 error_stream->PutCString ("error: ");
382 error_stream->Write(error_cstr, error_cstr_len);
383 if (!ends_with_newline)
384 error_stream->EOL();
385 }
386 else
387 {
388 error_stream->PutCString ("error: unknown error\n");
389 }
390
391 if (result)
392 result->SetStatus (eReturnStatusFailed);
Greg Clayton5fd05902011-06-24 22:31:10 +0000393 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000394 }
395 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000396 }
397 else
398 {
Caroline Tice6e8dc332011-06-13 20:20:29 +0000399 error_stream->Printf ("error: invalid execution context for expression\n");
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000400 return false;
Sean Callananebf77072010-07-23 00:16:21 +0000401 }
Sean Callanand1e5b432010-08-12 01:56:52 +0000402
Sean Callananebf77072010-07-23 00:16:21 +0000403 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404}
405
Greg Clayton44d93782014-01-27 23:43:24 +0000406void
Greg Clayton44d93782014-01-27 23:43:24 +0000407CommandObjectExpression::IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
408{
409 io_handler.SetIsDone(true);
410// StreamSP output_stream = io_handler.GetDebugger().GetAsyncOutputStream();
411// StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream();
412 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
413 StreamFileSP error_sp(io_handler.GetErrorStreamFile());
414
415 EvaluateExpression (line.c_str(),
416 output_sp.get(),
417 error_sp.get());
418 if (output_sp)
419 output_sp->Flush();
420 if (error_sp)
421 error_sp->Flush();
422}
423
424LineStatus
425CommandObjectExpression::IOHandlerLinesUpdated (IOHandler &io_handler,
426 StringList &lines,
427 uint32_t line_idx,
428 Error &error)
429{
430 if (line_idx == UINT32_MAX)
431 {
432 // Remove the last line from "lines" so it doesn't appear
433 // in our final expression
434 lines.PopBack();
435 error.Clear();
436 return LineStatus::Done;
437 }
438 else if (line_idx + 1 == lines.GetSize())
439 {
440 // The last line was edited, if this line is empty, then we are done
441 // getting our multiple lines.
442 if (lines[line_idx].empty())
443 return LineStatus::Done;
444 }
445 return LineStatus::Success;
446}
447
Greg Claytoncf28a8b2014-03-13 23:42:30 +0000448void
449CommandObjectExpression::GetMultilineExpression ()
450{
451 m_expr_lines.clear();
452 m_expr_line_count = 0;
453
454 Debugger &debugger = GetCommandInterpreter().GetDebugger();
Kate Stonee30f11d2014-11-17 19:06:59 +0000455 bool color_prompt = debugger.GetUseColor();
Greg Claytoncf28a8b2014-03-13 23:42:30 +0000456 const bool multiple_lines = true; // Get multiple lines
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000457 IOHandlerSP io_handler_sp(new IOHandlerEditline(debugger,
458 IOHandler::Type::Expression,
459 "lldb-expr", // Name of input reader for history
460 nullptr, // No prompt
461 nullptr, // Continuation prompt
462 multiple_lines,
463 color_prompt,
464 1, // Show line numbers starting at 1
465 *this));
Greg Claytoncf28a8b2014-03-13 23:42:30 +0000466
467 StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile());
468 if (output_sp)
469 {
470 output_sp->PutCString("Enter expressions, then terminate with an empty line to evaluate:\n");
471 output_sp->Flush();
472 }
473 debugger.PushIOHandler(io_handler_sp);
474}
475
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476bool
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000477CommandObjectExpression::DoExecute(const char *command,
478 CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479{
Greg Clayton1deb7962011-10-25 06:44:01 +0000480 m_option_group.NotifyOptionParsingStarting();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000482 const char * expr = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483
484 if (command[0] == '\0')
485 {
Greg Claytoncf28a8b2014-03-13 23:42:30 +0000486 GetMultilineExpression ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 return result.Succeeded();
488 }
489
490 if (command[0] == '-')
491 {
492 // We have some options and these options MUST end with --.
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000493 const char *end_options = nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 const char *s = command;
495 while (s && s[0])
496 {
497 end_options = ::strstr (s, "--");
498 if (end_options)
499 {
500 end_options += 2; // Get past the "--"
501 if (::isspace (end_options[0]))
502 {
503 expr = end_options;
504 while (::isspace (*expr))
505 ++expr;
506 break;
507 }
508 }
509 s = end_options;
510 }
511
512 if (end_options)
513 {
Pavel Labath00b7f952015-03-02 12:46:22 +0000514 Args args (llvm::StringRef(command, end_options - command));
Greg Claytona7015092010-09-18 01:14:36 +0000515 if (!ParseOptions (args, result))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516 return false;
Greg Claytonf6b8b582011-04-13 00:18:08 +0000517
Greg Clayton1deb7962011-10-25 06:44:01 +0000518 Error error (m_option_group.NotifyOptionParsingFinished());
Greg Claytonf6b8b582011-04-13 00:18:08 +0000519 if (error.Fail())
520 {
521 result.AppendError (error.AsCString());
522 result.SetStatus (eReturnStatusFailed);
523 return false;
524 }
Greg Claytoncf28a8b2014-03-13 23:42:30 +0000525
Sean Callananf2bd5c32015-10-20 00:55:21 +0000526 if (m_repl_option.GetOptionValue().GetCurrentValue())
527 {
528 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
529 if (target)
530 {
531 // Drop into REPL
532 m_expr_lines.clear();
533 m_expr_line_count = 0;
534
535 Debugger &debugger = target->GetDebugger();
536
537 // Check if the LLDB command interpreter is sitting on top of a REPL that
538 // launched it...
539 if (debugger.CheckTopIOHandlerTypes(IOHandler::Type::CommandInterpreter, IOHandler::Type::REPL))
540 {
541 // the LLDB command interpreter is sitting on top of a REPL that launched it,
542 // so just say the command interpreter is done and fall back to the existing REPL
543 m_interpreter.GetIOHandler(false)->SetIsDone(true);
544 }
545 else
546 {
547 // We are launching the REPL on top of the current LLDB command interpreter,
548 // so just push one
549 bool initialize = false;
550 Error repl_error;
551 REPLSP repl_sp (target->GetREPL(repl_error, m_command_options.language, nullptr, false));
552
553 if (!repl_sp)
554 {
555 initialize = true;
556 repl_sp = target->GetREPL(repl_error, m_command_options.language, nullptr, true);
557 if (!repl_error.Success())
558 {
559 result.SetError(repl_error);
560 return result.Succeeded();
561 }
562 }
563
564 if (repl_sp)
565 {
566 if (initialize)
567 {
568 repl_sp->SetCommandOptions(m_command_options);
569 repl_sp->SetFormatOptions(m_format_options);
570 repl_sp->SetValueObjectDisplayOptions(m_varobj_options);
571 }
572
573 IOHandlerSP io_handler_sp (repl_sp->GetIOHandler());
574
575 io_handler_sp->SetIsDone(false);
576
577 debugger.PushIOHandler(io_handler_sp);
578 }
579 else
580 {
581 repl_error.SetErrorStringWithFormat("Couldn't create a REPL for %s", Language::GetNameForLanguageType(m_command_options.language));
582 result.SetError(repl_error);
583 return result.Succeeded();
584 }
585 }
586 }
587 }
Greg Claytoncf28a8b2014-03-13 23:42:30 +0000588 // No expression following options
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000589 else if (expr == nullptr || expr[0] == '\0')
Greg Claytoncf28a8b2014-03-13 23:42:30 +0000590 {
591 GetMultilineExpression ();
592 return result.Succeeded();
593 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594 }
595 }
596
Eugene Zelenkoc8ecc2a2016-02-19 19:33:46 +0000597 if (expr == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 expr = command;
Sean Callanan16ad5fa2010-06-24 00:16:27 +0000599
Caroline Tice6e8dc332011-06-13 20:20:29 +0000600 if (EvaluateExpression (expr, &(result.GetOutputStream()), &(result.GetErrorStream()), &result))
Johnny Chenfcd43b72010-08-13 00:42:30 +0000601 return true;
602
603 result.SetStatus (eReturnStatusFailed);
604 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605}