]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/lib/gdb/command/type_printers.py
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / python / lib / gdb / command / type_printers.py
CommitLineData
18a9fc12 1# Type printer commands.
213516ef 2# Copyright (C) 2010-2023 Free Software Foundation, Inc.
18a9fc12
TT
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
17import copy
18import gdb
19
20"""GDB commands for working with type-printers."""
21
13123da8 22
18a9fc12
TT
23class InfoTypePrinter(gdb.Command):
24 """GDB command to list all registered type-printers.
25
13123da8 26 Usage: info type-printers"""
18a9fc12 27
13123da8
SM
28 def __init__(self):
29 super(InfoTypePrinter, self).__init__("info type-printers", gdb.COMMAND_DATA)
18a9fc12
TT
30
31 def list_type_printers(self, type_printers):
32 """Print a list of type printers."""
33 # A potential enhancement is to provide an option to list printers in
34 # "lookup order" (i.e. unsorted).
13123da8 35 sorted_type_printers = sorted(copy.copy(type_printers), key=lambda x: x.name)
18a9fc12
TT
36 for printer in sorted_type_printers:
37 if printer.enabled:
13123da8 38 enabled = ""
18a9fc12
TT
39 else:
40 enabled = " [disabled]"
13123da8 41 print(" %s%s" % (printer.name, enabled))
18a9fc12
TT
42
43 def invoke(self, arg, from_tty):
44 """GDB calls this to perform the command."""
13123da8 45 sep = ""
18a9fc12
TT
46 for objfile in gdb.objfiles():
47 if objfile.type_printers:
13123da8 48 print("%sType printers for %s:" % (sep, objfile.filename))
18a9fc12 49 self.list_type_printers(objfile.type_printers)
13123da8 50 sep = "\n"
18a9fc12 51 if gdb.current_progspace().type_printers:
13123da8 52 print("%sType printers for program space:" % sep)
18a9fc12 53 self.list_type_printers(gdb.current_progspace().type_printers)
13123da8 54 sep = "\n"
18a9fc12 55 if gdb.type_printers:
13123da8 56 print("%sGlobal type printers:" % sep)
18a9fc12
TT
57 self.list_type_printers(gdb.type_printers)
58
13123da8 59
18a9fc12
TT
60class _EnableOrDisableCommand(gdb.Command):
61 def __init__(self, setting, name):
62 super(_EnableOrDisableCommand, self).__init__(name, gdb.COMMAND_DATA)
63 self.setting = setting
64
65 def set_some(self, name, printers):
66 result = False
67 for p in printers:
68 if name == p.name:
69 p.enabled = self.setting
70 result = True
71 return result
72
73 def invoke(self, arg, from_tty):
74 """GDB calls this to perform the command."""
75 for name in arg.split():
76 ok = False
77 for objfile in gdb.objfiles():
78 if self.set_some(name, objfile.type_printers):
79 ok = True
80 if self.set_some(name, gdb.current_progspace().type_printers):
81 ok = True
82 if self.set_some(name, gdb.type_printers):
83 ok = True
84 if not ok:
13123da8 85 print("No type printer named '%s'" % name)
18a9fc12
TT
86
87 def add_some(self, result, word, printers):
88 for p in printers:
89 if p.name.startswith(word):
90 result.append(p.name)
91
92 def complete(self, text, word):
93 result = []
94 for objfile in gdb.objfiles():
95 self.add_some(result, word, objfile.type_printers)
96 self.add_some(result, word, gdb.current_progspace().type_printers)
97 self.add_some(result, word, gdb.type_printers)
98 return result
99
13123da8 100
18a9fc12
TT
101class EnableTypePrinter(_EnableOrDisableCommand):
102 """GDB command to enable the specified type printer.
103
13123da8 104 Usage: enable type-printer NAME
18a9fc12 105
13123da8 106 NAME is the name of the type-printer."""
18a9fc12
TT
107
108 def __init__(self):
109 super(EnableTypePrinter, self).__init__(True, "enable type-printer")
110
13123da8 111
18a9fc12
TT
112class DisableTypePrinter(_EnableOrDisableCommand):
113 """GDB command to disable the specified type-printer.
114
13123da8 115 Usage: disable type-printer NAME
18a9fc12 116
13123da8 117 NAME is the name of the type-printer."""
18a9fc12
TT
118
119 def __init__(self):
120 super(DisableTypePrinter, self).__init__(False, "disable type-printer")
121
13123da8 122
18a9fc12
TT
123InfoTypePrinter()
124EnableTypePrinter()
125DisableTypePrinter()