1 // Copyright 2011 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
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.
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.
29 * @fileoverview Log Reader is used to process log file produced by V8.
34 * Base class for processing log files.
36 * @param {Array.<Object>} dispatchTable A table used for parsing and processing
40 function LogReader(dispatchTable) {
42 * @type {Array.<Object>}
44 this.dispatchTable_ = dispatchTable;
56 this.csvParser_ = new CsvParser();
61 * Used for printing error messages.
63 * @param {string} str Error message.
65 LogReader.prototype.printError = function(str) {
71 * Processes a portion of V8 profiler event log.
73 * @param {string} chunk A portion of log.
75 LogReader.prototype.processLogChunk = function(chunk) {
76 this.processLog_(chunk.split('\n'));
81 * Processes a line of V8 profiler event log.
83 * @param {string} line A line of log.
85 LogReader.prototype.processLogLine = function(line) {
86 this.processLog_([line]);
91 * Processes stack record.
93 * @param {number} pc Program counter.
94 * @param {number} func JS Function.
95 * @param {Array.<string>} stack String representation of a stack.
96 * @return {Array.<number>} Processed stack.
98 LogReader.prototype.processStack = function(pc, func, stack) {
99 var fullStack = func ? [pc, func] : [pc];
101 for (var i = 0, n = stack.length; i < n; ++i) {
102 var frame = stack[i];
103 var firstChar = frame.charAt(0);
104 if (firstChar == '+' || firstChar == '-') {
105 // An offset from the previous frame.
106 prevFrame += parseInt(frame, 16);
107 fullStack.push(prevFrame);
108 // Filter out possible 'overflow' string.
109 } else if (firstChar != 'o') {
110 fullStack.push(parseInt(frame, 16));
112 print("dropping: " + frame);
120 * Returns whether a particular dispatch must be skipped.
122 * @param {!Object} dispatch Dispatch record.
123 * @return {boolean} True if dispatch must be skipped.
125 LogReader.prototype.skipDispatch = function(dispatch) {
131 * Does a dispatch of a log record.
133 * @param {Array.<string>} fields Log record.
136 LogReader.prototype.dispatchLogRow_ = function(fields) {
137 // Obtain the dispatch.
138 var command = fields[0];
139 if (!(command in this.dispatchTable_)) return;
141 var dispatch = this.dispatchTable_[command];
143 if (dispatch === null || this.skipDispatch(dispatch)) {
148 var parsedFields = [];
149 for (var i = 0; i < dispatch.parsers.length; ++i) {
150 var parser = dispatch.parsers[i];
151 if (parser === null) {
152 parsedFields.push(fields[1 + i]);
153 } else if (typeof parser == 'function') {
154 parsedFields.push(parser(fields[1 + i]));
157 parsedFields.push(fields.slice(1 + i));
162 // Run the processor.
163 dispatch.processor.apply(this, parsedFields);
168 * Processes log lines.
170 * @param {Array.<string>} lines Log lines.
173 LogReader.prototype.processLog_ = function(lines) {
174 for (var i = 0, n = lines.length; i < n; ++i, ++this.lineNum_) {
180 var fields = this.csvParser_.parseLine(line);
181 this.dispatchLogRow_(fields);
183 this.printError('line ' + (this.lineNum_ + 1) + ': ' + (e.message || e));