blob: 7ae96dbde48f738645a2c508dd181a939051c2d8 [file] [log] [blame]
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00001// 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.orgea88ce92011-03-23 11:19:56 +000031#include "string-stream.h"
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000032
33namespace v8 {
34namespace internal {
35
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000036
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000037const char* const Log::kLogToTemporaryFile = "&";
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000038
39
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000040Log::Log(Logger* logger)
whesse@chromium.org030d38e2011-07-13 13:23:34 +000041 : is_stopped_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000042 output_handle_(NULL),
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +000043 ll_output_handle_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000044 mutex_(NULL),
45 message_buffer_(NULL),
46 logger_(logger) {
47}
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000048
49
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000050static void AddIsolateIdIfNeeded(StringStream* stream) {
51 Isolate* isolate = Isolate::Current();
52 if (isolate->IsDefaultIsolate()) return;
53 stream->Add("isolate-%p-", isolate);
54}
55
56
57void Log::Initialize() {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000058 mutex_ = OS::CreateMutex();
59 message_buffer_ = NewArray<char>(kMessageBufferSize);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000060
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;
70 }
71
72 // --prof implies --log-code.
73 if (FLAG_prof) FLAG_log_code = true;
74
75 // --prof_lazy controls --log-code, implies --noprof_auto.
76 if (FLAG_prof_lazy) {
77 FLAG_log_code = false;
78 FLAG_prof_auto = false;
79 }
80
whesse@chromium.org030d38e2011-07-13 13:23:34 +000081 bool open_log_file = FLAG_log || FLAG_log_runtime || FLAG_log_api
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000082 || FLAG_log_code || FLAG_log_gc || FLAG_log_handles || FLAG_log_suspect
ulan@chromium.org8e8d8822012-11-23 14:36:46 +000083 || FLAG_log_regexp || FLAG_log_state_changes || FLAG_ll_prof
84 || FLAG_log_timer_events;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000085
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000086 // If we're logging anything, we need to open the log file.
87 if (open_log_file) {
88 if (strcmp(FLAG_logfile, "-") == 0) {
89 OpenStdout();
whesse@chromium.org030d38e2011-07-13 13:23:34 +000090 } else if (strcmp(FLAG_logfile, kLogToTemporaryFile) == 0) {
91 OpenTemporaryFile();
92 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000093 if (strchr(FLAG_logfile, '%') != NULL ||
94 !Isolate::Current()->IsDefaultIsolate()) {
95 // If there's a '%' in the log file name we have to expand
96 // placeholders.
97 HeapStringAllocator allocator;
98 StringStream stream(&allocator);
99 AddIsolateIdIfNeeded(&stream);
100 for (const char* p = FLAG_logfile; *p; p++) {
101 if (*p == '%') {
102 p++;
103 switch (*p) {
104 case '\0':
105 // If there's a % at the end of the string we back up
106 // one character so we can escape the loop properly.
107 p--;
108 break;
109 case 't': {
110 // %t expands to the current time in milliseconds.
111 double time = OS::TimeCurrentMillis();
112 stream.Add("%.0f", FmtElm(time));
113 break;
114 }
115 case '%':
116 // %% expands (contracts really) to %.
117 stream.Put('%');
118 break;
119 default:
120 // All other %'s expand to themselves.
121 stream.Put('%');
122 stream.Put(*p);
123 break;
124 }
125 } else {
126 stream.Put(*p);
127 }
128 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000129 SmartArrayPointer<const char> expanded = stream.ToCString();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000130 OpenFile(*expanded);
131 } else {
132 OpenFile(FLAG_logfile);
133 }
134 }
135 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000136}
137
138
139void Log::OpenStdout() {
140 ASSERT(!IsEnabled());
141 output_handle_ = stdout;
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000142}
143
144
145void Log::OpenTemporaryFile() {
146 ASSERT(!IsEnabled());
147 output_handle_ = i::OS::OpenTemporaryFile();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000148}
149
150
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000151// Extension added to V8 log file name to get the low-level log name.
152static const char kLowLevelLogExt[] = ".ll";
153
154// File buffer size of the low-level log. We don't use the default to
155// minimize the associated overhead.
156static const int kLowLevelLogBufferSize = 2 * MB;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000157
158
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000159void Log::OpenFile(const char* name) {
160 ASSERT(!IsEnabled());
161 output_handle_ = OS::FOpen(name, OS::LogFileOpenMode);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000162 if (FLAG_ll_prof) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000163 // Open the low-level log file.
164 size_t len = strlen(name);
165 ScopedVector<char> ll_name(static_cast<int>(len + sizeof(kLowLevelLogExt)));
166 memcpy(ll_name.start(), name, len);
167 memcpy(ll_name.start() + len, kLowLevelLogExt, sizeof(kLowLevelLogExt));
168 ll_output_handle_ = OS::FOpen(ll_name.start(), OS::LogFileOpenMode);
169 setvbuf(ll_output_handle_, NULL, _IOFBF, kLowLevelLogBufferSize);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000170 }
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000171}
172
173
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000174FILE* 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.org755c5b12009-05-29 11:04:38 +0000182 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000183 output_handle_ = NULL;
184 if (ll_output_handle_ != NULL) fclose(ll_output_handle_);
185 ll_output_handle_ = NULL;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000186
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000187 DeleteArray(message_buffer_);
188 message_buffer_ = NULL;
189
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000190 delete mutex_;
191 mutex_ = NULL;
192
193 is_stopped_ = false;
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000194 return result;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000195}
196
197
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000198LogMessageBuilder::LogMessageBuilder(Logger* logger)
199 : log_(logger->log_),
200 sl(log_->mutex_),
201 pos_(0) {
202 ASSERT(log_->message_buffer_ != NULL);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000203}
204
205
206void LogMessageBuilder::Append(const char* format, ...) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000207 Vector<char> buf(log_->message_buffer_ + pos_,
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000208 Log::kMessageBufferSize - pos_);
209 va_list args;
210 va_start(args, format);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000211 AppendVA(format, args);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000212 va_end(args);
213 ASSERT(pos_ <= Log::kMessageBufferSize);
214}
215
216
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000217void LogMessageBuilder::AppendVA(const char* format, va_list args) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000218 Vector<char> buf(log_->message_buffer_ + pos_,
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000219 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
232void LogMessageBuilder::Append(const char c) {
233 if (pos_ < Log::kMessageBufferSize) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000234 log_->message_buffer_[pos_++] = c;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000235 }
236 ASSERT(pos_ <= Log::kMessageBufferSize);
237}
238
239
240void LogMessageBuilder::Append(String* str) {
241 AssertNoAllocation no_heap_allocation; // Ensure string stay valid.
242 int length = str->length();
243 for (int i = 0; i < length; i++) {
244 Append(static_cast<char>(str->Get(i)));
245 }
246}
247
248
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000249void LogMessageBuilder::AppendAddress(Address addr) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000250 Append("0x%" V8PRIxPTR, addr);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000251}
252
253
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000254void LogMessageBuilder::AppendDetailed(String* str, bool show_impl_info) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000255 if (str == NULL) return;
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000256 AssertNoAllocation no_heap_allocation; // Ensure string stay valid.
257 int len = str->length();
258 if (len > 0x1000)
259 len = 0x1000;
260 if (show_impl_info) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000261 Append(str->IsOneByteRepresentation() ? 'a' : '2');
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000262 if (StringShape(str).IsExternal())
263 Append('e');
264 if (StringShape(str).IsSymbol())
265 Append('#');
266 Append(":%i:", str->length());
267 }
268 for (int i = 0; i < len; i++) {
269 uc32 c = str->Get(i);
270 if (c > 0xff) {
271 Append("\\u%04x", c);
272 } else if (c < 32 || c > 126) {
273 Append("\\x%02x", c);
274 } else if (c == ',') {
275 Append("\\,");
276 } else if (c == '\\') {
277 Append("\\\\");
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000278 } else if (c == '\"') {
279 Append("\"\"");
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000280 } else {
281 Append("%lc", c);
282 }
283 }
284}
285
286
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000287void LogMessageBuilder::AppendStringPart(const char* str, int len) {
288 if (pos_ + len > Log::kMessageBufferSize) {
289 len = Log::kMessageBufferSize - pos_;
290 ASSERT(len >= 0);
291 if (len == 0) return;
292 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000293 Vector<char> buf(log_->message_buffer_ + pos_,
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000294 Log::kMessageBufferSize - pos_);
295 OS::StrNCpy(buf, str, len);
296 pos_ += len;
297 ASSERT(pos_ <= Log::kMessageBufferSize);
298}
299
300
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000301void LogMessageBuilder::WriteToLogFile() {
302 ASSERT(pos_ <= Log::kMessageBufferSize);
whesse@chromium.org030d38e2011-07-13 13:23:34 +0000303 const int written = log_->WriteToFile(log_->message_buffer_, pos_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000304 if (written != pos_) {
305 log_->stop();
306 log_->logger_->LogFailure();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000307 }
308}
309
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000310
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000311} } // namespace v8::internal