xref: /third_party/node/deps/v8/tools/dump-cpp.py (revision 1cb0ef41)
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
9# for py2/py3 compatibility
10from __future__ import print_function
11
12import os
13import platform
14import re
15import subprocess
16import sys
17
18def is_file_executable(fPath):
19  return os.path.isfile(fPath) and os.access(fPath, os.X_OK)
20
21if __name__ == '__main__':
22  JS_FILES = ['dumpcpp-driver.mjs']
23  tools_path = os.path.dirname(os.path.realpath(__file__))
24  on_windows = platform.system() == 'Windows'
25  JS_FILES = [os.path.join(tools_path, f) for f in JS_FILES]
26
27  args = []
28  log_file = 'v8.log'
29  debug = False
30  for arg in sys.argv[1:]:
31    if arg == '--debug':
32      debug = True
33      continue
34    args.append(arg)
35    if not arg.startswith('-'):
36      log_file = arg
37
38  if on_windows:
39    args.append('--windows')
40
41  with open(log_file, 'r') as f:
42    lines = f.readlines()
43
44  d8_line = re.search(',\"(.*d8)', ''.join(lines))
45  if d8_line:
46    d8_exec = d8_line.group(1)
47    if not is_file_executable(d8_exec):
48      print('d8 binary path found in {} is not executable.'.format(log_file))
49      sys.exit(-1)
50  else:
51    print('No d8 binary path found in {}.'.format(log_file))
52    sys.exit(-1)
53
54  args = [d8_exec] + ['--module'] + JS_FILES + ['--'] + args
55
56  with open(log_file) as f:
57    sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
58                          stdin=f)
59    out, err = sp.communicate()
60  if debug:
61    print(err)
62  if sp.returncode != 0:
63    print(out)
64    exit(-1)
65
66  if on_windows and out:
67    out = re.sub('\r+\n', '\n', out)
68
69  is_written = not bool(out)
70  with open(log_file, 'w') as f:
71    for line in lines:
72      if not is_written and line.startswith('tick'):
73        f.write(out)
74        is_written = True
75      f.write(line)
76