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