tickprocessor.mjs 31.2 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
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import { LogReader, parseString, parseVarArgs } from "./logreader.mjs";
import { BaseArgumentsProcessor, parseBool } from "./arguments.mjs";
import { Profile, JsonProfile } from "./profile.mjs";
import { ViewBuilder } from "./profile_view.mjs";
32
import { WebInspector} from "./sourcemap.mjs";
33 34


35 36 37 38
class V8Profile extends Profile {
  static IC_RE =
      /^(LoadGlobalIC: )|(Handler: )|(?:CallIC|LoadIC|StoreIC)|(?:Builtin: (?:Keyed)?(?:Load|Store)IC_)/;
  static BYTECODES_RE = /^(BytecodeHandler: )/;
39
  static BASELINE_HANDLERS_RE = /^(Builtin: .*Baseline.*)/;
40 41
  static BUILTINS_RE = /^(Builtin: )/;
  static STUBS_RE = /^(Stub: )/;
42

43 44
  constructor(separateIc, separateBytecodes, separateBuiltins, separateStubs,
        separateBaselineHandlers) {
45
    super();
46
    const regexps = [];
47 48 49 50 51
    if (!separateIc) regexps.push(V8Profile.IC_RE);
    if (!separateBytecodes) regexps.push(V8Profile.BYTECODES_RE);
    if (!separateBuiltins) regexps.push(V8Profile.BUILTINS_RE);
    if (!separateStubs) regexps.push(V8Profile.STUBS_RE);
    if (regexps.length > 0) {
52
      this.skipThisFunction = function(name) {
53
        for (let i = 0; i < regexps.length; i++) {
54 55 56 57 58 59 60
          if (regexps[i].test(name)) return true;
        }
        return false;
      };
    }
  }
}
61 62


