symbolicate.js.flow 6.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
#!/usr/bin/env node
/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow strict-local
 * @format
 */

// Symbolicates a JavaScript stack trace using a source map.
// In our first form, we read a stack trace from stdin and symbolicate it via
// the provided source map.
// In our second form, we symbolicate using an explicit line number, and
// optionally a column.
// In our third form, we symbolicate using a module ID, a line number, and
// optionally a column.

'use strict';

// flowlint-next-line untyped-import:off
const SourceMapConsumer = require('source-map').SourceMapConsumer;
const Symbolication = require('./Symbolication.js');

const fs = require('fs');
// flowlint-next-line untyped-import:off
const through2 = require('through2');

async function main(
  argvInput: Array<string> = process.argv.slice(2),
  {
    stdin,
    stderr,
    stdout,
  }: {
    stdin: stream$Readable | tty$ReadStream,
    stderr: stream$Writable,
    stdout: stream$Writable,
    ...
  } = process,
): Promise<number> {
  const argv = argvInput.slice();
  function checkAndRemoveArg(arg, valuesPerArg = 0) {
    let values = null;
    for (let idx = argv.indexOf(arg); idx !== -1; idx = argv.indexOf(arg)) {
      argv.splice(idx, 1);
      values = values || [];
      values.push(argv.splice(idx, valuesPerArg));
    }
    return values;
  }

  function checkAndRemoveArgWithValue(arg) {
    const values = checkAndRemoveArg(arg, 1);
    return values ? values[0][0] : null;
  }
  try {
    const noFunctionNames = checkAndRemoveArg('--no-function-names');
    const isHermesCrash = checkAndRemoveArg('--hermes-crash');
    const inputLineStart = Number.parseInt(
      checkAndRemoveArgWithValue('--input-line-start') || '1',
      10,
    );
    const inputColumnStart = Number.parseInt(
      checkAndRemoveArgWithValue('--input-column-start') || '0',
      10,
    );
    const outputLineStart = Number.parseInt(
      checkAndRemoveArgWithValue('--output-line-start') || '1',
      10,
    );
    const outputColumnStart = Number.parseInt(
      checkAndRemoveArgWithValue('--output-column-start') || '0',
      10,
    );

    if (argv.length < 1 || argv.length > 4) {
      /* eslint no-path-concat: "off" */

      const usages = [
        'Usage: ' + __filename + ' <source-map-file>',
        '       ' + __filename + ' <source-map-file> <line> [column]',
        '       ' +
          __filename +
          ' <source-map-file> <moduleId>.js <line> [column]',
        '       ' + __filename + ' <source-map-file> <mapfile>.profmap',
        '       ' +
          __filename +
          ' <source-map-file> --attribution < in.jsonl > out.jsonl',
        '       ' + __filename + ' <source-map-file> <tracefile>.cpuprofile',
        ' Optional flags:',
        '  --no-function-names',
        '  --hermes-crash',
        '  --input-line-start <line> (default: 1)',
        '  --input-column-start <column> (default: 0)',
        '  --output-line-start <line> (default: 1)',
        '  --output-column-start <column> (default: 0)',
      ];
      console.error(usages.join('\n'));
      return 1;
    }

    // Read the source map.
    const sourceMapFileName = argv.shift();
    const options = {
      nameSource: noFunctionNames ? 'identifier_names' : 'function_names',
      inputLineStart,
      inputColumnStart,
      outputLineStart,
      outputColumnStart,
    };
    let context;
    if (fs.lstatSync(sourceMapFileName).isDirectory()) {
      context = Symbolication.unstable_createDirectoryContext(
        SourceMapConsumer,
        sourceMapFileName,
        options,
      );
    } else {
      const content = fs.readFileSync(sourceMapFileName, 'utf8');
      context = Symbolication.createContext(
        SourceMapConsumer,
        content,
        options,
      );
    }
    if (argv.length === 0) {
      const stackTrace = await readAll(stdin);
      if (isHermesCrash) {
        const stackTraceJSON = JSON.parse(stackTrace);
        const symbolicatedTrace = context.symbolicateHermesMinidumpTrace(
          stackTraceJSON,
        );
        stdout.write(JSON.stringify(symbolicatedTrace));
      } else {
        stdout.write(context.symbolicate(stackTrace));
      }
    } else if (argv[0].endsWith('.profmap')) {
      stdout.write(context.symbolicateProfilerMap(argv[0]));
    } else if (argv[0] === '--attribution') {
      let buffer = '';
      await waitForStream(
        stdin
          .pipe(
            through2(function(data, enc, callback) {
              // Take arbitrary strings, output single lines
              buffer += data;
              const lines = buffer.split('\n');
              for (let i = 0, e = lines.length - 1; i < e; i++) {
                this.push(lines[i]);
              }
              buffer = lines[lines.length - 1];
              callback();
            }),
          )
          .pipe(
            through2.obj(function(data, enc, callback) {
              // This is JSONL, so each line is a separate JSON object
              const obj = JSON.parse(data);
              context.symbolicateAttribution(obj);
              this.push(JSON.stringify(obj) + '\n');
              callback();
            }),
          )
          .pipe(stdout),
      );
    } else if (argv[0].endsWith('.cpuprofile')) {
      // NOTE: synchronous
      context.symbolicateChromeTrace(argv[0], {stdout, stderr});
    } else {
      // read-from-argv form.
      let moduleIds;
      if (argv[0].endsWith('.js')) {
        moduleIds = context.parseFileName(argv[0]);
        argv.shift();
      } else {
        moduleIds = null;
      }
      const lineNumber = argv.shift();
      const columnNumber = argv.shift() || 0;
      const original = context.getOriginalPositionFor(
        +lineNumber,
        +columnNumber,
        // $FlowFixMe context is a union here and so this parameter is a union
        moduleIds,
      );
      stdout.write(
        [
          original.source ?? 'null',
          original.line ?? 'null',
          original.name ?? 'null',
        ].join(':') + '\n',
      );
    }
  } catch (error) {
    stderr.write(error + '\n');
    return 1;
  }
  return 0;
}

function readAll(stream) {
  return new Promise(resolve => {
    let data = '';
    if (stream.isTTY === true) {
      resolve(data);
      return;
    }

    stream.setEncoding('utf8');
    stream.on('readable', () => {
      let chunk;
      // flowlint-next-line sketchy-null-string:off
      while ((chunk = stream.read())) {
        data += chunk.toString();
      }
    });
    stream.on('end', () => {
      resolve(data);
    });
  });
}

function waitForStream(stream) {
  return new Promise(resolve => {
    stream.on('finish', resolve);
  });
}

if (require.main === module) {
  main().then(code => process.exit(code));
}

module.exports = main;