blob: dfbc733e0df0623c40f3904fc04c50e596e7fe2d [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
Andrei Popescu402d9372010-02-26 13:31:12 +000026namespace {
27
28class ScopedLoggerInitializer {
29 public:
Steve Block6ded16b2010-05-10 14:33:55 +010030 explicit ScopedLoggerInitializer(bool prof_lazy)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000031 : saved_log_(i::FLAG_log),
32 saved_prof_lazy_(i::FLAG_prof_lazy),
Andrei Popescu402d9372010-02-26 13:31:12 +000033 saved_prof_(i::FLAG_prof),
34 saved_prof_auto_(i::FLAG_prof_auto),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000035 temp_file_(NULL),
36 // Need to run this prior to creating the scope.
Steve Block6ded16b2010-05-10 14:33:55 +010037 trick_to_run_init_flags_(init_flags_(prof_lazy)),
Andrei Popescu402d9372010-02-26 13:31:12 +000038 scope_(),
39 env_(v8::Context::New()) {
Andrei Popescu402d9372010-02-26 13:31:12 +000040 env_->Enter();
41 }
42
43 ~ScopedLoggerInitializer() {
44 env_->Exit();
Steve Block44f0eee2011-05-26 01:26:41 +010045 LOGGER->TearDown();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000046 if (temp_file_ != NULL) fclose(temp_file_);
Andrei Popescu402d9372010-02-26 13:31:12 +000047 i::FLAG_prof_lazy = saved_prof_lazy_;
48 i::FLAG_prof = saved_prof_;
49 i::FLAG_prof_auto = saved_prof_auto_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000050 i::FLAG_log = saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +000051 }
52
53 v8::Handle<v8::Context>& env() { return env_; }
54
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000055 FILE* StopLoggingGetTempFile() {
56 temp_file_ = LOGGER->TearDown();
57 CHECK_NE(NULL, temp_file_);
58 fflush(temp_file_);
59 rewind(temp_file_);
60 return temp_file_;
61 }
62
Andrei Popescu402d9372010-02-26 13:31:12 +000063 private:
Steve Block6ded16b2010-05-10 14:33:55 +010064 static bool init_flags_(bool prof_lazy) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000065 i::FLAG_log = true;
Andrei Popescu402d9372010-02-26 13:31:12 +000066 i::FLAG_prof = true;
67 i::FLAG_prof_lazy = prof_lazy;
68 i::FLAG_prof_auto = false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000069 i::FLAG_logfile = i::Log::kLogToTemporaryFile;
Andrei Popescu402d9372010-02-26 13:31:12 +000070 return prof_lazy;
71 }
72
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000073 const bool saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +000074 const bool saved_prof_lazy_;
75 const bool saved_prof_;
76 const bool saved_prof_auto_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000077 FILE* temp_file_;
Andrei Popescu402d9372010-02-26 13:31:12 +000078 const bool trick_to_run_init_flags_;
Andrei Popescu402d9372010-02-26 13:31:12 +000079 v8::HandleScope scope_;
80 v8::Handle<v8::Context> env_;
81
82 DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer);
83};
84
Andrei Popescu402d9372010-02-26 13:31:12 +000085} // namespace
86
87
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000088static const char* StrNStr(const char* s1, const char* s2, int n) {
89 if (s1[n] == '\0') return strstr(s1, s2);
90 i::ScopedVector<char> str(n + 1);
91 i::OS::StrNCpy(str, s1, static_cast<size_t>(n));
92 str[n] = '\0';
93 char* found = strstr(str.start(), s2);
94 return found != NULL ? s1 + (found - str.start()) : NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000095}
96
97
98TEST(ProfLazyMode) {
Steve Block6ded16b2010-05-10 14:33:55 +010099 ScopedLoggerInitializer initialize_logger(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000100
Ben Murdochb0fe1622011-05-05 13:52:32 +0100101 if (!i::V8::UseCrankshaft()) return;
102
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000103 LOGGER->StringEvent("test-start", "");
104 CompileRun("var a = (function(x) { return x + 1; })(10);");
105 LOGGER->StringEvent("test-profiler-start", "");
106 v8::V8::ResumeProfiler();
107 CompileRun(
108 "var b = (function(x) { return x + 2; })(10);\n"
109 "var c = (function(x) { return x + 3; })(10);\n"
110 "var d = (function(x) { return x + 4; })(10);\n"
111 "var e = (function(x) { return x + 5; })(10);");
112 v8::V8::PauseProfiler();
113 LOGGER->StringEvent("test-profiler-stop", "");
114 CompileRun("var f = (function(x) { return x + 6; })(10);");
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 // Check that profiling can be resumed again.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000116 LOGGER->StringEvent("test-profiler-start-2", "");
117 v8::V8::ResumeProfiler();
118 CompileRun(
119 "var g = (function(x) { return x + 7; })(10);\n"
120 "var h = (function(x) { return x + 8; })(10);\n"
121 "var i = (function(x) { return x + 9; })(10);\n"
122 "var j = (function(x) { return x + 10; })(10);");
123 v8::V8::PauseProfiler();
124 LOGGER->StringEvent("test-profiler-stop-2", "");
125 LOGGER->StringEvent("test-stop", "");
126
127 bool exists = false;
128 i::Vector<const char> log(
129 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
130 CHECK(exists);
131
132 const char* test_start_position =
133 StrNStr(log.start(), "test-start,", log.length());
134 CHECK_NE(NULL, test_start_position);
135 const char* test_profiler_start_position =
136 StrNStr(log.start(), "test-profiler-start,", log.length());
137 CHECK_NE(NULL, test_profiler_start_position);
138 CHECK_GT(test_profiler_start_position, test_start_position);
139 const char* test_profiler_stop_position =
140 StrNStr(log.start(), "test-profiler-stop,", log.length());
141 CHECK_NE(NULL, test_profiler_stop_position);
142 CHECK_GT(test_profiler_stop_position, test_profiler_start_position);
143 const char* test_profiler_start_2_position =
144 StrNStr(log.start(), "test-profiler-start-2,", log.length());
145 CHECK_NE(NULL, test_profiler_start_2_position);
146 CHECK_GT(test_profiler_start_2_position, test_profiler_stop_position);
147
148 // Nothing must be logged until profiling is resumed.
149 CHECK_EQ(NULL, StrNStr(test_start_position,
150 "code-creation,",
151 static_cast<int>(test_profiler_start_position -
152 test_start_position)));
153 // Nothing must be logged while profiling is suspended.
154 CHECK_EQ(NULL, StrNStr(test_profiler_stop_position,
155 "code-creation,",
156 static_cast<int>(test_profiler_start_2_position -
157 test_profiler_stop_position)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000158}
159
160
John Reck59135872010-11-02 12:39:01 -0700161// BUG(913). Need to implement support for profiling multiple VM threads.
162#if 0
Steve Blocka7e24c12009-10-30 11:49:00 +0000163
164namespace {
165
166class LoopingThread : public v8::internal::Thread {
167 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100168 explicit LoopingThread(v8::internal::Isolate* isolate)
169 : v8::internal::Thread(isolate),
Steve Blocka7e24c12009-10-30 11:49:00 +0000170 semaphore_(v8::internal::OS::CreateSemaphore(0)),
171 run_(true) {
172 }
173
174 virtual ~LoopingThread() { delete semaphore_; }
175
176 void Run() {
177 self_ = pthread_self();
178 RunLoop();
179 }
180
181 void SendSigProf() { pthread_kill(self_, SIGPROF); }
182
183 void Stop() { run_ = false; }
184
185 bool WaitForRunning() { return semaphore_->Wait(1000000); }
186
187 protected:
188 bool IsRunning() { return run_; }
189
190 virtual void RunLoop() = 0;
191
192 void SetV8ThreadId() {
193 v8_thread_id_ = v8::V8::GetCurrentThreadId();
194 }
195
196 void SignalRunning() { semaphore_->Signal(); }
197
198 private:
199 v8::internal::Semaphore* semaphore_;
200 bool run_;
201 pthread_t self_;
202 int v8_thread_id_;
203};
204
205
206class LoopingJsThread : public LoopingThread {
207 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100208 explicit LoopingJsThread(v8::internal::Isolate* isolate)
209 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 void RunLoop() {
Steve Block6ded16b2010-05-10 14:33:55 +0100211 v8::Locker locker;
Steve Block44f0eee2011-05-26 01:26:41 +0100212 CHECK(i::Isolate::Current() != NULL);
213 CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100214 SetV8ThreadId();
Steve Blocka7e24c12009-10-30 11:49:00 +0000215 while (IsRunning()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000216 v8::HandleScope scope;
217 v8::Persistent<v8::Context> context = v8::Context::New();
Steve Block6ded16b2010-05-10 14:33:55 +0100218 CHECK(!context.IsEmpty());
219 {
220 v8::Context::Scope context_scope(context);
221 SignalRunning();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000222 CompileRun(
Steve Block6ded16b2010-05-10 14:33:55 +0100223 "var j; for (var i=0; i<10000; ++i) { j = Math.sin(i); }");
224 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 context.Dispose();
226 i::OS::Sleep(1);
227 }
228 }
229};
230
231
232class LoopingNonJsThread : public LoopingThread {
233 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100234 explicit LoopingNonJsThread(v8::internal::Isolate* isolate)
235 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 void RunLoop() {
237 v8::Locker locker;
238 v8::Unlocker unlocker;
239 // Now thread has V8's id, but will not run VM code.
Steve Block44f0eee2011-05-26 01:26:41 +0100240 CHECK(i::Isolate::Current() != NULL);
241 CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000242 double i = 10;
243 SignalRunning();
244 while (IsRunning()) {
245 i = sin(i);
246 i::OS::Sleep(1);
247 }
248 }
249};
250
251
252class TestSampler : public v8::internal::Sampler {
253 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100254 explicit TestSampler(v8::internal::Isolate* isolate)
255 : Sampler(isolate, 0, true, true),
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 semaphore_(v8::internal::OS::CreateSemaphore(0)),
257 was_sample_stack_called_(false) {
258 }
259
260 ~TestSampler() { delete semaphore_; }
261
262 void SampleStack(v8::internal::TickSample*) {
263 was_sample_stack_called_ = true;
264 }
265
266 void Tick(v8::internal::TickSample*) { semaphore_->Signal(); }
267
268 bool WaitForTick() { return semaphore_->Wait(1000000); }
269
270 void Reset() { was_sample_stack_called_ = false; }
271
272 bool WasSampleStackCalled() { return was_sample_stack_called_; }
273
274 private:
275 v8::internal::Semaphore* semaphore_;
276 bool was_sample_stack_called_;
277};
278
279
280} // namespace
281
282TEST(ProfMultipleThreads) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100283 TestSampler* sampler = NULL;
284 {
285 v8::Locker locker;
Steve Block44f0eee2011-05-26 01:26:41 +0100286 sampler = new TestSampler(v8::internal::Isolate::Current());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100287 sampler->Start();
288 CHECK(sampler->IsActive());
289 }
290
Steve Block44f0eee2011-05-26 01:26:41 +0100291 LoopingJsThread jsThread(v8::internal::Isolate::Current());
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 jsThread.Start();
Steve Block44f0eee2011-05-26 01:26:41 +0100293 LoopingNonJsThread nonJsThread(v8::internal::Isolate::Current());
Steve Blocka7e24c12009-10-30 11:49:00 +0000294 nonJsThread.Start();
295
Ben Murdochb0fe1622011-05-05 13:52:32 +0100296 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000297 jsThread.WaitForRunning();
298 jsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100299 CHECK(sampler->WaitForTick());
300 CHECK(sampler->WasSampleStackCalled());
301 sampler->Reset();
302 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000303 nonJsThread.WaitForRunning();
304 nonJsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100305 CHECK(!sampler->WaitForTick());
306 CHECK(!sampler->WasSampleStackCalled());
307 sampler->Stop();
Steve Blocka7e24c12009-10-30 11:49:00 +0000308
309 jsThread.Stop();
310 nonJsThread.Stop();
311 jsThread.Join();
312 nonJsThread.Join();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100313
314 delete sampler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000315}
316
317#endif // __linux__
318
319
Steve Block3ce2e202009-11-05 08:53:23 +0000320// Test for issue http://crbug.com/23768 in Chromium.
321// Heap can contain scripts with already disposed external sources.
322// We need to verify that LogCompiledFunctions doesn't crash on them.
323namespace {
324
325class SimpleExternalString : public v8::String::ExternalStringResource {
326 public:
327 explicit SimpleExternalString(const char* source)
Steve Blockd0582a62009-12-15 09:54:21 +0000328 : utf_source_(StrLength(source)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000329 for (int i = 0; i < utf_source_.length(); ++i)
330 utf_source_[i] = source[i];
331 }
332 virtual ~SimpleExternalString() {}
333 virtual size_t length() const { return utf_source_.length(); }
334 virtual const uint16_t* data() const { return utf_source_.start(); }
335 private:
336 i::ScopedVector<uint16_t> utf_source_;
337};
338
339} // namespace
340
341TEST(Issue23768) {
342 v8::HandleScope scope;
343 v8::Handle<v8::Context> env = v8::Context::New();
344 env->Enter();
345
346 SimpleExternalString source_ext_str("(function ext() {})();");
347 v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str);
348 // Script needs to have a name in order to trigger InitLineEnds execution.
349 v8::Handle<v8::String> origin = v8::String::New("issue-23768-test");
350 v8::Handle<v8::Script> evil_script = v8::Script::Compile(source, origin);
351 CHECK(!evil_script.IsEmpty());
352 CHECK(!evil_script->Run().IsEmpty());
353 i::Handle<i::ExternalTwoByteString> i_source(
354 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
355 // This situation can happen if source was an external string disposed
356 // by its owner.
357 i_source->set_resource(NULL);
358
359 // Must not crash.
Steve Block44f0eee2011-05-26 01:26:41 +0100360 LOGGER->LogCompiledFunctions();
Steve Block3ce2e202009-11-05 08:53:23 +0000361}
362
363
Steve Blockd0582a62009-12-15 09:54:21 +0000364static v8::Handle<v8::Value> ObjMethod1(const v8::Arguments& args) {
365 return v8::Handle<v8::Value>();
366}
367
368TEST(LogCallbacks) {
Steve Block6ded16b2010-05-10 14:33:55 +0100369 ScopedLoggerInitializer initialize_logger(false);
Steve Blockd0582a62009-12-15 09:54:21 +0000370
371 v8::Persistent<v8::FunctionTemplate> obj =
372 v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000373 obj->SetClassName(v8_str("Obj"));
Steve Blockd0582a62009-12-15 09:54:21 +0000374 v8::Handle<v8::ObjectTemplate> proto = obj->PrototypeTemplate();
375 v8::Local<v8::Signature> signature = v8::Signature::New(obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000376 proto->Set(v8_str("method1"),
Steve Blockd0582a62009-12-15 09:54:21 +0000377 v8::FunctionTemplate::New(ObjMethod1,
378 v8::Handle<v8::Value>(),
379 signature),
380 static_cast<v8::PropertyAttribute>(v8::DontDelete));
381
Andrei Popescu402d9372010-02-26 13:31:12 +0000382 initialize_logger.env()->Global()->Set(v8_str("Obj"), obj->GetFunction());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000383 CompileRun("Obj.prototype.method1.toString();");
Steve Blockd0582a62009-12-15 09:54:21 +0000384
Steve Block44f0eee2011-05-26 01:26:41 +0100385 LOGGER->LogCompiledFunctions();
Steve Blockd0582a62009-12-15 09:54:21 +0000386
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000387 bool exists = false;
388 i::Vector<const char> log(
389 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
390 CHECK(exists);
391
392 i::EmbeddedVector<char, 100> ref_data;
Steve Blockd0582a62009-12-15 09:54:21 +0000393 i::OS::SNPrintF(ref_data,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000394 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"method1\"\0",
395 ObjMethod1);
396
397 CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length()));
Steve Blockd0582a62009-12-15 09:54:21 +0000398
399 obj.Dispose();
Steve Blockd0582a62009-12-15 09:54:21 +0000400}
401
402
403static v8::Handle<v8::Value> Prop1Getter(v8::Local<v8::String> property,
404 const v8::AccessorInfo& info) {
405 return v8::Handle<v8::Value>();
406}
407
408static void Prop1Setter(v8::Local<v8::String> property,
409 v8::Local<v8::Value> value,
410 const v8::AccessorInfo& info) {
411}
412
413static v8::Handle<v8::Value> Prop2Getter(v8::Local<v8::String> property,
414 const v8::AccessorInfo& info) {
415 return v8::Handle<v8::Value>();
416}
417
418TEST(LogAccessorCallbacks) {
Steve Block6ded16b2010-05-10 14:33:55 +0100419 ScopedLoggerInitializer initialize_logger(false);
Steve Blockd0582a62009-12-15 09:54:21 +0000420
421 v8::Persistent<v8::FunctionTemplate> obj =
422 v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000423 obj->SetClassName(v8_str("Obj"));
Steve Blockd0582a62009-12-15 09:54:21 +0000424 v8::Handle<v8::ObjectTemplate> inst = obj->InstanceTemplate();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000425 inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter);
426 inst->SetAccessor(v8_str("prop2"), Prop2Getter);
Steve Blockd0582a62009-12-15 09:54:21 +0000427
Steve Block44f0eee2011-05-26 01:26:41 +0100428 LOGGER->LogAccessorCallbacks();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000429
430 bool exists = false;
431 i::Vector<const char> log(
432 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
433 CHECK(exists);
Steve Blockd0582a62009-12-15 09:54:21 +0000434
435 EmbeddedVector<char, 100> prop1_getter_record;
436 i::OS::SNPrintF(prop1_getter_record,
437 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"get prop1\"",
438 Prop1Getter);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000439 CHECK_NE(NULL,
440 StrNStr(log.start(), prop1_getter_record.start(), log.length()));
441
Steve Blockd0582a62009-12-15 09:54:21 +0000442 EmbeddedVector<char, 100> prop1_setter_record;
443 i::OS::SNPrintF(prop1_setter_record,
444 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"set prop1\"",
445 Prop1Setter);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000446 CHECK_NE(NULL,
447 StrNStr(log.start(), prop1_setter_record.start(), log.length()));
448
Steve Blockd0582a62009-12-15 09:54:21 +0000449 EmbeddedVector<char, 100> prop2_getter_record;
450 i::OS::SNPrintF(prop2_getter_record,
451 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"get prop2\"",
452 Prop2Getter);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000453 CHECK_NE(NULL,
454 StrNStr(log.start(), prop2_getter_record.start(), log.length()));
Steve Blockd0582a62009-12-15 09:54:21 +0000455
456 obj.Dispose();
Andrei Popescu402d9372010-02-26 13:31:12 +0000457}
Steve Blockd0582a62009-12-15 09:54:21 +0000458
Andrei Popescu402d9372010-02-26 13:31:12 +0000459
Steve Block6ded16b2010-05-10 14:33:55 +0100460TEST(IsLoggingPreserved) {
461 ScopedLoggerInitializer initialize_logger(false);
462
Steve Block44f0eee2011-05-26 01:26:41 +0100463 CHECK(LOGGER->is_logging());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000464 LOGGER->ResumeProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +0100465 CHECK(LOGGER->is_logging());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000466 LOGGER->PauseProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +0100467 CHECK(LOGGER->is_logging());
Steve Block6ded16b2010-05-10 14:33:55 +0100468}
469
470
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000471typedef i::NativesCollection<i::TEST> TestSources;
Steve Blocka7e24c12009-10-30 11:49:00 +0000472
473// Test that logging of code create / move / delete events
474// is equivalent to traversal of a resulting heap.
475TEST(EquivalenceOfLoggingAndTraversal) {
476 // This test needs to be run on a "clean" V8 to ensure that snapshot log
477 // is loaded. This is always true when running using tools/test.py because
478 // it launches a new cctest instance for every test. To be sure that launching
479 // cctest manually also works, please be sure that no tests below
480 // are using V8.
481 //
482 // P.S. No, V8 can't be re-initialized after disposal, see include/v8.h.
483 CHECK(!i::V8::IsRunning());
484
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000485 // Start with profiling to capture all code events from the beginning.
486 ScopedLoggerInitializer initialize_logger(false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000487
488 // Compile and run a function that creates other functions.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000489 CompileRun(
Steve Blocka7e24c12009-10-30 11:49:00 +0000490 "(function f(obj) {\n"
491 " obj.test =\n"
492 " (function a(j) { return function b() { return j; } })(100);\n"
493 "})(this);");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000494 v8::V8::PauseProfiler();
495 HEAP->CollectAllGarbage(true);
496 LOGGER->StringEvent("test-logging-done", "");
Steve Blocka7e24c12009-10-30 11:49:00 +0000497
498 // Iterate heap to find compiled functions, will write to log.
Steve Block44f0eee2011-05-26 01:26:41 +0100499 LOGGER->LogCompiledFunctions();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000500 LOGGER->StringEvent("test-traversal-done", "");
Steve Blocka7e24c12009-10-30 11:49:00 +0000501
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000502 bool exists = false;
503 i::Vector<const char> log(
504 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
505 CHECK(exists);
506 v8::Handle<v8::String> log_str = v8::String::New(log.start(), log.length());
507 initialize_logger.env()->Global()->Set(v8_str("_log"), log_str);
Steve Blocka7e24c12009-10-30 11:49:00 +0000508
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000509 i::Vector<const unsigned char> source = TestSources::GetScriptsSource();
510 v8::Handle<v8::String> source_str = v8::String::New(
511 reinterpret_cast<const char*>(source.start()), source.length());
512 v8::TryCatch try_catch;
513 v8::Handle<v8::Script> script = v8::Script::Compile(source_str, v8_str(""));
514 if (script.IsEmpty()) {
515 v8::String::Utf8Value exception(try_catch.Exception());
516 printf("compile: %s\n", *exception);
517 CHECK(false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000518 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000519 v8::Handle<v8::Value> result = script->Run();
520 if (result.IsEmpty()) {
521 v8::String::Utf8Value exception(try_catch.Exception());
522 printf("run: %s\n", *exception);
523 CHECK(false);
524 }
525 // The result either be a "true" literal or problem description.
526 if (!result->IsTrue()) {
527 v8::Local<v8::String> s = result->ToString();
528 i::ScopedVector<char> data(s->Length() + 1);
529 CHECK_NE(NULL, data.start());
530 s->WriteAscii(data.start());
531 printf("%s\n", data.start());
532 // Make sure that our output is written prior crash due to CHECK failure.
533 fflush(stdout);
534 CHECK(false);
535 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000536}