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.abspath(os.path.join( |
| 18 os.path.dirname(__file__), '../../../..', |
| 19 'third_party/syzygy/binaries/exe/instrument.exe')) |
| 20 |
| 21 _LOGGER = logging.getLogger() |
| 22 |
| 23 def _Shell(*cmd, **kw): |
| 24 """Shells out to "cmd". Returns a tuple of cmd's stdout, stderr.""" |
| 25 _LOGGER.info('Running command "%s".', cmd) |
| 26 prog = subprocess.Popen(cmd, **kw) |
| 27 |
| 28 stdout, stderr = prog.communicate() |
| 29 if prog.returncode != 0: |
| 30 raise RuntimeError('Command "%s" returned %d.' % (cmd, prog.returncode)) |
| 31 |
| 32 return stdout, stderr |
| 33 |
| 34 |
| 35 def _InstrumentBinary(instrument_exe, mode, executable, symbol, dst_dir): |
| 36 """Instruments the executable found in input_dir, and writes the resultant |
| 37 instrumented executable and symbol files to dst_dir. |
| 38 """ |
| 39 cmd = [instrument_exe, |
| 40 '--overwrite', |
| 41 '--mode=%s' % mode, |
| 42 '--input-image=%s' % executable, |
| 43 '--input-pdb=%s' % symbol, |
| 44 '--output-image=%s' % os.path.abspath( |
| 45 os.path.join(dst_dir, os.path.basename(executable))), |
| 46 '--output-pdb=%s' % os.path.abspath( |
| 47 os.path.join(dst_dir, os.path.basename(symbol))),] |
| 48 |
| 49 return _Shell(*cmd) |
| 50 |
| 51 def _CopyAgentDLL(agent_dll, destination_dir): |
| 52 """Copy the agent DLL to the destination directory.""" |
| 53 dirname, agent_name = os.path.split(agent_dll); |
| 54 agent_dst_name = os.path.join(destination_dir, agent_name); |
| 55 shutil.copyfile(agent_dll, agent_dst_name) |
| 56 |
| 57 def main(options): |
| 58 logging.basicConfig(level=logging.INFO) |
| 59 |
| 60 # Make sure the destination directory exists. |
| 61 if not os.path.isdir(options.destination_dir): |
| 62 _LOGGER.info('Creating destination directory "%s".', |
| 63 options.destination_dir) |
| 64 os.makedirs(options.destination_dir) |
| 65 |
| 66 # Instruments the binaries into the destination directory. |
| 67 _InstrumentBinary(options.instrumenter, |
| 68 options.mode, |
| 69 options.input_executable, |
| 70 options.input_symbol, |
| 71 options.destination_dir) |
| 72 |
| 73 # Copy the agent DLL to the destination directory. |
| 74 _CopyAgentDLL(options.agent_dll, options.destination_dir); |
| 75 |
| 76 |
| 77 def _ParseOptions(): |
| 78 option_parser = optparse.OptionParser() |
| 79 option_parser.add_option('--input_executable', |
| 80 help='The path to the input executable.') |
| 81 option_parser.add_option('--input_symbol', |
| 82 help='The path to the input symbol file.') |
| 83 option_parser.add_option('--mode', |
| 84 help='Specifies which instrumentation mode is to be used.') |
| 85 option_parser.add_option('--agent_dll', |
| 86 help='The agent DLL used by this instrumentation.') |
| 87 option_parser.add_option('--instrumenter', default=_DEFAULT_INSTRUMENTER, |
| 88 help='Instrumenter executable to use, defaults to "%s"' |
| 89 % _DEFAULT_INSTRUMENTER) |
| 90 option_parser.add_option('-d', '--destination_dir', |
| 91 help='Destination directory for instrumented files, defaults to ' |
| 92 'the subdirectory "instrumented" in the output_dir.') |
| 93 options, args = option_parser.parse_args() |
| 94 |
| 95 if not options.mode: |
| 96 option_parser.error('You must provide an instrumentation mode.') |
| 97 if not options.agent_dll: |
| 98 option_parser.error('You must provide an agent DLL.') |
| 99 if not options.input_executable: |
| 100 option_parser.error('You must provide an input executable.') |
| 101 if not options.input_symbol: |
| 102 option_parser.error('You must provide an input symbol file.') |
| 103 |
| 104 if not options.destination_dir: |
| 105 options.destination_dir = os.path.join(options.output_dir, 'instrumented') |
| 106 |
| 107 return options |
| 108 |
| 109 |
| 110 if '__main__' == __name__: |
| 111 sys.exit(main(_ParseOptions())) |
OLD | NEW |