summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2009-04-07 00:09:59 +0200
committerBruno Haible <bruno@clisp.org>2009-04-08 13:02:33 +0200
commit3f1f7a4ed4803548df9656e1c91f980e1bf68724 (patch)
tree119383d984aa4b924fde2eec98d50d15f58c9587
parent38ed6193c913a56183c37dc0dd0f2b9b6a8937ae (diff)
downloadlibunistring-3f1f7a4ed4803548df9656e1c91f980e1bf68724.tar.gz
Support for running valgrind.
-rw-r--r--ChangeLog11
-rw-r--r--HACKING12
-rw-r--r--Makefile.am2
-rwxr-xr-xbuild-aux/run-test139
-rw-r--r--tests/Makefile.am11
5 files changed, 173 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index dd16b5f..ee329fa 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
2008-04-06 Bruno Haible <bruno@clisp.org>
+ Add support for checking with valgrind.
+ * build-aux/run-test: New file.
+ * Makefile.am (EXTRA_DIST): Add it.
+ * tests/Makefile.am (CHECKER, CHECKER_END_OF_COMMENTS): New variables.
+ (TESTS_ENVIRONMENT): Add a run-test invocation.
+ * HACKING: Mention how to use valgrind.
+
+2008-04-06 Bruno Haible <bruno@clisp.org>
+
* Makefile.am (EXTRA_DIST): Add version.sh, DEPENDENCIES, HACKING,
autogen.sh, build-aux/fixaclocal.
* version.sh: New file.
@@ -13,7 +22,7 @@
2008-04-05 Bruno Haible <bruno@clisp.org>
Use libtool versioning.
- * lib/Makefile.am (LTV_CURRENT, LTV_REVISION, LTV_AGE): New macros.
+ * lib/Makefile.am (LTV_CURRENT, LTV_REVISION, LTV_AGE): New variables.
(libunistring_la_LDFLAGS): Pass -version-info option.
2008-04-05 Bruno Haible <bruno@clisp.org>
diff --git a/HACKING b/HACKING
index 4c557c7..d4fbe66 100644
--- a/HACKING
+++ b/HACKING
@@ -42,3 +42,15 @@ Sources
=======
Most of the sources have their origin in gnulib.
+
+
+Running the testsuite in valgrind
+=================================
+
+To run the test suite with a memory access checker and leak detector such as
+valgrind,
+ 1) configure with the options --disable-shared and CFLAGS="-g",
+ 2) run "make",
+ 3) edit tests/Makefile to uncomment the appropriate definition of the CHECKER
+ macro,
+ 4) run "make check".
diff --git a/Makefile.am b/Makefile.am
index 79e32da..c4c9d13 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -24,7 +24,7 @@ SUBDIRS = lib tests
EXTRA_DIST = \
version.sh BUGS DEPENDENCIES HACKING autogen.sh \
- build-aux/fixaclocal
+ build-aux/fixaclocal build-aux/run-test
# Allow users to use "gnulib-tool --update".
diff --git a/build-aux/run-test b/build-aux/run-test
new file mode 100755
index 0000000..cfa7751
--- /dev/null
+++ b/build-aux/run-test
@@ -0,0 +1,139 @@
+#!/bin/sh
+#
+# Copyright (C) 2009 Free Software Foundation, Inc.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+# This program is a test driver that supports running a test under valgrind.
+# Usage: run-test CHECKER PROGRAM [ARGUMENT...]
+
+progname=$0
+
+# func_usage
+# outputs to stdout the --help usage message.
+func_usage ()
+{
+ echo "\
+Usage: run-test [OPTION...] CHECKER PROGRAM [ARGUMENT...]
+
+Runs PROGRAM under the control of CHECKER.
+
+CHECKER may be empty or a valgrind command with some options, such as
+'valgrind --tool=memcheck --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes'.
+
+When CHECKER is not empty, it is recommended that the package has been
+configured with
+ --disable-shared so that tests are real executables and not libtool
+ wrapper scripts, and
+ CFLAGS=\"-g\" so that valgrind shows line numbers.
+
+Report bugs to Bruno Haible."
+}
+
+# func_version
+# outputs to stdout the --version message.
+func_version ()
+{
+ echo "\
+run-test (GNU libunistring)
+Copyright (C) 2009 Free Software Foundation, Inc.
+License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
+This is free software: you are free to change and redistribute it.
+There is NO WARRANTY, to the extent permitted by law.
+
+Written by" "Bruno Haible"
+}
+
+# func_fatal_error message
+# outputs to stderr a fatal error message, and terminates the program.
+# Input:
+# - progname name of this program
+func_fatal_error ()
+{
+ echo "$progname: *** $1" 1>&2
+ echo "$progname: *** Stop." 1>&2
+ func_exit 1
+}
+
+# Command-line option processing.
+# Removes the OPTIONS from the arguments. Sets the variables:
+# - checker wrapper program for executables
+{
+ while test $# -gt 0; do
+ case "$1" in
+ --help | --hel | --he | --h )
+ func_usage
+ exit $? ;;
+ --version | --versio | --versi | --vers | --ver | --ve | --v )
+ func_version
+ exit $? ;;
+ -- )
+ # Stop option processing
+ shift
+ break ;;
+ -* )
+ echo "run-test: unknown option $1" 1>&2
+ echo "Try 'run-test --help' for more information." 1>&2
+ exit 1 ;;
+ * )
+ break ;;
+ esac
+ done
+
+ if test $# -lt 2; then
+ echo "run-test: too few arguments" 1>&2
+ echo "Try 'run-test --help' for more information." 1>&2
+ exit 1
+ fi
+
+ checker="$1"
+ shift
+}
+
+if test -z "$checker"; then
+ # No checker. Run the test directly.
+ exec "$@"
+else
+ # Using valgrind. We want to apply valgrind only to executables, not to
+ # shell script, because
+ # 1. we don't want to look for memory leaks in bash,
+ # 2. on a bi-arch system, we would get an error message such as
+ # "valgrind: wrong executable class (eg. 32-bit instead of 64-bit)".
+ case "$1" in
+ *.sh)
+ # A shell script. Ignore the checker.
+ exec "$@"
+ ;;
+ *)
+ # The 'file' command is not portable enough. So, look
+ # at the first two bytes of the file. Are they '#!'?
+ if { if od -A x < /dev/null >/dev/null 2>/dev/null; then
+ # Use POSIX od.
+ firstbytes=`od -A n -t o1 -N 2 < "$1" | tr -d ' '`
+ else
+ # Use BSD hexdump.
+ firstbytes=`dd if="$1" bs=1 count=2 2>/dev/null | hexdump -e '1/1 "%03o"'`
+ fi
+ test "$firstbytes" = "043041"
+ }; then
+ # A shell script. Ignore the checker.
+ exec "$@"
+ else
+ # An executable. Use the checker.
+ exec $checker "$@"
+ fi
+ ;;
+ esac
+fi
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f9dfa0a..7fbaa85 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -23,3 +23,14 @@ AUTOMAKE_OPTIONS += subdir-objects
# The test suite uses the 'localcharset' module.
TESTS_ENVIRONMENT += @LOCALCHARSET_TESTS_ENVIRONMENT@
+
+
+# For debugging memory leaks and memory allocation bugs.
+# You should build with --disable-shared when using valgrind.
+CHECKER =
+#CHECKER = valgrind --tool=memcheck --suppressions=$(srcdir)/../lib/malloca.valgrind --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes
+#CHECKER = valgrind --tool=massif --format=html --depth=10 --alloc-fn=xmalloc --alloc-fn=xrealloc --stacks=no
+CHECKER_END_OF_COMMENTS =
+
+# This must be the last thing that gets added to TESTS_ENVIRONMENT.
+TESTS_ENVIRONMENT += $(SHELL) $(top_srcdir)/build-aux/run-test '$(CHECKER)'