63
export class TickProcessor extends LogReader {
64
  constructor(
65 66 67 68 69
    cppEntriesProvider,
    separateIc,
    separateBytecodes,
    separateBuiltins,
    separateStubs,
70
    separateBaselineHandlers,
71 72 73 74 75 76 77 78 79 80 81
    callGraphSize,
    ignoreUnknown,
    stateFilter,
    distortion,
    range,
    sourceMap,
    timedRange,
    pairwiseTimedRange,
    onlySummary,
    runtimeTimerFilter,
    preprocessJson) {
82
    super({},
83 84
      timedRange,
      pairwiseTimedRange);
85 86 87 88 89
    this.dispatchTable_ = {
      'shared-library': {
        parsers: [parseString, parseInt, parseInt, parseInt],
        processor: this.processSharedLibrary
      },
90
      'code-creation': {
91 92 93 94
        parsers: [parseString, parseInt, parseInt, parseInt, parseInt,
          parseString, parseVarArgs],
        processor: this.processCodeCreation
      },
95
      'code-deopt': {
96 97 98 99 100 101 102 103 104 105 106 107
        parsers: [parseInt, parseInt, parseInt, parseInt, parseInt,
          parseString, parseString, parseString],
        processor: this.processCodeDeopt
      },
      'code-move': {
        parsers: [parseInt, parseInt,],
        processor: this.processCodeMove
      },
      'code-delete': {
        parsers: [parseInt],
        processor: this.processCodeDelete
      },
108
      'code-source-info': {
109 110 111 112
        parsers: [parseInt, parseInt, parseInt, parseInt, parseString,
          parseString, parseString],
        processor: this.processCodeSourceInfo
      },
113
      'script-source': {
114 115 116 117 118 119 120
        parsers: [parseInt, parseString, parseString],
        processor: this.processScriptSource
      },
      'sfi-move': {
        parsers: [parseInt, parseInt],
        processor: this.processFunctionMove
      },
121 122
      'active-runtime-timer': {
        parsers: [parseString],
123 124
        processor: this.processRuntimeTimerEvent
      },
125
      'tick': {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        parsers: [parseInt, parseInt, parseInt,
          parseInt, parseInt, parseVarArgs],
        processor: this.processTick
      },
      'heap-sample-begin': {
        parsers: [parseString, parseString, parseInt],
        processor: this.processHeapSampleBegin
      },
      'heap-sample-end': {
        parsers: [parseString, parseString],
        processor: this.processHeapSampleEnd
      },
      'timer-event-start': {
        parsers: [parseString, parseString, parseString],
        processor: this.advanceDistortion
      },
      'timer-event-end': {
        parsers: [parseString, parseString, parseString],
        processor: this.advanceDistortion
      },
146 147 148 149 150 151 152 153 154 155
      // Ignored events.
      'profiler': null,
      'function-creation': null,
      'function-move': null,
      'function-delete': null,
      'heap-sample-item': null,
      'current-time': null,  // Handled specially, not parsed.
      // Obsolete row types.
      'code-allocate': null,
      'begin-code-region': null,
156 157
      'end-code-region': null
    };
158

159 160 161 162 163 164
    this.preprocessJson = preprocessJson;
    this.cppEntriesProvider_ = cppEntriesProvider;
    this.callGraphSize_ = callGraphSize;
    this.ignoreUnknown_ = ignoreUnknown;
    this.stateFilter_ = stateFilter;
    this.runtimeTimerFilter_ = runtimeTimerFilter;
165
    this.sourceMap = this.loadSourceMap(sourceMap);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    const ticks = this.ticks_ =
      { total: 0, unaccounted: 0, excluded: 0, gc: 0 };

    distortion = parseInt(distortion);
    // Convert picoseconds to nanoseconds.
    this.distortion_per_entry = isNaN(distortion) ? 0 : (distortion / 1000);
    this.distortion = 0;
    const rangelimits = range ? range.split(",") : [];
    const range_start = parseInt(rangelimits[0]);
    const range_end = parseInt(rangelimits[1]);
    // Convert milliseconds to nanoseconds.
    this.range_start = isNaN(range_start) ? -Infinity : (range_start * 1000);
    this.range_end = isNaN(range_end) ? Infinity : (range_end * 1000)

    V8Profile.prototype.handleUnknownCode = function (
181
      operation, addr, opt_stackPos) {
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      const op = Profile.Operation;
      switch (operation) {
        case op.MOVE:
          printErr(`Code move event for unknown code: 0x${addr.toString(16)}`);
          break;
        case op.DELETE:
          printErr(`Code delete event for unknown code: 0x${addr.toString(16)}`);
          break;
        case op.TICK:
          // Only unknown PCs (the first frame) are reported as unaccounted,
          // otherwise tick balance will be corrupted (this behavior is compatible
          // with the original tickprocessor.py script.)
          if (opt_stackPos == 0) {
            ticks.unaccounted++;
          }
          break;
      }
    };
200

201 202 203 204
    if (preprocessJson) {
      this.profile_ = new JsonProfile();
    } else {
      this.profile_ = new V8Profile(separateIc, separateBytecodes,
205
        separateBuiltins, separateStubs, separateBaselineHandlers);
206 207 208 209 210 211 212 213 214 215 216
    }
    this.codeTypes_ = {};
    // Count each tick as a time unit.
    this.viewBuilder_ = new ViewBuilder(1);
    this.lastLogFileName_ = null;

    this.generation_ = 1;
    this.currentProducerProfile_ = null;
    this.onlySummary_ = onlySummary;
  }

217 218 219 220 221 222 223 224 225 226 227
  loadSourceMap(sourceMap) {
    if (!sourceMap) return null;
    // Overwrite the load function to load scripts synchronously.
    WebInspector.SourceMap.load = (sourceMapURL) => {
      const content = this.readFile(sourceMapURL);
      const sourceMapObject = JSON.parse(content);
      return new SourceMap(sourceMapURL, sourceMapObject);
    };
    return WebInspector.SourceMap.load(sourceMap);
  }

228 229 230 231 232
  static VmStates = {
    JS: 0,
    GC: 1,
    PARSER: 2,
    BYTECODE_COMPILER: 3,
233
    // TODO(cbruni): add BASELINE_COMPILER
234 235 236 237 238
    COMPILER: 4,
    OTHER: 5,
    EXTERNAL: 6,
    IDLE: 7,
  };
239

240 241 242 243 244 245
  static CodeTypes = {
    CPP: 0,
    SHARED_LIB: 1
  };
  // Otherwise, this is JS-related code. We are not adding it to
  // codeTypes_ map because there can be zillions of them.
246

247 248
  static CALL_PROFILE_CUTOFF_PCT = 1.0;
  static CALL_GRAPH_SIZE = 5;
249

250 251 252 253 254
  /**
   * @override
   */
  printError(str) {
    printErr(str);
255 256
  }

257 258 259
  setCodeType(name, type) {
    this.codeTypes_[name] = TickProcessor.CodeTypes[type];
  }
260

261 262 263
  isSharedLibrary(name) {
    return this.codeTypes_[name] == TickProcessor.CodeTypes.SHARED_LIB;
  }
264

265 266 267
  isCppCode(name) {
    return this.codeTypes_[name] == TickProcessor.CodeTypes.CPP;
  }
268

269 270 271
  isJsCode(name) {
    return name !== "UNKNOWN" && !(name in this.codeTypes_);
  }
272

273 274 275 276 277 278 279
  processLogFile(fileName) {
    this.lastLogFileName_ = fileName;
    let line;
    while (line = readline()) {
      this.processLogLine(line);
    }
  }
280

281 282 283
  processLogFileInTest(fileName) {
    // Hack file name to avoid dealing with platform specifics.
    this.lastLogFileName_ = 'v8.log';
284
    const contents = this.readFile(fileName);
285 286
    this.processLogChunk(contents);
  }
287

288 289 290
  processSharedLibrary(name, startAddr, endAddr, aslrSlide) {
    const entry = this.profile_.addLibrary(name, startAddr, endAddr, aslrSlide);
    this.setCodeType(entry.getName(), 'SHARED_LIB');
291
    this.cppEntriesProvider_.parseVmSymbols(
292 293 294 295
      name, startAddr, endAddr, aslrSlide, (fName, fStart, fEnd) => {
        this.profile_.addStaticCode(fName, fStart, fEnd);
        this.setCodeType(fName, 'CPP');
      });
296 297
  }

298
  processCodeCreation(type, kind, timestamp, start, size, name, maybe_func) {
299
    if (type != 'RegExp' && maybe_func.length) {
300 301 302 303 304 305 306
      const funcAddr = parseInt(maybe_func[0]);
      const state = Profile.parseState(maybe_func[1]);
      this.profile_.addFuncCode(type, name, timestamp, start, size, funcAddr, state);
    } else {
      this.profile_.addCode(type, name, timestamp, start, size);
    }
  }
307

308 309 310 311
  processCodeDeopt(
      timestamp, size, code, inliningId, scriptOffset, bailoutType,
      sourcePositionText, deoptReasonText) {
    this.profile_.deoptCode(timestamp, code, inliningId, scriptOffset,
312
      bailoutType, sourcePositionText, deoptReasonText);
313
  }
314

315 316 317
  processCodeMove(from, to) {
    this.profile_.moveCode(from, to);
  }
318

319 320 321
  processCodeDelete(start) {
    this.profile_.deleteCode(start);
  }
322

323 324 325 326 327 328
  processCodeSourceInfo(
      start, script, startPos, endPos, sourcePositions, inliningPositions,
      inlinedFunctions) {
    this.profile_.addSourcePositions(start, script, startPos,
      endPos, sourcePositions, inliningPositions, inlinedFunctions);
  }
329

330 331 332
  processScriptSource(script, url, source) {
    this.profile_.addScriptSource(script, url, source);
  }
333

334 335 336
  processFunctionMove(from, to) {
    this.profile_.moveFunc(from, to);
  }
337

338 339 340 341 342 343 344
  includeTick(vmState) {
    if (this.stateFilter_ !== null) {
      return this.stateFilter_ == vmState;
    } else if (this.runtimeTimerFilter_ !== null) {
      return this.currentRuntimeTimer == this.runtimeTimerFilter_;
    }
    return true;
345 346
  }

347 348 349
  processRuntimeTimerEvent(name) {
    this.currentRuntimeTimer = name;
  }
350

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  processTick(pc,
    ns_since_start,
    is_external_callback,
    tos_or_external_callback,
    vmState,
    stack) {
    this.distortion += this.distortion_per_entry;
    ns_since_start -= this.distortion;
    if (ns_since_start < this.range_start || ns_since_start > this.range_end) {
      return;
    }
    this.ticks_.total++;
    if (vmState == TickProcessor.VmStates.GC) this.ticks_.gc++;
    if (!this.includeTick(vmState)) {
      this.ticks_.excluded++;
      return;
    }
    if (is_external_callback) {
      // Don't use PC when in external callback code, as it can point
      // inside callback's code, and we will erroneously report
      // that a callback calls itself. Instead we use tos_or_external_callback,
      // as simply resetting PC will produce unaccounted ticks.
      pc = tos_or_external_callback;
374
      tos_or_external_callback = 0;
375 376 377 378 379 380 381
    } else if (tos_or_external_callback) {
      // Find out, if top of stack was pointing inside a JS function
      // meaning that we have encountered a frameless invocation.
      const funcEntry = this.profile_.findEntry(tos_or_external_callback);
      if (!funcEntry || !funcEntry.isJSFunction || !funcEntry.isJSFunction()) {
        tos_or_external_callback = 0;
      }
382 383
    }

384
    this.profile_.recordTick(
385 386
      ns_since_start, vmState,
      this.processStack(pc, tos_or_external_callback, stack));
387
  }
388

389 390 391
  advanceDistortion() {
    this.distortion += this.distortion_per_entry;
  }
392

393 394 395 396
  processHeapSampleBegin(space, state, ticks) {
    if (space != 'Heap') return;
    this.currentProducerProfile_ = new CallTree();
  }
397

398 399
  processHeapSampleEnd(space, state) {
    if (space != 'Heap' || !this.currentProducerProfile_) return;
400

401 402 403 404 405 406
    print(`Generation ${this.generation_}:`);
    const tree = this.currentProducerProfile_;
    tree.computeTotalWeights();
    const producersView = this.viewBuilder_.buildView(tree);
    // Sort by total time, desc, then by name, desc.
    producersView.sort((rec1, rec2) =>
407
      rec2.totalTime - rec1.totalTime ||
408 409
      (rec2.internalFuncName < rec1.internalFuncName ? -1 : 1));
    this.printHeavyProfile(producersView.head.children);
410

411 412
    this.currentProducerProfile_ = null;
    this.generation_++;
413 414
  }

415 416 417 418 419
  printVMSymbols() {
    console.log(
      JSON.stringify(this.profile_.serializeVMSymbols()));
  }

420 421 422 423 424 425 426 427 428
  printStatistics() {
    if (this.preprocessJson) {
      this.profile_.writeJson();
      return;
    }

    print(`Statistical profiling result from ${this.lastLogFileName_}` +
      `, (${this.ticks_.total} ticks, ${this.ticks_.unaccounted} unaccounted, ` +
      `${this.ticks_.excluded} excluded).`);
429

430

431
    if (this.ticks_.total == 0) return;
432

433 434 435 436
    const flatProfile = this.profile_.getFlatProfile();
    const flatView = this.viewBuilder_.buildView(flatProfile);
    // Sort by self time, desc, then by name, desc.
    flatView.sort((rec1, rec2) =>
437
      rec2.selfTime - rec1.selfTime ||
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
      (rec2.internalFuncName < rec1.internalFuncName ? -1 : 1));
    let totalTicks = this.ticks_.total;
    if (this.ignoreUnknown_) {
      totalTicks -= this.ticks_.unaccounted;
    }
    const printAllTicks = !this.onlySummary_;

    // Count library ticks
    const flatViewNodes = flatView.head.children;

    let libraryTicks = 0;
    if (printAllTicks) this.printHeader('Shared libraries');
    this.printEntries(flatViewNodes, totalTicks, null,
      name => this.isSharedLibrary(name),
      (rec) => { libraryTicks += rec.selfTime; }, printAllTicks);
    const nonLibraryTicks = totalTicks - libraryTicks;

    let jsTicks = 0;
    if (printAllTicks) this.printHeader('JavaScript');
    this.printEntries(flatViewNodes, totalTicks, nonLibraryTicks,
      name => this.isJsCode(name),
      (rec) => { jsTicks += rec.selfTime; }, printAllTicks);

    let cppTicks = 0;
    if (printAllTicks) this.printHeader('C++');
    this.printEntries(flatViewNodes, totalTicks, nonLibraryTicks,
      name => this.isCppCode(name),
      (rec) => { cppTicks += rec.selfTime; }, printAllTicks);

    this.printHeader('Summary');
    this.printLine('JavaScript', jsTicks, totalTicks, nonLibraryTicks);
    this.printLine('C++', cppTicks, totalTicks, nonLibraryTicks);
    this.printLine('GC', this.ticks_.gc, totalTicks, nonLibraryTicks);
    this.printLine('Shared libraries', libraryTicks, totalTicks, null);
    if (!this.ignoreUnknown_ && this.ticks_.unaccounted > 0) {
      this.printLine('Unaccounted', this.ticks_.unaccounted,
        this.ticks_.total, null);
475 476
    }

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    if (printAllTicks) {
      print('\n [C++ entry points]:');
      print('   ticks    cpp   total   name');
      const c_entry_functions = this.profile_.getCEntryProfile();
      const total_c_entry = c_entry_functions[0].ticks;
      for (let i = 1; i < c_entry_functions.length; i++) {
        const c = c_entry_functions[i];
        this.printLine(c.name, c.ticks, total_c_entry, totalTicks);
      }

      this.printHeavyProfHeader();
      const heavyProfile = this.profile_.getBottomUpProfile();
      const heavyView = this.viewBuilder_.buildView(heavyProfile);
      // To show the same percentages as in the flat profile.
      heavyView.head.totalTime = totalTicks;
      // Sort by total time, desc, then by name, desc.
      heavyView.sort((rec1, rec2) =>
494
        rec2.totalTime - rec1.totalTime ||
495 496 497
        (rec2.internalFuncName < rec1.internalFuncName ? -1 : 1));
      this.printHeavyProfile(heavyView.head.children);
    }
498 499
  }

500 501 502 503
  printHeader(headerTitle) {
    print(`\n [${headerTitle}]:`);
    print('   ticks  total  nonlib   name');
  }
504

505
  printLine(
506
    entry, ticks, totalTicks, nonLibTicks) {
507 508 509
    const pct = ticks * 100 / totalTicks;
    const nonLibPct = nonLibTicks != null
      ? `${(ticks * 100 / nonLibTicks).toFixed(1).toString().padStart(5)}%  `
510
      : '        ';
511 512 513
    print(`${`  ${ticks.toString().padStart(5)}  ` +
      pct.toFixed(1).toString().padStart(5)}%  ${nonLibPct}${entry}`);
  }
514

515 516 517 518 519 520 521 522
  printHeavyProfHeader() {
    print('\n [Bottom up (heavy) profile]:');
    print('  Note: percentage shows a share of a particular caller in the ' +
      'total\n' +
      '  amount of its parent calls.');
    print(`  Callers occupying less than ${TickProcessor.CALL_PROFILE_CUTOFF_PCT.toFixed(1)}% are not shown.\n`);
    print('   ticks parent  name');
  }
523

524 525 526 527 528 529 530
  processProfile(profile, filterP, func) {
    for (let i = 0, n = profile.length; i < n; ++i) {
      const rec = profile[i];
      if (!filterP(rec.internalFuncName)) {
        continue;
      }
      func(rec);
531 532 533
    }
  }

534 535 536 537 538 539 540
  getLineAndColumn(name) {
    const re = /:([0-9]+):([0-9]+)$/;
    const array = re.exec(name);
    if (!array) {
      return null;
    }
    return { line: array[1], column: array[2] };
541 542
  }

543 544
  hasSourceMap() {
    return this.sourceMap != null;
545 546
  }

547 548 549
  formatFunctionName(funcName) {
    if (!this.hasSourceMap()) {
      return funcName;
550
    }
551 552 553
    const lc = this.getLineAndColumn(funcName);
    if (lc == null) {
      return funcName;
554
    }
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    // in source maps lines and columns are zero based
    const lineNumber = lc.line - 1;
    const column = lc.column - 1;
    const entry = this.sourceMap.findEntry(lineNumber, column);
    const sourceFile = entry[2];
    const sourceLine = entry[3] + 1;
    const sourceColumn = entry[4] + 1;

    return `${sourceFile}:${sourceLine}:${sourceColumn} -> ${funcName}`;
  }

