Add trace event to differentiate b/w libunwindstack and core unwinder

Stack sampling in Android tracing could use either libunwindstack_unwinder or
native_unwinder_android. Both of these show up in traces as unwinder with
kCustomAndroid type. This CL adds a new unwinder type enum and passes
the unwinder type to CreateOnMainThread. With this we should see should
see different trace events for libunwindstack unwinder and native_unwinder_android

Bug: 1370988, b/246509988
Change-Id: I64050c353d567de4d9d0c987bdca5a3888409111
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3963598
Reviewed-by: Stephen Nusko <nuskos@chromium.org>
Reviewed-by: Tommy Nyquist <nyquist@chromium.org>
Commit-Queue: Kartar Singh <kartarsingh@google.com>
Reviewed-by: Mike Wittman <wittman@chromium.org>
Reviewed-by: Tushar Agarwal <agarwaltushar@google.com>
Cr-Commit-Position: refs/heads/main@{#1062849}
diff --git a/chrome/app/chrome_main_delegate.cc b/chrome/app/chrome_main_delegate.cc
index 3d62d009..305617d3 100644
--- a/chrome/app/chrome_main_delegate.cc
+++ b/chrome/app/chrome_main_delegate.cc
@@ -991,6 +991,8 @@
   // the main thread.
   base::RepeatingCallback tracing_factory =
       base::BindRepeating(&CreateCoreUnwindersFactory);
+  tracing::TracingSamplerProfiler::UnwinderType unwinder_type =
+      tracing::TracingSamplerProfiler::UnwinderType::kCustomAndroid;
 #if BUILDFLAG(IS_ANDROID)
   // If we are the browser process (missing process type), then use the
   // experimental libunwindstack unwinder.
@@ -998,11 +1000,13 @@
       chrome::android::IsJavaDrivenFeatureEnabled(
           chrome::android::kUseLibunwindstackNativeUnwinderAndroid)) {
     tracing_factory = base::BindRepeating(&CreateLibunwindstackUnwinderFactory);
+    unwinder_type = tracing::TracingSamplerProfiler::UnwinderType::
+        kLibunwindstackUnwinderAndroid;
   }
 #endif
   tracing_sampler_profiler_ =
       tracing::TracingSamplerProfiler::CreateOnMainThread(
-          std::move(tracing_factory));
+          std::move(tracing_factory), unwinder_type);
 
 #if BUILDFLAG(IS_WIN)
   v8_crashpad_support::SetUp();
diff --git a/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.cc b/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.cc
index edb0943..9b63ffac 100644
--- a/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.cc
+++ b/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.cc
@@ -392,6 +392,8 @@
       return "TracingSamplerProfiler (custom android unwinder)";
     case TracingSamplerProfiler::UnwinderType::kDefault:
       return "TracingSamplerProfiler (default unwinder)";
+    case TracingSamplerProfiler::UnwinderType::kLibunwindstackUnwinderAndroid:
+      return "TracingSamplerProfiler (libunwindstack unwinder android)";
   }
 }
 
@@ -736,10 +738,11 @@
 // static
 std::unique_ptr<TracingSamplerProfiler>
 TracingSamplerProfiler::CreateOnMainThread(
-    CoreUnwindersCallback core_unwinders_factory_function) {
+    CoreUnwindersCallback core_unwinders_factory_function,
+    UnwinderType unwinder_type) {
   auto profiler = std::make_unique<TracingSamplerProfiler>(
       base::GetSamplingProfilerCurrentThreadToken(),
-      std::move(core_unwinders_factory_function));
+      std::move(core_unwinders_factory_function), unwinder_type);
   // If running in single process mode, there may be multiple "main thread"
   // profilers created. In this case, we assume the first created one is the
   // browser one.
