Let threads opt in to having their stack traces printed

The video decoder thread is the pilot user.

For now this is an Android-only feature, since that's the only
platform we can print stack traces on.

Bug: webrtc:9987
Change-Id: Ie638c619673b5f159d91a32683fd787baf46479a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/126222
Reviewed-by: Magnus Jedvert <magjed@webrtc.org>
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27127}
diff --git a/rtc_base/system/BUILD.gn b/rtc_base/system/BUILD.gn
index 8b83d04..6448348 100644
--- a/rtc_base/system/BUILD.gn
+++ b/rtc_base/system/BUILD.gn
@@ -78,3 +78,19 @@
     libs = [ "Foundation.framework" ]
   }
 }
+
+rtc_source_set("thread_registry") {
+  sources = [
+    "thread_registry.h",
+  ]
+  deps = [
+    "..:rtc_base_approved",
+  ]
+  if (is_android && !build_with_chromium) {
+    sources += [ "thread_registry.cc" ]
+    deps += [
+      "../../sdk/android:native_api_stacktrace",
+      "//third_party/abseil-cpp/absl/base:core_headers",
+    ]
+  }
+}
diff --git a/rtc_base/system/DEPS b/rtc_base/system/DEPS
new file mode 100644
index 0000000..09d6c93
--- /dev/null
+++ b/rtc_base/system/DEPS
@@ -0,0 +1,5 @@
+specific_include_rules = {
+  "thread_registry\.cc": [
+    "+sdk/android/native_api/stacktrace/stacktrace.h",
+  ],
+}
diff --git a/rtc_base/system/thread_registry.cc b/rtc_base/system/thread_registry.cc
new file mode 100644
index 0000000..8d7cd58
--- /dev/null
+++ b/rtc_base/system/thread_registry.cc
@@ -0,0 +1,71 @@
+/*
+ *  Copyright 2019 The WebRTC Project Authors. All rights reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "rtc_base/system/thread_registry.h"
+
+#include <map>
+#include <utility>
+
+#include "absl/base/attributes.h"
+#include "rtc_base/critical_section.h"
+#include "rtc_base/logging.h"
+#include "rtc_base/platform_thread_types.h"
+#include "sdk/android/native_api/stacktrace/stacktrace.h"
+
+namespace webrtc {
+
+namespace {
+
+struct ThreadData {
+  const rtc::PlatformThreadId thread_id;
+  const rtc::Location location;
+};
+
+// The map of registered threads, and the lock that protects it. We create the
+// map on first use, and never destroy it.
+ABSL_CONST_INIT rtc::GlobalLockPod g_thread_registry_lock = {};
+ABSL_CONST_INIT std::map<const ScopedRegisterThreadForDebugging*, ThreadData>*
+    g_registered_threads = nullptr;
+
+}  // namespace
+
+ScopedRegisterThreadForDebugging::ScopedRegisterThreadForDebugging(
+    rtc::Location location) {
+  rtc::GlobalLockScope gls(&g_thread_registry_lock);
+  if (g_registered_threads == nullptr) {
+    g_registered_threads =
+        new std::map<const ScopedRegisterThreadForDebugging*, ThreadData>();
+  }
+  const auto result = g_registered_threads->insert(
+      std::make_pair(this, ThreadData{rtc::CurrentThreadId(), location}));
+  RTC_DCHECK(result.second);  // Insertion succeeded without collisions.
+}
+
+ScopedRegisterThreadForDebugging::~ScopedRegisterThreadForDebugging() {
+  rtc::GlobalLockScope gls(&g_thread_registry_lock);
+  RTC_DCHECK(g_registered_threads != nullptr);
+  const int num_erased = g_registered_threads->erase(this);
+  RTC_DCHECK_EQ(num_erased, 1);
+}
+
+void PrintStackTracesOfRegisteredThreads() {
+  rtc::GlobalLockScope gls(&g_thread_registry_lock);
+  if (g_registered_threads == nullptr) {
+    return;
+  }
+  for (const auto& e : *g_registered_threads) {
+    const ThreadData& td = e.second;
+    RTC_LOG(LS_WARNING) << "Thread " << td.thread_id << " registered at "
+                        << td.location.ToString() << ":";
+    RTC_LOG(LS_WARNING) << StackTraceToString(GetStackTrace(td.thread_id));
+  }
+}
+
+}  // namespace webrtc
diff --git a/rtc_base/system/thread_registry.h b/rtc_base/system/thread_registry.h
new file mode 100644
index 0000000..0e3187b
--- /dev/null
+++ b/rtc_base/system/thread_registry.h
@@ -0,0 +1,46 @@
+/*
+ *  Copyright 2019 The WebRTC Project Authors. All rights reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef RTC_BASE_SYSTEM_THREAD_REGISTRY_H_
+#define RTC_BASE_SYSTEM_THREAD_REGISTRY_H_
+
+#include "rtc_base/location.h"
+
+namespace webrtc {
+
+class ScopedRegisterThreadForDebugging {
+ public:
+#if defined(WEBRTC_ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD)
+  explicit ScopedRegisterThreadForDebugging(rtc::Location location);
+  ~ScopedRegisterThreadForDebugging();
+#else
+  explicit ScopedRegisterThreadForDebugging(rtc::Location) {}
+#endif
+
+  // Not movable or copyable, because we can't duplicate the resource it owns,
+  // and it needs a constant address.
+  ScopedRegisterThreadForDebugging(const ScopedRegisterThreadForDebugging&) =
+      delete;
+  ScopedRegisterThreadForDebugging(ScopedRegisterThreadForDebugging&&) = delete;
+  ScopedRegisterThreadForDebugging& operator=(
+      const ScopedRegisterThreadForDebugging&) = delete;
+  ScopedRegisterThreadForDebugging& operator=(
+      ScopedRegisterThreadForDebugging&&) = delete;
+};
+
+#if defined(WEBRTC_ANDROID) && !defined(WEBRTC_CHROMIUM_BUILD)
+void PrintStackTracesOfRegisteredThreads();
+#else
+inline void PrintStackTracesOfRegisteredThreads() {}
+#endif
+
+}  // namespace webrtc
+
+#endif  // RTC_BASE_SYSTEM_THREAD_REGISTRY_H_
diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn
index 9453e94..ff039cf 100644
--- a/sdk/android/BUILD.gn
+++ b/sdk/android/BUILD.gn
@@ -776,6 +776,7 @@
       "../../rtc_base:rtc_base",
       "../../rtc_base:rtc_base_approved",
       "../../rtc_base:rtc_task_queue",
+      "../../rtc_base/system:thread_registry",
       "../../system_wrappers:field_trial",
       "//third_party/abseil-cpp/absl/memory",
       "//third_party/abseil-cpp/absl/types:optional",
diff --git a/sdk/android/api/org/webrtc/PeerConnectionFactory.java b/sdk/android/api/org/webrtc/PeerConnectionFactory.java
index 7cab96c..6aa68c2 100644
--- a/sdk/android/api/org/webrtc/PeerConnectionFactory.java
+++ b/sdk/android/api/org/webrtc/PeerConnectionFactory.java
@@ -527,12 +527,15 @@
   /**
    * Print the Java stack traces for the critical threads used by PeerConnectionFactory, namely;
    * signaling thread, worker thread, and network thread. If printNativeStackTraces is true, also
-   * attempt to print the C++ stack traces for these threads.
+   * attempt to print the C++ stack traces for these (and some other) threads.
    */
   public void printInternalStackTraces(boolean printNativeStackTraces) {
     printStackTrace(signalingThread, printNativeStackTraces);
     printStackTrace(workerThread, printNativeStackTraces);
     printStackTrace(networkThread, printNativeStackTraces);
+    if (printNativeStackTraces) {
+      nativePrintStackTracesOfRegisteredThreads();
+    }
   }
 
   @CalledByNative
