Support running test suite in valgrind
diff --git a/ChangeLog b/ChangeLog
index 5d48d4a..8cc6b3d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2012-10-03  Petr Machata  <pmachata@redhat.com>
 
+	* configure.ac (--enable-valgrind): New option.
+	(USE_VALGRIND): New variable bound to that option.
+	* testsuite/Makefile.am (valgrind_cmd): Conditionally turn on when
+	--enable-valgrind.  Pass down via RUNTESTDEFAULTFLAGS.
+	* testsuite/lib/ltrace.exp (LTRACE): Adjust if VALGRIND variable
+	is passed.
+
+2012-10-03  Petr Machata  <pmachata@redhat.com>
+
 	* testsuite/ltrace.minor/libdl-simple.exp: Call get_compiler_info
 	before invoking ltrace_compile_shlib.
 
diff --git a/configure.ac b/configure.ac
index a858e06..125c5d2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -260,6 +260,17 @@
     AM_CFLAGS="${AM_CFLAGS} -Werror"
 fi
 
+AC_ARG_ENABLE([valgrind],
+    AS_HELP_STRING([--enable-valgrind],[run all tests under valgrind]),
+    [use_valgrind=$enableval], [use_valgrind=no])
+if test x$use_valgrind = xyes; then
+    AC_CHECK_PROG(HAVE_VALGRIND, valgrind, yes, no)
+    if test x$HAVE_VALGRIND = xno; then
+        AC_MSG_ERROR([valgrind not found])
+    fi
+fi
+AM_CONDITIONAL(USE_VALGRIND, test "$use_valgrind" = yes)
+
 AC_SUBST(AM_CPPFLAGS)
 AC_SUBST(AM_CFLAGS)
 AC_SUBST(AM_LDFLAGS)
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index a4c7cfa..8df707e 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -17,7 +17,11 @@
 
 AUTOMAKE_OPTIONS = dejagnu
 
-RUNTESTDEFAULTFLAGS = --srcdir $(srcdir)
+if USE_VALGRIND
+valgrind_cmd="VALGRIND=valgrind -q --error-exitcode=1"
+endif
+
+RUNTESTDEFAULTFLAGS = --srcdir $(srcdir) $(valgrind_cmd)
 
 SUBDIRS = ltrace.main ltrace.minor ltrace.torture
 
diff --git a/testsuite/lib/ltrace.exp b/testsuite/lib/ltrace.exp
index 8911b9d..c3cb5e1 100644
--- a/testsuite/lib/ltrace.exp
+++ b/testsuite/lib/ltrace.exp
@@ -13,6 +13,11 @@
 	set LTRACE $objdir/../ltrace
 }
 
+if {[info exists VALGRIND] && ![string equal $VALGRIND {}]} {
+	verbose "Running under valgrind command: `$VALGRIND'"
+	set LTRACE "$VALGRIND $LTRACE"
+}
+
 global LTRACE_OPTIONS
 set LTRACE_OPTIONS "";
 global LTRACE_ARGS