130fdc8d8SChris Lattner //===-- CommandObjectExpression.cpp -----------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel Malea 
1230fdc8d8SChris Lattner #include "CommandObjectExpression.h"
1330fdc8d8SChris Lattner 
1430fdc8d8SChris Lattner // C Includes
1530fdc8d8SChris Lattner // C++ Includes
1630fdc8d8SChris Lattner // Other libraries and framework includes
1730fdc8d8SChris Lattner // Project includes
18ebf7707eSSean Callanan #include "lldb/Interpreter/Args.h"
1930fdc8d8SChris Lattner #include "lldb/Core/Value.h"
206c68fb45SJim Ingham #include "lldb/Core/ValueObjectVariable.h"
214d93b8cdSEnrico Granata #include "lldb/DataFormatters/ValueObjectPrinter.h"
2230fdc8d8SChris Lattner #include "lldb/Expression/ClangExpressionVariable.h"
231a8d4093SSean Callanan #include "lldb/Expression/ClangUserExpression.h"
24ebb84b24SStephen Wilson #include "lldb/Expression/ClangFunction.h"
2530fdc8d8SChris Lattner #include "lldb/Expression/DWARFExpression.h"
2630fdc8d8SChris Lattner #include "lldb/Host/Host.h"
27ebf7707eSSean Callanan #include "lldb/Core/Debugger.h"
286611103cSGreg Clayton #include "lldb/Interpreter/CommandInterpreter.h"
2930fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h"
306c68fb45SJim Ingham #include "lldb/Target/ObjCLanguageRuntime.h"
3130fdc8d8SChris Lattner #include "lldb/Symbol/ObjectFile.h"
3230fdc8d8SChris Lattner #include "lldb/Symbol/Variable.h"
3330fdc8d8SChris Lattner #include "lldb/Target/Process.h"
34b57e4a1bSJason Molenda #include "lldb/Target/StackFrame.h"
3530fdc8d8SChris Lattner #include "lldb/Target/Target.h"
367260f620SGreg Clayton #include "lldb/Target/Thread.h"
37ebf7707eSSean Callanan #include "llvm/ADT/StringRef.h"
3830fdc8d8SChris Lattner 
3930fdc8d8SChris Lattner using namespace lldb;
4030fdc8d8SChris Lattner using namespace lldb_private;
4130fdc8d8SChris Lattner 
421deb7962SGreg Clayton CommandObjectExpression::CommandOptions::CommandOptions () :
431deb7962SGreg Clayton     OptionGroup()
4430fdc8d8SChris Lattner {
4530fdc8d8SChris Lattner }
4630fdc8d8SChris Lattner 
4730fdc8d8SChris Lattner 
4830fdc8d8SChris Lattner CommandObjectExpression::CommandOptions::~CommandOptions ()
4930fdc8d8SChris Lattner {
5030fdc8d8SChris Lattner }
5130fdc8d8SChris Lattner 
524d93b8cdSEnrico Granata static OptionEnumValueElement g_description_verbosity_type[] =
534d93b8cdSEnrico Granata {
544d93b8cdSEnrico Granata     { eLanguageRuntimeDescriptionDisplayVerbosityCompact,      "compact",       "Only show the description string"},
554d93b8cdSEnrico Granata     { eLanguageRuntimeDescriptionDisplayVerbosityFull,         "full",          "Show the full output, including persistent variable's name and type"},
564d93b8cdSEnrico Granata     { 0, NULL, NULL }
574d93b8cdSEnrico Granata };
584d93b8cdSEnrico Granata 
591deb7962SGreg Clayton OptionDefinition
601deb7962SGreg Clayton CommandObjectExpression::CommandOptions::g_option_table[] =
611deb7962SGreg Clayton {
62e2607b50SVirgile Bello     { 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."},
63e2607b50SVirgile Bello     { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "ignore-breakpoints", 'i', OptionParser::eRequiredArgument, NULL, 0, eArgTypeBoolean,    "Ignore breakpoint hits while running expressions"},
64e2607b50SVirgile Bello     { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "timeout",            't', OptionParser::eRequiredArgument, NULL, 0, eArgTypeUnsignedInteger,  "Timeout value (in microseconds) for running the expression."},
65e2607b50SVirgile Bello     { 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)."},
6662afb9f6SGreg Clayton     { 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)."},
674d93b8cdSEnrico Granata     { 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."},
681deb7962SGreg Clayton };
691deb7962SGreg Clayton 
701deb7962SGreg Clayton 
711deb7962SGreg Clayton uint32_t
721deb7962SGreg Clayton CommandObjectExpression::CommandOptions::GetNumDefinitions ()
731deb7962SGreg Clayton {
741deb7962SGreg Clayton     return sizeof(g_option_table)/sizeof(OptionDefinition);
751deb7962SGreg Clayton }
761deb7962SGreg Clayton 
7730fdc8d8SChris Lattner Error
781deb7962SGreg Clayton CommandObjectExpression::CommandOptions::SetOptionValue (CommandInterpreter &interpreter,
791deb7962SGreg Clayton                                                          uint32_t option_idx,
801deb7962SGreg Clayton                                                          const char *option_arg)
8130fdc8d8SChris Lattner {
8230fdc8d8SChris Lattner     Error error;
8330fdc8d8SChris Lattner 
843bcdfc0eSGreg Clayton     const int short_option = g_option_table[option_idx].short_option;
8530fdc8d8SChris Lattner 
8630fdc8d8SChris Lattner     switch (short_option)
8730fdc8d8SChris Lattner     {
883f4c09c1SCaroline Tice       //case 'l':
893f4c09c1SCaroline Tice       //if (language.SetLanguageFromCString (option_arg) == false)
903f4c09c1SCaroline Tice       //{
9186edbf41SGreg Clayton       //    error.SetErrorStringWithFormat("invalid language option argument '%s'", option_arg);
923f4c09c1SCaroline Tice       //}
933f4c09c1SCaroline Tice       //break;
9430fdc8d8SChris Lattner 
9535e1bda6SJim Ingham     case 'a':
9635e1bda6SJim Ingham         {
9735e1bda6SJim Ingham             bool success;
9835e1bda6SJim Ingham             bool result;
9935e1bda6SJim Ingham             result = Args::StringToBoolean(option_arg, true, &success);
10035e1bda6SJim Ingham             if (!success)
10135e1bda6SJim Ingham                 error.SetErrorStringWithFormat("invalid all-threads value setting: \"%s\"", option_arg);
10235e1bda6SJim Ingham             else
10335e1bda6SJim Ingham                 try_all_threads = result;
10435e1bda6SJim Ingham         }
1056c68fb45SJim Ingham         break;
1066c68fb45SJim Ingham 
107184e9811SJim Ingham     case 'i':
108184e9811SJim Ingham         {
109184e9811SJim Ingham             bool success;
110184e9811SJim Ingham             bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
111184e9811SJim Ingham             if (success)
112184e9811SJim Ingham                 ignore_breakpoints = tmp_value;
113184e9811SJim Ingham             else
114184e9811SJim Ingham                 error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg);
115184e9811SJim Ingham             break;
116184e9811SJim Ingham         }
11735e1bda6SJim Ingham     case 't':
11835e1bda6SJim Ingham         {
11935e1bda6SJim Ingham             bool success;
12035e1bda6SJim Ingham             uint32_t result;
12135e1bda6SJim Ingham             result = Args::StringToUInt32(option_arg, 0, 0, &success);
12235e1bda6SJim Ingham             if (success)
12335e1bda6SJim Ingham                 timeout = result;
12435e1bda6SJim Ingham             else
12535e1bda6SJim Ingham                 error.SetErrorStringWithFormat ("invalid timeout setting \"%s\"", option_arg);
12635e1bda6SJim Ingham         }
12735e1bda6SJim Ingham         break;
12835e1bda6SJim Ingham 
129399f1cafSJim Ingham     case 'u':
1303bfdaa2aSSean Callanan         {
131399f1cafSJim Ingham             bool success;
132184e9811SJim Ingham             bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
133184e9811SJim Ingham             if (success)
134184e9811SJim Ingham                 unwind_on_error = tmp_value;
135184e9811SJim Ingham             else
13686edbf41SGreg Clayton                 error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg);
137399f1cafSJim Ingham             break;
1383bfdaa2aSSean Callanan         }
1394d93b8cdSEnrico Granata 
1404d93b8cdSEnrico Granata     case 'v':
1414d93b8cdSEnrico Granata         if (!option_arg)
1424d93b8cdSEnrico Granata         {
1434d93b8cdSEnrico Granata             m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull;
1444d93b8cdSEnrico Granata             break;
1454d93b8cdSEnrico Granata         }
1464d93b8cdSEnrico Granata         m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error);
1474d93b8cdSEnrico Granata         if (!error.Success())
1484d93b8cdSEnrico Granata             error.SetErrorStringWithFormat ("unrecognized value for description-verbosity '%s'", option_arg);
1494d93b8cdSEnrico Granata         break;
1504d93b8cdSEnrico Granata 
15162afb9f6SGreg Clayton     case 'g':
15262afb9f6SGreg Clayton         debug = true;
15362afb9f6SGreg Clayton         unwind_on_error = false;
15462afb9f6SGreg Clayton         ignore_breakpoints = false;
15562afb9f6SGreg Clayton         break;
15662afb9f6SGreg Clayton 
15730fdc8d8SChris Lattner     default:
15886edbf41SGreg Clayton         error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
15930fdc8d8SChris Lattner         break;
16030fdc8d8SChris Lattner     }
16130fdc8d8SChris Lattner 
16230fdc8d8SChris Lattner     return error;
16330fdc8d8SChris Lattner }
16430fdc8d8SChris Lattner 
16530fdc8d8SChris Lattner void
1661deb7962SGreg Clayton CommandObjectExpression::CommandOptions::OptionParsingStarting (CommandInterpreter &interpreter)
16730fdc8d8SChris Lattner {
168184e9811SJim Ingham     Process *process = interpreter.GetExecutionContext().GetProcessPtr();
169184e9811SJim Ingham     if (process != NULL)
170184e9811SJim Ingham     {
171184e9811SJim Ingham         ignore_breakpoints = process->GetIgnoreBreakpointsInExpressions();
172184e9811SJim Ingham         unwind_on_error    = process->GetUnwindOnErrorInExpressions();
173184e9811SJim Ingham     }
174184e9811SJim Ingham     else
175184e9811SJim Ingham     {
176*fc03f8fcSGreg Clayton         ignore_breakpoints = true;
177399f1cafSJim Ingham         unwind_on_error = true;
178184e9811SJim Ingham     }
179184e9811SJim Ingham 
18030fdc8d8SChris Lattner     show_summary = true;
18135e1bda6SJim Ingham     try_all_threads = true;
18235e1bda6SJim Ingham     timeout = 0;
18362afb9f6SGreg Clayton     debug = false;
1844d93b8cdSEnrico Granata     m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact;
18530fdc8d8SChris Lattner }
18630fdc8d8SChris Lattner 
187e0d378b3SGreg Clayton const OptionDefinition*
18830fdc8d8SChris Lattner CommandObjectExpression::CommandOptions::GetDefinitions ()
18930fdc8d8SChris Lattner {
19030fdc8d8SChris Lattner     return g_option_table;
19130fdc8d8SChris Lattner }
19230fdc8d8SChris Lattner 
193a7015092SGreg Clayton CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
1945a988416SJim Ingham     CommandObjectRaw (interpreter,
19530fdc8d8SChris Lattner                       "expression",
1965c48d5c5SJim Ingham                       "Evaluate a C/ObjC/C++ expression in the current program context, using user defined variables and variables currently in scope.",
1975a988416SJim Ingham                       NULL,
198f9fc609fSGreg Clayton                       eFlagProcessMustBePaused | eFlagTryTargetAPILock),
19944d93782SGreg Clayton     IOHandlerDelegate (IOHandlerDelegate::Completion::Expression),
2001deb7962SGreg Clayton     m_option_group (interpreter),
2011deb7962SGreg Clayton     m_format_options (eFormatDefault),
2021deb7962SGreg Clayton     m_command_options (),
20330fdc8d8SChris Lattner     m_expr_line_count (0),
20430fdc8d8SChris Lattner     m_expr_lines ()
20530fdc8d8SChris Lattner {
20630fdc8d8SChris Lattner   SetHelpLong(
20735e1bda6SJim Ingham "Timeouts:\n\
20835e1bda6SJim Ingham     If the expression can be evaluated statically (without runnning code) then it will be.\n\
20935e1bda6SJim Ingham     Otherwise, by default the expression will run on the current thread with a short timeout:\n\
21035e1bda6SJim Ingham     currently .25 seconds.  If it doesn't return in that time, the evaluation will be interrupted\n\
21135e1bda6SJim Ingham     and resumed with all threads running.  You can use the -a option to disable retrying on all\n\
21235e1bda6SJim Ingham     threads.  You can use the -t option to set a shorter timeout.\n\
2135c48d5c5SJim Ingham \n\
2145c48d5c5SJim Ingham User defined variables:\n\
2155c48d5c5SJim Ingham     You can define your own variables for convenience or to be used in subsequent expressions.\n\
2165c48d5c5SJim Ingham     You define them the same way you would define variables in C.  If the first character of \n\
2175c48d5c5SJim Ingham     your user defined variable is a $, then the variable's value will be available in future\n\
2185c48d5c5SJim Ingham     expressions, otherwise it will just be available in the current expression.\n\
2195c48d5c5SJim Ingham \n\
22035e1bda6SJim Ingham Examples: \n\
22130fdc8d8SChris Lattner \n\
22230fdc8d8SChris Lattner    expr my_struct->a = my_array[3] \n\
22330fdc8d8SChris Lattner    expr -f bin -- (index * 8) + 5 \n\
2245c48d5c5SJim Ingham    expr unsigned int $foo = 5\n\
22530fdc8d8SChris Lattner    expr char c[] = \"foo\"; c[0]\n");
226405fe67fSCaroline Tice 
227405fe67fSCaroline Tice     CommandArgumentEntry arg;
228405fe67fSCaroline Tice     CommandArgumentData expression_arg;
229405fe67fSCaroline Tice 
230405fe67fSCaroline Tice     // Define the first (and only) variant of this arg.
231405fe67fSCaroline Tice     expression_arg.arg_type = eArgTypeExpression;
232405fe67fSCaroline Tice     expression_arg.arg_repetition = eArgRepeatPlain;
233405fe67fSCaroline Tice 
234405fe67fSCaroline Tice     // There is only one variant this argument could be; put it into the argument entry.
235405fe67fSCaroline Tice     arg.push_back (expression_arg);
236405fe67fSCaroline Tice 
237405fe67fSCaroline Tice     // Push the data for the first argument into the m_arguments vector.
238405fe67fSCaroline Tice     m_arguments.push_back (arg);
2391deb7962SGreg Clayton 
2405009f9d5SGreg Clayton     // Add the "--format" and "--gdb-format"
2415009f9d5SGreg Clayton     m_option_group.Append (&m_format_options, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1);
2421deb7962SGreg Clayton     m_option_group.Append (&m_command_options);
243b576bba2SEnrico Granata     m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1 | LLDB_OPT_SET_2);
2441deb7962SGreg Clayton     m_option_group.Finalize();
24530fdc8d8SChris Lattner }
24630fdc8d8SChris Lattner 
24730fdc8d8SChris Lattner CommandObjectExpression::~CommandObjectExpression ()
24830fdc8d8SChris Lattner {
24930fdc8d8SChris Lattner }
25030fdc8d8SChris Lattner 
25130fdc8d8SChris Lattner Options *
25230fdc8d8SChris Lattner CommandObjectExpression::GetOptions ()
25330fdc8d8SChris Lattner {
2541deb7962SGreg Clayton     return &m_option_group;
25530fdc8d8SChris Lattner }
25630fdc8d8SChris Lattner 
25730fdc8d8SChris Lattner bool
2581d3afba3SGreg Clayton CommandObjectExpression::EvaluateExpression
2591d3afba3SGreg Clayton (
2601d3afba3SGreg Clayton     const char *expr,
2616e8dc334SCaroline Tice     Stream *output_stream,
2626e8dc334SCaroline Tice     Stream *error_stream,
2631d3afba3SGreg Clayton     CommandReturnObject *result
2641d3afba3SGreg Clayton )
26530fdc8d8SChris Lattner {
266ba7b8e2cSGreg Clayton     // Don't use m_exe_ctx as this might be called asynchronously
267ba7b8e2cSGreg Clayton     // after the command object DoExecute has finished when doing
268ba7b8e2cSGreg Clayton     // multi-line expression that use an input reader...
269ba7b8e2cSGreg Clayton     ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
270ba7b8e2cSGreg Clayton 
271ba7b8e2cSGreg Clayton     Target *target = exe_ctx.GetTargetPtr();
272c0a6e061SSean Callanan 
273c0a6e061SSean Callanan     if (!target)
274c0a6e061SSean Callanan         target = Host::GetDummyTarget(m_interpreter.GetDebugger()).get();
275c0a6e061SSean Callanan 
276c14ee32dSGreg Clayton     if (target)
2776961e878SSean Callanan     {
2788b2fe6dcSGreg Clayton         lldb::ValueObjectSP result_valobj_sp;
2798b2fe6dcSGreg Clayton 
28092adcac9SSean Callanan         bool keep_in_memory = true;
28192adcac9SSean Callanan 
28235e1bda6SJim Ingham         EvaluateExpressionOptions options;
2836fbc48bcSJim Ingham         options.SetCoerceToId(m_varobj_options.use_objc);
2846fbc48bcSJim Ingham         options.SetUnwindOnError(m_command_options.unwind_on_error);
2856fbc48bcSJim Ingham         options.SetIgnoreBreakpoints (m_command_options.ignore_breakpoints);
2866fbc48bcSJim Ingham         options.SetKeepInMemory(keep_in_memory);
2876fbc48bcSJim Ingham         options.SetUseDynamic(m_varobj_options.use_dynamic);
2886fbc48bcSJim Ingham         options.SetTryAllThreads(m_command_options.try_all_threads);
2896fbc48bcSJim Ingham         options.SetDebug(m_command_options.debug);
29062afb9f6SGreg Clayton 
29123f8c95aSGreg Clayton         // If there is any chance we are going to stop and want to see
29223f8c95aSGreg Clayton         // what went wrong with our expression, we should generate debug info
29323f8c95aSGreg Clayton         if (!m_command_options.ignore_breakpoints ||
29423f8c95aSGreg Clayton             !m_command_options.unwind_on_error)
29523f8c95aSGreg Clayton             options.SetGenerateDebugInfo(true);
29623f8c95aSGreg Clayton 
29762afb9f6SGreg Clayton         if (m_command_options.timeout > 0)
29862afb9f6SGreg Clayton             options.SetTimeoutUsec(m_command_options.timeout);
2996f78f386SJim Ingham         else
3006f78f386SJim Ingham             options.SetTimeoutUsec(0);
301d4439aa9SEnrico Granata 
30262e5f4deSArnaud A. de Grandmaison         target->EvaluateExpression(expr, exe_ctx.GetFramePtr(),
30362e5f4deSArnaud A. de Grandmaison                                    result_valobj_sp, options);
3048b2fe6dcSGreg Clayton 
3058b2fe6dcSGreg Clayton         if (result_valobj_sp)
3068b2fe6dcSGreg Clayton         {
307bf154daeSSean Callanan             Format format = m_format_options.GetFormat();
308bf154daeSSean Callanan 
309b71f3844SGreg Clayton             if (result_valobj_sp->GetError().Success())
31030fdc8d8SChris Lattner             {
311bf154daeSSean Callanan                 if (format != eFormatVoid)
312bf154daeSSean Callanan                 {
3131deb7962SGreg Clayton                     if (format != eFormatDefault)
3141deb7962SGreg Clayton                         result_valobj_sp->SetFormat (format);
31532c4085bSGreg Clayton 
3164d93b8cdSEnrico Granata                     DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(m_command_options.m_verbosity,format));
317770eb05aSEnrico Granata 
3184d93b8cdSEnrico Granata                     result_valobj_sp->Dump(*output_stream,options);
3194d93b8cdSEnrico Granata 
320fcd43b71SJohnny Chen                     if (result)
321fcd43b71SJohnny Chen                         result->SetStatus (eReturnStatusSuccessFinishResult);
32230fdc8d8SChris Lattner                 }
323bf154daeSSean Callanan             }
32430fdc8d8SChris Lattner             else
32530fdc8d8SChris Lattner             {
326bccce813SSean Callanan                 if (result_valobj_sp->GetError().GetError() == ClangUserExpression::kNoResult)
327bccce813SSean Callanan                 {
328bcf897faSSean Callanan                     if (format != eFormatVoid && m_interpreter.GetDebugger().GetNotifyVoid())
329bf154daeSSean Callanan                     {
330bcf897faSSean Callanan                         error_stream->PutCString("(void)\n");
331bcf897faSSean Callanan                     }
332bccce813SSean Callanan 
333bccce813SSean Callanan                     if (result)
334bccce813SSean Callanan                         result->SetStatus (eReturnStatusSuccessFinishResult);
335bccce813SSean Callanan                 }
336bccce813SSean Callanan                 else
337bccce813SSean Callanan                 {
3385fd05903SGreg Clayton                     const char *error_cstr = result_valobj_sp->GetError().AsCString();
3395fd05903SGreg Clayton                     if (error_cstr && error_cstr[0])
3405fd05903SGreg Clayton                     {
341c7bece56SGreg Clayton                         const size_t error_cstr_len = strlen (error_cstr);
3425fd05903SGreg Clayton                         const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n';
3435fd05903SGreg Clayton                         if (strstr(error_cstr, "error:") != error_cstr)
3445fd05903SGreg Clayton                             error_stream->PutCString ("error: ");
3455fd05903SGreg Clayton                         error_stream->Write(error_cstr, error_cstr_len);
3465fd05903SGreg Clayton                         if (!ends_with_newline)
3475fd05903SGreg Clayton                             error_stream->EOL();
3485fd05903SGreg Clayton                     }
3495fd05903SGreg Clayton                     else
3505fd05903SGreg Clayton                     {
3515fd05903SGreg Clayton                         error_stream->PutCString ("error: unknown error\n");
3525fd05903SGreg Clayton                     }
3535fd05903SGreg Clayton 
354fcd43b71SJohnny Chen                     if (result)
355b71f3844SGreg Clayton                         result->SetStatus (eReturnStatusFailed);
35630fdc8d8SChris Lattner                 }
3578b2fe6dcSGreg Clayton             }
3588b2fe6dcSGreg Clayton         }
359bccce813SSean Callanan     }
3608b2fe6dcSGreg Clayton     else
3618b2fe6dcSGreg Clayton     {
3626e8dc334SCaroline Tice         error_stream->Printf ("error: invalid execution context for expression\n");
3638b2fe6dcSGreg Clayton         return false;
3648b2fe6dcSGreg Clayton     }
36530fdc8d8SChris Lattner 
36616ad5faeSSean Callanan     return true;
36730fdc8d8SChris Lattner }
36830fdc8d8SChris Lattner 
36944d93782SGreg Clayton void
37044d93782SGreg Clayton CommandObjectExpression::IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
37144d93782SGreg Clayton {
37244d93782SGreg Clayton     io_handler.SetIsDone(true);
37344d93782SGreg Clayton //    StreamSP output_stream = io_handler.GetDebugger().GetAsyncOutputStream();
37444d93782SGreg Clayton //    StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream();
37544d93782SGreg Clayton     StreamFileSP output_sp(io_handler.GetOutputStreamFile());
37644d93782SGreg Clayton     StreamFileSP error_sp(io_handler.GetErrorStreamFile());
37744d93782SGreg Clayton 
37844d93782SGreg Clayton     EvaluateExpression (line.c_str(),
37944d93782SGreg Clayton                         output_sp.get(),
38044d93782SGreg Clayton                         error_sp.get());
38144d93782SGreg Clayton     if (output_sp)
38244d93782SGreg Clayton         output_sp->Flush();
38344d93782SGreg Clayton     if (error_sp)
38444d93782SGreg Clayton         error_sp->Flush();
38544d93782SGreg Clayton }
38644d93782SGreg Clayton 
38744d93782SGreg Clayton LineStatus
38844d93782SGreg Clayton CommandObjectExpression::IOHandlerLinesUpdated (IOHandler &io_handler,
38944d93782SGreg Clayton                                                 StringList &lines,
39044d93782SGreg Clayton                                                 uint32_t line_idx,
39144d93782SGreg Clayton                                                 Error &error)
39244d93782SGreg Clayton {
39344d93782SGreg Clayton     if (line_idx == UINT32_MAX)
39444d93782SGreg Clayton     {
39544d93782SGreg Clayton         // Remove the last line from "lines" so it doesn't appear
39644d93782SGreg Clayton         // in our final expression
39744d93782SGreg Clayton         lines.PopBack();
39844d93782SGreg Clayton         error.Clear();
39944d93782SGreg Clayton         return LineStatus::Done;
40044d93782SGreg Clayton     }
40144d93782SGreg Clayton     else if (line_idx + 1 == lines.GetSize())
40244d93782SGreg Clayton     {
40344d93782SGreg Clayton         // The last line was edited, if this line is empty, then we are done
40444d93782SGreg Clayton         // getting our multiple lines.
40544d93782SGreg Clayton         if (lines[line_idx].empty())
40644d93782SGreg Clayton             return LineStatus::Done;
40744d93782SGreg Clayton     }
40844d93782SGreg Clayton     return LineStatus::Success;
40944d93782SGreg Clayton }
41044d93782SGreg Clayton 
411cf28a8b7SGreg Clayton void
412cf28a8b7SGreg Clayton CommandObjectExpression::GetMultilineExpression ()
41330fdc8d8SChris Lattner {
41430fdc8d8SChris Lattner     m_expr_lines.clear();
41530fdc8d8SChris Lattner     m_expr_line_count = 0;
41630fdc8d8SChris Lattner 
41744d93782SGreg Clayton     Debugger &debugger = GetCommandInterpreter().GetDebugger();
41844d93782SGreg Clayton     const bool multiple_lines = true; // Get multiple lines
41944d93782SGreg Clayton     IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
42044d93782SGreg Clayton                                                       "lldb-expr",      // Name of input reader for history
42144d93782SGreg Clayton                                                       NULL,             // No prompt
42244d93782SGreg Clayton                                                       multiple_lines,
423f6913cd7SGreg Clayton                                                       1,                // Show line numbers starting at 1
42444d93782SGreg Clayton                                                       *this));
425b6892508SGreg Clayton 
426b6892508SGreg Clayton     StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile());
427b6892508SGreg Clayton     if (output_sp)
428b6892508SGreg Clayton     {
429b6892508SGreg Clayton         output_sp->PutCString("Enter expressions, then terminate with an empty line to evaluate:\n");
430b6892508SGreg Clayton         output_sp->Flush();
431b6892508SGreg Clayton     }
43244d93782SGreg Clayton     debugger.PushIOHandler(io_handler_sp);
433cf28a8b7SGreg Clayton }
434cf28a8b7SGreg Clayton 
435cf28a8b7SGreg Clayton bool
436cf28a8b7SGreg Clayton CommandObjectExpression::DoExecute
437cf28a8b7SGreg Clayton (
438cf28a8b7SGreg Clayton     const char *command,
439cf28a8b7SGreg Clayton     CommandReturnObject &result
440cf28a8b7SGreg Clayton )
441cf28a8b7SGreg Clayton {
442cf28a8b7SGreg Clayton     m_option_group.NotifyOptionParsingStarting();
443cf28a8b7SGreg Clayton 
444cf28a8b7SGreg Clayton     const char * expr = NULL;
445cf28a8b7SGreg Clayton 
446cf28a8b7SGreg Clayton     if (command[0] == '\0')
447cf28a8b7SGreg Clayton     {
448cf28a8b7SGreg Clayton         GetMultilineExpression ();
44930fdc8d8SChris Lattner         return result.Succeeded();
45030fdc8d8SChris Lattner     }
45130fdc8d8SChris Lattner 
45230fdc8d8SChris Lattner     if (command[0] == '-')
45330fdc8d8SChris Lattner     {
45430fdc8d8SChris Lattner         // We have some options and these options MUST end with --.
45530fdc8d8SChris Lattner         const char *end_options = NULL;
45630fdc8d8SChris Lattner         const char *s = command;
45730fdc8d8SChris Lattner         while (s && s[0])
45830fdc8d8SChris Lattner         {
45930fdc8d8SChris Lattner             end_options = ::strstr (s, "--");
46030fdc8d8SChris Lattner             if (end_options)
46130fdc8d8SChris Lattner             {
46230fdc8d8SChris Lattner                 end_options += 2; // Get past the "--"
46330fdc8d8SChris Lattner                 if (::isspace (end_options[0]))
46430fdc8d8SChris Lattner                 {
46530fdc8d8SChris Lattner                     expr = end_options;
46630fdc8d8SChris Lattner                     while (::isspace (*expr))
46730fdc8d8SChris Lattner                         ++expr;
46830fdc8d8SChris Lattner                     break;
46930fdc8d8SChris Lattner                 }
47030fdc8d8SChris Lattner             }
47130fdc8d8SChris Lattner             s = end_options;
47230fdc8d8SChris Lattner         }
47330fdc8d8SChris Lattner 
47430fdc8d8SChris Lattner         if (end_options)
47530fdc8d8SChris Lattner         {
47630fdc8d8SChris Lattner             Args args (command, end_options - command);
477a7015092SGreg Clayton             if (!ParseOptions (args, result))
47830fdc8d8SChris Lattner                 return false;
479f6b8b581SGreg Clayton 
4801deb7962SGreg Clayton             Error error (m_option_group.NotifyOptionParsingFinished());
481f6b8b581SGreg Clayton             if (error.Fail())
482f6b8b581SGreg Clayton             {
483f6b8b581SGreg Clayton                 result.AppendError (error.AsCString());
484f6b8b581SGreg Clayton                 result.SetStatus (eReturnStatusFailed);
485f6b8b581SGreg Clayton                 return false;
486f6b8b581SGreg Clayton             }
487cf28a8b7SGreg Clayton 
488cf28a8b7SGreg Clayton             // No expression following options
48905da458cSGreg Clayton             if (expr == NULL || expr[0] == '\0')
490cf28a8b7SGreg Clayton             {
491cf28a8b7SGreg Clayton                 GetMultilineExpression ();
492cf28a8b7SGreg Clayton                 return result.Succeeded();
493cf28a8b7SGreg Clayton             }
49430fdc8d8SChris Lattner         }
49530fdc8d8SChris Lattner     }
49630fdc8d8SChris Lattner 
49730fdc8d8SChris Lattner     if (expr == NULL)
49830fdc8d8SChris Lattner         expr = command;
49930fdc8d8SChris Lattner 
5006e8dc334SCaroline Tice     if (EvaluateExpression (expr, &(result.GetOutputStream()), &(result.GetErrorStream()), &result))
501fcd43b71SJohnny Chen         return true;
502fcd43b71SJohnny Chen 
503fcd43b71SJohnny Chen     result.SetStatus (eReturnStatusFailed);
504fcd43b71SJohnny Chen     return false;
50530fdc8d8SChris Lattner }
50630fdc8d8SChris Lattner 
507