]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/lldb/source/Interpreter/OptionGroupFormat.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / lldb / source / Interpreter / OptionGroupFormat.cpp
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/lldb-python.h"
11
12 #include "lldb/Interpreter/OptionGroupFormat.h"
13
14 // C Includes
15 // C++ Includes
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/Core/ArchSpec.h"
19 #include "lldb/Interpreter/CommandInterpreter.h"
20 #include "lldb/Target/ExecutionContext.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/Utils.h"
23
24 using namespace lldb;
25 using namespace lldb_private;
26
27 OptionGroupFormat::OptionGroupFormat (lldb::Format default_format,
28                                       uint64_t default_byte_size,
29                                       uint64_t default_count) :
30     m_format (default_format, default_format),
31     m_byte_size (default_byte_size, default_byte_size),
32     m_count (default_count, default_count),
33     m_prev_gdb_format('x'),
34     m_prev_gdb_size('w')
35 {
36 }
37
38 OptionGroupFormat::~OptionGroupFormat ()
39 {
40 }
41
42 static OptionDefinition 
43 g_option_table[] =
44 {
45 { LLDB_OPT_SET_1, false, "format"    ,'f', OptionParser::eRequiredArgument, NULL, 0, eArgTypeFormat   , "Specify a format to be used for display."},
46 { LLDB_OPT_SET_2, false, "gdb-format",'G', OptionParser::eRequiredArgument, NULL, 0, eArgTypeGDBFormat, "Specify a format using a GDB format specifier string."},
47 { LLDB_OPT_SET_3, false, "size"      ,'s', OptionParser::eRequiredArgument, NULL, 0, eArgTypeByteSize , "The size in bytes to use when displaying with the selected format."},
48 { LLDB_OPT_SET_4, false, "count"     ,'c', OptionParser::eRequiredArgument, NULL, 0, eArgTypeCount    , "The number of total items to display."},
49 };
50
51 uint32_t
52 OptionGroupFormat::GetNumDefinitions ()
53 {
54     if (m_byte_size.GetDefaultValue() < UINT64_MAX)
55     {
56         if (m_count.GetDefaultValue() < UINT64_MAX)
57             return 4;
58         else
59             return 3;
60     }
61     return 2;
62 }
63
64 const OptionDefinition *
65 OptionGroupFormat::GetDefinitions ()
66 {
67     return g_option_table;
68 }
69
70 Error
71 OptionGroupFormat::SetOptionValue (CommandInterpreter &interpreter,
72                                    uint32_t option_idx,
73                                    const char *option_arg)
74 {
75     Error error;
76     const int short_option = g_option_table[option_idx].short_option;
77
78     switch (short_option)
79     {
80         case 'f':
81             error = m_format.SetValueFromCString (option_arg);
82             break;
83
84         case 'c':
85             if (m_count.GetDefaultValue() == 0)
86             {
87                 error.SetErrorString ("--count option is disabled");
88             }
89             else
90             {
91                 error = m_count.SetValueFromCString (option_arg);
92                 if (m_count.GetCurrentValue() == 0)
93                     error.SetErrorStringWithFormat("invalid --count option value '%s'", option_arg);
94             }
95             break;
96             
97         case 's':
98             if (m_byte_size.GetDefaultValue() == 0)
99             {
100                 error.SetErrorString ("--size option is disabled");
101             }
102             else
103             {
104                 error = m_byte_size.SetValueFromCString (option_arg);
105                 if (m_byte_size.GetCurrentValue() == 0)
106                     error.SetErrorStringWithFormat("invalid --size option value '%s'", option_arg);
107             }
108             break;
109
110         case 'G':
111             {
112                 char *end = NULL;
113                 const char *gdb_format_cstr = option_arg; 
114                 uint64_t count = 0;
115                 if (::isdigit (gdb_format_cstr[0]))
116                 {
117                     count = strtoull (gdb_format_cstr, &end, 0);
118
119                     if (option_arg != end)
120                         gdb_format_cstr = end;  // We have a valid count, advance the string position
121                     else
122                         count = 0;
123                 }
124
125                 Format format = eFormatDefault;
126                 uint32_t byte_size = 0;
127                 
128                 while (ParserGDBFormatLetter (interpreter, gdb_format_cstr[0], format, 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 (interpreter, m_prev_gdb_format, format, byte_size);
149                 
150                 const bool byte_size_enabled = m_byte_size.GetDefaultValue() < UINT64_MAX;
151                 const bool count_enabled = m_count.GetDefaultValue() < UINT64_MAX;
152                 if (byte_size_enabled)
153                 {
154                     // Byte size is enabled
155                     if (byte_size == 0)
156                         ParserGDBFormatLetter (interpreter, m_prev_gdb_size, format, byte_size);
157                 }
158                 else
159                 {
160                     // Byte size is disabled, make sure it wasn't specified
161                     // but if this is an address, it's actually necessary to
162                     // specify one so don't error out
163                     if (byte_size > 0 && format != lldb::eFormatAddressInfo)
164                     {
165                         error.SetErrorString ("this command doesn't support specifying a byte size");
166                         return error;
167                     }
168                 }
169
170                 if (count_enabled)
171                 {
172                     // Count is enabled and was not set, set it to the default for gdb format statements (which is 1).
173                     if (count == 0)
174                         count = 1;
175                 }
176                 else
177                 {
178                     // Count is disabled, make sure it wasn't specified
179                     if (count > 0)
180                     {
181                         error.SetErrorString ("this command doesn't support specifying a count");
182                         return error;
183                     }
184                 }
185
186                 m_format.SetCurrentValue (format);
187                 m_format.SetOptionWasSet ();
188                 if (byte_size_enabled)
189                 {
190                     m_byte_size.SetCurrentValue (byte_size);
191                     m_byte_size.SetOptionWasSet ();
192                 }
193                 if (count_enabled)
194                 {
195                     m_count.SetCurrentValue(count);
196                     m_count.SetOptionWasSet ();
197                 }
198             }
199             break;
200
201         default:
202             error.SetErrorStringWithFormat ("unrecognized option '%c'", short_option);
203             break;
204     }
205
206     return error;
207 }
208
209 bool
210 OptionGroupFormat::ParserGDBFormatLetter (CommandInterpreter &interpreter, char format_letter, Format &format, uint32_t &byte_size)
211 {
212     m_has_gdb_format = true;
213     switch (format_letter)
214     {
215         case 'o': format = eFormatOctal;        m_prev_gdb_format = format_letter; return true; 
216         case 'x': format = eFormatHex;          m_prev_gdb_format = format_letter; return true;
217         case 'd': format = eFormatDecimal;      m_prev_gdb_format = format_letter; return true;
218         case 'u': format = eFormatUnsigned;     m_prev_gdb_format = format_letter; return true;
219         case 't': format = eFormatBinary;       m_prev_gdb_format = format_letter; return true;
220         case 'f': format = eFormatFloat;        m_prev_gdb_format = format_letter; return true;
221         case 'a': format = eFormatAddressInfo;
222         {
223             ExecutionContext exe_ctx(interpreter.GetExecutionContext());
224             Target *target = exe_ctx.GetTargetPtr();
225             if (target)
226                 byte_size = target->GetArchitecture().GetAddressByteSize();
227             m_prev_gdb_format = format_letter;
228             return true;
229         }
230         case 'i': format = eFormatInstruction;  m_prev_gdb_format = format_letter; return true;
231         case 'c': format = eFormatChar;         m_prev_gdb_format = format_letter; return true;
232         case 's': format = eFormatCString;      m_prev_gdb_format = format_letter; return true;
233         case 'T': format = eFormatOSType;       m_prev_gdb_format = format_letter; return true;
234         case 'A': format = eFormatHexFloat;     m_prev_gdb_format = format_letter; return true;
235         case 'b': byte_size = 1;                m_prev_gdb_size = format_letter;   return true;
236         case 'h': byte_size = 2;                m_prev_gdb_size = format_letter;   return true;
237         case 'w': byte_size = 4;                m_prev_gdb_size = format_letter;   return true;
238         case 'g': byte_size = 8;                m_prev_gdb_size = format_letter;   return true;
239         default:  break;
240     }
241     return false;
242 }
243
244 void
245 OptionGroupFormat::OptionParsingStarting (CommandInterpreter &interpreter)
246 {
247     m_format.Clear();
248     m_byte_size.Clear();
249     m_count.Clear();
250     m_has_gdb_format = false;
251 }