blob: 2a8019a02d59a8261989fb5ac19bd3ea8482c565 [file] [log] [blame]
Joshua Hood65ecceb2022-03-14 16:18:501#!/usr/bin/env python3
aizatskyd892de032016-06-02 05:49:182#
3# Copyright 2016 The Chromium Authors. All rights reserved.
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
aizatsky57c14fe2016-07-07 22:12:169Invoked by libfuzzer buildbots. Executes dwarfdump to parse debug info.
aizatskyd892de032016-06-02 05:49:1810"""
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.*')
aizatsky57c14fe2016-07-07 22:12:1621at_name_re = re.compile('.*DW_AT_name.*"(.*)".*')
aizatskyd892de032016-06-02 05:49:1822
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')
aizatsky57c14fe2016-07-07 22:12:1630 parser.add_argument('--srcdir', required=True,
31 help='sources root directory to calculate zip entry names')
aizatskyd892de032016-06-02 05:49:1832 parser.add_argument('--output', required=True,
33 help='output zip file name')
aizatsky57c14fe2016-07-07 22:12:1634 parser.add_argument('--dwarfdump', required=False,
35 default='dwarfdump', help='path to dwarfdump utility')
aizatskyd892de032016-06-02 05:49:1836 args = parser.parse_args()
37
38 # Dump .debug_info section.
39 out = subprocess.check_output(
aizatsky57c14fe2016-07-07 22:12:1640 [args.dwarfdump, '-i', args.binary])
aizatskyd892de032016-06-02 05:49:1841
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)
aizatsky57c14fe2016-07-07 22:12:1660 z.write(src_file, os.path.relpath(src_file, args.srcdir))
aizatskyd892de032016-06-02 05:49:1861
62
63if __name__ == '__main__':
64 main()