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