1 //===-- CommandObjectExpression.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "CommandObjectExpression.h"
16 // Other libraries and framework includes
18 #include "lldb/Interpreter/Args.h"
19 #include "lldb/Core/Value.h"
20 #include "lldb/Core/ValueObjectVariable.h"
21 #include "lldb/DataFormatters/ValueObjectPrinter.h"
22 #include "lldb/Expression/ClangExpressionVariable.h"
23 #include "lldb/Expression/ClangUserExpression.h"
24 #include "lldb/Expression/ClangFunction.h"
25 #include "lldb/Expression/DWARFExpression.h"
26 #include "lldb/Host/Host.h"
27 #include "lldb/Core/Debugger.h"
28 #include "lldb/Interpreter/CommandInterpreter.h"
29 #include "lldb/Interpreter/CommandReturnObject.h"
30 #include "lldb/Target/ObjCLanguageRuntime.h"
31 #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"
36 #include "lldb/Target/Thread.h"
37 #include "llvm/ADT/STLExtras.h"
38 #include "llvm/ADT/StringRef.h"
41 using namespace lldb_private;
43 CommandObjectExpression::CommandOptions::CommandOptions () :
49 CommandObjectExpression::CommandOptions::~CommandOptions ()
53 static OptionEnumValueElement g_description_verbosity_type[] =
55 { eLanguageRuntimeDescriptionDisplayVerbosityCompact, "compact", "Only show the description string"},
56 { eLanguageRuntimeDescriptionDisplayVerbosityFull, "full", "Show the full output, including persistent variable's name and type"},
61 CommandObjectExpression::CommandOptions::g_option_table[] =
63 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "all-threads", 'a', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Should we run all threads if the execution doesn't complete on one thread."},
64 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "ignore-breakpoints", 'i', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeBoolean, "Ignore breakpoint hits while running expressions"},
65 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "timeout", 't', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeUnsignedInteger, "Timeout value (in microseconds) for running the expression."},
66 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "unwind-on-error", 'u', OptionParser::eRequiredArgument, NULL, 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)."},
67 { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "debug", 'g', OptionParser::eNoArgument , NULL, 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)."},
68 { LLDB_OPT_SET_1, false, "description-verbosity", 'v', OptionParser::eOptionalArgument, NULL, g_description_verbosity_type, 0, eArgTypeDescriptionVerbosity, "How verbose should the output of this expression be, if the object description is asked for."},
73 CommandObjectExpression::CommandOptions::GetNumDefinitions ()
75 return llvm::array_lengthof(g_option_table);
79 CommandObjectExpression::CommandOptions::SetOptionValue (CommandInterpreter &interpreter,
81 const char *option_arg)
85 const int short_option = g_option_table[option_idx].short_option;
90 //if (language.SetLanguageFromCString (option_arg) == false)
92 // error.SetErrorStringWithFormat("invalid language option argument '%s'", option_arg);
100 result = Args::StringToBoolean(option_arg, true, &success);
102 error.SetErrorStringWithFormat("invalid all-threads value setting: \"%s\"", option_arg);
104 try_all_threads = result;
111 bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
113 ignore_breakpoints = tmp_value;
115 error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg);
122 result = Args::StringToUInt32(option_arg, 0, 0, &success);
126 error.SetErrorStringWithFormat ("invalid timeout setting \"%s\"", option_arg);
133 bool tmp_value = Args::StringToBoolean(option_arg, true, &success);
135 unwind_on_error = tmp_value;
137 error.SetErrorStringWithFormat("could not convert \"%s\" to a boolean value.", option_arg);
144 m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityFull;
147 m_verbosity = (LanguageRuntimeDescriptionDisplayVerbosity) Args::StringToOptionEnum(option_arg, g_option_table[option_idx].enum_values, 0, error);
148 if (!error.Success())
149 error.SetErrorStringWithFormat ("unrecognized value for description-verbosity '%s'", option_arg);
154 unwind_on_error = false;
155 ignore_breakpoints = false;
159 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option);
167 CommandObjectExpression::CommandOptions::OptionParsingStarting (CommandInterpreter &interpreter)
169 Process *process = interpreter.GetExecutionContext().GetProcessPtr();
172 ignore_breakpoints = process->GetIgnoreBreakpointsInExpressions();
173 unwind_on_error = process->GetUnwindOnErrorInExpressions();
177 ignore_breakpoints = true;
178 unwind_on_error = true;
182 try_all_threads = true;
185 m_verbosity = eLanguageRuntimeDescriptionDisplayVerbosityCompact;
188 const OptionDefinition*
189 CommandObjectExpression::CommandOptions::GetDefinitions ()
191 return g_option_table;
194 CommandObjectExpression::CommandObjectExpression (CommandInterpreter &interpreter) :
195 CommandObjectRaw (interpreter,
197 "Evaluate a C/ObjC/C++ expression in the current program context, using user defined variables and variables currently in scope.",
199 eFlagProcessMustBePaused | eFlagTryTargetAPILock),
200 IOHandlerDelegate (IOHandlerDelegate::Completion::Expression),
201 m_option_group (interpreter),
202 m_format_options (eFormatDefault),
203 m_command_options (),
204 m_expr_line_count (0),
209 If the expression can be evaluated statically (without running code) then it will be.\n\
210 Otherwise, by default the expression will run on the current thread with a short timeout:\n\
211 currently .25 seconds. If it doesn't return in that time, the evaluation will be interrupted\n\
212 and resumed with all threads running. You can use the -a option to disable retrying on all\n\
213 threads. You can use the -t option to set a shorter timeout.\n\
215 User defined variables:\n\
216 You can define your own variables for convenience or to be used in subsequent expressions.\n\
217 You define them the same way you would define variables in C. If the first character of \n\
218 your user defined variable is a $, then the variable's value will be available in future\n\
219 expressions, otherwise it will just be available in the current expression.\n\
222 Continuing evaluation after a breakpoint:\n\
223 If the \"-i false\" option is used, and execution is interrupted by a breakpoint hit, once\n\
224 you are done with your investigation, you can either remove the expression execution frames\n\
225 from the stack with \"thread return -x\" or if you are still interested in the expression result\n\
226 you can issue the \"continue\" command and the expression evaluation will complete and the\n\
227 expression result will be available using the \"thread.completed-expression\" key in the thread\n\
232 expr my_struct->a = my_array[3] \n\
233 expr -f bin -- (index * 8) + 5 \n\
234 expr unsigned int $foo = 5\n\
235 expr char c[] = \"foo\"; c[0]\n");
237 CommandArgumentEntry arg;
238 CommandArgumentData expression_arg;
240 // Define the first (and only) variant of this arg.
241 expression_arg.arg_type = eArgTypeExpression;
242 expression_arg.arg_repetition = eArgRepeatPlain;
244 // There is only one variant this argument could be; put it into the argument entry.
245 arg.push_back (expression_arg);
247 // Push the data for the first argument into the m_arguments vector.
248 m_arguments.push_back (arg);
250 // Add the "--format" and "--gdb-format"
251 m_option_group.Append (&m_format_options, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1);
252 m_option_group.Append (&m_command_options);
253 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1 | LLDB_OPT_SET_2);
254 m_option_group.Finalize();
257 CommandObjectExpression::~CommandObjectExpression ()
262 CommandObjectExpression::GetOptions ()
264 return &m_option_group;
268 CommandObjectExpression::EvaluateExpression
271 Stream *output_stream,
272 Stream *error_stream,
273 CommandReturnObject *result
276 // Don't use m_exe_ctx as this might be called asynchronously
277 // after the command object DoExecute has finished when doing
278 // multi-line expression that use an input reader...
279 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
281 Target *target = exe_ctx.GetTargetPtr();
284 target = Host::GetDummyTarget(m_interpreter.GetDebugger()).get();
288 lldb::ValueObjectSP result_valobj_sp;
290 bool keep_in_memory = true;
292 EvaluateExpressionOptions options;
293 options.SetCoerceToId(m_varobj_options.use_objc);
294 options.SetUnwindOnError(m_command_options.unwind_on_error);
295 options.SetIgnoreBreakpoints (m_command_options.ignore_breakpoints);
296 options.SetKeepInMemory(keep_in_memory);
297 options.SetUseDynamic(m_varobj_options.use_dynamic);
298 options.SetTryAllThreads(m_command_options.try_all_threads);
299 options.SetDebug(m_command_options.debug);
301 // If there is any chance we are going to stop and want to see
302 // what went wrong with our expression, we should generate debug info
303 if (!m_command_options.ignore_breakpoints ||
304 !m_command_options.unwind_on_error)
305 options.SetGenerateDebugInfo(true);
307 if (m_command_options.timeout > 0)
308 options.SetTimeoutUsec(m_command_options.timeout);
310 options.SetTimeoutUsec(0);
312 target->EvaluateExpression(expr, exe_ctx.GetFramePtr(),
313 result_valobj_sp, options);
315 if (result_valobj_sp)
317 Format format = m_format_options.GetFormat();
319 if (result_valobj_sp->GetError().Success())
321 if (format != eFormatVoid)
323 if (format != eFormatDefault)
324 result_valobj_sp->SetFormat (format);
326 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(m_command_options.m_verbosity,format));
328 result_valobj_sp->Dump(*output_stream,options);
331 result->SetStatus (eReturnStatusSuccessFinishResult);
336 if (result_valobj_sp->GetError().GetError() == ClangUserExpression::kNoResult)
338 if (format != eFormatVoid && m_interpreter.GetDebugger().GetNotifyVoid())
340 error_stream->PutCString("(void)\n");
344 result->SetStatus (eReturnStatusSuccessFinishResult);
348 const char *error_cstr = result_valobj_sp->GetError().AsCString();
349 if (error_cstr && error_cstr[0])
351 const size_t error_cstr_len = strlen (error_cstr);
352 const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n';
353 if (strstr(error_cstr, "error:") != error_cstr)
354 error_stream->PutCString ("error: ");
355 error_stream->Write(error_cstr, error_cstr_len);
356 if (!ends_with_newline)
361 error_stream->PutCString ("error: unknown error\n");
365 result->SetStatus (eReturnStatusFailed);
372 error_stream->Printf ("error: invalid execution context for expression\n");
380 CommandObjectExpression::IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
382 io_handler.SetIsDone(true);
383 // StreamSP output_stream = io_handler.GetDebugger().GetAsyncOutputStream();
384 // StreamSP error_stream = io_handler.GetDebugger().GetAsyncErrorStream();
385 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
386 StreamFileSP error_sp(io_handler.GetErrorStreamFile());
388 EvaluateExpression (line.c_str(),
398 CommandObjectExpression::IOHandlerLinesUpdated (IOHandler &io_handler,
403 if (line_idx == UINT32_MAX)
405 // Remove the last line from "lines" so it doesn't appear
406 // in our final expression
409 return LineStatus::Done;
411 else if (line_idx + 1 == lines.GetSize())
413 // The last line was edited, if this line is empty, then we are done
414 // getting our multiple lines.
415 if (lines[line_idx].empty())
416 return LineStatus::Done;
418 return LineStatus::Success;
422 CommandObjectExpression::GetMultilineExpression ()
424 m_expr_lines.clear();
425 m_expr_line_count = 0;
427 Debugger &debugger = GetCommandInterpreter().GetDebugger();
428 const bool multiple_lines = true; // Get multiple lines
429 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
430 "lldb-expr", // Name of input reader for history
433 1, // Show line numbers starting at 1
436 StreamFileSP output_sp(io_handler_sp->GetOutputStreamFile());
439 output_sp->PutCString("Enter expressions, then terminate with an empty line to evaluate:\n");
442 debugger.PushIOHandler(io_handler_sp);
446 CommandObjectExpression::DoExecute
449 CommandReturnObject &result
452 m_option_group.NotifyOptionParsingStarting();
454 const char * expr = NULL;
456 if (command[0] == '\0')
458 GetMultilineExpression ();
459 return result.Succeeded();
462 if (command[0] == '-')
464 // We have some options and these options MUST end with --.
465 const char *end_options = NULL;
466 const char *s = command;
469 end_options = ::strstr (s, "--");
472 end_options += 2; // Get past the "--"
473 if (::isspace (end_options[0]))
476 while (::isspace (*expr))
486 Args args (command, end_options - command);
487 if (!ParseOptions (args, result))
490 Error error (m_option_group.NotifyOptionParsingFinished());
493 result.AppendError (error.AsCString());
494 result.SetStatus (eReturnStatusFailed);
498 // No expression following options
499 if (expr == NULL || expr[0] == '\0')
501 GetMultilineExpression ();
502 return result.Succeeded();
510 if (EvaluateExpression (expr, &(result.GetOutputStream()), &(result.GetErrorStream()), &result))
513 result.SetStatus (eReturnStatusFailed);