| 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 | |
| 17 | using namespace lldb; |
| 18 | using namespace lldb_private; |
| 19 | |
| 20 | // if you add any options here, remember to update the counters in |
| 21 | // OptionGroupVariable::GetNumDefinitions() |
| 22 | static 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 | |
| 53 | static constexpr auto g_num_frame_options = 4; |
| 54 | static const auto g_variable_options_noframe = |
| 55 | llvm::ArrayRef<OptionDefinition>(g_variable_options) |
| 56 | .drop_front(N: g_num_frame_options); |
| 57 | |
| 58 | static Status ValidateNamedSummary(const char *str, void *) { |
| 59 | if (!str || !str[0]) |
| 60 | return Status::FromErrorStringWithFormat( |
| 61 | format: "must specify a valid named summary" ); |
| 62 | TypeSummaryImplSP summary_sp; |
| 63 | if (!DataVisualization::NamedSummaryFormats::GetSummaryFormat( |
| 64 | type: ConstString(str), entry&: summary_sp)) |
| 65 | return Status::FromErrorStringWithFormat( |
| 66 | format: "must specify a valid named summary" ); |
| 67 | return Status(); |
| 68 | } |
| 69 | |
| 70 | static Status ValidateSummaryString(const char *str, void *) { |
| 71 | if (!str || !str[0]) |
| 72 | return Status::FromErrorStringWithFormat( |
| 73 | format: "must specify a non-empty summary string" ); |
| 74 | return Status(); |
| 75 | } |
| 76 | |
| 77 | OptionGroupVariable::OptionGroupVariable(bool show_frame_options) |
| 78 | : include_frame_options(show_frame_options), show_args(false), |
| 79 | show_recognized_args(false), show_locals(false), show_globals(false), |
| 80 | use_regex(false), show_scope(false), show_decl(false), |
| 81 | summary(ValidateNamedSummary), summary_string(ValidateSummaryString) {} |
| 82 | |
| 83 | Status |
| 84 | OptionGroupVariable::SetOptionValue(uint32_t option_idx, |
| 85 | llvm::StringRef option_arg, |
| 86 | ExecutionContext *execution_context) { |
| 87 | Status error; |
| 88 | llvm::ArrayRef<OptionDefinition> variable_options = |
| 89 | include_frame_options ? g_variable_options : g_variable_options_noframe; |
| 90 | const int short_option = variable_options[option_idx].short_option; |
| 91 | switch (short_option) { |
| 92 | case 'r': |
| 93 | use_regex = true; |
| 94 | break; |
| 95 | case 'a': |
| 96 | show_args = false; |
| 97 | break; |
| 98 | case 'l': |
| 99 | show_locals = false; |
| 100 | break; |
| 101 | case 'g': |
| 102 | show_globals = true; |
| 103 | break; |
| 104 | case 'c': |
| 105 | show_decl = true; |
| 106 | break; |
| 107 | case 's': |
| 108 | show_scope = true; |
| 109 | break; |
| 110 | case 't': |
| 111 | show_recognized_args = false; |
| 112 | break; |
| 113 | case 'y': |
| 114 | error = summary.SetCurrentValue(option_arg); |
| 115 | break; |
| 116 | case 'z': |
| 117 | error = summary_string.SetCurrentValue(option_arg); |
| 118 | break; |
| 119 | default: |
| 120 | llvm_unreachable("Unimplemented option" ); |
| 121 | } |
| 122 | |
| 123 | return error; |
| 124 | } |
| 125 | |
| 126 | void OptionGroupVariable::OptionParsingStarting( |
| 127 | ExecutionContext *execution_context) { |
| 128 | show_args = true; // Frame option only |
| 129 | show_recognized_args = true; // Frame option only |
| 130 | show_locals = true; // Frame option only |
| 131 | show_globals = false; // Frame option only |
| 132 | show_decl = false; |
| 133 | use_regex = false; |
| 134 | show_scope = false; |
| 135 | summary.Clear(); |
| 136 | summary_string.Clear(); |
| 137 | } |
| 138 | |
| 139 | llvm::ArrayRef<OptionDefinition> OptionGroupVariable::GetDefinitions() { |
| 140 | // Show the "--no-args", "--no-recognized-args", "--no-locals" and |
| 141 | // "--show-globals" options if we are showing frame specific options |
| 142 | return include_frame_options ? g_variable_options |
| 143 | : g_variable_options_noframe; |
| 144 | } |
| 145 | |