GDB (API)
|
00001 # Type printer commands. 00002 # Copyright (C) 2010-2013 Free Software Foundation, Inc. 00003 00004 # This program is free software; you can redistribute it and/or modify 00005 # it under the terms of the GNU General Public License as published by 00006 # the Free Software Foundation; either version 3 of the License, or 00007 # (at your option) any later version. 00008 # 00009 # This program is distributed in the hope that it will be useful, 00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 # GNU General Public License for more details. 00013 # 00014 # You should have received a copy of the GNU General Public License 00015 # along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 import copy 00018 import gdb 00019 00020 """GDB commands for working with type-printers.""" 00021 00022 class InfoTypePrinter(gdb.Command): 00023 """GDB command to list all registered type-printers. 00024 00025 Usage: info type-printers 00026 """ 00027 00028 def __init__ (self): 00029 super(InfoTypePrinter, self).__init__("info type-printers", 00030 gdb.COMMAND_DATA) 00031 00032 def list_type_printers(self, type_printers): 00033 """Print a list of type printers.""" 00034 # A potential enhancement is to provide an option to list printers in 00035 # "lookup order" (i.e. unsorted). 00036 sorted_type_printers = sorted (copy.copy(type_printers), 00037 key = lambda x: x.name) 00038 for printer in sorted_type_printers: 00039 if printer.enabled: 00040 enabled = '' 00041 else: 00042 enabled = " [disabled]" 00043 print (" %s%s" % (printer.name, enabled)) 00044 00045 def invoke(self, arg, from_tty): 00046 """GDB calls this to perform the command.""" 00047 sep = '' 00048 for objfile in gdb.objfiles(): 00049 if objfile.type_printers: 00050 print ("%sType printers for %s:" % (sep, objfile.name)) 00051 self.list_type_printers(objfile.type_printers) 00052 sep = '\n' 00053 if gdb.current_progspace().type_printers: 00054 print ("%sType printers for program space:" % sep) 00055 self.list_type_printers(gdb.current_progspace().type_printers) 00056 sep = '\n' 00057 if gdb.type_printers: 00058 print ("%sGlobal type printers:" % sep) 00059 self.list_type_printers(gdb.type_printers) 00060 00061 class _EnableOrDisableCommand(gdb.Command): 00062 def __init__(self, setting, name): 00063 super(_EnableOrDisableCommand, self).__init__(name, gdb.COMMAND_DATA) 00064 self.setting = setting 00065 00066 def set_some(self, name, printers): 00067 result = False 00068 for p in printers: 00069 if name == p.name: 00070 p.enabled = self.setting 00071 result = True 00072 return result 00073 00074 def invoke(self, arg, from_tty): 00075 """GDB calls this to perform the command.""" 00076 for name in arg.split(): 00077 ok = False 00078 for objfile in gdb.objfiles(): 00079 if self.set_some(name, objfile.type_printers): 00080 ok = True 00081 if self.set_some(name, gdb.current_progspace().type_printers): 00082 ok = True 00083 if self.set_some(name, gdb.type_printers): 00084 ok = True 00085 if not ok: 00086 print ("No type printer named '%s'" % name) 00087 00088 def add_some(self, result, word, printers): 00089 for p in printers: 00090 if p.name.startswith(word): 00091 result.append(p.name) 00092 00093 def complete(self, text, word): 00094 result = [] 00095 for objfile in gdb.objfiles(): 00096 self.add_some(result, word, objfile.type_printers) 00097 self.add_some(result, word, gdb.current_progspace().type_printers) 00098 self.add_some(result, word, gdb.type_printers) 00099 return result 00100 00101 class EnableTypePrinter(_EnableOrDisableCommand): 00102 """GDB command to enable the specified type printer. 00103 00104 Usage: enable type-printer NAME 00105 00106 NAME is the name of the type-printer. 00107 """ 00108 00109 def __init__(self): 00110 super(EnableTypePrinter, self).__init__(True, "enable type-printer") 00111 00112 class DisableTypePrinter(_EnableOrDisableCommand): 00113 """GDB command to disable the specified type-printer. 00114 00115 Usage: disable type-printer NAME 00116 00117 NAME is the name of the type-printer. 00118 """ 00119 00120 def __init__(self): 00121 super(DisableTypePrinter, self).__init__(False, "disable type-printer") 00122 00123 InfoTypePrinter() 00124 EnableTypePrinter() 00125 DisableTypePrinter()