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" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 31 | #include "string-stream.h" |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 32 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 36 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 37 | const char* const Log::kLogToTemporaryFile = "&"; |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 38 | const char* const Log::kLogToConsole = "-"; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 39 | |
| 40 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 41 | Log::Log(Logger* logger) |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 42 | : is_stopped_(false), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 43 | output_handle_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 44 | message_buffer_(NULL), |
| 45 | logger_(logger) { |
| 46 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 47 | |
| 48 | |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 49 | void Log::Initialize(const char* log_file_name) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 50 | message_buffer_ = NewArray<char>(kMessageBufferSize); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 51 | |
| 52 | // --log-all enables all the log flags. |
| 53 | if (FLAG_log_all) { |
| 54 | FLAG_log_runtime = true; |
| 55 | FLAG_log_api = true; |
| 56 | FLAG_log_code = true; |
| 57 | FLAG_log_gc = true; |
| 58 | FLAG_log_suspect = true; |
| 59 | FLAG_log_handles = true; |
| 60 | FLAG_log_regexp = true; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 61 | FLAG_log_internal_timer_events = true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // --prof implies --log-code. |
| 65 | if (FLAG_prof) FLAG_log_code = true; |
| 66 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 67 | // If we're logging anything, we need to open the log file. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 68 | if (Log::InitLogAtStart()) { |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 69 | if (strcmp(log_file_name, kLogToConsole) == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 70 | OpenStdout(); |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 71 | } else if (strcmp(log_file_name, kLogToTemporaryFile) == 0) { |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 72 | OpenTemporaryFile(); |
| 73 | } else { |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 74 | OpenFile(log_file_name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
| 80 | void Log::OpenStdout() { |
| 81 | ASSERT(!IsEnabled()); |
| 82 | output_handle_ = stdout; |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
| 86 | void Log::OpenTemporaryFile() { |
| 87 | ASSERT(!IsEnabled()); |
| 88 | output_handle_ = i::OS::OpenTemporaryFile(); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
| 92 | void Log::OpenFile(const char* name) { |
| 93 | ASSERT(!IsEnabled()); |
| 94 | output_handle_ = OS::FOpen(name, OS::LogFileOpenMode); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 98 | FILE* Log::Close() { |
| 99 | FILE* result = NULL; |
| 100 | if (output_handle_ != NULL) { |
| 101 | if (strcmp(FLAG_logfile, kLogToTemporaryFile) != 0) { |
| 102 | fclose(output_handle_); |
| 103 | } else { |
| 104 | result = output_handle_; |
| 105 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 106 | } |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 107 | output_handle_ = NULL; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 108 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 109 | DeleteArray(message_buffer_); |
| 110 | message_buffer_ = NULL; |
| 111 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 112 | is_stopped_ = false; |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 113 | return result; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 117 | Log::MessageBuilder::MessageBuilder(Log* log) |
| 118 | : log_(log), |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 119 | lock_guard_(&log_->mutex_), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 120 | pos_(0) { |
| 121 | ASSERT(log_->message_buffer_ != NULL); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 125 | void Log::MessageBuilder::Append(const char* format, ...) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 126 | Vector<char> buf(log_->message_buffer_ + pos_, |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 127 | Log::kMessageBufferSize - pos_); |
| 128 | va_list args; |
| 129 | va_start(args, format); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 130 | AppendVA(format, args); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 131 | va_end(args); |
| 132 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 133 | } |
| 134 | |
| 135 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 136 | void Log::MessageBuilder::AppendVA(const char* format, va_list args) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 137 | Vector<char> buf(log_->message_buffer_ + pos_, |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 138 | Log::kMessageBufferSize - pos_); |
| 139 | int result = v8::internal::OS::VSNPrintF(buf, format, args); |
| 140 | |
| 141 | // Result is -1 if output was truncated. |
| 142 | if (result >= 0) { |
| 143 | pos_ += result; |
| 144 | } else { |
| 145 | pos_ = Log::kMessageBufferSize; |
| 146 | } |
| 147 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 148 | } |
| 149 | |
| 150 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 151 | void Log::MessageBuilder::Append(const char c) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 152 | if (pos_ < Log::kMessageBufferSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 153 | log_->message_buffer_[pos_++] = c; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 154 | } |
| 155 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 156 | } |
| 157 | |
| 158 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 159 | void Log::MessageBuilder::AppendDoubleQuotedString(const char* string) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 160 | Append('"'); |
| 161 | for (const char* p = string; *p != '\0'; p++) { |
| 162 | if (*p == '"') { |
| 163 | Append('\\'); |
| 164 | } |
| 165 | Append(*p); |
| 166 | } |
| 167 | Append('"'); |
| 168 | } |
| 169 | |
| 170 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 171 | void Log::MessageBuilder::Append(String* str) { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 172 | DisallowHeapAllocation no_gc; // Ensure string stay valid. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 173 | int length = str->length(); |
| 174 | for (int i = 0; i < length; i++) { |
| 175 | Append(static_cast<char>(str->Get(i))); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 180 | void Log::MessageBuilder::AppendAddress(Address addr) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 181 | Append("0x%" V8PRIxPTR, addr); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 185 | void Log::MessageBuilder::AppendSymbolName(Symbol* symbol) { |
| 186 | ASSERT(symbol); |
| 187 | Append("symbol("); |
| 188 | if (!symbol->name()->IsUndefined()) { |
| 189 | Append("\""); |
| 190 | AppendDetailed(String::cast(symbol->name()), false); |
| 191 | Append("\" "); |
| 192 | } |
| 193 | Append("hash %x)", symbol->Hash()); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void Log::MessageBuilder::AppendDetailed(String* str, bool show_impl_info) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 198 | if (str == NULL) return; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 199 | DisallowHeapAllocation no_gc; // Ensure string stay valid. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 200 | int len = str->length(); |
| 201 | if (len > 0x1000) |
| 202 | len = 0x1000; |
| 203 | if (show_impl_info) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 204 | Append(str->IsOneByteRepresentation() ? 'a' : '2'); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 205 | if (StringShape(str).IsExternal()) |
| 206 | Append('e'); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 207 | if (StringShape(str).IsInternalized()) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 208 | Append('#'); |
| 209 | Append(":%i:", str->length()); |
| 210 | } |
| 211 | for (int i = 0; i < len; i++) { |
| 212 | uc32 c = str->Get(i); |
| 213 | if (c > 0xff) { |
| 214 | Append("\\u%04x", c); |
| 215 | } else if (c < 32 || c > 126) { |
| 216 | Append("\\x%02x", c); |
| 217 | } else if (c == ',') { |
| 218 | Append("\\,"); |
| 219 | } else if (c == '\\') { |
| 220 | Append("\\\\"); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 221 | } else if (c == '\"') { |
| 222 | Append("\"\""); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 223 | } else { |
| 224 | Append("%lc", c); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 230 | void Log::MessageBuilder::AppendStringPart(const char* str, int len) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 231 | if (pos_ + len > Log::kMessageBufferSize) { |
| 232 | len = Log::kMessageBufferSize - pos_; |
| 233 | ASSERT(len >= 0); |
| 234 | if (len == 0) return; |
| 235 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 236 | Vector<char> buf(log_->message_buffer_ + pos_, |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 237 | Log::kMessageBufferSize - pos_); |
| 238 | OS::StrNCpy(buf, str, len); |
| 239 | pos_ += len; |
| 240 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 241 | } |
| 242 | |
| 243 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 244 | void Log::MessageBuilder::WriteToLogFile() { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 245 | ASSERT(pos_ <= Log::kMessageBufferSize); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 246 | const int written = log_->WriteToFile(log_->message_buffer_, pos_); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 247 | if (written != pos_) { |
| 248 | log_->stop(); |
| 249 | log_->logger_->LogFailure(); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 253 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 254 | } } // namespace v8::internal |