unwcheck.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/python
  2. #
  3. # Usage: unwcheck.py FILE
  4. #
  5. # This script checks the unwind info of each function in file FILE
  6. # and verifies that the sum of the region-lengths matches the total
  7. # length of the function.
  8. #
  9. # Based on a shell/awk script originally written by Harish Patil,
  10. # which was converted to Perl by Matthew Chapman, which was converted
  11. # to Python by David Mosberger.
  12. #
  13. import os
  14. import re
  15. import sys
  16. if len(sys.argv) != 2:
  17. print "Usage: %s FILE" % sys.argv[0]
  18. sys.exit(2)
  19. readelf = os.getenv("READELF", "readelf")
  20. start_pattern = re.compile("<([^>]*)>: \[0x([0-9a-f]+)-0x([0-9a-f]+)\]")
  21. rlen_pattern = re.compile(".*rlen=([0-9]+)")
  22. def check_func (func, slots, rlen_sum):
  23. if slots != rlen_sum:
  24. global num_errors
  25. num_errors += 1
  26. if not func: func = "[%#x-%#x]" % (start, end)
  27. print "ERROR: %s: %lu slots, total region length = %lu" % (func, slots, rlen_sum)
  28. return
  29. num_funcs = 0
  30. num_errors = 0
  31. func = False
  32. slots = 0
  33. rlen_sum = 0
  34. for line in os.popen("%s -u %s" % (readelf, sys.argv[1])):
  35. m = start_pattern.match(line)
  36. if m:
  37. check_func(func, slots, rlen_sum)
  38. func = m.group(1)
  39. start = long(m.group(2), 16)
  40. end = long(m.group(3), 16)
  41. slots = 3 * (end - start) / 16
  42. rlen_sum = 0L
  43. num_funcs += 1
  44. else:
  45. m = rlen_pattern.match(line)
  46. if m:
  47. rlen_sum += long(m.group(1))
  48. check_func(func, slots, rlen_sum)
  49. if num_errors == 0:
  50. print "No errors detected in %u functions." % num_funcs
  51. else:
  52. if num_errors > 1:
  53. err="errors"
  54. else:
  55. err="error"
  56. print "%u %s detected in %u functions." % (num_errors, err, num_funcs)
  57. sys.exit(1)