1 1.1 mrg # -*- python -*- 2 1.12 mrg # Copyright (C) 2009-2022 Free Software Foundation, Inc. 3 1.1 mrg 4 1.1 mrg # This program is free software; you can redistribute it and/or modify 5 1.1 mrg # it under the terms of the GNU General Public License as published by 6 1.1 mrg # the Free Software Foundation; either version 3 of the License, or 7 1.1 mrg # (at your option) any later version. 8 1.1 mrg # 9 1.1 mrg # This program is distributed in the hope that it will be useful, 10 1.1 mrg # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 1.1 mrg # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 1.1 mrg # GNU General Public License for more details. 13 1.1 mrg # 14 1.1 mrg # You should have received a copy of the GNU General Public License 15 1.1 mrg # along with this program. If not, see <http://www.gnu.org/licenses/>. 16 1.1 mrg 17 1.1 mrg import sys 18 1.1 mrg import gdb 19 1.1 mrg import os 20 1.1 mrg import os.path 21 1.1 mrg 22 1.1 mrg pythondir = '@pythondir@' 23 1.1 mrg libdir = '@toolexeclibdir@' 24 1.1 mrg 25 1.1 mrg # This file might be loaded when there is no current objfile. This 26 1.1 mrg # can happen if the user loads it manually. In this case we don't 27 1.1 mrg # update sys.path; instead we just hope the user managed to do that 28 1.1 mrg # beforehand. 29 1.1 mrg if gdb.current_objfile () is not None: 30 1.1 mrg # Update module path. We want to find the relative path from libdir 31 1.1 mrg # to pythondir, and then we want to apply that relative path to the 32 1.1 mrg # directory holding the objfile with which this file is associated. 33 1.1 mrg # This preserves relocatability of the gcc tree. 34 1.1 mrg 35 1.1 mrg # Do a simple normalization that removes duplicate separators. 36 1.1 mrg pythondir = os.path.normpath (pythondir) 37 1.1 mrg libdir = os.path.normpath (libdir) 38 1.1 mrg 39 1.1 mrg prefix = os.path.commonprefix ([libdir, pythondir]) 40 1.1 mrg # In some bizarre configuration we might have found a match in the 41 1.1 mrg # middle of a directory name. 42 1.1 mrg if prefix[-1] != '/': 43 1.1 mrg prefix = os.path.dirname (prefix) + '/' 44 1.1 mrg 45 1.1 mrg # Strip off the prefix. 46 1.1 mrg pythondir = pythondir[len (prefix):] 47 1.1 mrg libdir = libdir[len (prefix):] 48 1.1 mrg 49 1.1 mrg # Compute the ".."s needed to get from libdir to the prefix. 50 1.1 mrg dotdots = ('..' + os.sep) * len (libdir.split (os.sep)) 51 1.1 mrg 52 1.1 mrg objfile = gdb.current_objfile ().filename 53 1.3 mrg dir_ = os.path.join (os.path.dirname (objfile), dotdots, pythondir) 54 1.1 mrg 55 1.3 mrg if not dir_ in sys.path: 56 1.3 mrg sys.path.insert(0, dir_) 57 1.1 mrg 58 1.5 mrg # Call a function as a plain import would not execute body of the included file 59 1.5 mrg # on repeated reloads of this object file. 60 1.5 mrg from libstdcxx.v6 import register_libstdcxx_printers 61 1.5 mrg register_libstdcxx_printers(gdb.current_objfile()) 62