]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/lib/gdb/__init__.py
7b6d8701548807518db079536bfb2e3b8a0cfa0d
1 # Copyright (C) 2010-2021 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/>.
21 # Python 3 moved "reload"
22 if sys
.version_info
>= (3, 4):
23 from importlib
import reload
24 elif sys
.version_info
[0] > 2:
25 from imp
import reload
30 class _GdbFile(object):
31 # These two are needed in Python 3
42 def writelines(self
, iterable
):
50 class _GdbOutputFile(_GdbFile
):
52 write(s
, stream
=STDOUT
)
55 sys
.stdout
= _GdbOutputFile()
58 class _GdbOutputErrorFile(_GdbFile
):
60 write(s
, stream
=STDERR
)
63 sys
.stderr
= _GdbOutputErrorFile()
65 # Default prompt hook does nothing.
68 # Ensure that sys.argv is set to something.
69 # We do not use PySys_SetArgvEx because it did not appear until 2.6.6.
72 # Initial pretty printers.
75 # Initial type printers.
77 # Initial xmethod matchers.
79 # Initial frame filters.
81 # Initial frame unwinders.
85 def _execute_unwinders(pending_frame
):
86 """Internal function called from GDB to execute all unwinders.
88 Runs each currently enabled unwinder until it finds the one that
89 can unwind given frame.
92 pending_frame: gdb.PendingFrame instance.
97 [0] gdb.UnwindInfo instance
98 [1] Name of unwinder that claimed the frame (type `str`)
100 or None, if no unwinder has claimed the frame.
102 for objfile
in objfiles():
103 for unwinder
in objfile
.frame_unwinders
:
105 unwind_info
= unwinder(pending_frame
)
106 if unwind_info
is not None:
107 return (unwind_info
, unwinder
.name
)
109 for unwinder
in current_progspace().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 frame_unwinders
:
117 unwind_info
= unwinder(pending_frame
)
118 if unwind_info
is not None:
119 return (unwind_info
, unwinder
.name
)
124 def _execute_file(filepath
):
125 """This function is used to replace Python 2's PyRun_SimpleFile.
127 Loads and executes the given file.
129 We could use the runpy module, but its documentation says:
130 "Furthermore, any functions and classes defined by the executed code are
131 not guaranteed to work correctly after a runpy function has returned."
133 globals = sys
.modules
["__main__"].__dict
__
135 # Set file (if not set) so that the imported file can use it (e.g. to
136 # access file-relative paths). This matches what PyRun_SimpleFile does.
137 if not hasattr(globals, "__file__"):
138 globals["__file__"] = filepath
141 with
open(filepath
, "rb") as file:
142 # We pass globals also as locals to match what Python does
143 # in PyRun_SimpleFile.
144 compiled
= compile(file.read(), filepath
, "exec")
145 exec(compiled
, globals, globals)
148 del globals["__file__"]
151 # Convenience variable to GDB's python directory
152 PYTHONDIR
= os
.path
.dirname(os
.path
.dirname(__file__
))
154 # Auto-load all functions/commands.
156 # Packages to auto-load.
158 packages
= ["function", "command", "printer"]
160 # pkgutil.iter_modules is not available prior to Python 2.6. Instead,
161 # manually iterate the list, collating the Python files in each module
162 # path. Construct the module name, and import.
165 def _auto_load_packages():
166 for package
in packages
:
167 location
= os
.path
.join(os
.path
.dirname(__file__
), package
)
168 if os
.path
.exists(location
):
170 lambda x
: x
.endswith(".py") and x
!= "__init__.py", os
.listdir(location
)
173 for py_file
in py_files
:
174 # Construct from foo.py, gdb.module.foo
175 modname
= "%s.%s.%s" % (__name__
, package
, py_file
[:-3])
177 if modname
in sys
.modules
:
178 # reload modules with duplicate names
179 reload(__import__(modname
))
183 sys
.stderr
.write(traceback
.format_exc() + "\n")
186 _auto_load_packages()
189 def GdbSetPythonDirectory(dir):
190 """Update sys.path, reload gdb and auto-load packages."""
194 sys
.path
.remove(PYTHONDIR
)
197 sys
.path
.insert(0, dir)
201 # note that reload overwrites the gdb module without deleting existing
203 reload(__import__(__name__
))
204 _auto_load_packages()
207 def current_progspace():
208 "Return the current Progspace."
209 return selected_inferior().progspace
213 "Return a sequence of the current program space's objfiles."
214 return current_progspace().objfiles()
217 def solib_name(addr
):
218 """solib_name (Long) -> String.\n\
219 Return the name of the shared library holding a given address, or None."""
220 return current_progspace().solib_name(addr
)
223 def block_for_pc(pc
):
224 "Return the block containing the given pc value, or None."
225 return current_progspace().block_for_pc(pc
)
228 def find_pc_line(pc
):
229 """find_pc_line (pc) -> Symtab_and_line.
230 Return the gdb.Symtab_and_line object corresponding to the pc value."""
231 return current_progspace().find_pc_line(pc
)
235 from pygments
import formatters
, lexers
, highlight
237 def colorize(filename
, contents
):
238 # Don't want any errors.
240 lexer
= lexers
.get_lexer_for_filename(filename
, stripnl
=False)
241 formatter
= formatters
.TerminalFormatter()
242 return highlight(contents
, lexer
, formatter
)
249 def colorize(filename
, contents
):