@@ -592,4 +595,5 @@
   private static native void nativeInjectLoggable(JNILogging jniLogging, int severity);
   private static native void nativeDeleteLoggable();
   private static native void nativePrintStackTrace(int tid);
+  private static native void nativePrintStackTracesOfRegisteredThreads();
 }
diff --git a/sdk/android/src/jni/pc/peer_connection_factory.cc b/sdk/android/src/jni/pc/peer_connection_factory.cc
index 00e207e..378d129 100644
--- a/sdk/android/src/jni/pc/peer_connection_factory.cc
+++ b/sdk/android/src/jni/pc/peer_connection_factory.cc
@@ -23,6 +23,7 @@
 // The user may pass in a nullptr.
 #include "modules/audio_processing/include/audio_processing.h"  // nogncheck
 #include "rtc_base/event_tracer.h"
+#include "rtc_base/system/thread_registry.h"
 #include "rtc_base/thread.h"
 #include "sdk/android/generated_peerconnection_jni/jni/PeerConnectionFactory_jni.h"
 #include "sdk/android/native_api/jni/java_types.h"
@@ -520,5 +521,10 @@
   RTC_LOG(LS_WARNING) << StackTraceToString(GetStackTrace(tid));
 }
 
+static void JNI_PeerConnectionFactory_PrintStackTracesOfRegisteredThreads(
+    JNIEnv* env) {
+  PrintStackTracesOfRegisteredThreads();
+}
+
 }  // namespace jni
 }  // namespace webrtc
diff --git a/video/BUILD.gn b/video/BUILD.gn
index ec6003c..27697fd 100644
--- a/video/BUILD.gn
+++ b/video/BUILD.gn
@@ -102,6 +102,7 @@
     "../rtc_base/experiments:quality_scaling_experiment",
     "../rtc_base/experiments:rate_control_settings",
     "../rtc_base/system:fallthrough",
+    "../rtc_base/system:thread_registry",
     "../rtc_base/task_utils:repeating_task",
     "../rtc_base/task_utils:to_queued_task",
     "../rtc_base/time:timestamp_extrapolator",
diff --git a/video/video_receive_stream.cc b/video/video_receive_stream.cc
index f84bced..1c1c402 100644
--- a/video/video_receive_stream.cc
+++ b/video/video_receive_stream.cc
@@ -43,6 +43,7 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/platform_file.h"
 #include "rtc_base/strings/string_builder.h"
+#include "rtc_base/system/thread_registry.h"
 #include "rtc_base/time_utils.h"
 #include "rtc_base/trace_event.h"
 #include "system_wrappers/include/clock.h"
@@ -556,6 +557,7 @@
 }
 
 void VideoReceiveStream::DecodeThreadFunction(void* ptr) {
+  ScopedRegisterThreadForDebugging thread_dbg(RTC_FROM_HERE);
   while (static_cast<VideoReceiveStream*>(ptr)->Decode()) {
   }
 }