blob: eee3e1341a8a55c59220813d139ddf86daafaac7 [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"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040045#include "src/version.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046#include "src/vm-state-inl.h"
47#include "test/cctest/cctest.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000048
49using v8::internal::Address;
50using v8::internal::EmbeddedVector;
51using v8::internal::Logger;
Steve Blockd0582a62009-12-15 09:54:21 +000052using v8::internal::StrLength;
Steve Blocka7e24c12009-10-30 11:49:00 +000053
Andrei Popescu402d9372010-02-26 13:31:12 +000054namespace {
55
Ben Murdoch589d6972011-11-30 16:04:58 +000056
Andrei Popescu402d9372010-02-26 13:31:12 +000057class ScopedLoggerInitializer {
58 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 ScopedLoggerInitializer()
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000060 : saved_log_(i::FLAG_log),
Andrei Popescu402d9372010-02-26 13:31:12 +000061 saved_prof_(i::FLAG_prof),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000062 temp_file_(NULL),
63 // Need to run this prior to creating the scope.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 trick_to_run_init_flags_(init_flags_()),
65 isolate_(v8::Isolate::New()),
66 isolate_scope_(isolate_),
67 scope_(isolate_),
68 env_(v8::Context::New(isolate_)),
69 logger_(reinterpret_cast<i::Isolate*>(isolate_)->logger()) {
Andrei Popescu402d9372010-02-26 13:31:12 +000070 env_->Enter();
71 }
72
73 ~ScopedLoggerInitializer() {
74 env_->Exit();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075 logger_->TearDown();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000076 if (temp_file_ != NULL) fclose(temp_file_);
Andrei Popescu402d9372010-02-26 13:31:12 +000077 i::FLAG_prof = saved_prof_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000078 i::FLAG_log = saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +000079 }
80
81 v8::Handle<v8::Context>& env() { return env_; }
82
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083 v8::Isolate* isolate() { return isolate_; }
84
85 Logger* logger() { return logger_; }
86
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000087 FILE* StopLoggingGetTempFile() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 temp_file_ = logger_->TearDown();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000089 CHECK_NE(NULL, temp_file_);
90 fflush(temp_file_);
91 rewind(temp_file_);
92 return temp_file_;
93 }
94
Andrei Popescu402d9372010-02-26 13:31:12 +000095 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 static bool init_flags_() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000097 i::FLAG_log = true;
Andrei Popescu402d9372010-02-26 13:31:12 +000098 i::FLAG_prof = true;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000099 i::FLAG_logfile = i::Log::kLogToTemporaryFile;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 i::FLAG_logfile_per_isolate = false;
101 return false;
Andrei Popescu402d9372010-02-26 13:31:12 +0000102 }
103
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000104 const bool saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000105 const bool saved_prof_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000106 FILE* temp_file_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000107 const bool trick_to_run_init_flags_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108 v8::Isolate* isolate_;
109 v8::Isolate::Scope isolate_scope_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000110 v8::HandleScope scope_;
111 v8::Handle<v8::Context> env_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 Logger* logger_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000113
114 DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer);
115};
116
Andrei Popescu402d9372010-02-26 13:31:12 +0000117} // namespace
118
119
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000120static const char* StrNStr(const char* s1, const char* s2, int n) {
121 if (s1[n] == '\0') return strstr(s1, s2);
122 i::ScopedVector<char> str(n + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 i::StrNCpy(str, s1, static_cast<size_t>(n));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000124 str[n] = '\0';
125 char* found = strstr(str.start(), s2);
126 return found != NULL ? s1 + (found - str.start()) : NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000127}
128
129
John Reck59135872010-11-02 12:39:01 -0700130// BUG(913). Need to implement support for profiling multiple VM threads.
131#if 0
Steve Blocka7e24c12009-10-30 11:49:00 +0000132
133namespace {
134
135class LoopingThread : public v8::internal::Thread {
136 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100137 explicit LoopingThread(v8::internal::Isolate* isolate)
138 : v8::internal::Thread(isolate),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139 semaphore_(new v8::internal::Semaphore(0)),
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 run_(true) {
141 }
142
143 virtual ~LoopingThread() { delete semaphore_; }
144
145 void Run() {
146 self_ = pthread_self();
147 RunLoop();
148 }
149
150 void SendSigProf() { pthread_kill(self_, SIGPROF); }
151
152 void Stop() { run_ = false; }
153
154 bool WaitForRunning() { return semaphore_->Wait(1000000); }
155
156 protected:
157 bool IsRunning() { return run_; }
158
159 virtual void RunLoop() = 0;
160
161 void SetV8ThreadId() {
162 v8_thread_id_ = v8::V8::GetCurrentThreadId();
163 }
164
165 void SignalRunning() { semaphore_->Signal(); }
166
167 private:
168 v8::internal::Semaphore* semaphore_;
169 bool run_;
170 pthread_t self_;
171 int v8_thread_id_;
172};
173
174
175class LoopingJsThread : public LoopingThread {
176 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100177 explicit LoopingJsThread(v8::internal::Isolate* isolate)
178 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 void RunLoop() {
Steve Block6ded16b2010-05-10 14:33:55 +0100180 v8::Locker locker;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 CHECK(CcTest::i_isolate() != NULL);
182 CHECK_GT(CcTest::i_isolate()->thread_manager()->CurrentId(), 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100183 SetV8ThreadId();
Steve Blocka7e24c12009-10-30 11:49:00 +0000184 while (IsRunning()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000185 v8::HandleScope scope;
186 v8::Persistent<v8::Context> context = v8::Context::New();
Steve Block6ded16b2010-05-10 14:33:55 +0100187 CHECK(!context.IsEmpty());
188 {
189 v8::Context::Scope context_scope(context);
190 SignalRunning();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000191 CompileRun(
Steve Block6ded16b2010-05-10 14:33:55 +0100192 "var j; for (var i=0; i<10000; ++i) { j = Math.sin(i); }");
193 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 context.Dispose();
195 i::OS::Sleep(1);
196 }
197 }
198};
199
200
201class LoopingNonJsThread : public LoopingThread {
202 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100203 explicit LoopingNonJsThread(v8::internal::Isolate* isolate)
204 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 void RunLoop() {
206 v8::Locker locker;
207 v8::Unlocker unlocker;
208 // Now thread has V8's id, but will not run VM code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000209 CHECK(CcTest::i_isolate() != NULL);
210 CHECK_GT(CcTest::i_isolate()->thread_manager()->CurrentId(), 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000211 double i = 10;
212 SignalRunning();
213 while (IsRunning()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 i = std::sin(i);
Steve Blocka7e24c12009-10-30 11:49:00 +0000215 i::OS::Sleep(1);
216 }
217 }
218};
219
220
221class TestSampler : public v8::internal::Sampler {
222 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100223 explicit TestSampler(v8::internal::Isolate* isolate)
224 : Sampler(isolate, 0, true, true),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000225 semaphore_(new v8::internal::Semaphore(0)),
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 was_sample_stack_called_(false) {
227 }
228
229 ~TestSampler() { delete semaphore_; }
230
231 void SampleStack(v8::internal::TickSample*) {
232 was_sample_stack_called_ = true;
233 }
234
235 void Tick(v8::internal::TickSample*) { semaphore_->Signal(); }
236
237 bool WaitForTick() { return semaphore_->Wait(1000000); }
238
239 void Reset() { was_sample_stack_called_ = false; }
240
241 bool WasSampleStackCalled() { return was_sample_stack_called_; }
242
243 private:
244 v8::internal::Semaphore* semaphore_;
245 bool was_sample_stack_called_;
246};
247
248
249} // namespace
250
251TEST(ProfMultipleThreads) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100252 TestSampler* sampler = NULL;
253 {
254 v8::Locker locker;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 sampler = new TestSampler(CcTest::i_isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100256 sampler->Start();
257 CHECK(sampler->IsActive());
258 }
259
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 LoopingJsThread jsThread(CcTest::i_isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 jsThread.Start();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000262 LoopingNonJsThread nonJsThread(CcTest::i_isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 nonJsThread.Start();
264
Ben Murdochb0fe1622011-05-05 13:52:32 +0100265 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000266 jsThread.WaitForRunning();
267 jsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100268 CHECK(sampler->WaitForTick());
269 CHECK(sampler->WasSampleStackCalled());
270 sampler->Reset();
271 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000272 nonJsThread.WaitForRunning();
273 nonJsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100274 CHECK(!sampler->WaitForTick());
275 CHECK(!sampler->WasSampleStackCalled());
276 sampler->Stop();
Steve Blocka7e24c12009-10-30 11:49:00 +0000277
278 jsThread.Stop();
279 nonJsThread.Stop();
280 jsThread.Join();
281 nonJsThread.Join();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100282
283 delete sampler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000284}
285
286#endif // __linux__
287
288
Steve Block3ce2e202009-11-05 08:53:23 +0000289// Test for issue http://crbug.com/23768 in Chromium.
290// Heap can contain scripts with already disposed external sources.
291// We need to verify that LogCompiledFunctions doesn't crash on them.
292namespace {
293
294class SimpleExternalString : public v8::String::ExternalStringResource {
295 public:
296 explicit SimpleExternalString(const char* source)
Steve Blockd0582a62009-12-15 09:54:21 +0000297 : utf_source_(StrLength(source)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000298 for (int i = 0; i < utf_source_.length(); ++i)
299 utf_source_[i] = source[i];
300 }
301 virtual ~SimpleExternalString() {}
302 virtual size_t length() const { return utf_source_.length(); }
303 virtual const uint16_t* data() const { return utf_source_.start(); }
304 private:
305 i::ScopedVector<uint16_t> utf_source_;
306};
307
308} // namespace
309
310TEST(Issue23768) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311 v8::HandleScope scope(CcTest::isolate());
312 v8::Handle<v8::Context> env = v8::Context::New(CcTest::isolate());
Steve Block3ce2e202009-11-05 08:53:23 +0000313 env->Enter();
314
315 SimpleExternalString source_ext_str("(function ext() {})();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000316 v8::Local<v8::String> source =
317 v8::String::NewExternal(CcTest::isolate(), &source_ext_str);
Steve Block3ce2e202009-11-05 08:53:23 +0000318 // Script needs to have a name in order to trigger InitLineEnds execution.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000319 v8::Handle<v8::String> origin =
320 v8::String::NewFromUtf8(CcTest::isolate(), "issue-23768-test");
321 v8::Handle<v8::Script> evil_script = CompileWithOrigin(source, origin);
Steve Block3ce2e202009-11-05 08:53:23 +0000322 CHECK(!evil_script.IsEmpty());
323 CHECK(!evil_script->Run().IsEmpty());
324 i::Handle<i::ExternalTwoByteString> i_source(
325 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
326 // This situation can happen if source was an external string disposed
327 // by its owner.
328 i_source->set_resource(NULL);
329
330 // Must not crash.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000331 CcTest::i_isolate()->logger()->LogCompiledFunctions();
Steve Block3ce2e202009-11-05 08:53:23 +0000332}
333
334
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335static void ObjMethod1(const v8::FunctionCallbackInfo<v8::Value>& args) {
Steve Blockd0582a62009-12-15 09:54:21 +0000336}
337
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000338
Steve Blockd0582a62009-12-15 09:54:21 +0000339TEST(LogCallbacks) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000340 v8::Isolate* isolate;
341 {
342 ScopedLoggerInitializer initialize_logger;
343 isolate = initialize_logger.isolate();
344 Logger* logger = initialize_logger.logger();
Steve Blockd0582a62009-12-15 09:54:21 +0000345
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000346 v8::Local<v8::FunctionTemplate> obj = v8::Local<v8::FunctionTemplate>::New(
347 isolate, v8::FunctionTemplate::New(isolate));
348 obj->SetClassName(v8_str("Obj"));
349 v8::Handle<v8::ObjectTemplate> proto = obj->PrototypeTemplate();
350 v8::Local<v8::Signature> signature = v8::Signature::New(isolate, obj);
351 proto->Set(v8_str("method1"),
352 v8::FunctionTemplate::New(isolate, ObjMethod1,
353 v8::Handle<v8::Value>(), signature),
354 static_cast<v8::PropertyAttribute>(v8::DontDelete));
Steve Blockd0582a62009-12-15 09:54:21 +0000355
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356 initialize_logger.env()->Global()->Set(v8_str("Obj"), obj->GetFunction());
357 CompileRun("Obj.prototype.method1.toString();");
Steve Blockd0582a62009-12-15 09:54:21 +0000358
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000359 logger->LogCompiledFunctions();
Steve Blockd0582a62009-12-15 09:54:21 +0000360
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000361 bool exists = false;
362 i::Vector<const char> log(
363 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
364 CHECK(exists);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000365
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000366 i::EmbeddedVector<char, 100> ref_data;
367 i::SNPrintF(ref_data,
368 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"method1\"",
369 reinterpret_cast<intptr_t>(ObjMethod1));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000370
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371 CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length()));
372 log.Dispose();
373 }
374 isolate->Dispose();
Steve Blockd0582a62009-12-15 09:54:21 +0000375}
376
377
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000378static void Prop1Getter(v8::Local<v8::String> property,
379 const v8::PropertyCallbackInfo<v8::Value>& info) {
Steve Blockd0582a62009-12-15 09:54:21 +0000380}
381
382static void Prop1Setter(v8::Local<v8::String> property,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383 v8::Local<v8::Value> value,
384 const v8::PropertyCallbackInfo<void>& info) {
Steve Blockd0582a62009-12-15 09:54:21 +0000385}
386
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387static void Prop2Getter(v8::Local<v8::String> property,
388 const v8::PropertyCallbackInfo<v8::Value>& info) {
Steve Blockd0582a62009-12-15 09:54:21 +0000389}
390
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000391
Steve Blockd0582a62009-12-15 09:54:21 +0000392TEST(LogAccessorCallbacks) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000393 v8::Isolate* isolate;
394 {
395 ScopedLoggerInitializer initialize_logger;
396 isolate = initialize_logger.isolate();
397 Logger* logger = initialize_logger.logger();
Steve Blockd0582a62009-12-15 09:54:21 +0000398
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399 v8::Local<v8::FunctionTemplate> obj = v8::Local<v8::FunctionTemplate>::New(
400 isolate, v8::FunctionTemplate::New(isolate));
401 obj->SetClassName(v8_str("Obj"));
402 v8::Handle<v8::ObjectTemplate> inst = obj->InstanceTemplate();
403 inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter);
404 inst->SetAccessor(v8_str("prop2"), Prop2Getter);
Steve Blockd0582a62009-12-15 09:54:21 +0000405
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000406 logger->LogAccessorCallbacks();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000407
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000408 bool exists = false;
409 i::Vector<const char> log(
410 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
411 CHECK(exists);
Steve Blockd0582a62009-12-15 09:54:21 +0000412
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000413 EmbeddedVector<char, 100> prop1_getter_record;
414 i::SNPrintF(prop1_getter_record,
415 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop1\"",
416 reinterpret_cast<intptr_t>(Prop1Getter));
417 CHECK_NE(NULL,
418 StrNStr(log.start(), prop1_getter_record.start(), log.length()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000419
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000420 EmbeddedVector<char, 100> prop1_setter_record;
421 i::SNPrintF(prop1_setter_record,
422 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"set prop1\"",
423 reinterpret_cast<intptr_t>(Prop1Setter));
424 CHECK_NE(NULL,
425 StrNStr(log.start(), prop1_setter_record.start(), log.length()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000426
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 EmbeddedVector<char, 100> prop2_getter_record;
428 i::SNPrintF(prop2_getter_record,
429 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop2\"",
430 reinterpret_cast<intptr_t>(Prop2Getter));
431 CHECK_NE(NULL,
432 StrNStr(log.start(), prop2_getter_record.start(), log.length()));
433 log.Dispose();
434 }
435 isolate->Dispose();
Steve Block6ded16b2010-05-10 14:33:55 +0100436}
437
438
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000439typedef i::NativesCollection<i::TEST> TestSources;
Steve Blocka7e24c12009-10-30 11:49:00 +0000440
Ben Murdoch589d6972011-11-30 16:04:58 +0000441
442// Test that logging of code create / move events is equivalent to traversal of
443// a resulting heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000444TEST(EquivalenceOfLoggingAndTraversal) {
445 // This test needs to be run on a "clean" V8 to ensure that snapshot log
446 // is loaded. This is always true when running using tools/test.py because
447 // it launches a new cctest instance for every test. To be sure that launching
448 // cctest manually also works, please be sure that no tests below
449 // are using V8.
Steve Blocka7e24c12009-10-30 11:49:00 +0000450
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000451 // Start with profiling to capture all code events from the beginning.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 v8::Isolate* isolate;
453 {
454 ScopedLoggerInitializer initialize_logger;
455 isolate = initialize_logger.isolate();
456 Logger* logger = initialize_logger.logger();
Steve Blocka7e24c12009-10-30 11:49:00 +0000457
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458 // Compile and run a function that creates other functions.
459 CompileRun(
460 "(function f(obj) {\n"
461 " obj.test =\n"
462 " (function a(j) { return function b() { return j; } })(100);\n"
463 "})(this);");
464 logger->StopProfiler();
465 reinterpret_cast<i::Isolate*>(isolate)->heap()->CollectAllGarbage(
466 i::Heap::kMakeHeapIterableMask);
467 logger->StringEvent("test-logging-done", "");
Steve Blocka7e24c12009-10-30 11:49:00 +0000468
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469 // Iterate heap to find compiled functions, will write to log.
470 logger->LogCompiledFunctions();
471 logger->StringEvent("test-traversal-done", "");
Steve Blocka7e24c12009-10-30 11:49:00 +0000472
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000473 bool exists = false;
474 i::Vector<const char> log(
475 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
476 CHECK(exists);
477 v8::Handle<v8::String> log_str = v8::String::NewFromUtf8(
478 isolate, log.start(), v8::String::kNormalString, log.length());
479 initialize_logger.env()->Global()->Set(v8_str("_log"), log_str);
Steve Blocka7e24c12009-10-30 11:49:00 +0000480
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400481 i::Vector<const char> source = TestSources::GetScriptsSource();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000482 v8::Handle<v8::String> source_str = v8::String::NewFromUtf8(
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400483 isolate, source.start(), v8::String::kNormalString, source.length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000484 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()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400499 v8::Local<v8::String> s = result->ToString(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500 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}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400511
512
513TEST(LogVersion) {
514 v8::Isolate* isolate;
515 {
516 ScopedLoggerInitializer initialize_logger;
517 isolate = initialize_logger.isolate();
518 bool exists = false;
519 i::Vector<const char> log(
520 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
521 CHECK(exists);
522 i::EmbeddedVector<char, 100> ref_data;
523 i::SNPrintF(ref_data, "v8-version,%d,%d,%d,%d,%d", i::Version::GetMajor(),
524 i::Version::GetMinor(), i::Version::GetBuild(),
525 i::Version::GetPatch(), i::Version::IsCandidate());
526 CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length()));
527 log.Dispose();
528 }
529 isolate->Dispose();
530}