sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +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 | #include "v8.h" |
| 29 | |
| 30 | #include "log-utils.h" |
| 31 | |
| 32 | namespace v8 { |
| 33 | namespace internal { |
| 34 | |
| 35 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 36 | |
| 37 | LogDynamicBuffer::LogDynamicBuffer( |
| 38 | int block_size, int max_size, const char* seal, int seal_size) |
| 39 | : block_size_(block_size), |
| 40 | max_size_(max_size - (max_size % block_size_)), |
| 41 | seal_(seal), |
| 42 | seal_size_(seal_size), |
| 43 | blocks_(max_size_ / block_size_ + 1), |
| 44 | write_pos_(0), block_index_(0), block_write_pos_(0), is_sealed_(false) { |
| 45 | ASSERT(BlocksCount() > 0); |
| 46 | AllocateBlock(0); |
| 47 | for (int i = 1; i < BlocksCount(); ++i) { |
| 48 | blocks_[i] = NULL; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | LogDynamicBuffer::~LogDynamicBuffer() { |
| 54 | for (int i = 0; i < BlocksCount(); ++i) { |
| 55 | DeleteArray(blocks_[i]); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | int LogDynamicBuffer::Read(int from_pos, char* dest_buf, int buf_size) { |
| 61 | if (buf_size == 0) return 0; |
| 62 | int read_pos = from_pos; |
| 63 | int block_read_index = BlockIndex(from_pos); |
| 64 | int block_read_pos = PosInBlock(from_pos); |
| 65 | int dest_buf_pos = 0; |
| 66 | // Read until dest_buf is filled, or write_pos_ encountered. |
| 67 | while (read_pos < write_pos_ && dest_buf_pos < buf_size) { |
| 68 | const int read_size = Min(write_pos_ - read_pos, |
| 69 | Min(buf_size - dest_buf_pos, block_size_ - block_read_pos)); |
| 70 | memcpy(dest_buf + dest_buf_pos, |
| 71 | blocks_[block_read_index] + block_read_pos, read_size); |
| 72 | block_read_pos += read_size; |
| 73 | dest_buf_pos += read_size; |
| 74 | read_pos += read_size; |
| 75 | if (block_read_pos == block_size_) { |
| 76 | block_read_pos = 0; |
| 77 | ++block_read_index; |
| 78 | } |
| 79 | } |
| 80 | return dest_buf_pos; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | int LogDynamicBuffer::Seal() { |
| 85 | WriteInternal(seal_, seal_size_); |
| 86 | is_sealed_ = true; |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | int LogDynamicBuffer::Write(const char* data, int data_size) { |
| 92 | if (is_sealed_) { |
| 93 | return 0; |
| 94 | } |
| 95 | if ((write_pos_ + data_size) <= (max_size_ - seal_size_)) { |
| 96 | return WriteInternal(data, data_size); |
| 97 | } else { |
| 98 | return Seal(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | int LogDynamicBuffer::WriteInternal(const char* data, int data_size) { |
| 104 | int data_pos = 0; |
| 105 | while (data_pos < data_size) { |
| 106 | const int write_size = |
| 107 | Min(data_size - data_pos, block_size_ - block_write_pos_); |
| 108 | memcpy(blocks_[block_index_] + block_write_pos_, data + data_pos, |
| 109 | write_size); |
| 110 | block_write_pos_ += write_size; |
| 111 | data_pos += write_size; |
| 112 | if (block_write_pos_ == block_size_) { |
| 113 | block_write_pos_ = 0; |
| 114 | AllocateBlock(++block_index_); |
| 115 | } |
| 116 | } |
| 117 | write_pos_ += data_size; |
| 118 | return data_size; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | bool Log::is_stopped_ = false; |
| 123 | Log::WritePtr Log::Write = NULL; |
| 124 | FILE* Log::output_handle_ = NULL; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 125 | FILE* Log::output_code_handle_ = NULL; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 126 | LogDynamicBuffer* Log::output_buffer_ = NULL; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 127 | // Must be the same message as in Logger::PauseProfiler. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 128 | const char* Log::kDynamicBufferSeal = "profiler,\"pause\"\n"; |
| 129 | Mutex* Log::mutex_ = NULL; |
| 130 | char* Log::message_buffer_ = NULL; |
| 131 | |
| 132 | |
| 133 | void Log::Init() { |
| 134 | mutex_ = OS::CreateMutex(); |
| 135 | message_buffer_ = NewArray<char>(kMessageBufferSize); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void Log::OpenStdout() { |
| 140 | ASSERT(!IsEnabled()); |
| 141 | output_handle_ = stdout; |
| 142 | Write = WriteToFile; |
| 143 | Init(); |
| 144 | } |
| 145 | |
| 146 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 147 | static const char kCodeLogExt[] = ".code"; |
| 148 | |
| 149 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 150 | void Log::OpenFile(const char* name) { |
| 151 | ASSERT(!IsEnabled()); |
| 152 | output_handle_ = OS::FOpen(name, OS::LogFileOpenMode); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 153 | if (FLAG_ll_prof) { |
| 154 | // Open a file for logging the contents of code objects so that |
| 155 | // they can be disassembled later. |
| 156 | size_t name_len = strlen(name); |
| 157 | ScopedVector<char> code_name( |
| 158 | static_cast<int>(name_len + sizeof(kCodeLogExt))); |
| 159 | memcpy(code_name.start(), name, name_len); |
| 160 | memcpy(code_name.start() + name_len, kCodeLogExt, sizeof(kCodeLogExt)); |
| 161 | output_code_handle_ = OS::FOpen(code_name.start(), OS::LogFileOpenMode); |
| 162 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 163 | Write = WriteToFile; |
| 164 | Init(); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | void Log::OpenMemoryBuffer() { |
| 169 | ASSERT(!IsEnabled()); |
| 170 | output_buffer_ = new LogDynamicBuffer( |
| 171 | kDynamicBufferBlockSize, kMaxDynamicBufferSize, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 172 | kDynamicBufferSeal, StrLength(kDynamicBufferSeal)); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 173 | Write = WriteToMemory; |
| 174 | Init(); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | void Log::Close() { |
| 179 | if (Write == WriteToFile) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 180 | if (output_handle_ != NULL) fclose(output_handle_); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 181 | output_handle_ = NULL; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 182 | if (output_code_handle_ != NULL) fclose(output_code_handle_); |
| 183 | output_code_handle_ = NULL; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 184 | } else if (Write == WriteToMemory) { |
| 185 | delete output_buffer_; |
| 186 | output_buffer_ = NULL; |
| 187 | } else { |
| 188 | ASSERT(Write == NULL); |
| 189 | } |
| 190 | Write = NULL; |
| 191 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 192 | DeleteArray(message_buffer_); |
| 193 | message_buffer_ = NULL; |
| 194 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 195 | delete mutex_; |
| 196 | mutex_ = NULL; |
| 197 | |
| 198 | is_stopped_ = false; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | int Log::GetLogLines(int from_pos, char* dest_buf, int max_size) { |
| 203 | if (Write != WriteToMemory) return 0; |
| 204 | ASSERT(output_buffer_ != NULL); |
| 205 | ASSERT(from_pos >= 0); |
| 206 | ASSERT(max_size >= 0); |
| 207 | int actual_size = output_buffer_->Read(from_pos, dest_buf, max_size); |
| 208 | ASSERT(actual_size <= max_size); |
| 209 | if (actual_size == 0) return 0; |
| 210 | |
| 211 | // Find previous log line boundary. |
| 212 | char* end_pos = dest_buf + actual_size - 1; |
| 213 | while (end_pos >= dest_buf && *end_pos != '\n') --end_pos; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 214 | actual_size = static_cast<int>(end_pos - dest_buf + 1); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 215 | // If the assertion below is hit, it means that there was no line end |
| 216 | // found --- something wrong has happened. |
| 217 | ASSERT(actual_size > 0); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 218 | ASSERT(actual_size <= max_size); |
| 219 | return actual_size; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | LogMessageBuilder::WriteFailureHandler |
| 224 | LogMessageBuilder::write_failure_handler = NULL; |
| 225 | |
| 226 | |
| 227 | LogMessageBuilder::LogMessageBuilder(): sl(Log::mutex_), pos_(0) { |
| 228 | ASSERT(Log::message_buffer_ != NULL); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | void LogMessageBuilder::Append(const char* format, ...) { |
| 233 | Vector<char> buf(Log::message_buffer_ + pos_, |
| 234 | Log::kMessageBufferSize - pos_); |
| 235 | va_list args; |
| 236 | va_start(args, format); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 237 | AppendVA(format, args); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 238 | va_end(args); |
| 239 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 240 | } |
| 241 | |
| 242 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 243 | void LogMessageBuilder::AppendVA(const char* format, va_list args) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 244 | Vector<char> buf(Log::message_buffer_ + pos_, |
| 245 | Log::kMessageBufferSize - pos_); |
| 246 | int result = v8::internal::OS::VSNPrintF(buf, format, args); |
| 247 | |
| 248 | // Result is -1 if output was truncated. |
| 249 | if (result >= 0) { |
| 250 | pos_ += result; |
| 251 | } else { |
| 252 | pos_ = Log::kMessageBufferSize; |
| 253 | } |
| 254 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | void LogMessageBuilder::Append(const char c) { |
| 259 | if (pos_ < Log::kMessageBufferSize) { |
| 260 | Log::message_buffer_[pos_++] = c; |
| 261 | } |
| 262 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | void LogMessageBuilder::Append(String* str) { |
| 267 | AssertNoAllocation no_heap_allocation; // Ensure string stay valid. |
| 268 | int length = str->length(); |
| 269 | for (int i = 0; i < length; i++) { |
| 270 | Append(static_cast<char>(str->Get(i))); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 275 | void LogMessageBuilder::AppendAddress(Address addr) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 276 | Append("0x%" V8PRIxPTR, addr); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 280 | void LogMessageBuilder::AppendDetailed(String* str, bool show_impl_info) { |
| 281 | AssertNoAllocation no_heap_allocation; // Ensure string stay valid. |
| 282 | int len = str->length(); |
| 283 | if (len > 0x1000) |
| 284 | len = 0x1000; |
| 285 | if (show_impl_info) { |
| 286 | Append(str->IsAsciiRepresentation() ? 'a' : '2'); |
| 287 | if (StringShape(str).IsExternal()) |
| 288 | Append('e'); |
| 289 | if (StringShape(str).IsSymbol()) |
| 290 | Append('#'); |
| 291 | Append(":%i:", str->length()); |
| 292 | } |
| 293 | for (int i = 0; i < len; i++) { |
| 294 | uc32 c = str->Get(i); |
| 295 | if (c > 0xff) { |
| 296 | Append("\\u%04x", c); |
| 297 | } else if (c < 32 || c > 126) { |
| 298 | Append("\\x%02x", c); |
| 299 | } else if (c == ',') { |
| 300 | Append("\\,"); |
| 301 | } else if (c == '\\') { |
| 302 | Append("\\\\"); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame^] | 303 | } else if (c == '\"') { |
| 304 | Append("\"\""); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 305 | } else { |
| 306 | Append("%lc", c); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 312 | void LogMessageBuilder::AppendStringPart(const char* str, int len) { |
| 313 | if (pos_ + len > Log::kMessageBufferSize) { |
| 314 | len = Log::kMessageBufferSize - pos_; |
| 315 | ASSERT(len >= 0); |
| 316 | if (len == 0) return; |
| 317 | } |
| 318 | Vector<char> buf(Log::message_buffer_ + pos_, |
| 319 | Log::kMessageBufferSize - pos_); |
| 320 | OS::StrNCpy(buf, str, len); |
| 321 | pos_ += len; |
| 322 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 323 | } |
| 324 | |
| 325 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 326 | void LogMessageBuilder::WriteToLogFile() { |
| 327 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 328 | const int written = Log::Write(Log::message_buffer_, pos_); |
| 329 | if (written != pos_ && write_failure_handler != NULL) { |
| 330 | write_failure_handler(); |
| 331 | } |
| 332 | } |
| 333 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 334 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 335 | |
| 336 | } } // namespace v8::internal |