OLD | NEW |
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 54 |
55 | 55 |
56 class ScopedLoggerInitializer { | 56 class ScopedLoggerInitializer { |
57 public: | 57 public: |
58 ScopedLoggerInitializer() | 58 ScopedLoggerInitializer() |
59 : saved_log_(i::FLAG_log), | 59 : saved_log_(i::FLAG_log), |
60 saved_prof_(i::FLAG_prof), | 60 saved_prof_(i::FLAG_prof), |
61 temp_file_(NULL), | 61 temp_file_(NULL), |
62 // Need to run this prior to creating the scope. | 62 // Need to run this prior to creating the scope. |
63 trick_to_run_init_flags_(init_flags_()), | 63 trick_to_run_init_flags_(init_flags_()), |
64 isolate_(v8::Isolate::New()), | 64 scope_(CcTest::isolate()), |
65 isolate_scope_(isolate_), | 65 env_(v8::Context::New(CcTest::isolate())), |
66 scope_(isolate_), | 66 logger_(CcTest::i_isolate()->logger()) { |
67 env_(v8::Context::New(isolate_)), | |
68 logger_(reinterpret_cast<i::Isolate*>(isolate_)->logger()) { | |
69 env_->Enter(); | 67 env_->Enter(); |
70 } | 68 } |
71 | 69 |
72 ~ScopedLoggerInitializer() { | 70 ~ScopedLoggerInitializer() { |
73 env_->Exit(); | 71 env_->Exit(); |
74 logger_->TearDown(); | 72 logger_->TearDown(); |
75 if (temp_file_ != NULL) fclose(temp_file_); | 73 if (temp_file_ != NULL) fclose(temp_file_); |
76 i::FLAG_prof = saved_prof_; | 74 i::FLAG_prof = saved_prof_; |
77 i::FLAG_log = saved_log_; | 75 i::FLAG_log = saved_log_; |
78 } | 76 } |
79 | 77 |
80 v8::Handle<v8::Context>& env() { return env_; } | 78 v8::Handle<v8::Context>& env() { return env_; } |
81 | 79 |
82 v8::Isolate* isolate() { return isolate_; } | |
83 | |
84 Logger* logger() { return logger_; } | 80 Logger* logger() { return logger_; } |
85 | 81 |
86 FILE* StopLoggingGetTempFile() { | 82 FILE* StopLoggingGetTempFile() { |
87 temp_file_ = logger_->TearDown(); | 83 temp_file_ = logger_->TearDown(); |
88 CHECK_NE(NULL, temp_file_); | 84 CHECK_NE(NULL, temp_file_); |
89 fflush(temp_file_); | 85 fflush(temp_file_); |
90 rewind(temp_file_); | 86 rewind(temp_file_); |
91 return temp_file_; | 87 return temp_file_; |
92 } | 88 } |
93 | 89 |
94 private: | 90 private: |
95 static bool init_flags_() { | 91 static bool init_flags_() { |
96 i::FLAG_log = true; | 92 i::FLAG_log = true; |
97 i::FLAG_prof = true; | 93 i::FLAG_prof = true; |
98 i::FLAG_logfile = i::Log::kLogToTemporaryFile; | 94 i::FLAG_logfile = i::Log::kLogToTemporaryFile; |
99 i::FLAG_logfile_per_isolate = false; | 95 i::FLAG_logfile_per_isolate = false; |
100 return false; | 96 return false; |
101 } | 97 } |
102 | 98 |
103 const bool saved_log_; | 99 const bool saved_log_; |
104 const bool saved_prof_; | 100 const bool saved_prof_; |
105 FILE* temp_file_; | 101 FILE* temp_file_; |
106 const bool trick_to_run_init_flags_; | 102 const bool trick_to_run_init_flags_; |
107 v8::Isolate* isolate_; | |
108 v8::Isolate::Scope isolate_scope_; | |
109 v8::HandleScope scope_; | 103 v8::HandleScope scope_; |
110 v8::Handle<v8::Context> env_; | 104 v8::Handle<v8::Context> env_; |
111 Logger* logger_; | 105 Logger* logger_; |
112 | 106 |
113 DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer); | 107 DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer); |
114 }; | 108 }; |
115 | 109 |
116 } // namespace | 110 } // namespace |
117 | 111 |
118 | 112 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 // Must not crash. | 323 // Must not crash. |
330 CcTest::i_isolate()->logger()->LogCompiledFunctions(); | 324 CcTest::i_isolate()->logger()->LogCompiledFunctions(); |
331 } | 325 } |
332 | 326 |
333 | 327 |
334 static void ObjMethod1(const v8::FunctionCallbackInfo<v8::Value>& args) { | 328 static void ObjMethod1(const v8::FunctionCallbackInfo<v8::Value>& args) { |
335 } | 329 } |
336 | 330 |
337 | 331 |
338 TEST(LogCallbacks) { | 332 TEST(LogCallbacks) { |
339 v8::Isolate* isolate; | 333 v8::Isolate* isolate = CcTest::isolate(); |
340 { | 334 ScopedLoggerInitializer initialize_logger; |
341 ScopedLoggerInitializer initialize_logger; | 335 Logger* logger = initialize_logger.logger(); |
342 isolate = initialize_logger.isolate(); | |
343 Logger* logger = initialize_logger.logger(); | |
344 | 336 |
345 v8::Local<v8::FunctionTemplate> obj = v8::Local<v8::FunctionTemplate>::New( | 337 v8::Local<v8::FunctionTemplate> obj = |
346 isolate, v8::FunctionTemplate::New(isolate)); | 338 v8::Local<v8::FunctionTemplate>::New(isolate, |
347 obj->SetClassName(v8_str("Obj")); | 339 v8::FunctionTemplate::New(isolate)); |
348 v8::Handle<v8::ObjectTemplate> proto = obj->PrototypeTemplate(); | 340 obj->SetClassName(v8_str("Obj")); |
349 v8::Local<v8::Signature> signature = v8::Signature::New(isolate, obj); | 341 v8::Handle<v8::ObjectTemplate> proto = obj->PrototypeTemplate(); |
350 proto->Set(v8_str("method1"), | 342 v8::Local<v8::Signature> signature = |
351 v8::FunctionTemplate::New(isolate, ObjMethod1, | 343 v8::Signature::New(isolate, obj); |
352 v8::Handle<v8::Value>(), signature), | 344 proto->Set(v8_str("method1"), |
353 static_cast<v8::PropertyAttribute>(v8::DontDelete)); | 345 v8::FunctionTemplate::New(isolate, |
| 346 ObjMethod1, |
| 347 v8::Handle<v8::Value>(), |
| 348 signature), |
| 349 static_cast<v8::PropertyAttribute>(v8::DontDelete)); |
354 | 350 |
355 initialize_logger.env()->Global()->Set(v8_str("Obj"), obj->GetFunction()); | 351 initialize_logger.env()->Global()->Set(v8_str("Obj"), obj->GetFunction()); |
356 CompileRun("Obj.prototype.method1.toString();"); | 352 CompileRun("Obj.prototype.method1.toString();"); |
357 | 353 |
358 logger->LogCompiledFunctions(); | 354 logger->LogCompiledFunctions(); |
359 | 355 |
360 bool exists = false; | 356 bool exists = false; |
361 i::Vector<const char> log( | 357 i::Vector<const char> log( |
362 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); | 358 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); |
363 CHECK(exists); | 359 CHECK(exists); |
364 | 360 |
365 i::EmbeddedVector<char, 100> ref_data; | 361 i::EmbeddedVector<char, 100> ref_data; |
366 i::SNPrintF(ref_data, | 362 i::SNPrintF(ref_data, |
367 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"method1\"", | 363 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"method1\"", |
368 reinterpret_cast<intptr_t>(ObjMethod1)); | 364 reinterpret_cast<intptr_t>(ObjMethod1)); |
369 | 365 |
370 CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length())); | 366 CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length())); |
371 log.Dispose(); | 367 log.Dispose(); |
372 } | |
373 isolate->Dispose(); | |
374 } | 368 } |
375 | 369 |
376 | 370 |
377 static void Prop1Getter(v8::Local<v8::String> property, | 371 static void Prop1Getter(v8::Local<v8::String> property, |
378 const v8::PropertyCallbackInfo<v8::Value>& info) { | 372 const v8::PropertyCallbackInfo<v8::Value>& info) { |
379 } | 373 } |
380 | 374 |
381 static void Prop1Setter(v8::Local<v8::String> property, | 375 static void Prop1Setter(v8::Local<v8::String> property, |
382 v8::Local<v8::Value> value, | 376 v8::Local<v8::Value> value, |
383 const v8::PropertyCallbackInfo<void>& info) { | 377 const v8::PropertyCallbackInfo<void>& info) { |
384 } | 378 } |
385 | 379 |
386 static void Prop2Getter(v8::Local<v8::String> property, | 380 static void Prop2Getter(v8::Local<v8::String> property, |
387 const v8::PropertyCallbackInfo<v8::Value>& info) { | 381 const v8::PropertyCallbackInfo<v8::Value>& info) { |
388 } | 382 } |
389 | 383 |
390 | 384 |
391 TEST(LogAccessorCallbacks) { | 385 TEST(LogAccessorCallbacks) { |
392 v8::Isolate* isolate; | 386 v8::Isolate* isolate = CcTest::isolate(); |
393 { | 387 ScopedLoggerInitializer initialize_logger; |
394 ScopedLoggerInitializer initialize_logger; | 388 Logger* logger = initialize_logger.logger(); |
395 isolate = initialize_logger.isolate(); | |
396 Logger* logger = initialize_logger.logger(); | |
397 | 389 |
398 v8::Local<v8::FunctionTemplate> obj = v8::Local<v8::FunctionTemplate>::New( | 390 v8::Local<v8::FunctionTemplate> obj = |
399 isolate, v8::FunctionTemplate::New(isolate)); | 391 v8::Local<v8::FunctionTemplate>::New(isolate, |
400 obj->SetClassName(v8_str("Obj")); | 392 v8::FunctionTemplate::New(isolate)); |
401 v8::Handle<v8::ObjectTemplate> inst = obj->InstanceTemplate(); | 393 obj->SetClassName(v8_str("Obj")); |
402 inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter); | 394 v8::Handle<v8::ObjectTemplate> inst = obj->InstanceTemplate(); |
403 inst->SetAccessor(v8_str("prop2"), Prop2Getter); | 395 inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter); |
| 396 inst->SetAccessor(v8_str("prop2"), Prop2Getter); |
404 | 397 |
405 logger->LogAccessorCallbacks(); | 398 logger->LogAccessorCallbacks(); |
406 | 399 |
407 bool exists = false; | 400 bool exists = false; |
408 i::Vector<const char> log( | 401 i::Vector<const char> log( |
409 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); | 402 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); |
410 CHECK(exists); | 403 CHECK(exists); |
411 | 404 |
412 EmbeddedVector<char, 100> prop1_getter_record; | 405 EmbeddedVector<char, 100> prop1_getter_record; |
413 i::SNPrintF(prop1_getter_record, | 406 i::SNPrintF(prop1_getter_record, |
414 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop1\"", | 407 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop1\"", |
415 reinterpret_cast<intptr_t>(Prop1Getter)); | 408 reinterpret_cast<intptr_t>(Prop1Getter)); |
416 CHECK_NE(NULL, | 409 CHECK_NE(NULL, |
417 StrNStr(log.start(), prop1_getter_record.start(), log.length())); | 410 StrNStr(log.start(), prop1_getter_record.start(), log.length())); |
418 | 411 |
419 EmbeddedVector<char, 100> prop1_setter_record; | 412 EmbeddedVector<char, 100> prop1_setter_record; |
420 i::SNPrintF(prop1_setter_record, | 413 i::SNPrintF(prop1_setter_record, |
421 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"set prop1\"", | 414 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"set prop1\"", |
422 reinterpret_cast<intptr_t>(Prop1Setter)); | 415 reinterpret_cast<intptr_t>(Prop1Setter)); |
423 CHECK_NE(NULL, | 416 CHECK_NE(NULL, |
424 StrNStr(log.start(), prop1_setter_record.start(), log.length())); | 417 StrNStr(log.start(), prop1_setter_record.start(), log.length())); |
425 | 418 |
426 EmbeddedVector<char, 100> prop2_getter_record; | 419 EmbeddedVector<char, 100> prop2_getter_record; |
427 i::SNPrintF(prop2_getter_record, | 420 i::SNPrintF(prop2_getter_record, |
428 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop2\"", | 421 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop2\"", |
429 reinterpret_cast<intptr_t>(Prop2Getter)); | 422 reinterpret_cast<intptr_t>(Prop2Getter)); |
430 CHECK_NE(NULL, | 423 CHECK_NE(NULL, |
431 StrNStr(log.start(), prop2_getter_record.start(), log.length())); | 424 StrNStr(log.start(), prop2_getter_record.start(), log.length())); |
432 log.Dispose(); | 425 log.Dispose(); |
433 } | |
434 isolate->Dispose(); | |
435 } | 426 } |
436 | 427 |
437 | 428 |
438 typedef i::NativesCollection<i::TEST> TestSources; | 429 typedef i::NativesCollection<i::TEST> TestSources; |
439 | 430 |
440 | 431 |
441 // Test that logging of code create / move events is equivalent to traversal of | 432 // Test that logging of code create / move events is equivalent to traversal of |
442 // a resulting heap. | 433 // a resulting heap. |
443 TEST(EquivalenceOfLoggingAndTraversal) { | 434 TEST(EquivalenceOfLoggingAndTraversal) { |
444 // This test needs to be run on a "clean" V8 to ensure that snapshot log | 435 // 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 | 436 // 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 | 437 // 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 | 438 // cctest manually also works, please be sure that no tests below |
448 // are using V8. | 439 // are using V8. |
449 | 440 |
450 // Start with profiling to capture all code events from the beginning. | 441 // Start with profiling to capture all code events from the beginning. |
451 v8::Isolate* isolate; | 442 ScopedLoggerInitializer initialize_logger; |
452 { | 443 Logger* logger = initialize_logger.logger(); |
453 ScopedLoggerInitializer initialize_logger; | |
454 isolate = initialize_logger.isolate(); | |
455 Logger* logger = initialize_logger.logger(); | |
456 | 444 |
457 // Compile and run a function that creates other functions. | 445 // Compile and run a function that creates other functions. |
458 CompileRun( | 446 CompileRun( |
459 "(function f(obj) {\n" | 447 "(function f(obj) {\n" |
460 " obj.test =\n" | 448 " obj.test =\n" |
461 " (function a(j) { return function b() { return j; } })(100);\n" | 449 " (function a(j) { return function b() { return j; } })(100);\n" |
462 "})(this);"); | 450 "})(this);"); |
463 logger->StopProfiler(); | 451 logger->StopProfiler(); |
464 reinterpret_cast<i::Isolate*>(isolate)->heap()->CollectAllGarbage( | 452 CcTest::heap()->CollectAllGarbage(i::Heap::kMakeHeapIterableMask); |
465 i::Heap::kMakeHeapIterableMask); | 453 logger->StringEvent("test-logging-done", ""); |
466 logger->StringEvent("test-logging-done", ""); | |
467 | 454 |
468 // Iterate heap to find compiled functions, will write to log. | 455 // Iterate heap to find compiled functions, will write to log. |
469 logger->LogCompiledFunctions(); | 456 logger->LogCompiledFunctions(); |
470 logger->StringEvent("test-traversal-done", ""); | 457 logger->StringEvent("test-traversal-done", ""); |
471 | 458 |
472 bool exists = false; | 459 bool exists = false; |
473 i::Vector<const char> log( | 460 i::Vector<const char> log( |
474 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); | 461 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); |
475 CHECK(exists); | 462 CHECK(exists); |
476 v8::Handle<v8::String> log_str = v8::String::NewFromUtf8( | 463 v8::Handle<v8::String> log_str = v8::String::NewFromUtf8( |
477 isolate, log.start(), v8::String::kNormalString, log.length()); | 464 CcTest::isolate(), log.start(), v8::String::kNormalString, log.length()); |
478 initialize_logger.env()->Global()->Set(v8_str("_log"), log_str); | 465 initialize_logger.env()->Global()->Set(v8_str("_log"), log_str); |
479 | 466 |
480 i::Vector<const unsigned char> source = TestSources::GetScriptsSource(); | 467 i::Vector<const unsigned char> source = TestSources::GetScriptsSource(); |
481 v8::Handle<v8::String> source_str = v8::String::NewFromUtf8( | 468 v8::Handle<v8::String> source_str = v8::String::NewFromUtf8( |
482 isolate, reinterpret_cast<const char*>(source.start()), | 469 CcTest::isolate(), reinterpret_cast<const char*>(source.start()), |
483 v8::String::kNormalString, source.length()); | 470 v8::String::kNormalString, source.length()); |
484 v8::TryCatch try_catch; | 471 v8::TryCatch try_catch; |
485 v8::Handle<v8::Script> script = CompileWithOrigin(source_str, ""); | 472 v8::Handle<v8::Script> script = CompileWithOrigin(source_str, ""); |
486 if (script.IsEmpty()) { | 473 if (script.IsEmpty()) { |
487 v8::String::Utf8Value exception(try_catch.Exception()); | 474 v8::String::Utf8Value exception(try_catch.Exception()); |
488 printf("compile: %s\n", *exception); | 475 printf("compile: %s\n", *exception); |
489 CHECK(false); | 476 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 } | |
508 } | 477 } |
509 isolate->Dispose(); | 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); |
| 482 CHECK(false); |
| 483 } |
| 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. |
| 492 fflush(stdout); |
| 493 CHECK(false); |
| 494 } |
510 } | 495 } |
OLD | NEW |