Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 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 |
| 4 | // met: |
| 5 | // |
| 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. |
| 15 | // |
| 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. |
| 27 | |
| 28 | /** |
| 29 | * @fileoverview Log Reader is used to process log file produced by V8. |
| 30 | */ |
| 31 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Base class for processing log files. |
| 35 | * |
| 36 | * @param {Array.<Object>} dispatchTable A table used for parsing and processing |
| 37 | * log records. |
| 38 | * @constructor |
| 39 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 40 | function LogReader(dispatchTable) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | /** |
| 42 | * @type {Array.<Object>} |
| 43 | */ |
| 44 | this.dispatchTable_ = dispatchTable; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Current line. |
| 48 | * @type {number} |
| 49 | */ |
| 50 | this.lineNum_ = 0; |
| 51 | |
| 52 | /** |
| 53 | * CSV lines parser. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 54 | * @type {CsvParser} |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 55 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 56 | this.csvParser_ = new CsvParser(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | |
| 60 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | * Used for printing error messages. |
| 62 | * |
| 63 | * @param {string} str Error message. |
| 64 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 65 | LogReader.prototype.printError = function(str) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | // Do nothing. |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Processes a portion of V8 profiler event log. |
| 72 | * |
| 73 | * @param {string} chunk A portion of log. |
| 74 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 75 | LogReader.prototype.processLogChunk = function(chunk) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | this.processLog_(chunk.split('\n')); |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | /** |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 81 | * Processes a line of V8 profiler event log. |
| 82 | * |
| 83 | * @param {string} line A line of log. |
| 84 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 85 | LogReader.prototype.processLogLine = function(line) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 86 | this.processLog_([line]); |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | * Processes stack record. |
| 92 | * |
| 93 | * @param {number} pc Program counter. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 94 | * @param {number} func JS Function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | * @param {Array.<string>} stack String representation of a stack. |
| 96 | * @return {Array.<number>} Processed stack. |
| 97 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 98 | LogReader.prototype.processStack = function(pc, func, stack) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 99 | var fullStack = func ? [pc, func] : [pc]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | var prevFrame = 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)); |
| 111 | } |
| 112 | } |
| 113 | return fullStack; |
| 114 | }; |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * Returns whether a particular dispatch must be skipped. |
| 119 | * |
| 120 | * @param {!Object} dispatch Dispatch record. |
| 121 | * @return {boolean} True if dispatch must be skipped. |
| 122 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 123 | LogReader.prototype.skipDispatch = function(dispatch) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | return false; |
| 125 | }; |
| 126 | |
| 127 | |
| 128 | /** |
| 129 | * Does a dispatch of a log record. |
| 130 | * |
| 131 | * @param {Array.<string>} fields Log record. |
| 132 | * @private |
| 133 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 134 | LogReader.prototype.dispatchLogRow_ = function(fields) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | // Obtain the dispatch. |
| 136 | var command = fields[0]; |
| 137 | if (!(command in this.dispatchTable_)) { |
| 138 | throw new Error('unknown command: ' + command); |
| 139 | } |
| 140 | var dispatch = this.dispatchTable_[command]; |
| 141 | |
| 142 | if (dispatch === null || this.skipDispatch(dispatch)) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | // Parse fields. |
| 147 | var parsedFields = []; |
| 148 | for (var i = 0; i < dispatch.parsers.length; ++i) { |
| 149 | var parser = dispatch.parsers[i]; |
| 150 | if (parser === null) { |
| 151 | parsedFields.push(fields[1 + i]); |
| 152 | } else if (typeof parser == 'function') { |
| 153 | parsedFields.push(parser(fields[1 + i])); |
| 154 | } else { |
| 155 | // var-args |
| 156 | parsedFields.push(fields.slice(1 + i)); |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Run the processor. |
| 162 | dispatch.processor.apply(this, parsedFields); |
| 163 | }; |
| 164 | |
| 165 | |
| 166 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | * Processes log lines. |
| 168 | * |
| 169 | * @param {Array.<string>} lines Log lines. |
| 170 | * @private |
| 171 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame^] | 172 | LogReader.prototype.processLog_ = function(lines) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 173 | for (var i = 0, n = lines.length; i < n; ++i, ++this.lineNum_) { |
| 174 | var line = lines[i]; |
| 175 | if (!line) { |
| 176 | continue; |
| 177 | } |
| 178 | try { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 179 | var fields = this.csvParser_.parseLine(line); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | this.dispatchLogRow_(fields); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 181 | } catch (e) { |
| 182 | this.printError('line ' + (this.lineNum_ + 1) + ': ' + (e.message || e)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | }; |