[PATCH v2] trace-cmd list: Allow adding a regex to filter out showing of options

Steven Rostedt <[email protected]> Fri, 2 Jan 2026 15:12:16 -0500
Newsgroups org.kernel.vger.linux-trace-devel
Message-ID <[email protected]>
From: "Steven Rostedt (Google)" <[email protected]>

The command "trace-cmd list -o" list all the options that are enabled. Most
of the time the user just wants to see a single option. Allow the -o option
to take an optional parameter that will filter the options shown.

Signed-off-by: Steven Rostedt (Google) <[email protected]>
---
Changes since v1: https://lore.kernel.org/all/[email protected]/

- Fix a missed "show_options()" update

 tracecmd/include/trace-local.h |  2 +-
 tracecmd/trace-list.c          | 19 ++++++++++++++++---
 tracecmd/trace-stat.c          |  2 +-
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h
index 54a1b51e..4ad07b63 100644
--- a/tracecmd/include/trace-local.h
+++ b/tracecmd/include/trace-local.h
@@ -343,7 +343,7 @@ void add_instance(struct buffer_instance *instance, int cpu_count);
 void update_first_instance(struct buffer_instance *instance, int topt);
 
 void show_instance_file(struct buffer_instance *instance, const char *name);
-void show_options(const char *prefix, struct buffer_instance *buffer);
+void show_options(const char *prefix, struct buffer_instance *buffer, const char *re);
 
 struct trace_guest {
 	struct tracefs_instance *instance;
diff --git a/tracecmd/trace-list.c b/tracecmd/trace-list.c
index 073b37fb..b9e23f66 100644
--- a/tracecmd/trace-list.c
+++ b/tracecmd/trace-list.c
@@ -389,14 +389,22 @@ static void show_tracers(void)
 	show_file("available_tracers");
 }
 
-void show_options(const char *prefix, struct buffer_instance *buffer)
+void show_options(const char *prefix, struct buffer_instance *buffer, const char *re)
 {
 	struct tracefs_instance *instance = buffer ? buffer->tracefs : NULL;
 	struct dirent *dent;
 	struct stat st;
+	regex_t reg;
 	char *path;
 	DIR *dir;
 
+	if (re && strlen(re)) {
+		if (regcomp(&reg, re, REG_ICASE|REG_NOSUB))
+			die("invalid function regex '%s'", re);
+	} else {
+		re = NULL;
+	}
+
 	if (!prefix)
 		prefix = "";
 
@@ -423,6 +431,9 @@ void show_options(const char *prefix, struct buffer_instance *buffer)
 		    strcmp(name, "..") == 0)
 			continue;
 
+		if (re && regexec(&reg, name, 0, NULL, 0) != 0)
+			continue;
+
 		ret = asprintf(&file, "options/%s", name);
 		if (ret < 0)
 			die("Failed to allocate file name");
@@ -645,6 +656,7 @@ void trace_list(int argc, char **argv)
 	const char *arg;
 	const char *funcre = NULL;
 	const char *eventre = NULL;
+	const char *optionre = NULL;
 
 	for (i = 2; i < argc; i++) {
 		arg = NULL;
@@ -694,6 +706,7 @@ void trace_list(int argc, char **argv)
 				break;
 			case 'o':
 				options = 1;
+				optionre = arg;
 				show_all = 0;
 				break;
 			case 'f':
@@ -735,7 +748,7 @@ void trace_list(int argc, char **argv)
 		show_tracers();
 
 	if (options)
-		show_options(NULL, NULL);
+		show_options(NULL, NULL, optionre);
 
 	if (plug)
 		show_plugins();
@@ -763,7 +776,7 @@ void trace_list(int argc, char **argv)
 		printf("\ntracers:\n");
 		show_tracers();
 		printf("\noptions:\n");
-		show_options(NULL, NULL);
+		show_options(NULL, NULL, NULL);
 		show_compression();
 	}
 
diff --git a/tracecmd/trace-stat.c b/tracecmd/trace-stat.c
index 7ac62f2a..41f6358f 100644
--- a/tracecmd/trace-stat.c
+++ b/tracecmd/trace-stat.c
@@ -851,7 +851,7 @@ static void stat_instance(struct buffer_instance *instance, bool opt)
 		    "Filtered function tracer notrace PIDs:\n");
 	if (opt) {
 		printf("\nOptions:\n");
-		show_options("   ", instance);
+		show_options("   ", instance, NULL);
 	}
 	report_traceon(instance);
 	report_file(instance, "error_log", "", "Error log:\n");
-- 
2.50.1