GDB (API)
/home/stan/gdb/src/gdb/python/lib/gdb/__init__.py
Go to the documentation of this file.
00001 # Copyright (C) 2010-2013 Free Software Foundation, Inc.
00002 
00003 # This program is free software; you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation; either version 3 of the License, or
00006 # (at your option) any later version.
00007 #
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 import traceback
00017 import os
00018 import sys
00019 import _gdb
00020 
00021 if sys.version_info[0] > 2:
00022     # Python 3 moved "reload"
00023     from imp import reload
00024 
00025 from _gdb import *
00026 
00027 class _GdbFile (object):
00028     # These two are needed in Python 3
00029     encoding = "UTF-8"
00030     errors = "strict"
00031     
00032     def close(self):
00033         # Do nothing.
00034         return None
00035 
00036     def isatty(self):
00037         return False
00038 
00039     def writelines(self, iterable):
00040         for line in iterable:
00041             self.write(line)
00042 
00043     def flush(self):
00044         flush()
00045 
00046 class GdbOutputFile (_GdbFile):
00047     def write(self, s):
00048         write(s, stream=STDOUT)
00049 
00050 sys.stdout = GdbOutputFile()
00051 
00052 class GdbOutputErrorFile (_GdbFile):
00053     def write(self, s):
00054         write(s, stream=STDERR)
00055 
00056 sys.stderr = GdbOutputErrorFile()
00057 
00058 # Default prompt hook does nothing.
00059 prompt_hook = None
00060 
00061 # Ensure that sys.argv is set to something.
00062 # We do not use PySys_SetArgvEx because it did not appear until 2.6.6.
00063 sys.argv = ['']
00064 
00065 # Initial pretty printers.
00066 pretty_printers = []
00067 
00068 # Initial type printers.
00069 type_printers = []
00070 # Initial frame filters.
00071 frame_filters = {}
00072 
00073 # Convenience variable to GDB's python directory
00074 PYTHONDIR = os.path.dirname(os.path.dirname(__file__))
00075 
00076 # Auto-load all functions/commands.
00077 
00078 # Packages to auto-load.
00079 
00080 packages = [
00081     'function',
00082     'command'
00083 ]
00084 
00085 # pkgutil.iter_modules is not available prior to Python 2.6.  Instead,
00086 # manually iterate the list, collating the Python files in each module
00087 # path.  Construct the module name, and import.
00088 
00089 def auto_load_packages():
00090     for package in packages:
00091         location = os.path.join(os.path.dirname(__file__), package)
00092         if os.path.exists(location):
00093             py_files = filter(lambda x: x.endswith('.py')
00094                                         and x != '__init__.py',
00095                               os.listdir(location))
00096 
00097             for py_file in py_files:
00098                 # Construct from foo.py, gdb.module.foo
00099                 modname = "%s.%s.%s" % ( __name__, package, py_file[:-3] )
00100                 try:
00101                     if modname in sys.modules:
00102                         # reload modules with duplicate names
00103                         reload(__import__(modname))
00104                     else:
00105                         __import__(modname)
00106                 except:
00107                     sys.stderr.write (traceback.format_exc() + "\n")
00108 
00109 auto_load_packages()
00110 
00111 def GdbSetPythonDirectory(dir):
00112     """Update sys.path, reload gdb and auto-load packages."""
00113     global PYTHONDIR
00114 
00115     try:
00116         sys.path.remove(PYTHONDIR)
00117     except ValueError:
00118         pass
00119     sys.path.insert(0, dir)
00120 
00121     PYTHONDIR = dir
00122 
00123     # note that reload overwrites the gdb module without deleting existing
00124     # attributes
00125     reload(__import__(__name__))
00126     auto_load_packages()
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines