JAVASCRIPT   18

v8_prof_polyfill.js

Guest on 15th May 2021 10:18:42 AM

  1. // Copyright 2012 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. //     * Redistributions of source code must retain the above copyright
  7. //       notice, this list of conditions and the following disclaimer.
  8. //     * Redistributions in binary form must reproduce the above
  9. //       copyright notice, this list of conditions and the following
  10. //       disclaimer in the documentation and/or other materials provided
  11. //       with the distribution.
  12. //     * Neither the name of Google Inc. nor the names of its
  13. //       contributors may be used to endorse or promote products derived
  14. //       from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  
  28. // Node polyfill
  29. const fs = require('fs');
  30. const cp = require('child_process');
  31. const os = {
  32.   system: function(name, args) {
  33.     if (process.platform === 'linux' && name === 'nm') {
  34.       // Filter out vdso and vsyscall entries.
  35.       const arg = args[args.length - 1];
  36.       if (arg === '[vdso]' ||
  37.           arg == '[vsyscall]' ||
  38.           /^[0-9a-f]+-[0-9a-f]+$/.test(arg)) {
  39.         return '';
  40.       }
  41.     }
  42.     let out = cp.spawnSync(name, args).stdout.toString();
  43.     // Auto c++filt names, but not [iItT]
  44.     if (process.platform === 'darwin' && name === 'nm')
  45.       out = macCppfiltNm(out);
  46.     return out;
  47.   }
  48. };
  49. const print = console.log;
  50. function read(fileName) {
  51.   return fs.readFileSync(fileName, 'utf8');
  52. }
  53. const quit = process.exit;
  54.  
  55. // Polyfill "readline()".
  56. const logFile = arguments[arguments.length - 1];
  57. try {
  58.   fs.accessSync(logFile);
  59. } catch(e) {
  60.   console.error('Please provide a valid isolate file as the final argument.');
  61.   process.exit(1);
  62. }
  63. const fd = fs.openSync(logFile, 'r');
  64. const buf = Buffer.allocUnsafe(4096);
  65. const dec = new (require('string_decoder').StringDecoder)('utf-8');
  66. var line = '';
  67. versionCheck();
  68. function readline() {
  69.   while (true) {
  70.     const lineBreak = line.indexOf('\n');
  71.     if (lineBreak !== -1) {
  72.       const res = line.slice(0, lineBreak);
  73.       line = line.slice(lineBreak + 1);
  74.       return res;
  75.     }
  76.     const bytes = fs.readSync(fd, buf, 0, buf.length);
  77.     line += dec.write(buf.slice(0, bytes));
  78.     if (line.length === 0) {
  79.       return '';
  80.     }
  81.   }
  82. }
  83.  
  84. function versionCheck() {
  85.   // v8-version looks like "v8-version,$major,$minor,$build,$patch,$candidate"
  86.   // whereas process.versions.v8 is either "$major.$minor.$build" or
  87.   // "$major.$minor.$build.$patch".
  88.   var firstLine = readline();
  89.   line = firstLine + '\n' + line;
  90.   firstLine = firstLine.split(',');
  91.   const curVer = process.versions.v8.split('.');
  92.   if (firstLine.length !== 6 && firstLine[0] !== 'v8-version') {
  93.     console.log('Unable to read v8-version from log file.');
  94.     return;
  95.   }
  96.   // Compare major, minor and build; ignore the patch and candidate fields.
  97.   for (var i = 0; i < 3; i++) {
  98.     if (curVer[i] !== firstLine[i + 1]) {
  99.       console.log('Testing v8 version different from logging version');
  100.       return;
  101.     }
  102.   }
  103. }
  104.  
  105. function macCppfiltNm(out) {
  106.   // Re-grouped copy-paste from `tickprocessor.js`
  107.   const FUNC_RE = /^([0-9a-fA-F]{8,16} [iItT] )(.*)$/gm;
  108.   const CLEAN_RE = /^[0-9a-fA-F]{8,16} [iItT] /;
  109.   let entries = out.match(FUNC_RE);
  110.   if (entries === null)
  111.     return out;
  112.  
  113.   entries = entries.map((entry) => {
  114.     return entry.replace(CLEAN_RE, '')
  115.   });
  116.  
  117.   let filtered;
  118.   try {
  119.     filtered = cp.spawnSync('c++filt', [ '-p' , '-i' ], {
  120.       input: entries.join('\n')
  121.     }).stdout.toString();
  122.   } catch (e) {
  123.     return out;
  124.   }
  125.  
  126.   let i = 0;
  127.   filtered = filtered.split('\n');
  128.   return out.replace(FUNC_RE, (all, prefix, postfix) => {
  129.     return prefix + (filtered[i++] || postfix);
  130.   });
  131. }

Raw Paste


Login or Register to edit or fork this paste. It's free.