{e.message || e}`); throw e; } } /** * Used for printing error messages. * * @param {string} str Error message. */ printError(str) { // Do nothing. } /** * Processes a portion of V8 profiler event log. * * @param {string} chunk A portion of log. */ async processLogChunk(chunk) { await this.processLog_(chunk.split('\n')); } /** * Processes a line of V8 profiler event log. * * @param {string} line A line of log. */ async processLogLine(line) { if (!this.timedRange_) { await this.processLogLine_(line); return; } if (line.startsWith("current-time")) { if (this.hasSeenTimerMarker_) { await this.processLog_(this.logLinesSinceLastTimerMarker_); this.logLinesSinceLastTimerMarker_ = []; // In pairwise mode, a "current-time" line ends the timed range. if (this.pairwiseTimedRange_) { this.hasSeenTimerMarker_ = false; } } else { this.hasSeenTimerMarker_ = true; } } else { if (this.hasSeenTimerMarker_) { this.logLinesSinceLastTimerMarker_.push(line); } else if (!line.startsWith("tick")) { await this.processLogLine_(line); } } } /** * Processes stack record. * * @param {number} pc Program counter. * @param {number} func JS Function. * @param {Array.} stack String representation of a stack. * @return {Array.} Processed stack. */ processStack(pc, func, stack) { const fullStack = func ? [pc, func] : [pc]; let prevFrame = pc; for (let i = 0, n = stack.length; i < n; ++i) { const frame = stack[i]; const firstChar = frame.charAt(0); if (firstChar == '+' || firstChar == '-') { // An offset from the previous frame. prevFrame += parseInt(frame, 16); fullStack.push(prevFrame); // Filter out possible 'overflow' string. } else if (firstChar != 'o') { fullStack.push(parseInt(frame, 16)); } else { console.error(`dropping: ${frame}`); } } return fullStack; } /** * Does a dispatch of a log record. * * @param {Array.} fields Log record. * @private */ async dispatchLogRow_(fields) { // Obtain the dispatch. const command = fields[0]; const dispatch = this.dispatchTable_[command]; if (dispatch === undefined) return; const parsers = dispatch.parsers; const length = parsers.length; // Parse fields. const parsedFields = []; for (let i = 0; i < length; ++i) { const parser = parsers[i]; if (parser === parseString) { parsedFields.push(fields[1 + i]); } else if (typeof parser == 'function') { parsedFields.push(parser(fields[1 + i])); } else if (parser === parseVarArgs) { // var-args parsedFields.push(fields.slice(1 + i)); break; } else { throw new Error(`Invalid log field parser: ${parser}`); } } // Run the processor. await dispatch.processor.apply(this, parsedFields); } /** * Processes log lines. * * @param {Array.} lines Log lines. * @private */ async processLog_(lines) { for (let i = 0, n = lines.length; i < n; ++i) { await this.processLogLine_(lines[i]); } } /** * Processes a single log line. * * @param {String} a log line * @private */ async processLogLine_(line) { if (line.length > 0) { try { const fields = this.csvParser_.parseLine(line); await this.dispatchLogRow_(fields); } catch (e) { this.printError(`line ${this.lineNum_ + 1}: ${e.message || e}\n${e.stack}`); } } this.lineNum_++; } }