7e7c299f3c7cdc098fd76c5955d96851fac7c9d1
[deliverable/binutils-gdb.git] / gdb / python / lib / gdb / __init__.py
1 # Copyright (C) 2010-2018 Free Software Foundation, Inc.
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/>.
15
16 import traceback
17 import os
18 import sys
19 import _gdb
20
21 if sys.version_info[0] > 2:
22 # Python 3 moved "reload"
23 from imp import reload
24
25 from _gdb import *
26
27 class _GdbFile (object):
28 # These two are needed in Python 3
29 encoding = "UTF-8"
30 errors = "strict"
31
32 def close(self):
33 # Do nothing.
34 return None
35
36 def isatty(self):
37 return False
38
39 def writelines(self, iterable):
40 for line in iterable:
41 self.write(line)
42
43 def flush(self):
44 flush()
45
46 class GdbOutputFile (_GdbFile):
47 def write(self, s):
48 write(s, stream=STDOUT)
49
50 sys.stdout = GdbOutputFile()
51
52 class GdbOutputErrorFile (_GdbFile):
53 def write(self, s):
54 write(s, stream=STDERR)
55
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
68 # Initial type printers.
69 type_printers = []
70 # Initial xmethod matchers.
71 xmethods = []
72 # Initial frame filters.
73 frame_filters = {}
74 # Initial frame unwinders.
75 frame_unwinders = []
76
77 def execute_unwinders(pending_frame):
78 """Internal function called from GDB to execute all unwinders.
79
80 Runs each currently enabled unwinder until it finds the one that
81 can unwind given frame.
82
83 Arguments:
84 pending_frame: gdb.PendingFrame instance.
85 Returns:
86 gdb.UnwindInfo instance or None.
87 """
88 for objfile in _gdb.objfiles():
89 for unwinder in objfile.frame_unwinders:
90 if unwinder.enabled:
91 unwind_info = unwinder(pending_frame)
92 if unwind_info is not None:
93 return unwind_info
94
95 current_progspace = _gdb.current_progspace()
96 for unwinder in current_progspace.frame_unwinders:
97 if unwinder.enabled:
98 unwind_info = unwinder(pending_frame)
99 if unwind_info is not None:
100 return unwind_info
101
102 for unwinder in frame_unwinders:
103 if unwinder.enabled:
104 unwind_info = unwinder(pending_frame)
105 if unwind_info is not None:
106 return unwind_info
107
108 return None
109
110
111 # Convenience variable to GDB's python directory
112 PYTHONDIR = os.path.dirname(os.path.dirname(__file__))
113
114 # Auto-load all functions/commands.
115
116 # Packages to auto-load.
117
118 packages = [
119 'function',
120 'command',
121 'printer'
122 ]
123
124 # pkgutil.iter_modules is not available prior to Python 2.6. Instead,
125 # manually iterate the list, collating the Python files in each module
126 # path. Construct the module name, and import.
127
128 def auto_load_packages():
129 for package in packages:
130 location = os.path.join(os.path.dirname(__file__), package)
131 if os.path.exists(location):
132 py_files = filter(lambda x: x.endswith('.py')
133 and x != '__init__.py',
134 os.listdir(location))
135
136 for py_file in py_files:
137 # Construct from foo.py, gdb.module.foo
138 modname = "%s.%s.%s" % ( __name__, package, py_file[:-3] )
139 try:
140 if modname in sys.modules:
141 # reload modules with duplicate names
142 reload(__import__(modname))
143 else:
144 __import__(modname)
145 except:
146 sys.stderr.write (traceback.format_exc() + "\n")
147
148 auto_load_packages()
149
150 def GdbSetPythonDirectory(dir):
151 """Update sys.path, reload gdb and auto-load packages."""
152 global PYTHONDIR
153
154 try:
155 sys.path.remove(PYTHONDIR)
156 except ValueError:
157 pass
158 sys.path.insert(0, dir)
159
160 PYTHONDIR = dir
161
162 # note that reload overwrites the gdb module without deleting existing
163 # attributes
164 reload(__import__(__name__))
165 auto_load_packages()
This page took 0.032995 seconds and 3 git commands to generate.