gdb/contrib/test_pubnames_and_indexes.py - gdb

Global variables defined

Functions defined

Source code

  1. #! /usr/bin/env python

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

  18. # This program requires readelf, gdb and objcopy.  The default values are gdb
  19. # from the build tree and objcopy and readelf from $PATH.  They may be
  20. # overridden by setting environment variables GDB, READELF and OBJCOPY
  21. # respectively.  We assume the current directory is either $obj/gdb or
  22. # $obj/gdb/testsuite.
  23. #
  24. # Example usage:
  25. #
  26. # bash$ cd $objdir/gdb/testsuite
  27. # bash$ python test_pubnames_and_indexes.py <binary_name>

  28. """test_pubnames_and_indexes.py

  29. Test that the gdb_index produced by gold is identical to the gdb_index
  30. produced by gdb itself.

  31. Further check that the pubnames and pubtypes produced by gcc are identical
  32. to those that gdb produces.

  33. Finally, check that all strings are canonicalized identically.
  34. """

  35. __author__ = 'saugustine@google.com (Sterling Augustine)'

  36. import os
  37. import subprocess
  38. import sys

  39. OBJCOPY = None
  40. READELF = None
  41. GDB = None

  42. def get_pub_info(filename, readelf_option):
  43.   """Parse and return all the pubnames or pubtypes produced by readelf with the
  44.   given option.
  45.   """
  46.   readelf = subprocess.Popen([READELF, '--debug-dump=' + readelf_option,
  47.                                        filename], stdout=subprocess.PIPE)
  48.   pubnames = []

  49.   in_list = False;
  50.   for line in readelf.stdout:
  51.     fields = line.split(None, 1)
  52.     if (len(fields) == 2 and fields[0] == 'Offset'
  53.         and fields[1].strip() == 'Name'):
  54.       in_list = True
  55.     # Either a blank-line or a new Length field terminates the current section.
  56.     elif (len(fields) == 0 or fields[0] == 'Length:'):
  57.       in_list = False;
  58.     elif (in_list):
  59.       pubnames.append(fields[1].strip())

  60.   readelf.wait()
  61.   return pubnames


  62. def get_gdb_index(filename):
  63.   """Use readelf to dump the gdb index and collect the types and names"""
  64.   readelf = subprocess.Popen([READELF, '--debug-dump=gdb_index',
  65.                               filename], stdout=subprocess.PIPE)
  66.   index_symbols = []
  67.   symbol_table_started = False
  68.   for line in readelf.stdout:
  69.     if (line == 'Symbol table:\n'):
  70.       symbol_table_started = True;
  71.     elif (symbol_table_started):
  72.       # Readelf prints gdb-index lines formatted like so:
  73.       # [  4] two::c2<double>::c2: 0
  74.       # So take the string between the first close bracket and the last colon.
  75.       index_symbols.append(line[line.find(']') + 2: line.rfind(':')])

  76.   readelf.wait()
  77.   return index_symbols


  78. def CheckSets(list0, list1, name0, name1):
  79.   """Report any setwise differences between the two lists"""

  80.   if len(list0) == 0 or len(list1) == 0:
  81.     return False

  82.   difference0 = set(list0) - set(list1)
  83.   if len(difference0) != 0:
  84.     print "Elements in " + name0 + " but not " + name1 + ": (",
  85.     print len(difference0),
  86.     print ")"
  87.     for element in difference0:
  88.       print "  " + element

  89.   difference1 = set(list1) - set(list0)
  90.   if len(difference1) != 0:
  91.     print "Elements in " + name1 + " but not " + name0 + ": (",
  92.     print len(difference1),
  93.     print ")"
  94.     for element in difference1:
  95.       print "  " + element

  96.   if (len(difference0) != 0 or len(difference1) != 0):
  97.     return True

  98.   print name0 + " and " + name1 + " are identical."
  99.   return False


  100. def find_executables():
  101.   """Find the copies of readelf, objcopy and gdb to use."""
  102.   # Executable finding logic follows cc-with-index.sh
  103.   global READELF
  104.   READELF = os.getenv('READELF')
  105.   if READELF is None:
  106.     READELF = 'readelf'
  107.   global OBJCOPY
  108.   OBJCOPY = os.getenv('OBJCOPY')
  109.   if OBJCOPY is None:
  110.     OBJCOPY = 'objcopy'

  111.   global GDB
  112.   GDB = os.getenv('GDB')
  113.   if (GDB is None):
  114.     if os.path.isfile('./gdb') and os.access('./gdb', os.X_OK):
  115.       GDB = './gdb'
  116.     elif os.path.isfile('../gdb') and os.access('../gdb', os.X_OK):
  117.       GDB = '../gdb'
  118.     elif os.path.isfile('../../gdb') and os.access('../../gdb', os.X_OK):
  119.       GDB = '../../gdb'
  120.     else:
  121.       # Punt and use the gdb in the path.
  122.       GDB = 'gdb'


  123. def main(argv):
  124.   """The main subprogram."""
  125.   if len(argv) != 2:
  126.     print "Usage: test_pubnames_and_indexes.py <filename>"
  127.     sys.exit(2)

  128.   find_executables();

  129.   # Get the index produced by Gold--It should have been built into the binary.
  130.   gold_index = get_gdb_index(argv[1])

  131.   # Collect the pubnames and types list
  132.   pubs_list = get_pub_info(argv[1], "pubnames")
  133.   pubs_list = pubs_list + get_pub_info(argv[1], "pubtypes")

  134.   # Generate a .gdb_index with gdb
  135.   gdb_index_file = argv[1] + '.gdb-generated-index'
  136.   subprocess.check_call([OBJCOPY, '--remove-section', '.gdb_index',
  137.                          argv[1], gdb_index_file])
  138.   subprocess.check_call([GDB, '-batch', '-nx', gdb_index_file,
  139.                          '-ex', 'save gdb-index ' + os.path.dirname(argv[1]),
  140.                          '-ex', 'quit'])
  141.   subprocess.check_call([OBJCOPY, '--add-section',
  142.                          '.gdb_index=' + gdb_index_file + '.gdb-index',
  143.                          gdb_index_file])
  144.   gdb_index = get_gdb_index(gdb_index_file)
  145.   os.remove(gdb_index_file)
  146.   os.remove(gdb_index_file + '.gdb-index')

  147.   failed = False
  148.   gdb_index.sort()
  149.   gold_index.sort()
  150.   pubs_list.sort()

  151.   # Find the differences between the various indices.
  152.   if len(gold_index) == 0:
  153.     print "Gold index is empty"
  154.     failed |= True

  155.   if len(gdb_index) == 0:
  156.     print "Gdb index is empty"
  157.     failed |= True

  158.   if len(pubs_list) == 0:
  159.     print "Pubs list is empty"
  160.     failed |= True

  161.   failed |= CheckSets(gdb_index, gold_index, "gdb index", "gold index")
  162.   failed |= CheckSets(pubs_list, gold_index, "pubs list", "gold index")
  163.   failed |= CheckSets(pubs_list, gdb_index, "pubs list", "gdb index")

  164.   if failed:
  165.     print "Test failed"
  166.     sys.exit(1)


  167. if __name__ == '__main__':
  168.   main(sys.argv)