blob: 262e7bb505264909b277ecc406d2f9cf30028ac4 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2//
3// Tests of logging functions from log.h
4
Steve Blocka7e24c12009-10-30 11:49:00 +00005#ifdef __linux__
6#include <math.h>
7#include <pthread.h>
8#include <signal.h>
9#include <unistd.h>
10#endif // __linux__
11
12#include "v8.h"
13#include "log.h"
Steve Block6ded16b2010-05-10 14:33:55 +010014#include "cpu-profiler.h"
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000015#include "natives.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000016#include "v8threads.h"
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017#include "v8utils.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000018#include "cctest.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010019#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000020
21using v8::internal::Address;
22using v8::internal::EmbeddedVector;
23using v8::internal::Logger;
Steve Blockd0582a62009-12-15 09:54:21 +000024using v8::internal::StrLength;
Steve Blocka7e24c12009-10-30 11:49:00 +000025
26namespace i = v8::internal;
27
Andrei Popescu402d9372010-02-26 13:31:12 +000028namespace {
29
30class ScopedLoggerInitializer {
31 public:
Steve Block6ded16b2010-05-10 14:33:55 +010032 explicit ScopedLoggerInitializer(bool prof_lazy)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000033 : saved_log_(i::FLAG_log),
34 saved_prof_lazy_(i::FLAG_prof_lazy),
Andrei Popescu402d9372010-02-26 13:31:12 +000035 saved_prof_(i::FLAG_prof),
36 saved_prof_auto_(i::FLAG_prof_auto),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000037 temp_file_(NULL),
38 // Need to run this prior to creating the scope.
Steve Block6ded16b2010-05-10 14:33:55 +010039 trick_to_run_init_flags_(init_flags_(prof_lazy)),
Andrei Popescu402d9372010-02-26 13:31:12 +000040 scope_(),
41 env_(v8::Context::New()) {
Andrei Popescu402d9372010-02-26 13:31:12 +000042 env_->Enter();
43 }
44
45 ~ScopedLoggerInitializer() {
46 env_->Exit();
Steve Block44f0eee2011-05-26 01:26:41 +010047 LOGGER->TearDown();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000048 if (temp_file_ != NULL) fclose(temp_file_);
Andrei Popescu402d9372010-02-26 13:31:12 +000049 i::FLAG_prof_lazy = saved_prof_lazy_;
50 i::FLAG_prof = saved_prof_;
51 i::FLAG_prof_auto = saved_prof_auto_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000052 i::FLAG_log = saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +000053 }
54
55 v8::Handle<v8::Context>& env() { return env_; }
56
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000057 FILE* StopLoggingGetTempFile() {
58 temp_file_ = LOGGER->TearDown();
59 CHECK_NE(NULL, temp_file_);
60 fflush(temp_file_);
61 rewind(temp_file_);
62 return temp_file_;
63 }
64
Andrei Popescu402d9372010-02-26 13:31:12 +000065 private:
Steve Block6ded16b2010-05-10 14:33:55 +010066 static bool init_flags_(bool prof_lazy) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000067 i::FLAG_log = true;
Andrei Popescu402d9372010-02-26 13:31:12 +000068 i::FLAG_prof = true;
69 i::FLAG_prof_lazy = prof_lazy;
70 i::FLAG_prof_auto = false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000071 i::FLAG_logfile = i::Log::kLogToTemporaryFile;
Andrei Popescu402d9372010-02-26 13:31:12 +000072 return prof_lazy;
73 }
74
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000075 const bool saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +000076 const bool saved_prof_lazy_;
77 const bool saved_prof_;
78 const bool saved_prof_auto_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000079 FILE* temp_file_;
Andrei Popescu402d9372010-02-26 13:31:12 +000080 const bool trick_to_run_init_flags_;
Andrei Popescu402d9372010-02-26 13:31:12 +000081 v8::HandleScope scope_;
82 v8::Handle<v8::Context> env_;
83
84 DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer);
85};
86
Andrei Popescu402d9372010-02-26 13:31:12 +000087} // namespace
88
89
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000090static const char* StrNStr(const char* s1, const char* s2, int n) {
91 if (s1[n] == '\0') return strstr(s1, s2);
92 i::ScopedVector<char> str(n + 1);
93 i::OS::StrNCpy(str, s1, static_cast<size_t>(n));
94 str[n] = '\0';
95 char* found = strstr(str.start(), s2);
96 return found != NULL ? s1 + (found - str.start()) : NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000097}
98
99
100TEST(ProfLazyMode) {
Steve Block6ded16b2010-05-10 14:33:55 +0100101 ScopedLoggerInitializer initialize_logger(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000102
Ben Murdochb0fe1622011-05-05 13:52:32 +0100103 if (!i::V8::UseCrankshaft()) return;
104
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000105 LOGGER->StringEvent("test-start", "");
106 CompileRun("var a = (function(x) { return x + 1; })(10);");
107 LOGGER->StringEvent("test-profiler-start", "");
108 v8::V8::ResumeProfiler();
109 CompileRun(
110 "var b = (function(x) { return x + 2; })(10);\n"
111 "var c = (function(x) { return x + 3; })(10);\n"
112 "var d = (function(x) { return x + 4; })(10);\n"
113 "var e = (function(x) { return x + 5; })(10);");
114 v8::V8::PauseProfiler();
115 LOGGER->StringEvent("test-profiler-stop", "");
116 CompileRun("var f = (function(x) { return x + 6; })(10);");
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 // Check that profiling can be resumed again.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000118 LOGGER->StringEvent("test-profiler-start-2", "");
119 v8::V8::ResumeProfiler();
120 CompileRun(
121 "var g = (function(x) { return x + 7; })(10);\n"
122 "var h = (function(x) { return x + 8; })(10);\n"
123 "var i = (function(x) { return x + 9; })(10);\n"
124 "var j = (function(x) { return x + 10; })(10);");
125 v8::V8::PauseProfiler();
126 LOGGER->StringEvent("test-profiler-stop-2", "");
127 LOGGER->StringEvent("test-stop", "");
128
129 bool exists = false;
130 i::Vector<const char> log(
131 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
132 CHECK(exists);
133
134 const char* test_start_position =
135 StrNStr(log.start(), "test-start,", log.length());
136 CHECK_NE(NULL, test_start_position);
137 const char* test_profiler_start_position =
138 StrNStr(log.start(), "test-profiler-start,", log.length());
139 CHECK_NE(NULL, test_profiler_start_position);
140 CHECK_GT(test_profiler_start_position, test_start_position);
141 const char* test_profiler_stop_position =
142 StrNStr(log.start(), "test-profiler-stop,", log.length());
143 CHECK_NE(NULL, test_profiler_stop_position);
144 CHECK_GT(test_profiler_stop_position, test_profiler_start_position);
145 const char* test_profiler_start_2_position =
146 StrNStr(log.start(), "test-profiler-start-2,", log.length());
147 CHECK_NE(NULL, test_profiler_start_2_position);
148 CHECK_GT(test_profiler_start_2_position, test_profiler_stop_position);
149
150 // Nothing must be logged until profiling is resumed.
151 CHECK_EQ(NULL, StrNStr(test_start_position,
152 "code-creation,",
153 static_cast<int>(test_profiler_start_position -
154 test_start_position)));
155 // Nothing must be logged while profiling is suspended.
156 CHECK_EQ(NULL, StrNStr(test_profiler_stop_position,
157 "code-creation,",
158 static_cast<int>(test_profiler_start_2_position -
159 test_profiler_stop_position)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000160}
161
162
John Reck59135872010-11-02 12:39:01 -0700163// BUG(913). Need to implement support for profiling multiple VM threads.
164#if 0
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
166namespace {
167
168class LoopingThread : public v8::internal::Thread {
169 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100170 explicit LoopingThread(v8::internal::Isolate* isolate)
171 : v8::internal::Thread(isolate),
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 semaphore_(v8::internal::OS::CreateSemaphore(0)),
173 run_(true) {
174 }
175
176 virtual ~LoopingThread() { delete semaphore_; }
177
178 void Run() {
179 self_ = pthread_self();
180 RunLoop();
181 }
182
183 void SendSigProf() { pthread_kill(self_, SIGPROF); }
184
185 void Stop() { run_ = false; }
186
187 bool WaitForRunning() { return semaphore_->Wait(1000000); }
188
189 protected:
190 bool IsRunning() { return run_; }
191
192 virtual void RunLoop() = 0;
193
194 void SetV8ThreadId() {
195 v8_thread_id_ = v8::V8::GetCurrentThreadId();
196 }
197
198 void SignalRunning() { semaphore_->Signal(); }
199
200 private:
201 v8::internal::Semaphore* semaphore_;
202 bool run_;
203 pthread_t self_;
204 int v8_thread_id_;
205};
206
207
208class LoopingJsThread : public LoopingThread {
209 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100210 explicit LoopingJsThread(v8::internal::Isolate* isolate)
211 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000212 void RunLoop() {
Steve Block6ded16b2010-05-10 14:33:55 +0100213 v8::Locker locker;
Steve Block44f0eee2011-05-26 01:26:41 +0100214 CHECK(i::Isolate::Current() != NULL);
215 CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100216 SetV8ThreadId();
Steve Blocka7e24c12009-10-30 11:49:00 +0000217 while (IsRunning()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000218 v8::HandleScope scope;
219 v8::Persistent<v8::Context> context = v8::Context::New();
Steve Block6ded16b2010-05-10 14:33:55 +0100220 CHECK(!context.IsEmpty());
221 {
222 v8::Context::Scope context_scope(context);
223 SignalRunning();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000224 CompileRun(
Steve Block6ded16b2010-05-10 14:33:55 +0100225 "var j; for (var i=0; i<10000; ++i) { j = Math.sin(i); }");
226 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 context.Dispose();
228 i::OS::Sleep(1);
229 }
230 }
231};
232
233
234class LoopingNonJsThread : public LoopingThread {
235 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100236 explicit LoopingNonJsThread(v8::internal::Isolate* isolate)
237 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 void RunLoop() {
239 v8::Locker locker;
240 v8::Unlocker unlocker;
241 // Now thread has V8's id, but will not run VM code.
Steve Block44f0eee2011-05-26 01:26:41 +0100242 CHECK(i::Isolate::Current() != NULL);
243 CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000244 double i = 10;
245 SignalRunning();
246 while (IsRunning()) {
247 i = sin(i);
248 i::OS::Sleep(1);
249 }
250 }
251};
252
253
254class TestSampler : public v8::internal::Sampler {
255 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100256 explicit TestSampler(v8::internal::Isolate* isolate)
257 : Sampler(isolate, 0, true, true),
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 semaphore_(v8::internal::OS::CreateSemaphore(0)),
259 was_sample_stack_called_(false) {
260 }
261
262 ~TestSampler() { delete semaphore_; }
263
264 void SampleStack(v8::internal::TickSample*) {
265 was_sample_stack_called_ = true;
266 }
267
268 void Tick(v8::internal::TickSample*) { semaphore_->Signal(); }
269
270 bool WaitForTick() { return semaphore_->Wait(1000000); }
271
272 void Reset() { was_sample_stack_called_ = false; }
273
274 bool WasSampleStackCalled() { return was_sample_stack_called_; }
275
276 private:
277 v8::internal::Semaphore* semaphore_;
278 bool was_sample_stack_called_;
279};
280
281
282} // namespace
283
284TEST(ProfMultipleThreads) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100285 TestSampler* sampler = NULL;
286 {
287 v8::Locker locker;
Steve Block44f0eee2011-05-26 01:26:41 +0100288 sampler = new TestSampler(v8::internal::Isolate::Current());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100289 sampler->Start();
290 CHECK(sampler->IsActive());
291 }
292
Steve Block44f0eee2011-05-26 01:26:41 +0100293 LoopingJsThread jsThread(v8::internal::Isolate::Current());
Steve Blocka7e24c12009-10-30 11:49:00 +0000294 jsThread.Start();
Steve Block44f0eee2011-05-26 01:26:41 +0100295 LoopingNonJsThread nonJsThread(v8::internal::Isolate::Current());
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 nonJsThread.Start();
297
Ben Murdochb0fe1622011-05-05 13:52:32 +0100298 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 jsThread.WaitForRunning();
300 jsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100301 CHECK(sampler->WaitForTick());
302 CHECK(sampler->WasSampleStackCalled());
303 sampler->Reset();
304 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 nonJsThread.WaitForRunning();
306 nonJsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100307 CHECK(!sampler->WaitForTick());
308 CHECK(!sampler->WasSampleStackCalled());
309 sampler->Stop();
Steve Blocka7e24c12009-10-30 11:49:00 +0000310
311 jsThread.Stop();
312 nonJsThread.Stop();
313 jsThread.Join();
314 nonJsThread.Join();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100315
316 delete sampler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000317}
318
319#endif // __linux__
320
321
Steve Block3ce2e202009-11-05 08:53:23 +0000322// Test for issue http://crbug.com/23768 in Chromium.
323// Heap can contain scripts with already disposed external sources.
324// We need to verify that LogCompiledFunctions doesn't crash on them.
325namespace {
326
327class SimpleExternalString : public v8::String::ExternalStringResource {
328 public:
329 explicit SimpleExternalString(const char* source)
Steve Blockd0582a62009-12-15 09:54:21 +0000330 : utf_source_(StrLength(source)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000331 for (int i = 0; i < utf_source_.length(); ++i)
332 utf_source_[i] = source[i];
333 }
334 virtual ~SimpleExternalString() {}
335 virtual size_t length() const { return utf_source_.length(); }
336 virtual const uint16_t* data() const { return utf_source_.start(); }
337 private:
338 i::ScopedVector<uint16_t> utf_source_;
339};
340
341} // namespace
342
343TEST(Issue23768) {
344 v8::HandleScope scope;
345 v8::Handle<v8::Context> env = v8::Context::New();
346 env->Enter();
347
348 SimpleExternalString source_ext_str("(function ext() {})();");
349 v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str);
350 // Script needs to have a name in order to trigger InitLineEnds execution.
351 v8::Handle<v8::String> origin = v8::String::New("issue-23768-test");
352 v8::Handle<v8::Script> evil_script = v8::Script::Compile(source, origin);
353 CHECK(!evil_script.IsEmpty());
354 CHECK(!evil_script->Run().IsEmpty());
355 i::Handle<i::ExternalTwoByteString> i_source(
356 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
357 // This situation can happen if source was an external string disposed
358 // by its owner.
359 i_source->set_resource(NULL);
360
361 // Must not crash.
Steve Block44f0eee2011-05-26 01:26:41 +0100362 LOGGER->LogCompiledFunctions();
Steve Block3ce2e202009-11-05 08:53:23 +0000363}
364
365
Steve Blockd0582a62009-12-15 09:54:21 +0000366static v8::Handle<v8::Value> ObjMethod1(const v8::Arguments& args) {
367 return v8::Handle<v8::Value>();
368}
369
370TEST(LogCallbacks) {
Steve Block6ded16b2010-05-10 14:33:55 +0100371 ScopedLoggerInitializer initialize_logger(false);
Steve Blockd0582a62009-12-15 09:54:21 +0000372
373 v8::Persistent<v8::FunctionTemplate> obj =
374 v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000375 obj->SetClassName(v8_str("Obj"));
Steve Blockd0582a62009-12-15 09:54:21 +0000376 v8::Handle<v8::ObjectTemplate> proto = obj->PrototypeTemplate();
377 v8::Local<v8::Signature> signature = v8::Signature::New(obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000378 proto->Set(v8_str("method1"),
Steve Blockd0582a62009-12-15 09:54:21 +0000379 v8::FunctionTemplate::New(ObjMethod1,
380 v8::Handle<v8::Value>(),
381 signature),
382 static_cast<v8::PropertyAttribute>(v8::DontDelete));
383
Andrei Popescu402d9372010-02-26 13:31:12 +0000384 initialize_logger.env()->Global()->Set(v8_str("Obj"), obj->GetFunction());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000385 CompileRun("Obj.prototype.method1.toString();");
Steve Blockd0582a62009-12-15 09:54:21 +0000386
Steve Block44f0eee2011-05-26 01:26:41 +0100387 LOGGER->LogCompiledFunctions();
Steve Blockd0582a62009-12-15 09:54:21 +0000388
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000389 bool exists = false;
390 i::Vector<const char> log(
391 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
392 CHECK(exists);
393
394 i::EmbeddedVector<char, 100> ref_data;
Steve Blockd0582a62009-12-15 09:54:21 +0000395 i::OS::SNPrintF(ref_data,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000396 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"method1\"\0",
397 ObjMethod1);
398
399 CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length()));
Steve Blockd0582a62009-12-15 09:54:21 +0000400
401 obj.Dispose();
Steve Blockd0582a62009-12-15 09:54:21 +0000402}
403
404
405static v8::Handle<v8::Value> Prop1Getter(v8::Local<v8::String> property,
406 const v8::AccessorInfo& info) {
407 return v8::Handle<v8::Value>();
408}
409
410static void Prop1Setter(v8::Local<v8::String> property,
411 v8::Local<v8::Value> value,
412 const v8::AccessorInfo& info) {
413}
414
415static v8::Handle<v8::Value> Prop2Getter(v8::Local<v8::String> property,
416 const v8::AccessorInfo& info) {
417 return v8::Handle<v8::Value>();
418}
419
420TEST(LogAccessorCallbacks) {
Steve Block6ded16b2010-05-10 14:33:55 +0100421 ScopedLoggerInitializer initialize_logger(false);
Steve Blockd0582a62009-12-15 09:54:21 +0000422
423 v8::Persistent<v8::FunctionTemplate> obj =
424 v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000425 obj->SetClassName(v8_str("Obj"));
Steve Blockd0582a62009-12-15 09:54:21 +0000426 v8::Handle<v8::ObjectTemplate> inst = obj->InstanceTemplate();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000427 inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter);
428 inst->SetAccessor(v8_str("prop2"), Prop2Getter);
Steve Blockd0582a62009-12-15 09:54:21 +0000429
Steve Block44f0eee2011-05-26 01:26:41 +0100430 LOGGER->LogAccessorCallbacks();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000431
432 bool exists = false;
433 i::Vector<const char> log(
434 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
435 CHECK(exists);
Steve Blockd0582a62009-12-15 09:54:21 +0000436
437 EmbeddedVector<char, 100> prop1_getter_record;
438 i::OS::SNPrintF(prop1_getter_record,
439 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"get prop1\"",
440 Prop1Getter);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000441 CHECK_NE(NULL,
442 StrNStr(log.start(), prop1_getter_record.start(), log.length()));
443
Steve Blockd0582a62009-12-15 09:54:21 +0000444 EmbeddedVector<char, 100> prop1_setter_record;
445 i::OS::SNPrintF(prop1_setter_record,
446 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"set prop1\"",
447 Prop1Setter);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000448 CHECK_NE(NULL,
449 StrNStr(log.start(), prop1_setter_record.start(), log.length()));
450
Steve Blockd0582a62009-12-15 09:54:21 +0000451 EmbeddedVector<char, 100> prop2_getter_record;
452 i::OS::SNPrintF(prop2_getter_record,
453 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"get prop2\"",
454 Prop2Getter);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000455 CHECK_NE(NULL,
456 StrNStr(log.start(), prop2_getter_record.start(), log.length()));
Steve Blockd0582a62009-12-15 09:54:21 +0000457
458 obj.Dispose();
Andrei Popescu402d9372010-02-26 13:31:12 +0000459}
Steve Blockd0582a62009-12-15 09:54:21 +0000460
Andrei Popescu402d9372010-02-26 13:31:12 +0000461
Steve Block6ded16b2010-05-10 14:33:55 +0100462TEST(IsLoggingPreserved) {
463 ScopedLoggerInitializer initialize_logger(false);
464
Steve Block44f0eee2011-05-26 01:26:41 +0100465 CHECK(LOGGER->is_logging());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000466 LOGGER->ResumeProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +0100467 CHECK(LOGGER->is_logging());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000468 LOGGER->PauseProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +0100469 CHECK(LOGGER->is_logging());
Steve Block6ded16b2010-05-10 14:33:55 +0100470}
471
472
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000473typedef i::NativesCollection<i::TEST> TestSources;
Steve Blocka7e24c12009-10-30 11:49:00 +0000474
475// Test that logging of code create / move / delete events
476// is equivalent to traversal of a resulting heap.
477TEST(EquivalenceOfLoggingAndTraversal) {
478 // This test needs to be run on a "clean" V8 to ensure that snapshot log
479 // is loaded. This is always true when running using tools/test.py because
480 // it launches a new cctest instance for every test. To be sure that launching
481 // cctest manually also works, please be sure that no tests below
482 // are using V8.
483 //
484 // P.S. No, V8 can't be re-initialized after disposal, see include/v8.h.
485 CHECK(!i::V8::IsRunning());
486
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000487 // Start with profiling to capture all code events from the beginning.
488 ScopedLoggerInitializer initialize_logger(false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000489
490 // Compile and run a function that creates other functions.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000491 CompileRun(
Steve Blocka7e24c12009-10-30 11:49:00 +0000492 "(function f(obj) {\n"
493 " obj.test =\n"
494 " (function a(j) { return function b() { return j; } })(100);\n"
495 "})(this);");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000496 v8::V8::PauseProfiler();
497 HEAP->CollectAllGarbage(true);
498 LOGGER->StringEvent("test-logging-done", "");
Steve Blocka7e24c12009-10-30 11:49:00 +0000499
500 // Iterate heap to find compiled functions, will write to log.
Steve Block44f0eee2011-05-26 01:26:41 +0100501 LOGGER->LogCompiledFunctions();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000502 LOGGER->StringEvent("test-traversal-done", "");
Steve Blocka7e24c12009-10-30 11:49:00 +0000503
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000504 bool exists = false;
505 i::Vector<const char> log(
506 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
507 CHECK(exists);
508 v8::Handle<v8::String> log_str = v8::String::New(log.start(), log.length());
509 initialize_logger.env()->Global()->Set(v8_str("_log"), log_str);
Steve Blocka7e24c12009-10-30 11:49:00 +0000510
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000511 i::Vector<const unsigned char> source = TestSources::GetScriptsSource();
512 v8::Handle<v8::String> source_str = v8::String::New(
513 reinterpret_cast<const char*>(source.start()), source.length());
514 v8::TryCatch try_catch;
515 v8::Handle<v8::Script> script = v8::Script::Compile(source_str, v8_str(""));
516 if (script.IsEmpty()) {
517 v8::String::Utf8Value exception(try_catch.Exception());
518 printf("compile: %s\n", *exception);
519 CHECK(false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000520 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000521 v8::Handle<v8::Value> result = script->Run();
522 if (result.IsEmpty()) {
523 v8::String::Utf8Value exception(try_catch.Exception());
524 printf("run: %s\n", *exception);
525 CHECK(false);
526 }
527 // The result either be a "true" literal or problem description.
528 if (!result->IsTrue()) {
529 v8::Local<v8::String> s = result->ToString();
530 i::ScopedVector<char> data(s->Length() + 1);
531 CHECK_NE(NULL, data.start());
532 s->WriteAscii(data.start());
533 printf("%s\n", data.start());
534 // Make sure that our output is written prior crash due to CHECK failure.
535 fflush(stdout);
536 CHECK(false);
537 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000538}