This is an automated email from Gerrit. "Antonio Borneo <borneo.anto...@gmail.com>" just uploaded a new patch set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/6791
-- gerrit commit 535c0aef7160229a1b2e01d0a6edb4df19f2e746 Author: Antonio Borneo <borneo.anto...@gmail.com> Date: Fri Dec 24 01:39:49 2021 +0100 openocd: add helper/oocd_valgrind.h OpenOCD implements queues to pack operations in a burst over the communication channel. E.g. JTAG elementary transfers are queued and flushed in one shot. Enqueued read operations are executed when the queue is flushed; the variable that will contain the read value has undefined value until the flush callback initialises it. Valgrind is already able to identify the use of uninitialized variables in very simple cases, e.g. first use of a local variable or of a malloc'ed area. Valgrind allows to instrument the code to pass further info to the running valgrind. It is possible, for example, to declare a variable as uninitialized. Valgrind documentation suggest to copy the include file valgrind.h in the project, so it will compile also on hosts where valgrind is not installed. But this has the drawback to require a periodic realignment with valgrind code. Let autoconf detect the presence of valgrind. Add a wrapper to valgrind's header files that allows compiling OpenOCD on hosts that have no valgrind installed. Change-Id: I98f5027c8663e33feacfbf9d17e791a854d73d97 Signed-off-by: Antonio Borneo <borneo.anto...@gmail.com> diff --git a/Makefile.am b/Makefile.am index a18c572fb..dd6901166 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,6 +43,11 @@ if INTERNAL_JIMTCL AM_CPPFLAGS += -I$(top_srcdir)/jimtcl \ -I$(top_builddir)/jimtcl endif + +if HAVE_VALGRIND +AM_CPPFLAGS += $(VALGRIND_CFLAGS) +endif + EXTRA_DIST += \ BUGS \ HACKING \ diff --git a/configure.ac b/configure.ac index a178284ee..4f8965411 100644 --- a/configure.ac +++ b/configure.ac @@ -152,7 +152,8 @@ m4_define([SERIAL_PORT_ADAPTERS], [[[buspirate], [Bus Pirate], [BUS_PIRATE]]]) m4_define([OPTIONAL_LIBRARIES], - [[[capstone], [Use Capstone disassembly framework], []]]) + [[[capstone], [Use Capstone disassembly framework], []], + [[valgrind], [Use Valgrind library], []]]) AC_ARG_ENABLE([doxygen-html], AS_HELP_STRING([--disable-doxygen-html], @@ -599,6 +600,26 @@ AS_IF([test "x$enable_capstone" != xno], [ ]) ]) +AC_ARG_WITH([valgrind], + AS_HELP_STRING([--with-valgrind], [Use Valgrind library (default=auto)]) + , [ + enable_valgrind=$withval + ], [ + enable_valgrind=auto +]) + +AS_IF([test "x$enable_valgrind" != xno], [ + PKG_CHECK_MODULES([VALGRIND], [valgrind], [ + AC_DEFINE([HAVE_VALGRIND], [1], [1 if you have Valgrind library.]) + ], [ + AC_DEFINE([HAVE_VALGRIND], [0], [0 if you don't have Valgrind library.]) + if test "x$enable_valgrind" != xauto; then + AC_MSG_ERROR([--with-valgrind was given, but test for Valgrind failed]) + fi + enable_valgrind=no + ]) +]) + for hidapi_lib in hidapi hidapi-hidraw hidapi-libusb; do PKG_CHECK_MODULES([HIDAPI],[$hidapi_lib],[ use_hidapi=yes @@ -709,6 +730,7 @@ AM_CONDITIONAL([USE_HIDAPI], [test "x$use_hidapi" = "xyes"]) AM_CONDITIONAL([USE_LIBJAYLINK], [test "x$use_libjaylink" = "xyes"]) AM_CONDITIONAL([RSHIM], [test "x$build_rshim" = "xyes"]) AM_CONDITIONAL([HAVE_CAPSTONE], [test "x$enable_capstone" != "xno"]) +AM_CONDITIONAL([HAVE_VALGRIND], [test "x$enable_valgrind" != "xno"]) AM_CONDITIONAL([INTERNAL_JIMTCL], [test "x$use_internal_jimtcl" = "xyes"]) AM_CONDITIONAL([INTERNAL_LIBJAYLINK], [test "x$use_internal_libjaylink" = "xyes"]) diff --git a/src/helper/Makefile.am b/src/helper/Makefile.am index c1aeebf00..6e4e31e62 100644 --- a/src/helper/Makefile.am +++ b/src/helper/Makefile.am @@ -28,7 +28,8 @@ noinst_LTLIBRARIES += %D%/libhelper.la %D%/system.h \ %D%/jep106.h \ %D%/jep106.inc \ - %D%/jim-nvp.h + %D%/jim-nvp.h \ + %D%/oocd_valgrind.h STARTUP_TCL_SRCS += %D%/startup.tcl EXTRA_DIST += \ diff --git a/src/helper/oocd_valgrind.h b/src/helper/oocd_valgrind.h new file mode 100644 index 000000000..a3e243911 --- /dev/null +++ b/src/helper/oocd_valgrind.h @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* + * Don't include <valgrind.h> or <memcheck.h> directly. + * Include this file that compiles without Valgrind installed + * + * Extend this file if other Valgrind client's API are needed + */ + +#ifndef OPENOCD_HELPER_OOCD_VALGRIND_H +#define OPENOCD_HELPER_OOCD_VALGRIND_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#if HAVE_VALGRIND + +#include <memcheck.h> + +#else /* HAVE_VALGRIND */ + +/* + * Returns the number of Valgrinds this code is running under. That + * is, 0 if running natively, 1 if running under Valgrind, 2 if + * running under Valgrind which is running under another Valgrind, etc. + */ +#define RUNNING_ON_VALGRIND (0) + +/* Mark memory at addr as addressable but undefined for len bytes */ +#define VALGRIND_MAKE_MEM_UNDEFINED(addr,len) do {} while (0) + +/* + * Print a printf-style message to the Valgrind log file followed by a + * stack backtrace. + */ +#define VALGRIND_PRINTF_BACKTRACE(format, ...) do {} while (0) + +#endif /* HAVE_VALGRIND */ + +#endif /* OPENOCD_HELPER_OOCD_VALGRIND_H */ --