blob: 72e663c4623012c21c1807369412eedf32c9c7a8 [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
Ben Murdoch589d6972011-11-30 16:04:58 +000028
Andrei Popescu402d9372010-02-26 13:31:12 +000029class ScopedLoggerInitializer {
30 public:
Steve Block6ded16b2010-05-10 14:33:55 +010031 explicit ScopedLoggerInitializer(bool prof_lazy)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000032 : saved_log_(i::FLAG_log),
33 saved_prof_lazy_(i::FLAG_prof_lazy),
Andrei Popescu402d9372010-02-26 13:31:12 +000034 saved_prof_(i::FLAG_prof),
35 saved_prof_auto_(i::FLAG_prof_auto),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000036 temp_file_(NULL),
37 // Need to run this prior to creating the scope.
Steve Block6ded16b2010-05-10 14:33:55 +010038 trick_to_run_init_flags_(init_flags_(prof_lazy)),
Andrei Popescu402d9372010-02-26 13:31:12 +000039 scope_(),
40 env_(v8::Context::New()) {
Andrei Popescu402d9372010-02-26 13:31:12 +000041 env_->Enter();
42 }
43
44 ~ScopedLoggerInitializer() {
45 env_->Exit();
Steve Block44f0eee2011-05-26 01:26:41 +010046 LOGGER->TearDown();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000047 if (temp_file_ != NULL) fclose(temp_file_);
Andrei Popescu402d9372010-02-26 13:31:12 +000048 i::FLAG_prof_lazy = saved_prof_lazy_;
49 i::FLAG_prof = saved_prof_;
50 i::FLAG_prof_auto = saved_prof_auto_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000051 i::FLAG_log = saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +000052 }
53
54 v8::Handle<v8::Context>& env() { return env_; }
55
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000056 FILE* StopLoggingGetTempFile() {
57 temp_file_ = LOGGER->TearDown();
58 CHECK_NE(NULL, temp_file_);
59 fflush(temp_file_);
60 rewind(temp_file_);
61 return temp_file_;
62 }
63
Andrei Popescu402d9372010-02-26 13:31:12 +000064 private:
Steve Block6ded16b2010-05-10 14:33:55 +010065 static bool init_flags_(bool prof_lazy) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000066 i::FLAG_log = true;
Andrei Popescu402d9372010-02-26 13:31:12 +000067 i::FLAG_prof = true;
68 i::FLAG_prof_lazy = prof_lazy;
69 i::FLAG_prof_auto = false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000070 i::FLAG_logfile = i::Log::kLogToTemporaryFile;
Andrei Popescu402d9372010-02-26 13:31:12 +000071 return prof_lazy;
72 }
73
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000074 const bool saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +000075 const bool saved_prof_lazy_;
76 const bool saved_prof_;
77 const bool saved_prof_auto_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000078 FILE* temp_file_;
Andrei Popescu402d9372010-02-26 13:31:12 +000079 const bool trick_to_run_init_flags_;
Andrei Popescu402d9372010-02-26 13:31:12 +000080 v8::HandleScope scope_;
81 v8::Handle<v8::Context> env_;
82
83 DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer);
84};
85
Andrei Popescu402d9372010-02-26 13:31:12 +000086} // namespace
87
88
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000089static const char* StrNStr(const char* s1, const char* s2, int n) {
90 if (s1[n] == '\0') return strstr(s1, s2);
91 i::ScopedVector<char> str(n + 1);
92 i::OS::StrNCpy(str, s1, static_cast<size_t>(n));
93 str[n] = '\0';
94 char* found = strstr(str.start(), s2);
95 return found != NULL ? s1 + (found - str.start()) : NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000096}
97
98
99TEST(ProfLazyMode) {
Steve Block6ded16b2010-05-10 14:33:55 +0100100 ScopedLoggerInitializer initialize_logger(true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000101
Ben Murdochb0fe1622011-05-05 13:52:32 +0100102 if (!i::V8::UseCrankshaft()) return;
103
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000104 LOGGER->StringEvent("test-start", "");
105 CompileRun("var a = (function(x) { return x + 1; })(10);");
106 LOGGER->StringEvent("test-profiler-start", "");
107 v8::V8::ResumeProfiler();
108 CompileRun(
109 "var b = (function(x) { return x + 2; })(10);\n"
110 "var c = (function(x) { return x + 3; })(10);\n"
111 "var d = (function(x) { return x + 4; })(10);\n"
112 "var e = (function(x) { return x + 5; })(10);");
113 v8::V8::PauseProfiler();
114 LOGGER->StringEvent("test-profiler-stop", "");
115 CompileRun("var f = (function(x) { return x + 6; })(10);");
Steve Blocka7e24c12009-10-30 11:49:00 +0000116 // Check that profiling can be resumed again.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000117 LOGGER->StringEvent("test-profiler-start-2", "");
118 v8::V8::ResumeProfiler();
119 CompileRun(
120 "var g = (function(x) { return x + 7; })(10);\n"
121 "var h = (function(x) { return x + 8; })(10);\n"
122 "var i = (function(x) { return x + 9; })(10);\n"
123 "var j = (function(x) { return x + 10; })(10);");
124 v8::V8::PauseProfiler();
125 LOGGER->StringEvent("test-profiler-stop-2", "");
126 LOGGER->StringEvent("test-stop", "");
127
128 bool exists = false;
129 i::Vector<const char> log(
130 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
131 CHECK(exists);
132
133 const char* test_start_position =
134 StrNStr(log.start(), "test-start,", log.length());
135 CHECK_NE(NULL, test_start_position);
136 const char* test_profiler_start_position =
137 StrNStr(log.start(), "test-profiler-start,", log.length());
138 CHECK_NE(NULL, test_profiler_start_position);
139 CHECK_GT(test_profiler_start_position, test_start_position);
140 const char* test_profiler_stop_position =
141 StrNStr(log.start(), "test-profiler-stop,", log.length());
142 CHECK_NE(NULL, test_profiler_stop_position);
143 CHECK_GT(test_profiler_stop_position, test_profiler_start_position);
144 const char* test_profiler_start_2_position =
145 StrNStr(log.start(), "test-profiler-start-2,", log.length());
146 CHECK_NE(NULL, test_profiler_start_2_position);
147 CHECK_GT(test_profiler_start_2_position, test_profiler_stop_position);
148
149 // Nothing must be logged until profiling is resumed.
150 CHECK_EQ(NULL, StrNStr(test_start_position,
151 "code-creation,",
152 static_cast<int>(test_profiler_start_position -
153 test_start_position)));
154 // Nothing must be logged while profiling is suspended.
155 CHECK_EQ(NULL, StrNStr(test_profiler_stop_position,
156 "code-creation,",
157 static_cast<int>(test_profiler_start_2_position -
158 test_profiler_stop_position)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000159}
160
161
John Reck59135872010-11-02 12:39:01 -0700162// BUG(913). Need to implement support for profiling multiple VM threads.
163#if 0
Steve Blocka7e24c12009-10-30 11:49:00 +0000164
165namespace {
166
167class LoopingThread : public v8::internal::Thread {
168 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100169 explicit LoopingThread(v8::internal::Isolate* isolate)
170 : v8::internal::Thread(isolate),
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 semaphore_(v8::internal::OS::CreateSemaphore(0)),
172 run_(true) {
173 }
174
175 virtual ~LoopingThread() { delete semaphore_; }
176
177 void Run() {
178 self_ = pthread_self();
179 RunLoop();
180 }
181
182 void SendSigProf() { pthread_kill(self_, SIGPROF); }
183
184 void Stop() { run_ = false; }
185
186 bool WaitForRunning() { return semaphore_->Wait(1000000); }
187
188 protected:
189 bool IsRunning() { return run_; }
190
191 virtual void RunLoop() = 0;
192
193 void SetV8ThreadId() {
194 v8_thread_id_ = v8::V8::GetCurrentThreadId();
195 }
196
197 void SignalRunning() { semaphore_->Signal(); }
198
199 private:
200 v8::internal::Semaphore* semaphore_;
201 bool run_;
202 pthread_t self_;
203 int v8_thread_id_;
204};
205
206
207class LoopingJsThread : public LoopingThread {
208 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100209 explicit LoopingJsThread(v8::internal::Isolate* isolate)
210 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000211 void RunLoop() {
Steve Block6ded16b2010-05-10 14:33:55 +0100212 v8::Locker locker;
Steve Block44f0eee2011-05-26 01:26:41 +0100213 CHECK(i::Isolate::Current() != NULL);
214 CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100215 SetV8ThreadId();
Steve Blocka7e24c12009-10-30 11:49:00 +0000216 while (IsRunning()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000217 v8::HandleScope scope;
218 v8::Persistent<v8::Context> context = v8::Context::New();
Steve Block6ded16b2010-05-10 14:33:55 +0100219 CHECK(!context.IsEmpty());
220 {
221 v8::Context::Scope context_scope(context);
222 SignalRunning();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000223 CompileRun(
Steve Block6ded16b2010-05-10 14:33:55 +0100224 "var j; for (var i=0; i<10000; ++i) { j = Math.sin(i); }");
225 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 context.Dispose();
227 i::OS::Sleep(1);
228 }
229 }
230};
231
232
233class LoopingNonJsThread : public LoopingThread {
234 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100235 explicit LoopingNonJsThread(v8::internal::Isolate* isolate)
236 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 void RunLoop() {
238 v8::Locker locker;
239 v8::Unlocker unlocker;
240 // Now thread has V8's id, but will not run VM code.
Steve Block44f0eee2011-05-26 01:26:41 +0100241 CHECK(i::Isolate::Current() != NULL);
242 CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000243 double i = 10;
244 SignalRunning();
245 while (IsRunning()) {
246 i = sin(i);
247 i::OS::Sleep(1);
248 }
249 }
250};
251
252
253class TestSampler : public v8::internal::Sampler {
254 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100255 explicit TestSampler(v8::internal::Isolate* isolate)
256 : Sampler(isolate, 0, true, true),
Steve Blocka7e24c12009-10-30 11:49:00 +0000257 semaphore_(v8::internal::OS::CreateSemaphore(0)),
258 was_sample_stack_called_(false) {
259 }
260
261 ~TestSampler() { delete semaphore_; }
262
263 void SampleStack(v8::internal::TickSample*) {
264 was_sample_stack_called_ = true;
265 }
266
267 void Tick(v8::internal::TickSample*) { semaphore_->Signal(); }
268
269 bool WaitForTick() { return semaphore_->Wait(1000000); }
270
271 void Reset() { was_sample_stack_called_ = false; }
272
273 bool WasSampleStackCalled() { return was_sample_stack_called_; }
274
275 private:
276 v8::internal::Semaphore* semaphore_;
277 bool was_sample_stack_called_;
278};
279
280
281} // namespace
282
283TEST(ProfMultipleThreads) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100284 TestSampler* sampler = NULL;
285 {
286 v8::Locker locker;
Steve Block44f0eee2011-05-26 01:26:41 +0100287 sampler = new TestSampler(v8::internal::Isolate::Current());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100288 sampler->Start();
289 CHECK(sampler->IsActive());
290 }
291
Steve Block44f0eee2011-05-26 01:26:41 +0100292 LoopingJsThread jsThread(v8::internal::Isolate::Current());
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 jsThread.Start();
Steve Block44f0eee2011-05-26 01:26:41 +0100294 LoopingNonJsThread nonJsThread(v8::internal::Isolate::Current());
Steve Blocka7e24c12009-10-30 11:49:00 +0000295 nonJsThread.Start();
296
Ben Murdochb0fe1622011-05-05 13:52:32 +0100297 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000298 jsThread.WaitForRunning();
299 jsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100300 CHECK(sampler->WaitForTick());
301 CHECK(sampler->WasSampleStackCalled());
302 sampler->Reset();
303 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 nonJsThread.WaitForRunning();
305 nonJsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100306 CHECK(!sampler->WaitForTick());
307 CHECK(!sampler->WasSampleStackCalled());
308 sampler->Stop();
Steve Blocka7e24c12009-10-30 11:49:00 +0000309
310 jsThread.Stop();
311 nonJsThread.Stop();
312 jsThread.Join();
313 nonJsThread.Join();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100314
315 delete sampler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000316}
317
318#endif // __linux__
319
320
Steve Block3ce2e202009-11-05 08:53:23 +0000321// Test for issue http://crbug.com/23768 in Chromium.
322// Heap can contain scripts with already disposed external sources.
323// We need to verify that LogCompiledFunctions doesn't crash on them.
324namespace {
325
326class SimpleExternalString : public v8::String::ExternalStringResource {
327 public:
328 explicit SimpleExternalString(const char* source)
Steve Blockd0582a62009-12-15 09:54:21 +0000329 : utf_source_(StrLength(source)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000330 for (int i = 0; i < utf_source_.length(); ++i)
331 utf_source_[i] = source[i];
332 }
333 virtual ~SimpleExternalString() {}
334 virtual size_t length() const { return utf_source_.length(); }
335 virtual const uint16_t* data() const { return utf_source_.start(); }
336 private:
337 i::ScopedVector<uint16_t> utf_source_;
338};
339
340} // namespace
341
342TEST(Issue23768) {
343 v8::HandleScope scope;
344 v8::Handle<v8::Context> env = v8::Context::New();
345 env->Enter();
346
347 SimpleExternalString source_ext_str("(function ext() {})();");
348 v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str);
349 // Script needs to have a name in order to trigger InitLineEnds execution.
350 v8::Handle<v8::String> origin = v8::String::New("issue-23768-test");
351 v8::Handle<v8::Script> evil_script = v8::Script::Compile(source, origin);
352 CHECK(!evil_script.IsEmpty());
353 CHECK(!evil_script->Run().IsEmpty());
354 i::Handle<i::ExternalTwoByteString> i_source(
355 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
356 // This situation can happen if source was an external string disposed
357 // by its owner.
358 i_source->set_resource(NULL);
359
360 // Must not crash.
Steve Block44f0eee2011-05-26 01:26:41 +0100361 LOGGER->LogCompiledFunctions();
Steve Block3ce2e202009-11-05 08:53:23 +0000362}
363
364
Steve Blockd0582a62009-12-15 09:54:21 +0000365static v8::Handle<v8::Value> ObjMethod1(const v8::Arguments& args) {
366 return v8::Handle<v8::Value>();
367}
368
369TEST(LogCallbacks) {
Steve Block6ded16b2010-05-10 14:33:55 +0100370 ScopedLoggerInitializer initialize_logger(false);
Steve Blockd0582a62009-12-15 09:54:21 +0000371
372 v8::Persistent<v8::FunctionTemplate> obj =
373 v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000374 obj->SetClassName(v8_str("Obj"));
Steve Blockd0582a62009-12-15 09:54:21 +0000375 v8::Handle<v8::ObjectTemplate> proto = obj->PrototypeTemplate();
376 v8::Local<v8::Signature> signature = v8::Signature::New(obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000377 proto->Set(v8_str("method1"),
Steve Blockd0582a62009-12-15 09:54:21 +0000378 v8::FunctionTemplate::New(ObjMethod1,
379 v8::Handle<v8::Value>(),
380 signature),
381 static_cast<v8::PropertyAttribute>(v8::DontDelete));
382
Andrei Popescu402d9372010-02-26 13:31:12 +0000383 initialize_logger.env()->Global()->Set(v8_str("Obj"), obj->GetFunction());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000384 CompileRun("Obj.prototype.method1.toString();");
Steve Blockd0582a62009-12-15 09:54:21 +0000385
Steve Block44f0eee2011-05-26 01:26:41 +0100386 LOGGER->LogCompiledFunctions();
Steve Blockd0582a62009-12-15 09:54:21 +0000387
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000388 bool exists = false;
389 i::Vector<const char> log(
390 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
391 CHECK(exists);
392
393 i::EmbeddedVector<char, 100> ref_data;
Steve Blockd0582a62009-12-15 09:54:21 +0000394 i::OS::SNPrintF(ref_data,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000395 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"method1\"\0",
396 ObjMethod1);
397
398 CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length()));
Steve Blockd0582a62009-12-15 09:54:21 +0000399
400 obj.Dispose();
Steve Blockd0582a62009-12-15 09:54:21 +0000401}
402
403
404static v8::Handle<v8::Value> Prop1Getter(v8::Local<v8::String> property,
405 const v8::AccessorInfo& info) {
406 return v8::Handle<v8::Value>();
407}
408
409static void Prop1Setter(v8::Local<v8::String> property,
410 v8::Local<v8::Value> value,
411 const v8::AccessorInfo& info) {
412}
413
414static v8::Handle<v8::Value> Prop2Getter(v8::Local<v8::String> property,
415 const v8::AccessorInfo& info) {
416 return v8::Handle<v8::Value>();
417}
418
419TEST(LogAccessorCallbacks) {
Steve Block6ded16b2010-05-10 14:33:55 +0100420 ScopedLoggerInitializer initialize_logger(false);
Steve Blockd0582a62009-12-15 09:54:21 +0000421
422 v8::Persistent<v8::FunctionTemplate> obj =
423 v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000424 obj->SetClassName(v8_str("Obj"));
Steve Blockd0582a62009-12-15 09:54:21 +0000425 v8::Handle<v8::ObjectTemplate> inst = obj->InstanceTemplate();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000426 inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter);
427 inst->SetAccessor(v8_str("prop2"), Prop2Getter);
Steve Blockd0582a62009-12-15 09:54:21 +0000428
Steve Block44f0eee2011-05-26 01:26:41 +0100429 LOGGER->LogAccessorCallbacks();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000430
431 bool exists = false;
432 i::Vector<const char> log(
433 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
434 CHECK(exists);
Steve Blockd0582a62009-12-15 09:54:21 +0000435
436 EmbeddedVector<char, 100> prop1_getter_record;
437 i::OS::SNPrintF(prop1_getter_record,
438 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"get prop1\"",
439 Prop1Getter);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000440 CHECK_NE(NULL,
441 StrNStr(log.start(), prop1_getter_record.start(), log.length()));
442
Steve Blockd0582a62009-12-15 09:54:21 +0000443 EmbeddedVector<char, 100> prop1_setter_record;
444 i::OS::SNPrintF(prop1_setter_record,
445 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"set prop1\"",
446 Prop1Setter);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000447 CHECK_NE(NULL,
448 StrNStr(log.start(), prop1_setter_record.start(), log.length()));
449
Steve Blockd0582a62009-12-15 09:54:21 +0000450 EmbeddedVector<char, 100> prop2_getter_record;
451 i::OS::SNPrintF(prop2_getter_record,
452 "code-creation,Callback,0x%" V8PRIxPTR ",1,\"get prop2\"",
453 Prop2Getter);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000454 CHECK_NE(NULL,
455 StrNStr(log.start(), prop2_getter_record.start(), log.length()));
Steve Blockd0582a62009-12-15 09:54:21 +0000456
457 obj.Dispose();
Andrei Popescu402d9372010-02-26 13:31:12 +0000458}
Steve Blockd0582a62009-12-15 09:54:21 +0000459
Andrei Popescu402d9372010-02-26 13:31:12 +0000460
Steve Block6ded16b2010-05-10 14:33:55 +0100461TEST(IsLoggingPreserved) {
462 ScopedLoggerInitializer initialize_logger(false);
463
Steve Block44f0eee2011-05-26 01:26:41 +0100464 CHECK(LOGGER->is_logging());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000465 LOGGER->ResumeProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +0100466 CHECK(LOGGER->is_logging());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000467 LOGGER->PauseProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +0100468 CHECK(LOGGER->is_logging());
Steve Block6ded16b2010-05-10 14:33:55 +0100469}
470
471
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000472typedef i::NativesCollection<i::TEST> TestSources;
Steve Blocka7e24c12009-10-30 11:49:00 +0000473
Ben Murdoch589d6972011-11-30 16:04:58 +0000474
475// Test that logging of code create / move events is equivalent to traversal of
476// a resulting heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000477TEST(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}