[DTrace-devel] [PATCH 4/7] options: reduce redundancy

Nick Alcock nick.alcock at oracle.com
Tue May 2 17:12:19 UTC 2023


The addition of _dtrace_btoptions pushed the amount of redundancy in
dtrace_setopt and dtrace_setoptenv up to ridiculous levels. Re-express
it as a loop, and in the process add the check for setting non-dynamic
runtime options in dt_setopt to dt_setoptenv as well (though there, we
just skip them if tracing is active rather than erroring, since callers
cannot decide to set only *some* options from env vars: it's all or
nothing).

Signed-off-by: Nick Alcock <nick.alcock at oracle.com>
---
 libdtrace/dt_options.c | 96 ++++++++++++++++++------------------------
 1 file changed, 42 insertions(+), 54 deletions(-)

diff --git a/libdtrace/dt_options.c b/libdtrace/dt_options.c
index 2787311b4dc54..2dd1a3d18b597 100644
--- a/libdtrace/dt_options.c
+++ b/libdtrace/dt_options.c
@@ -1275,6 +1275,13 @@ int
 dtrace_setopt(dtrace_hdl_t *dtp, const char *opt, const char *val)
 {
 	const dt_option_t *op;
+	const dt_option_t *ops[] = {
+		_dtrace_btoptions,
+		_dtrace_ctoptions,
+		_dtrace_drtoptions,
+		_dtrace_rtoptions,
+		NULL };
+	const dt_option_t **pops;
 
 	if (opt == NULL)
 		return dt_set_errno(dtp, EINVAL);
@@ -1282,31 +1289,18 @@ dtrace_setopt(dtrace_hdl_t *dtp, const char *opt, const char *val)
 	if (dtp == NULL)
 		return set_default_option(opt, val);
 
-	for (op = _dtrace_btoptions; op->o_name != NULL; op++) {
-		if (strcmp(op->o_name, opt) == 0)
-			return op->o_func(dtp, val, op->o_option);
-	}
-
-	for (op = _dtrace_ctoptions; op->o_name != NULL; op++) {
-		if (strcmp(op->o_name, opt) == 0)
-			return op->o_func(dtp, val, op->o_option);
-	}
-
-	for (op = _dtrace_drtoptions; op->o_name != NULL; op++) {
-		if (strcmp(op->o_name, opt) == 0)
-			return op->o_func(dtp, val, op->o_option);
-	}
-
-	for (op = _dtrace_rtoptions; op->o_name != NULL; op++) {
-		if (strcmp(op->o_name, opt) == 0) {
+	for (pops = ops; *pops != NULL; pops++) {
+		for (op = *pops; op->o_name != NULL; op++) {
 			/*
 			 * Only dynamic run-time options may be set while
 			 * tracing is active.
 			 */
-			if (dtp->dt_active)
-				return dt_set_errno(dtp, EDT_ACTIVE);
-
-			return op->o_func(dtp, val, op->o_option);
+			if (strcmp(op->o_name, opt) == 0) {
+				if (*pops == _dtrace_rtoptions && dtp->dt_active)
+					return dt_set_errno(dtp, EDT_ACTIVE);
+				else
+					return op->o_func(dtp, val, op->o_option);
+			}
 		}
 	}
 
@@ -1340,48 +1334,42 @@ void
 dtrace_setoptenv(dtrace_hdl_t *dtp, const char *prefix)
 {
 	const dt_option_t *op;
+	const dt_option_t *ops[] = {
+		_dtrace_btoptions,
+		_dtrace_ctoptions,
+		_dtrace_drtoptions,
+		_dtrace_rtoptions,
+		NULL };
+	const dt_option_t **pops;
+
 	const char *val;
 
 	if (prefix == NULL)
 		prefix = "DTRACE_OPT_";
 
-	for (op = _dtrace_btoptions; op->o_name != NULL; op++) {
-		if ((val = dt_opt_getenv_prefix(dtp, op->o_name,
-			    prefix)) != NULL) {
-			if (dtp == NULL)
-				set_default_option(op->o_name, val);
-			else
-				op->o_func(dtp, val, op->o_option);
-		}
-	}
+	for (pops = ops; *pops != NULL; pops++) {
+		for (op = *pops; op->o_name != NULL; op++) {
+			if ((val = dt_opt_getenv_prefix(dtp, op->o_name,
+				    prefix)) != NULL) {
 
-	for (op = _dtrace_ctoptions; op->o_name != NULL; op++) {
-		if ((val = dt_opt_getenv_prefix(dtp, op->o_name,
-			    prefix)) != NULL) {
-			if (dtp == NULL)
-				set_default_option(op->o_name, val);
-			else
-				op->o_func(dtp, val, op->o_option);
-		}
-	}
+				if (dtp == NULL) {
+					set_default_option(op->o_name, val);
+					continue;
+				}
 
-	for (op = _dtrace_drtoptions; op->o_name != NULL; op++) {
-		if ((val = dt_opt_getenv_prefix(dtp, op->o_name,
-			    prefix)) != NULL) {
-			if (dtp == NULL)
-				set_default_option(op->o_name, val);
-			else
-				op->o_func(dtp, val, op->o_option);
-		}
-	}
+				/*
+				 * Only dynamic run-time options may be
+				 * set while tracing is active.  Since
+				 * env var application is a wholesale
+				 * thing, just skip things we can't
+				 * apply.
+				 */
+				if (*pops == _dtrace_rtoptions &&
+				    dtp->dt_active)
+					continue;
 
-	for (op = _dtrace_rtoptions; op->o_name != NULL; op++) {
-		if ((val = dt_opt_getenv_prefix(dtp, op->o_name,
-			    prefix)) != NULL) {
-			if (dtp == NULL)
-				set_default_option(op->o_name, val);
-			else
 				op->o_func(dtp, val, op->o_option);
+			}
 		}
 	}
 }
-- 
2.39.1.268.g9de2f9a303




More information about the DTrace-devel mailing list