9d579bcf0bdf3eea9c3d97ae945665e7bf0a4a56
1 # Pretty-printer commands.
2 # Copyright (C) 2010-2021 Free Software Foundation, Inc.
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.
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.
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/>.
17 """GDB commands for working with pretty-printers."""
24 def parse_printer_regexps(arg
):
25 """Internal utility to parse a pretty-printer command argv.
28 arg: The arguments to the command. The format is:
29 [object-regexp [name-regexp]].
30 Individual printers in a collection are named as
31 printer-name;subprinter-name.
34 The result is a 3-tuple of compiled regular expressions, except that
35 the resulting compiled subprinter regexp is None if not provided.
38 SyntaxError: an error processing ARG
41 argv
= gdb
.string_to_argv(arg
)
43 object_regexp
= "" # match everything
44 name_regexp
= "" # match everything
47 raise SyntaxError("too many arguments")
49 object_regexp
= argv
[0]
51 name_subname
= argv
[1].split(";", 1)
52 name_regexp
= name_subname
[0]
53 if len(name_subname
) == 2:
54 subname_regexp
= name_subname
[1]
55 # That re.compile raises SyntaxError was determined empirically.
56 # We catch it and reraise it to provide a slightly more useful
57 # error message for the user.
59 object_re
= re
.compile(object_regexp
)
61 raise SyntaxError("invalid object regexp: %s" % object_regexp
)
63 name_re
= re
.compile(name_regexp
)
65 raise SyntaxError("invalid name regexp: %s" % name_regexp
)
66 if subname_regexp
is not None:
68 subname_re
= re
.compile(subname_regexp
)
70 raise SyntaxError("invalid subname regexp: %s" % subname_regexp
)
73 return (object_re
, name_re
, subname_re
)
76 def printer_enabled_p(printer
):
77 """Internal utility to see if printer (or subprinter) is enabled."""
78 if hasattr(printer
, "enabled"):
79 return printer
.enabled
84 class InfoPrettyPrinter(gdb
.Command
):
85 """GDB command to list all registered pretty-printers.
87 Usage: info pretty-printer [OBJECT-REGEXP [NAME-REGEXP]]
89 OBJECT-REGEXP is a regular expression matching the objects to list.
90 Objects are "global", the program space's file, and the objfiles within
93 NAME-REGEXP matches the name of the pretty-printer.
94 Individual printers in a collection are named as
95 printer-name;subprinter-name."""
98 super(InfoPrettyPrinter
, self
).__init
__("info pretty-printer", gdb
.COMMAND_DATA
)
101 def enabled_string(printer
):
102 """Return "" if PRINTER is enabled, otherwise " [disabled]"."""
103 if printer_enabled_p(printer
):
109 def printer_name(printer
):
110 """Return the printer's name."""
111 if hasattr(printer
, "name"):
113 if hasattr(printer
, "__name__"):
114 return printer
.__name
__
115 # This "shouldn't happen", but the public API allows for
116 # direct additions to the pretty-printer list, and we shouldn't
117 # crash because someone added a bogus printer.
118 # Plus we want to give the user a way to list unknown printers.
121 def list_pretty_printers(self
, pretty_printers
, name_re
, subname_re
):
122 """Print a list of pretty-printers."""
123 # A potential enhancement is to provide an option to list printers in
124 # "lookup order" (i.e. unsorted).
125 sorted_pretty_printers
= sorted(
126 copy
.copy(pretty_printers
), key
=self
.printer_name
128 for printer
in sorted_pretty_printers
:
129 name
= self
.printer_name(printer
)
130 enabled
= self
.enabled_string(printer
)
131 if name_re
.match(name
):
132 print(" %s%s" % (name
, enabled
))
133 if hasattr(printer
, "subprinters") and printer
.subprinters
is not None:
134 sorted_subprinters
= sorted(
135 copy
.copy(printer
.subprinters
), key
=self
.printer_name
137 for subprinter
in sorted_subprinters
:
138 if not subname_re
or subname_re
.match(subprinter
.name
):
141 % (subprinter
.name
, self
.enabled_string(subprinter
))
145 self
, title
, printer_list
, obj_name_to_match
, object_re
, name_re
, subname_re
147 """Subroutine of invoke to simplify it."""
148 if printer_list
and object_re
.match(obj_name_to_match
):
150 self
.list_pretty_printers(printer_list
, name_re
, subname_re
)
152 def invoke(self
, arg
, from_tty
):
153 """GDB calls this to perform the command."""
154 (object_re
, name_re
, subname_re
) = parse_printer_regexps(arg
)
156 "global pretty-printers:",
163 cp
= gdb
.current_progspace()
165 "progspace %s pretty-printers:" % cp
.filename
,
172 for objfile
in gdb
.objfiles():
174 "objfile %s pretty-printers:" % objfile
.filename
,
175 objfile
.pretty_printers
,
183 def count_enabled_printers(pretty_printers
):
184 """Return a 2-tuple of number of enabled and total printers."""
187 for printer
in pretty_printers
:
188 if hasattr(printer
, "subprinters") and printer
.subprinters
is not None:
189 if printer_enabled_p(printer
):
190 for subprinter
in printer
.subprinters
:
191 if printer_enabled_p(subprinter
):
193 total
+= len(printer
.subprinters
)
195 if printer_enabled_p(printer
):
198 return (enabled
, total
)
201 def count_all_enabled_printers():
202 """Return a 2-tuble of the enabled state and total number of all printers.
203 This includes subprinters.
207 (t_enabled
, t_total
) = count_enabled_printers(gdb
.pretty_printers
)
208 enabled_count
+= t_enabled
209 total_count
+= t_total
210 (t_enabled
, t_total
) = count_enabled_printers(
211 gdb
.current_progspace().pretty_printers
213 enabled_count
+= t_enabled
214 total_count
+= t_total
215 for objfile
in gdb
.objfiles():
216 (t_enabled
, t_total
) = count_enabled_printers(objfile
.pretty_printers
)
217 enabled_count
+= t_enabled
218 total_count
+= t_total
219 return (enabled_count
, total_count
)
222 def pluralize(text
, n
, suffix
="s"):
223 """Return TEXT pluralized if N != 1."""
225 return "%s%s" % (text
, suffix
)
230 def show_pretty_printer_enabled_summary():
231 """Print the number of printers enabled/disabled.
232 We count subprinters individually.
234 (enabled_count
, total_count
) = count_all_enabled_printers()
235 print("%d of %d printers enabled" % (enabled_count
, total_count
))
238 def do_enable_pretty_printer_1(pretty_printers
, name_re
, subname_re
, flag
):
239 """Worker for enabling/disabling pretty-printers.
242 pretty_printers: list of pretty-printers
243 name_re: regular-expression object to select printers
244 subname_re: regular expression object to select subprinters or None
246 flag: True for Enable, False for Disable
249 The number of printers affected.
250 This is just for informational purposes for the user.
253 for printer
in pretty_printers
:
255 hasattr(printer
, "name")
256 and name_re
.match(printer
.name
)
257 or hasattr(printer
, "__name__")
258 and name_re
.match(printer
.__name
__)
260 if hasattr(printer
, "subprinters") and printer
.subprinters
is not None:
262 # Only record printers that change state.
263 if printer_enabled_p(printer
) != flag
:
264 for subprinter
in printer
.subprinters
:
265 if printer_enabled_p(subprinter
):
267 # NOTE: We preserve individual subprinter settings.
268 printer
.enabled
= flag
270 # NOTE: Whether this actually disables the subprinter
271 # depends on whether the printer's lookup function supports
272 # the "enable" API. We can only assume it does.
273 for subprinter
in printer
.subprinters
:
274 if subname_re
.match(subprinter
.name
):
275 # Only record printers that change state.
277 printer_enabled_p(printer
)
278 and printer_enabled_p(subprinter
) != flag
281 subprinter
.enabled
= flag
283 # This printer has no subprinters.
284 # If the user does "disable pretty-printer .* .* foo"
285 # should we disable printers that don't have subprinters?
286 # How do we apply "foo" in this context? Since there is no
287 # "foo" subprinter it feels like we should skip this printer.
288 # There's still the issue of how to handle
289 # "disable pretty-printer .* .* .*", and every other variation
290 # that can match everything. For now punt and only support
291 # "disable pretty-printer .* .*" (i.e. subname is elided)
292 # to disable everything.
294 # Only record printers that change state.
295 if printer_enabled_p(printer
) != flag
:
297 printer
.enabled
= flag
301 def do_enable_pretty_printer(arg
, flag
):
302 """Internal worker for enabling/disabling pretty-printers."""
303 (object_re
, name_re
, subname_re
) = parse_printer_regexps(arg
)
306 if object_re
.match("global"):
307 total
+= do_enable_pretty_printer_1(
308 gdb
.pretty_printers
, name_re
, subname_re
, flag
310 cp
= gdb
.current_progspace()
311 if object_re
.match("progspace"):
312 total
+= do_enable_pretty_printer_1(
313 cp
.pretty_printers
, name_re
, subname_re
, flag
315 for objfile
in gdb
.objfiles():
316 if object_re
.match(objfile
.filename
):
317 total
+= do_enable_pretty_printer_1(
318 objfile
.pretty_printers
, name_re
, subname_re
, flag
325 print("%d %s %s" % (total
, pluralize("printer", total
), state
))
327 # Print the total list of printers currently enabled/disabled.
328 # This is to further assist the user in determining whether the result
329 # is expected. Since we use regexps to select it's useful.
330 show_pretty_printer_enabled_summary()
333 # Enable/Disable one or more pretty-printers.
335 # This is intended for use when a broken pretty-printer is shipped/installed
336 # and the user wants to disable that printer without disabling all the other
339 # A useful addition would be -v (verbose) to show each printer affected.
342 class EnablePrettyPrinter(gdb
.Command
):
343 """GDB command to enable the specified pretty-printer.
345 Usage: enable pretty-printer [OBJECT-REGEXP [NAME-REGEXP]]
347 OBJECT-REGEXP is a regular expression matching the objects to examine.
348 Objects are "global", the program space's file, and the objfiles within
351 NAME-REGEXP matches the name of the pretty-printer.
352 Individual printers in a collection are named as
353 printer-name;subprinter-name."""
356 super(EnablePrettyPrinter
, self
).__init
__(
357 "enable pretty-printer", gdb
.COMMAND_DATA
360 def invoke(self
, arg
, from_tty
):
361 """GDB calls this to perform the command."""
362 do_enable_pretty_printer(arg
, True)
365 class DisablePrettyPrinter(gdb
.Command
):
366 """GDB command to disable the specified pretty-printer.
368 Usage: disable pretty-printer [OBJECT-REGEXP [NAME-REGEXP]]
370 OBJECT-REGEXP is a regular expression matching the objects to examine.
371 Objects are "global", the program space's file, and the objfiles within
374 NAME-REGEXP matches the name of the pretty-printer.
375 Individual printers in a collection are named as
376 printer-name;subprinter-name."""
379 super(DisablePrettyPrinter
, self
).__init
__(
380 "disable pretty-printer", gdb
.COMMAND_DATA
383 def invoke(self
, arg
, from_tty
):
384 """GDB calls this to perform the command."""
385 do_enable_pretty_printer(arg
, False)
388 def register_pretty_printer_commands():
389 """Call from a top level script to install the pretty-printer commands."""
391 EnablePrettyPrinter()
392 DisablePrettyPrinter()
395 register_pretty_printer_commands()
This page took 0.046312 seconds and 3 git commands to generate.