From 3e4c141a6b0ec12b668143a26ecf01da5c151931 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Tue, 19 Jul 2011 18:03:38 -0700 Subject: [PATCH] Merge in backtrace --- libmemcached/assert.hpp | 1 + libmemcached/backtrace.cc | 108 +++++++++++++++++++++++++++++++++++ libmemcached/backtrace.hpp | 41 +++++++++++++ libmemcached/common.h | 11 +++- libmemcached/connect.cc | 1 - libmemcached/include.am | 3 + libmemcached/util/include.am | 1 + libmemcached/util/pool.cc | 2 +- 8 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 libmemcached/backtrace.cc create mode 100644 libmemcached/backtrace.hpp diff --git a/libmemcached/assert.hpp b/libmemcached/assert.hpp index 8a46784e..acd0198e 100644 --- a/libmemcached/assert.hpp +++ b/libmemcached/assert.hpp @@ -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 index 00000000..bc8846ba --- /dev/null +++ b/libmemcached/backtrace.cc @@ -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 + +#include +#include + +#ifdef __GNUC__ +#ifdef HAVE_BACKTRACE +#include +#include +#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 index 00000000..98db320c --- /dev/null +++ b/libmemcached/backtrace.hpp @@ -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); diff --git a/libmemcached/common.h b/libmemcached/common.h index f30589ca..d4b2c7df 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -103,6 +103,7 @@ memcached_return_t memcached_server_execute(memcached_st *ptr, #include #ifdef __cplusplus +#include #include #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; diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index dee0cd7b..8efbb119 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -37,7 +37,6 @@ #include -#include #include #include diff --git a/libmemcached/include.am b/libmemcached/include.am index db612892..1c2036bf 100644 --- a/libmemcached/include.am +++ b/libmemcached/include.am @@ -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 \ diff --git a/libmemcached/util/include.am b/libmemcached/util/include.am index ea80eb8f..9ac9c8c4 100644 --- a/libmemcached/util/include.am +++ b/libmemcached/util/include.am @@ -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 \ diff --git a/libmemcached/util/pool.cc b/libmemcached/util/pool.cc index 17cff077..0a3ef15f 100644 --- a/libmemcached/util/pool.cc +++ b/libmemcached/util/pool.cc @@ -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; } -- 2.30.2