V8 Project
log-utils.cc
Go to the documentation of this file.
1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/v8.h"
6 
7 #include "src/log-utils.h"
8 #include "src/string-stream.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 
14 const char* const Log::kLogToTemporaryFile = "&";
15 const char* const Log::kLogToConsole = "-";
16 
17 
18 Log::Log(Logger* logger)
19  : is_stopped_(false),
20  output_handle_(NULL),
21  message_buffer_(NULL),
22  logger_(logger) {
23 }
24 
25 
26 void Log::Initialize(const char* log_file_name) {
27  message_buffer_ = NewArray<char>(kMessageBufferSize);
28 
29  // --log-all enables all the log flags.
30  if (FLAG_log_all) {
31  FLAG_log_api = true;
32  FLAG_log_code = true;
33  FLAG_log_gc = true;
34  FLAG_log_suspect = true;
35  FLAG_log_handles = true;
36  FLAG_log_regexp = true;
37  FLAG_log_internal_timer_events = true;
38  }
39 
40  // --prof implies --log-code.
41  if (FLAG_prof) FLAG_log_code = true;
42 
43  // If we're logging anything, we need to open the log file.
44  if (Log::InitLogAtStart()) {
45  if (strcmp(log_file_name, kLogToConsole) == 0) {
46  OpenStdout();
47  } else if (strcmp(log_file_name, kLogToTemporaryFile) == 0) {
49  } else {
50  OpenFile(log_file_name);
51  }
52  }
53 }
54 
55 
57  DCHECK(!IsEnabled());
58  output_handle_ = stdout;
59 }
60 
61 
63  DCHECK(!IsEnabled());
65 }
66 
67 
68 void Log::OpenFile(const char* name) {
69  DCHECK(!IsEnabled());
71 }
72 
73 
74 FILE* Log::Close() {
75  FILE* result = NULL;
76  if (output_handle_ != NULL) {
77  if (strcmp(FLAG_logfile, kLogToTemporaryFile) != 0) {
78  fclose(output_handle_);
79  } else {
80  result = output_handle_;
81  }
82  }
84 
87 
88  is_stopped_ = false;
89  return result;
90 }
91 
92 
93 Log::MessageBuilder::MessageBuilder(Log* log)
94  : log_(log),
95  lock_guard_(&log_->mutex_),
96  pos_(0) {
97  DCHECK(log_->message_buffer_ != NULL);
98 }
99 
100 
101 void Log::MessageBuilder::Append(const char* format, ...) {
102  Vector<char> buf(log_->message_buffer_ + pos_,
103  Log::kMessageBufferSize - pos_);
104  va_list args;
105  va_start(args, format);
106  AppendVA(format, args);
107  va_end(args);
109 }
110 
111 
112 void Log::MessageBuilder::AppendVA(const char* format, va_list args) {
113  Vector<char> buf(log_->message_buffer_ + pos_,
114  Log::kMessageBufferSize - pos_);
115  int result = v8::internal::VSNPrintF(buf, format, args);
116 
117  // Result is -1 if output was truncated.
118  if (result >= 0) {
119  pos_ += result;
120  } else {
122  }
124 }
125 
126 
127 void Log::MessageBuilder::Append(const char c) {
128  if (pos_ < Log::kMessageBufferSize) {
129  log_->message_buffer_[pos_++] = c;
130  }
132 }
133 
134 
135 void Log::MessageBuilder::AppendDoubleQuotedString(const char* string) {
136  Append('"');
137  for (const char* p = string; *p != '\0'; p++) {
138  if (*p == '"') {
139  Append('\\');
140  }
141  Append(*p);
142  }
143  Append('"');
144 }
145 
146 
147 void Log::MessageBuilder::Append(String* str) {
148  DisallowHeapAllocation no_gc; // Ensure string stay valid.
149  int length = str->length();
150  for (int i = 0; i < length; i++) {
151  Append(static_cast<char>(str->Get(i)));
152  }
153 }
154 
155 
156 void Log::MessageBuilder::AppendAddress(Address addr) {
157  Append("0x%" V8PRIxPTR, addr);
158 }
159 
160 
161 void Log::MessageBuilder::AppendSymbolName(Symbol* symbol) {
162  DCHECK(symbol);
163  Append("symbol(");
164  if (!symbol->name()->IsUndefined()) {
165  Append("\"");
166  AppendDetailed(String::cast(symbol->name()), false);
167  Append("\" ");
168  }
169  Append("hash %x)", symbol->Hash());
170 }
171 
172 
173 void Log::MessageBuilder::AppendDetailed(String* str, bool show_impl_info) {
174  if (str == NULL) return;
175  DisallowHeapAllocation no_gc; // Ensure string stay valid.
176  int len = str->length();
177  if (len > 0x1000)
178  len = 0x1000;
179  if (show_impl_info) {
180  Append(str->IsOneByteRepresentation() ? 'a' : '2');
181  if (StringShape(str).IsExternal())
182  Append('e');
183  if (StringShape(str).IsInternalized())
184  Append('#');
185  Append(":%i:", str->length());
186  }
187  for (int i = 0; i < len; i++) {
188  uc32 c = str->Get(i);
189  if (c > 0xff) {
190  Append("\\u%04x", c);
191  } else if (c < 32 || c > 126) {
192  Append("\\x%02x", c);
193  } else if (c == ',') {
194  Append("\\,");
195  } else if (c == '\\') {
196  Append("\\\\");
197  } else if (c == '\"') {
198  Append("\"\"");
199  } else {
200  Append("%lc", c);
201  }
202  }
203 }
204 
205 
206 void Log::MessageBuilder::AppendStringPart(const char* str, int len) {
207  if (pos_ + len > Log::kMessageBufferSize) {
208  len = Log::kMessageBufferSize - pos_;
209  DCHECK(len >= 0);
210  if (len == 0) return;
211  }
212  Vector<char> buf(log_->message_buffer_ + pos_,
213  Log::kMessageBufferSize - pos_);
214  StrNCpy(buf, str, len);
215  pos_ += len;
217 }
218 
219 
220 void Log::MessageBuilder::WriteToLogFile() {
222  // Assert that we do not already have a new line at the end.
223  DCHECK(pos_ == 0 || log_->message_buffer_[pos_ - 1] != '\n');
224  if (pos_ == Log::kMessageBufferSize) pos_--;
225  log_->message_buffer_[pos_++] = '\n';
226  const int written = log_->WriteToFile(log_->message_buffer_, pos_);
227  if (written != pos_) {
228  log_->stop();
229  log_->logger_->LogFailure();
230  }
231 }
232 
233 
234 } } // namespace v8::internal
static const char *const LogFileOpenMode
Definition: platform.h:187
static FILE * OpenTemporaryFile()
static FILE * FOpen(const char *path, const char *mode)
bool IsEnabled()
Definition: log-utils.h:37
void OpenTemporaryFile()
Definition: log-utils.cc:62
void Initialize(const char *log_file_name)
Definition: log-utils.cc:26
FILE * output_handle_
Definition: log-utils.h:119
static const int kMessageBufferSize
Definition: log-utils.h:42
FILE * Close()
Definition: log-utils.cc:74
void OpenFile(const char *name)
Definition: log-utils.cc:68
static bool InitLogAtStart()
Definition: log-utils.h:24
static const char *const kLogToTemporaryFile
Definition: log-utils.h:46
Log(Logger *logger)
Definition: log-utils.cc:18
char * message_buffer_
Definition: log-utils.h:127
static const char *const kLogToConsole
Definition: log-utils.h:47
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in name
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define DCHECK(condition)
Definition: logging.h:205
#define V8PRIxPTR
Definition: macros.h:363
IN DWORD64 OUT PDWORD64 OUT PIMAGEHLP_SYMBOL64 Symbol
void DeleteArray(T *array)
Definition: allocation.h:68
void StrNCpy(Vector< char > dest, const char *src, size_t n)
Definition: utils.cc:119
PerThreadAssertScopeDebugOnly< HEAP_ALLOCATION_ASSERT, false > DisallowHeapAllocation
Definition: assert-scope.h:110
byte * Address
Definition: globals.h:101
int VSNPrintF(Vector< char > str, const char *format, va_list args)
Definition: utils.cc:114
int32_t uc32
Definition: globals.h:185
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20