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