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

source code of lldb/source/Interpreter/OptionGroupVariable.cpp