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 (CommandInterpreter &interpreter,
67                                                uint32_t option_idx,
68                                                const char *option_arg)
69 {
70     Error error;
71     const int short_option = g_option_table[option_idx].short_option;
72     bool success = false;
73 
74     switch (short_option)
75     {
76         case 'd':
77             {
78                 int32_t result;
79                 result = Args::StringToOptionEnum (option_arg, g_dynamic_value_types, 2, error);
80                 if (error.Success())
81                     use_dynamic = (lldb::DynamicValueType) result;
82             }
83             break;
84         case 'T':   show_types   = true;  break;
85         case 'L':   show_location= true;  break;
86         case 'F':   flat_output  = true;  break;
87         case 'O':   use_objc     = true;  break;
88         case 'R':   be_raw       = true;  break;
89         case 'A':   ignore_cap   = true;  break;
90 
91         case 'D':
92             max_depth = StringConvert::ToUInt32 (option_arg, UINT32_MAX, 0, &success);
93             if (!success)
94                 error.SetErrorStringWithFormat("invalid max depth '%s'", option_arg);
95             break;
96 
97         case 'Z':
98             elem_count = StringConvert::ToUInt32 (option_arg, UINT32_MAX, 0, &success);
99             if (!success)
100                 error.SetErrorStringWithFormat("invalid element count '%s'", option_arg);
101             break;
102 
103         case 'P':
104             ptr_depth = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
105             if (!success)
106                 error.SetErrorStringWithFormat("invalid pointer depth '%s'", option_arg);
107             break;
108 
109         case 'Y':
110             if (option_arg)
111             {
112                 no_summary_depth = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
113                 if (!success)
114                     error.SetErrorStringWithFormat("invalid pointer depth '%s'", option_arg);
115             }
116             else
117                 no_summary_depth = 1;
118             break;
119 
120         case 'S':
121             use_synth = Args::StringToBoolean(option_arg, true, &success);
122             if (!success)
123                 error.SetErrorStringWithFormat("invalid synthetic-type '%s'", option_arg);
124             break;
125 
126         case 'V':
127             run_validator = Args::StringToBoolean(option_arg, true, &success);
128             if (!success)
129                 error.SetErrorStringWithFormat("invalid validate '%s'", option_arg);
130             break;
131 
132         default:
133             error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
134             break;
135     }
136 
137     return error;
138 }
139 
140 void
141 OptionGroupValueObjectDisplay::OptionParsingStarting (CommandInterpreter &interpreter)
142 {
143     // If these defaults change, be sure to modify AnyOptionWasSet().
144     show_types        = false;
145     no_summary_depth  = 0;
146     show_location     = false;
147     flat_output       = false;
148     use_objc          = false;
149     max_depth         = UINT32_MAX;
150     ptr_depth         = 0;
151     elem_count        = 0;
152     use_synth         = true;
153     be_raw            = false;
154     ignore_cap        = false;
155     run_validator     = false;
156 
157     Target *target = interpreter.GetExecutionContext().GetTargetPtr();
158     if (target != nullptr)
159         use_dynamic = target->GetPreferDynamicValue();
160     else
161     {
162         // If we don't have any targets, then dynamic values won't do us much good.
163         use_dynamic = lldb::eNoDynamicValues;
164     }
165 }
166 
167 DumpValueObjectOptions
168 OptionGroupValueObjectDisplay::GetAsDumpOptions (LanguageRuntimeDescriptionDisplayVerbosity lang_descr_verbosity,
169                                                  lldb::Format format,
170                                                  lldb::TypeSummaryImplSP summary_sp)
171 {
172     DumpValueObjectOptions options;
173     options.SetMaximumPointerDepth( {DumpValueObjectOptions::PointerDepth::Mode::Always,ptr_depth} );
174     if (use_objc)
175         options.SetShowSummary(false);
176     else
177         options.SetOmitSummaryDepth(no_summary_depth);
178     options.SetMaximumDepth(max_depth)
179     .SetShowTypes(show_types)
180     .SetShowLocation(show_location)
181     .SetUseObjectiveC(use_objc)
182     .SetUseDynamicType(use_dynamic)
183     .SetUseSyntheticValue(use_synth)
184     .SetFlatOutput(flat_output)
185     .SetIgnoreCap(ignore_cap)
186     .SetFormat(format)
187     .SetSummary(summary_sp);
188 
189     if (lang_descr_verbosity == eLanguageRuntimeDescriptionDisplayVerbosityCompact)
190         options.SetHideRootType(use_objc)
191         .SetHideName(use_objc)
192         .SetHideValue(use_objc);
193 
194     if (be_raw)
195         options.SetRawDisplay();
196 
197     options.SetRunValidator(run_validator);
198 
199     options.SetElementCount(elem_count);
200 
201     return options;
202 }
203