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