Bug 1393710 - Add thread name into NS_DebugBreak for better debugging. r=froydnj
authorJames Cheng <jacheng@mozilla.com>
Wed, 30 Aug 2017 11:13:50 +0800
changeset 429969 a104181ac18b7d25cb058790d09a024d24f0a333
parent 429968 cabd92c1a177a2c55f057572b1e04c5b64051698
child 429970 f2bd9e8d0975b435bd437b001d94200d43cab5ff
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1393710
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1393710 - Add thread name into NS_DebugBreak for better debugging. r=froydnj MozReview-Commit-ID: AvqajMgtpuh
xpcom/base/nsDebugImpl.cpp
xpcom/threads/MainThreadUtils.h
xpcom/threads/nsThreadManager.cpp
--- a/xpcom/base/nsDebugImpl.cpp
+++ b/xpcom/base/nsDebugImpl.cpp
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 // Chromium headers must come before Mozilla headers.
 #include "base/process_util.h"
 
 #include "mozilla/Atomics.h"
 #include "mozilla/Printf.h"
 
+#include "MainThreadUtils.h"
 #include "nsDebugImpl.h"
 #include "nsDebug.h"
 #ifdef MOZ_CRASHREPORTER
 # include "nsExceptionHandler.h"
 #endif
 #include "nsString.h"
 #include "nsXULAppAPI.h"
 #include "prprf.h"
@@ -347,18 +348,27 @@ NS_DebugBreak(uint32_t aSeverity, const 
     nonPIDBuf.print("line %d", aLine);
   }
 
   // Print "[PID]" or "[Desc PID]" at the beginning of the message.
   buf.print("[");
   if (sMultiprocessDescription) {
     buf.print("%s ", sMultiprocessDescription);
   }
-  buf.print("%d] %s", base::GetCurrentProcId(), nonPIDBuf.buffer);
 
+  bool isMainthread = (NS_IsMainThreadTLSInitialized() && NS_IsMainThread());
+  PRThread *currentThread = PR_GetCurrentThread();
+  const char *currentThreadName = isMainthread
+    ? "Main Thread"
+    : PR_GetThreadName(currentThread);
+  if(currentThreadName) {
+    buf.print("%d, %s] %s", base::GetCurrentProcId(), currentThreadName , nonPIDBuf.buffer);
+  } else {
+    buf.print("%d, Unnamed thread %p] %s", base::GetCurrentProcId(), currentThread, nonPIDBuf.buffer);
+  }
 
   // errors on platforms without a debugdlg ring a bell on stderr
 #if !defined(XP_WIN)
   if (aSeverity != NS_DEBUG_WARNING) {
     fprintf(stderr, "\07");
   }
 #endif
 
--- a/xpcom/threads/MainThreadUtils.h
+++ b/xpcom/threads/MainThreadUtils.h
@@ -23,12 +23,13 @@ extern nsresult NS_GetMainThread(nsIThre
 // Fast access to the current thread.  Do not release the returned pointer!  If
 // you want to use this pointer from some other thread, then you will need to
 // AddRef it.  Otherwise, you should only consider this pointer valid from code
 // running on the current thread.
 extern nsIThread* NS_GetCurrentThread();
 #endif
 
 #ifdef MOZILLA_INTERNAL_API
+bool NS_IsMainThreadTLSInitialized();
 bool NS_IsMainThread();
 #endif
 
 #endif // MainThreadUtils_h_
--- a/xpcom/threads/nsThreadManager.cpp
+++ b/xpcom/threads/nsThreadManager.cpp
@@ -29,16 +29,22 @@
 #include "InputEventStatistics.h"
 
 using namespace mozilla;
 
 static MOZ_THREAD_LOCAL(bool) sTLSIsMainThread;
 static MOZ_THREAD_LOCAL(PRThread*) gTlsCurrentVirtualThread;
 
 bool
+NS_IsMainThreadTLSInitialized()
+{
+  return sTLSIsMainThread.initialized();
+}
+
+bool
 NS_IsMainThread()
 {
   return sTLSIsMainThread.get();
 }
 
 void
 NS_SetMainThread()
 {