Bug 1393710 - Add thread name into NS_DebugBreak for better debugging. draft
authorJames Cheng <jacheng@mozilla.com>
Wed, 30 Aug 2017 11:13:50 +0800
changeset 662966 276cd9a336efd83a9a9816f51eced4d42092749d
parent 662965 70623a09d7ede6559a957eb2f6705516ff46af86
child 731033 99d57d24cef81b7711536f96a2719e692381d420
push id79255
push userbmo:jacheng@mozilla.com
push dateTue, 12 Sep 2017 09:43:17 +0000
bugs1393710
milestone57.0a1
Bug 1393710 - Add thread name into NS_DebugBreak for better debugging. 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()
 {