OLD | NEW |
1 # Pretty-printer utilities. | 1 # Pretty-printer utilities. |
2 # Copyright (C) 2010-2012 Free Software Foundation, Inc. | 2 # Copyright (C) 2010-2012 Free Software Foundation, Inc. |
3 | 3 |
4 # This program is free software; you can redistribute it and/or modify | 4 # This program is free software; you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License as published by | 5 # it under the terms of the GNU General Public License as published by |
6 # the Free Software Foundation; either version 3 of the License, or | 6 # the Free Software Foundation; either version 3 of the License, or |
7 # (at your option) any later version. | 7 # (at your option) any later version. |
8 # | 8 # |
9 # This program is distributed in the hope that it will be useful, | 9 # This program is distributed in the hope that it will be useful, |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 199 |
200 # Iterate over table of type regexps to determine | 200 # Iterate over table of type regexps to determine |
201 # if a printer is registered for that type. | 201 # if a printer is registered for that type. |
202 # Return an instantiation of the printer if found. | 202 # Return an instantiation of the printer if found. |
203 for printer in self.subprinters: | 203 for printer in self.subprinters: |
204 if printer.enabled and printer.compiled_re.search(typename): | 204 if printer.enabled and printer.compiled_re.search(typename): |
205 return printer.gen_printer(val) | 205 return printer.gen_printer(val) |
206 | 206 |
207 # Cannot find a pretty printer. Return None. | 207 # Cannot find a pretty printer. Return None. |
208 return None | 208 return None |
| 209 |
| 210 # A helper class for printing enum types. This class is instantiated |
| 211 # with a list of enumerators to print a particular Value. |
| 212 class _EnumInstance: |
| 213 def __init__(self, enumerators, val): |
| 214 self.enumerators = enumerators |
| 215 self.val = val |
| 216 |
| 217 def to_string(self): |
| 218 flag_list = [] |
| 219 v = long(self.val) |
| 220 any_found = False |
| 221 for (e_name, e_value) in self.enumerators: |
| 222 if v & e_value != 0: |
| 223 flag_list.append(e_name) |
| 224 v = v & ~e_value |
| 225 any_found = True |
| 226 if not any_found or v != 0: |
| 227 # Leftover value. |
| 228 flag_list.append('<unknown: 0x%x>' % v) |
| 229 return "0x%x [%s]" % (self.val, " | ".join(flag_list)) |
| 230 |
| 231 class FlagEnumerationPrinter(PrettyPrinter): |
| 232 """A pretty-printer which can be used to print a flag-style enumeration. |
| 233 A flag-style enumeration is one where the enumerators are or'd |
| 234 together to create values. The new printer will print these |
| 235 symbolically using '|' notation. The printer must be registered |
| 236 manually. This printer is most useful when an enum is flag-like, |
| 237 but has some overlap. GDB's built-in printing will not handle |
| 238 this case, but this printer will attempt to.""" |
| 239 |
| 240 def __init__(self, enum_type): |
| 241 super(FlagEnumerationPrinter, self).__init__(enum_type) |
| 242 self.initialized = False |
| 243 |
| 244 def __call__(self, val): |
| 245 if not self.initialized: |
| 246 self.initialized = True |
| 247 flags = gdb.lookup_type(self.name) |
| 248 self.enumerators = [] |
| 249 for field in flags.fields(): |
| 250 self.enumerators.append((field.name, field.enumval)) |
| 251 # Sorting the enumerators by value usually does the right |
| 252 # thing. |
| 253 self.enumerators.sort(key = lambda x: x.enumval) |
| 254 |
| 255 if self.enabled: |
| 256 return _EnumInstance(self.enumerators, val) |
| 257 else: |
| 258 return None |
OLD | NEW |