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(uint32_t option_idx,
70                                   const char *option_arg,
71                                   ExecutionContext *execution_context)
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 (execution_context,
127                                               gdb_format_cstr[0], format,
128                                               byte_size))
129                 {
130                     ++gdb_format_cstr;
131                 }
132 
133                 // We the first character of the "gdb_format_cstr" is not the
134                 // NULL terminator, we didn't consume the entire string and
135                 // something is wrong. Also, if none of the format, size or count
136                 // was specified correctly, then abort.
137                 if (gdb_format_cstr[0] || (format == eFormatInvalid && byte_size == 0 && count == 0))
138                 {
139                     // Nothing got set correctly
140                     error.SetErrorStringWithFormat ("invalid gdb format string '%s'", option_arg);
141                     return error;
142                 }
143 
144                 // At least one of the format, size or count was set correctly.
145                 // Anything that wasn't set correctly should be set to the
146                 // previous default
147                 if (format == eFormatInvalid)
148                     ParserGDBFormatLetter (execution_context, m_prev_gdb_format,
149                                            format, byte_size);
150 
151                 const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX;
152                 const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX;
153                 if (byte_size_enabled)
154                 {
155                     // Byte size is enabled
156                     if (byte_size == 0)
157                         ParserGDBFormatLetter (execution_context, m_prev_gdb_size, format, byte_size);
158                 }
159                 else
160                 {
161                     // Byte size is disabled, make sure it wasn't specified
162                     // but if this is an address, it's actually necessary to
163                     // specify one so don't error out
164                     if (byte_size > 0 && format != lldb::eFormatAddressInfo)
165                     {
166                         error.SetErrorString ("this command doesn't support specifying a byte size");
167                         return error;
168                     }
169                 }
170 
171                 if (count_enabled)
172                 {
173                     // Count is enabled and was not set, set it to the default for gdb format statements (which is 1).
174                     if (count == 0)
175                         count = 1;
176                 }
177                 else
178                 {
179                     // Count is disabled, make sure it wasn't specified
180                     if (count > 0)
181                     {
182                         error.SetErrorString ("this command doesn't support specifying a count");
183                         return error;
184                     }
185                 }
186 
187                 m_format.SetCurrentValue (format);
188                 m_format.SetOptionWasSet ();
189                 if (byte_size_enabled)
190                 {
191                     m_byte_size.SetCurrentValue (byte_size);
192                     m_byte_size.SetOptionWasSet ();
193                 }
194                 if (count_enabled)
195                 {
196                     m_count.SetCurrentValue(count);
197                     m_count.SetOptionWasSet ();
198                 }
199             }
200             break;
201 
202         default:
203             error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
204             break;
205     }
206 
207     return error;
208 }
209 
210 bool
211 OptionGroupFormat::ParserGDBFormatLetter(ExecutionContext *execution_context,
212                                          char format_letter, Format &format,
213                                          uint32_t &byte_size)
214 {
215     m_has_gdb_format = true;
216     switch (format_letter)
217     {
218         case 'o': format = eFormatOctal;        m_prev_gdb_format = format_letter; return true;
219         case 'x': format = eFormatHex;          m_prev_gdb_format = format_letter; return true;
220         case 'd': format = eFormatDecimal;      m_prev_gdb_format = format_letter; return true;
221         case 'u': format = eFormatUnsigned;     m_prev_gdb_format = format_letter; return true;
222         case 't': format = eFormatBinary;       m_prev_gdb_format = format_letter; return true;
223         case 'f': format = eFormatFloat;        m_prev_gdb_format = format_letter; return true;
224         case 'a': format = eFormatAddressInfo;
225         {
226             TargetSP target_sp = execution_context ?
227                 execution_context->GetTargetSP() : TargetSP();
228             if (target_sp)
229                 byte_size = target_sp->GetArchitecture().GetAddressByteSize();
230             m_prev_gdb_format = format_letter;
231             return true;
232         }
233         case 'i': format = eFormatInstruction;  m_prev_gdb_format = format_letter; return true;
234         case 'c': format = eFormatChar;         m_prev_gdb_format = format_letter; return true;
235         case 's': format = eFormatCString;      m_prev_gdb_format = format_letter; return true;
236         case 'T': format = eFormatOSType;       m_prev_gdb_format = format_letter; return true;
237         case 'A': format = eFormatHexFloat;     m_prev_gdb_format = format_letter; return true;
238 
239         // Size isn't used for printing instructions, so if a size is specified, and the previous format was
240         // 'i', then we should reset it to the default ('x').  Otherwise we'll continue to print as instructions,
241         // which isn't expected.
242         case 'b':
243             byte_size = 1;
244             LLVM_FALLTHROUGH;
245         case 'h':
246             byte_size = 2;
247             LLVM_FALLTHROUGH;
248         case 'w':
249             byte_size = 4;
250             LLVM_FALLTHROUGH;
251         case 'g':
252             byte_size = 8;
253 
254             m_prev_gdb_size = format_letter;
255             if (m_prev_gdb_format == 'i')
256                 m_prev_gdb_format = 'x';
257             return true;
258 
259             break;
260         default:  break;
261     }
262     return false;
263 }
264 
265 void
266 OptionGroupFormat::OptionParsingStarting(ExecutionContext *execution_context)
267 {
268     m_format.Clear();
269     m_byte_size.Clear();
270     m_count.Clear();
271     m_has_gdb_format = false;
272 }
273