1dda28197Spatrick //===-- OptionGroupValueObjectDisplay.cpp ---------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
10061da546Spatrick 
11061da546Spatrick #include "lldb/DataFormatters/ValueObjectPrinter.h"
12061da546Spatrick #include "lldb/Host/OptionParser.h"
13061da546Spatrick #include "lldb/Interpreter/CommandInterpreter.h"
14061da546Spatrick #include "lldb/Interpreter/OptionArgParser.h"
15061da546Spatrick #include "lldb/Target/Target.h"
16061da546Spatrick 
17061da546Spatrick #include "llvm/ADT/ArrayRef.h"
18061da546Spatrick 
19061da546Spatrick using namespace lldb;
20061da546Spatrick using namespace lldb_private;
21061da546Spatrick 
22061da546Spatrick static const OptionDefinition g_option_table[] = {
23061da546Spatrick     {LLDB_OPT_SET_1, false, "dynamic-type", 'd',
24061da546Spatrick      OptionParser::eRequiredArgument, nullptr, GetDynamicValueTypes(), 0,
25061da546Spatrick      eArgTypeNone, "Show the object as its full dynamic type, not its static "
26061da546Spatrick                    "type, if available."},
27061da546Spatrick     {LLDB_OPT_SET_1, false, "synthetic-type", 'S',
28061da546Spatrick      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeBoolean,
29061da546Spatrick      "Show the object obeying its synthetic provider, if available."},
30061da546Spatrick     {LLDB_OPT_SET_1, false, "depth", 'D', OptionParser::eRequiredArgument,
31061da546Spatrick      nullptr, {}, 0, eArgTypeCount, "Set the max recurse depth when dumping "
32061da546Spatrick                                     "aggregate types (default is infinity)."},
33061da546Spatrick     {LLDB_OPT_SET_1, false, "flat", 'F', OptionParser::eNoArgument, nullptr,
34061da546Spatrick      {}, 0, eArgTypeNone, "Display results in a flat format that uses "
35061da546Spatrick                           "expression paths for each variable or member."},
36061da546Spatrick     {LLDB_OPT_SET_1, false, "location", 'L', OptionParser::eNoArgument, nullptr,
37061da546Spatrick      {}, 0, eArgTypeNone, "Show variable location information."},
38061da546Spatrick     {LLDB_OPT_SET_1, false, "object-description", 'O',
39061da546Spatrick      OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone,
40061da546Spatrick      "Display using a language-specific description API, if possible."},
41061da546Spatrick     {LLDB_OPT_SET_1, false, "ptr-depth", 'P', OptionParser::eRequiredArgument,
42061da546Spatrick      nullptr, {}, 0, eArgTypeCount, "The number of pointers to be traversed "
43061da546Spatrick                                     "when dumping values (default is zero)."},
44061da546Spatrick     {LLDB_OPT_SET_1, false, "show-types", 'T', OptionParser::eNoArgument,
45061da546Spatrick      nullptr, {}, 0, eArgTypeNone,
46061da546Spatrick      "Show variable types when dumping values."},
47061da546Spatrick     {LLDB_OPT_SET_1, false, "no-summary-depth", 'Y',
48061da546Spatrick      OptionParser::eOptionalArgument, nullptr, {}, 0, eArgTypeCount,
49061da546Spatrick      "Set the depth at which omitting summary information stops (default is "
50061da546Spatrick      "1)."},
51061da546Spatrick     {LLDB_OPT_SET_1, false, "raw-output", 'R', OptionParser::eNoArgument,
52061da546Spatrick      nullptr, {}, 0, eArgTypeNone, "Don't use formatting options."},
53061da546Spatrick     {LLDB_OPT_SET_1, false, "show-all-children", 'A', OptionParser::eNoArgument,
54061da546Spatrick      nullptr, {}, 0, eArgTypeNone,
55061da546Spatrick      "Ignore the upper bound on the number of children to show."},
56061da546Spatrick     {LLDB_OPT_SET_1, false, "validate", 'V', OptionParser::eRequiredArgument,
57061da546Spatrick      nullptr, {}, 0, eArgTypeBoolean, "Show results of type validators."},
58061da546Spatrick     {LLDB_OPT_SET_1, false, "element-count", 'Z',
59061da546Spatrick      OptionParser::eRequiredArgument, nullptr, {}, 0, eArgTypeCount,
60061da546Spatrick      "Treat the result of the expression as if its type is an array of this "
61061da546Spatrick      "many values."}};
62061da546Spatrick 
63061da546Spatrick llvm::ArrayRef<OptionDefinition>
GetDefinitions()64061da546Spatrick OptionGroupValueObjectDisplay::GetDefinitions() {
65*f6aab3d8Srobert   return llvm::ArrayRef(g_option_table);
66061da546Spatrick }
67061da546Spatrick 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)68061da546Spatrick Status OptionGroupValueObjectDisplay::SetOptionValue(
69061da546Spatrick     uint32_t option_idx, llvm::StringRef option_arg,
70061da546Spatrick     ExecutionContext *execution_context) {
71061da546Spatrick   Status error;
72061da546Spatrick   const int short_option = g_option_table[option_idx].short_option;
73061da546Spatrick   bool success = false;
74061da546Spatrick 
75061da546Spatrick   switch (short_option) {
76061da546Spatrick   case 'd': {
77061da546Spatrick     int32_t result;
78061da546Spatrick     result = OptionArgParser::ToOptionEnum(option_arg, GetDynamicValueTypes(),
79061da546Spatrick                                            2, error);
80061da546Spatrick     if (error.Success())
81061da546Spatrick       use_dynamic = (lldb::DynamicValueType)result;
82061da546Spatrick   } break;
83061da546Spatrick   case 'T':
84061da546Spatrick     show_types = true;
85061da546Spatrick     break;
86061da546Spatrick   case 'L':
87061da546Spatrick     show_location = true;
88061da546Spatrick     break;
89061da546Spatrick   case 'F':
90061da546Spatrick     flat_output = true;
91061da546Spatrick     break;
92061da546Spatrick   case 'O':
93061da546Spatrick     use_objc = true;
94061da546Spatrick     break;
95061da546Spatrick   case 'R':
96061da546Spatrick     be_raw = true;
97061da546Spatrick     break;
98061da546Spatrick   case 'A':
99061da546Spatrick     ignore_cap = true;
100061da546Spatrick     break;
101061da546Spatrick 
102061da546Spatrick   case 'D':
103061da546Spatrick     if (option_arg.getAsInteger(0, max_depth)) {
104061da546Spatrick       max_depth = UINT32_MAX;
105061da546Spatrick       error.SetErrorStringWithFormat("invalid max depth '%s'",
106061da546Spatrick                                      option_arg.str().c_str());
107*f6aab3d8Srobert     } else {
108*f6aab3d8Srobert       max_depth_is_default = false;
109061da546Spatrick     }
110061da546Spatrick     break;
111061da546Spatrick 
112061da546Spatrick   case 'Z':
113061da546Spatrick     if (option_arg.getAsInteger(0, elem_count)) {
114061da546Spatrick       elem_count = UINT32_MAX;
115061da546Spatrick       error.SetErrorStringWithFormat("invalid element count '%s'",
116061da546Spatrick                                      option_arg.str().c_str());
117061da546Spatrick     }
118061da546Spatrick     break;
119061da546Spatrick 
120061da546Spatrick   case 'P':
121061da546Spatrick     if (option_arg.getAsInteger(0, ptr_depth)) {
122061da546Spatrick       ptr_depth = 0;
123061da546Spatrick       error.SetErrorStringWithFormat("invalid pointer depth '%s'",
124061da546Spatrick                                      option_arg.str().c_str());
125061da546Spatrick     }
126061da546Spatrick     break;
127061da546Spatrick 
128061da546Spatrick   case 'Y':
129061da546Spatrick     if (option_arg.empty())
130061da546Spatrick       no_summary_depth = 1;
131061da546Spatrick     else if (option_arg.getAsInteger(0, no_summary_depth)) {
132061da546Spatrick       no_summary_depth = 0;
133061da546Spatrick       error.SetErrorStringWithFormat("invalid pointer depth '%s'",
134061da546Spatrick                                      option_arg.str().c_str());
135061da546Spatrick     }
136061da546Spatrick     break;
137061da546Spatrick 
138061da546Spatrick   case 'S':
139061da546Spatrick     use_synth = OptionArgParser::ToBoolean(option_arg, true, &success);
140061da546Spatrick     if (!success)
141061da546Spatrick       error.SetErrorStringWithFormat("invalid synthetic-type '%s'",
142061da546Spatrick                                      option_arg.str().c_str());
143061da546Spatrick     break;
144061da546Spatrick 
145061da546Spatrick   case 'V':
146061da546Spatrick     run_validator = OptionArgParser::ToBoolean(option_arg, true, &success);
147061da546Spatrick     if (!success)
148061da546Spatrick       error.SetErrorStringWithFormat("invalid validate '%s'",
149061da546Spatrick                                      option_arg.str().c_str());
150061da546Spatrick     break;
151061da546Spatrick 
152061da546Spatrick   default:
153061da546Spatrick     llvm_unreachable("Unimplemented option");
154061da546Spatrick   }
155061da546Spatrick 
156061da546Spatrick   return error;
157061da546Spatrick }
158061da546Spatrick 
OptionParsingStarting(ExecutionContext * execution_context)159061da546Spatrick void OptionGroupValueObjectDisplay::OptionParsingStarting(
160061da546Spatrick     ExecutionContext *execution_context) {
161061da546Spatrick   // If these defaults change, be sure to modify AnyOptionWasSet().
162061da546Spatrick   show_types = false;
163061da546Spatrick   no_summary_depth = 0;
164061da546Spatrick   show_location = false;
165061da546Spatrick   flat_output = false;
166061da546Spatrick   use_objc = false;
167061da546Spatrick   max_depth = UINT32_MAX;
168*f6aab3d8Srobert   max_depth_is_default = true;
169061da546Spatrick   ptr_depth = 0;
170061da546Spatrick   elem_count = 0;
171061da546Spatrick   use_synth = true;
172061da546Spatrick   be_raw = false;
173061da546Spatrick   ignore_cap = false;
174061da546Spatrick   run_validator = false;
175061da546Spatrick 
176061da546Spatrick   TargetSP target_sp =
177061da546Spatrick       execution_context ? execution_context->GetTargetSP() : TargetSP();
178*f6aab3d8Srobert   if (target_sp) {
179061da546Spatrick     use_dynamic = target_sp->GetPreferDynamicValue();
180*f6aab3d8Srobert     auto max_depth_config = target_sp->GetMaximumDepthOfChildrenToDisplay();
181*f6aab3d8Srobert     max_depth = std::get<uint32_t>(max_depth_config);
182*f6aab3d8Srobert     max_depth_is_default = std::get<bool>(max_depth_config);
183*f6aab3d8Srobert   } else {
184061da546Spatrick     // If we don't have any targets, then dynamic values won't do us much good.
185061da546Spatrick     use_dynamic = lldb::eNoDynamicValues;
186061da546Spatrick   }
187061da546Spatrick }
188061da546Spatrick 
GetAsDumpOptions(LanguageRuntimeDescriptionDisplayVerbosity lang_descr_verbosity,lldb::Format format,lldb::TypeSummaryImplSP summary_sp)189061da546Spatrick DumpValueObjectOptions OptionGroupValueObjectDisplay::GetAsDumpOptions(
190061da546Spatrick     LanguageRuntimeDescriptionDisplayVerbosity lang_descr_verbosity,
191061da546Spatrick     lldb::Format format, lldb::TypeSummaryImplSP summary_sp) {
192061da546Spatrick   DumpValueObjectOptions options;
193061da546Spatrick   options.SetMaximumPointerDepth(
194061da546Spatrick       {DumpValueObjectOptions::PointerDepth::Mode::Always, ptr_depth});
195061da546Spatrick   if (use_objc)
196061da546Spatrick     options.SetShowSummary(false);
197061da546Spatrick   else
198061da546Spatrick     options.SetOmitSummaryDepth(no_summary_depth);
199*f6aab3d8Srobert   options.SetMaximumDepth(max_depth, max_depth_is_default)
200061da546Spatrick       .SetShowTypes(show_types)
201061da546Spatrick       .SetShowLocation(show_location)
202061da546Spatrick       .SetUseObjectiveC(use_objc)
203061da546Spatrick       .SetUseDynamicType(use_dynamic)
204061da546Spatrick       .SetUseSyntheticValue(use_synth)
205061da546Spatrick       .SetFlatOutput(flat_output)
206061da546Spatrick       .SetIgnoreCap(ignore_cap)
207061da546Spatrick       .SetFormat(format)
208061da546Spatrick       .SetSummary(summary_sp);
209061da546Spatrick 
210061da546Spatrick   if (lang_descr_verbosity ==
211061da546Spatrick       eLanguageRuntimeDescriptionDisplayVerbosityCompact)
212061da546Spatrick     options.SetHideRootType(use_objc).SetHideName(use_objc).SetHideValue(
213061da546Spatrick         use_objc);
214061da546Spatrick 
215061da546Spatrick   if (be_raw)
216061da546Spatrick     options.SetRawDisplay();
217061da546Spatrick 
218061da546Spatrick   options.SetRunValidator(run_validator);
219061da546Spatrick 
220061da546Spatrick   options.SetElementCount(elem_count);
221061da546Spatrick 
222061da546Spatrick   return options;
223061da546Spatrick }
224