[Lldb-commits] [lldb] r149135 - /lldb/trunk/source/Host/common/Mutex.cpp

Greg Clayton gclayton at apple.com
Fri Jan 27 10:29:47 PST 2012


Author: gclayton
Date: Fri Jan 27 12:29:47 2012
New Revision: 149135

URL: http://llvm.org/viewvc/llvm-project?rev=149135&view=rev
Log:
Enable extra error checking for debug builds in our mutexes by
watching for errors from pthread_mutex_destroy () (usually "Resource
busy" errors for when you have a mutex locked and try to destroy
it), and pthread_mutex_lock, and pthread_mutex_unlock (usually for
trying to lock an invalid mutex that might have possible already
been freed).


Modified:
    lldb/trunk/source/Host/common/Mutex.cpp

Modified: lldb/trunk/source/Host/common/Mutex.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Mutex.cpp?rev=149135&r1=149134&r2=149135&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Mutex.cpp (original)
+++ lldb/trunk/source/Host/common/Mutex.cpp Fri Jan 27 12:29:47 2012
@@ -8,6 +8,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/Host/Mutex.h"
+#include "lldb/Host/Host.h"
+
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
 
 #if 0
 // This logging is way too verbose to enable even for a log channel. 
@@ -19,6 +24,11 @@
 #define DEBUG_LOG(fmt, ...)
 #endif
 
+// Enable extra mutex error checking
+#ifdef LLDB_CONFIGURATION_DEBUG
+#define ENABLE_MUTEX_ERROR_CHECKING 1
+#endif
+
 using namespace lldb_private;
 
 //----------------------------------------------------------------------
@@ -145,7 +155,11 @@
     switch (type)
     {
     case eMutexTypeNormal:
+#if ENABLE_MUTEX_ERROR_CHECKING
+        err = ::pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK);
+#else
         err = ::pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_NORMAL);
+#endif
         break;
 
     case eMutexTypeRecursive:
@@ -172,6 +186,14 @@
 {
     int err;
     err = ::pthread_mutex_destroy (&m_mutex);
+#if ENABLE_MUTEX_ERROR_CHECKING
+    if (err)
+    {
+        Host::SetCrashDescriptionWithFormat ("%s error: pthread_mutex_destroy() => err = %i (%s)", __PRETTY_FUNCTION__, err, strerror(err));
+        assert(err == 0);
+    }
+    memset (&m_mutex, '\xba', sizeof(m_mutex));
+#endif
 }
 
 //----------------------------------------------------------------------
@@ -188,6 +210,13 @@
 {
     DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_lock (%p)...\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr);
     int err = ::pthread_mutex_lock (mutex_ptr);
+#if ENABLE_MUTEX_ERROR_CHECKING
+    if (err)
+    {
+        Host::SetCrashDescriptionWithFormat ("%s error: pthread_mutex_destroy(%p) => err = %i (%s)", __PRETTY_FUNCTION__, mutex_ptr, err, strerror(err));
+        assert(err == 0);
+    }
+#endif
     DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_lock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr, err);
     return err;
 }
@@ -204,6 +233,13 @@
 Mutex::Unlock (pthread_mutex_t *mutex_ptr)
 {
     int err = ::pthread_mutex_unlock (mutex_ptr);
+#if ENABLE_MUTEX_ERROR_CHECKING
+    if (err)
+    {
+        Host::SetCrashDescriptionWithFormat ("%s error: pthread_mutex_unlock(%p) => err = %i (%s)", __PRETTY_FUNCTION__, mutex_ptr, err, strerror(err));
+        assert(err == 0);
+    }
+#endif
     DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_unlock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr, err);
     return err;
 }





More information about the lldb-commits mailing list