14fee23f9Smrg# -*- python -*- 2*b1e83836Smrg# Copyright (C) 2009-2022 Free Software Foundation, Inc. 34fee23f9Smrg 44fee23f9Smrg# This program is free software; you can redistribute it and/or modify 54fee23f9Smrg# it under the terms of the GNU General Public License as published by 64fee23f9Smrg# the Free Software Foundation; either version 3 of the License, or 74fee23f9Smrg# (at your option) any later version. 84fee23f9Smrg# 94fee23f9Smrg# This program is distributed in the hope that it will be useful, 104fee23f9Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of 114fee23f9Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 124fee23f9Smrg# GNU General Public License for more details. 134fee23f9Smrg# 144fee23f9Smrg# You should have received a copy of the GNU General Public License 154fee23f9Smrg# along with this program. If not, see <http://www.gnu.org/licenses/>. 164fee23f9Smrg 174fee23f9Smrgimport sys 184fee23f9Smrgimport gdb 194fee23f9Smrgimport os 204fee23f9Smrgimport os.path 214fee23f9Smrg 224fee23f9Smrgpythondir = '@pythondir@' 234fee23f9Smrglibdir = '@toolexeclibdir@' 244fee23f9Smrg 254fee23f9Smrg# This file might be loaded when there is no current objfile. This 264fee23f9Smrg# can happen if the user loads it manually. In this case we don't 274fee23f9Smrg# update sys.path; instead we just hope the user managed to do that 284fee23f9Smrg# beforehand. 294fee23f9Smrgif gdb.current_objfile () is not None: 304fee23f9Smrg # Update module path. We want to find the relative path from libdir 314fee23f9Smrg # to pythondir, and then we want to apply that relative path to the 324fee23f9Smrg # directory holding the objfile with which this file is associated. 334fee23f9Smrg # This preserves relocatability of the gcc tree. 344fee23f9Smrg 354fee23f9Smrg # Do a simple normalization that removes duplicate separators. 364fee23f9Smrg pythondir = os.path.normpath (pythondir) 374fee23f9Smrg libdir = os.path.normpath (libdir) 384fee23f9Smrg 394fee23f9Smrg prefix = os.path.commonprefix ([libdir, pythondir]) 404fee23f9Smrg # In some bizarre configuration we might have found a match in the 414fee23f9Smrg # middle of a directory name. 424fee23f9Smrg if prefix[-1] != '/': 434fee23f9Smrg prefix = os.path.dirname (prefix) + '/' 444fee23f9Smrg 454fee23f9Smrg # Strip off the prefix. 464fee23f9Smrg pythondir = pythondir[len (prefix):] 474fee23f9Smrg libdir = libdir[len (prefix):] 484fee23f9Smrg 494fee23f9Smrg # Compute the ".."s needed to get from libdir to the prefix. 504fee23f9Smrg dotdots = ('..' + os.sep) * len (libdir.split (os.sep)) 514fee23f9Smrg 524fee23f9Smrg objfile = gdb.current_objfile ().filename 5348fb7bfaSmrg dir_ = os.path.join (os.path.dirname (objfile), dotdots, pythondir) 544fee23f9Smrg 5548fb7bfaSmrg if not dir_ in sys.path: 5648fb7bfaSmrg sys.path.insert(0, dir_) 574fee23f9Smrg 584d5abbe8Smrg# Call a function as a plain import would not execute body of the included file 594d5abbe8Smrg# on repeated reloads of this object file. 604d5abbe8Smrgfrom libstdcxx.v6 import register_libstdcxx_printers 614fee23f9Smrgregister_libstdcxx_printers(gdb.current_objfile()) 62