]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/lib/gdb/__init__.py
1 # Copyright (C) 2010-2024 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from contextlib
import contextmanager
24 # Python 3 moved "reload"
25 if sys
.version_info
>= (3, 4):
26 from importlib
import reload
28 from imp
import reload
32 # Historically, gdb.events was always available, so ensure it's
33 # still available without an explicit import.
34 import _gdbevents
as events
36 sys
.modules
["gdb.events"] = events
39 class _GdbFile(object):
40 # These two are needed in Python 3
44 def __init__(self
, stream
):
54 def writelines(self
, iterable
):
59 flush(stream
=self
.stream
)
62 write(s
, stream
=self
.stream
)
65 sys
.stdout
= _GdbFile(STDOUT
)
67 sys
.stderr
= _GdbFile(STDERR
)
69 # Default prompt hook does nothing.
72 # Ensure that sys.argv is set to something.
73 # We do not use PySys_SetArgvEx because it did not appear until 2.6.6.
76 # Initial pretty printers.
79 # Initial type printers.
81 # Initial xmethod matchers.
83 # Initial frame filters.
85 # Initial frame unwinders.
87 # Initial missing debug handlers.
88 missing_debug_handlers
= []
91 def _execute_unwinders(pending_frame
):
92 """Internal function called from GDB to execute all unwinders.
94 Runs each currently enabled unwinder until it finds the one that
95 can unwind given frame.
98 pending_frame: gdb.PendingFrame instance.
103 [0] gdb.UnwindInfo instance
104 [1] Name of unwinder that claimed the frame (type `str`)
106 or None, if no unwinder has claimed the frame.
108 for objfile
in objfiles():
109 for unwinder
in objfile
.frame_unwinders
:
111 unwind_info
= unwinder(pending_frame
)
112 if unwind_info
is not None:
113 return (unwind_info
, unwinder
.name
)
115 for unwinder
in current_progspace().frame_unwinders
:
117 unwind_info
= unwinder(pending_frame
)
118 if unwind_info
is not None:
119 return (unwind_info
, unwinder
.name
)
121 for unwinder
in frame_unwinders
:
123 unwind_info
= unwinder(pending_frame
)
124 if unwind_info
is not None:
125 return (unwind_info
, unwinder
.name
)
130 def _execute_file(filepath
):
131 """This function is used to replace Python 2's PyRun_SimpleFile.
133 Loads and executes the given file.
135 We could use the runpy module, but its documentation says:
136 "Furthermore, any functions and classes defined by the executed code are
137 not guaranteed to work correctly after a runpy function has returned."
139 globals = sys
.modules
["__main__"].__dict
__
141 # Set file (if not set) so that the imported file can use it (e.g. to
142 # access file-relative paths). This matches what PyRun_SimpleFile does.
143 if not hasattr(globals, "__file__"):
144 globals["__file__"] = filepath
147 with
open(filepath
, "rb") as file:
148 # We pass globals also as locals to match what Python does
149 # in PyRun_SimpleFile.
150 compiled
= compile(file.read(), filepath
, "exec")
151 exec(compiled
, globals, globals)
154 del globals["__file__"]
157 # Convenience variable to GDB's python directory
158 PYTHONDIR
= os
.path
.dirname(os
.path
.dirname(__file__
))
160 # Auto-load all functions/commands.
162 # Packages to auto-load.
164 packages
= ["function", "command", "printer"]
166 # pkgutil.iter_modules is not available prior to Python 2.6. Instead,
167 # manually iterate the list, collating the Python files in each module
168 # path. Construct the module name, and import.
171 def _auto_load_packages():
172 for package
in packages
:
173 location
= os
.path
.join(os
.path
.dirname(__file__
), package
)
174 if os
.path
.exists(location
):
176 lambda x
: x
.endswith(".py") and x
!= "__init__.py", os
.listdir(location
)
179 for py_file
in py_files
:
180 # Construct from foo.py, gdb.module.foo
181 modname
= "%s.%s.%s" % (__name__
, package
, py_file
[:-3])
183 if modname
in sys
.modules
:
184 # reload modules with duplicate names
185 reload(__import__(modname
))
189 sys
.stderr
.write(traceback
.format_exc() + "\n")
192 _auto_load_packages()
195 def GdbSetPythonDirectory(dir):
196 """Update sys.path, reload gdb and auto-load packages."""
200 sys
.path
.remove(PYTHONDIR
)
203 sys
.path
.insert(0, dir)
207 # note that reload overwrites the gdb module without deleting existing
209 reload(__import__(__name__
))
210 _auto_load_packages()
213 def current_progspace():
214 "Return the current Progspace."
215 return selected_inferior().progspace
219 "Return a sequence of the current program space's objfiles."
220 return current_progspace().objfiles()
223 def solib_name(addr
):
224 """solib_name (Long) -> String.\n\
225 Return the name of the shared library holding a given address, or None."""
226 return current_progspace().solib_name(addr
)
229 def block_for_pc(pc
):
230 "Return the block containing the given pc value, or None."
231 return current_progspace().block_for_pc(pc
)
234 def find_pc_line(pc
):
235 """find_pc_line (pc) -> Symtab_and_line.
236 Return the gdb.Symtab_and_line object corresponding to the pc value."""
237 return current_progspace().find_pc_line(pc
)
240 def set_parameter(name
, value
):
241 """Set the GDB parameter NAME to VALUE."""
242 # Handle the specific cases of None and booleans here, because
243 # gdb.parameter can return them, but they can't be passed to 'set'
247 elif isinstance(value
, bool):
252 execute("set " + name
+ " " + str(value
), to_string
=True)
256 def with_parameter(name
, value
):
257 """Temporarily set the GDB parameter NAME to VALUE.
258 Note that this is a context manager."""
259 old_value
= parameter(name
)
260 set_parameter(name
, value
)
262 # Nothing that useful to return.
265 set_parameter(name
, old_value
)
269 def blocked_signals():
270 """A helper function that blocks and unblocks signals."""
271 if not hasattr(signal
, "pthread_sigmask"):
275 to_block
= {signal
.SIGCHLD
, signal
.SIGINT
, signal
.SIGALRM
, signal
.SIGWINCH
}
276 old_mask
= signal
.pthread_sigmask(signal
.SIG_BLOCK
, to_block
)
280 signal
.pthread_sigmask(signal
.SIG_SETMASK
, old_mask
)
283 class Thread(threading
.Thread
):
284 """A GDB-specific wrapper around threading.Thread
286 This wrapper ensures that the new thread blocks any signals that
287 must be delivered on GDB's main thread."""
290 # GDB requires that these be delivered to the main thread. We
291 # do this here to avoid any possible race with the creation of
292 # the new thread. The thread mask is inherited by new
294 with
blocked_signals():
298 def _handle_missing_debuginfo(objfile
):
299 """Internal function called from GDB to execute missing debug
302 Run each of the currently registered, and enabled missing debug
303 handler objects for the current program space and then from the
304 global list. Stop after the first handler that returns a result
308 objfile: A gdb.Objfile for which GDB could not find any debug
312 None: No debug information could be found for objfile.
313 False: A handler has done all it can with objfile, but no
314 debug information could be found.
315 True: Debug information might have been installed by a
316 handler, GDB should check again.
317 A string: This is the filename of a file containing the
318 required debug information.
320 pspace
= objfile
.progspace
322 for handler
in pspace
.missing_debug_handlers
:
324 result
= handler(objfile
)
325 if result
is not None:
328 for handler
in missing_debug_handlers
:
330 result
= handler(objfile
)
331 if result
is not None: