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
14de6bd243SJohnny Chen #include <string>
1530fdc8d8SChris Lattner // Other libraries and framework includes
1630fdc8d8SChris Lattner // Project includes
1701a67860SJohnny Chen #include "lldb/Breakpoint/Watchpoint.h"
180a976141SEnrico Granata #include "lldb/Core/DataVisualization.h"
1930fdc8d8SChris Lattner #include "lldb/Core/Debugger.h"
206d56d2ceSJim Ingham #include "lldb/Core/Module.h"
216d56d2ceSJim Ingham #include "lldb/Core/StreamFile.h"
22de6bd243SJohnny Chen #include "lldb/Core/StreamString.h"
2330fdc8d8SChris Lattner #include "lldb/Core/Timer.h"
246d56d2ceSJim Ingham #include "lldb/Core/Value.h"
256d56d2ceSJim Ingham #include "lldb/Core/ValueObject.h"
266d56d2ceSJim Ingham #include "lldb/Core/ValueObjectVariable.h"
277fb56d0aSGreg Clayton #include "lldb/Host/Host.h"
286d56d2ceSJim Ingham #include "lldb/Interpreter/Args.h"
2930fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h"
3030fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h"
316d56d2ceSJim Ingham #include "lldb/Interpreter/Options.h"
321deb7962SGreg Clayton #include "lldb/Interpreter/OptionGroupFormat.h"
332837b766SJim Ingham #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
34715c2365SGreg Clayton #include "lldb/Interpreter/OptionGroupVariable.h"
35b1d7529eSJohnny Chen #include "lldb/Interpreter/OptionGroupWatchpoint.h"
366d56d2ceSJim Ingham #include "lldb/Symbol/ClangASTType.h"
376d56d2ceSJim Ingham #include "lldb/Symbol/ClangASTContext.h"
386d56d2ceSJim Ingham #include "lldb/Symbol/ObjectFile.h"
396d56d2ceSJim Ingham #include "lldb/Symbol/SymbolContext.h"
406d56d2ceSJim Ingham #include "lldb/Symbol/Type.h"
416d56d2ceSJim Ingham #include "lldb/Symbol/Variable.h"
426d56d2ceSJim Ingham #include "lldb/Symbol/VariableList.h"
4330fdc8d8SChris Lattner #include "lldb/Target/Process.h"
4430fdc8d8SChris Lattner #include "lldb/Target/StackFrame.h"
4530fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
466d56d2ceSJim Ingham #include "lldb/Target/Target.h"
4730fdc8d8SChris Lattner 
4830fdc8d8SChris Lattner using namespace lldb;
4930fdc8d8SChris Lattner using namespace lldb_private;
5030fdc8d8SChris Lattner 
5130fdc8d8SChris Lattner #pragma mark CommandObjectFrameInfo
5230fdc8d8SChris Lattner 
5330fdc8d8SChris Lattner //-------------------------------------------------------------------------
5430fdc8d8SChris Lattner // CommandObjectFrameInfo
5530fdc8d8SChris Lattner //-------------------------------------------------------------------------
5630fdc8d8SChris Lattner 
5730fdc8d8SChris Lattner class CommandObjectFrameInfo : public CommandObject
5830fdc8d8SChris Lattner {
5930fdc8d8SChris Lattner public:
6030fdc8d8SChris Lattner 
61a7015092SGreg Clayton     CommandObjectFrameInfo (CommandInterpreter &interpreter) :
62a7015092SGreg Clayton         CommandObject (interpreter,
63a7015092SGreg Clayton                        "frame info",
64e3d26315SCaroline Tice                        "List information about the currently selected frame in the current thread.",
6530fdc8d8SChris Lattner                        "frame info",
6630fdc8d8SChris Lattner                        eFlagProcessMustBeLaunched | eFlagProcessMustBePaused)
6730fdc8d8SChris Lattner     {
6830fdc8d8SChris Lattner     }
6930fdc8d8SChris Lattner 
7030fdc8d8SChris Lattner     ~CommandObjectFrameInfo ()
7130fdc8d8SChris Lattner     {
7230fdc8d8SChris Lattner     }
7330fdc8d8SChris Lattner 
7430fdc8d8SChris Lattner     bool
75a7015092SGreg Clayton     Execute (Args& command,
7630fdc8d8SChris Lattner              CommandReturnObject &result)
7730fdc8d8SChris Lattner     {
788b82f087SGreg Clayton         ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
79c14ee32dSGreg Clayton         StackFrame *frame = exe_ctx.GetFramePtr();
80c14ee32dSGreg Clayton         if (frame)
8130fdc8d8SChris Lattner         {
82c14ee32dSGreg Clayton             frame->DumpUsingSettingsFormat (&result.GetOutputStream());
8330fdc8d8SChris Lattner             result.SetStatus (eReturnStatusSuccessFinishResult);
8430fdc8d8SChris Lattner         }
8530fdc8d8SChris Lattner         else
8630fdc8d8SChris Lattner         {
8730fdc8d8SChris Lattner             result.AppendError ("no current frame");
8830fdc8d8SChris Lattner             result.SetStatus (eReturnStatusFailed);
8930fdc8d8SChris Lattner         }
9030fdc8d8SChris Lattner         return result.Succeeded();
9130fdc8d8SChris Lattner     }
9230fdc8d8SChris Lattner };
9330fdc8d8SChris Lattner 
9430fdc8d8SChris Lattner #pragma mark CommandObjectFrameSelect
9530fdc8d8SChris Lattner 
9630fdc8d8SChris Lattner //-------------------------------------------------------------------------
9730fdc8d8SChris Lattner // CommandObjectFrameSelect
9830fdc8d8SChris Lattner //-------------------------------------------------------------------------
9930fdc8d8SChris Lattner 
10030fdc8d8SChris Lattner class CommandObjectFrameSelect : public CommandObject
10130fdc8d8SChris Lattner {
10230fdc8d8SChris Lattner public:
10330fdc8d8SChris Lattner 
104864174e1SGreg Clayton    class CommandOptions : public Options
105864174e1SGreg Clayton     {
106864174e1SGreg Clayton     public:
107864174e1SGreg Clayton 
108eb0103f2SGreg Clayton         CommandOptions (CommandInterpreter &interpreter) :
109f16066e8SJohnny Chen             Options(interpreter)
110864174e1SGreg Clayton         {
111f6b8b581SGreg Clayton             OptionParsingStarting ();
112864174e1SGreg Clayton         }
113864174e1SGreg Clayton 
114864174e1SGreg Clayton         virtual
115864174e1SGreg Clayton         ~CommandOptions ()
116864174e1SGreg Clayton         {
117864174e1SGreg Clayton         }
118864174e1SGreg Clayton 
119864174e1SGreg Clayton         virtual Error
120f6b8b581SGreg Clayton         SetOptionValue (uint32_t option_idx, const char *option_arg)
121864174e1SGreg Clayton         {
122864174e1SGreg Clayton             Error error;
123864174e1SGreg Clayton             bool success = false;
124864174e1SGreg Clayton             char short_option = (char) m_getopt_table[option_idx].val;
125864174e1SGreg Clayton             switch (short_option)
126864174e1SGreg Clayton             {
127864174e1SGreg Clayton             case 'r':
128864174e1SGreg Clayton                 relative_frame_offset = Args::StringToSInt32 (option_arg, INT32_MIN, 0, &success);
129864174e1SGreg Clayton                 if (!success)
130*86edbf41SGreg Clayton                     error.SetErrorStringWithFormat ("invalid frame offset argument '%s'", option_arg);
131864174e1SGreg Clayton                 break;
132864174e1SGreg Clayton 
133864174e1SGreg Clayton             default:
134*86edbf41SGreg Clayton                 error.SetErrorStringWithFormat ("invalid short option character '%c'", short_option);
135864174e1SGreg Clayton                 break;
136864174e1SGreg Clayton             }
137864174e1SGreg Clayton 
138864174e1SGreg Clayton             return error;
139864174e1SGreg Clayton         }
140864174e1SGreg Clayton 
141864174e1SGreg Clayton         void
142f6b8b581SGreg Clayton         OptionParsingStarting ()
143864174e1SGreg Clayton         {
144864174e1SGreg Clayton             relative_frame_offset = INT32_MIN;
145864174e1SGreg Clayton         }
146864174e1SGreg Clayton 
147e0d378b3SGreg Clayton         const OptionDefinition*
148864174e1SGreg Clayton         GetDefinitions ()
149864174e1SGreg Clayton         {
150864174e1SGreg Clayton             return g_option_table;
151864174e1SGreg Clayton         }
152864174e1SGreg Clayton 
153864174e1SGreg Clayton         // Options table: Required for subclasses of Options.
154864174e1SGreg Clayton 
155e0d378b3SGreg Clayton         static OptionDefinition g_option_table[];
156864174e1SGreg Clayton         int32_t relative_frame_offset;
157864174e1SGreg Clayton     };
158864174e1SGreg Clayton 
159a7015092SGreg Clayton     CommandObjectFrameSelect (CommandInterpreter &interpreter) :
160a7015092SGreg Clayton         CommandObject (interpreter,
161a7015092SGreg Clayton                        "frame select",
162e3d26315SCaroline Tice                        "Select a frame by index from within the current thread and make it the current frame.",
163405fe67fSCaroline Tice                        NULL,
164eb0103f2SGreg Clayton                        eFlagProcessMustBeLaunched | eFlagProcessMustBePaused),
165eb0103f2SGreg Clayton         m_options (interpreter)
16630fdc8d8SChris Lattner     {
167405fe67fSCaroline Tice         CommandArgumentEntry arg;
168405fe67fSCaroline Tice         CommandArgumentData index_arg;
169405fe67fSCaroline Tice 
170405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
171405fe67fSCaroline Tice         index_arg.arg_type = eArgTypeFrameIndex;
172864174e1SGreg Clayton         index_arg.arg_repetition = eArgRepeatOptional;
173405fe67fSCaroline Tice 
174405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
175405fe67fSCaroline Tice         arg.push_back (index_arg);
176405fe67fSCaroline Tice 
177405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
178405fe67fSCaroline Tice         m_arguments.push_back (arg);
17930fdc8d8SChris Lattner     }
18030fdc8d8SChris Lattner 
18130fdc8d8SChris Lattner     ~CommandObjectFrameSelect ()
18230fdc8d8SChris Lattner     {
18330fdc8d8SChris Lattner     }
18430fdc8d8SChris Lattner 
185864174e1SGreg Clayton     virtual
186864174e1SGreg Clayton     Options *
187864174e1SGreg Clayton     GetOptions ()
188864174e1SGreg Clayton     {
189864174e1SGreg Clayton         return &m_options;
190864174e1SGreg Clayton     }
191864174e1SGreg Clayton 
192864174e1SGreg Clayton 
19330fdc8d8SChris Lattner     bool
194a7015092SGreg Clayton     Execute (Args& command,
19530fdc8d8SChris Lattner              CommandReturnObject &result)
19630fdc8d8SChris Lattner     {
1978b82f087SGreg Clayton         ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
198c14ee32dSGreg Clayton         Thread *thread = exe_ctx.GetThreadPtr();
199c14ee32dSGreg Clayton         if (thread)
20030fdc8d8SChris Lattner         {
201c14ee32dSGreg Clayton             const uint32_t num_frames = thread->GetStackFrameCount();
202864174e1SGreg Clayton             uint32_t frame_idx = UINT32_MAX;
203864174e1SGreg Clayton             if (m_options.relative_frame_offset != INT32_MIN)
204864174e1SGreg Clayton             {
205864174e1SGreg Clayton                 // The one and only argument is a signed relative frame index
206c14ee32dSGreg Clayton                 frame_idx = thread->GetSelectedFrameIndex ();
207864174e1SGreg Clayton                 if (frame_idx == UINT32_MAX)
208864174e1SGreg Clayton                     frame_idx = 0;
209864174e1SGreg Clayton 
210864174e1SGreg Clayton                 if (m_options.relative_frame_offset < 0)
211864174e1SGreg Clayton                 {
212864174e1SGreg Clayton                     if (frame_idx >= -m_options.relative_frame_offset)
213864174e1SGreg Clayton                         frame_idx += m_options.relative_frame_offset;
214864174e1SGreg Clayton                     else
215213b4546SJim Ingham                     {
216213b4546SJim Ingham                         if (frame_idx == 0)
217213b4546SJim Ingham                         {
218213b4546SJim Ingham                             //If you are already at the bottom of the stack, then just warn and don't reset the frame.
219213b4546SJim Ingham                             result.AppendError("Already at the bottom of the stack");
220213b4546SJim Ingham                             result.SetStatus(eReturnStatusFailed);
221213b4546SJim Ingham                             return false;
222213b4546SJim Ingham                         }
223213b4546SJim Ingham                         else
224864174e1SGreg Clayton                             frame_idx = 0;
225864174e1SGreg Clayton                     }
226213b4546SJim Ingham                 }
227864174e1SGreg Clayton                 else if (m_options.relative_frame_offset > 0)
228864174e1SGreg Clayton                 {
229864174e1SGreg Clayton                     if (num_frames - frame_idx > m_options.relative_frame_offset)
230864174e1SGreg Clayton                         frame_idx += m_options.relative_frame_offset;
231864174e1SGreg Clayton                     else
232213b4546SJim Ingham                     {
233213b4546SJim Ingham                         if (frame_idx == num_frames - 1)
234213b4546SJim Ingham                         {
235213b4546SJim Ingham                             //If we are already at the top of the stack, just warn and don't reset the frame.
236213b4546SJim Ingham                             result.AppendError("Already at the top of the stack");
237213b4546SJim Ingham                             result.SetStatus(eReturnStatusFailed);
238213b4546SJim Ingham                             return false;
239213b4546SJim Ingham                         }
240213b4546SJim Ingham                         else
241864174e1SGreg Clayton                             frame_idx = num_frames - 1;
242864174e1SGreg Clayton                     }
243864174e1SGreg Clayton                 }
244213b4546SJim Ingham             }
245864174e1SGreg Clayton             else
246864174e1SGreg Clayton             {
24730fdc8d8SChris Lattner                 if (command.GetArgumentCount() == 1)
24830fdc8d8SChris Lattner                 {
24930fdc8d8SChris Lattner                     const char *frame_idx_cstr = command.GetArgumentAtIndex(0);
250864174e1SGreg Clayton                     frame_idx = Args::StringToUInt32 (frame_idx_cstr, UINT32_MAX, 0);
251864174e1SGreg Clayton                 }
252f56c0137SJason Molenda                 else if (command.GetArgumentCount() == 0)
25382d4a2b9SJason Molenda                 {
25482d4a2b9SJason Molenda                     frame_idx = thread->GetSelectedFrameIndex ();
25582d4a2b9SJason Molenda                     if (frame_idx == UINT32_MAX)
256f56c0137SJason Molenda                     {
25782d4a2b9SJason Molenda                         frame_idx = 0;
25882d4a2b9SJason Molenda                     }
259f56c0137SJason Molenda                 }
260864174e1SGreg Clayton                 else
261864174e1SGreg Clayton                 {
262864174e1SGreg Clayton                     result.AppendError ("invalid arguments.\n");
263eb0103f2SGreg Clayton                     m_options.GenerateOptionUsage (result.GetErrorStream(), this);
264864174e1SGreg Clayton                 }
265864174e1SGreg Clayton             }
26630fdc8d8SChris Lattner 
26730fdc8d8SChris Lattner             if (frame_idx < num_frames)
26830fdc8d8SChris Lattner             {
269c14ee32dSGreg Clayton                 thread->SetSelectedFrameByIndex (frame_idx);
270c14ee32dSGreg Clayton                 exe_ctx.SetFrameSP(thread->GetSelectedFrame ());
271c14ee32dSGreg Clayton                 StackFrame *frame = exe_ctx.GetFramePtr();
272c14ee32dSGreg Clayton                 if (frame)
27330fdc8d8SChris Lattner                 {
274e40e4218SJim Ingham                     bool already_shown = false;
275c14ee32dSGreg Clayton                     SymbolContext frame_sc(frame->GetSymbolContext(eSymbolContextLineEntry));
276daccaa9eSCaroline Tice                     if (m_interpreter.GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
277e40e4218SJim Ingham                     {
278e40e4218SJim Ingham                         already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
279e40e4218SJim Ingham                     }
280e40e4218SJim Ingham 
2817260f620SGreg Clayton                     bool show_frame_info = true;
2827260f620SGreg Clayton                     bool show_source = !already_shown;
2837260f620SGreg Clayton                     uint32_t source_lines_before = 3;
2847260f620SGreg Clayton                     uint32_t source_lines_after = 3;
285c14ee32dSGreg Clayton                     if (frame->GetStatus (result.GetOutputStream(),
2867260f620SGreg Clayton                                           show_frame_info,
2877260f620SGreg Clayton                                           show_source,
2887260f620SGreg Clayton                                           source_lines_before,
2897260f620SGreg Clayton                                           source_lines_after))
29030fdc8d8SChris Lattner                     {
29130fdc8d8SChris Lattner                         result.SetStatus (eReturnStatusSuccessFinishResult);
29230fdc8d8SChris Lattner                         return result.Succeeded();
29330fdc8d8SChris Lattner                     }
29430fdc8d8SChris Lattner                 }
29530fdc8d8SChris Lattner             }
29630fdc8d8SChris Lattner             result.AppendErrorWithFormat ("Frame index (%u) out of range.\n", frame_idx);
29730fdc8d8SChris Lattner         }
29830fdc8d8SChris Lattner         else
29930fdc8d8SChris Lattner         {
30030fdc8d8SChris Lattner             result.AppendError ("no current thread");
30130fdc8d8SChris Lattner         }
30230fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
30330fdc8d8SChris Lattner         return false;
30430fdc8d8SChris Lattner     }
305864174e1SGreg Clayton protected:
306864174e1SGreg Clayton 
307864174e1SGreg Clayton     CommandOptions m_options;
308864174e1SGreg Clayton };
309864174e1SGreg Clayton 
310e0d378b3SGreg Clayton OptionDefinition
311864174e1SGreg Clayton CommandObjectFrameSelect::CommandOptions::g_option_table[] =
312864174e1SGreg Clayton {
313864174e1SGreg Clayton { LLDB_OPT_SET_1, false, "relative", 'r', required_argument, NULL, 0, eArgTypeOffset, "A relative frame index offset from the current frame index."},
314864174e1SGreg Clayton { 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
31530fdc8d8SChris Lattner };
31630fdc8d8SChris Lattner 
3176d56d2ceSJim Ingham #pragma mark CommandObjectFrameVariable
3186d56d2ceSJim Ingham //----------------------------------------------------------------------
3196d56d2ceSJim Ingham // List images with associated information
3206d56d2ceSJim Ingham //----------------------------------------------------------------------
3216d56d2ceSJim Ingham class CommandObjectFrameVariable : public CommandObject
3226d56d2ceSJim Ingham {
3236d56d2ceSJim Ingham public:
3246d56d2ceSJim Ingham 
325a7015092SGreg Clayton     CommandObjectFrameVariable (CommandInterpreter &interpreter) :
326a7015092SGreg Clayton         CommandObject (interpreter,
3276d56d2ceSJim Ingham                        "frame variable",
328ed8a705cSGreg Clayton                        "Show frame variables. All argument and local variables "
329ed8a705cSGreg Clayton                        "that are in scope will be shown when no arguments are given. "
330ed8a705cSGreg Clayton                        "If any arguments are specified, they can be names of "
331ed8a705cSGreg Clayton                        "argument, local, file static and file global variables. "
332ed8a705cSGreg Clayton                        "Children of aggregate variables can be specified such as "
333b1d7529eSJohnny Chen                        "'var->child.x'. "
334b1d7529eSJohnny Chen                        "You can choose to watch a variable with the '-w' option. "
335b1d7529eSJohnny Chen                        "Note that hardware resources for watching are often limited.",
336ff471a94SJim Ingham                        NULL,
337eb0103f2SGreg Clayton                        eFlagProcessMustBeLaunched | eFlagProcessMustBePaused),
3382837b766SJim Ingham         m_option_group (interpreter),
339715c2365SGreg Clayton         m_option_variable(true), // Include the frame specific options by passing "true"
3401deb7962SGreg Clayton         m_option_format (eFormatDefault),
341b1d7529eSJohnny Chen         m_option_watchpoint(),
3422837b766SJim Ingham         m_varobj_options()
3436d56d2ceSJim Ingham     {
344405fe67fSCaroline Tice         CommandArgumentEntry arg;
345405fe67fSCaroline Tice         CommandArgumentData var_name_arg;
346405fe67fSCaroline Tice 
347405fe67fSCaroline Tice         // Define the first (and only) variant of this arg.
348405fe67fSCaroline Tice         var_name_arg.arg_type = eArgTypeVarName;
349405fe67fSCaroline Tice         var_name_arg.arg_repetition = eArgRepeatStar;
350405fe67fSCaroline Tice 
351405fe67fSCaroline Tice         // There is only one variant this argument could be; put it into the argument entry.
352405fe67fSCaroline Tice         arg.push_back (var_name_arg);
353405fe67fSCaroline Tice 
354405fe67fSCaroline Tice         // Push the data for the first argument into the m_arguments vector.
355405fe67fSCaroline Tice         m_arguments.push_back (arg);
3562837b766SJim Ingham 
357715c2365SGreg Clayton         m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
3581deb7962SGreg Clayton         m_option_group.Append (&m_option_format, OptionGroupFormat::OPTION_GROUP_FORMAT, LLDB_OPT_SET_1);
359b1d7529eSJohnny Chen         m_option_group.Append (&m_option_watchpoint, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
3602837b766SJim Ingham         m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
3612837b766SJim Ingham         m_option_group.Finalize();
3626d56d2ceSJim Ingham     }
3636d56d2ceSJim Ingham 
3646d56d2ceSJim Ingham     virtual
3656d56d2ceSJim Ingham     ~CommandObjectFrameVariable ()
3666d56d2ceSJim Ingham     {
3676d56d2ceSJim Ingham     }
3686d56d2ceSJim Ingham 
3696d56d2ceSJim Ingham     virtual
3706d56d2ceSJim Ingham     Options *
3716d56d2ceSJim Ingham     GetOptions ()
3726d56d2ceSJim Ingham     {
3732837b766SJim Ingham         return &m_option_group;
3746d56d2ceSJim Ingham     }
3756d56d2ceSJim Ingham 
3766d56d2ceSJim Ingham 
3776d56d2ceSJim Ingham     virtual bool
3786d56d2ceSJim Ingham     Execute
3796d56d2ceSJim Ingham     (
3806d56d2ceSJim Ingham         Args& command,
3816d56d2ceSJim Ingham         CommandReturnObject &result
3826d56d2ceSJim Ingham     )
3836d56d2ceSJim Ingham     {
3848b82f087SGreg Clayton         ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
385c14ee32dSGreg Clayton         StackFrame *frame = exe_ctx.GetFramePtr();
386c14ee32dSGreg Clayton         if (frame == NULL)
3876d56d2ceSJim Ingham         {
388340b2baaSGreg Clayton             result.AppendError ("you must be stopped in a valid stack frame to view frame variables.");
3896d56d2ceSJim Ingham             result.SetStatus (eReturnStatusFailed);
3906d56d2ceSJim Ingham             return false;
3916d56d2ceSJim Ingham         }
3921e49e5e7SJohnny Chen 
393a134cc1bSGreg Clayton         Stream &s = result.GetOutputStream();
3946d56d2ceSJim Ingham 
395a134cc1bSGreg Clayton         bool get_file_globals = true;
396650543f9SJim Ingham 
397650543f9SJim Ingham         // Be careful about the stack frame, if any summary formatter runs code, it might clear the StackFrameList
398650543f9SJim Ingham         // for the thread.  So hold onto a shared pointer to the frame so it stays alive.
399650543f9SJim Ingham 
400c14ee32dSGreg Clayton         VariableList *variable_list = frame->GetVariableList (get_file_globals);
401a134cc1bSGreg Clayton 
4026d56d2ceSJim Ingham         VariableSP var_sp;
4036d56d2ceSJim Ingham         ValueObjectSP valobj_sp;
40478a685aaSJim Ingham 
4056d56d2ceSJim Ingham         const char *name_cstr = NULL;
4066d56d2ceSJim Ingham         size_t idx;
4076d56d2ceSJim Ingham 
408f9fa6ee5SEnrico Granata         SummaryFormatSP summary_format_sp;
409f9fa6ee5SEnrico Granata         if (!m_option_variable.summary.empty())
41078d0638bSEnrico Granata             DataVisualization::NamedSummaryFormats::GetSummaryFormat(ConstString(m_option_variable.summary.c_str()), summary_format_sp);
411f9fa6ee5SEnrico Granata 
412379447a7SEnrico Granata         ValueObject::DumpValueObjectOptions options;
413379447a7SEnrico Granata 
414379447a7SEnrico Granata         options.SetPointerDepth(m_varobj_options.ptr_depth)
415379447a7SEnrico Granata             .SetMaximumDepth(m_varobj_options.max_depth)
416379447a7SEnrico Granata             .SetShowTypes(m_varobj_options.show_types)
417379447a7SEnrico Granata             .SetShowLocation(m_varobj_options.show_location)
418379447a7SEnrico Granata             .SetUseObjectiveC(m_varobj_options.use_objc)
419379447a7SEnrico Granata             .SetUseDynamicType(m_varobj_options.use_dynamic)
420379447a7SEnrico Granata             .SetUseSyntheticValue((lldb::SyntheticValueType)m_varobj_options.use_synth)
421379447a7SEnrico Granata             .SetFlatOutput(m_varobj_options.flat_output)
422379447a7SEnrico Granata             .SetOmitSummaryDepth(m_varobj_options.no_summary_depth)
423379447a7SEnrico Granata             .SetIgnoreCap(m_varobj_options.ignore_cap);
424379447a7SEnrico Granata 
425379447a7SEnrico Granata         if (m_varobj_options.be_raw)
426379447a7SEnrico Granata             options.SetRawDisplay(true);
427379447a7SEnrico Granata 
428715c2365SGreg Clayton         if (variable_list)
4299df87c17SGreg Clayton         {
4303a9838c0SJohnny Chen             // If watching a variable, there are certain restrictions to be followed.
4313a9838c0SJohnny Chen             if (m_option_watchpoint.watch_variable)
4323a9838c0SJohnny Chen             {
4333a9838c0SJohnny Chen                 if (command.GetArgumentCount() != 1) {
4343a9838c0SJohnny Chen                     result.GetErrorStream().Printf("error: specify exactly one variable when using the '-w' option\n");
4353a9838c0SJohnny Chen                     result.SetStatus(eReturnStatusFailed);
4363a9838c0SJohnny Chen                     return false;
4373a9838c0SJohnny Chen                 } else if (m_option_variable.use_regex) {
4383a9838c0SJohnny Chen                     result.GetErrorStream().Printf("error: specify your variable name exactly (no regex) when using the '-w' option\n");
4393a9838c0SJohnny Chen                     result.SetStatus(eReturnStatusFailed);
4403a9838c0SJohnny Chen                     return false;
4413a9838c0SJohnny Chen                 }
4423a9838c0SJohnny Chen 
4433a9838c0SJohnny Chen                 // Things have checked out ok...
444b62a3be1SJohnny Chen                 // m_option_watchpoint.watch_type specifies the type of watching.
4453a9838c0SJohnny Chen             }
4461deb7962SGreg Clayton 
4471deb7962SGreg Clayton             const Format format = m_option_format.GetFormat();
4481deb7962SGreg Clayton 
4499df87c17SGreg Clayton             if (command.GetArgumentCount() > 0)
4506d56d2ceSJim Ingham             {
45146747022SGreg Clayton                 VariableList regex_var_list;
45246747022SGreg Clayton 
4536d56d2ceSJim Ingham                 // If we have any args to the variable command, we will make
4546d56d2ceSJim Ingham                 // variable objects from them...
4556d56d2ceSJim Ingham                 for (idx = 0; (name_cstr = command.GetArgumentAtIndex(idx)) != NULL; ++idx)
4566d56d2ceSJim Ingham                 {
457715c2365SGreg Clayton                     if (m_option_variable.use_regex)
45846747022SGreg Clayton                     {
45946747022SGreg Clayton                         const uint32_t regex_start_index = regex_var_list.GetSize();
46046747022SGreg Clayton                         RegularExpression regex (name_cstr);
46146747022SGreg Clayton                         if (regex.Compile(name_cstr))
46246747022SGreg Clayton                         {
46346747022SGreg Clayton                             size_t num_matches = 0;
46478a685aaSJim Ingham                             const size_t num_new_regex_vars = variable_list->AppendVariablesIfUnique(regex,
46578a685aaSJim Ingham                                                                                                      regex_var_list,
46678a685aaSJim Ingham                                                                                                      num_matches);
46746747022SGreg Clayton                             if (num_new_regex_vars > 0)
46846747022SGreg Clayton                             {
46946747022SGreg Clayton                                 for (uint32_t regex_idx = regex_start_index, end_index = regex_var_list.GetSize();
47046747022SGreg Clayton                                      regex_idx < end_index;
47146747022SGreg Clayton                                      ++regex_idx)
47246747022SGreg Clayton                                 {
47346747022SGreg Clayton                                     var_sp = regex_var_list.GetVariableAtIndex (regex_idx);
47446747022SGreg Clayton                                     if (var_sp)
47546747022SGreg Clayton                                     {
476c14ee32dSGreg Clayton                                         valobj_sp = frame->GetValueObjectForFrameVariable (var_sp, m_varobj_options.use_dynamic);
47746747022SGreg Clayton                                         if (valobj_sp)
47846747022SGreg Clayton                                         {
4791deb7962SGreg Clayton                                             if (format != eFormatDefault)
4801deb7962SGreg Clayton                                                 valobj_sp->SetFormat (format);
481ded470d3SGreg Clayton 
482715c2365SGreg Clayton                                             if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
48346747022SGreg Clayton                                             {
48445ba8543SGreg Clayton                                                 bool show_fullpaths = false;
48545ba8543SGreg Clayton                                                 bool show_module = true;
48645ba8543SGreg Clayton                                                 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module))
48746747022SGreg Clayton                                                     s.PutCString (": ");
48846747022SGreg Clayton                                             }
489f9fa6ee5SEnrico Granata                                             if (summary_format_sp)
490f9fa6ee5SEnrico Granata                                                 valobj_sp->SetCustomSummaryFormat(summary_format_sp);
49146747022SGreg Clayton                                             ValueObject::DumpValueObject (result.GetOutputStream(),
49246747022SGreg Clayton                                                                           valobj_sp.get(),
493379447a7SEnrico Granata                                                                           options);
49446747022SGreg Clayton                                         }
49546747022SGreg Clayton                                     }
49646747022SGreg Clayton                                 }
49746747022SGreg Clayton                             }
49846747022SGreg Clayton                             else if (num_matches == 0)
49946747022SGreg Clayton                             {
50046747022SGreg Clayton                                 result.GetErrorStream().Printf ("error: no variables matched the regular expression '%s'.\n", name_cstr);
50146747022SGreg Clayton                             }
50246747022SGreg Clayton                         }
50346747022SGreg Clayton                         else
50446747022SGreg Clayton                         {
50546747022SGreg Clayton                             char regex_error[1024];
50646747022SGreg Clayton                             if (regex.GetErrorAsCString(regex_error, sizeof(regex_error)))
50746747022SGreg Clayton                                 result.GetErrorStream().Printf ("error: %s\n", regex_error);
50846747022SGreg Clayton                             else
50946747022SGreg Clayton                                 result.GetErrorStream().Printf ("error: unkown regex error when compiling '%s'\n", name_cstr);
51046747022SGreg Clayton                         }
51146747022SGreg Clayton                     }
512887062aeSJohnny Chen                     else // No regex, either exact variable names or variable expressions.
51346747022SGreg Clayton                     {
51454979cddSGreg Clayton                         Error error;
51578a685aaSJim Ingham                         uint32_t expr_path_options = StackFrame::eExpressionPathOptionCheckPtrVsMember;
5162837b766SJim Ingham                         lldb::VariableSP var_sp;
517c14ee32dSGreg Clayton                         valobj_sp = frame->GetValueForVariableExpressionPath (name_cstr,
5182837b766SJim Ingham                                                                               m_varobj_options.use_dynamic,
5192837b766SJim Ingham                                                                               expr_path_options,
5202837b766SJim Ingham                                                                               var_sp,
5212837b766SJim Ingham                                                                               error);
5226d56d2ceSJim Ingham                         if (valobj_sp)
5236d56d2ceSJim Ingham                         {
5241deb7962SGreg Clayton                             if (format != eFormatDefault)
5251deb7962SGreg Clayton                                 valobj_sp->SetFormat (format);
526715c2365SGreg Clayton                             if (m_option_variable.show_decl && var_sp && var_sp->GetDeclaration ().GetFile())
527a134cc1bSGreg Clayton                             {
528a134cc1bSGreg Clayton                                 var_sp->GetDeclaration ().DumpStopContext (&s, false);
529a134cc1bSGreg Clayton                                 s.PutCString (": ");
530a134cc1bSGreg Clayton                             }
531f9fa6ee5SEnrico Granata                             if (summary_format_sp)
532f9fa6ee5SEnrico Granata                                 valobj_sp->SetCustomSummaryFormat(summary_format_sp);
533887062aeSJohnny Chen 
534887062aeSJohnny Chen                             Stream &output_stream = result.GetOutputStream();
535887062aeSJohnny Chen                             ValueObject::DumpValueObject (output_stream,
536a134cc1bSGreg Clayton                                                           valobj_sp.get(),
53783c5cd9dSGreg Clayton                                                           valobj_sp->GetParent() ? name_cstr : NULL,
538379447a7SEnrico Granata                                                           options);
539887062aeSJohnny Chen                             // Process watchpoint if necessary.
540887062aeSJohnny Chen                             if (m_option_watchpoint.watch_variable)
541887062aeSJohnny Chen                             {
5422fd89a09SJohnny Chen                                 AddressType addr_type;
543b62a3be1SJohnny Chen                                 lldb::addr_t addr = 0;
544887062aeSJohnny Chen                                 size_t size = 0;
545b62a3be1SJohnny Chen                                 if (m_option_watchpoint.watch_size == 0) {
546b62a3be1SJohnny Chen                                     addr = valobj_sp->GetAddressOf(false, &addr_type);
5472fd89a09SJohnny Chen                                     if (addr_type == eAddressTypeLoad) {
5482fd89a09SJohnny Chen                                         // We're in business.
5492fd89a09SJohnny Chen                                         // Find out the size of this variable.
5502fd89a09SJohnny Chen                                         size = valobj_sp->GetByteSize();
5512fd89a09SJohnny Chen                                     }
552b62a3be1SJohnny Chen                                 } else {
553b62a3be1SJohnny Chen                                     // The '-xsize'/'-x' option means to treat the value object as
554b62a3be1SJohnny Chen                                     // a pointer and to watch the pointee with the specified size.
555b62a3be1SJohnny Chen                                     addr = valobj_sp->GetValueAsUnsigned(0);
556b62a3be1SJohnny Chen                                     size = m_option_watchpoint.watch_size;
557b62a3be1SJohnny Chen                                 }
558887062aeSJohnny Chen                                 uint32_t watch_type = m_option_watchpoint.watch_type;
55901a67860SJohnny Chen                                 Watchpoint *wp = exe_ctx.GetTargetRef().CreateWatchpoint(addr, size, watch_type).get();
56001a67860SJohnny Chen                                 if (wp)
561887062aeSJohnny Chen                                 {
562de6bd243SJohnny Chen                                     if (var_sp && var_sp->GetDeclaration().GetFile())
563de6bd243SJohnny Chen                                     {
564de6bd243SJohnny Chen                                         StreamString ss;
56518d7b950SJohnny Chen                                         // True to show fullpath for declaration file.
566de6bd243SJohnny Chen                                         var_sp->GetDeclaration().DumpStopContext(&ss, true);
56701a67860SJohnny Chen                                         wp->SetDeclInfo(ss.GetString());
568de6bd243SJohnny Chen                                     }
569de6bd243SJohnny Chen                                     StreamString ss;
570887062aeSJohnny Chen                                     output_stream.Printf("Watchpoint created: ");
57101a67860SJohnny Chen                                     wp->GetDescription(&output_stream, lldb::eDescriptionLevelFull);
572887062aeSJohnny Chen                                     output_stream.EOL();
573887062aeSJohnny Chen                                     result.SetStatus(eReturnStatusSuccessFinishResult);
574887062aeSJohnny Chen                                 }
575887062aeSJohnny Chen                                 else
576887062aeSJohnny Chen                                 {
577887062aeSJohnny Chen                                     result.AppendErrorWithFormat("Watchpoint creation failed.\n");
578887062aeSJohnny Chen                                     result.SetStatus(eReturnStatusFailed);
579887062aeSJohnny Chen                                 }
58001a67860SJohnny Chen                                 return (wp != NULL);
581887062aeSJohnny Chen                             }
5826d56d2ceSJim Ingham                         }
5836d56d2ceSJim Ingham                         else
5846d56d2ceSJim Ingham                         {
58554979cddSGreg Clayton                             const char *error_cstr = error.AsCString(NULL);
58654979cddSGreg Clayton                             if (error_cstr)
58754979cddSGreg Clayton                                 result.GetErrorStream().Printf("error: %s\n", error_cstr);
58854979cddSGreg Clayton                             else
58954979cddSGreg Clayton                                 result.GetErrorStream().Printf ("error: unable to find any variable expression path that matches '%s'\n", name_cstr);
5906d56d2ceSJim Ingham                         }
5916d56d2ceSJim Ingham                     }
5926d56d2ceSJim Ingham                 }
59346747022SGreg Clayton             }
594926d086eSJohnny Chen             else // No command arg specified.  Use variable_list, instead.
5956d56d2ceSJim Ingham             {
596a134cc1bSGreg Clayton                 const uint32_t num_variables = variable_list->GetSize();
5976d56d2ceSJim Ingham                 if (num_variables > 0)
5986d56d2ceSJim Ingham                 {
5996d56d2ceSJim Ingham                     for (uint32_t i=0; i<num_variables; i++)
6006d56d2ceSJim Ingham                     {
6011a65ae11SGreg Clayton                         var_sp = variable_list->GetVariableAtIndex(i);
6026d56d2ceSJim Ingham                         bool dump_variable = true;
603a134cc1bSGreg Clayton                         switch (var_sp->GetScope())
6046d56d2ceSJim Ingham                         {
6056d56d2ceSJim Ingham                             case eValueTypeVariableGlobal:
606715c2365SGreg Clayton                                 dump_variable = m_option_variable.show_globals;
607715c2365SGreg Clayton                                 if (dump_variable && m_option_variable.show_scope)
608a134cc1bSGreg Clayton                                     s.PutCString("GLOBAL: ");
6096d56d2ceSJim Ingham                                 break;
6106d56d2ceSJim Ingham 
6116d56d2ceSJim Ingham                             case eValueTypeVariableStatic:
612715c2365SGreg Clayton                                 dump_variable = m_option_variable.show_globals;
613715c2365SGreg Clayton                                 if (dump_variable && m_option_variable.show_scope)
614a134cc1bSGreg Clayton                                     s.PutCString("STATIC: ");
6156d56d2ceSJim Ingham                                 break;
6166d56d2ceSJim Ingham 
6176d56d2ceSJim Ingham                             case eValueTypeVariableArgument:
618715c2365SGreg Clayton                                 dump_variable = m_option_variable.show_args;
619715c2365SGreg Clayton                                 if (dump_variable && m_option_variable.show_scope)
620a134cc1bSGreg Clayton                                     s.PutCString("   ARG: ");
6216d56d2ceSJim Ingham                                 break;
6226d56d2ceSJim Ingham 
6236d56d2ceSJim Ingham                             case eValueTypeVariableLocal:
624715c2365SGreg Clayton                                 dump_variable = m_option_variable.show_locals;
625715c2365SGreg Clayton                                 if (dump_variable && m_option_variable.show_scope)
626a134cc1bSGreg Clayton                                     s.PutCString(" LOCAL: ");
6276d56d2ceSJim Ingham                                 break;
6286d56d2ceSJim Ingham 
6296d56d2ceSJim Ingham                             default:
6306d56d2ceSJim Ingham                                 break;
6316d56d2ceSJim Ingham                         }
6326d56d2ceSJim Ingham 
6336d56d2ceSJim Ingham                         if (dump_variable)
634a134cc1bSGreg Clayton                         {
635a134cc1bSGreg Clayton                             // Use the variable object code to make sure we are
636a134cc1bSGreg Clayton                             // using the same APIs as the the public API will be
637a134cc1bSGreg Clayton                             // using...
638c14ee32dSGreg Clayton                             valobj_sp = frame->GetValueObjectForFrameVariable (var_sp,
6392837b766SJim Ingham                                                                                m_varobj_options.use_dynamic);
640a134cc1bSGreg Clayton                             if (valobj_sp)
641a134cc1bSGreg Clayton                             {
6421deb7962SGreg Clayton                                 if (format != eFormatDefault)
6431deb7962SGreg Clayton                                     valobj_sp->SetFormat (format);
644ded470d3SGreg Clayton 
6456f00abd5SGreg Clayton                                 // When dumping all variables, don't print any variables
6466f00abd5SGreg Clayton                                 // that are not in scope to avoid extra unneeded output
6476035b67dSJim Ingham                                 if (valobj_sp->IsInScope ())
6486f00abd5SGreg Clayton                                 {
649715c2365SGreg Clayton                                     if (m_option_variable.show_decl && var_sp->GetDeclaration ().GetFile())
650a134cc1bSGreg Clayton                                     {
651a134cc1bSGreg Clayton                                         var_sp->GetDeclaration ().DumpStopContext (&s, false);
652a134cc1bSGreg Clayton                                         s.PutCString (": ");
653a134cc1bSGreg Clayton                                     }
654f9fa6ee5SEnrico Granata                                     if (summary_format_sp)
655f9fa6ee5SEnrico Granata                                         valobj_sp->SetCustomSummaryFormat(summary_format_sp);
6561d3afba3SGreg Clayton                                     ValueObject::DumpValueObject (result.GetOutputStream(),
657a134cc1bSGreg Clayton                                                                   valobj_sp.get(),
658a134cc1bSGreg Clayton                                                                   name_cstr,
659379447a7SEnrico Granata                                                                   options);
660a134cc1bSGreg Clayton                                 }
661a134cc1bSGreg Clayton                             }
6626d56d2ceSJim Ingham                         }
6636d56d2ceSJim Ingham                     }
6646d56d2ceSJim Ingham                 }
6656f00abd5SGreg Clayton             }
6666d56d2ceSJim Ingham             result.SetStatus (eReturnStatusSuccessFinishResult);
6676d56d2ceSJim Ingham         }
66861a80ba6SEnrico Granata 
66961a80ba6SEnrico Granata         if (m_interpreter.TruncationWarningNecessary())
67061a80ba6SEnrico Granata         {
67161a80ba6SEnrico Granata             result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(),
67261a80ba6SEnrico Granata                                             m_cmd_name.c_str());
67361a80ba6SEnrico Granata             m_interpreter.TruncationWarningGiven();
67461a80ba6SEnrico Granata         }
67561a80ba6SEnrico Granata 
6766d56d2ceSJim Ingham         return result.Succeeded();
6776d56d2ceSJim Ingham     }
6786d56d2ceSJim Ingham protected:
6796d56d2ceSJim Ingham 
6802837b766SJim Ingham     OptionGroupOptions m_option_group;
681715c2365SGreg Clayton     OptionGroupVariable m_option_variable;
6821deb7962SGreg Clayton     OptionGroupFormat m_option_format;
683b1d7529eSJohnny Chen     OptionGroupWatchpoint m_option_watchpoint;
6842837b766SJim Ingham     OptionGroupValueObjectDisplay m_varobj_options;
6856d56d2ceSJim Ingham };
6866d56d2ceSJim Ingham 
6872837b766SJim Ingham 
68830fdc8d8SChris Lattner #pragma mark CommandObjectMultiwordFrame
68930fdc8d8SChris Lattner 
69030fdc8d8SChris Lattner //-------------------------------------------------------------------------
69130fdc8d8SChris Lattner // CommandObjectMultiwordFrame
69230fdc8d8SChris Lattner //-------------------------------------------------------------------------
69330fdc8d8SChris Lattner 
6946611103cSGreg Clayton CommandObjectMultiwordFrame::CommandObjectMultiwordFrame (CommandInterpreter &interpreter) :
695a7015092SGreg Clayton     CommandObjectMultiword (interpreter,
696a7015092SGreg Clayton                             "frame",
69730fdc8d8SChris Lattner                             "A set of commands for operating on the current thread's frames.",
69830fdc8d8SChris Lattner                             "frame <subcommand> [<subcommand-options>]")
69930fdc8d8SChris Lattner {
700a7015092SGreg Clayton     LoadSubCommand ("info",   CommandObjectSP (new CommandObjectFrameInfo (interpreter)));
701a7015092SGreg Clayton     LoadSubCommand ("select", CommandObjectSP (new CommandObjectFrameSelect (interpreter)));
702a7015092SGreg Clayton     LoadSubCommand ("variable", CommandObjectSP (new CommandObjectFrameVariable (interpreter)));
70330fdc8d8SChris Lattner }
70430fdc8d8SChris Lattner 
70530fdc8d8SChris Lattner CommandObjectMultiwordFrame::~CommandObjectMultiwordFrame ()
70630fdc8d8SChris Lattner {
70730fdc8d8SChris Lattner }
70830fdc8d8SChris Lattner 
709