@@ -830,10 +833,12 @@
 
 TracingSamplerProfiler::TracingSamplerProfiler(
     base::SamplingProfilerThreadToken sampled_thread_token,
-    CoreUnwindersCallback core_unwinders_factory_function)
+    CoreUnwindersCallback core_unwinders_factory_function,
+    UnwinderType unwinder_type)
     : sampled_thread_token_(sampled_thread_token),
       core_unwinders_factory_function_(
-          std::move(core_unwinders_factory_function)) {
+          std::move(core_unwinders_factory_function)),
+      unwinder_type_(unwinder_type) {
   DCHECK_NE(sampled_thread_token_.id, base::kInvalidThreadId);
   TracingSamplerProfilerDataSource::Get()->RegisterProfiler(this);
 }
@@ -907,13 +912,19 @@
     core_unwinders_factory = core_unwinders_factory_function_.Run();
   }
   if (core_unwinders_factory) {
-    profile_builder->SetUnwinderType(UnwinderType::kCustomAndroid);
+    if (unwinder_type_ == UnwinderType::kUnknown) {
+      unwinder_type_ = UnwinderType::kCustomAndroid;
+    }
+    profile_builder->SetUnwinderType(unwinder_type_);
     profiler_ = std::make_unique<base::StackSamplingProfiler>(
         sampled_thread_token_, params, std::move(profile_builder),
         std::move(core_unwinders_factory));
   }
 #else   // BUILDFLAG(IS_ANDROID)
-  profile_builder->SetUnwinderType(UnwinderType::kDefault);
+  if (unwinder_type_ == UnwinderType::kUnknown) {
+    unwinder_type_ = UnwinderType::kDefault;
+  }
+  profile_builder->SetUnwinderType(unwinder_type_);
   profiler_ = std::make_unique<base::StackSamplingProfiler>(
       sampled_thread_token_, params, std::move(profile_builder));
 #endif  // BUILDFLAG(IS_ANDROID)
diff --git a/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.h b/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.h
index 6280236..768a8a22 100644
--- a/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.h
+++ b/services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.h
@@ -94,7 +94,12 @@
   };
 
   // Different kinds of unwinders that are used for stack sampling.
-  enum class UnwinderType { kUnknown, kCustomAndroid, kDefault };
+  enum class UnwinderType {
+    kUnknown,
+    kCustomAndroid,
+    kDefault,
+    kLibunwindstackUnwinderAndroid
+  };
 
   // This class will receive the sampling profiler stackframes and output them
   // to the chrome trace via an event. Exposed for testing.
@@ -187,7 +192,8 @@
   // be used to supply custom unwinders to be used during stack sampling.
   static std::unique_ptr<TracingSamplerProfiler> CreateOnMainThread(
       CoreUnwindersCallback core_unwinders_factory_function =
-          CoreUnwindersCallback());
+          CoreUnwindersCallback(),
+      UnwinderType unwinder_type = UnwinderType::kUnknown);
 
   TracingSamplerProfiler(const TracingSamplerProfiler&) = delete;
   TracingSamplerProfiler& operator=(const TracingSamplerProfiler&) = delete;
@@ -223,7 +229,8 @@
 
   explicit TracingSamplerProfiler(
       base::SamplingProfilerThreadToken sampled_thread_token,
-      CoreUnwindersCallback core_unwinders_factory_function);
+      CoreUnwindersCallback core_unwinders_factory_function,
+      UnwinderType unwinder_type = UnwinderType::kUnknown);
   virtual ~TracingSamplerProfiler();
 
   // Sets a callback to create auxiliary unwinders, for handling additional,
@@ -254,6 +261,11 @@
   CoreUnwindersCallback core_unwinders_factory_function_;
   base::RepeatingCallback<std::unique_ptr<base::Unwinder>()>
       aux_unwinder_factory_;
+  // To differentiate b/w different unwinders used for browser main
+  // thread sampling.
+  // TODO(crbug.com/1377364): Remove once we have single unwinder for browser
+  // main.
+  UnwinderType unwinder_type_;
 
   base::Lock lock_;
   std::unique_ptr<base::StackSamplingProfiler> profiler_;  // under |lock_|