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