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', required_argument, NULL, 0, eArgTypeFormat , "Specify a format to be used for display."}, 44 { LLDB_OPT_SET_2, false, "gdb-format",'G', required_argument, NULL, 0, eArgTypeGDBFormat, "Specify a format using a GDB format specifier string."}, 45 { LLDB_OPT_SET_3, false, "size" ,'s', required_argument, NULL, 0, eArgTypeByteSize , "The size in bytes to use when displaying with the selected format."}, 46 { LLDB_OPT_SET_4, false, "count" ,'c', required_argument, NULL, 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.SetValueFromCString (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.SetValueFromCString (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.SetValueFromCString (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 = NULL; 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 if (byte_size > 0) 160 { 161 error.SetErrorString ("this command doesn't support specifying a byte size"); 162 return error; 163 } 164 } 165 166 if (count_enabled) 167 { 168 // Count is enabled and was not set, set it to the default for gdb format statements (which is 1). 169 if (count == 0) 170 count = 1; 171 } 172 else 173 { 174 // Count is disabled, make sure it wasn't specified 175 if (count > 0) 176 { 177 error.SetErrorString ("this command doesn't support specifying a count"); 178 return error; 179 } 180 } 181 182 m_format.SetCurrentValue (format); 183 m_format.SetOptionWasSet (); 184 if (byte_size_enabled) 185 { 186 m_byte_size.SetCurrentValue (byte_size); 187 m_byte_size.SetOptionWasSet (); 188 } 189 if (count_enabled) 190 { 191 m_count.SetCurrentValue(count); 192 m_count.SetOptionWasSet (); 193 } 194 } 195 break; 196 197 default: 198 error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option); 199 break; 200 } 201 202 return error; 203 } 204 205 bool 206 OptionGroupFormat::ParserGDBFormatLetter (CommandInterpreter &interpreter, char format_letter, Format &format, uint32_t &byte_size) 207 { 208 m_has_gdb_format = true; 209 switch (format_letter) 210 { 211 case 'o': format = eFormatOctal; m_prev_gdb_format = format_letter; return true; 212 case 'x': format = eFormatHex; m_prev_gdb_format = format_letter; return true; 213 case 'd': format = eFormatDecimal; m_prev_gdb_format = format_letter; return true; 214 case 'u': format = eFormatUnsigned; m_prev_gdb_format = format_letter; return true; 215 case 't': format = eFormatBinary; m_prev_gdb_format = format_letter; return true; 216 case 'f': format = eFormatFloat; m_prev_gdb_format = format_letter; return true; 217 case 'a': format = eFormatAddressInfo; 218 { 219 ExecutionContext exe_ctx(interpreter.GetExecutionContext()); 220 Target *target = exe_ctx.GetTargetPtr(); 221 if (target) 222 byte_size = target->GetArchitecture().GetAddressByteSize(); 223 m_prev_gdb_format = format_letter; 224 return true; 225 } 226 case 'i': format = eFormatInstruction; m_prev_gdb_format = format_letter; return true; 227 case 'c': format = eFormatChar; m_prev_gdb_format = format_letter; return true; 228 case 's': format = eFormatCString; m_prev_gdb_format = format_letter; return true; 229 case 'T': format = eFormatOSType; m_prev_gdb_format = format_letter; return true; 230 case 'A': format = eFormatHexFloat; m_prev_gdb_format = format_letter; return true; 231 case 'b': byte_size = 1; m_prev_gdb_size = format_letter; return true; 232 case 'h': byte_size = 2; m_prev_gdb_size = format_letter; return true; 233 case 'w': byte_size = 4; m_prev_gdb_size = format_letter; return true; 234 case 'g': byte_size = 8; m_prev_gdb_size = format_letter; return true; 235 default: break; 236 } 237 return false; 238 } 239 240 void 241 OptionGroupFormat::OptionParsingStarting (CommandInterpreter &interpreter) 242 { 243 m_format.Clear(); 244 m_byte_size.Clear(); 245 m_count.Clear(); 246 m_has_gdb_format = false; 247 } 248