1 //===-- CommandObjectFrame.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 
10 #include "lldb/lldb-python.h"
11 
12 #include "CommandObjectFrame.h"
13 
14 // C Includes
15 // C++ Includes
16 #include <string>
17 // Other libraries and framework includes
18 // Project 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"
47 
48 using namespace lldb;
49 using namespace lldb_private;
50 
51 #pragma mark CommandObjectFrameInfo
52 
53 //-------------------------------------------------------------------------
54 // CommandObjectFrameInfo
55 //-------------------------------------------------------------------------
56 
57 class CommandObjectFrameInfo : public CommandObjectParsed
58 {
59 public:
60 
61     CommandObjectFrameInfo (CommandInterpreter &interpreter) :
62         CommandObjectParsed (interpreter,
63                              "frame info",
64                              "List information about the currently selected frame in the current thread.",
65                              "frame info",
66                              eFlagRequiresFrame         |
67                              eFlagTryTargetAPILock      |
68                              eFlagProcessMustBeLaunched |
69                              eFlagProcessMustBePaused   )
70     {
71     }
72 
73     ~CommandObjectFrameInfo ()
74     {
75     }
76 
77 protected:
78     bool
79     DoExecute (Args& command, CommandReturnObject &result)
80     {
81         m_exe_ctx.GetFrameRef().DumpUsingSettingsFormat (&result.GetOutputStream());
82         result.SetStatus (eReturnStatusSuccessFinishResult);
83         return result.Succeeded();
84     }
85 };
86 
87 #pragma mark CommandObjectFrameSelect
88 
89 //-------------------------------------------------------------------------
90 // CommandObjectFrameSelect
91 //-------------------------------------------------------------------------
92 
93 class CommandObjectFrameSelect : public CommandObjectParsed
94 {
95 public:
96 
97    class CommandOptions : public Options
98     {
99     public:
100 
101         CommandOptions (CommandInterpreter &interpreter) :
102             Options(interpreter)
103         {
104             OptionParsingStarting ();
105         }
106 
107         virtual
108         ~CommandOptions ()
109         {
110         }
111 
112         virtual Error
113         SetOptionValue (uint32_t option_idx, const char *option_arg)
114         {
115             Error error;
116             bool success = false;
117             const int short_option = m_getopt_table[option_idx].val;
118             switch (short_option)
119             {
120             case 'r':
121                 relative_frame_offset = Args::StringToSInt32 (option_arg, INT32_MIN, 0, &success);
122                 if (!success)
123                     error.SetErrorStringWithFormat ("invalid frame offset argument '%s'", option_arg);
124                 break;
125 
126             default:
127                 error.SetErrorStringWithFormat ("invalid short option character '%c'", short_option);
128                 break;
129             }
130 
131             return error;
132         }
133 
134         void
135         OptionParsingStarting ()
136         {
137             relative_frame_offset = INT32_MIN;
138         }
139 
140         const OptionDefinition*
141         GetDefinitions ()
142         {
143             return g_option_table;
144         }
145 
146         // Options table: Required for subclasses of Options.
147 
148         static OptionDefinition g_option_table[];
149         int32_t relative_frame_offset;
150     };
151 
152     CommandObjectFrameSelect (CommandInterpreter &interpreter) :
153         CommandObjectParsed (interpreter,
154                              "frame select",
155                              "Select a frame by index from within the current thread and make it the current frame.",
156                              NULL,
157                              eFlagRequiresThread        |
158                              eFlagTryTargetAPILock      |
159                              eFlagProcessMustBeLaunched |
160                              eFlagProcessMustBePaused   ),
161         m_options (interpreter)
162     {
163         CommandArgumentEntry arg;
164         CommandArgumentData index_arg;
165 
166         // Define the first (and only) variant of this arg.
167         index_arg.arg_type = eArgTypeFrameIndex;
168         index_arg.arg_repetition = eArgRepeatOptional;
169 
170         // There is only one variant this argument could be; put it into the argument entry.
171         arg.push_back (index_arg);
172 
173         // Push the data for the first argument into the m_arguments vector.
174         m_arguments.push_back (arg);
175     }
176 
177     ~CommandObjectFrameSelect ()
178     {
179     }
180 
181     virtual
182     Options *
183     GetOptions ()
184     {
185         return &m_options;
186     }
187 
188 
189 protected:
190     bool
191     DoExecute (Args& command, CommandReturnObject &result)
192     {
193         // No need to check "thread" for validity as eFlagRequiresThread ensures it is valid
194         Thread *thread = m_exe_ctx.GetThreadPtr();
195 
196         uint32_t frame_idx = UINT32_MAX;
197         if (m_options.relative_frame_offset != INT32_MIN)
198         {
199             // The one and only argument is a signed relative frame index
200             frame_idx = thread->GetSelectedFrameIndex ();
201             if (frame_idx == UINT32_MAX)
202                 frame_idx = 0;
203 
204             if (m_options.relative_frame_offset < 0)
205             {
206                 if (frame_idx >= -m_options.relative_frame_offset)
207                     frame_idx += m_options.relative_frame_offset;
208                 else
209                 {
210                     if (frame_idx == 0)
211                     {
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);
215                         return false;
216                     }
217                     else
218                         frame_idx = 0;
219                 }
220             }
221             else if (m_options.relative_frame_offset > 0)
222             {
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;
228                 else
229                 {
230                     if (frame_idx == num_frames - 1)
231                     {
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);
235                         return false;
236                     }
237                     else
238                         frame_idx = num_frames - 1;
239                 }
240             }
241         }
242         else
243         {
244             if (command.GetArgumentCount() == 1)
245             {
246                 const char *frame_idx_cstr = command.GetArgumentAtIndex(0);
247                 frame_idx = Args::StringToUInt32 (frame_idx_cstr, UINT32_MAX, 0);
248             }
249             else if (command.GetArgumentCount() == 0)
250             {
251                 frame_idx = thread->GetSelectedFrameIndex ();
252                 if (frame_idx == UINT32_MAX)
253                 {
254                     frame_idx = 0;
255                 }
256             }
257             else
258             {
259                 result.AppendError ("invalid arguments.\n");
260                 m_options.GenerateOptionUsage (result.GetErrorStream(), this);
261             }
262         }
263 
264         bool success = thread->SetSelectedFrameByIndexNoisily (frame_idx, result.GetOutputStream());
265         if (success)
266         {
267             m_exe_ctx.SetFrameSP(thread->GetSelectedFrame ());
268             result.SetStatus (eReturnStatusSuccessFinishResult);
269         }
270         else
271         {
272             result.AppendErrorWithFormat ("Frame index (%u) out of range.\n", frame_idx);
273             result.SetStatus (eReturnStatusFailed);
274         }
275 
276         return result.Succeeded();
277     }
278 protected:
279 
280     CommandOptions m_options;
281 };
282 
283 OptionDefinition
284 CommandObjectFrameSelect::CommandOptions::g_option_table[] =
285 {
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 }
288 };
289 
290 #pragma mark CommandObjectFrameVariable
291 //----------------------------------------------------------------------
292 // List images with associated information
293 //----------------------------------------------------------------------
294 class CommandObjectFrameVariable : public CommandObjectParsed
295 {
296 public:
297 
298     CommandObjectFrameVariable (CommandInterpreter &interpreter) :
299         CommandObjectParsed (interpreter,
300                              "frame variable",
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 "
306                              "'var->child.x'.",
307                              NULL,
308                              eFlagRequiresFrame |
309                              eFlagTryTargetAPILock |
310                              eFlagProcessMustBeLaunched |
311                              eFlagProcessMustBePaused),
312         m_option_group (interpreter),
313         m_option_variable(true), // Include the frame specific options by passing "true"
314         m_option_format (eFormatDefault),
315         m_varobj_options()
316     {
317         CommandArgumentEntry arg;
318         CommandArgumentData var_name_arg;
319 
320         // Define the first (and only) variant of this arg.
321         var_name_arg.arg_type = eArgTypeVarName;
322         var_name_arg.arg_repetition = eArgRepeatStar;
323 
324         // There is only one variant this argument could be; put it into the argument entry.
325         arg.push_back (var_name_arg);
326 
327         // Push the data for the first argument into the m_arguments vector.
328         m_arguments.push_back (arg);
329 
330         m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
331         m_option_group.Append (&m_option_format, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1);
332         m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
333         m_option_group.Finalize();
334     }
335 
336     virtual
337     ~CommandObjectFrameVariable ()
338     {
339     }
340 
341     virtual
342     Options *
343     GetOptions ()
344     {
345         return &m_option_group;
346     }
347 
348 protected:
349     virtual bool
350     DoExecute (Args& command, CommandReturnObject &result)
351     {
352         // No need to check "frame" for validity as eFlagRequiresFrame ensures it is valid
353         StackFrame *frame = m_exe_ctx.GetFramePtr();
354 
355         Stream &s = result.GetOutputStream();
356 
357         bool get_file_globals = true;
358 
359         // Be careful about the stack frame, if any summary formatter runs code, it might clear the StackFrameList
360         // for the thread.  So hold onto a shared pointer to the frame so it stays alive.
361 
362         VariableList *variable_list = frame->GetVariableList (get_file_globals);
363 
364         VariableSP var_sp;
365         ValueObjectSP valobj_sp;
366 
367         const char *name_cstr = NULL;
368         size_t idx;
369 
370         TypeSummaryImplSP summary_format_sp;
371         if (!m_option_variable.summary.IsCurrentValueEmpty())
372             DataVisualization::NamedSummaryFormats::GetSummaryFormat(ConstString(m_option_variable.summary.GetCurrentValue()), summary_format_sp);
373         else if (!m_option_variable.summary_string.IsCurrentValueEmpty())
374             summary_format_sp.reset(new StringSummaryFormat(TypeSummaryImpl::Flags(),m_option_variable.summary_string.GetCurrentValue()));
375 
376         ValueObject::DumpValueObjectOptions options;
377 
378         options.SetMaximumPointerDepth(m_varobj_options.ptr_depth)
379             .SetMaximumDepth(m_varobj_options.max_depth)
380             .SetShowTypes(m_varobj_options.show_types)
381             .SetShowLocation(m_varobj_options.show_location)
382             .SetUseObjectiveC(m_varobj_options.use_objc)
383             .SetUseDynamicType(m_varobj_options.use_dynamic)
384             .SetUseSyntheticValue(m_varobj_options.use_synth)
385             .SetFlatOutput(m_varobj_options.flat_output)
386             .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
387             .SetIgnoreCap(m_varobj_options.ignore_cap)
388             .SetSummary(summary_format_sp);
389 
390         if (m_varobj_options.be_raw)
391             options.SetRawDisplay(true);
392 
393         if (variable_list)
394         {
395             const Format format = m_option_format.GetFormat();
396             options.SetFormat(format);
397 
398             if (command.GetArgumentCount() > 0)
399             {
400                 VariableList regex_var_list;
401 
402                 // If we have any args to the variable command, we will make
403                 // variable objects from them...
404                 for (idx = 0; (name_cstr = command.GetArgumentAtIndex(idx)) != NULL; ++idx)
405                 {
406                     if (m_option_variable.use_regex)
407                     {
408                         const size_t regex_start_index = regex_var_list.GetSize();
409                         RegularExpression regex (name_cstr);
410                         if (regex.Compile(name_cstr))
411                         {
412                             size_t num_matches = 0;
413                             const size_t num_new_regex_vars = variable_list->AppendVariablesIfUnique(regex,
414                                                                                                      regex_var_list,
415                                                                                                      num_matches);
416                             if (num_new_regex_vars > 0)
417                             {
418                                 for (size_t regex_idx = regex_start_index, end_index = regex_var_list.GetSize();
419                                      regex_idx < end_index;
420                                      ++regex_idx)
421                                 {
422                                     var_sp = regex_var_list.GetVariableAtIndex (regex_idx);
423                                     if (var_sp)
424                                     {
425                                         valobj_sp = frame->GetValueObjectForFrameVariable (var_sp, m_varobj_options.use_dynamic);
426                                         if (valobj_sp)
427                                         {
428 //                                            if (format != eFormatDefault)
429 //                                                valobj_sp->SetFormat (format);
430 
431                                             if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
432                                             {
433                                                 bool show_fullpaths = false;
434                                                 bool show_module = true;
435                                                 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
436                                                     s.PutCString (": ");
437                                             }
438                                             ValueObject::DumpValueObject (result.GetOutputStream(),
439                                                                           valobj_sp.get(),
440                                                                           options);
441                                         }
442                                     }
443                                 }
444                             }
445                             else if (num_matches == 0)
446                             {
447                                 result.GetErrorStream().Printf ("error: no variables matched the regular expression '%s'.\n", name_cstr);
448                             }
449                         }
450                         else
451                         {
452                             char regex_error[1024];
453                             if (regex.GetErrorAsCString(regex_error, sizeof(regex_error)))
454                                 result.GetErrorStream().Printf ("error: %s\n", regex_error);
455                             else
456                                 result.GetErrorStream().Printf ("error: unkown regex error when compiling '%s'\n", name_cstr);
457                         }
458                     }
459                     else // No regex, either exact variable names or variable expressions.
460                     {
461                         Error error;
462                         uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember |
463                                                      StackFrame::eExpressionPathOptionsAllowDirectIVarAccess;
464                         lldb::VariableSP var_sp;
465                         valobj_sp = frame->GetValueForVariableExpressionPath (name_cstr,
466                                                                               m_varobj_options.use_dynamic,
467                                                                               expr_path_options,
468                                                                               var_sp,
469                                                                               error);
470                         if (valobj_sp)
471                         {
472 //                            if (format != eFormatDefault)
473 //                                valobj_sp->SetFormat (format);
474                             if (m_option_variable.show_decl && var_sp && var_sp->GetDeclaration ().GetFile())
475                             {
476                                 var_sp->GetDeclaration ().DumpStopContext (&s, false);
477                                 s.PutCString (": ");
478                             }
479 
480                             options.SetFormat(format);
481 
482                             Stream &output_stream = result.GetOutputStream();
483                             options.SetRootValueObjectName(valobj_sp->GetParent() ? name_cstr : NULL);
484                             ValueObject::DumpValueObject (output_stream,
485                                                           valobj_sp.get(),
486                                                           options);
487                         }
488                         else
489                         {
490                             const char *error_cstr = error.AsCString(NULL);
491                             if (error_cstr)
492                                 result.GetErrorStream().Printf("error: %s\n", error_cstr);
493                             else
494                                 result.GetErrorStream().Printf ("error: unable to find any variable expression path that matches '%s'\n", name_cstr);
495                         }
496                     }
497                 }
498             }
499             else // No command arg specified.  Use variable_list, instead.
500             {
501                 const size_t num_variables = variable_list->GetSize();
502                 if (num_variables > 0)
503                 {
504                     for (size_t i=0; i<num_variables; i++)
505                     {
506                         var_sp = variable_list->GetVariableAtIndex(i);
507                         bool dump_variable = true;
508                         switch (var_sp->GetScope())
509                         {
510                             case eValueTypeVariableGlobal:
511                                 dump_variable = m_option_variable.show_globals;
512                                 if (dump_variable && m_option_variable.show_scope)
513                                     s.PutCString("GLOBAL: ");
514                                 break;
515 
516                             case eValueTypeVariableStatic:
517                                 dump_variable = m_option_variable.show_globals;
518                                 if (dump_variable && m_option_variable.show_scope)
519                                     s.PutCString("STATIC: ");
520                                 break;
521 
522                             case eValueTypeVariableArgument:
523                                 dump_variable = m_option_variable.show_args;
524                                 if (dump_variable && m_option_variable.show_scope)
525                                     s.PutCString("   ARG: ");
526                                 break;
527 
528                             case eValueTypeVariableLocal:
529                                 dump_variable = m_option_variable.show_locals;
530                                 if (dump_variable && m_option_variable.show_scope)
531                                     s.PutCString(" LOCAL: ");
532                                 break;
533 
534                             default:
535                                 break;
536                         }
537 
538                         if (dump_variable)
539                         {
540                             // Use the variable object code to make sure we are
541                             // using the same APIs as the the public API will be
542                             // using...
543                             valobj_sp = frame->GetValueObjectForFrameVariable (var_sp,
544                                                                                m_varobj_options.use_dynamic);
545                             if (valobj_sp)
546                             {
547 //                                if (format != eFormatDefault)
548 //                                    valobj_sp->SetFormat (format);
549 
550                                 // When dumping all variables, don't print any variables
551                                 // that are not in scope to avoid extra unneeded output
552                                 if (valobj_sp->IsInScope ())
553                                 {
554                                     if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
555                                     {
556                                         var_sp->GetDeclaration ().DumpStopContext (&s, false);
557                                         s.PutCString (": ");
558                                     }
559 
560                                     options.SetFormat(format);
561                                     options.SetRootValueObjectName(name_cstr);
562                                     ValueObject::DumpValueObject (result.GetOutputStream(),
563                                                                   valobj_sp.get(),
564                                                                   options);
565                                 }
566                             }
567                         }
568                     }
569                 }
570             }
571             result.SetStatus (eReturnStatusSuccessFinishResult);
572         }
573 
574         if (m_interpreter.TruncationWarningNecessary())
575         {
576             result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
577                                             m_cmd_name.c_str());
578             m_interpreter.TruncationWarningGiven();
579         }
580 
581         return result.Succeeded();
582     }
583 protected:
584 
585     OptionGroupOptions m_option_group;
586     OptionGroupVariable m_option_variable;
587     OptionGroupFormat m_option_format;
588     OptionGroupValueObjectDisplay m_varobj_options;
589 };
590 
591 
592 #pragma mark CommandObjectMultiwordFrame
593 
594 //-------------------------------------------------------------------------
595 // CommandObjectMultiwordFrame
596 //-------------------------------------------------------------------------
597 
598 CommandObjectMultiwordFrame::CommandObjectMultiwordFrame (CommandInterpreter &interpreter) :
599     CommandObjectMultiword (interpreter,
600                             "frame",
601                             "A set of commands for operating on the current thread's frames.",
602                             "frame <subcommand> [<subcommand-options>]")
603 {
604     LoadSubCommand ("info",   CommandObjectSP (new CommandObjectFrameInfo (interpreter)));
605     LoadSubCommand ("select", CommandObjectSP (new CommandObjectFrameSelect (interpreter)));
606     LoadSubCommand ("variable", CommandObjectSP (new CommandObjectFrameVariable (interpreter)));
607 }
608 
609 CommandObjectMultiwordFrame::~CommandObjectMultiwordFrame ()
610 {
611 }
612 
613