  printEntries(
        profile, totalTicks, nonLibTicks, filterP, callback, printAllTicks) {
    this.processProfile(profile, filterP, (rec) => {
      if (rec.selfTime == 0) return;
      callback(rec);
      const funcName = this.formatFunctionName(rec.internalFuncName);
      if (printAllTicks) {
        this.printLine(funcName, rec.selfTime, totalTicks, nonLibTicks);
      }
    });
  }
577

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
  printHeavyProfile(profile, opt_indent) {
    const indent = opt_indent || 0;
    const indentStr = ''.padStart(indent);
    this.processProfile(profile, () => true, (rec) => {
      // Cut off too infrequent callers.
      if (rec.parentTotalPercent < TickProcessor.CALL_PROFILE_CUTOFF_PCT) return;
      const funcName = this.formatFunctionName(rec.internalFuncName);
      print(`${`  ${rec.totalTime.toString().padStart(5)}  ` +
        rec.parentTotalPercent.toFixed(1).toString().padStart(5)}%  ${indentStr}${funcName}`);
      // Limit backtrace depth.
      if (indent < 2 * this.callGraphSize_) {
        this.printHeavyProfile(rec.children, indent + 2);
      }
      // Delimit top-level functions.
      if (indent == 0) print('');
    });
594
  }
595
}
596 597


