|
| 1 | +#!/usr/bin/env python |
| 2 | +from __future__ import print_function |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | +from lib.config import LINUX_BINARIES, PLATFORM |
| 8 | +from lib.util import execute, get_objcopy_path, get_out_dir, safe_mkdir |
| 9 | + |
| 10 | +# It has to be done before stripping the binaries. |
| 11 | +def copy_debug_from_binaries(directory, out_dir, target_cpu, compress): |
| 12 | + for binary in LINUX_BINARIES: |
| 13 | + binary_path = os.path.join(directory, binary) |
| 14 | + if os.path.isfile(binary_path): |
| 15 | + copy_debug_from_binary(binary_path, out_dir, target_cpu, compress) |
| 16 | + |
| 17 | +def copy_debug_from_binary(binary_path, out_dir, target_cpu, compress): |
| 18 | + try: |
| 19 | + objcopy = get_objcopy_path(target_cpu) |
| 20 | + except: |
| 21 | + if PLATFORM == 'linux' and (target_cpu == 'x86' or target_cpu == 'arm' or |
| 22 | + target_cpu == 'arm64'): |
| 23 | + # Skip because no objcopy binary on the given target. |
| 24 | + return |
| 25 | + raise |
| 26 | + debug_name = get_debug_name(binary_path) |
| 27 | + cmd = [objcopy, '--only-keep-debug'] |
| 28 | + if compress: |
| 29 | + cmd.extend(['--compress-debug-sections']) |
| 30 | + cmd.extend([binary_path, os.path.join(out_dir, debug_name)]) |
| 31 | + execute(cmd) |
| 32 | + return debug_name |
| 33 | + |
| 34 | +def get_debug_name(binary_path): |
| 35 | + return os.path.basename(binary_path) + '.debug' |
| 36 | + |
| 37 | +def main(): |
| 38 | + args = parse_args() |
| 39 | + safe_mkdir(args.out_dir) |
| 40 | + if args.file: |
| 41 | + copy_debug_from_binary(args.file, args.out_dir, args.target_cpu, |
| 42 | + args.compress) |
| 43 | + else: |
| 44 | + copy_debug_from_binaries(args.directory, args.out_dir, args.target_cpu, |
| 45 | + args.compress) |
| 46 | + |
| 47 | +def parse_args(): |
| 48 | + parser = argparse.ArgumentParser(description='Copy debug from binaries') |
| 49 | + parser.add_argument('-d', '--directory', |
| 50 | + help='Path to the dir that contains files to copy', |
| 51 | + default=get_out_dir(), |
| 52 | + required=False) |
| 53 | + parser.add_argument('-f', '--file', |
| 54 | + help='Path to a specific file to copy debug symbols', |
| 55 | + required=False) |
| 56 | + parser.add_argument('-o', '--out-dir', |
| 57 | + help='Path to the dir that will contain the debugs', |
| 58 | + default=None, |
| 59 | + required=True) |
| 60 | + parser.add_argument('-v', '--verbose', |
| 61 | + action='store_true', |
| 62 | + help='Prints the output of the subprocesses') |
| 63 | + parser.add_argument('--target-cpu', |
| 64 | + default='', |
| 65 | + required=False, |
| 66 | + help='Target cpu of binaries to copy debug symbols') |
| 67 | + parser.add_argument('--compress', |
| 68 | + action='store_true', |
| 69 | + required=False, |
| 70 | + help='Compress the debug symbols') |
| 71 | + |
| 72 | + return parser.parse_args() |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + sys.exit(main()) |
0 commit comments