OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """A utility script to help building Syzygy-instrumented Chrome binaries.""" |
| 7 |
| 8 import logging |
| 9 import optparse |
| 10 import os |
| 11 import shutil |
| 12 import subprocess |
| 13 import sys |
| 14 |
| 15 |
| 16 # The default instrument executable to use to instrument binaries. |
| 17 _DEFAULT_INSTRUMENTER = os.path.join( |
| 18 os.path.join(os.path.dirname(__file__), '../../../..'), |
| 19 'third_party/syzygy/binaries/exe/instrument.exe') |
| 20 |
| 21 # The logger executable to ship with the instrumented binaries. |
| 22 _DEFAULT_LOGGER = os.path.join( |
| 23 os.path.join(os.path.dirname(__file__), '../../../..'), |
| 24 'third_party/syzygy/binaries/exe/logger.exe') |
| 25 |
| 26 _LOGGER = logging.getLogger() |
| 27 |
| 28 def _Shell(*cmd, **kw): |
| 29 """Shells out to "cmd". Returns a tuple of cmd's stdout, stderr.""" |
| 30 _LOGGER.info('Running command "%s".', cmd) |
| 31 prog = subprocess.Popen(cmd, **kw) |
| 32 |
| 33 stdout, stderr = prog.communicate() |
| 34 if prog.returncode != 0: |
| 35 raise RuntimeError('Command "%s" returned %d.' % (cmd, prog.returncode)) |
| 36 |
| 37 return stdout, stderr |
| 38 |
| 39 |
| 40 def _InstrumentBinary(instrument_exe, mode, executable, symbol, dst_dir): |
| 41 """Instruments the executable found in input_dir, and writes the resultant |
| 42 instrumented executable and symbol files to dst_dir. |
| 43 """ |
| 44 cmd = [instrument_exe, |
| 45 '--overwrite', |
| 46 '--mode=%s' % mode, |
| 47 '--input-image=%s' % executable, |
| 48 '--input-pdb=%s' % symbol, |
| 49 '--output-image=%s' % os.path.abspath( |
| 50 os.path.join(dst_dir, os.path.basename(executable))), |
| 51 '--output-pdb=%s' % os.path.abspath( |
| 52 os.path.join(dst_dir, os.path.basename(symbol))),] |
| 53 |
| 54 return _Shell(*cmd) |
| 55 |
| 56 def _CopyAgentDLL(agent_dll, destination_dir): |
| 57 """Copy the agent DLL to the destination directory.""" |
| 58 dirname, agent_name = os.path.split(agent_dll); |
| 59 agent_dst_name = os.path.join(destination_dir, agent_name); |
| 60 shutil.copyfile(agent_dll, agent_dst_name) |
| 61 |
| 62 def _CopyLogger(logger_exe, destination_dir): |
| 63 """Copy the logger executable to the destination directory.""" |
| 64 dirname, logger_name = os.path.split(logger_exe); |
| 65 logger_dst_name = os.path.join(destination_dir, logger_name); |
| 66 shutil.copyfile(logger_exe, logger_dst_name) |
| 67 |
| 68 def main(options): |
| 69 logging.basicConfig(level=logging.INFO) |
| 70 |
| 71 # Make sure the destination directory exists. |
| 72 if not os.path.isdir(options.destination_dir): |
| 73 _LOGGER.info('Creating destination directory "%s".', |
| 74 options.destination_dir) |
| 75 os.makedirs(options.destination_dir) |
| 76 |
| 77 # Instruments the binaries into the destination directory. |
| 78 _InstrumentBinary(options.instrumenter, |
| 79 options.mode, |
| 80 options.input_executable, |
| 81 options.input_symbol, |
| 82 options.destination_dir) |
| 83 |
| 84 # Copy the agent DLL to the destination directory. |
| 85 _CopyAgentDLL(options.agent_dll, options.destination_dir); |
| 86 |
| 87 if options.mode == 'asan': |
| 88 _CopyLogger(options.logger, options.destination_dir); |
| 89 |
| 90 |
| 91 def _ParseOptions(): |
| 92 option_parser = optparse.OptionParser() |
| 93 option_parser.add_option('--input_executable', |
| 94 help='The path to the input executable.') |
| 95 option_parser.add_option('--input_symbol', |
| 96 help='The path to the input symbol file.') |
| 97 option_parser.add_option('--mode', |
| 98 help='Specifies which instrumentation mode is to be used.') |
| 99 option_parser.add_option('--agent_dll', |
| 100 help='The agent DLL used by this instrumentation.') |
| 101 option_parser.add_option('--instrumenter', default=_DEFAULT_INSTRUMENTER, |
| 102 help='Instrumenter exectuable to use, defaults to "%s"' |
| 103 % _DEFAULT_INSTRUMENTER) |
| 104 option_parser.add_option('--logger', default=_DEFAULT_LOGGER, |
| 105 help='Logger executable to copy with the executable, defaults to "%s"' |
| 106 % _DEFAULT_LOGGER) |
| 107 option_parser.add_option('-d', '--destination_dir', |
| 108 help='Destination directory for instrumented files, defaults to ' |
| 109 'the subdirectory "instrumented" in the output_dir.') |
| 110 options, args = option_parser.parse_args() |
| 111 |
| 112 if not options.mode: |
| 113 option_parser.error('You must provide an instrumentation mode.') |
| 114 if not options.agent_dll: |
| 115 option_parser.error('You must provide an agent DLL.') |
| 116 if not options.input_executable: |
| 117 option_parser.error('You must provide an input executable.') |
| 118 if not options.input_symbol: |
| 119 option_parser.error('You must provide an input symbol file.') |
| 120 |
| 121 if not options.destination_dir: |
| 122 options.destination_dir = os.path.join(options.output_dir, 'instrumented') |
| 123 |
| 124 return options |
| 125 |
| 126 |
| 127 if '__main__' == __name__: |
| 128 sys.exit(main(_ParseOptions())) |
OLD | NEW |