1 # Missing debug related commands.
3 # Copyright 2023 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 def validate_regexp(exp
, idstring
):
23 """Compile exp into a compiler regular expression object.
26 exp: The string to compile into a re.Pattern object.
27 idstring: A string, what exp is a regexp for.
30 A re.Pattern object representing exp.
33 SyntaxError: If exp is an invalid regexp.
36 return re
.compile(exp
)
38 raise SyntaxError("Invalid %s regexp: %s." % (idstring
, exp
))
41 def parse_missing_debug_command_args(arg
):
42 """Internal utility to parse missing debug handler command argv.
45 arg: The arguments to the command. The format is:
46 [locus-regexp [name-regexp]]
49 A 2-tuple of compiled regular expressions.
52 SyntaxError: an error processing ARG
54 argv
= gdb
.string_to_argv(arg
)
57 raise SyntaxError("Too many arguments.")
61 locus_regexp
= argv
[0]
65 validate_regexp(locus_regexp
, "locus"),
66 validate_regexp(name_regexp
, "handler"),
70 class InfoMissingDebugHanders(gdb
.Command
):
71 """GDB command to list missing debug handlers.
73 Usage: info missing-debug-handlers [LOCUS-REGEXP [NAME-REGEXP]]
75 LOCUS-REGEXP is a regular expression matching the location of the
76 handler. If it is omitted, all registered handlers from all
77 loci are listed. A locus can be 'global', 'progspace' to list
78 the handlers from the current progspace, or a regular expression
79 matching filenames of progspaces.
81 NAME-REGEXP is a regular expression to filter missing debug
82 handler names. If this omitted for a specified locus, then all
83 registered handlers in the locus are listed.
87 super().__init
__("info missing-debug-handlers", gdb
.COMMAND_FILES
)
89 def list_handlers(self
, title
, handlers
, name_re
):
90 """Lists the missing debug handlers whose name matches regexp.
93 title: The line to print before the list.
94 handlers: The list of the missing debug handlers.
95 name_re: handler name filter.
100 for handler
in handlers
:
101 if name_re
.match(handler
.name
):
103 " %s%s" % (handler
.name
, "" if handler
.enabled
else " [disabled]")
106 def invoke(self
, arg
, from_tty
):
107 locus_re
, name_re
= parse_missing_debug_command_args(arg
)
109 if locus_re
.match("progspace") and locus_re
.pattern
!= "":
110 cp
= gdb
.current_progspace()
112 "Progspace %s:" % cp
.filename
, cp
.missing_debug_handlers
, name_re
115 for progspace
in gdb
.progspaces():
116 filename
= progspace
.filename
or ""
117 if locus_re
.match(filename
):
119 if progspace
== gdb
.current_progspace():
120 msg
= "Current Progspace:"
122 msg
= "Progspace <no-file>:"
124 msg
= "Progspace %s:" % filename
127 progspace
.missing_debug_handlers
,
131 # Print global handlers last, as these are invoked last.
132 if locus_re
.match("global"):
133 self
.list_handlers("Global:", gdb
.missing_debug_handlers
, name_re
)
136 def do_enable_handler1(handlers
, name_re
, flag
):
137 """Enable/disable missing debug handlers whose names match given regex.
140 handlers: The list of missing debug handlers.
141 name_re: Handler name filter.
142 flag: A boolean indicating if we should enable or disable.
145 The number of handlers affected.
148 for handler
in handlers
:
149 if name_re
.match(handler
.name
) and handler
.enabled
!= flag
:
150 handler
.enabled
= flag
155 def do_enable_handler(arg
, flag
):
156 """Enable or disable missing debug handlers."""
157 (locus_re
, name_re
) = parse_missing_debug_command_args(arg
)
159 if locus_re
.match("global"):
160 total
+= do_enable_handler1(gdb
.missing_debug_handlers
, name_re
, flag
)
161 if locus_re
.match("progspace") and locus_re
.pattern
!= "":
162 total
+= do_enable_handler1(
163 gdb
.current_progspace().missing_debug_handlers
, name_re
, flag
165 for progspace
in gdb
.progspaces():
166 filename
= progspace
.filename
or ""
167 if locus_re
.match(filename
):
168 total
+= do_enable_handler1(progspace
.missing_debug_handlers
, name_re
, flag
)
170 "%d missing debug handler%s %s"
171 % (total
, "" if total
== 1 else "s", "enabled" if flag
else "disabled")
175 class EnableMissingDebugHandler(gdb
.Command
):
176 """GDB command to enable missing debug handlers.
178 Usage: enable missing-debug-handler [LOCUS-REGEXP [NAME-REGEXP]]
180 LOCUS-REGEXP is a regular expression specifying the handlers to
181 enable. It can be 'global', 'progspace' for the current
182 progspace, or the filename for a file associated with a progspace.
184 NAME_REGEXP is a regular expression to filter handler names. If
185 this omitted for a specified locus, then all registered handlers
186 in the locus are affected.
190 super().__init
__("enable missing-debug-handler", gdb
.COMMAND_FILES
)
192 def invoke(self
, arg
, from_tty
):
193 """GDB calls this to perform the command."""
194 do_enable_handler(arg
, True)
197 class DisableMissingDebugHandler(gdb
.Command
):
198 """GDB command to disable missing debug handlers.
200 Usage: disable missing-debug-handler [LOCUS-REGEXP [NAME-REGEXP]]
202 LOCUS-REGEXP is a regular expression specifying the handlers to
203 enable. It can be 'global', 'progspace' for the current
204 progspace, or the filename for a file associated with a progspace.
206 NAME_REGEXP is a regular expression to filter handler names. If
207 this omitted for a specified locus, then all registered handlers
208 in the locus are affected.
212 super().__init
__("disable missing-debug-handler", gdb
.COMMAND_FILES
)
214 def invoke(self
, arg
, from_tty
):
215 """GDB calls this to perform the command."""
216 do_enable_handler(arg
, False)
219 def register_missing_debug_handler_commands():
220 """Installs the missing debug handler commands."""
221 InfoMissingDebugHanders()
222 EnableMissingDebugHandler()
223 DisableMissingDebugHandler()
226 register_missing_debug_handler_commands()