1 //===-- CommandObjectFrame.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 "CommandObjectFrame.h"
17 // Other libraries and framework includes
19 #include "lldb/Core/Debugger.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/StreamFile.h"
22 #include "lldb/Core/StreamString.h"
23 #include "lldb/Core/Timer.h"
24 #include "lldb/Core/Value.h"
25 #include "lldb/Core/ValueObject.h"
26 #include "lldb/Core/ValueObjectVariable.h"
27 #include "lldb/DataFormatters/DataVisualization.h"
28 #include "lldb/Host/Host.h"
29 #include "lldb/Interpreter/Args.h"
30 #include "lldb/Interpreter/CommandInterpreter.h"
31 #include "lldb/Interpreter/CommandReturnObject.h"
32 #include "lldb/Interpreter/Options.h"
33 #include "lldb/Interpreter/OptionGroupFormat.h"
34 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
35 #include "lldb/Interpreter/OptionGroupVariable.h"
36 #include "lldb/Symbol/ClangASTType.h"
37 #include "lldb/Symbol/ClangASTContext.h"
38 #include "lldb/Symbol/ObjectFile.h"
39 #include "lldb/Symbol/SymbolContext.h"
40 #include "lldb/Symbol/Type.h"
41 #include "lldb/Symbol/Variable.h"
42 #include "lldb/Symbol/VariableList.h"
43 #include "lldb/Target/Process.h"
44 #include "lldb/Target/StackFrame.h"
45 #include "lldb/Target/Thread.h"
46 #include "lldb/Target/Target.h"
49 using namespace lldb_private;
51 #pragma mark CommandObjectFrameInfo
53 //-------------------------------------------------------------------------
54 // CommandObjectFrameInfo
55 //-------------------------------------------------------------------------
57 class CommandObjectFrameInfo : public CommandObjectParsed
61 CommandObjectFrameInfo (CommandInterpreter &interpreter) :
62 CommandObjectParsed (interpreter,
64 "List information about the currently selected frame in the current thread.",
67 eFlagTryTargetAPILock |
68 eFlagProcessMustBeLaunched |
69 eFlagProcessMustBePaused )
73 ~CommandObjectFrameInfo ()
79 DoExecute (Args& command, CommandReturnObject &result)
81 m_exe_ctx.GetFrameRef().DumpUsingSettingsFormat (&result.GetOutputStream());
82 result.SetStatus (eReturnStatusSuccessFinishResult);
83 return result.Succeeded();
87 #pragma mark CommandObjectFrameSelect
89 //-------------------------------------------------------------------------
90 // CommandObjectFrameSelect
91 //-------------------------------------------------------------------------
93 class CommandObjectFrameSelect : public CommandObjectParsed
97 class CommandOptions : public Options
101 CommandOptions (CommandInterpreter &interpreter) :
104 OptionParsingStarting ();
113 SetOptionValue (uint32_t option_idx, const char *option_arg)
116 bool success = false;
117 const int short_option = m_getopt_table[option_idx].val;
118 switch (short_option)
121 relative_frame_offset = Args::StringToSInt32 (option_arg, INT32_MIN, 0, &success);
123 error.SetErrorStringWithFormat ("invalid frame offset argument '%s'", option_arg);
127 error.SetErrorStringWithFormat ("invalid short option character '%c'", short_option);
135 OptionParsingStarting ()
137 relative_frame_offset = INT32_MIN;
140 const OptionDefinition*
143 return g_option_table;
146 // Options table: Required for subclasses of Options.
148 static OptionDefinition g_option_table[];
149 int32_t relative_frame_offset;
152 CommandObjectFrameSelect (CommandInterpreter &interpreter) :
153 CommandObjectParsed (interpreter,
155 "Select a frame by index from within the current thread and make it the current frame.",
157 eFlagRequiresThread |
158 eFlagTryTargetAPILock |
159 eFlagProcessMustBeLaunched |
160 eFlagProcessMustBePaused ),
161 m_options (interpreter)
163 CommandArgumentEntry arg;
164 CommandArgumentData index_arg;
166 // Define the first (and only) variant of this arg.
167 index_arg.arg_type = eArgTypeFrameIndex;
168 index_arg.arg_repetition = eArgRepeatOptional;
170 // There is only one variant this argument could be; put it into the argument entry.
171 arg.push_back (index_arg);
173 // Push the data for the first argument into the m_arguments vector.
174 m_arguments.push_back (arg);
177 ~CommandObjectFrameSelect ()
191 DoExecute (Args& command, CommandReturnObject &result)
193 // No need to check "thread" for validity as eFlagRequiresThread ensures it is valid
194 Thread *thread = m_exe_ctx.GetThreadPtr();
196 uint32_t frame_idx = UINT32_MAX;
197 if (m_options.relative_frame_offset != INT32_MIN)
199 // The one and only argument is a signed relative frame index
200 frame_idx = thread->GetSelectedFrameIndex ();
201 if (frame_idx == UINT32_MAX)
204 if (m_options.relative_frame_offset < 0)
206 if (frame_idx >= -m_options.relative_frame_offset)
207 frame_idx += m_options.relative_frame_offset;
212 //If you are already at the bottom of the stack, then just warn and don't reset the frame.
213 result.AppendError("Already at the bottom of the stack");
214 result.SetStatus(eReturnStatusFailed);
221 else if (m_options.relative_frame_offset > 0)
223 // I don't want "up 20" where "20" takes you past the top of the stack to produce
224 // an error, but rather to just go to the top. So I have to count the stack here...
225 const uint32_t num_frames = thread->GetStackFrameCount();
226 if (num_frames - frame_idx > m_options.relative_frame_offset)
227 frame_idx += m_options.relative_frame_offset;
230 if (frame_idx == num_frames - 1)
232 //If we are already at the top of the stack, just warn and don't reset the frame.
233 result.AppendError("Already at the top of the stack");
234 result.SetStatus(eReturnStatusFailed);
238 frame_idx = num_frames - 1;
244 if (command.GetArgumentCount() == 1)
246 const char *frame_idx_cstr = command.GetArgumentAtIndex(0);
247 frame_idx = Args::StringToUInt32 (frame_idx_cstr, UINT32_MAX, 0);
249 else if (command.GetArgumentCount() == 0)
251 frame_idx = thread->GetSelectedFrameIndex ();
252 if (frame_idx == UINT32_MAX)
259 result.AppendError ("invalid arguments.\n");
260 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
264 bool success = thread->SetSelectedFrameByIndexNoisily (frame_idx, result.GetOutputStream());
267 m_exe_ctx.SetFrameSP(thread->GetSelectedFrame ());
268 result.SetStatus (eReturnStatusSuccessFinishResult);
272 result.AppendErrorWithFormat ("Frame index (%u) out of range.\n", frame_idx);
273 result.SetStatus (eReturnStatusFailed);
276 return result.Succeeded();
280 CommandOptions m_options;
284 CommandObjectFrameSelect::CommandOptions::g_option_table[] =
286 { LLDB_OPT_SET_1, false, "relative", 'r', required_argument, NULL, 0, eArgTypeOffset, "A relative frame index offset from the current frame index."},
287 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
290 #pragma mark CommandObjectFrameVariable
291 //----------------------------------------------------------------------
292 // List images with associated information
293 //----------------------------------------------------------------------
294 class CommandObjectFrameVariable : public CommandObjectParsed
298 CommandObjectFrameVariable (CommandInterpreter &interpreter) :
299 CommandObjectParsed (interpreter,
301 "Show frame variables. All argument and local variables "
302 "that are in scope will be shown when no arguments are given. "
303 "If any arguments are specified, they can be names of "
304 "argument, local, file static and file global variables. "
305 "Children of aggregate variables can be specified such as "
309 eFlagTryTargetAPILock |
310 eFlagProcessMustBeLaunched |
311 eFlagProcessMustBePaused |
312 eFlagRequiresProcess),
313 m_option_group (interpreter),
314 m_option_variable(true), // Include the frame specific options by passing "true"
315 m_option_format (eFormatDefault),
318 CommandArgumentEntry arg;
319 CommandArgumentData var_name_arg;
321 // Define the first (and only) variant of this arg.
322 var_name_arg.arg_type = eArgTypeVarName;
323 var_name_arg.arg_repetition = eArgRepeatStar;
325 // There is only one variant this argument could be; put it into the argument entry.
326 arg.push_back (var_name_arg);
328 // Push the data for the first argument into the m_arguments vector.
329 m_arguments.push_back (arg);
331 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
332 m_option_group.Append (&m_option_format, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1);
333 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
334 m_option_group.Finalize();
338 ~CommandObjectFrameVariable ()
346 return &m_option_group;
351 HandleArgumentCompletion (Args &input,
353 int &cursor_char_position,
354 OptionElementVector &opt_element_vector,
355 int match_start_point,
356 int max_return_elements,
360 // Arguments are the standard source file completer.
361 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
362 completion_str.erase (cursor_char_position);
364 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
365 CommandCompletions::eVariablePathCompletion,
366 completion_str.c_str(),
372 return matches.GetSize();
377 DoExecute (Args& command, CommandReturnObject &result)
379 // No need to check "frame" for validity as eFlagRequiresFrame ensures it is valid
380 StackFrame *frame = m_exe_ctx.GetFramePtr();
382 Stream &s = result.GetOutputStream();
384 bool get_file_globals = true;
386 // Be careful about the stack frame, if any summary formatter runs code, it might clear the StackFrameList
387 // for the thread. So hold onto a shared pointer to the frame so it stays alive.
389 VariableList *variable_list = frame->GetVariableList (get_file_globals);
392 ValueObjectSP valobj_sp;
394 const char *name_cstr = NULL;
397 TypeSummaryImplSP summary_format_sp;
398 if (!m_option_variable.summary.IsCurrentValueEmpty())
399 DataVisualization::NamedSummaryFormats::GetSummaryFormat(ConstString(m_option_variable.summary.GetCurrentValue()), summary_format_sp);
400 else if (!m_option_variable.summary_string.IsCurrentValueEmpty())
401 summary_format_sp.reset(new StringSummaryFormat(TypeSummaryImpl::Flags(),m_option_variable.summary_string.GetCurrentValue()));
403 ValueObject::DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(false,eFormatDefault,summary_format_sp));
407 const Format format = m_option_format.GetFormat();
408 options.SetFormat(format);
410 if (command.GetArgumentCount() > 0)
412 VariableList regex_var_list;
414 // If we have any args to the variable command, we will make
415 // variable objects from them...
416 for (idx = 0; (name_cstr = command.GetArgumentAtIndex(idx)) != NULL; ++idx)
418 if (m_option_variable.use_regex)
420 const size_t regex_start_index = regex_var_list.GetSize();
421 RegularExpression regex (name_cstr);
422 if (regex.Compile(name_cstr))
424 size_t num_matches = 0;
425 const size_t num_new_regex_vars = variable_list->AppendVariablesIfUnique(regex,
428 if (num_new_regex_vars > 0)
430 for (size_t regex_idx = regex_start_index, end_index = regex_var_list.GetSize();
431 regex_idx < end_index;
434 var_sp = regex_var_list.GetVariableAtIndex (regex_idx);
437 valobj_sp = frame->GetValueObjectForFrameVariable (var_sp, m_varobj_options.use_dynamic);
440 // if (format != eFormatDefault)
441 // valobj_sp->SetFormat (format);
443 if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
445 bool show_fullpaths = false;
446 bool show_module = true;
447 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
450 ValueObject::DumpValueObject (result.GetOutputStream(),
457 else if (num_matches == 0)
459 result.GetErrorStream().Printf ("error: no variables matched the regular expression '%s'.\n", name_cstr);
464 char regex_error[1024];
465 if (regex.GetErrorAsCString(regex_error, sizeof(regex_error)))
466 result.GetErrorStream().Printf ("error: %s\n", regex_error);
468 result.GetErrorStream().Printf ("error: unkown regex error when compiling '%s'\n", name_cstr);
471 else // No regex, either exact variable names or variable expressions.
474 uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
475 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
476 lldb::VariableSP var_sp;
477 valobj_sp = frame->GetValueForVariableExpressionPath (name_cstr,
478 m_varobj_options.use_dynamic,
484 // if (format != eFormatDefault)
485 // valobj_sp->SetFormat (format);
486 if (m_option_variable.show_decl && var_sp && var_sp->GetDeclaration ().GetFile())
488 var_sp->GetDeclaration ().DumpStopContext (&s, false);
492 options.SetFormat(format);
494 Stream &output_stream = result.GetOutputStream();
495 options.SetRootValueObjectName(valobj_sp->GetParent() ? name_cstr : NULL);
496 ValueObject::DumpValueObject (output_stream,
502 const char *error_cstr = error.AsCString(NULL);
504 result.GetErrorStream().Printf("error: %s\n", error_cstr);
506 result.GetErrorStream().Printf ("error: unable to find any variable expression path that matches '%s'\n", name_cstr);
511 else // No command arg specified. Use variable_list, instead.
513 const size_t num_variables = variable_list->GetSize();
514 if (num_variables > 0)
516 for (size_t i=0; i<num_variables; i++)
518 var_sp = variable_list->GetVariableAtIndex(i);
519 bool dump_variable = true;
520 switch (var_sp->GetScope())
522 case eValueTypeVariableGlobal:
523 dump_variable = m_option_variable.show_globals;
524 if (dump_variable && m_option_variable.show_scope)
525 s.PutCString("GLOBAL: ");
528 case eValueTypeVariableStatic:
529 dump_variable = m_option_variable.show_globals;
530 if (dump_variable && m_option_variable.show_scope)
531 s.PutCString("STATIC: ");
534 case eValueTypeVariableArgument:
535 dump_variable = m_option_variable.show_args;
536 if (dump_variable && m_option_variable.show_scope)
537 s.PutCString(" ARG: ");
540 case eValueTypeVariableLocal:
541 dump_variable = m_option_variable.show_locals;
542 if (dump_variable && m_option_variable.show_scope)
543 s.PutCString(" LOCAL: ");
552 // Use the variable object code to make sure we are
553 // using the same APIs as the the public API will be
555 valobj_sp = frame->GetValueObjectForFrameVariable (var_sp,
556 m_varobj_options.use_dynamic);
559 // if (format != eFormatDefault)
560 // valobj_sp->SetFormat (format);
562 // When dumping all variables, don't print any variables
563 // that are not in scope to avoid extra unneeded output
564 if (valobj_sp->IsInScope ())
566 if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
568 var_sp->GetDeclaration ().DumpStopContext (&s, false);
572 options.SetFormat(format);
573 options.SetRootValueObjectName(name_cstr);
574 ValueObject::DumpValueObject (result.GetOutputStream(),
583 result.SetStatus (eReturnStatusSuccessFinishResult);
586 if (m_interpreter.TruncationWarningNecessary())
588 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
590 m_interpreter.TruncationWarningGiven();
593 return result.Succeeded();
597 OptionGroupOptions m_option_group;
598 OptionGroupVariable m_option_variable;
599 OptionGroupFormat m_option_format;
600 OptionGroupValueObjectDisplay m_varobj_options;
604 #pragma mark CommandObjectMultiwordFrame
606 //-------------------------------------------------------------------------
607 // CommandObjectMultiwordFrame
608 //-------------------------------------------------------------------------
610 CommandObjectMultiwordFrame::CommandObjectMultiwordFrame (CommandInterpreter &interpreter) :
611 CommandObjectMultiword (interpreter,
613 "A set of commands for operating on the current thread's frames.",
614 "frame <subcommand> [<subcommand-options>]")
616 LoadSubCommand ("info", CommandObjectSP (new CommandObjectFrameInfo (interpreter)));
617 LoadSubCommand ("select", CommandObjectSP (new CommandObjectFrameSelect (interpreter)));
618 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectFrameVariable (interpreter)));
621 CommandObjectMultiwordFrame::~CommandObjectMultiwordFrame ()