• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- OptionGroupValueObjectDisplay.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/OptionGroupValueObjectDisplay.h"
10 
11 #include "lldb/DataFormatters/ValueObjectPrinter.h"
12 #include "lldb/Host/OptionParser.h"
13 #include "lldb/Interpreter/CommandInterpreter.h"
14 #include "lldb/Interpreter/OptionArgParser.h"
15 #include "lldb/Target/Target.h"
16 
17 #include "llvm/ADT/ArrayRef.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
OptionGroupValueObjectDisplay()22 OptionGroupValueObjectDisplay::OptionGroupValueObjectDisplay() {}
23 
~OptionGroupValueObjectDisplay()24 OptionGroupValueObjectDisplay::~OptionGroupValueObjectDisplay() {}
25 
26 static const OptionDefinition g_option_table[] = {
27     {LLDB_OPT_SET_1, false, "dynamic-type", 'd',
28      OptionParser::eRequiredArgument, nullptr, GetDynamicValueTypes(), 0,
29      eArgTypeNone, "Show the object as its full dynamic type, not its static "
30                    "type, if available."},
31     {LLDB_OPT_SET_1, false, "synthetic-type", 'S',
32      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean,
33      "Show the object obeying its synthetic provider, if available."},
34     {LLDB_OPT_SET_1, false, "depth", 'D', OptionParser::eRequiredArgument,
35      nullptr, {}, 0, eArgTypeCount, "Set the max recurse depth when dumping "
36                                     "aggregate types (default is infinity)."},
37     {LLDB_OPT_SET_1, false, "flat", 'F', OptionParser::eNoArgument, nullptr,
38      {}, 0, eArgTypeNone, "Display results in a flat format that uses "
39                           "expression paths for each variable or member."},
40     {LLDB_OPT_SET_1, false, "location", 'L', OptionParser::eNoArgument, nullptr,
41      {}, 0, eArgTypeNone, "Show variable location information."},
42     {LLDB_OPT_SET_1, false, "object-description", 'O',
43      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
44      "Display using a language-specific description API, if possible."},
45     {LLDB_OPT_SET_1, false, "ptr-depth", 'P', OptionParser::eRequiredArgument,
46      nullptr, {}, 0, eArgTypeCount, "The number of pointers to be traversed "
47                                     "when dumping values (default is zero)."},
48     {LLDB_OPT_SET_1, false, "show-types", 'T', OptionParser::eNoArgument,
49      nullptr, {}, 0, eArgTypeNone,
50      "Show variable types when dumping values."},
51     {LLDB_OPT_SET_1, false, "no-summary-depth", 'Y',
52      OptionParser::eOptionalArgument, nullptr, {}, 0, eArgTypeCount,
53      "Set the depth at which omitting summary information stops (default is "
54      "1)."},
55     {LLDB_OPT_SET_1, false, "raw-output", 'R', OptionParser::eNoArgument,
56      nullptr, {}, 0, eArgTypeNone, "Don't use formatting options."},
57     {LLDB_OPT_SET_1, false, "show-all-children", 'A', OptionParser::eNoArgument,
58      nullptr, {}, 0, eArgTypeNone,
59      "Ignore the upper bound on the number of children to show."},
60     {LLDB_OPT_SET_1, false, "validate", 'V', OptionParser::eRequiredArgument,
61      nullptr, {}, 0, eArgTypeBoolean, "Show results of type validators."},
62     {LLDB_OPT_SET_1, false, "element-count", 'Z',
63      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCount,
64      "Treat the result of the expression as if its type is an array of this "
65      "many values."}};
66 
67 llvm::ArrayRef<OptionDefinition>
GetDefinitions()68 OptionGroupValueObjectDisplay::GetDefinitions() {
69   return llvm::makeArrayRef(g_option_table);
70 }
71 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)72 Status OptionGroupValueObjectDisplay::SetOptionValue(
73     uint32_t option_idx, llvm::StringRef option_arg,
74     ExecutionContext *execution_context) {
75   Status error;
76   const int short_option = g_option_table[option_idx].short_option;
77   bool success = false;
78 
79   switch (short_option) {
80   case 'd': {
81     int32_t result;
82     result = OptionArgParser::ToOptionEnum(option_arg, GetDynamicValueTypes(),
83                                            2, error);
84     if (error.Success())
85       use_dynamic = (lldb::DynamicValueType)result;
86   } break;
87   case 'T':
88     show_types = true;
89     break;
90   case 'L':
91     show_location = true;
92     break;
93   case 'F':
94     flat_output = true;
95     break;
96   case 'O':
97     use_objc = true;
98     break;
99   case 'R':
100     be_raw = true;
101     break;
102   case 'A':
103     ignore_cap = true;
104     break;
105 
106   case 'D':
107     if (option_arg.getAsInteger(0, max_depth)) {
108       max_depth = UINT32_MAX;
109       error.SetErrorStringWithFormat("invalid max depth '%s'",
110                                      option_arg.str().c_str());
111     }
112     break;
113 
114   case 'Z':
115     if (option_arg.getAsInteger(0, elem_count)) {
116       elem_count = UINT32_MAX;
117       error.SetErrorStringWithFormat("invalid element count '%s'",
118                                      option_arg.str().c_str());
119     }
120     break;
121 
122   case 'P':
123     if (option_arg.getAsInteger(0, ptr_depth)) {
124       ptr_depth = 0;
125       error.SetErrorStringWithFormat("invalid pointer depth '%s'",
126                                      option_arg.str().c_str());
127     }
128     break;
129 
130   case 'Y':
131     if (option_arg.empty())
132       no_summary_depth = 1;
133     else if (option_arg.getAsInteger(0, no_summary_depth)) {
134       no_summary_depth = 0;
135       error.SetErrorStringWithFormat("invalid pointer depth '%s'",
136                                      option_arg.str().c_str());
137     }
138     break;
139 
140   case 'S':
141     use_synth = OptionArgParser::ToBoolean(option_arg, true, &success);
142     if (!success)
143       error.SetErrorStringWithFormat("invalid synthetic-type '%s'",
144                                      option_arg.str().c_str());
145     break;
146 
147   case 'V':
148     run_validator = OptionArgParser::ToBoolean(option_arg, true, &success);
149     if (!success)
150       error.SetErrorStringWithFormat("invalid validate '%s'",
151                                      option_arg.str().c_str());
152     break;
153 
154   default:
155     llvm_unreachable("Unimplemented option");
156   }
157 
158   return error;
159 }
160 
OptionParsingStarting(ExecutionContext * execution_context)161 void OptionGroupValueObjectDisplay::OptionParsingStarting(
162     ExecutionContext *execution_context) {
163   // If these defaults change, be sure to modify AnyOptionWasSet().
164   show_types = false;
165   no_summary_depth = 0;
166   show_location = false;
167   flat_output = false;
168   use_objc = false;
169   max_depth = UINT32_MAX;
170   ptr_depth = 0;
171   elem_count = 0;
172   use_synth = true;
173   be_raw = false;
174   ignore_cap = false;
175   run_validator = false;
176 
177   TargetSP target_sp =
178       execution_context ? execution_context->GetTargetSP() : TargetSP();
179   if (target_sp)
180     use_dynamic = target_sp->GetPreferDynamicValue();
181   else {
182     // If we don't have any targets, then dynamic values won't do us much good.
183     use_dynamic = lldb::eNoDynamicValues;
184   }
185 }
186 
GetAsDumpOptions(LanguageRuntimeDescriptionDisplayVerbosity lang_descr_verbosity,lldb::Format format,lldb::TypeSummaryImplSP summary_sp)187 DumpValueObjectOptions OptionGroupValueObjectDisplay::GetAsDumpOptions(
188     LanguageRuntimeDescriptionDisplayVerbosity lang_descr_verbosity,
189     lldb::Format format, lldb::TypeSummaryImplSP summary_sp) {
190   DumpValueObjectOptions options;
191   options.SetMaximumPointerDepth(
192       {DumpValueObjectOptions::PointerDepth::Mode::Always, ptr_depth});
193   if (use_objc)
194     options.SetShowSummary(false);
195   else
196     options.SetOmitSummaryDepth(no_summary_depth);
197   options.SetMaximumDepth(max_depth)
198       .SetShowTypes(show_types)
199       .SetShowLocation(show_location)
200       .SetUseObjectiveC(use_objc)
201       .SetUseDynamicType(use_dynamic)
202       .SetUseSyntheticValue(use_synth)
203       .SetFlatOutput(flat_output)
204       .SetIgnoreCap(ignore_cap)
205       .SetFormat(format)
206       .SetSummary(summary_sp);
207 
208   if (lang_descr_verbosity ==
209       eLanguageRuntimeDescriptionDisplayVerbosityCompact)
210     options.SetHideRootType(use_objc).SetHideName(use_objc).SetHideValue(
211         use_objc);
212 
213   if (be_raw)
214     options.SetRawDisplay();
215 
216   options.SetRunValidator(run_validator);
217 
218   options.SetElementCount(elem_count);
219 
220   return options;
221 }
222