1 // Copyright 2006-2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
28 // Tests of logging functions from log.h
39 #include "src/cpu-profiler.h"
40 #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"
48 using v8::internal::Address;
49 using v8::internal::EmbeddedVector;
50 using v8::internal::Logger;
51 using v8::internal::StrLength;
56 class ScopedLoggerInitializer {
58 ScopedLoggerInitializer()
59 : saved_log_(i::FLAG_log),
60 saved_prof_(i::FLAG_prof),
62 // Need to run this prior to creating the scope.
63 trick_to_run_init_flags_(init_flags_()),
64 scope_(CcTest::isolate()),
65 env_(v8::Context::New(CcTest::isolate())),
66 logger_(CcTest::i_isolate()->logger()) {
70 ~ScopedLoggerInitializer() {
73 if (temp_file_ != NULL) fclose(temp_file_);
74 i::FLAG_prof = saved_prof_;
75 i::FLAG_log = saved_log_;
78 v8::Handle<v8::Context>& env() { return env_; }
80 Logger* logger() { return logger_; }
82 FILE* StopLoggingGetTempFile() {
83 temp_file_ = logger_->TearDown();
84 CHECK_NE(NULL, temp_file_);
91 static bool init_flags_() {
94 i::FLAG_logfile = i::Log::kLogToTemporaryFile;
95 i::FLAG_logfile_per_isolate = false;
99 const bool saved_log_;
100 const bool saved_prof_;
102 const bool trick_to_run_init_flags_;
103 v8::HandleScope scope_;
104 v8::Handle<v8::Context> env_;
107 DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer);
113 static const char* StrNStr(const char* s1, const char* s2, int n) {
114 if (s1[n] == '\0') return strstr(s1, s2);
115 i::ScopedVector<char> str(n + 1);
116 i::StrNCpy(str, s1, static_cast<size_t>(n));
118 char* found = strstr(str.start(), s2);
119 return found != NULL ? s1 + (found - str.start()) : NULL;
123 // BUG(913). Need to implement support for profiling multiple VM threads.
128 class LoopingThread : public v8::internal::Thread {
130 explicit LoopingThread(v8::internal::Isolate* isolate)
131 : v8::internal::Thread(isolate),
132 semaphore_(new v8::internal::Semaphore(0)),
136 virtual ~LoopingThread() { delete semaphore_; }
139 self_ = pthread_self();
143 void SendSigProf() { pthread_kill(self_, SIGPROF); }
145 void Stop() { run_ = false; }
147 bool WaitForRunning() { return semaphore_->Wait(1000000); }
150 bool IsRunning() { return run_; }
152 virtual void RunLoop() = 0;
154 void SetV8ThreadId() {
155 v8_thread_id_ = v8::V8::GetCurrentThreadId();
158 void SignalRunning() { semaphore_->Signal(); }
161 v8::internal::Semaphore* semaphore_;
168 class LoopingJsThread : public LoopingThread {
170 explicit LoopingJsThread(v8::internal::Isolate* isolate)
171 : LoopingThread(isolate) { }
174 CHECK(CcTest::i_isolate() != NULL);
175 CHECK_GT(CcTest::i_isolate()->thread_manager()->CurrentId(), 0);
177 while (IsRunning()) {
178 v8::HandleScope scope;
179 v8::Persistent<v8::Context> context = v8::Context::New();
180 CHECK(!context.IsEmpty());
182 v8::Context::Scope context_scope(context);
185 "var j; for (var i=0; i<10000; ++i) { j = Math.sin(i); }");
194 class LoopingNonJsThread : public LoopingThread {
196 explicit LoopingNonJsThread(v8::internal::Isolate* isolate)
197 : LoopingThread(isolate) { }
200 v8::Unlocker unlocker;
201 // Now thread has V8's id, but will not run VM code.
202 CHECK(CcTest::i_isolate() != NULL);
203 CHECK_GT(CcTest::i_isolate()->thread_manager()->CurrentId(), 0);
206 while (IsRunning()) {
214 class TestSampler : public v8::internal::Sampler {
216 explicit TestSampler(v8::internal::Isolate* isolate)
217 : Sampler(isolate, 0, true, true),
218 semaphore_(new v8::internal::Semaphore(0)),
219 was_sample_stack_called_(false) {
222 ~TestSampler() { delete semaphore_; }
224 void SampleStack(v8::internal::TickSample*) {
225 was_sample_stack_called_ = true;
228 void Tick(v8::internal::TickSample*) { semaphore_->Signal(); }
230 bool WaitForTick() { return semaphore_->Wait(1000000); }
232 void Reset() { was_sample_stack_called_ = false; }
234 bool WasSampleStackCalled() { return was_sample_stack_called_; }
237 v8::internal::Semaphore* semaphore_;
238 bool was_sample_stack_called_;
244 TEST(ProfMultipleThreads) {
245 TestSampler* sampler = NULL;
248 sampler = new TestSampler(CcTest::i_isolate());
250 CHECK(sampler->IsActive());
253 LoopingJsThread jsThread(CcTest::i_isolate());
255 LoopingNonJsThread nonJsThread(CcTest::i_isolate());
258 CHECK(!sampler->WasSampleStackCalled());
259 jsThread.WaitForRunning();
260 jsThread.SendSigProf();
261 CHECK(sampler->WaitForTick());
262 CHECK(sampler->WasSampleStackCalled());
264 CHECK(!sampler->WasSampleStackCalled());
265 nonJsThread.WaitForRunning();
266 nonJsThread.SendSigProf();
267 CHECK(!sampler->WaitForTick());
268 CHECK(!sampler->WasSampleStackCalled());
282 // Test for issue http://crbug.com/23768 in Chromium.
283 // Heap can contain scripts with already disposed external sources.
284 // We need to verify that LogCompiledFunctions doesn't crash on them.
287 class SimpleExternalString : public v8::String::ExternalStringResource {
289 explicit SimpleExternalString(const char* source)
290 : utf_source_(StrLength(source)) {
291 for (int i = 0; i < utf_source_.length(); ++i)
292 utf_source_[i] = source[i];
294 virtual ~SimpleExternalString() {}
295 virtual size_t length() const { return utf_source_.length(); }
296 virtual const uint16_t* data() const { return utf_source_.start(); }
298 i::ScopedVector<uint16_t> utf_source_;
304 v8::HandleScope scope(CcTest::isolate());
305 v8::Handle<v8::Context> env = v8::Context::New(CcTest::isolate());
308 SimpleExternalString source_ext_str("(function ext() {})();");
309 v8::Local<v8::String> source =
310 v8::String::NewExternal(CcTest::isolate(), &source_ext_str);
311 // Script needs to have a name in order to trigger InitLineEnds execution.
312 v8::Handle<v8::String> origin =
313 v8::String::NewFromUtf8(CcTest::isolate(), "issue-23768-test");
314 v8::Handle<v8::Script> evil_script = CompileWithOrigin(source, origin);
315 CHECK(!evil_script.IsEmpty());
316 CHECK(!evil_script->Run().IsEmpty());
317 i::Handle<i::ExternalTwoByteString> i_source(
318 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
319 // This situation can happen if source was an external string disposed
321 i_source->set_resource(NULL);
324 CcTest::i_isolate()->logger()->LogCompiledFunctions();
328 static void ObjMethod1(const v8::FunctionCallbackInfo<v8::Value>& args) {
333 v8::Isolate* isolate = CcTest::isolate();
334 ScopedLoggerInitializer initialize_logger;
335 Logger* logger = initialize_logger.logger();
337 v8::Local<v8::FunctionTemplate> obj =
338 v8::Local<v8::FunctionTemplate>::New(isolate,
339 v8::FunctionTemplate::New(isolate));
340 obj->SetClassName(v8_str("Obj"));
341 v8::Handle<v8::ObjectTemplate> proto = obj->PrototypeTemplate();
342 v8::Local<v8::Signature> signature =
343 v8::Signature::New(isolate, obj);
344 proto->Set(v8_str("method1"),
345 v8::FunctionTemplate::New(isolate,
347 v8::Handle<v8::Value>(),
349 static_cast<v8::PropertyAttribute>(v8::DontDelete));
351 initialize_logger.env()->Global()->Set(v8_str("Obj"), obj->GetFunction());
352 CompileRun("Obj.prototype.method1.toString();");
354 logger->LogCompiledFunctions();
357 i::Vector<const char> log(
358 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
361 i::EmbeddedVector<char, 100> ref_data;
362 i::SNPrintF(ref_data,
363 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"method1\"",
364 reinterpret_cast<intptr_t>(ObjMethod1));
366 CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length()));
371 static void Prop1Getter(v8::Local<v8::String> property,
372 const v8::PropertyCallbackInfo<v8::Value>& info) {
375 static void Prop1Setter(v8::Local<v8::String> property,
376 v8::Local<v8::Value> value,
377 const v8::PropertyCallbackInfo<void>& info) {
380 static void Prop2Getter(v8::Local<v8::String> property,
381 const v8::PropertyCallbackInfo<v8::Value>& info) {
385 TEST(LogAccessorCallbacks) {
386 v8::Isolate* isolate = CcTest::isolate();
387 ScopedLoggerInitializer initialize_logger;
388 Logger* logger = initialize_logger.logger();
390 v8::Local<v8::FunctionTemplate> obj =
391 v8::Local<v8::FunctionTemplate>::New(isolate,
392 v8::FunctionTemplate::New(isolate));
393 obj->SetClassName(v8_str("Obj"));
394 v8::Handle<v8::ObjectTemplate> inst = obj->InstanceTemplate();
395 inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter);
396 inst->SetAccessor(v8_str("prop2"), Prop2Getter);
398 logger->LogAccessorCallbacks();
401 i::Vector<const char> log(
402 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
405 EmbeddedVector<char, 100> prop1_getter_record;
406 i::SNPrintF(prop1_getter_record,
407 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop1\"",
408 reinterpret_cast<intptr_t>(Prop1Getter));
410 StrNStr(log.start(), prop1_getter_record.start(), log.length()));
412 EmbeddedVector<char, 100> prop1_setter_record;
413 i::SNPrintF(prop1_setter_record,
414 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"set prop1\"",
415 reinterpret_cast<intptr_t>(Prop1Setter));
417 StrNStr(log.start(), prop1_setter_record.start(), log.length()));
419 EmbeddedVector<char, 100> prop2_getter_record;
420 i::SNPrintF(prop2_getter_record,
421 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop2\"",
422 reinterpret_cast<intptr_t>(Prop2Getter));
424 StrNStr(log.start(), prop2_getter_record.start(), log.length()));
429 typedef i::NativesCollection<i::TEST> TestSources;
432 // Test that logging of code create / move events is equivalent to traversal of
434 TEST(EquivalenceOfLoggingAndTraversal) {
435 // This test needs to be run on a "clean" V8 to ensure that snapshot log
436 // is loaded. This is always true when running using tools/test.py because
437 // it launches a new cctest instance for every test. To be sure that launching
438 // cctest manually also works, please be sure that no tests below
441 // Start with profiling to capture all code events from the beginning.
442 ScopedLoggerInitializer initialize_logger;
443 Logger* logger = initialize_logger.logger();
445 // Compile and run a function that creates other functions.
447 "(function f(obj) {\n"
449 " (function a(j) { return function b() { return j; } })(100);\n"
451 logger->StopProfiler();
452 CcTest::heap()->CollectAllGarbage(i::Heap::kMakeHeapIterableMask);
453 logger->StringEvent("test-logging-done", "");
455 // Iterate heap to find compiled functions, will write to log.
456 logger->LogCompiledFunctions();
457 logger->StringEvent("test-traversal-done", "");
460 i::Vector<const char> log(
461 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
463 v8::Handle<v8::String> log_str = v8::String::NewFromUtf8(
464 CcTest::isolate(), log.start(), v8::String::kNormalString, log.length());
465 initialize_logger.env()->Global()->Set(v8_str("_log"), log_str);
467 i::Vector<const unsigned char> source = TestSources::GetScriptsSource();
468 v8::Handle<v8::String> source_str = v8::String::NewFromUtf8(
469 CcTest::isolate(), reinterpret_cast<const char*>(source.start()),
470 v8::String::kNormalString, source.length());
471 v8::TryCatch try_catch;
472 v8::Handle<v8::Script> script = CompileWithOrigin(source_str, "");
473 if (script.IsEmpty()) {
474 v8::String::Utf8Value exception(try_catch.Exception());
475 printf("compile: %s\n", *exception);
478 v8::Handle<v8::Value> result = script->Run();
479 if (result.IsEmpty()) {
480 v8::String::Utf8Value exception(try_catch.Exception());
481 printf("run: %s\n", *exception);
484 // The result either be a "true" literal or problem description.
485 if (!result->IsTrue()) {
486 v8::Local<v8::String> s = result->ToString();
487 i::ScopedVector<char> data(s->Utf8Length() + 1);
488 CHECK_NE(NULL, data.start());
489 s->WriteUtf8(data.start());
490 printf("%s\n", data.start());
491 // Make sure that our output is written prior crash due to CHECK failure.