Merge in backtrace
authorBrian Aker <brian@tangent.org>
Wed, 20 Jul 2011 01:03:38 +0000 (18:03 -0700)
committerBrian Aker <brian@tangent.org>
Wed, 20 Jul 2011 01:03:38 +0000 (18:03 -0700)
libmemcached/assert.hpp
libmemcached/backtrace.cc [new file with mode: 0644]
libmemcached/backtrace.hpp [new file with mode: 0644]
libmemcached/common.h
libmemcached/connect.cc
libmemcached/include.am
libmemcached/util/include.am
libmemcached/util/pool.cc

index 8a46784ed22803a501e7091e2cb1dc628653fe8e..acd0198e3df52848cefa876a36068ea822f0a730 100644 (file)
@@ -50,6 +50,7 @@ do \
   if (not (__expr)) \
   { \
     fprintf(stderr, "\nAssertion \"%s\" failed for function \"%s\" likely for %s, at %s:%d\n", #__expr, __func__, (#__mesg),  __FILE__, __LINE__);\
+    custom_backtrace(); \
     abort(); \
   } \
 } while (0)
diff --git a/libmemcached/backtrace.cc b/libmemcached/backtrace.cc
new file mode 100644 (file)
index 0000000..bc8846b
--- /dev/null
@@ -0,0 +1,108 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  libmcachedd client library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <libmemcached/common.h>
+
+#include <cstring>
+#include <cstdlib>
+
+#ifdef __GNUC__
+#ifdef HAVE_BACKTRACE
+#include <execinfo.h>
+#include <cxxabi.h>
+#endif // HAVE_BACKTRACE
+#endif // __GNUC__
+
+
+void custom_backtrace(void)
+{
+#ifdef __GNUC__
+#ifdef HAVE_BACKTRACE
+  void *array[50];
+
+  size_t size= backtrace(array, 50);
+  char **strings= backtrace_symbols(array, size);
+
+  fprintf(stderr, "Number of stack frames obtained: %lu\n", (unsigned long)size);
+
+  for (size_t x= 1; x < size; x++) 
+  {
+    size_t sz= 200;
+    char *function= (char *)malloc(sz);
+    char *begin= 0;
+    char *end= 0;
+
+    for (char *j = strings[x]; *j; ++j)
+    {
+      if (*j == '(') {
+        begin = j;
+      }
+      else if (*j == '+') {
+        end = j;
+      }
+    }
+    if (begin && end)
+    {
+      begin++;
+      *end= '\0';
+
+      int status;
+      char *ret = abi::__cxa_demangle(begin, function, &sz, &status);
+      if (ret) 
+      {
+        function= ret;
+      }
+      else
+      {
+        strncpy(function, begin, sz);
+        strncat(function, "()", sz);
+        function[sz-1] = '\0';
+      }
+      fprintf(stderr, "%s\n", function);
+    }
+    else
+    {
+      fprintf(stderr, "%s\n", strings[x]);
+    }
+    free(function);
+  }
+
+
+  free (strings);
+#endif // HAVE_BACKTRACE
+#endif // __GNUC__
+}
diff --git a/libmemcached/backtrace.hpp b/libmemcached/backtrace.hpp
new file mode 100644 (file)
index 0000000..98db320
--- /dev/null
@@ -0,0 +1,41 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  libmcachedd client library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#pragma once
+
+LIBMEMCACHED_LOCAL
+void custom_backtrace(void);
index f30589ca8be21382c9a76daedec406149ff54af5..d4b2c7df3446660f26307541ec29a3560068767d 100644 (file)
@@ -103,6 +103,7 @@ memcached_return_t memcached_server_execute(memcached_st *ptr,
 #include <libmemcached/namespace.h>
 
 #ifdef __cplusplus
+#include <libmemcached/backtrace.hpp>
 #include <libmemcached/assert.hpp>
 #endif
 
@@ -168,20 +169,24 @@ LIBMEMCACHED_LOCAL
 
 static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
 {
-  unlikely (key_length == 0)
+  if (key_length == 0)
   {
     return MEMCACHED_BAD_KEY_PROVIDED;
   }
 
   if (binary)
   {
-    unlikely (key_length > 0xffff)
+    if (key_length > 0xffff)
+    {
       return MEMCACHED_BAD_KEY_PROVIDED;
+    }
   }
   else
   {
-    unlikely (key_length >= MEMCACHED_MAX_KEY)
+    if (key_length >= MEMCACHED_MAX_KEY)
+    {
       return MEMCACHED_BAD_KEY_PROVIDED;
+    }
   }
 
   return MEMCACHED_SUCCESS;
index dee0cd7b1124a67036a014e751d74a5a19469797..8efbb119b30e10c59756a13d0c006af6bb7cb681 100644 (file)
@@ -37,7 +37,6 @@
 
 
 #include <libmemcached/common.h>
-#include <cassert>
 #include <ctime>
 #include <sys/time.h>
 
index db612892245d430561b369b43b978547048741e8..1c2036bf1b05fd909f15719ebac4fbe5d877352c 100644 (file)
@@ -13,6 +13,7 @@ EXTRA_DIST+= \
 
 noinst_HEADERS+= \
                 libmemcached/assert.hpp \
+                libmemcached/backtrace.hpp \
                 libmemcached/byteorder.h \
                 libmemcached/common.h \
                 libmemcached/do.hpp \
@@ -82,6 +83,7 @@ nobase_include_HEADERS+= \
 noinst_LTLIBRARIES+= libmemcached/libmemcachedinternal.la
 libmemcached_libmemcachedinternal_la_SOURCES= \
                                              libmemcached/array.c \
+                                             libmemcached/backtrace.cc \
                                              libmemcached/error.cc \
                                              libmemcached/string.cc
 libmemcached_libmemcachedinternal_la_CFLAGS= \
@@ -111,6 +113,7 @@ libmemcached_libmemcached_la_SOURCES+= \
                                       libmemcached/analyze.cc \
                                       libmemcached/array.c \
                                       libmemcached/auto.cc \
+                                      libmemcached/backtrace.cc \
                                       libmemcached/behavior.cc \
                                       libmemcached/byteorder.cc \
                                       libmemcached/callback.cc \
index ea80eb8fc465a85fb38a43820bc598e61e240c27..9ac9c8c44065c455a729fb9bd8995b10a4e012bc 100644 (file)
@@ -15,6 +15,7 @@ lib_LTLIBRARIES+= libmemcached/libmemcachedutil.la
 endif
 
 libmemcached_libmemcachedutil_la_SOURCES= \
+                                         libmemcached/backtrace.cc \
                                          libmemcached/util/flush.cc \
                                          libmemcached/util/pid.cc \
                                          libmemcached/util/ping.cc \
index 17cff07726e80b49e6a5b921ad3cfa9ae4736cab..0a3ef15f9cb76f0714f96869142a4f87758d852c 100644 (file)
@@ -248,7 +248,7 @@ memcached_st* memcached_pool_pop(memcached_pool_st* pool,
     return NULL;
   }
 
-  if ((*rc= mutex_enter(&pool->mutex)) != MEMCACHED_SUCCESS)
+  if (memcached_failed((*rc= mutex_enter(&pool->mutex))))
   {
     return NULL;
   }