[go: nahoru, domu]

Stack sampler: extract some test utilities to a common place

This creates a common TestModule class from several similar ones
scattered around the code base. It also extracts operator== for
base::Frame

Bug: 1101399
Change-Id: Ibaf6ecc5c30d877837297b474c314a2613a68dc4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3430263
Reviewed-by: Mike Wittman <wittman@chromium.org>
Reviewed-by: Siddhartha S <ssid@chromium.org>
Commit-Queue: Leonard Grey <lgrey@chromium.org>
Cr-Commit-Position: refs/heads/main@{#967388}
diff --git a/base/profiler/chrome_unwinder_android_unittest.cc b/base/profiler/chrome_unwinder_android_unittest.cc
index 4dbbf99c..d6de663e 100644
--- a/base/profiler/chrome_unwinder_android_unittest.cc
+++ b/base/profiler/chrome_unwinder_android_unittest.cc
@@ -9,6 +9,7 @@
 #include "base/profiler/profile_builder.h"
 #include "base/profiler/stack_buffer.h"
 #include "base/profiler/stack_copier_signal.h"
+#include "base/profiler/stack_sampling_profiler_test_util.h"
 #include "base/profiler/thread_delegate_posix.h"
 #include "base/test/gtest_util.h"
 #include "build/build_config.h"
@@ -39,25 +40,6 @@
     0x9,
 };
 
-class TestModule : public ModuleCache::Module {
- public:
-  TestModule(uintptr_t base_address,
-             size_t size,
-             const std::string& build_id = "TestModule")
-      : base_address_(base_address), size_(size), build_id_(build_id) {}
-
-  uintptr_t GetBaseAddress() const override { return base_address_; }
-  std::string GetId() const override { return build_id_; }
-  FilePath GetDebugBasename() const override { return FilePath(); }
-  size_t GetSize() const override { return size_; }
-  bool IsNative() const override { return true; }
-
- private:
-  const uintptr_t base_address_;
-  const size_t size_;
-  const std::string build_id_;
-};
-
 // Utility function to add a single native module during test setup. Returns
 // a pointer to the provided module.
 const ModuleCache::Module* AddNativeModule(
@@ -77,10 +59,6 @@
 
 }  // namespace
 
-bool operator==(const Frame& a, const Frame& b) {
-  return a.instruction_pointer == b.instruction_pointer && a.module == b.module;
-}
-
 // Tests unwind step under normal operation.
 TEST(ChromeUnwinderAndroidTest, Step) {
   const std::vector<uintptr_t> stack_buffer = {
@@ -209,10 +187,8 @@
   auto cfi_table = ArmCFITable::Parse(
       {reinterpret_cast<const uint8_t*>(cfi_data), sizeof(cfi_data)});
 
-  auto chrome_module =
-      std::make_unique<TestModule>(0x1000, 0x500, "ChromeModule");
-  auto non_chrome_module =
-      std::make_unique<TestModule>(0x2000, 0x500, "OtherModule");
+  auto chrome_module = std::make_unique<TestModule>(0x1000, 0x500);
+  auto non_chrome_module = std::make_unique<TestModule>(0x2000, 0x500);
 
   ModuleCache module_cache;
   ChromeUnwinderAndroid unwinder(cfi_table.get(),
diff --git a/base/profiler/chrome_unwinder_android_v2_unittest.cc b/base/profiler/chrome_unwinder_android_v2_unittest.cc
index 8f14225..b76d5d9 100644
--- a/base/profiler/chrome_unwinder_android_v2_unittest.cc
+++ b/base/profiler/chrome_unwinder_android_v2_unittest.cc
@@ -5,6 +5,7 @@
 #include "base/profiler/chrome_unwinder_android_v2.h"
 
 #include "base/profiler/chrome_unwind_info_android.h"
+#include "base/profiler/stack_sampling_profiler_test_util.h"
 #include "base/test/gtest_util.h"
 #include "build/build_config.h"
 #include "testing/gtest/include/gtest/gtest.h"
@@ -949,25 +950,6 @@
   }
 }
 
-class TestModule : public ModuleCache::Module {
- public:
-  TestModule(uintptr_t base_address,
-             size_t size,
-             const std::string& build_id = "TestModule")
-      : base_address_(base_address), size_(size), build_id_(build_id) {}
-
-  uintptr_t GetBaseAddress() const override { return base_address_; }
-  std::string GetId() const override { return build_id_; }
-  FilePath GetDebugBasename() const override { return FilePath(); }
-  size_t GetSize() const override { return size_; }
-  bool IsNative() const override { return true; }
-
- private:
-  const uintptr_t base_address_;
-  const size_t size_;
-  const std::string build_id_;
-};
-
 // Utility function to add a single native module during test setup. Returns
 // a pointer to the provided module.
 const ModuleCache::Module* AddNativeModule(
@@ -990,10 +972,8 @@
       make_span(page_table, 1ul),
   };
 
-  auto chrome_module =
-      std::make_unique<TestModule>(0x1000, 0x500, "ChromeModule");
-  auto non_chrome_module =
-      std::make_unique<TestModule>(0x2000, 0x500, "OtherModule");
+  auto chrome_module = std::make_unique<TestModule>(0x1000, 0x500);
+  auto non_chrome_module = std::make_unique<TestModule>(0x2000, 0x500);
 
   ModuleCache module_cache;
   ChromeUnwinderAndroidV2 unwinder(dummy_unwind_info,
diff --git a/base/profiler/stack_sampler_impl_unittest.cc b/base/profiler/stack_sampler_impl_unittest.cc
index d31c49a8..19c83965 100644
--- a/base/profiler/stack_sampler_impl_unittest.cc
+++ b/base/profiler/stack_sampler_impl_unittest.cc
@@ -17,6 +17,7 @@
 #include "base/profiler/stack_buffer.h"
 #include "base/profiler/stack_copier.h"
 #include "base/profiler/stack_sampler_impl.h"
+#include "base/profiler/stack_sampling_profiler_test_util.h"
 #include "base/profiler/suspendable_thread_delegate.h"
 #include "base/profiler/unwinder.h"
 #include "build/build_config.h"
@@ -167,23 +168,6 @@
   bool update_modules_was_invoked_ = false;
 };
 
-class TestModule : public ModuleCache::Module {
- public:
-  TestModule(uintptr_t base_address, size_t size, bool is_native = true)
-      : base_address_(base_address), size_(size), is_native_(is_native) {}
-
-  uintptr_t GetBaseAddress() const override { return base_address_; }
-  std::string GetId() const override { return ""; }
-  FilePath GetDebugBasename() const override { return FilePath(); }
-  size_t GetSize() const override { return size_; }
-  bool IsNative() const override { return is_native_; }
-
- private:
-  const uintptr_t base_address_;
-  const size_t size_;
-  const bool is_native_;
-};
-
 // Utility function to form a vector from a single module.
 std::vector<std::unique_ptr<const ModuleCache::Module>> ToModuleVector(
     std::unique_ptr<const ModuleCache::Module> module) {
diff --git a/base/profiler/stack_sampling_profiler_test_util.cc b/base/profiler/stack_sampling_profiler_test_util.cc
index 083fa7f..975ee98 100644
--- a/base/profiler/stack_sampling_profiler_test_util.cc
+++ b/base/profiler/stack_sampling_profiler_test_util.cc
@@ -439,4 +439,23 @@
 #endif
 }
 
+uintptr_t TestModule::GetBaseAddress() const {
+  return base_address_;
+}
+std::string TestModule::GetId() const {
+  return id_;
+}
+FilePath TestModule::GetDebugBasename() const {
+  return debug_basename_;
+}
+size_t TestModule::GetSize() const {
+  return size_;
+}
+bool TestModule::IsNative() const {
+  return is_native_;
+}
+
+bool operator==(const Frame& a, const Frame& b) {
+  return a.instruction_pointer == b.instruction_pointer && a.module == b.module;
+}
 }  // namespace base
