diff --git a/Makefile.rules b/Makefile.rules
index 03bc7e81ae867c6e66e2aab9d6071873a153dad3..45f7e4bb0d949adfca2d8a91a07da68b3c8559d8 100644
--- a/Makefile.rules
+++ b/Makefile.rules
@@ -43,12 +43,17 @@ ifneq ($(findstring darwin,$(OSARCH)),)
   endif
 endif
 
-ifeq ($(findstring DONT_OPTIMIZE,$(MENUSELECT_CFLAGS)),)
+ifeq ($(findstring DONT_OPTIMIZE,$(MENUSELECT_CFLAGS))$(AST_CODE_COVERAGE),no)
     _ASTCFLAGS+=$(OPTIMIZE)
 else
     _ASTCFLAGS+=-O0
 endif
 
+ifeq ($(AST_CODE_COVERAGE),yes)
+    _ASTCFLAGS+=-ftest-coverage -fprofile-arcs
+    _ASTLDFLAGS+=-ftest-coverage -fprofile-arcs
+endif
+
 ifeq ($(findstring $(CONFIG_CFLAGS),$(_ASTCFLAGS)),)
     _ASTCFLAGS+=$(CONFIG_CFLAGS)
 endif
diff --git a/configure b/configure
index ed3f460fc5c3dd5ae3056f6e47d328651ca8cb79..7f40f51348852b218f7768742875493c90e5f279 100755
--- a/configure
+++ b/configure
@@ -1,5 +1,5 @@
 #! /bin/sh
-# From configure.ac Revision: 279624 .
+# From configure.ac Revision: 279659 .
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.65 for asterisk trunk.
 #
@@ -1008,6 +1008,7 @@ PBX_ALSA
 ALSA_DIR
 ALSA_INCLUDE
 ALSA_LIB
+AST_CODE_COVERAGE
 AST_DEVMODE
 NOISY_BUILD
 PTHREAD_CFLAGS
@@ -1132,6 +1133,7 @@ ac_user_opts='
 enable_option_checking
 with_gnu_ld
 enable_dev_mode
+enable_coverage
 with_asound
 with_avcodec
 with_bluetooth
@@ -1834,6 +1836,7 @@ Optional Features:
   --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
   --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
   --enable-dev-mode       Turn on developer mode
+  --enable-coverage       Turn on code coverage tracking (for gcov)
   --disable-largefile     omit support for large files
   --enable-internal-poll  Use Asterisk's poll implementation
   --disable-xmldoc        Explicity disable XML documentation
@@ -8473,6 +8476,18 @@ fi
 
 
 
+AST_CODE_COVERAGE=no
+# Check whether --enable-coverage was given.
+if test "${enable_coverage+set}" = set; then :
+  enableval=$enable_coverage; case "${enableval}" in
+	      y|ye|yes) AST_CODE_COVERAGE=yes ;;
+	      n|no) AST_CODE_COVERAGE=no ;;
+	      *) as_fn_error "bad value ${enableval} for --enable-coverage" "$LINENO" 5  ;;
+	esac
+fi
+
+
+
 # AST_EXT_LIB_SETUP is used to tell configure to handle variables for
 # various packages.
 # $1 is the prefix for the variables in makeopts and autoconfig.h
diff --git a/configure.ac b/configure.ac
index b0adfd7611c843dd15b81fb544212f2c096617f1..dfc45a5e83d0e33d6018f14395108a7bea5cd81d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -288,6 +288,17 @@ AC_ARG_ENABLE([dev-mode],
 AC_SUBST(NOISY_BUILD)
 AC_SUBST(AST_DEVMODE)
 
+AST_CODE_COVERAGE=no
+AC_ARG_ENABLE([coverage],
+	[AS_HELP_STRING([--enable-coverage],
+		[Turn on code coverage tracking (for gcov)])],
+	[case "${enableval}" in
+	      y|ye|yes) AST_CODE_COVERAGE=yes ;;
+	      n|no) AST_CODE_COVERAGE=no ;;
+	      *) AC_MSG_ERROR(bad value ${enableval} for --enable-coverage)  ;;
+	esac])
+AC_SUBST(AST_CODE_COVERAGE)
+
 # AST_EXT_LIB_SETUP is used to tell configure to handle variables for
 # various packages.
 # $1 is the prefix for the variables in makeopts and autoconfig.h
diff --git a/makeopts.in b/makeopts.in
index e425dbc3b3459d67972decfdaf906f9ba51ec0cd..750a41d7c25a3599d57e8b27c84e480d7a8129fb 100644
--- a/makeopts.in
+++ b/makeopts.in
@@ -79,6 +79,8 @@ sysconfdir = @sysconfdir@
 AST_DEVMODE=@AST_DEVMODE@
 NOISY_BUILD=@NOISY_BUILD@
 
+AST_CODE_COVERAGE=@AST_CODE_COVERAGE@
+
 AST_DECLARATION_AFTER_STATEMENT=@AST_DECLARATION_AFTER_STATEMENT@
 AST_NO_STRICT_OVERFLOW=@AST_NO_STRICT_OVERFLOW@
 AST_SHADOW_WARNINGS=@AST_SHADOW_WARNINGS@