11cb0ef41Sopenharmony_ci#!/usr/bin/env python
21cb0ef41Sopenharmony_ci# Copyright 2016 the V8 project authors. All rights reserved.
31cb0ef41Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
41cb0ef41Sopenharmony_ci# found in the LICENSE file.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci# This script executes dumpcpp.js, collects all dumped C++ symbols,
71cb0ef41Sopenharmony_ci# and merges them back into v8 log.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci# for py2/py3 compatibility
101cb0ef41Sopenharmony_cifrom __future__ import print_function
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciimport os
131cb0ef41Sopenharmony_ciimport platform
141cb0ef41Sopenharmony_ciimport re
151cb0ef41Sopenharmony_ciimport subprocess
161cb0ef41Sopenharmony_ciimport sys
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_cidef is_file_executable(fPath):
191cb0ef41Sopenharmony_ci  return os.path.isfile(fPath) and os.access(fPath, os.X_OK)
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciif __name__ == '__main__':
221cb0ef41Sopenharmony_ci  JS_FILES = ['dumpcpp-driver.mjs']
231cb0ef41Sopenharmony_ci  tools_path = os.path.dirname(os.path.realpath(__file__))
241cb0ef41Sopenharmony_ci  on_windows = platform.system() == 'Windows'
251cb0ef41Sopenharmony_ci  JS_FILES = [os.path.join(tools_path, f) for f in JS_FILES]
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  args = []
281cb0ef41Sopenharmony_ci  log_file = 'v8.log'
291cb0ef41Sopenharmony_ci  debug = False
301cb0ef41Sopenharmony_ci  for arg in sys.argv[1:]:
311cb0ef41Sopenharmony_ci    if arg == '--debug':
321cb0ef41Sopenharmony_ci      debug = True
331cb0ef41Sopenharmony_ci      continue
341cb0ef41Sopenharmony_ci    args.append(arg)
351cb0ef41Sopenharmony_ci    if not arg.startswith('-'):
361cb0ef41Sopenharmony_ci      log_file = arg
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  if on_windows:
391cb0ef41Sopenharmony_ci    args.append('--windows')
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  with open(log_file, 'r') as f:
421cb0ef41Sopenharmony_ci    lines = f.readlines()
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  d8_line = re.search(',\"(.*d8)', ''.join(lines))
451cb0ef41Sopenharmony_ci  if d8_line:
461cb0ef41Sopenharmony_ci    d8_exec = d8_line.group(1)
471cb0ef41Sopenharmony_ci    if not is_file_executable(d8_exec):
481cb0ef41Sopenharmony_ci      print('d8 binary path found in {} is not executable.'.format(log_file))
491cb0ef41Sopenharmony_ci      sys.exit(-1)
501cb0ef41Sopenharmony_ci  else:
511cb0ef41Sopenharmony_ci    print('No d8 binary path found in {}.'.format(log_file))
521cb0ef41Sopenharmony_ci    sys.exit(-1)
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  args = [d8_exec] + ['--module'] + JS_FILES + ['--'] + args
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  with open(log_file) as f:
571cb0ef41Sopenharmony_ci    sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
581cb0ef41Sopenharmony_ci                          stdin=f)
591cb0ef41Sopenharmony_ci    out, err = sp.communicate()
601cb0ef41Sopenharmony_ci  if debug:
611cb0ef41Sopenharmony_ci    print(err)
621cb0ef41Sopenharmony_ci  if sp.returncode != 0:
631cb0ef41Sopenharmony_ci    print(out)
641cb0ef41Sopenharmony_ci    exit(-1)
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  if on_windows and out:
671cb0ef41Sopenharmony_ci    out = re.sub('\r+\n', '\n', out)
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  is_written = not bool(out)
701cb0ef41Sopenharmony_ci  with open(log_file, 'w') as f:
711cb0ef41Sopenharmony_ci    for line in lines:
721cb0ef41Sopenharmony_ci      if not is_written and line.startswith('tick'):
731cb0ef41Sopenharmony_ci        f.write(out)
741cb0ef41Sopenharmony_ci        is_written = True
751cb0ef41Sopenharmony_ci      f.write(line)
76