diff --git a/base/profiler/stack_sampling_profiler_test_util.h b/base/profiler/stack_sampling_profiler_test_util.h
index 9b949e0..c23200ec 100644
--- a/base/profiler/stack_sampling_profiler_test_util.h
+++ b/base/profiler/stack_sampling_profiler_test_util.h
@@ -9,6 +9,7 @@
 #include <string>
 #include <vector>
 
+#include "base/base_export.h"
 #include "base/callback.h"
 #include "base/native_library.h"
 #include "base/profiler/frame.h"
@@ -93,6 +94,34 @@
   const SetupFunction setup_function_;
 };
 
+class TestModule : public ModuleCache::Module {
+ public:
+  explicit TestModule(uintptr_t base_address = 0,
+                      size_t size = 0,
+                      bool is_native = true)
+      : base_address_(base_address), size_(size), is_native_(is_native) {}
+
+  uintptr_t GetBaseAddress() const override;
+  std::string GetId() const override;
+  FilePath GetDebugBasename() const override;
+  size_t GetSize() const override;
+  bool IsNative() const override;
+
+  void set_id(const std::string& id) { id_ = id; }
+  void set_debug_basename(const FilePath& basename) {
+    debug_basename_ = basename;
+  }
+
+ private:
+  const uintptr_t base_address_;
+  const size_t size_;
+  const bool is_native_;
+  std::string id_;
+  FilePath debug_basename_;
+};
+
+bool operator==(const Frame& a, const Frame& b);
+
 // UnwindScenario setup function that calls into |wait_for_sample| without doing
 // any special unwinding setup, to exercise the "normal" unwind scenario.
 FunctionAddressRange CallWithPlainFunction(OnceClosure wait_for_sample);
diff --git a/base/profiler/win32_stack_frame_unwinder_unittest.cc b/base/profiler/win32_stack_frame_unwinder_unittest.cc
index 37aab10..7d32546 100644
--- a/base/profiler/win32_stack_frame_unwinder_unittest.cc
+++ b/base/profiler/win32_stack_frame_unwinder_unittest.cc
@@ -11,6 +11,8 @@
 #include "base/compiler_specific.h"
 #include "base/memory/ptr_util.h"
 #include "base/memory/raw_ptr.h"
+#include "base/profiler/stack_sampling_profiler_test_util.h"
+#include "build/build_config.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 namespace base {
@@ -21,21 +23,6 @@
 // incremented by the same value with each call.
 const uintptr_t kImageBaseIncrement = 1 << 20;
 
-// Stub module for testing.
-class TestModule : public ModuleCache::Module {
- public:
-  TestModule(uintptr_t base_address) : base_address_(base_address) {}
-
-  uintptr_t GetBaseAddress() const override { return base_address_; }
-  std::string GetId() const override { return ""; }
-  FilePath GetDebugBasename() const override { return FilePath(); }
-  size_t GetSize() const override { return 0; }
-  bool IsNative() const override { return true; }
-
- private:
-  const uintptr_t base_address_;
-};
-
 class TestUnwindFunctions : public Win32StackFrameUnwinder::UnwindFunctions {
  public:
   TestUnwindFunctions();