blob: f139ef8d3cc32509decfc9eddf5201980f8f413e [file] [log] [blame]
Joshua Hood65ecceb2022-03-14 16:18:501#!/usr/bin/env python3
aizatskyd892de032016-06-02 05:49:182#
Avi Drissmandfd880852022-09-15 20:11:093# Copyright 2016 The Chromium Authors
aizatskyd892de032016-06-02 05:49:184# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
aizatskyd892de032016-06-02 05:49:186"""Archive all source files that are references in binary debug info.
7
aizatsky57c14fe2016-07-07 22:12:168Invoked by libfuzzer buildbots. Executes dwarfdump to parse debug info.
aizatskyd892de032016-06-02 05:49:189"""
10
11from __future__ import print_function
12
13import argparse
14import os
15import re
16import subprocess
17import zipfile
18
19compile_unit_re = re.compile('.*DW_TAG_compile_unit.*')
aizatsky57c14fe2016-07-07 22:12:1620at_name_re = re.compile('.*DW_AT_name.*"(.*)".*')
aizatskyd892de032016-06-02 05:49:1821
22
23def main():
Brian Sheedy0d2300f32024-08-13 23:14:4124 parser = argparse.ArgumentParser(description='Zip binary sources.')
Ben Pasteneb5c67262024-05-15 21:24:0125 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')
aizatskyd892de032016-06-02 05:49:1838 args = parser.parse_args()
39
40 # Dump .debug_info section.
Ben Pasteneb5c67262024-05-15 21:24:0141 out = subprocess.check_output([args.dwarfdump, '-i', args.binary])
aizatskyd892de032016-06-02 05:49:1842
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)
aizatsky57c14fe2016-07-07 22:12:1661 z.write(src_file, os.path.relpath(src_file, args.srcdir))
aizatskyd892de032016-06-02 05:49:1862
63
64if __name__ == '__main__':
65 main()