1#!/usr/bin/env python
2# Copyright 2016 the V8 project 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# This script executes dumpcpp.js, collects all dumped C++ symbols,
7# and merges them back into v8 log.
8
9import os
10import platform
11import re
12import subprocess
13import sys
14
15def is_file_executable(fPath):
16  return os.path.isfile(fPath) and os.access(fPath, os.X_OK)
17
18if __name__ == '__main__':
19  JS_FILES = ['splaytree.js', 'codemap.js', 'csvparser.js', 'consarray.js',
20              'profile.js', 'logreader.js', 'tickprocessor.js', 'SourceMap.js',
21              'dumpcpp.js', 'dumpcpp-driver.js']
22  tools_path = os.path.dirname(os.path.realpath(__file__))
23  on_windows = platform.system() == 'Windows'
24  JS_FILES = [os.path.join(tools_path, f) for f in JS_FILES]
25
26  args = []
27  log_file = 'v8.log'
28  debug = False
29  for arg in sys.argv[1:]:
30    if arg == '--debug':
31      debug = True
32      continue
33    args.append(arg)
34    if not arg.startswith('-'):
35      log_file = arg
36
37  if on_windows:
38    args.append('--windows')
39
40  with open(log_file, 'r') as f:
41    lines = f.readlines()
42
43  d8_line = re.search(',\"(.*d8)', ''.join(lines))
44  if d8_line:
45    d8_exec = d8_line.group(1)
46    if not is_file_executable(d8_exec):
47      print 'd8 binary path found in {} is not executable.'.format(log_file)
48      sys.exit(-1)
49  else:
50    print 'No d8 binary path found in {}.'.format(log_file)
51    sys.exit(-1)
52
53  args = [d8_exec] + JS_FILES + ['--'] + args
54
55  with open(log_file) as f:
56    sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
57                          stdin=f)
58    out, err = sp.communicate()
59  if debug:
60    print err
61  if sp.returncode != 0:
62    print out
63    exit(-1)
64
65  if on_windows and out:
66    out = re.sub('\r+\n', '\n', out)
67
68  is_written = not bool(out)
69  with open(log_file, 'w') as f:
70    for line in lines:
71      if not is_written and line.startswith('tick'):
72        f.write(out)
73        is_written = True
74      f.write(line)
75