]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/lib/gdb/__init__.py
1 # Copyright (C) 2010-2019 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 if sys
.version_info
[0] > 2:
22 # Python 3 moved "reload"
23 from imp
import reload
27 class _GdbFile (object):
28 # These two are needed in Python 3
39 def writelines(self
, iterable
):
46 class _GdbOutputFile (_GdbFile
):
48 write(s
, stream
=STDOUT
)
50 sys
.stdout
= _GdbOutputFile()
52 class _GdbOutputErrorFile (_GdbFile
):
54 write(s
, stream
=STDERR
)
56 sys
.stderr
= _GdbOutputErrorFile()
58 # Default prompt hook does nothing.
61 # Ensure that sys.argv is set to something.
62 # We do not use PySys_SetArgvEx because it did not appear until 2.6.6.
65 # Initial pretty printers.
68 # Initial type printers.
70 # Initial xmethod matchers.
72 # Initial frame filters.
74 # Initial frame unwinders.
77 def _execute_unwinders(pending_frame
):
78 """Internal function called from GDB to execute all unwinders.
80 Runs each currently enabled unwinder until it finds the one that
81 can unwind given frame.
84 pending_frame: gdb.PendingFrame instance.
86 gdb.UnwindInfo instance or None.
88 for objfile
in objfiles():
89 for unwinder
in objfile
.frame_unwinders
:
91 unwind_info
= unwinder(pending_frame
)
92 if unwind_info
is not None:
95 for unwinder
in current_progspace().frame_unwinders
:
97 unwind_info
= unwinder(pending_frame
)
98 if unwind_info
is not None:
101 for unwinder
in frame_unwinders
:
103 unwind_info
= unwinder(pending_frame
)
104 if unwind_info
is not None:
110 # Convenience variable to GDB's python directory
111 PYTHONDIR
= os
.path
.dirname(os
.path
.dirname(__file__
))
113 # Auto-load all functions/commands.
115 # Packages to auto-load.
123 # pkgutil.iter_modules is not available prior to Python 2.6. Instead,
124 # manually iterate the list, collating the Python files in each module
125 # path. Construct the module name, and import.
127 def _auto_load_packages():
128 for package
in packages
:
129 location
= os
.path
.join(os
.path
.dirname(__file__
), package
)
130 if os
.path
.exists(location
):
131 py_files
= filter(lambda x
: x
.endswith('.py')
132 and x
!= '__init__.py',
133 os
.listdir(location
))
135 for py_file
in py_files
:
136 # Construct from foo.py, gdb.module.foo
137 modname
= "%s.%s.%s" % ( __name__
, package
, py_file
[:-3] )
139 if modname
in sys
.modules
:
140 # reload modules with duplicate names
141 reload(__import__(modname
))
145 sys
.stderr
.write (traceback
.format_exc() + "\n")
147 _auto_load_packages()
149 def GdbSetPythonDirectory(dir):
150 """Update sys.path, reload gdb and auto-load packages."""
154 sys
.path
.remove(PYTHONDIR
)
157 sys
.path
.insert(0, dir)
161 # note that reload overwrites the gdb module without deleting existing
163 reload(__import__(__name__
))
164 _auto_load_packages()
166 def current_progspace():
167 "Return the current Progspace."
168 return selected_inferior().progspace
171 "Return a sequence of the current program space's objfiles."
172 return current_progspace().objfiles()
174 def solib_name (addr
):
175 """solib_name (Long) -> String.\n\
176 Return the name of the shared library holding a given address, or None."""
177 return current_progspace().solib_name(addr
)
179 def block_for_pc(pc
):
180 "Return the block containing the given pc value, or None."
181 return current_progspace().block_for_pc(pc
)
183 def find_pc_line(pc
):
184 """find_pc_line (pc) -> Symtab_and_line.
185 Return the gdb.Symtab_and_line object corresponding to the pc value."""
186 return current_progspace().find_pc_line(pc
)