2009-05-28 07:08:09 +00:00
|
|
|
// Copyright 2009 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "log-utils.h"
|
2011-03-18 20:35:07 +00:00
|
|
|
#include "string-stream.h"
|
2009-05-28 07:08:09 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2009-05-28 13:56:32 +00:00
|
|
|
|
2011-08-05 11:32:46 +00:00
|
|
|
const char* const Log::kLogToTemporaryFile = "&";
|
2013-07-15 11:35:39 +00:00
|
|
|
const char* const Log::kLogToConsole = "-";
|
2009-05-28 13:56:32 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
Log::Log(Logger* logger)
|
2011-07-13 11:31:22 +00:00
|
|
|
: is_stopped_(false),
|
2011-03-18 20:35:07 +00:00
|
|
|
output_handle_(NULL),
|
|
|
|
message_buffer_(NULL),
|
|
|
|
logger_(logger) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-15 11:35:39 +00:00
|
|
|
void Log::Initialize(const char* log_file_name) {
|
2009-05-28 07:08:09 +00:00
|
|
|
message_buffer_ = NewArray<char>(kMessageBufferSize);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
|
|
|
// --log-all enables all the log flags.
|
|
|
|
if (FLAG_log_all) {
|
|
|
|
FLAG_log_runtime = true;
|
|
|
|
FLAG_log_api = true;
|
|
|
|
FLAG_log_code = true;
|
|
|
|
FLAG_log_gc = true;
|
|
|
|
FLAG_log_suspect = true;
|
|
|
|
FLAG_log_handles = true;
|
|
|
|
FLAG_log_regexp = true;
|
2012-12-05 16:22:14 +00:00
|
|
|
FLAG_log_internal_timer_events = true;
|
2011-03-18 20:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --prof implies --log-code.
|
|
|
|
if (FLAG_prof) FLAG_log_code = true;
|
|
|
|
|
|
|
|
// If we're logging anything, we need to open the log file.
|
2013-04-24 14:44:08 +00:00
|
|
|
if (Log::InitLogAtStart()) {
|
2013-07-15 11:35:39 +00:00
|
|
|
if (strcmp(log_file_name, kLogToConsole) == 0) {
|
2011-03-18 20:35:07 +00:00
|
|
|
OpenStdout();
|
2013-07-15 11:35:39 +00:00
|
|
|
} else if (strcmp(log_file_name, kLogToTemporaryFile) == 0) {
|
2011-07-13 11:31:22 +00:00
|
|
|
OpenTemporaryFile();
|
|
|
|
} else {
|
2013-07-15 11:35:39 +00:00
|
|
|
OpenFile(log_file_name);
|
2011-03-18 20:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-28 07:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Log::OpenStdout() {
|
|
|
|
ASSERT(!IsEnabled());
|
|
|
|
output_handle_ = stdout;
|
2011-07-13 11:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Log::OpenTemporaryFile() {
|
|
|
|
ASSERT(!IsEnabled());
|
|
|
|
output_handle_ = i::OS::OpenTemporaryFile();
|
2009-05-28 07:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Log::OpenFile(const char* name) {
|
|
|
|
ASSERT(!IsEnabled());
|
|
|
|
output_handle_ = OS::FOpen(name, OS::LogFileOpenMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-13 11:31:22 +00:00
|
|
|
FILE* Log::Close() {
|
|
|
|
FILE* result = NULL;
|
|
|
|
if (output_handle_ != NULL) {
|
|
|
|
if (strcmp(FLAG_logfile, kLogToTemporaryFile) != 0) {
|
|
|
|
fclose(output_handle_);
|
|
|
|
} else {
|
|
|
|
result = output_handle_;
|
|
|
|
}
|
2009-05-28 07:08:09 +00:00
|
|
|
}
|
2011-07-13 11:31:22 +00:00
|
|
|
output_handle_ = NULL;
|
2009-05-28 07:08:09 +00:00
|
|
|
|
2009-06-11 14:08:34 +00:00
|
|
|
DeleteArray(message_buffer_);
|
|
|
|
message_buffer_ = NULL;
|
|
|
|
|
2009-05-28 13:56:32 +00:00
|
|
|
is_stopped_ = false;
|
2011-07-13 11:31:22 +00:00
|
|
|
return result;
|
2009-05-28 07:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 17:19:31 +00:00
|
|
|
Log::MessageBuilder::MessageBuilder(Log* log)
|
|
|
|
: log_(log),
|
2013-08-29 09:58:30 +00:00
|
|
|
lock_guard_(&log_->mutex_),
|
2011-03-18 20:35:07 +00:00
|
|
|
pos_(0) {
|
|
|
|
ASSERT(log_->message_buffer_ != NULL);
|
2009-05-28 07:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 17:19:31 +00:00
|
|
|
void Log::MessageBuilder::Append(const char* format, ...) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Vector<char> buf(log_->message_buffer_ + pos_,
|
2009-05-28 07:08:09 +00:00
|
|
|
Log::kMessageBufferSize - pos_);
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2009-06-15 15:49:03 +00:00
|
|
|
AppendVA(format, args);
|
2009-05-28 07:08:09 +00:00
|
|
|
va_end(args);
|
|
|
|
ASSERT(pos_ <= Log::kMessageBufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 17:19:31 +00:00
|
|
|
void Log::MessageBuilder::AppendVA(const char* format, va_list args) {
|
2011-03-18 20:35:07 +00:00
|
|
|
Vector<char> buf(log_->message_buffer_ + pos_,
|
2009-05-28 07:08:09 +00:00
|
|
|
Log::kMessageBufferSize - pos_);
|
|
|
|
int result = v8::internal::OS::VSNPrintF(buf, format, args);
|
|
|
|
|
|
|
|
// Result is -1 if output was truncated.
|
|
|
|
if (result >= 0) {
|
|
|
|
pos_ += result;
|
|
|
|
} else {
|
|
|
|
pos_ = Log::kMessageBufferSize;
|
|
|
|
}
|
|
|
|
ASSERT(pos_ <= Log::kMessageBufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 17:19:31 +00:00
|
|
|
void Log::MessageBuilder::Append(const char c) {
|
2009-05-28 07:08:09 +00:00
|
|
|
if (pos_ < Log::kMessageBufferSize) {
|
2011-03-18 20:35:07 +00:00
|
|
|
log_->message_buffer_[pos_++] = c;
|
2009-05-28 07:08:09 +00:00
|
|
|
}
|
|
|
|
ASSERT(pos_ <= Log::kMessageBufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 17:19:31 +00:00
|
|
|
void Log::MessageBuilder::AppendDoubleQuotedString(const char* string) {
|
2013-06-24 12:55:19 +00:00
|
|
|
Append('"');
|
|
|
|
for (const char* p = string; *p != '\0'; p++) {
|
|
|
|
if (*p == '"') {
|
|
|
|
Append('\\');
|
|
|
|
}
|
|
|
|
Append(*p);
|
|
|
|
}
|
|
|
|
Append('"');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 17:19:31 +00:00
|
|
|
void Log::MessageBuilder::Append(String* str) {
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc; // Ensure string stay valid.
|
2009-05-28 07:08:09 +00:00
|
|
|
int length = str->length();
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
Append(static_cast<char>(str->Get(i)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 17:19:31 +00:00
|
|
|
void Log::MessageBuilder::AppendAddress(Address addr) {
|
2010-12-07 13:24:22 +00:00
|
|
|
Append("0x%" V8PRIxPTR, addr);
|
2009-06-15 09:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 17:19:31 +00:00
|
|
|
void Log::MessageBuilder::AppendSymbolName(Symbol* symbol) {
|
|
|
|
ASSERT(symbol);
|
|
|
|
Append("symbol(");
|
|
|
|
if (!symbol->name()->IsUndefined()) {
|
|
|
|
Append("\"");
|
|
|
|
AppendDetailed(String::cast(symbol->name()), false);
|
|
|
|
Append("\" ");
|
|
|
|
}
|
|
|
|
Append("hash %x)", symbol->Hash());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Log::MessageBuilder::AppendDetailed(String* str, bool show_impl_info) {
|
2011-04-29 16:06:25 +00:00
|
|
|
if (str == NULL) return;
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_gc; // Ensure string stay valid.
|
2009-05-28 07:08:09 +00:00
|
|
|
int len = str->length();
|
|
|
|
if (len > 0x1000)
|
|
|
|
len = 0x1000;
|
|
|
|
if (show_impl_info) {
|
2012-11-21 10:01:05 +00:00
|
|
|
Append(str->IsOneByteRepresentation() ? 'a' : '2');
|
2009-05-28 07:08:09 +00:00
|
|
|
if (StringShape(str).IsExternal())
|
|
|
|
Append('e');
|
2013-02-28 17:03:34 +00:00
|
|
|
if (StringShape(str).IsInternalized())
|
2009-05-28 07:08:09 +00:00
|
|
|
Append('#');
|
|
|
|
Append(":%i:", str->length());
|
|
|
|
}
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
uc32 c = str->Get(i);
|
|
|
|
if (c > 0xff) {
|
|
|
|
Append("\\u%04x", c);
|
|
|
|
} else if (c < 32 || c > 126) {
|
|
|
|
Append("\\x%02x", c);
|
|
|
|
} else if (c == ',') {
|
|
|
|
Append("\\,");
|
|
|
|
} else if (c == '\\') {
|
|
|
|
Append("\\\\");
|
2011-02-22 16:31:24 +00:00
|
|
|
} else if (c == '\"') {
|
|
|
|
Append("\"\"");
|
2009-05-28 07:08:09 +00:00
|
|
|
} else {
|
|
|
|
Append("%lc", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 17:19:31 +00:00
|
|
|
void Log::MessageBuilder::AppendStringPart(const char* str, int len) {
|
2009-09-18 12:05:18 +00:00
|
|
|
if (pos_ + len > Log::kMessageBufferSize) {
|
|
|
|
len = Log::kMessageBufferSize - pos_;
|
|
|
|
ASSERT(len >= 0);
|
|
|
|
if (len == 0) return;
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
Vector<char> buf(log_->message_buffer_ + pos_,
|
2009-09-18 13:23:58 +00:00
|
|
|
Log::kMessageBufferSize - pos_);
|
|
|
|
OS::StrNCpy(buf, str, len);
|
2009-09-18 12:05:18 +00:00
|
|
|
pos_ += len;
|
|
|
|
ASSERT(pos_ <= Log::kMessageBufferSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-18 17:19:31 +00:00
|
|
|
void Log::MessageBuilder::WriteToLogFile() {
|
2009-05-28 07:08:09 +00:00
|
|
|
ASSERT(pos_ <= Log::kMessageBufferSize);
|
2011-07-13 11:31:22 +00:00
|
|
|
const int written = log_->WriteToFile(log_->message_buffer_, pos_);
|
2011-03-18 20:35:07 +00:00
|
|
|
if (written != pos_) {
|
|
|
|
log_->stop();
|
|
|
|
log_->logger_->LogFailure();
|
2009-05-28 13:56:32 +00:00
|
|
|
}
|
2009-05-28 07:08:09 +00:00
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2009-05-28 07:08:09 +00:00
|
|
|
} } // namespace v8::internal
|