blob: e28c8e8261d2e1ba7c1733644fcc67ca8e43196d [file] [log] [blame]
Chris Lattner24943d22010-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 Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-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 Callanan841026f2010-07-23 00:16:21 +000018#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/Value.h"
20#include "lldb/Core/InputReader.h"
Jim Ingham324067b2010-09-30 00:54:27 +000021#include "lldb/Core/ValueObjectVariable.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Expression/ClangExpressionVariable.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000023#include "lldb/Expression/ClangUserExpression.h"
Stephen Wilson63c468c2010-07-23 21:47:22 +000024#include "lldb/Expression/ClangFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Expression/DWARFExpression.h"
26#include "lldb/Host/Host.h"
Sean Callanan841026f2010-07-23 00:16:21 +000027#include "lldb/Core/Debugger.h"
Greg Clayton63094e02010-06-23 01:19:29 +000028#include "lldb/Interpreter/CommandInterpreter.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Interpreter/CommandReturnObject.h"
Jim Ingham324067b2010-09-30 00:54:27 +000030#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-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 Claytonabe0fed2011-04-18 08:33:37 +000036#include "lldb/Target/Thread.h"
Sean Callanan841026f2010-07-23 00:16:21 +000037#include "llvm/ADT/StringRef.h"
Chris Lattner24943d22010-06-08 16:52:24 +000038
39using namespace lldb;
40using namespace lldb_private;
41
Greg Claytona42880a2011-10-25 06:44:01 +000042CommandObjectExpression::CommandOptions::CommandOptions () :
43 OptionGroup()
Chris Lattner24943d22010-06-08 16:52:24 +000044{
Chris Lattner24943d22010-06-08 16:52:24 +000045}
46
47
48CommandObjectExpression::CommandOptions::~CommandOptions ()
49{
50}
51
Greg Claytona42880a2011-10-25 06:44:01 +000052OptionDefinition
53CommandObjectExpression::CommandOptions::g_option_table[] =
54{
Jim Ingham47beabb2012-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 Claytona42880a2011-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 Ingham47beabb2012-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 Claytona42880a2011-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."},
59 { LLDB_OPT_SET_2 , false, "object-description", 'o', no_argument, NULL, 0, eArgTypeNone, "Print the object description of the value resulting from the expression."},
60};
61
62
63uint32_t
64CommandObjectExpression::CommandOptions::GetNumDefinitions ()
65{
66 return sizeof(g_option_table)/sizeof(OptionDefinition);
67}
68
Chris Lattner24943d22010-06-08 16:52:24 +000069Error
Greg Claytona42880a2011-10-25 06:44:01 +000070CommandObjectExpression::CommandOptions::SetOptionValue (CommandInterpreter &interpreter,
71 uint32_t option_idx,
72 const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000073{
74 Error error;
75
Greg Clayton6475c422012-12-04 00:32:51 +000076 const int short_option = g_option_table[option_idx].short_option;
Chris Lattner24943d22010-06-08 16:52:24 +000077
78 switch (short_option)
79 {
Caroline Ticec1ad82e2010-09-07 22:38:08 +000080 //case 'l':
81 //if (language.SetLanguageFromCString (option_arg) == false)
82 //{
Greg Clayton9c236732011-10-26 00:56:27 +000083 // error.SetErrorStringWithFormat("invalid language option argument '%s'", option_arg);
Caroline Ticec1ad82e2010-09-07 22:38:08 +000084 //}
85 //break;
Chris Lattner24943d22010-06-08 16:52:24 +000086
Jim Ingham47beabb2012-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 Ingham324067b2010-09-30 00:54:27 +000097 break;
Jim Inghamea9d4262010-11-05 19:25:48 +000098
Jim Inghame41494a2011-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 Clayton9c236732011-10-26 00:56:27 +0000105 error.SetErrorStringWithFormat("invalid dynamic value setting: \"%s\"", option_arg);
Jim Inghame41494a2011-04-16 00:01:13 +0000106 else
107 {
108 if (result)
Greg Clayton82f07462011-05-30 00:49:24 +0000109 use_dynamic = eLazyBoolYes;
Jim Inghame41494a2011-04-16 00:01:13 +0000110 else
111 use_dynamic = eLazyBoolNo;
112 }
113 }
114 break;
115
Jim Ingham47beabb2012-10-16 21:41:58 +0000116 case 'o':
117 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 Inghamea9d4262010-11-05 19:25:48 +0000132 case 'u':
Sean Callanan47dc4572011-09-15 02:13:07 +0000133 {
134 bool success;
135 unwind_on_error = Args::StringToBoolean(option_arg, true, &success);
136 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +0000137 error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg);
Sean Callanan47dc4572011-09-15 02:13:07 +0000138 break;
139 }
Chris Lattner24943d22010-06-08 16:52:24 +0000140 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000141 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000142 break;
143 }
144
145 return error;
146}
147
148void
Greg Claytona42880a2011-10-25 06:44:01 +0000149CommandObjectExpression::CommandOptions::OptionParsingStarting (CommandInterpreter &interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000150{
Jim Inghame41494a2011-04-16 00:01:13 +0000151 use_dynamic = eLazyBoolCalculate;
Greg Claytona42880a2011-10-25 06:44:01 +0000152 print_object = false;
Jim Inghamea9d4262010-11-05 19:25:48 +0000153 unwind_on_error = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000154 show_types = true;
155 show_summary = true;
Jim Ingham47beabb2012-10-16 21:41:58 +0000156 try_all_threads = true;
157 timeout = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000158}
159
Greg Claytonb3448432011-03-24 21:19:54 +0000160const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000161CommandObjectExpression::CommandOptions::GetDefinitions ()
162{
163 return g_option_table;
164}
165
Greg Clayton238c0a12010-09-18 01:14:36 +0000166CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000167 CommandObjectRaw (interpreter,
168 "expression",
Jim Ingham02ed2852012-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 Inghamda26bd22012-06-08 21:56:10 +0000170 NULL,
171 eFlagProcessMustBePaused),
Greg Claytona42880a2011-10-25 06:44:01 +0000172 m_option_group (interpreter),
173 m_format_options (eFormatDefault),
174 m_command_options (),
Johnny Chencc112ac2010-09-30 18:30:25 +0000175 m_expr_line_count (0),
Chris Lattner24943d22010-06-08 16:52:24 +0000176 m_expr_lines ()
177{
178 SetHelpLong(
Jim Ingham47beabb2012-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 Ingham02ed2852012-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 Ingham47beabb2012-10-16 21:41:58 +0000192Examples: \n\
Chris Lattner24943d22010-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 Ingham02ed2852012-10-25 18:11:24 +0000196 expr unsigned int $foo = 5\n\
Chris Lattner24943d22010-06-08 16:52:24 +0000197 expr char c[] = \"foo\"; c[0]\n");
Caroline Tice43b014a2010-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 Claytona42880a2011-10-25 06:44:01 +0000211
Greg Clayton24a6bd92011-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 Claytona42880a2011-10-25 06:44:01 +0000214 m_option_group.Append (&m_command_options);
215 m_option_group.Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000216}
217
218CommandObjectExpression::~CommandObjectExpression ()
219{
220}
221
222Options *
223CommandObjectExpression::GetOptions ()
224{
Greg Claytona42880a2011-10-25 06:44:01 +0000225 return &m_option_group;
Chris Lattner24943d22010-06-08 16:52:24 +0000226}
227
Chris Lattner24943d22010-06-08 16:52:24 +0000228size_t
229CommandObjectExpression::MultiLineExpressionCallback
230(
231 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +0000232 InputReader &reader,
Chris Lattner24943d22010-06-08 16:52:24 +0000233 lldb::InputReaderAction notification,
234 const char *bytes,
235 size_t bytes_len
236)
237{
Chris Lattner24943d22010-06-08 16:52:24 +0000238 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
Caroline Tice892fadd2011-06-16 16:27:19 +0000239 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
240
Chris Lattner24943d22010-06-08 16:52:24 +0000241 switch (notification)
242 {
243 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +0000244 if (!batch_mode)
Caroline Tice2b5e4e62011-06-15 19:35:17 +0000245 {
Greg Clayton234981a2011-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 Tice2b5e4e62011-06-15 19:35:17 +0000252 }
Chris Lattner24943d22010-06-08 16:52:24 +0000253 // Fall through
254 case eInputReaderReactivate:
Chris Lattner24943d22010-06-08 16:52:24 +0000255 break;
256
257 case eInputReaderDeactivate:
258 break;
259
Caroline Tice4a348082011-05-02 20:41:46 +0000260 case eInputReaderAsynchronousOutputWritten:
261 break;
262
Chris Lattner24943d22010-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 Clayton63094e02010-06-23 01:19:29 +0000271 reader.SetIsDone(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000272 break;
273
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000274 case eInputReaderInterrupt:
275 cmd_object_expr->m_expr_lines.clear();
276 reader.SetIsDone (true);
Caroline Tice892fadd2011-06-16 16:27:19 +0000277 if (!batch_mode)
Caroline Tice2b5e4e62011-06-15 19:35:17 +0000278 {
Greg Clayton234981a2011-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 Tice2b5e4e62011-06-15 19:35:17 +0000285 }
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000286 break;
287
288 case eInputReaderEndOfFile:
289 reader.SetIsDone (true);
290 break;
291
Chris Lattner24943d22010-06-08 16:52:24 +0000292 case eInputReaderDone:
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000293 if (cmd_object_expr->m_expr_lines.size() > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000294 {
Caroline Tice87e1f772011-06-13 20:20:29 +0000295 StreamSP output_stream = reader.GetDebugger().GetAsyncOutputStream();
296 StreamSP error_stream = reader.GetDebugger().GetAsyncErrorStream();
Chris Lattner24943d22010-06-08 16:52:24 +0000297 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
Caroline Tice87e1f772011-06-13 20:20:29 +0000298 output_stream.get(),
299 error_stream.get());
300 output_stream->Flush();
301 error_stream->Flush();
Chris Lattner24943d22010-06-08 16:52:24 +0000302 }
303 break;
304 }
305
306 return bytes_len;
307}
308
309bool
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000310CommandObjectExpression::EvaluateExpression
311(
312 const char *expr,
Caroline Tice87e1f772011-06-13 20:20:29 +0000313 Stream *output_stream,
314 Stream *error_stream,
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000315 CommandReturnObject *result
316)
Chris Lattner24943d22010-06-08 16:52:24 +0000317{
Greg Claytonf75e3912012-01-27 18:18:23 +0000318 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
Sean Callananf35a96c2011-10-27 21:22:25 +0000319
320 if (!target)
321 target = Host::GetDummyTarget(m_interpreter.GetDebugger()).get();
322
Greg Clayton567e7f32011-09-22 04:58:26 +0000323 if (target)
Chris Lattner24943d22010-06-08 16:52:24 +0000324 {
Greg Clayton427f2902010-12-14 02:59:59 +0000325 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton11730f32010-10-06 03:09:11 +0000326
Greg Claytonb3448432011-03-24 21:19:54 +0000327 ExecutionResults exe_results;
Sean Callanan6a925532011-01-13 08:53:35 +0000328
329 bool keep_in_memory = true;
Jim Ingham10de7d12011-05-04 03:43:18 +0000330 lldb::DynamicValueType use_dynamic;
Jim Inghame41494a2011-04-16 00:01:13 +0000331 // If use dynamic is not set, get it from the target:
Greg Claytona42880a2011-10-25 06:44:01 +0000332 switch (m_command_options.use_dynamic)
Jim Inghame41494a2011-04-16 00:01:13 +0000333 {
334 case eLazyBoolCalculate:
Greg Clayton567e7f32011-09-22 04:58:26 +0000335 use_dynamic = target->GetPreferDynamicValue();
Jim Inghame41494a2011-04-16 00:01:13 +0000336 break;
337 case eLazyBoolYes:
Jim Ingham10de7d12011-05-04 03:43:18 +0000338 use_dynamic = lldb::eDynamicCanRunTarget;
Jim Inghame41494a2011-04-16 00:01:13 +0000339 break;
340 case eLazyBoolNo:
Jim Ingham10de7d12011-05-04 03:43:18 +0000341 use_dynamic = lldb::eNoDynamicValues;
Jim Inghame41494a2011-04-16 00:01:13 +0000342 break;
343 }
Sean Callanan6a925532011-01-13 08:53:35 +0000344
Jim Ingham47beabb2012-10-16 21:41:58 +0000345 EvaluateExpressionOptions options;
Enrico Granatad27026e2012-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 Ingham47beabb2012-10-16 21:41:58 +0000350 .SetRunOthers(m_command_options.try_all_threads)
351 .SetTimeoutUsec(m_command_options.timeout);
Enrico Granatad27026e2012-09-05 20:41:26 +0000352
Greg Clayton567e7f32011-09-22 04:58:26 +0000353 exe_results = target->EvaluateExpression (expr,
Greg Claytonf75e3912012-01-27 18:18:23 +0000354 m_interpreter.GetExecutionContext().GetFramePtr(),
Enrico Granata6cca9692012-07-16 23:10:35 +0000355 result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +0000356 options);
Greg Clayton427f2902010-12-14 02:59:59 +0000357
Greg Claytona42880a2011-10-25 06:44:01 +0000358 if (exe_results == eExecutionInterrupted && !m_command_options.unwind_on_error)
Jim Ingham360f53f2010-11-30 02:22:11 +0000359 {
Greg Claytonabe0fed2011-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 Claytonf75e3912012-01-27 18:18:23 +0000363 Thread *thread = m_interpreter.GetExecutionContext().GetThreadPtr();
Greg Clayton567e7f32011-09-22 04:58:26 +0000364 if (thread)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000365 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000366 thread->GetStatus (result->GetOutputStream(),
367 start_frame,
368 num_frames,
369 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000370 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000371 else
Greg Claytonabe0fed2011-04-18 08:33:37 +0000372 {
Greg Claytonf75e3912012-01-27 18:18:23 +0000373 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Greg Clayton567e7f32011-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 Claytonabe0fed2011-04-18 08:33:37 +0000383 }
Greg Clayton427f2902010-12-14 02:59:59 +0000384 }
Sean Callanan96abc622012-08-08 17:35:10 +0000385
Greg Clayton427f2902010-12-14 02:59:59 +0000386 if (result_valobj_sp)
387 {
Sean Callanan96abc622012-08-08 17:35:10 +0000388 Format format = m_format_options.GetFormat();
389
Greg Clayton427f2902010-12-14 02:59:59 +0000390 if (result_valobj_sp->GetError().Success())
391 {
Sean Callanan96abc622012-08-08 17:35:10 +0000392 if (format != eFormatVoid)
393 {
394 if (format != eFormatDefault)
395 result_valobj_sp->SetFormat (format);
Greg Clayton427f2902010-12-14 02:59:59 +0000396
Sean Callanan96abc622012-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 Granata12213582012-08-09 16:51:25 +0000410 .SetShowSummary(!m_command_options.print_object)
411 .SetHideRootType(m_command_options.print_object);
Sean Callanan96abc622012-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 Clayton427f2902010-12-14 02:59:59 +0000419 }
420 else
421 {
Sean Callanan24312442011-08-23 21:20:51 +0000422 if (result_valobj_sp->GetError().GetError() == ClangUserExpression::kNoResult)
Greg Claytonfe1b47d2011-06-24 22:31:10 +0000423 {
Sean Callanan1e1e6cb2012-08-09 18:18:47 +0000424 if (format != eFormatVoid && m_interpreter.GetDebugger().GetNotifyVoid())
Sean Callanan96abc622012-08-08 17:35:10 +0000425 {
Sean Callanan1e1e6cb2012-08-09 18:18:47 +0000426 error_stream->PutCString("(void)\n");
Sean Callanan96abc622012-08-08 17:35:10 +0000427 }
Sean Callanan1e1e6cb2012-08-09 18:18:47 +0000428
429 if (result)
430 result->SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonfe1b47d2011-06-24 22:31:10 +0000431 }
432 else
433 {
Sean Callanan24312442011-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 Claytonfe1b47d2011-06-24 22:31:10 +0000452 }
Jim Ingham360f53f2010-11-30 02:22:11 +0000453 }
454 }
Greg Clayton427f2902010-12-14 02:59:59 +0000455 }
456 else
457 {
Caroline Tice87e1f772011-06-13 20:20:29 +0000458 error_stream->Printf ("error: invalid execution context for expression\n");
Greg Clayton427f2902010-12-14 02:59:59 +0000459 return false;
Sean Callanan841026f2010-07-23 00:16:21 +0000460 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000461
Sean Callanan841026f2010-07-23 00:16:21 +0000462 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000463}
464
465bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000466CommandObjectExpression::DoExecute
Chris Lattner24943d22010-06-08 16:52:24 +0000467(
468 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +0000469 CommandReturnObject &result
470)
471{
Greg Claytona42880a2011-10-25 06:44:01 +0000472 m_option_group.NotifyOptionParsingStarting();
Chris Lattner24943d22010-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 Clayton238c0a12010-09-18 01:14:36 +0000481 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
Chris Lattner24943d22010-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 Clayton63094e02010-06-23 01:19:29 +0000487 NULL, // end token
Chris Lattner24943d22010-06-08 16:52:24 +0000488 NULL, // prompt
489 true)); // echo input
490 if (err.Success())
491 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000492 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner24943d22010-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 Clayton63094e02010-06-23 01:19:29 +0000533 Args args (command, end_options - command);
Greg Clayton238c0a12010-09-18 01:14:36 +0000534 if (!ParseOptions (args, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000535 return false;
Greg Clayton143fcc32011-04-13 00:18:08 +0000536
Greg Claytona42880a2011-10-25 06:44:01 +0000537 Error error (m_option_group.NotifyOptionParsingFinished());
Greg Clayton143fcc32011-04-13 00:18:08 +0000538 if (error.Fail())
539 {
540 result.AppendError (error.AsCString());
541 result.SetStatus (eReturnStatusFailed);
542 return false;
543 }
Chris Lattner24943d22010-06-08 16:52:24 +0000544 }
545 }
546
Chris Lattner24943d22010-06-08 16:52:24 +0000547 if (expr == NULL)
548 expr = command;
Sean Callanan46b62492010-06-24 00:16:27 +0000549
Caroline Tice87e1f772011-06-13 20:20:29 +0000550 if (EvaluateExpression (expr, &(result.GetOutputStream()), &(result.GetErrorStream()), &result))
Johnny Chen0deefc72010-08-13 00:42:30 +0000551 return true;
552
553 result.SetStatus (eReturnStatusFailed);
554 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000555}
556