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