|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) 2013 GitHub, Inc. All rights reserved. |
| 3 | +# Copyright (c) 2013 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 | +"""Convert pdb to sym for given directories""" |
| 8 | + |
| 9 | +import errno |
| 10 | +import glob |
| 11 | +import optparse |
| 12 | +import os |
| 13 | +import Queue |
| 14 | +import re |
| 15 | +import subprocess |
| 16 | +import sys |
| 17 | +import threading |
| 18 | + |
| 19 | + |
| 20 | +CONCURRENT_TASKS=4 |
| 21 | +SOURCE_ROOT=os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) |
| 22 | +DUMP_SYMS=os.path.join(SOURCE_ROOT, 'vendor', 'breakpad', 'dump_syms.exe') |
| 23 | + |
| 24 | + |
| 25 | +def GetCommandOutput(command): |
| 26 | + """Runs the command list, returning its output. |
| 27 | +
|
| 28 | + Prints the given command (which should be a list of one or more strings), |
| 29 | + then runs it and returns its output (stdout) as a string. |
| 30 | +
|
| 31 | + From chromium_utils. |
| 32 | + """ |
| 33 | + devnull = open(os.devnull, 'w') |
| 34 | + proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=devnull, |
| 35 | + bufsize=1) |
| 36 | + output = proc.communicate()[0] |
| 37 | + return output |
| 38 | + |
| 39 | + |
| 40 | +def mkdir_p(path): |
| 41 | + """Simulates mkdir -p.""" |
| 42 | + try: |
| 43 | + os.makedirs(path) |
| 44 | + except OSError as e: |
| 45 | + if e.errno == errno.EEXIST and os.path.isdir(path): |
| 46 | + pass |
| 47 | + else: raise |
| 48 | + |
| 49 | + |
| 50 | +def GenerateSymbols(options, binaries): |
| 51 | + """Dumps the symbols of binary and places them in the given directory.""" |
| 52 | + |
| 53 | + queue = Queue.Queue() |
| 54 | + print_lock = threading.Lock() |
| 55 | + |
| 56 | + def _Worker(): |
| 57 | + while True: |
| 58 | + binary = queue.get() |
| 59 | + |
| 60 | + if options.verbose: |
| 61 | + with print_lock: |
| 62 | + print "Generating symbols for %s" % binary |
| 63 | + |
| 64 | + syms = GetCommandOutput([DUMP_SYMS, binary]) |
| 65 | + module_line = re.match("MODULE [^ ]+ [^ ]+ ([0-9A-F]+) (.*)\r\n", syms) |
| 66 | + if module_line == None: |
| 67 | + with print_lock: |
| 68 | + print "Failed to get symbols for %s" % binary |
| 69 | + queue.task_done() |
| 70 | + continue |
| 71 | + |
| 72 | + output_path = os.path.join(options.symbols_dir, module_line.group(2), |
| 73 | + module_line.group(1)) |
| 74 | + mkdir_p(output_path) |
| 75 | + symbol_file = "%s.sym" % module_line.group(2) |
| 76 | + f = open(os.path.join(output_path, symbol_file), 'w') |
| 77 | + f.write(syms) |
| 78 | + f.close() |
| 79 | + |
| 80 | + queue.task_done() |
| 81 | + |
| 82 | + for binary in binaries: |
| 83 | + queue.put(binary) |
| 84 | + |
| 85 | + for _ in range(options.jobs): |
| 86 | + t = threading.Thread(target=_Worker) |
| 87 | + t.daemon = True |
| 88 | + t.start() |
| 89 | + |
| 90 | + queue.join() |
| 91 | + |
| 92 | + |
| 93 | +def main(): |
| 94 | + parser = optparse.OptionParser() |
| 95 | + parser.add_option('', '--symbols-dir', default='', |
| 96 | + help='The directory where to write the symbols file.') |
| 97 | + parser.add_option('', '--clear', default=False, action='store_true', |
| 98 | + help='Clear the symbols directory before writing new ' |
| 99 | + 'symbols.') |
| 100 | + parser.add_option('-j', '--jobs', default=CONCURRENT_TASKS, action='store', |
| 101 | + type='int', help='Number of parallel tasks to run.') |
| 102 | + parser.add_option('-v', '--verbose', action='store_true', |
| 103 | + help='Print verbose status output.') |
| 104 | + |
| 105 | + (options, directories) = parser.parse_args() |
| 106 | + |
| 107 | + if not options.symbols_dir: |
| 108 | + print "Required option --symbols-dir missing." |
| 109 | + return 1 |
| 110 | + |
| 111 | + if options.clear: |
| 112 | + try: |
| 113 | + shutil.rmtree(options.symbols_dir) |
| 114 | + except: |
| 115 | + pass |
| 116 | + |
| 117 | + pdbs = [] |
| 118 | + for directory in directories: |
| 119 | + pdbs += glob.glob(os.path.join(directory, '*.pdb')) |
| 120 | + |
| 121 | + GenerateSymbols(options, pdbs) |
| 122 | + |
| 123 | + return 0 |
| 124 | + |
| 125 | + |
| 126 | +if '__main__' == __name__: |
| 127 | + sys.exit(main()) |
0 commit comments