Commit | Line | Data |
---|---|---|
ecd75fc8 | 1 | # Copyright (C) 2010-2014 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 | ||
9a27f2c6 PK |
21 | if sys.version_info[0] > 2: |
22 | # Python 3 moved "reload" | |
23 | from imp import reload | |
24 | ||
b9516fa1 YPK |
25 | from _gdb import * |
26 | ||
9a27f2c6 PK |
27 | class _GdbFile (object): |
28 | # These two are needed in Python 3 | |
29 | encoding = "UTF-8" | |
30 | errors = "strict" | |
31 | ||
b9516fa1 YPK |
32 | def close(self): |
33 | # Do nothing. | |
34 | return None | |
35 | ||
36 | def isatty(self): | |
37 | return False | |
38 | ||
b9516fa1 YPK |
39 | def writelines(self, iterable): |
40 | for line in iterable: | |
41 | self.write(line) | |
42 | ||
43 | def flush(self): | |
44 | flush() | |
45 | ||
9a27f2c6 PK |
46 | class GdbOutputFile (_GdbFile): |
47 | def write(self, s): | |
48 | write(s, stream=STDOUT) | |
b9516fa1 | 49 | |
9a27f2c6 | 50 | sys.stdout = GdbOutputFile() |
b9516fa1 | 51 | |
9a27f2c6 | 52 | class GdbOutputErrorFile (_GdbFile): |
b9516fa1 YPK |
53 | def write(self, s): |
54 | write(s, stream=STDERR) | |
55 | ||
b9516fa1 YPK |
56 | sys.stderr = GdbOutputErrorFile() |
57 | ||
58 | # Default prompt hook does nothing. | |
59 | prompt_hook = None | |
60 | ||
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. | |
63 | sys.argv = [''] | |
64 | ||
65 | # Initial pretty printers. | |
66 | pretty_printers = [] | |
67 | ||
18a9fc12 TT |
68 | # Initial type printers. |
69 | type_printers = [] | |
883964a7 SC |
70 | # Initial xmethod matchers. |
71 | xmethods = [] | |
1e611234 PM |
72 | # Initial frame filters. |
73 | frame_filters = {} | |
18a9fc12 | 74 | |
b9516fa1 YPK |
75 | # Convenience variable to GDB's python directory |
76 | PYTHONDIR = os.path.dirname(os.path.dirname(__file__)) | |
7b51bc51 | 77 | |
5e239b84 PM |
78 | # Auto-load all functions/commands. |
79 | ||
b9516fa1 | 80 | # Packages to auto-load. |
5e239b84 | 81 | |
b9516fa1 YPK |
82 | packages = [ |
83 | 'function', | |
84 | 'command' | |
85 | ] | |
5e239b84 | 86 | |
b9516fa1 YPK |
87 | # pkgutil.iter_modules is not available prior to Python 2.6. Instead, |
88 | # manually iterate the list, collating the Python files in each module | |
5e239b84 PM |
89 | # path. Construct the module name, and import. |
90 | ||
b9516fa1 YPK |
91 | def auto_load_packages(): |
92 | for package in packages: | |
93 | location = os.path.join(os.path.dirname(__file__), package) | |
94 | if os.path.exists(location): | |
95 | py_files = filter(lambda x: x.endswith('.py') | |
96 | and x != '__init__.py', | |
97 | os.listdir(location)) | |
98 | ||
99 | for py_file in py_files: | |
100 | # Construct from foo.py, gdb.module.foo | |
101 | modname = "%s.%s.%s" % ( __name__, package, py_file[:-3] ) | |
102 | try: | |
103 | if modname in sys.modules: | |
104 | # reload modules with duplicate names | |
105 | reload(__import__(modname)) | |
106 | else: | |
107 | __import__(modname) | |
108 | except: | |
9a27f2c6 | 109 | sys.stderr.write (traceback.format_exc() + "\n") |
b9516fa1 YPK |
110 | |
111 | auto_load_packages() | |
112 | ||
113 | def GdbSetPythonDirectory(dir): | |
114 | """Update sys.path, reload gdb and auto-load packages.""" | |
115 | global PYTHONDIR | |
116 | ||
117 | try: | |
118 | sys.path.remove(PYTHONDIR) | |
119 | except ValueError: | |
120 | pass | |
121 | sys.path.insert(0, dir) | |
122 | ||
123 | PYTHONDIR = dir | |
124 | ||
125 | # note that reload overwrites the gdb module without deleting existing | |
126 | # attributes | |
127 | reload(__import__(__name__)) | |
128 | auto_load_packages() |