]>
Commit | Line | Data |
---|---|---|
3666a048 | 1 | # Copyright (C) 2010-2021 Free Software Foundation, Inc. |
aa2e2d8d DE |
2 | |
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. | |
7 | # | |
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. | |
12 | # | |
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/>. | |
7b51bc51 | 15 | |
5e239b84 | 16 | import traceback |
b9516fa1 YPK |
17 | import os |
18 | import sys | |
19 | import _gdb | |
20 | ||
bfb9f5dc BS |
21 | # Python 3 moved "reload" |
22 | if sys.version_info >= (3, 4): | |
23 | from importlib import reload | |
24 | elif sys.version_info[0] > 2: | |
9a27f2c6 PK |
25 | from imp import reload |
26 | ||
b9516fa1 YPK |
27 | from _gdb import * |
28 | ||
13123da8 SM |
29 | |
30 | class _GdbFile(object): | |
9a27f2c6 PK |
31 | # These two are needed in Python 3 |
32 | encoding = "UTF-8" | |
33 | errors = "strict" | |
d11916aa | 34 | |
b9516fa1 YPK |
35 | def close(self): |
36 | # Do nothing. | |
37 | return None | |
38 | ||
39 | def isatty(self): | |
40 | return False | |
41 | ||
b9516fa1 YPK |
42 | def writelines(self, iterable): |
43 | for line in iterable: | |
44 | self.write(line) | |
45 | ||
46 | def flush(self): | |
47 | flush() | |
48 | ||
13123da8 SM |
49 | |
50 | class _GdbOutputFile(_GdbFile): | |
9a27f2c6 PK |
51 | def write(self, s): |
52 | write(s, stream=STDOUT) | |
b9516fa1 | 53 | |
13123da8 | 54 | |
08235187 | 55 | sys.stdout = _GdbOutputFile() |
b9516fa1 | 56 | |
13123da8 SM |
57 | |
58 | class _GdbOutputErrorFile(_GdbFile): | |
b9516fa1 YPK |
59 | def write(self, s): |
60 | write(s, stream=STDERR) | |
61 | ||
13123da8 | 62 | |
08235187 | 63 | sys.stderr = _GdbOutputErrorFile() |
b9516fa1 YPK |
64 | |
65 | # Default prompt hook does nothing. | |
66 | prompt_hook = None | |
67 | ||
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. | |
13123da8 | 70 | sys.argv = [""] |
b9516fa1 YPK |
71 | |
72 | # Initial pretty printers. | |
73 | pretty_printers = [] | |
74 | ||
18a9fc12 TT |
75 | # Initial type printers. |
76 | type_printers = [] | |
883964a7 SC |
77 | # Initial xmethod matchers. |
78 | xmethods = [] | |
1e611234 PM |
79 | # Initial frame filters. |
80 | frame_filters = {} | |
d11916aa SS |
81 | # Initial frame unwinders. |
82 | frame_unwinders = [] | |
83 | ||
13123da8 | 84 | |
08235187 | 85 | def _execute_unwinders(pending_frame): |
d11916aa SS |
86 | """Internal function called from GDB to execute all unwinders. |
87 | ||
88 | Runs each currently enabled unwinder until it finds the one that | |
89 | can unwind given frame. | |
90 | ||
91 | Arguments: | |
92 | pending_frame: gdb.PendingFrame instance. | |
4e317a76 | 93 | |
d11916aa | 94 | Returns: |
4e317a76 SM |
95 | Tuple with: |
96 | ||
224506e9 SM |
97 | [0] gdb.UnwindInfo instance |
98 | [1] Name of unwinder that claimed the frame (type `str`) | |
4e317a76 | 99 | |
224506e9 | 100 | or None, if no unwinder has claimed the frame. |
d11916aa | 101 | """ |
8743a9cd | 102 | for objfile in objfiles(): |
d11916aa SS |
103 | for unwinder in objfile.frame_unwinders: |
104 | if unwinder.enabled: | |
105 | unwind_info = unwinder(pending_frame) | |
106 | if unwind_info is not None: | |
4e317a76 | 107 | return (unwind_info, unwinder.name) |
d11916aa | 108 | |
8743a9cd | 109 | for unwinder in current_progspace().frame_unwinders: |
d11916aa SS |
110 | if unwinder.enabled: |
111 | unwind_info = unwinder(pending_frame) | |
112 | if unwind_info is not None: | |
4e317a76 | 113 | return (unwind_info, unwinder.name) |
d11916aa SS |
114 | |
115 | for unwinder in frame_unwinders: | |
116 | if unwinder.enabled: | |
117 | unwind_info = unwinder(pending_frame) | |
118 | if unwind_info is not None: | |
4e317a76 | 119 | return (unwind_info, unwinder.name) |
d11916aa SS |
120 | |
121 | return None | |
122 | ||
13123da8 | 123 | |
27204489 CB |
124 | def _execute_file(filepath): |
125 | """This function is used to replace Python 2's PyRun_SimpleFile. | |
126 | ||
127 | Loads and executes the given file. | |
128 | ||
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." | |
132 | """ | |
13123da8 | 133 | globals = sys.modules["__main__"].__dict__ |
27204489 CB |
134 | set_file = False |
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. | |
13123da8 SM |
137 | if not hasattr(globals, "__file__"): |
138 | globals["__file__"] = filepath | |
27204489 CB |
139 | set_file = True |
140 | try: | |
13123da8 | 141 | with open(filepath, "rb") as file: |
27204489 CB |
142 | # We pass globals also as locals to match what Python does |
143 | # in PyRun_SimpleFile. | |
13123da8 | 144 | compiled = compile(file.read(), filepath, "exec") |
27204489 CB |
145 | exec(compiled, globals, globals) |
146 | finally: | |
147 | if set_file: | |
13123da8 | 148 | del globals["__file__"] |
27204489 | 149 | |
18a9fc12 | 150 | |
b9516fa1 YPK |
151 | # Convenience variable to GDB's python directory |
152 | PYTHONDIR = os.path.dirname(os.path.dirname(__file__)) | |
7b51bc51 | 153 | |
5e239b84 PM |
154 | # Auto-load all functions/commands. |
155 | ||
b9516fa1 | 156 | # Packages to auto-load. |
5e239b84 | 157 | |
13123da8 | 158 | packages = ["function", "command", "printer"] |
5e239b84 | 159 | |
b9516fa1 YPK |
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 | |
5e239b84 PM |
162 | # path. Construct the module name, and import. |
163 | ||
13123da8 | 164 | |
08235187 | 165 | def _auto_load_packages(): |
b9516fa1 YPK |
166 | for package in packages: |
167 | location = os.path.join(os.path.dirname(__file__), package) | |
168 | if os.path.exists(location): | |
13123da8 SM |
169 | py_files = filter( |
170 | lambda x: x.endswith(".py") and x != "__init__.py", os.listdir(location) | |
171 | ) | |
b9516fa1 YPK |
172 | |
173 | for py_file in py_files: | |
174 | # Construct from foo.py, gdb.module.foo | |
13123da8 | 175 | modname = "%s.%s.%s" % (__name__, package, py_file[:-3]) |
b9516fa1 YPK |
176 | try: |
177 | if modname in sys.modules: | |
178 | # reload modules with duplicate names | |
179 | reload(__import__(modname)) | |
180 | else: | |
181 | __import__(modname) | |
182 | except: | |
13123da8 SM |
183 | sys.stderr.write(traceback.format_exc() + "\n") |
184 | ||
b9516fa1 | 185 | |
08235187 | 186 | _auto_load_packages() |
b9516fa1 | 187 | |
13123da8 | 188 | |
b9516fa1 YPK |
189 | def GdbSetPythonDirectory(dir): |
190 | """Update sys.path, reload gdb and auto-load packages.""" | |
191 | global PYTHONDIR | |
192 | ||
193 | try: | |
194 | sys.path.remove(PYTHONDIR) | |
195 | except ValueError: | |
196 | pass | |
197 | sys.path.insert(0, dir) | |
198 | ||
199 | PYTHONDIR = dir | |
200 | ||
201 | # note that reload overwrites the gdb module without deleting existing | |
202 | # attributes | |
203 | reload(__import__(__name__)) | |
08235187 | 204 | _auto_load_packages() |
8743a9cd | 205 | |
13123da8 | 206 | |
8743a9cd TT |
207 | def current_progspace(): |
208 | "Return the current Progspace." | |
209 | return selected_inferior().progspace | |
210 | ||
13123da8 | 211 | |
8743a9cd TT |
212 | def objfiles(): |
213 | "Return a sequence of the current program space's objfiles." | |
214 | return current_progspace().objfiles() | |
215 | ||
13123da8 SM |
216 | |
217 | def solib_name(addr): | |
8743a9cd TT |
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) | |
221 | ||
13123da8 | 222 | |
8743a9cd TT |
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) | |
226 | ||
13123da8 | 227 | |
8743a9cd TT |
228 | def find_pc_line(pc): |
229 | """find_pc_line (pc) -> Symtab_and_line. | |
13123da8 | 230 | Return the gdb.Symtab_and_line object corresponding to the pc value.""" |
8743a9cd | 231 | return current_progspace().find_pc_line(pc) |
f6474de9 | 232 | |
13123da8 | 233 | |
f6474de9 TT |
234 | try: |
235 | from pygments import formatters, lexers, highlight | |
13123da8 | 236 | |
f6474de9 TT |
237 | def colorize(filename, contents): |
238 | # Don't want any errors. | |
239 | try: | |
bdfc1e8a | 240 | lexer = lexers.get_lexer_for_filename(filename, stripnl=False) |
f6474de9 TT |
241 | formatter = formatters.TerminalFormatter() |
242 | return highlight(contents, lexer, formatter) | |
243 | except: | |
244 | return None | |
13123da8 SM |
245 | |
246 | ||
f6474de9 | 247 | except: |
13123da8 | 248 | |
f6474de9 TT |
249 | def colorize(filename, contents): |
250 | return None |