130fdc8d8SChris Lattner //===-- CommandObjectFrame.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 
1030fdc8d8SChris Lattner #include "CommandObjectFrame.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1530fdc8d8SChris Lattner // Project includes
160a976141SEnrico Granata #include "lldb/Core/DataVisualization.h"
1730fdc8d8SChris Lattner #include "lldb/Core/Debugger.h"
186d56d2ceSJim Ingham #include "lldb/Core/Module.h"
196d56d2ceSJim Ingham #include "lldb/Core/StreamFile.h"
2030fdc8d8SChris Lattner #include "lldb/Core/Timer.h"
216d56d2ceSJim Ingham #include "lldb/Core/Value.h"
226d56d2ceSJim Ingham #include "lldb/Core/ValueObject.h"
236d56d2ceSJim Ingham #include "lldb/Core/ValueObjectVariable.h"
247fb56d0aSGreg Clayton #include "lldb/Host/Host.h"
256d56d2ceSJim Ingham #include "lldb/Interpreter/Args.h"
2630fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h"
2730fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h"
286d56d2ceSJim Ingham #include "lldb/Interpreter/Options.h"
292837b766SJim Ingham #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
30715c2365SGreg Clayton #include "lldb/Interpreter/OptionGroupVariable.h"
31b1d7529eSJohnny Chen #include "lldb/Interpreter/OptionGroupWatchpoint.h"
326d56d2ceSJim Ingham #include "lldb/Symbol/ClangASTType.h"
336d56d2ceSJim Ingham #include "lldb/Symbol/ClangASTContext.h"
346d56d2ceSJim Ingham #include "lldb/Symbol/ObjectFile.h"
356d56d2ceSJim Ingham #include "lldb/Symbol/SymbolContext.h"
366d56d2ceSJim Ingham #include "lldb/Symbol/Type.h"
376d56d2ceSJim Ingham #include "lldb/Symbol/Variable.h"
386d56d2ceSJim Ingham #include "lldb/Symbol/VariableList.h"
3930fdc8d8SChris Lattner #include "lldb/Target/Process.h"
4030fdc8d8SChris Lattner #include "lldb/Target/StackFrame.h"
4130fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
426d56d2ceSJim Ingham #include "lldb/Target/Target.h"
4330fdc8d8SChris Lattner 
4430fdc8d8SChris Lattner using namespace lldb;
4530fdc8d8SChris Lattner using namespace lldb_private;
4630fdc8d8SChris Lattner 
4730fdc8d8SChris Lattner #pragma mark CommandObjectFrameInfo
4830fdc8d8SChris Lattner 
4930fdc8d8SChris Lattner //-------------------------------------------------------------------------
5030fdc8d8SChris Lattner // CommandObjectFrameInfo
5130fdc8d8SChris Lattner //-------------------------------------------------------------------------
5230fdc8d8SChris Lattner 
5330fdc8d8SChris Lattner class CommandObjectFrameInfo : public CommandObject
5430fdc8d8SChris Lattner {
5530fdc8d8SChris Lattner public:
5630fdc8d8SChris Lattner 
57a7015092SGreg Clayton     CommandObjectFrameInfo (CommandInterpreter &interpreter) :
58a7015092SGreg Clayton         CommandObject (interpreter,
59a7015092SGreg Clayton                        "frame info",
60e3d26315SCaroline Tice                        "List information about the currently selected frame in the current thread.",
6130fdc8d8SChris Lattner                        "frame info",
6230fdc8d8SChris Lattner                        eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
6330fdc8d8SChris Lattner     {
6430fdc8d8SChris Lattner     }
6530fdc8d8SChris Lattner 
6630fdc8d8SChris Lattner     ~CommandObjectFrameInfo ()
6730fdc8d8SChris Lattner     {
6830fdc8d8SChris Lattner     }
6930fdc8d8SChris Lattner 
7030fdc8d8SChris Lattner     bool
71a7015092SGreg Clayton     Execute (Args& command,
7230fdc8d8SChris Lattner              CommandReturnObject &result)
7330fdc8d8SChris Lattner     {
748b82f087SGreg Clayton         ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
7530fdc8d8SChris Lattner         if (exe_ctx.frame)
7630fdc8d8SChris Lattner         {
770603aa9dSGreg Clayton             exe_ctx.frame->DumpUsingSettingsFormat (&result.GetOutputStream());
7830fdc8d8SChris Lattner             result.SetStatus (eReturnStatusSuccessFinishResult);
7930fdc8d8SChris Lattner         }
8030fdc8d8SChris Lattner         else
8130fdc8d8SChris Lattner         {
8230fdc8d8SChris Lattner             result.AppendError ("no current frame");
8330fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
8430fdc8d8SChris Lattner         }
8530fdc8d8SChris Lattner         return result.Succeeded();
8630fdc8d8SChris Lattner     }
8730fdc8d8SChris Lattner };
8830fdc8d8SChris Lattner 
8930fdc8d8SChris Lattner #pragma mark CommandObjectFrameSelect
9030fdc8d8SChris Lattner 
9130fdc8d8SChris Lattner //-------------------------------------------------------------------------
9230fdc8d8SChris Lattner // CommandObjectFrameSelect
9330fdc8d8SChris Lattner //-------------------------------------------------------------------------
9430fdc8d8SChris Lattner 
9530fdc8d8SChris Lattner class CommandObjectFrameSelect : public CommandObject
9630fdc8d8SChris Lattner {
9730fdc8d8SChris Lattner public:
9830fdc8d8SChris Lattner 
99864174e1SGreg Clayton    class CommandOptions : public Options
100864174e1SGreg Clayton     {
101864174e1SGreg Clayton     public:
102864174e1SGreg Clayton 
103eb0103f2SGreg Clayton         CommandOptions (CommandInterpreter &interpreter) :
104f16066e8SJohnny Chen             Options(interpreter)
105864174e1SGreg Clayton         {
106f6b8b581SGreg Clayton             OptionParsingStarting ();
107864174e1SGreg Clayton         }
108864174e1SGreg Clayton 
109864174e1SGreg Clayton         virtual
110864174e1SGreg Clayton         ~CommandOptions ()
111864174e1SGreg Clayton         {
112864174e1SGreg Clayton         }
113864174e1SGreg Clayton 
114864174e1SGreg Clayton         virtual Error
115f6b8b581SGreg Clayton         SetOptionValue (uint32_t option_idx, const char *option_arg)
116864174e1SGreg Clayton         {
117864174e1SGreg Clayton             Error error;
118864174e1SGreg Clayton             bool success = false;
119864174e1SGreg Clayton             char short_option = (char) m_getopt_table[option_idx].val;
120864174e1SGreg Clayton             switch (short_option)
121864174e1SGreg Clayton             {
122864174e1SGreg Clayton             case 'r':
123864174e1SGreg Clayton                 relative_frame_offset = Args::StringToSInt32 (option_arg, INT32_MIN, 0, &success);
124864174e1SGreg Clayton                 if (!success)
125864174e1SGreg Clayton                     error.SetErrorStringWithFormat ("invalid frame offset argument '%s'.\n", option_arg);
126864174e1SGreg Clayton                 break;
127864174e1SGreg Clayton 
128864174e1SGreg Clayton             default:
1292c88643aSBenjamin Kramer                 error.SetErrorStringWithFormat ("Invalid short option character '%c'.\n", short_option);
130864174e1SGreg Clayton                 break;
131864174e1SGreg Clayton             }
132864174e1SGreg Clayton 
133864174e1SGreg Clayton             return error;
134864174e1SGreg Clayton         }
135864174e1SGreg Clayton 
136864174e1SGreg Clayton         void
137f6b8b581SGreg Clayton         OptionParsingStarting ()
138864174e1SGreg Clayton         {
139864174e1SGreg Clayton             relative_frame_offset = INT32_MIN;
140864174e1SGreg Clayton         }
141864174e1SGreg Clayton 
142e0d378b3SGreg Clayton         const OptionDefinition*
143864174e1SGreg Clayton         GetDefinitions ()
144864174e1SGreg Clayton         {
145864174e1SGreg Clayton             return g_option_table;
146864174e1SGreg Clayton         }
147864174e1SGreg Clayton 
148864174e1SGreg Clayton         // Options table: Required for subclasses of Options.
149864174e1SGreg Clayton 
150e0d378b3SGreg Clayton         static OptionDefinition g_option_table[];
151864174e1SGreg Clayton         int32_t relative_frame_offset;
152864174e1SGreg Clayton     };
153864174e1SGreg Clayton 
154a7015092SGreg Clayton     CommandObjectFrameSelect (CommandInterpreter &interpreter) :
155a7015092SGreg Clayton         CommandObject (interpreter,
156a7015092SGreg Clayton                        "frame select",
157e3d26315SCaroline Tice                        "Select a frame by index from within the current thread and make it the current frame.",
158405fe67fSCaroline Tice                        NULL,
159eb0103f2SGreg Clayton                        eFlagProcessMustBeLaunched | eFlagProcessMustBePaused),
160eb0103f2SGreg Clayton         m_options (interpreter)
16130fdc8d8SChris Lattner     {
162405fe67fSCaroline Tice         CommandArgumentEntry arg;
163405fe67fSCaroline Tice         CommandArgumentData index_arg;
164405fe67fSCaroline Tice 
165405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
166405fe67fSCaroline Tice         index_arg.arg_type = eArgTypeFrameIndex;
167864174e1SGreg Clayton         index_arg.arg_repetition = eArgRepeatOptional;
168405fe67fSCaroline Tice 
169405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
170405fe67fSCaroline Tice         arg.push_back (index_arg);
171405fe67fSCaroline Tice 
172405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
173405fe67fSCaroline Tice         m_arguments.push_back (arg);
17430fdc8d8SChris Lattner     }
17530fdc8d8SChris Lattner 
17630fdc8d8SChris Lattner     ~CommandObjectFrameSelect ()
17730fdc8d8SChris Lattner     {
17830fdc8d8SChris Lattner     }
17930fdc8d8SChris Lattner 
180864174e1SGreg Clayton     virtual
181864174e1SGreg Clayton     Options *
182864174e1SGreg Clayton     GetOptions ()
183864174e1SGreg Clayton     {
184864174e1SGreg Clayton         return &m_options;
185864174e1SGreg Clayton     }
186864174e1SGreg Clayton 
187864174e1SGreg Clayton 
18830fdc8d8SChris Lattner     bool
189a7015092SGreg Clayton     Execute (Args& command,
19030fdc8d8SChris Lattner              CommandReturnObject &result)
19130fdc8d8SChris Lattner     {
1928b82f087SGreg Clayton         ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
19330fdc8d8SChris Lattner         if (exe_ctx.thread)
19430fdc8d8SChris Lattner         {
195864174e1SGreg Clayton             const uint32_t num_frames = exe_ctx.thread->GetStackFrameCount();
196864174e1SGreg Clayton             uint32_t frame_idx = UINT32_MAX;
197864174e1SGreg Clayton             if (m_options.relative_frame_offset != INT32_MIN)
198864174e1SGreg Clayton             {
199864174e1SGreg Clayton                 // The one and only argument is a signed relative frame index
200864174e1SGreg Clayton                 frame_idx = exe_ctx.thread->GetSelectedFrameIndex ();
201864174e1SGreg Clayton                 if (frame_idx == UINT32_MAX)
202864174e1SGreg Clayton                     frame_idx = 0;
203864174e1SGreg Clayton 
204864174e1SGreg Clayton                 if (m_options.relative_frame_offset < 0)
205864174e1SGreg Clayton                 {
206864174e1SGreg Clayton                     if (frame_idx >= -m_options.relative_frame_offset)
207864174e1SGreg Clayton                         frame_idx += m_options.relative_frame_offset;
208864174e1SGreg Clayton                     else
209213b4546SJim Ingham                     {
210213b4546SJim Ingham                         if (frame_idx == 0)
211213b4546SJim Ingham                         {
212213b4546SJim Ingham                             //If you are already at the bottom of the stack, then just warn and don't reset the frame.
213213b4546SJim Ingham                             result.AppendError("Already at the bottom of the stack");
214213b4546SJim Ingham                             result.SetStatus(eReturnStatusFailed);
215213b4546SJim Ingham                             return false;
216213b4546SJim Ingham                         }
217213b4546SJim Ingham                         else
218864174e1SGreg Clayton                             frame_idx = 0;
219864174e1SGreg Clayton                     }
220213b4546SJim Ingham                 }
221864174e1SGreg Clayton                 else if (m_options.relative_frame_offset > 0)
222864174e1SGreg Clayton                 {
223864174e1SGreg Clayton                     if (num_frames - frame_idx > m_options.relative_frame_offset)
224864174e1SGreg Clayton                         frame_idx += m_options.relative_frame_offset;
225864174e1SGreg Clayton                     else
226213b4546SJim Ingham                     {
227213b4546SJim Ingham                         if (frame_idx == num_frames - 1)
228213b4546SJim Ingham                         {
229213b4546SJim Ingham                             //If we are already at the top of the stack, just warn and don't reset the frame.
230213b4546SJim Ingham                             result.AppendError("Already at the top of the stack");
231213b4546SJim Ingham                             result.SetStatus(eReturnStatusFailed);
232213b4546SJim Ingham                             return false;
233213b4546SJim Ingham                         }
234213b4546SJim Ingham                         else
235864174e1SGreg Clayton                             frame_idx = num_frames - 1;
236864174e1SGreg Clayton                     }
237864174e1SGreg Clayton                 }
238213b4546SJim Ingham             }
239864174e1SGreg Clayton             else
240864174e1SGreg Clayton             {
24130fdc8d8SChris Lattner                 if (command.GetArgumentCount() == 1)
24230fdc8d8SChris Lattner                 {
24330fdc8d8SChris Lattner                     const char *frame_idx_cstr = command.GetArgumentAtIndex(0);
244864174e1SGreg Clayton                     frame_idx = Args::StringToUInt32 (frame_idx_cstr, UINT32_MAX, 0);
245864174e1SGreg Clayton                 }
246864174e1SGreg Clayton                 else
247864174e1SGreg Clayton                 {
248864174e1SGreg Clayton                     result.AppendError ("invalid arguments.\n");
249eb0103f2SGreg Clayton                     m_options.GenerateOptionUsage (result.GetErrorStream(), this);
250864174e1SGreg Clayton                 }
251864174e1SGreg Clayton             }
25230fdc8d8SChris Lattner 
25330fdc8d8SChris Lattner             if (frame_idx < num_frames)
25430fdc8d8SChris Lattner             {
2552976d00aSJim Ingham                 exe_ctx.thread->SetSelectedFrameByIndex (frame_idx);
2562976d00aSJim Ingham                 exe_ctx.frame = exe_ctx.thread->GetSelectedFrame ().get();
25730fdc8d8SChris Lattner 
25830fdc8d8SChris Lattner                 if (exe_ctx.frame)
25930fdc8d8SChris Lattner                 {
260e40e4218SJim Ingham                     bool already_shown = false;
261e40e4218SJim Ingham                     SymbolContext frame_sc(exe_ctx.frame->GetSymbolContext(eSymbolContextLineEntry));
262daccaa9eSCaroline Tice                     if (m_interpreter.GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
263e40e4218SJim Ingham                     {
264e40e4218SJim Ingham                         already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
265e40e4218SJim Ingham                     }
266e40e4218SJim Ingham 
2677260f620SGreg Clayton                     bool show_frame_info = true;
2687260f620SGreg Clayton                     bool show_source = !already_shown;
2697260f620SGreg Clayton                     uint32_t source_lines_before = 3;
2707260f620SGreg Clayton                     uint32_t source_lines_after = 3;
2717260f620SGreg Clayton                     if (exe_ctx.frame->GetStatus(result.GetOutputStream(),
2727260f620SGreg Clayton                                                  show_frame_info,
2737260f620SGreg Clayton                                                  show_source,
2747260f620SGreg Clayton                                                  source_lines_before,
2757260f620SGreg Clayton                                                  source_lines_after))
27630fdc8d8SChris Lattner                     {
27730fdc8d8SChris Lattner                         result.SetStatus (eReturnStatusSuccessFinishResult);
27830fdc8d8SChris Lattner                         return result.Succeeded();
27930fdc8d8SChris Lattner                     }
28030fdc8d8SChris Lattner                 }
28130fdc8d8SChris Lattner             }
28230fdc8d8SChris Lattner             result.AppendErrorWithFormat ("Frame index (%u) out of range.\n", frame_idx);
28330fdc8d8SChris Lattner         }
28430fdc8d8SChris Lattner         else
28530fdc8d8SChris Lattner         {
28630fdc8d8SChris Lattner             result.AppendError ("no current thread");
28730fdc8d8SChris Lattner         }
28830fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
28930fdc8d8SChris Lattner         return false;
29030fdc8d8SChris Lattner     }
291864174e1SGreg Clayton protected:
292864174e1SGreg Clayton 
293864174e1SGreg Clayton     CommandOptions m_options;
294864174e1SGreg Clayton };
295864174e1SGreg Clayton 
296e0d378b3SGreg Clayton OptionDefinition
297864174e1SGreg Clayton CommandObjectFrameSelect::CommandOptions::g_option_table[] =
298864174e1SGreg Clayton {
299864174e1SGreg Clayton { LLDB_OPT_SET_1, false, "relative", 'r', required_argument, NULL, 0, eArgTypeOffset, "A relative frame index offset from the current frame index."},
300864174e1SGreg Clayton { 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
30130fdc8d8SChris Lattner };
30230fdc8d8SChris Lattner 
3036d56d2ceSJim Ingham #pragma mark CommandObjectFrameVariable
3046d56d2ceSJim Ingham //----------------------------------------------------------------------
3056d56d2ceSJim Ingham // List images with associated information
3066d56d2ceSJim Ingham //----------------------------------------------------------------------
3076d56d2ceSJim Ingham class CommandObjectFrameVariable : public CommandObject
3086d56d2ceSJim Ingham {
3096d56d2ceSJim Ingham public:
3106d56d2ceSJim Ingham 
311a7015092SGreg Clayton     CommandObjectFrameVariable (CommandInterpreter &interpreter) :
312a7015092SGreg Clayton         CommandObject (interpreter,
3136d56d2ceSJim Ingham                        "frame variable",
314ed8a705cSGreg Clayton                        "Show frame variables. All argument and local variables "
315ed8a705cSGreg Clayton                        "that are in scope will be shown when no arguments are given. "
316ed8a705cSGreg Clayton                        "If any arguments are specified, they can be names of "
317ed8a705cSGreg Clayton                        "argument, local, file static and file global variables. "
318ed8a705cSGreg Clayton                        "Children of aggregate variables can be specified such as "
319b1d7529eSJohnny Chen                        "'var->child.x'. "
320b1d7529eSJohnny Chen                        "NOTE that '-w' option is not working yet!!! "
321b1d7529eSJohnny Chen                        "You can choose to watch a variable with the '-w' option. "
322b1d7529eSJohnny Chen                        "Note that hardware resources for watching are often limited.",
323ff471a94SJim Ingham                        NULL,
324eb0103f2SGreg Clayton                        eFlagProcessMustBeLaunched | eFlagProcessMustBePaused),
3252837b766SJim Ingham         m_option_group (interpreter),
326715c2365SGreg Clayton         m_option_variable(true), // Include the frame specific options by passing "true"
327b1d7529eSJohnny Chen         m_option_watchpoint(),
3282837b766SJim Ingham         m_varobj_options()
3296d56d2ceSJim Ingham     {
330405fe67fSCaroline Tice         CommandArgumentEntry arg;
331405fe67fSCaroline Tice         CommandArgumentData var_name_arg;
332405fe67fSCaroline Tice 
333405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
334405fe67fSCaroline Tice         var_name_arg.arg_type = eArgTypeVarName;
335405fe67fSCaroline Tice         var_name_arg.arg_repetition = eArgRepeatStar;
336405fe67fSCaroline Tice 
337405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
338405fe67fSCaroline Tice         arg.push_back (var_name_arg);
339405fe67fSCaroline Tice 
340405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
341405fe67fSCaroline Tice         m_arguments.push_back (arg);
3422837b766SJim Ingham 
343715c2365SGreg Clayton         m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
344b1d7529eSJohnny Chen         m_option_group.Append (&m_option_watchpoint, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
3452837b766SJim Ingham         m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
3462837b766SJim Ingham         m_option_group.Finalize();
3476d56d2ceSJim Ingham     }
3486d56d2ceSJim Ingham 
3496d56d2ceSJim Ingham     virtual
3506d56d2ceSJim Ingham     ~CommandObjectFrameVariable ()
3516d56d2ceSJim Ingham     {
3526d56d2ceSJim Ingham     }
3536d56d2ceSJim Ingham 
3546d56d2ceSJim Ingham     virtual
3556d56d2ceSJim Ingham     Options *
3566d56d2ceSJim Ingham     GetOptions ()
3576d56d2ceSJim Ingham     {
3582837b766SJim Ingham         return &m_option_group;
3596d56d2ceSJim Ingham     }
3606d56d2ceSJim Ingham 
3616d56d2ceSJim Ingham 
3626d56d2ceSJim Ingham     virtual bool
3636d56d2ceSJim Ingham     Execute
3646d56d2ceSJim Ingham     (
3656d56d2ceSJim Ingham         Args& command,
3666d56d2ceSJim Ingham         CommandReturnObject &result
3676d56d2ceSJim Ingham     )
3686d56d2ceSJim Ingham     {
3698b82f087SGreg Clayton         ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
3706d56d2ceSJim Ingham         if (exe_ctx.frame == NULL)
3716d56d2ceSJim Ingham         {
372340b2baaSGreg Clayton             result.AppendError ("you must be stopped in a valid stack frame to view frame variables.");
3736d56d2ceSJim Ingham             result.SetStatus (eReturnStatusFailed);
3746d56d2ceSJim Ingham             return false;
3756d56d2ceSJim Ingham         }
3761e49e5e7SJohnny Chen 
377a134cc1bSGreg Clayton         Stream &s = result.GetOutputStream();
3786d56d2ceSJim Ingham 
379a134cc1bSGreg Clayton         bool get_file_globals = true;
380650543f9SJim Ingham 
381650543f9SJim Ingham         // Be careful about the stack frame, if any summary formatter runs code, it might clear the StackFrameList
382650543f9SJim Ingham         // for the thread.  So hold onto a shared pointer to the frame so it stays alive.
383650543f9SJim Ingham 
384650543f9SJim Ingham         StackFrameSP frame_sp = exe_ctx.frame->GetSP();
385650543f9SJim Ingham 
386650543f9SJim Ingham         VariableList *variable_list = frame_sp->GetVariableList (get_file_globals);
387a134cc1bSGreg Clayton 
3886d56d2ceSJim Ingham         VariableSP var_sp;
3896d56d2ceSJim Ingham         ValueObjectSP valobj_sp;
39078a685aaSJim Ingham 
3916d56d2ceSJim Ingham         const char *name_cstr = NULL;
3926d56d2ceSJim Ingham         size_t idx;
3936d56d2ceSJim Ingham 
394f9fa6ee5SEnrico Granata         SummaryFormatSP summary_format_sp;
395f9fa6ee5SEnrico Granata         if (!m_option_variable.summary.empty())
39678d0638bSEnrico Granata             DataVisualization::NamedSummaryFormats::GetSummaryFormat(ConstString(m_option_variable.summary.c_str()), summary_format_sp);
397f9fa6ee5SEnrico Granata 
398379447a7SEnrico Granata         ValueObject::DumpValueObjectOptions options;
399379447a7SEnrico Granata 
400379447a7SEnrico Granata         options.SetPointerDepth(m_varobj_options.ptr_depth)
401379447a7SEnrico Granata             .SetMaximumDepth(m_varobj_options.max_depth)
402379447a7SEnrico Granata             .SetShowTypes(m_varobj_options.show_types)
403379447a7SEnrico Granata             .SetShowLocation(m_varobj_options.show_location)
404379447a7SEnrico Granata             .SetUseObjectiveC(m_varobj_options.use_objc)
405379447a7SEnrico Granata             .SetUseDynamicType(m_varobj_options.use_dynamic)
406379447a7SEnrico Granata             .SetUseSyntheticValue((lldb::SyntheticValueType)m_varobj_options.use_synth)
407379447a7SEnrico Granata             .SetFlatOutput(m_varobj_options.flat_output)
408379447a7SEnrico Granata             .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
409379447a7SEnrico Granata             .SetIgnoreCap(m_varobj_options.ignore_cap);
410379447a7SEnrico Granata 
411379447a7SEnrico Granata         if (m_varobj_options.be_raw)
412379447a7SEnrico Granata             options.SetRawDisplay(true);
413379447a7SEnrico Granata 
414715c2365SGreg Clayton         if (variable_list)
4159df87c17SGreg Clayton         {
4163a9838c0SJohnny Chen             // If watching a variable, there are certain restrictions to be followed.
4173a9838c0SJohnny Chen             if (m_option_watchpoint.watch_variable)
4183a9838c0SJohnny Chen             {
4193a9838c0SJohnny Chen                 if (command.GetArgumentCount() != 1) {
4203a9838c0SJohnny Chen                     result.GetErrorStream().Printf("error: specify exactly one variable when using the '-w' option\n");
4213a9838c0SJohnny Chen                     result.SetStatus(eReturnStatusFailed);
4223a9838c0SJohnny Chen                     return false;
4233a9838c0SJohnny Chen                 } else if (m_option_variable.use_regex) {
4243a9838c0SJohnny Chen                     result.GetErrorStream().Printf("error: specify your variable name exactly (no regex) when using the '-w' option\n");
4253a9838c0SJohnny Chen                     result.SetStatus(eReturnStatusFailed);
4263a9838c0SJohnny Chen                     return false;
4273a9838c0SJohnny Chen                 }
4283a9838c0SJohnny Chen 
4293a9838c0SJohnny Chen                 // Things have checked out ok...
4303a9838c0SJohnny Chen                 // m_option_watchpoint.watch_mode specifies the mode for watching.
4313a9838c0SJohnny Chen             }
4329df87c17SGreg Clayton             if (command.GetArgumentCount() > 0)
4336d56d2ceSJim Ingham             {
43446747022SGreg Clayton                 VariableList regex_var_list;
43546747022SGreg Clayton 
4366d56d2ceSJim Ingham                 // If we have any args to the variable command, we will make
4376d56d2ceSJim Ingham                 // variable objects from them...
4386d56d2ceSJim Ingham                 for (idx = 0; (name_cstr = command.GetArgumentAtIndex(idx)) != NULL; ++idx)
4396d56d2ceSJim Ingham                 {
440715c2365SGreg Clayton                     if (m_option_variable.use_regex)
44146747022SGreg Clayton                     {
44246747022SGreg Clayton                         const uint32_t regex_start_index = regex_var_list.GetSize();
44346747022SGreg Clayton                         RegularExpression regex (name_cstr);
44446747022SGreg Clayton                         if (regex.Compile(name_cstr))
44546747022SGreg Clayton                         {
44646747022SGreg Clayton                             size_t num_matches = 0;
44778a685aaSJim Ingham                             const size_t num_new_regex_vars = variable_list->AppendVariablesIfUnique(regex,
44878a685aaSJim Ingham                                                                                                      regex_var_list,
44978a685aaSJim Ingham                                                                                                      num_matches);
45046747022SGreg Clayton                             if (num_new_regex_vars > 0)
45146747022SGreg Clayton                             {
45246747022SGreg Clayton                                 for (uint32_t regex_idx = regex_start_index, end_index = regex_var_list.GetSize();
45346747022SGreg Clayton                                      regex_idx < end_index;
45446747022SGreg Clayton                                      ++regex_idx)
45546747022SGreg Clayton                                 {
45646747022SGreg Clayton                                     var_sp = regex_var_list.GetVariableAtIndex (regex_idx);
45746747022SGreg Clayton                                     if (var_sp)
45846747022SGreg Clayton                                     {
459650543f9SJim Ingham                                         valobj_sp = frame_sp->GetValueObjectForFrameVariable (var_sp, m_varobj_options.use_dynamic);
46046747022SGreg Clayton                                         if (valobj_sp)
46146747022SGreg Clayton                                         {
462715c2365SGreg Clayton                                             if (m_option_variable.format != eFormatDefault)
463715c2365SGreg Clayton                                                 valobj_sp->SetFormat (m_option_variable.format);
464ded470d3SGreg Clayton 
465715c2365SGreg Clayton                                             if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
46646747022SGreg Clayton                                             {
46745ba8543SGreg Clayton                                                 bool show_fullpaths = false;
46845ba8543SGreg Clayton                                                 bool show_module = true;
46945ba8543SGreg Clayton                                                 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
47046747022SGreg Clayton                                                     s.PutCString (": ");
47146747022SGreg Clayton                                             }
472f9fa6ee5SEnrico Granata                                             if (summary_format_sp)
473f9fa6ee5SEnrico Granata                                                 valobj_sp->SetCustomSummaryFormat(summary_format_sp);
47446747022SGreg Clayton                                             ValueObject::DumpValueObject (result.GetOutputStream(),
47546747022SGreg Clayton                                                                           valobj_sp.get(),
476379447a7SEnrico Granata                                                                           options);
47746747022SGreg Clayton                                         }
47846747022SGreg Clayton                                     }
47946747022SGreg Clayton                                 }
48046747022SGreg Clayton                             }
48146747022SGreg Clayton                             else if (num_matches == 0)
48246747022SGreg Clayton                             {
48346747022SGreg Clayton                                 result.GetErrorStream().Printf ("error: no variables matched the regular expression '%s'.\n", name_cstr);
48446747022SGreg Clayton                             }
48546747022SGreg Clayton                         }
48646747022SGreg Clayton                         else
48746747022SGreg Clayton                         {
48846747022SGreg Clayton                             char regex_error[1024];
48946747022SGreg Clayton                             if (regex.GetErrorAsCString(regex_error, sizeof(regex_error)))
49046747022SGreg Clayton                                 result.GetErrorStream().Printf ("error: %s\n", regex_error);
49146747022SGreg Clayton                             else
49246747022SGreg Clayton                                 result.GetErrorStream().Printf ("error: unkown regex error when compiling '%s'\n", name_cstr);
49346747022SGreg Clayton                         }
49446747022SGreg Clayton                     }
495887062aeSJohnny Chen                     else // No regex, either exact variable names or variable expressions.
49646747022SGreg Clayton                     {
49754979cddSGreg Clayton                         Error error;
49878a685aaSJim Ingham                         uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember;
4992837b766SJim Ingham                         lldb::VariableSP var_sp;
500650543f9SJim Ingham                         valobj_sp = frame_sp->GetValueForVariableExpressionPath (name_cstr,
5012837b766SJim Ingham                                                                                  m_varobj_options.use_dynamic,
5022837b766SJim Ingham                                                                                  expr_path_options,
5032837b766SJim Ingham                                                                                  var_sp,
5042837b766SJim Ingham                                                                                  error);
5056d56d2ceSJim Ingham                         if (valobj_sp)
5066d56d2ceSJim Ingham                         {
507715c2365SGreg Clayton                             if (m_option_variable.format != eFormatDefault)
508715c2365SGreg Clayton                                 valobj_sp->SetFormat (m_option_variable.format);
509715c2365SGreg Clayton                             if (m_option_variable.show_decl && var_sp && var_sp->GetDeclaration ().GetFile())
510a134cc1bSGreg Clayton                             {
511a134cc1bSGreg Clayton                                 var_sp->GetDeclaration ().DumpStopContext (&s, false);
512a134cc1bSGreg Clayton                                 s.PutCString (": ");
513a134cc1bSGreg Clayton                             }
514f9fa6ee5SEnrico Granata                             if (summary_format_sp)
515f9fa6ee5SEnrico Granata                                 valobj_sp->SetCustomSummaryFormat(summary_format_sp);
516887062aeSJohnny Chen 
517887062aeSJohnny Chen                             Stream &output_stream = result.GetOutputStream();
518887062aeSJohnny Chen                             ValueObject::DumpValueObject (output_stream,
519a134cc1bSGreg Clayton                                                           valobj_sp.get(),
52083c5cd9dSGreg Clayton                                                           valobj_sp->GetParent() ? name_cstr : NULL,
521379447a7SEnrico Granata                                                           options);
522887062aeSJohnny Chen                             // Process watchpoint if necessary.
523887062aeSJohnny Chen                             if (m_option_watchpoint.watch_variable)
524887062aeSJohnny Chen                             {
525*2fd89a09SJohnny Chen                                 AddressType addr_type;
526*2fd89a09SJohnny Chen                                 lldb::addr_t addr = valobj_sp->GetAddressOf(false, &addr_type);
527887062aeSJohnny Chen                                 size_t size = 0;
528*2fd89a09SJohnny Chen                                 if (addr_type == eAddressTypeLoad) {
529*2fd89a09SJohnny Chen                                     // We're in business.
530*2fd89a09SJohnny Chen                                     // Find out the size of this variable.
531*2fd89a09SJohnny Chen                                     size = valobj_sp->GetByteSize();
532*2fd89a09SJohnny Chen                                 }
533887062aeSJohnny Chen                                 uint32_t watch_type = m_option_watchpoint.watch_type;
534887062aeSJohnny Chen                                 WatchpointLocation *wp_loc =
535887062aeSJohnny Chen                                     exe_ctx.target->CreateWatchpointLocation(addr, size, watch_type).get();
536887062aeSJohnny Chen                                 if (wp_loc)
537887062aeSJohnny Chen                                 {
538887062aeSJohnny Chen                                     output_stream.Printf("Watchpoint created: ");
539887062aeSJohnny Chen                                     wp_loc->GetDescription(&output_stream, lldb::eDescriptionLevelBrief);
540887062aeSJohnny Chen                                     output_stream.EOL();
541887062aeSJohnny Chen                                     result.SetStatus(eReturnStatusSuccessFinishResult);
542887062aeSJohnny Chen                                 }
543887062aeSJohnny Chen                                 else
544887062aeSJohnny Chen                                 {
545887062aeSJohnny Chen                                     result.AppendErrorWithFormat("Watchpoint creation failed.\n");
546887062aeSJohnny Chen                                     result.SetStatus(eReturnStatusFailed);
547887062aeSJohnny Chen                                 }
548887062aeSJohnny Chen                                 return (wp_loc != NULL);
549887062aeSJohnny Chen                             }
5506d56d2ceSJim Ingham                         }
5516d56d2ceSJim Ingham                         else
5526d56d2ceSJim Ingham                         {
55354979cddSGreg Clayton                             const char *error_cstr = error.AsCString(NULL);
55454979cddSGreg Clayton                             if (error_cstr)
55554979cddSGreg Clayton                                 result.GetErrorStream().Printf("error: %s\n", error_cstr);
55654979cddSGreg Clayton                             else
55754979cddSGreg Clayton                                 result.GetErrorStream().Printf ("error: unable to find any variable expression path that matches '%s'\n", name_cstr);
5586d56d2ceSJim Ingham                         }
5596d56d2ceSJim Ingham                     }
5606d56d2ceSJim Ingham                 }
56146747022SGreg Clayton             }
562926d086eSJohnny Chen             else // No command arg specified.  Use variable_list, instead.
5636d56d2ceSJim Ingham             {
564a134cc1bSGreg Clayton                 const uint32_t num_variables = variable_list->GetSize();
5656d56d2ceSJim Ingham                 if (num_variables > 0)
5666d56d2ceSJim Ingham                 {
5676d56d2ceSJim Ingham                     for (uint32_t i=0; i<num_variables; i++)
5686d56d2ceSJim Ingham                     {
5691a65ae11SGreg Clayton                         var_sp = variable_list->GetVariableAtIndex(i);
5706d56d2ceSJim Ingham                         bool dump_variable = true;
571a134cc1bSGreg Clayton                         switch (var_sp->GetScope())
5726d56d2ceSJim Ingham                         {
5736d56d2ceSJim Ingham                             case eValueTypeVariableGlobal:
574715c2365SGreg Clayton                                 dump_variable = m_option_variable.show_globals;
575715c2365SGreg Clayton                                 if (dump_variable && m_option_variable.show_scope)
576a134cc1bSGreg Clayton                                     s.PutCString("GLOBAL: ");
5776d56d2ceSJim Ingham                                 break;
5786d56d2ceSJim Ingham 
5796d56d2ceSJim Ingham                             case eValueTypeVariableStatic:
580715c2365SGreg Clayton                                 dump_variable = m_option_variable.show_globals;
581715c2365SGreg Clayton                                 if (dump_variable && m_option_variable.show_scope)
582a134cc1bSGreg Clayton                                     s.PutCString("STATIC: ");
5836d56d2ceSJim Ingham                                 break;
5846d56d2ceSJim Ingham 
5856d56d2ceSJim Ingham                             case eValueTypeVariableArgument:
586715c2365SGreg Clayton                                 dump_variable = m_option_variable.show_args;
587715c2365SGreg Clayton                                 if (dump_variable && m_option_variable.show_scope)
588a134cc1bSGreg Clayton                                     s.PutCString("   ARG: ");
5896d56d2ceSJim Ingham                                 break;
5906d56d2ceSJim Ingham 
5916d56d2ceSJim Ingham                             case eValueTypeVariableLocal:
592715c2365SGreg Clayton                                 dump_variable = m_option_variable.show_locals;
593715c2365SGreg Clayton                                 if (dump_variable && m_option_variable.show_scope)
594a134cc1bSGreg Clayton                                     s.PutCString(" LOCAL: ");
5956d56d2ceSJim Ingham                                 break;
5966d56d2ceSJim Ingham 
5976d56d2ceSJim Ingham                             default:
5986d56d2ceSJim Ingham                                 break;
5996d56d2ceSJim Ingham                         }
6006d56d2ceSJim Ingham 
6016d56d2ceSJim Ingham                         if (dump_variable)
602a134cc1bSGreg Clayton                         {
603a134cc1bSGreg Clayton                             // Use the variable object code to make sure we are
604a134cc1bSGreg Clayton                             // using the same APIs as the the public API will be
605a134cc1bSGreg Clayton                             // using...
606650543f9SJim Ingham                             valobj_sp = frame_sp->GetValueObjectForFrameVariable (var_sp,
6072837b766SJim Ingham                                                                                   m_varobj_options.use_dynamic);
608a134cc1bSGreg Clayton                             if (valobj_sp)
609a134cc1bSGreg Clayton                             {
610715c2365SGreg Clayton                                 if (m_option_variable.format != eFormatDefault)
611715c2365SGreg Clayton                                     valobj_sp->SetFormat (m_option_variable.format);
612ded470d3SGreg Clayton 
6136f00abd5SGreg Clayton                                 // When dumping all variables, don't print any variables
6146f00abd5SGreg Clayton                                 // that are not in scope to avoid extra unneeded output
6156035b67dSJim Ingham                                 if (valobj_sp->IsInScope ())
6166f00abd5SGreg Clayton                                 {
617715c2365SGreg Clayton                                     if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
618a134cc1bSGreg Clayton                                     {
619a134cc1bSGreg Clayton                                         var_sp->GetDeclaration ().DumpStopContext (&s, false);
620a134cc1bSGreg Clayton                                         s.PutCString (": ");
621a134cc1bSGreg Clayton                                     }
622f9fa6ee5SEnrico Granata                                     if (summary_format_sp)
623f9fa6ee5SEnrico Granata                                         valobj_sp->SetCustomSummaryFormat(summary_format_sp);
6241d3afba3SGreg Clayton                                     ValueObject::DumpValueObject (result.GetOutputStream(),
625a134cc1bSGreg Clayton                                                                   valobj_sp.get(),
626a134cc1bSGreg Clayton                                                                   name_cstr,
627379447a7SEnrico Granata                                                                   options);
628a134cc1bSGreg Clayton                                 }
629a134cc1bSGreg Clayton                             }
6306d56d2ceSJim Ingham                         }
6316d56d2ceSJim Ingham                     }
6326d56d2ceSJim Ingham                 }
6336f00abd5SGreg Clayton             }
6346d56d2ceSJim Ingham             result.SetStatus (eReturnStatusSuccessFinishResult);
6356d56d2ceSJim Ingham         }
63661a80ba6SEnrico Granata 
63761a80ba6SEnrico Granata         if (m_interpreter.TruncationWarningNecessary())
63861a80ba6SEnrico Granata         {
63961a80ba6SEnrico Granata             result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
64061a80ba6SEnrico Granata                                             m_cmd_name.c_str());
64161a80ba6SEnrico Granata             m_interpreter.TruncationWarningGiven();
64261a80ba6SEnrico Granata         }
64361a80ba6SEnrico Granata 
6446d56d2ceSJim Ingham         return result.Succeeded();
6456d56d2ceSJim Ingham     }
6466d56d2ceSJim Ingham protected:
6476d56d2ceSJim Ingham 
6482837b766SJim Ingham     OptionGroupOptions m_option_group;
649715c2365SGreg Clayton     OptionGroupVariable m_option_variable;
650b1d7529eSJohnny Chen     OptionGroupWatchpoint m_option_watchpoint;
6512837b766SJim Ingham     OptionGroupValueObjectDisplay m_varobj_options;
6526d56d2ceSJim Ingham };
6536d56d2ceSJim Ingham 
6542837b766SJim Ingham 
65530fdc8d8SChris Lattner #pragma mark CommandObjectMultiwordFrame
65630fdc8d8SChris Lattner 
65730fdc8d8SChris Lattner //-------------------------------------------------------------------------
65830fdc8d8SChris Lattner // CommandObjectMultiwordFrame
65930fdc8d8SChris Lattner //-------------------------------------------------------------------------
66030fdc8d8SChris Lattner 
6616611103cSGreg Clayton CommandObjectMultiwordFrame::CommandObjectMultiwordFrame (CommandInterpreter &interpreter) :
662a7015092SGreg Clayton     CommandObjectMultiword (interpreter,
663a7015092SGreg Clayton                             "frame",
66430fdc8d8SChris Lattner                             "A set of commands for operating on the current thread's frames.",
66530fdc8d8SChris Lattner                             "frame <subcommand> [<subcommand-options>]")
66630fdc8d8SChris Lattner {
667a7015092SGreg Clayton     LoadSubCommand ("info",   CommandObjectSP (new CommandObjectFrameInfo (interpreter)));
668a7015092SGreg Clayton     LoadSubCommand ("select", CommandObjectSP (new CommandObjectFrameSelect (interpreter)));
669a7015092SGreg Clayton     LoadSubCommand ("variable", CommandObjectSP (new CommandObjectFrameVariable (interpreter)));
67030fdc8d8SChris Lattner }
67130fdc8d8SChris Lattner 
67230fdc8d8SChris Lattner CommandObjectMultiwordFrame::~CommandObjectMultiwordFrame ()
67330fdc8d8SChris Lattner {
67430fdc8d8SChris Lattner }
67530fdc8d8SChris Lattner 
676