add-debug-link.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 in ('x86', 'arm', 'arm64'):
  15. # Skip because no objcopy binary on the given target.
  16. return
  17. debug_name = get_debug_name(binary_path)
  18. # Make sure the path to the binary is not relative because of cwd param.
  19. real_binary_path = os.path.realpath(binary_path)
  20. cmd = ['objcopy', '--add-gnu-debuglink=' + debug_name, real_binary_path]
  21. execute(cmd, cwd=debug_dir)
  22. def get_debug_name(binary_path):
  23. return os.path.basename(binary_path) + '.debug'
  24. def main():
  25. args = parse_args()
  26. if args.file:
  27. add_debug_link_into_binary(args.file, args.target_cpu, args.debug_dir)
  28. else:
  29. add_debug_link_into_binaries(args.directory, args.target_cpu,
  30. args.debug_dir)
  31. def parse_args():
  32. parser = argparse.ArgumentParser(description='Add debug link to binaries')
  33. parser.add_argument('-d', '--directory',
  34. help='Path to the dir that contains files to add links',
  35. default=get_out_dir(),
  36. required=False)
  37. parser.add_argument('-f', '--file',
  38. help='Path to a specific file to add debug link',
  39. required=False)
  40. parser.add_argument('-s', '--debug-dir',
  41. help='Path to the dir that contain the debugs',
  42. default=None,
  43. required=True)
  44. parser.add_argument('-v', '--verbose',
  45. action='store_true',
  46. help='Prints the output of the subprocesses')
  47. parser.add_argument('--target-cpu',
  48. default='',
  49. required=False,
  50. help='Target cpu of binaries to add debug link')
  51. return parser.parse_args()
  52. if __name__ == '__main__':
  53. sys.exit(main())