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/DataFormatters/DataVisualization.h"
17 #include "lldb/Host/OptionParser.h"
18 #include "lldb/Interpreter/CommandInterpreter.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Utility/Status.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
26 // OptionGroupVariable::GetNumDefinitions()
27 static constexpr OptionDefinition g_variable_options[] = {
28     {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-args", 'a',
29      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
30      "Omit function arguments."},
31     {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-recognized-args", 't',
32      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
33      "Omit recognized function arguments."},
34     {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "no-locals", 'l',
35      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
36      "Omit local variables."},
37     {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "show-globals", 'g',
38      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
39      "Show the current frame source file global and static variables."},
40     {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "show-declaration", 'c',
41      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
42      "Show variable declaration information (source file and line where the "
43      "variable was declared)."},
44     {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "regex", 'r',
45      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeRegularExpression,
46      "The <variable-name> argument for name lookups are regular expressions."},
47     {LLDB_OPT_SET_1 | LLDB_OPT_SET_2, false, "scope", 's',
48      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
49      "Show variable scope (argument, local, global, static)."},
50     {LLDB_OPT_SET_1, false, "summary", 'y', OptionParser::eRequiredArgument,
51      nullptr, {}, 0, eArgTypeName,
52      "Specify the summary that the variable output should use."},
53     {LLDB_OPT_SET_2, false, "summary-string", 'z',
54      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeName,
55      "Specify a summary string to use to format the variable output."},
56 };
57 
58 static Status ValidateNamedSummary(const char *str, void *) {
59   if (!str || !str[0])
60     return Status("must specify a valid named summary");
61   TypeSummaryImplSP summary_sp;
62   if (DataVisualization::NamedSummaryFormats::GetSummaryFormat(
63           ConstString(str), summary_sp) == false)
64     return Status("must specify a valid named summary");
65   return Status();
66 }
67 
68 static Status ValidateSummaryString(const char *str, void *) {
69   if (!str || !str[0])
70     return Status("must specify a non-empty summary string");
71   return Status();
72 }
73 
74 OptionGroupVariable::OptionGroupVariable(bool show_frame_options)
75     : OptionGroup(), include_frame_options(show_frame_options),
76       summary(ValidateNamedSummary), summary_string(ValidateSummaryString) {}
77 
78 OptionGroupVariable::~OptionGroupVariable() {}
79 
80 Status
81 OptionGroupVariable::SetOptionValue(uint32_t option_idx,
82                                     llvm::StringRef option_arg,
83                                     ExecutionContext *execution_context) {
84   Status error;
85   if (!include_frame_options)
86     option_idx += 3;
87   const int short_option = g_variable_options[option_idx].short_option;
88   switch (short_option) {
89   case 'r':
90     use_regex = true;
91     break;
92   case 'a':
93     show_args = false;
94     break;
95   case 'l':
96     show_locals = false;
97     break;
98   case 'g':
99     show_globals = true;
100     break;
101   case 'c':
102     show_decl = true;
103     break;
104   case 's':
105     show_scope = true;
106     break;
107   case 't':
108     show_recognized_args = false;
109     break;
110   case 'y':
111     error = summary.SetCurrentValue(option_arg);
112     break;
113   case 'z':
114     error = summary_string.SetCurrentValue(option_arg);
115     break;
116   default:
117     error.SetErrorStringWithFormat("unrecognized short option '%c'",
118                                    short_option);
119     break;
120   }
121 
122   return error;
123 }
124 
125 void OptionGroupVariable::OptionParsingStarting(
126     ExecutionContext *execution_context) {
127   show_args = true;     // Frame option only
128   show_recognized_args = true; // Frame option only
129   show_locals = true;   // Frame option only
130   show_globals = false; // Frame option only
131   show_decl = false;
132   use_regex = false;
133   show_scope = false;
134   summary.Clear();
135   summary_string.Clear();
136 }
137 
138 #define NUM_FRAME_OPTS 3
139 
140 llvm::ArrayRef<OptionDefinition> OptionGroupVariable::GetDefinitions() {
141   auto result = llvm::makeArrayRef(g_variable_options);
142   // Show the "--no-args", "--no-locals" and "--show-globals" options if we are
143   // showing frame specific options
144   if (include_frame_options)
145     return result;
146 
147   // Skip the "--no-args", "--no-locals" and "--show-globals" options if we are
148   // not showing frame specific options (globals only)
149   return result.drop_front(NUM_FRAME_OPTS);
150 }
151