598
class CppEntriesProvider {
599
  inRange(funcInfo, start, end) {
600 601 602
    return funcInfo.start >= start && funcInfo.end <= end;
  }

603 604
  parseVmSymbols(libName, libStart, libEnd, libASLRSlide, processorFunc) {
    this.loadSymbols(libName);
605

606 607 608 609 610 611 612 613 614
    let lastUnknownSize;
    let lastAdded;

    let addEntry = (funcInfo) => {
      // Several functions can be mapped onto the same address. To avoid
      // creating zero-sized entries, skip such duplicates.
      // Also double-check that function belongs to the library address space.

      if (lastUnknownSize &&
615
        lastUnknownSize.start < funcInfo.start) {
616 617
        // Try to update lastUnknownSize based on new entries start position.
        lastUnknownSize.end = funcInfo.start;
618
        if ((!lastAdded ||
619 620 621 622 623 624
            !this.inRange(lastUnknownSize, lastAdded.start, lastAdded.end)) &&
            this.inRange(lastUnknownSize, libStart, libEnd)) {
          processorFunc(
              lastUnknownSize.name, lastUnknownSize.start, lastUnknownSize.end);
          lastAdded = lastUnknownSize;
        }
625
      }
626 627 628 629 630 631 632 633 634 635 636 637 638
      lastUnknownSize = undefined;

      if (funcInfo.end) {
        // Skip duplicates that have the same start address as the last added.
        if ((!lastAdded || lastAdded.start != funcInfo.start) &&
          this.inRange(funcInfo, libStart, libEnd)) {
          processorFunc(funcInfo.name, funcInfo.start, funcInfo.end);
          lastAdded = funcInfo;
        }
      } else {
        // If a funcInfo doesn't have an end, try to match it up with then next
        // entry.
        lastUnknownSize = funcInfo;
639 640 641
      }
    }

642 643 644 645 646
    while (true) {
      const funcInfo = this.parseNextLine();
      if (funcInfo === null) continue;
      if (funcInfo === false) break;
      if (funcInfo.start < libStart - libASLRSlide &&
647
        funcInfo.start < libEnd - libStart) {
648 649 650 651 652 653 654 655
        funcInfo.start += libStart;
      } else {
        funcInfo.start += libASLRSlide;
      }
      if (funcInfo.size) {
        funcInfo.end = funcInfo.start + funcInfo.size;
      }
      addEntry(funcInfo);
656
    }
657
    addEntry({ name: '', start: libEnd });
658 659
  }

660
  loadSymbols(libName) {}
661

662
  parseNextLine() { return false }
663
}
664 665


