blob: 482f89f9c47675c4a70c1183260081cf07c26a92 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// 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.
Steve Blocka7e24c12009-10-30 11:49:00 +000027//
28// Tests of logging functions from log.h
29
Steve Blocka7e24c12009-10-30 11:49:00 +000030#ifdef __linux__
Steve Blocka7e24c12009-10-30 11:49:00 +000031#include <pthread.h>
32#include <signal.h>
33#include <unistd.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034#include <cmath>
Steve Blocka7e24c12009-10-30 11:49:00 +000035#endif // __linux__
36
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037#include "src/v8.h"
38
39#include "src/cpu-profiler.h"
40#include "src/log.h"
41#include "src/log-utils.h"
42#include "src/natives.h"
43#include "src/utils.h"
44#include "src/v8threads.h"
45#include "src/vm-state-inl.h"
46#include "test/cctest/cctest.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000047
48using v8::internal::Address;
49using v8::internal::EmbeddedVector;
50using v8::internal::Logger;
Steve Blockd0582a62009-12-15 09:54:21 +000051using v8::internal::StrLength;
Steve Blocka7e24c12009-10-30 11:49:00 +000052
Andrei Popescu402d9372010-02-26 13:31:12 +000053namespace {
54
Ben Murdoch589d6972011-11-30 16:04:58 +000055
Andrei Popescu402d9372010-02-26 13:31:12 +000056class ScopedLoggerInitializer {
57 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 ScopedLoggerInitializer()
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000059 : saved_log_(i::FLAG_log),
Andrei Popescu402d9372010-02-26 13:31:12 +000060 saved_prof_(i::FLAG_prof),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000061 temp_file_(NULL),
62 // Need to run this prior to creating the scope.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 trick_to_run_init_flags_(init_flags_()),
64 isolate_(v8::Isolate::New()),
65 isolate_scope_(isolate_),
66 scope_(isolate_),
67 env_(v8::Context::New(isolate_)),
68 logger_(reinterpret_cast<i::Isolate*>(isolate_)->logger()) {
Andrei Popescu402d9372010-02-26 13:31:12 +000069 env_->Enter();
70 }
71
72 ~ScopedLoggerInitializer() {
73 env_->Exit();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 logger_->TearDown();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000075 if (temp_file_ != NULL) fclose(temp_file_);
Andrei Popescu402d9372010-02-26 13:31:12 +000076 i::FLAG_prof = saved_prof_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000077 i::FLAG_log = saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +000078 }
79
80 v8::Handle<v8::Context>& env() { return env_; }
81
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 v8::Isolate* isolate() { return isolate_; }
83
84 Logger* logger() { return logger_; }
85
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000086 FILE* StopLoggingGetTempFile() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087 temp_file_ = logger_->TearDown();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000088 CHECK_NE(NULL, temp_file_);
89 fflush(temp_file_);
90 rewind(temp_file_);
91 return temp_file_;
92 }
93
Andrei Popescu402d9372010-02-26 13:31:12 +000094 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095 static bool init_flags_() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000096 i::FLAG_log = true;
Andrei Popescu402d9372010-02-26 13:31:12 +000097 i::FLAG_prof = true;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000098 i::FLAG_logfile = i::Log::kLogToTemporaryFile;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 i::FLAG_logfile_per_isolate = false;
100 return false;
Andrei Popescu402d9372010-02-26 13:31:12 +0000101 }
102
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000103 const bool saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000104 const bool saved_prof_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000105 FILE* temp_file_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000106 const bool trick_to_run_init_flags_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107 v8::Isolate* isolate_;
108 v8::Isolate::Scope isolate_scope_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000109 v8::HandleScope scope_;
110 v8::Handle<v8::Context> env_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000111 Logger* logger_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000112
113 DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer);
114};
115
Andrei Popescu402d9372010-02-26 13:31:12 +0000116} // namespace
117
118
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000119static const char* StrNStr(const char* s1, const char* s2, int n) {
120 if (s1[n] == '\0') return strstr(s1, s2);
121 i::ScopedVector<char> str(n + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122 i::StrNCpy(str, s1, static_cast<size_t>(n));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000123 str[n] = '\0';
124 char* found = strstr(str.start(), s2);
125 return found != NULL ? s1 + (found - str.start()) : NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000126}
127
128
John Reck59135872010-11-02 12:39:01 -0700129// BUG(913). Need to implement support for profiling multiple VM threads.
130#if 0
Steve Blocka7e24c12009-10-30 11:49:00 +0000131
132namespace {
133
134class LoopingThread : public v8::internal::Thread {
135 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100136 explicit LoopingThread(v8::internal::Isolate* isolate)
137 : v8::internal::Thread(isolate),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 semaphore_(new v8::internal::Semaphore(0)),
Steve Blocka7e24c12009-10-30 11:49:00 +0000139 run_(true) {
140 }
141
142 virtual ~LoopingThread() { delete semaphore_; }
143
144 void Run() {
145 self_ = pthread_self();
146 RunLoop();
147 }
148
149 void SendSigProf() { pthread_kill(self_, SIGPROF); }
150
151 void Stop() { run_ = false; }
152
153 bool WaitForRunning() { return semaphore_->Wait(1000000); }
154
155 protected:
156 bool IsRunning() { return run_; }
157
158 virtual void RunLoop() = 0;
159
160 void SetV8ThreadId() {
161 v8_thread_id_ = v8::V8::GetCurrentThreadId();
162 }
163
164 void SignalRunning() { semaphore_->Signal(); }
165
166 private:
167 v8::internal::Semaphore* semaphore_;
168 bool run_;
169 pthread_t self_;
170 int v8_thread_id_;
171};
172
173
174class LoopingJsThread : public LoopingThread {
175 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100176 explicit LoopingJsThread(v8::internal::Isolate* isolate)
177 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000178 void RunLoop() {
Steve Block6ded16b2010-05-10 14:33:55 +0100179 v8::Locker locker;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 CHECK(CcTest::i_isolate() != NULL);
181 CHECK_GT(CcTest::i_isolate()->thread_manager()->CurrentId(), 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100182 SetV8ThreadId();
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 while (IsRunning()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000184 v8::HandleScope scope;
185 v8::Persistent<v8::Context> context = v8::Context::New();
Steve Block6ded16b2010-05-10 14:33:55 +0100186 CHECK(!context.IsEmpty());
187 {
188 v8::Context::Scope context_scope(context);
189 SignalRunning();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000190 CompileRun(
Steve Block6ded16b2010-05-10 14:33:55 +0100191 "var j; for (var i=0; i<10000; ++i) { j = Math.sin(i); }");
192 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 context.Dispose();
194 i::OS::Sleep(1);
195 }
196 }
197};
198
199
200class LoopingNonJsThread : public LoopingThread {
201 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100202 explicit LoopingNonJsThread(v8::internal::Isolate* isolate)
203 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 void RunLoop() {
205 v8::Locker locker;
206 v8::Unlocker unlocker;
207 // Now thread has V8's id, but will not run VM code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000208 CHECK(CcTest::i_isolate() != NULL);
209 CHECK_GT(CcTest::i_isolate()->thread_manager()->CurrentId(), 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 double i = 10;
211 SignalRunning();
212 while (IsRunning()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 i = std::sin(i);
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 i::OS::Sleep(1);
215 }
216 }
217};
218
219
220class TestSampler : public v8::internal::Sampler {
221 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100222 explicit TestSampler(v8::internal::Isolate* isolate)
223 : Sampler(isolate, 0, true, true),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000224 semaphore_(new v8::internal::Semaphore(0)),
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 was_sample_stack_called_(false) {
226 }
227
228 ~TestSampler() { delete semaphore_; }
229
230 void SampleStack(v8::internal::TickSample*) {
231 was_sample_stack_called_ = true;
232 }
233
234 void Tick(v8::internal::TickSample*) { semaphore_->Signal(); }
235
236 bool WaitForTick() { return semaphore_->Wait(1000000); }
237
238 void Reset() { was_sample_stack_called_ = false; }
239
240 bool WasSampleStackCalled() { return was_sample_stack_called_; }
241
242 private:
243 v8::internal::Semaphore* semaphore_;
244 bool was_sample_stack_called_;
245};
246
247
248} // namespace
249
250TEST(ProfMultipleThreads) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100251 TestSampler* sampler = NULL;
252 {
253 v8::Locker locker;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000254 sampler = new TestSampler(CcTest::i_isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100255 sampler->Start();
256 CHECK(sampler->IsActive());
257 }
258
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000259 LoopingJsThread jsThread(CcTest::i_isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 jsThread.Start();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 LoopingNonJsThread nonJsThread(CcTest::i_isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000262 nonJsThread.Start();
263
Ben Murdochb0fe1622011-05-05 13:52:32 +0100264 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 jsThread.WaitForRunning();
266 jsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100267 CHECK(sampler->WaitForTick());
268 CHECK(sampler->WasSampleStackCalled());
269 sampler->Reset();
270 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 nonJsThread.WaitForRunning();
272 nonJsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100273 CHECK(!sampler->WaitForTick());
274 CHECK(!sampler->WasSampleStackCalled());
275 sampler->Stop();
Steve Blocka7e24c12009-10-30 11:49:00 +0000276
277 jsThread.Stop();
278 nonJsThread.Stop();
279 jsThread.Join();
280 nonJsThread.Join();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100281
282 delete sampler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000283}
284
285#endif // __linux__
286
287
Steve Block3ce2e202009-11-05 08:53:23 +0000288// Test for issue http://crbug.com/23768 in Chromium.
289// Heap can contain scripts with already disposed external sources.
290// We need to verify that LogCompiledFunctions doesn't crash on them.
291namespace {
292
293class SimpleExternalString : public v8::String::ExternalStringResource {
294 public:
295 explicit SimpleExternalString(const char* source)
Steve Blockd0582a62009-12-15 09:54:21 +0000296 : utf_source_(StrLength(source)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000297 for (int i = 0; i < utf_source_.length(); ++i)
298 utf_source_[i] = source[i];
299 }
300 virtual ~SimpleExternalString() {}
301 virtual size_t length() const { return utf_source_.length(); }
302 virtual const uint16_t* data() const { return utf_source_.start(); }
303 private:
304 i::ScopedVector<uint16_t> utf_source_;
305};
306
307} // namespace
308
309TEST(Issue23768) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310 v8::HandleScope scope(CcTest::isolate());
311 v8::Handle<v8::Context> env = v8::Context::New(CcTest::isolate());
Steve Block3ce2e202009-11-05 08:53:23 +0000312 env->Enter();
313
314 SimpleExternalString source_ext_str("(function ext() {})();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000315 v8::Local<v8::String> source =
316 v8::String::NewExternal(CcTest::isolate(), &source_ext_str);
Steve Block3ce2e202009-11-05 08:53:23 +0000317 // Script needs to have a name in order to trigger InitLineEnds execution.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318 v8::Handle<v8::String> origin =
319 v8::String::NewFromUtf8(CcTest::isolate(), "issue-23768-test");
320 v8::Handle<v8::Script> evil_script = CompileWithOrigin(source, origin);
Steve Block3ce2e202009-11-05 08:53:23 +0000321 CHECK(!evil_script.IsEmpty());
322 CHECK(!evil_script->Run().IsEmpty());
323 i::Handle<i::ExternalTwoByteString> i_source(
324 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
325 // This situation can happen if source was an external string disposed
326 // by its owner.
327 i_source->set_resource(NULL);
328
329 // Must not crash.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000330 CcTest::i_isolate()->logger()->LogCompiledFunctions();
Steve Block3ce2e202009-11-05 08:53:23 +0000331}
332
333
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334static void ObjMethod1(const v8::FunctionCallbackInfo<v8::Value>& args) {
Steve Blockd0582a62009-12-15 09:54:21 +0000335}
336
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337
Steve Blockd0582a62009-12-15 09:54:21 +0000338TEST(LogCallbacks) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000339 v8::Isolate* isolate;
340 {
341 ScopedLoggerInitializer initialize_logger;
342 isolate = initialize_logger.isolate();
343 Logger* logger = initialize_logger.logger();
Steve Blockd0582a62009-12-15 09:54:21 +0000344
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 v8::Local<v8::FunctionTemplate> obj = v8::Local<v8::FunctionTemplate>::New(
346 isolate, v8::FunctionTemplate::New(isolate));
347 obj->SetClassName(v8_str("Obj"));
348 v8::Handle<v8::ObjectTemplate> proto = obj->PrototypeTemplate();
349 v8::Local<v8::Signature> signature = v8::Signature::New(isolate, obj);
350 proto->Set(v8_str("method1"),
351 v8::FunctionTemplate::New(isolate, ObjMethod1,
352 v8::Handle<v8::Value>(), signature),
353 static_cast<v8::PropertyAttribute>(v8::DontDelete));
Steve Blockd0582a62009-12-15 09:54:21 +0000354
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355 initialize_logger.env()->Global()->Set(v8_str("Obj"), obj->GetFunction());
356 CompileRun("Obj.prototype.method1.toString();");
Steve Blockd0582a62009-12-15 09:54:21 +0000357
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000358 logger->LogCompiledFunctions();
Steve Blockd0582a62009-12-15 09:54:21 +0000359
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360 bool exists = false;
361 i::Vector<const char> log(
362 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
363 CHECK(exists);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000364
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000365 i::EmbeddedVector<char, 100> ref_data;
366 i::SNPrintF(ref_data,
367 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"method1\"",
368 reinterpret_cast<intptr_t>(ObjMethod1));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000369
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000370 CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length()));
371 log.Dispose();
372 }
373 isolate->Dispose();
Steve Blockd0582a62009-12-15 09:54:21 +0000374}
375
376
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000377static void Prop1Getter(v8::Local<v8::String> property,
378 const v8::PropertyCallbackInfo<v8::Value>& info) {
Steve Blockd0582a62009-12-15 09:54:21 +0000379}
380
381static void Prop1Setter(v8::Local<v8::String> property,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000382 v8::Local<v8::Value> value,
383 const v8::PropertyCallbackInfo<void>& info) {
Steve Blockd0582a62009-12-15 09:54:21 +0000384}
385
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386static void Prop2Getter(v8::Local<v8::String> property,
387 const v8::PropertyCallbackInfo<v8::Value>& info) {
Steve Blockd0582a62009-12-15 09:54:21 +0000388}
389
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390
Steve Blockd0582a62009-12-15 09:54:21 +0000391TEST(LogAccessorCallbacks) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000392 v8::Isolate* isolate;
393 {
394 ScopedLoggerInitializer initialize_logger;
395 isolate = initialize_logger.isolate();
396 Logger* logger = initialize_logger.logger();
Steve Blockd0582a62009-12-15 09:54:21 +0000397
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398 v8::Local<v8::FunctionTemplate> obj = v8::Local<v8::FunctionTemplate>::New(
399 isolate, v8::FunctionTemplate::New(isolate));
400 obj->SetClassName(v8_str("Obj"));
401 v8::Handle<v8::ObjectTemplate> inst = obj->InstanceTemplate();
402 inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter);
403 inst->SetAccessor(v8_str("prop2"), Prop2Getter);
Steve Blockd0582a62009-12-15 09:54:21 +0000404
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000405 logger->LogAccessorCallbacks();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000406
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000407 bool exists = false;
408 i::Vector<const char> log(
409 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
410 CHECK(exists);
Steve Blockd0582a62009-12-15 09:54:21 +0000411
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412 EmbeddedVector<char, 100> prop1_getter_record;
413 i::SNPrintF(prop1_getter_record,
414 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop1\"",
415 reinterpret_cast<intptr_t>(Prop1Getter));
416 CHECK_NE(NULL,
417 StrNStr(log.start(), prop1_getter_record.start(), log.length()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000418
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000419 EmbeddedVector<char, 100> prop1_setter_record;
420 i::SNPrintF(prop1_setter_record,
421 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"set prop1\"",
422 reinterpret_cast<intptr_t>(Prop1Setter));
423 CHECK_NE(NULL,
424 StrNStr(log.start(), prop1_setter_record.start(), log.length()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000425
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000426 EmbeddedVector<char, 100> prop2_getter_record;
427 i::SNPrintF(prop2_getter_record,
428 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop2\"",
429 reinterpret_cast<intptr_t>(Prop2Getter));
430 CHECK_NE(NULL,
431 StrNStr(log.start(), prop2_getter_record.start(), log.length()));
432 log.Dispose();
433 }
434 isolate->Dispose();
Steve Block6ded16b2010-05-10 14:33:55 +0100435}
436
437
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000438typedef i::NativesCollection<i::TEST> TestSources;
Steve Blocka7e24c12009-10-30 11:49:00 +0000439
Ben Murdoch589d6972011-11-30 16:04:58 +0000440
441// Test that logging of code create / move events is equivalent to traversal of
442// a resulting heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000443TEST(EquivalenceOfLoggingAndTraversal) {
444 // This test needs to be run on a "clean" V8 to ensure that snapshot log
445 // is loaded. This is always true when running using tools/test.py because
446 // it launches a new cctest instance for every test. To be sure that launching
447 // cctest manually also works, please be sure that no tests below
448 // are using V8.
Steve Blocka7e24c12009-10-30 11:49:00 +0000449
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000450 // Start with profiling to capture all code events from the beginning.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000451 v8::Isolate* isolate;
452 {
453 ScopedLoggerInitializer initialize_logger;
454 isolate = initialize_logger.isolate();
455 Logger* logger = initialize_logger.logger();
Steve Blocka7e24c12009-10-30 11:49:00 +0000456
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000457 // Compile and run a function that creates other functions.
458 CompileRun(
459 "(function f(obj) {\n"
460 " obj.test =\n"
461 " (function a(j) { return function b() { return j; } })(100);\n"
462 "})(this);");
463 logger->StopProfiler();
464 reinterpret_cast<i::Isolate*>(isolate)->heap()->CollectAllGarbage(
465 i::Heap::kMakeHeapIterableMask);
466 logger->StringEvent("test-logging-done", "");
Steve Blocka7e24c12009-10-30 11:49:00 +0000467
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000468 // Iterate heap to find compiled functions, will write to log.
469 logger->LogCompiledFunctions();
470 logger->StringEvent("test-traversal-done", "");
Steve Blocka7e24c12009-10-30 11:49:00 +0000471
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 bool exists = false;
473 i::Vector<const char> log(
474 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
475 CHECK(exists);
476 v8::Handle<v8::String> log_str = v8::String::NewFromUtf8(
477 isolate, log.start(), v8::String::kNormalString, log.length());
478 initialize_logger.env()->Global()->Set(v8_str("_log"), log_str);
Steve Blocka7e24c12009-10-30 11:49:00 +0000479
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480 i::Vector<const unsigned char> source = TestSources::GetScriptsSource();
481 v8::Handle<v8::String> source_str = v8::String::NewFromUtf8(
482 isolate, reinterpret_cast<const char*>(source.start()),
483 v8::String::kNormalString, source.length());
484 v8::TryCatch try_catch;
485 v8::Handle<v8::Script> script = CompileWithOrigin(source_str, "");
486 if (script.IsEmpty()) {
487 v8::String::Utf8Value exception(try_catch.Exception());
488 printf("compile: %s\n", *exception);
489 CHECK(false);
490 }
491 v8::Handle<v8::Value> result = script->Run();
492 if (result.IsEmpty()) {
493 v8::String::Utf8Value exception(try_catch.Exception());
494 printf("run: %s\n", *exception);
495 CHECK(false);
496 }
497 // The result either be a "true" literal or problem description.
498 if (!result->IsTrue()) {
499 v8::Local<v8::String> s = result->ToString();
500 i::ScopedVector<char> data(s->Utf8Length() + 1);
501 CHECK_NE(NULL, data.start());
502 s->WriteUtf8(data.start());
503 printf("%s\n", data.start());
504 // Make sure that our output is written prior crash due to CHECK failure.
505 fflush(stdout);
506 CHECK(false);
507 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000508 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000509 isolate->Dispose();
Steve Blocka7e24c12009-10-30 11:49:00 +0000510}