| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/browser_watcher/postmortem_report_collector.h" | 5 #include "components/browser_watcher/postmortem_report_collector.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 for (CollectionStatus status : unexpected_statuses) { | 104 for (CollectionStatus status : unexpected_statuses) { |
| 105 histogram_tester.ExpectBucketCount("ActivityTracker.Collect.Status", status, | 105 histogram_tester.ExpectBucketCount("ActivityTracker.Collect.Status", status, |
| 106 0); | 106 0); |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 | 109 |
| 110 const char kProductName[] = "TestProduct"; | 110 const char kProductName[] = "TestProduct"; |
| 111 const char kVersionNumber[] = "TestVersionNumber"; | 111 const char kVersionNumber[] = "TestVersionNumber"; |
| 112 const char kChannelName[] = "TestChannel"; | 112 const char kChannelName[] = "TestChannel"; |
| 113 | 113 |
| 114 // The tracker creates some data entries internally. | |
| 115 const size_t kInternalProcessDatums = 1; | |
| 116 | |
| 117 void ContainsKeyValue( | |
| 118 const google::protobuf::Map<std::string, TypedValue>& data, | |
| 119 const std::string& key, | |
| 120 const std::string& value) { | |
| 121 auto it = data.find(key); | |
| 122 ASSERT_TRUE(it != data.end()); | |
| 123 EXPECT_EQ(TypedValue::kStringValue, it->second.value_case()); | |
| 124 EXPECT_EQ(value, it->second.string_value()); | |
| 125 } | |
| 126 | |
| 127 // Exposes a public constructor in order to create a dummy database. | 114 // Exposes a public constructor in order to create a dummy database. |
| 128 class MockCrashReportDatabase : public CrashReportDatabase { | 115 class MockCrashReportDatabase : public CrashReportDatabase { |
| 129 public: | 116 public: |
| 130 MockCrashReportDatabase() {} | 117 MockCrashReportDatabase() {} |
| 131 MOCK_METHOD0(GetSettings, Settings*()); | 118 MOCK_METHOD0(GetSettings, Settings*()); |
| 132 MOCK_METHOD1(PrepareNewCrashReport, | 119 MOCK_METHOD1(PrepareNewCrashReport, |
| 133 CrashReportDatabase::CrashReportDatabase::OperationStatus( | 120 CrashReportDatabase::CrashReportDatabase::OperationStatus( |
| 134 NewReport** report)); | 121 NewReport** report)); |
| 135 MOCK_METHOD2(FinishedWritingCrashReport, | 122 MOCK_METHOD2(FinishedWritingCrashReport, |
| 136 CrashReportDatabase::CrashReportDatabase::OperationStatus( | 123 CrashReportDatabase::CrashReportDatabase::OperationStatus( |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 | 347 |
| 361 // Validate collection: random content appears as though there is not | 348 // Validate collection: random content appears as though there is not |
| 362 // stability data. | 349 // stability data. |
| 363 MockCrashReportDatabase crash_db; | 350 MockCrashReportDatabase crash_db; |
| 364 PostmortemReportCollector collector(kProductName, kVersionNumber, | 351 PostmortemReportCollector collector(kProductName, kVersionNumber, |
| 365 kChannelName, &crash_db, nullptr); | 352 kChannelName, &crash_db, nullptr); |
| 366 StabilityReport report; | 353 StabilityReport report; |
| 367 ASSERT_NE(SUCCESS, collector.CollectOneReport(file_path, &report)); | 354 ASSERT_NE(SUCCESS, collector.CollectOneReport(file_path, &report)); |
| 368 } | 355 } |
| 369 | 356 |
| 370 namespace { | |
| 371 | |
| 372 // Parameters for the activity tracking. | |
| 373 const size_t kFileSize = 64 << 10; // 64 KiB | |
| 374 const int kStackDepth = 6; | |
| 375 const uint64_t kAllocatorId = 0; | |
| 376 const char kAllocatorName[] = "PostmortemReportCollectorCollectionTest"; | |
| 377 const uint64_t kTaskSequenceNum = 42; | |
| 378 const uintptr_t kTaskOrigin = 1000U; | |
| 379 const uintptr_t kLockAddress = 1001U; | |
| 380 const uintptr_t kEventAddress = 1002U; | |
| 381 const int kThreadId = 43; | |
| 382 const int kProcessId = 44; | |
| 383 const int kAnotherThreadId = 45; | |
| 384 const uint32_t kGenericId = 46U; | |
| 385 const int32_t kGenericData = 47; | |
| 386 | |
| 387 } // namespace | |
| 388 | |
| 389 // Sets up a file backed thread tracker for direct access. A | |
| 390 // GlobalActivityTracker is not created, meaning there is no risk of | |
| 391 // the instrumentation interfering with the file's content. | |
| 392 class PostmortemReportCollectorCollectionTest : public testing::Test { | |
| 393 public: | |
| 394 // Create a proper debug file. | |
| 395 void SetUp() override { | |
| 396 testing::Test::SetUp(); | |
| 397 | |
| 398 // Create a file backed allocator. | |
| 399 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 400 debug_file_path_ = temp_dir_.GetPath().AppendASCII("debug_file.pma"); | |
| 401 allocator_ = CreateAllocator(); | |
| 402 ASSERT_NE(nullptr, allocator_); | |
| 403 | |
| 404 size_t tracker_mem_size = | |
| 405 ThreadActivityTracker::SizeForStackDepth(kStackDepth); | |
| 406 ASSERT_GT(kFileSize, tracker_mem_size); | |
| 407 | |
| 408 // Create a tracker. | |
| 409 tracker_ = CreateTracker(allocator_.get(), tracker_mem_size); | |
| 410 ASSERT_NE(nullptr, tracker_); | |
| 411 ASSERT_TRUE(tracker_->IsValid()); | |
| 412 } | |
| 413 | |
| 414 std::unique_ptr<PersistentMemoryAllocator> CreateAllocator() { | |
| 415 // Create the memory mapped file. | |
| 416 std::unique_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile()); | |
| 417 bool success = mmfile->Initialize( | |
| 418 File(debug_file_path_, File::FLAG_CREATE | File::FLAG_READ | | |
| 419 File::FLAG_WRITE | File::FLAG_SHARE_DELETE), | |
| 420 {0, static_cast<int64_t>(kFileSize)}, | |
| 421 MemoryMappedFile::READ_WRITE_EXTEND); | |
| 422 if (!success || !mmfile->IsValid()) | |
| 423 return nullptr; | |
| 424 | |
| 425 // Create a persistent memory allocator. | |
| 426 if (!FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile, true)) | |
| 427 return nullptr; | |
| 428 return WrapUnique(new FilePersistentMemoryAllocator( | |
| 429 std::move(mmfile), kFileSize, kAllocatorId, kAllocatorName, false)); | |
| 430 } | |
| 431 | |
| 432 std::unique_ptr<ThreadActivityTracker> CreateTracker( | |
| 433 PersistentMemoryAllocator* allocator, | |
| 434 size_t tracker_mem_size) { | |
| 435 // Allocate a block of memory for the tracker to use. | |
| 436 PersistentMemoryAllocator::Reference mem_reference = allocator->Allocate( | |
| 437 tracker_mem_size, GlobalActivityTracker::kTypeIdActivityTracker); | |
| 438 if (mem_reference == 0U) | |
| 439 return nullptr; | |
| 440 | |
| 441 // Get the memory's base address. | |
| 442 void* mem_base = allocator->GetAsArray<char>( | |
| 443 mem_reference, GlobalActivityTracker::kTypeIdActivityTracker, | |
| 444 PersistentMemoryAllocator::kSizeAny); | |
| 445 if (mem_base == nullptr) | |
| 446 return nullptr; | |
| 447 | |
| 448 // Make the allocation iterable so it can be found by other processes. | |
| 449 allocator->MakeIterable(mem_reference); | |
| 450 | |
| 451 return WrapUnique(new ThreadActivityTracker(mem_base, tracker_mem_size)); | |
| 452 } | |
| 453 | |
| 454 const FilePath& debug_file_path() const { return debug_file_path_; } | |
| 455 | |
| 456 protected: | |
| 457 base::ScopedTempDir temp_dir_; | |
| 458 FilePath debug_file_path_; | |
| 459 | |
| 460 std::unique_ptr<PersistentMemoryAllocator> allocator_; | |
| 461 std::unique_ptr<ThreadActivityTracker> tracker_; | |
| 462 }; | |
| 463 | |
| 464 TEST_F(PostmortemReportCollectorCollectionTest, CollectSuccess) { | |
| 465 // Create some activity data. | |
| 466 tracker_->PushActivity(reinterpret_cast<void*>(kTaskOrigin), | |
| 467 base::debug::Activity::ACT_TASK_RUN, | |
| 468 ActivityData::ForTask(kTaskSequenceNum)); | |
| 469 tracker_->PushActivity( | |
| 470 nullptr, base::debug::Activity::ACT_LOCK_ACQUIRE, | |
| 471 ActivityData::ForLock(reinterpret_cast<void*>(kLockAddress))); | |
| 472 ThreadActivityTracker::ActivityId activity_id = tracker_->PushActivity( | |
| 473 nullptr, base::debug::Activity::ACT_EVENT_WAIT, | |
| 474 ActivityData::ForEvent(reinterpret_cast<void*>(kEventAddress))); | |
| 475 tracker_->PushActivity(nullptr, base::debug::Activity::ACT_THREAD_JOIN, | |
| 476 ActivityData::ForThread(kThreadId)); | |
| 477 tracker_->PushActivity(nullptr, base::debug::Activity::ACT_PROCESS_WAIT, | |
| 478 ActivityData::ForProcess(kProcessId)); | |
| 479 tracker_->PushActivity(nullptr, base::debug::Activity::ACT_GENERIC, | |
| 480 ActivityData::ForGeneric(kGenericId, kGenericData)); | |
| 481 // Note: this exceeds the activity stack's capacity. | |
| 482 tracker_->PushActivity(nullptr, base::debug::Activity::ACT_THREAD_JOIN, | |
| 483 ActivityData::ForThread(kAnotherThreadId)); | |
| 484 | |
| 485 // Add some user data. | |
| 486 ActivityTrackerMemoryAllocator user_data_allocator( | |
| 487 allocator_.get(), GlobalActivityTracker::kTypeIdUserDataRecord, | |
| 488 GlobalActivityTracker::kTypeIdUserDataRecordFree, 1024U, 10U, false); | |
| 489 std::unique_ptr<ActivityUserData> user_data = | |
| 490 tracker_->GetUserData(activity_id, &user_data_allocator); | |
| 491 user_data->SetInt("some_int", 42); | |
| 492 | |
| 493 // Validate collection returns the expected report. | |
| 494 MockCrashReportDatabase crash_db; | |
| 495 PostmortemReportCollector collector(kProductName, kVersionNumber, | |
| 496 kChannelName, &crash_db, nullptr); | |
| 497 StabilityReport report; | |
| 498 ASSERT_EQ(SUCCESS, collector.CollectOneReport(debug_file_path(), &report)); | |
| 499 | |
| 500 // Validate the report. | |
| 501 ASSERT_EQ(1, report.process_states_size()); | |
| 502 const ProcessState& process_state = report.process_states(0); | |
| 503 EXPECT_EQ(base::GetCurrentProcId(), process_state.process_id()); | |
| 504 ASSERT_EQ(1, process_state.threads_size()); | |
| 505 | |
| 506 const ThreadState& thread_state = process_state.threads(0); | |
| 507 EXPECT_EQ(base::PlatformThread::GetName(), thread_state.thread_name()); | |
| 508 #if defined(OS_WIN) | |
| 509 EXPECT_EQ(base::PlatformThread::CurrentId(), thread_state.thread_id()); | |
| 510 #elif defined(OS_POSIX) | |
| 511 EXPECT_EQ(base::PlatformThread::CurrentHandle().platform_handle(), | |
| 512 thread_state.thread_id()); | |
| 513 #endif | |
| 514 | |
| 515 EXPECT_EQ(7, thread_state.activity_count()); | |
| 516 ASSERT_EQ(6, thread_state.activities_size()); | |
| 517 { | |
| 518 const Activity& activity = thread_state.activities(0); | |
| 519 EXPECT_EQ(Activity::ACT_TASK_RUN, activity.type()); | |
| 520 EXPECT_EQ(kTaskOrigin, activity.origin_address()); | |
| 521 EXPECT_EQ(kTaskSequenceNum, activity.task_sequence_id()); | |
| 522 EXPECT_EQ(0U, activity.user_data().size()); | |
| 523 } | |
| 524 { | |
| 525 const Activity& activity = thread_state.activities(1); | |
| 526 EXPECT_EQ(Activity::ACT_LOCK_ACQUIRE, activity.type()); | |
| 527 EXPECT_EQ(kLockAddress, activity.lock_address()); | |
| 528 EXPECT_EQ(0U, activity.user_data().size()); | |
| 529 } | |
| 530 { | |
| 531 const Activity& activity = thread_state.activities(2); | |
| 532 EXPECT_EQ(Activity::ACT_EVENT_WAIT, activity.type()); | |
| 533 EXPECT_EQ(kEventAddress, activity.event_address()); | |
| 534 ASSERT_EQ(1U, activity.user_data().size()); | |
| 535 ASSERT_TRUE(base::ContainsKey(activity.user_data(), "some_int")); | |
| 536 EXPECT_EQ(TypedValue::kSignedValue, | |
| 537 activity.user_data().at("some_int").value_case()); | |
| 538 EXPECT_EQ(42, activity.user_data().at("some_int").signed_value()); | |
| 539 } | |
| 540 { | |
| 541 const Activity& activity = thread_state.activities(3); | |
| 542 EXPECT_EQ(Activity::ACT_THREAD_JOIN, activity.type()); | |
| 543 EXPECT_EQ(kThreadId, activity.thread_id()); | |
| 544 EXPECT_EQ(0U, activity.user_data().size()); | |
| 545 } | |
| 546 { | |
| 547 const Activity& activity = thread_state.activities(4); | |
| 548 EXPECT_EQ(Activity::ACT_PROCESS_WAIT, activity.type()); | |
| 549 EXPECT_EQ(kProcessId, activity.process_id()); | |
| 550 EXPECT_EQ(0U, activity.user_data().size()); | |
| 551 } | |
| 552 { | |
| 553 const Activity& activity = thread_state.activities(5); | |
| 554 EXPECT_EQ(Activity::ACT_GENERIC, activity.type()); | |
| 555 EXPECT_EQ(kGenericId, activity.generic_id()); | |
| 556 EXPECT_EQ(kGenericData, activity.generic_data()); | |
| 557 EXPECT_EQ(0U, activity.user_data().size()); | |
| 558 } | |
| 559 } | |
| 560 | |
| 561 class PostmortemReportCollectorCollectionFromGlobalTrackerTest | 357 class PostmortemReportCollectorCollectionFromGlobalTrackerTest |
| 562 : public testing::Test { | 358 : public testing::Test { |
| 563 public: | 359 public: |
| 564 const int kMemorySize = 1 << 20; // 1MiB | 360 const int kMemorySize = 1 << 20; // 1MiB |
| 565 | 361 |
| 566 PostmortemReportCollectorCollectionFromGlobalTrackerTest() {} | 362 PostmortemReportCollectorCollectionFromGlobalTrackerTest() {} |
| 567 ~PostmortemReportCollectorCollectionFromGlobalTrackerTest() override { | 363 ~PostmortemReportCollectorCollectionFromGlobalTrackerTest() override { |
| 568 GlobalActivityTracker* global_tracker = GlobalActivityTracker::Get(); | 364 GlobalActivityTracker* global_tracker = GlobalActivityTracker::Get(); |
| 569 if (global_tracker) { | 365 if (global_tracker) { |
| 570 global_tracker->ReleaseTrackerForCurrentThreadForTesting(); | 366 global_tracker->ReleaseTrackerForCurrentThreadForTesting(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 581 } | 377 } |
| 582 | 378 |
| 583 const FilePath& debug_file_path() { return debug_file_path_; } | 379 const FilePath& debug_file_path() { return debug_file_path_; } |
| 584 | 380 |
| 585 protected: | 381 protected: |
| 586 base::ScopedTempDir temp_dir_; | 382 base::ScopedTempDir temp_dir_; |
| 587 FilePath debug_file_path_; | 383 FilePath debug_file_path_; |
| 588 }; | 384 }; |
| 589 | 385 |
| 590 TEST_F(PostmortemReportCollectorCollectionFromGlobalTrackerTest, | 386 TEST_F(PostmortemReportCollectorCollectionFromGlobalTrackerTest, |
| 591 LogCollection) { | |
| 592 // Record some log messages. | |
| 593 GlobalActivityTracker::CreateWithFile(debug_file_path(), kMemorySize, 0ULL, | |
| 594 "", 3); | |
| 595 GlobalActivityTracker::Get()->RecordLogMessage("hello world"); | |
| 596 GlobalActivityTracker::Get()->RecordLogMessage("foo bar"); | |
| 597 | |
| 598 // Collect the stability report. | |
| 599 MockCrashReportDatabase crash_db; | |
| 600 PostmortemReportCollector collector(kProductName, kVersionNumber, | |
| 601 kChannelName, &crash_db, nullptr); | |
| 602 StabilityReport report; | |
| 603 ASSERT_EQ(SUCCESS, collector.CollectOneReport(debug_file_path(), &report)); | |
| 604 | |
| 605 // Validate the report's log content. | |
| 606 ASSERT_EQ(2, report.log_messages_size()); | |
| 607 ASSERT_EQ("hello world", report.log_messages(0)); | |
| 608 ASSERT_EQ("foo bar", report.log_messages(1)); | |
| 609 } | |
| 610 | |
| 611 TEST_F(PostmortemReportCollectorCollectionFromGlobalTrackerTest, | |
| 612 ProcessUserDataCollection) { | |
| 613 const char string1[] = "foo"; | |
| 614 const char string2[] = "bar"; | |
| 615 | |
| 616 // Record some process user data. | |
| 617 GlobalActivityTracker::CreateWithFile(debug_file_path(), kMemorySize, 0ULL, | |
| 618 "", 3); | |
| 619 ActivityUserData& process_data = GlobalActivityTracker::Get()->process_data(); | |
| 620 ActivityUserData::Snapshot snapshot; | |
| 621 ASSERT_TRUE(process_data.CreateSnapshot(&snapshot)); | |
| 622 ASSERT_EQ(kInternalProcessDatums, snapshot.size()); | |
| 623 process_data.Set("raw", "foo", 3); | |
| 624 process_data.SetString("string", "bar"); | |
| 625 process_data.SetChar("char", '9'); | |
| 626 process_data.SetInt("int", -9999); | |
| 627 process_data.SetUint("uint", 9999); | |
| 628 process_data.SetBool("bool", true); | |
| 629 process_data.SetReference("ref", string1, strlen(string1)); | |
| 630 process_data.SetStringReference("sref", string2); | |
| 631 | |
| 632 // Collect the stability report. | |
| 633 MockCrashReportDatabase crash_db; | |
| 634 PostmortemReportCollector collector(kProductName, kVersionNumber, | |
| 635 kChannelName, &crash_db, nullptr); | |
| 636 StabilityReport report; | |
| 637 ASSERT_EQ(SUCCESS, collector.CollectOneReport(debug_file_path(), &report)); | |
| 638 | |
| 639 // Validate the report's user data. | |
| 640 const auto& collected_data = report.global_data(); | |
| 641 ASSERT_EQ(kInternalProcessDatums + 12U, collected_data.size()); | |
| 642 | |
| 643 ASSERT_TRUE(base::ContainsKey(collected_data, "raw")); | |
| 644 EXPECT_EQ(TypedValue::kBytesValue, collected_data.at("raw").value_case()); | |
| 645 EXPECT_EQ("foo", collected_data.at("raw").bytes_value()); | |
| 646 | |
| 647 ASSERT_TRUE(base::ContainsKey(collected_data, "string")); | |
| 648 EXPECT_EQ(TypedValue::kStringValue, collected_data.at("string").value_case()); | |
| 649 EXPECT_EQ("bar", collected_data.at("string").string_value()); | |
| 650 | |
| 651 ASSERT_TRUE(base::ContainsKey(collected_data, "char")); | |
| 652 EXPECT_EQ(TypedValue::kCharValue, collected_data.at("char").value_case()); | |
| 653 EXPECT_EQ("9", collected_data.at("char").char_value()); | |
| 654 | |
| 655 ASSERT_TRUE(base::ContainsKey(collected_data, "int")); | |
| 656 EXPECT_EQ(TypedValue::kSignedValue, collected_data.at("int").value_case()); | |
| 657 EXPECT_EQ(-9999, collected_data.at("int").signed_value()); | |
| 658 | |
| 659 ASSERT_TRUE(base::ContainsKey(collected_data, "uint")); | |
| 660 EXPECT_EQ(TypedValue::kUnsignedValue, collected_data.at("uint").value_case()); | |
| 661 EXPECT_EQ(9999U, collected_data.at("uint").unsigned_value()); | |
| 662 | |
| 663 ASSERT_TRUE(base::ContainsKey(collected_data, "bool")); | |
| 664 EXPECT_EQ(TypedValue::kBoolValue, collected_data.at("bool").value_case()); | |
| 665 EXPECT_TRUE(collected_data.at("bool").bool_value()); | |
| 666 | |
| 667 ASSERT_TRUE(base::ContainsKey(collected_data, "ref")); | |
| 668 EXPECT_EQ(TypedValue::kBytesReference, collected_data.at("ref").value_case()); | |
| 669 const TypedValue::Reference& ref = collected_data.at("ref").bytes_reference(); | |
| 670 EXPECT_EQ(reinterpret_cast<uintptr_t>(string1), ref.address()); | |
| 671 EXPECT_EQ(strlen(string1), static_cast<uint64_t>(ref.size())); | |
| 672 | |
| 673 ASSERT_TRUE(base::ContainsKey(collected_data, "sref")); | |
| 674 EXPECT_EQ(TypedValue::kStringReference, | |
| 675 collected_data.at("sref").value_case()); | |
| 676 const TypedValue::Reference& sref = | |
| 677 collected_data.at("sref").string_reference(); | |
| 678 EXPECT_EQ(reinterpret_cast<uintptr_t>(string2), sref.address()); | |
| 679 EXPECT_EQ(strlen(string2), static_cast<uint64_t>(sref.size())); | |
| 680 | |
| 681 // Reporter's version details. | |
| 682 ContainsKeyValue(collected_data, kStabilityReporterProduct, kProductName); | |
| 683 ContainsKeyValue(collected_data, kStabilityReporterChannel, kChannelName); | |
| 684 #if defined(ARCH_CPU_X86) | |
| 685 ContainsKeyValue(collected_data, kStabilityReporterPlatform, "Win32"); | |
| 686 #elif defined(ARCH_CPU_X86_64) | |
| 687 ContainsKeyValue(collected_data, kStabilityReporterPlatform, "Win64"); | |
| 688 #endif | |
| 689 ContainsKeyValue(collected_data, kStabilityReporterVersion, kVersionNumber); | |
| 690 } | |
| 691 | |
| 692 TEST_F(PostmortemReportCollectorCollectionFromGlobalTrackerTest, | |
| 693 FieldTrialCollection) { | |
| 694 // Record some data. | |
| 695 GlobalActivityTracker::CreateWithFile(debug_file_path(), kMemorySize, 0ULL, | |
| 696 "", 3); | |
| 697 ActivityUserData& process_data = GlobalActivityTracker::Get()->process_data(); | |
| 698 process_data.SetString("string", "bar"); | |
| 699 process_data.SetString("FieldTrial.string", "bar"); | |
| 700 process_data.SetString("FieldTrial.foo", "bar"); | |
| 701 | |
| 702 // Collect the stability report. | |
| 703 MockCrashReportDatabase crash_db; | |
| 704 PostmortemReportCollector collector(kProductName, kVersionNumber, | |
| 705 kChannelName, &crash_db, nullptr); | |
| 706 StabilityReport report; | |
| 707 ASSERT_EQ(SUCCESS, collector.CollectOneReport(debug_file_path(), &report)); | |
| 708 | |
| 709 // Validate the report's experiment and global data. | |
| 710 ASSERT_EQ(2, report.field_trials_size()); | |
| 711 EXPECT_NE(0U, report.field_trials(0).name_id()); | |
| 712 EXPECT_NE(0U, report.field_trials(0).group_id()); | |
| 713 EXPECT_NE(0U, report.field_trials(1).name_id()); | |
| 714 EXPECT_EQ(report.field_trials(0).group_id(), | |
| 715 report.field_trials(1).group_id()); | |
| 716 | |
| 717 // Expect 5 key/value pairs (including product details). | |
| 718 const auto& collected_data = report.global_data(); | |
| 719 EXPECT_EQ(kInternalProcessDatums + 5U, collected_data.size()); | |
| 720 EXPECT_TRUE(base::ContainsKey(collected_data, "string")); | |
| 721 } | |
| 722 | |
| 723 TEST_F(PostmortemReportCollectorCollectionFromGlobalTrackerTest, | |
| 724 ModuleCollection) { | |
| 725 // Record some module information. | |
| 726 GlobalActivityTracker::CreateWithFile(debug_file_path(), kMemorySize, 0ULL, | |
| 727 "", 3); | |
| 728 | |
| 729 base::debug::GlobalActivityTracker::ModuleInfo module_info = {}; | |
| 730 module_info.is_loaded = true; | |
| 731 module_info.address = 0x123456; | |
| 732 module_info.load_time = 1111LL; | |
| 733 module_info.size = 0x2d000; | |
| 734 module_info.timestamp = 0xCAFECAFE; | |
| 735 module_info.age = 1; | |
| 736 crashpad::UUID debug_uuid; | |
| 737 debug_uuid.InitializeFromString("11223344-5566-7788-abcd-0123456789ab"); | |
| 738 memcpy(module_info.identifier, &debug_uuid, sizeof(module_info.identifier)); | |
| 739 module_info.file = "foo"; | |
| 740 module_info.debug_file = "bar"; | |
| 741 | |
| 742 GlobalActivityTracker::Get()->RecordModuleInfo(module_info); | |
| 743 | |
| 744 // Collect the stability report. | |
| 745 MockCrashReportDatabase crash_db; | |
| 746 PostmortemReportCollector collector(kProductName, kVersionNumber, | |
| 747 kChannelName, &crash_db, nullptr); | |
| 748 StabilityReport report; | |
| 749 ASSERT_EQ(SUCCESS, collector.CollectOneReport(debug_file_path(), &report)); | |
| 750 | |
| 751 // Validate the report's modules content. | |
| 752 ASSERT_EQ(1, report.process_states_size()); | |
| 753 const ProcessState& process_state = report.process_states(0); | |
| 754 ASSERT_EQ(1, process_state.modules_size()); | |
| 755 | |
| 756 const CodeModule collected_module = process_state.modules(0); | |
| 757 EXPECT_EQ(module_info.address, | |
| 758 static_cast<uintptr_t>(collected_module.base_address())); | |
| 759 EXPECT_EQ(module_info.size, static_cast<size_t>(collected_module.size())); | |
| 760 EXPECT_EQ(module_info.file, collected_module.code_file()); | |
| 761 EXPECT_EQ("CAFECAFE2d000", collected_module.code_identifier()); | |
| 762 EXPECT_EQ(module_info.debug_file, collected_module.debug_file()); | |
| 763 EXPECT_EQ("1122334455667788ABCD0123456789AB1", | |
| 764 collected_module.debug_identifier()); | |
| 765 EXPECT_EQ("", collected_module.version()); | |
| 766 EXPECT_EQ(0LL, collected_module.shrink_down_delta()); | |
| 767 EXPECT_EQ(!module_info.is_loaded, collected_module.is_unloaded()); | |
| 768 } | |
| 769 | |
| 770 TEST_F(PostmortemReportCollectorCollectionFromGlobalTrackerTest, | |
| 771 SystemStateTest) { | 387 SystemStateTest) { |
| 772 // Setup. | 388 // Setup. |
| 773 GlobalActivityTracker::CreateWithFile(debug_file_path(), kMemorySize, 0ULL, | 389 GlobalActivityTracker::CreateWithFile(debug_file_path(), kMemorySize, 0ULL, |
| 774 "", 3); | 390 "", 3); |
| 775 ActivityUserData& process_data = GlobalActivityTracker::Get()->process_data(); | 391 ActivityUserData& process_data = GlobalActivityTracker::Get()->process_data(); |
| 776 process_data.SetInt(kStabilityStartTimestamp, 12345LL); | 392 process_data.SetInt(kStabilityStartTimestamp, 12345LL); |
| 777 | 393 |
| 778 // Collect. | 394 // Collect. |
| 779 MockSystemSessionAnalyzer analyzer; | 395 MockSystemSessionAnalyzer analyzer; |
| 780 EXPECT_CALL(analyzer, | 396 EXPECT_CALL(analyzer, |
| 781 IsSessionUnclean(base::Time::FromInternalValue(12345LL))) | 397 IsSessionUnclean(base::Time::FromInternalValue(12345LL))) |
| 782 .Times(1) | 398 .Times(1) |
| 783 .WillOnce(Return(SystemSessionAnalyzer::CLEAN)); | 399 .WillOnce(Return(SystemSessionAnalyzer::CLEAN)); |
| 784 MockCrashReportDatabase crash_db; | 400 MockCrashReportDatabase crash_db; |
| 785 PostmortemReportCollector collector(kProductName, kVersionNumber, | 401 PostmortemReportCollector collector(kProductName, kVersionNumber, |
| 786 kChannelName, &crash_db, &analyzer); | 402 kChannelName, &crash_db, &analyzer); |
| 787 StabilityReport report; | 403 StabilityReport report; |
| 788 ASSERT_EQ(SUCCESS, collector.CollectOneReport(debug_file_path(), &report)); | 404 ASSERT_EQ(SUCCESS, collector.CollectOneReport(debug_file_path(), &report)); |
| 789 | 405 |
| 790 // Validate the report. | 406 // Validate the report. |
| 791 ASSERT_EQ(SystemState::CLEAN, report.system_state().session_state()); | 407 ASSERT_EQ(SystemState::CLEAN, report.system_state().session_state()); |
| 792 } | 408 } |
| 793 | 409 |
| 794 } // namespace browser_watcher | 410 } // namespace browser_watcher |
| OLD | NEW |