blob: 81633d382835d93f57cf2eb7fdf923a1eb82afc3 [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."},
Jim Ingham47beabb2012-10-16 21:41:58 +000056 { 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 +000057 { 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."},
Greg Claytona42880a2011-10-25 06:44:01 +000058};
59
60
61uint32_t
62CommandObjectExpression::CommandOptions::GetNumDefinitions ()
63{
64 return sizeof(g_option_table)/sizeof(OptionDefinition);
65}
66
Chris Lattner24943d22010-06-08 16:52:24 +000067Error
Greg Claytona42880a2011-10-25 06:44:01 +000068CommandObjectExpression::CommandOptions::SetOptionValue (CommandInterpreter &interpreter,
69 uint32_t option_idx,
70 const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000071{
72 Error error;
73
Greg Clayton6475c422012-12-04 00:32:51 +000074 const int short_option = g_option_table[option_idx].short_option;
Chris Lattner24943d22010-06-08 16:52:24 +000075
76 switch (short_option)
77 {
Caroline Ticec1ad82e2010-09-07 22:38:08 +000078 //case 'l':
79 //if (language.SetLanguageFromCString (option_arg) == false)
80 //{
Greg Clayton9c236732011-10-26 00:56:27 +000081 // error.SetErrorStringWithFormat("invalid language option argument '%s'", option_arg);
Caroline Ticec1ad82e2010-09-07 22:38:08 +000082 //}
83 //break;
Chris Lattner24943d22010-06-08 16:52:24 +000084
Jim Ingham47beabb2012-10-16 21:41:58 +000085 case 'a':
86 {
87 bool success;
88 bool result;
89 result = Args::StringToBoolean(option_arg, true, &success);
90 if (!success)
91 error.SetErrorStringWithFormat("invalid all-threads value setting: \"%s\"", option_arg);
92 else
93 try_all_threads = result;
94 }
Jim Ingham324067b2010-09-30 00:54:27 +000095 break;
Jim Inghamea9d4262010-11-05 19:25:48 +000096
Jim Ingham47beabb2012-10-16 21:41:58 +000097 case 't':
98 {
99 bool success;
100 uint32_t result;
101 result = Args::StringToUInt32(option_arg, 0, 0, &success);
102 if (success)
103 timeout = result;
104 else
105 error.SetErrorStringWithFormat ("invalid timeout setting \"%s\"", option_arg);
106 }
107 break;
108
Jim Inghamea9d4262010-11-05 19:25:48 +0000109 case 'u':
Sean Callanan47dc4572011-09-15 02:13:07 +0000110 {
111 bool success;
112 unwind_on_error = Args::StringToBoolean(option_arg, true, &success);
113 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +0000114 error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg);
Sean Callanan47dc4572011-09-15 02:13:07 +0000115 break;
116 }
Chris Lattner24943d22010-06-08 16:52:24 +0000117 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000118 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000119 break;
120 }
121
122 return error;
123}
124
125void
Greg Claytona42880a2011-10-25 06:44:01 +0000126CommandObjectExpression::CommandOptions::OptionParsingStarting (CommandInterpreter &interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000127{
Jim Inghamea9d4262010-11-05 19:25:48 +0000128 unwind_on_error = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000129 show_summary = true;
Jim Ingham47beabb2012-10-16 21:41:58 +0000130 try_all_threads = true;
131 timeout = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000132}
133
Greg Claytonb3448432011-03-24 21:19:54 +0000134const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000135CommandObjectExpression::CommandOptions::GetDefinitions ()
136{
137 return g_option_table;
138}
139
Greg Clayton238c0a12010-09-18 01:14:36 +0000140CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000141 CommandObjectRaw (interpreter,
142 "expression",
Jim Ingham02ed2852012-10-25 18:11:24 +0000143 "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 +0000144 NULL,
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000145 eFlagProcessMustBePaused | eFlagTryTargetAPILock),
Greg Claytona42880a2011-10-25 06:44:01 +0000146 m_option_group (interpreter),
147 m_format_options (eFormatDefault),
148 m_command_options (),
Johnny Chencc112ac2010-09-30 18:30:25 +0000149 m_expr_line_count (0),
Chris Lattner24943d22010-06-08 16:52:24 +0000150 m_expr_lines ()
151{
152 SetHelpLong(
Jim Ingham47beabb2012-10-16 21:41:58 +0000153"Timeouts:\n\
154 If the expression can be evaluated statically (without runnning code) then it will be.\n\
155 Otherwise, by default the expression will run on the current thread with a short timeout:\n\
156 currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted\n\
157 and resumed with all threads running. You can use the -a option to disable retrying on all\n\
158 threads. You can use the -t option to set a shorter timeout.\n\
Jim Ingham02ed2852012-10-25 18:11:24 +0000159\n\
160User defined variables:\n\
161 You can define your own variables for convenience or to be used in subsequent expressions.\n\
162 You define them the same way you would define variables in C. If the first character of \n\
163 your user defined variable is a $, then the variable's value will be available in future\n\
164 expressions, otherwise it will just be available in the current expression.\n\
165\n\
Jim Ingham47beabb2012-10-16 21:41:58 +0000166Examples: \n\
Chris Lattner24943d22010-06-08 16:52:24 +0000167\n\
168 expr my_struct->a = my_array[3] \n\
169 expr -f bin -- (index * 8) + 5 \n\
Jim Ingham02ed2852012-10-25 18:11:24 +0000170 expr unsigned int $foo = 5\n\
Chris Lattner24943d22010-06-08 16:52:24 +0000171 expr char c[] = \"foo\"; c[0]\n");
Caroline Tice43b014a2010-10-04 22:28:36 +0000172
173 CommandArgumentEntry arg;
174 CommandArgumentData expression_arg;
175
176 // Define the first (and only) variant of this arg.
177 expression_arg.arg_type = eArgTypeExpression;
178 expression_arg.arg_repetition = eArgRepeatPlain;
179
180 // There is only one variant this argument could be; put it into the argument entry.
181 arg.push_back (expression_arg);
182
183 // Push the data for the first argument into the m_arguments vector.
184 m_arguments.push_back (arg);
Greg Claytona42880a2011-10-25 06:44:01 +0000185
Greg Clayton24a6bd92011-10-27 17:55:14 +0000186 // Add the "--format" and "--gdb-format"
187 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 +0000188 m_option_group.Append (&m_command_options);
Enrico Granata3a91e142013-01-09 20:12:53 +0000189 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1 | LLDB_OPT_SET_2);
Greg Claytona42880a2011-10-25 06:44:01 +0000190 m_option_group.Finalize();
Chris Lattner24943d22010-06-08 16:52:24 +0000191}
192
193CommandObjectExpression::~CommandObjectExpression ()
194{
195}
196
197Options *
198CommandObjectExpression::GetOptions ()
199{
Greg Claytona42880a2011-10-25 06:44:01 +0000200 return &m_option_group;
Chris Lattner24943d22010-06-08 16:52:24 +0000201}
202
Chris Lattner24943d22010-06-08 16:52:24 +0000203size_t
204CommandObjectExpression::MultiLineExpressionCallback
205(
206 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +0000207 InputReader &reader,
Chris Lattner24943d22010-06-08 16:52:24 +0000208 lldb::InputReaderAction notification,
209 const char *bytes,
210 size_t bytes_len
211)
212{
Chris Lattner24943d22010-06-08 16:52:24 +0000213 CommandObjectExpression *cmd_object_expr = (CommandObjectExpression *) baton;
Caroline Tice892fadd2011-06-16 16:27:19 +0000214 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
215
Chris Lattner24943d22010-06-08 16:52:24 +0000216 switch (notification)
217 {
218 case eInputReaderActivate:
Caroline Tice892fadd2011-06-16 16:27:19 +0000219 if (!batch_mode)
Caroline Tice2b5e4e62011-06-15 19:35:17 +0000220 {
Greg Clayton234981a2011-07-20 03:41:06 +0000221 StreamSP async_strm_sp(reader.GetDebugger().GetAsyncOutputStream());
222 if (async_strm_sp)
223 {
224 async_strm_sp->PutCString("Enter expressions, then terminate with an empty line to evaluate:\n");
225 async_strm_sp->Flush();
226 }
Caroline Tice2b5e4e62011-06-15 19:35:17 +0000227 }
Chris Lattner24943d22010-06-08 16:52:24 +0000228 // Fall through
229 case eInputReaderReactivate:
Chris Lattner24943d22010-06-08 16:52:24 +0000230 break;
231
232 case eInputReaderDeactivate:
233 break;
234
Caroline Tice4a348082011-05-02 20:41:46 +0000235 case eInputReaderAsynchronousOutputWritten:
236 break;
237
Chris Lattner24943d22010-06-08 16:52:24 +0000238 case eInputReaderGotToken:
239 ++cmd_object_expr->m_expr_line_count;
240 if (bytes && bytes_len)
241 {
242 cmd_object_expr->m_expr_lines.append (bytes, bytes_len + 1);
243 }
244
245 if (bytes_len == 0)
Greg Clayton63094e02010-06-23 01:19:29 +0000246 reader.SetIsDone(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000247 break;
248
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000249 case eInputReaderInterrupt:
250 cmd_object_expr->m_expr_lines.clear();
251 reader.SetIsDone (true);
Caroline Tice892fadd2011-06-16 16:27:19 +0000252 if (!batch_mode)
Caroline Tice2b5e4e62011-06-15 19:35:17 +0000253 {
Greg Clayton234981a2011-07-20 03:41:06 +0000254 StreamSP async_strm_sp (reader.GetDebugger().GetAsyncOutputStream());
255 if (async_strm_sp)
256 {
257 async_strm_sp->PutCString("Expression evaluation cancelled.\n");
258 async_strm_sp->Flush();
259 }
Caroline Tice2b5e4e62011-06-15 19:35:17 +0000260 }
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000261 break;
262
263 case eInputReaderEndOfFile:
264 reader.SetIsDone (true);
265 break;
266
Chris Lattner24943d22010-06-08 16:52:24 +0000267 case eInputReaderDone:
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000268 if (cmd_object_expr->m_expr_lines.size() > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000269 {
Caroline Tice87e1f772011-06-13 20:20:29 +0000270 StreamSP output_stream = reader.GetDebugger().GetAsyncOutputStream();
271 StreamSP error_stream = reader.GetDebugger().GetAsyncErrorStream();
Chris Lattner24943d22010-06-08 16:52:24 +0000272 cmd_object_expr->EvaluateExpression (cmd_object_expr->m_expr_lines.c_str(),
Caroline Tice87e1f772011-06-13 20:20:29 +0000273 output_stream.get(),
274 error_stream.get());
275 output_stream->Flush();
276 error_stream->Flush();
Chris Lattner24943d22010-06-08 16:52:24 +0000277 }
278 break;
279 }
280
281 return bytes_len;
282}
283
284bool
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000285CommandObjectExpression::EvaluateExpression
286(
287 const char *expr,
Caroline Tice87e1f772011-06-13 20:20:29 +0000288 Stream *output_stream,
289 Stream *error_stream,
Greg Clayton66ed2fb2010-10-05 00:00:42 +0000290 CommandReturnObject *result
291)
Chris Lattner24943d22010-06-08 16:52:24 +0000292{
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000293 Target *target = m_exe_ctx.GetTargetPtr();
Sean Callananf35a96c2011-10-27 21:22:25 +0000294
295 if (!target)
296 target = Host::GetDummyTarget(m_interpreter.GetDebugger()).get();
297
Greg Clayton567e7f32011-09-22 04:58:26 +0000298 if (target)
Chris Lattner24943d22010-06-08 16:52:24 +0000299 {
Greg Clayton427f2902010-12-14 02:59:59 +0000300 lldb::ValueObjectSP result_valobj_sp;
Greg Clayton11730f32010-10-06 03:09:11 +0000301
Greg Claytonb3448432011-03-24 21:19:54 +0000302 ExecutionResults exe_results;
Sean Callanan6a925532011-01-13 08:53:35 +0000303
304 bool keep_in_memory = true;
Enrico Granata3a91e142013-01-09 20:12:53 +0000305
Jim Ingham47beabb2012-10-16 21:41:58 +0000306 EvaluateExpressionOptions options;
Enrico Granata3a91e142013-01-09 20:12:53 +0000307 options.SetCoerceToId(m_varobj_options.use_objc)
Enrico Granatad27026e2012-09-05 20:41:26 +0000308 .SetUnwindOnError(m_command_options.unwind_on_error)
309 .SetKeepInMemory(keep_in_memory)
Enrico Granata3a91e142013-01-09 20:12:53 +0000310 .SetUseDynamic(m_varobj_options.use_dynamic)
Jim Ingham47beabb2012-10-16 21:41:58 +0000311 .SetRunOthers(m_command_options.try_all_threads)
312 .SetTimeoutUsec(m_command_options.timeout);
Enrico Granatad27026e2012-09-05 20:41:26 +0000313
Greg Clayton567e7f32011-09-22 04:58:26 +0000314 exe_results = target->EvaluateExpression (expr,
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000315 m_exe_ctx.GetFramePtr(),
Enrico Granata6cca9692012-07-16 23:10:35 +0000316 result_valobj_sp,
Enrico Granatad27026e2012-09-05 20:41:26 +0000317 options);
Greg Clayton427f2902010-12-14 02:59:59 +0000318
Greg Claytona42880a2011-10-25 06:44:01 +0000319 if (exe_results == eExecutionInterrupted && !m_command_options.unwind_on_error)
Jim Ingham360f53f2010-11-30 02:22:11 +0000320 {
Greg Claytonabe0fed2011-04-18 08:33:37 +0000321 uint32_t start_frame = 0;
322 uint32_t num_frames = 1;
323 uint32_t num_frames_with_source = 0;
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000324 Thread *thread = m_exe_ctx.GetThreadPtr();
Greg Clayton567e7f32011-09-22 04:58:26 +0000325 if (thread)
Greg Claytonabe0fed2011-04-18 08:33:37 +0000326 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000327 thread->GetStatus (result->GetOutputStream(),
328 start_frame,
329 num_frames,
330 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +0000331 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000332 else
Greg Claytonabe0fed2011-04-18 08:33:37 +0000333 {
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000334 Process *process = m_exe_ctx.GetProcessPtr();
Greg Clayton567e7f32011-09-22 04:58:26 +0000335 if (process)
336 {
337 bool only_threads_with_stop_reason = true;
338 process->GetThreadStatus (result->GetOutputStream(),
339 only_threads_with_stop_reason,
340 start_frame,
341 num_frames,
342 num_frames_with_source);
343 }
Greg Claytonabe0fed2011-04-18 08:33:37 +0000344 }
Greg Clayton427f2902010-12-14 02:59:59 +0000345 }
Sean Callanan96abc622012-08-08 17:35:10 +0000346
Greg Clayton427f2902010-12-14 02:59:59 +0000347 if (result_valobj_sp)
348 {
Sean Callanan96abc622012-08-08 17:35:10 +0000349 Format format = m_format_options.GetFormat();
350
Greg Clayton427f2902010-12-14 02:59:59 +0000351 if (result_valobj_sp->GetError().Success())
352 {
Sean Callanan96abc622012-08-08 17:35:10 +0000353 if (format != eFormatVoid)
354 {
355 if (format != eFormatDefault)
356 result_valobj_sp->SetFormat (format);
Greg Clayton427f2902010-12-14 02:59:59 +0000357
Sean Callanan96abc622012-08-08 17:35:10 +0000358 ValueObject::DumpValueObjectOptions options;
Enrico Granata3a91e142013-01-09 20:12:53 +0000359 options.SetMaximumPointerDepth(m_varobj_options.ptr_depth)
360 .SetMaximumDepth(m_varobj_options.max_depth)
361 .SetShowTypes(m_varobj_options.show_types)
362 .SetShowLocation(m_varobj_options.show_location)
363 .SetUseObjectiveC(m_varobj_options.use_objc)
364 .SetUseDynamicType(m_varobj_options.use_dynamic)
365 .SetUseSyntheticValue(m_varobj_options.use_synth)
366 .SetFlatOutput(m_varobj_options.flat_output)
367 .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
368 .SetIgnoreCap(m_varobj_options.ignore_cap)
Sean Callanan96abc622012-08-08 17:35:10 +0000369 .SetFormat(format)
370 .SetSummary()
Enrico Granata3a91e142013-01-09 20:12:53 +0000371 .SetShowSummary(!m_varobj_options.use_objc)
372 .SetHideRootType(m_varobj_options.use_objc);
373
Sean Callanan96abc622012-08-08 17:35:10 +0000374 ValueObject::DumpValueObject (*(output_stream),
375 result_valobj_sp.get(), // Variable object to dump
376 options);
377 if (result)
378 result->SetStatus (eReturnStatusSuccessFinishResult);
379 }
Greg Clayton427f2902010-12-14 02:59:59 +0000380 }
381 else
382 {
Sean Callanan24312442011-08-23 21:20:51 +0000383 if (result_valobj_sp->GetError().GetError() == ClangUserExpression::kNoResult)
Greg Claytonfe1b47d2011-06-24 22:31:10 +0000384 {
Sean Callanan1e1e6cb2012-08-09 18:18:47 +0000385 if (format != eFormatVoid && m_interpreter.GetDebugger().GetNotifyVoid())
Sean Callanan96abc622012-08-08 17:35:10 +0000386 {
Sean Callanan1e1e6cb2012-08-09 18:18:47 +0000387 error_stream->PutCString("(void)\n");
Sean Callanan96abc622012-08-08 17:35:10 +0000388 }
Sean Callanan1e1e6cb2012-08-09 18:18:47 +0000389
390 if (result)
391 result->SetStatus (eReturnStatusSuccessFinishResult);
Greg Claytonfe1b47d2011-06-24 22:31:10 +0000392 }
393 else
394 {
Sean Callanan24312442011-08-23 21:20:51 +0000395 const char *error_cstr = result_valobj_sp->GetError().AsCString();
396 if (error_cstr && error_cstr[0])
397 {
398 int error_cstr_len = strlen (error_cstr);
399 const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n';
400 if (strstr(error_cstr, "error:") != error_cstr)
401 error_stream->PutCString ("error: ");
402 error_stream->Write(error_cstr, error_cstr_len);
403 if (!ends_with_newline)
404 error_stream->EOL();
405 }
406 else
407 {
408 error_stream->PutCString ("error: unknown error\n");
409 }
410
411 if (result)
412 result->SetStatus (eReturnStatusFailed);
Greg Claytonfe1b47d2011-06-24 22:31:10 +0000413 }
Jim Ingham360f53f2010-11-30 02:22:11 +0000414 }
415 }
Greg Clayton427f2902010-12-14 02:59:59 +0000416 }
417 else
418 {
Caroline Tice87e1f772011-06-13 20:20:29 +0000419 error_stream->Printf ("error: invalid execution context for expression\n");
Greg Clayton427f2902010-12-14 02:59:59 +0000420 return false;
Sean Callanan841026f2010-07-23 00:16:21 +0000421 }
Sean Callanan82b74c82010-08-12 01:56:52 +0000422
Sean Callanan841026f2010-07-23 00:16:21 +0000423 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000424}
425
426bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000427CommandObjectExpression::DoExecute
Chris Lattner24943d22010-06-08 16:52:24 +0000428(
429 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +0000430 CommandReturnObject &result
431)
432{
Greg Claytona42880a2011-10-25 06:44:01 +0000433 m_option_group.NotifyOptionParsingStarting();
Chris Lattner24943d22010-06-08 16:52:24 +0000434
435 const char * expr = NULL;
436
437 if (command[0] == '\0')
438 {
439 m_expr_lines.clear();
440 m_expr_line_count = 0;
441
Greg Clayton238c0a12010-09-18 01:14:36 +0000442 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger()));
Chris Lattner24943d22010-06-08 16:52:24 +0000443 if (reader_sp)
444 {
445 Error err (reader_sp->Initialize (CommandObjectExpression::MultiLineExpressionCallback,
446 this, // baton
447 eInputReaderGranularityLine, // token size, to pass to callback function
Greg Clayton63094e02010-06-23 01:19:29 +0000448 NULL, // end token
Chris Lattner24943d22010-06-08 16:52:24 +0000449 NULL, // prompt
450 true)); // echo input
451 if (err.Success())
452 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000453 m_interpreter.GetDebugger().PushInputReader (reader_sp);
Chris Lattner24943d22010-06-08 16:52:24 +0000454 result.SetStatus (eReturnStatusSuccessFinishNoResult);
455 }
456 else
457 {
458 result.AppendError (err.AsCString());
459 result.SetStatus (eReturnStatusFailed);
460 }
461 }
462 else
463 {
464 result.AppendError("out of memory");
465 result.SetStatus (eReturnStatusFailed);
466 }
467 return result.Succeeded();
468 }
469
470 if (command[0] == '-')
471 {
472 // We have some options and these options MUST end with --.
473 const char *end_options = NULL;
474 const char *s = command;
475 while (s && s[0])
476 {
477 end_options = ::strstr (s, "--");
478 if (end_options)
479 {
480 end_options += 2; // Get past the "--"
481 if (::isspace (end_options[0]))
482 {
483 expr = end_options;
484 while (::isspace (*expr))
485 ++expr;
486 break;
487 }
488 }
489 s = end_options;
490 }
491
492 if (end_options)
493 {
Greg Clayton63094e02010-06-23 01:19:29 +0000494 Args args (command, end_options - command);
Greg Clayton238c0a12010-09-18 01:14:36 +0000495 if (!ParseOptions (args, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000496 return false;
Greg Clayton143fcc32011-04-13 00:18:08 +0000497
Greg Claytona42880a2011-10-25 06:44:01 +0000498 Error error (m_option_group.NotifyOptionParsingFinished());
Greg Clayton143fcc32011-04-13 00:18:08 +0000499 if (error.Fail())
500 {
501 result.AppendError (error.AsCString());
502 result.SetStatus (eReturnStatusFailed);
503 return false;
504 }
Chris Lattner24943d22010-06-08 16:52:24 +0000505 }
506 }
507
Chris Lattner24943d22010-06-08 16:52:24 +0000508 if (expr == NULL)
509 expr = command;
Sean Callanan46b62492010-06-24 00:16:27 +0000510
Caroline Tice87e1f772011-06-13 20:20:29 +0000511 if (EvaluateExpression (expr, &(result.GetOutputStream()), &(result.GetErrorStream()), &result))
Johnny Chen0deefc72010-08-13 00:42:30 +0000512 return true;
513
514 result.SetStatus (eReturnStatusFailed);
515 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000516}
517