diff mbox series

[v2,4/4] libtracefs: New API for applying filter on event

Message ID 20211202105926.32581-5-tz.stoyanov@gmail.com (mailing list archive)
State Accepted
Commit 351e998302a1bb9616816efd79f086cc19ea4d42
Headers show
Series New tracefs APIs | expand

Commit Message

Tzvetomir Stoyanov (VMware) Dec. 2, 2021, 10:59 a.m. UTC
There is no API for applying a filter string on event. Existing APIs
only constructs and verifies the filter string. Even though the actual
applying is just writing into the event's filter file, it is good to
have a dedicated API for that:
	tracefs_event_filter_apply()

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 Documentation/libtracefs-filter.txt | 12 ++++++++++--
 include/tracefs.h                   |  5 +++++
 src/tracefs-filter.c                | 18 ++++++++++++++++++
 3 files changed, 33 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/Documentation/libtracefs-filter.txt b/Documentation/libtracefs-filter.txt
index 23a2abf..7db3d60 100644
--- a/Documentation/libtracefs-filter.txt
+++ b/Documentation/libtracefs-filter.txt
@@ -3,7 +3,8 @@  libtracefs(3)
 
 NAME
 ----
-tracefs_filter_string_append, tracefs_filter_string_verify - Add and verify event filters
+tracefs_filter_string_append, tracefs_filter_string_verify, tracefs_event_apply_filter -
+Add, verify and apply event filters
 
 SYNOPSIS
 --------
@@ -15,7 +16,7 @@  int tracefs_filter_string_append(struct tep_event pass:[*]event, char pass:[**]
 				 struct tracefs_filter type, const char pass:[*]field,
 				 enum tracefs_synth_compare compare, const char pass:[*]val);
 int tracefs_filter_string_verify(struct tep_event pass:[*]event, const char pass:[*]filter, char pass:[**]err);
-
+int tracefs_event_filter_apply(struct tracefs_instance pass:[*]instance, struct tep_event pass:[*]event, const char pass:[*]filter);
 --
 
 DESCRIPTION
@@ -66,6 +67,8 @@  error in the syntax, and _err_ is not NULL, then it will be allocated with an
 error message stating what was found wrong with the filter. _err_ must be freed
 with *free*().
 
+*tracefs_event_filter_apply*() applies given _filter_ string on _event_ in given _instance_.
+
 RETURN VALUE
 ------------
 *tracefs_filter_string_append*() returns 0 on success and -1 on error.
@@ -75,6 +78,8 @@  is an error, and _errno_ is not *ENOMEM*, then _err_ is allocated and will
 contain a string describing what was found wrong with _filter_. _err_ must be
 freed with *free*().
 
+*tracefs_event_filter_apply*() returns 0 on success and -1 on error.
+
 EXAMPLE
 -------
 [source,c]
@@ -269,6 +274,9 @@  int main (int argc, char **argv)
 		}
 	}
 
+	if (tracefs_event_filter_apply(NULL, event, new_filter))
+		fprintf(stderr, "Failed to apply filter on event");
+
 	tep_free(tep);
 
 	printf("Created new filter: '%s'\n", new_filter);
diff --git a/include/tracefs.h b/include/tracefs.h
index 383974b..ac92dca 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -476,6 +476,10 @@  int tracefs_filter_string_append(struct tep_event *event, char **filter,
 int tracefs_filter_string_verify(struct tep_event *event, const char *filter,
 				 char **err);
 
+int tracefs_event_filter_apply(struct tracefs_instance *instance,
+			       struct tep_event *event, const char *filter);
+
+
 /** Deprecated do not use: Instead use tracefs_filter_string_append() **/
 int tracefs_event_append_filter(struct tep_event *event, char **filter,
 				enum tracefs_filter type,
@@ -486,6 +490,7 @@  int tracefs_event_append_filter(struct tep_event *event, char **filter,
 int tracefs_event_verify_filter(struct tep_event *event, const char *filter,
 				char **err);
 
+
 #define TRACEFS_TIMESTAMP "common_timestamp"
 #define TRACEFS_TIMESTAMP_USECS "common_timestamp.usecs"
 
diff --git a/src/tracefs-filter.c b/src/tracefs-filter.c
index e65fd94..7bc36fe 100644
--- a/src/tracefs-filter.c
+++ b/src/tracefs-filter.c
@@ -746,6 +746,24 @@  int tracefs_filter_string_verify(struct tep_event *event, const char *filter,
 	return 0;
 }
 
+/**
+ * tracefs_event_filter_apply - apply given filter on event in given instance
+ * @instance: The instance in which the filter will be applied (NULL for toplevel).
+ * @event: The event to apply the filter on.
+ * @filter: The filter to apply.
+ *
+ * Apply the @filter to given @event in givem @instance. The @filter string
+ * should be created with tracefs_filter_string_append().
+ *
+ * Returns 0 on succes and -1 on error.
+ */
+int tracefs_event_filter_apply(struct tracefs_instance *instance,
+			       struct tep_event *event, const char *filter)
+{
+	return tracefs_event_file_write(instance, event->system, event->name,
+					"filter", filter);
+}
+
 /** Deprecated **/
 int tracefs_event_append_filter(struct tep_event *event, char **filter,
 				enum tracefs_filter type,