add-debug-link.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python3
  2. from __future__ import print_function
  3. import argparse
  4. import os
  5. import sys
  6. from lib.config import LINUX_BINARIES, PLATFORM
  7. from lib.util import execute, get_out_dir
  8. def add_debug_link_into_binaries(directory, target_cpu, debug_dir):
  9. for binary in LINUX_BINARIES:
  10. binary_path = os.path.join(directory, binary)
  11. if os.path.isfile(binary_path):
  12. add_debug_link_into_binary(binary_path, target_cpu, debug_dir)
  13. def add_debug_link_into_binary(binary_path, target_cpu, debug_dir):
  14. if PLATFORM == 'linux' and (target_cpu == 'x86' or target_cpu == 'arm' or
  15. target_cpu == 'arm64'):
  16. # Skip because no objcopy binary on the given target.
  17. return
  18. debug_name = get_debug_name(binary_path)
  19. # Make sure the path to the binary is not relative because of cwd param.
  20. real_binary_path = os.path.realpath(binary_path)
  21. cmd = ['objcopy', '--add-gnu-debuglink=' + debug_name, real_binary_path]
  22. execute(cmd, cwd=debug_dir)
  23. def get_debug_name(binary_path):
  24. return os.path.basename(binary_path) + '.debug'
  25. def main():
  26. args = parse_args()
  27. if args.file:
  28. add_debug_link_into_binary(args.file, args.target_cpu, args.debug_dir)
  29. else:
  30. add_debug_link_into_binaries(args.directory, args.target_cpu,
  31. args.debug_dir)
  32. def parse_args():
  33. parser = argparse.ArgumentParser(description='Add debug link to binaries')
  34. parser.add_argument('-d', '--directory',
  35. help='Path to the dir that contains files to add links',
  36. default=get_out_dir(),
  37. required=False)
  38. parser.add_argument('-f', '--file',
  39. help='Path to a specific file to add debug link',
  40. required=False)
  41. parser.add_argument('-s', '--debug-dir',
  42. help='Path to the dir that contain the debugs',
  43. default=None,
  44. required=True)
  45. parser.add_argument('-v', '--verbose',
  46. action='store_true',
  47. help='Prints the output of the subprocesses')
  48. parser.add_argument('--target-cpu',
  49. default='',
  50. required=False,
  51. help='Target cpu of binaries to add debug link')
  52. return parser.parse_args()
  53. if __name__ == '__main__':
  54. sys.exit(main())