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