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 = "&"; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 38 | |
| 39 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 40 | Log::Log(Logger* logger) |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 41 | : is_stopped_(false), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 42 | output_handle_(NULL), |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 43 | ll_output_handle_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 44 | mutex_(NULL), |
| 45 | message_buffer_(NULL), |
| 46 | logger_(logger) { |
| 47 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 48 | |
| 49 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 50 | static void AddIsolateIdIfNeeded(StringStream* stream) { |
| 51 | Isolate* isolate = Isolate::Current(); |
| 52 | if (isolate->IsDefaultIsolate()) return; |
| 53 | stream->Add("isolate-%p-", isolate); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | void Log::Initialize() { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 58 | mutex_ = OS::CreateMutex(); |
| 59 | message_buffer_ = NewArray<char>(kMessageBufferSize); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 60 | |
| 61 | // --log-all enables all the log flags. |
| 62 | if (FLAG_log_all) { |
| 63 | FLAG_log_runtime = true; |
| 64 | FLAG_log_api = true; |
| 65 | FLAG_log_code = true; |
| 66 | FLAG_log_gc = true; |
| 67 | FLAG_log_suspect = true; |
| 68 | FLAG_log_handles = true; |
| 69 | FLAG_log_regexp = true; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 70 | FLAG_log_internal_timer_events = true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // --prof implies --log-code. |
| 74 | if (FLAG_prof) FLAG_log_code = true; |
| 75 | |
| 76 | // --prof_lazy controls --log-code, implies --noprof_auto. |
| 77 | if (FLAG_prof_lazy) { |
| 78 | FLAG_log_code = false; |
| 79 | FLAG_prof_auto = false; |
| 80 | } |
| 81 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 82 | // 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] | 83 | if (Log::InitLogAtStart()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 84 | if (strcmp(FLAG_logfile, "-") == 0) { |
| 85 | OpenStdout(); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 86 | } else if (strcmp(FLAG_logfile, kLogToTemporaryFile) == 0) { |
| 87 | OpenTemporaryFile(); |
| 88 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 89 | if (strchr(FLAG_logfile, '%') != NULL || |
| 90 | !Isolate::Current()->IsDefaultIsolate()) { |
| 91 | // If there's a '%' in the log file name we have to expand |
| 92 | // placeholders. |
| 93 | HeapStringAllocator allocator; |
| 94 | StringStream stream(&allocator); |
| 95 | AddIsolateIdIfNeeded(&stream); |
| 96 | for (const char* p = FLAG_logfile; *p; p++) { |
| 97 | if (*p == '%') { |
| 98 | p++; |
| 99 | switch (*p) { |
| 100 | case '\0': |
| 101 | // If there's a % at the end of the string we back up |
| 102 | // one character so we can escape the loop properly. |
| 103 | p--; |
| 104 | break; |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 105 | case 'p': |
| 106 | stream.Add("%d", OS::GetCurrentProcessId()); |
| 107 | break; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 108 | case 't': { |
| 109 | // %t expands to the current time in milliseconds. |
| 110 | double time = OS::TimeCurrentMillis(); |
| 111 | stream.Add("%.0f", FmtElm(time)); |
| 112 | break; |
| 113 | } |
| 114 | case '%': |
| 115 | // %% expands (contracts really) to %. |
| 116 | stream.Put('%'); |
| 117 | break; |
| 118 | default: |
| 119 | // All other %'s expand to themselves. |
| 120 | stream.Put('%'); |
| 121 | stream.Put(*p); |
| 122 | break; |
| 123 | } |
| 124 | } else { |
| 125 | stream.Put(*p); |
| 126 | } |
| 127 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 128 | SmartArrayPointer<const char> expanded = stream.ToCString(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 129 | OpenFile(*expanded); |
| 130 | } else { |
| 131 | OpenFile(FLAG_logfile); |
| 132 | } |
| 133 | } |
| 134 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
| 138 | void Log::OpenStdout() { |
| 139 | ASSERT(!IsEnabled()); |
| 140 | output_handle_ = stdout; |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | void Log::OpenTemporaryFile() { |
| 145 | ASSERT(!IsEnabled()); |
| 146 | output_handle_ = i::OS::OpenTemporaryFile(); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 150 | // Extension added to V8 log file name to get the low-level log name. |
| 151 | static const char kLowLevelLogExt[] = ".ll"; |
| 152 | |
| 153 | // File buffer size of the low-level log. We don't use the default to |
| 154 | // minimize the associated overhead. |
| 155 | static const int kLowLevelLogBufferSize = 2 * MB; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 156 | |
| 157 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 158 | void Log::OpenFile(const char* name) { |
| 159 | ASSERT(!IsEnabled()); |
| 160 | output_handle_ = OS::FOpen(name, OS::LogFileOpenMode); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 161 | if (FLAG_ll_prof) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 162 | // Open the low-level log file. |
| 163 | size_t len = strlen(name); |
| 164 | ScopedVector<char> ll_name(static_cast<int>(len + sizeof(kLowLevelLogExt))); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 165 | OS::MemCopy(ll_name.start(), name, len); |
| 166 | OS::MemCopy(ll_name.start() + len, |
| 167 | kLowLevelLogExt, sizeof(kLowLevelLogExt)); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 168 | ll_output_handle_ = OS::FOpen(ll_name.start(), OS::LogFileOpenMode); |
| 169 | setvbuf(ll_output_handle_, NULL, _IOFBF, kLowLevelLogBufferSize); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 170 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 174 | FILE* Log::Close() { |
| 175 | FILE* result = NULL; |
| 176 | if (output_handle_ != NULL) { |
| 177 | if (strcmp(FLAG_logfile, kLogToTemporaryFile) != 0) { |
| 178 | fclose(output_handle_); |
| 179 | } else { |
| 180 | result = output_handle_; |
| 181 | } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 182 | } |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 183 | output_handle_ = NULL; |
| 184 | if (ll_output_handle_ != NULL) fclose(ll_output_handle_); |
| 185 | ll_output_handle_ = NULL; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 186 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 187 | DeleteArray(message_buffer_); |
| 188 | message_buffer_ = NULL; |
| 189 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 190 | delete mutex_; |
| 191 | mutex_ = NULL; |
| 192 | |
| 193 | is_stopped_ = false; |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 194 | return result; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 198 | LogMessageBuilder::LogMessageBuilder(Logger* logger) |
| 199 | : log_(logger->log_), |
| 200 | sl(log_->mutex_), |
| 201 | pos_(0) { |
| 202 | ASSERT(log_->message_buffer_ != NULL); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
| 206 | void LogMessageBuilder::Append(const char* format, ...) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 207 | Vector<char> buf(log_->message_buffer_ + pos_, |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 208 | Log::kMessageBufferSize - pos_); |
| 209 | va_list args; |
| 210 | va_start(args, format); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 211 | AppendVA(format, args); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 212 | va_end(args); |
| 213 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 214 | } |
| 215 | |
| 216 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 217 | void LogMessageBuilder::AppendVA(const char* format, va_list args) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 218 | Vector<char> buf(log_->message_buffer_ + pos_, |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 219 | Log::kMessageBufferSize - pos_); |
| 220 | int result = v8::internal::OS::VSNPrintF(buf, format, args); |
| 221 | |
| 222 | // Result is -1 if output was truncated. |
| 223 | if (result >= 0) { |
| 224 | pos_ += result; |
| 225 | } else { |
| 226 | pos_ = Log::kMessageBufferSize; |
| 227 | } |
| 228 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | void LogMessageBuilder::Append(const char c) { |
| 233 | if (pos_ < Log::kMessageBufferSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 234 | log_->message_buffer_[pos_++] = c; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 235 | } |
| 236 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 237 | } |
| 238 | |
| 239 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 240 | void LogMessageBuilder::AppendDoubleQuotedString(const char* string) { |
| 241 | Append('"'); |
| 242 | for (const char* p = string; *p != '\0'; p++) { |
| 243 | if (*p == '"') { |
| 244 | Append('\\'); |
| 245 | } |
| 246 | Append(*p); |
| 247 | } |
| 248 | Append('"'); |
| 249 | } |
| 250 | |
| 251 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 252 | void LogMessageBuilder::Append(String* str) { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 253 | DisallowHeapAllocation no_gc; // Ensure string stay valid. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 254 | int length = str->length(); |
| 255 | for (int i = 0; i < length; i++) { |
| 256 | Append(static_cast<char>(str->Get(i))); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 261 | void LogMessageBuilder::AppendAddress(Address addr) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 262 | Append("0x%" V8PRIxPTR, addr); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 266 | void LogMessageBuilder::AppendDetailed(String* str, bool show_impl_info) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 267 | if (str == NULL) return; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 268 | DisallowHeapAllocation no_gc; // Ensure string stay valid. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 269 | int len = str->length(); |
| 270 | if (len > 0x1000) |
| 271 | len = 0x1000; |
| 272 | if (show_impl_info) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 273 | Append(str->IsOneByteRepresentation() ? 'a' : '2'); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 274 | if (StringShape(str).IsExternal()) |
| 275 | Append('e'); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 276 | if (StringShape(str).IsInternalized()) |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 277 | Append('#'); |
| 278 | Append(":%i:", str->length()); |
| 279 | } |
| 280 | for (int i = 0; i < len; i++) { |
| 281 | uc32 c = str->Get(i); |
| 282 | if (c > 0xff) { |
| 283 | Append("\\u%04x", c); |
| 284 | } else if (c < 32 || c > 126) { |
| 285 | Append("\\x%02x", c); |
| 286 | } else if (c == ',') { |
| 287 | Append("\\,"); |
| 288 | } else if (c == '\\') { |
| 289 | Append("\\\\"); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 290 | } else if (c == '\"') { |
| 291 | Append("\"\""); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 292 | } else { |
| 293 | Append("%lc", c); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 299 | void LogMessageBuilder::AppendStringPart(const char* str, int len) { |
| 300 | if (pos_ + len > Log::kMessageBufferSize) { |
| 301 | len = Log::kMessageBufferSize - pos_; |
| 302 | ASSERT(len >= 0); |
| 303 | if (len == 0) return; |
| 304 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 305 | Vector<char> buf(log_->message_buffer_ + pos_, |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 306 | Log::kMessageBufferSize - pos_); |
| 307 | OS::StrNCpy(buf, str, len); |
| 308 | pos_ += len; |
| 309 | ASSERT(pos_ <= Log::kMessageBufferSize); |
| 310 | } |
| 311 | |
| 312 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 313 | void LogMessageBuilder::WriteToLogFile() { |
| 314 | ASSERT(pos_ <= Log::kMessageBufferSize); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 315 | const int written = log_->WriteToFile(log_->message_buffer_, pos_); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 316 | if (written != pos_) { |
| 317 | log_->stop(); |
| 318 | log_->logger_->LogFailure(); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 322 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 323 | } } // namespace v8::internal |