666 667 668
export class UnixCppEntriesProvider extends CppEntriesProvider {
  constructor(nmExec, objdumpExec, targetRootFS, apkEmbeddedLibrary) {
    super();
669 670 671 672 673 674 675 676 677 678 679
    this.symbols = [];
    // File offset of a symbol minus the virtual address of a symbol found in
    // the symbol table.
    this.fileOffsetMinusVma = 0;
    this.parsePos = 0;
    this.nmExec = nmExec;
    this.objdumpExec = objdumpExec;
    this.targetRootFS = targetRootFS;
    this.apkEmbeddedLibrary = apkEmbeddedLibrary;
    this.FUNC_RE = /^([0-9a-fA-F]{8,16}) ([0-9a-fA-F]{8,16} )?[tTwW] (.*)$/;
  }
680 681


682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
  loadSymbols(libName) {
    this.parsePos = 0;
    if (this.apkEmbeddedLibrary && libName.endsWith('.apk')) {
      libName = this.apkEmbeddedLibrary;
    }
    if (this.targetRootFS) {
      libName = libName.substring(libName.lastIndexOf('/') + 1);
      libName = this.targetRootFS + libName;
    }
    try {
      this.symbols = [
        os.system(this.nmExec, ['-C', '-n', '-S', libName], -1, -1),
        os.system(this.nmExec, ['-C', '-n', '-S', '-D', libName], -1, -1)
      ];

      const objdumpOutput = os.system(this.objdumpExec, ['-h', libName], -1, -1);
      for (const line of objdumpOutput.split('\n')) {
        const [, sectionName, , vma, , fileOffset] = line.trim().split(/\s+/);
        if (sectionName === ".text") {
          this.fileOffsetMinusVma = parseInt(fileOffset, 16) - parseInt(vma, 16);
        }
703
      }
704 705 706
    } catch (e) {
      // If the library cannot be found on this system let's not panic.
      this.symbols = ['', ''];
707 708 709
    }
  }

710 711 712 713 714 715 716 717 718 719
  parseNextLine() {
    if (this.symbols.length == 0) {
      return false;
    }
    const lineEndPos = this.symbols[0].indexOf('\n', this.parsePos);
    if (lineEndPos == -1) {
      this.symbols.shift();
      this.parsePos = 0;
      return this.parseNextLine();
    }
720

721 722 723 724 725 726 727 728 729
    const line = this.symbols[0].substring(this.parsePos, lineEndPos);
    this.parsePos = lineEndPos + 1;
    const fields = line.match(this.FUNC_RE);
    let funcInfo = null;
    if (fields) {
      funcInfo = { name: fields[3], start: parseInt(fields[1], 16) + this.fileOffsetMinusVma };
      if (fields[2]) {
        funcInfo.size = parseInt(fields[2], 16);
      }
730
    }
731
    return funcInfo;
732
  }
733
}
734

