Fix dlerror(3).

Add unit tests for dlerror(3) in various situations. I think We're at least
as good as glibc now.

Also factor out the ScopedPthreadMutexLock and use it here too.

Bug: http://code.google.com/p/android/issues/detail?id=38398
Change-Id: I040938b4366ab836e3df46d1d8055b92f4ea6ed8
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 998e608..6e1c604 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -43,6 +43,7 @@
 // Private C library headers.
 #include <private/bionic_tls.h>
 #include <private/logd.h>
+#include <private/ScopedPthreadMutexLocker.h>
 
 #include "linker.h"
 #include "linker_debug.h"
@@ -161,12 +162,9 @@
 
 static char tmp_err_buf[768];
 static char __linker_dl_err_buf[768];
-#define BASENAME(s) (strrchr(s, '/') != NULL ? strrchr(s, '/') + 1 : s)
 #define DL_ERR(fmt, x...) \
     do { \
-        format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), \
-                      "%s(%s:%d): " fmt, \
-                      __FUNCTION__, BASENAME(__FILE__), __LINE__, ##x); \
+        format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), fmt, ##x); \
         ERROR(fmt "\n", ##x); \
     } while(0)
 
@@ -185,7 +183,7 @@
                                   RT_CONSISTENT, 0};
 static link_map* r_debug_tail = 0;
 
-static pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
 
 static void insert_soinfo_into_debug_map(soinfo * info) {
     // Copy the necessary fields into the debug structure.
@@ -232,7 +230,7 @@
         return;
     }
 
-    pthread_mutex_lock(&_r_debug_lock);
+    ScopedPthreadMutexLocker locker(&gDebugMutex);
 
     _r_debug.r_state = RT_ADD;
     rtld_db_dlactivity();
@@ -241,8 +239,6 @@
 
     _r_debug.r_state = RT_CONSISTENT;
     rtld_db_dlactivity();
-
-    pthread_mutex_unlock(&_r_debug_lock);
 }
 
 static void notify_gdb_of_unload(soinfo* info) {
@@ -251,7 +247,7 @@
         return;
     }
 
-    pthread_mutex_lock(&_r_debug_lock);
+    ScopedPthreadMutexLocker locker(&gDebugMutex);
 
     _r_debug.r_state = RT_DELETE;
     rtld_db_dlactivity();
@@ -260,8 +256,6 @@
 
     _r_debug.r_state = RT_CONSISTENT;
     rtld_db_dlactivity();
-
-    pthread_mutex_unlock(&_r_debug_lock);
 }
 
 extern "C" void notify_gdb_of_libraries()
@@ -345,7 +339,7 @@
  *
  * Intended to be called by libc's __gnu_Unwind_Find_exidx().
  *
- * This function is exposed via dlfcn.c and libdl.so.
+ * This function is exposed via dlfcn.cpp and libdl.so.
  */
 _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
 {