Skip to content

Commit d4a64e3

Browse files
JanMatCodasipborneoa
authored andcommitted
autoconf: Add support for code coverage
Add support for code coverage collection. This helps developers to check if their test scenarios really exercised all the OpenOCD functionality that they intended to test. - Option --enable-gcov has been added to configure.ac which enables the coverage collection using Gcov. (Disabled by default.) - The steps to collect and inspect the coverage have been described in HACKING file. Change-Id: I259e401937a255e7ad7f155359a0b7787e4d0752 Signed-off-by: Jan Matyas <[email protected]> Reviewed-on: https://review.openocd.org/c/openocd/+/8521 Tested-by: jenkins Reviewed-by: Evgeniy Naydanov <[email protected]> Reviewed-by: Antonio Borneo <[email protected]>
1 parent fd62626 commit d4a64e3

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
src/jtag/minidriver_imp.h
1616
src/jtag/jtag_minidriver.h
1717

18+
# coverage files (gcov)
19+
*.gcda
20+
*.gcno
21+
1822
# OpenULINK driver files generated by SDCC
1923
src/jtag/drivers/OpenULINK/*.rel
2024
src/jtag/drivers/OpenULINK/*.asm

HACKING

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ patch:
9292
make
9393
@endcode
9494

95+
- Code coverage analysis
96+
97+
By inspecting the code coverage, you can identify potential gaps in your testing
98+
and use that information to improve your test scenarios.
99+
100+
Example usage:
101+
@code
102+
mkdir build-gcov; cd build-gcov
103+
../configure --enable-gcov [...]
104+
make
105+
# ... Now execute your test scenarios to collect OpenOCD code coverage ...
106+
lcov --capture --directory ./src --output-file openocd-coverage.info
107+
genhtml openocd-coverage.info --output-directory coverage_report
108+
# ... Open coverage_report/index.html in a web browser ...
109+
@endcode
110+
95111
Please consider performing these additional checks where appropriate
96112
(especially Clang Static Analyzer for big portions of new code) and
97113
mention the results (e.g. "Valgrind-clean, no new Clang analyzer

Makefile.am

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ endif
3838

3939
# common flags used in openocd build
4040
AM_CFLAGS = $(GCC_WARNINGS)
41+
AM_LDFLAGS =
4142

4243
AM_CPPFLAGS = $(HOST_CPPFLAGS)\
4344
-I$(top_srcdir)/src \
@@ -51,6 +52,12 @@ AM_CPPFLAGS += -I$(top_srcdir)/jimtcl \
5152
else
5253
AM_CPPFLAGS += $(JIMTCL_CFLAGS)
5354
endif
55+
56+
if USE_GCOV
57+
AM_CFLAGS += --coverage
58+
AM_LDFLAGS += --coverage
59+
endif
60+
5461
EXTRA_DIST += \
5562
BUGS \
5663
HACKING \

configure.ac

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ m4_define([DUMMY_ADAPTER],
171171
m4_define([OPTIONAL_LIBRARIES],
172172
[[[capstone], [Use Capstone disassembly framework], []]])
173173

174+
m4_define([COVERAGE],
175+
[[[gcov], [Collect coverage using gcov], []]])
176+
174177
AC_ARG_ENABLE([doxygen-html],
175178
AS_HELP_STRING([--disable-doxygen-html],
176179
[Disable building Doxygen manual as HTML.]),
@@ -199,6 +202,19 @@ AC_ARG_ENABLE([werror],
199202
AS_HELP_STRING([--disable-werror], [Do not treat warnings as errors]),
200203
[gcc_werror=$enableval], [gcc_werror=$gcc_warnings])
201204

205+
AC_ARG_ENABLE([gcov],
206+
AS_HELP_STRING([--enable-gcov], [Enable runtime coverage collection via gcov]),
207+
[enable_gcov=$enableval], [enable_gcov=no])
208+
209+
AS_IF([test "x$enable_gcov" = "xyes"], [
210+
AC_DEFINE([USE_GCOV], [1], [1 to enable coverage collection using gcov.])
211+
dnl When collecting coverage, disable optimizations.
212+
dnl This overrides the "-O2" that autoconf uses by default:
213+
CFLAGS+=" -O0"
214+
], [
215+
AC_DEFINE([USE_GCOV], [0], [0 to leave coverage collection disabled.])
216+
])
217+
202218
# set default verbose options, overridden by following options
203219
debug_usb_io=no
204220
debug_usb_comms=no
@@ -787,6 +803,8 @@ AM_CONDITIONAL([INTERNAL_JIMTCL], [test "x$use_internal_jimtcl" = "xyes"])
787803
AM_CONDITIONAL([HAVE_JIMTCL_PKG_CONFIG], [test "x$have_jimtcl_pkg_config" = "xyes"])
788804
AM_CONDITIONAL([INTERNAL_LIBJAYLINK], [test "x$use_internal_libjaylink" = "xyes"])
789805

806+
AM_CONDITIONAL([USE_GCOV], [test "x$enable_gcov" = "xyes"])
807+
790808
# Look for environ alternatives. Possibility #1: is environ in unistd.h or stdlib.h?
791809
AC_MSG_CHECKING([for environ in unistd.h and stdlib.h])
792810
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
@@ -862,7 +880,8 @@ m4_foreach([adapter], [USB1_ADAPTERS,
862880
LIBGPIOD_ADAPTERS,
863881
LIBJAYLINK_ADAPTERS, PCIE_ADAPTERS, SERIAL_PORT_ADAPTERS,
864882
DUMMY_ADAPTER,
865-
OPTIONAL_LIBRARIES],
883+
OPTIONAL_LIBRARIES,
884+
COVERAGE],
866885
[s=m4_format(["%-40s"], ADAPTER_DESC([adapter]))
867886
AS_CASE([$ADAPTER_VAR([adapter])],
868887
[auto], [

src/openocd.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,13 @@ int openocd_main(int argc, char *argv[])
375375

376376
log_exit();
377377

378+
#if USE_GCOV
379+
/* Always explicitly dump coverage data before terminating.
380+
* Otherwise coverage would not be dumped when exit_on_signal occurs. */
381+
void __gcov_dump(void);
382+
__gcov_dump();
383+
#endif
384+
378385
if (ret == ERROR_FAIL)
379386
return EXIT_FAILURE;
380387
else if (ret != ERROR_OK)

0 commit comments

Comments
 (0)