gdb/python/lib/gdb/__init__.py - gdb

Global variables defined

Functions defined

Source code

  1. # Copyright (C) 2010-2015 Free Software Foundation, Inc.

  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program.  If not, see <http://www.gnu.org/licenses/>.

  14. import traceback
  15. import os
  16. import sys
  17. import _gdb

  18. if sys.version_info[0] > 2:
  19.     # Python 3 moved "reload"
  20.     from imp import reload

  21. from _gdb import *

  22. class _GdbFile (object):
  23.     # These two are needed in Python 3
  24.     encoding = "UTF-8"
  25.     errors = "strict"

  26.     def close(self):
  27.         # Do nothing.
  28.         return None

  29.     def isatty(self):
  30.         return False

  31.     def writelines(self, iterable):
  32.         for line in iterable:
  33.             self.write(line)

  34.     def flush(self):
  35.         flush()

  36. class GdbOutputFile (_GdbFile):
  37.     def write(self, s):
  38.         write(s, stream=STDOUT)

  39. sys.stdout = GdbOutputFile()

  40. class GdbOutputErrorFile (_GdbFile):
  41.     def write(self, s):
  42.         write(s, stream=STDERR)

  43. sys.stderr = GdbOutputErrorFile()

  44. # Default prompt hook does nothing.
  45. prompt_hook = None

  46. # Ensure that sys.argv is set to something.
  47. # We do not use PySys_SetArgvEx because it did not appear until 2.6.6.
  48. sys.argv = ['']

  49. # Initial pretty printers.
  50. pretty_printers = []

  51. # Initial type printers.
  52. type_printers = []
  53. # Initial xmethod matchers.
  54. xmethods = []
  55. # Initial frame filters.
  56. frame_filters = {}

  57. # Convenience variable to GDB's python directory
  58. PYTHONDIR = os.path.dirname(os.path.dirname(__file__))

  59. # Auto-load all functions/commands.

  60. # Packages to auto-load.

  61. packages = [
  62.     'function',
  63.     'command',
  64.     'printer'
  65. ]

  66. # pkgutil.iter_modules is not available prior to Python 2.6.  Instead,
  67. # manually iterate the list, collating the Python files in each module
  68. # path.  Construct the module name, and import.

  69. def auto_load_packages():
  70.     for package in packages:
  71.         location = os.path.join(os.path.dirname(__file__), package)
  72.         if os.path.exists(location):
  73.             py_files = filter(lambda x: x.endswith('.py')
  74.                                         and x != '__init__.py',
  75.                               os.listdir(location))

  76.             for py_file in py_files:
  77.                 # Construct from foo.py, gdb.module.foo
  78.                 modname = "%s.%s.%s" % ( __name__, package, py_file[:-3] )
  79.                 try:
  80.                     if modname in sys.modules:
  81.                         # reload modules with duplicate names
  82.                         reload(__import__(modname))
  83.                     else:
  84.                         __import__(modname)
  85.                 except:
  86.                     sys.stderr.write (traceback.format_exc() + "\n")

  87. auto_load_packages()

  88. def GdbSetPythonDirectory(dir):
  89.     """Update sys.path, reload gdb and auto-load packages."""
  90.     global PYTHONDIR

  91.     try:
  92.         sys.path.remove(PYTHONDIR)
  93.     except ValueError:
  94.         pass
  95.     sys.path.insert(0, dir)

  96.     PYTHONDIR = dir

  97.     # note that reload overwrites the gdb module without deleting existing
  98.     # attributes
  99.     reload(__import__(__name__))
  100.     auto_load_packages()