| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <cstdlib> | 8 #include <cstdlib> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/files/file_util.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 17 #include "base/native_library.h" | 18 #include "base/native_library.h" |
| 18 #include "base/path_service.h" | 19 #include "base/path_service.h" |
| 19 #include "base/profiler/native_stack_sampler.h" | 20 #include "base/profiler/native_stack_sampler.h" |
| 20 #include "base/profiler/stack_sampling_profiler.h" | 21 #include "base/profiler/stack_sampling_profiler.h" |
| 21 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
| 22 #include "base/scoped_native_library.h" | 23 #include "base/scoped_native_library.h" |
| 23 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
| 24 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
| 25 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 26 #include "base/synchronization/waitable_event.h" | 27 #include "base/synchronization/waitable_event.h" |
| 27 #include "base/threading/platform_thread.h" | 28 #include "base/threading/platform_thread.h" |
| 28 #include "base/threading/simple_thread.h" | 29 #include "base/threading/simple_thread.h" |
| 29 #include "base/time/time.h" | 30 #include "base/time/time.h" |
| 30 #include "build/build_config.h" | 31 #include "build/build_config.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 32 | 33 |
| 33 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 34 #include <intrin.h> | 35 #include <intrin.h> |
| 35 #include <malloc.h> | 36 #include <malloc.h> |
| 36 #include <windows.h> | 37 #include <windows.h> |
| 37 #else | 38 #else |
| 38 #include <alloca.h> | 39 #include <alloca.h> |
| 39 #endif | 40 #endif |
| 40 | 41 |
| 41 // STACK_SAMPLING_PROFILER_SUPPORTED is used to conditionally enable the tests | 42 // STACK_SAMPLING_PROFILER_SUPPORTED is used to conditionally enable the tests |
| 42 // below for supported platforms (currently Win x64). | 43 // below for supported platforms (currently Win x64 and Mac x64). |
| 43 #if defined(_WIN64) | 44 #if defined(_WIN64) || (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 44 #define STACK_SAMPLING_PROFILER_SUPPORTED 1 | 45 #define STACK_SAMPLING_PROFILER_SUPPORTED 1 |
| 45 #endif | 46 #endif |
| 46 | 47 |
| 47 #if defined(OS_WIN) | 48 #if defined(OS_WIN) |
| 48 #pragma intrinsic(_ReturnAddress) | 49 #pragma intrinsic(_ReturnAddress) |
| 49 #endif | 50 #endif |
| 50 | 51 |
| 51 namespace base { | 52 namespace base { |
| 52 | 53 |
| 53 using SamplingParams = StackSamplingProfiler::SamplingParams; | 54 using SamplingParams = StackSamplingProfiler::SamplingParams; |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 uintptr_t module_base_address = reinterpret_cast<uintptr_t>(library); | 301 uintptr_t module_base_address = reinterpret_cast<uintptr_t>(library); |
| 301 HMODULE module_handle; | 302 HMODULE module_handle; |
| 302 // Keep trying to get the module handle until the call fails. | 303 // Keep trying to get the module handle until the call fails. |
| 303 while (::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | | 304 while (::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | |
| 304 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, | 305 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 305 reinterpret_cast<LPCTSTR>(module_base_address), | 306 reinterpret_cast<LPCTSTR>(module_base_address), |
| 306 &module_handle) || | 307 &module_handle) || |
| 307 ::GetLastError() != ERROR_MOD_NOT_FOUND) { | 308 ::GetLastError() != ERROR_MOD_NOT_FOUND) { |
| 308 PlatformThread::Sleep(TimeDelta::FromMilliseconds(1)); | 309 PlatformThread::Sleep(TimeDelta::FromMilliseconds(1)); |
| 309 } | 310 } |
| 311 #elif defined(OS_MACOSX) |
| 312 // Unloading a library on the Mac is synchronous. |
| 310 #else | 313 #else |
| 311 NOTIMPLEMENTED(); | 314 NOTIMPLEMENTED(); |
| 312 #endif | 315 #endif |
| 313 } | 316 } |
| 314 | 317 |
| 315 // Called on the profiler thread when complete, to collect profiles. | 318 // Called on the profiler thread when complete, to collect profiles. |
| 316 void SaveProfiles(CallStackProfiles* profiles, | 319 void SaveProfiles(CallStackProfiles* profiles, |
| 317 CallStackProfiles pending_profiles) { | 320 CallStackProfiles pending_profiles) { |
| 318 *profiles = std::move(pending_profiles); | 321 *profiles = std::move(pending_profiles); |
| 319 } | 322 } |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 // SignalAndWaitUntilSignaled. The frame in the now-unloaded library is not | 585 // SignalAndWaitUntilSignaled. The frame in the now-unloaded library is not |
| 583 // recorded since we can't get module information. | 586 // recorded since we can't get module information. |
| 584 // | 587 // |
| 585 // ... WaitableEvent and system frames ... | 588 // ... WaitableEvent and system frames ... |
| 586 // TargetThread::SignalAndWaitUntilSignaled | 589 // TargetThread::SignalAndWaitUntilSignaled |
| 587 // TargetThread::OtherLibraryCallback | 590 // TargetThread::OtherLibraryCallback |
| 588 EXPECT_EQ(2, sample.frames.end() - end_frame) | 591 EXPECT_EQ(2, sample.frames.end() - end_frame) |
| 589 << "Stack:\n" | 592 << "Stack:\n" |
| 590 << FormatSampleForDiagnosticOutput(sample, profile.modules); | 593 << FormatSampleForDiagnosticOutput(sample, profile.modules); |
| 591 } else { | 594 } else { |
| 592 // We didn't wait for the asynchonous unloading to complete, so the results | 595 // We didn't wait for the asynchronous unloading to complete, so the results |
| 593 // are non-deterministic: if the library finished unloading we should have | 596 // are non-deterministic: if the library finished unloading we should have |
| 594 // the same stack as |wait_until_unloaded|, if not we should have the full | 597 // the same stack as |wait_until_unloaded|, if not we should have the full |
| 595 // stack. The important thing is that we should not crash. | 598 // stack. The important thing is that we should not crash. |
| 596 | 599 |
| 597 if (sample.frames.end() - end_frame == 2) { | 600 if (sample.frames.end() - end_frame == 2) { |
| 598 // This is the same case as |wait_until_unloaded|. | 601 // This is the same case as |wait_until_unloaded|. |
| 599 return; | 602 return; |
| 600 } | 603 } |
| 601 | 604 |
| 602 // Check that the stack contains a frame for | 605 // Check that the stack contains a frame for |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 679 Frames::const_iterator loc = FindFirstFrameWithinFunction( | 682 Frames::const_iterator loc = FindFirstFrameWithinFunction( |
| 680 sample, &TargetThread::SignalAndWaitUntilSignaled); | 683 sample, &TargetThread::SignalAndWaitUntilSignaled); |
| 681 ASSERT_TRUE(loc != sample.frames.end()) | 684 ASSERT_TRUE(loc != sample.frames.end()) |
| 682 << "Function at " | 685 << "Function at " |
| 683 << MaybeFixupFunctionAddressForILT(reinterpret_cast<const void*>( | 686 << MaybeFixupFunctionAddressForILT(reinterpret_cast<const void*>( |
| 684 &TargetThread::SignalAndWaitUntilSignaled)) | 687 &TargetThread::SignalAndWaitUntilSignaled)) |
| 685 << " was not found in stack:\n" | 688 << " was not found in stack:\n" |
| 686 << FormatSampleForDiagnosticOutput(sample, profile.modules); | 689 << FormatSampleForDiagnosticOutput(sample, profile.modules); |
| 687 FilePath executable_path; | 690 FilePath executable_path; |
| 688 EXPECT_TRUE(PathService::Get(FILE_EXE, &executable_path)); | 691 EXPECT_TRUE(PathService::Get(FILE_EXE, &executable_path)); |
| 689 EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); | 692 EXPECT_EQ(executable_path, |
| 693 MakeAbsoluteFilePath(profile.modules[loc->module_index].filename)); |
| 690 } | 694 } |
| 691 | 695 |
| 692 // Checks that annotations are recorded in samples. | 696 // Checks that annotations are recorded in samples. |
| 693 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 697 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
| 694 #define MAYBE_Annotations Annotations | 698 #define MAYBE_Annotations Annotations |
| 695 #else | 699 #else |
| 696 #define MAYBE_Annotations DISABLED_Annotations | 700 #define MAYBE_Annotations DISABLED_Annotations |
| 697 #endif | 701 #endif |
| 698 TEST_F(StackSamplingProfilerTest, MAYBE_Annotations) { | 702 TEST_F(StackSamplingProfilerTest, MAYBE_Annotations) { |
| 699 SamplingParams params; | 703 SamplingParams params; |
| (...skipping 719 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1419 // TargetThread::SignalAndWaitUntilSignaled | 1423 // TargetThread::SignalAndWaitUntilSignaled |
| 1420 // TargetThread::OtherLibraryCallback | 1424 // TargetThread::OtherLibraryCallback |
| 1421 // InvokeCallbackFunction (in other library) | 1425 // InvokeCallbackFunction (in other library) |
| 1422 // TargetThread::CallThroughOtherLibrary | 1426 // TargetThread::CallThroughOtherLibrary |
| 1423 EXPECT_EQ(3, other_library_frame - end_frame) | 1427 EXPECT_EQ(3, other_library_frame - end_frame) |
| 1424 << "Stack:\n" << FormatSampleForDiagnosticOutput(sample, profile.modules); | 1428 << "Stack:\n" << FormatSampleForDiagnosticOutput(sample, profile.modules); |
| 1425 } | 1429 } |
| 1426 | 1430 |
| 1427 // Checks that a stack that runs through a library that is unloading produces a | 1431 // Checks that a stack that runs through a library that is unloading produces a |
| 1428 // stack, and doesn't crash. | 1432 // stack, and doesn't crash. |
| 1429 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 1433 // Unloading is synchronous on the Mac, so this test is inapplicable. |
| 1434 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) && !defined(OS_MACOSX) |
| 1430 #define MAYBE_UnloadingLibrary UnloadingLibrary | 1435 #define MAYBE_UnloadingLibrary UnloadingLibrary |
| 1431 #else | 1436 #else |
| 1432 #define MAYBE_UnloadingLibrary DISABLED_UnloadingLibrary | 1437 #define MAYBE_UnloadingLibrary DISABLED_UnloadingLibrary |
| 1433 #endif | 1438 #endif |
| 1434 TEST_F(StackSamplingProfilerTest, MAYBE_UnloadingLibrary) { | 1439 TEST_F(StackSamplingProfilerTest, MAYBE_UnloadingLibrary) { |
| 1435 TestLibraryUnload(false); | 1440 TestLibraryUnload(false); |
| 1436 } | 1441 } |
| 1437 | 1442 |
| 1438 // Checks that a stack that runs through a library that has been unloaded | 1443 // Checks that a stack that runs through a library that has been unloaded |
| 1439 // produces a stack, and doesn't crash. | 1444 // produces a stack, and doesn't crash. |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1585 EXPECT_EQ(9u, profiler_thread1.profiles()[0].samples.size()); | 1590 EXPECT_EQ(9u, profiler_thread1.profiles()[0].samples.size()); |
| 1586 ASSERT_EQ(1u, profiler_thread2.profiles().size()); | 1591 ASSERT_EQ(1u, profiler_thread2.profiles().size()); |
| 1587 EXPECT_EQ(8u, profiler_thread2.profiles()[0].samples.size()); | 1592 EXPECT_EQ(8u, profiler_thread2.profiles()[0].samples.size()); |
| 1588 | 1593 |
| 1589 profiler_thread1.Join(); | 1594 profiler_thread1.Join(); |
| 1590 profiler_thread2.Join(); | 1595 profiler_thread2.Join(); |
| 1591 }); | 1596 }); |
| 1592 } | 1597 } |
| 1593 | 1598 |
| 1594 } // namespace base | 1599 } // namespace base |
| OLD | NEW |