1 //===-- OptionGroupVariable.cpp -----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Interpreter/OptionGroupVariable.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Error.h"
17 #include "lldb/DataFormatters/DataVisualization.h"
18 #include "lldb/Interpreter/CommandInterpreter.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Utility/Utils.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 // if you add any options here, remember to update the counters in OptionGroupVariable::GetNumDefinitions()
26 static OptionDefinition
27 g_option_table[] =
28 {
29     { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-args",         'a', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone, "Omit function arguments."},
30     { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-locals",       'l', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone, "Omit local variables."},
31     { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "show-globals",    'g', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone, "Show the current frame source file global and static variables."},
32     { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "show-declaration",'c', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone, "Show variable declaration information (source file and line where the variable was declared)."},
33     { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "regex",           'r', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeRegularExpression, "The <variable-name> argument for name lookups are regular expressions."},
34     { LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "scope",           's', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone, "Show variable scope (argument, local, global, static)."},
35     { LLDB_OPT_SET_1,                  false, "summary",         'y', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeName, "Specify the summary that the variable output should use."},
36     { LLDB_OPT_SET_2,                  false, "summary-string",  'z', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeName, "Specify a summary string to use to format the variable output."},
37 };
38 
39 static Error
40 ValidateNamedSummary (const char* str, void*)
41 {
42     if (!str || !str[0])
43         return Error("must specify a valid named summary");
44     TypeSummaryImplSP summary_sp;
45     if (DataVisualization::NamedSummaryFormats::GetSummaryFormat(ConstString(str), summary_sp) == false)
46         return Error("must specify a valid named summary");
47     return Error();
48 }
49 
50 static Error
51 ValidateSummaryString (const char* str, void*)
52 {
53     if (!str || !str[0])
54         return Error("must specify a non-empty summary string");
55     return Error();
56 }
57 
58 OptionGroupVariable::OptionGroupVariable (bool show_frame_options) :
59     OptionGroup(),
60     include_frame_options (show_frame_options),
61     summary(ValidateNamedSummary),
62     summary_string(ValidateSummaryString)
63 {
64 }
65 
66 OptionGroupVariable::~OptionGroupVariable ()
67 {
68 }
69 
70 Error
71 OptionGroupVariable::SetOptionValue (CommandInterpreter &interpreter,
72                                      uint32_t option_idx,
73                                      const char *option_arg)
74 {
75     Error error;
76     if (!include_frame_options)
77         option_idx += 3;
78     const int short_option = g_option_table[option_idx].short_option;
79     switch (short_option)
80     {
81         case 'r':   use_regex    = true;  break;
82         case 'a':   show_args    = false; break;
83         case 'l':   show_locals  = false; break;
84         case 'g':   show_globals = true;  break;
85         case 'c':   show_decl    = true;  break;
86         case 's':
87             show_scope = true;
88             break;
89         case 'y':
90             error = summary.SetCurrentValue(option_arg);
91             break;
92         case 'z':
93             error = summary_string.SetCurrentValue(option_arg);
94             break;
95         default:
96             error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
97             break;
98     }
99 
100     return error;
101 }
102 
103 void
104 OptionGroupVariable::OptionParsingStarting (CommandInterpreter &interpreter)
105 {
106     show_args     = true;   // Frame option only
107     show_locals   = true;   // Frame option only
108     show_globals  = false;  // Frame option only
109     show_decl     = false;
110     use_regex     = false;
111     show_scope    = false;
112     summary.Clear();
113     summary_string.Clear();
114 }
115 
116 #define NUM_FRAME_OPTS 3
117 
118 const OptionDefinition*
119 OptionGroupVariable::GetDefinitions ()
120 {
121     // Show the "--no-args", "--no-locals" and "--show-globals"
122     // options if we are showing frame specific options
123     if (include_frame_options)
124         return g_option_table;
125 
126     // Skip the "--no-args", "--no-locals" and "--show-globals"
127     // options if we are not showing frame specific options (globals only)
128     return &g_option_table[NUM_FRAME_OPTS];
129 }
130 
131 uint32_t
132 OptionGroupVariable::GetNumDefinitions ()
133 {
134     // Count the "--no-args", "--no-locals" and "--show-globals"
135     // options if we are showing frame specific options.
136     if (include_frame_options)
137         return llvm::array_lengthof(g_option_table);
138     else
139         return llvm::array_lengthof(g_option_table) - NUM_FRAME_OPTS;
140 }
141 
142 
143