blob: 6d44f71b8d9581b5fe65d9398b1a2286b1876e4b [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "CommandObjectExpression.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
Sean Callananebf77072010-07-23 00:16:21 +000018#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/Value.h"
20#include "lldb/Core/InputReader.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/ClangExpressionVariable.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000024#include "lldb/Expression/ClangUserExpression.h"
Stephen Wilsonebb84b22010-07-23 21:47:22 +000025#include "lldb/Expression/ClangFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Expression/DWARFExpression.h"
27#include "lldb/Host/Host.h"
Sean Callananebf77072010-07-23 00:16:21 +000028#include "lldb/Core/Debugger.h"
Greg Clayton66111032010-06-23 01:19:29 +000029#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Interpreter/CommandReturnObject.h"
Jim Ingham6c68fb42010-09-30 00:54:27 +000031#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Symbol/ObjectFile.h"
33#include "lldb/Symbol/Variable.h"
34#include "lldb/Target/Process.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000035#include "lldb/Target/StackFrame.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Target/Target.h"
Greg Clayton7260f622011-04-18 08:33:37 +000037#include "lldb/Target/Thread.h"
Sean Callananebf77072010-07-23 00:16:21 +000038#include "llvm/ADT/StringRef.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
Greg Clayton1deb7962011-10-25 06:44:01 +000043CommandObjectExpression::CommandOptions::CommandOptions () :
44 OptionGroup()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045{
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046}
47
48
49CommandObjectExpression::CommandOptions::~CommandOptions ()
50{
51}
52
Enrico Granata4d93b8c2013-09-30 19:11:51 +000053static OptionEnumValueElement g_description_verbosity_type[] =
54{
55 { eLanguageRuntimeDescriptionDisplayVerbosityCompact, "compact", "Only show the description string"},
56 { eLanguageRuntimeDescriptionDisplayVerbosityFull, "full", "Show the full output, including persistent variable's name and type"},
57 { 0, NULL, NULL }
58};
59
Greg Clayton1deb7962011-10-25 06:44:01 +000060OptionDefinition
61CommandObjectExpression::CommandOptions::g_option_table[] =
62{
Virgile Belloe2607b52013-09-05 16:42:23 +000063 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "all-threads", 'a', OptionParser::eRequiredArgument, NULL, 0, eArgTypeBoolean, "Should we run all threads if the execution doesn't complete on one thread."},
64 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "ignore-breakpoints", 'i', OptionParser::eRequiredArgument, NULL, 0, eArgTypeBoolean, "Ignore breakpoint hits while running expressions"},
65 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "timeout", 't', OptionParser::eRequiredArgument, NULL, 0, eArgTypeUnsignedInteger, "Timeout value (in microseconds) for running the expression."},
66 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "unwind-on-error", 'u', OptionParser::eRequiredArgument, 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)."},
Greg Clayton62afb9f2013-11-04 19:35:17 +000067 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "debug", 'g', OptionParser::eNoArgument , 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)."},
Enrico Granata4d93b8c2013-09-30 19:11:51 +000068 { LLDB_OPT_SET_1, false, "description-verbosity", 'v', OptionParser::eOptionalArgument, g_description_verbosity_type, 0, eArgTypeDescriptionVerbosity, "How verbose should the output of this expression be, if the object description is asked for."},
Greg Clayton1deb7962011-10-25 06:44:01 +000069};
70
71
72uint32_t
73CommandObjectExpression::CommandOptions::GetNumDefinitions ()
74{
75 return sizeof(g_option_table)/sizeof(OptionDefinition);
76}
77
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078Error
Greg Clayton1deb7962011-10-25 06:44:01 +000079CommandObjectExpression::CommandOptions::SetOptionValue (CommandInterpreter &interpreter,
80 uint32_t option_idx,
81 const char *option_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082{
83 Error error;
84
Greg Clayton3bcdfc02012-12-04 00:32:51 +000085 const int short_option = g_option_table[option_idx].short_option;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086
87 switch (short_option)
88 {
Caroline Tice3f4c09c2010-09-07 22:38:08 +000089 //case 'l':
90 //if (language.SetLanguageFromCString (option_arg) == false)
91 //{
Greg Clayton86edbf42011-10-26 00:56:27 +000092 // error.SetErrorStringWithFormat("invalid language option argument '%s'", option_arg);
Caroline Tice3f4c09c2010-09-07 22:38:08 +000093 //}
94 //break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095
Jim Ingham35e1bda2012-10-16 21:41:58 +000096 case 'a':
97 {
98 bool success;
99 bool result;
100 result = Args::StringToBoolean(option_arg, true, &success);
101 if (!success)
102 error.SetErrorStringWithFormat("invalid all-threads value setting: \"%s\"", option_arg);
103 else
104 try_all_threads = result;
105 }
Jim Ingham6c68fb42010-09-30 00:54:27 +0000106 break;
Jim Ingham399f1ca2010-11-05 19:25:48 +0000107
Jim Ingham184e9812013-01-15 02:47:48 +0000108 case 'i':
109 {
110 bool success;
111 bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
112 if (success)
113 ignore_breakpoints = tmp_value;
114 else
115 error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg);
116 break;
117 }
Jim Ingham35e1bda2012-10-16 21:41:58 +0000118 case 't':
119 {
120 bool success;
121 uint32_t result;
122 result = Args::StringToUInt32(option_arg, 0, 0, &success);
123 if (success)
124 timeout = result;
125 else
126 error.SetErrorStringWithFormat ("invalid timeout setting \"%s\"", option_arg);
127 }
128 break;
129
Jim Ingham399f1ca2010-11-05 19:25:48 +0000130 case 'u':
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000131 {
132 bool success;
Jim Ingham184e9812013-01-15 02:47:48 +0000133 bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
134 if (success)
135 unwind_on_error = tmp_value;
136 else
Greg Clayton86edbf42011-10-26 00:56:27 +0000137 error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg);
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000138 break;
139 }
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000140
141 case 'v':
142 if (!option_arg)
143 {
144 m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull;
145 break;
146 }
147 m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error);
148 if (!error.Success())
149 error.SetErrorStringWithFormat ("unrecognized value for description-verbosity '%s'", option_arg);
150 break;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000151
152 case 'g':
153 debug = true;
154 unwind_on_error = false;
155 ignore_breakpoints = false;
156 break;
157
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000159 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160 break;
161 }
162
163 return error;
164}
165
166void
Greg Clayton1deb7962011-10-25 06:44:01 +0000167CommandObjectExpression::CommandOptions::OptionParsingStarting (CommandInterpreter &interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168{
Jim Ingham184e9812013-01-15 02:47:48 +0000169 Process *process = interpreter.GetExecutionContext().GetProcessPtr();
170 if (process != NULL)
171 {
172 ignore_breakpoints = process->GetIgnoreBreakpointsInExpressions();
173 unwind_on_error = process->GetUnwindOnErrorInExpressions();
174 }
175 else
176 {
177 ignore_breakpoints = false;
178 unwind_on_error = true;
179 }
180
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 show_summary = true;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000182 try_all_threads = true;
183 timeout = 0;
Greg Clayton62afb9f2013-11-04 19:35:17 +0000184 debug = false;
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000185 m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186}
187
Greg Claytone0d378b2011-03-24 21:19:54 +0000188const OptionDefinition*
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189CommandObjectExpression::CommandOptions::GetDefinitions ()
190{
191 return g_option_table;
192}
193
Greg Claytona7015092010-09-18 01:14:36 +0000194CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000195 CommandObjectRaw (interpreter,
196 "expression",
Jim Ingham5c48d5c2012-10-25 18:11:24 +0000197 "Evaluate a C/ObjC/C++ expression in the current program context, using user defined variables and variables currently in scope.",
Jim Ingham5a988412012-06-08 21:56:10 +0000198 NULL,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000199 eFlagProcessMustBePaused | eFlagTryTargetAPILock),
Greg Clayton1deb7962011-10-25 06:44:01 +0000200 m_option_group (interpreter),
201 m_format_options (eFormatDefault),
202 m_command_options (),
Johnny Chenf7edb1c2010-09-30 18:30:25 +0000203 m_expr_line_count (0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 m_expr_lines ()
205{
206 SetHelpLong(
Jim Ingham35e1bda2012-10-16 21:41:58 +0000207"Timeouts:\n\
208 If the expression can be evaluated statically (without runnning code) then it will be.\n\
209 Otherwise, by default the expression will run on the current thread with a short timeout:\n\
210 currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted\n\
211 and resumed with all threads running. You can use the -a option to disable retrying on all\n\
212 threads. You can use the -t option to set a shorter timeout.\n\
Jim Ingham5c48d5c2012-10-25 18:11:24 +0000213\n\
214User defined variables:\n\
215 You can define your own variables for convenience or to be used in subsequent expressions.\n\
216 You define them the same way you would define variables in C. If the first character of \n\
217 your user defined variable is a $, then the variable's value will be available in future\n\
218 expressions, otherwise it will just be available in the current expression.\n\
219\n\
Jim Ingham35e1bda2012-10-16 21:41:58 +0000220Examples: \n\
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221\n\
222 expr my_struct->a = my_array[3] \n\
223 expr -f bin -- (index * 8) + 5 \n\
Jim Ingham5c48d5c2012-10-25 18:11:24 +0000224 expr unsigned int $foo = 5\n\
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225 expr char c[] = \"foo\"; c[0]\n");
Caroline Tice405fe672010-10-04 22:28:36 +0000226
227 CommandArgumentEntry arg;
228 CommandArgumentData expression_arg;
229
230 // Define the first (and only) variant of this arg.
231 expression_arg.arg_type = eArgTypeExpression;
232 expression_arg.arg_repetition = eArgRepeatPlain;
233
234 // There is only one variant this argument could be; put it into the argument entry.
235 arg.push_back (expression_arg);
236
237 // Push the data for the first argument into the m_arguments vector.
238 m_arguments.push_back (arg);
Greg Clayton1deb7962011-10-25 06:44:01 +0000239
Greg Clayton5009f9d2011-10-27 17:55:14 +0000240 // Add the "--format" and "--gdb-format"
241 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 +0000242 m_option_group.Append (&m_command_options);
Enrico Granatab576bba2013-01-09 20:12:53 +0000243 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1 | LLDB_OPT_SET_2);
Greg Clayton1deb7962011-10-25 06:44:01 +0000244 m_option_group.Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245}
246
247CommandObjectExpression::~CommandObjectExpression ()
248{
249}
250
251Options *
252CommandObjectExpression::GetOptions ()
253{
Greg Clayton1deb7962011-10-25 06:44:01 +0000254 return &m_option_group;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255}
256
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257size_t
258CommandObjectExpression::MultiLineExpressionCallback
259(
260 void *baton,
Greg Clayton66111032010-06-23 01:19:29 +0000261 InputReader &reader,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262 lldb::InputReaderAction notification,
263 const char *bytes,
264 size_t bytes_len
265)
266{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
Caroline Ticed61c10b2011-06-16 16:27:19 +0000268 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
269
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270 switch (notification)
271 {
272 case eInputReaderActivate:
Caroline Ticed61c10b2011-06-16 16:27:19 +0000273 if (!batch_mode)
Caroline Tice15356e72011-06-15 19:35:17 +0000274 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000275 StreamSP async_strm_sp(reader.GetDebugger().GetAsyncOutputStream());
276 if (async_strm_sp)
277 {
278 async_strm_sp->PutCString("Enter expressions, then terminate with an empty line to evaluate:\n");
279 async_strm_sp->Flush();
280 }
Caroline Tice15356e72011-06-15 19:35:17 +0000281 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 // Fall through
283 case eInputReaderReactivate:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284 break;
285
286 case eInputReaderDeactivate:
287 break;
288
Caroline Tice969ed3d2011-05-02 20:41:46 +0000289 case eInputReaderAsynchronousOutputWritten:
290 break;
291
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292 case eInputReaderGotToken:
293 ++cmd_object_expr->m_expr_line_count;
294 if (bytes && bytes_len)
295 {
296 cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
297 }
298
299 if (bytes_len == 0)
Greg Clayton66111032010-06-23 01:19:29 +0000300 reader.SetIsDone(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301 break;
302
Caroline Ticeefed6132010-11-19 20:47:54 +0000303 case eInputReaderInterrupt:
304 cmd_object_expr->m_expr_lines.clear();
305 reader.SetIsDone (true);
Caroline Ticed61c10b2011-06-16 16:27:19 +0000306 if (!batch_mode)
Caroline Tice15356e72011-06-15 19:35:17 +0000307 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000308 StreamSP async_strm_sp (reader.GetDebugger().GetAsyncOutputStream());
309 if (async_strm_sp)
310 {
311 async_strm_sp->PutCString("Expression evaluation cancelled.\n");
312 async_strm_sp->Flush();
313 }
Caroline Tice15356e72011-06-15 19:35:17 +0000314 }
Caroline Ticeefed6132010-11-19 20:47:54 +0000315 break;
316
317 case eInputReaderEndOfFile:
318 reader.SetIsDone (true);
319 break;
320
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 case eInputReaderDone:
Caroline Ticeefed6132010-11-19 20:47:54 +0000322 if (cmd_object_expr->m_expr_lines.size() > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 {
Caroline Tice6e8dc332011-06-13 20:20:29 +0000324 StreamSP output_stream = reader.GetDebugger().GetAsyncOutputStream();
325 StreamSP error_stream = reader.GetDebugger().GetAsyncErrorStream();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
Caroline Tice6e8dc332011-06-13 20:20:29 +0000327 output_stream.get(),
328 error_stream.get());
329 output_stream->Flush();
330 error_stream->Flush();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331 }
332 break;
333 }
334
335 return bytes_len;
336}
337
338bool
Greg Clayton1d3afba2010-10-05 00:00:42 +0000339CommandObjectExpression::EvaluateExpression
340(
341 const char *expr,
Caroline Tice6e8dc332011-06-13 20:20:29 +0000342 Stream *output_stream,
343 Stream *error_stream,
Greg Clayton1d3afba2010-10-05 00:00:42 +0000344 CommandReturnObject *result
345)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346{
Greg Claytonba7b8e22013-01-26 23:54:29 +0000347 // Don't use m_exe_ctx as this might be called asynchronously
348 // after the command object DoExecute has finished when doing
349 // multi-line expression that use an input reader...
350 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
351
352 Target *target = exe_ctx.GetTargetPtr();
Sean Callananc0a6e062011-10-27 21:22:25 +0000353
354 if (!target)
355 target = Host::GetDummyTarget(m_interpreter.GetDebugger()).get();
356
Greg Claytonc14ee322011-09-22 04:58:26 +0000357 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000359 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton32c40852010-10-06 03:09:11 +0000360
Greg Claytone0d378b2011-03-24 21:19:54 +0000361 ExecutionResults exe_results;
Sean Callanan92adcac2011-01-13 08:53:35 +0000362
363 bool keep_in_memory = true;
Enrico Granatab576bba2013-01-09 20:12:53 +0000364
Jim Ingham35e1bda2012-10-16 21:41:58 +0000365 EvaluateExpressionOptions options;
Enrico Granatab576bba2013-01-09 20:12:53 +0000366 options.SetCoerceToId(m_varobj_options.use_objc)
Enrico Granatad4439aa2012-09-05 20:41:26 +0000367 .SetUnwindOnError(m_command_options.unwind_on_error)
Jim Ingham184e9812013-01-15 02:47:48 +0000368 .SetIgnoreBreakpoints (m_command_options.ignore_breakpoints)
Enrico Granatad4439aa2012-09-05 20:41:26 +0000369 .SetKeepInMemory(keep_in_memory)
Enrico Granatab576bba2013-01-09 20:12:53 +0000370 .SetUseDynamic(m_varobj_options.use_dynamic)
Jim Ingham35e1bda2012-10-16 21:41:58 +0000371 .SetRunOthers(m_command_options.try_all_threads)
Greg Clayton62afb9f2013-11-04 19:35:17 +0000372 .SetDebug(m_command_options.debug);
373
374 if (m_command_options.timeout > 0)
375 options.SetTimeoutUsec(m_command_options.timeout);
Enrico Granatad4439aa2012-09-05 20:41:26 +0000376
Greg Claytonc14ee322011-09-22 04:58:26 +0000377 exe_results = target->EvaluateExpression (expr,
Greg Claytonba7b8e22013-01-26 23:54:29 +0000378 exe_ctx.GetFramePtr(),
Enrico Granata3372f582012-07-16 23:10:35 +0000379 result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +0000380 options);
Sean Callanan4b388c92013-07-30 19:54:09 +0000381
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000382 if (result_valobj_sp)
383 {
Sean Callananbf154da2012-08-08 17:35:10 +0000384 Format format = m_format_options.GetFormat();
385
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000386 if (result_valobj_sp->GetError().Success())
387 {
Sean Callananbf154da2012-08-08 17:35:10 +0000388 if (format != eFormatVoid)
389 {
390 if (format != eFormatDefault)
391 result_valobj_sp->SetFormat (format);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000392
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000393 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(m_command_options.m_verbosity,format));
Enrico Granatab576bba2013-01-09 20:12:53 +0000394
Enrico Granata4d93b8c2013-09-30 19:11:51 +0000395 result_valobj_sp->Dump(*output_stream,options);
396
Sean Callananbf154da2012-08-08 17:35:10 +0000397 if (result)
398 result->SetStatus (eReturnStatusSuccessFinishResult);
399 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000400 }
401 else
402 {
Sean Callananbccce812011-08-23 21:20:51 +0000403 if (result_valobj_sp->GetError().GetError() == ClangUserExpression::kNoResult)
Greg Clayton5fd05902011-06-24 22:31:10 +0000404 {
Sean Callananbcf897f2012-08-09 18:18:47 +0000405 if (format != eFormatVoid && m_interpreter.GetDebugger().GetNotifyVoid())
Sean Callananbf154da2012-08-08 17:35:10 +0000406 {
Sean Callananbcf897f2012-08-09 18:18:47 +0000407 error_stream->PutCString("(void)\n");
Sean Callananbf154da2012-08-08 17:35:10 +0000408 }
Sean Callananbcf897f2012-08-09 18:18:47 +0000409
410 if (result)
411 result->SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton5fd05902011-06-24 22:31:10 +0000412 }
413 else
414 {
Sean Callananbccce812011-08-23 21:20:51 +0000415 const char *error_cstr = result_valobj_sp->GetError().AsCString();
416 if (error_cstr && error_cstr[0])
417 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000418 const size_t error_cstr_len = strlen (error_cstr);
Sean Callananbccce812011-08-23 21:20:51 +0000419 const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n';
420 if (strstr(error_cstr, "error:") != error_cstr)
421 error_stream->PutCString ("error: ");
422 error_stream->Write(error_cstr, error_cstr_len);
423 if (!ends_with_newline)
424 error_stream->EOL();
425 }
426 else
427 {
428 error_stream->PutCString ("error: unknown error\n");
429 }
430
431 if (result)
432 result->SetStatus (eReturnStatusFailed);
Greg Clayton5fd05902011-06-24 22:31:10 +0000433 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000434 }
435 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000436 }
437 else
438 {
Caroline Tice6e8dc332011-06-13 20:20:29 +0000439 error_stream->Printf ("error: invalid execution context for expression\n");
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000440 return false;
Sean Callananebf77072010-07-23 00:16:21 +0000441 }
Sean Callanand1e5b432010-08-12 01:56:52 +0000442
Sean Callananebf77072010-07-23 00:16:21 +0000443 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444}
445
446bool
Jim Ingham5a988412012-06-08 21:56:10 +0000447CommandObjectExpression::DoExecute
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448(
449 const char *command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 CommandReturnObject &result
451)
452{
Greg Clayton1deb7962011-10-25 06:44:01 +0000453 m_option_group.NotifyOptionParsingStarting();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454
455 const char * expr = NULL;
456
457 if (command[0] == '\0')
458 {
459 m_expr_lines.clear();
460 m_expr_line_count = 0;
461
Greg Claytona7015092010-09-18 01:14:36 +0000462 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 if (reader_sp)
464 {
465 Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
466 this, // baton
467 eInputReaderGranularityLine, // token size, to pass to callback function
Greg Clayton66111032010-06-23 01:19:29 +0000468 NULL, // end token
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 NULL, // prompt
470 true)); // echo input
471 if (err.Success())
472 {
Greg Claytona7015092010-09-18 01:14:36 +0000473 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474 result.SetStatus (eReturnStatusSuccessFinishNoResult);
475 }
476 else
477 {
478 result.AppendError (err.AsCString());
479 result.SetStatus (eReturnStatusFailed);
480 }
481 }
482 else
483 {
484 result.AppendError("out of memory");
485 result.SetStatus (eReturnStatusFailed);
486 }
487 return result.Succeeded();
488 }
489
490 if (command[0] == '-')
491 {
492 // We have some options and these options MUST end with --.
493 const char *end_options = NULL;
494 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 {
Greg Clayton66111032010-06-23 01:19:29 +0000514 Args args (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 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 }
526 }
527
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528 if (expr == NULL)
529 expr = command;
Sean Callanan16ad5fa2010-06-24 00:16:27 +0000530
Caroline Tice6e8dc332011-06-13 20:20:29 +0000531 if (EvaluateExpression (expr, &(result.GetOutputStream()), &(result.GetErrorStream()), &result))
Johnny Chenfcd43b72010-08-13 00:42:30 +0000532 return true;
533
534 result.SetStatus (eReturnStatusFailed);
535 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536}
537