17"""GDB commands for working with pretty-printers."""
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)
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]
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)
77 """Internal utility to see if printer (or subprinter) is enabled."""
78 if hasattr(printer,
"enabled"):
79 return printer.enabled
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)
102 """Return "" if PRINTER is enabled, otherwise " [disabled]"."""
110 """Return the printer's name."""
111 if hasattr(printer,
"name"):
113 if hasattr(printer,
"__name__"):
114 return printer.__name__
122 """Print a list of pretty-printers."""
125 sorted_pretty_printers = sorted(
128 for printer
in sorted_pretty_printers:
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(
137 for subprinter
in sorted_subprinters:
138 if not subname_re
or subname_re.match(subprinter.name):
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):
153 """GDB calls this to perform the command."""
156 "global pretty-printers:",
165 "progspace %s pretty-printers:" % cp.filename,
174 "objfile %s pretty-printers:" % objfile.filename,
175 objfile.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:
190 for subprinter
in printer.subprinters:
193 total += len(printer.subprinters)
198 return (enabled, total)
202 """Return a 2-tuble of the enabled state and total number of all printers.
203 This includes subprinters.
208 enabled_count += t_enabled
209 total_count += t_total
213 enabled_count += t_enabled
214 total_count += t_total
217 enabled_count += t_enabled
218 total_count += t_total
219 return (enabled_count, total_count)
223 """Return TEXT pluralized if N != 1."""
225 return "%s%s" % (text, suffix)
231 """Print the number of printers enabled/disabled.
232 We count subprinters individually.
235 print(
"%d of %d printers enabled" % (enabled_count, total_count))
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:
264 for subprinter
in printer.subprinters:
268 printer.enabled = flag
273 for subprinter
in printer.subprinters:
274 if subname_re.match(subprinter.name):
281 subprinter.enabled = flag
297 printer.enabled = flag
302 """Internal worker for enabling/disabling pretty-printers."""
306 if object_re.match(
"global"):
308 gdb.pretty_printers, name_re, subname_re, flag
311 if object_re.match(
"progspace"):
313 cp.pretty_printers, name_re, subname_re, flag
316 if object_re.match(objfile.filename):
318 objfile.pretty_printers, name_re, subname_re, flag
325 print(
"%d %s %s" % (total,
pluralize(
"printer", total), state))
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
361 """GDB calls this to perform the 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
384 """GDB calls this to perform the command."""
389 """Call from a top level script to install the pretty-printer commands."""
invoke(self, arg, from_tty)
invoke(self, arg, from_tty)
invoke1(self, title, printer_list, obj_name_to_match, object_re, name_re, subname_re)
invoke(self, arg, from_tty)
list_pretty_printers(self, pretty_printers, name_re, subname_re)
show_pretty_printer_enabled_summary()
printer_enabled_p(printer)
count_enabled_printers(pretty_printers)
register_pretty_printer_commands()
do_enable_pretty_printer(arg, flag)
do_enable_pretty_printer_1(pretty_printers, name_re, subname_re, flag)
pluralize(text, n, suffix="s")
parse_printer_regexps(arg)
count_all_enabled_printers()