From a4fec245ed643f95eda6d5ad643158de5245ed66 Mon Sep 17 00:00:00 2001
From: George Neville-Neil <gnn@freebsd.org>
Date: Wed, 1 Mar 2017 20:50:07 +0000
Subject: [PATCH] Wrap the run time deubg option in a compile time option.
 (DEBUG)

---
 gtests/net/packetdrill/config.c          | 5 ++++-
 gtests/net/packetdrill/logging.h         | 4 ++++
 gtests/net/packetdrill/packet_parser.c   | 4 ++++
 gtests/net/packetdrill/run_packet.c      | 2 ++
 gtests/net/packetdrill/run_system_call.c | 5 ++++-
 5 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/gtests/net/packetdrill/config.c b/gtests/net/packetdrill/config.c
index 2c29cbc1..4df80329 100644
--- a/gtests/net/packetdrill/config.c
+++ b/gtests/net/packetdrill/config.c
@@ -119,7 +119,7 @@ void show_usage(void)
 		"\t[--wire_server_dev=<eth_dev_name>]\n"
 		"\t[--dry_run]\n"
 		"\t[--verbose|-v]\n"
-		"\t[--debug]\n"
+		"\t[--debug] * requires compilation with DEBUG *\n"
 		"\tscript_path ...\n");
 }
 
@@ -470,6 +470,9 @@ static void process_option(int opt, char *optarg, struct config *config,
 		config->verbose = true;
 		break;
 	case OPT_DEBUG:
+#if !defined(DEBUG)
+		die("error: --debug requires building with DEBUG on.\n");
+#endif
 		debug_logging = true;
 		break;
 	default:
diff --git a/gtests/net/packetdrill/logging.h b/gtests/net/packetdrill/logging.h
index 53a2c601..a537ffa4 100644
--- a/gtests/net/packetdrill/logging.h
+++ b/gtests/net/packetdrill/logging.h
@@ -31,11 +31,15 @@
 extern int debug_logging;
 
 /* Use a gcc variadic macro to conditionally compile debug printing. */
+#if defined(DEBUG)
 #define DEBUGP(...)				\
 	if (debug_logging) {			\
 		fprintf(stdout,  __VA_ARGS__);	\
 		fflush(stdout);			\
 	}
+#else
+#define DEBUGP(...) {}
+#endif /* DEBUG */
 
 /* Log the message to stderr and then exit with a failure status code. */
 extern void die(char *format, ...);
diff --git a/gtests/net/packetdrill/packet_parser.c b/gtests/net/packetdrill/packet_parser.c
index 056b1e0e..03209545 100644
--- a/gtests/net/packetdrill/packet_parser.c
+++ b/gtests/net/packetdrill/packet_parser.c
@@ -232,6 +232,7 @@ static int parse_ipv4(struct packet *packet, u8 *header_start, u8 *packet_end,
 	p += ip_header_bytes;
 	assert(p <= packet_end);
 
+#if defined(DEBUG)
 	if (debug_logging) {
 		char src_string[ADDR_STR_LEN];
 		char dst_string[ADDR_STR_LEN];
@@ -241,6 +242,7 @@ static int parse_ipv4(struct packet *packet, u8 *header_start, u8 *packet_end,
 		DEBUGP("src IP: %s\n", ip_to_string(&src_ip, src_string));
 		DEBUGP("dst IP: %s\n", ip_to_string(&dst_ip, dst_string));
 	}
+#endif /* DEBUG */
 
 	/* Examine the L4 header. */
 	const int layer4_bytes = ip_total_bytes - ip_header_bytes;
@@ -304,6 +306,7 @@ static int parse_ipv6(struct packet *packet, u8 *header_start, u8 *packet_end,
 	p += ip_header_bytes;
 	assert(p <= packet_end);
 
+#if defined(DEBUG)
 	if (debug_logging) {
 		char src_string[ADDR_STR_LEN];
 		char dst_string[ADDR_STR_LEN];
@@ -313,6 +316,7 @@ static int parse_ipv6(struct packet *packet, u8 *header_start, u8 *packet_end,
 		DEBUGP("src IP: %s\n", ip_to_string(&src_ip, src_string));
 		DEBUGP("dst IP: %s\n", ip_to_string(&dst_ip, dst_string));
 	}
+#endif /* DEBUG */
 
 	/* Examine the L4 header. */
 	const int layer4_bytes = ip_total_bytes - ip_header_bytes;
diff --git a/gtests/net/packetdrill/run_packet.c b/gtests/net/packetdrill/run_packet.c
index 30638f1a..07ebe30a 100644
--- a/gtests/net/packetdrill/run_packet.c
+++ b/gtests/net/packetdrill/run_packet.c
@@ -328,6 +328,7 @@ static struct socket *handle_listen_for_script_packet(
 		socket->live.remote_isn = ntohl(packet->tcp->seq);
 	}
 
+#if defined(DEBUG)
 	if (debug_logging) {
 		char local_string[ADDR_STR_LEN];
 		char remote_string[ADDR_STR_LEN];
@@ -344,6 +345,7 @@ static struct socket *handle_listen_for_script_packet(
 			DEBUGP("live: initial tsn: %u\n", socket->live.remote_initial_tsn);
 		}
 	}
+#endif /* DEBUG */
 
 	return socket;
 }
diff --git a/gtests/net/packetdrill/run_system_call.c b/gtests/net/packetdrill/run_system_call.c
index 6972d661..88db2335 100644
--- a/gtests/net/packetdrill/run_system_call.c
+++ b/gtests/net/packetdrill/run_system_call.c
@@ -1841,6 +1841,7 @@ static int run_syscall_accept(struct state *state,
 	}
 
 	for (socket = state->sockets; socket != NULL; socket = socket->next) {
+#if defined(DEBUG)
 		if (debug_logging) {
 			char remote_string[ADDR_STR_LEN];
 			DEBUGP("socket state=%d script addr: %s:%d\n",
@@ -1849,7 +1850,7 @@ static int run_syscall_accept(struct state *state,
 					    remote_string),
 			       socket->script.remote.port);
 		}
-
+#endif /* DEBUG */
 		if ((socket->state == SOCKET_PASSIVE_SYNACK_SENT) ||  /* TFO */
 		    (socket->state == SOCKET_PASSIVE_SYNACK_ACKED) ||
 		    (socket->state == SOCKET_PASSIVE_COOKIE_ECHO_RECEIVED)) {
@@ -1886,6 +1887,7 @@ static int run_syscall_accept(struct state *state,
 	socket->live.fd			= live_accepted_fd;
 	socket->script.fd		= script_accepted_fd;
 
+#if defined(DEBUG)
 	if (debug_logging) {
 		char local_string[ADDR_STR_LEN];
 		char remote_string[ADDR_STR_LEN];
@@ -1896,6 +1898,7 @@ static int run_syscall_accept(struct state *state,
 		       ip_to_string(&socket->live.remote.ip, remote_string),
 		       ntohs(socket->live.remote.port));
 	}
+#endif /* DEBUG */
 	return STATUS_OK;
 }
 
-- 
GitLab