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/DataFormatters/ValueObjectPrinter.h"
29 #include "lldb/Host/Host.h"
30 #include "lldb/Interpreter/Args.h"
31 #include "lldb/Interpreter/CommandInterpreter.h"
32 #include "lldb/Interpreter/CommandReturnObject.h"
33 #include "lldb/Interpreter/Options.h"
34 #include "lldb/Interpreter/OptionGroupFormat.h"
35 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
36 #include "lldb/Interpreter/OptionGroupVariable.h"
37 #include "lldb/Symbol/ClangASTType.h"
38 #include "lldb/Symbol/ClangASTContext.h"
39 #include "lldb/Symbol/ObjectFile.h"
40 #include "lldb/Symbol/SymbolContext.h"
41 #include "lldb/Symbol/Type.h"
42 #include "lldb/Symbol/Variable.h"
43 #include "lldb/Symbol/VariableList.h"
44 #include "lldb/Target/Process.h"
45 #include "lldb/Target/StackFrame.h"
46 #include "lldb/Target/Thread.h"
47 #include "lldb/Target/Target.h"
50 using namespace lldb_private;
52 #pragma mark CommandObjectFrameInfo
54 //-------------------------------------------------------------------------
55 // CommandObjectFrameInfo
56 //-------------------------------------------------------------------------
58 class CommandObjectFrameInfo : public CommandObjectParsed
62 CommandObjectFrameInfo (CommandInterpreter &interpreter) :
63 CommandObjectParsed (interpreter,
65 "List information about the currently selected frame in the current thread.",
68 eFlagTryTargetAPILock |
69 eFlagProcessMustBeLaunched |
70 eFlagProcessMustBePaused )
74 ~CommandObjectFrameInfo ()
80 DoExecute (Args& command, CommandReturnObject &result)
82 m_exe_ctx.GetFrameRef().DumpUsingSettingsFormat (&result.GetOutputStream());
83 result.SetStatus (eReturnStatusSuccessFinishResult);
84 return result.Succeeded();
88 #pragma mark CommandObjectFrameSelect
90 //-------------------------------------------------------------------------
91 // CommandObjectFrameSelect
92 //-------------------------------------------------------------------------
94 class CommandObjectFrameSelect : public CommandObjectParsed
98 class CommandOptions : public Options
102 CommandOptions (CommandInterpreter &interpreter) :
105 OptionParsingStarting ();
114 SetOptionValue (uint32_t option_idx, const char *option_arg)
117 bool success = false;
118 const int short_option = m_getopt_table[option_idx].val;
119 switch (short_option)
122 relative_frame_offset = Args::StringToSInt32 (option_arg, INT32_MIN, 0, &success);
124 error.SetErrorStringWithFormat ("invalid frame offset argument '%s'", option_arg);
128 error.SetErrorStringWithFormat ("invalid short option character '%c'", short_option);
136 OptionParsingStarting ()
138 relative_frame_offset = INT32_MIN;
141 const OptionDefinition*
144 return g_option_table;
147 // Options table: Required for subclasses of Options.
149 static OptionDefinition g_option_table[];
150 int32_t relative_frame_offset;
153 CommandObjectFrameSelect (CommandInterpreter &interpreter) :
154 CommandObjectParsed (interpreter,
156 "Select a frame by index from within the current thread and make it the current frame.",
158 eFlagRequiresThread |
159 eFlagTryTargetAPILock |
160 eFlagProcessMustBeLaunched |
161 eFlagProcessMustBePaused ),
162 m_options (interpreter)
164 CommandArgumentEntry arg;
165 CommandArgumentData index_arg;
167 // Define the first (and only) variant of this arg.
168 index_arg.arg_type = eArgTypeFrameIndex;
169 index_arg.arg_repetition = eArgRepeatOptional;
171 // There is only one variant this argument could be; put it into the argument entry.
172 arg.push_back (index_arg);
174 // Push the data for the first argument into the m_arguments vector.
175 m_arguments.push_back (arg);
178 ~CommandObjectFrameSelect ()
192 DoExecute (Args& command, CommandReturnObject &result)
194 // No need to check "thread" for validity as eFlagRequiresThread ensures it is valid
195 Thread *thread = m_exe_ctx.GetThreadPtr();
197 uint32_t frame_idx = UINT32_MAX;
198 if (m_options.relative_frame_offset != INT32_MIN)
200 // The one and only argument is a signed relative frame index
201 frame_idx = thread->GetSelectedFrameIndex ();
202 if (frame_idx == UINT32_MAX)
205 if (m_options.relative_frame_offset < 0)
207 if (static_cast<int32_t>(frame_idx) >= -m_options.relative_frame_offset)
208 frame_idx += m_options.relative_frame_offset;
213 //If you are already at the bottom of the stack, then just warn and don't reset the frame.
214 result.AppendError("Already at the bottom of the stack");
215 result.SetStatus(eReturnStatusFailed);
222 else if (m_options.relative_frame_offset > 0)
224 // I don't want "up 20" where "20" takes you past the top of the stack to produce
225 // an error, but rather to just go to the top. So I have to count the stack here...
226 const uint32_t num_frames = thread->GetStackFrameCount();
227 if (static_cast<int32_t>(num_frames - frame_idx) > m_options.relative_frame_offset)
228 frame_idx += m_options.relative_frame_offset;
231 if (frame_idx == num_frames - 1)
233 //If we are already at the top of the stack, just warn and don't reset the frame.
234 result.AppendError("Already at the top of the stack");
235 result.SetStatus(eReturnStatusFailed);
239 frame_idx = num_frames - 1;
245 if (command.GetArgumentCount() == 1)
247 const char *frame_idx_cstr = command.GetArgumentAtIndex(0);
248 bool success = false;
249 frame_idx = Args::StringToUInt32 (frame_idx_cstr, UINT32_MAX, 0, &success);
252 result.AppendErrorWithFormat ("invalid frame index argument '%s'", frame_idx_cstr);
253 result.SetStatus (eReturnStatusFailed);
257 else if (command.GetArgumentCount() == 0)
259 frame_idx = thread->GetSelectedFrameIndex ();
260 if (frame_idx == UINT32_MAX)
267 result.AppendError ("invalid arguments.\n");
268 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
272 bool success = thread->SetSelectedFrameByIndexNoisily (frame_idx, result.GetOutputStream());
275 m_exe_ctx.SetFrameSP(thread->GetSelectedFrame ());
276 result.SetStatus (eReturnStatusSuccessFinishResult);
280 result.AppendErrorWithFormat ("Frame index (%u) out of range.\n", frame_idx);
281 result.SetStatus (eReturnStatusFailed);
284 return result.Succeeded();
288 CommandOptions m_options;
292 CommandObjectFrameSelect::CommandOptions::g_option_table[] =
294 { LLDB_OPT_SET_1, false, "relative", 'r', OptionParser::eRequiredArgument, NULL, NULL, 0, eArgTypeOffset, "A relative frame index offset from the current frame index."},
295 { 0, false, NULL, 0, 0, NULL, NULL, 0, eArgTypeNone, NULL }
298 #pragma mark CommandObjectFrameVariable
299 //----------------------------------------------------------------------
300 // List images with associated information
301 //----------------------------------------------------------------------
302 class CommandObjectFrameVariable : public CommandObjectParsed
306 CommandObjectFrameVariable (CommandInterpreter &interpreter) :
307 CommandObjectParsed (interpreter,
309 "Show frame variables. All argument and local variables "
310 "that are in scope will be shown when no arguments are given. "
311 "If any arguments are specified, they can be names of "
312 "argument, local, file static and file global variables. "
313 "Children of aggregate variables can be specified such as "
317 eFlagTryTargetAPILock |
318 eFlagProcessMustBeLaunched |
319 eFlagProcessMustBePaused |
320 eFlagRequiresProcess),
321 m_option_group (interpreter),
322 m_option_variable(true), // Include the frame specific options by passing "true"
323 m_option_format (eFormatDefault),
326 CommandArgumentEntry arg;
327 CommandArgumentData var_name_arg;
329 // Define the first (and only) variant of this arg.
330 var_name_arg.arg_type = eArgTypeVarName;
331 var_name_arg.arg_repetition = eArgRepeatStar;
333 // There is only one variant this argument could be; put it into the argument entry.
334 arg.push_back (var_name_arg);
336 // Push the data for the first argument into the m_arguments vector.
337 m_arguments.push_back (arg);
339 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
340 m_option_group.Append (&m_option_format, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1);
341 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
342 m_option_group.Finalize();
346 ~CommandObjectFrameVariable ()
354 return &m_option_group;
359 HandleArgumentCompletion (Args &input,
361 int &cursor_char_position,
362 OptionElementVector &opt_element_vector,
363 int match_start_point,
364 int max_return_elements,
368 // Arguments are the standard source file completer.
369 std::string completion_str (input.GetArgumentAtIndex(cursor_index));
370 completion_str.erase (cursor_char_position);
372 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
373 CommandCompletions::eVariablePathCompletion,
374 completion_str.c_str(),
380 return matches.GetSize();
385 DoExecute (Args& command, CommandReturnObject &result)
387 // No need to check "frame" for validity as eFlagRequiresFrame ensures it is valid
388 StackFrame *frame = m_exe_ctx.GetFramePtr();
390 Stream &s = result.GetOutputStream();
392 bool get_file_globals = true;
394 // Be careful about the stack frame, if any summary formatter runs code, it might clear the StackFrameList
395 // for the thread. So hold onto a shared pointer to the frame so it stays alive.
397 VariableList *variable_list = frame->GetVariableList (get_file_globals);
400 ValueObjectSP valobj_sp;
402 const char *name_cstr = NULL;
405 TypeSummaryImplSP summary_format_sp;
406 if (!m_option_variable.summary.IsCurrentValueEmpty())
407 DataVisualization::NamedSummaryFormats::GetSummaryFormat(ConstString(m_option_variable.summary.GetCurrentValue()), summary_format_sp);
408 else if (!m_option_variable.summary_string.IsCurrentValueEmpty())
409 summary_format_sp.reset(new StringSummaryFormat(TypeSummaryImpl::Flags(),m_option_variable.summary_string.GetCurrentValue()));
411 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions(eLanguageRuntimeDescriptionDisplayVerbosityFull,eFormatDefault,summary_format_sp));
415 const Format format = m_option_format.GetFormat();
416 options.SetFormat(format);
418 if (command.GetArgumentCount() > 0)
420 VariableList regex_var_list;
422 // If we have any args to the variable command, we will make
423 // variable objects from them...
424 for (idx = 0; (name_cstr = command.GetArgumentAtIndex(idx)) != NULL; ++idx)
426 if (m_option_variable.use_regex)
428 const size_t regex_start_index = regex_var_list.GetSize();
429 RegularExpression regex (name_cstr);
430 if (regex.Compile(name_cstr))
432 size_t num_matches = 0;
433 const size_t num_new_regex_vars = variable_list->AppendVariablesIfUnique(regex,
436 if (num_new_regex_vars > 0)
438 for (size_t regex_idx = regex_start_index, end_index = regex_var_list.GetSize();
439 regex_idx < end_index;
442 var_sp = regex_var_list.GetVariableAtIndex (regex_idx);
445 valobj_sp = frame->GetValueObjectForFrameVariable (var_sp, m_varobj_options.use_dynamic);
448 // if (format != eFormatDefault)
449 // valobj_sp->SetFormat (format);
451 if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
453 bool show_fullpaths = false;
454 bool show_module = true;
455 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
458 valobj_sp->Dump(result.GetOutputStream(),options);
463 else if (num_matches == 0)
465 result.GetErrorStream().Printf ("error: no variables matched the regular expression '%s'.\n", name_cstr);
470 char regex_error[1024];
471 if (regex.GetErrorAsCString(regex_error, sizeof(regex_error)))
472 result.GetErrorStream().Printf ("error: %s\n", regex_error);
474 result.GetErrorStream().Printf ("error: unknown regex error when compiling '%s'\n", name_cstr);
477 else // No regex, either exact variable names or variable expressions.
480 uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
481 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
482 lldb::VariableSP var_sp;
483 valobj_sp = frame->GetValueForVariableExpressionPath (name_cstr,
484 m_varobj_options.use_dynamic,
490 // if (format != eFormatDefault)
491 // valobj_sp->SetFormat (format);
492 if (m_option_variable.show_decl && var_sp && var_sp->GetDeclaration ().GetFile())
494 var_sp->GetDeclaration ().DumpStopContext (&s, false);
498 options.SetFormat(format);
500 Stream &output_stream = result.GetOutputStream();
501 options.SetRootValueObjectName(valobj_sp->GetParent() ? name_cstr : NULL);
502 valobj_sp->Dump(output_stream,options);
506 const char *error_cstr = error.AsCString(NULL);
508 result.GetErrorStream().Printf("error: %s\n", error_cstr);
510 result.GetErrorStream().Printf ("error: unable to find any variable expression path that matches '%s'\n", name_cstr);
515 else // No command arg specified. Use variable_list, instead.
517 const size_t num_variables = variable_list->GetSize();
518 if (num_variables > 0)
520 for (size_t i=0; i<num_variables; i++)
522 var_sp = variable_list->GetVariableAtIndex(i);
523 bool dump_variable = true;
524 switch (var_sp->GetScope())
526 case eValueTypeVariableGlobal:
527 dump_variable = m_option_variable.show_globals;
528 if (dump_variable && m_option_variable.show_scope)
529 s.PutCString("GLOBAL: ");
532 case eValueTypeVariableStatic:
533 dump_variable = m_option_variable.show_globals;
534 if (dump_variable && m_option_variable.show_scope)
535 s.PutCString("STATIC: ");
538 case eValueTypeVariableArgument:
539 dump_variable = m_option_variable.show_args;
540 if (dump_variable && m_option_variable.show_scope)
541 s.PutCString(" ARG: ");
544 case eValueTypeVariableLocal:
545 dump_variable = m_option_variable.show_locals;
546 if (dump_variable && m_option_variable.show_scope)
547 s.PutCString(" LOCAL: ");
556 // Use the variable object code to make sure we are
557 // using the same APIs as the the public API will be
559 valobj_sp = frame->GetValueObjectForFrameVariable (var_sp,
560 m_varobj_options.use_dynamic);
563 // if (format != eFormatDefault)
564 // valobj_sp->SetFormat (format);
566 // When dumping all variables, don't print any variables
567 // that are not in scope to avoid extra unneeded output
568 if (valobj_sp->IsInScope ())
570 if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
572 var_sp->GetDeclaration ().DumpStopContext (&s, false);
576 options.SetFormat(format);
577 options.SetRootValueObjectName(name_cstr);
578 valobj_sp->Dump(result.GetOutputStream(),options);
585 result.SetStatus (eReturnStatusSuccessFinishResult);
588 if (m_interpreter.TruncationWarningNecessary())
590 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
592 m_interpreter.TruncationWarningGiven();
595 return result.Succeeded();
599 OptionGroupOptions m_option_group;
600 OptionGroupVariable m_option_variable;
601 OptionGroupFormat m_option_format;
602 OptionGroupValueObjectDisplay m_varobj_options;
606 #pragma mark CommandObjectMultiwordFrame
608 //-------------------------------------------------------------------------
609 // CommandObjectMultiwordFrame
610 //-------------------------------------------------------------------------
612 CommandObjectMultiwordFrame::CommandObjectMultiwordFrame (CommandInterpreter &interpreter) :
613 CommandObjectMultiword (interpreter,
615 "A set of commands for operating on the current thread's frames.",
616 "frame <subcommand> [<subcommand-options>]")
618 LoadSubCommand ("info", CommandObjectSP (new CommandObjectFrameInfo (interpreter)));
619 LoadSubCommand ("select", CommandObjectSP (new CommandObjectFrameSelect (interpreter)));
620 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectFrameVariable (interpreter)));
623 CommandObjectMultiwordFrame::~CommandObjectMultiwordFrame ()