Home | History | Annotate | Download | only in Interpreter
      1 //===-- OptionGroupFormat.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/OptionGroupFormat.h"
     13 
     14 // C Includes
     15 // C++ Includes
     16 // Other libraries and framework includes
     17 // Project includes
     18 #include "lldb/Core/ArchSpec.h"
     19 #include "lldb/Interpreter/CommandInterpreter.h"
     20 #include "lldb/Target/ExecutionContext.h"
     21 #include "lldb/Target/Target.h"
     22 #include "lldb/Utility/Utils.h"
     23 
     24 using namespace lldb;
     25 using namespace lldb_private;
     26 
     27 OptionGroupFormat::OptionGroupFormat (lldb::Format default_format,
     28                                       uint64_t default_byte_size,
     29                                       uint64_t default_count) :
     30     m_format (default_format, default_format),
     31     m_byte_size (default_byte_size, default_byte_size),
     32     m_count (default_count, default_count),
     33     m_prev_gdb_format('x'),
     34     m_prev_gdb_size('w')
     35 {
     36 }
     37 
     38 OptionGroupFormat::~OptionGroupFormat ()
     39 {
     40 }
     41 
     42 static OptionDefinition
     43 g_option_table[] =
     44 {
     45 { LLDB_OPT_SET_1, false, "format"    ,'f', required_argument, NULL, 0, eArgTypeFormat   , "Specify a format to be used for display."},
     46 { LLDB_OPT_SET_2, false, "gdb-format",'G', required_argument, NULL, 0, eArgTypeGDBFormat, "Specify a format using a GDB format specifier string."},
     47 { LLDB_OPT_SET_3, false, "size"      ,'s', required_argument, NULL, 0, eArgTypeByteSize , "The size in bytes to use when displaying with the selected format."},
     48 { LLDB_OPT_SET_4, false, "count"     ,'c', required_argument, NULL, 0, eArgTypeCount    , "The number of total items to display."},
     49 };
     50 
     51 uint32_t
     52 OptionGroupFormat::GetNumDefinitions ()
     53 {
     54     if (m_byte_size.GetDefaultValue() < UINT64_MAX)
     55     {
     56         if (m_count.GetDefaultValue() < UINT64_MAX)
     57             return 4;
     58         else
     59             return 3;
     60     }
     61     return 2;
     62 }
     63 
     64 const OptionDefinition *
     65 OptionGroupFormat::GetDefinitions ()
     66 {
     67     return g_option_table;
     68 }
     69 
     70 Error
     71 OptionGroupFormat::SetOptionValue (CommandInterpreter &interpreter,
     72                                    uint32_t option_idx,
     73                                    const char *option_arg)
     74 {
     75     Error error;
     76     const int short_option = g_option_table[option_idx].short_option;
     77 
     78     switch (short_option)
     79     {
     80         case 'f':
     81             error = m_format.SetValueFromCString (option_arg);
     82             break;
     83 
     84         case 'c':
     85             if (m_count.GetDefaultValue() == 0)
     86             {
     87                 error.SetErrorString ("--count option is disabled");
     88             }
     89             else
     90             {
     91                 error = m_count.SetValueFromCString (option_arg);
     92                 if (m_count.GetCurrentValue() == 0)
     93                     error.SetErrorStringWithFormat("invalid --count option value '%s'", option_arg);
     94             }
     95             break;
     96 
     97         case 's':
     98             if (m_byte_size.GetDefaultValue() == 0)
     99             {
    100                 error.SetErrorString ("--size option is disabled");
    101             }
    102             else
    103             {
    104                 error = m_byte_size.SetValueFromCString (option_arg);
    105                 if (m_byte_size.GetCurrentValue() == 0)
    106                     error.SetErrorStringWithFormat("invalid --size option value '%s'", option_arg);
    107             }
    108             break;
    109 
    110         case 'G':
    111             {
    112                 char *end = NULL;
    113                 const char *gdb_format_cstr = option_arg;
    114                 uint64_t count = 0;
    115                 if (::isdigit (gdb_format_cstr[0]))
    116                 {
    117                     count = strtoull (gdb_format_cstr, &end, 0);
    118 
    119                     if (option_arg != end)
    120                         gdb_format_cstr = end;  // We have a valid count, advance the string position
    121                     else
    122                         count = 0;
    123                 }
    124 
    125                 Format format = eFormatDefault;
    126                 uint32_t byte_size = 0;
    127 
    128                 while (ParserGDBFormatLetter (interpreter, gdb_format_cstr[0], format, byte_size))
    129                 {
    130                     ++gdb_format_cstr;
    131                 }
    132 
    133                 // We the first character of the "gdb_format_cstr" is not the
    134                 // NULL terminator, we didn't consume the entire string and
    135                 // something is wrong. Also, if none of the format, size or count
    136                 // was specified correctly, then abort.
    137                 if (gdb_format_cstr[0] || (format == eFormatInvalid && byte_size == 0 && count == 0))
    138                 {
    139                     // Nothing got set correctly
    140                     error.SetErrorStringWithFormat ("invalid gdb format string '%s'", option_arg);
    141                     return error;
    142                 }
    143 
    144                 // At least one of the format, size or count was set correctly.
    145                 // Anything that wasn't set correctly should be set to the
    146                 // previous default
    147                 if (format == eFormatInvalid)
    148                     ParserGDBFormatLetter (interpreter, m_prev_gdb_format, format, byte_size);
    149 
    150                 const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX;
    151                 const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX;
    152                 if (byte_size_enabled)
    153                 {
    154                     // Byte size is enabled
    155                     if (byte_size == 0)
    156                         ParserGDBFormatLetter (interpreter, m_prev_gdb_size, format, byte_size);
    157                 }
    158                 else
    159                 {
    160                     // Byte size is disabled, make sure it wasn't specified
    161                     if (byte_size > 0)
    162                     {
    163                         error.SetErrorString ("this command doesn't support specifying a byte size");
    164                         return error;
    165                     }
    166                 }
    167 
    168                 if (count_enabled)
    169                 {
    170                     // Count is enabled and was not set, set it to the default for gdb format statements (which is 1).
    171                     if (count == 0)
    172                         count = 1;
    173                 }
    174                 else
    175                 {
    176                     // Count is disabled, make sure it wasn't specified
    177                     if (count > 0)
    178                     {
    179                         error.SetErrorString ("this command doesn't support specifying a count");
    180                         return error;
    181                     }
    182                 }
    183 
    184                 m_format.SetCurrentValue (format);
    185                 m_format.SetOptionWasSet ();
    186                 if (byte_size_enabled)
    187                 {
    188                     m_byte_size.SetCurrentValue (byte_size);
    189                     m_byte_size.SetOptionWasSet ();
    190                 }
    191                 if (count_enabled)
    192                 {
    193                     m_count.SetCurrentValue(count);
    194                     m_count.SetOptionWasSet ();
    195                 }
    196             }
    197             break;
    198 
    199         default:
    200             error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
    201             break;
    202     }
    203 
    204     return error;
    205 }
    206 
    207 bool
    208 OptionGroupFormat::ParserGDBFormatLetter (CommandInterpreter &interpreter, char format_letter, Format &format, uint32_t &byte_size)
    209 {
    210     m_has_gdb_format = true;
    211     switch (format_letter)
    212     {
    213         case 'o': format = eFormatOctal;        m_prev_gdb_format = format_letter; return true;
    214         case 'x': format = eFormatHex;          m_prev_gdb_format = format_letter; return true;
    215         case 'd': format = eFormatDecimal;      m_prev_gdb_format = format_letter; return true;
    216         case 'u': format = eFormatUnsigned;     m_prev_gdb_format = format_letter; return true;
    217         case 't': format = eFormatBinary;       m_prev_gdb_format = format_letter; return true;
    218         case 'f': format = eFormatFloat;        m_prev_gdb_format = format_letter; return true;
    219         case 'a': format = eFormatAddressInfo;
    220         {
    221             ExecutionContext exe_ctx(interpreter.GetExecutionContext());
    222             Target *target = exe_ctx.GetTargetPtr();
    223             if (target)
    224                 byte_size = target->GetArchitecture().GetAddressByteSize();
    225             m_prev_gdb_format = format_letter;
    226             return true;
    227         }
    228         case 'i': format = eFormatInstruction;  m_prev_gdb_format = format_letter; return true;
    229         case 'c': format = eFormatChar;         m_prev_gdb_format = format_letter; return true;
    230         case 's': format = eFormatCString;      m_prev_gdb_format = format_letter; return true;
    231         case 'T': format = eFormatOSType;       m_prev_gdb_format = format_letter; return true;
    232         case 'A': format = eFormatHexFloat;     m_prev_gdb_format = format_letter; return true;
    233         case 'b': byte_size = 1;                m_prev_gdb_size = format_letter;   return true;
    234         case 'h': byte_size = 2;                m_prev_gdb_size = format_letter;   return true;
    235         case 'w': byte_size = 4;                m_prev_gdb_size = format_letter;   return true;
    236         case 'g': byte_size = 8;                m_prev_gdb_size = format_letter;   return true;
    237         default:  break;
    238     }
    239     return false;
    240 }
    241 
    242 void
    243 OptionGroupFormat::OptionParsingStarting (CommandInterpreter &interpreter)
    244 {
    245     m_format.Clear();
    246     m_byte_size.Clear();
    247     m_count.Clear();
    248     m_has_gdb_format = false;
    249 }
    250