diff mbox series

[2/4] libtracefs: Allow filter be NULL if RESET flag is set

Message ID 20210330183546.058728551@goodmis.org (mailing list archive)
State Accepted
Commit 8f26581f2732db2825e4206741acc9054c931ef5
Headers show
Series libtracefs: More updates to tracefs_function_filter() | expand

Commit Message

Steven Rostedt March 30, 2021, 6:33 p.m. UTC
From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

If the TRACEFS_FL_RESET flag is set, and the instance has yet to be
opened, the filter parameter can be NULL. This will allow simply resetting
the filter (clearing it).

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 Documentation/libtracefs-function-filter.txt | 27 ++++++++++++++------
 src/tracefs-tools.c                          | 15 ++++++++---
 2 files changed, 31 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/Documentation/libtracefs-function-filter.txt b/Documentation/libtracefs-function-filter.txt
index a022a2196b75..a4218b75deea 100644
--- a/Documentation/libtracefs-function-filter.txt
+++ b/Documentation/libtracefs-function-filter.txt
@@ -37,6 +37,12 @@  not a period, but will match any one character. To force a regular
 expression, either prefix _filter_ with a '^' or append it with a '$' as
 the _filter_ does complete matches of the functions anyway.
 
+The _filter_ may be NULL if a previous call to *tracefs_function_filter()* with
+the same _instance_ had *TRACEFS_FL_CONTINUE* set and this call does not. This is
+useful to simply commit the previous filters. It may also be NULL
+if *TRACEFS_FL_RESET* is set and the previous call did not have the same _instance_
+and *TRACEFS_FL_CONTINUE* set. This is useful to just clear the filter.
+
 FLAGS
 -----
 
@@ -102,20 +108,25 @@  int main(int argc, char *argv[])
 {
 	struct tracefs_instance *inst = tracefs_instance_create(INST);
 	int ret;
-	int reset = TRACEFS_FL_RESET;
 	int i;
 
 	if (!inst) {
 		/* Error creating new trace instance */
 	}
 
+	/* First reset the filter */
+	ret = tracefs_function_filter(inst, NULL, NULL,
+				      TRACEFS_FL_RESET | TRACEFS_FL_CONTINUE);
+	if (ret) {
+		printf("Failed to reset the filter\n");
+		/* Make sure it is closed, -1 means filter was started */
+		if (ret < 0)
+			tracefs_function_filter(inst, NULL, NULL, 0);
+	}
+
 	for (i = 0; filters[i]; i++) {
-		/*
-		 * Only the first call can have TRACEFS_FL_RESET set
-		 * while TRACEFS_FL_CONTINUE is set.
-		 */
 		ret = tracefs_function_filter(inst, filters[i], NULL,
-				      reset | TRACEFS_FL_CONTINUE);
+					      TRACEFS_FL_CONTINUE);
 
 		if (ret) {
 			if (errno == EINVAL)
@@ -123,7 +134,6 @@  int main(int argc, char *argv[])
 			else
 				printf("Failed writing %s\n", filters[i]);
 		}
-		reset = 0;
 	}
 
 	ret = tracefs_function_filter(inst, "*", "ext4", 0);
@@ -133,8 +143,9 @@  int main(int argc, char *argv[])
 		tracefs_function_filter(inst, NULL, NULL, 0);
 	}
 
+ out:
 	tracefs_instance_destroy(inst);
-	return 0;
+	return ret;
 }
 --
 
diff --git a/src/tracefs-tools.c b/src/tracefs-tools.c
index 5719ddf66982..a9a51beb02b2 100644
--- a/src/tracefs-tools.c
+++ b/src/tracefs-tools.c
@@ -794,6 +794,10 @@  int tracefs_function_filter(struct tracefs_instance *instance, const char *filte
 			close(*fd);
 			*fd = -1;
 		}
+		/* Also OK to call if reset flag is set */
+		if (reset)
+			goto open_file;
+
 		goto out;
 	}
 
@@ -804,6 +808,7 @@  int tracefs_function_filter(struct tracefs_instance *instance, const char *filte
 	if (ret)
 		goto out_free;
 
+ open_file:
 	ret = 1;
 	ftrace_filter_path = tracefs_instance_get_file(instance, TRACE_FILTER);
 	if (!ftrace_filter_path)
@@ -819,9 +824,13 @@  int tracefs_function_filter(struct tracefs_instance *instance, const char *filte
 
 	errno = 0;
 
-	ret = write_func_list(*fd, func_list);
-	if (ret > 0)
-		ret = controlled_write(*fd, &func_filter, module);
+	ret = 0;
+
+	if (filter) {
+		ret = write_func_list(*fd, func_list);
+		if (ret > 0)
+			ret = controlled_write(*fd, &func_filter, module);
+	}
 
 	if (!cont) {
 		close(*fd);