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/Interpreter/OptionGroupFormat.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Core/ArchSpec.h" 17 #include "lldb/Interpreter/CommandInterpreter.h" 18 #include "lldb/Target/ExecutionContext.h" 19 #include "lldb/Target/Target.h" 20 #include "lldb/Utility/Utils.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 OptionGroupFormat::OptionGroupFormat (lldb::Format default_format, 26 uint64_t default_byte_size, 27 uint64_t default_count) : 28 m_format (default_format, default_format), 29 m_byte_size (default_byte_size, default_byte_size), 30 m_count (default_count, default_count), 31 m_prev_gdb_format('x'), 32 m_prev_gdb_size('w') 33 { 34 } 35 36 OptionGroupFormat::~OptionGroupFormat () 37 { 38 } 39 40 static OptionDefinition 41 g_option_table[] = 42 { 43 { LLDB_OPT_SET_1, false, "format" ,'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeFormat , "Specify a format to be used for display."}, 44 { LLDB_OPT_SET_2, false, "gdb-format",'G', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeGDBFormat, "Specify a format using a GDB format specifier string."}, 45 { LLDB_OPT_SET_3, false, "size" ,'s', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeByteSize , "The size in bytes to use when displaying with the selected format."}, 46 { LLDB_OPT_SET_4, false, "count" ,'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeCount , "The number of total items to display."}, 47 }; 48 49 uint32_t 50 OptionGroupFormat::GetNumDefinitions () 51 { 52 if (m_byte_size.GetDefaultValue() < UINT64_MAX) 53 { 54 if (m_count.GetDefaultValue() < UINT64_MAX) 55 return 4; 56 else 57 return 3; 58 } 59 return 2; 60 } 61 62 const OptionDefinition * 63 OptionGroupFormat::GetDefinitions () 64 { 65 return g_option_table; 66 } 67 68 Error 69 OptionGroupFormat::SetOptionValue (CommandInterpreter &interpreter, 70 uint32_t option_idx, 71 const char *option_arg) 72 { 73 Error error; 74 const int short_option = g_option_table[option_idx].short_option; 75 76 switch (short_option) 77 { 78 case 'f': 79 error = m_format.SetValueFromString (option_arg); 80 break; 81 82 case 'c': 83 if (m_count.GetDefaultValue() == 0) 84 { 85 error.SetErrorString ("--count option is disabled"); 86 } 87 else 88 { 89 error = m_count.SetValueFromString (option_arg); 90 if (m_count.GetCurrentValue() == 0) 91 error.SetErrorStringWithFormat("invalid --count option value '%s'", option_arg); 92 } 93 break; 94 95 case 's': 96 if (m_byte_size.GetDefaultValue() == 0) 97 { 98 error.SetErrorString ("--size option is disabled"); 99 } 100 else 101 { 102 error = m_byte_size.SetValueFromString (option_arg); 103 if (m_byte_size.GetCurrentValue() == 0) 104 error.SetErrorStringWithFormat("invalid --size option value '%s'", option_arg); 105 } 106 break; 107 108 case 'G': 109 { 110 char *end = nullptr; 111 const char *gdb_format_cstr = option_arg; 112 uint64_t count = 0; 113 if (::isdigit (gdb_format_cstr[0])) 114 { 115 count = strtoull (gdb_format_cstr, &end, 0); 116 117 if (option_arg != end) 118 gdb_format_cstr = end; // We have a valid count, advance the string position 119 else 120 count = 0; 121 } 122 123 Format format = eFormatDefault; 124 uint32_t byte_size = 0; 125 126 while (ParserGDBFormatLetter (interpreter, gdb_format_cstr[0], format, byte_size)) 127 { 128 ++gdb_format_cstr; 129 } 130 131 // We the first character of the "gdb_format_cstr" is not the 132 // NULL terminator, we didn't consume the entire string and 133 // something is wrong. Also, if none of the format, size or count 134 // was specified correctly, then abort. 135 if (gdb_format_cstr[0] || (format == eFormatInvalid && byte_size == 0 && count == 0)) 136 { 137 // Nothing got set correctly 138 error.SetErrorStringWithFormat ("invalid gdb format string '%s'", option_arg); 139 return error; 140 } 141 142 // At least one of the format, size or count was set correctly. 143 // Anything that wasn't set correctly should be set to the 144 // previous default 145 if (format == eFormatInvalid) 146 ParserGDBFormatLetter (interpreter, m_prev_gdb_format, format, byte_size); 147 148 const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX; 149 const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX; 150 if (byte_size_enabled) 151 { 152 // Byte size is enabled 153 if (byte_size == 0) 154 ParserGDBFormatLetter (interpreter, m_prev_gdb_size, format, byte_size); 155 } 156 else 157 { 158 // Byte size is disabled, make sure it wasn't specified 159 // but if this is an address, it's actually necessary to 160 // specify one so don't error out 161 if (byte_size > 0 && format != lldb::eFormatAddressInfo) 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