blob: 39f22fd747c760dbda4019dd084169f6fd8fd66a [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Expression/ClangExpressionVariable.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000023#include "lldb/Expression/ClangUserExpression.h"
Stephen Wilsonebb84b22010-07-23 21:47:22 +000024#include "lldb/Expression/ClangFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Expression/DWARFExpression.h"
26#include "lldb/Host/Host.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 Ingham6c68fb42010-09-30 00:54:27 +000030#include "lldb/Target/ObjCLanguageRuntime.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"
34#include "lldb/Target/StackFrame.h"
35#include "lldb/Target/Target.h"
Greg Clayton7260f622011-04-18 08:33:37 +000036#include "lldb/Target/Thread.h"
Sean Callananebf77072010-07-23 00:16:21 +000037#include "llvm/ADT/StringRef.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038
39using namespace lldb;
40using namespace lldb_private;
41
Greg Clayton1deb7962011-10-25 06:44:01 +000042CommandObjectExpression::CommandOptions::CommandOptions () :
43 OptionGroup()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044{
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045}
46
47
48CommandObjectExpression::CommandOptions::~CommandOptions ()
49{
50}
51
Greg Clayton1deb7962011-10-25 06:44:01 +000052OptionDefinition
53CommandObjectExpression::CommandOptions::g_option_table[] =
54{
Jim Ingham35e1bda2012-10-16 21:41:58 +000055 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "all-threads", 'a', required_argument, NULL, 0, eArgTypeBoolean, "Should we run all threads if the execution doesn't complete on one thread."},
Greg Clayton1deb7962011-10-25 06:44:01 +000056 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "dynamic-value", 'd', required_argument, NULL, 0, eArgTypeBoolean, "Upcast the value resulting from the expression to its dynamic type if available."},
Jim Ingham35e1bda2012-10-16 21:41:58 +000057 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "timeout", 't', required_argument, NULL, 0, eArgTypeUnsignedInteger, "Timeout value for running the expression."},
Greg Clayton1deb7962011-10-25 06:44:01 +000058 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "unwind-on-error", 'u', required_argument, NULL, 0, eArgTypeBoolean, "Clean up program state if the expression causes a crash, breakpoint hit or signal."},
Enrico Granata7b8c5132012-12-12 03:23:37 +000059 { LLDB_OPT_SET_2 , false, "object-description", 'O', no_argument, NULL, 0, eArgTypeNone, "Print the object description of the value resulting from the expression."},
Greg Clayton1deb7962011-10-25 06:44:01 +000060};
61
62
63uint32_t
64CommandObjectExpression::CommandOptions::GetNumDefinitions ()
65{
66 return sizeof(g_option_table)/sizeof(OptionDefinition);
67}
68
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069Error
Greg Clayton1deb7962011-10-25 06:44:01 +000070CommandObjectExpression::CommandOptions::SetOptionValue (CommandInterpreter &interpreter,
71 uint32_t option_idx,
72 const char *option_arg)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073{
74 Error error;
75
Greg Clayton3bcdfc02012-12-04 00:32:51 +000076 const int short_option = g_option_table[option_idx].short_option;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077
78 switch (short_option)
79 {
Caroline Tice3f4c09c2010-09-07 22:38:08 +000080 //case 'l':
81 //if (language.SetLanguageFromCString (option_arg) == false)
82 //{
Greg Clayton86edbf42011-10-26 00:56:27 +000083 // error.SetErrorStringWithFormat("invalid language option argument '%s'", option_arg);
Caroline Tice3f4c09c2010-09-07 22:38:08 +000084 //}
85 //break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086
Jim Ingham35e1bda2012-10-16 21:41:58 +000087 case 'a':
88 {
89 bool success;
90 bool result;
91 result = Args::StringToBoolean(option_arg, true, &success);
92 if (!success)
93 error.SetErrorStringWithFormat("invalid all-threads value setting: \"%s\"", option_arg);
94 else
95 try_all_threads = result;
96 }
Jim Ingham6c68fb42010-09-30 00:54:27 +000097 break;
Jim Ingham399f1ca2010-11-05 19:25:48 +000098
Jim Ingham78a685a2011-04-16 00:01:13 +000099 case 'd':
100 {
101 bool success;
102 bool result;
103 result = Args::StringToBoolean(option_arg, true, &success);
104 if (!success)
Greg Clayton86edbf42011-10-26 00:56:27 +0000105 error.SetErrorStringWithFormat("invalid dynamic value setting: \"%s\"", option_arg);
Jim Ingham78a685a2011-04-16 00:01:13 +0000106 else
107 {
108 if (result)
Greg Clayton007d5be2011-05-30 00:49:24 +0000109 use_dynamic = eLazyBoolYes;
Jim Ingham78a685a2011-04-16 00:01:13 +0000110 else
111 use_dynamic = eLazyBoolNo;
112 }
113 }
114 break;
115
Enrico Granata7b8c5132012-12-12 03:23:37 +0000116 case 'O':
Jim Ingham35e1bda2012-10-16 21:41:58 +0000117 print_object = true;
118 break;
119
120 case 't':
121 {
122 bool success;
123 uint32_t result;
124 result = Args::StringToUInt32(option_arg, 0, 0, &success);
125 if (success)
126 timeout = result;
127 else
128 error.SetErrorStringWithFormat ("invalid timeout setting \"%s\"", option_arg);
129 }
130 break;
131
Jim Ingham399f1ca2010-11-05 19:25:48 +0000132 case 'u':
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000133 {
134 bool success;
135 unwind_on_error = Args::StringToBoolean(option_arg, true, &success);
136 if (!success)
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 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000141 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 break;
143 }
144
145 return error;
146}
147
148void
Greg Clayton1deb7962011-10-25 06:44:01 +0000149CommandObjectExpression::CommandOptions::OptionParsingStarting (CommandInterpreter &interpreter)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150{
Jim Ingham78a685a2011-04-16 00:01:13 +0000151 use_dynamic = eLazyBoolCalculate;
Greg Clayton1deb7962011-10-25 06:44:01 +0000152 print_object = false;
Jim Ingham399f1ca2010-11-05 19:25:48 +0000153 unwind_on_error = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 show_types = true;
155 show_summary = true;
Jim Ingham35e1bda2012-10-16 21:41:58 +0000156 try_all_threads = true;
157 timeout = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
Greg Claytone0d378b2011-03-24 21:19:54 +0000160const OptionDefinition*
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161CommandObjectExpression::CommandOptions::GetDefinitions ()
162{
163 return g_option_table;
164}
165
Greg Claytona7015092010-09-18 01:14:36 +0000166CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
Jim Ingham5a988412012-06-08 21:56:10 +0000167 CommandObjectRaw (interpreter,
168 "expression",
Jim Ingham5c48d5c2012-10-25 18:11:24 +0000169 "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 +0000170 NULL,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000171 eFlagProcessMustBePaused | eFlagTryTargetAPILock),
Greg Clayton1deb7962011-10-25 06:44:01 +0000172 m_option_group (interpreter),
173 m_format_options (eFormatDefault),
174 m_command_options (),
Johnny Chenf7edb1c2010-09-30 18:30:25 +0000175 m_expr_line_count (0),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176 m_expr_lines ()
177{
178 SetHelpLong(
Jim Ingham35e1bda2012-10-16 21:41:58 +0000179"Timeouts:\n\
180 If the expression can be evaluated statically (without runnning code) then it will be.\n\
181 Otherwise, by default the expression will run on the current thread with a short timeout:\n\
182 currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted\n\
183 and resumed with all threads running. You can use the -a option to disable retrying on all\n\
184 threads. You can use the -t option to set a shorter timeout.\n\
Jim Ingham5c48d5c2012-10-25 18:11:24 +0000185\n\
186User defined variables:\n\
187 You can define your own variables for convenience or to be used in subsequent expressions.\n\
188 You define them the same way you would define variables in C. If the first character of \n\
189 your user defined variable is a $, then the variable's value will be available in future\n\
190 expressions, otherwise it will just be available in the current expression.\n\
191\n\
Jim Ingham35e1bda2012-10-16 21:41:58 +0000192Examples: \n\
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193\n\
194 expr my_struct->a = my_array[3] \n\
195 expr -f bin -- (index * 8) + 5 \n\
Jim Ingham5c48d5c2012-10-25 18:11:24 +0000196 expr unsigned int $foo = 5\n\
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197 expr char c[] = \"foo\"; c[0]\n");
Caroline Tice405fe672010-10-04 22:28:36 +0000198
199 CommandArgumentEntry arg;
200 CommandArgumentData expression_arg;
201
202 // Define the first (and only) variant of this arg.
203 expression_arg.arg_type = eArgTypeExpression;
204 expression_arg.arg_repetition = eArgRepeatPlain;
205
206 // There is only one variant this argument could be; put it into the argument entry.
207 arg.push_back (expression_arg);
208
209 // Push the data for the first argument into the m_arguments vector.
210 m_arguments.push_back (arg);
Greg Clayton1deb7962011-10-25 06:44:01 +0000211
Greg Clayton5009f9d2011-10-27 17:55:14 +0000212 // Add the "--format" and "--gdb-format"
213 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 +0000214 m_option_group.Append (&m_command_options);
215 m_option_group.Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216}
217
218CommandObjectExpression::~CommandObjectExpression ()
219{
220}
221
222Options *
223CommandObjectExpression::GetOptions ()
224{
Greg Clayton1deb7962011-10-25 06:44:01 +0000225 return &m_option_group;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226}
227
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228size_t
229CommandObjectExpression::MultiLineExpressionCallback
230(
231 void *baton,
Greg Clayton66111032010-06-23 01:19:29 +0000232 InputReader &reader,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233 lldb::InputReaderAction notification,
234 const char *bytes,
235 size_t bytes_len
236)
237{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
Caroline Ticed61c10b2011-06-16 16:27:19 +0000239 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
240
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241 switch (notification)
242 {
243 case eInputReaderActivate:
Caroline Ticed61c10b2011-06-16 16:27:19 +0000244 if (!batch_mode)
Caroline Tice15356e72011-06-15 19:35:17 +0000245 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000246 StreamSP async_strm_sp(reader.GetDebugger().GetAsyncOutputStream());
247 if (async_strm_sp)
248 {
249 async_strm_sp->PutCString("Enter expressions, then terminate with an empty line to evaluate:\n");
250 async_strm_sp->Flush();
251 }
Caroline Tice15356e72011-06-15 19:35:17 +0000252 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253 // Fall through
254 case eInputReaderReactivate:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255 break;
256
257 case eInputReaderDeactivate:
258 break;
259
Caroline Tice969ed3d2011-05-02 20:41:46 +0000260 case eInputReaderAsynchronousOutputWritten:
261 break;
262
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263 case eInputReaderGotToken:
264 ++cmd_object_expr->m_expr_line_count;
265 if (bytes && bytes_len)
266 {
267 cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
268 }
269
270 if (bytes_len == 0)
Greg Clayton66111032010-06-23 01:19:29 +0000271 reader.SetIsDone(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272 break;
273
Caroline Ticeefed6132010-11-19 20:47:54 +0000274 case eInputReaderInterrupt:
275 cmd_object_expr->m_expr_lines.clear();
276 reader.SetIsDone (true);
Caroline Ticed61c10b2011-06-16 16:27:19 +0000277 if (!batch_mode)
Caroline Tice15356e72011-06-15 19:35:17 +0000278 {
Greg Clayton07e66e32011-07-20 03:41:06 +0000279 StreamSP async_strm_sp (reader.GetDebugger().GetAsyncOutputStream());
280 if (async_strm_sp)
281 {
282 async_strm_sp->PutCString("Expression evaluation cancelled.\n");
283 async_strm_sp->Flush();
284 }
Caroline Tice15356e72011-06-15 19:35:17 +0000285 }
Caroline Ticeefed6132010-11-19 20:47:54 +0000286 break;
287
288 case eInputReaderEndOfFile:
289 reader.SetIsDone (true);
290 break;
291
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292 case eInputReaderDone:
Caroline Ticeefed6132010-11-19 20:47:54 +0000293 if (cmd_object_expr->m_expr_lines.size() > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294 {
Caroline Tice6e8dc332011-06-13 20:20:29 +0000295 StreamSP output_stream = reader.GetDebugger().GetAsyncOutputStream();
296 StreamSP error_stream = reader.GetDebugger().GetAsyncErrorStream();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
Caroline Tice6e8dc332011-06-13 20:20:29 +0000298 output_stream.get(),
299 error_stream.get());
300 output_stream->Flush();
301 error_stream->Flush();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302 }
303 break;
304 }
305
306 return bytes_len;
307}
308
309bool
Greg Clayton1d3afba2010-10-05 00:00:42 +0000310CommandObjectExpression::EvaluateExpression
311(
312 const char *expr,
Caroline Tice6e8dc332011-06-13 20:20:29 +0000313 Stream *output_stream,
314 Stream *error_stream,
Greg Clayton1d3afba2010-10-05 00:00:42 +0000315 CommandReturnObject *result
316)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317{
Greg Claytonf9fc6092013-01-09 19:44:40 +0000318 Target *target = m_exe_ctx.GetTargetPtr();
Sean Callananc0a6e062011-10-27 21:22:25 +0000319
320 if (!target)
321 target = Host::GetDummyTarget(m_interpreter.GetDebugger()).get();
322
Greg Claytonc14ee322011-09-22 04:58:26 +0000323 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000325 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton32c40852010-10-06 03:09:11 +0000326
Greg Claytone0d378b2011-03-24 21:19:54 +0000327 ExecutionResults exe_results;
Sean Callanan92adcac2011-01-13 08:53:35 +0000328
329 bool keep_in_memory = true;
Jim Ingham2837b762011-05-04 03:43:18 +0000330 lldb::DynamicValueType use_dynamic;
Jim Ingham78a685a2011-04-16 00:01:13 +0000331 // If use dynamic is not set, get it from the target:
Greg Clayton1deb7962011-10-25 06:44:01 +0000332 switch (m_command_options.use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +0000333 {
334 case eLazyBoolCalculate:
Greg Claytonc14ee322011-09-22 04:58:26 +0000335 use_dynamic = target->GetPreferDynamicValue();
Jim Ingham78a685a2011-04-16 00:01:13 +0000336 break;
337 case eLazyBoolYes:
Jim Ingham2837b762011-05-04 03:43:18 +0000338 use_dynamic = lldb::eDynamicCanRunTarget;
Jim Ingham78a685a2011-04-16 00:01:13 +0000339 break;
340 case eLazyBoolNo:
Jim Ingham2837b762011-05-04 03:43:18 +0000341 use_dynamic = lldb::eNoDynamicValues;
Jim Ingham78a685a2011-04-16 00:01:13 +0000342 break;
343 }
Sean Callanan92adcac2011-01-13 08:53:35 +0000344
Jim Ingham35e1bda2012-10-16 21:41:58 +0000345 EvaluateExpressionOptions options;
Enrico Granatad4439aa2012-09-05 20:41:26 +0000346 options.SetCoerceToId(m_command_options.print_object)
347 .SetUnwindOnError(m_command_options.unwind_on_error)
348 .SetKeepInMemory(keep_in_memory)
349 .SetUseDynamic(use_dynamic)
Jim Ingham35e1bda2012-10-16 21:41:58 +0000350 .SetRunOthers(m_command_options.try_all_threads)
351 .SetTimeoutUsec(m_command_options.timeout);
Enrico Granatad4439aa2012-09-05 20:41:26 +0000352
Greg Claytonc14ee322011-09-22 04:58:26 +0000353 exe_results = target->EvaluateExpression (expr,
Greg Claytonf9fc6092013-01-09 19:44:40 +0000354 m_exe_ctx.GetFramePtr(),
Enrico Granata3372f582012-07-16 23:10:35 +0000355 result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +0000356 options);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000357
Greg Clayton1deb7962011-10-25 06:44:01 +0000358 if (exe_results == eExecutionInterrupted && !m_command_options.unwind_on_error)
Jim Inghamf48169b2010-11-30 02:22:11 +0000359 {
Greg Clayton7260f622011-04-18 08:33:37 +0000360 uint32_t start_frame = 0;
361 uint32_t num_frames = 1;
362 uint32_t num_frames_with_source = 0;
Greg Claytonf9fc6092013-01-09 19:44:40 +0000363 Thread *thread = m_exe_ctx.GetThreadPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +0000364 if (thread)
Greg Clayton7260f622011-04-18 08:33:37 +0000365 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000366 thread->GetStatus (result->GetOutputStream(),
367 start_frame,
368 num_frames,
369 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +0000370 }
Greg Claytonc14ee322011-09-22 04:58:26 +0000371 else
Greg Clayton7260f622011-04-18 08:33:37 +0000372 {
Greg Claytonf9fc6092013-01-09 19:44:40 +0000373 Process *process = m_exe_ctx.GetProcessPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +0000374 if (process)
375 {
376 bool only_threads_with_stop_reason = true;
377 process->GetThreadStatus (result->GetOutputStream(),
378 only_threads_with_stop_reason,
379 start_frame,
380 num_frames,
381 num_frames_with_source);
382 }
Greg Clayton7260f622011-04-18 08:33:37 +0000383 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000384 }
Sean Callananbf154da2012-08-08 17:35:10 +0000385
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000386 if (result_valobj_sp)
387 {
Sean Callananbf154da2012-08-08 17:35:10 +0000388 Format format = m_format_options.GetFormat();
389
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000390 if (result_valobj_sp->GetError().Success())
391 {
Sean Callananbf154da2012-08-08 17:35:10 +0000392 if (format != eFormatVoid)
393 {
394 if (format != eFormatDefault)
395 result_valobj_sp->SetFormat (format);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000396
Sean Callananbf154da2012-08-08 17:35:10 +0000397 ValueObject::DumpValueObjectOptions options;
398 options.SetMaximumPointerDepth(0)
399 .SetMaximumDepth(UINT32_MAX)
400 .SetShowLocation(false)
401 .SetShowTypes(m_command_options.show_types)
402 .SetUseObjectiveC(m_command_options.print_object)
403 .SetUseDynamicType(use_dynamic)
404 .SetScopeChecked(true)
405 .SetFlatOutput(false)
406 .SetUseSyntheticValue(true)
407 .SetIgnoreCap(false)
408 .SetFormat(format)
409 .SetSummary()
Enrico Granata2b2631c2012-08-09 16:51:25 +0000410 .SetShowSummary(!m_command_options.print_object)
411 .SetHideRootType(m_command_options.print_object);
Sean Callananbf154da2012-08-08 17:35:10 +0000412
413 ValueObject::DumpValueObject (*(output_stream),
414 result_valobj_sp.get(), // Variable object to dump
415 options);
416 if (result)
417 result->SetStatus (eReturnStatusSuccessFinishResult);
418 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000419 }
420 else
421 {
Sean Callananbccce812011-08-23 21:20:51 +0000422 if (result_valobj_sp->GetError().GetError() == ClangUserExpression::kNoResult)
Greg Clayton5fd05902011-06-24 22:31:10 +0000423 {
Sean Callananbcf897f2012-08-09 18:18:47 +0000424 if (format != eFormatVoid && m_interpreter.GetDebugger().GetNotifyVoid())
Sean Callananbf154da2012-08-08 17:35:10 +0000425 {
Sean Callananbcf897f2012-08-09 18:18:47 +0000426 error_stream->PutCString("(void)\n");
Sean Callananbf154da2012-08-08 17:35:10 +0000427 }
Sean Callananbcf897f2012-08-09 18:18:47 +0000428
429 if (result)
430 result->SetStatus (eReturnStatusSuccessFinishResult);
Greg Clayton5fd05902011-06-24 22:31:10 +0000431 }
432 else
433 {
Sean Callananbccce812011-08-23 21:20:51 +0000434 const char *error_cstr = result_valobj_sp->GetError().AsCString();
435 if (error_cstr && error_cstr[0])
436 {
437 int error_cstr_len = strlen (error_cstr);
438 const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n';
439 if (strstr(error_cstr, "error:") != error_cstr)
440 error_stream->PutCString ("error: ");
441 error_stream->Write(error_cstr, error_cstr_len);
442 if (!ends_with_newline)
443 error_stream->EOL();
444 }
445 else
446 {
447 error_stream->PutCString ("error: unknown error\n");
448 }
449
450 if (result)
451 result->SetStatus (eReturnStatusFailed);
Greg Clayton5fd05902011-06-24 22:31:10 +0000452 }
Jim Inghamf48169b2010-11-30 02:22:11 +0000453 }
454 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000455 }
456 else
457 {
Caroline Tice6e8dc332011-06-13 20:20:29 +0000458 error_stream->Printf ("error: invalid execution context for expression\n");
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000459 return false;
Sean Callananebf77072010-07-23 00:16:21 +0000460 }
Sean Callanand1e5b432010-08-12 01:56:52 +0000461
Sean Callananebf77072010-07-23 00:16:21 +0000462 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463}
464
465bool
Jim Ingham5a988412012-06-08 21:56:10 +0000466CommandObjectExpression::DoExecute
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467(
468 const char *command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 CommandReturnObject &result
470)
471{
Greg Clayton1deb7962011-10-25 06:44:01 +0000472 m_option_group.NotifyOptionParsingStarting();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473
474 const char * expr = NULL;
475
476 if (command[0] == '\0')
477 {
478 m_expr_lines.clear();
479 m_expr_line_count = 0;
480
Greg Claytona7015092010-09-18 01:14:36 +0000481 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 if (reader_sp)
483 {
484 Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
485 this, // baton
486 eInputReaderGranularityLine, // token size, to pass to callback function
Greg Clayton66111032010-06-23 01:19:29 +0000487 NULL, // end token
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 NULL, // prompt
489 true)); // echo input
490 if (err.Success())
491 {
Greg Claytona7015092010-09-18 01:14:36 +0000492 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493 result.SetStatus (eReturnStatusSuccessFinishNoResult);
494 }
495 else
496 {
497 result.AppendError (err.AsCString());
498 result.SetStatus (eReturnStatusFailed);
499 }
500 }
501 else
502 {
503 result.AppendError("out of memory");
504 result.SetStatus (eReturnStatusFailed);
505 }
506 return result.Succeeded();
507 }
508
509 if (command[0] == '-')
510 {
511 // We have some options and these options MUST end with --.
512 const char *end_options = NULL;
513 const char *s = command;
514 while (s && s[0])
515 {
516 end_options = ::strstr (s, "--");
517 if (end_options)
518 {
519 end_options += 2; // Get past the "--"
520 if (::isspace (end_options[0]))
521 {
522 expr = end_options;
523 while (::isspace (*expr))
524 ++expr;
525 break;
526 }
527 }
528 s = end_options;
529 }
530
531 if (end_options)
532 {
Greg Clayton66111032010-06-23 01:19:29 +0000533 Args args (command, end_options - command);
Greg Claytona7015092010-09-18 01:14:36 +0000534 if (!ParseOptions (args, result))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 return false;
Greg Claytonf6b8b582011-04-13 00:18:08 +0000536
Greg Clayton1deb7962011-10-25 06:44:01 +0000537 Error error (m_option_group.NotifyOptionParsingFinished());
Greg Claytonf6b8b582011-04-13 00:18:08 +0000538 if (error.Fail())
539 {
540 result.AppendError (error.AsCString());
541 result.SetStatus (eReturnStatusFailed);
542 return false;
543 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544 }
545 }
546
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547 if (expr == NULL)
548 expr = command;
Sean Callanan16ad5fa2010-06-24 00:16:27 +0000549
Caroline Tice6e8dc332011-06-13 20:20:29 +0000550 if (EvaluateExpression (expr, &(result.GetOutputStream()), &(result.GetErrorStream()), &result))
Johnny Chenfcd43b72010-08-13 00:42:30 +0000551 return true;
552
553 result.SetStatus (eReturnStatusFailed);
554 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555}
556