dump-cpp.py 1.9 KB

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