Ben Cheng | 09ebc13 | 2013-10-18 00:02:49 -0700 | [diff] [blame] | 1 | # Type printer commands. |
| 2 | # Copyright (C) 2010-2013 Free Software Foundation, Inc. |
| 3 | |
| 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 |
| 6 | # the Free Software Foundation; either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | import copy |
| 18 | import gdb |
| 19 | |
| 20 | """GDB commands for working with type-printers.""" |
| 21 | |
| 22 | class InfoTypePrinter(gdb.Command): |
| 23 | """GDB command to list all registered type-printers. |
| 24 | |
| 25 | Usage: info type-printers |
| 26 | """ |
| 27 | |
| 28 | def __init__ (self): |
| 29 | super(InfoTypePrinter, self).__init__("info type-printers", |
| 30 | gdb.COMMAND_DATA) |
| 31 | |
| 32 | def list_type_printers(self, type_printers): |
| 33 | """Print a list of type printers.""" |
| 34 | # A potential enhancement is to provide an option to list printers in |
| 35 | # "lookup order" (i.e. unsorted). |
| 36 | sorted_type_printers = sorted (copy.copy(type_printers), |
| 37 | key = lambda x: x.name) |
| 38 | for printer in sorted_type_printers: |
| 39 | if printer.enabled: |
| 40 | enabled = '' |
| 41 | else: |
| 42 | enabled = " [disabled]" |
| 43 | print (" %s%s" % (printer.name, enabled)) |
| 44 | |
| 45 | def invoke(self, arg, from_tty): |
| 46 | """GDB calls this to perform the command.""" |
| 47 | sep = '' |
| 48 | for objfile in gdb.objfiles(): |
| 49 | if objfile.type_printers: |
| 50 | print ("%sType printers for %s:" % (sep, objfile.name)) |
| 51 | self.list_type_printers(objfile.type_printers) |
| 52 | sep = '\n' |
| 53 | if gdb.current_progspace().type_printers: |
| 54 | print ("%sType printers for program space:" % sep) |
| 55 | self.list_type_printers(gdb.current_progspace().type_printers) |
| 56 | sep = '\n' |
| 57 | if gdb.type_printers: |
| 58 | print ("%sGlobal type printers:" % sep) |
| 59 | self.list_type_printers(gdb.type_printers) |
| 60 | |
| 61 | class _EnableOrDisableCommand(gdb.Command): |
| 62 | def __init__(self, setting, name): |
| 63 | super(_EnableOrDisableCommand, self).__init__(name, gdb.COMMAND_DATA) |
| 64 | self.setting = setting |
| 65 | |
| 66 | def set_some(self, name, printers): |
| 67 | result = False |
| 68 | for p in printers: |
| 69 | if name == p.name: |
| 70 | p.enabled = self.setting |
| 71 | result = True |
| 72 | return result |
| 73 | |
| 74 | def invoke(self, arg, from_tty): |
| 75 | """GDB calls this to perform the command.""" |
| 76 | for name in arg.split(): |
| 77 | ok = False |
| 78 | for objfile in gdb.objfiles(): |
| 79 | if self.set_some(name, objfile.type_printers): |
| 80 | ok = True |
| 81 | if self.set_some(name, gdb.current_progspace().type_printers): |
| 82 | ok = True |
| 83 | if self.set_some(name, gdb.type_printers): |
| 84 | ok = True |
| 85 | if not ok: |
| 86 | print ("No type printer named '%s'" % name) |
| 87 | |
| 88 | def add_some(self, result, word, printers): |
| 89 | for p in printers: |
| 90 | if p.name.startswith(word): |
| 91 | result.append(p.name) |
| 92 | |
| 93 | def complete(self, text, word): |
| 94 | result = [] |
| 95 | for objfile in gdb.objfiles(): |
| 96 | self.add_some(result, word, objfile.type_printers) |
| 97 | self.add_some(result, word, gdb.current_progspace().type_printers) |
| 98 | self.add_some(result, word, gdb.type_printers) |
| 99 | return result |
| 100 | |
| 101 | class EnableTypePrinter(_EnableOrDisableCommand): |
| 102 | """GDB command to enable the specified type printer. |
| 103 | |
| 104 | Usage: enable type-printer NAME |
| 105 | |
| 106 | NAME is the name of the type-printer. |
| 107 | """ |
| 108 | |
| 109 | def __init__(self): |
| 110 | super(EnableTypePrinter, self).__init__(True, "enable type-printer") |
| 111 | |
| 112 | class DisableTypePrinter(_EnableOrDisableCommand): |
| 113 | """GDB command to disable the specified type-printer. |
| 114 | |
| 115 | Usage: disable type-printer NAME |
| 116 | |
| 117 | NAME is the name of the type-printer. |
| 118 | """ |
| 119 | |
| 120 | def __init__(self): |
| 121 | super(DisableTypePrinter, self).__init__(False, "disable type-printer") |
| 122 | |
| 123 | InfoTypePrinter() |
| 124 | EnableTypePrinter() |
| 125 | DisableTypePrinter() |