--------------------- PatchSet 3663 Date: 2006/09/02 22:05:51 Author: hno Branch: valgrind Tag: (none) Log: Forward port of valgrind glue Members: configure.in:1.112->1.112.4.1 include/config.h:1.12->1.12.4.1 lib/MemPool.cc:1.3->1.3.4.1 src/cbdata.cc:1.23.4.1->1.23.4.2 src/comm_epoll.cc:1.12->1.12.4.1 src/main.cc:1.71->1.71.4.1 src/mem.cc:1.32->1.32.4.1 src/squid.h:1.29->1.29.4.1 Index: squid3/configure.in =================================================================== RCS file: /cvsroot/squid-sf//squid3/configure.in,v retrieving revision 1.112 retrieving revision 1.112.4.1 diff -u -r1.112 -r1.112.4.1 --- squid3/configure.in 20 Aug 2006 18:51:29 -0000 1.112 +++ squid3/configure.in 2 Sep 2006 22:05:51 -0000 1.112.4.1 @@ -1,7 +1,7 @@ dnl Configuration input file for Squid dnl -dnl $Id: configure.in,v 1.112 2006/08/20 18:51:29 squidadm Exp $ +dnl $Id: configure.in,v 1.112.4.1 2006/09/02 22:05:51 hno Exp $ dnl dnl dnl @@ -11,7 +11,7 @@ AC_CONFIG_AUX_DIR(cfgaux) AC_CONFIG_SRCDIR([src/main.cc]) AM_INIT_AUTOMAKE([tar-ustar]) -AC_REVISION($Revision: 1.112 $)dnl +AC_REVISION($Revision: 1.112.4.1 $)dnl AC_PREFIX_DEFAULT(/usr/local/squid) AM_MAINTAINER_MODE @@ -1643,6 +1643,26 @@ fi AC_SUBST(EXTERNAL_ACL_HELPERS) +AC_ARG_WITH(valgrind, +[ --with-valgrind Include debug instrumentation for use with valgrind], +[ case $withval in + yes) + valgrind=1 + ;; + no) + valgrind= + ;; + *) + CPPFLAGS="$CPPFLAGS -I${enableval}/include" + valgrind=1 + ;; + esac + if test $valgrind; then + AC_DEFINE(WITH_VALGRIND, 1, [Valgrind memory debugger support]) + echo "Valgrind debug support enabled" + fi +]) + dnl Disable "memPools" code AC_DEFINE(DISABLE_POOLS, 0, [Define if you have problems with memPools and want to disable Pools.]) AC_ARG_ENABLE(mempools, Index: squid3/include/config.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/include/config.h,v retrieving revision 1.12 retrieving revision 1.12.4.1 diff -u -r1.12 -r1.12.4.1 --- squid3/include/config.h 28 Aug 2006 10:50:39 -0000 1.12 +++ squid3/include/config.h 2 Sep 2006 22:05:52 -0000 1.12.4.1 @@ -1,5 +1,5 @@ /* - * $Id: config.h,v 1.12 2006/08/28 10:50:39 squidadm Exp $ + * $Id: config.h,v 1.12.4.1 2006/09/02 22:05:52 hno Exp $ * * AUTHOR: Duane Wessels * @@ -420,4 +420,31 @@ #define PRINTF_FORMAT_ARG3 #endif +/* + * Determine if this is a leak check build or standard + */ +#if PURIFY +#define LEAK_CHECK_MODE 1 +#elif WITH_VALGRIND +#define LEAK_CHECK_MODE 1 +#elif XMALLOC_TRACE +#define LEAK_CHECK_MODE 1 +#endif + +/* + * valgrind debug support + */ +#if WITH_VALGRIND +#include +#else +#define VALGRIND_MAKE_NOACCESS(a,b) (0) +#define VALGRIND_MAKE_WRITABLE(a,b) (0) +#define VALGRIND_MAKE_READABLE(a,b) (0) +#define VALGRIND_CHECK_WRITABLE(a,b) (0) +#define VALGRIND_CHECK_READABLE(a,b) (0) +#define VALGRIND_MALLOCLIKE_BLOCK(a,b,c,d) +#define VALGRIND_FREELIKE_BLOCK(a,b) +#define RUNNING_ON_VALGRIND 0 +#endif /* WITH_VALGRIND */ + #endif /* SQUID_CONFIG_H */ Index: squid3/lib/MemPool.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/lib/MemPool.cc,v retrieving revision 1.3 retrieving revision 1.3.4.1 diff -u -r1.3 -r1.3.4.1 --- squid3/lib/MemPool.cc 18 Jun 2006 09:50:52 -0000 1.3 +++ squid3/lib/MemPool.cc 2 Sep 2006 22:05:52 -0000 1.3.4.1 @@ -1,6 +1,6 @@ /* - * $Id: MemPool.cc,v 1.3 2006/06/18 09:50:52 squidadm Exp $ + * $Id: MemPool.cc,v 1.3.4.1 2006/09/02 22:05:52 hno Exp $ * * DEBUG: section 63 Low Level Memory Pool Management * AUTHOR: Alex Rousskov, Andres Kroonmaa, Robert Collins @@ -208,7 +208,9 @@ for (int i = 1; i < pool->chunk_capacity; i++) { *Free = (void *) ((char *) Free + pool->obj_size); - Free = (void **)*Free; + void *nexFree = (void **)*Free; + (void) VALGRIND_MAKE_NOACCESS(Free, pool->obj_size); + Free = nextFree; } nextFreeChunk = pool->nextFreeChunk; pool->nextFreeChunk = this; @@ -273,6 +275,7 @@ Free = (void **)obj; *Free = freeCache; freeCache = obj; + (void) VALGRIND_MAKE_NOACCESS(obj, obj_size); } /* @@ -289,6 +292,7 @@ /* first, try cache */ if (freeCache) { Free = (void **)freeCache; + (void) VALGRIND_MAKE_READABLE(Free, obj_size); freeCache = *Free; *Free = NULL; return Free; @@ -311,6 +315,7 @@ /* last free in this chunk, so remove us from perchunk freelist chain */ nextFreeChunk = chunk->nextFreeChunk; } + (void) VALGRIND_MAKE_READABLE(Free, obj_size); return Free; } @@ -351,7 +356,7 @@ * MemPools::GetInstance().setDefaultPoolChunking() can be called. */ MemPools::MemPools() : pools(NULL), mem_idle_limit(2 * MB), - poolCount (0), defaultIsChunked (!DISABLE_POOLS) + poolCount (0), defaultIsChunked (!DISABLE_POOLS && !RUNNING_ON_VALGRIND) { #if HAVE_MALLOPT && M_MMAP_MAX mallopt(M_MMAP_MAX, MEM_MAX_MMAP_CHUNKS); @@ -535,6 +540,7 @@ MemImplementingAllocator::free(void *obj) { assert(obj != NULL); + (void) VALGRIND_CHECK_WRITABLE(obj, obj_size); deallocate(obj); ++free_calls; } @@ -573,8 +579,10 @@ assert(splayLastResult == 0); assert(chunk->inuse_count > 0); chunk->inuse_count--; + (void) VALGRIND_MAKE_READABLE(Free, sizeof(void *)); freeCache = *(void **)Free; /* remove from global cache */ *(void **)Free = chunk->freeList; /* stuff into chunks freelist */ + (void) VALGRIND_MAKE_NOACCESS(Free, sizeof(void *)); chunk->freeList = Free; chunk->lastref = squid_curtime; } Index: squid3/src/cbdata.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/cbdata.cc,v retrieving revision 1.23.4.1 retrieving revision 1.23.4.2 diff -u -r1.23.4.1 -r1.23.4.2 --- squid3/src/cbdata.cc 2 Sep 2006 03:01:36 -0000 1.23.4.1 +++ squid3/src/cbdata.cc 2 Sep 2006 22:05:52 -0000 1.23.4.2 @@ -1,6 +1,6 @@ /* - * $Id: cbdata.cc,v 1.23.4.1 2006/09/02 03:01:36 hno Exp $ + * $Id: cbdata.cc,v 1.23.4.2 2006/09/02 22:05:52 hno Exp $ * * DEBUG: section 45 Callback Data Registry * ORIGINAL AUTHOR: Duane Wessels @@ -120,10 +120,12 @@ /* cookie used while debugging */ long cookie; - /* MUST be the last per-instance member */ /* TODO: examine making cbdata templated on this - so we get type * safe access to data - RBC 20030902 */ +#if !HASHED_CBDATA + /* MUST be the last per-instance member */ void *data; +#endif void check(int line) const {assert(cookie == ((long)this ^ Cookie));} size_t dataSize() const { return sizeof(data);} Index: squid3/src/comm_epoll.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/comm_epoll.cc,v retrieving revision 1.12 retrieving revision 1.12.4.1 diff -u -r1.12 -r1.12.4.1 --- squid3/src/comm_epoll.cc 29 May 2006 21:50:26 -0000 1.12 +++ squid3/src/comm_epoll.cc 2 Sep 2006 22:05:52 -0000 1.12.4.1 @@ -1,6 +1,6 @@ /* - * $Id: comm_epoll.cc,v 1.12 2006/05/29 21:50:26 squidadm Exp $ + * $Id: comm_epoll.cc,v 1.12.4.1 2006/09/02 22:05:52 hno Exp $ * * DEBUG: section 5 Socket functions * @@ -136,6 +136,10 @@ debug(5, DEBUG_EPOLL ? 0 : 8) ("commSetSelect(FD %d,type=%u,handler=%p,client_data=%p,timeout=%ld)\n", fd,type,handler,client_data,timeout); + if (RUNNING_ON_VALGRIND) { + /* Keep valgrind happy.. complains about uninitialized bytes otherwise */ + memset(&ev, 0, sizeof(ev)); + } ev.events = 0; ev.data.fd = fd; Index: squid3/src/main.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/main.cc,v retrieving revision 1.71 retrieving revision 1.71.4.1 diff -u -r1.71 -r1.71.4.1 --- squid3/src/main.cc 21 Aug 2006 01:51:50 -0000 1.71 +++ squid3/src/main.cc 2 Sep 2006 22:05:52 -0000 1.71.4.1 @@ -1,6 +1,6 @@ /* - * $Id: main.cc,v 1.71 2006/08/21 01:51:50 squidadm Exp $ + * $Id: main.cc,v 1.71.4.1 2006/09/02 22:05:52 hno Exp $ * * DEBUG: section 1 Startup and Main Loop * AUTHOR: Harvest Derived @@ -1682,7 +1682,7 @@ Store::Root().sync(); /* Flush log close */ StoreFileSystem::FreeAllFs(); DiskIOModule::FreeAllModules(); -#if PURIFY || XMALLOC_TRACE +#if LEAK_CHECK_MODE configFreeMemory(); storeFreeMemory(); Index: squid3/src/mem.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/mem.cc,v retrieving revision 1.32 retrieving revision 1.32.4.1 diff -u -r1.32 -r1.32.4.1 --- squid3/src/mem.cc 7 Aug 2006 02:51:14 -0000 1.32 +++ squid3/src/mem.cc 2 Sep 2006 22:05:52 -0000 1.32.4.1 @@ -1,6 +1,6 @@ /* - * $Id: mem.cc,v 1.32 2006/08/07 02:51:14 squidadm Exp $ + * $Id: mem.cc,v 1.32.4.1 2006/09/02 22:05:52 hno Exp $ * * DEBUG: section 13 High Level Memory Pool Management * AUTHOR: Harvest Derived @@ -138,6 +138,21 @@ Report(stream); memStringStats(stream); memBufStats(stream); +#if WITH_VALGRIND + if (RUNNING_ON_VALGRIND) { + long int leaked = 0, dubious = 0, reachable = 0, suppressed = 0; + stream << "Valgrind Report:\n"; + stream << "Type\tAmount\n"); + debug(13, 1) ("Asking valgrind for memleaks\n"); + VALGRIND_DO_LEAK_CHECK; + debug(13, 1) ("Getting valgrind statistics\n"); + VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed); + stream << "Leaked\t" << leaked << "\n"; + stream << "Dubious\t" << dubious << "\n"; + stream << "Reachable\t" << reachable << "\n"; + stream << "Suppressed\t" << suppressed << "\n"; + } +#endif stream.flush(); } Index: squid3/src/squid.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/squid.h,v retrieving revision 1.29 retrieving revision 1.29.4.1 diff -u -r1.29 -r1.29.4.1 --- squid3/src/squid.h 28 Aug 2006 10:50:39 -0000 1.29 +++ squid3/src/squid.h 2 Sep 2006 22:05:52 -0000 1.29.4.1 @@ -1,6 +1,6 @@ /* - * $Id: squid.h,v 1.29 2006/08/28 10:50:39 squidadm Exp $ + * $Id: squid.h,v 1.29.4.1 2006/09/02 22:05:52 hno Exp $ * * AUTHOR: Duane Wessels * @@ -304,7 +304,7 @@ #define SA_RESETHAND SA_ONESHOT #endif -#if PURIFY +#if LEACK_CHECK_MODE #define LOCAL_ARRAY(type,name,size) \ static type *local_##name=NULL; \ type *name = local_##name ? local_##name : \