735 736
export class MacCppEntriesProvider extends UnixCppEntriesProvider {
  constructor(nmExec, objdumpExec, targetRootFS, apkEmbeddedLibrary) {
737 738 739 740
    super(nmExec, objdumpExec, targetRootFS, apkEmbeddedLibrary);
    // Note an empty group. It is required, as UnixCppEntriesProvider expects 3 groups.
    this.FUNC_RE = /^([0-9a-fA-F]{8,16})() (.*)$/;
  }
741

742 743 744
  loadSymbols(libName) {
    this.parsePos = 0;
    libName = this.targetRootFS + libName;
745

746 747 748 749 750 751 752 753
    // It seems that in OS X `nm` thinks that `-f` is a format option, not a
    // "flat" display option flag.
    try {
      this.symbols = [os.system(this.nmExec, ['-n', libName], -1, -1), ''];
    } catch (e) {
      // If the library cannot be found on this system let's not panic.
      this.symbols = '';
    }
754
  }
755
}
756 757


758 759
export class WindowsCppEntriesProvider extends CppEntriesProvider {
  constructor(_ignored_nmExec, _ignored_objdumpExec, targetRootFS,
760 761 762 763 764 765
    _ignored_apkEmbeddedLibrary) {
    super();
    this.targetRootFS = targetRootFS;
    this.symbols = '';
    this.parsePos = 0;
  }
766

767 768
  static FILENAME_RE = /^(.*)\.([^.]+)$/;
  static FUNC_RE =
769
    /^\s+0001:[0-9a-fA-F]{8}\s+([_\?@$0-9a-zA-Z]+)\s+([0-9a-fA-F]{8}).*$/;
770
  static IMAGE_BASE_RE =
771
    /^\s+0000:00000000\s+___ImageBase\s+([0-9a-fA-F]{8}).*$/;
772 773
  // This is almost a constant on Windows.
  static EXE_IMAGE_BASE = 0x00400000;
774

775 776 777 778 779 780 781 782 783 784 785 786
  loadSymbols(libName) {
    libName = this.targetRootFS + libName;
    const fileNameFields = libName.match(WindowsCppEntriesProvider.FILENAME_RE);
    if (!fileNameFields) return;
    const mapFileName = `${fileNameFields[1]}.map`;
    this.moduleType_ = fileNameFields[2].toLowerCase();
    try {
      this.symbols = read(mapFileName);
    } catch (e) {
      // If .map file cannot be found let's not panic.
      this.symbols = '';
    }
787 788
  }

789 790 791 792 793
  parseNextLine() {
    const lineEndPos = this.symbols.indexOf('\r\n', this.parsePos);
    if (lineEndPos == -1) {
      return false;
    }
794

795 796
    const line = this.symbols.substring(this.parsePos, lineEndPos);
    this.parsePos = lineEndPos + 2;
797

798 799 800 801 802 803
    // Image base entry is above all other symbols, so we can just
    // terminate parsing.
    const imageBaseFields = line.match(WindowsCppEntriesProvider.IMAGE_BASE_RE);
    if (imageBaseFields) {
      const imageBase = parseInt(imageBaseFields[1], 16);
      if ((this.moduleType_ == 'exe') !=
804
        (imageBase == WindowsCppEntriesProvider.EXE_IMAGE_BASE)) {
805 806
        return false;
      }
807 808
    }

809 810
    const fields = line.match(WindowsCppEntriesProvider.FUNC_RE);
    return fields ?
811 812
      { name: this.unmangleName(fields[1]), start: parseInt(fields[2], 16) } :
      null;
813
  }
814

815 816 817 818 819 820 821 822
  /**
   * Performs very simple unmangling of C++ names.
   *
   * Does not handle arguments and template arguments. The mangled names have
   * the form:
   *
   *   ?LookupInDescriptor@JSObject@internal@v8@@...arguments info...
   */
823
  unmangleName(name) {
824 825 826 827 828 829 830
    // Empty or non-mangled name.
    if (name.length < 1 || name.charAt(0) != '?') return name;
    const nameEndPos = name.indexOf('@@');
    const components = name.substring(1, nameEndPos).split('@');
    components.reverse();
    return components.join('::');
  }
831
}
832 833 834 835 836


