Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-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 | #ifndef V8_LOG_UTILS_H_ |
| 29 | #define V8_LOG_UTILS_H_ |
| 30 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 31 | #include "allocation.h" |
| 32 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 37 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 38 | class Logger; |
| 39 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | // A memory buffer that increments its size as you write in it. Size |
| 41 | // is incremented with 'block_size' steps, never exceeding 'max_size'. |
| 42 | // During growth, memory contents are never copied. At the end of the |
| 43 | // buffer an amount of memory specified in 'seal_size' is reserved. |
| 44 | // When writing position reaches max_size - seal_size, buffer auto-seals |
| 45 | // itself with 'seal' and allows no further writes. Data pointed by |
| 46 | // 'seal' must be available during entire LogDynamicBuffer lifetime. |
| 47 | // |
| 48 | // An instance of this class is created dynamically by Log. |
| 49 | class LogDynamicBuffer { |
| 50 | public: |
| 51 | LogDynamicBuffer( |
| 52 | int block_size, int max_size, const char* seal, int seal_size); |
| 53 | |
| 54 | ~LogDynamicBuffer(); |
| 55 | |
| 56 | // Reads contents of the buffer starting from 'from_pos'. Upon |
| 57 | // return, 'dest_buf' is filled with the data. Actual amount of data |
| 58 | // filled is returned, it is <= 'buf_size'. |
| 59 | int Read(int from_pos, char* dest_buf, int buf_size); |
| 60 | |
| 61 | // Writes 'data' to the buffer, making it larger if necessary. If |
| 62 | // data is too big to fit in the buffer, it doesn't get written at |
| 63 | // all. In that case, buffer auto-seals itself and stops to accept |
| 64 | // any incoming writes. Returns amount of data written (it is either |
| 65 | // 'data_size', or 0, if 'data' is too big). |
| 66 | int Write(const char* data, int data_size); |
| 67 | |
| 68 | private: |
| 69 | void AllocateBlock(int index) { |
| 70 | blocks_[index] = NewArray<char>(block_size_); |
| 71 | } |
| 72 | |
| 73 | int BlockIndex(int pos) const { return pos / block_size_; } |
| 74 | |
| 75 | int BlocksCount() const { return BlockIndex(max_size_) + 1; } |
| 76 | |
| 77 | int PosInBlock(int pos) const { return pos % block_size_; } |
| 78 | |
| 79 | int Seal(); |
| 80 | |
| 81 | int WriteInternal(const char* data, int data_size); |
| 82 | |
| 83 | const int block_size_; |
| 84 | const int max_size_; |
| 85 | const char* seal_; |
| 86 | const int seal_size_; |
| 87 | ScopedVector<char*> blocks_; |
| 88 | int write_pos_; |
| 89 | int block_index_; |
| 90 | int block_write_pos_; |
| 91 | bool is_sealed_; |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | // Functions and data for performing output of log messages. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 96 | class Log { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 99 | // Performs process-wide initialization. |
| 100 | void Initialize(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | |
| 102 | // Disables logging, but preserves acquired resources. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 103 | void stop() { is_stopped_ = true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 104 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 105 | // Frees all resources acquired in Initialize and Open... functions. |
| 106 | void Close(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 107 | |
| 108 | // See description in include/v8.h. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 109 | int GetLogLines(int from_pos, char* dest_buf, int max_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 110 | |
| 111 | // Returns whether logging is enabled. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 112 | bool IsEnabled() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | return !is_stopped_ && (output_handle_ != NULL || output_buffer_ != NULL); |
| 114 | } |
| 115 | |
| 116 | // Size of buffer used for formatting log messages. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 117 | static const int kMessageBufferSize = v8::V8::kMinimumSizeForLogLinesBuffer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 118 | |
| 119 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 120 | explicit Log(Logger* logger); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 122 | // Opens stdout for logging. |
| 123 | void OpenStdout(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 125 | // Opens file for logging. |
| 126 | void OpenFile(const char* name); |
| 127 | |
| 128 | // Opens memory buffer for logging. |
| 129 | void OpenMemoryBuffer(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | |
| 131 | // Implementation of writing to a log file. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 132 | int WriteToFile(const char* msg, int length) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 133 | ASSERT(output_handle_ != NULL); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 134 | size_t rv = fwrite(msg, 1, length, output_handle_); |
| 135 | ASSERT(static_cast<size_t>(length) == rv); |
| 136 | USE(rv); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 137 | fflush(output_handle_); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 138 | return length; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Implementation of writing to a memory buffer. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 142 | int WriteToMemory(const char* msg, int length) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 143 | ASSERT(output_buffer_ != NULL); |
| 144 | return output_buffer_->Write(msg, length); |
| 145 | } |
| 146 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 147 | bool write_to_file_; |
| 148 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 149 | // Whether logging is stopped (e.g. due to insufficient resources). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 150 | bool is_stopped_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 151 | |
| 152 | // When logging is active, either output_handle_ or output_buffer_ is used |
| 153 | // to store a pointer to log destination. If logging was opened via OpenStdout |
| 154 | // or OpenFile, then output_handle_ is used. If logging was opened |
| 155 | // via OpenMemoryBuffer, then output_buffer_ is used. |
| 156 | // mutex_ should be acquired before using output_handle_ or output_buffer_. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 157 | FILE* output_handle_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 159 | // Used when low-level profiling is active. |
| 160 | FILE* ll_output_handle_; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 161 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 162 | LogDynamicBuffer* output_buffer_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | |
| 164 | // Size of dynamic buffer block (and dynamic buffer initial size). |
| 165 | static const int kDynamicBufferBlockSize = 65536; |
| 166 | |
| 167 | // Maximum size of dynamic buffer. |
| 168 | static const int kMaxDynamicBufferSize = 50 * 1024 * 1024; |
| 169 | |
| 170 | // Message to "seal" dynamic buffer with. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 171 | static const char* const kDynamicBufferSeal; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 172 | |
| 173 | // mutex_ is a Mutex used for enforcing exclusive |
| 174 | // access to the formatting buffer and the log file or log memory buffer. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 175 | Mutex* mutex_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | |
| 177 | // Buffer used for formatting log messages. This is a singleton buffer and |
| 178 | // mutex_ should be acquired before using it. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 179 | char* message_buffer_; |
| 180 | |
| 181 | Logger* logger_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 182 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 183 | friend class Logger; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | friend class LogMessageBuilder; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | |
| 188 | // Utility class for formatting log messages. It fills the message into the |
| 189 | // static buffer in Log. |
| 190 | class LogMessageBuilder BASE_EMBEDDED { |
| 191 | public: |
| 192 | // Create a message builder starting from position 0. This acquires the mutex |
| 193 | // in the log as well. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 194 | explicit LogMessageBuilder(Logger* logger); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | ~LogMessageBuilder() { } |
| 196 | |
| 197 | // Append string data to the log message. |
| 198 | void Append(const char* format, ...); |
| 199 | |
| 200 | // Append string data to the log message. |
| 201 | void AppendVA(const char* format, va_list args); |
| 202 | |
| 203 | // Append a character to the log message. |
| 204 | void Append(const char c); |
| 205 | |
| 206 | // Append a heap string. |
| 207 | void Append(String* str); |
| 208 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 209 | // Appends an address. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 210 | void AppendAddress(Address addr); |
| 211 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | void AppendDetailed(String* str, bool show_impl_info); |
| 213 | |
| 214 | // Append a portion of a string. |
| 215 | void AppendStringPart(const char* str, int len); |
| 216 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 217 | // Write the log message to the log file currently opened. |
| 218 | void WriteToLogFile(); |
| 219 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 220 | private: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 221 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 222 | Log* log_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 223 | ScopedLock sl; |
| 224 | int pos_; |
| 225 | }; |
| 226 | |
| 227 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 228 | |
| 229 | } } // namespace v8::internal |
| 230 | |
| 231 | #endif // V8_LOG_UTILS_H_ |