vapier 14/07/30 04:16:25 Modified: lddtree.py Log: lddtree.py: rework debug handling and add a bit more throughout the processing
Revision Changes Path 1.47 pax-utils/lddtree.py file : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/pax-utils/lddtree.py?rev=1.47&view=markup plain: http://sources.gentoo.org/viewvc.cgi/gentoo-projects/pax-utils/lddtree.py?rev=1.47&content-type=text/plain diff : http://sources.gentoo.org/viewvc.cgi/gentoo-projects/pax-utils/lddtree.py?r1=1.46&r2=1.47 Index: lddtree.py =================================================================== RCS file: /var/cvsroot/gentoo-projects/pax-utils/lddtree.py,v retrieving revision 1.46 retrieving revision 1.47 diff -u -r1.46 -r1.47 --- lddtree.py 30 Jul 2014 04:07:43 -0000 1.46 +++ lddtree.py 30 Jul 2014 04:16:25 -0000 1.47 @@ -4,7 +4,7 @@ # Copyright 2012-2014 The Chromium OS Authors # Use of this source code is governed by a BSD-style license (BSD-3) # pylint: disable=C0301 -# $Header: /var/cvsroot/gentoo-projects/pax-utils/lddtree.py,v 1.46 2014/07/30 04:07:43 vapier Exp $ +# $Header: /var/cvsroot/gentoo-projects/pax-utils/lddtree.py,v 1.47 2014/07/30 04:16:25 vapier Exp $ # TODO: Handle symlinks. @@ -38,6 +38,12 @@ sys.exit(status) +def dbg(debug, *args, **kwargs): + """Pass |args| and |kwargs| to print() when |debug| is True""" + if debug: + print(*args, **kwargs) + + def bstr(buf): """Decode the byte string into a string""" return buf.decode('utf-8') @@ -243,19 +249,22 @@ elf1.header['e_machine'] == elf2.header['e_machine']) -def FindLib(elf, lib, ldpaths): +def FindLib(elf, lib, ldpaths, debug=False): """Try to locate a |lib| that is compatible to |elf| in the given |ldpaths| Args: - elf: the elf which the library should be compatible with (ELF wise) - lib: the library (basename) to search for - ldpaths: a list of paths to search + elf: The elf which the library should be compatible with (ELF wise) + lib: The library (basename) to search for + ldpaths: A list of paths to search + debug: Enable debug output Returns: the full path to the desired library """ + dbg(debug, ' FindLib(%s)' % lib) for ldpath in ldpaths: path = os.path.join(ldpath, lib) + dbg(debug, ' checking:', path) if os.path.exists(path): with open(path, 'rb') as f: libelf = ELFFile(f) @@ -265,7 +274,7 @@ def ParseELF(path, root='/', prefix='', ldpaths={'conf':[], 'env':[], 'interp':[]}, - _first=True, _all_libs={}): + debug=False, _first=True, _all_libs={}): """Parse the ELF dependency tree of the specified file Args: @@ -275,6 +284,7 @@ prefix: The path under |root| to search ldpaths: dict containing library paths to search; should have the keys: conf, env, interp + debug: Enable debug output _first: Recursive use only; is this the first ELF ? _all_libs: Recursive use only; dict of all libs we've seen @@ -307,6 +317,8 @@ 'libs': _all_libs, } + dbg(debug, 'ParseELF(%s)' % path) + with open(path, 'rb') as f: elf = ELFFile(f) @@ -317,6 +329,7 @@ continue interp = bstr(segment.get_interp_name()) + dbg(debug, ' interp =', interp) ret['interp'] = normpath(root + interp) ret['libs'][os.path.basename(interp)] = { 'path': ret['interp'], @@ -327,6 +340,7 @@ normpath(root + os.path.dirname(interp)), normpath(root + prefix + '/usr' + os.path.dirname(interp).lstrip(prefix)), ] + dbg(debug, ' ldpaths[interp] =', ldpaths['interp']) break # Parse the ELF's dynamic tags. @@ -356,6 +370,8 @@ # used at runtime to locate things. ldpaths['rpath'] = rpaths ldpaths['runpath'] = runpaths + dbg(debug, ' ldpaths[rpath] =', rpaths) + dbg(debug, ' ldpaths[runpath] =', runpaths) ret['rpath'] = rpaths ret['runpath'] = runpaths ret['needed'] = libs @@ -367,7 +383,7 @@ continue if all_ldpaths is None: all_ldpaths = rpaths + ldpaths['rpath'] + ldpaths['env'] + runpaths + ldpaths['runpath'] + ldpaths['conf'] + ldpaths['interp'] - fullpath = FindLib(elf, lib, all_ldpaths) + fullpath = FindLib(elf, lib, all_ldpaths, debug=debug) _all_libs[lib] = { 'path': fullpath, 'needed': [], @@ -386,7 +402,7 @@ def _ShowVersion(_option, _opt, _value, _parser): - d = '$Id: lddtree.py,v 1.46 2014/07/30 04:07:43 vapier Exp $'.split() + d = '$Id: lddtree.py,v 1.47 2014/07/30 04:16:25 vapier Exp $'.split() print('%s-%s %s %s' % (d[1].split('.')[0], d[2], d[3], d[4])) sys.exit(0) @@ -612,32 +628,33 @@ if options.skip_non_elfs and options.copy_non_elfs: parser.error('pick one handler for non-ELFs: skip or copy') - if options.debug: - print('root =', options.root) - if options.dest: - print('dest =', options.dest) + dbg(options.debug, 'root =', options.root) + if options.dest: + dbg(options.debug, 'dest =', options.dest) if not paths: err('missing ELF files to scan') ldpaths = LoadLdpaths(options.root, options.prefix) - if options.debug: - print('ldpaths[conf] =', ldpaths['conf']) - print('ldpaths[env] =', ldpaths['env']) + dbg(options.debug, 'ldpaths[conf] =', ldpaths['conf']) + dbg(options.debug, 'ldpaths[env] =', ldpaths['env']) # Process all the files specified. ret = 0 for path in paths: + dbg(options.debug, 'argv[x] =', path) # Only auto-prefix the path if the ELF is absolute. # If it's a relative path, the user most likely wants # the local path. if options.auto_root and path.startswith('/'): path = options.root + path.lstrip('/') + dbg(options.debug, ' +auto-root =', path) matched = False for p in glob.iglob(path): matched = True try: - elf = ParseELF(p, options.root, options.prefix, ldpaths) + elf = ParseELF(p, options.root, options.prefix, ldpaths, + debug=options.debug) except exceptions.ELFError as e: if options.skip_non_elfs: continue
