1#!/usr/bin/env python3 2# 3# Copyright 2016 The Chromium Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Archive all source files that are references in binary debug info. 8 9Invoked by libfuzzer buildbots. Executes dwarfdump to parse debug info. 10""" 11 12from __future__ import print_function 13 14import argparse 15import os 16import re 17import subprocess 18import zipfile 19 20compile_unit_re = re.compile('.*DW_TAG_compile_unit.*') 21at_name_re = re.compile('.*DW_AT_name.*"(.*)".*') 22 23 24def main(): 25 parser = argparse.ArgumentParser(description="Zip binary sources.") 26 parser.add_argument('--binary', required=True, 27 help='binary file to read') 28 parser.add_argument('--workdir', required=True, 29 help='working directory to use to resolve relative paths') 30 parser.add_argument('--srcdir', required=True, 31 help='sources root directory to calculate zip entry names') 32 parser.add_argument('--output', required=True, 33 help='output zip file name') 34 parser.add_argument('--dwarfdump', required=False, 35 default='dwarfdump', help='path to dwarfdump utility') 36 args = parser.parse_args() 37 38 # Dump .debug_info section. 39 out = subprocess.check_output( 40 [args.dwarfdump, '-i', args.binary]) 41 42 looking_for_unit = True 43 compile_units = set() 44 45 # Look for DW_AT_name within DW_TAG_compile_unit 46 for line in out.splitlines(): 47 if looking_for_unit and compile_unit_re.match(line): 48 looking_for_unit = False 49 elif not looking_for_unit: 50 match = at_name_re.match(line) 51 if match: 52 compile_units.add(match.group(1)) 53 looking_for_unit = True 54 55 # Zip sources. 56 with zipfile.ZipFile(args.output, 'w') as z: 57 for compile_unit in sorted(compile_units): 58 src_file = os.path.abspath(os.path.join(args.workdir, compile_unit)) 59 print(src_file) 60 z.write(src_file, os.path.relpath(src_file, args.srcdir)) 61 62 63if __name__ == '__main__': 64 main() 65