libtracefs: Updates to event filtering
Subsystems (2)
cover libtracefs: Updates to event filtering
From: "Steven Rostedt (VMware)" <[email protected]>
First, move the version to 1.3-dev, as I found that installing this with a newer stable (1.2.4), where this forked form stable at 1.2.1, kept code from using this version.
Change "GR" and "NQ" to "GT" and "NE" to represent "greater than" and "not equal" respectively.
Changed the way filters are built, to something that can easily be used, and is less error prone.
Updated kerneldoc.
Added filtering APIs for normal events, and also a way to verify a filter.
Steven Rostedt (VMware) (6):
libtracefs: Update to version 1.3-dev
libtracefs: Rename GR and NQ to GT and NE respectively
libtracefs: Append the synth filter with parens and conjunctions
libtracefs: Add kerneldoc comments to the remaining synth functions
libtracefs: Add filter creating and verify API
libtracefs: Add man pages for tracefs_event_append_filter()
Documentation/libtracefs-filter.txt | 329 ++++++++++++ Documentation/libtracefs-synth.txt | 78 +-- Makefile | 4 +- include/tracefs-local.h | 14 + include/tracefs.h | 42 +- src/Makefile | 1 + src/tracefs-filter.c | 747 ++++++++++++++++++++++++++++ src/tracefs-hist.c | 345 ++++++------- 8 files changed, 1308 insertions(+), 252 deletions(-) create mode 100644 Documentation/libtracefs-filter.txt create mode 100644 src/tracefs-filter.c
Signature · 1 line
2.30.2
PATCH 1/6 libtracefs: Update to version 1.3-dev
From: "Steven Rostedt (VMware)" <[email protected]>
Since the development of 1.3 source has diverged from the stable 1.2 path, it still has the old 1.2.1 as the version number. If this is added to a system with 1.2.2 or above, it wont be used, as the symlinks point to the highest version (1.2.2 or above).
Set this to 1.3.dev to make it the latest version to be used.
--- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Makefile +2 -2
@@ -1,8 +1,8 @@ # SPDX-License-Identifier: LGPL-2.1 # libtracefs version TFS_VERSION = 1 -TFS_PATCHLEVEL = 2 -TFS_EXTRAVERSION = 1 +TFS_PATCHLEVEL = 3 +TFS_EXTRAVERSION = dev TRACEFS_VERSION = $(TFS_VERSION).$(TFS_PATCHLEVEL).$(TFS_EXTRAVERSION) export TFS_VERSION
Signature · 1 line
2.30.2
PATCH 2/6 libtracefs: Rename GR and NQ to GT and NE respectively
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
When writing the sqlhist interface, I found that calling ">" GT and "!=" NE were more sensible than calling them GR and NQ, as we already have LT as "less than" why not use GT as "greater than". As well as EQ for "equal", it seems more appropriate to have NE be "not equal".
--- Documentation/libtracefs-synth.txt | 4 ++-- include/tracefs.h | 4 ++-- src/tracefs-hist.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
Documentation/libtracefs-synth.txt +2 -2
@@ -137,9 +137,9 @@ may be one of: *TRACEFS_COMPARE_EQ* - _field_ == _val_ -*TRACEFS_COMPARE_NQ* - _field_ != _val_ +*TRACEFS_COMPARE_NE* - _field_ != _val_ -*TRACEFS_COMPARE_GR* - _field_ > _val_ +*TRACEFS_COMPARE_GT* - _field_ > _val_ *TRACEFS_COMPARE_GE* - _field_ >= _val_
include/tracefs.h +2 -2
@@ -315,8 +315,8 @@ enum tracefs_synth_calc {enum tracefs_synth_compare {TRACEFS_COMPARE_EQ , -TRACEFS_COMPARE_NQ , -TRACEFS_COMPARE_GR , +TRACEFS_COMPARE_NE , +TRACEFS_COMPARE_GT ,TRACEFS_COMPARE_GE ,TRACEFS_COMPARE_LT ,TRACEFS_COMPARE_LE ,
src/tracefs-hist.c +2 -2
@@ -1178,11 +1178,11 @@ static int add_synth_filter(char **filter, const char *field, op ="==" ;break ; -case TRACEFS_COMPARE_NQ : +case TRACEFS_COMPARE_NE : op ="!=" ;break ; -case TRACEFS_COMPARE_GR : +case TRACEFS_COMPARE_GT : op =">" ;if (is_string) goto inval;
Signature · 1 line
2.30.2
PATCH 3/6 libtracefs: Append the synth filter with parens and conjunctions
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
While implementing the sqlhist, I found that the tracefs_synth_add_start/end_filter() was not sufficient in adding the possible filters that the kernel accepts. That is, we could not implement:
A && (B || C) && D
as it only allowed appending operators, and the kernel sets && as a higher precedence than ||. That is, if we try to do the above with just:
A && B || C && D
The kernel will interpret it as:
(A && B) || (C && D)
I tried to fix this with a precedence, showing different levels of precedence between options and it too wasn't sufficient to handle all the cases that the kernel can.
Instead, go with the KISS approach, and do it with just letting the user append the parenthesis, nots and conjunctions. Keeping the state of the last update makes sure that an invalid append does not go through.
For example, to produce:
A || (!(B && !C) && !(D)
tracefs_synth_append_start_field( TRACEFS_FILTER_COMPARE, A ); tracefs_synth_append_start_field( TRACEFS_FILTER_OR, NULL ); tracefs_synth_append_start_field( TRACEFS_FILTER_OPEN_PAREN, NULL ); tracefs_synth_append_start_field( TRACEFS_FILTER_NOT, NULL ); tracefs_synth_append_start_field( TRACEFS_FILTER_OPEN_PAREN, NULL ); tracefs_synth_append_start_field( TRACEFS_FILTER_COMPARE, B ); tracefs_synth_append_start_field( TRACEFS_FILTER_AND, NULL ); tracefs_synth_append_start_field( TRACEFS_FILTER_NOT, NULL ); tracefs_synth_append_start_field( TRACEFS_FILTER_COMPARE, C ); tracefs_synth_append_start_field( TRACEFS_FILTER_CLOSE_PAREN, NULL ); tracefs_synth_append_start_field( TRACEFS_FILTER_AND, NULL ); tracefs_synth_append_start_field( TRACEFS_FILTER_NOT, NULL ); tracefs_synth_append_start_field( TRACEFS_FILTER_OPEN_PAREN, NULL ); tracefs_synth_append_start_field( TRACEFS_FILTER_COMPARE, D );
The number of left over open parenthesis is kept track of and on printing or executing the synth, it will close all the parenthesis that are left open.
(note, the original code had "-" for negative, which was wrong)
Also, rename the parameter in tracefs from "or" to "or_conj" as "or" is a keyword in C++ as reported by Yordan Karadzhov:
--- Documentation/libtracefs-synth.txt | 74 +++--- include/tracefs.h | 29 ++- src/tracefs-hist.c | 366 ++++++++++++++++++----------- 3 files changed, 293 insertions(+), 176 deletions(-)
Documentation/libtracefs-synth.txt +45 -29
@@ -4,7 +4,7 @@ libtracefs(3) NAME ---- tracefs_synth_init, tracefs_synth_add_match_field, tracefs_synth_add_compare_field, tracefs_synth_add_start_field, -tracefs_synth_add_end_field, tracefs_synth_add_start_filter, tracefs_synth_add_end_filter, tracefs_synth_create, +tracefs_synth_add_end_field, tracefs_synth_append_start_filter, tracefs_synth_append_end_filter, tracefs_synth_create, tracefs_synth_destroy, tracefs_synth_free, tracefs_synth_show - Creation of synthetic events SYNOPSIS
@@ -37,16 +37,16 @@ int tracefs_synth_add_start_field(struct tracefs_synth pass:[*]synth, int tracefs_synth_add_end_field(struct tracefs_synth pass:[*]synth, const char pass:[*]end_field, const char pass:[*]name); -int tracefs_synth_add_start_filter(struct tracefs_synth pass:[*]synth, - const char pass:[*]field, - enum tracefs_synth_compare compare, - const char pass:[*]val, - bool neg, bool or); -int tracefs_synth_add_end_filter(struct tracefs_synth pass:[*]synth, - const char pass:[*]field, - enum tracefs_synth_compare compare, - const char pass:[*]val, - bool neg, bool or); +int tracefs_synth_append_start_filter(struct tracefs_synth pass:[*]synth, + struct tracefs_filter type, + const char pass:[*]field, + enum tracefs_synth_compare compare, + const char pass:[*]val); +int tracefs_synth_append_end_filter(struct tracefs_synth pass:[*]synth, + struct tracefs_filter type, + const char pass:[*]field, + enum tracefs_synth_compare compare, + const char pass:[*]val); int tracefs_synth_create(struct tracefs_instance pass:[*]instance, struct tracefs_synth pass:[*]synth); int tracefs_synth_destroy(struct tracefs_instance pass:[*]instance,
@@ -127,13 +127,21 @@ will be the same as _start_field_. event as _name_ in the synthetic event. If _name_ is NULL, then the name used will be the same as _end_field_. -*tracefs_synth_add_start_filter*() adds a filter to the starting event -comparing the content of the starting event's _field_ to _val_ based -on _compare_. If _neg_ is set, then the compare is wrapped in parenthesis -and negated. The _or_ field only is used if more than one call to -*tracefs_synth_add_start_filter*() is done, and if _or_ is set, the -next compare is "or'd" (||), otherwise it is "and'd" (&&). _compare_ -may be one of: +*tracefs_synth_append_start_filter*() creates a filter or appends to it for the +starting event. Depending on _type_, it will build a string of tokens for +parenthesis or logic statemens, or it may add a comparison of _field_ +to _val_ based on _compare_. + +If _type_ is: +*TRACEFS_FILTER_COMPARE* - See below +*TRACEFS_FILTER_AND* - Append "&&" to the filter +*TRACEFS_FILTER_OR* - Append "||" to the filter +*TRACEFS_FILTER_NOT* - Append "!" to the filter +*TRACEFS_FILTER_OPEN_PAREN* - Append "(" to the filter +*TRACEFS_FILTER_CLOSE_PAREN* - Append ")" to the filter + +_field_, _compare_, and _val_ are ignored unless _type_ is equal to +*TRACEFS_FILTER_COMPARE*, then _compare will be used for the following: *TRACEFS_COMPARE_EQ* - _field_ == _val_
@@ -151,7 +159,7 @@ may be one of: *TRACEFS_COMPARE_AND* - _field_ & _val_ : where _field_ is a flags field. -*tracefs_synth_add_end_filter*() is the same as *tracefs_synth_add_start_filter* but +*tracefs_synth_append_end_filter*() is the same as *tracefs_synth_append_start_filter* but filters on the ending event. *tracefs_synth_create*() creates the synthetic event in the system in the system
@@ -249,20 +257,28 @@ static void make_event(void) TRACEFS_SYNTH_DELTA_END, "delta"); /* Only record if start event "prio" is less than 100 */ - tracefs_synth_add_start_filter(synth, "prio", - TRACEFS_COMPARE_LT, "100", - false, false); + tracefs_synth_append_start_filter(synth, TRACEFS_FILTER_COMPARE, + "prio", TRACEFS_COMPARE_LT, "100"); /* * Only record if end event "next_prio" is less than 50 - * or the previous task's prio was less than 100. + * or the previous task's prio was not greater than or equal to 100. + * next_prio < 50 || !(prev_prio >= 100) + */ + tracefs_synth_append_end_filter(synth, TRACEFS_FILTER_COMPARE, + "next_prio", TRACEFS_COMPARE_LT, "50"); + tracefs_synth_append_end_filter(synth, TRACEFS_FILTER_OR, NULL, 0, NULL); + tracefs_synth_append_end_filter(synth, TRACEFS_FILTER_NOT, NULL, 0, NULL); + tracefs_synth_append_end_filter(synth, TRACEFS_FILTER_OPEN_PAREN, NULL, 0, NULL); + tracefs_synth_append_end_filter(synth, TRACEFS_FILTER_COMPARE, + "prev_prio", TRACEFS_COMPARE_GE, "100"); + /* + * Note, the above only added: "next_prio < 50 || !(prev_prio >= 100" + * That's because, when the synth is executed, the remaining close parenthesis + * will be added. That is, the string will end up being: + * "next_prio < 50 || !(prev_prio >= 100)" when one of tracefs_sync_create() + * or tracefs_sync_show() is run. */ - tracefs_synth_add_end_filter(synth, "next_prio", - TRACEFS_COMPARE_LT, "50", - false, false); - tracefs_synth_add_end_filter(synth, "prev_prio", - TRACEFS_COMPARE_LT, "100", - false, true); } /* Display how to create the synthetic event */
include/tracefs.h +19 -10
@@ -324,6 +324,15 @@ enum tracefs_synth_compare { TRACEFS_COMPARE_AND, }; +enum tracefs_filter { +TRACEFS_FILTER_COMPARE , +TRACEFS_FILTER_AND , +TRACEFS_FILTER_OR , +TRACEFS_FILTER_NOT , +TRACEFS_FILTER_OPEN_PAREN , +TRACEFS_FILTER_CLOSE_PAREN , +}; +#define TRACEFS_TIMESTAMP "common_timestamp"#define TRACEFS_TIMESTAMP_USECS "common_timestamp.usecs"
@@ -351,16 +360,16 @@ int tracefs_synth_add_start_field(struct tracefs_synth *synth,int tracefs_synth_add_end_field (struct tracefs_synth *synth,const char *end_field,const char *name); -int tracefs_synth_add_start_filter (struct tracefs_synth *synth, -const char *field, -enum tracefs_synth_compare compare, -const char *val, -bool neg,bool or); -int tracefs_synth_add_end_filter (struct tracefs_synth *synth, -const char *field, -enum tracefs_synth_compare compare, -const char *val, -bool neg,bool or); +int tracefs_synth_append_start_filter (struct tracefs_synth *synth, +enum tracefs_filter type, +const char *field, +enum tracefs_synth_compare compare, +const char *val); +int tracefs_synth_append_end_filter (struct tracefs_synth *synth, +enum tracefs_filter type, +const char *field, +enum tracefs_synth_compare compare, +const char *val);int tracefs_synth_create (struct tracefs_instance *instance,struct tracefs_synth *synth); int tracefs_synth_destroy(struct tracefs_instance *instance,
src/tracefs-hist.c +229 -137
465 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH 4/6 libtracefs: Add kerneldoc comments to the remaining synth functions
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Added kerneldoc comments to:
tracefs_synth_show() tracefs_synth_append_start_filter() tracefs_synth_append_end_filter();
--- src/tracefs-hist.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)
src/tracefs-hist.c +69 -0
@@ -580,6 +580,15 @@ static const struct tep_format_field common_timestamp_usecs = { .size = 8, }; +/** +* tracefs_synth_free - free the resources alloced to a synth +* @synth: The tracefs_synth descriptor +* +* Frees the resources allocated for a @synth created with +* tracefs_synth_init(). It does not touch the system. That is, +* any synthetic event created, will not be destroyed by this +* function. +*/ void tracefs_synth_free (struct tracefs_synth *synth) {if (!synth)
@@ -1354,6 +1363,55 @@ inval:return -1 ; } +/** +* tracefs_synth_append_start_filter - create or append a filter +* @synth: The tracefs_synth descriptor +* @type: The type of element to add to the filter +* @field: For @type == TRACEFS_FILTER_COMPARE, the field to compare +* @compare: For @type == TRACEFS_FILTER_COMPARE, how to compare @field to @val +* @val: For @type == TRACEFS_FILTER_COMPARE, what value @field is to be +* +* This will put together a filter string for the starting event +* of @synth. It check to make sure that what is added is correct compared +* to the filter that is already built. +* +* @type can be: +* TRACEFS_FILTER_COMPARE: See below +* TRACEFS_FILTER_AND: Append "&&" to the filter +* TRACEFS_FILTER_OR: Append "||" to the filter +* TRACEFS_FILTER_NOT: Append "!" to the filter +* TRACEFS_FILTER_OPEN_PAREN: Append "(" to the filter +* TRACEFS_FILTER_CLOSE_PAREN: Append ")" to the filter +* +* For all types except TRACEFS_FILTER_COMPARE, the @field, @compare, +* and @val are ignored. +* +* For @type == TRACEFS_FILTER_COMPARE. +* +* @field is the name of the field for the start event to compare. +* If it is not a field for the start event, this return an +* error. +* +* @compare can be one of: +* TRACEFS_COMPARE_EQ: Test @field == @val +* TRACEFS_COMPARE_NE: Test @field != @val +* TRACEFS_COMPARE_GT: Test @field > @val +* TRACEFS_COMPARE_GE: Test @field >= @val +* TRACEFS_COMPARE_LT: Test @field < @val +* TRACEFS_COMPARE_LE: Test @field <= @val +* TRACEFS_COMPARE_RE: Test @field ~ @val +* TRACEFS_COMPARE_AND: Test @field & @val +* +* If the @field is of type string, and @compare is not +* TRACEFS_COMPARE_EQ, TRACEFS_COMPARE_NE or TRACEFS_COMPARE_RE, +* then this will return an error. +* +* Various other checks are made, for instance, if more CLOSE_PARENs +* are added than existing OPEN_PARENs. Or if AND is added after an +* OPEN_PAREN or another AND or an OR or a NOT. +* +* Returns 0 on success and -1 on failure. +*/ int tracefs_synth_append_start_filter(struct tracefs_synth *synth, enum tracefs_filter type, const char *field,
@@ -1366,6 +1424,17 @@ int tracefs_synth_append_start_filter(struct tracefs_synth *synth, type, field, compare, val); } +/** +* tracefs_synth_append_end_filter - create or append a filter +* @synth: The tracefs_synth descriptor +* @type: The type of element to add to the filter +* @field: For @type == TRACEFS_FILTER_COMPARE, the field to compare +* @compare: For @type == TRACEFS_FILTER_COMPARE, how to compare @field to @val +* @val: For @type == TRACEFS_FILTER_COMPARE, what value @field is to be +* +* Performs the same thing as tracefs_synth_append_start_filter() but +* for the @synth end event. +*/ int tracefs_synth_append_end_filter(struct tracefs_synth *synth, enum tracefs_filter type, const char *field,
Signature · 1 line
2.30.2
PATCH 5/6 libtracefs: Add filter creating and verify API
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add the following API:
tracefs_event_append_filter()
tracefs_event_verify_filter()
Pull out the filter logic from trace-hist.c and place it into its own
file: tracefs-filter.c, that allows users to use these filters directly for individual events.
--- include/tracefs-local.h | 14 + include/tracefs.h | 13 +- src/Makefile | 1 + src/tracefs-filter.c | 747 ++++++++++++++++++++++++++++++++++++++++ src/tracefs-hist.c | 258 +------------- 5 files changed, 787 insertions(+), 246 deletions(-) create mode 100644 src/tracefs-filter.c
include/tracefs-local.h +14 -0
@@ -71,4 +71,18 @@ struct tracefs_options_mask * enabled_opts_mask(struct tracefs_instance *instance);char **trace_list_create_empty (void ); + +char *append_string (char *str,const char *delim,const char *add); +int trace_test_state (int state); +bool trace_verify_event_field (struct tep_event *event, +const char *field_name, +const struct tep_format_field **ptr_field); +int trace_append_filter (char **filter,unsigned int *state, +unsigned int *open_parens, +struct tep_event *event, +enum tracefs_filter type, +const char *field_name, +enum tracefs_compare compare, +const char *val); +#endif /* _TRACE_FS_LOCAL_H */
include/tracefs.h +10 -3
@@ -313,7 +313,7 @@ enum tracefs_synth_calc { TRACEFS_SYNTH_ADD, }; -enum tracefs_synth_compare { +enum tracefs_compare {TRACEFS_COMPARE_EQ ,TRACEFS_COMPARE_NE ,TRACEFS_COMPARE_GT ,
@@ -333,6 +333,13 @@ enum tracefs_filter { TRACEFS_FILTER_CLOSE_PAREN, }; +int tracefs_event_append_filter (struct tep_event *event,char **filter, +enum tracefs_filter type, +const char *field,enum tracefs_compare compare, +const char *val); +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"
@@ -363,12 +370,12 @@ int tracefs_synth_add_end_field(struct tracefs_synth *synth,int tracefs_synth_append_start_filter (struct tracefs_synth *synth,enum tracefs_filter type,const char *field, -enum tracefs_synth_compare compare, +enum tracefs_compare compare,const char *val);int tracefs_synth_append_end_filter (struct tracefs_synth *synth,enum tracefs_filter type,const char *field, -enum tracefs_synth_compare compare, +enum tracefs_compare compare,const char *val);int tracefs_synth_create (struct tracefs_instance *instance,struct tracefs_synth *synth);
src/Makefile +1 -0
@@ -10,6 +10,7 @@ OBJS += tracefs-tools.o OBJS += tracefs-marker.o OBJS += tracefs-kprobes.o OBJS += tracefs-hist.o +OBJS += tracefs-filter.o OBJS := $(OBJS:%.o=$(bdir)/%.o) DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d)
src/tracefs-filter.c new file +747 -0
747 lines not on this page · raw message
src/tracefs-hist.c +15 -243
328 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH 6/6 libtracefs: Add man pages for tracefs_event_append_filter()
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add man pages for tracefs_event_append_filter() and tracefs_event_verify_filter()
--- Documentation/libtracefs-filter.txt | 329 ++++++++++++++++++++++++++++ 1 file changed, 329 insertions(+) create mode 100644 Documentation/libtracefs-filter.txt
Documentation/libtracefs-filter.txt new file +329 -0
@@ -0,0 +1,329 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_event_append_filter, tracefs_event_verify_filter - Add and verify event filters + +SYNOPSIS +-------- +[verse] +-- +*#include <tracefs.h>* + +int tracefs_event_append_filter(struct tep_event pass:[*]event, char pass:[**] filter, + struct tracefs_filter type, const char pass:[*]field, + enum tracefs_synth_compare compare, const char pass:[*]val); +int tracefs_event_verify_filter(struct tep_event pass:[*]event, const char pass:[*]filter, char pass:[**]err); + +-- + +DESCRIPTION +----------- +*tracefs_event_append_filter*() is a way to create and verify event filters for +a given event. It will verify that the _field_ belongs to the event and that +the _compare_ option that is used is valid for the type of the field, as well +as _val_. For the _type_ that is not of *TRACEFS_FILTER_COMPARE*, it will build +the logical string and also make sure that the syntax is correct. For example, +there are no more close parenthesis than open parenthesis. An AND (&&) or OR +(||) is not misplaced, etc. + +*tracefs_synth_append_start_filter*() creates a filter or appends to it for the +starting event. Depending on _type_, it will build a string of tokens for +parenthesis or logic statemens, or it may add a comparison of _field_ +to _val_ based on _compare_. + +If _type_ is: +*TRACEFS_FILTER_COMPARE* - See below +*TRACEFS_FILTER_AND* - Append "&&" to the filter +*TRACEFS_FILTER_OR* - Append "||" to the filter +*TRACEFS_FILTER_NOT* - Append "!" to the filter +*TRACEFS_FILTER_OPEN_PAREN* - Append "(" to the filter +*TRACEFS_FILTER_CLOSE_PAREN* - Append ")" to the filter + +_field_, _compare_, and _val_ are ignored unless _type_ is equal to +*TRACEFS_FILTER_COMPARE*, then _compare will be used for the following: + +*TRACEFS_COMPARE_EQ* - _field_ == _val_ + +*TRACEFS_COMPARE_NE* - _field_ != _val_ + +*TRACEFS_COMPARE_GT* - _field_ > _val_ + +*TRACEFS_COMPARE_GE* - _field_ >= _val_ + +*TRACEFS_COMPARE_LT* - _field_ < _val_ + +*TRACEFS_COMPARE_LE* - _field_ <pass:[=] _val_ + +*TRACEFS_COMPARE_RE* - _field_ ~ "_val_" : where _field_ is a string. + +*TRACEFS_COMPARE_AND* - _field_ & _val_ : where _field_ is a flags field. + +*tracefs_event_verify_filter*() will parse _filter_ to make sure that the +fields are for the _event_, and that the syntax is correct. If there's an +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*(). + +RETURN VALUE +------------ +*tracefs_event_append_filter*() returns 0 on success and -1 on error. + +*tracefs_event_verify_filter*() returns 0 on success and -1 on error. if there +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*(). + +EXAMPLE +------- +[source,c] +-- +#include <stdlib.h> +#include <stdio.h> +#include <ctype.h> +#include <tracefs.h> + +static void usage(char **argv) +{ + fprintf(stderr, "usage: %s [system] event filter\n", argv[0]); + exit(-1); +} + +int main (int argc, char **argv) +{ + struct tep_handle *tep; + struct tep_event *event; + const char *system = NULL; + const char *event_name; + const char *filter; + char *new_filter = NULL; + char *err = NULL; + int i; + + if (argc < 3) + usage(argv); + + if (argc < 4) { + event_name = argv[1]; + filter = argv[2]; + } else { + system = argv[1]; + event_name = argv[2]; + filter = argv[3]; + } + + /* Load all events from the system */ + tep = tracefs_local_events(NULL); + if (!tep) { + perror("tep"); + exit(-1); + } + + event = tep_find_event_by_name(tep, system, event_name); + if (!event) { + fprintf(stderr, "Event %s%s%s not found\n", + system ? system : "" , system ? " " : "", + event_name); + exit(-1); + } + + if (tracefs_event_verify_filter(event, filter, &err) < 0) { + perror("tracecfs_event_verify_filter"); + if (err) + fprintf(stderr, "%s", err); + free(err); + exit(-1); + } + + for (i = 0; filter[i]; i++) { + char buf[strlen(filter)]; + char *field = NULL; + char *val = NULL; + enum tracefs_filter type; + enum tracefs_compare compare = 0; + int start_i, n; + int quote; + bool backslash; + + while (isspace(filter[i])) + i++; + + switch(filter[i]) { + case '(': + type = TRACEFS_FILTER_OPEN_PAREN; + break; + case ')': + type = TRACEFS_FILTER_CLOSE_PAREN; + break; + case '!': + type = TRACEFS_FILTER_NOT; + break; + case '&': + type = TRACEFS_FILTER_AND; + i++; + break; + case '|': + type = TRACEFS_FILTER_OR; + i++; + break; + default: + type = TRACEFS_FILTER_COMPARE; + + while (isspace(filter[i])) + i++; + + start_i = i; + for (; filter[i]; i++) { + switch(filter[i]) { + case 'a' ... 'z': + case 'A' ... 'Z': + case '0' ... '9': + case '_': + continue; + } + break; + } + + n = i - start_i; + field = buf; + strncpy(field, filter + start_i, n); + field[n++] = '\0'; + + val = buf + n; + + while (isspace(filter[i])) + i++; + + start_i = i; + switch(filter[i++]) { + case '>': + compare = TRACEFS_COMPARE_GT; + if (filter[i] == '=') { + i++; + compare = TRACEFS_COMPARE_GE; + } + break; + case '<': + compare = TRACEFS_COMPARE_LT; + if (filter[i] == '=') { + i++; + compare = TRACEFS_COMPARE_LE; + } + break; + case '=': + compare = TRACEFS_COMPARE_EQ; + i++; + break; + case '!': + compare = TRACEFS_COMPARE_NE; + i++; + break; + case '~': + compare = TRACEFS_COMPARE_RE; + break; + case '&': + compare = TRACEFS_COMPARE_AND; + break; + } + + while (isspace(filter[i])) + i++; + + quote = 0; + backslash = false; + start_i = i; + for (; filter[i]; i++) { + if (quote) { + if (backslash) + backslash = false; + else if (filter[i] == '\\') + backslash = true; + else if (filter[i] == quote) + quote = 0; + continue; + } + switch(filter[i]) { + case '"': case '\'': + quote = filter[i]; + continue; + case 'a' ... 'z': + case 'A' ... 'Z': + case '0' ... '9': + case '_': + continue; + } + break; + } + n = i - start_i; + strncpy(val, filter + start_i, n); + val[n] = '\0'; + break; + } + n = tracefs_event_append_filter(event, &new_filter, type, + field, compare, val); + if (n < 0) { + fprintf(stderr, "Failed making new filter:\n'%s'\n", + new_filter ? new_filter : "(null)"); + exit(-1); + } + } + + tep_free(tep); + + printf("Created new filter: '%s'\n", new_filter); + free(new_filter); + + exit(0); +} +-- + +FILES +----- +[verse] +-- +*tracefs.h* + Header file to include in order to have access to the library APIs. +*-ltracefs* + Linker switch to add when building a program that uses the library. +-- + +SEE ALSO +-------- +_libtracefs(3)_, +_libtraceevent(3)_, +_trace-cmd(1)_, +_tracefs_hist_alloc(3)_, +_tracefs_hist_free(3)_, +_tracefs_hist_add_key(3)_, +_tracefs_hist_add_value(3)_, +_tracefs_hist_add_name(3)_, +_tracefs_hist_start(3)_, +_tracefs_hist_destory(3)_, +_tracefs_hist_add_sort_key(3)_, +_tracefs_hist_sort_key_direction(3)_ + +AUTHOR +------ +[verse] +-- +*Steven Rostedt* <[email protected]> +*Tzvetomir Stoyanov* <[email protected]> +*sameeruddin shaik* <[email protected]> +-- +REPORTING BUGS +-------------- +Report bugs to <[email protected]> + +LICENSE +------- +libtracefs is Free Software licensed under the GNU LGPL 2.1 + +RESOURCES +--------- +https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/ + +COPYING +------- +Copyright \(C) 2020 VMware, Inc. Free use of this software is granted under +the terms of the GNU Public License (GPL).
Signature · 1 line
2.30.2