184c39663SGreg Clayton //===-- OptionGroupValueObjectDisplay.cpp -----------------------*- C++ -*-===//
284c39663SGreg Clayton //
384c39663SGreg Clayton //                     The LLVM Compiler Infrastructure
484c39663SGreg Clayton //
584c39663SGreg Clayton // This file is distributed under the University of Illinois Open Source
684c39663SGreg Clayton // License. See LICENSE.TXT for details.
784c39663SGreg Clayton //
884c39663SGreg Clayton //===----------------------------------------------------------------------===//
984c39663SGreg Clayton 
104bee32e5SJohnny Chen #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
1184c39663SGreg Clayton 
1284c39663SGreg Clayton // C Includes
1384c39663SGreg Clayton // C++ Includes
1484c39663SGreg Clayton // Other libraries and framework includes
1584c39663SGreg Clayton // Project includes
164d93b8cdSEnrico Granata #include "lldb/DataFormatters/ValueObjectPrinter.h"
175275aaa0SVince Harron #include "lldb/Host/StringConvert.h"
182837b766SJim Ingham #include "lldb/Target/Target.h"
192837b766SJim Ingham #include "lldb/Interpreter/CommandInterpreter.h"
207c575b3bSJohnny Chen #include "lldb/Utility/Utils.h"
2184c39663SGreg Clayton 
2284c39663SGreg Clayton using namespace lldb;
2384c39663SGreg Clayton using namespace lldb_private;
2484c39663SGreg Clayton 
2584c39663SGreg Clayton OptionGroupValueObjectDisplay::OptionGroupValueObjectDisplay()
2684c39663SGreg Clayton {
2784c39663SGreg Clayton }
2884c39663SGreg Clayton 
2984c39663SGreg Clayton OptionGroupValueObjectDisplay::~OptionGroupValueObjectDisplay ()
3084c39663SGreg Clayton {
3184c39663SGreg Clayton }
3284c39663SGreg Clayton 
3368ebae61SGreg Clayton static OptionDefinition
3484c39663SGreg Clayton g_option_table[] =
3584c39663SGreg Clayton {
36d37221dcSZachary Turner     { 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."},
37d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "synthetic-type",     'S', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean,   "Show the object obeying its synthetic provider, if available."},
38d37221dcSZachary Turner     { 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)."},
39d37221dcSZachary Turner     { 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."},
40d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "location",           'L', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Show variable location information."},
41d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "object-description", 'O', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Print as an Objective-C object."},
42d37221dcSZachary Turner     { 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)."},
43d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "show-types",         'T', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Show variable types when dumping values."},
44d37221dcSZachary Turner     { 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)."},
45d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "raw-output",         'R', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Don't use formatting options."},
46d37221dcSZachary Turner     { 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."},
470f883ffbSEnrico Granata     { LLDB_OPT_SET_1, false, "validate",           'V',  OptionParser::eRequiredArgument, nullptr, nullptr,0, eArgTypeBoolean,   "Show results of type validators."},
48520a422bSEnrico Granata     { 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."},
49d37221dcSZachary Turner     { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
5084c39663SGreg Clayton };
5184c39663SGreg Clayton 
5284c39663SGreg Clayton uint32_t
5384c39663SGreg Clayton OptionGroupValueObjectDisplay::GetNumDefinitions ()
5484c39663SGreg Clayton {
556ebc8c45SJohnny Chen     return llvm::array_lengthof(g_option_table);
5684c39663SGreg Clayton }
5784c39663SGreg Clayton 
5884c39663SGreg Clayton const OptionDefinition *
5984c39663SGreg Clayton OptionGroupValueObjectDisplay::GetDefinitions ()
6084c39663SGreg Clayton {
6184c39663SGreg Clayton     return g_option_table;
6284c39663SGreg Clayton }
6384c39663SGreg Clayton 
6484c39663SGreg Clayton 
6584c39663SGreg Clayton Error
66*e1cfbc79STodd Fiala OptionGroupValueObjectDisplay::SetOptionValue(uint32_t option_idx,
67*e1cfbc79STodd Fiala                                               const char *option_arg,
68*e1cfbc79STodd Fiala                                               ExecutionContext
69*e1cfbc79STodd Fiala                                               *execution_context)
7084c39663SGreg Clayton {
7184c39663SGreg Clayton     Error error;
723bcdfc0eSGreg Clayton     const int short_option = g_option_table[option_idx].short_option;
7384c39663SGreg Clayton     bool success = false;
7484c39663SGreg Clayton 
7584c39663SGreg Clayton     switch (short_option)
7684c39663SGreg Clayton     {
772837b766SJim Ingham         case 'd':
782837b766SJim Ingham             {
792837b766SJim Ingham                 int32_t result;
8067cc0636SGreg Clayton                 result = Args::StringToOptionEnum (option_arg, g_dynamic_value_types, 2, error);
81cf0e4f0dSGreg Clayton                 if (error.Success())
822837b766SJim Ingham                     use_dynamic = (lldb::DynamicValueType) result;
832837b766SJim Ingham             }
842837b766SJim Ingham             break;
8568ebae61SGreg Clayton         case 'T':   show_types   = true;  break;
8684c39663SGreg Clayton         case 'L':   show_location= true;  break;
8784c39663SGreg Clayton         case 'F':   flat_output  = true;  break;
8868ebae61SGreg Clayton         case 'O':   use_objc     = true;  break;
89ce68b02cSEnrico Granata         case 'R':   be_raw       = true;  break;
9022c55d18SEnrico Granata         case 'A':   ignore_cap   = true;  break;
91ce68b02cSEnrico Granata 
9268ebae61SGreg Clayton         case 'D':
935275aaa0SVince Harron             max_depth = StringConvert::ToUInt32 (option_arg, UINT32_MAX, 0, &success);
9484c39663SGreg Clayton             if (!success)
9586edbf41SGreg Clayton                 error.SetErrorStringWithFormat("invalid max depth '%s'", option_arg);
9684c39663SGreg Clayton             break;
9784c39663SGreg Clayton 
98520a422bSEnrico Granata         case 'Z':
99520a422bSEnrico Granata             elem_count = StringConvert::ToUInt32 (option_arg, UINT32_MAX, 0, &success);
100520a422bSEnrico Granata             if (!success)
101520a422bSEnrico Granata                 error.SetErrorStringWithFormat("invalid element count '%s'", option_arg);
102520a422bSEnrico Granata             break;
103520a422bSEnrico Granata 
10468ebae61SGreg Clayton         case 'P':
1055275aaa0SVince Harron             ptr_depth = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
10684c39663SGreg Clayton             if (!success)
10786edbf41SGreg Clayton                 error.SetErrorStringWithFormat("invalid pointer depth '%s'", option_arg);
10884c39663SGreg Clayton             break;
10984c39663SGreg Clayton 
1100c5ef693SEnrico Granata         case 'Y':
1110c5ef693SEnrico Granata             if (option_arg)
1120c5ef693SEnrico Granata             {
1135275aaa0SVince Harron                 no_summary_depth = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
1140c5ef693SEnrico Granata                 if (!success)
11586edbf41SGreg Clayton                     error.SetErrorStringWithFormat("invalid pointer depth '%s'", option_arg);
1160c5ef693SEnrico Granata             }
1170c5ef693SEnrico Granata             else
1180c5ef693SEnrico Granata                 no_summary_depth = 1;
1190c5ef693SEnrico Granata             break;
1200c5ef693SEnrico Granata 
121d55546b2SEnrico Granata         case 'S':
122d55546b2SEnrico Granata             use_synth = Args::StringToBoolean(option_arg, true, &success);
123d55546b2SEnrico Granata             if (!success)
12486edbf41SGreg Clayton                 error.SetErrorStringWithFormat("invalid synthetic-type '%s'", option_arg);
125d55546b2SEnrico Granata             break;
1260f883ffbSEnrico Granata 
1270f883ffbSEnrico Granata         case 'V':
1280f883ffbSEnrico Granata             run_validator = Args::StringToBoolean(option_arg, true, &success);
1290f883ffbSEnrico Granata             if (!success)
1300f883ffbSEnrico Granata                 error.SetErrorStringWithFormat("invalid validate '%s'", option_arg);
1310f883ffbSEnrico Granata             break;
1320f883ffbSEnrico Granata 
13384c39663SGreg Clayton         default:
13486edbf41SGreg Clayton             error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
13584c39663SGreg Clayton             break;
13684c39663SGreg Clayton     }
13784c39663SGreg Clayton 
13884c39663SGreg Clayton     return error;
13984c39663SGreg Clayton }
14084c39663SGreg Clayton 
14184c39663SGreg Clayton void
142*e1cfbc79STodd Fiala OptionGroupValueObjectDisplay::OptionParsingStarting(ExecutionContext
143*e1cfbc79STodd Fiala                                                      *execution_context)
14484c39663SGreg Clayton {
14582f4cf46SGreg Clayton     // If these defaults change, be sure to modify AnyOptionWasSet().
14684c39663SGreg Clayton     show_types        = false;
1470c5ef693SEnrico Granata     no_summary_depth  = 0;
14884c39663SGreg Clayton     show_location     = false;
14984c39663SGreg Clayton     flat_output       = false;
15084c39663SGreg Clayton     use_objc          = false;
15184c39663SGreg Clayton     max_depth         = UINT32_MAX;
15284c39663SGreg Clayton     ptr_depth         = 0;
153520a422bSEnrico Granata     elem_count        = 0;
154d55546b2SEnrico Granata     use_synth         = true;
155ce68b02cSEnrico Granata     be_raw            = false;
15622c55d18SEnrico Granata     ignore_cap        = false;
1570f883ffbSEnrico Granata     run_validator     = false;
15884c39663SGreg Clayton 
159*e1cfbc79STodd Fiala     TargetSP target_sp =
160*e1cfbc79STodd Fiala         execution_context ? execution_context->GetTargetSP() : TargetSP();
161*e1cfbc79STodd Fiala     if (target_sp)
162*e1cfbc79STodd Fiala         use_dynamic = target_sp->GetPreferDynamicValue();
1632837b766SJim Ingham     else
1642837b766SJim Ingham     {
1652837b766SJim Ingham         // If we don't have any targets, then dynamic values won't do us much good.
1662837b766SJim Ingham         use_dynamic = lldb::eNoDynamicValues;
1672837b766SJim Ingham     }
1682837b766SJim Ingham }
1699fb5ab55SEnrico Granata 
1704d93b8cdSEnrico Granata DumpValueObjectOptions
1714d93b8cdSEnrico Granata OptionGroupValueObjectDisplay::GetAsDumpOptions (LanguageRuntimeDescriptionDisplayVerbosity lang_descr_verbosity,
1729fb5ab55SEnrico Granata                                                  lldb::Format format,
1739fb5ab55SEnrico Granata                                                  lldb::TypeSummaryImplSP summary_sp)
1749fb5ab55SEnrico Granata {
1754d93b8cdSEnrico Granata     DumpValueObjectOptions options;
176c1b7c09aSEnrico Granata     options.SetMaximumPointerDepth( {DumpValueObjectOptions::PointerDepth::Mode::Always,ptr_depth} );
1779fb5ab55SEnrico Granata     if (use_objc)
1789fb5ab55SEnrico Granata         options.SetShowSummary(false);
1799fb5ab55SEnrico Granata     else
1809fb5ab55SEnrico Granata         options.SetOmitSummaryDepth(no_summary_depth);
1819fb5ab55SEnrico Granata     options.SetMaximumDepth(max_depth)
1829fb5ab55SEnrico Granata     .SetShowTypes(show_types)
1839fb5ab55SEnrico Granata     .SetShowLocation(show_location)
1849fb5ab55SEnrico Granata     .SetUseObjectiveC(use_objc)
1859fb5ab55SEnrico Granata     .SetUseDynamicType(use_dynamic)
1869fb5ab55SEnrico Granata     .SetUseSyntheticValue(use_synth)
1879fb5ab55SEnrico Granata     .SetFlatOutput(flat_output)
1889fb5ab55SEnrico Granata     .SetIgnoreCap(ignore_cap)
1899fb5ab55SEnrico Granata     .SetFormat(format)
1909fb5ab55SEnrico Granata     .SetSummary(summary_sp);
1919fb5ab55SEnrico Granata 
1924d93b8cdSEnrico Granata     if (lang_descr_verbosity == eLanguageRuntimeDescriptionDisplayVerbosityCompact)
1939fb5ab55SEnrico Granata         options.SetHideRootType(use_objc)
1949fb5ab55SEnrico Granata         .SetHideName(use_objc)
1959fb5ab55SEnrico Granata         .SetHideValue(use_objc);
1969fb5ab55SEnrico Granata 
1979fb5ab55SEnrico Granata     if (be_raw)
198a126e462SEnrico Granata         options.SetRawDisplay();
1999fb5ab55SEnrico Granata 
2000f883ffbSEnrico Granata     options.SetRunValidator(run_validator);
2010f883ffbSEnrico Granata 
202520a422bSEnrico Granata     options.SetElementCount(elem_count);
203520a422bSEnrico Granata 
2049fb5ab55SEnrico Granata     return options;
2059fb5ab55SEnrico Granata }
206