add-debug-link.py 2.2 KB

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