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 21 using namespace lldb; 22 using namespace lldb_private; 23 24 OptionGroupFormat::OptionGroupFormat(lldb::Format default_format, 25 uint64_t default_byte_size, 26 uint64_t default_count) 27 : m_format(default_format, default_format), 28 m_byte_size(default_byte_size, default_byte_size), 29 m_count(default_count, default_count), m_prev_gdb_format('x'), 30 m_prev_gdb_size('w') {} 31 32 OptionGroupFormat::~OptionGroupFormat() {} 33 34 static OptionDefinition g_option_table[] = { 35 {LLDB_OPT_SET_1, false, "format", 'f', OptionParser::eRequiredArgument, 36 nullptr, nullptr, 0, eArgTypeFormat, 37 "Specify a format to be used for display."}, 38 {LLDB_OPT_SET_2, false, "gdb-format", 'G', OptionParser::eRequiredArgument, 39 nullptr, nullptr, 0, eArgTypeGDBFormat, 40 "Specify a format using a GDB format specifier string."}, 41 {LLDB_OPT_SET_3, false, "size", 's', OptionParser::eRequiredArgument, 42 nullptr, nullptr, 0, eArgTypeByteSize, 43 "The size in bytes to use when displaying with the selected format."}, 44 {LLDB_OPT_SET_4, false, "count", 'c', OptionParser::eRequiredArgument, 45 nullptr, nullptr, 0, eArgTypeCount, 46 "The number of total items to display."}, 47 }; 48 49 llvm::ArrayRef<OptionDefinition> OptionGroupFormat::GetDefinitions() { 50 auto result = llvm::makeArrayRef(g_option_table); 51 if (m_byte_size.GetDefaultValue() < UINT64_MAX) { 52 if (m_count.GetDefaultValue() < UINT64_MAX) 53 return result; 54 else 55 return result.take_front(3); 56 } 57 return result.take_front(2); 58 } 59 60 Error OptionGroupFormat::SetOptionValue(uint32_t option_idx, 61 llvm::StringRef option_arg, 62 ExecutionContext *execution_context) { 63 Error error; 64 const int short_option = g_option_table[option_idx].short_option; 65 66 switch (short_option) { 67 case 'f': 68 error = m_format.SetValueFromString(option_arg); 69 break; 70 71 case 'c': 72 if (m_count.GetDefaultValue() == 0) { 73 error.SetErrorString("--count option is disabled"); 74 } else { 75 error = m_count.SetValueFromString(option_arg); 76 if (m_count.GetCurrentValue() == 0) 77 error.SetErrorStringWithFormat("invalid --count option value '%s'", 78 option_arg.str().c_str()); 79 } 80 break; 81 82 case 's': 83 if (m_byte_size.GetDefaultValue() == 0) { 84 error.SetErrorString("--size option is disabled"); 85 } else { 86 error = m_byte_size.SetValueFromString(option_arg); 87 if (m_byte_size.GetCurrentValue() == 0) 88 error.SetErrorStringWithFormat("invalid --size option value '%s'", 89 option_arg.str().c_str()); 90 } 91 break; 92 93 case 'G': { 94 uint64_t count = 0; 95 llvm::StringRef gdb_format_str = option_arg; 96 gdb_format_str.consumeInteger(0, count); 97 98 Format format = eFormatDefault; 99 uint32_t byte_size = 0; 100 101 while (!gdb_format_str.empty() && 102 ParserGDBFormatLetter(execution_context, gdb_format_str[0], format, 103 byte_size)) { 104 gdb_format_str = gdb_format_str.drop_front(); 105 } 106 107 // We the first character of the "gdb_format_str" is not the 108 // NULL terminator, we didn't consume the entire string and 109 // something is wrong. Also, if none of the format, size or count 110 // was specified correctly, then abort. 111 if (!gdb_format_str.empty() || 112 (format == eFormatInvalid && byte_size == 0 && count == 0)) { 113 // Nothing got set correctly 114 error.SetErrorStringWithFormat("invalid gdb format string '%s'", 115 option_arg.str().c_str()); 116 return error; 117 } 118 119 // At least one of the format, size or count was set correctly. 120 // Anything that wasn't set correctly should be set to the 121 // previous default 122 if (format == eFormatInvalid) 123 ParserGDBFormatLetter(execution_context, m_prev_gdb_format, format, 124 byte_size); 125 126 const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX; 127 const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX; 128 if (byte_size_enabled) { 129 // Byte size is enabled 130 if (byte_size == 0) 131 ParserGDBFormatLetter(execution_context, m_prev_gdb_size, format, 132 byte_size); 133 } else { 134 // Byte size is disabled, make sure it wasn't specified 135 // but if this is an address, it's actually necessary to 136 // specify one so don't error out 137 if (byte_size > 0 && format != lldb::eFormatAddressInfo) { 138 error.SetErrorString( 139 "this command doesn't support specifying a byte size"); 140 return error; 141 } 142 } 143 144 if (count_enabled) { 145 // Count is enabled and was not set, set it to the default for gdb format 146 // statements (which is 1). 147 if (count == 0) 148 count = 1; 149 } else { 150 // Count is disabled, make sure it wasn't specified 151 if (count > 0) { 152 error.SetErrorString("this command doesn't support specifying a count"); 153 return error; 154 } 155 } 156 157 m_format.SetCurrentValue(format); 158 m_format.SetOptionWasSet(); 159 if (byte_size_enabled) { 160 m_byte_size.SetCurrentValue(byte_size); 161 m_byte_size.SetOptionWasSet(); 162 } 163 if (count_enabled) { 164 m_count.SetCurrentValue(count); 165 m_count.SetOptionWasSet(); 166 } 167 } break; 168 169 default: 170 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); 171 break; 172 } 173 174 return error; 175 } 176 177 bool OptionGroupFormat::ParserGDBFormatLetter( 178 ExecutionContext *execution_context, char format_letter, Format &format, 179 uint32_t &byte_size) { 180 m_has_gdb_format = true; 181 switch (format_letter) { 182 case 'o': 183 format = eFormatOctal; 184 m_prev_gdb_format = format_letter; 185 return true; 186 case 'x': 187 format = eFormatHex; 188 m_prev_gdb_format = format_letter; 189 return true; 190 case 'd': 191 format = eFormatDecimal; 192 m_prev_gdb_format = format_letter; 193 return true; 194 case 'u': 195 format = eFormatUnsigned; 196 m_prev_gdb_format = format_letter; 197 return true; 198 case 't': 199 format = eFormatBinary; 200 m_prev_gdb_format = format_letter; 201 return true; 202 case 'f': 203 format = eFormatFloat; 204 m_prev_gdb_format = format_letter; 205 return true; 206 case 'a': 207 format = eFormatAddressInfo; 208 { 209 TargetSP target_sp = 210 execution_context ? execution_context->GetTargetSP() : TargetSP(); 211 if (target_sp) 212 byte_size = target_sp->GetArchitecture().GetAddressByteSize(); 213 m_prev_gdb_format = format_letter; 214 return true; 215 } 216 case 'i': 217 format = eFormatInstruction; 218 m_prev_gdb_format = format_letter; 219 return true; 220 case 'c': 221 format = eFormatChar; 222 m_prev_gdb_format = format_letter; 223 return true; 224 case 's': 225 format = eFormatCString; 226 m_prev_gdb_format = format_letter; 227 return true; 228 case 'T': 229 format = eFormatOSType; 230 m_prev_gdb_format = format_letter; 231 return true; 232 case 'A': 233 format = eFormatHexFloat; 234 m_prev_gdb_format = format_letter; 235 return true; 236 237 case 'b': 238 case 'h': 239 case 'w': 240 case 'g': 241 { 242 // Size isn't used for printing instructions, so if a size is specified, and 243 // the previous format was 244 // 'i', then we should reset it to the default ('x'). Otherwise we'll 245 // continue to print as instructions, 246 // which isn't expected. 247 if (format_letter == 'b') 248 byte_size = 1; 249 else if (format_letter == 'h') 250 byte_size = 2; 251 else if (format_letter == 'w') 252 byte_size = 4; 253 else if (format_letter == 'g') 254 byte_size = 8; 255 256 m_prev_gdb_size = format_letter; 257 if (m_prev_gdb_format == 'i') 258 m_prev_gdb_format = 'x'; 259 return true; 260 } 261 break; 262 default: 263 break; 264 } 265 266 267 return false; 268 } 269 270 void OptionGroupFormat::OptionParsingStarting( 271 ExecutionContext *execution_context) { 272 m_format.Clear(); 273 m_byte_size.Clear(); 274 m_count.Clear(); 275 m_has_gdb_format = false; 276 } 277