1# Copyright (C) 2010-2013 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
16import traceback
17import os
18import sys
19import _gdb
20
21if sys.version_info[0] > 2:
22    # Python 3 moved "reload"
23    from imp import reload
24
25from _gdb import *
26
27class _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
46class GdbOutputFile (_GdbFile):
47    def write(self, s):
48        write(s, stream=STDOUT)
49
50sys.stdout = GdbOutputFile()
51
52class GdbOutputErrorFile (_GdbFile):
53    def write(self, s):
54        write(s, stream=STDERR)
55
56sys.stderr = GdbOutputErrorFile()
57
58# Default prompt hook does nothing.
59prompt_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.
63sys.argv = ['']
64
65# Initial pretty printers.
66pretty_printers = []
67
68# Initial type printers.
69type_printers = []
70
71# Convenience variable to GDB's python directory
72PYTHONDIR = os.path.dirname(os.path.dirname(__file__))
73
74# Auto-load all functions/commands.
75
76# Packages to auto-load.
77
78packages = [
79    'function',
80    'command'
81]
82
83# pkgutil.iter_modules is not available prior to Python 2.6.  Instead,
84# manually iterate the list, collating the Python files in each module
85# path.  Construct the module name, and import.
86
87def auto_load_packages():
88    for package in packages:
89        location = os.path.join(os.path.dirname(__file__), package)
90        if os.path.exists(location):
91            py_files = filter(lambda x: x.endswith('.py')
92                                        and x != '__init__.py',
93                              os.listdir(location))
94
95            for py_file in py_files:
96                # Construct from foo.py, gdb.module.foo
97                modname = "%s.%s.%s" % ( __name__, package, py_file[:-3] )
98                try:
99                    if modname in sys.modules:
100                        # reload modules with duplicate names
101                        reload(__import__(modname))
102                    else:
103                        __import__(modname)
104                except:
105                    sys.stderr.write (traceback.format_exc() + "\n")
106
107auto_load_packages()
108
109def GdbSetPythonDirectory(dir):
110    """Update sys.path, reload gdb and auto-load packages."""
111    global PYTHONDIR
112
113    try:
114        sys.path.remove(PYTHONDIR)
115    except ValueError:
116        pass
117    sys.path.insert(0, dir)
118
119    PYTHONDIR = dir
120
121    # note that reload overwrites the gdb module without deleting existing
122    # attributes
123    reload(__import__(__name__))
124    auto_load_packages()
125