1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 * @fileoverview Log Reader is used to process log file produced by V8.
10 base.exportTo('tracing.importer.v8', function() {
12 * Creates a CSV lines parser.
14 function CsvParser() {
19 * A regex for matching a CSV field.
22 CsvParser.CSV_FIELD_RE_ = /^"((?:[^"]|"")*)"|([^,]*)/;
26 * A regex for matching a double quote.
29 CsvParser.DOUBLE_QUOTE_RE_ = /""/g;
33 * Parses a line of CSV-encoded values. Returns an array of fields.
35 * @param {string} line Input line.
37 CsvParser.prototype.parseLine = function(line) {
38 var fieldRe = CsvParser.CSV_FIELD_RE_;
39 var doubleQuoteRe = CsvParser.DOUBLE_QUOTE_RE_;
41 var endPos = line.length;
45 var fieldMatch = fieldRe.exec(line.substr(pos));
46 if (typeof fieldMatch[1] === 'string') {
47 var field = fieldMatch[1];
48 pos += field.length + 3; // Skip comma and quotes.
49 fields.push(field.replace(doubleQuoteRe, '"'));
51 // The second field pattern will match anything, thus
52 // in the worst case the match will be an empty string.
53 var field = fieldMatch[2];
54 pos += field.length + 1; // Skip comma.
57 } while (pos <= endPos);
63 * Base class for processing log files.
65 * @param {Array.<Object>} dispatchTable A table used for parsing and
66 * processing log records.
70 function LogReader(dispatchTable) {
72 * @type {Array.<Object>}
74 this.dispatchTable_ = dispatchTable;
86 this.csvParser_ = new CsvParser();
91 * Used for printing error messages.
93 * @param {string} str Error message.
95 LogReader.prototype.printError = function(str) {
101 * Processes a portion of V8 profiler event log.
103 * @param {string} chunk A portion of log.
105 LogReader.prototype.processLogChunk = function(chunk) {
106 this.processLog_(chunk.split('\n'));
111 * Processes a line of V8 profiler event log.
113 * @param {string} line A line of log.
115 LogReader.prototype.processLogLine = function(line) {
116 this.processLog_([line]);
121 * Processes stack record.
123 * @param {number} pc Program counter.
124 * @param {number} func JS Function.
125 * @param {Array.<string>} stack String representation of a stack.
126 * @return {Array.<number>} Processed stack.
128 LogReader.prototype.processStack = function(pc, func, stack) {
129 var fullStack = func ? [pc, func] : [pc];
131 for (var i = 0, n = stack.length; i < n; ++i) {
132 var frame = stack[i];
133 var firstChar = frame.charAt(0);
134 if (firstChar == '+' || firstChar == '-') {
135 // An offset from the previous frame.
136 prevFrame += parseInt(frame, 16);
137 fullStack.push(prevFrame);
138 // Filter out possible 'overflow' string.
139 } else if (firstChar != 'o') {
140 fullStack.push(parseInt(frame, 16));
148 * Returns whether a particular dispatch must be skipped.
150 * @param {!Object} dispatch Dispatch record.
151 * @return {boolean} True if dispatch must be skipped.
153 LogReader.prototype.skipDispatch = function(dispatch) {
159 * Does a dispatch of a log record.
161 * @param {Array.<string>} fields Log record.
164 LogReader.prototype.dispatchLogRow_ = function(fields) {
165 // Obtain the dispatch.
166 var command = fields[0];
167 if (!(command in this.dispatchTable_)) return;
169 var dispatch = this.dispatchTable_[command];
171 if (dispatch === null || this.skipDispatch(dispatch)) {
176 var parsedFields = [];
177 for (var i = 0; i < dispatch.parsers.length; ++i) {
178 var parser = dispatch.parsers[i];
179 if (parser === null) {
180 parsedFields.push(fields[1 + i]);
181 } else if (typeof parser == 'function') {
182 parsedFields.push(parser(fields[1 + i]));
185 parsedFields.push(fields.slice(1 + i));
190 // Run the processor.
191 dispatch.processor.apply(this, parsedFields);
196 * Processes log lines.
198 * @param {Array.<string>} lines Log lines.
201 LogReader.prototype.processLog_ = function(lines) {
202 for (var i = 0, n = lines.length; i < n; ++i, ++this.lineNum_) {
208 var fields = this.csvParser_.parseLine(line);
209 this.dispatchLogRow_(fields);
211 this.printError('line ' + (this.lineNum_ + 1) + ': ' +