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 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel Malea 
124bee32e5SJohnny Chen #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h"
1384c39663SGreg Clayton 
1484c39663SGreg Clayton // C Includes
1584c39663SGreg Clayton // C++ Includes
1684c39663SGreg Clayton // Other libraries and framework includes
1784c39663SGreg Clayton // Project includes
184d93b8cdSEnrico Granata #include "lldb/DataFormatters/ValueObjectPrinter.h"
19*5275aaa0SVince Harron #include "lldb/Host/StringConvert.h"
202837b766SJim Ingham #include "lldb/Target/Target.h"
212837b766SJim Ingham #include "lldb/Interpreter/CommandInterpreter.h"
227c575b3bSJohnny Chen #include "lldb/Utility/Utils.h"
2384c39663SGreg Clayton 
2484c39663SGreg Clayton using namespace lldb;
2584c39663SGreg Clayton using namespace lldb_private;
2684c39663SGreg Clayton 
2784c39663SGreg Clayton OptionGroupValueObjectDisplay::OptionGroupValueObjectDisplay()
2884c39663SGreg Clayton {
2984c39663SGreg Clayton }
3084c39663SGreg Clayton 
3184c39663SGreg Clayton OptionGroupValueObjectDisplay::~OptionGroupValueObjectDisplay ()
3284c39663SGreg Clayton {
3384c39663SGreg Clayton }
3484c39663SGreg Clayton 
3568ebae61SGreg Clayton static OptionDefinition
3684c39663SGreg Clayton g_option_table[] =
3784c39663SGreg Clayton {
38d37221dcSZachary 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."},
39d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "synthetic-type",     'S', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean,   "Show the object obeying its synthetic provider, if available."},
40d37221dcSZachary 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)."},
41d37221dcSZachary 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."},
42d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "location",           'L', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Show variable location information."},
43d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "object-description", 'O', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Print as an Objective-C object."},
44d37221dcSZachary 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)."},
45d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "show-types",         'T', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Show variable types when dumping values."},
46d37221dcSZachary 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)."},
47d37221dcSZachary Turner     { LLDB_OPT_SET_1, false, "raw-output",         'R', OptionParser::eNoArgument,       nullptr, nullptr, 0, eArgTypeNone,      "Don't use formatting options."},
48d37221dcSZachary 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."},
490f883ffbSEnrico Granata     { LLDB_OPT_SET_1, false, "validate",           'V',  OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean,   "Show results of type validators."},
50d37221dcSZachary Turner     { 0, false, nullptr, 0, 0, nullptr, nullptr, 0, eArgTypeNone, nullptr }
5184c39663SGreg Clayton };
5284c39663SGreg Clayton 
5384c39663SGreg Clayton uint32_t
5484c39663SGreg Clayton OptionGroupValueObjectDisplay::GetNumDefinitions ()
5584c39663SGreg Clayton {
566ebc8c45SJohnny Chen     return llvm::array_lengthof(g_option_table);
5784c39663SGreg Clayton }
5884c39663SGreg Clayton 
5984c39663SGreg Clayton const OptionDefinition *
6084c39663SGreg Clayton OptionGroupValueObjectDisplay::GetDefinitions ()
6184c39663SGreg Clayton {
6284c39663SGreg Clayton     return g_option_table;
6384c39663SGreg Clayton }
6484c39663SGreg Clayton 
6584c39663SGreg Clayton 
6684c39663SGreg Clayton Error
6784c39663SGreg Clayton OptionGroupValueObjectDisplay::SetOptionValue (CommandInterpreter &interpreter,
6884c39663SGreg Clayton                                                uint32_t option_idx,
6984c39663SGreg Clayton                                                const char *option_arg)
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':
93*5275aaa0SVince 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 
9868ebae61SGreg Clayton         case 'P':
99*5275aaa0SVince Harron             ptr_depth = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
10084c39663SGreg Clayton             if (!success)
10186edbf41SGreg Clayton                 error.SetErrorStringWithFormat("invalid pointer depth '%s'", option_arg);
10284c39663SGreg Clayton             break;
10384c39663SGreg Clayton 
1040c5ef693SEnrico Granata         case 'Y':
1050c5ef693SEnrico Granata             if (option_arg)
1060c5ef693SEnrico Granata             {
107*5275aaa0SVince Harron                 no_summary_depth = StringConvert::ToUInt32 (option_arg, 0, 0, &success);
1080c5ef693SEnrico Granata                 if (!success)
10986edbf41SGreg Clayton                     error.SetErrorStringWithFormat("invalid pointer depth '%s'", option_arg);
1100c5ef693SEnrico Granata             }
1110c5ef693SEnrico Granata             else
1120c5ef693SEnrico Granata                 no_summary_depth = 1;
1130c5ef693SEnrico Granata             break;
1140c5ef693SEnrico Granata 
115d55546b2SEnrico Granata         case 'S':
116d55546b2SEnrico Granata             use_synth = Args::StringToBoolean(option_arg, true, &success);
117d55546b2SEnrico Granata             if (!success)
11886edbf41SGreg Clayton                 error.SetErrorStringWithFormat("invalid synthetic-type '%s'", option_arg);
119d55546b2SEnrico Granata             break;
1200f883ffbSEnrico Granata 
1210f883ffbSEnrico Granata         case 'V':
1220f883ffbSEnrico Granata             run_validator = Args::StringToBoolean(option_arg, true, &success);
1230f883ffbSEnrico Granata             if (!success)
1240f883ffbSEnrico Granata                 error.SetErrorStringWithFormat("invalid validate '%s'", option_arg);
1250f883ffbSEnrico Granata             break;
1260f883ffbSEnrico Granata 
12784c39663SGreg Clayton         default:
12886edbf41SGreg Clayton             error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
12984c39663SGreg Clayton             break;
13084c39663SGreg Clayton     }
13184c39663SGreg Clayton 
13284c39663SGreg Clayton     return error;
13384c39663SGreg Clayton }
13484c39663SGreg Clayton 
13584c39663SGreg Clayton void
13684c39663SGreg Clayton OptionGroupValueObjectDisplay::OptionParsingStarting (CommandInterpreter &interpreter)
13784c39663SGreg Clayton {
13882f4cf46SGreg Clayton     // If these defaults change, be sure to modify AnyOptionWasSet().
13984c39663SGreg Clayton     show_types        = false;
1400c5ef693SEnrico Granata     no_summary_depth  = 0;
14184c39663SGreg Clayton     show_location     = false;
14284c39663SGreg Clayton     flat_output       = false;
14384c39663SGreg Clayton     use_objc          = false;
14484c39663SGreg Clayton     max_depth         = UINT32_MAX;
14584c39663SGreg Clayton     ptr_depth         = 0;
146d55546b2SEnrico Granata     use_synth         = true;
147ce68b02cSEnrico Granata     be_raw            = false;
14822c55d18SEnrico Granata     ignore_cap        = false;
1490f883ffbSEnrico Granata     run_validator     = false;
15084c39663SGreg Clayton 
151c14ee32dSGreg Clayton     Target *target = interpreter.GetExecutionContext().GetTargetPtr();
152d78c9576SEd Maste     if (target != nullptr)
1532837b766SJim Ingham         use_dynamic = target->GetPreferDynamicValue();
1542837b766SJim Ingham     else
1552837b766SJim Ingham     {
1562837b766SJim Ingham         // If we don't have any targets, then dynamic values won't do us much good.
1572837b766SJim Ingham         use_dynamic = lldb::eNoDynamicValues;
1582837b766SJim Ingham     }
1592837b766SJim Ingham }
1609fb5ab55SEnrico Granata 
1614d93b8cdSEnrico Granata DumpValueObjectOptions
1624d93b8cdSEnrico Granata OptionGroupValueObjectDisplay::GetAsDumpOptions (LanguageRuntimeDescriptionDisplayVerbosity lang_descr_verbosity,
1639fb5ab55SEnrico Granata                                                  lldb::Format format,
1649fb5ab55SEnrico Granata                                                  lldb::TypeSummaryImplSP summary_sp)
1659fb5ab55SEnrico Granata {
1664d93b8cdSEnrico Granata     DumpValueObjectOptions options;
1679fb5ab55SEnrico Granata     options.SetMaximumPointerDepth(ptr_depth);
1689fb5ab55SEnrico Granata     if (use_objc)
1699fb5ab55SEnrico Granata         options.SetShowSummary(false);
1709fb5ab55SEnrico Granata     else
1719fb5ab55SEnrico Granata         options.SetOmitSummaryDepth(no_summary_depth);
1729fb5ab55SEnrico Granata     options.SetMaximumDepth(max_depth)
1739fb5ab55SEnrico Granata     .SetShowTypes(show_types)
1749fb5ab55SEnrico Granata     .SetShowLocation(show_location)
1759fb5ab55SEnrico Granata     .SetUseObjectiveC(use_objc)
1769fb5ab55SEnrico Granata     .SetUseDynamicType(use_dynamic)
1779fb5ab55SEnrico Granata     .SetUseSyntheticValue(use_synth)
1789fb5ab55SEnrico Granata     .SetFlatOutput(flat_output)
1799fb5ab55SEnrico Granata     .SetIgnoreCap(ignore_cap)
1809fb5ab55SEnrico Granata     .SetFormat(format)
1819fb5ab55SEnrico Granata     .SetSummary(summary_sp);
1829fb5ab55SEnrico Granata 
1834d93b8cdSEnrico Granata     if (lang_descr_verbosity == eLanguageRuntimeDescriptionDisplayVerbosityCompact)
1849fb5ab55SEnrico Granata         options.SetHideRootType(use_objc)
1859fb5ab55SEnrico Granata         .SetHideName(use_objc)
1869fb5ab55SEnrico Granata         .SetHideValue(use_objc);
1879fb5ab55SEnrico Granata 
1889fb5ab55SEnrico Granata     if (be_raw)
189a126e462SEnrico Granata         options.SetRawDisplay();
1909fb5ab55SEnrico Granata 
1910f883ffbSEnrico Granata     options.SetRunValidator(run_validator);
1920f883ffbSEnrico Granata 
1939fb5ab55SEnrico Granata     return options;
1949fb5ab55SEnrico Granata }
195