export class ArgumentsProcessor extends BaseArgumentsProcessor {
  getArgsDispatch() {
    let dispatch = {
837
      __proto__:null,
838
      '-j': ['stateFilter', TickProcessor.VmStates.JS,
839
        'Show only ticks from JS VM state'],
840
      '-g': ['stateFilter', TickProcessor.VmStates.GC,
841
        'Show only ticks from GC VM state'],
842
      '-p': ['stateFilter', TickProcessor.VmStates.PARSER,
843
        'Show only ticks from PARSER VM state'],
844
      '-b': ['stateFilter', TickProcessor.VmStates.BYTECODE_COMPILER,
845
        'Show only ticks from BYTECODE_COMPILER VM state'],
846
      '-c': ['stateFilter', TickProcessor.VmStates.COMPILER,
847
        'Show only ticks from COMPILER VM state'],
848
      '-o': ['stateFilter', TickProcessor.VmStates.OTHER,
849
        'Show only ticks from OTHER VM state'],
850
      '-e': ['stateFilter', TickProcessor.VmStates.EXTERNAL,
851
        'Show only ticks from EXTERNAL VM state'],
852
      '--filter-runtime-timer': ['runtimeTimerFilter', null,
853
        'Show only ticks matching the given runtime timer scope'],
854
      '--call-graph-size': ['callGraphSize', TickProcessor.CALL_GRAPH_SIZE,
855
        'Set the call graph size'],
856
      '--ignore-unknown': ['ignoreUnknown', true,
857
        'Exclude ticks of unknown code entries from processing'],
858
      '--separate-ic': ['separateIc', parseBool,
859
        'Separate IC entries'],
860
      '--separate-bytecodes': ['separateBytecodes', parseBool,
861
        'Separate Bytecode entries'],
862
      '--separate-builtins': ['separateBuiltins', parseBool,
863
        'Separate Builtin entries'],
864
      '--separate-stubs': ['separateStubs', parseBool,
865
        'Separate Stub entries'],
866 867
      '--separate-baseline-handlers': ['separateBaselineHandlers', parseBool,
        'Separate Baseline Handler entries'],
868
      '--unix': ['platform', 'unix',
869
        'Specify that we are running on *nix platform'],
870
      '--windows': ['platform', 'windows',
871
        'Specify that we are running on Windows platform'],
872
      '--mac': ['platform', 'mac',
873
        'Specify that we are running on Mac OS X platform'],
874
      '--nm': ['nm', 'nm',
875
        'Specify the \'nm\' executable to use (e.g. --nm=/my_dir/nm)'],
876
      '--objdump': ['objdump', 'objdump',
877
        'Specify the \'objdump\' executable to use (e.g. --objdump=/my_dir/objdump)'],
878
      '--target': ['targetRootFS', '',
879
        'Specify the target root directory for cross environment'],
880
      '--apk-embedded-library': ['apkEmbeddedLibrary', '',
881
        'Specify the path of the embedded library for Android traces'],
882
      '--range': ['range', 'auto,auto',
883
        'Specify the range limit as [start],[end]'],
884
      '--distortion': ['distortion', 0,
885
        'Specify the logging overhead in picoseconds'],
886
      '--source-map': ['sourceMap', null,
887
        'Specify the source map that should be used for output'],
888
      '--timed-range': ['timedRange', true,
889
        'Ignore ticks before first and after last Date.now() call'],
890
      '--pairwise-timed-range': ['pairwiseTimedRange', true,
891
        'Ignore ticks outside pairs of Date.now() calls'],
892
      '--only-summary': ['onlySummary', true,
893
        'Print only tick summary, exclude other information'],
894 895
      '--serialize-vm-symbols': ['serializeVMSymbols', true,
        'Print all C++ symbols and library addresses as JSON data'],
896
      '--preprocess': ['preprocessJson', true,
897
        'Preprocess for consumption with web interface']
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
    };
    dispatch['--js'] = dispatch['-j'];
    dispatch['--gc'] = dispatch['-g'];
    dispatch['--compiler'] = dispatch['-c'];
    dispatch['--other'] = dispatch['-o'];
    dispatch['--external'] = dispatch['-e'];
    dispatch['--ptr'] = dispatch['--pairwise-timed-range'];
    return dispatch;
  }

  getDefaultResults() {
    return {
      logFileName: 'v8.log',
      platform: 'unix',
      stateFilter: null,
      callGraphSize: 5,
      ignoreUnknown: false,
      separateIc: true,
      separateBytecodes: false,
      separateBuiltins: true,
      separateStubs: true,
919
      separateBaselineHandlers: false,
920
      preprocessJson: null,
921
      sourceMap: null,
922 923 924 925 926 927 928 929 930
      targetRootFS: '',
      nm: 'nm',
      objdump: 'objdump',
      range: 'auto,auto',
      distortion: 0,
      timedRange: false,
      pairwiseTimedRange: false,
      onlySummary: false,
      runtimeTimerFilter: null,
931
      serializeVMSymbols: false,
932 933 934
    };
  }
}