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/lldb-python.h"
11 
12 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
13 
14 // C Includes
15 // C++ Includes
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/DataFormatters/ValueObjectPrinter.h"
19 #include "lldb/Host/StringConvert.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Interpreter/CommandInterpreter.h"
22 #include "lldb/Utility/Utils.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 OptionGroupValueObjectDisplay::OptionGroupValueObjectDisplay()
28 {
29 }
30 
31 OptionGroupValueObjectDisplay::~OptionGroupValueObjectDisplay ()
32 {
33 }
34 
35 static OptionDefinition
36 g_option_table[] =
37 {
38     { 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."},
39     { LLDB_OPT_SET_1, false, "synthetic-type",     'S', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean,   "Show the object obeying its synthetic provider, if available."},
40     { 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)."},
41     { 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."},
42     { LLDB_OPT_SET_1, false, "location",           'L', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Show variable location information."},
43     { LLDB_OPT_SET_1, false, "object-description", 'O', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Print as an Objective-C object."},
44     { 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)."},
45     { LLDB_OPT_SET_1, false, "show-types",         'T', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Show variable types when dumping values."},
46     { 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)."},
47     { LLDB_OPT_SET_1, false, "raw-output",         'R', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Don't use formatting options."},
48     { 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."},
49     { LLDB_OPT_SET_1, false, "validate",           'V',  OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean,   "Show results of type validators."},
50     { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
51 };
52 
53 uint32_t
54 OptionGroupValueObjectDisplay::GetNumDefinitions ()
55 {
56     return llvm::array_lengthof(g_option_table);
57 }
58 
59 const OptionDefinition *
60 OptionGroupValueObjectDisplay::GetDefinitions ()
61 {
62     return g_option_table;
63 }
64 
65 
66 Error
67 OptionGroupValueObjectDisplay::SetOptionValue (CommandInterpreter &interpreter,
68                                                uint32_t option_idx,
69                                                const char *option_arg)
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 'P':
99             ptr_depth = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
100             if (!success)
101                 error.SetErrorStringWithFormat("invalid pointer depth '%s'", option_arg);
102             break;
103 
104         case 'Y':
105             if (option_arg)
106             {
107                 no_summary_depth = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
108                 if (!success)
109                     error.SetErrorStringWithFormat("invalid pointer depth '%s'", option_arg);
110             }
111             else
112                 no_summary_depth = 1;
113             break;
114 
115         case 'S':
116             use_synth = Args::StringToBoolean(option_arg, true, &success);
117             if (!success)
118                 error.SetErrorStringWithFormat("invalid synthetic-type '%s'", option_arg);
119             break;
120 
121         case 'V':
122             run_validator = Args::StringToBoolean(option_arg, true, &success);
123             if (!success)
124                 error.SetErrorStringWithFormat("invalid validate '%s'", option_arg);
125             break;
126 
127         default:
128             error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
129             break;
130     }
131 
132     return error;
133 }
134 
135 void
136 OptionGroupValueObjectDisplay::OptionParsingStarting (CommandInterpreter &interpreter)
137 {
138     // If these defaults change, be sure to modify AnyOptionWasSet().
139     show_types        = false;
140     no_summary_depth  = 0;
141     show_location     = false;
142     flat_output       = false;
143     use_objc          = false;
144     max_depth         = UINT32_MAX;
145     ptr_depth         = 0;
146     use_synth         = true;
147     be_raw            = false;
148     ignore_cap        = false;
149     run_validator     = false;
150 
151     Target *target = interpreter.GetExecutionContext().GetTargetPtr();
152     if (target != nullptr)
153         use_dynamic = target->GetPreferDynamicValue();
154     else
155     {
156         // If we don't have any targets, then dynamic values won't do us much good.
157         use_dynamic = lldb::eNoDynamicValues;
158     }
159 }
160 
161 DumpValueObjectOptions
162 OptionGroupValueObjectDisplay::GetAsDumpOptions (LanguageRuntimeDescriptionDisplayVerbosity lang_descr_verbosity,
163                                                  lldb::Format format,
164                                                  lldb::TypeSummaryImplSP summary_sp)
165 {
166     DumpValueObjectOptions options;
167     options.SetMaximumPointerDepth(ptr_depth);
168     if (use_objc)
169         options.SetShowSummary(false);
170     else
171         options.SetOmitSummaryDepth(no_summary_depth);
172     options.SetMaximumDepth(max_depth)
173     .SetShowTypes(show_types)
174     .SetShowLocation(show_location)
175     .SetUseObjectiveC(use_objc)
176     .SetUseDynamicType(use_dynamic)
177     .SetUseSyntheticValue(use_synth)
178     .SetFlatOutput(flat_output)
179     .SetIgnoreCap(ignore_cap)
180     .SetFormat(format)
181     .SetSummary(summary_sp);
182 
183     if (lang_descr_verbosity == eLanguageRuntimeDescriptionDisplayVerbosityCompact)
184         options.SetHideRootType(use_objc)
185         .SetHideName(use_objc)
186         .SetHideValue(use_objc);
187 
188     if (be_raw)
189         options.SetRawDisplay();
190 
191     options.SetRunValidator(run_validator);
192 
193     return options;
194 }
195